elfinterp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #if defined (__SUPPORT_LD_DEBUG__)
  30. static const char *_dl_reltypes_tab[] =
  31. {
  32. [0] "R_MIPS_NONE", "R_MIPS_16", "R_MIPS_32",
  33. [3] "R_MIPS_REL32", "R_MIPS_26", "R_MIPS_HI16",
  34. [6] "R_MIPS_LO16", "R_MIPS_GPREL16", "R_MIPS_LITERAL",
  35. [9] "R_MIPS_GOT16", "R_MIPS_PC16", "R_MIPS_CALL16",
  36. [12] "R_MIPS_GPREL32",
  37. [16] "R_MIPS_SHIFT5", "R_MIPS_SHIFT6", "R_MIPS_64",
  38. [19] "R_MIPS_GOT_DISP", "R_MIPS_GOT_PAGE", "R_MIPS_GOT_OFST",
  39. [22] "R_MIPS_GOT_HI16", "R_MIPS_GOT_LO16", "R_MIPS_SUB",
  40. [25] "R_MIPS_INSERT_A", "R_MIPS_INSERT_B", "R_MIPS_DELETE",
  41. [28] "R_MIPS_HIGHER", "R_MIPS_HIGHEST", "R_MIPS_CALL_HI16",
  42. [31] "R_MIPS_CALL_LO16", "R_MIPS_SCN_DISP", "R_MIPS_REL16",
  43. [34] "R_MIPS_ADD_IMMEDIATE", "R_MIPS_PJUMP", "R_MIPS_RELGOT",
  44. [37] "R_MIPS_JALR",
  45. };
  46. static const char *
  47. _dl_reltypes(int type)
  48. {
  49. static char buf[22];
  50. const char *str;
  51. if (type >= (int)(sizeof (_dl_reltypes_tab)/sizeof(_dl_reltypes_tab[0])) ||
  52. NULL == (str = _dl_reltypes_tab[type]))
  53. {
  54. str =_dl_simple_ltoa( buf, (unsigned long)(type));
  55. }
  56. return str;
  57. }
  58. static
  59. void debug_sym(Elf32_Sym *symtab,char *strtab,int symtab_index)
  60. {
  61. if(_dl_debug_symbols)
  62. {
  63. if(symtab_index){
  64. _dl_dprintf(_dl_debug_file, "\n%s\n\tvalue=%x\tsize=%x\tinfo=%x\tother=%x\tshndx=%x",
  65. strtab + symtab[symtab_index].st_name,
  66. symtab[symtab_index].st_value,
  67. symtab[symtab_index].st_size,
  68. symtab[symtab_index].st_info,
  69. symtab[symtab_index].st_other,
  70. symtab[symtab_index].st_shndx);
  71. }
  72. }
  73. }
  74. static void debug_reloc(Elf32_Sym *symtab,char *strtab, ELF_RELOC *rpnt)
  75. {
  76. if(_dl_debug_reloc)
  77. {
  78. int symtab_index;
  79. const char *sym;
  80. symtab_index = ELF32_R_SYM(rpnt->r_info);
  81. sym = symtab_index ? strtab + symtab[symtab_index].st_name : "sym=0x0";
  82. if(_dl_debug_symbols)
  83. _dl_dprintf(_dl_debug_file, "\n\t");
  84. else
  85. _dl_dprintf(_dl_debug_file, "\n%s\n\t", sym);
  86. #ifdef ELF_USES_RELOCA
  87. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\taddend=%x",
  88. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  89. rpnt->r_offset,
  90. rpnt->r_addend);
  91. #else
  92. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\n",
  93. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  94. rpnt->r_offset);
  95. #endif
  96. }
  97. }
  98. #endif
  99. extern int _dl_linux_resolve(void);
  100. #define OFFSET_GP_GOT 0x7ff0
  101. unsigned long _dl_linux_resolver(unsigned long sym_index,
  102. unsigned long old_gpreg)
  103. {
  104. unsigned long *got = (unsigned long *) (old_gpreg - OFFSET_GP_GOT);
  105. struct elf_resolve *tpnt = (struct elf_resolve *) got[1];
  106. Elf32_Sym *sym;
  107. char *strtab;
  108. unsigned long local_gotno;
  109. unsigned long gotsym;
  110. unsigned long new_addr;
  111. unsigned long instr_addr;
  112. char **got_addr;
  113. char *symname;
  114. gotsym = tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
  115. local_gotno = tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX];
  116. sym = ((Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr)) + sym_index;
  117. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  118. symname = strtab + sym->st_name;
  119. new_addr = (unsigned long) _dl_find_hash(symname,
  120. tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  121. if (unlikely(!new_addr)) {
  122. _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
  123. _dl_progname, symname);
  124. _dl_exit (1);
  125. }
  126. /* Address of jump instruction to fix up */
  127. instr_addr = (unsigned long) (got + local_gotno + sym_index - gotsym);
  128. got_addr = (char **) instr_addr;
  129. #if defined (__SUPPORT_LD_DEBUG__)
  130. if (_dl_debug_bindings)
  131. {
  132. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  133. if(_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  134. "\n\tpatched %x ==> %x @ %x\n", *got_addr, new_addr, got_addr);
  135. }
  136. if (!_dl_debug_nofixups) {
  137. *got_addr = (char*)new_addr;
  138. }
  139. #else
  140. *got_addr = (char*)new_addr;
  141. #endif
  142. return new_addr;
  143. }
  144. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  145. unsigned long rel_addr, unsigned long rel_size)
  146. {
  147. /* Nothing to do */
  148. return;
  149. }
  150. int _dl_parse_relocation_information(struct dyn_elf *xpnt,
  151. unsigned long rel_addr, unsigned long rel_size)
  152. {
  153. Elf32_Sym *symtab;
  154. Elf32_Rel *rpnt;
  155. char *strtab;
  156. unsigned long i;
  157. unsigned long *got;
  158. unsigned long *reloc_addr=NULL;
  159. unsigned long symbol_addr;
  160. int reloc_type, symtab_index;
  161. struct elf_resolve *tpnt = xpnt->dyn;
  162. #if defined (__SUPPORT_LD_DEBUG__)
  163. unsigned long old_val=0;
  164. #endif
  165. /* Now parse the relocation information */
  166. rel_size = rel_size / sizeof(Elf32_Rel);
  167. rpnt = (Elf32_Rel *) (rel_addr + tpnt->loadaddr);
  168. symtab = (Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  169. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  170. got = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT] + tpnt->loadaddr);
  171. for (i = 0; i < rel_size; i++, rpnt++) {
  172. reloc_addr = (unsigned long *) (tpnt->loadaddr +
  173. (unsigned long) rpnt->r_offset);
  174. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  175. symtab_index = ELF32_R_SYM(rpnt->r_info);
  176. symbol_addr = 0;
  177. #if defined (__SUPPORT_LD_DEBUG__)
  178. debug_sym(symtab,strtab,symtab_index);
  179. debug_reloc(symtab,strtab,rpnt);
  180. if (reloc_addr)
  181. old_val = *reloc_addr;
  182. #endif
  183. switch (reloc_type) {
  184. case R_MIPS_REL32:
  185. if (symtab_index) {
  186. if (symtab_index < tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX])
  187. *reloc_addr +=
  188. symtab[symtab_index].st_value +
  189. (unsigned long) tpnt->loadaddr;
  190. else {
  191. *reloc_addr += got[symtab_index + tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX] -
  192. tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX]];
  193. }
  194. }
  195. else {
  196. *reloc_addr += (unsigned long) tpnt->loadaddr;
  197. }
  198. break;
  199. case R_MIPS_NONE:
  200. break;
  201. default:
  202. {
  203. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  204. _dl_dprintf(2, "\n%s: ",_dl_progname);
  205. if (symtab_index)
  206. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  207. #if defined (__SUPPORT_LD_DEBUG__)
  208. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  209. #else
  210. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  211. #endif
  212. _dl_exit(1);
  213. }
  214. };
  215. };
  216. #if defined (__SUPPORT_LD_DEBUG__)
  217. if(_dl_debug_reloc && _dl_debug_detail && reloc_addr)
  218. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
  219. #endif
  220. return 0;
  221. }
  222. /* Relocate the global GOT entries for the object */
  223. void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy)
  224. {
  225. Elf32_Sym *sym;
  226. char *strtab;
  227. unsigned long i, tmp_lazy;
  228. unsigned long *got_entry;
  229. for (; tpnt ; tpnt = tpnt->next) {
  230. /* We don't touch the dynamic linker */
  231. if (tpnt->libtype == program_interpreter)
  232. continue;
  233. /* Setup the loop variables */
  234. got_entry = (unsigned long *) (tpnt->loadaddr +
  235. tpnt->dynamic_info[DT_PLTGOT]) + tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX];
  236. sym = (Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] +
  237. (unsigned long) tpnt->loadaddr) + tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
  238. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] +
  239. (unsigned long) tpnt->loadaddr);
  240. i = tpnt->dynamic_info[DT_MIPS_SYMTABNO_IDX] - tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
  241. #if defined (__SUPPORT_LD_DEBUG__)
  242. if(_dl_debug_reloc)
  243. _dl_dprintf(2, "_dl_perform_mips_global_got_relocations for '%s'\n", tpnt->libname);
  244. #endif
  245. tmp_lazy = lazy && !tpnt->dynamic_info[DT_BIND_NOW];
  246. /* Relocate the global GOT entries for the object */
  247. while(i--) {
  248. if (sym->st_shndx == SHN_UNDEF) {
  249. if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC && sym->st_value && tmp_lazy) {
  250. *got_entry = sym->st_value + (unsigned long) tpnt->loadaddr;
  251. }
  252. else {
  253. *got_entry = (unsigned long) _dl_find_hash(strtab +
  254. sym->st_name, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  255. }
  256. }
  257. else if (sym->st_shndx == SHN_COMMON) {
  258. *got_entry = (unsigned long) _dl_find_hash(strtab +
  259. sym->st_name, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  260. }
  261. else if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
  262. *got_entry != sym->st_value && tmp_lazy) {
  263. *got_entry += (unsigned long) tpnt->loadaddr;
  264. }
  265. else if (ELF32_ST_TYPE(sym->st_info) == STT_SECTION) {
  266. if (sym->st_other == 0)
  267. *got_entry += (unsigned long) tpnt->loadaddr;
  268. }
  269. else {
  270. *got_entry = (unsigned long) _dl_find_hash(strtab +
  271. sym->st_name, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  272. }
  273. got_entry++;
  274. sym++;
  275. }
  276. }
  277. }