elfinterp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* vi: set sw=4 ts=4: */
  2. /* SuperH ELF shared library loader suppport
  3. *
  4. * Copyright (C) 2002, Stefan Allius <allius@atecom.com> and
  5. * Eddie C. Dost <ecd@atecom.com>
  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. #if defined (__SUPPORT_LD_DEBUG__)
  31. static const char *_dl_reltypes_tab[] =
  32. {
  33. [0] "R_SH_NONE", "R_SH_DIR32", "R_SH_REL32", "R_SH_DIR8WPN",
  34. [4] "R_SH_IND12W", "R_SH_DIR8WPL", "R_SH_DIR8WPZ", "R_SH_DIR8BP",
  35. [8] "R_SH_DIR8W", "R_SH_DIR8L",
  36. [25] "R_SH_SWITCH16","R_SH_SWITCH32","R_SH_USES",
  37. [28] "R_SH_COUNT", "R_SH_ALIGN", "R_SH_CODE", "R_SH_DATA",
  38. [32] "R_SH_LABEL", "R_SH_SWITCH8", "R_SH_GNU_VTINHERIT","R_SH_GNU_VTENTRY",
  39. [160] "R_SH_GOT32", "R_SH_PLT32", "R_SH_COPY", "R_SH_GLOB_DAT",
  40. [164] "R_SH_JMP_SLOT","R_SH_RELATIVE","R_SH_GOTOFF", "R_SH_GOTPC",
  41. };
  42. static const char *
  43. _dl_reltypes(int type)
  44. {
  45. static char buf[22];
  46. const char *str;
  47. if (type >= (int)(sizeof (_dl_reltypes_tab)/sizeof(_dl_reltypes_tab[0])) ||
  48. NULL == (str = _dl_reltypes_tab[type]))
  49. {
  50. str =_dl_simple_ltoa( buf, (unsigned long)(type));
  51. }
  52. return str;
  53. }
  54. static
  55. void debug_sym(Elf32_Sym *symtab,char *strtab,int symtab_index)
  56. {
  57. if(_dl_debug_symbols)
  58. {
  59. if(symtab_index){
  60. _dl_dprintf(_dl_debug_file, "\n%s\tvalue=%x\tsize=%x\tinfo=%x\tother=%x\tshndx=%x",
  61. strtab + symtab[symtab_index].st_name,
  62. symtab[symtab_index].st_value,
  63. symtab[symtab_index].st_size,
  64. symtab[symtab_index].st_info,
  65. symtab[symtab_index].st_other,
  66. symtab[symtab_index].st_shndx);
  67. }
  68. }
  69. }
  70. static void debug_reloc(Elf32_Sym *symtab,char *strtab, ELF_RELOC *rpnt)
  71. {
  72. if(_dl_debug_reloc)
  73. {
  74. int symtab_index;
  75. const char *sym;
  76. symtab_index = ELF32_R_SYM(rpnt->r_info);
  77. sym = symtab_index ? strtab + symtab[symtab_index].st_name : "sym=0x0";
  78. if(_dl_debug_symbols)
  79. _dl_dprintf(_dl_debug_file, "\n\t");
  80. else
  81. _dl_dprintf(_dl_debug_file, "\n%s\n\t", sym);
  82. #ifdef ELF_USES_RELOCA
  83. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\taddend=%x",
  84. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  85. rpnt->r_offset,
  86. rpnt->r_addend);
  87. #else
  88. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\n",
  89. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  90. rpnt->r_offset);
  91. #endif
  92. }
  93. }
  94. #endif
  95. /* Program to load an ELF binary on a linux system, and run it.
  96. References to symbols in sharable libraries can be resolved by either
  97. an ELF sharable library or a linux style of shared library. */
  98. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  99. I ever taken any courses on internals. This program was developed using
  100. information available through the book "UNIX SYSTEM V RELEASE 4,
  101. Programmers guide: Ansi C and Programming Support Tools", which did
  102. a more than adequate job of explaining everything required to get this
  103. working. */
  104. extern int _dl_linux_resolve(void);
  105. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  106. {
  107. int reloc_type;
  108. ELF_RELOC *this_reloc;
  109. char *strtab;
  110. Elf32_Sym *symtab;
  111. int symtab_index;
  112. char *rel_addr;
  113. char *new_addr;
  114. char **got_addr;
  115. unsigned long instr_addr;
  116. char *symname;
  117. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  118. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  119. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  120. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  121. symtab = (Elf32_Sym *)(intptr_t) tpnt->dynamic_info[DT_SYMTAB];
  122. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  123. symname = strtab + symtab[symtab_index].st_name;
  124. if (unlikely(reloc_type != R_SH_JMP_SLOT)) {
  125. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  126. _dl_progname);
  127. _dl_exit(1);
  128. }
  129. /* Address of jump instruction to fix up */
  130. instr_addr = ((unsigned long) this_reloc->r_offset +
  131. (unsigned long) tpnt->loadaddr);
  132. got_addr = (char **) instr_addr;
  133. /* Get the address of the GOT entry */
  134. new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  135. if (unlikely(!new_addr)) {
  136. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  137. _dl_exit(1);
  138. }
  139. #if defined (__SUPPORT_LD_DEBUG__)
  140. if ((unsigned long) got_addr < 0x20000000)
  141. {
  142. if (_dl_debug_bindings)
  143. {
  144. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  145. if(_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  146. "\n\tpatched %x ==> %x @ %x\n", *got_addr, new_addr, got_addr);
  147. }
  148. }
  149. if (!_dl_debug_nofixups) {
  150. *got_addr = new_addr;
  151. }
  152. #else
  153. *got_addr = new_addr;
  154. #endif
  155. return (unsigned long) new_addr;
  156. }
  157. static int
  158. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  159. unsigned long rel_addr, unsigned long rel_size,
  160. int (*reloc_fnc) (struct elf_resolve *tpnt, struct dyn_elf *scope,
  161. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  162. {
  163. unsigned int i;
  164. char *strtab;
  165. Elf32_Sym *symtab;
  166. ELF_RELOC *rpnt;
  167. int symtab_index;
  168. /* Now parse the relocation information */
  169. rpnt = (ELF_RELOC *)(intptr_t) rel_addr;
  170. rel_size = rel_size / sizeof(ELF_RELOC);
  171. symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  172. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  173. for (i = 0; i < rel_size; i++, rpnt++) {
  174. int res;
  175. symtab_index = ELF32_R_SYM(rpnt->r_info);
  176. #if defined (__SUPPORT_LD_DEBUG__)
  177. debug_sym(symtab,strtab,symtab_index);
  178. debug_reloc(symtab,strtab,rpnt);
  179. #endif
  180. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  181. if (res==0) continue;
  182. _dl_dprintf(2, "\n%s: ",_dl_progname);
  183. if (symtab_index)
  184. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  185. if (unlikely(res <0))
  186. {
  187. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  188. #if defined (__SUPPORT_LD_DEBUG__)
  189. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  190. #else
  191. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  192. #endif
  193. _dl_exit(-res);
  194. }
  195. if (unlikely(res >0))
  196. {
  197. _dl_dprintf(2, "can't resolve symbol\n");
  198. return res;
  199. }
  200. }
  201. return 0;
  202. }
  203. static int
  204. _dl_do_reloc (struct elf_resolve *tpnt,struct dyn_elf *scope,
  205. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  206. {
  207. int reloc_type;
  208. int symtab_index;
  209. char *symname;
  210. unsigned long *reloc_addr;
  211. unsigned long symbol_addr;
  212. #if defined (__SUPPORT_LD_DEBUG__)
  213. unsigned long old_val;
  214. #endif
  215. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  216. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  217. symtab_index = ELF32_R_SYM(rpnt->r_info);
  218. symbol_addr = 0;
  219. symname = strtab + symtab[symtab_index].st_name;
  220. if (symtab_index) {
  221. symbol_addr = (unsigned long) _dl_find_hash(symname, scope, tpnt,
  222. elf_machine_type_class(reloc_type));
  223. /*
  224. * We want to allow undefined references to weak symbols - this might
  225. * have been intentional. We should not be linking local symbols
  226. * here, so all bases should be covered.
  227. */
  228. if (!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK) {
  229. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  230. _dl_progname, strtab + symtab[symtab_index].st_name);
  231. _dl_exit (1);
  232. }
  233. }
  234. #if defined (__SUPPORT_LD_DEBUG__)
  235. old_val = *reloc_addr;
  236. #endif
  237. switch (reloc_type) {
  238. case R_SH_NONE:
  239. break;
  240. case R_SH_COPY:
  241. if (symbol_addr) {
  242. #if defined (__SUPPORT_LD_DEBUG__)
  243. if(_dl_debug_move)
  244. _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
  245. symname, symtab[symtab_index].st_size,
  246. symbol_addr, reloc_addr);
  247. #endif
  248. _dl_memcpy((char *) reloc_addr, (char *) symbol_addr, symtab[symtab_index].st_size);
  249. }
  250. return 0; /* no further LD_DEBUG messages for copy relocs */
  251. case R_SH_DIR32:
  252. case R_SH_GLOB_DAT:
  253. case R_SH_JMP_SLOT:
  254. *reloc_addr = symbol_addr + rpnt->r_addend;
  255. break;
  256. case R_SH_REL32:
  257. *reloc_addr = symbol_addr + rpnt->r_addend -
  258. (unsigned long) reloc_addr;
  259. break;
  260. case R_SH_RELATIVE:
  261. *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
  262. break;
  263. default:
  264. return -1; /*call _dl_exit(1) */
  265. }
  266. #if defined (__SUPPORT_LD_DEBUG__)
  267. if(_dl_debug_reloc && _dl_debug_detail)
  268. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  269. #endif
  270. return 0;
  271. }
  272. static int
  273. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
  274. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  275. {
  276. int reloc_type;
  277. unsigned long *reloc_addr;
  278. #if defined (__SUPPORT_LD_DEBUG__)
  279. unsigned long old_val;
  280. #endif
  281. (void)scope;
  282. (void)symtab;
  283. (void)strtab;
  284. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  285. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  286. #if defined (__SUPPORT_LD_DEBUG__)
  287. old_val = *reloc_addr;
  288. #endif
  289. switch (reloc_type) {
  290. case R_SH_NONE:
  291. break;
  292. case R_SH_JMP_SLOT:
  293. *reloc_addr += (unsigned long) tpnt->loadaddr;
  294. break;
  295. default:
  296. return -1; /*call _dl_exit(1) */
  297. }
  298. #if defined (__SUPPORT_LD_DEBUG__)
  299. if(_dl_debug_reloc && _dl_debug_detail)
  300. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  301. #endif
  302. return 0;
  303. }
  304. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  305. unsigned long rel_addr, unsigned long rel_size)
  306. {
  307. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  308. }
  309. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  310. unsigned long rel_addr, unsigned long rel_size)
  311. {
  312. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  313. }