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