elfinterp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* RISCV ELF shared library loader suppport
  2. *
  3. * Copyright (C) 2001-2004 Erik Andersen
  4. * Copyright (C) 2019 Waldemar Brodkorb <wbx@uclibc-ng.org>
  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. ElfW(Addr) instr_addr;
  44. char *symname;
  45. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  46. this_reloc = (ELF_RELOC *)(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. symname = strtab + symtab[symtab_index].st_name;
  51. /* Address of jump instruction to fix up */
  52. instr_addr = (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 (_dl_debug_bindings) {
  62. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  63. if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  64. "\tpatched %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  65. }
  66. if (!_dl_debug_nofixups) {
  67. *got_addr = new_addr;
  68. }
  69. #else
  70. *got_addr = new_addr;
  71. #endif
  72. return (unsigned long)new_addr;
  73. }
  74. static int
  75. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  76. unsigned long rel_addr, unsigned long rel_size,
  77. int (*reloc_fnc) (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  78. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  79. {
  80. unsigned int i;
  81. char *strtab;
  82. ElfW(Sym) *symtab;
  83. ELF_RELOC *rpnt;
  84. int symtab_index;
  85. /* Parse the relocation information */
  86. rpnt = (ELF_RELOC *)rel_addr;
  87. rel_size = rel_size / sizeof(ELF_RELOC);
  88. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  89. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  90. for (i = 0; i < rel_size; i++, rpnt++) {
  91. int res;
  92. symtab_index = ELF_R_SYM(rpnt->r_info);
  93. debug_sym(symtab, strtab, symtab_index);
  94. debug_reloc(symtab, strtab, rpnt);
  95. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  96. if (res==0)
  97. continue;
  98. _dl_dprintf(2, "\n%s: ", _dl_progname);
  99. if (symtab_index)
  100. _dl_dprintf(2, "symbol '%s': ",
  101. strtab + symtab[symtab_index].st_name);
  102. if (unlikely(res < 0)) {
  103. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  104. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  105. _dl_exit(-res);
  106. } else 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. #if defined USE_TLS && USE_TLS
  121. struct elf_resolve *tls_tpnt = NULL;
  122. #endif
  123. struct symbol_ref sym_ref;
  124. ElfW(Addr) *reloc_addr;
  125. ElfW(Addr) symbol_addr;
  126. #if defined (__SUPPORT_LD_DEBUG__)
  127. ElfW(Addr) old_val;
  128. #endif
  129. reloc_addr = (ElfW(Addr)*)(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. sym_ref.sym = &symtab[symtab_index];
  133. sym_ref.tpnt = NULL;
  134. symbol_addr = 0;
  135. symname = strtab + sym_ref.sym->st_name;
  136. if (symtab_index) {
  137. symbol_addr = (ElfW(Addr))_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 (unlikely (!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. return 1;
  148. }
  149. if (_dl_trace_prelink) {
  150. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  151. &sym_ref, elf_machine_type_class(reloc_type));
  152. }
  153. #if defined USE_TLS && USE_TLS
  154. tls_tpnt = sym_ref.tpnt;
  155. #endif
  156. } else {
  157. /*
  158. * Relocs against STN_UNDEF are usually treated as using a
  159. * symbol value of zero, and using the module containing the
  160. * reloc itself.
  161. */
  162. symbol_addr = sym_ref.sym->st_value;
  163. #if defined USE_TLS && USE_TLS
  164. tls_tpnt = tpnt;
  165. #endif
  166. }
  167. #if defined (__SUPPORT_LD_DEBUG__)
  168. old_val = *reloc_addr;
  169. #endif
  170. switch (reloc_type) {
  171. case R_RISCV_NONE:
  172. break;
  173. case R_RISCV_64: /* REL_SYMBOLIC */
  174. case R_RISCV_JUMP_SLOT: /* REL_PLT */
  175. *reloc_addr = symbol_addr + rpnt->r_addend;
  176. break;
  177. case R_RISCV_RELATIVE:
  178. *reloc_addr += tpnt->loadaddr + rpnt->r_addend;
  179. break;
  180. case R_RISCV_COPY:
  181. _dl_memcpy((void *) reloc_addr,
  182. (void *) symbol_addr, sym_ref.sym->st_size);
  183. break;
  184. #if defined USE_TLS && USE_TLS
  185. case R_RISCV_TLS_DTPMOD64:
  186. *reloc_addr = tls_tpnt->l_tls_modid;
  187. break;
  188. case R_RISCV_TLS_DTPREL64:
  189. *reloc_addr = symbol_addr;
  190. break;
  191. case R_RISCV_TLS_TPREL64:
  192. CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
  193. *reloc_addr = tls_tpnt->l_tls_offset + symbol_addr + rpnt->r_addend;
  194. break;
  195. #endif
  196. default:
  197. return -1; /*call _dl_exit(1) */
  198. }
  199. #if defined (__SUPPORT_LD_DEBUG__)
  200. if (_dl_debug_reloc && _dl_debug_detail) {
  201. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  202. old_val, *reloc_addr, reloc_addr);
  203. }
  204. #endif
  205. return 0;
  206. }
  207. #undef __RISCV_LAZY_RELOC_WORKS
  208. #ifdef __RISCV_LAZY_RELOC_WORKS
  209. static int
  210. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  211. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  212. {
  213. int reloc_type;
  214. ElfW(Addr) *reloc_addr;
  215. #if defined (__SUPPORT_LD_DEBUG__)
  216. ElfW(Addr) old_val;
  217. #endif
  218. (void)scope;
  219. (void)strtab;
  220. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  221. reloc_type = ELF_R_TYPE(rpnt->r_info);
  222. #if defined (__SUPPORT_LD_DEBUG__)
  223. old_val = *reloc_addr;
  224. #endif
  225. switch (reloc_type) {
  226. case R_RISCV_NONE:
  227. break;
  228. case R_RISCV_JUMP_SLOT:
  229. break;
  230. default:
  231. return -1; /*call _dl_exit(1) */
  232. }
  233. #if defined (__SUPPORT_LD_DEBUG__)
  234. if (_dl_debug_reloc && _dl_debug_detail) {
  235. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  236. old_val, *reloc_addr, reloc_addr);
  237. }
  238. #endif
  239. return 0;
  240. }
  241. #endif
  242. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  243. unsigned long rel_addr, unsigned long rel_size)
  244. {
  245. #ifdef __RISCV_LAZY_RELOC_WORKS
  246. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  247. #else
  248. _dl_parse_relocation_information(rpnt, &_dl_loaded_modules->symbol_scope,
  249. rel_addr, rel_size);
  250. #endif
  251. }
  252. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  253. struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
  254. {
  255. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  256. }