#include <sys/types.h>
#include <sys/mman.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <debug.h>
#include <conv.h>
#include <elfcap.h>
#include "_rtld.h"
#include "_elf.h"
#include "_audit.h"
#include "msg.h"
static int
compare(const void *vp_a, const void *vp_b)
{
Fdesc *fdp_a = (Fdesc *)vp_a, *fdp_b = (Fdesc *)vp_b;
char *strcap_a, *strcap_b;
Xword hwcap_a, hwcap_b;
strcap_a = fdp_a->fd_scapset.sc_plat;
strcap_b = fdp_b->fd_scapset.sc_plat;
if (strcap_a && (strcap_b == NULL))
return (-1);
if (strcap_b && (strcap_a == NULL))
return (1);
strcap_a = fdp_a->fd_scapset.sc_mach;
strcap_b = fdp_b->fd_scapset.sc_mach;
if (strcap_a && (strcap_b == NULL))
return (-1);
if (strcap_b && (strcap_a == NULL))
return (1);
hwcap_a = fdp_a->fd_scapset.sc_hw_3;
hwcap_b = fdp_b->fd_scapset.sc_hw_3;
if (hwcap_a > hwcap_b)
return (-1);
if (hwcap_a < hwcap_b)
return (1);
hwcap_a = fdp_a->fd_scapset.sc_hw_2;
hwcap_b = fdp_b->fd_scapset.sc_hw_2;
if (hwcap_a > hwcap_b)
return (-1);
if (hwcap_a < hwcap_b)
return (1);
hwcap_a = fdp_a->fd_scapset.sc_hw_1;
hwcap_b = fdp_b->fd_scapset.sc_hw_1;
if (hwcap_a > hwcap_b)
return (-1);
if (hwcap_a < hwcap_b)
return (1);
DBG_CALL(Dbg_cap_identical(fdp_a->fd_lml, fdp_a->fd_nname,
fdp_b->fd_nname));
fdp_a->fd_flags |= FLG_FD_IGNORE;
fdp_b->fd_flags |= FLG_FD_IGNORE;
return (0);
}
int
hwcap1_check(Syscapset *scapset, Xword val, Rej_desc *rej)
{
Xword mval;
if ((rtld_flags2 & RT_FL2_HWCAP) &&
((mval = (val & ~scapset->sc_hw_1)) != 0)) {
if (rej) {
static Conv_cap_val_hw1_buf_t cap_buf;
rej->rej_type = SGS_REJ_HWCAP_1;
rej->rej_str = conv_cap_val_hw1(mval,
M_MACH, 0, &cap_buf);
}
return (0);
}
return (1);
}
int
hwcap2_check(Syscapset *scapset, Xword val, Rej_desc *rej)
{
Xword mval;
if ((mval = (val & ~scapset->sc_hw_2)) != 0) {
if (rej) {
static Conv_cap_val_hw2_buf_t cap_buf;
rej->rej_type = SGS_REJ_HWCAP_2;
rej->rej_str = conv_cap_val_hw2(mval,
M_MACH, 0, &cap_buf);
}
return (0);
}
return (1);
}
int
hwcap3_check(Syscapset *scapset, Xword val, Rej_desc *rej)
{
Xword mval;
if ((mval = (val & ~scapset->sc_hw_3)) != 0) {
if (rej) {
static Conv_cap_val_hw3_buf_t cap_buf;
rej->rej_type = SGS_REJ_HWCAP_3;
rej->rej_str = conv_cap_val_hw3(mval,
M_MACH, 0, &cap_buf);
}
return (0);
}
return (1);
}
int
sfcap1_check(Syscapset *scapset, Xword val, Rej_desc *rej)
{
#if defined(_ELF64)
if ((val & SF1_SUNW_ADDR32) && ((rtld_flags2 & RT_FL2_ADDR32) == 0)) {
if (rej) {
static Conv_cap_val_sf1_buf_t cap_buf;
rej->rej_type = SGS_REJ_SFCAP_1;
rej->rej_str = conv_cap_val_sf1(SF1_SUNW_ADDR32,
M_MACH, 0, &cap_buf);
}
return (0);
}
#endif
return (1);
}
int
platcap_check(Syscapset *scapset, const char *str, Rej_desc *rej)
{
if ((scapset->sc_plat == NULL) &&
(scapset->sc_platsz == 0))
platform_name(scapset);
if ((scapset->sc_plat == NULL) ||
(str && strcmp(scapset->sc_plat, str))) {
if (rej) {
rej->rej_type = SGS_REJ_PLATCAP;
rej->rej_str = stravl_insert(str, 0, 0, 0);
}
return (0);
}
return (1);
}
int
machcap_check(Syscapset *scapset, const char *str, Rej_desc *rej)
{
if ((scapset->sc_mach == NULL) &&
(scapset->sc_machsz == 0))
machine_name(scapset);
if ((scapset->sc_mach == NULL) ||
(str && strcmp(scapset->sc_mach, str))) {
if (rej) {
rej->rej_type = SGS_REJ_MACHCAP;
rej->rej_str = stravl_insert(str, 0, 0, 0);
}
return (0);
}
return (1);
}
static int
cap_check(Cap *cptr, char *strs, int alt, Fdesc *fdp, Rej_desc *rej)
{
Syscapset *scapset;
int totplat, ivlplat, totmach, ivlmach;
if (cptr == NULL)
return (1);
if (alt)
scapset = alt_scapset;
else
scapset = org_scapset;
totplat = ivlplat = totmach = ivlmach = 0;
while (cptr->c_tag != CA_SUNW_NULL) {
Xword val = cptr->c_un.c_val;
char *str;
switch (cptr->c_tag) {
case CA_SUNW_HW_1:
val &= ~AV_HW1_IGNORE;
if (hwcap1_check(scapset, val, rej) == 0)
return (0);
if (fdp)
fdp->fd_scapset.sc_hw_1 = val;
break;
case CA_SUNW_SF_1:
if (sfcap1_check(scapset, val, rej) == 0)
return (0);
if (fdp)
fdp->fd_scapset.sc_sf_1 = val;
break;
case CA_SUNW_HW_2:
if (hwcap2_check(scapset, val, rej) == 0)
return (0);
if (fdp)
fdp->fd_scapset.sc_hw_2 = val;
break;
case CA_SUNW_PLAT:
if (totplat == ivlplat) {
totplat++;
str = strs + val;
if (platcap_check(scapset, str, rej) == 0)
ivlplat++;
else if (fdp)
fdp->fd_scapset.sc_plat = str;
}
break;
case CA_SUNW_MACH:
if (totmach == ivlmach) {
totmach++;
str = strs + val;
if (machcap_check(scapset, str, rej) == 0)
ivlmach++;
else if (fdp)
fdp->fd_scapset.sc_mach = str;
}
break;
case CA_SUNW_ID:
break;
case CA_SUNW_HW_3:
if (hwcap3_check(scapset, val, rej) == 0)
return (0);
if (fdp)
fdp->fd_scapset.sc_hw_3 = val;
break;
default:
rej->rej_type = SGS_REJ_UNKCAP;
rej->rej_info = cptr->c_tag;
return (0);
}
cptr++;
}
if ((totplat && (totplat == ivlplat)) ||
(totmach && (totmach == ivlmach)))
return (0);
return (1);
}
#define HWAVL_RECORDED(n) pnavl_recorded(&capavl, n, 0, NULL)
static void
cap_check_lmp_init(Rt_map *lmp)
{
int alt = 0;
if (capavl) {
const char *file;
if ((file = strrchr(NAME(lmp), '/')) != NULL)
file++;
else
file = NULL;
if ((file && (HWAVL_RECORDED(file) != 0)) ||
(HWAVL_RECORDED(NAME(lmp)) != 0) ||
((PATHNAME(lmp) != NAME(lmp)) &&
(HWAVL_RECORDED(PATHNAME(lmp)) != 0)))
alt = 1;
if (alt == 0) {
Aliste idx;
const char *cp;
for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
if ((alt = HWAVL_RECORDED(cp)) != 0)
break;
}
}
}
if ((org_scapset != alt_scapset) && ((capavl == NULL) || alt))
FLAGS1(lmp) |= FL1_RT_ALTCAP;
FLAGS1(lmp) |= FL1_RT_ALTCHECK;
}
int
cap_check_lmp(Rt_map *lmp, Rej_desc *rej)
{
if ((FLAGS1(lmp) & FL1_RT_ALTCHECK) == 0)
cap_check_lmp_init(lmp);
return (cap_check(CAP(lmp), STRTAB(lmp),
(FLAGS1(lmp) & FL1_RT_ALTCAP), NULL, rej));
}
int
cap_check_fdesc(Fdesc *fdp, Cap *cptr, char *strs, Rej_desc *rej)
{
int alt = 0;
if (capavl) {
const char *file;
if (fdp->fd_oname &&
((file = strrchr(fdp->fd_oname, '/')) != NULL))
file++;
else
file = NULL;
if ((file && (HWAVL_RECORDED(file) != 0)) ||
(fdp->fd_oname && (HWAVL_RECORDED(fdp->fd_oname) != 0)) ||
(fdp->fd_nname && (HWAVL_RECORDED(fdp->fd_nname) != 0)) ||
(fdp->fd_pname && (fdp->fd_pname != fdp->fd_nname) &&
(HWAVL_RECORDED(fdp->fd_pname) != 0)))
alt = 1;
}
if ((org_scapset != alt_scapset) && ((capavl == NULL) || alt))
fdp->fd_flags |= FLG_FD_ALTCAP;
fdp->fd_flags |= FLG_FD_ALTCHECK;
return (cap_check(cptr, strs, (fdp->fd_flags & FLG_FD_ALTCAP),
fdp, rej));
}
void
free_fd(Alist *fdalp)
{
if (fdalp) {
Aliste idx;
Fdesc *fdp;
for (ALIST_TRAVERSE(fdalp, idx, fdp)) {
if (fdp->fd_oname)
free((void *)fdp->fd_oname);
}
free(fdalp);
}
}
static int
cap_dir(Alist **fdalpp, Lm_list *lml, const char *dname, Rt_map *clmp,
uint_t flags, Rej_desc *rej, int *in_nfavl)
{
char path[PATH_MAX], *dst;
const char *src;
DIR *dir;
struct dirent *dirent;
Alist *fdalp = NULL;
Aliste idx;
Fdesc *fdp;
int error = 0;
if ((dir = opendir(dname)) == NULL) {
Rej_desc _rej = { 0 };
_rej.rej_type = SGS_REJ_STR;
_rej.rej_name = dname;
_rej.rej_str = strerror(errno);
DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
rejection_inherit(rej, &_rej);
return (0);
}
for (dst = path, src = dname; *src; dst++, src++)
*dst = *src;
*dst++ = '/';
while ((dirent = readdir(dir)) != NULL) {
const char *file = dirent->d_name;
char *_dst;
Fdesc fd = { 0 };
Rej_desc _rej = { 0 };
Pdesc pd = { 0 };
if ((file[0] == '.') && ((file[1] == '\0') ||
((file[1] == '.') && (file[2] == '\0'))))
continue;
for (_dst = dst, src = file, file = dst; *src; _dst++, src++)
*_dst = *src;
*_dst = '\0';
pd.pd_pname = path;
pd.pd_flags = PD_FLG_PNSLASH;
if (load_trace(lml, &pd, clmp, &fd) == NULL)
continue;
if (find_path(lml, clmp, flags, &fd, &_rej, in_nfavl) == 0) {
rejection_inherit(rej, &_rej);
continue;
}
DBG_CALL(Dbg_cap_candidate(lml, fd.fd_nname));
if (fd.fd_lmp)
fd.fd_scapset = CAPSET(fd.fd_lmp);
fd.fd_lml = lml;
if (((fd.fd_oname = strdup(fd.fd_oname)) == NULL) ||
(alist_append(&fdalp, &fd, sizeof (Fdesc),
AL_CNT_CAP) == NULL)) {
error = 1;
break;
}
}
(void) closedir(dir);
if ((fdalp == NULL) || error) {
if (fdalp)
free_fd(fdalp);
return (0);
}
qsort(fdalp->al_data, fdalp->al_nitems, fdalp->al_size, compare);
for (ALIST_TRAVERSE(fdalp, idx, fdp)) {
if (fdp->fd_flags & FLG_FD_IGNORE)
alist_delete(fdalp, &idx);
}
if (fdalp->al_nitems == 0) {
free_fd(fdalp);
return (0);
}
*fdalpp = fdalp;
return (1);
}
int
cap_filtees(Alist **alpp, Aliste oidx, const char *dir, Aliste nlmco,
Rt_map *flmp, Rt_map *clmp, const char *ref, int mode, uint_t flags,
int *in_nfavl)
{
Alist *fdalp = NULL;
Aliste idx;
Fdesc *fdp;
Lm_list *lml = LIST(flmp);
int unused = 0;
Rej_desc rej = { 0 };
if (cap_dir(&fdalp, lml, dir, flmp, flags, &rej, in_nfavl) == 0)
return (0);
for (ALIST_TRAVERSE(fdalp, idx, fdp)) {
Rt_map *nlmp;
Grp_hdl *ghp = NULL;
Pdesc *pdp;
int audit = 0;
if (unused)
continue;
DBG_CALL(Dbg_file_filtee(lml, NAME(flmp), fdp->fd_nname, 0));
nlmp = load_path(lml, nlmco, flmp, mode,
(flags | FLG_RT_PUBHDL), &ghp, fdp, &rej, in_nfavl);
if (nlmp == NULL)
continue;
if ((pdp = alist_insert(alpp, 0, sizeof (Pdesc),
AL_CNT_FILTEES, ++oidx)) == NULL) {
if (ghp)
remove_lmc(lml, flmp, nlmco, NAME(nlmp));
return (0);
}
pdp->pd_pname = NAME(nlmp);
pdp->pd_plen = strlen(NAME(nlmp));
if (nlmp && ghp) {
ghp->gh_flags |= GPH_FILTEE;
pdp->pd_info = (void *)ghp;
}
if (nlmp && (lml->lm_tflags | FLAGS1(flmp)) &
LML_TFLG_AUD_OBJFILTER) {
if (audit_objfilter(flmp, ref, nlmp, 0) == 0) {
audit = 1;
nlmp = NULL;
}
}
if (nlmp && ghp && (((nlmp = analyze_lmc(lml, nlmco, nlmp,
clmp, in_nfavl)) == NULL) ||
(relocate_lmc(lml, nlmco, flmp, nlmp, in_nfavl) == 0)))
nlmp = NULL;
DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
if (nlmp && ghp &&
(hdl_add(ghp, flmp, GPD_FILTER, NULL) == NULL))
nlmp = NULL;
if (nlmp && ghp && (FLAGS1(nlmp) & FL1_RT_ENDFILTE))
unused = 1;
if (nlmp == NULL) {
DBG_CALL(Dbg_file_filtee(lml, 0, pdp->pd_pname, audit));
if (nlmco != ALIST_OFF_DATA)
remove_lmc(lml, flmp, nlmco, pdp->pd_pname);
pdp->pd_plen = 0;
pdp->pd_info = NULL;
}
}
free_fd(fdalp);
return (1);
}
Rt_map *
load_cap(Lm_list *lml, Aliste lmco, const char *dir, Rt_map *clmp,
uint_t mode, uint_t flags, Grp_hdl **hdl, Rej_desc *rej, int *in_nfavl)
{
Alist *fdalp = NULL;
Aliste idx;
Fdesc *fdp;
int found = 0;
Rt_map *lmp = NULL;
if (cap_dir(&fdalp, lml, dir, clmp, flags, rej, in_nfavl) == 0)
return (NULL);
for (ALIST_TRAVERSE(fdalp, idx, fdp)) {
Fdesc fd = *fdp;
if ((found == 0) && ((lmp = load_path(lml, lmco, clmp, mode,
flags, hdl, &fd, rej, in_nfavl)) != NULL))
found++;
}
free_fd(fdalp);
return (lmp);
}
#define ELFCAP_STYLE ELFCAP_STYLE_LC | ELFCAP_STYLE_F_ICMP
typedef enum {
CAP_OVERRIDE = 0,
CAP_ENABLE = 1,
CAP_DISABLE = 2
} cap_mode;
typedef enum {
CAP_OVR_HW_1 = 0,
CAP_OVR_SF_1,
CAP_OVR_HW_2,
CAP_OVR_HW_3,
CAP_OVR_MAX
} cap_index_t;
static struct {
elfcap_mask_t cs_val[3];
int cs_set[3];
elfcap_mask_t *cs_aval;
} cap_settings[CAP_OVR_MAX] = {
{ { 0, 0, 0 }, { 0, 0, 0 }, NULL },
{ { 0, 0, 0 }, { 0, 0, 0 }, NULL },
{ { 0, 0, 0 }, { 0, 0, 0 }, NULL },
{ { 0, 0, 0 }, { 0, 0, 0 }, NULL }
};
static int
cap_modify(Xword tag, const char *str)
{
char *caps, *ptr, *next;
cap_mode mode = CAP_OVERRIDE;
cap_index_t ndx;
if ((caps = strdup(str)) == NULL)
return (0);
for (ptr = strtok_r(caps, MSG_ORIG(MSG_CAP_DELIMIT), &next);
ptr != NULL;
ptr = strtok_r(NULL, MSG_ORIG(MSG_CAP_DELIMIT), &next)) {
Xword val = 0;
if (*ptr == '+') {
mode = CAP_ENABLE;
ptr++;
} else if (*ptr == '-') {
mode = CAP_DISABLE;
ptr++;
}
switch (tag) {
case CA_SUNW_HW_1:
if ((val = (Xword)elfcap_hw3_from_str(ELFCAP_STYLE,
ptr, M_MACH)) != 0) {
ndx = CAP_OVR_HW_3;
break;
}
if ((val = (Xword)elfcap_hw2_from_str(ELFCAP_STYLE,
ptr, M_MACH)) != 0) {
ndx = CAP_OVR_HW_2;
break;
}
if ((val = (Xword)elfcap_hw1_from_str(ELFCAP_STYLE,
ptr, M_MACH)) != 0)
ndx = CAP_OVR_HW_1;
break;
case CA_SUNW_SF_1:
if ((val = (Xword)elfcap_sf1_from_str(ELFCAP_STYLE,
ptr, M_MACH)) != 0)
ndx = CAP_OVR_SF_1;
break;
}
if (val == 0) {
char *end;
if ((*ptr == '[') && (*(ptr + 2) == ']')) {
if (*(ptr + 1) == '1') {
ndx = CAP_OVR_HW_1;
ptr += 3;
} else if (*(ptr + 1) == '3') {
if (tag == CA_SUNW_HW_1) {
ndx = CAP_OVR_HW_3;
ptr += 3;
} else {
continue;
}
} else if (*(ptr + 1) == '2') {
if (tag == CA_SUNW_HW_1) {
ndx = CAP_OVR_HW_2;
ptr += 3;
} else {
continue;
}
} else {
continue;
}
} else {
ndx = tag - 1;
}
errno = 0;
if (((val = strtol(ptr, &end, 16)) == 0) && errno)
continue;
if (*end != '\0') {
eprintf(NULL, ERR_WARNING,
MSG_INTL(MSG_CAP_IGN_UNKCAP), ptr);
continue;
}
}
cap_settings[ndx].cs_val[mode] |= val;
cap_settings[ndx].cs_set[mode]++;
}
for (ndx = 0; ndx < CAP_OVR_MAX; ndx++) {
if (cap_settings[ndx].cs_set[CAP_OVERRIDE])
*(cap_settings[ndx].cs_aval) =
cap_settings[ndx].cs_val[CAP_OVERRIDE];
if (cap_settings[ndx].cs_set[CAP_ENABLE])
*(cap_settings[ndx].cs_aval) |=
cap_settings[ndx].cs_val[CAP_ENABLE];
if (cap_settings[ndx].cs_set[CAP_DISABLE])
*(cap_settings[ndx].cs_aval) &=
~cap_settings[ndx].cs_val[CAP_DISABLE];
}
free(caps);
return (1);
}
#undef ELFCAP_STYLE
static int
cap_files(const char *str)
{
char *caps, *name, *next;
if ((caps = strdup(str)) == NULL)
return (0);
for (name = strtok_r(caps, MSG_ORIG(MSG_CAP_DELIMIT), &next);
name != NULL;
name = strtok_r(NULL, MSG_ORIG(MSG_CAP_DELIMIT), &next)) {
avl_index_t where;
PathNode *pnp;
uint_t hash = sgs_str_hash(name);
if (pnavl_recorded(&capavl, name, hash, &where))
continue;
if ((pnp = calloc(1, sizeof (PathNode))) != NULL) {
pnp->pn_name = name;
pnp->pn_hash = hash;
avl_insert(capavl, pnp, where);
}
}
return (1);
}
int
cap_alternative(void)
{
if ((rpl_hwcap == NULL) && (rpl_sfcap == NULL) &&
(rpl_machcap == NULL) && (rpl_platcap == NULL) &&
(prm_hwcap == NULL) && (prm_sfcap == NULL) &&
(prm_machcap == NULL) && (prm_platcap == NULL))
return (1);
if ((alt_scapset = malloc(sizeof (Syscapset))) == NULL)
return (0);
*alt_scapset = *org_scapset;
cap_settings[CAP_OVR_HW_1].cs_aval = &alt_scapset->sc_hw_1;
cap_settings[CAP_OVR_SF_1].cs_aval = &alt_scapset->sc_sf_1;
cap_settings[CAP_OVR_HW_2].cs_aval = &alt_scapset->sc_hw_2;
cap_settings[CAP_OVR_HW_3].cs_aval = &alt_scapset->sc_hw_3;
if (rpl_hwcap && (cap_modify(CA_SUNW_HW_1, rpl_hwcap) == 0))
return (0);
if (rpl_sfcap && (cap_modify(CA_SUNW_SF_1, rpl_sfcap) == 0))
return (0);
if (rpl_platcap) {
alt_scapset->sc_plat = (char *)rpl_platcap;
alt_scapset->sc_platsz = strlen(rpl_platcap);
}
if (rpl_machcap) {
alt_scapset->sc_mach = (char *)rpl_machcap;
alt_scapset->sc_machsz = strlen(rpl_machcap);
}
if (rpl_cap_files && (cap_files(rpl_cap_files) == 0))
return (0);
if (prm_hwcap && (cap_modify(CA_SUNW_HW_1, prm_hwcap) == 0))
return (0);
if (prm_sfcap && (cap_modify(CA_SUNW_SF_1, prm_sfcap) == 0))
return (0);
if (prm_platcap) {
alt_scapset->sc_plat = (char *)prm_platcap;
alt_scapset->sc_platsz = strlen(prm_platcap);
}
if (prm_machcap) {
alt_scapset->sc_mach = (char *)prm_machcap;
alt_scapset->sc_machsz = strlen(prm_machcap);
}
if (prm_cap_files && (cap_files(prm_cap_files) == 0))
return (0);
rpl_hwcap = rpl_sfcap = rpl_machcap = rpl_platcap =
rpl_cap_files = NULL;
return (1);
}
static int
sym_cap_check(Cap *cptr, uint_t cndx, Syscapset *bestcapset, Rt_map *lmp,
const char *name, uint_t ndx)
{
Syscapset *scapset;
int totplat, ivlplat, totmach, ivlmach, capfail = 0;
if ((FLAGS1(lmp) & FL1_RT_ALTCHECK) == 0)
cap_check_lmp_init(lmp);
if (FLAGS1(lmp) & FL1_RT_ALTCAP)
scapset = alt_scapset;
else
scapset = org_scapset;
totplat = ivlplat = totmach = ivlmach = 0;
for (cptr += cndx; cptr->c_tag != CA_SUNW_NULL; cptr++) {
Xword val = cptr->c_un.c_val;
char *str;
switch (cptr->c_tag) {
case CA_SUNW_HW_1:
val &= ~AV_HW1_IGNORE;
bestcapset->sc_hw_1 = val;
DBG_CALL(Dbg_syms_cap_lookup(lmp, DBG_CAP_HW_1,
name, ndx, M_MACH, bestcapset));
if (hwcap1_check(scapset, val, NULL) == 0)
capfail++;
break;
case CA_SUNW_SF_1:
bestcapset->sc_sf_1 = val;
DBG_CALL(Dbg_syms_cap_lookup(lmp, DBG_CAP_SF_1,
name, ndx, M_MACH, bestcapset));
if (sfcap1_check(scapset, val, NULL) == 0)
capfail++;
break;
case CA_SUNW_HW_2:
bestcapset->sc_hw_2 = val;
DBG_CALL(Dbg_syms_cap_lookup(lmp, DBG_CAP_HW_2,
name, ndx, M_MACH, bestcapset));
if (hwcap2_check(scapset, val, NULL) == 0)
capfail++;
break;
case CA_SUNW_PLAT:
if (totplat == ivlplat) {
totplat++;
str = STRTAB(lmp) + val;
bestcapset->sc_plat = str;
DBG_CALL(Dbg_syms_cap_lookup(lmp, DBG_CAP_PLAT,
name, ndx, M_MACH, bestcapset));
if (platcap_check(scapset, str, NULL) == 0)
ivlplat++;
}
break;
case CA_SUNW_MACH:
if (totmach == ivlmach) {
totmach++;
str = STRTAB(lmp) + val;
bestcapset->sc_mach = str;
DBG_CALL(Dbg_syms_cap_lookup(lmp, DBG_CAP_MACH,
name, ndx, M_MACH, bestcapset));
if (machcap_check(scapset, str, NULL) == 0)
ivlmach++;
}
break;
case CA_SUNW_HW_3:
bestcapset->sc_hw_3 = val;
DBG_CALL(Dbg_syms_cap_lookup(lmp, DBG_CAP_HW_3,
name, ndx, M_MACH, bestcapset));
if (hwcap3_check(scapset, val, NULL) == 0)
capfail++;
break;
default:
break;
}
}
if (capfail || (totplat && (totplat == ivlplat)) ||
(totmach && (totmach == ivlmach))) {
DBG_CALL(Dbg_syms_cap_lookup(lmp, DBG_CAP_REJECTED, name, ndx,
M_MACH, NULL));
return (0);
}
DBG_CALL(Dbg_syms_cap_lookup(lmp, DBG_CAP_CANDIDATE, name, ndx,
M_MACH, NULL));
return (1);
}
inline static int
is_sym_the_best(Syscapset *bestcapset, Syscapset *symcapset)
{
if (bestcapset->sc_plat && (symcapset->sc_plat == NULL))
return (0);
if ((bestcapset->sc_plat == NULL) && symcapset->sc_plat)
return (1);
if (bestcapset->sc_mach && (symcapset->sc_mach == NULL))
return (0);
if ((bestcapset->sc_mach == NULL) && symcapset->sc_mach)
return (1);
if (bestcapset->sc_hw_3 > symcapset->sc_hw_3)
return (0);
if (bestcapset->sc_hw_3 < symcapset->sc_hw_3)
return (1);
if (bestcapset->sc_hw_2 > symcapset->sc_hw_2)
return (0);
if (bestcapset->sc_hw_2 < symcapset->sc_hw_2)
return (1);
if (bestcapset->sc_hw_1 > symcapset->sc_hw_1)
return (0);
if (bestcapset->sc_hw_1 < symcapset->sc_hw_1)
return (1);
return (0);
}
int
cap_match(Sresult *srp, uint_t symndx, Sym *symtabptr, char *strtabptr)
{
Rt_map *ilmp = srp->sr_dmap;
Sym *bsym = NULL;
const char *bname;
Syscapset bestcapset = { 0 };
Cap *cap;
Capchain *capchain;
uchar_t grpndx;
uint_t ochainndx, nchainndx, bndx;
cap = CAP(ilmp);
capchain = CAPCHAIN(ilmp);
grpndx = (uchar_t)ELF_C_GROUP(CAPINFO(ilmp)[symndx]);
if (grpndx != CAPINFO_SUNW_GLOB) {
Syscapset symcapset = { 0 };
return (sym_cap_check(cap, grpndx, &symcapset, ilmp,
srp->sr_name, symndx));
}
if (capchain == NULL)
return (1);
ochainndx = (uint_t)ELF_C_SYM(CAPINFO(ilmp)[symndx]);
if (capchain[ochainndx] && (capchain[ochainndx + 1] == 0)) {
Sym *fsym = symtabptr + capchain[ochainndx];
const char *fname = strtabptr + fsym->st_name;
DBG_CALL(Dbg_syms_cap_lookup(ilmp, DBG_CAP_USED, fname,
capchain[ochainndx], M_MACH, NULL));
srp->sr_sym = fsym;
srp->sr_name = fname;
return (1);
}
DBG_CALL(Dbg_syms_cap_lookup(ilmp, DBG_CAP_DEFAULT, srp->sr_name,
symndx, M_MACH, NULL));
bsym = srp->sr_sym;
bname = srp->sr_name;
bndx = symndx;
for (nchainndx = ochainndx + 1, symndx = capchain[nchainndx]; symndx;
nchainndx++, symndx = capchain[nchainndx]) {
Sym *nsym = symtabptr + symndx;
const char *nname = strtabptr + nsym->st_name;
Syscapset symcapset = { 0 };
if ((grpndx =
(uchar_t)ELF_C_GROUP(CAPINFO(ilmp)[symndx])) == 0)
continue;
if (sym_cap_check(cap, grpndx, &symcapset, ilmp,
nname, symndx) == 0)
continue;
if (is_sym_the_best(&bestcapset, &symcapset)) {
bestcapset = symcapset;
bsym = nsym;
bname = nname;
bndx = symndx;
}
}
DBG_CALL(Dbg_syms_cap_lookup(ilmp, DBG_CAP_USED, bname, bndx,
M_MACH, NULL));
capchain[ochainndx] = bndx;
capchain[ochainndx + 1] = 0;
srp->sr_sym = bsym;
srp->sr_name = bname;
return (1);
}