linux-user: Add ELF_PLATFORM for arm
[qemu/ar7.git] / linux-user / elfload.c
blob940a97a2f2e9cab034ef30b905c4fa58bc330df0
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>
7 #include "qemu.h"
8 #include "disas/disas.h"
9 #include "qemu/path.h"
11 #ifdef _ARCH_PPC64
12 #undef ARCH_DLINFO
13 #undef ELF_PLATFORM
14 #undef ELF_HWCAP
15 #undef ELF_HWCAP2
16 #undef ELF_CLASS
17 #undef ELF_DATA
18 #undef ELF_ARCH
19 #endif
21 #define ELF_OSABI ELFOSABI_SYSV
23 /* from personality.h */
26 * Flags for bug emulation.
28 * These occupy the top three bytes.
30 enum {
31 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
32 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
33 descriptors (signal handling) */
34 MMAP_PAGE_ZERO = 0x0100000,
35 ADDR_COMPAT_LAYOUT = 0x0200000,
36 READ_IMPLIES_EXEC = 0x0400000,
37 ADDR_LIMIT_32BIT = 0x0800000,
38 SHORT_INODE = 0x1000000,
39 WHOLE_SECONDS = 0x2000000,
40 STICKY_TIMEOUTS = 0x4000000,
41 ADDR_LIMIT_3GB = 0x8000000,
45 * Personality types.
47 * These go in the low byte. Avoid using the top bit, it will
48 * conflict with error returns.
50 enum {
51 PER_LINUX = 0x0000,
52 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
53 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
54 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
55 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
56 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
57 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
58 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
59 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
60 PER_BSD = 0x0006,
61 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
62 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
63 PER_LINUX32 = 0x0008,
64 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
65 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
66 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
67 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
68 PER_RISCOS = 0x000c,
69 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
70 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
71 PER_OSF4 = 0x000f, /* OSF/1 v4 */
72 PER_HPUX = 0x0010,
73 PER_MASK = 0x00ff,
77 * Return the base personality without flags.
79 #define personality(pers) (pers & PER_MASK)
81 int info_is_fdpic(struct image_info *info)
83 return info->personality == PER_LINUX_FDPIC;
86 /* this flag is uneffective under linux too, should be deleted */
87 #ifndef MAP_DENYWRITE
88 #define MAP_DENYWRITE 0
89 #endif
91 /* should probably go in elf.h */
92 #ifndef ELIBBAD
93 #define ELIBBAD 80
94 #endif
96 #ifdef TARGET_WORDS_BIGENDIAN
97 #define ELF_DATA ELFDATA2MSB
98 #else
99 #define ELF_DATA ELFDATA2LSB
100 #endif
102 #ifdef TARGET_ABI_MIPSN32
103 typedef abi_ullong target_elf_greg_t;
104 #define tswapreg(ptr) tswap64(ptr)
105 #else
106 typedef abi_ulong target_elf_greg_t;
107 #define tswapreg(ptr) tswapal(ptr)
108 #endif
110 #ifdef USE_UID16
111 typedef abi_ushort target_uid_t;
112 typedef abi_ushort target_gid_t;
113 #else
114 typedef abi_uint target_uid_t;
115 typedef abi_uint target_gid_t;
116 #endif
117 typedef abi_int target_pid_t;
119 #ifdef TARGET_I386
121 #define ELF_PLATFORM get_elf_platform()
123 static const char *get_elf_platform(void)
125 static char elf_platform[] = "i386";
126 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
127 if (family > 6)
128 family = 6;
129 if (family >= 3)
130 elf_platform[1] = '0' + family;
131 return elf_platform;
134 #define ELF_HWCAP get_elf_hwcap()
136 static uint32_t get_elf_hwcap(void)
138 X86CPU *cpu = X86_CPU(thread_cpu);
140 return cpu->env.features[FEAT_1_EDX];
143 #ifdef TARGET_X86_64
144 #define ELF_START_MMAP 0x2aaaaab000ULL
146 #define ELF_CLASS ELFCLASS64
147 #define ELF_ARCH EM_X86_64
149 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
151 regs->rax = 0;
152 regs->rsp = infop->start_stack;
153 regs->rip = infop->entry;
156 #define ELF_NREG 27
157 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
160 * Note that ELF_NREG should be 29 as there should be place for
161 * TRAPNO and ERR "registers" as well but linux doesn't dump
162 * those.
164 * See linux kernel: arch/x86/include/asm/elf.h
166 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
168 (*regs)[0] = env->regs[15];
169 (*regs)[1] = env->regs[14];
170 (*regs)[2] = env->regs[13];
171 (*regs)[3] = env->regs[12];
172 (*regs)[4] = env->regs[R_EBP];
173 (*regs)[5] = env->regs[R_EBX];
174 (*regs)[6] = env->regs[11];
175 (*regs)[7] = env->regs[10];
176 (*regs)[8] = env->regs[9];
177 (*regs)[9] = env->regs[8];
178 (*regs)[10] = env->regs[R_EAX];
179 (*regs)[11] = env->regs[R_ECX];
180 (*regs)[12] = env->regs[R_EDX];
181 (*regs)[13] = env->regs[R_ESI];
182 (*regs)[14] = env->regs[R_EDI];
183 (*regs)[15] = env->regs[R_EAX]; /* XXX */
184 (*regs)[16] = env->eip;
185 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
186 (*regs)[18] = env->eflags;
187 (*regs)[19] = env->regs[R_ESP];
188 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
189 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
190 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
191 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
192 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
193 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
194 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
197 #else
199 #define ELF_START_MMAP 0x80000000
202 * This is used to ensure we don't load something for the wrong architecture.
204 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
207 * These are used to set parameters in the core dumps.
209 #define ELF_CLASS ELFCLASS32
210 #define ELF_ARCH EM_386
212 static inline void init_thread(struct target_pt_regs *regs,
213 struct image_info *infop)
215 regs->esp = infop->start_stack;
216 regs->eip = infop->entry;
218 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
219 starts %edx contains a pointer to a function which might be
220 registered using `atexit'. This provides a mean for the
221 dynamic linker to call DT_FINI functions for shared libraries
222 that have been loaded before the code runs.
224 A value of 0 tells we have no such handler. */
225 regs->edx = 0;
228 #define ELF_NREG 17
229 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
232 * Note that ELF_NREG should be 19 as there should be place for
233 * TRAPNO and ERR "registers" as well but linux doesn't dump
234 * those.
236 * See linux kernel: arch/x86/include/asm/elf.h
238 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
240 (*regs)[0] = env->regs[R_EBX];
241 (*regs)[1] = env->regs[R_ECX];
242 (*regs)[2] = env->regs[R_EDX];
243 (*regs)[3] = env->regs[R_ESI];
244 (*regs)[4] = env->regs[R_EDI];
245 (*regs)[5] = env->regs[R_EBP];
246 (*regs)[6] = env->regs[R_EAX];
247 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
248 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
249 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
250 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
251 (*regs)[11] = env->regs[R_EAX]; /* XXX */
252 (*regs)[12] = env->eip;
253 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
254 (*regs)[14] = env->eflags;
255 (*regs)[15] = env->regs[R_ESP];
256 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
258 #endif
260 #define USE_ELF_CORE_DUMP
261 #define ELF_EXEC_PAGESIZE 4096
263 #endif
265 #ifdef TARGET_ARM
267 #ifndef TARGET_AARCH64
268 /* 32 bit ARM definitions */
270 #define ELF_START_MMAP 0x80000000
272 #define ELF_ARCH EM_ARM
273 #define ELF_CLASS ELFCLASS32
275 static inline void init_thread(struct target_pt_regs *regs,
276 struct image_info *infop)
278 abi_long stack = infop->start_stack;
279 memset(regs, 0, sizeof(*regs));
281 regs->uregs[16] = ARM_CPU_MODE_USR;
282 if (infop->entry & 1) {
283 regs->uregs[16] |= CPSR_T;
285 regs->uregs[15] = infop->entry & 0xfffffffe;
286 regs->uregs[13] = infop->start_stack;
287 /* FIXME - what to for failure of get_user()? */
288 get_user_ual(regs->uregs[2], stack + 8); /* envp */
289 get_user_ual(regs->uregs[1], stack + 4); /* envp */
290 /* XXX: it seems that r0 is zeroed after ! */
291 regs->uregs[0] = 0;
292 /* For uClinux PIC binaries. */
293 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
294 regs->uregs[10] = infop->start_data;
296 /* Support ARM FDPIC. */
297 if (info_is_fdpic(infop)) {
298 /* As described in the ABI document, r7 points to the loadmap info
299 * prepared by the kernel. If an interpreter is needed, r8 points
300 * to the interpreter loadmap and r9 points to the interpreter
301 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
302 * r9 points to the main program PT_DYNAMIC info.
304 regs->uregs[7] = infop->loadmap_addr;
305 if (infop->interpreter_loadmap_addr) {
306 /* Executable is dynamically loaded. */
307 regs->uregs[8] = infop->interpreter_loadmap_addr;
308 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
309 } else {
310 regs->uregs[8] = 0;
311 regs->uregs[9] = infop->pt_dynamic_addr;
316 #define ELF_NREG 18
317 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
319 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
321 (*regs)[0] = tswapreg(env->regs[0]);
322 (*regs)[1] = tswapreg(env->regs[1]);
323 (*regs)[2] = tswapreg(env->regs[2]);
324 (*regs)[3] = tswapreg(env->regs[3]);
325 (*regs)[4] = tswapreg(env->regs[4]);
326 (*regs)[5] = tswapreg(env->regs[5]);
327 (*regs)[6] = tswapreg(env->regs[6]);
328 (*regs)[7] = tswapreg(env->regs[7]);
329 (*regs)[8] = tswapreg(env->regs[8]);
330 (*regs)[9] = tswapreg(env->regs[9]);
331 (*regs)[10] = tswapreg(env->regs[10]);
332 (*regs)[11] = tswapreg(env->regs[11]);
333 (*regs)[12] = tswapreg(env->regs[12]);
334 (*regs)[13] = tswapreg(env->regs[13]);
335 (*regs)[14] = tswapreg(env->regs[14]);
336 (*regs)[15] = tswapreg(env->regs[15]);
338 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
339 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
342 #define USE_ELF_CORE_DUMP
343 #define ELF_EXEC_PAGESIZE 4096
345 enum
347 ARM_HWCAP_ARM_SWP = 1 << 0,
348 ARM_HWCAP_ARM_HALF = 1 << 1,
349 ARM_HWCAP_ARM_THUMB = 1 << 2,
350 ARM_HWCAP_ARM_26BIT = 1 << 3,
351 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
352 ARM_HWCAP_ARM_FPA = 1 << 5,
353 ARM_HWCAP_ARM_VFP = 1 << 6,
354 ARM_HWCAP_ARM_EDSP = 1 << 7,
355 ARM_HWCAP_ARM_JAVA = 1 << 8,
356 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
357 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
358 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
359 ARM_HWCAP_ARM_NEON = 1 << 12,
360 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
361 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
362 ARM_HWCAP_ARM_TLS = 1 << 15,
363 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
364 ARM_HWCAP_ARM_IDIVA = 1 << 17,
365 ARM_HWCAP_ARM_IDIVT = 1 << 18,
366 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
367 ARM_HWCAP_ARM_LPAE = 1 << 20,
368 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
371 enum {
372 ARM_HWCAP2_ARM_AES = 1 << 0,
373 ARM_HWCAP2_ARM_PMULL = 1 << 1,
374 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
375 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
376 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
379 /* The commpage only exists for 32 bit kernels */
381 /* Return 1 if the proposed guest space is suitable for the guest.
382 * Return 0 if the proposed guest space isn't suitable, but another
383 * address space should be tried.
384 * Return -1 if there is no way the proposed guest space can be
385 * valid regardless of the base.
386 * The guest code may leave a page mapped and populate it if the
387 * address is suitable.
389 static int init_guest_commpage(unsigned long guest_base,
390 unsigned long guest_size)
392 unsigned long real_start, test_page_addr;
394 /* We need to check that we can force a fault on access to the
395 * commpage at 0xffff0fxx
397 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
399 /* If the commpage lies within the already allocated guest space,
400 * then there is no way we can allocate it.
402 * You may be thinking that that this check is redundant because
403 * we already validated the guest size against MAX_RESERVED_VA;
404 * but if qemu_host_page_mask is unusually large, then
405 * test_page_addr may be lower.
407 if (test_page_addr >= guest_base
408 && test_page_addr < (guest_base + guest_size)) {
409 return -1;
412 /* Note it needs to be writeable to let us initialise it */
413 real_start = (unsigned long)
414 mmap((void *)test_page_addr, qemu_host_page_size,
415 PROT_READ | PROT_WRITE,
416 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
418 /* If we can't map it then try another address */
419 if (real_start == -1ul) {
420 return 0;
423 if (real_start != test_page_addr) {
424 /* OS didn't put the page where we asked - unmap and reject */
425 munmap((void *)real_start, qemu_host_page_size);
426 return 0;
429 /* Leave the page mapped
430 * Populate it (mmap should have left it all 0'd)
433 /* Kernel helper versions */
434 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
436 /* Now it's populated make it RO */
437 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
438 perror("Protecting guest commpage");
439 exit(-1);
442 return 1; /* All good */
445 #define ELF_HWCAP get_elf_hwcap()
446 #define ELF_HWCAP2 get_elf_hwcap2()
448 static uint32_t get_elf_hwcap(void)
450 ARMCPU *cpu = ARM_CPU(thread_cpu);
451 uint32_t hwcaps = 0;
453 hwcaps |= ARM_HWCAP_ARM_SWP;
454 hwcaps |= ARM_HWCAP_ARM_HALF;
455 hwcaps |= ARM_HWCAP_ARM_THUMB;
456 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
458 /* probe for the extra features */
459 #define GET_FEATURE(feat, hwcap) \
460 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
462 #define GET_FEATURE_ID(feat, hwcap) \
463 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
465 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
466 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
467 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
468 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
469 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
470 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
471 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
472 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
473 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
474 GET_FEATURE_ID(arm_div, ARM_HWCAP_ARM_IDIVA);
475 GET_FEATURE_ID(thumb_div, ARM_HWCAP_ARM_IDIVT);
476 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
477 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
478 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
479 * to our VFP_FP16 feature bit.
481 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
482 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
484 return hwcaps;
487 static uint32_t get_elf_hwcap2(void)
489 ARMCPU *cpu = ARM_CPU(thread_cpu);
490 uint32_t hwcaps = 0;
492 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
493 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
494 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
495 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
496 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
497 return hwcaps;
500 #undef GET_FEATURE
501 #undef GET_FEATURE_ID
503 #define ELF_PLATFORM get_elf_platform()
505 static const char *get_elf_platform(void)
507 CPUARMState *env = thread_cpu->env_ptr;
509 #ifdef TARGET_WORDS_BIGENDIAN
510 # define END "b"
511 #else
512 # define END "l"
513 #endif
515 if (arm_feature(env, ARM_FEATURE_V8)) {
516 return "v8" END;
517 } else if (arm_feature(env, ARM_FEATURE_V7)) {
518 if (arm_feature(env, ARM_FEATURE_M)) {
519 return "v7m" END;
520 } else {
521 return "v7" END;
523 } else if (arm_feature(env, ARM_FEATURE_V6)) {
524 return "v6" END;
525 } else if (arm_feature(env, ARM_FEATURE_V5)) {
526 return "v5" END;
527 } else {
528 return "v4" END;
531 #undef END
534 #else
535 /* 64 bit ARM definitions */
536 #define ELF_START_MMAP 0x80000000
538 #define ELF_ARCH EM_AARCH64
539 #define ELF_CLASS ELFCLASS64
540 #define ELF_PLATFORM "aarch64"
542 static inline void init_thread(struct target_pt_regs *regs,
543 struct image_info *infop)
545 abi_long stack = infop->start_stack;
546 memset(regs, 0, sizeof(*regs));
548 regs->pc = infop->entry & ~0x3ULL;
549 regs->sp = stack;
552 #define ELF_NREG 34
553 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
555 static void elf_core_copy_regs(target_elf_gregset_t *regs,
556 const CPUARMState *env)
558 int i;
560 for (i = 0; i < 32; i++) {
561 (*regs)[i] = tswapreg(env->xregs[i]);
563 (*regs)[32] = tswapreg(env->pc);
564 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
567 #define USE_ELF_CORE_DUMP
568 #define ELF_EXEC_PAGESIZE 4096
570 enum {
571 ARM_HWCAP_A64_FP = 1 << 0,
572 ARM_HWCAP_A64_ASIMD = 1 << 1,
573 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
574 ARM_HWCAP_A64_AES = 1 << 3,
575 ARM_HWCAP_A64_PMULL = 1 << 4,
576 ARM_HWCAP_A64_SHA1 = 1 << 5,
577 ARM_HWCAP_A64_SHA2 = 1 << 6,
578 ARM_HWCAP_A64_CRC32 = 1 << 7,
579 ARM_HWCAP_A64_ATOMICS = 1 << 8,
580 ARM_HWCAP_A64_FPHP = 1 << 9,
581 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
582 ARM_HWCAP_A64_CPUID = 1 << 11,
583 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
584 ARM_HWCAP_A64_JSCVT = 1 << 13,
585 ARM_HWCAP_A64_FCMA = 1 << 14,
586 ARM_HWCAP_A64_LRCPC = 1 << 15,
587 ARM_HWCAP_A64_DCPOP = 1 << 16,
588 ARM_HWCAP_A64_SHA3 = 1 << 17,
589 ARM_HWCAP_A64_SM3 = 1 << 18,
590 ARM_HWCAP_A64_SM4 = 1 << 19,
591 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
592 ARM_HWCAP_A64_SHA512 = 1 << 21,
593 ARM_HWCAP_A64_SVE = 1 << 22,
594 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
595 ARM_HWCAP_A64_DIT = 1 << 24,
596 ARM_HWCAP_A64_USCAT = 1 << 25,
597 ARM_HWCAP_A64_ILRCPC = 1 << 26,
598 ARM_HWCAP_A64_FLAGM = 1 << 27,
599 ARM_HWCAP_A64_SSBS = 1 << 28,
600 ARM_HWCAP_A64_SB = 1 << 29,
601 ARM_HWCAP_A64_PACA = 1 << 30,
602 ARM_HWCAP_A64_PACG = 1UL << 31,
605 #define ELF_HWCAP get_elf_hwcap()
607 static uint32_t get_elf_hwcap(void)
609 ARMCPU *cpu = ARM_CPU(thread_cpu);
610 uint32_t hwcaps = 0;
612 hwcaps |= ARM_HWCAP_A64_FP;
613 hwcaps |= ARM_HWCAP_A64_ASIMD;
614 hwcaps |= ARM_HWCAP_A64_CPUID;
616 /* probe for the extra features */
617 #define GET_FEATURE_ID(feat, hwcap) \
618 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
620 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
621 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
622 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
623 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
624 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
625 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
626 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
627 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
628 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
629 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
630 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
631 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
632 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
633 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
634 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
635 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
636 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
637 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
638 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
639 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
641 #undef GET_FEATURE_ID
643 return hwcaps;
646 #endif /* not TARGET_AARCH64 */
647 #endif /* TARGET_ARM */
649 #ifdef TARGET_SPARC
650 #ifdef TARGET_SPARC64
652 #define ELF_START_MMAP 0x80000000
653 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
654 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
655 #ifndef TARGET_ABI32
656 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
657 #else
658 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
659 #endif
661 #define ELF_CLASS ELFCLASS64
662 #define ELF_ARCH EM_SPARCV9
664 #define STACK_BIAS 2047
666 static inline void init_thread(struct target_pt_regs *regs,
667 struct image_info *infop)
669 #ifndef TARGET_ABI32
670 regs->tstate = 0;
671 #endif
672 regs->pc = infop->entry;
673 regs->npc = regs->pc + 4;
674 regs->y = 0;
675 #ifdef TARGET_ABI32
676 regs->u_regs[14] = infop->start_stack - 16 * 4;
677 #else
678 if (personality(infop->personality) == PER_LINUX32)
679 regs->u_regs[14] = infop->start_stack - 16 * 4;
680 else
681 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
682 #endif
685 #else
686 #define ELF_START_MMAP 0x80000000
687 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
688 | HWCAP_SPARC_MULDIV)
690 #define ELF_CLASS ELFCLASS32
691 #define ELF_ARCH EM_SPARC
693 static inline void init_thread(struct target_pt_regs *regs,
694 struct image_info *infop)
696 regs->psr = 0;
697 regs->pc = infop->entry;
698 regs->npc = regs->pc + 4;
699 regs->y = 0;
700 regs->u_regs[14] = infop->start_stack - 16 * 4;
703 #endif
704 #endif
706 #ifdef TARGET_PPC
708 #define ELF_MACHINE PPC_ELF_MACHINE
709 #define ELF_START_MMAP 0x80000000
711 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
713 #define elf_check_arch(x) ( (x) == EM_PPC64 )
715 #define ELF_CLASS ELFCLASS64
717 #else
719 #define ELF_CLASS ELFCLASS32
721 #endif
723 #define ELF_ARCH EM_PPC
725 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
726 See arch/powerpc/include/asm/cputable.h. */
727 enum {
728 QEMU_PPC_FEATURE_32 = 0x80000000,
729 QEMU_PPC_FEATURE_64 = 0x40000000,
730 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
731 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
732 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
733 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
734 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
735 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
736 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
737 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
738 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
739 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
740 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
741 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
742 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
743 QEMU_PPC_FEATURE_CELL = 0x00010000,
744 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
745 QEMU_PPC_FEATURE_SMT = 0x00004000,
746 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
747 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
748 QEMU_PPC_FEATURE_PA6T = 0x00000800,
749 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
750 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
751 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
752 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
753 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
755 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
756 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
758 /* Feature definitions in AT_HWCAP2. */
759 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
760 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
761 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
762 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
763 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
764 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
765 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
768 #define ELF_HWCAP get_elf_hwcap()
770 static uint32_t get_elf_hwcap(void)
772 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
773 uint32_t features = 0;
775 /* We don't have to be terribly complete here; the high points are
776 Altivec/FP/SPE support. Anything else is just a bonus. */
777 #define GET_FEATURE(flag, feature) \
778 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
779 #define GET_FEATURE2(flags, feature) \
780 do { \
781 if ((cpu->env.insns_flags2 & flags) == flags) { \
782 features |= feature; \
784 } while (0)
785 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
786 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
787 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
788 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
789 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
790 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
791 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
792 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
793 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
794 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
795 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
796 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
797 QEMU_PPC_FEATURE_ARCH_2_06);
798 #undef GET_FEATURE
799 #undef GET_FEATURE2
801 return features;
804 #define ELF_HWCAP2 get_elf_hwcap2()
806 static uint32_t get_elf_hwcap2(void)
808 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
809 uint32_t features = 0;
811 #define GET_FEATURE(flag, feature) \
812 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
813 #define GET_FEATURE2(flag, feature) \
814 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
816 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
817 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
818 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
819 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07);
820 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00);
822 #undef GET_FEATURE
823 #undef GET_FEATURE2
825 return features;
829 * The requirements here are:
830 * - keep the final alignment of sp (sp & 0xf)
831 * - make sure the 32-bit value at the first 16 byte aligned position of
832 * AUXV is greater than 16 for glibc compatibility.
833 * AT_IGNOREPPC is used for that.
834 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
835 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
837 #define DLINFO_ARCH_ITEMS 5
838 #define ARCH_DLINFO \
839 do { \
840 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
841 /* \
842 * Handle glibc compatibility: these magic entries must \
843 * be at the lowest addresses in the final auxv. \
844 */ \
845 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
846 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
847 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
848 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
849 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
850 } while (0)
852 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
854 _regs->gpr[1] = infop->start_stack;
855 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
856 if (get_ppc64_abi(infop) < 2) {
857 uint64_t val;
858 get_user_u64(val, infop->entry + 8);
859 _regs->gpr[2] = val + infop->load_bias;
860 get_user_u64(val, infop->entry);
861 infop->entry = val + infop->load_bias;
862 } else {
863 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
865 #endif
866 _regs->nip = infop->entry;
869 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
870 #define ELF_NREG 48
871 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
873 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
875 int i;
876 target_ulong ccr = 0;
878 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
879 (*regs)[i] = tswapreg(env->gpr[i]);
882 (*regs)[32] = tswapreg(env->nip);
883 (*regs)[33] = tswapreg(env->msr);
884 (*regs)[35] = tswapreg(env->ctr);
885 (*regs)[36] = tswapreg(env->lr);
886 (*regs)[37] = tswapreg(env->xer);
888 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
889 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
891 (*regs)[38] = tswapreg(ccr);
894 #define USE_ELF_CORE_DUMP
895 #define ELF_EXEC_PAGESIZE 4096
897 #endif
899 #ifdef TARGET_MIPS
901 #define ELF_START_MMAP 0x80000000
903 #ifdef TARGET_MIPS64
904 #define ELF_CLASS ELFCLASS64
905 #else
906 #define ELF_CLASS ELFCLASS32
907 #endif
908 #define ELF_ARCH EM_MIPS
910 #define elf_check_arch(x) ((x) == EM_MIPS || (x) == EM_NANOMIPS)
912 static inline void init_thread(struct target_pt_regs *regs,
913 struct image_info *infop)
915 regs->cp0_status = 2 << CP0St_KSU;
916 regs->cp0_epc = infop->entry;
917 regs->regs[29] = infop->start_stack;
920 /* See linux kernel: arch/mips/include/asm/elf.h. */
921 #define ELF_NREG 45
922 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
924 /* See linux kernel: arch/mips/include/asm/reg.h. */
925 enum {
926 #ifdef TARGET_MIPS64
927 TARGET_EF_R0 = 0,
928 #else
929 TARGET_EF_R0 = 6,
930 #endif
931 TARGET_EF_R26 = TARGET_EF_R0 + 26,
932 TARGET_EF_R27 = TARGET_EF_R0 + 27,
933 TARGET_EF_LO = TARGET_EF_R0 + 32,
934 TARGET_EF_HI = TARGET_EF_R0 + 33,
935 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
936 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
937 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
938 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
941 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
942 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
944 int i;
946 for (i = 0; i < TARGET_EF_R0; i++) {
947 (*regs)[i] = 0;
949 (*regs)[TARGET_EF_R0] = 0;
951 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
952 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
955 (*regs)[TARGET_EF_R26] = 0;
956 (*regs)[TARGET_EF_R27] = 0;
957 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
958 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
959 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
960 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
961 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
962 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
965 #define USE_ELF_CORE_DUMP
966 #define ELF_EXEC_PAGESIZE 4096
968 /* See arch/mips/include/uapi/asm/hwcap.h. */
969 enum {
970 HWCAP_MIPS_R6 = (1 << 0),
971 HWCAP_MIPS_MSA = (1 << 1),
974 #define ELF_HWCAP get_elf_hwcap()
976 static uint32_t get_elf_hwcap(void)
978 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
979 uint32_t hwcaps = 0;
981 #define GET_FEATURE(flag, hwcap) \
982 do { if (cpu->env.insn_flags & (flag)) { hwcaps |= hwcap; } } while (0)
984 GET_FEATURE(ISA_MIPS32R6 | ISA_MIPS64R6, HWCAP_MIPS_R6);
985 GET_FEATURE(ASE_MSA, HWCAP_MIPS_MSA);
987 #undef GET_FEATURE
989 return hwcaps;
992 #endif /* TARGET_MIPS */
994 #ifdef TARGET_MICROBLAZE
996 #define ELF_START_MMAP 0x80000000
998 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1000 #define ELF_CLASS ELFCLASS32
1001 #define ELF_ARCH EM_MICROBLAZE
1003 static inline void init_thread(struct target_pt_regs *regs,
1004 struct image_info *infop)
1006 regs->pc = infop->entry;
1007 regs->r1 = infop->start_stack;
1011 #define ELF_EXEC_PAGESIZE 4096
1013 #define USE_ELF_CORE_DUMP
1014 #define ELF_NREG 38
1015 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1017 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1018 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1020 int i, pos = 0;
1022 for (i = 0; i < 32; i++) {
1023 (*regs)[pos++] = tswapreg(env->regs[i]);
1026 for (i = 0; i < 6; i++) {
1027 (*regs)[pos++] = tswapreg(env->sregs[i]);
1031 #endif /* TARGET_MICROBLAZE */
1033 #ifdef TARGET_NIOS2
1035 #define ELF_START_MMAP 0x80000000
1037 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1039 #define ELF_CLASS ELFCLASS32
1040 #define ELF_ARCH EM_ALTERA_NIOS2
1042 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1044 regs->ea = infop->entry;
1045 regs->sp = infop->start_stack;
1046 regs->estatus = 0x3;
1049 #define ELF_EXEC_PAGESIZE 4096
1051 #define USE_ELF_CORE_DUMP
1052 #define ELF_NREG 49
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,
1057 const CPUNios2State *env)
1059 int i;
1061 (*regs)[0] = -1;
1062 for (i = 1; i < 8; i++) /* r0-r7 */
1063 (*regs)[i] = tswapreg(env->regs[i + 7]);
1065 for (i = 8; i < 16; i++) /* r8-r15 */
1066 (*regs)[i] = tswapreg(env->regs[i - 8]);
1068 for (i = 16; i < 24; i++) /* r16-r23 */
1069 (*regs)[i] = tswapreg(env->regs[i + 7]);
1070 (*regs)[24] = -1; /* R_ET */
1071 (*regs)[25] = -1; /* R_BT */
1072 (*regs)[26] = tswapreg(env->regs[R_GP]);
1073 (*regs)[27] = tswapreg(env->regs[R_SP]);
1074 (*regs)[28] = tswapreg(env->regs[R_FP]);
1075 (*regs)[29] = tswapreg(env->regs[R_EA]);
1076 (*regs)[30] = -1; /* R_SSTATUS */
1077 (*regs)[31] = tswapreg(env->regs[R_RA]);
1079 (*regs)[32] = tswapreg(env->regs[R_PC]);
1081 (*regs)[33] = -1; /* R_STATUS */
1082 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1084 for (i = 35; i < 49; i++) /* ... */
1085 (*regs)[i] = -1;
1088 #endif /* TARGET_NIOS2 */
1090 #ifdef TARGET_OPENRISC
1092 #define ELF_START_MMAP 0x08000000
1094 #define ELF_ARCH EM_OPENRISC
1095 #define ELF_CLASS ELFCLASS32
1096 #define ELF_DATA ELFDATA2MSB
1098 static inline void init_thread(struct target_pt_regs *regs,
1099 struct image_info *infop)
1101 regs->pc = infop->entry;
1102 regs->gpr[1] = infop->start_stack;
1105 #define USE_ELF_CORE_DUMP
1106 #define ELF_EXEC_PAGESIZE 8192
1108 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1109 #define ELF_NREG 34 /* gprs and pc, sr */
1110 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1112 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1113 const CPUOpenRISCState *env)
1115 int i;
1117 for (i = 0; i < 32; i++) {
1118 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1120 (*regs)[32] = tswapreg(env->pc);
1121 (*regs)[33] = tswapreg(cpu_get_sr(env));
1123 #define ELF_HWCAP 0
1124 #define ELF_PLATFORM NULL
1126 #endif /* TARGET_OPENRISC */
1128 #ifdef TARGET_SH4
1130 #define ELF_START_MMAP 0x80000000
1132 #define ELF_CLASS ELFCLASS32
1133 #define ELF_ARCH EM_SH
1135 static inline void init_thread(struct target_pt_regs *regs,
1136 struct image_info *infop)
1138 /* Check other registers XXXXX */
1139 regs->pc = infop->entry;
1140 regs->regs[15] = infop->start_stack;
1143 /* See linux kernel: arch/sh/include/asm/elf.h. */
1144 #define ELF_NREG 23
1145 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1147 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1148 enum {
1149 TARGET_REG_PC = 16,
1150 TARGET_REG_PR = 17,
1151 TARGET_REG_SR = 18,
1152 TARGET_REG_GBR = 19,
1153 TARGET_REG_MACH = 20,
1154 TARGET_REG_MACL = 21,
1155 TARGET_REG_SYSCALL = 22
1158 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1159 const CPUSH4State *env)
1161 int i;
1163 for (i = 0; i < 16; i++) {
1164 (*regs)[i] = tswapreg(env->gregs[i]);
1167 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1168 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1169 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1170 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1171 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1172 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1173 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1176 #define USE_ELF_CORE_DUMP
1177 #define ELF_EXEC_PAGESIZE 4096
1179 enum {
1180 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1181 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1182 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1183 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1184 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1185 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1186 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1187 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1188 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1189 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1192 #define ELF_HWCAP get_elf_hwcap()
1194 static uint32_t get_elf_hwcap(void)
1196 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1197 uint32_t hwcap = 0;
1199 hwcap |= SH_CPU_HAS_FPU;
1201 if (cpu->env.features & SH_FEATURE_SH4A) {
1202 hwcap |= SH_CPU_HAS_LLSC;
1205 return hwcap;
1208 #endif
1210 #ifdef TARGET_CRIS
1212 #define ELF_START_MMAP 0x80000000
1214 #define ELF_CLASS ELFCLASS32
1215 #define ELF_ARCH EM_CRIS
1217 static inline void init_thread(struct target_pt_regs *regs,
1218 struct image_info *infop)
1220 regs->erp = infop->entry;
1223 #define ELF_EXEC_PAGESIZE 8192
1225 #endif
1227 #ifdef TARGET_M68K
1229 #define ELF_START_MMAP 0x80000000
1231 #define ELF_CLASS ELFCLASS32
1232 #define ELF_ARCH EM_68K
1234 /* ??? Does this need to do anything?
1235 #define ELF_PLAT_INIT(_r) */
1237 static inline void init_thread(struct target_pt_regs *regs,
1238 struct image_info *infop)
1240 regs->usp = infop->start_stack;
1241 regs->sr = 0;
1242 regs->pc = infop->entry;
1245 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1246 #define ELF_NREG 20
1247 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1249 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1251 (*regs)[0] = tswapreg(env->dregs[1]);
1252 (*regs)[1] = tswapreg(env->dregs[2]);
1253 (*regs)[2] = tswapreg(env->dregs[3]);
1254 (*regs)[3] = tswapreg(env->dregs[4]);
1255 (*regs)[4] = tswapreg(env->dregs[5]);
1256 (*regs)[5] = tswapreg(env->dregs[6]);
1257 (*regs)[6] = tswapreg(env->dregs[7]);
1258 (*regs)[7] = tswapreg(env->aregs[0]);
1259 (*regs)[8] = tswapreg(env->aregs[1]);
1260 (*regs)[9] = tswapreg(env->aregs[2]);
1261 (*regs)[10] = tswapreg(env->aregs[3]);
1262 (*regs)[11] = tswapreg(env->aregs[4]);
1263 (*regs)[12] = tswapreg(env->aregs[5]);
1264 (*regs)[13] = tswapreg(env->aregs[6]);
1265 (*regs)[14] = tswapreg(env->dregs[0]);
1266 (*regs)[15] = tswapreg(env->aregs[7]);
1267 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1268 (*regs)[17] = tswapreg(env->sr);
1269 (*regs)[18] = tswapreg(env->pc);
1270 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1273 #define USE_ELF_CORE_DUMP
1274 #define ELF_EXEC_PAGESIZE 8192
1276 #endif
1278 #ifdef TARGET_ALPHA
1280 #define ELF_START_MMAP (0x30000000000ULL)
1282 #define ELF_CLASS ELFCLASS64
1283 #define ELF_ARCH EM_ALPHA
1285 static inline void init_thread(struct target_pt_regs *regs,
1286 struct image_info *infop)
1288 regs->pc = infop->entry;
1289 regs->ps = 8;
1290 regs->usp = infop->start_stack;
1293 #define ELF_EXEC_PAGESIZE 8192
1295 #endif /* TARGET_ALPHA */
1297 #ifdef TARGET_S390X
1299 #define ELF_START_MMAP (0x20000000000ULL)
1301 #define ELF_CLASS ELFCLASS64
1302 #define ELF_DATA ELFDATA2MSB
1303 #define ELF_ARCH EM_S390
1305 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1307 regs->psw.addr = infop->entry;
1308 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1309 regs->gprs[15] = infop->start_stack;
1312 #endif /* TARGET_S390X */
1314 #ifdef TARGET_TILEGX
1316 /* 42 bits real used address, a half for user mode */
1317 #define ELF_START_MMAP (0x00000020000000000ULL)
1319 #define elf_check_arch(x) ((x) == EM_TILEGX)
1321 #define ELF_CLASS ELFCLASS64
1322 #define ELF_DATA ELFDATA2LSB
1323 #define ELF_ARCH EM_TILEGX
1325 static inline void init_thread(struct target_pt_regs *regs,
1326 struct image_info *infop)
1328 regs->pc = infop->entry;
1329 regs->sp = infop->start_stack;
1333 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1335 #endif /* TARGET_TILEGX */
1337 #ifdef TARGET_RISCV
1339 #define ELF_START_MMAP 0x80000000
1340 #define ELF_ARCH EM_RISCV
1342 #ifdef TARGET_RISCV32
1343 #define ELF_CLASS ELFCLASS32
1344 #else
1345 #define ELF_CLASS ELFCLASS64
1346 #endif
1348 static inline void init_thread(struct target_pt_regs *regs,
1349 struct image_info *infop)
1351 regs->sepc = infop->entry;
1352 regs->sp = infop->start_stack;
1355 #define ELF_EXEC_PAGESIZE 4096
1357 #endif /* TARGET_RISCV */
1359 #ifdef TARGET_HPPA
1361 #define ELF_START_MMAP 0x80000000
1362 #define ELF_CLASS ELFCLASS32
1363 #define ELF_ARCH EM_PARISC
1364 #define ELF_PLATFORM "PARISC"
1365 #define STACK_GROWS_DOWN 0
1366 #define STACK_ALIGNMENT 64
1368 static inline void init_thread(struct target_pt_regs *regs,
1369 struct image_info *infop)
1371 regs->iaoq[0] = infop->entry;
1372 regs->iaoq[1] = infop->entry + 4;
1373 regs->gr[23] = 0;
1374 regs->gr[24] = infop->arg_start;
1375 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1376 /* The top-of-stack contains a linkage buffer. */
1377 regs->gr[30] = infop->start_stack + 64;
1378 regs->gr[31] = infop->entry;
1381 #endif /* TARGET_HPPA */
1383 #ifdef TARGET_XTENSA
1385 #define ELF_START_MMAP 0x20000000
1387 #define ELF_CLASS ELFCLASS32
1388 #define ELF_ARCH EM_XTENSA
1390 static inline void init_thread(struct target_pt_regs *regs,
1391 struct image_info *infop)
1393 regs->windowbase = 0;
1394 regs->windowstart = 1;
1395 regs->areg[1] = infop->start_stack;
1396 regs->pc = infop->entry;
1399 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1400 #define ELF_NREG 128
1401 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1403 enum {
1404 TARGET_REG_PC,
1405 TARGET_REG_PS,
1406 TARGET_REG_LBEG,
1407 TARGET_REG_LEND,
1408 TARGET_REG_LCOUNT,
1409 TARGET_REG_SAR,
1410 TARGET_REG_WINDOWSTART,
1411 TARGET_REG_WINDOWBASE,
1412 TARGET_REG_THREADPTR,
1413 TARGET_REG_AR0 = 64,
1416 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1417 const CPUXtensaState *env)
1419 unsigned i;
1421 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1422 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1423 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1424 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1425 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1426 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1427 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1428 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1429 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1430 xtensa_sync_phys_from_window((CPUXtensaState *)env);
1431 for (i = 0; i < env->config->nareg; ++i) {
1432 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1436 #define USE_ELF_CORE_DUMP
1437 #define ELF_EXEC_PAGESIZE 4096
1439 #endif /* TARGET_XTENSA */
1441 #ifndef ELF_PLATFORM
1442 #define ELF_PLATFORM (NULL)
1443 #endif
1445 #ifndef ELF_MACHINE
1446 #define ELF_MACHINE ELF_ARCH
1447 #endif
1449 #ifndef elf_check_arch
1450 #define elf_check_arch(x) ((x) == ELF_ARCH)
1451 #endif
1453 #ifndef ELF_HWCAP
1454 #define ELF_HWCAP 0
1455 #endif
1457 #ifndef STACK_GROWS_DOWN
1458 #define STACK_GROWS_DOWN 1
1459 #endif
1461 #ifndef STACK_ALIGNMENT
1462 #define STACK_ALIGNMENT 16
1463 #endif
1465 #ifdef TARGET_ABI32
1466 #undef ELF_CLASS
1467 #define ELF_CLASS ELFCLASS32
1468 #undef bswaptls
1469 #define bswaptls(ptr) bswap32s(ptr)
1470 #endif
1472 #include "elf.h"
1474 struct exec
1476 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1477 unsigned int a_text; /* length of text, in bytes */
1478 unsigned int a_data; /* length of data, in bytes */
1479 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1480 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1481 unsigned int a_entry; /* start address */
1482 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1483 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1487 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1488 #define OMAGIC 0407
1489 #define NMAGIC 0410
1490 #define ZMAGIC 0413
1491 #define QMAGIC 0314
1493 /* Necessary parameters */
1494 #define TARGET_ELF_EXEC_PAGESIZE \
1495 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1496 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1497 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1498 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1499 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1500 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1502 #define DLINFO_ITEMS 15
1504 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1506 memcpy(to, from, n);
1509 #ifdef BSWAP_NEEDED
1510 static void bswap_ehdr(struct elfhdr *ehdr)
1512 bswap16s(&ehdr->e_type); /* Object file type */
1513 bswap16s(&ehdr->e_machine); /* Architecture */
1514 bswap32s(&ehdr->e_version); /* Object file version */
1515 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1516 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1517 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1518 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1519 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1520 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1521 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1522 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1523 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1524 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1527 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1529 int i;
1530 for (i = 0; i < phnum; ++i, ++phdr) {
1531 bswap32s(&phdr->p_type); /* Segment type */
1532 bswap32s(&phdr->p_flags); /* Segment flags */
1533 bswaptls(&phdr->p_offset); /* Segment file offset */
1534 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1535 bswaptls(&phdr->p_paddr); /* Segment physical address */
1536 bswaptls(&phdr->p_filesz); /* Segment size in file */
1537 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1538 bswaptls(&phdr->p_align); /* Segment alignment */
1542 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1544 int i;
1545 for (i = 0; i < shnum; ++i, ++shdr) {
1546 bswap32s(&shdr->sh_name);
1547 bswap32s(&shdr->sh_type);
1548 bswaptls(&shdr->sh_flags);
1549 bswaptls(&shdr->sh_addr);
1550 bswaptls(&shdr->sh_offset);
1551 bswaptls(&shdr->sh_size);
1552 bswap32s(&shdr->sh_link);
1553 bswap32s(&shdr->sh_info);
1554 bswaptls(&shdr->sh_addralign);
1555 bswaptls(&shdr->sh_entsize);
1559 static void bswap_sym(struct elf_sym *sym)
1561 bswap32s(&sym->st_name);
1562 bswaptls(&sym->st_value);
1563 bswaptls(&sym->st_size);
1564 bswap16s(&sym->st_shndx);
1567 #ifdef TARGET_MIPS
1568 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
1570 bswap16s(&abiflags->version);
1571 bswap32s(&abiflags->ases);
1572 bswap32s(&abiflags->isa_ext);
1573 bswap32s(&abiflags->flags1);
1574 bswap32s(&abiflags->flags2);
1576 #endif
1577 #else
1578 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1579 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1580 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1581 static inline void bswap_sym(struct elf_sym *sym) { }
1582 #ifdef TARGET_MIPS
1583 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
1584 #endif
1585 #endif
1587 #ifdef USE_ELF_CORE_DUMP
1588 static int elf_core_dump(int, const CPUArchState *);
1589 #endif /* USE_ELF_CORE_DUMP */
1590 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1592 /* Verify the portions of EHDR within E_IDENT for the target.
1593 This can be performed before bswapping the entire header. */
1594 static bool elf_check_ident(struct elfhdr *ehdr)
1596 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1597 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1598 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1599 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1600 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1601 && ehdr->e_ident[EI_DATA] == ELF_DATA
1602 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1605 /* Verify the portions of EHDR outside of E_IDENT for the target.
1606 This has to wait until after bswapping the header. */
1607 static bool elf_check_ehdr(struct elfhdr *ehdr)
1609 return (elf_check_arch(ehdr->e_machine)
1610 && ehdr->e_ehsize == sizeof(struct elfhdr)
1611 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1612 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1616 * 'copy_elf_strings()' copies argument/envelope strings from user
1617 * memory to free pages in kernel mem. These are in a format ready
1618 * to be put directly into the top of new user memory.
1621 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1622 abi_ulong p, abi_ulong stack_limit)
1624 char *tmp;
1625 int len, i;
1626 abi_ulong top = p;
1628 if (!p) {
1629 return 0; /* bullet-proofing */
1632 if (STACK_GROWS_DOWN) {
1633 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1634 for (i = argc - 1; i >= 0; --i) {
1635 tmp = argv[i];
1636 if (!tmp) {
1637 fprintf(stderr, "VFS: argc is wrong");
1638 exit(-1);
1640 len = strlen(tmp) + 1;
1641 tmp += len;
1643 if (len > (p - stack_limit)) {
1644 return 0;
1646 while (len) {
1647 int bytes_to_copy = (len > offset) ? offset : len;
1648 tmp -= bytes_to_copy;
1649 p -= bytes_to_copy;
1650 offset -= bytes_to_copy;
1651 len -= bytes_to_copy;
1653 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1655 if (offset == 0) {
1656 memcpy_to_target(p, scratch, top - p);
1657 top = p;
1658 offset = TARGET_PAGE_SIZE;
1662 if (p != top) {
1663 memcpy_to_target(p, scratch + offset, top - p);
1665 } else {
1666 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1667 for (i = 0; i < argc; ++i) {
1668 tmp = argv[i];
1669 if (!tmp) {
1670 fprintf(stderr, "VFS: argc is wrong");
1671 exit(-1);
1673 len = strlen(tmp) + 1;
1674 if (len > (stack_limit - p)) {
1675 return 0;
1677 while (len) {
1678 int bytes_to_copy = (len > remaining) ? remaining : len;
1680 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1682 tmp += bytes_to_copy;
1683 remaining -= bytes_to_copy;
1684 p += bytes_to_copy;
1685 len -= bytes_to_copy;
1687 if (remaining == 0) {
1688 memcpy_to_target(top, scratch, p - top);
1689 top = p;
1690 remaining = TARGET_PAGE_SIZE;
1694 if (p != top) {
1695 memcpy_to_target(top, scratch, p - top);
1699 return p;
1702 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1703 * argument/environment space. Newer kernels (>2.6.33) allow more,
1704 * dependent on stack size, but guarantee at least 32 pages for
1705 * backwards compatibility.
1707 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1709 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1710 struct image_info *info)
1712 abi_ulong size, error, guard;
1714 size = guest_stack_size;
1715 if (size < STACK_LOWER_LIMIT) {
1716 size = STACK_LOWER_LIMIT;
1718 guard = TARGET_PAGE_SIZE;
1719 if (guard < qemu_real_host_page_size) {
1720 guard = qemu_real_host_page_size;
1723 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1724 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1725 if (error == -1) {
1726 perror("mmap stack");
1727 exit(-1);
1730 /* We reserve one extra page at the top of the stack as guard. */
1731 if (STACK_GROWS_DOWN) {
1732 target_mprotect(error, guard, PROT_NONE);
1733 info->stack_limit = error + guard;
1734 return info->stack_limit + size - sizeof(void *);
1735 } else {
1736 target_mprotect(error + size, guard, PROT_NONE);
1737 info->stack_limit = error + size;
1738 return error;
1742 /* Map and zero the bss. We need to explicitly zero any fractional pages
1743 after the data section (i.e. bss). */
1744 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1746 uintptr_t host_start, host_map_start, host_end;
1748 last_bss = TARGET_PAGE_ALIGN(last_bss);
1750 /* ??? There is confusion between qemu_real_host_page_size and
1751 qemu_host_page_size here and elsewhere in target_mmap, which
1752 may lead to the end of the data section mapping from the file
1753 not being mapped. At least there was an explicit test and
1754 comment for that here, suggesting that "the file size must
1755 be known". The comment probably pre-dates the introduction
1756 of the fstat system call in target_mmap which does in fact
1757 find out the size. What isn't clear is if the workaround
1758 here is still actually needed. For now, continue with it,
1759 but merge it with the "normal" mmap that would allocate the bss. */
1761 host_start = (uintptr_t) g2h(elf_bss);
1762 host_end = (uintptr_t) g2h(last_bss);
1763 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1765 if (host_map_start < host_end) {
1766 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1767 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1768 if (p == MAP_FAILED) {
1769 perror("cannot mmap brk");
1770 exit(-1);
1774 /* Ensure that the bss page(s) are valid */
1775 if ((page_get_flags(last_bss-1) & prot) != prot) {
1776 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1779 if (host_start < host_map_start) {
1780 memset((void *)host_start, 0, host_map_start - host_start);
1784 #ifdef TARGET_ARM
1785 static int elf_is_fdpic(struct elfhdr *exec)
1787 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
1789 #else
1790 /* Default implementation, always false. */
1791 static int elf_is_fdpic(struct elfhdr *exec)
1793 return 0;
1795 #endif
1797 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1799 uint16_t n;
1800 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1802 /* elf32_fdpic_loadseg */
1803 n = info->nsegs;
1804 while (n--) {
1805 sp -= 12;
1806 put_user_u32(loadsegs[n].addr, sp+0);
1807 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1808 put_user_u32(loadsegs[n].p_memsz, sp+8);
1811 /* elf32_fdpic_loadmap */
1812 sp -= 4;
1813 put_user_u16(0, sp+0); /* version */
1814 put_user_u16(info->nsegs, sp+2); /* nsegs */
1816 info->personality = PER_LINUX_FDPIC;
1817 info->loadmap_addr = sp;
1819 return sp;
1822 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1823 struct elfhdr *exec,
1824 struct image_info *info,
1825 struct image_info *interp_info)
1827 abi_ulong sp;
1828 abi_ulong u_argc, u_argv, u_envp, u_auxv;
1829 int size;
1830 int i;
1831 abi_ulong u_rand_bytes;
1832 uint8_t k_rand_bytes[16];
1833 abi_ulong u_platform;
1834 const char *k_platform;
1835 const int n = sizeof(elf_addr_t);
1837 sp = p;
1839 /* Needs to be before we load the env/argc/... */
1840 if (elf_is_fdpic(exec)) {
1841 /* Need 4 byte alignment for these structs */
1842 sp &= ~3;
1843 sp = loader_build_fdpic_loadmap(info, sp);
1844 info->other_info = interp_info;
1845 if (interp_info) {
1846 interp_info->other_info = info;
1847 sp = loader_build_fdpic_loadmap(interp_info, sp);
1848 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
1849 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
1850 } else {
1851 info->interpreter_loadmap_addr = 0;
1852 info->interpreter_pt_dynamic_addr = 0;
1856 u_platform = 0;
1857 k_platform = ELF_PLATFORM;
1858 if (k_platform) {
1859 size_t len = strlen(k_platform) + 1;
1860 if (STACK_GROWS_DOWN) {
1861 sp -= (len + n - 1) & ~(n - 1);
1862 u_platform = sp;
1863 /* FIXME - check return value of memcpy_to_target() for failure */
1864 memcpy_to_target(sp, k_platform, len);
1865 } else {
1866 memcpy_to_target(sp, k_platform, len);
1867 u_platform = sp;
1868 sp += len + 1;
1872 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1873 * the argv and envp pointers.
1875 if (STACK_GROWS_DOWN) {
1876 sp = QEMU_ALIGN_DOWN(sp, 16);
1877 } else {
1878 sp = QEMU_ALIGN_UP(sp, 16);
1882 * Generate 16 random bytes for userspace PRNG seeding (not
1883 * cryptically secure but it's not the aim of QEMU).
1885 for (i = 0; i < 16; i++) {
1886 k_rand_bytes[i] = rand();
1888 if (STACK_GROWS_DOWN) {
1889 sp -= 16;
1890 u_rand_bytes = sp;
1891 /* FIXME - check return value of memcpy_to_target() for failure */
1892 memcpy_to_target(sp, k_rand_bytes, 16);
1893 } else {
1894 memcpy_to_target(sp, k_rand_bytes, 16);
1895 u_rand_bytes = sp;
1896 sp += 16;
1899 size = (DLINFO_ITEMS + 1) * 2;
1900 if (k_platform)
1901 size += 2;
1902 #ifdef DLINFO_ARCH_ITEMS
1903 size += DLINFO_ARCH_ITEMS * 2;
1904 #endif
1905 #ifdef ELF_HWCAP2
1906 size += 2;
1907 #endif
1908 info->auxv_len = size * n;
1910 size += envc + argc + 2;
1911 size += 1; /* argc itself */
1912 size *= n;
1914 /* Allocate space and finalize stack alignment for entry now. */
1915 if (STACK_GROWS_DOWN) {
1916 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
1917 sp = u_argc;
1918 } else {
1919 u_argc = sp;
1920 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
1923 u_argv = u_argc + n;
1924 u_envp = u_argv + (argc + 1) * n;
1925 u_auxv = u_envp + (envc + 1) * n;
1926 info->saved_auxv = u_auxv;
1927 info->arg_start = u_argv;
1928 info->arg_end = u_argv + argc * n;
1930 /* This is correct because Linux defines
1931 * elf_addr_t as Elf32_Off / Elf64_Off
1933 #define NEW_AUX_ENT(id, val) do { \
1934 put_user_ual(id, u_auxv); u_auxv += n; \
1935 put_user_ual(val, u_auxv); u_auxv += n; \
1936 } while(0)
1938 #ifdef ARCH_DLINFO
1940 * ARCH_DLINFO must come first so platform specific code can enforce
1941 * special alignment requirements on the AUXV if necessary (eg. PPC).
1943 ARCH_DLINFO;
1944 #endif
1945 /* There must be exactly DLINFO_ITEMS entries here, or the assert
1946 * on info->auxv_len will trigger.
1948 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1949 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1950 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1951 if ((info->alignment & ~qemu_host_page_mask) != 0) {
1952 /* Target doesn't support host page size alignment */
1953 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
1954 } else {
1955 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
1956 qemu_host_page_size)));
1958 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1959 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1960 NEW_AUX_ENT(AT_ENTRY, info->entry);
1961 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1962 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1963 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1964 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1965 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1966 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1967 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1968 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
1970 #ifdef ELF_HWCAP2
1971 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1972 #endif
1974 if (u_platform) {
1975 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1977 NEW_AUX_ENT (AT_NULL, 0);
1978 #undef NEW_AUX_ENT
1980 /* Check that our initial calculation of the auxv length matches how much
1981 * we actually put into it.
1983 assert(info->auxv_len == u_auxv - info->saved_auxv);
1985 put_user_ual(argc, u_argc);
1987 p = info->arg_strings;
1988 for (i = 0; i < argc; ++i) {
1989 put_user_ual(p, u_argv);
1990 u_argv += n;
1991 p += target_strlen(p) + 1;
1993 put_user_ual(0, u_argv);
1995 p = info->env_strings;
1996 for (i = 0; i < envc; ++i) {
1997 put_user_ual(p, u_envp);
1998 u_envp += n;
1999 p += target_strlen(p) + 1;
2001 put_user_ual(0, u_envp);
2003 return sp;
2006 unsigned long init_guest_space(unsigned long host_start,
2007 unsigned long host_size,
2008 unsigned long guest_start,
2009 bool fixed)
2011 unsigned long current_start, aligned_start;
2012 int flags;
2014 assert(host_start || host_size);
2016 /* If just a starting address is given, then just verify that
2017 * address. */
2018 if (host_start && !host_size) {
2019 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2020 if (init_guest_commpage(host_start, host_size) != 1) {
2021 return (unsigned long)-1;
2023 #endif
2024 return host_start;
2027 /* Setup the initial flags and start address. */
2028 current_start = host_start & qemu_host_page_mask;
2029 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2030 if (fixed) {
2031 flags |= MAP_FIXED;
2034 /* Otherwise, a non-zero size region of memory needs to be mapped
2035 * and validated. */
2037 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2038 /* On 32-bit ARM, we need to map not just the usable memory, but
2039 * also the commpage. Try to find a suitable place by allocating
2040 * a big chunk for all of it. If host_start, then the naive
2041 * strategy probably does good enough.
2043 if (!host_start) {
2044 unsigned long guest_full_size, host_full_size, real_start;
2046 guest_full_size =
2047 (0xffff0f00 & qemu_host_page_mask) + qemu_host_page_size;
2048 host_full_size = guest_full_size - guest_start;
2049 real_start = (unsigned long)
2050 mmap(NULL, host_full_size, PROT_NONE, flags, -1, 0);
2051 if (real_start == (unsigned long)-1) {
2052 if (host_size < host_full_size - qemu_host_page_size) {
2053 /* We failed to map a continous segment, but we're
2054 * allowed to have a gap between the usable memory and
2055 * the commpage where other things can be mapped.
2056 * This sparseness gives us more flexibility to find
2057 * an address range.
2059 goto naive;
2061 return (unsigned long)-1;
2063 munmap((void *)real_start, host_full_size);
2064 if (real_start & ~qemu_host_page_mask) {
2065 /* The same thing again, but with an extra qemu_host_page_size
2066 * so that we can shift around alignment.
2068 unsigned long real_size = host_full_size + qemu_host_page_size;
2069 real_start = (unsigned long)
2070 mmap(NULL, real_size, PROT_NONE, flags, -1, 0);
2071 if (real_start == (unsigned long)-1) {
2072 if (host_size < host_full_size - qemu_host_page_size) {
2073 goto naive;
2075 return (unsigned long)-1;
2077 munmap((void *)real_start, real_size);
2078 real_start = HOST_PAGE_ALIGN(real_start);
2080 current_start = real_start;
2082 naive:
2083 #endif
2085 while (1) {
2086 unsigned long real_start, real_size, aligned_size;
2087 aligned_size = real_size = host_size;
2089 /* Do not use mmap_find_vma here because that is limited to the
2090 * guest address space. We are going to make the
2091 * guest address space fit whatever we're given.
2093 real_start = (unsigned long)
2094 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
2095 if (real_start == (unsigned long)-1) {
2096 return (unsigned long)-1;
2099 /* Check to see if the address is valid. */
2100 if (host_start && real_start != current_start) {
2101 goto try_again;
2104 /* Ensure the address is properly aligned. */
2105 if (real_start & ~qemu_host_page_mask) {
2106 /* Ideally, we adjust like
2108 * pages: [ ][ ][ ][ ][ ]
2109 * old: [ real ]
2110 * [ aligned ]
2111 * new: [ real ]
2112 * [ aligned ]
2114 * But if there is something else mapped right after it,
2115 * then obviously it won't have room to grow, and the
2116 * kernel will put the new larger real someplace else with
2117 * unknown alignment (if we made it to here, then
2118 * fixed=false). Which is why we grow real by a full page
2119 * size, instead of by part of one; so that even if we get
2120 * moved, we can still guarantee alignment. But this does
2121 * mean that there is a padding of < 1 page both before
2122 * and after the aligned range; the "after" could could
2123 * cause problems for ARM emulation where it could butt in
2124 * to where we need to put the commpage.
2126 munmap((void *)real_start, host_size);
2127 real_size = aligned_size + qemu_host_page_size;
2128 real_start = (unsigned long)
2129 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
2130 if (real_start == (unsigned long)-1) {
2131 return (unsigned long)-1;
2133 aligned_start = HOST_PAGE_ALIGN(real_start);
2134 } else {
2135 aligned_start = real_start;
2138 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2139 /* On 32-bit ARM, we need to also be able to map the commpage. */
2140 int valid = init_guest_commpage(aligned_start - guest_start,
2141 aligned_size + guest_start);
2142 if (valid == -1) {
2143 munmap((void *)real_start, real_size);
2144 return (unsigned long)-1;
2145 } else if (valid == 0) {
2146 goto try_again;
2148 #endif
2150 /* If nothing has said `return -1` or `goto try_again` yet,
2151 * then the address we have is good.
2153 break;
2155 try_again:
2156 /* That address didn't work. Unmap and try a different one.
2157 * The address the host picked because is typically right at
2158 * the top of the host address space and leaves the guest with
2159 * no usable address space. Resort to a linear search. We
2160 * already compensated for mmap_min_addr, so this should not
2161 * happen often. Probably means we got unlucky and host
2162 * address space randomization put a shared library somewhere
2163 * inconvenient.
2165 * This is probably a good strategy if host_start, but is
2166 * probably a bad strategy if not, which means we got here
2167 * because of trouble with ARM commpage setup.
2169 munmap((void *)real_start, real_size);
2170 current_start += qemu_host_page_size;
2171 if (host_start == current_start) {
2172 /* Theoretically possible if host doesn't have any suitably
2173 * aligned areas. Normally the first mmap will fail.
2175 return (unsigned long)-1;
2179 qemu_log_mask(CPU_LOG_PAGE, "Reserved 0x%lx bytes of guest address space\n", host_size);
2181 return aligned_start;
2184 static void probe_guest_base(const char *image_name,
2185 abi_ulong loaddr, abi_ulong hiaddr)
2187 /* Probe for a suitable guest base address, if the user has not set
2188 * it explicitly, and set guest_base appropriately.
2189 * In case of error we will print a suitable message and exit.
2191 const char *errmsg;
2192 if (!have_guest_base && !reserved_va) {
2193 unsigned long host_start, real_start, host_size;
2195 /* Round addresses to page boundaries. */
2196 loaddr &= qemu_host_page_mask;
2197 hiaddr = HOST_PAGE_ALIGN(hiaddr);
2199 if (loaddr < mmap_min_addr) {
2200 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
2201 } else {
2202 host_start = loaddr;
2203 if (host_start != loaddr) {
2204 errmsg = "Address overflow loading ELF binary";
2205 goto exit_errmsg;
2208 host_size = hiaddr - loaddr;
2210 /* Setup the initial guest memory space with ranges gleaned from
2211 * the ELF image that is being loaded.
2213 real_start = init_guest_space(host_start, host_size, loaddr, false);
2214 if (real_start == (unsigned long)-1) {
2215 errmsg = "Unable to find space for application";
2216 goto exit_errmsg;
2218 guest_base = real_start - loaddr;
2220 qemu_log_mask(CPU_LOG_PAGE, "Relocating guest address space from 0x"
2221 TARGET_ABI_FMT_lx " to 0x%lx\n",
2222 loaddr, real_start);
2224 return;
2226 exit_errmsg:
2227 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2228 exit(-1);
2232 /* Load an ELF image into the address space.
2234 IMAGE_NAME is the filename of the image, to use in error messages.
2235 IMAGE_FD is the open file descriptor for the image.
2237 BPRM_BUF is a copy of the beginning of the file; this of course
2238 contains the elf file header at offset 0. It is assumed that this
2239 buffer is sufficiently aligned to present no problems to the host
2240 in accessing data at aligned offsets within the buffer.
2242 On return: INFO values will be filled in, as necessary or available. */
2244 static void load_elf_image(const char *image_name, int image_fd,
2245 struct image_info *info, char **pinterp_name,
2246 char bprm_buf[BPRM_BUF_SIZE])
2248 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2249 struct elf_phdr *phdr;
2250 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
2251 int i, retval;
2252 const char *errmsg;
2254 /* First of all, some simple consistency checks */
2255 errmsg = "Invalid ELF image for this architecture";
2256 if (!elf_check_ident(ehdr)) {
2257 goto exit_errmsg;
2259 bswap_ehdr(ehdr);
2260 if (!elf_check_ehdr(ehdr)) {
2261 goto exit_errmsg;
2264 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2265 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2266 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
2267 } else {
2268 phdr = (struct elf_phdr *) alloca(i);
2269 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
2270 if (retval != i) {
2271 goto exit_read;
2274 bswap_phdr(phdr, ehdr->e_phnum);
2276 info->nsegs = 0;
2277 info->pt_dynamic_addr = 0;
2279 mmap_lock();
2281 /* Find the maximum size of the image and allocate an appropriate
2282 amount of memory to handle that. */
2283 loaddr = -1, hiaddr = 0;
2284 info->alignment = 0;
2285 for (i = 0; i < ehdr->e_phnum; ++i) {
2286 if (phdr[i].p_type == PT_LOAD) {
2287 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
2288 if (a < loaddr) {
2289 loaddr = a;
2291 a = phdr[i].p_vaddr + phdr[i].p_memsz;
2292 if (a > hiaddr) {
2293 hiaddr = a;
2295 ++info->nsegs;
2296 info->alignment |= phdr[i].p_align;
2300 load_addr = loaddr;
2301 if (ehdr->e_type == ET_DYN) {
2302 /* The image indicates that it can be loaded anywhere. Find a
2303 location that can hold the memory space required. If the
2304 image is pre-linked, LOADDR will be non-zero. Since we do
2305 not supply MAP_FIXED here we'll use that address if and
2306 only if it remains available. */
2307 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2308 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
2309 -1, 0);
2310 if (load_addr == -1) {
2311 goto exit_perror;
2313 } else if (pinterp_name != NULL) {
2314 /* This is the main executable. Make sure that the low
2315 address does not conflict with MMAP_MIN_ADDR or the
2316 QEMU application itself. */
2317 probe_guest_base(image_name, loaddr, hiaddr);
2319 load_bias = load_addr - loaddr;
2321 if (elf_is_fdpic(ehdr)) {
2322 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2323 g_malloc(sizeof(*loadsegs) * info->nsegs);
2325 for (i = 0; i < ehdr->e_phnum; ++i) {
2326 switch (phdr[i].p_type) {
2327 case PT_DYNAMIC:
2328 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2329 break;
2330 case PT_LOAD:
2331 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2332 loadsegs->p_vaddr = phdr[i].p_vaddr;
2333 loadsegs->p_memsz = phdr[i].p_memsz;
2334 ++loadsegs;
2335 break;
2340 info->load_bias = load_bias;
2341 info->load_addr = load_addr;
2342 info->entry = ehdr->e_entry + load_bias;
2343 info->start_code = -1;
2344 info->end_code = 0;
2345 info->start_data = -1;
2346 info->end_data = 0;
2347 info->brk = 0;
2348 info->elf_flags = ehdr->e_flags;
2350 for (i = 0; i < ehdr->e_phnum; i++) {
2351 struct elf_phdr *eppnt = phdr + i;
2352 if (eppnt->p_type == PT_LOAD) {
2353 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
2354 int elf_prot = 0;
2356 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
2357 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
2358 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
2360 vaddr = load_bias + eppnt->p_vaddr;
2361 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2362 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2363 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
2365 error = target_mmap(vaddr_ps, vaddr_len,
2366 elf_prot, MAP_PRIVATE | MAP_FIXED,
2367 image_fd, eppnt->p_offset - vaddr_po);
2368 if (error == -1) {
2369 goto exit_perror;
2372 vaddr_ef = vaddr + eppnt->p_filesz;
2373 vaddr_em = vaddr + eppnt->p_memsz;
2375 /* If the load segment requests extra zeros (e.g. bss), map it. */
2376 if (vaddr_ef < vaddr_em) {
2377 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2380 /* Find the full program boundaries. */
2381 if (elf_prot & PROT_EXEC) {
2382 if (vaddr < info->start_code) {
2383 info->start_code = vaddr;
2385 if (vaddr_ef > info->end_code) {
2386 info->end_code = vaddr_ef;
2389 if (elf_prot & PROT_WRITE) {
2390 if (vaddr < info->start_data) {
2391 info->start_data = vaddr;
2393 if (vaddr_ef > info->end_data) {
2394 info->end_data = vaddr_ef;
2396 if (vaddr_em > info->brk) {
2397 info->brk = vaddr_em;
2400 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2401 char *interp_name;
2403 if (*pinterp_name) {
2404 errmsg = "Multiple PT_INTERP entries";
2405 goto exit_errmsg;
2407 interp_name = malloc(eppnt->p_filesz);
2408 if (!interp_name) {
2409 goto exit_perror;
2412 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2413 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2414 eppnt->p_filesz);
2415 } else {
2416 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2417 eppnt->p_offset);
2418 if (retval != eppnt->p_filesz) {
2419 goto exit_perror;
2422 if (interp_name[eppnt->p_filesz - 1] != 0) {
2423 errmsg = "Invalid PT_INTERP entry";
2424 goto exit_errmsg;
2426 *pinterp_name = interp_name;
2427 #ifdef TARGET_MIPS
2428 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
2429 Mips_elf_abiflags_v0 abiflags;
2430 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
2431 errmsg = "Invalid PT_MIPS_ABIFLAGS entry";
2432 goto exit_errmsg;
2434 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2435 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
2436 sizeof(Mips_elf_abiflags_v0));
2437 } else {
2438 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
2439 eppnt->p_offset);
2440 if (retval != sizeof(Mips_elf_abiflags_v0)) {
2441 goto exit_perror;
2444 bswap_mips_abiflags(&abiflags);
2445 info->fp_abi = abiflags.fp_abi;
2446 #endif
2450 if (info->end_data == 0) {
2451 info->start_data = info->end_code;
2452 info->end_data = info->end_code;
2453 info->brk = info->end_code;
2456 if (qemu_log_enabled()) {
2457 load_symbols(ehdr, image_fd, load_bias);
2460 mmap_unlock();
2462 close(image_fd);
2463 return;
2465 exit_read:
2466 if (retval >= 0) {
2467 errmsg = "Incomplete read of file header";
2468 goto exit_errmsg;
2470 exit_perror:
2471 errmsg = strerror(errno);
2472 exit_errmsg:
2473 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2474 exit(-1);
2477 static void load_elf_interp(const char *filename, struct image_info *info,
2478 char bprm_buf[BPRM_BUF_SIZE])
2480 int fd, retval;
2482 fd = open(path(filename), O_RDONLY);
2483 if (fd < 0) {
2484 goto exit_perror;
2487 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2488 if (retval < 0) {
2489 goto exit_perror;
2491 if (retval < BPRM_BUF_SIZE) {
2492 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2495 load_elf_image(filename, fd, info, NULL, bprm_buf);
2496 return;
2498 exit_perror:
2499 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2500 exit(-1);
2503 static int symfind(const void *s0, const void *s1)
2505 target_ulong addr = *(target_ulong *)s0;
2506 struct elf_sym *sym = (struct elf_sym *)s1;
2507 int result = 0;
2508 if (addr < sym->st_value) {
2509 result = -1;
2510 } else if (addr >= sym->st_value + sym->st_size) {
2511 result = 1;
2513 return result;
2516 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2518 #if ELF_CLASS == ELFCLASS32
2519 struct elf_sym *syms = s->disas_symtab.elf32;
2520 #else
2521 struct elf_sym *syms = s->disas_symtab.elf64;
2522 #endif
2524 // binary search
2525 struct elf_sym *sym;
2527 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2528 if (sym != NULL) {
2529 return s->disas_strtab + sym->st_name;
2532 return "";
2535 /* FIXME: This should use elf_ops.h */
2536 static int symcmp(const void *s0, const void *s1)
2538 struct elf_sym *sym0 = (struct elf_sym *)s0;
2539 struct elf_sym *sym1 = (struct elf_sym *)s1;
2540 return (sym0->st_value < sym1->st_value)
2541 ? -1
2542 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2545 /* Best attempt to load symbols from this ELF object. */
2546 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2548 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2549 uint64_t segsz;
2550 struct elf_shdr *shdr;
2551 char *strings = NULL;
2552 struct syminfo *s = NULL;
2553 struct elf_sym *new_syms, *syms = NULL;
2555 shnum = hdr->e_shnum;
2556 i = shnum * sizeof(struct elf_shdr);
2557 shdr = (struct elf_shdr *)alloca(i);
2558 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2559 return;
2562 bswap_shdr(shdr, shnum);
2563 for (i = 0; i < shnum; ++i) {
2564 if (shdr[i].sh_type == SHT_SYMTAB) {
2565 sym_idx = i;
2566 str_idx = shdr[i].sh_link;
2567 goto found;
2571 /* There will be no symbol table if the file was stripped. */
2572 return;
2574 found:
2575 /* Now know where the strtab and symtab are. Snarf them. */
2576 s = g_try_new(struct syminfo, 1);
2577 if (!s) {
2578 goto give_up;
2581 segsz = shdr[str_idx].sh_size;
2582 s->disas_strtab = strings = g_try_malloc(segsz);
2583 if (!strings ||
2584 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
2585 goto give_up;
2588 segsz = shdr[sym_idx].sh_size;
2589 syms = g_try_malloc(segsz);
2590 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
2591 goto give_up;
2594 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
2595 /* Implausibly large symbol table: give up rather than ploughing
2596 * on with the number of symbols calculation overflowing
2598 goto give_up;
2600 nsyms = segsz / sizeof(struct elf_sym);
2601 for (i = 0; i < nsyms; ) {
2602 bswap_sym(syms + i);
2603 /* Throw away entries which we do not need. */
2604 if (syms[i].st_shndx == SHN_UNDEF
2605 || syms[i].st_shndx >= SHN_LORESERVE
2606 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2607 if (i < --nsyms) {
2608 syms[i] = syms[nsyms];
2610 } else {
2611 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2612 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2613 syms[i].st_value &= ~(target_ulong)1;
2614 #endif
2615 syms[i].st_value += load_bias;
2616 i++;
2620 /* No "useful" symbol. */
2621 if (nsyms == 0) {
2622 goto give_up;
2625 /* Attempt to free the storage associated with the local symbols
2626 that we threw away. Whether or not this has any effect on the
2627 memory allocation depends on the malloc implementation and how
2628 many symbols we managed to discard. */
2629 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
2630 if (new_syms == NULL) {
2631 goto give_up;
2633 syms = new_syms;
2635 qsort(syms, nsyms, sizeof(*syms), symcmp);
2637 s->disas_num_syms = nsyms;
2638 #if ELF_CLASS == ELFCLASS32
2639 s->disas_symtab.elf32 = syms;
2640 #else
2641 s->disas_symtab.elf64 = syms;
2642 #endif
2643 s->lookup_symbol = lookup_symbolxx;
2644 s->next = syminfos;
2645 syminfos = s;
2647 return;
2649 give_up:
2650 g_free(s);
2651 g_free(strings);
2652 g_free(syms);
2655 uint32_t get_elf_eflags(int fd)
2657 struct elfhdr ehdr;
2658 off_t offset;
2659 int ret;
2661 /* Read ELF header */
2662 offset = lseek(fd, 0, SEEK_SET);
2663 if (offset == (off_t) -1) {
2664 return 0;
2666 ret = read(fd, &ehdr, sizeof(ehdr));
2667 if (ret < sizeof(ehdr)) {
2668 return 0;
2670 offset = lseek(fd, offset, SEEK_SET);
2671 if (offset == (off_t) -1) {
2672 return 0;
2675 /* Check ELF signature */
2676 if (!elf_check_ident(&ehdr)) {
2677 return 0;
2680 /* check header */
2681 bswap_ehdr(&ehdr);
2682 if (!elf_check_ehdr(&ehdr)) {
2683 return 0;
2686 /* return architecture id */
2687 return ehdr.e_flags;
2690 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2692 struct image_info interp_info;
2693 struct elfhdr elf_ex;
2694 char *elf_interpreter = NULL;
2695 char *scratch;
2697 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2699 load_elf_image(bprm->filename, bprm->fd, info,
2700 &elf_interpreter, bprm->buf);
2702 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2703 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2704 when we load the interpreter. */
2705 elf_ex = *(struct elfhdr *)bprm->buf;
2707 /* Do this so that we can load the interpreter, if need be. We will
2708 change some of these later */
2709 bprm->p = setup_arg_pages(bprm, info);
2711 scratch = g_new0(char, TARGET_PAGE_SIZE);
2712 if (STACK_GROWS_DOWN) {
2713 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2714 bprm->p, info->stack_limit);
2715 info->file_string = bprm->p;
2716 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2717 bprm->p, info->stack_limit);
2718 info->env_strings = bprm->p;
2719 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2720 bprm->p, info->stack_limit);
2721 info->arg_strings = bprm->p;
2722 } else {
2723 info->arg_strings = bprm->p;
2724 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2725 bprm->p, info->stack_limit);
2726 info->env_strings = bprm->p;
2727 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2728 bprm->p, info->stack_limit);
2729 info->file_string = bprm->p;
2730 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2731 bprm->p, info->stack_limit);
2734 g_free(scratch);
2736 if (!bprm->p) {
2737 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2738 exit(-1);
2741 if (elf_interpreter) {
2742 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2744 /* If the program interpreter is one of these two, then assume
2745 an iBCS2 image. Otherwise assume a native linux image. */
2747 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2748 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2749 info->personality = PER_SVR4;
2751 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2752 and some applications "depend" upon this behavior. Since
2753 we do not have the power to recompile these, we emulate
2754 the SVr4 behavior. Sigh. */
2755 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2756 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2758 #ifdef TARGET_MIPS
2759 info->interp_fp_abi = interp_info.fp_abi;
2760 #endif
2763 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2764 info, (elf_interpreter ? &interp_info : NULL));
2765 info->start_stack = bprm->p;
2767 /* If we have an interpreter, set that as the program's entry point.
2768 Copy the load_bias as well, to help PPC64 interpret the entry
2769 point as a function descriptor. Do this after creating elf tables
2770 so that we copy the original program entry point into the AUXV. */
2771 if (elf_interpreter) {
2772 info->load_bias = interp_info.load_bias;
2773 info->entry = interp_info.entry;
2774 free(elf_interpreter);
2777 #ifdef USE_ELF_CORE_DUMP
2778 bprm->core_dump = &elf_core_dump;
2779 #endif
2781 return 0;
2784 #ifdef USE_ELF_CORE_DUMP
2786 * Definitions to generate Intel SVR4-like core files.
2787 * These mostly have the same names as the SVR4 types with "target_elf_"
2788 * tacked on the front to prevent clashes with linux definitions,
2789 * and the typedef forms have been avoided. This is mostly like
2790 * the SVR4 structure, but more Linuxy, with things that Linux does
2791 * not support and which gdb doesn't really use excluded.
2793 * Fields we don't dump (their contents is zero) in linux-user qemu
2794 * are marked with XXX.
2796 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2798 * Porting ELF coredump for target is (quite) simple process. First you
2799 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2800 * the target resides):
2802 * #define USE_ELF_CORE_DUMP
2804 * Next you define type of register set used for dumping. ELF specification
2805 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2807 * typedef <target_regtype> target_elf_greg_t;
2808 * #define ELF_NREG <number of registers>
2809 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2811 * Last step is to implement target specific function that copies registers
2812 * from given cpu into just specified register set. Prototype is:
2814 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2815 * const CPUArchState *env);
2817 * Parameters:
2818 * regs - copy register values into here (allocated and zeroed by caller)
2819 * env - copy registers from here
2821 * Example for ARM target is provided in this file.
2824 /* An ELF note in memory */
2825 struct memelfnote {
2826 const char *name;
2827 size_t namesz;
2828 size_t namesz_rounded;
2829 int type;
2830 size_t datasz;
2831 size_t datasz_rounded;
2832 void *data;
2833 size_t notesz;
2836 struct target_elf_siginfo {
2837 abi_int si_signo; /* signal number */
2838 abi_int si_code; /* extra code */
2839 abi_int si_errno; /* errno */
2842 struct target_elf_prstatus {
2843 struct target_elf_siginfo pr_info; /* Info associated with signal */
2844 abi_short pr_cursig; /* Current signal */
2845 abi_ulong pr_sigpend; /* XXX */
2846 abi_ulong pr_sighold; /* XXX */
2847 target_pid_t pr_pid;
2848 target_pid_t pr_ppid;
2849 target_pid_t pr_pgrp;
2850 target_pid_t pr_sid;
2851 struct target_timeval pr_utime; /* XXX User time */
2852 struct target_timeval pr_stime; /* XXX System time */
2853 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2854 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2855 target_elf_gregset_t pr_reg; /* GP registers */
2856 abi_int pr_fpvalid; /* XXX */
2859 #define ELF_PRARGSZ (80) /* Number of chars for args */
2861 struct target_elf_prpsinfo {
2862 char pr_state; /* numeric process state */
2863 char pr_sname; /* char for pr_state */
2864 char pr_zomb; /* zombie */
2865 char pr_nice; /* nice val */
2866 abi_ulong pr_flag; /* flags */
2867 target_uid_t pr_uid;
2868 target_gid_t pr_gid;
2869 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2870 /* Lots missing */
2871 char pr_fname[16]; /* filename of executable */
2872 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2875 /* Here is the structure in which status of each thread is captured. */
2876 struct elf_thread_status {
2877 QTAILQ_ENTRY(elf_thread_status) ets_link;
2878 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2879 #if 0
2880 elf_fpregset_t fpu; /* NT_PRFPREG */
2881 struct task_struct *thread;
2882 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2883 #endif
2884 struct memelfnote notes[1];
2885 int num_notes;
2888 struct elf_note_info {
2889 struct memelfnote *notes;
2890 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2891 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2893 QTAILQ_HEAD(, elf_thread_status) thread_list;
2894 #if 0
2896 * Current version of ELF coredump doesn't support
2897 * dumping fp regs etc.
2899 elf_fpregset_t *fpu;
2900 elf_fpxregset_t *xfpu;
2901 int thread_status_size;
2902 #endif
2903 int notes_size;
2904 int numnote;
2907 struct vm_area_struct {
2908 target_ulong vma_start; /* start vaddr of memory region */
2909 target_ulong vma_end; /* end vaddr of memory region */
2910 abi_ulong vma_flags; /* protection etc. flags for the region */
2911 QTAILQ_ENTRY(vm_area_struct) vma_link;
2914 struct mm_struct {
2915 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2916 int mm_count; /* number of mappings */
2919 static struct mm_struct *vma_init(void);
2920 static void vma_delete(struct mm_struct *);
2921 static int vma_add_mapping(struct mm_struct *, target_ulong,
2922 target_ulong, abi_ulong);
2923 static int vma_get_mapping_count(const struct mm_struct *);
2924 static struct vm_area_struct *vma_first(const struct mm_struct *);
2925 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2926 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2927 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2928 unsigned long flags);
2930 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2931 static void fill_note(struct memelfnote *, const char *, int,
2932 unsigned int, void *);
2933 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2934 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2935 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2936 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2937 static size_t note_size(const struct memelfnote *);
2938 static void free_note_info(struct elf_note_info *);
2939 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2940 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2941 static int core_dump_filename(const TaskState *, char *, size_t);
2943 static int dump_write(int, const void *, size_t);
2944 static int write_note(struct memelfnote *, int);
2945 static int write_note_info(struct elf_note_info *, int);
2947 #ifdef BSWAP_NEEDED
2948 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2950 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2951 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2952 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2953 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2954 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2955 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2956 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2957 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2958 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2959 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2960 /* cpu times are not filled, so we skip them */
2961 /* regs should be in correct format already */
2962 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2965 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2967 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2968 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2969 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2970 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2971 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2972 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2973 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2976 static void bswap_note(struct elf_note *en)
2978 bswap32s(&en->n_namesz);
2979 bswap32s(&en->n_descsz);
2980 bswap32s(&en->n_type);
2982 #else
2983 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2984 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2985 static inline void bswap_note(struct elf_note *en) { }
2986 #endif /* BSWAP_NEEDED */
2989 * Minimal support for linux memory regions. These are needed
2990 * when we are finding out what memory exactly belongs to
2991 * emulated process. No locks needed here, as long as
2992 * thread that received the signal is stopped.
2995 static struct mm_struct *vma_init(void)
2997 struct mm_struct *mm;
2999 if ((mm = g_malloc(sizeof (*mm))) == NULL)
3000 return (NULL);
3002 mm->mm_count = 0;
3003 QTAILQ_INIT(&mm->mm_mmap);
3005 return (mm);
3008 static void vma_delete(struct mm_struct *mm)
3010 struct vm_area_struct *vma;
3012 while ((vma = vma_first(mm)) != NULL) {
3013 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
3014 g_free(vma);
3016 g_free(mm);
3019 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3020 target_ulong end, abi_ulong flags)
3022 struct vm_area_struct *vma;
3024 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
3025 return (-1);
3027 vma->vma_start = start;
3028 vma->vma_end = end;
3029 vma->vma_flags = flags;
3031 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
3032 mm->mm_count++;
3034 return (0);
3037 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3039 return (QTAILQ_FIRST(&mm->mm_mmap));
3042 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3044 return (QTAILQ_NEXT(vma, vma_link));
3047 static int vma_get_mapping_count(const struct mm_struct *mm)
3049 return (mm->mm_count);
3053 * Calculate file (dump) size of given memory region.
3055 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3057 /* if we cannot even read the first page, skip it */
3058 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
3059 return (0);
3062 * Usually we don't dump executable pages as they contain
3063 * non-writable code that debugger can read directly from
3064 * target library etc. However, thread stacks are marked
3065 * also executable so we read in first page of given region
3066 * and check whether it contains elf header. If there is
3067 * no elf header, we dump it.
3069 if (vma->vma_flags & PROT_EXEC) {
3070 char page[TARGET_PAGE_SIZE];
3072 copy_from_user(page, vma->vma_start, sizeof (page));
3073 if ((page[EI_MAG0] == ELFMAG0) &&
3074 (page[EI_MAG1] == ELFMAG1) &&
3075 (page[EI_MAG2] == ELFMAG2) &&
3076 (page[EI_MAG3] == ELFMAG3)) {
3078 * Mappings are possibly from ELF binary. Don't dump
3079 * them.
3081 return (0);
3085 return (vma->vma_end - vma->vma_start);
3088 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3089 unsigned long flags)
3091 struct mm_struct *mm = (struct mm_struct *)priv;
3093 vma_add_mapping(mm, start, end, flags);
3094 return (0);
3097 static void fill_note(struct memelfnote *note, const char *name, int type,
3098 unsigned int sz, void *data)
3100 unsigned int namesz;
3102 namesz = strlen(name) + 1;
3103 note->name = name;
3104 note->namesz = namesz;
3105 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3106 note->type = type;
3107 note->datasz = sz;
3108 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3110 note->data = data;
3113 * We calculate rounded up note size here as specified by
3114 * ELF document.
3116 note->notesz = sizeof (struct elf_note) +
3117 note->namesz_rounded + note->datasz_rounded;
3120 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
3121 uint32_t flags)
3123 (void) memset(elf, 0, sizeof(*elf));
3125 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
3126 elf->e_ident[EI_CLASS] = ELF_CLASS;
3127 elf->e_ident[EI_DATA] = ELF_DATA;
3128 elf->e_ident[EI_VERSION] = EV_CURRENT;
3129 elf->e_ident[EI_OSABI] = ELF_OSABI;
3131 elf->e_type = ET_CORE;
3132 elf->e_machine = machine;
3133 elf->e_version = EV_CURRENT;
3134 elf->e_phoff = sizeof(struct elfhdr);
3135 elf->e_flags = flags;
3136 elf->e_ehsize = sizeof(struct elfhdr);
3137 elf->e_phentsize = sizeof(struct elf_phdr);
3138 elf->e_phnum = segs;
3140 bswap_ehdr(elf);
3143 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
3145 phdr->p_type = PT_NOTE;
3146 phdr->p_offset = offset;
3147 phdr->p_vaddr = 0;
3148 phdr->p_paddr = 0;
3149 phdr->p_filesz = sz;
3150 phdr->p_memsz = 0;
3151 phdr->p_flags = 0;
3152 phdr->p_align = 0;
3154 bswap_phdr(phdr, 1);
3157 static size_t note_size(const struct memelfnote *note)
3159 return (note->notesz);
3162 static void fill_prstatus(struct target_elf_prstatus *prstatus,
3163 const TaskState *ts, int signr)
3165 (void) memset(prstatus, 0, sizeof (*prstatus));
3166 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
3167 prstatus->pr_pid = ts->ts_tid;
3168 prstatus->pr_ppid = getppid();
3169 prstatus->pr_pgrp = getpgrp();
3170 prstatus->pr_sid = getsid(0);
3172 bswap_prstatus(prstatus);
3175 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
3177 char *base_filename;
3178 unsigned int i, len;
3180 (void) memset(psinfo, 0, sizeof (*psinfo));
3182 len = ts->info->arg_end - ts->info->arg_start;
3183 if (len >= ELF_PRARGSZ)
3184 len = ELF_PRARGSZ - 1;
3185 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
3186 return -EFAULT;
3187 for (i = 0; i < len; i++)
3188 if (psinfo->pr_psargs[i] == 0)
3189 psinfo->pr_psargs[i] = ' ';
3190 psinfo->pr_psargs[len] = 0;
3192 psinfo->pr_pid = getpid();
3193 psinfo->pr_ppid = getppid();
3194 psinfo->pr_pgrp = getpgrp();
3195 psinfo->pr_sid = getsid(0);
3196 psinfo->pr_uid = getuid();
3197 psinfo->pr_gid = getgid();
3199 base_filename = g_path_get_basename(ts->bprm->filename);
3201 * Using strncpy here is fine: at max-length,
3202 * this field is not NUL-terminated.
3204 (void) strncpy(psinfo->pr_fname, base_filename,
3205 sizeof(psinfo->pr_fname));
3207 g_free(base_filename);
3208 bswap_psinfo(psinfo);
3209 return (0);
3212 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
3214 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
3215 elf_addr_t orig_auxv = auxv;
3216 void *ptr;
3217 int len = ts->info->auxv_len;
3220 * Auxiliary vector is stored in target process stack. It contains
3221 * {type, value} pairs that we need to dump into note. This is not
3222 * strictly necessary but we do it here for sake of completeness.
3225 /* read in whole auxv vector and copy it to memelfnote */
3226 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
3227 if (ptr != NULL) {
3228 fill_note(note, "CORE", NT_AUXV, len, ptr);
3229 unlock_user(ptr, auxv, len);
3234 * Constructs name of coredump file. We have following convention
3235 * for the name:
3236 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3238 * Returns 0 in case of success, -1 otherwise (errno is set).
3240 static int core_dump_filename(const TaskState *ts, char *buf,
3241 size_t bufsize)
3243 char timestamp[64];
3244 char *base_filename = NULL;
3245 struct timeval tv;
3246 struct tm tm;
3248 assert(bufsize >= PATH_MAX);
3250 if (gettimeofday(&tv, NULL) < 0) {
3251 (void) fprintf(stderr, "unable to get current timestamp: %s",
3252 strerror(errno));
3253 return (-1);
3256 base_filename = g_path_get_basename(ts->bprm->filename);
3257 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
3258 localtime_r(&tv.tv_sec, &tm));
3259 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
3260 base_filename, timestamp, (int)getpid());
3261 g_free(base_filename);
3263 return (0);
3266 static int dump_write(int fd, const void *ptr, size_t size)
3268 const char *bufp = (const char *)ptr;
3269 ssize_t bytes_written, bytes_left;
3270 struct rlimit dumpsize;
3271 off_t pos;
3273 bytes_written = 0;
3274 getrlimit(RLIMIT_CORE, &dumpsize);
3275 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
3276 if (errno == ESPIPE) { /* not a seekable stream */
3277 bytes_left = size;
3278 } else {
3279 return pos;
3281 } else {
3282 if (dumpsize.rlim_cur <= pos) {
3283 return -1;
3284 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
3285 bytes_left = size;
3286 } else {
3287 size_t limit_left=dumpsize.rlim_cur - pos;
3288 bytes_left = limit_left >= size ? size : limit_left ;
3293 * In normal conditions, single write(2) should do but
3294 * in case of socket etc. this mechanism is more portable.
3296 do {
3297 bytes_written = write(fd, bufp, bytes_left);
3298 if (bytes_written < 0) {
3299 if (errno == EINTR)
3300 continue;
3301 return (-1);
3302 } else if (bytes_written == 0) { /* eof */
3303 return (-1);
3305 bufp += bytes_written;
3306 bytes_left -= bytes_written;
3307 } while (bytes_left > 0);
3309 return (0);
3312 static int write_note(struct memelfnote *men, int fd)
3314 struct elf_note en;
3316 en.n_namesz = men->namesz;
3317 en.n_type = men->type;
3318 en.n_descsz = men->datasz;
3320 bswap_note(&en);
3322 if (dump_write(fd, &en, sizeof(en)) != 0)
3323 return (-1);
3324 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3325 return (-1);
3326 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
3327 return (-1);
3329 return (0);
3332 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
3334 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3335 TaskState *ts = (TaskState *)cpu->opaque;
3336 struct elf_thread_status *ets;
3338 ets = g_malloc0(sizeof (*ets));
3339 ets->num_notes = 1; /* only prstatus is dumped */
3340 fill_prstatus(&ets->prstatus, ts, 0);
3341 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3342 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
3343 &ets->prstatus);
3345 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
3347 info->notes_size += note_size(&ets->notes[0]);
3350 static void init_note_info(struct elf_note_info *info)
3352 /* Initialize the elf_note_info structure so that it is at
3353 * least safe to call free_note_info() on it. Must be
3354 * called before calling fill_note_info().
3356 memset(info, 0, sizeof (*info));
3357 QTAILQ_INIT(&info->thread_list);
3360 static int fill_note_info(struct elf_note_info *info,
3361 long signr, const CPUArchState *env)
3363 #define NUMNOTES 3
3364 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3365 TaskState *ts = (TaskState *)cpu->opaque;
3366 int i;
3368 info->notes = g_new0(struct memelfnote, NUMNOTES);
3369 if (info->notes == NULL)
3370 return (-ENOMEM);
3371 info->prstatus = g_malloc0(sizeof (*info->prstatus));
3372 if (info->prstatus == NULL)
3373 return (-ENOMEM);
3374 info->psinfo = g_malloc0(sizeof (*info->psinfo));
3375 if (info->prstatus == NULL)
3376 return (-ENOMEM);
3379 * First fill in status (and registers) of current thread
3380 * including process info & aux vector.
3382 fill_prstatus(info->prstatus, ts, signr);
3383 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3384 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
3385 sizeof (*info->prstatus), info->prstatus);
3386 fill_psinfo(info->psinfo, ts);
3387 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
3388 sizeof (*info->psinfo), info->psinfo);
3389 fill_auxv_note(&info->notes[2], ts);
3390 info->numnote = 3;
3392 info->notes_size = 0;
3393 for (i = 0; i < info->numnote; i++)
3394 info->notes_size += note_size(&info->notes[i]);
3396 /* read and fill status of all threads */
3397 cpu_list_lock();
3398 CPU_FOREACH(cpu) {
3399 if (cpu == thread_cpu) {
3400 continue;
3402 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
3404 cpu_list_unlock();
3406 return (0);
3409 static void free_note_info(struct elf_note_info *info)
3411 struct elf_thread_status *ets;
3413 while (!QTAILQ_EMPTY(&info->thread_list)) {
3414 ets = QTAILQ_FIRST(&info->thread_list);
3415 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
3416 g_free(ets);
3419 g_free(info->prstatus);
3420 g_free(info->psinfo);
3421 g_free(info->notes);
3424 static int write_note_info(struct elf_note_info *info, int fd)
3426 struct elf_thread_status *ets;
3427 int i, error = 0;
3429 /* write prstatus, psinfo and auxv for current thread */
3430 for (i = 0; i < info->numnote; i++)
3431 if ((error = write_note(&info->notes[i], fd)) != 0)
3432 return (error);
3434 /* write prstatus for each thread */
3435 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
3436 if ((error = write_note(&ets->notes[0], fd)) != 0)
3437 return (error);
3440 return (0);
3444 * Write out ELF coredump.
3446 * See documentation of ELF object file format in:
3447 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3449 * Coredump format in linux is following:
3451 * 0 +----------------------+ \
3452 * | ELF header | ET_CORE |
3453 * +----------------------+ |
3454 * | ELF program headers | |--- headers
3455 * | - NOTE section | |
3456 * | - PT_LOAD sections | |
3457 * +----------------------+ /
3458 * | NOTEs: |
3459 * | - NT_PRSTATUS |
3460 * | - NT_PRSINFO |
3461 * | - NT_AUXV |
3462 * +----------------------+ <-- aligned to target page
3463 * | Process memory dump |
3464 * : :
3465 * . .
3466 * : :
3467 * | |
3468 * +----------------------+
3470 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3471 * NT_PRSINFO -> struct elf_prpsinfo
3472 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3474 * Format follows System V format as close as possible. Current
3475 * version limitations are as follows:
3476 * - no floating point registers are dumped
3478 * Function returns 0 in case of success, negative errno otherwise.
3480 * TODO: make this work also during runtime: it should be
3481 * possible to force coredump from running process and then
3482 * continue processing. For example qemu could set up SIGUSR2
3483 * handler (provided that target process haven't registered
3484 * handler for that) that does the dump when signal is received.
3486 static int elf_core_dump(int signr, const CPUArchState *env)
3488 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3489 const TaskState *ts = (const TaskState *)cpu->opaque;
3490 struct vm_area_struct *vma = NULL;
3491 char corefile[PATH_MAX];
3492 struct elf_note_info info;
3493 struct elfhdr elf;
3494 struct elf_phdr phdr;
3495 struct rlimit dumpsize;
3496 struct mm_struct *mm = NULL;
3497 off_t offset = 0, data_offset = 0;
3498 int segs = 0;
3499 int fd = -1;
3501 init_note_info(&info);
3503 errno = 0;
3504 getrlimit(RLIMIT_CORE, &dumpsize);
3505 if (dumpsize.rlim_cur == 0)
3506 return 0;
3508 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
3509 return (-errno);
3511 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3512 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3513 return (-errno);
3516 * Walk through target process memory mappings and
3517 * set up structure containing this information. After
3518 * this point vma_xxx functions can be used.
3520 if ((mm = vma_init()) == NULL)
3521 goto out;
3523 walk_memory_regions(mm, vma_walker);
3524 segs = vma_get_mapping_count(mm);
3527 * Construct valid coredump ELF header. We also
3528 * add one more segment for notes.
3530 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3531 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3532 goto out;
3534 /* fill in the in-memory version of notes */
3535 if (fill_note_info(&info, signr, env) < 0)
3536 goto out;
3538 offset += sizeof (elf); /* elf header */
3539 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3541 /* write out notes program header */
3542 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3544 offset += info.notes_size;
3545 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3546 goto out;
3549 * ELF specification wants data to start at page boundary so
3550 * we align it here.
3552 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3555 * Write program headers for memory regions mapped in
3556 * the target process.
3558 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3559 (void) memset(&phdr, 0, sizeof (phdr));
3561 phdr.p_type = PT_LOAD;
3562 phdr.p_offset = offset;
3563 phdr.p_vaddr = vma->vma_start;
3564 phdr.p_paddr = 0;
3565 phdr.p_filesz = vma_dump_size(vma);
3566 offset += phdr.p_filesz;
3567 phdr.p_memsz = vma->vma_end - vma->vma_start;
3568 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3569 if (vma->vma_flags & PROT_WRITE)
3570 phdr.p_flags |= PF_W;
3571 if (vma->vma_flags & PROT_EXEC)
3572 phdr.p_flags |= PF_X;
3573 phdr.p_align = ELF_EXEC_PAGESIZE;
3575 bswap_phdr(&phdr, 1);
3576 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
3577 goto out;
3582 * Next we write notes just after program headers. No
3583 * alignment needed here.
3585 if (write_note_info(&info, fd) < 0)
3586 goto out;
3588 /* align data to page boundary */
3589 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3590 goto out;
3593 * Finally we can dump process memory into corefile as well.
3595 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3596 abi_ulong addr;
3597 abi_ulong end;
3599 end = vma->vma_start + vma_dump_size(vma);
3601 for (addr = vma->vma_start; addr < end;
3602 addr += TARGET_PAGE_SIZE) {
3603 char page[TARGET_PAGE_SIZE];
3604 int error;
3607 * Read in page from target process memory and
3608 * write it to coredump file.
3610 error = copy_from_user(page, addr, sizeof (page));
3611 if (error != 0) {
3612 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3613 addr);
3614 errno = -error;
3615 goto out;
3617 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3618 goto out;
3622 out:
3623 free_note_info(&info);
3624 if (mm != NULL)
3625 vma_delete(mm);
3626 (void) close(fd);
3628 if (errno != 0)
3629 return (-errno);
3630 return (0);
3632 #endif /* USE_ELF_CORE_DUMP */
3634 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3636 init_thread(regs, infop);