1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
5 #include <sys/resource.h>
9 #include "disas/disas.h"
10 #include "qemu/path.h"
11 #include "qemu/queue.h"
12 #include "qemu/guest-random.h"
13 #include "qemu/units.h"
25 #define ELF_OSABI ELFOSABI_SYSV
27 /* from personality.h */
30 * Flags for bug emulation.
32 * These occupy the top three bytes.
35 ADDR_NO_RANDOMIZE
= 0x0040000, /* disable randomization of VA space */
36 FDPIC_FUNCPTRS
= 0x0080000, /* userspace function ptrs point to
37 descriptors (signal handling) */
38 MMAP_PAGE_ZERO
= 0x0100000,
39 ADDR_COMPAT_LAYOUT
= 0x0200000,
40 READ_IMPLIES_EXEC
= 0x0400000,
41 ADDR_LIMIT_32BIT
= 0x0800000,
42 SHORT_INODE
= 0x1000000,
43 WHOLE_SECONDS
= 0x2000000,
44 STICKY_TIMEOUTS
= 0x4000000,
45 ADDR_LIMIT_3GB
= 0x8000000,
51 * These go in the low byte. Avoid using the top bit, it will
52 * conflict with error returns.
56 PER_LINUX_32BIT
= 0x0000 | ADDR_LIMIT_32BIT
,
57 PER_LINUX_FDPIC
= 0x0000 | FDPIC_FUNCPTRS
,
58 PER_SVR4
= 0x0001 | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
59 PER_SVR3
= 0x0002 | STICKY_TIMEOUTS
| SHORT_INODE
,
60 PER_SCOSVR3
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
| SHORT_INODE
,
61 PER_OSR5
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
,
62 PER_WYSEV386
= 0x0004 | STICKY_TIMEOUTS
| SHORT_INODE
,
63 PER_ISCR4
= 0x0005 | STICKY_TIMEOUTS
,
65 PER_SUNOS
= 0x0006 | STICKY_TIMEOUTS
,
66 PER_XENIX
= 0x0007 | STICKY_TIMEOUTS
| SHORT_INODE
,
68 PER_LINUX32_3GB
= 0x0008 | ADDR_LIMIT_3GB
,
69 PER_IRIX32
= 0x0009 | STICKY_TIMEOUTS
,/* IRIX5 32-bit */
70 PER_IRIXN32
= 0x000a | STICKY_TIMEOUTS
,/* IRIX6 new 32-bit */
71 PER_IRIX64
= 0x000b | STICKY_TIMEOUTS
,/* IRIX6 64-bit */
73 PER_SOLARIS
= 0x000d | STICKY_TIMEOUTS
,
74 PER_UW7
= 0x000e | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
75 PER_OSF4
= 0x000f, /* OSF/1 v4 */
81 * Return the base personality without flags.
83 #define personality(pers) (pers & PER_MASK)
85 int info_is_fdpic(struct image_info
*info
)
87 return info
->personality
== PER_LINUX_FDPIC
;
90 /* this flag is uneffective under linux too, should be deleted */
92 #define MAP_DENYWRITE 0
95 /* should probably go in elf.h */
100 #ifdef TARGET_WORDS_BIGENDIAN
101 #define ELF_DATA ELFDATA2MSB
103 #define ELF_DATA ELFDATA2LSB
106 #ifdef TARGET_ABI_MIPSN32
107 typedef abi_ullong target_elf_greg_t
;
108 #define tswapreg(ptr) tswap64(ptr)
110 typedef abi_ulong target_elf_greg_t
;
111 #define tswapreg(ptr) tswapal(ptr)
115 typedef abi_ushort target_uid_t
;
116 typedef abi_ushort target_gid_t
;
118 typedef abi_uint target_uid_t
;
119 typedef abi_uint target_gid_t
;
121 typedef abi_int target_pid_t
;
125 #define ELF_PLATFORM get_elf_platform()
127 static const char *get_elf_platform(void)
129 static char elf_platform
[] = "i386";
130 int family
= object_property_get_int(OBJECT(thread_cpu
), "family", NULL
);
134 elf_platform
[1] = '0' + family
;
138 #define ELF_HWCAP get_elf_hwcap()
140 static uint32_t get_elf_hwcap(void)
142 X86CPU
*cpu
= X86_CPU(thread_cpu
);
144 return cpu
->env
.features
[FEAT_1_EDX
];
148 #define ELF_START_MMAP 0x2aaaaab000ULL
150 #define ELF_CLASS ELFCLASS64
151 #define ELF_ARCH EM_X86_64
153 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
156 regs
->rsp
= infop
->start_stack
;
157 regs
->rip
= infop
->entry
;
161 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
164 * Note that ELF_NREG should be 29 as there should be place for
165 * TRAPNO and ERR "registers" as well but linux doesn't dump
168 * See linux kernel: arch/x86/include/asm/elf.h
170 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
172 (*regs
)[0] = env
->regs
[15];
173 (*regs
)[1] = env
->regs
[14];
174 (*regs
)[2] = env
->regs
[13];
175 (*regs
)[3] = env
->regs
[12];
176 (*regs
)[4] = env
->regs
[R_EBP
];
177 (*regs
)[5] = env
->regs
[R_EBX
];
178 (*regs
)[6] = env
->regs
[11];
179 (*regs
)[7] = env
->regs
[10];
180 (*regs
)[8] = env
->regs
[9];
181 (*regs
)[9] = env
->regs
[8];
182 (*regs
)[10] = env
->regs
[R_EAX
];
183 (*regs
)[11] = env
->regs
[R_ECX
];
184 (*regs
)[12] = env
->regs
[R_EDX
];
185 (*regs
)[13] = env
->regs
[R_ESI
];
186 (*regs
)[14] = env
->regs
[R_EDI
];
187 (*regs
)[15] = env
->regs
[R_EAX
]; /* XXX */
188 (*regs
)[16] = env
->eip
;
189 (*regs
)[17] = env
->segs
[R_CS
].selector
& 0xffff;
190 (*regs
)[18] = env
->eflags
;
191 (*regs
)[19] = env
->regs
[R_ESP
];
192 (*regs
)[20] = env
->segs
[R_SS
].selector
& 0xffff;
193 (*regs
)[21] = env
->segs
[R_FS
].selector
& 0xffff;
194 (*regs
)[22] = env
->segs
[R_GS
].selector
& 0xffff;
195 (*regs
)[23] = env
->segs
[R_DS
].selector
& 0xffff;
196 (*regs
)[24] = env
->segs
[R_ES
].selector
& 0xffff;
197 (*regs
)[25] = env
->segs
[R_FS
].selector
& 0xffff;
198 (*regs
)[26] = env
->segs
[R_GS
].selector
& 0xffff;
203 #define ELF_START_MMAP 0x80000000
206 * This is used to ensure we don't load something for the wrong architecture.
208 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
211 * These are used to set parameters in the core dumps.
213 #define ELF_CLASS ELFCLASS32
214 #define ELF_ARCH EM_386
216 static inline void init_thread(struct target_pt_regs
*regs
,
217 struct image_info
*infop
)
219 regs
->esp
= infop
->start_stack
;
220 regs
->eip
= infop
->entry
;
222 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
223 starts %edx contains a pointer to a function which might be
224 registered using `atexit'. This provides a mean for the
225 dynamic linker to call DT_FINI functions for shared libraries
226 that have been loaded before the code runs.
228 A value of 0 tells we have no such handler. */
233 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
236 * Note that ELF_NREG should be 19 as there should be place for
237 * TRAPNO and ERR "registers" as well but linux doesn't dump
240 * See linux kernel: arch/x86/include/asm/elf.h
242 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
244 (*regs
)[0] = env
->regs
[R_EBX
];
245 (*regs
)[1] = env
->regs
[R_ECX
];
246 (*regs
)[2] = env
->regs
[R_EDX
];
247 (*regs
)[3] = env
->regs
[R_ESI
];
248 (*regs
)[4] = env
->regs
[R_EDI
];
249 (*regs
)[5] = env
->regs
[R_EBP
];
250 (*regs
)[6] = env
->regs
[R_EAX
];
251 (*regs
)[7] = env
->segs
[R_DS
].selector
& 0xffff;
252 (*regs
)[8] = env
->segs
[R_ES
].selector
& 0xffff;
253 (*regs
)[9] = env
->segs
[R_FS
].selector
& 0xffff;
254 (*regs
)[10] = env
->segs
[R_GS
].selector
& 0xffff;
255 (*regs
)[11] = env
->regs
[R_EAX
]; /* XXX */
256 (*regs
)[12] = env
->eip
;
257 (*regs
)[13] = env
->segs
[R_CS
].selector
& 0xffff;
258 (*regs
)[14] = env
->eflags
;
259 (*regs
)[15] = env
->regs
[R_ESP
];
260 (*regs
)[16] = env
->segs
[R_SS
].selector
& 0xffff;
264 #define USE_ELF_CORE_DUMP
265 #define ELF_EXEC_PAGESIZE 4096
271 #ifndef TARGET_AARCH64
272 /* 32 bit ARM definitions */
274 #define ELF_START_MMAP 0x80000000
276 #define ELF_ARCH EM_ARM
277 #define ELF_CLASS ELFCLASS32
279 static inline void init_thread(struct target_pt_regs
*regs
,
280 struct image_info
*infop
)
282 abi_long stack
= infop
->start_stack
;
283 memset(regs
, 0, sizeof(*regs
));
285 regs
->uregs
[16] = ARM_CPU_MODE_USR
;
286 if (infop
->entry
& 1) {
287 regs
->uregs
[16] |= CPSR_T
;
289 regs
->uregs
[15] = infop
->entry
& 0xfffffffe;
290 regs
->uregs
[13] = infop
->start_stack
;
291 /* FIXME - what to for failure of get_user()? */
292 get_user_ual(regs
->uregs
[2], stack
+ 8); /* envp */
293 get_user_ual(regs
->uregs
[1], stack
+ 4); /* envp */
294 /* XXX: it seems that r0 is zeroed after ! */
296 /* For uClinux PIC binaries. */
297 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
298 regs
->uregs
[10] = infop
->start_data
;
300 /* Support ARM FDPIC. */
301 if (info_is_fdpic(infop
)) {
302 /* As described in the ABI document, r7 points to the loadmap info
303 * prepared by the kernel. If an interpreter is needed, r8 points
304 * to the interpreter loadmap and r9 points to the interpreter
305 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
306 * r9 points to the main program PT_DYNAMIC info.
308 regs
->uregs
[7] = infop
->loadmap_addr
;
309 if (infop
->interpreter_loadmap_addr
) {
310 /* Executable is dynamically loaded. */
311 regs
->uregs
[8] = infop
->interpreter_loadmap_addr
;
312 regs
->uregs
[9] = infop
->interpreter_pt_dynamic_addr
;
315 regs
->uregs
[9] = infop
->pt_dynamic_addr
;
321 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
323 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUARMState
*env
)
325 (*regs
)[0] = tswapreg(env
->regs
[0]);
326 (*regs
)[1] = tswapreg(env
->regs
[1]);
327 (*regs
)[2] = tswapreg(env
->regs
[2]);
328 (*regs
)[3] = tswapreg(env
->regs
[3]);
329 (*regs
)[4] = tswapreg(env
->regs
[4]);
330 (*regs
)[5] = tswapreg(env
->regs
[5]);
331 (*regs
)[6] = tswapreg(env
->regs
[6]);
332 (*regs
)[7] = tswapreg(env
->regs
[7]);
333 (*regs
)[8] = tswapreg(env
->regs
[8]);
334 (*regs
)[9] = tswapreg(env
->regs
[9]);
335 (*regs
)[10] = tswapreg(env
->regs
[10]);
336 (*regs
)[11] = tswapreg(env
->regs
[11]);
337 (*regs
)[12] = tswapreg(env
->regs
[12]);
338 (*regs
)[13] = tswapreg(env
->regs
[13]);
339 (*regs
)[14] = tswapreg(env
->regs
[14]);
340 (*regs
)[15] = tswapreg(env
->regs
[15]);
342 (*regs
)[16] = tswapreg(cpsr_read((CPUARMState
*)env
));
343 (*regs
)[17] = tswapreg(env
->regs
[0]); /* XXX */
346 #define USE_ELF_CORE_DUMP
347 #define ELF_EXEC_PAGESIZE 4096
351 ARM_HWCAP_ARM_SWP
= 1 << 0,
352 ARM_HWCAP_ARM_HALF
= 1 << 1,
353 ARM_HWCAP_ARM_THUMB
= 1 << 2,
354 ARM_HWCAP_ARM_26BIT
= 1 << 3,
355 ARM_HWCAP_ARM_FAST_MULT
= 1 << 4,
356 ARM_HWCAP_ARM_FPA
= 1 << 5,
357 ARM_HWCAP_ARM_VFP
= 1 << 6,
358 ARM_HWCAP_ARM_EDSP
= 1 << 7,
359 ARM_HWCAP_ARM_JAVA
= 1 << 8,
360 ARM_HWCAP_ARM_IWMMXT
= 1 << 9,
361 ARM_HWCAP_ARM_CRUNCH
= 1 << 10,
362 ARM_HWCAP_ARM_THUMBEE
= 1 << 11,
363 ARM_HWCAP_ARM_NEON
= 1 << 12,
364 ARM_HWCAP_ARM_VFPv3
= 1 << 13,
365 ARM_HWCAP_ARM_VFPv3D16
= 1 << 14,
366 ARM_HWCAP_ARM_TLS
= 1 << 15,
367 ARM_HWCAP_ARM_VFPv4
= 1 << 16,
368 ARM_HWCAP_ARM_IDIVA
= 1 << 17,
369 ARM_HWCAP_ARM_IDIVT
= 1 << 18,
370 ARM_HWCAP_ARM_VFPD32
= 1 << 19,
371 ARM_HWCAP_ARM_LPAE
= 1 << 20,
372 ARM_HWCAP_ARM_EVTSTRM
= 1 << 21,
376 ARM_HWCAP2_ARM_AES
= 1 << 0,
377 ARM_HWCAP2_ARM_PMULL
= 1 << 1,
378 ARM_HWCAP2_ARM_SHA1
= 1 << 2,
379 ARM_HWCAP2_ARM_SHA2
= 1 << 3,
380 ARM_HWCAP2_ARM_CRC32
= 1 << 4,
383 /* The commpage only exists for 32 bit kernels */
385 /* Return 1 if the proposed guest space is suitable for the guest.
386 * Return 0 if the proposed guest space isn't suitable, but another
387 * address space should be tried.
388 * Return -1 if there is no way the proposed guest space can be
389 * valid regardless of the base.
390 * The guest code may leave a page mapped and populate it if the
391 * address is suitable.
393 static int init_guest_commpage(unsigned long guest_base
,
394 unsigned long guest_size
)
396 unsigned long real_start
, test_page_addr
;
398 /* We need to check that we can force a fault on access to the
399 * commpage at 0xffff0fxx
401 test_page_addr
= guest_base
+ (0xffff0f00 & qemu_host_page_mask
);
403 /* If the commpage lies within the already allocated guest space,
404 * then there is no way we can allocate it.
406 * You may be thinking that that this check is redundant because
407 * we already validated the guest size against MAX_RESERVED_VA;
408 * but if qemu_host_page_mask is unusually large, then
409 * test_page_addr may be lower.
411 if (test_page_addr
>= guest_base
412 && test_page_addr
< (guest_base
+ guest_size
)) {
416 /* Note it needs to be writeable to let us initialise it */
417 real_start
= (unsigned long)
418 mmap((void *)test_page_addr
, qemu_host_page_size
,
419 PROT_READ
| PROT_WRITE
,
420 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
422 /* If we can't map it then try another address */
423 if (real_start
== -1ul) {
427 if (real_start
!= test_page_addr
) {
428 /* OS didn't put the page where we asked - unmap and reject */
429 munmap((void *)real_start
, qemu_host_page_size
);
433 /* Leave the page mapped
434 * Populate it (mmap should have left it all 0'd)
437 /* Kernel helper versions */
438 __put_user(5, (uint32_t *)g2h(0xffff0ffcul
));
440 /* Now it's populated make it RO */
441 if (mprotect((void *)test_page_addr
, qemu_host_page_size
, PROT_READ
)) {
442 perror("Protecting guest commpage");
446 return 1; /* All good */
449 #define ELF_HWCAP get_elf_hwcap()
450 #define ELF_HWCAP2 get_elf_hwcap2()
452 static uint32_t get_elf_hwcap(void)
454 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
457 hwcaps
|= ARM_HWCAP_ARM_SWP
;
458 hwcaps
|= ARM_HWCAP_ARM_HALF
;
459 hwcaps
|= ARM_HWCAP_ARM_THUMB
;
460 hwcaps
|= ARM_HWCAP_ARM_FAST_MULT
;
462 /* probe for the extra features */
463 #define GET_FEATURE(feat, hwcap) \
464 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
466 #define GET_FEATURE_ID(feat, hwcap) \
467 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
469 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
470 GET_FEATURE(ARM_FEATURE_V5
, ARM_HWCAP_ARM_EDSP
);
471 GET_FEATURE(ARM_FEATURE_IWMMXT
, ARM_HWCAP_ARM_IWMMXT
);
472 GET_FEATURE(ARM_FEATURE_THUMB2EE
, ARM_HWCAP_ARM_THUMBEE
);
473 GET_FEATURE(ARM_FEATURE_NEON
, ARM_HWCAP_ARM_NEON
);
474 GET_FEATURE(ARM_FEATURE_V6K
, ARM_HWCAP_ARM_TLS
);
475 GET_FEATURE(ARM_FEATURE_LPAE
, ARM_HWCAP_ARM_LPAE
);
476 GET_FEATURE_ID(aa32_arm_div
, ARM_HWCAP_ARM_IDIVA
);
477 GET_FEATURE_ID(aa32_thumb_div
, ARM_HWCAP_ARM_IDIVT
);
478 GET_FEATURE_ID(aa32_vfp
, ARM_HWCAP_ARM_VFP
);
480 if (cpu_isar_feature(aa32_fpsp_v3
, cpu
) ||
481 cpu_isar_feature(aa32_fpdp_v3
, cpu
)) {
482 hwcaps
|= ARM_HWCAP_ARM_VFPv3
;
483 if (cpu_isar_feature(aa32_simd_r32
, cpu
)) {
484 hwcaps
|= ARM_HWCAP_ARM_VFPD32
;
486 hwcaps
|= ARM_HWCAP_ARM_VFPv3D16
;
489 GET_FEATURE_ID(aa32_simdfmac
, ARM_HWCAP_ARM_VFPv4
);
494 static uint32_t get_elf_hwcap2(void)
496 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
499 GET_FEATURE_ID(aa32_aes
, ARM_HWCAP2_ARM_AES
);
500 GET_FEATURE_ID(aa32_pmull
, ARM_HWCAP2_ARM_PMULL
);
501 GET_FEATURE_ID(aa32_sha1
, ARM_HWCAP2_ARM_SHA1
);
502 GET_FEATURE_ID(aa32_sha2
, ARM_HWCAP2_ARM_SHA2
);
503 GET_FEATURE_ID(aa32_crc32
, ARM_HWCAP2_ARM_CRC32
);
508 #undef GET_FEATURE_ID
510 #define ELF_PLATFORM get_elf_platform()
512 static const char *get_elf_platform(void)
514 CPUARMState
*env
= thread_cpu
->env_ptr
;
516 #ifdef TARGET_WORDS_BIGENDIAN
522 if (arm_feature(env
, ARM_FEATURE_V8
)) {
524 } else if (arm_feature(env
, ARM_FEATURE_V7
)) {
525 if (arm_feature(env
, ARM_FEATURE_M
)) {
530 } else if (arm_feature(env
, ARM_FEATURE_V6
)) {
532 } else if (arm_feature(env
, ARM_FEATURE_V5
)) {
542 /* 64 bit ARM definitions */
543 #define ELF_START_MMAP 0x80000000
545 #define ELF_ARCH EM_AARCH64
546 #define ELF_CLASS ELFCLASS64
547 #ifdef TARGET_WORDS_BIGENDIAN
548 # define ELF_PLATFORM "aarch64_be"
550 # define ELF_PLATFORM "aarch64"
553 static inline void init_thread(struct target_pt_regs
*regs
,
554 struct image_info
*infop
)
556 abi_long stack
= infop
->start_stack
;
557 memset(regs
, 0, sizeof(*regs
));
559 regs
->pc
= infop
->entry
& ~0x3ULL
;
564 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
566 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
567 const CPUARMState
*env
)
571 for (i
= 0; i
< 32; i
++) {
572 (*regs
)[i
] = tswapreg(env
->xregs
[i
]);
574 (*regs
)[32] = tswapreg(env
->pc
);
575 (*regs
)[33] = tswapreg(pstate_read((CPUARMState
*)env
));
578 #define USE_ELF_CORE_DUMP
579 #define ELF_EXEC_PAGESIZE 4096
582 ARM_HWCAP_A64_FP
= 1 << 0,
583 ARM_HWCAP_A64_ASIMD
= 1 << 1,
584 ARM_HWCAP_A64_EVTSTRM
= 1 << 2,
585 ARM_HWCAP_A64_AES
= 1 << 3,
586 ARM_HWCAP_A64_PMULL
= 1 << 4,
587 ARM_HWCAP_A64_SHA1
= 1 << 5,
588 ARM_HWCAP_A64_SHA2
= 1 << 6,
589 ARM_HWCAP_A64_CRC32
= 1 << 7,
590 ARM_HWCAP_A64_ATOMICS
= 1 << 8,
591 ARM_HWCAP_A64_FPHP
= 1 << 9,
592 ARM_HWCAP_A64_ASIMDHP
= 1 << 10,
593 ARM_HWCAP_A64_CPUID
= 1 << 11,
594 ARM_HWCAP_A64_ASIMDRDM
= 1 << 12,
595 ARM_HWCAP_A64_JSCVT
= 1 << 13,
596 ARM_HWCAP_A64_FCMA
= 1 << 14,
597 ARM_HWCAP_A64_LRCPC
= 1 << 15,
598 ARM_HWCAP_A64_DCPOP
= 1 << 16,
599 ARM_HWCAP_A64_SHA3
= 1 << 17,
600 ARM_HWCAP_A64_SM3
= 1 << 18,
601 ARM_HWCAP_A64_SM4
= 1 << 19,
602 ARM_HWCAP_A64_ASIMDDP
= 1 << 20,
603 ARM_HWCAP_A64_SHA512
= 1 << 21,
604 ARM_HWCAP_A64_SVE
= 1 << 22,
605 ARM_HWCAP_A64_ASIMDFHM
= 1 << 23,
606 ARM_HWCAP_A64_DIT
= 1 << 24,
607 ARM_HWCAP_A64_USCAT
= 1 << 25,
608 ARM_HWCAP_A64_ILRCPC
= 1 << 26,
609 ARM_HWCAP_A64_FLAGM
= 1 << 27,
610 ARM_HWCAP_A64_SSBS
= 1 << 28,
611 ARM_HWCAP_A64_SB
= 1 << 29,
612 ARM_HWCAP_A64_PACA
= 1 << 30,
613 ARM_HWCAP_A64_PACG
= 1UL << 31,
615 ARM_HWCAP2_A64_DCPODP
= 1 << 0,
616 ARM_HWCAP2_A64_SVE2
= 1 << 1,
617 ARM_HWCAP2_A64_SVEAES
= 1 << 2,
618 ARM_HWCAP2_A64_SVEPMULL
= 1 << 3,
619 ARM_HWCAP2_A64_SVEBITPERM
= 1 << 4,
620 ARM_HWCAP2_A64_SVESHA3
= 1 << 5,
621 ARM_HWCAP2_A64_SVESM4
= 1 << 6,
622 ARM_HWCAP2_A64_FLAGM2
= 1 << 7,
623 ARM_HWCAP2_A64_FRINT
= 1 << 8,
626 #define ELF_HWCAP get_elf_hwcap()
627 #define ELF_HWCAP2 get_elf_hwcap2()
629 #define GET_FEATURE_ID(feat, hwcap) \
630 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
632 static uint32_t get_elf_hwcap(void)
634 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
637 hwcaps
|= ARM_HWCAP_A64_FP
;
638 hwcaps
|= ARM_HWCAP_A64_ASIMD
;
639 hwcaps
|= ARM_HWCAP_A64_CPUID
;
641 /* probe for the extra features */
643 GET_FEATURE_ID(aa64_aes
, ARM_HWCAP_A64_AES
);
644 GET_FEATURE_ID(aa64_pmull
, ARM_HWCAP_A64_PMULL
);
645 GET_FEATURE_ID(aa64_sha1
, ARM_HWCAP_A64_SHA1
);
646 GET_FEATURE_ID(aa64_sha256
, ARM_HWCAP_A64_SHA2
);
647 GET_FEATURE_ID(aa64_sha512
, ARM_HWCAP_A64_SHA512
);
648 GET_FEATURE_ID(aa64_crc32
, ARM_HWCAP_A64_CRC32
);
649 GET_FEATURE_ID(aa64_sha3
, ARM_HWCAP_A64_SHA3
);
650 GET_FEATURE_ID(aa64_sm3
, ARM_HWCAP_A64_SM3
);
651 GET_FEATURE_ID(aa64_sm4
, ARM_HWCAP_A64_SM4
);
652 GET_FEATURE_ID(aa64_fp16
, ARM_HWCAP_A64_FPHP
| ARM_HWCAP_A64_ASIMDHP
);
653 GET_FEATURE_ID(aa64_atomics
, ARM_HWCAP_A64_ATOMICS
);
654 GET_FEATURE_ID(aa64_rdm
, ARM_HWCAP_A64_ASIMDRDM
);
655 GET_FEATURE_ID(aa64_dp
, ARM_HWCAP_A64_ASIMDDP
);
656 GET_FEATURE_ID(aa64_fcma
, ARM_HWCAP_A64_FCMA
);
657 GET_FEATURE_ID(aa64_sve
, ARM_HWCAP_A64_SVE
);
658 GET_FEATURE_ID(aa64_pauth
, ARM_HWCAP_A64_PACA
| ARM_HWCAP_A64_PACG
);
659 GET_FEATURE_ID(aa64_fhm
, ARM_HWCAP_A64_ASIMDFHM
);
660 GET_FEATURE_ID(aa64_jscvt
, ARM_HWCAP_A64_JSCVT
);
661 GET_FEATURE_ID(aa64_sb
, ARM_HWCAP_A64_SB
);
662 GET_FEATURE_ID(aa64_condm_4
, ARM_HWCAP_A64_FLAGM
);
663 GET_FEATURE_ID(aa64_dcpop
, ARM_HWCAP_A64_DCPOP
);
664 GET_FEATURE_ID(aa64_rcpc_8_3
, ARM_HWCAP_A64_LRCPC
);
665 GET_FEATURE_ID(aa64_rcpc_8_4
, ARM_HWCAP_A64_ILRCPC
);
670 static uint32_t get_elf_hwcap2(void)
672 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
675 GET_FEATURE_ID(aa64_dcpodp
, ARM_HWCAP2_A64_DCPODP
);
676 GET_FEATURE_ID(aa64_condm_5
, ARM_HWCAP2_A64_FLAGM2
);
677 GET_FEATURE_ID(aa64_frint
, ARM_HWCAP2_A64_FRINT
);
682 #undef GET_FEATURE_ID
684 #endif /* not TARGET_AARCH64 */
685 #endif /* TARGET_ARM */
688 #ifdef TARGET_SPARC64
690 #define ELF_START_MMAP 0x80000000
691 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
692 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
694 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
696 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
699 #define ELF_CLASS ELFCLASS64
700 #define ELF_ARCH EM_SPARCV9
702 #define STACK_BIAS 2047
704 static inline void init_thread(struct target_pt_regs
*regs
,
705 struct image_info
*infop
)
710 regs
->pc
= infop
->entry
;
711 regs
->npc
= regs
->pc
+ 4;
714 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
716 if (personality(infop
->personality
) == PER_LINUX32
)
717 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
719 regs
->u_regs
[14] = infop
->start_stack
- 16 * 8 - STACK_BIAS
;
724 #define ELF_START_MMAP 0x80000000
725 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
726 | HWCAP_SPARC_MULDIV)
728 #define ELF_CLASS ELFCLASS32
729 #define ELF_ARCH EM_SPARC
731 static inline void init_thread(struct target_pt_regs
*regs
,
732 struct image_info
*infop
)
735 regs
->pc
= infop
->entry
;
736 regs
->npc
= regs
->pc
+ 4;
738 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
746 #define ELF_MACHINE PPC_ELF_MACHINE
747 #define ELF_START_MMAP 0x80000000
749 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
751 #define elf_check_arch(x) ( (x) == EM_PPC64 )
753 #define ELF_CLASS ELFCLASS64
757 #define ELF_CLASS ELFCLASS32
761 #define ELF_ARCH EM_PPC
763 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
764 See arch/powerpc/include/asm/cputable.h. */
766 QEMU_PPC_FEATURE_32
= 0x80000000,
767 QEMU_PPC_FEATURE_64
= 0x40000000,
768 QEMU_PPC_FEATURE_601_INSTR
= 0x20000000,
769 QEMU_PPC_FEATURE_HAS_ALTIVEC
= 0x10000000,
770 QEMU_PPC_FEATURE_HAS_FPU
= 0x08000000,
771 QEMU_PPC_FEATURE_HAS_MMU
= 0x04000000,
772 QEMU_PPC_FEATURE_HAS_4xxMAC
= 0x02000000,
773 QEMU_PPC_FEATURE_UNIFIED_CACHE
= 0x01000000,
774 QEMU_PPC_FEATURE_HAS_SPE
= 0x00800000,
775 QEMU_PPC_FEATURE_HAS_EFP_SINGLE
= 0x00400000,
776 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
= 0x00200000,
777 QEMU_PPC_FEATURE_NO_TB
= 0x00100000,
778 QEMU_PPC_FEATURE_POWER4
= 0x00080000,
779 QEMU_PPC_FEATURE_POWER5
= 0x00040000,
780 QEMU_PPC_FEATURE_POWER5_PLUS
= 0x00020000,
781 QEMU_PPC_FEATURE_CELL
= 0x00010000,
782 QEMU_PPC_FEATURE_BOOKE
= 0x00008000,
783 QEMU_PPC_FEATURE_SMT
= 0x00004000,
784 QEMU_PPC_FEATURE_ICACHE_SNOOP
= 0x00002000,
785 QEMU_PPC_FEATURE_ARCH_2_05
= 0x00001000,
786 QEMU_PPC_FEATURE_PA6T
= 0x00000800,
787 QEMU_PPC_FEATURE_HAS_DFP
= 0x00000400,
788 QEMU_PPC_FEATURE_POWER6_EXT
= 0x00000200,
789 QEMU_PPC_FEATURE_ARCH_2_06
= 0x00000100,
790 QEMU_PPC_FEATURE_HAS_VSX
= 0x00000080,
791 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT
= 0x00000040,
793 QEMU_PPC_FEATURE_TRUE_LE
= 0x00000002,
794 QEMU_PPC_FEATURE_PPC_LE
= 0x00000001,
796 /* Feature definitions in AT_HWCAP2. */
797 QEMU_PPC_FEATURE2_ARCH_2_07
= 0x80000000, /* ISA 2.07 */
798 QEMU_PPC_FEATURE2_HAS_HTM
= 0x40000000, /* Hardware Transactional Memory */
799 QEMU_PPC_FEATURE2_HAS_DSCR
= 0x20000000, /* Data Stream Control Register */
800 QEMU_PPC_FEATURE2_HAS_EBB
= 0x10000000, /* Event Base Branching */
801 QEMU_PPC_FEATURE2_HAS_ISEL
= 0x08000000, /* Integer Select */
802 QEMU_PPC_FEATURE2_HAS_TAR
= 0x04000000, /* Target Address Register */
803 QEMU_PPC_FEATURE2_VEC_CRYPTO
= 0x02000000,
804 QEMU_PPC_FEATURE2_HTM_NOSC
= 0x01000000,
805 QEMU_PPC_FEATURE2_ARCH_3_00
= 0x00800000, /* ISA 3.00 */
806 QEMU_PPC_FEATURE2_HAS_IEEE128
= 0x00400000, /* VSX IEEE Bin Float 128-bit */
807 QEMU_PPC_FEATURE2_DARN
= 0x00200000, /* darn random number insn */
808 QEMU_PPC_FEATURE2_SCV
= 0x00100000, /* scv syscall */
809 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND
= 0x00080000, /* TM w/o suspended state */
812 #define ELF_HWCAP get_elf_hwcap()
814 static uint32_t get_elf_hwcap(void)
816 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
817 uint32_t features
= 0;
819 /* We don't have to be terribly complete here; the high points are
820 Altivec/FP/SPE support. Anything else is just a bonus. */
821 #define GET_FEATURE(flag, feature) \
822 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
823 #define GET_FEATURE2(flags, feature) \
825 if ((cpu->env.insns_flags2 & flags) == flags) { \
826 features |= feature; \
829 GET_FEATURE(PPC_64B
, QEMU_PPC_FEATURE_64
);
830 GET_FEATURE(PPC_FLOAT
, QEMU_PPC_FEATURE_HAS_FPU
);
831 GET_FEATURE(PPC_ALTIVEC
, QEMU_PPC_FEATURE_HAS_ALTIVEC
);
832 GET_FEATURE(PPC_SPE
, QEMU_PPC_FEATURE_HAS_SPE
);
833 GET_FEATURE(PPC_SPE_SINGLE
, QEMU_PPC_FEATURE_HAS_EFP_SINGLE
);
834 GET_FEATURE(PPC_SPE_DOUBLE
, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
);
835 GET_FEATURE(PPC_BOOKE
, QEMU_PPC_FEATURE_BOOKE
);
836 GET_FEATURE(PPC_405_MAC
, QEMU_PPC_FEATURE_HAS_4xxMAC
);
837 GET_FEATURE2(PPC2_DFP
, QEMU_PPC_FEATURE_HAS_DFP
);
838 GET_FEATURE2(PPC2_VSX
, QEMU_PPC_FEATURE_HAS_VSX
);
839 GET_FEATURE2((PPC2_PERM_ISA206
| PPC2_DIVE_ISA206
| PPC2_ATOMIC_ISA206
|
840 PPC2_FP_CVT_ISA206
| PPC2_FP_TST_ISA206
),
841 QEMU_PPC_FEATURE_ARCH_2_06
);
848 #define ELF_HWCAP2 get_elf_hwcap2()
850 static uint32_t get_elf_hwcap2(void)
852 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
853 uint32_t features
= 0;
855 #define GET_FEATURE(flag, feature) \
856 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
857 #define GET_FEATURE2(flag, feature) \
858 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
860 GET_FEATURE(PPC_ISEL
, QEMU_PPC_FEATURE2_HAS_ISEL
);
861 GET_FEATURE2(PPC2_BCTAR_ISA207
, QEMU_PPC_FEATURE2_HAS_TAR
);
862 GET_FEATURE2((PPC2_BCTAR_ISA207
| PPC2_LSQ_ISA207
| PPC2_ALTIVEC_207
|
863 PPC2_ISA207S
), QEMU_PPC_FEATURE2_ARCH_2_07
|
864 QEMU_PPC_FEATURE2_VEC_CRYPTO
);
865 GET_FEATURE2(PPC2_ISA300
, QEMU_PPC_FEATURE2_ARCH_3_00
|
866 QEMU_PPC_FEATURE2_DARN
);
875 * The requirements here are:
876 * - keep the final alignment of sp (sp & 0xf)
877 * - make sure the 32-bit value at the first 16 byte aligned position of
878 * AUXV is greater than 16 for glibc compatibility.
879 * AT_IGNOREPPC is used for that.
880 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
881 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
883 #define DLINFO_ARCH_ITEMS 5
884 #define ARCH_DLINFO \
886 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
888 * Handle glibc compatibility: these magic entries must \
889 * be at the lowest addresses in the final auxv. \
891 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
892 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
893 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
894 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
895 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
898 static inline void init_thread(struct target_pt_regs
*_regs
, struct image_info
*infop
)
900 _regs
->gpr
[1] = infop
->start_stack
;
901 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
902 if (get_ppc64_abi(infop
) < 2) {
904 get_user_u64(val
, infop
->entry
+ 8);
905 _regs
->gpr
[2] = val
+ infop
->load_bias
;
906 get_user_u64(val
, infop
->entry
);
907 infop
->entry
= val
+ infop
->load_bias
;
909 _regs
->gpr
[12] = infop
->entry
; /* r12 set to global entry address */
912 _regs
->nip
= infop
->entry
;
915 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
917 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
919 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUPPCState
*env
)
922 target_ulong ccr
= 0;
924 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
925 (*regs
)[i
] = tswapreg(env
->gpr
[i
]);
928 (*regs
)[32] = tswapreg(env
->nip
);
929 (*regs
)[33] = tswapreg(env
->msr
);
930 (*regs
)[35] = tswapreg(env
->ctr
);
931 (*regs
)[36] = tswapreg(env
->lr
);
932 (*regs
)[37] = tswapreg(env
->xer
);
934 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
935 ccr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
937 (*regs
)[38] = tswapreg(ccr
);
940 #define USE_ELF_CORE_DUMP
941 #define ELF_EXEC_PAGESIZE 4096
947 #define ELF_START_MMAP 0x80000000
950 #define ELF_CLASS ELFCLASS64
952 #define ELF_CLASS ELFCLASS32
954 #define ELF_ARCH EM_MIPS
956 #define elf_check_arch(x) ((x) == EM_MIPS || (x) == EM_NANOMIPS)
958 static inline void init_thread(struct target_pt_regs
*regs
,
959 struct image_info
*infop
)
961 regs
->cp0_status
= 2 << CP0St_KSU
;
962 regs
->cp0_epc
= infop
->entry
;
963 regs
->regs
[29] = infop
->start_stack
;
966 /* See linux kernel: arch/mips/include/asm/elf.h. */
968 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
970 /* See linux kernel: arch/mips/include/asm/reg.h. */
977 TARGET_EF_R26
= TARGET_EF_R0
+ 26,
978 TARGET_EF_R27
= TARGET_EF_R0
+ 27,
979 TARGET_EF_LO
= TARGET_EF_R0
+ 32,
980 TARGET_EF_HI
= TARGET_EF_R0
+ 33,
981 TARGET_EF_CP0_EPC
= TARGET_EF_R0
+ 34,
982 TARGET_EF_CP0_BADVADDR
= TARGET_EF_R0
+ 35,
983 TARGET_EF_CP0_STATUS
= TARGET_EF_R0
+ 36,
984 TARGET_EF_CP0_CAUSE
= TARGET_EF_R0
+ 37
987 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
988 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMIPSState
*env
)
992 for (i
= 0; i
< TARGET_EF_R0
; i
++) {
995 (*regs
)[TARGET_EF_R0
] = 0;
997 for (i
= 1; i
< ARRAY_SIZE(env
->active_tc
.gpr
); i
++) {
998 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->active_tc
.gpr
[i
]);
1001 (*regs
)[TARGET_EF_R26
] = 0;
1002 (*regs
)[TARGET_EF_R27
] = 0;
1003 (*regs
)[TARGET_EF_LO
] = tswapreg(env
->active_tc
.LO
[0]);
1004 (*regs
)[TARGET_EF_HI
] = tswapreg(env
->active_tc
.HI
[0]);
1005 (*regs
)[TARGET_EF_CP0_EPC
] = tswapreg(env
->active_tc
.PC
);
1006 (*regs
)[TARGET_EF_CP0_BADVADDR
] = tswapreg(env
->CP0_BadVAddr
);
1007 (*regs
)[TARGET_EF_CP0_STATUS
] = tswapreg(env
->CP0_Status
);
1008 (*regs
)[TARGET_EF_CP0_CAUSE
] = tswapreg(env
->CP0_Cause
);
1011 #define USE_ELF_CORE_DUMP
1012 #define ELF_EXEC_PAGESIZE 4096
1014 /* See arch/mips/include/uapi/asm/hwcap.h. */
1016 HWCAP_MIPS_R6
= (1 << 0),
1017 HWCAP_MIPS_MSA
= (1 << 1),
1020 #define ELF_HWCAP get_elf_hwcap()
1022 static uint32_t get_elf_hwcap(void)
1024 MIPSCPU
*cpu
= MIPS_CPU(thread_cpu
);
1025 uint32_t hwcaps
= 0;
1027 #define GET_FEATURE(flag, hwcap) \
1028 do { if (cpu->env.insn_flags & (flag)) { hwcaps |= hwcap; } } while (0)
1030 GET_FEATURE(ISA_MIPS32R6
| ISA_MIPS64R6
, HWCAP_MIPS_R6
);
1031 GET_FEATURE(ASE_MSA
, HWCAP_MIPS_MSA
);
1038 #endif /* TARGET_MIPS */
1040 #ifdef TARGET_MICROBLAZE
1042 #define ELF_START_MMAP 0x80000000
1044 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1046 #define ELF_CLASS ELFCLASS32
1047 #define ELF_ARCH EM_MICROBLAZE
1049 static inline void init_thread(struct target_pt_regs
*regs
,
1050 struct image_info
*infop
)
1052 regs
->pc
= infop
->entry
;
1053 regs
->r1
= infop
->start_stack
;
1057 #define ELF_EXEC_PAGESIZE 4096
1059 #define USE_ELF_CORE_DUMP
1061 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1063 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1064 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMBState
*env
)
1068 for (i
= 0; i
< 32; i
++) {
1069 (*regs
)[pos
++] = tswapreg(env
->regs
[i
]);
1072 for (i
= 0; i
< 6; i
++) {
1073 (*regs
)[pos
++] = tswapreg(env
->sregs
[i
]);
1077 #endif /* TARGET_MICROBLAZE */
1081 #define ELF_START_MMAP 0x80000000
1083 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1085 #define ELF_CLASS ELFCLASS32
1086 #define ELF_ARCH EM_ALTERA_NIOS2
1088 static void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1090 regs
->ea
= infop
->entry
;
1091 regs
->sp
= infop
->start_stack
;
1092 regs
->estatus
= 0x3;
1095 #define ELF_EXEC_PAGESIZE 4096
1097 #define USE_ELF_CORE_DUMP
1099 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1101 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1102 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1103 const CPUNios2State
*env
)
1108 for (i
= 1; i
< 8; i
++) /* r0-r7 */
1109 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1111 for (i
= 8; i
< 16; i
++) /* r8-r15 */
1112 (*regs
)[i
] = tswapreg(env
->regs
[i
- 8]);
1114 for (i
= 16; i
< 24; i
++) /* r16-r23 */
1115 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1116 (*regs
)[24] = -1; /* R_ET */
1117 (*regs
)[25] = -1; /* R_BT */
1118 (*regs
)[26] = tswapreg(env
->regs
[R_GP
]);
1119 (*regs
)[27] = tswapreg(env
->regs
[R_SP
]);
1120 (*regs
)[28] = tswapreg(env
->regs
[R_FP
]);
1121 (*regs
)[29] = tswapreg(env
->regs
[R_EA
]);
1122 (*regs
)[30] = -1; /* R_SSTATUS */
1123 (*regs
)[31] = tswapreg(env
->regs
[R_RA
]);
1125 (*regs
)[32] = tswapreg(env
->regs
[R_PC
]);
1127 (*regs
)[33] = -1; /* R_STATUS */
1128 (*regs
)[34] = tswapreg(env
->regs
[CR_ESTATUS
]);
1130 for (i
= 35; i
< 49; i
++) /* ... */
1134 #endif /* TARGET_NIOS2 */
1136 #ifdef TARGET_OPENRISC
1138 #define ELF_START_MMAP 0x08000000
1140 #define ELF_ARCH EM_OPENRISC
1141 #define ELF_CLASS ELFCLASS32
1142 #define ELF_DATA ELFDATA2MSB
1144 static inline void init_thread(struct target_pt_regs
*regs
,
1145 struct image_info
*infop
)
1147 regs
->pc
= infop
->entry
;
1148 regs
->gpr
[1] = infop
->start_stack
;
1151 #define USE_ELF_CORE_DUMP
1152 #define ELF_EXEC_PAGESIZE 8192
1154 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1155 #define ELF_NREG 34 /* gprs and pc, sr */
1156 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1158 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1159 const CPUOpenRISCState
*env
)
1163 for (i
= 0; i
< 32; i
++) {
1164 (*regs
)[i
] = tswapreg(cpu_get_gpr(env
, i
));
1166 (*regs
)[32] = tswapreg(env
->pc
);
1167 (*regs
)[33] = tswapreg(cpu_get_sr(env
));
1170 #define ELF_PLATFORM NULL
1172 #endif /* TARGET_OPENRISC */
1176 #define ELF_START_MMAP 0x80000000
1178 #define ELF_CLASS ELFCLASS32
1179 #define ELF_ARCH EM_SH
1181 static inline void init_thread(struct target_pt_regs
*regs
,
1182 struct image_info
*infop
)
1184 /* Check other registers XXXXX */
1185 regs
->pc
= infop
->entry
;
1186 regs
->regs
[15] = infop
->start_stack
;
1189 /* See linux kernel: arch/sh/include/asm/elf.h. */
1191 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1193 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1198 TARGET_REG_GBR
= 19,
1199 TARGET_REG_MACH
= 20,
1200 TARGET_REG_MACL
= 21,
1201 TARGET_REG_SYSCALL
= 22
1204 static inline void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1205 const CPUSH4State
*env
)
1209 for (i
= 0; i
< 16; i
++) {
1210 (*regs
)[i
] = tswapreg(env
->gregs
[i
]);
1213 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1214 (*regs
)[TARGET_REG_PR
] = tswapreg(env
->pr
);
1215 (*regs
)[TARGET_REG_SR
] = tswapreg(env
->sr
);
1216 (*regs
)[TARGET_REG_GBR
] = tswapreg(env
->gbr
);
1217 (*regs
)[TARGET_REG_MACH
] = tswapreg(env
->mach
);
1218 (*regs
)[TARGET_REG_MACL
] = tswapreg(env
->macl
);
1219 (*regs
)[TARGET_REG_SYSCALL
] = 0; /* FIXME */
1222 #define USE_ELF_CORE_DUMP
1223 #define ELF_EXEC_PAGESIZE 4096
1226 SH_CPU_HAS_FPU
= 0x0001, /* Hardware FPU support */
1227 SH_CPU_HAS_P2_FLUSH_BUG
= 0x0002, /* Need to flush the cache in P2 area */
1228 SH_CPU_HAS_MMU_PAGE_ASSOC
= 0x0004, /* SH3: TLB way selection bit support */
1229 SH_CPU_HAS_DSP
= 0x0008, /* SH-DSP: DSP support */
1230 SH_CPU_HAS_PERF_COUNTER
= 0x0010, /* Hardware performance counters */
1231 SH_CPU_HAS_PTEA
= 0x0020, /* PTEA register */
1232 SH_CPU_HAS_LLSC
= 0x0040, /* movli.l/movco.l */
1233 SH_CPU_HAS_L2_CACHE
= 0x0080, /* Secondary cache / URAM */
1234 SH_CPU_HAS_OP32
= 0x0100, /* 32-bit instruction support */
1235 SH_CPU_HAS_PTEAEX
= 0x0200, /* PTE ASID Extension support */
1238 #define ELF_HWCAP get_elf_hwcap()
1240 static uint32_t get_elf_hwcap(void)
1242 SuperHCPU
*cpu
= SUPERH_CPU(thread_cpu
);
1245 hwcap
|= SH_CPU_HAS_FPU
;
1247 if (cpu
->env
.features
& SH_FEATURE_SH4A
) {
1248 hwcap
|= SH_CPU_HAS_LLSC
;
1258 #define ELF_START_MMAP 0x80000000
1260 #define ELF_CLASS ELFCLASS32
1261 #define ELF_ARCH EM_CRIS
1263 static inline void init_thread(struct target_pt_regs
*regs
,
1264 struct image_info
*infop
)
1266 regs
->erp
= infop
->entry
;
1269 #define ELF_EXEC_PAGESIZE 8192
1275 #define ELF_START_MMAP 0x80000000
1277 #define ELF_CLASS ELFCLASS32
1278 #define ELF_ARCH EM_68K
1280 /* ??? Does this need to do anything?
1281 #define ELF_PLAT_INIT(_r) */
1283 static inline void init_thread(struct target_pt_regs
*regs
,
1284 struct image_info
*infop
)
1286 regs
->usp
= infop
->start_stack
;
1288 regs
->pc
= infop
->entry
;
1291 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1293 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1295 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUM68KState
*env
)
1297 (*regs
)[0] = tswapreg(env
->dregs
[1]);
1298 (*regs
)[1] = tswapreg(env
->dregs
[2]);
1299 (*regs
)[2] = tswapreg(env
->dregs
[3]);
1300 (*regs
)[3] = tswapreg(env
->dregs
[4]);
1301 (*regs
)[4] = tswapreg(env
->dregs
[5]);
1302 (*regs
)[5] = tswapreg(env
->dregs
[6]);
1303 (*regs
)[6] = tswapreg(env
->dregs
[7]);
1304 (*regs
)[7] = tswapreg(env
->aregs
[0]);
1305 (*regs
)[8] = tswapreg(env
->aregs
[1]);
1306 (*regs
)[9] = tswapreg(env
->aregs
[2]);
1307 (*regs
)[10] = tswapreg(env
->aregs
[3]);
1308 (*regs
)[11] = tswapreg(env
->aregs
[4]);
1309 (*regs
)[12] = tswapreg(env
->aregs
[5]);
1310 (*regs
)[13] = tswapreg(env
->aregs
[6]);
1311 (*regs
)[14] = tswapreg(env
->dregs
[0]);
1312 (*regs
)[15] = tswapreg(env
->aregs
[7]);
1313 (*regs
)[16] = tswapreg(env
->dregs
[0]); /* FIXME: orig_d0 */
1314 (*regs
)[17] = tswapreg(env
->sr
);
1315 (*regs
)[18] = tswapreg(env
->pc
);
1316 (*regs
)[19] = 0; /* FIXME: regs->format | regs->vector */
1319 #define USE_ELF_CORE_DUMP
1320 #define ELF_EXEC_PAGESIZE 8192
1326 #define ELF_START_MMAP (0x30000000000ULL)
1328 #define ELF_CLASS ELFCLASS64
1329 #define ELF_ARCH EM_ALPHA
1331 static inline void init_thread(struct target_pt_regs
*regs
,
1332 struct image_info
*infop
)
1334 regs
->pc
= infop
->entry
;
1336 regs
->usp
= infop
->start_stack
;
1339 #define ELF_EXEC_PAGESIZE 8192
1341 #endif /* TARGET_ALPHA */
1345 #define ELF_START_MMAP (0x20000000000ULL)
1347 #define ELF_CLASS ELFCLASS64
1348 #define ELF_DATA ELFDATA2MSB
1349 #define ELF_ARCH EM_S390
1353 #define ELF_HWCAP get_elf_hwcap()
1355 #define GET_FEATURE(_feat, _hwcap) \
1356 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1358 static uint32_t get_elf_hwcap(void)
1361 * Let's assume we always have esan3 and zarch.
1362 * 31-bit processes can use 64-bit registers (high gprs).
1364 uint32_t hwcap
= HWCAP_S390_ESAN3
| HWCAP_S390_ZARCH
| HWCAP_S390_HIGH_GPRS
;
1366 GET_FEATURE(S390_FEAT_STFLE
, HWCAP_S390_STFLE
);
1367 GET_FEATURE(S390_FEAT_MSA
, HWCAP_S390_MSA
);
1368 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT
, HWCAP_S390_LDISP
);
1369 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE
, HWCAP_S390_EIMM
);
1370 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3
) &&
1371 s390_has_feat(S390_FEAT_ETF3_ENH
)) {
1372 hwcap
|= HWCAP_S390_ETF3EH
;
1374 GET_FEATURE(S390_FEAT_VECTOR
, HWCAP_S390_VXRS
);
1379 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1381 regs
->psw
.addr
= infop
->entry
;
1382 regs
->psw
.mask
= PSW_MASK_64
| PSW_MASK_32
;
1383 regs
->gprs
[15] = infop
->start_stack
;
1386 #endif /* TARGET_S390X */
1388 #ifdef TARGET_TILEGX
1390 /* 42 bits real used address, a half for user mode */
1391 #define ELF_START_MMAP (0x00000020000000000ULL)
1393 #define elf_check_arch(x) ((x) == EM_TILEGX)
1395 #define ELF_CLASS ELFCLASS64
1396 #define ELF_DATA ELFDATA2LSB
1397 #define ELF_ARCH EM_TILEGX
1399 static inline void init_thread(struct target_pt_regs
*regs
,
1400 struct image_info
*infop
)
1402 regs
->pc
= infop
->entry
;
1403 regs
->sp
= infop
->start_stack
;
1407 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1409 #endif /* TARGET_TILEGX */
1413 #define ELF_START_MMAP 0x80000000
1414 #define ELF_ARCH EM_RISCV
1416 #ifdef TARGET_RISCV32
1417 #define ELF_CLASS ELFCLASS32
1419 #define ELF_CLASS ELFCLASS64
1422 static inline void init_thread(struct target_pt_regs
*regs
,
1423 struct image_info
*infop
)
1425 regs
->sepc
= infop
->entry
;
1426 regs
->sp
= infop
->start_stack
;
1429 #define ELF_EXEC_PAGESIZE 4096
1431 #endif /* TARGET_RISCV */
1435 #define ELF_START_MMAP 0x80000000
1436 #define ELF_CLASS ELFCLASS32
1437 #define ELF_ARCH EM_PARISC
1438 #define ELF_PLATFORM "PARISC"
1439 #define STACK_GROWS_DOWN 0
1440 #define STACK_ALIGNMENT 64
1442 static inline void init_thread(struct target_pt_regs
*regs
,
1443 struct image_info
*infop
)
1445 regs
->iaoq
[0] = infop
->entry
;
1446 regs
->iaoq
[1] = infop
->entry
+ 4;
1448 regs
->gr
[24] = infop
->arg_start
;
1449 regs
->gr
[25] = (infop
->arg_end
- infop
->arg_start
) / sizeof(abi_ulong
);
1450 /* The top-of-stack contains a linkage buffer. */
1451 regs
->gr
[30] = infop
->start_stack
+ 64;
1452 regs
->gr
[31] = infop
->entry
;
1455 #endif /* TARGET_HPPA */
1457 #ifdef TARGET_XTENSA
1459 #define ELF_START_MMAP 0x20000000
1461 #define ELF_CLASS ELFCLASS32
1462 #define ELF_ARCH EM_XTENSA
1464 static inline void init_thread(struct target_pt_regs
*regs
,
1465 struct image_info
*infop
)
1467 regs
->windowbase
= 0;
1468 regs
->windowstart
= 1;
1469 regs
->areg
[1] = infop
->start_stack
;
1470 regs
->pc
= infop
->entry
;
1473 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1474 #define ELF_NREG 128
1475 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1484 TARGET_REG_WINDOWSTART
,
1485 TARGET_REG_WINDOWBASE
,
1486 TARGET_REG_THREADPTR
,
1487 TARGET_REG_AR0
= 64,
1490 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1491 const CPUXtensaState
*env
)
1495 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1496 (*regs
)[TARGET_REG_PS
] = tswapreg(env
->sregs
[PS
] & ~PS_EXCM
);
1497 (*regs
)[TARGET_REG_LBEG
] = tswapreg(env
->sregs
[LBEG
]);
1498 (*regs
)[TARGET_REG_LEND
] = tswapreg(env
->sregs
[LEND
]);
1499 (*regs
)[TARGET_REG_LCOUNT
] = tswapreg(env
->sregs
[LCOUNT
]);
1500 (*regs
)[TARGET_REG_SAR
] = tswapreg(env
->sregs
[SAR
]);
1501 (*regs
)[TARGET_REG_WINDOWSTART
] = tswapreg(env
->sregs
[WINDOW_START
]);
1502 (*regs
)[TARGET_REG_WINDOWBASE
] = tswapreg(env
->sregs
[WINDOW_BASE
]);
1503 (*regs
)[TARGET_REG_THREADPTR
] = tswapreg(env
->uregs
[THREADPTR
]);
1504 xtensa_sync_phys_from_window((CPUXtensaState
*)env
);
1505 for (i
= 0; i
< env
->config
->nareg
; ++i
) {
1506 (*regs
)[TARGET_REG_AR0
+ i
] = tswapreg(env
->phys_regs
[i
]);
1510 #define USE_ELF_CORE_DUMP
1511 #define ELF_EXEC_PAGESIZE 4096
1513 #endif /* TARGET_XTENSA */
1515 #ifndef ELF_PLATFORM
1516 #define ELF_PLATFORM (NULL)
1520 #define ELF_MACHINE ELF_ARCH
1523 #ifndef elf_check_arch
1524 #define elf_check_arch(x) ((x) == ELF_ARCH)
1531 #ifndef STACK_GROWS_DOWN
1532 #define STACK_GROWS_DOWN 1
1535 #ifndef STACK_ALIGNMENT
1536 #define STACK_ALIGNMENT 16
1541 #define ELF_CLASS ELFCLASS32
1543 #define bswaptls(ptr) bswap32s(ptr)
1550 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
1551 unsigned int a_text
; /* length of text, in bytes */
1552 unsigned int a_data
; /* length of data, in bytes */
1553 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
1554 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
1555 unsigned int a_entry
; /* start address */
1556 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
1557 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
1561 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1567 /* Necessary parameters */
1568 #define TARGET_ELF_EXEC_PAGESIZE \
1569 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1570 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1571 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1572 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1573 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1574 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1576 #define DLINFO_ITEMS 16
1578 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
1580 memcpy(to
, from
, n
);
1584 static void bswap_ehdr(struct elfhdr
*ehdr
)
1586 bswap16s(&ehdr
->e_type
); /* Object file type */
1587 bswap16s(&ehdr
->e_machine
); /* Architecture */
1588 bswap32s(&ehdr
->e_version
); /* Object file version */
1589 bswaptls(&ehdr
->e_entry
); /* Entry point virtual address */
1590 bswaptls(&ehdr
->e_phoff
); /* Program header table file offset */
1591 bswaptls(&ehdr
->e_shoff
); /* Section header table file offset */
1592 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
1593 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
1594 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
1595 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
1596 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
1597 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
1598 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
1601 static void bswap_phdr(struct elf_phdr
*phdr
, int phnum
)
1604 for (i
= 0; i
< phnum
; ++i
, ++phdr
) {
1605 bswap32s(&phdr
->p_type
); /* Segment type */
1606 bswap32s(&phdr
->p_flags
); /* Segment flags */
1607 bswaptls(&phdr
->p_offset
); /* Segment file offset */
1608 bswaptls(&phdr
->p_vaddr
); /* Segment virtual address */
1609 bswaptls(&phdr
->p_paddr
); /* Segment physical address */
1610 bswaptls(&phdr
->p_filesz
); /* Segment size in file */
1611 bswaptls(&phdr
->p_memsz
); /* Segment size in memory */
1612 bswaptls(&phdr
->p_align
); /* Segment alignment */
1616 static void bswap_shdr(struct elf_shdr
*shdr
, int shnum
)
1619 for (i
= 0; i
< shnum
; ++i
, ++shdr
) {
1620 bswap32s(&shdr
->sh_name
);
1621 bswap32s(&shdr
->sh_type
);
1622 bswaptls(&shdr
->sh_flags
);
1623 bswaptls(&shdr
->sh_addr
);
1624 bswaptls(&shdr
->sh_offset
);
1625 bswaptls(&shdr
->sh_size
);
1626 bswap32s(&shdr
->sh_link
);
1627 bswap32s(&shdr
->sh_info
);
1628 bswaptls(&shdr
->sh_addralign
);
1629 bswaptls(&shdr
->sh_entsize
);
1633 static void bswap_sym(struct elf_sym
*sym
)
1635 bswap32s(&sym
->st_name
);
1636 bswaptls(&sym
->st_value
);
1637 bswaptls(&sym
->st_size
);
1638 bswap16s(&sym
->st_shndx
);
1642 static void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
)
1644 bswap16s(&abiflags
->version
);
1645 bswap32s(&abiflags
->ases
);
1646 bswap32s(&abiflags
->isa_ext
);
1647 bswap32s(&abiflags
->flags1
);
1648 bswap32s(&abiflags
->flags2
);
1652 static inline void bswap_ehdr(struct elfhdr
*ehdr
) { }
1653 static inline void bswap_phdr(struct elf_phdr
*phdr
, int phnum
) { }
1654 static inline void bswap_shdr(struct elf_shdr
*shdr
, int shnum
) { }
1655 static inline void bswap_sym(struct elf_sym
*sym
) { }
1657 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
) { }
1661 #ifdef USE_ELF_CORE_DUMP
1662 static int elf_core_dump(int, const CPUArchState
*);
1663 #endif /* USE_ELF_CORE_DUMP */
1664 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
);
1666 /* Verify the portions of EHDR within E_IDENT for the target.
1667 This can be performed before bswapping the entire header. */
1668 static bool elf_check_ident(struct elfhdr
*ehdr
)
1670 return (ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
1671 && ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
1672 && ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
1673 && ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
1674 && ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
1675 && ehdr
->e_ident
[EI_DATA
] == ELF_DATA
1676 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
);
1679 /* Verify the portions of EHDR outside of E_IDENT for the target.
1680 This has to wait until after bswapping the header. */
1681 static bool elf_check_ehdr(struct elfhdr
*ehdr
)
1683 return (elf_check_arch(ehdr
->e_machine
)
1684 && ehdr
->e_ehsize
== sizeof(struct elfhdr
)
1685 && ehdr
->e_phentsize
== sizeof(struct elf_phdr
)
1686 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
));
1690 * 'copy_elf_strings()' copies argument/envelope strings from user
1691 * memory to free pages in kernel mem. These are in a format ready
1692 * to be put directly into the top of new user memory.
1695 static abi_ulong
copy_elf_strings(int argc
, char **argv
, char *scratch
,
1696 abi_ulong p
, abi_ulong stack_limit
)
1703 return 0; /* bullet-proofing */
1706 if (STACK_GROWS_DOWN
) {
1707 int offset
= ((p
- 1) % TARGET_PAGE_SIZE
) + 1;
1708 for (i
= argc
- 1; i
>= 0; --i
) {
1711 fprintf(stderr
, "VFS: argc is wrong");
1714 len
= strlen(tmp
) + 1;
1717 if (len
> (p
- stack_limit
)) {
1721 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
1722 tmp
-= bytes_to_copy
;
1724 offset
-= bytes_to_copy
;
1725 len
-= bytes_to_copy
;
1727 memcpy_fromfs(scratch
+ offset
, tmp
, bytes_to_copy
);
1730 memcpy_to_target(p
, scratch
, top
- p
);
1732 offset
= TARGET_PAGE_SIZE
;
1737 memcpy_to_target(p
, scratch
+ offset
, top
- p
);
1740 int remaining
= TARGET_PAGE_SIZE
- (p
% TARGET_PAGE_SIZE
);
1741 for (i
= 0; i
< argc
; ++i
) {
1744 fprintf(stderr
, "VFS: argc is wrong");
1747 len
= strlen(tmp
) + 1;
1748 if (len
> (stack_limit
- p
)) {
1752 int bytes_to_copy
= (len
> remaining
) ? remaining
: len
;
1754 memcpy_fromfs(scratch
+ (p
- top
), tmp
, bytes_to_copy
);
1756 tmp
+= bytes_to_copy
;
1757 remaining
-= bytes_to_copy
;
1759 len
-= bytes_to_copy
;
1761 if (remaining
== 0) {
1762 memcpy_to_target(top
, scratch
, p
- top
);
1764 remaining
= TARGET_PAGE_SIZE
;
1769 memcpy_to_target(top
, scratch
, p
- top
);
1776 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1777 * argument/environment space. Newer kernels (>2.6.33) allow more,
1778 * dependent on stack size, but guarantee at least 32 pages for
1779 * backwards compatibility.
1781 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1783 static abi_ulong
setup_arg_pages(struct linux_binprm
*bprm
,
1784 struct image_info
*info
)
1786 abi_ulong size
, error
, guard
;
1788 size
= guest_stack_size
;
1789 if (size
< STACK_LOWER_LIMIT
) {
1790 size
= STACK_LOWER_LIMIT
;
1792 guard
= TARGET_PAGE_SIZE
;
1793 if (guard
< qemu_real_host_page_size
) {
1794 guard
= qemu_real_host_page_size
;
1797 error
= target_mmap(0, size
+ guard
, PROT_READ
| PROT_WRITE
,
1798 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1800 perror("mmap stack");
1804 /* We reserve one extra page at the top of the stack as guard. */
1805 if (STACK_GROWS_DOWN
) {
1806 target_mprotect(error
, guard
, PROT_NONE
);
1807 info
->stack_limit
= error
+ guard
;
1808 return info
->stack_limit
+ size
- sizeof(void *);
1810 target_mprotect(error
+ size
, guard
, PROT_NONE
);
1811 info
->stack_limit
= error
+ size
;
1816 /* Map and zero the bss. We need to explicitly zero any fractional pages
1817 after the data section (i.e. bss). */
1818 static void zero_bss(abi_ulong elf_bss
, abi_ulong last_bss
, int prot
)
1820 uintptr_t host_start
, host_map_start
, host_end
;
1822 last_bss
= TARGET_PAGE_ALIGN(last_bss
);
1824 /* ??? There is confusion between qemu_real_host_page_size and
1825 qemu_host_page_size here and elsewhere in target_mmap, which
1826 may lead to the end of the data section mapping from the file
1827 not being mapped. At least there was an explicit test and
1828 comment for that here, suggesting that "the file size must
1829 be known". The comment probably pre-dates the introduction
1830 of the fstat system call in target_mmap which does in fact
1831 find out the size. What isn't clear is if the workaround
1832 here is still actually needed. For now, continue with it,
1833 but merge it with the "normal" mmap that would allocate the bss. */
1835 host_start
= (uintptr_t) g2h(elf_bss
);
1836 host_end
= (uintptr_t) g2h(last_bss
);
1837 host_map_start
= REAL_HOST_PAGE_ALIGN(host_start
);
1839 if (host_map_start
< host_end
) {
1840 void *p
= mmap((void *)host_map_start
, host_end
- host_map_start
,
1841 prot
, MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1842 if (p
== MAP_FAILED
) {
1843 perror("cannot mmap brk");
1848 /* Ensure that the bss page(s) are valid */
1849 if ((page_get_flags(last_bss
-1) & prot
) != prot
) {
1850 page_set_flags(elf_bss
& TARGET_PAGE_MASK
, last_bss
, prot
| PAGE_VALID
);
1853 if (host_start
< host_map_start
) {
1854 memset((void *)host_start
, 0, host_map_start
- host_start
);
1859 static int elf_is_fdpic(struct elfhdr
*exec
)
1861 return exec
->e_ident
[EI_OSABI
] == ELFOSABI_ARM_FDPIC
;
1864 /* Default implementation, always false. */
1865 static int elf_is_fdpic(struct elfhdr
*exec
)
1871 static abi_ulong
loader_build_fdpic_loadmap(struct image_info
*info
, abi_ulong sp
)
1874 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
;
1876 /* elf32_fdpic_loadseg */
1880 put_user_u32(loadsegs
[n
].addr
, sp
+0);
1881 put_user_u32(loadsegs
[n
].p_vaddr
, sp
+4);
1882 put_user_u32(loadsegs
[n
].p_memsz
, sp
+8);
1885 /* elf32_fdpic_loadmap */
1887 put_user_u16(0, sp
+0); /* version */
1888 put_user_u16(info
->nsegs
, sp
+2); /* nsegs */
1890 info
->personality
= PER_LINUX_FDPIC
;
1891 info
->loadmap_addr
= sp
;
1896 static abi_ulong
create_elf_tables(abi_ulong p
, int argc
, int envc
,
1897 struct elfhdr
*exec
,
1898 struct image_info
*info
,
1899 struct image_info
*interp_info
)
1902 abi_ulong u_argc
, u_argv
, u_envp
, u_auxv
;
1905 abi_ulong u_rand_bytes
;
1906 uint8_t k_rand_bytes
[16];
1907 abi_ulong u_platform
;
1908 const char *k_platform
;
1909 const int n
= sizeof(elf_addr_t
);
1913 /* Needs to be before we load the env/argc/... */
1914 if (elf_is_fdpic(exec
)) {
1915 /* Need 4 byte alignment for these structs */
1917 sp
= loader_build_fdpic_loadmap(info
, sp
);
1918 info
->other_info
= interp_info
;
1920 interp_info
->other_info
= info
;
1921 sp
= loader_build_fdpic_loadmap(interp_info
, sp
);
1922 info
->interpreter_loadmap_addr
= interp_info
->loadmap_addr
;
1923 info
->interpreter_pt_dynamic_addr
= interp_info
->pt_dynamic_addr
;
1925 info
->interpreter_loadmap_addr
= 0;
1926 info
->interpreter_pt_dynamic_addr
= 0;
1931 k_platform
= ELF_PLATFORM
;
1933 size_t len
= strlen(k_platform
) + 1;
1934 if (STACK_GROWS_DOWN
) {
1935 sp
-= (len
+ n
- 1) & ~(n
- 1);
1937 /* FIXME - check return value of memcpy_to_target() for failure */
1938 memcpy_to_target(sp
, k_platform
, len
);
1940 memcpy_to_target(sp
, k_platform
, len
);
1946 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1947 * the argv and envp pointers.
1949 if (STACK_GROWS_DOWN
) {
1950 sp
= QEMU_ALIGN_DOWN(sp
, 16);
1952 sp
= QEMU_ALIGN_UP(sp
, 16);
1956 * Generate 16 random bytes for userspace PRNG seeding.
1958 qemu_guest_getrandom_nofail(k_rand_bytes
, sizeof(k_rand_bytes
));
1959 if (STACK_GROWS_DOWN
) {
1962 /* FIXME - check return value of memcpy_to_target() for failure */
1963 memcpy_to_target(sp
, k_rand_bytes
, 16);
1965 memcpy_to_target(sp
, k_rand_bytes
, 16);
1970 size
= (DLINFO_ITEMS
+ 1) * 2;
1973 #ifdef DLINFO_ARCH_ITEMS
1974 size
+= DLINFO_ARCH_ITEMS
* 2;
1979 info
->auxv_len
= size
* n
;
1981 size
+= envc
+ argc
+ 2;
1982 size
+= 1; /* argc itself */
1985 /* Allocate space and finalize stack alignment for entry now. */
1986 if (STACK_GROWS_DOWN
) {
1987 u_argc
= QEMU_ALIGN_DOWN(sp
- size
, STACK_ALIGNMENT
);
1991 sp
= QEMU_ALIGN_UP(sp
+ size
, STACK_ALIGNMENT
);
1994 u_argv
= u_argc
+ n
;
1995 u_envp
= u_argv
+ (argc
+ 1) * n
;
1996 u_auxv
= u_envp
+ (envc
+ 1) * n
;
1997 info
->saved_auxv
= u_auxv
;
1998 info
->arg_start
= u_argv
;
1999 info
->arg_end
= u_argv
+ argc
* n
;
2001 /* This is correct because Linux defines
2002 * elf_addr_t as Elf32_Off / Elf64_Off
2004 #define NEW_AUX_ENT(id, val) do { \
2005 put_user_ual(id, u_auxv); u_auxv += n; \
2006 put_user_ual(val, u_auxv); u_auxv += n; \
2011 * ARCH_DLINFO must come first so platform specific code can enforce
2012 * special alignment requirements on the AUXV if necessary (eg. PPC).
2016 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2017 * on info->auxv_len will trigger.
2019 NEW_AUX_ENT(AT_PHDR
, (abi_ulong
)(info
->load_addr
+ exec
->e_phoff
));
2020 NEW_AUX_ENT(AT_PHENT
, (abi_ulong
)(sizeof (struct elf_phdr
)));
2021 NEW_AUX_ENT(AT_PHNUM
, (abi_ulong
)(exec
->e_phnum
));
2022 if ((info
->alignment
& ~qemu_host_page_mask
) != 0) {
2023 /* Target doesn't support host page size alignment */
2024 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(TARGET_PAGE_SIZE
));
2026 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(MAX(TARGET_PAGE_SIZE
,
2027 qemu_host_page_size
)));
2029 NEW_AUX_ENT(AT_BASE
, (abi_ulong
)(interp_info
? interp_info
->load_addr
: 0));
2030 NEW_AUX_ENT(AT_FLAGS
, (abi_ulong
)0);
2031 NEW_AUX_ENT(AT_ENTRY
, info
->entry
);
2032 NEW_AUX_ENT(AT_UID
, (abi_ulong
) getuid());
2033 NEW_AUX_ENT(AT_EUID
, (abi_ulong
) geteuid());
2034 NEW_AUX_ENT(AT_GID
, (abi_ulong
) getgid());
2035 NEW_AUX_ENT(AT_EGID
, (abi_ulong
) getegid());
2036 NEW_AUX_ENT(AT_HWCAP
, (abi_ulong
) ELF_HWCAP
);
2037 NEW_AUX_ENT(AT_CLKTCK
, (abi_ulong
) sysconf(_SC_CLK_TCK
));
2038 NEW_AUX_ENT(AT_RANDOM
, (abi_ulong
) u_rand_bytes
);
2039 NEW_AUX_ENT(AT_SECURE
, (abi_ulong
) qemu_getauxval(AT_SECURE
));
2040 NEW_AUX_ENT(AT_EXECFN
, info
->file_string
);
2043 NEW_AUX_ENT(AT_HWCAP2
, (abi_ulong
) ELF_HWCAP2
);
2047 NEW_AUX_ENT(AT_PLATFORM
, u_platform
);
2049 NEW_AUX_ENT (AT_NULL
, 0);
2052 /* Check that our initial calculation of the auxv length matches how much
2053 * we actually put into it.
2055 assert(info
->auxv_len
== u_auxv
- info
->saved_auxv
);
2057 put_user_ual(argc
, u_argc
);
2059 p
= info
->arg_strings
;
2060 for (i
= 0; i
< argc
; ++i
) {
2061 put_user_ual(p
, u_argv
);
2063 p
+= target_strlen(p
) + 1;
2065 put_user_ual(0, u_argv
);
2067 p
= info
->env_strings
;
2068 for (i
= 0; i
< envc
; ++i
) {
2069 put_user_ual(p
, u_envp
);
2071 p
+= target_strlen(p
) + 1;
2073 put_user_ual(0, u_envp
);
2078 unsigned long init_guest_space(unsigned long host_start
,
2079 unsigned long host_size
,
2080 unsigned long guest_start
,
2083 /* In order to use host shmat, we must be able to honor SHMLBA. */
2084 unsigned long align
= MAX(SHMLBA
, qemu_host_page_size
);
2085 unsigned long current_start
, aligned_start
;
2088 assert(host_start
|| host_size
);
2090 /* If just a starting address is given, then just verify that
2092 if (host_start
&& !host_size
) {
2093 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2094 if (init_guest_commpage(host_start
, host_size
) != 1) {
2095 return (unsigned long)-1;
2101 /* Setup the initial flags and start address. */
2102 current_start
= host_start
& -align
;
2103 flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
;
2108 /* Otherwise, a non-zero size region of memory needs to be mapped
2111 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2112 /* On 32-bit ARM, we need to map not just the usable memory, but
2113 * also the commpage. Try to find a suitable place by allocating
2114 * a big chunk for all of it. If host_start, then the naive
2115 * strategy probably does good enough.
2118 unsigned long guest_full_size
, host_full_size
, real_start
;
2121 (0xffff0f00 & qemu_host_page_mask
) + qemu_host_page_size
;
2122 host_full_size
= guest_full_size
- guest_start
;
2123 real_start
= (unsigned long)
2124 mmap(NULL
, host_full_size
, PROT_NONE
, flags
, -1, 0);
2125 if (real_start
== (unsigned long)-1) {
2126 if (host_size
< host_full_size
- qemu_host_page_size
) {
2127 /* We failed to map a continous segment, but we're
2128 * allowed to have a gap between the usable memory and
2129 * the commpage where other things can be mapped.
2130 * This sparseness gives us more flexibility to find
2135 return (unsigned long)-1;
2137 munmap((void *)real_start
, host_full_size
);
2138 if (real_start
& (align
- 1)) {
2139 /* The same thing again, but with extra
2140 * so that we can shift around alignment.
2142 unsigned long real_size
= host_full_size
+ qemu_host_page_size
;
2143 real_start
= (unsigned long)
2144 mmap(NULL
, real_size
, PROT_NONE
, flags
, -1, 0);
2145 if (real_start
== (unsigned long)-1) {
2146 if (host_size
< host_full_size
- qemu_host_page_size
) {
2149 return (unsigned long)-1;
2151 munmap((void *)real_start
, real_size
);
2152 real_start
= ROUND_UP(real_start
, align
);
2154 current_start
= real_start
;
2160 unsigned long real_start
, real_size
, aligned_size
;
2161 aligned_size
= real_size
= host_size
;
2163 /* Do not use mmap_find_vma here because that is limited to the
2164 * guest address space. We are going to make the
2165 * guest address space fit whatever we're given.
2167 real_start
= (unsigned long)
2168 mmap((void *)current_start
, host_size
, PROT_NONE
, flags
, -1, 0);
2169 if (real_start
== (unsigned long)-1) {
2170 return (unsigned long)-1;
2173 /* Check to see if the address is valid. */
2174 if (host_start
&& real_start
!= current_start
) {
2175 qemu_log_mask(CPU_LOG_PAGE
, "invalid %lx && %lx != %lx\n",
2176 host_start
, real_start
, current_start
);
2180 /* Ensure the address is properly aligned. */
2181 if (real_start
& (align
- 1)) {
2182 /* Ideally, we adjust like
2184 * pages: [ ][ ][ ][ ][ ]
2190 * But if there is something else mapped right after it,
2191 * then obviously it won't have room to grow, and the
2192 * kernel will put the new larger real someplace else with
2193 * unknown alignment (if we made it to here, then
2194 * fixed=false). Which is why we grow real by a full page
2195 * size, instead of by part of one; so that even if we get
2196 * moved, we can still guarantee alignment. But this does
2197 * mean that there is a padding of < 1 page both before
2198 * and after the aligned range; the "after" could could
2199 * cause problems for ARM emulation where it could butt in
2200 * to where we need to put the commpage.
2202 munmap((void *)real_start
, host_size
);
2203 real_size
= aligned_size
+ align
;
2204 real_start
= (unsigned long)
2205 mmap((void *)real_start
, real_size
, PROT_NONE
, flags
, -1, 0);
2206 if (real_start
== (unsigned long)-1) {
2207 return (unsigned long)-1;
2209 aligned_start
= ROUND_UP(real_start
, align
);
2211 aligned_start
= real_start
;
2214 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2215 /* On 32-bit ARM, we need to also be able to map the commpage. */
2216 int valid
= init_guest_commpage(aligned_start
- guest_start
,
2217 aligned_size
+ guest_start
);
2219 munmap((void *)real_start
, real_size
);
2220 return (unsigned long)-1;
2221 } else if (valid
== 0) {
2226 /* If nothing has said `return -1` or `goto try_again` yet,
2227 * then the address we have is good.
2232 /* That address didn't work. Unmap and try a different one.
2233 * The address the host picked because is typically right at
2234 * the top of the host address space and leaves the guest with
2235 * no usable address space. Resort to a linear search. We
2236 * already compensated for mmap_min_addr, so this should not
2237 * happen often. Probably means we got unlucky and host
2238 * address space randomization put a shared library somewhere
2241 * This is probably a good strategy if host_start, but is
2242 * probably a bad strategy if not, which means we got here
2243 * because of trouble with ARM commpage setup.
2245 if (munmap((void *)real_start
, real_size
) != 0) {
2246 error_report("%s: failed to unmap %lx:%lx (%s)", __func__
,
2247 real_start
, real_size
, strerror(errno
));
2250 current_start
+= align
;
2251 if (host_start
== current_start
) {
2252 /* Theoretically possible if host doesn't have any suitably
2253 * aligned areas. Normally the first mmap will fail.
2255 return (unsigned long)-1;
2259 qemu_log_mask(CPU_LOG_PAGE
, "Reserved 0x%lx bytes of guest address space\n", host_size
);
2261 return aligned_start
;
2264 static void probe_guest_base(const char *image_name
,
2265 abi_ulong loaddr
, abi_ulong hiaddr
)
2267 /* Probe for a suitable guest base address, if the user has not set
2268 * it explicitly, and set guest_base appropriately.
2269 * In case of error we will print a suitable message and exit.
2272 if (!have_guest_base
&& !reserved_va
) {
2273 unsigned long host_start
, real_start
, host_size
;
2275 /* Round addresses to page boundaries. */
2276 loaddr
&= qemu_host_page_mask
;
2277 hiaddr
= HOST_PAGE_ALIGN(hiaddr
);
2279 if (loaddr
< mmap_min_addr
) {
2280 host_start
= HOST_PAGE_ALIGN(mmap_min_addr
);
2282 host_start
= loaddr
;
2283 if (host_start
!= loaddr
) {
2284 errmsg
= "Address overflow loading ELF binary";
2288 host_size
= hiaddr
- loaddr
;
2290 /* Setup the initial guest memory space with ranges gleaned from
2291 * the ELF image that is being loaded.
2293 real_start
= init_guest_space(host_start
, host_size
, loaddr
, false);
2294 if (real_start
== (unsigned long)-1) {
2295 errmsg
= "Unable to find space for application";
2298 guest_base
= real_start
- loaddr
;
2300 qemu_log_mask(CPU_LOG_PAGE
, "Relocating guest address space from 0x"
2301 TARGET_ABI_FMT_lx
" to 0x%lx\n",
2302 loaddr
, real_start
);
2307 fprintf(stderr
, "%s: %s\n", image_name
, errmsg
);
2312 /* Load an ELF image into the address space.
2314 IMAGE_NAME is the filename of the image, to use in error messages.
2315 IMAGE_FD is the open file descriptor for the image.
2317 BPRM_BUF is a copy of the beginning of the file; this of course
2318 contains the elf file header at offset 0. It is assumed that this
2319 buffer is sufficiently aligned to present no problems to the host
2320 in accessing data at aligned offsets within the buffer.
2322 On return: INFO values will be filled in, as necessary or available. */
2324 static void load_elf_image(const char *image_name
, int image_fd
,
2325 struct image_info
*info
, char **pinterp_name
,
2326 char bprm_buf
[BPRM_BUF_SIZE
])
2328 struct elfhdr
*ehdr
= (struct elfhdr
*)bprm_buf
;
2329 struct elf_phdr
*phdr
;
2330 abi_ulong load_addr
, load_bias
, loaddr
, hiaddr
, error
;
2334 /* First of all, some simple consistency checks */
2335 errmsg
= "Invalid ELF image for this architecture";
2336 if (!elf_check_ident(ehdr
)) {
2340 if (!elf_check_ehdr(ehdr
)) {
2344 i
= ehdr
->e_phnum
* sizeof(struct elf_phdr
);
2345 if (ehdr
->e_phoff
+ i
<= BPRM_BUF_SIZE
) {
2346 phdr
= (struct elf_phdr
*)(bprm_buf
+ ehdr
->e_phoff
);
2348 phdr
= (struct elf_phdr
*) alloca(i
);
2349 retval
= pread(image_fd
, phdr
, i
, ehdr
->e_phoff
);
2354 bswap_phdr(phdr
, ehdr
->e_phnum
);
2357 info
->pt_dynamic_addr
= 0;
2361 /* Find the maximum size of the image and allocate an appropriate
2362 amount of memory to handle that. */
2363 loaddr
= -1, hiaddr
= 0;
2364 info
->alignment
= 0;
2365 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
2366 if (phdr
[i
].p_type
== PT_LOAD
) {
2367 abi_ulong a
= phdr
[i
].p_vaddr
- phdr
[i
].p_offset
;
2371 a
= phdr
[i
].p_vaddr
+ phdr
[i
].p_memsz
;
2376 info
->alignment
|= phdr
[i
].p_align
;
2380 if (pinterp_name
!= NULL
) {
2382 * This is the main executable.
2384 * Reserve extra space for brk.
2385 * We hold on to this space while placing the interpreter
2386 * and the stack, lest they be placed immediately after
2387 * the data segment and block allocation from the brk.
2389 * 16MB is chosen as "large enough" without being so large
2390 * as to allow the result to not fit with a 32-bit guest on
2393 info
->reserve_brk
= 16 * MiB
;
2394 hiaddr
+= info
->reserve_brk
;
2396 if (ehdr
->e_type
== ET_EXEC
) {
2398 * Make sure that the low address does not conflict with
2399 * MMAP_MIN_ADDR or the QEMU application itself.
2401 probe_guest_base(image_name
, loaddr
, hiaddr
);
2406 * Reserve address space for all of this.
2408 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
2409 * exactly the address range that is required.
2411 * Otherwise this is ET_DYN, and we are searching for a location
2412 * that can hold the memory space required. If the image is
2413 * pre-linked, LOADDR will be non-zero, and the kernel should
2414 * honor that address if it happens to be free.
2416 * In both cases, we will overwrite pages in this range with mappings
2417 * from the executable.
2419 load_addr
= target_mmap(loaddr
, hiaddr
- loaddr
, PROT_NONE
,
2420 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
|
2421 (ehdr
->e_type
== ET_EXEC
? MAP_FIXED
: 0),
2423 if (load_addr
== -1) {
2426 load_bias
= load_addr
- loaddr
;
2428 if (elf_is_fdpic(ehdr
)) {
2429 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
=
2430 g_malloc(sizeof(*loadsegs
) * info
->nsegs
);
2432 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
2433 switch (phdr
[i
].p_type
) {
2435 info
->pt_dynamic_addr
= phdr
[i
].p_vaddr
+ load_bias
;
2438 loadsegs
->addr
= phdr
[i
].p_vaddr
+ load_bias
;
2439 loadsegs
->p_vaddr
= phdr
[i
].p_vaddr
;
2440 loadsegs
->p_memsz
= phdr
[i
].p_memsz
;
2447 info
->load_bias
= load_bias
;
2448 info
->code_offset
= load_bias
;
2449 info
->data_offset
= load_bias
;
2450 info
->load_addr
= load_addr
;
2451 info
->entry
= ehdr
->e_entry
+ load_bias
;
2452 info
->start_code
= -1;
2454 info
->start_data
= -1;
2457 info
->elf_flags
= ehdr
->e_flags
;
2459 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
2460 struct elf_phdr
*eppnt
= phdr
+ i
;
2461 if (eppnt
->p_type
== PT_LOAD
) {
2462 abi_ulong vaddr
, vaddr_po
, vaddr_ps
, vaddr_ef
, vaddr_em
, vaddr_len
;
2465 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
2466 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
2467 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
2469 vaddr
= load_bias
+ eppnt
->p_vaddr
;
2470 vaddr_po
= TARGET_ELF_PAGEOFFSET(vaddr
);
2471 vaddr_ps
= TARGET_ELF_PAGESTART(vaddr
);
2472 vaddr_len
= TARGET_ELF_PAGELENGTH(eppnt
->p_filesz
+ vaddr_po
);
2475 * Some segments may be completely empty without any backing file
2476 * segment, in that case just let zero_bss allocate an empty buffer
2479 if (eppnt
->p_filesz
!= 0) {
2480 error
= target_mmap(vaddr_ps
, vaddr_len
, elf_prot
,
2481 MAP_PRIVATE
| MAP_FIXED
,
2482 image_fd
, eppnt
->p_offset
- vaddr_po
);
2489 vaddr_ef
= vaddr
+ eppnt
->p_filesz
;
2490 vaddr_em
= vaddr
+ eppnt
->p_memsz
;
2492 /* If the load segment requests extra zeros (e.g. bss), map it. */
2493 if (vaddr_ef
< vaddr_em
) {
2494 zero_bss(vaddr_ef
, vaddr_em
, elf_prot
);
2497 /* Find the full program boundaries. */
2498 if (elf_prot
& PROT_EXEC
) {
2499 if (vaddr
< info
->start_code
) {
2500 info
->start_code
= vaddr
;
2502 if (vaddr_ef
> info
->end_code
) {
2503 info
->end_code
= vaddr_ef
;
2506 if (elf_prot
& PROT_WRITE
) {
2507 if (vaddr
< info
->start_data
) {
2508 info
->start_data
= vaddr
;
2510 if (vaddr_ef
> info
->end_data
) {
2511 info
->end_data
= vaddr_ef
;
2513 if (vaddr_em
> info
->brk
) {
2514 info
->brk
= vaddr_em
;
2517 } else if (eppnt
->p_type
== PT_INTERP
&& pinterp_name
) {
2520 if (*pinterp_name
) {
2521 errmsg
= "Multiple PT_INTERP entries";
2524 interp_name
= malloc(eppnt
->p_filesz
);
2529 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
2530 memcpy(interp_name
, bprm_buf
+ eppnt
->p_offset
,
2533 retval
= pread(image_fd
, interp_name
, eppnt
->p_filesz
,
2535 if (retval
!= eppnt
->p_filesz
) {
2539 if (interp_name
[eppnt
->p_filesz
- 1] != 0) {
2540 errmsg
= "Invalid PT_INTERP entry";
2543 *pinterp_name
= interp_name
;
2545 } else if (eppnt
->p_type
== PT_MIPS_ABIFLAGS
) {
2546 Mips_elf_abiflags_v0 abiflags
;
2547 if (eppnt
->p_filesz
< sizeof(Mips_elf_abiflags_v0
)) {
2548 errmsg
= "Invalid PT_MIPS_ABIFLAGS entry";
2551 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
2552 memcpy(&abiflags
, bprm_buf
+ eppnt
->p_offset
,
2553 sizeof(Mips_elf_abiflags_v0
));
2555 retval
= pread(image_fd
, &abiflags
, sizeof(Mips_elf_abiflags_v0
),
2557 if (retval
!= sizeof(Mips_elf_abiflags_v0
)) {
2561 bswap_mips_abiflags(&abiflags
);
2562 info
->fp_abi
= abiflags
.fp_abi
;
2567 if (info
->end_data
== 0) {
2568 info
->start_data
= info
->end_code
;
2569 info
->end_data
= info
->end_code
;
2570 info
->brk
= info
->end_code
;
2573 if (qemu_log_enabled()) {
2574 load_symbols(ehdr
, image_fd
, load_bias
);
2584 errmsg
= "Incomplete read of file header";
2588 errmsg
= strerror(errno
);
2590 fprintf(stderr
, "%s: %s\n", image_name
, errmsg
);
2594 static void load_elf_interp(const char *filename
, struct image_info
*info
,
2595 char bprm_buf
[BPRM_BUF_SIZE
])
2599 fd
= open(path(filename
), O_RDONLY
);
2604 retval
= read(fd
, bprm_buf
, BPRM_BUF_SIZE
);
2608 if (retval
< BPRM_BUF_SIZE
) {
2609 memset(bprm_buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
2612 load_elf_image(filename
, fd
, info
, NULL
, bprm_buf
);
2616 fprintf(stderr
, "%s: %s\n", filename
, strerror(errno
));
2620 static int symfind(const void *s0
, const void *s1
)
2622 target_ulong addr
= *(target_ulong
*)s0
;
2623 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
2625 if (addr
< sym
->st_value
) {
2627 } else if (addr
>= sym
->st_value
+ sym
->st_size
) {
2633 static const char *lookup_symbolxx(struct syminfo
*s
, target_ulong orig_addr
)
2635 #if ELF_CLASS == ELFCLASS32
2636 struct elf_sym
*syms
= s
->disas_symtab
.elf32
;
2638 struct elf_sym
*syms
= s
->disas_symtab
.elf64
;
2642 struct elf_sym
*sym
;
2644 sym
= bsearch(&orig_addr
, syms
, s
->disas_num_syms
, sizeof(*syms
), symfind
);
2646 return s
->disas_strtab
+ sym
->st_name
;
2652 /* FIXME: This should use elf_ops.h */
2653 static int symcmp(const void *s0
, const void *s1
)
2655 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
2656 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
2657 return (sym0
->st_value
< sym1
->st_value
)
2659 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
2662 /* Best attempt to load symbols from this ELF object. */
2663 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
)
2665 int i
, shnum
, nsyms
, sym_idx
= 0, str_idx
= 0;
2667 struct elf_shdr
*shdr
;
2668 char *strings
= NULL
;
2669 struct syminfo
*s
= NULL
;
2670 struct elf_sym
*new_syms
, *syms
= NULL
;
2672 shnum
= hdr
->e_shnum
;
2673 i
= shnum
* sizeof(struct elf_shdr
);
2674 shdr
= (struct elf_shdr
*)alloca(i
);
2675 if (pread(fd
, shdr
, i
, hdr
->e_shoff
) != i
) {
2679 bswap_shdr(shdr
, shnum
);
2680 for (i
= 0; i
< shnum
; ++i
) {
2681 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
2683 str_idx
= shdr
[i
].sh_link
;
2688 /* There will be no symbol table if the file was stripped. */
2692 /* Now know where the strtab and symtab are. Snarf them. */
2693 s
= g_try_new(struct syminfo
, 1);
2698 segsz
= shdr
[str_idx
].sh_size
;
2699 s
->disas_strtab
= strings
= g_try_malloc(segsz
);
2701 pread(fd
, strings
, segsz
, shdr
[str_idx
].sh_offset
) != segsz
) {
2705 segsz
= shdr
[sym_idx
].sh_size
;
2706 syms
= g_try_malloc(segsz
);
2707 if (!syms
|| pread(fd
, syms
, segsz
, shdr
[sym_idx
].sh_offset
) != segsz
) {
2711 if (segsz
/ sizeof(struct elf_sym
) > INT_MAX
) {
2712 /* Implausibly large symbol table: give up rather than ploughing
2713 * on with the number of symbols calculation overflowing
2717 nsyms
= segsz
/ sizeof(struct elf_sym
);
2718 for (i
= 0; i
< nsyms
; ) {
2719 bswap_sym(syms
+ i
);
2720 /* Throw away entries which we do not need. */
2721 if (syms
[i
].st_shndx
== SHN_UNDEF
2722 || syms
[i
].st_shndx
>= SHN_LORESERVE
2723 || ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
2725 syms
[i
] = syms
[nsyms
];
2728 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2729 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2730 syms
[i
].st_value
&= ~(target_ulong
)1;
2732 syms
[i
].st_value
+= load_bias
;
2737 /* No "useful" symbol. */
2742 /* Attempt to free the storage associated with the local symbols
2743 that we threw away. Whether or not this has any effect on the
2744 memory allocation depends on the malloc implementation and how
2745 many symbols we managed to discard. */
2746 new_syms
= g_try_renew(struct elf_sym
, syms
, nsyms
);
2747 if (new_syms
== NULL
) {
2752 qsort(syms
, nsyms
, sizeof(*syms
), symcmp
);
2754 s
->disas_num_syms
= nsyms
;
2755 #if ELF_CLASS == ELFCLASS32
2756 s
->disas_symtab
.elf32
= syms
;
2758 s
->disas_symtab
.elf64
= syms
;
2760 s
->lookup_symbol
= lookup_symbolxx
;
2772 uint32_t get_elf_eflags(int fd
)
2778 /* Read ELF header */
2779 offset
= lseek(fd
, 0, SEEK_SET
);
2780 if (offset
== (off_t
) -1) {
2783 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
2784 if (ret
< sizeof(ehdr
)) {
2787 offset
= lseek(fd
, offset
, SEEK_SET
);
2788 if (offset
== (off_t
) -1) {
2792 /* Check ELF signature */
2793 if (!elf_check_ident(&ehdr
)) {
2799 if (!elf_check_ehdr(&ehdr
)) {
2803 /* return architecture id */
2804 return ehdr
.e_flags
;
2807 int load_elf_binary(struct linux_binprm
*bprm
, struct image_info
*info
)
2809 struct image_info interp_info
;
2810 struct elfhdr elf_ex
;
2811 char *elf_interpreter
= NULL
;
2814 memset(&interp_info
, 0, sizeof(interp_info
));
2816 interp_info
.fp_abi
= MIPS_ABI_FP_UNKNOWN
;
2819 info
->start_mmap
= (abi_ulong
)ELF_START_MMAP
;
2821 load_elf_image(bprm
->filename
, bprm
->fd
, info
,
2822 &elf_interpreter
, bprm
->buf
);
2824 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2825 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2826 when we load the interpreter. */
2827 elf_ex
= *(struct elfhdr
*)bprm
->buf
;
2829 /* Do this so that we can load the interpreter, if need be. We will
2830 change some of these later */
2831 bprm
->p
= setup_arg_pages(bprm
, info
);
2833 scratch
= g_new0(char, TARGET_PAGE_SIZE
);
2834 if (STACK_GROWS_DOWN
) {
2835 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
2836 bprm
->p
, info
->stack_limit
);
2837 info
->file_string
= bprm
->p
;
2838 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
2839 bprm
->p
, info
->stack_limit
);
2840 info
->env_strings
= bprm
->p
;
2841 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
2842 bprm
->p
, info
->stack_limit
);
2843 info
->arg_strings
= bprm
->p
;
2845 info
->arg_strings
= bprm
->p
;
2846 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
2847 bprm
->p
, info
->stack_limit
);
2848 info
->env_strings
= bprm
->p
;
2849 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
2850 bprm
->p
, info
->stack_limit
);
2851 info
->file_string
= bprm
->p
;
2852 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
2853 bprm
->p
, info
->stack_limit
);
2859 fprintf(stderr
, "%s: %s\n", bprm
->filename
, strerror(E2BIG
));
2863 if (elf_interpreter
) {
2864 load_elf_interp(elf_interpreter
, &interp_info
, bprm
->buf
);
2866 /* If the program interpreter is one of these two, then assume
2867 an iBCS2 image. Otherwise assume a native linux image. */
2869 if (strcmp(elf_interpreter
, "/usr/lib/libc.so.1") == 0
2870 || strcmp(elf_interpreter
, "/usr/lib/ld.so.1") == 0) {
2871 info
->personality
= PER_SVR4
;
2873 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2874 and some applications "depend" upon this behavior. Since
2875 we do not have the power to recompile these, we emulate
2876 the SVr4 behavior. Sigh. */
2877 target_mmap(0, qemu_host_page_size
, PROT_READ
| PROT_EXEC
,
2878 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2881 info
->interp_fp_abi
= interp_info
.fp_abi
;
2885 bprm
->p
= create_elf_tables(bprm
->p
, bprm
->argc
, bprm
->envc
, &elf_ex
,
2886 info
, (elf_interpreter
? &interp_info
: NULL
));
2887 info
->start_stack
= bprm
->p
;
2889 /* If we have an interpreter, set that as the program's entry point.
2890 Copy the load_bias as well, to help PPC64 interpret the entry
2891 point as a function descriptor. Do this after creating elf tables
2892 so that we copy the original program entry point into the AUXV. */
2893 if (elf_interpreter
) {
2894 info
->load_bias
= interp_info
.load_bias
;
2895 info
->entry
= interp_info
.entry
;
2896 free(elf_interpreter
);
2899 #ifdef USE_ELF_CORE_DUMP
2900 bprm
->core_dump
= &elf_core_dump
;
2904 * If we reserved extra space for brk, release it now.
2905 * The implementation of do_brk in syscalls.c expects to be able
2906 * to mmap pages in this space.
2908 if (info
->reserve_brk
) {
2909 abi_ulong start_brk
= HOST_PAGE_ALIGN(info
->brk
);
2910 abi_ulong end_brk
= HOST_PAGE_ALIGN(info
->brk
+ info
->reserve_brk
);
2911 target_munmap(start_brk
, end_brk
- start_brk
);
2917 #ifdef USE_ELF_CORE_DUMP
2919 * Definitions to generate Intel SVR4-like core files.
2920 * These mostly have the same names as the SVR4 types with "target_elf_"
2921 * tacked on the front to prevent clashes with linux definitions,
2922 * and the typedef forms have been avoided. This is mostly like
2923 * the SVR4 structure, but more Linuxy, with things that Linux does
2924 * not support and which gdb doesn't really use excluded.
2926 * Fields we don't dump (their contents is zero) in linux-user qemu
2927 * are marked with XXX.
2929 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2931 * Porting ELF coredump for target is (quite) simple process. First you
2932 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2933 * the target resides):
2935 * #define USE_ELF_CORE_DUMP
2937 * Next you define type of register set used for dumping. ELF specification
2938 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2940 * typedef <target_regtype> target_elf_greg_t;
2941 * #define ELF_NREG <number of registers>
2942 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2944 * Last step is to implement target specific function that copies registers
2945 * from given cpu into just specified register set. Prototype is:
2947 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2948 * const CPUArchState *env);
2951 * regs - copy register values into here (allocated and zeroed by caller)
2952 * env - copy registers from here
2954 * Example for ARM target is provided in this file.
2957 /* An ELF note in memory */
2961 size_t namesz_rounded
;
2964 size_t datasz_rounded
;
2969 struct target_elf_siginfo
{
2970 abi_int si_signo
; /* signal number */
2971 abi_int si_code
; /* extra code */
2972 abi_int si_errno
; /* errno */
2975 struct target_elf_prstatus
{
2976 struct target_elf_siginfo pr_info
; /* Info associated with signal */
2977 abi_short pr_cursig
; /* Current signal */
2978 abi_ulong pr_sigpend
; /* XXX */
2979 abi_ulong pr_sighold
; /* XXX */
2980 target_pid_t pr_pid
;
2981 target_pid_t pr_ppid
;
2982 target_pid_t pr_pgrp
;
2983 target_pid_t pr_sid
;
2984 struct target_timeval pr_utime
; /* XXX User time */
2985 struct target_timeval pr_stime
; /* XXX System time */
2986 struct target_timeval pr_cutime
; /* XXX Cumulative user time */
2987 struct target_timeval pr_cstime
; /* XXX Cumulative system time */
2988 target_elf_gregset_t pr_reg
; /* GP registers */
2989 abi_int pr_fpvalid
; /* XXX */
2992 #define ELF_PRARGSZ (80) /* Number of chars for args */
2994 struct target_elf_prpsinfo
{
2995 char pr_state
; /* numeric process state */
2996 char pr_sname
; /* char for pr_state */
2997 char pr_zomb
; /* zombie */
2998 char pr_nice
; /* nice val */
2999 abi_ulong pr_flag
; /* flags */
3000 target_uid_t pr_uid
;
3001 target_gid_t pr_gid
;
3002 target_pid_t pr_pid
, pr_ppid
, pr_pgrp
, pr_sid
;
3004 char pr_fname
[16] QEMU_NONSTRING
; /* filename of executable */
3005 char pr_psargs
[ELF_PRARGSZ
]; /* initial part of arg list */
3008 /* Here is the structure in which status of each thread is captured. */
3009 struct elf_thread_status
{
3010 QTAILQ_ENTRY(elf_thread_status
) ets_link
;
3011 struct target_elf_prstatus prstatus
; /* NT_PRSTATUS */
3013 elf_fpregset_t fpu
; /* NT_PRFPREG */
3014 struct task_struct
*thread
;
3015 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
3017 struct memelfnote notes
[1];
3021 struct elf_note_info
{
3022 struct memelfnote
*notes
;
3023 struct target_elf_prstatus
*prstatus
; /* NT_PRSTATUS */
3024 struct target_elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
3026 QTAILQ_HEAD(, elf_thread_status
) thread_list
;
3029 * Current version of ELF coredump doesn't support
3030 * dumping fp regs etc.
3032 elf_fpregset_t
*fpu
;
3033 elf_fpxregset_t
*xfpu
;
3034 int thread_status_size
;
3040 struct vm_area_struct
{
3041 target_ulong vma_start
; /* start vaddr of memory region */
3042 target_ulong vma_end
; /* end vaddr of memory region */
3043 abi_ulong vma_flags
; /* protection etc. flags for the region */
3044 QTAILQ_ENTRY(vm_area_struct
) vma_link
;
3048 QTAILQ_HEAD(, vm_area_struct
) mm_mmap
;
3049 int mm_count
; /* number of mappings */
3052 static struct mm_struct
*vma_init(void);
3053 static void vma_delete(struct mm_struct
*);
3054 static int vma_add_mapping(struct mm_struct
*, target_ulong
,
3055 target_ulong
, abi_ulong
);
3056 static int vma_get_mapping_count(const struct mm_struct
*);
3057 static struct vm_area_struct
*vma_first(const struct mm_struct
*);
3058 static struct vm_area_struct
*vma_next(struct vm_area_struct
*);
3059 static abi_ulong
vma_dump_size(const struct vm_area_struct
*);
3060 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
3061 unsigned long flags
);
3063 static void fill_elf_header(struct elfhdr
*, int, uint16_t, uint32_t);
3064 static void fill_note(struct memelfnote
*, const char *, int,
3065 unsigned int, void *);
3066 static void fill_prstatus(struct target_elf_prstatus
*, const TaskState
*, int);
3067 static int fill_psinfo(struct target_elf_prpsinfo
*, const TaskState
*);
3068 static void fill_auxv_note(struct memelfnote
*, const TaskState
*);
3069 static void fill_elf_note_phdr(struct elf_phdr
*, int, off_t
);
3070 static size_t note_size(const struct memelfnote
*);
3071 static void free_note_info(struct elf_note_info
*);
3072 static int fill_note_info(struct elf_note_info
*, long, const CPUArchState
*);
3073 static void fill_thread_info(struct elf_note_info
*, const CPUArchState
*);
3074 static int core_dump_filename(const TaskState
*, char *, size_t);
3076 static int dump_write(int, const void *, size_t);
3077 static int write_note(struct memelfnote
*, int);
3078 static int write_note_info(struct elf_note_info
*, int);
3081 static void bswap_prstatus(struct target_elf_prstatus
*prstatus
)
3083 prstatus
->pr_info
.si_signo
= tswap32(prstatus
->pr_info
.si_signo
);
3084 prstatus
->pr_info
.si_code
= tswap32(prstatus
->pr_info
.si_code
);
3085 prstatus
->pr_info
.si_errno
= tswap32(prstatus
->pr_info
.si_errno
);
3086 prstatus
->pr_cursig
= tswap16(prstatus
->pr_cursig
);
3087 prstatus
->pr_sigpend
= tswapal(prstatus
->pr_sigpend
);
3088 prstatus
->pr_sighold
= tswapal(prstatus
->pr_sighold
);
3089 prstatus
->pr_pid
= tswap32(prstatus
->pr_pid
);
3090 prstatus
->pr_ppid
= tswap32(prstatus
->pr_ppid
);
3091 prstatus
->pr_pgrp
= tswap32(prstatus
->pr_pgrp
);
3092 prstatus
->pr_sid
= tswap32(prstatus
->pr_sid
);
3093 /* cpu times are not filled, so we skip them */
3094 /* regs should be in correct format already */
3095 prstatus
->pr_fpvalid
= tswap32(prstatus
->pr_fpvalid
);
3098 static void bswap_psinfo(struct target_elf_prpsinfo
*psinfo
)
3100 psinfo
->pr_flag
= tswapal(psinfo
->pr_flag
);
3101 psinfo
->pr_uid
= tswap16(psinfo
->pr_uid
);
3102 psinfo
->pr_gid
= tswap16(psinfo
->pr_gid
);
3103 psinfo
->pr_pid
= tswap32(psinfo
->pr_pid
);
3104 psinfo
->pr_ppid
= tswap32(psinfo
->pr_ppid
);
3105 psinfo
->pr_pgrp
= tswap32(psinfo
->pr_pgrp
);
3106 psinfo
->pr_sid
= tswap32(psinfo
->pr_sid
);
3109 static void bswap_note(struct elf_note
*en
)
3111 bswap32s(&en
->n_namesz
);
3112 bswap32s(&en
->n_descsz
);
3113 bswap32s(&en
->n_type
);
3116 static inline void bswap_prstatus(struct target_elf_prstatus
*p
) { }
3117 static inline void bswap_psinfo(struct target_elf_prpsinfo
*p
) {}
3118 static inline void bswap_note(struct elf_note
*en
) { }
3119 #endif /* BSWAP_NEEDED */
3122 * Minimal support for linux memory regions. These are needed
3123 * when we are finding out what memory exactly belongs to
3124 * emulated process. No locks needed here, as long as
3125 * thread that received the signal is stopped.
3128 static struct mm_struct
*vma_init(void)
3130 struct mm_struct
*mm
;
3132 if ((mm
= g_malloc(sizeof (*mm
))) == NULL
)
3136 QTAILQ_INIT(&mm
->mm_mmap
);
3141 static void vma_delete(struct mm_struct
*mm
)
3143 struct vm_area_struct
*vma
;
3145 while ((vma
= vma_first(mm
)) != NULL
) {
3146 QTAILQ_REMOVE(&mm
->mm_mmap
, vma
, vma_link
);
3152 static int vma_add_mapping(struct mm_struct
*mm
, target_ulong start
,
3153 target_ulong end
, abi_ulong flags
)
3155 struct vm_area_struct
*vma
;
3157 if ((vma
= g_malloc0(sizeof (*vma
))) == NULL
)
3160 vma
->vma_start
= start
;
3162 vma
->vma_flags
= flags
;
3164 QTAILQ_INSERT_TAIL(&mm
->mm_mmap
, vma
, vma_link
);
3170 static struct vm_area_struct
*vma_first(const struct mm_struct
*mm
)
3172 return (QTAILQ_FIRST(&mm
->mm_mmap
));
3175 static struct vm_area_struct
*vma_next(struct vm_area_struct
*vma
)
3177 return (QTAILQ_NEXT(vma
, vma_link
));
3180 static int vma_get_mapping_count(const struct mm_struct
*mm
)
3182 return (mm
->mm_count
);
3186 * Calculate file (dump) size of given memory region.
3188 static abi_ulong
vma_dump_size(const struct vm_area_struct
*vma
)
3190 /* if we cannot even read the first page, skip it */
3191 if (!access_ok(VERIFY_READ
, vma
->vma_start
, TARGET_PAGE_SIZE
))
3195 * Usually we don't dump executable pages as they contain
3196 * non-writable code that debugger can read directly from
3197 * target library etc. However, thread stacks are marked
3198 * also executable so we read in first page of given region
3199 * and check whether it contains elf header. If there is
3200 * no elf header, we dump it.
3202 if (vma
->vma_flags
& PROT_EXEC
) {
3203 char page
[TARGET_PAGE_SIZE
];
3205 copy_from_user(page
, vma
->vma_start
, sizeof (page
));
3206 if ((page
[EI_MAG0
] == ELFMAG0
) &&
3207 (page
[EI_MAG1
] == ELFMAG1
) &&
3208 (page
[EI_MAG2
] == ELFMAG2
) &&
3209 (page
[EI_MAG3
] == ELFMAG3
)) {
3211 * Mappings are possibly from ELF binary. Don't dump
3218 return (vma
->vma_end
- vma
->vma_start
);
3221 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
3222 unsigned long flags
)
3224 struct mm_struct
*mm
= (struct mm_struct
*)priv
;
3226 vma_add_mapping(mm
, start
, end
, flags
);
3230 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
3231 unsigned int sz
, void *data
)
3233 unsigned int namesz
;
3235 namesz
= strlen(name
) + 1;
3237 note
->namesz
= namesz
;
3238 note
->namesz_rounded
= roundup(namesz
, sizeof (int32_t));
3241 note
->datasz_rounded
= roundup(sz
, sizeof (int32_t));
3246 * We calculate rounded up note size here as specified by
3249 note
->notesz
= sizeof (struct elf_note
) +
3250 note
->namesz_rounded
+ note
->datasz_rounded
;
3253 static void fill_elf_header(struct elfhdr
*elf
, int segs
, uint16_t machine
,
3256 (void) memset(elf
, 0, sizeof(*elf
));
3258 (void) memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
3259 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
3260 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
3261 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
3262 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
3264 elf
->e_type
= ET_CORE
;
3265 elf
->e_machine
= machine
;
3266 elf
->e_version
= EV_CURRENT
;
3267 elf
->e_phoff
= sizeof(struct elfhdr
);
3268 elf
->e_flags
= flags
;
3269 elf
->e_ehsize
= sizeof(struct elfhdr
);
3270 elf
->e_phentsize
= sizeof(struct elf_phdr
);
3271 elf
->e_phnum
= segs
;
3276 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
3278 phdr
->p_type
= PT_NOTE
;
3279 phdr
->p_offset
= offset
;
3282 phdr
->p_filesz
= sz
;
3287 bswap_phdr(phdr
, 1);
3290 static size_t note_size(const struct memelfnote
*note
)
3292 return (note
->notesz
);
3295 static void fill_prstatus(struct target_elf_prstatus
*prstatus
,
3296 const TaskState
*ts
, int signr
)
3298 (void) memset(prstatus
, 0, sizeof (*prstatus
));
3299 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
3300 prstatus
->pr_pid
= ts
->ts_tid
;
3301 prstatus
->pr_ppid
= getppid();
3302 prstatus
->pr_pgrp
= getpgrp();
3303 prstatus
->pr_sid
= getsid(0);
3305 bswap_prstatus(prstatus
);
3308 static int fill_psinfo(struct target_elf_prpsinfo
*psinfo
, const TaskState
*ts
)
3310 char *base_filename
;
3311 unsigned int i
, len
;
3313 (void) memset(psinfo
, 0, sizeof (*psinfo
));
3315 len
= ts
->info
->arg_end
- ts
->info
->arg_start
;
3316 if (len
>= ELF_PRARGSZ
)
3317 len
= ELF_PRARGSZ
- 1;
3318 if (copy_from_user(&psinfo
->pr_psargs
, ts
->info
->arg_start
, len
))
3320 for (i
= 0; i
< len
; i
++)
3321 if (psinfo
->pr_psargs
[i
] == 0)
3322 psinfo
->pr_psargs
[i
] = ' ';
3323 psinfo
->pr_psargs
[len
] = 0;
3325 psinfo
->pr_pid
= getpid();
3326 psinfo
->pr_ppid
= getppid();
3327 psinfo
->pr_pgrp
= getpgrp();
3328 psinfo
->pr_sid
= getsid(0);
3329 psinfo
->pr_uid
= getuid();
3330 psinfo
->pr_gid
= getgid();
3332 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
3334 * Using strncpy here is fine: at max-length,
3335 * this field is not NUL-terminated.
3337 (void) strncpy(psinfo
->pr_fname
, base_filename
,
3338 sizeof(psinfo
->pr_fname
));
3340 g_free(base_filename
);
3341 bswap_psinfo(psinfo
);
3345 static void fill_auxv_note(struct memelfnote
*note
, const TaskState
*ts
)
3347 elf_addr_t auxv
= (elf_addr_t
)ts
->info
->saved_auxv
;
3348 elf_addr_t orig_auxv
= auxv
;
3350 int len
= ts
->info
->auxv_len
;
3353 * Auxiliary vector is stored in target process stack. It contains
3354 * {type, value} pairs that we need to dump into note. This is not
3355 * strictly necessary but we do it here for sake of completeness.
3358 /* read in whole auxv vector and copy it to memelfnote */
3359 ptr
= lock_user(VERIFY_READ
, orig_auxv
, len
, 0);
3361 fill_note(note
, "CORE", NT_AUXV
, len
, ptr
);
3362 unlock_user(ptr
, auxv
, len
);
3367 * Constructs name of coredump file. We have following convention
3369 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3371 * Returns 0 in case of success, -1 otherwise (errno is set).
3373 static int core_dump_filename(const TaskState
*ts
, char *buf
,
3377 char *base_filename
= NULL
;
3381 assert(bufsize
>= PATH_MAX
);
3383 if (gettimeofday(&tv
, NULL
) < 0) {
3384 (void) fprintf(stderr
, "unable to get current timestamp: %s",
3389 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
3390 (void) strftime(timestamp
, sizeof (timestamp
), "%Y%m%d-%H%M%S",
3391 localtime_r(&tv
.tv_sec
, &tm
));
3392 (void) snprintf(buf
, bufsize
, "qemu_%s_%s_%d.core",
3393 base_filename
, timestamp
, (int)getpid());
3394 g_free(base_filename
);
3399 static int dump_write(int fd
, const void *ptr
, size_t size
)
3401 const char *bufp
= (const char *)ptr
;
3402 ssize_t bytes_written
, bytes_left
;
3403 struct rlimit dumpsize
;
3407 getrlimit(RLIMIT_CORE
, &dumpsize
);
3408 if ((pos
= lseek(fd
, 0, SEEK_CUR
))==-1) {
3409 if (errno
== ESPIPE
) { /* not a seekable stream */
3415 if (dumpsize
.rlim_cur
<= pos
) {
3417 } else if (dumpsize
.rlim_cur
== RLIM_INFINITY
) {
3420 size_t limit_left
=dumpsize
.rlim_cur
- pos
;
3421 bytes_left
= limit_left
>= size
? size
: limit_left
;
3426 * In normal conditions, single write(2) should do but
3427 * in case of socket etc. this mechanism is more portable.
3430 bytes_written
= write(fd
, bufp
, bytes_left
);
3431 if (bytes_written
< 0) {
3435 } else if (bytes_written
== 0) { /* eof */
3438 bufp
+= bytes_written
;
3439 bytes_left
-= bytes_written
;
3440 } while (bytes_left
> 0);
3445 static int write_note(struct memelfnote
*men
, int fd
)
3449 en
.n_namesz
= men
->namesz
;
3450 en
.n_type
= men
->type
;
3451 en
.n_descsz
= men
->datasz
;
3455 if (dump_write(fd
, &en
, sizeof(en
)) != 0)
3457 if (dump_write(fd
, men
->name
, men
->namesz_rounded
) != 0)
3459 if (dump_write(fd
, men
->data
, men
->datasz_rounded
) != 0)
3465 static void fill_thread_info(struct elf_note_info
*info
, const CPUArchState
*env
)
3467 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
3468 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
3469 struct elf_thread_status
*ets
;
3471 ets
= g_malloc0(sizeof (*ets
));
3472 ets
->num_notes
= 1; /* only prstatus is dumped */
3473 fill_prstatus(&ets
->prstatus
, ts
, 0);
3474 elf_core_copy_regs(&ets
->prstatus
.pr_reg
, env
);
3475 fill_note(&ets
->notes
[0], "CORE", NT_PRSTATUS
, sizeof (ets
->prstatus
),
3478 QTAILQ_INSERT_TAIL(&info
->thread_list
, ets
, ets_link
);
3480 info
->notes_size
+= note_size(&ets
->notes
[0]);
3483 static void init_note_info(struct elf_note_info
*info
)
3485 /* Initialize the elf_note_info structure so that it is at
3486 * least safe to call free_note_info() on it. Must be
3487 * called before calling fill_note_info().
3489 memset(info
, 0, sizeof (*info
));
3490 QTAILQ_INIT(&info
->thread_list
);
3493 static int fill_note_info(struct elf_note_info
*info
,
3494 long signr
, const CPUArchState
*env
)
3497 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
3498 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
3501 info
->notes
= g_new0(struct memelfnote
, NUMNOTES
);
3502 if (info
->notes
== NULL
)
3504 info
->prstatus
= g_malloc0(sizeof (*info
->prstatus
));
3505 if (info
->prstatus
== NULL
)
3507 info
->psinfo
= g_malloc0(sizeof (*info
->psinfo
));
3508 if (info
->prstatus
== NULL
)
3512 * First fill in status (and registers) of current thread
3513 * including process info & aux vector.
3515 fill_prstatus(info
->prstatus
, ts
, signr
);
3516 elf_core_copy_regs(&info
->prstatus
->pr_reg
, env
);
3517 fill_note(&info
->notes
[0], "CORE", NT_PRSTATUS
,
3518 sizeof (*info
->prstatus
), info
->prstatus
);
3519 fill_psinfo(info
->psinfo
, ts
);
3520 fill_note(&info
->notes
[1], "CORE", NT_PRPSINFO
,
3521 sizeof (*info
->psinfo
), info
->psinfo
);
3522 fill_auxv_note(&info
->notes
[2], ts
);
3525 info
->notes_size
= 0;
3526 for (i
= 0; i
< info
->numnote
; i
++)
3527 info
->notes_size
+= note_size(&info
->notes
[i
]);
3529 /* read and fill status of all threads */
3532 if (cpu
== thread_cpu
) {
3535 fill_thread_info(info
, (CPUArchState
*)cpu
->env_ptr
);
3542 static void free_note_info(struct elf_note_info
*info
)
3544 struct elf_thread_status
*ets
;
3546 while (!QTAILQ_EMPTY(&info
->thread_list
)) {
3547 ets
= QTAILQ_FIRST(&info
->thread_list
);
3548 QTAILQ_REMOVE(&info
->thread_list
, ets
, ets_link
);
3552 g_free(info
->prstatus
);
3553 g_free(info
->psinfo
);
3554 g_free(info
->notes
);
3557 static int write_note_info(struct elf_note_info
*info
, int fd
)
3559 struct elf_thread_status
*ets
;
3562 /* write prstatus, psinfo and auxv for current thread */
3563 for (i
= 0; i
< info
->numnote
; i
++)
3564 if ((error
= write_note(&info
->notes
[i
], fd
)) != 0)
3567 /* write prstatus for each thread */
3568 QTAILQ_FOREACH(ets
, &info
->thread_list
, ets_link
) {
3569 if ((error
= write_note(&ets
->notes
[0], fd
)) != 0)
3577 * Write out ELF coredump.
3579 * See documentation of ELF object file format in:
3580 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3582 * Coredump format in linux is following:
3584 * 0 +----------------------+ \
3585 * | ELF header | ET_CORE |
3586 * +----------------------+ |
3587 * | ELF program headers | |--- headers
3588 * | - NOTE section | |
3589 * | - PT_LOAD sections | |
3590 * +----------------------+ /
3595 * +----------------------+ <-- aligned to target page
3596 * | Process memory dump |
3601 * +----------------------+
3603 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3604 * NT_PRSINFO -> struct elf_prpsinfo
3605 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3607 * Format follows System V format as close as possible. Current
3608 * version limitations are as follows:
3609 * - no floating point registers are dumped
3611 * Function returns 0 in case of success, negative errno otherwise.
3613 * TODO: make this work also during runtime: it should be
3614 * possible to force coredump from running process and then
3615 * continue processing. For example qemu could set up SIGUSR2
3616 * handler (provided that target process haven't registered
3617 * handler for that) that does the dump when signal is received.
3619 static int elf_core_dump(int signr
, const CPUArchState
*env
)
3621 const CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
3622 const TaskState
*ts
= (const TaskState
*)cpu
->opaque
;
3623 struct vm_area_struct
*vma
= NULL
;
3624 char corefile
[PATH_MAX
];
3625 struct elf_note_info info
;
3627 struct elf_phdr phdr
;
3628 struct rlimit dumpsize
;
3629 struct mm_struct
*mm
= NULL
;
3630 off_t offset
= 0, data_offset
= 0;
3634 init_note_info(&info
);
3637 getrlimit(RLIMIT_CORE
, &dumpsize
);
3638 if (dumpsize
.rlim_cur
== 0)
3641 if (core_dump_filename(ts
, corefile
, sizeof (corefile
)) < 0)
3644 if ((fd
= open(corefile
, O_WRONLY
| O_CREAT
,
3645 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) < 0)
3649 * Walk through target process memory mappings and
3650 * set up structure containing this information. After
3651 * this point vma_xxx functions can be used.
3653 if ((mm
= vma_init()) == NULL
)
3656 walk_memory_regions(mm
, vma_walker
);
3657 segs
= vma_get_mapping_count(mm
);
3660 * Construct valid coredump ELF header. We also
3661 * add one more segment for notes.
3663 fill_elf_header(&elf
, segs
+ 1, ELF_MACHINE
, 0);
3664 if (dump_write(fd
, &elf
, sizeof (elf
)) != 0)
3667 /* fill in the in-memory version of notes */
3668 if (fill_note_info(&info
, signr
, env
) < 0)
3671 offset
+= sizeof (elf
); /* elf header */
3672 offset
+= (segs
+ 1) * sizeof (struct elf_phdr
); /* program headers */
3674 /* write out notes program header */
3675 fill_elf_note_phdr(&phdr
, info
.notes_size
, offset
);
3677 offset
+= info
.notes_size
;
3678 if (dump_write(fd
, &phdr
, sizeof (phdr
)) != 0)
3682 * ELF specification wants data to start at page boundary so
3685 data_offset
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
3688 * Write program headers for memory regions mapped in
3689 * the target process.
3691 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
3692 (void) memset(&phdr
, 0, sizeof (phdr
));
3694 phdr
.p_type
= PT_LOAD
;
3695 phdr
.p_offset
= offset
;
3696 phdr
.p_vaddr
= vma
->vma_start
;
3698 phdr
.p_filesz
= vma_dump_size(vma
);
3699 offset
+= phdr
.p_filesz
;
3700 phdr
.p_memsz
= vma
->vma_end
- vma
->vma_start
;
3701 phdr
.p_flags
= vma
->vma_flags
& PROT_READ
? PF_R
: 0;
3702 if (vma
->vma_flags
& PROT_WRITE
)
3703 phdr
.p_flags
|= PF_W
;
3704 if (vma
->vma_flags
& PROT_EXEC
)
3705 phdr
.p_flags
|= PF_X
;
3706 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
3708 bswap_phdr(&phdr
, 1);
3709 if (dump_write(fd
, &phdr
, sizeof(phdr
)) != 0) {
3715 * Next we write notes just after program headers. No
3716 * alignment needed here.
3718 if (write_note_info(&info
, fd
) < 0)
3721 /* align data to page boundary */
3722 if (lseek(fd
, data_offset
, SEEK_SET
) != data_offset
)
3726 * Finally we can dump process memory into corefile as well.
3728 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
3732 end
= vma
->vma_start
+ vma_dump_size(vma
);
3734 for (addr
= vma
->vma_start
; addr
< end
;
3735 addr
+= TARGET_PAGE_SIZE
) {
3736 char page
[TARGET_PAGE_SIZE
];
3740 * Read in page from target process memory and
3741 * write it to coredump file.
3743 error
= copy_from_user(page
, addr
, sizeof (page
));
3745 (void) fprintf(stderr
, "unable to dump " TARGET_ABI_FMT_lx
"\n",
3750 if (dump_write(fd
, page
, TARGET_PAGE_SIZE
) < 0)
3756 free_note_info(&info
);
3765 #endif /* USE_ELF_CORE_DUMP */
3767 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
3769 init_thread(regs
, infop
);