elfinterp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* i386 ELF shared library loader suppport
  2. *
  3. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  4. * David Engel, Hongjiu Lu and Mitch D'Souza
  5. * Copyright (C) 2001-2004 Erik Andersen
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. The name of the above contributors may not be
  15. * used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include "ldso.h"
  31. /* Program to load an ELF binary on a linux system, and run it.
  32. References to symbols in sharable libraries can be resolved by either
  33. an ELF sharable library or a linux style of shared library. */
  34. extern int _dl_linux_resolve(void);
  35. unsigned long
  36. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  37. {
  38. ELF_RELOC *this_reloc;
  39. char *strtab;
  40. ElfW(Sym) *symtab;
  41. int symtab_index;
  42. char *rel_addr;
  43. char *new_addr;
  44. char **got_addr;
  45. unsigned long instr_addr;
  46. char *symname;
  47. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  48. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  49. symtab_index = ELF_R_SYM(this_reloc->r_info);
  50. symtab = (ElfW(Sym) *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  51. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  52. symname = strtab + symtab[symtab_index].st_name;
  53. /* Address of the jump instruction to fix up. */
  54. instr_addr = ((unsigned long)this_reloc->r_offset +
  55. (unsigned long)tpnt->loadaddr);
  56. got_addr = (char **)instr_addr;
  57. /* Get the address of the GOT entry. */
  58. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  59. if (unlikely(!new_addr)) {
  60. _dl_dprintf(2, "%s: can't resolve symbol '%s' in lib '%s'.\n", _dl_progname, symname, tpnt->libname);
  61. _dl_exit(1);
  62. }
  63. #if defined (__SUPPORT_LD_DEBUG__)
  64. if ((unsigned long)got_addr < 0x40000000) {
  65. if (_dl_debug_bindings) {
  66. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  67. if (_dl_debug_detail)
  68. _dl_dprintf(_dl_debug_file,
  69. "\n\tpatched: %x ==> %x @ %x",
  70. *got_addr, new_addr, got_addr);
  71. }
  72. }
  73. if (!_dl_debug_nofixups) {
  74. *got_addr = new_addr;
  75. }
  76. #else
  77. *got_addr = new_addr;
  78. #endif
  79. return (unsigned long)new_addr;
  80. }
  81. static int
  82. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  83. unsigned long rel_addr, unsigned long rel_size,
  84. int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  85. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  86. {
  87. unsigned int i;
  88. char *strtab;
  89. ElfW(Sym) *symtab;
  90. ELF_RELOC *rpnt;
  91. int symtab_index;
  92. /* Parse the relocation information. */
  93. rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
  94. rel_size /= sizeof(ELF_RELOC);
  95. symtab = (ElfW(Sym) *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  96. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  97. for (i = 0; i < rel_size; i++, rpnt++) {
  98. int res;
  99. symtab_index = ELF_R_SYM(rpnt->r_info);
  100. debug_sym(symtab, strtab, symtab_index);
  101. debug_reloc(symtab, strtab, rpnt);
  102. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  103. if (res == 0)
  104. continue;
  105. _dl_dprintf(2, "\n%s: ", _dl_progname);
  106. if (symtab_index)
  107. _dl_dprintf(2, "symbol '%s': ",
  108. strtab + symtab[symtab_index].st_name);
  109. if (unlikely(res < 0)) {
  110. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  111. _dl_dprintf(2, "can't handle reloc type %x in lib '%s'\n",
  112. reloc_type, tpnt->libname);
  113. return res;
  114. } else if (unlikely(res > 0)) {
  115. _dl_dprintf(2, "can't resolve symbol in lib '%s'.\n", tpnt->libname);
  116. return res;
  117. }
  118. }
  119. return 0;
  120. }
  121. static int
  122. _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  123. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  124. {
  125. int reloc_type;
  126. int symtab_index;
  127. char *symname;
  128. struct elf_resolve *tls_tpnt = NULL;
  129. unsigned long *reloc_addr;
  130. unsigned long symbol_addr;
  131. #if defined (__SUPPORT_LD_DEBUG__)
  132. unsigned long old_val;
  133. #endif
  134. struct symbol_ref sym_ref;
  135. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  136. reloc_type = ELF_R_TYPE(rpnt->r_info);
  137. symtab_index = ELF_R_SYM(rpnt->r_info);
  138. symbol_addr = 0;
  139. sym_ref.sym = &symtab[symtab_index];
  140. sym_ref.tpnt = NULL;
  141. symname = strtab + symtab[symtab_index].st_name;
  142. if (symtab_index) {
  143. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  144. elf_machine_type_class(reloc_type), &sym_ref);
  145. /*
  146. * We want to allow undefined references to weak symbols - this
  147. * might have been intentional. We should not be linking local
  148. * symbols here, so all bases should be covered.
  149. */
  150. if (unlikely(!symbol_addr && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
  151. && ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK))
  152. return 1;
  153. if (_dl_trace_prelink) {
  154. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  155. &sym_ref, elf_machine_type_class(reloc_type));
  156. }
  157. tls_tpnt = sym_ref.tpnt;
  158. } else {
  159. symbol_addr = symtab[symtab_index].st_value;
  160. tls_tpnt = tpnt;
  161. }
  162. #if defined (__SUPPORT_LD_DEBUG__)
  163. old_val = *reloc_addr;
  164. #endif
  165. switch (reloc_type) {
  166. case R_386_NONE:
  167. break;
  168. case R_386_32:
  169. *reloc_addr += symbol_addr;
  170. break;
  171. case R_386_PC32:
  172. *reloc_addr += symbol_addr - (unsigned long)reloc_addr;
  173. break;
  174. case R_386_GLOB_DAT:
  175. case R_386_JMP_SLOT:
  176. *reloc_addr = symbol_addr;
  177. break;
  178. case R_386_RELATIVE:
  179. *reloc_addr += (unsigned long)tpnt->loadaddr;
  180. break;
  181. case R_386_COPY:
  182. if (symbol_addr) {
  183. #if defined (__SUPPORT_LD_DEBUG__)
  184. if (_dl_debug_move)
  185. _dl_dprintf(_dl_debug_file,
  186. "\n%s move %d bytes from %x to %x",
  187. symname, symtab[symtab_index].st_size,
  188. symbol_addr, reloc_addr);
  189. #endif
  190. _dl_memcpy((char *)reloc_addr,
  191. (char *)symbol_addr,
  192. symtab[symtab_index].st_size);
  193. }
  194. break;
  195. #if defined USE_TLS && USE_TLS
  196. case R_386_TLS_DTPMOD32:
  197. *reloc_addr = tls_tpnt->l_tls_modid;
  198. break;
  199. case R_386_TLS_DTPOFF32:
  200. /* During relocation all TLS symbols are defined and used.
  201. * Therefore the offset is already correct. */
  202. *reloc_addr = symbol_addr;
  203. break;
  204. case R_386_TLS_TPOFF32:
  205. /* The offset is positive, backward from the thread pointer. */
  206. CHECK_STATIC_TLS((struct link_map*) tls_tpnt);
  207. *reloc_addr += tls_tpnt->l_tls_offset - symbol_addr;
  208. break;
  209. case R_386_TLS_TPOFF:
  210. /* The offset is negative, forward from the thread pointer. */
  211. CHECK_STATIC_TLS((struct link_map*) tls_tpnt);
  212. *reloc_addr += symbol_addr - tls_tpnt->l_tls_offset;
  213. break;
  214. #endif
  215. default:
  216. return -1;
  217. }
  218. #if defined (__SUPPORT_LD_DEBUG__)
  219. if (_dl_debug_reloc && _dl_debug_detail)
  220. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  221. old_val, *reloc_addr, reloc_addr);
  222. #endif
  223. return 0;
  224. }
  225. static int
  226. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  227. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  228. {
  229. int reloc_type;
  230. unsigned long *reloc_addr;
  231. #if defined (__SUPPORT_LD_DEBUG__)
  232. unsigned long old_val;
  233. #endif
  234. (void)scope;
  235. (void)symtab;
  236. (void)strtab;
  237. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  238. reloc_type = ELF_R_TYPE(rpnt->r_info);
  239. #if defined (__SUPPORT_LD_DEBUG__)
  240. old_val = *reloc_addr;
  241. #endif
  242. switch (reloc_type) {
  243. case R_386_NONE:
  244. break;
  245. case R_386_JMP_SLOT:
  246. *reloc_addr += (unsigned long)tpnt->loadaddr;
  247. break;
  248. default:
  249. return -1;
  250. }
  251. #if defined (__SUPPORT_LD_DEBUG__)
  252. if (_dl_debug_reloc && _dl_debug_detail)
  253. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  254. old_val, *reloc_addr, reloc_addr);
  255. #endif
  256. return 0;
  257. }
  258. void
  259. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  260. unsigned long rel_addr,
  261. unsigned long rel_size)
  262. {
  263. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  264. }
  265. int
  266. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  267. struct r_scope_elem *scope,
  268. unsigned long rel_addr,
  269. unsigned long rel_size)
  270. {
  271. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  272. }