hw/misc/iotkit-secctl: Arm IoT Kit security controller initial skeleton
[qemu/ar7.git] / linux-user / elfload.c
blob6689089cd2af77dd5c0587ffd74d369443d9038c
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 /* this flag is uneffective under linux too, should be deleted */
82 #ifndef MAP_DENYWRITE
83 #define MAP_DENYWRITE 0
84 #endif
86 /* should probably go in elf.h */
87 #ifndef ELIBBAD
88 #define ELIBBAD 80
89 #endif
91 #ifdef TARGET_WORDS_BIGENDIAN
92 #define ELF_DATA ELFDATA2MSB
93 #else
94 #define ELF_DATA ELFDATA2LSB
95 #endif
97 #ifdef TARGET_ABI_MIPSN32
98 typedef abi_ullong target_elf_greg_t;
99 #define tswapreg(ptr) tswap64(ptr)
100 #else
101 typedef abi_ulong target_elf_greg_t;
102 #define tswapreg(ptr) tswapal(ptr)
103 #endif
105 #ifdef USE_UID16
106 typedef abi_ushort target_uid_t;
107 typedef abi_ushort target_gid_t;
108 #else
109 typedef abi_uint target_uid_t;
110 typedef abi_uint target_gid_t;
111 #endif
112 typedef abi_int target_pid_t;
114 #ifdef TARGET_I386
116 #define ELF_PLATFORM get_elf_platform()
118 static const char *get_elf_platform(void)
120 static char elf_platform[] = "i386";
121 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
122 if (family > 6)
123 family = 6;
124 if (family >= 3)
125 elf_platform[1] = '0' + family;
126 return elf_platform;
129 #define ELF_HWCAP get_elf_hwcap()
131 static uint32_t get_elf_hwcap(void)
133 X86CPU *cpu = X86_CPU(thread_cpu);
135 return cpu->env.features[FEAT_1_EDX];
138 #ifdef TARGET_X86_64
139 #define ELF_START_MMAP 0x2aaaaab000ULL
141 #define ELF_CLASS ELFCLASS64
142 #define ELF_ARCH EM_X86_64
144 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
146 regs->rax = 0;
147 regs->rsp = infop->start_stack;
148 regs->rip = infop->entry;
151 #define ELF_NREG 27
152 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
155 * Note that ELF_NREG should be 29 as there should be place for
156 * TRAPNO and ERR "registers" as well but linux doesn't dump
157 * those.
159 * See linux kernel: arch/x86/include/asm/elf.h
161 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
163 (*regs)[0] = env->regs[15];
164 (*regs)[1] = env->regs[14];
165 (*regs)[2] = env->regs[13];
166 (*regs)[3] = env->regs[12];
167 (*regs)[4] = env->regs[R_EBP];
168 (*regs)[5] = env->regs[R_EBX];
169 (*regs)[6] = env->regs[11];
170 (*regs)[7] = env->regs[10];
171 (*regs)[8] = env->regs[9];
172 (*regs)[9] = env->regs[8];
173 (*regs)[10] = env->regs[R_EAX];
174 (*regs)[11] = env->regs[R_ECX];
175 (*regs)[12] = env->regs[R_EDX];
176 (*regs)[13] = env->regs[R_ESI];
177 (*regs)[14] = env->regs[R_EDI];
178 (*regs)[15] = env->regs[R_EAX]; /* XXX */
179 (*regs)[16] = env->eip;
180 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
181 (*regs)[18] = env->eflags;
182 (*regs)[19] = env->regs[R_ESP];
183 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
184 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
185 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
186 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
187 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
188 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
189 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
192 #else
194 #define ELF_START_MMAP 0x80000000
197 * This is used to ensure we don't load something for the wrong architecture.
199 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
202 * These are used to set parameters in the core dumps.
204 #define ELF_CLASS ELFCLASS32
205 #define ELF_ARCH EM_386
207 static inline void init_thread(struct target_pt_regs *regs,
208 struct image_info *infop)
210 regs->esp = infop->start_stack;
211 regs->eip = infop->entry;
213 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
214 starts %edx contains a pointer to a function which might be
215 registered using `atexit'. This provides a mean for the
216 dynamic linker to call DT_FINI functions for shared libraries
217 that have been loaded before the code runs.
219 A value of 0 tells we have no such handler. */
220 regs->edx = 0;
223 #define ELF_NREG 17
224 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
227 * Note that ELF_NREG should be 19 as there should be place for
228 * TRAPNO and ERR "registers" as well but linux doesn't dump
229 * those.
231 * See linux kernel: arch/x86/include/asm/elf.h
233 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
235 (*regs)[0] = env->regs[R_EBX];
236 (*regs)[1] = env->regs[R_ECX];
237 (*regs)[2] = env->regs[R_EDX];
238 (*regs)[3] = env->regs[R_ESI];
239 (*regs)[4] = env->regs[R_EDI];
240 (*regs)[5] = env->regs[R_EBP];
241 (*regs)[6] = env->regs[R_EAX];
242 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
243 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
244 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
245 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
246 (*regs)[11] = env->regs[R_EAX]; /* XXX */
247 (*regs)[12] = env->eip;
248 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
249 (*regs)[14] = env->eflags;
250 (*regs)[15] = env->regs[R_ESP];
251 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
253 #endif
255 #define USE_ELF_CORE_DUMP
256 #define ELF_EXEC_PAGESIZE 4096
258 #endif
260 #ifdef TARGET_ARM
262 #ifndef TARGET_AARCH64
263 /* 32 bit ARM definitions */
265 #define ELF_START_MMAP 0x80000000
267 #define ELF_ARCH EM_ARM
268 #define ELF_CLASS ELFCLASS32
270 static inline void init_thread(struct target_pt_regs *regs,
271 struct image_info *infop)
273 abi_long stack = infop->start_stack;
274 memset(regs, 0, sizeof(*regs));
276 regs->uregs[16] = ARM_CPU_MODE_USR;
277 if (infop->entry & 1) {
278 regs->uregs[16] |= CPSR_T;
280 regs->uregs[15] = infop->entry & 0xfffffffe;
281 regs->uregs[13] = infop->start_stack;
282 /* FIXME - what to for failure of get_user()? */
283 get_user_ual(regs->uregs[2], stack + 8); /* envp */
284 get_user_ual(regs->uregs[1], stack + 4); /* envp */
285 /* XXX: it seems that r0 is zeroed after ! */
286 regs->uregs[0] = 0;
287 /* For uClinux PIC binaries. */
288 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
289 regs->uregs[10] = infop->start_data;
292 #define ELF_NREG 18
293 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
295 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
297 (*regs)[0] = tswapreg(env->regs[0]);
298 (*regs)[1] = tswapreg(env->regs[1]);
299 (*regs)[2] = tswapreg(env->regs[2]);
300 (*regs)[3] = tswapreg(env->regs[3]);
301 (*regs)[4] = tswapreg(env->regs[4]);
302 (*regs)[5] = tswapreg(env->regs[5]);
303 (*regs)[6] = tswapreg(env->regs[6]);
304 (*regs)[7] = tswapreg(env->regs[7]);
305 (*regs)[8] = tswapreg(env->regs[8]);
306 (*regs)[9] = tswapreg(env->regs[9]);
307 (*regs)[10] = tswapreg(env->regs[10]);
308 (*regs)[11] = tswapreg(env->regs[11]);
309 (*regs)[12] = tswapreg(env->regs[12]);
310 (*regs)[13] = tswapreg(env->regs[13]);
311 (*regs)[14] = tswapreg(env->regs[14]);
312 (*regs)[15] = tswapreg(env->regs[15]);
314 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
315 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
318 #define USE_ELF_CORE_DUMP
319 #define ELF_EXEC_PAGESIZE 4096
321 enum
323 ARM_HWCAP_ARM_SWP = 1 << 0,
324 ARM_HWCAP_ARM_HALF = 1 << 1,
325 ARM_HWCAP_ARM_THUMB = 1 << 2,
326 ARM_HWCAP_ARM_26BIT = 1 << 3,
327 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
328 ARM_HWCAP_ARM_FPA = 1 << 5,
329 ARM_HWCAP_ARM_VFP = 1 << 6,
330 ARM_HWCAP_ARM_EDSP = 1 << 7,
331 ARM_HWCAP_ARM_JAVA = 1 << 8,
332 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
333 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
334 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
335 ARM_HWCAP_ARM_NEON = 1 << 12,
336 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
337 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
338 ARM_HWCAP_ARM_TLS = 1 << 15,
339 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
340 ARM_HWCAP_ARM_IDIVA = 1 << 17,
341 ARM_HWCAP_ARM_IDIVT = 1 << 18,
342 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
343 ARM_HWCAP_ARM_LPAE = 1 << 20,
344 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
347 enum {
348 ARM_HWCAP2_ARM_AES = 1 << 0,
349 ARM_HWCAP2_ARM_PMULL = 1 << 1,
350 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
351 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
352 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
355 /* The commpage only exists for 32 bit kernels */
357 #define TARGET_HAS_VALIDATE_GUEST_SPACE
358 /* Return 1 if the proposed guest space is suitable for the guest.
359 * Return 0 if the proposed guest space isn't suitable, but another
360 * address space should be tried.
361 * Return -1 if there is no way the proposed guest space can be
362 * valid regardless of the base.
363 * The guest code may leave a page mapped and populate it if the
364 * address is suitable.
366 static int validate_guest_space(unsigned long guest_base,
367 unsigned long guest_size)
369 unsigned long real_start, test_page_addr;
371 /* We need to check that we can force a fault on access to the
372 * commpage at 0xffff0fxx
374 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
376 /* If the commpage lies within the already allocated guest space,
377 * then there is no way we can allocate it.
379 if (test_page_addr >= guest_base
380 && test_page_addr < (guest_base + guest_size)) {
381 return -1;
384 /* Note it needs to be writeable to let us initialise it */
385 real_start = (unsigned long)
386 mmap((void *)test_page_addr, qemu_host_page_size,
387 PROT_READ | PROT_WRITE,
388 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
390 /* If we can't map it then try another address */
391 if (real_start == -1ul) {
392 return 0;
395 if (real_start != test_page_addr) {
396 /* OS didn't put the page where we asked - unmap and reject */
397 munmap((void *)real_start, qemu_host_page_size);
398 return 0;
401 /* Leave the page mapped
402 * Populate it (mmap should have left it all 0'd)
405 /* Kernel helper versions */
406 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
408 /* Now it's populated make it RO */
409 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
410 perror("Protecting guest commpage");
411 exit(-1);
414 return 1; /* All good */
417 #define ELF_HWCAP get_elf_hwcap()
418 #define ELF_HWCAP2 get_elf_hwcap2()
420 static uint32_t get_elf_hwcap(void)
422 ARMCPU *cpu = ARM_CPU(thread_cpu);
423 uint32_t hwcaps = 0;
425 hwcaps |= ARM_HWCAP_ARM_SWP;
426 hwcaps |= ARM_HWCAP_ARM_HALF;
427 hwcaps |= ARM_HWCAP_ARM_THUMB;
428 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
430 /* probe for the extra features */
431 #define GET_FEATURE(feat, hwcap) \
432 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
433 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
434 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
435 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
436 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
437 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
438 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
439 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
440 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
441 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
442 GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA);
443 GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT);
444 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
445 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
446 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
447 * to our VFP_FP16 feature bit.
449 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
450 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
452 return hwcaps;
455 static uint32_t get_elf_hwcap2(void)
457 ARMCPU *cpu = ARM_CPU(thread_cpu);
458 uint32_t hwcaps = 0;
460 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP2_ARM_AES);
461 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP2_ARM_PMULL);
462 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP2_ARM_SHA1);
463 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP2_ARM_SHA2);
464 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP2_ARM_CRC32);
465 return hwcaps;
468 #undef GET_FEATURE
470 #else
471 /* 64 bit ARM definitions */
472 #define ELF_START_MMAP 0x80000000
474 #define ELF_ARCH EM_AARCH64
475 #define ELF_CLASS ELFCLASS64
476 #define ELF_PLATFORM "aarch64"
478 static inline void init_thread(struct target_pt_regs *regs,
479 struct image_info *infop)
481 abi_long stack = infop->start_stack;
482 memset(regs, 0, sizeof(*regs));
484 regs->pc = infop->entry & ~0x3ULL;
485 regs->sp = stack;
488 #define ELF_NREG 34
489 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
491 static void elf_core_copy_regs(target_elf_gregset_t *regs,
492 const CPUARMState *env)
494 int i;
496 for (i = 0; i < 32; i++) {
497 (*regs)[i] = tswapreg(env->xregs[i]);
499 (*regs)[32] = tswapreg(env->pc);
500 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
503 #define USE_ELF_CORE_DUMP
504 #define ELF_EXEC_PAGESIZE 4096
506 enum {
507 ARM_HWCAP_A64_FP = 1 << 0,
508 ARM_HWCAP_A64_ASIMD = 1 << 1,
509 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
510 ARM_HWCAP_A64_AES = 1 << 3,
511 ARM_HWCAP_A64_PMULL = 1 << 4,
512 ARM_HWCAP_A64_SHA1 = 1 << 5,
513 ARM_HWCAP_A64_SHA2 = 1 << 6,
514 ARM_HWCAP_A64_CRC32 = 1 << 7,
515 ARM_HWCAP_A64_ATOMICS = 1 << 8,
516 ARM_HWCAP_A64_FPHP = 1 << 9,
517 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
518 ARM_HWCAP_A64_CPUID = 1 << 11,
519 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
520 ARM_HWCAP_A64_JSCVT = 1 << 13,
521 ARM_HWCAP_A64_FCMA = 1 << 14,
522 ARM_HWCAP_A64_LRCPC = 1 << 15,
523 ARM_HWCAP_A64_DCPOP = 1 << 16,
524 ARM_HWCAP_A64_SHA3 = 1 << 17,
525 ARM_HWCAP_A64_SM3 = 1 << 18,
526 ARM_HWCAP_A64_SM4 = 1 << 19,
527 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
528 ARM_HWCAP_A64_SHA512 = 1 << 21,
529 ARM_HWCAP_A64_SVE = 1 << 22,
532 #define ELF_HWCAP get_elf_hwcap()
534 static uint32_t get_elf_hwcap(void)
536 ARMCPU *cpu = ARM_CPU(thread_cpu);
537 uint32_t hwcaps = 0;
539 hwcaps |= ARM_HWCAP_A64_FP;
540 hwcaps |= ARM_HWCAP_A64_ASIMD;
542 /* probe for the extra features */
543 #define GET_FEATURE(feat, hwcap) \
544 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
545 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP_A64_AES);
546 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP_A64_PMULL);
547 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP_A64_SHA1);
548 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP_A64_SHA2);
549 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP_A64_CRC32);
550 GET_FEATURE(ARM_FEATURE_V8_SHA3, ARM_HWCAP_A64_SHA3);
551 GET_FEATURE(ARM_FEATURE_V8_SM3, ARM_HWCAP_A64_SM3);
552 GET_FEATURE(ARM_FEATURE_V8_SM4, ARM_HWCAP_A64_SM4);
553 GET_FEATURE(ARM_FEATURE_V8_SHA512, ARM_HWCAP_A64_SHA512);
554 GET_FEATURE(ARM_FEATURE_V8_FP16,
555 ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
556 #undef GET_FEATURE
558 return hwcaps;
561 #endif /* not TARGET_AARCH64 */
562 #endif /* TARGET_ARM */
564 #ifdef TARGET_UNICORE32
566 #define ELF_START_MMAP 0x80000000
568 #define ELF_CLASS ELFCLASS32
569 #define ELF_DATA ELFDATA2LSB
570 #define ELF_ARCH EM_UNICORE32
572 static inline void init_thread(struct target_pt_regs *regs,
573 struct image_info *infop)
575 abi_long stack = infop->start_stack;
576 memset(regs, 0, sizeof(*regs));
577 regs->UC32_REG_asr = 0x10;
578 regs->UC32_REG_pc = infop->entry & 0xfffffffe;
579 regs->UC32_REG_sp = infop->start_stack;
580 /* FIXME - what to for failure of get_user()? */
581 get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */
582 get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */
583 /* XXX: it seems that r0 is zeroed after ! */
584 regs->UC32_REG_00 = 0;
587 #define ELF_NREG 34
588 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
590 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
592 (*regs)[0] = env->regs[0];
593 (*regs)[1] = env->regs[1];
594 (*regs)[2] = env->regs[2];
595 (*regs)[3] = env->regs[3];
596 (*regs)[4] = env->regs[4];
597 (*regs)[5] = env->regs[5];
598 (*regs)[6] = env->regs[6];
599 (*regs)[7] = env->regs[7];
600 (*regs)[8] = env->regs[8];
601 (*regs)[9] = env->regs[9];
602 (*regs)[10] = env->regs[10];
603 (*regs)[11] = env->regs[11];
604 (*regs)[12] = env->regs[12];
605 (*regs)[13] = env->regs[13];
606 (*regs)[14] = env->regs[14];
607 (*regs)[15] = env->regs[15];
608 (*regs)[16] = env->regs[16];
609 (*regs)[17] = env->regs[17];
610 (*regs)[18] = env->regs[18];
611 (*regs)[19] = env->regs[19];
612 (*regs)[20] = env->regs[20];
613 (*regs)[21] = env->regs[21];
614 (*regs)[22] = env->regs[22];
615 (*regs)[23] = env->regs[23];
616 (*regs)[24] = env->regs[24];
617 (*regs)[25] = env->regs[25];
618 (*regs)[26] = env->regs[26];
619 (*regs)[27] = env->regs[27];
620 (*regs)[28] = env->regs[28];
621 (*regs)[29] = env->regs[29];
622 (*regs)[30] = env->regs[30];
623 (*regs)[31] = env->regs[31];
625 (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
626 (*regs)[33] = env->regs[0]; /* XXX */
629 #define USE_ELF_CORE_DUMP
630 #define ELF_EXEC_PAGESIZE 4096
632 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
634 #endif
636 #ifdef TARGET_SPARC
637 #ifdef TARGET_SPARC64
639 #define ELF_START_MMAP 0x80000000
640 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
641 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
642 #ifndef TARGET_ABI32
643 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
644 #else
645 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
646 #endif
648 #define ELF_CLASS ELFCLASS64
649 #define ELF_ARCH EM_SPARCV9
651 #define STACK_BIAS 2047
653 static inline void init_thread(struct target_pt_regs *regs,
654 struct image_info *infop)
656 #ifndef TARGET_ABI32
657 regs->tstate = 0;
658 #endif
659 regs->pc = infop->entry;
660 regs->npc = regs->pc + 4;
661 regs->y = 0;
662 #ifdef TARGET_ABI32
663 regs->u_regs[14] = infop->start_stack - 16 * 4;
664 #else
665 if (personality(infop->personality) == PER_LINUX32)
666 regs->u_regs[14] = infop->start_stack - 16 * 4;
667 else
668 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
669 #endif
672 #else
673 #define ELF_START_MMAP 0x80000000
674 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
675 | HWCAP_SPARC_MULDIV)
677 #define ELF_CLASS ELFCLASS32
678 #define ELF_ARCH EM_SPARC
680 static inline void init_thread(struct target_pt_regs *regs,
681 struct image_info *infop)
683 regs->psr = 0;
684 regs->pc = infop->entry;
685 regs->npc = regs->pc + 4;
686 regs->y = 0;
687 regs->u_regs[14] = infop->start_stack - 16 * 4;
690 #endif
691 #endif
693 #ifdef TARGET_PPC
695 #define ELF_MACHINE PPC_ELF_MACHINE
696 #define ELF_START_MMAP 0x80000000
698 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
700 #define elf_check_arch(x) ( (x) == EM_PPC64 )
702 #define ELF_CLASS ELFCLASS64
704 #else
706 #define ELF_CLASS ELFCLASS32
708 #endif
710 #define ELF_ARCH EM_PPC
712 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
713 See arch/powerpc/include/asm/cputable.h. */
714 enum {
715 QEMU_PPC_FEATURE_32 = 0x80000000,
716 QEMU_PPC_FEATURE_64 = 0x40000000,
717 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
718 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
719 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
720 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
721 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
722 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
723 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
724 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
725 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
726 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
727 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
728 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
729 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
730 QEMU_PPC_FEATURE_CELL = 0x00010000,
731 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
732 QEMU_PPC_FEATURE_SMT = 0x00004000,
733 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
734 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
735 QEMU_PPC_FEATURE_PA6T = 0x00000800,
736 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
737 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
738 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
739 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
740 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
742 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
743 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
745 /* Feature definitions in AT_HWCAP2. */
746 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
747 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
748 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
749 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
750 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
751 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
754 #define ELF_HWCAP get_elf_hwcap()
756 static uint32_t get_elf_hwcap(void)
758 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
759 uint32_t features = 0;
761 /* We don't have to be terribly complete here; the high points are
762 Altivec/FP/SPE support. Anything else is just a bonus. */
763 #define GET_FEATURE(flag, feature) \
764 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
765 #define GET_FEATURE2(flags, feature) \
766 do { \
767 if ((cpu->env.insns_flags2 & flags) == flags) { \
768 features |= feature; \
770 } while (0)
771 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
772 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
773 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
774 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
775 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
776 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
777 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
778 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
779 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
780 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
781 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
782 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
783 QEMU_PPC_FEATURE_ARCH_2_06);
784 #undef GET_FEATURE
785 #undef GET_FEATURE2
787 return features;
790 #define ELF_HWCAP2 get_elf_hwcap2()
792 static uint32_t get_elf_hwcap2(void)
794 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
795 uint32_t features = 0;
797 #define GET_FEATURE(flag, feature) \
798 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
799 #define GET_FEATURE2(flag, feature) \
800 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
802 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
803 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
804 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
805 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07);
807 #undef GET_FEATURE
808 #undef GET_FEATURE2
810 return features;
814 * The requirements here are:
815 * - keep the final alignment of sp (sp & 0xf)
816 * - make sure the 32-bit value at the first 16 byte aligned position of
817 * AUXV is greater than 16 for glibc compatibility.
818 * AT_IGNOREPPC is used for that.
819 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
820 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
822 #define DLINFO_ARCH_ITEMS 5
823 #define ARCH_DLINFO \
824 do { \
825 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
826 /* \
827 * Handle glibc compatibility: these magic entries must \
828 * be at the lowest addresses in the final auxv. \
829 */ \
830 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
831 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
832 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
833 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
834 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
835 } while (0)
837 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
839 _regs->gpr[1] = infop->start_stack;
840 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
841 if (get_ppc64_abi(infop) < 2) {
842 uint64_t val;
843 get_user_u64(val, infop->entry + 8);
844 _regs->gpr[2] = val + infop->load_bias;
845 get_user_u64(val, infop->entry);
846 infop->entry = val + infop->load_bias;
847 } else {
848 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
850 #endif
851 _regs->nip = infop->entry;
854 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
855 #define ELF_NREG 48
856 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
858 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
860 int i;
861 target_ulong ccr = 0;
863 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
864 (*regs)[i] = tswapreg(env->gpr[i]);
867 (*regs)[32] = tswapreg(env->nip);
868 (*regs)[33] = tswapreg(env->msr);
869 (*regs)[35] = tswapreg(env->ctr);
870 (*regs)[36] = tswapreg(env->lr);
871 (*regs)[37] = tswapreg(env->xer);
873 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
874 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
876 (*regs)[38] = tswapreg(ccr);
879 #define USE_ELF_CORE_DUMP
880 #define ELF_EXEC_PAGESIZE 4096
882 #endif
884 #ifdef TARGET_MIPS
886 #define ELF_START_MMAP 0x80000000
888 #ifdef TARGET_MIPS64
889 #define ELF_CLASS ELFCLASS64
890 #else
891 #define ELF_CLASS ELFCLASS32
892 #endif
893 #define ELF_ARCH EM_MIPS
895 static inline void init_thread(struct target_pt_regs *regs,
896 struct image_info *infop)
898 regs->cp0_status = 2 << CP0St_KSU;
899 regs->cp0_epc = infop->entry;
900 regs->regs[29] = infop->start_stack;
903 /* See linux kernel: arch/mips/include/asm/elf.h. */
904 #define ELF_NREG 45
905 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
907 /* See linux kernel: arch/mips/include/asm/reg.h. */
908 enum {
909 #ifdef TARGET_MIPS64
910 TARGET_EF_R0 = 0,
911 #else
912 TARGET_EF_R0 = 6,
913 #endif
914 TARGET_EF_R26 = TARGET_EF_R0 + 26,
915 TARGET_EF_R27 = TARGET_EF_R0 + 27,
916 TARGET_EF_LO = TARGET_EF_R0 + 32,
917 TARGET_EF_HI = TARGET_EF_R0 + 33,
918 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
919 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
920 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
921 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
924 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
925 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
927 int i;
929 for (i = 0; i < TARGET_EF_R0; i++) {
930 (*regs)[i] = 0;
932 (*regs)[TARGET_EF_R0] = 0;
934 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
935 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
938 (*regs)[TARGET_EF_R26] = 0;
939 (*regs)[TARGET_EF_R27] = 0;
940 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
941 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
942 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
943 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
944 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
945 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
948 #define USE_ELF_CORE_DUMP
949 #define ELF_EXEC_PAGESIZE 4096
951 #endif /* TARGET_MIPS */
953 #ifdef TARGET_MICROBLAZE
955 #define ELF_START_MMAP 0x80000000
957 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
959 #define ELF_CLASS ELFCLASS32
960 #define ELF_ARCH EM_MICROBLAZE
962 static inline void init_thread(struct target_pt_regs *regs,
963 struct image_info *infop)
965 regs->pc = infop->entry;
966 regs->r1 = infop->start_stack;
970 #define ELF_EXEC_PAGESIZE 4096
972 #define USE_ELF_CORE_DUMP
973 #define ELF_NREG 38
974 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
976 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
977 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
979 int i, pos = 0;
981 for (i = 0; i < 32; i++) {
982 (*regs)[pos++] = tswapreg(env->regs[i]);
985 for (i = 0; i < 6; i++) {
986 (*regs)[pos++] = tswapreg(env->sregs[i]);
990 #endif /* TARGET_MICROBLAZE */
992 #ifdef TARGET_NIOS2
994 #define ELF_START_MMAP 0x80000000
996 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
998 #define ELF_CLASS ELFCLASS32
999 #define ELF_ARCH EM_ALTERA_NIOS2
1001 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1003 regs->ea = infop->entry;
1004 regs->sp = infop->start_stack;
1005 regs->estatus = 0x3;
1008 #define ELF_EXEC_PAGESIZE 4096
1010 #define USE_ELF_CORE_DUMP
1011 #define ELF_NREG 49
1012 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1014 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1015 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1016 const CPUNios2State *env)
1018 int i;
1020 (*regs)[0] = -1;
1021 for (i = 1; i < 8; i++) /* r0-r7 */
1022 (*regs)[i] = tswapreg(env->regs[i + 7]);
1024 for (i = 8; i < 16; i++) /* r8-r15 */
1025 (*regs)[i] = tswapreg(env->regs[i - 8]);
1027 for (i = 16; i < 24; i++) /* r16-r23 */
1028 (*regs)[i] = tswapreg(env->regs[i + 7]);
1029 (*regs)[24] = -1; /* R_ET */
1030 (*regs)[25] = -1; /* R_BT */
1031 (*regs)[26] = tswapreg(env->regs[R_GP]);
1032 (*regs)[27] = tswapreg(env->regs[R_SP]);
1033 (*regs)[28] = tswapreg(env->regs[R_FP]);
1034 (*regs)[29] = tswapreg(env->regs[R_EA]);
1035 (*regs)[30] = -1; /* R_SSTATUS */
1036 (*regs)[31] = tswapreg(env->regs[R_RA]);
1038 (*regs)[32] = tswapreg(env->regs[R_PC]);
1040 (*regs)[33] = -1; /* R_STATUS */
1041 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1043 for (i = 35; i < 49; i++) /* ... */
1044 (*regs)[i] = -1;
1047 #endif /* TARGET_NIOS2 */
1049 #ifdef TARGET_OPENRISC
1051 #define ELF_START_MMAP 0x08000000
1053 #define ELF_ARCH EM_OPENRISC
1054 #define ELF_CLASS ELFCLASS32
1055 #define ELF_DATA ELFDATA2MSB
1057 static inline void init_thread(struct target_pt_regs *regs,
1058 struct image_info *infop)
1060 regs->pc = infop->entry;
1061 regs->gpr[1] = infop->start_stack;
1064 #define USE_ELF_CORE_DUMP
1065 #define ELF_EXEC_PAGESIZE 8192
1067 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1068 #define ELF_NREG 34 /* gprs and pc, sr */
1069 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1071 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1072 const CPUOpenRISCState *env)
1074 int i;
1076 for (i = 0; i < 32; i++) {
1077 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1079 (*regs)[32] = tswapreg(env->pc);
1080 (*regs)[33] = tswapreg(cpu_get_sr(env));
1082 #define ELF_HWCAP 0
1083 #define ELF_PLATFORM NULL
1085 #endif /* TARGET_OPENRISC */
1087 #ifdef TARGET_SH4
1089 #define ELF_START_MMAP 0x80000000
1091 #define ELF_CLASS ELFCLASS32
1092 #define ELF_ARCH EM_SH
1094 static inline void init_thread(struct target_pt_regs *regs,
1095 struct image_info *infop)
1097 /* Check other registers XXXXX */
1098 regs->pc = infop->entry;
1099 regs->regs[15] = infop->start_stack;
1102 /* See linux kernel: arch/sh/include/asm/elf.h. */
1103 #define ELF_NREG 23
1104 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1106 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1107 enum {
1108 TARGET_REG_PC = 16,
1109 TARGET_REG_PR = 17,
1110 TARGET_REG_SR = 18,
1111 TARGET_REG_GBR = 19,
1112 TARGET_REG_MACH = 20,
1113 TARGET_REG_MACL = 21,
1114 TARGET_REG_SYSCALL = 22
1117 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1118 const CPUSH4State *env)
1120 int i;
1122 for (i = 0; i < 16; i++) {
1123 (*regs)[i] = tswapreg(env->gregs[i]);
1126 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1127 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1128 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1129 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1130 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1131 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1132 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1135 #define USE_ELF_CORE_DUMP
1136 #define ELF_EXEC_PAGESIZE 4096
1138 enum {
1139 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1140 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1141 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1142 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1143 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1144 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1145 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1146 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1147 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1148 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1151 #define ELF_HWCAP get_elf_hwcap()
1153 static uint32_t get_elf_hwcap(void)
1155 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1156 uint32_t hwcap = 0;
1158 hwcap |= SH_CPU_HAS_FPU;
1160 if (cpu->env.features & SH_FEATURE_SH4A) {
1161 hwcap |= SH_CPU_HAS_LLSC;
1164 return hwcap;
1167 #endif
1169 #ifdef TARGET_CRIS
1171 #define ELF_START_MMAP 0x80000000
1173 #define ELF_CLASS ELFCLASS32
1174 #define ELF_ARCH EM_CRIS
1176 static inline void init_thread(struct target_pt_regs *regs,
1177 struct image_info *infop)
1179 regs->erp = infop->entry;
1182 #define ELF_EXEC_PAGESIZE 8192
1184 #endif
1186 #ifdef TARGET_M68K
1188 #define ELF_START_MMAP 0x80000000
1190 #define ELF_CLASS ELFCLASS32
1191 #define ELF_ARCH EM_68K
1193 /* ??? Does this need to do anything?
1194 #define ELF_PLAT_INIT(_r) */
1196 static inline void init_thread(struct target_pt_regs *regs,
1197 struct image_info *infop)
1199 regs->usp = infop->start_stack;
1200 regs->sr = 0;
1201 regs->pc = infop->entry;
1204 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1205 #define ELF_NREG 20
1206 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1208 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1210 (*regs)[0] = tswapreg(env->dregs[1]);
1211 (*regs)[1] = tswapreg(env->dregs[2]);
1212 (*regs)[2] = tswapreg(env->dregs[3]);
1213 (*regs)[3] = tswapreg(env->dregs[4]);
1214 (*regs)[4] = tswapreg(env->dregs[5]);
1215 (*regs)[5] = tswapreg(env->dregs[6]);
1216 (*regs)[6] = tswapreg(env->dregs[7]);
1217 (*regs)[7] = tswapreg(env->aregs[0]);
1218 (*regs)[8] = tswapreg(env->aregs[1]);
1219 (*regs)[9] = tswapreg(env->aregs[2]);
1220 (*regs)[10] = tswapreg(env->aregs[3]);
1221 (*regs)[11] = tswapreg(env->aregs[4]);
1222 (*regs)[12] = tswapreg(env->aregs[5]);
1223 (*regs)[13] = tswapreg(env->aregs[6]);
1224 (*regs)[14] = tswapreg(env->dregs[0]);
1225 (*regs)[15] = tswapreg(env->aregs[7]);
1226 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1227 (*regs)[17] = tswapreg(env->sr);
1228 (*regs)[18] = tswapreg(env->pc);
1229 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1232 #define USE_ELF_CORE_DUMP
1233 #define ELF_EXEC_PAGESIZE 8192
1235 #endif
1237 #ifdef TARGET_ALPHA
1239 #define ELF_START_MMAP (0x30000000000ULL)
1241 #define ELF_CLASS ELFCLASS64
1242 #define ELF_ARCH EM_ALPHA
1244 static inline void init_thread(struct target_pt_regs *regs,
1245 struct image_info *infop)
1247 regs->pc = infop->entry;
1248 regs->ps = 8;
1249 regs->usp = infop->start_stack;
1252 #define ELF_EXEC_PAGESIZE 8192
1254 #endif /* TARGET_ALPHA */
1256 #ifdef TARGET_S390X
1258 #define ELF_START_MMAP (0x20000000000ULL)
1260 #define ELF_CLASS ELFCLASS64
1261 #define ELF_DATA ELFDATA2MSB
1262 #define ELF_ARCH EM_S390
1264 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1266 regs->psw.addr = infop->entry;
1267 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1268 regs->gprs[15] = infop->start_stack;
1271 #endif /* TARGET_S390X */
1273 #ifdef TARGET_TILEGX
1275 /* 42 bits real used address, a half for user mode */
1276 #define ELF_START_MMAP (0x00000020000000000ULL)
1278 #define elf_check_arch(x) ((x) == EM_TILEGX)
1280 #define ELF_CLASS ELFCLASS64
1281 #define ELF_DATA ELFDATA2LSB
1282 #define ELF_ARCH EM_TILEGX
1284 static inline void init_thread(struct target_pt_regs *regs,
1285 struct image_info *infop)
1287 regs->pc = infop->entry;
1288 regs->sp = infop->start_stack;
1292 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1294 #endif /* TARGET_TILEGX */
1296 #ifdef TARGET_HPPA
1298 #define ELF_START_MMAP 0x80000000
1299 #define ELF_CLASS ELFCLASS32
1300 #define ELF_ARCH EM_PARISC
1301 #define ELF_PLATFORM "PARISC"
1302 #define STACK_GROWS_DOWN 0
1303 #define STACK_ALIGNMENT 64
1305 static inline void init_thread(struct target_pt_regs *regs,
1306 struct image_info *infop)
1308 regs->iaoq[0] = infop->entry;
1309 regs->iaoq[1] = infop->entry + 4;
1310 regs->gr[23] = 0;
1311 regs->gr[24] = infop->arg_start;
1312 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1313 /* The top-of-stack contains a linkage buffer. */
1314 regs->gr[30] = infop->start_stack + 64;
1315 regs->gr[31] = infop->entry;
1318 #endif /* TARGET_HPPA */
1320 #ifndef ELF_PLATFORM
1321 #define ELF_PLATFORM (NULL)
1322 #endif
1324 #ifndef ELF_MACHINE
1325 #define ELF_MACHINE ELF_ARCH
1326 #endif
1328 #ifndef elf_check_arch
1329 #define elf_check_arch(x) ((x) == ELF_ARCH)
1330 #endif
1332 #ifndef ELF_HWCAP
1333 #define ELF_HWCAP 0
1334 #endif
1336 #ifndef STACK_GROWS_DOWN
1337 #define STACK_GROWS_DOWN 1
1338 #endif
1340 #ifndef STACK_ALIGNMENT
1341 #define STACK_ALIGNMENT 16
1342 #endif
1344 #ifdef TARGET_ABI32
1345 #undef ELF_CLASS
1346 #define ELF_CLASS ELFCLASS32
1347 #undef bswaptls
1348 #define bswaptls(ptr) bswap32s(ptr)
1349 #endif
1351 #include "elf.h"
1353 struct exec
1355 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1356 unsigned int a_text; /* length of text, in bytes */
1357 unsigned int a_data; /* length of data, in bytes */
1358 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1359 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1360 unsigned int a_entry; /* start address */
1361 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1362 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1366 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1367 #define OMAGIC 0407
1368 #define NMAGIC 0410
1369 #define ZMAGIC 0413
1370 #define QMAGIC 0314
1372 /* Necessary parameters */
1373 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1374 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1375 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1376 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1378 #define DLINFO_ITEMS 15
1380 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1382 memcpy(to, from, n);
1385 #ifdef BSWAP_NEEDED
1386 static void bswap_ehdr(struct elfhdr *ehdr)
1388 bswap16s(&ehdr->e_type); /* Object file type */
1389 bswap16s(&ehdr->e_machine); /* Architecture */
1390 bswap32s(&ehdr->e_version); /* Object file version */
1391 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1392 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1393 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1394 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1395 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1396 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1397 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1398 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1399 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1400 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1403 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1405 int i;
1406 for (i = 0; i < phnum; ++i, ++phdr) {
1407 bswap32s(&phdr->p_type); /* Segment type */
1408 bswap32s(&phdr->p_flags); /* Segment flags */
1409 bswaptls(&phdr->p_offset); /* Segment file offset */
1410 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1411 bswaptls(&phdr->p_paddr); /* Segment physical address */
1412 bswaptls(&phdr->p_filesz); /* Segment size in file */
1413 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1414 bswaptls(&phdr->p_align); /* Segment alignment */
1418 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1420 int i;
1421 for (i = 0; i < shnum; ++i, ++shdr) {
1422 bswap32s(&shdr->sh_name);
1423 bswap32s(&shdr->sh_type);
1424 bswaptls(&shdr->sh_flags);
1425 bswaptls(&shdr->sh_addr);
1426 bswaptls(&shdr->sh_offset);
1427 bswaptls(&shdr->sh_size);
1428 bswap32s(&shdr->sh_link);
1429 bswap32s(&shdr->sh_info);
1430 bswaptls(&shdr->sh_addralign);
1431 bswaptls(&shdr->sh_entsize);
1435 static void bswap_sym(struct elf_sym *sym)
1437 bswap32s(&sym->st_name);
1438 bswaptls(&sym->st_value);
1439 bswaptls(&sym->st_size);
1440 bswap16s(&sym->st_shndx);
1442 #else
1443 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1444 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1445 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1446 static inline void bswap_sym(struct elf_sym *sym) { }
1447 #endif
1449 #ifdef USE_ELF_CORE_DUMP
1450 static int elf_core_dump(int, const CPUArchState *);
1451 #endif /* USE_ELF_CORE_DUMP */
1452 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1454 /* Verify the portions of EHDR within E_IDENT for the target.
1455 This can be performed before bswapping the entire header. */
1456 static bool elf_check_ident(struct elfhdr *ehdr)
1458 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1459 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1460 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1461 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1462 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1463 && ehdr->e_ident[EI_DATA] == ELF_DATA
1464 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1467 /* Verify the portions of EHDR outside of E_IDENT for the target.
1468 This has to wait until after bswapping the header. */
1469 static bool elf_check_ehdr(struct elfhdr *ehdr)
1471 return (elf_check_arch(ehdr->e_machine)
1472 && ehdr->e_ehsize == sizeof(struct elfhdr)
1473 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1474 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1478 * 'copy_elf_strings()' copies argument/envelope strings from user
1479 * memory to free pages in kernel mem. These are in a format ready
1480 * to be put directly into the top of new user memory.
1483 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1484 abi_ulong p, abi_ulong stack_limit)
1486 char *tmp;
1487 int len, i;
1488 abi_ulong top = p;
1490 if (!p) {
1491 return 0; /* bullet-proofing */
1494 if (STACK_GROWS_DOWN) {
1495 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1496 for (i = argc - 1; i >= 0; --i) {
1497 tmp = argv[i];
1498 if (!tmp) {
1499 fprintf(stderr, "VFS: argc is wrong");
1500 exit(-1);
1502 len = strlen(tmp) + 1;
1503 tmp += len;
1505 if (len > (p - stack_limit)) {
1506 return 0;
1508 while (len) {
1509 int bytes_to_copy = (len > offset) ? offset : len;
1510 tmp -= bytes_to_copy;
1511 p -= bytes_to_copy;
1512 offset -= bytes_to_copy;
1513 len -= bytes_to_copy;
1515 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1517 if (offset == 0) {
1518 memcpy_to_target(p, scratch, top - p);
1519 top = p;
1520 offset = TARGET_PAGE_SIZE;
1524 if (p != top) {
1525 memcpy_to_target(p, scratch + offset, top - p);
1527 } else {
1528 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1529 for (i = 0; i < argc; ++i) {
1530 tmp = argv[i];
1531 if (!tmp) {
1532 fprintf(stderr, "VFS: argc is wrong");
1533 exit(-1);
1535 len = strlen(tmp) + 1;
1536 if (len > (stack_limit - p)) {
1537 return 0;
1539 while (len) {
1540 int bytes_to_copy = (len > remaining) ? remaining : len;
1542 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1544 tmp += bytes_to_copy;
1545 remaining -= bytes_to_copy;
1546 p += bytes_to_copy;
1547 len -= bytes_to_copy;
1549 if (remaining == 0) {
1550 memcpy_to_target(top, scratch, p - top);
1551 top = p;
1552 remaining = TARGET_PAGE_SIZE;
1556 if (p != top) {
1557 memcpy_to_target(top, scratch, p - top);
1561 return p;
1564 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1565 * argument/environment space. Newer kernels (>2.6.33) allow more,
1566 * dependent on stack size, but guarantee at least 32 pages for
1567 * backwards compatibility.
1569 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1571 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1572 struct image_info *info)
1574 abi_ulong size, error, guard;
1576 size = guest_stack_size;
1577 if (size < STACK_LOWER_LIMIT) {
1578 size = STACK_LOWER_LIMIT;
1580 guard = TARGET_PAGE_SIZE;
1581 if (guard < qemu_real_host_page_size) {
1582 guard = qemu_real_host_page_size;
1585 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1586 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1587 if (error == -1) {
1588 perror("mmap stack");
1589 exit(-1);
1592 /* We reserve one extra page at the top of the stack as guard. */
1593 if (STACK_GROWS_DOWN) {
1594 target_mprotect(error, guard, PROT_NONE);
1595 info->stack_limit = error + guard;
1596 return info->stack_limit + size - sizeof(void *);
1597 } else {
1598 target_mprotect(error + size, guard, PROT_NONE);
1599 info->stack_limit = error + size;
1600 return error;
1604 /* Map and zero the bss. We need to explicitly zero any fractional pages
1605 after the data section (i.e. bss). */
1606 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1608 uintptr_t host_start, host_map_start, host_end;
1610 last_bss = TARGET_PAGE_ALIGN(last_bss);
1612 /* ??? There is confusion between qemu_real_host_page_size and
1613 qemu_host_page_size here and elsewhere in target_mmap, which
1614 may lead to the end of the data section mapping from the file
1615 not being mapped. At least there was an explicit test and
1616 comment for that here, suggesting that "the file size must
1617 be known". The comment probably pre-dates the introduction
1618 of the fstat system call in target_mmap which does in fact
1619 find out the size. What isn't clear is if the workaround
1620 here is still actually needed. For now, continue with it,
1621 but merge it with the "normal" mmap that would allocate the bss. */
1623 host_start = (uintptr_t) g2h(elf_bss);
1624 host_end = (uintptr_t) g2h(last_bss);
1625 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1627 if (host_map_start < host_end) {
1628 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1629 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1630 if (p == MAP_FAILED) {
1631 perror("cannot mmap brk");
1632 exit(-1);
1636 /* Ensure that the bss page(s) are valid */
1637 if ((page_get_flags(last_bss-1) & prot) != prot) {
1638 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1641 if (host_start < host_map_start) {
1642 memset((void *)host_start, 0, host_map_start - host_start);
1646 #ifdef CONFIG_USE_FDPIC
1647 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1649 uint16_t n;
1650 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1652 /* elf32_fdpic_loadseg */
1653 n = info->nsegs;
1654 while (n--) {
1655 sp -= 12;
1656 put_user_u32(loadsegs[n].addr, sp+0);
1657 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1658 put_user_u32(loadsegs[n].p_memsz, sp+8);
1661 /* elf32_fdpic_loadmap */
1662 sp -= 4;
1663 put_user_u16(0, sp+0); /* version */
1664 put_user_u16(info->nsegs, sp+2); /* nsegs */
1666 info->personality = PER_LINUX_FDPIC;
1667 info->loadmap_addr = sp;
1669 return sp;
1671 #endif
1673 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1674 struct elfhdr *exec,
1675 struct image_info *info,
1676 struct image_info *interp_info)
1678 abi_ulong sp;
1679 abi_ulong u_argc, u_argv, u_envp, u_auxv;
1680 int size;
1681 int i;
1682 abi_ulong u_rand_bytes;
1683 uint8_t k_rand_bytes[16];
1684 abi_ulong u_platform;
1685 const char *k_platform;
1686 const int n = sizeof(elf_addr_t);
1688 sp = p;
1690 #ifdef CONFIG_USE_FDPIC
1691 /* Needs to be before we load the env/argc/... */
1692 if (elf_is_fdpic(exec)) {
1693 /* Need 4 byte alignment for these structs */
1694 sp &= ~3;
1695 sp = loader_build_fdpic_loadmap(info, sp);
1696 info->other_info = interp_info;
1697 if (interp_info) {
1698 interp_info->other_info = info;
1699 sp = loader_build_fdpic_loadmap(interp_info, sp);
1702 #endif
1704 u_platform = 0;
1705 k_platform = ELF_PLATFORM;
1706 if (k_platform) {
1707 size_t len = strlen(k_platform) + 1;
1708 if (STACK_GROWS_DOWN) {
1709 sp -= (len + n - 1) & ~(n - 1);
1710 u_platform = sp;
1711 /* FIXME - check return value of memcpy_to_target() for failure */
1712 memcpy_to_target(sp, k_platform, len);
1713 } else {
1714 memcpy_to_target(sp, k_platform, len);
1715 u_platform = sp;
1716 sp += len + 1;
1720 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1721 * the argv and envp pointers.
1723 if (STACK_GROWS_DOWN) {
1724 sp = QEMU_ALIGN_DOWN(sp, 16);
1725 } else {
1726 sp = QEMU_ALIGN_UP(sp, 16);
1730 * Generate 16 random bytes for userspace PRNG seeding (not
1731 * cryptically secure but it's not the aim of QEMU).
1733 for (i = 0; i < 16; i++) {
1734 k_rand_bytes[i] = rand();
1736 if (STACK_GROWS_DOWN) {
1737 sp -= 16;
1738 u_rand_bytes = sp;
1739 /* FIXME - check return value of memcpy_to_target() for failure */
1740 memcpy_to_target(sp, k_rand_bytes, 16);
1741 } else {
1742 memcpy_to_target(sp, k_rand_bytes, 16);
1743 u_rand_bytes = sp;
1744 sp += 16;
1747 size = (DLINFO_ITEMS + 1) * 2;
1748 if (k_platform)
1749 size += 2;
1750 #ifdef DLINFO_ARCH_ITEMS
1751 size += DLINFO_ARCH_ITEMS * 2;
1752 #endif
1753 #ifdef ELF_HWCAP2
1754 size += 2;
1755 #endif
1756 info->auxv_len = size * n;
1758 size += envc + argc + 2;
1759 size += 1; /* argc itself */
1760 size *= n;
1762 /* Allocate space and finalize stack alignment for entry now. */
1763 if (STACK_GROWS_DOWN) {
1764 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
1765 sp = u_argc;
1766 } else {
1767 u_argc = sp;
1768 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
1771 u_argv = u_argc + n;
1772 u_envp = u_argv + (argc + 1) * n;
1773 u_auxv = u_envp + (envc + 1) * n;
1774 info->saved_auxv = u_auxv;
1775 info->arg_start = u_argv;
1776 info->arg_end = u_argv + argc * n;
1778 /* This is correct because Linux defines
1779 * elf_addr_t as Elf32_Off / Elf64_Off
1781 #define NEW_AUX_ENT(id, val) do { \
1782 put_user_ual(id, u_auxv); u_auxv += n; \
1783 put_user_ual(val, u_auxv); u_auxv += n; \
1784 } while(0)
1786 #ifdef ARCH_DLINFO
1788 * ARCH_DLINFO must come first so platform specific code can enforce
1789 * special alignment requirements on the AUXV if necessary (eg. PPC).
1791 ARCH_DLINFO;
1792 #endif
1793 /* There must be exactly DLINFO_ITEMS entries here, or the assert
1794 * on info->auxv_len will trigger.
1796 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1797 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1798 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1799 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize())));
1800 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1801 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1802 NEW_AUX_ENT(AT_ENTRY, info->entry);
1803 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1804 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1805 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1806 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1807 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1808 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1809 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1810 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
1812 #ifdef ELF_HWCAP2
1813 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1814 #endif
1816 if (u_platform) {
1817 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1819 NEW_AUX_ENT (AT_NULL, 0);
1820 #undef NEW_AUX_ENT
1822 /* Check that our initial calculation of the auxv length matches how much
1823 * we actually put into it.
1825 assert(info->auxv_len == u_auxv - info->saved_auxv);
1827 put_user_ual(argc, u_argc);
1829 p = info->arg_strings;
1830 for (i = 0; i < argc; ++i) {
1831 put_user_ual(p, u_argv);
1832 u_argv += n;
1833 p += target_strlen(p) + 1;
1835 put_user_ual(0, u_argv);
1837 p = info->env_strings;
1838 for (i = 0; i < envc; ++i) {
1839 put_user_ual(p, u_envp);
1840 u_envp += n;
1841 p += target_strlen(p) + 1;
1843 put_user_ual(0, u_envp);
1845 return sp;
1848 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1849 /* If the guest doesn't have a validation function just agree */
1850 static int validate_guest_space(unsigned long guest_base,
1851 unsigned long guest_size)
1853 return 1;
1855 #endif
1857 unsigned long init_guest_space(unsigned long host_start,
1858 unsigned long host_size,
1859 unsigned long guest_start,
1860 bool fixed)
1862 unsigned long current_start, real_start;
1863 int flags;
1865 assert(host_start || host_size);
1867 /* If just a starting address is given, then just verify that
1868 * address. */
1869 if (host_start && !host_size) {
1870 if (validate_guest_space(host_start, host_size) == 1) {
1871 return host_start;
1872 } else {
1873 return (unsigned long)-1;
1877 /* Setup the initial flags and start address. */
1878 current_start = host_start & qemu_host_page_mask;
1879 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1880 if (fixed) {
1881 flags |= MAP_FIXED;
1884 /* Otherwise, a non-zero size region of memory needs to be mapped
1885 * and validated. */
1886 while (1) {
1887 unsigned long real_size = host_size;
1889 /* Do not use mmap_find_vma here because that is limited to the
1890 * guest address space. We are going to make the
1891 * guest address space fit whatever we're given.
1893 real_start = (unsigned long)
1894 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1895 if (real_start == (unsigned long)-1) {
1896 return (unsigned long)-1;
1899 /* Ensure the address is properly aligned. */
1900 if (real_start & ~qemu_host_page_mask) {
1901 munmap((void *)real_start, host_size);
1902 real_size = host_size + qemu_host_page_size;
1903 real_start = (unsigned long)
1904 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1905 if (real_start == (unsigned long)-1) {
1906 return (unsigned long)-1;
1908 real_start = HOST_PAGE_ALIGN(real_start);
1911 /* Check to see if the address is valid. */
1912 if (!host_start || real_start == current_start) {
1913 int valid = validate_guest_space(real_start - guest_start,
1914 real_size);
1915 if (valid == 1) {
1916 break;
1917 } else if (valid == -1) {
1918 return (unsigned long)-1;
1920 /* valid == 0, so try again. */
1923 /* That address didn't work. Unmap and try a different one.
1924 * The address the host picked because is typically right at
1925 * the top of the host address space and leaves the guest with
1926 * no usable address space. Resort to a linear search. We
1927 * already compensated for mmap_min_addr, so this should not
1928 * happen often. Probably means we got unlucky and host
1929 * address space randomization put a shared library somewhere
1930 * inconvenient.
1932 munmap((void *)real_start, host_size);
1933 current_start += qemu_host_page_size;
1934 if (host_start == current_start) {
1935 /* Theoretically possible if host doesn't have any suitably
1936 * aligned areas. Normally the first mmap will fail.
1938 return (unsigned long)-1;
1942 qemu_log_mask(CPU_LOG_PAGE, "Reserved 0x%lx bytes of guest address space\n", host_size);
1944 return real_start;
1947 static void probe_guest_base(const char *image_name,
1948 abi_ulong loaddr, abi_ulong hiaddr)
1950 /* Probe for a suitable guest base address, if the user has not set
1951 * it explicitly, and set guest_base appropriately.
1952 * In case of error we will print a suitable message and exit.
1954 const char *errmsg;
1955 if (!have_guest_base && !reserved_va) {
1956 unsigned long host_start, real_start, host_size;
1958 /* Round addresses to page boundaries. */
1959 loaddr &= qemu_host_page_mask;
1960 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1962 if (loaddr < mmap_min_addr) {
1963 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1964 } else {
1965 host_start = loaddr;
1966 if (host_start != loaddr) {
1967 errmsg = "Address overflow loading ELF binary";
1968 goto exit_errmsg;
1971 host_size = hiaddr - loaddr;
1973 /* Setup the initial guest memory space with ranges gleaned from
1974 * the ELF image that is being loaded.
1976 real_start = init_guest_space(host_start, host_size, loaddr, false);
1977 if (real_start == (unsigned long)-1) {
1978 errmsg = "Unable to find space for application";
1979 goto exit_errmsg;
1981 guest_base = real_start - loaddr;
1983 qemu_log_mask(CPU_LOG_PAGE, "Relocating guest address space from 0x"
1984 TARGET_ABI_FMT_lx " to 0x%lx\n",
1985 loaddr, real_start);
1987 return;
1989 exit_errmsg:
1990 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1991 exit(-1);
1995 /* Load an ELF image into the address space.
1997 IMAGE_NAME is the filename of the image, to use in error messages.
1998 IMAGE_FD is the open file descriptor for the image.
2000 BPRM_BUF is a copy of the beginning of the file; this of course
2001 contains the elf file header at offset 0. It is assumed that this
2002 buffer is sufficiently aligned to present no problems to the host
2003 in accessing data at aligned offsets within the buffer.
2005 On return: INFO values will be filled in, as necessary or available. */
2007 static void load_elf_image(const char *image_name, int image_fd,
2008 struct image_info *info, char **pinterp_name,
2009 char bprm_buf[BPRM_BUF_SIZE])
2011 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2012 struct elf_phdr *phdr;
2013 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
2014 int i, retval;
2015 const char *errmsg;
2017 /* First of all, some simple consistency checks */
2018 errmsg = "Invalid ELF image for this architecture";
2019 if (!elf_check_ident(ehdr)) {
2020 goto exit_errmsg;
2022 bswap_ehdr(ehdr);
2023 if (!elf_check_ehdr(ehdr)) {
2024 goto exit_errmsg;
2027 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2028 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2029 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
2030 } else {
2031 phdr = (struct elf_phdr *) alloca(i);
2032 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
2033 if (retval != i) {
2034 goto exit_read;
2037 bswap_phdr(phdr, ehdr->e_phnum);
2039 #ifdef CONFIG_USE_FDPIC
2040 info->nsegs = 0;
2041 info->pt_dynamic_addr = 0;
2042 #endif
2044 mmap_lock();
2046 /* Find the maximum size of the image and allocate an appropriate
2047 amount of memory to handle that. */
2048 loaddr = -1, hiaddr = 0;
2049 for (i = 0; i < ehdr->e_phnum; ++i) {
2050 if (phdr[i].p_type == PT_LOAD) {
2051 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
2052 if (a < loaddr) {
2053 loaddr = a;
2055 a = phdr[i].p_vaddr + phdr[i].p_memsz;
2056 if (a > hiaddr) {
2057 hiaddr = a;
2059 #ifdef CONFIG_USE_FDPIC
2060 ++info->nsegs;
2061 #endif
2065 load_addr = loaddr;
2066 if (ehdr->e_type == ET_DYN) {
2067 /* The image indicates that it can be loaded anywhere. Find a
2068 location that can hold the memory space required. If the
2069 image is pre-linked, LOADDR will be non-zero. Since we do
2070 not supply MAP_FIXED here we'll use that address if and
2071 only if it remains available. */
2072 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2073 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
2074 -1, 0);
2075 if (load_addr == -1) {
2076 goto exit_perror;
2078 } else if (pinterp_name != NULL) {
2079 /* This is the main executable. Make sure that the low
2080 address does not conflict with MMAP_MIN_ADDR or the
2081 QEMU application itself. */
2082 probe_guest_base(image_name, loaddr, hiaddr);
2084 load_bias = load_addr - loaddr;
2086 #ifdef CONFIG_USE_FDPIC
2088 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2089 g_malloc(sizeof(*loadsegs) * info->nsegs);
2091 for (i = 0; i < ehdr->e_phnum; ++i) {
2092 switch (phdr[i].p_type) {
2093 case PT_DYNAMIC:
2094 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2095 break;
2096 case PT_LOAD:
2097 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2098 loadsegs->p_vaddr = phdr[i].p_vaddr;
2099 loadsegs->p_memsz = phdr[i].p_memsz;
2100 ++loadsegs;
2101 break;
2105 #endif
2107 info->load_bias = load_bias;
2108 info->load_addr = load_addr;
2109 info->entry = ehdr->e_entry + load_bias;
2110 info->start_code = -1;
2111 info->end_code = 0;
2112 info->start_data = -1;
2113 info->end_data = 0;
2114 info->brk = 0;
2115 info->elf_flags = ehdr->e_flags;
2117 for (i = 0; i < ehdr->e_phnum; i++) {
2118 struct elf_phdr *eppnt = phdr + i;
2119 if (eppnt->p_type == PT_LOAD) {
2120 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
2121 int elf_prot = 0;
2123 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
2124 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
2125 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
2127 vaddr = load_bias + eppnt->p_vaddr;
2128 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2129 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2131 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
2132 elf_prot, MAP_PRIVATE | MAP_FIXED,
2133 image_fd, eppnt->p_offset - vaddr_po);
2134 if (error == -1) {
2135 goto exit_perror;
2138 vaddr_ef = vaddr + eppnt->p_filesz;
2139 vaddr_em = vaddr + eppnt->p_memsz;
2141 /* If the load segment requests extra zeros (e.g. bss), map it. */
2142 if (vaddr_ef < vaddr_em) {
2143 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2146 /* Find the full program boundaries. */
2147 if (elf_prot & PROT_EXEC) {
2148 if (vaddr < info->start_code) {
2149 info->start_code = vaddr;
2151 if (vaddr_ef > info->end_code) {
2152 info->end_code = vaddr_ef;
2155 if (elf_prot & PROT_WRITE) {
2156 if (vaddr < info->start_data) {
2157 info->start_data = vaddr;
2159 if (vaddr_ef > info->end_data) {
2160 info->end_data = vaddr_ef;
2162 if (vaddr_em > info->brk) {
2163 info->brk = vaddr_em;
2166 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2167 char *interp_name;
2169 if (*pinterp_name) {
2170 errmsg = "Multiple PT_INTERP entries";
2171 goto exit_errmsg;
2173 interp_name = malloc(eppnt->p_filesz);
2174 if (!interp_name) {
2175 goto exit_perror;
2178 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2179 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2180 eppnt->p_filesz);
2181 } else {
2182 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2183 eppnt->p_offset);
2184 if (retval != eppnt->p_filesz) {
2185 goto exit_perror;
2188 if (interp_name[eppnt->p_filesz - 1] != 0) {
2189 errmsg = "Invalid PT_INTERP entry";
2190 goto exit_errmsg;
2192 *pinterp_name = interp_name;
2196 if (info->end_data == 0) {
2197 info->start_data = info->end_code;
2198 info->end_data = info->end_code;
2199 info->brk = info->end_code;
2202 if (qemu_log_enabled()) {
2203 load_symbols(ehdr, image_fd, load_bias);
2206 mmap_unlock();
2208 close(image_fd);
2209 return;
2211 exit_read:
2212 if (retval >= 0) {
2213 errmsg = "Incomplete read of file header";
2214 goto exit_errmsg;
2216 exit_perror:
2217 errmsg = strerror(errno);
2218 exit_errmsg:
2219 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2220 exit(-1);
2223 static void load_elf_interp(const char *filename, struct image_info *info,
2224 char bprm_buf[BPRM_BUF_SIZE])
2226 int fd, retval;
2228 fd = open(path(filename), O_RDONLY);
2229 if (fd < 0) {
2230 goto exit_perror;
2233 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2234 if (retval < 0) {
2235 goto exit_perror;
2237 if (retval < BPRM_BUF_SIZE) {
2238 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2241 load_elf_image(filename, fd, info, NULL, bprm_buf);
2242 return;
2244 exit_perror:
2245 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2246 exit(-1);
2249 static int symfind(const void *s0, const void *s1)
2251 target_ulong addr = *(target_ulong *)s0;
2252 struct elf_sym *sym = (struct elf_sym *)s1;
2253 int result = 0;
2254 if (addr < sym->st_value) {
2255 result = -1;
2256 } else if (addr >= sym->st_value + sym->st_size) {
2257 result = 1;
2259 return result;
2262 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2264 #if ELF_CLASS == ELFCLASS32
2265 struct elf_sym *syms = s->disas_symtab.elf32;
2266 #else
2267 struct elf_sym *syms = s->disas_symtab.elf64;
2268 #endif
2270 // binary search
2271 struct elf_sym *sym;
2273 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2274 if (sym != NULL) {
2275 return s->disas_strtab + sym->st_name;
2278 return "";
2281 /* FIXME: This should use elf_ops.h */
2282 static int symcmp(const void *s0, const void *s1)
2284 struct elf_sym *sym0 = (struct elf_sym *)s0;
2285 struct elf_sym *sym1 = (struct elf_sym *)s1;
2286 return (sym0->st_value < sym1->st_value)
2287 ? -1
2288 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2291 /* Best attempt to load symbols from this ELF object. */
2292 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2294 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2295 uint64_t segsz;
2296 struct elf_shdr *shdr;
2297 char *strings = NULL;
2298 struct syminfo *s = NULL;
2299 struct elf_sym *new_syms, *syms = NULL;
2301 shnum = hdr->e_shnum;
2302 i = shnum * sizeof(struct elf_shdr);
2303 shdr = (struct elf_shdr *)alloca(i);
2304 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2305 return;
2308 bswap_shdr(shdr, shnum);
2309 for (i = 0; i < shnum; ++i) {
2310 if (shdr[i].sh_type == SHT_SYMTAB) {
2311 sym_idx = i;
2312 str_idx = shdr[i].sh_link;
2313 goto found;
2317 /* There will be no symbol table if the file was stripped. */
2318 return;
2320 found:
2321 /* Now know where the strtab and symtab are. Snarf them. */
2322 s = g_try_new(struct syminfo, 1);
2323 if (!s) {
2324 goto give_up;
2327 segsz = shdr[str_idx].sh_size;
2328 s->disas_strtab = strings = g_try_malloc(segsz);
2329 if (!strings ||
2330 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
2331 goto give_up;
2334 segsz = shdr[sym_idx].sh_size;
2335 syms = g_try_malloc(segsz);
2336 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
2337 goto give_up;
2340 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
2341 /* Implausibly large symbol table: give up rather than ploughing
2342 * on with the number of symbols calculation overflowing
2344 goto give_up;
2346 nsyms = segsz / sizeof(struct elf_sym);
2347 for (i = 0; i < nsyms; ) {
2348 bswap_sym(syms + i);
2349 /* Throw away entries which we do not need. */
2350 if (syms[i].st_shndx == SHN_UNDEF
2351 || syms[i].st_shndx >= SHN_LORESERVE
2352 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2353 if (i < --nsyms) {
2354 syms[i] = syms[nsyms];
2356 } else {
2357 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2358 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2359 syms[i].st_value &= ~(target_ulong)1;
2360 #endif
2361 syms[i].st_value += load_bias;
2362 i++;
2366 /* No "useful" symbol. */
2367 if (nsyms == 0) {
2368 goto give_up;
2371 /* Attempt to free the storage associated with the local symbols
2372 that we threw away. Whether or not this has any effect on the
2373 memory allocation depends on the malloc implementation and how
2374 many symbols we managed to discard. */
2375 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
2376 if (new_syms == NULL) {
2377 goto give_up;
2379 syms = new_syms;
2381 qsort(syms, nsyms, sizeof(*syms), symcmp);
2383 s->disas_num_syms = nsyms;
2384 #if ELF_CLASS == ELFCLASS32
2385 s->disas_symtab.elf32 = syms;
2386 #else
2387 s->disas_symtab.elf64 = syms;
2388 #endif
2389 s->lookup_symbol = lookup_symbolxx;
2390 s->next = syminfos;
2391 syminfos = s;
2393 return;
2395 give_up:
2396 g_free(s);
2397 g_free(strings);
2398 g_free(syms);
2401 uint32_t get_elf_eflags(int fd)
2403 struct elfhdr ehdr;
2404 off_t offset;
2405 int ret;
2407 /* Read ELF header */
2408 offset = lseek(fd, 0, SEEK_SET);
2409 if (offset == (off_t) -1) {
2410 return 0;
2412 ret = read(fd, &ehdr, sizeof(ehdr));
2413 if (ret < sizeof(ehdr)) {
2414 return 0;
2416 offset = lseek(fd, offset, SEEK_SET);
2417 if (offset == (off_t) -1) {
2418 return 0;
2421 /* Check ELF signature */
2422 if (!elf_check_ident(&ehdr)) {
2423 return 0;
2426 /* check header */
2427 bswap_ehdr(&ehdr);
2428 if (!elf_check_ehdr(&ehdr)) {
2429 return 0;
2432 /* return architecture id */
2433 return ehdr.e_flags;
2436 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2438 struct image_info interp_info;
2439 struct elfhdr elf_ex;
2440 char *elf_interpreter = NULL;
2441 char *scratch;
2443 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2445 load_elf_image(bprm->filename, bprm->fd, info,
2446 &elf_interpreter, bprm->buf);
2448 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2449 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2450 when we load the interpreter. */
2451 elf_ex = *(struct elfhdr *)bprm->buf;
2453 /* Do this so that we can load the interpreter, if need be. We will
2454 change some of these later */
2455 bprm->p = setup_arg_pages(bprm, info);
2457 scratch = g_new0(char, TARGET_PAGE_SIZE);
2458 if (STACK_GROWS_DOWN) {
2459 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2460 bprm->p, info->stack_limit);
2461 info->file_string = bprm->p;
2462 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2463 bprm->p, info->stack_limit);
2464 info->env_strings = bprm->p;
2465 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2466 bprm->p, info->stack_limit);
2467 info->arg_strings = bprm->p;
2468 } else {
2469 info->arg_strings = bprm->p;
2470 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2471 bprm->p, info->stack_limit);
2472 info->env_strings = bprm->p;
2473 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2474 bprm->p, info->stack_limit);
2475 info->file_string = bprm->p;
2476 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2477 bprm->p, info->stack_limit);
2480 g_free(scratch);
2482 if (!bprm->p) {
2483 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2484 exit(-1);
2487 if (elf_interpreter) {
2488 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2490 /* If the program interpreter is one of these two, then assume
2491 an iBCS2 image. Otherwise assume a native linux image. */
2493 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2494 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2495 info->personality = PER_SVR4;
2497 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2498 and some applications "depend" upon this behavior. Since
2499 we do not have the power to recompile these, we emulate
2500 the SVr4 behavior. Sigh. */
2501 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2502 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2506 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2507 info, (elf_interpreter ? &interp_info : NULL));
2508 info->start_stack = bprm->p;
2510 /* If we have an interpreter, set that as the program's entry point.
2511 Copy the load_bias as well, to help PPC64 interpret the entry
2512 point as a function descriptor. Do this after creating elf tables
2513 so that we copy the original program entry point into the AUXV. */
2514 if (elf_interpreter) {
2515 info->load_bias = interp_info.load_bias;
2516 info->entry = interp_info.entry;
2517 free(elf_interpreter);
2520 #ifdef USE_ELF_CORE_DUMP
2521 bprm->core_dump = &elf_core_dump;
2522 #endif
2524 return 0;
2527 #ifdef USE_ELF_CORE_DUMP
2529 * Definitions to generate Intel SVR4-like core files.
2530 * These mostly have the same names as the SVR4 types with "target_elf_"
2531 * tacked on the front to prevent clashes with linux definitions,
2532 * and the typedef forms have been avoided. This is mostly like
2533 * the SVR4 structure, but more Linuxy, with things that Linux does
2534 * not support and which gdb doesn't really use excluded.
2536 * Fields we don't dump (their contents is zero) in linux-user qemu
2537 * are marked with XXX.
2539 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2541 * Porting ELF coredump for target is (quite) simple process. First you
2542 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2543 * the target resides):
2545 * #define USE_ELF_CORE_DUMP
2547 * Next you define type of register set used for dumping. ELF specification
2548 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2550 * typedef <target_regtype> target_elf_greg_t;
2551 * #define ELF_NREG <number of registers>
2552 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2554 * Last step is to implement target specific function that copies registers
2555 * from given cpu into just specified register set. Prototype is:
2557 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2558 * const CPUArchState *env);
2560 * Parameters:
2561 * regs - copy register values into here (allocated and zeroed by caller)
2562 * env - copy registers from here
2564 * Example for ARM target is provided in this file.
2567 /* An ELF note in memory */
2568 struct memelfnote {
2569 const char *name;
2570 size_t namesz;
2571 size_t namesz_rounded;
2572 int type;
2573 size_t datasz;
2574 size_t datasz_rounded;
2575 void *data;
2576 size_t notesz;
2579 struct target_elf_siginfo {
2580 abi_int si_signo; /* signal number */
2581 abi_int si_code; /* extra code */
2582 abi_int si_errno; /* errno */
2585 struct target_elf_prstatus {
2586 struct target_elf_siginfo pr_info; /* Info associated with signal */
2587 abi_short pr_cursig; /* Current signal */
2588 abi_ulong pr_sigpend; /* XXX */
2589 abi_ulong pr_sighold; /* XXX */
2590 target_pid_t pr_pid;
2591 target_pid_t pr_ppid;
2592 target_pid_t pr_pgrp;
2593 target_pid_t pr_sid;
2594 struct target_timeval pr_utime; /* XXX User time */
2595 struct target_timeval pr_stime; /* XXX System time */
2596 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2597 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2598 target_elf_gregset_t pr_reg; /* GP registers */
2599 abi_int pr_fpvalid; /* XXX */
2602 #define ELF_PRARGSZ (80) /* Number of chars for args */
2604 struct target_elf_prpsinfo {
2605 char pr_state; /* numeric process state */
2606 char pr_sname; /* char for pr_state */
2607 char pr_zomb; /* zombie */
2608 char pr_nice; /* nice val */
2609 abi_ulong pr_flag; /* flags */
2610 target_uid_t pr_uid;
2611 target_gid_t pr_gid;
2612 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2613 /* Lots missing */
2614 char pr_fname[16]; /* filename of executable */
2615 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2618 /* Here is the structure in which status of each thread is captured. */
2619 struct elf_thread_status {
2620 QTAILQ_ENTRY(elf_thread_status) ets_link;
2621 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2622 #if 0
2623 elf_fpregset_t fpu; /* NT_PRFPREG */
2624 struct task_struct *thread;
2625 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2626 #endif
2627 struct memelfnote notes[1];
2628 int num_notes;
2631 struct elf_note_info {
2632 struct memelfnote *notes;
2633 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2634 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2636 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2637 #if 0
2639 * Current version of ELF coredump doesn't support
2640 * dumping fp regs etc.
2642 elf_fpregset_t *fpu;
2643 elf_fpxregset_t *xfpu;
2644 int thread_status_size;
2645 #endif
2646 int notes_size;
2647 int numnote;
2650 struct vm_area_struct {
2651 target_ulong vma_start; /* start vaddr of memory region */
2652 target_ulong vma_end; /* end vaddr of memory region */
2653 abi_ulong vma_flags; /* protection etc. flags for the region */
2654 QTAILQ_ENTRY(vm_area_struct) vma_link;
2657 struct mm_struct {
2658 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2659 int mm_count; /* number of mappings */
2662 static struct mm_struct *vma_init(void);
2663 static void vma_delete(struct mm_struct *);
2664 static int vma_add_mapping(struct mm_struct *, target_ulong,
2665 target_ulong, abi_ulong);
2666 static int vma_get_mapping_count(const struct mm_struct *);
2667 static struct vm_area_struct *vma_first(const struct mm_struct *);
2668 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2669 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2670 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2671 unsigned long flags);
2673 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2674 static void fill_note(struct memelfnote *, const char *, int,
2675 unsigned int, void *);
2676 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2677 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2678 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2679 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2680 static size_t note_size(const struct memelfnote *);
2681 static void free_note_info(struct elf_note_info *);
2682 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2683 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2684 static int core_dump_filename(const TaskState *, char *, size_t);
2686 static int dump_write(int, const void *, size_t);
2687 static int write_note(struct memelfnote *, int);
2688 static int write_note_info(struct elf_note_info *, int);
2690 #ifdef BSWAP_NEEDED
2691 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2693 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2694 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2695 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2696 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2697 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2698 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2699 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2700 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2701 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2702 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2703 /* cpu times are not filled, so we skip them */
2704 /* regs should be in correct format already */
2705 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2708 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2710 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2711 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2712 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2713 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2714 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2715 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2716 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2719 static void bswap_note(struct elf_note *en)
2721 bswap32s(&en->n_namesz);
2722 bswap32s(&en->n_descsz);
2723 bswap32s(&en->n_type);
2725 #else
2726 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2727 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2728 static inline void bswap_note(struct elf_note *en) { }
2729 #endif /* BSWAP_NEEDED */
2732 * Minimal support for linux memory regions. These are needed
2733 * when we are finding out what memory exactly belongs to
2734 * emulated process. No locks needed here, as long as
2735 * thread that received the signal is stopped.
2738 static struct mm_struct *vma_init(void)
2740 struct mm_struct *mm;
2742 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2743 return (NULL);
2745 mm->mm_count = 0;
2746 QTAILQ_INIT(&mm->mm_mmap);
2748 return (mm);
2751 static void vma_delete(struct mm_struct *mm)
2753 struct vm_area_struct *vma;
2755 while ((vma = vma_first(mm)) != NULL) {
2756 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2757 g_free(vma);
2759 g_free(mm);
2762 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
2763 target_ulong end, abi_ulong flags)
2765 struct vm_area_struct *vma;
2767 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2768 return (-1);
2770 vma->vma_start = start;
2771 vma->vma_end = end;
2772 vma->vma_flags = flags;
2774 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2775 mm->mm_count++;
2777 return (0);
2780 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2782 return (QTAILQ_FIRST(&mm->mm_mmap));
2785 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2787 return (QTAILQ_NEXT(vma, vma_link));
2790 static int vma_get_mapping_count(const struct mm_struct *mm)
2792 return (mm->mm_count);
2796 * Calculate file (dump) size of given memory region.
2798 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2800 /* if we cannot even read the first page, skip it */
2801 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2802 return (0);
2805 * Usually we don't dump executable pages as they contain
2806 * non-writable code that debugger can read directly from
2807 * target library etc. However, thread stacks are marked
2808 * also executable so we read in first page of given region
2809 * and check whether it contains elf header. If there is
2810 * no elf header, we dump it.
2812 if (vma->vma_flags & PROT_EXEC) {
2813 char page[TARGET_PAGE_SIZE];
2815 copy_from_user(page, vma->vma_start, sizeof (page));
2816 if ((page[EI_MAG0] == ELFMAG0) &&
2817 (page[EI_MAG1] == ELFMAG1) &&
2818 (page[EI_MAG2] == ELFMAG2) &&
2819 (page[EI_MAG3] == ELFMAG3)) {
2821 * Mappings are possibly from ELF binary. Don't dump
2822 * them.
2824 return (0);
2828 return (vma->vma_end - vma->vma_start);
2831 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2832 unsigned long flags)
2834 struct mm_struct *mm = (struct mm_struct *)priv;
2836 vma_add_mapping(mm, start, end, flags);
2837 return (0);
2840 static void fill_note(struct memelfnote *note, const char *name, int type,
2841 unsigned int sz, void *data)
2843 unsigned int namesz;
2845 namesz = strlen(name) + 1;
2846 note->name = name;
2847 note->namesz = namesz;
2848 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2849 note->type = type;
2850 note->datasz = sz;
2851 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2853 note->data = data;
2856 * We calculate rounded up note size here as specified by
2857 * ELF document.
2859 note->notesz = sizeof (struct elf_note) +
2860 note->namesz_rounded + note->datasz_rounded;
2863 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2864 uint32_t flags)
2866 (void) memset(elf, 0, sizeof(*elf));
2868 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2869 elf->e_ident[EI_CLASS] = ELF_CLASS;
2870 elf->e_ident[EI_DATA] = ELF_DATA;
2871 elf->e_ident[EI_VERSION] = EV_CURRENT;
2872 elf->e_ident[EI_OSABI] = ELF_OSABI;
2874 elf->e_type = ET_CORE;
2875 elf->e_machine = machine;
2876 elf->e_version = EV_CURRENT;
2877 elf->e_phoff = sizeof(struct elfhdr);
2878 elf->e_flags = flags;
2879 elf->e_ehsize = sizeof(struct elfhdr);
2880 elf->e_phentsize = sizeof(struct elf_phdr);
2881 elf->e_phnum = segs;
2883 bswap_ehdr(elf);
2886 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2888 phdr->p_type = PT_NOTE;
2889 phdr->p_offset = offset;
2890 phdr->p_vaddr = 0;
2891 phdr->p_paddr = 0;
2892 phdr->p_filesz = sz;
2893 phdr->p_memsz = 0;
2894 phdr->p_flags = 0;
2895 phdr->p_align = 0;
2897 bswap_phdr(phdr, 1);
2900 static size_t note_size(const struct memelfnote *note)
2902 return (note->notesz);
2905 static void fill_prstatus(struct target_elf_prstatus *prstatus,
2906 const TaskState *ts, int signr)
2908 (void) memset(prstatus, 0, sizeof (*prstatus));
2909 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2910 prstatus->pr_pid = ts->ts_tid;
2911 prstatus->pr_ppid = getppid();
2912 prstatus->pr_pgrp = getpgrp();
2913 prstatus->pr_sid = getsid(0);
2915 bswap_prstatus(prstatus);
2918 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2920 char *base_filename;
2921 unsigned int i, len;
2923 (void) memset(psinfo, 0, sizeof (*psinfo));
2925 len = ts->info->arg_end - ts->info->arg_start;
2926 if (len >= ELF_PRARGSZ)
2927 len = ELF_PRARGSZ - 1;
2928 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2929 return -EFAULT;
2930 for (i = 0; i < len; i++)
2931 if (psinfo->pr_psargs[i] == 0)
2932 psinfo->pr_psargs[i] = ' ';
2933 psinfo->pr_psargs[len] = 0;
2935 psinfo->pr_pid = getpid();
2936 psinfo->pr_ppid = getppid();
2937 psinfo->pr_pgrp = getpgrp();
2938 psinfo->pr_sid = getsid(0);
2939 psinfo->pr_uid = getuid();
2940 psinfo->pr_gid = getgid();
2942 base_filename = g_path_get_basename(ts->bprm->filename);
2944 * Using strncpy here is fine: at max-length,
2945 * this field is not NUL-terminated.
2947 (void) strncpy(psinfo->pr_fname, base_filename,
2948 sizeof(psinfo->pr_fname));
2950 g_free(base_filename);
2951 bswap_psinfo(psinfo);
2952 return (0);
2955 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2957 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2958 elf_addr_t orig_auxv = auxv;
2959 void *ptr;
2960 int len = ts->info->auxv_len;
2963 * Auxiliary vector is stored in target process stack. It contains
2964 * {type, value} pairs that we need to dump into note. This is not
2965 * strictly necessary but we do it here for sake of completeness.
2968 /* read in whole auxv vector and copy it to memelfnote */
2969 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2970 if (ptr != NULL) {
2971 fill_note(note, "CORE", NT_AUXV, len, ptr);
2972 unlock_user(ptr, auxv, len);
2977 * Constructs name of coredump file. We have following convention
2978 * for the name:
2979 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2981 * Returns 0 in case of success, -1 otherwise (errno is set).
2983 static int core_dump_filename(const TaskState *ts, char *buf,
2984 size_t bufsize)
2986 char timestamp[64];
2987 char *base_filename = NULL;
2988 struct timeval tv;
2989 struct tm tm;
2991 assert(bufsize >= PATH_MAX);
2993 if (gettimeofday(&tv, NULL) < 0) {
2994 (void) fprintf(stderr, "unable to get current timestamp: %s",
2995 strerror(errno));
2996 return (-1);
2999 base_filename = g_path_get_basename(ts->bprm->filename);
3000 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
3001 localtime_r(&tv.tv_sec, &tm));
3002 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
3003 base_filename, timestamp, (int)getpid());
3004 g_free(base_filename);
3006 return (0);
3009 static int dump_write(int fd, const void *ptr, size_t size)
3011 const char *bufp = (const char *)ptr;
3012 ssize_t bytes_written, bytes_left;
3013 struct rlimit dumpsize;
3014 off_t pos;
3016 bytes_written = 0;
3017 getrlimit(RLIMIT_CORE, &dumpsize);
3018 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
3019 if (errno == ESPIPE) { /* not a seekable stream */
3020 bytes_left = size;
3021 } else {
3022 return pos;
3024 } else {
3025 if (dumpsize.rlim_cur <= pos) {
3026 return -1;
3027 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
3028 bytes_left = size;
3029 } else {
3030 size_t limit_left=dumpsize.rlim_cur - pos;
3031 bytes_left = limit_left >= size ? size : limit_left ;
3036 * In normal conditions, single write(2) should do but
3037 * in case of socket etc. this mechanism is more portable.
3039 do {
3040 bytes_written = write(fd, bufp, bytes_left);
3041 if (bytes_written < 0) {
3042 if (errno == EINTR)
3043 continue;
3044 return (-1);
3045 } else if (bytes_written == 0) { /* eof */
3046 return (-1);
3048 bufp += bytes_written;
3049 bytes_left -= bytes_written;
3050 } while (bytes_left > 0);
3052 return (0);
3055 static int write_note(struct memelfnote *men, int fd)
3057 struct elf_note en;
3059 en.n_namesz = men->namesz;
3060 en.n_type = men->type;
3061 en.n_descsz = men->datasz;
3063 bswap_note(&en);
3065 if (dump_write(fd, &en, sizeof(en)) != 0)
3066 return (-1);
3067 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3068 return (-1);
3069 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
3070 return (-1);
3072 return (0);
3075 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
3077 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3078 TaskState *ts = (TaskState *)cpu->opaque;
3079 struct elf_thread_status *ets;
3081 ets = g_malloc0(sizeof (*ets));
3082 ets->num_notes = 1; /* only prstatus is dumped */
3083 fill_prstatus(&ets->prstatus, ts, 0);
3084 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3085 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
3086 &ets->prstatus);
3088 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
3090 info->notes_size += note_size(&ets->notes[0]);
3093 static void init_note_info(struct elf_note_info *info)
3095 /* Initialize the elf_note_info structure so that it is at
3096 * least safe to call free_note_info() on it. Must be
3097 * called before calling fill_note_info().
3099 memset(info, 0, sizeof (*info));
3100 QTAILQ_INIT(&info->thread_list);
3103 static int fill_note_info(struct elf_note_info *info,
3104 long signr, const CPUArchState *env)
3106 #define NUMNOTES 3
3107 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3108 TaskState *ts = (TaskState *)cpu->opaque;
3109 int i;
3111 info->notes = g_new0(struct memelfnote, NUMNOTES);
3112 if (info->notes == NULL)
3113 return (-ENOMEM);
3114 info->prstatus = g_malloc0(sizeof (*info->prstatus));
3115 if (info->prstatus == NULL)
3116 return (-ENOMEM);
3117 info->psinfo = g_malloc0(sizeof (*info->psinfo));
3118 if (info->prstatus == NULL)
3119 return (-ENOMEM);
3122 * First fill in status (and registers) of current thread
3123 * including process info & aux vector.
3125 fill_prstatus(info->prstatus, ts, signr);
3126 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3127 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
3128 sizeof (*info->prstatus), info->prstatus);
3129 fill_psinfo(info->psinfo, ts);
3130 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
3131 sizeof (*info->psinfo), info->psinfo);
3132 fill_auxv_note(&info->notes[2], ts);
3133 info->numnote = 3;
3135 info->notes_size = 0;
3136 for (i = 0; i < info->numnote; i++)
3137 info->notes_size += note_size(&info->notes[i]);
3139 /* read and fill status of all threads */
3140 cpu_list_lock();
3141 CPU_FOREACH(cpu) {
3142 if (cpu == thread_cpu) {
3143 continue;
3145 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
3147 cpu_list_unlock();
3149 return (0);
3152 static void free_note_info(struct elf_note_info *info)
3154 struct elf_thread_status *ets;
3156 while (!QTAILQ_EMPTY(&info->thread_list)) {
3157 ets = QTAILQ_FIRST(&info->thread_list);
3158 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
3159 g_free(ets);
3162 g_free(info->prstatus);
3163 g_free(info->psinfo);
3164 g_free(info->notes);
3167 static int write_note_info(struct elf_note_info *info, int fd)
3169 struct elf_thread_status *ets;
3170 int i, error = 0;
3172 /* write prstatus, psinfo and auxv for current thread */
3173 for (i = 0; i < info->numnote; i++)
3174 if ((error = write_note(&info->notes[i], fd)) != 0)
3175 return (error);
3177 /* write prstatus for each thread */
3178 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
3179 if ((error = write_note(&ets->notes[0], fd)) != 0)
3180 return (error);
3183 return (0);
3187 * Write out ELF coredump.
3189 * See documentation of ELF object file format in:
3190 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3192 * Coredump format in linux is following:
3194 * 0 +----------------------+ \
3195 * | ELF header | ET_CORE |
3196 * +----------------------+ |
3197 * | ELF program headers | |--- headers
3198 * | - NOTE section | |
3199 * | - PT_LOAD sections | |
3200 * +----------------------+ /
3201 * | NOTEs: |
3202 * | - NT_PRSTATUS |
3203 * | - NT_PRSINFO |
3204 * | - NT_AUXV |
3205 * +----------------------+ <-- aligned to target page
3206 * | Process memory dump |
3207 * : :
3208 * . .
3209 * : :
3210 * | |
3211 * +----------------------+
3213 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3214 * NT_PRSINFO -> struct elf_prpsinfo
3215 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3217 * Format follows System V format as close as possible. Current
3218 * version limitations are as follows:
3219 * - no floating point registers are dumped
3221 * Function returns 0 in case of success, negative errno otherwise.
3223 * TODO: make this work also during runtime: it should be
3224 * possible to force coredump from running process and then
3225 * continue processing. For example qemu could set up SIGUSR2
3226 * handler (provided that target process haven't registered
3227 * handler for that) that does the dump when signal is received.
3229 static int elf_core_dump(int signr, const CPUArchState *env)
3231 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
3232 const TaskState *ts = (const TaskState *)cpu->opaque;
3233 struct vm_area_struct *vma = NULL;
3234 char corefile[PATH_MAX];
3235 struct elf_note_info info;
3236 struct elfhdr elf;
3237 struct elf_phdr phdr;
3238 struct rlimit dumpsize;
3239 struct mm_struct *mm = NULL;
3240 off_t offset = 0, data_offset = 0;
3241 int segs = 0;
3242 int fd = -1;
3244 init_note_info(&info);
3246 errno = 0;
3247 getrlimit(RLIMIT_CORE, &dumpsize);
3248 if (dumpsize.rlim_cur == 0)
3249 return 0;
3251 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
3252 return (-errno);
3254 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3255 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3256 return (-errno);
3259 * Walk through target process memory mappings and
3260 * set up structure containing this information. After
3261 * this point vma_xxx functions can be used.
3263 if ((mm = vma_init()) == NULL)
3264 goto out;
3266 walk_memory_regions(mm, vma_walker);
3267 segs = vma_get_mapping_count(mm);
3270 * Construct valid coredump ELF header. We also
3271 * add one more segment for notes.
3273 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3274 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3275 goto out;
3277 /* fill in the in-memory version of notes */
3278 if (fill_note_info(&info, signr, env) < 0)
3279 goto out;
3281 offset += sizeof (elf); /* elf header */
3282 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3284 /* write out notes program header */
3285 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3287 offset += info.notes_size;
3288 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3289 goto out;
3292 * ELF specification wants data to start at page boundary so
3293 * we align it here.
3295 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3298 * Write program headers for memory regions mapped in
3299 * the target process.
3301 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3302 (void) memset(&phdr, 0, sizeof (phdr));
3304 phdr.p_type = PT_LOAD;
3305 phdr.p_offset = offset;
3306 phdr.p_vaddr = vma->vma_start;
3307 phdr.p_paddr = 0;
3308 phdr.p_filesz = vma_dump_size(vma);
3309 offset += phdr.p_filesz;
3310 phdr.p_memsz = vma->vma_end - vma->vma_start;
3311 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3312 if (vma->vma_flags & PROT_WRITE)
3313 phdr.p_flags |= PF_W;
3314 if (vma->vma_flags & PROT_EXEC)
3315 phdr.p_flags |= PF_X;
3316 phdr.p_align = ELF_EXEC_PAGESIZE;
3318 bswap_phdr(&phdr, 1);
3319 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
3320 goto out;
3325 * Next we write notes just after program headers. No
3326 * alignment needed here.
3328 if (write_note_info(&info, fd) < 0)
3329 goto out;
3331 /* align data to page boundary */
3332 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3333 goto out;
3336 * Finally we can dump process memory into corefile as well.
3338 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3339 abi_ulong addr;
3340 abi_ulong end;
3342 end = vma->vma_start + vma_dump_size(vma);
3344 for (addr = vma->vma_start; addr < end;
3345 addr += TARGET_PAGE_SIZE) {
3346 char page[TARGET_PAGE_SIZE];
3347 int error;
3350 * Read in page from target process memory and
3351 * write it to coredump file.
3353 error = copy_from_user(page, addr, sizeof (page));
3354 if (error != 0) {
3355 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3356 addr);
3357 errno = -error;
3358 goto out;
3360 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3361 goto out;
3365 out:
3366 free_note_info(&info);
3367 if (mm != NULL)
3368 vma_delete(mm);
3369 (void) close(fd);
3371 if (errno != 0)
3372 return (-errno);
3373 return (0);
3375 #endif /* USE_ELF_CORE_DUMP */
3377 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3379 init_thread(regs, infop);