elfinterp.c 9.7 KB

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