elfinterp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* FR-V FDPIC ELF shared library loader suppport
  2. Copyright (C) 2003, 2004 Red Hat, Inc.
  3. Contributed by Alexandre Oliva <aoliva@redhat.com>
  4. Lots of code copied from ../i386/elfinterp.c, so:
  5. Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  6. David Engel, Hongjiu Lu and Mitch D'Souza
  7. Copyright (C) 2001-2002, Erik Andersen
  8. All rights reserved.
  9. This file is part of uClibc.
  10. uClibc is free software; you can redistribute it and/or modify it
  11. under the terms of the GNU Lesser General Public License as
  12. published by the Free Software Foundation; either version 2.1 of the
  13. License, or (at your option) any later version.
  14. uClibc is distributed in the hope that it will be useful, but WITHOUT
  15. ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Library General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with uClibc; see the file COPYING.LIB. If not, write to
  20. the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  21. USA. */
  22. #include <features.h>
  23. /* Program to load an ELF binary on a linux system, and run it.
  24. References to symbols in sharable libraries can be resolved by either
  25. an ELF sharable library or a linux style of shared library. */
  26. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  27. I ever taken any courses on internals. This program was developed using
  28. information available through the book "UNIX SYSTEM V RELEASE 4,
  29. Programmers guide: Ansi C and Programming Support Tools", which did
  30. a more than adequate job of explaining everything required to get this
  31. working. */
  32. struct funcdesc_value volatile attribute_hidden *
  33. _dl_linux_resolver (struct elf_resolve *tpnt, int reloc_entry)
  34. {
  35. int reloc_type;
  36. ELF_RELOC *this_reloc;
  37. char *strtab;
  38. Elf32_Sym *symtab;
  39. int symtab_index;
  40. char *rel_addr;
  41. struct elf_resolve *new_tpnt;
  42. char *new_addr;
  43. struct funcdesc_value funcval;
  44. struct funcdesc_value volatile *got_entry;
  45. char *symname;
  46. rel_addr = DL_RELOC_ADDR (tpnt->dynamic_info[DT_JMPREL],
  47. tpnt->loadaddr);
  48. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  49. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  50. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  51. symtab = (Elf32_Sym *)(intptr_t)
  52. DL_RELOC_ADDR (tpnt->dynamic_info[DT_SYMTAB],
  53. tpnt->loadaddr);
  54. strtab = DL_RELOC_ADDR (tpnt->dynamic_info[DT_STRTAB], tpnt->loadaddr);
  55. symname= strtab + symtab[symtab_index].st_name;
  56. if (reloc_type != R_FRV_FUNCDESC_VALUE) {
  57. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  58. _dl_progname);
  59. _dl_exit(1);
  60. }
  61. /* Address of GOT entry fix up */
  62. got_entry = (struct funcdesc_value *)
  63. DL_RELOC_ADDR (this_reloc->r_offset, tpnt->loadaddr);
  64. /* Get the address to be used to fill in the GOT entry. */
  65. new_addr = _dl_find_hash_mod(symname, tpnt->symbol_scope, NULL, 0,
  66. &new_tpnt);
  67. if (!new_addr) {
  68. new_addr = _dl_find_hash_mod(symname, NULL, NULL, 0,
  69. &new_tpnt);
  70. if (!new_addr) {
  71. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
  72. _dl_progname, symname);
  73. _dl_exit(1);
  74. }
  75. }
  76. funcval.entry_point = new_addr;
  77. funcval.got_value = new_tpnt->loadaddr.got_value;
  78. #if defined (__SUPPORT_LD_DEBUG__)
  79. if (_dl_debug_bindings)
  80. {
  81. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  82. if(_dl_debug_detail)
  83. _dl_dprintf(_dl_debug_file,
  84. "\n\tpatched (%x,%x) ==> (%x,%x) @ %x\n",
  85. got_entry->entry_point, got_entry->got_value,
  86. funcval.entry_point, funcval.got_value,
  87. got_entry);
  88. }
  89. if (!_dl_debug_nofixups) {
  90. *got_entry = funcval;
  91. }
  92. #else
  93. *got_entry = funcval;
  94. #endif
  95. return got_entry;
  96. }
  97. static int
  98. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  99. unsigned long rel_addr, unsigned long rel_size,
  100. int (*reloc_fnc) (struct elf_resolve *tpnt, struct dyn_elf *scope,
  101. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  102. {
  103. unsigned int i;
  104. char *strtab;
  105. Elf32_Sym *symtab;
  106. ELF_RELOC *rpnt;
  107. int symtab_index;
  108. /* Now parse the relocation information */
  109. rpnt = (ELF_RELOC *)(intptr_t) DL_RELOC_ADDR (rel_addr, tpnt->loadaddr);
  110. rel_size = rel_size / sizeof(ELF_RELOC);
  111. symtab = (Elf32_Sym *)(intptr_t)
  112. DL_RELOC_ADDR (tpnt->dynamic_info[DT_SYMTAB], tpnt->loadaddr);
  113. strtab = DL_RELOC_ADDR (tpnt->dynamic_info[DT_STRTAB], tpnt->loadaddr);
  114. for (i = 0; i < rel_size; i++, rpnt++) {
  115. int res;
  116. symtab_index = ELF32_R_SYM(rpnt->r_info);
  117. debug_sym(symtab,strtab,symtab_index);
  118. debug_reloc(symtab,strtab,rpnt);
  119. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  120. if (res==0) continue;
  121. _dl_dprintf(2, "\n%s: ",_dl_progname);
  122. if (symtab_index)
  123. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  124. if (res <0)
  125. {
  126. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  127. #if defined (__SUPPORT_LD_DEBUG__)
  128. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  129. #else
  130. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  131. #endif
  132. _dl_exit(-res);
  133. }
  134. else if (res >0)
  135. {
  136. _dl_dprintf(2, "can't resolve symbol\n");
  137. return res;
  138. }
  139. }
  140. return 0;
  141. }
  142. static int
  143. _dl_do_reloc (struct elf_resolve *tpnt,struct dyn_elf *scope,
  144. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  145. {
  146. int reloc_type;
  147. int symtab_index;
  148. char *symname;
  149. unsigned long reloc_value = 0, *reloc_addr;
  150. struct { unsigned long v; } __attribute__((__packed__))
  151. *reloc_addr_packed;
  152. unsigned long symbol_addr;
  153. struct elf_resolve *symbol_tpnt;
  154. struct funcdesc_value funcval;
  155. #if defined (__SUPPORT_LD_DEBUG__)
  156. unsigned long old_val;
  157. #endif
  158. reloc_addr = (unsigned long *)(intptr_t)
  159. DL_RELOC_ADDR (rpnt->r_offset, tpnt->loadaddr);
  160. asm ("" : "=r" (reloc_addr_packed) : "0" (reloc_addr));
  161. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  162. symtab_index = ELF32_R_SYM(rpnt->r_info);
  163. symbol_addr = 0;
  164. symname = strtab + symtab[symtab_index].st_name;
  165. if (ELF32_ST_BIND (symtab[symtab_index].st_info) == STB_LOCAL) {
  166. symbol_addr = (unsigned long)
  167. DL_RELOC_ADDR (symtab[symtab_index].st_value,
  168. tpnt->loadaddr);
  169. symbol_tpnt = tpnt;
  170. } else {
  171. symbol_addr = (unsigned long)
  172. _dl_find_hash_mod(symname, scope, NULL, 0, &symbol_tpnt);
  173. /*
  174. * We want to allow undefined references to weak symbols - this might
  175. * have been intentional. We should not be linking local symbols
  176. * here, so all bases should be covered.
  177. */
  178. if (!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK) {
  179. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  180. _dl_progname, strtab + symtab[symtab_index].st_name);
  181. _dl_exit (1);
  182. }
  183. }
  184. #if defined (__SUPPORT_LD_DEBUG__)
  185. if (_dl_debug_reloc && _dl_debug_detail)
  186. {
  187. if ((long)reloc_addr_packed & 3)
  188. old_val = reloc_addr_packed->v;
  189. else
  190. old_val = *reloc_addr;
  191. }
  192. else
  193. old_val = 0;
  194. #endif
  195. switch (reloc_type) {
  196. case R_FRV_NONE:
  197. break;
  198. case R_FRV_32:
  199. if ((long)reloc_addr_packed & 3)
  200. reloc_value = reloc_addr_packed->v += symbol_addr;
  201. else
  202. reloc_value = *reloc_addr += symbol_addr;
  203. break;
  204. case R_FRV_FUNCDESC_VALUE:
  205. funcval.entry_point = (void*)symbol_addr;
  206. /* The addend of FUNCDESC_VALUE
  207. relocations referencing global
  208. symbols must be ignored, because it
  209. may hold the address of a lazy PLT
  210. entry. */
  211. if (ELF32_ST_BIND
  212. (symtab[symtab_index].st_info)
  213. == STB_LOCAL)
  214. funcval.entry_point += *reloc_addr;
  215. reloc_value = (unsigned long)funcval.entry_point;
  216. if (symbol_addr)
  217. funcval.got_value
  218. = symbol_tpnt->loadaddr.got_value;
  219. else
  220. funcval.got_value = 0;
  221. asm ("std%I0\t%1, %M0"
  222. : "=m" (*(struct funcdesc_value *)reloc_addr)
  223. : "e" (funcval));
  224. break;
  225. case R_FRV_FUNCDESC:
  226. if ((long)reloc_addr_packed & 3)
  227. reloc_value = reloc_addr_packed->v;
  228. else
  229. reloc_value = *reloc_addr;
  230. if (symbol_addr)
  231. reloc_value = (unsigned long)_dl_funcdesc_for
  232. ((char *)symbol_addr + reloc_value,
  233. symbol_tpnt->loadaddr.got_value);
  234. else
  235. reloc_value = 0;
  236. if ((long)reloc_addr_packed & 3)
  237. reloc_addr_packed->v = reloc_value;
  238. else
  239. *reloc_addr = reloc_value;
  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, "\tpatched: %x ==> %x @ %x", old_val, reloc_value, reloc_addr);
  247. switch (reloc_type) {
  248. case R_FRV_FUNCDESC_VALUE:
  249. _dl_dprintf(_dl_debug_file, " got %x", ((struct funcdesc_value *)reloc_value)->got_value);
  250. break;
  251. case R_FRV_FUNCDESC:
  252. if (! reloc_value)
  253. break;
  254. _dl_dprintf(_dl_debug_file, " funcdesc (%x,%x)",
  255. ((struct funcdesc_value *)reloc_value)->entry_point,
  256. ((struct funcdesc_value *)reloc_value)->got_value);
  257. break;
  258. }
  259. }
  260. #endif
  261. return 0;
  262. }
  263. static int
  264. _dl_do_lazy_reloc (struct elf_resolve *tpnt,
  265. struct dyn_elf *scope __attribute_used__,
  266. ELF_RELOC *rpnt, Elf32_Sym *symtab __attribute_used__,
  267. char *strtab __attribute_used__)
  268. {
  269. int reloc_type;
  270. struct funcdesc_value volatile *reloc_addr;
  271. struct funcdesc_value funcval;
  272. #if defined (__SUPPORT_LD_DEBUG__)
  273. unsigned long old_val;
  274. #endif
  275. reloc_addr = (struct funcdesc_value *)(intptr_t)
  276. DL_RELOC_ADDR (rpnt->r_offset, tpnt->loadaddr);
  277. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  278. #if defined (__SUPPORT_LD_DEBUG__)
  279. old_val = (unsigned long)reloc_addr->entry_point;
  280. #endif
  281. switch (reloc_type) {
  282. case R_FRV_NONE:
  283. break;
  284. case R_FRV_FUNCDESC_VALUE:
  285. funcval = *reloc_addr;
  286. funcval.entry_point =
  287. DL_RELOC_ADDR (funcval.entry_point,
  288. tpnt->loadaddr);
  289. funcval.got_value = tpnt->loadaddr.got_value;
  290. *reloc_addr = funcval;
  291. break;
  292. default:
  293. return -1; /*call _dl_exit(1) */
  294. }
  295. #if defined (__SUPPORT_LD_DEBUG__)
  296. if(_dl_debug_reloc && _dl_debug_detail)
  297. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, reloc_addr->entry_point, reloc_addr);
  298. #endif
  299. return 0;
  300. }
  301. void
  302. _dl_parse_lazy_relocation_information
  303. (struct dyn_elf *rpnt, unsigned long rel_addr, unsigned long rel_size)
  304. {
  305. _dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  306. }
  307. int
  308. _dl_parse_relocation_information
  309. (struct dyn_elf *rpnt, unsigned long rel_addr, unsigned long rel_size)
  310. {
  311. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  312. }
  313. /* We don't have copy relocs. */
  314. int
  315. _dl_parse_copy_information
  316. (struct dyn_elf *rpnt __attribute_used__,
  317. unsigned long rel_addr __attribute_used__,
  318. unsigned long rel_size __attribute_used__)
  319. {
  320. return 0;
  321. }
  322. #ifndef LIBDL
  323. # include "../../libc/sysdeps/linux/frv/crtreloc.c"
  324. #endif