elfinterp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* vi: set sw=4 ts=4: */
  2. /* i386 ELF shared library loader suppport
  3. *
  4. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  5. * David Engel, Hongjiu Lu and Mitch D'Souza
  6. * Copyright (C) 2001-2004 Erik Andersen
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include "ldso.h"
  32. #if defined (__SUPPORT_LD_DEBUG__)
  33. static const char *_dl_reltypes_tab[] = {
  34. [0] "R_386_NONE", "R_386_32", "R_386_PC32", "R_386_GOT32",
  35. [4] "R_386_PLT32", "R_386_COPY", "R_386_GLOB_DAT", "R_386_JMP_SLOT",
  36. [8] "R_386_RELATIVE", "R_386_GOTOFF", "R_386_GOTPC",
  37. };
  38. static const char *
  39. _dl_reltypes(int type)
  40. {
  41. static char buf[22];
  42. const char *str;
  43. if (type >= (int)(sizeof(_dl_reltypes_tab)/sizeof(_dl_reltypes_tab[0])) ||
  44. NULL == (str = _dl_reltypes_tab[type])) {
  45. str = _dl_simple_ltoa(buf, (unsigned long)type);
  46. }
  47. return str;
  48. }
  49. static void
  50. debug_sym(Elf32_Sym *symtab, char *strtab, int symtab_index)
  51. {
  52. if (_dl_debug_symbols) {
  53. if (symtab_index) {
  54. _dl_dprintf(_dl_debug_file,
  55. "\n%s\n\tvalue=%x\tsize=%x\tinfo=%x\tother=%x\tshndx=%x",
  56. strtab + symtab[symtab_index].st_name,
  57. symtab[symtab_index].st_value,
  58. symtab[symtab_index].st_size,
  59. symtab[symtab_index].st_info,
  60. symtab[symtab_index].st_other,
  61. symtab[symtab_index].st_shndx);
  62. }
  63. }
  64. }
  65. static void
  66. debug_reloc(Elf32_Sym *symtab, char *strtab, ELF_RELOC *rpnt)
  67. {
  68. if (_dl_debug_reloc) {
  69. int symtab_index;
  70. const char *sym;
  71. symtab_index = ELF32_R_SYM(rpnt->r_info);
  72. sym = symtab_index ? strtab + symtab[symtab_index].st_name : "sym=0x0";
  73. if (_dl_debug_symbols)
  74. _dl_dprintf(_dl_debug_file, "\n\t");
  75. else
  76. _dl_dprintf(_dl_debug_file, "\n%s\n\t", sym);
  77. #ifdef ELF_USES_RELOCA
  78. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\taddend=%x",
  79. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  80. rpnt->r_offset,
  81. rpnt->r_addend);
  82. #else
  83. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\n",
  84. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  85. rpnt->r_offset);
  86. #endif
  87. }
  88. }
  89. #endif /* __SUPPORT_LD_DEBUG__ */
  90. /* Program to load an ELF binary on a linux system, and run it.
  91. References to symbols in sharable libraries can be resolved by either
  92. an ELF sharable library or a linux style of shared library. */
  93. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  94. I ever taken any courses on internals. This program was developed using
  95. information available through the book "UNIX SYSTEM V RELEASE 4,
  96. Programmers guide: Ansi C and Programming Support Tools", which did
  97. a more than adequate job of explaining everything required to get this
  98. working. */
  99. extern int _dl_linux_resolve(void);
  100. unsigned long
  101. _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  102. {
  103. int reloc_type;
  104. ELF_RELOC *this_reloc;
  105. char *strtab;
  106. Elf32_Sym *symtab;
  107. int symtab_index;
  108. char *rel_addr;
  109. char *new_addr;
  110. char **got_addr;
  111. unsigned long instr_addr;
  112. char *symname;
  113. rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
  114. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  115. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  116. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  117. symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  118. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  119. symname = strtab + symtab[symtab_index].st_name;
  120. if (unlikely(reloc_type != R_386_JMP_SLOT)) {
  121. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  122. _dl_progname);
  123. _dl_exit(1);
  124. }
  125. /* Address of the jump instruction to fix up. */
  126. instr_addr = ((unsigned long)this_reloc->r_offset +
  127. (unsigned long)tpnt->loadaddr);
  128. got_addr = (char **)instr_addr;
  129. /* Get the address of the GOT entry. */
  130. new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  131. if (unlikely(!new_addr)) {
  132. _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
  133. _dl_exit(1);
  134. }
  135. #if defined (__SUPPORT_LD_DEBUG__)
  136. if ((unsigned long)got_addr < 0x40000000) {
  137. if (_dl_debug_bindings) {
  138. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  139. if (_dl_debug_detail)
  140. _dl_dprintf(_dl_debug_file,
  141. "\n\tpatched: %x ==> %x @ %x",
  142. *got_addr, new_addr, got_addr);
  143. }
  144. }
  145. if (!_dl_debug_nofixups) {
  146. *got_addr = new_addr;
  147. }
  148. #else
  149. *got_addr = new_addr;
  150. #endif
  151. return (unsigned long)new_addr;
  152. }
  153. static int
  154. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  155. unsigned long rel_addr, unsigned long rel_size,
  156. int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
  157. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  158. {
  159. unsigned int i;
  160. char *strtab;
  161. Elf32_Sym *symtab;
  162. ELF_RELOC *rpnt;
  163. int symtab_index;
  164. /* Parse the relocation information. */
  165. rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
  166. rel_size /= sizeof(ELF_RELOC);
  167. symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  168. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  169. for (i = 0; i < rel_size; i++, rpnt++) {
  170. int res;
  171. symtab_index = ELF32_R_SYM(rpnt->r_info);
  172. #if defined (__SUPPORT_LD_DEBUG__)
  173. debug_sym(symtab, strtab, symtab_index);
  174. debug_reloc(symtab, strtab, rpnt);
  175. #endif
  176. res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
  177. if (res == 0)
  178. continue;
  179. _dl_dprintf(2, "\n%s: ", _dl_progname);
  180. if (symtab_index)
  181. _dl_dprintf(2, "symbol '%s': ",
  182. strtab + symtab[symtab_index].st_name);
  183. if (unlikely(res < 0)) {
  184. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  185. #if defined (__SUPPORT_LD_DEBUG__)
  186. _dl_dprintf(2, "can't handle reloc type %s\n",
  187. _dl_reltypes(reloc_type));
  188. #else
  189. _dl_dprintf(2, "can't handle reloc type %x\n",
  190. reloc_type);
  191. #endif
  192. _dl_exit(-res);
  193. } else if (unlikely(res > 0)) {
  194. _dl_dprintf(2, "can't resolve symbol\n");
  195. return res;
  196. }
  197. }
  198. return 0;
  199. }
  200. static int
  201. _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  202. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  203. {
  204. int reloc_type;
  205. int symtab_index;
  206. char *symname;
  207. unsigned long *reloc_addr;
  208. unsigned long symbol_addr;
  209. #if defined (__SUPPORT_LD_DEBUG__)
  210. unsigned long old_val;
  211. #endif
  212. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  213. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  214. symtab_index = ELF32_R_SYM(rpnt->r_info);
  215. symbol_addr = 0;
  216. symname = strtab + symtab[symtab_index].st_name;
  217. if (symtab_index) {
  218. symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
  219. elf_machine_type_class(reloc_type));
  220. /*
  221. * We want to allow undefined references to weak symbols - this
  222. * might have been intentional. We should not be linking local
  223. * symbols here, so all bases should be covered.
  224. */
  225. if (unlikely(!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
  226. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  227. _dl_exit(1);
  228. };
  229. }
  230. #if defined (__SUPPORT_LD_DEBUG__)
  231. old_val = *reloc_addr;
  232. #endif
  233. switch (reloc_type) {
  234. case R_386_NONE:
  235. break;
  236. case R_386_32:
  237. *reloc_addr += symbol_addr;
  238. break;
  239. case R_386_PC32:
  240. *reloc_addr += symbol_addr - (unsigned long)reloc_addr;
  241. break;
  242. case R_386_GLOB_DAT:
  243. case R_386_JMP_SLOT:
  244. *reloc_addr = symbol_addr;
  245. break;
  246. case R_386_RELATIVE:
  247. *reloc_addr += (unsigned long)tpnt->loadaddr;
  248. break;
  249. case R_386_COPY:
  250. if (symbol_addr) {
  251. #if defined (__SUPPORT_LD_DEBUG__)
  252. if (_dl_debug_move)
  253. _dl_dprintf(_dl_debug_file,
  254. "\n%s move %d bytes from %x to %x",
  255. symname, symtab[symtab_index].st_size,
  256. symbol_addr, reloc_addr);
  257. #endif
  258. _dl_memcpy((char *)reloc_addr,
  259. (char *)symbol_addr,
  260. symtab[symtab_index].st_size);
  261. }
  262. break;
  263. default:
  264. return -1; /* Calls _dl_exit(1). */
  265. }
  266. #if defined (__SUPPORT_LD_DEBUG__)
  267. if (_dl_debug_reloc && _dl_debug_detail)
  268. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x",
  269. old_val, *reloc_addr, reloc_addr);
  270. #endif
  271. return 0;
  272. }
  273. static int
  274. _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
  275. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  276. {
  277. int reloc_type;
  278. unsigned long *reloc_addr;
  279. #if defined (__SUPPORT_LD_DEBUG__)
  280. unsigned long old_val;
  281. #endif
  282. (void)scope;
  283. (void)symtab;
  284. (void)strtab;
  285. reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
  286. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  287. #if defined (__SUPPORT_LD_DEBUG__)
  288. old_val = *reloc_addr;
  289. #endif
  290. switch (reloc_type) {
  291. case R_386_NONE:
  292. break;
  293. case R_386_JMP_SLOT:
  294. *reloc_addr += (unsigned long)tpnt->loadaddr;
  295. break;
  296. default:
  297. return -1; /* Calls _dl_exit(1). */
  298. }
  299. #if defined (__SUPPORT_LD_DEBUG__)
  300. if (_dl_debug_reloc && _dl_debug_detail)
  301. _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x",
  302. old_val, *reloc_addr, reloc_addr);
  303. #endif
  304. return 0;
  305. }
  306. void
  307. _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  308. unsigned long rel_addr,
  309. unsigned long rel_size)
  310. {
  311. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  312. }
  313. int
  314. _dl_parse_relocation_information(struct dyn_elf *rpnt,
  315. unsigned long rel_addr,
  316. unsigned long rel_size)
  317. {
  318. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  319. }