diff --git a/include/dlfcn.h b/include/dlfcn.h index 8c45822..8524e0b 100644 --- a/include/dlfcn.h +++ b/include/dlfcn.h @@ -20,14 +20,10 @@ void *dlsym(void *, const char *); #ifdef _GNU_SOURCE typedef struct { - const char *dli_fname; /* Pathname of shared object that - contains address */ - void *dli_fbase; /* Address at which shared object - is loaded */ - const char *dli_sname; /* Name of nearest symbol with address - lower than addr */ - void *dli_saddr; /* Exact address of symbol named - in dli_sname */ + const char *dli_fname; + void *dli_fbase; + const char *dli_sname; + void *dli_saddr; } Dl_info; int dladdr (void *addr, Dl_info *info); diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c index 4a236b2..d63da1c 100644 --- a/src/ldso/dynlink.c +++ b/src/ldso/dynlink.c @@ -59,8 +59,8 @@ struct dso { int refcnt; Sym *syms; - uint32_t hashalgs; - uint32_t *hashtabs[NHASH]; + uint32_t *hashtab; + uint32_t *ghashtab; char *strings; unsigned char *map; size_t map_len; @@ -93,12 +93,23 @@ struct debug *_dl_debug_addr = &debug; #define AUX_CNT 24 #define DYN_CNT 34 -static void decode_vec(size_t *v, size_t *a, size_t cnt) +struct tag_range { + size_t start; + size_t size; +}; + +static void decode_vec(size_t *v, const struct tag_range *defs, size_t **storages, size_t defsize) { - memset(a, 0, cnt*sizeof(size_t)); - for (; v[0]; v+=2) if (v[0]defs[i].start && v[0]<=defs[i].start+defs[i].size) { + storages[i][0] |= 1ULL<syms; - uint32_t *hashtab = dso->hashtabs[SYSV_HASH]; + uint32_t *hashtab = dso->hashtab; char *strings = dso->strings; for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) { if (!strcmp(s, strings+syms[i].st_name)) @@ -140,7 +151,7 @@ static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso) size_t i; Sym *sym; char *strings = dso->strings; - uint32_t *hashtab = dso->hashtabs[GNU_HASH]; + uint32_t *hashtab = dso->ghashtab; uint32_t nbuckets = hashtab[0]; size_t *maskwords = (size_t *)(hashtab + 4); uint32_t *buckets = hashtab + 4 + (hashtab[2]*(sizeof(size_t)/sizeof(uint32_t))); @@ -182,52 +193,37 @@ static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso) static void *find_sym(struct dso *dso, const char *s, int need_def) { void *def = 0; - static uint32_t precomp[NHASH][3] = { + static uint32_t precomp[2][3] = { {0x6b366be, 0x6b3afd, 0x595a4cc}, {0xf9040207, 0xf4dc4ae, 0x1f4039c9}, }; - uint32_t h[NHASH]; - uint8_t ok = 0; - uint8_t algs = dso->hashalgs; - uint8_t alg; - if (algs & (1 << SYSV_HASH)) { - h[SYSV_HASH] = sysv_hash(s); - alg = SYSV_HASH; + uint32_t *precomptab; + uint32_t h = 0, gh = 0; + if (dso->hashtab) { + h = sysv_hash(s); + precomptab = precomp[0]; } else { - h[GNU_HASH] = gnu_hash(s); - alg = GNU_HASH; + gh = gnu_hash(s); + precomptab = precomp[0]; } - ok |= (1 << alg); - - if (h[alg] == precomp[alg][0] && !strcmp(s, "dlopen")) rtld_used = 1; - if (h[alg] == precomp[alg][1] && !strcmp(s, "dlsym")) rtld_used = 1; - if (h[alg] == precomp[alg][2] && !strcmp(s, "__stack_chk_fail")) ssp_used = 1; + if (h == precomptab[0] && !strcmp(s, "dlopen")) rtld_used = 1; + if (h == precomptab[1] && !strcmp(s, "dlsym")) rtld_used = 1; + if (h == precomptab[2] && !strcmp(s, "__stack_chk_fail")) ssp_used = 1; for (; dso; dso=dso->next) { Sym *sym; if (!dso->global) continue; - algs = dso->hashalgs; - if (!(algs & ok)) { - if (algs & (1 << SYSV_HASH)) { - alg = SYSV_HASH; - h[alg] = sysv_hash(s); - sym = sysv_lookup(s, h[alg], dso); - } - else { - alg = GNU_HASH; - h[alg] = gnu_hash(s); - sym = gnu_lookup(s, h[alg], dso); - } - - ok |= (1 << alg); + if (dso->hashtab && (h || !dso->ghashtab)) { + if (!h) + h = sysv_hash(s); + sym = sysv_lookup(s, h, dso); } else { - if ((algs & ok) & (1 << SYSV_HASH)) - sym = sysv_lookup(s, h[SYSV_HASH], dso); - else - sym = gnu_lookup(s, h[GNU_HASH], dso); + if (!gh) + gh = gnu_hash(s); + sym = gnu_lookup(s, gh, dso); } if (sym && (!need_def || sym->st_shndx) && sym->st_value @@ -418,28 +414,18 @@ static int path_open(const char *name, const char *search, char *buf, size_t buf static void decode_dyn(struct dso *p) { - size_t *v; - p->hashalgs = 0; - for (v = p->dynv; v[0]; v+=2) { - switch (v[0]) { - case DT_SYMTAB: - p->syms = (void *)(p->base + v[1]); - break; - case DT_HASH: - p->hashtabs[SYSV_HASH] = (void *)(p->base + v[1]); - p->hashalgs |= (1 << SYSV_HASH); - break; - case DT_STRTAB: - p->strings = (void *)(p->base + v[1]); - break; - case DT_GNU_HASH: - p->hashtabs[GNU_HASH] = (void *)(p->base + v[1]); - p->hashalgs |= (1 << GNU_HASH); - break; - default: - break; - } - } + static const struct tag_range defs[2] = {{0, DYN_CNT}, {DT_ADDRRNGHI-DT_ADDRNUM, DT_ADDRNUM}}; + size_t dynbase[DYN_CNT] = {0}; + size_t dynaddr[DT_ADDRNUM+1] = {0}; + size_t *storage[2] = {dynbase, dynaddr}; + decode_vec (p->dynv, defs, storage, 2); + p->syms = (void *)(p->base + dynbase[DT_SYMTAB]); + p->strings = (void *)(p->base + dynbase[DT_STRTAB]); + if (dynbase[DT_HASH]) + p->hashtab = (void *)(p->base + dynbase[DT_HASH]); + if (dynaddr[DT_ADDRNUM-DT_ADDRTAGIDX(DT_GNU_HASH)]) + p->ghashtab = (void *)(p->base + dynaddr[DT_ADDRNUM-DT_ADDRTAGIDX(DT_GNU_HASH)]); + } static struct dso *load_library(const char *name) @@ -601,9 +587,11 @@ static void make_global(struct dso *p) static void reloc_all(struct dso *p) { size_t dyn[DYN_CNT] = {0}; + static const struct tag_range defs = {0, DYN_CNT}; + size_t *storage = dyn; for (; p; p=p->next) { if (p->relocated) continue; - decode_vec(p->dynv, dyn, DYN_CNT); + decode_vec(p->dynv, &defs, &storage, 1); #ifdef NEED_ARCH_RELOCS do_arch_relocs(p, head); #endif @@ -636,9 +624,11 @@ static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride) static void do_init_fini(struct dso *p) { size_t dyn[DYN_CNT] = {0}; + static const struct tag_range defs = {0, DYN_CNT}; + size_t *storage = dyn; for (; p; p=p->prev) { if (p->constructed) return; - decode_vec(p->dynv, dyn, DYN_CNT); + decode_vec(p->dynv, &defs, &storage, 1); if (dyn[0] & (1<base + dyn[DT_FINI])); if (dyn[0] & (1<hashalgs; - uint32_t h[NHASH]; + uint32_t h = 0, gh = 0; if (p == RTLD_NEXT) { for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next); @@ -917,36 +907,26 @@ static void *do_dlsym(struct dso *p, const char *s, void *ra) return res; } - if (algs & (1 << SYSV_HASH)) { - h[SYSV_HASH] = sysv_hash(s); - sym = sysv_lookup(s, h[SYSV_HASH], p); - ok |= 1 << SYSV_HASH; + if (p->hashtab) { + h = sysv_hash(s); + sym = sysv_lookup(s, h, p); } else { - h[GNU_HASH] = gnu_hash(s); - sym = gnu_lookup(s, h[GNU_HASH], p); - ok |= 1 << GNU_HASH; + gh = gnu_hash(s); + sym = gnu_lookup(s, gh, p); } if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) return p->base + sym->st_value; if (p->deps) for (i=0; p->deps[i]; i++) { - algs = p->deps[i]->hashalgs; - if (!(algs & ok)) { - if (algs & SYSV_HASH) { - h[SYSV_HASH] = sysv_hash(s); - sym = sysv_lookup(s, h[SYSV_HASH], p->deps[i]); - ok |= SYSV_HASH; - } else { - h[GNU_HASH] = gnu_hash(s); - sym = gnu_lookup(s, h[GNU_HASH], p->deps[i]); - ok |= GNU_HASH; - } + if (p->deps[i]->hashtab && (h || !p->deps[i]->ghashtab)) { + if (!h) + h = sysv_hash(s); + sym = sysv_lookup(s, h, p->deps[i]); } else { - if (algs & SYSV_HASH) - sym = sysv_lookup(s, h[SYSV_HASH], p->deps[i]); - else - sym = gnu_lookup(s, h[GNU_HASH], p->deps[i]); + if (!gh) + gh = gnu_hash(s); + sym = gnu_lookup(s, h, p->deps[i]); } if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) @@ -1007,18 +987,18 @@ static int do_dladdr (void *addr, Dl_info *info) size_t i; info->dli_fname = p->name; info->dli_fbase = p->base; - if (p->hashalgs & (1 << SYSV_HASH)) { - hashtab = p->hashtabs[SYSV_HASH]; + if (p->hashtab) { + hashtab = p->hashtab; for (i = 0; i < hashtab[1]; i++) { if (!find_closest_sym (p, syms + i, &search)) return 1; } - } else if(p->hashalgs & (1 << GNU_HASH)) { + } else { uint32_t *buckets; uint32_t nbuckets; uint32_t *hashvals; uint32_t symndx; - hashtab = p->hashtabs[GNU_HASH]; + hashtab = p->ghashtab; buckets = hashtab + 4 + (hashtab[2] * (sizeof(size_t)/sizeof(uint32_t))); nbuckets = hashtab[0]; hashvals = buckets + nbuckets;