elfinterp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* vi: set sw=4 ts=4: */
  2. /* m68k 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. * Adapted to ELF/68k by Andreas Schwab.
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. /* Program to load an ELF binary on a linux system, and run it.
  32. References to symbols in sharable libraries can be resolved by either
  33. an ELF sharable library or a linux style of shared library. */
  34. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  35. I ever taken any courses on internals. This program was developed using
  36. information available through the book "UNIX SYSTEM V RELEASE 4,
  37. Programmers guide: Ansi C and Programming Support Tools", which did
  38. a more than adequate job of explaining everything required to get this
  39. working. */
  40. #include "ldso.h"
  41. extern int _dl_linux_resolve(void);
  42. unsigned long
  43. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  44. {
  45. ELF_RELOC *this_reloc;
  46. char *strtab;
  47. ElfW(Sym) *symtab;
  48. int symtab_index;
  49. char *rel_addr;
  50. char *new_addr;
  51. char **got_addr;
  52. ElfW(Addr) instr_addr;
  53. char *symname;
  54. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  55. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  56. symtab_index = ELF_R_SYM(this_reloc->r_info);
  57. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  58. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  59. symname = strtab + symtab[symtab_index].st_name;
  60. /* Address of the jump instruction to fix up. */
  61. instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
  62. got_addr = (char **)instr_addr;
  63. /* Get the address of the GOT entry. */
  64. new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  65. if (unlikely(!new_addr)) {
  66. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  67. _dl_exit(1);
  68. }
  69. #if defined (__SUPPORT_LD_DEBUG__)
  70. if ((unsigned long)got_addr < 0x40000000) {
  71. if (_dl_debug_bindings) {
  72. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  73. if (_dl_debug_detail)
  74. _dl_dprintf(_dl_debug_file,
  75. "\tpatched: %x ==> %x @ %x\n",
  76. *got_addr, new_addr, got_addr);
  77. }
  78. }
  79. if (!_dl_debug_nofixups)
  80. #endif
  81. *got_addr = new_addr;
  82. return (unsigned int)new_addr;
  83. }
  84. static int
  85. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  86. unsigned long rel_addr, unsigned long rel_size,
  87. int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
  88. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  89. {
  90. unsigned int i;
  91. char *strtab;
  92. ElfW(Sym) *symtab;
  93. ELF_RELOC *rpnt;
  94. int symtab_index;
  95. /* Parse the relocation information. */
  96. rpnt = (ELF_RELOC *)rel_addr;
  97. rel_size /= sizeof(ELF_RELOC);
  98. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  99. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  100. for (i = 0; i < rel_size; i++, rpnt++) {
  101. int res;
  102. symtab_index = ELF_R_SYM(rpnt->r_info);
  103. debug_sym(symtab, strtab, symtab_index);
  104. debug_reloc(symtab, strtab, rpnt);
  105. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  106. if (res == 0)
  107. continue;
  108. _dl_dprintf(2, "\n%s: ", _dl_progname);
  109. if (symtab_index)
  110. _dl_dprintf(2, "symbol '%s': ",
  111. strtab + symtab[symtab_index].st_name);
  112. if (unlikely(res < 0)) {
  113. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  114. _dl_dprintf(2, "can't handle reloc type "
  115. #if defined (__SUPPORT_LD_DEBUG__)
  116. "%s\n", _dl_reltypes(reloc_type));
  117. #else
  118. "%x\n", reloc_type);
  119. #endif
  120. _dl_exit(-res);
  121. } else if (unlikely(res > 0)) {
  122. _dl_dprintf(2, "can't resolve symbol\n");
  123. return res;
  124. }
  125. }
  126. return 0;
  127. }
  128. static int
  129. _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  130. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  131. {
  132. int reloc_type;
  133. int symtab_index;
  134. char *symname;
  135. ElfW(Sym) *sym;
  136. ElfW(Addr) *reloc_addr;
  137. ElfW(Addr) symbol_addr;
  138. #if defined (__SUPPORT_LD_DEBUG__)
  139. ElfW(Addr) old_val;
  140. #endif
  141. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  142. reloc_type = ELF_R_TYPE(rpnt->r_info);
  143. symtab_index = ELF_R_SYM(rpnt->r_info);
  144. sym = &symtab[symtab_index];
  145. symbol_addr = 0;
  146. symname = strtab + sym->st_name;
  147. if (symtab_index) {
  148. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  149. elf_machine_type_class(reloc_type), NULL);
  150. /*
  151. * We want to allow undefined references to weak symbols - this
  152. * might have been intentional. We should not be linking local
  153. * symbols here, so all bases should be covered.
  154. */
  155. if (unlikely(!symbol_addr && ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
  156. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  157. _dl_exit(1);
  158. }
  159. }
  160. #if defined (__SUPPORT_LD_DEBUG__)
  161. old_val = *reloc_addr;
  162. #endif
  163. switch (reloc_type) {
  164. case R_68K_NONE:
  165. break;
  166. case R_68K_8:
  167. *(char *) reloc_addr = symbol_addr + rpnt->r_addend;
  168. break;
  169. case R_68K_16:
  170. *(short *) reloc_addr = symbol_addr + rpnt->r_addend;
  171. break;
  172. case R_68K_32:
  173. *reloc_addr = symbol_addr + rpnt->r_addend;
  174. break;
  175. case R_68K_PC8:
  176. *(char *) reloc_addr = (symbol_addr + rpnt->r_addend
  177. - (unsigned int) reloc_addr);
  178. break;
  179. case R_68K_PC16:
  180. *(short *) reloc_addr = (symbol_addr + rpnt->r_addend
  181. - (unsigned int) reloc_addr);
  182. break;
  183. case R_68K_PC32:
  184. *reloc_addr = (symbol_addr + rpnt->r_addend
  185. - (unsigned int) reloc_addr);
  186. break;
  187. case R_68K_GLOB_DAT:
  188. case R_68K_JMP_SLOT:
  189. *reloc_addr = symbol_addr + rpnt->r_addend;
  190. break;
  191. /* handled by elf_machine_relative()
  192. case R_68K_RELATIVE:
  193. *reloc_addr = ((unsigned int) tpnt->loadaddr
  194. / * Compatibility kludge. * /
  195. + (rpnt->r_addend ? : *reloc_addr));
  196. */
  197. break;
  198. case R_68K_COPY:
  199. if (symbol_addr) {
  200. #if defined (__SUPPORT_LD_DEBUG__)
  201. if (_dl_debug_move)
  202. _dl_dprintf(_dl_debug_file,
  203. "\t%s move %d bytes from %x to %x\n",
  204. symname, sym->st_size,
  205. symbol_addr, reloc_addr);
  206. #endif
  207. _dl_memcpy ((void *) reloc_addr,
  208. (void *) symbol_addr,
  209. sym->st_size);
  210. } else
  211. _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
  212. break;
  213. default:
  214. return -1; /* Calls _dl_exit(1). */
  215. }
  216. #if defined (__SUPPORT_LD_DEBUG__)
  217. if (_dl_debug_reloc && _dl_debug_detail)
  218. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  219. old_val, *reloc_addr, reloc_addr);
  220. #endif
  221. return 0;
  222. }
  223. #undef LAZY_RELOC_WORKS
  224. #ifdef LAZY_RELOC_WORKS
  225. static int
  226. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  227. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  228. {
  229. int reloc_type;
  230. int symtab_index;
  231. ElfW(Addr) *reloc_addr;
  232. #if defined (__SUPPORT_LD_DEBUG__)
  233. ElfW(Addr) old_val;
  234. #endif
  235. (void)scope;
  236. symtab_index = ELF_R_SYM(rpnt->r_info);
  237. (void)strtab;
  238. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  239. reloc_type = ELF_R_TYPE(rpnt->r_info);
  240. #if defined (__SUPPORT_LD_DEBUG__)
  241. old_val = *reloc_addr;
  242. #endif
  243. switch (reloc_type) {
  244. case R_68K_NONE:
  245. break;
  246. case R_68K_JMP_SLOT:
  247. *reloc_addr += (unsigned int) tpnt->loadaddr;
  248. break;
  249. default:
  250. _dl_exit(1);
  251. }
  252. #if defined (__SUPPORT_LD_DEBUG__)
  253. if (_dl_debug_reloc && _dl_debug_detail)
  254. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  255. old_val, *reloc_addr, reloc_addr);
  256. #endif
  257. return 0;
  258. }
  259. #endif
  260. void
  261. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  262. unsigned long rel_addr,
  263. unsigned long rel_size)
  264. {
  265. #ifdef LAZY_RELOC_WORKS
  266. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  267. #else
  268. _dl_parse_relocation_information(rpnt, rel_addr, rel_size);
  269. #endif
  270. }
  271. int
  272. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  273. unsigned long rel_addr,
  274. unsigned long rel_size)
  275. {
  276. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  277. }