linux-user: init_guest_space: Clean up if we can't initialize the commpage
[qemu.git] / linux-user / elfload.c
blobdcdd75690815596b62883afb81c013a3a6a0baab
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/resource.h>
7 #include "qemu.h"
8 #include "disas/disas.h"
9 #include "qemu/path.h"
11 #ifdef _ARCH_PPC64
12 #undef ARCH_DLINFO
13 #undef ELF_PLATFORM
14 #undef ELF_HWCAP
15 #undef ELF_HWCAP2
16 #undef ELF_CLASS
17 #undef ELF_DATA
18 #undef ELF_ARCH
19 #endif
21 #define ELF_OSABI ELFOSABI_SYSV
23 /* from personality.h */
26 * Flags for bug emulation.
28 * These occupy the top three bytes.
30 enum {
31 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
32 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
33 descriptors (signal handling) */
34 MMAP_PAGE_ZERO = 0x0100000,
35 ADDR_COMPAT_LAYOUT = 0x0200000,
36 READ_IMPLIES_EXEC = 0x0400000,
37 ADDR_LIMIT_32BIT = 0x0800000,
38 SHORT_INODE = 0x1000000,
39 WHOLE_SECONDS = 0x2000000,
40 STICKY_TIMEOUTS = 0x4000000,
41 ADDR_LIMIT_3GB = 0x8000000,
45 * Personality types.
47 * These go in the low byte. Avoid using the top bit, it will
48 * conflict with error returns.
50 enum {
51 PER_LINUX = 0x0000,
52 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
53 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
54 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
55 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
56 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
57 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
58 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
59 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
60 PER_BSD = 0x0006,
61 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
62 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
63 PER_LINUX32 = 0x0008,
64 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
65 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
66 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
67 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
68 PER_RISCOS = 0x000c,
69 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
70 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
71 PER_OSF4 = 0x000f, /* OSF/1 v4 */
72 PER_HPUX = 0x0010,
73 PER_MASK = 0x00ff,
77 * Return the base personality without flags.
79 #define personality(pers) (pers & PER_MASK)
81 /* this flag is uneffective under linux too, should be deleted */
82 #ifndef MAP_DENYWRITE
83 #define MAP_DENYWRITE 0
84 #endif
86 /* should probably go in elf.h */
87 #ifndef ELIBBAD
88 #define ELIBBAD 80
89 #endif
91 #ifdef TARGET_WORDS_BIGENDIAN
92 #define ELF_DATA ELFDATA2MSB
93 #else
94 #define ELF_DATA ELFDATA2LSB
95 #endif
97 #ifdef TARGET_ABI_MIPSN32
98 typedef abi_ullong target_elf_greg_t;
99 #define tswapreg(ptr) tswap64(ptr)
100 #else
101 typedef abi_ulong target_elf_greg_t;
102 #define tswapreg(ptr) tswapal(ptr)
103 #endif
105 #ifdef USE_UID16
106 typedef abi_ushort target_uid_t;
107 typedef abi_ushort target_gid_t;
108 #else
109 typedef abi_uint target_uid_t;
110 typedef abi_uint target_gid_t;
111 #endif
112 typedef abi_int target_pid_t;
114 #ifdef TARGET_I386
116 #define ELF_PLATFORM get_elf_platform()
118 static const char *get_elf_platform(void)
120 static char elf_platform[] = "i386";
121 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
122 if (family > 6)
123 family = 6;
124 if (family >= 3)
125 elf_platform[1] = '0' + family;
126 return elf_platform;
129 #define ELF_HWCAP get_elf_hwcap()
131 static uint32_t get_elf_hwcap(void)
133 X86CPU *cpu = X86_CPU(thread_cpu);
135 return cpu->env.features[FEAT_1_EDX];
138 #ifdef TARGET_X86_64
139 #define ELF_START_MMAP 0x2aaaaab000ULL
141 #define ELF_CLASS ELFCLASS64
142 #define ELF_ARCH EM_X86_64
144 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
146 regs->rax = 0;
147 regs->rsp = infop->start_stack;
148 regs->rip = infop->entry;
151 #define ELF_NREG 27
152 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
155 * Note that ELF_NREG should be 29 as there should be place for
156 * TRAPNO and ERR "registers" as well but linux doesn't dump
157 * those.
159 * See linux kernel: arch/x86/include/asm/elf.h
161 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
163 (*regs)[0] = env->regs[15];
164 (*regs)[1] = env->regs[14];
165 (*regs)[2] = env->regs[13];
166 (*regs)[3] = env->regs[12];
167 (*regs)[4] = env->regs[R_EBP];
168 (*regs)[5] = env->regs[R_EBX];
169 (*regs)[6] = env->regs[11];
170 (*regs)[7] = env->regs[10];
171 (*regs)[8] = env->regs[9];
172 (*regs)[9] = env->regs[8];
173 (*regs)[10] = env->regs[R_EAX];
174 (*regs)[11] = env->regs[R_ECX];
175 (*regs)[12] = env->regs[R_EDX];
176 (*regs)[13] = env->regs[R_ESI];
177 (*regs)[14] = env->regs[R_EDI];
178 (*regs)[15] = env->regs[R_EAX]; /* XXX */
179 (*regs)[16] = env->eip;
180 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
181 (*regs)[18] = env->eflags;
182 (*regs)[19] = env->regs[R_ESP];
183 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
184 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
185 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
186 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
187 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
188 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
189 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
192 #else
194 #define ELF_START_MMAP 0x80000000
197 * This is used to ensure we don't load something for the wrong architecture.
199 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
202 * These are used to set parameters in the core dumps.
204 #define ELF_CLASS ELFCLASS32
205 #define ELF_ARCH EM_386
207 static inline void init_thread(struct target_pt_regs *regs,
208 struct image_info *infop)
210 regs->esp = infop->start_stack;
211 regs->eip = infop->entry;
213 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
214 starts %edx contains a pointer to a function which might be
215 registered using `atexit'. This provides a mean for the
216 dynamic linker to call DT_FINI functions for shared libraries
217 that have been loaded before the code runs.
219 A value of 0 tells we have no such handler. */
220 regs->edx = 0;
223 #define ELF_NREG 17
224 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
227 * Note that ELF_NREG should be 19 as there should be place for
228 * TRAPNO and ERR "registers" as well but linux doesn't dump
229 * those.
231 * See linux kernel: arch/x86/include/asm/elf.h
233 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
235 (*regs)[0] = env->regs[R_EBX];
236 (*regs)[1] = env->regs[R_ECX];
237 (*regs)[2] = env->regs[R_EDX];
238 (*regs)[3] = env->regs[R_ESI];
239 (*regs)[4] = env->regs[R_EDI];
240 (*regs)[5] = env->regs[R_EBP];
241 (*regs)[6] = env->regs[R_EAX];
242 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
243 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
244 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
245 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
246 (*regs)[11] = env->regs[R_EAX]; /* XXX */
247 (*regs)[12] = env->eip;
248 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
249 (*regs)[14] = env->eflags;
250 (*regs)[15] = env->regs[R_ESP];
251 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
253 #endif
255 #define USE_ELF_CORE_DUMP
256 #define ELF_EXEC_PAGESIZE 4096
258 #endif
260 #ifdef TARGET_ARM
262 #ifndef TARGET_AARCH64
263 /* 32 bit ARM definitions */
265 #define ELF_START_MMAP 0x80000000
267 #define ELF_ARCH EM_ARM
268 #define ELF_CLASS ELFCLASS32
270 static inline void init_thread(struct target_pt_regs *regs,
271 struct image_info *infop)
273 abi_long stack = infop->start_stack;
274 memset(regs, 0, sizeof(*regs));
276 regs->uregs[16] = ARM_CPU_MODE_USR;
277 if (infop->entry & 1) {
278 regs->uregs[16] |= CPSR_T;
280 regs->uregs[15] = infop->entry & 0xfffffffe;
281 regs->uregs[13] = infop->start_stack;
282 /* FIXME - what to for failure of get_user()? */
283 get_user_ual(regs->uregs[2], stack + 8); /* envp */
284 get_user_ual(regs->uregs[1], stack + 4); /* envp */
285 /* XXX: it seems that r0 is zeroed after ! */
286 regs->uregs[0] = 0;
287 /* For uClinux PIC binaries. */
288 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
289 regs->uregs[10] = infop->start_data;
292 #define ELF_NREG 18
293 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
295 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
297 (*regs)[0] = tswapreg(env->regs[0]);
298 (*regs)[1] = tswapreg(env->regs[1]);
299 (*regs)[2] = tswapreg(env->regs[2]);
300 (*regs)[3] = tswapreg(env->regs[3]);
301 (*regs)[4] = tswapreg(env->regs[4]);
302 (*regs)[5] = tswapreg(env->regs[5]);
303 (*regs)[6] = tswapreg(env->regs[6]);
304 (*regs)[7] = tswapreg(env->regs[7]);
305 (*regs)[8] = tswapreg(env->regs[8]);
306 (*regs)[9] = tswapreg(env->regs[9]);
307 (*regs)[10] = tswapreg(env->regs[10]);
308 (*regs)[11] = tswapreg(env->regs[11]);
309 (*regs)[12] = tswapreg(env->regs[12]);
310 (*regs)[13] = tswapreg(env->regs[13]);
311 (*regs)[14] = tswapreg(env->regs[14]);
312 (*regs)[15] = tswapreg(env->regs[15]);
314 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
315 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
318 #define USE_ELF_CORE_DUMP
319 #define ELF_EXEC_PAGESIZE 4096
321 enum
323 ARM_HWCAP_ARM_SWP = 1 << 0,
324 ARM_HWCAP_ARM_HALF = 1 << 1,
325 ARM_HWCAP_ARM_THUMB = 1 << 2,
326 ARM_HWCAP_ARM_26BIT = 1 << 3,
327 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
328 ARM_HWCAP_ARM_FPA = 1 << 5,
329 ARM_HWCAP_ARM_VFP = 1 << 6,
330 ARM_HWCAP_ARM_EDSP = 1 << 7,
331 ARM_HWCAP_ARM_JAVA = 1 << 8,
332 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
333 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
334 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
335 ARM_HWCAP_ARM_NEON = 1 << 12,
336 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
337 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
338 ARM_HWCAP_ARM_TLS = 1 << 15,
339 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
340 ARM_HWCAP_ARM_IDIVA = 1 << 17,
341 ARM_HWCAP_ARM_IDIVT = 1 << 18,
342 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
343 ARM_HWCAP_ARM_LPAE = 1 << 20,
344 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
347 enum {
348 ARM_HWCAP2_ARM_AES = 1 << 0,
349 ARM_HWCAP2_ARM_PMULL = 1 << 1,
350 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
351 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
352 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
355 /* The commpage only exists for 32 bit kernels */
357 /* Return 1 if the proposed guest space is suitable for the guest.
358 * Return 0 if the proposed guest space isn't suitable, but another
359 * address space should be tried.
360 * Return -1 if there is no way the proposed guest space can be
361 * valid regardless of the base.
362 * The guest code may leave a page mapped and populate it if the
363 * address is suitable.
365 static int init_guest_commpage(unsigned long guest_base,
366 unsigned long guest_size)
368 unsigned long real_start, test_page_addr;
370 /* We need to check that we can force a fault on access to the
371 * commpage at 0xffff0fxx
373 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
375 /* If the commpage lies within the already allocated guest space,
376 * then there is no way we can allocate it.
378 if (test_page_addr >= guest_base
379 && test_page_addr < (guest_base + guest_size)) {
380 return -1;
383 /* Note it needs to be writeable to let us initialise it */
384 real_start = (unsigned long)
385 mmap((void *)test_page_addr, qemu_host_page_size,
386 PROT_READ | PROT_WRITE,
387 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
389 /* If we can't map it then try another address */
390 if (real_start == -1ul) {
391 return 0;
394 if (real_start != test_page_addr) {
395 /* OS didn't put the page where we asked - unmap and reject */
396 munmap((void *)real_start, qemu_host_page_size);
397 return 0;
400 /* Leave the page mapped
401 * Populate it (mmap should have left it all 0'd)
404 /* Kernel helper versions */
405 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
407 /* Now it's populated make it RO */
408 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
409 perror("Protecting guest commpage");
410 exit(-1);
413 return 1; /* All good */
416 #define ELF_HWCAP get_elf_hwcap()
417 #define ELF_HWCAP2 get_elf_hwcap2()
419 static uint32_t get_elf_hwcap(void)
421 ARMCPU *cpu = ARM_CPU(thread_cpu);
422 uint32_t hwcaps = 0;
424 hwcaps |= ARM_HWCAP_ARM_SWP;
425 hwcaps |= ARM_HWCAP_ARM_HALF;
426 hwcaps |= ARM_HWCAP_ARM_THUMB;
427 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
429 /* probe for the extra features */
430 #define GET_FEATURE(feat, hwcap) \
431 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
432 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
433 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
434 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
435 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
436 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
437 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
438 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
439 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
440 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
441 GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA);
442 GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT);
443 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
444 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
445 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
446 * to our VFP_FP16 feature bit.
448 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
449 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
451 return hwcaps;
454 static uint32_t get_elf_hwcap2(void)
456 ARMCPU *cpu = ARM_CPU(thread_cpu);
457 uint32_t hwcaps = 0;
459 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP2_ARM_AES);
460 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP2_ARM_PMULL);
461 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP2_ARM_SHA1);
462 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP2_ARM_SHA2);
463 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP2_ARM_CRC32);
464 return hwcaps;
467 #undef GET_FEATURE
469 #else
470 /* 64 bit ARM definitions */
471 #define ELF_START_MMAP 0x80000000
473 #define ELF_ARCH EM_AARCH64
474 #define ELF_CLASS ELFCLASS64
475 #define ELF_PLATFORM "aarch64"
477 static inline void init_thread(struct target_pt_regs *regs,
478 struct image_info *infop)
480 abi_long stack = infop->start_stack;
481 memset(regs, 0, sizeof(*regs));
483 regs->pc = infop->entry & ~0x3ULL;
484 regs->sp = stack;
487 #define ELF_NREG 34
488 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
490 static void elf_core_copy_regs(target_elf_gregset_t *regs,
491 const CPUARMState *env)
493 int i;
495 for (i = 0; i < 32; i++) {
496 (*regs)[i] = tswapreg(env->xregs[i]);
498 (*regs)[32] = tswapreg(env->pc);
499 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
502 #define USE_ELF_CORE_DUMP
503 #define ELF_EXEC_PAGESIZE 4096
505 enum {
506 ARM_HWCAP_A64_FP = 1 << 0,
507 ARM_HWCAP_A64_ASIMD = 1 << 1,
508 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
509 ARM_HWCAP_A64_AES = 1 << 3,
510 ARM_HWCAP_A64_PMULL = 1 << 4,
511 ARM_HWCAP_A64_SHA1 = 1 << 5,
512 ARM_HWCAP_A64_SHA2 = 1 << 6,
513 ARM_HWCAP_A64_CRC32 = 1 << 7,
514 ARM_HWCAP_A64_ATOMICS = 1 << 8,
515 ARM_HWCAP_A64_FPHP = 1 << 9,
516 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
517 ARM_HWCAP_A64_CPUID = 1 << 11,
518 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
519 ARM_HWCAP_A64_JSCVT = 1 << 13,
520 ARM_HWCAP_A64_FCMA = 1 << 14,
521 ARM_HWCAP_A64_LRCPC = 1 << 15,
522 ARM_HWCAP_A64_DCPOP = 1 << 16,
523 ARM_HWCAP_A64_SHA3 = 1 << 17,
524 ARM_HWCAP_A64_SM3 = 1 << 18,
525 ARM_HWCAP_A64_SM4 = 1 << 19,
526 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
527 ARM_HWCAP_A64_SHA512 = 1 << 21,
528 ARM_HWCAP_A64_SVE = 1 << 22,
531 #define ELF_HWCAP get_elf_hwcap()
533 static uint32_t get_elf_hwcap(void)
535 ARMCPU *cpu = ARM_CPU(thread_cpu);
536 uint32_t hwcaps = 0;
538 hwcaps |= ARM_HWCAP_A64_FP;
539 hwcaps |= ARM_HWCAP_A64_ASIMD;
541 /* probe for the extra features */
542 #define GET_FEATURE(feat, hwcap) \
543 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
544 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP_A64_AES);
545 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP_A64_PMULL);
546 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP_A64_SHA1);
547 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP_A64_SHA2);
548 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP_A64_CRC32);
549 GET_FEATURE(ARM_FEATURE_V8_SHA3, ARM_HWCAP_A64_SHA3);
550 GET_FEATURE(ARM_FEATURE_V8_SM3, ARM_HWCAP_A64_SM3);
551 GET_FEATURE(ARM_FEATURE_V8_SM4, ARM_HWCAP_A64_SM4);
552 GET_FEATURE(ARM_FEATURE_V8_SHA512, ARM_HWCAP_A64_SHA512);
553 GET_FEATURE(ARM_FEATURE_V8_FP16,
554 ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
555 GET_FEATURE(ARM_FEATURE_V8_RDM, ARM_HWCAP_A64_ASIMDRDM);
556 GET_FEATURE(ARM_FEATURE_V8_FCMA, ARM_HWCAP_A64_FCMA);
557 #undef GET_FEATURE
559 return hwcaps;
562 #endif /* not TARGET_AARCH64 */
563 #endif /* TARGET_ARM */
565 #ifdef TARGET_SPARC
566 #ifdef TARGET_SPARC64
568 #define ELF_START_MMAP 0x80000000
569 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
570 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
571 #ifndef TARGET_ABI32
572 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
573 #else
574 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
575 #endif
577 #define ELF_CLASS ELFCLASS64
578 #define ELF_ARCH EM_SPARCV9
580 #define STACK_BIAS 2047
582 static inline void init_thread(struct target_pt_regs *regs,
583 struct image_info *infop)
585 #ifndef TARGET_ABI32
586 regs->tstate = 0;
587 #endif
588 regs->pc = infop->entry;
589 regs->npc = regs->pc + 4;
590 regs->y = 0;
591 #ifdef TARGET_ABI32
592 regs->u_regs[14] = infop->start_stack - 16 * 4;
593 #else
594 if (personality(infop->personality) == PER_LINUX32)
595 regs->u_regs[14] = infop->start_stack - 16 * 4;
596 else
597 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
598 #endif
601 #else
602 #define ELF_START_MMAP 0x80000000
603 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
604 | HWCAP_SPARC_MULDIV)
606 #define ELF_CLASS ELFCLASS32
607 #define ELF_ARCH EM_SPARC
609 static inline void init_thread(struct target_pt_regs *regs,
610 struct image_info *infop)
612 regs->psr = 0;
613 regs->pc = infop->entry;
614 regs->npc = regs->pc + 4;
615 regs->y = 0;
616 regs->u_regs[14] = infop->start_stack - 16 * 4;
619 #endif
620 #endif
622 #ifdef TARGET_PPC
624 #define ELF_MACHINE PPC_ELF_MACHINE
625 #define ELF_START_MMAP 0x80000000
627 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
629 #define elf_check_arch(x) ( (x) == EM_PPC64 )
631 #define ELF_CLASS ELFCLASS64
633 #else
635 #define ELF_CLASS ELFCLASS32
637 #endif
639 #define ELF_ARCH EM_PPC
641 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
642 See arch/powerpc/include/asm/cputable.h. */
643 enum {
644 QEMU_PPC_FEATURE_32 = 0x80000000,
645 QEMU_PPC_FEATURE_64 = 0x40000000,
646 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
647 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
648 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
649 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
650 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
651 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
652 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
653 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
654 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
655 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
656 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
657 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
658 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
659 QEMU_PPC_FEATURE_CELL = 0x00010000,
660 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
661 QEMU_PPC_FEATURE_SMT = 0x00004000,
662 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
663 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
664 QEMU_PPC_FEATURE_PA6T = 0x00000800,
665 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
666 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
667 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
668 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
669 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
671 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
672 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
674 /* Feature definitions in AT_HWCAP2. */
675 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
676 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
677 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
678 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
679 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
680 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
683 #define ELF_HWCAP get_elf_hwcap()
685 static uint32_t get_elf_hwcap(void)
687 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
688 uint32_t features = 0;
690 /* We don't have to be terribly complete here; the high points are
691 Altivec/FP/SPE support. Anything else is just a bonus. */
692 #define GET_FEATURE(flag, feature) \
693 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
694 #define GET_FEATURE2(flags, feature) \
695 do { \
696 if ((cpu->env.insns_flags2 & flags) == flags) { \
697 features |= feature; \
699 } while (0)
700 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
701 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
702 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
703 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
704 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
705 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
706 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
707 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
708 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
709 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
710 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
711 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
712 QEMU_PPC_FEATURE_ARCH_2_06);
713 #undef GET_FEATURE
714 #undef GET_FEATURE2
716 return features;
719 #define ELF_HWCAP2 get_elf_hwcap2()
721 static uint32_t get_elf_hwcap2(void)
723 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
724 uint32_t features = 0;
726 #define GET_FEATURE(flag, feature) \
727 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
728 #define GET_FEATURE2(flag, feature) \
729 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
731 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
732 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
733 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
734 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07);
736 #undef GET_FEATURE
737 #undef GET_FEATURE2
739 return features;
743 * The requirements here are:
744 * - keep the final alignment of sp (sp & 0xf)
745 * - make sure the 32-bit value at the first 16 byte aligned position of
746 * AUXV is greater than 16 for glibc compatibility.
747 * AT_IGNOREPPC is used for that.
748 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
749 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
751 #define DLINFO_ARCH_ITEMS 5
752 #define ARCH_DLINFO \
753 do { \
754 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
755 /* \
756 * Handle glibc compatibility: these magic entries must \
757 * be at the lowest addresses in the final auxv. \
758 */ \
759 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
760 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
761 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
762 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
763 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
764 } while (0)
766 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
768 _regs->gpr[1] = infop->start_stack;
769 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
770 if (get_ppc64_abi(infop) < 2) {
771 uint64_t val;
772 get_user_u64(val, infop->entry + 8);
773 _regs->gpr[2] = val + infop->load_bias;
774 get_user_u64(val, infop->entry);
775 infop->entry = val + infop->load_bias;
776 } else {
777 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
779 #endif
780 _regs->nip = infop->entry;
783 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
784 #define ELF_NREG 48
785 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
787 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
789 int i;
790 target_ulong ccr = 0;
792 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
793 (*regs)[i] = tswapreg(env->gpr[i]);
796 (*regs)[32] = tswapreg(env->nip);
797 (*regs)[33] = tswapreg(env->msr);
798 (*regs)[35] = tswapreg(env->ctr);
799 (*regs)[36] = tswapreg(env->lr);
800 (*regs)[37] = tswapreg(env->xer);
802 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
803 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
805 (*regs)[38] = tswapreg(ccr);
808 #define USE_ELF_CORE_DUMP
809 #define ELF_EXEC_PAGESIZE 4096
811 #endif
813 #ifdef TARGET_MIPS
815 #define ELF_START_MMAP 0x80000000
817 #ifdef TARGET_MIPS64
818 #define ELF_CLASS ELFCLASS64
819 #else
820 #define ELF_CLASS ELFCLASS32
821 #endif
822 #define ELF_ARCH EM_MIPS
824 static inline void init_thread(struct target_pt_regs *regs,
825 struct image_info *infop)
827 regs->cp0_status = 2 << CP0St_KSU;
828 regs->cp0_epc = infop->entry;
829 regs->regs[29] = infop->start_stack;
832 /* See linux kernel: arch/mips/include/asm/elf.h. */
833 #define ELF_NREG 45
834 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
836 /* See linux kernel: arch/mips/include/asm/reg.h. */
837 enum {
838 #ifdef TARGET_MIPS64
839 TARGET_EF_R0 = 0,
840 #else
841 TARGET_EF_R0 = 6,
842 #endif
843 TARGET_EF_R26 = TARGET_EF_R0 + 26,
844 TARGET_EF_R27 = TARGET_EF_R0 + 27,
845 TARGET_EF_LO = TARGET_EF_R0 + 32,
846 TARGET_EF_HI = TARGET_EF_R0 + 33,
847 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
848 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
849 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
850 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
853 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
854 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
856 int i;
858 for (i = 0; i < TARGET_EF_R0; i++) {
859 (*regs)[i] = 0;
861 (*regs)[TARGET_EF_R0] = 0;
863 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
864 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
867 (*regs)[TARGET_EF_R26] = 0;
868 (*regs)[TARGET_EF_R27] = 0;
869 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
870 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
871 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
872 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
873 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
874 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
877 #define USE_ELF_CORE_DUMP
878 #define ELF_EXEC_PAGESIZE 4096
880 #endif /* TARGET_MIPS */
882 #ifdef TARGET_MICROBLAZE
884 #define ELF_START_MMAP 0x80000000
886 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
888 #define ELF_CLASS ELFCLASS32
889 #define ELF_ARCH EM_MICROBLAZE
891 static inline void init_thread(struct target_pt_regs *regs,
892 struct image_info *infop)
894 regs->pc = infop->entry;
895 regs->r1 = infop->start_stack;
899 #define ELF_EXEC_PAGESIZE 4096
901 #define USE_ELF_CORE_DUMP
902 #define ELF_NREG 38
903 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
905 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
906 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
908 int i, pos = 0;
910 for (i = 0; i < 32; i++) {
911 (*regs)[pos++] = tswapreg(env->regs[i]);
914 for (i = 0; i < 6; i++) {
915 (*regs)[pos++] = tswapreg(env->sregs[i]);
919 #endif /* TARGET_MICROBLAZE */
921 #ifdef TARGET_NIOS2
923 #define ELF_START_MMAP 0x80000000
925 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
927 #define ELF_CLASS ELFCLASS32
928 #define ELF_ARCH EM_ALTERA_NIOS2
930 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
932 regs->ea = infop->entry;
933 regs->sp = infop->start_stack;
934 regs->estatus = 0x3;
937 #define ELF_EXEC_PAGESIZE 4096
939 #define USE_ELF_CORE_DUMP
940 #define ELF_NREG 49
941 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
943 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
944 static void elf_core_copy_regs(target_elf_gregset_t *regs,
945 const CPUNios2State *env)
947 int i;
949 (*regs)[0] = -1;
950 for (i = 1; i < 8; i++) /* r0-r7 */
951 (*regs)[i] = tswapreg(env->regs[i + 7]);
953 for (i = 8; i < 16; i++) /* r8-r15 */
954 (*regs)[i] = tswapreg(env->regs[i - 8]);
956 for (i = 16; i < 24; i++) /* r16-r23 */
957 (*regs)[i] = tswapreg(env->regs[i + 7]);
958 (*regs)[24] = -1; /* R_ET */
959 (*regs)[25] = -1; /* R_BT */
960 (*regs)[26] = tswapreg(env->regs[R_GP]);
961 (*regs)[27] = tswapreg(env->regs[R_SP]);
962 (*regs)[28] = tswapreg(env->regs[R_FP]);
963 (*regs)[29] = tswapreg(env->regs[R_EA]);
964 (*regs)[30] = -1; /* R_SSTATUS */
965 (*regs)[31] = tswapreg(env->regs[R_RA]);
967 (*regs)[32] = tswapreg(env->regs[R_PC]);
969 (*regs)[33] = -1; /* R_STATUS */
970 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
972 for (i = 35; i < 49; i++) /* ... */
973 (*regs)[i] = -1;
976 #endif /* TARGET_NIOS2 */
978 #ifdef TARGET_OPENRISC
980 #define ELF_START_MMAP 0x08000000
982 #define ELF_ARCH EM_OPENRISC
983 #define ELF_CLASS ELFCLASS32
984 #define ELF_DATA ELFDATA2MSB
986 static inline void init_thread(struct target_pt_regs *regs,
987 struct image_info *infop)
989 regs->pc = infop->entry;
990 regs->gpr[1] = infop->start_stack;
993 #define USE_ELF_CORE_DUMP
994 #define ELF_EXEC_PAGESIZE 8192
996 /* See linux kernel arch/openrisc/include/asm/elf.h. */
997 #define ELF_NREG 34 /* gprs and pc, sr */
998 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1000 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1001 const CPUOpenRISCState *env)
1003 int i;
1005 for (i = 0; i < 32; i++) {
1006 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1008 (*regs)[32] = tswapreg(env->pc);
1009 (*regs)[33] = tswapreg(cpu_get_sr(env));
1011 #define ELF_HWCAP 0
1012 #define ELF_PLATFORM NULL
1014 #endif /* TARGET_OPENRISC */
1016 #ifdef TARGET_SH4
1018 #define ELF_START_MMAP 0x80000000
1020 #define ELF_CLASS ELFCLASS32
1021 #define ELF_ARCH EM_SH
1023 static inline void init_thread(struct target_pt_regs *regs,
1024 struct image_info *infop)
1026 /* Check other registers XXXXX */
1027 regs->pc = infop->entry;
1028 regs->regs[15] = infop->start_stack;
1031 /* See linux kernel: arch/sh/include/asm/elf.h. */
1032 #define ELF_NREG 23
1033 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1035 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1036 enum {
1037 TARGET_REG_PC = 16,
1038 TARGET_REG_PR = 17,
1039 TARGET_REG_SR = 18,
1040 TARGET_REG_GBR = 19,
1041 TARGET_REG_MACH = 20,
1042 TARGET_REG_MACL = 21,
1043 TARGET_REG_SYSCALL = 22
1046 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1047 const CPUSH4State *env)
1049 int i;
1051 for (i = 0; i < 16; i++) {
1052 (*regs)[i] = tswapreg(env->gregs[i]);
1055 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1056 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1057 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1058 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1059 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1060 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1061 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1064 #define USE_ELF_CORE_DUMP
1065 #define ELF_EXEC_PAGESIZE 4096
1067 enum {
1068 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1069 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1070 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1071 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1072 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1073 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1074 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1075 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1076 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1077 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1080 #define ELF_HWCAP get_elf_hwcap()
1082 static uint32_t get_elf_hwcap(void)
1084 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1085 uint32_t hwcap = 0;
1087 hwcap |= SH_CPU_HAS_FPU;
1089 if (cpu->env.features & SH_FEATURE_SH4A) {
1090 hwcap |= SH_CPU_HAS_LLSC;
1093 return hwcap;
1096 #endif
1098 #ifdef TARGET_CRIS
1100 #define ELF_START_MMAP 0x80000000
1102 #define ELF_CLASS ELFCLASS32
1103 #define ELF_ARCH EM_CRIS
1105 static inline void init_thread(struct target_pt_regs *regs,
1106 struct image_info *infop)
1108 regs->erp = infop->entry;
1111 #define ELF_EXEC_PAGESIZE 8192
1113 #endif
1115 #ifdef TARGET_M68K
1117 #define ELF_START_MMAP 0x80000000
1119 #define ELF_CLASS ELFCLASS32
1120 #define ELF_ARCH EM_68K
1122 /* ??? Does this need to do anything?
1123 #define ELF_PLAT_INIT(_r) */
1125 static inline void init_thread(struct target_pt_regs *regs,
1126 struct image_info *infop)
1128 regs->usp = infop->start_stack;
1129 regs->sr = 0;
1130 regs->pc = infop->entry;
1133 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1134 #define ELF_NREG 20
1135 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1137 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1139 (*regs)[0] = tswapreg(env->dregs[1]);
1140 (*regs)[1] = tswapreg(env->dregs[2]);
1141 (*regs)[2] = tswapreg(env->dregs[3]);
1142 (*regs)[3] = tswapreg(env->dregs[4]);
1143 (*regs)[4] = tswapreg(env->dregs[5]);
1144 (*regs)[5] = tswapreg(env->dregs[6]);
1145 (*regs)[6] = tswapreg(env->dregs[7]);
1146 (*regs)[7] = tswapreg(env->aregs[0]);
1147 (*regs)[8] = tswapreg(env->aregs[1]);
1148 (*regs)[9] = tswapreg(env->aregs[2]);
1149 (*regs)[10] = tswapreg(env->aregs[3]);
1150 (*regs)[11] = tswapreg(env->aregs[4]);
1151 (*regs)[12] = tswapreg(env->aregs[5]);
1152 (*regs)[13] = tswapreg(env->aregs[6]);
1153 (*regs)[14] = tswapreg(env->dregs[0]);
1154 (*regs)[15] = tswapreg(env->aregs[7]);
1155 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1156 (*regs)[17] = tswapreg(env->sr);
1157 (*regs)[18] = tswapreg(env->pc);
1158 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1161 #define USE_ELF_CORE_DUMP
1162 #define ELF_EXEC_PAGESIZE 8192
1164 #endif
1166 #ifdef TARGET_ALPHA
1168 #define ELF_START_MMAP (0x30000000000ULL)
1170 #define ELF_CLASS ELFCLASS64
1171 #define ELF_ARCH EM_ALPHA
1173 static inline void init_thread(struct target_pt_regs *regs,
1174 struct image_info *infop)
1176 regs->pc = infop->entry;
1177 regs->ps = 8;
1178 regs->usp = infop->start_stack;
1181 #define ELF_EXEC_PAGESIZE 8192
1183 #endif /* TARGET_ALPHA */
1185 #ifdef TARGET_S390X
1187 #define ELF_START_MMAP (0x20000000000ULL)
1189 #define ELF_CLASS ELFCLASS64
1190 #define ELF_DATA ELFDATA2MSB
1191 #define ELF_ARCH EM_S390
1193 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1195 regs->psw.addr = infop->entry;
1196 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1197 regs->gprs[15] = infop->start_stack;
1200 #endif /* TARGET_S390X */
1202 #ifdef TARGET_TILEGX
1204 /* 42 bits real used address, a half for user mode */
1205 #define ELF_START_MMAP (0x00000020000000000ULL)
1207 #define elf_check_arch(x) ((x) == EM_TILEGX)
1209 #define ELF_CLASS ELFCLASS64
1210 #define ELF_DATA ELFDATA2LSB
1211 #define ELF_ARCH EM_TILEGX
1213 static inline void init_thread(struct target_pt_regs *regs,
1214 struct image_info *infop)
1216 regs->pc = infop->entry;
1217 regs->sp = infop->start_stack;
1221 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1223 #endif /* TARGET_TILEGX */
1225 #ifdef TARGET_RISCV
1227 #define ELF_START_MMAP 0x80000000
1228 #define ELF_ARCH EM_RISCV
1230 #ifdef TARGET_RISCV32
1231 #define ELF_CLASS ELFCLASS32
1232 #else
1233 #define ELF_CLASS ELFCLASS64
1234 #endif
1236 static inline void init_thread(struct target_pt_regs *regs,
1237 struct image_info *infop)
1239 regs->sepc = infop->entry;
1240 regs->sp = infop->start_stack;
1243 #define ELF_EXEC_PAGESIZE 4096
1245 #endif /* TARGET_RISCV */
1247 #ifdef TARGET_HPPA
1249 #define ELF_START_MMAP 0x80000000
1250 #define ELF_CLASS ELFCLASS32
1251 #define ELF_ARCH EM_PARISC
1252 #define ELF_PLATFORM "PARISC"
1253 #define STACK_GROWS_DOWN 0
1254 #define STACK_ALIGNMENT 64
1256 static inline void init_thread(struct target_pt_regs *regs,
1257 struct image_info *infop)
1259 regs->iaoq[0] = infop->entry;
1260 regs->iaoq[1] = infop->entry + 4;
1261 regs->gr[23] = 0;
1262 regs->gr[24] = infop->arg_start;
1263 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1264 /* The top-of-stack contains a linkage buffer. */
1265 regs->gr[30] = infop->start_stack + 64;
1266 regs->gr[31] = infop->entry;
1269 #endif /* TARGET_HPPA */
1271 #ifndef ELF_PLATFORM
1272 #define ELF_PLATFORM (NULL)
1273 #endif
1275 #ifndef ELF_MACHINE
1276 #define ELF_MACHINE ELF_ARCH
1277 #endif
1279 #ifndef elf_check_arch
1280 #define elf_check_arch(x) ((x) == ELF_ARCH)
1281 #endif
1283 #ifndef ELF_HWCAP
1284 #define ELF_HWCAP 0
1285 #endif
1287 #ifndef STACK_GROWS_DOWN
1288 #define STACK_GROWS_DOWN 1
1289 #endif
1291 #ifndef STACK_ALIGNMENT
1292 #define STACK_ALIGNMENT 16
1293 #endif
1295 #ifdef TARGET_ABI32
1296 #undef ELF_CLASS
1297 #define ELF_CLASS ELFCLASS32
1298 #undef bswaptls
1299 #define bswaptls(ptr) bswap32s(ptr)
1300 #endif
1302 #include "elf.h"
1304 struct exec
1306 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1307 unsigned int a_text; /* length of text, in bytes */
1308 unsigned int a_data; /* length of data, in bytes */
1309 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1310 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1311 unsigned int a_entry; /* start address */
1312 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1313 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1317 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1318 #define OMAGIC 0407
1319 #define NMAGIC 0410
1320 #define ZMAGIC 0413
1321 #define QMAGIC 0314
1323 /* Necessary parameters */
1324 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1325 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1326 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1327 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1329 #define DLINFO_ITEMS 15
1331 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1333 memcpy(to, from, n);
1336 #ifdef BSWAP_NEEDED
1337 static void bswap_ehdr(struct elfhdr *ehdr)
1339 bswap16s(&ehdr->e_type); /* Object file type */
1340 bswap16s(&ehdr->e_machine); /* Architecture */
1341 bswap32s(&ehdr->e_version); /* Object file version */
1342 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1343 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1344 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1345 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1346 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1347 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1348 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1349 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1350 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1351 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1354 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1356 int i;
1357 for (i = 0; i < phnum; ++i, ++phdr) {
1358 bswap32s(&phdr->p_type); /* Segment type */
1359 bswap32s(&phdr->p_flags); /* Segment flags */
1360 bswaptls(&phdr->p_offset); /* Segment file offset */
1361 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1362 bswaptls(&phdr->p_paddr); /* Segment physical address */
1363 bswaptls(&phdr->p_filesz); /* Segment size in file */
1364 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1365 bswaptls(&phdr->p_align); /* Segment alignment */
1369 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1371 int i;
1372 for (i = 0; i < shnum; ++i, ++shdr) {
1373 bswap32s(&shdr->sh_name);
1374 bswap32s(&shdr->sh_type);
1375 bswaptls(&shdr->sh_flags);
1376 bswaptls(&shdr->sh_addr);
1377 bswaptls(&shdr->sh_offset);
1378 bswaptls(&shdr->sh_size);
1379 bswap32s(&shdr->sh_link);
1380 bswap32s(&shdr->sh_info);
1381 bswaptls(&shdr->sh_addralign);
1382 bswaptls(&shdr->sh_entsize);
1386 static void bswap_sym(struct elf_sym *sym)
1388 bswap32s(&sym->st_name);
1389 bswaptls(&sym->st_value);
1390 bswaptls(&sym->st_size);
1391 bswap16s(&sym->st_shndx);
1393 #else
1394 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1395 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1396 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1397 static inline void bswap_sym(struct elf_sym *sym) { }
1398 #endif
1400 #ifdef USE_ELF_CORE_DUMP
1401 static int elf_core_dump(int, const CPUArchState *);
1402 #endif /* USE_ELF_CORE_DUMP */
1403 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1405 /* Verify the portions of EHDR within E_IDENT for the target.
1406 This can be performed before bswapping the entire header. */
1407 static bool elf_check_ident(struct elfhdr *ehdr)
1409 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1410 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1411 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1412 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1413 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1414 && ehdr->e_ident[EI_DATA] == ELF_DATA
1415 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1418 /* Verify the portions of EHDR outside of E_IDENT for the target.
1419 This has to wait until after bswapping the header. */
1420 static bool elf_check_ehdr(struct elfhdr *ehdr)
1422 return (elf_check_arch(ehdr->e_machine)
1423 && ehdr->e_ehsize == sizeof(struct elfhdr)
1424 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1425 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1429 * 'copy_elf_strings()' copies argument/envelope strings from user
1430 * memory to free pages in kernel mem. These are in a format ready
1431 * to be put directly into the top of new user memory.
1434 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1435 abi_ulong p, abi_ulong stack_limit)
1437 char *tmp;
1438 int len, i;
1439 abi_ulong top = p;
1441 if (!p) {
1442 return 0; /* bullet-proofing */
1445 if (STACK_GROWS_DOWN) {
1446 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1447 for (i = argc - 1; i >= 0; --i) {
1448 tmp = argv[i];
1449 if (!tmp) {
1450 fprintf(stderr, "VFS: argc is wrong");
1451 exit(-1);
1453 len = strlen(tmp) + 1;
1454 tmp += len;
1456 if (len > (p - stack_limit)) {
1457 return 0;
1459 while (len) {
1460 int bytes_to_copy = (len > offset) ? offset : len;
1461 tmp -= bytes_to_copy;
1462 p -= bytes_to_copy;
1463 offset -= bytes_to_copy;
1464 len -= bytes_to_copy;
1466 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1468 if (offset == 0) {
1469 memcpy_to_target(p, scratch, top - p);
1470 top = p;
1471 offset = TARGET_PAGE_SIZE;
1475 if (p != top) {
1476 memcpy_to_target(p, scratch + offset, top - p);
1478 } else {
1479 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1480 for (i = 0; i < argc; ++i) {
1481 tmp = argv[i];
1482 if (!tmp) {
1483 fprintf(stderr, "VFS: argc is wrong");
1484 exit(-1);
1486 len = strlen(tmp) + 1;
1487 if (len > (stack_limit - p)) {
1488 return 0;
1490 while (len) {
1491 int bytes_to_copy = (len > remaining) ? remaining : len;
1493 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1495 tmp += bytes_to_copy;
1496 remaining -= bytes_to_copy;
1497 p += bytes_to_copy;
1498 len -= bytes_to_copy;
1500 if (remaining == 0) {
1501 memcpy_to_target(top, scratch, p - top);
1502 top = p;
1503 remaining = TARGET_PAGE_SIZE;
1507 if (p != top) {
1508 memcpy_to_target(top, scratch, p - top);
1512 return p;
1515 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1516 * argument/environment space. Newer kernels (>2.6.33) allow more,
1517 * dependent on stack size, but guarantee at least 32 pages for
1518 * backwards compatibility.
1520 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1522 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1523 struct image_info *info)
1525 abi_ulong size, error, guard;
1527 size = guest_stack_size;
1528 if (size < STACK_LOWER_LIMIT) {
1529 size = STACK_LOWER_LIMIT;
1531 guard = TARGET_PAGE_SIZE;
1532 if (guard < qemu_real_host_page_size) {
1533 guard = qemu_real_host_page_size;
1536 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1537 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1538 if (error == -1) {
1539 perror("mmap stack");
1540 exit(-1);
1543 /* We reserve one extra page at the top of the stack as guard. */
1544 if (STACK_GROWS_DOWN) {
1545 target_mprotect(error, guard, PROT_NONE);
1546 info->stack_limit = error + guard;
1547 return info->stack_limit + size - sizeof(void *);
1548 } else {
1549 target_mprotect(error + size, guard, PROT_NONE);
1550 info->stack_limit = error + size;
1551 return error;
1555 /* Map and zero the bss. We need to explicitly zero any fractional pages
1556 after the data section (i.e. bss). */
1557 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1559 uintptr_t host_start, host_map_start, host_end;
1561 last_bss = TARGET_PAGE_ALIGN(last_bss);
1563 /* ??? There is confusion between qemu_real_host_page_size and
1564 qemu_host_page_size here and elsewhere in target_mmap, which
1565 may lead to the end of the data section mapping from the file
1566 not being mapped. At least there was an explicit test and
1567 comment for that here, suggesting that "the file size must
1568 be known". The comment probably pre-dates the introduction
1569 of the fstat system call in target_mmap which does in fact
1570 find out the size. What isn't clear is if the workaround
1571 here is still actually needed. For now, continue with it,
1572 but merge it with the "normal" mmap that would allocate the bss. */
1574 host_start = (uintptr_t) g2h(elf_bss);
1575 host_end = (uintptr_t) g2h(last_bss);
1576 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1578 if (host_map_start < host_end) {
1579 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1580 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1581 if (p == MAP_FAILED) {
1582 perror("cannot mmap brk");
1583 exit(-1);
1587 /* Ensure that the bss page(s) are valid */
1588 if ((page_get_flags(last_bss-1) & prot) != prot) {
1589 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1592 if (host_start < host_map_start) {
1593 memset((void *)host_start, 0, host_map_start - host_start);
1597 #ifdef CONFIG_USE_FDPIC
1598 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1600 uint16_t n;
1601 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1603 /* elf32_fdpic_loadseg */
1604 n = info->nsegs;
1605 while (n--) {
1606 sp -= 12;
1607 put_user_u32(loadsegs[n].addr, sp+0);
1608 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1609 put_user_u32(loadsegs[n].p_memsz, sp+8);
1612 /* elf32_fdpic_loadmap */
1613 sp -= 4;
1614 put_user_u16(0, sp+0); /* version */
1615 put_user_u16(info->nsegs, sp+2); /* nsegs */
1617 info->personality = PER_LINUX_FDPIC;
1618 info->loadmap_addr = sp;
1620 return sp;
1622 #endif
1624 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1625 struct elfhdr *exec,
1626 struct image_info *info,
1627 struct image_info *interp_info)
1629 abi_ulong sp;
1630 abi_ulong u_argc, u_argv, u_envp, u_auxv;
1631 int size;
1632 int i;
1633 abi_ulong u_rand_bytes;
1634 uint8_t k_rand_bytes[16];
1635 abi_ulong u_platform;
1636 const char *k_platform;
1637 const int n = sizeof(elf_addr_t);
1639 sp = p;
1641 #ifdef CONFIG_USE_FDPIC
1642 /* Needs to be before we load the env/argc/... */
1643 if (elf_is_fdpic(exec)) {
1644 /* Need 4 byte alignment for these structs */
1645 sp &= ~3;
1646 sp = loader_build_fdpic_loadmap(info, sp);
1647 info->other_info = interp_info;
1648 if (interp_info) {
1649 interp_info->other_info = info;
1650 sp = loader_build_fdpic_loadmap(interp_info, sp);
1653 #endif
1655 u_platform = 0;
1656 k_platform = ELF_PLATFORM;
1657 if (k_platform) {
1658 size_t len = strlen(k_platform) + 1;
1659 if (STACK_GROWS_DOWN) {
1660 sp -= (len + n - 1) & ~(n - 1);
1661 u_platform = sp;
1662 /* FIXME - check return value of memcpy_to_target() for failure */
1663 memcpy_to_target(sp, k_platform, len);
1664 } else {
1665 memcpy_to_target(sp, k_platform, len);
1666 u_platform = sp;
1667 sp += len + 1;
1671 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1672 * the argv and envp pointers.
1674 if (STACK_GROWS_DOWN) {
1675 sp = QEMU_ALIGN_DOWN(sp, 16);
1676 } else {
1677 sp = QEMU_ALIGN_UP(sp, 16);
1681 * Generate 16 random bytes for userspace PRNG seeding (not
1682 * cryptically secure but it's not the aim of QEMU).
1684 for (i = 0; i < 16; i++) {
1685 k_rand_bytes[i] = rand();
1687 if (STACK_GROWS_DOWN) {
1688 sp -= 16;
1689 u_rand_bytes = sp;
1690 /* FIXME - check return value of memcpy_to_target() for failure */
1691 memcpy_to_target(sp, k_rand_bytes, 16);
1692 } else {
1693 memcpy_to_target(sp, k_rand_bytes, 16);
1694 u_rand_bytes = sp;
1695 sp += 16;
1698 size = (DLINFO_ITEMS + 1) * 2;
1699 if (k_platform)
1700 size += 2;
1701 #ifdef DLINFO_ARCH_ITEMS
1702 size += DLINFO_ARCH_ITEMS * 2;
1703 #endif
1704 #ifdef ELF_HWCAP2
1705 size += 2;
1706 #endif
1707 info->auxv_len = size * n;
1709 size += envc + argc + 2;
1710 size += 1; /* argc itself */
1711 size *= n;
1713 /* Allocate space and finalize stack alignment for entry now. */
1714 if (STACK_GROWS_DOWN) {
1715 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
1716 sp = u_argc;
1717 } else {
1718 u_argc = sp;
1719 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
1722 u_argv = u_argc + n;
1723 u_envp = u_argv + (argc + 1) * n;
1724 u_auxv = u_envp + (envc + 1) * n;
1725 info->saved_auxv = u_auxv;
1726 info->arg_start = u_argv;
1727 info->arg_end = u_argv + argc * n;
1729 /* This is correct because Linux defines
1730 * elf_addr_t as Elf32_Off / Elf64_Off
1732 #define NEW_AUX_ENT(id, val) do { \
1733 put_user_ual(id, u_auxv); u_auxv += n; \
1734 put_user_ual(val, u_auxv); u_auxv += n; \
1735 } while(0)
1737 #ifdef ARCH_DLINFO
1739 * ARCH_DLINFO must come first so platform specific code can enforce
1740 * special alignment requirements on the AUXV if necessary (eg. PPC).
1742 ARCH_DLINFO;
1743 #endif
1744 /* There must be exactly DLINFO_ITEMS entries here, or the assert
1745 * on info->auxv_len will trigger.
1747 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1748 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1749 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1750 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize())));
1751 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1752 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1753 NEW_AUX_ENT(AT_ENTRY, info->entry);
1754 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1755 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1756 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1757 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1758 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1759 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1760 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1761 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
1763 #ifdef ELF_HWCAP2
1764 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1765 #endif
1767 if (u_platform) {
1768 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1770 NEW_AUX_ENT (AT_NULL, 0);
1771 #undef NEW_AUX_ENT
1773 /* Check that our initial calculation of the auxv length matches how much
1774 * we actually put into it.
1776 assert(info->auxv_len == u_auxv - info->saved_auxv);
1778 put_user_ual(argc, u_argc);
1780 p = info->arg_strings;
1781 for (i = 0; i < argc; ++i) {
1782 put_user_ual(p, u_argv);
1783 u_argv += n;
1784 p += target_strlen(p) + 1;
1786 put_user_ual(0, u_argv);
1788 p = info->env_strings;
1789 for (i = 0; i < envc; ++i) {
1790 put_user_ual(p, u_envp);
1791 u_envp += n;
1792 p += target_strlen(p) + 1;
1794 put_user_ual(0, u_envp);
1796 return sp;
1799 unsigned long init_guest_space(unsigned long host_start,
1800 unsigned long host_size,
1801 unsigned long guest_start,
1802 bool fixed)
1804 unsigned long current_start, real_start;
1805 int flags;
1807 assert(host_start || host_size);
1809 /* If just a starting address is given, then just verify that
1810 * address. */
1811 if (host_start && !host_size) {
1812 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
1813 if (init_guest_commpage(host_start, host_size) != 1) {
1814 return (unsigned long)-1;
1816 #endif
1817 return host_start;
1820 /* Setup the initial flags and start address. */
1821 current_start = host_start & qemu_host_page_mask;
1822 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1823 if (fixed) {
1824 flags |= MAP_FIXED;
1827 /* Otherwise, a non-zero size region of memory needs to be mapped
1828 * and validated. */
1829 while (1) {
1830 unsigned long real_size = host_size;
1832 /* Do not use mmap_find_vma here because that is limited to the
1833 * guest address space. We are going to make the
1834 * guest address space fit whatever we're given.
1836 real_start = (unsigned long)
1837 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1838 if (real_start == (unsigned long)-1) {
1839 return (unsigned long)-1;
1842 /* Ensure the address is properly aligned. */
1843 if (real_start & ~qemu_host_page_mask) {
1844 munmap((void *)real_start, host_size);
1845 real_size = host_size + qemu_host_page_size;
1846 real_start = (unsigned long)
1847 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1848 if (real_start == (unsigned long)-1) {
1849 return (unsigned long)-1;
1851 real_start = HOST_PAGE_ALIGN(real_start);
1854 /* Check to see if the address is valid. */
1855 if (!host_start || real_start == current_start) {
1856 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
1857 /* On 32-bit ARM, we need to also be able to map the commpage. */
1858 int valid = init_guest_commpage(real_start - guest_start,
1859 real_size);
1860 if (valid == 1) {
1861 break;
1862 } else if (valid == -1) {
1863 munmap((void *)real_start, host_size);
1864 return (unsigned long)-1;
1866 /* valid == 0, so try again. */
1867 #else
1868 /* On other architectures, whatever we have here is fine. */
1869 break;
1870 #endif
1873 /* That address didn't work. Unmap and try a different one.
1874 * The address the host picked because is typically right at
1875 * the top of the host address space and leaves the guest with
1876 * no usable address space. Resort to a linear search. We
1877 * already compensated for mmap_min_addr, so this should not
1878 * happen often. Probably means we got unlucky and host
1879 * address space randomization put a shared library somewhere
1880 * inconvenient.
1882 munmap((void *)real_start, host_size);
1883 current_start += qemu_host_page_size;
1884 if (host_start == current_start) {
1885 /* Theoretically possible if host doesn't have any suitably
1886 * aligned areas. Normally the first mmap will fail.
1888 return (unsigned long)-1;
1892 qemu_log_mask(CPU_LOG_PAGE, "Reserved 0x%lx bytes of guest address space\n", host_size);
1894 return real_start;
1897 static void probe_guest_base(const char *image_name,
1898 abi_ulong loaddr, abi_ulong hiaddr)
1900 /* Probe for a suitable guest base address, if the user has not set
1901 * it explicitly, and set guest_base appropriately.
1902 * In case of error we will print a suitable message and exit.
1904 const char *errmsg;
1905 if (!have_guest_base && !reserved_va) {
1906 unsigned long host_start, real_start, host_size;
1908 /* Round addresses to page boundaries. */
1909 loaddr &= qemu_host_page_mask;
1910 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1912 if (loaddr < mmap_min_addr) {
1913 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1914 } else {
1915 host_start = loaddr;
1916 if (host_start != loaddr) {
1917 errmsg = "Address overflow loading ELF binary";
1918 goto exit_errmsg;
1921 host_size = hiaddr - loaddr;
1923 /* Setup the initial guest memory space with ranges gleaned from
1924 * the ELF image that is being loaded.
1926 real_start = init_guest_space(host_start, host_size, loaddr, false);
1927 if (real_start == (unsigned long)-1) {
1928 errmsg = "Unable to find space for application";
1929 goto exit_errmsg;
1931 guest_base = real_start - loaddr;
1933 qemu_log_mask(CPU_LOG_PAGE, "Relocating guest address space from 0x"
1934 TARGET_ABI_FMT_lx " to 0x%lx\n",
1935 loaddr, real_start);
1937 return;
1939 exit_errmsg:
1940 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1941 exit(-1);
1945 /* Load an ELF image into the address space.
1947 IMAGE_NAME is the filename of the image, to use in error messages.
1948 IMAGE_FD is the open file descriptor for the image.
1950 BPRM_BUF is a copy of the beginning of the file; this of course
1951 contains the elf file header at offset 0. It is assumed that this
1952 buffer is sufficiently aligned to present no problems to the host
1953 in accessing data at aligned offsets within the buffer.
1955 On return: INFO values will be filled in, as necessary or available. */
1957 static void load_elf_image(const char *image_name, int image_fd,
1958 struct image_info *info, char **pinterp_name,
1959 char bprm_buf[BPRM_BUF_SIZE])
1961 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1962 struct elf_phdr *phdr;
1963 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1964 int i, retval;
1965 const char *errmsg;
1967 /* First of all, some simple consistency checks */
1968 errmsg = "Invalid ELF image for this architecture";
1969 if (!elf_check_ident(ehdr)) {
1970 goto exit_errmsg;
1972 bswap_ehdr(ehdr);
1973 if (!elf_check_ehdr(ehdr)) {
1974 goto exit_errmsg;
1977 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1978 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1979 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
1980 } else {
1981 phdr = (struct elf_phdr *) alloca(i);
1982 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
1983 if (retval != i) {
1984 goto exit_read;
1987 bswap_phdr(phdr, ehdr->e_phnum);
1989 #ifdef CONFIG_USE_FDPIC
1990 info->nsegs = 0;
1991 info->pt_dynamic_addr = 0;
1992 #endif
1994 mmap_lock();
1996 /* Find the maximum size of the image and allocate an appropriate
1997 amount of memory to handle that. */
1998 loaddr = -1, hiaddr = 0;
1999 for (i = 0; i < ehdr->e_phnum; ++i) {
2000 if (phdr[i].p_type == PT_LOAD) {
2001 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
2002 if (a < loaddr) {
2003 loaddr = a;
2005 a = phdr[i].p_vaddr + phdr[i].p_memsz;
2006 if (a > hiaddr) {
2007 hiaddr = a;
2009 #ifdef CONFIG_USE_FDPIC
2010 ++info->nsegs;
2011 #endif
2015 load_addr = loaddr;
2016 if (ehdr->e_type == ET_DYN) {
2017 /* The image indicates that it can be loaded anywhere. Find a
2018 location that can hold the memory space required. If the
2019 image is pre-linked, LOADDR will be non-zero. Since we do
2020 not supply MAP_FIXED here we'll use that address if and
2021 only if it remains available. */
2022 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2023 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
2024 -1, 0);
2025 if (load_addr == -1) {
2026 goto exit_perror;
2028 } else if (pinterp_name != NULL) {
2029 /* This is the main executable. Make sure that the low
2030 address does not conflict with MMAP_MIN_ADDR or the
2031 QEMU application itself. */
2032 probe_guest_base(image_name, loaddr, hiaddr);
2034 load_bias = load_addr - loaddr;
2036 #ifdef CONFIG_USE_FDPIC
2038 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2039 g_malloc(sizeof(*loadsegs) * info->nsegs);
2041 for (i = 0; i < ehdr->e_phnum; ++i) {
2042 switch (phdr[i].p_type) {
2043 case PT_DYNAMIC:
2044 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2045 break;
2046 case PT_LOAD:
2047 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2048 loadsegs->p_vaddr = phdr[i].p_vaddr;
2049 loadsegs->p_memsz = phdr[i].p_memsz;
2050 ++loadsegs;
2051 break;
2055 #endif
2057 info->load_bias = load_bias;
2058 info->load_addr = load_addr;
2059 info->entry = ehdr->e_entry + load_bias;
2060 info->start_code = -1;
2061 info->end_code = 0;
2062 info->start_data = -1;
2063 info->end_data = 0;
2064 info->brk = 0;
2065 info->elf_flags = ehdr->e_flags;
2067 for (i = 0; i < ehdr->e_phnum; i++) {
2068 struct elf_phdr *eppnt = phdr + i;
2069 if (eppnt->p_type == PT_LOAD) {
2070 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
2071 int elf_prot = 0;
2073 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
2074 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
2075 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
2077 vaddr = load_bias + eppnt->p_vaddr;
2078 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2079 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2081 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
2082 elf_prot, MAP_PRIVATE | MAP_FIXED,
2083 image_fd, eppnt->p_offset - vaddr_po);
2084 if (error == -1) {
2085 goto exit_perror;
2088 vaddr_ef = vaddr + eppnt->p_filesz;
2089 vaddr_em = vaddr + eppnt->p_memsz;
2091 /* If the load segment requests extra zeros (e.g. bss), map it. */
2092 if (vaddr_ef < vaddr_em) {
2093 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2096 /* Find the full program boundaries. */
2097 if (elf_prot & PROT_EXEC) {
2098 if (vaddr < info->start_code) {
2099 info->start_code = vaddr;
2101 if (vaddr_ef > info->end_code) {
2102 info->end_code = vaddr_ef;
2105 if (elf_prot & PROT_WRITE) {
2106 if (vaddr < info->start_data) {
2107 info->start_data = vaddr;
2109 if (vaddr_ef > info->end_data) {
2110 info->end_data = vaddr_ef;
2112 if (vaddr_em > info->brk) {
2113 info->brk = vaddr_em;
2116 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2117 char *interp_name;
2119 if (*pinterp_name) {
2120 errmsg = "Multiple PT_INTERP entries";
2121 goto exit_errmsg;
2123 interp_name = malloc(eppnt->p_filesz);
2124 if (!interp_name) {
2125 goto exit_perror;
2128 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2129 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2130 eppnt->p_filesz);
2131 } else {
2132 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2133 eppnt->p_offset);
2134 if (retval != eppnt->p_filesz) {
2135 goto exit_perror;
2138 if (interp_name[eppnt->p_filesz - 1] != 0) {
2139 errmsg = "Invalid PT_INTERP entry";
2140 goto exit_errmsg;
2142 *pinterp_name = interp_name;
2146 if (info->end_data == 0) {
2147 info->start_data = info->end_code;
2148 info->end_data = info->end_code;
2149 info->brk = info->end_code;
2152 if (qemu_log_enabled()) {
2153 load_symbols(ehdr, image_fd, load_bias);
2156 mmap_unlock();
2158 close(image_fd);
2159 return;
2161 exit_read:
2162 if (retval >= 0) {
2163 errmsg = "Incomplete read of file header";
2164 goto exit_errmsg;
2166 exit_perror:
2167 errmsg = strerror(errno);
2168 exit_errmsg:
2169 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2170 exit(-1);
2173 static void load_elf_interp(const char *filename, struct image_info *info,
2174 char bprm_buf[BPRM_BUF_SIZE])
2176 int fd, retval;
2178 fd = open(path(filename), O_RDONLY);
2179 if (fd < 0) {
2180 goto exit_perror;
2183 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2184 if (retval < 0) {
2185 goto exit_perror;
2187 if (retval < BPRM_BUF_SIZE) {
2188 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2191 load_elf_image(filename, fd, info, NULL, bprm_buf);
2192 return;
2194 exit_perror:
2195 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2196 exit(-1);
2199 static int symfind(const void *s0, const void *s1)
2201 target_ulong addr = *(target_ulong *)s0;
2202 struct elf_sym *sym = (struct elf_sym *)s1;
2203 int result = 0;
2204 if (addr < sym->st_value) {
2205 result = -1;
2206 } else if (addr >= sym->st_value + sym->st_size) {
2207 result = 1;
2209 return result;
2212 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2214 #if ELF_CLASS == ELFCLASS32
2215 struct elf_sym *syms = s->disas_symtab.elf32;
2216 #else
2217 struct elf_sym *syms = s->disas_symtab.elf64;
2218 #endif
2220 // binary search
2221 struct elf_sym *sym;
2223 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2224 if (sym != NULL) {
2225 return s->disas_strtab + sym->st_name;
2228 return "";
2231 /* FIXME: This should use elf_ops.h */
2232 static int symcmp(const void *s0, const void *s1)
2234 struct elf_sym *sym0 = (struct elf_sym *)s0;
2235 struct elf_sym *sym1 = (struct elf_sym *)s1;
2236 return (sym0->st_value < sym1->st_value)
2237 ? -1
2238 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2241 /* Best attempt to load symbols from this ELF object. */
2242 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2244 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2245 uint64_t segsz;
2246 struct elf_shdr *shdr;
2247 char *strings = NULL;
2248 struct syminfo *s = NULL;
2249 struct elf_sym *new_syms, *syms = NULL;
2251 shnum = hdr->e_shnum;
2252 i = shnum * sizeof(struct elf_shdr);
2253 shdr = (struct elf_shdr *)alloca(i);
2254 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2255 return;
2258 bswap_shdr(shdr, shnum);
2259 for (i = 0; i < shnum; ++i) {
2260 if (shdr[i].sh_type == SHT_SYMTAB) {
2261 sym_idx = i;
2262 str_idx = shdr[i].sh_link;
2263 goto found;
2267 /* There will be no symbol table if the file was stripped. */
2268 return;
2270 found:
2271 /* Now know where the strtab and symtab are. Snarf them. */
2272 s = g_try_new(struct syminfo, 1);
2273 if (!s) {
2274 goto give_up;
2277 segsz = shdr[str_idx].sh_size;
2278 s->disas_strtab = strings = g_try_malloc(segsz);
2279 if (!strings ||
2280 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
2281 goto give_up;
2284 segsz = shdr[sym_idx].sh_size;
2285 syms = g_try_malloc(segsz);
2286 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
2287 goto give_up;
2290 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
2291 /* Implausibly large symbol table: give up rather than ploughing
2292 * on with the number of symbols calculation overflowing
2294 goto give_up;
2296 nsyms = segsz / sizeof(struct elf_sym);
2297 for (i = 0; i < nsyms; ) {
2298 bswap_sym(syms + i);
2299 /* Throw away entries which we do not need. */
2300 if (syms[i].st_shndx == SHN_UNDEF
2301 || syms[i].st_shndx >= SHN_LORESERVE
2302 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2303 if (i < --nsyms) {
2304 syms[i] = syms[nsyms];
2306 } else {
2307 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2308 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2309 syms[i].st_value &= ~(target_ulong)1;
2310 #endif
2311 syms[i].st_value += load_bias;
2312 i++;
2316 /* No "useful" symbol. */
2317 if (nsyms == 0) {
2318 goto give_up;
2321 /* Attempt to free the storage associated with the local symbols
2322 that we threw away. Whether or not this has any effect on the
2323 memory allocation depends on the malloc implementation and how
2324 many symbols we managed to discard. */
2325 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
2326 if (new_syms == NULL) {
2327 goto give_up;
2329 syms = new_syms;
2331 qsort(syms, nsyms, sizeof(*syms), symcmp);
2333 s->disas_num_syms = nsyms;
2334 #if ELF_CLASS == ELFCLASS32
2335 s->disas_symtab.elf32 = syms;
2336 #else
2337 s->disas_symtab.elf64 = syms;
2338 #endif
2339 s->lookup_symbol = lookup_symbolxx;
2340 s->next = syminfos;
2341 syminfos = s;
2343 return;
2345 give_up:
2346 g_free(s);
2347 g_free(strings);
2348 g_free(syms);
2351 uint32_t get_elf_eflags(int fd)
2353 struct elfhdr ehdr;
2354 off_t offset;
2355 int ret;
2357 /* Read ELF header */
2358 offset = lseek(fd, 0, SEEK_SET);
2359 if (offset == (off_t) -1) {
2360 return 0;
2362 ret = read(fd, &ehdr, sizeof(ehdr));
2363 if (ret < sizeof(ehdr)) {
2364 return 0;
2366 offset = lseek(fd, offset, SEEK_SET);
2367 if (offset == (off_t) -1) {
2368 return 0;
2371 /* Check ELF signature */
2372 if (!elf_check_ident(&ehdr)) {
2373 return 0;
2376 /* check header */
2377 bswap_ehdr(&ehdr);
2378 if (!elf_check_ehdr(&ehdr)) {
2379 return 0;
2382 /* return architecture id */
2383 return ehdr.e_flags;
2386 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2388 struct image_info interp_info;
2389 struct elfhdr elf_ex;
2390 char *elf_interpreter = NULL;
2391 char *scratch;
2393 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2395 load_elf_image(bprm->filename, bprm->fd, info,
2396 &elf_interpreter, bprm->buf);
2398 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2399 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2400 when we load the interpreter. */
2401 elf_ex = *(struct elfhdr *)bprm->buf;
2403 /* Do this so that we can load the interpreter, if need be. We will
2404 change some of these later */
2405 bprm->p = setup_arg_pages(bprm, info);
2407 scratch = g_new0(char, TARGET_PAGE_SIZE);
2408 if (STACK_GROWS_DOWN) {
2409 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2410 bprm->p, info->stack_limit);
2411 info->file_string = bprm->p;
2412 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2413 bprm->p, info->stack_limit);
2414 info->env_strings = bprm->p;
2415 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2416 bprm->p, info->stack_limit);
2417 info->arg_strings = bprm->p;
2418 } else {
2419 info->arg_strings = bprm->p;
2420 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2421 bprm->p, info->stack_limit);
2422 info->env_strings = bprm->p;
2423 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2424 bprm->p, info->stack_limit);
2425 info->file_string = bprm->p;
2426 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2427 bprm->p, info->stack_limit);
2430 g_free(scratch);
2432 if (!bprm->p) {
2433 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2434 exit(-1);
2437 if (elf_interpreter) {
2438 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2440 /* If the program interpreter is one of these two, then assume
2441 an iBCS2 image. Otherwise assume a native linux image. */
2443 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2444 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2445 info->personality = PER_SVR4;
2447 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2448 and some applications "depend" upon this behavior. Since
2449 we do not have the power to recompile these, we emulate
2450 the SVr4 behavior. Sigh. */
2451 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2452 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2456 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2457 info, (elf_interpreter ? &interp_info : NULL));
2458 info->start_stack = bprm->p;
2460 /* If we have an interpreter, set that as the program's entry point.
2461 Copy the load_bias as well, to help PPC64 interpret the entry
2462 point as a function descriptor. Do this after creating elf tables
2463 so that we copy the original program entry point into the AUXV. */
2464 if (elf_interpreter) {
2465 info->load_bias = interp_info.load_bias;
2466 info->entry = interp_info.entry;
2467 free(elf_interpreter);
2470 #ifdef USE_ELF_CORE_DUMP
2471 bprm->core_dump = &elf_core_dump;
2472 #endif
2474 return 0;
2477 #ifdef USE_ELF_CORE_DUMP
2479 * Definitions to generate Intel SVR4-like core files.
2480 * These mostly have the same names as the SVR4 types with "target_elf_"
2481 * tacked on the front to prevent clashes with linux definitions,
2482 * and the typedef forms have been avoided. This is mostly like
2483 * the SVR4 structure, but more Linuxy, with things that Linux does
2484 * not support and which gdb doesn't really use excluded.
2486 * Fields we don't dump (their contents is zero) in linux-user qemu
2487 * are marked with XXX.
2489 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2491 * Porting ELF coredump for target is (quite) simple process. First you
2492 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2493 * the target resides):
2495 * #define USE_ELF_CORE_DUMP
2497 * Next you define type of register set used for dumping. ELF specification
2498 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2500 * typedef <target_regtype> target_elf_greg_t;
2501 * #define ELF_NREG <number of registers>
2502 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2504 * Last step is to implement target specific function that copies registers
2505 * from given cpu into just specified register set. Prototype is:
2507 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2508 * const CPUArchState *env);
2510 * Parameters:
2511 * regs - copy register values into here (allocated and zeroed by caller)
2512 * env - copy registers from here
2514 * Example for ARM target is provided in this file.
2517 /* An ELF note in memory */
2518 struct memelfnote {
2519 const char *name;
2520 size_t namesz;
2521 size_t namesz_rounded;
2522 int type;
2523 size_t datasz;
2524 size_t datasz_rounded;
2525 void *data;
2526 size_t notesz;
2529 struct target_elf_siginfo {
2530 abi_int si_signo; /* signal number */
2531 abi_int si_code; /* extra code */
2532 abi_int si_errno; /* errno */
2535 struct target_elf_prstatus {
2536 struct target_elf_siginfo pr_info; /* Info associated with signal */
2537 abi_short pr_cursig; /* Current signal */
2538 abi_ulong pr_sigpend; /* XXX */
2539 abi_ulong pr_sighold; /* XXX */
2540 target_pid_t pr_pid;
2541 target_pid_t pr_ppid;
2542 target_pid_t pr_pgrp;
2543 target_pid_t pr_sid;
2544 struct target_timeval pr_utime; /* XXX User time */
2545 struct target_timeval pr_stime; /* XXX System time */
2546 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2547 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2548 target_elf_gregset_t pr_reg; /* GP registers */
2549 abi_int pr_fpvalid; /* XXX */
2552 #define ELF_PRARGSZ (80) /* Number of chars for args */
2554 struct target_elf_prpsinfo {
2555 char pr_state; /* numeric process state */
2556 char pr_sname; /* char for pr_state */
2557 char pr_zomb; /* zombie */
2558 char pr_nice; /* nice val */
2559 abi_ulong pr_flag; /* flags */
2560 target_uid_t pr_uid;
2561 target_gid_t pr_gid;
2562 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2563 /* Lots missing */
2564 char pr_fname[16]; /* filename of executable */
2565 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2568 /* Here is the structure in which status of each thread is captured. */
2569 struct elf_thread_status {
2570 QTAILQ_ENTRY(elf_thread_status) ets_link;
2571 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2572 #if 0
2573 elf_fpregset_t fpu; /* NT_PRFPREG */
2574 struct task_struct *thread;
2575 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2576 #endif
2577 struct memelfnote notes[1];
2578 int num_notes;
2581 struct elf_note_info {
2582 struct memelfnote *notes;
2583 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2584 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2586 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2587 #if 0
2589 * Current version of ELF coredump doesn't support
2590 * dumping fp regs etc.
2592 elf_fpregset_t *fpu;
2593 elf_fpxregset_t *xfpu;
2594 int thread_status_size;
2595 #endif
2596 int notes_size;
2597 int numnote;
2600 struct vm_area_struct {
2601 target_ulong vma_start; /* start vaddr of memory region */
2602 target_ulong vma_end; /* end vaddr of memory region */
2603 abi_ulong vma_flags; /* protection etc. flags for the region */
2604 QTAILQ_ENTRY(vm_area_struct) vma_link;
2607 struct mm_struct {
2608 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2609 int mm_count; /* number of mappings */
2612 static struct mm_struct *vma_init(void);
2613 static void vma_delete(struct mm_struct *);
2614 static int vma_add_mapping(struct mm_struct *, target_ulong,
2615 target_ulong, abi_ulong);
2616 static int vma_get_mapping_count(const struct mm_struct *);
2617 static struct vm_area_struct *vma_first(const struct mm_struct *);
2618 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2619 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2620 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2621 unsigned long flags);
2623 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2624 static void fill_note(struct memelfnote *, const char *, int,
2625 unsigned int, void *);
2626 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2627 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2628 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2629 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2630 static size_t note_size(const struct memelfnote *);
2631 static void free_note_info(struct elf_note_info *);
2632 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2633 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2634 static int core_dump_filename(const TaskState *, char *, size_t);
2636 static int dump_write(int, const void *, size_t);
2637 static int write_note(struct memelfnote *, int);
2638 static int write_note_info(struct elf_note_info *, int);
2640 #ifdef BSWAP_NEEDED
2641 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2643 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2644 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2645 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2646 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2647 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2648 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2649 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2650 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2651 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2652 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2653 /* cpu times are not filled, so we skip them */
2654 /* regs should be in correct format already */
2655 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2658 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2660 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2661 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2662 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2663 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2664 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2665 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2666 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2669 static void bswap_note(struct elf_note *en)
2671 bswap32s(&en->n_namesz);
2672 bswap32s(&en->n_descsz);
2673 bswap32s(&en->n_type);
2675 #else
2676 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2677 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2678 static inline void bswap_note(struct elf_note *en) { }
2679 #endif /* BSWAP_NEEDED */
2682 * Minimal support for linux memory regions. These are needed
2683 * when we are finding out what memory exactly belongs to
2684 * emulated process. No locks needed here, as long as
2685 * thread that received the signal is stopped.
2688 static struct mm_struct *vma_init(void)
2690 struct mm_struct *mm;
2692 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2693 return (NULL);
2695 mm->mm_count = 0;
2696 QTAILQ_INIT(&mm->mm_mmap);
2698 return (mm);
2701 static void vma_delete(struct mm_struct *mm)
2703 struct vm_area_struct *vma;
2705 while ((vma = vma_first(mm)) != NULL) {
2706 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2707 g_free(vma);
2709 g_free(mm);
2712 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
2713 target_ulong end, abi_ulong flags)
2715 struct vm_area_struct *vma;
2717 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2718 return (-1);
2720 vma->vma_start = start;
2721 vma->vma_end = end;
2722 vma->vma_flags = flags;
2724 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2725 mm->mm_count++;
2727 return (0);
2730 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2732 return (QTAILQ_FIRST(&mm->mm_mmap));
2735 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2737 return (QTAILQ_NEXT(vma, vma_link));
2740 static int vma_get_mapping_count(const struct mm_struct *mm)
2742 return (mm->mm_count);
2746 * Calculate file (dump) size of given memory region.
2748 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2750 /* if we cannot even read the first page, skip it */
2751 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2752 return (0);
2755 * Usually we don't dump executable pages as they contain
2756 * non-writable code that debugger can read directly from
2757 * target library etc. However, thread stacks are marked
2758 * also executable so we read in first page of given region
2759 * and check whether it contains elf header. If there is
2760 * no elf header, we dump it.
2762 if (vma->vma_flags & PROT_EXEC) {
2763 char page[TARGET_PAGE_SIZE];
2765 copy_from_user(page, vma->vma_start, sizeof (page));
2766 if ((page[EI_MAG0] == ELFMAG0) &&
2767 (page[EI_MAG1] == ELFMAG1) &&
2768 (page[EI_MAG2] == ELFMAG2) &&
2769 (page[EI_MAG3] == ELFMAG3)) {
2771 * Mappings are possibly from ELF binary. Don't dump
2772 * them.
2774 return (0);
2778 return (vma->vma_end - vma->vma_start);
2781 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2782 unsigned long flags)
2784 struct mm_struct *mm = (struct mm_struct *)priv;
2786 vma_add_mapping(mm, start, end, flags);
2787 return (0);
2790 static void fill_note(struct memelfnote *note, const char *name, int type,
2791 unsigned int sz, void *data)
2793 unsigned int namesz;
2795 namesz = strlen(name) + 1;
2796 note->name = name;
2797 note->namesz = namesz;
2798 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2799 note->type = type;
2800 note->datasz = sz;
2801 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2803 note->data = data;
2806 * We calculate rounded up note size here as specified by
2807 * ELF document.
2809 note->notesz = sizeof (struct elf_note) +
2810 note->namesz_rounded + note->datasz_rounded;
2813 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2814 uint32_t flags)
2816 (void) memset(elf, 0, sizeof(*elf));
2818 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2819 elf->e_ident[EI_CLASS] = ELF_CLASS;
2820 elf->e_ident[EI_DATA] = ELF_DATA;
2821 elf->e_ident[EI_VERSION] = EV_CURRENT;
2822 elf->e_ident[EI_OSABI] = ELF_OSABI;
2824 elf->e_type = ET_CORE;
2825 elf->e_machine = machine;
2826 elf->e_version = EV_CURRENT;
2827 elf->e_phoff = sizeof(struct elfhdr);
2828 elf->e_flags = flags;
2829 elf->e_ehsize = sizeof(struct elfhdr);
2830 elf->e_phentsize = sizeof(struct elf_phdr);
2831 elf->e_phnum = segs;
2833 bswap_ehdr(elf);
2836 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2838 phdr->p_type = PT_NOTE;
2839 phdr->p_offset = offset;
2840 phdr->p_vaddr = 0;
2841 phdr->p_paddr = 0;
2842 phdr->p_filesz = sz;
2843 phdr->p_memsz = 0;
2844 phdr->p_flags = 0;
2845 phdr->p_align = 0;
2847 bswap_phdr(phdr, 1);
2850 static size_t note_size(const struct memelfnote *note)
2852 return (note->notesz);
2855 static void fill_prstatus(struct target_elf_prstatus *prstatus,
2856 const TaskState *ts, int signr)
2858 (void) memset(prstatus, 0, sizeof (*prstatus));
2859 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2860 prstatus->pr_pid = ts->ts_tid;
2861 prstatus->pr_ppid = getppid();
2862 prstatus->pr_pgrp = getpgrp();
2863 prstatus->pr_sid = getsid(0);
2865 bswap_prstatus(prstatus);
2868 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2870 char *base_filename;
2871 unsigned int i, len;
2873 (void) memset(psinfo, 0, sizeof (*psinfo));
2875 len = ts->info->arg_end - ts->info->arg_start;
2876 if (len >= ELF_PRARGSZ)
2877 len = ELF_PRARGSZ - 1;
2878 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2879 return -EFAULT;
2880 for (i = 0; i < len; i++)
2881 if (psinfo->pr_psargs[i] == 0)
2882 psinfo->pr_psargs[i] = ' ';
2883 psinfo->pr_psargs[len] = 0;
2885 psinfo->pr_pid = getpid();
2886 psinfo->pr_ppid = getppid();
2887 psinfo->pr_pgrp = getpgrp();
2888 psinfo->pr_sid = getsid(0);
2889 psinfo->pr_uid = getuid();
2890 psinfo->pr_gid = getgid();
2892 base_filename = g_path_get_basename(ts->bprm->filename);
2894 * Using strncpy here is fine: at max-length,
2895 * this field is not NUL-terminated.
2897 (void) strncpy(psinfo->pr_fname, base_filename,
2898 sizeof(psinfo->pr_fname));
2900 g_free(base_filename);
2901 bswap_psinfo(psinfo);
2902 return (0);
2905 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2907 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2908 elf_addr_t orig_auxv = auxv;
2909 void *ptr;
2910 int len = ts->info->auxv_len;
2913 * Auxiliary vector is stored in target process stack. It contains
2914 * {type, value} pairs that we need to dump into note. This is not
2915 * strictly necessary but we do it here for sake of completeness.
2918 /* read in whole auxv vector and copy it to memelfnote */
2919 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2920 if (ptr != NULL) {
2921 fill_note(note, "CORE", NT_AUXV, len, ptr);
2922 unlock_user(ptr, auxv, len);
2927 * Constructs name of coredump file. We have following convention
2928 * for the name:
2929 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2931 * Returns 0 in case of success, -1 otherwise (errno is set).
2933 static int core_dump_filename(const TaskState *ts, char *buf,
2934 size_t bufsize)
2936 char timestamp[64];
2937 char *base_filename = NULL;
2938 struct timeval tv;
2939 struct tm tm;
2941 assert(bufsize >= PATH_MAX);
2943 if (gettimeofday(&tv, NULL) < 0) {
2944 (void) fprintf(stderr, "unable to get current timestamp: %s",
2945 strerror(errno));
2946 return (-1);
2949 base_filename = g_path_get_basename(ts->bprm->filename);
2950 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2951 localtime_r(&tv.tv_sec, &tm));
2952 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2953 base_filename, timestamp, (int)getpid());
2954 g_free(base_filename);
2956 return (0);
2959 static int dump_write(int fd, const void *ptr, size_t size)
2961 const char *bufp = (const char *)ptr;
2962 ssize_t bytes_written, bytes_left;
2963 struct rlimit dumpsize;
2964 off_t pos;
2966 bytes_written = 0;
2967 getrlimit(RLIMIT_CORE, &dumpsize);
2968 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2969 if (errno == ESPIPE) { /* not a seekable stream */
2970 bytes_left = size;
2971 } else {
2972 return pos;
2974 } else {
2975 if (dumpsize.rlim_cur <= pos) {
2976 return -1;
2977 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2978 bytes_left = size;
2979 } else {
2980 size_t limit_left=dumpsize.rlim_cur - pos;
2981 bytes_left = limit_left >= size ? size : limit_left ;
2986 * In normal conditions, single write(2) should do but
2987 * in case of socket etc. this mechanism is more portable.
2989 do {
2990 bytes_written = write(fd, bufp, bytes_left);
2991 if (bytes_written < 0) {
2992 if (errno == EINTR)
2993 continue;
2994 return (-1);
2995 } else if (bytes_written == 0) { /* eof */
2996 return (-1);
2998 bufp += bytes_written;
2999 bytes_left -= bytes_written;
3000 } while (bytes_left > 0);
3002 return (0);
3005 static int write_note(struct memelfnote *men, int fd)
3007 struct elf_note en;
3009 en.n_namesz = men->namesz;
3010 en.n_type = men->type;
3011 en.n_descsz = men->datasz;
3013 bswap_note(&en);
3015 if (dump_write(fd, &en, sizeof(en)) != 0)
3016 return (-1);
3017 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3018 return (-1);
3019 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
3020 return (-1);
3022 return (0);
3025 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
3027 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3028 TaskState *ts = (TaskState *)cpu->opaque;
3029 struct elf_thread_status *ets;
3031 ets = g_malloc0(sizeof (*ets));
3032 ets->num_notes = 1; /* only prstatus is dumped */
3033 fill_prstatus(&ets->prstatus, ts, 0);
3034 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3035 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
3036 &ets->prstatus);
3038 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
3040 info->notes_size += note_size(&ets->notes[0]);
3043 static void init_note_info(struct elf_note_info *info)
3045 /* Initialize the elf_note_info structure so that it is at
3046 * least safe to call free_note_info() on it. Must be
3047 * called before calling fill_note_info().
3049 memset(info, 0, sizeof (*info));
3050 QTAILQ_INIT(&info->thread_list);
3053 static int fill_note_info(struct elf_note_info *info,
3054 long signr, const CPUArchState *env)
3056 #define NUMNOTES 3
3057 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3058 TaskState *ts = (TaskState *)cpu->opaque;
3059 int i;
3061 info->notes = g_new0(struct memelfnote, NUMNOTES);
3062 if (info->notes == NULL)
3063 return (-ENOMEM);
3064 info->prstatus = g_malloc0(sizeof (*info->prstatus));
3065 if (info->prstatus == NULL)
3066 return (-ENOMEM);
3067 info->psinfo = g_malloc0(sizeof (*info->psinfo));
3068 if (info->prstatus == NULL)
3069 return (-ENOMEM);
3072 * First fill in status (and registers) of current thread
3073 * including process info & aux vector.
3075 fill_prstatus(info->prstatus, ts, signr);
3076 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3077 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
3078 sizeof (*info->prstatus), info->prstatus);
3079 fill_psinfo(info->psinfo, ts);
3080 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
3081 sizeof (*info->psinfo), info->psinfo);
3082 fill_auxv_note(&info->notes[2], ts);
3083 info->numnote = 3;
3085 info->notes_size = 0;
3086 for (i = 0; i < info->numnote; i++)
3087 info->notes_size += note_size(&info->notes[i]);
3089 /* read and fill status of all threads */
3090 cpu_list_lock();
3091 CPU_FOREACH(cpu) {
3092 if (cpu == thread_cpu) {
3093 continue;
3095 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
3097 cpu_list_unlock();
3099 return (0);
3102 static void free_note_info(struct elf_note_info *info)
3104 struct elf_thread_status *ets;
3106 while (!QTAILQ_EMPTY(&info->thread_list)) {
3107 ets = QTAILQ_FIRST(&info->thread_list);
3108 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
3109 g_free(ets);
3112 g_free(info->prstatus);
3113 g_free(info->psinfo);
3114 g_free(info->notes);
3117 static int write_note_info(struct elf_note_info *info, int fd)
3119 struct elf_thread_status *ets;
3120 int i, error = 0;
3122 /* write prstatus, psinfo and auxv for current thread */
3123 for (i = 0; i < info->numnote; i++)
3124 if ((error = write_note(&info->notes[i], fd)) != 0)
3125 return (error);
3127 /* write prstatus for each thread */
3128 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
3129 if ((error = write_note(&ets->notes[0], fd)) != 0)
3130 return (error);
3133 return (0);
3137 * Write out ELF coredump.
3139 * See documentation of ELF object file format in:
3140 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3142 * Coredump format in linux is following:
3144 * 0 +----------------------+ \
3145 * | ELF header | ET_CORE |
3146 * +----------------------+ |
3147 * | ELF program headers | |--- headers
3148 * | - NOTE section | |
3149 * | - PT_LOAD sections | |
3150 * +----------------------+ /
3151 * | NOTEs: |
3152 * | - NT_PRSTATUS |
3153 * | - NT_PRSINFO |
3154 * | - NT_AUXV |
3155 * +----------------------+ <-- aligned to target page
3156 * | Process memory dump |
3157 * : :
3158 * . .
3159 * : :
3160 * | |
3161 * +----------------------+
3163 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3164 * NT_PRSINFO -> struct elf_prpsinfo
3165 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3167 * Format follows System V format as close as possible. Current
3168 * version limitations are as follows:
3169 * - no floating point registers are dumped
3171 * Function returns 0 in case of success, negative errno otherwise.
3173 * TODO: make this work also during runtime: it should be
3174 * possible to force coredump from running process and then
3175 * continue processing. For example qemu could set up SIGUSR2
3176 * handler (provided that target process haven't registered
3177 * handler for that) that does the dump when signal is received.
3179 static int elf_core_dump(int signr, const CPUArchState *env)
3181 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3182 const TaskState *ts = (const TaskState *)cpu->opaque;
3183 struct vm_area_struct *vma = NULL;
3184 char corefile[PATH_MAX];
3185 struct elf_note_info info;
3186 struct elfhdr elf;
3187 struct elf_phdr phdr;
3188 struct rlimit dumpsize;
3189 struct mm_struct *mm = NULL;
3190 off_t offset = 0, data_offset = 0;
3191 int segs = 0;
3192 int fd = -1;
3194 init_note_info(&info);
3196 errno = 0;
3197 getrlimit(RLIMIT_CORE, &dumpsize);
3198 if (dumpsize.rlim_cur == 0)
3199 return 0;
3201 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
3202 return (-errno);
3204 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3205 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3206 return (-errno);
3209 * Walk through target process memory mappings and
3210 * set up structure containing this information. After
3211 * this point vma_xxx functions can be used.
3213 if ((mm = vma_init()) == NULL)
3214 goto out;
3216 walk_memory_regions(mm, vma_walker);
3217 segs = vma_get_mapping_count(mm);
3220 * Construct valid coredump ELF header. We also
3221 * add one more segment for notes.
3223 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3224 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3225 goto out;
3227 /* fill in the in-memory version of notes */
3228 if (fill_note_info(&info, signr, env) < 0)
3229 goto out;
3231 offset += sizeof (elf); /* elf header */
3232 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3234 /* write out notes program header */
3235 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3237 offset += info.notes_size;
3238 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3239 goto out;
3242 * ELF specification wants data to start at page boundary so
3243 * we align it here.
3245 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3248 * Write program headers for memory regions mapped in
3249 * the target process.
3251 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3252 (void) memset(&phdr, 0, sizeof (phdr));
3254 phdr.p_type = PT_LOAD;
3255 phdr.p_offset = offset;
3256 phdr.p_vaddr = vma->vma_start;
3257 phdr.p_paddr = 0;
3258 phdr.p_filesz = vma_dump_size(vma);
3259 offset += phdr.p_filesz;
3260 phdr.p_memsz = vma->vma_end - vma->vma_start;
3261 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3262 if (vma->vma_flags & PROT_WRITE)
3263 phdr.p_flags |= PF_W;
3264 if (vma->vma_flags & PROT_EXEC)
3265 phdr.p_flags |= PF_X;
3266 phdr.p_align = ELF_EXEC_PAGESIZE;
3268 bswap_phdr(&phdr, 1);
3269 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
3270 goto out;
3275 * Next we write notes just after program headers. No
3276 * alignment needed here.
3278 if (write_note_info(&info, fd) < 0)
3279 goto out;
3281 /* align data to page boundary */
3282 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3283 goto out;
3286 * Finally we can dump process memory into corefile as well.
3288 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3289 abi_ulong addr;
3290 abi_ulong end;
3292 end = vma->vma_start + vma_dump_size(vma);
3294 for (addr = vma->vma_start; addr < end;
3295 addr += TARGET_PAGE_SIZE) {
3296 char page[TARGET_PAGE_SIZE];
3297 int error;
3300 * Read in page from target process memory and
3301 * write it to coredump file.
3303 error = copy_from_user(page, addr, sizeof (page));
3304 if (error != 0) {
3305 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3306 addr);
3307 errno = -error;
3308 goto out;
3310 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3311 goto out;
3315 out:
3316 free_note_info(&info);
3317 if (mm != NULL)
3318 vma_delete(mm);
3319 (void) close(fd);
3321 if (errno != 0)
3322 return (-errno);
3323 return (0);
3325 #endif /* USE_ELF_CORE_DUMP */
3327 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3329 init_thread(regs, infop);