qemu-img bench: Fix uninitialised writethrough mode
[qemu.git] / linux-user / elfload.c
blobbb2558f2841a4ef1fa092fea0c01229b989d4d43
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
3 #include <sys/param.h>
5 #include <sys/mman.h>
6 #include <sys/resource.h>
8 #include "qemu.h"
9 #include "disas/disas.h"
10 #include "qemu/path.h"
12 #ifdef _ARCH_PPC64
13 #undef ARCH_DLINFO
14 #undef ELF_PLATFORM
15 #undef ELF_HWCAP
16 #undef ELF_HWCAP2
17 #undef ELF_CLASS
18 #undef ELF_DATA
19 #undef ELF_ARCH
20 #endif
22 #define ELF_OSABI ELFOSABI_SYSV
24 /* from personality.h */
27 * Flags for bug emulation.
29 * These occupy the top three bytes.
31 enum {
32 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
33 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
34 descriptors (signal handling) */
35 MMAP_PAGE_ZERO = 0x0100000,
36 ADDR_COMPAT_LAYOUT = 0x0200000,
37 READ_IMPLIES_EXEC = 0x0400000,
38 ADDR_LIMIT_32BIT = 0x0800000,
39 SHORT_INODE = 0x1000000,
40 WHOLE_SECONDS = 0x2000000,
41 STICKY_TIMEOUTS = 0x4000000,
42 ADDR_LIMIT_3GB = 0x8000000,
46 * Personality types.
48 * These go in the low byte. Avoid using the top bit, it will
49 * conflict with error returns.
51 enum {
52 PER_LINUX = 0x0000,
53 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
54 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
55 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
56 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
57 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
58 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
59 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
60 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
61 PER_BSD = 0x0006,
62 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
63 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
64 PER_LINUX32 = 0x0008,
65 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
66 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
67 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
68 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
69 PER_RISCOS = 0x000c,
70 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
71 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
72 PER_OSF4 = 0x000f, /* OSF/1 v4 */
73 PER_HPUX = 0x0010,
74 PER_MASK = 0x00ff,
78 * Return the base personality without flags.
80 #define personality(pers) (pers & PER_MASK)
82 /* this flag is uneffective under linux too, should be deleted */
83 #ifndef MAP_DENYWRITE
84 #define MAP_DENYWRITE 0
85 #endif
87 /* should probably go in elf.h */
88 #ifndef ELIBBAD
89 #define ELIBBAD 80
90 #endif
92 #ifdef TARGET_WORDS_BIGENDIAN
93 #define ELF_DATA ELFDATA2MSB
94 #else
95 #define ELF_DATA ELFDATA2LSB
96 #endif
98 #ifdef TARGET_ABI_MIPSN32
99 typedef abi_ullong target_elf_greg_t;
100 #define tswapreg(ptr) tswap64(ptr)
101 #else
102 typedef abi_ulong target_elf_greg_t;
103 #define tswapreg(ptr) tswapal(ptr)
104 #endif
106 #ifdef USE_UID16
107 typedef abi_ushort target_uid_t;
108 typedef abi_ushort target_gid_t;
109 #else
110 typedef abi_uint target_uid_t;
111 typedef abi_uint target_gid_t;
112 #endif
113 typedef abi_int target_pid_t;
115 #ifdef TARGET_I386
117 #define ELF_PLATFORM get_elf_platform()
119 static const char *get_elf_platform(void)
121 static char elf_platform[] = "i386";
122 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
123 if (family > 6)
124 family = 6;
125 if (family >= 3)
126 elf_platform[1] = '0' + family;
127 return elf_platform;
130 #define ELF_HWCAP get_elf_hwcap()
132 static uint32_t get_elf_hwcap(void)
134 X86CPU *cpu = X86_CPU(thread_cpu);
136 return cpu->env.features[FEAT_1_EDX];
139 #ifdef TARGET_X86_64
140 #define ELF_START_MMAP 0x2aaaaab000ULL
142 #define ELF_CLASS ELFCLASS64
143 #define ELF_ARCH EM_X86_64
145 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
147 regs->rax = 0;
148 regs->rsp = infop->start_stack;
149 regs->rip = infop->entry;
152 #define ELF_NREG 27
153 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
156 * Note that ELF_NREG should be 29 as there should be place for
157 * TRAPNO and ERR "registers" as well but linux doesn't dump
158 * those.
160 * See linux kernel: arch/x86/include/asm/elf.h
162 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
164 (*regs)[0] = env->regs[15];
165 (*regs)[1] = env->regs[14];
166 (*regs)[2] = env->regs[13];
167 (*regs)[3] = env->regs[12];
168 (*regs)[4] = env->regs[R_EBP];
169 (*regs)[5] = env->regs[R_EBX];
170 (*regs)[6] = env->regs[11];
171 (*regs)[7] = env->regs[10];
172 (*regs)[8] = env->regs[9];
173 (*regs)[9] = env->regs[8];
174 (*regs)[10] = env->regs[R_EAX];
175 (*regs)[11] = env->regs[R_ECX];
176 (*regs)[12] = env->regs[R_EDX];
177 (*regs)[13] = env->regs[R_ESI];
178 (*regs)[14] = env->regs[R_EDI];
179 (*regs)[15] = env->regs[R_EAX]; /* XXX */
180 (*regs)[16] = env->eip;
181 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
182 (*regs)[18] = env->eflags;
183 (*regs)[19] = env->regs[R_ESP];
184 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
185 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
186 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
187 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
188 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
189 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
190 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
193 #else
195 #define ELF_START_MMAP 0x80000000
198 * This is used to ensure we don't load something for the wrong architecture.
200 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
203 * These are used to set parameters in the core dumps.
205 #define ELF_CLASS ELFCLASS32
206 #define ELF_ARCH EM_386
208 static inline void init_thread(struct target_pt_regs *regs,
209 struct image_info *infop)
211 regs->esp = infop->start_stack;
212 regs->eip = infop->entry;
214 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
215 starts %edx contains a pointer to a function which might be
216 registered using `atexit'. This provides a mean for the
217 dynamic linker to call DT_FINI functions for shared libraries
218 that have been loaded before the code runs.
220 A value of 0 tells we have no such handler. */
221 regs->edx = 0;
224 #define ELF_NREG 17
225 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
228 * Note that ELF_NREG should be 19 as there should be place for
229 * TRAPNO and ERR "registers" as well but linux doesn't dump
230 * those.
232 * See linux kernel: arch/x86/include/asm/elf.h
234 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
236 (*regs)[0] = env->regs[R_EBX];
237 (*regs)[1] = env->regs[R_ECX];
238 (*regs)[2] = env->regs[R_EDX];
239 (*regs)[3] = env->regs[R_ESI];
240 (*regs)[4] = env->regs[R_EDI];
241 (*regs)[5] = env->regs[R_EBP];
242 (*regs)[6] = env->regs[R_EAX];
243 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
244 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
245 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
246 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
247 (*regs)[11] = env->regs[R_EAX]; /* XXX */
248 (*regs)[12] = env->eip;
249 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
250 (*regs)[14] = env->eflags;
251 (*regs)[15] = env->regs[R_ESP];
252 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
254 #endif
256 #define USE_ELF_CORE_DUMP
257 #define ELF_EXEC_PAGESIZE 4096
259 #endif
261 #ifdef TARGET_ARM
263 #ifndef TARGET_AARCH64
264 /* 32 bit ARM definitions */
266 #define ELF_START_MMAP 0x80000000
268 #define ELF_ARCH EM_ARM
269 #define ELF_CLASS ELFCLASS32
271 static inline void init_thread(struct target_pt_regs *regs,
272 struct image_info *infop)
274 abi_long stack = infop->start_stack;
275 memset(regs, 0, sizeof(*regs));
277 regs->uregs[16] = ARM_CPU_MODE_USR;
278 if (infop->entry & 1) {
279 regs->uregs[16] |= CPSR_T;
281 regs->uregs[15] = infop->entry & 0xfffffffe;
282 regs->uregs[13] = infop->start_stack;
283 /* FIXME - what to for failure of get_user()? */
284 get_user_ual(regs->uregs[2], stack + 8); /* envp */
285 get_user_ual(regs->uregs[1], stack + 4); /* envp */
286 /* XXX: it seems that r0 is zeroed after ! */
287 regs->uregs[0] = 0;
288 /* For uClinux PIC binaries. */
289 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
290 regs->uregs[10] = infop->start_data;
293 #define ELF_NREG 18
294 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
296 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
298 (*regs)[0] = tswapreg(env->regs[0]);
299 (*regs)[1] = tswapreg(env->regs[1]);
300 (*regs)[2] = tswapreg(env->regs[2]);
301 (*regs)[3] = tswapreg(env->regs[3]);
302 (*regs)[4] = tswapreg(env->regs[4]);
303 (*regs)[5] = tswapreg(env->regs[5]);
304 (*regs)[6] = tswapreg(env->regs[6]);
305 (*regs)[7] = tswapreg(env->regs[7]);
306 (*regs)[8] = tswapreg(env->regs[8]);
307 (*regs)[9] = tswapreg(env->regs[9]);
308 (*regs)[10] = tswapreg(env->regs[10]);
309 (*regs)[11] = tswapreg(env->regs[11]);
310 (*regs)[12] = tswapreg(env->regs[12]);
311 (*regs)[13] = tswapreg(env->regs[13]);
312 (*regs)[14] = tswapreg(env->regs[14]);
313 (*regs)[15] = tswapreg(env->regs[15]);
315 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
316 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
319 #define USE_ELF_CORE_DUMP
320 #define ELF_EXEC_PAGESIZE 4096
322 enum
324 ARM_HWCAP_ARM_SWP = 1 << 0,
325 ARM_HWCAP_ARM_HALF = 1 << 1,
326 ARM_HWCAP_ARM_THUMB = 1 << 2,
327 ARM_HWCAP_ARM_26BIT = 1 << 3,
328 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
329 ARM_HWCAP_ARM_FPA = 1 << 5,
330 ARM_HWCAP_ARM_VFP = 1 << 6,
331 ARM_HWCAP_ARM_EDSP = 1 << 7,
332 ARM_HWCAP_ARM_JAVA = 1 << 8,
333 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
334 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
335 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
336 ARM_HWCAP_ARM_NEON = 1 << 12,
337 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
338 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
339 ARM_HWCAP_ARM_TLS = 1 << 15,
340 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
341 ARM_HWCAP_ARM_IDIVA = 1 << 17,
342 ARM_HWCAP_ARM_IDIVT = 1 << 18,
343 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
344 ARM_HWCAP_ARM_LPAE = 1 << 20,
345 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
348 enum {
349 ARM_HWCAP2_ARM_AES = 1 << 0,
350 ARM_HWCAP2_ARM_PMULL = 1 << 1,
351 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
352 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
353 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
356 /* The commpage only exists for 32 bit kernels */
358 #define TARGET_HAS_VALIDATE_GUEST_SPACE
359 /* Return 1 if the proposed guest space is suitable for the guest.
360 * Return 0 if the proposed guest space isn't suitable, but another
361 * address space should be tried.
362 * Return -1 if there is no way the proposed guest space can be
363 * valid regardless of the base.
364 * The guest code may leave a page mapped and populate it if the
365 * address is suitable.
367 static int validate_guest_space(unsigned long guest_base,
368 unsigned long guest_size)
370 unsigned long real_start, test_page_addr;
372 /* We need to check that we can force a fault on access to the
373 * commpage at 0xffff0fxx
375 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
377 /* If the commpage lies within the already allocated guest space,
378 * then there is no way we can allocate it.
380 if (test_page_addr >= guest_base
381 && test_page_addr <= (guest_base + guest_size)) {
382 return -1;
385 /* Note it needs to be writeable to let us initialise it */
386 real_start = (unsigned long)
387 mmap((void *)test_page_addr, qemu_host_page_size,
388 PROT_READ | PROT_WRITE,
389 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
391 /* If we can't map it then try another address */
392 if (real_start == -1ul) {
393 return 0;
396 if (real_start != test_page_addr) {
397 /* OS didn't put the page where we asked - unmap and reject */
398 munmap((void *)real_start, qemu_host_page_size);
399 return 0;
402 /* Leave the page mapped
403 * Populate it (mmap should have left it all 0'd)
406 /* Kernel helper versions */
407 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
409 /* Now it's populated make it RO */
410 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
411 perror("Protecting guest commpage");
412 exit(-1);
415 return 1; /* All good */
418 #define ELF_HWCAP get_elf_hwcap()
419 #define ELF_HWCAP2 get_elf_hwcap2()
421 static uint32_t get_elf_hwcap(void)
423 ARMCPU *cpu = ARM_CPU(thread_cpu);
424 uint32_t hwcaps = 0;
426 hwcaps |= ARM_HWCAP_ARM_SWP;
427 hwcaps |= ARM_HWCAP_ARM_HALF;
428 hwcaps |= ARM_HWCAP_ARM_THUMB;
429 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
431 /* probe for the extra features */
432 #define GET_FEATURE(feat, hwcap) \
433 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
434 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
435 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
436 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
437 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
438 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
439 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
440 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
441 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
442 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
443 GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA);
444 GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT);
445 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
446 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
447 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
448 * to our VFP_FP16 feature bit.
450 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
451 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
453 return hwcaps;
456 static uint32_t get_elf_hwcap2(void)
458 ARMCPU *cpu = ARM_CPU(thread_cpu);
459 uint32_t hwcaps = 0;
461 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP2_ARM_AES);
462 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP2_ARM_PMULL);
463 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP2_ARM_SHA1);
464 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP2_ARM_SHA2);
465 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP2_ARM_CRC32);
466 return hwcaps;
469 #undef GET_FEATURE
471 #else
472 /* 64 bit ARM definitions */
473 #define ELF_START_MMAP 0x80000000
475 #define ELF_ARCH EM_AARCH64
476 #define ELF_CLASS ELFCLASS64
477 #define ELF_PLATFORM "aarch64"
479 static inline void init_thread(struct target_pt_regs *regs,
480 struct image_info *infop)
482 abi_long stack = infop->start_stack;
483 memset(regs, 0, sizeof(*regs));
485 regs->pc = infop->entry & ~0x3ULL;
486 regs->sp = stack;
489 #define ELF_NREG 34
490 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
492 static void elf_core_copy_regs(target_elf_gregset_t *regs,
493 const CPUARMState *env)
495 int i;
497 for (i = 0; i < 32; i++) {
498 (*regs)[i] = tswapreg(env->xregs[i]);
500 (*regs)[32] = tswapreg(env->pc);
501 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
504 #define USE_ELF_CORE_DUMP
505 #define ELF_EXEC_PAGESIZE 4096
507 enum {
508 ARM_HWCAP_A64_FP = 1 << 0,
509 ARM_HWCAP_A64_ASIMD = 1 << 1,
510 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
511 ARM_HWCAP_A64_AES = 1 << 3,
512 ARM_HWCAP_A64_PMULL = 1 << 4,
513 ARM_HWCAP_A64_SHA1 = 1 << 5,
514 ARM_HWCAP_A64_SHA2 = 1 << 6,
515 ARM_HWCAP_A64_CRC32 = 1 << 7,
518 #define ELF_HWCAP get_elf_hwcap()
520 static uint32_t get_elf_hwcap(void)
522 ARMCPU *cpu = ARM_CPU(thread_cpu);
523 uint32_t hwcaps = 0;
525 hwcaps |= ARM_HWCAP_A64_FP;
526 hwcaps |= ARM_HWCAP_A64_ASIMD;
528 /* probe for the extra features */
529 #define GET_FEATURE(feat, hwcap) \
530 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
531 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP_A64_AES);
532 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP_A64_PMULL);
533 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP_A64_SHA1);
534 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP_A64_SHA2);
535 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP_A64_CRC32);
536 #undef GET_FEATURE
538 return hwcaps;
541 #endif /* not TARGET_AARCH64 */
542 #endif /* TARGET_ARM */
544 #ifdef TARGET_UNICORE32
546 #define ELF_START_MMAP 0x80000000
548 #define ELF_CLASS ELFCLASS32
549 #define ELF_DATA ELFDATA2LSB
550 #define ELF_ARCH EM_UNICORE32
552 static inline void init_thread(struct target_pt_regs *regs,
553 struct image_info *infop)
555 abi_long stack = infop->start_stack;
556 memset(regs, 0, sizeof(*regs));
557 regs->UC32_REG_asr = 0x10;
558 regs->UC32_REG_pc = infop->entry & 0xfffffffe;
559 regs->UC32_REG_sp = infop->start_stack;
560 /* FIXME - what to for failure of get_user()? */
561 get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */
562 get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */
563 /* XXX: it seems that r0 is zeroed after ! */
564 regs->UC32_REG_00 = 0;
567 #define ELF_NREG 34
568 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
570 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
572 (*regs)[0] = env->regs[0];
573 (*regs)[1] = env->regs[1];
574 (*regs)[2] = env->regs[2];
575 (*regs)[3] = env->regs[3];
576 (*regs)[4] = env->regs[4];
577 (*regs)[5] = env->regs[5];
578 (*regs)[6] = env->regs[6];
579 (*regs)[7] = env->regs[7];
580 (*regs)[8] = env->regs[8];
581 (*regs)[9] = env->regs[9];
582 (*regs)[10] = env->regs[10];
583 (*regs)[11] = env->regs[11];
584 (*regs)[12] = env->regs[12];
585 (*regs)[13] = env->regs[13];
586 (*regs)[14] = env->regs[14];
587 (*regs)[15] = env->regs[15];
588 (*regs)[16] = env->regs[16];
589 (*regs)[17] = env->regs[17];
590 (*regs)[18] = env->regs[18];
591 (*regs)[19] = env->regs[19];
592 (*regs)[20] = env->regs[20];
593 (*regs)[21] = env->regs[21];
594 (*regs)[22] = env->regs[22];
595 (*regs)[23] = env->regs[23];
596 (*regs)[24] = env->regs[24];
597 (*regs)[25] = env->regs[25];
598 (*regs)[26] = env->regs[26];
599 (*regs)[27] = env->regs[27];
600 (*regs)[28] = env->regs[28];
601 (*regs)[29] = env->regs[29];
602 (*regs)[30] = env->regs[30];
603 (*regs)[31] = env->regs[31];
605 (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
606 (*regs)[33] = env->regs[0]; /* XXX */
609 #define USE_ELF_CORE_DUMP
610 #define ELF_EXEC_PAGESIZE 4096
612 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
614 #endif
616 #ifdef TARGET_SPARC
617 #ifdef TARGET_SPARC64
619 #define ELF_START_MMAP 0x80000000
620 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
621 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
622 #ifndef TARGET_ABI32
623 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
624 #else
625 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
626 #endif
628 #define ELF_CLASS ELFCLASS64
629 #define ELF_ARCH EM_SPARCV9
631 #define STACK_BIAS 2047
633 static inline void init_thread(struct target_pt_regs *regs,
634 struct image_info *infop)
636 #ifndef TARGET_ABI32
637 regs->tstate = 0;
638 #endif
639 regs->pc = infop->entry;
640 regs->npc = regs->pc + 4;
641 regs->y = 0;
642 #ifdef TARGET_ABI32
643 regs->u_regs[14] = infop->start_stack - 16 * 4;
644 #else
645 if (personality(infop->personality) == PER_LINUX32)
646 regs->u_regs[14] = infop->start_stack - 16 * 4;
647 else
648 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
649 #endif
652 #else
653 #define ELF_START_MMAP 0x80000000
654 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
655 | HWCAP_SPARC_MULDIV)
657 #define ELF_CLASS ELFCLASS32
658 #define ELF_ARCH EM_SPARC
660 static inline void init_thread(struct target_pt_regs *regs,
661 struct image_info *infop)
663 regs->psr = 0;
664 regs->pc = infop->entry;
665 regs->npc = regs->pc + 4;
666 regs->y = 0;
667 regs->u_regs[14] = infop->start_stack - 16 * 4;
670 #endif
671 #endif
673 #ifdef TARGET_PPC
675 #define ELF_MACHINE PPC_ELF_MACHINE
676 #define ELF_START_MMAP 0x80000000
678 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
680 #define elf_check_arch(x) ( (x) == EM_PPC64 )
682 #define ELF_CLASS ELFCLASS64
684 #else
686 #define ELF_CLASS ELFCLASS32
688 #endif
690 #define ELF_ARCH EM_PPC
692 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
693 See arch/powerpc/include/asm/cputable.h. */
694 enum {
695 QEMU_PPC_FEATURE_32 = 0x80000000,
696 QEMU_PPC_FEATURE_64 = 0x40000000,
697 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
698 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
699 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
700 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
701 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
702 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
703 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
704 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
705 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
706 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
707 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
708 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
709 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
710 QEMU_PPC_FEATURE_CELL = 0x00010000,
711 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
712 QEMU_PPC_FEATURE_SMT = 0x00004000,
713 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
714 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
715 QEMU_PPC_FEATURE_PA6T = 0x00000800,
716 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
717 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
718 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
719 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
720 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
722 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
723 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
725 /* Feature definitions in AT_HWCAP2. */
726 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
727 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
728 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
729 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
730 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
731 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
734 #define ELF_HWCAP get_elf_hwcap()
736 static uint32_t get_elf_hwcap(void)
738 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
739 uint32_t features = 0;
741 /* We don't have to be terribly complete here; the high points are
742 Altivec/FP/SPE support. Anything else is just a bonus. */
743 #define GET_FEATURE(flag, feature) \
744 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
745 #define GET_FEATURE2(flag, feature) \
746 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
747 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
748 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
749 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
750 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
751 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
752 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
753 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
754 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
755 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
756 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
757 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
758 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
759 QEMU_PPC_FEATURE_ARCH_2_06);
760 #undef GET_FEATURE
761 #undef GET_FEATURE2
763 return features;
766 #define ELF_HWCAP2 get_elf_hwcap2()
768 static uint32_t get_elf_hwcap2(void)
770 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
771 uint32_t features = 0;
773 #define GET_FEATURE(flag, feature) \
774 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
775 #define GET_FEATURE2(flag, feature) \
776 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
778 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
779 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
780 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
781 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07);
783 #undef GET_FEATURE
784 #undef GET_FEATURE2
786 return features;
790 * The requirements here are:
791 * - keep the final alignment of sp (sp & 0xf)
792 * - make sure the 32-bit value at the first 16 byte aligned position of
793 * AUXV is greater than 16 for glibc compatibility.
794 * AT_IGNOREPPC is used for that.
795 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
796 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
798 #define DLINFO_ARCH_ITEMS 5
799 #define ARCH_DLINFO \
800 do { \
801 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
802 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
803 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
804 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
805 /* \
806 * Now handle glibc compatibility. \
807 */ \
808 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
809 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
810 } while (0)
812 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
814 _regs->gpr[1] = infop->start_stack;
815 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
816 if (get_ppc64_abi(infop) < 2) {
817 uint64_t val;
818 get_user_u64(val, infop->entry + 8);
819 _regs->gpr[2] = val + infop->load_bias;
820 get_user_u64(val, infop->entry);
821 infop->entry = val + infop->load_bias;
822 } else {
823 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
825 #endif
826 _regs->nip = infop->entry;
829 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
830 #define ELF_NREG 48
831 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
833 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
835 int i;
836 target_ulong ccr = 0;
838 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
839 (*regs)[i] = tswapreg(env->gpr[i]);
842 (*regs)[32] = tswapreg(env->nip);
843 (*regs)[33] = tswapreg(env->msr);
844 (*regs)[35] = tswapreg(env->ctr);
845 (*regs)[36] = tswapreg(env->lr);
846 (*regs)[37] = tswapreg(env->xer);
848 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
849 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
851 (*regs)[38] = tswapreg(ccr);
854 #define USE_ELF_CORE_DUMP
855 #define ELF_EXEC_PAGESIZE 4096
857 #endif
859 #ifdef TARGET_MIPS
861 #define ELF_START_MMAP 0x80000000
863 #ifdef TARGET_MIPS64
864 #define ELF_CLASS ELFCLASS64
865 #else
866 #define ELF_CLASS ELFCLASS32
867 #endif
868 #define ELF_ARCH EM_MIPS
870 static inline void init_thread(struct target_pt_regs *regs,
871 struct image_info *infop)
873 regs->cp0_status = 2 << CP0St_KSU;
874 regs->cp0_epc = infop->entry;
875 regs->regs[29] = infop->start_stack;
878 /* See linux kernel: arch/mips/include/asm/elf.h. */
879 #define ELF_NREG 45
880 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
882 /* See linux kernel: arch/mips/include/asm/reg.h. */
883 enum {
884 #ifdef TARGET_MIPS64
885 TARGET_EF_R0 = 0,
886 #else
887 TARGET_EF_R0 = 6,
888 #endif
889 TARGET_EF_R26 = TARGET_EF_R0 + 26,
890 TARGET_EF_R27 = TARGET_EF_R0 + 27,
891 TARGET_EF_LO = TARGET_EF_R0 + 32,
892 TARGET_EF_HI = TARGET_EF_R0 + 33,
893 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
894 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
895 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
896 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
899 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
900 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
902 int i;
904 for (i = 0; i < TARGET_EF_R0; i++) {
905 (*regs)[i] = 0;
907 (*regs)[TARGET_EF_R0] = 0;
909 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
910 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
913 (*regs)[TARGET_EF_R26] = 0;
914 (*regs)[TARGET_EF_R27] = 0;
915 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
916 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
917 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
918 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
919 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
920 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
923 #define USE_ELF_CORE_DUMP
924 #define ELF_EXEC_PAGESIZE 4096
926 #endif /* TARGET_MIPS */
928 #ifdef TARGET_MICROBLAZE
930 #define ELF_START_MMAP 0x80000000
932 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
934 #define ELF_CLASS ELFCLASS32
935 #define ELF_ARCH EM_MICROBLAZE
937 static inline void init_thread(struct target_pt_regs *regs,
938 struct image_info *infop)
940 regs->pc = infop->entry;
941 regs->r1 = infop->start_stack;
945 #define ELF_EXEC_PAGESIZE 4096
947 #define USE_ELF_CORE_DUMP
948 #define ELF_NREG 38
949 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
951 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
952 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
954 int i, pos = 0;
956 for (i = 0; i < 32; i++) {
957 (*regs)[pos++] = tswapreg(env->regs[i]);
960 for (i = 0; i < 6; i++) {
961 (*regs)[pos++] = tswapreg(env->sregs[i]);
965 #endif /* TARGET_MICROBLAZE */
967 #ifdef TARGET_OPENRISC
969 #define ELF_START_MMAP 0x08000000
971 #define ELF_ARCH EM_OPENRISC
972 #define ELF_CLASS ELFCLASS32
973 #define ELF_DATA ELFDATA2MSB
975 static inline void init_thread(struct target_pt_regs *regs,
976 struct image_info *infop)
978 regs->pc = infop->entry;
979 regs->gpr[1] = infop->start_stack;
982 #define USE_ELF_CORE_DUMP
983 #define ELF_EXEC_PAGESIZE 8192
985 /* See linux kernel arch/openrisc/include/asm/elf.h. */
986 #define ELF_NREG 34 /* gprs and pc, sr */
987 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
989 static void elf_core_copy_regs(target_elf_gregset_t *regs,
990 const CPUOpenRISCState *env)
992 int i;
994 for (i = 0; i < 32; i++) {
995 (*regs)[i] = tswapreg(env->gpr[i]);
998 (*regs)[32] = tswapreg(env->pc);
999 (*regs)[33] = tswapreg(env->sr);
1001 #define ELF_HWCAP 0
1002 #define ELF_PLATFORM NULL
1004 #endif /* TARGET_OPENRISC */
1006 #ifdef TARGET_SH4
1008 #define ELF_START_MMAP 0x80000000
1010 #define ELF_CLASS ELFCLASS32
1011 #define ELF_ARCH EM_SH
1013 static inline void init_thread(struct target_pt_regs *regs,
1014 struct image_info *infop)
1016 /* Check other registers XXXXX */
1017 regs->pc = infop->entry;
1018 regs->regs[15] = infop->start_stack;
1021 /* See linux kernel: arch/sh/include/asm/elf.h. */
1022 #define ELF_NREG 23
1023 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1025 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1026 enum {
1027 TARGET_REG_PC = 16,
1028 TARGET_REG_PR = 17,
1029 TARGET_REG_SR = 18,
1030 TARGET_REG_GBR = 19,
1031 TARGET_REG_MACH = 20,
1032 TARGET_REG_MACL = 21,
1033 TARGET_REG_SYSCALL = 22
1036 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1037 const CPUSH4State *env)
1039 int i;
1041 for (i = 0; i < 16; i++) {
1042 (*regs[i]) = tswapreg(env->gregs[i]);
1045 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1046 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1047 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1048 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1049 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1050 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1051 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1054 #define USE_ELF_CORE_DUMP
1055 #define ELF_EXEC_PAGESIZE 4096
1057 enum {
1058 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1059 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1060 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1061 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1062 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1063 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1064 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1065 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1066 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1067 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1070 #define ELF_HWCAP get_elf_hwcap()
1072 static uint32_t get_elf_hwcap(void)
1074 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1075 uint32_t hwcap = 0;
1077 hwcap |= SH_CPU_HAS_FPU;
1079 if (cpu->env.features & SH_FEATURE_SH4A) {
1080 hwcap |= SH_CPU_HAS_LLSC;
1083 return hwcap;
1086 #endif
1088 #ifdef TARGET_CRIS
1090 #define ELF_START_MMAP 0x80000000
1092 #define ELF_CLASS ELFCLASS32
1093 #define ELF_ARCH EM_CRIS
1095 static inline void init_thread(struct target_pt_regs *regs,
1096 struct image_info *infop)
1098 regs->erp = infop->entry;
1101 #define ELF_EXEC_PAGESIZE 8192
1103 #endif
1105 #ifdef TARGET_M68K
1107 #define ELF_START_MMAP 0x80000000
1109 #define ELF_CLASS ELFCLASS32
1110 #define ELF_ARCH EM_68K
1112 /* ??? Does this need to do anything?
1113 #define ELF_PLAT_INIT(_r) */
1115 static inline void init_thread(struct target_pt_regs *regs,
1116 struct image_info *infop)
1118 regs->usp = infop->start_stack;
1119 regs->sr = 0;
1120 regs->pc = infop->entry;
1123 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1124 #define ELF_NREG 20
1125 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1127 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1129 (*regs)[0] = tswapreg(env->dregs[1]);
1130 (*regs)[1] = tswapreg(env->dregs[2]);
1131 (*regs)[2] = tswapreg(env->dregs[3]);
1132 (*regs)[3] = tswapreg(env->dregs[4]);
1133 (*regs)[4] = tswapreg(env->dregs[5]);
1134 (*regs)[5] = tswapreg(env->dregs[6]);
1135 (*regs)[6] = tswapreg(env->dregs[7]);
1136 (*regs)[7] = tswapreg(env->aregs[0]);
1137 (*regs)[8] = tswapreg(env->aregs[1]);
1138 (*regs)[9] = tswapreg(env->aregs[2]);
1139 (*regs)[10] = tswapreg(env->aregs[3]);
1140 (*regs)[11] = tswapreg(env->aregs[4]);
1141 (*regs)[12] = tswapreg(env->aregs[5]);
1142 (*regs)[13] = tswapreg(env->aregs[6]);
1143 (*regs)[14] = tswapreg(env->dregs[0]);
1144 (*regs)[15] = tswapreg(env->aregs[7]);
1145 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1146 (*regs)[17] = tswapreg(env->sr);
1147 (*regs)[18] = tswapreg(env->pc);
1148 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1151 #define USE_ELF_CORE_DUMP
1152 #define ELF_EXEC_PAGESIZE 8192
1154 #endif
1156 #ifdef TARGET_ALPHA
1158 #define ELF_START_MMAP (0x30000000000ULL)
1160 #define ELF_CLASS ELFCLASS64
1161 #define ELF_ARCH EM_ALPHA
1163 static inline void init_thread(struct target_pt_regs *regs,
1164 struct image_info *infop)
1166 regs->pc = infop->entry;
1167 regs->ps = 8;
1168 regs->usp = infop->start_stack;
1171 #define ELF_EXEC_PAGESIZE 8192
1173 #endif /* TARGET_ALPHA */
1175 #ifdef TARGET_S390X
1177 #define ELF_START_MMAP (0x20000000000ULL)
1179 #define ELF_CLASS ELFCLASS64
1180 #define ELF_DATA ELFDATA2MSB
1181 #define ELF_ARCH EM_S390
1183 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1185 regs->psw.addr = infop->entry;
1186 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1187 regs->gprs[15] = infop->start_stack;
1190 #endif /* TARGET_S390X */
1192 #ifdef TARGET_TILEGX
1194 /* 42 bits real used address, a half for user mode */
1195 #define ELF_START_MMAP (0x00000020000000000ULL)
1197 #define elf_check_arch(x) ((x) == EM_TILEGX)
1199 #define ELF_CLASS ELFCLASS64
1200 #define ELF_DATA ELFDATA2LSB
1201 #define ELF_ARCH EM_TILEGX
1203 static inline void init_thread(struct target_pt_regs *regs,
1204 struct image_info *infop)
1206 regs->pc = infop->entry;
1207 regs->sp = infop->start_stack;
1211 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1213 #endif /* TARGET_TILEGX */
1215 #ifndef ELF_PLATFORM
1216 #define ELF_PLATFORM (NULL)
1217 #endif
1219 #ifndef ELF_MACHINE
1220 #define ELF_MACHINE ELF_ARCH
1221 #endif
1223 #ifndef elf_check_arch
1224 #define elf_check_arch(x) ((x) == ELF_ARCH)
1225 #endif
1227 #ifndef ELF_HWCAP
1228 #define ELF_HWCAP 0
1229 #endif
1231 #ifdef TARGET_ABI32
1232 #undef ELF_CLASS
1233 #define ELF_CLASS ELFCLASS32
1234 #undef bswaptls
1235 #define bswaptls(ptr) bswap32s(ptr)
1236 #endif
1238 #include "elf.h"
1240 struct exec
1242 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1243 unsigned int a_text; /* length of text, in bytes */
1244 unsigned int a_data; /* length of data, in bytes */
1245 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1246 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1247 unsigned int a_entry; /* start address */
1248 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1249 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1253 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1254 #define OMAGIC 0407
1255 #define NMAGIC 0410
1256 #define ZMAGIC 0413
1257 #define QMAGIC 0314
1259 /* Necessary parameters */
1260 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1261 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1262 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1263 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1265 #define DLINFO_ITEMS 14
1267 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1269 memcpy(to, from, n);
1272 #ifdef BSWAP_NEEDED
1273 static void bswap_ehdr(struct elfhdr *ehdr)
1275 bswap16s(&ehdr->e_type); /* Object file type */
1276 bswap16s(&ehdr->e_machine); /* Architecture */
1277 bswap32s(&ehdr->e_version); /* Object file version */
1278 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1279 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1280 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1281 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1282 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1283 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1284 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1285 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1286 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1287 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1290 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1292 int i;
1293 for (i = 0; i < phnum; ++i, ++phdr) {
1294 bswap32s(&phdr->p_type); /* Segment type */
1295 bswap32s(&phdr->p_flags); /* Segment flags */
1296 bswaptls(&phdr->p_offset); /* Segment file offset */
1297 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1298 bswaptls(&phdr->p_paddr); /* Segment physical address */
1299 bswaptls(&phdr->p_filesz); /* Segment size in file */
1300 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1301 bswaptls(&phdr->p_align); /* Segment alignment */
1305 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1307 int i;
1308 for (i = 0; i < shnum; ++i, ++shdr) {
1309 bswap32s(&shdr->sh_name);
1310 bswap32s(&shdr->sh_type);
1311 bswaptls(&shdr->sh_flags);
1312 bswaptls(&shdr->sh_addr);
1313 bswaptls(&shdr->sh_offset);
1314 bswaptls(&shdr->sh_size);
1315 bswap32s(&shdr->sh_link);
1316 bswap32s(&shdr->sh_info);
1317 bswaptls(&shdr->sh_addralign);
1318 bswaptls(&shdr->sh_entsize);
1322 static void bswap_sym(struct elf_sym *sym)
1324 bswap32s(&sym->st_name);
1325 bswaptls(&sym->st_value);
1326 bswaptls(&sym->st_size);
1327 bswap16s(&sym->st_shndx);
1329 #else
1330 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1331 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1332 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1333 static inline void bswap_sym(struct elf_sym *sym) { }
1334 #endif
1336 #ifdef USE_ELF_CORE_DUMP
1337 static int elf_core_dump(int, const CPUArchState *);
1338 #endif /* USE_ELF_CORE_DUMP */
1339 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1341 /* Verify the portions of EHDR within E_IDENT for the target.
1342 This can be performed before bswapping the entire header. */
1343 static bool elf_check_ident(struct elfhdr *ehdr)
1345 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1346 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1347 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1348 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1349 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1350 && ehdr->e_ident[EI_DATA] == ELF_DATA
1351 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1354 /* Verify the portions of EHDR outside of E_IDENT for the target.
1355 This has to wait until after bswapping the header. */
1356 static bool elf_check_ehdr(struct elfhdr *ehdr)
1358 return (elf_check_arch(ehdr->e_machine)
1359 && ehdr->e_ehsize == sizeof(struct elfhdr)
1360 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1361 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1365 * 'copy_elf_strings()' copies argument/envelope strings from user
1366 * memory to free pages in kernel mem. These are in a format ready
1367 * to be put directly into the top of new user memory.
1370 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1371 abi_ulong p, abi_ulong stack_limit)
1373 char *tmp;
1374 int len, offset;
1375 abi_ulong top = p;
1377 if (!p) {
1378 return 0; /* bullet-proofing */
1381 offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1383 while (argc-- > 0) {
1384 tmp = argv[argc];
1385 if (!tmp) {
1386 fprintf(stderr, "VFS: argc is wrong");
1387 exit(-1);
1389 len = strlen(tmp) + 1;
1390 tmp += len;
1392 if (len > (p - stack_limit)) {
1393 return 0;
1395 while (len) {
1396 int bytes_to_copy = (len > offset) ? offset : len;
1397 tmp -= bytes_to_copy;
1398 p -= bytes_to_copy;
1399 offset -= bytes_to_copy;
1400 len -= bytes_to_copy;
1402 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1404 if (offset == 0) {
1405 memcpy_to_target(p, scratch, top - p);
1406 top = p;
1407 offset = TARGET_PAGE_SIZE;
1411 if (offset) {
1412 memcpy_to_target(p, scratch + offset, top - p);
1415 return p;
1418 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1419 * argument/environment space. Newer kernels (>2.6.33) allow more,
1420 * dependent on stack size, but guarantee at least 32 pages for
1421 * backwards compatibility.
1423 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1425 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1426 struct image_info *info)
1428 abi_ulong size, error, guard;
1430 size = guest_stack_size;
1431 if (size < STACK_LOWER_LIMIT) {
1432 size = STACK_LOWER_LIMIT;
1434 guard = TARGET_PAGE_SIZE;
1435 if (guard < qemu_real_host_page_size) {
1436 guard = qemu_real_host_page_size;
1439 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1440 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1441 if (error == -1) {
1442 perror("mmap stack");
1443 exit(-1);
1446 /* We reserve one extra page at the top of the stack as guard. */
1447 target_mprotect(error, guard, PROT_NONE);
1449 info->stack_limit = error + guard;
1451 return info->stack_limit + size - sizeof(void *);
1454 /* Map and zero the bss. We need to explicitly zero any fractional pages
1455 after the data section (i.e. bss). */
1456 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1458 uintptr_t host_start, host_map_start, host_end;
1460 last_bss = TARGET_PAGE_ALIGN(last_bss);
1462 /* ??? There is confusion between qemu_real_host_page_size and
1463 qemu_host_page_size here and elsewhere in target_mmap, which
1464 may lead to the end of the data section mapping from the file
1465 not being mapped. At least there was an explicit test and
1466 comment for that here, suggesting that "the file size must
1467 be known". The comment probably pre-dates the introduction
1468 of the fstat system call in target_mmap which does in fact
1469 find out the size. What isn't clear is if the workaround
1470 here is still actually needed. For now, continue with it,
1471 but merge it with the "normal" mmap that would allocate the bss. */
1473 host_start = (uintptr_t) g2h(elf_bss);
1474 host_end = (uintptr_t) g2h(last_bss);
1475 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1477 if (host_map_start < host_end) {
1478 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1479 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1480 if (p == MAP_FAILED) {
1481 perror("cannot mmap brk");
1482 exit(-1);
1486 /* Ensure that the bss page(s) are valid */
1487 if ((page_get_flags(last_bss-1) & prot) != prot) {
1488 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1491 if (host_start < host_map_start) {
1492 memset((void *)host_start, 0, host_map_start - host_start);
1496 #ifdef CONFIG_USE_FDPIC
1497 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1499 uint16_t n;
1500 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1502 /* elf32_fdpic_loadseg */
1503 n = info->nsegs;
1504 while (n--) {
1505 sp -= 12;
1506 put_user_u32(loadsegs[n].addr, sp+0);
1507 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1508 put_user_u32(loadsegs[n].p_memsz, sp+8);
1511 /* elf32_fdpic_loadmap */
1512 sp -= 4;
1513 put_user_u16(0, sp+0); /* version */
1514 put_user_u16(info->nsegs, sp+2); /* nsegs */
1516 info->personality = PER_LINUX_FDPIC;
1517 info->loadmap_addr = sp;
1519 return sp;
1521 #endif
1523 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1524 struct elfhdr *exec,
1525 struct image_info *info,
1526 struct image_info *interp_info)
1528 abi_ulong sp;
1529 abi_ulong sp_auxv;
1530 int size;
1531 int i;
1532 abi_ulong u_rand_bytes;
1533 uint8_t k_rand_bytes[16];
1534 abi_ulong u_platform;
1535 const char *k_platform;
1536 const int n = sizeof(elf_addr_t);
1538 sp = p;
1540 #ifdef CONFIG_USE_FDPIC
1541 /* Needs to be before we load the env/argc/... */
1542 if (elf_is_fdpic(exec)) {
1543 /* Need 4 byte alignment for these structs */
1544 sp &= ~3;
1545 sp = loader_build_fdpic_loadmap(info, sp);
1546 info->other_info = interp_info;
1547 if (interp_info) {
1548 interp_info->other_info = info;
1549 sp = loader_build_fdpic_loadmap(interp_info, sp);
1552 #endif
1554 u_platform = 0;
1555 k_platform = ELF_PLATFORM;
1556 if (k_platform) {
1557 size_t len = strlen(k_platform) + 1;
1558 sp -= (len + n - 1) & ~(n - 1);
1559 u_platform = sp;
1560 /* FIXME - check return value of memcpy_to_target() for failure */
1561 memcpy_to_target(sp, k_platform, len);
1565 * Generate 16 random bytes for userspace PRNG seeding (not
1566 * cryptically secure but it's not the aim of QEMU).
1568 for (i = 0; i < 16; i++) {
1569 k_rand_bytes[i] = rand();
1571 sp -= 16;
1572 u_rand_bytes = sp;
1573 /* FIXME - check return value of memcpy_to_target() for failure */
1574 memcpy_to_target(sp, k_rand_bytes, 16);
1577 * Force 16 byte _final_ alignment here for generality.
1579 sp = sp &~ (abi_ulong)15;
1580 size = (DLINFO_ITEMS + 1) * 2;
1581 if (k_platform)
1582 size += 2;
1583 #ifdef DLINFO_ARCH_ITEMS
1584 size += DLINFO_ARCH_ITEMS * 2;
1585 #endif
1586 #ifdef ELF_HWCAP2
1587 size += 2;
1588 #endif
1589 size += envc + argc + 2;
1590 size += 1; /* argc itself */
1591 size *= n;
1592 if (size & 15)
1593 sp -= 16 - (size & 15);
1595 /* This is correct because Linux defines
1596 * elf_addr_t as Elf32_Off / Elf64_Off
1598 #define NEW_AUX_ENT(id, val) do { \
1599 sp -= n; put_user_ual(val, sp); \
1600 sp -= n; put_user_ual(id, sp); \
1601 } while(0)
1603 sp_auxv = sp;
1604 NEW_AUX_ENT (AT_NULL, 0);
1606 /* There must be exactly DLINFO_ITEMS entries here. */
1607 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1608 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1609 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1610 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize())));
1611 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1612 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1613 NEW_AUX_ENT(AT_ENTRY, info->entry);
1614 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1615 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1616 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1617 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1618 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1619 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1620 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1622 #ifdef ELF_HWCAP2
1623 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1624 #endif
1626 if (k_platform)
1627 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1628 #ifdef ARCH_DLINFO
1630 * ARCH_DLINFO must come last so platform specific code can enforce
1631 * special alignment requirements on the AUXV if necessary (eg. PPC).
1633 ARCH_DLINFO;
1634 #endif
1635 #undef NEW_AUX_ENT
1637 info->saved_auxv = sp;
1638 info->auxv_len = sp_auxv - sp;
1640 sp = loader_build_argptr(envc, argc, sp, p, 0);
1641 /* Check the right amount of stack was allocated for auxvec, envp & argv. */
1642 assert(sp_auxv - sp == size);
1643 return sp;
1646 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1647 /* If the guest doesn't have a validation function just agree */
1648 static int validate_guest_space(unsigned long guest_base,
1649 unsigned long guest_size)
1651 return 1;
1653 #endif
1655 unsigned long init_guest_space(unsigned long host_start,
1656 unsigned long host_size,
1657 unsigned long guest_start,
1658 bool fixed)
1660 unsigned long current_start, real_start;
1661 int flags;
1663 assert(host_start || host_size);
1665 /* If just a starting address is given, then just verify that
1666 * address. */
1667 if (host_start && !host_size) {
1668 if (validate_guest_space(host_start, host_size) == 1) {
1669 return host_start;
1670 } else {
1671 return (unsigned long)-1;
1675 /* Setup the initial flags and start address. */
1676 current_start = host_start & qemu_host_page_mask;
1677 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1678 if (fixed) {
1679 flags |= MAP_FIXED;
1682 /* Otherwise, a non-zero size region of memory needs to be mapped
1683 * and validated. */
1684 while (1) {
1685 unsigned long real_size = host_size;
1687 /* Do not use mmap_find_vma here because that is limited to the
1688 * guest address space. We are going to make the
1689 * guest address space fit whatever we're given.
1691 real_start = (unsigned long)
1692 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1693 if (real_start == (unsigned long)-1) {
1694 return (unsigned long)-1;
1697 /* Ensure the address is properly aligned. */
1698 if (real_start & ~qemu_host_page_mask) {
1699 munmap((void *)real_start, host_size);
1700 real_size = host_size + qemu_host_page_size;
1701 real_start = (unsigned long)
1702 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1703 if (real_start == (unsigned long)-1) {
1704 return (unsigned long)-1;
1706 real_start = HOST_PAGE_ALIGN(real_start);
1709 /* Check to see if the address is valid. */
1710 if (!host_start || real_start == current_start) {
1711 int valid = validate_guest_space(real_start - guest_start,
1712 real_size);
1713 if (valid == 1) {
1714 break;
1715 } else if (valid == -1) {
1716 return (unsigned long)-1;
1718 /* valid == 0, so try again. */
1721 /* That address didn't work. Unmap and try a different one.
1722 * The address the host picked because is typically right at
1723 * the top of the host address space and leaves the guest with
1724 * no usable address space. Resort to a linear search. We
1725 * already compensated for mmap_min_addr, so this should not
1726 * happen often. Probably means we got unlucky and host
1727 * address space randomization put a shared library somewhere
1728 * inconvenient.
1730 munmap((void *)real_start, host_size);
1731 current_start += qemu_host_page_size;
1732 if (host_start == current_start) {
1733 /* Theoretically possible if host doesn't have any suitably
1734 * aligned areas. Normally the first mmap will fail.
1736 return (unsigned long)-1;
1740 qemu_log_mask(CPU_LOG_PAGE, "Reserved 0x%lx bytes of guest address space\n", host_size);
1742 return real_start;
1745 static void probe_guest_base(const char *image_name,
1746 abi_ulong loaddr, abi_ulong hiaddr)
1748 /* Probe for a suitable guest base address, if the user has not set
1749 * it explicitly, and set guest_base appropriately.
1750 * In case of error we will print a suitable message and exit.
1752 const char *errmsg;
1753 if (!have_guest_base && !reserved_va) {
1754 unsigned long host_start, real_start, host_size;
1756 /* Round addresses to page boundaries. */
1757 loaddr &= qemu_host_page_mask;
1758 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1760 if (loaddr < mmap_min_addr) {
1761 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1762 } else {
1763 host_start = loaddr;
1764 if (host_start != loaddr) {
1765 errmsg = "Address overflow loading ELF binary";
1766 goto exit_errmsg;
1769 host_size = hiaddr - loaddr;
1771 /* Setup the initial guest memory space with ranges gleaned from
1772 * the ELF image that is being loaded.
1774 real_start = init_guest_space(host_start, host_size, loaddr, false);
1775 if (real_start == (unsigned long)-1) {
1776 errmsg = "Unable to find space for application";
1777 goto exit_errmsg;
1779 guest_base = real_start - loaddr;
1781 qemu_log_mask(CPU_LOG_PAGE, "Relocating guest address space from 0x"
1782 TARGET_ABI_FMT_lx " to 0x%lx\n",
1783 loaddr, real_start);
1785 return;
1787 exit_errmsg:
1788 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1789 exit(-1);
1793 /* Load an ELF image into the address space.
1795 IMAGE_NAME is the filename of the image, to use in error messages.
1796 IMAGE_FD is the open file descriptor for the image.
1798 BPRM_BUF is a copy of the beginning of the file; this of course
1799 contains the elf file header at offset 0. It is assumed that this
1800 buffer is sufficiently aligned to present no problems to the host
1801 in accessing data at aligned offsets within the buffer.
1803 On return: INFO values will be filled in, as necessary or available. */
1805 static void load_elf_image(const char *image_name, int image_fd,
1806 struct image_info *info, char **pinterp_name,
1807 char bprm_buf[BPRM_BUF_SIZE])
1809 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1810 struct elf_phdr *phdr;
1811 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1812 int i, retval;
1813 const char *errmsg;
1815 /* First of all, some simple consistency checks */
1816 errmsg = "Invalid ELF image for this architecture";
1817 if (!elf_check_ident(ehdr)) {
1818 goto exit_errmsg;
1820 bswap_ehdr(ehdr);
1821 if (!elf_check_ehdr(ehdr)) {
1822 goto exit_errmsg;
1825 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1826 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1827 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
1828 } else {
1829 phdr = (struct elf_phdr *) alloca(i);
1830 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
1831 if (retval != i) {
1832 goto exit_read;
1835 bswap_phdr(phdr, ehdr->e_phnum);
1837 #ifdef CONFIG_USE_FDPIC
1838 info->nsegs = 0;
1839 info->pt_dynamic_addr = 0;
1840 #endif
1842 /* Find the maximum size of the image and allocate an appropriate
1843 amount of memory to handle that. */
1844 loaddr = -1, hiaddr = 0;
1845 for (i = 0; i < ehdr->e_phnum; ++i) {
1846 if (phdr[i].p_type == PT_LOAD) {
1847 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
1848 if (a < loaddr) {
1849 loaddr = a;
1851 a = phdr[i].p_vaddr + phdr[i].p_memsz;
1852 if (a > hiaddr) {
1853 hiaddr = a;
1855 #ifdef CONFIG_USE_FDPIC
1856 ++info->nsegs;
1857 #endif
1861 load_addr = loaddr;
1862 if (ehdr->e_type == ET_DYN) {
1863 /* The image indicates that it can be loaded anywhere. Find a
1864 location that can hold the memory space required. If the
1865 image is pre-linked, LOADDR will be non-zero. Since we do
1866 not supply MAP_FIXED here we'll use that address if and
1867 only if it remains available. */
1868 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
1869 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
1870 -1, 0);
1871 if (load_addr == -1) {
1872 goto exit_perror;
1874 } else if (pinterp_name != NULL) {
1875 /* This is the main executable. Make sure that the low
1876 address does not conflict with MMAP_MIN_ADDR or the
1877 QEMU application itself. */
1878 probe_guest_base(image_name, loaddr, hiaddr);
1880 load_bias = load_addr - loaddr;
1882 #ifdef CONFIG_USE_FDPIC
1884 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
1885 g_malloc(sizeof(*loadsegs) * info->nsegs);
1887 for (i = 0; i < ehdr->e_phnum; ++i) {
1888 switch (phdr[i].p_type) {
1889 case PT_DYNAMIC:
1890 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
1891 break;
1892 case PT_LOAD:
1893 loadsegs->addr = phdr[i].p_vaddr + load_bias;
1894 loadsegs->p_vaddr = phdr[i].p_vaddr;
1895 loadsegs->p_memsz = phdr[i].p_memsz;
1896 ++loadsegs;
1897 break;
1901 #endif
1903 info->load_bias = load_bias;
1904 info->load_addr = load_addr;
1905 info->entry = ehdr->e_entry + load_bias;
1906 info->start_code = -1;
1907 info->end_code = 0;
1908 info->start_data = -1;
1909 info->end_data = 0;
1910 info->brk = 0;
1911 info->elf_flags = ehdr->e_flags;
1913 for (i = 0; i < ehdr->e_phnum; i++) {
1914 struct elf_phdr *eppnt = phdr + i;
1915 if (eppnt->p_type == PT_LOAD) {
1916 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
1917 int elf_prot = 0;
1919 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1920 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1921 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1923 vaddr = load_bias + eppnt->p_vaddr;
1924 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
1925 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
1927 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
1928 elf_prot, MAP_PRIVATE | MAP_FIXED,
1929 image_fd, eppnt->p_offset - vaddr_po);
1930 if (error == -1) {
1931 goto exit_perror;
1934 vaddr_ef = vaddr + eppnt->p_filesz;
1935 vaddr_em = vaddr + eppnt->p_memsz;
1937 /* If the load segment requests extra zeros (e.g. bss), map it. */
1938 if (vaddr_ef < vaddr_em) {
1939 zero_bss(vaddr_ef, vaddr_em, elf_prot);
1942 /* Find the full program boundaries. */
1943 if (elf_prot & PROT_EXEC) {
1944 if (vaddr < info->start_code) {
1945 info->start_code = vaddr;
1947 if (vaddr_ef > info->end_code) {
1948 info->end_code = vaddr_ef;
1951 if (elf_prot & PROT_WRITE) {
1952 if (vaddr < info->start_data) {
1953 info->start_data = vaddr;
1955 if (vaddr_ef > info->end_data) {
1956 info->end_data = vaddr_ef;
1958 if (vaddr_em > info->brk) {
1959 info->brk = vaddr_em;
1962 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
1963 char *interp_name;
1965 if (*pinterp_name) {
1966 errmsg = "Multiple PT_INTERP entries";
1967 goto exit_errmsg;
1969 interp_name = malloc(eppnt->p_filesz);
1970 if (!interp_name) {
1971 goto exit_perror;
1974 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
1975 memcpy(interp_name, bprm_buf + eppnt->p_offset,
1976 eppnt->p_filesz);
1977 } else {
1978 retval = pread(image_fd, interp_name, eppnt->p_filesz,
1979 eppnt->p_offset);
1980 if (retval != eppnt->p_filesz) {
1981 goto exit_perror;
1984 if (interp_name[eppnt->p_filesz - 1] != 0) {
1985 errmsg = "Invalid PT_INTERP entry";
1986 goto exit_errmsg;
1988 *pinterp_name = interp_name;
1992 if (info->end_data == 0) {
1993 info->start_data = info->end_code;
1994 info->end_data = info->end_code;
1995 info->brk = info->end_code;
1998 if (qemu_log_enabled()) {
1999 load_symbols(ehdr, image_fd, load_bias);
2002 close(image_fd);
2003 return;
2005 exit_read:
2006 if (retval >= 0) {
2007 errmsg = "Incomplete read of file header";
2008 goto exit_errmsg;
2010 exit_perror:
2011 errmsg = strerror(errno);
2012 exit_errmsg:
2013 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2014 exit(-1);
2017 static void load_elf_interp(const char *filename, struct image_info *info,
2018 char bprm_buf[BPRM_BUF_SIZE])
2020 int fd, retval;
2022 fd = open(path(filename), O_RDONLY);
2023 if (fd < 0) {
2024 goto exit_perror;
2027 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2028 if (retval < 0) {
2029 goto exit_perror;
2031 if (retval < BPRM_BUF_SIZE) {
2032 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2035 load_elf_image(filename, fd, info, NULL, bprm_buf);
2036 return;
2038 exit_perror:
2039 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2040 exit(-1);
2043 static int symfind(const void *s0, const void *s1)
2045 target_ulong addr = *(target_ulong *)s0;
2046 struct elf_sym *sym = (struct elf_sym *)s1;
2047 int result = 0;
2048 if (addr < sym->st_value) {
2049 result = -1;
2050 } else if (addr >= sym->st_value + sym->st_size) {
2051 result = 1;
2053 return result;
2056 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2058 #if ELF_CLASS == ELFCLASS32
2059 struct elf_sym *syms = s->disas_symtab.elf32;
2060 #else
2061 struct elf_sym *syms = s->disas_symtab.elf64;
2062 #endif
2064 // binary search
2065 struct elf_sym *sym;
2067 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2068 if (sym != NULL) {
2069 return s->disas_strtab + sym->st_name;
2072 return "";
2075 /* FIXME: This should use elf_ops.h */
2076 static int symcmp(const void *s0, const void *s1)
2078 struct elf_sym *sym0 = (struct elf_sym *)s0;
2079 struct elf_sym *sym1 = (struct elf_sym *)s1;
2080 return (sym0->st_value < sym1->st_value)
2081 ? -1
2082 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2085 /* Best attempt to load symbols from this ELF object. */
2086 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2088 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2089 struct elf_shdr *shdr;
2090 char *strings = NULL;
2091 struct syminfo *s = NULL;
2092 struct elf_sym *new_syms, *syms = NULL;
2094 shnum = hdr->e_shnum;
2095 i = shnum * sizeof(struct elf_shdr);
2096 shdr = (struct elf_shdr *)alloca(i);
2097 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2098 return;
2101 bswap_shdr(shdr, shnum);
2102 for (i = 0; i < shnum; ++i) {
2103 if (shdr[i].sh_type == SHT_SYMTAB) {
2104 sym_idx = i;
2105 str_idx = shdr[i].sh_link;
2106 goto found;
2110 /* There will be no symbol table if the file was stripped. */
2111 return;
2113 found:
2114 /* Now know where the strtab and symtab are. Snarf them. */
2115 s = malloc(sizeof(*s));
2116 if (!s) {
2117 goto give_up;
2120 i = shdr[str_idx].sh_size;
2121 s->disas_strtab = strings = malloc(i);
2122 if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
2123 goto give_up;
2126 i = shdr[sym_idx].sh_size;
2127 syms = malloc(i);
2128 if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
2129 goto give_up;
2132 nsyms = i / sizeof(struct elf_sym);
2133 for (i = 0; i < nsyms; ) {
2134 bswap_sym(syms + i);
2135 /* Throw away entries which we do not need. */
2136 if (syms[i].st_shndx == SHN_UNDEF
2137 || syms[i].st_shndx >= SHN_LORESERVE
2138 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2139 if (i < --nsyms) {
2140 syms[i] = syms[nsyms];
2142 } else {
2143 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2144 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2145 syms[i].st_value &= ~(target_ulong)1;
2146 #endif
2147 syms[i].st_value += load_bias;
2148 i++;
2152 /* No "useful" symbol. */
2153 if (nsyms == 0) {
2154 goto give_up;
2157 /* Attempt to free the storage associated with the local symbols
2158 that we threw away. Whether or not this has any effect on the
2159 memory allocation depends on the malloc implementation and how
2160 many symbols we managed to discard. */
2161 new_syms = realloc(syms, nsyms * sizeof(*syms));
2162 if (new_syms == NULL) {
2163 goto give_up;
2165 syms = new_syms;
2167 qsort(syms, nsyms, sizeof(*syms), symcmp);
2169 s->disas_num_syms = nsyms;
2170 #if ELF_CLASS == ELFCLASS32
2171 s->disas_symtab.elf32 = syms;
2172 #else
2173 s->disas_symtab.elf64 = syms;
2174 #endif
2175 s->lookup_symbol = lookup_symbolxx;
2176 s->next = syminfos;
2177 syminfos = s;
2179 return;
2181 give_up:
2182 free(s);
2183 free(strings);
2184 free(syms);
2187 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2189 struct image_info interp_info;
2190 struct elfhdr elf_ex;
2191 char *elf_interpreter = NULL;
2192 char *scratch;
2194 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2196 load_elf_image(bprm->filename, bprm->fd, info,
2197 &elf_interpreter, bprm->buf);
2199 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2200 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2201 when we load the interpreter. */
2202 elf_ex = *(struct elfhdr *)bprm->buf;
2204 /* Do this so that we can load the interpreter, if need be. We will
2205 change some of these later */
2206 bprm->p = setup_arg_pages(bprm, info);
2208 scratch = g_new0(char, TARGET_PAGE_SIZE);
2209 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2210 bprm->p, info->stack_limit);
2211 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2212 bprm->p, info->stack_limit);
2213 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2214 bprm->p, info->stack_limit);
2215 g_free(scratch);
2217 if (!bprm->p) {
2218 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2219 exit(-1);
2222 if (elf_interpreter) {
2223 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2225 /* If the program interpreter is one of these two, then assume
2226 an iBCS2 image. Otherwise assume a native linux image. */
2228 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2229 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2230 info->personality = PER_SVR4;
2232 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2233 and some applications "depend" upon this behavior. Since
2234 we do not have the power to recompile these, we emulate
2235 the SVr4 behavior. Sigh. */
2236 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2237 MAP_FIXED | MAP_PRIVATE, -1, 0);
2241 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2242 info, (elf_interpreter ? &interp_info : NULL));
2243 info->start_stack = bprm->p;
2245 /* If we have an interpreter, set that as the program's entry point.
2246 Copy the load_bias as well, to help PPC64 interpret the entry
2247 point as a function descriptor. Do this after creating elf tables
2248 so that we copy the original program entry point into the AUXV. */
2249 if (elf_interpreter) {
2250 info->load_bias = interp_info.load_bias;
2251 info->entry = interp_info.entry;
2252 free(elf_interpreter);
2255 #ifdef USE_ELF_CORE_DUMP
2256 bprm->core_dump = &elf_core_dump;
2257 #endif
2259 return 0;
2262 #ifdef USE_ELF_CORE_DUMP
2264 * Definitions to generate Intel SVR4-like core files.
2265 * These mostly have the same names as the SVR4 types with "target_elf_"
2266 * tacked on the front to prevent clashes with linux definitions,
2267 * and the typedef forms have been avoided. This is mostly like
2268 * the SVR4 structure, but more Linuxy, with things that Linux does
2269 * not support and which gdb doesn't really use excluded.
2271 * Fields we don't dump (their contents is zero) in linux-user qemu
2272 * are marked with XXX.
2274 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2276 * Porting ELF coredump for target is (quite) simple process. First you
2277 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2278 * the target resides):
2280 * #define USE_ELF_CORE_DUMP
2282 * Next you define type of register set used for dumping. ELF specification
2283 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2285 * typedef <target_regtype> target_elf_greg_t;
2286 * #define ELF_NREG <number of registers>
2287 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2289 * Last step is to implement target specific function that copies registers
2290 * from given cpu into just specified register set. Prototype is:
2292 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2293 * const CPUArchState *env);
2295 * Parameters:
2296 * regs - copy register values into here (allocated and zeroed by caller)
2297 * env - copy registers from here
2299 * Example for ARM target is provided in this file.
2302 /* An ELF note in memory */
2303 struct memelfnote {
2304 const char *name;
2305 size_t namesz;
2306 size_t namesz_rounded;
2307 int type;
2308 size_t datasz;
2309 size_t datasz_rounded;
2310 void *data;
2311 size_t notesz;
2314 struct target_elf_siginfo {
2315 abi_int si_signo; /* signal number */
2316 abi_int si_code; /* extra code */
2317 abi_int si_errno; /* errno */
2320 struct target_elf_prstatus {
2321 struct target_elf_siginfo pr_info; /* Info associated with signal */
2322 abi_short pr_cursig; /* Current signal */
2323 abi_ulong pr_sigpend; /* XXX */
2324 abi_ulong pr_sighold; /* XXX */
2325 target_pid_t pr_pid;
2326 target_pid_t pr_ppid;
2327 target_pid_t pr_pgrp;
2328 target_pid_t pr_sid;
2329 struct target_timeval pr_utime; /* XXX User time */
2330 struct target_timeval pr_stime; /* XXX System time */
2331 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2332 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2333 target_elf_gregset_t pr_reg; /* GP registers */
2334 abi_int pr_fpvalid; /* XXX */
2337 #define ELF_PRARGSZ (80) /* Number of chars for args */
2339 struct target_elf_prpsinfo {
2340 char pr_state; /* numeric process state */
2341 char pr_sname; /* char for pr_state */
2342 char pr_zomb; /* zombie */
2343 char pr_nice; /* nice val */
2344 abi_ulong pr_flag; /* flags */
2345 target_uid_t pr_uid;
2346 target_gid_t pr_gid;
2347 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2348 /* Lots missing */
2349 char pr_fname[16]; /* filename of executable */
2350 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2353 /* Here is the structure in which status of each thread is captured. */
2354 struct elf_thread_status {
2355 QTAILQ_ENTRY(elf_thread_status) ets_link;
2356 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2357 #if 0
2358 elf_fpregset_t fpu; /* NT_PRFPREG */
2359 struct task_struct *thread;
2360 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2361 #endif
2362 struct memelfnote notes[1];
2363 int num_notes;
2366 struct elf_note_info {
2367 struct memelfnote *notes;
2368 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2369 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2371 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2372 #if 0
2374 * Current version of ELF coredump doesn't support
2375 * dumping fp regs etc.
2377 elf_fpregset_t *fpu;
2378 elf_fpxregset_t *xfpu;
2379 int thread_status_size;
2380 #endif
2381 int notes_size;
2382 int numnote;
2385 struct vm_area_struct {
2386 target_ulong vma_start; /* start vaddr of memory region */
2387 target_ulong vma_end; /* end vaddr of memory region */
2388 abi_ulong vma_flags; /* protection etc. flags for the region */
2389 QTAILQ_ENTRY(vm_area_struct) vma_link;
2392 struct mm_struct {
2393 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2394 int mm_count; /* number of mappings */
2397 static struct mm_struct *vma_init(void);
2398 static void vma_delete(struct mm_struct *);
2399 static int vma_add_mapping(struct mm_struct *, target_ulong,
2400 target_ulong, abi_ulong);
2401 static int vma_get_mapping_count(const struct mm_struct *);
2402 static struct vm_area_struct *vma_first(const struct mm_struct *);
2403 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2404 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2405 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2406 unsigned long flags);
2408 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2409 static void fill_note(struct memelfnote *, const char *, int,
2410 unsigned int, void *);
2411 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2412 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2413 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2414 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2415 static size_t note_size(const struct memelfnote *);
2416 static void free_note_info(struct elf_note_info *);
2417 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2418 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2419 static int core_dump_filename(const TaskState *, char *, size_t);
2421 static int dump_write(int, const void *, size_t);
2422 static int write_note(struct memelfnote *, int);
2423 static int write_note_info(struct elf_note_info *, int);
2425 #ifdef BSWAP_NEEDED
2426 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2428 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2429 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2430 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2431 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2432 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2433 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2434 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2435 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2436 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2437 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2438 /* cpu times are not filled, so we skip them */
2439 /* regs should be in correct format already */
2440 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2443 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2445 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2446 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2447 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2448 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2449 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2450 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2451 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2454 static void bswap_note(struct elf_note *en)
2456 bswap32s(&en->n_namesz);
2457 bswap32s(&en->n_descsz);
2458 bswap32s(&en->n_type);
2460 #else
2461 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2462 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2463 static inline void bswap_note(struct elf_note *en) { }
2464 #endif /* BSWAP_NEEDED */
2467 * Minimal support for linux memory regions. These are needed
2468 * when we are finding out what memory exactly belongs to
2469 * emulated process. No locks needed here, as long as
2470 * thread that received the signal is stopped.
2473 static struct mm_struct *vma_init(void)
2475 struct mm_struct *mm;
2477 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2478 return (NULL);
2480 mm->mm_count = 0;
2481 QTAILQ_INIT(&mm->mm_mmap);
2483 return (mm);
2486 static void vma_delete(struct mm_struct *mm)
2488 struct vm_area_struct *vma;
2490 while ((vma = vma_first(mm)) != NULL) {
2491 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2492 g_free(vma);
2494 g_free(mm);
2497 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
2498 target_ulong end, abi_ulong flags)
2500 struct vm_area_struct *vma;
2502 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2503 return (-1);
2505 vma->vma_start = start;
2506 vma->vma_end = end;
2507 vma->vma_flags = flags;
2509 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2510 mm->mm_count++;
2512 return (0);
2515 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2517 return (QTAILQ_FIRST(&mm->mm_mmap));
2520 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2522 return (QTAILQ_NEXT(vma, vma_link));
2525 static int vma_get_mapping_count(const struct mm_struct *mm)
2527 return (mm->mm_count);
2531 * Calculate file (dump) size of given memory region.
2533 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2535 /* if we cannot even read the first page, skip it */
2536 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2537 return (0);
2540 * Usually we don't dump executable pages as they contain
2541 * non-writable code that debugger can read directly from
2542 * target library etc. However, thread stacks are marked
2543 * also executable so we read in first page of given region
2544 * and check whether it contains elf header. If there is
2545 * no elf header, we dump it.
2547 if (vma->vma_flags & PROT_EXEC) {
2548 char page[TARGET_PAGE_SIZE];
2550 copy_from_user(page, vma->vma_start, sizeof (page));
2551 if ((page[EI_MAG0] == ELFMAG0) &&
2552 (page[EI_MAG1] == ELFMAG1) &&
2553 (page[EI_MAG2] == ELFMAG2) &&
2554 (page[EI_MAG3] == ELFMAG3)) {
2556 * Mappings are possibly from ELF binary. Don't dump
2557 * them.
2559 return (0);
2563 return (vma->vma_end - vma->vma_start);
2566 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2567 unsigned long flags)
2569 struct mm_struct *mm = (struct mm_struct *)priv;
2571 vma_add_mapping(mm, start, end, flags);
2572 return (0);
2575 static void fill_note(struct memelfnote *note, const char *name, int type,
2576 unsigned int sz, void *data)
2578 unsigned int namesz;
2580 namesz = strlen(name) + 1;
2581 note->name = name;
2582 note->namesz = namesz;
2583 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2584 note->type = type;
2585 note->datasz = sz;
2586 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2588 note->data = data;
2591 * We calculate rounded up note size here as specified by
2592 * ELF document.
2594 note->notesz = sizeof (struct elf_note) +
2595 note->namesz_rounded + note->datasz_rounded;
2598 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2599 uint32_t flags)
2601 (void) memset(elf, 0, sizeof(*elf));
2603 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2604 elf->e_ident[EI_CLASS] = ELF_CLASS;
2605 elf->e_ident[EI_DATA] = ELF_DATA;
2606 elf->e_ident[EI_VERSION] = EV_CURRENT;
2607 elf->e_ident[EI_OSABI] = ELF_OSABI;
2609 elf->e_type = ET_CORE;
2610 elf->e_machine = machine;
2611 elf->e_version = EV_CURRENT;
2612 elf->e_phoff = sizeof(struct elfhdr);
2613 elf->e_flags = flags;
2614 elf->e_ehsize = sizeof(struct elfhdr);
2615 elf->e_phentsize = sizeof(struct elf_phdr);
2616 elf->e_phnum = segs;
2618 bswap_ehdr(elf);
2621 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2623 phdr->p_type = PT_NOTE;
2624 phdr->p_offset = offset;
2625 phdr->p_vaddr = 0;
2626 phdr->p_paddr = 0;
2627 phdr->p_filesz = sz;
2628 phdr->p_memsz = 0;
2629 phdr->p_flags = 0;
2630 phdr->p_align = 0;
2632 bswap_phdr(phdr, 1);
2635 static size_t note_size(const struct memelfnote *note)
2637 return (note->notesz);
2640 static void fill_prstatus(struct target_elf_prstatus *prstatus,
2641 const TaskState *ts, int signr)
2643 (void) memset(prstatus, 0, sizeof (*prstatus));
2644 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2645 prstatus->pr_pid = ts->ts_tid;
2646 prstatus->pr_ppid = getppid();
2647 prstatus->pr_pgrp = getpgrp();
2648 prstatus->pr_sid = getsid(0);
2650 bswap_prstatus(prstatus);
2653 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2655 char *base_filename;
2656 unsigned int i, len;
2658 (void) memset(psinfo, 0, sizeof (*psinfo));
2660 len = ts->info->arg_end - ts->info->arg_start;
2661 if (len >= ELF_PRARGSZ)
2662 len = ELF_PRARGSZ - 1;
2663 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2664 return -EFAULT;
2665 for (i = 0; i < len; i++)
2666 if (psinfo->pr_psargs[i] == 0)
2667 psinfo->pr_psargs[i] = ' ';
2668 psinfo->pr_psargs[len] = 0;
2670 psinfo->pr_pid = getpid();
2671 psinfo->pr_ppid = getppid();
2672 psinfo->pr_pgrp = getpgrp();
2673 psinfo->pr_sid = getsid(0);
2674 psinfo->pr_uid = getuid();
2675 psinfo->pr_gid = getgid();
2677 base_filename = g_path_get_basename(ts->bprm->filename);
2679 * Using strncpy here is fine: at max-length,
2680 * this field is not NUL-terminated.
2682 (void) strncpy(psinfo->pr_fname, base_filename,
2683 sizeof(psinfo->pr_fname));
2685 g_free(base_filename);
2686 bswap_psinfo(psinfo);
2687 return (0);
2690 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2692 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2693 elf_addr_t orig_auxv = auxv;
2694 void *ptr;
2695 int len = ts->info->auxv_len;
2698 * Auxiliary vector is stored in target process stack. It contains
2699 * {type, value} pairs that we need to dump into note. This is not
2700 * strictly necessary but we do it here for sake of completeness.
2703 /* read in whole auxv vector and copy it to memelfnote */
2704 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2705 if (ptr != NULL) {
2706 fill_note(note, "CORE", NT_AUXV, len, ptr);
2707 unlock_user(ptr, auxv, len);
2712 * Constructs name of coredump file. We have following convention
2713 * for the name:
2714 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2716 * Returns 0 in case of success, -1 otherwise (errno is set).
2718 static int core_dump_filename(const TaskState *ts, char *buf,
2719 size_t bufsize)
2721 char timestamp[64];
2722 char *filename = NULL;
2723 char *base_filename = NULL;
2724 struct timeval tv;
2725 struct tm tm;
2727 assert(bufsize >= PATH_MAX);
2729 if (gettimeofday(&tv, NULL) < 0) {
2730 (void) fprintf(stderr, "unable to get current timestamp: %s",
2731 strerror(errno));
2732 return (-1);
2735 filename = strdup(ts->bprm->filename);
2736 base_filename = strdup(basename(filename));
2737 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2738 localtime_r(&tv.tv_sec, &tm));
2739 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2740 base_filename, timestamp, (int)getpid());
2741 free(base_filename);
2742 free(filename);
2744 return (0);
2747 static int dump_write(int fd, const void *ptr, size_t size)
2749 const char *bufp = (const char *)ptr;
2750 ssize_t bytes_written, bytes_left;
2751 struct rlimit dumpsize;
2752 off_t pos;
2754 bytes_written = 0;
2755 getrlimit(RLIMIT_CORE, &dumpsize);
2756 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2757 if (errno == ESPIPE) { /* not a seekable stream */
2758 bytes_left = size;
2759 } else {
2760 return pos;
2762 } else {
2763 if (dumpsize.rlim_cur <= pos) {
2764 return -1;
2765 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2766 bytes_left = size;
2767 } else {
2768 size_t limit_left=dumpsize.rlim_cur - pos;
2769 bytes_left = limit_left >= size ? size : limit_left ;
2774 * In normal conditions, single write(2) should do but
2775 * in case of socket etc. this mechanism is more portable.
2777 do {
2778 bytes_written = write(fd, bufp, bytes_left);
2779 if (bytes_written < 0) {
2780 if (errno == EINTR)
2781 continue;
2782 return (-1);
2783 } else if (bytes_written == 0) { /* eof */
2784 return (-1);
2786 bufp += bytes_written;
2787 bytes_left -= bytes_written;
2788 } while (bytes_left > 0);
2790 return (0);
2793 static int write_note(struct memelfnote *men, int fd)
2795 struct elf_note en;
2797 en.n_namesz = men->namesz;
2798 en.n_type = men->type;
2799 en.n_descsz = men->datasz;
2801 bswap_note(&en);
2803 if (dump_write(fd, &en, sizeof(en)) != 0)
2804 return (-1);
2805 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2806 return (-1);
2807 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
2808 return (-1);
2810 return (0);
2813 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
2815 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2816 TaskState *ts = (TaskState *)cpu->opaque;
2817 struct elf_thread_status *ets;
2819 ets = g_malloc0(sizeof (*ets));
2820 ets->num_notes = 1; /* only prstatus is dumped */
2821 fill_prstatus(&ets->prstatus, ts, 0);
2822 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2823 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2824 &ets->prstatus);
2826 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
2828 info->notes_size += note_size(&ets->notes[0]);
2831 static void init_note_info(struct elf_note_info *info)
2833 /* Initialize the elf_note_info structure so that it is at
2834 * least safe to call free_note_info() on it. Must be
2835 * called before calling fill_note_info().
2837 memset(info, 0, sizeof (*info));
2838 QTAILQ_INIT(&info->thread_list);
2841 static int fill_note_info(struct elf_note_info *info,
2842 long signr, const CPUArchState *env)
2844 #define NUMNOTES 3
2845 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2846 TaskState *ts = (TaskState *)cpu->opaque;
2847 int i;
2849 info->notes = g_new0(struct memelfnote, NUMNOTES);
2850 if (info->notes == NULL)
2851 return (-ENOMEM);
2852 info->prstatus = g_malloc0(sizeof (*info->prstatus));
2853 if (info->prstatus == NULL)
2854 return (-ENOMEM);
2855 info->psinfo = g_malloc0(sizeof (*info->psinfo));
2856 if (info->prstatus == NULL)
2857 return (-ENOMEM);
2860 * First fill in status (and registers) of current thread
2861 * including process info & aux vector.
2863 fill_prstatus(info->prstatus, ts, signr);
2864 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2865 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2866 sizeof (*info->prstatus), info->prstatus);
2867 fill_psinfo(info->psinfo, ts);
2868 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2869 sizeof (*info->psinfo), info->psinfo);
2870 fill_auxv_note(&info->notes[2], ts);
2871 info->numnote = 3;
2873 info->notes_size = 0;
2874 for (i = 0; i < info->numnote; i++)
2875 info->notes_size += note_size(&info->notes[i]);
2877 /* read and fill status of all threads */
2878 cpu_list_lock();
2879 CPU_FOREACH(cpu) {
2880 if (cpu == thread_cpu) {
2881 continue;
2883 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
2885 cpu_list_unlock();
2887 return (0);
2890 static void free_note_info(struct elf_note_info *info)
2892 struct elf_thread_status *ets;
2894 while (!QTAILQ_EMPTY(&info->thread_list)) {
2895 ets = QTAILQ_FIRST(&info->thread_list);
2896 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
2897 g_free(ets);
2900 g_free(info->prstatus);
2901 g_free(info->psinfo);
2902 g_free(info->notes);
2905 static int write_note_info(struct elf_note_info *info, int fd)
2907 struct elf_thread_status *ets;
2908 int i, error = 0;
2910 /* write prstatus, psinfo and auxv for current thread */
2911 for (i = 0; i < info->numnote; i++)
2912 if ((error = write_note(&info->notes[i], fd)) != 0)
2913 return (error);
2915 /* write prstatus for each thread */
2916 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
2917 if ((error = write_note(&ets->notes[0], fd)) != 0)
2918 return (error);
2921 return (0);
2925 * Write out ELF coredump.
2927 * See documentation of ELF object file format in:
2928 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2930 * Coredump format in linux is following:
2932 * 0 +----------------------+ \
2933 * | ELF header | ET_CORE |
2934 * +----------------------+ |
2935 * | ELF program headers | |--- headers
2936 * | - NOTE section | |
2937 * | - PT_LOAD sections | |
2938 * +----------------------+ /
2939 * | NOTEs: |
2940 * | - NT_PRSTATUS |
2941 * | - NT_PRSINFO |
2942 * | - NT_AUXV |
2943 * +----------------------+ <-- aligned to target page
2944 * | Process memory dump |
2945 * : :
2946 * . .
2947 * : :
2948 * | |
2949 * +----------------------+
2951 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2952 * NT_PRSINFO -> struct elf_prpsinfo
2953 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2955 * Format follows System V format as close as possible. Current
2956 * version limitations are as follows:
2957 * - no floating point registers are dumped
2959 * Function returns 0 in case of success, negative errno otherwise.
2961 * TODO: make this work also during runtime: it should be
2962 * possible to force coredump from running process and then
2963 * continue processing. For example qemu could set up SIGUSR2
2964 * handler (provided that target process haven't registered
2965 * handler for that) that does the dump when signal is received.
2967 static int elf_core_dump(int signr, const CPUArchState *env)
2969 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2970 const TaskState *ts = (const TaskState *)cpu->opaque;
2971 struct vm_area_struct *vma = NULL;
2972 char corefile[PATH_MAX];
2973 struct elf_note_info info;
2974 struct elfhdr elf;
2975 struct elf_phdr phdr;
2976 struct rlimit dumpsize;
2977 struct mm_struct *mm = NULL;
2978 off_t offset = 0, data_offset = 0;
2979 int segs = 0;
2980 int fd = -1;
2982 init_note_info(&info);
2984 errno = 0;
2985 getrlimit(RLIMIT_CORE, &dumpsize);
2986 if (dumpsize.rlim_cur == 0)
2987 return 0;
2989 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2990 return (-errno);
2992 if ((fd = open(corefile, O_WRONLY | O_CREAT,
2993 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2994 return (-errno);
2997 * Walk through target process memory mappings and
2998 * set up structure containing this information. After
2999 * this point vma_xxx functions can be used.
3001 if ((mm = vma_init()) == NULL)
3002 goto out;
3004 walk_memory_regions(mm, vma_walker);
3005 segs = vma_get_mapping_count(mm);
3008 * Construct valid coredump ELF header. We also
3009 * add one more segment for notes.
3011 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3012 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3013 goto out;
3015 /* fill in the in-memory version of notes */
3016 if (fill_note_info(&info, signr, env) < 0)
3017 goto out;
3019 offset += sizeof (elf); /* elf header */
3020 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3022 /* write out notes program header */
3023 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3025 offset += info.notes_size;
3026 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3027 goto out;
3030 * ELF specification wants data to start at page boundary so
3031 * we align it here.
3033 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3036 * Write program headers for memory regions mapped in
3037 * the target process.
3039 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3040 (void) memset(&phdr, 0, sizeof (phdr));
3042 phdr.p_type = PT_LOAD;
3043 phdr.p_offset = offset;
3044 phdr.p_vaddr = vma->vma_start;
3045 phdr.p_paddr = 0;
3046 phdr.p_filesz = vma_dump_size(vma);
3047 offset += phdr.p_filesz;
3048 phdr.p_memsz = vma->vma_end - vma->vma_start;
3049 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3050 if (vma->vma_flags & PROT_WRITE)
3051 phdr.p_flags |= PF_W;
3052 if (vma->vma_flags & PROT_EXEC)
3053 phdr.p_flags |= PF_X;
3054 phdr.p_align = ELF_EXEC_PAGESIZE;
3056 bswap_phdr(&phdr, 1);
3057 dump_write(fd, &phdr, sizeof (phdr));
3061 * Next we write notes just after program headers. No
3062 * alignment needed here.
3064 if (write_note_info(&info, fd) < 0)
3065 goto out;
3067 /* align data to page boundary */
3068 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3069 goto out;
3072 * Finally we can dump process memory into corefile as well.
3074 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3075 abi_ulong addr;
3076 abi_ulong end;
3078 end = vma->vma_start + vma_dump_size(vma);
3080 for (addr = vma->vma_start; addr < end;
3081 addr += TARGET_PAGE_SIZE) {
3082 char page[TARGET_PAGE_SIZE];
3083 int error;
3086 * Read in page from target process memory and
3087 * write it to coredump file.
3089 error = copy_from_user(page, addr, sizeof (page));
3090 if (error != 0) {
3091 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3092 addr);
3093 errno = -error;
3094 goto out;
3096 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3097 goto out;
3101 out:
3102 free_note_info(&info);
3103 if (mm != NULL)
3104 vma_delete(mm);
3105 (void) close(fd);
3107 if (errno != 0)
3108 return (-errno);
3109 return (0);
3111 #endif /* USE_ELF_CORE_DUMP */
3113 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3115 init_thread(regs, infop);