elfinterp.c 9.4 KB

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