elfinterp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #if defined (__SUPPORT_LD_DEBUG__)
  107. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  108. #else
  109. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  110. #endif
  111. _dl_exit(-res);
  112. }
  113. else if (res >0)
  114. {
  115. _dl_dprintf(2, "can't resolve symbol\n");
  116. return res;
  117. }
  118. }
  119. return 0;
  120. }
  121. static int
  122. _dl_do_reloc (struct elf_resolve *tpnt,struct r_scope_elem *scope,
  123. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  124. {
  125. int reloc_type;
  126. int symtab_index;
  127. char *symname;
  128. unsigned long reloc_value = 0, *reloc_addr;
  129. struct { unsigned long v; } __attribute__((__packed__))
  130. *reloc_addr_packed;
  131. unsigned long symbol_addr;
  132. struct elf_resolve *symbol_tpnt;
  133. struct funcdesc_value funcval;
  134. #if defined (__SUPPORT_LD_DEBUG__)
  135. unsigned long old_val;
  136. #endif
  137. struct symbol_ref sym_ref;
  138. reloc_addr = (unsigned long *) DL_RELOC_ADDR (tpnt->loadaddr, rpnt->r_offset);
  139. __asm__ ("" : "=r" (reloc_addr_packed) : "0" (reloc_addr));
  140. reloc_type = ELF_R_TYPE(rpnt->r_info);
  141. symtab_index = ELF_R_SYM(rpnt->r_info);
  142. symbol_addr = 0;
  143. sym_ref.sym = &symtab[symtab_index];
  144. sym_ref.tpnt = NULL;
  145. symname = strtab + symtab[symtab_index].st_name;
  146. if (ELF_ST_BIND (symtab[symtab_index].st_info) == STB_LOCAL) {
  147. symbol_addr = (unsigned long) DL_RELOC_ADDR(tpnt->loadaddr, symtab[symtab_index].st_value);
  148. symbol_tpnt = tpnt;
  149. } else {
  150. symbol_addr = (unsigned long)
  151. _dl_find_hash(symname, scope, NULL, 0, &sym_ref);
  152. /*
  153. * We want to allow undefined references to weak symbols - this might
  154. * have been intentional. We should not be linking local symbols
  155. * here, so all bases should be covered.
  156. */
  157. if (!symbol_addr && ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK) {
  158. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  159. _dl_progname, strtab + symtab[symtab_index].st_name);
  160. _dl_exit (1);
  161. }
  162. symbol_tpnt = sym_ref.tpnt;
  163. }
  164. #if defined (__SUPPORT_LD_DEBUG__)
  165. if (_dl_debug_reloc && _dl_debug_detail)
  166. {
  167. if ((long)reloc_addr_packed & 3)
  168. old_val = reloc_addr_packed->v;
  169. else
  170. old_val = *reloc_addr;
  171. }
  172. else
  173. old_val = 0;
  174. #endif
  175. switch (reloc_type) {
  176. case R_FRV_NONE:
  177. break;
  178. case R_FRV_32:
  179. if ((long)reloc_addr_packed & 3)
  180. reloc_value = reloc_addr_packed->v += symbol_addr;
  181. else
  182. reloc_value = *reloc_addr += symbol_addr;
  183. break;
  184. case R_FRV_FUNCDESC_VALUE:
  185. funcval.entry_point = (void*)symbol_addr;
  186. /* The addend of FUNCDESC_VALUE
  187. relocations referencing global
  188. symbols must be ignored, because it
  189. may hold the address of a lazy PLT
  190. entry. */
  191. if (ELF_ST_BIND
  192. (symtab[symtab_index].st_info)
  193. == STB_LOCAL)
  194. funcval.entry_point += *reloc_addr;
  195. reloc_value = (unsigned long)funcval.entry_point;
  196. if (symbol_addr)
  197. funcval.got_value
  198. = symbol_tpnt->loadaddr.got_value;
  199. else
  200. funcval.got_value = 0;
  201. __asm__ ("std%I0\t%1, %M0"
  202. : "=m" (*(struct funcdesc_value *)reloc_addr)
  203. : "e" (funcval));
  204. break;
  205. case R_FRV_FUNCDESC:
  206. if ((long)reloc_addr_packed & 3)
  207. reloc_value = reloc_addr_packed->v;
  208. else
  209. reloc_value = *reloc_addr;
  210. if (symbol_addr)
  211. reloc_value = (unsigned long)_dl_funcdesc_for
  212. ((char *)symbol_addr + reloc_value,
  213. symbol_tpnt->loadaddr.got_value);
  214. else
  215. reloc_value = 0;
  216. if ((long)reloc_addr_packed & 3)
  217. reloc_addr_packed->v = reloc_value;
  218. else
  219. *reloc_addr = reloc_value;
  220. break;
  221. default:
  222. return -1; /*call _dl_exit(1) */
  223. }
  224. #if defined (__SUPPORT_LD_DEBUG__)
  225. if (_dl_debug_reloc && _dl_debug_detail) {
  226. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, reloc_value, reloc_addr);
  227. switch (reloc_type) {
  228. case R_FRV_FUNCDESC_VALUE:
  229. _dl_dprintf(_dl_debug_file, " got %x", ((struct funcdesc_value *)reloc_value)->got_value);
  230. break;
  231. case R_FRV_FUNCDESC:
  232. if (! reloc_value)
  233. break;
  234. _dl_dprintf(_dl_debug_file, " funcdesc (%x,%x)",
  235. ((struct funcdesc_value *)reloc_value)->entry_point,
  236. ((struct funcdesc_value *)reloc_value)->got_value);
  237. break;
  238. }
  239. }
  240. #endif
  241. return 0;
  242. }
  243. static int
  244. _dl_do_lazy_reloc (struct elf_resolve *tpnt,
  245. struct r_scope_elem *scope __attribute__((unused)),
  246. ELF_RELOC *rpnt, ElfW(Sym) *symtab __attribute__((unused)),
  247. char *strtab __attribute__((unused)))
  248. {
  249. int reloc_type;
  250. struct funcdesc_value volatile *reloc_addr;
  251. struct funcdesc_value funcval;
  252. #if defined (__SUPPORT_LD_DEBUG__)
  253. unsigned long old_val;
  254. #endif
  255. reloc_addr = (struct funcdesc_value *)(intptr_t)
  256. DL_RELOC_ADDR (tpnt->loadaddr, rpnt->r_offset);
  257. reloc_type = ELF_R_TYPE(rpnt->r_info);
  258. #if defined (__SUPPORT_LD_DEBUG__)
  259. old_val = (unsigned long)reloc_addr->entry_point;
  260. #endif
  261. switch (reloc_type) {
  262. case R_FRV_NONE:
  263. break;
  264. case R_FRV_FUNCDESC_VALUE:
  265. funcval = *reloc_addr;
  266. funcval.entry_point = (void *) DL_RELOC_ADDR(tpnt->loadaddr, funcval.entry_point);
  267. funcval.got_value = tpnt->loadaddr.got_value;
  268. *reloc_addr = funcval;
  269. break;
  270. default:
  271. return -1; /*call _dl_exit(1) */
  272. }
  273. #if defined (__SUPPORT_LD_DEBUG__)
  274. if (_dl_debug_reloc && _dl_debug_detail)
  275. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, reloc_addr->entry_point, reloc_addr);
  276. #endif
  277. return 0;
  278. }
  279. void
  280. _dl_parse_lazy_relocation_information
  281. (struct dyn_elf *rpnt, unsigned long rel_addr, unsigned long rel_size)
  282. {
  283. _dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  284. }
  285. int
  286. _dl_parse_relocation_information
  287. (struct dyn_elf *rpnt, struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
  288. {
  289. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  290. }
  291. /* We don't have copy relocs. */
  292. int
  293. _dl_parse_copy_information
  294. (struct dyn_elf *rpnt __attribute__((unused)),
  295. unsigned long rel_addr __attribute__((unused)),
  296. unsigned long rel_size __attribute__((unused)))
  297. {
  298. return 0;
  299. }
  300. #ifndef IS_IN_libdl
  301. # include "../../libc/sysdeps/linux/frv/crtreloc.c"
  302. #endif