elfinterp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* ARM ELF shared library loader suppport
  2. *
  3. * Copyright (C) 2001-2004 Erik Andersen
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. The name of the above contributors may not be
  13. * used to endorse or promote products derived from this software
  14. * without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /* Program to load an ELF binary on a linux system, and run it.
  29. References to symbols in sharable libraries can be resolved by either
  30. an ELF sharable library or a linux style of shared library. */
  31. #include "ldso.h"
  32. extern int _dl_linux_resolve(void);
  33. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  34. {
  35. #if __FDPIC__
  36. /* FIXME: implement. */
  37. while(1) ;
  38. return 0;
  39. #else
  40. ELF_RELOC *this_reloc;
  41. char *strtab;
  42. char *symname;
  43. ElfW(Sym) *symtab;
  44. ELF_RELOC *rel_addr;
  45. int symtab_index;
  46. unsigned long new_addr;
  47. char **got_addr;
  48. unsigned long instr_addr;
  49. rel_addr = (ELF_RELOC *) tpnt->dynamic_info[DT_JMPREL];
  50. this_reloc = rel_addr + reloc_entry;
  51. symtab_index = ELF_R_SYM(this_reloc->r_info);
  52. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  53. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  54. symname = strtab + symtab[symtab_index].st_name;
  55. /* Address of jump instruction to fix up */
  56. instr_addr = ((unsigned long) this_reloc->r_offset +
  57. (unsigned long) tpnt->loadaddr);
  58. got_addr = (char **) instr_addr;
  59. /* Get the address of the GOT entry */
  60. new_addr = (unsigned long)_dl_find_hash(symname, &_dl_loaded_modules->symbol_scope,
  61. tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  62. if (unlikely(!new_addr)) {
  63. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
  64. _dl_progname, symname);
  65. _dl_exit(1);
  66. }
  67. #if defined (__SUPPORT_LD_DEBUG__)
  68. # if !defined __SUPPORT_LD_DEBUG_EARLY__
  69. if ((unsigned long) got_addr < 0x40000000)
  70. # endif
  71. {
  72. if (_dl_debug_bindings)
  73. {
  74. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  75. if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  76. "\tpatch %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  77. }
  78. }
  79. if (!_dl_debug_nofixups) {
  80. *got_addr = (char *)new_addr;
  81. }
  82. #else
  83. *got_addr = (char *)new_addr;
  84. #endif
  85. return new_addr;
  86. #endif
  87. }
  88. static int
  89. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  90. unsigned long rel_addr, unsigned long rel_size,
  91. int (*reloc_fnc) (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  92. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  93. {
  94. int i;
  95. char *strtab;
  96. int goof = 0;
  97. ElfW(Sym) *symtab;
  98. ELF_RELOC *rpnt;
  99. int symtab_index;
  100. /* Now parse the relocation information */
  101. rpnt = (ELF_RELOC *) rel_addr;
  102. rel_size = rel_size / sizeof(ELF_RELOC);
  103. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  104. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  105. for (i = 0; i < rel_size; i++, rpnt++) {
  106. int res;
  107. symtab_index = ELF_R_SYM(rpnt->r_info);
  108. debug_sym(symtab,strtab,symtab_index);
  109. debug_reloc(symtab,strtab,rpnt);
  110. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  111. if (res==0) continue;
  112. _dl_dprintf(2, "\n%s: ",_dl_progname);
  113. if (symtab_index)
  114. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  115. if (unlikely(res <0))
  116. {
  117. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  118. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  119. _dl_exit(-res);
  120. }
  121. if (unlikely(res >0))
  122. {
  123. _dl_dprintf(2, "can't resolve symbol\n");
  124. goof += res;
  125. }
  126. }
  127. return goof;
  128. }
  129. #if 0
  130. static unsigned long
  131. fix_bad_pc24 (unsigned long *const reloc_addr, unsigned long value)
  132. {
  133. static void *fix_page;
  134. static unsigned int fix_offset;
  135. unsigned int *fix_address;
  136. if (! fix_page)
  137. {
  138. fix_page = _dl_mmap (NULL, PAGE_SIZE , PROT_READ | PROT_WRITE | PROT_EXEC,
  139. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  140. fix_offset = 0;
  141. }
  142. fix_address = (unsigned int *)(fix_page + fix_offset);
  143. fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
  144. fix_address[1] = value;
  145. fix_offset += 8;
  146. if (fix_offset >= PAGE_SIZE)
  147. fix_page = NULL;
  148. return (unsigned long)fix_address;
  149. }
  150. #endif
  151. static int
  152. _dl_do_reloc (struct elf_resolve *tpnt,struct r_scope_elem *scope,
  153. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  154. {
  155. int reloc_type;
  156. int symtab_index;
  157. char *symname;
  158. unsigned long *reloc_addr;
  159. unsigned long symbol_addr;
  160. struct symbol_ref sym_ref;
  161. struct elf_resolve *def_mod = 0;
  162. int goof = 0;
  163. reloc_addr = (unsigned long *) DL_RELOC_ADDR(tpnt->loadaddr, rpnt->r_offset);
  164. reloc_type = ELF_R_TYPE(rpnt->r_info);
  165. symtab_index = ELF_R_SYM(rpnt->r_info);
  166. symbol_addr = 0;
  167. sym_ref.sym = &symtab[symtab_index];
  168. sym_ref.tpnt = NULL;
  169. symname = strtab + symtab[symtab_index].st_name;
  170. if (symtab_index) {
  171. if (ELF_ST_BIND (symtab[symtab_index].st_info) == STB_LOCAL) {
  172. symbol_addr = (unsigned long) DL_RELOC_ADDR(tpnt->loadaddr, symtab[symtab_index].st_value);
  173. def_mod = tpnt;
  174. } else {
  175. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  176. elf_machine_type_class(reloc_type), &sym_ref);
  177. /*
  178. * We want to allow undefined references to weak symbols - this might
  179. * have been intentional. We should not be linking local symbols
  180. * here, so all bases should be covered.
  181. */
  182. if (!symbol_addr && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
  183. && (ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
  184. /* This may be non-fatal if called from dlopen. */
  185. return 1;
  186. }
  187. if (_dl_trace_prelink) {
  188. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  189. &sym_ref, elf_machine_type_class(reloc_type));
  190. }
  191. def_mod = sym_ref.tpnt;
  192. }
  193. } else {
  194. /*
  195. * Relocs against STN_UNDEF are usually treated as using a
  196. * symbol value of zero, and using the module containing the
  197. * reloc itself.
  198. */
  199. symbol_addr = symtab[symtab_index].st_value;
  200. def_mod = tpnt;
  201. }
  202. #if defined (__SUPPORT_LD_DEBUG__)
  203. {
  204. unsigned long old_val = *reloc_addr;
  205. #endif
  206. switch (reloc_type) {
  207. case R_ARM_NONE:
  208. break;
  209. case R_ARM_ABS32:
  210. *reloc_addr += symbol_addr;
  211. break;
  212. case R_ARM_PC24:
  213. #if 0
  214. {
  215. unsigned long addend;
  216. long newvalue, topbits;
  217. addend = *reloc_addr & 0x00ffffff;
  218. if (addend & 0x00800000) addend |= 0xff000000;
  219. newvalue = symbol_addr - (unsigned long)reloc_addr + (addend << 2);
  220. topbits = newvalue & 0xfe000000;
  221. if (topbits != 0xfe000000 && topbits != 0x00000000)
  222. {
  223. newvalue = fix_bad_pc24(reloc_addr, symbol_addr)
  224. - (unsigned long)reloc_addr + (addend << 2);
  225. topbits = newvalue & 0xfe000000;
  226. if (unlikely(topbits != 0xfe000000 && topbits != 0x00000000))
  227. {
  228. _dl_dprintf(2,"symbol '%s': R_ARM_PC24 relocation out of range.",
  229. symtab[symtab_index].st_name);
  230. _dl_exit(1);
  231. }
  232. }
  233. newvalue >>= 2;
  234. symbol_addr = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
  235. *reloc_addr = symbol_addr;
  236. break;
  237. }
  238. #else
  239. _dl_dprintf(2,"R_ARM_PC24: Compile shared libraries with -fPIC!\n");
  240. _dl_exit(1);
  241. #endif
  242. case R_ARM_GLOB_DAT:
  243. case R_ARM_JUMP_SLOT:
  244. *reloc_addr = symbol_addr;
  245. break;
  246. case R_ARM_RELATIVE:
  247. *reloc_addr = DL_RELOC_ADDR(tpnt->loadaddr, *reloc_addr);
  248. break;
  249. case R_ARM_COPY:
  250. _dl_memcpy((void *) reloc_addr,
  251. (void *) symbol_addr, symtab[symtab_index].st_size);
  252. break;
  253. #ifdef __FDPIC__
  254. case R_ARM_FUNCDESC_VALUE:
  255. {
  256. struct funcdesc_value funcval;
  257. struct funcdesc_value *dst = (struct funcdesc_value *) reloc_addr;
  258. funcval.entry_point = (void*)symbol_addr;
  259. /* Add offset to section address for local symbols. */
  260. if (ELF_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL)
  261. funcval.entry_point += *reloc_addr;
  262. funcval.got_value = def_mod->loadaddr.got_value;
  263. *dst = funcval;
  264. }
  265. break;
  266. case R_ARM_FUNCDESC:
  267. {
  268. unsigned long reloc_value = *reloc_addr;
  269. if (symbol_addr)
  270. reloc_value = (unsigned long) _dl_funcdesc_for(symbol_addr + reloc_value, sym_ref.tpnt->loadaddr.got_value);
  271. else
  272. /* Relocation against an
  273. undefined weak symbol:
  274. set funcdesc to zero. */
  275. reloc_value = 0;
  276. *reloc_addr = reloc_value;
  277. }
  278. break;
  279. #endif
  280. #if defined USE_TLS && USE_TLS
  281. case R_ARM_TLS_DTPMOD32:
  282. *reloc_addr = def_mod->l_tls_modid;
  283. break;
  284. case R_ARM_TLS_DTPOFF32:
  285. *reloc_addr += symbol_addr;
  286. break;
  287. case R_ARM_TLS_TPOFF32:
  288. CHECK_STATIC_TLS ((struct link_map *) def_mod);
  289. *reloc_addr += (symbol_addr + def_mod->l_tls_offset);
  290. break;
  291. #endif
  292. default:
  293. return -1; /*call _dl_exit(1) */
  294. }
  295. #if defined (__SUPPORT_LD_DEBUG__)
  296. if (_dl_debug_reloc && _dl_debug_detail)
  297. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  298. }
  299. #endif
  300. return goof;
  301. }
  302. static int
  303. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  304. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  305. {
  306. int reloc_type;
  307. unsigned long *reloc_addr;
  308. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  309. reloc_type = ELF_R_TYPE(rpnt->r_info);
  310. #if defined (__SUPPORT_LD_DEBUG__)
  311. {
  312. unsigned long old_val = *reloc_addr;
  313. #endif
  314. switch (reloc_type) {
  315. case R_ARM_NONE:
  316. break;
  317. case R_ARM_JUMP_SLOT:
  318. *reloc_addr += (unsigned long) tpnt->loadaddr;
  319. break;
  320. default:
  321. return -1; /*call _dl_exit(1) */
  322. }
  323. #if defined (__SUPPORT_LD_DEBUG__)
  324. if (_dl_debug_reloc && _dl_debug_detail)
  325. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  326. }
  327. #endif
  328. return 0;
  329. }
  330. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  331. unsigned long rel_addr, unsigned long rel_size)
  332. {
  333. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  334. }
  335. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  336. struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
  337. {
  338. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  339. }
  340. #ifndef IS_IN_libdl
  341. # include "../../libc/sysdeps/linux/arm/crtreloc.c"
  342. #endif