hw/s390x/s390-stattrib: Don't call register_savevm_live() during instance_init()
[qemu/ar7.git] / linux-user / elfload.c
blob8761f9e26b8bcbdd66ac7d96f8d1565fcbe5eaa4
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 uint32_t get_elf_hwcap2(void)
557 ARMCPU *cpu = ARM_CPU(thread_cpu);
558 uint32_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_rdm, ARM_HWCAP_A64_ASIMDRDM);
804 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
805 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
806 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
807 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
808 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
809 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
810 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
811 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
812 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
813 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
814 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
816 return hwcaps;
819 uint32_t get_elf_hwcap2(void)
821 ARMCPU *cpu = ARM_CPU(thread_cpu);
822 uint32_t hwcaps = 0;
824 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
825 GET_FEATURE_ID(aa64_sve2, ARM_HWCAP2_A64_SVE2);
826 GET_FEATURE_ID(aa64_sve2_aes, ARM_HWCAP2_A64_SVEAES);
827 GET_FEATURE_ID(aa64_sve2_pmull128, ARM_HWCAP2_A64_SVEPMULL);
828 GET_FEATURE_ID(aa64_sve2_bitperm, ARM_HWCAP2_A64_SVEBITPERM);
829 GET_FEATURE_ID(aa64_sve2_sha3, ARM_HWCAP2_A64_SVESHA3);
830 GET_FEATURE_ID(aa64_sve2_sm4, ARM_HWCAP2_A64_SVESM4);
831 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
832 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
833 GET_FEATURE_ID(aa64_sve_i8mm, ARM_HWCAP2_A64_SVEI8MM);
834 GET_FEATURE_ID(aa64_sve_f32mm, ARM_HWCAP2_A64_SVEF32MM);
835 GET_FEATURE_ID(aa64_sve_f64mm, ARM_HWCAP2_A64_SVEF64MM);
836 GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16);
837 GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM);
838 GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16);
839 GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG);
840 GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI);
841 GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE);
842 GET_FEATURE_ID(aa64_sme, (ARM_HWCAP2_A64_SME |
843 ARM_HWCAP2_A64_SME_F32F32 |
844 ARM_HWCAP2_A64_SME_B16F32 |
845 ARM_HWCAP2_A64_SME_F16F32 |
846 ARM_HWCAP2_A64_SME_I8I32));
847 GET_FEATURE_ID(aa64_sme_f64f64, ARM_HWCAP2_A64_SME_F64F64);
848 GET_FEATURE_ID(aa64_sme_i16i64, ARM_HWCAP2_A64_SME_I16I64);
849 GET_FEATURE_ID(aa64_sme_fa64, ARM_HWCAP2_A64_SME_FA64);
850 GET_FEATURE_ID(aa64_hbc, ARM_HWCAP2_A64_HBC);
851 GET_FEATURE_ID(aa64_mops, ARM_HWCAP2_A64_MOPS);
853 return hwcaps;
856 const char *elf_hwcap_str(uint32_t bit)
858 static const char *hwcap_str[] = {
859 [__builtin_ctz(ARM_HWCAP_A64_FP )] = "fp",
860 [__builtin_ctz(ARM_HWCAP_A64_ASIMD )] = "asimd",
861 [__builtin_ctz(ARM_HWCAP_A64_EVTSTRM )] = "evtstrm",
862 [__builtin_ctz(ARM_HWCAP_A64_AES )] = "aes",
863 [__builtin_ctz(ARM_HWCAP_A64_PMULL )] = "pmull",
864 [__builtin_ctz(ARM_HWCAP_A64_SHA1 )] = "sha1",
865 [__builtin_ctz(ARM_HWCAP_A64_SHA2 )] = "sha2",
866 [__builtin_ctz(ARM_HWCAP_A64_CRC32 )] = "crc32",
867 [__builtin_ctz(ARM_HWCAP_A64_ATOMICS )] = "atomics",
868 [__builtin_ctz(ARM_HWCAP_A64_FPHP )] = "fphp",
869 [__builtin_ctz(ARM_HWCAP_A64_ASIMDHP )] = "asimdhp",
870 [__builtin_ctz(ARM_HWCAP_A64_CPUID )] = "cpuid",
871 [__builtin_ctz(ARM_HWCAP_A64_ASIMDRDM)] = "asimdrdm",
872 [__builtin_ctz(ARM_HWCAP_A64_JSCVT )] = "jscvt",
873 [__builtin_ctz(ARM_HWCAP_A64_FCMA )] = "fcma",
874 [__builtin_ctz(ARM_HWCAP_A64_LRCPC )] = "lrcpc",
875 [__builtin_ctz(ARM_HWCAP_A64_DCPOP )] = "dcpop",
876 [__builtin_ctz(ARM_HWCAP_A64_SHA3 )] = "sha3",
877 [__builtin_ctz(ARM_HWCAP_A64_SM3 )] = "sm3",
878 [__builtin_ctz(ARM_HWCAP_A64_SM4 )] = "sm4",
879 [__builtin_ctz(ARM_HWCAP_A64_ASIMDDP )] = "asimddp",
880 [__builtin_ctz(ARM_HWCAP_A64_SHA512 )] = "sha512",
881 [__builtin_ctz(ARM_HWCAP_A64_SVE )] = "sve",
882 [__builtin_ctz(ARM_HWCAP_A64_ASIMDFHM)] = "asimdfhm",
883 [__builtin_ctz(ARM_HWCAP_A64_DIT )] = "dit",
884 [__builtin_ctz(ARM_HWCAP_A64_USCAT )] = "uscat",
885 [__builtin_ctz(ARM_HWCAP_A64_ILRCPC )] = "ilrcpc",
886 [__builtin_ctz(ARM_HWCAP_A64_FLAGM )] = "flagm",
887 [__builtin_ctz(ARM_HWCAP_A64_SSBS )] = "ssbs",
888 [__builtin_ctz(ARM_HWCAP_A64_SB )] = "sb",
889 [__builtin_ctz(ARM_HWCAP_A64_PACA )] = "paca",
890 [__builtin_ctz(ARM_HWCAP_A64_PACG )] = "pacg",
893 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
896 const char *elf_hwcap2_str(uint32_t bit)
898 static const char *hwcap_str[] = {
899 [__builtin_ctz(ARM_HWCAP2_A64_DCPODP )] = "dcpodp",
900 [__builtin_ctz(ARM_HWCAP2_A64_SVE2 )] = "sve2",
901 [__builtin_ctz(ARM_HWCAP2_A64_SVEAES )] = "sveaes",
902 [__builtin_ctz(ARM_HWCAP2_A64_SVEPMULL )] = "svepmull",
903 [__builtin_ctz(ARM_HWCAP2_A64_SVEBITPERM )] = "svebitperm",
904 [__builtin_ctz(ARM_HWCAP2_A64_SVESHA3 )] = "svesha3",
905 [__builtin_ctz(ARM_HWCAP2_A64_SVESM4 )] = "svesm4",
906 [__builtin_ctz(ARM_HWCAP2_A64_FLAGM2 )] = "flagm2",
907 [__builtin_ctz(ARM_HWCAP2_A64_FRINT )] = "frint",
908 [__builtin_ctz(ARM_HWCAP2_A64_SVEI8MM )] = "svei8mm",
909 [__builtin_ctz(ARM_HWCAP2_A64_SVEF32MM )] = "svef32mm",
910 [__builtin_ctz(ARM_HWCAP2_A64_SVEF64MM )] = "svef64mm",
911 [__builtin_ctz(ARM_HWCAP2_A64_SVEBF16 )] = "svebf16",
912 [__builtin_ctz(ARM_HWCAP2_A64_I8MM )] = "i8mm",
913 [__builtin_ctz(ARM_HWCAP2_A64_BF16 )] = "bf16",
914 [__builtin_ctz(ARM_HWCAP2_A64_DGH )] = "dgh",
915 [__builtin_ctz(ARM_HWCAP2_A64_RNG )] = "rng",
916 [__builtin_ctz(ARM_HWCAP2_A64_BTI )] = "bti",
917 [__builtin_ctz(ARM_HWCAP2_A64_MTE )] = "mte",
918 [__builtin_ctz(ARM_HWCAP2_A64_ECV )] = "ecv",
919 [__builtin_ctz(ARM_HWCAP2_A64_AFP )] = "afp",
920 [__builtin_ctz(ARM_HWCAP2_A64_RPRES )] = "rpres",
921 [__builtin_ctz(ARM_HWCAP2_A64_MTE3 )] = "mte3",
922 [__builtin_ctz(ARM_HWCAP2_A64_SME )] = "sme",
923 [__builtin_ctz(ARM_HWCAP2_A64_SME_I16I64 )] = "smei16i64",
924 [__builtin_ctz(ARM_HWCAP2_A64_SME_F64F64 )] = "smef64f64",
925 [__builtin_ctz(ARM_HWCAP2_A64_SME_I8I32 )] = "smei8i32",
926 [__builtin_ctz(ARM_HWCAP2_A64_SME_F16F32 )] = "smef16f32",
927 [__builtin_ctz(ARM_HWCAP2_A64_SME_B16F32 )] = "smeb16f32",
928 [__builtin_ctz(ARM_HWCAP2_A64_SME_F32F32 )] = "smef32f32",
929 [__builtin_ctz(ARM_HWCAP2_A64_SME_FA64 )] = "smefa64",
930 [__builtin_ctz(ARM_HWCAP2_A64_WFXT )] = "wfxt",
931 [__builtin_ctzll(ARM_HWCAP2_A64_EBF16 )] = "ebf16",
932 [__builtin_ctzll(ARM_HWCAP2_A64_SVE_EBF16 )] = "sveebf16",
933 [__builtin_ctzll(ARM_HWCAP2_A64_CSSC )] = "cssc",
934 [__builtin_ctzll(ARM_HWCAP2_A64_RPRFM )] = "rprfm",
935 [__builtin_ctzll(ARM_HWCAP2_A64_SVE2P1 )] = "sve2p1",
936 [__builtin_ctzll(ARM_HWCAP2_A64_SME2 )] = "sme2",
937 [__builtin_ctzll(ARM_HWCAP2_A64_SME2P1 )] = "sme2p1",
938 [__builtin_ctzll(ARM_HWCAP2_A64_SME_I16I32 )] = "smei16i32",
939 [__builtin_ctzll(ARM_HWCAP2_A64_SME_BI32I32)] = "smebi32i32",
940 [__builtin_ctzll(ARM_HWCAP2_A64_SME_B16B16 )] = "smeb16b16",
941 [__builtin_ctzll(ARM_HWCAP2_A64_SME_F16F16 )] = "smef16f16",
942 [__builtin_ctzll(ARM_HWCAP2_A64_MOPS )] = "mops",
943 [__builtin_ctzll(ARM_HWCAP2_A64_HBC )] = "hbc",
946 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
949 #undef GET_FEATURE_ID
951 #endif /* not TARGET_AARCH64 */
953 #if TARGET_BIG_ENDIAN
954 # define VDSO_HEADER "vdso-be.c.inc"
955 #else
956 # define VDSO_HEADER "vdso-le.c.inc"
957 #endif
959 #endif /* TARGET_ARM */
961 #ifdef TARGET_SPARC
962 #ifdef TARGET_SPARC64
964 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
965 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
966 #ifndef TARGET_ABI32
967 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
968 #else
969 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
970 #endif
972 #define ELF_CLASS ELFCLASS64
973 #define ELF_ARCH EM_SPARCV9
974 #else
975 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
976 | HWCAP_SPARC_MULDIV)
977 #define ELF_CLASS ELFCLASS32
978 #define ELF_ARCH EM_SPARC
979 #endif /* TARGET_SPARC64 */
981 static inline void init_thread(struct target_pt_regs *regs,
982 struct image_info *infop)
984 /* Note that target_cpu_copy_regs does not read psr/tstate. */
985 regs->pc = infop->entry;
986 regs->npc = regs->pc + 4;
987 regs->y = 0;
988 regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong)
989 - TARGET_STACK_BIAS);
991 #endif /* TARGET_SPARC */
993 #ifdef TARGET_PPC
995 #define ELF_MACHINE PPC_ELF_MACHINE
997 #if defined(TARGET_PPC64)
999 #define elf_check_arch(x) ( (x) == EM_PPC64 )
1001 #define ELF_CLASS ELFCLASS64
1003 #else
1005 #define ELF_CLASS ELFCLASS32
1006 #define EXSTACK_DEFAULT true
1008 #endif
1010 #define ELF_ARCH EM_PPC
1012 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
1013 See arch/powerpc/include/asm/cputable.h. */
1014 enum {
1015 QEMU_PPC_FEATURE_32 = 0x80000000,
1016 QEMU_PPC_FEATURE_64 = 0x40000000,
1017 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
1018 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
1019 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
1020 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
1021 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
1022 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
1023 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
1024 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
1025 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
1026 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
1027 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
1028 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
1029 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
1030 QEMU_PPC_FEATURE_CELL = 0x00010000,
1031 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
1032 QEMU_PPC_FEATURE_SMT = 0x00004000,
1033 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
1034 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
1035 QEMU_PPC_FEATURE_PA6T = 0x00000800,
1036 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
1037 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
1038 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
1039 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
1040 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
1042 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
1043 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
1045 /* Feature definitions in AT_HWCAP2. */
1046 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
1047 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
1048 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
1049 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
1050 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
1051 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
1052 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
1053 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
1054 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
1055 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
1056 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
1057 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
1058 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
1059 QEMU_PPC_FEATURE2_ARCH_3_1 = 0x00040000, /* ISA 3.1 */
1060 QEMU_PPC_FEATURE2_MMA = 0x00020000, /* Matrix-Multiply Assist */
1063 #define ELF_HWCAP get_elf_hwcap()
1065 static uint32_t get_elf_hwcap(void)
1067 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
1068 uint32_t features = 0;
1070 /* We don't have to be terribly complete here; the high points are
1071 Altivec/FP/SPE support. Anything else is just a bonus. */
1072 #define GET_FEATURE(flag, feature) \
1073 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1074 #define GET_FEATURE2(flags, feature) \
1075 do { \
1076 if ((cpu->env.insns_flags2 & flags) == flags) { \
1077 features |= feature; \
1079 } while (0)
1080 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
1081 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
1082 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
1083 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
1084 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
1085 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
1086 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
1087 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
1088 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
1089 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
1090 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
1091 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
1092 QEMU_PPC_FEATURE_ARCH_2_06);
1093 #undef GET_FEATURE
1094 #undef GET_FEATURE2
1096 return features;
1099 #define ELF_HWCAP2 get_elf_hwcap2()
1101 static uint32_t get_elf_hwcap2(void)
1103 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
1104 uint32_t features = 0;
1106 #define GET_FEATURE(flag, feature) \
1107 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1108 #define GET_FEATURE2(flag, feature) \
1109 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
1111 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
1112 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
1113 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
1114 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
1115 QEMU_PPC_FEATURE2_VEC_CRYPTO);
1116 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
1117 QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128);
1118 GET_FEATURE2(PPC2_ISA310, QEMU_PPC_FEATURE2_ARCH_3_1 |
1119 QEMU_PPC_FEATURE2_MMA);
1121 #undef GET_FEATURE
1122 #undef GET_FEATURE2
1124 return features;
1128 * The requirements here are:
1129 * - keep the final alignment of sp (sp & 0xf)
1130 * - make sure the 32-bit value at the first 16 byte aligned position of
1131 * AUXV is greater than 16 for glibc compatibility.
1132 * AT_IGNOREPPC is used for that.
1133 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
1134 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
1136 #define DLINFO_ARCH_ITEMS 5
1137 #define ARCH_DLINFO \
1138 do { \
1139 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
1140 /* \
1141 * Handle glibc compatibility: these magic entries must \
1142 * be at the lowest addresses in the final auxv. \
1143 */ \
1144 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1145 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1146 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
1147 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
1148 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
1149 } while (0)
1151 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
1153 _regs->gpr[1] = infop->start_stack;
1154 #if defined(TARGET_PPC64)
1155 if (get_ppc64_abi(infop) < 2) {
1156 uint64_t val;
1157 get_user_u64(val, infop->entry + 8);
1158 _regs->gpr[2] = val + infop->load_bias;
1159 get_user_u64(val, infop->entry);
1160 infop->entry = val + infop->load_bias;
1161 } else {
1162 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
1164 #endif
1165 _regs->nip = infop->entry;
1168 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
1169 #define ELF_NREG 48
1170 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1172 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
1174 int i;
1175 target_ulong ccr = 0;
1177 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
1178 (*regs)[i] = tswapreg(env->gpr[i]);
1181 (*regs)[32] = tswapreg(env->nip);
1182 (*regs)[33] = tswapreg(env->msr);
1183 (*regs)[35] = tswapreg(env->ctr);
1184 (*regs)[36] = tswapreg(env->lr);
1185 (*regs)[37] = tswapreg(cpu_read_xer(env));
1187 ccr = ppc_get_cr(env);
1188 (*regs)[38] = tswapreg(ccr);
1191 #define USE_ELF_CORE_DUMP
1192 #define ELF_EXEC_PAGESIZE 4096
1194 #ifndef TARGET_PPC64
1195 # define VDSO_HEADER "vdso-32.c.inc"
1196 #elif TARGET_BIG_ENDIAN
1197 # define VDSO_HEADER "vdso-64.c.inc"
1198 #else
1199 # define VDSO_HEADER "vdso-64le.c.inc"
1200 #endif
1202 #endif
1204 #ifdef TARGET_LOONGARCH64
1206 #define ELF_CLASS ELFCLASS64
1207 #define ELF_ARCH EM_LOONGARCH
1208 #define EXSTACK_DEFAULT true
1210 #define elf_check_arch(x) ((x) == EM_LOONGARCH)
1212 #define VDSO_HEADER "vdso.c.inc"
1214 static inline void init_thread(struct target_pt_regs *regs,
1215 struct image_info *infop)
1217 /*Set crmd PG,DA = 1,0 */
1218 regs->csr.crmd = 2 << 3;
1219 regs->csr.era = infop->entry;
1220 regs->regs[3] = infop->start_stack;
1223 /* See linux kernel: arch/loongarch/include/asm/elf.h */
1224 #define ELF_NREG 45
1225 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1227 enum {
1228 TARGET_EF_R0 = 0,
1229 TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33,
1230 TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34,
1233 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1234 const CPULoongArchState *env)
1236 int i;
1238 (*regs)[TARGET_EF_R0] = 0;
1240 for (i = 1; i < ARRAY_SIZE(env->gpr); i++) {
1241 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]);
1244 (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc);
1245 (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV);
1248 #define USE_ELF_CORE_DUMP
1249 #define ELF_EXEC_PAGESIZE 4096
1251 #define ELF_HWCAP get_elf_hwcap()
1253 /* See arch/loongarch/include/uapi/asm/hwcap.h */
1254 enum {
1255 HWCAP_LOONGARCH_CPUCFG = (1 << 0),
1256 HWCAP_LOONGARCH_LAM = (1 << 1),
1257 HWCAP_LOONGARCH_UAL = (1 << 2),
1258 HWCAP_LOONGARCH_FPU = (1 << 3),
1259 HWCAP_LOONGARCH_LSX = (1 << 4),
1260 HWCAP_LOONGARCH_LASX = (1 << 5),
1261 HWCAP_LOONGARCH_CRC32 = (1 << 6),
1262 HWCAP_LOONGARCH_COMPLEX = (1 << 7),
1263 HWCAP_LOONGARCH_CRYPTO = (1 << 8),
1264 HWCAP_LOONGARCH_LVZ = (1 << 9),
1265 HWCAP_LOONGARCH_LBT_X86 = (1 << 10),
1266 HWCAP_LOONGARCH_LBT_ARM = (1 << 11),
1267 HWCAP_LOONGARCH_LBT_MIPS = (1 << 12),
1270 static uint32_t get_elf_hwcap(void)
1272 LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu);
1273 uint32_t hwcaps = 0;
1275 hwcaps |= HWCAP_LOONGARCH_CRC32;
1277 if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) {
1278 hwcaps |= HWCAP_LOONGARCH_UAL;
1281 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) {
1282 hwcaps |= HWCAP_LOONGARCH_FPU;
1285 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) {
1286 hwcaps |= HWCAP_LOONGARCH_LAM;
1289 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LSX)) {
1290 hwcaps |= HWCAP_LOONGARCH_LSX;
1293 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LASX)) {
1294 hwcaps |= HWCAP_LOONGARCH_LASX;
1297 return hwcaps;
1300 #define ELF_PLATFORM "loongarch"
1302 #endif /* TARGET_LOONGARCH64 */
1304 #ifdef TARGET_MIPS
1306 #ifdef TARGET_MIPS64
1307 #define ELF_CLASS ELFCLASS64
1308 #else
1309 #define ELF_CLASS ELFCLASS32
1310 #endif
1311 #define ELF_ARCH EM_MIPS
1312 #define EXSTACK_DEFAULT true
1314 #ifdef TARGET_ABI_MIPSN32
1315 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1316 #else
1317 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1318 #endif
1320 #define ELF_BASE_PLATFORM get_elf_base_platform()
1322 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \
1323 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1324 { return _base_platform; } } while (0)
1326 static const char *get_elf_base_platform(void)
1328 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1330 /* 64 bit ISAs goes first */
1331 MATCH_PLATFORM_INSN(CPU_MIPS64R6, "mips64r6");
1332 MATCH_PLATFORM_INSN(CPU_MIPS64R5, "mips64r5");
1333 MATCH_PLATFORM_INSN(CPU_MIPS64R2, "mips64r2");
1334 MATCH_PLATFORM_INSN(CPU_MIPS64R1, "mips64");
1335 MATCH_PLATFORM_INSN(CPU_MIPS5, "mips5");
1336 MATCH_PLATFORM_INSN(CPU_MIPS4, "mips4");
1337 MATCH_PLATFORM_INSN(CPU_MIPS3, "mips3");
1339 /* 32 bit ISAs */
1340 MATCH_PLATFORM_INSN(CPU_MIPS32R6, "mips32r6");
1341 MATCH_PLATFORM_INSN(CPU_MIPS32R5, "mips32r5");
1342 MATCH_PLATFORM_INSN(CPU_MIPS32R2, "mips32r2");
1343 MATCH_PLATFORM_INSN(CPU_MIPS32R1, "mips32");
1344 MATCH_PLATFORM_INSN(CPU_MIPS2, "mips2");
1346 /* Fallback */
1347 return "mips";
1349 #undef MATCH_PLATFORM_INSN
1351 static inline void init_thread(struct target_pt_regs *regs,
1352 struct image_info *infop)
1354 regs->cp0_status = 2 << CP0St_KSU;
1355 regs->cp0_epc = infop->entry;
1356 regs->regs[29] = infop->start_stack;
1359 /* See linux kernel: arch/mips/include/asm/elf.h. */
1360 #define ELF_NREG 45
1361 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1363 /* See linux kernel: arch/mips/include/asm/reg.h. */
1364 enum {
1365 #ifdef TARGET_MIPS64
1366 TARGET_EF_R0 = 0,
1367 #else
1368 TARGET_EF_R0 = 6,
1369 #endif
1370 TARGET_EF_R26 = TARGET_EF_R0 + 26,
1371 TARGET_EF_R27 = TARGET_EF_R0 + 27,
1372 TARGET_EF_LO = TARGET_EF_R0 + 32,
1373 TARGET_EF_HI = TARGET_EF_R0 + 33,
1374 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
1375 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
1376 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
1377 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
1380 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1381 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
1383 int i;
1385 for (i = 0; i < TARGET_EF_R0; i++) {
1386 (*regs)[i] = 0;
1388 (*regs)[TARGET_EF_R0] = 0;
1390 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
1391 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
1394 (*regs)[TARGET_EF_R26] = 0;
1395 (*regs)[TARGET_EF_R27] = 0;
1396 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
1397 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
1398 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
1399 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
1400 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
1401 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
1404 #define USE_ELF_CORE_DUMP
1405 #define ELF_EXEC_PAGESIZE 4096
1407 /* See arch/mips/include/uapi/asm/hwcap.h. */
1408 enum {
1409 HWCAP_MIPS_R6 = (1 << 0),
1410 HWCAP_MIPS_MSA = (1 << 1),
1411 HWCAP_MIPS_CRC32 = (1 << 2),
1412 HWCAP_MIPS_MIPS16 = (1 << 3),
1413 HWCAP_MIPS_MDMX = (1 << 4),
1414 HWCAP_MIPS_MIPS3D = (1 << 5),
1415 HWCAP_MIPS_SMARTMIPS = (1 << 6),
1416 HWCAP_MIPS_DSP = (1 << 7),
1417 HWCAP_MIPS_DSP2 = (1 << 8),
1418 HWCAP_MIPS_DSP3 = (1 << 9),
1419 HWCAP_MIPS_MIPS16E2 = (1 << 10),
1420 HWCAP_LOONGSON_MMI = (1 << 11),
1421 HWCAP_LOONGSON_EXT = (1 << 12),
1422 HWCAP_LOONGSON_EXT2 = (1 << 13),
1423 HWCAP_LOONGSON_CPUCFG = (1 << 14),
1426 #define ELF_HWCAP get_elf_hwcap()
1428 #define GET_FEATURE_INSN(_flag, _hwcap) \
1429 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1431 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1432 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1434 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1435 do { \
1436 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1437 hwcaps |= _hwcap; \
1439 } while (0)
1441 static uint32_t get_elf_hwcap(void)
1443 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1444 uint32_t hwcaps = 0;
1446 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH,
1447 2, HWCAP_MIPS_R6);
1448 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA);
1449 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI);
1450 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT);
1452 return hwcaps;
1455 #undef GET_FEATURE_REG_EQU
1456 #undef GET_FEATURE_REG_SET
1457 #undef GET_FEATURE_INSN
1459 #endif /* TARGET_MIPS */
1461 #ifdef TARGET_MICROBLAZE
1463 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1465 #define ELF_CLASS ELFCLASS32
1466 #define ELF_ARCH EM_MICROBLAZE
1468 static inline void init_thread(struct target_pt_regs *regs,
1469 struct image_info *infop)
1471 regs->pc = infop->entry;
1472 regs->r1 = infop->start_stack;
1476 #define ELF_EXEC_PAGESIZE 4096
1478 #define USE_ELF_CORE_DUMP
1479 #define ELF_NREG 38
1480 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1482 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1483 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1485 int i, pos = 0;
1487 for (i = 0; i < 32; i++) {
1488 (*regs)[pos++] = tswapreg(env->regs[i]);
1491 (*regs)[pos++] = tswapreg(env->pc);
1492 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
1493 (*regs)[pos++] = 0;
1494 (*regs)[pos++] = tswapreg(env->ear);
1495 (*regs)[pos++] = 0;
1496 (*regs)[pos++] = tswapreg(env->esr);
1499 #endif /* TARGET_MICROBLAZE */
1501 #ifdef TARGET_NIOS2
1503 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1505 #define ELF_CLASS ELFCLASS32
1506 #define ELF_ARCH EM_ALTERA_NIOS2
1508 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1510 regs->ea = infop->entry;
1511 regs->sp = infop->start_stack;
1514 #define LO_COMMPAGE TARGET_PAGE_SIZE
1516 static bool init_guest_commpage(void)
1518 static const uint8_t kuser_page[4 + 2 * 64] = {
1519 /* __kuser_helper_version */
1520 [0x00] = 0x02, 0x00, 0x00, 0x00,
1522 /* __kuser_cmpxchg */
1523 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1524 0x3a, 0x28, 0x00, 0xf8, /* ret */
1526 /* __kuser_sigtramp */
1527 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1528 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1531 void *want = g2h_untagged(LO_COMMPAGE & -qemu_host_page_size);
1532 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
1533 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1535 if (addr == MAP_FAILED) {
1536 perror("Allocating guest commpage");
1537 exit(EXIT_FAILURE);
1539 if (addr != want) {
1540 return false;
1543 memcpy(addr, kuser_page, sizeof(kuser_page));
1545 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
1546 perror("Protecting guest commpage");
1547 exit(EXIT_FAILURE);
1550 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1551 PAGE_READ | PAGE_EXEC | PAGE_VALID);
1552 return true;
1555 #define ELF_EXEC_PAGESIZE 4096
1557 #define USE_ELF_CORE_DUMP
1558 #define ELF_NREG 49
1559 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1561 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1562 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1563 const CPUNios2State *env)
1565 int i;
1567 (*regs)[0] = -1;
1568 for (i = 1; i < 8; i++) /* r0-r7 */
1569 (*regs)[i] = tswapreg(env->regs[i + 7]);
1571 for (i = 8; i < 16; i++) /* r8-r15 */
1572 (*regs)[i] = tswapreg(env->regs[i - 8]);
1574 for (i = 16; i < 24; i++) /* r16-r23 */
1575 (*regs)[i] = tswapreg(env->regs[i + 7]);
1576 (*regs)[24] = -1; /* R_ET */
1577 (*regs)[25] = -1; /* R_BT */
1578 (*regs)[26] = tswapreg(env->regs[R_GP]);
1579 (*regs)[27] = tswapreg(env->regs[R_SP]);
1580 (*regs)[28] = tswapreg(env->regs[R_FP]);
1581 (*regs)[29] = tswapreg(env->regs[R_EA]);
1582 (*regs)[30] = -1; /* R_SSTATUS */
1583 (*regs)[31] = tswapreg(env->regs[R_RA]);
1585 (*regs)[32] = tswapreg(env->pc);
1587 (*regs)[33] = -1; /* R_STATUS */
1588 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1590 for (i = 35; i < 49; i++) /* ... */
1591 (*regs)[i] = -1;
1594 #endif /* TARGET_NIOS2 */
1596 #ifdef TARGET_OPENRISC
1598 #define ELF_ARCH EM_OPENRISC
1599 #define ELF_CLASS ELFCLASS32
1600 #define ELF_DATA ELFDATA2MSB
1602 static inline void init_thread(struct target_pt_regs *regs,
1603 struct image_info *infop)
1605 regs->pc = infop->entry;
1606 regs->gpr[1] = infop->start_stack;
1609 #define USE_ELF_CORE_DUMP
1610 #define ELF_EXEC_PAGESIZE 8192
1612 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1613 #define ELF_NREG 34 /* gprs and pc, sr */
1614 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1616 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1617 const CPUOpenRISCState *env)
1619 int i;
1621 for (i = 0; i < 32; i++) {
1622 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1624 (*regs)[32] = tswapreg(env->pc);
1625 (*regs)[33] = tswapreg(cpu_get_sr(env));
1627 #define ELF_HWCAP 0
1628 #define ELF_PLATFORM NULL
1630 #endif /* TARGET_OPENRISC */
1632 #ifdef TARGET_SH4
1634 #define ELF_CLASS ELFCLASS32
1635 #define ELF_ARCH EM_SH
1637 static inline void init_thread(struct target_pt_regs *regs,
1638 struct image_info *infop)
1640 /* Check other registers XXXXX */
1641 regs->pc = infop->entry;
1642 regs->regs[15] = infop->start_stack;
1645 /* See linux kernel: arch/sh/include/asm/elf.h. */
1646 #define ELF_NREG 23
1647 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1649 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1650 enum {
1651 TARGET_REG_PC = 16,
1652 TARGET_REG_PR = 17,
1653 TARGET_REG_SR = 18,
1654 TARGET_REG_GBR = 19,
1655 TARGET_REG_MACH = 20,
1656 TARGET_REG_MACL = 21,
1657 TARGET_REG_SYSCALL = 22
1660 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1661 const CPUSH4State *env)
1663 int i;
1665 for (i = 0; i < 16; i++) {
1666 (*regs)[i] = tswapreg(env->gregs[i]);
1669 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1670 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1671 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1672 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1673 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1674 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1675 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1678 #define USE_ELF_CORE_DUMP
1679 #define ELF_EXEC_PAGESIZE 4096
1681 enum {
1682 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1683 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1684 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1685 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1686 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1687 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1688 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1689 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1690 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1691 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1694 #define ELF_HWCAP get_elf_hwcap()
1696 static uint32_t get_elf_hwcap(void)
1698 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1699 uint32_t hwcap = 0;
1701 hwcap |= SH_CPU_HAS_FPU;
1703 if (cpu->env.features & SH_FEATURE_SH4A) {
1704 hwcap |= SH_CPU_HAS_LLSC;
1707 return hwcap;
1710 #endif
1712 #ifdef TARGET_CRIS
1714 #define ELF_CLASS ELFCLASS32
1715 #define ELF_ARCH EM_CRIS
1717 static inline void init_thread(struct target_pt_regs *regs,
1718 struct image_info *infop)
1720 regs->erp = infop->entry;
1723 #define ELF_EXEC_PAGESIZE 8192
1725 #endif
1727 #ifdef TARGET_M68K
1729 #define ELF_CLASS ELFCLASS32
1730 #define ELF_ARCH EM_68K
1732 /* ??? Does this need to do anything?
1733 #define ELF_PLAT_INIT(_r) */
1735 static inline void init_thread(struct target_pt_regs *regs,
1736 struct image_info *infop)
1738 regs->usp = infop->start_stack;
1739 regs->sr = 0;
1740 regs->pc = infop->entry;
1743 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1744 #define ELF_NREG 20
1745 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1747 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1749 (*regs)[0] = tswapreg(env->dregs[1]);
1750 (*regs)[1] = tswapreg(env->dregs[2]);
1751 (*regs)[2] = tswapreg(env->dregs[3]);
1752 (*regs)[3] = tswapreg(env->dregs[4]);
1753 (*regs)[4] = tswapreg(env->dregs[5]);
1754 (*regs)[5] = tswapreg(env->dregs[6]);
1755 (*regs)[6] = tswapreg(env->dregs[7]);
1756 (*regs)[7] = tswapreg(env->aregs[0]);
1757 (*regs)[8] = tswapreg(env->aregs[1]);
1758 (*regs)[9] = tswapreg(env->aregs[2]);
1759 (*regs)[10] = tswapreg(env->aregs[3]);
1760 (*regs)[11] = tswapreg(env->aregs[4]);
1761 (*regs)[12] = tswapreg(env->aregs[5]);
1762 (*regs)[13] = tswapreg(env->aregs[6]);
1763 (*regs)[14] = tswapreg(env->dregs[0]);
1764 (*regs)[15] = tswapreg(env->aregs[7]);
1765 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1766 (*regs)[17] = tswapreg(env->sr);
1767 (*regs)[18] = tswapreg(env->pc);
1768 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1771 #define USE_ELF_CORE_DUMP
1772 #define ELF_EXEC_PAGESIZE 8192
1774 #endif
1776 #ifdef TARGET_ALPHA
1778 #define ELF_CLASS ELFCLASS64
1779 #define ELF_ARCH EM_ALPHA
1781 static inline void init_thread(struct target_pt_regs *regs,
1782 struct image_info *infop)
1784 regs->pc = infop->entry;
1785 regs->ps = 8;
1786 regs->usp = infop->start_stack;
1789 #define ELF_EXEC_PAGESIZE 8192
1791 #endif /* TARGET_ALPHA */
1793 #ifdef TARGET_S390X
1795 #define ELF_CLASS ELFCLASS64
1796 #define ELF_DATA ELFDATA2MSB
1797 #define ELF_ARCH EM_S390
1799 #include "elf.h"
1801 #define ELF_HWCAP get_elf_hwcap()
1803 #define GET_FEATURE(_feat, _hwcap) \
1804 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1806 uint32_t get_elf_hwcap(void)
1809 * Let's assume we always have esan3 and zarch.
1810 * 31-bit processes can use 64-bit registers (high gprs).
1812 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1814 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1815 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1816 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1817 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1818 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1819 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1820 hwcap |= HWCAP_S390_ETF3EH;
1822 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1823 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT);
1824 GET_FEATURE(S390_FEAT_VECTOR_ENH2, HWCAP_S390_VXRS_EXT2);
1826 return hwcap;
1829 const char *elf_hwcap_str(uint32_t bit)
1831 static const char *hwcap_str[] = {
1832 [HWCAP_S390_NR_ESAN3] = "esan3",
1833 [HWCAP_S390_NR_ZARCH] = "zarch",
1834 [HWCAP_S390_NR_STFLE] = "stfle",
1835 [HWCAP_S390_NR_MSA] = "msa",
1836 [HWCAP_S390_NR_LDISP] = "ldisp",
1837 [HWCAP_S390_NR_EIMM] = "eimm",
1838 [HWCAP_S390_NR_DFP] = "dfp",
1839 [HWCAP_S390_NR_HPAGE] = "edat",
1840 [HWCAP_S390_NR_ETF3EH] = "etf3eh",
1841 [HWCAP_S390_NR_HIGH_GPRS] = "highgprs",
1842 [HWCAP_S390_NR_TE] = "te",
1843 [HWCAP_S390_NR_VXRS] = "vx",
1844 [HWCAP_S390_NR_VXRS_BCD] = "vxd",
1845 [HWCAP_S390_NR_VXRS_EXT] = "vxe",
1846 [HWCAP_S390_NR_GS] = "gs",
1847 [HWCAP_S390_NR_VXRS_EXT2] = "vxe2",
1848 [HWCAP_S390_NR_VXRS_PDE] = "vxp",
1849 [HWCAP_S390_NR_SORT] = "sort",
1850 [HWCAP_S390_NR_DFLT] = "dflt",
1851 [HWCAP_S390_NR_NNPA] = "nnpa",
1852 [HWCAP_S390_NR_PCI_MIO] = "pcimio",
1853 [HWCAP_S390_NR_SIE] = "sie",
1856 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
1859 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1861 regs->psw.addr = infop->entry;
1862 regs->psw.mask = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
1863 PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_MASK_64 | \
1864 PSW_MASK_32;
1865 regs->gprs[15] = infop->start_stack;
1868 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1869 #define ELF_NREG 27
1870 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1872 enum {
1873 TARGET_REG_PSWM = 0,
1874 TARGET_REG_PSWA = 1,
1875 TARGET_REG_GPRS = 2,
1876 TARGET_REG_ARS = 18,
1877 TARGET_REG_ORIG_R2 = 26,
1880 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1881 const CPUS390XState *env)
1883 int i;
1884 uint32_t *aregs;
1886 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask);
1887 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr);
1888 for (i = 0; i < 16; i++) {
1889 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]);
1891 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]);
1892 for (i = 0; i < 16; i++) {
1893 aregs[i] = tswap32(env->aregs[i]);
1895 (*regs)[TARGET_REG_ORIG_R2] = 0;
1898 #define USE_ELF_CORE_DUMP
1899 #define ELF_EXEC_PAGESIZE 4096
1901 #define VDSO_HEADER "vdso.c.inc"
1903 #endif /* TARGET_S390X */
1905 #ifdef TARGET_RISCV
1907 #define ELF_ARCH EM_RISCV
1909 #ifdef TARGET_RISCV32
1910 #define ELF_CLASS ELFCLASS32
1911 #define VDSO_HEADER "vdso-32.c.inc"
1912 #else
1913 #define ELF_CLASS ELFCLASS64
1914 #define VDSO_HEADER "vdso-64.c.inc"
1915 #endif
1917 #define ELF_HWCAP get_elf_hwcap()
1919 static uint32_t get_elf_hwcap(void)
1921 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1922 RISCVCPU *cpu = RISCV_CPU(thread_cpu);
1923 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1924 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C')
1925 | MISA_BIT('V');
1927 return cpu->env.misa_ext & mask;
1928 #undef MISA_BIT
1931 static inline void init_thread(struct target_pt_regs *regs,
1932 struct image_info *infop)
1934 regs->sepc = infop->entry;
1935 regs->sp = infop->start_stack;
1938 #define ELF_EXEC_PAGESIZE 4096
1940 #endif /* TARGET_RISCV */
1942 #ifdef TARGET_HPPA
1944 #define ELF_CLASS ELFCLASS32
1945 #define ELF_ARCH EM_PARISC
1946 #define ELF_PLATFORM "PARISC"
1947 #define STACK_GROWS_DOWN 0
1948 #define STACK_ALIGNMENT 64
1950 #define VDSO_HEADER "vdso.c.inc"
1952 static inline void init_thread(struct target_pt_regs *regs,
1953 struct image_info *infop)
1955 regs->iaoq[0] = infop->entry;
1956 regs->iaoq[1] = infop->entry + 4;
1957 regs->gr[23] = 0;
1958 regs->gr[24] = infop->argv;
1959 regs->gr[25] = infop->argc;
1960 /* The top-of-stack contains a linkage buffer. */
1961 regs->gr[30] = infop->start_stack + 64;
1962 regs->gr[31] = infop->entry;
1965 #define LO_COMMPAGE 0
1967 static bool init_guest_commpage(void)
1969 void *want = g2h_untagged(LO_COMMPAGE);
1970 void *addr = mmap(want, qemu_host_page_size, PROT_NONE,
1971 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1973 if (addr == MAP_FAILED) {
1974 perror("Allocating guest commpage");
1975 exit(EXIT_FAILURE);
1977 if (addr != want) {
1978 return false;
1982 * On Linux, page zero is normally marked execute only + gateway.
1983 * Normal read or write is supposed to fail (thus PROT_NONE above),
1984 * but specific offsets have kernel code mapped to raise permissions
1985 * and implement syscalls. Here, simply mark the page executable.
1986 * Special case the entry points during translation (see do_page_zero).
1988 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1989 PAGE_EXEC | PAGE_VALID);
1990 return true;
1993 #endif /* TARGET_HPPA */
1995 #ifdef TARGET_XTENSA
1997 #define ELF_CLASS ELFCLASS32
1998 #define ELF_ARCH EM_XTENSA
2000 static inline void init_thread(struct target_pt_regs *regs,
2001 struct image_info *infop)
2003 regs->windowbase = 0;
2004 regs->windowstart = 1;
2005 regs->areg[1] = infop->start_stack;
2006 regs->pc = infop->entry;
2007 if (info_is_fdpic(infop)) {
2008 regs->areg[4] = infop->loadmap_addr;
2009 regs->areg[5] = infop->interpreter_loadmap_addr;
2010 if (infop->interpreter_loadmap_addr) {
2011 regs->areg[6] = infop->interpreter_pt_dynamic_addr;
2012 } else {
2013 regs->areg[6] = infop->pt_dynamic_addr;
2018 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
2019 #define ELF_NREG 128
2020 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
2022 enum {
2023 TARGET_REG_PC,
2024 TARGET_REG_PS,
2025 TARGET_REG_LBEG,
2026 TARGET_REG_LEND,
2027 TARGET_REG_LCOUNT,
2028 TARGET_REG_SAR,
2029 TARGET_REG_WINDOWSTART,
2030 TARGET_REG_WINDOWBASE,
2031 TARGET_REG_THREADPTR,
2032 TARGET_REG_AR0 = 64,
2035 static void elf_core_copy_regs(target_elf_gregset_t *regs,
2036 const CPUXtensaState *env)
2038 unsigned i;
2040 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
2041 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
2042 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
2043 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
2044 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
2045 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
2046 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
2047 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
2048 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
2049 xtensa_sync_phys_from_window((CPUXtensaState *)env);
2050 for (i = 0; i < env->config->nareg; ++i) {
2051 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
2055 #define USE_ELF_CORE_DUMP
2056 #define ELF_EXEC_PAGESIZE 4096
2058 #endif /* TARGET_XTENSA */
2060 #ifdef TARGET_HEXAGON
2062 #define ELF_CLASS ELFCLASS32
2063 #define ELF_ARCH EM_HEXAGON
2065 static inline void init_thread(struct target_pt_regs *regs,
2066 struct image_info *infop)
2068 regs->sepc = infop->entry;
2069 regs->sp = infop->start_stack;
2072 #endif /* TARGET_HEXAGON */
2074 #ifndef ELF_BASE_PLATFORM
2075 #define ELF_BASE_PLATFORM (NULL)
2076 #endif
2078 #ifndef ELF_PLATFORM
2079 #define ELF_PLATFORM (NULL)
2080 #endif
2082 #ifndef ELF_MACHINE
2083 #define ELF_MACHINE ELF_ARCH
2084 #endif
2086 #ifndef elf_check_arch
2087 #define elf_check_arch(x) ((x) == ELF_ARCH)
2088 #endif
2090 #ifndef elf_check_abi
2091 #define elf_check_abi(x) (1)
2092 #endif
2094 #ifndef ELF_HWCAP
2095 #define ELF_HWCAP 0
2096 #endif
2098 #ifndef STACK_GROWS_DOWN
2099 #define STACK_GROWS_DOWN 1
2100 #endif
2102 #ifndef STACK_ALIGNMENT
2103 #define STACK_ALIGNMENT 16
2104 #endif
2106 #ifdef TARGET_ABI32
2107 #undef ELF_CLASS
2108 #define ELF_CLASS ELFCLASS32
2109 #undef bswaptls
2110 #define bswaptls(ptr) bswap32s(ptr)
2111 #endif
2113 #ifndef EXSTACK_DEFAULT
2114 #define EXSTACK_DEFAULT false
2115 #endif
2117 #include "elf.h"
2119 /* We must delay the following stanzas until after "elf.h". */
2120 #if defined(TARGET_AARCH64)
2122 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2123 const uint32_t *data,
2124 struct image_info *info,
2125 Error **errp)
2127 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
2128 if (pr_datasz != sizeof(uint32_t)) {
2129 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
2130 return false;
2132 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
2133 info->note_flags = *data;
2135 return true;
2137 #define ARCH_USE_GNU_PROPERTY 1
2139 #else
2141 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2142 const uint32_t *data,
2143 struct image_info *info,
2144 Error **errp)
2146 g_assert_not_reached();
2148 #define ARCH_USE_GNU_PROPERTY 0
2150 #endif
2152 struct exec
2154 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
2155 unsigned int a_text; /* length of text, in bytes */
2156 unsigned int a_data; /* length of data, in bytes */
2157 unsigned int a_bss; /* length of uninitialized data area, in bytes */
2158 unsigned int a_syms; /* length of symbol table data in file, in bytes */
2159 unsigned int a_entry; /* start address */
2160 unsigned int a_trsize; /* length of relocation info for text, in bytes */
2161 unsigned int a_drsize; /* length of relocation info for data, in bytes */
2165 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
2166 #define OMAGIC 0407
2167 #define NMAGIC 0410
2168 #define ZMAGIC 0413
2169 #define QMAGIC 0314
2171 #define DLINFO_ITEMS 16
2173 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
2175 memcpy(to, from, n);
2178 #ifdef BSWAP_NEEDED
2179 static void bswap_ehdr(struct elfhdr *ehdr)
2181 bswap16s(&ehdr->e_type); /* Object file type */
2182 bswap16s(&ehdr->e_machine); /* Architecture */
2183 bswap32s(&ehdr->e_version); /* Object file version */
2184 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
2185 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
2186 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
2187 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
2188 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
2189 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
2190 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
2191 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
2192 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
2193 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
2196 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
2198 int i;
2199 for (i = 0; i < phnum; ++i, ++phdr) {
2200 bswap32s(&phdr->p_type); /* Segment type */
2201 bswap32s(&phdr->p_flags); /* Segment flags */
2202 bswaptls(&phdr->p_offset); /* Segment file offset */
2203 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
2204 bswaptls(&phdr->p_paddr); /* Segment physical address */
2205 bswaptls(&phdr->p_filesz); /* Segment size in file */
2206 bswaptls(&phdr->p_memsz); /* Segment size in memory */
2207 bswaptls(&phdr->p_align); /* Segment alignment */
2211 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
2213 int i;
2214 for (i = 0; i < shnum; ++i, ++shdr) {
2215 bswap32s(&shdr->sh_name);
2216 bswap32s(&shdr->sh_type);
2217 bswaptls(&shdr->sh_flags);
2218 bswaptls(&shdr->sh_addr);
2219 bswaptls(&shdr->sh_offset);
2220 bswaptls(&shdr->sh_size);
2221 bswap32s(&shdr->sh_link);
2222 bswap32s(&shdr->sh_info);
2223 bswaptls(&shdr->sh_addralign);
2224 bswaptls(&shdr->sh_entsize);
2228 static void bswap_sym(struct elf_sym *sym)
2230 bswap32s(&sym->st_name);
2231 bswaptls(&sym->st_value);
2232 bswaptls(&sym->st_size);
2233 bswap16s(&sym->st_shndx);
2236 #ifdef TARGET_MIPS
2237 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
2239 bswap16s(&abiflags->version);
2240 bswap32s(&abiflags->ases);
2241 bswap32s(&abiflags->isa_ext);
2242 bswap32s(&abiflags->flags1);
2243 bswap32s(&abiflags->flags2);
2245 #endif
2246 #else
2247 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
2248 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
2249 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
2250 static inline void bswap_sym(struct elf_sym *sym) { }
2251 #ifdef TARGET_MIPS
2252 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
2253 #endif
2254 #endif
2256 #ifdef USE_ELF_CORE_DUMP
2257 static int elf_core_dump(int, const CPUArchState *);
2258 #endif /* USE_ELF_CORE_DUMP */
2259 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
2260 abi_ulong load_bias);
2262 /* Verify the portions of EHDR within E_IDENT for the target.
2263 This can be performed before bswapping the entire header. */
2264 static bool elf_check_ident(struct elfhdr *ehdr)
2266 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
2267 && ehdr->e_ident[EI_MAG1] == ELFMAG1
2268 && ehdr->e_ident[EI_MAG2] == ELFMAG2
2269 && ehdr->e_ident[EI_MAG3] == ELFMAG3
2270 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
2271 && ehdr->e_ident[EI_DATA] == ELF_DATA
2272 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
2275 /* Verify the portions of EHDR outside of E_IDENT for the target.
2276 This has to wait until after bswapping the header. */
2277 static bool elf_check_ehdr(struct elfhdr *ehdr)
2279 return (elf_check_arch(ehdr->e_machine)
2280 && elf_check_abi(ehdr->e_flags)
2281 && ehdr->e_ehsize == sizeof(struct elfhdr)
2282 && ehdr->e_phentsize == sizeof(struct elf_phdr)
2283 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
2287 * 'copy_elf_strings()' copies argument/envelope strings from user
2288 * memory to free pages in kernel mem. These are in a format ready
2289 * to be put directly into the top of new user memory.
2292 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
2293 abi_ulong p, abi_ulong stack_limit)
2295 char *tmp;
2296 int len, i;
2297 abi_ulong top = p;
2299 if (!p) {
2300 return 0; /* bullet-proofing */
2303 if (STACK_GROWS_DOWN) {
2304 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
2305 for (i = argc - 1; i >= 0; --i) {
2306 tmp = argv[i];
2307 if (!tmp) {
2308 fprintf(stderr, "VFS: argc is wrong");
2309 exit(-1);
2311 len = strlen(tmp) + 1;
2312 tmp += len;
2314 if (len > (p - stack_limit)) {
2315 return 0;
2317 while (len) {
2318 int bytes_to_copy = (len > offset) ? offset : len;
2319 tmp -= bytes_to_copy;
2320 p -= bytes_to_copy;
2321 offset -= bytes_to_copy;
2322 len -= bytes_to_copy;
2324 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
2326 if (offset == 0) {
2327 memcpy_to_target(p, scratch, top - p);
2328 top = p;
2329 offset = TARGET_PAGE_SIZE;
2333 if (p != top) {
2334 memcpy_to_target(p, scratch + offset, top - p);
2336 } else {
2337 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
2338 for (i = 0; i < argc; ++i) {
2339 tmp = argv[i];
2340 if (!tmp) {
2341 fprintf(stderr, "VFS: argc is wrong");
2342 exit(-1);
2344 len = strlen(tmp) + 1;
2345 if (len > (stack_limit - p)) {
2346 return 0;
2348 while (len) {
2349 int bytes_to_copy = (len > remaining) ? remaining : len;
2351 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
2353 tmp += bytes_to_copy;
2354 remaining -= bytes_to_copy;
2355 p += bytes_to_copy;
2356 len -= bytes_to_copy;
2358 if (remaining == 0) {
2359 memcpy_to_target(top, scratch, p - top);
2360 top = p;
2361 remaining = TARGET_PAGE_SIZE;
2365 if (p != top) {
2366 memcpy_to_target(top, scratch, p - top);
2370 return p;
2373 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2374 * argument/environment space. Newer kernels (>2.6.33) allow more,
2375 * dependent on stack size, but guarantee at least 32 pages for
2376 * backwards compatibility.
2378 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2380 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
2381 struct image_info *info)
2383 abi_ulong size, error, guard;
2384 int prot;
2386 size = guest_stack_size;
2387 if (size < STACK_LOWER_LIMIT) {
2388 size = STACK_LOWER_LIMIT;
2391 if (STACK_GROWS_DOWN) {
2392 guard = TARGET_PAGE_SIZE;
2393 if (guard < qemu_real_host_page_size()) {
2394 guard = qemu_real_host_page_size();
2396 } else {
2397 /* no guard page for hppa target where stack grows upwards. */
2398 guard = 0;
2401 prot = PROT_READ | PROT_WRITE;
2402 if (info->exec_stack) {
2403 prot |= PROT_EXEC;
2405 error = target_mmap(0, size + guard, prot,
2406 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2407 if (error == -1) {
2408 perror("mmap stack");
2409 exit(-1);
2412 /* We reserve one extra page at the top of the stack as guard. */
2413 if (STACK_GROWS_DOWN) {
2414 target_mprotect(error, guard, PROT_NONE);
2415 info->stack_limit = error + guard;
2416 return info->stack_limit + size - sizeof(void *);
2417 } else {
2418 info->stack_limit = error + size;
2419 return error;
2424 * zero_bss:
2426 * Map and zero the bss. We need to explicitly zero any fractional pages
2427 * after the data section (i.e. bss). Return false on mapping failure.
2429 static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss,
2430 int prot, Error **errp)
2432 abi_ulong align_bss;
2434 /* We only expect writable bss; the code segment shouldn't need this. */
2435 if (!(prot & PROT_WRITE)) {
2436 error_setg(errp, "PT_LOAD with non-writable bss");
2437 return false;
2440 align_bss = TARGET_PAGE_ALIGN(start_bss);
2441 end_bss = TARGET_PAGE_ALIGN(end_bss);
2443 if (start_bss < align_bss) {
2444 int flags = page_get_flags(start_bss);
2446 if (!(flags & PAGE_BITS)) {
2448 * The whole address space of the executable was reserved
2449 * at the start, therefore all pages will be VALID.
2450 * But assuming there are no PROT_NONE PT_LOAD segments,
2451 * a PROT_NONE page means no data all bss, and we can
2452 * simply extend the new anon mapping back to the start
2453 * of the page of bss.
2455 align_bss -= TARGET_PAGE_SIZE;
2456 } else {
2458 * The start of the bss shares a page with something.
2459 * The only thing that we expect is the data section,
2460 * which would already be marked writable.
2461 * Overlapping the RX code segment seems malformed.
2463 if (!(flags & PAGE_WRITE)) {
2464 error_setg(errp, "PT_LOAD with bss overlapping "
2465 "non-writable page");
2466 return false;
2469 /* The page is already mapped and writable. */
2470 memset(g2h_untagged(start_bss), 0, align_bss - start_bss);
2474 if (align_bss < end_bss &&
2475 target_mmap(align_bss, end_bss - align_bss, prot,
2476 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
2477 error_setg_errno(errp, errno, "Error mapping bss");
2478 return false;
2480 return true;
2483 #if defined(TARGET_ARM)
2484 static int elf_is_fdpic(struct elfhdr *exec)
2486 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
2488 #elif defined(TARGET_XTENSA)
2489 static int elf_is_fdpic(struct elfhdr *exec)
2491 return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC;
2493 #else
2494 /* Default implementation, always false. */
2495 static int elf_is_fdpic(struct elfhdr *exec)
2497 return 0;
2499 #endif
2501 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
2503 uint16_t n;
2504 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
2506 /* elf32_fdpic_loadseg */
2507 n = info->nsegs;
2508 while (n--) {
2509 sp -= 12;
2510 put_user_u32(loadsegs[n].addr, sp+0);
2511 put_user_u32(loadsegs[n].p_vaddr, sp+4);
2512 put_user_u32(loadsegs[n].p_memsz, sp+8);
2515 /* elf32_fdpic_loadmap */
2516 sp -= 4;
2517 put_user_u16(0, sp+0); /* version */
2518 put_user_u16(info->nsegs, sp+2); /* nsegs */
2520 info->personality = PER_LINUX_FDPIC;
2521 info->loadmap_addr = sp;
2523 return sp;
2526 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
2527 struct elfhdr *exec,
2528 struct image_info *info,
2529 struct image_info *interp_info,
2530 struct image_info *vdso_info)
2532 abi_ulong sp;
2533 abi_ulong u_argc, u_argv, u_envp, u_auxv;
2534 int size;
2535 int i;
2536 abi_ulong u_rand_bytes;
2537 uint8_t k_rand_bytes[16];
2538 abi_ulong u_platform, u_base_platform;
2539 const char *k_platform, *k_base_platform;
2540 const int n = sizeof(elf_addr_t);
2542 sp = p;
2544 /* Needs to be before we load the env/argc/... */
2545 if (elf_is_fdpic(exec)) {
2546 /* Need 4 byte alignment for these structs */
2547 sp &= ~3;
2548 sp = loader_build_fdpic_loadmap(info, sp);
2549 info->other_info = interp_info;
2550 if (interp_info) {
2551 interp_info->other_info = info;
2552 sp = loader_build_fdpic_loadmap(interp_info, sp);
2553 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
2554 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
2555 } else {
2556 info->interpreter_loadmap_addr = 0;
2557 info->interpreter_pt_dynamic_addr = 0;
2561 u_base_platform = 0;
2562 k_base_platform = ELF_BASE_PLATFORM;
2563 if (k_base_platform) {
2564 size_t len = strlen(k_base_platform) + 1;
2565 if (STACK_GROWS_DOWN) {
2566 sp -= (len + n - 1) & ~(n - 1);
2567 u_base_platform = sp;
2568 /* FIXME - check return value of memcpy_to_target() for failure */
2569 memcpy_to_target(sp, k_base_platform, len);
2570 } else {
2571 memcpy_to_target(sp, k_base_platform, len);
2572 u_base_platform = sp;
2573 sp += len + 1;
2577 u_platform = 0;
2578 k_platform = ELF_PLATFORM;
2579 if (k_platform) {
2580 size_t len = strlen(k_platform) + 1;
2581 if (STACK_GROWS_DOWN) {
2582 sp -= (len + n - 1) & ~(n - 1);
2583 u_platform = sp;
2584 /* FIXME - check return value of memcpy_to_target() for failure */
2585 memcpy_to_target(sp, k_platform, len);
2586 } else {
2587 memcpy_to_target(sp, k_platform, len);
2588 u_platform = sp;
2589 sp += len + 1;
2593 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2594 * the argv and envp pointers.
2596 if (STACK_GROWS_DOWN) {
2597 sp = QEMU_ALIGN_DOWN(sp, 16);
2598 } else {
2599 sp = QEMU_ALIGN_UP(sp, 16);
2603 * Generate 16 random bytes for userspace PRNG seeding.
2605 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
2606 if (STACK_GROWS_DOWN) {
2607 sp -= 16;
2608 u_rand_bytes = sp;
2609 /* FIXME - check return value of memcpy_to_target() for failure */
2610 memcpy_to_target(sp, k_rand_bytes, 16);
2611 } else {
2612 memcpy_to_target(sp, k_rand_bytes, 16);
2613 u_rand_bytes = sp;
2614 sp += 16;
2617 size = (DLINFO_ITEMS + 1) * 2;
2618 if (k_base_platform) {
2619 size += 2;
2621 if (k_platform) {
2622 size += 2;
2624 if (vdso_info) {
2625 size += 2;
2627 #ifdef DLINFO_ARCH_ITEMS
2628 size += DLINFO_ARCH_ITEMS * 2;
2629 #endif
2630 #ifdef ELF_HWCAP2
2631 size += 2;
2632 #endif
2633 info->auxv_len = size * n;
2635 size += envc + argc + 2;
2636 size += 1; /* argc itself */
2637 size *= n;
2639 /* Allocate space and finalize stack alignment for entry now. */
2640 if (STACK_GROWS_DOWN) {
2641 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
2642 sp = u_argc;
2643 } else {
2644 u_argc = sp;
2645 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2648 u_argv = u_argc + n;
2649 u_envp = u_argv + (argc + 1) * n;
2650 u_auxv = u_envp + (envc + 1) * n;
2651 info->saved_auxv = u_auxv;
2652 info->argc = argc;
2653 info->envc = envc;
2654 info->argv = u_argv;
2655 info->envp = u_envp;
2657 /* This is correct because Linux defines
2658 * elf_addr_t as Elf32_Off / Elf64_Off
2660 #define NEW_AUX_ENT(id, val) do { \
2661 put_user_ual(id, u_auxv); u_auxv += n; \
2662 put_user_ual(val, u_auxv); u_auxv += n; \
2663 } while(0)
2665 #ifdef ARCH_DLINFO
2667 * ARCH_DLINFO must come first so platform specific code can enforce
2668 * special alignment requirements on the AUXV if necessary (eg. PPC).
2670 ARCH_DLINFO;
2671 #endif
2672 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2673 * on info->auxv_len will trigger.
2675 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2676 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2677 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2678 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2679 /* Target doesn't support host page size alignment */
2680 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2681 } else {
2682 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2683 qemu_host_page_size)));
2685 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2686 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2687 NEW_AUX_ENT(AT_ENTRY, info->entry);
2688 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2689 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2690 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2691 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2692 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2693 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2694 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2695 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2696 NEW_AUX_ENT(AT_EXECFN, info->file_string);
2698 #ifdef ELF_HWCAP2
2699 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2700 #endif
2702 if (u_base_platform) {
2703 NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform);
2705 if (u_platform) {
2706 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2708 if (vdso_info) {
2709 NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr);
2711 NEW_AUX_ENT (AT_NULL, 0);
2712 #undef NEW_AUX_ENT
2714 /* Check that our initial calculation of the auxv length matches how much
2715 * we actually put into it.
2717 assert(info->auxv_len == u_auxv - info->saved_auxv);
2719 put_user_ual(argc, u_argc);
2721 p = info->arg_strings;
2722 for (i = 0; i < argc; ++i) {
2723 put_user_ual(p, u_argv);
2724 u_argv += n;
2725 p += target_strlen(p) + 1;
2727 put_user_ual(0, u_argv);
2729 p = info->env_strings;
2730 for (i = 0; i < envc; ++i) {
2731 put_user_ual(p, u_envp);
2732 u_envp += n;
2733 p += target_strlen(p) + 1;
2735 put_user_ual(0, u_envp);
2737 return sp;
2740 #if defined(HI_COMMPAGE)
2741 #define LO_COMMPAGE -1
2742 #elif defined(LO_COMMPAGE)
2743 #define HI_COMMPAGE 0
2744 #else
2745 #define HI_COMMPAGE 0
2746 #define LO_COMMPAGE -1
2747 #ifndef INIT_GUEST_COMMPAGE
2748 #define init_guest_commpage() true
2749 #endif
2750 #endif
2753 * pgb_try_mmap:
2754 * @addr: host start address
2755 * @addr_last: host last address
2756 * @keep: do not unmap the probe region
2758 * Return 1 if [@addr, @addr_last] is not mapped in the host,
2759 * return 0 if it is not available to map, and -1 on mmap error.
2760 * If @keep, the region is left mapped on success, otherwise unmapped.
2762 static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep)
2764 size_t size = addr_last - addr + 1;
2765 void *p = mmap((void *)addr, size, PROT_NONE,
2766 MAP_ANONYMOUS | MAP_PRIVATE |
2767 MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0);
2768 int ret;
2770 if (p == MAP_FAILED) {
2771 return errno == EEXIST ? 0 : -1;
2773 ret = p == (void *)addr;
2774 if (!keep || !ret) {
2775 munmap(p, size);
2777 return ret;
2781 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk)
2782 * @addr: host address
2783 * @addr_last: host last address
2784 * @brk: host brk
2786 * Like pgb_try_mmap, but additionally reserve some memory following brk.
2788 static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last,
2789 uintptr_t brk, bool keep)
2791 uintptr_t brk_last = brk + 16 * MiB - 1;
2793 /* Do not map anything close to the host brk. */
2794 if (addr <= brk_last && brk <= addr_last) {
2795 return 0;
2797 return pgb_try_mmap(addr, addr_last, keep);
2801 * pgb_try_mmap_set:
2802 * @ga: set of guest addrs
2803 * @base: guest_base
2804 * @brk: host brk
2806 * Return true if all @ga can be mapped by the host at @base.
2807 * On success, retain the mapping at index 0 for reserved_va.
2810 typedef struct PGBAddrs {
2811 uintptr_t bounds[3][2]; /* start/last pairs */
2812 int nbounds;
2813 } PGBAddrs;
2815 static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk)
2817 for (int i = ga->nbounds - 1; i >= 0; --i) {
2818 if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base,
2819 ga->bounds[i][1] + base,
2820 brk, i == 0 && reserved_va) <= 0) {
2821 return false;
2824 return true;
2828 * pgb_addr_set:
2829 * @ga: output set of guest addrs
2830 * @guest_loaddr: guest image low address
2831 * @guest_loaddr: guest image high address
2832 * @identity: create for identity mapping
2834 * Fill in @ga with the image, COMMPAGE and NULL page.
2836 static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr,
2837 abi_ulong guest_hiaddr, bool try_identity)
2839 int n;
2842 * With a low commpage, or a guest mapped very low,
2843 * we may not be able to use the identity map.
2845 if (try_identity) {
2846 if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) {
2847 return false;
2849 if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) {
2850 return false;
2854 memset(ga, 0, sizeof(*ga));
2855 n = 0;
2857 if (reserved_va) {
2858 ga->bounds[n][0] = try_identity ? mmap_min_addr : 0;
2859 ga->bounds[n][1] = reserved_va;
2860 n++;
2861 /* LO_COMMPAGE and NULL handled by reserving from 0. */
2862 } else {
2863 /* Add any LO_COMMPAGE or NULL page. */
2864 if (LO_COMMPAGE != -1) {
2865 ga->bounds[n][0] = 0;
2866 ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1;
2867 n++;
2868 } else if (!try_identity) {
2869 ga->bounds[n][0] = 0;
2870 ga->bounds[n][1] = TARGET_PAGE_SIZE - 1;
2871 n++;
2874 /* Add the guest image for ET_EXEC. */
2875 if (guest_loaddr) {
2876 ga->bounds[n][0] = guest_loaddr;
2877 ga->bounds[n][1] = guest_hiaddr;
2878 n++;
2883 * Temporarily disable
2884 * "comparison is always false due to limited range of data type"
2885 * due to comparison between unsigned and (possible) 0.
2887 #pragma GCC diagnostic push
2888 #pragma GCC diagnostic ignored "-Wtype-limits"
2890 /* Add any HI_COMMPAGE not covered by reserved_va. */
2891 if (reserved_va < HI_COMMPAGE) {
2892 ga->bounds[n][0] = HI_COMMPAGE & qemu_host_page_mask;
2893 ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1;
2894 n++;
2897 #pragma GCC diagnostic pop
2899 ga->nbounds = n;
2900 return true;
2903 static void pgb_fail_in_use(const char *image_name)
2905 error_report("%s: requires virtual address space that is in use "
2906 "(omit the -B option or choose a different value)",
2907 image_name);
2908 exit(EXIT_FAILURE);
2911 static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr,
2912 uintptr_t guest_hiaddr, uintptr_t align)
2914 PGBAddrs ga;
2915 uintptr_t brk = (uintptr_t)sbrk(0);
2917 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2918 fprintf(stderr, "Requested guest base %p does not satisfy "
2919 "host minimum alignment (0x%" PRIxPTR ")\n",
2920 (void *)guest_base, align);
2921 exit(EXIT_FAILURE);
2924 if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base)
2925 || !pgb_try_mmap_set(&ga, guest_base, brk)) {
2926 pgb_fail_in_use(image_name);
2931 * pgb_find_fallback:
2933 * This is a fallback method for finding holes in the host address space
2934 * if we don't have the benefit of being able to access /proc/self/map.
2935 * It can potentially take a very long time as we can only dumbly iterate
2936 * up the host address space seeing if the allocation would work.
2938 static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align,
2939 uintptr_t brk)
2941 /* TODO: come up with a better estimate of how much to skip. */
2942 uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB;
2944 for (uintptr_t base = skip; ; base += skip) {
2945 base = ROUND_UP(base, align);
2946 if (pgb_try_mmap_set(ga, base, brk)) {
2947 return base;
2949 if (base >= -skip) {
2950 return -1;
2955 static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base,
2956 IntervalTreeRoot *root)
2958 for (int i = ga->nbounds - 1; i >= 0; --i) {
2959 uintptr_t s = base + ga->bounds[i][0];
2960 uintptr_t l = base + ga->bounds[i][1];
2961 IntervalTreeNode *n;
2963 if (l < s) {
2964 /* Wraparound. Skip to advance S to mmap_min_addr. */
2965 return mmap_min_addr - s;
2968 n = interval_tree_iter_first(root, s, l);
2969 if (n != NULL) {
2970 /* Conflict. Skip to advance S to LAST + 1. */
2971 return n->last - s + 1;
2974 return 0; /* success */
2977 static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root,
2978 uintptr_t align, uintptr_t brk)
2980 uintptr_t last = mmap_min_addr;
2981 uintptr_t base, skip;
2983 while (true) {
2984 base = ROUND_UP(last, align);
2985 if (base < last) {
2986 return -1;
2989 skip = pgb_try_itree(ga, base, root);
2990 if (skip == 0) {
2991 break;
2994 last = base + skip;
2995 if (last < base) {
2996 return -1;
3001 * We've chosen 'base' based on holes in the interval tree,
3002 * but we don't yet know if it is a valid host address.
3003 * Because it is the first matching hole, if the host addresses
3004 * are invalid we know there are no further matches.
3006 return pgb_try_mmap_set(ga, base, brk) ? base : -1;
3009 static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr,
3010 uintptr_t guest_hiaddr, uintptr_t align)
3012 IntervalTreeRoot *root;
3013 uintptr_t brk, ret;
3014 PGBAddrs ga;
3016 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
3018 /* Try the identity map first. */
3019 if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {
3020 brk = (uintptr_t)sbrk(0);
3021 if (pgb_try_mmap_set(&ga, 0, brk)) {
3022 guest_base = 0;
3023 return;
3028 * Rebuild the address set for non-identity map.
3029 * This differs in the mapping of the guest NULL page.
3031 pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false);
3033 root = read_self_maps();
3035 /* Read brk after we've read the maps, which will malloc. */
3036 brk = (uintptr_t)sbrk(0);
3038 if (!root) {
3039 ret = pgb_find_fallback(&ga, align, brk);
3040 } else {
3042 * Reserve the area close to the host brk.
3043 * This will be freed with the rest of the tree.
3045 IntervalTreeNode *b = g_new0(IntervalTreeNode, 1);
3046 b->start = brk;
3047 b->last = brk + 16 * MiB - 1;
3048 interval_tree_insert(b, root);
3050 ret = pgb_find_itree(&ga, root, align, brk);
3051 free_self_maps(root);
3054 if (ret == -1) {
3055 int w = TARGET_LONG_BITS / 4;
3057 error_report("%s: Unable to find a guest_base to satisfy all "
3058 "guest address mapping requirements", image_name);
3060 for (int i = 0; i < ga.nbounds; ++i) {
3061 error_printf(" %0*" PRIx64 "-%0*" PRIx64 "\n",
3062 w, (uint64_t)ga.bounds[i][0],
3063 w, (uint64_t)ga.bounds[i][1]);
3065 exit(EXIT_FAILURE);
3067 guest_base = ret;
3070 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
3071 abi_ulong guest_hiaddr)
3073 /* In order to use host shmat, we must be able to honor SHMLBA. */
3074 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
3076 /* Sanity check the guest binary. */
3077 if (reserved_va) {
3078 if (guest_hiaddr > reserved_va) {
3079 error_report("%s: requires more than reserved virtual "
3080 "address space (0x%" PRIx64 " > 0x%lx)",
3081 image_name, (uint64_t)guest_hiaddr, reserved_va);
3082 exit(EXIT_FAILURE);
3084 } else {
3085 if (guest_hiaddr != (uintptr_t)guest_hiaddr) {
3086 error_report("%s: requires more virtual address space "
3087 "than the host can provide (0x%" PRIx64 ")",
3088 image_name, (uint64_t)guest_hiaddr + 1);
3089 exit(EXIT_FAILURE);
3093 if (have_guest_base) {
3094 pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align);
3095 } else {
3096 pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align);
3099 /* Reserve and initialize the commpage. */
3100 if (!init_guest_commpage()) {
3101 /* We have already probed for the commpage being free. */
3102 g_assert_not_reached();
3105 assert(QEMU_IS_ALIGNED(guest_base, align));
3106 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
3107 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
3110 enum {
3111 /* The string "GNU\0" as a magic number. */
3112 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
3113 NOTE_DATA_SZ = 1 * KiB,
3114 NOTE_NAME_SZ = 4,
3115 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
3119 * Process a single gnu_property entry.
3120 * Return false for error.
3122 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
3123 struct image_info *info, bool have_prev_type,
3124 uint32_t *prev_type, Error **errp)
3126 uint32_t pr_type, pr_datasz, step;
3128 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
3129 goto error_data;
3131 datasz -= *off;
3132 data += *off / sizeof(uint32_t);
3134 if (datasz < 2 * sizeof(uint32_t)) {
3135 goto error_data;
3137 pr_type = data[0];
3138 pr_datasz = data[1];
3139 data += 2;
3140 datasz -= 2 * sizeof(uint32_t);
3141 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
3142 if (step > datasz) {
3143 goto error_data;
3146 /* Properties are supposed to be unique and sorted on pr_type. */
3147 if (have_prev_type && pr_type <= *prev_type) {
3148 if (pr_type == *prev_type) {
3149 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
3150 } else {
3151 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
3153 return false;
3155 *prev_type = pr_type;
3157 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
3158 return false;
3161 *off += 2 * sizeof(uint32_t) + step;
3162 return true;
3164 error_data:
3165 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
3166 return false;
3169 /* Process NT_GNU_PROPERTY_TYPE_0. */
3170 static bool parse_elf_properties(const ImageSource *src,
3171 struct image_info *info,
3172 const struct elf_phdr *phdr,
3173 Error **errp)
3175 union {
3176 struct elf_note nhdr;
3177 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
3178 } note;
3180 int n, off, datasz;
3181 bool have_prev_type;
3182 uint32_t prev_type;
3184 /* Unless the arch requires properties, ignore them. */
3185 if (!ARCH_USE_GNU_PROPERTY) {
3186 return true;
3189 /* If the properties are crazy large, that's too bad. */
3190 n = phdr->p_filesz;
3191 if (n > sizeof(note)) {
3192 error_setg(errp, "PT_GNU_PROPERTY too large");
3193 return false;
3195 if (n < sizeof(note.nhdr)) {
3196 error_setg(errp, "PT_GNU_PROPERTY too small");
3197 return false;
3200 if (!imgsrc_read(&note, phdr->p_offset, n, src, errp)) {
3201 return false;
3205 * The contents of a valid PT_GNU_PROPERTY is a sequence
3206 * of uint32_t -- swap them all now.
3208 #ifdef BSWAP_NEEDED
3209 for (int i = 0; i < n / 4; i++) {
3210 bswap32s(note.data + i);
3212 #endif
3215 * Note that nhdr is 3 words, and that the "name" described by namesz
3216 * immediately follows nhdr and is thus at the 4th word. Further, all
3217 * of the inputs to the kernel's round_up are multiples of 4.
3219 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
3220 note.nhdr.n_namesz != NOTE_NAME_SZ ||
3221 note.data[3] != GNU0_MAGIC) {
3222 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
3223 return false;
3225 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
3227 datasz = note.nhdr.n_descsz + off;
3228 if (datasz > n) {
3229 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
3230 return false;
3233 have_prev_type = false;
3234 prev_type = 0;
3235 while (1) {
3236 if (off == datasz) {
3237 return true; /* end, exit ok */
3239 if (!parse_elf_property(note.data, &off, datasz, info,
3240 have_prev_type, &prev_type, errp)) {
3241 return false;
3243 have_prev_type = true;
3248 * load_elf_image: Load an ELF image into the address space.
3249 * @image_name: the filename of the image, to use in error messages.
3250 * @src: the ImageSource from which to read.
3251 * @info: info collected from the loaded image.
3252 * @ehdr: the ELF header, not yet bswapped.
3253 * @pinterp_name: record any PT_INTERP string found.
3255 * On return: @info values will be filled in, as necessary or available.
3258 static void load_elf_image(const char *image_name, const ImageSource *src,
3259 struct image_info *info, struct elfhdr *ehdr,
3260 char **pinterp_name)
3262 g_autofree struct elf_phdr *phdr = NULL;
3263 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
3264 int i, prot_exec;
3265 Error *err = NULL;
3268 * First of all, some simple consistency checks.
3269 * Note that we rely on the bswapped ehdr staying in bprm_buf,
3270 * for later use by load_elf_binary and create_elf_tables.
3272 if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) {
3273 goto exit_errmsg;
3275 if (!elf_check_ident(ehdr)) {
3276 error_setg(&err, "Invalid ELF image for this architecture");
3277 goto exit_errmsg;
3279 bswap_ehdr(ehdr);
3280 if (!elf_check_ehdr(ehdr)) {
3281 error_setg(&err, "Invalid ELF image for this architecture");
3282 goto exit_errmsg;
3285 phdr = imgsrc_read_alloc(ehdr->e_phoff,
3286 ehdr->e_phnum * sizeof(struct elf_phdr),
3287 src, &err);
3288 if (phdr == NULL) {
3289 goto exit_errmsg;
3291 bswap_phdr(phdr, ehdr->e_phnum);
3293 info->nsegs = 0;
3294 info->pt_dynamic_addr = 0;
3296 mmap_lock();
3299 * Find the maximum size of the image and allocate an appropriate
3300 * amount of memory to handle that. Locate the interpreter, if any.
3302 loaddr = -1, hiaddr = 0;
3303 info->alignment = 0;
3304 info->exec_stack = EXSTACK_DEFAULT;
3305 for (i = 0; i < ehdr->e_phnum; ++i) {
3306 struct elf_phdr *eppnt = phdr + i;
3307 if (eppnt->p_type == PT_LOAD) {
3308 abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
3309 if (a < loaddr) {
3310 loaddr = a;
3312 a = eppnt->p_vaddr + eppnt->p_memsz - 1;
3313 if (a > hiaddr) {
3314 hiaddr = a;
3316 ++info->nsegs;
3317 info->alignment |= eppnt->p_align;
3318 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
3319 g_autofree char *interp_name = NULL;
3321 if (*pinterp_name) {
3322 error_setg(&err, "Multiple PT_INTERP entries");
3323 goto exit_errmsg;
3326 interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz,
3327 src, &err);
3328 if (interp_name == NULL) {
3329 goto exit_errmsg;
3331 if (interp_name[eppnt->p_filesz - 1] != 0) {
3332 error_setg(&err, "Invalid PT_INTERP entry");
3333 goto exit_errmsg;
3335 *pinterp_name = g_steal_pointer(&interp_name);
3336 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
3337 if (!parse_elf_properties(src, info, eppnt, &err)) {
3338 goto exit_errmsg;
3340 } else if (eppnt->p_type == PT_GNU_STACK) {
3341 info->exec_stack = eppnt->p_flags & PF_X;
3345 load_addr = loaddr;
3347 if (pinterp_name != NULL) {
3348 if (ehdr->e_type == ET_EXEC) {
3350 * Make sure that the low address does not conflict with
3351 * MMAP_MIN_ADDR or the QEMU application itself.
3353 probe_guest_base(image_name, loaddr, hiaddr);
3354 } else {
3355 abi_ulong align;
3358 * The binary is dynamic, but we still need to
3359 * select guest_base. In this case we pass a size.
3361 probe_guest_base(image_name, 0, hiaddr - loaddr);
3364 * Avoid collision with the loader by providing a different
3365 * default load address.
3367 load_addr += elf_et_dyn_base;
3370 * TODO: Better support for mmap alignment is desirable.
3371 * Since we do not have complete control over the guest
3372 * address space, we prefer the kernel to choose some address
3373 * rather than force the use of LOAD_ADDR via MAP_FIXED.
3374 * But without MAP_FIXED we cannot guarantee alignment,
3375 * only suggest it.
3377 align = pow2ceil(info->alignment);
3378 if (align) {
3379 load_addr &= -align;
3385 * Reserve address space for all of this.
3387 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get
3388 * exactly the address range that is required. Without reserved_va,
3389 * the guest address space is not isolated. We have attempted to avoid
3390 * conflict with the host program itself via probe_guest_base, but using
3391 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check.
3393 * Otherwise this is ET_DYN, and we are searching for a location
3394 * that can hold the memory space required. If the image is
3395 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should
3396 * honor that address if it happens to be free.
3398 * In both cases, we will overwrite pages in this range with mappings
3399 * from the executable.
3401 load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE,
3402 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
3403 (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0),
3404 -1, 0);
3405 if (load_addr == -1) {
3406 goto exit_mmap;
3408 load_bias = load_addr - loaddr;
3410 if (elf_is_fdpic(ehdr)) {
3411 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
3412 g_malloc(sizeof(*loadsegs) * info->nsegs);
3414 for (i = 0; i < ehdr->e_phnum; ++i) {
3415 switch (phdr[i].p_type) {
3416 case PT_DYNAMIC:
3417 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
3418 break;
3419 case PT_LOAD:
3420 loadsegs->addr = phdr[i].p_vaddr + load_bias;
3421 loadsegs->p_vaddr = phdr[i].p_vaddr;
3422 loadsegs->p_memsz = phdr[i].p_memsz;
3423 ++loadsegs;
3424 break;
3429 info->load_bias = load_bias;
3430 info->code_offset = load_bias;
3431 info->data_offset = load_bias;
3432 info->load_addr = load_addr;
3433 info->entry = ehdr->e_entry + load_bias;
3434 info->start_code = -1;
3435 info->end_code = 0;
3436 info->start_data = -1;
3437 info->end_data = 0;
3438 /* Usual start for brk is after all sections of the main executable. */
3439 info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
3440 info->elf_flags = ehdr->e_flags;
3442 prot_exec = PROT_EXEC;
3443 #ifdef TARGET_AARCH64
3445 * If the BTI feature is present, this indicates that the executable
3446 * pages of the startup binary should be mapped with PROT_BTI, so that
3447 * branch targets are enforced.
3449 * The startup binary is either the interpreter or the static executable.
3450 * The interpreter is responsible for all pages of a dynamic executable.
3452 * Elf notes are backward compatible to older cpus.
3453 * Do not enable BTI unless it is supported.
3455 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
3456 && (pinterp_name == NULL || *pinterp_name == 0)
3457 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
3458 prot_exec |= TARGET_PROT_BTI;
3460 #endif
3462 for (i = 0; i < ehdr->e_phnum; i++) {
3463 struct elf_phdr *eppnt = phdr + i;
3464 if (eppnt->p_type == PT_LOAD) {
3465 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
3466 int elf_prot = 0;
3468 if (eppnt->p_flags & PF_R) {
3469 elf_prot |= PROT_READ;
3471 if (eppnt->p_flags & PF_W) {
3472 elf_prot |= PROT_WRITE;
3474 if (eppnt->p_flags & PF_X) {
3475 elf_prot |= prot_exec;
3478 vaddr = load_bias + eppnt->p_vaddr;
3479 vaddr_po = vaddr & ~TARGET_PAGE_MASK;
3480 vaddr_ps = vaddr & TARGET_PAGE_MASK;
3482 vaddr_ef = vaddr + eppnt->p_filesz;
3483 vaddr_em = vaddr + eppnt->p_memsz;
3486 * Some segments may be completely empty, with a non-zero p_memsz
3487 * but no backing file segment.
3489 if (eppnt->p_filesz != 0) {
3490 error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
3491 elf_prot, MAP_PRIVATE | MAP_FIXED,
3492 src, eppnt->p_offset - vaddr_po);
3493 if (error == -1) {
3494 goto exit_mmap;
3498 /* If the load segment requests extra zeros (e.g. bss), map it. */
3499 if (vaddr_ef < vaddr_em &&
3500 !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) {
3501 goto exit_errmsg;
3504 /* Find the full program boundaries. */
3505 if (elf_prot & PROT_EXEC) {
3506 if (vaddr < info->start_code) {
3507 info->start_code = vaddr;
3509 if (vaddr_ef > info->end_code) {
3510 info->end_code = vaddr_ef;
3513 if (elf_prot & PROT_WRITE) {
3514 if (vaddr < info->start_data) {
3515 info->start_data = vaddr;
3517 if (vaddr_ef > info->end_data) {
3518 info->end_data = vaddr_ef;
3521 #ifdef TARGET_MIPS
3522 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
3523 Mips_elf_abiflags_v0 abiflags;
3525 if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags),
3526 src, &err)) {
3527 goto exit_errmsg;
3529 bswap_mips_abiflags(&abiflags);
3530 info->fp_abi = abiflags.fp_abi;
3531 #endif
3535 if (info->end_data == 0) {
3536 info->start_data = info->end_code;
3537 info->end_data = info->end_code;
3540 if (qemu_log_enabled()) {
3541 load_symbols(ehdr, src, load_bias);
3544 debuginfo_report_elf(image_name, src->fd, load_bias);
3546 mmap_unlock();
3548 close(src->fd);
3549 return;
3551 exit_mmap:
3552 error_setg_errno(&err, errno, "Error mapping file");
3553 goto exit_errmsg;
3554 exit_errmsg:
3555 error_reportf_err(err, "%s: ", image_name);
3556 exit(-1);
3559 static void load_elf_interp(const char *filename, struct image_info *info,
3560 char bprm_buf[BPRM_BUF_SIZE])
3562 struct elfhdr ehdr;
3563 ImageSource src;
3564 int fd, retval;
3565 Error *err = NULL;
3567 fd = open(path(filename), O_RDONLY);
3568 if (fd < 0) {
3569 error_setg_file_open(&err, errno, filename);
3570 error_report_err(err);
3571 exit(-1);
3574 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
3575 if (retval < 0) {
3576 error_setg_errno(&err, errno, "Error reading file header");
3577 error_reportf_err(err, "%s: ", filename);
3578 exit(-1);
3581 src.fd = fd;
3582 src.cache = bprm_buf;
3583 src.cache_size = retval;
3585 load_elf_image(filename, &src, info, &ehdr, NULL);
3588 #ifdef VDSO_HEADER
3589 #include VDSO_HEADER
3590 #define vdso_image_info() &vdso_image_info
3591 #else
3592 #define vdso_image_info() NULL
3593 #endif
3595 static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso)
3597 ImageSource src;
3598 struct elfhdr ehdr;
3599 abi_ulong load_bias, load_addr;
3601 src.fd = -1;
3602 src.cache = vdso->image;
3603 src.cache_size = vdso->image_size;
3605 load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL);
3606 load_addr = info->load_addr;
3607 load_bias = info->load_bias;
3610 * We need to relocate the VDSO image. The one built into the kernel
3611 * is built for a fixed address. The one built for QEMU is not, since
3612 * that requires close control of the guest address space.
3613 * We pre-processed the image to locate all of the addresses that need
3614 * to be updated.
3616 for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) {
3617 abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]);
3618 *addr = tswapal(tswapal(*addr) + load_bias);
3621 /* Install signal trampolines, if present. */
3622 if (vdso->sigreturn_ofs) {
3623 default_sigreturn = load_addr + vdso->sigreturn_ofs;
3625 if (vdso->rt_sigreturn_ofs) {
3626 default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs;
3629 /* Remove write from VDSO segment. */
3630 target_mprotect(info->start_data, info->end_data - info->start_data,
3631 PROT_READ | PROT_EXEC);
3634 static int symfind(const void *s0, const void *s1)
3636 struct elf_sym *sym = (struct elf_sym *)s1;
3637 __typeof(sym->st_value) addr = *(uint64_t *)s0;
3638 int result = 0;
3640 if (addr < sym->st_value) {
3641 result = -1;
3642 } else if (addr >= sym->st_value + sym->st_size) {
3643 result = 1;
3645 return result;
3648 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
3650 #if ELF_CLASS == ELFCLASS32
3651 struct elf_sym *syms = s->disas_symtab.elf32;
3652 #else
3653 struct elf_sym *syms = s->disas_symtab.elf64;
3654 #endif
3656 // binary search
3657 struct elf_sym *sym;
3659 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
3660 if (sym != NULL) {
3661 return s->disas_strtab + sym->st_name;
3664 return "";
3667 /* FIXME: This should use elf_ops.h */
3668 static int symcmp(const void *s0, const void *s1)
3670 struct elf_sym *sym0 = (struct elf_sym *)s0;
3671 struct elf_sym *sym1 = (struct elf_sym *)s1;
3672 return (sym0->st_value < sym1->st_value)
3673 ? -1
3674 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
3677 /* Best attempt to load symbols from this ELF object. */
3678 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
3679 abi_ulong load_bias)
3681 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
3682 g_autofree struct elf_shdr *shdr = NULL;
3683 char *strings = NULL;
3684 struct elf_sym *syms = NULL;
3685 struct elf_sym *new_syms;
3686 uint64_t segsz;
3688 shnum = hdr->e_shnum;
3689 shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr),
3690 src, NULL);
3691 if (shdr == NULL) {
3692 return;
3695 bswap_shdr(shdr, shnum);
3696 for (i = 0; i < shnum; ++i) {
3697 if (shdr[i].sh_type == SHT_SYMTAB) {
3698 sym_idx = i;
3699 str_idx = shdr[i].sh_link;
3700 goto found;
3704 /* There will be no symbol table if the file was stripped. */
3705 return;
3707 found:
3708 /* Now know where the strtab and symtab are. Snarf them. */
3710 segsz = shdr[str_idx].sh_size;
3711 strings = g_try_malloc(segsz);
3712 if (!strings) {
3713 goto give_up;
3715 if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) {
3716 goto give_up;
3719 segsz = shdr[sym_idx].sh_size;
3720 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
3722 * Implausibly large symbol table: give up rather than ploughing
3723 * on with the number of symbols calculation overflowing.
3725 goto give_up;
3727 nsyms = segsz / sizeof(struct elf_sym);
3728 syms = g_try_malloc(segsz);
3729 if (!syms) {
3730 goto give_up;
3732 if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) {
3733 goto give_up;
3736 for (i = 0; i < nsyms; ) {
3737 bswap_sym(syms + i);
3738 /* Throw away entries which we do not need. */
3739 if (syms[i].st_shndx == SHN_UNDEF
3740 || syms[i].st_shndx >= SHN_LORESERVE
3741 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3742 if (i < --nsyms) {
3743 syms[i] = syms[nsyms];
3745 } else {
3746 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3747 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3748 syms[i].st_value &= ~(target_ulong)1;
3749 #endif
3750 syms[i].st_value += load_bias;
3751 i++;
3755 /* No "useful" symbol. */
3756 if (nsyms == 0) {
3757 goto give_up;
3761 * Attempt to free the storage associated with the local symbols
3762 * that we threw away. Whether or not this has any effect on the
3763 * memory allocation depends on the malloc implementation and how
3764 * many symbols we managed to discard.
3766 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3767 if (new_syms == NULL) {
3768 goto give_up;
3770 syms = new_syms;
3772 qsort(syms, nsyms, sizeof(*syms), symcmp);
3775 struct syminfo *s = g_new(struct syminfo, 1);
3777 s->disas_strtab = strings;
3778 s->disas_num_syms = nsyms;
3779 #if ELF_CLASS == ELFCLASS32
3780 s->disas_symtab.elf32 = syms;
3781 #else
3782 s->disas_symtab.elf64 = syms;
3783 #endif
3784 s->lookup_symbol = lookup_symbolxx;
3785 s->next = syminfos;
3786 syminfos = s;
3788 return;
3790 give_up:
3791 g_free(strings);
3792 g_free(syms);
3795 uint32_t get_elf_eflags(int fd)
3797 struct elfhdr ehdr;
3798 off_t offset;
3799 int ret;
3801 /* Read ELF header */
3802 offset = lseek(fd, 0, SEEK_SET);
3803 if (offset == (off_t) -1) {
3804 return 0;
3806 ret = read(fd, &ehdr, sizeof(ehdr));
3807 if (ret < sizeof(ehdr)) {
3808 return 0;
3810 offset = lseek(fd, offset, SEEK_SET);
3811 if (offset == (off_t) -1) {
3812 return 0;
3815 /* Check ELF signature */
3816 if (!elf_check_ident(&ehdr)) {
3817 return 0;
3820 /* check header */
3821 bswap_ehdr(&ehdr);
3822 if (!elf_check_ehdr(&ehdr)) {
3823 return 0;
3826 /* return architecture id */
3827 return ehdr.e_flags;
3830 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3833 * We need a copy of the elf header for passing to create_elf_tables.
3834 * We will have overwritten the original when we re-use bprm->buf
3835 * while loading the interpreter. Allocate the storage for this now
3836 * and let elf_load_image do any swapping that may be required.
3838 struct elfhdr ehdr;
3839 struct image_info interp_info, vdso_info;
3840 char *elf_interpreter = NULL;
3841 char *scratch;
3843 memset(&interp_info, 0, sizeof(interp_info));
3844 #ifdef TARGET_MIPS
3845 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3846 #endif
3848 load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter);
3850 /* Do this so that we can load the interpreter, if need be. We will
3851 change some of these later */
3852 bprm->p = setup_arg_pages(bprm, info);
3854 scratch = g_new0(char, TARGET_PAGE_SIZE);
3855 if (STACK_GROWS_DOWN) {
3856 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3857 bprm->p, info->stack_limit);
3858 info->file_string = bprm->p;
3859 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3860 bprm->p, info->stack_limit);
3861 info->env_strings = bprm->p;
3862 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3863 bprm->p, info->stack_limit);
3864 info->arg_strings = bprm->p;
3865 } else {
3866 info->arg_strings = bprm->p;
3867 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3868 bprm->p, info->stack_limit);
3869 info->env_strings = bprm->p;
3870 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3871 bprm->p, info->stack_limit);
3872 info->file_string = bprm->p;
3873 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3874 bprm->p, info->stack_limit);
3877 g_free(scratch);
3879 if (!bprm->p) {
3880 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3881 exit(-1);
3884 if (elf_interpreter) {
3885 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3888 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky
3889 * with the mappings the interpreter can be loaded above but
3890 * near the main executable, which can leave very little room
3891 * for the heap.
3892 * If the current brk has less than 16MB, use the end of the
3893 * interpreter.
3895 if (interp_info.brk > info->brk &&
3896 interp_info.load_bias - info->brk < 16 * MiB) {
3897 info->brk = interp_info.brk;
3900 /* If the program interpreter is one of these two, then assume
3901 an iBCS2 image. Otherwise assume a native linux image. */
3903 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3904 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3905 info->personality = PER_SVR4;
3907 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3908 and some applications "depend" upon this behavior. Since
3909 we do not have the power to recompile these, we emulate
3910 the SVr4 behavior. Sigh. */
3911 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
3912 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3914 #ifdef TARGET_MIPS
3915 info->interp_fp_abi = interp_info.fp_abi;
3916 #endif
3920 * Load a vdso if available, which will amongst other things contain the
3921 * signal trampolines. Otherwise, allocate a separate page for them.
3923 const VdsoImageInfo *vdso = vdso_image_info();
3924 if (vdso) {
3925 load_elf_vdso(&vdso_info, vdso);
3926 info->vdso = vdso_info.load_bias;
3927 } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) {
3928 abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE,
3929 PROT_READ | PROT_WRITE,
3930 MAP_PRIVATE | MAP_ANON, -1, 0);
3931 if (tramp_page == -1) {
3932 return -errno;
3935 setup_sigtramp(tramp_page);
3936 target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC);
3939 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info,
3940 elf_interpreter ? &interp_info : NULL,
3941 vdso ? &vdso_info : NULL);
3942 info->start_stack = bprm->p;
3944 /* If we have an interpreter, set that as the program's entry point.
3945 Copy the load_bias as well, to help PPC64 interpret the entry
3946 point as a function descriptor. Do this after creating elf tables
3947 so that we copy the original program entry point into the AUXV. */
3948 if (elf_interpreter) {
3949 info->load_bias = interp_info.load_bias;
3950 info->entry = interp_info.entry;
3951 g_free(elf_interpreter);
3954 #ifdef USE_ELF_CORE_DUMP
3955 bprm->core_dump = &elf_core_dump;
3956 #endif
3958 return 0;
3961 #ifdef USE_ELF_CORE_DUMP
3963 * Definitions to generate Intel SVR4-like core files.
3964 * These mostly have the same names as the SVR4 types with "target_elf_"
3965 * tacked on the front to prevent clashes with linux definitions,
3966 * and the typedef forms have been avoided. This is mostly like
3967 * the SVR4 structure, but more Linuxy, with things that Linux does
3968 * not support and which gdb doesn't really use excluded.
3970 * Fields we don't dump (their contents is zero) in linux-user qemu
3971 * are marked with XXX.
3973 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3975 * Porting ELF coredump for target is (quite) simple process. First you
3976 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3977 * the target resides):
3979 * #define USE_ELF_CORE_DUMP
3981 * Next you define type of register set used for dumping. ELF specification
3982 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3984 * typedef <target_regtype> target_elf_greg_t;
3985 * #define ELF_NREG <number of registers>
3986 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3988 * Last step is to implement target specific function that copies registers
3989 * from given cpu into just specified register set. Prototype is:
3991 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3992 * const CPUArchState *env);
3994 * Parameters:
3995 * regs - copy register values into here (allocated and zeroed by caller)
3996 * env - copy registers from here
3998 * Example for ARM target is provided in this file.
4001 /* An ELF note in memory */
4002 struct memelfnote {
4003 const char *name;
4004 size_t namesz;
4005 size_t namesz_rounded;
4006 int type;
4007 size_t datasz;
4008 size_t datasz_rounded;
4009 void *data;
4010 size_t notesz;
4013 struct target_elf_siginfo {
4014 abi_int si_signo; /* signal number */
4015 abi_int si_code; /* extra code */
4016 abi_int si_errno; /* errno */
4019 struct target_elf_prstatus {
4020 struct target_elf_siginfo pr_info; /* Info associated with signal */
4021 abi_short pr_cursig; /* Current signal */
4022 abi_ulong pr_sigpend; /* XXX */
4023 abi_ulong pr_sighold; /* XXX */
4024 target_pid_t pr_pid;
4025 target_pid_t pr_ppid;
4026 target_pid_t pr_pgrp;
4027 target_pid_t pr_sid;
4028 struct target_timeval pr_utime; /* XXX User time */
4029 struct target_timeval pr_stime; /* XXX System time */
4030 struct target_timeval pr_cutime; /* XXX Cumulative user time */
4031 struct target_timeval pr_cstime; /* XXX Cumulative system time */
4032 target_elf_gregset_t pr_reg; /* GP registers */
4033 abi_int pr_fpvalid; /* XXX */
4036 #define ELF_PRARGSZ (80) /* Number of chars for args */
4038 struct target_elf_prpsinfo {
4039 char pr_state; /* numeric process state */
4040 char pr_sname; /* char for pr_state */
4041 char pr_zomb; /* zombie */
4042 char pr_nice; /* nice val */
4043 abi_ulong pr_flag; /* flags */
4044 target_uid_t pr_uid;
4045 target_gid_t pr_gid;
4046 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
4047 /* Lots missing */
4048 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
4049 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
4052 /* Here is the structure in which status of each thread is captured. */
4053 struct elf_thread_status {
4054 QTAILQ_ENTRY(elf_thread_status) ets_link;
4055 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
4056 #if 0
4057 elf_fpregset_t fpu; /* NT_PRFPREG */
4058 struct task_struct *thread;
4059 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
4060 #endif
4061 struct memelfnote notes[1];
4062 int num_notes;
4065 struct elf_note_info {
4066 struct memelfnote *notes;
4067 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
4068 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
4070 QTAILQ_HEAD(, elf_thread_status) thread_list;
4071 #if 0
4073 * Current version of ELF coredump doesn't support
4074 * dumping fp regs etc.
4076 elf_fpregset_t *fpu;
4077 elf_fpxregset_t *xfpu;
4078 int thread_status_size;
4079 #endif
4080 int notes_size;
4081 int numnote;
4084 struct vm_area_struct {
4085 target_ulong vma_start; /* start vaddr of memory region */
4086 target_ulong vma_end; /* end vaddr of memory region */
4087 abi_ulong vma_flags; /* protection etc. flags for the region */
4088 QTAILQ_ENTRY(vm_area_struct) vma_link;
4091 struct mm_struct {
4092 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
4093 int mm_count; /* number of mappings */
4096 static struct mm_struct *vma_init(void);
4097 static void vma_delete(struct mm_struct *);
4098 static int vma_add_mapping(struct mm_struct *, target_ulong,
4099 target_ulong, abi_ulong);
4100 static int vma_get_mapping_count(const struct mm_struct *);
4101 static struct vm_area_struct *vma_first(const struct mm_struct *);
4102 static struct vm_area_struct *vma_next(struct vm_area_struct *);
4103 static abi_ulong vma_dump_size(const struct vm_area_struct *);
4104 static int vma_walker(void *priv, target_ulong start, target_ulong end,
4105 unsigned long flags);
4107 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
4108 static void fill_note(struct memelfnote *, const char *, int,
4109 unsigned int, void *);
4110 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
4111 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
4112 static void fill_auxv_note(struct memelfnote *, const TaskState *);
4113 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
4114 static size_t note_size(const struct memelfnote *);
4115 static void free_note_info(struct elf_note_info *);
4116 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
4117 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
4119 static int dump_write(int, const void *, size_t);
4120 static int write_note(struct memelfnote *, int);
4121 static int write_note_info(struct elf_note_info *, int);
4123 #ifdef BSWAP_NEEDED
4124 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
4126 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
4127 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
4128 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
4129 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
4130 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
4131 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
4132 prstatus->pr_pid = tswap32(prstatus->pr_pid);
4133 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
4134 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
4135 prstatus->pr_sid = tswap32(prstatus->pr_sid);
4136 /* cpu times are not filled, so we skip them */
4137 /* regs should be in correct format already */
4138 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
4141 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
4143 psinfo->pr_flag = tswapal(psinfo->pr_flag);
4144 psinfo->pr_uid = tswap16(psinfo->pr_uid);
4145 psinfo->pr_gid = tswap16(psinfo->pr_gid);
4146 psinfo->pr_pid = tswap32(psinfo->pr_pid);
4147 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
4148 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
4149 psinfo->pr_sid = tswap32(psinfo->pr_sid);
4152 static void bswap_note(struct elf_note *en)
4154 bswap32s(&en->n_namesz);
4155 bswap32s(&en->n_descsz);
4156 bswap32s(&en->n_type);
4158 #else
4159 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
4160 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
4161 static inline void bswap_note(struct elf_note *en) { }
4162 #endif /* BSWAP_NEEDED */
4165 * Minimal support for linux memory regions. These are needed
4166 * when we are finding out what memory exactly belongs to
4167 * emulated process. No locks needed here, as long as
4168 * thread that received the signal is stopped.
4171 static struct mm_struct *vma_init(void)
4173 struct mm_struct *mm;
4175 if ((mm = g_malloc(sizeof (*mm))) == NULL)
4176 return (NULL);
4178 mm->mm_count = 0;
4179 QTAILQ_INIT(&mm->mm_mmap);
4181 return (mm);
4184 static void vma_delete(struct mm_struct *mm)
4186 struct vm_area_struct *vma;
4188 while ((vma = vma_first(mm)) != NULL) {
4189 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
4190 g_free(vma);
4192 g_free(mm);
4195 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
4196 target_ulong end, abi_ulong flags)
4198 struct vm_area_struct *vma;
4200 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
4201 return (-1);
4203 vma->vma_start = start;
4204 vma->vma_end = end;
4205 vma->vma_flags = flags;
4207 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
4208 mm->mm_count++;
4210 return (0);
4213 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
4215 return (QTAILQ_FIRST(&mm->mm_mmap));
4218 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
4220 return (QTAILQ_NEXT(vma, vma_link));
4223 static int vma_get_mapping_count(const struct mm_struct *mm)
4225 return (mm->mm_count);
4229 * Calculate file (dump) size of given memory region.
4231 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
4233 /* if we cannot even read the first page, skip it */
4234 if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
4235 return (0);
4238 * Usually we don't dump executable pages as they contain
4239 * non-writable code that debugger can read directly from
4240 * target library etc. However, thread stacks are marked
4241 * also executable so we read in first page of given region
4242 * and check whether it contains elf header. If there is
4243 * no elf header, we dump it.
4245 if (vma->vma_flags & PROT_EXEC) {
4246 char page[TARGET_PAGE_SIZE];
4248 if (copy_from_user(page, vma->vma_start, sizeof (page))) {
4249 return 0;
4251 if ((page[EI_MAG0] == ELFMAG0) &&
4252 (page[EI_MAG1] == ELFMAG1) &&
4253 (page[EI_MAG2] == ELFMAG2) &&
4254 (page[EI_MAG3] == ELFMAG3)) {
4256 * Mappings are possibly from ELF binary. Don't dump
4257 * them.
4259 return (0);
4263 return (vma->vma_end - vma->vma_start);
4266 static int vma_walker(void *priv, target_ulong start, target_ulong end,
4267 unsigned long flags)
4269 struct mm_struct *mm = (struct mm_struct *)priv;
4271 vma_add_mapping(mm, start, end, flags);
4272 return (0);
4275 static void fill_note(struct memelfnote *note, const char *name, int type,
4276 unsigned int sz, void *data)
4278 unsigned int namesz;
4280 namesz = strlen(name) + 1;
4281 note->name = name;
4282 note->namesz = namesz;
4283 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
4284 note->type = type;
4285 note->datasz = sz;
4286 note->datasz_rounded = roundup(sz, sizeof (int32_t));
4288 note->data = data;
4291 * We calculate rounded up note size here as specified by
4292 * ELF document.
4294 note->notesz = sizeof (struct elf_note) +
4295 note->namesz_rounded + note->datasz_rounded;
4298 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
4299 uint32_t flags)
4301 (void) memset(elf, 0, sizeof(*elf));
4303 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
4304 elf->e_ident[EI_CLASS] = ELF_CLASS;
4305 elf->e_ident[EI_DATA] = ELF_DATA;
4306 elf->e_ident[EI_VERSION] = EV_CURRENT;
4307 elf->e_ident[EI_OSABI] = ELF_OSABI;
4309 elf->e_type = ET_CORE;
4310 elf->e_machine = machine;
4311 elf->e_version = EV_CURRENT;
4312 elf->e_phoff = sizeof(struct elfhdr);
4313 elf->e_flags = flags;
4314 elf->e_ehsize = sizeof(struct elfhdr);
4315 elf->e_phentsize = sizeof(struct elf_phdr);
4316 elf->e_phnum = segs;
4318 bswap_ehdr(elf);
4321 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
4323 phdr->p_type = PT_NOTE;
4324 phdr->p_offset = offset;
4325 phdr->p_vaddr = 0;
4326 phdr->p_paddr = 0;
4327 phdr->p_filesz = sz;
4328 phdr->p_memsz = 0;
4329 phdr->p_flags = 0;
4330 phdr->p_align = 0;
4332 bswap_phdr(phdr, 1);
4335 static size_t note_size(const struct memelfnote *note)
4337 return (note->notesz);
4340 static void fill_prstatus(struct target_elf_prstatus *prstatus,
4341 const TaskState *ts, int signr)
4343 (void) memset(prstatus, 0, sizeof (*prstatus));
4344 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
4345 prstatus->pr_pid = ts->ts_tid;
4346 prstatus->pr_ppid = getppid();
4347 prstatus->pr_pgrp = getpgrp();
4348 prstatus->pr_sid = getsid(0);
4350 bswap_prstatus(prstatus);
4353 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
4355 char *base_filename;
4356 unsigned int i, len;
4358 (void) memset(psinfo, 0, sizeof (*psinfo));
4360 len = ts->info->env_strings - ts->info->arg_strings;
4361 if (len >= ELF_PRARGSZ)
4362 len = ELF_PRARGSZ - 1;
4363 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) {
4364 return -EFAULT;
4366 for (i = 0; i < len; i++)
4367 if (psinfo->pr_psargs[i] == 0)
4368 psinfo->pr_psargs[i] = ' ';
4369 psinfo->pr_psargs[len] = 0;
4371 psinfo->pr_pid = getpid();
4372 psinfo->pr_ppid = getppid();
4373 psinfo->pr_pgrp = getpgrp();
4374 psinfo->pr_sid = getsid(0);
4375 psinfo->pr_uid = getuid();
4376 psinfo->pr_gid = getgid();
4378 base_filename = g_path_get_basename(ts->bprm->filename);
4380 * Using strncpy here is fine: at max-length,
4381 * this field is not NUL-terminated.
4383 (void) strncpy(psinfo->pr_fname, base_filename,
4384 sizeof(psinfo->pr_fname));
4386 g_free(base_filename);
4387 bswap_psinfo(psinfo);
4388 return (0);
4391 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
4393 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
4394 elf_addr_t orig_auxv = auxv;
4395 void *ptr;
4396 int len = ts->info->auxv_len;
4399 * Auxiliary vector is stored in target process stack. It contains
4400 * {type, value} pairs that we need to dump into note. This is not
4401 * strictly necessary but we do it here for sake of completeness.
4404 /* read in whole auxv vector and copy it to memelfnote */
4405 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
4406 if (ptr != NULL) {
4407 fill_note(note, "CORE", NT_AUXV, len, ptr);
4408 unlock_user(ptr, auxv, len);
4413 * Constructs name of coredump file. We have following convention
4414 * for the name:
4415 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4417 * Returns the filename
4419 static char *core_dump_filename(const TaskState *ts)
4421 g_autoptr(GDateTime) now = g_date_time_new_now_local();
4422 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
4423 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
4425 return g_strdup_printf("qemu_%s_%s_%d.core",
4426 base_filename, nowstr, (int)getpid());
4429 static int dump_write(int fd, const void *ptr, size_t size)
4431 const char *bufp = (const char *)ptr;
4432 ssize_t bytes_written, bytes_left;
4433 struct rlimit dumpsize;
4434 off_t pos;
4436 bytes_written = 0;
4437 getrlimit(RLIMIT_CORE, &dumpsize);
4438 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
4439 if (errno == ESPIPE) { /* not a seekable stream */
4440 bytes_left = size;
4441 } else {
4442 return pos;
4444 } else {
4445 if (dumpsize.rlim_cur <= pos) {
4446 return -1;
4447 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
4448 bytes_left = size;
4449 } else {
4450 size_t limit_left=dumpsize.rlim_cur - pos;
4451 bytes_left = limit_left >= size ? size : limit_left ;
4456 * In normal conditions, single write(2) should do but
4457 * in case of socket etc. this mechanism is more portable.
4459 do {
4460 bytes_written = write(fd, bufp, bytes_left);
4461 if (bytes_written < 0) {
4462 if (errno == EINTR)
4463 continue;
4464 return (-1);
4465 } else if (bytes_written == 0) { /* eof */
4466 return (-1);
4468 bufp += bytes_written;
4469 bytes_left -= bytes_written;
4470 } while (bytes_left > 0);
4472 return (0);
4475 static int write_note(struct memelfnote *men, int fd)
4477 struct elf_note en;
4479 en.n_namesz = men->namesz;
4480 en.n_type = men->type;
4481 en.n_descsz = men->datasz;
4483 bswap_note(&en);
4485 if (dump_write(fd, &en, sizeof(en)) != 0)
4486 return (-1);
4487 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
4488 return (-1);
4489 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
4490 return (-1);
4492 return (0);
4495 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
4497 CPUState *cpu = env_cpu((CPUArchState *)env);
4498 TaskState *ts = (TaskState *)cpu->opaque;
4499 struct elf_thread_status *ets;
4501 ets = g_malloc0(sizeof (*ets));
4502 ets->num_notes = 1; /* only prstatus is dumped */
4503 fill_prstatus(&ets->prstatus, ts, 0);
4504 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
4505 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
4506 &ets->prstatus);
4508 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
4510 info->notes_size += note_size(&ets->notes[0]);
4513 static void init_note_info(struct elf_note_info *info)
4515 /* Initialize the elf_note_info structure so that it is at
4516 * least safe to call free_note_info() on it. Must be
4517 * called before calling fill_note_info().
4519 memset(info, 0, sizeof (*info));
4520 QTAILQ_INIT(&info->thread_list);
4523 static int fill_note_info(struct elf_note_info *info,
4524 long signr, const CPUArchState *env)
4526 #define NUMNOTES 3
4527 CPUState *cpu = env_cpu((CPUArchState *)env);
4528 TaskState *ts = (TaskState *)cpu->opaque;
4529 int i;
4531 info->notes = g_new0(struct memelfnote, NUMNOTES);
4532 if (info->notes == NULL)
4533 return (-ENOMEM);
4534 info->prstatus = g_malloc0(sizeof (*info->prstatus));
4535 if (info->prstatus == NULL)
4536 return (-ENOMEM);
4537 info->psinfo = g_malloc0(sizeof (*info->psinfo));
4538 if (info->prstatus == NULL)
4539 return (-ENOMEM);
4542 * First fill in status (and registers) of current thread
4543 * including process info & aux vector.
4545 fill_prstatus(info->prstatus, ts, signr);
4546 elf_core_copy_regs(&info->prstatus->pr_reg, env);
4547 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
4548 sizeof (*info->prstatus), info->prstatus);
4549 fill_psinfo(info->psinfo, ts);
4550 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
4551 sizeof (*info->psinfo), info->psinfo);
4552 fill_auxv_note(&info->notes[2], ts);
4553 info->numnote = 3;
4555 info->notes_size = 0;
4556 for (i = 0; i < info->numnote; i++)
4557 info->notes_size += note_size(&info->notes[i]);
4559 /* read and fill status of all threads */
4560 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock) {
4561 CPU_FOREACH(cpu) {
4562 if (cpu == thread_cpu) {
4563 continue;
4565 fill_thread_info(info, cpu_env(cpu));
4569 return (0);
4572 static void free_note_info(struct elf_note_info *info)
4574 struct elf_thread_status *ets;
4576 while (!QTAILQ_EMPTY(&info->thread_list)) {
4577 ets = QTAILQ_FIRST(&info->thread_list);
4578 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
4579 g_free(ets);
4582 g_free(info->prstatus);
4583 g_free(info->psinfo);
4584 g_free(info->notes);
4587 static int write_note_info(struct elf_note_info *info, int fd)
4589 struct elf_thread_status *ets;
4590 int i, error = 0;
4592 /* write prstatus, psinfo and auxv for current thread */
4593 for (i = 0; i < info->numnote; i++)
4594 if ((error = write_note(&info->notes[i], fd)) != 0)
4595 return (error);
4597 /* write prstatus for each thread */
4598 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
4599 if ((error = write_note(&ets->notes[0], fd)) != 0)
4600 return (error);
4603 return (0);
4607 * Write out ELF coredump.
4609 * See documentation of ELF object file format in:
4610 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4612 * Coredump format in linux is following:
4614 * 0 +----------------------+ \
4615 * | ELF header | ET_CORE |
4616 * +----------------------+ |
4617 * | ELF program headers | |--- headers
4618 * | - NOTE section | |
4619 * | - PT_LOAD sections | |
4620 * +----------------------+ /
4621 * | NOTEs: |
4622 * | - NT_PRSTATUS |
4623 * | - NT_PRSINFO |
4624 * | - NT_AUXV |
4625 * +----------------------+ <-- aligned to target page
4626 * | Process memory dump |
4627 * : :
4628 * . .
4629 * : :
4630 * | |
4631 * +----------------------+
4633 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4634 * NT_PRSINFO -> struct elf_prpsinfo
4635 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4637 * Format follows System V format as close as possible. Current
4638 * version limitations are as follows:
4639 * - no floating point registers are dumped
4641 * Function returns 0 in case of success, negative errno otherwise.
4643 * TODO: make this work also during runtime: it should be
4644 * possible to force coredump from running process and then
4645 * continue processing. For example qemu could set up SIGUSR2
4646 * handler (provided that target process haven't registered
4647 * handler for that) that does the dump when signal is received.
4649 static int elf_core_dump(int signr, const CPUArchState *env)
4651 const CPUState *cpu = env_cpu((CPUArchState *)env);
4652 const TaskState *ts = (const TaskState *)cpu->opaque;
4653 struct vm_area_struct *vma = NULL;
4654 g_autofree char *corefile = NULL;
4655 struct elf_note_info info;
4656 struct elfhdr elf;
4657 struct elf_phdr phdr;
4658 struct rlimit dumpsize;
4659 struct mm_struct *mm = NULL;
4660 off_t offset = 0, data_offset = 0;
4661 int segs = 0;
4662 int fd = -1;
4664 init_note_info(&info);
4666 errno = 0;
4667 getrlimit(RLIMIT_CORE, &dumpsize);
4668 if (dumpsize.rlim_cur == 0)
4669 return 0;
4671 corefile = core_dump_filename(ts);
4673 if ((fd = open(corefile, O_WRONLY | O_CREAT,
4674 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
4675 return (-errno);
4678 * Walk through target process memory mappings and
4679 * set up structure containing this information. After
4680 * this point vma_xxx functions can be used.
4682 if ((mm = vma_init()) == NULL)
4683 goto out;
4685 walk_memory_regions(mm, vma_walker);
4686 segs = vma_get_mapping_count(mm);
4689 * Construct valid coredump ELF header. We also
4690 * add one more segment for notes.
4692 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
4693 if (dump_write(fd, &elf, sizeof (elf)) != 0)
4694 goto out;
4696 /* fill in the in-memory version of notes */
4697 if (fill_note_info(&info, signr, env) < 0)
4698 goto out;
4700 offset += sizeof (elf); /* elf header */
4701 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
4703 /* write out notes program header */
4704 fill_elf_note_phdr(&phdr, info.notes_size, offset);
4706 offset += info.notes_size;
4707 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
4708 goto out;
4711 * ELF specification wants data to start at page boundary so
4712 * we align it here.
4714 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
4717 * Write program headers for memory regions mapped in
4718 * the target process.
4720 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4721 (void) memset(&phdr, 0, sizeof (phdr));
4723 phdr.p_type = PT_LOAD;
4724 phdr.p_offset = offset;
4725 phdr.p_vaddr = vma->vma_start;
4726 phdr.p_paddr = 0;
4727 phdr.p_filesz = vma_dump_size(vma);
4728 offset += phdr.p_filesz;
4729 phdr.p_memsz = vma->vma_end - vma->vma_start;
4730 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
4731 if (vma->vma_flags & PROT_WRITE)
4732 phdr.p_flags |= PF_W;
4733 if (vma->vma_flags & PROT_EXEC)
4734 phdr.p_flags |= PF_X;
4735 phdr.p_align = ELF_EXEC_PAGESIZE;
4737 bswap_phdr(&phdr, 1);
4738 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
4739 goto out;
4744 * Next we write notes just after program headers. No
4745 * alignment needed here.
4747 if (write_note_info(&info, fd) < 0)
4748 goto out;
4750 /* align data to page boundary */
4751 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4752 goto out;
4755 * Finally we can dump process memory into corefile as well.
4757 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4758 abi_ulong addr;
4759 abi_ulong end;
4761 end = vma->vma_start + vma_dump_size(vma);
4763 for (addr = vma->vma_start; addr < end;
4764 addr += TARGET_PAGE_SIZE) {
4765 char page[TARGET_PAGE_SIZE];
4766 int error;
4769 * Read in page from target process memory and
4770 * write it to coredump file.
4772 error = copy_from_user(page, addr, sizeof (page));
4773 if (error != 0) {
4774 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
4775 addr);
4776 errno = -error;
4777 goto out;
4779 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4780 goto out;
4784 out:
4785 free_note_info(&info);
4786 if (mm != NULL)
4787 vma_delete(mm);
4788 (void) close(fd);
4790 if (errno != 0)
4791 return (-errno);
4792 return (0);
4794 #endif /* USE_ELF_CORE_DUMP */
4796 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4798 init_thread(regs, infop);