elfinterp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* AARCH64 ELF shared library loader suppport
  2. *
  3. * Copyright (C) 2001-2004 Erik Andersen
  4. * Copyright (C) 2016-2017 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. #if defined(USE_TLS) && USE_TLS
  34. #include "dl-tls.h"
  35. #include "tlsdeschtab.h"
  36. #endif
  37. extern int _dl_linux_resolve(void);
  38. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  39. {
  40. ELF_RELOC *this_reloc;
  41. char *strtab;
  42. ElfW(Sym) *symtab;
  43. int symtab_index;
  44. char *rel_addr;
  45. char *new_addr;
  46. char **got_addr;
  47. ElfW(Addr) instr_addr;
  48. char *symname;
  49. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  50. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  51. symtab_index = ELF_R_SYM(this_reloc->r_info);
  52. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  53. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  54. symname = strtab + symtab[symtab_index].st_name;
  55. /* Address of jump instruction to fix up */
  56. instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
  57. got_addr = (char **)instr_addr;
  58. /* Get the address of the GOT entry */
  59. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  60. if (unlikely(!new_addr)) {
  61. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  62. _dl_exit(1);
  63. }
  64. #if defined (__SUPPORT_LD_DEBUG__)
  65. if (_dl_debug_bindings) {
  66. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  67. if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  68. "\tpatched %x ==> %x @ %x", *got_addr, new_addr, got_addr);
  69. }
  70. if (!_dl_debug_nofixups) {
  71. *got_addr = new_addr;
  72. }
  73. #else
  74. *got_addr = new_addr;
  75. #endif
  76. return (unsigned long)new_addr;
  77. }
  78. static int
  79. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  80. unsigned long rel_addr, unsigned long rel_size,
  81. int (*reloc_fnc) (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  82. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  83. {
  84. unsigned int i;
  85. char *strtab;
  86. ElfW(Sym) *symtab;
  87. ELF_RELOC *rpnt;
  88. int symtab_index;
  89. /* Parse the relocation information */
  90. rpnt = (ELF_RELOC *)rel_addr;
  91. rel_size = rel_size / sizeof(ELF_RELOC);
  92. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  93. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  94. for (i = 0; i < rel_size; i++, rpnt++) {
  95. int res;
  96. symtab_index = ELF_R_SYM(rpnt->r_info);
  97. debug_sym(symtab, strtab, symtab_index);
  98. debug_reloc(symtab, strtab, rpnt);
  99. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  100. if (res==0)
  101. continue;
  102. _dl_dprintf(2, "\n%s: ", _dl_progname);
  103. if (symtab_index)
  104. _dl_dprintf(2, "symbol '%s': ",
  105. strtab + symtab[symtab_index].st_name);
  106. if (unlikely(res < 0)) {
  107. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  108. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  109. _dl_exit(-res);
  110. } else if (unlikely(res > 0)) {
  111. _dl_dprintf(2, "can't resolve symbol\n");
  112. return res;
  113. }
  114. }
  115. return 0;
  116. }
  117. static int
  118. _dl_do_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  119. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  120. {
  121. int reloc_type;
  122. int symtab_index;
  123. char *symname;
  124. #if defined USE_TLS && USE_TLS
  125. struct elf_resolve *tls_tpnt = NULL;
  126. #endif
  127. struct symbol_ref sym_ref;
  128. ElfW(Addr) *reloc_addr;
  129. ElfW(Addr) symbol_addr;
  130. #if defined (__SUPPORT_LD_DEBUG__)
  131. ElfW(Addr) old_val;
  132. #endif
  133. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  134. reloc_type = ELF_R_TYPE(rpnt->r_info);
  135. symtab_index = ELF_R_SYM(rpnt->r_info);
  136. sym_ref.sym = &symtab[symtab_index];
  137. sym_ref.tpnt = NULL;
  138. symbol_addr = 0;
  139. symname = strtab + sym_ref.sym->st_name;
  140. if (symtab_index) {
  141. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  142. elf_machine_type_class(reloc_type), &sym_ref);
  143. /*
  144. * We want to allow undefined references to weak symbols - this might
  145. * have been intentional. We should not be linking local symbols
  146. * here, so all bases should be covered.
  147. */
  148. if (unlikely (!symbol_addr &&
  149. (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS) &&
  150. (ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK))) {
  151. return 1;
  152. }
  153. if (_dl_trace_prelink) {
  154. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  155. &sym_ref, elf_machine_type_class(reloc_type));
  156. }
  157. #if defined USE_TLS && USE_TLS
  158. tls_tpnt = sym_ref.tpnt;
  159. #endif
  160. } else {
  161. /*
  162. * Relocs against STN_UNDEF are usually treated as using a
  163. * symbol value of zero, and using the module containing the
  164. * reloc itself.
  165. */
  166. symbol_addr = sym_ref.sym->st_value;
  167. #if defined USE_TLS && USE_TLS
  168. tls_tpnt = tpnt;
  169. #endif
  170. }
  171. #if defined (__SUPPORT_LD_DEBUG__)
  172. old_val = *reloc_addr;
  173. #endif
  174. switch (reloc_type) {
  175. case R_AARCH64_NONE:
  176. break;
  177. case R_AARCH64_ABS64: /* REL_SYMBOLIC */
  178. case R_AARCH64_GLOB_DAT: /* REL_GOT */
  179. case R_AARCH64_JUMP_SLOT: /* REL_PLT */
  180. *reloc_addr = symbol_addr + rpnt->r_addend;
  181. break;
  182. case R_AARCH64_RELATIVE:
  183. *reloc_addr += tpnt->loadaddr + rpnt->r_addend;
  184. break;
  185. case R_AARCH64_COPY:
  186. _dl_memcpy((void *) reloc_addr,
  187. (void *) symbol_addr, sym_ref.sym->st_size);
  188. break;
  189. #if defined USE_TLS && USE_TLS
  190. case R_AARCH64_TLS_TPREL:
  191. CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
  192. *reloc_addr = (symbol_addr + tls_tpnt->l_tls_offset);
  193. break;
  194. case R_AARCH64_TLSDESC:
  195. {
  196. struct tlsdesc volatile *td =
  197. (struct tlsdesc volatile *)reloc_addr;
  198. #ifndef SHARED
  199. CHECK_STATIC_TLS((struct link_map *) tls_tpnt);
  200. #else
  201. if (!TRY_STATIC_TLS ((struct link_map *) tls_tpnt))
  202. {
  203. td->arg = _dl_make_tlsdesc_dynamic((struct link_map *) tls_tpnt, symbol_addr);
  204. td->entry = _dl_tlsdesc_dynamic;
  205. }
  206. else
  207. #endif
  208. {
  209. td->arg = symbol_addr + tls_tpnt->l_tls_offset;
  210. td->entry = _dl_tlsdesc_return;
  211. }
  212. }
  213. break;
  214. #endif
  215. default:
  216. return -1; /*call _dl_exit(1) */
  217. }
  218. #if defined (__SUPPORT_LD_DEBUG__)
  219. if (_dl_debug_reloc && _dl_debug_detail) {
  220. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  221. old_val, *reloc_addr, reloc_addr);
  222. }
  223. #endif
  224. return 0;
  225. }
  226. static int
  227. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  228. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  229. {
  230. int reloc_type;
  231. ElfW(Addr) *reloc_addr;
  232. #if defined (__SUPPORT_LD_DEBUG__)
  233. ElfW(Addr) old_val;
  234. #endif
  235. (void)scope;
  236. (void)strtab;
  237. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  238. reloc_type = ELF_R_TYPE(rpnt->r_info);
  239. #if defined (__SUPPORT_LD_DEBUG__)
  240. old_val = *reloc_addr;
  241. #endif
  242. switch (reloc_type) {
  243. case R_AARCH64_NONE:
  244. break;
  245. case R_AARCH64_JUMP_SLOT:
  246. *reloc_addr += tpnt->loadaddr;
  247. break;
  248. default:
  249. return -1; /*call _dl_exit(1) */
  250. }
  251. #if defined (__SUPPORT_LD_DEBUG__)
  252. if (_dl_debug_reloc && _dl_debug_detail) {
  253. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  254. old_val, *reloc_addr, reloc_addr);
  255. }
  256. #endif
  257. return 0;
  258. }
  259. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  260. unsigned long rel_addr, unsigned long rel_size)
  261. {
  262. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  263. }
  264. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  265. struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
  266. {
  267. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  268. }