elfinterp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 reloc_type;
  42. int symtab_index;
  43. char *strtab;
  44. char *symname;
  45. char *new_addr;
  46. char *rel_addr;
  47. char **got_addr;
  48. Elf32_Sym *symtab;
  49. ELF_RELOC *this_reloc;
  50. unsigned long instr_addr;
  51. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  52. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  53. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  54. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  55. symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  56. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  57. symname = strtab + symtab[symtab_index].st_name;
  58. if (unlikely(reloc_type != R_CRIS_JUMP_SLOT)) {
  59. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  60. _dl_progname);
  61. _dl_exit(1);
  62. }
  63. /* Address of the jump instruction to fix up. */
  64. instr_addr = ((unsigned long)this_reloc->r_offset +
  65. (unsigned long)tpnt->loadaddr);
  66. got_addr = (char **)instr_addr;
  67. /* Get the address of the GOT entry. */
  68. new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  69. if (unlikely(!new_addr)) {
  70. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  71. _dl_exit(1);
  72. }
  73. #if defined (__SUPPORT_LD_DEBUG__)
  74. if (_dl_debug_bindings) {
  75. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  76. if (_dl_debug_detail)
  77. _dl_dprintf(_dl_debug_file,
  78. "\n\tpatched: %x ==> %x @ %x",
  79. *got_addr, new_addr, got_addr);
  80. }
  81. if (!_dl_debug_nofixups) {
  82. *got_addr = new_addr;
  83. }
  84. #else
  85. *got_addr = new_addr;
  86. #endif
  87. return (unsigned long)new_addr;
  88. }
  89. static int
  90. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  91. unsigned long rel_addr, unsigned long rel_size,
  92. int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
  93. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  94. {
  95. int symtab_index;
  96. unsigned int i;
  97. char *strtab;
  98. Elf32_Sym *symtab;
  99. ELF_RELOC *rpnt;
  100. /* Parse the relocation information. */
  101. rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
  102. rel_size /= sizeof(ELF_RELOC);
  103. symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  104. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  105. for (i = 0; i < rel_size; i++, rpnt++) {
  106. int res;
  107. symtab_index = ELF32_R_SYM(rpnt->r_info);
  108. #if defined (__SUPPORT_LD_DEBUG__)
  109. debug_sym(symtab, strtab, symtab_index);
  110. debug_reloc(symtab, strtab, rpnt);
  111. #endif
  112. /* Pass over to actual relocation function. */
  113. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  114. if (res == 0)
  115. continue;
  116. _dl_dprintf(2, "\n%s: ", _dl_progname);
  117. if (symtab_index)
  118. _dl_dprintf(2, "symbol '%s': ",
  119. strtab + symtab[symtab_index].st_name);
  120. if (unlikely(res < 0)) {
  121. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  122. #if defined (__SUPPORT_LD_DEBUG__)
  123. _dl_dprintf(2, "can't handle reloc type %s\n",
  124. _dl_reltypes(reloc_type));
  125. #else
  126. _dl_dprintf(2, "can't handle reloc type %x\n",
  127. reloc_type);
  128. #endif
  129. _dl_exit(-res);
  130. } else if (unlikely(res > 0)) {
  131. _dl_dprintf(2, "can't resolve symbol\n");
  132. return res;
  133. }
  134. }
  135. return 0;
  136. }
  137. static int
  138. _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  139. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  140. {
  141. int reloc_type;
  142. int symtab_index;
  143. char *symname;
  144. unsigned long *reloc_addr;
  145. unsigned long symbol_addr;
  146. #if defined (__SUPPORT_LD_DEBUG__)
  147. unsigned long old_val;
  148. #endif
  149. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  150. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  151. symtab_index = ELF32_R_SYM(rpnt->r_info);
  152. symbol_addr = 0;
  153. symname = strtab + symtab[symtab_index].st_name;
  154. if (symtab_index) {
  155. if (symtab[symtab_index].st_shndx != SHN_UNDEF &&
  156. ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL) {
  157. symbol_addr = (unsigned long)tpnt->loadaddr;
  158. } else {
  159. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  160. elf_machine_type_class(reloc_type));
  161. }
  162. if (unlikely(!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
  163. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  164. _dl_exit(1);
  165. };
  166. symbol_addr += rpnt->r_addend;
  167. }
  168. #if defined (__SUPPORT_LD_DEBUG__)
  169. old_val = *reloc_addr;
  170. #endif
  171. switch (reloc_type) {
  172. case R_CRIS_NONE:
  173. break;
  174. case R_CRIS_GLOB_DAT:
  175. case R_CRIS_JUMP_SLOT:
  176. case R_CRIS_32:
  177. *reloc_addr = symbol_addr;
  178. break;
  179. case R_CRIS_COPY:
  180. #if defined (__SUPPORT_LD_DEBUG__)
  181. if (_dl_debug_move)
  182. _dl_dprintf(_dl_debug_file,
  183. "\n%s move %d bytes from %x to %x",
  184. symname, symtab[symtab_index].st_size,
  185. symbol_addr, reloc_addr);
  186. #endif
  187. _dl_memcpy((char *)reloc_addr,
  188. (char *)symbol_addr,
  189. symtab[symtab_index].st_size);
  190. break;
  191. case R_CRIS_RELATIVE:
  192. *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
  193. break;
  194. default:
  195. return -1; /* Calls _dl_exit(1). */
  196. }
  197. #if defined (__SUPPORT_LD_DEBUG__)
  198. if (_dl_debug_reloc && _dl_debug_detail)
  199. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x",
  200. old_val, *reloc_addr, reloc_addr);
  201. #endif
  202. return 0;
  203. }
  204. static int
  205. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  206. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  207. {
  208. int reloc_type;
  209. unsigned long *reloc_addr;
  210. #if defined (__SUPPORT_LD_DEBUG__)
  211. unsigned long old_val;
  212. #endif
  213. /* Don't care about these, just keep the compiler happy. */
  214. (void)scope;
  215. (void)symtab;
  216. (void)strtab;
  217. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  218. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  219. #if defined (__SUPPORT_LD_DEBUG__)
  220. old_val = *reloc_addr;
  221. #endif
  222. switch (reloc_type) {
  223. case R_CRIS_NONE:
  224. break;
  225. case R_CRIS_JUMP_SLOT:
  226. *reloc_addr += (unsigned long)tpnt->loadaddr;
  227. break;
  228. default:
  229. return -1; /* Calls _dl_exit(1). */
  230. }
  231. #if defined (__SUPPORT_LD_DEBUG__)
  232. if (_dl_debug_reloc && _dl_debug_detail)
  233. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x",
  234. old_val, *reloc_addr, reloc_addr);
  235. #endif
  236. return 0;
  237. }
  238. /* External interface to the generic part of the dynamic linker. */
  239. void
  240. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  241. unsigned long rel_addr,
  242. unsigned long rel_size)
  243. {
  244. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  245. }
  246. int
  247. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  248. unsigned long rel_addr,
  249. unsigned long rel_size)
  250. {
  251. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  252. }