| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "elf_ops.h" |
| 17 |
|
| 18 |
#include <elf.h> |
| 19 |
|
| 20 |
#include "libc_wrappers.h" |
| 21 |
struct gnu_hash_header { |
| 22 |
uint32_t nbuckets; |
| 23 |
uint32_t symndx; |
| 24 |
|
| 25 |
uint32_t maskwords; |
| 26 |
uint32_t shift2; |
| 27 |
}; |
| 28 |
|
| 29 |
static uint32_t gnu_hash_func(const char *str) { |
| 30 |
uint32_t hash = 5381; |
| 31 |
for (; *str != '\0'; str++) { |
| 32 |
hash = hash * 33 + *str; |
| 33 |
} |
| 34 |
return hash; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms, |
| 49 |
ElfW(Half) * versym, char *symnames, |
| 50 |
void *sheader) { |
| 51 |
uint32_t *buckets, *vals; |
| 52 |
uint32_t hash_val; |
| 53 |
uint32_t cur_sym, cur_sym_hashval; |
| 54 |
signed long latest_sym = -1; |
| 55 |
ElfW(Half) latest_sym_ver = 0; |
| 56 |
struct gnu_hash_header *header = (struct gnu_hash_header *)(sheader); |
| 57 |
|
| 58 |
buckets = (uint32_t *)(((unsigned char *)(header + 1)) + |
| 59 |
(header->maskwords * sizeof(ElfW(Addr)))); |
| 60 |
vals = buckets + header->nbuckets; |
| 61 |
|
| 62 |
hash_val = gnu_hash_func(name); |
| 63 |
cur_sym = buckets[hash_val % header->nbuckets]; |
| 64 |
if (cur_sym == 0) { |
| 65 |
return -1; |
| 66 |
} |
| 67 |
|
| 68 |
hash_val &= ~1; |
| 69 |
for (;;) { |
| 70 |
cur_sym_hashval = vals[cur_sym - header->symndx]; |
| 71 |
if (((cur_sym_hashval & ~1) == hash_val) && |
| 72 |
(gotcha_strcmp(name, symnames + syms[cur_sym].st_name) == 0)) { |
| 73 |
if (!versym) { |
| 74 |
return (signed long)cur_sym; |
| 75 |
} |
| 76 |
|
| 77 |
if ((versym[cur_sym] & 0x7fff) > latest_sym_ver) { |
| 78 |
latest_sym = (signed long)cur_sym; |
| 79 |
latest_sym_ver = versym[cur_sym] & 0x7fff; |
| 80 |
} |
| 81 |
} |
| 82 |
if (cur_sym_hashval & 1) { |
| 83 |
break; |
| 84 |
} |
| 85 |
cur_sym++; |
| 86 |
} |
| 87 |
|
| 88 |
return latest_sym; |
| 89 |
} |
| 90 |
|
| 91 |
static unsigned long elf_hash(const unsigned char *name) { |
| 92 |
unsigned int h = 0, g; |
| 93 |
while (*name != '\0') { |
| 94 |
h = (h << 4) + *name++; |
| 95 |
if ((g = h & 0xf0000000)) { |
| 96 |
h ^= g >> 24; |
| 97 |
} |
| 98 |
h &= ~g; |
| 99 |
} |
| 100 |
return h; |
| 101 |
} |
| 102 |
|
| 103 |
signed long lookup_elf_hash_symbol(const char *name, ElfW(Sym) * syms, |
| 104 |
ElfW(Half) * versym, char *symnames, |
| 105 |
ElfW(Word) * header) { |
| 106 |
ElfW(Word) *nbucket = header + 0; |
| 107 |
ElfW(Word) *buckets = header + 2; |
| 108 |
ElfW(Word) *chains = buckets + *nbucket; |
| 109 |
signed long latest_sym = -1; |
| 110 |
ElfW(Half) latest_sym_ver = 0; |
| 111 |
|
| 112 |
unsigned int x = elf_hash((const unsigned char *)name); |
| 113 |
signed long y = (signed long)buckets[x % *nbucket]; |
| 114 |
while (y != STN_UNDEF) { |
| 115 |
if (gotcha_strcmp(name, symnames + syms[y].st_name) == 0) { |
| 116 |
if (!versym) { |
| 117 |
|
| 118 |
return y; |
| 119 |
} |
| 120 |
|
| 121 |
if ((versym[y] & 0x7fff) > latest_sym_ver) { |
| 122 |
latest_sym = y; |
| 123 |
latest_sym_ver = versym[y] & 0x7fff; |
| 124 |
} |
| 125 |
} |
| 126 |
y = chains[y]; |
| 127 |
} |
| 128 |
|
| 129 |
return latest_sym; |
| 130 |
} |