dl-hash.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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-2004 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. /* This is the hash function that is used by the ELF linker to generate the
  51. * hash table that each executable and library is required to have. We need
  52. * it to decode the hash table. */
  53. static inline Elf_Symndx _dl_elf_hash(const char *name)
  54. {
  55. unsigned long hash=0;
  56. unsigned long tmp;
  57. while (*name) {
  58. hash = (hash << 4) + *name++;
  59. tmp = hash & 0xf0000000;
  60. /* The algorithm specified in the ELF ABI is as follows:
  61. if (tmp != 0)
  62. hash ^= tmp >> 24;
  63. hash &= ~tmp;
  64. But the following is equivalent and a lot
  65. faster, especially on modern processors. */
  66. hash ^= tmp;
  67. hash ^= tmp >> 24;
  68. }
  69. return hash;
  70. }
  71. /*
  72. * We call this function when we have just read an ELF library or executable.
  73. * We add the relevant info to the symbol chain, so that we can resolve all
  74. * externals properly.
  75. */
  76. struct elf_resolve *_dl_add_elf_hash_table(const char *libname,
  77. char *loadaddr, unsigned long *dynamic_info, unsigned long dynamic_addr,
  78. attribute_unused unsigned long dynamic_size)
  79. {
  80. Elf_Symndx *hash_addr;
  81. struct elf_resolve *tpnt;
  82. int i;
  83. if (!_dl_loaded_modules) {
  84. tpnt = _dl_loaded_modules = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  85. _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
  86. } else {
  87. tpnt = _dl_loaded_modules;
  88. while (tpnt->next)
  89. tpnt = tpnt->next;
  90. tpnt->next = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  91. _dl_memset(tpnt->next, 0, sizeof(struct elf_resolve));
  92. tpnt->next->prev = tpnt;
  93. tpnt = tpnt->next;
  94. };
  95. tpnt->next = NULL;
  96. tpnt->init_flag = 0;
  97. tpnt->libname = _dl_strdup(libname);
  98. tpnt->dynamic_addr = (ElfW(Dyn) *)dynamic_addr;
  99. tpnt->libtype = loaded_file;
  100. if (dynamic_info[DT_HASH] != 0) {
  101. hash_addr = (Elf_Symndx*)dynamic_info[DT_HASH];
  102. tpnt->nbucket = *hash_addr++;
  103. tpnt->nchain = *hash_addr++;
  104. tpnt->elf_buckets = hash_addr;
  105. hash_addr += tpnt->nbucket;
  106. tpnt->chains = hash_addr;
  107. }
  108. tpnt->loadaddr = (ElfW(Addr))loadaddr;
  109. for (i = 0; i < DYNAMIC_SIZE; i++)
  110. tpnt->dynamic_info[i] = dynamic_info[i];
  111. return tpnt;
  112. }
  113. /*
  114. * This function resolves externals, and this is either called when we process
  115. * relocations or when we call an entry in the PLT table for the first time.
  116. */
  117. char *_dl_find_hash(const char *name, struct dyn_elf *rpnt, struct elf_resolve *mytpnt, int type_class)
  118. {
  119. struct elf_resolve *tpnt;
  120. int si;
  121. char *strtab;
  122. ElfW(Sym) *symtab;
  123. unsigned long elf_hash_number, hn;
  124. const ElfW(Sym) *sym;
  125. char *weak_result = NULL;
  126. elf_hash_number = _dl_elf_hash(name);
  127. for (; rpnt; rpnt = rpnt->next) {
  128. tpnt = rpnt->dyn;
  129. if (!(tpnt->rtld_flags & RTLD_GLOBAL) && mytpnt) {
  130. if (mytpnt == tpnt)
  131. ;
  132. else {
  133. struct init_fini_list *tmp;
  134. for (tmp = mytpnt->rtld_local; tmp; tmp = tmp->next) {
  135. if (tmp->tpnt == tpnt)
  136. break;
  137. }
  138. if (!tmp)
  139. continue;
  140. }
  141. }
  142. /* Don't search the executable when resolving a copy reloc. */
  143. if ((type_class & ELF_RTYPE_CLASS_COPY) && tpnt->libtype == elf_executable)
  144. continue;
  145. /* Avoid calling .urem here. */
  146. do_rem(hn, elf_hash_number, tpnt->nbucket);
  147. symtab = (ElfW(Sym) *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB]);
  148. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]);
  149. for (si = tpnt->elf_buckets[hn]; si != STN_UNDEF; si = tpnt->chains[si]) {
  150. sym = &symtab[si];
  151. if (type_class & (sym->st_shndx == SHN_UNDEF))
  152. continue;
  153. if (_dl_strcmp(strtab + sym->st_name, name) != 0)
  154. continue;
  155. if (sym->st_value == 0)
  156. continue;
  157. if (ELF_ST_TYPE(sym->st_info) > STT_FUNC)
  158. continue;
  159. switch (ELF_ST_BIND(sym->st_info)) {
  160. case STB_WEAK:
  161. #if 0
  162. /* Perhaps we should support old style weak symbol handling
  163. * per what glibc does when you export LD_DYNAMIC_WEAK */
  164. if (!weak_result)
  165. weak_result = (char *)tpnt->loadaddr + sym->st_value;
  166. break;
  167. #endif
  168. case STB_GLOBAL:
  169. return (char*)tpnt->loadaddr + sym->st_value;
  170. default: /* Local symbols not handled here */
  171. break;
  172. }
  173. }
  174. }
  175. return weak_result;
  176. }