elfinterp.c 10 KB

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