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