elfinterp.c 9.2 KB

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