elfinterp.c 8.7 KB

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