elfinterp.c 9.2 KB

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