#define ELF_TARGET_AMD64
#define ELF_TARGET_SPARC
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <link.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/systeminfo.h>
#include <debug.h>
#include <msg.h>
#include <_libld.h>
static int
ifl_verify(Ehdr *ehdr, Ofl_desc *ofl, Rej_desc *rej)
{
if ((ehdr->e_machine != ld_targ.t_m.m_mach) &&
((ehdr->e_machine != ld_targ.t_m.m_machplus) &&
((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0))) {
rej->rej_type = SGS_REJ_MACH;
rej->rej_info = (uint_t)ehdr->e_machine;
return (0);
}
if (ehdr->e_ident[EI_DATA] != ld_targ.t_m.m_data) {
rej->rej_type = SGS_REJ_DATA;
rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
return (0);
}
if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
rej->rej_type = SGS_REJ_VERSION;
rej->rej_info = (uint_t)ehdr->e_version;
return (0);
}
return (1);
}
static Ifl_desc *
ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl,
Rej_desc *rej)
{
Ifl_desc *ifl;
Rej_desc _rej = { 0 };
if (ifl_verify(ehdr, ofl, &_rej) == 0) {
_rej.rej_name = name;
DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
ld_targ.t_m.m_mach));
if (rej->rej_type == 0) {
*rej = _rej;
rej->rej_name = strdup(_rej.rej_name);
}
return (0);
}
if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == NULL)
return ((Ifl_desc *)S_ERROR);
ifl->ifl_name = name;
ifl->ifl_ehdr = ehdr;
ifl->ifl_elf = elf;
ifl->ifl_flags = flags;
if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
Elf_Scn *scn;
Shdr *shdr0;
if ((scn = elf_getscn(elf, 0)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
name);
return ((Ifl_desc *)S_ERROR);
}
if ((shdr0 = elf_getshdr(scn)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
name);
return ((Ifl_desc *)S_ERROR);
}
ifl->ifl_shnum = (Word)shdr0->sh_size;
if (ehdr->e_shstrndx == SHN_XINDEX)
ifl->ifl_shstrndx = shdr0->sh_link;
else
ifl->ifl_shstrndx = ehdr->e_shstrndx;
} else {
ifl->ifl_shnum = ehdr->e_shnum;
ifl->ifl_shstrndx = ehdr->e_shstrndx;
}
if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
sizeof (Is_desc *))) == NULL)
return ((Ifl_desc *)S_ERROR);
if (ifl->ifl_ehdr->e_type == ET_DYN) {
if (aplist_append(&ofl->ofl_sos, ifl, AL_CNT_OFL_LIBS) == NULL)
return ((Ifl_desc *)S_ERROR);
} else {
if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
return ((Ifl_desc *)S_ERROR);
}
return (ifl);
}
static inline Boolean
section_is_exclude(Ofl_desc *ofl, Shdr *shdr)
{
if (shdr->sh_flags & SHF_EXCLUDE) {
if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
return (TRUE);
if (ofl->ofl_flags & FLG_OF_KMOD)
return (TRUE);
}
return (FALSE);
}
static uintptr_t
process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
Is_desc *isp;
if ((isp = libld_calloc(1, sizeof (Is_desc))) == NULL)
return (S_ERROR);
isp->is_shdr = shdr;
isp->is_file = ifl;
isp->is_name = name;
isp->is_scnndx = ndx;
isp->is_flags = FLG_IS_EXTERNAL;
isp->is_keyident = ident;
if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
ifl->ifl_name);
return (0);
}
if (section_is_exclude(ofl, shdr))
isp->is_flags |= FLG_IS_DISCARD;
ifl->ifl_isdesc[ndx] = isp;
if (ident) {
if (shdr->sh_flags & ALL_SHF_ORDER) {
isp->is_flags |= FLG_IS_ORDERED;
ifl->ifl_flags |= FLG_IF_ORDERED;
}
isp->is_flags |= FLG_IS_PLACE;
}
return (1);
}
static void
sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, Is_desc *cisp)
{
#define FP_FLAGS (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)
Xword badval;
if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP1) {
DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
CA_SUNW_SF_1, val, ld_targ.t_m.m_mach));
return;
}
#if !defined(_ELF64)
if (ifl && (ifl->ifl_ehdr->e_type == ET_REL)) {
if (val & SF1_SUNW_ADDR32) {
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(MSG_FIL_INADDR32SF1), ifl->ifl_name,
EC_WORD(cisp->is_scnndx), cisp->is_name);
val &= ~SF1_SUNW_ADDR32;
}
}
#endif
if (val == 0)
return;
if ((badval = (val & ~SF1_SUNW_MASK)) != 0) {
ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
EC_XWORD(badval));
val &= SF1_SUNW_MASK;
}
if ((val & FP_FLAGS) == SF1_SUNW_FPUSED) {
ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
EC_XWORD(val));
return;
}
if (ifl->ifl_ehdr->e_type != ET_REL) {
#if defined(_ELF64)
if ((val & SF1_SUNW_ADDR32) && (ofl->ofl_flags & FLG_OF_EXEC) &&
((ofl->ofl_ocapset.oc_sf_1.cm_val &
SF1_SUNW_ADDR32) == 0)) {
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(MSG_FIL_EXADDR32SF1), ifl->ifl_name,
EC_WORD(cisp->is_scnndx), cisp->is_name);
}
#endif
return;
}
if (DBG_ENABLED) {
Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_SF_1,
ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach);
Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_SF_1,
val, ld_targ.t_m.m_mach);
}
if ((ofl->ofl_ocapset.oc_sf_1.cm_val & FP_FLAGS) == FP_FLAGS) {
if ((val & FP_FLAGS) != FP_FLAGS)
ofl->ofl_ocapset.oc_sf_1.cm_val &= ~SF1_SUNW_FPUSED;
val &= ~FP_FLAGS;
} else if ((ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_FPKNWN) == 0) {
ofl->ofl_ocapset.oc_sf_1.cm_val &= ~FP_FLAGS;
} else {
val &= ~FP_FLAGS;
}
ofl->ofl_ocapset.oc_sf_1.cm_val |= val;
DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
CA_SUNW_SF_1, ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
#undef FP_FLAGS
}
static void
hw_cap(Ofl_desc *ofl, Xword tag, Xword val)
{
elfcap_mask_t *hwcap;
ofl_flag_t flags1;
switch (tag) {
case CA_SUNW_HW_1:
hwcap = &ofl->ofl_ocapset.oc_hw_1.cm_val;
flags1 = FLG_OF1_OVHWCAP1;
break;
case CA_SUNW_HW_2:
hwcap = &ofl->ofl_ocapset.oc_hw_2.cm_val;
flags1 = FLG_OF1_OVHWCAP2;
break;
case CA_SUNW_HW_3:
hwcap = &ofl->ofl_ocapset.oc_hw_3.cm_val;
flags1 = FLG_OF1_OVHWCAP3;
break;
default:
assert(0);
}
if (ofl->ofl_flags1 & flags1) {
DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
tag, val, ld_targ.t_m.m_mach));
return;
}
if (val == 0)
return;
if (DBG_ENABLED) {
Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_HW_1,
ofl->ofl_ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach);
Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_HW_1,
val, ld_targ.t_m.m_mach);
}
*hwcap |= val;
DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED, tag,
*hwcap, ld_targ.t_m.m_mach));
}
static void
str_cap(Ofl_desc *ofl, char *pstr, ofl_flag_t flags, Xword tag, Caplist *list)
{
Capstr *capstr;
Aliste idx;
Boolean found = FALSE;
if (ofl->ofl_flags1 & flags) {
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
tag, pstr));
return;
}
for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
DBG_STATE_CURRENT, tag, capstr->cs_str));
if (strcmp(capstr->cs_str, pstr) == 0)
found = TRUE;
}
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW, tag, pstr));
if (found == FALSE) {
if ((capstr = alist_append(&list->cl_val, NULL,
sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL) {
ofl->ofl_flags |= FLG_OF_FATAL;
return;
}
capstr->cs_str = pstr;
}
if (DBG_ENABLED) {
for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
DBG_STATE_RESOLVED, tag, capstr->cs_str));
}
}
}
static void
id_cap(Ofl_desc *ofl, char *pstr, oc_flag_t flags)
{
Objcapset *ocapset = &ofl->ofl_ocapset;
if (ocapset->oc_id.cs_str) {
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_CURRENT,
CA_SUNW_ID, ocapset->oc_id.cs_str));
if ((ocapset->oc_flags & FLG_OCS_USRDEFID) ||
((flags & FLG_OCS_USRDEFID) == 0)) {
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
DBG_STATE_IGNORED, CA_SUNW_ID, pstr));
return;
}
}
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW,
CA_SUNW_ID, pstr));
ocapset->oc_id.cs_str = pstr;
ocapset->oc_flags |= flags;
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
CA_SUNW_ID, pstr));
}
void
ld_cap_move_symtoobj(Ofl_desc *ofl)
{
Cap_group *cgp;
Aliste idx1;
for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
Objcapset *scapset = &cgp->cg_set;
Capstr *capstr;
Aliste idx2;
if (scapset->oc_id.cs_str) {
if (scapset->oc_flags & FLG_OCS_USRDEFID)
id_cap(ofl, scapset->oc_id.cs_str,
scapset->oc_flags);
}
if (scapset->oc_plat.cl_val) {
for (ALIST_TRAVERSE(scapset->oc_plat.cl_val, idx2,
capstr)) {
str_cap(ofl, capstr->cs_str, FLG_OF1_OVPLATCAP,
CA_SUNW_PLAT, &ofl->ofl_ocapset.oc_plat);
}
}
if (scapset->oc_mach.cl_val) {
for (ALIST_TRAVERSE(scapset->oc_mach.cl_val, idx2,
capstr)) {
str_cap(ofl, capstr->cs_str, FLG_OF1_OVMACHCAP,
CA_SUNW_MACH, &ofl->ofl_ocapset.oc_mach);
}
}
if (scapset->oc_hw_3.cm_val)
hw_cap(ofl, CA_SUNW_HW_3, scapset->oc_hw_3.cm_val);
if (scapset->oc_hw_2.cm_val)
hw_cap(ofl, CA_SUNW_HW_2, scapset->oc_hw_2.cm_val);
if (scapset->oc_hw_1.cm_val)
hw_cap(ofl, CA_SUNW_HW_1, scapset->oc_hw_1.cm_val);
if (scapset->oc_sf_1.cm_val)
sf1_cap(ofl, scapset->oc_sf_1.cm_val, NULL, NULL);
}
}
static Cap_group *
get_cap_group(Objcapset *ocapset, Word cnum, Ofl_desc *ofl, Is_desc *isp)
{
Aliste idx;
Cap_group *cgp;
Word ccnum = cnum;
if (ocapset->oc_id.cs_str)
ccnum--;
for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx, cgp)) {
Word onum = cgp->cg_num;
Alist *calp, *oalp;
if (cgp->cg_set.oc_id.cs_str)
onum--;
if (onum != ccnum)
continue;
if (cgp->cg_set.oc_hw_1.cm_val != ocapset->oc_hw_1.cm_val)
continue;
if (cgp->cg_set.oc_sf_1.cm_val != ocapset->oc_sf_1.cm_val)
continue;
if (cgp->cg_set.oc_hw_2.cm_val != ocapset->oc_hw_2.cm_val)
continue;
if (cgp->cg_set.oc_hw_3.cm_val != ocapset->oc_hw_3.cm_val)
continue;
calp = cgp->cg_set.oc_plat.cl_val;
oalp = ocapset->oc_plat.cl_val;
if ((calp == NULL) && oalp)
continue;
if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
continue;
calp = cgp->cg_set.oc_mach.cl_val;
oalp = ocapset->oc_mach.cl_val;
if ((calp == NULL) && oalp)
continue;
if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
continue;
if (aplist_append(&(cgp->cg_secs), isp,
AL_CNT_CAP_SECS) == NULL)
return (NULL);
return (cgp);
}
if (((cgp = libld_calloc(1, sizeof (Cap_group))) == NULL) ||
(aplist_append(&(ofl->ofl_capgroups), cgp,
AL_CNT_CAP_DESCS) == NULL))
return (NULL);
if ((isp->is_file->ifl_flags & FLG_IF_OTOSCAP) &&
(ocapset->oc_id.cs_str == NULL)) {
size_t len;
len = MSG_STR_CAPGROUPID_SIZE + CONV_INV_BUFSIZE;
if ((ocapset->oc_id.cs_str = libld_malloc(len)) == NULL)
return (NULL);
(void) snprintf(ocapset->oc_id.cs_str, len,
MSG_ORIG(MSG_STR_CAPGROUPID),
aplist_nitems(ofl->ofl_capgroups));
cnum++;
}
cgp->cg_set = *ocapset;
cgp->cg_num = cnum;
ocapset->oc_plat.cl_val = ocapset->oc_mach.cl_val = NULL;
if (aplist_append(&(cgp->cg_secs), isp, AL_CNT_CAP_SECS) == NULL)
return (NULL);
ofl->ofl_capsymcnt += (cnum + 1);
return (cgp);
}
uintptr_t
ld_cap_add_family(Ofl_desc *ofl, Sym_desc *lsdp, Sym_desc *csdp, Cap_group *cgp,
APlist **csyms)
{
Cap_avlnode qcav, *cav;
avl_tree_t *avlt;
avl_index_t where = 0;
Cap_sym *mcsp;
Aliste idx;
if ((avlt = ofl->ofl_capfamilies) == NULL) {
if ((avlt = libld_calloc(1, sizeof (avl_tree_t))) == NULL)
return (S_ERROR);
avl_create(avlt, &ld_sym_avl_comp, sizeof (Cap_avlnode),
SGSOFFSETOF(Cap_avlnode, cn_symavlnode.sav_node));
ofl->ofl_capfamilies = avlt;
ofl->ofl_capchaincnt = 1;
}
qcav.cn_symavlnode.sav_hash = (Word)elf_hash(lsdp->sd_name);
qcav.cn_symavlnode.sav_name = lsdp->sd_name;
if ((cav = avl_find(avlt, &qcav, &where)) == NULL) {
if ((cav = libld_calloc(1, sizeof (Cap_avlnode))) == NULL)
return (S_ERROR);
cav->cn_symavlnode.sav_hash = qcav.cn_symavlnode.sav_hash;
cav->cn_symavlnode.sav_name = qcav.cn_symavlnode.sav_name;
cav->cn_symavlnode.sav_sdp = lsdp;
avl_insert(avlt, cav, where);
ofl->ofl_capchaincnt += 2;
}
if (cgp == NULL) {
if ((lsdp != csdp) && (aplist_append(&cav->cn_aliases, csdp,
AL_CNT_CAP_ALIASES) == NULL))
return (S_ERROR);
return (0);
}
for (APLIST_TRAVERSE(cav->cn_members, idx, mcsp)) {
Sym_desc *msdp;
if (cgp != mcsp->cs_group)
continue;
msdp = mcsp->cs_sdp;
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_CAP_MULDEF),
demangle(lsdp->sd_name));
ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_CAP_MULDEFSYMS),
msdp->sd_file->ifl_name, msdp->sd_name,
csdp->sd_file->ifl_name, csdp->sd_name);
}
if (((mcsp = libld_malloc(sizeof (Cap_sym))) == NULL) ||
(aplist_append(&cav->cn_members, mcsp, AL_CNT_CAP_MEMS) == NULL))
return (S_ERROR);
mcsp->cs_sdp = csdp;
mcsp->cs_group = cgp;
ofl->ofl_capchaincnt++;
if (csyms && (aplist_append(csyms, mcsp, AL_CNT_CAP_SYMS) == NULL))
return (S_ERROR);
return (0);
}
static uintptr_t
process_cap(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *cisp)
{
Objcapset ocapset = { 0 };
Cap_desc *cdp;
Cap *data, *cdata;
char *strs;
Word ndx, cnum;
int objcapndx, descapndx, symcapndx;
int nulls, capstrs = 0;
cdata = (Cap *)cisp->is_indata->d_buf;
cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
if ((cdata == NULL) || (cnum == 0))
return (0);
DBG_CALL(Dbg_cap_sec_title(ofl->ofl_lml, ifl->ifl_name));
objcapndx = 0;
descapndx = symcapndx = -1;
nulls = 0;
for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
switch (data->c_tag) {
case CA_SUNW_NULL:
if (nulls++ == 0) {
if (ndx == 0)
objcapndx = -1;
} else if ((symcapndx == -1) && (descapndx != -1))
symcapndx = descapndx;
break;
case CA_SUNW_PLAT:
case CA_SUNW_MACH:
case CA_SUNW_ID:
capstrs++;
case CA_SUNW_HW_1:
case CA_SUNW_SF_1:
case CA_SUNW_HW_2:
case CA_SUNW_HW_3:
if (descapndx == -1)
descapndx = ndx;
break;
default:
ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_UNKCAP),
ifl->ifl_name, EC_WORD(cisp->is_scnndx),
cisp->is_name, data->c_tag);
}
}
if (capstrs) {
Word info = cisp->is_shdr->sh_info;
if ((info == 0) || (info > ifl->ifl_shnum)) {
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
ifl->ifl_name, EC_WORD(cisp->is_scnndx),
cisp->is_name, EC_XWORD(info));
return (S_ERROR);
}
strs = (char *)ifl->ifl_isdesc[info]->is_indata->d_buf;
}
if ((objcapndx == 0) &&
((symcapndx != -1) || ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))) {
for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
if (data->c_tag == CA_SUNW_NULL)
break;
if (data->c_tag == CA_SUNW_SF_1) {
sf1_cap(ofl, data->c_un.c_val, ifl, cisp);
continue;
}
if (ifl->ifl_ehdr->e_type != ET_REL)
continue;
switch (data->c_tag) {
case CA_SUNW_HW_1:
case CA_SUNW_HW_2:
hw_cap(ofl, data->c_tag, data->c_un.c_val);
break;
case CA_SUNW_PLAT:
str_cap(ofl, strs + data->c_un.c_ptr,
FLG_OF1_OVPLATCAP, CA_SUNW_PLAT,
&ofl->ofl_ocapset.oc_plat);
break;
case CA_SUNW_MACH:
str_cap(ofl, strs + data->c_un.c_ptr,
FLG_OF1_OVMACHCAP, CA_SUNW_MACH,
&ofl->ofl_ocapset.oc_mach);
break;
case CA_SUNW_ID:
id_cap(ofl, strs + data->c_un.c_ptr,
FLG_OCS_USRDEFID);
break;
default:
assert(0);
}
}
if ((symcapndx == -1) &&
((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))
return (1);
}
if (ifl->ifl_ehdr->e_type != ET_REL)
return (1);
if ((objcapndx == 0) &&
(symcapndx == -1) && (ofl->ofl_flags & FLG_OF_OTOSCAP))
ifl->ifl_flags |= FLG_IF_OTOSCAP;
if (((cdp = libld_calloc(1, sizeof (Cap_desc))) == NULL) ||
(aplist_append(&(cdp->ca_groups), NULL, cnum) == NULL))
return (S_ERROR);
(void) memset(&cdp->ca_groups->apl_data[0], 0,
(cnum * sizeof (void *)));
cdp->ca_groups->apl_nitems = cnum;
ifl->ifl_caps = cdp;
descapndx = -1;
nulls = 0;
for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
Capstr *capstr;
switch (data->c_tag) {
case CA_SUNW_NULL:
nulls++;
if (descapndx != -1) {
if ((nulls > 1) ||
(ifl->ifl_flags & FLG_IF_OTOSCAP)) {
APlist *alp = cdp->ca_groups;
if ((alp->apl_data[descapndx] =
get_cap_group(&ocapset,
(ndx - descapndx), ofl,
cisp)) == NULL)
return (S_ERROR);
}
ocapset.oc_hw_1.cm_val =
ocapset.oc_sf_1.cm_val =
ocapset.oc_hw_2.cm_val =
ocapset.oc_hw_3.cm_val = 0;
if (ocapset.oc_plat.cl_val) {
free((void *)ocapset.oc_plat.cl_val);
ocapset.oc_plat.cl_val = NULL;
}
if (ocapset.oc_mach.cl_val) {
free((void *)ocapset.oc_mach.cl_val);
ocapset.oc_mach.cl_val = NULL;
}
descapndx = -1;
}
continue;
case CA_SUNW_HW_1:
ocapset.oc_hw_1.cm_val = data->c_un.c_val;
DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
DBG_STATE_ORIGINAL, CA_SUNW_HW_1,
ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach));
break;
case CA_SUNW_SF_1:
ocapset.oc_sf_1.cm_val = data->c_un.c_val;
DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
DBG_STATE_ORIGINAL, CA_SUNW_SF_1,
ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
break;
case CA_SUNW_HW_2:
ocapset.oc_hw_2.cm_val = data->c_un.c_val;
DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
DBG_STATE_ORIGINAL, CA_SUNW_HW_2,
ocapset.oc_hw_2.cm_val, ld_targ.t_m.m_mach));
break;
case CA_SUNW_PLAT:
if ((capstr = alist_append(&ocapset.oc_plat.cl_val,
NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
return (S_ERROR);
capstr->cs_str = strs + data->c_un.c_ptr;
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
DBG_STATE_ORIGINAL, CA_SUNW_PLAT, capstr->cs_str));
break;
case CA_SUNW_MACH:
if ((capstr = alist_append(&ocapset.oc_mach.cl_val,
NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
return (S_ERROR);
capstr->cs_str = strs + data->c_un.c_ptr;
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
DBG_STATE_ORIGINAL, CA_SUNW_MACH, capstr->cs_str));
break;
case CA_SUNW_ID:
ocapset.oc_id.cs_str = strs + data->c_un.c_ptr;
DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
DBG_STATE_ORIGINAL, CA_SUNW_ID,
ocapset.oc_id.cs_str));
break;
case CA_SUNW_HW_3:
ocapset.oc_hw_3.cm_val = data->c_un.c_val;
DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
DBG_STATE_ORIGINAL, CA_SUNW_HW_3,
ocapset.oc_hw_3.cm_val, ld_targ.t_m.m_mach));
break;
}
if (descapndx == -1)
descapndx = ndx;
}
return (1);
}
static uintptr_t
process_capinfo(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *isp)
{
Cap_desc *cdp = ifl->ifl_caps;
Capinfo *capinfo = isp->is_indata->d_buf;
Shdr *shdr = isp->is_shdr;
Word cndx, capinfonum;
capinfonum = (Word)(shdr->sh_size / shdr->sh_entsize);
if ((cdp == NULL) || (capinfo == NULL) || (capinfonum == 0))
return (0);
for (cndx = 1, capinfo++; cndx < capinfonum; cndx++, capinfo++) {
Sym_desc *sdp, *lsdp;
Word lndx;
uchar_t gndx;
if ((gndx = (uchar_t)ELF_C_GROUP(*capinfo)) == 0)
continue;
lndx = (Word)ELF_C_SYM(*capinfo);
if ((sdp = ifl->ifl_oldndx[cndx]) == NULL) {
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(MSG_CAPINFO_INVALSYM), ifl->ifl_name,
EC_WORD(isp->is_scnndx), isp->is_name, cndx,
MSG_INTL(MSG_STR_UNKNOWN));
continue;
}
if ((lndx == 0) || (lndx >= ifl->ifl_symscnt) ||
((lsdp = ifl->ifl_oldndx[lndx]) == NULL) ||
(ELF_ST_BIND(lsdp->sd_sym->st_info) != STB_GLOBAL)) {
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(MSG_CAPINFO_INVALLEAD), ifl->ifl_name,
EC_WORD(isp->is_scnndx), isp->is_name, cndx, lsdp ?
demangle(lsdp->sd_name) : MSG_INTL(MSG_STR_UNKNOWN),
lndx);
continue;
}
sdp->sd_flags |= FLG_SY_CAP;
if (gndx == CAPINFO_SUNW_GLOB) {
if (ld_cap_add_family(ofl, lsdp, sdp,
NULL, NULL) == S_ERROR)
return (S_ERROR);
continue;
}
ofl->ofl_caploclcnt++;
if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
(st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1))
return (S_ERROR);
ofl->ofl_locscnt--;
if (lsdp->sd_flags & FLG_SY_NODYNSORT) {
Sym *lsym = lsdp->sd_sym;
uchar_t ltype = ELF_ST_TYPE(lsym->st_info);
DYNSORT_COUNT(lsdp, lsym, ltype, --);
lsdp->sd_flags |= FLG_SY_NODYNSORT;
}
if (ld_cap_add_family(ofl, lsdp, sdp,
cdp->ca_groups->apl_data[gndx], NULL) == S_ERROR)
return (S_ERROR);
}
return (0);
}
static uintptr_t
process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
return (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_null, ofl));
}
static uintptr_t
process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
if (process_section(name, ifl,
shdr, scn, ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
return (S_ERROR);
if (ifl->ifl_ehdr->e_type == ET_REL) {
if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
ofl->ofl_relocincnt +=
(Word)(shdr->sh_size / shdr->sh_entsize);
} else if (ofl->ofl_flags & FLG_OF_EXEC) {
if (aplist_append(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx],
AL_CNT_IFL_RELSECS) == NULL)
return (S_ERROR);
}
return (1);
}
static uintptr_t
process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
char *data;
size_t size;
Is_desc *isp;
uintptr_t error;
if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
(strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
(strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
return (1);
if ((ident != ld_targ.t_id.id_null) &&
(strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
ident = ld_targ.t_id.id_null;
error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
if ((error == 0) || (error == S_ERROR))
return (error);
isp = ifl->ifl_isdesc[ndx];
size = isp->is_indata->d_size;
if (size) {
data = isp->is_indata->d_buf;
if (data[0] != '\0' || data[size - 1] != '\0')
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name,
EC_WORD(isp->is_scnndx), name);
} else
isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
ifl->ifl_flags |= FLG_IF_HSTRTAB;
return (1);
}
static uintptr_t
invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
Conv_inv_buf_t inv_buf;
ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
ifl->ifl_name, EC_WORD(ndx), name,
conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
ifl->ifl_ehdr->e_machine, shdr->sh_type, CONV_FMT_ALT_CF,
&inv_buf));
return (1);
}
static int
is_name_cmp(const char *is_name, const char *match_name, size_t match_len)
{
if (strncmp(is_name, match_name, match_len) != 0)
return (0);
is_name += match_len;
return ((*is_name == '\0') || (*is_name == '%'));
}
inline static Boolean
process_progbits_alloc(const char *name, Ifl_desc *ifl, Shdr *shdr,
Word ndx, int *ident, Ofl_desc *ofl, Boolean is_stab_index,
Word *is_flags)
{
Boolean done = FALSE;
if (name[0] == '.') {
switch (name[1]) {
case 'e':
if (!is_name_cmp(name, MSG_ORIG(MSG_SCN_EHFRAME),
MSG_SCN_EHFRAME_SIZE))
break;
*ident = ld_targ.t_id.id_unwind;
*is_flags |= FLG_IS_EHFRAME;
done = TRUE;
break;
case 'g':
if (is_name_cmp(name, MSG_ORIG(MSG_SCN_GOT),
MSG_SCN_GOT_SIZE)) {
*ident = ld_targ.t_id.id_null;
done = TRUE;
break;
}
if ((ld_targ.t_m.m_sht_unwind == SHT_PROGBITS) &&
is_name_cmp(name, MSG_ORIG(MSG_SCN_GCC_X_TBL),
MSG_SCN_GCC_X_TBL_SIZE)) {
*ident = ld_targ.t_id.id_unwind;
done = TRUE;
break;
}
break;
case 'p':
if (is_name_cmp(name, MSG_ORIG(MSG_SCN_PLT),
MSG_SCN_PLT_SIZE)) {
*ident = ld_targ.t_id.id_null;
done = TRUE;
}
break;
}
}
if (!done) {
if (is_stab_index) {
*ident = ld_targ.t_id.id_interp;
} else {
*ident = ld_targ.t_id.id_data;
}
}
return (TRUE);
}
static uintptr_t
process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
Boolean is_stab_index = FALSE;
Word is_flags = 0;
uintptr_t r;
if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
MSG_SCN_STAB_SIZE) == 0)) {
if ((ofl->ofl_flags & FLG_OF_STRIP) ||
(strcmp((name + MSG_SCN_STAB_SIZE),
MSG_ORIG(MSG_SCN_EXCL)) == 0))
return (1);
if (strcmp((name + MSG_SCN_STAB_SIZE),
MSG_ORIG(MSG_SCN_INDEX)) == 0)
is_stab_index = TRUE;
}
if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
MSG_SCN_DEBUG_SIZE) == 0) ||
(strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
return (1);
}
if (ident) {
if (shdr->sh_flags & SHF_TLS) {
ident = ld_targ.t_id.id_tls;
} else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
(SHF_ALLOC | SHF_EXECINSTR)) {
ident = ld_targ.t_id.id_text;
} else if (shdr->sh_flags & SHF_ALLOC) {
if (process_progbits_alloc(name, ifl, shdr, ndx,
&ident, ofl, is_stab_index, &is_flags) == FALSE)
return (S_ERROR);
} else {
ident = ld_targ.t_id.id_note;
}
}
r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
if ((is_flags != 0) && (r == 1))
ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
return (r);
}
static uintptr_t
process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
if (ofl->ofl_flags & FLG_OF_STRIP) {
return (1);
}
return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
}
static uintptr_t
process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
if (ident) {
if (shdr->sh_flags & SHF_TLS)
ident = ld_targ.t_id.id_tlsbss;
#if defined(_ELF64)
else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
(ld_targ.t_m.m_mach == EM_AMD64))
ident = ld_targ.t_id.id_lbss;
#endif
else
ident = ld_targ.t_id.id_bss;
}
return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
}
static uintptr_t
process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
uintptr_t error;
if (ident)
ident = ld_targ.t_id.id_array;
error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
if ((error == 0) || (error == S_ERROR))
return (error);
return (1);
}
static uintptr_t
array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
{
Os_desc *osp;
Shdr *shdr;
if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
return (0);
shdr = isc->is_shdr;
if ((shdr->sh_type == SHT_FINI_ARRAY) &&
(ofl->ofl_osfiniarray == NULL))
ofl->ofl_osfiniarray = osp;
else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
(ofl->ofl_osinitarray == NULL))
ofl->ofl_osinitarray = osp;
else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
(ofl->ofl_ospreinitarray == NULL))
ofl->ofl_ospreinitarray = osp;
return (1);
}
static uintptr_t
process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
return (S_ERROR);
if (shdr->sh_link < ndx) {
Is_desc *isp = ifl->ifl_isdesc[shdr->sh_link];
if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
(isp->is_shdr->sh_type != SHT_DYNSYM))) {
ld_eprintf(ofl, ERR_FATAL,
MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
EC_WORD(ndx), name, EC_XWORD(shdr->sh_link));
return (S_ERROR);
}
isp->is_symshndx = ifl->ifl_isdesc[ndx];
}
return (1);
}
static uintptr_t
sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
{
if (isc->is_shdr->sh_link > isc->is_scnndx) {
Is_desc *isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
(isp->is_shdr->sh_type != SHT_DYNSYM))) {
ld_eprintf(ofl, ERR_FATAL,
MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
EC_WORD(isc->is_scnndx), isc->is_name,
EC_XWORD(isc->is_shdr->sh_link));
return (S_ERROR);
}
isp->is_symshndx = isc;
}
return (1);
}
static uintptr_t
process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
Dyn *dyn;
Elf_Scn *strscn;
Elf_Data *dp;
char *str;
if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
return (1);
if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
ifl->ifl_name);
return (0);
}
dp = elf_getdata(strscn, NULL);
str = (char *)dp->d_buf;
dp = elf_getdata(scn, NULL);
for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
Ifl_desc *difl;
switch (dyn->d_tag) {
case DT_NEEDED:
case DT_USED:
if (((difl = libld_calloc(1,
sizeof (Ifl_desc))) == NULL) ||
(aplist_append(&ofl->ofl_sos, difl,
AL_CNT_OFL_LIBS) == NULL))
return (S_ERROR);
difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
difl->ifl_flags = FLG_IF_NEEDSTR;
break;
case DT_RPATH:
case DT_RUNPATH:
if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
(str + (size_t)dyn->d_un.d_val))) ==
(const char *)S_ERROR)
return (S_ERROR);
break;
case DT_VERSYM:
ifl->ifl_flags |= FLG_IF_GNUVER;
break;
}
}
return (1);
}
static char *machine = NULL;
static size_t machine_sz = 0;
static char *platform = NULL;
static size_t platform_sz = 0;
static Isa_desc *isa = NULL;
static Uts_desc *uts = NULL;
static char *
expand(const char *parent, const char *name, char **next)
{
char _name[PATH_MAX], *nptr, *_next;
const char *optr;
size_t nrem = PATH_MAX - 1;
int expanded = 0, _expanded, isaflag = 0;
optr = name;
nptr = _name;
while (*optr) {
if (nrem == 0)
return ((char *)name);
if (*optr != '$') {
*nptr++ = *optr++, nrem--;
continue;
}
_expanded = 0;
if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
MSG_STR_ORIGIN_SIZE) == 0) {
char *eptr;
if ((eptr = strrchr(parent, '/')) == NULL) {
*nptr++ = '.';
nrem--;
} else {
size_t len = eptr - parent;
if (len >= nrem)
return ((char *)name);
(void) strncpy(nptr, parent, len);
nptr = nptr + len;
nrem -= len;
}
optr += MSG_STR_ORIGIN_SIZE;
expanded = _expanded = 1;
} else if (strncmp(optr, MSG_ORIG(MSG_STR_MACHINE),
MSG_STR_MACHINE_SIZE) == 0) {
if ((machine == NULL) && (machine_sz == 0)) {
char info[SYS_NMLN];
long size;
size = sysinfo(SI_MACHINE, info, SYS_NMLN);
if ((size != -1) &&
(machine = libld_malloc((size_t)size))) {
(void) strcpy(machine, info);
machine_sz = (size_t)size - 1;
} else
machine_sz = 1;
}
if (machine) {
if (machine_sz >= nrem)
return ((char *)name);
(void) strncpy(nptr, machine, machine_sz);
nptr = nptr + machine_sz;
nrem -= machine_sz;
optr += MSG_STR_MACHINE_SIZE;
expanded = _expanded = 1;
}
} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
MSG_STR_PLATFORM_SIZE) == 0) {
if ((platform == NULL) && (platform_sz == 0)) {
char info[SYS_NMLN];
long size;
size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
if ((size != -1) &&
(platform = libld_malloc((size_t)size))) {
(void) strcpy(platform, info);
platform_sz = (size_t)size - 1;
} else
platform_sz = 1;
}
if (platform) {
if (platform_sz >= nrem)
return ((char *)name);
(void) strncpy(nptr, platform, platform_sz);
nptr = nptr + platform_sz;
nrem -= platform_sz;
optr += MSG_STR_PLATFORM_SIZE;
expanded = _expanded = 1;
}
} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
MSG_STR_OSNAME_SIZE) == 0) {
if (uts == NULL)
uts = conv_uts();
if (uts && uts->uts_osnamesz) {
if (uts->uts_osnamesz >= nrem)
return ((char *)name);
(void) strncpy(nptr, uts->uts_osname,
uts->uts_osnamesz);
nptr = nptr + uts->uts_osnamesz;
nrem -= uts->uts_osnamesz;
optr += MSG_STR_OSNAME_SIZE;
expanded = _expanded = 1;
}
} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
MSG_STR_OSREL_SIZE) == 0) {
if (uts == NULL)
uts = conv_uts();
if (uts && uts->uts_osrelsz) {
if (uts->uts_osrelsz >= nrem)
return ((char *)name);
(void) strncpy(nptr, uts->uts_osrel,
uts->uts_osrelsz);
nptr = nptr + uts->uts_osrelsz;
nrem -= uts->uts_osrelsz;
optr += MSG_STR_OSREL_SIZE;
expanded = _expanded = 1;
}
} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
if (isa == NULL)
isa = conv_isalist();
if (isa && isa->isa_listsz &&
(nrem > isa->isa_opt->isa_namesz)) {
size_t mlen, tlen, hlen = optr - name;
size_t no;
char *lptr;
Isa_opt *opt = isa->isa_opt;
(void) strncpy(nptr, opt->isa_name,
opt->isa_namesz);
nptr = nptr + opt->isa_namesz;
nrem -= opt->isa_namesz;
optr += MSG_STR_ISALIST_SIZE;
expanded = _expanded = 1;
tlen = strlen(optr);
mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
isa->isa_listsz - opt->isa_namesz;
if (*next)
mlen += strlen(*next);
if ((_next = lptr = libld_malloc(mlen)) == NULL)
return (0);
for (no = 1, opt++; no < isa->isa_optno;
no++, opt++) {
(void) strncpy(lptr, name, hlen);
lptr = lptr + hlen;
(void) strncpy(lptr, opt->isa_name,
opt->isa_namesz);
lptr = lptr + opt->isa_namesz;
(void) strncpy(lptr, optr, tlen);
lptr = lptr + tlen;
*lptr++ = ':';
}
if (*next)
(void) strcpy(lptr, *next);
else
*--lptr = '\0';
}
}
if (_expanded == 0)
*nptr++ = *optr++, nrem--;
}
if (isaflag) {
if (isaflag == 1)
*next = _next;
else
return ((char *)name);
}
*nptr = '\0';
if (expanded) {
if ((nptr = libld_malloc(strlen(_name) + 1)) == NULL)
return ((char *)name);
(void) strcpy(nptr, _name);
return (nptr);
}
return ((char *)name);
}
static uintptr_t
process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
{
Dyn *dyn;
Elf_Data *dp;
uintptr_t error;
error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
if ((error == 0) || (error == S_ERROR))
return (error);
dp = elf_getdata(scn, NULL);
for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
if (dyn->d_tag == DT_VERSYM) {
ifl->ifl_flags |= FLG_IF_GNUVER;
break;
}
}
return (1);
}
static uintptr_t
process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
{
Dyn *data, *dyn;
char *str, *rpath = NULL;
const char *soname, *needed;
Boolean no_undef;
data = (Dyn *)isc->is_indata->d_buf;
str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
no_undef = (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS);
if (no_undef) {
for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
if ((dyn->d_tag != DT_RPATH) &&
(dyn->d_tag != DT_RUNPATH))
continue;
if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
continue;
break;
}
}
for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
if (dyn->d_tag == DT_SONAME) {
if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
continue;
ifl->ifl_soname = soname;
} else if ((dyn->d_tag == DT_NEEDED) ||
(dyn->d_tag == DT_USED)) {
Sdf_desc *sdf;
if (!no_undef)
continue;
if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
continue;
needed = expand(ifl->ifl_name, needed, NULL);
if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
if ((sdf = sdf_add(needed,
&ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
return (S_ERROR);
sdf->sdf_rfile = ifl->ifl_name;
}
if (rpath && (sdf->sdf_rpath == NULL))
sdf->sdf_rpath = rpath;
} else if (dyn->d_tag == DT_FLAGS_1) {
if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
ifl->ifl_flags &= ~FLG_IF_LAZYLD;
if (dyn->d_un.d_val & DF_1_DISPRELPND)
ifl->ifl_flags |= FLG_IF_DISPPEND;
if (dyn->d_un.d_val & DF_1_DISPRELDNE)
ifl->ifl_flags |= FLG_IF_DISPDONE;
if (dyn->d_un.d_val & DF_1_NODIRECT)
ifl->ifl_flags |= FLG_IF_NODIRECT;
if ((dyn->d_un.d_val & DF_1_INTERPOSE) &&
(ofl->ofl_flags & FLG_OF_EXEC))
ifl->ifl_flags |= FLG_IF_DEPREQD;
} else if ((dyn->d_tag == DT_AUDIT) &&
(ifl->ifl_flags & FLG_IF_NEEDED)) {
if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
(str + (size_t)dyn->d_un.d_val))) ==
(const char *)S_ERROR)
return (S_ERROR);
} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
ifl->ifl_flags &= ~MSK_IF_POSFLAG1;
ifl->ifl_flags |= FLG_IF_RTLDINF;
}
}
if (ifl->ifl_flags & FLG_IF_NEEDED) {
Ifl_desc *sifl;
Aliste idx;
for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
(ifl != sifl)) {
const char *hint, *iflb, *siflb;
iflb = strrchr(ifl->ifl_name, '/');
if (iflb == NULL)
iflb = ifl->ifl_name;
else
iflb++;
siflb = strrchr(sifl->ifl_name, '/');
if (siflb == NULL)
siflb = sifl->ifl_name;
else
siflb++;
if (strcmp(iflb, siflb) == 0)
hint = MSG_INTL(MSG_REC_CNFLTHINT);
else
hint = MSG_ORIG(MSG_STR_EMPTY);
ld_eprintf(ofl, ERR_FATAL,
MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
ifl->ifl_name, sifl->ifl_soname, hint);
return (0);
}
}
if (ofl->ofl_soname &&
(strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
ld_eprintf(ofl, ERR_FATAL,
MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
return (0);
}
}
return (1);
}
static uintptr_t
process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
{
if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
(ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
return (S_ERROR);
return (1);
}
static uintptr_t
process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, int ident, Ofl_desc *ofl)
{
uintptr_t error;
error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
if ((error == 0) || (error == S_ERROR))
return (error);
ifl->ifl_flags |= FLG_IF_GROUPS;
return (1);
}
static uintptr_t
rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
{
Word rndx;
Is_desc *risc;
Os_desc *osp;
Shdr *shdr = isc->is_shdr;
Conv_inv_buf_t inv_buf;
if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
ifl->ifl_ehdr->e_machine, shdr->sh_type, CONV_FMT_ALT_CF,
&inv_buf));
return (0);
}
rndx = shdr->sh_info;
if (rndx >= ifl->ifl_shnum) {
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
EC_XWORD(rndx));
return (0);
}
if (rndx == 0) {
if (aplist_append(&ofl->ofl_extrarels, isc,
AL_CNT_OFL_RELS) == NULL)
return (S_ERROR);
} else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
if (risc->is_flags & FLG_IS_DISCARD)
return (1);
if ((osp = risc->is_osdesc) == NULL) {
if (risc->is_shdr->sh_type == SHT_SUNW_move) {
if (aplist_append(&ofl->ofl_ismoverel,
isc, AL_CNT_OFL_MOVE) == NULL)
return (S_ERROR);
return (1);
}
ld_eprintf(ofl, ERR_FATAL,
MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
EC_WORD(isc->is_scnndx), isc->is_name,
EC_WORD(risc->is_scnndx), risc->is_name);
return (0);
}
if (aplist_append(&osp->os_relisdescs, isc,
AL_CNT_OS_RELISDESCS) == NULL)
return (S_ERROR);
}
return (1);
}
static uintptr_t
process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
Word ndx, Ofl_desc *ofl)
{
if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
return (0);
if (shdr->sh_flags & SHF_ALLOC) {
ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
ifl->ifl_name, EC_WORD(ndx), name);
return (0);
}
return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
}
typedef uintptr_t (* initial_func_t)(const char *, Ifl_desc *, Shdr *,
Elf_Scn *, Word, int, Ofl_desc *);
static initial_func_t Initial[SHT_NUM][2] = {
invalid_section, invalid_section,
process_progbits, process_progbits,
process_input, process_input,
process_strtab, process_strtab,
process_reloc, process_reloc,
invalid_section, NULL,
process_rel_dynamic, process_dynamic_isgnu,
process_section, NULL,
process_nobits, process_nobits,
process_reloc, process_reloc,
process_section, invalid_section,
invalid_section, process_input,
process_progbits, process_progbits,
process_progbits, process_progbits,
process_array, NULL,
process_array, NULL,
process_array, NULL,
process_group, invalid_section,
process_sym_shndx, NULL
};
typedef uintptr_t (* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
static final_func_t Final[SHT_NUM][2] = {
NULL, NULL,
process_progbits_final, NULL,
ld_sym_process, ld_sym_process,
NULL, NULL,
rel_process, NULL,
NULL, NULL,
NULL, process_dynamic,
NULL, NULL,
NULL, NULL,
rel_process, NULL,
NULL, NULL,
NULL, ld_sym_process,
NULL, NULL,
NULL, NULL,
array_process, NULL,
array_process, NULL,
array_process, NULL,
NULL, NULL,
sym_shndx_process, NULL
};
#define MAXNDXSIZE 10
static uintptr_t
process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
{
Elf_Scn *scn;
Shdr *shdr;
Word ndx, sndx, ordndx = 0, ordcnt = 0;
char *str, *name;
Word row, column;
int ident;
uintptr_t error;
Is_desc *vdfisp, *vndisp, *vsyisp, *sifisp;
Is_desc *capinfoisp, *capisp;
Sdf_desc *sdf;
Place_path_info path_info_buf, *path_info;
path_info = ld_place_path_info_init(ofl, ifl, &path_info_buf);
ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
sndx = ifl->ifl_shstrndx;
if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
ifl->ifl_name);
return (0);
}
if ((shdr = elf_getshdr(scn)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
ifl->ifl_name);
return (0);
}
if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
ifl->ifl_name);
return (0);
}
if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
elf) == S_ERROR)
return (S_ERROR);
if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
ifl->ifl_name);
return (0);
}
error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
if ((error == 0) || (error == S_ERROR))
return (error);
str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
if (ifl->ifl_ehdr->e_type == ET_DYN) {
column = 1;
ofl->ofl_soscnt++;
ident = ld_targ.t_id.id_null;
} else {
column = 0;
ofl->ofl_objscnt++;
ident = ld_targ.t_id.id_unknown;
}
DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
ndx = 0;
vdfisp = vndisp = vsyisp = sifisp = capinfoisp = capisp = NULL;
scn = NULL;
while (scn = elf_nextscn(elf, scn)) {
ndx++;
if (ndx == sndx)
continue;
if ((shdr = elf_getshdr(scn)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
ifl->ifl_name);
return (0);
}
name = str + (size_t)(shdr->sh_name);
if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
elf) == S_ERROR)
return (S_ERROR);
name = str + (size_t)(shdr->sh_name);
row = shdr->sh_type;
if (section_is_exclude(ofl, shdr)) {
if ((error = process_exclude(name, ifl, shdr, scn,
ndx, ofl)) == S_ERROR)
return (S_ERROR);
if (error == 1)
continue;
}
if (row < SHT_NUM) {
if (Initial[row][column] != NULL) {
if (Initial[row][column](name, ifl, shdr, scn,
ndx, ident, ofl) == S_ERROR)
return (S_ERROR);
}
} else {
if ((row < (Word)SHT_LOSUNW) &&
((shdr->sh_flags & SHF_EXCLUDE) == 0)) {
Conv_inv_buf_t inv_buf;
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
EC_WORD(ndx), name, conv_sec_type(
ifl->ifl_ehdr->e_ident[EI_OSABI],
ifl->ifl_ehdr->e_machine,
shdr->sh_type, CONV_FMT_ALT_CF, &inv_buf));
}
switch (row) {
case SHT_SUNW_dof:
if (process_section(name, ifl, shdr, scn,
ndx, ident, ofl) == S_ERROR)
return (S_ERROR);
break;
case SHT_SUNW_cap:
if (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_null, ofl) == S_ERROR)
return (S_ERROR);
capisp = ifl->ifl_isdesc[ndx];
break;
case SHT_SUNW_capinfo:
if (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_null, ofl) == S_ERROR)
return (S_ERROR);
capinfoisp = ifl->ifl_isdesc[ndx];
break;
case SHT_SUNW_DEBUGSTR:
case SHT_SUNW_DEBUG:
if (process_debug(name, ifl, shdr, scn,
ndx, ident, ofl) == S_ERROR)
return (S_ERROR);
break;
case SHT_SUNW_move:
if (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_null, ofl) == S_ERROR)
return (S_ERROR);
break;
case SHT_SUNW_syminfo:
if (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_null, ofl) == S_ERROR)
return (S_ERROR);
sifisp = ifl->ifl_isdesc[ndx];
break;
case SHT_SUNW_ANNOTATE:
if (process_progbits(name, ifl, shdr, scn,
ndx, ident, ofl) == S_ERROR)
return (S_ERROR);
break;
case SHT_SUNW_COMDAT:
if (process_progbits(name, ifl, shdr, scn,
ndx, ident, ofl) == S_ERROR)
return (S_ERROR);
ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
break;
case SHT_SUNW_verdef:
if (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_null, ofl) == S_ERROR)
return (S_ERROR);
vdfisp = ifl->ifl_isdesc[ndx];
break;
case SHT_SUNW_verneed:
if (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_null, ofl) == S_ERROR)
return (S_ERROR);
vndisp = ifl->ifl_isdesc[ndx];
break;
case SHT_SUNW_versym:
if (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_null, ofl) == S_ERROR)
return (S_ERROR);
vsyisp = ifl->ifl_isdesc[ndx];
break;
case SHT_SPARC_GOTDATA:
if (ld_targ.t_m.m_mach !=
LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
goto do_default;
if (process_section(name, ifl, shdr, scn, ndx,
ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
return (S_ERROR);
break;
#if defined(_ELF64)
case SHT_AMD64_UNWIND:
if (ld_targ.t_m.m_mach != EM_AMD64)
goto do_default;
if (column == 0) {
if (process_section(name, ifl, shdr,
scn, ndx, ld_targ.t_id.id_unwind,
ofl) == S_ERROR)
return (S_ERROR);
ifl->ifl_isdesc[ndx]->is_flags |=
FLG_IS_EHFRAME;
}
break;
#endif
default:
do_default:
if (process_section(name, ifl, shdr, scn, ndx,
((ident == ld_targ.t_id.id_null) ?
ident : ld_targ.t_id.id_user), ofl) ==
S_ERROR)
return (S_ERROR);
break;
}
}
}
if (ifl->ifl_flags & FLG_IF_GROUPS) {
for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
Is_desc *isp;
if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
(isp->is_shdr->sh_type != SHT_GROUP))
continue;
if (ld_group_process(isp, ofl) == S_ERROR)
return (S_ERROR);
}
}
ld_comdat_validate(ofl, ifl);
for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
Is_desc *isp;
if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
((isp->is_flags & FLG_IS_PLACE) == 0))
continue;
if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
if (ld_place_section(ofl, isp, path_info,
isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
return (S_ERROR);
continue;
}
ordcnt++;
if (ordndx == 0)
ordndx = ndx;
}
if (ifl->ifl_flags & FLG_IF_ORDERED) {
for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
Is_desc *isp;
if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
((isp->is_flags &
(FLG_IS_PLACE | FLG_IS_ORDERED)) !=
(FLG_IS_PLACE | FLG_IS_ORDERED)))
continue;
if (ld_process_ordered(ofl, ifl, path_info, ndx) ==
S_ERROR)
return (S_ERROR);
if (--ordcnt == 0)
break;
}
}
sdf = NULL;
if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
const char *base;
if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
base = ifl->ifl_name;
else
base++;
if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
sdf->sdf_file = ifl;
ifl->ifl_sdfdesc = sdf;
}
}
if (capisp && (process_cap(ofl, ifl, capisp) == S_ERROR))
return (S_ERROR);
if (vndisp && ((ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)))
if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
return (S_ERROR);
if (vsyisp)
(void) ld_vers_sym_process(ofl, vsyisp, ifl);
if (ifl->ifl_versym &&
(vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
return (S_ERROR);
for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
Is_desc *isp;
if ((isp = ifl->ifl_isdesc[ndx]) == NULL)
continue;
row = isp->is_shdr->sh_type;
if ((isp->is_flags & FLG_IS_DISCARD) == 0)
ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
isp->is_indata, elf);
if ((row == SHT_SUNW_move) && (column == 0)) {
if (aplist_append(&(ofl->ofl_ismove), isp,
AL_CNT_OFL_MOVE) == NULL)
return (S_ERROR);
}
if (row < SHT_NUM) {
if (Final[row][column] != NULL) {
if (Final[row][column](isp, ifl,
ofl) == S_ERROR)
return (S_ERROR);
}
#if defined(_ELF64)
} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
Os_desc *osp = isp->is_osdesc;
if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
(ld_unwind_register(osp, ofl) == S_ERROR))
return (S_ERROR);
#endif
}
}
if (capinfoisp && (ifl->ifl_ehdr->e_type == ET_REL) &&
(process_capinfo(ofl, ifl, capinfoisp) == S_ERROR))
return (S_ERROR);
if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
(FLG_IF_NEEDED | FLG_IF_NODIRECT)))
(void) ld_sym_nodirect(sifisp, ifl, ofl);
return (1);
}
uintptr_t
ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
Word flags, Ofl_desc *ofl, Rej_desc *rej, Ifl_desc **ifl_ret)
{
Ifl_desc *ifl;
Ehdr *ehdr;
uintptr_t error = 0;
struct stat status;
Ar_desc *adp;
Rej_desc _rej;
if (fd && ((flags & FLG_IF_EXTRACT) == 0))
(void) fstat(fd, &status);
else {
status.st_dev = 0;
status.st_ino = 0;
}
switch (elf_kind(elf)) {
case ELF_K_AR:
if (ifl_ret != NULL) {
_rej.rej_type = SGS_REJ_ARCHIVE;
_rej.rej_name = name;
DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
ld_targ.t_m.m_mach));
if (rej->rej_type == 0) {
*rej = _rej;
rej->rej_name = strdup(_rej.rej_name);
}
return (0);
}
if (!(flags & FLG_IF_EXTRACT)) {
Aliste idx;
for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
if ((adp->ad_stdev != status.st_dev) ||
(adp->ad_stino != status.st_ino))
continue;
DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
adp->ad_name));
(void) elf_end(elf);
if (!ld_process_archive(name, -1, adp, ofl))
return (S_ERROR);
return (1);
}
}
adp = ld_ar_setup(name, elf, ofl);
if ((adp == NULL) || (adp == (Ar_desc *)S_ERROR))
return ((uintptr_t)adp);
adp->ad_stdev = status.st_dev;
adp->ad_stino = status.st_ino;
ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
name);
return (0);
}
if (!ld_process_archive(name, -1, adp, ofl))
return (S_ERROR);
return (1);
case ELF_K_ELF:
if ((ehdr = elf_getehdr(elf)) == NULL) {
int _class = gelf_getclass(elf);
if (ld_targ.t_m.m_class != _class) {
_rej.rej_type = SGS_REJ_CLASS;
_rej.rej_info = (uint_t)_class;
} else {
_rej.rej_type = SGS_REJ_STR;
_rej.rej_str = elf_errmsg(-1);
}
_rej.rej_name = name;
DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
ld_targ.t_m.m_mach));
if (rej->rej_type == 0) {
*rej = _rej;
rej->rej_name = strdup(_rej.rej_name);
}
return (0);
}
if (_gelf_getdynval(elf, DT_SUNW_KMOD) > 0) {
_rej.rej_name = name;
DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
ld_targ.t_m.m_mach));
_rej.rej_type = SGS_REJ_KMOD;
_rej.rej_str = elf_errmsg(-1);
_rej.rej_name = name;
if (rej->rej_type == 0) {
*rej = _rej;
rej->rej_name = strdup(_rej.rej_name);
}
return (0);
}
if (!(flags & FLG_IF_EXTRACT)) {
APlist *apl;
Aliste idx;
if (ehdr->e_type == ET_REL)
apl = ofl->ofl_objs;
else
apl = ofl->ofl_sos;
for (APLIST_TRAVERSE(apl, idx, ifl)) {
if (ifl->ifl_flags & FLG_IF_NEEDSTR)
continue;
if ((ifl->ifl_stino != status.st_ino) ||
(ifl->ifl_stdev != status.st_dev))
continue;
DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
ifl->ifl_name, name));
(void) elf_end(elf);
if ((flags & FLG_IF_CMDLINE) ||
(ifl->ifl_flags & FLG_IF_CMDLINE)) {
const char *errmsg;
errmsg =
(strcmp(name, ifl->ifl_name) == 0) ?
MSG_INTL(MSG_FIL_MULINC_1) :
MSG_INTL(MSG_FIL_MULINC_2);
ld_eprintf(ofl, ERR_WARNING,
errmsg, name, ifl->ifl_name);
}
if (ifl_ret)
*ifl_ret = ifl;
return (1);
}
}
ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
if ((ifl == NULL) || (ifl == (Ifl_desc *)S_ERROR))
return ((uintptr_t)ifl);
ifl->ifl_stdev = status.st_dev;
ifl->ifl_stino = status.st_ino;
if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
ifl->ifl_flags |= FLG_IF_IGNORE;
switch (ehdr->e_type) {
case ET_REL:
(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
error = process_elf(ifl, elf, ofl);
break;
case ET_DYN:
if ((ofl->ofl_flags & FLG_OF_STATIC) ||
!(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
ld_eprintf(ofl, ERR_FATAL,
MSG_INTL(MSG_FIL_SOINSTAT), name);
return (0);
}
if (soname == NULL)
ifl->ifl_soname = ifl->ifl_name;
else
ifl->ifl_soname = soname;
if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
ifl->ifl_flags |= FLG_IF_DIRECT;
if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
ifl->ifl_flags |= FLG_IF_LAZYLD;
if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
ifl->ifl_flags |= FLG_IF_GRPPRM;
if (ofl->ofl_flags1 & FLG_OF1_DEFERRED)
ifl->ifl_flags |=
(FLG_IF_LAZYLD | FLG_IF_DEFERRED);
error = process_elf(ifl, elf, ofl);
if (ifl->ifl_flags & MSK_IF_SYMINFO)
ofl->ofl_flags |= FLG_OF_SYMINFO;
if (OFL_GUIDANCE(ofl, FLG_OFG_NO_LAZY) &&
((ifl->ifl_flags & FLG_IF_RTLDINF) == 0)) {
ld_eprintf(ofl, ERR_GUIDANCE,
MSG_INTL(MSG_GUIDE_LAZYLOAD));
ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
}
if (OFL_GUIDANCE(ofl, FLG_OFG_NO_DB)) {
ld_eprintf(ofl, ERR_GUIDANCE,
MSG_INTL(MSG_GUIDE_DIRECT));
ofl->ofl_guideflags |= FLG_OFG_NO_DB;
}
break;
default:
(void) elf_errno();
_rej.rej_type = SGS_REJ_UNKFILE;
_rej.rej_name = name;
DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
ld_targ.t_m.m_mach));
if (rej->rej_type == 0) {
*rej = _rej;
rej->rej_name = strdup(_rej.rej_name);
}
return (0);
}
break;
default:
(void) elf_errno();
_rej.rej_type = SGS_REJ_UNKFILE;
_rej.rej_name = name;
DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
ld_targ.t_m.m_mach));
if (rej->rej_type == 0) {
*rej = _rej;
rej->rej_name = strdup(_rej.rej_name);
}
return (0);
}
if ((error == 0) || (error == S_ERROR))
return (error);
if (ifl_ret)
*ifl_ret = ifl;
return (1);
}
uintptr_t
ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
Word flags, Rej_desc *rej, Ifl_desc **ifl_ret)
{
Elf *elf;
const char *npath = opath;
const char *nfile = ofile;
if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
return (0);
}
ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
elf_kind(elf));
if ((*fd == -1) || (elf == NULL))
return (0);
return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej,
ifl_ret));
}
Ifl_desc *
ld_process_mem(const char *path, const char *file, char *addr, size_t size,
Ofl_desc *ofl, Rej_desc *rej)
{
Elf *elf;
uintptr_t open_ret;
Ifl_desc *ifl;
if ((elf = elf_memory(addr, size)) == NULL) {
ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
return (0);
}
open_ret = ld_process_ifl(path, file, 0, elf, 0, ofl, rej, &ifl);
if (open_ret != 1)
return ((Ifl_desc *) open_ret);
return (ifl);
}
static Ifl_desc *
process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
Ofl_desc *ofl, Rej_desc *rej)
{
size_t dlen, plen;
int fd;
char path[PATH_MAX];
const char *_dir = dir;
if ((dlen = strlen(dir)) == 0) {
_dir = MSG_ORIG(MSG_STR_DOT);
dlen = 1;
}
dlen++;
plen = dlen + strlen(file) + 1;
if (plen > PATH_MAX) {
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
_dir, file);
return (0);
}
(void) strcpy(path, _dir);
(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
(void) strcat(path, file);
DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
sdf->sdf_rfile, path));
if ((fd = open(path, O_RDONLY)) == -1)
return (0);
else {
uintptr_t open_ret;
Ifl_desc *ifl;
char *_path;
if ((_path = libld_malloc(strlen(path) + 1)) == NULL)
return ((Ifl_desc *)S_ERROR);
(void) strcpy(_path, path);
open_ret = ld_process_open(_path, &_path[dlen], &fd, ofl,
0, rej, &ifl);
if (fd != -1)
(void) close(fd);
if (open_ret != 1)
return ((Ifl_desc *)open_ret);
return (ifl);
}
}
uintptr_t
ld_finish_libs(Ofl_desc *ofl)
{
Aliste idx1;
Sdf_desc *sdf;
Rej_desc rej = { 0 };
ofl->ofl_flags |= FLG_OF_DYNLIBS;
for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
Aliste idx2;
char *path, *slash = NULL;
int fd;
Ifl_desc *ifl;
char *file = (char *)sdf->sdf_name;
if (sdf->sdf_file)
continue;
for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
(strcmp(file, ifl->ifl_soname) == 0)) {
sdf->sdf_file = ifl;
break;
}
}
if (sdf->sdf_file)
continue;
for (path = file; *path; path++) {
if (*path == '/')
slash = path;
}
if (slash) {
DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
sdf->sdf_rfile, file));
if ((fd = open(file, O_RDONLY)) == -1) {
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(MSG_FIL_NOTFOUND), file,
sdf->sdf_rfile);
} else {
uintptr_t open_ret;
Rej_desc _rej = { 0 };
open_ret = ld_process_open(file, ++slash,
&fd, ofl, 0, &_rej, &ifl);
if (fd != -1)
(void) close(fd);
if (open_ret == S_ERROR)
return (S_ERROR);
if (_rej.rej_type) {
Conv_reject_desc_buf_t rej_buf;
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(reject[_rej.rej_type]),
_rej.rej_name ? rej.rej_name :
MSG_INTL(MSG_STR_UNKNOWN),
conv_reject_desc(&_rej, &rej_buf,
ld_targ.t_m.m_mach));
} else
sdf->sdf_file = ifl;
}
continue;
}
for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
Rej_desc _rej = { 0 };
ifl = process_req_lib(sdf, path, file, ofl, &_rej);
if (ifl == (Ifl_desc *)S_ERROR) {
return (S_ERROR);
}
if (_rej.rej_type) {
if (rej.rej_type == 0) {
rej = _rej;
rej.rej_name = strdup(_rej.rej_name);
}
}
if (ifl) {
sdf->sdf_file = ifl;
break;
}
}
if (sdf->sdf_file)
continue;
if (sdf->sdf_rpath != NULL) {
char *rpath, *next;
rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
if (rpath == NULL)
return (S_ERROR);
(void) strcpy(rpath, sdf->sdf_rpath);
DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
LA_SER_RUNPATH, sdf->sdf_rfile));
if ((path = strtok_r(rpath,
MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
do {
Rej_desc _rej = { 0 };
path = expand(sdf->sdf_rfile, path,
&next);
ifl = process_req_lib(sdf, path,
file, ofl, &_rej);
if (ifl == (Ifl_desc *)S_ERROR) {
return (S_ERROR);
}
if ((_rej.rej_type) &&
(rej.rej_type == 0)) {
rej = _rej;
rej.rej_name =
strdup(_rej.rej_name);
}
if (ifl) {
sdf->sdf_file = ifl;
break;
}
} while ((path = strtok_r(NULL,
MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
}
}
if (sdf->sdf_file)
continue;
for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
Rej_desc _rej = { 0 };
ifl = process_req_lib(sdf, path, file, ofl, &rej);
if (ifl == (Ifl_desc *)S_ERROR) {
return (S_ERROR);
}
if (_rej.rej_type) {
if (rej.rej_type == 0) {
rej = _rej;
rej.rej_name = strdup(_rej.rej_name);
}
}
if (ifl) {
sdf->sdf_file = ifl;
break;
}
}
if (sdf->sdf_file)
continue;
if (rej.rej_type) {
Conv_reject_desc_buf_t rej_buf;
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(reject[rej.rej_type]),
rej.rej_name ? rej.rej_name :
MSG_INTL(MSG_STR_UNKNOWN),
conv_reject_desc(&rej, &rej_buf,
ld_targ.t_m.m_mach));
} else {
ld_eprintf(ofl, ERR_WARNING,
MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
}
}
return (ld_vers_verify(ofl));
}