elfinterp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2016 Andes Technology, Inc.
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* NDS32 ELF shared library loader suppport
  6. *
  7. * Copyright (C) 2001-2004 Erik Andersen
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. The name of the above contributors may not be
  17. * used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. /* Program to load an ELF binary on a linux system, and run it.
  33. References to symbols in sharable libraries can be resolved by either
  34. an ELF sharable library or a linux style of shared library. */
  35. #include "ldso.h"
  36. #if defined(USE_TLS) && USE_TLS
  37. #include "dl-tls.h"
  38. #include "tlsdeschtab.h"
  39. #endif
  40. extern int _dl_linux_resolve(void);
  41. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  42. {
  43. int reloc_type;
  44. ELF_RELOC *this_reloc;
  45. char *strtab;
  46. char *symname;
  47. Elf32_Sym *symtab;
  48. ELF_RELOC *rel_addr;
  49. int symtab_index;
  50. char *new_addr;
  51. char **got_addr;
  52. unsigned long instr_addr;
  53. rel_addr = (ELF_RELOC *) tpnt->dynamic_info[DT_JMPREL];
  54. this_reloc = rel_addr + reloc_entry/sizeof(ELF_RELOC);
  55. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  56. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  57. symtab = (Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB];
  58. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  59. symname = strtab + symtab[symtab_index].st_name;
  60. if (unlikely(reloc_type != R_NDS32_JMP_SLOT)) {
  61. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  62. _dl_progname);
  63. _dl_exit(1);
  64. }
  65. /* Address of jump instruction to fix up */
  66. instr_addr = ((unsigned long) this_reloc->r_offset +
  67. (unsigned long) tpnt->loadaddr);
  68. got_addr = (char **) instr_addr;
  69. /* Get the address of the GOT entry */
  70. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt,
  71. ELF_RTYPE_CLASS_PLT, NULL);
  72. if (unlikely(!new_addr)) {
  73. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
  74. _dl_progname, symname);
  75. _dl_exit(1);
  76. }
  77. #if defined (__SUPPORT_LD_DEBUG__)
  78. if ((unsigned long) got_addr < 0x40000000)
  79. {
  80. if (_dl_debug_bindings)
  81. {
  82. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  83. if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  84. "\tpatch %x ==> %x @ %x", (unsigned int)*got_addr, (unsigned int)new_addr, (unsigned int)got_addr);
  85. }
  86. }
  87. if (!_dl_debug_nofixups) {
  88. *got_addr = new_addr;
  89. }
  90. #else
  91. *got_addr = new_addr;
  92. #endif
  93. return (unsigned long) new_addr;
  94. }
  95. static int
  96. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  97. unsigned long rel_addr, unsigned long rel_size,
  98. int (*reloc_fnc) (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  99. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  100. {
  101. int symtab_index;
  102. int i;
  103. char *strtab;
  104. int goof = 0;
  105. ElfW(Sym) *symtab;
  106. ELF_RELOC *rpnt;
  107. /* Now parse the relocation information */
  108. rpnt = (ELF_RELOC *) rel_addr;
  109. rel_size = rel_size / sizeof(ELF_RELOC);
  110. symtab = (Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB];
  111. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  112. for (i = 0; i < rel_size; i++, rpnt++) {
  113. int res;
  114. symtab_index = ELF32_R_SYM(rpnt->r_info);
  115. debug_sym(symtab,strtab,symtab_index);
  116. debug_reloc(symtab,strtab,rpnt);
  117. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  118. if (res==0) continue;
  119. _dl_dprintf(2, "\n%s: ",_dl_progname);
  120. if (symtab_index)
  121. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  122. if (unlikely(res <0))
  123. {
  124. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  125. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  126. _dl_exit(-res);
  127. }
  128. if (unlikely(res >0))
  129. {
  130. _dl_dprintf(2, "can't resolve symbol\n");
  131. goof += res;
  132. }
  133. }
  134. return goof;
  135. }
  136. static int
  137. _dl_do_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  138. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  139. {
  140. int reloc_type;
  141. int symtab_index;
  142. char *symname = NULL;
  143. #if defined USE_TLS && USE_TLS
  144. struct elf_resolve *tls_tpnt = NULL;
  145. #endif
  146. unsigned long *reloc_addr;
  147. unsigned long symbol_addr;
  148. int goof = 0;
  149. struct symbol_ref sym_ref;
  150. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  151. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  152. symtab_index = ELF32_R_SYM(rpnt->r_info);
  153. symbol_addr = 0;
  154. sym_ref.sym = &symtab[symtab_index];
  155. sym_ref.tpnt = NULL;
  156. if (symtab_index) {
  157. symname = strtab + symtab[symtab_index].st_name;
  158. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  159. elf_machine_type_class(reloc_type), &sym_ref);
  160. /*
  161. * We want to allow undefined references to weak symbols - this might
  162. * have been intentional. We should not be linking local symbols
  163. * here, so all bases should be covered.
  164. */
  165. if (!symbol_addr
  166. && (ELF32_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
  167. && (ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
  168. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  169. _dl_progname, symname);
  170. _dl_exit (1);
  171. }
  172. if (_dl_trace_prelink) {
  173. _dl_debug_lookup(symname, tpnt, &symtab[symtab_index],
  174. &sym_ref, elf_machine_type_class(reloc_type));
  175. }
  176. #if defined USE_TLS && USE_TLS
  177. tls_tpnt = sym_ref.tpnt;
  178. #endif
  179. }
  180. #if defined USE_TLS && USE_TLS
  181. /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
  182. symbol. This is the case for a static tls variable, so the lookup
  183. module is just that one is referencing the tls variable. */
  184. if (!tls_tpnt)
  185. tls_tpnt = tpnt;
  186. #endif
  187. #define COPY_UNALIGNED_WORD(swp, twp) \
  188. { \
  189. __typeof (swp) __tmp = __builtin_nds32_unaligned_load_w ((unsigned int*)&swp); \
  190. __builtin_nds32_unaligned_store_w ((unsigned int *)twp, __tmp); \
  191. }
  192. #if defined (__SUPPORT_LD_DEBUG__)
  193. {
  194. unsigned long old_val = 0;
  195. if(reloc_type != R_NDS32_NONE)
  196. old_val = *reloc_addr;
  197. #endif
  198. symbol_addr += rpnt->r_addend ;
  199. switch (reloc_type) {
  200. case R_NDS32_NONE:
  201. break;
  202. case R_NDS32_32:
  203. case R_NDS32_GLOB_DAT:
  204. case R_NDS32_JMP_SLOT:
  205. *reloc_addr = symbol_addr;
  206. break;
  207. case R_NDS32_32_RELA:
  208. COPY_UNALIGNED_WORD (symbol_addr, reloc_addr);
  209. break;
  210. #undef COPY_UNALIGNED_WORD
  211. case R_NDS32_RELATIVE:
  212. *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
  213. break;
  214. case R_NDS32_COPY:
  215. _dl_memcpy((void *) reloc_addr,
  216. (void *) symbol_addr, symtab[symtab_index].st_size);
  217. break;
  218. #if defined USE_TLS && USE_TLS
  219. case R_NDS32_TLS_TPOFF:
  220. CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
  221. *reloc_addr = (symbol_addr + tls_tpnt->l_tls_offset);
  222. break;
  223. case R_NDS32_TLS_DESC:
  224. {
  225. struct tlsdesc volatile *td =
  226. (struct tlsdesc volatile *)reloc_addr;
  227. #ifndef SHARED
  228. CHECK_STATIC_TLS((struct link_map *) tls_tpnt);
  229. #else
  230. if (!TRY_STATIC_TLS ((struct link_map *) tls_tpnt))
  231. {
  232. td->argument.pointer = _dl_make_tlsdesc_dynamic((struct link_map *) tls_tpnt, symbol_addr);
  233. td->entry = _dl_tlsdesc_dynamic;
  234. }
  235. else
  236. #endif
  237. {
  238. td->argument.value = symbol_addr + tls_tpnt->l_tls_offset;
  239. td->entry = _dl_tlsdesc_return;
  240. }
  241. }
  242. break;
  243. #endif
  244. default:
  245. return -1; /*call _dl_exit(1) */
  246. }
  247. #if defined (__SUPPORT_LD_DEBUG__)
  248. if (_dl_debug_reloc && _dl_debug_detail)
  249. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", (unsigned int)old_val, (unsigned int)*reloc_addr, (unsigned int)reloc_addr);
  250. }
  251. #endif
  252. return goof;
  253. }
  254. static int
  255. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
  256. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  257. {
  258. int reloc_type;
  259. unsigned long *reloc_addr;
  260. reloc_addr = (unsigned long *) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  261. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  262. #if defined (__SUPPORT_LD_DEBUG__)
  263. {
  264. unsigned long old_val = *reloc_addr;
  265. #endif
  266. switch (reloc_type) {
  267. case R_NDS32_NONE:
  268. break;
  269. case R_NDS32_JMP_SLOT:
  270. *reloc_addr += (unsigned long) tpnt->loadaddr;
  271. break;
  272. default:
  273. return -1; /*call _dl_exit(1) */
  274. }
  275. #if defined (__SUPPORT_LD_DEBUG__)
  276. if (_dl_debug_reloc && _dl_debug_detail)
  277. _dl_dprintf(_dl_debug_file, "\tpatch: %x ==> %x @ %x", (unsigned int)old_val, (unsigned int)*reloc_addr, (unsigned int)reloc_addr);
  278. }
  279. #endif
  280. return 0;
  281. }
  282. void
  283. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  284. unsigned long rel_addr,
  285. unsigned long rel_size)
  286. {
  287. _dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  288. }
  289. int
  290. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  291. struct r_scope_elem *scope,
  292. unsigned long rel_addr,
  293. unsigned long rel_size)
  294. {
  295. return _dl_parse(rpnt->dyn, scope, rel_addr,
  296. rel_size, _dl_do_reloc);
  297. }