elfinterp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* vi: set sw=4 ts=4: */
  2. /* sparc 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. *
  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. /* Program to load an ELF binary on a linux system, and run it.
  31. References to symbols in sharable libraries can be resolved by either
  32. an ELF sharable library or a linux style of shared library. */
  33. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  34. I ever taken any courses on internals. This program was developed using
  35. information available through the book "UNIX SYSTEM V RELEASE 4,
  36. Programmers guide: Ansi C and Programming Support Tools", which did
  37. a more than adequate job of explaining everything required to get this
  38. working. */
  39. /* Some SPARC opcodes we need to use for self-modifying code. */
  40. #define OPCODE_NOP 0x01000000 /* nop */
  41. #define OPCODE_CALL 0x40000000 /* call ?; add PC-rel word address */
  42. #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
  43. #define OPCODE_JMP_G1 0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
  44. #define OPCODE_SAVE_SP 0x9de3bfa8 /* save %sp, -(16+6)*4, %sp */
  45. #define OPCODE_BA 0x30800000 /* b,a ?; add PC-rel word address */
  46. extern int _dl_linux_resolve(void);
  47. unsigned long
  48. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  49. {
  50. int reloc_type;
  51. ELF_RELOC *this_reloc;
  52. char *strtab;
  53. ElfW(Sym) *symtab;
  54. int symtab_index;
  55. char *rel_addr;
  56. char *new_addr;
  57. char **got_addr;
  58. ElfW(Addr) instr_addr;
  59. char *symname;
  60. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  61. /*
  62. * Generate the correct relocation index into the .rela.plt section.
  63. */
  64. reloc_entry = (reloc_entry >> 10) - 0xc;
  65. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  66. reloc_type = ELF_R_TYPE(this_reloc->r_info);
  67. symtab_index = ELF_R_SYM(this_reloc->r_info);
  68. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  69. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  70. symname = strtab + symtab[symtab_index].st_name;
  71. if (unlikely(reloc_type != R_SPARC_JMP_SLOT)) {
  72. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  73. _dl_progname);
  74. _dl_exit(1);
  75. }
  76. /* Address of the jump instruction to fix up. */
  77. instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
  78. got_addr = (char **)instr_addr;
  79. /* Get the address of the GOT entry */
  80. new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  81. if (unlikely(!new_addr)) {
  82. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  83. _dl_exit(1);
  84. }
  85. #if defined (__SUPPORT_LD_DEBUG__)
  86. if ((unsigned long)got_addr < 0x40000000) {
  87. if (_dl_debug_bindings) {
  88. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  89. if (_dl_debug_detail)
  90. _dl_dprintf(_dl_debug_file,
  91. "\tpatched: %x ==> %x @ %x\n",
  92. *got_addr, new_addr, got_addr);
  93. }
  94. }
  95. if (!_dl_debug_nofixups)
  96. #endif
  97. {
  98. got_addr[1] = (char *) (0x03000000 | (((unsigned int) new_addr >> 10) & 0x3fffff));
  99. got_addr[2] = (char *) (0x81c06000 | ((unsigned int) new_addr & 0x3ff));
  100. }
  101. return (unsigned long)new_addr;
  102. }
  103. static int
  104. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  105. unsigned long rel_addr, unsigned long rel_size,
  106. int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
  107. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  108. {
  109. unsigned int i;
  110. char *strtab;
  111. ElfW(Sym) *symtab;
  112. ELF_RELOC *rpnt;
  113. int symtab_index;
  114. /* Parse the relocation information. */
  115. rpnt = (ELF_RELOC *)rel_addr;
  116. rel_size /= sizeof(ELF_RELOC);
  117. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  118. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  119. for (i = 0; i < rel_size; i++, rpnt++) {
  120. int res;
  121. symtab_index = ELF_R_SYM(rpnt->r_info);
  122. debug_sym(symtab, strtab, symtab_index);
  123. debug_reloc(symtab, strtab, rpnt);
  124. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  125. if (res == 0)
  126. continue;
  127. _dl_dprintf(2, "\n%s: ", _dl_progname);
  128. if (symtab_index)
  129. _dl_dprintf(2, "symbol '%s': ",
  130. strtab + symtab[symtab_index].st_name);
  131. if (unlikely(res < 0)) {
  132. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  133. _dl_dprintf(2, "can't handle reloc type "
  134. #if defined (__SUPPORT_LD_DEBUG__)
  135. "%s\n", _dl_reltypes(reloc_type));
  136. #else
  137. "%x\n", reloc_type);
  138. #endif
  139. _dl_exit(-res);
  140. } else if (unlikely(res > 0)) {
  141. _dl_dprintf(2, "can't resolve symbol\n");
  142. return res;
  143. }
  144. }
  145. return 0;
  146. }
  147. static int
  148. _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  149. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  150. {
  151. int reloc_type;
  152. int symtab_index;
  153. char *symname;
  154. ElfW(Sym) *sym;
  155. ElfW(Addr) *reloc_addr;
  156. ElfW(Addr) symbol_addr;
  157. #if defined (__SUPPORT_LD_DEBUG__)
  158. ElfW(Addr) old_val;
  159. #endif
  160. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  161. reloc_type = ELF_R_TYPE(rpnt->r_info);
  162. symtab_index = ELF_R_SYM(rpnt->r_info);
  163. sym = &symtab[symtab_index];
  164. symbol_addr = 0;
  165. symname = strtab + sym->st_name;
  166. if (symtab_index) {
  167. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  168. elf_machine_type_class(reloc_type));
  169. /*
  170. * We want to allow undefined references to weak symbols - this
  171. * might have been intentional. We should not be linking local
  172. * symbols here, so all bases should be covered.
  173. */
  174. if (unlikely(!symbol_addr && ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
  175. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  176. _dl_exit(1);
  177. }
  178. }
  179. #if defined (__SUPPORT_LD_DEBUG__)
  180. old_val = *reloc_addr;
  181. #endif
  182. symbol_addr += rpnt->r_addend; /* Assume copy relocs have zero addend. */
  183. switch (reloc_type) {
  184. case R_SPARC_NONE:
  185. break;
  186. #if 0 /* these dont really seem to be useful */
  187. case R_SPARC_8:
  188. *(char *) reloc_addr = symbol_addr;
  189. break;
  190. case R_SPARC_16:
  191. *(short *) reloc_addr = symbol_addr;
  192. break;
  193. case R_SPARC_DISP8:
  194. *(char *) reloc_addr = (symbol_addr) - (Elf32_Addr) reloc_addr;
  195. break;
  196. case R_SPARC_DISP16:
  197. *(short *) reloc_addr = (symbol_addr) - (Elf32_Addr) reloc_addr;
  198. break;
  199. #endif
  200. case R_SPARC_DISP32:
  201. *reloc_addr = symbol_addr - (unsigned int) reloc_addr;
  202. break;
  203. case R_SPARC_LO10:
  204. if (!symbol_addr)
  205. symbol_addr = tpnt->loadaddr + rpnt->r_addend;
  206. else
  207. symbol_addr += rpnt->r_addend;
  208. *reloc_addr = (*reloc_addr & ~0x3ff)|(symbol_addr & 0x3ff);
  209. break;
  210. case R_SPARC_GLOB_DAT:
  211. case R_SPARC_32:
  212. *reloc_addr = symbol_addr;
  213. break;
  214. case R_SPARC_JMP_SLOT:
  215. /*
  216. value = symbol_addr;
  217. value += reloc->r_addend;
  218. disp = value - reloc_addr;
  219. reloc_addr[1] = OPCODE_JMP_G1 | (value & 0x3ff);
  220. reloc_addr[0] = OPCODE_SETHI_G1 | (value >> 10);
  221. reloc_addr[1] = OPCODE_JMP_G1 | ((symbol_addr-(Elf32_Addr)reloc_addr) & 0x3ff);
  222. reloc_addr[0] = OPCODE_SETHI_G1 | ((symbol_addr-(Elf32_Addr)reloc_addr) >> 10);
  223. */
  224. reloc_addr[1] = 0x03000000 | ((symbol_addr >> 10) & 0x3fffff);
  225. reloc_addr[2] = 0x81c06000 | (symbol_addr & 0x3ff);
  226. break;
  227. case R_SPARC_RELATIVE:
  228. *reloc_addr += tpnt->loadaddr + rpnt->r_addend;
  229. break;
  230. case R_SPARC_WDISP30:
  231. *reloc_addr = (*reloc_addr & 0xc0000000)|
  232. ((symbol_addr - (unsigned int) reloc_addr) >> 2);
  233. break;
  234. case R_SPARC_HI22:
  235. if (!symbol_addr)
  236. symbol_addr = tpnt->loadaddr + rpnt->r_addend;
  237. else
  238. symbol_addr += rpnt->r_addend;
  239. *reloc_addr = (*reloc_addr & 0xffc00000) | (symbol_addr >> 10);
  240. break;
  241. case R_SPARC_COPY:
  242. if (symbol_addr) {
  243. #if defined (__SUPPORT_LD_DEBUG__)
  244. if (_dl_debug_move)
  245. _dl_dprintf(_dl_debug_file,
  246. "\t%s move %d bytes from %x to %x\n",
  247. symname, sym->st_size,
  248. symbol_addr, reloc_addr);
  249. #endif
  250. _dl_memcpy((char *)reloc_addr,
  251. (char *)symbol_addr,
  252. sym->st_size);
  253. } else
  254. _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
  255. break;
  256. default:
  257. return -1; /* Calls _dl_exit(1). */
  258. }
  259. #if defined (__SUPPORT_LD_DEBUG__)
  260. if (_dl_debug_reloc && _dl_debug_detail)
  261. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  262. old_val, *reloc_addr, reloc_addr);
  263. #endif
  264. return 0;
  265. }
  266. #undef __SPARC_LAZY_RELOC_WORKS
  267. #ifdef __SPARC_LAZY_RELOC_WORKS
  268. static int
  269. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  270. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  271. {
  272. int reloc_type;
  273. int symtab_index;
  274. ElfW(Addr) *reloc_addr;
  275. #if defined (__SUPPORT_LD_DEBUG__)
  276. ElfW(Addr) old_val;
  277. #endif
  278. (void)scope;
  279. symtab_index = ELF_R_SYM(rpnt->r_info);
  280. (void)strtab;
  281. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  282. reloc_type = ELF_R_TYPE(rpnt->r_info);
  283. #if defined (__SUPPORT_LD_DEBUG__)
  284. old_val = *reloc_addr;
  285. #endif
  286. switch (reloc_type) {
  287. case R_SPARC_NONE:
  288. break;
  289. case R_SPARC_JMP_SLOT:
  290. break;
  291. default:
  292. _dl_exit(1);
  293. }
  294. #if defined (__SUPPORT_LD_DEBUG__)
  295. if (_dl_debug_reloc && _dl_debug_detail)
  296. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  297. old_val, *reloc_addr, reloc_addr);
  298. #endif
  299. return 0;
  300. }
  301. #endif
  302. void
  303. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  304. unsigned long rel_addr,
  305. unsigned long rel_size)
  306. {
  307. #ifdef __SPARC_LAZY_RELOC_WORKS
  308. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  309. #else
  310. _dl_parse_relocation_information(rpnt, rel_addr, rel_size);
  311. #endif
  312. }
  313. int
  314. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  315. unsigned long rel_addr,
  316. unsigned long rel_size)
  317. {
  318. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  319. }