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