elfinterp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * CRIS ELF shared library loader support.
  3. *
  4. * Program to load an elf binary on a linux system, and run it.
  5. * References to symbols in sharable libraries can be resolved
  6. * by either an ELF sharable library or a linux style of shared
  7. * library.
  8. *
  9. * Copyright (C) 2002, Axis Communications AB
  10. * All rights reserved
  11. *
  12. * Author: Tobias Anderberg, <tobiasa@axis.com>
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. The name of the above contributors may not be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. */
  35. /* Support for the LD_DEBUG variable. */
  36. #if defined (__SUPPORT_LD_DEBUG__)
  37. static const char *_dl_reltypes_tab[] = {
  38. [0] "R_CRIS_NONE", "R_CRIS_8", "R_CRIS_16", "R_CRIS_32",
  39. [4] "R_CRIS_8_PCREL", "R_CRIS_16_PCREL", "R_CRIS_32_PCREL", "R_CRIS_GNU_VTINHERIT",
  40. [8] "R_CRIS_GNU_VTENTRY", "R_CRIS_COPY", "R_CRIS_GLOB_DAT", "R_CRIS_JUMP_SLOT",
  41. [16] "R_CRIS_RELATIVE", "R_CRIS_16_GOT", "R_CRIS_32_GOT", "R_CRIS_16_GOTPLT",
  42. [32] "R_CRIS_32_GOTPLT", "R_CRIS_32_GOTREL", "R_CRIS_32_PLT_GOTREL", "R_CRIS_32_PLT_PCREL",
  43. };
  44. static const char *
  45. _dl_reltypes(int type)
  46. {
  47. const char *str;
  48. static char buf[22];
  49. if (type >= (sizeof(_dl_reltypes_tab)/sizeof(_dl_reltypes_tab[0])) ||
  50. NULL == (str = _dl_reltypes_tab[type]))
  51. str = _dl_simple_ltoa(buf, (unsigned long) (type));
  52. return str;
  53. }
  54. static void
  55. debug_sym(Elf32_Sym *symtab, char *strtab, int symtab_index)
  56. {
  57. if (_dl_debug_symbols) {
  58. if (symtab_index) {
  59. _dl_dprintf(_dl_debug_file,
  60. "\n%s\tvalue=%x\tsize=%x\tinfo=%x\tother=%x\tshndx=%x",
  61. strtab + symtab[symtab_index].st_name,
  62. symtab[symtab_index].st_value,
  63. symtab[symtab_index].st_size,
  64. symtab[symtab_index].st_info,
  65. symtab[symtab_index].st_other,
  66. symtab[symtab_index].st_shndx);
  67. }
  68. }
  69. }
  70. static void
  71. debug_reloc(Elf32_Sym *symtab, char *strtab, ELF_RELOC *rpnt)
  72. {
  73. if (_dl_debug_reloc) {
  74. int symtab_index;
  75. const char *sym;
  76. symtab_index = ELF32_R_SYM(rpnt->r_info);
  77. sym = symtab_index ? strtab + symtab[symtab_index].st_name : "sym=0x0";
  78. if (_dl_debug_symbols)
  79. _dl_dprintf(_dl_debug_file, "\n\t");
  80. else
  81. _dl_dprintf(_dl_debug_file, "\n%s\n\t", sym);
  82. #ifdef ELF_USES_RELOCA
  83. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\taddend=%x",
  84. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  85. rpnt->r_offset,
  86. rpnt->r_addend);
  87. #else
  88. _dl_dprintf(_dl_debug_file, "%s\toffset%x\n",
  89. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  90. rpnt->r_offset);
  91. #endif
  92. }
  93. }
  94. #endif /* __SUPPORT_LD_DEBUG__ */
  95. /* Defined in resolve.S. */
  96. extern int _dl_linux_resolv(void);
  97. unsigned long
  98. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  99. {
  100. int reloc_type;
  101. int symtab_index;
  102. char *strtab;
  103. char *symname;
  104. char *new_addr;
  105. char *rel_addr;
  106. char **got_addr;
  107. Elf32_Sym *symtab;
  108. ELF_RELOC *this_reloc;
  109. unsigned long instr_addr;
  110. rel_addr = (char *) (tpnt->dynamic_info[DT_JMPREL] + tpnt->loadaddr);
  111. this_reloc = (ELF_RELOC *) (intptr_t)(rel_addr + reloc_entry);
  112. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  113. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  114. symtab = (Elf32_Sym *) (intptr_t)(tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  115. strtab = (char *)(tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  116. symname = strtab + symtab[symtab_index].st_name;
  117. if (reloc_type != R_CRIS_JUMP_SLOT) {
  118. _dl_dprintf(2, "%s: Incorrect relocation type for jump relocations.\n",
  119. _dl_progname);
  120. _dl_exit(1);
  121. }
  122. /* Fetch the address of the jump instruction to fix up. */
  123. instr_addr = ((unsigned long) this_reloc->r_offset + (unsigned long) tpnt->loadaddr);
  124. got_addr = (char **) instr_addr;
  125. /* Fetch the address of the GOT entry. */
  126. new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, resolver);
  127. if (!new_addr) {
  128. new_addr = _dl_find_hash(symname, NULL, NULL, resolver);
  129. if (new_addr)
  130. return (unsigned long) new_addr;
  131. _dl_dprintf(2, "%s: Can't resolv symbol '%s'\n", _dl_progname, symname);
  132. _dl_exit(1);
  133. }
  134. #if defined (__SUPPORT_LD_DEBUG__)
  135. if (_dl_debug_bindings) {
  136. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  137. if (_dl_debug_detail)
  138. _dl_dprintf(_dl_debug_file, "\tpatch %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  139. }
  140. #endif
  141. *got_addr = new_addr;
  142. return (unsigned long) new_addr;
  143. }
  144. static int
  145. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope, unsigned long rel_addr,
  146. unsigned long rel_size, int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
  147. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  148. {
  149. int symtab_index;
  150. int res;
  151. unsigned int i;
  152. char *strtab;
  153. Elf32_Sym *symtab;
  154. ELF_RELOC *rpnt;
  155. /* Parse the relocation information. */
  156. rpnt = (ELF_RELOC *) (intptr_t) (rel_addr + tpnt->loadaddr);
  157. rel_size /= sizeof(ELF_RELOC);
  158. symtab = (Elf32_Sym *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  159. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  160. for (i = 0; i < rel_size; i++, rpnt++) {
  161. symtab_index = ELF32_R_SYM(rpnt->r_info);
  162. /*
  163. * Make sure the same symbols that the linker resolved when it
  164. * bootstapped itself isn't resolved again.
  165. */
  166. if (!symtab_index && tpnt->libtype == program_interpreter)
  167. continue;
  168. if (symtab_index && tpnt->libtype == program_interpreter &&
  169. _dl_symbol(strtab + symtab[symtab_index].st_name))
  170. continue;
  171. #if defined (__SUPPORT_LD_DEBUG__)
  172. debug_sym(symtab, strtab, symtab_index);
  173. debug_reloc(symtab, strtab, rpnt);
  174. #endif
  175. /* Pass over to actual relocation function. */
  176. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  177. if (res == 0)
  178. continue;
  179. _dl_dprintf(2, "\n%s: ", _dl_progname);
  180. if (symtab_index)
  181. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  182. if (res < 0) {
  183. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  184. #if defined (__SUPPORT_LD_DEBUG__)
  185. _dl_dprintf(2, "can't handle relocation type '%s'\n", _dl_reltypes(reloc_type));
  186. #else
  187. _dl_dprintf(2, "can't handle relocation type %x\n", reloc_type);
  188. #endif
  189. _dl_exit(-res);
  190. }
  191. else if (res > 0) {
  192. _dl_dprintf(2, "can't resolv symbol\n");
  193. return res;
  194. }
  195. }
  196. return 0;
  197. }
  198. static int
  199. _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope, ELF_RELOC *rpnt,
  200. Elf32_Sym *symtab, char *strtab)
  201. {
  202. int reloc_type;
  203. int symtab_index;
  204. char *symname;
  205. unsigned long *reloc_addr;
  206. unsigned symbol_addr;
  207. #if defined (__SUPPORT_LD_DEBUG__)
  208. unsigned long old_val;
  209. #endif
  210. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  211. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  212. symtab_index = ELF32_R_SYM(rpnt->r_info);
  213. symbol_addr = 0;
  214. symname = strtab + symtab[symtab_index].st_name;
  215. if (symtab_index) {
  216. if (symtab[symtab_index].st_shndx != SHN_UNDEF &&
  217. ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL) {
  218. symbol_addr = (unsigned long) tpnt->loadaddr;
  219. }
  220. else {
  221. symbol_addr = (unsigned long) _dl_find_hash(symname, scope,
  222. (reloc_type == R_CRIS_JUMP_SLOT ? tpnt : NULL), symbolrel);
  223. }
  224. if (!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_GLOBAL) {
  225. #if defined (__SUPPORT_LD_DEBUG__)
  226. _dl_dprintf(2, "\tglobal symbol '%s' already defined in '%s'\n",
  227. symname, tpnt->libname);
  228. #endif
  229. return 0;
  230. }
  231. symbol_addr += rpnt->r_addend;
  232. }
  233. #if defined (__SUPPORT_LD_DEBUG__)
  234. old_val = *reloc_addr;
  235. #endif
  236. switch (reloc_type) {
  237. case R_CRIS_NONE:
  238. break;
  239. case R_CRIS_GLOB_DAT:
  240. case R_CRIS_JUMP_SLOT:
  241. case R_CRIS_32:
  242. case R_CRIS_COPY:
  243. *reloc_addr = symbol_addr;
  244. break;
  245. case R_CRIS_RELATIVE:
  246. *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
  247. break;
  248. default:
  249. return -1; /* Call _dl_exit(1). */
  250. }
  251. #if defined (__SUPPORT_LD_DEBUG__)
  252. if (_dl_debug_reloc && _dl_debug_detail)
  253. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  254. #endif
  255. return 0;
  256. }
  257. static int
  258. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope, ELF_RELOC *rpnt,
  259. Elf32_Sym *symtab, char *strtab)
  260. {
  261. int reloc_type;
  262. unsigned long *reloc_addr;
  263. #if defined (__SUPPORT_LD_DEBUG__)
  264. unsigned long old_val;
  265. #endif
  266. /* Don't care about these, just keep the compiler happy. */
  267. (void) scope;
  268. (void) symtab;
  269. (void) strtab;
  270. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  271. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  272. #if defined (__SUPPORT_LD_DEBUG__)
  273. old_val = *reloc_addr;
  274. #endif
  275. switch (reloc_type) {
  276. case R_CRIS_NONE:
  277. break;
  278. case R_CRIS_JUMP_SLOT:
  279. *reloc_addr += (unsigned long) tpnt->loadaddr;
  280. break;
  281. default:
  282. return -1; /* Calls _dl_exit(1). */
  283. }
  284. #if defined (__SUPPORT_LD_DEBUG__)
  285. if (_dl_debug_reloc && _dl_debug_detail)
  286. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  287. #endif
  288. return 0;
  289. }
  290. static int
  291. _dl_do_copy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope, ELF_RELOC *rpnt,
  292. Elf32_Sym *symtab, char *strtab)
  293. {
  294. int goof;
  295. int reloc_type;
  296. int symtab_index;
  297. char *symname;
  298. unsigned long *reloc_addr;
  299. unsigned long symbol_addr;
  300. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  301. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  302. if (reloc_type != R_CRIS_COPY)
  303. return 0;
  304. symtab_index = ELF32_R_SYM(rpnt->r_info);
  305. symbol_addr = 0;
  306. symname = strtab + symtab[symtab_index].st_name;
  307. goof = 0;
  308. if (symtab_index) {
  309. symbol_addr = (unsigned long) _dl_find_hash(symname, scope, NULL, copyrel);
  310. if (!symbol_addr)
  311. goof++;
  312. }
  313. if (!goof) {
  314. #if defined (__SUPPORT_LD_DEBUG__)
  315. if (_dl_debug_move)
  316. _dl_dprintf(_dl_debug_file, "\n%s move %x bytes from %x to %x",
  317. symname, symtab[symtab_index].st_size, symbol_addr, symtab[symtab_index].st_value);
  318. #endif
  319. _dl_memcpy((char *) symtab[symtab_index].st_value,
  320. (char *) symbol_addr, symtab[symtab_index].st_size);
  321. }
  322. return goof;
  323. }
  324. /* External interface to the generic part of the dynamic linker. */
  325. int
  326. _dl_parse_relocation_information(struct elf_resolve *tpnt, unsigned long rel_addr,
  327. unsigned long rel_size, int type)
  328. {
  329. /* Keep the compiler happy. */
  330. (void) type;
  331. return _dl_parse(tpnt, tpnt->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  332. }
  333. void
  334. _dl_parse_lazy_relocation_information(struct elf_resolve *tpnt, unsigned long rel_addr,
  335. unsigned long rel_size, int type)
  336. {
  337. /* Keep the compiler happy. */
  338. (void) type;
  339. _dl_parse(tpnt, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  340. }
  341. int
  342. _dl_parse_copy_information(struct dyn_elf *xpnt, unsigned long rel_addr,
  343. unsigned long rel_size, int type)
  344. {
  345. /* Keep the compiler happy. */
  346. (void) type;
  347. return _dl_parse(xpnt->dyn, xpnt->next, rel_addr, rel_size, _dl_do_copy_reloc);
  348. }