elfinterp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* vi: set sw=4 ts=4: */
  2. /* ARM ELF shared library loader suppport
  3. *
  4. * Copyright (C) 2001-2004 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. /* Program to load an ELF binary on a linux system, and run it.
  30. References to symbols in sharable libraries can be resolved by either
  31. an ELF sharable library or a linux style of shared library. */
  32. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  33. I ever taken any courses on internals. This program was developed using
  34. information available through the book "UNIX SYSTEM V RELEASE 4,
  35. Programmers guide: Ansi C and Programming Support Tools", which did
  36. a more than adequate job of explaining everything required to get this
  37. working. */
  38. #include "ldso.h"
  39. extern int _dl_linux_resolve(void);
  40. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  41. {
  42. int reloc_type;
  43. ELF_RELOC *this_reloc;
  44. char *strtab;
  45. char *symname;
  46. Elf32_Sym *symtab;
  47. ELF_RELOC *rel_addr;
  48. int symtab_index;
  49. char *new_addr;
  50. char **got_addr;
  51. unsigned long instr_addr;
  52. rel_addr = (ELF_RELOC *) tpnt->dynamic_info[DT_JMPREL];
  53. this_reloc = rel_addr + (reloc_entry >> 3);
  54. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  55. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  56. symtab = (Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB];
  57. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  58. symname = strtab + symtab[symtab_index].st_name;
  59. if (unlikely(reloc_type != R_ARM_JUMP_SLOT)) {
  60. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  61. _dl_progname);
  62. _dl_exit(1);
  63. };
  64. /* Address of jump instruction to fix up */
  65. instr_addr = ((unsigned long) this_reloc->r_offset +
  66. (unsigned long) tpnt->loadaddr);
  67. got_addr = (char **) instr_addr;
  68. /* Get the address of the GOT entry */
  69. new_addr = _dl_find_hash(symname, tpnt->symbol_scope,
  70. tpnt, ELF_RTYPE_CLASS_PLT);
  71. if (unlikely(!new_addr)) {
  72. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
  73. _dl_progname, symname);
  74. _dl_exit(1);
  75. };
  76. #if defined (__SUPPORT_LD_DEBUG__)
  77. if ((unsigned long) got_addr < 0x40000000)
  78. {
  79. if (_dl_debug_bindings)
  80. {
  81. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  82. if(_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  83. "\tpatch %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  84. }
  85. }
  86. if (!_dl_debug_nofixups) {
  87. *got_addr = new_addr;
  88. }
  89. #else
  90. *got_addr = new_addr;
  91. #endif
  92. return (unsigned long) new_addr;
  93. }
  94. static int
  95. _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, struct dyn_elf *scope,
  98. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  99. {
  100. int i;
  101. char *strtab;
  102. int goof = 0;
  103. Elf32_Sym *symtab;
  104. ELF_RELOC *rpnt;
  105. int symtab_index;
  106. /* Now parse the relocation information */
  107. rpnt = (ELF_RELOC *) rel_addr;
  108. rel_size = rel_size / sizeof(ELF_RELOC);
  109. symtab = (Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB];
  110. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  111. for (i = 0; i < rel_size; i++, rpnt++) {
  112. int res;
  113. symtab_index = ELF32_R_SYM(rpnt->r_info);
  114. #if defined (__SUPPORT_LD_DEBUG__)
  115. debug_sym(symtab,strtab,symtab_index);
  116. debug_reloc(symtab,strtab,rpnt);
  117. #endif
  118. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  119. if (res==0) continue;
  120. _dl_dprintf(2, "\n%s: ",_dl_progname);
  121. if (symtab_index)
  122. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  123. if (unlikely(res <0))
  124. {
  125. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  126. #if defined (__SUPPORT_LD_DEBUG__)
  127. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  128. #else
  129. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  130. #endif
  131. _dl_exit(-res);
  132. }
  133. if (unlikely(res >0))
  134. {
  135. _dl_dprintf(2, "can't resolve symbol\n");
  136. goof += res;
  137. }
  138. }
  139. return goof;
  140. }
  141. #if 0
  142. static unsigned long
  143. fix_bad_pc24 (unsigned long *const reloc_addr, unsigned long value)
  144. {
  145. static void *fix_page;
  146. static unsigned int fix_offset;
  147. unsigned int *fix_address;
  148. if (! fix_page)
  149. {
  150. fix_page = _dl_mmap (NULL, PAGE_SIZE , PROT_READ | PROT_WRITE | PROT_EXEC,
  151. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  152. fix_offset = 0;
  153. }
  154. fix_address = (unsigned int *)(fix_page + fix_offset);
  155. fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
  156. fix_address[1] = value;
  157. fix_offset += 8;
  158. if (fix_offset >= PAGE_SIZE)
  159. fix_page = NULL;
  160. return (unsigned long)fix_address;
  161. }
  162. #endif
  163. static int
  164. _dl_do_reloc (struct elf_resolve *tpnt,struct dyn_elf *scope,
  165. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  166. {
  167. int reloc_type;
  168. int symtab_index;
  169. unsigned long *reloc_addr;
  170. unsigned long symbol_addr;
  171. int goof = 0;
  172. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  173. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  174. symtab_index = ELF32_R_SYM(rpnt->r_info);
  175. symbol_addr = 0;
  176. if (symtab_index) {
  177. symbol_addr = (unsigned long) _dl_find_hash(strtab + symtab[symtab_index].st_name,
  178. scope, tpnt, elf_machine_type_class(reloc_type));
  179. /*
  180. * We want to allow undefined references to weak symbols - this might
  181. * have been intentional. We should not be linking local symbols
  182. * here, so all bases should be covered.
  183. */
  184. if (!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK) {
  185. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  186. _dl_progname, strtab + symtab[symtab_index].st_name);
  187. _dl_exit (1);
  188. }
  189. }
  190. #if defined (__SUPPORT_LD_DEBUG__)
  191. {
  192. unsigned long old_val = *reloc_addr;
  193. #endif
  194. switch (reloc_type) {
  195. case R_ARM_NONE:
  196. break;
  197. case R_ARM_ABS32:
  198. *reloc_addr += symbol_addr;
  199. break;
  200. case R_ARM_PC24:
  201. #if 0
  202. {
  203. unsigned long addend;
  204. long newvalue, topbits;
  205. addend = *reloc_addr & 0x00ffffff;
  206. if (addend & 0x00800000) addend |= 0xff000000;
  207. newvalue = symbol_addr - (unsigned long)reloc_addr + (addend << 2);
  208. topbits = newvalue & 0xfe000000;
  209. if (topbits != 0xfe000000 && topbits != 0x00000000)
  210. {
  211. newvalue = fix_bad_pc24(reloc_addr, symbol_addr)
  212. - (unsigned long)reloc_addr + (addend << 2);
  213. topbits = newvalue & 0xfe000000;
  214. if (unlikely(topbits != 0xfe000000 && topbits != 0x00000000))
  215. {
  216. _dl_dprintf(2,"symbol '%s': R_ARM_PC24 relocation out of range.",
  217. symtab[symtab_index].st_name);
  218. _dl_exit(1);
  219. }
  220. }
  221. newvalue >>= 2;
  222. symbol_addr = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
  223. *reloc_addr = symbol_addr;
  224. break;
  225. }
  226. #else
  227. _dl_dprintf(2,"R_ARM_PC24: Compile shared libraries with -fPIC!\n");
  228. _dl_exit(1);
  229. #endif
  230. case R_ARM_GLOB_DAT:
  231. case R_ARM_JUMP_SLOT:
  232. *reloc_addr = symbol_addr;
  233. break;
  234. case R_ARM_RELATIVE:
  235. *reloc_addr += (unsigned long) tpnt->loadaddr;
  236. break;
  237. case R_ARM_COPY:
  238. _dl_memcpy((void *) reloc_addr,
  239. (void *) symbol_addr, symtab[symtab_index].st_size);
  240. break;
  241. default:
  242. return -1; /*call _dl_exit(1) */
  243. }
  244. #if defined (__SUPPORT_LD_DEBUG__)
  245. if(_dl_debug_reloc && _dl_debug_detail)
  246. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  247. }
  248. #endif
  249. return goof;
  250. }
  251. static int
  252. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
  253. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  254. {
  255. int reloc_type;
  256. unsigned long *reloc_addr;
  257. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  258. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  259. #if defined (__SUPPORT_LD_DEBUG__)
  260. {
  261. unsigned long old_val = *reloc_addr;
  262. #endif
  263. switch (reloc_type) {
  264. case R_ARM_NONE:
  265. break;
  266. case R_ARM_JUMP_SLOT:
  267. *reloc_addr += (unsigned long) tpnt->loadaddr;
  268. break;
  269. default:
  270. return -1; /*call _dl_exit(1) */
  271. }
  272. #if defined (__SUPPORT_LD_DEBUG__)
  273. if(_dl_debug_reloc && _dl_debug_detail)
  274. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  275. }
  276. #endif
  277. return 0;
  278. }
  279. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  280. unsigned long rel_addr, unsigned long rel_size)
  281. {
  282. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  283. }
  284. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  285. unsigned long rel_addr, unsigned long rel_size)
  286. {
  287. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  288. }