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