elfinterp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. Elf32_Sym *sym;
  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 = &symtab[symtab_index];
  136. symbol_addr = 0;
  137. symname = strtab + sym->st_name;
  138. if (symtab_index) {
  139. symbol_addr = (Elf32_Addr)
  140. _dl_find_hash (symname, scope, tpnt,
  141. elf_machine_type_class (reloc_type), NULL);
  142. /*
  143. * We want to allow undefined references to weak symbols - this might
  144. * have been intentional. We should not be linking local symbols
  145. * here, so all bases should be covered.
  146. */
  147. if (unlikely (!symbol_addr &&
  148. ELF32_ST_BIND (sym->st_info) != STB_WEAK)) {
  149. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  150. _dl_progname, symname);
  151. _dl_exit (1);
  152. }
  153. }
  154. #if defined (__SUPPORT_LD_DEBUG__)
  155. old_val = *reloc_addr;
  156. #endif
  157. switch (reloc_type) {
  158. case R_XTENSA_NONE:
  159. break;
  160. case R_XTENSA_GLOB_DAT:
  161. case R_XTENSA_JMP_SLOT:
  162. *reloc_addr = symbol_addr + rpnt->r_addend;
  163. break;
  164. case R_XTENSA_RTLD:
  165. if (rpnt->r_addend == 1) {
  166. /* Grab the function pointer stashed at the beginning of the
  167. GOT by the GOT_INIT function. */
  168. *reloc_addr = *(Elf32_Addr *) tpnt->dynamic_info[DT_PLTGOT];
  169. } else if (rpnt->r_addend == 2) {
  170. /* Store the link map for the object. */
  171. *reloc_addr = (Elf32_Addr) tpnt;
  172. } else {
  173. _dl_exit (1);
  174. }
  175. break;
  176. case R_XTENSA_RELATIVE:
  177. *reloc_addr += tpnt->loadaddr + rpnt->r_addend;
  178. break;
  179. default:
  180. return -1; /* Calls _dl_exit(1). */
  181. }
  182. #if defined (__SUPPORT_LD_DEBUG__)
  183. if (_dl_debug_reloc && _dl_debug_detail)
  184. _dl_dprintf (_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  185. old_val, *reloc_addr, reloc_addr);
  186. #endif
  187. return 0;
  188. }
  189. static int
  190. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
  191. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  192. {
  193. int reloc_type;
  194. Elf32_Addr *reloc_addr;
  195. #if defined (__SUPPORT_LD_DEBUG__)
  196. Elf32_Addr old_val;
  197. #endif
  198. reloc_addr = (Elf32_Addr *) (tpnt->loadaddr + rpnt->r_offset);
  199. reloc_type = ELF32_R_TYPE (rpnt->r_info);
  200. #if defined (__SUPPORT_LD_DEBUG__)
  201. old_val = *reloc_addr;
  202. #endif
  203. switch (reloc_type) {
  204. case R_XTENSA_JMP_SLOT:
  205. /* Perform a RELATIVE reloc on the GOT entry that transfers
  206. to the stub function. */
  207. *reloc_addr += tpnt->loadaddr;
  208. break;
  209. case R_XTENSA_NONE:
  210. break;
  211. default:
  212. _dl_exit (1);
  213. }
  214. #if defined (__SUPPORT_LD_DEBUG__)
  215. if (_dl_debug_reloc && _dl_debug_detail)
  216. _dl_dprintf (_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
  217. old_val, *reloc_addr, reloc_addr);
  218. #endif
  219. return 0;
  220. }
  221. void
  222. _dl_parse_lazy_relocation_information (struct dyn_elf *rpnt,
  223. unsigned long rel_addr,
  224. unsigned long rel_size)
  225. {
  226. (void) _dl_parse (rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  227. }
  228. int
  229. _dl_parse_relocation_information (struct dyn_elf *rpnt,
  230. unsigned long rel_addr,
  231. unsigned long rel_size)
  232. {
  233. return _dl_parse (rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size,
  234. _dl_do_reloc);
  235. }