elfinterp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* x86_64 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. ElfW(Addr) instr_addr;
  52. char *symname;
  53. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  54. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  55. symtab_index = ELF_R_SYM(this_reloc->r_info);
  56. symtab = (ElfW(Sym) *)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 = (this_reloc->r_offset + tpnt->loadaddr);
  61. got_addr = (char **)instr_addr;
  62. /* Get the address of the GOT entry. */
  63. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  64. if (unlikely(!new_addr)) {
  65. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  66. _dl_exit(1);
  67. }
  68. #if defined (__SUPPORT_LD_DEBUG__)
  69. if ((unsigned long)got_addr < 0x40000000) {
  70. if (_dl_debug_bindings) {
  71. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  72. if (_dl_debug_detail)
  73. _dl_dprintf(_dl_debug_file,
  74. "\tpatched: %x ==> %x @ %x\n",
  75. *got_addr, new_addr, got_addr);
  76. }
  77. }
  78. if (!_dl_debug_nofixups)
  79. #endif
  80. *got_addr = new_addr;
  81. return (unsigned long)new_addr;
  82. }
  83. static int
  84. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  85. unsigned long rel_addr, unsigned long rel_size,
  86. int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  87. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  88. {
  89. unsigned int i;
  90. char *strtab;
  91. ElfW(Sym) *symtab;
  92. ELF_RELOC *rpnt;
  93. int symtab_index;
  94. /* Parse the relocation information. */
  95. rpnt = (ELF_RELOC *)rel_addr;
  96. rel_size /= sizeof(ELF_RELOC);
  97. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  98. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  99. for (i = 0; i < rel_size; i++, rpnt++) {
  100. int res;
  101. symtab_index = ELF_R_SYM(rpnt->r_info);
  102. debug_sym(symtab, strtab, symtab_index);
  103. debug_reloc(symtab, strtab, rpnt);
  104. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  105. if (res == 0)
  106. continue;
  107. _dl_dprintf(2, "\n%s: ", _dl_progname);
  108. if (symtab_index)
  109. _dl_dprintf(2, "symbol '%s': ",
  110. strtab + symtab[symtab_index].st_name);
  111. if (unlikely(res < 0)) {
  112. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  113. _dl_dprintf(2, "can't handle reloc type "
  114. "%x\n", reloc_type);
  115. _dl_exit(-res);
  116. } else if (unlikely(res > 0)) {
  117. _dl_dprintf(2, "can't resolve symbol\n");
  118. return res;
  119. }
  120. }
  121. return 0;
  122. }
  123. static int
  124. _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  125. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  126. {
  127. int reloc_type;
  128. int symtab_index;
  129. char *symname;
  130. #if defined USE_TLS && USE_TLS
  131. struct elf_resolve *tls_tpnt;
  132. #endif
  133. struct symbol_ref sym_ref;
  134. ElfW(Addr) *reloc_addr;
  135. ElfW(Addr) symbol_addr;
  136. #if defined (__SUPPORT_LD_DEBUG__)
  137. ElfW(Addr) old_val;
  138. #endif
  139. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  140. reloc_type = ELF_R_TYPE(rpnt->r_info);
  141. symtab_index = ELF_R_SYM(rpnt->r_info);
  142. sym_ref.sym = &symtab[symtab_index];
  143. sym_ref.tpnt = NULL;
  144. symbol_addr = 0;
  145. symname = strtab + sym_ref.sym->st_name;
  146. if (symtab_index) {
  147. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  148. elf_machine_type_class(reloc_type), &sym_ref);
  149. /*
  150. * We want to allow undefined references to weak symbols - this
  151. * might have been intentional. We should not be linking local
  152. * symbols here, so all bases should be covered.
  153. */
  154. if (unlikely(!symbol_addr && (ELF_ST_TYPE(sym_ref.sym->st_info) != STT_TLS)
  155. && (ELF_ST_BIND(sym_ref.sym->st_info) != STB_WEAK))) {
  156. /* This may be non-fatal if called from dlopen. */
  157. return 1;
  158. }
  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. #if defined USE_TLS && USE_TLS
  164. tls_tpnt = sym_ref.tpnt;
  165. #endif
  166. } else {
  167. /* Relocs against STN_UNDEF are usually treated as using a
  168. * symbol value of zero, and using the module containing the
  169. * reloc itself. */
  170. symbol_addr = sym_ref.sym->st_value;
  171. #if defined USE_TLS && USE_TLS
  172. tls_tpnt = tpnt;
  173. #endif
  174. }
  175. #if defined (__SUPPORT_LD_DEBUG__)
  176. old_val = *reloc_addr;
  177. #endif
  178. switch (reloc_type) {
  179. case R_X86_64_NONE:
  180. break;
  181. case R_X86_64_64:
  182. *reloc_addr = symbol_addr + rpnt->r_addend;
  183. break;
  184. case R_X86_64_PC32:
  185. *reloc_addr = symbol_addr + rpnt->r_addend - rpnt->r_offset;
  186. break;
  187. case R_X86_64_GLOB_DAT:
  188. case R_X86_64_JUMP_SLOT:
  189. *reloc_addr = symbol_addr + rpnt->r_addend;
  190. break;
  191. /* handled by elf_machine_relative()
  192. case R_X86_64_RELATIVE:
  193. *reloc_addr = map->l_addr + rpnt->r_addend;
  194. break;
  195. */
  196. #if defined USE_TLS && USE_TLS
  197. case R_X86_64_DTPMOD64:
  198. *reloc_addr = tls_tpnt->l_tls_modid;
  199. break;
  200. case R_X86_64_DTPOFF64:
  201. /* During relocation all TLS symbols are defined and used.
  202. * Therefore the offset is already correct. */
  203. *reloc_addr = symbol_addr + rpnt->r_addend;
  204. break;
  205. case R_X86_64_TPOFF64:
  206. /* The offset is negative, forward from the thread pointer.
  207. * We know the offset of the object the symbol is contained in.
  208. * It is a negative value which will be added to the
  209. * thread pointer. */
  210. CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
  211. *reloc_addr = symbol_addr - tls_tpnt->l_tls_offset + rpnt->r_addend;
  212. break;
  213. #endif
  214. case R_X86_64_32:
  215. *(unsigned int *) reloc_addr = symbol_addr + rpnt->r_addend;
  216. /* XXX: should check for overflow eh ? */
  217. break;
  218. case R_X86_64_COPY:
  219. if (symbol_addr) {
  220. #if defined (__SUPPORT_LD_DEBUG__)
  221. if (_dl_debug_move)
  222. _dl_dprintf(_dl_debug_file,
  223. "\t%s move %d bytes from %x to %x\n",
  224. symname, sym_ref.sym->st_size,
  225. symbol_addr, reloc_addr);
  226. #endif
  227. _dl_memcpy((char *)reloc_addr,
  228. (char *)symbol_addr,
  229. sym_ref.sym->st_size);
  230. }
  231. #if defined (__SUPPORT_LD_DEBUG__)
  232. else
  233. _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
  234. #endif
  235. break;
  236. default:
  237. return -1; /* Calls _dl_exit(1). */
  238. }
  239. #if defined (__SUPPORT_LD_DEBUG__)
  240. if (_dl_debug_reloc && _dl_debug_detail)
  241. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  242. old_val, *reloc_addr, reloc_addr);
  243. #endif
  244. return 0;
  245. }
  246. static int
  247. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  248. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  249. {
  250. int reloc_type;
  251. ElfW(Addr) *reloc_addr;
  252. #if defined (__SUPPORT_LD_DEBUG__)
  253. ElfW(Addr) old_val;
  254. #endif
  255. (void)scope;
  256. (void)strtab;
  257. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  258. reloc_type = ELF_R_TYPE(rpnt->r_info);
  259. #if defined (__SUPPORT_LD_DEBUG__)
  260. old_val = *reloc_addr;
  261. #endif
  262. switch (reloc_type) {
  263. case R_X86_64_NONE:
  264. break;
  265. case R_X86_64_JUMP_SLOT:
  266. *reloc_addr += (unsigned long)tpnt->loadaddr;
  267. break;
  268. default:
  269. _dl_exit(1);
  270. }
  271. #if defined (__SUPPORT_LD_DEBUG__)
  272. if (_dl_debug_reloc && _dl_debug_detail)
  273. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  274. old_val, *reloc_addr, reloc_addr);
  275. #endif
  276. return 0;
  277. }
  278. void
  279. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  280. unsigned long rel_addr,
  281. unsigned long rel_size)
  282. {
  283. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  284. }
  285. int
  286. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  287. struct r_scope_elem *scope,
  288. unsigned long rel_addr,
  289. unsigned long rel_size)
  290. {
  291. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  292. }