dl-hash.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* vi: set sw=4 ts=4: */
  2. /* Program to load an ELF binary on a linux system, and run it
  3. * after resolving ELF shared library symbols
  4. *
  5. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  6. * David Engel, Hongjiu Lu and Mitch D'Souza
  7. * Copyright (C) 2001-2002, Erik Andersen
  8. *
  9. * All rights reserved.
  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. /*
  51. * This is the hash function that is used by the ELF linker to generate
  52. * the hash table that each executable and library is required to
  53. * have. We need it to decode the hash table.
  54. */
  55. unsigned long _dl_elf_hash(const char *name)
  56. {
  57. unsigned long hash = 0;
  58. unsigned long tmp;
  59. while (*name) {
  60. hash = (hash << 4) + *name++;
  61. if ((tmp = hash & 0xf0000000))
  62. hash ^= tmp >> 24;
  63. hash &= ~tmp;
  64. };
  65. return hash;
  66. }
  67. /*
  68. * Check to see if a library has already been added to the hash chain.
  69. */
  70. struct elf_resolve *_dl_check_hashed_files(const char *libname)
  71. {
  72. struct elf_resolve *tpnt;
  73. int len = _dl_strlen(libname);
  74. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  75. if (_dl_strncmp(tpnt->libname, libname, len) == 0 &&
  76. (tpnt->libname[len] == '\0' || tpnt->libname[len] == '.'))
  77. return tpnt;
  78. }
  79. return NULL;
  80. }
  81. /*
  82. * We call this function when we have just read an ELF library or executable.
  83. * We add the relevant info to the symbol chain, so that we can resolve all
  84. * externals properly.
  85. */
  86. struct elf_resolve *_dl_add_elf_hash_table(const char *libname,
  87. char *loadaddr, unsigned long *dynamic_info, unsigned long dynamic_addr,
  88. unsigned long dynamic_size)
  89. {
  90. unsigned long *hash_addr;
  91. struct elf_resolve *tpnt;
  92. int i;
  93. if (!_dl_loaded_modules) {
  94. tpnt = _dl_loaded_modules =
  95. (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  96. _dl_memset(tpnt, 0, sizeof(*tpnt));
  97. } else {
  98. tpnt = _dl_loaded_modules;
  99. while (tpnt->next)
  100. tpnt = tpnt->next;
  101. tpnt->next = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  102. _dl_memset(tpnt->next, 0, sizeof(*(tpnt->next)));
  103. tpnt->next->prev = tpnt;
  104. tpnt = tpnt->next;
  105. };
  106. tpnt->next = NULL;
  107. tpnt->init_flag = 0;
  108. tpnt->libname = _dl_strdup(libname);
  109. tpnt->dynamic_addr = dynamic_addr;
  110. tpnt->dynamic_size = dynamic_size;
  111. tpnt->libtype = loaded_file;
  112. if (dynamic_info[DT_HASH] != 0) {
  113. hash_addr = (unsigned long *) (intptr_t)(dynamic_info[DT_HASH] + loadaddr);
  114. tpnt->nbucket = *hash_addr++;
  115. tpnt->nchain = *hash_addr++;
  116. tpnt->elf_buckets = hash_addr;
  117. hash_addr += tpnt->nbucket;
  118. tpnt->chains = hash_addr;
  119. }
  120. tpnt->loadaddr = loadaddr;
  121. for (i = 0; i < 24; i++)
  122. tpnt->dynamic_info[i] = dynamic_info[i];
  123. #ifdef __mips__
  124. {
  125. Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
  126. while(dpnt->d_tag) {
  127. if (dpnt->d_tag == DT_MIPS_GOTSYM)
  128. tpnt->mips_gotsym = dpnt->d_un.d_val;
  129. if (dpnt->d_tag == DT_MIPS_LOCAL_GOTNO)
  130. tpnt->mips_local_gotno = dpnt->d_un.d_val;
  131. if (dpnt->d_tag == DT_MIPS_SYMTABNO)
  132. tpnt->mips_symtabno = dpnt->d_un.d_val;
  133. dpnt++;
  134. }
  135. }
  136. #endif
  137. return tpnt;
  138. }
  139. /*
  140. * This function resolves externals, and this is either called when we process
  141. * relocations or when we call an entry in the PLT table for the first time.
  142. */
  143. char *_dl_find_hash(const char *name, struct dyn_elf *rpnt1,
  144. struct elf_resolve *f_tpnt, enum caller_type caller_type)
  145. {
  146. struct elf_resolve *tpnt;
  147. int si;
  148. char *pnt;
  149. int pass;
  150. char *strtab;
  151. Elf32_Sym *symtab;
  152. unsigned long elf_hash_number, hn;
  153. char *weak_result;
  154. struct elf_resolve *first_def;
  155. struct dyn_elf *rpnt, first;
  156. char *data_result = 0; /* nakao */
  157. weak_result = 0;
  158. elf_hash_number = _dl_elf_hash(name);
  159. /* A quick little hack to make sure that any symbol in the executable
  160. will be preferred to one in a shared library. This is necessary so
  161. that any shared library data symbols referenced in the executable
  162. will be seen at the same address by the executable, shared libraries
  163. and dynamically loaded code. -Rob Ryan (robr@cmu.edu) */
  164. if (!caller_type && rpnt1) {
  165. first = (*_dl_symbol_tables);
  166. first.next = rpnt1;
  167. rpnt1 = (&first);
  168. }
  169. /*
  170. * The passes are so that we can first search the regular symbols
  171. * for whatever module was specified, and then search anything
  172. * loaded with RTLD_GLOBAL. When pass is 1, it means we are just
  173. * starting the first dlopened module, and anything above that
  174. * is just the next one in the chain.
  175. */
  176. for (pass = 0; (1 == 1); pass++) {
  177. /*
  178. * If we are just starting to search for RTLD_GLOBAL, setup
  179. * the pointer for the start of the search.
  180. */
  181. if (pass == 1) {
  182. rpnt1 = _dl_handles;
  183. }
  184. /*
  185. * Anything after this, we need to skip to the next module.
  186. */
  187. else if (pass >= 2) {
  188. rpnt1 = rpnt1->next_handle;
  189. }
  190. /*
  191. * Make sure we still have a module, and make sure that this
  192. * module was loaded with RTLD_GLOBAL.
  193. */
  194. if (pass != 0) {
  195. if (rpnt1 == NULL)
  196. break;
  197. if ((rpnt1->flags & RTLD_GLOBAL) == 0)
  198. continue;
  199. }
  200. for (rpnt = (rpnt1 ? rpnt1 : _dl_symbol_tables); rpnt; rpnt = rpnt->next) {
  201. tpnt = rpnt->dyn;
  202. /*
  203. * The idea here is that if we are using dlsym, we want to
  204. * first search the entire chain loaded from dlopen, and
  205. * return a result from that if we found anything. If this
  206. * fails, then we continue the search into the stuff loaded
  207. * when the image was activated. For normal lookups, we start
  208. * with rpnt == NULL, so we should never hit this.
  209. */
  210. if (tpnt->libtype == elf_executable && weak_result != 0) {
  211. break;
  212. }
  213. /*
  214. * Avoid calling .urem here.
  215. */
  216. do_rem(hn, elf_hash_number, tpnt->nbucket);
  217. symtab = (Elf32_Sym *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  218. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  219. /*
  220. * This crap is required because the first instance of a
  221. * symbol on the chain will be used for all symbol references.
  222. * Thus this instance must be resolved to an address that
  223. * contains the actual function,
  224. */
  225. first_def = NULL;
  226. for (si = tpnt->elf_buckets[hn]; si; si = tpnt->chains[si]) {
  227. pnt = strtab + symtab[si].st_name;
  228. if (_dl_strcmp(pnt, name) == 0 &&
  229. symtab[si].st_value != 0)
  230. {
  231. if ((ELF32_ST_TYPE(symtab[si].st_info) == STT_FUNC ||
  232. ELF32_ST_TYPE(symtab[si].st_info) == STT_NOTYPE ||
  233. ELF32_ST_TYPE(symtab[si].st_info) == STT_OBJECT) &&
  234. symtab[si].st_shndx != SHN_UNDEF) {
  235. /* Here we make sure that we find a module where the symbol is
  236. * actually defined.
  237. */
  238. if (f_tpnt) {
  239. if (!first_def)
  240. first_def = tpnt;
  241. if (first_def == f_tpnt
  242. && symtab[si].st_shndx == 0)
  243. continue;
  244. }
  245. switch (ELF32_ST_BIND(symtab[si].st_info)) {
  246. case STB_GLOBAL:
  247. if (tpnt->libtype != elf_executable &&
  248. ELF32_ST_TYPE(symtab[si].st_info)
  249. == STT_NOTYPE)
  250. { /* nakao */
  251. data_result = tpnt->loadaddr +
  252. symtab[si].st_value; /* nakao */
  253. break; /* nakao */
  254. } else /* nakao */
  255. return tpnt->loadaddr + symtab[si].st_value;
  256. case STB_WEAK:
  257. if (!weak_result)
  258. weak_result =
  259. tpnt->loadaddr + symtab[si].st_value;
  260. break;
  261. default: /* Do local symbols need to be examined? */
  262. break;
  263. }
  264. }
  265. #ifndef __mips__
  266. /*
  267. * References to the address of a function from an executable file and
  268. * the shared objects associated with it might not resolve to the same
  269. * value. To allow comparisons of function addresses we must resolve
  270. * to the address of the plt entry of the executable instead of the
  271. * real function address.
  272. * see "TIS ELF Specification Version 1.2, Book 3, A-11 (Function
  273. * Adresses)
  274. */
  275. if (resolver != caller_type &&
  276. NULL==f_tpnt && /*trick: don't handle R_??_JMP_SLOT reloc type*/
  277. tpnt->libtype == elf_executable &&
  278. ELF32_ST_TYPE(symtab[si].st_info) == STT_FUNC &&
  279. symtab[si].st_shndx == SHN_UNDEF)
  280. {
  281. return (char*)symtab[si].st_value;
  282. }
  283. #endif
  284. }
  285. }
  286. }
  287. }
  288. if (data_result)
  289. return data_result; /* nakao */
  290. return weak_result;
  291. }