elfinterp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* vi: set sw=4 ts=4: */
  2. /* x86_64 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. int reloc_type;
  46. ELF_RELOC *this_reloc;
  47. char *strtab;
  48. ElfW(Sym) *symtab;
  49. int symtab_index;
  50. char *rel_addr;
  51. char *new_addr;
  52. char **got_addr;
  53. ElfW(Addr) instr_addr;
  54. char *symname;
  55. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  56. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  57. reloc_type = ELF_R_TYPE(this_reloc->r_info);
  58. symtab_index = ELF_R_SYM(this_reloc->r_info);
  59. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  60. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  61. symname = strtab + symtab[symtab_index].st_name;
  62. if (unlikely(reloc_type != R_X86_64_JUMP_SLOT)) {
  63. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  64. _dl_progname);
  65. _dl_exit(1);
  66. }
  67. /* Address of the jump instruction to fix up. */
  68. instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
  69. got_addr = (char **)instr_addr;
  70. /* Get the address of the GOT entry. */
  71. new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  72. if (unlikely(!new_addr)) {
  73. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  74. _dl_exit(1);
  75. }
  76. #if defined (__SUPPORT_LD_DEBUG__)
  77. if ((unsigned long)got_addr < 0x40000000) {
  78. if (_dl_debug_bindings) {
  79. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  80. if (_dl_debug_detail)
  81. _dl_dprintf(_dl_debug_file,
  82. "\tpatched: %x ==> %x @ %x\n",
  83. *got_addr, new_addr, got_addr);
  84. }
  85. }
  86. if (!_dl_debug_nofixups)
  87. #endif
  88. *got_addr = new_addr;
  89. return (unsigned long)new_addr;
  90. }
  91. static int
  92. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  93. unsigned long rel_addr, unsigned long rel_size,
  94. int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
  95. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  96. {
  97. unsigned int i;
  98. char *strtab;
  99. ElfW(Sym) *symtab;
  100. ELF_RELOC *rpnt;
  101. int symtab_index;
  102. /* Parse the relocation information. */
  103. rpnt = (ELF_RELOC *)rel_addr;
  104. rel_size /= sizeof(ELF_RELOC);
  105. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  106. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  107. for (i = 0; i < rel_size; i++, rpnt++) {
  108. int res;
  109. symtab_index = ELF_R_SYM(rpnt->r_info);
  110. debug_sym(symtab, strtab, symtab_index);
  111. debug_reloc(symtab, strtab, rpnt);
  112. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  113. if (res == 0)
  114. continue;
  115. _dl_dprintf(2, "\n%s: ", _dl_progname);
  116. if (symtab_index)
  117. _dl_dprintf(2, "symbol '%s': ",
  118. strtab + symtab[symtab_index].st_name);
  119. if (unlikely(res < 0)) {
  120. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  121. _dl_dprintf(2, "can't handle reloc type "
  122. #if defined (__SUPPORT_LD_DEBUG__)
  123. "%s\n", _dl_reltypes(reloc_type));
  124. #else
  125. "%x\n", reloc_type);
  126. #endif
  127. _dl_exit(-res);
  128. } else if (unlikely(res > 0)) {
  129. _dl_dprintf(2, "can't resolve symbol\n");
  130. return res;
  131. }
  132. }
  133. return 0;
  134. }
  135. static int
  136. _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  137. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  138. {
  139. int reloc_type;
  140. int symtab_index;
  141. char *symname;
  142. ElfW(Sym) *sym;
  143. ElfW(Addr) *reloc_addr;
  144. ElfW(Addr) symbol_addr;
  145. #if defined (__SUPPORT_LD_DEBUG__)
  146. ElfW(Addr) old_val;
  147. #endif
  148. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  149. reloc_type = ELF_R_TYPE(rpnt->r_info);
  150. symtab_index = ELF_R_SYM(rpnt->r_info);
  151. sym = &symtab[symtab_index];
  152. symbol_addr = 0;
  153. symname = strtab + sym->st_name;
  154. if (symtab_index) {
  155. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  156. elf_machine_type_class(reloc_type));
  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_BIND(sym->st_info) != STB_WEAK)) {
  163. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  164. _dl_exit(1);
  165. }
  166. }
  167. #if defined (__SUPPORT_LD_DEBUG__)
  168. old_val = *reloc_addr;
  169. #endif
  170. switch (reloc_type) {
  171. case R_X86_64_NONE:
  172. break;
  173. case R_X86_64_64:
  174. *reloc_addr = symbol_addr + rpnt->r_addend;
  175. break;
  176. case R_X86_64_PC32:
  177. *reloc_addr = symbol_addr + rpnt->r_addend - rpnt->r_offset;
  178. break;
  179. case R_X86_64_GLOB_DAT:
  180. case R_X86_64_JUMP_SLOT:
  181. *reloc_addr = symbol_addr + rpnt->r_addend;
  182. break;
  183. /* handled by elf_machine_relative()
  184. case R_X86_64_RELATIVE:
  185. *reloc_addr = map->l_addr + rpnt->r_addend;
  186. break;
  187. */
  188. case R_X86_64_DTPMOD64:
  189. *reloc_addr = 1;
  190. break;
  191. case R_X86_64_DTPOFF64:
  192. *reloc_addr = sym->st_value + rpnt->r_addend;
  193. break;
  194. case R_X86_64_TPOFF64:
  195. *reloc_addr = sym->st_value + rpnt->r_addend - symbol_addr;
  196. break;
  197. case R_X86_64_32:
  198. *(unsigned int *) reloc_addr = symbol_addr + rpnt->r_addend;
  199. /* XXX: should check for overflow eh ? */
  200. break;
  201. case R_X86_64_COPY:
  202. if (symbol_addr) {
  203. #if defined (__SUPPORT_LD_DEBUG__)
  204. if (_dl_debug_move)
  205. _dl_dprintf(_dl_debug_file,
  206. "\t%s move %d bytes from %x to %x\n",
  207. symname, sym->st_size,
  208. symbol_addr, reloc_addr);
  209. #endif
  210. _dl_memcpy((char *)reloc_addr,
  211. (char *)symbol_addr,
  212. sym->st_size);
  213. } else
  214. _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
  215. break;
  216. default:
  217. return -1; /* Calls _dl_exit(1). */
  218. }
  219. #if defined (__SUPPORT_LD_DEBUG__)
  220. if (_dl_debug_reloc && _dl_debug_detail)
  221. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  222. old_val, *reloc_addr, reloc_addr);
  223. #endif
  224. return 0;
  225. }
  226. static int
  227. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  228. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  229. {
  230. int reloc_type;
  231. int symtab_index;
  232. ElfW(Addr) *reloc_addr;
  233. #if defined (__SUPPORT_LD_DEBUG__)
  234. ElfW(Addr) old_val;
  235. #endif
  236. (void)scope;
  237. symtab_index = ELF_R_SYM(rpnt->r_info);
  238. (void)strtab;
  239. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  240. reloc_type = ELF_R_TYPE(rpnt->r_info);
  241. #if defined (__SUPPORT_LD_DEBUG__)
  242. old_val = *reloc_addr;
  243. #endif
  244. switch (reloc_type) {
  245. case R_X86_64_NONE:
  246. break;
  247. case R_X86_64_JUMP_SLOT:
  248. *reloc_addr += (unsigned long)tpnt->loadaddr;
  249. break;
  250. default:
  251. _dl_exit(1);
  252. }
  253. #if defined (__SUPPORT_LD_DEBUG__)
  254. if (_dl_debug_reloc && _dl_debug_detail)
  255. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  256. old_val, *reloc_addr, reloc_addr);
  257. #endif
  258. return 0;
  259. }
  260. void
  261. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  262. unsigned long rel_addr,
  263. unsigned long rel_size)
  264. {
  265. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  266. }
  267. int
  268. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  269. unsigned long rel_addr,
  270. unsigned long rel_size)
  271. {
  272. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  273. }