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