elfinterp.c 9.5 KB

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