dl-hash.c 11 KB

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