dl-hash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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@codpoet.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. unsigned long _dl_elf_hash(const unsigned char *name)
  54. {
  55. unsigned long hash = 0;
  56. unsigned long tmp;
  57. while (*name) {
  58. hash = (hash << 4) + *name++;
  59. if ((tmp = hash & 0xf0000000))
  60. hash ^= tmp >> 24;
  61. hash &= ~tmp;
  62. }
  63. return hash;
  64. }
  65. /* Check to see if a library has already been added to the hash chain. */
  66. struct elf_resolve *_dl_check_hashed_files(const char *libname)
  67. {
  68. struct elf_resolve *tpnt;
  69. int len = _dl_strlen(libname);
  70. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  71. if (_dl_strncmp(tpnt->libname, libname, len) == 0 &&
  72. (tpnt->libname[len] == '\0' || tpnt->libname[len] == '.'))
  73. return tpnt;
  74. }
  75. return NULL;
  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. char *loadaddr, unsigned long *dynamic_info, unsigned long dynamic_addr,
  84. unsigned long dynamic_size)
  85. {
  86. unsigned long *hash_addr;
  87. struct elf_resolve *tpnt;
  88. int i;
  89. if (!_dl_loaded_modules) {
  90. tpnt = _dl_loaded_modules = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  91. _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
  92. } else {
  93. tpnt = _dl_loaded_modules;
  94. while (tpnt->next)
  95. tpnt = tpnt->next;
  96. tpnt->next = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  97. _dl_memset(tpnt->next, 0, sizeof(struct elf_resolve));
  98. tpnt->next->prev = tpnt;
  99. tpnt = tpnt->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->dynamic_size = dynamic_size;
  106. tpnt->libtype = loaded_file;
  107. if (dynamic_info[DT_HASH] != 0) {
  108. hash_addr = (unsigned long *) (intptr_t)(dynamic_info[DT_HASH] + loadaddr);
  109. tpnt->nbucket = *hash_addr++;
  110. tpnt->nchain = *hash_addr++;
  111. tpnt->elf_buckets = hash_addr;
  112. hash_addr += tpnt->nbucket;
  113. tpnt->chains = hash_addr;
  114. }
  115. tpnt->loadaddr = (ElfW(Addr))loadaddr;
  116. for (i = 0; i < 24; i++)
  117. tpnt->dynamic_info[i] = dynamic_info[i];
  118. #ifdef __mips__
  119. {
  120. Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
  121. while(dpnt->d_tag) {
  122. if (dpnt->d_tag == DT_MIPS_GOTSYM)
  123. tpnt->mips_gotsym = dpnt->d_un.d_val;
  124. if (dpnt->d_tag == DT_MIPS_LOCAL_GOTNO)
  125. tpnt->mips_local_gotno = dpnt->d_un.d_val;
  126. if (dpnt->d_tag == DT_MIPS_SYMTABNO)
  127. tpnt->mips_symtabno = dpnt->d_un.d_val;
  128. dpnt++;
  129. }
  130. }
  131. #endif
  132. return tpnt;
  133. }
  134. /*
  135. * This function resolves externals, and this is either called when we process
  136. * relocations or when we call an entry in the PLT table for the first time.
  137. */
  138. char *_dl_find_hash(const char *name, struct dyn_elf *rpnt1,
  139. int type_class)
  140. {
  141. struct elf_resolve *tpnt;
  142. int si;
  143. int pass;
  144. char *strtab;
  145. Elf32_Sym *symtab;
  146. unsigned long elf_hash_number, hn;
  147. char *weak_result;
  148. struct dyn_elf *rpnt;
  149. const ElfW(Sym) *sym;
  150. weak_result = 0;
  151. elf_hash_number = _dl_elf_hash(name);
  152. /*
  153. * The passes are so that we can first search the regular symbols
  154. * for whatever module was specified, and then search anything
  155. * loaded with RTLD_GLOBAL. When pass is 1, it means we are just
  156. * starting the first dlopened module, and anything above that
  157. * is just the next one in the chain.
  158. */
  159. if (rpnt1 == NULL)
  160. rpnt1 = _dl_symbol_tables;
  161. for (pass = 0; (1 == 1); pass++) {
  162. /*
  163. * If we are just starting to search for RTLD_GLOBAL, setup
  164. * the pointer for the start of the search.
  165. */
  166. if (pass == 1)
  167. rpnt1 = _dl_handles;
  168. /*
  169. * Anything after this, we need to skip to the next module.
  170. */
  171. else if (pass >= 2)
  172. rpnt1 = rpnt1->next_handle;
  173. /*
  174. * Make sure we still have a module.
  175. */
  176. if (rpnt1 == NULL)
  177. break;
  178. for (rpnt = rpnt1; rpnt; rpnt = rpnt->next) {
  179. tpnt = rpnt->dyn;
  180. /* Don't search the executable when resolving a copy reloc. */
  181. if ((type_class & ELF_RTYPE_CLASS_COPY) && tpnt->libtype == elf_executable)
  182. continue;
  183. /*
  184. * Avoid calling .urem here.
  185. */
  186. do_rem(hn, elf_hash_number, tpnt->nbucket);
  187. symtab = (Elf32_Sym *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  188. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  189. for (si = tpnt->elf_buckets[hn]; si != STN_UNDEF; si = tpnt->chains[si]) {
  190. sym = &symtab[si];
  191. if (sym->st_value == 0)
  192. continue;
  193. if (ELF32_ST_TYPE(sym->st_info) > STT_FUNC)
  194. continue;
  195. if (type_class & (sym->st_shndx == SHN_UNDEF))
  196. continue;
  197. if (_dl_strcmp(strtab + sym->st_name, name) != 0)
  198. continue;
  199. switch (ELF32_ST_BIND(sym->st_info)) {
  200. case STB_WEAK:
  201. #ifndef __LIBDL_SHARED__
  202. /*
  203. Due to a special hack in libdl.c, one must handle the _dl_ symbols
  204. according to the OLD weak symbol scheme. This stuff can be deleted
  205. once that hack has been fixed.
  206. */
  207. if(_dl_symbol((char *)name)) {
  208. if (!weak_result)
  209. weak_result = (char *)tpnt->loadaddr + sym->st_value;
  210. break;
  211. }
  212. #endif
  213. case STB_GLOBAL:
  214. return (char*)tpnt->loadaddr + sym->st_value;
  215. default: /* Local symbols not handled here */
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. return weak_result;
  222. }