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