elfinterp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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) || defined (LD_DEBUG_SYMBOLS)
  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 >= (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. #ifdef ELF_USES_RELOCA
  79. _dl_dprintf(_dl_debug_file, "\n%s\toffset=%x\taddend=%x %s",
  80. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  81. rpnt->r_offset,
  82. rpnt->r_addend,
  83. sym);
  84. #else
  85. _dl_dprintf(_dl_debug_file, "\n%s\toffset=%x %s",
  86. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  87. rpnt->r_offset,
  88. sym);
  89. #endif
  90. }
  91. }
  92. #endif
  93. /* Program to load an ELF binary on a linux system, and run it.
  94. References to symbols in sharable libraries can be resolved by either
  95. an ELF sharable library or a linux style of shared library. */
  96. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  97. I ever taken any courses on internals. This program was developed using
  98. information available through the book "UNIX SYSTEM V RELEASE 4,
  99. Programmers guide: Ansi C and Programming Support Tools", which did
  100. a more than adequate job of explaining everything required to get this
  101. working. */
  102. extern int _dl_linux_resolve(void);
  103. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  104. {
  105. int reloc_type;
  106. ELF_RELOC *this_reloc;
  107. char *strtab;
  108. Elf32_Sym *symtab;
  109. int symtab_index;
  110. char *rel_addr;
  111. char *new_addr;
  112. char **got_addr;
  113. unsigned long instr_addr;
  114. rel_addr = (char *) (tpnt->dynamic_info[DT_JMPREL] + tpnt->loadaddr);
  115. this_reloc = (ELF_RELOC *) (rel_addr + reloc_entry);
  116. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  117. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  118. symtab = (Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  119. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  120. if (reloc_type != R_SH_JMP_SLOT) {
  121. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  122. _dl_progname);
  123. _dl_exit(1);
  124. }
  125. /* Address of jump instruction to fix up */
  126. instr_addr = ((unsigned long) this_reloc->r_offset +
  127. (unsigned long) tpnt->loadaddr);
  128. got_addr = (char **) instr_addr;
  129. /* Get the address of the GOT entry */
  130. new_addr = _dl_find_hash(strtab + symtab[symtab_index].st_name,
  131. tpnt->symbol_scope, tpnt, resolver);
  132. if (!new_addr) {
  133. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
  134. _dl_progname, strtab + symtab[symtab_index].st_name);
  135. _dl_exit(1);
  136. }
  137. #if defined (SUPPORT_LD_DEBUG) || defined (LD_NEVER_FIXUP_SYMBOLS)
  138. if ((unsigned long) got_addr < 0x20000000)
  139. {
  140. #ifndef SUPPORT_LD_DEBUG
  141. if (_dl_debug_bindings)
  142. {
  143. _dl_dprintf(_dl_debug_file, "\nresolve function: %s",
  144. strtab + symtab[symtab_index].st_name);
  145. if(_dl_debug_detail) _dl_dprintf(_dl_debug_file, "\tpatch %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  146. }
  147. #endif
  148. #ifndef LD_NEVER_FIXUP_SYMBOLS
  149. *got_addr = new_addr;
  150. #endif
  151. } else {
  152. *got_addr = new_addr;
  153. }
  154. #else
  155. *got_addr = new_addr;
  156. #endif
  157. return (unsigned long) new_addr;
  158. }
  159. static int
  160. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  161. unsigned long rel_addr, unsigned long rel_size,
  162. int (*reloc_fnc) (struct elf_resolve *tpnt, struct dyn_elf *scope,
  163. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  164. {
  165. int i;
  166. char *strtab;
  167. int goof = 0;
  168. Elf32_Sym *symtab;
  169. ELF_RELOC *rpnt;
  170. int symtab_index;
  171. /* Now parse the relocation information */
  172. rpnt = (ELF_RELOC *) (rel_addr + tpnt->loadaddr);
  173. rel_size = rel_size / sizeof(ELF_RELOC);
  174. symtab = (Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  175. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  176. for (i = 0; i < rel_size; i++, rpnt++) {
  177. int res;
  178. symtab_index = ELF32_R_SYM(rpnt->r_info);
  179. /* When the dynamic linker bootstrapped itself, it resolved some symbols.
  180. Make sure we do not do them again */
  181. if (!symtab_index && tpnt->libtype == program_interpreter)
  182. continue;
  183. if (symtab_index && tpnt->libtype == program_interpreter &&
  184. _dl_symbol(strtab + symtab[symtab_index].st_name))
  185. continue;
  186. #if defined (SUPPORT_LD_DEBUG)
  187. debug_sym(symtab,strtab,symtab_index);
  188. debug_reloc(symtab,strtab,rpnt);
  189. #endif
  190. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  191. if (res==0) continue;
  192. _dl_dprintf(2, "\n%s: ",_dl_progname);
  193. if (symtab_index)
  194. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  195. if (res <0)
  196. {
  197. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  198. #if defined (SUPPORT_LD_DEBUG)
  199. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  200. #else
  201. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  202. #endif
  203. _dl_exit(-res);
  204. }
  205. else if (res >0)
  206. {
  207. _dl_dprintf(2, "can't resolve symbol '%s'\n");
  208. goof += res;
  209. }
  210. }
  211. return goof;
  212. }
  213. static int
  214. _dl_do_reloc (struct elf_resolve *tpnt,struct dyn_elf *scope,
  215. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  216. {
  217. int reloc_type;
  218. int symtab_index;
  219. unsigned long *reloc_addr;
  220. unsigned long symbol_addr;
  221. int goof = 0;
  222. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  223. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  224. symtab_index = ELF32_R_SYM(rpnt->r_info);
  225. symbol_addr = 0;
  226. if (symtab_index) {
  227. symbol_addr = (unsigned long) _dl_find_hash(strtab + symtab[symtab_index].st_name,
  228. scope,
  229. (reloc_type == R_SH_JMP_SLOT ? tpnt : NULL), symbolrel);
  230. /*
  231. * We want to allow undefined references to weak symbols - this might
  232. * have been intentional. We should not be linking local symbols
  233. * here, so all bases should be covered.
  234. */
  235. if (!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_GLOBAL) {
  236. goof++;
  237. }
  238. }
  239. #if defined (SUPPORT_LD_DEBUG)
  240. {
  241. unsigned long old_val = *reloc_addr;
  242. #endif
  243. switch (reloc_type) {
  244. case R_SH_NONE:
  245. break;
  246. case R_SH_COPY:
  247. /* handled later on */
  248. break;
  249. case R_SH_DIR32:
  250. *reloc_addr += symbol_addr + rpnt->r_addend;
  251. break;
  252. case R_SH_JMP_SLOT:
  253. *reloc_addr = symbol_addr + rpnt->r_addend;
  254. break;
  255. case R_SH_REL32:
  256. *reloc_addr += rpnt->r_addend -
  257. (unsigned long) tpnt->loadaddr;
  258. break;
  259. case R_SH_RELATIVE:
  260. *reloc_addr += (unsigned long) tpnt->loadaddr;
  261. break;
  262. default:
  263. return -1; /*call _dl_exit(1) */
  264. }
  265. #if defined (SUPPORT_LD_DEBUG)
  266. if(_dl_debug_reloc && _dl_debug_detail)
  267. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  268. }
  269. #endif
  270. return goof;
  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. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  279. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  280. #if defined (SUPPORT_LD_DEBUG)
  281. {
  282. unsigned long old_val = *reloc_addr;
  283. #endif
  284. switch (reloc_type) {
  285. case R_SH_NONE:
  286. break;
  287. case R_SH_JMP_SLOT:
  288. *reloc_addr += (unsigned long) tpnt->loadaddr;
  289. break;
  290. default:
  291. return -1; /*call _dl_exit(1) */
  292. }
  293. #if defined (SUPPORT_LD_DEBUG)
  294. if(_dl_debug_reloc && _dl_debug_detail)
  295. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  296. }
  297. #endif
  298. return 0;
  299. }
  300. /* This is done as a separate step, because there are cases where
  301. information is first copied and later initialized. This results in
  302. the wrong information being copied. Someone at Sun was complaining about
  303. a bug in the handling of _COPY by SVr4, and this may in fact be what he
  304. was talking about. Sigh. */
  305. /* No, there are cases where the SVr4 linker fails to emit COPY relocs
  306. at all */
  307. static int
  308. _dl_do_copy (struct elf_resolve *tpnt, struct dyn_elf *scope,
  309. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  310. {
  311. int reloc_type;
  312. int symtab_index;
  313. unsigned long *reloc_addr;
  314. unsigned long symbol_addr;
  315. int goof = 0;
  316. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  317. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  318. if (reloc_type != R_SH_COPY)
  319. return 0;
  320. symtab_index = ELF32_R_SYM(rpnt->r_info);
  321. symbol_addr = 0;
  322. if (symtab_index) {
  323. symbol_addr = (unsigned long) _dl_find_hash(strtab +
  324. symtab[symtab_index].st_name, scope,
  325. NULL, copyrel);
  326. if (!symbol_addr) goof++;
  327. }
  328. if (!goof) {
  329. #if defined (SUPPORT_LD_DEBUG)
  330. if(_dl_debug_move)
  331. _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
  332. strtab + symtab[symtab_index].st_name,
  333. symtab[symtab_index].st_size,
  334. symbol_addr, symtab[symtab_index].st_value);
  335. #endif
  336. _dl_memcpy((char *) symtab[symtab_index].st_value,
  337. (char *) symbol_addr, symtab[symtab_index].st_size);
  338. }
  339. return goof;
  340. }
  341. void _dl_parse_lazy_relocation_information(struct elf_resolve *tpnt,
  342. unsigned long rel_addr, unsigned long rel_size, int type)
  343. {
  344. (void)_dl_parse(tpnt, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  345. }
  346. int _dl_parse_relocation_information(struct elf_resolve *tpnt,
  347. unsigned long rel_addr, unsigned long rel_size, int type)
  348. {
  349. return _dl_parse(tpnt, tpnt->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  350. }
  351. int _dl_parse_copy_information(struct dyn_elf *xpnt, unsigned long rel_addr,
  352. unsigned long rel_size, int type)
  353. {
  354. return _dl_parse(xpnt->dyn, xpnt->next, rel_addr, rel_size, _dl_do_copy);
  355. }