X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=blobdiff_plain;f=src%2Fldso%2Fdynlink.c;h=981288c35a36ed53eac7f8171064230cc63021f0;hp=0cb025928617dffeb5ae1d52381457e3b14519e8;hb=dbcb3ad9255c71cbfeb4e9d65d56a8f49dcda6a6;hpb=05eff01e89ee345e70acdbebc9c3778766b76ee2 diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c index 0cb02592..981288c3 100644 --- a/src/ldso/dynlink.c +++ b/src/ldso/dynlink.c @@ -22,8 +22,6 @@ static char errbuf[128]; #ifdef SHARED -#include "reloc.h" - #if ULONG_MAX == 0xffffffff typedef Elf32_Ehdr Ehdr; typedef Elf32_Phdr Phdr; @@ -55,6 +53,7 @@ struct dso { int refcnt; Sym *syms; uint32_t *hashtab; + uint32_t *ghashtab; char *strings; unsigned char *map; size_t map_len; @@ -68,6 +67,8 @@ struct dso { char buf[]; }; +#include "reloc.h" + void __init_ssp(size_t *); static struct dso *head, *tail, *libc; @@ -76,9 +77,11 @@ static int rtld_used; static int ssp_used; static int runtime; static int ldd_mode; +static int ldso_fail; static jmp_buf rtld_fail; static pthread_rwlock_t lock; static struct debug debug; +static size_t *auxv; struct debug *_dl_debug_addr = &debug; @@ -94,7 +97,15 @@ static void decode_vec(size_t *v, size_t *a, size_t cnt) } } -static uint32_t hash(const char *s0) +static int search_vec(size_t *v, size_t *r, size_t key) +{ + for (; v[0]!=key; v+=2) + if (!v[0]) return 0; + *r = v[1]; + return 1; +} + +static uint32_t sysv_hash(const char *s0) { const unsigned char *s = (void *)s0; uint_fast32_t h = 0; @@ -105,7 +116,16 @@ static uint32_t hash(const char *s0) return h & 0xfffffff; } -static Sym *lookup(const char *s, uint32_t h, struct dso *dso) +static uint32_t gnu_hash(const char *s0) +{ + const unsigned char *s = (void *)s0; + uint_fast32_t h = 5381; + for (; *s; s++) + h = h*33 + *s; + return h; +} + +static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso) { size_t i; Sym *syms = dso->syms; @@ -118,20 +138,61 @@ static Sym *lookup(const char *s, uint32_t h, struct dso *dso) return 0; } +static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso) +{ + Sym *sym; + char *strings; + uint32_t *hashtab = dso->ghashtab; + uint32_t nbuckets = hashtab[0]; + uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4); + uint32_t h2; + uint32_t *hashval; + uint32_t n = buckets[h1 % nbuckets]; + + if (!n) return 0; + + strings = dso->strings; + sym = dso->syms + n; + hashval = buckets + nbuckets + (n - hashtab[1]); + + for (h1 |= 1; ; sym++) { + h2 = *hashval++; + if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name)) + return sym; + if (h2 & 1) break; + } + + return 0; +} + #define OK_TYPES (1<ghashtab) { + gh = gnu_hash(s); + if (gh == 0xf9040207 && !strcmp(s, "dlopen")) rtld_used = 1; + if (gh == 0xf4dc4ae && !strcmp(s, "dlsym")) rtld_used = 1; + if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1; + } else { + h = sysv_hash(s); + if (h == 0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1; + if (h == 0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1; + if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1; + } for (; dso; dso=dso->next) { Sym *sym; if (!dso->global) continue; - sym = lookup(s, h, dso); + if (dso->ghashtab) { + if (!gh) gh = gnu_hash(s); + sym = gnu_lookup(s, gh, dso); + } else { + if (!h) h = sysv_hash(s); + sym = sysv_lookup(s, h, dso); + } if (sym && (!need_def || sym->st_shndx) && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES) && (1<<(sym->st_info>>4) & OK_BINDS)) { @@ -143,8 +204,11 @@ static void *find_sym(struct dso *dso, const char *s, int need_def) return def; } -static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso) +static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride) { + unsigned char *base = dso->base; + Sym *syms = dso->syms; + char *strings = dso->strings; Sym *sym; const char *name; size_t sym_val, sym_size; @@ -160,7 +224,7 @@ static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t if (sym_index) { sym = syms + sym_index; name = strings + sym->st_name; - ctx = IS_COPY(type) ? dso->next : dso; + ctx = IS_COPY(type) ? head->next : head; sym_val = (size_t)find_sym(ctx, name, IS_PLT(type)); if (!sym_val && sym->st_info>>4 != STB_WEAK) { snprintf(errbuf, sizeof errbuf, @@ -168,9 +232,12 @@ static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t dso->name, name); if (runtime) longjmp(rtld_fail, 1); dprintf(2, "%s\n", errbuf); - _exit(127); + ldso_fail = 1; + continue; } sym_size = sym->st_size; + } else { + sym_val = sym_size = 0; } do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]); } @@ -318,8 +385,11 @@ static void decode_dyn(struct dso *p) size_t dyn[DYN_CNT] = {0}; decode_vec(p->dynv, dyn, DYN_CNT); p->syms = (void *)(p->base + dyn[DT_SYMTAB]); - p->hashtab = (void *)(p->base + dyn[DT_HASH]); p->strings = (void *)(p->base + dyn[DT_STRTAB]); + if (dyn[0]&(1<hashtab = (void *)(p->base + dyn[DT_HASH]); + if (search_vec(p->dynv, dyn, DT_GNU_HASH)) + p->ghashtab = (void *)(p->base + *dyn); } static struct dso *load_library(const char *name) @@ -445,7 +515,8 @@ static void load_deps(struct dso *p) p->strings + p->dynv[i+1], p->name); if (runtime) longjmp(rtld_fail, 1); dprintf(2, "%s\n", errbuf); - _exit(127); + ldso_fail = 1; + continue; } if (runtime) { tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2)); @@ -484,12 +555,13 @@ static void reloc_all(struct dso *p) for (; p; p=p->next) { if (p->relocated) continue; decode_vec(p->dynv, dyn, DYN_CNT); - do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], - 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head); - do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], - 2, p->syms, p->strings, head); - do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], - 3, p->syms, p->strings, head); +#ifdef NEED_ARCH_RELOCS + do_arch_relocs(p, head); +#endif + do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], + 2+(dyn[DT_PLTREL]==DT_RELA)); + do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2); + do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3); p->relocated = 1; } } @@ -532,7 +604,7 @@ void _dl_debug_state(void) void *__dynlink(int argc, char **argv) { - size_t *auxv, aux[AUX_CNT] = {0}; + size_t aux[AUX_CNT] = {0}; size_t i; Phdr *phdr; Ehdr *ehdr; @@ -541,6 +613,7 @@ void *__dynlink(int argc, char **argv) struct dso *const lib = builtin_dsos+1; struct dso *const vdso = builtin_dsos+2; char *env_preload=0; + size_t vdso_base; /* Find aux vector just past environ[] */ for (i=argc+1; argv[i]; i++) @@ -629,9 +702,7 @@ void *__dynlink(int argc, char **argv) decode_dyn(app); /* Attach to vdso, if provided by the kernel */ - for (i=0; auxv[i]; i+=2) { - size_t vdso_base = auxv[i+1]; - if (auxv[i] != AT_SYSINFO_EHDR) continue; + if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) { ehdr = (void *)vdso_base; phdr = (void *)(vdso_base + ehdr->e_phoff); for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) { @@ -645,7 +716,6 @@ void *__dynlink(int argc, char **argv) decode_dyn(vdso); vdso->prev = lib; lib->next = vdso; - break; } /* Initial dso chain consists only of the app. We temporarily @@ -676,6 +746,7 @@ void *__dynlink(int argc, char **argv) reloc_all(app->next); reloc_all(app); + if (ldso_fail) _exit(127); if (ldd_mode) _exit(0); /* Switch to runtime mode: any further failures in the dynamic @@ -684,9 +755,11 @@ void *__dynlink(int argc, char **argv) * all memory used by the dynamic linker. */ runtime = 1; +#ifndef DYNAMIC_IS_RO for (i=0; app->dynv[i]; i+=2) if (app->dynv[i]==DT_DEBUG) app->dynv[i+1] = (size_t)&debug; +#endif debug.ver = 1; debug.bp = _dl_debug_state; debug.head = head; @@ -764,6 +837,8 @@ void *dlopen(const char *file, int mode) p->global = 1; } + if (ssp_used) __init_ssp(auxv); + _dl_debug_state(); do_init_fini(tail); @@ -776,7 +851,7 @@ end: static void *do_dlsym(struct dso *p, const char *s, void *ra) { size_t i; - uint32_t h; + uint32_t h = 0, gh = 0; Sym *sym; if (p == RTLD_NEXT) { for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next); @@ -790,12 +865,23 @@ static void *do_dlsym(struct dso *p, const char *s, void *ra) if (!res) goto failed; return res; } - h = hash(s); - sym = lookup(s, h, p); + if (p->ghashtab) { + gh = gnu_hash(s); + sym = gnu_lookup(s, gh, p); + } else { + h = sysv_hash(s); + sym = sysv_lookup(s, h, 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++) { - sym = lookup(s, h, p); + if (p->deps[i]->ghashtab) { + if (!gh) gh = gnu_hash(s); + sym = gnu_lookup(s, h, p->deps[i]); + } else { + if (!h) h = sysv_hash(s); + sym = sysv_lookup(s, h, p->deps[i]); + } if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) return p->deps[i]->base + sym->st_value; }