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