elfinterp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* vi: set sw=4 ts=4: */
  2. /* ARM ELF shared library loader suppport
  3. *
  4. * Copyright (C) 2001-2002, Erik Andersen
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. The name of the above contributors may not be
  14. * used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #if defined (__SUPPORT_LD_DEBUG__)
  30. static const char *_dl_reltypes_tab[] =
  31. {
  32. [0] "R_ARM_NONE", "R_ARM_PC24", "R_ARM_ABS32", "R_ARM_REL32",
  33. [4] "R_ARM_PC13", "R_ARM_ABS16", "R_ARM_ABS12", "R_ARM_THM_ABS5",
  34. [8] "R_ARM_ABS8", "R_ARM_SBREL32","R_ARM_THM_PC22", "R_ARM_THM_PC8",
  35. [12] "R_ARM_AMP_VCALL9", "R_ARM_SWI24", "R_ARM_THM_SWI8", "R_ARM_XPC25",
  36. [16] "R_ARM_THM_XPC22",
  37. [20] "R_ARM_COPY", "R_ARM_GLOB_DAT","R_ARM_JUMP_SLOT", "R_ARM_RELATIVE",
  38. [24] "R_ARM_GOTOFF", "R_ARM_GOTPC", "R_ARM_GOT32", "R_ARM_PLT32",
  39. [32] "R_ARM_ALU_PCREL_7_0","R_ARM_ALU_PCREL_15_8","R_ARM_ALU_PCREL_23_15","R_ARM_LDR_SBREL_11_0",
  40. [36] "R_ARM_ALU_SBREL_19_12","R_ARM_ALU_SBREL_27_20",
  41. [100] "R_ARM_GNU_VTENTRY","R_ARM_GNU_VTINHERIT","R_ARM_THM_PC11","R_ARM_THM_PC9",
  42. [249] "R_ARM_RXPC25", "R_ARM_RSBREL32", "R_ARM_THM_RPC22", "R_ARM_RREL32",
  43. [253] "R_ARM_RABS22", "R_ARM_RPC24", "R_ARM_RBASE",
  44. };
  45. static const char *
  46. _dl_reltypes(int type)
  47. {
  48. static char buf[22];
  49. const char *str;
  50. if (type >= (sizeof (_dl_reltypes_tab)/sizeof(_dl_reltypes_tab[0])) ||
  51. NULL == (str = _dl_reltypes_tab[type]))
  52. {
  53. str =_dl_simple_ltoa( buf, (unsigned long)(type));
  54. }
  55. return str;
  56. }
  57. static
  58. void debug_sym(Elf32_Sym *symtab,char *strtab,int symtab_index)
  59. {
  60. if(_dl_debug_symbols)
  61. {
  62. if(symtab_index){
  63. _dl_dprintf(_dl_debug_file, "\n%s\tvalue=%x\tsize=%x\tinfo=%x\tother=%x\tshndx=%x",
  64. strtab + symtab[symtab_index].st_name,
  65. symtab[symtab_index].st_value,
  66. symtab[symtab_index].st_size,
  67. symtab[symtab_index].st_info,
  68. symtab[symtab_index].st_other,
  69. symtab[symtab_index].st_shndx);
  70. }
  71. }
  72. }
  73. static void debug_reloc(Elf32_Sym *symtab,char *strtab, ELF_RELOC *rpnt)
  74. {
  75. if(_dl_debug_reloc)
  76. {
  77. int symtab_index;
  78. const char *sym;
  79. symtab_index = ELF32_R_SYM(rpnt->r_info);
  80. sym = symtab_index ? strtab + symtab[symtab_index].st_name : "sym=0x0";
  81. #ifdef ELF_USES_RELOCA
  82. _dl_dprintf(_dl_debug_file, "\n%s\toffset=%x\taddend=%x %s",
  83. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  84. rpnt->r_offset,
  85. rpnt->r_addend,
  86. sym);
  87. #else
  88. _dl_dprintf(_dl_debug_file, "\n%s\toffset=%x %s",
  89. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  90. rpnt->r_offset,
  91. sym);
  92. #endif
  93. }
  94. }
  95. #endif
  96. /* Program to load an ELF binary on a linux system, and run it.
  97. References to symbols in sharable libraries can be resolved by either
  98. an ELF sharable library or a linux style of shared library. */
  99. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  100. I ever taken any courses on internals. This program was developed using
  101. information available through the book "UNIX SYSTEM V RELEASE 4,
  102. Programmers guide: Ansi C and Programming Support Tools", which did
  103. a more than adequate job of explaining everything required to get this
  104. working. */
  105. extern int _dl_linux_resolve(void);
  106. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  107. {
  108. int reloc_type;
  109. ELF_RELOC *this_reloc;
  110. char *strtab;
  111. Elf32_Sym *symtab;
  112. ELF_RELOC *rel_addr;
  113. int symtab_index;
  114. char *new_addr;
  115. char **got_addr;
  116. unsigned long instr_addr;
  117. rel_addr = (ELF_RELOC *) (tpnt->dynamic_info[DT_JMPREL] + tpnt->loadaddr);
  118. this_reloc = rel_addr + (reloc_entry >> 3);
  119. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  120. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  121. symtab = (Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  122. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  123. if (reloc_type != R_ARM_JUMP_SLOT) {
  124. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  125. _dl_progname);
  126. _dl_exit(1);
  127. };
  128. /* Address of jump instruction to fix up */
  129. instr_addr = ((unsigned long) this_reloc->r_offset +
  130. (unsigned long) tpnt->loadaddr);
  131. got_addr = (char **) instr_addr;
  132. /* Get the address of the GOT entry */
  133. new_addr = _dl_find_hash(strtab + symtab[symtab_index].st_name,
  134. tpnt->symbol_scope, tpnt, resolver);
  135. if (!new_addr) {
  136. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
  137. _dl_progname, strtab + symtab[symtab_index].st_name);
  138. _dl_exit(1);
  139. };
  140. #if defined (__SUPPORT_LD_DEBUG__)
  141. if ((unsigned long) got_addr < 0x40000000)
  142. {
  143. if (_dl_debug_bindings)
  144. {
  145. _dl_dprintf(_dl_debug_file, "\nresolve function: %s",
  146. strtab + symtab[symtab_index].st_name);
  147. if(_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  148. "\tpatch %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  149. }
  150. }
  151. if (!_dl_debug_nofixups) {
  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\n");
  208. goof += res;
  209. }
  210. }
  211. return goof;
  212. }
  213. static unsigned long
  214. fix_bad_pc24 (unsigned long *const reloc_addr, unsigned long value)
  215. {
  216. static void *fix_page;
  217. static unsigned int fix_offset;
  218. unsigned int *fix_address;
  219. if (! fix_page)
  220. {
  221. fix_page = _dl_mmap (NULL, 4096 , PROT_READ | PROT_WRITE | PROT_EXEC,
  222. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  223. fix_offset = 0;
  224. }
  225. fix_address = (unsigned int *)(fix_page + fix_offset);
  226. fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
  227. fix_address[1] = value;
  228. fix_offset += 8;
  229. if (fix_offset >= 4096)
  230. fix_page = NULL;
  231. return (unsigned long)fix_address;
  232. }
  233. static int
  234. _dl_do_reloc (struct elf_resolve *tpnt,struct dyn_elf *scope,
  235. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  236. {
  237. int reloc_type;
  238. int symtab_index;
  239. unsigned long *reloc_addr;
  240. unsigned long symbol_addr;
  241. int goof = 0;
  242. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  243. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  244. symtab_index = ELF32_R_SYM(rpnt->r_info);
  245. symbol_addr = 0;
  246. if (symtab_index) {
  247. symbol_addr = (unsigned long) _dl_find_hash(strtab + symtab[symtab_index].st_name,
  248. scope, (reloc_type == R_ARM_JUMP_SLOT ? tpnt : NULL), symbolrel);
  249. /*
  250. * We want to allow undefined references to weak symbols - this might
  251. * have been intentional. We should not be linking local symbols
  252. * here, so all bases should be covered.
  253. */
  254. if (!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_GLOBAL) {
  255. goof++;
  256. }
  257. }
  258. #if defined (__SUPPORT_LD_DEBUG__)
  259. {
  260. unsigned long old_val = *reloc_addr;
  261. #endif
  262. switch (reloc_type) {
  263. case R_ARM_NONE:
  264. break;
  265. case R_ARM_ABS32:
  266. *reloc_addr += symbol_addr;
  267. break;
  268. case R_ARM_PC24:
  269. {
  270. unsigned long addend;
  271. long newvalue, topbits;
  272. addend = *reloc_addr & 0x00ffffff;
  273. if (addend & 0x00800000) addend |= 0xff000000;
  274. newvalue = symbol_addr - (unsigned long)reloc_addr + (addend << 2);
  275. topbits = newvalue & 0xfe000000;
  276. if (topbits != 0xfe000000 && topbits != 0x00000000)
  277. {
  278. newvalue = fix_bad_pc24(reloc_addr, symbol_addr)
  279. - (unsigned long)reloc_addr + (addend << 2);
  280. topbits = newvalue & 0xfe000000;
  281. if (topbits != 0xfe000000 && topbits != 0x00000000)
  282. {
  283. _dl_dprintf(2,"symbol '%s': R_ARM_PC24 relocation out of range.",
  284. symtab[symtab_index].st_name);
  285. _dl_exit(1);
  286. }
  287. }
  288. newvalue >>= 2;
  289. symbol_addr = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
  290. *reloc_addr = symbol_addr;
  291. break;
  292. }
  293. case R_ARM_GLOB_DAT:
  294. case R_ARM_JUMP_SLOT:
  295. *reloc_addr = symbol_addr;
  296. break;
  297. case R_ARM_RELATIVE:
  298. *reloc_addr += (unsigned long) tpnt->loadaddr;
  299. break;
  300. case R_ARM_COPY:
  301. #if 0
  302. /* Do this later */
  303. _dl_dprintf(2, "Doing copy for symbol ");
  304. if (symtab_index) _dl_dprintf(2, strtab + symtab[symtab_index].st_name);
  305. _dl_dprintf(2, "\n");
  306. _dl_memcpy((void *) symtab[symtab_index].st_value,
  307. (void *) symbol_addr, symtab[symtab_index].st_size);
  308. #endif
  309. break;
  310. default:
  311. return -1; /*call _dl_exit(1) */
  312. }
  313. #if defined (__SUPPORT_LD_DEBUG__)
  314. if(_dl_debug_reloc && _dl_debug_detail)
  315. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  316. }
  317. #endif
  318. return goof;
  319. }
  320. static int
  321. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
  322. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  323. {
  324. int reloc_type;
  325. unsigned long *reloc_addr;
  326. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  327. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  328. #if defined (__SUPPORT_LD_DEBUG__)
  329. {
  330. unsigned long old_val = *reloc_addr;
  331. #endif
  332. switch (reloc_type) {
  333. case R_ARM_NONE:
  334. break;
  335. case R_ARM_JUMP_SLOT:
  336. *reloc_addr += (unsigned long) tpnt->loadaddr;
  337. break;
  338. default:
  339. return -1; /*call _dl_exit(1) */
  340. }
  341. #if defined (__SUPPORT_LD_DEBUG__)
  342. if(_dl_debug_reloc && _dl_debug_detail)
  343. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  344. }
  345. #endif
  346. return 0;
  347. }
  348. /* This is done as a separate step, because there are cases where
  349. information is first copied and later initialized. This results in
  350. the wrong information being copied. Someone at Sun was complaining about
  351. a bug in the handling of _COPY by SVr4, and this may in fact be what he
  352. was talking about. Sigh. */
  353. /* No, there are cases where the SVr4 linker fails to emit COPY relocs
  354. at all */
  355. static int
  356. _dl_do_copy (struct elf_resolve *tpnt, struct dyn_elf *scope,
  357. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  358. {
  359. int reloc_type;
  360. int symtab_index;
  361. unsigned long *reloc_addr;
  362. unsigned long symbol_addr;
  363. int goof = 0;
  364. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  365. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  366. if (reloc_type != R_ARM_COPY)
  367. return 0;
  368. symtab_index = ELF32_R_SYM(rpnt->r_info);
  369. symbol_addr = 0;
  370. if (symtab_index) {
  371. symbol_addr = (unsigned long) _dl_find_hash(strtab +
  372. symtab[symtab_index].st_name, scope,
  373. NULL, copyrel);
  374. if (!symbol_addr) goof++;
  375. }
  376. if (!goof) {
  377. #if defined (__SUPPORT_LD_DEBUG__)
  378. if(_dl_debug_move)
  379. _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
  380. strtab + symtab[symtab_index].st_name,
  381. symtab[symtab_index].st_size,
  382. symbol_addr, symtab[symtab_index].st_value);
  383. #endif
  384. _dl_memcpy((char *) symtab[symtab_index].st_value,
  385. (char *) symbol_addr, symtab[symtab_index].st_size);
  386. }
  387. return goof;
  388. }
  389. void _dl_parse_lazy_relocation_information(struct elf_resolve *tpnt,
  390. unsigned long rel_addr, unsigned long rel_size, int type)
  391. {
  392. (void)_dl_parse(tpnt, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  393. }
  394. int _dl_parse_relocation_information(struct elf_resolve *tpnt,
  395. unsigned long rel_addr, unsigned long rel_size, int type)
  396. {
  397. return _dl_parse(tpnt, tpnt->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  398. }
  399. int _dl_parse_copy_information(struct dyn_elf *xpnt, unsigned long rel_addr,
  400. unsigned long rel_size, int type)
  401. {
  402. return _dl_parse(xpnt->dyn, xpnt->next, rel_addr, rel_size, _dl_do_copy);
  403. }