elfinterp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #if defined (__SUPPORT_LD_DEBUG__)
  114. _dl_dprintf(2, "can't handle reloc type %s\n",
  115. _dl_reltypes(reloc_type));
  116. #else
  117. _dl_dprintf(2, "can't handle reloc type %x\n",
  118. reloc_type);
  119. #endif
  120. _dl_exit(-res);
  121. } else if (unlikely(res > 0)) {
  122. _dl_dprintf(2, "can't resolve symbol\n");
  123. return res;
  124. }
  125. }
  126. return 0;
  127. }
  128. static int
  129. _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  130. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  131. {
  132. int reloc_type;
  133. int symtab_index;
  134. char *symname;
  135. unsigned long *reloc_addr;
  136. unsigned long symbol_addr;
  137. #if defined (__SUPPORT_LD_DEBUG__)
  138. unsigned long old_val;
  139. #endif
  140. struct symbol_ref sym_ref;
  141. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  142. reloc_type = ELF_R_TYPE(rpnt->r_info);
  143. symtab_index = ELF_R_SYM(rpnt->r_info);
  144. symbol_addr = 0;
  145. sym_ref.sym = &symtab[symtab_index];
  146. sym_ref.tpnt = NULL;
  147. symname = strtab + symtab[symtab_index].st_name;
  148. if (symtab_index) {
  149. if (symtab[symtab_index].st_shndx != SHN_UNDEF &&
  150. ELF_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL) {
  151. symbol_addr = (unsigned long)tpnt->loadaddr;
  152. } else {
  153. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  154. elf_machine_type_class(reloc_type), &sym_ref);
  155. }
  156. if (unlikely(!symbol_addr && ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
  157. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  158. _dl_exit(1);
  159. }
  160. symbol_addr += rpnt->r_addend;
  161. if (_dl_trace_prelink) {
  162. _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
  163. &sym_ref, elf_machine_type_class(reloc_type));
  164. }
  165. }
  166. #if defined (__SUPPORT_LD_DEBUG__)
  167. old_val = *reloc_addr;
  168. #endif
  169. switch (reloc_type) {
  170. case R_CRIS_NONE:
  171. break;
  172. case R_CRIS_GLOB_DAT:
  173. case R_CRIS_JUMP_SLOT:
  174. case R_CRIS_32:
  175. *reloc_addr = symbol_addr;
  176. break;
  177. case R_CRIS_COPY:
  178. #if defined (__SUPPORT_LD_DEBUG__)
  179. if (_dl_debug_move)
  180. _dl_dprintf(_dl_debug_file,
  181. "\n%s move %d bytes from %x to %x",
  182. symname, symtab[symtab_index].st_size,
  183. symbol_addr, reloc_addr);
  184. #endif
  185. _dl_memcpy((char *)reloc_addr,
  186. (char *)symbol_addr,
  187. symtab[symtab_index].st_size);
  188. break;
  189. case R_CRIS_RELATIVE:
  190. *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
  191. break;
  192. default:
  193. return -1; /* Calls _dl_exit(1). */
  194. }
  195. #if defined (__SUPPORT_LD_DEBUG__)
  196. if (_dl_debug_reloc && _dl_debug_detail)
  197. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  198. old_val, *reloc_addr, reloc_addr);
  199. #endif
  200. return 0;
  201. }
  202. static int
  203. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
  204. ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
  205. {
  206. int reloc_type;
  207. unsigned long *reloc_addr;
  208. #if defined (__SUPPORT_LD_DEBUG__)
  209. unsigned long old_val;
  210. #endif
  211. /* Don't care about these, just keep the compiler happy. */
  212. (void)scope;
  213. (void)symtab;
  214. (void)strtab;
  215. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  216. reloc_type = ELF_R_TYPE(rpnt->r_info);
  217. #if defined (__SUPPORT_LD_DEBUG__)
  218. old_val = *reloc_addr;
  219. #endif
  220. switch (reloc_type) {
  221. case R_CRIS_NONE:
  222. break;
  223. case R_CRIS_JUMP_SLOT:
  224. *reloc_addr += (unsigned long)tpnt->loadaddr;
  225. break;
  226. default:
  227. return -1; /* Calls _dl_exit(1). */
  228. }
  229. #if defined (__SUPPORT_LD_DEBUG__)
  230. if (_dl_debug_reloc && _dl_debug_detail)
  231. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  232. old_val, *reloc_addr, reloc_addr);
  233. #endif
  234. return 0;
  235. }
  236. /* External interface to the generic part of the dynamic linker. */
  237. void
  238. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  239. unsigned long rel_addr,
  240. unsigned long rel_size)
  241. {
  242. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  243. }
  244. int
  245. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  246. struct r_scope_elem *scope,
  247. unsigned long rel_addr,
  248. unsigned long rel_size)
  249. {
  250. return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
  251. }