elfinterp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* vi: set sw=4 ts=4: */
  2. /* SuperH ELF shared library loader suppport
  3. *
  4. * Copyright (C) 2002, Stefan Allius <allius@atecom.com> and
  5. * Eddie C. Dost <ecd@atecom.com>
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. The name of the above contributors may not be
  15. * used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /* Program to load an ELF binary on a linux system, and run it.
  31. References to symbols in sharable libraries can be resolved by either
  32. an ELF sharable library or a linux style of shared library. */
  33. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  34. I ever taken any courses on internals. This program was developed using
  35. information available through the book "UNIX SYSTEM V RELEASE 4,
  36. Programmers guide: Ansi C and Programming Support Tools", which did
  37. a more than adequate job of explaining everything required to get this
  38. working. */
  39. #include "ldso.h"
  40. extern int _dl_linux_resolve(void);
  41. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  42. {
  43. ELF_RELOC *this_reloc;
  44. char *strtab;
  45. ElfW(Sym) *symtab;
  46. int symtab_index;
  47. char *rel_addr;
  48. char *new_addr;
  49. char **got_addr;
  50. unsigned long instr_addr;
  51. char *symname;
  52. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  53. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  54. symtab_index = ELF_R_SYM(this_reloc->r_info);
  55. symtab = (ElfW(Sym) *)(intptr_t) tpnt->dynamic_info[DT_SYMTAB];
  56. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  57. symname = strtab + symtab[symtab_index].st_name;
  58. /* Address of jump instruction to fix up */
  59. instr_addr = (unsigned long) (this_reloc->r_offset + tpnt->loadaddr);
  60. got_addr = (char **) instr_addr;
  61. /* Get the address of the GOT entry */
  62. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  63. if (unlikely(!new_addr)) {
  64. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  65. _dl_exit(1);
  66. }
  67. #if defined (__SUPPORT_LD_DEBUG__)
  68. if ((unsigned long) got_addr < 0x20000000) {
  69. if (_dl_debug_bindings) {
  70. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  71. if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  72. "\n\tpatched %x ==> %x @ %x\n", *got_addr, new_addr, got_addr);
  73. }
  74. }
  75. if (!_dl_debug_nofixups)
  76. *got_addr = new_addr;
  77. #else
  78. *got_addr = new_addr;
  79. #endif
  80. return (unsigned long) new_addr;
  81. }
  82. static int
  83. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  84. unsigned long rel_addr, unsigned long rel_size,
  85. int (*reloc_fnc) (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  86. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  87. {
  88. unsigned int i;
  89. char *strtab;
  90. ElfW(Sym) *symtab;
  91. ELF_RELOC *rpnt;
  92. int symtab_index;
  93. /* Now parse the relocation information */
  94. rpnt = (ELF_RELOC *)(intptr_t) rel_addr;
  95. rel_size = rel_size / sizeof(ELF_RELOC);
  96. symtab = (ElfW(Sym) *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  97. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  98. for (i = 0; i < rel_size; i++, rpnt++) {
  99. int res;
  100. symtab_index = ELF_R_SYM(rpnt->r_info);
  101. debug_sym(symtab,strtab,symtab_index);
  102. debug_reloc(symtab,strtab,rpnt);
  103. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  104. if (res == 0) continue;
  105. _dl_dprintf(2, "\n%s: ",_dl_progname);
  106. if (symtab_index)
  107. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  108. if (unlikely(res < 0)) {
  109. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  110. #if defined (__SUPPORT_LD_DEBUG__)
  111. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  112. #else
  113. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  114. #endif
  115. _dl_exit(-res);
  116. }
  117. if (unlikely(res > 0)) {
  118. _dl_dprintf(2, "can't resolve symbol\n");
  119. return res;
  120. }
  121. }
  122. return 0;
  123. }
  124. static int
  125. _dl_do_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  126. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  127. {
  128. int reloc_type;
  129. int symtab_index;
  130. char *symname;
  131. unsigned long *reloc_addr;
  132. unsigned long symbol_addr;
  133. #if defined (__SUPPORT_LD_DEBUG__)
  134. unsigned long old_val;
  135. #endif
  136. #if defined USE_TLS && USE_TLS
  137. struct elf_resolve *tls_tpnt = NULL;
  138. #endif
  139. struct symbol_ref sym_ref;
  140. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  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. if (symtab_index) {
  147. symname = strtab + symtab[symtab_index].st_name;
  148. symbol_addr = (unsigned long) _dl_find_hash(symname, scope, tpnt,
  149. elf_machine_type_class(reloc_type), &sym_ref);
  150. /*
  151. * We want to allow undefined references to weak symbols - this might
  152. * have been intentional. We should not be linking local symbols
  153. * here, so all bases should be covered.
  154. */
  155. if (!symbol_addr
  156. && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
  157. && (ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
  158. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
  159. _dl_progname, symname);
  160. /* Let the caller to handle the error: it may be non fatal if called from dlopen */
  161. return 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. #if defined USE_TLS && USE_TLS
  168. tls_tpnt = sym_ref.tpnt;
  169. #endif
  170. }
  171. #if defined (__SUPPORT_LD_DEBUG__)
  172. old_val = *reloc_addr;
  173. #endif
  174. #if defined USE_TLS && USE_TLS
  175. /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
  176. symbol. This is the case for a static tls variable, so the lookup
  177. module is just that one is referencing the tls variable. */
  178. if (!tls_tpnt)
  179. tls_tpnt = tpnt;
  180. #endif
  181. switch (reloc_type) {
  182. case R_SH_NONE:
  183. break;
  184. case R_SH_COPY:
  185. if (symbol_addr) {
  186. #if defined (__SUPPORT_LD_DEBUG__)
  187. if (_dl_debug_move)
  188. _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
  189. symname, symtab[symtab_index].st_size,
  190. symbol_addr, reloc_addr);
  191. #endif
  192. _dl_memcpy((char *) reloc_addr, (char *) symbol_addr, symtab[symtab_index].st_size);
  193. }
  194. return 0; /* no further LD_DEBUG messages for copy relocs */
  195. case R_SH_DIR32:
  196. case R_SH_GLOB_DAT:
  197. case R_SH_JMP_SLOT:
  198. *reloc_addr = symbol_addr + rpnt->r_addend;
  199. break;
  200. case R_SH_REL32:
  201. *reloc_addr = symbol_addr + rpnt->r_addend -
  202. (unsigned long) reloc_addr;
  203. break;
  204. case R_SH_RELATIVE:
  205. *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
  206. break;
  207. #if defined USE_TLS && USE_TLS
  208. case R_SH_TLS_DTPMOD32:
  209. *reloc_addr = tls_tpnt->l_tls_modid;
  210. break;
  211. case R_SH_TLS_DTPOFF32:
  212. *reloc_addr = symbol_addr;
  213. break;
  214. case R_SH_TLS_TPOFF32:
  215. CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
  216. *reloc_addr = tls_tpnt->l_tls_offset + symbol_addr + rpnt->r_addend;
  217. break;
  218. #endif
  219. default:
  220. return -1;
  221. }
  222. #if defined (__SUPPORT_LD_DEBUG__)
  223. if (_dl_debug_reloc && _dl_debug_detail)
  224. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
  225. #endif
  226. return 0;
  227. }
  228. static int
  229. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  230. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  231. {
  232. int reloc_type;
  233. unsigned long *reloc_addr;
  234. #if defined (__SUPPORT_LD_DEBUG__)
  235. unsigned long old_val;
  236. #endif
  237. (void)scope;
  238. (void)symtab;
  239. (void)strtab;
  240. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  241. reloc_type = ELF_R_TYPE(rpnt->r_info);
  242. #if defined (__SUPPORT_LD_DEBUG__)
  243. old_val = *reloc_addr;
  244. #endif
  245. switch (reloc_type) {
  246. case R_SH_NONE:
  247. break;
  248. case R_SH_JMP_SLOT:
  249. *reloc_addr += (unsigned long) tpnt->loadaddr;
  250. break;
  251. default:
  252. return -1;
  253. }
  254. #if defined (__SUPPORT_LD_DEBUG__)
  255. if (_dl_debug_reloc && _dl_debug_detail)
  256. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
  257. #endif
  258. return 0;
  259. }
  260. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  261. unsigned long rel_addr, unsigned long rel_size)
  262. {
  263. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  264. }
  265. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  266. struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
  267. {
  268. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  269. }