elfinterp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * CRIS ELF shared library loader support.
  3. *
  4. * Program to load an elf binary on a linux system, and run it.
  5. * References to symbols in sharable libraries can be resolved
  6. * by either an ELF sharable library or a linux style of shared
  7. * library.
  8. *
  9. * Copyright (C) 2002-2004, Axis Communications AB
  10. * All rights reserved
  11. *
  12. * Author: Tobias Anderberg, <tobiasa@axis.com>
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. The name of the above contributors may not be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. */
  35. #include "ldso.h"
  36. /* Defined in resolve.S. */
  37. extern int _dl_linux_resolve(void);
  38. unsigned long
  39. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  40. {
  41. int symtab_index;
  42. char *strtab;
  43. char *symname;
  44. char *new_addr;
  45. char *rel_addr;
  46. char **got_addr;
  47. ElfW(Sym) *symtab;
  48. ELF_RELOC *this_reloc;
  49. unsigned long instr_addr;
  50. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  51. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  52. symtab_index = ELF_R_SYM(this_reloc->r_info);
  53. symtab = (ElfW(Sym) *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  54. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  55. symname = strtab + symtab[symtab_index].st_name;
  56. /* Address of the jump instruction to fix up. */
  57. instr_addr = ((unsigned long)this_reloc->r_offset +
  58. (unsigned long)tpnt->loadaddr);
  59. got_addr = (char **)instr_addr;
  60. /* Get the address of the GOT entry. */
  61. new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
  62. if (unlikely(!new_addr)) {
  63. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  64. _dl_exit(1);
  65. }
  66. #if defined (__SUPPORT_LD_DEBUG__)
  67. if (_dl_debug_bindings) {
  68. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  69. if (_dl_debug_detail)
  70. _dl_dprintf(_dl_debug_file,
  71. "\n\tpatched: %x ==> %x @ %x\n",
  72. *got_addr, new_addr, got_addr);
  73. }
  74. if (!_dl_debug_nofixups) {
  75. *got_addr = new_addr;
  76. }
  77. #else
  78. *got_addr = new_addr;
  79. #endif
  80. return (unsigned long)new_addr;
  81. }
  82. static int
  83. _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  84. unsigned long rel_addr, unsigned long rel_size,
  85. int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  86. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
  87. {
  88. int symtab_index;
  89. unsigned int i;
  90. char *strtab;
  91. ElfW(Sym) *symtab;
  92. ELF_RELOC *rpnt;
  93. /* Parse the relocation information. */
  94. rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
  95. rel_size /= sizeof(ELF_RELOC);
  96. symtab = (ElfW(Sym) *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  97. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  98. for (i = 0; i < rel_size; i++, rpnt++) {
  99. int res;
  100. symtab_index = ELF_R_SYM(rpnt->r_info);
  101. debug_sym(symtab, strtab, symtab_index);
  102. debug_reloc(symtab, strtab, rpnt);
  103. /* Pass over to actual relocation function. */
  104. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  105. if (res == 0)
  106. continue;
  107. _dl_dprintf(2, "\n%s: ", _dl_progname);
  108. if (symtab_index)
  109. _dl_dprintf(2, "symbol '%s': ",
  110. strtab + symtab[symtab_index].st_name);
  111. if (unlikely(res < 0)) {
  112. int reloc_type = ELF_R_TYPE(rpnt->r_info);
  113. _dl_dprintf(2, "can't handle reloc type %x\n",
  114. reloc_type);
  115. _dl_exit(-res);
  116. } else if (unlikely(res > 0)) {
  117. _dl_dprintf(2, "can't resolve symbol\n");
  118. return res;
  119. }
  120. }
  121. return 0;
  122. }
  123. static int
  124. _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  125. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  126. {
  127. int reloc_type;
  128. int symtab_index;
  129. char *symname;
  130. unsigned long *reloc_addr;
  131. unsigned long symbol_addr;
  132. #if defined (__SUPPORT_LD_DEBUG__)
  133. unsigned long old_val;
  134. #endif
  135. struct symbol_ref sym_ref;
  136. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  137. reloc_type = ELF_R_TYPE(rpnt->r_info);
  138. symtab_index = ELF_R_SYM(rpnt->r_info);
  139. symbol_addr = 0;
  140. sym_ref.sym = &symtab[symtab_index];
  141. sym_ref.tpnt = NULL;
  142. symname = strtab + symtab[symtab_index].st_name;
  143. if (symtab_index) {
  144. if (symtab[symtab_index].st_shndx != SHN_UNDEF &&
  145. ELF_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL) {
  146. symbol_addr = (unsigned long)tpnt->loadaddr;
  147. } else {
  148. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  149. elf_machine_type_class(reloc_type), &sym_ref);
  150. }
  151. if (unlikely(!symbol_addr && ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
  152. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  153. _dl_exit(1);
  154. }
  155. symbol_addr += rpnt->r_addend;
  156. if (_dl_trace_prelink) {
  157. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  158. &sym_ref, elf_machine_type_class(reloc_type));
  159. }
  160. }
  161. #if defined (__SUPPORT_LD_DEBUG__)
  162. old_val = *reloc_addr;
  163. #endif
  164. switch (reloc_type) {
  165. case R_CRIS_NONE:
  166. break;
  167. case R_CRIS_GLOB_DAT:
  168. case R_CRIS_JUMP_SLOT:
  169. case R_CRIS_32:
  170. *reloc_addr = symbol_addr;
  171. break;
  172. case R_CRIS_COPY:
  173. #if defined (__SUPPORT_LD_DEBUG__)
  174. if (_dl_debug_move)
  175. _dl_dprintf(_dl_debug_file,
  176. "\n%s move %d bytes from %x to %x",
  177. symname, symtab[symtab_index].st_size,
  178. symbol_addr, reloc_addr);
  179. #endif
  180. _dl_memcpy((char *)reloc_addr,
  181. (char *)symbol_addr,
  182. symtab[symtab_index].st_size);
  183. break;
  184. case R_CRIS_RELATIVE:
  185. *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
  186. break;
  187. default:
  188. return -1; /* Calls _dl_exit(1). */
  189. }
  190. #if defined (__SUPPORT_LD_DEBUG__)
  191. if (_dl_debug_reloc && _dl_debug_detail)
  192. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  193. old_val, *reloc_addr, reloc_addr);
  194. #endif
  195. return 0;
  196. }
  197. static int
  198. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  199. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  200. {
  201. int reloc_type;
  202. unsigned long *reloc_addr;
  203. #if defined (__SUPPORT_LD_DEBUG__)
  204. unsigned long old_val;
  205. #endif
  206. /* Don't care about these, just keep the compiler happy. */
  207. (void)scope;
  208. (void)symtab;
  209. (void)strtab;
  210. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  211. reloc_type = ELF_R_TYPE(rpnt->r_info);
  212. #if defined (__SUPPORT_LD_DEBUG__)
  213. old_val = *reloc_addr;
  214. #endif
  215. switch (reloc_type) {
  216. case R_CRIS_NONE:
  217. break;
  218. case R_CRIS_JUMP_SLOT:
  219. *reloc_addr += (unsigned long)tpnt->loadaddr;
  220. break;
  221. default:
  222. return -1; /* Calls _dl_exit(1). */
  223. }
  224. #if defined (__SUPPORT_LD_DEBUG__)
  225. if (_dl_debug_reloc && _dl_debug_detail)
  226. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  227. old_val, *reloc_addr, reloc_addr);
  228. #endif
  229. return 0;
  230. }
  231. /* External interface to the generic part of the dynamic linker. */
  232. void
  233. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  234. unsigned long rel_addr,
  235. unsigned long rel_size)
  236. {
  237. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  238. }
  239. int
  240. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  241. struct r_scope_elem *scope,
  242. unsigned long rel_addr,
  243. unsigned long rel_size)
  244. {
  245. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  246. }