elfinterp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. Elf32_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 = ELF32_R_SYM(this_reloc->r_info);
  53. symtab = (Elf32_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, tpnt->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 dyn_elf *scope,
  84. unsigned long rel_addr, unsigned long rel_size,
  85. int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
  86. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  87. {
  88. int symtab_index;
  89. unsigned int i;
  90. char *strtab;
  91. Elf32_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 = (Elf32_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 = ELF32_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 = ELF32_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 dyn_elf *scope,
  130. ELF_RELOC *rpnt, Elf32_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 = ELF32_R_TYPE(rpnt->r_info);
  143. symtab_index = ELF32_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. ELF32_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 && ELF32_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. }
  162. #if defined (__SUPPORT_LD_DEBUG__)
  163. old_val = *reloc_addr;
  164. #endif
  165. switch (reloc_type) {
  166. case R_CRIS_NONE:
  167. break;
  168. case R_CRIS_GLOB_DAT:
  169. case R_CRIS_JUMP_SLOT:
  170. case R_CRIS_32:
  171. *reloc_addr = symbol_addr;
  172. break;
  173. case R_CRIS_COPY:
  174. #if defined (__SUPPORT_LD_DEBUG__)
  175. if (_dl_debug_move)
  176. _dl_dprintf(_dl_debug_file,
  177. "\n%s move %d bytes from %x to %x",
  178. symname, symtab[symtab_index].st_size,
  179. symbol_addr, reloc_addr);
  180. #endif
  181. _dl_memcpy((char *)reloc_addr,
  182. (char *)symbol_addr,
  183. symtab[symtab_index].st_size);
  184. break;
  185. case R_CRIS_RELATIVE:
  186. *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
  187. break;
  188. default:
  189. return -1; /* Calls _dl_exit(1). */
  190. }
  191. #if defined (__SUPPORT_LD_DEBUG__)
  192. if (_dl_debug_reloc && _dl_debug_detail)
  193. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  194. old_val, *reloc_addr, reloc_addr);
  195. #endif
  196. return 0;
  197. }
  198. static int
  199. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  200. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  201. {
  202. int reloc_type;
  203. unsigned long *reloc_addr;
  204. #if defined (__SUPPORT_LD_DEBUG__)
  205. unsigned long old_val;
  206. #endif
  207. /* Don't care about these, just keep the compiler happy. */
  208. (void)scope;
  209. (void)symtab;
  210. (void)strtab;
  211. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  212. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  213. #if defined (__SUPPORT_LD_DEBUG__)
  214. old_val = *reloc_addr;
  215. #endif
  216. switch (reloc_type) {
  217. case R_CRIS_NONE:
  218. break;
  219. case R_CRIS_JUMP_SLOT:
  220. *reloc_addr += (unsigned long)tpnt->loadaddr;
  221. break;
  222. default:
  223. return -1; /* Calls _dl_exit(1). */
  224. }
  225. #if defined (__SUPPORT_LD_DEBUG__)
  226. if (_dl_debug_reloc && _dl_debug_detail)
  227. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
  228. old_val, *reloc_addr, reloc_addr);
  229. #endif
  230. return 0;
  231. }
  232. /* External interface to the generic part of the dynamic linker. */
  233. void
  234. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  235. unsigned long rel_addr,
  236. unsigned long rel_size)
  237. {
  238. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  239. }
  240. int
  241. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  242. unsigned long rel_addr,
  243. unsigned long rel_size)
  244. {
  245. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  246. }