hash.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Run an ELF binary on a linux system.
  2. Copyright (C) 1993-1996, Eric Youngdale.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. /* Various symbol table handling functions, including symbol lookup */
  15. #include "string.h"
  16. #include "dlfcn.h"
  17. #include "hash.h"
  18. #include "linuxelf.h"
  19. #include "syscall.h"
  20. #include "string.h"
  21. #include "sysdep.h"
  22. /*
  23. * This is the start of the linked list that describes all of the files present
  24. * in the system with pointers to all of the symbol, string, and hash tables,
  25. * as well as all of the other good stuff in the binary.
  26. */
  27. struct elf_resolve *_dl_loaded_modules = NULL;
  28. /*
  29. * This is the list of modules that are loaded when the image is first
  30. * started. As we add more via dlopen, they get added into other
  31. * chains.
  32. */
  33. struct dyn_elf *_dl_symbol_tables = NULL;
  34. /*
  35. * This is the list of modules that are loaded via dlopen. We may need
  36. * to search these for RTLD_GLOBAL files.
  37. */
  38. struct dyn_elf *_dl_handles = NULL;
  39. /*
  40. * This is the hash function that is used by the ELF linker to generate
  41. * the hash table that each executable and library is required to
  42. * have. We need it to decode the hash table.
  43. */
  44. unsigned long _dl_elf_hash(const char *name)
  45. {
  46. unsigned long hash = 0;
  47. unsigned long tmp;
  48. while (*name) {
  49. hash = (hash << 4) + *name++;
  50. if ((tmp = hash & 0xf0000000))
  51. hash ^= tmp >> 24;
  52. hash &= ~tmp;
  53. };
  54. return hash;
  55. }
  56. /*
  57. * Check to see if a library has already been added to the hash chain.
  58. */
  59. struct elf_resolve *_dl_check_hashed_files(char *libname)
  60. {
  61. struct elf_resolve *tpnt;
  62. int len = _dl_strlen(libname);
  63. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  64. if (_dl_strncmp(tpnt->libname, libname, len) == 0 &&
  65. (tpnt->libname[len] == '\0' || tpnt->libname[len] == '.'))
  66. return tpnt;
  67. }
  68. return NULL;
  69. }
  70. /*
  71. * We call this function when we have just read an ELF library or executable.
  72. * We add the relevant info to the symbol chain, so that we can resolve all
  73. * externals properly.
  74. */
  75. struct elf_resolve *_dl_add_elf_hash_table(char *libname,
  76. char *loadaddr, unsigned long *dynamic_info, unsigned long dynamic_addr,
  77. unsigned long dynamic_size)
  78. {
  79. unsigned long *hash_addr;
  80. struct elf_resolve *tpnt;
  81. int i;
  82. if (!_dl_loaded_modules) {
  83. tpnt = _dl_loaded_modules =
  84. (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  85. _dl_memset(tpnt, 0, sizeof(*tpnt));
  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(*(tpnt->next)));
  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 = dynamic_addr;
  99. tpnt->dynamic_size = dynamic_size;
  100. tpnt->libtype = loaded_file;
  101. if (dynamic_info[DT_HASH] != 0) {
  102. hash_addr = (unsigned long *) (dynamic_info[DT_HASH] + loadaddr);
  103. tpnt->nbucket = *hash_addr++;
  104. tpnt->nchain = *hash_addr++;
  105. tpnt->elf_buckets = hash_addr;
  106. hash_addr += tpnt->nbucket;
  107. tpnt->chains = hash_addr;
  108. }
  109. tpnt->loadaddr = loadaddr;
  110. for (i = 0; i < 24; i++)
  111. tpnt->dynamic_info[i] = dynamic_info[i];
  112. return tpnt;
  113. }
  114. /*
  115. * This function resolves externals, and this is either called when we process
  116. * relocations or when we call an entry in the PLT table for the first time.
  117. */
  118. char *_dl_find_hash(char *name, struct dyn_elf *rpnt1,
  119. unsigned long instr_addr, struct elf_resolve *f_tpnt, int copyrel)
  120. {
  121. struct elf_resolve *tpnt;
  122. int si;
  123. char *pnt;
  124. int pass;
  125. char *strtab;
  126. Elf32_Sym *symtab;
  127. unsigned long elf_hash_number, hn;
  128. char *weak_result;
  129. struct elf_resolve *first_def;
  130. struct dyn_elf *rpnt, first;
  131. char *data_result = 0; /* nakao */
  132. weak_result = 0;
  133. elf_hash_number = _dl_elf_hash(name);
  134. /* A quick little hack to make sure that any symbol in the executable
  135. will be preferred to one in a shared library. This is necessary so
  136. that any shared library data symbols referenced in the executable
  137. will be seen at the same address by the executable, shared libraries
  138. and dynamically loaded code. -Rob Ryan (robr@cmu.edu) */
  139. if (!copyrel && rpnt1) {
  140. first = (*_dl_symbol_tables);
  141. first.next = rpnt1;
  142. rpnt1 = (&first);
  143. }
  144. /*
  145. * The passes are so that we can first search the regular symbols
  146. * for whatever module was specified, and then search anything
  147. * loaded with RTLD_GLOBAL. When pass is 1, it means we are just
  148. * starting the first dlopened module, and anything above that
  149. * is just the next one in the chain.
  150. */
  151. for (pass = 0; (1 == 1); pass++) {
  152. /*
  153. * If we are just starting to search for RTLD_GLOBAL, setup
  154. * the pointer for the start of the search.
  155. */
  156. if (pass == 1) {
  157. rpnt1 = _dl_handles;
  158. }
  159. /*
  160. * Anything after this, we need to skip to the next module.
  161. */
  162. else if (pass >= 2) {
  163. rpnt1 = rpnt1->next_handle;
  164. }
  165. /*
  166. * Make sure we still have a module, and make sure that this
  167. * module was loaded with RTLD_GLOBAL.
  168. */
  169. if (pass != 0) {
  170. if (rpnt1 == NULL)
  171. break;
  172. if ((rpnt1->flags & RTLD_GLOBAL) == 0)
  173. continue;
  174. }
  175. for (rpnt = (rpnt1 ? rpnt1 : _dl_symbol_tables); rpnt; rpnt = rpnt->next) {
  176. tpnt = rpnt->dyn;
  177. /*
  178. * The idea here is that if we are using dlsym, we want to
  179. * first search the entire chain loaded from dlopen, and
  180. * return a result from that if we found anything. If this
  181. * fails, then we continue the search into the stuff loaded
  182. * when the image was activated. For normal lookups, we start
  183. * with rpnt == NULL, so we should never hit this.
  184. */
  185. if (tpnt->libtype == elf_executable && weak_result != 0) {
  186. break;
  187. }
  188. /*
  189. * Avoid calling .urem here.
  190. */
  191. do_rem(hn, elf_hash_number, tpnt->nbucket);
  192. symtab = (Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  193. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  194. /*
  195. * This crap is required because the first instance of a
  196. * symbol on the chain will be used for all symbol references.
  197. * Thus this instance must be resolved to an address that
  198. * contains the actual function,
  199. */
  200. first_def = NULL;
  201. for (si = tpnt->elf_buckets[hn]; si; si = tpnt->chains[si]) {
  202. pnt = strtab + symtab[si].st_name;
  203. if (_dl_strcmp(pnt, name) == 0 &&
  204. (ELF32_ST_TYPE(symtab[si].st_info) == STT_FUNC ||
  205. ELF32_ST_TYPE(symtab[si].st_info) == STT_NOTYPE ||
  206. ELF32_ST_TYPE(symtab[si].st_info) == STT_OBJECT) &&
  207. symtab[si].st_value != 0) {
  208. /* Here we make sure that we find a module where the symbol is
  209. * actually defined.
  210. */
  211. if (f_tpnt) {
  212. if (!first_def)
  213. first_def = tpnt;
  214. if (first_def == f_tpnt
  215. && symtab[si].st_shndx == 0)
  216. continue;
  217. }
  218. switch (ELF32_ST_BIND(symtab[si].st_info)) {
  219. case STB_GLOBAL:
  220. if (tpnt->libtype != elf_executable &&
  221. ELF32_ST_TYPE(symtab[si].st_info)
  222. == STT_NOTYPE)
  223. { /* nakao */
  224. data_result = tpnt->loadaddr +
  225. symtab[si].st_value; /* nakao */
  226. break; /* nakao */
  227. } else /* nakao */
  228. return tpnt->loadaddr + symtab[si].st_value;
  229. case STB_WEAK:
  230. if (!weak_result)
  231. weak_result =
  232. tpnt->loadaddr + symtab[si].st_value;
  233. break;
  234. default: /* Do local symbols need to be examined? */
  235. break;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. if (data_result)
  242. return data_result; /* nakao */
  243. return weak_result;
  244. }