elfinterp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #if __FDPIC__
  34. unsigned long _dl_linux_resolver (struct elf_resolve *tpnt, int reloc_offet)
  35. {
  36. ELF_RELOC *this_reloc;
  37. char *strtab;
  38. ElfW(Sym) *symtab;
  39. int symtab_index;
  40. char *rel_addr;
  41. char *new_addr;
  42. struct funcdesc_value funcval;
  43. struct funcdesc_value volatile *got_entry;
  44. char *symname;
  45. struct symbol_ref sym_ref;
  46. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  47. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_offet);
  48. symtab_index = ELF_R_SYM(this_reloc->r_info);
  49. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  50. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  51. sym_ref.sym = &symtab[symtab_index];
  52. sym_ref.tpnt = NULL;
  53. symname= strtab + symtab[symtab_index].st_name;
  54. /* Address of GOT entry fix up */
  55. got_entry = (struct funcdesc_value *) DL_RELOC_ADDR(tpnt->loadaddr, this_reloc->r_offset);
  56. /* Get the address to be used to fill in the GOT entry. */
  57. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, NULL, 0, &sym_ref);
  58. if (!new_addr) {
  59. new_addr = _dl_find_hash(symname, NULL, NULL, 0, &sym_ref);
  60. if (!new_addr) {
  61. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  62. _dl_exit(1);
  63. }
  64. }
  65. funcval.entry_point = new_addr;
  66. funcval.got_value = sym_ref.tpnt->loadaddr.got_value;
  67. #if defined (__SUPPORT_LD_DEBUG__)
  68. if (_dl_debug_bindings) {
  69. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  70. if (_dl_debug_detail)
  71. _dl_dprintf(_dl_debug_file,
  72. "\n\tpatched (%x,%x) ==> (%x,%x) @ %x\n",
  73. got_entry->entry_point, got_entry->got_value,
  74. funcval.entry_point, funcval.got_value,
  75. got_entry);
  76. }
  77. if (1 || !_dl_debug_nofixups) {
  78. *got_entry = funcval;
  79. }
  80. #else
  81. *got_entry = funcval;
  82. #endif
  83. return got_entry;
  84. }
  85. #else
  86. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  87. {
  88. ELF_RELOC *this_reloc;
  89. char *strtab;
  90. char *symname;
  91. ElfW(Sym) *symtab;
  92. ELF_RELOC *rel_addr;
  93. int symtab_index;
  94. unsigned long new_addr;
  95. char **got_addr;
  96. unsigned long instr_addr;
  97. rel_addr = (ELF_RELOC *) tpnt->dynamic_info[DT_JMPREL];
  98. this_reloc = rel_addr + reloc_entry;
  99. symtab_index = ELF_R_SYM(this_reloc->r_info);
  100. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  101. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  102. symname = strtab + symtab[symtab_index].st_name;
  103. /* Address of jump instruction to fix up */
  104. instr_addr = ((unsigned long) this_reloc->r_offset +
  105. (unsigned long) tpnt->loadaddr);
  106. got_addr = (char **) instr_addr;
  107. /* Get the address of the GOT entry */
  108. new_addr = (unsigned long)_dl_find_hash(symname, &_dl_loaded_modules->symbol_scope,
  109. tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  110. if (unlikely(!new_addr)) {
  111. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
  112. _dl_progname, symname);
  113. _dl_exit(1);
  114. }
  115. #if defined (__SUPPORT_LD_DEBUG__)
  116. # if !defined __SUPPORT_LD_DEBUG_EARLY__
  117. if ((unsigned long) got_addr < 0x40000000)
  118. # endif
  119. {
  120. if (_dl_debug_bindings)
  121. {
  122. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  123. if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  124. "\tpatch %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  125. }
  126. }
  127. if (!_dl_debug_nofixups) {
  128. *got_addr = (char *)new_addr;
  129. }
  130. #else
  131. *got_addr = (char *)new_addr;
  132. #endif
  133. return new_addr;
  134. }
  135. #endif
  136. static int
  137. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  138. unsigned long rel_addr, unsigned long rel_size,
  139. int (*reloc_fnc) (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  140. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  141. {
  142. int i;
  143. char *strtab;
  144. int goof = 0;
  145. ElfW(Sym) *symtab;
  146. ELF_RELOC *rpnt;
  147. int symtab_index;
  148. /* Now parse the relocation information */
  149. rpnt = (ELF_RELOC *) rel_addr;
  150. rel_size = rel_size / sizeof(ELF_RELOC);
  151. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  152. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  153. for (i = 0; i < rel_size; i++, rpnt++) {
  154. int res;
  155. symtab_index = ELF_R_SYM(rpnt->r_info);
  156. debug_sym(symtab,strtab,symtab_index);
  157. debug_reloc(symtab,strtab,rpnt);
  158. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  159. if (res==0) continue;
  160. _dl_dprintf(2, "\n%s: ",_dl_progname);
  161. if (symtab_index)
  162. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  163. if (unlikely(res <0))
  164. {
  165. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  166. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  167. _dl_exit(-res);
  168. }
  169. if (unlikely(res >0))
  170. {
  171. _dl_dprintf(2, "can't resolve symbol\n");
  172. goof += res;
  173. }
  174. }
  175. return goof;
  176. }
  177. #if 0
  178. static unsigned long
  179. fix_bad_pc24 (unsigned long *const reloc_addr, unsigned long value)
  180. {
  181. static void *fix_page;
  182. static unsigned int fix_offset;
  183. unsigned int *fix_address;
  184. if (! fix_page)
  185. {
  186. fix_page = _dl_mmap (NULL, PAGE_SIZE , PROT_READ | PROT_WRITE | PROT_EXEC,
  187. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  188. fix_offset = 0;
  189. }
  190. fix_address = (unsigned int *)(fix_page + fix_offset);
  191. fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
  192. fix_address[1] = value;
  193. fix_offset += 8;
  194. if (fix_offset >= PAGE_SIZE)
  195. fix_page = NULL;
  196. return (unsigned long)fix_address;
  197. }
  198. #endif
  199. static int
  200. _dl_do_reloc (struct elf_resolve *tpnt,struct r_scope_elem *scope,
  201. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  202. {
  203. int reloc_type;
  204. int symtab_index;
  205. char *symname;
  206. unsigned long *reloc_addr;
  207. unsigned long symbol_addr;
  208. struct symbol_ref sym_ref;
  209. struct elf_resolve *def_mod = 0;
  210. int goof = 0;
  211. reloc_addr = (unsigned long *) DL_RELOC_ADDR(tpnt->loadaddr, rpnt->r_offset);
  212. reloc_type = ELF_R_TYPE(rpnt->r_info);
  213. symtab_index = ELF_R_SYM(rpnt->r_info);
  214. symbol_addr = 0;
  215. sym_ref.sym = &symtab[symtab_index];
  216. sym_ref.tpnt = NULL;
  217. symname = strtab + symtab[symtab_index].st_name;
  218. if (symtab_index) {
  219. if (ELF_ST_BIND (symtab[symtab_index].st_info) == STB_LOCAL) {
  220. symbol_addr = (unsigned long) DL_RELOC_ADDR(tpnt->loadaddr, symtab[symtab_index].st_value);
  221. def_mod = tpnt;
  222. } else {
  223. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  224. elf_machine_type_class(reloc_type), &sym_ref);
  225. /*
  226. * We want to allow undefined references to weak symbols - this might
  227. * have been intentional. We should not be linking local symbols
  228. * here, so all bases should be covered.
  229. */
  230. if (!symbol_addr && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
  231. && (ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
  232. /* This may be non-fatal if called from dlopen. */
  233. return 1;
  234. }
  235. if (_dl_trace_prelink) {
  236. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  237. &sym_ref, elf_machine_type_class(reloc_type));
  238. }
  239. def_mod = sym_ref.tpnt;
  240. }
  241. } else {
  242. /*
  243. * Relocs against STN_UNDEF are usually treated as using a
  244. * symbol value of zero, and using the module containing the
  245. * reloc itself.
  246. */
  247. symbol_addr = symtab[symtab_index].st_value;
  248. def_mod = tpnt;
  249. }
  250. #if defined (__SUPPORT_LD_DEBUG__)
  251. {
  252. unsigned long old_val;
  253. if (reloc_type != R_ARM_NONE)
  254. old_val = *reloc_addr;
  255. #endif
  256. switch (reloc_type) {
  257. case R_ARM_NONE:
  258. break;
  259. case R_ARM_ABS32:
  260. *reloc_addr += symbol_addr;
  261. break;
  262. case R_ARM_PC24:
  263. #if 0
  264. {
  265. unsigned long addend;
  266. long newvalue, topbits;
  267. addend = *reloc_addr & 0x00ffffff;
  268. if (addend & 0x00800000) addend |= 0xff000000;
  269. newvalue = symbol_addr - (unsigned long)reloc_addr + (addend << 2);
  270. topbits = newvalue & 0xfe000000;
  271. if (topbits != 0xfe000000 && topbits != 0x00000000)
  272. {
  273. newvalue = fix_bad_pc24(reloc_addr, symbol_addr)
  274. - (unsigned long)reloc_addr + (addend << 2);
  275. topbits = newvalue & 0xfe000000;
  276. if (unlikely(topbits != 0xfe000000 && topbits != 0x00000000))
  277. {
  278. _dl_dprintf(2,"symbol '%s': R_ARM_PC24 relocation out of range.",
  279. symtab[symtab_index].st_name);
  280. _dl_exit(1);
  281. }
  282. }
  283. newvalue >>= 2;
  284. symbol_addr = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
  285. *reloc_addr = symbol_addr;
  286. break;
  287. }
  288. #else
  289. _dl_dprintf(2,"R_ARM_PC24: Compile shared libraries with -fPIC!\n");
  290. _dl_exit(1);
  291. #endif
  292. case R_ARM_GLOB_DAT:
  293. case R_ARM_JUMP_SLOT:
  294. *reloc_addr = symbol_addr;
  295. break;
  296. case R_ARM_RELATIVE:
  297. *reloc_addr = DL_RELOC_ADDR(tpnt->loadaddr, *reloc_addr);
  298. break;
  299. case R_ARM_COPY:
  300. _dl_memcpy((void *) reloc_addr,
  301. (void *) symbol_addr, symtab[symtab_index].st_size);
  302. break;
  303. #ifdef __FDPIC__
  304. case R_ARM_FUNCDESC_VALUE:
  305. {
  306. struct funcdesc_value funcval;
  307. struct funcdesc_value *dst = (struct funcdesc_value *) reloc_addr;
  308. funcval.entry_point = (void*)symbol_addr;
  309. /* Add offset to section address for local symbols. */
  310. if (ELF_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL)
  311. funcval.entry_point += *reloc_addr;
  312. funcval.got_value = def_mod->loadaddr.got_value;
  313. *dst = funcval;
  314. }
  315. break;
  316. case R_ARM_FUNCDESC:
  317. {
  318. unsigned long reloc_value = *reloc_addr;
  319. if (symbol_addr)
  320. reloc_value = (unsigned long) _dl_funcdesc_for(symbol_addr + reloc_value, sym_ref.tpnt->loadaddr.got_value);
  321. else
  322. /* Relocation against an
  323. undefined weak symbol:
  324. set funcdesc to zero. */
  325. reloc_value = 0;
  326. *reloc_addr = reloc_value;
  327. }
  328. break;
  329. #endif
  330. #if defined USE_TLS && USE_TLS
  331. case R_ARM_TLS_DTPMOD32:
  332. *reloc_addr = def_mod->l_tls_modid;
  333. break;
  334. case R_ARM_TLS_DTPOFF32:
  335. *reloc_addr += symbol_addr;
  336. break;
  337. case R_ARM_TLS_TPOFF32:
  338. CHECK_STATIC_TLS ((struct link_map *) def_mod);
  339. *reloc_addr += (symbol_addr + def_mod->l_tls_offset);
  340. break;
  341. #endif
  342. default:
  343. return -1; /*call _dl_exit(1) */
  344. }
  345. #if defined (__SUPPORT_LD_DEBUG__)
  346. if (_dl_debug_reloc && _dl_debug_detail && reloc_type != R_ARM_NONE)
  347. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  348. }
  349. #endif
  350. return goof;
  351. }
  352. static int
  353. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  354. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  355. {
  356. int reloc_type;
  357. unsigned long *reloc_addr;
  358. reloc_addr = (unsigned long *) DL_RELOC_ADDR(tpnt->loadaddr, rpnt->r_offset);
  359. reloc_type = ELF_R_TYPE(rpnt->r_info);
  360. #if defined (__SUPPORT_LD_DEBUG__)
  361. {
  362. unsigned long old_val;
  363. if (reloc_type != R_ARM_NONE)
  364. old_val = *reloc_addr;
  365. #endif
  366. switch (reloc_type) {
  367. case R_ARM_NONE:
  368. break;
  369. case R_ARM_JUMP_SLOT:
  370. *reloc_addr = DL_RELOC_ADDR(tpnt->loadaddr, *reloc_addr);
  371. break;
  372. #ifdef __FDPIC__
  373. case R_ARM_FUNCDESC_VALUE:
  374. {
  375. struct funcdesc_value *dst = (struct funcdesc_value *) reloc_addr;
  376. dst->entry_point = DL_RELOC_ADDR(tpnt->loadaddr, dst->entry_point);
  377. dst->got_value = tpnt->loadaddr.got_value;
  378. }
  379. break;
  380. #endif
  381. default:
  382. return -1; /*call _dl_exit(1) */
  383. }
  384. #if defined (__SUPPORT_LD_DEBUG__)
  385. if (_dl_debug_reloc && _dl_debug_detail && reloc_type != R_ARM_NONE)
  386. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  387. }
  388. #endif
  389. return 0;
  390. }
  391. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  392. unsigned long rel_addr, unsigned long rel_size)
  393. {
  394. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  395. }
  396. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  397. struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
  398. {
  399. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  400. }
  401. #ifndef IS_IN_libdl
  402. # include "../../libc/sysdeps/linux/arm/crtreloc.c"
  403. #endif