elfinterp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* vi: set sw=4 ts=4: */
  2. /* mips/mipsel ELF shared library loader suppport
  3. *
  4. Copyright (C) 2002, Steven J. Hill (sjhill@realitydiluted.com)
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. The name of the above contributors may not be
  14. * used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "ldso.h"
  30. extern int _dl_runtime_resolve(void);
  31. #define OFFSET_GP_GOT 0x7ff0
  32. unsigned long __dl_runtime_resolve(unsigned long sym_index,
  33. unsigned long old_gpreg)
  34. {
  35. unsigned long *got = (unsigned long *) (old_gpreg - OFFSET_GP_GOT);
  36. struct elf_resolve *tpnt = (struct elf_resolve *) got[1];
  37. Elf32_Sym *sym;
  38. char *strtab;
  39. unsigned long local_gotno;
  40. unsigned long gotsym;
  41. unsigned long new_addr;
  42. unsigned long instr_addr;
  43. char **got_addr;
  44. char *symname;
  45. gotsym = tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
  46. local_gotno = tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX];
  47. sym = ((Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB]) + sym_index;
  48. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  49. symname = strtab + sym->st_name;
  50. new_addr = (unsigned long) _dl_find_hash(symname,
  51. tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  52. if (unlikely(!new_addr)) {
  53. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  54. _dl_progname, symname);
  55. _dl_exit (1);
  56. }
  57. /* Address of jump instruction to fix up */
  58. instr_addr = (unsigned long) (got + local_gotno + sym_index - gotsym);
  59. got_addr = (char **) instr_addr;
  60. #if defined (__SUPPORT_LD_DEBUG__)
  61. if (_dl_debug_bindings)
  62. {
  63. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  64. if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  65. "\n\tpatched %x ==> %x @ %x\n", *got_addr, new_addr, got_addr);
  66. }
  67. if (!_dl_debug_nofixups) {
  68. *got_addr = (char*)new_addr;
  69. }
  70. #else
  71. *got_addr = (char*)new_addr;
  72. #endif
  73. return new_addr;
  74. }
  75. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  76. unsigned long rel_addr, unsigned long rel_size)
  77. {
  78. /* Nothing to do */
  79. return;
  80. }
  81. int _dl_parse_relocation_information(struct dyn_elf *xpnt,
  82. unsigned long rel_addr, unsigned long rel_size)
  83. {
  84. Elf32_Sym *symtab;
  85. Elf32_Rel *rpnt;
  86. char *strtab;
  87. unsigned long i;
  88. unsigned long *got;
  89. unsigned long *reloc_addr=NULL;
  90. unsigned long symbol_addr;
  91. int reloc_type, symtab_index;
  92. struct elf_resolve *tpnt = xpnt->dyn;
  93. #if defined (__SUPPORT_LD_DEBUG__)
  94. unsigned long old_val=0;
  95. #endif
  96. /* Now parse the relocation information */
  97. rel_size = rel_size / sizeof(Elf32_Rel);
  98. rpnt = (Elf32_Rel *) rel_addr;
  99. symtab = (Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB];
  100. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  101. got = (unsigned long *) tpnt->dynamic_info[DT_PLTGOT];
  102. for (i = 0; i < rel_size; i++, rpnt++) {
  103. reloc_addr = (unsigned long *) (tpnt->loadaddr +
  104. (unsigned long) rpnt->r_offset);
  105. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  106. symtab_index = ELF32_R_SYM(rpnt->r_info);
  107. symbol_addr = 0;
  108. debug_sym(symtab,strtab,symtab_index);
  109. debug_reloc(symtab,strtab,rpnt);
  110. #if defined (__SUPPORT_LD_DEBUG__)
  111. if (reloc_addr)
  112. old_val = *reloc_addr;
  113. #endif
  114. switch (reloc_type) {
  115. case R_MIPS_REL32:
  116. if (symtab_index) {
  117. if (symtab_index < tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX])
  118. *reloc_addr +=
  119. symtab[symtab_index].st_value +
  120. (unsigned long) tpnt->loadaddr;
  121. else {
  122. *reloc_addr += got[symtab_index + tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX] -
  123. tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX]];
  124. }
  125. }
  126. else {
  127. *reloc_addr += (unsigned long) tpnt->loadaddr;
  128. }
  129. break;
  130. case R_MIPS_NONE:
  131. break;
  132. default:
  133. {
  134. _dl_dprintf(2, "\n%s: ",_dl_progname);
  135. if (symtab_index)
  136. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  137. #if defined (__SUPPORT_LD_DEBUG__)
  138. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  139. #else
  140. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  141. #endif
  142. _dl_exit(1);
  143. }
  144. }
  145. }
  146. #if defined (__SUPPORT_LD_DEBUG__)
  147. if (_dl_debug_reloc && _dl_debug_detail && reloc_addr)
  148. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
  149. #endif
  150. return 0;
  151. }
  152. /* Relocate the global GOT entries for the object */
  153. void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy)
  154. {
  155. Elf32_Sym *sym;
  156. char *strtab;
  157. unsigned long i, tmp_lazy;
  158. unsigned long *got_entry;
  159. for (; tpnt ; tpnt = tpnt->next) {
  160. /* We don't touch the dynamic linker */
  161. if (tpnt->libtype == program_interpreter)
  162. continue;
  163. /* Setup the loop variables */
  164. got_entry = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT])
  165. + tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX];
  166. sym = (Elf32_Sym *) tpnt->dynamic_info[DT_SYMTAB] + tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
  167. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  168. i = tpnt->dynamic_info[DT_MIPS_SYMTABNO_IDX] - tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
  169. #if defined (__SUPPORT_LD_DEBUG__)
  170. if (_dl_debug_reloc)
  171. _dl_dprintf(2, "_dl_perform_mips_global_got_relocations for '%s'\n", tpnt->libname);
  172. #endif
  173. tmp_lazy = lazy && !tpnt->dynamic_info[DT_BIND_NOW];
  174. /* Relocate the global GOT entries for the object */
  175. while (i--) {
  176. if (sym->st_shndx == SHN_UNDEF) {
  177. if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC && sym->st_value && tmp_lazy) {
  178. *got_entry = sym->st_value + (unsigned long) tpnt->loadaddr;
  179. }
  180. else {
  181. *got_entry = (unsigned long) _dl_find_hash(strtab +
  182. sym->st_name, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  183. }
  184. }
  185. else if (sym->st_shndx == SHN_COMMON) {
  186. *got_entry = (unsigned long) _dl_find_hash(strtab +
  187. sym->st_name, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  188. }
  189. else if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
  190. *got_entry != sym->st_value && tmp_lazy) {
  191. *got_entry += (unsigned long) tpnt->loadaddr;
  192. }
  193. else if (ELF32_ST_TYPE(sym->st_info) == STT_SECTION) {
  194. if (sym->st_other == 0)
  195. *got_entry += (unsigned long) tpnt->loadaddr;
  196. }
  197. else {
  198. *got_entry = (unsigned long) _dl_find_hash(strtab +
  199. sym->st_name, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  200. }
  201. got_entry++;
  202. sym++;
  203. }
  204. }
  205. }