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