elfinterp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* nios2 ELF shared library loader suppport
  2. *
  3. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  4. * David Engel, Hongjiu Lu and Mitch D'Souza
  5. * Copyright (C) 2001-2004 Erik Andersen
  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. #include "ldso.h"
  31. /* Program to load an ELF binary on a linux system, and run it.
  32. References to symbols in sharable libraries can be resolved by either
  33. an ELF sharable library or a linux style of shared library. */
  34. extern int _dl_linux_resolve(void);
  35. unsigned long
  36. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  37. {
  38. ELF_RELOC *this_reloc;
  39. char *strtab;
  40. ElfW(Sym) *symtab;
  41. int symtab_index;
  42. char *rel_addr;
  43. char *new_addr;
  44. char **got_addr;
  45. ElfW(Addr) instr_addr;
  46. char *symname;
  47. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  48. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  49. symtab_index = ELF_R_SYM(this_reloc->r_info);
  50. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  51. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  52. symname = strtab + symtab[symtab_index].st_name;
  53. /* Address of the jump instruction to fix up. */
  54. instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
  55. got_addr = (char **)instr_addr;
  56. /* Get the address of the GOT entry. */
  57. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  58. if (unlikely(!new_addr)) {
  59. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  60. _dl_exit(1);
  61. }
  62. #if defined (__SUPPORT_LD_DEBUG__)
  63. if (_dl_debug_bindings) {
  64. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  65. if (_dl_debug_detail)
  66. _dl_dprintf(_dl_debug_file,
  67. "\tpatched: %x ==> %x @ %x\n",
  68. *got_addr, new_addr, got_addr);
  69. }
  70. if (!_dl_debug_nofixups)
  71. #endif
  72. *got_addr = new_addr;
  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. /* Parse the relocation information. */
  87. rpnt = (ELF_RELOC *)rel_addr;
  88. rel_size /= sizeof(ELF_RELOC);
  89. symtab = (ElfW(Sym) *)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)
  98. continue;
  99. _dl_dprintf(2, "\n%s: ", _dl_progname);
  100. if (symtab_index)
  101. _dl_dprintf(2, "symbol '%s': ",
  102. strtab + symtab[symtab_index].st_name);
  103. if (unlikely(res < 0)) {
  104. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  105. _dl_dprintf(2, "can't handle reloc type "
  106. "%x\n", reloc_type);
  107. _dl_exit(-res);
  108. } else if (unlikely(res > 0)) {
  109. _dl_dprintf(2, "can't resolve symbol\n");
  110. return res;
  111. }
  112. }
  113. return 0;
  114. }
  115. static int
  116. _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  117. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  118. {
  119. int reloc_type;
  120. int symtab_index;
  121. char *symname;
  122. #if defined USE_TLS && USE_TLS
  123. struct elf_resolve *tls_tpnt = NULL;
  124. #endif
  125. struct symbol_ref sym_ref;
  126. ElfW(Addr) *reloc_addr;
  127. ElfW(Addr) symbol_addr;
  128. #if defined (__SUPPORT_LD_DEBUG__)
  129. ElfW(Addr) old_val;
  130. #endif
  131. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  132. reloc_type = ELF_R_TYPE(rpnt->r_info);
  133. symtab_index = ELF_R_SYM(rpnt->r_info);
  134. sym_ref.sym = &symtab[symtab_index];
  135. sym_ref.tpnt = NULL;
  136. symbol_addr = 0;
  137. symname = strtab + sym_ref.sym->st_name;
  138. if (symtab_index) {
  139. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  140. elf_machine_type_class(reloc_type), &sym_ref);
  141. /*
  142. * We want to allow undefined references to weak symbols - this
  143. * might have been intentional. We should not be linking local
  144. * symbols here, so all bases should be covered.
  145. */
  146. if (unlikely(!symbol_addr && (ELF_ST_TYPE(sym_ref.sym->st_info) != STT_TLS)
  147. && (ELF_ST_BIND(sym_ref.sym->st_info) != STB_WEAK))) {
  148. /* This may be non-fatal if called from dlopen. */
  149. return 1;
  150. }
  151. #if defined USE_TLS && USE_TLS
  152. tls_tpnt = sym_ref.tpnt;
  153. #endif
  154. } else {
  155. /* Relocs against STN_UNDEF are usually treated as using a
  156. * symbol value of zero, and using the module containing the
  157. * reloc itself. */
  158. symbol_addr = sym_ref.sym->st_value;
  159. #if defined USE_TLS && USE_TLS
  160. tls_tpnt = tpnt;
  161. #endif
  162. }
  163. #if defined (__SUPPORT_LD_DEBUG__)
  164. if (reloc_addr) {
  165. old_val = *reloc_addr;
  166. } else {
  167. old_val = 0;
  168. }
  169. #endif
  170. switch (reloc_type) {
  171. case R_NIOS2_NONE:
  172. break;
  173. case R_NIOS2_BFD_RELOC_32:
  174. case R_NIOS2_GLOB_DAT:
  175. case R_NIOS2_JUMP_SLOT:
  176. *reloc_addr = symbol_addr + rpnt->r_addend;
  177. break;
  178. case R_NIOS2_RELATIVE:
  179. *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
  180. break;
  181. case R_NIOS2_COPY:
  182. if (symbol_addr) {
  183. #if defined (__SUPPORT_LD_DEBUG__)
  184. if (_dl_debug_move)
  185. _dl_dprintf(_dl_debug_file,
  186. "\t%s move %d bytes from %x to %x\n",
  187. symname, sym_ref.sym->st_size,
  188. symbol_addr, reloc_addr);
  189. #endif
  190. _dl_memcpy((char *)reloc_addr,
  191. (char *)symbol_addr,
  192. sym_ref.sym->st_size);
  193. }
  194. break;
  195. default:
  196. return -1; /* Calls _dl_exit(1). */
  197. }
  198. #if defined (__SUPPORT_LD_DEBUG__)
  199. if (_dl_debug_reloc && _dl_debug_detail)
  200. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  201. old_val, *reloc_addr, reloc_addr);
  202. #endif
  203. return 0;
  204. }
  205. static int
  206. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  207. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  208. {
  209. int reloc_type;
  210. ElfW(Addr) *reloc_addr;
  211. #if defined (__SUPPORT_LD_DEBUG__)
  212. ElfW(Addr) old_val;
  213. #endif
  214. (void)scope;
  215. (void)strtab;
  216. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  217. reloc_type = ELF_R_TYPE(rpnt->r_info);
  218. #if defined (__SUPPORT_LD_DEBUG__)
  219. old_val = *reloc_addr;
  220. #endif
  221. switch (reloc_type) {
  222. case R_NIOS2_NONE:
  223. break;
  224. case R_NIOS2_JUMP_SLOT:
  225. *reloc_addr += (unsigned long)tpnt->loadaddr;
  226. break;
  227. default:
  228. _dl_exit(1);
  229. }
  230. #if defined (__SUPPORT_LD_DEBUG__)
  231. if (_dl_debug_reloc && _dl_debug_detail)
  232. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  233. old_val, *reloc_addr, reloc_addr);
  234. #endif
  235. return 0;
  236. }
  237. void
  238. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  239. unsigned long rel_addr, unsigned long rel_size)
  240. {
  241. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  242. }
  243. int
  244. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  245. struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
  246. {
  247. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  248. }