xref: /openbmc/qemu/linux-user/elfload.c (revision 4fd71d19)
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
3 #include <sys/param.h>
4 
5 #include <sys/prctl.h>
6 #include <sys/resource.h>
7 #include <sys/shm.h>
8 
9 #include "qemu.h"
10 #include "user/tswap-target.h"
11 #include "exec/page-protection.h"
12 #include "user/guest-base.h"
13 #include "user-internals.h"
14 #include "signal-common.h"
15 #include "loader.h"
16 #include "user-mmap.h"
17 #include "disas/disas.h"
18 #include "qemu/bitops.h"
19 #include "qemu/path.h"
20 #include "qemu/queue.h"
21 #include "qemu/guest-random.h"
22 #include "qemu/units.h"
23 #include "qemu/selfmap.h"
24 #include "qemu/lockable.h"
25 #include "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "target_signal.h"
28 #include "tcg/debuginfo.h"
29 
30 #ifdef TARGET_ARM
31 #include "target/arm/cpu-features.h"
32 #endif
33 
34 #ifdef _ARCH_PPC64
35 #undef ARCH_DLINFO
36 #undef ELF_PLATFORM
37 #undef ELF_HWCAP
38 #undef ELF_HWCAP2
39 #undef ELF_CLASS
40 #undef ELF_DATA
41 #undef ELF_ARCH
42 #endif
43 
44 #ifndef TARGET_ARCH_HAS_SIGTRAMP_PAGE
45 #define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0
46 #endif
47 
48 typedef struct {
49     const uint8_t *image;
50     const uint32_t *relocs;
51     unsigned image_size;
52     unsigned reloc_count;
53     unsigned sigreturn_ofs;
54     unsigned rt_sigreturn_ofs;
55 } VdsoImageInfo;
56 
57 #define ELF_OSABI   ELFOSABI_SYSV
58 
59 /* from personality.h */
60 
61 /*
62  * Flags for bug emulation.
63  *
64  * These occupy the top three bytes.
65  */
66 enum {
67     ADDR_NO_RANDOMIZE = 0x0040000,      /* disable randomization of VA space */
68     FDPIC_FUNCPTRS =    0x0080000,      /* userspace function ptrs point to
69                                            descriptors (signal handling) */
70     MMAP_PAGE_ZERO =    0x0100000,
71     ADDR_COMPAT_LAYOUT = 0x0200000,
72     READ_IMPLIES_EXEC = 0x0400000,
73     ADDR_LIMIT_32BIT =  0x0800000,
74     SHORT_INODE =       0x1000000,
75     WHOLE_SECONDS =     0x2000000,
76     STICKY_TIMEOUTS =   0x4000000,
77     ADDR_LIMIT_3GB =    0x8000000,
78 };
79 
80 /*
81  * Personality types.
82  *
83  * These go in the low byte.  Avoid using the top bit, it will
84  * conflict with error returns.
85  */
86 enum {
87     PER_LINUX =         0x0000,
88     PER_LINUX_32BIT =   0x0000 | ADDR_LIMIT_32BIT,
89     PER_LINUX_FDPIC =   0x0000 | FDPIC_FUNCPTRS,
90     PER_SVR4 =          0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
91     PER_SVR3 =          0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
92     PER_SCOSVR3 =       0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
93     PER_OSR5 =          0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
94     PER_WYSEV386 =      0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
95     PER_ISCR4 =         0x0005 | STICKY_TIMEOUTS,
96     PER_BSD =           0x0006,
97     PER_SUNOS =         0x0006 | STICKY_TIMEOUTS,
98     PER_XENIX =         0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
99     PER_LINUX32 =       0x0008,
100     PER_LINUX32_3GB =   0x0008 | ADDR_LIMIT_3GB,
101     PER_IRIX32 =        0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
102     PER_IRIXN32 =       0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
103     PER_IRIX64 =        0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
104     PER_RISCOS =        0x000c,
105     PER_SOLARIS =       0x000d | STICKY_TIMEOUTS,
106     PER_UW7 =           0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
107     PER_OSF4 =          0x000f,                  /* OSF/1 v4 */
108     PER_HPUX =          0x0010,
109     PER_MASK =          0x00ff,
110 };
111 
112 /*
113  * Return the base personality without flags.
114  */
115 #define personality(pers)       (pers & PER_MASK)
116 
117 int info_is_fdpic(struct image_info *info)
118 {
119     return info->personality == PER_LINUX_FDPIC;
120 }
121 
122 /* this flag is uneffective under linux too, should be deleted */
123 #ifndef MAP_DENYWRITE
124 #define MAP_DENYWRITE 0
125 #endif
126 
127 /* should probably go in elf.h */
128 #ifndef ELIBBAD
129 #define ELIBBAD 80
130 #endif
131 
132 #if TARGET_BIG_ENDIAN
133 #define ELF_DATA        ELFDATA2MSB
134 #else
135 #define ELF_DATA        ELFDATA2LSB
136 #endif
137 
138 #ifdef TARGET_ABI_MIPSN32
139 typedef abi_ullong      target_elf_greg_t;
140 #define tswapreg(ptr)   tswap64(ptr)
141 #else
142 typedef abi_ulong       target_elf_greg_t;
143 #define tswapreg(ptr)   tswapal(ptr)
144 #endif
145 
146 #ifdef USE_UID16
147 typedef abi_ushort      target_uid_t;
148 typedef abi_ushort      target_gid_t;
149 #else
150 typedef abi_uint        target_uid_t;
151 typedef abi_uint        target_gid_t;
152 #endif
153 typedef abi_int         target_pid_t;
154 
155 #ifdef TARGET_I386
156 
157 #define ELF_HWCAP get_elf_hwcap()
158 
159 static uint32_t get_elf_hwcap(void)
160 {
161     X86CPU *cpu = X86_CPU(thread_cpu);
162 
163     return cpu->env.features[FEAT_1_EDX];
164 }
165 
166 #ifdef TARGET_X86_64
167 #define ELF_CLASS      ELFCLASS64
168 #define ELF_ARCH       EM_X86_64
169 
170 #define ELF_PLATFORM   "x86_64"
171 
172 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
173 {
174     regs->rax = 0;
175     regs->rsp = infop->start_stack;
176     regs->rip = infop->entry;
177 }
178 
179 #define ELF_NREG    27
180 typedef target_elf_greg_t  target_elf_gregset_t[ELF_NREG];
181 
182 /*
183  * Note that ELF_NREG should be 29 as there should be place for
184  * TRAPNO and ERR "registers" as well but linux doesn't dump
185  * those.
186  *
187  * See linux kernel: arch/x86/include/asm/elf.h
188  */
189 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
190 {
191     (*regs)[0] = tswapreg(env->regs[15]);
192     (*regs)[1] = tswapreg(env->regs[14]);
193     (*regs)[2] = tswapreg(env->regs[13]);
194     (*regs)[3] = tswapreg(env->regs[12]);
195     (*regs)[4] = tswapreg(env->regs[R_EBP]);
196     (*regs)[5] = tswapreg(env->regs[R_EBX]);
197     (*regs)[6] = tswapreg(env->regs[11]);
198     (*regs)[7] = tswapreg(env->regs[10]);
199     (*regs)[8] = tswapreg(env->regs[9]);
200     (*regs)[9] = tswapreg(env->regs[8]);
201     (*regs)[10] = tswapreg(env->regs[R_EAX]);
202     (*regs)[11] = tswapreg(env->regs[R_ECX]);
203     (*regs)[12] = tswapreg(env->regs[R_EDX]);
204     (*regs)[13] = tswapreg(env->regs[R_ESI]);
205     (*regs)[14] = tswapreg(env->regs[R_EDI]);
206     (*regs)[15] = tswapreg(env->regs[R_EAX]); /* XXX */
207     (*regs)[16] = tswapreg(env->eip);
208     (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff);
209     (*regs)[18] = tswapreg(env->eflags);
210     (*regs)[19] = tswapreg(env->regs[R_ESP]);
211     (*regs)[20] = tswapreg(env->segs[R_SS].selector & 0xffff);
212     (*regs)[21] = tswapreg(env->segs[R_FS].selector & 0xffff);
213     (*regs)[22] = tswapreg(env->segs[R_GS].selector & 0xffff);
214     (*regs)[23] = tswapreg(env->segs[R_DS].selector & 0xffff);
215     (*regs)[24] = tswapreg(env->segs[R_ES].selector & 0xffff);
216     (*regs)[25] = tswapreg(env->segs[R_FS].selector & 0xffff);
217     (*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff);
218 }
219 
220 #if ULONG_MAX > UINT32_MAX
221 #define INIT_GUEST_COMMPAGE
222 static bool init_guest_commpage(void)
223 {
224     /*
225      * The vsyscall page is at a high negative address aka kernel space,
226      * which means that we cannot actually allocate it with target_mmap.
227      * We still should be able to use page_set_flags, unless the user
228      * has specified -R reserved_va, which would trigger an assert().
229      */
230     if (reserved_va != 0 &&
231         TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE - 1 > reserved_va) {
232         error_report("Cannot allocate vsyscall page");
233         exit(EXIT_FAILURE);
234     }
235     page_set_flags(TARGET_VSYSCALL_PAGE,
236                    TARGET_VSYSCALL_PAGE | ~TARGET_PAGE_MASK,
237                    PAGE_EXEC | PAGE_VALID);
238     return true;
239 }
240 #endif
241 #else
242 
243 /*
244  * This is used to ensure we don't load something for the wrong architecture.
245  */
246 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
247 
248 /*
249  * These are used to set parameters in the core dumps.
250  */
251 #define ELF_CLASS       ELFCLASS32
252 #define ELF_ARCH        EM_386
253 
254 #define ELF_PLATFORM get_elf_platform()
255 #define EXSTACK_DEFAULT true
256 
257 static const char *get_elf_platform(void)
258 {
259     static char elf_platform[] = "i386";
260     int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
261     if (family > 6) {
262         family = 6;
263     }
264     if (family >= 3) {
265         elf_platform[1] = '0' + family;
266     }
267     return elf_platform;
268 }
269 
270 static inline void init_thread(struct target_pt_regs *regs,
271                                struct image_info *infop)
272 {
273     regs->esp = infop->start_stack;
274     regs->eip = infop->entry;
275 
276     /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
277        starts %edx contains a pointer to a function which might be
278        registered using `atexit'.  This provides a mean for the
279        dynamic linker to call DT_FINI functions for shared libraries
280        that have been loaded before the code runs.
281 
282        A value of 0 tells we have no such handler.  */
283     regs->edx = 0;
284 }
285 
286 #define ELF_NREG    17
287 typedef target_elf_greg_t  target_elf_gregset_t[ELF_NREG];
288 
289 /*
290  * Note that ELF_NREG should be 19 as there should be place for
291  * TRAPNO and ERR "registers" as well but linux doesn't dump
292  * those.
293  *
294  * See linux kernel: arch/x86/include/asm/elf.h
295  */
296 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
297 {
298     (*regs)[0] = tswapreg(env->regs[R_EBX]);
299     (*regs)[1] = tswapreg(env->regs[R_ECX]);
300     (*regs)[2] = tswapreg(env->regs[R_EDX]);
301     (*regs)[3] = tswapreg(env->regs[R_ESI]);
302     (*regs)[4] = tswapreg(env->regs[R_EDI]);
303     (*regs)[5] = tswapreg(env->regs[R_EBP]);
304     (*regs)[6] = tswapreg(env->regs[R_EAX]);
305     (*regs)[7] = tswapreg(env->segs[R_DS].selector & 0xffff);
306     (*regs)[8] = tswapreg(env->segs[R_ES].selector & 0xffff);
307     (*regs)[9] = tswapreg(env->segs[R_FS].selector & 0xffff);
308     (*regs)[10] = tswapreg(env->segs[R_GS].selector & 0xffff);
309     (*regs)[11] = tswapreg(env->regs[R_EAX]); /* XXX */
310     (*regs)[12] = tswapreg(env->eip);
311     (*regs)[13] = tswapreg(env->segs[R_CS].selector & 0xffff);
312     (*regs)[14] = tswapreg(env->eflags);
313     (*regs)[15] = tswapreg(env->regs[R_ESP]);
314     (*regs)[16] = tswapreg(env->segs[R_SS].selector & 0xffff);
315 }
316 
317 /*
318  * i386 is the only target which supplies AT_SYSINFO for the vdso.
319  * All others only supply AT_SYSINFO_EHDR.
320  */
321 #define DLINFO_ARCH_ITEMS (vdso_info != NULL)
322 #define ARCH_DLINFO                                     \
323     do {                                                \
324         if (vdso_info) {                                \
325             NEW_AUX_ENT(AT_SYSINFO, vdso_info->entry);  \
326         }                                               \
327     } while (0)
328 
329 #endif /* TARGET_X86_64 */
330 
331 #define VDSO_HEADER "vdso.c.inc"
332 
333 #define USE_ELF_CORE_DUMP
334 #define ELF_EXEC_PAGESIZE       4096
335 
336 #endif /* TARGET_I386 */
337 
338 #ifdef TARGET_ARM
339 
340 #ifndef TARGET_AARCH64
341 /* 32 bit ARM definitions */
342 
343 #define ELF_ARCH        EM_ARM
344 #define ELF_CLASS       ELFCLASS32
345 #define EXSTACK_DEFAULT true
346 
347 static inline void init_thread(struct target_pt_regs *regs,
348                                struct image_info *infop)
349 {
350     abi_long stack = infop->start_stack;
351     memset(regs, 0, sizeof(*regs));
352 
353     regs->uregs[16] = ARM_CPU_MODE_USR;
354     if (infop->entry & 1) {
355         regs->uregs[16] |= CPSR_T;
356     }
357     regs->uregs[15] = infop->entry & 0xfffffffe;
358     regs->uregs[13] = infop->start_stack;
359     /* FIXME - what to for failure of get_user()? */
360     get_user_ual(regs->uregs[2], stack + 8); /* envp */
361     get_user_ual(regs->uregs[1], stack + 4); /* envp */
362     /* XXX: it seems that r0 is zeroed after ! */
363     regs->uregs[0] = 0;
364     /* For uClinux PIC binaries.  */
365     /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
366     regs->uregs[10] = infop->start_data;
367 
368     /* Support ARM FDPIC.  */
369     if (info_is_fdpic(infop)) {
370         /* As described in the ABI document, r7 points to the loadmap info
371          * prepared by the kernel. If an interpreter is needed, r8 points
372          * to the interpreter loadmap and r9 points to the interpreter
373          * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
374          * r9 points to the main program PT_DYNAMIC info.
375          */
376         regs->uregs[7] = infop->loadmap_addr;
377         if (infop->interpreter_loadmap_addr) {
378             /* Executable is dynamically loaded.  */
379             regs->uregs[8] = infop->interpreter_loadmap_addr;
380             regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
381         } else {
382             regs->uregs[8] = 0;
383             regs->uregs[9] = infop->pt_dynamic_addr;
384         }
385     }
386 }
387 
388 #define ELF_NREG    18
389 typedef target_elf_greg_t  target_elf_gregset_t[ELF_NREG];
390 
391 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
392 {
393     (*regs)[0] = tswapreg(env->regs[0]);
394     (*regs)[1] = tswapreg(env->regs[1]);
395     (*regs)[2] = tswapreg(env->regs[2]);
396     (*regs)[3] = tswapreg(env->regs[3]);
397     (*regs)[4] = tswapreg(env->regs[4]);
398     (*regs)[5] = tswapreg(env->regs[5]);
399     (*regs)[6] = tswapreg(env->regs[6]);
400     (*regs)[7] = tswapreg(env->regs[7]);
401     (*regs)[8] = tswapreg(env->regs[8]);
402     (*regs)[9] = tswapreg(env->regs[9]);
403     (*regs)[10] = tswapreg(env->regs[10]);
404     (*regs)[11] = tswapreg(env->regs[11]);
405     (*regs)[12] = tswapreg(env->regs[12]);
406     (*regs)[13] = tswapreg(env->regs[13]);
407     (*regs)[14] = tswapreg(env->regs[14]);
408     (*regs)[15] = tswapreg(env->regs[15]);
409 
410     (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
411     (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
412 }
413 
414 #define USE_ELF_CORE_DUMP
415 #define ELF_EXEC_PAGESIZE       4096
416 
417 enum
418 {
419     ARM_HWCAP_ARM_SWP       = 1 << 0,
420     ARM_HWCAP_ARM_HALF      = 1 << 1,
421     ARM_HWCAP_ARM_THUMB     = 1 << 2,
422     ARM_HWCAP_ARM_26BIT     = 1 << 3,
423     ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
424     ARM_HWCAP_ARM_FPA       = 1 << 5,
425     ARM_HWCAP_ARM_VFP       = 1 << 6,
426     ARM_HWCAP_ARM_EDSP      = 1 << 7,
427     ARM_HWCAP_ARM_JAVA      = 1 << 8,
428     ARM_HWCAP_ARM_IWMMXT    = 1 << 9,
429     ARM_HWCAP_ARM_CRUNCH    = 1 << 10,
430     ARM_HWCAP_ARM_THUMBEE   = 1 << 11,
431     ARM_HWCAP_ARM_NEON      = 1 << 12,
432     ARM_HWCAP_ARM_VFPv3     = 1 << 13,
433     ARM_HWCAP_ARM_VFPv3D16  = 1 << 14,
434     ARM_HWCAP_ARM_TLS       = 1 << 15,
435     ARM_HWCAP_ARM_VFPv4     = 1 << 16,
436     ARM_HWCAP_ARM_IDIVA     = 1 << 17,
437     ARM_HWCAP_ARM_IDIVT     = 1 << 18,
438     ARM_HWCAP_ARM_VFPD32    = 1 << 19,
439     ARM_HWCAP_ARM_LPAE      = 1 << 20,
440     ARM_HWCAP_ARM_EVTSTRM   = 1 << 21,
441     ARM_HWCAP_ARM_FPHP      = 1 << 22,
442     ARM_HWCAP_ARM_ASIMDHP   = 1 << 23,
443     ARM_HWCAP_ARM_ASIMDDP   = 1 << 24,
444     ARM_HWCAP_ARM_ASIMDFHM  = 1 << 25,
445     ARM_HWCAP_ARM_ASIMDBF16 = 1 << 26,
446     ARM_HWCAP_ARM_I8MM      = 1 << 27,
447 };
448 
449 enum {
450     ARM_HWCAP2_ARM_AES      = 1 << 0,
451     ARM_HWCAP2_ARM_PMULL    = 1 << 1,
452     ARM_HWCAP2_ARM_SHA1     = 1 << 2,
453     ARM_HWCAP2_ARM_SHA2     = 1 << 3,
454     ARM_HWCAP2_ARM_CRC32    = 1 << 4,
455     ARM_HWCAP2_ARM_SB       = 1 << 5,
456     ARM_HWCAP2_ARM_SSBS     = 1 << 6,
457 };
458 
459 /* The commpage only exists for 32 bit kernels */
460 
461 #define HI_COMMPAGE (intptr_t)0xffff0f00u
462 
463 static bool init_guest_commpage(void)
464 {
465     ARMCPU *cpu = ARM_CPU(thread_cpu);
466     int host_page_size = qemu_real_host_page_size();
467     abi_ptr commpage;
468     void *want;
469     void *addr;
470 
471     /*
472      * M-profile allocates maximum of 2GB address space, so can never
473      * allocate the commpage.  Skip it.
474      */
475     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
476         return true;
477     }
478 
479     commpage = HI_COMMPAGE & -host_page_size;
480     want = g2h_untagged(commpage);
481     addr = mmap(want, host_page_size, PROT_READ | PROT_WRITE,
482                 MAP_ANONYMOUS | MAP_PRIVATE |
483                 (commpage < reserved_va ? MAP_FIXED : MAP_FIXED_NOREPLACE),
484                 -1, 0);
485 
486     if (addr == MAP_FAILED) {
487         perror("Allocating guest commpage");
488         exit(EXIT_FAILURE);
489     }
490     if (addr != want) {
491         return false;
492     }
493 
494     /* Set kernel helper versions; rest of page is 0.  */
495     __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
496 
497     if (mprotect(addr, host_page_size, PROT_READ)) {
498         perror("Protecting guest commpage");
499         exit(EXIT_FAILURE);
500     }
501 
502     page_set_flags(commpage, commpage | (host_page_size - 1),
503                    PAGE_READ | PAGE_EXEC | PAGE_VALID);
504     return true;
505 }
506 
507 #define ELF_HWCAP get_elf_hwcap()
508 #define ELF_HWCAP2 get_elf_hwcap2()
509 
510 uint32_t get_elf_hwcap(void)
511 {
512     ARMCPU *cpu = ARM_CPU(thread_cpu);
513     uint32_t hwcaps = 0;
514 
515     hwcaps |= ARM_HWCAP_ARM_SWP;
516     hwcaps |= ARM_HWCAP_ARM_HALF;
517     hwcaps |= ARM_HWCAP_ARM_THUMB;
518     hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
519 
520     /* probe for the extra features */
521 #define GET_FEATURE(feat, hwcap) \
522     do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
523 
524 #define GET_FEATURE_ID(feat, hwcap) \
525     do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
526 
527     /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
528     GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
529     GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
530     GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
531     GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
532     GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
533     GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
534     GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
535     GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
536     GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
537 
538     if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
539         cpu_isar_feature(aa32_fpdp_v3, cpu)) {
540         hwcaps |= ARM_HWCAP_ARM_VFPv3;
541         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
542             hwcaps |= ARM_HWCAP_ARM_VFPD32;
543         } else {
544             hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
545         }
546     }
547     GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
548     /*
549      * MVFR1.FPHP and .SIMDHP must be in sync, and QEMU uses the same
550      * isar_feature function for both. The kernel reports them as two hwcaps.
551      */
552     GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_FPHP);
553     GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_ASIMDHP);
554     GET_FEATURE_ID(aa32_dp, ARM_HWCAP_ARM_ASIMDDP);
555     GET_FEATURE_ID(aa32_fhm, ARM_HWCAP_ARM_ASIMDFHM);
556     GET_FEATURE_ID(aa32_bf16, ARM_HWCAP_ARM_ASIMDBF16);
557     GET_FEATURE_ID(aa32_i8mm, ARM_HWCAP_ARM_I8MM);
558 
559     return hwcaps;
560 }
561 
562 uint64_t get_elf_hwcap2(void)
563 {
564     ARMCPU *cpu = ARM_CPU(thread_cpu);
565     uint64_t hwcaps = 0;
566 
567     GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
568     GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
569     GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
570     GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
571     GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
572     GET_FEATURE_ID(aa32_sb, ARM_HWCAP2_ARM_SB);
573     GET_FEATURE_ID(aa32_ssbs, ARM_HWCAP2_ARM_SSBS);
574     return hwcaps;
575 }
576 
577 const char *elf_hwcap_str(uint32_t bit)
578 {
579     static const char *hwcap_str[] = {
580     [__builtin_ctz(ARM_HWCAP_ARM_SWP      )] = "swp",
581     [__builtin_ctz(ARM_HWCAP_ARM_HALF     )] = "half",
582     [__builtin_ctz(ARM_HWCAP_ARM_THUMB    )] = "thumb",
583     [__builtin_ctz(ARM_HWCAP_ARM_26BIT    )] = "26bit",
584     [__builtin_ctz(ARM_HWCAP_ARM_FAST_MULT)] = "fast_mult",
585     [__builtin_ctz(ARM_HWCAP_ARM_FPA      )] = "fpa",
586     [__builtin_ctz(ARM_HWCAP_ARM_VFP      )] = "vfp",
587     [__builtin_ctz(ARM_HWCAP_ARM_EDSP     )] = "edsp",
588     [__builtin_ctz(ARM_HWCAP_ARM_JAVA     )] = "java",
589     [__builtin_ctz(ARM_HWCAP_ARM_IWMMXT   )] = "iwmmxt",
590     [__builtin_ctz(ARM_HWCAP_ARM_CRUNCH   )] = "crunch",
591     [__builtin_ctz(ARM_HWCAP_ARM_THUMBEE  )] = "thumbee",
592     [__builtin_ctz(ARM_HWCAP_ARM_NEON     )] = "neon",
593     [__builtin_ctz(ARM_HWCAP_ARM_VFPv3    )] = "vfpv3",
594     [__builtin_ctz(ARM_HWCAP_ARM_VFPv3D16 )] = "vfpv3d16",
595     [__builtin_ctz(ARM_HWCAP_ARM_TLS      )] = "tls",
596     [__builtin_ctz(ARM_HWCAP_ARM_VFPv4    )] = "vfpv4",
597     [__builtin_ctz(ARM_HWCAP_ARM_IDIVA    )] = "idiva",
598     [__builtin_ctz(ARM_HWCAP_ARM_IDIVT    )] = "idivt",
599     [__builtin_ctz(ARM_HWCAP_ARM_VFPD32   )] = "vfpd32",
600     [__builtin_ctz(ARM_HWCAP_ARM_LPAE     )] = "lpae",
601     [__builtin_ctz(ARM_HWCAP_ARM_EVTSTRM  )] = "evtstrm",
602     [__builtin_ctz(ARM_HWCAP_ARM_FPHP     )] = "fphp",
603     [__builtin_ctz(ARM_HWCAP_ARM_ASIMDHP  )] = "asimdhp",
604     [__builtin_ctz(ARM_HWCAP_ARM_ASIMDDP  )] = "asimddp",
605     [__builtin_ctz(ARM_HWCAP_ARM_ASIMDFHM )] = "asimdfhm",
606     [__builtin_ctz(ARM_HWCAP_ARM_ASIMDBF16)] = "asimdbf16",
607     [__builtin_ctz(ARM_HWCAP_ARM_I8MM     )] = "i8mm",
608     };
609 
610     return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
611 }
612 
613 const char *elf_hwcap2_str(uint32_t bit)
614 {
615     static const char *hwcap_str[] = {
616     [__builtin_ctz(ARM_HWCAP2_ARM_AES  )] = "aes",
617     [__builtin_ctz(ARM_HWCAP2_ARM_PMULL)] = "pmull",
618     [__builtin_ctz(ARM_HWCAP2_ARM_SHA1 )] = "sha1",
619     [__builtin_ctz(ARM_HWCAP2_ARM_SHA2 )] = "sha2",
620     [__builtin_ctz(ARM_HWCAP2_ARM_CRC32)] = "crc32",
621     [__builtin_ctz(ARM_HWCAP2_ARM_SB   )] = "sb",
622     [__builtin_ctz(ARM_HWCAP2_ARM_SSBS )] = "ssbs",
623     };
624 
625     return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
626 }
627 
628 #undef GET_FEATURE
629 #undef GET_FEATURE_ID
630 
631 #define ELF_PLATFORM get_elf_platform()
632 
633 static const char *get_elf_platform(void)
634 {
635     CPUARMState *env = cpu_env(thread_cpu);
636 
637 #if TARGET_BIG_ENDIAN
638 # define END  "b"
639 #else
640 # define END  "l"
641 #endif
642 
643     if (arm_feature(env, ARM_FEATURE_V8)) {
644         return "v8" END;
645     } else if (arm_feature(env, ARM_FEATURE_V7)) {
646         if (arm_feature(env, ARM_FEATURE_M)) {
647             return "v7m" END;
648         } else {
649             return "v7" END;
650         }
651     } else if (arm_feature(env, ARM_FEATURE_V6)) {
652         return "v6" END;
653     } else if (arm_feature(env, ARM_FEATURE_V5)) {
654         return "v5" END;
655     } else {
656         return "v4" END;
657     }
658 
659 #undef END
660 }
661 
662 #else
663 /* 64 bit ARM definitions */
664 
665 #define ELF_ARCH        EM_AARCH64
666 #define ELF_CLASS       ELFCLASS64
667 #if TARGET_BIG_ENDIAN
668 # define ELF_PLATFORM    "aarch64_be"
669 #else
670 # define ELF_PLATFORM    "aarch64"
671 #endif
672 
673 static inline void init_thread(struct target_pt_regs *regs,
674                                struct image_info *infop)
675 {
676     abi_long stack = infop->start_stack;
677     memset(regs, 0, sizeof(*regs));
678 
679     regs->pc = infop->entry & ~0x3ULL;
680     regs->sp = stack;
681 }
682 
683 #define ELF_NREG    34
684 typedef target_elf_greg_t  target_elf_gregset_t[ELF_NREG];
685 
686 static void elf_core_copy_regs(target_elf_gregset_t *regs,
687                                const CPUARMState *env)
688 {
689     int i;
690 
691     for (i = 0; i < 32; i++) {
692         (*regs)[i] = tswapreg(env->xregs[i]);
693     }
694     (*regs)[32] = tswapreg(env->pc);
695     (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
696 }
697 
698 #define USE_ELF_CORE_DUMP
699 #define ELF_EXEC_PAGESIZE       4096
700 
701 enum {
702     ARM_HWCAP_A64_FP            = 1 << 0,
703     ARM_HWCAP_A64_ASIMD         = 1 << 1,
704     ARM_HWCAP_A64_EVTSTRM       = 1 << 2,
705     ARM_HWCAP_A64_AES           = 1 << 3,
706     ARM_HWCAP_A64_PMULL         = 1 << 4,
707     ARM_HWCAP_A64_SHA1          = 1 << 5,
708     ARM_HWCAP_A64_SHA2          = 1 << 6,
709     ARM_HWCAP_A64_CRC32         = 1 << 7,
710     ARM_HWCAP_A64_ATOMICS       = 1 << 8,
711     ARM_HWCAP_A64_FPHP          = 1 << 9,
712     ARM_HWCAP_A64_ASIMDHP       = 1 << 10,
713     ARM_HWCAP_A64_CPUID         = 1 << 11,
714     ARM_HWCAP_A64_ASIMDRDM      = 1 << 12,
715     ARM_HWCAP_A64_JSCVT         = 1 << 13,
716     ARM_HWCAP_A64_FCMA          = 1 << 14,
717     ARM_HWCAP_A64_LRCPC         = 1 << 15,
718     ARM_HWCAP_A64_DCPOP         = 1 << 16,
719     ARM_HWCAP_A64_SHA3          = 1 << 17,
720     ARM_HWCAP_A64_SM3           = 1 << 18,
721     ARM_HWCAP_A64_SM4           = 1 << 19,
722     ARM_HWCAP_A64_ASIMDDP       = 1 << 20,
723     ARM_HWCAP_A64_SHA512        = 1 << 21,
724     ARM_HWCAP_A64_SVE           = 1 << 22,
725     ARM_HWCAP_A64_ASIMDFHM      = 1 << 23,
726     ARM_HWCAP_A64_DIT           = 1 << 24,
727     ARM_HWCAP_A64_USCAT         = 1 << 25,
728     ARM_HWCAP_A64_ILRCPC        = 1 << 26,
729     ARM_HWCAP_A64_FLAGM         = 1 << 27,
730     ARM_HWCAP_A64_SSBS          = 1 << 28,
731     ARM_HWCAP_A64_SB            = 1 << 29,
732     ARM_HWCAP_A64_PACA          = 1 << 30,
733     ARM_HWCAP_A64_PACG          = 1UL << 31,
734 
735     ARM_HWCAP2_A64_DCPODP       = 1 << 0,
736     ARM_HWCAP2_A64_SVE2         = 1 << 1,
737     ARM_HWCAP2_A64_SVEAES       = 1 << 2,
738     ARM_HWCAP2_A64_SVEPMULL     = 1 << 3,
739     ARM_HWCAP2_A64_SVEBITPERM   = 1 << 4,
740     ARM_HWCAP2_A64_SVESHA3      = 1 << 5,
741     ARM_HWCAP2_A64_SVESM4       = 1 << 6,
742     ARM_HWCAP2_A64_FLAGM2       = 1 << 7,
743     ARM_HWCAP2_A64_FRINT        = 1 << 8,
744     ARM_HWCAP2_A64_SVEI8MM      = 1 << 9,
745     ARM_HWCAP2_A64_SVEF32MM     = 1 << 10,
746     ARM_HWCAP2_A64_SVEF64MM     = 1 << 11,
747     ARM_HWCAP2_A64_SVEBF16      = 1 << 12,
748     ARM_HWCAP2_A64_I8MM         = 1 << 13,
749     ARM_HWCAP2_A64_BF16         = 1 << 14,
750     ARM_HWCAP2_A64_DGH          = 1 << 15,
751     ARM_HWCAP2_A64_RNG          = 1 << 16,
752     ARM_HWCAP2_A64_BTI          = 1 << 17,
753     ARM_HWCAP2_A64_MTE          = 1 << 18,
754     ARM_HWCAP2_A64_ECV          = 1 << 19,
755     ARM_HWCAP2_A64_AFP          = 1 << 20,
756     ARM_HWCAP2_A64_RPRES        = 1 << 21,
757     ARM_HWCAP2_A64_MTE3         = 1 << 22,
758     ARM_HWCAP2_A64_SME          = 1 << 23,
759     ARM_HWCAP2_A64_SME_I16I64   = 1 << 24,
760     ARM_HWCAP2_A64_SME_F64F64   = 1 << 25,
761     ARM_HWCAP2_A64_SME_I8I32    = 1 << 26,
762     ARM_HWCAP2_A64_SME_F16F32   = 1 << 27,
763     ARM_HWCAP2_A64_SME_B16F32   = 1 << 28,
764     ARM_HWCAP2_A64_SME_F32F32   = 1 << 29,
765     ARM_HWCAP2_A64_SME_FA64     = 1 << 30,
766     ARM_HWCAP2_A64_WFXT         = 1ULL << 31,
767     ARM_HWCAP2_A64_EBF16        = 1ULL << 32,
768     ARM_HWCAP2_A64_SVE_EBF16    = 1ULL << 33,
769     ARM_HWCAP2_A64_CSSC         = 1ULL << 34,
770     ARM_HWCAP2_A64_RPRFM        = 1ULL << 35,
771     ARM_HWCAP2_A64_SVE2P1       = 1ULL << 36,
772     ARM_HWCAP2_A64_SME2         = 1ULL << 37,
773     ARM_HWCAP2_A64_SME2P1       = 1ULL << 38,
774     ARM_HWCAP2_A64_SME_I16I32   = 1ULL << 39,
775     ARM_HWCAP2_A64_SME_BI32I32  = 1ULL << 40,
776     ARM_HWCAP2_A64_SME_B16B16   = 1ULL << 41,
777     ARM_HWCAP2_A64_SME_F16F16   = 1ULL << 42,
778     ARM_HWCAP2_A64_MOPS         = 1ULL << 43,
779     ARM_HWCAP2_A64_HBC          = 1ULL << 44,
780 };
781 
782 #define ELF_HWCAP   get_elf_hwcap()
783 #define ELF_HWCAP2  get_elf_hwcap2()
784 
785 #define GET_FEATURE_ID(feat, hwcap) \
786     do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
787 
788 uint32_t get_elf_hwcap(void)
789 {
790     ARMCPU *cpu = ARM_CPU(thread_cpu);
791     uint32_t hwcaps = 0;
792 
793     hwcaps |= ARM_HWCAP_A64_FP;
794     hwcaps |= ARM_HWCAP_A64_ASIMD;
795     hwcaps |= ARM_HWCAP_A64_CPUID;
796 
797     /* probe for the extra features */
798 
799     GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
800     GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
801     GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
802     GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
803     GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
804     GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
805     GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
806     GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
807     GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
808     GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
809     GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
810     GET_FEATURE_ID(aa64_lse2, ARM_HWCAP_A64_USCAT);
811     GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
812     GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
813     GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
814     GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
815     GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
816     GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
817     GET_FEATURE_ID(aa64_dit, ARM_HWCAP_A64_DIT);
818     GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
819     GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
820     GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
821     GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
822     GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
823     GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
824 
825     return hwcaps;
826 }
827 
828 uint64_t get_elf_hwcap2(void)
829 {
830     ARMCPU *cpu = ARM_CPU(thread_cpu);
831     uint64_t hwcaps = 0;
832 
833     GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
834     GET_FEATURE_ID(aa64_sve2, ARM_HWCAP2_A64_SVE2);
835     GET_FEATURE_ID(aa64_sve2_aes, ARM_HWCAP2_A64_SVEAES);
836     GET_FEATURE_ID(aa64_sve2_pmull128, ARM_HWCAP2_A64_SVEPMULL);
837     GET_FEATURE_ID(aa64_sve2_bitperm, ARM_HWCAP2_A64_SVEBITPERM);
838     GET_FEATURE_ID(aa64_sve2_sha3, ARM_HWCAP2_A64_SVESHA3);
839     GET_FEATURE_ID(aa64_sve2_sm4, ARM_HWCAP2_A64_SVESM4);
840     GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
841     GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
842     GET_FEATURE_ID(aa64_sve_i8mm, ARM_HWCAP2_A64_SVEI8MM);
843     GET_FEATURE_ID(aa64_sve_f32mm, ARM_HWCAP2_A64_SVEF32MM);
844     GET_FEATURE_ID(aa64_sve_f64mm, ARM_HWCAP2_A64_SVEF64MM);
845     GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16);
846     GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM);
847     GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16);
848     GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG);
849     GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI);
850     GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE);
851     GET_FEATURE_ID(aa64_mte3, ARM_HWCAP2_A64_MTE3);
852     GET_FEATURE_ID(aa64_sme, (ARM_HWCAP2_A64_SME |
853                               ARM_HWCAP2_A64_SME_F32F32 |
854                               ARM_HWCAP2_A64_SME_B16F32 |
855                               ARM_HWCAP2_A64_SME_F16F32 |
856                               ARM_HWCAP2_A64_SME_I8I32));
857     GET_FEATURE_ID(aa64_sme_f64f64, ARM_HWCAP2_A64_SME_F64F64);
858     GET_FEATURE_ID(aa64_sme_i16i64, ARM_HWCAP2_A64_SME_I16I64);
859     GET_FEATURE_ID(aa64_sme_fa64, ARM_HWCAP2_A64_SME_FA64);
860     GET_FEATURE_ID(aa64_hbc, ARM_HWCAP2_A64_HBC);
861     GET_FEATURE_ID(aa64_mops, ARM_HWCAP2_A64_MOPS);
862 
863     return hwcaps;
864 }
865 
866 const char *elf_hwcap_str(uint32_t bit)
867 {
868     static const char *hwcap_str[] = {
869     [__builtin_ctz(ARM_HWCAP_A64_FP      )] = "fp",
870     [__builtin_ctz(ARM_HWCAP_A64_ASIMD   )] = "asimd",
871     [__builtin_ctz(ARM_HWCAP_A64_EVTSTRM )] = "evtstrm",
872     [__builtin_ctz(ARM_HWCAP_A64_AES     )] = "aes",
873     [__builtin_ctz(ARM_HWCAP_A64_PMULL   )] = "pmull",
874     [__builtin_ctz(ARM_HWCAP_A64_SHA1    )] = "sha1",
875     [__builtin_ctz(ARM_HWCAP_A64_SHA2    )] = "sha2",
876     [__builtin_ctz(ARM_HWCAP_A64_CRC32   )] = "crc32",
877     [__builtin_ctz(ARM_HWCAP_A64_ATOMICS )] = "atomics",
878     [__builtin_ctz(ARM_HWCAP_A64_FPHP    )] = "fphp",
879     [__builtin_ctz(ARM_HWCAP_A64_ASIMDHP )] = "asimdhp",
880     [__builtin_ctz(ARM_HWCAP_A64_CPUID   )] = "cpuid",
881     [__builtin_ctz(ARM_HWCAP_A64_ASIMDRDM)] = "asimdrdm",
882     [__builtin_ctz(ARM_HWCAP_A64_JSCVT   )] = "jscvt",
883     [__builtin_ctz(ARM_HWCAP_A64_FCMA    )] = "fcma",
884     [__builtin_ctz(ARM_HWCAP_A64_LRCPC   )] = "lrcpc",
885     [__builtin_ctz(ARM_HWCAP_A64_DCPOP   )] = "dcpop",
886     [__builtin_ctz(ARM_HWCAP_A64_SHA3    )] = "sha3",
887     [__builtin_ctz(ARM_HWCAP_A64_SM3     )] = "sm3",
888     [__builtin_ctz(ARM_HWCAP_A64_SM4     )] = "sm4",
889     [__builtin_ctz(ARM_HWCAP_A64_ASIMDDP )] = "asimddp",
890     [__builtin_ctz(ARM_HWCAP_A64_SHA512  )] = "sha512",
891     [__builtin_ctz(ARM_HWCAP_A64_SVE     )] = "sve",
892     [__builtin_ctz(ARM_HWCAP_A64_ASIMDFHM)] = "asimdfhm",
893     [__builtin_ctz(ARM_HWCAP_A64_DIT     )] = "dit",
894     [__builtin_ctz(ARM_HWCAP_A64_USCAT   )] = "uscat",
895     [__builtin_ctz(ARM_HWCAP_A64_ILRCPC  )] = "ilrcpc",
896     [__builtin_ctz(ARM_HWCAP_A64_FLAGM   )] = "flagm",
897     [__builtin_ctz(ARM_HWCAP_A64_SSBS    )] = "ssbs",
898     [__builtin_ctz(ARM_HWCAP_A64_SB      )] = "sb",
899     [__builtin_ctz(ARM_HWCAP_A64_PACA    )] = "paca",
900     [__builtin_ctz(ARM_HWCAP_A64_PACG    )] = "pacg",
901     };
902 
903     return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
904 }
905 
906 const char *elf_hwcap2_str(uint32_t bit)
907 {
908     static const char *hwcap_str[] = {
909     [__builtin_ctz(ARM_HWCAP2_A64_DCPODP       )] = "dcpodp",
910     [__builtin_ctz(ARM_HWCAP2_A64_SVE2         )] = "sve2",
911     [__builtin_ctz(ARM_HWCAP2_A64_SVEAES       )] = "sveaes",
912     [__builtin_ctz(ARM_HWCAP2_A64_SVEPMULL     )] = "svepmull",
913     [__builtin_ctz(ARM_HWCAP2_A64_SVEBITPERM   )] = "svebitperm",
914     [__builtin_ctz(ARM_HWCAP2_A64_SVESHA3      )] = "svesha3",
915     [__builtin_ctz(ARM_HWCAP2_A64_SVESM4       )] = "svesm4",
916     [__builtin_ctz(ARM_HWCAP2_A64_FLAGM2       )] = "flagm2",
917     [__builtin_ctz(ARM_HWCAP2_A64_FRINT        )] = "frint",
918     [__builtin_ctz(ARM_HWCAP2_A64_SVEI8MM      )] = "svei8mm",
919     [__builtin_ctz(ARM_HWCAP2_A64_SVEF32MM     )] = "svef32mm",
920     [__builtin_ctz(ARM_HWCAP2_A64_SVEF64MM     )] = "svef64mm",
921     [__builtin_ctz(ARM_HWCAP2_A64_SVEBF16      )] = "svebf16",
922     [__builtin_ctz(ARM_HWCAP2_A64_I8MM         )] = "i8mm",
923     [__builtin_ctz(ARM_HWCAP2_A64_BF16         )] = "bf16",
924     [__builtin_ctz(ARM_HWCAP2_A64_DGH          )] = "dgh",
925     [__builtin_ctz(ARM_HWCAP2_A64_RNG          )] = "rng",
926     [__builtin_ctz(ARM_HWCAP2_A64_BTI          )] = "bti",
927     [__builtin_ctz(ARM_HWCAP2_A64_MTE          )] = "mte",
928     [__builtin_ctz(ARM_HWCAP2_A64_ECV          )] = "ecv",
929     [__builtin_ctz(ARM_HWCAP2_A64_AFP          )] = "afp",
930     [__builtin_ctz(ARM_HWCAP2_A64_RPRES        )] = "rpres",
931     [__builtin_ctz(ARM_HWCAP2_A64_MTE3         )] = "mte3",
932     [__builtin_ctz(ARM_HWCAP2_A64_SME          )] = "sme",
933     [__builtin_ctz(ARM_HWCAP2_A64_SME_I16I64   )] = "smei16i64",
934     [__builtin_ctz(ARM_HWCAP2_A64_SME_F64F64   )] = "smef64f64",
935     [__builtin_ctz(ARM_HWCAP2_A64_SME_I8I32    )] = "smei8i32",
936     [__builtin_ctz(ARM_HWCAP2_A64_SME_F16F32   )] = "smef16f32",
937     [__builtin_ctz(ARM_HWCAP2_A64_SME_B16F32   )] = "smeb16f32",
938     [__builtin_ctz(ARM_HWCAP2_A64_SME_F32F32   )] = "smef32f32",
939     [__builtin_ctz(ARM_HWCAP2_A64_SME_FA64     )] = "smefa64",
940     [__builtin_ctz(ARM_HWCAP2_A64_WFXT         )] = "wfxt",
941     [__builtin_ctzll(ARM_HWCAP2_A64_EBF16      )] = "ebf16",
942     [__builtin_ctzll(ARM_HWCAP2_A64_SVE_EBF16  )] = "sveebf16",
943     [__builtin_ctzll(ARM_HWCAP2_A64_CSSC       )] = "cssc",
944     [__builtin_ctzll(ARM_HWCAP2_A64_RPRFM      )] = "rprfm",
945     [__builtin_ctzll(ARM_HWCAP2_A64_SVE2P1     )] = "sve2p1",
946     [__builtin_ctzll(ARM_HWCAP2_A64_SME2       )] = "sme2",
947     [__builtin_ctzll(ARM_HWCAP2_A64_SME2P1     )] = "sme2p1",
948     [__builtin_ctzll(ARM_HWCAP2_A64_SME_I16I32 )] = "smei16i32",
949     [__builtin_ctzll(ARM_HWCAP2_A64_SME_BI32I32)] = "smebi32i32",
950     [__builtin_ctzll(ARM_HWCAP2_A64_SME_B16B16 )] = "smeb16b16",
951     [__builtin_ctzll(ARM_HWCAP2_A64_SME_F16F16 )] = "smef16f16",
952     [__builtin_ctzll(ARM_HWCAP2_A64_MOPS       )] = "mops",
953     [__builtin_ctzll(ARM_HWCAP2_A64_HBC        )] = "hbc",
954     };
955 
956     return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
957 }
958 
959 #undef GET_FEATURE_ID
960 
961 #endif /* not TARGET_AARCH64 */
962 
963 #if TARGET_BIG_ENDIAN
964 # define VDSO_HEADER  "vdso-be.c.inc"
965 #else
966 # define VDSO_HEADER  "vdso-le.c.inc"
967 #endif
968 
969 #endif /* TARGET_ARM */
970 
971 #ifdef TARGET_SPARC
972 
973 #ifndef TARGET_SPARC64
974 # define ELF_CLASS  ELFCLASS32
975 # define ELF_ARCH   EM_SPARC
976 #elif defined(TARGET_ABI32)
977 # define ELF_CLASS  ELFCLASS32
978 # define elf_check_arch(x) ((x) == EM_SPARC32PLUS || (x) == EM_SPARC)
979 #else
980 # define ELF_CLASS  ELFCLASS64
981 # define ELF_ARCH   EM_SPARCV9
982 #endif
983 
984 #include "elf.h"
985 
986 #define ELF_HWCAP get_elf_hwcap()
987 
988 static uint32_t get_elf_hwcap(void)
989 {
990     /* There are not many sparc32 hwcap bits -- we have all of them. */
991     uint32_t r = HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR |
992                  HWCAP_SPARC_SWAP | HWCAP_SPARC_MULDIV;
993 
994 #ifdef TARGET_SPARC64
995     CPUSPARCState *env = cpu_env(thread_cpu);
996     uint32_t features = env->def.features;
997 
998     r |= HWCAP_SPARC_V9 | HWCAP_SPARC_V8PLUS;
999     /* 32x32 multiply and divide are efficient. */
1000     r |= HWCAP_SPARC_MUL32 | HWCAP_SPARC_DIV32;
1001     /* We don't have an internal feature bit for this. */
1002     r |= HWCAP_SPARC_POPC;
1003     r |= features & CPU_FEATURE_FSMULD ? HWCAP_SPARC_FSMULD : 0;
1004     r |= features & CPU_FEATURE_VIS1 ? HWCAP_SPARC_VIS : 0;
1005     r |= features & CPU_FEATURE_VIS2 ? HWCAP_SPARC_VIS2 : 0;
1006     r |= features & CPU_FEATURE_FMAF ? HWCAP_SPARC_FMAF : 0;
1007 #endif
1008 
1009     return r;
1010 }
1011 
1012 static inline void init_thread(struct target_pt_regs *regs,
1013                                struct image_info *infop)
1014 {
1015     /* Note that target_cpu_copy_regs does not read psr/tstate. */
1016     regs->pc = infop->entry;
1017     regs->npc = regs->pc + 4;
1018     regs->y = 0;
1019     regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong)
1020                         - TARGET_STACK_BIAS);
1021 }
1022 #endif /* TARGET_SPARC */
1023 
1024 #ifdef TARGET_PPC
1025 
1026 #define ELF_MACHINE    PPC_ELF_MACHINE
1027 
1028 #if defined(TARGET_PPC64)
1029 
1030 #define elf_check_arch(x) ( (x) == EM_PPC64 )
1031 
1032 #define ELF_CLASS       ELFCLASS64
1033 
1034 #else
1035 
1036 #define ELF_CLASS       ELFCLASS32
1037 #define EXSTACK_DEFAULT true
1038 
1039 #endif
1040 
1041 #define ELF_ARCH        EM_PPC
1042 
1043 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
1044    See arch/powerpc/include/asm/cputable.h.  */
1045 enum {
1046     QEMU_PPC_FEATURE_32 = 0x80000000,
1047     QEMU_PPC_FEATURE_64 = 0x40000000,
1048     QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
1049     QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
1050     QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
1051     QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
1052     QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
1053     QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
1054     QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
1055     QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
1056     QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
1057     QEMU_PPC_FEATURE_NO_TB = 0x00100000,
1058     QEMU_PPC_FEATURE_POWER4 = 0x00080000,
1059     QEMU_PPC_FEATURE_POWER5 = 0x00040000,
1060     QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
1061     QEMU_PPC_FEATURE_CELL = 0x00010000,
1062     QEMU_PPC_FEATURE_BOOKE = 0x00008000,
1063     QEMU_PPC_FEATURE_SMT = 0x00004000,
1064     QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
1065     QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
1066     QEMU_PPC_FEATURE_PA6T = 0x00000800,
1067     QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
1068     QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
1069     QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
1070     QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
1071     QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
1072 
1073     QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
1074     QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
1075 
1076     /* Feature definitions in AT_HWCAP2.  */
1077     QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
1078     QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
1079     QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
1080     QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
1081     QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
1082     QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
1083     QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
1084     QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
1085     QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
1086     QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
1087     QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
1088     QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
1089     QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
1090     QEMU_PPC_FEATURE2_ARCH_3_1 = 0x00040000, /* ISA 3.1 */
1091     QEMU_PPC_FEATURE2_MMA = 0x00020000, /* Matrix-Multiply Assist */
1092 };
1093 
1094 #define ELF_HWCAP get_elf_hwcap()
1095 
1096 static uint32_t get_elf_hwcap(void)
1097 {
1098     PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
1099     uint32_t features = 0;
1100 
1101     /* We don't have to be terribly complete here; the high points are
1102        Altivec/FP/SPE support.  Anything else is just a bonus.  */
1103 #define GET_FEATURE(flag, feature)                                      \
1104     do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1105 #define GET_FEATURE2(flags, feature) \
1106     do { \
1107         if ((cpu->env.insns_flags2 & flags) == flags) { \
1108             features |= feature; \
1109         } \
1110     } while (0)
1111     GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
1112     GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
1113     GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
1114     GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
1115     GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
1116     GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
1117     GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
1118     GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
1119     GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
1120     GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
1121     GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
1122                   PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
1123                   QEMU_PPC_FEATURE_ARCH_2_06);
1124 #undef GET_FEATURE
1125 #undef GET_FEATURE2
1126 
1127     return features;
1128 }
1129 
1130 #define ELF_HWCAP2 get_elf_hwcap2()
1131 
1132 static uint32_t get_elf_hwcap2(void)
1133 {
1134     PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
1135     uint32_t features = 0;
1136 
1137 #define GET_FEATURE(flag, feature)                                      \
1138     do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1139 #define GET_FEATURE2(flag, feature)                                      \
1140     do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
1141 
1142     GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
1143     GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
1144     GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
1145                   PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
1146                   QEMU_PPC_FEATURE2_VEC_CRYPTO);
1147     GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
1148                  QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128);
1149     GET_FEATURE2(PPC2_ISA310, QEMU_PPC_FEATURE2_ARCH_3_1 |
1150                  QEMU_PPC_FEATURE2_MMA);
1151 
1152 #undef GET_FEATURE
1153 #undef GET_FEATURE2
1154 
1155     return features;
1156 }
1157 
1158 /*
1159  * The requirements here are:
1160  * - keep the final alignment of sp (sp & 0xf)
1161  * - make sure the 32-bit value at the first 16 byte aligned position of
1162  *   AUXV is greater than 16 for glibc compatibility.
1163  *   AT_IGNOREPPC is used for that.
1164  * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
1165  *   even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
1166  */
1167 #define DLINFO_ARCH_ITEMS       5
1168 #define ARCH_DLINFO                                     \
1169     do {                                                \
1170         PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);              \
1171         /*                                              \
1172          * Handle glibc compatibility: these magic entries must \
1173          * be at the lowest addresses in the final auxv.        \
1174          */                                             \
1175         NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC);        \
1176         NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC);        \
1177         NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
1178         NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
1179         NEW_AUX_ENT(AT_UCACHEBSIZE, 0);                 \
1180     } while (0)
1181 
1182 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
1183 {
1184     _regs->gpr[1] = infop->start_stack;
1185 #if defined(TARGET_PPC64)
1186     if (get_ppc64_abi(infop) < 2) {
1187         uint64_t val;
1188         get_user_u64(val, infop->entry + 8);
1189         _regs->gpr[2] = val + infop->load_bias;
1190         get_user_u64(val, infop->entry);
1191         infop->entry = val + infop->load_bias;
1192     } else {
1193         _regs->gpr[12] = infop->entry;  /* r12 set to global entry address */
1194     }
1195 #endif
1196     _regs->nip = infop->entry;
1197 }
1198 
1199 /* See linux kernel: arch/powerpc/include/asm/elf.h.  */
1200 #define ELF_NREG 48
1201 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1202 
1203 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
1204 {
1205     int i;
1206     target_ulong ccr = 0;
1207 
1208     for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
1209         (*regs)[i] = tswapreg(env->gpr[i]);
1210     }
1211 
1212     (*regs)[32] = tswapreg(env->nip);
1213     (*regs)[33] = tswapreg(env->msr);
1214     (*regs)[35] = tswapreg(env->ctr);
1215     (*regs)[36] = tswapreg(env->lr);
1216     (*regs)[37] = tswapreg(cpu_read_xer(env));
1217 
1218     ccr = ppc_get_cr(env);
1219     (*regs)[38] = tswapreg(ccr);
1220 }
1221 
1222 #define USE_ELF_CORE_DUMP
1223 #define ELF_EXEC_PAGESIZE       4096
1224 
1225 #ifndef TARGET_PPC64
1226 # define VDSO_HEADER  "vdso-32.c.inc"
1227 #elif TARGET_BIG_ENDIAN
1228 # define VDSO_HEADER  "vdso-64.c.inc"
1229 #else
1230 # define VDSO_HEADER  "vdso-64le.c.inc"
1231 #endif
1232 
1233 #endif
1234 
1235 #ifdef TARGET_LOONGARCH64
1236 
1237 #define ELF_CLASS   ELFCLASS64
1238 #define ELF_ARCH    EM_LOONGARCH
1239 #define EXSTACK_DEFAULT true
1240 
1241 #define elf_check_arch(x) ((x) == EM_LOONGARCH)
1242 
1243 #define VDSO_HEADER "vdso.c.inc"
1244 
1245 static inline void init_thread(struct target_pt_regs *regs,
1246                                struct image_info *infop)
1247 {
1248     /*Set crmd PG,DA = 1,0 */
1249     regs->csr.crmd = 2 << 3;
1250     regs->csr.era = infop->entry;
1251     regs->regs[3] = infop->start_stack;
1252 }
1253 
1254 /* See linux kernel: arch/loongarch/include/asm/elf.h */
1255 #define ELF_NREG 45
1256 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1257 
1258 enum {
1259     TARGET_EF_R0 = 0,
1260     TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33,
1261     TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34,
1262 };
1263 
1264 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1265                                const CPULoongArchState *env)
1266 {
1267     int i;
1268 
1269     (*regs)[TARGET_EF_R0] = 0;
1270 
1271     for (i = 1; i < ARRAY_SIZE(env->gpr); i++) {
1272         (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]);
1273     }
1274 
1275     (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc);
1276     (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV);
1277 }
1278 
1279 #define USE_ELF_CORE_DUMP
1280 #define ELF_EXEC_PAGESIZE        4096
1281 
1282 #define ELF_HWCAP get_elf_hwcap()
1283 
1284 /* See arch/loongarch/include/uapi/asm/hwcap.h */
1285 enum {
1286     HWCAP_LOONGARCH_CPUCFG   = (1 << 0),
1287     HWCAP_LOONGARCH_LAM      = (1 << 1),
1288     HWCAP_LOONGARCH_UAL      = (1 << 2),
1289     HWCAP_LOONGARCH_FPU      = (1 << 3),
1290     HWCAP_LOONGARCH_LSX      = (1 << 4),
1291     HWCAP_LOONGARCH_LASX     = (1 << 5),
1292     HWCAP_LOONGARCH_CRC32    = (1 << 6),
1293     HWCAP_LOONGARCH_COMPLEX  = (1 << 7),
1294     HWCAP_LOONGARCH_CRYPTO   = (1 << 8),
1295     HWCAP_LOONGARCH_LVZ      = (1 << 9),
1296     HWCAP_LOONGARCH_LBT_X86  = (1 << 10),
1297     HWCAP_LOONGARCH_LBT_ARM  = (1 << 11),
1298     HWCAP_LOONGARCH_LBT_MIPS = (1 << 12),
1299 };
1300 
1301 static uint32_t get_elf_hwcap(void)
1302 {
1303     LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu);
1304     uint32_t hwcaps = 0;
1305 
1306     hwcaps |= HWCAP_LOONGARCH_CRC32;
1307 
1308     if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) {
1309         hwcaps |= HWCAP_LOONGARCH_UAL;
1310     }
1311 
1312     if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) {
1313         hwcaps |= HWCAP_LOONGARCH_FPU;
1314     }
1315 
1316     if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) {
1317         hwcaps |= HWCAP_LOONGARCH_LAM;
1318     }
1319 
1320     if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LSX)) {
1321         hwcaps |= HWCAP_LOONGARCH_LSX;
1322     }
1323 
1324     if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LASX)) {
1325         hwcaps |= HWCAP_LOONGARCH_LASX;
1326     }
1327 
1328     return hwcaps;
1329 }
1330 
1331 #define ELF_PLATFORM "loongarch"
1332 
1333 #endif /* TARGET_LOONGARCH64 */
1334 
1335 #ifdef TARGET_MIPS
1336 
1337 #ifdef TARGET_MIPS64
1338 #define ELF_CLASS   ELFCLASS64
1339 #else
1340 #define ELF_CLASS   ELFCLASS32
1341 #endif
1342 #define ELF_ARCH    EM_MIPS
1343 #define EXSTACK_DEFAULT true
1344 
1345 #ifdef TARGET_ABI_MIPSN32
1346 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1347 #else
1348 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1349 #endif
1350 
1351 #define ELF_BASE_PLATFORM get_elf_base_platform()
1352 
1353 #define MATCH_PLATFORM_INSN(_flags, _base_platform)      \
1354     do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1355     { return _base_platform; } } while (0)
1356 
1357 static const char *get_elf_base_platform(void)
1358 {
1359     MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1360 
1361     /* 64 bit ISAs goes first */
1362     MATCH_PLATFORM_INSN(CPU_MIPS64R6, "mips64r6");
1363     MATCH_PLATFORM_INSN(CPU_MIPS64R5, "mips64r5");
1364     MATCH_PLATFORM_INSN(CPU_MIPS64R2, "mips64r2");
1365     MATCH_PLATFORM_INSN(CPU_MIPS64R1, "mips64");
1366     MATCH_PLATFORM_INSN(CPU_MIPS5, "mips5");
1367     MATCH_PLATFORM_INSN(CPU_MIPS4, "mips4");
1368     MATCH_PLATFORM_INSN(CPU_MIPS3, "mips3");
1369 
1370     /* 32 bit ISAs */
1371     MATCH_PLATFORM_INSN(CPU_MIPS32R6, "mips32r6");
1372     MATCH_PLATFORM_INSN(CPU_MIPS32R5, "mips32r5");
1373     MATCH_PLATFORM_INSN(CPU_MIPS32R2, "mips32r2");
1374     MATCH_PLATFORM_INSN(CPU_MIPS32R1, "mips32");
1375     MATCH_PLATFORM_INSN(CPU_MIPS2, "mips2");
1376 
1377     /* Fallback */
1378     return "mips";
1379 }
1380 #undef MATCH_PLATFORM_INSN
1381 
1382 static inline void init_thread(struct target_pt_regs *regs,
1383                                struct image_info *infop)
1384 {
1385     regs->cp0_status = 2 << CP0St_KSU;
1386     regs->cp0_epc = infop->entry;
1387     regs->regs[29] = infop->start_stack;
1388 }
1389 
1390 /* See linux kernel: arch/mips/include/asm/elf.h.  */
1391 #define ELF_NREG 45
1392 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1393 
1394 /* See linux kernel: arch/mips/include/asm/reg.h.  */
1395 enum {
1396 #ifdef TARGET_MIPS64
1397     TARGET_EF_R0 = 0,
1398 #else
1399     TARGET_EF_R0 = 6,
1400 #endif
1401     TARGET_EF_R26 = TARGET_EF_R0 + 26,
1402     TARGET_EF_R27 = TARGET_EF_R0 + 27,
1403     TARGET_EF_LO = TARGET_EF_R0 + 32,
1404     TARGET_EF_HI = TARGET_EF_R0 + 33,
1405     TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
1406     TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
1407     TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
1408     TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
1409 };
1410 
1411 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs.  */
1412 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
1413 {
1414     int i;
1415 
1416     for (i = 0; i < TARGET_EF_R0; i++) {
1417         (*regs)[i] = 0;
1418     }
1419     (*regs)[TARGET_EF_R0] = 0;
1420 
1421     for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
1422         (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
1423     }
1424 
1425     (*regs)[TARGET_EF_R26] = 0;
1426     (*regs)[TARGET_EF_R27] = 0;
1427     (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
1428     (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
1429     (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
1430     (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
1431     (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
1432     (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
1433 }
1434 
1435 #define USE_ELF_CORE_DUMP
1436 #define ELF_EXEC_PAGESIZE        4096
1437 
1438 /* See arch/mips/include/uapi/asm/hwcap.h.  */
1439 enum {
1440     HWCAP_MIPS_R6           = (1 << 0),
1441     HWCAP_MIPS_MSA          = (1 << 1),
1442     HWCAP_MIPS_CRC32        = (1 << 2),
1443     HWCAP_MIPS_MIPS16       = (1 << 3),
1444     HWCAP_MIPS_MDMX         = (1 << 4),
1445     HWCAP_MIPS_MIPS3D       = (1 << 5),
1446     HWCAP_MIPS_SMARTMIPS    = (1 << 6),
1447     HWCAP_MIPS_DSP          = (1 << 7),
1448     HWCAP_MIPS_DSP2         = (1 << 8),
1449     HWCAP_MIPS_DSP3         = (1 << 9),
1450     HWCAP_MIPS_MIPS16E2     = (1 << 10),
1451     HWCAP_LOONGSON_MMI      = (1 << 11),
1452     HWCAP_LOONGSON_EXT      = (1 << 12),
1453     HWCAP_LOONGSON_EXT2     = (1 << 13),
1454     HWCAP_LOONGSON_CPUCFG   = (1 << 14),
1455 };
1456 
1457 #define ELF_HWCAP get_elf_hwcap()
1458 
1459 #define GET_FEATURE_INSN(_flag, _hwcap) \
1460     do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1461 
1462 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1463     do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1464 
1465 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1466     do { \
1467         if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1468             hwcaps |= _hwcap; \
1469         } \
1470     } while (0)
1471 
1472 static uint32_t get_elf_hwcap(void)
1473 {
1474     MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1475     uint32_t hwcaps = 0;
1476 
1477     GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH,
1478                         2, HWCAP_MIPS_R6);
1479     GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA);
1480     GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI);
1481     GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT);
1482 
1483     return hwcaps;
1484 }
1485 
1486 #undef GET_FEATURE_REG_EQU
1487 #undef GET_FEATURE_REG_SET
1488 #undef GET_FEATURE_INSN
1489 
1490 #endif /* TARGET_MIPS */
1491 
1492 #ifdef TARGET_MICROBLAZE
1493 
1494 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1495 
1496 #define ELF_CLASS   ELFCLASS32
1497 #define ELF_ARCH    EM_MICROBLAZE
1498 
1499 static inline void init_thread(struct target_pt_regs *regs,
1500                                struct image_info *infop)
1501 {
1502     regs->pc = infop->entry;
1503     regs->r1 = infop->start_stack;
1504 
1505 }
1506 
1507 #define ELF_EXEC_PAGESIZE        4096
1508 
1509 #define USE_ELF_CORE_DUMP
1510 #define ELF_NREG 38
1511 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1512 
1513 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs.  */
1514 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1515 {
1516     int i, pos = 0;
1517 
1518     for (i = 0; i < 32; i++) {
1519         (*regs)[pos++] = tswapreg(env->regs[i]);
1520     }
1521 
1522     (*regs)[pos++] = tswapreg(env->pc);
1523     (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
1524     (*regs)[pos++] = 0;
1525     (*regs)[pos++] = tswapreg(env->ear);
1526     (*regs)[pos++] = 0;
1527     (*regs)[pos++] = tswapreg(env->esr);
1528 }
1529 
1530 #endif /* TARGET_MICROBLAZE */
1531 
1532 #ifdef TARGET_OPENRISC
1533 
1534 #define ELF_ARCH EM_OPENRISC
1535 #define ELF_CLASS ELFCLASS32
1536 #define ELF_DATA  ELFDATA2MSB
1537 
1538 static inline void init_thread(struct target_pt_regs *regs,
1539                                struct image_info *infop)
1540 {
1541     regs->pc = infop->entry;
1542     regs->gpr[1] = infop->start_stack;
1543 }
1544 
1545 #define USE_ELF_CORE_DUMP
1546 #define ELF_EXEC_PAGESIZE 8192
1547 
1548 /* See linux kernel arch/openrisc/include/asm/elf.h.  */
1549 #define ELF_NREG 34 /* gprs and pc, sr */
1550 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1551 
1552 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1553                                const CPUOpenRISCState *env)
1554 {
1555     int i;
1556 
1557     for (i = 0; i < 32; i++) {
1558         (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1559     }
1560     (*regs)[32] = tswapreg(env->pc);
1561     (*regs)[33] = tswapreg(cpu_get_sr(env));
1562 }
1563 #define ELF_HWCAP 0
1564 #define ELF_PLATFORM NULL
1565 
1566 #endif /* TARGET_OPENRISC */
1567 
1568 #ifdef TARGET_SH4
1569 
1570 #define ELF_CLASS ELFCLASS32
1571 #define ELF_ARCH  EM_SH
1572 
1573 static inline void init_thread(struct target_pt_regs *regs,
1574                                struct image_info *infop)
1575 {
1576     /* Check other registers XXXXX */
1577     regs->pc = infop->entry;
1578     regs->regs[15] = infop->start_stack;
1579 }
1580 
1581 /* See linux kernel: arch/sh/include/asm/elf.h.  */
1582 #define ELF_NREG 23
1583 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1584 
1585 /* See linux kernel: arch/sh/include/asm/ptrace.h.  */
1586 enum {
1587     TARGET_REG_PC = 16,
1588     TARGET_REG_PR = 17,
1589     TARGET_REG_SR = 18,
1590     TARGET_REG_GBR = 19,
1591     TARGET_REG_MACH = 20,
1592     TARGET_REG_MACL = 21,
1593     TARGET_REG_SYSCALL = 22
1594 };
1595 
1596 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1597                                       const CPUSH4State *env)
1598 {
1599     int i;
1600 
1601     for (i = 0; i < 16; i++) {
1602         (*regs)[i] = tswapreg(env->gregs[i]);
1603     }
1604 
1605     (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1606     (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1607     (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1608     (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1609     (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1610     (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1611     (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1612 }
1613 
1614 #define USE_ELF_CORE_DUMP
1615 #define ELF_EXEC_PAGESIZE        4096
1616 
1617 enum {
1618     SH_CPU_HAS_FPU            = 0x0001, /* Hardware FPU support */
1619     SH_CPU_HAS_P2_FLUSH_BUG   = 0x0002, /* Need to flush the cache in P2 area */
1620     SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1621     SH_CPU_HAS_DSP            = 0x0008, /* SH-DSP: DSP support */
1622     SH_CPU_HAS_PERF_COUNTER   = 0x0010, /* Hardware performance counters */
1623     SH_CPU_HAS_PTEA           = 0x0020, /* PTEA register */
1624     SH_CPU_HAS_LLSC           = 0x0040, /* movli.l/movco.l */
1625     SH_CPU_HAS_L2_CACHE       = 0x0080, /* Secondary cache / URAM */
1626     SH_CPU_HAS_OP32           = 0x0100, /* 32-bit instruction support */
1627     SH_CPU_HAS_PTEAEX         = 0x0200, /* PTE ASID Extension support */
1628 };
1629 
1630 #define ELF_HWCAP get_elf_hwcap()
1631 
1632 static uint32_t get_elf_hwcap(void)
1633 {
1634     SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1635     uint32_t hwcap = 0;
1636 
1637     hwcap |= SH_CPU_HAS_FPU;
1638 
1639     if (cpu->env.features & SH_FEATURE_SH4A) {
1640         hwcap |= SH_CPU_HAS_LLSC;
1641     }
1642 
1643     return hwcap;
1644 }
1645 
1646 #endif
1647 
1648 #ifdef TARGET_CRIS
1649 
1650 #define ELF_CLASS ELFCLASS32
1651 #define ELF_ARCH  EM_CRIS
1652 
1653 static inline void init_thread(struct target_pt_regs *regs,
1654                                struct image_info *infop)
1655 {
1656     regs->erp = infop->entry;
1657 }
1658 
1659 #define ELF_EXEC_PAGESIZE        8192
1660 
1661 #endif
1662 
1663 #ifdef TARGET_M68K
1664 
1665 #define ELF_CLASS       ELFCLASS32
1666 #define ELF_ARCH        EM_68K
1667 
1668 /* ??? Does this need to do anything?
1669    #define ELF_PLAT_INIT(_r) */
1670 
1671 static inline void init_thread(struct target_pt_regs *regs,
1672                                struct image_info *infop)
1673 {
1674     regs->usp = infop->start_stack;
1675     regs->sr = 0;
1676     regs->pc = infop->entry;
1677 }
1678 
1679 /* See linux kernel: arch/m68k/include/asm/elf.h.  */
1680 #define ELF_NREG 20
1681 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1682 
1683 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1684 {
1685     (*regs)[0] = tswapreg(env->dregs[1]);
1686     (*regs)[1] = tswapreg(env->dregs[2]);
1687     (*regs)[2] = tswapreg(env->dregs[3]);
1688     (*regs)[3] = tswapreg(env->dregs[4]);
1689     (*regs)[4] = tswapreg(env->dregs[5]);
1690     (*regs)[5] = tswapreg(env->dregs[6]);
1691     (*regs)[6] = tswapreg(env->dregs[7]);
1692     (*regs)[7] = tswapreg(env->aregs[0]);
1693     (*regs)[8] = tswapreg(env->aregs[1]);
1694     (*regs)[9] = tswapreg(env->aregs[2]);
1695     (*regs)[10] = tswapreg(env->aregs[3]);
1696     (*regs)[11] = tswapreg(env->aregs[4]);
1697     (*regs)[12] = tswapreg(env->aregs[5]);
1698     (*regs)[13] = tswapreg(env->aregs[6]);
1699     (*regs)[14] = tswapreg(env->dregs[0]);
1700     (*regs)[15] = tswapreg(env->aregs[7]);
1701     (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1702     (*regs)[17] = tswapreg(env->sr);
1703     (*regs)[18] = tswapreg(env->pc);
1704     (*regs)[19] = 0;  /* FIXME: regs->format | regs->vector */
1705 }
1706 
1707 #define USE_ELF_CORE_DUMP
1708 #define ELF_EXEC_PAGESIZE       8192
1709 
1710 #endif
1711 
1712 #ifdef TARGET_ALPHA
1713 
1714 #define ELF_CLASS      ELFCLASS64
1715 #define ELF_ARCH       EM_ALPHA
1716 
1717 static inline void init_thread(struct target_pt_regs *regs,
1718                                struct image_info *infop)
1719 {
1720     regs->pc = infop->entry;
1721     regs->ps = 8;
1722     regs->usp = infop->start_stack;
1723 }
1724 
1725 #define ELF_EXEC_PAGESIZE        8192
1726 
1727 #endif /* TARGET_ALPHA */
1728 
1729 #ifdef TARGET_S390X
1730 
1731 #define ELF_CLASS	ELFCLASS64
1732 #define ELF_DATA	ELFDATA2MSB
1733 #define ELF_ARCH	EM_S390
1734 
1735 #include "elf.h"
1736 
1737 #define ELF_HWCAP get_elf_hwcap()
1738 
1739 #define GET_FEATURE(_feat, _hwcap) \
1740     do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1741 
1742 uint32_t get_elf_hwcap(void)
1743 {
1744     /*
1745      * Let's assume we always have esan3 and zarch.
1746      * 31-bit processes can use 64-bit registers (high gprs).
1747      */
1748     uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1749 
1750     GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1751     GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1752     GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1753     GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1754     if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1755         s390_has_feat(S390_FEAT_ETF3_ENH)) {
1756         hwcap |= HWCAP_S390_ETF3EH;
1757     }
1758     GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1759     GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT);
1760     GET_FEATURE(S390_FEAT_VECTOR_ENH2, HWCAP_S390_VXRS_EXT2);
1761 
1762     return hwcap;
1763 }
1764 
1765 const char *elf_hwcap_str(uint32_t bit)
1766 {
1767     static const char *hwcap_str[] = {
1768         [HWCAP_S390_NR_ESAN3]     = "esan3",
1769         [HWCAP_S390_NR_ZARCH]     = "zarch",
1770         [HWCAP_S390_NR_STFLE]     = "stfle",
1771         [HWCAP_S390_NR_MSA]       = "msa",
1772         [HWCAP_S390_NR_LDISP]     = "ldisp",
1773         [HWCAP_S390_NR_EIMM]      = "eimm",
1774         [HWCAP_S390_NR_DFP]       = "dfp",
1775         [HWCAP_S390_NR_HPAGE]     = "edat",
1776         [HWCAP_S390_NR_ETF3EH]    = "etf3eh",
1777         [HWCAP_S390_NR_HIGH_GPRS] = "highgprs",
1778         [HWCAP_S390_NR_TE]        = "te",
1779         [HWCAP_S390_NR_VXRS]      = "vx",
1780         [HWCAP_S390_NR_VXRS_BCD]  = "vxd",
1781         [HWCAP_S390_NR_VXRS_EXT]  = "vxe",
1782         [HWCAP_S390_NR_GS]        = "gs",
1783         [HWCAP_S390_NR_VXRS_EXT2] = "vxe2",
1784         [HWCAP_S390_NR_VXRS_PDE]  = "vxp",
1785         [HWCAP_S390_NR_SORT]      = "sort",
1786         [HWCAP_S390_NR_DFLT]      = "dflt",
1787         [HWCAP_S390_NR_NNPA]      = "nnpa",
1788         [HWCAP_S390_NR_PCI_MIO]   = "pcimio",
1789         [HWCAP_S390_NR_SIE]       = "sie",
1790     };
1791 
1792     return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
1793 }
1794 
1795 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1796 {
1797     regs->psw.addr = infop->entry;
1798     regs->psw.mask = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
1799                      PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_MASK_64 | \
1800                      PSW_MASK_32;
1801     regs->gprs[15] = infop->start_stack;
1802 }
1803 
1804 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs).  */
1805 #define ELF_NREG 27
1806 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1807 
1808 enum {
1809     TARGET_REG_PSWM = 0,
1810     TARGET_REG_PSWA = 1,
1811     TARGET_REG_GPRS = 2,
1812     TARGET_REG_ARS = 18,
1813     TARGET_REG_ORIG_R2 = 26,
1814 };
1815 
1816 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1817                                const CPUS390XState *env)
1818 {
1819     int i;
1820     uint32_t *aregs;
1821 
1822     (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask);
1823     (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr);
1824     for (i = 0; i < 16; i++) {
1825         (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]);
1826     }
1827     aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]);
1828     for (i = 0; i < 16; i++) {
1829         aregs[i] = tswap32(env->aregs[i]);
1830     }
1831     (*regs)[TARGET_REG_ORIG_R2] = 0;
1832 }
1833 
1834 #define USE_ELF_CORE_DUMP
1835 #define ELF_EXEC_PAGESIZE 4096
1836 
1837 #define VDSO_HEADER "vdso.c.inc"
1838 
1839 #endif /* TARGET_S390X */
1840 
1841 #ifdef TARGET_RISCV
1842 
1843 #define ELF_ARCH  EM_RISCV
1844 
1845 #ifdef TARGET_RISCV32
1846 #define ELF_CLASS ELFCLASS32
1847 #define VDSO_HEADER "vdso-32.c.inc"
1848 #else
1849 #define ELF_CLASS ELFCLASS64
1850 #define VDSO_HEADER "vdso-64.c.inc"
1851 #endif
1852 
1853 #define ELF_HWCAP get_elf_hwcap()
1854 
1855 static uint32_t get_elf_hwcap(void)
1856 {
1857 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1858     RISCVCPU *cpu = RISCV_CPU(thread_cpu);
1859     uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1860                     | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C')
1861                     | MISA_BIT('V');
1862 
1863     return cpu->env.misa_ext & mask;
1864 #undef MISA_BIT
1865 }
1866 
1867 static inline void init_thread(struct target_pt_regs *regs,
1868                                struct image_info *infop)
1869 {
1870     regs->sepc = infop->entry;
1871     regs->sp = infop->start_stack;
1872 }
1873 
1874 #define ELF_EXEC_PAGESIZE 4096
1875 
1876 #endif /* TARGET_RISCV */
1877 
1878 #ifdef TARGET_HPPA
1879 
1880 #define ELF_CLASS       ELFCLASS32
1881 #define ELF_ARCH        EM_PARISC
1882 #define ELF_PLATFORM    "PARISC"
1883 #define STACK_GROWS_DOWN 0
1884 #define STACK_ALIGNMENT  64
1885 
1886 #define VDSO_HEADER "vdso.c.inc"
1887 
1888 static inline void init_thread(struct target_pt_regs *regs,
1889                                struct image_info *infop)
1890 {
1891     regs->iaoq[0] = infop->entry | PRIV_USER;
1892     regs->iaoq[1] = regs->iaoq[0] + 4;
1893     regs->gr[23] = 0;
1894     regs->gr[24] = infop->argv;
1895     regs->gr[25] = infop->argc;
1896     /* The top-of-stack contains a linkage buffer.  */
1897     regs->gr[30] = infop->start_stack + 64;
1898     regs->gr[31] = infop->entry;
1899 }
1900 
1901 #define LO_COMMPAGE  0
1902 
1903 static bool init_guest_commpage(void)
1904 {
1905     /* If reserved_va, then we have already mapped 0 page on the host. */
1906     if (!reserved_va) {
1907         void *want, *addr;
1908 
1909         want = g2h_untagged(LO_COMMPAGE);
1910         addr = mmap(want, TARGET_PAGE_SIZE, PROT_NONE,
1911                     MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED_NOREPLACE, -1, 0);
1912         if (addr == MAP_FAILED) {
1913             perror("Allocating guest commpage");
1914             exit(EXIT_FAILURE);
1915         }
1916         if (addr != want) {
1917             return false;
1918         }
1919     }
1920 
1921     /*
1922      * On Linux, page zero is normally marked execute only + gateway.
1923      * Normal read or write is supposed to fail (thus PROT_NONE above),
1924      * but specific offsets have kernel code mapped to raise permissions
1925      * and implement syscalls.  Here, simply mark the page executable.
1926      * Special case the entry points during translation (see do_page_zero).
1927      */
1928     page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1929                    PAGE_EXEC | PAGE_VALID);
1930     return true;
1931 }
1932 
1933 #endif /* TARGET_HPPA */
1934 
1935 #ifdef TARGET_XTENSA
1936 
1937 #define ELF_CLASS       ELFCLASS32
1938 #define ELF_ARCH        EM_XTENSA
1939 
1940 static inline void init_thread(struct target_pt_regs *regs,
1941                                struct image_info *infop)
1942 {
1943     regs->windowbase = 0;
1944     regs->windowstart = 1;
1945     regs->areg[1] = infop->start_stack;
1946     regs->pc = infop->entry;
1947     if (info_is_fdpic(infop)) {
1948         regs->areg[4] = infop->loadmap_addr;
1949         regs->areg[5] = infop->interpreter_loadmap_addr;
1950         if (infop->interpreter_loadmap_addr) {
1951             regs->areg[6] = infop->interpreter_pt_dynamic_addr;
1952         } else {
1953             regs->areg[6] = infop->pt_dynamic_addr;
1954         }
1955     }
1956 }
1957 
1958 /* See linux kernel: arch/xtensa/include/asm/elf.h.  */
1959 #define ELF_NREG 128
1960 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1961 
1962 enum {
1963     TARGET_REG_PC,
1964     TARGET_REG_PS,
1965     TARGET_REG_LBEG,
1966     TARGET_REG_LEND,
1967     TARGET_REG_LCOUNT,
1968     TARGET_REG_SAR,
1969     TARGET_REG_WINDOWSTART,
1970     TARGET_REG_WINDOWBASE,
1971     TARGET_REG_THREADPTR,
1972     TARGET_REG_AR0 = 64,
1973 };
1974 
1975 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1976                                const CPUXtensaState *env)
1977 {
1978     unsigned i;
1979 
1980     (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1981     (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1982     (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1983     (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1984     (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1985     (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1986     (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1987     (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1988     (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1989     xtensa_sync_phys_from_window((CPUXtensaState *)env);
1990     for (i = 0; i < env->config->nareg; ++i) {
1991         (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1992     }
1993 }
1994 
1995 #define USE_ELF_CORE_DUMP
1996 #define ELF_EXEC_PAGESIZE       4096
1997 
1998 #endif /* TARGET_XTENSA */
1999 
2000 #ifdef TARGET_HEXAGON
2001 
2002 #define ELF_CLASS       ELFCLASS32
2003 #define ELF_ARCH        EM_HEXAGON
2004 
2005 static inline void init_thread(struct target_pt_regs *regs,
2006                                struct image_info *infop)
2007 {
2008     regs->sepc = infop->entry;
2009     regs->sp = infop->start_stack;
2010 }
2011 
2012 #endif /* TARGET_HEXAGON */
2013 
2014 #ifndef ELF_BASE_PLATFORM
2015 #define ELF_BASE_PLATFORM (NULL)
2016 #endif
2017 
2018 #ifndef ELF_PLATFORM
2019 #define ELF_PLATFORM (NULL)
2020 #endif
2021 
2022 #ifndef ELF_MACHINE
2023 #define ELF_MACHINE ELF_ARCH
2024 #endif
2025 
2026 #ifndef elf_check_arch
2027 #define elf_check_arch(x) ((x) == ELF_ARCH)
2028 #endif
2029 
2030 #ifndef elf_check_abi
2031 #define elf_check_abi(x) (1)
2032 #endif
2033 
2034 #ifndef ELF_HWCAP
2035 #define ELF_HWCAP 0
2036 #endif
2037 
2038 #ifndef STACK_GROWS_DOWN
2039 #define STACK_GROWS_DOWN 1
2040 #endif
2041 
2042 #ifndef STACK_ALIGNMENT
2043 #define STACK_ALIGNMENT 16
2044 #endif
2045 
2046 #ifdef TARGET_ABI32
2047 #undef ELF_CLASS
2048 #define ELF_CLASS ELFCLASS32
2049 #undef bswaptls
2050 #define bswaptls(ptr) bswap32s(ptr)
2051 #endif
2052 
2053 #ifndef EXSTACK_DEFAULT
2054 #define EXSTACK_DEFAULT false
2055 #endif
2056 
2057 #include "elf.h"
2058 
2059 /* We must delay the following stanzas until after "elf.h". */
2060 #if defined(TARGET_AARCH64)
2061 
2062 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2063                                     const uint32_t *data,
2064                                     struct image_info *info,
2065                                     Error **errp)
2066 {
2067     if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
2068         if (pr_datasz != sizeof(uint32_t)) {
2069             error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
2070             return false;
2071         }
2072         /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
2073         info->note_flags = *data;
2074     }
2075     return true;
2076 }
2077 #define ARCH_USE_GNU_PROPERTY 1
2078 
2079 #else
2080 
2081 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2082                                     const uint32_t *data,
2083                                     struct image_info *info,
2084                                     Error **errp)
2085 {
2086     g_assert_not_reached();
2087 }
2088 #define ARCH_USE_GNU_PROPERTY 0
2089 
2090 #endif
2091 
2092 struct exec
2093 {
2094     unsigned int a_info;   /* Use macros N_MAGIC, etc for access */
2095     unsigned int a_text;   /* length of text, in bytes */
2096     unsigned int a_data;   /* length of data, in bytes */
2097     unsigned int a_bss;    /* length of uninitialized data area, in bytes */
2098     unsigned int a_syms;   /* length of symbol table data in file, in bytes */
2099     unsigned int a_entry;  /* start address */
2100     unsigned int a_trsize; /* length of relocation info for text, in bytes */
2101     unsigned int a_drsize; /* length of relocation info for data, in bytes */
2102 };
2103 
2104 
2105 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
2106 #define OMAGIC 0407
2107 #define NMAGIC 0410
2108 #define ZMAGIC 0413
2109 #define QMAGIC 0314
2110 
2111 #define DLINFO_ITEMS 16
2112 
2113 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
2114 {
2115     memcpy(to, from, n);
2116 }
2117 
2118 #ifdef BSWAP_NEEDED
2119 static void bswap_ehdr(struct elfhdr *ehdr)
2120 {
2121     bswap16s(&ehdr->e_type);            /* Object file type */
2122     bswap16s(&ehdr->e_machine);         /* Architecture */
2123     bswap32s(&ehdr->e_version);         /* Object file version */
2124     bswaptls(&ehdr->e_entry);           /* Entry point virtual address */
2125     bswaptls(&ehdr->e_phoff);           /* Program header table file offset */
2126     bswaptls(&ehdr->e_shoff);           /* Section header table file offset */
2127     bswap32s(&ehdr->e_flags);           /* Processor-specific flags */
2128     bswap16s(&ehdr->e_ehsize);          /* ELF header size in bytes */
2129     bswap16s(&ehdr->e_phentsize);       /* Program header table entry size */
2130     bswap16s(&ehdr->e_phnum);           /* Program header table entry count */
2131     bswap16s(&ehdr->e_shentsize);       /* Section header table entry size */
2132     bswap16s(&ehdr->e_shnum);           /* Section header table entry count */
2133     bswap16s(&ehdr->e_shstrndx);        /* Section header string table index */
2134 }
2135 
2136 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
2137 {
2138     int i;
2139     for (i = 0; i < phnum; ++i, ++phdr) {
2140         bswap32s(&phdr->p_type);        /* Segment type */
2141         bswap32s(&phdr->p_flags);       /* Segment flags */
2142         bswaptls(&phdr->p_offset);      /* Segment file offset */
2143         bswaptls(&phdr->p_vaddr);       /* Segment virtual address */
2144         bswaptls(&phdr->p_paddr);       /* Segment physical address */
2145         bswaptls(&phdr->p_filesz);      /* Segment size in file */
2146         bswaptls(&phdr->p_memsz);       /* Segment size in memory */
2147         bswaptls(&phdr->p_align);       /* Segment alignment */
2148     }
2149 }
2150 
2151 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
2152 {
2153     int i;
2154     for (i = 0; i < shnum; ++i, ++shdr) {
2155         bswap32s(&shdr->sh_name);
2156         bswap32s(&shdr->sh_type);
2157         bswaptls(&shdr->sh_flags);
2158         bswaptls(&shdr->sh_addr);
2159         bswaptls(&shdr->sh_offset);
2160         bswaptls(&shdr->sh_size);
2161         bswap32s(&shdr->sh_link);
2162         bswap32s(&shdr->sh_info);
2163         bswaptls(&shdr->sh_addralign);
2164         bswaptls(&shdr->sh_entsize);
2165     }
2166 }
2167 
2168 static void bswap_sym(struct elf_sym *sym)
2169 {
2170     bswap32s(&sym->st_name);
2171     bswaptls(&sym->st_value);
2172     bswaptls(&sym->st_size);
2173     bswap16s(&sym->st_shndx);
2174 }
2175 
2176 #ifdef TARGET_MIPS
2177 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
2178 {
2179     bswap16s(&abiflags->version);
2180     bswap32s(&abiflags->ases);
2181     bswap32s(&abiflags->isa_ext);
2182     bswap32s(&abiflags->flags1);
2183     bswap32s(&abiflags->flags2);
2184 }
2185 #endif
2186 #else
2187 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
2188 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
2189 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
2190 static inline void bswap_sym(struct elf_sym *sym) { }
2191 #ifdef TARGET_MIPS
2192 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
2193 #endif
2194 #endif
2195 
2196 #ifdef USE_ELF_CORE_DUMP
2197 static int elf_core_dump(int, const CPUArchState *);
2198 #endif /* USE_ELF_CORE_DUMP */
2199 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
2200                          abi_ulong load_bias);
2201 
2202 /* Verify the portions of EHDR within E_IDENT for the target.
2203    This can be performed before bswapping the entire header.  */
2204 static bool elf_check_ident(struct elfhdr *ehdr)
2205 {
2206     return (ehdr->e_ident[EI_MAG0] == ELFMAG0
2207             && ehdr->e_ident[EI_MAG1] == ELFMAG1
2208             && ehdr->e_ident[EI_MAG2] == ELFMAG2
2209             && ehdr->e_ident[EI_MAG3] == ELFMAG3
2210             && ehdr->e_ident[EI_CLASS] == ELF_CLASS
2211             && ehdr->e_ident[EI_DATA] == ELF_DATA
2212             && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
2213 }
2214 
2215 /* Verify the portions of EHDR outside of E_IDENT for the target.
2216    This has to wait until after bswapping the header.  */
2217 static bool elf_check_ehdr(struct elfhdr *ehdr)
2218 {
2219     return (elf_check_arch(ehdr->e_machine)
2220             && elf_check_abi(ehdr->e_flags)
2221             && ehdr->e_ehsize == sizeof(struct elfhdr)
2222             && ehdr->e_phentsize == sizeof(struct elf_phdr)
2223             && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
2224 }
2225 
2226 /*
2227  * 'copy_elf_strings()' copies argument/envelope strings from user
2228  * memory to free pages in kernel mem. These are in a format ready
2229  * to be put directly into the top of new user memory.
2230  *
2231  */
2232 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
2233                                   abi_ulong p, abi_ulong stack_limit)
2234 {
2235     char *tmp;
2236     int len, i;
2237     abi_ulong top = p;
2238 
2239     if (!p) {
2240         return 0;       /* bullet-proofing */
2241     }
2242 
2243     if (STACK_GROWS_DOWN) {
2244         int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
2245         for (i = argc - 1; i >= 0; --i) {
2246             tmp = argv[i];
2247             if (!tmp) {
2248                 fprintf(stderr, "VFS: argc is wrong");
2249                 exit(-1);
2250             }
2251             len = strlen(tmp) + 1;
2252             tmp += len;
2253 
2254             if (len > (p - stack_limit)) {
2255                 return 0;
2256             }
2257             while (len) {
2258                 int bytes_to_copy = (len > offset) ? offset : len;
2259                 tmp -= bytes_to_copy;
2260                 p -= bytes_to_copy;
2261                 offset -= bytes_to_copy;
2262                 len -= bytes_to_copy;
2263 
2264                 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
2265 
2266                 if (offset == 0) {
2267                     memcpy_to_target(p, scratch, top - p);
2268                     top = p;
2269                     offset = TARGET_PAGE_SIZE;
2270                 }
2271             }
2272         }
2273         if (p != top) {
2274             memcpy_to_target(p, scratch + offset, top - p);
2275         }
2276     } else {
2277         int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
2278         for (i = 0; i < argc; ++i) {
2279             tmp = argv[i];
2280             if (!tmp) {
2281                 fprintf(stderr, "VFS: argc is wrong");
2282                 exit(-1);
2283             }
2284             len = strlen(tmp) + 1;
2285             if (len > (stack_limit - p)) {
2286                 return 0;
2287             }
2288             while (len) {
2289                 int bytes_to_copy = (len > remaining) ? remaining : len;
2290 
2291                 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
2292 
2293                 tmp += bytes_to_copy;
2294                 remaining -= bytes_to_copy;
2295                 p += bytes_to_copy;
2296                 len -= bytes_to_copy;
2297 
2298                 if (remaining == 0) {
2299                     memcpy_to_target(top, scratch, p - top);
2300                     top = p;
2301                     remaining = TARGET_PAGE_SIZE;
2302                 }
2303             }
2304         }
2305         if (p != top) {
2306             memcpy_to_target(top, scratch, p - top);
2307         }
2308     }
2309 
2310     return p;
2311 }
2312 
2313 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2314  * argument/environment space. Newer kernels (>2.6.33) allow more,
2315  * dependent on stack size, but guarantee at least 32 pages for
2316  * backwards compatibility.
2317  */
2318 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2319 
2320 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
2321                                  struct image_info *info)
2322 {
2323     abi_ulong size, error, guard;
2324     int prot;
2325 
2326     size = guest_stack_size;
2327     if (size < STACK_LOWER_LIMIT) {
2328         size = STACK_LOWER_LIMIT;
2329     }
2330 
2331     if (STACK_GROWS_DOWN) {
2332         guard = TARGET_PAGE_SIZE;
2333         if (guard < qemu_real_host_page_size()) {
2334             guard = qemu_real_host_page_size();
2335         }
2336     } else {
2337         /* no guard page for hppa target where stack grows upwards. */
2338         guard = 0;
2339     }
2340 
2341     prot = PROT_READ | PROT_WRITE;
2342     if (info->exec_stack) {
2343         prot |= PROT_EXEC;
2344     }
2345     error = target_mmap(0, size + guard, prot,
2346                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2347     if (error == -1) {
2348         perror("mmap stack");
2349         exit(-1);
2350     }
2351 
2352     /* We reserve one extra page at the top of the stack as guard.  */
2353     if (STACK_GROWS_DOWN) {
2354         target_mprotect(error, guard, PROT_NONE);
2355         info->stack_limit = error + guard;
2356         return info->stack_limit + size - sizeof(void *);
2357     } else {
2358         info->stack_limit = error + size;
2359         return error;
2360     }
2361 }
2362 
2363 /**
2364  * zero_bss:
2365  *
2366  * Map and zero the bss.  We need to explicitly zero any fractional pages
2367  * after the data section (i.e. bss).  Return false on mapping failure.
2368  */
2369 static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss,
2370                      int prot, Error **errp)
2371 {
2372     abi_ulong align_bss;
2373 
2374     /* We only expect writable bss; the code segment shouldn't need this. */
2375     if (!(prot & PROT_WRITE)) {
2376         error_setg(errp, "PT_LOAD with non-writable bss");
2377         return false;
2378     }
2379 
2380     align_bss = TARGET_PAGE_ALIGN(start_bss);
2381     end_bss = TARGET_PAGE_ALIGN(end_bss);
2382 
2383     if (start_bss < align_bss) {
2384         int flags = page_get_flags(start_bss);
2385 
2386         if (!(flags & PAGE_RWX)) {
2387             /*
2388              * The whole address space of the executable was reserved
2389              * at the start, therefore all pages will be VALID.
2390              * But assuming there are no PROT_NONE PT_LOAD segments,
2391              * a PROT_NONE page means no data all bss, and we can
2392              * simply extend the new anon mapping back to the start
2393              * of the page of bss.
2394              */
2395             align_bss -= TARGET_PAGE_SIZE;
2396         } else {
2397             /*
2398              * The start of the bss shares a page with something.
2399              * The only thing that we expect is the data section,
2400              * which would already be marked writable.
2401              * Overlapping the RX code segment seems malformed.
2402              */
2403             if (!(flags & PAGE_WRITE)) {
2404                 error_setg(errp, "PT_LOAD with bss overlapping "
2405                            "non-writable page");
2406                 return false;
2407             }
2408 
2409             /* The page is already mapped and writable. */
2410             memset(g2h_untagged(start_bss), 0, align_bss - start_bss);
2411         }
2412     }
2413 
2414     if (align_bss < end_bss &&
2415         target_mmap(align_bss, end_bss - align_bss, prot,
2416                     MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
2417         error_setg_errno(errp, errno, "Error mapping bss");
2418         return false;
2419     }
2420     return true;
2421 }
2422 
2423 #if defined(TARGET_ARM)
2424 static int elf_is_fdpic(struct elfhdr *exec)
2425 {
2426     return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
2427 }
2428 #elif defined(TARGET_XTENSA)
2429 static int elf_is_fdpic(struct elfhdr *exec)
2430 {
2431     return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC;
2432 }
2433 #else
2434 /* Default implementation, always false.  */
2435 static int elf_is_fdpic(struct elfhdr *exec)
2436 {
2437     return 0;
2438 }
2439 #endif
2440 
2441 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
2442 {
2443     uint16_t n;
2444     struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
2445 
2446     /* elf32_fdpic_loadseg */
2447     n = info->nsegs;
2448     while (n--) {
2449         sp -= 12;
2450         put_user_u32(loadsegs[n].addr, sp+0);
2451         put_user_u32(loadsegs[n].p_vaddr, sp+4);
2452         put_user_u32(loadsegs[n].p_memsz, sp+8);
2453     }
2454 
2455     /* elf32_fdpic_loadmap */
2456     sp -= 4;
2457     put_user_u16(0, sp+0); /* version */
2458     put_user_u16(info->nsegs, sp+2); /* nsegs */
2459 
2460     info->personality = PER_LINUX_FDPIC;
2461     info->loadmap_addr = sp;
2462 
2463     return sp;
2464 }
2465 
2466 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
2467                                    struct elfhdr *exec,
2468                                    struct image_info *info,
2469                                    struct image_info *interp_info,
2470                                    struct image_info *vdso_info)
2471 {
2472     abi_ulong sp;
2473     abi_ulong u_argc, u_argv, u_envp, u_auxv;
2474     int size;
2475     int i;
2476     abi_ulong u_rand_bytes;
2477     uint8_t k_rand_bytes[16];
2478     abi_ulong u_platform, u_base_platform;
2479     const char *k_platform, *k_base_platform;
2480     const int n = sizeof(elf_addr_t);
2481 
2482     sp = p;
2483 
2484     /* Needs to be before we load the env/argc/... */
2485     if (elf_is_fdpic(exec)) {
2486         /* Need 4 byte alignment for these structs */
2487         sp &= ~3;
2488         sp = loader_build_fdpic_loadmap(info, sp);
2489         info->other_info = interp_info;
2490         if (interp_info) {
2491             interp_info->other_info = info;
2492             sp = loader_build_fdpic_loadmap(interp_info, sp);
2493             info->interpreter_loadmap_addr = interp_info->loadmap_addr;
2494             info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
2495         } else {
2496             info->interpreter_loadmap_addr = 0;
2497             info->interpreter_pt_dynamic_addr = 0;
2498         }
2499     }
2500 
2501     u_base_platform = 0;
2502     k_base_platform = ELF_BASE_PLATFORM;
2503     if (k_base_platform) {
2504         size_t len = strlen(k_base_platform) + 1;
2505         if (STACK_GROWS_DOWN) {
2506             sp -= (len + n - 1) & ~(n - 1);
2507             u_base_platform = sp;
2508             /* FIXME - check return value of memcpy_to_target() for failure */
2509             memcpy_to_target(sp, k_base_platform, len);
2510         } else {
2511             memcpy_to_target(sp, k_base_platform, len);
2512             u_base_platform = sp;
2513             sp += len + 1;
2514         }
2515     }
2516 
2517     u_platform = 0;
2518     k_platform = ELF_PLATFORM;
2519     if (k_platform) {
2520         size_t len = strlen(k_platform) + 1;
2521         if (STACK_GROWS_DOWN) {
2522             sp -= (len + n - 1) & ~(n - 1);
2523             u_platform = sp;
2524             /* FIXME - check return value of memcpy_to_target() for failure */
2525             memcpy_to_target(sp, k_platform, len);
2526         } else {
2527             memcpy_to_target(sp, k_platform, len);
2528             u_platform = sp;
2529             sp += len + 1;
2530         }
2531     }
2532 
2533     /* Provide 16 byte alignment for the PRNG, and basic alignment for
2534      * the argv and envp pointers.
2535      */
2536     if (STACK_GROWS_DOWN) {
2537         sp = QEMU_ALIGN_DOWN(sp, 16);
2538     } else {
2539         sp = QEMU_ALIGN_UP(sp, 16);
2540     }
2541 
2542     /*
2543      * Generate 16 random bytes for userspace PRNG seeding.
2544      */
2545     qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
2546     if (STACK_GROWS_DOWN) {
2547         sp -= 16;
2548         u_rand_bytes = sp;
2549         /* FIXME - check return value of memcpy_to_target() for failure */
2550         memcpy_to_target(sp, k_rand_bytes, 16);
2551     } else {
2552         memcpy_to_target(sp, k_rand_bytes, 16);
2553         u_rand_bytes = sp;
2554         sp += 16;
2555     }
2556 
2557     size = (DLINFO_ITEMS + 1) * 2;
2558     if (k_base_platform) {
2559         size += 2;
2560     }
2561     if (k_platform) {
2562         size += 2;
2563     }
2564     if (vdso_info) {
2565         size += 2;
2566     }
2567 #ifdef DLINFO_ARCH_ITEMS
2568     size += DLINFO_ARCH_ITEMS * 2;
2569 #endif
2570 #ifdef ELF_HWCAP2
2571     size += 2;
2572 #endif
2573     info->auxv_len = size * n;
2574 
2575     size += envc + argc + 2;
2576     size += 1;  /* argc itself */
2577     size *= n;
2578 
2579     /* Allocate space and finalize stack alignment for entry now.  */
2580     if (STACK_GROWS_DOWN) {
2581         u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
2582         sp = u_argc;
2583     } else {
2584         u_argc = sp;
2585         sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2586     }
2587 
2588     u_argv = u_argc + n;
2589     u_envp = u_argv + (argc + 1) * n;
2590     u_auxv = u_envp + (envc + 1) * n;
2591     info->saved_auxv = u_auxv;
2592     info->argc = argc;
2593     info->envc = envc;
2594     info->argv = u_argv;
2595     info->envp = u_envp;
2596 
2597     /* This is correct because Linux defines
2598      * elf_addr_t as Elf32_Off / Elf64_Off
2599      */
2600 #define NEW_AUX_ENT(id, val) do {               \
2601         put_user_ual(id, u_auxv);  u_auxv += n; \
2602         put_user_ual(val, u_auxv); u_auxv += n; \
2603     } while(0)
2604 
2605 #ifdef ARCH_DLINFO
2606     /*
2607      * ARCH_DLINFO must come first so platform specific code can enforce
2608      * special alignment requirements on the AUXV if necessary (eg. PPC).
2609      */
2610     ARCH_DLINFO;
2611 #endif
2612     /* There must be exactly DLINFO_ITEMS entries here, or the assert
2613      * on info->auxv_len will trigger.
2614      */
2615     NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2616     NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2617     NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2618     NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2619     NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2620     NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2621     NEW_AUX_ENT(AT_ENTRY, info->entry);
2622     NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2623     NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2624     NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2625     NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2626     NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2627     NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2628     NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2629     NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2630     NEW_AUX_ENT(AT_EXECFN, info->file_string);
2631 
2632 #ifdef ELF_HWCAP2
2633     NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2634 #endif
2635 
2636     if (u_base_platform) {
2637         NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform);
2638     }
2639     if (u_platform) {
2640         NEW_AUX_ENT(AT_PLATFORM, u_platform);
2641     }
2642     if (vdso_info) {
2643         NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr);
2644     }
2645     NEW_AUX_ENT (AT_NULL, 0);
2646 #undef NEW_AUX_ENT
2647 
2648     /* Check that our initial calculation of the auxv length matches how much
2649      * we actually put into it.
2650      */
2651     assert(info->auxv_len == u_auxv - info->saved_auxv);
2652 
2653     put_user_ual(argc, u_argc);
2654 
2655     p = info->arg_strings;
2656     for (i = 0; i < argc; ++i) {
2657         put_user_ual(p, u_argv);
2658         u_argv += n;
2659         p += target_strlen(p) + 1;
2660     }
2661     put_user_ual(0, u_argv);
2662 
2663     p = info->env_strings;
2664     for (i = 0; i < envc; ++i) {
2665         put_user_ual(p, u_envp);
2666         u_envp += n;
2667         p += target_strlen(p) + 1;
2668     }
2669     put_user_ual(0, u_envp);
2670 
2671     return sp;
2672 }
2673 
2674 #if defined(HI_COMMPAGE)
2675 #define LO_COMMPAGE -1
2676 #elif defined(LO_COMMPAGE)
2677 #define HI_COMMPAGE 0
2678 #else
2679 #define HI_COMMPAGE 0
2680 #define LO_COMMPAGE -1
2681 #ifndef INIT_GUEST_COMMPAGE
2682 #define init_guest_commpage() true
2683 #endif
2684 #endif
2685 
2686 /**
2687  * pgb_try_mmap:
2688  * @addr: host start address
2689  * @addr_last: host last address
2690  * @keep: do not unmap the probe region
2691  *
2692  * Return 1 if [@addr, @addr_last] is not mapped in the host,
2693  * return 0 if it is not available to map, and -1 on mmap error.
2694  * If @keep, the region is left mapped on success, otherwise unmapped.
2695  */
2696 static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep)
2697 {
2698     size_t size = addr_last - addr + 1;
2699     void *p = mmap((void *)addr, size, PROT_NONE,
2700                    MAP_ANONYMOUS | MAP_PRIVATE |
2701                    MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0);
2702     int ret;
2703 
2704     if (p == MAP_FAILED) {
2705         return errno == EEXIST ? 0 : -1;
2706     }
2707     ret = p == (void *)addr;
2708     if (!keep || !ret) {
2709         munmap(p, size);
2710     }
2711     return ret;
2712 }
2713 
2714 /**
2715  * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk)
2716  * @addr: host address
2717  * @addr_last: host last address
2718  * @brk: host brk
2719  *
2720  * Like pgb_try_mmap, but additionally reserve some memory following brk.
2721  */
2722 static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last,
2723                                  uintptr_t brk, bool keep)
2724 {
2725     uintptr_t brk_last = brk + 16 * MiB - 1;
2726 
2727     /* Do not map anything close to the host brk. */
2728     if (addr <= brk_last && brk <= addr_last) {
2729         return 0;
2730     }
2731     return pgb_try_mmap(addr, addr_last, keep);
2732 }
2733 
2734 /**
2735  * pgb_try_mmap_set:
2736  * @ga: set of guest addrs
2737  * @base: guest_base
2738  * @brk: host brk
2739  *
2740  * Return true if all @ga can be mapped by the host at @base.
2741  * On success, retain the mapping at index 0 for reserved_va.
2742  */
2743 
2744 typedef struct PGBAddrs {
2745     uintptr_t bounds[3][2]; /* start/last pairs */
2746     int nbounds;
2747 } PGBAddrs;
2748 
2749 static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk)
2750 {
2751     for (int i = ga->nbounds - 1; i >= 0; --i) {
2752         if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base,
2753                                   ga->bounds[i][1] + base,
2754                                   brk, i == 0 && reserved_va) <= 0) {
2755             return false;
2756         }
2757     }
2758     return true;
2759 }
2760 
2761 /**
2762  * pgb_addr_set:
2763  * @ga: output set of guest addrs
2764  * @guest_loaddr: guest image low address
2765  * @guest_loaddr: guest image high address
2766  * @identity: create for identity mapping
2767  *
2768  * Fill in @ga with the image, COMMPAGE and NULL page.
2769  */
2770 static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr,
2771                          abi_ulong guest_hiaddr, bool try_identity)
2772 {
2773     int n;
2774 
2775     /*
2776      * With a low commpage, or a guest mapped very low,
2777      * we may not be able to use the identity map.
2778      */
2779     if (try_identity) {
2780         if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) {
2781             return false;
2782         }
2783         if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) {
2784             return false;
2785         }
2786     }
2787 
2788     memset(ga, 0, sizeof(*ga));
2789     n = 0;
2790 
2791     if (reserved_va) {
2792         ga->bounds[n][0] = try_identity ? mmap_min_addr : 0;
2793         ga->bounds[n][1] = reserved_va;
2794         n++;
2795         /* LO_COMMPAGE and NULL handled by reserving from 0. */
2796     } else {
2797         /* Add any LO_COMMPAGE or NULL page. */
2798         if (LO_COMMPAGE != -1) {
2799             ga->bounds[n][0] = 0;
2800             ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1;
2801             n++;
2802         } else if (!try_identity) {
2803             ga->bounds[n][0] = 0;
2804             ga->bounds[n][1] = TARGET_PAGE_SIZE - 1;
2805             n++;
2806         }
2807 
2808         /* Add the guest image for ET_EXEC. */
2809         if (guest_loaddr) {
2810             ga->bounds[n][0] = guest_loaddr;
2811             ga->bounds[n][1] = guest_hiaddr;
2812             n++;
2813         }
2814     }
2815 
2816     /*
2817      * Temporarily disable
2818      *   "comparison is always false due to limited range of data type"
2819      * due to comparison between unsigned and (possible) 0.
2820      */
2821 #pragma GCC diagnostic push
2822 #pragma GCC diagnostic ignored "-Wtype-limits"
2823 
2824     /* Add any HI_COMMPAGE not covered by reserved_va. */
2825     if (reserved_va < HI_COMMPAGE) {
2826         ga->bounds[n][0] = HI_COMMPAGE & qemu_real_host_page_mask();
2827         ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1;
2828         n++;
2829     }
2830 
2831 #pragma GCC diagnostic pop
2832 
2833     ga->nbounds = n;
2834     return true;
2835 }
2836 
2837 static void pgb_fail_in_use(const char *image_name)
2838 {
2839     error_report("%s: requires virtual address space that is in use "
2840                  "(omit the -B option or choose a different value)",
2841                  image_name);
2842     exit(EXIT_FAILURE);
2843 }
2844 
2845 static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr,
2846                       uintptr_t guest_hiaddr, uintptr_t align)
2847 {
2848     PGBAddrs ga;
2849     uintptr_t brk = (uintptr_t)sbrk(0);
2850 
2851     if (!QEMU_IS_ALIGNED(guest_base, align)) {
2852         fprintf(stderr, "Requested guest base %p does not satisfy "
2853                 "host minimum alignment (0x%" PRIxPTR ")\n",
2854                 (void *)guest_base, align);
2855         exit(EXIT_FAILURE);
2856     }
2857 
2858     if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base)
2859         || !pgb_try_mmap_set(&ga, guest_base, brk)) {
2860         pgb_fail_in_use(image_name);
2861     }
2862 }
2863 
2864 /**
2865  * pgb_find_fallback:
2866  *
2867  * This is a fallback method for finding holes in the host address space
2868  * if we don't have the benefit of being able to access /proc/self/map.
2869  * It can potentially take a very long time as we can only dumbly iterate
2870  * up the host address space seeing if the allocation would work.
2871  */
2872 static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align,
2873                                    uintptr_t brk)
2874 {
2875     /* TODO: come up with a better estimate of how much to skip. */
2876     uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB;
2877 
2878     for (uintptr_t base = skip; ; base += skip) {
2879         base = ROUND_UP(base, align);
2880         if (pgb_try_mmap_set(ga, base, brk)) {
2881             return base;
2882         }
2883         if (base >= -skip) {
2884             return -1;
2885         }
2886     }
2887 }
2888 
2889 static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base,
2890                                IntervalTreeRoot *root)
2891 {
2892     for (int i = ga->nbounds - 1; i >= 0; --i) {
2893         uintptr_t s = base + ga->bounds[i][0];
2894         uintptr_t l = base + ga->bounds[i][1];
2895         IntervalTreeNode *n;
2896 
2897         if (l < s) {
2898             /* Wraparound. Skip to advance S to mmap_min_addr. */
2899             return mmap_min_addr - s;
2900         }
2901 
2902         n = interval_tree_iter_first(root, s, l);
2903         if (n != NULL) {
2904             /* Conflict.  Skip to advance S to LAST + 1. */
2905             return n->last - s + 1;
2906         }
2907     }
2908     return 0;  /* success */
2909 }
2910 
2911 static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root,
2912                                 uintptr_t align, uintptr_t brk)
2913 {
2914     uintptr_t last = mmap_min_addr;
2915     uintptr_t base, skip;
2916 
2917     while (true) {
2918         base = ROUND_UP(last, align);
2919         if (base < last) {
2920             return -1;
2921         }
2922 
2923         skip = pgb_try_itree(ga, base, root);
2924         if (skip == 0) {
2925             break;
2926         }
2927 
2928         last = base + skip;
2929         if (last < base) {
2930             return -1;
2931         }
2932     }
2933 
2934     /*
2935      * We've chosen 'base' based on holes in the interval tree,
2936      * but we don't yet know if it is a valid host address.
2937      * Because it is the first matching hole, if the host addresses
2938      * are invalid we know there are no further matches.
2939      */
2940     return pgb_try_mmap_set(ga, base, brk) ? base : -1;
2941 }
2942 
2943 static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr,
2944                         uintptr_t guest_hiaddr, uintptr_t align)
2945 {
2946     IntervalTreeRoot *root;
2947     uintptr_t brk, ret;
2948     PGBAddrs ga;
2949 
2950     /* Try the identity map first. */
2951     if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {
2952         brk = (uintptr_t)sbrk(0);
2953         if (pgb_try_mmap_set(&ga, 0, brk)) {
2954             guest_base = 0;
2955             return;
2956         }
2957     }
2958 
2959     /*
2960      * Rebuild the address set for non-identity map.
2961      * This differs in the mapping of the guest NULL page.
2962      */
2963     pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false);
2964 
2965     root = read_self_maps();
2966 
2967     /* Read brk after we've read the maps, which will malloc. */
2968     brk = (uintptr_t)sbrk(0);
2969 
2970     if (!root) {
2971         ret = pgb_find_fallback(&ga, align, brk);
2972     } else {
2973         /*
2974          * Reserve the area close to the host brk.
2975          * This will be freed with the rest of the tree.
2976          */
2977         IntervalTreeNode *b = g_new0(IntervalTreeNode, 1);
2978         b->start = brk;
2979         b->last = brk + 16 * MiB - 1;
2980         interval_tree_insert(b, root);
2981 
2982         ret = pgb_find_itree(&ga, root, align, brk);
2983         free_self_maps(root);
2984     }
2985 
2986     if (ret == -1) {
2987         int w = TARGET_LONG_BITS / 4;
2988 
2989         error_report("%s: Unable to find a guest_base to satisfy all "
2990                      "guest address mapping requirements", image_name);
2991 
2992         for (int i = 0; i < ga.nbounds; ++i) {
2993             error_printf("  %0*" PRIx64 "-%0*" PRIx64 "\n",
2994                          w, (uint64_t)ga.bounds[i][0],
2995                          w, (uint64_t)ga.bounds[i][1]);
2996         }
2997         exit(EXIT_FAILURE);
2998     }
2999     guest_base = ret;
3000 }
3001 
3002 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
3003                       abi_ulong guest_hiaddr)
3004 {
3005     /* In order to use host shmat, we must be able to honor SHMLBA.  */
3006     uintptr_t align = MAX(SHMLBA, TARGET_PAGE_SIZE);
3007 
3008     /* Sanity check the guest binary. */
3009     if (reserved_va) {
3010         if (guest_hiaddr > reserved_va) {
3011             error_report("%s: requires more than reserved virtual "
3012                          "address space (0x%" PRIx64 " > 0x%lx)",
3013                          image_name, (uint64_t)guest_hiaddr, reserved_va);
3014             exit(EXIT_FAILURE);
3015         }
3016     } else {
3017         if (guest_hiaddr != (uintptr_t)guest_hiaddr) {
3018             error_report("%s: requires more virtual address space "
3019                          "than the host can provide (0x%" PRIx64 ")",
3020                          image_name, (uint64_t)guest_hiaddr + 1);
3021             exit(EXIT_FAILURE);
3022         }
3023     }
3024 
3025     if (have_guest_base) {
3026         pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align);
3027     } else {
3028         pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align);
3029     }
3030 
3031     /* Reserve and initialize the commpage. */
3032     if (!init_guest_commpage()) {
3033         /* We have already probed for the commpage being free. */
3034         g_assert_not_reached();
3035     }
3036 
3037     assert(QEMU_IS_ALIGNED(guest_base, align));
3038     qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
3039                   "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
3040 }
3041 
3042 enum {
3043     /* The string "GNU\0" as a magic number. */
3044     GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
3045     NOTE_DATA_SZ = 1 * KiB,
3046     NOTE_NAME_SZ = 4,
3047     ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
3048 };
3049 
3050 /*
3051  * Process a single gnu_property entry.
3052  * Return false for error.
3053  */
3054 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
3055                                struct image_info *info, bool have_prev_type,
3056                                uint32_t *prev_type, Error **errp)
3057 {
3058     uint32_t pr_type, pr_datasz, step;
3059 
3060     if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
3061         goto error_data;
3062     }
3063     datasz -= *off;
3064     data += *off / sizeof(uint32_t);
3065 
3066     if (datasz < 2 * sizeof(uint32_t)) {
3067         goto error_data;
3068     }
3069     pr_type = data[0];
3070     pr_datasz = data[1];
3071     data += 2;
3072     datasz -= 2 * sizeof(uint32_t);
3073     step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
3074     if (step > datasz) {
3075         goto error_data;
3076     }
3077 
3078     /* Properties are supposed to be unique and sorted on pr_type. */
3079     if (have_prev_type && pr_type <= *prev_type) {
3080         if (pr_type == *prev_type) {
3081             error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
3082         } else {
3083             error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
3084         }
3085         return false;
3086     }
3087     *prev_type = pr_type;
3088 
3089     if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
3090         return false;
3091     }
3092 
3093     *off += 2 * sizeof(uint32_t) + step;
3094     return true;
3095 
3096  error_data:
3097     error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
3098     return false;
3099 }
3100 
3101 /* Process NT_GNU_PROPERTY_TYPE_0. */
3102 static bool parse_elf_properties(const ImageSource *src,
3103                                  struct image_info *info,
3104                                  const struct elf_phdr *phdr,
3105                                  Error **errp)
3106 {
3107     union {
3108         struct elf_note nhdr;
3109         uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
3110     } note;
3111 
3112     int n, off, datasz;
3113     bool have_prev_type;
3114     uint32_t prev_type;
3115 
3116     /* Unless the arch requires properties, ignore them. */
3117     if (!ARCH_USE_GNU_PROPERTY) {
3118         return true;
3119     }
3120 
3121     /* If the properties are crazy large, that's too bad. */
3122     n = phdr->p_filesz;
3123     if (n > sizeof(note)) {
3124         error_setg(errp, "PT_GNU_PROPERTY too large");
3125         return false;
3126     }
3127     if (n < sizeof(note.nhdr)) {
3128         error_setg(errp, "PT_GNU_PROPERTY too small");
3129         return false;
3130     }
3131 
3132     if (!imgsrc_read(&note, phdr->p_offset, n, src, errp)) {
3133         return false;
3134     }
3135 
3136     /*
3137      * The contents of a valid PT_GNU_PROPERTY is a sequence
3138      * of uint32_t -- swap them all now.
3139      */
3140 #ifdef BSWAP_NEEDED
3141     for (int i = 0; i < n / 4; i++) {
3142         bswap32s(note.data + i);
3143     }
3144 #endif
3145 
3146     /*
3147      * Note that nhdr is 3 words, and that the "name" described by namesz
3148      * immediately follows nhdr and is thus at the 4th word.  Further, all
3149      * of the inputs to the kernel's round_up are multiples of 4.
3150      */
3151     if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
3152         note.nhdr.n_namesz != NOTE_NAME_SZ ||
3153         note.data[3] != GNU0_MAGIC) {
3154         error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
3155         return false;
3156     }
3157     off = sizeof(note.nhdr) + NOTE_NAME_SZ;
3158 
3159     datasz = note.nhdr.n_descsz + off;
3160     if (datasz > n) {
3161         error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
3162         return false;
3163     }
3164 
3165     have_prev_type = false;
3166     prev_type = 0;
3167     while (1) {
3168         if (off == datasz) {
3169             return true;  /* end, exit ok */
3170         }
3171         if (!parse_elf_property(note.data, &off, datasz, info,
3172                                 have_prev_type, &prev_type, errp)) {
3173             return false;
3174         }
3175         have_prev_type = true;
3176     }
3177 }
3178 
3179 /**
3180  * load_elf_image: Load an ELF image into the address space.
3181  * @image_name: the filename of the image, to use in error messages.
3182  * @src: the ImageSource from which to read.
3183  * @info: info collected from the loaded image.
3184  * @ehdr: the ELF header, not yet bswapped.
3185  * @pinterp_name: record any PT_INTERP string found.
3186  *
3187  * On return: @info values will be filled in, as necessary or available.
3188  */
3189 
3190 static void load_elf_image(const char *image_name, const ImageSource *src,
3191                            struct image_info *info, struct elfhdr *ehdr,
3192                            char **pinterp_name)
3193 {
3194     g_autofree struct elf_phdr *phdr = NULL;
3195     abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
3196     int i, prot_exec;
3197     Error *err = NULL;
3198 
3199     /*
3200      * First of all, some simple consistency checks.
3201      * Note that we rely on the bswapped ehdr staying in bprm_buf,
3202      * for later use by load_elf_binary and create_elf_tables.
3203      */
3204     if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) {
3205         goto exit_errmsg;
3206     }
3207     if (!elf_check_ident(ehdr)) {
3208         error_setg(&err, "Invalid ELF image for this architecture");
3209         goto exit_errmsg;
3210     }
3211     bswap_ehdr(ehdr);
3212     if (!elf_check_ehdr(ehdr)) {
3213         error_setg(&err, "Invalid ELF image for this architecture");
3214         goto exit_errmsg;
3215     }
3216 
3217     phdr = imgsrc_read_alloc(ehdr->e_phoff,
3218                              ehdr->e_phnum * sizeof(struct elf_phdr),
3219                              src, &err);
3220     if (phdr == NULL) {
3221         goto exit_errmsg;
3222     }
3223     bswap_phdr(phdr, ehdr->e_phnum);
3224 
3225     info->nsegs = 0;
3226     info->pt_dynamic_addr = 0;
3227 
3228     mmap_lock();
3229 
3230     /*
3231      * Find the maximum size of the image and allocate an appropriate
3232      * amount of memory to handle that.  Locate the interpreter, if any.
3233      */
3234     loaddr = -1, hiaddr = 0;
3235     info->alignment = 0;
3236     info->exec_stack = EXSTACK_DEFAULT;
3237     for (i = 0; i < ehdr->e_phnum; ++i) {
3238         struct elf_phdr *eppnt = phdr + i;
3239         if (eppnt->p_type == PT_LOAD) {
3240             abi_ulong a = eppnt->p_vaddr & TARGET_PAGE_MASK;
3241             if (a < loaddr) {
3242                 loaddr = a;
3243             }
3244             a = eppnt->p_vaddr + eppnt->p_memsz - 1;
3245             if (a > hiaddr) {
3246                 hiaddr = a;
3247             }
3248             ++info->nsegs;
3249             info->alignment |= eppnt->p_align;
3250         } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
3251             g_autofree char *interp_name = NULL;
3252 
3253             if (*pinterp_name) {
3254                 error_setg(&err, "Multiple PT_INTERP entries");
3255                 goto exit_errmsg;
3256             }
3257 
3258             interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz,
3259                                             src, &err);
3260             if (interp_name == NULL) {
3261                 goto exit_errmsg;
3262             }
3263             if (interp_name[eppnt->p_filesz - 1] != 0) {
3264                 error_setg(&err, "Invalid PT_INTERP entry");
3265                 goto exit_errmsg;
3266             }
3267             *pinterp_name = g_steal_pointer(&interp_name);
3268         } else if (eppnt->p_type == PT_GNU_PROPERTY) {
3269             if (!parse_elf_properties(src, info, eppnt, &err)) {
3270                 goto exit_errmsg;
3271             }
3272         } else if (eppnt->p_type == PT_GNU_STACK) {
3273             info->exec_stack = eppnt->p_flags & PF_X;
3274         }
3275     }
3276 
3277     load_addr = loaddr;
3278 
3279     if (pinterp_name != NULL) {
3280         if (ehdr->e_type == ET_EXEC) {
3281             /*
3282              * Make sure that the low address does not conflict with
3283              * MMAP_MIN_ADDR or the QEMU application itself.
3284              */
3285             probe_guest_base(image_name, loaddr, hiaddr);
3286         } else {
3287             abi_ulong align;
3288 
3289             /*
3290              * The binary is dynamic, but we still need to
3291              * select guest_base.  In this case we pass a size.
3292              */
3293             probe_guest_base(image_name, 0, hiaddr - loaddr);
3294 
3295             /*
3296              * Avoid collision with the loader by providing a different
3297              * default load address.
3298              */
3299             load_addr += elf_et_dyn_base;
3300 
3301             /*
3302              * TODO: Better support for mmap alignment is desirable.
3303              * Since we do not have complete control over the guest
3304              * address space, we prefer the kernel to choose some address
3305              * rather than force the use of LOAD_ADDR via MAP_FIXED.
3306              * But without MAP_FIXED we cannot guarantee alignment,
3307              * only suggest it.
3308              */
3309             align = pow2ceil(info->alignment);
3310             if (align) {
3311                 load_addr &= -align;
3312             }
3313         }
3314     }
3315 
3316     /*
3317      * Reserve address space for all of this.
3318      *
3319      * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get
3320      * exactly the address range that is required.  Without reserved_va,
3321      * the guest address space is not isolated.  We have attempted to avoid
3322      * conflict with the host program itself via probe_guest_base, but using
3323      * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check.
3324      *
3325      * Otherwise this is ET_DYN, and we are searching for a location
3326      * that can hold the memory space required.  If the image is
3327      * pre-linked, LOAD_ADDR will be non-zero, and the kernel should
3328      * honor that address if it happens to be free.
3329      *
3330      * In both cases, we will overwrite pages in this range with mappings
3331      * from the executable.
3332      */
3333     load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE,
3334                             MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
3335                             (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0),
3336                             -1, 0);
3337     if (load_addr == -1) {
3338         goto exit_mmap;
3339     }
3340     load_bias = load_addr - loaddr;
3341 
3342     if (elf_is_fdpic(ehdr)) {
3343         struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
3344             g_malloc(sizeof(*loadsegs) * info->nsegs);
3345 
3346         for (i = 0; i < ehdr->e_phnum; ++i) {
3347             switch (phdr[i].p_type) {
3348             case PT_DYNAMIC:
3349                 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
3350                 break;
3351             case PT_LOAD:
3352                 loadsegs->addr = phdr[i].p_vaddr + load_bias;
3353                 loadsegs->p_vaddr = phdr[i].p_vaddr;
3354                 loadsegs->p_memsz = phdr[i].p_memsz;
3355                 ++loadsegs;
3356                 break;
3357             }
3358         }
3359     }
3360 
3361     info->load_bias = load_bias;
3362     info->code_offset = load_bias;
3363     info->data_offset = load_bias;
3364     info->load_addr = load_addr;
3365     info->entry = ehdr->e_entry + load_bias;
3366     info->start_code = -1;
3367     info->end_code = 0;
3368     info->start_data = -1;
3369     info->end_data = 0;
3370     /* Usual start for brk is after all sections of the main executable. */
3371     info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
3372     info->elf_flags = ehdr->e_flags;
3373 
3374     prot_exec = PROT_EXEC;
3375 #ifdef TARGET_AARCH64
3376     /*
3377      * If the BTI feature is present, this indicates that the executable
3378      * pages of the startup binary should be mapped with PROT_BTI, so that
3379      * branch targets are enforced.
3380      *
3381      * The startup binary is either the interpreter or the static executable.
3382      * The interpreter is responsible for all pages of a dynamic executable.
3383      *
3384      * Elf notes are backward compatible to older cpus.
3385      * Do not enable BTI unless it is supported.
3386      */
3387     if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
3388         && (pinterp_name == NULL || *pinterp_name == 0)
3389         && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
3390         prot_exec |= TARGET_PROT_BTI;
3391     }
3392 #endif
3393 
3394     for (i = 0; i < ehdr->e_phnum; i++) {
3395         struct elf_phdr *eppnt = phdr + i;
3396         if (eppnt->p_type == PT_LOAD) {
3397             abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
3398             int elf_prot = 0;
3399 
3400             if (eppnt->p_flags & PF_R) {
3401                 elf_prot |= PROT_READ;
3402             }
3403             if (eppnt->p_flags & PF_W) {
3404                 elf_prot |= PROT_WRITE;
3405             }
3406             if (eppnt->p_flags & PF_X) {
3407                 elf_prot |= prot_exec;
3408             }
3409 
3410             vaddr = load_bias + eppnt->p_vaddr;
3411             vaddr_po = vaddr & ~TARGET_PAGE_MASK;
3412             vaddr_ps = vaddr & TARGET_PAGE_MASK;
3413 
3414             vaddr_ef = vaddr + eppnt->p_filesz;
3415             vaddr_em = vaddr + eppnt->p_memsz;
3416 
3417             /*
3418              * Some segments may be completely empty, with a non-zero p_memsz
3419              * but no backing file segment.
3420              */
3421             if (eppnt->p_filesz != 0) {
3422                 error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
3423                                     elf_prot, MAP_PRIVATE | MAP_FIXED,
3424                                     src, eppnt->p_offset - vaddr_po);
3425                 if (error == -1) {
3426                     goto exit_mmap;
3427                 }
3428             }
3429 
3430             /* If the load segment requests extra zeros (e.g. bss), map it. */
3431             if (vaddr_ef < vaddr_em &&
3432                 !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) {
3433                 goto exit_errmsg;
3434             }
3435 
3436             /* Find the full program boundaries.  */
3437             if (elf_prot & PROT_EXEC) {
3438                 if (vaddr < info->start_code) {
3439                     info->start_code = vaddr;
3440                 }
3441                 if (vaddr_ef > info->end_code) {
3442                     info->end_code = vaddr_ef;
3443                 }
3444             }
3445             if (elf_prot & PROT_WRITE) {
3446                 if (vaddr < info->start_data) {
3447                     info->start_data = vaddr;
3448                 }
3449                 if (vaddr_ef > info->end_data) {
3450                     info->end_data = vaddr_ef;
3451                 }
3452             }
3453 #ifdef TARGET_MIPS
3454         } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
3455             Mips_elf_abiflags_v0 abiflags;
3456 
3457             if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags),
3458                              src, &err)) {
3459                 goto exit_errmsg;
3460             }
3461             bswap_mips_abiflags(&abiflags);
3462             info->fp_abi = abiflags.fp_abi;
3463 #endif
3464         }
3465     }
3466 
3467     if (info->end_data == 0) {
3468         info->start_data = info->end_code;
3469         info->end_data = info->end_code;
3470     }
3471 
3472     if (qemu_log_enabled()) {
3473         load_symbols(ehdr, src, load_bias);
3474     }
3475 
3476     debuginfo_report_elf(image_name, src->fd, load_bias);
3477 
3478     mmap_unlock();
3479 
3480     close(src->fd);
3481     return;
3482 
3483  exit_mmap:
3484     error_setg_errno(&err, errno, "Error mapping file");
3485     goto exit_errmsg;
3486  exit_errmsg:
3487     error_reportf_err(err, "%s: ", image_name);
3488     exit(-1);
3489 }
3490 
3491 static void load_elf_interp(const char *filename, struct image_info *info,
3492                             char bprm_buf[BPRM_BUF_SIZE])
3493 {
3494     struct elfhdr ehdr;
3495     ImageSource src;
3496     int fd, retval;
3497     Error *err = NULL;
3498 
3499     fd = open(path(filename), O_RDONLY);
3500     if (fd < 0) {
3501         error_setg_file_open(&err, errno, filename);
3502         error_report_err(err);
3503         exit(-1);
3504     }
3505 
3506     retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
3507     if (retval < 0) {
3508         error_setg_errno(&err, errno, "Error reading file header");
3509         error_reportf_err(err, "%s: ", filename);
3510         exit(-1);
3511     }
3512 
3513     src.fd = fd;
3514     src.cache = bprm_buf;
3515     src.cache_size = retval;
3516 
3517     load_elf_image(filename, &src, info, &ehdr, NULL);
3518 }
3519 
3520 #ifdef VDSO_HEADER
3521 #include VDSO_HEADER
3522 #define  vdso_image_info()  &vdso_image_info
3523 #else
3524 #define  vdso_image_info()  NULL
3525 #endif
3526 
3527 static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso)
3528 {
3529     ImageSource src;
3530     struct elfhdr ehdr;
3531     abi_ulong load_bias, load_addr;
3532 
3533     src.fd = -1;
3534     src.cache = vdso->image;
3535     src.cache_size = vdso->image_size;
3536 
3537     load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL);
3538     load_addr = info->load_addr;
3539     load_bias = info->load_bias;
3540 
3541     /*
3542      * We need to relocate the VDSO image.  The one built into the kernel
3543      * is built for a fixed address.  The one built for QEMU is not, since
3544      * that requires close control of the guest address space.
3545      * We pre-processed the image to locate all of the addresses that need
3546      * to be updated.
3547      */
3548     for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) {
3549         abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]);
3550         *addr = tswapal(tswapal(*addr) + load_bias);
3551     }
3552 
3553     /* Install signal trampolines, if present. */
3554     if (vdso->sigreturn_ofs) {
3555         default_sigreturn = load_addr + vdso->sigreturn_ofs;
3556     }
3557     if (vdso->rt_sigreturn_ofs) {
3558         default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs;
3559     }
3560 
3561     /* Remove write from VDSO segment. */
3562     target_mprotect(info->start_data, info->end_data - info->start_data,
3563                     PROT_READ | PROT_EXEC);
3564 }
3565 
3566 static int symfind(const void *s0, const void *s1)
3567 {
3568     struct elf_sym *sym = (struct elf_sym *)s1;
3569     __typeof(sym->st_value) addr = *(uint64_t *)s0;
3570     int result = 0;
3571 
3572     if (addr < sym->st_value) {
3573         result = -1;
3574     } else if (addr >= sym->st_value + sym->st_size) {
3575         result = 1;
3576     }
3577     return result;
3578 }
3579 
3580 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
3581 {
3582 #if ELF_CLASS == ELFCLASS32
3583     struct elf_sym *syms = s->disas_symtab.elf32;
3584 #else
3585     struct elf_sym *syms = s->disas_symtab.elf64;
3586 #endif
3587 
3588     // binary search
3589     struct elf_sym *sym;
3590 
3591     sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
3592     if (sym != NULL) {
3593         return s->disas_strtab + sym->st_name;
3594     }
3595 
3596     return "";
3597 }
3598 
3599 /* FIXME: This should use elf_ops.h.inc  */
3600 static int symcmp(const void *s0, const void *s1)
3601 {
3602     struct elf_sym *sym0 = (struct elf_sym *)s0;
3603     struct elf_sym *sym1 = (struct elf_sym *)s1;
3604     return (sym0->st_value < sym1->st_value)
3605         ? -1
3606         : ((sym0->st_value > sym1->st_value) ? 1 : 0);
3607 }
3608 
3609 /* Best attempt to load symbols from this ELF object. */
3610 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
3611                          abi_ulong load_bias)
3612 {
3613     int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
3614     g_autofree struct elf_shdr *shdr = NULL;
3615     char *strings = NULL;
3616     struct elf_sym *syms = NULL;
3617     struct elf_sym *new_syms;
3618     uint64_t segsz;
3619 
3620     shnum = hdr->e_shnum;
3621     shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr),
3622                              src, NULL);
3623     if (shdr == NULL) {
3624         return;
3625     }
3626 
3627     bswap_shdr(shdr, shnum);
3628     for (i = 0; i < shnum; ++i) {
3629         if (shdr[i].sh_type == SHT_SYMTAB) {
3630             sym_idx = i;
3631             str_idx = shdr[i].sh_link;
3632             goto found;
3633         }
3634     }
3635 
3636     /* There will be no symbol table if the file was stripped.  */
3637     return;
3638 
3639  found:
3640     /* Now know where the strtab and symtab are.  Snarf them.  */
3641 
3642     segsz = shdr[str_idx].sh_size;
3643     strings = g_try_malloc(segsz);
3644     if (!strings) {
3645         goto give_up;
3646     }
3647     if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) {
3648         goto give_up;
3649     }
3650 
3651     segsz = shdr[sym_idx].sh_size;
3652     if (segsz / sizeof(struct elf_sym) > INT_MAX) {
3653         /*
3654          * Implausibly large symbol table: give up rather than ploughing
3655          * on with the number of symbols calculation overflowing.
3656          */
3657         goto give_up;
3658     }
3659     nsyms = segsz / sizeof(struct elf_sym);
3660     syms = g_try_malloc(segsz);
3661     if (!syms) {
3662         goto give_up;
3663     }
3664     if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) {
3665         goto give_up;
3666     }
3667 
3668     for (i = 0; i < nsyms; ) {
3669         bswap_sym(syms + i);
3670         /* Throw away entries which we do not need.  */
3671         if (syms[i].st_shndx == SHN_UNDEF
3672             || syms[i].st_shndx >= SHN_LORESERVE
3673             || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3674             if (i < --nsyms) {
3675                 syms[i] = syms[nsyms];
3676             }
3677         } else {
3678 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3679             /* The bottom address bit marks a Thumb or MIPS16 symbol.  */
3680             syms[i].st_value &= ~(target_ulong)1;
3681 #endif
3682             syms[i].st_value += load_bias;
3683             i++;
3684         }
3685     }
3686 
3687     /* No "useful" symbol.  */
3688     if (nsyms == 0) {
3689         goto give_up;
3690     }
3691 
3692     /*
3693      * Attempt to free the storage associated with the local symbols
3694      * that we threw away.  Whether or not this has any effect on the
3695      * memory allocation depends on the malloc implementation and how
3696      * many symbols we managed to discard.
3697      */
3698     new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3699     if (new_syms == NULL) {
3700         goto give_up;
3701     }
3702     syms = new_syms;
3703 
3704     qsort(syms, nsyms, sizeof(*syms), symcmp);
3705 
3706     {
3707         struct syminfo *s = g_new(struct syminfo, 1);
3708 
3709         s->disas_strtab = strings;
3710         s->disas_num_syms = nsyms;
3711 #if ELF_CLASS == ELFCLASS32
3712         s->disas_symtab.elf32 = syms;
3713 #else
3714         s->disas_symtab.elf64 = syms;
3715 #endif
3716         s->lookup_symbol = lookup_symbolxx;
3717         s->next = syminfos;
3718         syminfos = s;
3719     }
3720     return;
3721 
3722  give_up:
3723     g_free(strings);
3724     g_free(syms);
3725 }
3726 
3727 uint32_t get_elf_eflags(int fd)
3728 {
3729     struct elfhdr ehdr;
3730     off_t offset;
3731     int ret;
3732 
3733     /* Read ELF header */
3734     offset = lseek(fd, 0, SEEK_SET);
3735     if (offset == (off_t) -1) {
3736         return 0;
3737     }
3738     ret = read(fd, &ehdr, sizeof(ehdr));
3739     if (ret < sizeof(ehdr)) {
3740         return 0;
3741     }
3742     offset = lseek(fd, offset, SEEK_SET);
3743     if (offset == (off_t) -1) {
3744         return 0;
3745     }
3746 
3747     /* Check ELF signature */
3748     if (!elf_check_ident(&ehdr)) {
3749         return 0;
3750     }
3751 
3752     /* check header */
3753     bswap_ehdr(&ehdr);
3754     if (!elf_check_ehdr(&ehdr)) {
3755         return 0;
3756     }
3757 
3758     /* return architecture id */
3759     return ehdr.e_flags;
3760 }
3761 
3762 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3763 {
3764     /*
3765      * We need a copy of the elf header for passing to create_elf_tables.
3766      * We will have overwritten the original when we re-use bprm->buf
3767      * while loading the interpreter.  Allocate the storage for this now
3768      * and let elf_load_image do any swapping that may be required.
3769      */
3770     struct elfhdr ehdr;
3771     struct image_info interp_info, vdso_info;
3772     char *elf_interpreter = NULL;
3773     char *scratch;
3774 
3775     memset(&interp_info, 0, sizeof(interp_info));
3776 #ifdef TARGET_MIPS
3777     interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3778 #endif
3779 
3780     load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter);
3781 
3782     /* Do this so that we can load the interpreter, if need be.  We will
3783        change some of these later */
3784     bprm->p = setup_arg_pages(bprm, info);
3785 
3786     scratch = g_new0(char, TARGET_PAGE_SIZE);
3787     if (STACK_GROWS_DOWN) {
3788         bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3789                                    bprm->p, info->stack_limit);
3790         info->file_string = bprm->p;
3791         bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3792                                    bprm->p, info->stack_limit);
3793         info->env_strings = bprm->p;
3794         bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3795                                    bprm->p, info->stack_limit);
3796         info->arg_strings = bprm->p;
3797     } else {
3798         info->arg_strings = bprm->p;
3799         bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3800                                    bprm->p, info->stack_limit);
3801         info->env_strings = bprm->p;
3802         bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3803                                    bprm->p, info->stack_limit);
3804         info->file_string = bprm->p;
3805         bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3806                                    bprm->p, info->stack_limit);
3807     }
3808 
3809     g_free(scratch);
3810 
3811     if (!bprm->p) {
3812         fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3813         exit(-1);
3814     }
3815 
3816     if (elf_interpreter) {
3817         load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3818 
3819         /*
3820          * While unusual because of ELF_ET_DYN_BASE, if we are unlucky
3821          * with the mappings the interpreter can be loaded above but
3822          * near the main executable, which can leave very little room
3823          * for the heap.
3824          * If the current brk has less than 16MB, use the end of the
3825          * interpreter.
3826          */
3827         if (interp_info.brk > info->brk &&
3828             interp_info.load_bias - info->brk < 16 * MiB)  {
3829             info->brk = interp_info.brk;
3830         }
3831 
3832         /* If the program interpreter is one of these two, then assume
3833            an iBCS2 image.  Otherwise assume a native linux image.  */
3834 
3835         if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3836             || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3837             info->personality = PER_SVR4;
3838 
3839             /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
3840                and some applications "depend" upon this behavior.  Since
3841                we do not have the power to recompile these, we emulate
3842                the SVr4 behavior.  Sigh.  */
3843             target_mmap(0, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC,
3844                         MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANONYMOUS,
3845                         -1, 0);
3846         }
3847 #ifdef TARGET_MIPS
3848         info->interp_fp_abi = interp_info.fp_abi;
3849 #endif
3850     }
3851 
3852     /*
3853      * Load a vdso if available, which will amongst other things contain the
3854      * signal trampolines.  Otherwise, allocate a separate page for them.
3855      */
3856     const VdsoImageInfo *vdso = vdso_image_info();
3857     if (vdso) {
3858         load_elf_vdso(&vdso_info, vdso);
3859         info->vdso = vdso_info.load_bias;
3860     } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) {
3861         abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE,
3862                                           PROT_READ | PROT_WRITE,
3863                                           MAP_PRIVATE | MAP_ANON, -1, 0);
3864         if (tramp_page == -1) {
3865             return -errno;
3866         }
3867 
3868         setup_sigtramp(tramp_page);
3869         target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC);
3870     }
3871 
3872     bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info,
3873                                 elf_interpreter ? &interp_info : NULL,
3874                                 vdso ? &vdso_info : NULL);
3875     info->start_stack = bprm->p;
3876 
3877     /* If we have an interpreter, set that as the program's entry point.
3878        Copy the load_bias as well, to help PPC64 interpret the entry
3879        point as a function descriptor.  Do this after creating elf tables
3880        so that we copy the original program entry point into the AUXV.  */
3881     if (elf_interpreter) {
3882         info->load_bias = interp_info.load_bias;
3883         info->entry = interp_info.entry;
3884         g_free(elf_interpreter);
3885     }
3886 
3887 #ifdef USE_ELF_CORE_DUMP
3888     bprm->core_dump = &elf_core_dump;
3889 #endif
3890 
3891     return 0;
3892 }
3893 
3894 #ifdef USE_ELF_CORE_DUMP
3895 #include "exec/translate-all.h"
3896 
3897 /*
3898  * Definitions to generate Intel SVR4-like core files.
3899  * These mostly have the same names as the SVR4 types with "target_elf_"
3900  * tacked on the front to prevent clashes with linux definitions,
3901  * and the typedef forms have been avoided.  This is mostly like
3902  * the SVR4 structure, but more Linuxy, with things that Linux does
3903  * not support and which gdb doesn't really use excluded.
3904  *
3905  * Fields we don't dump (their contents is zero) in linux-user qemu
3906  * are marked with XXX.
3907  *
3908  * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3909  *
3910  * Porting ELF coredump for target is (quite) simple process.  First you
3911  * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3912  * the target resides):
3913  *
3914  * #define USE_ELF_CORE_DUMP
3915  *
3916  * Next you define type of register set used for dumping.  ELF specification
3917  * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3918  *
3919  * typedef <target_regtype> target_elf_greg_t;
3920  * #define ELF_NREG <number of registers>
3921  * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3922  *
3923  * Last step is to implement target specific function that copies registers
3924  * from given cpu into just specified register set.  Prototype is:
3925  *
3926  * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3927  *                                const CPUArchState *env);
3928  *
3929  * Parameters:
3930  *     regs - copy register values into here (allocated and zeroed by caller)
3931  *     env - copy registers from here
3932  *
3933  * Example for ARM target is provided in this file.
3934  */
3935 
3936 struct target_elf_siginfo {
3937     abi_int    si_signo; /* signal number */
3938     abi_int    si_code;  /* extra code */
3939     abi_int    si_errno; /* errno */
3940 };
3941 
3942 struct target_elf_prstatus {
3943     struct target_elf_siginfo pr_info;      /* Info associated with signal */
3944     abi_short          pr_cursig;    /* Current signal */
3945     abi_ulong          pr_sigpend;   /* XXX */
3946     abi_ulong          pr_sighold;   /* XXX */
3947     target_pid_t       pr_pid;
3948     target_pid_t       pr_ppid;
3949     target_pid_t       pr_pgrp;
3950     target_pid_t       pr_sid;
3951     struct target_timeval pr_utime;  /* XXX User time */
3952     struct target_timeval pr_stime;  /* XXX System time */
3953     struct target_timeval pr_cutime; /* XXX Cumulative user time */
3954     struct target_timeval pr_cstime; /* XXX Cumulative system time */
3955     target_elf_gregset_t      pr_reg;       /* GP registers */
3956     abi_int            pr_fpvalid;   /* XXX */
3957 };
3958 
3959 #define ELF_PRARGSZ     (80) /* Number of chars for args */
3960 
3961 struct target_elf_prpsinfo {
3962     char         pr_state;       /* numeric process state */
3963     char         pr_sname;       /* char for pr_state */
3964     char         pr_zomb;        /* zombie */
3965     char         pr_nice;        /* nice val */
3966     abi_ulong    pr_flag;        /* flags */
3967     target_uid_t pr_uid;
3968     target_gid_t pr_gid;
3969     target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
3970     /* Lots missing */
3971     char    pr_fname[16] QEMU_NONSTRING; /* filename of executable */
3972     char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
3973 };
3974 
3975 #ifdef BSWAP_NEEDED
3976 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
3977 {
3978     prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3979     prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3980     prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
3981     prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
3982     prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3983     prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
3984     prstatus->pr_pid = tswap32(prstatus->pr_pid);
3985     prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3986     prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3987     prstatus->pr_sid = tswap32(prstatus->pr_sid);
3988     /* cpu times are not filled, so we skip them */
3989     /* regs should be in correct format already */
3990     prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3991 }
3992 
3993 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
3994 {
3995     psinfo->pr_flag = tswapal(psinfo->pr_flag);
3996     psinfo->pr_uid = tswap16(psinfo->pr_uid);
3997     psinfo->pr_gid = tswap16(psinfo->pr_gid);
3998     psinfo->pr_pid = tswap32(psinfo->pr_pid);
3999     psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
4000     psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
4001     psinfo->pr_sid = tswap32(psinfo->pr_sid);
4002 }
4003 
4004 static void bswap_note(struct elf_note *en)
4005 {
4006     bswap32s(&en->n_namesz);
4007     bswap32s(&en->n_descsz);
4008     bswap32s(&en->n_type);
4009 }
4010 #else
4011 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
4012 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
4013 static inline void bswap_note(struct elf_note *en) { }
4014 #endif /* BSWAP_NEEDED */
4015 
4016 /*
4017  * Calculate file (dump) size of given memory region.
4018  */
4019 static size_t vma_dump_size(target_ulong start, target_ulong end,
4020                             unsigned long flags)
4021 {
4022     /* The area must be readable. */
4023     if (!(flags & PAGE_READ)) {
4024         return 0;
4025     }
4026 
4027     /*
4028      * Usually we don't dump executable pages as they contain
4029      * non-writable code that debugger can read directly from
4030      * target library etc. If there is no elf header, we dump it.
4031      */
4032     if (!(flags & PAGE_WRITE_ORG) &&
4033         (flags & PAGE_EXEC) &&
4034         memcmp(g2h_untagged(start), ELFMAG, SELFMAG) == 0) {
4035         return 0;
4036     }
4037 
4038     return end - start;
4039 }
4040 
4041 static size_t size_note(const char *name, size_t datasz)
4042 {
4043     size_t namesz = strlen(name) + 1;
4044 
4045     namesz = ROUND_UP(namesz, 4);
4046     datasz = ROUND_UP(datasz, 4);
4047 
4048     return sizeof(struct elf_note) + namesz + datasz;
4049 }
4050 
4051 static void *fill_note(void **pptr, int type, const char *name, size_t datasz)
4052 {
4053     void *ptr = *pptr;
4054     struct elf_note *n = ptr;
4055     size_t namesz = strlen(name) + 1;
4056 
4057     n->n_namesz = namesz;
4058     n->n_descsz = datasz;
4059     n->n_type = type;
4060     bswap_note(n);
4061 
4062     ptr += sizeof(*n);
4063     memcpy(ptr, name, namesz);
4064 
4065     namesz = ROUND_UP(namesz, 4);
4066     datasz = ROUND_UP(datasz, 4);
4067 
4068     *pptr = ptr + namesz + datasz;
4069     return ptr + namesz;
4070 }
4071 
4072 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
4073                             uint32_t flags)
4074 {
4075     memcpy(elf->e_ident, ELFMAG, SELFMAG);
4076 
4077     elf->e_ident[EI_CLASS] = ELF_CLASS;
4078     elf->e_ident[EI_DATA] = ELF_DATA;
4079     elf->e_ident[EI_VERSION] = EV_CURRENT;
4080     elf->e_ident[EI_OSABI] = ELF_OSABI;
4081 
4082     elf->e_type = ET_CORE;
4083     elf->e_machine = machine;
4084     elf->e_version = EV_CURRENT;
4085     elf->e_phoff = sizeof(struct elfhdr);
4086     elf->e_flags = flags;
4087     elf->e_ehsize = sizeof(struct elfhdr);
4088     elf->e_phentsize = sizeof(struct elf_phdr);
4089     elf->e_phnum = segs;
4090 
4091     bswap_ehdr(elf);
4092 }
4093 
4094 static void fill_elf_note_phdr(struct elf_phdr *phdr, size_t sz, off_t offset)
4095 {
4096     phdr->p_type = PT_NOTE;
4097     phdr->p_offset = offset;
4098     phdr->p_filesz = sz;
4099 
4100     bswap_phdr(phdr, 1);
4101 }
4102 
4103 static void fill_prstatus_note(void *data, const TaskState *ts,
4104                                CPUState *cpu, int signr)
4105 {
4106     /*
4107      * Because note memory is only aligned to 4, and target_elf_prstatus
4108      * may well have higher alignment requirements, fill locally and
4109      * memcpy to the destination afterward.
4110      */
4111     struct target_elf_prstatus prstatus = {
4112         .pr_info.si_signo = signr,
4113         .pr_cursig = signr,
4114         .pr_pid = ts->ts_tid,
4115         .pr_ppid = getppid(),
4116         .pr_pgrp = getpgrp(),
4117         .pr_sid = getsid(0),
4118     };
4119 
4120     elf_core_copy_regs(&prstatus.pr_reg, cpu_env(cpu));
4121     bswap_prstatus(&prstatus);
4122     memcpy(data, &prstatus, sizeof(prstatus));
4123 }
4124 
4125 static void fill_prpsinfo_note(void *data, const TaskState *ts)
4126 {
4127     /*
4128      * Because note memory is only aligned to 4, and target_elf_prpsinfo
4129      * may well have higher alignment requirements, fill locally and
4130      * memcpy to the destination afterward.
4131      */
4132     struct target_elf_prpsinfo psinfo = {
4133         .pr_pid = getpid(),
4134         .pr_ppid = getppid(),
4135         .pr_pgrp = getpgrp(),
4136         .pr_sid = getsid(0),
4137         .pr_uid = getuid(),
4138         .pr_gid = getgid(),
4139     };
4140     char *base_filename;
4141     size_t len;
4142 
4143     len = ts->info->env_strings - ts->info->arg_strings;
4144     len = MIN(len, ELF_PRARGSZ);
4145     memcpy(&psinfo.pr_psargs, g2h_untagged(ts->info->arg_strings), len);
4146     for (size_t i = 0; i < len; i++) {
4147         if (psinfo.pr_psargs[i] == 0) {
4148             psinfo.pr_psargs[i] = ' ';
4149         }
4150     }
4151 
4152     base_filename = g_path_get_basename(ts->bprm->filename);
4153     /*
4154      * Using strncpy here is fine: at max-length,
4155      * this field is not NUL-terminated.
4156      */
4157     strncpy(psinfo.pr_fname, base_filename, sizeof(psinfo.pr_fname));
4158     g_free(base_filename);
4159 
4160     bswap_psinfo(&psinfo);
4161     memcpy(data, &psinfo, sizeof(psinfo));
4162 }
4163 
4164 static void fill_auxv_note(void *data, const TaskState *ts)
4165 {
4166     memcpy(data, g2h_untagged(ts->info->saved_auxv), ts->info->auxv_len);
4167 }
4168 
4169 /*
4170  * Constructs name of coredump file.  We have following convention
4171  * for the name:
4172  *     qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4173  *
4174  * Returns the filename
4175  */
4176 static char *core_dump_filename(const TaskState *ts)
4177 {
4178     g_autoptr(GDateTime) now = g_date_time_new_now_local();
4179     g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
4180     g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
4181 
4182     return g_strdup_printf("qemu_%s_%s_%d.core",
4183                            base_filename, nowstr, (int)getpid());
4184 }
4185 
4186 static int dump_write(int fd, const void *ptr, size_t size)
4187 {
4188     const char *bufp = (const char *)ptr;
4189     ssize_t bytes_written, bytes_left;
4190 
4191     bytes_written = 0;
4192     bytes_left = size;
4193 
4194     /*
4195      * In normal conditions, single write(2) should do but
4196      * in case of socket etc. this mechanism is more portable.
4197      */
4198     do {
4199         bytes_written = write(fd, bufp, bytes_left);
4200         if (bytes_written < 0) {
4201             if (errno == EINTR)
4202                 continue;
4203             return (-1);
4204         } else if (bytes_written == 0) { /* eof */
4205             return (-1);
4206         }
4207         bufp += bytes_written;
4208         bytes_left -= bytes_written;
4209     } while (bytes_left > 0);
4210 
4211     return (0);
4212 }
4213 
4214 static int wmr_page_unprotect_regions(void *opaque, target_ulong start,
4215                                       target_ulong end, unsigned long flags)
4216 {
4217     if ((flags & (PAGE_WRITE | PAGE_WRITE_ORG)) == PAGE_WRITE_ORG) {
4218         size_t step = MAX(TARGET_PAGE_SIZE, qemu_real_host_page_size());
4219 
4220         while (1) {
4221             page_unprotect(start, 0);
4222             if (end - start <= step) {
4223                 break;
4224             }
4225             start += step;
4226         }
4227     }
4228     return 0;
4229 }
4230 
4231 typedef struct {
4232     unsigned count;
4233     size_t size;
4234 } CountAndSizeRegions;
4235 
4236 static int wmr_count_and_size_regions(void *opaque, target_ulong start,
4237                                       target_ulong end, unsigned long flags)
4238 {
4239     CountAndSizeRegions *css = opaque;
4240 
4241     css->count++;
4242     css->size += vma_dump_size(start, end, flags);
4243     return 0;
4244 }
4245 
4246 typedef struct {
4247     struct elf_phdr *phdr;
4248     off_t offset;
4249 } FillRegionPhdr;
4250 
4251 static int wmr_fill_region_phdr(void *opaque, target_ulong start,
4252                                 target_ulong end, unsigned long flags)
4253 {
4254     FillRegionPhdr *d = opaque;
4255     struct elf_phdr *phdr = d->phdr;
4256 
4257     phdr->p_type = PT_LOAD;
4258     phdr->p_vaddr = start;
4259     phdr->p_paddr = 0;
4260     phdr->p_filesz = vma_dump_size(start, end, flags);
4261     phdr->p_offset = d->offset;
4262     d->offset += phdr->p_filesz;
4263     phdr->p_memsz = end - start;
4264     phdr->p_flags = (flags & PAGE_READ ? PF_R : 0)
4265                   | (flags & PAGE_WRITE_ORG ? PF_W : 0)
4266                   | (flags & PAGE_EXEC ? PF_X : 0);
4267     phdr->p_align = ELF_EXEC_PAGESIZE;
4268 
4269     bswap_phdr(phdr, 1);
4270     d->phdr = phdr + 1;
4271     return 0;
4272 }
4273 
4274 static int wmr_write_region(void *opaque, target_ulong start,
4275                             target_ulong end, unsigned long flags)
4276 {
4277     int fd = *(int *)opaque;
4278     size_t size = vma_dump_size(start, end, flags);
4279 
4280     if (!size) {
4281         return 0;
4282     }
4283     return dump_write(fd, g2h_untagged(start), size);
4284 }
4285 
4286 /*
4287  * Write out ELF coredump.
4288  *
4289  * See documentation of ELF object file format in:
4290  * http://www.caldera.com/developers/devspecs/gabi41.pdf
4291  *
4292  * Coredump format in linux is following:
4293  *
4294  * 0   +----------------------+         \
4295  *     | ELF header           | ET_CORE  |
4296  *     +----------------------+          |
4297  *     | ELF program headers  |          |--- headers
4298  *     | - NOTE section       |          |
4299  *     | - PT_LOAD sections   |          |
4300  *     +----------------------+         /
4301  *     | NOTEs:               |
4302  *     | - NT_PRSTATUS        |
4303  *     | - NT_PRSINFO         |
4304  *     | - NT_AUXV            |
4305  *     +----------------------+ <-- aligned to target page
4306  *     | Process memory dump  |
4307  *     :                      :
4308  *     .                      .
4309  *     :                      :
4310  *     |                      |
4311  *     +----------------------+
4312  *
4313  * NT_PRSTATUS -> struct elf_prstatus (per thread)
4314  * NT_PRSINFO  -> struct elf_prpsinfo
4315  * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4316  *
4317  * Format follows System V format as close as possible.  Current
4318  * version limitations are as follows:
4319  *     - no floating point registers are dumped
4320  *
4321  * Function returns 0 in case of success, negative errno otherwise.
4322  *
4323  * TODO: make this work also during runtime: it should be
4324  * possible to force coredump from running process and then
4325  * continue processing.  For example qemu could set up SIGUSR2
4326  * handler (provided that target process haven't registered
4327  * handler for that) that does the dump when signal is received.
4328  */
4329 static int elf_core_dump(int signr, const CPUArchState *env)
4330 {
4331     const CPUState *cpu = env_cpu((CPUArchState *)env);
4332     const TaskState *ts = (const TaskState *)get_task_state((CPUState *)cpu);
4333     struct rlimit dumpsize;
4334     CountAndSizeRegions css;
4335     off_t offset, note_offset, data_offset;
4336     size_t note_size;
4337     int cpus, ret;
4338     int fd = -1;
4339     CPUState *cpu_iter;
4340 
4341     if (prctl(PR_GET_DUMPABLE) == 0) {
4342         return 0;
4343     }
4344 
4345     if (getrlimit(RLIMIT_CORE, &dumpsize) < 0 || dumpsize.rlim_cur == 0) {
4346         return 0;
4347     }
4348 
4349     cpu_list_lock();
4350     mmap_lock();
4351 
4352     /* By unprotecting, we merge vmas that might be split. */
4353     walk_memory_regions(NULL, wmr_page_unprotect_regions);
4354 
4355     /*
4356      * Walk through target process memory mappings and
4357      * set up structure containing this information.
4358      */
4359     memset(&css, 0, sizeof(css));
4360     walk_memory_regions(&css, wmr_count_and_size_regions);
4361 
4362     cpus = 0;
4363     CPU_FOREACH(cpu_iter) {
4364         cpus++;
4365     }
4366 
4367     offset = sizeof(struct elfhdr);
4368     offset += (css.count + 1) * sizeof(struct elf_phdr);
4369     note_offset = offset;
4370 
4371     offset += size_note("CORE", ts->info->auxv_len);
4372     offset += size_note("CORE", sizeof(struct target_elf_prpsinfo));
4373     offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus;
4374     note_size = offset - note_offset;
4375     data_offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE);
4376 
4377     /* Do not dump if the corefile size exceeds the limit. */
4378     if (dumpsize.rlim_cur != RLIM_INFINITY
4379         && dumpsize.rlim_cur < data_offset + css.size) {
4380         errno = 0;
4381         goto out;
4382     }
4383 
4384     {
4385         g_autofree char *corefile = core_dump_filename(ts);
4386         fd = open(corefile, O_WRONLY | O_CREAT | O_TRUNC,
4387                   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
4388     }
4389     if (fd < 0) {
4390         goto out;
4391     }
4392 
4393     /*
4394      * There is a fair amount of alignment padding within the notes
4395      * as well as preceeding the process memory.  Allocate a zeroed
4396      * block to hold it all.  Write all of the headers directly into
4397      * this buffer and then write it out as a block.
4398      */
4399     {
4400         g_autofree void *header = g_malloc0(data_offset);
4401         FillRegionPhdr frp;
4402         void *hptr, *dptr;
4403 
4404         /* Create elf file header. */
4405         hptr = header;
4406         fill_elf_header(hptr, css.count + 1, ELF_MACHINE, 0);
4407         hptr += sizeof(struct elfhdr);
4408 
4409         /* Create elf program headers. */
4410         fill_elf_note_phdr(hptr, note_size, note_offset);
4411         hptr += sizeof(struct elf_phdr);
4412 
4413         frp.phdr = hptr;
4414         frp.offset = data_offset;
4415         walk_memory_regions(&frp, wmr_fill_region_phdr);
4416         hptr = frp.phdr;
4417 
4418         /* Create the notes. */
4419         dptr = fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len);
4420         fill_auxv_note(dptr, ts);
4421 
4422         dptr = fill_note(&hptr, NT_PRPSINFO, "CORE",
4423                          sizeof(struct target_elf_prpsinfo));
4424         fill_prpsinfo_note(dptr, ts);
4425 
4426         CPU_FOREACH(cpu_iter) {
4427             dptr = fill_note(&hptr, NT_PRSTATUS, "CORE",
4428                              sizeof(struct target_elf_prstatus));
4429             fill_prstatus_note(dptr, ts, cpu_iter,
4430                                cpu_iter == cpu ? signr : 0);
4431         }
4432 
4433         if (dump_write(fd, header, data_offset) < 0) {
4434             goto out;
4435         }
4436     }
4437 
4438     /*
4439      * Finally write process memory into the corefile as well.
4440      */
4441     if (walk_memory_regions(&fd, wmr_write_region) < 0) {
4442         goto out;
4443     }
4444     errno = 0;
4445 
4446  out:
4447     ret = -errno;
4448     mmap_unlock();
4449     cpu_list_unlock();
4450     if (fd >= 0) {
4451         close(fd);
4452     }
4453     return ret;
4454 }
4455 #endif /* USE_ELF_CORE_DUMP */
4456 
4457 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4458 {
4459     init_thread(regs, infop);
4460 }
4461