elfinterp.c 10 KB

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