elfinterp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* m68k 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. * Adapted to ELF/68k by Andreas Schwab.
  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. #include "ldso.h"
  34. extern int _dl_linux_resolve(void);
  35. unsigned long
  36. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  37. {
  38. ELF_RELOC *this_reloc;
  39. char *strtab;
  40. ElfW(Sym) *symtab;
  41. int symtab_index;
  42. char *rel_addr;
  43. char *new_addr;
  44. char **got_addr;
  45. ElfW(Addr) instr_addr;
  46. char *symname;
  47. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  48. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  49. symtab_index = ELF_R_SYM(this_reloc->r_info);
  50. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  51. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  52. symname = strtab + symtab[symtab_index].st_name;
  53. /* Address of the jump instruction to fix up. */
  54. instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
  55. got_addr = (char **)instr_addr;
  56. /* Get the address of the GOT entry. */
  57. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  58. if (unlikely(!new_addr)) {
  59. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  60. _dl_exit(1);
  61. }
  62. #if defined (__SUPPORT_LD_DEBUG__)
  63. if ((unsigned long)got_addr < 0x40000000) {
  64. if (_dl_debug_bindings) {
  65. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  66. if (_dl_debug_detail)
  67. _dl_dprintf(_dl_debug_file,
  68. "\tpatched: %x ==> %x @ %x\n",
  69. *got_addr, new_addr, got_addr);
  70. }
  71. }
  72. if (!_dl_debug_nofixups)
  73. #endif
  74. *got_addr = new_addr;
  75. return (unsigned int)new_addr;
  76. }
  77. static int
  78. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  79. unsigned long rel_addr, unsigned long rel_size,
  80. int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  81. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  82. {
  83. unsigned int i;
  84. char *strtab;
  85. ElfW(Sym) *symtab;
  86. ELF_RELOC *rpnt;
  87. int symtab_index;
  88. /* Parse the relocation information. */
  89. rpnt = (ELF_RELOC *)rel_addr;
  90. rel_size /= sizeof(ELF_RELOC);
  91. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  92. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  93. for (i = 0; i < rel_size; i++, rpnt++) {
  94. int res;
  95. symtab_index = ELF_R_SYM(rpnt->r_info);
  96. debug_sym(symtab, strtab, symtab_index);
  97. debug_reloc(symtab, strtab, rpnt);
  98. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  99. if (res == 0)
  100. continue;
  101. _dl_dprintf(2, "\n%s: ", _dl_progname);
  102. if (symtab_index)
  103. _dl_dprintf(2, "symbol '%s': ",
  104. strtab + symtab[symtab_index].st_name);
  105. if (unlikely(res < 0)) {
  106. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  107. _dl_dprintf(2, "can't handle reloc type "
  108. "%x\n", reloc_type);
  109. _dl_exit(-res);
  110. } else if (unlikely(res > 0)) {
  111. _dl_dprintf(2, "can't resolve symbol\n");
  112. return res;
  113. }
  114. }
  115. return 0;
  116. }
  117. static int
  118. _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  119. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  120. {
  121. int reloc_type;
  122. int symtab_index;
  123. char *symname;
  124. struct symbol_ref sym_ref;
  125. ElfW(Addr) *reloc_addr;
  126. ElfW(Addr) symbol_addr;
  127. #if defined (__SUPPORT_LD_DEBUG__)
  128. ElfW(Addr) old_val;
  129. #endif
  130. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  131. reloc_type = ELF_R_TYPE(rpnt->r_info);
  132. symtab_index = ELF_R_SYM(rpnt->r_info);
  133. sym_ref.sym = &symtab[symtab_index];
  134. sym_ref.tpnt = NULL;
  135. symbol_addr = 0;
  136. symname = strtab + sym_ref.sym->st_name;
  137. if (symtab_index) {
  138. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  139. elf_machine_type_class(reloc_type), &sym_ref);
  140. /*
  141. * We want to allow undefined references to weak symbols - this
  142. * might have been intentional. We should not be linking local
  143. * symbols here, so all bases should be covered.
  144. */
  145. if (unlikely(!symbol_addr && ELF_ST_BIND(sym_ref.sym->st_info) != STB_WEAK)) {
  146. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  147. _dl_exit(1);
  148. }
  149. if (_dl_trace_prelink) {
  150. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  151. &sym_ref, elf_machine_type_class(reloc_type));
  152. }
  153. }
  154. #if defined (__SUPPORT_LD_DEBUG__)
  155. old_val = *reloc_addr;
  156. #endif
  157. switch (reloc_type) {
  158. case R_68K_NONE:
  159. break;
  160. case R_68K_8:
  161. *(char *) reloc_addr = symbol_addr + rpnt->r_addend;
  162. break;
  163. case R_68K_16:
  164. *(short *) reloc_addr = symbol_addr + rpnt->r_addend;
  165. break;
  166. case R_68K_32:
  167. *reloc_addr = symbol_addr + rpnt->r_addend;
  168. break;
  169. case R_68K_PC8:
  170. *(char *) reloc_addr = (symbol_addr + rpnt->r_addend
  171. - (unsigned int) reloc_addr);
  172. break;
  173. case R_68K_PC16:
  174. *(short *) reloc_addr = (symbol_addr + rpnt->r_addend
  175. - (unsigned int) reloc_addr);
  176. break;
  177. case R_68K_PC32:
  178. *reloc_addr = (symbol_addr + rpnt->r_addend
  179. - (unsigned int) reloc_addr);
  180. break;
  181. case R_68K_GLOB_DAT:
  182. case R_68K_JMP_SLOT:
  183. *reloc_addr = symbol_addr + rpnt->r_addend;
  184. break;
  185. /* handled by elf_machine_relative()
  186. case R_68K_RELATIVE:
  187. *reloc_addr = ((unsigned int) tpnt->loadaddr
  188. / * Compatibility kludge. * /
  189. + (rpnt->r_addend ? : *reloc_addr));
  190. */
  191. break;
  192. case R_68K_COPY:
  193. if (symbol_addr) {
  194. #if defined (__SUPPORT_LD_DEBUG__)
  195. if (_dl_debug_move)
  196. _dl_dprintf(_dl_debug_file,
  197. "\t%s move %d bytes from %x to %x\n",
  198. symname, sym_ref.sym->st_size,
  199. symbol_addr, reloc_addr);
  200. #endif
  201. _dl_memcpy ((void *) reloc_addr,
  202. (void *) symbol_addr,
  203. sym_ref.sym->st_size);
  204. }
  205. #if defined (__SUPPORT_LD_DEBUG__)
  206. else
  207. _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
  208. #endif
  209. break;
  210. default:
  211. return -1; /* Calls _dl_exit(1). */
  212. }
  213. #if defined (__SUPPORT_LD_DEBUG__)
  214. if (_dl_debug_reloc && _dl_debug_detail)
  215. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  216. old_val, *reloc_addr, reloc_addr);
  217. #endif
  218. return 0;
  219. }
  220. #undef LAZY_RELOC_WORKS
  221. #ifdef LAZY_RELOC_WORKS
  222. static int
  223. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  224. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  225. {
  226. int reloc_type;
  227. int symtab_index;
  228. ElfW(Addr) *reloc_addr;
  229. #if defined (__SUPPORT_LD_DEBUG__)
  230. ElfW(Addr) old_val;
  231. #endif
  232. (void)scope;
  233. symtab_index = ELF_R_SYM(rpnt->r_info);
  234. (void)strtab;
  235. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  236. reloc_type = ELF_R_TYPE(rpnt->r_info);
  237. #if defined (__SUPPORT_LD_DEBUG__)
  238. old_val = *reloc_addr;
  239. #endif
  240. switch (reloc_type) {
  241. case R_68K_NONE:
  242. break;
  243. case R_68K_JMP_SLOT:
  244. *reloc_addr += (unsigned int) tpnt->loadaddr;
  245. break;
  246. default:
  247. _dl_exit(1);
  248. }
  249. #if defined (__SUPPORT_LD_DEBUG__)
  250. if (_dl_debug_reloc && _dl_debug_detail)
  251. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  252. old_val, *reloc_addr, reloc_addr);
  253. #endif
  254. return 0;
  255. }
  256. #endif
  257. void
  258. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  259. unsigned long rel_addr,
  260. unsigned long rel_size)
  261. {
  262. #ifdef LAZY_RELOC_WORKS
  263. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  264. #else
  265. _dl_parse_relocation_information(rpnt, &_dl_loaded_modules->symbol_scope, rel_addr, rel_size);
  266. #endif
  267. }
  268. int
  269. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  270. struct r_scope_elem *scope,
  271. unsigned long rel_addr,
  272. unsigned long rel_size)
  273. {
  274. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  275. }