dl-hash.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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@codepoet.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. static inline 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->libtype = loaded_file;
  106. if (dynamic_info[DT_HASH] != 0) {
  107. hash_addr = (unsigned long *) (intptr_t)(dynamic_info[DT_HASH]);
  108. tpnt->nbucket = *hash_addr++;
  109. tpnt->nchain = *hash_addr++;
  110. tpnt->elf_buckets = hash_addr;
  111. hash_addr += tpnt->nbucket;
  112. tpnt->chains = hash_addr;
  113. }
  114. tpnt->loadaddr = (ElfW(Addr))loadaddr;
  115. for (i = 0; i < DYNAMIC_SIZE; i++)
  116. tpnt->dynamic_info[i] = dynamic_info[i];
  117. return tpnt;
  118. }
  119. /*
  120. * This function resolves externals, and this is either called when we process
  121. * relocations or when we call an entry in the PLT table for the first time.
  122. */
  123. char *_dl_find_hash(const char *name, struct dyn_elf *rpnt, struct elf_resolve *mytpnt, int type_class)
  124. {
  125. struct elf_resolve *tpnt;
  126. int si;
  127. char *strtab;
  128. Elf32_Sym *symtab;
  129. unsigned long elf_hash_number, hn;
  130. const ElfW(Sym) *sym;
  131. char *weak_result = NULL;
  132. elf_hash_number = _dl_elf_hash(name);
  133. for (; rpnt; rpnt = rpnt->next) {
  134. tpnt = rpnt->dyn;
  135. if (!(tpnt->rtld_flags & RTLD_GLOBAL) && mytpnt) {
  136. if (mytpnt == tpnt)
  137. ;
  138. else {
  139. struct init_fini_list *tmp;
  140. for (tmp = mytpnt->rtld_local; tmp; tmp = tmp->next) {
  141. if (tmp->tpnt == tpnt)
  142. break;
  143. }
  144. if (!tmp)
  145. continue;
  146. }
  147. }
  148. /* Don't search the executable when resolving a copy reloc. */
  149. if ((type_class & ELF_RTYPE_CLASS_COPY) && tpnt->libtype == elf_executable)
  150. continue;
  151. /* Avoid calling .urem here. */
  152. do_rem(hn, elf_hash_number, tpnt->nbucket);
  153. symtab = (Elf32_Sym *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB]);
  154. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]);
  155. for (si = tpnt->elf_buckets[hn]; si != STN_UNDEF; si = tpnt->chains[si]) {
  156. sym = &symtab[si];
  157. if (type_class & (sym->st_shndx == SHN_UNDEF))
  158. continue;
  159. if (_dl_strcmp(strtab + sym->st_name, name) != 0)
  160. continue;
  161. if (sym->st_value == 0)
  162. continue;
  163. if (ELF32_ST_TYPE(sym->st_info) > STT_FUNC)
  164. continue;
  165. switch (ELF32_ST_BIND(sym->st_info)) {
  166. case STB_WEAK:
  167. #if 0
  168. /* Perhaps we should support old style weak symbol handling
  169. * per what glibc does when you export LD_DYNAMIC_WEAK */
  170. if (!weak_result)
  171. weak_result = (char *)tpnt->loadaddr + sym->st_value;
  172. break;
  173. #endif
  174. case STB_GLOBAL:
  175. return (char*)tpnt->loadaddr + sym->st_value;
  176. default: /* Local symbols not handled here */
  177. break;
  178. }
  179. }
  180. }
  181. return weak_result;
  182. }