From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/1435 Path: news.gmane.org!not-for-mail From: boris brezillon Newsgroups: gmane.linux.lib.musl.general Subject: ldso : gnu hash support. Date: Tue, 7 Aug 2012 00:49:52 +0200 Message-ID: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: dough.gmane.org 1344293407 32201 80.91.229.3 (6 Aug 2012 22:50:07 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 6 Aug 2012 22:50:07 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-1436-gllmg-musl=m.gmane.org@lists.openwall.com Tue Aug 07 00:50:08 2012 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1SyW7h-0007KZ-6v for gllmg-musl@plane.gmane.org; Tue, 07 Aug 2012 00:50:05 +0200 Original-Received: (qmail 23553 invoked by uid 550); 6 Aug 2012 22:50:04 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 22521 invoked from network); 6 Aug 2012 22:50:04 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=VyZKT58ynbnlE2uMk6NE67vTtufT9zT4Jpifh8fmRg0=; b=fdIxaZWxdHnqJYrOo93UtzQQadEqs6kIo/WlKj8jJg29rJGR4DbUwETY51oqyldxJf ZQAb/Pm4i/BWwkUW+REjhQZDSsTcOdJMBn5a0UPEY6kGXsh+u66d+lEnF+l/HjEJwKlf bVSU0zn7D7wuwkAthq7SoaSDoyffvdtbFZT+/v7sdsUagiankWTwbxFJC28TFZnnCqzr rkQox5TjcnMTaqVC+A3AiAIEJNAQb1w1fs4Nm/xEgeEGZPOtKYj2SpRRuYMAVi7Y766n G957MFBkwihqVstsrZHjl7W+3fSy97yBXpAXmtj/ngC5TkQeJr7WPA+fck6cN9uJTqLW XDJw== Xref: news.gmane.org gmane.linux.lib.musl.general:1435 Archived-At: Hi, This patch adds support for gnu hash section (this was requested in a previous mail : hm, libc crashes loading libc). I've done some tests and it seems to work (algorithm described here : https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections). BTW, I really like this new libc (clean code, light and efficient implementation). I plan to use it in my future projects and I'd like to contribute to it's development. Let me know if you need some help. Regards, Boris Brezillon --- src/ldso/dynlink.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 115 insertions(+), 10 deletions(-) diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c index 31ef684..0c8d75a 100644 --- a/src/ldso/dynlink.c +++ b/src/ldso/dynlink.c @@ -52,6 +52,7 @@ struct dso { int refcnt; Sym *syms; + uint32_t hashalg; uint32_t *hashtab; char *strings; unsigned char *map; @@ -66,6 +67,15 @@ struct dso { char buf[]; }; +struct hash_algo { + uint32_t (*hash) (const char *); + Sym *(*lookup) (const char *s, uint32_t h, struct dso *dso); +}; + +#define SYSV_HASH_ALG_IDX 0 +#define GNU_HASH_ALG_IDX 1 +#define HASH_ALG_CNT 2 + #include "reloc.h" void __init_ssp(size_t *); @@ -94,7 +104,7 @@ static void decode_vec(size_t *v, size_t *a, size_t cnt) } } -static uint32_t hash(const char *s0) +static uint32_t sysv_hash(const char *s0) { const unsigned char *s = (void *)s0; uint_fast32_t h = 0; @@ -105,7 +115,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 (unsigned char c = *s; c != '\0'; c = *++s) + h = h * 33 + c; + return h & 0xffffffff; +} + +static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso) { size_t i; Sym *syms = dso->syms; @@ -118,20 +137,86 @@ 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) +{ + size_t i; + Sym *sym; + char *strings = dso->strings; + uint32_t *hashtab = dso->hashtab; + 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))); + uint32_t symndx = hashtab[1]; + Sym *syms = dso->syms; + uint32_t shift2 = hashtab[3]; + uint32_t h2 = h1 >> shift2; + uint32_t *hashvals = buckets + nbuckets; + uint32_t *hashval; + size_t c = sizeof(size_t) * 8; + size_t n = (h1 / c) & (hashtab[2] - 1); + size_t bitmask = (1 << (h1 % c)) | (1 << (h2 % c)); + + if ((maskwords[n] & bitmask) != bitmask) + return 0; + + n = buckets[h1 % nbuckets]; + if (!n) + return 0; + + sym = syms + n; + hashval = hashvals + n - symndx; + + for (h1 &= ~1; 1; sym++) { + h2 = *hashval++; + if ((h1 == (h2 & ~1)) && !strcmp(s, strings + sym->st_name)) + return sym; + + if (h2 & 1) + break; + } + + return 0; +} + +static struct hash_algo hashalgs[] = { + { + .hash = sysv_hash, + .lookup = sysv_lookup, + }, + { + .hash = gnu_hash, + .lookup = gnu_lookup, + }, +}; + + #define OK_TYPES (1<next) { Sym *sym; + uint32_t h; if (!dso->global) continue; - sym = lookup(s, h, dso); + + if (!(computed[dso->hashalg / 32] & (1 << (dso->hashalg % 32)))) { + h = hashalgs[dso->hashalg].hash(s); + hashes[dso->hashalg] = h; + computed[dso->hashalg / 32] |= (1 << (dso->hashalg % 32)); + } + else { + h = hashes[dso->hashalg]; + } + + sym = hashalgs[dso->hashalg].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)) { @@ -320,11 +405,17 @@ 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->dynv; 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->hashalg = SYSV_HASH_ALG_IDX; p->strings = (void *)(p->base + dyn[DT_STRTAB]); + for (; v[0]; v+=2) if (v[0] == DT_GNU_HASH) { + p->hashtab = (void *)(p->base + v[1]); + p->hashalg = GNU_HASH_ALG_IDX; + } } static struct dso *load_library(const char *name) @@ -786,6 +877,9 @@ static void *do_dlsym(struct dso *p, const char *s, void *ra) size_t i; uint32_t h; Sym *sym; + uint32_t computed[HASH_ALG_CNT / 32 + 1]; + uint32_t hashes[HASH_ALG_CNT]; + if (p == RTLD_NEXT) { for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next); if (!p) p=head; @@ -798,12 +892,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); + memset (computed, 0, sizeof (computed)); + h = hashalgs[p->hashalg].hash(s); + computed[p->hashalg / 32] |= (1 << (p->hashalg % 32)); + hashes[p->hashalg] = h; + sym = hashalgs[p->hashalg].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 (!(computed[p->deps[i]->hashalg / 32] & (1 << (p->deps[i]->hashalg % 32)))) { + h = hashalgs[p->deps[i]->hashalg].hash(s); + hashes[p->deps[i]->hashalg] = h; + computed[p->deps[i]->hashalg / 32] |= (1 << (p->deps[i]->hashalg % 32)); + } + else { + h = hashes[p->deps[i]->hashalg]; + } + sym = hashalgs[p->deps[i]->hashalg].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; } -- 1.7.9.5