elfinterp.c 10 KB

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