elfinterp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* vi: set sw=4 ts=4: */
  2. /* OpenRISC 1000 ELF shared library loader suppport
  3. *
  4. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  5. * David Engel, Hongjiu Lu and Mitch D'Souza
  6. * Copyright (C) 2001-2004 Erik Andersen
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include "ldso.h"
  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. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  36. I ever taken any courses on internals. This program was developed using
  37. information available through the book "UNIX SYSTEM V RELEASE 4,
  38. Programmers guide: Ansi C and Programming Support Tools", which did
  39. a more than adequate job of explaining everything required to get this
  40. working. */
  41. extern int _dl_linux_resolve(void);
  42. unsigned long
  43. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  44. {
  45. ELF_RELOC *this_reloc;
  46. char *strtab;
  47. ElfW(Sym) *symtab;
  48. int symtab_index;
  49. char *rel_addr;
  50. char *new_addr;
  51. char **got_addr;
  52. ElfW(Addr) instr_addr;
  53. char *symname;
  54. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  55. this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
  56. symtab_index = ELF_R_SYM(this_reloc->r_info);
  57. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  58. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  59. symname = strtab + symtab[symtab_index].st_name;
  60. /* Address of the jump instruction to fix up. */
  61. instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
  62. got_addr = (char **)instr_addr;
  63. /* Get the address of the GOT entry. */
  64. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  65. if (unlikely(!new_addr)) {
  66. _dl_dprintf(2, "%s: can't resolve symbol '%s' in lib '%s'.\n",
  67. _dl_progname, symname, tpnt->libname);
  68. _dl_exit(1);
  69. }
  70. #if defined (__SUPPORT_LD_DEBUG__)
  71. if ((unsigned long)got_addr < 0x40000000) {
  72. if (_dl_debug_bindings) {
  73. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  74. if (_dl_debug_detail)
  75. _dl_dprintf(_dl_debug_file,
  76. "\tpatched: %x ==> %x @ %x\n",
  77. *got_addr, new_addr, got_addr);
  78. }
  79. }
  80. if (!_dl_debug_nofixups)
  81. #endif
  82. *got_addr = new_addr;
  83. return (unsigned long)new_addr;
  84. }
  85. static int
  86. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  87. unsigned long rel_addr, unsigned long rel_size,
  88. int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  89. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  90. {
  91. unsigned int i;
  92. char *strtab;
  93. ElfW(Sym) *symtab;
  94. ELF_RELOC *rpnt;
  95. int symtab_index;
  96. /* Parse the relocation information. */
  97. rpnt = (ELF_RELOC *)rel_addr;
  98. rel_size /= sizeof(ELF_RELOC);
  99. symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
  100. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  101. for (i = 0; i < rel_size; i++, rpnt++) {
  102. int res;
  103. symtab_index = ELF_R_SYM(rpnt->r_info);
  104. debug_sym(symtab, strtab, symtab_index);
  105. debug_reloc(symtab, strtab, rpnt);
  106. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  107. if (res == 0)
  108. continue;
  109. _dl_dprintf(2, "\n%s: ", _dl_progname);
  110. if (symtab_index)
  111. _dl_dprintf(2, "symbol '%s': ",
  112. strtab + symtab[symtab_index].st_name);
  113. if (unlikely(res < 0)) {
  114. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  115. _dl_dprintf(2, "can't handle reloc type "
  116. #if defined (__SUPPORT_LD_DEBUG__)
  117. "%s\n", _dl_reltypes(reloc_type));
  118. #else
  119. "%x\n", reloc_type);
  120. #endif
  121. _dl_exit(-res);
  122. } else if (unlikely(res > 0)) {
  123. _dl_dprintf(2, "can't resolve symbol\n");
  124. return res;
  125. }
  126. }
  127. return 0;
  128. }
  129. static int
  130. _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  131. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  132. {
  133. int reloc_type;
  134. int symtab_index;
  135. char *symname;
  136. #if defined USE_TLS && USE_TLS
  137. struct elf_resolve *tls_tpnt;
  138. #endif
  139. struct symbol_ref sym_ref;
  140. ElfW(Addr) *reloc_addr;
  141. ElfW(Addr) symbol_addr;
  142. #if defined (__SUPPORT_LD_DEBUG__)
  143. ElfW(Addr) old_val;
  144. #endif
  145. struct unaligned {
  146. Elf32_Addr x;
  147. } __attribute__ ((packed, may_alias));
  148. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  149. reloc_type = ELF_R_TYPE(rpnt->r_info);
  150. symtab_index = ELF_R_SYM(rpnt->r_info);
  151. sym_ref.sym = &symtab[symtab_index];
  152. sym_ref.tpnt = NULL;
  153. symbol_addr = 0;
  154. symname = strtab + sym_ref.sym->st_name;
  155. if (symtab_index) {
  156. symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
  157. elf_machine_type_class(reloc_type), &sym_ref);
  158. /*
  159. * We want to allow undefined references to weak symbols - this
  160. * might have been intentional. We should not be linking local
  161. * symbols here, so all bases should be covered.
  162. */
  163. if (unlikely(!symbol_addr && (ELF_ST_TYPE(sym_ref.sym->st_info) != STT_TLS)
  164. && (ELF_ST_BIND(sym_ref.sym->st_info) != STB_WEAK))) {
  165. /* This may be non-fatal if called from dlopen. */
  166. return 1;
  167. }
  168. #if defined USE_TLS && USE_TLS
  169. tls_tpnt = sym_ref.tpnt;
  170. #endif
  171. } else {
  172. /* Relocs against STN_UNDEF are usually treated as using a
  173. * symbol value of zero, and using the module containing the
  174. * reloc itself. */
  175. symbol_addr = sym_ref.sym->st_value;
  176. #if defined USE_TLS && USE_TLS
  177. tls_tpnt = tpnt;
  178. #endif
  179. }
  180. #if defined (__SUPPORT_LD_DEBUG__)
  181. if (reloc_addr) {
  182. old_val = ((struct unaligned *)reloc_addr)->x;
  183. } else {
  184. old_val = 0;
  185. }
  186. #endif
  187. switch (reloc_type) {
  188. case R_OR1K_NONE:
  189. break;
  190. case R_OR1K_8:
  191. case R_OR1K_16:
  192. case R_OR1K_32:
  193. /* Support relocations on mis-aligned offsets. */
  194. ((struct unaligned *)reloc_addr)->x = symbol_addr +
  195. rpnt->r_addend;
  196. break;
  197. case R_OR1K_8_PCREL:
  198. case R_OR1K_16_PCREL:
  199. case R_OR1K_32_PCREL:
  200. case R_OR1K_INSN_REL_26:
  201. *reloc_addr = symbol_addr + rpnt->r_addend;
  202. break;
  203. case R_OR1K_GLOB_DAT:
  204. case R_OR1K_JMP_SLOT:
  205. *reloc_addr = symbol_addr + rpnt->r_addend;
  206. break;
  207. /* Handled by elf_machine_relative */
  208. case R_OR1K_RELATIVE:
  209. *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
  210. break;
  211. case R_OR1K_COPY:
  212. if (symbol_addr) {
  213. #if defined (__SUPPORT_LD_DEBUG__)
  214. if (_dl_debug_move)
  215. _dl_dprintf(_dl_debug_file,
  216. "\t%s move %d bytes from %x to %x\n",
  217. symname, sym_ref.sym->st_size,
  218. symbol_addr, reloc_addr);
  219. #endif
  220. _dl_memcpy((char *)reloc_addr,
  221. (char *)symbol_addr,
  222. sym_ref.sym->st_size);
  223. }
  224. #if defined (__SUPPORT_LD_DEBUG__)
  225. else
  226. _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
  227. #endif
  228. break;
  229. default:
  230. return -1; /* Calls _dl_exit(1). */
  231. }
  232. #if defined (__SUPPORT_LD_DEBUG__)
  233. if (_dl_debug_reloc && _dl_debug_detail)
  234. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  235. old_val, ((struct unaligned *)reloc_addr)->x,
  236. reloc_addr);
  237. #endif
  238. return 0;
  239. }
  240. static int
  241. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  242. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  243. {
  244. int reloc_type;
  245. int symtab_index;
  246. ElfW(Addr) *reloc_addr;
  247. #if defined (__SUPPORT_LD_DEBUG__)
  248. ElfW(Addr) old_val;
  249. #endif
  250. (void)scope;
  251. symtab_index = ELF_R_SYM(rpnt->r_info);
  252. (void)strtab;
  253. reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
  254. reloc_type = ELF_R_TYPE(rpnt->r_info);
  255. #if defined (__SUPPORT_LD_DEBUG__)
  256. old_val = *reloc_addr;
  257. #endif
  258. switch (reloc_type) {
  259. case R_OR1K_NONE:
  260. break;
  261. case R_OR1K_JMP_SLOT:
  262. *reloc_addr += (unsigned long)tpnt->loadaddr;
  263. break;
  264. default:
  265. _dl_exit(1);
  266. }
  267. #if defined (__SUPPORT_LD_DEBUG__)
  268. if (_dl_debug_reloc && _dl_debug_detail)
  269. _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
  270. old_val, *reloc_addr, reloc_addr);
  271. #endif
  272. return 0;
  273. }
  274. void
  275. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  276. unsigned long rel_addr, unsigned long rel_size)
  277. {
  278. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  279. }
  280. int
  281. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  282. struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
  283. {
  284. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  285. }