meson: move accelerator dependency checks together
[qemu/ar7.git] / linux-user / elfload.c
blobcf9e74468b11c7dd35dc6e6c3c57ef9d6336d01d
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
3 #include <sys/param.h>
5 #include <sys/resource.h>
6 #include <sys/shm.h>
8 #include "qemu.h"
9 #include "user-internals.h"
10 #include "signal-common.h"
11 #include "loader.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"
26 #ifdef TARGET_ARM
27 #include "target/arm/cpu-features.h"
28 #endif
30 #ifdef _ARCH_PPC64
31 #undef ARCH_DLINFO
32 #undef ELF_PLATFORM
33 #undef ELF_HWCAP
34 #undef ELF_HWCAP2
35 #undef ELF_CLASS
36 #undef ELF_DATA
37 #undef ELF_ARCH
38 #endif
40 #ifndef TARGET_ARCH_HAS_SIGTRAMP_PAGE
41 #define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0
42 #endif
44 typedef struct {
45 const uint8_t *image;
46 const uint32_t *relocs;
47 unsigned image_size;
48 unsigned reloc_count;
49 unsigned sigreturn_ofs;
50 unsigned rt_sigreturn_ofs;
51 } VdsoImageInfo;
53 #define ELF_OSABI ELFOSABI_SYSV
55 /* from personality.h */
58 * Flags for bug emulation.
60 * These occupy the top three bytes.
62 enum {
63 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
64 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
65 descriptors (signal handling) */
66 MMAP_PAGE_ZERO = 0x0100000,
67 ADDR_COMPAT_LAYOUT = 0x0200000,
68 READ_IMPLIES_EXEC = 0x0400000,
69 ADDR_LIMIT_32BIT = 0x0800000,
70 SHORT_INODE = 0x1000000,
71 WHOLE_SECONDS = 0x2000000,
72 STICKY_TIMEOUTS = 0x4000000,
73 ADDR_LIMIT_3GB = 0x8000000,
77 * Personality types.
79 * These go in the low byte. Avoid using the top bit, it will
80 * conflict with error returns.
82 enum {
83 PER_LINUX = 0x0000,
84 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
85 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
86 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
87 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
88 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
89 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
90 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
91 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
92 PER_BSD = 0x0006,
93 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
94 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
95 PER_LINUX32 = 0x0008,
96 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
97 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
98 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
99 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
100 PER_RISCOS = 0x000c,
101 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
102 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
103 PER_OSF4 = 0x000f, /* OSF/1 v4 */
104 PER_HPUX = 0x0010,
105 PER_MASK = 0x00ff,
109 * Return the base personality without flags.
111 #define personality(pers) (pers & PER_MASK)
113 int info_is_fdpic(struct image_info *info)
115 return info->personality == PER_LINUX_FDPIC;
118 /* this flag is uneffective under linux too, should be deleted */
119 #ifndef MAP_DENYWRITE
120 #define MAP_DENYWRITE 0
121 #endif
123 /* should probably go in elf.h */
124 #ifndef ELIBBAD
125 #define ELIBBAD 80
126 #endif
128 #if TARGET_BIG_ENDIAN
129 #define ELF_DATA ELFDATA2MSB
130 #else
131 #define ELF_DATA ELFDATA2LSB
132 #endif
134 #ifdef TARGET_ABI_MIPSN32
135 typedef abi_ullong target_elf_greg_t;
136 #define tswapreg(ptr) tswap64(ptr)
137 #else
138 typedef abi_ulong target_elf_greg_t;
139 #define tswapreg(ptr) tswapal(ptr)
140 #endif
142 #ifdef USE_UID16
143 typedef abi_ushort target_uid_t;
144 typedef abi_ushort target_gid_t;
145 #else
146 typedef abi_uint target_uid_t;
147 typedef abi_uint target_gid_t;
148 #endif
149 typedef abi_int target_pid_t;
151 #ifdef TARGET_I386
153 #define ELF_HWCAP get_elf_hwcap()
155 static uint32_t get_elf_hwcap(void)
157 X86CPU *cpu = X86_CPU(thread_cpu);
159 return cpu->env.features[FEAT_1_EDX];
162 #ifdef TARGET_X86_64
163 #define ELF_CLASS ELFCLASS64
164 #define ELF_ARCH EM_X86_64
166 #define ELF_PLATFORM "x86_64"
168 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
170 regs->rax = 0;
171 regs->rsp = infop->start_stack;
172 regs->rip = infop->entry;
175 #define ELF_NREG 27
176 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
179 * Note that ELF_NREG should be 29 as there should be place for
180 * TRAPNO and ERR "registers" as well but linux doesn't dump
181 * those.
183 * See linux kernel: arch/x86/include/asm/elf.h
185 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
187 (*regs)[0] = tswapreg(env->regs[15]);
188 (*regs)[1] = tswapreg(env->regs[14]);
189 (*regs)[2] = tswapreg(env->regs[13]);
190 (*regs)[3] = tswapreg(env->regs[12]);
191 (*regs)[4] = tswapreg(env->regs[R_EBP]);
192 (*regs)[5] = tswapreg(env->regs[R_EBX]);
193 (*regs)[6] = tswapreg(env->regs[11]);
194 (*regs)[7] = tswapreg(env->regs[10]);
195 (*regs)[8] = tswapreg(env->regs[9]);
196 (*regs)[9] = tswapreg(env->regs[8]);
197 (*regs)[10] = tswapreg(env->regs[R_EAX]);
198 (*regs)[11] = tswapreg(env->regs[R_ECX]);
199 (*regs)[12] = tswapreg(env->regs[R_EDX]);
200 (*regs)[13] = tswapreg(env->regs[R_ESI]);
201 (*regs)[14] = tswapreg(env->regs[R_EDI]);
202 (*regs)[15] = tswapreg(env->regs[R_EAX]); /* XXX */
203 (*regs)[16] = tswapreg(env->eip);
204 (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff);
205 (*regs)[18] = tswapreg(env->eflags);
206 (*regs)[19] = tswapreg(env->regs[R_ESP]);
207 (*regs)[20] = tswapreg(env->segs[R_SS].selector & 0xffff);
208 (*regs)[21] = tswapreg(env->segs[R_FS].selector & 0xffff);
209 (*regs)[22] = tswapreg(env->segs[R_GS].selector & 0xffff);
210 (*regs)[23] = tswapreg(env->segs[R_DS].selector & 0xffff);
211 (*regs)[24] = tswapreg(env->segs[R_ES].selector & 0xffff);
212 (*regs)[25] = tswapreg(env->segs[R_FS].selector & 0xffff);
213 (*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff);
216 #if ULONG_MAX > UINT32_MAX
217 #define INIT_GUEST_COMMPAGE
218 static bool init_guest_commpage(void)
221 * The vsyscall page is at a high negative address aka kernel space,
222 * which means that we cannot actually allocate it with target_mmap.
223 * We still should be able to use page_set_flags, unless the user
224 * has specified -R reserved_va, which would trigger an assert().
226 if (reserved_va != 0 &&
227 TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE - 1 > reserved_va) {
228 error_report("Cannot allocate vsyscall page");
229 exit(EXIT_FAILURE);
231 page_set_flags(TARGET_VSYSCALL_PAGE,
232 TARGET_VSYSCALL_PAGE | ~TARGET_PAGE_MASK,
233 PAGE_EXEC | PAGE_VALID);
234 return true;
236 #endif
237 #else
240 * This is used to ensure we don't load something for the wrong architecture.
242 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
245 * These are used to set parameters in the core dumps.
247 #define ELF_CLASS ELFCLASS32
248 #define ELF_ARCH EM_386
250 #define ELF_PLATFORM get_elf_platform()
251 #define EXSTACK_DEFAULT true
253 static const char *get_elf_platform(void)
255 static char elf_platform[] = "i386";
256 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
257 if (family > 6) {
258 family = 6;
260 if (family >= 3) {
261 elf_platform[1] = '0' + family;
263 return elf_platform;
266 static inline void init_thread(struct target_pt_regs *regs,
267 struct image_info *infop)
269 regs->esp = infop->start_stack;
270 regs->eip = infop->entry;
272 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
273 starts %edx contains a pointer to a function which might be
274 registered using `atexit'. This provides a mean for the
275 dynamic linker to call DT_FINI functions for shared libraries
276 that have been loaded before the code runs.
278 A value of 0 tells we have no such handler. */
279 regs->edx = 0;
282 #define ELF_NREG 17
283 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
286 * Note that ELF_NREG should be 19 as there should be place for
287 * TRAPNO and ERR "registers" as well but linux doesn't dump
288 * those.
290 * See linux kernel: arch/x86/include/asm/elf.h
292 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
294 (*regs)[0] = tswapreg(env->regs[R_EBX]);
295 (*regs)[1] = tswapreg(env->regs[R_ECX]);
296 (*regs)[2] = tswapreg(env->regs[R_EDX]);
297 (*regs)[3] = tswapreg(env->regs[R_ESI]);
298 (*regs)[4] = tswapreg(env->regs[R_EDI]);
299 (*regs)[5] = tswapreg(env->regs[R_EBP]);
300 (*regs)[6] = tswapreg(env->regs[R_EAX]);
301 (*regs)[7] = tswapreg(env->segs[R_DS].selector & 0xffff);
302 (*regs)[8] = tswapreg(env->segs[R_ES].selector & 0xffff);
303 (*regs)[9] = tswapreg(env->segs[R_FS].selector & 0xffff);
304 (*regs)[10] = tswapreg(env->segs[R_GS].selector & 0xffff);
305 (*regs)[11] = tswapreg(env->regs[R_EAX]); /* XXX */
306 (*regs)[12] = tswapreg(env->eip);
307 (*regs)[13] = tswapreg(env->segs[R_CS].selector & 0xffff);
308 (*regs)[14] = tswapreg(env->eflags);
309 (*regs)[15] = tswapreg(env->regs[R_ESP]);
310 (*regs)[16] = tswapreg(env->segs[R_SS].selector & 0xffff);
314 * i386 is the only target which supplies AT_SYSINFO for the vdso.
315 * All others only supply AT_SYSINFO_EHDR.
317 #define DLINFO_ARCH_ITEMS (vdso_info != NULL)
318 #define ARCH_DLINFO \
319 do { \
320 if (vdso_info) { \
321 NEW_AUX_ENT(AT_SYSINFO, vdso_info->entry); \
323 } while (0)
325 #endif /* TARGET_X86_64 */
327 #define VDSO_HEADER "vdso.c.inc"
329 #define USE_ELF_CORE_DUMP
330 #define ELF_EXEC_PAGESIZE 4096
332 #endif /* TARGET_I386 */
334 #ifdef TARGET_ARM
336 #ifndef TARGET_AARCH64
337 /* 32 bit ARM definitions */
339 #define ELF_ARCH EM_ARM
340 #define ELF_CLASS ELFCLASS32
341 #define EXSTACK_DEFAULT true
343 static inline void init_thread(struct target_pt_regs *regs,
344 struct image_info *infop)
346 abi_long stack = infop->start_stack;
347 memset(regs, 0, sizeof(*regs));
349 regs->uregs[16] = ARM_CPU_MODE_USR;
350 if (infop->entry & 1) {
351 regs->uregs[16] |= CPSR_T;
353 regs->uregs[15] = infop->entry & 0xfffffffe;
354 regs->uregs[13] = infop->start_stack;
355 /* FIXME - what to for failure of get_user()? */
356 get_user_ual(regs->uregs[2], stack + 8); /* envp */
357 get_user_ual(regs->uregs[1], stack + 4); /* envp */
358 /* XXX: it seems that r0 is zeroed after ! */
359 regs->uregs[0] = 0;
360 /* For uClinux PIC binaries. */
361 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
362 regs->uregs[10] = infop->start_data;
364 /* Support ARM FDPIC. */
365 if (info_is_fdpic(infop)) {
366 /* As described in the ABI document, r7 points to the loadmap info
367 * prepared by the kernel. If an interpreter is needed, r8 points
368 * to the interpreter loadmap and r9 points to the interpreter
369 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
370 * r9 points to the main program PT_DYNAMIC info.
372 regs->uregs[7] = infop->loadmap_addr;
373 if (infop->interpreter_loadmap_addr) {
374 /* Executable is dynamically loaded. */
375 regs->uregs[8] = infop->interpreter_loadmap_addr;
376 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
377 } else {
378 regs->uregs[8] = 0;
379 regs->uregs[9] = infop->pt_dynamic_addr;
384 #define ELF_NREG 18
385 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
387 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
389 (*regs)[0] = tswapreg(env->regs[0]);
390 (*regs)[1] = tswapreg(env->regs[1]);
391 (*regs)[2] = tswapreg(env->regs[2]);
392 (*regs)[3] = tswapreg(env->regs[3]);
393 (*regs)[4] = tswapreg(env->regs[4]);
394 (*regs)[5] = tswapreg(env->regs[5]);
395 (*regs)[6] = tswapreg(env->regs[6]);
396 (*regs)[7] = tswapreg(env->regs[7]);
397 (*regs)[8] = tswapreg(env->regs[8]);
398 (*regs)[9] = tswapreg(env->regs[9]);
399 (*regs)[10] = tswapreg(env->regs[10]);
400 (*regs)[11] = tswapreg(env->regs[11]);
401 (*regs)[12] = tswapreg(env->regs[12]);
402 (*regs)[13] = tswapreg(env->regs[13]);
403 (*regs)[14] = tswapreg(env->regs[14]);
404 (*regs)[15] = tswapreg(env->regs[15]);
406 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
407 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
410 #define USE_ELF_CORE_DUMP
411 #define ELF_EXEC_PAGESIZE 4096
413 enum
415 ARM_HWCAP_ARM_SWP = 1 << 0,
416 ARM_HWCAP_ARM_HALF = 1 << 1,
417 ARM_HWCAP_ARM_THUMB = 1 << 2,
418 ARM_HWCAP_ARM_26BIT = 1 << 3,
419 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
420 ARM_HWCAP_ARM_FPA = 1 << 5,
421 ARM_HWCAP_ARM_VFP = 1 << 6,
422 ARM_HWCAP_ARM_EDSP = 1 << 7,
423 ARM_HWCAP_ARM_JAVA = 1 << 8,
424 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
425 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
426 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
427 ARM_HWCAP_ARM_NEON = 1 << 12,
428 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
429 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
430 ARM_HWCAP_ARM_TLS = 1 << 15,
431 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
432 ARM_HWCAP_ARM_IDIVA = 1 << 17,
433 ARM_HWCAP_ARM_IDIVT = 1 << 18,
434 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
435 ARM_HWCAP_ARM_LPAE = 1 << 20,
436 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
437 ARM_HWCAP_ARM_FPHP = 1 << 22,
438 ARM_HWCAP_ARM_ASIMDHP = 1 << 23,
439 ARM_HWCAP_ARM_ASIMDDP = 1 << 24,
440 ARM_HWCAP_ARM_ASIMDFHM = 1 << 25,
441 ARM_HWCAP_ARM_ASIMDBF16 = 1 << 26,
442 ARM_HWCAP_ARM_I8MM = 1 << 27,
445 enum {
446 ARM_HWCAP2_ARM_AES = 1 << 0,
447 ARM_HWCAP2_ARM_PMULL = 1 << 1,
448 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
449 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
450 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
451 ARM_HWCAP2_ARM_SB = 1 << 5,
452 ARM_HWCAP2_ARM_SSBS = 1 << 6,
455 /* The commpage only exists for 32 bit kernels */
457 #define HI_COMMPAGE (intptr_t)0xffff0f00u
459 static bool init_guest_commpage(void)
461 ARMCPU *cpu = ARM_CPU(thread_cpu);
462 abi_ptr commpage;
463 void *want;
464 void *addr;
467 * M-profile allocates maximum of 2GB address space, so can never
468 * allocate the commpage. Skip it.
470 if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
471 return true;
474 commpage = HI_COMMPAGE & -qemu_host_page_size;
475 want = g2h_untagged(commpage);
476 addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
477 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
479 if (addr == MAP_FAILED) {
480 perror("Allocating guest commpage");
481 exit(EXIT_FAILURE);
483 if (addr != want) {
484 return false;
487 /* Set kernel helper versions; rest of page is 0. */
488 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
490 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
491 perror("Protecting guest commpage");
492 exit(EXIT_FAILURE);
495 page_set_flags(commpage, commpage | ~qemu_host_page_mask,
496 PAGE_READ | PAGE_EXEC | PAGE_VALID);
497 return true;
500 #define ELF_HWCAP get_elf_hwcap()
501 #define ELF_HWCAP2 get_elf_hwcap2()
503 uint32_t get_elf_hwcap(void)
505 ARMCPU *cpu = ARM_CPU(thread_cpu);
506 uint32_t hwcaps = 0;
508 hwcaps |= ARM_HWCAP_ARM_SWP;
509 hwcaps |= ARM_HWCAP_ARM_HALF;
510 hwcaps |= ARM_HWCAP_ARM_THUMB;
511 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
513 /* probe for the extra features */
514 #define GET_FEATURE(feat, hwcap) \
515 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
517 #define GET_FEATURE_ID(feat, hwcap) \
518 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
520 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
521 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
522 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
523 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
524 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
525 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
526 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
527 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
528 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
529 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
531 if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
532 cpu_isar_feature(aa32_fpdp_v3, cpu)) {
533 hwcaps |= ARM_HWCAP_ARM_VFPv3;
534 if (cpu_isar_feature(aa32_simd_r32, cpu)) {
535 hwcaps |= ARM_HWCAP_ARM_VFPD32;
536 } else {
537 hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
540 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
542 * MVFR1.FPHP and .SIMDHP must be in sync, and QEMU uses the same
543 * isar_feature function for both. The kernel reports them as two hwcaps.
545 GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_FPHP);
546 GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_ASIMDHP);
547 GET_FEATURE_ID(aa32_dp, ARM_HWCAP_ARM_ASIMDDP);
548 GET_FEATURE_ID(aa32_fhm, ARM_HWCAP_ARM_ASIMDFHM);
549 GET_FEATURE_ID(aa32_bf16, ARM_HWCAP_ARM_ASIMDBF16);
550 GET_FEATURE_ID(aa32_i8mm, ARM_HWCAP_ARM_I8MM);
552 return hwcaps;
555 uint64_t get_elf_hwcap2(void)
557 ARMCPU *cpu = ARM_CPU(thread_cpu);
558 uint64_t hwcaps = 0;
560 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
561 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
562 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
563 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
564 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
565 GET_FEATURE_ID(aa32_sb, ARM_HWCAP2_ARM_SB);
566 GET_FEATURE_ID(aa32_ssbs, ARM_HWCAP2_ARM_SSBS);
567 return hwcaps;
570 const char *elf_hwcap_str(uint32_t bit)
572 static const char *hwcap_str[] = {
573 [__builtin_ctz(ARM_HWCAP_ARM_SWP )] = "swp",
574 [__builtin_ctz(ARM_HWCAP_ARM_HALF )] = "half",
575 [__builtin_ctz(ARM_HWCAP_ARM_THUMB )] = "thumb",
576 [__builtin_ctz(ARM_HWCAP_ARM_26BIT )] = "26bit",
577 [__builtin_ctz(ARM_HWCAP_ARM_FAST_MULT)] = "fast_mult",
578 [__builtin_ctz(ARM_HWCAP_ARM_FPA )] = "fpa",
579 [__builtin_ctz(ARM_HWCAP_ARM_VFP )] = "vfp",
580 [__builtin_ctz(ARM_HWCAP_ARM_EDSP )] = "edsp",
581 [__builtin_ctz(ARM_HWCAP_ARM_JAVA )] = "java",
582 [__builtin_ctz(ARM_HWCAP_ARM_IWMMXT )] = "iwmmxt",
583 [__builtin_ctz(ARM_HWCAP_ARM_CRUNCH )] = "crunch",
584 [__builtin_ctz(ARM_HWCAP_ARM_THUMBEE )] = "thumbee",
585 [__builtin_ctz(ARM_HWCAP_ARM_NEON )] = "neon",
586 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3 )] = "vfpv3",
587 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3D16 )] = "vfpv3d16",
588 [__builtin_ctz(ARM_HWCAP_ARM_TLS )] = "tls",
589 [__builtin_ctz(ARM_HWCAP_ARM_VFPv4 )] = "vfpv4",
590 [__builtin_ctz(ARM_HWCAP_ARM_IDIVA )] = "idiva",
591 [__builtin_ctz(ARM_HWCAP_ARM_IDIVT )] = "idivt",
592 [__builtin_ctz(ARM_HWCAP_ARM_VFPD32 )] = "vfpd32",
593 [__builtin_ctz(ARM_HWCAP_ARM_LPAE )] = "lpae",
594 [__builtin_ctz(ARM_HWCAP_ARM_EVTSTRM )] = "evtstrm",
595 [__builtin_ctz(ARM_HWCAP_ARM_FPHP )] = "fphp",
596 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDHP )] = "asimdhp",
597 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDDP )] = "asimddp",
598 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDFHM )] = "asimdfhm",
599 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDBF16)] = "asimdbf16",
600 [__builtin_ctz(ARM_HWCAP_ARM_I8MM )] = "i8mm",
603 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
606 const char *elf_hwcap2_str(uint32_t bit)
608 static const char *hwcap_str[] = {
609 [__builtin_ctz(ARM_HWCAP2_ARM_AES )] = "aes",
610 [__builtin_ctz(ARM_HWCAP2_ARM_PMULL)] = "pmull",
611 [__builtin_ctz(ARM_HWCAP2_ARM_SHA1 )] = "sha1",
612 [__builtin_ctz(ARM_HWCAP2_ARM_SHA2 )] = "sha2",
613 [__builtin_ctz(ARM_HWCAP2_ARM_CRC32)] = "crc32",
614 [__builtin_ctz(ARM_HWCAP2_ARM_SB )] = "sb",
615 [__builtin_ctz(ARM_HWCAP2_ARM_SSBS )] = "ssbs",
618 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
621 #undef GET_FEATURE
622 #undef GET_FEATURE_ID
624 #define ELF_PLATFORM get_elf_platform()
626 static const char *get_elf_platform(void)
628 CPUARMState *env = cpu_env(thread_cpu);
630 #if TARGET_BIG_ENDIAN
631 # define END "b"
632 #else
633 # define END "l"
634 #endif
636 if (arm_feature(env, ARM_FEATURE_V8)) {
637 return "v8" END;
638 } else if (arm_feature(env, ARM_FEATURE_V7)) {
639 if (arm_feature(env, ARM_FEATURE_M)) {
640 return "v7m" END;
641 } else {
642 return "v7" END;
644 } else if (arm_feature(env, ARM_FEATURE_V6)) {
645 return "v6" END;
646 } else if (arm_feature(env, ARM_FEATURE_V5)) {
647 return "v5" END;
648 } else {
649 return "v4" END;
652 #undef END
655 #else
656 /* 64 bit ARM definitions */
658 #define ELF_ARCH EM_AARCH64
659 #define ELF_CLASS ELFCLASS64
660 #if TARGET_BIG_ENDIAN
661 # define ELF_PLATFORM "aarch64_be"
662 #else
663 # define ELF_PLATFORM "aarch64"
664 #endif
666 static inline void init_thread(struct target_pt_regs *regs,
667 struct image_info *infop)
669 abi_long stack = infop->start_stack;
670 memset(regs, 0, sizeof(*regs));
672 regs->pc = infop->entry & ~0x3ULL;
673 regs->sp = stack;
676 #define ELF_NREG 34
677 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
679 static void elf_core_copy_regs(target_elf_gregset_t *regs,
680 const CPUARMState *env)
682 int i;
684 for (i = 0; i < 32; i++) {
685 (*regs)[i] = tswapreg(env->xregs[i]);
687 (*regs)[32] = tswapreg(env->pc);
688 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
691 #define USE_ELF_CORE_DUMP
692 #define ELF_EXEC_PAGESIZE 4096
694 enum {
695 ARM_HWCAP_A64_FP = 1 << 0,
696 ARM_HWCAP_A64_ASIMD = 1 << 1,
697 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
698 ARM_HWCAP_A64_AES = 1 << 3,
699 ARM_HWCAP_A64_PMULL = 1 << 4,
700 ARM_HWCAP_A64_SHA1 = 1 << 5,
701 ARM_HWCAP_A64_SHA2 = 1 << 6,
702 ARM_HWCAP_A64_CRC32 = 1 << 7,
703 ARM_HWCAP_A64_ATOMICS = 1 << 8,
704 ARM_HWCAP_A64_FPHP = 1 << 9,
705 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
706 ARM_HWCAP_A64_CPUID = 1 << 11,
707 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
708 ARM_HWCAP_A64_JSCVT = 1 << 13,
709 ARM_HWCAP_A64_FCMA = 1 << 14,
710 ARM_HWCAP_A64_LRCPC = 1 << 15,
711 ARM_HWCAP_A64_DCPOP = 1 << 16,
712 ARM_HWCAP_A64_SHA3 = 1 << 17,
713 ARM_HWCAP_A64_SM3 = 1 << 18,
714 ARM_HWCAP_A64_SM4 = 1 << 19,
715 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
716 ARM_HWCAP_A64_SHA512 = 1 << 21,
717 ARM_HWCAP_A64_SVE = 1 << 22,
718 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
719 ARM_HWCAP_A64_DIT = 1 << 24,
720 ARM_HWCAP_A64_USCAT = 1 << 25,
721 ARM_HWCAP_A64_ILRCPC = 1 << 26,
722 ARM_HWCAP_A64_FLAGM = 1 << 27,
723 ARM_HWCAP_A64_SSBS = 1 << 28,
724 ARM_HWCAP_A64_SB = 1 << 29,
725 ARM_HWCAP_A64_PACA = 1 << 30,
726 ARM_HWCAP_A64_PACG = 1UL << 31,
728 ARM_HWCAP2_A64_DCPODP = 1 << 0,
729 ARM_HWCAP2_A64_SVE2 = 1 << 1,
730 ARM_HWCAP2_A64_SVEAES = 1 << 2,
731 ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
732 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
733 ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
734 ARM_HWCAP2_A64_SVESM4 = 1 << 6,
735 ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
736 ARM_HWCAP2_A64_FRINT = 1 << 8,
737 ARM_HWCAP2_A64_SVEI8MM = 1 << 9,
738 ARM_HWCAP2_A64_SVEF32MM = 1 << 10,
739 ARM_HWCAP2_A64_SVEF64MM = 1 << 11,
740 ARM_HWCAP2_A64_SVEBF16 = 1 << 12,
741 ARM_HWCAP2_A64_I8MM = 1 << 13,
742 ARM_HWCAP2_A64_BF16 = 1 << 14,
743 ARM_HWCAP2_A64_DGH = 1 << 15,
744 ARM_HWCAP2_A64_RNG = 1 << 16,
745 ARM_HWCAP2_A64_BTI = 1 << 17,
746 ARM_HWCAP2_A64_MTE = 1 << 18,
747 ARM_HWCAP2_A64_ECV = 1 << 19,
748 ARM_HWCAP2_A64_AFP = 1 << 20,
749 ARM_HWCAP2_A64_RPRES = 1 << 21,
750 ARM_HWCAP2_A64_MTE3 = 1 << 22,
751 ARM_HWCAP2_A64_SME = 1 << 23,
752 ARM_HWCAP2_A64_SME_I16I64 = 1 << 24,
753 ARM_HWCAP2_A64_SME_F64F64 = 1 << 25,
754 ARM_HWCAP2_A64_SME_I8I32 = 1 << 26,
755 ARM_HWCAP2_A64_SME_F16F32 = 1 << 27,
756 ARM_HWCAP2_A64_SME_B16F32 = 1 << 28,
757 ARM_HWCAP2_A64_SME_F32F32 = 1 << 29,
758 ARM_HWCAP2_A64_SME_FA64 = 1 << 30,
759 ARM_HWCAP2_A64_WFXT = 1ULL << 31,
760 ARM_HWCAP2_A64_EBF16 = 1ULL << 32,
761 ARM_HWCAP2_A64_SVE_EBF16 = 1ULL << 33,
762 ARM_HWCAP2_A64_CSSC = 1ULL << 34,
763 ARM_HWCAP2_A64_RPRFM = 1ULL << 35,
764 ARM_HWCAP2_A64_SVE2P1 = 1ULL << 36,
765 ARM_HWCAP2_A64_SME2 = 1ULL << 37,
766 ARM_HWCAP2_A64_SME2P1 = 1ULL << 38,
767 ARM_HWCAP2_A64_SME_I16I32 = 1ULL << 39,
768 ARM_HWCAP2_A64_SME_BI32I32 = 1ULL << 40,
769 ARM_HWCAP2_A64_SME_B16B16 = 1ULL << 41,
770 ARM_HWCAP2_A64_SME_F16F16 = 1ULL << 42,
771 ARM_HWCAP2_A64_MOPS = 1ULL << 43,
772 ARM_HWCAP2_A64_HBC = 1ULL << 44,
775 #define ELF_HWCAP get_elf_hwcap()
776 #define ELF_HWCAP2 get_elf_hwcap2()
778 #define GET_FEATURE_ID(feat, hwcap) \
779 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
781 uint32_t get_elf_hwcap(void)
783 ARMCPU *cpu = ARM_CPU(thread_cpu);
784 uint32_t hwcaps = 0;
786 hwcaps |= ARM_HWCAP_A64_FP;
787 hwcaps |= ARM_HWCAP_A64_ASIMD;
788 hwcaps |= ARM_HWCAP_A64_CPUID;
790 /* probe for the extra features */
792 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
793 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
794 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
795 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
796 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
797 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
798 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
799 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
800 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
801 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
802 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
803 GET_FEATURE_ID(aa64_lse2, ARM_HWCAP_A64_USCAT);
804 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
805 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
806 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
807 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
808 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
809 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
810 GET_FEATURE_ID(aa64_dit, ARM_HWCAP_A64_DIT);
811 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
812 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
813 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
814 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
815 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
816 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
818 return hwcaps;
821 uint64_t get_elf_hwcap2(void)
823 ARMCPU *cpu = ARM_CPU(thread_cpu);
824 uint64_t hwcaps = 0;
826 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
827 GET_FEATURE_ID(aa64_sve2, ARM_HWCAP2_A64_SVE2);
828 GET_FEATURE_ID(aa64_sve2_aes, ARM_HWCAP2_A64_SVEAES);
829 GET_FEATURE_ID(aa64_sve2_pmull128, ARM_HWCAP2_A64_SVEPMULL);
830 GET_FEATURE_ID(aa64_sve2_bitperm, ARM_HWCAP2_A64_SVEBITPERM);
831 GET_FEATURE_ID(aa64_sve2_sha3, ARM_HWCAP2_A64_SVESHA3);
832 GET_FEATURE_ID(aa64_sve2_sm4, ARM_HWCAP2_A64_SVESM4);
833 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
834 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
835 GET_FEATURE_ID(aa64_sve_i8mm, ARM_HWCAP2_A64_SVEI8MM);
836 GET_FEATURE_ID(aa64_sve_f32mm, ARM_HWCAP2_A64_SVEF32MM);
837 GET_FEATURE_ID(aa64_sve_f64mm, ARM_HWCAP2_A64_SVEF64MM);
838 GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16);
839 GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM);
840 GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16);
841 GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG);
842 GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI);
843 GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE);
844 GET_FEATURE_ID(aa64_mte3, ARM_HWCAP2_A64_MTE3);
845 GET_FEATURE_ID(aa64_sme, (ARM_HWCAP2_A64_SME |
846 ARM_HWCAP2_A64_SME_F32F32 |
847 ARM_HWCAP2_A64_SME_B16F32 |
848 ARM_HWCAP2_A64_SME_F16F32 |
849 ARM_HWCAP2_A64_SME_I8I32));
850 GET_FEATURE_ID(aa64_sme_f64f64, ARM_HWCAP2_A64_SME_F64F64);
851 GET_FEATURE_ID(aa64_sme_i16i64, ARM_HWCAP2_A64_SME_I16I64);
852 GET_FEATURE_ID(aa64_sme_fa64, ARM_HWCAP2_A64_SME_FA64);
853 GET_FEATURE_ID(aa64_hbc, ARM_HWCAP2_A64_HBC);
854 GET_FEATURE_ID(aa64_mops, ARM_HWCAP2_A64_MOPS);
856 return hwcaps;
859 const char *elf_hwcap_str(uint32_t bit)
861 static const char *hwcap_str[] = {
862 [__builtin_ctz(ARM_HWCAP_A64_FP )] = "fp",
863 [__builtin_ctz(ARM_HWCAP_A64_ASIMD )] = "asimd",
864 [__builtin_ctz(ARM_HWCAP_A64_EVTSTRM )] = "evtstrm",
865 [__builtin_ctz(ARM_HWCAP_A64_AES )] = "aes",
866 [__builtin_ctz(ARM_HWCAP_A64_PMULL )] = "pmull",
867 [__builtin_ctz(ARM_HWCAP_A64_SHA1 )] = "sha1",
868 [__builtin_ctz(ARM_HWCAP_A64_SHA2 )] = "sha2",
869 [__builtin_ctz(ARM_HWCAP_A64_CRC32 )] = "crc32",
870 [__builtin_ctz(ARM_HWCAP_A64_ATOMICS )] = "atomics",
871 [__builtin_ctz(ARM_HWCAP_A64_FPHP )] = "fphp",
872 [__builtin_ctz(ARM_HWCAP_A64_ASIMDHP )] = "asimdhp",
873 [__builtin_ctz(ARM_HWCAP_A64_CPUID )] = "cpuid",
874 [__builtin_ctz(ARM_HWCAP_A64_ASIMDRDM)] = "asimdrdm",
875 [__builtin_ctz(ARM_HWCAP_A64_JSCVT )] = "jscvt",
876 [__builtin_ctz(ARM_HWCAP_A64_FCMA )] = "fcma",
877 [__builtin_ctz(ARM_HWCAP_A64_LRCPC )] = "lrcpc",
878 [__builtin_ctz(ARM_HWCAP_A64_DCPOP )] = "dcpop",
879 [__builtin_ctz(ARM_HWCAP_A64_SHA3 )] = "sha3",
880 [__builtin_ctz(ARM_HWCAP_A64_SM3 )] = "sm3",
881 [__builtin_ctz(ARM_HWCAP_A64_SM4 )] = "sm4",
882 [__builtin_ctz(ARM_HWCAP_A64_ASIMDDP )] = "asimddp",
883 [__builtin_ctz(ARM_HWCAP_A64_SHA512 )] = "sha512",
884 [__builtin_ctz(ARM_HWCAP_A64_SVE )] = "sve",
885 [__builtin_ctz(ARM_HWCAP_A64_ASIMDFHM)] = "asimdfhm",
886 [__builtin_ctz(ARM_HWCAP_A64_DIT )] = "dit",
887 [__builtin_ctz(ARM_HWCAP_A64_USCAT )] = "uscat",
888 [__builtin_ctz(ARM_HWCAP_A64_ILRCPC )] = "ilrcpc",
889 [__builtin_ctz(ARM_HWCAP_A64_FLAGM )] = "flagm",
890 [__builtin_ctz(ARM_HWCAP_A64_SSBS )] = "ssbs",
891 [__builtin_ctz(ARM_HWCAP_A64_SB )] = "sb",
892 [__builtin_ctz(ARM_HWCAP_A64_PACA )] = "paca",
893 [__builtin_ctz(ARM_HWCAP_A64_PACG )] = "pacg",
896 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
899 const char *elf_hwcap2_str(uint32_t bit)
901 static const char *hwcap_str[] = {
902 [__builtin_ctz(ARM_HWCAP2_A64_DCPODP )] = "dcpodp",
903 [__builtin_ctz(ARM_HWCAP2_A64_SVE2 )] = "sve2",
904 [__builtin_ctz(ARM_HWCAP2_A64_SVEAES )] = "sveaes",
905 [__builtin_ctz(ARM_HWCAP2_A64_SVEPMULL )] = "svepmull",
906 [__builtin_ctz(ARM_HWCAP2_A64_SVEBITPERM )] = "svebitperm",
907 [__builtin_ctz(ARM_HWCAP2_A64_SVESHA3 )] = "svesha3",
908 [__builtin_ctz(ARM_HWCAP2_A64_SVESM4 )] = "svesm4",
909 [__builtin_ctz(ARM_HWCAP2_A64_FLAGM2 )] = "flagm2",
910 [__builtin_ctz(ARM_HWCAP2_A64_FRINT )] = "frint",
911 [__builtin_ctz(ARM_HWCAP2_A64_SVEI8MM )] = "svei8mm",
912 [__builtin_ctz(ARM_HWCAP2_A64_SVEF32MM )] = "svef32mm",
913 [__builtin_ctz(ARM_HWCAP2_A64_SVEF64MM )] = "svef64mm",
914 [__builtin_ctz(ARM_HWCAP2_A64_SVEBF16 )] = "svebf16",
915 [__builtin_ctz(ARM_HWCAP2_A64_I8MM )] = "i8mm",
916 [__builtin_ctz(ARM_HWCAP2_A64_BF16 )] = "bf16",
917 [__builtin_ctz(ARM_HWCAP2_A64_DGH )] = "dgh",
918 [__builtin_ctz(ARM_HWCAP2_A64_RNG )] = "rng",
919 [__builtin_ctz(ARM_HWCAP2_A64_BTI )] = "bti",
920 [__builtin_ctz(ARM_HWCAP2_A64_MTE )] = "mte",
921 [__builtin_ctz(ARM_HWCAP2_A64_ECV )] = "ecv",
922 [__builtin_ctz(ARM_HWCAP2_A64_AFP )] = "afp",
923 [__builtin_ctz(ARM_HWCAP2_A64_RPRES )] = "rpres",
924 [__builtin_ctz(ARM_HWCAP2_A64_MTE3 )] = "mte3",
925 [__builtin_ctz(ARM_HWCAP2_A64_SME )] = "sme",
926 [__builtin_ctz(ARM_HWCAP2_A64_SME_I16I64 )] = "smei16i64",
927 [__builtin_ctz(ARM_HWCAP2_A64_SME_F64F64 )] = "smef64f64",
928 [__builtin_ctz(ARM_HWCAP2_A64_SME_I8I32 )] = "smei8i32",
929 [__builtin_ctz(ARM_HWCAP2_A64_SME_F16F32 )] = "smef16f32",
930 [__builtin_ctz(ARM_HWCAP2_A64_SME_B16F32 )] = "smeb16f32",
931 [__builtin_ctz(ARM_HWCAP2_A64_SME_F32F32 )] = "smef32f32",
932 [__builtin_ctz(ARM_HWCAP2_A64_SME_FA64 )] = "smefa64",
933 [__builtin_ctz(ARM_HWCAP2_A64_WFXT )] = "wfxt",
934 [__builtin_ctzll(ARM_HWCAP2_A64_EBF16 )] = "ebf16",
935 [__builtin_ctzll(ARM_HWCAP2_A64_SVE_EBF16 )] = "sveebf16",
936 [__builtin_ctzll(ARM_HWCAP2_A64_CSSC )] = "cssc",
937 [__builtin_ctzll(ARM_HWCAP2_A64_RPRFM )] = "rprfm",
938 [__builtin_ctzll(ARM_HWCAP2_A64_SVE2P1 )] = "sve2p1",
939 [__builtin_ctzll(ARM_HWCAP2_A64_SME2 )] = "sme2",
940 [__builtin_ctzll(ARM_HWCAP2_A64_SME2P1 )] = "sme2p1",
941 [__builtin_ctzll(ARM_HWCAP2_A64_SME_I16I32 )] = "smei16i32",
942 [__builtin_ctzll(ARM_HWCAP2_A64_SME_BI32I32)] = "smebi32i32",
943 [__builtin_ctzll(ARM_HWCAP2_A64_SME_B16B16 )] = "smeb16b16",
944 [__builtin_ctzll(ARM_HWCAP2_A64_SME_F16F16 )] = "smef16f16",
945 [__builtin_ctzll(ARM_HWCAP2_A64_MOPS )] = "mops",
946 [__builtin_ctzll(ARM_HWCAP2_A64_HBC )] = "hbc",
949 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
952 #undef GET_FEATURE_ID
954 #endif /* not TARGET_AARCH64 */
956 #if TARGET_BIG_ENDIAN
957 # define VDSO_HEADER "vdso-be.c.inc"
958 #else
959 # define VDSO_HEADER "vdso-le.c.inc"
960 #endif
962 #endif /* TARGET_ARM */
964 #ifdef TARGET_SPARC
965 #ifdef TARGET_SPARC64
967 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
968 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
969 #ifndef TARGET_ABI32
970 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
971 #else
972 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
973 #endif
975 #define ELF_CLASS ELFCLASS64
976 #define ELF_ARCH EM_SPARCV9
977 #else
978 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
979 | HWCAP_SPARC_MULDIV)
980 #define ELF_CLASS ELFCLASS32
981 #define ELF_ARCH EM_SPARC
982 #endif /* TARGET_SPARC64 */
984 static inline void init_thread(struct target_pt_regs *regs,
985 struct image_info *infop)
987 /* Note that target_cpu_copy_regs does not read psr/tstate. */
988 regs->pc = infop->entry;
989 regs->npc = regs->pc + 4;
990 regs->y = 0;
991 regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong)
992 - TARGET_STACK_BIAS);
994 #endif /* TARGET_SPARC */
996 #ifdef TARGET_PPC
998 #define ELF_MACHINE PPC_ELF_MACHINE
1000 #if defined(TARGET_PPC64)
1002 #define elf_check_arch(x) ( (x) == EM_PPC64 )
1004 #define ELF_CLASS ELFCLASS64
1006 #else
1008 #define ELF_CLASS ELFCLASS32
1009 #define EXSTACK_DEFAULT true
1011 #endif
1013 #define ELF_ARCH EM_PPC
1015 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
1016 See arch/powerpc/include/asm/cputable.h. */
1017 enum {
1018 QEMU_PPC_FEATURE_32 = 0x80000000,
1019 QEMU_PPC_FEATURE_64 = 0x40000000,
1020 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
1021 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
1022 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
1023 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
1024 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
1025 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
1026 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
1027 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
1028 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
1029 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
1030 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
1031 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
1032 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
1033 QEMU_PPC_FEATURE_CELL = 0x00010000,
1034 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
1035 QEMU_PPC_FEATURE_SMT = 0x00004000,
1036 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
1037 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
1038 QEMU_PPC_FEATURE_PA6T = 0x00000800,
1039 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
1040 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
1041 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
1042 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
1043 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
1045 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
1046 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
1048 /* Feature definitions in AT_HWCAP2. */
1049 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
1050 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
1051 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
1052 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
1053 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
1054 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
1055 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
1056 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
1057 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
1058 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
1059 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
1060 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
1061 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
1062 QEMU_PPC_FEATURE2_ARCH_3_1 = 0x00040000, /* ISA 3.1 */
1063 QEMU_PPC_FEATURE2_MMA = 0x00020000, /* Matrix-Multiply Assist */
1066 #define ELF_HWCAP get_elf_hwcap()
1068 static uint32_t get_elf_hwcap(void)
1070 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
1071 uint32_t features = 0;
1073 /* We don't have to be terribly complete here; the high points are
1074 Altivec/FP/SPE support. Anything else is just a bonus. */
1075 #define GET_FEATURE(flag, feature) \
1076 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1077 #define GET_FEATURE2(flags, feature) \
1078 do { \
1079 if ((cpu->env.insns_flags2 & flags) == flags) { \
1080 features |= feature; \
1082 } while (0)
1083 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
1084 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
1085 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
1086 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
1087 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
1088 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
1089 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
1090 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
1091 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
1092 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
1093 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
1094 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
1095 QEMU_PPC_FEATURE_ARCH_2_06);
1096 #undef GET_FEATURE
1097 #undef GET_FEATURE2
1099 return features;
1102 #define ELF_HWCAP2 get_elf_hwcap2()
1104 static uint32_t get_elf_hwcap2(void)
1106 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
1107 uint32_t features = 0;
1109 #define GET_FEATURE(flag, feature) \
1110 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1111 #define GET_FEATURE2(flag, feature) \
1112 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
1114 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
1115 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
1116 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
1117 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
1118 QEMU_PPC_FEATURE2_VEC_CRYPTO);
1119 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
1120 QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128);
1121 GET_FEATURE2(PPC2_ISA310, QEMU_PPC_FEATURE2_ARCH_3_1 |
1122 QEMU_PPC_FEATURE2_MMA);
1124 #undef GET_FEATURE
1125 #undef GET_FEATURE2
1127 return features;
1131 * The requirements here are:
1132 * - keep the final alignment of sp (sp & 0xf)
1133 * - make sure the 32-bit value at the first 16 byte aligned position of
1134 * AUXV is greater than 16 for glibc compatibility.
1135 * AT_IGNOREPPC is used for that.
1136 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
1137 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
1139 #define DLINFO_ARCH_ITEMS 5
1140 #define ARCH_DLINFO \
1141 do { \
1142 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
1143 /* \
1144 * Handle glibc compatibility: these magic entries must \
1145 * be at the lowest addresses in the final auxv. \
1146 */ \
1147 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1148 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1149 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
1150 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
1151 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
1152 } while (0)
1154 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
1156 _regs->gpr[1] = infop->start_stack;
1157 #if defined(TARGET_PPC64)
1158 if (get_ppc64_abi(infop) < 2) {
1159 uint64_t val;
1160 get_user_u64(val, infop->entry + 8);
1161 _regs->gpr[2] = val + infop->load_bias;
1162 get_user_u64(val, infop->entry);
1163 infop->entry = val + infop->load_bias;
1164 } else {
1165 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
1167 #endif
1168 _regs->nip = infop->entry;
1171 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
1172 #define ELF_NREG 48
1173 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1175 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
1177 int i;
1178 target_ulong ccr = 0;
1180 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
1181 (*regs)[i] = tswapreg(env->gpr[i]);
1184 (*regs)[32] = tswapreg(env->nip);
1185 (*regs)[33] = tswapreg(env->msr);
1186 (*regs)[35] = tswapreg(env->ctr);
1187 (*regs)[36] = tswapreg(env->lr);
1188 (*regs)[37] = tswapreg(cpu_read_xer(env));
1190 ccr = ppc_get_cr(env);
1191 (*regs)[38] = tswapreg(ccr);
1194 #define USE_ELF_CORE_DUMP
1195 #define ELF_EXEC_PAGESIZE 4096
1197 #ifndef TARGET_PPC64
1198 # define VDSO_HEADER "vdso-32.c.inc"
1199 #elif TARGET_BIG_ENDIAN
1200 # define VDSO_HEADER "vdso-64.c.inc"
1201 #else
1202 # define VDSO_HEADER "vdso-64le.c.inc"
1203 #endif
1205 #endif
1207 #ifdef TARGET_LOONGARCH64
1209 #define ELF_CLASS ELFCLASS64
1210 #define ELF_ARCH EM_LOONGARCH
1211 #define EXSTACK_DEFAULT true
1213 #define elf_check_arch(x) ((x) == EM_LOONGARCH)
1215 #define VDSO_HEADER "vdso.c.inc"
1217 static inline void init_thread(struct target_pt_regs *regs,
1218 struct image_info *infop)
1220 /*Set crmd PG,DA = 1,0 */
1221 regs->csr.crmd = 2 << 3;
1222 regs->csr.era = infop->entry;
1223 regs->regs[3] = infop->start_stack;
1226 /* See linux kernel: arch/loongarch/include/asm/elf.h */
1227 #define ELF_NREG 45
1228 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1230 enum {
1231 TARGET_EF_R0 = 0,
1232 TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33,
1233 TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34,
1236 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1237 const CPULoongArchState *env)
1239 int i;
1241 (*regs)[TARGET_EF_R0] = 0;
1243 for (i = 1; i < ARRAY_SIZE(env->gpr); i++) {
1244 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]);
1247 (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc);
1248 (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV);
1251 #define USE_ELF_CORE_DUMP
1252 #define ELF_EXEC_PAGESIZE 4096
1254 #define ELF_HWCAP get_elf_hwcap()
1256 /* See arch/loongarch/include/uapi/asm/hwcap.h */
1257 enum {
1258 HWCAP_LOONGARCH_CPUCFG = (1 << 0),
1259 HWCAP_LOONGARCH_LAM = (1 << 1),
1260 HWCAP_LOONGARCH_UAL = (1 << 2),
1261 HWCAP_LOONGARCH_FPU = (1 << 3),
1262 HWCAP_LOONGARCH_LSX = (1 << 4),
1263 HWCAP_LOONGARCH_LASX = (1 << 5),
1264 HWCAP_LOONGARCH_CRC32 = (1 << 6),
1265 HWCAP_LOONGARCH_COMPLEX = (1 << 7),
1266 HWCAP_LOONGARCH_CRYPTO = (1 << 8),
1267 HWCAP_LOONGARCH_LVZ = (1 << 9),
1268 HWCAP_LOONGARCH_LBT_X86 = (1 << 10),
1269 HWCAP_LOONGARCH_LBT_ARM = (1 << 11),
1270 HWCAP_LOONGARCH_LBT_MIPS = (1 << 12),
1273 static uint32_t get_elf_hwcap(void)
1275 LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu);
1276 uint32_t hwcaps = 0;
1278 hwcaps |= HWCAP_LOONGARCH_CRC32;
1280 if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) {
1281 hwcaps |= HWCAP_LOONGARCH_UAL;
1284 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) {
1285 hwcaps |= HWCAP_LOONGARCH_FPU;
1288 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) {
1289 hwcaps |= HWCAP_LOONGARCH_LAM;
1292 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LSX)) {
1293 hwcaps |= HWCAP_LOONGARCH_LSX;
1296 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LASX)) {
1297 hwcaps |= HWCAP_LOONGARCH_LASX;
1300 return hwcaps;
1303 #define ELF_PLATFORM "loongarch"
1305 #endif /* TARGET_LOONGARCH64 */
1307 #ifdef TARGET_MIPS
1309 #ifdef TARGET_MIPS64
1310 #define ELF_CLASS ELFCLASS64
1311 #else
1312 #define ELF_CLASS ELFCLASS32
1313 #endif
1314 #define ELF_ARCH EM_MIPS
1315 #define EXSTACK_DEFAULT true
1317 #ifdef TARGET_ABI_MIPSN32
1318 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1319 #else
1320 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1321 #endif
1323 #define ELF_BASE_PLATFORM get_elf_base_platform()
1325 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \
1326 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1327 { return _base_platform; } } while (0)
1329 static const char *get_elf_base_platform(void)
1331 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1333 /* 64 bit ISAs goes first */
1334 MATCH_PLATFORM_INSN(CPU_MIPS64R6, "mips64r6");
1335 MATCH_PLATFORM_INSN(CPU_MIPS64R5, "mips64r5");
1336 MATCH_PLATFORM_INSN(CPU_MIPS64R2, "mips64r2");
1337 MATCH_PLATFORM_INSN(CPU_MIPS64R1, "mips64");
1338 MATCH_PLATFORM_INSN(CPU_MIPS5, "mips5");
1339 MATCH_PLATFORM_INSN(CPU_MIPS4, "mips4");
1340 MATCH_PLATFORM_INSN(CPU_MIPS3, "mips3");
1342 /* 32 bit ISAs */
1343 MATCH_PLATFORM_INSN(CPU_MIPS32R6, "mips32r6");
1344 MATCH_PLATFORM_INSN(CPU_MIPS32R5, "mips32r5");
1345 MATCH_PLATFORM_INSN(CPU_MIPS32R2, "mips32r2");
1346 MATCH_PLATFORM_INSN(CPU_MIPS32R1, "mips32");
1347 MATCH_PLATFORM_INSN(CPU_MIPS2, "mips2");
1349 /* Fallback */
1350 return "mips";
1352 #undef MATCH_PLATFORM_INSN
1354 static inline void init_thread(struct target_pt_regs *regs,
1355 struct image_info *infop)
1357 regs->cp0_status = 2 << CP0St_KSU;
1358 regs->cp0_epc = infop->entry;
1359 regs->regs[29] = infop->start_stack;
1362 /* See linux kernel: arch/mips/include/asm/elf.h. */
1363 #define ELF_NREG 45
1364 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1366 /* See linux kernel: arch/mips/include/asm/reg.h. */
1367 enum {
1368 #ifdef TARGET_MIPS64
1369 TARGET_EF_R0 = 0,
1370 #else
1371 TARGET_EF_R0 = 6,
1372 #endif
1373 TARGET_EF_R26 = TARGET_EF_R0 + 26,
1374 TARGET_EF_R27 = TARGET_EF_R0 + 27,
1375 TARGET_EF_LO = TARGET_EF_R0 + 32,
1376 TARGET_EF_HI = TARGET_EF_R0 + 33,
1377 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
1378 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
1379 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
1380 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
1383 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1384 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
1386 int i;
1388 for (i = 0; i < TARGET_EF_R0; i++) {
1389 (*regs)[i] = 0;
1391 (*regs)[TARGET_EF_R0] = 0;
1393 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
1394 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
1397 (*regs)[TARGET_EF_R26] = 0;
1398 (*regs)[TARGET_EF_R27] = 0;
1399 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
1400 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
1401 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
1402 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
1403 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
1404 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
1407 #define USE_ELF_CORE_DUMP
1408 #define ELF_EXEC_PAGESIZE 4096
1410 /* See arch/mips/include/uapi/asm/hwcap.h. */
1411 enum {
1412 HWCAP_MIPS_R6 = (1 << 0),
1413 HWCAP_MIPS_MSA = (1 << 1),
1414 HWCAP_MIPS_CRC32 = (1 << 2),
1415 HWCAP_MIPS_MIPS16 = (1 << 3),
1416 HWCAP_MIPS_MDMX = (1 << 4),
1417 HWCAP_MIPS_MIPS3D = (1 << 5),
1418 HWCAP_MIPS_SMARTMIPS = (1 << 6),
1419 HWCAP_MIPS_DSP = (1 << 7),
1420 HWCAP_MIPS_DSP2 = (1 << 8),
1421 HWCAP_MIPS_DSP3 = (1 << 9),
1422 HWCAP_MIPS_MIPS16E2 = (1 << 10),
1423 HWCAP_LOONGSON_MMI = (1 << 11),
1424 HWCAP_LOONGSON_EXT = (1 << 12),
1425 HWCAP_LOONGSON_EXT2 = (1 << 13),
1426 HWCAP_LOONGSON_CPUCFG = (1 << 14),
1429 #define ELF_HWCAP get_elf_hwcap()
1431 #define GET_FEATURE_INSN(_flag, _hwcap) \
1432 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1434 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1435 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1437 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1438 do { \
1439 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1440 hwcaps |= _hwcap; \
1442 } while (0)
1444 static uint32_t get_elf_hwcap(void)
1446 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1447 uint32_t hwcaps = 0;
1449 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH,
1450 2, HWCAP_MIPS_R6);
1451 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA);
1452 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI);
1453 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT);
1455 return hwcaps;
1458 #undef GET_FEATURE_REG_EQU
1459 #undef GET_FEATURE_REG_SET
1460 #undef GET_FEATURE_INSN
1462 #endif /* TARGET_MIPS */
1464 #ifdef TARGET_MICROBLAZE
1466 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1468 #define ELF_CLASS ELFCLASS32
1469 #define ELF_ARCH EM_MICROBLAZE
1471 static inline void init_thread(struct target_pt_regs *regs,
1472 struct image_info *infop)
1474 regs->pc = infop->entry;
1475 regs->r1 = infop->start_stack;
1479 #define ELF_EXEC_PAGESIZE 4096
1481 #define USE_ELF_CORE_DUMP
1482 #define ELF_NREG 38
1483 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1485 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1486 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1488 int i, pos = 0;
1490 for (i = 0; i < 32; i++) {
1491 (*regs)[pos++] = tswapreg(env->regs[i]);
1494 (*regs)[pos++] = tswapreg(env->pc);
1495 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
1496 (*regs)[pos++] = 0;
1497 (*regs)[pos++] = tswapreg(env->ear);
1498 (*regs)[pos++] = 0;
1499 (*regs)[pos++] = tswapreg(env->esr);
1502 #endif /* TARGET_MICROBLAZE */
1504 #ifdef TARGET_NIOS2
1506 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1508 #define ELF_CLASS ELFCLASS32
1509 #define ELF_ARCH EM_ALTERA_NIOS2
1511 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1513 regs->ea = infop->entry;
1514 regs->sp = infop->start_stack;
1517 #define LO_COMMPAGE TARGET_PAGE_SIZE
1519 static bool init_guest_commpage(void)
1521 static const uint8_t kuser_page[4 + 2 * 64] = {
1522 /* __kuser_helper_version */
1523 [0x00] = 0x02, 0x00, 0x00, 0x00,
1525 /* __kuser_cmpxchg */
1526 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1527 0x3a, 0x28, 0x00, 0xf8, /* ret */
1529 /* __kuser_sigtramp */
1530 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1531 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1534 void *want = g2h_untagged(LO_COMMPAGE & -qemu_host_page_size);
1535 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
1536 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1538 if (addr == MAP_FAILED) {
1539 perror("Allocating guest commpage");
1540 exit(EXIT_FAILURE);
1542 if (addr != want) {
1543 return false;
1546 memcpy(addr, kuser_page, sizeof(kuser_page));
1548 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
1549 perror("Protecting guest commpage");
1550 exit(EXIT_FAILURE);
1553 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1554 PAGE_READ | PAGE_EXEC | PAGE_VALID);
1555 return true;
1558 #define ELF_EXEC_PAGESIZE 4096
1560 #define USE_ELF_CORE_DUMP
1561 #define ELF_NREG 49
1562 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1564 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1565 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1566 const CPUNios2State *env)
1568 int i;
1570 (*regs)[0] = -1;
1571 for (i = 1; i < 8; i++) /* r0-r7 */
1572 (*regs)[i] = tswapreg(env->regs[i + 7]);
1574 for (i = 8; i < 16; i++) /* r8-r15 */
1575 (*regs)[i] = tswapreg(env->regs[i - 8]);
1577 for (i = 16; i < 24; i++) /* r16-r23 */
1578 (*regs)[i] = tswapreg(env->regs[i + 7]);
1579 (*regs)[24] = -1; /* R_ET */
1580 (*regs)[25] = -1; /* R_BT */
1581 (*regs)[26] = tswapreg(env->regs[R_GP]);
1582 (*regs)[27] = tswapreg(env->regs[R_SP]);
1583 (*regs)[28] = tswapreg(env->regs[R_FP]);
1584 (*regs)[29] = tswapreg(env->regs[R_EA]);
1585 (*regs)[30] = -1; /* R_SSTATUS */
1586 (*regs)[31] = tswapreg(env->regs[R_RA]);
1588 (*regs)[32] = tswapreg(env->pc);
1590 (*regs)[33] = -1; /* R_STATUS */
1591 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1593 for (i = 35; i < 49; i++) /* ... */
1594 (*regs)[i] = -1;
1597 #endif /* TARGET_NIOS2 */
1599 #ifdef TARGET_OPENRISC
1601 #define ELF_ARCH EM_OPENRISC
1602 #define ELF_CLASS ELFCLASS32
1603 #define ELF_DATA ELFDATA2MSB
1605 static inline void init_thread(struct target_pt_regs *regs,
1606 struct image_info *infop)
1608 regs->pc = infop->entry;
1609 regs->gpr[1] = infop->start_stack;
1612 #define USE_ELF_CORE_DUMP
1613 #define ELF_EXEC_PAGESIZE 8192
1615 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1616 #define ELF_NREG 34 /* gprs and pc, sr */
1617 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1619 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1620 const CPUOpenRISCState *env)
1622 int i;
1624 for (i = 0; i < 32; i++) {
1625 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1627 (*regs)[32] = tswapreg(env->pc);
1628 (*regs)[33] = tswapreg(cpu_get_sr(env));
1630 #define ELF_HWCAP 0
1631 #define ELF_PLATFORM NULL
1633 #endif /* TARGET_OPENRISC */
1635 #ifdef TARGET_SH4
1637 #define ELF_CLASS ELFCLASS32
1638 #define ELF_ARCH EM_SH
1640 static inline void init_thread(struct target_pt_regs *regs,
1641 struct image_info *infop)
1643 /* Check other registers XXXXX */
1644 regs->pc = infop->entry;
1645 regs->regs[15] = infop->start_stack;
1648 /* See linux kernel: arch/sh/include/asm/elf.h. */
1649 #define ELF_NREG 23
1650 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1652 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1653 enum {
1654 TARGET_REG_PC = 16,
1655 TARGET_REG_PR = 17,
1656 TARGET_REG_SR = 18,
1657 TARGET_REG_GBR = 19,
1658 TARGET_REG_MACH = 20,
1659 TARGET_REG_MACL = 21,
1660 TARGET_REG_SYSCALL = 22
1663 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1664 const CPUSH4State *env)
1666 int i;
1668 for (i = 0; i < 16; i++) {
1669 (*regs)[i] = tswapreg(env->gregs[i]);
1672 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1673 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1674 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1675 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1676 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1677 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1678 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1681 #define USE_ELF_CORE_DUMP
1682 #define ELF_EXEC_PAGESIZE 4096
1684 enum {
1685 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1686 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1687 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1688 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1689 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1690 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1691 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1692 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1693 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1694 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1697 #define ELF_HWCAP get_elf_hwcap()
1699 static uint32_t get_elf_hwcap(void)
1701 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1702 uint32_t hwcap = 0;
1704 hwcap |= SH_CPU_HAS_FPU;
1706 if (cpu->env.features & SH_FEATURE_SH4A) {
1707 hwcap |= SH_CPU_HAS_LLSC;
1710 return hwcap;
1713 #endif
1715 #ifdef TARGET_CRIS
1717 #define ELF_CLASS ELFCLASS32
1718 #define ELF_ARCH EM_CRIS
1720 static inline void init_thread(struct target_pt_regs *regs,
1721 struct image_info *infop)
1723 regs->erp = infop->entry;
1726 #define ELF_EXEC_PAGESIZE 8192
1728 #endif
1730 #ifdef TARGET_M68K
1732 #define ELF_CLASS ELFCLASS32
1733 #define ELF_ARCH EM_68K
1735 /* ??? Does this need to do anything?
1736 #define ELF_PLAT_INIT(_r) */
1738 static inline void init_thread(struct target_pt_regs *regs,
1739 struct image_info *infop)
1741 regs->usp = infop->start_stack;
1742 regs->sr = 0;
1743 regs->pc = infop->entry;
1746 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1747 #define ELF_NREG 20
1748 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1750 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1752 (*regs)[0] = tswapreg(env->dregs[1]);
1753 (*regs)[1] = tswapreg(env->dregs[2]);
1754 (*regs)[2] = tswapreg(env->dregs[3]);
1755 (*regs)[3] = tswapreg(env->dregs[4]);
1756 (*regs)[4] = tswapreg(env->dregs[5]);
1757 (*regs)[5] = tswapreg(env->dregs[6]);
1758 (*regs)[6] = tswapreg(env->dregs[7]);
1759 (*regs)[7] = tswapreg(env->aregs[0]);
1760 (*regs)[8] = tswapreg(env->aregs[1]);
1761 (*regs)[9] = tswapreg(env->aregs[2]);
1762 (*regs)[10] = tswapreg(env->aregs[3]);
1763 (*regs)[11] = tswapreg(env->aregs[4]);
1764 (*regs)[12] = tswapreg(env->aregs[5]);
1765 (*regs)[13] = tswapreg(env->aregs[6]);
1766 (*regs)[14] = tswapreg(env->dregs[0]);
1767 (*regs)[15] = tswapreg(env->aregs[7]);
1768 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1769 (*regs)[17] = tswapreg(env->sr);
1770 (*regs)[18] = tswapreg(env->pc);
1771 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1774 #define USE_ELF_CORE_DUMP
1775 #define ELF_EXEC_PAGESIZE 8192
1777 #endif
1779 #ifdef TARGET_ALPHA
1781 #define ELF_CLASS ELFCLASS64
1782 #define ELF_ARCH EM_ALPHA
1784 static inline void init_thread(struct target_pt_regs *regs,
1785 struct image_info *infop)
1787 regs->pc = infop->entry;
1788 regs->ps = 8;
1789 regs->usp = infop->start_stack;
1792 #define ELF_EXEC_PAGESIZE 8192
1794 #endif /* TARGET_ALPHA */
1796 #ifdef TARGET_S390X
1798 #define ELF_CLASS ELFCLASS64
1799 #define ELF_DATA ELFDATA2MSB
1800 #define ELF_ARCH EM_S390
1802 #include "elf.h"
1804 #define ELF_HWCAP get_elf_hwcap()
1806 #define GET_FEATURE(_feat, _hwcap) \
1807 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1809 uint32_t get_elf_hwcap(void)
1812 * Let's assume we always have esan3 and zarch.
1813 * 31-bit processes can use 64-bit registers (high gprs).
1815 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1817 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1818 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1819 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1820 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1821 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1822 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1823 hwcap |= HWCAP_S390_ETF3EH;
1825 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1826 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT);
1827 GET_FEATURE(S390_FEAT_VECTOR_ENH2, HWCAP_S390_VXRS_EXT2);
1829 return hwcap;
1832 const char *elf_hwcap_str(uint32_t bit)
1834 static const char *hwcap_str[] = {
1835 [HWCAP_S390_NR_ESAN3] = "esan3",
1836 [HWCAP_S390_NR_ZARCH] = "zarch",
1837 [HWCAP_S390_NR_STFLE] = "stfle",
1838 [HWCAP_S390_NR_MSA] = "msa",
1839 [HWCAP_S390_NR_LDISP] = "ldisp",
1840 [HWCAP_S390_NR_EIMM] = "eimm",
1841 [HWCAP_S390_NR_DFP] = "dfp",
1842 [HWCAP_S390_NR_HPAGE] = "edat",
1843 [HWCAP_S390_NR_ETF3EH] = "etf3eh",
1844 [HWCAP_S390_NR_HIGH_GPRS] = "highgprs",
1845 [HWCAP_S390_NR_TE] = "te",
1846 [HWCAP_S390_NR_VXRS] = "vx",
1847 [HWCAP_S390_NR_VXRS_BCD] = "vxd",
1848 [HWCAP_S390_NR_VXRS_EXT] = "vxe",
1849 [HWCAP_S390_NR_GS] = "gs",
1850 [HWCAP_S390_NR_VXRS_EXT2] = "vxe2",
1851 [HWCAP_S390_NR_VXRS_PDE] = "vxp",
1852 [HWCAP_S390_NR_SORT] = "sort",
1853 [HWCAP_S390_NR_DFLT] = "dflt",
1854 [HWCAP_S390_NR_NNPA] = "nnpa",
1855 [HWCAP_S390_NR_PCI_MIO] = "pcimio",
1856 [HWCAP_S390_NR_SIE] = "sie",
1859 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
1862 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1864 regs->psw.addr = infop->entry;
1865 regs->psw.mask = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
1866 PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_MASK_64 | \
1867 PSW_MASK_32;
1868 regs->gprs[15] = infop->start_stack;
1871 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1872 #define ELF_NREG 27
1873 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1875 enum {
1876 TARGET_REG_PSWM = 0,
1877 TARGET_REG_PSWA = 1,
1878 TARGET_REG_GPRS = 2,
1879 TARGET_REG_ARS = 18,
1880 TARGET_REG_ORIG_R2 = 26,
1883 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1884 const CPUS390XState *env)
1886 int i;
1887 uint32_t *aregs;
1889 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask);
1890 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr);
1891 for (i = 0; i < 16; i++) {
1892 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]);
1894 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]);
1895 for (i = 0; i < 16; i++) {
1896 aregs[i] = tswap32(env->aregs[i]);
1898 (*regs)[TARGET_REG_ORIG_R2] = 0;
1901 #define USE_ELF_CORE_DUMP
1902 #define ELF_EXEC_PAGESIZE 4096
1904 #define VDSO_HEADER "vdso.c.inc"
1906 #endif /* TARGET_S390X */
1908 #ifdef TARGET_RISCV
1910 #define ELF_ARCH EM_RISCV
1912 #ifdef TARGET_RISCV32
1913 #define ELF_CLASS ELFCLASS32
1914 #define VDSO_HEADER "vdso-32.c.inc"
1915 #else
1916 #define ELF_CLASS ELFCLASS64
1917 #define VDSO_HEADER "vdso-64.c.inc"
1918 #endif
1920 #define ELF_HWCAP get_elf_hwcap()
1922 static uint32_t get_elf_hwcap(void)
1924 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1925 RISCVCPU *cpu = RISCV_CPU(thread_cpu);
1926 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1927 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C')
1928 | MISA_BIT('V');
1930 return cpu->env.misa_ext & mask;
1931 #undef MISA_BIT
1934 static inline void init_thread(struct target_pt_regs *regs,
1935 struct image_info *infop)
1937 regs->sepc = infop->entry;
1938 regs->sp = infop->start_stack;
1941 #define ELF_EXEC_PAGESIZE 4096
1943 #endif /* TARGET_RISCV */
1945 #ifdef TARGET_HPPA
1947 #define ELF_CLASS ELFCLASS32
1948 #define ELF_ARCH EM_PARISC
1949 #define ELF_PLATFORM "PARISC"
1950 #define STACK_GROWS_DOWN 0
1951 #define STACK_ALIGNMENT 64
1953 #define VDSO_HEADER "vdso.c.inc"
1955 static inline void init_thread(struct target_pt_regs *regs,
1956 struct image_info *infop)
1958 regs->iaoq[0] = infop->entry;
1959 regs->iaoq[1] = infop->entry + 4;
1960 regs->gr[23] = 0;
1961 regs->gr[24] = infop->argv;
1962 regs->gr[25] = infop->argc;
1963 /* The top-of-stack contains a linkage buffer. */
1964 regs->gr[30] = infop->start_stack + 64;
1965 regs->gr[31] = infop->entry;
1968 #define LO_COMMPAGE 0
1970 static bool init_guest_commpage(void)
1972 void *want = g2h_untagged(LO_COMMPAGE);
1973 void *addr = mmap(want, qemu_host_page_size, PROT_NONE,
1974 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1976 if (addr == MAP_FAILED) {
1977 perror("Allocating guest commpage");
1978 exit(EXIT_FAILURE);
1980 if (addr != want) {
1981 return false;
1985 * On Linux, page zero is normally marked execute only + gateway.
1986 * Normal read or write is supposed to fail (thus PROT_NONE above),
1987 * but specific offsets have kernel code mapped to raise permissions
1988 * and implement syscalls. Here, simply mark the page executable.
1989 * Special case the entry points during translation (see do_page_zero).
1991 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1992 PAGE_EXEC | PAGE_VALID);
1993 return true;
1996 #endif /* TARGET_HPPA */
1998 #ifdef TARGET_XTENSA
2000 #define ELF_CLASS ELFCLASS32
2001 #define ELF_ARCH EM_XTENSA
2003 static inline void init_thread(struct target_pt_regs *regs,
2004 struct image_info *infop)
2006 regs->windowbase = 0;
2007 regs->windowstart = 1;
2008 regs->areg[1] = infop->start_stack;
2009 regs->pc = infop->entry;
2010 if (info_is_fdpic(infop)) {
2011 regs->areg[4] = infop->loadmap_addr;
2012 regs->areg[5] = infop->interpreter_loadmap_addr;
2013 if (infop->interpreter_loadmap_addr) {
2014 regs->areg[6] = infop->interpreter_pt_dynamic_addr;
2015 } else {
2016 regs->areg[6] = infop->pt_dynamic_addr;
2021 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
2022 #define ELF_NREG 128
2023 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
2025 enum {
2026 TARGET_REG_PC,
2027 TARGET_REG_PS,
2028 TARGET_REG_LBEG,
2029 TARGET_REG_LEND,
2030 TARGET_REG_LCOUNT,
2031 TARGET_REG_SAR,
2032 TARGET_REG_WINDOWSTART,
2033 TARGET_REG_WINDOWBASE,
2034 TARGET_REG_THREADPTR,
2035 TARGET_REG_AR0 = 64,
2038 static void elf_core_copy_regs(target_elf_gregset_t *regs,
2039 const CPUXtensaState *env)
2041 unsigned i;
2043 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
2044 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
2045 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
2046 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
2047 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
2048 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
2049 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
2050 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
2051 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
2052 xtensa_sync_phys_from_window((CPUXtensaState *)env);
2053 for (i = 0; i < env->config->nareg; ++i) {
2054 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
2058 #define USE_ELF_CORE_DUMP
2059 #define ELF_EXEC_PAGESIZE 4096
2061 #endif /* TARGET_XTENSA */
2063 #ifdef TARGET_HEXAGON
2065 #define ELF_CLASS ELFCLASS32
2066 #define ELF_ARCH EM_HEXAGON
2068 static inline void init_thread(struct target_pt_regs *regs,
2069 struct image_info *infop)
2071 regs->sepc = infop->entry;
2072 regs->sp = infop->start_stack;
2075 #endif /* TARGET_HEXAGON */
2077 #ifndef ELF_BASE_PLATFORM
2078 #define ELF_BASE_PLATFORM (NULL)
2079 #endif
2081 #ifndef ELF_PLATFORM
2082 #define ELF_PLATFORM (NULL)
2083 #endif
2085 #ifndef ELF_MACHINE
2086 #define ELF_MACHINE ELF_ARCH
2087 #endif
2089 #ifndef elf_check_arch
2090 #define elf_check_arch(x) ((x) == ELF_ARCH)
2091 #endif
2093 #ifndef elf_check_abi
2094 #define elf_check_abi(x) (1)
2095 #endif
2097 #ifndef ELF_HWCAP
2098 #define ELF_HWCAP 0
2099 #endif
2101 #ifndef STACK_GROWS_DOWN
2102 #define STACK_GROWS_DOWN 1
2103 #endif
2105 #ifndef STACK_ALIGNMENT
2106 #define STACK_ALIGNMENT 16
2107 #endif
2109 #ifdef TARGET_ABI32
2110 #undef ELF_CLASS
2111 #define ELF_CLASS ELFCLASS32
2112 #undef bswaptls
2113 #define bswaptls(ptr) bswap32s(ptr)
2114 #endif
2116 #ifndef EXSTACK_DEFAULT
2117 #define EXSTACK_DEFAULT false
2118 #endif
2120 #include "elf.h"
2122 /* We must delay the following stanzas until after "elf.h". */
2123 #if defined(TARGET_AARCH64)
2125 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2126 const uint32_t *data,
2127 struct image_info *info,
2128 Error **errp)
2130 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
2131 if (pr_datasz != sizeof(uint32_t)) {
2132 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
2133 return false;
2135 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
2136 info->note_flags = *data;
2138 return true;
2140 #define ARCH_USE_GNU_PROPERTY 1
2142 #else
2144 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2145 const uint32_t *data,
2146 struct image_info *info,
2147 Error **errp)
2149 g_assert_not_reached();
2151 #define ARCH_USE_GNU_PROPERTY 0
2153 #endif
2155 struct exec
2157 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
2158 unsigned int a_text; /* length of text, in bytes */
2159 unsigned int a_data; /* length of data, in bytes */
2160 unsigned int a_bss; /* length of uninitialized data area, in bytes */
2161 unsigned int a_syms; /* length of symbol table data in file, in bytes */
2162 unsigned int a_entry; /* start address */
2163 unsigned int a_trsize; /* length of relocation info for text, in bytes */
2164 unsigned int a_drsize; /* length of relocation info for data, in bytes */
2168 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
2169 #define OMAGIC 0407
2170 #define NMAGIC 0410
2171 #define ZMAGIC 0413
2172 #define QMAGIC 0314
2174 #define DLINFO_ITEMS 16
2176 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
2178 memcpy(to, from, n);
2181 #ifdef BSWAP_NEEDED
2182 static void bswap_ehdr(struct elfhdr *ehdr)
2184 bswap16s(&ehdr->e_type); /* Object file type */
2185 bswap16s(&ehdr->e_machine); /* Architecture */
2186 bswap32s(&ehdr->e_version); /* Object file version */
2187 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
2188 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
2189 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
2190 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
2191 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
2192 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
2193 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
2194 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
2195 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
2196 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
2199 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
2201 int i;
2202 for (i = 0; i < phnum; ++i, ++phdr) {
2203 bswap32s(&phdr->p_type); /* Segment type */
2204 bswap32s(&phdr->p_flags); /* Segment flags */
2205 bswaptls(&phdr->p_offset); /* Segment file offset */
2206 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
2207 bswaptls(&phdr->p_paddr); /* Segment physical address */
2208 bswaptls(&phdr->p_filesz); /* Segment size in file */
2209 bswaptls(&phdr->p_memsz); /* Segment size in memory */
2210 bswaptls(&phdr->p_align); /* Segment alignment */
2214 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
2216 int i;
2217 for (i = 0; i < shnum; ++i, ++shdr) {
2218 bswap32s(&shdr->sh_name);
2219 bswap32s(&shdr->sh_type);
2220 bswaptls(&shdr->sh_flags);
2221 bswaptls(&shdr->sh_addr);
2222 bswaptls(&shdr->sh_offset);
2223 bswaptls(&shdr->sh_size);
2224 bswap32s(&shdr->sh_link);
2225 bswap32s(&shdr->sh_info);
2226 bswaptls(&shdr->sh_addralign);
2227 bswaptls(&shdr->sh_entsize);
2231 static void bswap_sym(struct elf_sym *sym)
2233 bswap32s(&sym->st_name);
2234 bswaptls(&sym->st_value);
2235 bswaptls(&sym->st_size);
2236 bswap16s(&sym->st_shndx);
2239 #ifdef TARGET_MIPS
2240 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
2242 bswap16s(&abiflags->version);
2243 bswap32s(&abiflags->ases);
2244 bswap32s(&abiflags->isa_ext);
2245 bswap32s(&abiflags->flags1);
2246 bswap32s(&abiflags->flags2);
2248 #endif
2249 #else
2250 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
2251 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
2252 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
2253 static inline void bswap_sym(struct elf_sym *sym) { }
2254 #ifdef TARGET_MIPS
2255 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
2256 #endif
2257 #endif
2259 #ifdef USE_ELF_CORE_DUMP
2260 static int elf_core_dump(int, const CPUArchState *);
2261 #endif /* USE_ELF_CORE_DUMP */
2262 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
2263 abi_ulong load_bias);
2265 /* Verify the portions of EHDR within E_IDENT for the target.
2266 This can be performed before bswapping the entire header. */
2267 static bool elf_check_ident(struct elfhdr *ehdr)
2269 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
2270 && ehdr->e_ident[EI_MAG1] == ELFMAG1
2271 && ehdr->e_ident[EI_MAG2] == ELFMAG2
2272 && ehdr->e_ident[EI_MAG3] == ELFMAG3
2273 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
2274 && ehdr->e_ident[EI_DATA] == ELF_DATA
2275 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
2278 /* Verify the portions of EHDR outside of E_IDENT for the target.
2279 This has to wait until after bswapping the header. */
2280 static bool elf_check_ehdr(struct elfhdr *ehdr)
2282 return (elf_check_arch(ehdr->e_machine)
2283 && elf_check_abi(ehdr->e_flags)
2284 && ehdr->e_ehsize == sizeof(struct elfhdr)
2285 && ehdr->e_phentsize == sizeof(struct elf_phdr)
2286 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
2290 * 'copy_elf_strings()' copies argument/envelope strings from user
2291 * memory to free pages in kernel mem. These are in a format ready
2292 * to be put directly into the top of new user memory.
2295 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
2296 abi_ulong p, abi_ulong stack_limit)
2298 char *tmp;
2299 int len, i;
2300 abi_ulong top = p;
2302 if (!p) {
2303 return 0; /* bullet-proofing */
2306 if (STACK_GROWS_DOWN) {
2307 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
2308 for (i = argc - 1; i >= 0; --i) {
2309 tmp = argv[i];
2310 if (!tmp) {
2311 fprintf(stderr, "VFS: argc is wrong");
2312 exit(-1);
2314 len = strlen(tmp) + 1;
2315 tmp += len;
2317 if (len > (p - stack_limit)) {
2318 return 0;
2320 while (len) {
2321 int bytes_to_copy = (len > offset) ? offset : len;
2322 tmp -= bytes_to_copy;
2323 p -= bytes_to_copy;
2324 offset -= bytes_to_copy;
2325 len -= bytes_to_copy;
2327 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
2329 if (offset == 0) {
2330 memcpy_to_target(p, scratch, top - p);
2331 top = p;
2332 offset = TARGET_PAGE_SIZE;
2336 if (p != top) {
2337 memcpy_to_target(p, scratch + offset, top - p);
2339 } else {
2340 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
2341 for (i = 0; i < argc; ++i) {
2342 tmp = argv[i];
2343 if (!tmp) {
2344 fprintf(stderr, "VFS: argc is wrong");
2345 exit(-1);
2347 len = strlen(tmp) + 1;
2348 if (len > (stack_limit - p)) {
2349 return 0;
2351 while (len) {
2352 int bytes_to_copy = (len > remaining) ? remaining : len;
2354 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
2356 tmp += bytes_to_copy;
2357 remaining -= bytes_to_copy;
2358 p += bytes_to_copy;
2359 len -= bytes_to_copy;
2361 if (remaining == 0) {
2362 memcpy_to_target(top, scratch, p - top);
2363 top = p;
2364 remaining = TARGET_PAGE_SIZE;
2368 if (p != top) {
2369 memcpy_to_target(top, scratch, p - top);
2373 return p;
2376 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2377 * argument/environment space. Newer kernels (>2.6.33) allow more,
2378 * dependent on stack size, but guarantee at least 32 pages for
2379 * backwards compatibility.
2381 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2383 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
2384 struct image_info *info)
2386 abi_ulong size, error, guard;
2387 int prot;
2389 size = guest_stack_size;
2390 if (size < STACK_LOWER_LIMIT) {
2391 size = STACK_LOWER_LIMIT;
2394 if (STACK_GROWS_DOWN) {
2395 guard = TARGET_PAGE_SIZE;
2396 if (guard < qemu_real_host_page_size()) {
2397 guard = qemu_real_host_page_size();
2399 } else {
2400 /* no guard page for hppa target where stack grows upwards. */
2401 guard = 0;
2404 prot = PROT_READ | PROT_WRITE;
2405 if (info->exec_stack) {
2406 prot |= PROT_EXEC;
2408 error = target_mmap(0, size + guard, prot,
2409 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2410 if (error == -1) {
2411 perror("mmap stack");
2412 exit(-1);
2415 /* We reserve one extra page at the top of the stack as guard. */
2416 if (STACK_GROWS_DOWN) {
2417 target_mprotect(error, guard, PROT_NONE);
2418 info->stack_limit = error + guard;
2419 return info->stack_limit + size - sizeof(void *);
2420 } else {
2421 info->stack_limit = error + size;
2422 return error;
2427 * zero_bss:
2429 * Map and zero the bss. We need to explicitly zero any fractional pages
2430 * after the data section (i.e. bss). Return false on mapping failure.
2432 static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss,
2433 int prot, Error **errp)
2435 abi_ulong align_bss;
2437 /* We only expect writable bss; the code segment shouldn't need this. */
2438 if (!(prot & PROT_WRITE)) {
2439 error_setg(errp, "PT_LOAD with non-writable bss");
2440 return false;
2443 align_bss = TARGET_PAGE_ALIGN(start_bss);
2444 end_bss = TARGET_PAGE_ALIGN(end_bss);
2446 if (start_bss < align_bss) {
2447 int flags = page_get_flags(start_bss);
2449 if (!(flags & PAGE_BITS)) {
2451 * The whole address space of the executable was reserved
2452 * at the start, therefore all pages will be VALID.
2453 * But assuming there are no PROT_NONE PT_LOAD segments,
2454 * a PROT_NONE page means no data all bss, and we can
2455 * simply extend the new anon mapping back to the start
2456 * of the page of bss.
2458 align_bss -= TARGET_PAGE_SIZE;
2459 } else {
2461 * The start of the bss shares a page with something.
2462 * The only thing that we expect is the data section,
2463 * which would already be marked writable.
2464 * Overlapping the RX code segment seems malformed.
2466 if (!(flags & PAGE_WRITE)) {
2467 error_setg(errp, "PT_LOAD with bss overlapping "
2468 "non-writable page");
2469 return false;
2472 /* The page is already mapped and writable. */
2473 memset(g2h_untagged(start_bss), 0, align_bss - start_bss);
2477 if (align_bss < end_bss &&
2478 target_mmap(align_bss, end_bss - align_bss, prot,
2479 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
2480 error_setg_errno(errp, errno, "Error mapping bss");
2481 return false;
2483 return true;
2486 #if defined(TARGET_ARM)
2487 static int elf_is_fdpic(struct elfhdr *exec)
2489 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
2491 #elif defined(TARGET_XTENSA)
2492 static int elf_is_fdpic(struct elfhdr *exec)
2494 return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC;
2496 #else
2497 /* Default implementation, always false. */
2498 static int elf_is_fdpic(struct elfhdr *exec)
2500 return 0;
2502 #endif
2504 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
2506 uint16_t n;
2507 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
2509 /* elf32_fdpic_loadseg */
2510 n = info->nsegs;
2511 while (n--) {
2512 sp -= 12;
2513 put_user_u32(loadsegs[n].addr, sp+0);
2514 put_user_u32(loadsegs[n].p_vaddr, sp+4);
2515 put_user_u32(loadsegs[n].p_memsz, sp+8);
2518 /* elf32_fdpic_loadmap */
2519 sp -= 4;
2520 put_user_u16(0, sp+0); /* version */
2521 put_user_u16(info->nsegs, sp+2); /* nsegs */
2523 info->personality = PER_LINUX_FDPIC;
2524 info->loadmap_addr = sp;
2526 return sp;
2529 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
2530 struct elfhdr *exec,
2531 struct image_info *info,
2532 struct image_info *interp_info,
2533 struct image_info *vdso_info)
2535 abi_ulong sp;
2536 abi_ulong u_argc, u_argv, u_envp, u_auxv;
2537 int size;
2538 int i;
2539 abi_ulong u_rand_bytes;
2540 uint8_t k_rand_bytes[16];
2541 abi_ulong u_platform, u_base_platform;
2542 const char *k_platform, *k_base_platform;
2543 const int n = sizeof(elf_addr_t);
2545 sp = p;
2547 /* Needs to be before we load the env/argc/... */
2548 if (elf_is_fdpic(exec)) {
2549 /* Need 4 byte alignment for these structs */
2550 sp &= ~3;
2551 sp = loader_build_fdpic_loadmap(info, sp);
2552 info->other_info = interp_info;
2553 if (interp_info) {
2554 interp_info->other_info = info;
2555 sp = loader_build_fdpic_loadmap(interp_info, sp);
2556 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
2557 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
2558 } else {
2559 info->interpreter_loadmap_addr = 0;
2560 info->interpreter_pt_dynamic_addr = 0;
2564 u_base_platform = 0;
2565 k_base_platform = ELF_BASE_PLATFORM;
2566 if (k_base_platform) {
2567 size_t len = strlen(k_base_platform) + 1;
2568 if (STACK_GROWS_DOWN) {
2569 sp -= (len + n - 1) & ~(n - 1);
2570 u_base_platform = sp;
2571 /* FIXME - check return value of memcpy_to_target() for failure */
2572 memcpy_to_target(sp, k_base_platform, len);
2573 } else {
2574 memcpy_to_target(sp, k_base_platform, len);
2575 u_base_platform = sp;
2576 sp += len + 1;
2580 u_platform = 0;
2581 k_platform = ELF_PLATFORM;
2582 if (k_platform) {
2583 size_t len = strlen(k_platform) + 1;
2584 if (STACK_GROWS_DOWN) {
2585 sp -= (len + n - 1) & ~(n - 1);
2586 u_platform = sp;
2587 /* FIXME - check return value of memcpy_to_target() for failure */
2588 memcpy_to_target(sp, k_platform, len);
2589 } else {
2590 memcpy_to_target(sp, k_platform, len);
2591 u_platform = sp;
2592 sp += len + 1;
2596 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2597 * the argv and envp pointers.
2599 if (STACK_GROWS_DOWN) {
2600 sp = QEMU_ALIGN_DOWN(sp, 16);
2601 } else {
2602 sp = QEMU_ALIGN_UP(sp, 16);
2606 * Generate 16 random bytes for userspace PRNG seeding.
2608 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
2609 if (STACK_GROWS_DOWN) {
2610 sp -= 16;
2611 u_rand_bytes = sp;
2612 /* FIXME - check return value of memcpy_to_target() for failure */
2613 memcpy_to_target(sp, k_rand_bytes, 16);
2614 } else {
2615 memcpy_to_target(sp, k_rand_bytes, 16);
2616 u_rand_bytes = sp;
2617 sp += 16;
2620 size = (DLINFO_ITEMS + 1) * 2;
2621 if (k_base_platform) {
2622 size += 2;
2624 if (k_platform) {
2625 size += 2;
2627 if (vdso_info) {
2628 size += 2;
2630 #ifdef DLINFO_ARCH_ITEMS
2631 size += DLINFO_ARCH_ITEMS * 2;
2632 #endif
2633 #ifdef ELF_HWCAP2
2634 size += 2;
2635 #endif
2636 info->auxv_len = size * n;
2638 size += envc + argc + 2;
2639 size += 1; /* argc itself */
2640 size *= n;
2642 /* Allocate space and finalize stack alignment for entry now. */
2643 if (STACK_GROWS_DOWN) {
2644 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
2645 sp = u_argc;
2646 } else {
2647 u_argc = sp;
2648 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2651 u_argv = u_argc + n;
2652 u_envp = u_argv + (argc + 1) * n;
2653 u_auxv = u_envp + (envc + 1) * n;
2654 info->saved_auxv = u_auxv;
2655 info->argc = argc;
2656 info->envc = envc;
2657 info->argv = u_argv;
2658 info->envp = u_envp;
2660 /* This is correct because Linux defines
2661 * elf_addr_t as Elf32_Off / Elf64_Off
2663 #define NEW_AUX_ENT(id, val) do { \
2664 put_user_ual(id, u_auxv); u_auxv += n; \
2665 put_user_ual(val, u_auxv); u_auxv += n; \
2666 } while(0)
2668 #ifdef ARCH_DLINFO
2670 * ARCH_DLINFO must come first so platform specific code can enforce
2671 * special alignment requirements on the AUXV if necessary (eg. PPC).
2673 ARCH_DLINFO;
2674 #endif
2675 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2676 * on info->auxv_len will trigger.
2678 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2679 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2680 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2681 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2682 /* Target doesn't support host page size alignment */
2683 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2684 } else {
2685 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2686 qemu_host_page_size)));
2688 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2689 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2690 NEW_AUX_ENT(AT_ENTRY, info->entry);
2691 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2692 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2693 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2694 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2695 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2696 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2697 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2698 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2699 NEW_AUX_ENT(AT_EXECFN, info->file_string);
2701 #ifdef ELF_HWCAP2
2702 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2703 #endif
2705 if (u_base_platform) {
2706 NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform);
2708 if (u_platform) {
2709 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2711 if (vdso_info) {
2712 NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr);
2714 NEW_AUX_ENT (AT_NULL, 0);
2715 #undef NEW_AUX_ENT
2717 /* Check that our initial calculation of the auxv length matches how much
2718 * we actually put into it.
2720 assert(info->auxv_len == u_auxv - info->saved_auxv);
2722 put_user_ual(argc, u_argc);
2724 p = info->arg_strings;
2725 for (i = 0; i < argc; ++i) {
2726 put_user_ual(p, u_argv);
2727 u_argv += n;
2728 p += target_strlen(p) + 1;
2730 put_user_ual(0, u_argv);
2732 p = info->env_strings;
2733 for (i = 0; i < envc; ++i) {
2734 put_user_ual(p, u_envp);
2735 u_envp += n;
2736 p += target_strlen(p) + 1;
2738 put_user_ual(0, u_envp);
2740 return sp;
2743 #if defined(HI_COMMPAGE)
2744 #define LO_COMMPAGE -1
2745 #elif defined(LO_COMMPAGE)
2746 #define HI_COMMPAGE 0
2747 #else
2748 #define HI_COMMPAGE 0
2749 #define LO_COMMPAGE -1
2750 #ifndef INIT_GUEST_COMMPAGE
2751 #define init_guest_commpage() true
2752 #endif
2753 #endif
2756 * pgb_try_mmap:
2757 * @addr: host start address
2758 * @addr_last: host last address
2759 * @keep: do not unmap the probe region
2761 * Return 1 if [@addr, @addr_last] is not mapped in the host,
2762 * return 0 if it is not available to map, and -1 on mmap error.
2763 * If @keep, the region is left mapped on success, otherwise unmapped.
2765 static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep)
2767 size_t size = addr_last - addr + 1;
2768 void *p = mmap((void *)addr, size, PROT_NONE,
2769 MAP_ANONYMOUS | MAP_PRIVATE |
2770 MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0);
2771 int ret;
2773 if (p == MAP_FAILED) {
2774 return errno == EEXIST ? 0 : -1;
2776 ret = p == (void *)addr;
2777 if (!keep || !ret) {
2778 munmap(p, size);
2780 return ret;
2784 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk)
2785 * @addr: host address
2786 * @addr_last: host last address
2787 * @brk: host brk
2789 * Like pgb_try_mmap, but additionally reserve some memory following brk.
2791 static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last,
2792 uintptr_t brk, bool keep)
2794 uintptr_t brk_last = brk + 16 * MiB - 1;
2796 /* Do not map anything close to the host brk. */
2797 if (addr <= brk_last && brk <= addr_last) {
2798 return 0;
2800 return pgb_try_mmap(addr, addr_last, keep);
2804 * pgb_try_mmap_set:
2805 * @ga: set of guest addrs
2806 * @base: guest_base
2807 * @brk: host brk
2809 * Return true if all @ga can be mapped by the host at @base.
2810 * On success, retain the mapping at index 0 for reserved_va.
2813 typedef struct PGBAddrs {
2814 uintptr_t bounds[3][2]; /* start/last pairs */
2815 int nbounds;
2816 } PGBAddrs;
2818 static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk)
2820 for (int i = ga->nbounds - 1; i >= 0; --i) {
2821 if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base,
2822 ga->bounds[i][1] + base,
2823 brk, i == 0 && reserved_va) <= 0) {
2824 return false;
2827 return true;
2831 * pgb_addr_set:
2832 * @ga: output set of guest addrs
2833 * @guest_loaddr: guest image low address
2834 * @guest_loaddr: guest image high address
2835 * @identity: create for identity mapping
2837 * Fill in @ga with the image, COMMPAGE and NULL page.
2839 static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr,
2840 abi_ulong guest_hiaddr, bool try_identity)
2842 int n;
2845 * With a low commpage, or a guest mapped very low,
2846 * we may not be able to use the identity map.
2848 if (try_identity) {
2849 if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) {
2850 return false;
2852 if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) {
2853 return false;
2857 memset(ga, 0, sizeof(*ga));
2858 n = 0;
2860 if (reserved_va) {
2861 ga->bounds[n][0] = try_identity ? mmap_min_addr : 0;
2862 ga->bounds[n][1] = reserved_va;
2863 n++;
2864 /* LO_COMMPAGE and NULL handled by reserving from 0. */
2865 } else {
2866 /* Add any LO_COMMPAGE or NULL page. */
2867 if (LO_COMMPAGE != -1) {
2868 ga->bounds[n][0] = 0;
2869 ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1;
2870 n++;
2871 } else if (!try_identity) {
2872 ga->bounds[n][0] = 0;
2873 ga->bounds[n][1] = TARGET_PAGE_SIZE - 1;
2874 n++;
2877 /* Add the guest image for ET_EXEC. */
2878 if (guest_loaddr) {
2879 ga->bounds[n][0] = guest_loaddr;
2880 ga->bounds[n][1] = guest_hiaddr;
2881 n++;
2886 * Temporarily disable
2887 * "comparison is always false due to limited range of data type"
2888 * due to comparison between unsigned and (possible) 0.
2890 #pragma GCC diagnostic push
2891 #pragma GCC diagnostic ignored "-Wtype-limits"
2893 /* Add any HI_COMMPAGE not covered by reserved_va. */
2894 if (reserved_va < HI_COMMPAGE) {
2895 ga->bounds[n][0] = HI_COMMPAGE & qemu_host_page_mask;
2896 ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1;
2897 n++;
2900 #pragma GCC diagnostic pop
2902 ga->nbounds = n;
2903 return true;
2906 static void pgb_fail_in_use(const char *image_name)
2908 error_report("%s: requires virtual address space that is in use "
2909 "(omit the -B option or choose a different value)",
2910 image_name);
2911 exit(EXIT_FAILURE);
2914 static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr,
2915 uintptr_t guest_hiaddr, uintptr_t align)
2917 PGBAddrs ga;
2918 uintptr_t brk = (uintptr_t)sbrk(0);
2920 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2921 fprintf(stderr, "Requested guest base %p does not satisfy "
2922 "host minimum alignment (0x%" PRIxPTR ")\n",
2923 (void *)guest_base, align);
2924 exit(EXIT_FAILURE);
2927 if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base)
2928 || !pgb_try_mmap_set(&ga, guest_base, brk)) {
2929 pgb_fail_in_use(image_name);
2934 * pgb_find_fallback:
2936 * This is a fallback method for finding holes in the host address space
2937 * if we don't have the benefit of being able to access /proc/self/map.
2938 * It can potentially take a very long time as we can only dumbly iterate
2939 * up the host address space seeing if the allocation would work.
2941 static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align,
2942 uintptr_t brk)
2944 /* TODO: come up with a better estimate of how much to skip. */
2945 uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB;
2947 for (uintptr_t base = skip; ; base += skip) {
2948 base = ROUND_UP(base, align);
2949 if (pgb_try_mmap_set(ga, base, brk)) {
2950 return base;
2952 if (base >= -skip) {
2953 return -1;
2958 static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base,
2959 IntervalTreeRoot *root)
2961 for (int i = ga->nbounds - 1; i >= 0; --i) {
2962 uintptr_t s = base + ga->bounds[i][0];
2963 uintptr_t l = base + ga->bounds[i][1];
2964 IntervalTreeNode *n;
2966 if (l < s) {
2967 /* Wraparound. Skip to advance S to mmap_min_addr. */
2968 return mmap_min_addr - s;
2971 n = interval_tree_iter_first(root, s, l);
2972 if (n != NULL) {
2973 /* Conflict. Skip to advance S to LAST + 1. */
2974 return n->last - s + 1;
2977 return 0; /* success */
2980 static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root,
2981 uintptr_t align, uintptr_t brk)
2983 uintptr_t last = mmap_min_addr;
2984 uintptr_t base, skip;
2986 while (true) {
2987 base = ROUND_UP(last, align);
2988 if (base < last) {
2989 return -1;
2992 skip = pgb_try_itree(ga, base, root);
2993 if (skip == 0) {
2994 break;
2997 last = base + skip;
2998 if (last < base) {
2999 return -1;
3004 * We've chosen 'base' based on holes in the interval tree,
3005 * but we don't yet know if it is a valid host address.
3006 * Because it is the first matching hole, if the host addresses
3007 * are invalid we know there are no further matches.
3009 return pgb_try_mmap_set(ga, base, brk) ? base : -1;
3012 static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr,
3013 uintptr_t guest_hiaddr, uintptr_t align)
3015 IntervalTreeRoot *root;
3016 uintptr_t brk, ret;
3017 PGBAddrs ga;
3019 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
3021 /* Try the identity map first. */
3022 if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {
3023 brk = (uintptr_t)sbrk(0);
3024 if (pgb_try_mmap_set(&ga, 0, brk)) {
3025 guest_base = 0;
3026 return;
3031 * Rebuild the address set for non-identity map.
3032 * This differs in the mapping of the guest NULL page.
3034 pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false);
3036 root = read_self_maps();
3038 /* Read brk after we've read the maps, which will malloc. */
3039 brk = (uintptr_t)sbrk(0);
3041 if (!root) {
3042 ret = pgb_find_fallback(&ga, align, brk);
3043 } else {
3045 * Reserve the area close to the host brk.
3046 * This will be freed with the rest of the tree.
3048 IntervalTreeNode *b = g_new0(IntervalTreeNode, 1);
3049 b->start = brk;
3050 b->last = brk + 16 * MiB - 1;
3051 interval_tree_insert(b, root);
3053 ret = pgb_find_itree(&ga, root, align, brk);
3054 free_self_maps(root);
3057 if (ret == -1) {
3058 int w = TARGET_LONG_BITS / 4;
3060 error_report("%s: Unable to find a guest_base to satisfy all "
3061 "guest address mapping requirements", image_name);
3063 for (int i = 0; i < ga.nbounds; ++i) {
3064 error_printf(" %0*" PRIx64 "-%0*" PRIx64 "\n",
3065 w, (uint64_t)ga.bounds[i][0],
3066 w, (uint64_t)ga.bounds[i][1]);
3068 exit(EXIT_FAILURE);
3070 guest_base = ret;
3073 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
3074 abi_ulong guest_hiaddr)
3076 /* In order to use host shmat, we must be able to honor SHMLBA. */
3077 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
3079 /* Sanity check the guest binary. */
3080 if (reserved_va) {
3081 if (guest_hiaddr > reserved_va) {
3082 error_report("%s: requires more than reserved virtual "
3083 "address space (0x%" PRIx64 " > 0x%lx)",
3084 image_name, (uint64_t)guest_hiaddr, reserved_va);
3085 exit(EXIT_FAILURE);
3087 } else {
3088 if (guest_hiaddr != (uintptr_t)guest_hiaddr) {
3089 error_report("%s: requires more virtual address space "
3090 "than the host can provide (0x%" PRIx64 ")",
3091 image_name, (uint64_t)guest_hiaddr + 1);
3092 exit(EXIT_FAILURE);
3096 if (have_guest_base) {
3097 pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align);
3098 } else {
3099 pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align);
3102 /* Reserve and initialize the commpage. */
3103 if (!init_guest_commpage()) {
3104 /* We have already probed for the commpage being free. */
3105 g_assert_not_reached();
3108 assert(QEMU_IS_ALIGNED(guest_base, align));
3109 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
3110 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
3113 enum {
3114 /* The string "GNU\0" as a magic number. */
3115 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
3116 NOTE_DATA_SZ = 1 * KiB,
3117 NOTE_NAME_SZ = 4,
3118 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
3122 * Process a single gnu_property entry.
3123 * Return false for error.
3125 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
3126 struct image_info *info, bool have_prev_type,
3127 uint32_t *prev_type, Error **errp)
3129 uint32_t pr_type, pr_datasz, step;
3131 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
3132 goto error_data;
3134 datasz -= *off;
3135 data += *off / sizeof(uint32_t);
3137 if (datasz < 2 * sizeof(uint32_t)) {
3138 goto error_data;
3140 pr_type = data[0];
3141 pr_datasz = data[1];
3142 data += 2;
3143 datasz -= 2 * sizeof(uint32_t);
3144 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
3145 if (step > datasz) {
3146 goto error_data;
3149 /* Properties are supposed to be unique and sorted on pr_type. */
3150 if (have_prev_type && pr_type <= *prev_type) {
3151 if (pr_type == *prev_type) {
3152 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
3153 } else {
3154 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
3156 return false;
3158 *prev_type = pr_type;
3160 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
3161 return false;
3164 *off += 2 * sizeof(uint32_t) + step;
3165 return true;
3167 error_data:
3168 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
3169 return false;
3172 /* Process NT_GNU_PROPERTY_TYPE_0. */
3173 static bool parse_elf_properties(const ImageSource *src,
3174 struct image_info *info,
3175 const struct elf_phdr *phdr,
3176 Error **errp)
3178 union {
3179 struct elf_note nhdr;
3180 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
3181 } note;
3183 int n, off, datasz;
3184 bool have_prev_type;
3185 uint32_t prev_type;
3187 /* Unless the arch requires properties, ignore them. */
3188 if (!ARCH_USE_GNU_PROPERTY) {
3189 return true;
3192 /* If the properties are crazy large, that's too bad. */
3193 n = phdr->p_filesz;
3194 if (n > sizeof(note)) {
3195 error_setg(errp, "PT_GNU_PROPERTY too large");
3196 return false;
3198 if (n < sizeof(note.nhdr)) {
3199 error_setg(errp, "PT_GNU_PROPERTY too small");
3200 return false;
3203 if (!imgsrc_read(&note, phdr->p_offset, n, src, errp)) {
3204 return false;
3208 * The contents of a valid PT_GNU_PROPERTY is a sequence
3209 * of uint32_t -- swap them all now.
3211 #ifdef BSWAP_NEEDED
3212 for (int i = 0; i < n / 4; i++) {
3213 bswap32s(note.data + i);
3215 #endif
3218 * Note that nhdr is 3 words, and that the "name" described by namesz
3219 * immediately follows nhdr and is thus at the 4th word. Further, all
3220 * of the inputs to the kernel's round_up are multiples of 4.
3222 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
3223 note.nhdr.n_namesz != NOTE_NAME_SZ ||
3224 note.data[3] != GNU0_MAGIC) {
3225 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
3226 return false;
3228 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
3230 datasz = note.nhdr.n_descsz + off;
3231 if (datasz > n) {
3232 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
3233 return false;
3236 have_prev_type = false;
3237 prev_type = 0;
3238 while (1) {
3239 if (off == datasz) {
3240 return true; /* end, exit ok */
3242 if (!parse_elf_property(note.data, &off, datasz, info,
3243 have_prev_type, &prev_type, errp)) {
3244 return false;
3246 have_prev_type = true;
3251 * load_elf_image: Load an ELF image into the address space.
3252 * @image_name: the filename of the image, to use in error messages.
3253 * @src: the ImageSource from which to read.
3254 * @info: info collected from the loaded image.
3255 * @ehdr: the ELF header, not yet bswapped.
3256 * @pinterp_name: record any PT_INTERP string found.
3258 * On return: @info values will be filled in, as necessary or available.
3261 static void load_elf_image(const char *image_name, const ImageSource *src,
3262 struct image_info *info, struct elfhdr *ehdr,
3263 char **pinterp_name)
3265 g_autofree struct elf_phdr *phdr = NULL;
3266 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
3267 int i, prot_exec;
3268 Error *err = NULL;
3271 * First of all, some simple consistency checks.
3272 * Note that we rely on the bswapped ehdr staying in bprm_buf,
3273 * for later use by load_elf_binary and create_elf_tables.
3275 if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) {
3276 goto exit_errmsg;
3278 if (!elf_check_ident(ehdr)) {
3279 error_setg(&err, "Invalid ELF image for this architecture");
3280 goto exit_errmsg;
3282 bswap_ehdr(ehdr);
3283 if (!elf_check_ehdr(ehdr)) {
3284 error_setg(&err, "Invalid ELF image for this architecture");
3285 goto exit_errmsg;
3288 phdr = imgsrc_read_alloc(ehdr->e_phoff,
3289 ehdr->e_phnum * sizeof(struct elf_phdr),
3290 src, &err);
3291 if (phdr == NULL) {
3292 goto exit_errmsg;
3294 bswap_phdr(phdr, ehdr->e_phnum);
3296 info->nsegs = 0;
3297 info->pt_dynamic_addr = 0;
3299 mmap_lock();
3302 * Find the maximum size of the image and allocate an appropriate
3303 * amount of memory to handle that. Locate the interpreter, if any.
3305 loaddr = -1, hiaddr = 0;
3306 info->alignment = 0;
3307 info->exec_stack = EXSTACK_DEFAULT;
3308 for (i = 0; i < ehdr->e_phnum; ++i) {
3309 struct elf_phdr *eppnt = phdr + i;
3310 if (eppnt->p_type == PT_LOAD) {
3311 abi_ulong a = eppnt->p_vaddr & TARGET_PAGE_MASK;
3312 if (a < loaddr) {
3313 loaddr = a;
3315 a = eppnt->p_vaddr + eppnt->p_memsz - 1;
3316 if (a > hiaddr) {
3317 hiaddr = a;
3319 ++info->nsegs;
3320 info->alignment |= eppnt->p_align;
3321 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
3322 g_autofree char *interp_name = NULL;
3324 if (*pinterp_name) {
3325 error_setg(&err, "Multiple PT_INTERP entries");
3326 goto exit_errmsg;
3329 interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz,
3330 src, &err);
3331 if (interp_name == NULL) {
3332 goto exit_errmsg;
3334 if (interp_name[eppnt->p_filesz - 1] != 0) {
3335 error_setg(&err, "Invalid PT_INTERP entry");
3336 goto exit_errmsg;
3338 *pinterp_name = g_steal_pointer(&interp_name);
3339 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
3340 if (!parse_elf_properties(src, info, eppnt, &err)) {
3341 goto exit_errmsg;
3343 } else if (eppnt->p_type == PT_GNU_STACK) {
3344 info->exec_stack = eppnt->p_flags & PF_X;
3348 load_addr = loaddr;
3350 if (pinterp_name != NULL) {
3351 if (ehdr->e_type == ET_EXEC) {
3353 * Make sure that the low address does not conflict with
3354 * MMAP_MIN_ADDR or the QEMU application itself.
3356 probe_guest_base(image_name, loaddr, hiaddr);
3357 } else {
3358 abi_ulong align;
3361 * The binary is dynamic, but we still need to
3362 * select guest_base. In this case we pass a size.
3364 probe_guest_base(image_name, 0, hiaddr - loaddr);
3367 * Avoid collision with the loader by providing a different
3368 * default load address.
3370 load_addr += elf_et_dyn_base;
3373 * TODO: Better support for mmap alignment is desirable.
3374 * Since we do not have complete control over the guest
3375 * address space, we prefer the kernel to choose some address
3376 * rather than force the use of LOAD_ADDR via MAP_FIXED.
3377 * But without MAP_FIXED we cannot guarantee alignment,
3378 * only suggest it.
3380 align = pow2ceil(info->alignment);
3381 if (align) {
3382 load_addr &= -align;
3388 * Reserve address space for all of this.
3390 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get
3391 * exactly the address range that is required. Without reserved_va,
3392 * the guest address space is not isolated. We have attempted to avoid
3393 * conflict with the host program itself via probe_guest_base, but using
3394 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check.
3396 * Otherwise this is ET_DYN, and we are searching for a location
3397 * that can hold the memory space required. If the image is
3398 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should
3399 * honor that address if it happens to be free.
3401 * In both cases, we will overwrite pages in this range with mappings
3402 * from the executable.
3404 load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE,
3405 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
3406 (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0),
3407 -1, 0);
3408 if (load_addr == -1) {
3409 goto exit_mmap;
3411 load_bias = load_addr - loaddr;
3413 if (elf_is_fdpic(ehdr)) {
3414 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
3415 g_malloc(sizeof(*loadsegs) * info->nsegs);
3417 for (i = 0; i < ehdr->e_phnum; ++i) {
3418 switch (phdr[i].p_type) {
3419 case PT_DYNAMIC:
3420 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
3421 break;
3422 case PT_LOAD:
3423 loadsegs->addr = phdr[i].p_vaddr + load_bias;
3424 loadsegs->p_vaddr = phdr[i].p_vaddr;
3425 loadsegs->p_memsz = phdr[i].p_memsz;
3426 ++loadsegs;
3427 break;
3432 info->load_bias = load_bias;
3433 info->code_offset = load_bias;
3434 info->data_offset = load_bias;
3435 info->load_addr = load_addr;
3436 info->entry = ehdr->e_entry + load_bias;
3437 info->start_code = -1;
3438 info->end_code = 0;
3439 info->start_data = -1;
3440 info->end_data = 0;
3441 /* Usual start for brk is after all sections of the main executable. */
3442 info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
3443 info->elf_flags = ehdr->e_flags;
3445 prot_exec = PROT_EXEC;
3446 #ifdef TARGET_AARCH64
3448 * If the BTI feature is present, this indicates that the executable
3449 * pages of the startup binary should be mapped with PROT_BTI, so that
3450 * branch targets are enforced.
3452 * The startup binary is either the interpreter or the static executable.
3453 * The interpreter is responsible for all pages of a dynamic executable.
3455 * Elf notes are backward compatible to older cpus.
3456 * Do not enable BTI unless it is supported.
3458 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
3459 && (pinterp_name == NULL || *pinterp_name == 0)
3460 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
3461 prot_exec |= TARGET_PROT_BTI;
3463 #endif
3465 for (i = 0; i < ehdr->e_phnum; i++) {
3466 struct elf_phdr *eppnt = phdr + i;
3467 if (eppnt->p_type == PT_LOAD) {
3468 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
3469 int elf_prot = 0;
3471 if (eppnt->p_flags & PF_R) {
3472 elf_prot |= PROT_READ;
3474 if (eppnt->p_flags & PF_W) {
3475 elf_prot |= PROT_WRITE;
3477 if (eppnt->p_flags & PF_X) {
3478 elf_prot |= prot_exec;
3481 vaddr = load_bias + eppnt->p_vaddr;
3482 vaddr_po = vaddr & ~TARGET_PAGE_MASK;
3483 vaddr_ps = vaddr & TARGET_PAGE_MASK;
3485 vaddr_ef = vaddr + eppnt->p_filesz;
3486 vaddr_em = vaddr + eppnt->p_memsz;
3489 * Some segments may be completely empty, with a non-zero p_memsz
3490 * but no backing file segment.
3492 if (eppnt->p_filesz != 0) {
3493 error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
3494 elf_prot, MAP_PRIVATE | MAP_FIXED,
3495 src, eppnt->p_offset - vaddr_po);
3496 if (error == -1) {
3497 goto exit_mmap;
3501 /* If the load segment requests extra zeros (e.g. bss), map it. */
3502 if (vaddr_ef < vaddr_em &&
3503 !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) {
3504 goto exit_errmsg;
3507 /* Find the full program boundaries. */
3508 if (elf_prot & PROT_EXEC) {
3509 if (vaddr < info->start_code) {
3510 info->start_code = vaddr;
3512 if (vaddr_ef > info->end_code) {
3513 info->end_code = vaddr_ef;
3516 if (elf_prot & PROT_WRITE) {
3517 if (vaddr < info->start_data) {
3518 info->start_data = vaddr;
3520 if (vaddr_ef > info->end_data) {
3521 info->end_data = vaddr_ef;
3524 #ifdef TARGET_MIPS
3525 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
3526 Mips_elf_abiflags_v0 abiflags;
3528 if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags),
3529 src, &err)) {
3530 goto exit_errmsg;
3532 bswap_mips_abiflags(&abiflags);
3533 info->fp_abi = abiflags.fp_abi;
3534 #endif
3538 if (info->end_data == 0) {
3539 info->start_data = info->end_code;
3540 info->end_data = info->end_code;
3543 if (qemu_log_enabled()) {
3544 load_symbols(ehdr, src, load_bias);
3547 debuginfo_report_elf(image_name, src->fd, load_bias);
3549 mmap_unlock();
3551 close(src->fd);
3552 return;
3554 exit_mmap:
3555 error_setg_errno(&err, errno, "Error mapping file");
3556 goto exit_errmsg;
3557 exit_errmsg:
3558 error_reportf_err(err, "%s: ", image_name);
3559 exit(-1);
3562 static void load_elf_interp(const char *filename, struct image_info *info,
3563 char bprm_buf[BPRM_BUF_SIZE])
3565 struct elfhdr ehdr;
3566 ImageSource src;
3567 int fd, retval;
3568 Error *err = NULL;
3570 fd = open(path(filename), O_RDONLY);
3571 if (fd < 0) {
3572 error_setg_file_open(&err, errno, filename);
3573 error_report_err(err);
3574 exit(-1);
3577 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
3578 if (retval < 0) {
3579 error_setg_errno(&err, errno, "Error reading file header");
3580 error_reportf_err(err, "%s: ", filename);
3581 exit(-1);
3584 src.fd = fd;
3585 src.cache = bprm_buf;
3586 src.cache_size = retval;
3588 load_elf_image(filename, &src, info, &ehdr, NULL);
3591 #ifdef VDSO_HEADER
3592 #include VDSO_HEADER
3593 #define vdso_image_info() &vdso_image_info
3594 #else
3595 #define vdso_image_info() NULL
3596 #endif
3598 static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso)
3600 ImageSource src;
3601 struct elfhdr ehdr;
3602 abi_ulong load_bias, load_addr;
3604 src.fd = -1;
3605 src.cache = vdso->image;
3606 src.cache_size = vdso->image_size;
3608 load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL);
3609 load_addr = info->load_addr;
3610 load_bias = info->load_bias;
3613 * We need to relocate the VDSO image. The one built into the kernel
3614 * is built for a fixed address. The one built for QEMU is not, since
3615 * that requires close control of the guest address space.
3616 * We pre-processed the image to locate all of the addresses that need
3617 * to be updated.
3619 for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) {
3620 abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]);
3621 *addr = tswapal(tswapal(*addr) + load_bias);
3624 /* Install signal trampolines, if present. */
3625 if (vdso->sigreturn_ofs) {
3626 default_sigreturn = load_addr + vdso->sigreturn_ofs;
3628 if (vdso->rt_sigreturn_ofs) {
3629 default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs;
3632 /* Remove write from VDSO segment. */
3633 target_mprotect(info->start_data, info->end_data - info->start_data,
3634 PROT_READ | PROT_EXEC);
3637 static int symfind(const void *s0, const void *s1)
3639 struct elf_sym *sym = (struct elf_sym *)s1;
3640 __typeof(sym->st_value) addr = *(uint64_t *)s0;
3641 int result = 0;
3643 if (addr < sym->st_value) {
3644 result = -1;
3645 } else if (addr >= sym->st_value + sym->st_size) {
3646 result = 1;
3648 return result;
3651 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
3653 #if ELF_CLASS == ELFCLASS32
3654 struct elf_sym *syms = s->disas_symtab.elf32;
3655 #else
3656 struct elf_sym *syms = s->disas_symtab.elf64;
3657 #endif
3659 // binary search
3660 struct elf_sym *sym;
3662 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
3663 if (sym != NULL) {
3664 return s->disas_strtab + sym->st_name;
3667 return "";
3670 /* FIXME: This should use elf_ops.h */
3671 static int symcmp(const void *s0, const void *s1)
3673 struct elf_sym *sym0 = (struct elf_sym *)s0;
3674 struct elf_sym *sym1 = (struct elf_sym *)s1;
3675 return (sym0->st_value < sym1->st_value)
3676 ? -1
3677 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
3680 /* Best attempt to load symbols from this ELF object. */
3681 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
3682 abi_ulong load_bias)
3684 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
3685 g_autofree struct elf_shdr *shdr = NULL;
3686 char *strings = NULL;
3687 struct elf_sym *syms = NULL;
3688 struct elf_sym *new_syms;
3689 uint64_t segsz;
3691 shnum = hdr->e_shnum;
3692 shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr),
3693 src, NULL);
3694 if (shdr == NULL) {
3695 return;
3698 bswap_shdr(shdr, shnum);
3699 for (i = 0; i < shnum; ++i) {
3700 if (shdr[i].sh_type == SHT_SYMTAB) {
3701 sym_idx = i;
3702 str_idx = shdr[i].sh_link;
3703 goto found;
3707 /* There will be no symbol table if the file was stripped. */
3708 return;
3710 found:
3711 /* Now know where the strtab and symtab are. Snarf them. */
3713 segsz = shdr[str_idx].sh_size;
3714 strings = g_try_malloc(segsz);
3715 if (!strings) {
3716 goto give_up;
3718 if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) {
3719 goto give_up;
3722 segsz = shdr[sym_idx].sh_size;
3723 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
3725 * Implausibly large symbol table: give up rather than ploughing
3726 * on with the number of symbols calculation overflowing.
3728 goto give_up;
3730 nsyms = segsz / sizeof(struct elf_sym);
3731 syms = g_try_malloc(segsz);
3732 if (!syms) {
3733 goto give_up;
3735 if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) {
3736 goto give_up;
3739 for (i = 0; i < nsyms; ) {
3740 bswap_sym(syms + i);
3741 /* Throw away entries which we do not need. */
3742 if (syms[i].st_shndx == SHN_UNDEF
3743 || syms[i].st_shndx >= SHN_LORESERVE
3744 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3745 if (i < --nsyms) {
3746 syms[i] = syms[nsyms];
3748 } else {
3749 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3750 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3751 syms[i].st_value &= ~(target_ulong)1;
3752 #endif
3753 syms[i].st_value += load_bias;
3754 i++;
3758 /* No "useful" symbol. */
3759 if (nsyms == 0) {
3760 goto give_up;
3764 * Attempt to free the storage associated with the local symbols
3765 * that we threw away. Whether or not this has any effect on the
3766 * memory allocation depends on the malloc implementation and how
3767 * many symbols we managed to discard.
3769 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3770 if (new_syms == NULL) {
3771 goto give_up;
3773 syms = new_syms;
3775 qsort(syms, nsyms, sizeof(*syms), symcmp);
3778 struct syminfo *s = g_new(struct syminfo, 1);
3780 s->disas_strtab = strings;
3781 s->disas_num_syms = nsyms;
3782 #if ELF_CLASS == ELFCLASS32
3783 s->disas_symtab.elf32 = syms;
3784 #else
3785 s->disas_symtab.elf64 = syms;
3786 #endif
3787 s->lookup_symbol = lookup_symbolxx;
3788 s->next = syminfos;
3789 syminfos = s;
3791 return;
3793 give_up:
3794 g_free(strings);
3795 g_free(syms);
3798 uint32_t get_elf_eflags(int fd)
3800 struct elfhdr ehdr;
3801 off_t offset;
3802 int ret;
3804 /* Read ELF header */
3805 offset = lseek(fd, 0, SEEK_SET);
3806 if (offset == (off_t) -1) {
3807 return 0;
3809 ret = read(fd, &ehdr, sizeof(ehdr));
3810 if (ret < sizeof(ehdr)) {
3811 return 0;
3813 offset = lseek(fd, offset, SEEK_SET);
3814 if (offset == (off_t) -1) {
3815 return 0;
3818 /* Check ELF signature */
3819 if (!elf_check_ident(&ehdr)) {
3820 return 0;
3823 /* check header */
3824 bswap_ehdr(&ehdr);
3825 if (!elf_check_ehdr(&ehdr)) {
3826 return 0;
3829 /* return architecture id */
3830 return ehdr.e_flags;
3833 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3836 * We need a copy of the elf header for passing to create_elf_tables.
3837 * We will have overwritten the original when we re-use bprm->buf
3838 * while loading the interpreter. Allocate the storage for this now
3839 * and let elf_load_image do any swapping that may be required.
3841 struct elfhdr ehdr;
3842 struct image_info interp_info, vdso_info;
3843 char *elf_interpreter = NULL;
3844 char *scratch;
3846 memset(&interp_info, 0, sizeof(interp_info));
3847 #ifdef TARGET_MIPS
3848 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3849 #endif
3851 load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter);
3853 /* Do this so that we can load the interpreter, if need be. We will
3854 change some of these later */
3855 bprm->p = setup_arg_pages(bprm, info);
3857 scratch = g_new0(char, TARGET_PAGE_SIZE);
3858 if (STACK_GROWS_DOWN) {
3859 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3860 bprm->p, info->stack_limit);
3861 info->file_string = bprm->p;
3862 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3863 bprm->p, info->stack_limit);
3864 info->env_strings = bprm->p;
3865 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3866 bprm->p, info->stack_limit);
3867 info->arg_strings = bprm->p;
3868 } else {
3869 info->arg_strings = bprm->p;
3870 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3871 bprm->p, info->stack_limit);
3872 info->env_strings = bprm->p;
3873 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3874 bprm->p, info->stack_limit);
3875 info->file_string = bprm->p;
3876 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3877 bprm->p, info->stack_limit);
3880 g_free(scratch);
3882 if (!bprm->p) {
3883 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3884 exit(-1);
3887 if (elf_interpreter) {
3888 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3891 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky
3892 * with the mappings the interpreter can be loaded above but
3893 * near the main executable, which can leave very little room
3894 * for the heap.
3895 * If the current brk has less than 16MB, use the end of the
3896 * interpreter.
3898 if (interp_info.brk > info->brk &&
3899 interp_info.load_bias - info->brk < 16 * MiB) {
3900 info->brk = interp_info.brk;
3903 /* If the program interpreter is one of these two, then assume
3904 an iBCS2 image. Otherwise assume a native linux image. */
3906 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3907 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3908 info->personality = PER_SVR4;
3910 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3911 and some applications "depend" upon this behavior. Since
3912 we do not have the power to recompile these, we emulate
3913 the SVr4 behavior. Sigh. */
3914 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
3915 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3917 #ifdef TARGET_MIPS
3918 info->interp_fp_abi = interp_info.fp_abi;
3919 #endif
3923 * Load a vdso if available, which will amongst other things contain the
3924 * signal trampolines. Otherwise, allocate a separate page for them.
3926 const VdsoImageInfo *vdso = vdso_image_info();
3927 if (vdso) {
3928 load_elf_vdso(&vdso_info, vdso);
3929 info->vdso = vdso_info.load_bias;
3930 } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) {
3931 abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE,
3932 PROT_READ | PROT_WRITE,
3933 MAP_PRIVATE | MAP_ANON, -1, 0);
3934 if (tramp_page == -1) {
3935 return -errno;
3938 setup_sigtramp(tramp_page);
3939 target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC);
3942 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info,
3943 elf_interpreter ? &interp_info : NULL,
3944 vdso ? &vdso_info : NULL);
3945 info->start_stack = bprm->p;
3947 /* If we have an interpreter, set that as the program's entry point.
3948 Copy the load_bias as well, to help PPC64 interpret the entry
3949 point as a function descriptor. Do this after creating elf tables
3950 so that we copy the original program entry point into the AUXV. */
3951 if (elf_interpreter) {
3952 info->load_bias = interp_info.load_bias;
3953 info->entry = interp_info.entry;
3954 g_free(elf_interpreter);
3957 #ifdef USE_ELF_CORE_DUMP
3958 bprm->core_dump = &elf_core_dump;
3959 #endif
3961 return 0;
3964 #ifdef USE_ELF_CORE_DUMP
3966 * Definitions to generate Intel SVR4-like core files.
3967 * These mostly have the same names as the SVR4 types with "target_elf_"
3968 * tacked on the front to prevent clashes with linux definitions,
3969 * and the typedef forms have been avoided. This is mostly like
3970 * the SVR4 structure, but more Linuxy, with things that Linux does
3971 * not support and which gdb doesn't really use excluded.
3973 * Fields we don't dump (their contents is zero) in linux-user qemu
3974 * are marked with XXX.
3976 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3978 * Porting ELF coredump for target is (quite) simple process. First you
3979 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3980 * the target resides):
3982 * #define USE_ELF_CORE_DUMP
3984 * Next you define type of register set used for dumping. ELF specification
3985 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3987 * typedef <target_regtype> target_elf_greg_t;
3988 * #define ELF_NREG <number of registers>
3989 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3991 * Last step is to implement target specific function that copies registers
3992 * from given cpu into just specified register set. Prototype is:
3994 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3995 * const CPUArchState *env);
3997 * Parameters:
3998 * regs - copy register values into here (allocated and zeroed by caller)
3999 * env - copy registers from here
4001 * Example for ARM target is provided in this file.
4004 /* An ELF note in memory */
4005 struct memelfnote {
4006 const char *name;
4007 size_t namesz;
4008 size_t namesz_rounded;
4009 int type;
4010 size_t datasz;
4011 size_t datasz_rounded;
4012 void *data;
4013 size_t notesz;
4016 struct target_elf_siginfo {
4017 abi_int si_signo; /* signal number */
4018 abi_int si_code; /* extra code */
4019 abi_int si_errno; /* errno */
4022 struct target_elf_prstatus {
4023 struct target_elf_siginfo pr_info; /* Info associated with signal */
4024 abi_short pr_cursig; /* Current signal */
4025 abi_ulong pr_sigpend; /* XXX */
4026 abi_ulong pr_sighold; /* XXX */
4027 target_pid_t pr_pid;
4028 target_pid_t pr_ppid;
4029 target_pid_t pr_pgrp;
4030 target_pid_t pr_sid;
4031 struct target_timeval pr_utime; /* XXX User time */
4032 struct target_timeval pr_stime; /* XXX System time */
4033 struct target_timeval pr_cutime; /* XXX Cumulative user time */
4034 struct target_timeval pr_cstime; /* XXX Cumulative system time */
4035 target_elf_gregset_t pr_reg; /* GP registers */
4036 abi_int pr_fpvalid; /* XXX */
4039 #define ELF_PRARGSZ (80) /* Number of chars for args */
4041 struct target_elf_prpsinfo {
4042 char pr_state; /* numeric process state */
4043 char pr_sname; /* char for pr_state */
4044 char pr_zomb; /* zombie */
4045 char pr_nice; /* nice val */
4046 abi_ulong pr_flag; /* flags */
4047 target_uid_t pr_uid;
4048 target_gid_t pr_gid;
4049 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
4050 /* Lots missing */
4051 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
4052 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
4055 /* Here is the structure in which status of each thread is captured. */
4056 struct elf_thread_status {
4057 QTAILQ_ENTRY(elf_thread_status) ets_link;
4058 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
4059 #if 0
4060 elf_fpregset_t fpu; /* NT_PRFPREG */
4061 struct task_struct *thread;
4062 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
4063 #endif
4064 struct memelfnote notes[1];
4065 int num_notes;
4068 struct elf_note_info {
4069 struct memelfnote *notes;
4070 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
4071 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
4073 QTAILQ_HEAD(, elf_thread_status) thread_list;
4074 #if 0
4076 * Current version of ELF coredump doesn't support
4077 * dumping fp regs etc.
4079 elf_fpregset_t *fpu;
4080 elf_fpxregset_t *xfpu;
4081 int thread_status_size;
4082 #endif
4083 int notes_size;
4084 int numnote;
4087 struct vm_area_struct {
4088 target_ulong vma_start; /* start vaddr of memory region */
4089 target_ulong vma_end; /* end vaddr of memory region */
4090 abi_ulong vma_flags; /* protection etc. flags for the region */
4091 QTAILQ_ENTRY(vm_area_struct) vma_link;
4094 struct mm_struct {
4095 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
4096 int mm_count; /* number of mappings */
4099 static struct mm_struct *vma_init(void);
4100 static void vma_delete(struct mm_struct *);
4101 static int vma_add_mapping(struct mm_struct *, target_ulong,
4102 target_ulong, abi_ulong);
4103 static int vma_get_mapping_count(const struct mm_struct *);
4104 static struct vm_area_struct *vma_first(const struct mm_struct *);
4105 static struct vm_area_struct *vma_next(struct vm_area_struct *);
4106 static abi_ulong vma_dump_size(const struct vm_area_struct *);
4107 static int vma_walker(void *priv, target_ulong start, target_ulong end,
4108 unsigned long flags);
4110 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
4111 static void fill_note(struct memelfnote *, const char *, int,
4112 unsigned int, void *);
4113 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
4114 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
4115 static void fill_auxv_note(struct memelfnote *, const TaskState *);
4116 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
4117 static size_t note_size(const struct memelfnote *);
4118 static void free_note_info(struct elf_note_info *);
4119 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
4120 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
4122 static int dump_write(int, const void *, size_t);
4123 static int write_note(struct memelfnote *, int);
4124 static int write_note_info(struct elf_note_info *, int);
4126 #ifdef BSWAP_NEEDED
4127 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
4129 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
4130 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
4131 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
4132 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
4133 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
4134 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
4135 prstatus->pr_pid = tswap32(prstatus->pr_pid);
4136 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
4137 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
4138 prstatus->pr_sid = tswap32(prstatus->pr_sid);
4139 /* cpu times are not filled, so we skip them */
4140 /* regs should be in correct format already */
4141 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
4144 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
4146 psinfo->pr_flag = tswapal(psinfo->pr_flag);
4147 psinfo->pr_uid = tswap16(psinfo->pr_uid);
4148 psinfo->pr_gid = tswap16(psinfo->pr_gid);
4149 psinfo->pr_pid = tswap32(psinfo->pr_pid);
4150 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
4151 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
4152 psinfo->pr_sid = tswap32(psinfo->pr_sid);
4155 static void bswap_note(struct elf_note *en)
4157 bswap32s(&en->n_namesz);
4158 bswap32s(&en->n_descsz);
4159 bswap32s(&en->n_type);
4161 #else
4162 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
4163 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
4164 static inline void bswap_note(struct elf_note *en) { }
4165 #endif /* BSWAP_NEEDED */
4168 * Minimal support for linux memory regions. These are needed
4169 * when we are finding out what memory exactly belongs to
4170 * emulated process. No locks needed here, as long as
4171 * thread that received the signal is stopped.
4174 static struct mm_struct *vma_init(void)
4176 struct mm_struct *mm;
4178 if ((mm = g_malloc(sizeof (*mm))) == NULL)
4179 return (NULL);
4181 mm->mm_count = 0;
4182 QTAILQ_INIT(&mm->mm_mmap);
4184 return (mm);
4187 static void vma_delete(struct mm_struct *mm)
4189 struct vm_area_struct *vma;
4191 while ((vma = vma_first(mm)) != NULL) {
4192 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
4193 g_free(vma);
4195 g_free(mm);
4198 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
4199 target_ulong end, abi_ulong flags)
4201 struct vm_area_struct *vma;
4203 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
4204 return (-1);
4206 vma->vma_start = start;
4207 vma->vma_end = end;
4208 vma->vma_flags = flags;
4210 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
4211 mm->mm_count++;
4213 return (0);
4216 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
4218 return (QTAILQ_FIRST(&mm->mm_mmap));
4221 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
4223 return (QTAILQ_NEXT(vma, vma_link));
4226 static int vma_get_mapping_count(const struct mm_struct *mm)
4228 return (mm->mm_count);
4232 * Calculate file (dump) size of given memory region.
4234 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
4236 /* if we cannot even read the first page, skip it */
4237 if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
4238 return (0);
4241 * Usually we don't dump executable pages as they contain
4242 * non-writable code that debugger can read directly from
4243 * target library etc. However, thread stacks are marked
4244 * also executable so we read in first page of given region
4245 * and check whether it contains elf header. If there is
4246 * no elf header, we dump it.
4248 if (vma->vma_flags & PROT_EXEC) {
4249 char page[TARGET_PAGE_SIZE];
4251 if (copy_from_user(page, vma->vma_start, sizeof (page))) {
4252 return 0;
4254 if ((page[EI_MAG0] == ELFMAG0) &&
4255 (page[EI_MAG1] == ELFMAG1) &&
4256 (page[EI_MAG2] == ELFMAG2) &&
4257 (page[EI_MAG3] == ELFMAG3)) {
4259 * Mappings are possibly from ELF binary. Don't dump
4260 * them.
4262 return (0);
4266 return (vma->vma_end - vma->vma_start);
4269 static int vma_walker(void *priv, target_ulong start, target_ulong end,
4270 unsigned long flags)
4272 struct mm_struct *mm = (struct mm_struct *)priv;
4274 vma_add_mapping(mm, start, end, flags);
4275 return (0);
4278 static void fill_note(struct memelfnote *note, const char *name, int type,
4279 unsigned int sz, void *data)
4281 unsigned int namesz;
4283 namesz = strlen(name) + 1;
4284 note->name = name;
4285 note->namesz = namesz;
4286 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
4287 note->type = type;
4288 note->datasz = sz;
4289 note->datasz_rounded = roundup(sz, sizeof (int32_t));
4291 note->data = data;
4294 * We calculate rounded up note size here as specified by
4295 * ELF document.
4297 note->notesz = sizeof (struct elf_note) +
4298 note->namesz_rounded + note->datasz_rounded;
4301 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
4302 uint32_t flags)
4304 (void) memset(elf, 0, sizeof(*elf));
4306 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
4307 elf->e_ident[EI_CLASS] = ELF_CLASS;
4308 elf->e_ident[EI_DATA] = ELF_DATA;
4309 elf->e_ident[EI_VERSION] = EV_CURRENT;
4310 elf->e_ident[EI_OSABI] = ELF_OSABI;
4312 elf->e_type = ET_CORE;
4313 elf->e_machine = machine;
4314 elf->e_version = EV_CURRENT;
4315 elf->e_phoff = sizeof(struct elfhdr);
4316 elf->e_flags = flags;
4317 elf->e_ehsize = sizeof(struct elfhdr);
4318 elf->e_phentsize = sizeof(struct elf_phdr);
4319 elf->e_phnum = segs;
4321 bswap_ehdr(elf);
4324 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
4326 phdr->p_type = PT_NOTE;
4327 phdr->p_offset = offset;
4328 phdr->p_vaddr = 0;
4329 phdr->p_paddr = 0;
4330 phdr->p_filesz = sz;
4331 phdr->p_memsz = 0;
4332 phdr->p_flags = 0;
4333 phdr->p_align = 0;
4335 bswap_phdr(phdr, 1);
4338 static size_t note_size(const struct memelfnote *note)
4340 return (note->notesz);
4343 static void fill_prstatus(struct target_elf_prstatus *prstatus,
4344 const TaskState *ts, int signr)
4346 (void) memset(prstatus, 0, sizeof (*prstatus));
4347 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
4348 prstatus->pr_pid = ts->ts_tid;
4349 prstatus->pr_ppid = getppid();
4350 prstatus->pr_pgrp = getpgrp();
4351 prstatus->pr_sid = getsid(0);
4353 bswap_prstatus(prstatus);
4356 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
4358 char *base_filename;
4359 unsigned int i, len;
4361 (void) memset(psinfo, 0, sizeof (*psinfo));
4363 len = ts->info->env_strings - ts->info->arg_strings;
4364 if (len >= ELF_PRARGSZ)
4365 len = ELF_PRARGSZ - 1;
4366 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) {
4367 return -EFAULT;
4369 for (i = 0; i < len; i++)
4370 if (psinfo->pr_psargs[i] == 0)
4371 psinfo->pr_psargs[i] = ' ';
4372 psinfo->pr_psargs[len] = 0;
4374 psinfo->pr_pid = getpid();
4375 psinfo->pr_ppid = getppid();
4376 psinfo->pr_pgrp = getpgrp();
4377 psinfo->pr_sid = getsid(0);
4378 psinfo->pr_uid = getuid();
4379 psinfo->pr_gid = getgid();
4381 base_filename = g_path_get_basename(ts->bprm->filename);
4383 * Using strncpy here is fine: at max-length,
4384 * this field is not NUL-terminated.
4386 (void) strncpy(psinfo->pr_fname, base_filename,
4387 sizeof(psinfo->pr_fname));
4389 g_free(base_filename);
4390 bswap_psinfo(psinfo);
4391 return (0);
4394 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
4396 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
4397 elf_addr_t orig_auxv = auxv;
4398 void *ptr;
4399 int len = ts->info->auxv_len;
4402 * Auxiliary vector is stored in target process stack. It contains
4403 * {type, value} pairs that we need to dump into note. This is not
4404 * strictly necessary but we do it here for sake of completeness.
4407 /* read in whole auxv vector and copy it to memelfnote */
4408 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
4409 if (ptr != NULL) {
4410 fill_note(note, "CORE", NT_AUXV, len, ptr);
4411 unlock_user(ptr, auxv, len);
4416 * Constructs name of coredump file. We have following convention
4417 * for the name:
4418 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4420 * Returns the filename
4422 static char *core_dump_filename(const TaskState *ts)
4424 g_autoptr(GDateTime) now = g_date_time_new_now_local();
4425 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
4426 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
4428 return g_strdup_printf("qemu_%s_%s_%d.core",
4429 base_filename, nowstr, (int)getpid());
4432 static int dump_write(int fd, const void *ptr, size_t size)
4434 const char *bufp = (const char *)ptr;
4435 ssize_t bytes_written, bytes_left;
4436 struct rlimit dumpsize;
4437 off_t pos;
4439 bytes_written = 0;
4440 getrlimit(RLIMIT_CORE, &dumpsize);
4441 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
4442 if (errno == ESPIPE) { /* not a seekable stream */
4443 bytes_left = size;
4444 } else {
4445 return pos;
4447 } else {
4448 if (dumpsize.rlim_cur <= pos) {
4449 return -1;
4450 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
4451 bytes_left = size;
4452 } else {
4453 size_t limit_left=dumpsize.rlim_cur - pos;
4454 bytes_left = limit_left >= size ? size : limit_left ;
4459 * In normal conditions, single write(2) should do but
4460 * in case of socket etc. this mechanism is more portable.
4462 do {
4463 bytes_written = write(fd, bufp, bytes_left);
4464 if (bytes_written < 0) {
4465 if (errno == EINTR)
4466 continue;
4467 return (-1);
4468 } else if (bytes_written == 0) { /* eof */
4469 return (-1);
4471 bufp += bytes_written;
4472 bytes_left -= bytes_written;
4473 } while (bytes_left > 0);
4475 return (0);
4478 static int write_note(struct memelfnote *men, int fd)
4480 struct elf_note en;
4482 en.n_namesz = men->namesz;
4483 en.n_type = men->type;
4484 en.n_descsz = men->datasz;
4486 bswap_note(&en);
4488 if (dump_write(fd, &en, sizeof(en)) != 0)
4489 return (-1);
4490 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
4491 return (-1);
4492 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
4493 return (-1);
4495 return (0);
4498 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
4500 CPUState *cpu = env_cpu((CPUArchState *)env);
4501 TaskState *ts = (TaskState *)cpu->opaque;
4502 struct elf_thread_status *ets;
4504 ets = g_malloc0(sizeof (*ets));
4505 ets->num_notes = 1; /* only prstatus is dumped */
4506 fill_prstatus(&ets->prstatus, ts, 0);
4507 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
4508 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
4509 &ets->prstatus);
4511 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
4513 info->notes_size += note_size(&ets->notes[0]);
4516 static void init_note_info(struct elf_note_info *info)
4518 /* Initialize the elf_note_info structure so that it is at
4519 * least safe to call free_note_info() on it. Must be
4520 * called before calling fill_note_info().
4522 memset(info, 0, sizeof (*info));
4523 QTAILQ_INIT(&info->thread_list);
4526 static int fill_note_info(struct elf_note_info *info,
4527 long signr, const CPUArchState *env)
4529 #define NUMNOTES 3
4530 CPUState *cpu = env_cpu((CPUArchState *)env);
4531 TaskState *ts = (TaskState *)cpu->opaque;
4532 int i;
4534 info->notes = g_new0(struct memelfnote, NUMNOTES);
4535 if (info->notes == NULL)
4536 return (-ENOMEM);
4537 info->prstatus = g_malloc0(sizeof (*info->prstatus));
4538 if (info->prstatus == NULL)
4539 return (-ENOMEM);
4540 info->psinfo = g_malloc0(sizeof (*info->psinfo));
4541 if (info->prstatus == NULL)
4542 return (-ENOMEM);
4545 * First fill in status (and registers) of current thread
4546 * including process info & aux vector.
4548 fill_prstatus(info->prstatus, ts, signr);
4549 elf_core_copy_regs(&info->prstatus->pr_reg, env);
4550 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
4551 sizeof (*info->prstatus), info->prstatus);
4552 fill_psinfo(info->psinfo, ts);
4553 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
4554 sizeof (*info->psinfo), info->psinfo);
4555 fill_auxv_note(&info->notes[2], ts);
4556 info->numnote = 3;
4558 info->notes_size = 0;
4559 for (i = 0; i < info->numnote; i++)
4560 info->notes_size += note_size(&info->notes[i]);
4562 /* read and fill status of all threads */
4563 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock) {
4564 CPU_FOREACH(cpu) {
4565 if (cpu == thread_cpu) {
4566 continue;
4568 fill_thread_info(info, cpu_env(cpu));
4572 return (0);
4575 static void free_note_info(struct elf_note_info *info)
4577 struct elf_thread_status *ets;
4579 while (!QTAILQ_EMPTY(&info->thread_list)) {
4580 ets = QTAILQ_FIRST(&info->thread_list);
4581 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
4582 g_free(ets);
4585 g_free(info->prstatus);
4586 g_free(info->psinfo);
4587 g_free(info->notes);
4590 static int write_note_info(struct elf_note_info *info, int fd)
4592 struct elf_thread_status *ets;
4593 int i, error = 0;
4595 /* write prstatus, psinfo and auxv for current thread */
4596 for (i = 0; i < info->numnote; i++)
4597 if ((error = write_note(&info->notes[i], fd)) != 0)
4598 return (error);
4600 /* write prstatus for each thread */
4601 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
4602 if ((error = write_note(&ets->notes[0], fd)) != 0)
4603 return (error);
4606 return (0);
4610 * Write out ELF coredump.
4612 * See documentation of ELF object file format in:
4613 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4615 * Coredump format in linux is following:
4617 * 0 +----------------------+ \
4618 * | ELF header | ET_CORE |
4619 * +----------------------+ |
4620 * | ELF program headers | |--- headers
4621 * | - NOTE section | |
4622 * | - PT_LOAD sections | |
4623 * +----------------------+ /
4624 * | NOTEs: |
4625 * | - NT_PRSTATUS |
4626 * | - NT_PRSINFO |
4627 * | - NT_AUXV |
4628 * +----------------------+ <-- aligned to target page
4629 * | Process memory dump |
4630 * : :
4631 * . .
4632 * : :
4633 * | |
4634 * +----------------------+
4636 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4637 * NT_PRSINFO -> struct elf_prpsinfo
4638 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4640 * Format follows System V format as close as possible. Current
4641 * version limitations are as follows:
4642 * - no floating point registers are dumped
4644 * Function returns 0 in case of success, negative errno otherwise.
4646 * TODO: make this work also during runtime: it should be
4647 * possible to force coredump from running process and then
4648 * continue processing. For example qemu could set up SIGUSR2
4649 * handler (provided that target process haven't registered
4650 * handler for that) that does the dump when signal is received.
4652 static int elf_core_dump(int signr, const CPUArchState *env)
4654 const CPUState *cpu = env_cpu((CPUArchState *)env);
4655 const TaskState *ts = (const TaskState *)cpu->opaque;
4656 struct vm_area_struct *vma = NULL;
4657 g_autofree char *corefile = NULL;
4658 struct elf_note_info info;
4659 struct elfhdr elf;
4660 struct elf_phdr phdr;
4661 struct rlimit dumpsize;
4662 struct mm_struct *mm = NULL;
4663 off_t offset = 0, data_offset = 0;
4664 int segs = 0;
4665 int fd = -1;
4667 init_note_info(&info);
4669 errno = 0;
4670 getrlimit(RLIMIT_CORE, &dumpsize);
4671 if (dumpsize.rlim_cur == 0)
4672 return 0;
4674 corefile = core_dump_filename(ts);
4676 if ((fd = open(corefile, O_WRONLY | O_CREAT,
4677 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
4678 return (-errno);
4681 * Walk through target process memory mappings and
4682 * set up structure containing this information. After
4683 * this point vma_xxx functions can be used.
4685 if ((mm = vma_init()) == NULL)
4686 goto out;
4688 walk_memory_regions(mm, vma_walker);
4689 segs = vma_get_mapping_count(mm);
4692 * Construct valid coredump ELF header. We also
4693 * add one more segment for notes.
4695 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
4696 if (dump_write(fd, &elf, sizeof (elf)) != 0)
4697 goto out;
4699 /* fill in the in-memory version of notes */
4700 if (fill_note_info(&info, signr, env) < 0)
4701 goto out;
4703 offset += sizeof (elf); /* elf header */
4704 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
4706 /* write out notes program header */
4707 fill_elf_note_phdr(&phdr, info.notes_size, offset);
4709 offset += info.notes_size;
4710 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
4711 goto out;
4714 * ELF specification wants data to start at page boundary so
4715 * we align it here.
4717 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
4720 * Write program headers for memory regions mapped in
4721 * the target process.
4723 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4724 (void) memset(&phdr, 0, sizeof (phdr));
4726 phdr.p_type = PT_LOAD;
4727 phdr.p_offset = offset;
4728 phdr.p_vaddr = vma->vma_start;
4729 phdr.p_paddr = 0;
4730 phdr.p_filesz = vma_dump_size(vma);
4731 offset += phdr.p_filesz;
4732 phdr.p_memsz = vma->vma_end - vma->vma_start;
4733 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
4734 if (vma->vma_flags & PROT_WRITE)
4735 phdr.p_flags |= PF_W;
4736 if (vma->vma_flags & PROT_EXEC)
4737 phdr.p_flags |= PF_X;
4738 phdr.p_align = ELF_EXEC_PAGESIZE;
4740 bswap_phdr(&phdr, 1);
4741 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
4742 goto out;
4747 * Next we write notes just after program headers. No
4748 * alignment needed here.
4750 if (write_note_info(&info, fd) < 0)
4751 goto out;
4753 /* align data to page boundary */
4754 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4755 goto out;
4758 * Finally we can dump process memory into corefile as well.
4760 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4761 abi_ulong addr;
4762 abi_ulong end;
4764 end = vma->vma_start + vma_dump_size(vma);
4766 for (addr = vma->vma_start; addr < end;
4767 addr += TARGET_PAGE_SIZE) {
4768 char page[TARGET_PAGE_SIZE];
4769 int error;
4772 * Read in page from target process memory and
4773 * write it to coredump file.
4775 error = copy_from_user(page, addr, sizeof (page));
4776 if (error != 0) {
4777 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
4778 addr);
4779 errno = -error;
4780 goto out;
4782 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4783 goto out;
4787 out:
4788 free_note_info(&info);
4789 if (mm != NULL)
4790 vma_delete(mm);
4791 (void) close(fd);
4793 if (errno != 0)
4794 return (-errno);
4795 return (0);
4797 #endif /* USE_ELF_CORE_DUMP */
4799 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4801 init_thread(regs, infop);