|
|
1.1 root 1: /* This is the Linux kernel elf-loading code, ported into user space */
1.1.1.8 root 2: #include <sys/time.h>
3: #include <sys/param.h>
1.1 root 4:
5: #include <stdio.h>
6: #include <sys/types.h>
7: #include <fcntl.h>
8: #include <errno.h>
9: #include <unistd.h>
10: #include <sys/mman.h>
1.1.1.8 root 11: #include <sys/resource.h>
1.1 root 12: #include <stdlib.h>
13: #include <string.h>
1.1.1.8 root 14: #include <time.h>
1.1 root 15:
16: #include "qemu.h"
17: #include "disas.h"
18:
1.1.1.7 root 19: #ifdef _ARCH_PPC64
20: #undef ARCH_DLINFO
21: #undef ELF_PLATFORM
22: #undef ELF_HWCAP
23: #undef ELF_CLASS
24: #undef ELF_DATA
25: #undef ELF_ARCH
26: #endif
27:
1.1.1.8 root 28: #define ELF_OSABI ELFOSABI_SYSV
29:
1.1.1.6 root 30: /* from personality.h */
31:
32: /*
33: * Flags for bug emulation.
34: *
35: * These occupy the top three bytes.
36: */
37: enum {
38: ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
39: FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to descriptors
40: * (signal handling)
41: */
42: MMAP_PAGE_ZERO = 0x0100000,
43: ADDR_COMPAT_LAYOUT = 0x0200000,
44: READ_IMPLIES_EXEC = 0x0400000,
45: ADDR_LIMIT_32BIT = 0x0800000,
46: SHORT_INODE = 0x1000000,
47: WHOLE_SECONDS = 0x2000000,
48: STICKY_TIMEOUTS = 0x4000000,
49: ADDR_LIMIT_3GB = 0x8000000,
50: };
51:
52: /*
53: * Personality types.
54: *
55: * These go in the low byte. Avoid using the top bit, it will
56: * conflict with error returns.
57: */
58: enum {
59: PER_LINUX = 0x0000,
60: PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
61: PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
62: PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
63: PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
64: PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS |
65: WHOLE_SECONDS | SHORT_INODE,
66: PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
67: PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
68: PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
69: PER_BSD = 0x0006,
70: PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
71: PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
72: PER_LINUX32 = 0x0008,
73: PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
74: PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
75: PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
76: PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
77: PER_RISCOS = 0x000c,
78: PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
79: PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
80: PER_OSF4 = 0x000f, /* OSF/1 v4 */
81: PER_HPUX = 0x0010,
82: PER_MASK = 0x00ff,
83: };
84:
85: /*
86: * Return the base personality without flags.
87: */
88: #define personality(pers) (pers & PER_MASK)
89:
1.1 root 90: /* this flag is uneffective under linux too, should be deleted */
91: #ifndef MAP_DENYWRITE
92: #define MAP_DENYWRITE 0
93: #endif
94:
95: /* should probably go in elf.h */
96: #ifndef ELIBBAD
97: #define ELIBBAD 80
98: #endif
99:
100: #ifdef TARGET_I386
101:
1.1.1.2 root 102: #define ELF_PLATFORM get_elf_platform()
103:
104: static const char *get_elf_platform(void)
105: {
106: static char elf_platform[] = "i386";
1.1.1.7 root 107: int family = (thread_env->cpuid_version >> 8) & 0xff;
1.1.1.2 root 108: if (family > 6)
109: family = 6;
110: if (family >= 3)
111: elf_platform[1] = '0' + family;
112: return elf_platform;
113: }
114:
115: #define ELF_HWCAP get_elf_hwcap()
116:
117: static uint32_t get_elf_hwcap(void)
118: {
1.1.1.7 root 119: return thread_env->cpuid_features;
1.1.1.2 root 120: }
121:
1.1.1.6 root 122: #ifdef TARGET_X86_64
123: #define ELF_START_MMAP 0x2aaaaab000ULL
124: #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
125:
126: #define ELF_CLASS ELFCLASS64
127: #define ELF_DATA ELFDATA2LSB
128: #define ELF_ARCH EM_X86_64
129:
130: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
131: {
132: regs->rax = 0;
133: regs->rsp = infop->start_stack;
134: regs->rip = infop->entry;
135: }
136:
1.1.1.9 root 137: typedef target_ulong target_elf_greg_t;
1.1.1.8 root 138: typedef uint32_t target_uid_t;
139: typedef uint32_t target_gid_t;
140: typedef int32_t target_pid_t;
141:
142: #define ELF_NREG 27
1.1.1.9 root 143: typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1.1.1.8 root 144:
145: /*
146: * Note that ELF_NREG should be 29 as there should be place for
147: * TRAPNO and ERR "registers" as well but linux doesn't dump
148: * those.
149: *
150: * See linux kernel: arch/x86/include/asm/elf.h
151: */
1.1.1.9 root 152: static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
1.1.1.8 root 153: {
154: (*regs)[0] = env->regs[15];
155: (*regs)[1] = env->regs[14];
156: (*regs)[2] = env->regs[13];
157: (*regs)[3] = env->regs[12];
158: (*regs)[4] = env->regs[R_EBP];
159: (*regs)[5] = env->regs[R_EBX];
160: (*regs)[6] = env->regs[11];
161: (*regs)[7] = env->regs[10];
162: (*regs)[8] = env->regs[9];
163: (*regs)[9] = env->regs[8];
164: (*regs)[10] = env->regs[R_EAX];
165: (*regs)[11] = env->regs[R_ECX];
166: (*regs)[12] = env->regs[R_EDX];
167: (*regs)[13] = env->regs[R_ESI];
168: (*regs)[14] = env->regs[R_EDI];
169: (*regs)[15] = env->regs[R_EAX]; /* XXX */
170: (*regs)[16] = env->eip;
171: (*regs)[17] = env->segs[R_CS].selector & 0xffff;
172: (*regs)[18] = env->eflags;
173: (*regs)[19] = env->regs[R_ESP];
174: (*regs)[20] = env->segs[R_SS].selector & 0xffff;
175: (*regs)[21] = env->segs[R_FS].selector & 0xffff;
176: (*regs)[22] = env->segs[R_GS].selector & 0xffff;
177: (*regs)[23] = env->segs[R_DS].selector & 0xffff;
178: (*regs)[24] = env->segs[R_ES].selector & 0xffff;
179: (*regs)[25] = env->segs[R_FS].selector & 0xffff;
180: (*regs)[26] = env->segs[R_GS].selector & 0xffff;
181: }
182:
1.1.1.6 root 183: #else
184:
1.1 root 185: #define ELF_START_MMAP 0x80000000
186:
187: /*
188: * This is used to ensure we don't load something for the wrong architecture.
189: */
190: #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
191:
192: /*
193: * These are used to set parameters in the core dumps.
194: */
195: #define ELF_CLASS ELFCLASS32
196: #define ELF_DATA ELFDATA2LSB
197: #define ELF_ARCH EM_386
198:
199: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
200: {
201: regs->esp = infop->start_stack;
202: regs->eip = infop->entry;
1.1.1.4 root 203:
204: /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
205: starts %edx contains a pointer to a function which might be
206: registered using `atexit'. This provides a mean for the
207: dynamic linker to call DT_FINI functions for shared libraries
208: that have been loaded before the code runs.
209:
210: A value of 0 tells we have no such handler. */
211: regs->edx = 0;
1.1 root 212: }
1.1.1.8 root 213:
1.1.1.9 root 214: typedef target_ulong target_elf_greg_t;
1.1.1.8 root 215: typedef uint16_t target_uid_t;
216: typedef uint16_t target_gid_t;
217: typedef int32_t target_pid_t;
218:
219: #define ELF_NREG 17
1.1.1.9 root 220: typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1.1.1.8 root 221:
222: /*
223: * Note that ELF_NREG should be 19 as there should be place for
224: * TRAPNO and ERR "registers" as well but linux doesn't dump
225: * those.
226: *
227: * See linux kernel: arch/x86/include/asm/elf.h
228: */
1.1.1.9 root 229: static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
1.1.1.8 root 230: {
231: (*regs)[0] = env->regs[R_EBX];
232: (*regs)[1] = env->regs[R_ECX];
233: (*regs)[2] = env->regs[R_EDX];
234: (*regs)[3] = env->regs[R_ESI];
235: (*regs)[4] = env->regs[R_EDI];
236: (*regs)[5] = env->regs[R_EBP];
237: (*regs)[6] = env->regs[R_EAX];
238: (*regs)[7] = env->segs[R_DS].selector & 0xffff;
239: (*regs)[8] = env->segs[R_ES].selector & 0xffff;
240: (*regs)[9] = env->segs[R_FS].selector & 0xffff;
241: (*regs)[10] = env->segs[R_GS].selector & 0xffff;
242: (*regs)[11] = env->regs[R_EAX]; /* XXX */
243: (*regs)[12] = env->eip;
244: (*regs)[13] = env->segs[R_CS].selector & 0xffff;
245: (*regs)[14] = env->eflags;
246: (*regs)[15] = env->regs[R_ESP];
247: (*regs)[16] = env->segs[R_SS].selector & 0xffff;
248: }
1.1.1.6 root 249: #endif
1.1 root 250:
251: #define USE_ELF_CORE_DUMP
252: #define ELF_EXEC_PAGESIZE 4096
253:
254: #endif
255:
256: #ifdef TARGET_ARM
257:
258: #define ELF_START_MMAP 0x80000000
259:
260: #define elf_check_arch(x) ( (x) == EM_ARM )
261:
262: #define ELF_CLASS ELFCLASS32
263: #ifdef TARGET_WORDS_BIGENDIAN
264: #define ELF_DATA ELFDATA2MSB
265: #else
266: #define ELF_DATA ELFDATA2LSB
267: #endif
268: #define ELF_ARCH EM_ARM
269:
270: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
271: {
1.1.1.6 root 272: abi_long stack = infop->start_stack;
1.1 root 273: memset(regs, 0, sizeof(*regs));
274: regs->ARM_cpsr = 0x10;
1.1.1.3 root 275: if (infop->entry & 1)
276: regs->ARM_cpsr |= CPSR_T;
277: regs->ARM_pc = infop->entry & 0xfffffffe;
1.1 root 278: regs->ARM_sp = infop->start_stack;
1.1.1.6 root 279: /* FIXME - what to for failure of get_user()? */
280: get_user_ual(regs->ARM_r2, stack + 8); /* envp */
281: get_user_ual(regs->ARM_r1, stack + 4); /* envp */
1.1 root 282: /* XXX: it seems that r0 is zeroed after ! */
1.1.1.4 root 283: regs->ARM_r0 = 0;
284: /* For uClinux PIC binaries. */
1.1.1.6 root 285: /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
1.1.1.4 root 286: regs->ARM_r10 = infop->start_data;
1.1 root 287: }
288:
1.1.1.9 root 289: typedef uint32_t target_elf_greg_t;
1.1.1.8 root 290: typedef uint16_t target_uid_t;
291: typedef uint16_t target_gid_t;
292: typedef int32_t target_pid_t;
293:
294: #define ELF_NREG 18
1.1.1.9 root 295: typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1.1.1.8 root 296:
1.1.1.9 root 297: static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
1.1.1.8 root 298: {
299: (*regs)[0] = env->regs[0];
300: (*regs)[1] = env->regs[1];
301: (*regs)[2] = env->regs[2];
302: (*regs)[3] = env->regs[3];
303: (*regs)[4] = env->regs[4];
304: (*regs)[5] = env->regs[5];
305: (*regs)[6] = env->regs[6];
306: (*regs)[7] = env->regs[7];
307: (*regs)[8] = env->regs[8];
308: (*regs)[9] = env->regs[9];
309: (*regs)[10] = env->regs[10];
310: (*regs)[11] = env->regs[11];
311: (*regs)[12] = env->regs[12];
312: (*regs)[13] = env->regs[13];
313: (*regs)[14] = env->regs[14];
314: (*regs)[15] = env->regs[15];
315:
316: (*regs)[16] = cpsr_read((CPUState *)env);
317: (*regs)[17] = env->regs[0]; /* XXX */
318: }
319:
1.1 root 320: #define USE_ELF_CORE_DUMP
321: #define ELF_EXEC_PAGESIZE 4096
322:
1.1.1.2 root 323: enum
324: {
325: ARM_HWCAP_ARM_SWP = 1 << 0,
326: ARM_HWCAP_ARM_HALF = 1 << 1,
327: ARM_HWCAP_ARM_THUMB = 1 << 2,
328: ARM_HWCAP_ARM_26BIT = 1 << 3,
329: ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
330: ARM_HWCAP_ARM_FPA = 1 << 5,
331: ARM_HWCAP_ARM_VFP = 1 << 6,
332: ARM_HWCAP_ARM_EDSP = 1 << 7,
1.1.1.10! root 333: ARM_HWCAP_ARM_JAVA = 1 << 8,
! 334: ARM_HWCAP_ARM_IWMMXT = 1 << 9,
! 335: ARM_HWCAP_ARM_THUMBEE = 1 << 10,
! 336: ARM_HWCAP_ARM_NEON = 1 << 11,
! 337: ARM_HWCAP_ARM_VFPv3 = 1 << 12,
! 338: ARM_HWCAP_ARM_VFPv3D16 = 1 << 13,
1.1.1.2 root 339: };
340:
341: #define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF \
342: | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT \
1.1.1.10! root 343: | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP \
! 344: | ARM_HWCAP_ARM_NEON | ARM_HWCAP_ARM_VFPv3 )
1.1.1.2 root 345:
1.1 root 346: #endif
347:
348: #ifdef TARGET_SPARC
349: #ifdef TARGET_SPARC64
350:
351: #define ELF_START_MMAP 0x80000000
352:
1.1.1.6 root 353: #ifndef TARGET_ABI32
354: #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
355: #else
356: #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
357: #endif
1.1 root 358:
359: #define ELF_CLASS ELFCLASS64
360: #define ELF_DATA ELFDATA2MSB
1.1.1.4 root 361: #define ELF_ARCH EM_SPARCV9
1.1 root 362:
1.1.1.4 root 363: #define STACK_BIAS 2047
1.1 root 364:
365: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
366: {
1.1.1.6 root 367: #ifndef TARGET_ABI32
1.1 root 368: regs->tstate = 0;
1.1.1.6 root 369: #endif
1.1 root 370: regs->pc = infop->entry;
371: regs->npc = regs->pc + 4;
372: regs->y = 0;
1.1.1.6 root 373: #ifdef TARGET_ABI32
374: regs->u_regs[14] = infop->start_stack - 16 * 4;
375: #else
376: if (personality(infop->personality) == PER_LINUX32)
377: regs->u_regs[14] = infop->start_stack - 16 * 4;
378: else
379: regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
380: #endif
1.1 root 381: }
382:
383: #else
384: #define ELF_START_MMAP 0x80000000
385:
386: #define elf_check_arch(x) ( (x) == EM_SPARC )
387:
388: #define ELF_CLASS ELFCLASS32
389: #define ELF_DATA ELFDATA2MSB
390: #define ELF_ARCH EM_SPARC
391:
392: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
393: {
394: regs->psr = 0;
395: regs->pc = infop->entry;
396: regs->npc = regs->pc + 4;
397: regs->y = 0;
398: regs->u_regs[14] = infop->start_stack - 16 * 4;
399: }
400:
401: #endif
402: #endif
403:
404: #ifdef TARGET_PPC
405:
406: #define ELF_START_MMAP 0x80000000
407:
1.1.1.6 root 408: #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
409:
410: #define elf_check_arch(x) ( (x) == EM_PPC64 )
411:
412: #define ELF_CLASS ELFCLASS64
413:
414: #else
415:
1.1 root 416: #define elf_check_arch(x) ( (x) == EM_PPC )
417:
418: #define ELF_CLASS ELFCLASS32
1.1.1.6 root 419:
420: #endif
421:
1.1 root 422: #ifdef TARGET_WORDS_BIGENDIAN
423: #define ELF_DATA ELFDATA2MSB
424: #else
425: #define ELF_DATA ELFDATA2LSB
426: #endif
427: #define ELF_ARCH EM_PPC
428:
1.1.1.8 root 429: /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
430: See arch/powerpc/include/asm/cputable.h. */
431: enum {
1.1.1.10! root 432: QEMU_PPC_FEATURE_32 = 0x80000000,
! 433: QEMU_PPC_FEATURE_64 = 0x40000000,
! 434: QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
! 435: QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
! 436: QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
! 437: QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
! 438: QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
! 439: QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
! 440: QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
! 441: QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
! 442: QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
! 443: QEMU_PPC_FEATURE_NO_TB = 0x00100000,
! 444: QEMU_PPC_FEATURE_POWER4 = 0x00080000,
! 445: QEMU_PPC_FEATURE_POWER5 = 0x00040000,
! 446: QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
! 447: QEMU_PPC_FEATURE_CELL = 0x00010000,
! 448: QEMU_PPC_FEATURE_BOOKE = 0x00008000,
! 449: QEMU_PPC_FEATURE_SMT = 0x00004000,
! 450: QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
! 451: QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
! 452: QEMU_PPC_FEATURE_PA6T = 0x00000800,
! 453: QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
! 454: QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
! 455: QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
! 456: QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
! 457: QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
1.1.1.8 root 458:
1.1.1.10! root 459: QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
! 460: QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
1.1.1.8 root 461: };
462:
463: #define ELF_HWCAP get_elf_hwcap()
464:
465: static uint32_t get_elf_hwcap(void)
466: {
467: CPUState *e = thread_env;
468: uint32_t features = 0;
469:
470: /* We don't have to be terribly complete here; the high points are
471: Altivec/FP/SPE support. Anything else is just a bonus. */
472: #define GET_FEATURE(flag, feature) \
473: do {if (e->insns_flags & flag) features |= feature; } while(0)
1.1.1.10! root 474: GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
! 475: GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
! 476: GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
! 477: GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
! 478: GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
! 479: GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
! 480: GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
! 481: GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
1.1.1.8 root 482: #undef GET_FEATURE
483:
484: return features;
485: }
486:
1.1 root 487: /*
488: * We need to put in some extra aux table entries to tell glibc what
489: * the cache block size is, so it can use the dcbz instruction safely.
490: */
491: #define AT_DCACHEBSIZE 19
492: #define AT_ICACHEBSIZE 20
493: #define AT_UCACHEBSIZE 21
494: /* A special ignored type value for PPC, for glibc compatibility. */
495: #define AT_IGNOREPPC 22
496: /*
497: * The requirements here are:
498: * - keep the final alignment of sp (sp & 0xf)
499: * - make sure the 32-bit value at the first 16 byte aligned position of
500: * AUXV is greater than 16 for glibc compatibility.
501: * AT_IGNOREPPC is used for that.
502: * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
503: * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
504: */
505: #define DLINFO_ARCH_ITEMS 5
506: #define ARCH_DLINFO \
507: do { \
508: NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
509: NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
510: NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
511: /* \
512: * Now handle glibc compatibility. \
513: */ \
514: NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
515: NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
516: } while (0)
517:
518: static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
519: {
1.1.1.6 root 520: abi_ulong pos = infop->start_stack;
521: abi_ulong tmp;
522: #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
523: abi_ulong entry, toc;
524: #endif
1.1.1.4 root 525:
1.1 root 526: _regs->gpr[1] = infop->start_stack;
1.1.1.6 root 527: #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
528: entry = ldq_raw(infop->entry) + infop->load_addr;
529: toc = ldq_raw(infop->entry + 8) + infop->load_addr;
530: _regs->gpr[2] = toc;
531: infop->entry = entry;
532: #endif
1.1 root 533: _regs->nip = infop->entry;
1.1.1.4 root 534: /* Note that isn't exactly what regular kernel does
535: * but this is what the ABI wants and is needed to allow
536: * execution of PPC BSD programs.
537: */
1.1.1.6 root 538: /* FIXME - what to for failure of get_user()? */
539: get_user_ual(_regs->gpr[3], pos);
540: pos += sizeof(abi_ulong);
1.1.1.4 root 541: _regs->gpr[4] = pos;
1.1.1.6 root 542: for (tmp = 1; tmp != 0; pos += sizeof(abi_ulong))
1.1.1.4 root 543: tmp = ldl(pos);
544: _regs->gpr[5] = pos;
1.1 root 545: }
546:
547: #define ELF_EXEC_PAGESIZE 4096
548:
549: #endif
550:
1.1.1.2 root 551: #ifdef TARGET_MIPS
552:
553: #define ELF_START_MMAP 0x80000000
554:
555: #define elf_check_arch(x) ( (x) == EM_MIPS )
556:
1.1.1.6 root 557: #ifdef TARGET_MIPS64
558: #define ELF_CLASS ELFCLASS64
559: #else
1.1.1.2 root 560: #define ELF_CLASS ELFCLASS32
1.1.1.6 root 561: #endif
1.1.1.2 root 562: #ifdef TARGET_WORDS_BIGENDIAN
563: #define ELF_DATA ELFDATA2MSB
564: #else
565: #define ELF_DATA ELFDATA2LSB
566: #endif
567: #define ELF_ARCH EM_MIPS
568:
569: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
570: {
1.1.1.6 root 571: regs->cp0_status = 2 << CP0St_KSU;
1.1.1.2 root 572: regs->cp0_epc = infop->entry;
573: regs->regs[29] = infop->start_stack;
574: }
575:
1.1.1.6 root 576: #define ELF_EXEC_PAGESIZE 4096
577:
1.1.1.2 root 578: #endif /* TARGET_MIPS */
579:
1.1.1.8 root 580: #ifdef TARGET_MICROBLAZE
581:
582: #define ELF_START_MMAP 0x80000000
583:
584: #define elf_check_arch(x) ( (x) == EM_XILINX_MICROBLAZE )
585:
586: #define ELF_CLASS ELFCLASS32
587: #define ELF_DATA ELFDATA2MSB
588: #define ELF_ARCH EM_MIPS
589:
590: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
591: {
592: regs->pc = infop->entry;
593: regs->r1 = infop->start_stack;
594:
595: }
596:
597: #define ELF_EXEC_PAGESIZE 4096
598:
599: #endif /* TARGET_MICROBLAZE */
600:
1.1.1.3 root 601: #ifdef TARGET_SH4
602:
603: #define ELF_START_MMAP 0x80000000
604:
605: #define elf_check_arch(x) ( (x) == EM_SH )
606:
607: #define ELF_CLASS ELFCLASS32
608: #define ELF_DATA ELFDATA2LSB
609: #define ELF_ARCH EM_SH
610:
611: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
612: {
613: /* Check other registers XXXXX */
614: regs->pc = infop->entry;
1.1.1.6 root 615: regs->regs[15] = infop->start_stack;
1.1.1.3 root 616: }
617:
618: #define ELF_EXEC_PAGESIZE 4096
619:
620: #endif
621:
1.1.1.6 root 622: #ifdef TARGET_CRIS
623:
624: #define ELF_START_MMAP 0x80000000
625:
626: #define elf_check_arch(x) ( (x) == EM_CRIS )
627:
628: #define ELF_CLASS ELFCLASS32
629: #define ELF_DATA ELFDATA2LSB
630: #define ELF_ARCH EM_CRIS
631:
632: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
633: {
634: regs->erp = infop->entry;
635: }
636:
637: #define ELF_EXEC_PAGESIZE 8192
638:
639: #endif
640:
1.1.1.5 root 641: #ifdef TARGET_M68K
642:
643: #define ELF_START_MMAP 0x80000000
644:
645: #define elf_check_arch(x) ( (x) == EM_68K )
646:
647: #define ELF_CLASS ELFCLASS32
648: #define ELF_DATA ELFDATA2MSB
649: #define ELF_ARCH EM_68K
650:
651: /* ??? Does this need to do anything?
652: #define ELF_PLAT_INIT(_r) */
653:
654: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
655: {
656: regs->usp = infop->start_stack;
657: regs->sr = 0;
658: regs->pc = infop->entry;
659: }
660:
661: #define ELF_EXEC_PAGESIZE 8192
662:
663: #endif
664:
1.1.1.6 root 665: #ifdef TARGET_ALPHA
666:
667: #define ELF_START_MMAP (0x30000000000ULL)
668:
669: #define elf_check_arch(x) ( (x) == ELF_ARCH )
670:
671: #define ELF_CLASS ELFCLASS64
672: #define ELF_DATA ELFDATA2MSB
673: #define ELF_ARCH EM_ALPHA
674:
675: static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
676: {
677: regs->pc = infop->entry;
678: regs->ps = 8;
679: regs->usp = infop->start_stack;
680: regs->unique = infop->start_data; /* ? */
681: printf("Set unique value to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n",
682: regs->unique, infop->start_data);
683: }
684:
685: #define ELF_EXEC_PAGESIZE 8192
686:
687: #endif /* TARGET_ALPHA */
688:
1.1.1.2 root 689: #ifndef ELF_PLATFORM
690: #define ELF_PLATFORM (NULL)
691: #endif
692:
693: #ifndef ELF_HWCAP
694: #define ELF_HWCAP 0
695: #endif
696:
1.1.1.6 root 697: #ifdef TARGET_ABI32
698: #undef ELF_CLASS
699: #define ELF_CLASS ELFCLASS32
700: #undef bswaptls
701: #define bswaptls(ptr) bswap32s(ptr)
702: #endif
703:
1.1 root 704: #include "elf.h"
705:
706: struct exec
707: {
708: unsigned int a_info; /* Use macros N_MAGIC, etc for access */
709: unsigned int a_text; /* length of text, in bytes */
710: unsigned int a_data; /* length of data, in bytes */
711: unsigned int a_bss; /* length of uninitialized data area, in bytes */
712: unsigned int a_syms; /* length of symbol table data in file, in bytes */
713: unsigned int a_entry; /* start address */
714: unsigned int a_trsize; /* length of relocation info for text, in bytes */
715: unsigned int a_drsize; /* length of relocation info for data, in bytes */
716: };
717:
718:
719: #define N_MAGIC(exec) ((exec).a_info & 0xffff)
720: #define OMAGIC 0407
721: #define NMAGIC 0410
722: #define ZMAGIC 0413
723: #define QMAGIC 0314
724:
725: /* max code+data+bss space allocated to elf interpreter */
726: #define INTERP_MAP_SIZE (32 * 1024 * 1024)
727:
728: /* max code+data+bss+brk space allocated to ET_DYN executables */
729: #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
730:
731: /* Necessary parameters */
732: #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
733: #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
734: #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
735:
736: #define INTERPRETER_NONE 0
737: #define INTERPRETER_AOUT 1
738: #define INTERPRETER_ELF 2
739:
1.1.1.2 root 740: #define DLINFO_ITEMS 12
1.1 root 741:
742: static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
743: {
744: memcpy(to, from, n);
745: }
746:
747: static int load_aout_interp(void * exptr, int interp_fd);
748:
749: #ifdef BSWAP_NEEDED
750: static void bswap_ehdr(struct elfhdr *ehdr)
751: {
752: bswap16s(&ehdr->e_type); /* Object file type */
753: bswap16s(&ehdr->e_machine); /* Architecture */
754: bswap32s(&ehdr->e_version); /* Object file version */
755: bswaptls(&ehdr->e_entry); /* Entry point virtual address */
756: bswaptls(&ehdr->e_phoff); /* Program header table file offset */
757: bswaptls(&ehdr->e_shoff); /* Section header table file offset */
758: bswap32s(&ehdr->e_flags); /* Processor-specific flags */
759: bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
760: bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
761: bswap16s(&ehdr->e_phnum); /* Program header table entry count */
762: bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
763: bswap16s(&ehdr->e_shnum); /* Section header table entry count */
764: bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
765: }
766:
767: static void bswap_phdr(struct elf_phdr *phdr)
768: {
769: bswap32s(&phdr->p_type); /* Segment type */
770: bswaptls(&phdr->p_offset); /* Segment file offset */
771: bswaptls(&phdr->p_vaddr); /* Segment virtual address */
772: bswaptls(&phdr->p_paddr); /* Segment physical address */
773: bswaptls(&phdr->p_filesz); /* Segment size in file */
774: bswaptls(&phdr->p_memsz); /* Segment size in memory */
775: bswap32s(&phdr->p_flags); /* Segment flags */
776: bswaptls(&phdr->p_align); /* Segment alignment */
777: }
778:
779: static void bswap_shdr(struct elf_shdr *shdr)
780: {
781: bswap32s(&shdr->sh_name);
782: bswap32s(&shdr->sh_type);
783: bswaptls(&shdr->sh_flags);
784: bswaptls(&shdr->sh_addr);
785: bswaptls(&shdr->sh_offset);
786: bswaptls(&shdr->sh_size);
787: bswap32s(&shdr->sh_link);
788: bswap32s(&shdr->sh_info);
789: bswaptls(&shdr->sh_addralign);
790: bswaptls(&shdr->sh_entsize);
791: }
792:
1.1.1.6 root 793: static void bswap_sym(struct elf_sym *sym)
1.1 root 794: {
795: bswap32s(&sym->st_name);
1.1.1.6 root 796: bswaptls(&sym->st_value);
797: bswaptls(&sym->st_size);
1.1 root 798: bswap16s(&sym->st_shndx);
799: }
800: #endif
801:
1.1.1.8 root 802: #ifdef USE_ELF_CORE_DUMP
803: static int elf_core_dump(int, const CPUState *);
804:
805: #ifdef BSWAP_NEEDED
806: static void bswap_note(struct elf_note *en)
807: {
1.1.1.10! root 808: bswap32s(&en->n_namesz);
! 809: bswap32s(&en->n_descsz);
! 810: bswap32s(&en->n_type);
1.1.1.8 root 811: }
812: #endif /* BSWAP_NEEDED */
813:
814: #endif /* USE_ELF_CORE_DUMP */
815:
1.1 root 816: /*
1.1.1.4 root 817: * 'copy_elf_strings()' copies argument/envelope strings from user
1.1 root 818: * memory to free pages in kernel mem. These are in a format ready
819: * to be put directly into the top of new user memory.
820: *
821: */
1.1.1.6 root 822: static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
823: abi_ulong p)
1.1 root 824: {
825: char *tmp, *tmp1, *pag = NULL;
826: int len, offset = 0;
827:
828: if (!p) {
829: return 0; /* bullet-proofing */
830: }
831: while (argc-- > 0) {
832: tmp = argv[argc];
833: if (!tmp) {
834: fprintf(stderr, "VFS: argc is wrong");
835: exit(-1);
836: }
837: tmp1 = tmp;
838: while (*tmp++);
839: len = tmp - tmp1;
840: if (p < len) { /* this shouldn't happen - 128kB */
841: return 0;
842: }
843: while (len) {
844: --p; --tmp; --len;
845: if (--offset < 0) {
846: offset = p % TARGET_PAGE_SIZE;
1.1.1.3 root 847: pag = (char *)page[p/TARGET_PAGE_SIZE];
1.1 root 848: if (!pag) {
1.1.1.3 root 849: pag = (char *)malloc(TARGET_PAGE_SIZE);
1.1.1.6 root 850: memset(pag, 0, TARGET_PAGE_SIZE);
1.1.1.3 root 851: page[p/TARGET_PAGE_SIZE] = pag;
1.1 root 852: if (!pag)
853: return 0;
854: }
855: }
856: if (len == 0 || offset == 0) {
857: *(pag + offset) = *tmp;
858: }
859: else {
860: int bytes_to_copy = (len > offset) ? offset : len;
861: tmp -= bytes_to_copy;
862: p -= bytes_to_copy;
863: offset -= bytes_to_copy;
864: len -= bytes_to_copy;
865: memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
866: }
867: }
868: }
869: return p;
870: }
871:
1.1.1.6 root 872: static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
873: struct image_info *info)
1.1 root 874: {
1.1.1.6 root 875: abi_ulong stack_base, size, error;
1.1 root 876: int i;
877:
878: /* Create enough stack to hold everything. If we don't use
879: * it for args, we'll use it for something else...
880: */
881: size = x86_stack_size;
882: if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
883: size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1.1.1.6 root 884: error = target_mmap(0,
1.1 root 885: size + qemu_host_page_size,
886: PROT_READ | PROT_WRITE,
887: MAP_PRIVATE | MAP_ANONYMOUS,
888: -1, 0);
889: if (error == -1) {
890: perror("stk mmap");
891: exit(-1);
892: }
893: /* we reserve one extra page at the top of the stack as guard */
894: target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
895:
896: stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
897: p += stack_base;
898:
899: for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
900: if (bprm->page[i]) {
901: info->rss++;
1.1.1.6 root 902: /* FIXME - check return value of memcpy_to_target() for failure */
1.1.1.3 root 903: memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
904: free(bprm->page[i]);
1.1 root 905: }
1.1.1.3 root 906: stack_base += TARGET_PAGE_SIZE;
1.1 root 907: }
908: return p;
909: }
910:
1.1.1.6 root 911: static void set_brk(abi_ulong start, abi_ulong end)
1.1 root 912: {
913: /* page-align the start and end addresses... */
914: start = HOST_PAGE_ALIGN(start);
915: end = HOST_PAGE_ALIGN(end);
916: if (end <= start)
917: return;
918: if(target_mmap(start, end - start,
919: PROT_READ | PROT_WRITE | PROT_EXEC,
920: MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
921: perror("cannot mmap brk");
922: exit(-1);
923: }
924: }
925:
926:
927: /* We need to explicitly zero any fractional pages after the data
928: section (i.e. bss). This would contain the junk from the file that
929: should not be in memory. */
1.1.1.6 root 930: static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
1.1 root 931: {
1.1.1.6 root 932: abi_ulong nbyte;
1.1 root 933:
1.1.1.5 root 934: if (elf_bss >= last_bss)
935: return;
936:
1.1 root 937: /* XXX: this is really a hack : if the real host page size is
938: smaller than the target page size, some pages after the end
939: of the file may not be mapped. A better fix would be to
940: patch target_mmap(), but it is more complicated as the file
941: size must be known */
942: if (qemu_real_host_page_size < qemu_host_page_size) {
1.1.1.6 root 943: abi_ulong end_addr, end_addr1;
944: end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
1.1 root 945: ~(qemu_real_host_page_size - 1);
946: end_addr = HOST_PAGE_ALIGN(elf_bss);
947: if (end_addr1 < end_addr) {
1.1.1.6 root 948: mmap((void *)g2h(end_addr1), end_addr - end_addr1,
1.1 root 949: PROT_READ|PROT_WRITE|PROT_EXEC,
950: MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
951: }
952: }
953:
954: nbyte = elf_bss & (qemu_host_page_size-1);
955: if (nbyte) {
956: nbyte = qemu_host_page_size - nbyte;
957: do {
1.1.1.6 root 958: /* FIXME - what to do if put_user() fails? */
959: put_user_u8(0, elf_bss);
1.1.1.3 root 960: elf_bss++;
1.1 root 961: } while (--nbyte);
962: }
963: }
964:
1.1.1.3 root 965:
1.1.1.6 root 966: static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
967: struct elfhdr * exec,
968: abi_ulong load_addr,
969: abi_ulong load_bias,
970: abi_ulong interp_load_addr, int ibcs,
971: struct image_info *info)
1.1.1.3 root 972: {
1.1.1.6 root 973: abi_ulong sp;
1.1.1.3 root 974: int size;
1.1.1.6 root 975: abi_ulong u_platform;
1.1.1.2 root 976: const char *k_platform;
1.1.1.6 root 977: const int n = sizeof(elf_addr_t);
1.1 root 978:
1.1.1.3 root 979: sp = p;
980: u_platform = 0;
1.1.1.2 root 981: k_platform = ELF_PLATFORM;
982: if (k_platform) {
983: size_t len = strlen(k_platform) + 1;
1.1.1.3 root 984: sp -= (len + n - 1) & ~(n - 1);
985: u_platform = sp;
1.1.1.6 root 986: /* FIXME - check return value of memcpy_to_target() for failure */
1.1.1.3 root 987: memcpy_to_target(sp, k_platform, len);
1.1.1.2 root 988: }
1.1.1.3 root 989: /*
990: * Force 16 byte _final_ alignment here for generality.
991: */
1.1.1.6 root 992: sp = sp &~ (abi_ulong)15;
1.1.1.3 root 993: size = (DLINFO_ITEMS + 1) * 2;
1.1.1.2 root 994: if (k_platform)
1.1.1.3 root 995: size += 2;
1.1 root 996: #ifdef DLINFO_ARCH_ITEMS
1.1.1.3 root 997: size += DLINFO_ARCH_ITEMS * 2;
1.1 root 998: #endif
1.1.1.3 root 999: size += envc + argc + 2;
1000: size += (!ibcs ? 3 : 1); /* argc itself */
1001: size *= n;
1002: if (size & 15)
1003: sp -= 16 - (size & 15);
1.1.1.6 root 1004:
1005: /* This is correct because Linux defines
1006: * elf_addr_t as Elf32_Off / Elf64_Off
1007: */
1008: #define NEW_AUX_ENT(id, val) do { \
1009: sp -= n; put_user_ual(val, sp); \
1010: sp -= n; put_user_ual(id, sp); \
1.1.1.3 root 1011: } while(0)
1.1.1.6 root 1012:
1.1 root 1013: NEW_AUX_ENT (AT_NULL, 0);
1014:
1015: /* There must be exactly DLINFO_ITEMS entries here. */
1.1.1.6 root 1016: NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
1017: NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1018: NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1019: NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
1020: NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
1021: NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1.1 root 1022: NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
1.1.1.6 root 1023: NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1024: NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1025: NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1026: NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1027: NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1.1.1.7 root 1028: NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1.1.1.2 root 1029: if (k_platform)
1.1.1.3 root 1030: NEW_AUX_ENT(AT_PLATFORM, u_platform);
1.1 root 1031: #ifdef ARCH_DLINFO
1.1.1.6 root 1032: /*
1.1 root 1033: * ARCH_DLINFO must come last so platform specific code can enforce
1034: * special alignment requirements on the AUXV if necessary (eg. PPC).
1035: */
1036: ARCH_DLINFO;
1037: #endif
1038: #undef NEW_AUX_ENT
1039:
1.1.1.8 root 1040: info->saved_auxv = sp;
1041:
1.1.1.4 root 1042: sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
1.1 root 1043: return sp;
1044: }
1045:
1046:
1.1.1.6 root 1047: static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
1048: int interpreter_fd,
1049: abi_ulong *interp_load_addr)
1.1 root 1050: {
1051: struct elf_phdr *elf_phdata = NULL;
1052: struct elf_phdr *eppnt;
1.1.1.6 root 1053: abi_ulong load_addr = 0;
1.1 root 1054: int load_addr_set = 0;
1055: int retval;
1.1.1.6 root 1056: abi_ulong last_bss, elf_bss;
1057: abi_ulong error;
1.1 root 1058: int i;
1.1.1.6 root 1059:
1.1 root 1060: elf_bss = 0;
1061: last_bss = 0;
1062: error = 0;
1063:
1064: #ifdef BSWAP_NEEDED
1065: bswap_ehdr(interp_elf_ex);
1066: #endif
1067: /* First of all, some simple consistency checks */
1.1.1.6 root 1068: if ((interp_elf_ex->e_type != ET_EXEC &&
1069: interp_elf_ex->e_type != ET_DYN) ||
1.1 root 1070: !elf_check_arch(interp_elf_ex->e_machine)) {
1.1.1.6 root 1071: return ~((abi_ulong)0UL);
1.1 root 1072: }
1.1.1.6 root 1073:
1.1 root 1074:
1075: /* Now read in all of the header information */
1.1.1.6 root 1076:
1.1 root 1077: if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
1.1.1.6 root 1078: return ~(abi_ulong)0UL;
1079:
1080: elf_phdata = (struct elf_phdr *)
1.1 root 1081: malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1082:
1083: if (!elf_phdata)
1.1.1.6 root 1084: return ~((abi_ulong)0UL);
1085:
1.1 root 1086: /*
1087: * If the size of this structure has changed, then punt, since
1088: * we will be doing the wrong thing.
1089: */
1090: if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
1091: free(elf_phdata);
1.1.1.6 root 1092: return ~((abi_ulong)0UL);
1.1 root 1093: }
1094:
1095: retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
1096: if(retval >= 0) {
1097: retval = read(interpreter_fd,
1098: (char *) elf_phdata,
1099: sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1100: }
1101: if (retval < 0) {
1102: perror("load_elf_interp");
1103: exit(-1);
1104: free (elf_phdata);
1105: return retval;
1106: }
1107: #ifdef BSWAP_NEEDED
1108: eppnt = elf_phdata;
1109: for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
1110: bswap_phdr(eppnt);
1111: }
1112: #endif
1113:
1114: if (interp_elf_ex->e_type == ET_DYN) {
1.1.1.6 root 1115: /* in order to avoid hardcoding the interpreter load
1.1 root 1116: address in qemu, we allocate a big enough memory zone */
1117: error = target_mmap(0, INTERP_MAP_SIZE,
1.1.1.6 root 1118: PROT_NONE, MAP_PRIVATE | MAP_ANON,
1.1 root 1119: -1, 0);
1120: if (error == -1) {
1121: perror("mmap");
1122: exit(-1);
1123: }
1124: load_addr = error;
1125: load_addr_set = 1;
1126: }
1127:
1128: eppnt = elf_phdata;
1129: for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
1130: if (eppnt->p_type == PT_LOAD) {
1131: int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
1132: int elf_prot = 0;
1.1.1.6 root 1133: abi_ulong vaddr = 0;
1134: abi_ulong k;
1.1 root 1135:
1136: if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1137: if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1138: if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1139: if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
1140: elf_type |= MAP_FIXED;
1141: vaddr = eppnt->p_vaddr;
1142: }
1143: error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
1144: eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
1145: elf_prot,
1146: elf_type,
1147: interpreter_fd,
1148: eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
1.1.1.6 root 1149:
1.1.1.3 root 1150: if (error == -1) {
1.1 root 1151: /* Real error */
1152: close(interpreter_fd);
1153: free(elf_phdata);
1.1.1.6 root 1154: return ~((abi_ulong)0UL);
1.1 root 1155: }
1156:
1157: if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
1158: load_addr = error;
1159: load_addr_set = 1;
1160: }
1161:
1162: /*
1163: * Find the end of the file mapping for this phdr, and keep
1164: * track of the largest address we see for this.
1165: */
1166: k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
1167: if (k > elf_bss) elf_bss = k;
1168:
1169: /*
1170: * Do the same thing for the memory mapping - between
1171: * elf_bss and last_bss is the bss section.
1172: */
1173: k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
1174: if (k > last_bss) last_bss = k;
1175: }
1.1.1.6 root 1176:
1.1 root 1177: /* Now use mmap to map the library into memory. */
1178:
1179: close(interpreter_fd);
1180:
1181: /*
1182: * Now fill out the bss section. First pad the last page up
1183: * to the page boundary, and then perform a mmap to make sure
1184: * that there are zeromapped pages up to and including the last
1185: * bss page.
1186: */
1.1.1.5 root 1187: padzero(elf_bss, last_bss);
1.1 root 1188: elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
1189:
1190: /* Map the last of the bss segment */
1191: if (last_bss > elf_bss) {
1192: target_mmap(elf_bss, last_bss-elf_bss,
1193: PROT_READ|PROT_WRITE|PROT_EXEC,
1194: MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1195: }
1196: free(elf_phdata);
1197:
1198: *interp_load_addr = load_addr;
1.1.1.6 root 1199: return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
1.1 root 1200: }
1201:
1.1.1.7 root 1202: static int symfind(const void *s0, const void *s1)
1203: {
1204: struct elf_sym *key = (struct elf_sym *)s0;
1205: struct elf_sym *sym = (struct elf_sym *)s1;
1206: int result = 0;
1207: if (key->st_value < sym->st_value) {
1208: result = -1;
1.1.1.8 root 1209: } else if (key->st_value >= sym->st_value + sym->st_size) {
1.1.1.7 root 1210: result = 1;
1211: }
1212: return result;
1213: }
1214:
1215: static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
1216: {
1217: #if ELF_CLASS == ELFCLASS32
1218: struct elf_sym *syms = s->disas_symtab.elf32;
1219: #else
1220: struct elf_sym *syms = s->disas_symtab.elf64;
1221: #endif
1222:
1223: // binary search
1224: struct elf_sym key;
1225: struct elf_sym *sym;
1226:
1227: key.st_value = orig_addr;
1228:
1229: sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), symfind);
1.1.1.8 root 1230: if (sym != NULL) {
1.1.1.7 root 1231: return s->disas_strtab + sym->st_name;
1232: }
1233:
1234: return "";
1235: }
1236:
1237: /* FIXME: This should use elf_ops.h */
1238: static int symcmp(const void *s0, const void *s1)
1239: {
1240: struct elf_sym *sym0 = (struct elf_sym *)s0;
1241: struct elf_sym *sym1 = (struct elf_sym *)s1;
1242: return (sym0->st_value < sym1->st_value)
1243: ? -1
1244: : ((sym0->st_value > sym1->st_value) ? 1 : 0);
1245: }
1246:
1.1 root 1247: /* Best attempt to load symbols from this ELF object. */
1248: static void load_symbols(struct elfhdr *hdr, int fd)
1249: {
1.1.1.7 root 1250: unsigned int i, nsyms;
1.1 root 1251: struct elf_shdr sechdr, symtab, strtab;
1252: char *strings;
1253: struct syminfo *s;
1.1.1.7 root 1254: struct elf_sym *syms;
1.1 root 1255:
1256: lseek(fd, hdr->e_shoff, SEEK_SET);
1257: for (i = 0; i < hdr->e_shnum; i++) {
1.1.1.7 root 1258: if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
1259: return;
1.1 root 1260: #ifdef BSWAP_NEEDED
1.1.1.7 root 1261: bswap_shdr(&sechdr);
1.1 root 1262: #endif
1.1.1.7 root 1263: if (sechdr.sh_type == SHT_SYMTAB) {
1264: symtab = sechdr;
1265: lseek(fd, hdr->e_shoff
1266: + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
1267: if (read(fd, &strtab, sizeof(strtab))
1268: != sizeof(strtab))
1269: return;
1.1 root 1270: #ifdef BSWAP_NEEDED
1.1.1.7 root 1271: bswap_shdr(&strtab);
1.1 root 1272: #endif
1.1.1.7 root 1273: goto found;
1274: }
1.1 root 1275: }
1276: return; /* Shouldn't happen... */
1277:
1278: found:
1279: /* Now know where the strtab and symtab are. Snarf them. */
1280: s = malloc(sizeof(*s));
1.1.1.7 root 1281: syms = malloc(symtab.sh_size);
1282: if (!syms)
1283: return;
1.1 root 1284: s->disas_strtab = strings = malloc(strtab.sh_size);
1.1.1.7 root 1285: if (!s->disas_strtab)
1286: return;
1.1.1.6 root 1287:
1.1 root 1288: lseek(fd, symtab.sh_offset, SEEK_SET);
1.1.1.7 root 1289: if (read(fd, syms, symtab.sh_size) != symtab.sh_size)
1290: return;
1291:
1292: nsyms = symtab.sh_size / sizeof(struct elf_sym);
1.1 root 1293:
1.1.1.7 root 1294: i = 0;
1295: while (i < nsyms) {
1.1 root 1296: #ifdef BSWAP_NEEDED
1.1.1.7 root 1297: bswap_sym(syms + i);
1.1 root 1298: #endif
1.1.1.7 root 1299: // Throw away entries which we do not need.
1300: if (syms[i].st_shndx == SHN_UNDEF ||
1301: syms[i].st_shndx >= SHN_LORESERVE ||
1302: ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
1303: nsyms--;
1304: if (i < nsyms) {
1305: syms[i] = syms[nsyms];
1306: }
1307: continue;
1308: }
1309: #if defined(TARGET_ARM) || defined (TARGET_MIPS)
1310: /* The bottom address bit marks a Thumb or MIPS16 symbol. */
1311: syms[i].st_value &= ~(target_ulong)1;
1.1.1.6 root 1312: #endif
1.1.1.7 root 1313: i++;
1.1.1.6 root 1314: }
1.1.1.7 root 1315: syms = realloc(syms, nsyms * sizeof(*syms));
1316:
1317: qsort(syms, nsyms, sizeof(*syms), symcmp);
1.1 root 1318:
1319: lseek(fd, strtab.sh_offset, SEEK_SET);
1320: if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
1.1.1.7 root 1321: return;
1322: s->disas_num_syms = nsyms;
1323: #if ELF_CLASS == ELFCLASS32
1324: s->disas_symtab.elf32 = syms;
1.1.1.10! root 1325: s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
1.1.1.7 root 1326: #else
1327: s->disas_symtab.elf64 = syms;
1.1.1.10! root 1328: s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
1.1.1.7 root 1329: #endif
1.1 root 1330: s->next = syminfos;
1331: syminfos = s;
1332: }
1333:
1.1.1.4 root 1334: int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
1335: struct image_info * info)
1.1 root 1336: {
1337: struct elfhdr elf_ex;
1338: struct elfhdr interp_elf_ex;
1339: struct exec interp_ex;
1340: int interpreter_fd = -1; /* avoid warning */
1.1.1.6 root 1341: abi_ulong load_addr, load_bias;
1.1 root 1342: int load_addr_set = 0;
1343: unsigned int interpreter_type = INTERPRETER_NONE;
1344: unsigned char ibcs2_interpreter;
1345: int i;
1.1.1.6 root 1346: abi_ulong mapped_addr;
1.1 root 1347: struct elf_phdr * elf_ppnt;
1348: struct elf_phdr *elf_phdata;
1.1.1.6 root 1349: abi_ulong elf_bss, k, elf_brk;
1.1 root 1350: int retval;
1351: char * elf_interpreter;
1.1.1.6 root 1352: abi_ulong elf_entry, interp_load_addr = 0;
1.1 root 1353: int status;
1.1.1.6 root 1354: abi_ulong start_code, end_code, start_data, end_data;
1355: abi_ulong reloc_func_desc = 0;
1356: abi_ulong elf_stack;
1.1 root 1357: char passed_fileno[6];
1358:
1359: ibcs2_interpreter = 0;
1360: status = 0;
1361: load_addr = 0;
1362: load_bias = 0;
1363: elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
1364: #ifdef BSWAP_NEEDED
1365: bswap_ehdr(&elf_ex);
1366: #endif
1367:
1368: /* First of all, some simple consistency checks */
1369: if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
1370: (! elf_check_arch(elf_ex.e_machine))) {
1371: return -ENOEXEC;
1372: }
1373:
1.1.1.4 root 1374: bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
1375: bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
1376: bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
1377: if (!bprm->p) {
1378: retval = -E2BIG;
1379: }
1380:
1.1 root 1381: /* Now read in all of the header information */
1382: elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
1383: if (elf_phdata == NULL) {
1384: return -ENOMEM;
1385: }
1386:
1387: retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
1388: if(retval > 0) {
1.1.1.6 root 1389: retval = read(bprm->fd, (char *) elf_phdata,
1.1 root 1390: elf_ex.e_phentsize * elf_ex.e_phnum);
1391: }
1392:
1393: if (retval < 0) {
1394: perror("load_elf_binary");
1395: exit(-1);
1396: free (elf_phdata);
1397: return -errno;
1398: }
1399:
1400: #ifdef BSWAP_NEEDED
1401: elf_ppnt = elf_phdata;
1402: for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
1403: bswap_phdr(elf_ppnt);
1404: }
1405: #endif
1406: elf_ppnt = elf_phdata;
1407:
1408: elf_bss = 0;
1409: elf_brk = 0;
1410:
1411:
1.1.1.6 root 1412: elf_stack = ~((abi_ulong)0UL);
1.1 root 1413: elf_interpreter = NULL;
1.1.1.6 root 1414: start_code = ~((abi_ulong)0UL);
1.1 root 1415: end_code = 0;
1.1.1.6 root 1416: start_data = 0;
1.1 root 1417: end_data = 0;
1.1.1.7 root 1418: interp_ex.a_info = 0;
1.1 root 1419:
1420: for(i=0;i < elf_ex.e_phnum; i++) {
1421: if (elf_ppnt->p_type == PT_INTERP) {
1422: if ( elf_interpreter != NULL )
1423: {
1424: free (elf_phdata);
1425: free(elf_interpreter);
1426: close(bprm->fd);
1427: return -EINVAL;
1428: }
1429:
1430: /* This is the program interpreter used for
1431: * shared libraries - for now assume that this
1432: * is an a.out format binary
1433: */
1434:
1435: elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
1436:
1437: if (elf_interpreter == NULL) {
1438: free (elf_phdata);
1439: close(bprm->fd);
1440: return -ENOMEM;
1441: }
1442:
1443: retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
1444: if(retval >= 0) {
1445: retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
1446: }
1447: if(retval < 0) {
1448: perror("load_elf_binary2");
1449: exit(-1);
1.1.1.6 root 1450: }
1.1 root 1451:
1452: /* If the program interpreter is one of these two,
1453: then assume an iBCS2 image. Otherwise assume
1454: a native linux image. */
1455:
1456: /* JRP - Need to add X86 lib dir stuff here... */
1457:
1458: if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
1459: strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
1460: ibcs2_interpreter = 1;
1461: }
1462:
1463: #if 0
1.1.1.10! root 1464: printf("Using ELF interpreter %s\n", path(elf_interpreter));
1.1 root 1465: #endif
1466: if (retval >= 0) {
1467: retval = open(path(elf_interpreter), O_RDONLY);
1468: if(retval >= 0) {
1469: interpreter_fd = retval;
1470: }
1471: else {
1472: perror(elf_interpreter);
1473: exit(-1);
1474: /* retval = -errno; */
1475: }
1476: }
1477:
1478: if (retval >= 0) {
1479: retval = lseek(interpreter_fd, 0, SEEK_SET);
1480: if(retval >= 0) {
1481: retval = read(interpreter_fd,bprm->buf,128);
1482: }
1483: }
1484: if (retval >= 0) {
1485: interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
1.1.1.10! root 1486: interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */
1.1 root 1487: }
1488: if (retval < 0) {
1489: perror("load_elf_binary3");
1490: exit(-1);
1491: free (elf_phdata);
1492: free(elf_interpreter);
1493: close(bprm->fd);
1494: return retval;
1495: }
1496: }
1497: elf_ppnt++;
1498: }
1499:
1500: /* Some simple consistency checks for the interpreter */
1501: if (elf_interpreter){
1502: interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
1503:
1504: /* Now figure out which format our binary is */
1505: if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
1506: (N_MAGIC(interp_ex) != QMAGIC)) {
1507: interpreter_type = INTERPRETER_ELF;
1508: }
1509:
1510: if (interp_elf_ex.e_ident[0] != 0x7f ||
1.1.1.7 root 1511: strncmp((char *)&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
1.1 root 1512: interpreter_type &= ~INTERPRETER_ELF;
1513: }
1514:
1515: if (!interpreter_type) {
1516: free(elf_interpreter);
1517: free(elf_phdata);
1518: close(bprm->fd);
1519: return -ELIBBAD;
1520: }
1521: }
1522:
1523: /* OK, we are done with that, now set up the arg stuff,
1524: and then start this sucker up */
1525:
1.1.1.4 root 1526: {
1.1 root 1527: char * passed_p;
1528:
1529: if (interpreter_type == INTERPRETER_AOUT) {
1530: snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
1531: passed_p = passed_fileno;
1532:
1533: if (elf_interpreter) {
1.1.1.4 root 1534: bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
1.1 root 1535: bprm->argc++;
1536: }
1537: }
1538: if (!bprm->p) {
1539: if (elf_interpreter) {
1540: free(elf_interpreter);
1541: }
1542: free (elf_phdata);
1543: close(bprm->fd);
1544: return -E2BIG;
1545: }
1546: }
1547:
1548: /* OK, This is the point of no return */
1549: info->end_data = 0;
1550: info->end_code = 0;
1.1.1.6 root 1551: info->start_mmap = (abi_ulong)ELF_START_MMAP;
1.1 root 1552: info->mmap = 0;
1.1.1.6 root 1553: elf_entry = (abi_ulong) elf_ex.e_entry;
1.1 root 1554:
1.1.1.10! root 1555: #if defined(CONFIG_USE_GUEST_BASE)
! 1556: /*
! 1557: * In case where user has not explicitly set the guest_base, we
! 1558: * probe here that should we set it automatically.
! 1559: */
! 1560: if (!have_guest_base) {
! 1561: /*
! 1562: * Go through ELF program header table and find out whether
! 1563: * any of the segments drop below our current mmap_min_addr and
! 1564: * in that case set guest_base to corresponding address.
! 1565: */
! 1566: for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum;
! 1567: i++, elf_ppnt++) {
! 1568: if (elf_ppnt->p_type != PT_LOAD)
! 1569: continue;
! 1570: if (HOST_PAGE_ALIGN(elf_ppnt->p_vaddr) < mmap_min_addr) {
! 1571: guest_base = HOST_PAGE_ALIGN(mmap_min_addr);
! 1572: break;
! 1573: }
! 1574: }
! 1575: }
! 1576: #endif /* CONFIG_USE_GUEST_BASE */
! 1577:
1.1 root 1578: /* Do this so that we can load the interpreter, if need be. We will
1579: change some of these later */
1580: info->rss = 0;
1581: bprm->p = setup_arg_pages(bprm->p, bprm, info);
1582: info->start_stack = bprm->p;
1583:
1584: /* Now we do a little grungy work by mmaping the ELF image into
1585: * the correct location in memory. At this point, we assume that
1586: * the image should be loaded at fixed address, not at a variable
1587: * address.
1588: */
1589:
1590: for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
1591: int elf_prot = 0;
1592: int elf_flags = 0;
1.1.1.6 root 1593: abi_ulong error;
1594:
1.1 root 1595: if (elf_ppnt->p_type != PT_LOAD)
1596: continue;
1.1.1.6 root 1597:
1.1 root 1598: if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1599: if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1600: if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1601: elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
1602: if (elf_ex.e_type == ET_EXEC || load_addr_set) {
1603: elf_flags |= MAP_FIXED;
1604: } else if (elf_ex.e_type == ET_DYN) {
1605: /* Try and get dynamic programs out of the way of the default mmap
1606: base, as well as whatever program they might try to exec. This
1607: is because the brk will follow the loader, and is not movable. */
1608: /* NOTE: for qemu, we do a big mmap to get enough space
1.1.1.6 root 1609: without hardcoding any address */
1.1 root 1610: error = target_mmap(0, ET_DYN_MAP_SIZE,
1.1.1.6 root 1611: PROT_NONE, MAP_PRIVATE | MAP_ANON,
1.1 root 1612: -1, 0);
1613: if (error == -1) {
1614: perror("mmap");
1615: exit(-1);
1616: }
1617: load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
1618: }
1.1.1.6 root 1619:
1.1 root 1620: error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1621: (elf_ppnt->p_filesz +
1622: TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1623: elf_prot,
1624: (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1625: bprm->fd,
1.1.1.6 root 1626: (elf_ppnt->p_offset -
1.1 root 1627: TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
1628: if (error == -1) {
1629: perror("mmap");
1630: exit(-1);
1631: }
1632:
1633: #ifdef LOW_ELF_STACK
1634: if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1635: elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
1636: #endif
1.1.1.6 root 1637:
1.1 root 1638: if (!load_addr_set) {
1639: load_addr_set = 1;
1640: load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1641: if (elf_ex.e_type == ET_DYN) {
1642: load_bias += error -
1643: TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
1644: load_addr += load_bias;
1.1.1.6 root 1645: reloc_func_desc = load_bias;
1.1 root 1646: }
1647: }
1648: k = elf_ppnt->p_vaddr;
1.1.1.6 root 1649: if (k < start_code)
1.1 root 1650: start_code = k;
1.1.1.6 root 1651: if (start_data < k)
1652: start_data = k;
1.1 root 1653: k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
1.1.1.6 root 1654: if (k > elf_bss)
1.1 root 1655: elf_bss = k;
1656: if ((elf_ppnt->p_flags & PF_X) && end_code < k)
1657: end_code = k;
1.1.1.6 root 1658: if (end_data < k)
1.1 root 1659: end_data = k;
1660: k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1661: if (k > elf_brk) elf_brk = k;
1662: }
1663:
1664: elf_entry += load_bias;
1665: elf_bss += load_bias;
1666: elf_brk += load_bias;
1667: start_code += load_bias;
1668: end_code += load_bias;
1.1.1.6 root 1669: start_data += load_bias;
1.1 root 1670: end_data += load_bias;
1671:
1672: if (elf_interpreter) {
1673: if (interpreter_type & 1) {
1674: elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1675: }
1676: else if (interpreter_type & 2) {
1677: elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1678: &interp_load_addr);
1679: }
1.1.1.6 root 1680: reloc_func_desc = interp_load_addr;
1.1 root 1681:
1682: close(interpreter_fd);
1683: free(elf_interpreter);
1684:
1.1.1.6 root 1685: if (elf_entry == ~((abi_ulong)0UL)) {
1.1 root 1686: printf("Unable to load interpreter\n");
1687: free(elf_phdata);
1688: exit(-1);
1689: return 0;
1690: }
1691: }
1692:
1693: free(elf_phdata);
1694:
1.1.1.7 root 1695: if (qemu_log_enabled())
1.1 root 1696: load_symbols(&elf_ex, bprm->fd);
1697:
1698: if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
1699: info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
1700:
1701: #ifdef LOW_ELF_STACK
1702: info->start_stack = bprm->p = elf_stack - 4;
1703: #endif
1.1.1.3 root 1704: bprm->p = create_elf_tables(bprm->p,
1.1 root 1705: bprm->argc,
1706: bprm->envc,
1707: &elf_ex,
1708: load_addr, load_bias,
1709: interp_load_addr,
1710: (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1711: info);
1.1.1.6 root 1712: info->load_addr = reloc_func_desc;
1.1 root 1713: info->start_brk = info->brk = elf_brk;
1714: info->end_code = end_code;
1715: info->start_code = start_code;
1.1.1.6 root 1716: info->start_data = start_data;
1.1 root 1717: info->end_data = end_data;
1718: info->start_stack = bprm->p;
1719:
1720: /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1721: sections */
1722: set_brk(elf_bss, elf_brk);
1723:
1.1.1.5 root 1724: padzero(elf_bss, elf_brk);
1.1 root 1725:
1726: #if 0
1727: printf("(start_brk) %x\n" , info->start_brk);
1728: printf("(end_code) %x\n" , info->end_code);
1729: printf("(start_code) %x\n" , info->start_code);
1730: printf("(end_data) %x\n" , info->end_data);
1731: printf("(start_stack) %x\n" , info->start_stack);
1732: printf("(brk) %x\n" , info->brk);
1733: #endif
1734:
1735: if ( info->personality == PER_SVR4 )
1736: {
1737: /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1738: and some applications "depend" upon this behavior.
1739: Since we do not have the power to recompile these, we
1740: emulate the SVr4 behavior. Sigh. */
1741: mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
1742: MAP_FIXED | MAP_PRIVATE, -1, 0);
1743: }
1744:
1745: info->entry = elf_entry;
1746:
1.1.1.8 root 1747: #ifdef USE_ELF_CORE_DUMP
1748: bprm->core_dump = &elf_core_dump;
1749: #endif
1750:
1.1 root 1751: return 0;
1752: }
1753:
1.1.1.8 root 1754: #ifdef USE_ELF_CORE_DUMP
1755:
1756: /*
1757: * Definitions to generate Intel SVR4-like core files.
1.1.1.9 root 1758: * These mostly have the same names as the SVR4 types with "target_elf_"
1.1.1.8 root 1759: * tacked on the front to prevent clashes with linux definitions,
1760: * and the typedef forms have been avoided. This is mostly like
1761: * the SVR4 structure, but more Linuxy, with things that Linux does
1762: * not support and which gdb doesn't really use excluded.
1763: *
1764: * Fields we don't dump (their contents is zero) in linux-user qemu
1765: * are marked with XXX.
1766: *
1767: * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
1768: *
1769: * Porting ELF coredump for target is (quite) simple process. First you
1770: * define ELF_USE_CORE_DUMP in target ELF code (where init_thread() for
1771: * the target resides):
1772: *
1773: * #define USE_ELF_CORE_DUMP
1774: *
1775: * Next you define type of register set used for dumping. ELF specification
1776: * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
1777: *
1.1.1.9 root 1778: * typedef <target_regtype> target_elf_greg_t;
1.1.1.8 root 1779: * #define ELF_NREG <number of registers>
1.1.1.9 root 1780: * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
1.1.1.8 root 1781: *
1782: * Then define following types to match target types. Actual types can
1783: * be found from linux kernel (arch/<ARCH>/include/asm/posix_types.h):
1784: *
1785: * typedef <target_uid_type> target_uid_t;
1786: * typedef <target_gid_type> target_gid_t;
1787: * typedef <target_pid_type> target_pid_t;
1788: *
1789: * Last step is to implement target specific function that copies registers
1790: * from given cpu into just specified register set. Prototype is:
1791: *
1.1.1.9 root 1792: * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
1793: * const CPUState *env);
1.1.1.8 root 1794: *
1795: * Parameters:
1796: * regs - copy register values into here (allocated and zeroed by caller)
1797: * env - copy registers from here
1798: *
1799: * Example for ARM target is provided in this file.
1800: */
1801:
1802: /* An ELF note in memory */
1803: struct memelfnote {
1804: const char *name;
1805: size_t namesz;
1806: size_t namesz_rounded;
1807: int type;
1808: size_t datasz;
1809: void *data;
1810: size_t notesz;
1811: };
1812:
1.1.1.9 root 1813: struct target_elf_siginfo {
1.1.1.8 root 1814: int si_signo; /* signal number */
1815: int si_code; /* extra code */
1816: int si_errno; /* errno */
1817: };
1818:
1.1.1.9 root 1819: struct target_elf_prstatus {
1820: struct target_elf_siginfo pr_info; /* Info associated with signal */
1.1.1.8 root 1821: short pr_cursig; /* Current signal */
1822: target_ulong pr_sigpend; /* XXX */
1823: target_ulong pr_sighold; /* XXX */
1824: target_pid_t pr_pid;
1825: target_pid_t pr_ppid;
1826: target_pid_t pr_pgrp;
1827: target_pid_t pr_sid;
1828: struct target_timeval pr_utime; /* XXX User time */
1829: struct target_timeval pr_stime; /* XXX System time */
1830: struct target_timeval pr_cutime; /* XXX Cumulative user time */
1831: struct target_timeval pr_cstime; /* XXX Cumulative system time */
1.1.1.9 root 1832: target_elf_gregset_t pr_reg; /* GP registers */
1.1.1.8 root 1833: int pr_fpvalid; /* XXX */
1834: };
1835:
1836: #define ELF_PRARGSZ (80) /* Number of chars for args */
1837:
1.1.1.9 root 1838: struct target_elf_prpsinfo {
1.1.1.8 root 1839: char pr_state; /* numeric process state */
1840: char pr_sname; /* char for pr_state */
1841: char pr_zomb; /* zombie */
1842: char pr_nice; /* nice val */
1843: target_ulong pr_flag; /* flags */
1844: target_uid_t pr_uid;
1845: target_gid_t pr_gid;
1846: target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
1847: /* Lots missing */
1848: char pr_fname[16]; /* filename of executable */
1849: char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
1850: };
1851:
1852: /* Here is the structure in which status of each thread is captured. */
1853: struct elf_thread_status {
1.1.1.10! root 1854: QTAILQ_ENTRY(elf_thread_status) ets_link;
1.1.1.9 root 1855: struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
1.1.1.8 root 1856: #if 0
1857: elf_fpregset_t fpu; /* NT_PRFPREG */
1858: struct task_struct *thread;
1859: elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
1860: #endif
1861: struct memelfnote notes[1];
1862: int num_notes;
1863: };
1864:
1865: struct elf_note_info {
1866: struct memelfnote *notes;
1.1.1.9 root 1867: struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
1868: struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
1.1.1.8 root 1869:
1.1.1.10! root 1870: QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
1.1.1.8 root 1871: #if 0
1872: /*
1873: * Current version of ELF coredump doesn't support
1874: * dumping fp regs etc.
1875: */
1876: elf_fpregset_t *fpu;
1877: elf_fpxregset_t *xfpu;
1878: int thread_status_size;
1879: #endif
1880: int notes_size;
1881: int numnote;
1882: };
1883:
1884: struct vm_area_struct {
1885: abi_ulong vma_start; /* start vaddr of memory region */
1886: abi_ulong vma_end; /* end vaddr of memory region */
1887: abi_ulong vma_flags; /* protection etc. flags for the region */
1.1.1.10! root 1888: QTAILQ_ENTRY(vm_area_struct) vma_link;
1.1.1.8 root 1889: };
1890:
1891: struct mm_struct {
1.1.1.10! root 1892: QTAILQ_HEAD(, vm_area_struct) mm_mmap;
1.1.1.8 root 1893: int mm_count; /* number of mappings */
1894: };
1895:
1896: static struct mm_struct *vma_init(void);
1897: static void vma_delete(struct mm_struct *);
1898: static int vma_add_mapping(struct mm_struct *, abi_ulong,
1899: abi_ulong, abi_ulong);
1900: static int vma_get_mapping_count(const struct mm_struct *);
1901: static struct vm_area_struct *vma_first(const struct mm_struct *);
1902: static struct vm_area_struct *vma_next(struct vm_area_struct *);
1903: static abi_ulong vma_dump_size(const struct vm_area_struct *);
1904: static int vma_walker(void *priv, unsigned long start, unsigned long end,
1905: unsigned long flags);
1906:
1907: static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
1908: static void fill_note(struct memelfnote *, const char *, int,
1909: unsigned int, void *);
1.1.1.9 root 1910: static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
1911: static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
1.1.1.8 root 1912: static void fill_auxv_note(struct memelfnote *, const TaskState *);
1913: static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
1914: static size_t note_size(const struct memelfnote *);
1915: static void free_note_info(struct elf_note_info *);
1916: static int fill_note_info(struct elf_note_info *, long, const CPUState *);
1917: static void fill_thread_info(struct elf_note_info *, const CPUState *);
1918: static int core_dump_filename(const TaskState *, char *, size_t);
1919:
1920: static int dump_write(int, const void *, size_t);
1921: static int write_note(struct memelfnote *, int);
1922: static int write_note_info(struct elf_note_info *, int);
1923:
1924: #ifdef BSWAP_NEEDED
1.1.1.9 root 1925: static void bswap_prstatus(struct target_elf_prstatus *);
1926: static void bswap_psinfo(struct target_elf_prpsinfo *);
1.1.1.8 root 1927:
1.1.1.9 root 1928: static void bswap_prstatus(struct target_elf_prstatus *prstatus)
1.1.1.8 root 1929: {
1930: prstatus->pr_info.si_signo = tswapl(prstatus->pr_info.si_signo);
1931: prstatus->pr_info.si_code = tswapl(prstatus->pr_info.si_code);
1932: prstatus->pr_info.si_errno = tswapl(prstatus->pr_info.si_errno);
1933: prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
1934: prstatus->pr_sigpend = tswapl(prstatus->pr_sigpend);
1935: prstatus->pr_sighold = tswapl(prstatus->pr_sighold);
1936: prstatus->pr_pid = tswap32(prstatus->pr_pid);
1937: prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
1938: prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
1939: prstatus->pr_sid = tswap32(prstatus->pr_sid);
1940: /* cpu times are not filled, so we skip them */
1941: /* regs should be in correct format already */
1942: prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
1943: }
1944:
1.1.1.9 root 1945: static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
1.1.1.8 root 1946: {
1947: psinfo->pr_flag = tswapl(psinfo->pr_flag);
1948: psinfo->pr_uid = tswap16(psinfo->pr_uid);
1949: psinfo->pr_gid = tswap16(psinfo->pr_gid);
1950: psinfo->pr_pid = tswap32(psinfo->pr_pid);
1951: psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
1952: psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
1953: psinfo->pr_sid = tswap32(psinfo->pr_sid);
1954: }
1955: #endif /* BSWAP_NEEDED */
1956:
1957: /*
1958: * Minimal support for linux memory regions. These are needed
1959: * when we are finding out what memory exactly belongs to
1960: * emulated process. No locks needed here, as long as
1961: * thread that received the signal is stopped.
1962: */
1963:
1964: static struct mm_struct *vma_init(void)
1965: {
1966: struct mm_struct *mm;
1967:
1968: if ((mm = qemu_malloc(sizeof (*mm))) == NULL)
1969: return (NULL);
1970:
1971: mm->mm_count = 0;
1.1.1.10! root 1972: QTAILQ_INIT(&mm->mm_mmap);
1.1.1.8 root 1973:
1974: return (mm);
1975: }
1976:
1977: static void vma_delete(struct mm_struct *mm)
1978: {
1979: struct vm_area_struct *vma;
1980:
1981: while ((vma = vma_first(mm)) != NULL) {
1.1.1.10! root 1982: QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
1.1.1.8 root 1983: qemu_free(vma);
1984: }
1985: qemu_free(mm);
1986: }
1987:
1988: static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
1989: abi_ulong end, abi_ulong flags)
1990: {
1991: struct vm_area_struct *vma;
1992:
1993: if ((vma = qemu_mallocz(sizeof (*vma))) == NULL)
1994: return (-1);
1995:
1996: vma->vma_start = start;
1997: vma->vma_end = end;
1998: vma->vma_flags = flags;
1999:
1.1.1.10! root 2000: QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
1.1.1.8 root 2001: mm->mm_count++;
2002:
2003: return (0);
2004: }
2005:
2006: static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2007: {
1.1.1.10! root 2008: return (QTAILQ_FIRST(&mm->mm_mmap));
1.1.1.8 root 2009: }
2010:
2011: static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2012: {
1.1.1.10! root 2013: return (QTAILQ_NEXT(vma, vma_link));
1.1.1.8 root 2014: }
2015:
2016: static int vma_get_mapping_count(const struct mm_struct *mm)
2017: {
2018: return (mm->mm_count);
2019: }
2020:
2021: /*
2022: * Calculate file (dump) size of given memory region.
2023: */
2024: static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2025: {
2026: /* if we cannot even read the first page, skip it */
2027: if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2028: return (0);
2029:
2030: /*
2031: * Usually we don't dump executable pages as they contain
2032: * non-writable code that debugger can read directly from
2033: * target library etc. However, thread stacks are marked
2034: * also executable so we read in first page of given region
2035: * and check whether it contains elf header. If there is
2036: * no elf header, we dump it.
2037: */
2038: if (vma->vma_flags & PROT_EXEC) {
2039: char page[TARGET_PAGE_SIZE];
2040:
2041: copy_from_user(page, vma->vma_start, sizeof (page));
2042: if ((page[EI_MAG0] == ELFMAG0) &&
2043: (page[EI_MAG1] == ELFMAG1) &&
2044: (page[EI_MAG2] == ELFMAG2) &&
2045: (page[EI_MAG3] == ELFMAG3)) {
2046: /*
2047: * Mappings are possibly from ELF binary. Don't dump
2048: * them.
2049: */
2050: return (0);
2051: }
2052: }
2053:
2054: return (vma->vma_end - vma->vma_start);
2055: }
2056:
2057: static int vma_walker(void *priv, unsigned long start, unsigned long end,
2058: unsigned long flags)
2059: {
2060: struct mm_struct *mm = (struct mm_struct *)priv;
2061:
2062: /*
2063: * Don't dump anything that qemu has reserved for internal use.
2064: */
2065: if (flags & PAGE_RESERVED)
2066: return (0);
2067:
2068: vma_add_mapping(mm, start, end, flags);
2069: return (0);
2070: }
2071:
2072: static void fill_note(struct memelfnote *note, const char *name, int type,
2073: unsigned int sz, void *data)
2074: {
2075: unsigned int namesz;
2076:
2077: namesz = strlen(name) + 1;
2078: note->name = name;
2079: note->namesz = namesz;
2080: note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2081: note->type = type;
2082: note->datasz = roundup(sz, sizeof (int32_t));;
2083: note->data = data;
2084:
2085: /*
2086: * We calculate rounded up note size here as specified by
2087: * ELF document.
2088: */
2089: note->notesz = sizeof (struct elf_note) +
2090: note->namesz_rounded + note->datasz;
2091: }
2092:
2093: static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2094: uint32_t flags)
2095: {
2096: (void) memset(elf, 0, sizeof(*elf));
2097:
2098: (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2099: elf->e_ident[EI_CLASS] = ELF_CLASS;
2100: elf->e_ident[EI_DATA] = ELF_DATA;
2101: elf->e_ident[EI_VERSION] = EV_CURRENT;
2102: elf->e_ident[EI_OSABI] = ELF_OSABI;
2103:
2104: elf->e_type = ET_CORE;
2105: elf->e_machine = machine;
2106: elf->e_version = EV_CURRENT;
2107: elf->e_phoff = sizeof(struct elfhdr);
2108: elf->e_flags = flags;
2109: elf->e_ehsize = sizeof(struct elfhdr);
2110: elf->e_phentsize = sizeof(struct elf_phdr);
2111: elf->e_phnum = segs;
2112:
2113: #ifdef BSWAP_NEEDED
2114: bswap_ehdr(elf);
2115: #endif
2116: }
2117:
2118: static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2119: {
2120: phdr->p_type = PT_NOTE;
2121: phdr->p_offset = offset;
2122: phdr->p_vaddr = 0;
2123: phdr->p_paddr = 0;
2124: phdr->p_filesz = sz;
2125: phdr->p_memsz = 0;
2126: phdr->p_flags = 0;
2127: phdr->p_align = 0;
2128:
2129: #ifdef BSWAP_NEEDED
2130: bswap_phdr(phdr);
2131: #endif
2132: }
2133:
2134: static size_t note_size(const struct memelfnote *note)
2135: {
2136: return (note->notesz);
2137: }
2138:
1.1.1.9 root 2139: static void fill_prstatus(struct target_elf_prstatus *prstatus,
1.1.1.8 root 2140: const TaskState *ts, int signr)
2141: {
2142: (void) memset(prstatus, 0, sizeof (*prstatus));
2143: prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2144: prstatus->pr_pid = ts->ts_tid;
2145: prstatus->pr_ppid = getppid();
2146: prstatus->pr_pgrp = getpgrp();
2147: prstatus->pr_sid = getsid(0);
2148:
2149: #ifdef BSWAP_NEEDED
2150: bswap_prstatus(prstatus);
2151: #endif
2152: }
2153:
1.1.1.9 root 2154: static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
1.1.1.8 root 2155: {
2156: char *filename, *base_filename;
2157: unsigned int i, len;
2158:
2159: (void) memset(psinfo, 0, sizeof (*psinfo));
2160:
2161: len = ts->info->arg_end - ts->info->arg_start;
2162: if (len >= ELF_PRARGSZ)
2163: len = ELF_PRARGSZ - 1;
2164: if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2165: return -EFAULT;
2166: for (i = 0; i < len; i++)
2167: if (psinfo->pr_psargs[i] == 0)
2168: psinfo->pr_psargs[i] = ' ';
2169: psinfo->pr_psargs[len] = 0;
2170:
2171: psinfo->pr_pid = getpid();
2172: psinfo->pr_ppid = getppid();
2173: psinfo->pr_pgrp = getpgrp();
2174: psinfo->pr_sid = getsid(0);
2175: psinfo->pr_uid = getuid();
2176: psinfo->pr_gid = getgid();
2177:
2178: filename = strdup(ts->bprm->filename);
2179: base_filename = strdup(basename(filename));
2180: (void) strncpy(psinfo->pr_fname, base_filename,
2181: sizeof(psinfo->pr_fname));
2182: free(base_filename);
2183: free(filename);
2184:
2185: #ifdef BSWAP_NEEDED
2186: bswap_psinfo(psinfo);
2187: #endif
2188: return (0);
2189: }
2190:
2191: static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2192: {
2193: elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2194: elf_addr_t orig_auxv = auxv;
2195: abi_ulong val;
2196: void *ptr;
2197: int i, len;
2198:
2199: /*
2200: * Auxiliary vector is stored in target process stack. It contains
2201: * {type, value} pairs that we need to dump into note. This is not
2202: * strictly necessary but we do it here for sake of completeness.
2203: */
2204:
2205: /* find out lenght of the vector, AT_NULL is terminator */
2206: i = len = 0;
2207: do {
2208: get_user_ual(val, auxv);
2209: i += 2;
2210: auxv += 2 * sizeof (elf_addr_t);
2211: } while (val != AT_NULL);
2212: len = i * sizeof (elf_addr_t);
2213:
2214: /* read in whole auxv vector and copy it to memelfnote */
2215: ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2216: if (ptr != NULL) {
2217: fill_note(note, "CORE", NT_AUXV, len, ptr);
2218: unlock_user(ptr, auxv, len);
2219: }
2220: }
2221:
2222: /*
2223: * Constructs name of coredump file. We have following convention
2224: * for the name:
2225: * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2226: *
2227: * Returns 0 in case of success, -1 otherwise (errno is set).
2228: */
2229: static int core_dump_filename(const TaskState *ts, char *buf,
2230: size_t bufsize)
2231: {
2232: char timestamp[64];
2233: char *filename = NULL;
2234: char *base_filename = NULL;
2235: struct timeval tv;
2236: struct tm tm;
2237:
2238: assert(bufsize >= PATH_MAX);
2239:
2240: if (gettimeofday(&tv, NULL) < 0) {
2241: (void) fprintf(stderr, "unable to get current timestamp: %s",
2242: strerror(errno));
2243: return (-1);
2244: }
2245:
2246: filename = strdup(ts->bprm->filename);
2247: base_filename = strdup(basename(filename));
2248: (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2249: localtime_r(&tv.tv_sec, &tm));
2250: (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2251: base_filename, timestamp, (int)getpid());
2252: free(base_filename);
2253: free(filename);
2254:
2255: return (0);
2256: }
2257:
2258: static int dump_write(int fd, const void *ptr, size_t size)
2259: {
2260: const char *bufp = (const char *)ptr;
2261: ssize_t bytes_written, bytes_left;
2262: struct rlimit dumpsize;
2263: off_t pos;
2264:
2265: bytes_written = 0;
2266: getrlimit(RLIMIT_CORE, &dumpsize);
2267: if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2268: if (errno == ESPIPE) { /* not a seekable stream */
2269: bytes_left = size;
2270: } else {
2271: return pos;
2272: }
2273: } else {
2274: if (dumpsize.rlim_cur <= pos) {
2275: return -1;
2276: } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2277: bytes_left = size;
2278: } else {
2279: size_t limit_left=dumpsize.rlim_cur - pos;
2280: bytes_left = limit_left >= size ? size : limit_left ;
2281: }
2282: }
2283:
2284: /*
2285: * In normal conditions, single write(2) should do but
2286: * in case of socket etc. this mechanism is more portable.
2287: */
2288: do {
2289: bytes_written = write(fd, bufp, bytes_left);
2290: if (bytes_written < 0) {
2291: if (errno == EINTR)
2292: continue;
2293: return (-1);
2294: } else if (bytes_written == 0) { /* eof */
2295: return (-1);
2296: }
2297: bufp += bytes_written;
2298: bytes_left -= bytes_written;
2299: } while (bytes_left > 0);
2300:
2301: return (0);
2302: }
2303:
2304: static int write_note(struct memelfnote *men, int fd)
2305: {
2306: struct elf_note en;
2307:
2308: en.n_namesz = men->namesz;
2309: en.n_type = men->type;
2310: en.n_descsz = men->datasz;
2311:
2312: #ifdef BSWAP_NEEDED
2313: bswap_note(&en);
2314: #endif
2315:
2316: if (dump_write(fd, &en, sizeof(en)) != 0)
2317: return (-1);
2318: if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2319: return (-1);
2320: if (dump_write(fd, men->data, men->datasz) != 0)
2321: return (-1);
2322:
2323: return (0);
2324: }
2325:
2326: static void fill_thread_info(struct elf_note_info *info, const CPUState *env)
2327: {
2328: TaskState *ts = (TaskState *)env->opaque;
2329: struct elf_thread_status *ets;
2330:
2331: ets = qemu_mallocz(sizeof (*ets));
2332: ets->num_notes = 1; /* only prstatus is dumped */
2333: fill_prstatus(&ets->prstatus, ts, 0);
2334: elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2335: fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2336: &ets->prstatus);
2337:
1.1.1.10! root 2338: QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
1.1.1.8 root 2339:
2340: info->notes_size += note_size(&ets->notes[0]);
2341: }
2342:
2343: static int fill_note_info(struct elf_note_info *info,
2344: long signr, const CPUState *env)
2345: {
2346: #define NUMNOTES 3
2347: CPUState *cpu = NULL;
2348: TaskState *ts = (TaskState *)env->opaque;
2349: int i;
2350:
2351: (void) memset(info, 0, sizeof (*info));
2352:
1.1.1.10! root 2353: QTAILQ_INIT(&info->thread_list);
1.1.1.8 root 2354:
2355: info->notes = qemu_mallocz(NUMNOTES * sizeof (struct memelfnote));
2356: if (info->notes == NULL)
2357: return (-ENOMEM);
2358: info->prstatus = qemu_mallocz(sizeof (*info->prstatus));
2359: if (info->prstatus == NULL)
2360: return (-ENOMEM);
2361: info->psinfo = qemu_mallocz(sizeof (*info->psinfo));
2362: if (info->prstatus == NULL)
2363: return (-ENOMEM);
2364:
2365: /*
2366: * First fill in status (and registers) of current thread
2367: * including process info & aux vector.
2368: */
2369: fill_prstatus(info->prstatus, ts, signr);
2370: elf_core_copy_regs(&info->prstatus->pr_reg, env);
2371: fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2372: sizeof (*info->prstatus), info->prstatus);
2373: fill_psinfo(info->psinfo, ts);
2374: fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2375: sizeof (*info->psinfo), info->psinfo);
2376: fill_auxv_note(&info->notes[2], ts);
2377: info->numnote = 3;
2378:
2379: info->notes_size = 0;
2380: for (i = 0; i < info->numnote; i++)
2381: info->notes_size += note_size(&info->notes[i]);
2382:
2383: /* read and fill status of all threads */
2384: cpu_list_lock();
2385: for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2386: if (cpu == thread_env)
2387: continue;
2388: fill_thread_info(info, cpu);
2389: }
2390: cpu_list_unlock();
2391:
2392: return (0);
2393: }
2394:
2395: static void free_note_info(struct elf_note_info *info)
2396: {
2397: struct elf_thread_status *ets;
2398:
1.1.1.10! root 2399: while (!QTAILQ_EMPTY(&info->thread_list)) {
! 2400: ets = QTAILQ_FIRST(&info->thread_list);
! 2401: QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
1.1.1.8 root 2402: qemu_free(ets);
2403: }
2404:
2405: qemu_free(info->prstatus);
2406: qemu_free(info->psinfo);
2407: qemu_free(info->notes);
2408: }
2409:
2410: static int write_note_info(struct elf_note_info *info, int fd)
2411: {
2412: struct elf_thread_status *ets;
2413: int i, error = 0;
2414:
2415: /* write prstatus, psinfo and auxv for current thread */
2416: for (i = 0; i < info->numnote; i++)
2417: if ((error = write_note(&info->notes[i], fd)) != 0)
2418: return (error);
2419:
2420: /* write prstatus for each thread */
2421: for (ets = info->thread_list.tqh_first; ets != NULL;
2422: ets = ets->ets_link.tqe_next) {
2423: if ((error = write_note(&ets->notes[0], fd)) != 0)
2424: return (error);
2425: }
2426:
2427: return (0);
2428: }
2429:
2430: /*
2431: * Write out ELF coredump.
2432: *
2433: * See documentation of ELF object file format in:
2434: * http://www.caldera.com/developers/devspecs/gabi41.pdf
2435: *
2436: * Coredump format in linux is following:
2437: *
2438: * 0 +----------------------+ \
2439: * | ELF header | ET_CORE |
2440: * +----------------------+ |
2441: * | ELF program headers | |--- headers
2442: * | - NOTE section | |
2443: * | - PT_LOAD sections | |
2444: * +----------------------+ /
2445: * | NOTEs: |
2446: * | - NT_PRSTATUS |
2447: * | - NT_PRSINFO |
2448: * | - NT_AUXV |
2449: * +----------------------+ <-- aligned to target page
2450: * | Process memory dump |
2451: * : :
2452: * . .
2453: * : :
2454: * | |
2455: * +----------------------+
2456: *
2457: * NT_PRSTATUS -> struct elf_prstatus (per thread)
2458: * NT_PRSINFO -> struct elf_prpsinfo
2459: * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2460: *
2461: * Format follows System V format as close as possible. Current
2462: * version limitations are as follows:
2463: * - no floating point registers are dumped
2464: *
2465: * Function returns 0 in case of success, negative errno otherwise.
2466: *
2467: * TODO: make this work also during runtime: it should be
2468: * possible to force coredump from running process and then
2469: * continue processing. For example qemu could set up SIGUSR2
2470: * handler (provided that target process haven't registered
2471: * handler for that) that does the dump when signal is received.
2472: */
2473: static int elf_core_dump(int signr, const CPUState *env)
2474: {
2475: const TaskState *ts = (const TaskState *)env->opaque;
2476: struct vm_area_struct *vma = NULL;
2477: char corefile[PATH_MAX];
2478: struct elf_note_info info;
2479: struct elfhdr elf;
2480: struct elf_phdr phdr;
2481: struct rlimit dumpsize;
2482: struct mm_struct *mm = NULL;
2483: off_t offset = 0, data_offset = 0;
2484: int segs = 0;
2485: int fd = -1;
2486:
2487: errno = 0;
2488: getrlimit(RLIMIT_CORE, &dumpsize);
2489: if (dumpsize.rlim_cur == 0)
2490: return 0;
2491:
2492: if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2493: return (-errno);
2494:
2495: if ((fd = open(corefile, O_WRONLY | O_CREAT,
2496: S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2497: return (-errno);
2498:
2499: /*
2500: * Walk through target process memory mappings and
2501: * set up structure containing this information. After
2502: * this point vma_xxx functions can be used.
2503: */
2504: if ((mm = vma_init()) == NULL)
2505: goto out;
2506:
2507: walk_memory_regions(mm, vma_walker);
2508: segs = vma_get_mapping_count(mm);
2509:
2510: /*
2511: * Construct valid coredump ELF header. We also
2512: * add one more segment for notes.
2513: */
2514: fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
2515: if (dump_write(fd, &elf, sizeof (elf)) != 0)
2516: goto out;
2517:
2518: /* fill in in-memory version of notes */
2519: if (fill_note_info(&info, signr, env) < 0)
2520: goto out;
2521:
2522: offset += sizeof (elf); /* elf header */
2523: offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
2524:
2525: /* write out notes program header */
2526: fill_elf_note_phdr(&phdr, info.notes_size, offset);
2527:
2528: offset += info.notes_size;
2529: if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
2530: goto out;
2531:
2532: /*
2533: * ELF specification wants data to start at page boundary so
2534: * we align it here.
2535: */
2536: offset = roundup(offset, ELF_EXEC_PAGESIZE);
2537:
2538: /*
2539: * Write program headers for memory regions mapped in
2540: * the target process.
2541: */
2542: for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2543: (void) memset(&phdr, 0, sizeof (phdr));
2544:
2545: phdr.p_type = PT_LOAD;
2546: phdr.p_offset = offset;
2547: phdr.p_vaddr = vma->vma_start;
2548: phdr.p_paddr = 0;
2549: phdr.p_filesz = vma_dump_size(vma);
2550: offset += phdr.p_filesz;
2551: phdr.p_memsz = vma->vma_end - vma->vma_start;
2552: phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
2553: if (vma->vma_flags & PROT_WRITE)
2554: phdr.p_flags |= PF_W;
2555: if (vma->vma_flags & PROT_EXEC)
2556: phdr.p_flags |= PF_X;
2557: phdr.p_align = ELF_EXEC_PAGESIZE;
2558:
2559: dump_write(fd, &phdr, sizeof (phdr));
2560: }
2561:
2562: /*
2563: * Next we write notes just after program headers. No
2564: * alignment needed here.
2565: */
2566: if (write_note_info(&info, fd) < 0)
2567: goto out;
2568:
2569: /* align data to page boundary */
2570: data_offset = lseek(fd, 0, SEEK_CUR);
2571: data_offset = TARGET_PAGE_ALIGN(data_offset);
2572: if (lseek(fd, data_offset, SEEK_SET) != data_offset)
2573: goto out;
2574:
2575: /*
2576: * Finally we can dump process memory into corefile as well.
2577: */
2578: for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2579: abi_ulong addr;
2580: abi_ulong end;
2581:
2582: end = vma->vma_start + vma_dump_size(vma);
2583:
2584: for (addr = vma->vma_start; addr < end;
2585: addr += TARGET_PAGE_SIZE) {
2586: char page[TARGET_PAGE_SIZE];
2587: int error;
2588:
2589: /*
2590: * Read in page from target process memory and
2591: * write it to coredump file.
2592: */
2593: error = copy_from_user(page, addr, sizeof (page));
2594: if (error != 0) {
2595: (void) fprintf(stderr, "unable to dump " TARGET_FMT_lx "\n",
2596: addr);
2597: errno = -error;
2598: goto out;
2599: }
2600: if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
2601: goto out;
2602: }
2603: }
2604:
2605: out:
2606: free_note_info(&info);
2607: if (mm != NULL)
2608: vma_delete(mm);
2609: (void) close(fd);
2610:
2611: if (errno != 0)
2612: return (-errno);
2613: return (0);
2614: }
2615:
2616: #endif /* USE_ELF_CORE_DUMP */
2617:
1.1 root 2618: static int load_aout_interp(void * exptr, int interp_fd)
2619: {
2620: printf("a.out interpreter not yet supported\n");
2621: return(0);
2622: }
2623:
1.1.1.4 root 2624: void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
2625: {
2626: init_thread(regs, infop);
2627: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.