#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <libelf.h>
#include <string.h>
#include <dlfcn.h>
#include <debug.h>
#include <libld.h>
#include "_rtld.h"
#include "_audit.h"
#include "_elf.h"
static Rt_map *olmp = NULL;
static Alist *mpalp = NULL;
static Ehdr dehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
M_CLASS, M_DATA }, 0, M_MACH, EV_CURRENT };
static Rt_map *
elf_obj_init(Lm_list *lml, Aliste lmco, const char *oname)
{
Ofl_desc *ofl;
const char *name;
if ((name = stravl_insert(oname, 0, 0, 0)) == NULL)
return (NULL);
if ((ofl = calloc(1, sizeof (Ofl_desc))) == NULL)
return (NULL);
ofl->ofl_dehdr = &dehdr;
ofl->ofl_flags = (FLG_OF_DYNAMIC | FLG_OF_SHAROBJ | FLG_OF_STRIP);
ofl->ofl_flags1 = (FLG_OF1_RELDYN | FLG_OF1_TEXTOFF | FLG_OF1_MEMORY);
ofl->ofl_lml = lml;
if ((elf_rtld_load() == 0) || (ld_init_target(lml, M_MACH) != 0)) {
free(ofl);
return (NULL);
}
if ((ld_ent_setup(ofl, syspagsz) == S_ERROR) ||
((olmp = calloc(1, sizeof (*olmp))) == NULL)) {
free(ofl);
return (NULL);
}
DBG_CALL(Dbg_file_elf(lml, name, 0, 0, lml->lm_lmidstr, lmco));
FLAGS(olmp) |= FLG_RT_OBJECT;
ELFPRV(olmp) = (void *)ofl;
if (ld_init_strings(ofl) == S_ERROR) {
free(ofl);
free(olmp);
olmp = NULL;
return (NULL);
}
ofl->ofl_name = name;
NAME(olmp) = (char *)name;
LIST(olmp) = lml;
lm_append(lml, lmco, olmp);
return (olmp);
}
typedef struct {
mmapobj_result_t *md_mpp;
uint_t md_mnum;
} Mmap_desc;
Rt_map *
elf_obj_file(Lm_list *lml, Aliste lmco, Rt_map *clmp, const char *name,
mmapobj_result_t *hmpp, mmapobj_result_t *mpp, uint_t mnum)
{
Rej_desc rej;
Mmap_desc md;
if ((olmp == NULL) && ((olmp = elf_obj_init(lml, lmco, name)) == NULL))
return (NULL);
DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
md.md_mpp = mpp;
md.md_mnum = mnum;
if (alist_append(&mpalp, &md, sizeof (Mmap_desc),
AL_CNT_MPOBJS) == NULL) {
remove_so(lml, olmp, clmp);
return (NULL);
}
if (ld_process_mem(name, name, hmpp->mr_addr, hmpp->mr_msize,
(Ofl_desc *)ELFPRV(olmp), &rej) == (Ifl_desc *)S_ERROR) {
remove_so(lml, olmp, clmp);
return (NULL);
}
return (olmp);
}
inline static int
check_plat_names(Syscapset *scapset, Alist *caps, Rej_desc *rej)
{
Capstr *capstr;
Aliste idx;
for (ALIST_TRAVERSE(caps, idx, capstr)) {
if (platcap_check(scapset, capstr->cs_str, rej) == 1)
return (1);
}
return (0);
}
inline static int
check_mach_names(Syscapset *scapset, Alist *caps, Rej_desc *rej)
{
Capstr *capstr;
Aliste idx;
for (ALIST_TRAVERSE(caps, idx, capstr)) {
if (machcap_check(scapset, capstr->cs_str, rej) == 1)
return (1);
}
return (0);
}
Rt_map *
elf_obj_fini(Lm_list *lml, Rt_map *lmp, Rt_map *clmp, int *in_nfavl)
{
Ofl_desc *ofl = (Ofl_desc *)ELFPRV(lmp);
Rt_map *nlmp, *tlmp;
Ehdr *ehdr;
Phdr *phdr;
mmapobj_result_t *mpp, *hmpp;
uint_t phnum;
int mnum;
Lm_cntl *lmc;
Aliste idx1;
Mmap_desc *mdp;
Fdesc fd = { 0 };
Grp_hdl *ghp;
Rej_desc rej = { 0 };
Syscapset *scapset;
elfcap_mask_t omsk;
Alist *oalp;
DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
if (ld_reloc_init(ofl) == S_ERROR)
return (NULL);
if (ld_sym_validate(ofl) == S_ERROR)
return (NULL);
if (pnavl_recorded(&capavl, ofl->ofl_name, 0, NULL))
scapset = alt_scapset;
else
scapset = org_scapset;
if ((((omsk = ofl->ofl_ocapset.oc_hw_1.cm_val) != 0) &&
(hwcap1_check(scapset, omsk, &rej) == 0)) ||
(((omsk = ofl->ofl_ocapset.oc_sf_1.cm_val) != 0) &&
(sfcap1_check(scapset, omsk, &rej) == 0)) ||
(((omsk = ofl->ofl_ocapset.oc_hw_2.cm_val) != 0) &&
(hwcap2_check(scapset, omsk, &rej) == 0)) ||
(((oalp = ofl->ofl_ocapset.oc_plat.cl_val) != NULL) &&
(check_plat_names(scapset, oalp, &rej) == 0)) ||
(((oalp = ofl->ofl_ocapset.oc_mach.cl_val) != NULL) &&
(check_mach_names(scapset, oalp, &rej) == 0))) {
if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) && lmp &&
(FLAGS1(lmp) & FL1_RT_LDDSTUB) && (NEXT(lmp) == NULL)) {
(void) printf(MSG_INTL(ldd_reject[rej.rej_type]),
ofl->ofl_name, rej.rej_str);
}
return (NULL);
}
if (ld_make_sections(ofl) == S_ERROR)
return (NULL);
if (ld_create_outfile(ofl) == S_ERROR)
return (NULL);
if (ld_update_outfile(ofl) == S_ERROR)
return (NULL);
if (ld_reloc_process(ofl) == S_ERROR)
return (NULL);
DBG_CALL(Dbg_file_output(ofl));
ehdr = ofl->ofl_nehdr;
phdr = ofl->ofl_phdr;
if ((mpp = hmpp = calloc(ehdr->e_phnum,
sizeof (mmapobj_result_t))) == NULL)
return (NULL);
for (mnum = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) {
if (phdr[phnum].p_type != PT_LOAD)
continue;
mpp[mnum].mr_addr = (caddr_t)((uintptr_t)phdr[phnum].p_vaddr +
(uintptr_t)ehdr);
mpp[mnum].mr_msize = phdr[phnum].p_memsz;
mpp[mnum].mr_fsize = phdr[phnum].p_filesz;
mpp[mnum].mr_prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
mnum++;
}
fd.fd_nname = ofl->ofl_name;
if ((nlmp = elf_new_lmp(lml, CNTL(olmp), &fd, (Addr)hmpp->mr_addr,
ofl->ofl_size, NULL, clmp, in_nfavl)) == NULL)
return (NULL);
MMAPS(nlmp) = hmpp;
MMAPCNT(nlmp) = mnum;
PADSTART(nlmp) = (ulong_t)hmpp->mr_addr;
PADIMLEN(nlmp) = mpp->mr_addr + mpp->mr_msize - hmpp->mr_addr;
lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(nlmp));
lml->lm_obj--;
if ((tlmp = PREV_RT_MAP(nlmp)) == olmp)
tlmp = nlmp;
if (PREV(olmp)) {
NEXT(PREV_RT_MAP(olmp)) = (Link_map *)nlmp;
PREV(nlmp) = PREV(olmp);
} else {
PREV(nlmp) = NULL;
lmc->lc_head = nlmp;
if (CNTL(nlmp) == ALIST_OFF_DATA)
lml->lm_head = nlmp;
}
if (NEXT(olmp) != (Link_map *)nlmp) {
NEXT(nlmp) = NEXT(olmp);
PREV(NEXT_RT_MAP(olmp)) = (Link_map *)nlmp;
}
NEXT(tlmp) = NULL;
lmc->lc_tail = tlmp;
if (CNTL(nlmp) == ALIST_OFF_DATA)
lml->lm_tail = tlmp;
HANDLES(nlmp) = HANDLES(olmp);
GROUPS(nlmp) = GROUPS(olmp);
STDEV(nlmp) = STDEV(olmp);
STINO(nlmp) = STINO(olmp);
FLAGS(nlmp) |= ((FLAGS(olmp) & ~FLG_RT_OBJECT) | FLG_RT_IMGALLOC);
FLAGS1(nlmp) |= FLAGS1(olmp);
MODE(nlmp) |= MODE(olmp);
NAME(nlmp) = NAME(olmp);
for (APLIST_TRAVERSE(HANDLES(nlmp), idx1, ghp)) {
Grp_desc *gdp;
Aliste idx2;
ghp->gh_ownlmp = nlmp;
for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
if (gdp->gd_depend == olmp) {
gdp->gd_depend = nlmp;
break;
}
}
}
ld_ofl_cleanup(ofl);
free(ELFPRV(olmp));
free(olmp);
olmp = 0;
for (ALIST_TRAVERSE(mpalp, idx1, mdp)) {
unmap_obj(mdp->md_mpp, mdp->md_mnum);
free(mdp->md_mpp);
}
free(mpalp);
mpalp = NULL;
(void) fullpath(nlmp, 0);
if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)
return (NULL);
if ((lml->lm_tflags | AFLAGS(nlmp)) & LML_TFLG_AUD_MASK) {
if (audit_objopen(nlmp, nlmp) == 0)
return (NULL);
}
return (nlmp);
}