hash.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <stdlib.h>*/
  16. #include "string.h"
  17. #include <linux/unistd.h>
  18. #include <linux/elf.h>
  19. #include "libdl/dlfcn.h"
  20. #include "hash.h"
  21. #include "linuxelf.h"
  22. #include "syscall.h"
  23. #include "string.h"
  24. #include "sysdep.h"
  25. /*
  26. * This is the start of the linked list that describes all of the files present
  27. * in the system with pointers to all of the symbol, string, and hash tables,
  28. * as well as all of the other good stuff in the binary.
  29. */
  30. struct elf_resolve * _dl_loaded_modules = NULL;
  31. /*
  32. * This is the list of modules that are loaded when the image is first
  33. * started. As we add more via dlopen, they get added into other
  34. * chains.
  35. */
  36. struct dyn_elf * _dl_symbol_tables = NULL;
  37. /*
  38. * This is the list of modules that are loaded via dlopen. We may need
  39. * to search these for RTLD_GLOBAL files.
  40. */
  41. struct dyn_elf * _dl_handles = NULL;
  42. /*
  43. * This is the hash function that is used by the ELF linker to generate
  44. * the hash table that each executable and library is required to
  45. * have. We need it to decode the hash table.
  46. */
  47. unsigned long _dl_elf_hash(const char * name){
  48. unsigned long hash = 0;
  49. unsigned long tmp;
  50. while (*name){
  51. hash = (hash << 4) + *name++;
  52. if((tmp = hash & 0xf0000000)) hash ^= tmp >> 24;
  53. hash &= ~tmp;
  54. };
  55. return hash;
  56. }
  57. /*
  58. * Check to see if a library has already been added to the hash chain.
  59. */
  60. struct elf_resolve * _dl_check_hashed_files(char * libname){
  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,
  77. unsigned int * dynamic_info,
  78. unsigned int dynamic_addr,
  79. unsigned int dynamic_size){
  80. unsigned int * hash_addr;
  81. struct elf_resolve * tpnt;
  82. int i;
  83. if (!_dl_loaded_modules) {
  84. tpnt = _dl_loaded_modules =
  85. (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  86. _dl_memset (tpnt, 0, sizeof (*tpnt));
  87. }
  88. else {
  89. tpnt = _dl_loaded_modules;
  90. while(tpnt->next) tpnt = tpnt->next;
  91. tpnt->next = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  92. _dl_memset (tpnt->next, 0, sizeof (*(tpnt->next)));
  93. tpnt->next->prev = tpnt;
  94. tpnt = tpnt->next;
  95. };
  96. tpnt->next = NULL;
  97. tpnt->init_flag = 0;
  98. tpnt->libname = _dl_strdup(libname);
  99. tpnt->dynamic_addr = dynamic_addr;
  100. tpnt->dynamic_size = dynamic_size;
  101. tpnt->libtype = loaded_file;
  102. if( dynamic_info[DT_HASH] != 0 )
  103. {
  104. hash_addr = (unsigned int *) (dynamic_info[DT_HASH] + loadaddr);
  105. tpnt->nbucket = *hash_addr++;
  106. tpnt->nchain = *hash_addr++;
  107. tpnt->elf_buckets = hash_addr;
  108. hash_addr += tpnt->nbucket;
  109. tpnt->chains = hash_addr;
  110. }
  111. tpnt->loadaddr = loadaddr;
  112. for(i=0; i<24; i++) tpnt->dynamic_info[i] = dynamic_info[i];
  113. return tpnt;
  114. }
  115. /*
  116. * This function resolves externals, and this is either called when we process
  117. * relocations or when we call an entry in the PLT table for the first time.
  118. */
  119. char * _dl_find_hash(char * name, struct dyn_elf * rpnt1,
  120. unsigned int instr_addr, struct elf_resolve * f_tpnt,
  121. int copyrel){
  122. struct elf_resolve * tpnt;
  123. int si;
  124. char * pnt;
  125. int pass;
  126. char * strtab;
  127. struct elf32_sym * symtab;
  128. unsigned int elf_hash_number, hn;
  129. char * weak_result;
  130. struct elf_resolve * first_def;
  131. struct dyn_elf * rpnt, first;
  132. char * data_result = 0; /* nakao */
  133. weak_result = 0;
  134. elf_hash_number = _dl_elf_hash(name);
  135. /* A quick little hack to make sure that any symbol in the executable
  136. will be preferred to one in a shared library. This is necessary so
  137. that any shared library data symbols referenced in the executable
  138. will be seen at the same address by the executable, shared libraries
  139. and dynamically loaded code. -Rob Ryan (robr@cmu.edu) */
  140. if(!copyrel && rpnt1) {
  141. first=(*_dl_symbol_tables);
  142. first.next=rpnt1;
  143. rpnt1=(&first);
  144. }
  145. /*
  146. * The passes are so that we can first search the regular symbols
  147. * for whatever module was specified, and then search anything
  148. * loaded with RTLD_GLOBAL. When pass is 1, it means we are just
  149. * starting the first dlopened module, and anything above that
  150. * is just the next one in the chain.
  151. */
  152. for(pass = 0; (1==1); pass++)
  153. {
  154. /*
  155. * If we are just starting to search for RTLD_GLOBAL, setup
  156. * the pointer for the start of the search.
  157. */
  158. if( pass == 1) {
  159. rpnt1 = _dl_handles;
  160. }
  161. /*
  162. * Anything after this, we need to skip to the next module.
  163. */
  164. else if( pass >= 2) {
  165. rpnt1 = rpnt1->next_handle;
  166. }
  167. /*
  168. * Make sure we still have a module, and make sure that this
  169. * module was loaded with RTLD_GLOBAL.
  170. */
  171. if( pass != 0 )
  172. {
  173. if( rpnt1 == NULL ) break;
  174. if( (rpnt1->flags & RTLD_GLOBAL) == 0) continue;
  175. }
  176. for(rpnt = (rpnt1 ? rpnt1 : _dl_symbol_tables);
  177. rpnt; rpnt = rpnt->next) {
  178. tpnt = rpnt->dyn;
  179. /*
  180. * The idea here is that if we are using dlsym, we want to
  181. * first search the entire chain loaded from dlopen, and
  182. * return a result from that if we found anything. If this
  183. * fails, then we continue the search into the stuff loaded
  184. * when the image was activated. For normal lookups, we start
  185. * with rpnt == NULL, so we should never hit this.
  186. */
  187. if( tpnt->libtype == elf_executable
  188. && weak_result != 0 )
  189. {
  190. break;
  191. }
  192. /*
  193. * Avoid calling .urem here.
  194. */
  195. do_rem(hn, elf_hash_number, tpnt->nbucket);
  196. symtab = (struct elf32_sym *) (tpnt->dynamic_info[DT_SYMTAB] +
  197. tpnt->loadaddr);
  198. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  199. /*
  200. * This crap is required because the first instance of a
  201. * symbol on the chain will be used for all symbol references.
  202. * Thus this instance must be resolved to an address that
  203. * contains the actual function,
  204. */
  205. first_def = NULL;
  206. for(si = tpnt->elf_buckets[hn]; si; si = tpnt->chains[si]){
  207. pnt = strtab + symtab[si].st_name;
  208. if(_dl_strcmp(pnt, name) == 0 &&
  209. (ELF32_ST_TYPE(symtab[si].st_info) == STT_FUNC ||
  210. ELF32_ST_TYPE(symtab[si].st_info) == STT_NOTYPE ||
  211. ELF32_ST_TYPE(symtab[si].st_info) == STT_OBJECT) &&
  212. symtab[si].st_value != 0) {
  213. /* Here we make sure that we find a module where the symbol is
  214. * actually defined.
  215. */
  216. if(f_tpnt) {
  217. if(!first_def) first_def = tpnt;
  218. if(first_def == f_tpnt && symtab[si].st_shndx == 0)
  219. continue;
  220. }
  221. switch(ELF32_ST_BIND(symtab[si].st_info)){
  222. case STB_GLOBAL:
  223. if ( tpnt->libtype != elf_executable
  224. && ELF32_ST_TYPE(symtab[si].st_info) == STT_NOTYPE) { /* nakao */
  225. data_result = tpnt->loadaddr + 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) weak_result = tpnt->loadaddr + symtab[si].st_value;
  231. break;
  232. default: /* Do local symbols need to be examined? */
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. if (data_result) return data_result; /* nakao */
  240. return weak_result;
  241. }