dl-hash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Program to load an ELF binary on a linux system, and run it
  3. * after resolving ELF shared library symbols
  4. *
  5. * Copyright (C) 2004 by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
  6. * Copyright (C) 2000-2006 by Erik Andersen <andersen@codepoet.org>
  7. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  8. * David Engel, Hongjiu Lu and Mitch D'Souza
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. /* Various symbol table handling functions, including symbol lookup */
  32. /*
  33. * This is the list of modules that are loaded when the image is first
  34. * started. As we add more via dlopen, they get added into other
  35. * chains.
  36. */
  37. struct dyn_elf *_dl_symbol_tables = NULL;
  38. /*
  39. * This is the list of modules that are loaded via dlopen. We may need
  40. * to search these for RTLD_GLOBAL files.
  41. */
  42. struct dyn_elf *_dl_handles = NULL;
  43. #ifdef __LDSO_GNU_HASH_SUPPORT__
  44. /* This is the new hash function that is used by the ELF linker to generate the
  45. * GNU hash table that each executable and library will have if --hash-style=[gnu,both]
  46. * is passed to the linker. We need it to decode the GNU hash table. */
  47. static __inline__ Elf_Symndx _dl_gnu_hash (const unsigned char *name)
  48. {
  49. unsigned long h = 5381;
  50. unsigned char c;
  51. for (c = *name; c != '\0'; c = *++name)
  52. h = h * 33 + c;
  53. return h & 0xffffffff;
  54. }
  55. #endif
  56. /* This is the hash function that is used by the ELF linker to generate the
  57. * hash table that each executable and library is required to have. We need
  58. * it to decode the hash table. */
  59. static __inline__ Elf_Symndx _dl_elf_hash(const unsigned char *name)
  60. {
  61. unsigned long hash=0;
  62. unsigned long tmp;
  63. while (*name) {
  64. hash = (hash << 4) + *name++;
  65. tmp = hash & 0xf0000000;
  66. /* The algorithm specified in the ELF ABI is as follows:
  67. if (tmp != 0)
  68. hash ^= tmp >> 24;
  69. hash &= ~tmp;
  70. But the following is equivalent and a lot
  71. faster, especially on modern processors. */
  72. hash ^= tmp;
  73. hash ^= tmp >> 24;
  74. }
  75. return hash;
  76. }
  77. /*
  78. * We call this function when we have just read an ELF library or executable.
  79. * We add the relevant info to the symbol chain, so that we can resolve all
  80. * externals properly.
  81. */
  82. struct elf_resolve *_dl_add_elf_hash_table(const char *libname,
  83. DL_LOADADDR_TYPE loadaddr, unsigned long *dynamic_info, unsigned long dynamic_addr,
  84. attribute_unused unsigned long dynamic_size)
  85. {
  86. Elf_Symndx *hash_addr;
  87. struct elf_resolve *tpnt;
  88. int i;
  89. tpnt = _dl_malloc(sizeof(struct elf_resolve));
  90. _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
  91. if (!_dl_loaded_modules)
  92. _dl_loaded_modules = tpnt;
  93. else {
  94. struct elf_resolve *t = _dl_loaded_modules;
  95. while (t->next)
  96. t = t->next;
  97. t->next = tpnt;
  98. t->next->prev = t;
  99. tpnt = t->next;
  100. }
  101. tpnt->next = NULL;
  102. tpnt->init_flag = 0;
  103. tpnt->libname = _dl_strdup(libname);
  104. tpnt->dynamic_addr = (ElfW(Dyn) *)dynamic_addr;
  105. tpnt->libtype = loaded_file;
  106. #ifdef __DSBT__
  107. if (dynamic_info[DT_DSBT_BASE_IDX] != 0)
  108. tpnt->dsbt_table = (void *)dynamic_info[DT_DSBT_BASE_IDX];
  109. if (dynamic_info[DT_DSBT_SIZE_IDX] != 0)
  110. tpnt->dsbt_size = dynamic_info[DT_DSBT_SIZE_IDX];
  111. if (dynamic_info[DT_DSBT_INDEX_IDX] != 0)
  112. tpnt->dsbt_index = dynamic_info[DT_DSBT_INDEX_IDX];
  113. #endif /* __DSBT__ */
  114. #ifdef __LDSO_GNU_HASH_SUPPORT__
  115. if (dynamic_info[DT_GNU_HASH_IDX] != 0) {
  116. Elf32_Word *hash32 = (Elf_Symndx*)dynamic_info[DT_GNU_HASH_IDX];
  117. tpnt->nbucket = *hash32++;
  118. Elf32_Word symbias = *hash32++;
  119. Elf32_Word bitmask_nwords = *hash32++;
  120. /* Must be a power of two. */
  121. _dl_assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
  122. tpnt->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
  123. tpnt->l_gnu_shift = *hash32++;
  124. tpnt->l_gnu_bitmask = (ElfW(Addr) *) hash32;
  125. hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
  126. tpnt->l_gnu_buckets = hash32;
  127. hash32 += tpnt->nbucket;
  128. tpnt->l_gnu_chain_zero = hash32 - symbias;
  129. } else
  130. /* Fall using old SysV hash table if GNU hash is not present */
  131. #endif
  132. if (dynamic_info[DT_HASH] != 0) {
  133. hash_addr = (Elf_Symndx*)dynamic_info[DT_HASH];
  134. tpnt->nbucket = *hash_addr++;
  135. tpnt->nchain = *hash_addr++;
  136. tpnt->elf_buckets = hash_addr;
  137. hash_addr += tpnt->nbucket;
  138. tpnt->chains = hash_addr;
  139. }
  140. tpnt->loadaddr = loadaddr;
  141. for (i = 0; i < DYNAMIC_SIZE; i++)
  142. tpnt->dynamic_info[i] = dynamic_info[i];
  143. return tpnt;
  144. }
  145. /* Routine to check whether the symbol matches. */
  146. static __attribute_noinline__ const ElfW(Sym) *
  147. check_match (const ElfW(Sym) *sym, char *strtab, const char* undef_name, int type_class)
  148. {
  149. #if defined(USE_TLS) && USE_TLS
  150. if ((sym->st_value == 0 && (ELF_ST_TYPE(sym->st_info) != STT_TLS))
  151. || (type_class & (sym->st_shndx == SHN_UNDEF)))
  152. /* No value or undefined symbol itself */
  153. return NULL;
  154. if (ELF_ST_TYPE(sym->st_info) > STT_FUNC
  155. && ELF_ST_TYPE(sym->st_info) != STT_COMMON
  156. && ELF_ST_TYPE(sym->st_info) != STT_TLS)
  157. /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC and STT_COMMON
  158. * entries (and STT_TLS if TLS is supported) since these
  159. * are no code/data definitions.
  160. */
  161. return NULL;
  162. #else
  163. if (type_class & (sym->st_shndx == SHN_UNDEF))
  164. /* undefined symbol itself */
  165. return NULL;
  166. if (sym->st_value == 0)
  167. /* No value */
  168. return NULL;
  169. if (ELF_ST_TYPE(sym->st_info) > STT_FUNC
  170. && ELF_ST_TYPE(sym->st_info) != STT_COMMON)
  171. /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC
  172. * and STT_COMMON entries since these are no
  173. * code/data definitions
  174. */
  175. return NULL;
  176. #endif
  177. #ifdef ARCH_SKIP_RELOC
  178. if (ARCH_SKIP_RELOC(type_class, sym))
  179. return NULL;
  180. #endif
  181. if (_dl_strcmp(strtab + sym->st_name, undef_name) != 0)
  182. return NULL;
  183. /* This is the matching symbol */
  184. return sym;
  185. }
  186. #ifdef __LDSO_GNU_HASH_SUPPORT__
  187. static __always_inline const ElfW(Sym) *
  188. _dl_lookup_gnu_hash(struct elf_resolve *tpnt, ElfW(Sym) *symtab, unsigned long hash,
  189. const char* undef_name, int type_class)
  190. {
  191. Elf_Symndx symidx;
  192. const ElfW(Sym) *sym;
  193. char *strtab;
  194. const ElfW(Addr) *bitmask = tpnt->l_gnu_bitmask;
  195. ElfW(Addr) bitmask_word = bitmask[(hash / __ELF_NATIVE_CLASS) & tpnt->l_gnu_bitmask_idxbits];
  196. unsigned int hashbit1 = hash & (__ELF_NATIVE_CLASS - 1);
  197. unsigned int hashbit2 = ((hash >> tpnt->l_gnu_shift) & (__ELF_NATIVE_CLASS - 1));
  198. _dl_assert (bitmask != NULL);
  199. if (unlikely((bitmask_word >> hashbit1) & (bitmask_word >> hashbit2) & 1)) {
  200. unsigned long rem;
  201. Elf32_Word bucket;
  202. do_rem (rem, hash, tpnt->nbucket);
  203. bucket = tpnt->l_gnu_buckets[rem];
  204. if (bucket != 0) {
  205. const Elf32_Word *hasharr = &tpnt->l_gnu_chain_zero[bucket];
  206. do {
  207. if (((*hasharr ^ hash) >> 1) == 0) {
  208. symidx = hasharr - tpnt->l_gnu_chain_zero;
  209. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]);
  210. sym = check_match (&symtab[symidx], strtab, undef_name, type_class);
  211. if (sym != NULL)
  212. return sym;
  213. }
  214. } while ((*hasharr++ & 1u) == 0);
  215. }
  216. }
  217. /* No symbol found. */
  218. return NULL;
  219. }
  220. #endif
  221. static __always_inline const ElfW(Sym) *
  222. _dl_lookup_sysv_hash(struct elf_resolve *tpnt, ElfW(Sym) *symtab, unsigned long hash, const char* undef_name, int type_class)
  223. {
  224. unsigned long hn;
  225. char *strtab;
  226. const ElfW(Sym) *sym;
  227. Elf_Symndx symidx;
  228. /* Avoid calling .urem here. */
  229. do_rem(hn, hash, tpnt->nbucket);
  230. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]);
  231. _dl_assert(tpnt->elf_buckets != NULL);
  232. for (symidx = tpnt->elf_buckets[hn]; symidx != STN_UNDEF; symidx = tpnt->chains[symidx]) {
  233. sym = check_match (&symtab[symidx], strtab, undef_name, type_class);
  234. if (sym != NULL)
  235. /* At this point the symbol is that we are looking for */
  236. return sym;
  237. }
  238. /* No symbol found into the current module*/
  239. return NULL;
  240. }
  241. /*
  242. * This function resolves externals, and this is either called when we process
  243. * relocations or when we call an entry in the PLT table for the first time.
  244. */
  245. char *_dl_find_hash(const char *name, struct r_scope_elem *scope, struct elf_resolve *mytpnt,
  246. int type_class, struct symbol_ref *sym_ref)
  247. {
  248. struct elf_resolve *tpnt = NULL;
  249. ElfW(Sym) *symtab;
  250. int i = 0;
  251. unsigned long elf_hash_number = 0xffffffff;
  252. const ElfW(Sym) *sym = NULL;
  253. char *weak_result = NULL;
  254. struct r_scope_elem *loop_scope;
  255. #ifdef __LDSO_GNU_HASH_SUPPORT__
  256. unsigned long gnu_hash_number = _dl_gnu_hash((const unsigned char *)name);
  257. #endif
  258. if ((sym_ref) && (sym_ref->sym) && (ELF32_ST_VISIBILITY(sym_ref->sym->st_other) == STV_PROTECTED)) {
  259. sym = sym_ref->sym;
  260. if (mytpnt)
  261. tpnt = mytpnt;
  262. } else
  263. for (loop_scope = scope; loop_scope && !sym; loop_scope = loop_scope->next) {
  264. for (i = 0; i < loop_scope->r_nlist; i++) {
  265. tpnt = loop_scope->r_list[i];
  266. if (!(tpnt->rtld_flags & RTLD_GLOBAL) && mytpnt) {
  267. if (mytpnt == tpnt)
  268. ;
  269. else {
  270. struct init_fini_list *tmp;
  271. for (tmp = mytpnt->rtld_local; tmp; tmp = tmp->next) {
  272. if (tmp->tpnt == tpnt)
  273. break;
  274. }
  275. if (!tmp)
  276. continue;
  277. }
  278. }
  279. /* Don't search the executable when resolving a copy reloc. */
  280. if ((type_class & ELF_RTYPE_CLASS_COPY) && tpnt->libtype == elf_executable)
  281. continue;
  282. /* If the hash table is empty there is nothing to do here. */
  283. if (tpnt->nbucket == 0)
  284. continue;
  285. symtab = (ElfW(Sym) *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB]);
  286. #ifdef __LDSO_GNU_HASH_SUPPORT__
  287. /* Prefer GNU hash style, if any */
  288. if (tpnt->l_gnu_bitmask) {
  289. sym = _dl_lookup_gnu_hash(tpnt, symtab, gnu_hash_number, name, type_class);
  290. if (sym != NULL)
  291. /* If sym has been found, do not search further */
  292. break;
  293. } else {
  294. #endif
  295. /* Use the old SysV-style hash table */
  296. /* Calculate the old sysv hash number only once */
  297. if (elf_hash_number == 0xffffffff)
  298. elf_hash_number = _dl_elf_hash((const unsigned char *)name);
  299. sym = _dl_lookup_sysv_hash(tpnt, symtab, elf_hash_number, name, type_class);
  300. if (sym != NULL)
  301. /* If sym has been found, do not search further */
  302. break;
  303. #ifdef __LDSO_GNU_HASH_SUPPORT__
  304. }
  305. #endif
  306. } /* End of inner for */
  307. }
  308. if (sym) {
  309. if (sym_ref) {
  310. sym_ref->sym = sym;
  311. sym_ref->tpnt = tpnt;
  312. }
  313. /* At this point we have found the requested symbol, do binding */
  314. #if defined(USE_TLS) && USE_TLS
  315. if (ELF_ST_TYPE(sym->st_info) == STT_TLS) {
  316. _dl_assert(sym_ref != NULL);
  317. return (char *)sym->st_value;
  318. }
  319. #endif
  320. switch (ELF_ST_BIND(sym->st_info)) {
  321. case STB_WEAK:
  322. #if 0
  323. /* Perhaps we should support old style weak symbol handling
  324. * per what glibc does when you export LD_DYNAMIC_WEAK */
  325. if (!weak_result)
  326. weak_result = (char *)DL_FIND_HASH_VALUE(tpnt, type_class, sym);
  327. break;
  328. #endif
  329. case STB_GLOBAL:
  330. #if defined(__FRV_FDPIC__) || defined(__BFIN_FDPIC__)
  331. if (sym_ref)
  332. sym_ref->tpnt = tpnt;
  333. #endif
  334. return (char *)DL_FIND_HASH_VALUE(tpnt, type_class, sym);
  335. default: /* Local symbols not handled here */
  336. break;
  337. }
  338. }
  339. #if defined(__FRV_FDPIC__) || defined(__BFIN_FDPIC__)
  340. if (sym_ref)
  341. sym_ref->tpnt = tpnt;
  342. #endif
  343. return weak_result;
  344. }