elfinterp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. {
  35. [0] "R_386_NONE", "R_386_32", "R_386_PC32", "R_386_GOT32",
  36. [4] "R_386_PLT32", "R_386_COPY", "R_386_GLOB_DAT", "R_386_JMP_SLOT",
  37. [8] "R_386_RELATIVE", "R_386_GOTOFF", "R_386_GOTPC",
  38. };
  39. static const char *
  40. _dl_reltypes(int type)
  41. {
  42. static char buf[22];
  43. const char *str;
  44. if (type >= (int)(sizeof (_dl_reltypes_tab)/sizeof(_dl_reltypes_tab[0])) ||
  45. NULL == (str = _dl_reltypes_tab[type]))
  46. {
  47. str =_dl_simple_ltoa( buf, (unsigned long)(type));
  48. }
  49. return str;
  50. }
  51. static
  52. void debug_sym(Elf32_Sym *symtab,char *strtab,int symtab_index)
  53. {
  54. if(_dl_debug_symbols)
  55. {
  56. if(symtab_index){
  57. _dl_dprintf(_dl_debug_file, "\n%s\n\tvalue=%x\tsize=%x\tinfo=%x\tother=%x\tshndx=%x",
  58. strtab + symtab[symtab_index].st_name,
  59. symtab[symtab_index].st_value,
  60. symtab[symtab_index].st_size,
  61. symtab[symtab_index].st_info,
  62. symtab[symtab_index].st_other,
  63. symtab[symtab_index].st_shndx);
  64. }
  65. }
  66. }
  67. static void debug_reloc(Elf32_Sym *symtab,char *strtab, ELF_RELOC *rpnt)
  68. {
  69. if(_dl_debug_reloc)
  70. {
  71. int symtab_index;
  72. const char *sym;
  73. symtab_index = ELF32_R_SYM(rpnt->r_info);
  74. sym = symtab_index ? strtab + symtab[symtab_index].st_name : "sym=0x0";
  75. if(_dl_debug_symbols)
  76. _dl_dprintf(_dl_debug_file, "\n\t");
  77. else
  78. _dl_dprintf(_dl_debug_file, "\n%s\n\t", sym);
  79. #ifdef ELF_USES_RELOCA
  80. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\taddend=%x",
  81. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  82. rpnt->r_offset,
  83. rpnt->r_addend);
  84. #else
  85. _dl_dprintf(_dl_debug_file, "%s\toffset=%x\n",
  86. _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
  87. rpnt->r_offset);
  88. #endif
  89. }
  90. }
  91. #endif
  92. /* Program to load an ELF binary on a linux system, and run it.
  93. References to symbols in sharable libraries can be resolved by either
  94. an ELF sharable library or a linux style of shared library. */
  95. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  96. I ever taken any courses on internals. This program was developed using
  97. information available through the book "UNIX SYSTEM V RELEASE 4,
  98. Programmers guide: Ansi C and Programming Support Tools", which did
  99. a more than adequate job of explaining everything required to get this
  100. working. */
  101. extern int _dl_linux_resolve(void);
  102. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  103. {
  104. int reloc_type;
  105. ELF_RELOC *this_reloc;
  106. char *strtab;
  107. Elf32_Sym *symtab;
  108. int symtab_index;
  109. char *rel_addr;
  110. char *new_addr;
  111. char **got_addr;
  112. unsigned long instr_addr;
  113. char *symname;
  114. rel_addr = (char *) (tpnt->dynamic_info[DT_JMPREL] + tpnt->loadaddr);
  115. this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
  116. reloc_type = ELF32_R_TYPE(this_reloc->r_info);
  117. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  118. symtab = (Elf32_Sym *)(intptr_t) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  119. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  120. symname= strtab + symtab[symtab_index].st_name;
  121. if (unlikely(reloc_type != R_386_JMP_SLOT)) {
  122. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
  123. _dl_progname);
  124. _dl_exit(1);
  125. }
  126. /* Address of jump instruction to fix up */
  127. instr_addr = ((unsigned long) this_reloc->r_offset +
  128. (unsigned long) tpnt->loadaddr);
  129. got_addr = (char **) instr_addr;
  130. /* Get the address of the GOT entry */
  131. new_addr = _dl_find_hash(symname, tpnt->symbol_scope, ELF_RTYPE_CLASS_PLT);
  132. if (unlikely(!new_addr)) {
  133. _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
  134. _dl_exit(1);
  135. };
  136. #if defined (__SUPPORT_LD_DEBUG__)
  137. if ((unsigned long) got_addr < 0x40000000)
  138. {
  139. if (_dl_debug_bindings)
  140. {
  141. _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
  142. if(_dl_debug_detail) _dl_dprintf(_dl_debug_file,
  143. "\n\tpatched %x ==> %x @ %x\n", *got_addr, new_addr, got_addr);
  144. }
  145. }
  146. if (!_dl_debug_nofixups) {
  147. *got_addr = new_addr;
  148. }
  149. #else
  150. *got_addr = new_addr;
  151. #endif
  152. return (unsigned long) new_addr;
  153. }
  154. static int
  155. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  156. unsigned long rel_addr, unsigned long rel_size,
  157. int (*reloc_fnc) (struct elf_resolve *tpnt, struct dyn_elf *scope,
  158. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  159. {
  160. unsigned int i;
  161. char *strtab;
  162. Elf32_Sym *symtab;
  163. ELF_RELOC *rpnt;
  164. int symtab_index;
  165. /* Now parse the relocation information */
  166. rpnt = (ELF_RELOC *)(intptr_t) (rel_addr + tpnt->loadaddr);
  167. rel_size = rel_size / sizeof(ELF_RELOC);
  168. symtab = (Elf32_Sym *)(intptr_t) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
  169. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  170. for (i = 0; i < rel_size; i++, rpnt++) {
  171. int res;
  172. symtab_index = ELF32_R_SYM(rpnt->r_info);
  173. /* When the dynamic linker bootstrapped itself, it resolved some symbols.
  174. Make sure we do not do them again */
  175. if (!symtab_index && tpnt->libtype == program_interpreter)
  176. continue;
  177. if (symtab_index && tpnt->libtype == program_interpreter &&
  178. _dl_symbol(strtab + symtab[symtab_index].st_name))
  179. continue;
  180. #if defined (__SUPPORT_LD_DEBUG__)
  181. debug_sym(symtab,strtab,symtab_index);
  182. debug_reloc(symtab,strtab,rpnt);
  183. #endif
  184. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  185. if (res==0) continue;
  186. _dl_dprintf(2, "\n%s: ",_dl_progname);
  187. if (symtab_index)
  188. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  189. if (res <0)
  190. {
  191. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  192. #if defined (__SUPPORT_LD_DEBUG__)
  193. _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
  194. #else
  195. _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
  196. #endif
  197. _dl_exit(-res);
  198. }
  199. else if (res >0)
  200. {
  201. _dl_dprintf(2, "can't resolve symbol\n");
  202. return res;
  203. }
  204. }
  205. return 0;
  206. }
  207. static int
  208. _dl_do_reloc (struct elf_resolve *tpnt,struct dyn_elf *scope,
  209. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  210. {
  211. int reloc_type;
  212. int symtab_index;
  213. char *symname;
  214. unsigned long *reloc_addr;
  215. unsigned long symbol_addr;
  216. #if defined (__SUPPORT_LD_DEBUG__)
  217. unsigned long old_val;
  218. #endif
  219. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  220. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  221. symtab_index = ELF32_R_SYM(rpnt->r_info);
  222. symbol_addr = 0;
  223. symname = strtab + symtab[symtab_index].st_name;
  224. if (symtab_index) {
  225. symbol_addr = (unsigned long) _dl_find_hash(symname, scope,
  226. elf_machine_type_class(reloc_type));
  227. /*
  228. * We want to allow undefined references to weak symbols - this might
  229. * have been intentional. We should not be linking local symbols
  230. * here, so all bases should be covered.
  231. */
  232. if (!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_GLOBAL) {
  233. #if defined (__SUPPORT_LD_DEBUG__)
  234. _dl_dprintf(2, "\tglobal symbol '%s' already defined in '%s', rel type: %s\n",
  235. symname, tpnt->libname, _dl_reltypes(reloc_type));
  236. #endif
  237. return 0;
  238. }
  239. }
  240. #if defined (__SUPPORT_LD_DEBUG__)
  241. old_val = *reloc_addr;
  242. #endif
  243. switch (reloc_type) {
  244. case R_386_NONE:
  245. break;
  246. case R_386_32:
  247. *reloc_addr += symbol_addr;
  248. break;
  249. case R_386_PC32:
  250. *reloc_addr += symbol_addr - (unsigned long) reloc_addr;
  251. break;
  252. case R_386_GLOB_DAT:
  253. case R_386_JMP_SLOT:
  254. *reloc_addr = symbol_addr;
  255. break;
  256. case R_386_RELATIVE:
  257. *reloc_addr += (unsigned long) tpnt->loadaddr;
  258. break;
  259. case R_386_COPY:
  260. /* handled later on */
  261. break;
  262. default:
  263. return -1; /*call _dl_exit(1) */
  264. }
  265. #if defined (__SUPPORT_LD_DEBUG__)
  266. if(_dl_debug_reloc && _dl_debug_detail)
  267. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  268. #endif
  269. return 0;
  270. }
  271. static int
  272. _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
  273. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  274. {
  275. int reloc_type;
  276. unsigned long *reloc_addr;
  277. #if defined (__SUPPORT_LD_DEBUG__)
  278. unsigned long old_val;
  279. #endif
  280. (void)scope;
  281. (void)symtab;
  282. (void)strtab;
  283. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  284. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  285. #if defined (__SUPPORT_LD_DEBUG__)
  286. old_val = *reloc_addr;
  287. #endif
  288. switch (reloc_type) {
  289. case R_386_NONE:
  290. break;
  291. case R_386_JMP_SLOT:
  292. *reloc_addr += (unsigned long) tpnt->loadaddr;
  293. break;
  294. default:
  295. return -1; /*call _dl_exit(1) */
  296. }
  297. #if defined (__SUPPORT_LD_DEBUG__)
  298. if(_dl_debug_reloc && _dl_debug_detail)
  299. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  300. #endif
  301. return 0;
  302. }
  303. /* This is done as a separate step, because there are cases where
  304. information is first copied and later initialized. This results in
  305. the wrong information being copied. Someone at Sun was complaining about
  306. a bug in the handling of _COPY by SVr4, and this may in fact be what he
  307. was talking about. Sigh. */
  308. /* No, there are cases where the SVr4 linker fails to emit COPY relocs
  309. at all */
  310. static int
  311. _dl_do_copy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
  312. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  313. {
  314. int reloc_type;
  315. int symtab_index;
  316. unsigned long *reloc_addr;
  317. unsigned long symbol_addr;
  318. int goof = 0;
  319. char *symname;
  320. reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  321. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  322. if (reloc_type != R_386_COPY)
  323. return 0;
  324. symtab_index = ELF32_R_SYM(rpnt->r_info);
  325. symbol_addr = 0;
  326. symname = strtab + symtab[symtab_index].st_name;
  327. if (symtab_index) {
  328. symbol_addr = (unsigned long) _dl_find_hash(symname, scope, ELF_RTYPE_CLASS_COPY);
  329. if (!symbol_addr) goof++;
  330. }
  331. if (!goof) {
  332. #if defined (__SUPPORT_LD_DEBUG__)
  333. if(_dl_debug_move)
  334. _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
  335. symname, symtab[symtab_index].st_size,
  336. symbol_addr, symtab[symtab_index].st_value);
  337. #endif
  338. _dl_memcpy((char *) symtab[symtab_index].st_value,
  339. (char *) symbol_addr, symtab[symtab_index].st_size);
  340. }
  341. return goof;
  342. }
  343. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  344. unsigned long rel_addr, unsigned long rel_size, int type)
  345. {
  346. (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
  347. }
  348. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  349. unsigned long rel_addr, unsigned long rel_size, int type)
  350. {
  351. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  352. }
  353. int _dl_parse_copy_information(struct dyn_elf *rpnt,
  354. unsigned long rel_addr, unsigned long rel_size, int type)
  355. {
  356. return _dl_parse(rpnt->dyn, rpnt->next, rel_addr, rel_size, _dl_do_copy_reloc);
  357. }