elfinterp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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, ELF_RTYPE_CLASS_PLT);
  127. if (!new_addr) {
  128. _dl_dprintf(2, "%s: Can't resolv symbol '%s'\n", _dl_progname, symname);
  129. _dl_exit(1);
  130. }
  131. #if defined (__SUPPORT_LD_DEBUG__)
  132. if (_dl_debug_bindings) {
  133. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  134. if (_dl_debug_detail)
  135. _dl_dprintf(_dl_debug_file, "\tpatch %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  136. }
  137. #endif
  138. *got_addr = new_addr;
  139. return (unsigned long) new_addr;
  140. }
  141. static int
  142. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope, unsigned long rel_addr,
  143. unsigned long rel_size, int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
  144. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  145. {
  146. int symtab_index;
  147. int res;
  148. unsigned int i;
  149. char *strtab;
  150. Elf32_Sym *symtab;
  151. ELF_RELOC *rpnt;
  152. /* Parse the relocation information. */
  153. rpnt = (ELF_RELOC *) (intptr_t) (rel_addr + tpnt->loadaddr);
  154. rel_size /= sizeof(ELF_RELOC);
  155. symtab = (Elf32_Sym *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  156. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  157. for (i = 0; i < rel_size; i++, rpnt++) {
  158. symtab_index = ELF32_R_SYM(rpnt->r_info);
  159. /*
  160. * Make sure the same symbols that the linker resolved when it
  161. * bootstapped itself isn't resolved again.
  162. */
  163. if (!symtab_index && tpnt->libtype == program_interpreter)
  164. continue;
  165. if (symtab_index && tpnt->libtype == program_interpreter &&
  166. _dl_symbol(strtab + symtab[symtab_index].st_name))
  167. continue;
  168. #if defined (__SUPPORT_LD_DEBUG__)
  169. debug_sym(symtab, strtab, symtab_index);
  170. debug_reloc(symtab, strtab, rpnt);
  171. #endif
  172. /* Pass over to actual relocation function. */
  173. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  174. if (res == 0)
  175. continue;
  176. _dl_dprintf(2, "\n%s: ", _dl_progname);
  177. if (symtab_index)
  178. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  179. if (res < 0) {
  180. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  181. #if defined (__SUPPORT_LD_DEBUG__)
  182. _dl_dprintf(2, "can't handle relocation type '%s'\n", _dl_reltypes(reloc_type));
  183. #else
  184. _dl_dprintf(2, "can't handle relocation type %x\n", reloc_type);
  185. #endif
  186. _dl_exit(-res);
  187. }
  188. else if (res > 0) {
  189. _dl_dprintf(2, "can't resolv symbol\n");
  190. return res;
  191. }
  192. }
  193. return 0;
  194. }
  195. static int
  196. _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope, ELF_RELOC *rpnt,
  197. Elf32_Sym *symtab, char *strtab)
  198. {
  199. int reloc_type;
  200. int symtab_index;
  201. char *symname;
  202. unsigned long *reloc_addr;
  203. unsigned symbol_addr;
  204. #if defined (__SUPPORT_LD_DEBUG__)
  205. unsigned long old_val;
  206. #endif
  207. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  208. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  209. symtab_index = ELF32_R_SYM(rpnt->r_info);
  210. symbol_addr = 0;
  211. symname = strtab + symtab[symtab_index].st_name;
  212. if (symtab_index) {
  213. if (symtab[symtab_index].st_shndx != SHN_UNDEF &&
  214. ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL) {
  215. symbol_addr = (unsigned long) tpnt->loadaddr;
  216. }
  217. else {
  218. symbol_addr = (unsigned long) _dl_find_hash(symname, scope,
  219. elf_machine_type_class(reloc_type));
  220. }
  221. if (!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_GLOBAL) {
  222. #if defined (__SUPPORT_LD_DEBUG__)
  223. _dl_dprintf(2, "\tglobal symbol '%s' already defined in '%s'\n",
  224. symname, tpnt->libname);
  225. #endif
  226. return 0;
  227. }
  228. symbol_addr += rpnt->r_addend;
  229. }
  230. #if defined (__SUPPORT_LD_DEBUG__)
  231. old_val = *reloc_addr;
  232. #endif
  233. switch (reloc_type) {
  234. case R_CRIS_NONE:
  235. break;
  236. case R_CRIS_GLOB_DAT:
  237. case R_CRIS_JUMP_SLOT:
  238. case R_CRIS_32:
  239. case R_CRIS_COPY:
  240. *reloc_addr = symbol_addr;
  241. break;
  242. case R_CRIS_RELATIVE:
  243. *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
  244. break;
  245. default:
  246. return -1; /* Call _dl_exit(1). */
  247. }
  248. #if defined (__SUPPORT_LD_DEBUG__)
  249. if (_dl_debug_reloc && _dl_debug_detail)
  250. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  251. #endif
  252. return 0;
  253. }
  254. static int
  255. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope, ELF_RELOC *rpnt,
  256. Elf32_Sym *symtab, char *strtab)
  257. {
  258. int reloc_type;
  259. unsigned long *reloc_addr;
  260. #if defined (__SUPPORT_LD_DEBUG__)
  261. unsigned long old_val;
  262. #endif
  263. /* Don't care about these, just keep the compiler happy. */
  264. (void) scope;
  265. (void) symtab;
  266. (void) strtab;
  267. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  268. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  269. #if defined (__SUPPORT_LD_DEBUG__)
  270. old_val = *reloc_addr;
  271. #endif
  272. switch (reloc_type) {
  273. case R_CRIS_NONE:
  274. break;
  275. case R_CRIS_JUMP_SLOT:
  276. *reloc_addr += (unsigned long) tpnt->loadaddr;
  277. break;
  278. default:
  279. return -1; /* Calls _dl_exit(1). */
  280. }
  281. #if defined (__SUPPORT_LD_DEBUG__)
  282. if (_dl_debug_reloc && _dl_debug_detail)
  283. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  284. #endif
  285. return 0;
  286. }
  287. static int
  288. _dl_do_copy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope, ELF_RELOC *rpnt,
  289. Elf32_Sym *symtab, char *strtab)
  290. {
  291. int goof;
  292. int reloc_type;
  293. int symtab_index;
  294. char *symname;
  295. unsigned long *reloc_addr;
  296. unsigned long symbol_addr;
  297. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  298. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  299. if (reloc_type != R_CRIS_COPY)
  300. return 0;
  301. symtab_index = ELF32_R_SYM(rpnt->r_info);
  302. symbol_addr = 0;
  303. symname = strtab + symtab[symtab_index].st_name;
  304. goof = 0;
  305. if (symtab_index) {
  306. symbol_addr = (unsigned long) _dl_find_hash(symname, scope, ELF_RTYPE_CLASS_COPY);
  307. if (!symbol_addr)
  308. goof++;
  309. }
  310. if (!goof) {
  311. #if defined (__SUPPORT_LD_DEBUG__)
  312. if (_dl_debug_move)
  313. _dl_dprintf(_dl_debug_file, "\n%s move %x bytes from %x to %x",
  314. symname, symtab[symtab_index].st_size, symbol_addr, symtab[symtab_index].st_value);
  315. #endif
  316. _dl_memcpy((char *) symtab[symtab_index].st_value,
  317. (char *) symbol_addr, symtab[symtab_index].st_size);
  318. }
  319. return goof;
  320. }
  321. /* External interface to the generic part of the dynamic linker. */
  322. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  323. unsigned long rel_addr, unsigned long rel_size, int type)
  324. {
  325. /* Keep the compiler happy. */
  326. (void) type;
  327. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  328. }
  329. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  330. unsigned long rel_addr, unsigned long rel_size, int type)
  331. {
  332. /* Keep the compiler happy. */
  333. (void) type;
  334. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  335. }
  336. int _dl_parse_copy_information(struct dyn_elf *rpnt,
  337. unsigned long rel_addr, unsigned long rel_size, int type)
  338. {
  339. /* Keep the compiler happy. */
  340. (void) type;
  341. return _dl_parse(rpnt->dyn, rpnt->next, rel_addr, rel_size, _dl_do_copy_reloc);
  342. }