elfinterp.c 11 KB

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