target/i386: Fix physical address truncation
[qemu/ar7.git] / linux-user / elfload.c
blobb8eef893d0c77fa4acf23b971f132ec112c6dd2c
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/prctl.h>
6 #include <sys/resource.h>
7 #include <sys/shm.h>
9 #include "qemu.h"
10 #include "user-internals.h"
11 #include "signal-common.h"
12 #include "loader.h"
13 #include "user-mmap.h"
14 #include "disas/disas.h"
15 #include "qemu/bitops.h"
16 #include "qemu/path.h"
17 #include "qemu/queue.h"
18 #include "qemu/guest-random.h"
19 #include "qemu/units.h"
20 #include "qemu/selfmap.h"
21 #include "qemu/lockable.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
24 #include "target_signal.h"
25 #include "tcg/debuginfo.h"
27 #ifdef TARGET_ARM
28 #include "target/arm/cpu-features.h"
29 #endif
31 #ifdef _ARCH_PPC64
32 #undef ARCH_DLINFO
33 #undef ELF_PLATFORM
34 #undef ELF_HWCAP
35 #undef ELF_HWCAP2
36 #undef ELF_CLASS
37 #undef ELF_DATA
38 #undef ELF_ARCH
39 #endif
41 #ifndef TARGET_ARCH_HAS_SIGTRAMP_PAGE
42 #define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0
43 #endif
45 typedef struct {
46 const uint8_t *image;
47 const uint32_t *relocs;
48 unsigned image_size;
49 unsigned reloc_count;
50 unsigned sigreturn_ofs;
51 unsigned rt_sigreturn_ofs;
52 } VdsoImageInfo;
54 #define ELF_OSABI ELFOSABI_SYSV
56 /* from personality.h */
59 * Flags for bug emulation.
61 * These occupy the top three bytes.
63 enum {
64 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
65 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
66 descriptors (signal handling) */
67 MMAP_PAGE_ZERO = 0x0100000,
68 ADDR_COMPAT_LAYOUT = 0x0200000,
69 READ_IMPLIES_EXEC = 0x0400000,
70 ADDR_LIMIT_32BIT = 0x0800000,
71 SHORT_INODE = 0x1000000,
72 WHOLE_SECONDS = 0x2000000,
73 STICKY_TIMEOUTS = 0x4000000,
74 ADDR_LIMIT_3GB = 0x8000000,
78 * Personality types.
80 * These go in the low byte. Avoid using the top bit, it will
81 * conflict with error returns.
83 enum {
84 PER_LINUX = 0x0000,
85 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
86 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
87 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
88 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
89 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
90 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
91 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
92 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
93 PER_BSD = 0x0006,
94 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
95 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
96 PER_LINUX32 = 0x0008,
97 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
98 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
99 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
100 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
101 PER_RISCOS = 0x000c,
102 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
103 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
104 PER_OSF4 = 0x000f, /* OSF/1 v4 */
105 PER_HPUX = 0x0010,
106 PER_MASK = 0x00ff,
110 * Return the base personality without flags.
112 #define personality(pers) (pers & PER_MASK)
114 int info_is_fdpic(struct image_info *info)
116 return info->personality == PER_LINUX_FDPIC;
119 /* this flag is uneffective under linux too, should be deleted */
120 #ifndef MAP_DENYWRITE
121 #define MAP_DENYWRITE 0
122 #endif
124 /* should probably go in elf.h */
125 #ifndef ELIBBAD
126 #define ELIBBAD 80
127 #endif
129 #if TARGET_BIG_ENDIAN
130 #define ELF_DATA ELFDATA2MSB
131 #else
132 #define ELF_DATA ELFDATA2LSB
133 #endif
135 #ifdef TARGET_ABI_MIPSN32
136 typedef abi_ullong target_elf_greg_t;
137 #define tswapreg(ptr) tswap64(ptr)
138 #else
139 typedef abi_ulong target_elf_greg_t;
140 #define tswapreg(ptr) tswapal(ptr)
141 #endif
143 #ifdef USE_UID16
144 typedef abi_ushort target_uid_t;
145 typedef abi_ushort target_gid_t;
146 #else
147 typedef abi_uint target_uid_t;
148 typedef abi_uint target_gid_t;
149 #endif
150 typedef abi_int target_pid_t;
152 #ifdef TARGET_I386
154 #define ELF_HWCAP get_elf_hwcap()
156 static uint32_t get_elf_hwcap(void)
158 X86CPU *cpu = X86_CPU(thread_cpu);
160 return cpu->env.features[FEAT_1_EDX];
163 #ifdef TARGET_X86_64
164 #define ELF_CLASS ELFCLASS64
165 #define ELF_ARCH EM_X86_64
167 #define ELF_PLATFORM "x86_64"
169 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
171 regs->rax = 0;
172 regs->rsp = infop->start_stack;
173 regs->rip = infop->entry;
176 #define ELF_NREG 27
177 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
180 * Note that ELF_NREG should be 29 as there should be place for
181 * TRAPNO and ERR "registers" as well but linux doesn't dump
182 * those.
184 * See linux kernel: arch/x86/include/asm/elf.h
186 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
188 (*regs)[0] = tswapreg(env->regs[15]);
189 (*regs)[1] = tswapreg(env->regs[14]);
190 (*regs)[2] = tswapreg(env->regs[13]);
191 (*regs)[3] = tswapreg(env->regs[12]);
192 (*regs)[4] = tswapreg(env->regs[R_EBP]);
193 (*regs)[5] = tswapreg(env->regs[R_EBX]);
194 (*regs)[6] = tswapreg(env->regs[11]);
195 (*regs)[7] = tswapreg(env->regs[10]);
196 (*regs)[8] = tswapreg(env->regs[9]);
197 (*regs)[9] = tswapreg(env->regs[8]);
198 (*regs)[10] = tswapreg(env->regs[R_EAX]);
199 (*regs)[11] = tswapreg(env->regs[R_ECX]);
200 (*regs)[12] = tswapreg(env->regs[R_EDX]);
201 (*regs)[13] = tswapreg(env->regs[R_ESI]);
202 (*regs)[14] = tswapreg(env->regs[R_EDI]);
203 (*regs)[15] = tswapreg(env->regs[R_EAX]); /* XXX */
204 (*regs)[16] = tswapreg(env->eip);
205 (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff);
206 (*regs)[18] = tswapreg(env->eflags);
207 (*regs)[19] = tswapreg(env->regs[R_ESP]);
208 (*regs)[20] = tswapreg(env->segs[R_SS].selector & 0xffff);
209 (*regs)[21] = tswapreg(env->segs[R_FS].selector & 0xffff);
210 (*regs)[22] = tswapreg(env->segs[R_GS].selector & 0xffff);
211 (*regs)[23] = tswapreg(env->segs[R_DS].selector & 0xffff);
212 (*regs)[24] = tswapreg(env->segs[R_ES].selector & 0xffff);
213 (*regs)[25] = tswapreg(env->segs[R_FS].selector & 0xffff);
214 (*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff);
217 #if ULONG_MAX > UINT32_MAX
218 #define INIT_GUEST_COMMPAGE
219 static bool init_guest_commpage(void)
222 * The vsyscall page is at a high negative address aka kernel space,
223 * which means that we cannot actually allocate it with target_mmap.
224 * We still should be able to use page_set_flags, unless the user
225 * has specified -R reserved_va, which would trigger an assert().
227 if (reserved_va != 0 &&
228 TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE - 1 > reserved_va) {
229 error_report("Cannot allocate vsyscall page");
230 exit(EXIT_FAILURE);
232 page_set_flags(TARGET_VSYSCALL_PAGE,
233 TARGET_VSYSCALL_PAGE | ~TARGET_PAGE_MASK,
234 PAGE_EXEC | PAGE_VALID);
235 return true;
237 #endif
238 #else
241 * This is used to ensure we don't load something for the wrong architecture.
243 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
246 * These are used to set parameters in the core dumps.
248 #define ELF_CLASS ELFCLASS32
249 #define ELF_ARCH EM_386
251 #define ELF_PLATFORM get_elf_platform()
252 #define EXSTACK_DEFAULT true
254 static const char *get_elf_platform(void)
256 static char elf_platform[] = "i386";
257 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
258 if (family > 6) {
259 family = 6;
261 if (family >= 3) {
262 elf_platform[1] = '0' + family;
264 return elf_platform;
267 static inline void init_thread(struct target_pt_regs *regs,
268 struct image_info *infop)
270 regs->esp = infop->start_stack;
271 regs->eip = infop->entry;
273 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
274 starts %edx contains a pointer to a function which might be
275 registered using `atexit'. This provides a mean for the
276 dynamic linker to call DT_FINI functions for shared libraries
277 that have been loaded before the code runs.
279 A value of 0 tells we have no such handler. */
280 regs->edx = 0;
283 #define ELF_NREG 17
284 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
287 * Note that ELF_NREG should be 19 as there should be place for
288 * TRAPNO and ERR "registers" as well but linux doesn't dump
289 * those.
291 * See linux kernel: arch/x86/include/asm/elf.h
293 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
295 (*regs)[0] = tswapreg(env->regs[R_EBX]);
296 (*regs)[1] = tswapreg(env->regs[R_ECX]);
297 (*regs)[2] = tswapreg(env->regs[R_EDX]);
298 (*regs)[3] = tswapreg(env->regs[R_ESI]);
299 (*regs)[4] = tswapreg(env->regs[R_EDI]);
300 (*regs)[5] = tswapreg(env->regs[R_EBP]);
301 (*regs)[6] = tswapreg(env->regs[R_EAX]);
302 (*regs)[7] = tswapreg(env->segs[R_DS].selector & 0xffff);
303 (*regs)[8] = tswapreg(env->segs[R_ES].selector & 0xffff);
304 (*regs)[9] = tswapreg(env->segs[R_FS].selector & 0xffff);
305 (*regs)[10] = tswapreg(env->segs[R_GS].selector & 0xffff);
306 (*regs)[11] = tswapreg(env->regs[R_EAX]); /* XXX */
307 (*regs)[12] = tswapreg(env->eip);
308 (*regs)[13] = tswapreg(env->segs[R_CS].selector & 0xffff);
309 (*regs)[14] = tswapreg(env->eflags);
310 (*regs)[15] = tswapreg(env->regs[R_ESP]);
311 (*regs)[16] = tswapreg(env->segs[R_SS].selector & 0xffff);
315 * i386 is the only target which supplies AT_SYSINFO for the vdso.
316 * All others only supply AT_SYSINFO_EHDR.
318 #define DLINFO_ARCH_ITEMS (vdso_info != NULL)
319 #define ARCH_DLINFO \
320 do { \
321 if (vdso_info) { \
322 NEW_AUX_ENT(AT_SYSINFO, vdso_info->entry); \
324 } while (0)
326 #endif /* TARGET_X86_64 */
328 #define VDSO_HEADER "vdso.c.inc"
330 #define USE_ELF_CORE_DUMP
331 #define ELF_EXEC_PAGESIZE 4096
333 #endif /* TARGET_I386 */
335 #ifdef TARGET_ARM
337 #ifndef TARGET_AARCH64
338 /* 32 bit ARM definitions */
340 #define ELF_ARCH EM_ARM
341 #define ELF_CLASS ELFCLASS32
342 #define EXSTACK_DEFAULT true
344 static inline void init_thread(struct target_pt_regs *regs,
345 struct image_info *infop)
347 abi_long stack = infop->start_stack;
348 memset(regs, 0, sizeof(*regs));
350 regs->uregs[16] = ARM_CPU_MODE_USR;
351 if (infop->entry & 1) {
352 regs->uregs[16] |= CPSR_T;
354 regs->uregs[15] = infop->entry & 0xfffffffe;
355 regs->uregs[13] = infop->start_stack;
356 /* FIXME - what to for failure of get_user()? */
357 get_user_ual(regs->uregs[2], stack + 8); /* envp */
358 get_user_ual(regs->uregs[1], stack + 4); /* envp */
359 /* XXX: it seems that r0 is zeroed after ! */
360 regs->uregs[0] = 0;
361 /* For uClinux PIC binaries. */
362 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
363 regs->uregs[10] = infop->start_data;
365 /* Support ARM FDPIC. */
366 if (info_is_fdpic(infop)) {
367 /* As described in the ABI document, r7 points to the loadmap info
368 * prepared by the kernel. If an interpreter is needed, r8 points
369 * to the interpreter loadmap and r9 points to the interpreter
370 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
371 * r9 points to the main program PT_DYNAMIC info.
373 regs->uregs[7] = infop->loadmap_addr;
374 if (infop->interpreter_loadmap_addr) {
375 /* Executable is dynamically loaded. */
376 regs->uregs[8] = infop->interpreter_loadmap_addr;
377 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
378 } else {
379 regs->uregs[8] = 0;
380 regs->uregs[9] = infop->pt_dynamic_addr;
385 #define ELF_NREG 18
386 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
388 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
390 (*regs)[0] = tswapreg(env->regs[0]);
391 (*regs)[1] = tswapreg(env->regs[1]);
392 (*regs)[2] = tswapreg(env->regs[2]);
393 (*regs)[3] = tswapreg(env->regs[3]);
394 (*regs)[4] = tswapreg(env->regs[4]);
395 (*regs)[5] = tswapreg(env->regs[5]);
396 (*regs)[6] = tswapreg(env->regs[6]);
397 (*regs)[7] = tswapreg(env->regs[7]);
398 (*regs)[8] = tswapreg(env->regs[8]);
399 (*regs)[9] = tswapreg(env->regs[9]);
400 (*regs)[10] = tswapreg(env->regs[10]);
401 (*regs)[11] = tswapreg(env->regs[11]);
402 (*regs)[12] = tswapreg(env->regs[12]);
403 (*regs)[13] = tswapreg(env->regs[13]);
404 (*regs)[14] = tswapreg(env->regs[14]);
405 (*regs)[15] = tswapreg(env->regs[15]);
407 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
408 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
411 #define USE_ELF_CORE_DUMP
412 #define ELF_EXEC_PAGESIZE 4096
414 enum
416 ARM_HWCAP_ARM_SWP = 1 << 0,
417 ARM_HWCAP_ARM_HALF = 1 << 1,
418 ARM_HWCAP_ARM_THUMB = 1 << 2,
419 ARM_HWCAP_ARM_26BIT = 1 << 3,
420 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
421 ARM_HWCAP_ARM_FPA = 1 << 5,
422 ARM_HWCAP_ARM_VFP = 1 << 6,
423 ARM_HWCAP_ARM_EDSP = 1 << 7,
424 ARM_HWCAP_ARM_JAVA = 1 << 8,
425 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
426 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
427 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
428 ARM_HWCAP_ARM_NEON = 1 << 12,
429 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
430 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
431 ARM_HWCAP_ARM_TLS = 1 << 15,
432 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
433 ARM_HWCAP_ARM_IDIVA = 1 << 17,
434 ARM_HWCAP_ARM_IDIVT = 1 << 18,
435 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
436 ARM_HWCAP_ARM_LPAE = 1 << 20,
437 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
438 ARM_HWCAP_ARM_FPHP = 1 << 22,
439 ARM_HWCAP_ARM_ASIMDHP = 1 << 23,
440 ARM_HWCAP_ARM_ASIMDDP = 1 << 24,
441 ARM_HWCAP_ARM_ASIMDFHM = 1 << 25,
442 ARM_HWCAP_ARM_ASIMDBF16 = 1 << 26,
443 ARM_HWCAP_ARM_I8MM = 1 << 27,
446 enum {
447 ARM_HWCAP2_ARM_AES = 1 << 0,
448 ARM_HWCAP2_ARM_PMULL = 1 << 1,
449 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
450 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
451 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
452 ARM_HWCAP2_ARM_SB = 1 << 5,
453 ARM_HWCAP2_ARM_SSBS = 1 << 6,
456 /* The commpage only exists for 32 bit kernels */
458 #define HI_COMMPAGE (intptr_t)0xffff0f00u
460 static bool init_guest_commpage(void)
462 ARMCPU *cpu = ARM_CPU(thread_cpu);
463 abi_ptr commpage;
464 void *want;
465 void *addr;
468 * M-profile allocates maximum of 2GB address space, so can never
469 * allocate the commpage. Skip it.
471 if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
472 return true;
475 commpage = HI_COMMPAGE & -qemu_host_page_size;
476 want = g2h_untagged(commpage);
477 addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
478 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
480 if (addr == MAP_FAILED) {
481 perror("Allocating guest commpage");
482 exit(EXIT_FAILURE);
484 if (addr != want) {
485 return false;
488 /* Set kernel helper versions; rest of page is 0. */
489 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
491 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
492 perror("Protecting guest commpage");
493 exit(EXIT_FAILURE);
496 page_set_flags(commpage, commpage | ~qemu_host_page_mask,
497 PAGE_READ | PAGE_EXEC | PAGE_VALID);
498 return true;
501 #define ELF_HWCAP get_elf_hwcap()
502 #define ELF_HWCAP2 get_elf_hwcap2()
504 uint32_t get_elf_hwcap(void)
506 ARMCPU *cpu = ARM_CPU(thread_cpu);
507 uint32_t hwcaps = 0;
509 hwcaps |= ARM_HWCAP_ARM_SWP;
510 hwcaps |= ARM_HWCAP_ARM_HALF;
511 hwcaps |= ARM_HWCAP_ARM_THUMB;
512 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
514 /* probe for the extra features */
515 #define GET_FEATURE(feat, hwcap) \
516 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
518 #define GET_FEATURE_ID(feat, hwcap) \
519 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
521 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
522 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
523 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
524 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
525 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
526 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
527 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
528 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
529 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
530 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
532 if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
533 cpu_isar_feature(aa32_fpdp_v3, cpu)) {
534 hwcaps |= ARM_HWCAP_ARM_VFPv3;
535 if (cpu_isar_feature(aa32_simd_r32, cpu)) {
536 hwcaps |= ARM_HWCAP_ARM_VFPD32;
537 } else {
538 hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
541 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
543 * MVFR1.FPHP and .SIMDHP must be in sync, and QEMU uses the same
544 * isar_feature function for both. The kernel reports them as two hwcaps.
546 GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_FPHP);
547 GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_ASIMDHP);
548 GET_FEATURE_ID(aa32_dp, ARM_HWCAP_ARM_ASIMDDP);
549 GET_FEATURE_ID(aa32_fhm, ARM_HWCAP_ARM_ASIMDFHM);
550 GET_FEATURE_ID(aa32_bf16, ARM_HWCAP_ARM_ASIMDBF16);
551 GET_FEATURE_ID(aa32_i8mm, ARM_HWCAP_ARM_I8MM);
553 return hwcaps;
556 uint64_t get_elf_hwcap2(void)
558 ARMCPU *cpu = ARM_CPU(thread_cpu);
559 uint64_t hwcaps = 0;
561 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
562 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
563 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
564 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
565 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
566 GET_FEATURE_ID(aa32_sb, ARM_HWCAP2_ARM_SB);
567 GET_FEATURE_ID(aa32_ssbs, ARM_HWCAP2_ARM_SSBS);
568 return hwcaps;
571 const char *elf_hwcap_str(uint32_t bit)
573 static const char *hwcap_str[] = {
574 [__builtin_ctz(ARM_HWCAP_ARM_SWP )] = "swp",
575 [__builtin_ctz(ARM_HWCAP_ARM_HALF )] = "half",
576 [__builtin_ctz(ARM_HWCAP_ARM_THUMB )] = "thumb",
577 [__builtin_ctz(ARM_HWCAP_ARM_26BIT )] = "26bit",
578 [__builtin_ctz(ARM_HWCAP_ARM_FAST_MULT)] = "fast_mult",
579 [__builtin_ctz(ARM_HWCAP_ARM_FPA )] = "fpa",
580 [__builtin_ctz(ARM_HWCAP_ARM_VFP )] = "vfp",
581 [__builtin_ctz(ARM_HWCAP_ARM_EDSP )] = "edsp",
582 [__builtin_ctz(ARM_HWCAP_ARM_JAVA )] = "java",
583 [__builtin_ctz(ARM_HWCAP_ARM_IWMMXT )] = "iwmmxt",
584 [__builtin_ctz(ARM_HWCAP_ARM_CRUNCH )] = "crunch",
585 [__builtin_ctz(ARM_HWCAP_ARM_THUMBEE )] = "thumbee",
586 [__builtin_ctz(ARM_HWCAP_ARM_NEON )] = "neon",
587 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3 )] = "vfpv3",
588 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3D16 )] = "vfpv3d16",
589 [__builtin_ctz(ARM_HWCAP_ARM_TLS )] = "tls",
590 [__builtin_ctz(ARM_HWCAP_ARM_VFPv4 )] = "vfpv4",
591 [__builtin_ctz(ARM_HWCAP_ARM_IDIVA )] = "idiva",
592 [__builtin_ctz(ARM_HWCAP_ARM_IDIVT )] = "idivt",
593 [__builtin_ctz(ARM_HWCAP_ARM_VFPD32 )] = "vfpd32",
594 [__builtin_ctz(ARM_HWCAP_ARM_LPAE )] = "lpae",
595 [__builtin_ctz(ARM_HWCAP_ARM_EVTSTRM )] = "evtstrm",
596 [__builtin_ctz(ARM_HWCAP_ARM_FPHP )] = "fphp",
597 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDHP )] = "asimdhp",
598 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDDP )] = "asimddp",
599 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDFHM )] = "asimdfhm",
600 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDBF16)] = "asimdbf16",
601 [__builtin_ctz(ARM_HWCAP_ARM_I8MM )] = "i8mm",
604 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
607 const char *elf_hwcap2_str(uint32_t bit)
609 static const char *hwcap_str[] = {
610 [__builtin_ctz(ARM_HWCAP2_ARM_AES )] = "aes",
611 [__builtin_ctz(ARM_HWCAP2_ARM_PMULL)] = "pmull",
612 [__builtin_ctz(ARM_HWCAP2_ARM_SHA1 )] = "sha1",
613 [__builtin_ctz(ARM_HWCAP2_ARM_SHA2 )] = "sha2",
614 [__builtin_ctz(ARM_HWCAP2_ARM_CRC32)] = "crc32",
615 [__builtin_ctz(ARM_HWCAP2_ARM_SB )] = "sb",
616 [__builtin_ctz(ARM_HWCAP2_ARM_SSBS )] = "ssbs",
619 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
622 #undef GET_FEATURE
623 #undef GET_FEATURE_ID
625 #define ELF_PLATFORM get_elf_platform()
627 static const char *get_elf_platform(void)
629 CPUARMState *env = cpu_env(thread_cpu);
631 #if TARGET_BIG_ENDIAN
632 # define END "b"
633 #else
634 # define END "l"
635 #endif
637 if (arm_feature(env, ARM_FEATURE_V8)) {
638 return "v8" END;
639 } else if (arm_feature(env, ARM_FEATURE_V7)) {
640 if (arm_feature(env, ARM_FEATURE_M)) {
641 return "v7m" END;
642 } else {
643 return "v7" END;
645 } else if (arm_feature(env, ARM_FEATURE_V6)) {
646 return "v6" END;
647 } else if (arm_feature(env, ARM_FEATURE_V5)) {
648 return "v5" END;
649 } else {
650 return "v4" END;
653 #undef END
656 #else
657 /* 64 bit ARM definitions */
659 #define ELF_ARCH EM_AARCH64
660 #define ELF_CLASS ELFCLASS64
661 #if TARGET_BIG_ENDIAN
662 # define ELF_PLATFORM "aarch64_be"
663 #else
664 # define ELF_PLATFORM "aarch64"
665 #endif
667 static inline void init_thread(struct target_pt_regs *regs,
668 struct image_info *infop)
670 abi_long stack = infop->start_stack;
671 memset(regs, 0, sizeof(*regs));
673 regs->pc = infop->entry & ~0x3ULL;
674 regs->sp = stack;
677 #define ELF_NREG 34
678 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
680 static void elf_core_copy_regs(target_elf_gregset_t *regs,
681 const CPUARMState *env)
683 int i;
685 for (i = 0; i < 32; i++) {
686 (*regs)[i] = tswapreg(env->xregs[i]);
688 (*regs)[32] = tswapreg(env->pc);
689 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
692 #define USE_ELF_CORE_DUMP
693 #define ELF_EXEC_PAGESIZE 4096
695 enum {
696 ARM_HWCAP_A64_FP = 1 << 0,
697 ARM_HWCAP_A64_ASIMD = 1 << 1,
698 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
699 ARM_HWCAP_A64_AES = 1 << 3,
700 ARM_HWCAP_A64_PMULL = 1 << 4,
701 ARM_HWCAP_A64_SHA1 = 1 << 5,
702 ARM_HWCAP_A64_SHA2 = 1 << 6,
703 ARM_HWCAP_A64_CRC32 = 1 << 7,
704 ARM_HWCAP_A64_ATOMICS = 1 << 8,
705 ARM_HWCAP_A64_FPHP = 1 << 9,
706 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
707 ARM_HWCAP_A64_CPUID = 1 << 11,
708 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
709 ARM_HWCAP_A64_JSCVT = 1 << 13,
710 ARM_HWCAP_A64_FCMA = 1 << 14,
711 ARM_HWCAP_A64_LRCPC = 1 << 15,
712 ARM_HWCAP_A64_DCPOP = 1 << 16,
713 ARM_HWCAP_A64_SHA3 = 1 << 17,
714 ARM_HWCAP_A64_SM3 = 1 << 18,
715 ARM_HWCAP_A64_SM4 = 1 << 19,
716 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
717 ARM_HWCAP_A64_SHA512 = 1 << 21,
718 ARM_HWCAP_A64_SVE = 1 << 22,
719 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
720 ARM_HWCAP_A64_DIT = 1 << 24,
721 ARM_HWCAP_A64_USCAT = 1 << 25,
722 ARM_HWCAP_A64_ILRCPC = 1 << 26,
723 ARM_HWCAP_A64_FLAGM = 1 << 27,
724 ARM_HWCAP_A64_SSBS = 1 << 28,
725 ARM_HWCAP_A64_SB = 1 << 29,
726 ARM_HWCAP_A64_PACA = 1 << 30,
727 ARM_HWCAP_A64_PACG = 1UL << 31,
729 ARM_HWCAP2_A64_DCPODP = 1 << 0,
730 ARM_HWCAP2_A64_SVE2 = 1 << 1,
731 ARM_HWCAP2_A64_SVEAES = 1 << 2,
732 ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
733 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
734 ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
735 ARM_HWCAP2_A64_SVESM4 = 1 << 6,
736 ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
737 ARM_HWCAP2_A64_FRINT = 1 << 8,
738 ARM_HWCAP2_A64_SVEI8MM = 1 << 9,
739 ARM_HWCAP2_A64_SVEF32MM = 1 << 10,
740 ARM_HWCAP2_A64_SVEF64MM = 1 << 11,
741 ARM_HWCAP2_A64_SVEBF16 = 1 << 12,
742 ARM_HWCAP2_A64_I8MM = 1 << 13,
743 ARM_HWCAP2_A64_BF16 = 1 << 14,
744 ARM_HWCAP2_A64_DGH = 1 << 15,
745 ARM_HWCAP2_A64_RNG = 1 << 16,
746 ARM_HWCAP2_A64_BTI = 1 << 17,
747 ARM_HWCAP2_A64_MTE = 1 << 18,
748 ARM_HWCAP2_A64_ECV = 1 << 19,
749 ARM_HWCAP2_A64_AFP = 1 << 20,
750 ARM_HWCAP2_A64_RPRES = 1 << 21,
751 ARM_HWCAP2_A64_MTE3 = 1 << 22,
752 ARM_HWCAP2_A64_SME = 1 << 23,
753 ARM_HWCAP2_A64_SME_I16I64 = 1 << 24,
754 ARM_HWCAP2_A64_SME_F64F64 = 1 << 25,
755 ARM_HWCAP2_A64_SME_I8I32 = 1 << 26,
756 ARM_HWCAP2_A64_SME_F16F32 = 1 << 27,
757 ARM_HWCAP2_A64_SME_B16F32 = 1 << 28,
758 ARM_HWCAP2_A64_SME_F32F32 = 1 << 29,
759 ARM_HWCAP2_A64_SME_FA64 = 1 << 30,
760 ARM_HWCAP2_A64_WFXT = 1ULL << 31,
761 ARM_HWCAP2_A64_EBF16 = 1ULL << 32,
762 ARM_HWCAP2_A64_SVE_EBF16 = 1ULL << 33,
763 ARM_HWCAP2_A64_CSSC = 1ULL << 34,
764 ARM_HWCAP2_A64_RPRFM = 1ULL << 35,
765 ARM_HWCAP2_A64_SVE2P1 = 1ULL << 36,
766 ARM_HWCAP2_A64_SME2 = 1ULL << 37,
767 ARM_HWCAP2_A64_SME2P1 = 1ULL << 38,
768 ARM_HWCAP2_A64_SME_I16I32 = 1ULL << 39,
769 ARM_HWCAP2_A64_SME_BI32I32 = 1ULL << 40,
770 ARM_HWCAP2_A64_SME_B16B16 = 1ULL << 41,
771 ARM_HWCAP2_A64_SME_F16F16 = 1ULL << 42,
772 ARM_HWCAP2_A64_MOPS = 1ULL << 43,
773 ARM_HWCAP2_A64_HBC = 1ULL << 44,
776 #define ELF_HWCAP get_elf_hwcap()
777 #define ELF_HWCAP2 get_elf_hwcap2()
779 #define GET_FEATURE_ID(feat, hwcap) \
780 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
782 uint32_t get_elf_hwcap(void)
784 ARMCPU *cpu = ARM_CPU(thread_cpu);
785 uint32_t hwcaps = 0;
787 hwcaps |= ARM_HWCAP_A64_FP;
788 hwcaps |= ARM_HWCAP_A64_ASIMD;
789 hwcaps |= ARM_HWCAP_A64_CPUID;
791 /* probe for the extra features */
793 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
794 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
795 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
796 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
797 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
798 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
799 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
800 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
801 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
802 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
803 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
804 GET_FEATURE_ID(aa64_lse2, ARM_HWCAP_A64_USCAT);
805 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
806 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
807 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
808 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
809 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
810 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
811 GET_FEATURE_ID(aa64_dit, ARM_HWCAP_A64_DIT);
812 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
813 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
814 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
815 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
816 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
817 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
819 return hwcaps;
822 uint64_t get_elf_hwcap2(void)
824 ARMCPU *cpu = ARM_CPU(thread_cpu);
825 uint64_t hwcaps = 0;
827 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
828 GET_FEATURE_ID(aa64_sve2, ARM_HWCAP2_A64_SVE2);
829 GET_FEATURE_ID(aa64_sve2_aes, ARM_HWCAP2_A64_SVEAES);
830 GET_FEATURE_ID(aa64_sve2_pmull128, ARM_HWCAP2_A64_SVEPMULL);
831 GET_FEATURE_ID(aa64_sve2_bitperm, ARM_HWCAP2_A64_SVEBITPERM);
832 GET_FEATURE_ID(aa64_sve2_sha3, ARM_HWCAP2_A64_SVESHA3);
833 GET_FEATURE_ID(aa64_sve2_sm4, ARM_HWCAP2_A64_SVESM4);
834 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
835 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
836 GET_FEATURE_ID(aa64_sve_i8mm, ARM_HWCAP2_A64_SVEI8MM);
837 GET_FEATURE_ID(aa64_sve_f32mm, ARM_HWCAP2_A64_SVEF32MM);
838 GET_FEATURE_ID(aa64_sve_f64mm, ARM_HWCAP2_A64_SVEF64MM);
839 GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16);
840 GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM);
841 GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16);
842 GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG);
843 GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI);
844 GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE);
845 GET_FEATURE_ID(aa64_mte3, ARM_HWCAP2_A64_MTE3);
846 GET_FEATURE_ID(aa64_sme, (ARM_HWCAP2_A64_SME |
847 ARM_HWCAP2_A64_SME_F32F32 |
848 ARM_HWCAP2_A64_SME_B16F32 |
849 ARM_HWCAP2_A64_SME_F16F32 |
850 ARM_HWCAP2_A64_SME_I8I32));
851 GET_FEATURE_ID(aa64_sme_f64f64, ARM_HWCAP2_A64_SME_F64F64);
852 GET_FEATURE_ID(aa64_sme_i16i64, ARM_HWCAP2_A64_SME_I16I64);
853 GET_FEATURE_ID(aa64_sme_fa64, ARM_HWCAP2_A64_SME_FA64);
854 GET_FEATURE_ID(aa64_hbc, ARM_HWCAP2_A64_HBC);
855 GET_FEATURE_ID(aa64_mops, ARM_HWCAP2_A64_MOPS);
857 return hwcaps;
860 const char *elf_hwcap_str(uint32_t bit)
862 static const char *hwcap_str[] = {
863 [__builtin_ctz(ARM_HWCAP_A64_FP )] = "fp",
864 [__builtin_ctz(ARM_HWCAP_A64_ASIMD )] = "asimd",
865 [__builtin_ctz(ARM_HWCAP_A64_EVTSTRM )] = "evtstrm",
866 [__builtin_ctz(ARM_HWCAP_A64_AES )] = "aes",
867 [__builtin_ctz(ARM_HWCAP_A64_PMULL )] = "pmull",
868 [__builtin_ctz(ARM_HWCAP_A64_SHA1 )] = "sha1",
869 [__builtin_ctz(ARM_HWCAP_A64_SHA2 )] = "sha2",
870 [__builtin_ctz(ARM_HWCAP_A64_CRC32 )] = "crc32",
871 [__builtin_ctz(ARM_HWCAP_A64_ATOMICS )] = "atomics",
872 [__builtin_ctz(ARM_HWCAP_A64_FPHP )] = "fphp",
873 [__builtin_ctz(ARM_HWCAP_A64_ASIMDHP )] = "asimdhp",
874 [__builtin_ctz(ARM_HWCAP_A64_CPUID )] = "cpuid",
875 [__builtin_ctz(ARM_HWCAP_A64_ASIMDRDM)] = "asimdrdm",
876 [__builtin_ctz(ARM_HWCAP_A64_JSCVT )] = "jscvt",
877 [__builtin_ctz(ARM_HWCAP_A64_FCMA )] = "fcma",
878 [__builtin_ctz(ARM_HWCAP_A64_LRCPC )] = "lrcpc",
879 [__builtin_ctz(ARM_HWCAP_A64_DCPOP )] = "dcpop",
880 [__builtin_ctz(ARM_HWCAP_A64_SHA3 )] = "sha3",
881 [__builtin_ctz(ARM_HWCAP_A64_SM3 )] = "sm3",
882 [__builtin_ctz(ARM_HWCAP_A64_SM4 )] = "sm4",
883 [__builtin_ctz(ARM_HWCAP_A64_ASIMDDP )] = "asimddp",
884 [__builtin_ctz(ARM_HWCAP_A64_SHA512 )] = "sha512",
885 [__builtin_ctz(ARM_HWCAP_A64_SVE )] = "sve",
886 [__builtin_ctz(ARM_HWCAP_A64_ASIMDFHM)] = "asimdfhm",
887 [__builtin_ctz(ARM_HWCAP_A64_DIT )] = "dit",
888 [__builtin_ctz(ARM_HWCAP_A64_USCAT )] = "uscat",
889 [__builtin_ctz(ARM_HWCAP_A64_ILRCPC )] = "ilrcpc",
890 [__builtin_ctz(ARM_HWCAP_A64_FLAGM )] = "flagm",
891 [__builtin_ctz(ARM_HWCAP_A64_SSBS )] = "ssbs",
892 [__builtin_ctz(ARM_HWCAP_A64_SB )] = "sb",
893 [__builtin_ctz(ARM_HWCAP_A64_PACA )] = "paca",
894 [__builtin_ctz(ARM_HWCAP_A64_PACG )] = "pacg",
897 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
900 const char *elf_hwcap2_str(uint32_t bit)
902 static const char *hwcap_str[] = {
903 [__builtin_ctz(ARM_HWCAP2_A64_DCPODP )] = "dcpodp",
904 [__builtin_ctz(ARM_HWCAP2_A64_SVE2 )] = "sve2",
905 [__builtin_ctz(ARM_HWCAP2_A64_SVEAES )] = "sveaes",
906 [__builtin_ctz(ARM_HWCAP2_A64_SVEPMULL )] = "svepmull",
907 [__builtin_ctz(ARM_HWCAP2_A64_SVEBITPERM )] = "svebitperm",
908 [__builtin_ctz(ARM_HWCAP2_A64_SVESHA3 )] = "svesha3",
909 [__builtin_ctz(ARM_HWCAP2_A64_SVESM4 )] = "svesm4",
910 [__builtin_ctz(ARM_HWCAP2_A64_FLAGM2 )] = "flagm2",
911 [__builtin_ctz(ARM_HWCAP2_A64_FRINT )] = "frint",
912 [__builtin_ctz(ARM_HWCAP2_A64_SVEI8MM )] = "svei8mm",
913 [__builtin_ctz(ARM_HWCAP2_A64_SVEF32MM )] = "svef32mm",
914 [__builtin_ctz(ARM_HWCAP2_A64_SVEF64MM )] = "svef64mm",
915 [__builtin_ctz(ARM_HWCAP2_A64_SVEBF16 )] = "svebf16",
916 [__builtin_ctz(ARM_HWCAP2_A64_I8MM )] = "i8mm",
917 [__builtin_ctz(ARM_HWCAP2_A64_BF16 )] = "bf16",
918 [__builtin_ctz(ARM_HWCAP2_A64_DGH )] = "dgh",
919 [__builtin_ctz(ARM_HWCAP2_A64_RNG )] = "rng",
920 [__builtin_ctz(ARM_HWCAP2_A64_BTI )] = "bti",
921 [__builtin_ctz(ARM_HWCAP2_A64_MTE )] = "mte",
922 [__builtin_ctz(ARM_HWCAP2_A64_ECV )] = "ecv",
923 [__builtin_ctz(ARM_HWCAP2_A64_AFP )] = "afp",
924 [__builtin_ctz(ARM_HWCAP2_A64_RPRES )] = "rpres",
925 [__builtin_ctz(ARM_HWCAP2_A64_MTE3 )] = "mte3",
926 [__builtin_ctz(ARM_HWCAP2_A64_SME )] = "sme",
927 [__builtin_ctz(ARM_HWCAP2_A64_SME_I16I64 )] = "smei16i64",
928 [__builtin_ctz(ARM_HWCAP2_A64_SME_F64F64 )] = "smef64f64",
929 [__builtin_ctz(ARM_HWCAP2_A64_SME_I8I32 )] = "smei8i32",
930 [__builtin_ctz(ARM_HWCAP2_A64_SME_F16F32 )] = "smef16f32",
931 [__builtin_ctz(ARM_HWCAP2_A64_SME_B16F32 )] = "smeb16f32",
932 [__builtin_ctz(ARM_HWCAP2_A64_SME_F32F32 )] = "smef32f32",
933 [__builtin_ctz(ARM_HWCAP2_A64_SME_FA64 )] = "smefa64",
934 [__builtin_ctz(ARM_HWCAP2_A64_WFXT )] = "wfxt",
935 [__builtin_ctzll(ARM_HWCAP2_A64_EBF16 )] = "ebf16",
936 [__builtin_ctzll(ARM_HWCAP2_A64_SVE_EBF16 )] = "sveebf16",
937 [__builtin_ctzll(ARM_HWCAP2_A64_CSSC )] = "cssc",
938 [__builtin_ctzll(ARM_HWCAP2_A64_RPRFM )] = "rprfm",
939 [__builtin_ctzll(ARM_HWCAP2_A64_SVE2P1 )] = "sve2p1",
940 [__builtin_ctzll(ARM_HWCAP2_A64_SME2 )] = "sme2",
941 [__builtin_ctzll(ARM_HWCAP2_A64_SME2P1 )] = "sme2p1",
942 [__builtin_ctzll(ARM_HWCAP2_A64_SME_I16I32 )] = "smei16i32",
943 [__builtin_ctzll(ARM_HWCAP2_A64_SME_BI32I32)] = "smebi32i32",
944 [__builtin_ctzll(ARM_HWCAP2_A64_SME_B16B16 )] = "smeb16b16",
945 [__builtin_ctzll(ARM_HWCAP2_A64_SME_F16F16 )] = "smef16f16",
946 [__builtin_ctzll(ARM_HWCAP2_A64_MOPS )] = "mops",
947 [__builtin_ctzll(ARM_HWCAP2_A64_HBC )] = "hbc",
950 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
953 #undef GET_FEATURE_ID
955 #endif /* not TARGET_AARCH64 */
957 #if TARGET_BIG_ENDIAN
958 # define VDSO_HEADER "vdso-be.c.inc"
959 #else
960 # define VDSO_HEADER "vdso-le.c.inc"
961 #endif
963 #endif /* TARGET_ARM */
965 #ifdef TARGET_SPARC
966 #ifdef TARGET_SPARC64
968 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
969 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
970 #ifndef TARGET_ABI32
971 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
972 #else
973 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
974 #endif
976 #define ELF_CLASS ELFCLASS64
977 #define ELF_ARCH EM_SPARCV9
978 #else
979 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
980 | HWCAP_SPARC_MULDIV)
981 #define ELF_CLASS ELFCLASS32
982 #define ELF_ARCH EM_SPARC
983 #endif /* TARGET_SPARC64 */
985 static inline void init_thread(struct target_pt_regs *regs,
986 struct image_info *infop)
988 /* Note that target_cpu_copy_regs does not read psr/tstate. */
989 regs->pc = infop->entry;
990 regs->npc = regs->pc + 4;
991 regs->y = 0;
992 regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong)
993 - TARGET_STACK_BIAS);
995 #endif /* TARGET_SPARC */
997 #ifdef TARGET_PPC
999 #define ELF_MACHINE PPC_ELF_MACHINE
1001 #if defined(TARGET_PPC64)
1003 #define elf_check_arch(x) ( (x) == EM_PPC64 )
1005 #define ELF_CLASS ELFCLASS64
1007 #else
1009 #define ELF_CLASS ELFCLASS32
1010 #define EXSTACK_DEFAULT true
1012 #endif
1014 #define ELF_ARCH EM_PPC
1016 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
1017 See arch/powerpc/include/asm/cputable.h. */
1018 enum {
1019 QEMU_PPC_FEATURE_32 = 0x80000000,
1020 QEMU_PPC_FEATURE_64 = 0x40000000,
1021 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
1022 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
1023 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
1024 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
1025 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
1026 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
1027 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
1028 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
1029 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
1030 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
1031 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
1032 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
1033 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
1034 QEMU_PPC_FEATURE_CELL = 0x00010000,
1035 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
1036 QEMU_PPC_FEATURE_SMT = 0x00004000,
1037 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
1038 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
1039 QEMU_PPC_FEATURE_PA6T = 0x00000800,
1040 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
1041 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
1042 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
1043 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
1044 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
1046 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
1047 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
1049 /* Feature definitions in AT_HWCAP2. */
1050 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
1051 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
1052 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
1053 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
1054 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
1055 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
1056 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
1057 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
1058 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
1059 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
1060 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
1061 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
1062 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
1063 QEMU_PPC_FEATURE2_ARCH_3_1 = 0x00040000, /* ISA 3.1 */
1064 QEMU_PPC_FEATURE2_MMA = 0x00020000, /* Matrix-Multiply Assist */
1067 #define ELF_HWCAP get_elf_hwcap()
1069 static uint32_t get_elf_hwcap(void)
1071 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
1072 uint32_t features = 0;
1074 /* We don't have to be terribly complete here; the high points are
1075 Altivec/FP/SPE support. Anything else is just a bonus. */
1076 #define GET_FEATURE(flag, feature) \
1077 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1078 #define GET_FEATURE2(flags, feature) \
1079 do { \
1080 if ((cpu->env.insns_flags2 & flags) == flags) { \
1081 features |= feature; \
1083 } while (0)
1084 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
1085 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
1086 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
1087 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
1088 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
1089 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
1090 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
1091 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
1092 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
1093 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
1094 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
1095 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
1096 QEMU_PPC_FEATURE_ARCH_2_06);
1097 #undef GET_FEATURE
1098 #undef GET_FEATURE2
1100 return features;
1103 #define ELF_HWCAP2 get_elf_hwcap2()
1105 static uint32_t get_elf_hwcap2(void)
1107 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
1108 uint32_t features = 0;
1110 #define GET_FEATURE(flag, feature) \
1111 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1112 #define GET_FEATURE2(flag, feature) \
1113 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
1115 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
1116 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
1117 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
1118 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
1119 QEMU_PPC_FEATURE2_VEC_CRYPTO);
1120 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
1121 QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128);
1122 GET_FEATURE2(PPC2_ISA310, QEMU_PPC_FEATURE2_ARCH_3_1 |
1123 QEMU_PPC_FEATURE2_MMA);
1125 #undef GET_FEATURE
1126 #undef GET_FEATURE2
1128 return features;
1132 * The requirements here are:
1133 * - keep the final alignment of sp (sp & 0xf)
1134 * - make sure the 32-bit value at the first 16 byte aligned position of
1135 * AUXV is greater than 16 for glibc compatibility.
1136 * AT_IGNOREPPC is used for that.
1137 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
1138 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
1140 #define DLINFO_ARCH_ITEMS 5
1141 #define ARCH_DLINFO \
1142 do { \
1143 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
1144 /* \
1145 * Handle glibc compatibility: these magic entries must \
1146 * be at the lowest addresses in the final auxv. \
1147 */ \
1148 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1149 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1150 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
1151 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
1152 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
1153 } while (0)
1155 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
1157 _regs->gpr[1] = infop->start_stack;
1158 #if defined(TARGET_PPC64)
1159 if (get_ppc64_abi(infop) < 2) {
1160 uint64_t val;
1161 get_user_u64(val, infop->entry + 8);
1162 _regs->gpr[2] = val + infop->load_bias;
1163 get_user_u64(val, infop->entry);
1164 infop->entry = val + infop->load_bias;
1165 } else {
1166 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
1168 #endif
1169 _regs->nip = infop->entry;
1172 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
1173 #define ELF_NREG 48
1174 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1176 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
1178 int i;
1179 target_ulong ccr = 0;
1181 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
1182 (*regs)[i] = tswapreg(env->gpr[i]);
1185 (*regs)[32] = tswapreg(env->nip);
1186 (*regs)[33] = tswapreg(env->msr);
1187 (*regs)[35] = tswapreg(env->ctr);
1188 (*regs)[36] = tswapreg(env->lr);
1189 (*regs)[37] = tswapreg(cpu_read_xer(env));
1191 ccr = ppc_get_cr(env);
1192 (*regs)[38] = tswapreg(ccr);
1195 #define USE_ELF_CORE_DUMP
1196 #define ELF_EXEC_PAGESIZE 4096
1198 #ifndef TARGET_PPC64
1199 # define VDSO_HEADER "vdso-32.c.inc"
1200 #elif TARGET_BIG_ENDIAN
1201 # define VDSO_HEADER "vdso-64.c.inc"
1202 #else
1203 # define VDSO_HEADER "vdso-64le.c.inc"
1204 #endif
1206 #endif
1208 #ifdef TARGET_LOONGARCH64
1210 #define ELF_CLASS ELFCLASS64
1211 #define ELF_ARCH EM_LOONGARCH
1212 #define EXSTACK_DEFAULT true
1214 #define elf_check_arch(x) ((x) == EM_LOONGARCH)
1216 #define VDSO_HEADER "vdso.c.inc"
1218 static inline void init_thread(struct target_pt_regs *regs,
1219 struct image_info *infop)
1221 /*Set crmd PG,DA = 1,0 */
1222 regs->csr.crmd = 2 << 3;
1223 regs->csr.era = infop->entry;
1224 regs->regs[3] = infop->start_stack;
1227 /* See linux kernel: arch/loongarch/include/asm/elf.h */
1228 #define ELF_NREG 45
1229 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1231 enum {
1232 TARGET_EF_R0 = 0,
1233 TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33,
1234 TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34,
1237 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1238 const CPULoongArchState *env)
1240 int i;
1242 (*regs)[TARGET_EF_R0] = 0;
1244 for (i = 1; i < ARRAY_SIZE(env->gpr); i++) {
1245 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]);
1248 (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc);
1249 (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV);
1252 #define USE_ELF_CORE_DUMP
1253 #define ELF_EXEC_PAGESIZE 4096
1255 #define ELF_HWCAP get_elf_hwcap()
1257 /* See arch/loongarch/include/uapi/asm/hwcap.h */
1258 enum {
1259 HWCAP_LOONGARCH_CPUCFG = (1 << 0),
1260 HWCAP_LOONGARCH_LAM = (1 << 1),
1261 HWCAP_LOONGARCH_UAL = (1 << 2),
1262 HWCAP_LOONGARCH_FPU = (1 << 3),
1263 HWCAP_LOONGARCH_LSX = (1 << 4),
1264 HWCAP_LOONGARCH_LASX = (1 << 5),
1265 HWCAP_LOONGARCH_CRC32 = (1 << 6),
1266 HWCAP_LOONGARCH_COMPLEX = (1 << 7),
1267 HWCAP_LOONGARCH_CRYPTO = (1 << 8),
1268 HWCAP_LOONGARCH_LVZ = (1 << 9),
1269 HWCAP_LOONGARCH_LBT_X86 = (1 << 10),
1270 HWCAP_LOONGARCH_LBT_ARM = (1 << 11),
1271 HWCAP_LOONGARCH_LBT_MIPS = (1 << 12),
1274 static uint32_t get_elf_hwcap(void)
1276 LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu);
1277 uint32_t hwcaps = 0;
1279 hwcaps |= HWCAP_LOONGARCH_CRC32;
1281 if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) {
1282 hwcaps |= HWCAP_LOONGARCH_UAL;
1285 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) {
1286 hwcaps |= HWCAP_LOONGARCH_FPU;
1289 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) {
1290 hwcaps |= HWCAP_LOONGARCH_LAM;
1293 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LSX)) {
1294 hwcaps |= HWCAP_LOONGARCH_LSX;
1297 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LASX)) {
1298 hwcaps |= HWCAP_LOONGARCH_LASX;
1301 return hwcaps;
1304 #define ELF_PLATFORM "loongarch"
1306 #endif /* TARGET_LOONGARCH64 */
1308 #ifdef TARGET_MIPS
1310 #ifdef TARGET_MIPS64
1311 #define ELF_CLASS ELFCLASS64
1312 #else
1313 #define ELF_CLASS ELFCLASS32
1314 #endif
1315 #define ELF_ARCH EM_MIPS
1316 #define EXSTACK_DEFAULT true
1318 #ifdef TARGET_ABI_MIPSN32
1319 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1320 #else
1321 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1322 #endif
1324 #define ELF_BASE_PLATFORM get_elf_base_platform()
1326 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \
1327 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1328 { return _base_platform; } } while (0)
1330 static const char *get_elf_base_platform(void)
1332 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1334 /* 64 bit ISAs goes first */
1335 MATCH_PLATFORM_INSN(CPU_MIPS64R6, "mips64r6");
1336 MATCH_PLATFORM_INSN(CPU_MIPS64R5, "mips64r5");
1337 MATCH_PLATFORM_INSN(CPU_MIPS64R2, "mips64r2");
1338 MATCH_PLATFORM_INSN(CPU_MIPS64R1, "mips64");
1339 MATCH_PLATFORM_INSN(CPU_MIPS5, "mips5");
1340 MATCH_PLATFORM_INSN(CPU_MIPS4, "mips4");
1341 MATCH_PLATFORM_INSN(CPU_MIPS3, "mips3");
1343 /* 32 bit ISAs */
1344 MATCH_PLATFORM_INSN(CPU_MIPS32R6, "mips32r6");
1345 MATCH_PLATFORM_INSN(CPU_MIPS32R5, "mips32r5");
1346 MATCH_PLATFORM_INSN(CPU_MIPS32R2, "mips32r2");
1347 MATCH_PLATFORM_INSN(CPU_MIPS32R1, "mips32");
1348 MATCH_PLATFORM_INSN(CPU_MIPS2, "mips2");
1350 /* Fallback */
1351 return "mips";
1353 #undef MATCH_PLATFORM_INSN
1355 static inline void init_thread(struct target_pt_regs *regs,
1356 struct image_info *infop)
1358 regs->cp0_status = 2 << CP0St_KSU;
1359 regs->cp0_epc = infop->entry;
1360 regs->regs[29] = infop->start_stack;
1363 /* See linux kernel: arch/mips/include/asm/elf.h. */
1364 #define ELF_NREG 45
1365 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1367 /* See linux kernel: arch/mips/include/asm/reg.h. */
1368 enum {
1369 #ifdef TARGET_MIPS64
1370 TARGET_EF_R0 = 0,
1371 #else
1372 TARGET_EF_R0 = 6,
1373 #endif
1374 TARGET_EF_R26 = TARGET_EF_R0 + 26,
1375 TARGET_EF_R27 = TARGET_EF_R0 + 27,
1376 TARGET_EF_LO = TARGET_EF_R0 + 32,
1377 TARGET_EF_HI = TARGET_EF_R0 + 33,
1378 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
1379 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
1380 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
1381 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
1384 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1385 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
1387 int i;
1389 for (i = 0; i < TARGET_EF_R0; i++) {
1390 (*regs)[i] = 0;
1392 (*regs)[TARGET_EF_R0] = 0;
1394 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
1395 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
1398 (*regs)[TARGET_EF_R26] = 0;
1399 (*regs)[TARGET_EF_R27] = 0;
1400 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
1401 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
1402 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
1403 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
1404 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
1405 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
1408 #define USE_ELF_CORE_DUMP
1409 #define ELF_EXEC_PAGESIZE 4096
1411 /* See arch/mips/include/uapi/asm/hwcap.h. */
1412 enum {
1413 HWCAP_MIPS_R6 = (1 << 0),
1414 HWCAP_MIPS_MSA = (1 << 1),
1415 HWCAP_MIPS_CRC32 = (1 << 2),
1416 HWCAP_MIPS_MIPS16 = (1 << 3),
1417 HWCAP_MIPS_MDMX = (1 << 4),
1418 HWCAP_MIPS_MIPS3D = (1 << 5),
1419 HWCAP_MIPS_SMARTMIPS = (1 << 6),
1420 HWCAP_MIPS_DSP = (1 << 7),
1421 HWCAP_MIPS_DSP2 = (1 << 8),
1422 HWCAP_MIPS_DSP3 = (1 << 9),
1423 HWCAP_MIPS_MIPS16E2 = (1 << 10),
1424 HWCAP_LOONGSON_MMI = (1 << 11),
1425 HWCAP_LOONGSON_EXT = (1 << 12),
1426 HWCAP_LOONGSON_EXT2 = (1 << 13),
1427 HWCAP_LOONGSON_CPUCFG = (1 << 14),
1430 #define ELF_HWCAP get_elf_hwcap()
1432 #define GET_FEATURE_INSN(_flag, _hwcap) \
1433 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1435 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1436 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1438 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1439 do { \
1440 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1441 hwcaps |= _hwcap; \
1443 } while (0)
1445 static uint32_t get_elf_hwcap(void)
1447 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1448 uint32_t hwcaps = 0;
1450 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH,
1451 2, HWCAP_MIPS_R6);
1452 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA);
1453 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI);
1454 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT);
1456 return hwcaps;
1459 #undef GET_FEATURE_REG_EQU
1460 #undef GET_FEATURE_REG_SET
1461 #undef GET_FEATURE_INSN
1463 #endif /* TARGET_MIPS */
1465 #ifdef TARGET_MICROBLAZE
1467 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1469 #define ELF_CLASS ELFCLASS32
1470 #define ELF_ARCH EM_MICROBLAZE
1472 static inline void init_thread(struct target_pt_regs *regs,
1473 struct image_info *infop)
1475 regs->pc = infop->entry;
1476 regs->r1 = infop->start_stack;
1480 #define ELF_EXEC_PAGESIZE 4096
1482 #define USE_ELF_CORE_DUMP
1483 #define ELF_NREG 38
1484 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1486 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1487 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1489 int i, pos = 0;
1491 for (i = 0; i < 32; i++) {
1492 (*regs)[pos++] = tswapreg(env->regs[i]);
1495 (*regs)[pos++] = tswapreg(env->pc);
1496 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
1497 (*regs)[pos++] = 0;
1498 (*regs)[pos++] = tswapreg(env->ear);
1499 (*regs)[pos++] = 0;
1500 (*regs)[pos++] = tswapreg(env->esr);
1503 #endif /* TARGET_MICROBLAZE */
1505 #ifdef TARGET_NIOS2
1507 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1509 #define ELF_CLASS ELFCLASS32
1510 #define ELF_ARCH EM_ALTERA_NIOS2
1512 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1514 regs->ea = infop->entry;
1515 regs->sp = infop->start_stack;
1518 #define LO_COMMPAGE TARGET_PAGE_SIZE
1520 static bool init_guest_commpage(void)
1522 static const uint8_t kuser_page[4 + 2 * 64] = {
1523 /* __kuser_helper_version */
1524 [0x00] = 0x02, 0x00, 0x00, 0x00,
1526 /* __kuser_cmpxchg */
1527 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1528 0x3a, 0x28, 0x00, 0xf8, /* ret */
1530 /* __kuser_sigtramp */
1531 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1532 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1535 void *want = g2h_untagged(LO_COMMPAGE & -qemu_host_page_size);
1536 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
1537 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1539 if (addr == MAP_FAILED) {
1540 perror("Allocating guest commpage");
1541 exit(EXIT_FAILURE);
1543 if (addr != want) {
1544 return false;
1547 memcpy(addr, kuser_page, sizeof(kuser_page));
1549 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
1550 perror("Protecting guest commpage");
1551 exit(EXIT_FAILURE);
1554 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1555 PAGE_READ | PAGE_EXEC | PAGE_VALID);
1556 return true;
1559 #define ELF_EXEC_PAGESIZE 4096
1561 #define USE_ELF_CORE_DUMP
1562 #define ELF_NREG 49
1563 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1565 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1566 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1567 const CPUNios2State *env)
1569 int i;
1571 (*regs)[0] = -1;
1572 for (i = 1; i < 8; i++) /* r0-r7 */
1573 (*regs)[i] = tswapreg(env->regs[i + 7]);
1575 for (i = 8; i < 16; i++) /* r8-r15 */
1576 (*regs)[i] = tswapreg(env->regs[i - 8]);
1578 for (i = 16; i < 24; i++) /* r16-r23 */
1579 (*regs)[i] = tswapreg(env->regs[i + 7]);
1580 (*regs)[24] = -1; /* R_ET */
1581 (*regs)[25] = -1; /* R_BT */
1582 (*regs)[26] = tswapreg(env->regs[R_GP]);
1583 (*regs)[27] = tswapreg(env->regs[R_SP]);
1584 (*regs)[28] = tswapreg(env->regs[R_FP]);
1585 (*regs)[29] = tswapreg(env->regs[R_EA]);
1586 (*regs)[30] = -1; /* R_SSTATUS */
1587 (*regs)[31] = tswapreg(env->regs[R_RA]);
1589 (*regs)[32] = tswapreg(env->pc);
1591 (*regs)[33] = -1; /* R_STATUS */
1592 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1594 for (i = 35; i < 49; i++) /* ... */
1595 (*regs)[i] = -1;
1598 #endif /* TARGET_NIOS2 */
1600 #ifdef TARGET_OPENRISC
1602 #define ELF_ARCH EM_OPENRISC
1603 #define ELF_CLASS ELFCLASS32
1604 #define ELF_DATA ELFDATA2MSB
1606 static inline void init_thread(struct target_pt_regs *regs,
1607 struct image_info *infop)
1609 regs->pc = infop->entry;
1610 regs->gpr[1] = infop->start_stack;
1613 #define USE_ELF_CORE_DUMP
1614 #define ELF_EXEC_PAGESIZE 8192
1616 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1617 #define ELF_NREG 34 /* gprs and pc, sr */
1618 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1620 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1621 const CPUOpenRISCState *env)
1623 int i;
1625 for (i = 0; i < 32; i++) {
1626 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1628 (*regs)[32] = tswapreg(env->pc);
1629 (*regs)[33] = tswapreg(cpu_get_sr(env));
1631 #define ELF_HWCAP 0
1632 #define ELF_PLATFORM NULL
1634 #endif /* TARGET_OPENRISC */
1636 #ifdef TARGET_SH4
1638 #define ELF_CLASS ELFCLASS32
1639 #define ELF_ARCH EM_SH
1641 static inline void init_thread(struct target_pt_regs *regs,
1642 struct image_info *infop)
1644 /* Check other registers XXXXX */
1645 regs->pc = infop->entry;
1646 regs->regs[15] = infop->start_stack;
1649 /* See linux kernel: arch/sh/include/asm/elf.h. */
1650 #define ELF_NREG 23
1651 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1653 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1654 enum {
1655 TARGET_REG_PC = 16,
1656 TARGET_REG_PR = 17,
1657 TARGET_REG_SR = 18,
1658 TARGET_REG_GBR = 19,
1659 TARGET_REG_MACH = 20,
1660 TARGET_REG_MACL = 21,
1661 TARGET_REG_SYSCALL = 22
1664 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1665 const CPUSH4State *env)
1667 int i;
1669 for (i = 0; i < 16; i++) {
1670 (*regs)[i] = tswapreg(env->gregs[i]);
1673 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1674 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1675 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1676 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1677 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1678 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1679 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1682 #define USE_ELF_CORE_DUMP
1683 #define ELF_EXEC_PAGESIZE 4096
1685 enum {
1686 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1687 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1688 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1689 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1690 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1691 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1692 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1693 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1694 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1695 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1698 #define ELF_HWCAP get_elf_hwcap()
1700 static uint32_t get_elf_hwcap(void)
1702 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1703 uint32_t hwcap = 0;
1705 hwcap |= SH_CPU_HAS_FPU;
1707 if (cpu->env.features & SH_FEATURE_SH4A) {
1708 hwcap |= SH_CPU_HAS_LLSC;
1711 return hwcap;
1714 #endif
1716 #ifdef TARGET_CRIS
1718 #define ELF_CLASS ELFCLASS32
1719 #define ELF_ARCH EM_CRIS
1721 static inline void init_thread(struct target_pt_regs *regs,
1722 struct image_info *infop)
1724 regs->erp = infop->entry;
1727 #define ELF_EXEC_PAGESIZE 8192
1729 #endif
1731 #ifdef TARGET_M68K
1733 #define ELF_CLASS ELFCLASS32
1734 #define ELF_ARCH EM_68K
1736 /* ??? Does this need to do anything?
1737 #define ELF_PLAT_INIT(_r) */
1739 static inline void init_thread(struct target_pt_regs *regs,
1740 struct image_info *infop)
1742 regs->usp = infop->start_stack;
1743 regs->sr = 0;
1744 regs->pc = infop->entry;
1747 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1748 #define ELF_NREG 20
1749 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1751 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1753 (*regs)[0] = tswapreg(env->dregs[1]);
1754 (*regs)[1] = tswapreg(env->dregs[2]);
1755 (*regs)[2] = tswapreg(env->dregs[3]);
1756 (*regs)[3] = tswapreg(env->dregs[4]);
1757 (*regs)[4] = tswapreg(env->dregs[5]);
1758 (*regs)[5] = tswapreg(env->dregs[6]);
1759 (*regs)[6] = tswapreg(env->dregs[7]);
1760 (*regs)[7] = tswapreg(env->aregs[0]);
1761 (*regs)[8] = tswapreg(env->aregs[1]);
1762 (*regs)[9] = tswapreg(env->aregs[2]);
1763 (*regs)[10] = tswapreg(env->aregs[3]);
1764 (*regs)[11] = tswapreg(env->aregs[4]);
1765 (*regs)[12] = tswapreg(env->aregs[5]);
1766 (*regs)[13] = tswapreg(env->aregs[6]);
1767 (*regs)[14] = tswapreg(env->dregs[0]);
1768 (*regs)[15] = tswapreg(env->aregs[7]);
1769 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1770 (*regs)[17] = tswapreg(env->sr);
1771 (*regs)[18] = tswapreg(env->pc);
1772 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1775 #define USE_ELF_CORE_DUMP
1776 #define ELF_EXEC_PAGESIZE 8192
1778 #endif
1780 #ifdef TARGET_ALPHA
1782 #define ELF_CLASS ELFCLASS64
1783 #define ELF_ARCH EM_ALPHA
1785 static inline void init_thread(struct target_pt_regs *regs,
1786 struct image_info *infop)
1788 regs->pc = infop->entry;
1789 regs->ps = 8;
1790 regs->usp = infop->start_stack;
1793 #define ELF_EXEC_PAGESIZE 8192
1795 #endif /* TARGET_ALPHA */
1797 #ifdef TARGET_S390X
1799 #define ELF_CLASS ELFCLASS64
1800 #define ELF_DATA ELFDATA2MSB
1801 #define ELF_ARCH EM_S390
1803 #include "elf.h"
1805 #define ELF_HWCAP get_elf_hwcap()
1807 #define GET_FEATURE(_feat, _hwcap) \
1808 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1810 uint32_t get_elf_hwcap(void)
1813 * Let's assume we always have esan3 and zarch.
1814 * 31-bit processes can use 64-bit registers (high gprs).
1816 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1818 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1819 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1820 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1821 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1822 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1823 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1824 hwcap |= HWCAP_S390_ETF3EH;
1826 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1827 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT);
1828 GET_FEATURE(S390_FEAT_VECTOR_ENH2, HWCAP_S390_VXRS_EXT2);
1830 return hwcap;
1833 const char *elf_hwcap_str(uint32_t bit)
1835 static const char *hwcap_str[] = {
1836 [HWCAP_S390_NR_ESAN3] = "esan3",
1837 [HWCAP_S390_NR_ZARCH] = "zarch",
1838 [HWCAP_S390_NR_STFLE] = "stfle",
1839 [HWCAP_S390_NR_MSA] = "msa",
1840 [HWCAP_S390_NR_LDISP] = "ldisp",
1841 [HWCAP_S390_NR_EIMM] = "eimm",
1842 [HWCAP_S390_NR_DFP] = "dfp",
1843 [HWCAP_S390_NR_HPAGE] = "edat",
1844 [HWCAP_S390_NR_ETF3EH] = "etf3eh",
1845 [HWCAP_S390_NR_HIGH_GPRS] = "highgprs",
1846 [HWCAP_S390_NR_TE] = "te",
1847 [HWCAP_S390_NR_VXRS] = "vx",
1848 [HWCAP_S390_NR_VXRS_BCD] = "vxd",
1849 [HWCAP_S390_NR_VXRS_EXT] = "vxe",
1850 [HWCAP_S390_NR_GS] = "gs",
1851 [HWCAP_S390_NR_VXRS_EXT2] = "vxe2",
1852 [HWCAP_S390_NR_VXRS_PDE] = "vxp",
1853 [HWCAP_S390_NR_SORT] = "sort",
1854 [HWCAP_S390_NR_DFLT] = "dflt",
1855 [HWCAP_S390_NR_NNPA] = "nnpa",
1856 [HWCAP_S390_NR_PCI_MIO] = "pcimio",
1857 [HWCAP_S390_NR_SIE] = "sie",
1860 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
1863 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1865 regs->psw.addr = infop->entry;
1866 regs->psw.mask = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
1867 PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_MASK_64 | \
1868 PSW_MASK_32;
1869 regs->gprs[15] = infop->start_stack;
1872 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1873 #define ELF_NREG 27
1874 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1876 enum {
1877 TARGET_REG_PSWM = 0,
1878 TARGET_REG_PSWA = 1,
1879 TARGET_REG_GPRS = 2,
1880 TARGET_REG_ARS = 18,
1881 TARGET_REG_ORIG_R2 = 26,
1884 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1885 const CPUS390XState *env)
1887 int i;
1888 uint32_t *aregs;
1890 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask);
1891 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr);
1892 for (i = 0; i < 16; i++) {
1893 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]);
1895 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]);
1896 for (i = 0; i < 16; i++) {
1897 aregs[i] = tswap32(env->aregs[i]);
1899 (*regs)[TARGET_REG_ORIG_R2] = 0;
1902 #define USE_ELF_CORE_DUMP
1903 #define ELF_EXEC_PAGESIZE 4096
1905 #define VDSO_HEADER "vdso.c.inc"
1907 #endif /* TARGET_S390X */
1909 #ifdef TARGET_RISCV
1911 #define ELF_ARCH EM_RISCV
1913 #ifdef TARGET_RISCV32
1914 #define ELF_CLASS ELFCLASS32
1915 #define VDSO_HEADER "vdso-32.c.inc"
1916 #else
1917 #define ELF_CLASS ELFCLASS64
1918 #define VDSO_HEADER "vdso-64.c.inc"
1919 #endif
1921 #define ELF_HWCAP get_elf_hwcap()
1923 static uint32_t get_elf_hwcap(void)
1925 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1926 RISCVCPU *cpu = RISCV_CPU(thread_cpu);
1927 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1928 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C')
1929 | MISA_BIT('V');
1931 return cpu->env.misa_ext & mask;
1932 #undef MISA_BIT
1935 static inline void init_thread(struct target_pt_regs *regs,
1936 struct image_info *infop)
1938 regs->sepc = infop->entry;
1939 regs->sp = infop->start_stack;
1942 #define ELF_EXEC_PAGESIZE 4096
1944 #endif /* TARGET_RISCV */
1946 #ifdef TARGET_HPPA
1948 #define ELF_CLASS ELFCLASS32
1949 #define ELF_ARCH EM_PARISC
1950 #define ELF_PLATFORM "PARISC"
1951 #define STACK_GROWS_DOWN 0
1952 #define STACK_ALIGNMENT 64
1954 #define VDSO_HEADER "vdso.c.inc"
1956 static inline void init_thread(struct target_pt_regs *regs,
1957 struct image_info *infop)
1959 regs->iaoq[0] = infop->entry;
1960 regs->iaoq[1] = infop->entry + 4;
1961 regs->gr[23] = 0;
1962 regs->gr[24] = infop->argv;
1963 regs->gr[25] = infop->argc;
1964 /* The top-of-stack contains a linkage buffer. */
1965 regs->gr[30] = infop->start_stack + 64;
1966 regs->gr[31] = infop->entry;
1969 #define LO_COMMPAGE 0
1971 static bool init_guest_commpage(void)
1973 void *want = g2h_untagged(LO_COMMPAGE);
1974 void *addr = mmap(want, qemu_host_page_size, PROT_NONE,
1975 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1977 if (addr == MAP_FAILED) {
1978 perror("Allocating guest commpage");
1979 exit(EXIT_FAILURE);
1981 if (addr != want) {
1982 return false;
1986 * On Linux, page zero is normally marked execute only + gateway.
1987 * Normal read or write is supposed to fail (thus PROT_NONE above),
1988 * but specific offsets have kernel code mapped to raise permissions
1989 * and implement syscalls. Here, simply mark the page executable.
1990 * Special case the entry points during translation (see do_page_zero).
1992 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1993 PAGE_EXEC | PAGE_VALID);
1994 return true;
1997 #endif /* TARGET_HPPA */
1999 #ifdef TARGET_XTENSA
2001 #define ELF_CLASS ELFCLASS32
2002 #define ELF_ARCH EM_XTENSA
2004 static inline void init_thread(struct target_pt_regs *regs,
2005 struct image_info *infop)
2007 regs->windowbase = 0;
2008 regs->windowstart = 1;
2009 regs->areg[1] = infop->start_stack;
2010 regs->pc = infop->entry;
2011 if (info_is_fdpic(infop)) {
2012 regs->areg[4] = infop->loadmap_addr;
2013 regs->areg[5] = infop->interpreter_loadmap_addr;
2014 if (infop->interpreter_loadmap_addr) {
2015 regs->areg[6] = infop->interpreter_pt_dynamic_addr;
2016 } else {
2017 regs->areg[6] = infop->pt_dynamic_addr;
2022 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
2023 #define ELF_NREG 128
2024 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
2026 enum {
2027 TARGET_REG_PC,
2028 TARGET_REG_PS,
2029 TARGET_REG_LBEG,
2030 TARGET_REG_LEND,
2031 TARGET_REG_LCOUNT,
2032 TARGET_REG_SAR,
2033 TARGET_REG_WINDOWSTART,
2034 TARGET_REG_WINDOWBASE,
2035 TARGET_REG_THREADPTR,
2036 TARGET_REG_AR0 = 64,
2039 static void elf_core_copy_regs(target_elf_gregset_t *regs,
2040 const CPUXtensaState *env)
2042 unsigned i;
2044 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
2045 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
2046 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
2047 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
2048 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
2049 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
2050 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
2051 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
2052 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
2053 xtensa_sync_phys_from_window((CPUXtensaState *)env);
2054 for (i = 0; i < env->config->nareg; ++i) {
2055 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
2059 #define USE_ELF_CORE_DUMP
2060 #define ELF_EXEC_PAGESIZE 4096
2062 #endif /* TARGET_XTENSA */
2064 #ifdef TARGET_HEXAGON
2066 #define ELF_CLASS ELFCLASS32
2067 #define ELF_ARCH EM_HEXAGON
2069 static inline void init_thread(struct target_pt_regs *regs,
2070 struct image_info *infop)
2072 regs->sepc = infop->entry;
2073 regs->sp = infop->start_stack;
2076 #endif /* TARGET_HEXAGON */
2078 #ifndef ELF_BASE_PLATFORM
2079 #define ELF_BASE_PLATFORM (NULL)
2080 #endif
2082 #ifndef ELF_PLATFORM
2083 #define ELF_PLATFORM (NULL)
2084 #endif
2086 #ifndef ELF_MACHINE
2087 #define ELF_MACHINE ELF_ARCH
2088 #endif
2090 #ifndef elf_check_arch
2091 #define elf_check_arch(x) ((x) == ELF_ARCH)
2092 #endif
2094 #ifndef elf_check_abi
2095 #define elf_check_abi(x) (1)
2096 #endif
2098 #ifndef ELF_HWCAP
2099 #define ELF_HWCAP 0
2100 #endif
2102 #ifndef STACK_GROWS_DOWN
2103 #define STACK_GROWS_DOWN 1
2104 #endif
2106 #ifndef STACK_ALIGNMENT
2107 #define STACK_ALIGNMENT 16
2108 #endif
2110 #ifdef TARGET_ABI32
2111 #undef ELF_CLASS
2112 #define ELF_CLASS ELFCLASS32
2113 #undef bswaptls
2114 #define bswaptls(ptr) bswap32s(ptr)
2115 #endif
2117 #ifndef EXSTACK_DEFAULT
2118 #define EXSTACK_DEFAULT false
2119 #endif
2121 #include "elf.h"
2123 /* We must delay the following stanzas until after "elf.h". */
2124 #if defined(TARGET_AARCH64)
2126 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2127 const uint32_t *data,
2128 struct image_info *info,
2129 Error **errp)
2131 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
2132 if (pr_datasz != sizeof(uint32_t)) {
2133 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
2134 return false;
2136 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
2137 info->note_flags = *data;
2139 return true;
2141 #define ARCH_USE_GNU_PROPERTY 1
2143 #else
2145 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2146 const uint32_t *data,
2147 struct image_info *info,
2148 Error **errp)
2150 g_assert_not_reached();
2152 #define ARCH_USE_GNU_PROPERTY 0
2154 #endif
2156 struct exec
2158 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
2159 unsigned int a_text; /* length of text, in bytes */
2160 unsigned int a_data; /* length of data, in bytes */
2161 unsigned int a_bss; /* length of uninitialized data area, in bytes */
2162 unsigned int a_syms; /* length of symbol table data in file, in bytes */
2163 unsigned int a_entry; /* start address */
2164 unsigned int a_trsize; /* length of relocation info for text, in bytes */
2165 unsigned int a_drsize; /* length of relocation info for data, in bytes */
2169 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
2170 #define OMAGIC 0407
2171 #define NMAGIC 0410
2172 #define ZMAGIC 0413
2173 #define QMAGIC 0314
2175 #define DLINFO_ITEMS 16
2177 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
2179 memcpy(to, from, n);
2182 #ifdef BSWAP_NEEDED
2183 static void bswap_ehdr(struct elfhdr *ehdr)
2185 bswap16s(&ehdr->e_type); /* Object file type */
2186 bswap16s(&ehdr->e_machine); /* Architecture */
2187 bswap32s(&ehdr->e_version); /* Object file version */
2188 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
2189 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
2190 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
2191 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
2192 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
2193 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
2194 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
2195 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
2196 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
2197 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
2200 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
2202 int i;
2203 for (i = 0; i < phnum; ++i, ++phdr) {
2204 bswap32s(&phdr->p_type); /* Segment type */
2205 bswap32s(&phdr->p_flags); /* Segment flags */
2206 bswaptls(&phdr->p_offset); /* Segment file offset */
2207 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
2208 bswaptls(&phdr->p_paddr); /* Segment physical address */
2209 bswaptls(&phdr->p_filesz); /* Segment size in file */
2210 bswaptls(&phdr->p_memsz); /* Segment size in memory */
2211 bswaptls(&phdr->p_align); /* Segment alignment */
2215 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
2217 int i;
2218 for (i = 0; i < shnum; ++i, ++shdr) {
2219 bswap32s(&shdr->sh_name);
2220 bswap32s(&shdr->sh_type);
2221 bswaptls(&shdr->sh_flags);
2222 bswaptls(&shdr->sh_addr);
2223 bswaptls(&shdr->sh_offset);
2224 bswaptls(&shdr->sh_size);
2225 bswap32s(&shdr->sh_link);
2226 bswap32s(&shdr->sh_info);
2227 bswaptls(&shdr->sh_addralign);
2228 bswaptls(&shdr->sh_entsize);
2232 static void bswap_sym(struct elf_sym *sym)
2234 bswap32s(&sym->st_name);
2235 bswaptls(&sym->st_value);
2236 bswaptls(&sym->st_size);
2237 bswap16s(&sym->st_shndx);
2240 #ifdef TARGET_MIPS
2241 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
2243 bswap16s(&abiflags->version);
2244 bswap32s(&abiflags->ases);
2245 bswap32s(&abiflags->isa_ext);
2246 bswap32s(&abiflags->flags1);
2247 bswap32s(&abiflags->flags2);
2249 #endif
2250 #else
2251 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
2252 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
2253 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
2254 static inline void bswap_sym(struct elf_sym *sym) { }
2255 #ifdef TARGET_MIPS
2256 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
2257 #endif
2258 #endif
2260 #ifdef USE_ELF_CORE_DUMP
2261 static int elf_core_dump(int, const CPUArchState *);
2262 #endif /* USE_ELF_CORE_DUMP */
2263 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
2264 abi_ulong load_bias);
2266 /* Verify the portions of EHDR within E_IDENT for the target.
2267 This can be performed before bswapping the entire header. */
2268 static bool elf_check_ident(struct elfhdr *ehdr)
2270 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
2271 && ehdr->e_ident[EI_MAG1] == ELFMAG1
2272 && ehdr->e_ident[EI_MAG2] == ELFMAG2
2273 && ehdr->e_ident[EI_MAG3] == ELFMAG3
2274 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
2275 && ehdr->e_ident[EI_DATA] == ELF_DATA
2276 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
2279 /* Verify the portions of EHDR outside of E_IDENT for the target.
2280 This has to wait until after bswapping the header. */
2281 static bool elf_check_ehdr(struct elfhdr *ehdr)
2283 return (elf_check_arch(ehdr->e_machine)
2284 && elf_check_abi(ehdr->e_flags)
2285 && ehdr->e_ehsize == sizeof(struct elfhdr)
2286 && ehdr->e_phentsize == sizeof(struct elf_phdr)
2287 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
2291 * 'copy_elf_strings()' copies argument/envelope strings from user
2292 * memory to free pages in kernel mem. These are in a format ready
2293 * to be put directly into the top of new user memory.
2296 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
2297 abi_ulong p, abi_ulong stack_limit)
2299 char *tmp;
2300 int len, i;
2301 abi_ulong top = p;
2303 if (!p) {
2304 return 0; /* bullet-proofing */
2307 if (STACK_GROWS_DOWN) {
2308 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
2309 for (i = argc - 1; i >= 0; --i) {
2310 tmp = argv[i];
2311 if (!tmp) {
2312 fprintf(stderr, "VFS: argc is wrong");
2313 exit(-1);
2315 len = strlen(tmp) + 1;
2316 tmp += len;
2318 if (len > (p - stack_limit)) {
2319 return 0;
2321 while (len) {
2322 int bytes_to_copy = (len > offset) ? offset : len;
2323 tmp -= bytes_to_copy;
2324 p -= bytes_to_copy;
2325 offset -= bytes_to_copy;
2326 len -= bytes_to_copy;
2328 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
2330 if (offset == 0) {
2331 memcpy_to_target(p, scratch, top - p);
2332 top = p;
2333 offset = TARGET_PAGE_SIZE;
2337 if (p != top) {
2338 memcpy_to_target(p, scratch + offset, top - p);
2340 } else {
2341 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
2342 for (i = 0; i < argc; ++i) {
2343 tmp = argv[i];
2344 if (!tmp) {
2345 fprintf(stderr, "VFS: argc is wrong");
2346 exit(-1);
2348 len = strlen(tmp) + 1;
2349 if (len > (stack_limit - p)) {
2350 return 0;
2352 while (len) {
2353 int bytes_to_copy = (len > remaining) ? remaining : len;
2355 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
2357 tmp += bytes_to_copy;
2358 remaining -= bytes_to_copy;
2359 p += bytes_to_copy;
2360 len -= bytes_to_copy;
2362 if (remaining == 0) {
2363 memcpy_to_target(top, scratch, p - top);
2364 top = p;
2365 remaining = TARGET_PAGE_SIZE;
2369 if (p != top) {
2370 memcpy_to_target(top, scratch, p - top);
2374 return p;
2377 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2378 * argument/environment space. Newer kernels (>2.6.33) allow more,
2379 * dependent on stack size, but guarantee at least 32 pages for
2380 * backwards compatibility.
2382 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2384 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
2385 struct image_info *info)
2387 abi_ulong size, error, guard;
2388 int prot;
2390 size = guest_stack_size;
2391 if (size < STACK_LOWER_LIMIT) {
2392 size = STACK_LOWER_LIMIT;
2395 if (STACK_GROWS_DOWN) {
2396 guard = TARGET_PAGE_SIZE;
2397 if (guard < qemu_real_host_page_size()) {
2398 guard = qemu_real_host_page_size();
2400 } else {
2401 /* no guard page for hppa target where stack grows upwards. */
2402 guard = 0;
2405 prot = PROT_READ | PROT_WRITE;
2406 if (info->exec_stack) {
2407 prot |= PROT_EXEC;
2409 error = target_mmap(0, size + guard, prot,
2410 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2411 if (error == -1) {
2412 perror("mmap stack");
2413 exit(-1);
2416 /* We reserve one extra page at the top of the stack as guard. */
2417 if (STACK_GROWS_DOWN) {
2418 target_mprotect(error, guard, PROT_NONE);
2419 info->stack_limit = error + guard;
2420 return info->stack_limit + size - sizeof(void *);
2421 } else {
2422 info->stack_limit = error + size;
2423 return error;
2428 * zero_bss:
2430 * Map and zero the bss. We need to explicitly zero any fractional pages
2431 * after the data section (i.e. bss). Return false on mapping failure.
2433 static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss,
2434 int prot, Error **errp)
2436 abi_ulong align_bss;
2438 /* We only expect writable bss; the code segment shouldn't need this. */
2439 if (!(prot & PROT_WRITE)) {
2440 error_setg(errp, "PT_LOAD with non-writable bss");
2441 return false;
2444 align_bss = TARGET_PAGE_ALIGN(start_bss);
2445 end_bss = TARGET_PAGE_ALIGN(end_bss);
2447 if (start_bss < align_bss) {
2448 int flags = page_get_flags(start_bss);
2450 if (!(flags & PAGE_BITS)) {
2452 * The whole address space of the executable was reserved
2453 * at the start, therefore all pages will be VALID.
2454 * But assuming there are no PROT_NONE PT_LOAD segments,
2455 * a PROT_NONE page means no data all bss, and we can
2456 * simply extend the new anon mapping back to the start
2457 * of the page of bss.
2459 align_bss -= TARGET_PAGE_SIZE;
2460 } else {
2462 * The start of the bss shares a page with something.
2463 * The only thing that we expect is the data section,
2464 * which would already be marked writable.
2465 * Overlapping the RX code segment seems malformed.
2467 if (!(flags & PAGE_WRITE)) {
2468 error_setg(errp, "PT_LOAD with bss overlapping "
2469 "non-writable page");
2470 return false;
2473 /* The page is already mapped and writable. */
2474 memset(g2h_untagged(start_bss), 0, align_bss - start_bss);
2478 if (align_bss < end_bss &&
2479 target_mmap(align_bss, end_bss - align_bss, prot,
2480 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
2481 error_setg_errno(errp, errno, "Error mapping bss");
2482 return false;
2484 return true;
2487 #if defined(TARGET_ARM)
2488 static int elf_is_fdpic(struct elfhdr *exec)
2490 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
2492 #elif defined(TARGET_XTENSA)
2493 static int elf_is_fdpic(struct elfhdr *exec)
2495 return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC;
2497 #else
2498 /* Default implementation, always false. */
2499 static int elf_is_fdpic(struct elfhdr *exec)
2501 return 0;
2503 #endif
2505 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
2507 uint16_t n;
2508 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
2510 /* elf32_fdpic_loadseg */
2511 n = info->nsegs;
2512 while (n--) {
2513 sp -= 12;
2514 put_user_u32(loadsegs[n].addr, sp+0);
2515 put_user_u32(loadsegs[n].p_vaddr, sp+4);
2516 put_user_u32(loadsegs[n].p_memsz, sp+8);
2519 /* elf32_fdpic_loadmap */
2520 sp -= 4;
2521 put_user_u16(0, sp+0); /* version */
2522 put_user_u16(info->nsegs, sp+2); /* nsegs */
2524 info->personality = PER_LINUX_FDPIC;
2525 info->loadmap_addr = sp;
2527 return sp;
2530 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
2531 struct elfhdr *exec,
2532 struct image_info *info,
2533 struct image_info *interp_info,
2534 struct image_info *vdso_info)
2536 abi_ulong sp;
2537 abi_ulong u_argc, u_argv, u_envp, u_auxv;
2538 int size;
2539 int i;
2540 abi_ulong u_rand_bytes;
2541 uint8_t k_rand_bytes[16];
2542 abi_ulong u_platform, u_base_platform;
2543 const char *k_platform, *k_base_platform;
2544 const int n = sizeof(elf_addr_t);
2546 sp = p;
2548 /* Needs to be before we load the env/argc/... */
2549 if (elf_is_fdpic(exec)) {
2550 /* Need 4 byte alignment for these structs */
2551 sp &= ~3;
2552 sp = loader_build_fdpic_loadmap(info, sp);
2553 info->other_info = interp_info;
2554 if (interp_info) {
2555 interp_info->other_info = info;
2556 sp = loader_build_fdpic_loadmap(interp_info, sp);
2557 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
2558 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
2559 } else {
2560 info->interpreter_loadmap_addr = 0;
2561 info->interpreter_pt_dynamic_addr = 0;
2565 u_base_platform = 0;
2566 k_base_platform = ELF_BASE_PLATFORM;
2567 if (k_base_platform) {
2568 size_t len = strlen(k_base_platform) + 1;
2569 if (STACK_GROWS_DOWN) {
2570 sp -= (len + n - 1) & ~(n - 1);
2571 u_base_platform = sp;
2572 /* FIXME - check return value of memcpy_to_target() for failure */
2573 memcpy_to_target(sp, k_base_platform, len);
2574 } else {
2575 memcpy_to_target(sp, k_base_platform, len);
2576 u_base_platform = sp;
2577 sp += len + 1;
2581 u_platform = 0;
2582 k_platform = ELF_PLATFORM;
2583 if (k_platform) {
2584 size_t len = strlen(k_platform) + 1;
2585 if (STACK_GROWS_DOWN) {
2586 sp -= (len + n - 1) & ~(n - 1);
2587 u_platform = sp;
2588 /* FIXME - check return value of memcpy_to_target() for failure */
2589 memcpy_to_target(sp, k_platform, len);
2590 } else {
2591 memcpy_to_target(sp, k_platform, len);
2592 u_platform = sp;
2593 sp += len + 1;
2597 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2598 * the argv and envp pointers.
2600 if (STACK_GROWS_DOWN) {
2601 sp = QEMU_ALIGN_DOWN(sp, 16);
2602 } else {
2603 sp = QEMU_ALIGN_UP(sp, 16);
2607 * Generate 16 random bytes for userspace PRNG seeding.
2609 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
2610 if (STACK_GROWS_DOWN) {
2611 sp -= 16;
2612 u_rand_bytes = sp;
2613 /* FIXME - check return value of memcpy_to_target() for failure */
2614 memcpy_to_target(sp, k_rand_bytes, 16);
2615 } else {
2616 memcpy_to_target(sp, k_rand_bytes, 16);
2617 u_rand_bytes = sp;
2618 sp += 16;
2621 size = (DLINFO_ITEMS + 1) * 2;
2622 if (k_base_platform) {
2623 size += 2;
2625 if (k_platform) {
2626 size += 2;
2628 if (vdso_info) {
2629 size += 2;
2631 #ifdef DLINFO_ARCH_ITEMS
2632 size += DLINFO_ARCH_ITEMS * 2;
2633 #endif
2634 #ifdef ELF_HWCAP2
2635 size += 2;
2636 #endif
2637 info->auxv_len = size * n;
2639 size += envc + argc + 2;
2640 size += 1; /* argc itself */
2641 size *= n;
2643 /* Allocate space and finalize stack alignment for entry now. */
2644 if (STACK_GROWS_DOWN) {
2645 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
2646 sp = u_argc;
2647 } else {
2648 u_argc = sp;
2649 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2652 u_argv = u_argc + n;
2653 u_envp = u_argv + (argc + 1) * n;
2654 u_auxv = u_envp + (envc + 1) * n;
2655 info->saved_auxv = u_auxv;
2656 info->argc = argc;
2657 info->envc = envc;
2658 info->argv = u_argv;
2659 info->envp = u_envp;
2661 /* This is correct because Linux defines
2662 * elf_addr_t as Elf32_Off / Elf64_Off
2664 #define NEW_AUX_ENT(id, val) do { \
2665 put_user_ual(id, u_auxv); u_auxv += n; \
2666 put_user_ual(val, u_auxv); u_auxv += n; \
2667 } while(0)
2669 #ifdef ARCH_DLINFO
2671 * ARCH_DLINFO must come first so platform specific code can enforce
2672 * special alignment requirements on the AUXV if necessary (eg. PPC).
2674 ARCH_DLINFO;
2675 #endif
2676 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2677 * on info->auxv_len will trigger.
2679 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2680 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2681 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2682 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2683 /* Target doesn't support host page size alignment */
2684 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2685 } else {
2686 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2687 qemu_host_page_size)));
2689 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2690 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2691 NEW_AUX_ENT(AT_ENTRY, info->entry);
2692 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2693 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2694 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2695 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2696 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2697 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2698 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2699 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2700 NEW_AUX_ENT(AT_EXECFN, info->file_string);
2702 #ifdef ELF_HWCAP2
2703 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2704 #endif
2706 if (u_base_platform) {
2707 NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform);
2709 if (u_platform) {
2710 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2712 if (vdso_info) {
2713 NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr);
2715 NEW_AUX_ENT (AT_NULL, 0);
2716 #undef NEW_AUX_ENT
2718 /* Check that our initial calculation of the auxv length matches how much
2719 * we actually put into it.
2721 assert(info->auxv_len == u_auxv - info->saved_auxv);
2723 put_user_ual(argc, u_argc);
2725 p = info->arg_strings;
2726 for (i = 0; i < argc; ++i) {
2727 put_user_ual(p, u_argv);
2728 u_argv += n;
2729 p += target_strlen(p) + 1;
2731 put_user_ual(0, u_argv);
2733 p = info->env_strings;
2734 for (i = 0; i < envc; ++i) {
2735 put_user_ual(p, u_envp);
2736 u_envp += n;
2737 p += target_strlen(p) + 1;
2739 put_user_ual(0, u_envp);
2741 return sp;
2744 #if defined(HI_COMMPAGE)
2745 #define LO_COMMPAGE -1
2746 #elif defined(LO_COMMPAGE)
2747 #define HI_COMMPAGE 0
2748 #else
2749 #define HI_COMMPAGE 0
2750 #define LO_COMMPAGE -1
2751 #ifndef INIT_GUEST_COMMPAGE
2752 #define init_guest_commpage() true
2753 #endif
2754 #endif
2757 * pgb_try_mmap:
2758 * @addr: host start address
2759 * @addr_last: host last address
2760 * @keep: do not unmap the probe region
2762 * Return 1 if [@addr, @addr_last] is not mapped in the host,
2763 * return 0 if it is not available to map, and -1 on mmap error.
2764 * If @keep, the region is left mapped on success, otherwise unmapped.
2766 static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep)
2768 size_t size = addr_last - addr + 1;
2769 void *p = mmap((void *)addr, size, PROT_NONE,
2770 MAP_ANONYMOUS | MAP_PRIVATE |
2771 MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0);
2772 int ret;
2774 if (p == MAP_FAILED) {
2775 return errno == EEXIST ? 0 : -1;
2777 ret = p == (void *)addr;
2778 if (!keep || !ret) {
2779 munmap(p, size);
2781 return ret;
2785 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk)
2786 * @addr: host address
2787 * @addr_last: host last address
2788 * @brk: host brk
2790 * Like pgb_try_mmap, but additionally reserve some memory following brk.
2792 static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last,
2793 uintptr_t brk, bool keep)
2795 uintptr_t brk_last = brk + 16 * MiB - 1;
2797 /* Do not map anything close to the host brk. */
2798 if (addr <= brk_last && brk <= addr_last) {
2799 return 0;
2801 return pgb_try_mmap(addr, addr_last, keep);
2805 * pgb_try_mmap_set:
2806 * @ga: set of guest addrs
2807 * @base: guest_base
2808 * @brk: host brk
2810 * Return true if all @ga can be mapped by the host at @base.
2811 * On success, retain the mapping at index 0 for reserved_va.
2814 typedef struct PGBAddrs {
2815 uintptr_t bounds[3][2]; /* start/last pairs */
2816 int nbounds;
2817 } PGBAddrs;
2819 static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk)
2821 for (int i = ga->nbounds - 1; i >= 0; --i) {
2822 if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base,
2823 ga->bounds[i][1] + base,
2824 brk, i == 0 && reserved_va) <= 0) {
2825 return false;
2828 return true;
2832 * pgb_addr_set:
2833 * @ga: output set of guest addrs
2834 * @guest_loaddr: guest image low address
2835 * @guest_loaddr: guest image high address
2836 * @identity: create for identity mapping
2838 * Fill in @ga with the image, COMMPAGE and NULL page.
2840 static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr,
2841 abi_ulong guest_hiaddr, bool try_identity)
2843 int n;
2846 * With a low commpage, or a guest mapped very low,
2847 * we may not be able to use the identity map.
2849 if (try_identity) {
2850 if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) {
2851 return false;
2853 if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) {
2854 return false;
2858 memset(ga, 0, sizeof(*ga));
2859 n = 0;
2861 if (reserved_va) {
2862 ga->bounds[n][0] = try_identity ? mmap_min_addr : 0;
2863 ga->bounds[n][1] = reserved_va;
2864 n++;
2865 /* LO_COMMPAGE and NULL handled by reserving from 0. */
2866 } else {
2867 /* Add any LO_COMMPAGE or NULL page. */
2868 if (LO_COMMPAGE != -1) {
2869 ga->bounds[n][0] = 0;
2870 ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1;
2871 n++;
2872 } else if (!try_identity) {
2873 ga->bounds[n][0] = 0;
2874 ga->bounds[n][1] = TARGET_PAGE_SIZE - 1;
2875 n++;
2878 /* Add the guest image for ET_EXEC. */
2879 if (guest_loaddr) {
2880 ga->bounds[n][0] = guest_loaddr;
2881 ga->bounds[n][1] = guest_hiaddr;
2882 n++;
2887 * Temporarily disable
2888 * "comparison is always false due to limited range of data type"
2889 * due to comparison between unsigned and (possible) 0.
2891 #pragma GCC diagnostic push
2892 #pragma GCC diagnostic ignored "-Wtype-limits"
2894 /* Add any HI_COMMPAGE not covered by reserved_va. */
2895 if (reserved_va < HI_COMMPAGE) {
2896 ga->bounds[n][0] = HI_COMMPAGE & qemu_host_page_mask;
2897 ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1;
2898 n++;
2901 #pragma GCC diagnostic pop
2903 ga->nbounds = n;
2904 return true;
2907 static void pgb_fail_in_use(const char *image_name)
2909 error_report("%s: requires virtual address space that is in use "
2910 "(omit the -B option or choose a different value)",
2911 image_name);
2912 exit(EXIT_FAILURE);
2915 static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr,
2916 uintptr_t guest_hiaddr, uintptr_t align)
2918 PGBAddrs ga;
2919 uintptr_t brk = (uintptr_t)sbrk(0);
2921 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2922 fprintf(stderr, "Requested guest base %p does not satisfy "
2923 "host minimum alignment (0x%" PRIxPTR ")\n",
2924 (void *)guest_base, align);
2925 exit(EXIT_FAILURE);
2928 if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base)
2929 || !pgb_try_mmap_set(&ga, guest_base, brk)) {
2930 pgb_fail_in_use(image_name);
2935 * pgb_find_fallback:
2937 * This is a fallback method for finding holes in the host address space
2938 * if we don't have the benefit of being able to access /proc/self/map.
2939 * It can potentially take a very long time as we can only dumbly iterate
2940 * up the host address space seeing if the allocation would work.
2942 static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align,
2943 uintptr_t brk)
2945 /* TODO: come up with a better estimate of how much to skip. */
2946 uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB;
2948 for (uintptr_t base = skip; ; base += skip) {
2949 base = ROUND_UP(base, align);
2950 if (pgb_try_mmap_set(ga, base, brk)) {
2951 return base;
2953 if (base >= -skip) {
2954 return -1;
2959 static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base,
2960 IntervalTreeRoot *root)
2962 for (int i = ga->nbounds - 1; i >= 0; --i) {
2963 uintptr_t s = base + ga->bounds[i][0];
2964 uintptr_t l = base + ga->bounds[i][1];
2965 IntervalTreeNode *n;
2967 if (l < s) {
2968 /* Wraparound. Skip to advance S to mmap_min_addr. */
2969 return mmap_min_addr - s;
2972 n = interval_tree_iter_first(root, s, l);
2973 if (n != NULL) {
2974 /* Conflict. Skip to advance S to LAST + 1. */
2975 return n->last - s + 1;
2978 return 0; /* success */
2981 static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root,
2982 uintptr_t align, uintptr_t brk)
2984 uintptr_t last = mmap_min_addr;
2985 uintptr_t base, skip;
2987 while (true) {
2988 base = ROUND_UP(last, align);
2989 if (base < last) {
2990 return -1;
2993 skip = pgb_try_itree(ga, base, root);
2994 if (skip == 0) {
2995 break;
2998 last = base + skip;
2999 if (last < base) {
3000 return -1;
3005 * We've chosen 'base' based on holes in the interval tree,
3006 * but we don't yet know if it is a valid host address.
3007 * Because it is the first matching hole, if the host addresses
3008 * are invalid we know there are no further matches.
3010 return pgb_try_mmap_set(ga, base, brk) ? base : -1;
3013 static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr,
3014 uintptr_t guest_hiaddr, uintptr_t align)
3016 IntervalTreeRoot *root;
3017 uintptr_t brk, ret;
3018 PGBAddrs ga;
3020 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
3022 /* Try the identity map first. */
3023 if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {
3024 brk = (uintptr_t)sbrk(0);
3025 if (pgb_try_mmap_set(&ga, 0, brk)) {
3026 guest_base = 0;
3027 return;
3032 * Rebuild the address set for non-identity map.
3033 * This differs in the mapping of the guest NULL page.
3035 pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false);
3037 root = read_self_maps();
3039 /* Read brk after we've read the maps, which will malloc. */
3040 brk = (uintptr_t)sbrk(0);
3042 if (!root) {
3043 ret = pgb_find_fallback(&ga, align, brk);
3044 } else {
3046 * Reserve the area close to the host brk.
3047 * This will be freed with the rest of the tree.
3049 IntervalTreeNode *b = g_new0(IntervalTreeNode, 1);
3050 b->start = brk;
3051 b->last = brk + 16 * MiB - 1;
3052 interval_tree_insert(b, root);
3054 ret = pgb_find_itree(&ga, root, align, brk);
3055 free_self_maps(root);
3058 if (ret == -1) {
3059 int w = TARGET_LONG_BITS / 4;
3061 error_report("%s: Unable to find a guest_base to satisfy all "
3062 "guest address mapping requirements", image_name);
3064 for (int i = 0; i < ga.nbounds; ++i) {
3065 error_printf(" %0*" PRIx64 "-%0*" PRIx64 "\n",
3066 w, (uint64_t)ga.bounds[i][0],
3067 w, (uint64_t)ga.bounds[i][1]);
3069 exit(EXIT_FAILURE);
3071 guest_base = ret;
3074 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
3075 abi_ulong guest_hiaddr)
3077 /* In order to use host shmat, we must be able to honor SHMLBA. */
3078 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
3080 /* Sanity check the guest binary. */
3081 if (reserved_va) {
3082 if (guest_hiaddr > reserved_va) {
3083 error_report("%s: requires more than reserved virtual "
3084 "address space (0x%" PRIx64 " > 0x%lx)",
3085 image_name, (uint64_t)guest_hiaddr, reserved_va);
3086 exit(EXIT_FAILURE);
3088 } else {
3089 if (guest_hiaddr != (uintptr_t)guest_hiaddr) {
3090 error_report("%s: requires more virtual address space "
3091 "than the host can provide (0x%" PRIx64 ")",
3092 image_name, (uint64_t)guest_hiaddr + 1);
3093 exit(EXIT_FAILURE);
3097 if (have_guest_base) {
3098 pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align);
3099 } else {
3100 pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align);
3103 /* Reserve and initialize the commpage. */
3104 if (!init_guest_commpage()) {
3105 /* We have already probed for the commpage being free. */
3106 g_assert_not_reached();
3109 assert(QEMU_IS_ALIGNED(guest_base, align));
3110 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
3111 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
3114 enum {
3115 /* The string "GNU\0" as a magic number. */
3116 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
3117 NOTE_DATA_SZ = 1 * KiB,
3118 NOTE_NAME_SZ = 4,
3119 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
3123 * Process a single gnu_property entry.
3124 * Return false for error.
3126 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
3127 struct image_info *info, bool have_prev_type,
3128 uint32_t *prev_type, Error **errp)
3130 uint32_t pr_type, pr_datasz, step;
3132 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
3133 goto error_data;
3135 datasz -= *off;
3136 data += *off / sizeof(uint32_t);
3138 if (datasz < 2 * sizeof(uint32_t)) {
3139 goto error_data;
3141 pr_type = data[0];
3142 pr_datasz = data[1];
3143 data += 2;
3144 datasz -= 2 * sizeof(uint32_t);
3145 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
3146 if (step > datasz) {
3147 goto error_data;
3150 /* Properties are supposed to be unique and sorted on pr_type. */
3151 if (have_prev_type && pr_type <= *prev_type) {
3152 if (pr_type == *prev_type) {
3153 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
3154 } else {
3155 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
3157 return false;
3159 *prev_type = pr_type;
3161 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
3162 return false;
3165 *off += 2 * sizeof(uint32_t) + step;
3166 return true;
3168 error_data:
3169 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
3170 return false;
3173 /* Process NT_GNU_PROPERTY_TYPE_0. */
3174 static bool parse_elf_properties(const ImageSource *src,
3175 struct image_info *info,
3176 const struct elf_phdr *phdr,
3177 Error **errp)
3179 union {
3180 struct elf_note nhdr;
3181 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
3182 } note;
3184 int n, off, datasz;
3185 bool have_prev_type;
3186 uint32_t prev_type;
3188 /* Unless the arch requires properties, ignore them. */
3189 if (!ARCH_USE_GNU_PROPERTY) {
3190 return true;
3193 /* If the properties are crazy large, that's too bad. */
3194 n = phdr->p_filesz;
3195 if (n > sizeof(note)) {
3196 error_setg(errp, "PT_GNU_PROPERTY too large");
3197 return false;
3199 if (n < sizeof(note.nhdr)) {
3200 error_setg(errp, "PT_GNU_PROPERTY too small");
3201 return false;
3204 if (!imgsrc_read(&note, phdr->p_offset, n, src, errp)) {
3205 return false;
3209 * The contents of a valid PT_GNU_PROPERTY is a sequence
3210 * of uint32_t -- swap them all now.
3212 #ifdef BSWAP_NEEDED
3213 for (int i = 0; i < n / 4; i++) {
3214 bswap32s(note.data + i);
3216 #endif
3219 * Note that nhdr is 3 words, and that the "name" described by namesz
3220 * immediately follows nhdr and is thus at the 4th word. Further, all
3221 * of the inputs to the kernel's round_up are multiples of 4.
3223 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
3224 note.nhdr.n_namesz != NOTE_NAME_SZ ||
3225 note.data[3] != GNU0_MAGIC) {
3226 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
3227 return false;
3229 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
3231 datasz = note.nhdr.n_descsz + off;
3232 if (datasz > n) {
3233 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
3234 return false;
3237 have_prev_type = false;
3238 prev_type = 0;
3239 while (1) {
3240 if (off == datasz) {
3241 return true; /* end, exit ok */
3243 if (!parse_elf_property(note.data, &off, datasz, info,
3244 have_prev_type, &prev_type, errp)) {
3245 return false;
3247 have_prev_type = true;
3252 * load_elf_image: Load an ELF image into the address space.
3253 * @image_name: the filename of the image, to use in error messages.
3254 * @src: the ImageSource from which to read.
3255 * @info: info collected from the loaded image.
3256 * @ehdr: the ELF header, not yet bswapped.
3257 * @pinterp_name: record any PT_INTERP string found.
3259 * On return: @info values will be filled in, as necessary or available.
3262 static void load_elf_image(const char *image_name, const ImageSource *src,
3263 struct image_info *info, struct elfhdr *ehdr,
3264 char **pinterp_name)
3266 g_autofree struct elf_phdr *phdr = NULL;
3267 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
3268 int i, prot_exec;
3269 Error *err = NULL;
3272 * First of all, some simple consistency checks.
3273 * Note that we rely on the bswapped ehdr staying in bprm_buf,
3274 * for later use by load_elf_binary and create_elf_tables.
3276 if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) {
3277 goto exit_errmsg;
3279 if (!elf_check_ident(ehdr)) {
3280 error_setg(&err, "Invalid ELF image for this architecture");
3281 goto exit_errmsg;
3283 bswap_ehdr(ehdr);
3284 if (!elf_check_ehdr(ehdr)) {
3285 error_setg(&err, "Invalid ELF image for this architecture");
3286 goto exit_errmsg;
3289 phdr = imgsrc_read_alloc(ehdr->e_phoff,
3290 ehdr->e_phnum * sizeof(struct elf_phdr),
3291 src, &err);
3292 if (phdr == NULL) {
3293 goto exit_errmsg;
3295 bswap_phdr(phdr, ehdr->e_phnum);
3297 info->nsegs = 0;
3298 info->pt_dynamic_addr = 0;
3300 mmap_lock();
3303 * Find the maximum size of the image and allocate an appropriate
3304 * amount of memory to handle that. Locate the interpreter, if any.
3306 loaddr = -1, hiaddr = 0;
3307 info->alignment = 0;
3308 info->exec_stack = EXSTACK_DEFAULT;
3309 for (i = 0; i < ehdr->e_phnum; ++i) {
3310 struct elf_phdr *eppnt = phdr + i;
3311 if (eppnt->p_type == PT_LOAD) {
3312 abi_ulong a = eppnt->p_vaddr & TARGET_PAGE_MASK;
3313 if (a < loaddr) {
3314 loaddr = a;
3316 a = eppnt->p_vaddr + eppnt->p_memsz - 1;
3317 if (a > hiaddr) {
3318 hiaddr = a;
3320 ++info->nsegs;
3321 info->alignment |= eppnt->p_align;
3322 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
3323 g_autofree char *interp_name = NULL;
3325 if (*pinterp_name) {
3326 error_setg(&err, "Multiple PT_INTERP entries");
3327 goto exit_errmsg;
3330 interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz,
3331 src, &err);
3332 if (interp_name == NULL) {
3333 goto exit_errmsg;
3335 if (interp_name[eppnt->p_filesz - 1] != 0) {
3336 error_setg(&err, "Invalid PT_INTERP entry");
3337 goto exit_errmsg;
3339 *pinterp_name = g_steal_pointer(&interp_name);
3340 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
3341 if (!parse_elf_properties(src, info, eppnt, &err)) {
3342 goto exit_errmsg;
3344 } else if (eppnt->p_type == PT_GNU_STACK) {
3345 info->exec_stack = eppnt->p_flags & PF_X;
3349 load_addr = loaddr;
3351 if (pinterp_name != NULL) {
3352 if (ehdr->e_type == ET_EXEC) {
3354 * Make sure that the low address does not conflict with
3355 * MMAP_MIN_ADDR or the QEMU application itself.
3357 probe_guest_base(image_name, loaddr, hiaddr);
3358 } else {
3359 abi_ulong align;
3362 * The binary is dynamic, but we still need to
3363 * select guest_base. In this case we pass a size.
3365 probe_guest_base(image_name, 0, hiaddr - loaddr);
3368 * Avoid collision with the loader by providing a different
3369 * default load address.
3371 load_addr += elf_et_dyn_base;
3374 * TODO: Better support for mmap alignment is desirable.
3375 * Since we do not have complete control over the guest
3376 * address space, we prefer the kernel to choose some address
3377 * rather than force the use of LOAD_ADDR via MAP_FIXED.
3378 * But without MAP_FIXED we cannot guarantee alignment,
3379 * only suggest it.
3381 align = pow2ceil(info->alignment);
3382 if (align) {
3383 load_addr &= -align;
3389 * Reserve address space for all of this.
3391 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get
3392 * exactly the address range that is required. Without reserved_va,
3393 * the guest address space is not isolated. We have attempted to avoid
3394 * conflict with the host program itself via probe_guest_base, but using
3395 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check.
3397 * Otherwise this is ET_DYN, and we are searching for a location
3398 * that can hold the memory space required. If the image is
3399 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should
3400 * honor that address if it happens to be free.
3402 * In both cases, we will overwrite pages in this range with mappings
3403 * from the executable.
3405 load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE,
3406 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
3407 (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0),
3408 -1, 0);
3409 if (load_addr == -1) {
3410 goto exit_mmap;
3412 load_bias = load_addr - loaddr;
3414 if (elf_is_fdpic(ehdr)) {
3415 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
3416 g_malloc(sizeof(*loadsegs) * info->nsegs);
3418 for (i = 0; i < ehdr->e_phnum; ++i) {
3419 switch (phdr[i].p_type) {
3420 case PT_DYNAMIC:
3421 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
3422 break;
3423 case PT_LOAD:
3424 loadsegs->addr = phdr[i].p_vaddr + load_bias;
3425 loadsegs->p_vaddr = phdr[i].p_vaddr;
3426 loadsegs->p_memsz = phdr[i].p_memsz;
3427 ++loadsegs;
3428 break;
3433 info->load_bias = load_bias;
3434 info->code_offset = load_bias;
3435 info->data_offset = load_bias;
3436 info->load_addr = load_addr;
3437 info->entry = ehdr->e_entry + load_bias;
3438 info->start_code = -1;
3439 info->end_code = 0;
3440 info->start_data = -1;
3441 info->end_data = 0;
3442 /* Usual start for brk is after all sections of the main executable. */
3443 info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
3444 info->elf_flags = ehdr->e_flags;
3446 prot_exec = PROT_EXEC;
3447 #ifdef TARGET_AARCH64
3449 * If the BTI feature is present, this indicates that the executable
3450 * pages of the startup binary should be mapped with PROT_BTI, so that
3451 * branch targets are enforced.
3453 * The startup binary is either the interpreter or the static executable.
3454 * The interpreter is responsible for all pages of a dynamic executable.
3456 * Elf notes are backward compatible to older cpus.
3457 * Do not enable BTI unless it is supported.
3459 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
3460 && (pinterp_name == NULL || *pinterp_name == 0)
3461 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
3462 prot_exec |= TARGET_PROT_BTI;
3464 #endif
3466 for (i = 0; i < ehdr->e_phnum; i++) {
3467 struct elf_phdr *eppnt = phdr + i;
3468 if (eppnt->p_type == PT_LOAD) {
3469 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
3470 int elf_prot = 0;
3472 if (eppnt->p_flags & PF_R) {
3473 elf_prot |= PROT_READ;
3475 if (eppnt->p_flags & PF_W) {
3476 elf_prot |= PROT_WRITE;
3478 if (eppnt->p_flags & PF_X) {
3479 elf_prot |= prot_exec;
3482 vaddr = load_bias + eppnt->p_vaddr;
3483 vaddr_po = vaddr & ~TARGET_PAGE_MASK;
3484 vaddr_ps = vaddr & TARGET_PAGE_MASK;
3486 vaddr_ef = vaddr + eppnt->p_filesz;
3487 vaddr_em = vaddr + eppnt->p_memsz;
3490 * Some segments may be completely empty, with a non-zero p_memsz
3491 * but no backing file segment.
3493 if (eppnt->p_filesz != 0) {
3494 error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
3495 elf_prot, MAP_PRIVATE | MAP_FIXED,
3496 src, eppnt->p_offset - vaddr_po);
3497 if (error == -1) {
3498 goto exit_mmap;
3502 /* If the load segment requests extra zeros (e.g. bss), map it. */
3503 if (vaddr_ef < vaddr_em &&
3504 !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) {
3505 goto exit_errmsg;
3508 /* Find the full program boundaries. */
3509 if (elf_prot & PROT_EXEC) {
3510 if (vaddr < info->start_code) {
3511 info->start_code = vaddr;
3513 if (vaddr_ef > info->end_code) {
3514 info->end_code = vaddr_ef;
3517 if (elf_prot & PROT_WRITE) {
3518 if (vaddr < info->start_data) {
3519 info->start_data = vaddr;
3521 if (vaddr_ef > info->end_data) {
3522 info->end_data = vaddr_ef;
3525 #ifdef TARGET_MIPS
3526 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
3527 Mips_elf_abiflags_v0 abiflags;
3529 if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags),
3530 src, &err)) {
3531 goto exit_errmsg;
3533 bswap_mips_abiflags(&abiflags);
3534 info->fp_abi = abiflags.fp_abi;
3535 #endif
3539 if (info->end_data == 0) {
3540 info->start_data = info->end_code;
3541 info->end_data = info->end_code;
3544 if (qemu_log_enabled()) {
3545 load_symbols(ehdr, src, load_bias);
3548 debuginfo_report_elf(image_name, src->fd, load_bias);
3550 mmap_unlock();
3552 close(src->fd);
3553 return;
3555 exit_mmap:
3556 error_setg_errno(&err, errno, "Error mapping file");
3557 goto exit_errmsg;
3558 exit_errmsg:
3559 error_reportf_err(err, "%s: ", image_name);
3560 exit(-1);
3563 static void load_elf_interp(const char *filename, struct image_info *info,
3564 char bprm_buf[BPRM_BUF_SIZE])
3566 struct elfhdr ehdr;
3567 ImageSource src;
3568 int fd, retval;
3569 Error *err = NULL;
3571 fd = open(path(filename), O_RDONLY);
3572 if (fd < 0) {
3573 error_setg_file_open(&err, errno, filename);
3574 error_report_err(err);
3575 exit(-1);
3578 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
3579 if (retval < 0) {
3580 error_setg_errno(&err, errno, "Error reading file header");
3581 error_reportf_err(err, "%s: ", filename);
3582 exit(-1);
3585 src.fd = fd;
3586 src.cache = bprm_buf;
3587 src.cache_size = retval;
3589 load_elf_image(filename, &src, info, &ehdr, NULL);
3592 #ifdef VDSO_HEADER
3593 #include VDSO_HEADER
3594 #define vdso_image_info() &vdso_image_info
3595 #else
3596 #define vdso_image_info() NULL
3597 #endif
3599 static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso)
3601 ImageSource src;
3602 struct elfhdr ehdr;
3603 abi_ulong load_bias, load_addr;
3605 src.fd = -1;
3606 src.cache = vdso->image;
3607 src.cache_size = vdso->image_size;
3609 load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL);
3610 load_addr = info->load_addr;
3611 load_bias = info->load_bias;
3614 * We need to relocate the VDSO image. The one built into the kernel
3615 * is built for a fixed address. The one built for QEMU is not, since
3616 * that requires close control of the guest address space.
3617 * We pre-processed the image to locate all of the addresses that need
3618 * to be updated.
3620 for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) {
3621 abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]);
3622 *addr = tswapal(tswapal(*addr) + load_bias);
3625 /* Install signal trampolines, if present. */
3626 if (vdso->sigreturn_ofs) {
3627 default_sigreturn = load_addr + vdso->sigreturn_ofs;
3629 if (vdso->rt_sigreturn_ofs) {
3630 default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs;
3633 /* Remove write from VDSO segment. */
3634 target_mprotect(info->start_data, info->end_data - info->start_data,
3635 PROT_READ | PROT_EXEC);
3638 static int symfind(const void *s0, const void *s1)
3640 struct elf_sym *sym = (struct elf_sym *)s1;
3641 __typeof(sym->st_value) addr = *(uint64_t *)s0;
3642 int result = 0;
3644 if (addr < sym->st_value) {
3645 result = -1;
3646 } else if (addr >= sym->st_value + sym->st_size) {
3647 result = 1;
3649 return result;
3652 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
3654 #if ELF_CLASS == ELFCLASS32
3655 struct elf_sym *syms = s->disas_symtab.elf32;
3656 #else
3657 struct elf_sym *syms = s->disas_symtab.elf64;
3658 #endif
3660 // binary search
3661 struct elf_sym *sym;
3663 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
3664 if (sym != NULL) {
3665 return s->disas_strtab + sym->st_name;
3668 return "";
3671 /* FIXME: This should use elf_ops.h */
3672 static int symcmp(const void *s0, const void *s1)
3674 struct elf_sym *sym0 = (struct elf_sym *)s0;
3675 struct elf_sym *sym1 = (struct elf_sym *)s1;
3676 return (sym0->st_value < sym1->st_value)
3677 ? -1
3678 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
3681 /* Best attempt to load symbols from this ELF object. */
3682 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
3683 abi_ulong load_bias)
3685 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
3686 g_autofree struct elf_shdr *shdr = NULL;
3687 char *strings = NULL;
3688 struct elf_sym *syms = NULL;
3689 struct elf_sym *new_syms;
3690 uint64_t segsz;
3692 shnum = hdr->e_shnum;
3693 shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr),
3694 src, NULL);
3695 if (shdr == NULL) {
3696 return;
3699 bswap_shdr(shdr, shnum);
3700 for (i = 0; i < shnum; ++i) {
3701 if (shdr[i].sh_type == SHT_SYMTAB) {
3702 sym_idx = i;
3703 str_idx = shdr[i].sh_link;
3704 goto found;
3708 /* There will be no symbol table if the file was stripped. */
3709 return;
3711 found:
3712 /* Now know where the strtab and symtab are. Snarf them. */
3714 segsz = shdr[str_idx].sh_size;
3715 strings = g_try_malloc(segsz);
3716 if (!strings) {
3717 goto give_up;
3719 if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) {
3720 goto give_up;
3723 segsz = shdr[sym_idx].sh_size;
3724 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
3726 * Implausibly large symbol table: give up rather than ploughing
3727 * on with the number of symbols calculation overflowing.
3729 goto give_up;
3731 nsyms = segsz / sizeof(struct elf_sym);
3732 syms = g_try_malloc(segsz);
3733 if (!syms) {
3734 goto give_up;
3736 if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) {
3737 goto give_up;
3740 for (i = 0; i < nsyms; ) {
3741 bswap_sym(syms + i);
3742 /* Throw away entries which we do not need. */
3743 if (syms[i].st_shndx == SHN_UNDEF
3744 || syms[i].st_shndx >= SHN_LORESERVE
3745 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3746 if (i < --nsyms) {
3747 syms[i] = syms[nsyms];
3749 } else {
3750 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3751 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3752 syms[i].st_value &= ~(target_ulong)1;
3753 #endif
3754 syms[i].st_value += load_bias;
3755 i++;
3759 /* No "useful" symbol. */
3760 if (nsyms == 0) {
3761 goto give_up;
3765 * Attempt to free the storage associated with the local symbols
3766 * that we threw away. Whether or not this has any effect on the
3767 * memory allocation depends on the malloc implementation and how
3768 * many symbols we managed to discard.
3770 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3771 if (new_syms == NULL) {
3772 goto give_up;
3774 syms = new_syms;
3776 qsort(syms, nsyms, sizeof(*syms), symcmp);
3779 struct syminfo *s = g_new(struct syminfo, 1);
3781 s->disas_strtab = strings;
3782 s->disas_num_syms = nsyms;
3783 #if ELF_CLASS == ELFCLASS32
3784 s->disas_symtab.elf32 = syms;
3785 #else
3786 s->disas_symtab.elf64 = syms;
3787 #endif
3788 s->lookup_symbol = lookup_symbolxx;
3789 s->next = syminfos;
3790 syminfos = s;
3792 return;
3794 give_up:
3795 g_free(strings);
3796 g_free(syms);
3799 uint32_t get_elf_eflags(int fd)
3801 struct elfhdr ehdr;
3802 off_t offset;
3803 int ret;
3805 /* Read ELF header */
3806 offset = lseek(fd, 0, SEEK_SET);
3807 if (offset == (off_t) -1) {
3808 return 0;
3810 ret = read(fd, &ehdr, sizeof(ehdr));
3811 if (ret < sizeof(ehdr)) {
3812 return 0;
3814 offset = lseek(fd, offset, SEEK_SET);
3815 if (offset == (off_t) -1) {
3816 return 0;
3819 /* Check ELF signature */
3820 if (!elf_check_ident(&ehdr)) {
3821 return 0;
3824 /* check header */
3825 bswap_ehdr(&ehdr);
3826 if (!elf_check_ehdr(&ehdr)) {
3827 return 0;
3830 /* return architecture id */
3831 return ehdr.e_flags;
3834 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3837 * We need a copy of the elf header for passing to create_elf_tables.
3838 * We will have overwritten the original when we re-use bprm->buf
3839 * while loading the interpreter. Allocate the storage for this now
3840 * and let elf_load_image do any swapping that may be required.
3842 struct elfhdr ehdr;
3843 struct image_info interp_info, vdso_info;
3844 char *elf_interpreter = NULL;
3845 char *scratch;
3847 memset(&interp_info, 0, sizeof(interp_info));
3848 #ifdef TARGET_MIPS
3849 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3850 #endif
3852 load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter);
3854 /* Do this so that we can load the interpreter, if need be. We will
3855 change some of these later */
3856 bprm->p = setup_arg_pages(bprm, info);
3858 scratch = g_new0(char, TARGET_PAGE_SIZE);
3859 if (STACK_GROWS_DOWN) {
3860 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3861 bprm->p, info->stack_limit);
3862 info->file_string = bprm->p;
3863 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3864 bprm->p, info->stack_limit);
3865 info->env_strings = bprm->p;
3866 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3867 bprm->p, info->stack_limit);
3868 info->arg_strings = bprm->p;
3869 } else {
3870 info->arg_strings = bprm->p;
3871 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3872 bprm->p, info->stack_limit);
3873 info->env_strings = bprm->p;
3874 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3875 bprm->p, info->stack_limit);
3876 info->file_string = bprm->p;
3877 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3878 bprm->p, info->stack_limit);
3881 g_free(scratch);
3883 if (!bprm->p) {
3884 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3885 exit(-1);
3888 if (elf_interpreter) {
3889 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3892 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky
3893 * with the mappings the interpreter can be loaded above but
3894 * near the main executable, which can leave very little room
3895 * for the heap.
3896 * If the current brk has less than 16MB, use the end of the
3897 * interpreter.
3899 if (interp_info.brk > info->brk &&
3900 interp_info.load_bias - info->brk < 16 * MiB) {
3901 info->brk = interp_info.brk;
3904 /* If the program interpreter is one of these two, then assume
3905 an iBCS2 image. Otherwise assume a native linux image. */
3907 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3908 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3909 info->personality = PER_SVR4;
3911 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3912 and some applications "depend" upon this behavior. Since
3913 we do not have the power to recompile these, we emulate
3914 the SVr4 behavior. Sigh. */
3915 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
3916 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3918 #ifdef TARGET_MIPS
3919 info->interp_fp_abi = interp_info.fp_abi;
3920 #endif
3924 * Load a vdso if available, which will amongst other things contain the
3925 * signal trampolines. Otherwise, allocate a separate page for them.
3927 const VdsoImageInfo *vdso = vdso_image_info();
3928 if (vdso) {
3929 load_elf_vdso(&vdso_info, vdso);
3930 info->vdso = vdso_info.load_bias;
3931 } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) {
3932 abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE,
3933 PROT_READ | PROT_WRITE,
3934 MAP_PRIVATE | MAP_ANON, -1, 0);
3935 if (tramp_page == -1) {
3936 return -errno;
3939 setup_sigtramp(tramp_page);
3940 target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC);
3943 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info,
3944 elf_interpreter ? &interp_info : NULL,
3945 vdso ? &vdso_info : NULL);
3946 info->start_stack = bprm->p;
3948 /* If we have an interpreter, set that as the program's entry point.
3949 Copy the load_bias as well, to help PPC64 interpret the entry
3950 point as a function descriptor. Do this after creating elf tables
3951 so that we copy the original program entry point into the AUXV. */
3952 if (elf_interpreter) {
3953 info->load_bias = interp_info.load_bias;
3954 info->entry = interp_info.entry;
3955 g_free(elf_interpreter);
3958 #ifdef USE_ELF_CORE_DUMP
3959 bprm->core_dump = &elf_core_dump;
3960 #endif
3962 return 0;
3965 #ifdef USE_ELF_CORE_DUMP
3967 * Definitions to generate Intel SVR4-like core files.
3968 * These mostly have the same names as the SVR4 types with "target_elf_"
3969 * tacked on the front to prevent clashes with linux definitions,
3970 * and the typedef forms have been avoided. This is mostly like
3971 * the SVR4 structure, but more Linuxy, with things that Linux does
3972 * not support and which gdb doesn't really use excluded.
3974 * Fields we don't dump (their contents is zero) in linux-user qemu
3975 * are marked with XXX.
3977 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3979 * Porting ELF coredump for target is (quite) simple process. First you
3980 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3981 * the target resides):
3983 * #define USE_ELF_CORE_DUMP
3985 * Next you define type of register set used for dumping. ELF specification
3986 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3988 * typedef <target_regtype> target_elf_greg_t;
3989 * #define ELF_NREG <number of registers>
3990 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3992 * Last step is to implement target specific function that copies registers
3993 * from given cpu into just specified register set. Prototype is:
3995 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3996 * const CPUArchState *env);
3998 * Parameters:
3999 * regs - copy register values into here (allocated and zeroed by caller)
4000 * env - copy registers from here
4002 * Example for ARM target is provided in this file.
4005 /* An ELF note in memory */
4006 struct memelfnote {
4007 const char *name;
4008 size_t namesz;
4009 size_t namesz_rounded;
4010 int type;
4011 size_t datasz;
4012 size_t datasz_rounded;
4013 void *data;
4014 size_t notesz;
4017 struct target_elf_siginfo {
4018 abi_int si_signo; /* signal number */
4019 abi_int si_code; /* extra code */
4020 abi_int si_errno; /* errno */
4023 struct target_elf_prstatus {
4024 struct target_elf_siginfo pr_info; /* Info associated with signal */
4025 abi_short pr_cursig; /* Current signal */
4026 abi_ulong pr_sigpend; /* XXX */
4027 abi_ulong pr_sighold; /* XXX */
4028 target_pid_t pr_pid;
4029 target_pid_t pr_ppid;
4030 target_pid_t pr_pgrp;
4031 target_pid_t pr_sid;
4032 struct target_timeval pr_utime; /* XXX User time */
4033 struct target_timeval pr_stime; /* XXX System time */
4034 struct target_timeval pr_cutime; /* XXX Cumulative user time */
4035 struct target_timeval pr_cstime; /* XXX Cumulative system time */
4036 target_elf_gregset_t pr_reg; /* GP registers */
4037 abi_int pr_fpvalid; /* XXX */
4040 #define ELF_PRARGSZ (80) /* Number of chars for args */
4042 struct target_elf_prpsinfo {
4043 char pr_state; /* numeric process state */
4044 char pr_sname; /* char for pr_state */
4045 char pr_zomb; /* zombie */
4046 char pr_nice; /* nice val */
4047 abi_ulong pr_flag; /* flags */
4048 target_uid_t pr_uid;
4049 target_gid_t pr_gid;
4050 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
4051 /* Lots missing */
4052 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
4053 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
4056 /* Here is the structure in which status of each thread is captured. */
4057 struct elf_thread_status {
4058 QTAILQ_ENTRY(elf_thread_status) ets_link;
4059 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
4060 #if 0
4061 elf_fpregset_t fpu; /* NT_PRFPREG */
4062 struct task_struct *thread;
4063 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
4064 #endif
4065 struct memelfnote notes[1];
4066 int num_notes;
4069 struct elf_note_info {
4070 struct memelfnote *notes;
4071 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
4072 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
4074 QTAILQ_HEAD(, elf_thread_status) thread_list;
4075 #if 0
4077 * Current version of ELF coredump doesn't support
4078 * dumping fp regs etc.
4080 elf_fpregset_t *fpu;
4081 elf_fpxregset_t *xfpu;
4082 int thread_status_size;
4083 #endif
4084 int notes_size;
4085 int numnote;
4088 struct vm_area_struct {
4089 target_ulong vma_start; /* start vaddr of memory region */
4090 target_ulong vma_end; /* end vaddr of memory region */
4091 abi_ulong vma_flags; /* protection etc. flags for the region */
4092 QTAILQ_ENTRY(vm_area_struct) vma_link;
4095 struct mm_struct {
4096 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
4097 int mm_count; /* number of mappings */
4100 static struct mm_struct *vma_init(void);
4101 static void vma_delete(struct mm_struct *);
4102 static int vma_add_mapping(struct mm_struct *, target_ulong,
4103 target_ulong, abi_ulong);
4104 static int vma_get_mapping_count(const struct mm_struct *);
4105 static struct vm_area_struct *vma_first(const struct mm_struct *);
4106 static struct vm_area_struct *vma_next(struct vm_area_struct *);
4107 static abi_ulong vma_dump_size(const struct vm_area_struct *);
4108 static int vma_walker(void *priv, target_ulong start, target_ulong end,
4109 unsigned long flags);
4111 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
4112 static void fill_note(struct memelfnote *, const char *, int,
4113 unsigned int, void *);
4114 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
4115 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
4116 static void fill_auxv_note(struct memelfnote *, const TaskState *);
4117 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
4118 static size_t note_size(const struct memelfnote *);
4119 static void free_note_info(struct elf_note_info *);
4120 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
4121 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
4123 static int dump_write(int, const void *, size_t);
4124 static int write_note(struct memelfnote *, int);
4125 static int write_note_info(struct elf_note_info *, int);
4127 #ifdef BSWAP_NEEDED
4128 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
4130 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
4131 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
4132 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
4133 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
4134 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
4135 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
4136 prstatus->pr_pid = tswap32(prstatus->pr_pid);
4137 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
4138 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
4139 prstatus->pr_sid = tswap32(prstatus->pr_sid);
4140 /* cpu times are not filled, so we skip them */
4141 /* regs should be in correct format already */
4142 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
4145 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
4147 psinfo->pr_flag = tswapal(psinfo->pr_flag);
4148 psinfo->pr_uid = tswap16(psinfo->pr_uid);
4149 psinfo->pr_gid = tswap16(psinfo->pr_gid);
4150 psinfo->pr_pid = tswap32(psinfo->pr_pid);
4151 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
4152 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
4153 psinfo->pr_sid = tswap32(psinfo->pr_sid);
4156 static void bswap_note(struct elf_note *en)
4158 bswap32s(&en->n_namesz);
4159 bswap32s(&en->n_descsz);
4160 bswap32s(&en->n_type);
4162 #else
4163 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
4164 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
4165 static inline void bswap_note(struct elf_note *en) { }
4166 #endif /* BSWAP_NEEDED */
4169 * Minimal support for linux memory regions. These are needed
4170 * when we are finding out what memory exactly belongs to
4171 * emulated process. No locks needed here, as long as
4172 * thread that received the signal is stopped.
4175 static struct mm_struct *vma_init(void)
4177 struct mm_struct *mm;
4179 if ((mm = g_malloc(sizeof (*mm))) == NULL)
4180 return (NULL);
4182 mm->mm_count = 0;
4183 QTAILQ_INIT(&mm->mm_mmap);
4185 return (mm);
4188 static void vma_delete(struct mm_struct *mm)
4190 struct vm_area_struct *vma;
4192 while ((vma = vma_first(mm)) != NULL) {
4193 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
4194 g_free(vma);
4196 g_free(mm);
4199 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
4200 target_ulong end, abi_ulong flags)
4202 struct vm_area_struct *vma;
4204 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
4205 return (-1);
4207 vma->vma_start = start;
4208 vma->vma_end = end;
4209 vma->vma_flags = flags;
4211 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
4212 mm->mm_count++;
4214 return (0);
4217 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
4219 return (QTAILQ_FIRST(&mm->mm_mmap));
4222 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
4224 return (QTAILQ_NEXT(vma, vma_link));
4227 static int vma_get_mapping_count(const struct mm_struct *mm)
4229 return (mm->mm_count);
4233 * Calculate file (dump) size of given memory region.
4235 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
4237 /* if we cannot even read the first page, skip it */
4238 if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
4239 return (0);
4242 * Usually we don't dump executable pages as they contain
4243 * non-writable code that debugger can read directly from
4244 * target library etc. However, thread stacks are marked
4245 * also executable so we read in first page of given region
4246 * and check whether it contains elf header. If there is
4247 * no elf header, we dump it.
4249 if (vma->vma_flags & PROT_EXEC) {
4250 char page[TARGET_PAGE_SIZE];
4252 if (copy_from_user(page, vma->vma_start, sizeof (page))) {
4253 return 0;
4255 if ((page[EI_MAG0] == ELFMAG0) &&
4256 (page[EI_MAG1] == ELFMAG1) &&
4257 (page[EI_MAG2] == ELFMAG2) &&
4258 (page[EI_MAG3] == ELFMAG3)) {
4260 * Mappings are possibly from ELF binary. Don't dump
4261 * them.
4263 return (0);
4267 return (vma->vma_end - vma->vma_start);
4270 static int vma_walker(void *priv, target_ulong start, target_ulong end,
4271 unsigned long flags)
4273 struct mm_struct *mm = (struct mm_struct *)priv;
4275 vma_add_mapping(mm, start, end, flags);
4276 return (0);
4279 static void fill_note(struct memelfnote *note, const char *name, int type,
4280 unsigned int sz, void *data)
4282 unsigned int namesz;
4284 namesz = strlen(name) + 1;
4285 note->name = name;
4286 note->namesz = namesz;
4287 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
4288 note->type = type;
4289 note->datasz = sz;
4290 note->datasz_rounded = roundup(sz, sizeof (int32_t));
4292 note->data = data;
4295 * We calculate rounded up note size here as specified by
4296 * ELF document.
4298 note->notesz = sizeof (struct elf_note) +
4299 note->namesz_rounded + note->datasz_rounded;
4302 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
4303 uint32_t flags)
4305 (void) memset(elf, 0, sizeof(*elf));
4307 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
4308 elf->e_ident[EI_CLASS] = ELF_CLASS;
4309 elf->e_ident[EI_DATA] = ELF_DATA;
4310 elf->e_ident[EI_VERSION] = EV_CURRENT;
4311 elf->e_ident[EI_OSABI] = ELF_OSABI;
4313 elf->e_type = ET_CORE;
4314 elf->e_machine = machine;
4315 elf->e_version = EV_CURRENT;
4316 elf->e_phoff = sizeof(struct elfhdr);
4317 elf->e_flags = flags;
4318 elf->e_ehsize = sizeof(struct elfhdr);
4319 elf->e_phentsize = sizeof(struct elf_phdr);
4320 elf->e_phnum = segs;
4322 bswap_ehdr(elf);
4325 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
4327 phdr->p_type = PT_NOTE;
4328 phdr->p_offset = offset;
4329 phdr->p_vaddr = 0;
4330 phdr->p_paddr = 0;
4331 phdr->p_filesz = sz;
4332 phdr->p_memsz = 0;
4333 phdr->p_flags = 0;
4334 phdr->p_align = 0;
4336 bswap_phdr(phdr, 1);
4339 static size_t note_size(const struct memelfnote *note)
4341 return (note->notesz);
4344 static void fill_prstatus(struct target_elf_prstatus *prstatus,
4345 const TaskState *ts, int signr)
4347 (void) memset(prstatus, 0, sizeof (*prstatus));
4348 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
4349 prstatus->pr_pid = ts->ts_tid;
4350 prstatus->pr_ppid = getppid();
4351 prstatus->pr_pgrp = getpgrp();
4352 prstatus->pr_sid = getsid(0);
4354 bswap_prstatus(prstatus);
4357 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
4359 char *base_filename;
4360 unsigned int i, len;
4362 (void) memset(psinfo, 0, sizeof (*psinfo));
4364 len = ts->info->env_strings - ts->info->arg_strings;
4365 if (len >= ELF_PRARGSZ)
4366 len = ELF_PRARGSZ - 1;
4367 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) {
4368 return -EFAULT;
4370 for (i = 0; i < len; i++)
4371 if (psinfo->pr_psargs[i] == 0)
4372 psinfo->pr_psargs[i] = ' ';
4373 psinfo->pr_psargs[len] = 0;
4375 psinfo->pr_pid = getpid();
4376 psinfo->pr_ppid = getppid();
4377 psinfo->pr_pgrp = getpgrp();
4378 psinfo->pr_sid = getsid(0);
4379 psinfo->pr_uid = getuid();
4380 psinfo->pr_gid = getgid();
4382 base_filename = g_path_get_basename(ts->bprm->filename);
4384 * Using strncpy here is fine: at max-length,
4385 * this field is not NUL-terminated.
4387 (void) strncpy(psinfo->pr_fname, base_filename,
4388 sizeof(psinfo->pr_fname));
4390 g_free(base_filename);
4391 bswap_psinfo(psinfo);
4392 return (0);
4395 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
4397 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
4398 elf_addr_t orig_auxv = auxv;
4399 void *ptr;
4400 int len = ts->info->auxv_len;
4403 * Auxiliary vector is stored in target process stack. It contains
4404 * {type, value} pairs that we need to dump into note. This is not
4405 * strictly necessary but we do it here for sake of completeness.
4408 /* read in whole auxv vector and copy it to memelfnote */
4409 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
4410 if (ptr != NULL) {
4411 fill_note(note, "CORE", NT_AUXV, len, ptr);
4412 unlock_user(ptr, auxv, len);
4417 * Constructs name of coredump file. We have following convention
4418 * for the name:
4419 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4421 * Returns the filename
4423 static char *core_dump_filename(const TaskState *ts)
4425 g_autoptr(GDateTime) now = g_date_time_new_now_local();
4426 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
4427 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
4429 return g_strdup_printf("qemu_%s_%s_%d.core",
4430 base_filename, nowstr, (int)getpid());
4433 static int dump_write(int fd, const void *ptr, size_t size)
4435 const char *bufp = (const char *)ptr;
4436 ssize_t bytes_written, bytes_left;
4437 struct rlimit dumpsize;
4438 off_t pos;
4440 bytes_written = 0;
4441 getrlimit(RLIMIT_CORE, &dumpsize);
4442 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
4443 if (errno == ESPIPE) { /* not a seekable stream */
4444 bytes_left = size;
4445 } else {
4446 return pos;
4448 } else {
4449 if (dumpsize.rlim_cur <= pos) {
4450 return -1;
4451 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
4452 bytes_left = size;
4453 } else {
4454 size_t limit_left=dumpsize.rlim_cur - pos;
4455 bytes_left = limit_left >= size ? size : limit_left ;
4460 * In normal conditions, single write(2) should do but
4461 * in case of socket etc. this mechanism is more portable.
4463 do {
4464 bytes_written = write(fd, bufp, bytes_left);
4465 if (bytes_written < 0) {
4466 if (errno == EINTR)
4467 continue;
4468 return (-1);
4469 } else if (bytes_written == 0) { /* eof */
4470 return (-1);
4472 bufp += bytes_written;
4473 bytes_left -= bytes_written;
4474 } while (bytes_left > 0);
4476 return (0);
4479 static int write_note(struct memelfnote *men, int fd)
4481 struct elf_note en;
4483 en.n_namesz = men->namesz;
4484 en.n_type = men->type;
4485 en.n_descsz = men->datasz;
4487 bswap_note(&en);
4489 if (dump_write(fd, &en, sizeof(en)) != 0)
4490 return (-1);
4491 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
4492 return (-1);
4493 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
4494 return (-1);
4496 return (0);
4499 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
4501 CPUState *cpu = env_cpu((CPUArchState *)env);
4502 TaskState *ts = (TaskState *)cpu->opaque;
4503 struct elf_thread_status *ets;
4505 ets = g_malloc0(sizeof (*ets));
4506 ets->num_notes = 1; /* only prstatus is dumped */
4507 fill_prstatus(&ets->prstatus, ts, 0);
4508 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
4509 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
4510 &ets->prstatus);
4512 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
4514 info->notes_size += note_size(&ets->notes[0]);
4517 static void init_note_info(struct elf_note_info *info)
4519 /* Initialize the elf_note_info structure so that it is at
4520 * least safe to call free_note_info() on it. Must be
4521 * called before calling fill_note_info().
4523 memset(info, 0, sizeof (*info));
4524 QTAILQ_INIT(&info->thread_list);
4527 static int fill_note_info(struct elf_note_info *info,
4528 long signr, const CPUArchState *env)
4530 #define NUMNOTES 3
4531 CPUState *cpu = env_cpu((CPUArchState *)env);
4532 TaskState *ts = (TaskState *)cpu->opaque;
4533 int i;
4535 info->notes = g_new0(struct memelfnote, NUMNOTES);
4536 if (info->notes == NULL)
4537 return (-ENOMEM);
4538 info->prstatus = g_malloc0(sizeof (*info->prstatus));
4539 if (info->prstatus == NULL)
4540 return (-ENOMEM);
4541 info->psinfo = g_malloc0(sizeof (*info->psinfo));
4542 if (info->prstatus == NULL)
4543 return (-ENOMEM);
4546 * First fill in status (and registers) of current thread
4547 * including process info & aux vector.
4549 fill_prstatus(info->prstatus, ts, signr);
4550 elf_core_copy_regs(&info->prstatus->pr_reg, env);
4551 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
4552 sizeof (*info->prstatus), info->prstatus);
4553 fill_psinfo(info->psinfo, ts);
4554 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
4555 sizeof (*info->psinfo), info->psinfo);
4556 fill_auxv_note(&info->notes[2], ts);
4557 info->numnote = 3;
4559 info->notes_size = 0;
4560 for (i = 0; i < info->numnote; i++)
4561 info->notes_size += note_size(&info->notes[i]);
4563 /* read and fill status of all threads */
4564 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock) {
4565 CPU_FOREACH(cpu) {
4566 if (cpu == thread_cpu) {
4567 continue;
4569 fill_thread_info(info, cpu_env(cpu));
4573 return (0);
4576 static void free_note_info(struct elf_note_info *info)
4578 struct elf_thread_status *ets;
4580 while (!QTAILQ_EMPTY(&info->thread_list)) {
4581 ets = QTAILQ_FIRST(&info->thread_list);
4582 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
4583 g_free(ets);
4586 g_free(info->prstatus);
4587 g_free(info->psinfo);
4588 g_free(info->notes);
4591 static int write_note_info(struct elf_note_info *info, int fd)
4593 struct elf_thread_status *ets;
4594 int i, error = 0;
4596 /* write prstatus, psinfo and auxv for current thread */
4597 for (i = 0; i < info->numnote; i++)
4598 if ((error = write_note(&info->notes[i], fd)) != 0)
4599 return (error);
4601 /* write prstatus for each thread */
4602 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
4603 if ((error = write_note(&ets->notes[0], fd)) != 0)
4604 return (error);
4607 return (0);
4611 * Write out ELF coredump.
4613 * See documentation of ELF object file format in:
4614 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4616 * Coredump format in linux is following:
4618 * 0 +----------------------+ \
4619 * | ELF header | ET_CORE |
4620 * +----------------------+ |
4621 * | ELF program headers | |--- headers
4622 * | - NOTE section | |
4623 * | - PT_LOAD sections | |
4624 * +----------------------+ /
4625 * | NOTEs: |
4626 * | - NT_PRSTATUS |
4627 * | - NT_PRSINFO |
4628 * | - NT_AUXV |
4629 * +----------------------+ <-- aligned to target page
4630 * | Process memory dump |
4631 * : :
4632 * . .
4633 * : :
4634 * | |
4635 * +----------------------+
4637 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4638 * NT_PRSINFO -> struct elf_prpsinfo
4639 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4641 * Format follows System V format as close as possible. Current
4642 * version limitations are as follows:
4643 * - no floating point registers are dumped
4645 * Function returns 0 in case of success, negative errno otherwise.
4647 * TODO: make this work also during runtime: it should be
4648 * possible to force coredump from running process and then
4649 * continue processing. For example qemu could set up SIGUSR2
4650 * handler (provided that target process haven't registered
4651 * handler for that) that does the dump when signal is received.
4653 static int elf_core_dump(int signr, const CPUArchState *env)
4655 const CPUState *cpu = env_cpu((CPUArchState *)env);
4656 const TaskState *ts = (const TaskState *)cpu->opaque;
4657 struct vm_area_struct *vma = NULL;
4658 g_autofree char *corefile = NULL;
4659 struct elf_note_info info;
4660 struct elfhdr elf;
4661 struct elf_phdr phdr;
4662 struct rlimit dumpsize;
4663 struct mm_struct *mm = NULL;
4664 off_t offset = 0, data_offset = 0;
4665 int segs = 0;
4666 int fd = -1;
4668 init_note_info(&info);
4670 errno = 0;
4672 if (prctl(PR_GET_DUMPABLE) == 0) {
4673 return 0;
4676 if (getrlimit(RLIMIT_CORE, &dumpsize) == 0 && dumpsize.rlim_cur == 0) {
4677 return 0;
4680 corefile = core_dump_filename(ts);
4682 if ((fd = open(corefile, O_WRONLY | O_CREAT,
4683 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
4684 return (-errno);
4687 * Walk through target process memory mappings and
4688 * set up structure containing this information. After
4689 * this point vma_xxx functions can be used.
4691 if ((mm = vma_init()) == NULL)
4692 goto out;
4694 walk_memory_regions(mm, vma_walker);
4695 segs = vma_get_mapping_count(mm);
4698 * Construct valid coredump ELF header. We also
4699 * add one more segment for notes.
4701 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
4702 if (dump_write(fd, &elf, sizeof (elf)) != 0)
4703 goto out;
4705 /* fill in the in-memory version of notes */
4706 if (fill_note_info(&info, signr, env) < 0)
4707 goto out;
4709 offset += sizeof (elf); /* elf header */
4710 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
4712 /* write out notes program header */
4713 fill_elf_note_phdr(&phdr, info.notes_size, offset);
4715 offset += info.notes_size;
4716 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
4717 goto out;
4720 * ELF specification wants data to start at page boundary so
4721 * we align it here.
4723 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
4726 * Write program headers for memory regions mapped in
4727 * the target process.
4729 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4730 (void) memset(&phdr, 0, sizeof (phdr));
4732 phdr.p_type = PT_LOAD;
4733 phdr.p_offset = offset;
4734 phdr.p_vaddr = vma->vma_start;
4735 phdr.p_paddr = 0;
4736 phdr.p_filesz = vma_dump_size(vma);
4737 offset += phdr.p_filesz;
4738 phdr.p_memsz = vma->vma_end - vma->vma_start;
4739 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
4740 if (vma->vma_flags & PROT_WRITE)
4741 phdr.p_flags |= PF_W;
4742 if (vma->vma_flags & PROT_EXEC)
4743 phdr.p_flags |= PF_X;
4744 phdr.p_align = ELF_EXEC_PAGESIZE;
4746 bswap_phdr(&phdr, 1);
4747 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
4748 goto out;
4753 * Next we write notes just after program headers. No
4754 * alignment needed here.
4756 if (write_note_info(&info, fd) < 0)
4757 goto out;
4759 /* align data to page boundary */
4760 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4761 goto out;
4764 * Finally we can dump process memory into corefile as well.
4766 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4767 abi_ulong addr;
4768 abi_ulong end;
4770 end = vma->vma_start + vma_dump_size(vma);
4772 for (addr = vma->vma_start; addr < end;
4773 addr += TARGET_PAGE_SIZE) {
4774 char page[TARGET_PAGE_SIZE];
4775 int error;
4778 * Read in page from target process memory and
4779 * write it to coredump file.
4781 error = copy_from_user(page, addr, sizeof (page));
4782 if (error != 0) {
4783 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
4784 addr);
4785 errno = -error;
4786 goto out;
4788 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4789 goto out;
4793 out:
4794 free_note_info(&info);
4795 if (mm != NULL)
4796 vma_delete(mm);
4797 (void) close(fd);
4799 if (errno != 0)
4800 return (-errno);
4801 return (0);
4803 #endif /* USE_ELF_CORE_DUMP */
4805 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4807 init_thread(regs, infop);