elfinterp.c 7.8 KB

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