elfinterp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  35. I ever taken any courses on internals. This program was developed using
  36. information available through the book "UNIX SYSTEM V RELEASE 4,
  37. Programmers guide: Ansi C and Programming Support Tools", which did
  38. a more than adequate job of explaining everything required to get this
  39. working. */
  40. extern int _dl_linux_resolve(void);
  41. unsigned long
  42. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  43. {
  44. ELF_RELOC *this_reloc;
  45. char *strtab;
  46. ElfW(Sym) *symtab;
  47. int symtab_index;
  48. char *rel_addr;
  49. char *new_addr;
  50. char **got_addr;
  51. unsigned long instr_addr;
  52. char *symname;
  53. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  54. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  55. symtab_index = ELF_R_SYM(this_reloc->r_info);
  56. symtab = (ElfW(Sym) *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  57. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  58. symname = strtab + symtab[symtab_index].st_name;
  59. /* Address of the jump instruction to fix up. */
  60. instr_addr = ((unsigned long)this_reloc->r_offset +
  61. (unsigned long)tpnt->loadaddr);
  62. got_addr = (char **)instr_addr;
  63. /* Get the address of the GOT entry. */
  64. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  65. if (unlikely(!new_addr)) {
  66. _dl_dprintf(2, "%s: can't resolve symbol '%s' in lib '%s'.\n", _dl_progname, symname, tpnt->libname);
  67. _dl_exit(1);
  68. }
  69. #if defined (__SUPPORT_LD_DEBUG__)
  70. if ((unsigned long)got_addr < 0x40000000) {
  71. if (_dl_debug_bindings) {
  72. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  73. if (_dl_debug_detail)
  74. _dl_dprintf(_dl_debug_file,
  75. "\n\tpatched: %x ==> %x @ %x",
  76. *got_addr, new_addr, got_addr);
  77. }
  78. }
  79. if (!_dl_debug_nofixups) {
  80. *got_addr = new_addr;
  81. }
  82. #else
  83. *got_addr = new_addr;
  84. #endif
  85. return (unsigned long)new_addr;
  86. }
  87. static int
  88. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  89. unsigned long rel_addr, unsigned long rel_size,
  90. int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  91. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  92. {
  93. unsigned int i;
  94. char *strtab;
  95. ElfW(Sym) *symtab;
  96. ELF_RELOC *rpnt;
  97. int symtab_index;
  98. /* Parse the relocation information. */
  99. rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
  100. rel_size /= sizeof(ELF_RELOC);
  101. symtab = (ElfW(Sym) *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  102. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  103. for (i = 0; i < rel_size; i++, rpnt++) {
  104. int res;
  105. symtab_index = ELF_R_SYM(rpnt->r_info);
  106. debug_sym(symtab, strtab, symtab_index);
  107. debug_reloc(symtab, strtab, rpnt);
  108. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  109. if (res == 0)
  110. continue;
  111. _dl_dprintf(2, "\n%s: ", _dl_progname);
  112. if (symtab_index)
  113. _dl_dprintf(2, "symbol '%s': ",
  114. strtab + symtab[symtab_index].st_name);
  115. if (unlikely(res < 0)) {
  116. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  117. _dl_dprintf(2, "can't handle reloc type %x in lib '%s'\n",
  118. reloc_type, tpnt->libname);
  119. return res;
  120. } else if (unlikely(res > 0)) {
  121. _dl_dprintf(2, "can't resolve symbol in lib '%s'.\n", tpnt->libname);
  122. return res;
  123. }
  124. }
  125. return 0;
  126. }
  127. static int
  128. _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  129. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  130. {
  131. int reloc_type;
  132. int symtab_index;
  133. char *symname;
  134. struct elf_resolve *tls_tpnt = NULL;
  135. unsigned long *reloc_addr;
  136. unsigned long symbol_addr;
  137. #if defined (__SUPPORT_LD_DEBUG__)
  138. unsigned long old_val;
  139. #endif
  140. struct symbol_ref sym_ref;
  141. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  142. reloc_type = ELF_R_TYPE(rpnt->r_info);
  143. symtab_index = ELF_R_SYM(rpnt->r_info);
  144. symbol_addr = 0;
  145. sym_ref.sym = &symtab[symtab_index];
  146. sym_ref.tpnt = NULL;
  147. symname = strtab + symtab[symtab_index].st_name;
  148. if (symtab_index) {
  149. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  150. elf_machine_type_class(reloc_type), &sym_ref);
  151. /*
  152. * We want to allow undefined references to weak symbols - this
  153. * might have been intentional. We should not be linking local
  154. * symbols here, so all bases should be covered.
  155. */
  156. if (unlikely(!symbol_addr && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
  157. && ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK))
  158. return 1;
  159. if (_dl_trace_prelink) {
  160. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  161. &sym_ref, elf_machine_type_class(reloc_type));
  162. }
  163. tls_tpnt = sym_ref.tpnt;
  164. } else {
  165. symbol_addr = symtab[symtab_index].st_value;
  166. tls_tpnt = tpnt;
  167. }
  168. #if defined (__SUPPORT_LD_DEBUG__)
  169. old_val = *reloc_addr;
  170. #endif
  171. switch (reloc_type) {
  172. case R_386_NONE:
  173. break;
  174. case R_386_32:
  175. *reloc_addr += symbol_addr;
  176. break;
  177. case R_386_PC32:
  178. *reloc_addr += symbol_addr - (unsigned long)reloc_addr;
  179. break;
  180. case R_386_GLOB_DAT:
  181. case R_386_JMP_SLOT:
  182. *reloc_addr = symbol_addr;
  183. break;
  184. case R_386_RELATIVE:
  185. *reloc_addr += (unsigned long)tpnt->loadaddr;
  186. break;
  187. case R_386_COPY:
  188. if (symbol_addr) {
  189. #if defined (__SUPPORT_LD_DEBUG__)
  190. if (_dl_debug_move)
  191. _dl_dprintf(_dl_debug_file,
  192. "\n%s move %d bytes from %x to %x",
  193. symname, symtab[symtab_index].st_size,
  194. symbol_addr, reloc_addr);
  195. #endif
  196. _dl_memcpy((char *)reloc_addr,
  197. (char *)symbol_addr,
  198. symtab[symtab_index].st_size);
  199. }
  200. break;
  201. #if defined USE_TLS && USE_TLS
  202. case R_386_TLS_DTPMOD32:
  203. *reloc_addr = tls_tpnt->l_tls_modid;
  204. break;
  205. case R_386_TLS_DTPOFF32:
  206. /* During relocation all TLS symbols are defined and used.
  207. * Therefore the offset is already correct. */
  208. *reloc_addr = symbol_addr;
  209. break;
  210. case R_386_TLS_TPOFF32:
  211. /* The offset is positive, backward from the thread pointer. */
  212. CHECK_STATIC_TLS((struct link_map*) tls_tpnt);
  213. *reloc_addr += tls_tpnt->l_tls_offset - symbol_addr;
  214. break;
  215. case R_386_TLS_TPOFF:
  216. /* The offset is negative, forward from the thread pointer. */
  217. CHECK_STATIC_TLS((struct link_map*) tls_tpnt);
  218. *reloc_addr += symbol_addr - tls_tpnt->l_tls_offset;
  219. break;
  220. #endif
  221. default:
  222. return -1;
  223. }
  224. #if defined (__SUPPORT_LD_DEBUG__)
  225. if (_dl_debug_reloc && _dl_debug_detail)
  226. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  227. old_val, *reloc_addr, reloc_addr);
  228. #endif
  229. return 0;
  230. }
  231. static int
  232. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  233. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  234. {
  235. int reloc_type;
  236. unsigned long *reloc_addr;
  237. #if defined (__SUPPORT_LD_DEBUG__)
  238. unsigned long old_val;
  239. #endif
  240. (void)scope;
  241. (void)symtab;
  242. (void)strtab;
  243. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  244. reloc_type = ELF_R_TYPE(rpnt->r_info);
  245. #if defined (__SUPPORT_LD_DEBUG__)
  246. old_val = *reloc_addr;
  247. #endif
  248. switch (reloc_type) {
  249. case R_386_NONE:
  250. break;
  251. case R_386_JMP_SLOT:
  252. *reloc_addr += (unsigned long)tpnt->loadaddr;
  253. break;
  254. default:
  255. return -1;
  256. }
  257. #if defined (__SUPPORT_LD_DEBUG__)
  258. if (_dl_debug_reloc && _dl_debug_detail)
  259. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  260. old_val, *reloc_addr, reloc_addr);
  261. #endif
  262. return 0;
  263. }
  264. void
  265. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  266. unsigned long rel_addr,
  267. unsigned long rel_size)
  268. {
  269. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  270. }
  271. int
  272. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  273. struct r_scope_elem *scope,
  274. unsigned long rel_addr,
  275. unsigned long rel_size)
  276. {
  277. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  278. }