elfinterp.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* vi: set sw=4 ts=4: */
  2. /* Xtensa ELF shared library loader suppport
  3. *
  4. * Copyright (C) 2007 Tensilica Inc.
  5. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  6. * David Engel, Hongjiu Lu and Mitch D'Souza
  7. * Copyright (C) 2001-2004 Erik Andersen
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. The name of the above contributors may not be
  17. * used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #include "ldso.h"
  33. unsigned long
  34. _dl_linux_resolver (struct elf_resolve *tpnt, int reloc_entry)
  35. {
  36. ELF_RELOC *this_reloc;
  37. char *strtab;
  38. Elf32_Sym *symtab;
  39. int symtab_index;
  40. char *rel_addr;
  41. char *new_addr;
  42. char **got_addr;
  43. char *symname;
  44. rel_addr = (char *) tpnt->dynamic_info[DT_JMPREL];
  45. this_reloc = (ELF_RELOC *) (rel_addr + reloc_entry);
  46. symtab_index = ELF32_R_SYM (this_reloc->r_info);
  47. symtab = (Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB];
  48. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  49. symname = strtab + symtab[symtab_index].st_name;
  50. /* Address of the literal to fix up. */
  51. got_addr = (char **) (this_reloc->r_offset + tpnt->loadaddr);
  52. /* Get the address of the GOT entry. */
  53. new_addr = _dl_find_hash (symname, tpnt->symbol_scope, tpnt,
  54. ELF_RTYPE_CLASS_PLT, NULL);
  55. if (unlikely (!new_addr)) {
  56. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  57. _dl_progname, symname);
  58. _dl_exit (1);
  59. }
  60. #if defined (__SUPPORT_LD_DEBUG__)
  61. if (_dl_debug_bindings) {
  62. _dl_dprintf (_dl_debug_file, "\nresolve function: %s", symname);
  63. if (_dl_debug_detail)
  64. _dl_dprintf (_dl_debug_file, "\n\tpatched %x ==> %x @ %x\n",
  65. *got_addr, new_addr, got_addr);
  66. }
  67. if (!_dl_debug_nofixups)
  68. *got_addr = new_addr;
  69. #else
  70. *got_addr = new_addr;
  71. #endif
  72. return (unsigned long) new_addr;
  73. }
  74. static int
  75. _dl_parse (struct elf_resolve *tpnt, struct dyn_elf *scope,
  76. unsigned long rel_addr, unsigned long rel_size,
  77. int (*reloc_fnc) (struct elf_resolve *tpnt, struct dyn_elf *scope,
  78. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  79. {
  80. unsigned int i;
  81. char *strtab;
  82. Elf32_Sym *symtab;
  83. ELF_RELOC *rpnt;
  84. int symtab_index;
  85. /* Parse the relocation information. */
  86. rpnt = (ELF_RELOC *) rel_addr;
  87. rel_size /= sizeof (ELF_RELOC);
  88. symtab = (Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB];
  89. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  90. for (i = 0; i < rel_size; i++, rpnt++) {
  91. int res;
  92. symtab_index = ELF32_R_SYM (rpnt->r_info);
  93. debug_sym (symtab, strtab, symtab_index);
  94. debug_reloc (symtab, strtab, rpnt);
  95. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  96. if (res == 0)
  97. continue;
  98. _dl_dprintf (2, "\n%s: ", _dl_progname);
  99. if (symtab_index)
  100. _dl_dprintf (2, "symbol '%s': ",
  101. strtab + symtab[symtab_index].st_name);
  102. if (unlikely (res < 0)) {
  103. int reloc_type = ELF32_R_TYPE (rpnt->r_info);
  104. #if defined (__SUPPORT_LD_DEBUG__)
  105. _dl_dprintf (2, "can't handle reloc type %s\n",
  106. _dl_reltypes (reloc_type));
  107. #else
  108. _dl_dprintf (2, "can't handle reloc type %x\n", reloc_type);
  109. #endif
  110. _dl_exit (-res);
  111. }
  112. if (unlikely (res > 0)) {
  113. _dl_dprintf (2, "can't resolve symbol\n");
  114. return res;
  115. }
  116. }
  117. return 0;
  118. }
  119. static int
  120. _dl_do_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
  121. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  122. {
  123. int reloc_type;
  124. int symtab_index;
  125. char *symname;
  126. struct symbol_ref sym_ref;
  127. Elf32_Addr *reloc_addr;
  128. Elf32_Addr symbol_addr;
  129. #if defined (__SUPPORT_LD_DEBUG__)
  130. Elf32_Addr old_val;
  131. #endif
  132. reloc_addr = (Elf32_Addr *) (tpnt->loadaddr + rpnt->r_offset);
  133. reloc_type = ELF32_R_TYPE (rpnt->r_info);
  134. symtab_index = ELF32_R_SYM (rpnt->r_info);
  135. sym_ref.sym = &symtab[symtab_index];
  136. sym_ref.tpnt = NULL;
  137. symbol_addr = 0;
  138. symname = strtab + sym_ref.sym->st_name;
  139. if (symtab_index) {
  140. symbol_addr = (Elf32_Addr)
  141. _dl_find_hash (symname, scope, tpnt,
  142. elf_machine_type_class (reloc_type), &sym_ref);
  143. /*
  144. * We want to allow undefined references to weak symbols - this might
  145. * have been intentional. We should not be linking local symbols
  146. * here, so all bases should be covered.
  147. */
  148. if (unlikely (!symbol_addr &&
  149. ELF32_ST_BIND (sym_ref.sym->st_info) != STB_WEAK)) {
  150. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  151. _dl_progname, symname);
  152. _dl_exit (1);
  153. }
  154. }
  155. #if defined (__SUPPORT_LD_DEBUG__)
  156. old_val = *reloc_addr;
  157. #endif
  158. switch (reloc_type) {
  159. case R_XTENSA_NONE:
  160. break;
  161. case R_XTENSA_GLOB_DAT:
  162. case R_XTENSA_JMP_SLOT:
  163. *reloc_addr = symbol_addr + rpnt->r_addend;
  164. break;
  165. case R_XTENSA_RTLD:
  166. if (rpnt->r_addend == 1) {
  167. /* Grab the function pointer stashed at the beginning of the
  168. GOT by the GOT_INIT function. */
  169. *reloc_addr = *(Elf32_Addr *) tpnt->dynamic_info[DT_PLTGOT];
  170. } else if (rpnt->r_addend == 2) {
  171. /* Store the link map for the object. */
  172. *reloc_addr = (Elf32_Addr) tpnt;
  173. } else {
  174. _dl_exit (1);
  175. }
  176. break;
  177. case R_XTENSA_RELATIVE:
  178. *reloc_addr += tpnt->loadaddr + rpnt->r_addend;
  179. break;
  180. default:
  181. return -1; /* Calls _dl_exit(1). */
  182. }
  183. #if defined (__SUPPORT_LD_DEBUG__)
  184. if (_dl_debug_reloc && _dl_debug_detail)
  185. _dl_dprintf (_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  186. old_val, *reloc_addr, reloc_addr);
  187. #endif
  188. return 0;
  189. }
  190. static int
  191. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
  192. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  193. {
  194. int reloc_type;
  195. Elf32_Addr *reloc_addr;
  196. #if defined (__SUPPORT_LD_DEBUG__)
  197. Elf32_Addr old_val;
  198. #endif
  199. reloc_addr = (Elf32_Addr *) (tpnt->loadaddr + rpnt->r_offset);
  200. reloc_type = ELF32_R_TYPE (rpnt->r_info);
  201. #if defined (__SUPPORT_LD_DEBUG__)
  202. old_val = *reloc_addr;
  203. #endif
  204. switch (reloc_type) {
  205. case R_XTENSA_JMP_SLOT:
  206. /* Perform a RELATIVE reloc on the GOT entry that transfers
  207. to the stub function. */
  208. *reloc_addr += tpnt->loadaddr;
  209. break;
  210. case R_XTENSA_NONE:
  211. break;
  212. default:
  213. _dl_exit (1);
  214. }
  215. #if defined (__SUPPORT_LD_DEBUG__)
  216. if (_dl_debug_reloc && _dl_debug_detail)
  217. _dl_dprintf (_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  218. old_val, *reloc_addr, reloc_addr);
  219. #endif
  220. return 0;
  221. }
  222. void
  223. _dl_parse_lazy_relocation_information (struct dyn_elf *rpnt,
  224. unsigned long rel_addr,
  225. unsigned long rel_size)
  226. {
  227. (void) _dl_parse (rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  228. }
  229. int
  230. _dl_parse_relocation_information (struct dyn_elf *rpnt,
  231. unsigned long rel_addr,
  232. unsigned long rel_size)
  233. {
  234. return _dl_parse (rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size,
  235. _dl_do_reloc);
  236. }