1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
5 #include <sys/resource.h>
8 #include "disas/disas.h"
21 #define ELF_OSABI ELFOSABI_SYSV
23 /* from personality.h */
26 * Flags for bug emulation.
28 * These occupy the top three bytes.
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,
47 * These go in the low byte. Avoid using the top bit, it will
48 * conflict with error returns.
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
,
61 PER_SUNOS
= 0x0006 | STICKY_TIMEOUTS
,
62 PER_XENIX
= 0x0007 | STICKY_TIMEOUTS
| SHORT_INODE
,
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 */
69 PER_SOLARIS
= 0x000d | STICKY_TIMEOUTS
,
70 PER_UW7
= 0x000e | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
71 PER_OSF4
= 0x000f, /* OSF/1 v4 */
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 */
83 #define MAP_DENYWRITE 0
86 /* should probably go in elf.h */
91 #ifdef TARGET_WORDS_BIGENDIAN
92 #define ELF_DATA ELFDATA2MSB
94 #define ELF_DATA ELFDATA2LSB
97 #ifdef TARGET_ABI_MIPSN32
98 typedef abi_ullong target_elf_greg_t
;
99 #define tswapreg(ptr) tswap64(ptr)
101 typedef abi_ulong target_elf_greg_t
;
102 #define tswapreg(ptr) tswapal(ptr)
106 typedef abi_ushort target_uid_t
;
107 typedef abi_ushort target_gid_t
;
109 typedef abi_uint target_uid_t
;
110 typedef abi_uint target_gid_t
;
112 typedef abi_int target_pid_t
;
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
);
125 elf_platform
[1] = '0' + family
;
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
];
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
)
147 regs
->rsp
= infop
->start_stack
;
148 regs
->rip
= infop
->entry
;
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
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;
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. */
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
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;
255 #define USE_ELF_CORE_DUMP
256 #define ELF_EXEC_PAGESIZE 4096
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 ! */
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
;
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
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,
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 #define TARGET_HAS_VALIDATE_GUEST_SPACE
358 /* Return 1 if the proposed guest space is suitable for the guest.
359 * Return 0 if the proposed guest space isn't suitable, but another
360 * address space should be tried.
361 * Return -1 if there is no way the proposed guest space can be
362 * valid regardless of the base.
363 * The guest code may leave a page mapped and populate it if the
364 * address is suitable.
366 static int validate_guest_space(unsigned long guest_base
,
367 unsigned long guest_size
)
369 unsigned long real_start
, test_page_addr
;
371 /* We need to check that we can force a fault on access to the
372 * commpage at 0xffff0fxx
374 test_page_addr
= guest_base
+ (0xffff0f00 & qemu_host_page_mask
);
376 /* If the commpage lies within the already allocated guest space,
377 * then there is no way we can allocate it.
379 if (test_page_addr
>= guest_base
380 && test_page_addr
< (guest_base
+ guest_size
)) {
384 /* Note it needs to be writeable to let us initialise it */
385 real_start
= (unsigned long)
386 mmap((void *)test_page_addr
, qemu_host_page_size
,
387 PROT_READ
| PROT_WRITE
,
388 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
390 /* If we can't map it then try another address */
391 if (real_start
== -1ul) {
395 if (real_start
!= test_page_addr
) {
396 /* OS didn't put the page where we asked - unmap and reject */
397 munmap((void *)real_start
, qemu_host_page_size
);
401 /* Leave the page mapped
402 * Populate it (mmap should have left it all 0'd)
405 /* Kernel helper versions */
406 __put_user(5, (uint32_t *)g2h(0xffff0ffcul
));
408 /* Now it's populated make it RO */
409 if (mprotect((void *)test_page_addr
, qemu_host_page_size
, PROT_READ
)) {
410 perror("Protecting guest commpage");
414 return 1; /* All good */
417 #define ELF_HWCAP get_elf_hwcap()
418 #define ELF_HWCAP2 get_elf_hwcap2()
420 static uint32_t get_elf_hwcap(void)
422 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
425 hwcaps
|= ARM_HWCAP_ARM_SWP
;
426 hwcaps
|= ARM_HWCAP_ARM_HALF
;
427 hwcaps
|= ARM_HWCAP_ARM_THUMB
;
428 hwcaps
|= ARM_HWCAP_ARM_FAST_MULT
;
430 /* probe for the extra features */
431 #define GET_FEATURE(feat, hwcap) \
432 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
433 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
434 GET_FEATURE(ARM_FEATURE_V5
, ARM_HWCAP_ARM_EDSP
);
435 GET_FEATURE(ARM_FEATURE_VFP
, ARM_HWCAP_ARM_VFP
);
436 GET_FEATURE(ARM_FEATURE_IWMMXT
, ARM_HWCAP_ARM_IWMMXT
);
437 GET_FEATURE(ARM_FEATURE_THUMB2EE
, ARM_HWCAP_ARM_THUMBEE
);
438 GET_FEATURE(ARM_FEATURE_NEON
, ARM_HWCAP_ARM_NEON
);
439 GET_FEATURE(ARM_FEATURE_VFP3
, ARM_HWCAP_ARM_VFPv3
);
440 GET_FEATURE(ARM_FEATURE_V6K
, ARM_HWCAP_ARM_TLS
);
441 GET_FEATURE(ARM_FEATURE_VFP4
, ARM_HWCAP_ARM_VFPv4
);
442 GET_FEATURE(ARM_FEATURE_ARM_DIV
, ARM_HWCAP_ARM_IDIVA
);
443 GET_FEATURE(ARM_FEATURE_THUMB_DIV
, ARM_HWCAP_ARM_IDIVT
);
444 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
445 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
446 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
447 * to our VFP_FP16 feature bit.
449 GET_FEATURE(ARM_FEATURE_VFP3
, ARM_HWCAP_ARM_VFPD32
);
450 GET_FEATURE(ARM_FEATURE_LPAE
, ARM_HWCAP_ARM_LPAE
);
455 static uint32_t get_elf_hwcap2(void)
457 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
460 GET_FEATURE(ARM_FEATURE_V8_AES
, ARM_HWCAP2_ARM_AES
);
461 GET_FEATURE(ARM_FEATURE_V8_PMULL
, ARM_HWCAP2_ARM_PMULL
);
462 GET_FEATURE(ARM_FEATURE_V8_SHA1
, ARM_HWCAP2_ARM_SHA1
);
463 GET_FEATURE(ARM_FEATURE_V8_SHA256
, ARM_HWCAP2_ARM_SHA2
);
464 GET_FEATURE(ARM_FEATURE_CRC
, ARM_HWCAP2_ARM_CRC32
);
471 /* 64 bit ARM definitions */
472 #define ELF_START_MMAP 0x80000000
474 #define ELF_ARCH EM_AARCH64
475 #define ELF_CLASS ELFCLASS64
476 #define ELF_PLATFORM "aarch64"
478 static inline void init_thread(struct target_pt_regs
*regs
,
479 struct image_info
*infop
)
481 abi_long stack
= infop
->start_stack
;
482 memset(regs
, 0, sizeof(*regs
));
484 regs
->pc
= infop
->entry
& ~0x3ULL
;
489 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
491 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
492 const CPUARMState
*env
)
496 for (i
= 0; i
< 32; i
++) {
497 (*regs
)[i
] = tswapreg(env
->xregs
[i
]);
499 (*regs
)[32] = tswapreg(env
->pc
);
500 (*regs
)[33] = tswapreg(pstate_read((CPUARMState
*)env
));
503 #define USE_ELF_CORE_DUMP
504 #define ELF_EXEC_PAGESIZE 4096
507 ARM_HWCAP_A64_FP
= 1 << 0,
508 ARM_HWCAP_A64_ASIMD
= 1 << 1,
509 ARM_HWCAP_A64_EVTSTRM
= 1 << 2,
510 ARM_HWCAP_A64_AES
= 1 << 3,
511 ARM_HWCAP_A64_PMULL
= 1 << 4,
512 ARM_HWCAP_A64_SHA1
= 1 << 5,
513 ARM_HWCAP_A64_SHA2
= 1 << 6,
514 ARM_HWCAP_A64_CRC32
= 1 << 7,
515 ARM_HWCAP_A64_ATOMICS
= 1 << 8,
516 ARM_HWCAP_A64_FPHP
= 1 << 9,
517 ARM_HWCAP_A64_ASIMDHP
= 1 << 10,
518 ARM_HWCAP_A64_CPUID
= 1 << 11,
519 ARM_HWCAP_A64_ASIMDRDM
= 1 << 12,
520 ARM_HWCAP_A64_JSCVT
= 1 << 13,
521 ARM_HWCAP_A64_FCMA
= 1 << 14,
522 ARM_HWCAP_A64_LRCPC
= 1 << 15,
523 ARM_HWCAP_A64_DCPOP
= 1 << 16,
524 ARM_HWCAP_A64_SHA3
= 1 << 17,
525 ARM_HWCAP_A64_SM3
= 1 << 18,
526 ARM_HWCAP_A64_SM4
= 1 << 19,
527 ARM_HWCAP_A64_ASIMDDP
= 1 << 20,
528 ARM_HWCAP_A64_SHA512
= 1 << 21,
529 ARM_HWCAP_A64_SVE
= 1 << 22,
532 #define ELF_HWCAP get_elf_hwcap()
534 static uint32_t get_elf_hwcap(void)
536 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
539 hwcaps
|= ARM_HWCAP_A64_FP
;
540 hwcaps
|= ARM_HWCAP_A64_ASIMD
;
542 /* probe for the extra features */
543 #define GET_FEATURE(feat, hwcap) \
544 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
545 GET_FEATURE(ARM_FEATURE_V8_AES
, ARM_HWCAP_A64_AES
);
546 GET_FEATURE(ARM_FEATURE_V8_PMULL
, ARM_HWCAP_A64_PMULL
);
547 GET_FEATURE(ARM_FEATURE_V8_SHA1
, ARM_HWCAP_A64_SHA1
);
548 GET_FEATURE(ARM_FEATURE_V8_SHA256
, ARM_HWCAP_A64_SHA2
);
549 GET_FEATURE(ARM_FEATURE_CRC
, ARM_HWCAP_A64_CRC32
);
550 GET_FEATURE(ARM_FEATURE_V8_SHA3
, ARM_HWCAP_A64_SHA3
);
551 GET_FEATURE(ARM_FEATURE_V8_SM3
, ARM_HWCAP_A64_SM3
);
552 GET_FEATURE(ARM_FEATURE_V8_SM4
, ARM_HWCAP_A64_SM4
);
553 GET_FEATURE(ARM_FEATURE_V8_SHA512
, ARM_HWCAP_A64_SHA512
);
554 GET_FEATURE(ARM_FEATURE_V8_FP16
,
555 ARM_HWCAP_A64_FPHP
| ARM_HWCAP_A64_ASIMDHP
);
556 GET_FEATURE(ARM_FEATURE_V8_RDM
, ARM_HWCAP_A64_ASIMDRDM
);
557 GET_FEATURE(ARM_FEATURE_V8_FCMA
, ARM_HWCAP_A64_FCMA
);
563 #endif /* not TARGET_AARCH64 */
564 #endif /* TARGET_ARM */
566 #ifdef TARGET_UNICORE32
568 #define ELF_START_MMAP 0x80000000
570 #define ELF_CLASS ELFCLASS32
571 #define ELF_DATA ELFDATA2LSB
572 #define ELF_ARCH EM_UNICORE32
574 static inline void init_thread(struct target_pt_regs
*regs
,
575 struct image_info
*infop
)
577 abi_long stack
= infop
->start_stack
;
578 memset(regs
, 0, sizeof(*regs
));
579 regs
->UC32_REG_asr
= 0x10;
580 regs
->UC32_REG_pc
= infop
->entry
& 0xfffffffe;
581 regs
->UC32_REG_sp
= infop
->start_stack
;
582 /* FIXME - what to for failure of get_user()? */
583 get_user_ual(regs
->UC32_REG_02
, stack
+ 8); /* envp */
584 get_user_ual(regs
->UC32_REG_01
, stack
+ 4); /* envp */
585 /* XXX: it seems that r0 is zeroed after ! */
586 regs
->UC32_REG_00
= 0;
590 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
592 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUUniCore32State
*env
)
594 (*regs
)[0] = env
->regs
[0];
595 (*regs
)[1] = env
->regs
[1];
596 (*regs
)[2] = env
->regs
[2];
597 (*regs
)[3] = env
->regs
[3];
598 (*regs
)[4] = env
->regs
[4];
599 (*regs
)[5] = env
->regs
[5];
600 (*regs
)[6] = env
->regs
[6];
601 (*regs
)[7] = env
->regs
[7];
602 (*regs
)[8] = env
->regs
[8];
603 (*regs
)[9] = env
->regs
[9];
604 (*regs
)[10] = env
->regs
[10];
605 (*regs
)[11] = env
->regs
[11];
606 (*regs
)[12] = env
->regs
[12];
607 (*regs
)[13] = env
->regs
[13];
608 (*regs
)[14] = env
->regs
[14];
609 (*regs
)[15] = env
->regs
[15];
610 (*regs
)[16] = env
->regs
[16];
611 (*regs
)[17] = env
->regs
[17];
612 (*regs
)[18] = env
->regs
[18];
613 (*regs
)[19] = env
->regs
[19];
614 (*regs
)[20] = env
->regs
[20];
615 (*regs
)[21] = env
->regs
[21];
616 (*regs
)[22] = env
->regs
[22];
617 (*regs
)[23] = env
->regs
[23];
618 (*regs
)[24] = env
->regs
[24];
619 (*regs
)[25] = env
->regs
[25];
620 (*regs
)[26] = env
->regs
[26];
621 (*regs
)[27] = env
->regs
[27];
622 (*regs
)[28] = env
->regs
[28];
623 (*regs
)[29] = env
->regs
[29];
624 (*regs
)[30] = env
->regs
[30];
625 (*regs
)[31] = env
->regs
[31];
627 (*regs
)[32] = cpu_asr_read((CPUUniCore32State
*)env
);
628 (*regs
)[33] = env
->regs
[0]; /* XXX */
631 #define USE_ELF_CORE_DUMP
632 #define ELF_EXEC_PAGESIZE 4096
634 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
639 #ifdef TARGET_SPARC64
641 #define ELF_START_MMAP 0x80000000
642 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
643 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
645 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
647 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
650 #define ELF_CLASS ELFCLASS64
651 #define ELF_ARCH EM_SPARCV9
653 #define STACK_BIAS 2047
655 static inline void init_thread(struct target_pt_regs
*regs
,
656 struct image_info
*infop
)
661 regs
->pc
= infop
->entry
;
662 regs
->npc
= regs
->pc
+ 4;
665 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
667 if (personality(infop
->personality
) == PER_LINUX32
)
668 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
670 regs
->u_regs
[14] = infop
->start_stack
- 16 * 8 - STACK_BIAS
;
675 #define ELF_START_MMAP 0x80000000
676 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
677 | HWCAP_SPARC_MULDIV)
679 #define ELF_CLASS ELFCLASS32
680 #define ELF_ARCH EM_SPARC
682 static inline void init_thread(struct target_pt_regs
*regs
,
683 struct image_info
*infop
)
686 regs
->pc
= infop
->entry
;
687 regs
->npc
= regs
->pc
+ 4;
689 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
697 #define ELF_MACHINE PPC_ELF_MACHINE
698 #define ELF_START_MMAP 0x80000000
700 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
702 #define elf_check_arch(x) ( (x) == EM_PPC64 )
704 #define ELF_CLASS ELFCLASS64
708 #define ELF_CLASS ELFCLASS32
712 #define ELF_ARCH EM_PPC
714 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
715 See arch/powerpc/include/asm/cputable.h. */
717 QEMU_PPC_FEATURE_32
= 0x80000000,
718 QEMU_PPC_FEATURE_64
= 0x40000000,
719 QEMU_PPC_FEATURE_601_INSTR
= 0x20000000,
720 QEMU_PPC_FEATURE_HAS_ALTIVEC
= 0x10000000,
721 QEMU_PPC_FEATURE_HAS_FPU
= 0x08000000,
722 QEMU_PPC_FEATURE_HAS_MMU
= 0x04000000,
723 QEMU_PPC_FEATURE_HAS_4xxMAC
= 0x02000000,
724 QEMU_PPC_FEATURE_UNIFIED_CACHE
= 0x01000000,
725 QEMU_PPC_FEATURE_HAS_SPE
= 0x00800000,
726 QEMU_PPC_FEATURE_HAS_EFP_SINGLE
= 0x00400000,
727 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
= 0x00200000,
728 QEMU_PPC_FEATURE_NO_TB
= 0x00100000,
729 QEMU_PPC_FEATURE_POWER4
= 0x00080000,
730 QEMU_PPC_FEATURE_POWER5
= 0x00040000,
731 QEMU_PPC_FEATURE_POWER5_PLUS
= 0x00020000,
732 QEMU_PPC_FEATURE_CELL
= 0x00010000,
733 QEMU_PPC_FEATURE_BOOKE
= 0x00008000,
734 QEMU_PPC_FEATURE_SMT
= 0x00004000,
735 QEMU_PPC_FEATURE_ICACHE_SNOOP
= 0x00002000,
736 QEMU_PPC_FEATURE_ARCH_2_05
= 0x00001000,
737 QEMU_PPC_FEATURE_PA6T
= 0x00000800,
738 QEMU_PPC_FEATURE_HAS_DFP
= 0x00000400,
739 QEMU_PPC_FEATURE_POWER6_EXT
= 0x00000200,
740 QEMU_PPC_FEATURE_ARCH_2_06
= 0x00000100,
741 QEMU_PPC_FEATURE_HAS_VSX
= 0x00000080,
742 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT
= 0x00000040,
744 QEMU_PPC_FEATURE_TRUE_LE
= 0x00000002,
745 QEMU_PPC_FEATURE_PPC_LE
= 0x00000001,
747 /* Feature definitions in AT_HWCAP2. */
748 QEMU_PPC_FEATURE2_ARCH_2_07
= 0x80000000, /* ISA 2.07 */
749 QEMU_PPC_FEATURE2_HAS_HTM
= 0x40000000, /* Hardware Transactional Memory */
750 QEMU_PPC_FEATURE2_HAS_DSCR
= 0x20000000, /* Data Stream Control Register */
751 QEMU_PPC_FEATURE2_HAS_EBB
= 0x10000000, /* Event Base Branching */
752 QEMU_PPC_FEATURE2_HAS_ISEL
= 0x08000000, /* Integer Select */
753 QEMU_PPC_FEATURE2_HAS_TAR
= 0x04000000, /* Target Address Register */
756 #define ELF_HWCAP get_elf_hwcap()
758 static uint32_t get_elf_hwcap(void)
760 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
761 uint32_t features
= 0;
763 /* We don't have to be terribly complete here; the high points are
764 Altivec/FP/SPE support. Anything else is just a bonus. */
765 #define GET_FEATURE(flag, feature) \
766 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
767 #define GET_FEATURE2(flags, feature) \
769 if ((cpu->env.insns_flags2 & flags) == flags) { \
770 features |= feature; \
773 GET_FEATURE(PPC_64B
, QEMU_PPC_FEATURE_64
);
774 GET_FEATURE(PPC_FLOAT
, QEMU_PPC_FEATURE_HAS_FPU
);
775 GET_FEATURE(PPC_ALTIVEC
, QEMU_PPC_FEATURE_HAS_ALTIVEC
);
776 GET_FEATURE(PPC_SPE
, QEMU_PPC_FEATURE_HAS_SPE
);
777 GET_FEATURE(PPC_SPE_SINGLE
, QEMU_PPC_FEATURE_HAS_EFP_SINGLE
);
778 GET_FEATURE(PPC_SPE_DOUBLE
, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
);
779 GET_FEATURE(PPC_BOOKE
, QEMU_PPC_FEATURE_BOOKE
);
780 GET_FEATURE(PPC_405_MAC
, QEMU_PPC_FEATURE_HAS_4xxMAC
);
781 GET_FEATURE2(PPC2_DFP
, QEMU_PPC_FEATURE_HAS_DFP
);
782 GET_FEATURE2(PPC2_VSX
, QEMU_PPC_FEATURE_HAS_VSX
);
783 GET_FEATURE2((PPC2_PERM_ISA206
| PPC2_DIVE_ISA206
| PPC2_ATOMIC_ISA206
|
784 PPC2_FP_CVT_ISA206
| PPC2_FP_TST_ISA206
),
785 QEMU_PPC_FEATURE_ARCH_2_06
);
792 #define ELF_HWCAP2 get_elf_hwcap2()
794 static uint32_t get_elf_hwcap2(void)
796 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
797 uint32_t features
= 0;
799 #define GET_FEATURE(flag, feature) \
800 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
801 #define GET_FEATURE2(flag, feature) \
802 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
804 GET_FEATURE(PPC_ISEL
, QEMU_PPC_FEATURE2_HAS_ISEL
);
805 GET_FEATURE2(PPC2_BCTAR_ISA207
, QEMU_PPC_FEATURE2_HAS_TAR
);
806 GET_FEATURE2((PPC2_BCTAR_ISA207
| PPC2_LSQ_ISA207
| PPC2_ALTIVEC_207
|
807 PPC2_ISA207S
), QEMU_PPC_FEATURE2_ARCH_2_07
);
816 * The requirements here are:
817 * - keep the final alignment of sp (sp & 0xf)
818 * - make sure the 32-bit value at the first 16 byte aligned position of
819 * AUXV is greater than 16 for glibc compatibility.
820 * AT_IGNOREPPC is used for that.
821 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
822 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
824 #define DLINFO_ARCH_ITEMS 5
825 #define ARCH_DLINFO \
827 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
829 * Handle glibc compatibility: these magic entries must \
830 * be at the lowest addresses in the final auxv. \
832 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
833 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
834 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
835 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
836 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
839 static inline void init_thread(struct target_pt_regs
*_regs
, struct image_info
*infop
)
841 _regs
->gpr
[1] = infop
->start_stack
;
842 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
843 if (get_ppc64_abi(infop
) < 2) {
845 get_user_u64(val
, infop
->entry
+ 8);
846 _regs
->gpr
[2] = val
+ infop
->load_bias
;
847 get_user_u64(val
, infop
->entry
);
848 infop
->entry
= val
+ infop
->load_bias
;
850 _regs
->gpr
[12] = infop
->entry
; /* r12 set to global entry address */
853 _regs
->nip
= infop
->entry
;
856 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
858 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
860 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUPPCState
*env
)
863 target_ulong ccr
= 0;
865 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
866 (*regs
)[i
] = tswapreg(env
->gpr
[i
]);
869 (*regs
)[32] = tswapreg(env
->nip
);
870 (*regs
)[33] = tswapreg(env
->msr
);
871 (*regs
)[35] = tswapreg(env
->ctr
);
872 (*regs
)[36] = tswapreg(env
->lr
);
873 (*regs
)[37] = tswapreg(env
->xer
);
875 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
876 ccr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
878 (*regs
)[38] = tswapreg(ccr
);
881 #define USE_ELF_CORE_DUMP
882 #define ELF_EXEC_PAGESIZE 4096
888 #define ELF_START_MMAP 0x80000000
891 #define ELF_CLASS ELFCLASS64
893 #define ELF_CLASS ELFCLASS32
895 #define ELF_ARCH EM_MIPS
897 static inline void init_thread(struct target_pt_regs
*regs
,
898 struct image_info
*infop
)
900 regs
->cp0_status
= 2 << CP0St_KSU
;
901 regs
->cp0_epc
= infop
->entry
;
902 regs
->regs
[29] = infop
->start_stack
;
905 /* See linux kernel: arch/mips/include/asm/elf.h. */
907 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
909 /* See linux kernel: arch/mips/include/asm/reg.h. */
916 TARGET_EF_R26
= TARGET_EF_R0
+ 26,
917 TARGET_EF_R27
= TARGET_EF_R0
+ 27,
918 TARGET_EF_LO
= TARGET_EF_R0
+ 32,
919 TARGET_EF_HI
= TARGET_EF_R0
+ 33,
920 TARGET_EF_CP0_EPC
= TARGET_EF_R0
+ 34,
921 TARGET_EF_CP0_BADVADDR
= TARGET_EF_R0
+ 35,
922 TARGET_EF_CP0_STATUS
= TARGET_EF_R0
+ 36,
923 TARGET_EF_CP0_CAUSE
= TARGET_EF_R0
+ 37
926 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
927 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMIPSState
*env
)
931 for (i
= 0; i
< TARGET_EF_R0
; i
++) {
934 (*regs
)[TARGET_EF_R0
] = 0;
936 for (i
= 1; i
< ARRAY_SIZE(env
->active_tc
.gpr
); i
++) {
937 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->active_tc
.gpr
[i
]);
940 (*regs
)[TARGET_EF_R26
] = 0;
941 (*regs
)[TARGET_EF_R27
] = 0;
942 (*regs
)[TARGET_EF_LO
] = tswapreg(env
->active_tc
.LO
[0]);
943 (*regs
)[TARGET_EF_HI
] = tswapreg(env
->active_tc
.HI
[0]);
944 (*regs
)[TARGET_EF_CP0_EPC
] = tswapreg(env
->active_tc
.PC
);
945 (*regs
)[TARGET_EF_CP0_BADVADDR
] = tswapreg(env
->CP0_BadVAddr
);
946 (*regs
)[TARGET_EF_CP0_STATUS
] = tswapreg(env
->CP0_Status
);
947 (*regs
)[TARGET_EF_CP0_CAUSE
] = tswapreg(env
->CP0_Cause
);
950 #define USE_ELF_CORE_DUMP
951 #define ELF_EXEC_PAGESIZE 4096
953 #endif /* TARGET_MIPS */
955 #ifdef TARGET_MICROBLAZE
957 #define ELF_START_MMAP 0x80000000
959 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
961 #define ELF_CLASS ELFCLASS32
962 #define ELF_ARCH EM_MICROBLAZE
964 static inline void init_thread(struct target_pt_regs
*regs
,
965 struct image_info
*infop
)
967 regs
->pc
= infop
->entry
;
968 regs
->r1
= infop
->start_stack
;
972 #define ELF_EXEC_PAGESIZE 4096
974 #define USE_ELF_CORE_DUMP
976 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
978 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
979 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMBState
*env
)
983 for (i
= 0; i
< 32; i
++) {
984 (*regs
)[pos
++] = tswapreg(env
->regs
[i
]);
987 for (i
= 0; i
< 6; i
++) {
988 (*regs
)[pos
++] = tswapreg(env
->sregs
[i
]);
992 #endif /* TARGET_MICROBLAZE */
996 #define ELF_START_MMAP 0x80000000
998 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1000 #define ELF_CLASS ELFCLASS32
1001 #define ELF_ARCH EM_ALTERA_NIOS2
1003 static void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1005 regs
->ea
= infop
->entry
;
1006 regs
->sp
= infop
->start_stack
;
1007 regs
->estatus
= 0x3;
1010 #define ELF_EXEC_PAGESIZE 4096
1012 #define USE_ELF_CORE_DUMP
1014 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1016 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1017 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1018 const CPUNios2State
*env
)
1023 for (i
= 1; i
< 8; i
++) /* r0-r7 */
1024 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1026 for (i
= 8; i
< 16; i
++) /* r8-r15 */
1027 (*regs
)[i
] = tswapreg(env
->regs
[i
- 8]);
1029 for (i
= 16; i
< 24; i
++) /* r16-r23 */
1030 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1031 (*regs
)[24] = -1; /* R_ET */
1032 (*regs
)[25] = -1; /* R_BT */
1033 (*regs
)[26] = tswapreg(env
->regs
[R_GP
]);
1034 (*regs
)[27] = tswapreg(env
->regs
[R_SP
]);
1035 (*regs
)[28] = tswapreg(env
->regs
[R_FP
]);
1036 (*regs
)[29] = tswapreg(env
->regs
[R_EA
]);
1037 (*regs
)[30] = -1; /* R_SSTATUS */
1038 (*regs
)[31] = tswapreg(env
->regs
[R_RA
]);
1040 (*regs
)[32] = tswapreg(env
->regs
[R_PC
]);
1042 (*regs
)[33] = -1; /* R_STATUS */
1043 (*regs
)[34] = tswapreg(env
->regs
[CR_ESTATUS
]);
1045 for (i
= 35; i
< 49; i
++) /* ... */
1049 #endif /* TARGET_NIOS2 */
1051 #ifdef TARGET_OPENRISC
1053 #define ELF_START_MMAP 0x08000000
1055 #define ELF_ARCH EM_OPENRISC
1056 #define ELF_CLASS ELFCLASS32
1057 #define ELF_DATA ELFDATA2MSB
1059 static inline void init_thread(struct target_pt_regs
*regs
,
1060 struct image_info
*infop
)
1062 regs
->pc
= infop
->entry
;
1063 regs
->gpr
[1] = infop
->start_stack
;
1066 #define USE_ELF_CORE_DUMP
1067 #define ELF_EXEC_PAGESIZE 8192
1069 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1070 #define ELF_NREG 34 /* gprs and pc, sr */
1071 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1073 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1074 const CPUOpenRISCState
*env
)
1078 for (i
= 0; i
< 32; i
++) {
1079 (*regs
)[i
] = tswapreg(cpu_get_gpr(env
, i
));
1081 (*regs
)[32] = tswapreg(env
->pc
);
1082 (*regs
)[33] = tswapreg(cpu_get_sr(env
));
1085 #define ELF_PLATFORM NULL
1087 #endif /* TARGET_OPENRISC */
1091 #define ELF_START_MMAP 0x80000000
1093 #define ELF_CLASS ELFCLASS32
1094 #define ELF_ARCH EM_SH
1096 static inline void init_thread(struct target_pt_regs
*regs
,
1097 struct image_info
*infop
)
1099 /* Check other registers XXXXX */
1100 regs
->pc
= infop
->entry
;
1101 regs
->regs
[15] = infop
->start_stack
;
1104 /* See linux kernel: arch/sh/include/asm/elf.h. */
1106 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1108 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1113 TARGET_REG_GBR
= 19,
1114 TARGET_REG_MACH
= 20,
1115 TARGET_REG_MACL
= 21,
1116 TARGET_REG_SYSCALL
= 22
1119 static inline void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1120 const CPUSH4State
*env
)
1124 for (i
= 0; i
< 16; i
++) {
1125 (*regs
)[i
] = tswapreg(env
->gregs
[i
]);
1128 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1129 (*regs
)[TARGET_REG_PR
] = tswapreg(env
->pr
);
1130 (*regs
)[TARGET_REG_SR
] = tswapreg(env
->sr
);
1131 (*regs
)[TARGET_REG_GBR
] = tswapreg(env
->gbr
);
1132 (*regs
)[TARGET_REG_MACH
] = tswapreg(env
->mach
);
1133 (*regs
)[TARGET_REG_MACL
] = tswapreg(env
->macl
);
1134 (*regs
)[TARGET_REG_SYSCALL
] = 0; /* FIXME */
1137 #define USE_ELF_CORE_DUMP
1138 #define ELF_EXEC_PAGESIZE 4096
1141 SH_CPU_HAS_FPU
= 0x0001, /* Hardware FPU support */
1142 SH_CPU_HAS_P2_FLUSH_BUG
= 0x0002, /* Need to flush the cache in P2 area */
1143 SH_CPU_HAS_MMU_PAGE_ASSOC
= 0x0004, /* SH3: TLB way selection bit support */
1144 SH_CPU_HAS_DSP
= 0x0008, /* SH-DSP: DSP support */
1145 SH_CPU_HAS_PERF_COUNTER
= 0x0010, /* Hardware performance counters */
1146 SH_CPU_HAS_PTEA
= 0x0020, /* PTEA register */
1147 SH_CPU_HAS_LLSC
= 0x0040, /* movli.l/movco.l */
1148 SH_CPU_HAS_L2_CACHE
= 0x0080, /* Secondary cache / URAM */
1149 SH_CPU_HAS_OP32
= 0x0100, /* 32-bit instruction support */
1150 SH_CPU_HAS_PTEAEX
= 0x0200, /* PTE ASID Extension support */
1153 #define ELF_HWCAP get_elf_hwcap()
1155 static uint32_t get_elf_hwcap(void)
1157 SuperHCPU
*cpu
= SUPERH_CPU(thread_cpu
);
1160 hwcap
|= SH_CPU_HAS_FPU
;
1162 if (cpu
->env
.features
& SH_FEATURE_SH4A
) {
1163 hwcap
|= SH_CPU_HAS_LLSC
;
1173 #define ELF_START_MMAP 0x80000000
1175 #define ELF_CLASS ELFCLASS32
1176 #define ELF_ARCH EM_CRIS
1178 static inline void init_thread(struct target_pt_regs
*regs
,
1179 struct image_info
*infop
)
1181 regs
->erp
= infop
->entry
;
1184 #define ELF_EXEC_PAGESIZE 8192
1190 #define ELF_START_MMAP 0x80000000
1192 #define ELF_CLASS ELFCLASS32
1193 #define ELF_ARCH EM_68K
1195 /* ??? Does this need to do anything?
1196 #define ELF_PLAT_INIT(_r) */
1198 static inline void init_thread(struct target_pt_regs
*regs
,
1199 struct image_info
*infop
)
1201 regs
->usp
= infop
->start_stack
;
1203 regs
->pc
= infop
->entry
;
1206 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1208 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1210 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUM68KState
*env
)
1212 (*regs
)[0] = tswapreg(env
->dregs
[1]);
1213 (*regs
)[1] = tswapreg(env
->dregs
[2]);
1214 (*regs
)[2] = tswapreg(env
->dregs
[3]);
1215 (*regs
)[3] = tswapreg(env
->dregs
[4]);
1216 (*regs
)[4] = tswapreg(env
->dregs
[5]);
1217 (*regs
)[5] = tswapreg(env
->dregs
[6]);
1218 (*regs
)[6] = tswapreg(env
->dregs
[7]);
1219 (*regs
)[7] = tswapreg(env
->aregs
[0]);
1220 (*regs
)[8] = tswapreg(env
->aregs
[1]);
1221 (*regs
)[9] = tswapreg(env
->aregs
[2]);
1222 (*regs
)[10] = tswapreg(env
->aregs
[3]);
1223 (*regs
)[11] = tswapreg(env
->aregs
[4]);
1224 (*regs
)[12] = tswapreg(env
->aregs
[5]);
1225 (*regs
)[13] = tswapreg(env
->aregs
[6]);
1226 (*regs
)[14] = tswapreg(env
->dregs
[0]);
1227 (*regs
)[15] = tswapreg(env
->aregs
[7]);
1228 (*regs
)[16] = tswapreg(env
->dregs
[0]); /* FIXME: orig_d0 */
1229 (*regs
)[17] = tswapreg(env
->sr
);
1230 (*regs
)[18] = tswapreg(env
->pc
);
1231 (*regs
)[19] = 0; /* FIXME: regs->format | regs->vector */
1234 #define USE_ELF_CORE_DUMP
1235 #define ELF_EXEC_PAGESIZE 8192
1241 #define ELF_START_MMAP (0x30000000000ULL)
1243 #define ELF_CLASS ELFCLASS64
1244 #define ELF_ARCH EM_ALPHA
1246 static inline void init_thread(struct target_pt_regs
*regs
,
1247 struct image_info
*infop
)
1249 regs
->pc
= infop
->entry
;
1251 regs
->usp
= infop
->start_stack
;
1254 #define ELF_EXEC_PAGESIZE 8192
1256 #endif /* TARGET_ALPHA */
1260 #define ELF_START_MMAP (0x20000000000ULL)
1262 #define ELF_CLASS ELFCLASS64
1263 #define ELF_DATA ELFDATA2MSB
1264 #define ELF_ARCH EM_S390
1266 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1268 regs
->psw
.addr
= infop
->entry
;
1269 regs
->psw
.mask
= PSW_MASK_64
| PSW_MASK_32
;
1270 regs
->gprs
[15] = infop
->start_stack
;
1273 #endif /* TARGET_S390X */
1275 #ifdef TARGET_TILEGX
1277 /* 42 bits real used address, a half for user mode */
1278 #define ELF_START_MMAP (0x00000020000000000ULL)
1280 #define elf_check_arch(x) ((x) == EM_TILEGX)
1282 #define ELF_CLASS ELFCLASS64
1283 #define ELF_DATA ELFDATA2LSB
1284 #define ELF_ARCH EM_TILEGX
1286 static inline void init_thread(struct target_pt_regs
*regs
,
1287 struct image_info
*infop
)
1289 regs
->pc
= infop
->entry
;
1290 regs
->sp
= infop
->start_stack
;
1294 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1296 #endif /* TARGET_TILEGX */
1300 #define ELF_START_MMAP 0x80000000
1301 #define ELF_ARCH EM_RISCV
1303 #ifdef TARGET_RISCV32
1304 #define ELF_CLASS ELFCLASS32
1306 #define ELF_CLASS ELFCLASS64
1309 static inline void init_thread(struct target_pt_regs
*regs
,
1310 struct image_info
*infop
)
1312 regs
->sepc
= infop
->entry
;
1313 regs
->sp
= infop
->start_stack
;
1316 #define ELF_EXEC_PAGESIZE 4096
1318 #endif /* TARGET_RISCV */
1322 #define ELF_START_MMAP 0x80000000
1323 #define ELF_CLASS ELFCLASS32
1324 #define ELF_ARCH EM_PARISC
1325 #define ELF_PLATFORM "PARISC"
1326 #define STACK_GROWS_DOWN 0
1327 #define STACK_ALIGNMENT 64
1329 static inline void init_thread(struct target_pt_regs
*regs
,
1330 struct image_info
*infop
)
1332 regs
->iaoq
[0] = infop
->entry
;
1333 regs
->iaoq
[1] = infop
->entry
+ 4;
1335 regs
->gr
[24] = infop
->arg_start
;
1336 regs
->gr
[25] = (infop
->arg_end
- infop
->arg_start
) / sizeof(abi_ulong
);
1337 /* The top-of-stack contains a linkage buffer. */
1338 regs
->gr
[30] = infop
->start_stack
+ 64;
1339 regs
->gr
[31] = infop
->entry
;
1342 #endif /* TARGET_HPPA */
1344 #ifdef TARGET_XTENSA
1346 #define ELF_START_MMAP 0x20000000
1348 #define ELF_CLASS ELFCLASS32
1349 #define ELF_ARCH EM_XTENSA
1351 static inline void init_thread(struct target_pt_regs
*regs
,
1352 struct image_info
*infop
)
1354 regs
->windowbase
= 0;
1355 regs
->windowstart
= 1;
1356 regs
->areg
[1] = infop
->start_stack
;
1357 regs
->pc
= infop
->entry
;
1360 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1361 #define ELF_NREG 128
1362 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1371 TARGET_REG_WINDOWSTART
,
1372 TARGET_REG_WINDOWBASE
,
1373 TARGET_REG_THREADPTR
,
1374 TARGET_REG_AR0
= 64,
1377 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1378 const CPUXtensaState
*env
)
1382 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1383 (*regs
)[TARGET_REG_PS
] = tswapreg(env
->sregs
[PS
] & ~PS_EXCM
);
1384 (*regs
)[TARGET_REG_LBEG
] = tswapreg(env
->sregs
[LBEG
]);
1385 (*regs
)[TARGET_REG_LEND
] = tswapreg(env
->sregs
[LEND
]);
1386 (*regs
)[TARGET_REG_LCOUNT
] = tswapreg(env
->sregs
[LCOUNT
]);
1387 (*regs
)[TARGET_REG_SAR
] = tswapreg(env
->sregs
[SAR
]);
1388 (*regs
)[TARGET_REG_WINDOWSTART
] = tswapreg(env
->sregs
[WINDOW_START
]);
1389 (*regs
)[TARGET_REG_WINDOWBASE
] = tswapreg(env
->sregs
[WINDOW_BASE
]);
1390 (*regs
)[TARGET_REG_THREADPTR
] = tswapreg(env
->uregs
[THREADPTR
]);
1391 xtensa_sync_phys_from_window((CPUXtensaState
*)env
);
1392 for (i
= 0; i
< env
->config
->nareg
; ++i
) {
1393 (*regs
)[TARGET_REG_AR0
+ i
] = tswapreg(env
->phys_regs
[i
]);
1397 #define USE_ELF_CORE_DUMP
1398 #define ELF_EXEC_PAGESIZE 4096
1400 #endif /* TARGET_XTENSA */
1402 #ifndef ELF_PLATFORM
1403 #define ELF_PLATFORM (NULL)
1407 #define ELF_MACHINE ELF_ARCH
1410 #ifndef elf_check_arch
1411 #define elf_check_arch(x) ((x) == ELF_ARCH)
1418 #ifndef STACK_GROWS_DOWN
1419 #define STACK_GROWS_DOWN 1
1422 #ifndef STACK_ALIGNMENT
1423 #define STACK_ALIGNMENT 16
1428 #define ELF_CLASS ELFCLASS32
1430 #define bswaptls(ptr) bswap32s(ptr)
1437 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
1438 unsigned int a_text
; /* length of text, in bytes */
1439 unsigned int a_data
; /* length of data, in bytes */
1440 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
1441 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
1442 unsigned int a_entry
; /* start address */
1443 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
1444 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
1448 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1454 /* Necessary parameters */
1455 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1456 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1457 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1458 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1460 #define DLINFO_ITEMS 15
1462 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
1464 memcpy(to
, from
, n
);
1468 static void bswap_ehdr(struct elfhdr
*ehdr
)
1470 bswap16s(&ehdr
->e_type
); /* Object file type */
1471 bswap16s(&ehdr
->e_machine
); /* Architecture */
1472 bswap32s(&ehdr
->e_version
); /* Object file version */
1473 bswaptls(&ehdr
->e_entry
); /* Entry point virtual address */
1474 bswaptls(&ehdr
->e_phoff
); /* Program header table file offset */
1475 bswaptls(&ehdr
->e_shoff
); /* Section header table file offset */
1476 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
1477 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
1478 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
1479 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
1480 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
1481 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
1482 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
1485 static void bswap_phdr(struct elf_phdr
*phdr
, int phnum
)
1488 for (i
= 0; i
< phnum
; ++i
, ++phdr
) {
1489 bswap32s(&phdr
->p_type
); /* Segment type */
1490 bswap32s(&phdr
->p_flags
); /* Segment flags */
1491 bswaptls(&phdr
->p_offset
); /* Segment file offset */
1492 bswaptls(&phdr
->p_vaddr
); /* Segment virtual address */
1493 bswaptls(&phdr
->p_paddr
); /* Segment physical address */
1494 bswaptls(&phdr
->p_filesz
); /* Segment size in file */
1495 bswaptls(&phdr
->p_memsz
); /* Segment size in memory */
1496 bswaptls(&phdr
->p_align
); /* Segment alignment */
1500 static void bswap_shdr(struct elf_shdr
*shdr
, int shnum
)
1503 for (i
= 0; i
< shnum
; ++i
, ++shdr
) {
1504 bswap32s(&shdr
->sh_name
);
1505 bswap32s(&shdr
->sh_type
);
1506 bswaptls(&shdr
->sh_flags
);
1507 bswaptls(&shdr
->sh_addr
);
1508 bswaptls(&shdr
->sh_offset
);
1509 bswaptls(&shdr
->sh_size
);
1510 bswap32s(&shdr
->sh_link
);
1511 bswap32s(&shdr
->sh_info
);
1512 bswaptls(&shdr
->sh_addralign
);
1513 bswaptls(&shdr
->sh_entsize
);
1517 static void bswap_sym(struct elf_sym
*sym
)
1519 bswap32s(&sym
->st_name
);
1520 bswaptls(&sym
->st_value
);
1521 bswaptls(&sym
->st_size
);
1522 bswap16s(&sym
->st_shndx
);
1525 static inline void bswap_ehdr(struct elfhdr
*ehdr
) { }
1526 static inline void bswap_phdr(struct elf_phdr
*phdr
, int phnum
) { }
1527 static inline void bswap_shdr(struct elf_shdr
*shdr
, int shnum
) { }
1528 static inline void bswap_sym(struct elf_sym
*sym
) { }
1531 #ifdef USE_ELF_CORE_DUMP
1532 static int elf_core_dump(int, const CPUArchState
*);
1533 #endif /* USE_ELF_CORE_DUMP */
1534 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
);
1536 /* Verify the portions of EHDR within E_IDENT for the target.
1537 This can be performed before bswapping the entire header. */
1538 static bool elf_check_ident(struct elfhdr
*ehdr
)
1540 return (ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
1541 && ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
1542 && ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
1543 && ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
1544 && ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
1545 && ehdr
->e_ident
[EI_DATA
] == ELF_DATA
1546 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
);
1549 /* Verify the portions of EHDR outside of E_IDENT for the target.
1550 This has to wait until after bswapping the header. */
1551 static bool elf_check_ehdr(struct elfhdr
*ehdr
)
1553 return (elf_check_arch(ehdr
->e_machine
)
1554 && ehdr
->e_ehsize
== sizeof(struct elfhdr
)
1555 && ehdr
->e_phentsize
== sizeof(struct elf_phdr
)
1556 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
));
1560 * 'copy_elf_strings()' copies argument/envelope strings from user
1561 * memory to free pages in kernel mem. These are in a format ready
1562 * to be put directly into the top of new user memory.
1565 static abi_ulong
copy_elf_strings(int argc
, char **argv
, char *scratch
,
1566 abi_ulong p
, abi_ulong stack_limit
)
1573 return 0; /* bullet-proofing */
1576 if (STACK_GROWS_DOWN
) {
1577 int offset
= ((p
- 1) % TARGET_PAGE_SIZE
) + 1;
1578 for (i
= argc
- 1; i
>= 0; --i
) {
1581 fprintf(stderr
, "VFS: argc is wrong");
1584 len
= strlen(tmp
) + 1;
1587 if (len
> (p
- stack_limit
)) {
1591 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
1592 tmp
-= bytes_to_copy
;
1594 offset
-= bytes_to_copy
;
1595 len
-= bytes_to_copy
;
1597 memcpy_fromfs(scratch
+ offset
, tmp
, bytes_to_copy
);
1600 memcpy_to_target(p
, scratch
, top
- p
);
1602 offset
= TARGET_PAGE_SIZE
;
1607 memcpy_to_target(p
, scratch
+ offset
, top
- p
);
1610 int remaining
= TARGET_PAGE_SIZE
- (p
% TARGET_PAGE_SIZE
);
1611 for (i
= 0; i
< argc
; ++i
) {
1614 fprintf(stderr
, "VFS: argc is wrong");
1617 len
= strlen(tmp
) + 1;
1618 if (len
> (stack_limit
- p
)) {
1622 int bytes_to_copy
= (len
> remaining
) ? remaining
: len
;
1624 memcpy_fromfs(scratch
+ (p
- top
), tmp
, bytes_to_copy
);
1626 tmp
+= bytes_to_copy
;
1627 remaining
-= bytes_to_copy
;
1629 len
-= bytes_to_copy
;
1631 if (remaining
== 0) {
1632 memcpy_to_target(top
, scratch
, p
- top
);
1634 remaining
= TARGET_PAGE_SIZE
;
1639 memcpy_to_target(top
, scratch
, p
- top
);
1646 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1647 * argument/environment space. Newer kernels (>2.6.33) allow more,
1648 * dependent on stack size, but guarantee at least 32 pages for
1649 * backwards compatibility.
1651 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1653 static abi_ulong
setup_arg_pages(struct linux_binprm
*bprm
,
1654 struct image_info
*info
)
1656 abi_ulong size
, error
, guard
;
1658 size
= guest_stack_size
;
1659 if (size
< STACK_LOWER_LIMIT
) {
1660 size
= STACK_LOWER_LIMIT
;
1662 guard
= TARGET_PAGE_SIZE
;
1663 if (guard
< qemu_real_host_page_size
) {
1664 guard
= qemu_real_host_page_size
;
1667 error
= target_mmap(0, size
+ guard
, PROT_READ
| PROT_WRITE
,
1668 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1670 perror("mmap stack");
1674 /* We reserve one extra page at the top of the stack as guard. */
1675 if (STACK_GROWS_DOWN
) {
1676 target_mprotect(error
, guard
, PROT_NONE
);
1677 info
->stack_limit
= error
+ guard
;
1678 return info
->stack_limit
+ size
- sizeof(void *);
1680 target_mprotect(error
+ size
, guard
, PROT_NONE
);
1681 info
->stack_limit
= error
+ size
;
1686 /* Map and zero the bss. We need to explicitly zero any fractional pages
1687 after the data section (i.e. bss). */
1688 static void zero_bss(abi_ulong elf_bss
, abi_ulong last_bss
, int prot
)
1690 uintptr_t host_start
, host_map_start
, host_end
;
1692 last_bss
= TARGET_PAGE_ALIGN(last_bss
);
1694 /* ??? There is confusion between qemu_real_host_page_size and
1695 qemu_host_page_size here and elsewhere in target_mmap, which
1696 may lead to the end of the data section mapping from the file
1697 not being mapped. At least there was an explicit test and
1698 comment for that here, suggesting that "the file size must
1699 be known". The comment probably pre-dates the introduction
1700 of the fstat system call in target_mmap which does in fact
1701 find out the size. What isn't clear is if the workaround
1702 here is still actually needed. For now, continue with it,
1703 but merge it with the "normal" mmap that would allocate the bss. */
1705 host_start
= (uintptr_t) g2h(elf_bss
);
1706 host_end
= (uintptr_t) g2h(last_bss
);
1707 host_map_start
= REAL_HOST_PAGE_ALIGN(host_start
);
1709 if (host_map_start
< host_end
) {
1710 void *p
= mmap((void *)host_map_start
, host_end
- host_map_start
,
1711 prot
, MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1712 if (p
== MAP_FAILED
) {
1713 perror("cannot mmap brk");
1718 /* Ensure that the bss page(s) are valid */
1719 if ((page_get_flags(last_bss
-1) & prot
) != prot
) {
1720 page_set_flags(elf_bss
& TARGET_PAGE_MASK
, last_bss
, prot
| PAGE_VALID
);
1723 if (host_start
< host_map_start
) {
1724 memset((void *)host_start
, 0, host_map_start
- host_start
);
1728 #ifdef CONFIG_USE_FDPIC
1729 static abi_ulong
loader_build_fdpic_loadmap(struct image_info
*info
, abi_ulong sp
)
1732 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
;
1734 /* elf32_fdpic_loadseg */
1738 put_user_u32(loadsegs
[n
].addr
, sp
+0);
1739 put_user_u32(loadsegs
[n
].p_vaddr
, sp
+4);
1740 put_user_u32(loadsegs
[n
].p_memsz
, sp
+8);
1743 /* elf32_fdpic_loadmap */
1745 put_user_u16(0, sp
+0); /* version */
1746 put_user_u16(info
->nsegs
, sp
+2); /* nsegs */
1748 info
->personality
= PER_LINUX_FDPIC
;
1749 info
->loadmap_addr
= sp
;
1755 static abi_ulong
create_elf_tables(abi_ulong p
, int argc
, int envc
,
1756 struct elfhdr
*exec
,
1757 struct image_info
*info
,
1758 struct image_info
*interp_info
)
1761 abi_ulong u_argc
, u_argv
, u_envp
, u_auxv
;
1764 abi_ulong u_rand_bytes
;
1765 uint8_t k_rand_bytes
[16];
1766 abi_ulong u_platform
;
1767 const char *k_platform
;
1768 const int n
= sizeof(elf_addr_t
);
1772 #ifdef CONFIG_USE_FDPIC
1773 /* Needs to be before we load the env/argc/... */
1774 if (elf_is_fdpic(exec
)) {
1775 /* Need 4 byte alignment for these structs */
1777 sp
= loader_build_fdpic_loadmap(info
, sp
);
1778 info
->other_info
= interp_info
;
1780 interp_info
->other_info
= info
;
1781 sp
= loader_build_fdpic_loadmap(interp_info
, sp
);
1787 k_platform
= ELF_PLATFORM
;
1789 size_t len
= strlen(k_platform
) + 1;
1790 if (STACK_GROWS_DOWN
) {
1791 sp
-= (len
+ n
- 1) & ~(n
- 1);
1793 /* FIXME - check return value of memcpy_to_target() for failure */
1794 memcpy_to_target(sp
, k_platform
, len
);
1796 memcpy_to_target(sp
, k_platform
, len
);
1802 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1803 * the argv and envp pointers.
1805 if (STACK_GROWS_DOWN
) {
1806 sp
= QEMU_ALIGN_DOWN(sp
, 16);
1808 sp
= QEMU_ALIGN_UP(sp
, 16);
1812 * Generate 16 random bytes for userspace PRNG seeding (not
1813 * cryptically secure but it's not the aim of QEMU).
1815 for (i
= 0; i
< 16; i
++) {
1816 k_rand_bytes
[i
] = rand();
1818 if (STACK_GROWS_DOWN
) {
1821 /* FIXME - check return value of memcpy_to_target() for failure */
1822 memcpy_to_target(sp
, k_rand_bytes
, 16);
1824 memcpy_to_target(sp
, k_rand_bytes
, 16);
1829 size
= (DLINFO_ITEMS
+ 1) * 2;
1832 #ifdef DLINFO_ARCH_ITEMS
1833 size
+= DLINFO_ARCH_ITEMS
* 2;
1838 info
->auxv_len
= size
* n
;
1840 size
+= envc
+ argc
+ 2;
1841 size
+= 1; /* argc itself */
1844 /* Allocate space and finalize stack alignment for entry now. */
1845 if (STACK_GROWS_DOWN
) {
1846 u_argc
= QEMU_ALIGN_DOWN(sp
- size
, STACK_ALIGNMENT
);
1850 sp
= QEMU_ALIGN_UP(sp
+ size
, STACK_ALIGNMENT
);
1853 u_argv
= u_argc
+ n
;
1854 u_envp
= u_argv
+ (argc
+ 1) * n
;
1855 u_auxv
= u_envp
+ (envc
+ 1) * n
;
1856 info
->saved_auxv
= u_auxv
;
1857 info
->arg_start
= u_argv
;
1858 info
->arg_end
= u_argv
+ argc
* n
;
1860 /* This is correct because Linux defines
1861 * elf_addr_t as Elf32_Off / Elf64_Off
1863 #define NEW_AUX_ENT(id, val) do { \
1864 put_user_ual(id, u_auxv); u_auxv += n; \
1865 put_user_ual(val, u_auxv); u_auxv += n; \
1870 * ARCH_DLINFO must come first so platform specific code can enforce
1871 * special alignment requirements on the AUXV if necessary (eg. PPC).
1875 /* There must be exactly DLINFO_ITEMS entries here, or the assert
1876 * on info->auxv_len will trigger.
1878 NEW_AUX_ENT(AT_PHDR
, (abi_ulong
)(info
->load_addr
+ exec
->e_phoff
));
1879 NEW_AUX_ENT(AT_PHENT
, (abi_ulong
)(sizeof (struct elf_phdr
)));
1880 NEW_AUX_ENT(AT_PHNUM
, (abi_ulong
)(exec
->e_phnum
));
1881 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(MAX(TARGET_PAGE_SIZE
, getpagesize())));
1882 NEW_AUX_ENT(AT_BASE
, (abi_ulong
)(interp_info
? interp_info
->load_addr
: 0));
1883 NEW_AUX_ENT(AT_FLAGS
, (abi_ulong
)0);
1884 NEW_AUX_ENT(AT_ENTRY
, info
->entry
);
1885 NEW_AUX_ENT(AT_UID
, (abi_ulong
) getuid());
1886 NEW_AUX_ENT(AT_EUID
, (abi_ulong
) geteuid());
1887 NEW_AUX_ENT(AT_GID
, (abi_ulong
) getgid());
1888 NEW_AUX_ENT(AT_EGID
, (abi_ulong
) getegid());
1889 NEW_AUX_ENT(AT_HWCAP
, (abi_ulong
) ELF_HWCAP
);
1890 NEW_AUX_ENT(AT_CLKTCK
, (abi_ulong
) sysconf(_SC_CLK_TCK
));
1891 NEW_AUX_ENT(AT_RANDOM
, (abi_ulong
) u_rand_bytes
);
1892 NEW_AUX_ENT(AT_SECURE
, (abi_ulong
) qemu_getauxval(AT_SECURE
));
1895 NEW_AUX_ENT(AT_HWCAP2
, (abi_ulong
) ELF_HWCAP2
);
1899 NEW_AUX_ENT(AT_PLATFORM
, u_platform
);
1901 NEW_AUX_ENT (AT_NULL
, 0);
1904 /* Check that our initial calculation of the auxv length matches how much
1905 * we actually put into it.
1907 assert(info
->auxv_len
== u_auxv
- info
->saved_auxv
);
1909 put_user_ual(argc
, u_argc
);
1911 p
= info
->arg_strings
;
1912 for (i
= 0; i
< argc
; ++i
) {
1913 put_user_ual(p
, u_argv
);
1915 p
+= target_strlen(p
) + 1;
1917 put_user_ual(0, u_argv
);
1919 p
= info
->env_strings
;
1920 for (i
= 0; i
< envc
; ++i
) {
1921 put_user_ual(p
, u_envp
);
1923 p
+= target_strlen(p
) + 1;
1925 put_user_ual(0, u_envp
);
1930 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1931 /* If the guest doesn't have a validation function just agree */
1932 static int validate_guest_space(unsigned long guest_base
,
1933 unsigned long guest_size
)
1939 unsigned long init_guest_space(unsigned long host_start
,
1940 unsigned long host_size
,
1941 unsigned long guest_start
,
1944 unsigned long current_start
, real_start
;
1947 assert(host_start
|| host_size
);
1949 /* If just a starting address is given, then just verify that
1951 if (host_start
&& !host_size
) {
1952 if (validate_guest_space(host_start
, host_size
) == 1) {
1955 return (unsigned long)-1;
1959 /* Setup the initial flags and start address. */
1960 current_start
= host_start
& qemu_host_page_mask
;
1961 flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
;
1966 /* Otherwise, a non-zero size region of memory needs to be mapped
1969 unsigned long real_size
= host_size
;
1971 /* Do not use mmap_find_vma here because that is limited to the
1972 * guest address space. We are going to make the
1973 * guest address space fit whatever we're given.
1975 real_start
= (unsigned long)
1976 mmap((void *)current_start
, host_size
, PROT_NONE
, flags
, -1, 0);
1977 if (real_start
== (unsigned long)-1) {
1978 return (unsigned long)-1;
1981 /* Ensure the address is properly aligned. */
1982 if (real_start
& ~qemu_host_page_mask
) {
1983 munmap((void *)real_start
, host_size
);
1984 real_size
= host_size
+ qemu_host_page_size
;
1985 real_start
= (unsigned long)
1986 mmap((void *)real_start
, real_size
, PROT_NONE
, flags
, -1, 0);
1987 if (real_start
== (unsigned long)-1) {
1988 return (unsigned long)-1;
1990 real_start
= HOST_PAGE_ALIGN(real_start
);
1993 /* Check to see if the address is valid. */
1994 if (!host_start
|| real_start
== current_start
) {
1995 int valid
= validate_guest_space(real_start
- guest_start
,
1999 } else if (valid
== -1) {
2000 return (unsigned long)-1;
2002 /* valid == 0, so try again. */
2005 /* That address didn't work. Unmap and try a different one.
2006 * The address the host picked because is typically right at
2007 * the top of the host address space and leaves the guest with
2008 * no usable address space. Resort to a linear search. We
2009 * already compensated for mmap_min_addr, so this should not
2010 * happen often. Probably means we got unlucky and host
2011 * address space randomization put a shared library somewhere
2014 munmap((void *)real_start
, host_size
);
2015 current_start
+= qemu_host_page_size
;
2016 if (host_start
== current_start
) {
2017 /* Theoretically possible if host doesn't have any suitably
2018 * aligned areas. Normally the first mmap will fail.
2020 return (unsigned long)-1;
2024 qemu_log_mask(CPU_LOG_PAGE
, "Reserved 0x%lx bytes of guest address space\n", host_size
);
2029 static void probe_guest_base(const char *image_name
,
2030 abi_ulong loaddr
, abi_ulong hiaddr
)
2032 /* Probe for a suitable guest base address, if the user has not set
2033 * it explicitly, and set guest_base appropriately.
2034 * In case of error we will print a suitable message and exit.
2037 if (!have_guest_base
&& !reserved_va
) {
2038 unsigned long host_start
, real_start
, host_size
;
2040 /* Round addresses to page boundaries. */
2041 loaddr
&= qemu_host_page_mask
;
2042 hiaddr
= HOST_PAGE_ALIGN(hiaddr
);
2044 if (loaddr
< mmap_min_addr
) {
2045 host_start
= HOST_PAGE_ALIGN(mmap_min_addr
);
2047 host_start
= loaddr
;
2048 if (host_start
!= loaddr
) {
2049 errmsg
= "Address overflow loading ELF binary";
2053 host_size
= hiaddr
- loaddr
;
2055 /* Setup the initial guest memory space with ranges gleaned from
2056 * the ELF image that is being loaded.
2058 real_start
= init_guest_space(host_start
, host_size
, loaddr
, false);
2059 if (real_start
== (unsigned long)-1) {
2060 errmsg
= "Unable to find space for application";
2063 guest_base
= real_start
- loaddr
;
2065 qemu_log_mask(CPU_LOG_PAGE
, "Relocating guest address space from 0x"
2066 TARGET_ABI_FMT_lx
" to 0x%lx\n",
2067 loaddr
, real_start
);
2072 fprintf(stderr
, "%s: %s\n", image_name
, errmsg
);
2077 /* Load an ELF image into the address space.
2079 IMAGE_NAME is the filename of the image, to use in error messages.
2080 IMAGE_FD is the open file descriptor for the image.
2082 BPRM_BUF is a copy of the beginning of the file; this of course
2083 contains the elf file header at offset 0. It is assumed that this
2084 buffer is sufficiently aligned to present no problems to the host
2085 in accessing data at aligned offsets within the buffer.
2087 On return: INFO values will be filled in, as necessary or available. */
2089 static void load_elf_image(const char *image_name
, int image_fd
,
2090 struct image_info
*info
, char **pinterp_name
,
2091 char bprm_buf
[BPRM_BUF_SIZE
])
2093 struct elfhdr
*ehdr
= (struct elfhdr
*)bprm_buf
;
2094 struct elf_phdr
*phdr
;
2095 abi_ulong load_addr
, load_bias
, loaddr
, hiaddr
, error
;
2099 /* First of all, some simple consistency checks */
2100 errmsg
= "Invalid ELF image for this architecture";
2101 if (!elf_check_ident(ehdr
)) {
2105 if (!elf_check_ehdr(ehdr
)) {
2109 i
= ehdr
->e_phnum
* sizeof(struct elf_phdr
);
2110 if (ehdr
->e_phoff
+ i
<= BPRM_BUF_SIZE
) {
2111 phdr
= (struct elf_phdr
*)(bprm_buf
+ ehdr
->e_phoff
);
2113 phdr
= (struct elf_phdr
*) alloca(i
);
2114 retval
= pread(image_fd
, phdr
, i
, ehdr
->e_phoff
);
2119 bswap_phdr(phdr
, ehdr
->e_phnum
);
2121 #ifdef CONFIG_USE_FDPIC
2123 info
->pt_dynamic_addr
= 0;
2128 /* Find the maximum size of the image and allocate an appropriate
2129 amount of memory to handle that. */
2130 loaddr
= -1, hiaddr
= 0;
2131 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
2132 if (phdr
[i
].p_type
== PT_LOAD
) {
2133 abi_ulong a
= phdr
[i
].p_vaddr
- phdr
[i
].p_offset
;
2137 a
= phdr
[i
].p_vaddr
+ phdr
[i
].p_memsz
;
2141 #ifdef CONFIG_USE_FDPIC
2148 if (ehdr
->e_type
== ET_DYN
) {
2149 /* The image indicates that it can be loaded anywhere. Find a
2150 location that can hold the memory space required. If the
2151 image is pre-linked, LOADDR will be non-zero. Since we do
2152 not supply MAP_FIXED here we'll use that address if and
2153 only if it remains available. */
2154 load_addr
= target_mmap(loaddr
, hiaddr
- loaddr
, PROT_NONE
,
2155 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
,
2157 if (load_addr
== -1) {
2160 } else if (pinterp_name
!= NULL
) {
2161 /* This is the main executable. Make sure that the low
2162 address does not conflict with MMAP_MIN_ADDR or the
2163 QEMU application itself. */
2164 probe_guest_base(image_name
, loaddr
, hiaddr
);
2166 load_bias
= load_addr
- loaddr
;
2168 #ifdef CONFIG_USE_FDPIC
2170 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
=
2171 g_malloc(sizeof(*loadsegs
) * info
->nsegs
);
2173 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
2174 switch (phdr
[i
].p_type
) {
2176 info
->pt_dynamic_addr
= phdr
[i
].p_vaddr
+ load_bias
;
2179 loadsegs
->addr
= phdr
[i
].p_vaddr
+ load_bias
;
2180 loadsegs
->p_vaddr
= phdr
[i
].p_vaddr
;
2181 loadsegs
->p_memsz
= phdr
[i
].p_memsz
;
2189 info
->load_bias
= load_bias
;
2190 info
->load_addr
= load_addr
;
2191 info
->entry
= ehdr
->e_entry
+ load_bias
;
2192 info
->start_code
= -1;
2194 info
->start_data
= -1;
2197 info
->elf_flags
= ehdr
->e_flags
;
2199 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
2200 struct elf_phdr
*eppnt
= phdr
+ i
;
2201 if (eppnt
->p_type
== PT_LOAD
) {
2202 abi_ulong vaddr
, vaddr_po
, vaddr_ps
, vaddr_ef
, vaddr_em
;
2205 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
2206 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
2207 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
2209 vaddr
= load_bias
+ eppnt
->p_vaddr
;
2210 vaddr_po
= TARGET_ELF_PAGEOFFSET(vaddr
);
2211 vaddr_ps
= TARGET_ELF_PAGESTART(vaddr
);
2213 error
= target_mmap(vaddr_ps
, eppnt
->p_filesz
+ vaddr_po
,
2214 elf_prot
, MAP_PRIVATE
| MAP_FIXED
,
2215 image_fd
, eppnt
->p_offset
- vaddr_po
);
2220 vaddr_ef
= vaddr
+ eppnt
->p_filesz
;
2221 vaddr_em
= vaddr
+ eppnt
->p_memsz
;
2223 /* If the load segment requests extra zeros (e.g. bss), map it. */
2224 if (vaddr_ef
< vaddr_em
) {
2225 zero_bss(vaddr_ef
, vaddr_em
, elf_prot
);
2228 /* Find the full program boundaries. */
2229 if (elf_prot
& PROT_EXEC
) {
2230 if (vaddr
< info
->start_code
) {
2231 info
->start_code
= vaddr
;
2233 if (vaddr_ef
> info
->end_code
) {
2234 info
->end_code
= vaddr_ef
;
2237 if (elf_prot
& PROT_WRITE
) {
2238 if (vaddr
< info
->start_data
) {
2239 info
->start_data
= vaddr
;
2241 if (vaddr_ef
> info
->end_data
) {
2242 info
->end_data
= vaddr_ef
;
2244 if (vaddr_em
> info
->brk
) {
2245 info
->brk
= vaddr_em
;
2248 } else if (eppnt
->p_type
== PT_INTERP
&& pinterp_name
) {
2251 if (*pinterp_name
) {
2252 errmsg
= "Multiple PT_INTERP entries";
2255 interp_name
= malloc(eppnt
->p_filesz
);
2260 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
2261 memcpy(interp_name
, bprm_buf
+ eppnt
->p_offset
,
2264 retval
= pread(image_fd
, interp_name
, eppnt
->p_filesz
,
2266 if (retval
!= eppnt
->p_filesz
) {
2270 if (interp_name
[eppnt
->p_filesz
- 1] != 0) {
2271 errmsg
= "Invalid PT_INTERP entry";
2274 *pinterp_name
= interp_name
;
2278 if (info
->end_data
== 0) {
2279 info
->start_data
= info
->end_code
;
2280 info
->end_data
= info
->end_code
;
2281 info
->brk
= info
->end_code
;
2284 if (qemu_log_enabled()) {
2285 load_symbols(ehdr
, image_fd
, load_bias
);
2295 errmsg
= "Incomplete read of file header";
2299 errmsg
= strerror(errno
);
2301 fprintf(stderr
, "%s: %s\n", image_name
, errmsg
);
2305 static void load_elf_interp(const char *filename
, struct image_info
*info
,
2306 char bprm_buf
[BPRM_BUF_SIZE
])
2310 fd
= open(path(filename
), O_RDONLY
);
2315 retval
= read(fd
, bprm_buf
, BPRM_BUF_SIZE
);
2319 if (retval
< BPRM_BUF_SIZE
) {
2320 memset(bprm_buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
2323 load_elf_image(filename
, fd
, info
, NULL
, bprm_buf
);
2327 fprintf(stderr
, "%s: %s\n", filename
, strerror(errno
));
2331 static int symfind(const void *s0
, const void *s1
)
2333 target_ulong addr
= *(target_ulong
*)s0
;
2334 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
2336 if (addr
< sym
->st_value
) {
2338 } else if (addr
>= sym
->st_value
+ sym
->st_size
) {
2344 static const char *lookup_symbolxx(struct syminfo
*s
, target_ulong orig_addr
)
2346 #if ELF_CLASS == ELFCLASS32
2347 struct elf_sym
*syms
= s
->disas_symtab
.elf32
;
2349 struct elf_sym
*syms
= s
->disas_symtab
.elf64
;
2353 struct elf_sym
*sym
;
2355 sym
= bsearch(&orig_addr
, syms
, s
->disas_num_syms
, sizeof(*syms
), symfind
);
2357 return s
->disas_strtab
+ sym
->st_name
;
2363 /* FIXME: This should use elf_ops.h */
2364 static int symcmp(const void *s0
, const void *s1
)
2366 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
2367 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
2368 return (sym0
->st_value
< sym1
->st_value
)
2370 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
2373 /* Best attempt to load symbols from this ELF object. */
2374 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
)
2376 int i
, shnum
, nsyms
, sym_idx
= 0, str_idx
= 0;
2378 struct elf_shdr
*shdr
;
2379 char *strings
= NULL
;
2380 struct syminfo
*s
= NULL
;
2381 struct elf_sym
*new_syms
, *syms
= NULL
;
2383 shnum
= hdr
->e_shnum
;
2384 i
= shnum
* sizeof(struct elf_shdr
);
2385 shdr
= (struct elf_shdr
*)alloca(i
);
2386 if (pread(fd
, shdr
, i
, hdr
->e_shoff
) != i
) {
2390 bswap_shdr(shdr
, shnum
);
2391 for (i
= 0; i
< shnum
; ++i
) {
2392 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
2394 str_idx
= shdr
[i
].sh_link
;
2399 /* There will be no symbol table if the file was stripped. */
2403 /* Now know where the strtab and symtab are. Snarf them. */
2404 s
= g_try_new(struct syminfo
, 1);
2409 segsz
= shdr
[str_idx
].sh_size
;
2410 s
->disas_strtab
= strings
= g_try_malloc(segsz
);
2412 pread(fd
, strings
, segsz
, shdr
[str_idx
].sh_offset
) != segsz
) {
2416 segsz
= shdr
[sym_idx
].sh_size
;
2417 syms
= g_try_malloc(segsz
);
2418 if (!syms
|| pread(fd
, syms
, segsz
, shdr
[sym_idx
].sh_offset
) != segsz
) {
2422 if (segsz
/ sizeof(struct elf_sym
) > INT_MAX
) {
2423 /* Implausibly large symbol table: give up rather than ploughing
2424 * on with the number of symbols calculation overflowing
2428 nsyms
= segsz
/ sizeof(struct elf_sym
);
2429 for (i
= 0; i
< nsyms
; ) {
2430 bswap_sym(syms
+ i
);
2431 /* Throw away entries which we do not need. */
2432 if (syms
[i
].st_shndx
== SHN_UNDEF
2433 || syms
[i
].st_shndx
>= SHN_LORESERVE
2434 || ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
2436 syms
[i
] = syms
[nsyms
];
2439 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2440 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2441 syms
[i
].st_value
&= ~(target_ulong
)1;
2443 syms
[i
].st_value
+= load_bias
;
2448 /* No "useful" symbol. */
2453 /* Attempt to free the storage associated with the local symbols
2454 that we threw away. Whether or not this has any effect on the
2455 memory allocation depends on the malloc implementation and how
2456 many symbols we managed to discard. */
2457 new_syms
= g_try_renew(struct elf_sym
, syms
, nsyms
);
2458 if (new_syms
== NULL
) {
2463 qsort(syms
, nsyms
, sizeof(*syms
), symcmp
);
2465 s
->disas_num_syms
= nsyms
;
2466 #if ELF_CLASS == ELFCLASS32
2467 s
->disas_symtab
.elf32
= syms
;
2469 s
->disas_symtab
.elf64
= syms
;
2471 s
->lookup_symbol
= lookup_symbolxx
;
2483 uint32_t get_elf_eflags(int fd
)
2489 /* Read ELF header */
2490 offset
= lseek(fd
, 0, SEEK_SET
);
2491 if (offset
== (off_t
) -1) {
2494 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
2495 if (ret
< sizeof(ehdr
)) {
2498 offset
= lseek(fd
, offset
, SEEK_SET
);
2499 if (offset
== (off_t
) -1) {
2503 /* Check ELF signature */
2504 if (!elf_check_ident(&ehdr
)) {
2510 if (!elf_check_ehdr(&ehdr
)) {
2514 /* return architecture id */
2515 return ehdr
.e_flags
;
2518 int load_elf_binary(struct linux_binprm
*bprm
, struct image_info
*info
)
2520 struct image_info interp_info
;
2521 struct elfhdr elf_ex
;
2522 char *elf_interpreter
= NULL
;
2525 info
->start_mmap
= (abi_ulong
)ELF_START_MMAP
;
2527 load_elf_image(bprm
->filename
, bprm
->fd
, info
,
2528 &elf_interpreter
, bprm
->buf
);
2530 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2531 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2532 when we load the interpreter. */
2533 elf_ex
= *(struct elfhdr
*)bprm
->buf
;
2535 /* Do this so that we can load the interpreter, if need be. We will
2536 change some of these later */
2537 bprm
->p
= setup_arg_pages(bprm
, info
);
2539 scratch
= g_new0(char, TARGET_PAGE_SIZE
);
2540 if (STACK_GROWS_DOWN
) {
2541 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
2542 bprm
->p
, info
->stack_limit
);
2543 info
->file_string
= bprm
->p
;
2544 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
2545 bprm
->p
, info
->stack_limit
);
2546 info
->env_strings
= bprm
->p
;
2547 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
2548 bprm
->p
, info
->stack_limit
);
2549 info
->arg_strings
= bprm
->p
;
2551 info
->arg_strings
= bprm
->p
;
2552 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
2553 bprm
->p
, info
->stack_limit
);
2554 info
->env_strings
= bprm
->p
;
2555 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
2556 bprm
->p
, info
->stack_limit
);
2557 info
->file_string
= bprm
->p
;
2558 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
2559 bprm
->p
, info
->stack_limit
);
2565 fprintf(stderr
, "%s: %s\n", bprm
->filename
, strerror(E2BIG
));
2569 if (elf_interpreter
) {
2570 load_elf_interp(elf_interpreter
, &interp_info
, bprm
->buf
);
2572 /* If the program interpreter is one of these two, then assume
2573 an iBCS2 image. Otherwise assume a native linux image. */
2575 if (strcmp(elf_interpreter
, "/usr/lib/libc.so.1") == 0
2576 || strcmp(elf_interpreter
, "/usr/lib/ld.so.1") == 0) {
2577 info
->personality
= PER_SVR4
;
2579 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2580 and some applications "depend" upon this behavior. Since
2581 we do not have the power to recompile these, we emulate
2582 the SVr4 behavior. Sigh. */
2583 target_mmap(0, qemu_host_page_size
, PROT_READ
| PROT_EXEC
,
2584 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2588 bprm
->p
= create_elf_tables(bprm
->p
, bprm
->argc
, bprm
->envc
, &elf_ex
,
2589 info
, (elf_interpreter
? &interp_info
: NULL
));
2590 info
->start_stack
= bprm
->p
;
2592 /* If we have an interpreter, set that as the program's entry point.
2593 Copy the load_bias as well, to help PPC64 interpret the entry
2594 point as a function descriptor. Do this after creating elf tables
2595 so that we copy the original program entry point into the AUXV. */
2596 if (elf_interpreter
) {
2597 info
->load_bias
= interp_info
.load_bias
;
2598 info
->entry
= interp_info
.entry
;
2599 free(elf_interpreter
);
2602 #ifdef USE_ELF_CORE_DUMP
2603 bprm
->core_dump
= &elf_core_dump
;
2609 #ifdef USE_ELF_CORE_DUMP
2611 * Definitions to generate Intel SVR4-like core files.
2612 * These mostly have the same names as the SVR4 types with "target_elf_"
2613 * tacked on the front to prevent clashes with linux definitions,
2614 * and the typedef forms have been avoided. This is mostly like
2615 * the SVR4 structure, but more Linuxy, with things that Linux does
2616 * not support and which gdb doesn't really use excluded.
2618 * Fields we don't dump (their contents is zero) in linux-user qemu
2619 * are marked with XXX.
2621 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2623 * Porting ELF coredump for target is (quite) simple process. First you
2624 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2625 * the target resides):
2627 * #define USE_ELF_CORE_DUMP
2629 * Next you define type of register set used for dumping. ELF specification
2630 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2632 * typedef <target_regtype> target_elf_greg_t;
2633 * #define ELF_NREG <number of registers>
2634 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2636 * Last step is to implement target specific function that copies registers
2637 * from given cpu into just specified register set. Prototype is:
2639 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2640 * const CPUArchState *env);
2643 * regs - copy register values into here (allocated and zeroed by caller)
2644 * env - copy registers from here
2646 * Example for ARM target is provided in this file.
2649 /* An ELF note in memory */
2653 size_t namesz_rounded
;
2656 size_t datasz_rounded
;
2661 struct target_elf_siginfo
{
2662 abi_int si_signo
; /* signal number */
2663 abi_int si_code
; /* extra code */
2664 abi_int si_errno
; /* errno */
2667 struct target_elf_prstatus
{
2668 struct target_elf_siginfo pr_info
; /* Info associated with signal */
2669 abi_short pr_cursig
; /* Current signal */
2670 abi_ulong pr_sigpend
; /* XXX */
2671 abi_ulong pr_sighold
; /* XXX */
2672 target_pid_t pr_pid
;
2673 target_pid_t pr_ppid
;
2674 target_pid_t pr_pgrp
;
2675 target_pid_t pr_sid
;
2676 struct target_timeval pr_utime
; /* XXX User time */
2677 struct target_timeval pr_stime
; /* XXX System time */
2678 struct target_timeval pr_cutime
; /* XXX Cumulative user time */
2679 struct target_timeval pr_cstime
; /* XXX Cumulative system time */
2680 target_elf_gregset_t pr_reg
; /* GP registers */
2681 abi_int pr_fpvalid
; /* XXX */
2684 #define ELF_PRARGSZ (80) /* Number of chars for args */
2686 struct target_elf_prpsinfo
{
2687 char pr_state
; /* numeric process state */
2688 char pr_sname
; /* char for pr_state */
2689 char pr_zomb
; /* zombie */
2690 char pr_nice
; /* nice val */
2691 abi_ulong pr_flag
; /* flags */
2692 target_uid_t pr_uid
;
2693 target_gid_t pr_gid
;
2694 target_pid_t pr_pid
, pr_ppid
, pr_pgrp
, pr_sid
;
2696 char pr_fname
[16]; /* filename of executable */
2697 char pr_psargs
[ELF_PRARGSZ
]; /* initial part of arg list */
2700 /* Here is the structure in which status of each thread is captured. */
2701 struct elf_thread_status
{
2702 QTAILQ_ENTRY(elf_thread_status
) ets_link
;
2703 struct target_elf_prstatus prstatus
; /* NT_PRSTATUS */
2705 elf_fpregset_t fpu
; /* NT_PRFPREG */
2706 struct task_struct
*thread
;
2707 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
2709 struct memelfnote notes
[1];
2713 struct elf_note_info
{
2714 struct memelfnote
*notes
;
2715 struct target_elf_prstatus
*prstatus
; /* NT_PRSTATUS */
2716 struct target_elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
2718 QTAILQ_HEAD(thread_list_head
, elf_thread_status
) thread_list
;
2721 * Current version of ELF coredump doesn't support
2722 * dumping fp regs etc.
2724 elf_fpregset_t
*fpu
;
2725 elf_fpxregset_t
*xfpu
;
2726 int thread_status_size
;
2732 struct vm_area_struct
{
2733 target_ulong vma_start
; /* start vaddr of memory region */
2734 target_ulong vma_end
; /* end vaddr of memory region */
2735 abi_ulong vma_flags
; /* protection etc. flags for the region */
2736 QTAILQ_ENTRY(vm_area_struct
) vma_link
;
2740 QTAILQ_HEAD(, vm_area_struct
) mm_mmap
;
2741 int mm_count
; /* number of mappings */
2744 static struct mm_struct
*vma_init(void);
2745 static void vma_delete(struct mm_struct
*);
2746 static int vma_add_mapping(struct mm_struct
*, target_ulong
,
2747 target_ulong
, abi_ulong
);
2748 static int vma_get_mapping_count(const struct mm_struct
*);
2749 static struct vm_area_struct
*vma_first(const struct mm_struct
*);
2750 static struct vm_area_struct
*vma_next(struct vm_area_struct
*);
2751 static abi_ulong
vma_dump_size(const struct vm_area_struct
*);
2752 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
2753 unsigned long flags
);
2755 static void fill_elf_header(struct elfhdr
*, int, uint16_t, uint32_t);
2756 static void fill_note(struct memelfnote
*, const char *, int,
2757 unsigned int, void *);
2758 static void fill_prstatus(struct target_elf_prstatus
*, const TaskState
*, int);
2759 static int fill_psinfo(struct target_elf_prpsinfo
*, const TaskState
*);
2760 static void fill_auxv_note(struct memelfnote
*, const TaskState
*);
2761 static void fill_elf_note_phdr(struct elf_phdr
*, int, off_t
);
2762 static size_t note_size(const struct memelfnote
*);
2763 static void free_note_info(struct elf_note_info
*);
2764 static int fill_note_info(struct elf_note_info
*, long, const CPUArchState
*);
2765 static void fill_thread_info(struct elf_note_info
*, const CPUArchState
*);
2766 static int core_dump_filename(const TaskState
*, char *, size_t);
2768 static int dump_write(int, const void *, size_t);
2769 static int write_note(struct memelfnote
*, int);
2770 static int write_note_info(struct elf_note_info
*, int);
2773 static void bswap_prstatus(struct target_elf_prstatus
*prstatus
)
2775 prstatus
->pr_info
.si_signo
= tswap32(prstatus
->pr_info
.si_signo
);
2776 prstatus
->pr_info
.si_code
= tswap32(prstatus
->pr_info
.si_code
);
2777 prstatus
->pr_info
.si_errno
= tswap32(prstatus
->pr_info
.si_errno
);
2778 prstatus
->pr_cursig
= tswap16(prstatus
->pr_cursig
);
2779 prstatus
->pr_sigpend
= tswapal(prstatus
->pr_sigpend
);
2780 prstatus
->pr_sighold
= tswapal(prstatus
->pr_sighold
);
2781 prstatus
->pr_pid
= tswap32(prstatus
->pr_pid
);
2782 prstatus
->pr_ppid
= tswap32(prstatus
->pr_ppid
);
2783 prstatus
->pr_pgrp
= tswap32(prstatus
->pr_pgrp
);
2784 prstatus
->pr_sid
= tswap32(prstatus
->pr_sid
);
2785 /* cpu times are not filled, so we skip them */
2786 /* regs should be in correct format already */
2787 prstatus
->pr_fpvalid
= tswap32(prstatus
->pr_fpvalid
);
2790 static void bswap_psinfo(struct target_elf_prpsinfo
*psinfo
)
2792 psinfo
->pr_flag
= tswapal(psinfo
->pr_flag
);
2793 psinfo
->pr_uid
= tswap16(psinfo
->pr_uid
);
2794 psinfo
->pr_gid
= tswap16(psinfo
->pr_gid
);
2795 psinfo
->pr_pid
= tswap32(psinfo
->pr_pid
);
2796 psinfo
->pr_ppid
= tswap32(psinfo
->pr_ppid
);
2797 psinfo
->pr_pgrp
= tswap32(psinfo
->pr_pgrp
);
2798 psinfo
->pr_sid
= tswap32(psinfo
->pr_sid
);
2801 static void bswap_note(struct elf_note
*en
)
2803 bswap32s(&en
->n_namesz
);
2804 bswap32s(&en
->n_descsz
);
2805 bswap32s(&en
->n_type
);
2808 static inline void bswap_prstatus(struct target_elf_prstatus
*p
) { }
2809 static inline void bswap_psinfo(struct target_elf_prpsinfo
*p
) {}
2810 static inline void bswap_note(struct elf_note
*en
) { }
2811 #endif /* BSWAP_NEEDED */
2814 * Minimal support for linux memory regions. These are needed
2815 * when we are finding out what memory exactly belongs to
2816 * emulated process. No locks needed here, as long as
2817 * thread that received the signal is stopped.
2820 static struct mm_struct
*vma_init(void)
2822 struct mm_struct
*mm
;
2824 if ((mm
= g_malloc(sizeof (*mm
))) == NULL
)
2828 QTAILQ_INIT(&mm
->mm_mmap
);
2833 static void vma_delete(struct mm_struct
*mm
)
2835 struct vm_area_struct
*vma
;
2837 while ((vma
= vma_first(mm
)) != NULL
) {
2838 QTAILQ_REMOVE(&mm
->mm_mmap
, vma
, vma_link
);
2844 static int vma_add_mapping(struct mm_struct
*mm
, target_ulong start
,
2845 target_ulong end
, abi_ulong flags
)
2847 struct vm_area_struct
*vma
;
2849 if ((vma
= g_malloc0(sizeof (*vma
))) == NULL
)
2852 vma
->vma_start
= start
;
2854 vma
->vma_flags
= flags
;
2856 QTAILQ_INSERT_TAIL(&mm
->mm_mmap
, vma
, vma_link
);
2862 static struct vm_area_struct
*vma_first(const struct mm_struct
*mm
)
2864 return (QTAILQ_FIRST(&mm
->mm_mmap
));
2867 static struct vm_area_struct
*vma_next(struct vm_area_struct
*vma
)
2869 return (QTAILQ_NEXT(vma
, vma_link
));
2872 static int vma_get_mapping_count(const struct mm_struct
*mm
)
2874 return (mm
->mm_count
);
2878 * Calculate file (dump) size of given memory region.
2880 static abi_ulong
vma_dump_size(const struct vm_area_struct
*vma
)
2882 /* if we cannot even read the first page, skip it */
2883 if (!access_ok(VERIFY_READ
, vma
->vma_start
, TARGET_PAGE_SIZE
))
2887 * Usually we don't dump executable pages as they contain
2888 * non-writable code that debugger can read directly from
2889 * target library etc. However, thread stacks are marked
2890 * also executable so we read in first page of given region
2891 * and check whether it contains elf header. If there is
2892 * no elf header, we dump it.
2894 if (vma
->vma_flags
& PROT_EXEC
) {
2895 char page
[TARGET_PAGE_SIZE
];
2897 copy_from_user(page
, vma
->vma_start
, sizeof (page
));
2898 if ((page
[EI_MAG0
] == ELFMAG0
) &&
2899 (page
[EI_MAG1
] == ELFMAG1
) &&
2900 (page
[EI_MAG2
] == ELFMAG2
) &&
2901 (page
[EI_MAG3
] == ELFMAG3
)) {
2903 * Mappings are possibly from ELF binary. Don't dump
2910 return (vma
->vma_end
- vma
->vma_start
);
2913 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
2914 unsigned long flags
)
2916 struct mm_struct
*mm
= (struct mm_struct
*)priv
;
2918 vma_add_mapping(mm
, start
, end
, flags
);
2922 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
2923 unsigned int sz
, void *data
)
2925 unsigned int namesz
;
2927 namesz
= strlen(name
) + 1;
2929 note
->namesz
= namesz
;
2930 note
->namesz_rounded
= roundup(namesz
, sizeof (int32_t));
2933 note
->datasz_rounded
= roundup(sz
, sizeof (int32_t));
2938 * We calculate rounded up note size here as specified by
2941 note
->notesz
= sizeof (struct elf_note
) +
2942 note
->namesz_rounded
+ note
->datasz_rounded
;
2945 static void fill_elf_header(struct elfhdr
*elf
, int segs
, uint16_t machine
,
2948 (void) memset(elf
, 0, sizeof(*elf
));
2950 (void) memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
2951 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
2952 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
2953 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
2954 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
2956 elf
->e_type
= ET_CORE
;
2957 elf
->e_machine
= machine
;
2958 elf
->e_version
= EV_CURRENT
;
2959 elf
->e_phoff
= sizeof(struct elfhdr
);
2960 elf
->e_flags
= flags
;
2961 elf
->e_ehsize
= sizeof(struct elfhdr
);
2962 elf
->e_phentsize
= sizeof(struct elf_phdr
);
2963 elf
->e_phnum
= segs
;
2968 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
2970 phdr
->p_type
= PT_NOTE
;
2971 phdr
->p_offset
= offset
;
2974 phdr
->p_filesz
= sz
;
2979 bswap_phdr(phdr
, 1);
2982 static size_t note_size(const struct memelfnote
*note
)
2984 return (note
->notesz
);
2987 static void fill_prstatus(struct target_elf_prstatus
*prstatus
,
2988 const TaskState
*ts
, int signr
)
2990 (void) memset(prstatus
, 0, sizeof (*prstatus
));
2991 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
2992 prstatus
->pr_pid
= ts
->ts_tid
;
2993 prstatus
->pr_ppid
= getppid();
2994 prstatus
->pr_pgrp
= getpgrp();
2995 prstatus
->pr_sid
= getsid(0);
2997 bswap_prstatus(prstatus
);
3000 static int fill_psinfo(struct target_elf_prpsinfo
*psinfo
, const TaskState
*ts
)
3002 char *base_filename
;
3003 unsigned int i
, len
;
3005 (void) memset(psinfo
, 0, sizeof (*psinfo
));
3007 len
= ts
->info
->arg_end
- ts
->info
->arg_start
;
3008 if (len
>= ELF_PRARGSZ
)
3009 len
= ELF_PRARGSZ
- 1;
3010 if (copy_from_user(&psinfo
->pr_psargs
, ts
->info
->arg_start
, len
))
3012 for (i
= 0; i
< len
; i
++)
3013 if (psinfo
->pr_psargs
[i
] == 0)
3014 psinfo
->pr_psargs
[i
] = ' ';
3015 psinfo
->pr_psargs
[len
] = 0;
3017 psinfo
->pr_pid
= getpid();
3018 psinfo
->pr_ppid
= getppid();
3019 psinfo
->pr_pgrp
= getpgrp();
3020 psinfo
->pr_sid
= getsid(0);
3021 psinfo
->pr_uid
= getuid();
3022 psinfo
->pr_gid
= getgid();
3024 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
3026 * Using strncpy here is fine: at max-length,
3027 * this field is not NUL-terminated.
3029 (void) strncpy(psinfo
->pr_fname
, base_filename
,
3030 sizeof(psinfo
->pr_fname
));
3032 g_free(base_filename
);
3033 bswap_psinfo(psinfo
);
3037 static void fill_auxv_note(struct memelfnote
*note
, const TaskState
*ts
)
3039 elf_addr_t auxv
= (elf_addr_t
)ts
->info
->saved_auxv
;
3040 elf_addr_t orig_auxv
= auxv
;
3042 int len
= ts
->info
->auxv_len
;
3045 * Auxiliary vector is stored in target process stack. It contains
3046 * {type, value} pairs that we need to dump into note. This is not
3047 * strictly necessary but we do it here for sake of completeness.
3050 /* read in whole auxv vector and copy it to memelfnote */
3051 ptr
= lock_user(VERIFY_READ
, orig_auxv
, len
, 0);
3053 fill_note(note
, "CORE", NT_AUXV
, len
, ptr
);
3054 unlock_user(ptr
, auxv
, len
);
3059 * Constructs name of coredump file. We have following convention
3061 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3063 * Returns 0 in case of success, -1 otherwise (errno is set).
3065 static int core_dump_filename(const TaskState
*ts
, char *buf
,
3069 char *base_filename
= NULL
;
3073 assert(bufsize
>= PATH_MAX
);
3075 if (gettimeofday(&tv
, NULL
) < 0) {
3076 (void) fprintf(stderr
, "unable to get current timestamp: %s",
3081 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
3082 (void) strftime(timestamp
, sizeof (timestamp
), "%Y%m%d-%H%M%S",
3083 localtime_r(&tv
.tv_sec
, &tm
));
3084 (void) snprintf(buf
, bufsize
, "qemu_%s_%s_%d.core",
3085 base_filename
, timestamp
, (int)getpid());
3086 g_free(base_filename
);
3091 static int dump_write(int fd
, const void *ptr
, size_t size
)
3093 const char *bufp
= (const char *)ptr
;
3094 ssize_t bytes_written
, bytes_left
;
3095 struct rlimit dumpsize
;
3099 getrlimit(RLIMIT_CORE
, &dumpsize
);
3100 if ((pos
= lseek(fd
, 0, SEEK_CUR
))==-1) {
3101 if (errno
== ESPIPE
) { /* not a seekable stream */
3107 if (dumpsize
.rlim_cur
<= pos
) {
3109 } else if (dumpsize
.rlim_cur
== RLIM_INFINITY
) {
3112 size_t limit_left
=dumpsize
.rlim_cur
- pos
;
3113 bytes_left
= limit_left
>= size
? size
: limit_left
;
3118 * In normal conditions, single write(2) should do but
3119 * in case of socket etc. this mechanism is more portable.
3122 bytes_written
= write(fd
, bufp
, bytes_left
);
3123 if (bytes_written
< 0) {
3127 } else if (bytes_written
== 0) { /* eof */
3130 bufp
+= bytes_written
;
3131 bytes_left
-= bytes_written
;
3132 } while (bytes_left
> 0);
3137 static int write_note(struct memelfnote
*men
, int fd
)
3141 en
.n_namesz
= men
->namesz
;
3142 en
.n_type
= men
->type
;
3143 en
.n_descsz
= men
->datasz
;
3147 if (dump_write(fd
, &en
, sizeof(en
)) != 0)
3149 if (dump_write(fd
, men
->name
, men
->namesz_rounded
) != 0)
3151 if (dump_write(fd
, men
->data
, men
->datasz_rounded
) != 0)
3157 static void fill_thread_info(struct elf_note_info
*info
, const CPUArchState
*env
)
3159 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)env
);
3160 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
3161 struct elf_thread_status
*ets
;
3163 ets
= g_malloc0(sizeof (*ets
));
3164 ets
->num_notes
= 1; /* only prstatus is dumped */
3165 fill_prstatus(&ets
->prstatus
, ts
, 0);
3166 elf_core_copy_regs(&ets
->prstatus
.pr_reg
, env
);
3167 fill_note(&ets
->notes
[0], "CORE", NT_PRSTATUS
, sizeof (ets
->prstatus
),
3170 QTAILQ_INSERT_TAIL(&info
->thread_list
, ets
, ets_link
);
3172 info
->notes_size
+= note_size(&ets
->notes
[0]);
3175 static void init_note_info(struct elf_note_info
*info
)
3177 /* Initialize the elf_note_info structure so that it is at
3178 * least safe to call free_note_info() on it. Must be
3179 * called before calling fill_note_info().
3181 memset(info
, 0, sizeof (*info
));
3182 QTAILQ_INIT(&info
->thread_list
);
3185 static int fill_note_info(struct elf_note_info
*info
,
3186 long signr
, const CPUArchState
*env
)
3189 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)env
);
3190 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
3193 info
->notes
= g_new0(struct memelfnote
, NUMNOTES
);
3194 if (info
->notes
== NULL
)
3196 info
->prstatus
= g_malloc0(sizeof (*info
->prstatus
));
3197 if (info
->prstatus
== NULL
)
3199 info
->psinfo
= g_malloc0(sizeof (*info
->psinfo
));
3200 if (info
->prstatus
== NULL
)
3204 * First fill in status (and registers) of current thread
3205 * including process info & aux vector.
3207 fill_prstatus(info
->prstatus
, ts
, signr
);
3208 elf_core_copy_regs(&info
->prstatus
->pr_reg
, env
);
3209 fill_note(&info
->notes
[0], "CORE", NT_PRSTATUS
,
3210 sizeof (*info
->prstatus
), info
->prstatus
);
3211 fill_psinfo(info
->psinfo
, ts
);
3212 fill_note(&info
->notes
[1], "CORE", NT_PRPSINFO
,
3213 sizeof (*info
->psinfo
), info
->psinfo
);
3214 fill_auxv_note(&info
->notes
[2], ts
);
3217 info
->notes_size
= 0;
3218 for (i
= 0; i
< info
->numnote
; i
++)
3219 info
->notes_size
+= note_size(&info
->notes
[i
]);
3221 /* read and fill status of all threads */
3224 if (cpu
== thread_cpu
) {
3227 fill_thread_info(info
, (CPUArchState
*)cpu
->env_ptr
);
3234 static void free_note_info(struct elf_note_info
*info
)
3236 struct elf_thread_status
*ets
;
3238 while (!QTAILQ_EMPTY(&info
->thread_list
)) {
3239 ets
= QTAILQ_FIRST(&info
->thread_list
);
3240 QTAILQ_REMOVE(&info
->thread_list
, ets
, ets_link
);
3244 g_free(info
->prstatus
);
3245 g_free(info
->psinfo
);
3246 g_free(info
->notes
);
3249 static int write_note_info(struct elf_note_info
*info
, int fd
)
3251 struct elf_thread_status
*ets
;
3254 /* write prstatus, psinfo and auxv for current thread */
3255 for (i
= 0; i
< info
->numnote
; i
++)
3256 if ((error
= write_note(&info
->notes
[i
], fd
)) != 0)
3259 /* write prstatus for each thread */
3260 QTAILQ_FOREACH(ets
, &info
->thread_list
, ets_link
) {
3261 if ((error
= write_note(&ets
->notes
[0], fd
)) != 0)
3269 * Write out ELF coredump.
3271 * See documentation of ELF object file format in:
3272 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3274 * Coredump format in linux is following:
3276 * 0 +----------------------+ \
3277 * | ELF header | ET_CORE |
3278 * +----------------------+ |
3279 * | ELF program headers | |--- headers
3280 * | - NOTE section | |
3281 * | - PT_LOAD sections | |
3282 * +----------------------+ /
3287 * +----------------------+ <-- aligned to target page
3288 * | Process memory dump |
3293 * +----------------------+
3295 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3296 * NT_PRSINFO -> struct elf_prpsinfo
3297 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3299 * Format follows System V format as close as possible. Current
3300 * version limitations are as follows:
3301 * - no floating point registers are dumped
3303 * Function returns 0 in case of success, negative errno otherwise.
3305 * TODO: make this work also during runtime: it should be
3306 * possible to force coredump from running process and then
3307 * continue processing. For example qemu could set up SIGUSR2
3308 * handler (provided that target process haven't registered
3309 * handler for that) that does the dump when signal is received.
3311 static int elf_core_dump(int signr
, const CPUArchState
*env
)
3313 const CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)env
);
3314 const TaskState
*ts
= (const TaskState
*)cpu
->opaque
;
3315 struct vm_area_struct
*vma
= NULL
;
3316 char corefile
[PATH_MAX
];
3317 struct elf_note_info info
;
3319 struct elf_phdr phdr
;
3320 struct rlimit dumpsize
;
3321 struct mm_struct
*mm
= NULL
;
3322 off_t offset
= 0, data_offset
= 0;
3326 init_note_info(&info
);
3329 getrlimit(RLIMIT_CORE
, &dumpsize
);
3330 if (dumpsize
.rlim_cur
== 0)
3333 if (core_dump_filename(ts
, corefile
, sizeof (corefile
)) < 0)
3336 if ((fd
= open(corefile
, O_WRONLY
| O_CREAT
,
3337 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) < 0)
3341 * Walk through target process memory mappings and
3342 * set up structure containing this information. After
3343 * this point vma_xxx functions can be used.
3345 if ((mm
= vma_init()) == NULL
)
3348 walk_memory_regions(mm
, vma_walker
);
3349 segs
= vma_get_mapping_count(mm
);
3352 * Construct valid coredump ELF header. We also
3353 * add one more segment for notes.
3355 fill_elf_header(&elf
, segs
+ 1, ELF_MACHINE
, 0);
3356 if (dump_write(fd
, &elf
, sizeof (elf
)) != 0)
3359 /* fill in the in-memory version of notes */
3360 if (fill_note_info(&info
, signr
, env
) < 0)
3363 offset
+= sizeof (elf
); /* elf header */
3364 offset
+= (segs
+ 1) * sizeof (struct elf_phdr
); /* program headers */
3366 /* write out notes program header */
3367 fill_elf_note_phdr(&phdr
, info
.notes_size
, offset
);
3369 offset
+= info
.notes_size
;
3370 if (dump_write(fd
, &phdr
, sizeof (phdr
)) != 0)
3374 * ELF specification wants data to start at page boundary so
3377 data_offset
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
3380 * Write program headers for memory regions mapped in
3381 * the target process.
3383 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
3384 (void) memset(&phdr
, 0, sizeof (phdr
));
3386 phdr
.p_type
= PT_LOAD
;
3387 phdr
.p_offset
= offset
;
3388 phdr
.p_vaddr
= vma
->vma_start
;
3390 phdr
.p_filesz
= vma_dump_size(vma
);
3391 offset
+= phdr
.p_filesz
;
3392 phdr
.p_memsz
= vma
->vma_end
- vma
->vma_start
;
3393 phdr
.p_flags
= vma
->vma_flags
& PROT_READ
? PF_R
: 0;
3394 if (vma
->vma_flags
& PROT_WRITE
)
3395 phdr
.p_flags
|= PF_W
;
3396 if (vma
->vma_flags
& PROT_EXEC
)
3397 phdr
.p_flags
|= PF_X
;
3398 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
3400 bswap_phdr(&phdr
, 1);
3401 if (dump_write(fd
, &phdr
, sizeof(phdr
)) != 0) {
3407 * Next we write notes just after program headers. No
3408 * alignment needed here.
3410 if (write_note_info(&info
, fd
) < 0)
3413 /* align data to page boundary */
3414 if (lseek(fd
, data_offset
, SEEK_SET
) != data_offset
)
3418 * Finally we can dump process memory into corefile as well.
3420 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
3424 end
= vma
->vma_start
+ vma_dump_size(vma
);
3426 for (addr
= vma
->vma_start
; addr
< end
;
3427 addr
+= TARGET_PAGE_SIZE
) {
3428 char page
[TARGET_PAGE_SIZE
];
3432 * Read in page from target process memory and
3433 * write it to coredump file.
3435 error
= copy_from_user(page
, addr
, sizeof (page
));
3437 (void) fprintf(stderr
, "unable to dump " TARGET_ABI_FMT_lx
"\n",
3442 if (dump_write(fd
, page
, TARGET_PAGE_SIZE
) < 0)
3448 free_note_info(&info
);
3457 #endif /* USE_ELF_CORE_DUMP */
3459 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
3461 init_thread(regs
, infop
);