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 "user-internals.h"
10 #include "signal-common.h"
12 #include "user-mmap.h"
13 #include "disas/disas.h"
14 #include "qemu/bitops.h"
15 #include "qemu/path.h"
16 #include "qemu/queue.h"
17 #include "qemu/guest-random.h"
18 #include "qemu/units.h"
19 #include "qemu/selfmap.h"
20 #include "qemu/lockable.h"
21 #include "qapi/error.h"
22 #include "qemu/error-report.h"
23 #include "target_signal.h"
24 #include "accel/tcg/debuginfo.h"
36 #define ELF_OSABI ELFOSABI_SYSV
38 /* from personality.h */
41 * Flags for bug emulation.
43 * These occupy the top three bytes.
46 ADDR_NO_RANDOMIZE
= 0x0040000, /* disable randomization of VA space */
47 FDPIC_FUNCPTRS
= 0x0080000, /* userspace function ptrs point to
48 descriptors (signal handling) */
49 MMAP_PAGE_ZERO
= 0x0100000,
50 ADDR_COMPAT_LAYOUT
= 0x0200000,
51 READ_IMPLIES_EXEC
= 0x0400000,
52 ADDR_LIMIT_32BIT
= 0x0800000,
53 SHORT_INODE
= 0x1000000,
54 WHOLE_SECONDS
= 0x2000000,
55 STICKY_TIMEOUTS
= 0x4000000,
56 ADDR_LIMIT_3GB
= 0x8000000,
62 * These go in the low byte. Avoid using the top bit, it will
63 * conflict with error returns.
67 PER_LINUX_32BIT
= 0x0000 | ADDR_LIMIT_32BIT
,
68 PER_LINUX_FDPIC
= 0x0000 | FDPIC_FUNCPTRS
,
69 PER_SVR4
= 0x0001 | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
70 PER_SVR3
= 0x0002 | STICKY_TIMEOUTS
| SHORT_INODE
,
71 PER_SCOSVR3
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
| SHORT_INODE
,
72 PER_OSR5
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
,
73 PER_WYSEV386
= 0x0004 | STICKY_TIMEOUTS
| SHORT_INODE
,
74 PER_ISCR4
= 0x0005 | STICKY_TIMEOUTS
,
76 PER_SUNOS
= 0x0006 | STICKY_TIMEOUTS
,
77 PER_XENIX
= 0x0007 | STICKY_TIMEOUTS
| SHORT_INODE
,
79 PER_LINUX32_3GB
= 0x0008 | ADDR_LIMIT_3GB
,
80 PER_IRIX32
= 0x0009 | STICKY_TIMEOUTS
,/* IRIX5 32-bit */
81 PER_IRIXN32
= 0x000a | STICKY_TIMEOUTS
,/* IRIX6 new 32-bit */
82 PER_IRIX64
= 0x000b | STICKY_TIMEOUTS
,/* IRIX6 64-bit */
84 PER_SOLARIS
= 0x000d | STICKY_TIMEOUTS
,
85 PER_UW7
= 0x000e | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
86 PER_OSF4
= 0x000f, /* OSF/1 v4 */
92 * Return the base personality without flags.
94 #define personality(pers) (pers & PER_MASK)
96 int info_is_fdpic(struct image_info
*info
)
98 return info
->personality
== PER_LINUX_FDPIC
;
101 /* this flag is uneffective under linux too, should be deleted */
102 #ifndef MAP_DENYWRITE
103 #define MAP_DENYWRITE 0
106 /* should probably go in elf.h */
111 #if TARGET_BIG_ENDIAN
112 #define ELF_DATA ELFDATA2MSB
114 #define ELF_DATA ELFDATA2LSB
117 #ifdef TARGET_ABI_MIPSN32
118 typedef abi_ullong target_elf_greg_t
;
119 #define tswapreg(ptr) tswap64(ptr)
121 typedef abi_ulong target_elf_greg_t
;
122 #define tswapreg(ptr) tswapal(ptr)
126 typedef abi_ushort target_uid_t
;
127 typedef abi_ushort target_gid_t
;
129 typedef abi_uint target_uid_t
;
130 typedef abi_uint target_gid_t
;
132 typedef abi_int target_pid_t
;
136 #define ELF_HWCAP get_elf_hwcap()
138 static uint32_t get_elf_hwcap(void)
140 X86CPU
*cpu
= X86_CPU(thread_cpu
);
142 return cpu
->env
.features
[FEAT_1_EDX
];
146 #define ELF_CLASS ELFCLASS64
147 #define ELF_ARCH EM_X86_64
149 #define ELF_PLATFORM "x86_64"
151 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
154 regs
->rsp
= infop
->start_stack
;
155 regs
->rip
= infop
->entry
;
159 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
162 * Note that ELF_NREG should be 29 as there should be place for
163 * TRAPNO and ERR "registers" as well but linux doesn't dump
166 * See linux kernel: arch/x86/include/asm/elf.h
168 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
170 (*regs
)[0] = tswapreg(env
->regs
[15]);
171 (*regs
)[1] = tswapreg(env
->regs
[14]);
172 (*regs
)[2] = tswapreg(env
->regs
[13]);
173 (*regs
)[3] = tswapreg(env
->regs
[12]);
174 (*regs
)[4] = tswapreg(env
->regs
[R_EBP
]);
175 (*regs
)[5] = tswapreg(env
->regs
[R_EBX
]);
176 (*regs
)[6] = tswapreg(env
->regs
[11]);
177 (*regs
)[7] = tswapreg(env
->regs
[10]);
178 (*regs
)[8] = tswapreg(env
->regs
[9]);
179 (*regs
)[9] = tswapreg(env
->regs
[8]);
180 (*regs
)[10] = tswapreg(env
->regs
[R_EAX
]);
181 (*regs
)[11] = tswapreg(env
->regs
[R_ECX
]);
182 (*regs
)[12] = tswapreg(env
->regs
[R_EDX
]);
183 (*regs
)[13] = tswapreg(env
->regs
[R_ESI
]);
184 (*regs
)[14] = tswapreg(env
->regs
[R_EDI
]);
185 (*regs
)[15] = tswapreg(env
->regs
[R_EAX
]); /* XXX */
186 (*regs
)[16] = tswapreg(env
->eip
);
187 (*regs
)[17] = tswapreg(env
->segs
[R_CS
].selector
& 0xffff);
188 (*regs
)[18] = tswapreg(env
->eflags
);
189 (*regs
)[19] = tswapreg(env
->regs
[R_ESP
]);
190 (*regs
)[20] = tswapreg(env
->segs
[R_SS
].selector
& 0xffff);
191 (*regs
)[21] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
192 (*regs
)[22] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
193 (*regs
)[23] = tswapreg(env
->segs
[R_DS
].selector
& 0xffff);
194 (*regs
)[24] = tswapreg(env
->segs
[R_ES
].selector
& 0xffff);
195 (*regs
)[25] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
196 (*regs
)[26] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
199 #if ULONG_MAX > UINT32_MAX
200 #define INIT_GUEST_COMMPAGE
201 static bool init_guest_commpage(void)
204 * The vsyscall page is at a high negative address aka kernel space,
205 * which means that we cannot actually allocate it with target_mmap.
206 * We still should be able to use page_set_flags, unless the user
207 * has specified -R reserved_va, which would trigger an assert().
209 if (reserved_va
!= 0 &&
210 TARGET_VSYSCALL_PAGE
+ TARGET_PAGE_SIZE
- 1 > reserved_va
) {
211 error_report("Cannot allocate vsyscall page");
214 page_set_flags(TARGET_VSYSCALL_PAGE
,
215 TARGET_VSYSCALL_PAGE
| ~TARGET_PAGE_MASK
,
216 PAGE_EXEC
| PAGE_VALID
);
223 * This is used to ensure we don't load something for the wrong architecture.
225 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
228 * These are used to set parameters in the core dumps.
230 #define ELF_CLASS ELFCLASS32
231 #define ELF_ARCH EM_386
233 #define ELF_PLATFORM get_elf_platform()
234 #define EXSTACK_DEFAULT true
236 static const char *get_elf_platform(void)
238 static char elf_platform
[] = "i386";
239 int family
= object_property_get_int(OBJECT(thread_cpu
), "family", NULL
);
244 elf_platform
[1] = '0' + family
;
249 static inline void init_thread(struct target_pt_regs
*regs
,
250 struct image_info
*infop
)
252 regs
->esp
= infop
->start_stack
;
253 regs
->eip
= infop
->entry
;
255 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
256 starts %edx contains a pointer to a function which might be
257 registered using `atexit'. This provides a mean for the
258 dynamic linker to call DT_FINI functions for shared libraries
259 that have been loaded before the code runs.
261 A value of 0 tells we have no such handler. */
266 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
269 * Note that ELF_NREG should be 19 as there should be place for
270 * TRAPNO and ERR "registers" as well but linux doesn't dump
273 * See linux kernel: arch/x86/include/asm/elf.h
275 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
277 (*regs
)[0] = tswapreg(env
->regs
[R_EBX
]);
278 (*regs
)[1] = tswapreg(env
->regs
[R_ECX
]);
279 (*regs
)[2] = tswapreg(env
->regs
[R_EDX
]);
280 (*regs
)[3] = tswapreg(env
->regs
[R_ESI
]);
281 (*regs
)[4] = tswapreg(env
->regs
[R_EDI
]);
282 (*regs
)[5] = tswapreg(env
->regs
[R_EBP
]);
283 (*regs
)[6] = tswapreg(env
->regs
[R_EAX
]);
284 (*regs
)[7] = tswapreg(env
->segs
[R_DS
].selector
& 0xffff);
285 (*regs
)[8] = tswapreg(env
->segs
[R_ES
].selector
& 0xffff);
286 (*regs
)[9] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
287 (*regs
)[10] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
288 (*regs
)[11] = tswapreg(env
->regs
[R_EAX
]); /* XXX */
289 (*regs
)[12] = tswapreg(env
->eip
);
290 (*regs
)[13] = tswapreg(env
->segs
[R_CS
].selector
& 0xffff);
291 (*regs
)[14] = tswapreg(env
->eflags
);
292 (*regs
)[15] = tswapreg(env
->regs
[R_ESP
]);
293 (*regs
)[16] = tswapreg(env
->segs
[R_SS
].selector
& 0xffff);
297 #define USE_ELF_CORE_DUMP
298 #define ELF_EXEC_PAGESIZE 4096
304 #ifndef TARGET_AARCH64
305 /* 32 bit ARM definitions */
307 #define ELF_ARCH EM_ARM
308 #define ELF_CLASS ELFCLASS32
309 #define EXSTACK_DEFAULT true
311 static inline void init_thread(struct target_pt_regs
*regs
,
312 struct image_info
*infop
)
314 abi_long stack
= infop
->start_stack
;
315 memset(regs
, 0, sizeof(*regs
));
317 regs
->uregs
[16] = ARM_CPU_MODE_USR
;
318 if (infop
->entry
& 1) {
319 regs
->uregs
[16] |= CPSR_T
;
321 regs
->uregs
[15] = infop
->entry
& 0xfffffffe;
322 regs
->uregs
[13] = infop
->start_stack
;
323 /* FIXME - what to for failure of get_user()? */
324 get_user_ual(regs
->uregs
[2], stack
+ 8); /* envp */
325 get_user_ual(regs
->uregs
[1], stack
+ 4); /* envp */
326 /* XXX: it seems that r0 is zeroed after ! */
328 /* For uClinux PIC binaries. */
329 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
330 regs
->uregs
[10] = infop
->start_data
;
332 /* Support ARM FDPIC. */
333 if (info_is_fdpic(infop
)) {
334 /* As described in the ABI document, r7 points to the loadmap info
335 * prepared by the kernel. If an interpreter is needed, r8 points
336 * to the interpreter loadmap and r9 points to the interpreter
337 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
338 * r9 points to the main program PT_DYNAMIC info.
340 regs
->uregs
[7] = infop
->loadmap_addr
;
341 if (infop
->interpreter_loadmap_addr
) {
342 /* Executable is dynamically loaded. */
343 regs
->uregs
[8] = infop
->interpreter_loadmap_addr
;
344 regs
->uregs
[9] = infop
->interpreter_pt_dynamic_addr
;
347 regs
->uregs
[9] = infop
->pt_dynamic_addr
;
353 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
355 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUARMState
*env
)
357 (*regs
)[0] = tswapreg(env
->regs
[0]);
358 (*regs
)[1] = tswapreg(env
->regs
[1]);
359 (*regs
)[2] = tswapreg(env
->regs
[2]);
360 (*regs
)[3] = tswapreg(env
->regs
[3]);
361 (*regs
)[4] = tswapreg(env
->regs
[4]);
362 (*regs
)[5] = tswapreg(env
->regs
[5]);
363 (*regs
)[6] = tswapreg(env
->regs
[6]);
364 (*regs
)[7] = tswapreg(env
->regs
[7]);
365 (*regs
)[8] = tswapreg(env
->regs
[8]);
366 (*regs
)[9] = tswapreg(env
->regs
[9]);
367 (*regs
)[10] = tswapreg(env
->regs
[10]);
368 (*regs
)[11] = tswapreg(env
->regs
[11]);
369 (*regs
)[12] = tswapreg(env
->regs
[12]);
370 (*regs
)[13] = tswapreg(env
->regs
[13]);
371 (*regs
)[14] = tswapreg(env
->regs
[14]);
372 (*regs
)[15] = tswapreg(env
->regs
[15]);
374 (*regs
)[16] = tswapreg(cpsr_read((CPUARMState
*)env
));
375 (*regs
)[17] = tswapreg(env
->regs
[0]); /* XXX */
378 #define USE_ELF_CORE_DUMP
379 #define ELF_EXEC_PAGESIZE 4096
383 ARM_HWCAP_ARM_SWP
= 1 << 0,
384 ARM_HWCAP_ARM_HALF
= 1 << 1,
385 ARM_HWCAP_ARM_THUMB
= 1 << 2,
386 ARM_HWCAP_ARM_26BIT
= 1 << 3,
387 ARM_HWCAP_ARM_FAST_MULT
= 1 << 4,
388 ARM_HWCAP_ARM_FPA
= 1 << 5,
389 ARM_HWCAP_ARM_VFP
= 1 << 6,
390 ARM_HWCAP_ARM_EDSP
= 1 << 7,
391 ARM_HWCAP_ARM_JAVA
= 1 << 8,
392 ARM_HWCAP_ARM_IWMMXT
= 1 << 9,
393 ARM_HWCAP_ARM_CRUNCH
= 1 << 10,
394 ARM_HWCAP_ARM_THUMBEE
= 1 << 11,
395 ARM_HWCAP_ARM_NEON
= 1 << 12,
396 ARM_HWCAP_ARM_VFPv3
= 1 << 13,
397 ARM_HWCAP_ARM_VFPv3D16
= 1 << 14,
398 ARM_HWCAP_ARM_TLS
= 1 << 15,
399 ARM_HWCAP_ARM_VFPv4
= 1 << 16,
400 ARM_HWCAP_ARM_IDIVA
= 1 << 17,
401 ARM_HWCAP_ARM_IDIVT
= 1 << 18,
402 ARM_HWCAP_ARM_VFPD32
= 1 << 19,
403 ARM_HWCAP_ARM_LPAE
= 1 << 20,
404 ARM_HWCAP_ARM_EVTSTRM
= 1 << 21,
405 ARM_HWCAP_ARM_FPHP
= 1 << 22,
406 ARM_HWCAP_ARM_ASIMDHP
= 1 << 23,
407 ARM_HWCAP_ARM_ASIMDDP
= 1 << 24,
408 ARM_HWCAP_ARM_ASIMDFHM
= 1 << 25,
409 ARM_HWCAP_ARM_ASIMDBF16
= 1 << 26,
410 ARM_HWCAP_ARM_I8MM
= 1 << 27,
414 ARM_HWCAP2_ARM_AES
= 1 << 0,
415 ARM_HWCAP2_ARM_PMULL
= 1 << 1,
416 ARM_HWCAP2_ARM_SHA1
= 1 << 2,
417 ARM_HWCAP2_ARM_SHA2
= 1 << 3,
418 ARM_HWCAP2_ARM_CRC32
= 1 << 4,
419 ARM_HWCAP2_ARM_SB
= 1 << 5,
420 ARM_HWCAP2_ARM_SSBS
= 1 << 6,
423 /* The commpage only exists for 32 bit kernels */
425 #define HI_COMMPAGE (intptr_t)0xffff0f00u
427 static bool init_guest_commpage(void)
429 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
435 * M-profile allocates maximum of 2GB address space, so can never
436 * allocate the commpage. Skip it.
438 if (arm_feature(&cpu
->env
, ARM_FEATURE_M
)) {
442 commpage
= HI_COMMPAGE
& -qemu_host_page_size
;
443 want
= g2h_untagged(commpage
);
444 addr
= mmap(want
, qemu_host_page_size
, PROT_READ
| PROT_WRITE
,
445 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
447 if (addr
== MAP_FAILED
) {
448 perror("Allocating guest commpage");
455 /* Set kernel helper versions; rest of page is 0. */
456 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu
));
458 if (mprotect(addr
, qemu_host_page_size
, PROT_READ
)) {
459 perror("Protecting guest commpage");
463 page_set_flags(commpage
, commpage
| ~qemu_host_page_mask
,
464 PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
468 #define ELF_HWCAP get_elf_hwcap()
469 #define ELF_HWCAP2 get_elf_hwcap2()
471 uint32_t get_elf_hwcap(void)
473 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
476 hwcaps
|= ARM_HWCAP_ARM_SWP
;
477 hwcaps
|= ARM_HWCAP_ARM_HALF
;
478 hwcaps
|= ARM_HWCAP_ARM_THUMB
;
479 hwcaps
|= ARM_HWCAP_ARM_FAST_MULT
;
481 /* probe for the extra features */
482 #define GET_FEATURE(feat, hwcap) \
483 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
485 #define GET_FEATURE_ID(feat, hwcap) \
486 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
488 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
489 GET_FEATURE(ARM_FEATURE_V5
, ARM_HWCAP_ARM_EDSP
);
490 GET_FEATURE(ARM_FEATURE_IWMMXT
, ARM_HWCAP_ARM_IWMMXT
);
491 GET_FEATURE(ARM_FEATURE_THUMB2EE
, ARM_HWCAP_ARM_THUMBEE
);
492 GET_FEATURE(ARM_FEATURE_NEON
, ARM_HWCAP_ARM_NEON
);
493 GET_FEATURE(ARM_FEATURE_V6K
, ARM_HWCAP_ARM_TLS
);
494 GET_FEATURE(ARM_FEATURE_LPAE
, ARM_HWCAP_ARM_LPAE
);
495 GET_FEATURE_ID(aa32_arm_div
, ARM_HWCAP_ARM_IDIVA
);
496 GET_FEATURE_ID(aa32_thumb_div
, ARM_HWCAP_ARM_IDIVT
);
497 GET_FEATURE_ID(aa32_vfp
, ARM_HWCAP_ARM_VFP
);
499 if (cpu_isar_feature(aa32_fpsp_v3
, cpu
) ||
500 cpu_isar_feature(aa32_fpdp_v3
, cpu
)) {
501 hwcaps
|= ARM_HWCAP_ARM_VFPv3
;
502 if (cpu_isar_feature(aa32_simd_r32
, cpu
)) {
503 hwcaps
|= ARM_HWCAP_ARM_VFPD32
;
505 hwcaps
|= ARM_HWCAP_ARM_VFPv3D16
;
508 GET_FEATURE_ID(aa32_simdfmac
, ARM_HWCAP_ARM_VFPv4
);
510 * MVFR1.FPHP and .SIMDHP must be in sync, and QEMU uses the same
511 * isar_feature function for both. The kernel reports them as two hwcaps.
513 GET_FEATURE_ID(aa32_fp16_arith
, ARM_HWCAP_ARM_FPHP
);
514 GET_FEATURE_ID(aa32_fp16_arith
, ARM_HWCAP_ARM_ASIMDHP
);
515 GET_FEATURE_ID(aa32_dp
, ARM_HWCAP_ARM_ASIMDDP
);
516 GET_FEATURE_ID(aa32_fhm
, ARM_HWCAP_ARM_ASIMDFHM
);
517 GET_FEATURE_ID(aa32_bf16
, ARM_HWCAP_ARM_ASIMDBF16
);
518 GET_FEATURE_ID(aa32_i8mm
, ARM_HWCAP_ARM_I8MM
);
523 uint32_t get_elf_hwcap2(void)
525 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
528 GET_FEATURE_ID(aa32_aes
, ARM_HWCAP2_ARM_AES
);
529 GET_FEATURE_ID(aa32_pmull
, ARM_HWCAP2_ARM_PMULL
);
530 GET_FEATURE_ID(aa32_sha1
, ARM_HWCAP2_ARM_SHA1
);
531 GET_FEATURE_ID(aa32_sha2
, ARM_HWCAP2_ARM_SHA2
);
532 GET_FEATURE_ID(aa32_crc32
, ARM_HWCAP2_ARM_CRC32
);
533 GET_FEATURE_ID(aa32_sb
, ARM_HWCAP2_ARM_SB
);
534 GET_FEATURE_ID(aa32_ssbs
, ARM_HWCAP2_ARM_SSBS
);
538 const char *elf_hwcap_str(uint32_t bit
)
540 static const char *hwcap_str
[] = {
541 [__builtin_ctz(ARM_HWCAP_ARM_SWP
)] = "swp",
542 [__builtin_ctz(ARM_HWCAP_ARM_HALF
)] = "half",
543 [__builtin_ctz(ARM_HWCAP_ARM_THUMB
)] = "thumb",
544 [__builtin_ctz(ARM_HWCAP_ARM_26BIT
)] = "26bit",
545 [__builtin_ctz(ARM_HWCAP_ARM_FAST_MULT
)] = "fast_mult",
546 [__builtin_ctz(ARM_HWCAP_ARM_FPA
)] = "fpa",
547 [__builtin_ctz(ARM_HWCAP_ARM_VFP
)] = "vfp",
548 [__builtin_ctz(ARM_HWCAP_ARM_EDSP
)] = "edsp",
549 [__builtin_ctz(ARM_HWCAP_ARM_JAVA
)] = "java",
550 [__builtin_ctz(ARM_HWCAP_ARM_IWMMXT
)] = "iwmmxt",
551 [__builtin_ctz(ARM_HWCAP_ARM_CRUNCH
)] = "crunch",
552 [__builtin_ctz(ARM_HWCAP_ARM_THUMBEE
)] = "thumbee",
553 [__builtin_ctz(ARM_HWCAP_ARM_NEON
)] = "neon",
554 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3
)] = "vfpv3",
555 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3D16
)] = "vfpv3d16",
556 [__builtin_ctz(ARM_HWCAP_ARM_TLS
)] = "tls",
557 [__builtin_ctz(ARM_HWCAP_ARM_VFPv4
)] = "vfpv4",
558 [__builtin_ctz(ARM_HWCAP_ARM_IDIVA
)] = "idiva",
559 [__builtin_ctz(ARM_HWCAP_ARM_IDIVT
)] = "idivt",
560 [__builtin_ctz(ARM_HWCAP_ARM_VFPD32
)] = "vfpd32",
561 [__builtin_ctz(ARM_HWCAP_ARM_LPAE
)] = "lpae",
562 [__builtin_ctz(ARM_HWCAP_ARM_EVTSTRM
)] = "evtstrm",
563 [__builtin_ctz(ARM_HWCAP_ARM_FPHP
)] = "fphp",
564 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDHP
)] = "asimdhp",
565 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDDP
)] = "asimddp",
566 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDFHM
)] = "asimdfhm",
567 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDBF16
)] = "asimdbf16",
568 [__builtin_ctz(ARM_HWCAP_ARM_I8MM
)] = "i8mm",
571 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
574 const char *elf_hwcap2_str(uint32_t bit
)
576 static const char *hwcap_str
[] = {
577 [__builtin_ctz(ARM_HWCAP2_ARM_AES
)] = "aes",
578 [__builtin_ctz(ARM_HWCAP2_ARM_PMULL
)] = "pmull",
579 [__builtin_ctz(ARM_HWCAP2_ARM_SHA1
)] = "sha1",
580 [__builtin_ctz(ARM_HWCAP2_ARM_SHA2
)] = "sha2",
581 [__builtin_ctz(ARM_HWCAP2_ARM_CRC32
)] = "crc32",
582 [__builtin_ctz(ARM_HWCAP2_ARM_SB
)] = "sb",
583 [__builtin_ctz(ARM_HWCAP2_ARM_SSBS
)] = "ssbs",
586 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
590 #undef GET_FEATURE_ID
592 #define ELF_PLATFORM get_elf_platform()
594 static const char *get_elf_platform(void)
596 CPUARMState
*env
= cpu_env(thread_cpu
);
598 #if TARGET_BIG_ENDIAN
604 if (arm_feature(env
, ARM_FEATURE_V8
)) {
606 } else if (arm_feature(env
, ARM_FEATURE_V7
)) {
607 if (arm_feature(env
, ARM_FEATURE_M
)) {
612 } else if (arm_feature(env
, ARM_FEATURE_V6
)) {
614 } else if (arm_feature(env
, ARM_FEATURE_V5
)) {
624 /* 64 bit ARM definitions */
626 #define ELF_ARCH EM_AARCH64
627 #define ELF_CLASS ELFCLASS64
628 #if TARGET_BIG_ENDIAN
629 # define ELF_PLATFORM "aarch64_be"
631 # define ELF_PLATFORM "aarch64"
634 static inline void init_thread(struct target_pt_regs
*regs
,
635 struct image_info
*infop
)
637 abi_long stack
= infop
->start_stack
;
638 memset(regs
, 0, sizeof(*regs
));
640 regs
->pc
= infop
->entry
& ~0x3ULL
;
645 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
647 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
648 const CPUARMState
*env
)
652 for (i
= 0; i
< 32; i
++) {
653 (*regs
)[i
] = tswapreg(env
->xregs
[i
]);
655 (*regs
)[32] = tswapreg(env
->pc
);
656 (*regs
)[33] = tswapreg(pstate_read((CPUARMState
*)env
));
659 #define USE_ELF_CORE_DUMP
660 #define ELF_EXEC_PAGESIZE 4096
663 ARM_HWCAP_A64_FP
= 1 << 0,
664 ARM_HWCAP_A64_ASIMD
= 1 << 1,
665 ARM_HWCAP_A64_EVTSTRM
= 1 << 2,
666 ARM_HWCAP_A64_AES
= 1 << 3,
667 ARM_HWCAP_A64_PMULL
= 1 << 4,
668 ARM_HWCAP_A64_SHA1
= 1 << 5,
669 ARM_HWCAP_A64_SHA2
= 1 << 6,
670 ARM_HWCAP_A64_CRC32
= 1 << 7,
671 ARM_HWCAP_A64_ATOMICS
= 1 << 8,
672 ARM_HWCAP_A64_FPHP
= 1 << 9,
673 ARM_HWCAP_A64_ASIMDHP
= 1 << 10,
674 ARM_HWCAP_A64_CPUID
= 1 << 11,
675 ARM_HWCAP_A64_ASIMDRDM
= 1 << 12,
676 ARM_HWCAP_A64_JSCVT
= 1 << 13,
677 ARM_HWCAP_A64_FCMA
= 1 << 14,
678 ARM_HWCAP_A64_LRCPC
= 1 << 15,
679 ARM_HWCAP_A64_DCPOP
= 1 << 16,
680 ARM_HWCAP_A64_SHA3
= 1 << 17,
681 ARM_HWCAP_A64_SM3
= 1 << 18,
682 ARM_HWCAP_A64_SM4
= 1 << 19,
683 ARM_HWCAP_A64_ASIMDDP
= 1 << 20,
684 ARM_HWCAP_A64_SHA512
= 1 << 21,
685 ARM_HWCAP_A64_SVE
= 1 << 22,
686 ARM_HWCAP_A64_ASIMDFHM
= 1 << 23,
687 ARM_HWCAP_A64_DIT
= 1 << 24,
688 ARM_HWCAP_A64_USCAT
= 1 << 25,
689 ARM_HWCAP_A64_ILRCPC
= 1 << 26,
690 ARM_HWCAP_A64_FLAGM
= 1 << 27,
691 ARM_HWCAP_A64_SSBS
= 1 << 28,
692 ARM_HWCAP_A64_SB
= 1 << 29,
693 ARM_HWCAP_A64_PACA
= 1 << 30,
694 ARM_HWCAP_A64_PACG
= 1UL << 31,
696 ARM_HWCAP2_A64_DCPODP
= 1 << 0,
697 ARM_HWCAP2_A64_SVE2
= 1 << 1,
698 ARM_HWCAP2_A64_SVEAES
= 1 << 2,
699 ARM_HWCAP2_A64_SVEPMULL
= 1 << 3,
700 ARM_HWCAP2_A64_SVEBITPERM
= 1 << 4,
701 ARM_HWCAP2_A64_SVESHA3
= 1 << 5,
702 ARM_HWCAP2_A64_SVESM4
= 1 << 6,
703 ARM_HWCAP2_A64_FLAGM2
= 1 << 7,
704 ARM_HWCAP2_A64_FRINT
= 1 << 8,
705 ARM_HWCAP2_A64_SVEI8MM
= 1 << 9,
706 ARM_HWCAP2_A64_SVEF32MM
= 1 << 10,
707 ARM_HWCAP2_A64_SVEF64MM
= 1 << 11,
708 ARM_HWCAP2_A64_SVEBF16
= 1 << 12,
709 ARM_HWCAP2_A64_I8MM
= 1 << 13,
710 ARM_HWCAP2_A64_BF16
= 1 << 14,
711 ARM_HWCAP2_A64_DGH
= 1 << 15,
712 ARM_HWCAP2_A64_RNG
= 1 << 16,
713 ARM_HWCAP2_A64_BTI
= 1 << 17,
714 ARM_HWCAP2_A64_MTE
= 1 << 18,
715 ARM_HWCAP2_A64_ECV
= 1 << 19,
716 ARM_HWCAP2_A64_AFP
= 1 << 20,
717 ARM_HWCAP2_A64_RPRES
= 1 << 21,
718 ARM_HWCAP2_A64_MTE3
= 1 << 22,
719 ARM_HWCAP2_A64_SME
= 1 << 23,
720 ARM_HWCAP2_A64_SME_I16I64
= 1 << 24,
721 ARM_HWCAP2_A64_SME_F64F64
= 1 << 25,
722 ARM_HWCAP2_A64_SME_I8I32
= 1 << 26,
723 ARM_HWCAP2_A64_SME_F16F32
= 1 << 27,
724 ARM_HWCAP2_A64_SME_B16F32
= 1 << 28,
725 ARM_HWCAP2_A64_SME_F32F32
= 1 << 29,
726 ARM_HWCAP2_A64_SME_FA64
= 1 << 30,
727 ARM_HWCAP2_A64_WFXT
= 1ULL << 31,
728 ARM_HWCAP2_A64_EBF16
= 1ULL << 32,
729 ARM_HWCAP2_A64_SVE_EBF16
= 1ULL << 33,
730 ARM_HWCAP2_A64_CSSC
= 1ULL << 34,
731 ARM_HWCAP2_A64_RPRFM
= 1ULL << 35,
732 ARM_HWCAP2_A64_SVE2P1
= 1ULL << 36,
733 ARM_HWCAP2_A64_SME2
= 1ULL << 37,
734 ARM_HWCAP2_A64_SME2P1
= 1ULL << 38,
735 ARM_HWCAP2_A64_SME_I16I32
= 1ULL << 39,
736 ARM_HWCAP2_A64_SME_BI32I32
= 1ULL << 40,
737 ARM_HWCAP2_A64_SME_B16B16
= 1ULL << 41,
738 ARM_HWCAP2_A64_SME_F16F16
= 1ULL << 42,
739 ARM_HWCAP2_A64_MOPS
= 1ULL << 43,
740 ARM_HWCAP2_A64_HBC
= 1ULL << 44,
743 #define ELF_HWCAP get_elf_hwcap()
744 #define ELF_HWCAP2 get_elf_hwcap2()
746 #define GET_FEATURE_ID(feat, hwcap) \
747 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
749 uint32_t get_elf_hwcap(void)
751 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
754 hwcaps
|= ARM_HWCAP_A64_FP
;
755 hwcaps
|= ARM_HWCAP_A64_ASIMD
;
756 hwcaps
|= ARM_HWCAP_A64_CPUID
;
758 /* probe for the extra features */
760 GET_FEATURE_ID(aa64_aes
, ARM_HWCAP_A64_AES
);
761 GET_FEATURE_ID(aa64_pmull
, ARM_HWCAP_A64_PMULL
);
762 GET_FEATURE_ID(aa64_sha1
, ARM_HWCAP_A64_SHA1
);
763 GET_FEATURE_ID(aa64_sha256
, ARM_HWCAP_A64_SHA2
);
764 GET_FEATURE_ID(aa64_sha512
, ARM_HWCAP_A64_SHA512
);
765 GET_FEATURE_ID(aa64_crc32
, ARM_HWCAP_A64_CRC32
);
766 GET_FEATURE_ID(aa64_sha3
, ARM_HWCAP_A64_SHA3
);
767 GET_FEATURE_ID(aa64_sm3
, ARM_HWCAP_A64_SM3
);
768 GET_FEATURE_ID(aa64_sm4
, ARM_HWCAP_A64_SM4
);
769 GET_FEATURE_ID(aa64_fp16
, ARM_HWCAP_A64_FPHP
| ARM_HWCAP_A64_ASIMDHP
);
770 GET_FEATURE_ID(aa64_atomics
, ARM_HWCAP_A64_ATOMICS
);
771 GET_FEATURE_ID(aa64_rdm
, ARM_HWCAP_A64_ASIMDRDM
);
772 GET_FEATURE_ID(aa64_dp
, ARM_HWCAP_A64_ASIMDDP
);
773 GET_FEATURE_ID(aa64_fcma
, ARM_HWCAP_A64_FCMA
);
774 GET_FEATURE_ID(aa64_sve
, ARM_HWCAP_A64_SVE
);
775 GET_FEATURE_ID(aa64_pauth
, ARM_HWCAP_A64_PACA
| ARM_HWCAP_A64_PACG
);
776 GET_FEATURE_ID(aa64_fhm
, ARM_HWCAP_A64_ASIMDFHM
);
777 GET_FEATURE_ID(aa64_jscvt
, ARM_HWCAP_A64_JSCVT
);
778 GET_FEATURE_ID(aa64_sb
, ARM_HWCAP_A64_SB
);
779 GET_FEATURE_ID(aa64_condm_4
, ARM_HWCAP_A64_FLAGM
);
780 GET_FEATURE_ID(aa64_dcpop
, ARM_HWCAP_A64_DCPOP
);
781 GET_FEATURE_ID(aa64_rcpc_8_3
, ARM_HWCAP_A64_LRCPC
);
782 GET_FEATURE_ID(aa64_rcpc_8_4
, ARM_HWCAP_A64_ILRCPC
);
787 uint32_t get_elf_hwcap2(void)
789 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
792 GET_FEATURE_ID(aa64_dcpodp
, ARM_HWCAP2_A64_DCPODP
);
793 GET_FEATURE_ID(aa64_sve2
, ARM_HWCAP2_A64_SVE2
);
794 GET_FEATURE_ID(aa64_sve2_aes
, ARM_HWCAP2_A64_SVEAES
);
795 GET_FEATURE_ID(aa64_sve2_pmull128
, ARM_HWCAP2_A64_SVEPMULL
);
796 GET_FEATURE_ID(aa64_sve2_bitperm
, ARM_HWCAP2_A64_SVEBITPERM
);
797 GET_FEATURE_ID(aa64_sve2_sha3
, ARM_HWCAP2_A64_SVESHA3
);
798 GET_FEATURE_ID(aa64_sve2_sm4
, ARM_HWCAP2_A64_SVESM4
);
799 GET_FEATURE_ID(aa64_condm_5
, ARM_HWCAP2_A64_FLAGM2
);
800 GET_FEATURE_ID(aa64_frint
, ARM_HWCAP2_A64_FRINT
);
801 GET_FEATURE_ID(aa64_sve_i8mm
, ARM_HWCAP2_A64_SVEI8MM
);
802 GET_FEATURE_ID(aa64_sve_f32mm
, ARM_HWCAP2_A64_SVEF32MM
);
803 GET_FEATURE_ID(aa64_sve_f64mm
, ARM_HWCAP2_A64_SVEF64MM
);
804 GET_FEATURE_ID(aa64_sve_bf16
, ARM_HWCAP2_A64_SVEBF16
);
805 GET_FEATURE_ID(aa64_i8mm
, ARM_HWCAP2_A64_I8MM
);
806 GET_FEATURE_ID(aa64_bf16
, ARM_HWCAP2_A64_BF16
);
807 GET_FEATURE_ID(aa64_rndr
, ARM_HWCAP2_A64_RNG
);
808 GET_FEATURE_ID(aa64_bti
, ARM_HWCAP2_A64_BTI
);
809 GET_FEATURE_ID(aa64_mte
, ARM_HWCAP2_A64_MTE
);
810 GET_FEATURE_ID(aa64_sme
, (ARM_HWCAP2_A64_SME
|
811 ARM_HWCAP2_A64_SME_F32F32
|
812 ARM_HWCAP2_A64_SME_B16F32
|
813 ARM_HWCAP2_A64_SME_F16F32
|
814 ARM_HWCAP2_A64_SME_I8I32
));
815 GET_FEATURE_ID(aa64_sme_f64f64
, ARM_HWCAP2_A64_SME_F64F64
);
816 GET_FEATURE_ID(aa64_sme_i16i64
, ARM_HWCAP2_A64_SME_I16I64
);
817 GET_FEATURE_ID(aa64_sme_fa64
, ARM_HWCAP2_A64_SME_FA64
);
818 GET_FEATURE_ID(aa64_hbc
, ARM_HWCAP2_A64_HBC
);
819 GET_FEATURE_ID(aa64_mops
, ARM_HWCAP2_A64_MOPS
);
824 const char *elf_hwcap_str(uint32_t bit
)
826 static const char *hwcap_str
[] = {
827 [__builtin_ctz(ARM_HWCAP_A64_FP
)] = "fp",
828 [__builtin_ctz(ARM_HWCAP_A64_ASIMD
)] = "asimd",
829 [__builtin_ctz(ARM_HWCAP_A64_EVTSTRM
)] = "evtstrm",
830 [__builtin_ctz(ARM_HWCAP_A64_AES
)] = "aes",
831 [__builtin_ctz(ARM_HWCAP_A64_PMULL
)] = "pmull",
832 [__builtin_ctz(ARM_HWCAP_A64_SHA1
)] = "sha1",
833 [__builtin_ctz(ARM_HWCAP_A64_SHA2
)] = "sha2",
834 [__builtin_ctz(ARM_HWCAP_A64_CRC32
)] = "crc32",
835 [__builtin_ctz(ARM_HWCAP_A64_ATOMICS
)] = "atomics",
836 [__builtin_ctz(ARM_HWCAP_A64_FPHP
)] = "fphp",
837 [__builtin_ctz(ARM_HWCAP_A64_ASIMDHP
)] = "asimdhp",
838 [__builtin_ctz(ARM_HWCAP_A64_CPUID
)] = "cpuid",
839 [__builtin_ctz(ARM_HWCAP_A64_ASIMDRDM
)] = "asimdrdm",
840 [__builtin_ctz(ARM_HWCAP_A64_JSCVT
)] = "jscvt",
841 [__builtin_ctz(ARM_HWCAP_A64_FCMA
)] = "fcma",
842 [__builtin_ctz(ARM_HWCAP_A64_LRCPC
)] = "lrcpc",
843 [__builtin_ctz(ARM_HWCAP_A64_DCPOP
)] = "dcpop",
844 [__builtin_ctz(ARM_HWCAP_A64_SHA3
)] = "sha3",
845 [__builtin_ctz(ARM_HWCAP_A64_SM3
)] = "sm3",
846 [__builtin_ctz(ARM_HWCAP_A64_SM4
)] = "sm4",
847 [__builtin_ctz(ARM_HWCAP_A64_ASIMDDP
)] = "asimddp",
848 [__builtin_ctz(ARM_HWCAP_A64_SHA512
)] = "sha512",
849 [__builtin_ctz(ARM_HWCAP_A64_SVE
)] = "sve",
850 [__builtin_ctz(ARM_HWCAP_A64_ASIMDFHM
)] = "asimdfhm",
851 [__builtin_ctz(ARM_HWCAP_A64_DIT
)] = "dit",
852 [__builtin_ctz(ARM_HWCAP_A64_USCAT
)] = "uscat",
853 [__builtin_ctz(ARM_HWCAP_A64_ILRCPC
)] = "ilrcpc",
854 [__builtin_ctz(ARM_HWCAP_A64_FLAGM
)] = "flagm",
855 [__builtin_ctz(ARM_HWCAP_A64_SSBS
)] = "ssbs",
856 [__builtin_ctz(ARM_HWCAP_A64_SB
)] = "sb",
857 [__builtin_ctz(ARM_HWCAP_A64_PACA
)] = "paca",
858 [__builtin_ctz(ARM_HWCAP_A64_PACG
)] = "pacg",
861 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
864 const char *elf_hwcap2_str(uint32_t bit
)
866 static const char *hwcap_str
[] = {
867 [__builtin_ctz(ARM_HWCAP2_A64_DCPODP
)] = "dcpodp",
868 [__builtin_ctz(ARM_HWCAP2_A64_SVE2
)] = "sve2",
869 [__builtin_ctz(ARM_HWCAP2_A64_SVEAES
)] = "sveaes",
870 [__builtin_ctz(ARM_HWCAP2_A64_SVEPMULL
)] = "svepmull",
871 [__builtin_ctz(ARM_HWCAP2_A64_SVEBITPERM
)] = "svebitperm",
872 [__builtin_ctz(ARM_HWCAP2_A64_SVESHA3
)] = "svesha3",
873 [__builtin_ctz(ARM_HWCAP2_A64_SVESM4
)] = "svesm4",
874 [__builtin_ctz(ARM_HWCAP2_A64_FLAGM2
)] = "flagm2",
875 [__builtin_ctz(ARM_HWCAP2_A64_FRINT
)] = "frint",
876 [__builtin_ctz(ARM_HWCAP2_A64_SVEI8MM
)] = "svei8mm",
877 [__builtin_ctz(ARM_HWCAP2_A64_SVEF32MM
)] = "svef32mm",
878 [__builtin_ctz(ARM_HWCAP2_A64_SVEF64MM
)] = "svef64mm",
879 [__builtin_ctz(ARM_HWCAP2_A64_SVEBF16
)] = "svebf16",
880 [__builtin_ctz(ARM_HWCAP2_A64_I8MM
)] = "i8mm",
881 [__builtin_ctz(ARM_HWCAP2_A64_BF16
)] = "bf16",
882 [__builtin_ctz(ARM_HWCAP2_A64_DGH
)] = "dgh",
883 [__builtin_ctz(ARM_HWCAP2_A64_RNG
)] = "rng",
884 [__builtin_ctz(ARM_HWCAP2_A64_BTI
)] = "bti",
885 [__builtin_ctz(ARM_HWCAP2_A64_MTE
)] = "mte",
886 [__builtin_ctz(ARM_HWCAP2_A64_ECV
)] = "ecv",
887 [__builtin_ctz(ARM_HWCAP2_A64_AFP
)] = "afp",
888 [__builtin_ctz(ARM_HWCAP2_A64_RPRES
)] = "rpres",
889 [__builtin_ctz(ARM_HWCAP2_A64_MTE3
)] = "mte3",
890 [__builtin_ctz(ARM_HWCAP2_A64_SME
)] = "sme",
891 [__builtin_ctz(ARM_HWCAP2_A64_SME_I16I64
)] = "smei16i64",
892 [__builtin_ctz(ARM_HWCAP2_A64_SME_F64F64
)] = "smef64f64",
893 [__builtin_ctz(ARM_HWCAP2_A64_SME_I8I32
)] = "smei8i32",
894 [__builtin_ctz(ARM_HWCAP2_A64_SME_F16F32
)] = "smef16f32",
895 [__builtin_ctz(ARM_HWCAP2_A64_SME_B16F32
)] = "smeb16f32",
896 [__builtin_ctz(ARM_HWCAP2_A64_SME_F32F32
)] = "smef32f32",
897 [__builtin_ctz(ARM_HWCAP2_A64_SME_FA64
)] = "smefa64",
898 [__builtin_ctz(ARM_HWCAP2_A64_WFXT
)] = "wfxt",
899 [__builtin_ctzll(ARM_HWCAP2_A64_EBF16
)] = "ebf16",
900 [__builtin_ctzll(ARM_HWCAP2_A64_SVE_EBF16
)] = "sveebf16",
901 [__builtin_ctzll(ARM_HWCAP2_A64_CSSC
)] = "cssc",
902 [__builtin_ctzll(ARM_HWCAP2_A64_RPRFM
)] = "rprfm",
903 [__builtin_ctzll(ARM_HWCAP2_A64_SVE2P1
)] = "sve2p1",
904 [__builtin_ctzll(ARM_HWCAP2_A64_SME2
)] = "sme2",
905 [__builtin_ctzll(ARM_HWCAP2_A64_SME2P1
)] = "sme2p1",
906 [__builtin_ctzll(ARM_HWCAP2_A64_SME_I16I32
)] = "smei16i32",
907 [__builtin_ctzll(ARM_HWCAP2_A64_SME_BI32I32
)] = "smebi32i32",
908 [__builtin_ctzll(ARM_HWCAP2_A64_SME_B16B16
)] = "smeb16b16",
909 [__builtin_ctzll(ARM_HWCAP2_A64_SME_F16F16
)] = "smef16f16",
910 [__builtin_ctzll(ARM_HWCAP2_A64_MOPS
)] = "mops",
911 [__builtin_ctzll(ARM_HWCAP2_A64_HBC
)] = "hbc",
914 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
917 #undef GET_FEATURE_ID
919 #endif /* not TARGET_AARCH64 */
920 #endif /* TARGET_ARM */
923 #ifdef TARGET_SPARC64
925 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
926 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
928 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
930 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
933 #define ELF_CLASS ELFCLASS64
934 #define ELF_ARCH EM_SPARCV9
936 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
937 | HWCAP_SPARC_MULDIV)
938 #define ELF_CLASS ELFCLASS32
939 #define ELF_ARCH EM_SPARC
940 #endif /* TARGET_SPARC64 */
942 static inline void init_thread(struct target_pt_regs
*regs
,
943 struct image_info
*infop
)
945 /* Note that target_cpu_copy_regs does not read psr/tstate. */
946 regs
->pc
= infop
->entry
;
947 regs
->npc
= regs
->pc
+ 4;
949 regs
->u_regs
[14] = (infop
->start_stack
- 16 * sizeof(abi_ulong
)
950 - TARGET_STACK_BIAS
);
952 #endif /* TARGET_SPARC */
956 #define ELF_MACHINE PPC_ELF_MACHINE
958 #if defined(TARGET_PPC64)
960 #define elf_check_arch(x) ( (x) == EM_PPC64 )
962 #define ELF_CLASS ELFCLASS64
966 #define ELF_CLASS ELFCLASS32
967 #define EXSTACK_DEFAULT true
971 #define ELF_ARCH EM_PPC
973 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
974 See arch/powerpc/include/asm/cputable.h. */
976 QEMU_PPC_FEATURE_32
= 0x80000000,
977 QEMU_PPC_FEATURE_64
= 0x40000000,
978 QEMU_PPC_FEATURE_601_INSTR
= 0x20000000,
979 QEMU_PPC_FEATURE_HAS_ALTIVEC
= 0x10000000,
980 QEMU_PPC_FEATURE_HAS_FPU
= 0x08000000,
981 QEMU_PPC_FEATURE_HAS_MMU
= 0x04000000,
982 QEMU_PPC_FEATURE_HAS_4xxMAC
= 0x02000000,
983 QEMU_PPC_FEATURE_UNIFIED_CACHE
= 0x01000000,
984 QEMU_PPC_FEATURE_HAS_SPE
= 0x00800000,
985 QEMU_PPC_FEATURE_HAS_EFP_SINGLE
= 0x00400000,
986 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
= 0x00200000,
987 QEMU_PPC_FEATURE_NO_TB
= 0x00100000,
988 QEMU_PPC_FEATURE_POWER4
= 0x00080000,
989 QEMU_PPC_FEATURE_POWER5
= 0x00040000,
990 QEMU_PPC_FEATURE_POWER5_PLUS
= 0x00020000,
991 QEMU_PPC_FEATURE_CELL
= 0x00010000,
992 QEMU_PPC_FEATURE_BOOKE
= 0x00008000,
993 QEMU_PPC_FEATURE_SMT
= 0x00004000,
994 QEMU_PPC_FEATURE_ICACHE_SNOOP
= 0x00002000,
995 QEMU_PPC_FEATURE_ARCH_2_05
= 0x00001000,
996 QEMU_PPC_FEATURE_PA6T
= 0x00000800,
997 QEMU_PPC_FEATURE_HAS_DFP
= 0x00000400,
998 QEMU_PPC_FEATURE_POWER6_EXT
= 0x00000200,
999 QEMU_PPC_FEATURE_ARCH_2_06
= 0x00000100,
1000 QEMU_PPC_FEATURE_HAS_VSX
= 0x00000080,
1001 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT
= 0x00000040,
1003 QEMU_PPC_FEATURE_TRUE_LE
= 0x00000002,
1004 QEMU_PPC_FEATURE_PPC_LE
= 0x00000001,
1006 /* Feature definitions in AT_HWCAP2. */
1007 QEMU_PPC_FEATURE2_ARCH_2_07
= 0x80000000, /* ISA 2.07 */
1008 QEMU_PPC_FEATURE2_HAS_HTM
= 0x40000000, /* Hardware Transactional Memory */
1009 QEMU_PPC_FEATURE2_HAS_DSCR
= 0x20000000, /* Data Stream Control Register */
1010 QEMU_PPC_FEATURE2_HAS_EBB
= 0x10000000, /* Event Base Branching */
1011 QEMU_PPC_FEATURE2_HAS_ISEL
= 0x08000000, /* Integer Select */
1012 QEMU_PPC_FEATURE2_HAS_TAR
= 0x04000000, /* Target Address Register */
1013 QEMU_PPC_FEATURE2_VEC_CRYPTO
= 0x02000000,
1014 QEMU_PPC_FEATURE2_HTM_NOSC
= 0x01000000,
1015 QEMU_PPC_FEATURE2_ARCH_3_00
= 0x00800000, /* ISA 3.00 */
1016 QEMU_PPC_FEATURE2_HAS_IEEE128
= 0x00400000, /* VSX IEEE Bin Float 128-bit */
1017 QEMU_PPC_FEATURE2_DARN
= 0x00200000, /* darn random number insn */
1018 QEMU_PPC_FEATURE2_SCV
= 0x00100000, /* scv syscall */
1019 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND
= 0x00080000, /* TM w/o suspended state */
1020 QEMU_PPC_FEATURE2_ARCH_3_1
= 0x00040000, /* ISA 3.1 */
1021 QEMU_PPC_FEATURE2_MMA
= 0x00020000, /* Matrix-Multiply Assist */
1024 #define ELF_HWCAP get_elf_hwcap()
1026 static uint32_t get_elf_hwcap(void)
1028 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
1029 uint32_t features
= 0;
1031 /* We don't have to be terribly complete here; the high points are
1032 Altivec/FP/SPE support. Anything else is just a bonus. */
1033 #define GET_FEATURE(flag, feature) \
1034 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1035 #define GET_FEATURE2(flags, feature) \
1037 if ((cpu->env.insns_flags2 & flags) == flags) { \
1038 features |= feature; \
1041 GET_FEATURE(PPC_64B
, QEMU_PPC_FEATURE_64
);
1042 GET_FEATURE(PPC_FLOAT
, QEMU_PPC_FEATURE_HAS_FPU
);
1043 GET_FEATURE(PPC_ALTIVEC
, QEMU_PPC_FEATURE_HAS_ALTIVEC
);
1044 GET_FEATURE(PPC_SPE
, QEMU_PPC_FEATURE_HAS_SPE
);
1045 GET_FEATURE(PPC_SPE_SINGLE
, QEMU_PPC_FEATURE_HAS_EFP_SINGLE
);
1046 GET_FEATURE(PPC_SPE_DOUBLE
, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
);
1047 GET_FEATURE(PPC_BOOKE
, QEMU_PPC_FEATURE_BOOKE
);
1048 GET_FEATURE(PPC_405_MAC
, QEMU_PPC_FEATURE_HAS_4xxMAC
);
1049 GET_FEATURE2(PPC2_DFP
, QEMU_PPC_FEATURE_HAS_DFP
);
1050 GET_FEATURE2(PPC2_VSX
, QEMU_PPC_FEATURE_HAS_VSX
);
1051 GET_FEATURE2((PPC2_PERM_ISA206
| PPC2_DIVE_ISA206
| PPC2_ATOMIC_ISA206
|
1052 PPC2_FP_CVT_ISA206
| PPC2_FP_TST_ISA206
),
1053 QEMU_PPC_FEATURE_ARCH_2_06
);
1060 #define ELF_HWCAP2 get_elf_hwcap2()
1062 static uint32_t get_elf_hwcap2(void)
1064 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
1065 uint32_t features
= 0;
1067 #define GET_FEATURE(flag, feature) \
1068 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1069 #define GET_FEATURE2(flag, feature) \
1070 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
1072 GET_FEATURE(PPC_ISEL
, QEMU_PPC_FEATURE2_HAS_ISEL
);
1073 GET_FEATURE2(PPC2_BCTAR_ISA207
, QEMU_PPC_FEATURE2_HAS_TAR
);
1074 GET_FEATURE2((PPC2_BCTAR_ISA207
| PPC2_LSQ_ISA207
| PPC2_ALTIVEC_207
|
1075 PPC2_ISA207S
), QEMU_PPC_FEATURE2_ARCH_2_07
|
1076 QEMU_PPC_FEATURE2_VEC_CRYPTO
);
1077 GET_FEATURE2(PPC2_ISA300
, QEMU_PPC_FEATURE2_ARCH_3_00
|
1078 QEMU_PPC_FEATURE2_DARN
| QEMU_PPC_FEATURE2_HAS_IEEE128
);
1079 GET_FEATURE2(PPC2_ISA310
, QEMU_PPC_FEATURE2_ARCH_3_1
|
1080 QEMU_PPC_FEATURE2_MMA
);
1089 * The requirements here are:
1090 * - keep the final alignment of sp (sp & 0xf)
1091 * - make sure the 32-bit value at the first 16 byte aligned position of
1092 * AUXV is greater than 16 for glibc compatibility.
1093 * AT_IGNOREPPC is used for that.
1094 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
1095 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
1097 #define DLINFO_ARCH_ITEMS 5
1098 #define ARCH_DLINFO \
1100 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
1102 * Handle glibc compatibility: these magic entries must \
1103 * be at the lowest addresses in the final auxv. \
1105 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1106 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1107 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
1108 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
1109 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
1112 static inline void init_thread(struct target_pt_regs
*_regs
, struct image_info
*infop
)
1114 _regs
->gpr
[1] = infop
->start_stack
;
1115 #if defined(TARGET_PPC64)
1116 if (get_ppc64_abi(infop
) < 2) {
1118 get_user_u64(val
, infop
->entry
+ 8);
1119 _regs
->gpr
[2] = val
+ infop
->load_bias
;
1120 get_user_u64(val
, infop
->entry
);
1121 infop
->entry
= val
+ infop
->load_bias
;
1123 _regs
->gpr
[12] = infop
->entry
; /* r12 set to global entry address */
1126 _regs
->nip
= infop
->entry
;
1129 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
1131 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1133 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUPPCState
*env
)
1136 target_ulong ccr
= 0;
1138 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
1139 (*regs
)[i
] = tswapreg(env
->gpr
[i
]);
1142 (*regs
)[32] = tswapreg(env
->nip
);
1143 (*regs
)[33] = tswapreg(env
->msr
);
1144 (*regs
)[35] = tswapreg(env
->ctr
);
1145 (*regs
)[36] = tswapreg(env
->lr
);
1146 (*regs
)[37] = tswapreg(cpu_read_xer(env
));
1148 ccr
= ppc_get_cr(env
);
1149 (*regs
)[38] = tswapreg(ccr
);
1152 #define USE_ELF_CORE_DUMP
1153 #define ELF_EXEC_PAGESIZE 4096
1157 #ifdef TARGET_LOONGARCH64
1159 #define ELF_CLASS ELFCLASS64
1160 #define ELF_ARCH EM_LOONGARCH
1161 #define EXSTACK_DEFAULT true
1163 #define elf_check_arch(x) ((x) == EM_LOONGARCH)
1165 static inline void init_thread(struct target_pt_regs
*regs
,
1166 struct image_info
*infop
)
1168 /*Set crmd PG,DA = 1,0 */
1169 regs
->csr
.crmd
= 2 << 3;
1170 regs
->csr
.era
= infop
->entry
;
1171 regs
->regs
[3] = infop
->start_stack
;
1174 /* See linux kernel: arch/loongarch/include/asm/elf.h */
1176 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1180 TARGET_EF_CSR_ERA
= TARGET_EF_R0
+ 33,
1181 TARGET_EF_CSR_BADV
= TARGET_EF_R0
+ 34,
1184 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1185 const CPULoongArchState
*env
)
1189 (*regs
)[TARGET_EF_R0
] = 0;
1191 for (i
= 1; i
< ARRAY_SIZE(env
->gpr
); i
++) {
1192 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->gpr
[i
]);
1195 (*regs
)[TARGET_EF_CSR_ERA
] = tswapreg(env
->pc
);
1196 (*regs
)[TARGET_EF_CSR_BADV
] = tswapreg(env
->CSR_BADV
);
1199 #define USE_ELF_CORE_DUMP
1200 #define ELF_EXEC_PAGESIZE 4096
1202 #define ELF_HWCAP get_elf_hwcap()
1204 /* See arch/loongarch/include/uapi/asm/hwcap.h */
1206 HWCAP_LOONGARCH_CPUCFG
= (1 << 0),
1207 HWCAP_LOONGARCH_LAM
= (1 << 1),
1208 HWCAP_LOONGARCH_UAL
= (1 << 2),
1209 HWCAP_LOONGARCH_FPU
= (1 << 3),
1210 HWCAP_LOONGARCH_LSX
= (1 << 4),
1211 HWCAP_LOONGARCH_LASX
= (1 << 5),
1212 HWCAP_LOONGARCH_CRC32
= (1 << 6),
1213 HWCAP_LOONGARCH_COMPLEX
= (1 << 7),
1214 HWCAP_LOONGARCH_CRYPTO
= (1 << 8),
1215 HWCAP_LOONGARCH_LVZ
= (1 << 9),
1216 HWCAP_LOONGARCH_LBT_X86
= (1 << 10),
1217 HWCAP_LOONGARCH_LBT_ARM
= (1 << 11),
1218 HWCAP_LOONGARCH_LBT_MIPS
= (1 << 12),
1221 static uint32_t get_elf_hwcap(void)
1223 LoongArchCPU
*cpu
= LOONGARCH_CPU(thread_cpu
);
1224 uint32_t hwcaps
= 0;
1226 hwcaps
|= HWCAP_LOONGARCH_CRC32
;
1228 if (FIELD_EX32(cpu
->env
.cpucfg
[1], CPUCFG1
, UAL
)) {
1229 hwcaps
|= HWCAP_LOONGARCH_UAL
;
1232 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, FP
)) {
1233 hwcaps
|= HWCAP_LOONGARCH_FPU
;
1236 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, LAM
)) {
1237 hwcaps
|= HWCAP_LOONGARCH_LAM
;
1243 #define ELF_PLATFORM "loongarch"
1245 #endif /* TARGET_LOONGARCH64 */
1249 #ifdef TARGET_MIPS64
1250 #define ELF_CLASS ELFCLASS64
1252 #define ELF_CLASS ELFCLASS32
1254 #define ELF_ARCH EM_MIPS
1255 #define EXSTACK_DEFAULT true
1257 #ifdef TARGET_ABI_MIPSN32
1258 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1260 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1263 #define ELF_BASE_PLATFORM get_elf_base_platform()
1265 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \
1266 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1267 { return _base_platform; } } while (0)
1269 static const char *get_elf_base_platform(void)
1271 MIPSCPU
*cpu
= MIPS_CPU(thread_cpu
);
1273 /* 64 bit ISAs goes first */
1274 MATCH_PLATFORM_INSN(CPU_MIPS64R6
, "mips64r6");
1275 MATCH_PLATFORM_INSN(CPU_MIPS64R5
, "mips64r5");
1276 MATCH_PLATFORM_INSN(CPU_MIPS64R2
, "mips64r2");
1277 MATCH_PLATFORM_INSN(CPU_MIPS64R1
, "mips64");
1278 MATCH_PLATFORM_INSN(CPU_MIPS5
, "mips5");
1279 MATCH_PLATFORM_INSN(CPU_MIPS4
, "mips4");
1280 MATCH_PLATFORM_INSN(CPU_MIPS3
, "mips3");
1283 MATCH_PLATFORM_INSN(CPU_MIPS32R6
, "mips32r6");
1284 MATCH_PLATFORM_INSN(CPU_MIPS32R5
, "mips32r5");
1285 MATCH_PLATFORM_INSN(CPU_MIPS32R2
, "mips32r2");
1286 MATCH_PLATFORM_INSN(CPU_MIPS32R1
, "mips32");
1287 MATCH_PLATFORM_INSN(CPU_MIPS2
, "mips2");
1292 #undef MATCH_PLATFORM_INSN
1294 static inline void init_thread(struct target_pt_regs
*regs
,
1295 struct image_info
*infop
)
1297 regs
->cp0_status
= 2 << CP0St_KSU
;
1298 regs
->cp0_epc
= infop
->entry
;
1299 regs
->regs
[29] = infop
->start_stack
;
1302 /* See linux kernel: arch/mips/include/asm/elf.h. */
1304 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1306 /* See linux kernel: arch/mips/include/asm/reg.h. */
1308 #ifdef TARGET_MIPS64
1313 TARGET_EF_R26
= TARGET_EF_R0
+ 26,
1314 TARGET_EF_R27
= TARGET_EF_R0
+ 27,
1315 TARGET_EF_LO
= TARGET_EF_R0
+ 32,
1316 TARGET_EF_HI
= TARGET_EF_R0
+ 33,
1317 TARGET_EF_CP0_EPC
= TARGET_EF_R0
+ 34,
1318 TARGET_EF_CP0_BADVADDR
= TARGET_EF_R0
+ 35,
1319 TARGET_EF_CP0_STATUS
= TARGET_EF_R0
+ 36,
1320 TARGET_EF_CP0_CAUSE
= TARGET_EF_R0
+ 37
1323 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1324 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMIPSState
*env
)
1328 for (i
= 0; i
< TARGET_EF_R0
; i
++) {
1331 (*regs
)[TARGET_EF_R0
] = 0;
1333 for (i
= 1; i
< ARRAY_SIZE(env
->active_tc
.gpr
); i
++) {
1334 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->active_tc
.gpr
[i
]);
1337 (*regs
)[TARGET_EF_R26
] = 0;
1338 (*regs
)[TARGET_EF_R27
] = 0;
1339 (*regs
)[TARGET_EF_LO
] = tswapreg(env
->active_tc
.LO
[0]);
1340 (*regs
)[TARGET_EF_HI
] = tswapreg(env
->active_tc
.HI
[0]);
1341 (*regs
)[TARGET_EF_CP0_EPC
] = tswapreg(env
->active_tc
.PC
);
1342 (*regs
)[TARGET_EF_CP0_BADVADDR
] = tswapreg(env
->CP0_BadVAddr
);
1343 (*regs
)[TARGET_EF_CP0_STATUS
] = tswapreg(env
->CP0_Status
);
1344 (*regs
)[TARGET_EF_CP0_CAUSE
] = tswapreg(env
->CP0_Cause
);
1347 #define USE_ELF_CORE_DUMP
1348 #define ELF_EXEC_PAGESIZE 4096
1350 /* See arch/mips/include/uapi/asm/hwcap.h. */
1352 HWCAP_MIPS_R6
= (1 << 0),
1353 HWCAP_MIPS_MSA
= (1 << 1),
1354 HWCAP_MIPS_CRC32
= (1 << 2),
1355 HWCAP_MIPS_MIPS16
= (1 << 3),
1356 HWCAP_MIPS_MDMX
= (1 << 4),
1357 HWCAP_MIPS_MIPS3D
= (1 << 5),
1358 HWCAP_MIPS_SMARTMIPS
= (1 << 6),
1359 HWCAP_MIPS_DSP
= (1 << 7),
1360 HWCAP_MIPS_DSP2
= (1 << 8),
1361 HWCAP_MIPS_DSP3
= (1 << 9),
1362 HWCAP_MIPS_MIPS16E2
= (1 << 10),
1363 HWCAP_LOONGSON_MMI
= (1 << 11),
1364 HWCAP_LOONGSON_EXT
= (1 << 12),
1365 HWCAP_LOONGSON_EXT2
= (1 << 13),
1366 HWCAP_LOONGSON_CPUCFG
= (1 << 14),
1369 #define ELF_HWCAP get_elf_hwcap()
1371 #define GET_FEATURE_INSN(_flag, _hwcap) \
1372 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1374 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1375 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1377 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1379 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1384 static uint32_t get_elf_hwcap(void)
1386 MIPSCPU
*cpu
= MIPS_CPU(thread_cpu
);
1387 uint32_t hwcaps
= 0;
1389 GET_FEATURE_REG_EQU(CP0_Config0
, CP0C0_AR
, CP0C0_AR_LENGTH
,
1391 GET_FEATURE_REG_SET(CP0_Config3
, 1 << CP0C3_MSAP
, HWCAP_MIPS_MSA
);
1392 GET_FEATURE_INSN(ASE_LMMI
, HWCAP_LOONGSON_MMI
);
1393 GET_FEATURE_INSN(ASE_LEXT
, HWCAP_LOONGSON_EXT
);
1398 #undef GET_FEATURE_REG_EQU
1399 #undef GET_FEATURE_REG_SET
1400 #undef GET_FEATURE_INSN
1402 #endif /* TARGET_MIPS */
1404 #ifdef TARGET_MICROBLAZE
1406 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1408 #define ELF_CLASS ELFCLASS32
1409 #define ELF_ARCH EM_MICROBLAZE
1411 static inline void init_thread(struct target_pt_regs
*regs
,
1412 struct image_info
*infop
)
1414 regs
->pc
= infop
->entry
;
1415 regs
->r1
= infop
->start_stack
;
1419 #define ELF_EXEC_PAGESIZE 4096
1421 #define USE_ELF_CORE_DUMP
1423 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1425 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1426 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMBState
*env
)
1430 for (i
= 0; i
< 32; i
++) {
1431 (*regs
)[pos
++] = tswapreg(env
->regs
[i
]);
1434 (*regs
)[pos
++] = tswapreg(env
->pc
);
1435 (*regs
)[pos
++] = tswapreg(mb_cpu_read_msr(env
));
1437 (*regs
)[pos
++] = tswapreg(env
->ear
);
1439 (*regs
)[pos
++] = tswapreg(env
->esr
);
1442 #endif /* TARGET_MICROBLAZE */
1446 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1448 #define ELF_CLASS ELFCLASS32
1449 #define ELF_ARCH EM_ALTERA_NIOS2
1451 static void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1453 regs
->ea
= infop
->entry
;
1454 regs
->sp
= infop
->start_stack
;
1457 #define LO_COMMPAGE TARGET_PAGE_SIZE
1459 static bool init_guest_commpage(void)
1461 static const uint8_t kuser_page
[4 + 2 * 64] = {
1462 /* __kuser_helper_version */
1463 [0x00] = 0x02, 0x00, 0x00, 0x00,
1465 /* __kuser_cmpxchg */
1466 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1467 0x3a, 0x28, 0x00, 0xf8, /* ret */
1469 /* __kuser_sigtramp */
1470 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1471 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1474 void *want
= g2h_untagged(LO_COMMPAGE
& -qemu_host_page_size
);
1475 void *addr
= mmap(want
, qemu_host_page_size
, PROT_READ
| PROT_WRITE
,
1476 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
1478 if (addr
== MAP_FAILED
) {
1479 perror("Allocating guest commpage");
1486 memcpy(addr
, kuser_page
, sizeof(kuser_page
));
1488 if (mprotect(addr
, qemu_host_page_size
, PROT_READ
)) {
1489 perror("Protecting guest commpage");
1493 page_set_flags(LO_COMMPAGE
, LO_COMMPAGE
| ~TARGET_PAGE_MASK
,
1494 PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
1498 #define ELF_EXEC_PAGESIZE 4096
1500 #define USE_ELF_CORE_DUMP
1502 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1504 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1505 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1506 const CPUNios2State
*env
)
1511 for (i
= 1; i
< 8; i
++) /* r0-r7 */
1512 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1514 for (i
= 8; i
< 16; i
++) /* r8-r15 */
1515 (*regs
)[i
] = tswapreg(env
->regs
[i
- 8]);
1517 for (i
= 16; i
< 24; i
++) /* r16-r23 */
1518 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1519 (*regs
)[24] = -1; /* R_ET */
1520 (*regs
)[25] = -1; /* R_BT */
1521 (*regs
)[26] = tswapreg(env
->regs
[R_GP
]);
1522 (*regs
)[27] = tswapreg(env
->regs
[R_SP
]);
1523 (*regs
)[28] = tswapreg(env
->regs
[R_FP
]);
1524 (*regs
)[29] = tswapreg(env
->regs
[R_EA
]);
1525 (*regs
)[30] = -1; /* R_SSTATUS */
1526 (*regs
)[31] = tswapreg(env
->regs
[R_RA
]);
1528 (*regs
)[32] = tswapreg(env
->pc
);
1530 (*regs
)[33] = -1; /* R_STATUS */
1531 (*regs
)[34] = tswapreg(env
->regs
[CR_ESTATUS
]);
1533 for (i
= 35; i
< 49; i
++) /* ... */
1537 #endif /* TARGET_NIOS2 */
1539 #ifdef TARGET_OPENRISC
1541 #define ELF_ARCH EM_OPENRISC
1542 #define ELF_CLASS ELFCLASS32
1543 #define ELF_DATA ELFDATA2MSB
1545 static inline void init_thread(struct target_pt_regs
*regs
,
1546 struct image_info
*infop
)
1548 regs
->pc
= infop
->entry
;
1549 regs
->gpr
[1] = infop
->start_stack
;
1552 #define USE_ELF_CORE_DUMP
1553 #define ELF_EXEC_PAGESIZE 8192
1555 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1556 #define ELF_NREG 34 /* gprs and pc, sr */
1557 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1559 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1560 const CPUOpenRISCState
*env
)
1564 for (i
= 0; i
< 32; i
++) {
1565 (*regs
)[i
] = tswapreg(cpu_get_gpr(env
, i
));
1567 (*regs
)[32] = tswapreg(env
->pc
);
1568 (*regs
)[33] = tswapreg(cpu_get_sr(env
));
1571 #define ELF_PLATFORM NULL
1573 #endif /* TARGET_OPENRISC */
1577 #define ELF_CLASS ELFCLASS32
1578 #define ELF_ARCH EM_SH
1580 static inline void init_thread(struct target_pt_regs
*regs
,
1581 struct image_info
*infop
)
1583 /* Check other registers XXXXX */
1584 regs
->pc
= infop
->entry
;
1585 regs
->regs
[15] = infop
->start_stack
;
1588 /* See linux kernel: arch/sh/include/asm/elf.h. */
1590 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1592 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1597 TARGET_REG_GBR
= 19,
1598 TARGET_REG_MACH
= 20,
1599 TARGET_REG_MACL
= 21,
1600 TARGET_REG_SYSCALL
= 22
1603 static inline void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1604 const CPUSH4State
*env
)
1608 for (i
= 0; i
< 16; i
++) {
1609 (*regs
)[i
] = tswapreg(env
->gregs
[i
]);
1612 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1613 (*regs
)[TARGET_REG_PR
] = tswapreg(env
->pr
);
1614 (*regs
)[TARGET_REG_SR
] = tswapreg(env
->sr
);
1615 (*regs
)[TARGET_REG_GBR
] = tswapreg(env
->gbr
);
1616 (*regs
)[TARGET_REG_MACH
] = tswapreg(env
->mach
);
1617 (*regs
)[TARGET_REG_MACL
] = tswapreg(env
->macl
);
1618 (*regs
)[TARGET_REG_SYSCALL
] = 0; /* FIXME */
1621 #define USE_ELF_CORE_DUMP
1622 #define ELF_EXEC_PAGESIZE 4096
1625 SH_CPU_HAS_FPU
= 0x0001, /* Hardware FPU support */
1626 SH_CPU_HAS_P2_FLUSH_BUG
= 0x0002, /* Need to flush the cache in P2 area */
1627 SH_CPU_HAS_MMU_PAGE_ASSOC
= 0x0004, /* SH3: TLB way selection bit support */
1628 SH_CPU_HAS_DSP
= 0x0008, /* SH-DSP: DSP support */
1629 SH_CPU_HAS_PERF_COUNTER
= 0x0010, /* Hardware performance counters */
1630 SH_CPU_HAS_PTEA
= 0x0020, /* PTEA register */
1631 SH_CPU_HAS_LLSC
= 0x0040, /* movli.l/movco.l */
1632 SH_CPU_HAS_L2_CACHE
= 0x0080, /* Secondary cache / URAM */
1633 SH_CPU_HAS_OP32
= 0x0100, /* 32-bit instruction support */
1634 SH_CPU_HAS_PTEAEX
= 0x0200, /* PTE ASID Extension support */
1637 #define ELF_HWCAP get_elf_hwcap()
1639 static uint32_t get_elf_hwcap(void)
1641 SuperHCPU
*cpu
= SUPERH_CPU(thread_cpu
);
1644 hwcap
|= SH_CPU_HAS_FPU
;
1646 if (cpu
->env
.features
& SH_FEATURE_SH4A
) {
1647 hwcap
|= SH_CPU_HAS_LLSC
;
1657 #define ELF_CLASS ELFCLASS32
1658 #define ELF_ARCH EM_CRIS
1660 static inline void init_thread(struct target_pt_regs
*regs
,
1661 struct image_info
*infop
)
1663 regs
->erp
= infop
->entry
;
1666 #define ELF_EXEC_PAGESIZE 8192
1672 #define ELF_CLASS ELFCLASS32
1673 #define ELF_ARCH EM_68K
1675 /* ??? Does this need to do anything?
1676 #define ELF_PLAT_INIT(_r) */
1678 static inline void init_thread(struct target_pt_regs
*regs
,
1679 struct image_info
*infop
)
1681 regs
->usp
= infop
->start_stack
;
1683 regs
->pc
= infop
->entry
;
1686 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1688 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1690 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUM68KState
*env
)
1692 (*regs
)[0] = tswapreg(env
->dregs
[1]);
1693 (*regs
)[1] = tswapreg(env
->dregs
[2]);
1694 (*regs
)[2] = tswapreg(env
->dregs
[3]);
1695 (*regs
)[3] = tswapreg(env
->dregs
[4]);
1696 (*regs
)[4] = tswapreg(env
->dregs
[5]);
1697 (*regs
)[5] = tswapreg(env
->dregs
[6]);
1698 (*regs
)[6] = tswapreg(env
->dregs
[7]);
1699 (*regs
)[7] = tswapreg(env
->aregs
[0]);
1700 (*regs
)[8] = tswapreg(env
->aregs
[1]);
1701 (*regs
)[9] = tswapreg(env
->aregs
[2]);
1702 (*regs
)[10] = tswapreg(env
->aregs
[3]);
1703 (*regs
)[11] = tswapreg(env
->aregs
[4]);
1704 (*regs
)[12] = tswapreg(env
->aregs
[5]);
1705 (*regs
)[13] = tswapreg(env
->aregs
[6]);
1706 (*regs
)[14] = tswapreg(env
->dregs
[0]);
1707 (*regs
)[15] = tswapreg(env
->aregs
[7]);
1708 (*regs
)[16] = tswapreg(env
->dregs
[0]); /* FIXME: orig_d0 */
1709 (*regs
)[17] = tswapreg(env
->sr
);
1710 (*regs
)[18] = tswapreg(env
->pc
);
1711 (*regs
)[19] = 0; /* FIXME: regs->format | regs->vector */
1714 #define USE_ELF_CORE_DUMP
1715 #define ELF_EXEC_PAGESIZE 8192
1721 #define ELF_CLASS ELFCLASS64
1722 #define ELF_ARCH EM_ALPHA
1724 static inline void init_thread(struct target_pt_regs
*regs
,
1725 struct image_info
*infop
)
1727 regs
->pc
= infop
->entry
;
1729 regs
->usp
= infop
->start_stack
;
1732 #define ELF_EXEC_PAGESIZE 8192
1734 #endif /* TARGET_ALPHA */
1738 #define ELF_CLASS ELFCLASS64
1739 #define ELF_DATA ELFDATA2MSB
1740 #define ELF_ARCH EM_S390
1744 #define ELF_HWCAP get_elf_hwcap()
1746 #define GET_FEATURE(_feat, _hwcap) \
1747 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1749 uint32_t get_elf_hwcap(void)
1752 * Let's assume we always have esan3 and zarch.
1753 * 31-bit processes can use 64-bit registers (high gprs).
1755 uint32_t hwcap
= HWCAP_S390_ESAN3
| HWCAP_S390_ZARCH
| HWCAP_S390_HIGH_GPRS
;
1757 GET_FEATURE(S390_FEAT_STFLE
, HWCAP_S390_STFLE
);
1758 GET_FEATURE(S390_FEAT_MSA
, HWCAP_S390_MSA
);
1759 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT
, HWCAP_S390_LDISP
);
1760 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE
, HWCAP_S390_EIMM
);
1761 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3
) &&
1762 s390_has_feat(S390_FEAT_ETF3_ENH
)) {
1763 hwcap
|= HWCAP_S390_ETF3EH
;
1765 GET_FEATURE(S390_FEAT_VECTOR
, HWCAP_S390_VXRS
);
1766 GET_FEATURE(S390_FEAT_VECTOR_ENH
, HWCAP_S390_VXRS_EXT
);
1767 GET_FEATURE(S390_FEAT_VECTOR_ENH2
, HWCAP_S390_VXRS_EXT2
);
1772 const char *elf_hwcap_str(uint32_t bit
)
1774 static const char *hwcap_str
[] = {
1775 [HWCAP_S390_NR_ESAN3
] = "esan3",
1776 [HWCAP_S390_NR_ZARCH
] = "zarch",
1777 [HWCAP_S390_NR_STFLE
] = "stfle",
1778 [HWCAP_S390_NR_MSA
] = "msa",
1779 [HWCAP_S390_NR_LDISP
] = "ldisp",
1780 [HWCAP_S390_NR_EIMM
] = "eimm",
1781 [HWCAP_S390_NR_DFP
] = "dfp",
1782 [HWCAP_S390_NR_HPAGE
] = "edat",
1783 [HWCAP_S390_NR_ETF3EH
] = "etf3eh",
1784 [HWCAP_S390_NR_HIGH_GPRS
] = "highgprs",
1785 [HWCAP_S390_NR_TE
] = "te",
1786 [HWCAP_S390_NR_VXRS
] = "vx",
1787 [HWCAP_S390_NR_VXRS_BCD
] = "vxd",
1788 [HWCAP_S390_NR_VXRS_EXT
] = "vxe",
1789 [HWCAP_S390_NR_GS
] = "gs",
1790 [HWCAP_S390_NR_VXRS_EXT2
] = "vxe2",
1791 [HWCAP_S390_NR_VXRS_PDE
] = "vxp",
1792 [HWCAP_S390_NR_SORT
] = "sort",
1793 [HWCAP_S390_NR_DFLT
] = "dflt",
1794 [HWCAP_S390_NR_NNPA
] = "nnpa",
1795 [HWCAP_S390_NR_PCI_MIO
] = "pcimio",
1796 [HWCAP_S390_NR_SIE
] = "sie",
1799 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
1802 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1804 regs
->psw
.addr
= infop
->entry
;
1805 regs
->psw
.mask
= PSW_MASK_DAT
| PSW_MASK_IO
| PSW_MASK_EXT
| \
1806 PSW_MASK_MCHECK
| PSW_MASK_PSTATE
| PSW_MASK_64
| \
1808 regs
->gprs
[15] = infop
->start_stack
;
1811 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1813 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1816 TARGET_REG_PSWM
= 0,
1817 TARGET_REG_PSWA
= 1,
1818 TARGET_REG_GPRS
= 2,
1819 TARGET_REG_ARS
= 18,
1820 TARGET_REG_ORIG_R2
= 26,
1823 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1824 const CPUS390XState
*env
)
1829 (*regs
)[TARGET_REG_PSWM
] = tswapreg(env
->psw
.mask
);
1830 (*regs
)[TARGET_REG_PSWA
] = tswapreg(env
->psw
.addr
);
1831 for (i
= 0; i
< 16; i
++) {
1832 (*regs
)[TARGET_REG_GPRS
+ i
] = tswapreg(env
->regs
[i
]);
1834 aregs
= (uint32_t *)&((*regs
)[TARGET_REG_ARS
]);
1835 for (i
= 0; i
< 16; i
++) {
1836 aregs
[i
] = tswap32(env
->aregs
[i
]);
1838 (*regs
)[TARGET_REG_ORIG_R2
] = 0;
1841 #define USE_ELF_CORE_DUMP
1842 #define ELF_EXEC_PAGESIZE 4096
1844 #endif /* TARGET_S390X */
1848 #define ELF_ARCH EM_RISCV
1850 #ifdef TARGET_RISCV32
1851 #define ELF_CLASS ELFCLASS32
1853 #define ELF_CLASS ELFCLASS64
1856 #define ELF_HWCAP get_elf_hwcap()
1858 static uint32_t get_elf_hwcap(void)
1860 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1861 RISCVCPU
*cpu
= RISCV_CPU(thread_cpu
);
1862 uint32_t mask
= MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1863 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C')
1866 return cpu
->env
.misa_ext
& mask
;
1870 static inline void init_thread(struct target_pt_regs
*regs
,
1871 struct image_info
*infop
)
1873 regs
->sepc
= infop
->entry
;
1874 regs
->sp
= infop
->start_stack
;
1877 #define ELF_EXEC_PAGESIZE 4096
1879 #endif /* TARGET_RISCV */
1883 #define ELF_CLASS ELFCLASS32
1884 #define ELF_ARCH EM_PARISC
1885 #define ELF_PLATFORM "PARISC"
1886 #define STACK_GROWS_DOWN 0
1887 #define STACK_ALIGNMENT 64
1889 static inline void init_thread(struct target_pt_regs
*regs
,
1890 struct image_info
*infop
)
1892 regs
->iaoq
[0] = infop
->entry
;
1893 regs
->iaoq
[1] = infop
->entry
+ 4;
1895 regs
->gr
[24] = infop
->argv
;
1896 regs
->gr
[25] = infop
->argc
;
1897 /* The top-of-stack contains a linkage buffer. */
1898 regs
->gr
[30] = infop
->start_stack
+ 64;
1899 regs
->gr
[31] = infop
->entry
;
1902 #define LO_COMMPAGE 0
1904 static bool init_guest_commpage(void)
1906 void *want
= g2h_untagged(LO_COMMPAGE
);
1907 void *addr
= mmap(want
, qemu_host_page_size
, PROT_NONE
,
1908 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
1910 if (addr
== MAP_FAILED
) {
1911 perror("Allocating guest commpage");
1919 * On Linux, page zero is normally marked execute only + gateway.
1920 * Normal read or write is supposed to fail (thus PROT_NONE above),
1921 * but specific offsets have kernel code mapped to raise permissions
1922 * and implement syscalls. Here, simply mark the page executable.
1923 * Special case the entry points during translation (see do_page_zero).
1925 page_set_flags(LO_COMMPAGE
, LO_COMMPAGE
| ~TARGET_PAGE_MASK
,
1926 PAGE_EXEC
| PAGE_VALID
);
1930 #endif /* TARGET_HPPA */
1932 #ifdef TARGET_XTENSA
1934 #define ELF_CLASS ELFCLASS32
1935 #define ELF_ARCH EM_XTENSA
1937 static inline void init_thread(struct target_pt_regs
*regs
,
1938 struct image_info
*infop
)
1940 regs
->windowbase
= 0;
1941 regs
->windowstart
= 1;
1942 regs
->areg
[1] = infop
->start_stack
;
1943 regs
->pc
= infop
->entry
;
1944 if (info_is_fdpic(infop
)) {
1945 regs
->areg
[4] = infop
->loadmap_addr
;
1946 regs
->areg
[5] = infop
->interpreter_loadmap_addr
;
1947 if (infop
->interpreter_loadmap_addr
) {
1948 regs
->areg
[6] = infop
->interpreter_pt_dynamic_addr
;
1950 regs
->areg
[6] = infop
->pt_dynamic_addr
;
1955 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1956 #define ELF_NREG 128
1957 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1966 TARGET_REG_WINDOWSTART
,
1967 TARGET_REG_WINDOWBASE
,
1968 TARGET_REG_THREADPTR
,
1969 TARGET_REG_AR0
= 64,
1972 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1973 const CPUXtensaState
*env
)
1977 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1978 (*regs
)[TARGET_REG_PS
] = tswapreg(env
->sregs
[PS
] & ~PS_EXCM
);
1979 (*regs
)[TARGET_REG_LBEG
] = tswapreg(env
->sregs
[LBEG
]);
1980 (*regs
)[TARGET_REG_LEND
] = tswapreg(env
->sregs
[LEND
]);
1981 (*regs
)[TARGET_REG_LCOUNT
] = tswapreg(env
->sregs
[LCOUNT
]);
1982 (*regs
)[TARGET_REG_SAR
] = tswapreg(env
->sregs
[SAR
]);
1983 (*regs
)[TARGET_REG_WINDOWSTART
] = tswapreg(env
->sregs
[WINDOW_START
]);
1984 (*regs
)[TARGET_REG_WINDOWBASE
] = tswapreg(env
->sregs
[WINDOW_BASE
]);
1985 (*regs
)[TARGET_REG_THREADPTR
] = tswapreg(env
->uregs
[THREADPTR
]);
1986 xtensa_sync_phys_from_window((CPUXtensaState
*)env
);
1987 for (i
= 0; i
< env
->config
->nareg
; ++i
) {
1988 (*regs
)[TARGET_REG_AR0
+ i
] = tswapreg(env
->phys_regs
[i
]);
1992 #define USE_ELF_CORE_DUMP
1993 #define ELF_EXEC_PAGESIZE 4096
1995 #endif /* TARGET_XTENSA */
1997 #ifdef TARGET_HEXAGON
1999 #define ELF_CLASS ELFCLASS32
2000 #define ELF_ARCH EM_HEXAGON
2002 static inline void init_thread(struct target_pt_regs
*regs
,
2003 struct image_info
*infop
)
2005 regs
->sepc
= infop
->entry
;
2006 regs
->sp
= infop
->start_stack
;
2009 #endif /* TARGET_HEXAGON */
2011 #ifndef ELF_BASE_PLATFORM
2012 #define ELF_BASE_PLATFORM (NULL)
2015 #ifndef ELF_PLATFORM
2016 #define ELF_PLATFORM (NULL)
2020 #define ELF_MACHINE ELF_ARCH
2023 #ifndef elf_check_arch
2024 #define elf_check_arch(x) ((x) == ELF_ARCH)
2027 #ifndef elf_check_abi
2028 #define elf_check_abi(x) (1)
2035 #ifndef STACK_GROWS_DOWN
2036 #define STACK_GROWS_DOWN 1
2039 #ifndef STACK_ALIGNMENT
2040 #define STACK_ALIGNMENT 16
2045 #define ELF_CLASS ELFCLASS32
2047 #define bswaptls(ptr) bswap32s(ptr)
2050 #ifndef EXSTACK_DEFAULT
2051 #define EXSTACK_DEFAULT false
2056 /* We must delay the following stanzas until after "elf.h". */
2057 #if defined(TARGET_AARCH64)
2059 static bool arch_parse_elf_property(uint32_t pr_type
, uint32_t pr_datasz
,
2060 const uint32_t *data
,
2061 struct image_info
*info
,
2064 if (pr_type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
) {
2065 if (pr_datasz
!= sizeof(uint32_t)) {
2066 error_setg(errp
, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
2069 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
2070 info
->note_flags
= *data
;
2074 #define ARCH_USE_GNU_PROPERTY 1
2078 static bool arch_parse_elf_property(uint32_t pr_type
, uint32_t pr_datasz
,
2079 const uint32_t *data
,
2080 struct image_info
*info
,
2083 g_assert_not_reached();
2085 #define ARCH_USE_GNU_PROPERTY 0
2091 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
2092 unsigned int a_text
; /* length of text, in bytes */
2093 unsigned int a_data
; /* length of data, in bytes */
2094 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
2095 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
2096 unsigned int a_entry
; /* start address */
2097 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
2098 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
2102 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
2108 #define DLINFO_ITEMS 16
2110 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
2112 memcpy(to
, from
, n
);
2116 static void bswap_ehdr(struct elfhdr
*ehdr
)
2118 bswap16s(&ehdr
->e_type
); /* Object file type */
2119 bswap16s(&ehdr
->e_machine
); /* Architecture */
2120 bswap32s(&ehdr
->e_version
); /* Object file version */
2121 bswaptls(&ehdr
->e_entry
); /* Entry point virtual address */
2122 bswaptls(&ehdr
->e_phoff
); /* Program header table file offset */
2123 bswaptls(&ehdr
->e_shoff
); /* Section header table file offset */
2124 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
2125 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
2126 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
2127 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
2128 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
2129 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
2130 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
2133 static void bswap_phdr(struct elf_phdr
*phdr
, int phnum
)
2136 for (i
= 0; i
< phnum
; ++i
, ++phdr
) {
2137 bswap32s(&phdr
->p_type
); /* Segment type */
2138 bswap32s(&phdr
->p_flags
); /* Segment flags */
2139 bswaptls(&phdr
->p_offset
); /* Segment file offset */
2140 bswaptls(&phdr
->p_vaddr
); /* Segment virtual address */
2141 bswaptls(&phdr
->p_paddr
); /* Segment physical address */
2142 bswaptls(&phdr
->p_filesz
); /* Segment size in file */
2143 bswaptls(&phdr
->p_memsz
); /* Segment size in memory */
2144 bswaptls(&phdr
->p_align
); /* Segment alignment */
2148 static void bswap_shdr(struct elf_shdr
*shdr
, int shnum
)
2151 for (i
= 0; i
< shnum
; ++i
, ++shdr
) {
2152 bswap32s(&shdr
->sh_name
);
2153 bswap32s(&shdr
->sh_type
);
2154 bswaptls(&shdr
->sh_flags
);
2155 bswaptls(&shdr
->sh_addr
);
2156 bswaptls(&shdr
->sh_offset
);
2157 bswaptls(&shdr
->sh_size
);
2158 bswap32s(&shdr
->sh_link
);
2159 bswap32s(&shdr
->sh_info
);
2160 bswaptls(&shdr
->sh_addralign
);
2161 bswaptls(&shdr
->sh_entsize
);
2165 static void bswap_sym(struct elf_sym
*sym
)
2167 bswap32s(&sym
->st_name
);
2168 bswaptls(&sym
->st_value
);
2169 bswaptls(&sym
->st_size
);
2170 bswap16s(&sym
->st_shndx
);
2174 static void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
)
2176 bswap16s(&abiflags
->version
);
2177 bswap32s(&abiflags
->ases
);
2178 bswap32s(&abiflags
->isa_ext
);
2179 bswap32s(&abiflags
->flags1
);
2180 bswap32s(&abiflags
->flags2
);
2184 static inline void bswap_ehdr(struct elfhdr
*ehdr
) { }
2185 static inline void bswap_phdr(struct elf_phdr
*phdr
, int phnum
) { }
2186 static inline void bswap_shdr(struct elf_shdr
*shdr
, int shnum
) { }
2187 static inline void bswap_sym(struct elf_sym
*sym
) { }
2189 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
) { }
2193 #ifdef USE_ELF_CORE_DUMP
2194 static int elf_core_dump(int, const CPUArchState
*);
2195 #endif /* USE_ELF_CORE_DUMP */
2196 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
);
2198 /* Verify the portions of EHDR within E_IDENT for the target.
2199 This can be performed before bswapping the entire header. */
2200 static bool elf_check_ident(struct elfhdr
*ehdr
)
2202 return (ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
2203 && ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
2204 && ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
2205 && ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
2206 && ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
2207 && ehdr
->e_ident
[EI_DATA
] == ELF_DATA
2208 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
);
2211 /* Verify the portions of EHDR outside of E_IDENT for the target.
2212 This has to wait until after bswapping the header. */
2213 static bool elf_check_ehdr(struct elfhdr
*ehdr
)
2215 return (elf_check_arch(ehdr
->e_machine
)
2216 && elf_check_abi(ehdr
->e_flags
)
2217 && ehdr
->e_ehsize
== sizeof(struct elfhdr
)
2218 && ehdr
->e_phentsize
== sizeof(struct elf_phdr
)
2219 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
));
2223 * 'copy_elf_strings()' copies argument/envelope strings from user
2224 * memory to free pages in kernel mem. These are in a format ready
2225 * to be put directly into the top of new user memory.
2228 static abi_ulong
copy_elf_strings(int argc
, char **argv
, char *scratch
,
2229 abi_ulong p
, abi_ulong stack_limit
)
2236 return 0; /* bullet-proofing */
2239 if (STACK_GROWS_DOWN
) {
2240 int offset
= ((p
- 1) % TARGET_PAGE_SIZE
) + 1;
2241 for (i
= argc
- 1; i
>= 0; --i
) {
2244 fprintf(stderr
, "VFS: argc is wrong");
2247 len
= strlen(tmp
) + 1;
2250 if (len
> (p
- stack_limit
)) {
2254 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
2255 tmp
-= bytes_to_copy
;
2257 offset
-= bytes_to_copy
;
2258 len
-= bytes_to_copy
;
2260 memcpy_fromfs(scratch
+ offset
, tmp
, bytes_to_copy
);
2263 memcpy_to_target(p
, scratch
, top
- p
);
2265 offset
= TARGET_PAGE_SIZE
;
2270 memcpy_to_target(p
, scratch
+ offset
, top
- p
);
2273 int remaining
= TARGET_PAGE_SIZE
- (p
% TARGET_PAGE_SIZE
);
2274 for (i
= 0; i
< argc
; ++i
) {
2277 fprintf(stderr
, "VFS: argc is wrong");
2280 len
= strlen(tmp
) + 1;
2281 if (len
> (stack_limit
- p
)) {
2285 int bytes_to_copy
= (len
> remaining
) ? remaining
: len
;
2287 memcpy_fromfs(scratch
+ (p
- top
), tmp
, bytes_to_copy
);
2289 tmp
+= bytes_to_copy
;
2290 remaining
-= bytes_to_copy
;
2292 len
-= bytes_to_copy
;
2294 if (remaining
== 0) {
2295 memcpy_to_target(top
, scratch
, p
- top
);
2297 remaining
= TARGET_PAGE_SIZE
;
2302 memcpy_to_target(top
, scratch
, p
- top
);
2309 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2310 * argument/environment space. Newer kernels (>2.6.33) allow more,
2311 * dependent on stack size, but guarantee at least 32 pages for
2312 * backwards compatibility.
2314 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2316 static abi_ulong
setup_arg_pages(struct linux_binprm
*bprm
,
2317 struct image_info
*info
)
2319 abi_ulong size
, error
, guard
;
2322 size
= guest_stack_size
;
2323 if (size
< STACK_LOWER_LIMIT
) {
2324 size
= STACK_LOWER_LIMIT
;
2327 if (STACK_GROWS_DOWN
) {
2328 guard
= TARGET_PAGE_SIZE
;
2329 if (guard
< qemu_real_host_page_size()) {
2330 guard
= qemu_real_host_page_size();
2333 /* no guard page for hppa target where stack grows upwards. */
2337 prot
= PROT_READ
| PROT_WRITE
;
2338 if (info
->exec_stack
) {
2341 error
= target_mmap(0, size
+ guard
, prot
,
2342 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2344 perror("mmap stack");
2348 /* We reserve one extra page at the top of the stack as guard. */
2349 if (STACK_GROWS_DOWN
) {
2350 target_mprotect(error
, guard
, PROT_NONE
);
2351 info
->stack_limit
= error
+ guard
;
2352 return info
->stack_limit
+ size
- sizeof(void *);
2354 info
->stack_limit
= error
+ size
;
2362 * Map and zero the bss. We need to explicitly zero any fractional pages
2363 * after the data section (i.e. bss). Return false on mapping failure.
2365 static bool zero_bss(abi_ulong start_bss
, abi_ulong end_bss
,
2366 int prot
, Error
**errp
)
2368 abi_ulong align_bss
;
2370 /* We only expect writable bss; the code segment shouldn't need this. */
2371 if (!(prot
& PROT_WRITE
)) {
2372 error_setg(errp
, "PT_LOAD with non-writable bss");
2376 align_bss
= TARGET_PAGE_ALIGN(start_bss
);
2377 end_bss
= TARGET_PAGE_ALIGN(end_bss
);
2379 if (start_bss
< align_bss
) {
2380 int flags
= page_get_flags(start_bss
);
2382 if (!(flags
& PAGE_BITS
)) {
2384 * The whole address space of the executable was reserved
2385 * at the start, therefore all pages will be VALID.
2386 * But assuming there are no PROT_NONE PT_LOAD segments,
2387 * a PROT_NONE page means no data all bss, and we can
2388 * simply extend the new anon mapping back to the start
2389 * of the page of bss.
2391 align_bss
-= TARGET_PAGE_SIZE
;
2394 * The start of the bss shares a page with something.
2395 * The only thing that we expect is the data section,
2396 * which would already be marked writable.
2397 * Overlapping the RX code segment seems malformed.
2399 if (!(flags
& PAGE_WRITE
)) {
2400 error_setg(errp
, "PT_LOAD with bss overlapping "
2401 "non-writable page");
2405 /* The page is already mapped and writable. */
2406 memset(g2h_untagged(start_bss
), 0, align_bss
- start_bss
);
2410 if (align_bss
< end_bss
&&
2411 target_mmap(align_bss
, end_bss
- align_bss
, prot
,
2412 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
, -1, 0) == -1) {
2413 error_setg_errno(errp
, errno
, "Error mapping bss");
2419 #if defined(TARGET_ARM)
2420 static int elf_is_fdpic(struct elfhdr
*exec
)
2422 return exec
->e_ident
[EI_OSABI
] == ELFOSABI_ARM_FDPIC
;
2424 #elif defined(TARGET_XTENSA)
2425 static int elf_is_fdpic(struct elfhdr
*exec
)
2427 return exec
->e_ident
[EI_OSABI
] == ELFOSABI_XTENSA_FDPIC
;
2430 /* Default implementation, always false. */
2431 static int elf_is_fdpic(struct elfhdr
*exec
)
2437 static abi_ulong
loader_build_fdpic_loadmap(struct image_info
*info
, abi_ulong sp
)
2440 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
;
2442 /* elf32_fdpic_loadseg */
2446 put_user_u32(loadsegs
[n
].addr
, sp
+0);
2447 put_user_u32(loadsegs
[n
].p_vaddr
, sp
+4);
2448 put_user_u32(loadsegs
[n
].p_memsz
, sp
+8);
2451 /* elf32_fdpic_loadmap */
2453 put_user_u16(0, sp
+0); /* version */
2454 put_user_u16(info
->nsegs
, sp
+2); /* nsegs */
2456 info
->personality
= PER_LINUX_FDPIC
;
2457 info
->loadmap_addr
= sp
;
2462 static abi_ulong
create_elf_tables(abi_ulong p
, int argc
, int envc
,
2463 struct elfhdr
*exec
,
2464 struct image_info
*info
,
2465 struct image_info
*interp_info
)
2468 abi_ulong u_argc
, u_argv
, u_envp
, u_auxv
;
2471 abi_ulong u_rand_bytes
;
2472 uint8_t k_rand_bytes
[16];
2473 abi_ulong u_platform
, u_base_platform
;
2474 const char *k_platform
, *k_base_platform
;
2475 const int n
= sizeof(elf_addr_t
);
2479 /* Needs to be before we load the env/argc/... */
2480 if (elf_is_fdpic(exec
)) {
2481 /* Need 4 byte alignment for these structs */
2483 sp
= loader_build_fdpic_loadmap(info
, sp
);
2484 info
->other_info
= interp_info
;
2486 interp_info
->other_info
= info
;
2487 sp
= loader_build_fdpic_loadmap(interp_info
, sp
);
2488 info
->interpreter_loadmap_addr
= interp_info
->loadmap_addr
;
2489 info
->interpreter_pt_dynamic_addr
= interp_info
->pt_dynamic_addr
;
2491 info
->interpreter_loadmap_addr
= 0;
2492 info
->interpreter_pt_dynamic_addr
= 0;
2496 u_base_platform
= 0;
2497 k_base_platform
= ELF_BASE_PLATFORM
;
2498 if (k_base_platform
) {
2499 size_t len
= strlen(k_base_platform
) + 1;
2500 if (STACK_GROWS_DOWN
) {
2501 sp
-= (len
+ n
- 1) & ~(n
- 1);
2502 u_base_platform
= sp
;
2503 /* FIXME - check return value of memcpy_to_target() for failure */
2504 memcpy_to_target(sp
, k_base_platform
, len
);
2506 memcpy_to_target(sp
, k_base_platform
, len
);
2507 u_base_platform
= sp
;
2513 k_platform
= ELF_PLATFORM
;
2515 size_t len
= strlen(k_platform
) + 1;
2516 if (STACK_GROWS_DOWN
) {
2517 sp
-= (len
+ n
- 1) & ~(n
- 1);
2519 /* FIXME - check return value of memcpy_to_target() for failure */
2520 memcpy_to_target(sp
, k_platform
, len
);
2522 memcpy_to_target(sp
, k_platform
, len
);
2528 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2529 * the argv and envp pointers.
2531 if (STACK_GROWS_DOWN
) {
2532 sp
= QEMU_ALIGN_DOWN(sp
, 16);
2534 sp
= QEMU_ALIGN_UP(sp
, 16);
2538 * Generate 16 random bytes for userspace PRNG seeding.
2540 qemu_guest_getrandom_nofail(k_rand_bytes
, sizeof(k_rand_bytes
));
2541 if (STACK_GROWS_DOWN
) {
2544 /* FIXME - check return value of memcpy_to_target() for failure */
2545 memcpy_to_target(sp
, k_rand_bytes
, 16);
2547 memcpy_to_target(sp
, k_rand_bytes
, 16);
2552 size
= (DLINFO_ITEMS
+ 1) * 2;
2553 if (k_base_platform
)
2557 #ifdef DLINFO_ARCH_ITEMS
2558 size
+= DLINFO_ARCH_ITEMS
* 2;
2563 info
->auxv_len
= size
* n
;
2565 size
+= envc
+ argc
+ 2;
2566 size
+= 1; /* argc itself */
2569 /* Allocate space and finalize stack alignment for entry now. */
2570 if (STACK_GROWS_DOWN
) {
2571 u_argc
= QEMU_ALIGN_DOWN(sp
- size
, STACK_ALIGNMENT
);
2575 sp
= QEMU_ALIGN_UP(sp
+ size
, STACK_ALIGNMENT
);
2578 u_argv
= u_argc
+ n
;
2579 u_envp
= u_argv
+ (argc
+ 1) * n
;
2580 u_auxv
= u_envp
+ (envc
+ 1) * n
;
2581 info
->saved_auxv
= u_auxv
;
2584 info
->argv
= u_argv
;
2585 info
->envp
= u_envp
;
2587 /* This is correct because Linux defines
2588 * elf_addr_t as Elf32_Off / Elf64_Off
2590 #define NEW_AUX_ENT(id, val) do { \
2591 put_user_ual(id, u_auxv); u_auxv += n; \
2592 put_user_ual(val, u_auxv); u_auxv += n; \
2597 * ARCH_DLINFO must come first so platform specific code can enforce
2598 * special alignment requirements on the AUXV if necessary (eg. PPC).
2602 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2603 * on info->auxv_len will trigger.
2605 NEW_AUX_ENT(AT_PHDR
, (abi_ulong
)(info
->load_addr
+ exec
->e_phoff
));
2606 NEW_AUX_ENT(AT_PHENT
, (abi_ulong
)(sizeof (struct elf_phdr
)));
2607 NEW_AUX_ENT(AT_PHNUM
, (abi_ulong
)(exec
->e_phnum
));
2608 if ((info
->alignment
& ~qemu_host_page_mask
) != 0) {
2609 /* Target doesn't support host page size alignment */
2610 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(TARGET_PAGE_SIZE
));
2612 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(MAX(TARGET_PAGE_SIZE
,
2613 qemu_host_page_size
)));
2615 NEW_AUX_ENT(AT_BASE
, (abi_ulong
)(interp_info
? interp_info
->load_addr
: 0));
2616 NEW_AUX_ENT(AT_FLAGS
, (abi_ulong
)0);
2617 NEW_AUX_ENT(AT_ENTRY
, info
->entry
);
2618 NEW_AUX_ENT(AT_UID
, (abi_ulong
) getuid());
2619 NEW_AUX_ENT(AT_EUID
, (abi_ulong
) geteuid());
2620 NEW_AUX_ENT(AT_GID
, (abi_ulong
) getgid());
2621 NEW_AUX_ENT(AT_EGID
, (abi_ulong
) getegid());
2622 NEW_AUX_ENT(AT_HWCAP
, (abi_ulong
) ELF_HWCAP
);
2623 NEW_AUX_ENT(AT_CLKTCK
, (abi_ulong
) sysconf(_SC_CLK_TCK
));
2624 NEW_AUX_ENT(AT_RANDOM
, (abi_ulong
) u_rand_bytes
);
2625 NEW_AUX_ENT(AT_SECURE
, (abi_ulong
) qemu_getauxval(AT_SECURE
));
2626 NEW_AUX_ENT(AT_EXECFN
, info
->file_string
);
2629 NEW_AUX_ENT(AT_HWCAP2
, (abi_ulong
) ELF_HWCAP2
);
2632 if (u_base_platform
) {
2633 NEW_AUX_ENT(AT_BASE_PLATFORM
, u_base_platform
);
2636 NEW_AUX_ENT(AT_PLATFORM
, u_platform
);
2638 NEW_AUX_ENT (AT_NULL
, 0);
2641 /* Check that our initial calculation of the auxv length matches how much
2642 * we actually put into it.
2644 assert(info
->auxv_len
== u_auxv
- info
->saved_auxv
);
2646 put_user_ual(argc
, u_argc
);
2648 p
= info
->arg_strings
;
2649 for (i
= 0; i
< argc
; ++i
) {
2650 put_user_ual(p
, u_argv
);
2652 p
+= target_strlen(p
) + 1;
2654 put_user_ual(0, u_argv
);
2656 p
= info
->env_strings
;
2657 for (i
= 0; i
< envc
; ++i
) {
2658 put_user_ual(p
, u_envp
);
2660 p
+= target_strlen(p
) + 1;
2662 put_user_ual(0, u_envp
);
2667 #if defined(HI_COMMPAGE)
2668 #define LO_COMMPAGE -1
2669 #elif defined(LO_COMMPAGE)
2670 #define HI_COMMPAGE 0
2672 #define HI_COMMPAGE 0
2673 #define LO_COMMPAGE -1
2674 #ifndef INIT_GUEST_COMMPAGE
2675 #define init_guest_commpage() true
2681 * @addr: host start address
2682 * @addr_last: host last address
2683 * @keep: do not unmap the probe region
2685 * Return 1 if [@addr, @addr_last] is not mapped in the host,
2686 * return 0 if it is not available to map, and -1 on mmap error.
2687 * If @keep, the region is left mapped on success, otherwise unmapped.
2689 static int pgb_try_mmap(uintptr_t addr
, uintptr_t addr_last
, bool keep
)
2691 size_t size
= addr_last
- addr
+ 1;
2692 void *p
= mmap((void *)addr
, size
, PROT_NONE
,
2693 MAP_ANONYMOUS
| MAP_PRIVATE
|
2694 MAP_NORESERVE
| MAP_FIXED_NOREPLACE
, -1, 0);
2697 if (p
== MAP_FAILED
) {
2698 return errno
== EEXIST
? 0 : -1;
2700 ret
= p
== (void *)addr
;
2701 if (!keep
|| !ret
) {
2708 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk)
2709 * @addr: host address
2710 * @addr_last: host last address
2713 * Like pgb_try_mmap, but additionally reserve some memory following brk.
2715 static int pgb_try_mmap_skip_brk(uintptr_t addr
, uintptr_t addr_last
,
2716 uintptr_t brk
, bool keep
)
2718 uintptr_t brk_last
= brk
+ 16 * MiB
- 1;
2720 /* Do not map anything close to the host brk. */
2721 if (addr
<= brk_last
&& brk
<= addr_last
) {
2724 return pgb_try_mmap(addr
, addr_last
, keep
);
2729 * @ga: set of guest addrs
2733 * Return true if all @ga can be mapped by the host at @base.
2734 * On success, retain the mapping at index 0 for reserved_va.
2737 typedef struct PGBAddrs
{
2738 uintptr_t bounds
[3][2]; /* start/last pairs */
2742 static bool pgb_try_mmap_set(const PGBAddrs
*ga
, uintptr_t base
, uintptr_t brk
)
2744 for (int i
= ga
->nbounds
- 1; i
>= 0; --i
) {
2745 if (pgb_try_mmap_skip_brk(ga
->bounds
[i
][0] + base
,
2746 ga
->bounds
[i
][1] + base
,
2747 brk
, i
== 0 && reserved_va
) <= 0) {
2756 * @ga: output set of guest addrs
2757 * @guest_loaddr: guest image low address
2758 * @guest_loaddr: guest image high address
2759 * @identity: create for identity mapping
2761 * Fill in @ga with the image, COMMPAGE and NULL page.
2763 static bool pgb_addr_set(PGBAddrs
*ga
, abi_ulong guest_loaddr
,
2764 abi_ulong guest_hiaddr
, bool try_identity
)
2769 * With a low commpage, or a guest mapped very low,
2770 * we may not be able to use the identity map.
2773 if (LO_COMMPAGE
!= -1 && LO_COMMPAGE
< mmap_min_addr
) {
2776 if (guest_loaddr
!= 0 && guest_loaddr
< mmap_min_addr
) {
2781 memset(ga
, 0, sizeof(*ga
));
2785 ga
->bounds
[n
][0] = try_identity
? mmap_min_addr
: 0;
2786 ga
->bounds
[n
][1] = reserved_va
;
2788 /* LO_COMMPAGE and NULL handled by reserving from 0. */
2790 /* Add any LO_COMMPAGE or NULL page. */
2791 if (LO_COMMPAGE
!= -1) {
2792 ga
->bounds
[n
][0] = 0;
2793 ga
->bounds
[n
][1] = LO_COMMPAGE
+ TARGET_PAGE_SIZE
- 1;
2795 } else if (!try_identity
) {
2796 ga
->bounds
[n
][0] = 0;
2797 ga
->bounds
[n
][1] = TARGET_PAGE_SIZE
- 1;
2801 /* Add the guest image for ET_EXEC. */
2803 ga
->bounds
[n
][0] = guest_loaddr
;
2804 ga
->bounds
[n
][1] = guest_hiaddr
;
2810 * Temporarily disable
2811 * "comparison is always false due to limited range of data type"
2812 * due to comparison between unsigned and (possible) 0.
2814 #pragma GCC diagnostic push
2815 #pragma GCC diagnostic ignored "-Wtype-limits"
2817 /* Add any HI_COMMPAGE not covered by reserved_va. */
2818 if (reserved_va
< HI_COMMPAGE
) {
2819 ga
->bounds
[n
][0] = HI_COMMPAGE
& qemu_host_page_mask
;
2820 ga
->bounds
[n
][1] = HI_COMMPAGE
+ TARGET_PAGE_SIZE
- 1;
2824 #pragma GCC diagnostic pop
2830 static void pgb_fail_in_use(const char *image_name
)
2832 error_report("%s: requires virtual address space that is in use "
2833 "(omit the -B option or choose a different value)",
2838 static void pgb_fixed(const char *image_name
, uintptr_t guest_loaddr
,
2839 uintptr_t guest_hiaddr
, uintptr_t align
)
2842 uintptr_t brk
= (uintptr_t)sbrk(0);
2844 if (!QEMU_IS_ALIGNED(guest_base
, align
)) {
2845 fprintf(stderr
, "Requested guest base %p does not satisfy "
2846 "host minimum alignment (0x%" PRIxPTR
")\n",
2847 (void *)guest_base
, align
);
2851 if (!pgb_addr_set(&ga
, guest_loaddr
, guest_hiaddr
, !guest_base
)
2852 || !pgb_try_mmap_set(&ga
, guest_base
, brk
)) {
2853 pgb_fail_in_use(image_name
);
2858 * pgb_find_fallback:
2860 * This is a fallback method for finding holes in the host address space
2861 * if we don't have the benefit of being able to access /proc/self/map.
2862 * It can potentially take a very long time as we can only dumbly iterate
2863 * up the host address space seeing if the allocation would work.
2865 static uintptr_t pgb_find_fallback(const PGBAddrs
*ga
, uintptr_t align
,
2868 /* TODO: come up with a better estimate of how much to skip. */
2869 uintptr_t skip
= sizeof(uintptr_t) == 4 ? MiB
: GiB
;
2871 for (uintptr_t base
= skip
; ; base
+= skip
) {
2872 base
= ROUND_UP(base
, align
);
2873 if (pgb_try_mmap_set(ga
, base
, brk
)) {
2876 if (base
>= -skip
) {
2882 static uintptr_t pgb_try_itree(const PGBAddrs
*ga
, uintptr_t base
,
2883 IntervalTreeRoot
*root
)
2885 for (int i
= ga
->nbounds
- 1; i
>= 0; --i
) {
2886 uintptr_t s
= base
+ ga
->bounds
[i
][0];
2887 uintptr_t l
= base
+ ga
->bounds
[i
][1];
2888 IntervalTreeNode
*n
;
2891 /* Wraparound. Skip to advance S to mmap_min_addr. */
2892 return mmap_min_addr
- s
;
2895 n
= interval_tree_iter_first(root
, s
, l
);
2897 /* Conflict. Skip to advance S to LAST + 1. */
2898 return n
->last
- s
+ 1;
2901 return 0; /* success */
2904 static uintptr_t pgb_find_itree(const PGBAddrs
*ga
, IntervalTreeRoot
*root
,
2905 uintptr_t align
, uintptr_t brk
)
2907 uintptr_t last
= mmap_min_addr
;
2908 uintptr_t base
, skip
;
2911 base
= ROUND_UP(last
, align
);
2916 skip
= pgb_try_itree(ga
, base
, root
);
2928 * We've chosen 'base' based on holes in the interval tree,
2929 * but we don't yet know if it is a valid host address.
2930 * Because it is the first matching hole, if the host addresses
2931 * are invalid we know there are no further matches.
2933 return pgb_try_mmap_set(ga
, base
, brk
) ? base
: -1;
2936 static void pgb_dynamic(const char *image_name
, uintptr_t guest_loaddr
,
2937 uintptr_t guest_hiaddr
, uintptr_t align
)
2939 IntervalTreeRoot
*root
;
2943 assert(QEMU_IS_ALIGNED(guest_loaddr
, align
));
2945 /* Try the identity map first. */
2946 if (pgb_addr_set(&ga
, guest_loaddr
, guest_hiaddr
, true)) {
2947 brk
= (uintptr_t)sbrk(0);
2948 if (pgb_try_mmap_set(&ga
, 0, brk
)) {
2955 * Rebuild the address set for non-identity map.
2956 * This differs in the mapping of the guest NULL page.
2958 pgb_addr_set(&ga
, guest_loaddr
, guest_hiaddr
, false);
2960 root
= read_self_maps();
2962 /* Read brk after we've read the maps, which will malloc. */
2963 brk
= (uintptr_t)sbrk(0);
2966 ret
= pgb_find_fallback(&ga
, align
, brk
);
2969 * Reserve the area close to the host brk.
2970 * This will be freed with the rest of the tree.
2972 IntervalTreeNode
*b
= g_new0(IntervalTreeNode
, 1);
2974 b
->last
= brk
+ 16 * MiB
- 1;
2975 interval_tree_insert(b
, root
);
2977 ret
= pgb_find_itree(&ga
, root
, align
, brk
);
2978 free_self_maps(root
);
2982 int w
= TARGET_LONG_BITS
/ 4;
2984 error_report("%s: Unable to find a guest_base to satisfy all "
2985 "guest address mapping requirements", image_name
);
2987 for (int i
= 0; i
< ga
.nbounds
; ++i
) {
2988 error_printf(" %0*" PRIx64
"-%0*" PRIx64
"\n",
2989 w
, (uint64_t)ga
.bounds
[i
][0],
2990 w
, (uint64_t)ga
.bounds
[i
][1]);
2997 void probe_guest_base(const char *image_name
, abi_ulong guest_loaddr
,
2998 abi_ulong guest_hiaddr
)
3000 /* In order to use host shmat, we must be able to honor SHMLBA. */
3001 uintptr_t align
= MAX(SHMLBA
, qemu_host_page_size
);
3003 /* Sanity check the guest binary. */
3005 if (guest_hiaddr
> reserved_va
) {
3006 error_report("%s: requires more than reserved virtual "
3007 "address space (0x%" PRIx64
" > 0x%lx)",
3008 image_name
, (uint64_t)guest_hiaddr
, reserved_va
);
3012 if (guest_hiaddr
!= (uintptr_t)guest_hiaddr
) {
3013 error_report("%s: requires more virtual address space "
3014 "than the host can provide (0x%" PRIx64
")",
3015 image_name
, (uint64_t)guest_hiaddr
+ 1);
3020 if (have_guest_base
) {
3021 pgb_fixed(image_name
, guest_loaddr
, guest_hiaddr
, align
);
3023 pgb_dynamic(image_name
, guest_loaddr
, guest_hiaddr
, align
);
3026 /* Reserve and initialize the commpage. */
3027 if (!init_guest_commpage()) {
3028 /* We have already probed for the commpage being free. */
3029 g_assert_not_reached();
3032 assert(QEMU_IS_ALIGNED(guest_base
, align
));
3033 qemu_log_mask(CPU_LOG_PAGE
, "Locating guest address space "
3034 "@ 0x%" PRIx64
"\n", (uint64_t)guest_base
);
3038 /* The string "GNU\0" as a magic number. */
3039 GNU0_MAGIC
= const_le32('G' | 'N' << 8 | 'U' << 16),
3040 NOTE_DATA_SZ
= 1 * KiB
,
3042 ELF_GNU_PROPERTY_ALIGN
= ELF_CLASS
== ELFCLASS32
? 4 : 8,
3046 * Process a single gnu_property entry.
3047 * Return false for error.
3049 static bool parse_elf_property(const uint32_t *data
, int *off
, int datasz
,
3050 struct image_info
*info
, bool have_prev_type
,
3051 uint32_t *prev_type
, Error
**errp
)
3053 uint32_t pr_type
, pr_datasz
, step
;
3055 if (*off
> datasz
|| !QEMU_IS_ALIGNED(*off
, ELF_GNU_PROPERTY_ALIGN
)) {
3059 data
+= *off
/ sizeof(uint32_t);
3061 if (datasz
< 2 * sizeof(uint32_t)) {
3065 pr_datasz
= data
[1];
3067 datasz
-= 2 * sizeof(uint32_t);
3068 step
= ROUND_UP(pr_datasz
, ELF_GNU_PROPERTY_ALIGN
);
3069 if (step
> datasz
) {
3073 /* Properties are supposed to be unique and sorted on pr_type. */
3074 if (have_prev_type
&& pr_type
<= *prev_type
) {
3075 if (pr_type
== *prev_type
) {
3076 error_setg(errp
, "Duplicate property in PT_GNU_PROPERTY");
3078 error_setg(errp
, "Unsorted property in PT_GNU_PROPERTY");
3082 *prev_type
= pr_type
;
3084 if (!arch_parse_elf_property(pr_type
, pr_datasz
, data
, info
, errp
)) {
3088 *off
+= 2 * sizeof(uint32_t) + step
;
3092 error_setg(errp
, "Ill-formed property in PT_GNU_PROPERTY");
3096 /* Process NT_GNU_PROPERTY_TYPE_0. */
3097 static bool parse_elf_properties(int image_fd
,
3098 struct image_info
*info
,
3099 const struct elf_phdr
*phdr
,
3100 char bprm_buf
[BPRM_BUF_SIZE
],
3104 struct elf_note nhdr
;
3105 uint32_t data
[NOTE_DATA_SZ
/ sizeof(uint32_t)];
3109 bool have_prev_type
;
3112 /* Unless the arch requires properties, ignore them. */
3113 if (!ARCH_USE_GNU_PROPERTY
) {
3117 /* If the properties are crazy large, that's too bad. */
3119 if (n
> sizeof(note
)) {
3120 error_setg(errp
, "PT_GNU_PROPERTY too large");
3123 if (n
< sizeof(note
.nhdr
)) {
3124 error_setg(errp
, "PT_GNU_PROPERTY too small");
3128 if (phdr
->p_offset
+ n
<= BPRM_BUF_SIZE
) {
3129 memcpy(¬e
, bprm_buf
+ phdr
->p_offset
, n
);
3131 ssize_t len
= pread(image_fd
, ¬e
, n
, phdr
->p_offset
);
3133 error_setg_errno(errp
, errno
, "Error reading file header");
3139 * The contents of a valid PT_GNU_PROPERTY is a sequence
3140 * of uint32_t -- swap them all now.
3143 for (int i
= 0; i
< n
/ 4; i
++) {
3144 bswap32s(note
.data
+ i
);
3149 * Note that nhdr is 3 words, and that the "name" described by namesz
3150 * immediately follows nhdr and is thus at the 4th word. Further, all
3151 * of the inputs to the kernel's round_up are multiples of 4.
3153 if (note
.nhdr
.n_type
!= NT_GNU_PROPERTY_TYPE_0
||
3154 note
.nhdr
.n_namesz
!= NOTE_NAME_SZ
||
3155 note
.data
[3] != GNU0_MAGIC
) {
3156 error_setg(errp
, "Invalid note in PT_GNU_PROPERTY");
3159 off
= sizeof(note
.nhdr
) + NOTE_NAME_SZ
;
3161 datasz
= note
.nhdr
.n_descsz
+ off
;
3163 error_setg(errp
, "Invalid note size in PT_GNU_PROPERTY");
3167 have_prev_type
= false;
3170 if (off
== datasz
) {
3171 return true; /* end, exit ok */
3173 if (!parse_elf_property(note
.data
, &off
, datasz
, info
,
3174 have_prev_type
, &prev_type
, errp
)) {
3177 have_prev_type
= true;
3181 /* Load an ELF image into the address space.
3183 IMAGE_NAME is the filename of the image, to use in error messages.
3184 IMAGE_FD is the open file descriptor for the image.
3186 BPRM_BUF is a copy of the beginning of the file; this of course
3187 contains the elf file header at offset 0. It is assumed that this
3188 buffer is sufficiently aligned to present no problems to the host
3189 in accessing data at aligned offsets within the buffer.
3191 On return: INFO values will be filled in, as necessary or available. */
3193 static void load_elf_image(const char *image_name
, int image_fd
,
3194 struct image_info
*info
, char **pinterp_name
,
3195 char bprm_buf
[BPRM_BUF_SIZE
])
3197 struct elfhdr
*ehdr
= (struct elfhdr
*)bprm_buf
;
3198 struct elf_phdr
*phdr
;
3199 abi_ulong load_addr
, load_bias
, loaddr
, hiaddr
, error
;
3200 int i
, retval
, prot_exec
;
3203 /* First of all, some simple consistency checks */
3204 if (!elf_check_ident(ehdr
)) {
3205 error_setg(&err
, "Invalid ELF image for this architecture");
3209 if (!elf_check_ehdr(ehdr
)) {
3210 error_setg(&err
, "Invalid ELF image for this architecture");
3214 i
= ehdr
->e_phnum
* sizeof(struct elf_phdr
);
3215 if (ehdr
->e_phoff
+ i
<= BPRM_BUF_SIZE
) {
3216 phdr
= (struct elf_phdr
*)(bprm_buf
+ ehdr
->e_phoff
);
3218 phdr
= (struct elf_phdr
*) alloca(i
);
3219 retval
= pread(image_fd
, phdr
, i
, ehdr
->e_phoff
);
3224 bswap_phdr(phdr
, ehdr
->e_phnum
);
3227 info
->pt_dynamic_addr
= 0;
3232 * Find the maximum size of the image and allocate an appropriate
3233 * amount of memory to handle that. Locate the interpreter, if any.
3235 loaddr
= -1, hiaddr
= 0;
3236 info
->alignment
= 0;
3237 info
->exec_stack
= EXSTACK_DEFAULT
;
3238 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
3239 struct elf_phdr
*eppnt
= phdr
+ i
;
3240 if (eppnt
->p_type
== PT_LOAD
) {
3241 abi_ulong a
= eppnt
->p_vaddr
- eppnt
->p_offset
;
3245 a
= eppnt
->p_vaddr
+ eppnt
->p_memsz
- 1;
3250 info
->alignment
|= eppnt
->p_align
;
3251 } else if (eppnt
->p_type
== PT_INTERP
&& pinterp_name
) {
3252 g_autofree
char *interp_name
= NULL
;
3254 if (*pinterp_name
) {
3255 error_setg(&err
, "Multiple PT_INTERP entries");
3259 interp_name
= g_malloc(eppnt
->p_filesz
);
3261 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
3262 memcpy(interp_name
, bprm_buf
+ eppnt
->p_offset
,
3265 retval
= pread(image_fd
, interp_name
, eppnt
->p_filesz
,
3267 if (retval
!= eppnt
->p_filesz
) {
3271 if (interp_name
[eppnt
->p_filesz
- 1] != 0) {
3272 error_setg(&err
, "Invalid PT_INTERP entry");
3275 *pinterp_name
= g_steal_pointer(&interp_name
);
3276 } else if (eppnt
->p_type
== PT_GNU_PROPERTY
) {
3277 if (!parse_elf_properties(image_fd
, info
, eppnt
, bprm_buf
, &err
)) {
3280 } else if (eppnt
->p_type
== PT_GNU_STACK
) {
3281 info
->exec_stack
= eppnt
->p_flags
& PF_X
;
3287 if (pinterp_name
!= NULL
) {
3288 if (ehdr
->e_type
== ET_EXEC
) {
3290 * Make sure that the low address does not conflict with
3291 * MMAP_MIN_ADDR or the QEMU application itself.
3293 probe_guest_base(image_name
, loaddr
, hiaddr
);
3298 * The binary is dynamic, but we still need to
3299 * select guest_base. In this case we pass a size.
3301 probe_guest_base(image_name
, 0, hiaddr
- loaddr
);
3304 * Avoid collision with the loader by providing a different
3305 * default load address.
3307 load_addr
+= elf_et_dyn_base
;
3310 * TODO: Better support for mmap alignment is desirable.
3311 * Since we do not have complete control over the guest
3312 * address space, we prefer the kernel to choose some address
3313 * rather than force the use of LOAD_ADDR via MAP_FIXED.
3314 * But without MAP_FIXED we cannot guarantee alignment,
3317 align
= pow2ceil(info
->alignment
);
3319 load_addr
&= -align
;
3325 * Reserve address space for all of this.
3327 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get
3328 * exactly the address range that is required. Without reserved_va,
3329 * the guest address space is not isolated. We have attempted to avoid
3330 * conflict with the host program itself via probe_guest_base, but using
3331 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check.
3333 * Otherwise this is ET_DYN, and we are searching for a location
3334 * that can hold the memory space required. If the image is
3335 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should
3336 * honor that address if it happens to be free.
3338 * In both cases, we will overwrite pages in this range with mappings
3339 * from the executable.
3341 load_addr
= target_mmap(load_addr
, (size_t)hiaddr
- loaddr
+ 1, PROT_NONE
,
3342 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
|
3343 (ehdr
->e_type
== ET_EXEC
? MAP_FIXED_NOREPLACE
: 0),
3345 if (load_addr
== -1) {
3348 load_bias
= load_addr
- loaddr
;
3350 if (elf_is_fdpic(ehdr
)) {
3351 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
=
3352 g_malloc(sizeof(*loadsegs
) * info
->nsegs
);
3354 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
3355 switch (phdr
[i
].p_type
) {
3357 info
->pt_dynamic_addr
= phdr
[i
].p_vaddr
+ load_bias
;
3360 loadsegs
->addr
= phdr
[i
].p_vaddr
+ load_bias
;
3361 loadsegs
->p_vaddr
= phdr
[i
].p_vaddr
;
3362 loadsegs
->p_memsz
= phdr
[i
].p_memsz
;
3369 info
->load_bias
= load_bias
;
3370 info
->code_offset
= load_bias
;
3371 info
->data_offset
= load_bias
;
3372 info
->load_addr
= load_addr
;
3373 info
->entry
= ehdr
->e_entry
+ load_bias
;
3374 info
->start_code
= -1;
3376 info
->start_data
= -1;
3378 /* Usual start for brk is after all sections of the main executable. */
3379 info
->brk
= TARGET_PAGE_ALIGN(hiaddr
+ load_bias
);
3380 info
->elf_flags
= ehdr
->e_flags
;
3382 prot_exec
= PROT_EXEC
;
3383 #ifdef TARGET_AARCH64
3385 * If the BTI feature is present, this indicates that the executable
3386 * pages of the startup binary should be mapped with PROT_BTI, so that
3387 * branch targets are enforced.
3389 * The startup binary is either the interpreter or the static executable.
3390 * The interpreter is responsible for all pages of a dynamic executable.
3392 * Elf notes are backward compatible to older cpus.
3393 * Do not enable BTI unless it is supported.
3395 if ((info
->note_flags
& GNU_PROPERTY_AARCH64_FEATURE_1_BTI
)
3396 && (pinterp_name
== NULL
|| *pinterp_name
== 0)
3397 && cpu_isar_feature(aa64_bti
, ARM_CPU(thread_cpu
))) {
3398 prot_exec
|= TARGET_PROT_BTI
;
3402 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
3403 struct elf_phdr
*eppnt
= phdr
+ i
;
3404 if (eppnt
->p_type
== PT_LOAD
) {
3405 abi_ulong vaddr
, vaddr_po
, vaddr_ps
, vaddr_ef
, vaddr_em
;
3408 if (eppnt
->p_flags
& PF_R
) {
3409 elf_prot
|= PROT_READ
;
3411 if (eppnt
->p_flags
& PF_W
) {
3412 elf_prot
|= PROT_WRITE
;
3414 if (eppnt
->p_flags
& PF_X
) {
3415 elf_prot
|= prot_exec
;
3418 vaddr
= load_bias
+ eppnt
->p_vaddr
;
3419 vaddr_po
= vaddr
& ~TARGET_PAGE_MASK
;
3420 vaddr_ps
= vaddr
& TARGET_PAGE_MASK
;
3422 vaddr_ef
= vaddr
+ eppnt
->p_filesz
;
3423 vaddr_em
= vaddr
+ eppnt
->p_memsz
;
3426 * Some segments may be completely empty, with a non-zero p_memsz
3427 * but no backing file segment.
3429 if (eppnt
->p_filesz
!= 0) {
3430 error
= target_mmap(vaddr_ps
, eppnt
->p_filesz
+ vaddr_po
,
3431 elf_prot
, MAP_PRIVATE
| MAP_FIXED
,
3432 image_fd
, eppnt
->p_offset
- vaddr_po
);
3438 /* If the load segment requests extra zeros (e.g. bss), map it. */
3439 if (vaddr_ef
< vaddr_em
&&
3440 !zero_bss(vaddr_ef
, vaddr_em
, elf_prot
, &err
)) {
3444 /* Find the full program boundaries. */
3445 if (elf_prot
& PROT_EXEC
) {
3446 if (vaddr
< info
->start_code
) {
3447 info
->start_code
= vaddr
;
3449 if (vaddr_ef
> info
->end_code
) {
3450 info
->end_code
= vaddr_ef
;
3453 if (elf_prot
& PROT_WRITE
) {
3454 if (vaddr
< info
->start_data
) {
3455 info
->start_data
= vaddr
;
3457 if (vaddr_ef
> info
->end_data
) {
3458 info
->end_data
= vaddr_ef
;
3462 } else if (eppnt
->p_type
== PT_MIPS_ABIFLAGS
) {
3463 Mips_elf_abiflags_v0 abiflags
;
3464 if (eppnt
->p_filesz
< sizeof(Mips_elf_abiflags_v0
)) {
3465 error_setg(&err
, "Invalid PT_MIPS_ABIFLAGS entry");
3468 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
3469 memcpy(&abiflags
, bprm_buf
+ eppnt
->p_offset
,
3470 sizeof(Mips_elf_abiflags_v0
));
3472 retval
= pread(image_fd
, &abiflags
, sizeof(Mips_elf_abiflags_v0
),
3474 if (retval
!= sizeof(Mips_elf_abiflags_v0
)) {
3478 bswap_mips_abiflags(&abiflags
);
3479 info
->fp_abi
= abiflags
.fp_abi
;
3484 if (info
->end_data
== 0) {
3485 info
->start_data
= info
->end_code
;
3486 info
->end_data
= info
->end_code
;
3489 if (qemu_log_enabled()) {
3490 load_symbols(ehdr
, image_fd
, load_bias
);
3493 debuginfo_report_elf(image_name
, image_fd
, load_bias
);
3502 error_setg(&err
, "Incomplete read of file header");
3504 error_setg_errno(&err
, errno
, "Error reading file header");
3508 error_setg_errno(&err
, errno
, "Error mapping file");
3511 error_reportf_err(err
, "%s: ", image_name
);
3515 static void load_elf_interp(const char *filename
, struct image_info
*info
,
3516 char bprm_buf
[BPRM_BUF_SIZE
])
3521 fd
= open(path(filename
), O_RDONLY
);
3523 error_setg_file_open(&err
, errno
, filename
);
3524 error_report_err(err
);
3528 retval
= read(fd
, bprm_buf
, BPRM_BUF_SIZE
);
3530 error_setg_errno(&err
, errno
, "Error reading file header");
3531 error_reportf_err(err
, "%s: ", filename
);
3535 if (retval
< BPRM_BUF_SIZE
) {
3536 memset(bprm_buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
3539 load_elf_image(filename
, fd
, info
, NULL
, bprm_buf
);
3542 static int symfind(const void *s0
, const void *s1
)
3544 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
3545 __typeof(sym
->st_value
) addr
= *(uint64_t *)s0
;
3548 if (addr
< sym
->st_value
) {
3550 } else if (addr
>= sym
->st_value
+ sym
->st_size
) {
3556 static const char *lookup_symbolxx(struct syminfo
*s
, uint64_t orig_addr
)
3558 #if ELF_CLASS == ELFCLASS32
3559 struct elf_sym
*syms
= s
->disas_symtab
.elf32
;
3561 struct elf_sym
*syms
= s
->disas_symtab
.elf64
;
3565 struct elf_sym
*sym
;
3567 sym
= bsearch(&orig_addr
, syms
, s
->disas_num_syms
, sizeof(*syms
), symfind
);
3569 return s
->disas_strtab
+ sym
->st_name
;
3575 /* FIXME: This should use elf_ops.h */
3576 static int symcmp(const void *s0
, const void *s1
)
3578 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
3579 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
3580 return (sym0
->st_value
< sym1
->st_value
)
3582 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
3585 /* Best attempt to load symbols from this ELF object. */
3586 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
)
3588 int i
, shnum
, nsyms
, sym_idx
= 0, str_idx
= 0;
3590 struct elf_shdr
*shdr
;
3591 char *strings
= NULL
;
3592 struct syminfo
*s
= NULL
;
3593 struct elf_sym
*new_syms
, *syms
= NULL
;
3595 shnum
= hdr
->e_shnum
;
3596 i
= shnum
* sizeof(struct elf_shdr
);
3597 shdr
= (struct elf_shdr
*)alloca(i
);
3598 if (pread(fd
, shdr
, i
, hdr
->e_shoff
) != i
) {
3602 bswap_shdr(shdr
, shnum
);
3603 for (i
= 0; i
< shnum
; ++i
) {
3604 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
3606 str_idx
= shdr
[i
].sh_link
;
3611 /* There will be no symbol table if the file was stripped. */
3615 /* Now know where the strtab and symtab are. Snarf them. */
3616 s
= g_try_new(struct syminfo
, 1);
3621 segsz
= shdr
[str_idx
].sh_size
;
3622 s
->disas_strtab
= strings
= g_try_malloc(segsz
);
3624 pread(fd
, strings
, segsz
, shdr
[str_idx
].sh_offset
) != segsz
) {
3628 segsz
= shdr
[sym_idx
].sh_size
;
3629 syms
= g_try_malloc(segsz
);
3630 if (!syms
|| pread(fd
, syms
, segsz
, shdr
[sym_idx
].sh_offset
) != segsz
) {
3634 if (segsz
/ sizeof(struct elf_sym
) > INT_MAX
) {
3635 /* Implausibly large symbol table: give up rather than ploughing
3636 * on with the number of symbols calculation overflowing
3640 nsyms
= segsz
/ sizeof(struct elf_sym
);
3641 for (i
= 0; i
< nsyms
; ) {
3642 bswap_sym(syms
+ i
);
3643 /* Throw away entries which we do not need. */
3644 if (syms
[i
].st_shndx
== SHN_UNDEF
3645 || syms
[i
].st_shndx
>= SHN_LORESERVE
3646 || ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
3648 syms
[i
] = syms
[nsyms
];
3651 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3652 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3653 syms
[i
].st_value
&= ~(target_ulong
)1;
3655 syms
[i
].st_value
+= load_bias
;
3660 /* No "useful" symbol. */
3665 /* Attempt to free the storage associated with the local symbols
3666 that we threw away. Whether or not this has any effect on the
3667 memory allocation depends on the malloc implementation and how
3668 many symbols we managed to discard. */
3669 new_syms
= g_try_renew(struct elf_sym
, syms
, nsyms
);
3670 if (new_syms
== NULL
) {
3675 qsort(syms
, nsyms
, sizeof(*syms
), symcmp
);
3677 s
->disas_num_syms
= nsyms
;
3678 #if ELF_CLASS == ELFCLASS32
3679 s
->disas_symtab
.elf32
= syms
;
3681 s
->disas_symtab
.elf64
= syms
;
3683 s
->lookup_symbol
= lookup_symbolxx
;
3695 uint32_t get_elf_eflags(int fd
)
3701 /* Read ELF header */
3702 offset
= lseek(fd
, 0, SEEK_SET
);
3703 if (offset
== (off_t
) -1) {
3706 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
3707 if (ret
< sizeof(ehdr
)) {
3710 offset
= lseek(fd
, offset
, SEEK_SET
);
3711 if (offset
== (off_t
) -1) {
3715 /* Check ELF signature */
3716 if (!elf_check_ident(&ehdr
)) {
3722 if (!elf_check_ehdr(&ehdr
)) {
3726 /* return architecture id */
3727 return ehdr
.e_flags
;
3730 int load_elf_binary(struct linux_binprm
*bprm
, struct image_info
*info
)
3732 struct image_info interp_info
;
3733 struct elfhdr elf_ex
;
3734 char *elf_interpreter
= NULL
;
3737 memset(&interp_info
, 0, sizeof(interp_info
));
3739 interp_info
.fp_abi
= MIPS_ABI_FP_UNKNOWN
;
3742 load_elf_image(bprm
->filename
, bprm
->fd
, info
,
3743 &elf_interpreter
, bprm
->buf
);
3745 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3746 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3747 when we load the interpreter. */
3748 elf_ex
= *(struct elfhdr
*)bprm
->buf
;
3750 /* Do this so that we can load the interpreter, if need be. We will
3751 change some of these later */
3752 bprm
->p
= setup_arg_pages(bprm
, info
);
3754 scratch
= g_new0(char, TARGET_PAGE_SIZE
);
3755 if (STACK_GROWS_DOWN
) {
3756 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
3757 bprm
->p
, info
->stack_limit
);
3758 info
->file_string
= bprm
->p
;
3759 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
3760 bprm
->p
, info
->stack_limit
);
3761 info
->env_strings
= bprm
->p
;
3762 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
3763 bprm
->p
, info
->stack_limit
);
3764 info
->arg_strings
= bprm
->p
;
3766 info
->arg_strings
= bprm
->p
;
3767 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
3768 bprm
->p
, info
->stack_limit
);
3769 info
->env_strings
= bprm
->p
;
3770 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
3771 bprm
->p
, info
->stack_limit
);
3772 info
->file_string
= bprm
->p
;
3773 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
3774 bprm
->p
, info
->stack_limit
);
3780 fprintf(stderr
, "%s: %s\n", bprm
->filename
, strerror(E2BIG
));
3784 if (elf_interpreter
) {
3785 load_elf_interp(elf_interpreter
, &interp_info
, bprm
->buf
);
3788 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky
3789 * with the mappings the interpreter can be loaded above but
3790 * near the main executable, which can leave very little room
3792 * If the current brk has less than 16MB, use the end of the
3795 if (interp_info
.brk
> info
->brk
&&
3796 interp_info
.load_bias
- info
->brk
< 16 * MiB
) {
3797 info
->brk
= interp_info
.brk
;
3800 /* If the program interpreter is one of these two, then assume
3801 an iBCS2 image. Otherwise assume a native linux image. */
3803 if (strcmp(elf_interpreter
, "/usr/lib/libc.so.1") == 0
3804 || strcmp(elf_interpreter
, "/usr/lib/ld.so.1") == 0) {
3805 info
->personality
= PER_SVR4
;
3807 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3808 and some applications "depend" upon this behavior. Since
3809 we do not have the power to recompile these, we emulate
3810 the SVr4 behavior. Sigh. */
3811 target_mmap(0, qemu_host_page_size
, PROT_READ
| PROT_EXEC
,
3812 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
3815 info
->interp_fp_abi
= interp_info
.fp_abi
;
3820 * TODO: load a vdso, which would also contain the signal trampolines.
3821 * Otherwise, allocate a private page to hold them.
3823 if (TARGET_ARCH_HAS_SIGTRAMP_PAGE
) {
3824 abi_long tramp_page
= target_mmap(0, TARGET_PAGE_SIZE
,
3825 PROT_READ
| PROT_WRITE
,
3826 MAP_PRIVATE
| MAP_ANON
, -1, 0);
3827 if (tramp_page
== -1) {
3831 setup_sigtramp(tramp_page
);
3832 target_mprotect(tramp_page
, TARGET_PAGE_SIZE
, PROT_READ
| PROT_EXEC
);
3835 bprm
->p
= create_elf_tables(bprm
->p
, bprm
->argc
, bprm
->envc
, &elf_ex
,
3836 info
, (elf_interpreter
? &interp_info
: NULL
));
3837 info
->start_stack
= bprm
->p
;
3839 /* If we have an interpreter, set that as the program's entry point.
3840 Copy the load_bias as well, to help PPC64 interpret the entry
3841 point as a function descriptor. Do this after creating elf tables
3842 so that we copy the original program entry point into the AUXV. */
3843 if (elf_interpreter
) {
3844 info
->load_bias
= interp_info
.load_bias
;
3845 info
->entry
= interp_info
.entry
;
3846 g_free(elf_interpreter
);
3849 #ifdef USE_ELF_CORE_DUMP
3850 bprm
->core_dump
= &elf_core_dump
;
3856 #ifdef USE_ELF_CORE_DUMP
3858 * Definitions to generate Intel SVR4-like core files.
3859 * These mostly have the same names as the SVR4 types with "target_elf_"
3860 * tacked on the front to prevent clashes with linux definitions,
3861 * and the typedef forms have been avoided. This is mostly like
3862 * the SVR4 structure, but more Linuxy, with things that Linux does
3863 * not support and which gdb doesn't really use excluded.
3865 * Fields we don't dump (their contents is zero) in linux-user qemu
3866 * are marked with XXX.
3868 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3870 * Porting ELF coredump for target is (quite) simple process. First you
3871 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3872 * the target resides):
3874 * #define USE_ELF_CORE_DUMP
3876 * Next you define type of register set used for dumping. ELF specification
3877 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3879 * typedef <target_regtype> target_elf_greg_t;
3880 * #define ELF_NREG <number of registers>
3881 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3883 * Last step is to implement target specific function that copies registers
3884 * from given cpu into just specified register set. Prototype is:
3886 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3887 * const CPUArchState *env);
3890 * regs - copy register values into here (allocated and zeroed by caller)
3891 * env - copy registers from here
3893 * Example for ARM target is provided in this file.
3896 /* An ELF note in memory */
3900 size_t namesz_rounded
;
3903 size_t datasz_rounded
;
3908 struct target_elf_siginfo
{
3909 abi_int si_signo
; /* signal number */
3910 abi_int si_code
; /* extra code */
3911 abi_int si_errno
; /* errno */
3914 struct target_elf_prstatus
{
3915 struct target_elf_siginfo pr_info
; /* Info associated with signal */
3916 abi_short pr_cursig
; /* Current signal */
3917 abi_ulong pr_sigpend
; /* XXX */
3918 abi_ulong pr_sighold
; /* XXX */
3919 target_pid_t pr_pid
;
3920 target_pid_t pr_ppid
;
3921 target_pid_t pr_pgrp
;
3922 target_pid_t pr_sid
;
3923 struct target_timeval pr_utime
; /* XXX User time */
3924 struct target_timeval pr_stime
; /* XXX System time */
3925 struct target_timeval pr_cutime
; /* XXX Cumulative user time */
3926 struct target_timeval pr_cstime
; /* XXX Cumulative system time */
3927 target_elf_gregset_t pr_reg
; /* GP registers */
3928 abi_int pr_fpvalid
; /* XXX */
3931 #define ELF_PRARGSZ (80) /* Number of chars for args */
3933 struct target_elf_prpsinfo
{
3934 char pr_state
; /* numeric process state */
3935 char pr_sname
; /* char for pr_state */
3936 char pr_zomb
; /* zombie */
3937 char pr_nice
; /* nice val */
3938 abi_ulong pr_flag
; /* flags */
3939 target_uid_t pr_uid
;
3940 target_gid_t pr_gid
;
3941 target_pid_t pr_pid
, pr_ppid
, pr_pgrp
, pr_sid
;
3943 char pr_fname
[16] QEMU_NONSTRING
; /* filename of executable */
3944 char pr_psargs
[ELF_PRARGSZ
]; /* initial part of arg list */
3947 /* Here is the structure in which status of each thread is captured. */
3948 struct elf_thread_status
{
3949 QTAILQ_ENTRY(elf_thread_status
) ets_link
;
3950 struct target_elf_prstatus prstatus
; /* NT_PRSTATUS */
3952 elf_fpregset_t fpu
; /* NT_PRFPREG */
3953 struct task_struct
*thread
;
3954 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
3956 struct memelfnote notes
[1];
3960 struct elf_note_info
{
3961 struct memelfnote
*notes
;
3962 struct target_elf_prstatus
*prstatus
; /* NT_PRSTATUS */
3963 struct target_elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
3965 QTAILQ_HEAD(, elf_thread_status
) thread_list
;
3968 * Current version of ELF coredump doesn't support
3969 * dumping fp regs etc.
3971 elf_fpregset_t
*fpu
;
3972 elf_fpxregset_t
*xfpu
;
3973 int thread_status_size
;
3979 struct vm_area_struct
{
3980 target_ulong vma_start
; /* start vaddr of memory region */
3981 target_ulong vma_end
; /* end vaddr of memory region */
3982 abi_ulong vma_flags
; /* protection etc. flags for the region */
3983 QTAILQ_ENTRY(vm_area_struct
) vma_link
;
3987 QTAILQ_HEAD(, vm_area_struct
) mm_mmap
;
3988 int mm_count
; /* number of mappings */
3991 static struct mm_struct
*vma_init(void);
3992 static void vma_delete(struct mm_struct
*);
3993 static int vma_add_mapping(struct mm_struct
*, target_ulong
,
3994 target_ulong
, abi_ulong
);
3995 static int vma_get_mapping_count(const struct mm_struct
*);
3996 static struct vm_area_struct
*vma_first(const struct mm_struct
*);
3997 static struct vm_area_struct
*vma_next(struct vm_area_struct
*);
3998 static abi_ulong
vma_dump_size(const struct vm_area_struct
*);
3999 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
4000 unsigned long flags
);
4002 static void fill_elf_header(struct elfhdr
*, int, uint16_t, uint32_t);
4003 static void fill_note(struct memelfnote
*, const char *, int,
4004 unsigned int, void *);
4005 static void fill_prstatus(struct target_elf_prstatus
*, const TaskState
*, int);
4006 static int fill_psinfo(struct target_elf_prpsinfo
*, const TaskState
*);
4007 static void fill_auxv_note(struct memelfnote
*, const TaskState
*);
4008 static void fill_elf_note_phdr(struct elf_phdr
*, int, off_t
);
4009 static size_t note_size(const struct memelfnote
*);
4010 static void free_note_info(struct elf_note_info
*);
4011 static int fill_note_info(struct elf_note_info
*, long, const CPUArchState
*);
4012 static void fill_thread_info(struct elf_note_info
*, const CPUArchState
*);
4014 static int dump_write(int, const void *, size_t);
4015 static int write_note(struct memelfnote
*, int);
4016 static int write_note_info(struct elf_note_info
*, int);
4019 static void bswap_prstatus(struct target_elf_prstatus
*prstatus
)
4021 prstatus
->pr_info
.si_signo
= tswap32(prstatus
->pr_info
.si_signo
);
4022 prstatus
->pr_info
.si_code
= tswap32(prstatus
->pr_info
.si_code
);
4023 prstatus
->pr_info
.si_errno
= tswap32(prstatus
->pr_info
.si_errno
);
4024 prstatus
->pr_cursig
= tswap16(prstatus
->pr_cursig
);
4025 prstatus
->pr_sigpend
= tswapal(prstatus
->pr_sigpend
);
4026 prstatus
->pr_sighold
= tswapal(prstatus
->pr_sighold
);
4027 prstatus
->pr_pid
= tswap32(prstatus
->pr_pid
);
4028 prstatus
->pr_ppid
= tswap32(prstatus
->pr_ppid
);
4029 prstatus
->pr_pgrp
= tswap32(prstatus
->pr_pgrp
);
4030 prstatus
->pr_sid
= tswap32(prstatus
->pr_sid
);
4031 /* cpu times are not filled, so we skip them */
4032 /* regs should be in correct format already */
4033 prstatus
->pr_fpvalid
= tswap32(prstatus
->pr_fpvalid
);
4036 static void bswap_psinfo(struct target_elf_prpsinfo
*psinfo
)
4038 psinfo
->pr_flag
= tswapal(psinfo
->pr_flag
);
4039 psinfo
->pr_uid
= tswap16(psinfo
->pr_uid
);
4040 psinfo
->pr_gid
= tswap16(psinfo
->pr_gid
);
4041 psinfo
->pr_pid
= tswap32(psinfo
->pr_pid
);
4042 psinfo
->pr_ppid
= tswap32(psinfo
->pr_ppid
);
4043 psinfo
->pr_pgrp
= tswap32(psinfo
->pr_pgrp
);
4044 psinfo
->pr_sid
= tswap32(psinfo
->pr_sid
);
4047 static void bswap_note(struct elf_note
*en
)
4049 bswap32s(&en
->n_namesz
);
4050 bswap32s(&en
->n_descsz
);
4051 bswap32s(&en
->n_type
);
4054 static inline void bswap_prstatus(struct target_elf_prstatus
*p
) { }
4055 static inline void bswap_psinfo(struct target_elf_prpsinfo
*p
) {}
4056 static inline void bswap_note(struct elf_note
*en
) { }
4057 #endif /* BSWAP_NEEDED */
4060 * Minimal support for linux memory regions. These are needed
4061 * when we are finding out what memory exactly belongs to
4062 * emulated process. No locks needed here, as long as
4063 * thread that received the signal is stopped.
4066 static struct mm_struct
*vma_init(void)
4068 struct mm_struct
*mm
;
4070 if ((mm
= g_malloc(sizeof (*mm
))) == NULL
)
4074 QTAILQ_INIT(&mm
->mm_mmap
);
4079 static void vma_delete(struct mm_struct
*mm
)
4081 struct vm_area_struct
*vma
;
4083 while ((vma
= vma_first(mm
)) != NULL
) {
4084 QTAILQ_REMOVE(&mm
->mm_mmap
, vma
, vma_link
);
4090 static int vma_add_mapping(struct mm_struct
*mm
, target_ulong start
,
4091 target_ulong end
, abi_ulong flags
)
4093 struct vm_area_struct
*vma
;
4095 if ((vma
= g_malloc0(sizeof (*vma
))) == NULL
)
4098 vma
->vma_start
= start
;
4100 vma
->vma_flags
= flags
;
4102 QTAILQ_INSERT_TAIL(&mm
->mm_mmap
, vma
, vma_link
);
4108 static struct vm_area_struct
*vma_first(const struct mm_struct
*mm
)
4110 return (QTAILQ_FIRST(&mm
->mm_mmap
));
4113 static struct vm_area_struct
*vma_next(struct vm_area_struct
*vma
)
4115 return (QTAILQ_NEXT(vma
, vma_link
));
4118 static int vma_get_mapping_count(const struct mm_struct
*mm
)
4120 return (mm
->mm_count
);
4124 * Calculate file (dump) size of given memory region.
4126 static abi_ulong
vma_dump_size(const struct vm_area_struct
*vma
)
4128 /* if we cannot even read the first page, skip it */
4129 if (!access_ok_untagged(VERIFY_READ
, vma
->vma_start
, TARGET_PAGE_SIZE
))
4133 * Usually we don't dump executable pages as they contain
4134 * non-writable code that debugger can read directly from
4135 * target library etc. However, thread stacks are marked
4136 * also executable so we read in first page of given region
4137 * and check whether it contains elf header. If there is
4138 * no elf header, we dump it.
4140 if (vma
->vma_flags
& PROT_EXEC
) {
4141 char page
[TARGET_PAGE_SIZE
];
4143 if (copy_from_user(page
, vma
->vma_start
, sizeof (page
))) {
4146 if ((page
[EI_MAG0
] == ELFMAG0
) &&
4147 (page
[EI_MAG1
] == ELFMAG1
) &&
4148 (page
[EI_MAG2
] == ELFMAG2
) &&
4149 (page
[EI_MAG3
] == ELFMAG3
)) {
4151 * Mappings are possibly from ELF binary. Don't dump
4158 return (vma
->vma_end
- vma
->vma_start
);
4161 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
4162 unsigned long flags
)
4164 struct mm_struct
*mm
= (struct mm_struct
*)priv
;
4166 vma_add_mapping(mm
, start
, end
, flags
);
4170 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
4171 unsigned int sz
, void *data
)
4173 unsigned int namesz
;
4175 namesz
= strlen(name
) + 1;
4177 note
->namesz
= namesz
;
4178 note
->namesz_rounded
= roundup(namesz
, sizeof (int32_t));
4181 note
->datasz_rounded
= roundup(sz
, sizeof (int32_t));
4186 * We calculate rounded up note size here as specified by
4189 note
->notesz
= sizeof (struct elf_note
) +
4190 note
->namesz_rounded
+ note
->datasz_rounded
;
4193 static void fill_elf_header(struct elfhdr
*elf
, int segs
, uint16_t machine
,
4196 (void) memset(elf
, 0, sizeof(*elf
));
4198 (void) memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
4199 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
4200 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
4201 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
4202 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
4204 elf
->e_type
= ET_CORE
;
4205 elf
->e_machine
= machine
;
4206 elf
->e_version
= EV_CURRENT
;
4207 elf
->e_phoff
= sizeof(struct elfhdr
);
4208 elf
->e_flags
= flags
;
4209 elf
->e_ehsize
= sizeof(struct elfhdr
);
4210 elf
->e_phentsize
= sizeof(struct elf_phdr
);
4211 elf
->e_phnum
= segs
;
4216 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
4218 phdr
->p_type
= PT_NOTE
;
4219 phdr
->p_offset
= offset
;
4222 phdr
->p_filesz
= sz
;
4227 bswap_phdr(phdr
, 1);
4230 static size_t note_size(const struct memelfnote
*note
)
4232 return (note
->notesz
);
4235 static void fill_prstatus(struct target_elf_prstatus
*prstatus
,
4236 const TaskState
*ts
, int signr
)
4238 (void) memset(prstatus
, 0, sizeof (*prstatus
));
4239 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
4240 prstatus
->pr_pid
= ts
->ts_tid
;
4241 prstatus
->pr_ppid
= getppid();
4242 prstatus
->pr_pgrp
= getpgrp();
4243 prstatus
->pr_sid
= getsid(0);
4245 bswap_prstatus(prstatus
);
4248 static int fill_psinfo(struct target_elf_prpsinfo
*psinfo
, const TaskState
*ts
)
4250 char *base_filename
;
4251 unsigned int i
, len
;
4253 (void) memset(psinfo
, 0, sizeof (*psinfo
));
4255 len
= ts
->info
->env_strings
- ts
->info
->arg_strings
;
4256 if (len
>= ELF_PRARGSZ
)
4257 len
= ELF_PRARGSZ
- 1;
4258 if (copy_from_user(&psinfo
->pr_psargs
, ts
->info
->arg_strings
, len
)) {
4261 for (i
= 0; i
< len
; i
++)
4262 if (psinfo
->pr_psargs
[i
] == 0)
4263 psinfo
->pr_psargs
[i
] = ' ';
4264 psinfo
->pr_psargs
[len
] = 0;
4266 psinfo
->pr_pid
= getpid();
4267 psinfo
->pr_ppid
= getppid();
4268 psinfo
->pr_pgrp
= getpgrp();
4269 psinfo
->pr_sid
= getsid(0);
4270 psinfo
->pr_uid
= getuid();
4271 psinfo
->pr_gid
= getgid();
4273 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
4275 * Using strncpy here is fine: at max-length,
4276 * this field is not NUL-terminated.
4278 (void) strncpy(psinfo
->pr_fname
, base_filename
,
4279 sizeof(psinfo
->pr_fname
));
4281 g_free(base_filename
);
4282 bswap_psinfo(psinfo
);
4286 static void fill_auxv_note(struct memelfnote
*note
, const TaskState
*ts
)
4288 elf_addr_t auxv
= (elf_addr_t
)ts
->info
->saved_auxv
;
4289 elf_addr_t orig_auxv
= auxv
;
4291 int len
= ts
->info
->auxv_len
;
4294 * Auxiliary vector is stored in target process stack. It contains
4295 * {type, value} pairs that we need to dump into note. This is not
4296 * strictly necessary but we do it here for sake of completeness.
4299 /* read in whole auxv vector and copy it to memelfnote */
4300 ptr
= lock_user(VERIFY_READ
, orig_auxv
, len
, 0);
4302 fill_note(note
, "CORE", NT_AUXV
, len
, ptr
);
4303 unlock_user(ptr
, auxv
, len
);
4308 * Constructs name of coredump file. We have following convention
4310 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4312 * Returns the filename
4314 static char *core_dump_filename(const TaskState
*ts
)
4316 g_autoptr(GDateTime
) now
= g_date_time_new_now_local();
4317 g_autofree
char *nowstr
= g_date_time_format(now
, "%Y%m%d-%H%M%S");
4318 g_autofree
char *base_filename
= g_path_get_basename(ts
->bprm
->filename
);
4320 return g_strdup_printf("qemu_%s_%s_%d.core",
4321 base_filename
, nowstr
, (int)getpid());
4324 static int dump_write(int fd
, const void *ptr
, size_t size
)
4326 const char *bufp
= (const char *)ptr
;
4327 ssize_t bytes_written
, bytes_left
;
4328 struct rlimit dumpsize
;
4332 getrlimit(RLIMIT_CORE
, &dumpsize
);
4333 if ((pos
= lseek(fd
, 0, SEEK_CUR
))==-1) {
4334 if (errno
== ESPIPE
) { /* not a seekable stream */
4340 if (dumpsize
.rlim_cur
<= pos
) {
4342 } else if (dumpsize
.rlim_cur
== RLIM_INFINITY
) {
4345 size_t limit_left
=dumpsize
.rlim_cur
- pos
;
4346 bytes_left
= limit_left
>= size
? size
: limit_left
;
4351 * In normal conditions, single write(2) should do but
4352 * in case of socket etc. this mechanism is more portable.
4355 bytes_written
= write(fd
, bufp
, bytes_left
);
4356 if (bytes_written
< 0) {
4360 } else if (bytes_written
== 0) { /* eof */
4363 bufp
+= bytes_written
;
4364 bytes_left
-= bytes_written
;
4365 } while (bytes_left
> 0);
4370 static int write_note(struct memelfnote
*men
, int fd
)
4374 en
.n_namesz
= men
->namesz
;
4375 en
.n_type
= men
->type
;
4376 en
.n_descsz
= men
->datasz
;
4380 if (dump_write(fd
, &en
, sizeof(en
)) != 0)
4382 if (dump_write(fd
, men
->name
, men
->namesz_rounded
) != 0)
4384 if (dump_write(fd
, men
->data
, men
->datasz_rounded
) != 0)
4390 static void fill_thread_info(struct elf_note_info
*info
, const CPUArchState
*env
)
4392 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4393 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
4394 struct elf_thread_status
*ets
;
4396 ets
= g_malloc0(sizeof (*ets
));
4397 ets
->num_notes
= 1; /* only prstatus is dumped */
4398 fill_prstatus(&ets
->prstatus
, ts
, 0);
4399 elf_core_copy_regs(&ets
->prstatus
.pr_reg
, env
);
4400 fill_note(&ets
->notes
[0], "CORE", NT_PRSTATUS
, sizeof (ets
->prstatus
),
4403 QTAILQ_INSERT_TAIL(&info
->thread_list
, ets
, ets_link
);
4405 info
->notes_size
+= note_size(&ets
->notes
[0]);
4408 static void init_note_info(struct elf_note_info
*info
)
4410 /* Initialize the elf_note_info structure so that it is at
4411 * least safe to call free_note_info() on it. Must be
4412 * called before calling fill_note_info().
4414 memset(info
, 0, sizeof (*info
));
4415 QTAILQ_INIT(&info
->thread_list
);
4418 static int fill_note_info(struct elf_note_info
*info
,
4419 long signr
, const CPUArchState
*env
)
4422 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4423 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
4426 info
->notes
= g_new0(struct memelfnote
, NUMNOTES
);
4427 if (info
->notes
== NULL
)
4429 info
->prstatus
= g_malloc0(sizeof (*info
->prstatus
));
4430 if (info
->prstatus
== NULL
)
4432 info
->psinfo
= g_malloc0(sizeof (*info
->psinfo
));
4433 if (info
->prstatus
== NULL
)
4437 * First fill in status (and registers) of current thread
4438 * including process info & aux vector.
4440 fill_prstatus(info
->prstatus
, ts
, signr
);
4441 elf_core_copy_regs(&info
->prstatus
->pr_reg
, env
);
4442 fill_note(&info
->notes
[0], "CORE", NT_PRSTATUS
,
4443 sizeof (*info
->prstatus
), info
->prstatus
);
4444 fill_psinfo(info
->psinfo
, ts
);
4445 fill_note(&info
->notes
[1], "CORE", NT_PRPSINFO
,
4446 sizeof (*info
->psinfo
), info
->psinfo
);
4447 fill_auxv_note(&info
->notes
[2], ts
);
4450 info
->notes_size
= 0;
4451 for (i
= 0; i
< info
->numnote
; i
++)
4452 info
->notes_size
+= note_size(&info
->notes
[i
]);
4454 /* read and fill status of all threads */
4455 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock
) {
4457 if (cpu
== thread_cpu
) {
4460 fill_thread_info(info
, cpu_env(cpu
));
4467 static void free_note_info(struct elf_note_info
*info
)
4469 struct elf_thread_status
*ets
;
4471 while (!QTAILQ_EMPTY(&info
->thread_list
)) {
4472 ets
= QTAILQ_FIRST(&info
->thread_list
);
4473 QTAILQ_REMOVE(&info
->thread_list
, ets
, ets_link
);
4477 g_free(info
->prstatus
);
4478 g_free(info
->psinfo
);
4479 g_free(info
->notes
);
4482 static int write_note_info(struct elf_note_info
*info
, int fd
)
4484 struct elf_thread_status
*ets
;
4487 /* write prstatus, psinfo and auxv for current thread */
4488 for (i
= 0; i
< info
->numnote
; i
++)
4489 if ((error
= write_note(&info
->notes
[i
], fd
)) != 0)
4492 /* write prstatus for each thread */
4493 QTAILQ_FOREACH(ets
, &info
->thread_list
, ets_link
) {
4494 if ((error
= write_note(&ets
->notes
[0], fd
)) != 0)
4502 * Write out ELF coredump.
4504 * See documentation of ELF object file format in:
4505 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4507 * Coredump format in linux is following:
4509 * 0 +----------------------+ \
4510 * | ELF header | ET_CORE |
4511 * +----------------------+ |
4512 * | ELF program headers | |--- headers
4513 * | - NOTE section | |
4514 * | - PT_LOAD sections | |
4515 * +----------------------+ /
4520 * +----------------------+ <-- aligned to target page
4521 * | Process memory dump |
4526 * +----------------------+
4528 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4529 * NT_PRSINFO -> struct elf_prpsinfo
4530 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4532 * Format follows System V format as close as possible. Current
4533 * version limitations are as follows:
4534 * - no floating point registers are dumped
4536 * Function returns 0 in case of success, negative errno otherwise.
4538 * TODO: make this work also during runtime: it should be
4539 * possible to force coredump from running process and then
4540 * continue processing. For example qemu could set up SIGUSR2
4541 * handler (provided that target process haven't registered
4542 * handler for that) that does the dump when signal is received.
4544 static int elf_core_dump(int signr
, const CPUArchState
*env
)
4546 const CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4547 const TaskState
*ts
= (const TaskState
*)cpu
->opaque
;
4548 struct vm_area_struct
*vma
= NULL
;
4549 g_autofree
char *corefile
= NULL
;
4550 struct elf_note_info info
;
4552 struct elf_phdr phdr
;
4553 struct rlimit dumpsize
;
4554 struct mm_struct
*mm
= NULL
;
4555 off_t offset
= 0, data_offset
= 0;
4559 init_note_info(&info
);
4562 getrlimit(RLIMIT_CORE
, &dumpsize
);
4563 if (dumpsize
.rlim_cur
== 0)
4566 corefile
= core_dump_filename(ts
);
4568 if ((fd
= open(corefile
, O_WRONLY
| O_CREAT
,
4569 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) < 0)
4573 * Walk through target process memory mappings and
4574 * set up structure containing this information. After
4575 * this point vma_xxx functions can be used.
4577 if ((mm
= vma_init()) == NULL
)
4580 walk_memory_regions(mm
, vma_walker
);
4581 segs
= vma_get_mapping_count(mm
);
4584 * Construct valid coredump ELF header. We also
4585 * add one more segment for notes.
4587 fill_elf_header(&elf
, segs
+ 1, ELF_MACHINE
, 0);
4588 if (dump_write(fd
, &elf
, sizeof (elf
)) != 0)
4591 /* fill in the in-memory version of notes */
4592 if (fill_note_info(&info
, signr
, env
) < 0)
4595 offset
+= sizeof (elf
); /* elf header */
4596 offset
+= (segs
+ 1) * sizeof (struct elf_phdr
); /* program headers */
4598 /* write out notes program header */
4599 fill_elf_note_phdr(&phdr
, info
.notes_size
, offset
);
4601 offset
+= info
.notes_size
;
4602 if (dump_write(fd
, &phdr
, sizeof (phdr
)) != 0)
4606 * ELF specification wants data to start at page boundary so
4609 data_offset
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
4612 * Write program headers for memory regions mapped in
4613 * the target process.
4615 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
4616 (void) memset(&phdr
, 0, sizeof (phdr
));
4618 phdr
.p_type
= PT_LOAD
;
4619 phdr
.p_offset
= offset
;
4620 phdr
.p_vaddr
= vma
->vma_start
;
4622 phdr
.p_filesz
= vma_dump_size(vma
);
4623 offset
+= phdr
.p_filesz
;
4624 phdr
.p_memsz
= vma
->vma_end
- vma
->vma_start
;
4625 phdr
.p_flags
= vma
->vma_flags
& PROT_READ
? PF_R
: 0;
4626 if (vma
->vma_flags
& PROT_WRITE
)
4627 phdr
.p_flags
|= PF_W
;
4628 if (vma
->vma_flags
& PROT_EXEC
)
4629 phdr
.p_flags
|= PF_X
;
4630 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
4632 bswap_phdr(&phdr
, 1);
4633 if (dump_write(fd
, &phdr
, sizeof(phdr
)) != 0) {
4639 * Next we write notes just after program headers. No
4640 * alignment needed here.
4642 if (write_note_info(&info
, fd
) < 0)
4645 /* align data to page boundary */
4646 if (lseek(fd
, data_offset
, SEEK_SET
) != data_offset
)
4650 * Finally we can dump process memory into corefile as well.
4652 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
4656 end
= vma
->vma_start
+ vma_dump_size(vma
);
4658 for (addr
= vma
->vma_start
; addr
< end
;
4659 addr
+= TARGET_PAGE_SIZE
) {
4660 char page
[TARGET_PAGE_SIZE
];
4664 * Read in page from target process memory and
4665 * write it to coredump file.
4667 error
= copy_from_user(page
, addr
, sizeof (page
));
4669 (void) fprintf(stderr
, "unable to dump " TARGET_ABI_FMT_lx
"\n",
4674 if (dump_write(fd
, page
, TARGET_PAGE_SIZE
) < 0)
4680 free_note_info(&info
);
4689 #endif /* USE_ELF_CORE_DUMP */
4691 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
4693 init_thread(regs
, infop
);