elfinterp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* sparc 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. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. The name of the above contributors may not be
  14. * used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /* Program to load an ELF binary on a linux system, and run it.
  30. References to symbols in sharable libraries can be resolved by either
  31. an ELF sharable library or a linux style of shared library. */
  32. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  33. I ever taken any courses on internals. This program was developed using
  34. information available through the book "UNIX SYSTEM V RELEASE 4,
  35. Programmers guide: Ansi C and Programming Support Tools", which did
  36. a more than adequate job of explaining everything required to get this
  37. working. */
  38. /* Some SPARC opcodes we need to use for self-modifying code. */
  39. #define OPCODE_NOP 0x01000000 /* nop */
  40. #define OPCODE_CALL 0x40000000 /* call ?; add PC-rel word address */
  41. #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
  42. #define OPCODE_JMP_G1 0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
  43. #define OPCODE_SAVE_SP 0x9de3bfa8 /* save %sp, -(16+6)*4, %sp */
  44. #define OPCODE_BA 0x30800000 /* b,a ?; add PC-rel word address */
  45. extern int _dl_linux_resolve(void);
  46. unsigned long
  47. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  48. {
  49. ELF_RELOC *this_reloc;
  50. char *strtab;
  51. ElfW(Sym) *symtab;
  52. int symtab_index;
  53. char *rel_addr;
  54. char *new_addr;
  55. char **got_addr;
  56. ElfW(Addr) instr_addr;
  57. char *symname;
  58. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  59. /*
  60. * Generate the correct relocation index into the .rela.plt section.
  61. */
  62. reloc_entry = (reloc_entry >> 10) - 0xc;
  63. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  64. symtab_index = ELF_R_SYM(this_reloc->r_info);
  65. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  66. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  67. symname = strtab + symtab[symtab_index].st_name;
  68. /* Address of the jump instruction to fix up. */
  69. instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
  70. got_addr = (char **)instr_addr;
  71. /* Get the address of the GOT entry */
  72. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  73. if (unlikely(!new_addr)) {
  74. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  75. _dl_exit(1);
  76. }
  77. #if defined (__SUPPORT_LD_DEBUG__)
  78. if ((unsigned long)got_addr < 0x40000000) {
  79. if (_dl_debug_bindings) {
  80. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  81. if (_dl_debug_detail)
  82. _dl_dprintf(_dl_debug_file,
  83. "\tpatched: %x ==> %x @ %x\n",
  84. *got_addr, new_addr, got_addr);
  85. }
  86. }
  87. if (!_dl_debug_nofixups)
  88. #endif
  89. {
  90. got_addr[1] = (char *) (OPCODE_SETHI_G1 | (((unsigned int) new_addr >> 10) & 0x3fffff));
  91. got_addr[2] = (char *) (OPCODE_JMP_G1 | ((unsigned int) new_addr & 0x3ff));
  92. }
  93. return (unsigned long)new_addr;
  94. }
  95. static int
  96. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  97. unsigned long rel_addr, unsigned long rel_size,
  98. int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  99. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  100. {
  101. unsigned int i;
  102. char *strtab;
  103. ElfW(Sym) *symtab;
  104. ELF_RELOC *rpnt;
  105. int symtab_index;
  106. /* Parse the relocation information. */
  107. rpnt = (ELF_RELOC *)rel_addr;
  108. rel_size /= sizeof(ELF_RELOC);
  109. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  110. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  111. for (i = 0; i < rel_size; i++, rpnt++) {
  112. int res;
  113. symtab_index = ELF_R_SYM(rpnt->r_info);
  114. debug_sym(symtab, strtab, symtab_index);
  115. debug_reloc(symtab, strtab, rpnt);
  116. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  117. if (res == 0)
  118. continue;
  119. _dl_dprintf(2, "\n%s: ", _dl_progname);
  120. if (symtab_index)
  121. _dl_dprintf(2, "symbol '%s': ",
  122. strtab + symtab[symtab_index].st_name);
  123. if (unlikely(res < 0)) {
  124. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  125. _dl_dprintf(2, "can't handle reloc type "
  126. "%x\n", reloc_type);
  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 r_scope_elem *scope,
  137. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  138. {
  139. int reloc_type;
  140. int symtab_index;
  141. char *symname;
  142. struct elf_resolve *tls_tpnt = NULL;
  143. struct symbol_ref sym_ref;
  144. ElfW(Addr) *reloc_addr;
  145. ElfW(Addr) symbol_addr;
  146. #if defined (__SUPPORT_LD_DEBUG__)
  147. ElfW(Addr) old_val;
  148. #endif
  149. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  150. reloc_type = ELF_R_TYPE(rpnt->r_info);
  151. symtab_index = ELF_R_SYM(rpnt->r_info);
  152. sym_ref.sym = &symtab[symtab_index];
  153. sym_ref.tpnt = NULL;
  154. symbol_addr = 0;
  155. symname = strtab + sym_ref.sym->st_name;
  156. if (symtab_index) {
  157. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  158. elf_machine_type_class(reloc_type), &sym_ref);
  159. /*
  160. * We want to allow undefined references to weak symbols - this
  161. * might have been intentional. We should not be linking local
  162. * symbols here, so all bases should be covered.
  163. */
  164. if (unlikely(!symbol_addr && (ELF_ST_TYPE(sym_ref.sym->st_info) != STT_TLS)
  165. && (ELF_ST_BIND(sym_ref.sym->st_info) != STB_WEAK))) {
  166. /* This may be non-fatal if called from dlopen. */
  167. return 1;
  168. }
  169. if (_dl_trace_prelink) {
  170. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  171. &sym_ref, elf_machine_type_class(reloc_type));
  172. }
  173. tls_tpnt = sym_ref.tpnt;
  174. } else {
  175. /* Relocs against STN_UNDEF are usually treated as using a
  176. * symbol value of zero, and using the module containing the
  177. * reloc itself. */
  178. symbol_addr = sym_ref.sym->st_value;
  179. tls_tpnt = tpnt;
  180. }
  181. #if defined (__SUPPORT_LD_DEBUG__)
  182. old_val = *reloc_addr;
  183. #endif
  184. symbol_addr += rpnt->r_addend; /* Assume copy relocs have zero addend. */
  185. switch (reloc_type) {
  186. case R_SPARC_NONE:
  187. break;
  188. case R_SPARC_DISP32:
  189. *reloc_addr = symbol_addr - (unsigned int) reloc_addr;
  190. break;
  191. case R_SPARC_LO10:
  192. if (!symbol_addr)
  193. symbol_addr = tpnt->loadaddr + rpnt->r_addend;
  194. else
  195. symbol_addr += rpnt->r_addend;
  196. *reloc_addr = (*reloc_addr & ~0x3ff) | (symbol_addr & 0x3ff);
  197. break;
  198. case R_SPARC_GLOB_DAT:
  199. case R_SPARC_32:
  200. *reloc_addr = symbol_addr;
  201. break;
  202. case R_SPARC_JMP_SLOT:
  203. reloc_addr[1] = OPCODE_SETHI_G1 | (( symbol_addr >> 10 ) & 0x3fffff);
  204. reloc_addr[2] = OPCODE_JMP_G1 | ( symbol_addr & 0x3ff );
  205. break;
  206. case R_SPARC_RELATIVE:
  207. *reloc_addr += tpnt->loadaddr + rpnt->r_addend;
  208. break;
  209. case R_SPARC_WDISP30:
  210. *reloc_addr = (*reloc_addr & 0xc0000000)|
  211. ((symbol_addr - (unsigned int) reloc_addr) >> 2);
  212. break;
  213. case R_SPARC_HI22:
  214. if (!symbol_addr)
  215. symbol_addr = tpnt->loadaddr + rpnt->r_addend;
  216. else
  217. symbol_addr += rpnt->r_addend;
  218. *reloc_addr = (*reloc_addr & 0xffc00000) | (symbol_addr >> 10);
  219. break;
  220. case R_SPARC_COPY:
  221. if (symbol_addr) {
  222. #if defined (__SUPPORT_LD_DEBUG__)
  223. if (_dl_debug_move)
  224. _dl_dprintf(_dl_debug_file,
  225. "\t%s move %d bytes from %x to %x\n",
  226. symname, sym_ref.sym->st_size,
  227. symbol_addr, reloc_addr);
  228. #endif
  229. _dl_memcpy((char *)reloc_addr,
  230. (char *)symbol_addr,
  231. sym_ref.sym->st_size);
  232. }
  233. #if defined (__SUPPORT_LD_DEBUG__)
  234. else
  235. _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
  236. #endif
  237. break;
  238. #if defined USE_TLS && USE_TLS
  239. case R_SPARC_TLS_DTPMOD32:
  240. *reloc_addr = tls_tpnt->l_tls_modid;
  241. break;
  242. case R_SPARC_TLS_DTPOFF32:
  243. /* During relocation all TLS symbols are defined and used.
  244. * Therefore the offset is already correct. */
  245. *reloc_addr = sym_ref.sym->st_value + rpnt->r_addend;
  246. break;
  247. case R_SPARC_TLS_TPOFF32:
  248. /* The offset is negative, forward from the thread pointer.
  249. * We know the offset of the object the symbol is contained in.
  250. * It is a negative value which will be added to the
  251. * thread pointer. */
  252. CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
  253. *reloc_addr = sym_ref.sym->st_value - tls_tpnt->l_tls_offset + rpnt->r_addend;
  254. break;
  255. #endif
  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 r_scope_elem *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, &_dl_loaded_modules->symbol_scope,
  311. rel_addr, rel_size);
  312. #endif
  313. }
  314. int
  315. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  316. struct r_scope_elem *scope,
  317. unsigned long rel_addr,
  318. unsigned long rel_size)
  319. {
  320. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  321. }