elfinterp.c 10 KB

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