hash.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 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. struct elf_resolve *f_tpnt, enum caller_type caller_type)
  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, first;
  149. const ElfW(Sym) *sym;
  150. weak_result = 0;
  151. elf_hash_number = _dl_elf_hash(name);
  152. /* A quick little hack to make sure that any symbol in the executable
  153. will be preferred to one in a shared library. This is necessary so
  154. that any shared library data symbols referenced in the executable
  155. will be seen at the same address by the executable, shared libraries
  156. and dynamically loaded code. -Rob Ryan (robr@cmu.edu) */
  157. if (_dl_symbol_tables && rpnt1) {
  158. first = (*_dl_symbol_tables);
  159. first.next = rpnt1;
  160. rpnt1 = (&first);
  161. }
  162. /*
  163. * The passes are so that we can first search the regular symbols
  164. * for whatever module was specified, and then search anything
  165. * loaded with RTLD_GLOBAL. When pass is 1, it means we are just
  166. * starting the first dlopened module, and anything above that
  167. * is just the next one in the chain.
  168. */
  169. for (pass = 0; (1 == 1); pass++) {
  170. /*
  171. * If we are just starting to search for RTLD_GLOBAL, setup
  172. * the pointer for the start of the search.
  173. */
  174. if (pass == 1) {
  175. rpnt1 = _dl_handles;
  176. }
  177. /*
  178. * Anything after this, we need to skip to the next module.
  179. */
  180. else if (pass >= 2) {
  181. rpnt1 = rpnt1->next_handle;
  182. }
  183. /*
  184. * Make sure we still have a module, and make sure that this
  185. * module was loaded with RTLD_GLOBAL.
  186. */
  187. if (pass != 0) {
  188. if (rpnt1 == NULL)
  189. break;
  190. //if ((rpnt1->flags & RTLD_GLOBAL) == 0)
  191. //continue;
  192. }
  193. for (rpnt = (rpnt1 ? rpnt1 : _dl_symbol_tables); rpnt; rpnt = rpnt->next) {
  194. tpnt = rpnt->dyn;
  195. /* Don't search the executable when resolving a copy reloc. */
  196. if (tpnt->libtype == elf_executable && caller_type == copyrel)
  197. continue;
  198. /*
  199. * Avoid calling .urem here.
  200. */
  201. do_rem(hn, elf_hash_number, tpnt->nbucket);
  202. symtab = (Elf32_Sym *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  203. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  204. for (si = tpnt->elf_buckets[hn]; si != STN_UNDEF; si = tpnt->chains[si]) {
  205. sym = &symtab[si];
  206. if (sym->st_value == 0)
  207. continue;
  208. if (ELF32_ST_TYPE(sym->st_info) > STT_FUNC)
  209. continue;
  210. if (sym->st_shndx == SHN_UNDEF && caller_type != copyrel)
  211. continue;
  212. if (_dl_strcmp(strtab + sym->st_name, name) != 0)
  213. continue;
  214. switch (ELF32_ST_BIND(sym->st_info)) {
  215. case STB_WEAK:
  216. //Disable this to match current glibc behavior. Of course,
  217. //this doesn't actually work yet and will cause segfaults...
  218. #if 1
  219. if (!weak_result)
  220. weak_result = (char *)tpnt->loadaddr + sym->st_value;
  221. break;
  222. #endif
  223. case STB_GLOBAL:
  224. return (char*)tpnt->loadaddr + sym->st_value;
  225. default: /* Local symbols not handled here */
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. return weak_result;
  232. }