target-ppc: Add POWER8's MMCR2/MMCRS SPRs
[qemu/cris-port.git] / linux-user / elfload.c
blobeb8d3adce3d06520297add3dd60f9143e5a68717
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include <sys/time.h>
3 #include <sys/param.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <sys/mman.h>
11 #include <sys/resource.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
16 #include "qemu.h"
17 #include "disas/disas.h"
19 #ifdef _ARCH_PPC64
20 #undef ARCH_DLINFO
21 #undef ELF_PLATFORM
22 #undef ELF_HWCAP
23 #undef ELF_HWCAP2
24 #undef ELF_CLASS
25 #undef ELF_DATA
26 #undef ELF_ARCH
27 #endif
29 #define ELF_OSABI ELFOSABI_SYSV
31 /* from personality.h */
34 * Flags for bug emulation.
36 * These occupy the top three bytes.
38 enum {
39 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
40 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
41 descriptors (signal handling) */
42 MMAP_PAGE_ZERO = 0x0100000,
43 ADDR_COMPAT_LAYOUT = 0x0200000,
44 READ_IMPLIES_EXEC = 0x0400000,
45 ADDR_LIMIT_32BIT = 0x0800000,
46 SHORT_INODE = 0x1000000,
47 WHOLE_SECONDS = 0x2000000,
48 STICKY_TIMEOUTS = 0x4000000,
49 ADDR_LIMIT_3GB = 0x8000000,
53 * Personality types.
55 * These go in the low byte. Avoid using the top bit, it will
56 * conflict with error returns.
58 enum {
59 PER_LINUX = 0x0000,
60 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
61 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
62 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
63 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
64 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
65 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
66 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
67 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
68 PER_BSD = 0x0006,
69 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
70 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
71 PER_LINUX32 = 0x0008,
72 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
73 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
74 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
75 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
76 PER_RISCOS = 0x000c,
77 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
78 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
79 PER_OSF4 = 0x000f, /* OSF/1 v4 */
80 PER_HPUX = 0x0010,
81 PER_MASK = 0x00ff,
85 * Return the base personality without flags.
87 #define personality(pers) (pers & PER_MASK)
89 /* this flag is uneffective under linux too, should be deleted */
90 #ifndef MAP_DENYWRITE
91 #define MAP_DENYWRITE 0
92 #endif
94 /* should probably go in elf.h */
95 #ifndef ELIBBAD
96 #define ELIBBAD 80
97 #endif
99 #ifdef TARGET_WORDS_BIGENDIAN
100 #define ELF_DATA ELFDATA2MSB
101 #else
102 #define ELF_DATA ELFDATA2LSB
103 #endif
105 #ifdef TARGET_ABI_MIPSN32
106 typedef abi_ullong target_elf_greg_t;
107 #define tswapreg(ptr) tswap64(ptr)
108 #else
109 typedef abi_ulong target_elf_greg_t;
110 #define tswapreg(ptr) tswapal(ptr)
111 #endif
113 #ifdef USE_UID16
114 typedef abi_ushort target_uid_t;
115 typedef abi_ushort target_gid_t;
116 #else
117 typedef abi_uint target_uid_t;
118 typedef abi_uint target_gid_t;
119 #endif
120 typedef abi_int target_pid_t;
122 #ifdef TARGET_I386
124 #define ELF_PLATFORM get_elf_platform()
126 static const char *get_elf_platform(void)
128 static char elf_platform[] = "i386";
129 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
130 if (family > 6)
131 family = 6;
132 if (family >= 3)
133 elf_platform[1] = '0' + family;
134 return elf_platform;
137 #define ELF_HWCAP get_elf_hwcap()
139 static uint32_t get_elf_hwcap(void)
141 X86CPU *cpu = X86_CPU(thread_cpu);
143 return cpu->env.features[FEAT_1_EDX];
146 #ifdef TARGET_X86_64
147 #define ELF_START_MMAP 0x2aaaaab000ULL
148 #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
150 #define ELF_CLASS ELFCLASS64
151 #define ELF_ARCH EM_X86_64
153 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
155 regs->rax = 0;
156 regs->rsp = infop->start_stack;
157 regs->rip = infop->entry;
160 #define ELF_NREG 27
161 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
164 * Note that ELF_NREG should be 29 as there should be place for
165 * TRAPNO and ERR "registers" as well but linux doesn't dump
166 * those.
168 * See linux kernel: arch/x86/include/asm/elf.h
170 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
172 (*regs)[0] = env->regs[15];
173 (*regs)[1] = env->regs[14];
174 (*regs)[2] = env->regs[13];
175 (*regs)[3] = env->regs[12];
176 (*regs)[4] = env->regs[R_EBP];
177 (*regs)[5] = env->regs[R_EBX];
178 (*regs)[6] = env->regs[11];
179 (*regs)[7] = env->regs[10];
180 (*regs)[8] = env->regs[9];
181 (*regs)[9] = env->regs[8];
182 (*regs)[10] = env->regs[R_EAX];
183 (*regs)[11] = env->regs[R_ECX];
184 (*regs)[12] = env->regs[R_EDX];
185 (*regs)[13] = env->regs[R_ESI];
186 (*regs)[14] = env->regs[R_EDI];
187 (*regs)[15] = env->regs[R_EAX]; /* XXX */
188 (*regs)[16] = env->eip;
189 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
190 (*regs)[18] = env->eflags;
191 (*regs)[19] = env->regs[R_ESP];
192 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
193 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
194 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
195 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
196 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
197 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
198 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
201 #else
203 #define ELF_START_MMAP 0x80000000
206 * This is used to ensure we don't load something for the wrong architecture.
208 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
211 * These are used to set parameters in the core dumps.
213 #define ELF_CLASS ELFCLASS32
214 #define ELF_ARCH EM_386
216 static inline void init_thread(struct target_pt_regs *regs,
217 struct image_info *infop)
219 regs->esp = infop->start_stack;
220 regs->eip = infop->entry;
222 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
223 starts %edx contains a pointer to a function which might be
224 registered using `atexit'. This provides a mean for the
225 dynamic linker to call DT_FINI functions for shared libraries
226 that have been loaded before the code runs.
228 A value of 0 tells we have no such handler. */
229 regs->edx = 0;
232 #define ELF_NREG 17
233 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
236 * Note that ELF_NREG should be 19 as there should be place for
237 * TRAPNO and ERR "registers" as well but linux doesn't dump
238 * those.
240 * See linux kernel: arch/x86/include/asm/elf.h
242 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
244 (*regs)[0] = env->regs[R_EBX];
245 (*regs)[1] = env->regs[R_ECX];
246 (*regs)[2] = env->regs[R_EDX];
247 (*regs)[3] = env->regs[R_ESI];
248 (*regs)[4] = env->regs[R_EDI];
249 (*regs)[5] = env->regs[R_EBP];
250 (*regs)[6] = env->regs[R_EAX];
251 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
252 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
253 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
254 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
255 (*regs)[11] = env->regs[R_EAX]; /* XXX */
256 (*regs)[12] = env->eip;
257 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
258 (*regs)[14] = env->eflags;
259 (*regs)[15] = env->regs[R_ESP];
260 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
262 #endif
264 #define USE_ELF_CORE_DUMP
265 #define ELF_EXEC_PAGESIZE 4096
267 #endif
269 #ifdef TARGET_ARM
271 #ifndef TARGET_AARCH64
272 /* 32 bit ARM definitions */
274 #define ELF_START_MMAP 0x80000000
276 #define elf_check_arch(x) ((x) == ELF_MACHINE)
278 #define ELF_ARCH ELF_MACHINE
279 #define ELF_CLASS ELFCLASS32
281 static inline void init_thread(struct target_pt_regs *regs,
282 struct image_info *infop)
284 abi_long stack = infop->start_stack;
285 memset(regs, 0, sizeof(*regs));
287 regs->ARM_cpsr = 0x10;
288 if (infop->entry & 1)
289 regs->ARM_cpsr |= CPSR_T;
290 regs->ARM_pc = infop->entry & 0xfffffffe;
291 regs->ARM_sp = infop->start_stack;
292 /* FIXME - what to for failure of get_user()? */
293 get_user_ual(regs->ARM_r2, stack + 8); /* envp */
294 get_user_ual(regs->ARM_r1, stack + 4); /* envp */
295 /* XXX: it seems that r0 is zeroed after ! */
296 regs->ARM_r0 = 0;
297 /* For uClinux PIC binaries. */
298 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
299 regs->ARM_r10 = infop->start_data;
302 #define ELF_NREG 18
303 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
305 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
307 (*regs)[0] = tswapreg(env->regs[0]);
308 (*regs)[1] = tswapreg(env->regs[1]);
309 (*regs)[2] = tswapreg(env->regs[2]);
310 (*regs)[3] = tswapreg(env->regs[3]);
311 (*regs)[4] = tswapreg(env->regs[4]);
312 (*regs)[5] = tswapreg(env->regs[5]);
313 (*regs)[6] = tswapreg(env->regs[6]);
314 (*regs)[7] = tswapreg(env->regs[7]);
315 (*regs)[8] = tswapreg(env->regs[8]);
316 (*regs)[9] = tswapreg(env->regs[9]);
317 (*regs)[10] = tswapreg(env->regs[10]);
318 (*regs)[11] = tswapreg(env->regs[11]);
319 (*regs)[12] = tswapreg(env->regs[12]);
320 (*regs)[13] = tswapreg(env->regs[13]);
321 (*regs)[14] = tswapreg(env->regs[14]);
322 (*regs)[15] = tswapreg(env->regs[15]);
324 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
325 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
328 #define USE_ELF_CORE_DUMP
329 #define ELF_EXEC_PAGESIZE 4096
331 enum
333 ARM_HWCAP_ARM_SWP = 1 << 0,
334 ARM_HWCAP_ARM_HALF = 1 << 1,
335 ARM_HWCAP_ARM_THUMB = 1 << 2,
336 ARM_HWCAP_ARM_26BIT = 1 << 3,
337 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
338 ARM_HWCAP_ARM_FPA = 1 << 5,
339 ARM_HWCAP_ARM_VFP = 1 << 6,
340 ARM_HWCAP_ARM_EDSP = 1 << 7,
341 ARM_HWCAP_ARM_JAVA = 1 << 8,
342 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
343 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
344 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
345 ARM_HWCAP_ARM_NEON = 1 << 12,
346 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
347 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
348 ARM_HWCAP_ARM_TLS = 1 << 15,
349 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
350 ARM_HWCAP_ARM_IDIVA = 1 << 17,
351 ARM_HWCAP_ARM_IDIVT = 1 << 18,
352 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
353 ARM_HWCAP_ARM_LPAE = 1 << 20,
354 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
357 enum {
358 ARM_HWCAP2_ARM_AES = 1 << 0,
359 ARM_HWCAP2_ARM_PMULL = 1 << 1,
360 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
361 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
362 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
365 /* The commpage only exists for 32 bit kernels */
367 #define TARGET_HAS_VALIDATE_GUEST_SPACE
368 /* Return 1 if the proposed guest space is suitable for the guest.
369 * Return 0 if the proposed guest space isn't suitable, but another
370 * address space should be tried.
371 * Return -1 if there is no way the proposed guest space can be
372 * valid regardless of the base.
373 * The guest code may leave a page mapped and populate it if the
374 * address is suitable.
376 static int validate_guest_space(unsigned long guest_base,
377 unsigned long guest_size)
379 unsigned long real_start, test_page_addr;
381 /* We need to check that we can force a fault on access to the
382 * commpage at 0xffff0fxx
384 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
386 /* If the commpage lies within the already allocated guest space,
387 * then there is no way we can allocate it.
389 if (test_page_addr >= guest_base
390 && test_page_addr <= (guest_base + guest_size)) {
391 return -1;
394 /* Note it needs to be writeable to let us initialise it */
395 real_start = (unsigned long)
396 mmap((void *)test_page_addr, qemu_host_page_size,
397 PROT_READ | PROT_WRITE,
398 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
400 /* If we can't map it then try another address */
401 if (real_start == -1ul) {
402 return 0;
405 if (real_start != test_page_addr) {
406 /* OS didn't put the page where we asked - unmap and reject */
407 munmap((void *)real_start, qemu_host_page_size);
408 return 0;
411 /* Leave the page mapped
412 * Populate it (mmap should have left it all 0'd)
415 /* Kernel helper versions */
416 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
418 /* Now it's populated make it RO */
419 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
420 perror("Protecting guest commpage");
421 exit(-1);
424 return 1; /* All good */
427 #define ELF_HWCAP get_elf_hwcap()
428 #define ELF_HWCAP2 get_elf_hwcap2()
430 static uint32_t get_elf_hwcap(void)
432 ARMCPU *cpu = ARM_CPU(thread_cpu);
433 uint32_t hwcaps = 0;
435 hwcaps |= ARM_HWCAP_ARM_SWP;
436 hwcaps |= ARM_HWCAP_ARM_HALF;
437 hwcaps |= ARM_HWCAP_ARM_THUMB;
438 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
440 /* probe for the extra features */
441 #define GET_FEATURE(feat, hwcap) \
442 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
443 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
444 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
445 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
446 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
447 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
448 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
449 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
450 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
451 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
452 GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA);
453 GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT);
454 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
455 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
456 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
457 * to our VFP_FP16 feature bit.
459 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
460 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
462 return hwcaps;
465 static uint32_t get_elf_hwcap2(void)
467 ARMCPU *cpu = ARM_CPU(thread_cpu);
468 uint32_t hwcaps = 0;
470 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP2_ARM_AES);
471 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP2_ARM_PMULL);
472 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP2_ARM_SHA1);
473 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP2_ARM_SHA2);
474 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP2_ARM_CRC32);
475 return hwcaps;
478 #undef GET_FEATURE
480 #else
481 /* 64 bit ARM definitions */
482 #define ELF_START_MMAP 0x80000000
484 #define elf_check_arch(x) ((x) == ELF_MACHINE)
486 #define ELF_ARCH ELF_MACHINE
487 #define ELF_CLASS ELFCLASS64
488 #define ELF_PLATFORM "aarch64"
490 static inline void init_thread(struct target_pt_regs *regs,
491 struct image_info *infop)
493 abi_long stack = infop->start_stack;
494 memset(regs, 0, sizeof(*regs));
496 regs->pc = infop->entry & ~0x3ULL;
497 regs->sp = stack;
500 #define ELF_NREG 34
501 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
503 static void elf_core_copy_regs(target_elf_gregset_t *regs,
504 const CPUARMState *env)
506 int i;
508 for (i = 0; i < 32; i++) {
509 (*regs)[i] = tswapreg(env->xregs[i]);
511 (*regs)[32] = tswapreg(env->pc);
512 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
515 #define USE_ELF_CORE_DUMP
516 #define ELF_EXEC_PAGESIZE 4096
518 enum {
519 ARM_HWCAP_A64_FP = 1 << 0,
520 ARM_HWCAP_A64_ASIMD = 1 << 1,
521 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
522 ARM_HWCAP_A64_AES = 1 << 3,
523 ARM_HWCAP_A64_PMULL = 1 << 4,
524 ARM_HWCAP_A64_SHA1 = 1 << 5,
525 ARM_HWCAP_A64_SHA2 = 1 << 6,
526 ARM_HWCAP_A64_CRC32 = 1 << 7,
529 #define ELF_HWCAP get_elf_hwcap()
531 static uint32_t get_elf_hwcap(void)
533 ARMCPU *cpu = ARM_CPU(thread_cpu);
534 uint32_t hwcaps = 0;
536 hwcaps |= ARM_HWCAP_A64_FP;
537 hwcaps |= ARM_HWCAP_A64_ASIMD;
539 /* probe for the extra features */
540 #define GET_FEATURE(feat, hwcap) \
541 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
542 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP_A64_AES);
543 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP_A64_PMULL);
544 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP_A64_SHA1);
545 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP_A64_SHA2);
546 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP_A64_CRC32);
547 #undef GET_FEATURE
549 return hwcaps;
552 #endif /* not TARGET_AARCH64 */
553 #endif /* TARGET_ARM */
555 #ifdef TARGET_UNICORE32
557 #define ELF_START_MMAP 0x80000000
559 #define elf_check_arch(x) ((x) == EM_UNICORE32)
561 #define ELF_CLASS ELFCLASS32
562 #define ELF_DATA ELFDATA2LSB
563 #define ELF_ARCH EM_UNICORE32
565 static inline void init_thread(struct target_pt_regs *regs,
566 struct image_info *infop)
568 abi_long stack = infop->start_stack;
569 memset(regs, 0, sizeof(*regs));
570 regs->UC32_REG_asr = 0x10;
571 regs->UC32_REG_pc = infop->entry & 0xfffffffe;
572 regs->UC32_REG_sp = infop->start_stack;
573 /* FIXME - what to for failure of get_user()? */
574 get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */
575 get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */
576 /* XXX: it seems that r0 is zeroed after ! */
577 regs->UC32_REG_00 = 0;
580 #define ELF_NREG 34
581 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
583 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
585 (*regs)[0] = env->regs[0];
586 (*regs)[1] = env->regs[1];
587 (*regs)[2] = env->regs[2];
588 (*regs)[3] = env->regs[3];
589 (*regs)[4] = env->regs[4];
590 (*regs)[5] = env->regs[5];
591 (*regs)[6] = env->regs[6];
592 (*regs)[7] = env->regs[7];
593 (*regs)[8] = env->regs[8];
594 (*regs)[9] = env->regs[9];
595 (*regs)[10] = env->regs[10];
596 (*regs)[11] = env->regs[11];
597 (*regs)[12] = env->regs[12];
598 (*regs)[13] = env->regs[13];
599 (*regs)[14] = env->regs[14];
600 (*regs)[15] = env->regs[15];
601 (*regs)[16] = env->regs[16];
602 (*regs)[17] = env->regs[17];
603 (*regs)[18] = env->regs[18];
604 (*regs)[19] = env->regs[19];
605 (*regs)[20] = env->regs[20];
606 (*regs)[21] = env->regs[21];
607 (*regs)[22] = env->regs[22];
608 (*regs)[23] = env->regs[23];
609 (*regs)[24] = env->regs[24];
610 (*regs)[25] = env->regs[25];
611 (*regs)[26] = env->regs[26];
612 (*regs)[27] = env->regs[27];
613 (*regs)[28] = env->regs[28];
614 (*regs)[29] = env->regs[29];
615 (*regs)[30] = env->regs[30];
616 (*regs)[31] = env->regs[31];
618 (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
619 (*regs)[33] = env->regs[0]; /* XXX */
622 #define USE_ELF_CORE_DUMP
623 #define ELF_EXEC_PAGESIZE 4096
625 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
627 #endif
629 #ifdef TARGET_SPARC
630 #ifdef TARGET_SPARC64
632 #define ELF_START_MMAP 0x80000000
633 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
634 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
635 #ifndef TARGET_ABI32
636 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
637 #else
638 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
639 #endif
641 #define ELF_CLASS ELFCLASS64
642 #define ELF_ARCH EM_SPARCV9
644 #define STACK_BIAS 2047
646 static inline void init_thread(struct target_pt_regs *regs,
647 struct image_info *infop)
649 #ifndef TARGET_ABI32
650 regs->tstate = 0;
651 #endif
652 regs->pc = infop->entry;
653 regs->npc = regs->pc + 4;
654 regs->y = 0;
655 #ifdef TARGET_ABI32
656 regs->u_regs[14] = infop->start_stack - 16 * 4;
657 #else
658 if (personality(infop->personality) == PER_LINUX32)
659 regs->u_regs[14] = infop->start_stack - 16 * 4;
660 else
661 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
662 #endif
665 #else
666 #define ELF_START_MMAP 0x80000000
667 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
668 | HWCAP_SPARC_MULDIV)
669 #define elf_check_arch(x) ( (x) == EM_SPARC )
671 #define ELF_CLASS ELFCLASS32
672 #define ELF_ARCH EM_SPARC
674 static inline void init_thread(struct target_pt_regs *regs,
675 struct image_info *infop)
677 regs->psr = 0;
678 regs->pc = infop->entry;
679 regs->npc = regs->pc + 4;
680 regs->y = 0;
681 regs->u_regs[14] = infop->start_stack - 16 * 4;
684 #endif
685 #endif
687 #ifdef TARGET_PPC
689 #define ELF_START_MMAP 0x80000000
691 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
693 #define elf_check_arch(x) ( (x) == EM_PPC64 )
695 #define ELF_CLASS ELFCLASS64
697 #else
699 #define elf_check_arch(x) ( (x) == EM_PPC )
701 #define ELF_CLASS ELFCLASS32
703 #endif
705 #define ELF_ARCH EM_PPC
707 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
708 See arch/powerpc/include/asm/cputable.h. */
709 enum {
710 QEMU_PPC_FEATURE_32 = 0x80000000,
711 QEMU_PPC_FEATURE_64 = 0x40000000,
712 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
713 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
714 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
715 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
716 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
717 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
718 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
719 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
720 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
721 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
722 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
723 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
724 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
725 QEMU_PPC_FEATURE_CELL = 0x00010000,
726 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
727 QEMU_PPC_FEATURE_SMT = 0x00004000,
728 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
729 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
730 QEMU_PPC_FEATURE_PA6T = 0x00000800,
731 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
732 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
733 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
734 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
735 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
737 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
738 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
741 #define ELF_HWCAP get_elf_hwcap()
743 static uint32_t get_elf_hwcap(void)
745 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
746 uint32_t features = 0;
748 /* We don't have to be terribly complete here; the high points are
749 Altivec/FP/SPE support. Anything else is just a bonus. */
750 #define GET_FEATURE(flag, feature) \
751 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
752 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
753 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
754 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
755 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
756 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
757 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
758 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
759 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
760 #undef GET_FEATURE
762 return features;
766 * The requirements here are:
767 * - keep the final alignment of sp (sp & 0xf)
768 * - make sure the 32-bit value at the first 16 byte aligned position of
769 * AUXV is greater than 16 for glibc compatibility.
770 * AT_IGNOREPPC is used for that.
771 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
772 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
774 #define DLINFO_ARCH_ITEMS 5
775 #define ARCH_DLINFO \
776 do { \
777 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
778 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
779 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
780 /* \
781 * Now handle glibc compatibility. \
782 */ \
783 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
784 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
785 } while (0)
787 static inline uint32_t get_ppc64_abi(struct image_info *infop);
789 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
791 _regs->gpr[1] = infop->start_stack;
792 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
793 if (get_ppc64_abi(infop) < 2) {
794 _regs->gpr[2] = ldq_raw(infop->entry + 8) + infop->load_bias;
795 infop->entry = ldq_raw(infop->entry) + infop->load_bias;
796 } else {
797 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
799 #endif
800 _regs->nip = infop->entry;
803 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
804 #define ELF_NREG 48
805 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
807 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
809 int i;
810 target_ulong ccr = 0;
812 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
813 (*regs)[i] = tswapreg(env->gpr[i]);
816 (*regs)[32] = tswapreg(env->nip);
817 (*regs)[33] = tswapreg(env->msr);
818 (*regs)[35] = tswapreg(env->ctr);
819 (*regs)[36] = tswapreg(env->lr);
820 (*regs)[37] = tswapreg(env->xer);
822 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
823 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
825 (*regs)[38] = tswapreg(ccr);
828 #define USE_ELF_CORE_DUMP
829 #define ELF_EXEC_PAGESIZE 4096
831 #endif
833 #ifdef TARGET_MIPS
835 #define ELF_START_MMAP 0x80000000
837 #define elf_check_arch(x) ( (x) == EM_MIPS )
839 #ifdef TARGET_MIPS64
840 #define ELF_CLASS ELFCLASS64
841 #else
842 #define ELF_CLASS ELFCLASS32
843 #endif
844 #define ELF_ARCH EM_MIPS
846 static inline void init_thread(struct target_pt_regs *regs,
847 struct image_info *infop)
849 regs->cp0_status = 2 << CP0St_KSU;
850 regs->cp0_epc = infop->entry;
851 regs->regs[29] = infop->start_stack;
854 /* See linux kernel: arch/mips/include/asm/elf.h. */
855 #define ELF_NREG 45
856 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
858 /* See linux kernel: arch/mips/include/asm/reg.h. */
859 enum {
860 #ifdef TARGET_MIPS64
861 TARGET_EF_R0 = 0,
862 #else
863 TARGET_EF_R0 = 6,
864 #endif
865 TARGET_EF_R26 = TARGET_EF_R0 + 26,
866 TARGET_EF_R27 = TARGET_EF_R0 + 27,
867 TARGET_EF_LO = TARGET_EF_R0 + 32,
868 TARGET_EF_HI = TARGET_EF_R0 + 33,
869 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
870 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
871 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
872 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
875 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
876 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
878 int i;
880 for (i = 0; i < TARGET_EF_R0; i++) {
881 (*regs)[i] = 0;
883 (*regs)[TARGET_EF_R0] = 0;
885 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
886 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
889 (*regs)[TARGET_EF_R26] = 0;
890 (*regs)[TARGET_EF_R27] = 0;
891 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
892 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
893 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
894 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
895 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
896 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
899 #define USE_ELF_CORE_DUMP
900 #define ELF_EXEC_PAGESIZE 4096
902 #endif /* TARGET_MIPS */
904 #ifdef TARGET_MICROBLAZE
906 #define ELF_START_MMAP 0x80000000
908 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
910 #define ELF_CLASS ELFCLASS32
911 #define ELF_ARCH EM_MICROBLAZE
913 static inline void init_thread(struct target_pt_regs *regs,
914 struct image_info *infop)
916 regs->pc = infop->entry;
917 regs->r1 = infop->start_stack;
921 #define ELF_EXEC_PAGESIZE 4096
923 #define USE_ELF_CORE_DUMP
924 #define ELF_NREG 38
925 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
927 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
928 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
930 int i, pos = 0;
932 for (i = 0; i < 32; i++) {
933 (*regs)[pos++] = tswapreg(env->regs[i]);
936 for (i = 0; i < 6; i++) {
937 (*regs)[pos++] = tswapreg(env->sregs[i]);
941 #endif /* TARGET_MICROBLAZE */
943 #ifdef TARGET_OPENRISC
945 #define ELF_START_MMAP 0x08000000
947 #define elf_check_arch(x) ((x) == EM_OPENRISC)
949 #define ELF_ARCH EM_OPENRISC
950 #define ELF_CLASS ELFCLASS32
951 #define ELF_DATA ELFDATA2MSB
953 static inline void init_thread(struct target_pt_regs *regs,
954 struct image_info *infop)
956 regs->pc = infop->entry;
957 regs->gpr[1] = infop->start_stack;
960 #define USE_ELF_CORE_DUMP
961 #define ELF_EXEC_PAGESIZE 8192
963 /* See linux kernel arch/openrisc/include/asm/elf.h. */
964 #define ELF_NREG 34 /* gprs and pc, sr */
965 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
967 static void elf_core_copy_regs(target_elf_gregset_t *regs,
968 const CPUOpenRISCState *env)
970 int i;
972 for (i = 0; i < 32; i++) {
973 (*regs)[i] = tswapreg(env->gpr[i]);
976 (*regs)[32] = tswapreg(env->pc);
977 (*regs)[33] = tswapreg(env->sr);
979 #define ELF_HWCAP 0
980 #define ELF_PLATFORM NULL
982 #endif /* TARGET_OPENRISC */
984 #ifdef TARGET_SH4
986 #define ELF_START_MMAP 0x80000000
988 #define elf_check_arch(x) ( (x) == EM_SH )
990 #define ELF_CLASS ELFCLASS32
991 #define ELF_ARCH EM_SH
993 static inline void init_thread(struct target_pt_regs *regs,
994 struct image_info *infop)
996 /* Check other registers XXXXX */
997 regs->pc = infop->entry;
998 regs->regs[15] = infop->start_stack;
1001 /* See linux kernel: arch/sh/include/asm/elf.h. */
1002 #define ELF_NREG 23
1003 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1005 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1006 enum {
1007 TARGET_REG_PC = 16,
1008 TARGET_REG_PR = 17,
1009 TARGET_REG_SR = 18,
1010 TARGET_REG_GBR = 19,
1011 TARGET_REG_MACH = 20,
1012 TARGET_REG_MACL = 21,
1013 TARGET_REG_SYSCALL = 22
1016 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1017 const CPUSH4State *env)
1019 int i;
1021 for (i = 0; i < 16; i++) {
1022 (*regs[i]) = tswapreg(env->gregs[i]);
1025 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1026 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1027 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1028 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1029 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1030 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1031 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1034 #define USE_ELF_CORE_DUMP
1035 #define ELF_EXEC_PAGESIZE 4096
1037 #endif
1039 #ifdef TARGET_CRIS
1041 #define ELF_START_MMAP 0x80000000
1043 #define elf_check_arch(x) ( (x) == EM_CRIS )
1045 #define ELF_CLASS ELFCLASS32
1046 #define ELF_ARCH EM_CRIS
1048 static inline void init_thread(struct target_pt_regs *regs,
1049 struct image_info *infop)
1051 regs->erp = infop->entry;
1054 #define ELF_EXEC_PAGESIZE 8192
1056 #endif
1058 #ifdef TARGET_M68K
1060 #define ELF_START_MMAP 0x80000000
1062 #define elf_check_arch(x) ( (x) == EM_68K )
1064 #define ELF_CLASS ELFCLASS32
1065 #define ELF_ARCH EM_68K
1067 /* ??? Does this need to do anything?
1068 #define ELF_PLAT_INIT(_r) */
1070 static inline void init_thread(struct target_pt_regs *regs,
1071 struct image_info *infop)
1073 regs->usp = infop->start_stack;
1074 regs->sr = 0;
1075 regs->pc = infop->entry;
1078 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1079 #define ELF_NREG 20
1080 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1082 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1084 (*regs)[0] = tswapreg(env->dregs[1]);
1085 (*regs)[1] = tswapreg(env->dregs[2]);
1086 (*regs)[2] = tswapreg(env->dregs[3]);
1087 (*regs)[3] = tswapreg(env->dregs[4]);
1088 (*regs)[4] = tswapreg(env->dregs[5]);
1089 (*regs)[5] = tswapreg(env->dregs[6]);
1090 (*regs)[6] = tswapreg(env->dregs[7]);
1091 (*regs)[7] = tswapreg(env->aregs[0]);
1092 (*regs)[8] = tswapreg(env->aregs[1]);
1093 (*regs)[9] = tswapreg(env->aregs[2]);
1094 (*regs)[10] = tswapreg(env->aregs[3]);
1095 (*regs)[11] = tswapreg(env->aregs[4]);
1096 (*regs)[12] = tswapreg(env->aregs[5]);
1097 (*regs)[13] = tswapreg(env->aregs[6]);
1098 (*regs)[14] = tswapreg(env->dregs[0]);
1099 (*regs)[15] = tswapreg(env->aregs[7]);
1100 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1101 (*regs)[17] = tswapreg(env->sr);
1102 (*regs)[18] = tswapreg(env->pc);
1103 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1106 #define USE_ELF_CORE_DUMP
1107 #define ELF_EXEC_PAGESIZE 8192
1109 #endif
1111 #ifdef TARGET_ALPHA
1113 #define ELF_START_MMAP (0x30000000000ULL)
1115 #define elf_check_arch(x) ( (x) == ELF_ARCH )
1117 #define ELF_CLASS ELFCLASS64
1118 #define ELF_ARCH EM_ALPHA
1120 static inline void init_thread(struct target_pt_regs *regs,
1121 struct image_info *infop)
1123 regs->pc = infop->entry;
1124 regs->ps = 8;
1125 regs->usp = infop->start_stack;
1128 #define ELF_EXEC_PAGESIZE 8192
1130 #endif /* TARGET_ALPHA */
1132 #ifdef TARGET_S390X
1134 #define ELF_START_MMAP (0x20000000000ULL)
1136 #define elf_check_arch(x) ( (x) == ELF_ARCH )
1138 #define ELF_CLASS ELFCLASS64
1139 #define ELF_DATA ELFDATA2MSB
1140 #define ELF_ARCH EM_S390
1142 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1144 regs->psw.addr = infop->entry;
1145 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1146 regs->gprs[15] = infop->start_stack;
1149 #endif /* TARGET_S390X */
1151 #ifndef ELF_PLATFORM
1152 #define ELF_PLATFORM (NULL)
1153 #endif
1155 #ifndef ELF_HWCAP
1156 #define ELF_HWCAP 0
1157 #endif
1159 #ifdef TARGET_ABI32
1160 #undef ELF_CLASS
1161 #define ELF_CLASS ELFCLASS32
1162 #undef bswaptls
1163 #define bswaptls(ptr) bswap32s(ptr)
1164 #endif
1166 #include "elf.h"
1168 #ifdef TARGET_PPC
1169 static inline uint32_t get_ppc64_abi(struct image_info *infop)
1171 return infop->elf_flags & EF_PPC64_ABI;
1173 #endif
1175 struct exec
1177 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1178 unsigned int a_text; /* length of text, in bytes */
1179 unsigned int a_data; /* length of data, in bytes */
1180 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1181 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1182 unsigned int a_entry; /* start address */
1183 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1184 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1188 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1189 #define OMAGIC 0407
1190 #define NMAGIC 0410
1191 #define ZMAGIC 0413
1192 #define QMAGIC 0314
1194 /* Necessary parameters */
1195 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1196 #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
1197 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1199 #define DLINFO_ITEMS 14
1201 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1203 memcpy(to, from, n);
1206 #ifdef BSWAP_NEEDED
1207 static void bswap_ehdr(struct elfhdr *ehdr)
1209 bswap16s(&ehdr->e_type); /* Object file type */
1210 bswap16s(&ehdr->e_machine); /* Architecture */
1211 bswap32s(&ehdr->e_version); /* Object file version */
1212 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1213 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1214 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1215 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1216 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1217 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1218 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1219 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1220 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1221 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1224 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1226 int i;
1227 for (i = 0; i < phnum; ++i, ++phdr) {
1228 bswap32s(&phdr->p_type); /* Segment type */
1229 bswap32s(&phdr->p_flags); /* Segment flags */
1230 bswaptls(&phdr->p_offset); /* Segment file offset */
1231 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1232 bswaptls(&phdr->p_paddr); /* Segment physical address */
1233 bswaptls(&phdr->p_filesz); /* Segment size in file */
1234 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1235 bswaptls(&phdr->p_align); /* Segment alignment */
1239 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1241 int i;
1242 for (i = 0; i < shnum; ++i, ++shdr) {
1243 bswap32s(&shdr->sh_name);
1244 bswap32s(&shdr->sh_type);
1245 bswaptls(&shdr->sh_flags);
1246 bswaptls(&shdr->sh_addr);
1247 bswaptls(&shdr->sh_offset);
1248 bswaptls(&shdr->sh_size);
1249 bswap32s(&shdr->sh_link);
1250 bswap32s(&shdr->sh_info);
1251 bswaptls(&shdr->sh_addralign);
1252 bswaptls(&shdr->sh_entsize);
1256 static void bswap_sym(struct elf_sym *sym)
1258 bswap32s(&sym->st_name);
1259 bswaptls(&sym->st_value);
1260 bswaptls(&sym->st_size);
1261 bswap16s(&sym->st_shndx);
1263 #else
1264 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1265 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1266 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1267 static inline void bswap_sym(struct elf_sym *sym) { }
1268 #endif
1270 #ifdef USE_ELF_CORE_DUMP
1271 static int elf_core_dump(int, const CPUArchState *);
1272 #endif /* USE_ELF_CORE_DUMP */
1273 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1275 /* Verify the portions of EHDR within E_IDENT for the target.
1276 This can be performed before bswapping the entire header. */
1277 static bool elf_check_ident(struct elfhdr *ehdr)
1279 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1280 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1281 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1282 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1283 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1284 && ehdr->e_ident[EI_DATA] == ELF_DATA
1285 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1288 /* Verify the portions of EHDR outside of E_IDENT for the target.
1289 This has to wait until after bswapping the header. */
1290 static bool elf_check_ehdr(struct elfhdr *ehdr)
1292 return (elf_check_arch(ehdr->e_machine)
1293 && ehdr->e_ehsize == sizeof(struct elfhdr)
1294 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1295 && ehdr->e_shentsize == sizeof(struct elf_shdr)
1296 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1300 * 'copy_elf_strings()' copies argument/envelope strings from user
1301 * memory to free pages in kernel mem. These are in a format ready
1302 * to be put directly into the top of new user memory.
1305 static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
1306 abi_ulong p)
1308 char *tmp, *tmp1, *pag = NULL;
1309 int len, offset = 0;
1311 if (!p) {
1312 return 0; /* bullet-proofing */
1314 while (argc-- > 0) {
1315 tmp = argv[argc];
1316 if (!tmp) {
1317 fprintf(stderr, "VFS: argc is wrong");
1318 exit(-1);
1320 tmp1 = tmp;
1321 while (*tmp++);
1322 len = tmp - tmp1;
1323 if (p < len) { /* this shouldn't happen - 128kB */
1324 return 0;
1326 while (len) {
1327 --p; --tmp; --len;
1328 if (--offset < 0) {
1329 offset = p % TARGET_PAGE_SIZE;
1330 pag = (char *)page[p/TARGET_PAGE_SIZE];
1331 if (!pag) {
1332 pag = g_try_malloc0(TARGET_PAGE_SIZE);
1333 page[p/TARGET_PAGE_SIZE] = pag;
1334 if (!pag)
1335 return 0;
1338 if (len == 0 || offset == 0) {
1339 *(pag + offset) = *tmp;
1341 else {
1342 int bytes_to_copy = (len > offset) ? offset : len;
1343 tmp -= bytes_to_copy;
1344 p -= bytes_to_copy;
1345 offset -= bytes_to_copy;
1346 len -= bytes_to_copy;
1347 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
1351 return p;
1354 static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
1355 struct image_info *info)
1357 abi_ulong stack_base, size, error, guard;
1358 int i;
1360 /* Create enough stack to hold everything. If we don't use
1361 it for args, we'll use it for something else. */
1362 size = guest_stack_size;
1363 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) {
1364 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1366 guard = TARGET_PAGE_SIZE;
1367 if (guard < qemu_real_host_page_size) {
1368 guard = qemu_real_host_page_size;
1371 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1372 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1373 if (error == -1) {
1374 perror("mmap stack");
1375 exit(-1);
1378 /* We reserve one extra page at the top of the stack as guard. */
1379 target_mprotect(error, guard, PROT_NONE);
1381 info->stack_limit = error + guard;
1382 stack_base = info->stack_limit + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1383 p += stack_base;
1385 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1386 if (bprm->page[i]) {
1387 info->rss++;
1388 /* FIXME - check return value of memcpy_to_target() for failure */
1389 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
1390 g_free(bprm->page[i]);
1392 stack_base += TARGET_PAGE_SIZE;
1394 return p;
1397 /* Map and zero the bss. We need to explicitly zero any fractional pages
1398 after the data section (i.e. bss). */
1399 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1401 uintptr_t host_start, host_map_start, host_end;
1403 last_bss = TARGET_PAGE_ALIGN(last_bss);
1405 /* ??? There is confusion between qemu_real_host_page_size and
1406 qemu_host_page_size here and elsewhere in target_mmap, which
1407 may lead to the end of the data section mapping from the file
1408 not being mapped. At least there was an explicit test and
1409 comment for that here, suggesting that "the file size must
1410 be known". The comment probably pre-dates the introduction
1411 of the fstat system call in target_mmap which does in fact
1412 find out the size. What isn't clear is if the workaround
1413 here is still actually needed. For now, continue with it,
1414 but merge it with the "normal" mmap that would allocate the bss. */
1416 host_start = (uintptr_t) g2h(elf_bss);
1417 host_end = (uintptr_t) g2h(last_bss);
1418 host_map_start = (host_start + qemu_real_host_page_size - 1);
1419 host_map_start &= -qemu_real_host_page_size;
1421 if (host_map_start < host_end) {
1422 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1423 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1424 if (p == MAP_FAILED) {
1425 perror("cannot mmap brk");
1426 exit(-1);
1430 /* Ensure that the bss page(s) are valid */
1431 if ((page_get_flags(last_bss-1) & prot) != prot) {
1432 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1435 if (host_start < host_map_start) {
1436 memset((void *)host_start, 0, host_map_start - host_start);
1440 #ifdef CONFIG_USE_FDPIC
1441 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1443 uint16_t n;
1444 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1446 /* elf32_fdpic_loadseg */
1447 n = info->nsegs;
1448 while (n--) {
1449 sp -= 12;
1450 put_user_u32(loadsegs[n].addr, sp+0);
1451 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1452 put_user_u32(loadsegs[n].p_memsz, sp+8);
1455 /* elf32_fdpic_loadmap */
1456 sp -= 4;
1457 put_user_u16(0, sp+0); /* version */
1458 put_user_u16(info->nsegs, sp+2); /* nsegs */
1460 info->personality = PER_LINUX_FDPIC;
1461 info->loadmap_addr = sp;
1463 return sp;
1465 #endif
1467 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1468 struct elfhdr *exec,
1469 struct image_info *info,
1470 struct image_info *interp_info)
1472 abi_ulong sp;
1473 abi_ulong sp_auxv;
1474 int size;
1475 int i;
1476 abi_ulong u_rand_bytes;
1477 uint8_t k_rand_bytes[16];
1478 abi_ulong u_platform;
1479 const char *k_platform;
1480 const int n = sizeof(elf_addr_t);
1482 sp = p;
1484 #ifdef CONFIG_USE_FDPIC
1485 /* Needs to be before we load the env/argc/... */
1486 if (elf_is_fdpic(exec)) {
1487 /* Need 4 byte alignment for these structs */
1488 sp &= ~3;
1489 sp = loader_build_fdpic_loadmap(info, sp);
1490 info->other_info = interp_info;
1491 if (interp_info) {
1492 interp_info->other_info = info;
1493 sp = loader_build_fdpic_loadmap(interp_info, sp);
1496 #endif
1498 u_platform = 0;
1499 k_platform = ELF_PLATFORM;
1500 if (k_platform) {
1501 size_t len = strlen(k_platform) + 1;
1502 sp -= (len + n - 1) & ~(n - 1);
1503 u_platform = sp;
1504 /* FIXME - check return value of memcpy_to_target() for failure */
1505 memcpy_to_target(sp, k_platform, len);
1509 * Generate 16 random bytes for userspace PRNG seeding (not
1510 * cryptically secure but it's not the aim of QEMU).
1512 srand((unsigned int) time(NULL));
1513 for (i = 0; i < 16; i++) {
1514 k_rand_bytes[i] = rand();
1516 sp -= 16;
1517 u_rand_bytes = sp;
1518 /* FIXME - check return value of memcpy_to_target() for failure */
1519 memcpy_to_target(sp, k_rand_bytes, 16);
1522 * Force 16 byte _final_ alignment here for generality.
1524 sp = sp &~ (abi_ulong)15;
1525 size = (DLINFO_ITEMS + 1) * 2;
1526 if (k_platform)
1527 size += 2;
1528 #ifdef DLINFO_ARCH_ITEMS
1529 size += DLINFO_ARCH_ITEMS * 2;
1530 #endif
1531 #ifdef ELF_HWCAP2
1532 size += 2;
1533 #endif
1534 size += envc + argc + 2;
1535 size += 1; /* argc itself */
1536 size *= n;
1537 if (size & 15)
1538 sp -= 16 - (size & 15);
1540 /* This is correct because Linux defines
1541 * elf_addr_t as Elf32_Off / Elf64_Off
1543 #define NEW_AUX_ENT(id, val) do { \
1544 sp -= n; put_user_ual(val, sp); \
1545 sp -= n; put_user_ual(id, sp); \
1546 } while(0)
1548 sp_auxv = sp;
1549 NEW_AUX_ENT (AT_NULL, 0);
1551 /* There must be exactly DLINFO_ITEMS entries here. */
1552 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1553 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1554 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1555 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
1556 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1557 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1558 NEW_AUX_ENT(AT_ENTRY, info->entry);
1559 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1560 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1561 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1562 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1563 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1564 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1565 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1567 #ifdef ELF_HWCAP2
1568 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1569 #endif
1571 if (k_platform)
1572 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1573 #ifdef ARCH_DLINFO
1575 * ARCH_DLINFO must come last so platform specific code can enforce
1576 * special alignment requirements on the AUXV if necessary (eg. PPC).
1578 ARCH_DLINFO;
1579 #endif
1580 #undef NEW_AUX_ENT
1582 info->saved_auxv = sp;
1583 info->auxv_len = sp_auxv - sp;
1585 sp = loader_build_argptr(envc, argc, sp, p, 0);
1586 /* Check the right amount of stack was allocated for auxvec, envp & argv. */
1587 assert(sp_auxv - sp == size);
1588 return sp;
1591 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1592 /* If the guest doesn't have a validation function just agree */
1593 static int validate_guest_space(unsigned long guest_base,
1594 unsigned long guest_size)
1596 return 1;
1598 #endif
1600 unsigned long init_guest_space(unsigned long host_start,
1601 unsigned long host_size,
1602 unsigned long guest_start,
1603 bool fixed)
1605 unsigned long current_start, real_start;
1606 int flags;
1608 assert(host_start || host_size);
1610 /* If just a starting address is given, then just verify that
1611 * address. */
1612 if (host_start && !host_size) {
1613 if (validate_guest_space(host_start, host_size) == 1) {
1614 return host_start;
1615 } else {
1616 return (unsigned long)-1;
1620 /* Setup the initial flags and start address. */
1621 current_start = host_start & qemu_host_page_mask;
1622 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1623 if (fixed) {
1624 flags |= MAP_FIXED;
1627 /* Otherwise, a non-zero size region of memory needs to be mapped
1628 * and validated. */
1629 while (1) {
1630 unsigned long real_size = host_size;
1632 /* Do not use mmap_find_vma here because that is limited to the
1633 * guest address space. We are going to make the
1634 * guest address space fit whatever we're given.
1636 real_start = (unsigned long)
1637 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1638 if (real_start == (unsigned long)-1) {
1639 return (unsigned long)-1;
1642 /* Ensure the address is properly aligned. */
1643 if (real_start & ~qemu_host_page_mask) {
1644 munmap((void *)real_start, host_size);
1645 real_size = host_size + qemu_host_page_size;
1646 real_start = (unsigned long)
1647 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1648 if (real_start == (unsigned long)-1) {
1649 return (unsigned long)-1;
1651 real_start = HOST_PAGE_ALIGN(real_start);
1654 /* Check to see if the address is valid. */
1655 if (!host_start || real_start == current_start) {
1656 int valid = validate_guest_space(real_start - guest_start,
1657 real_size);
1658 if (valid == 1) {
1659 break;
1660 } else if (valid == -1) {
1661 return (unsigned long)-1;
1663 /* valid == 0, so try again. */
1666 /* That address didn't work. Unmap and try a different one.
1667 * The address the host picked because is typically right at
1668 * the top of the host address space and leaves the guest with
1669 * no usable address space. Resort to a linear search. We
1670 * already compensated for mmap_min_addr, so this should not
1671 * happen often. Probably means we got unlucky and host
1672 * address space randomization put a shared library somewhere
1673 * inconvenient.
1675 munmap((void *)real_start, host_size);
1676 current_start += qemu_host_page_size;
1677 if (host_start == current_start) {
1678 /* Theoretically possible if host doesn't have any suitably
1679 * aligned areas. Normally the first mmap will fail.
1681 return (unsigned long)-1;
1685 qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size);
1687 return real_start;
1690 static void probe_guest_base(const char *image_name,
1691 abi_ulong loaddr, abi_ulong hiaddr)
1693 /* Probe for a suitable guest base address, if the user has not set
1694 * it explicitly, and set guest_base appropriately.
1695 * In case of error we will print a suitable message and exit.
1697 #if defined(CONFIG_USE_GUEST_BASE)
1698 const char *errmsg;
1699 if (!have_guest_base && !reserved_va) {
1700 unsigned long host_start, real_start, host_size;
1702 /* Round addresses to page boundaries. */
1703 loaddr &= qemu_host_page_mask;
1704 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1706 if (loaddr < mmap_min_addr) {
1707 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1708 } else {
1709 host_start = loaddr;
1710 if (host_start != loaddr) {
1711 errmsg = "Address overflow loading ELF binary";
1712 goto exit_errmsg;
1715 host_size = hiaddr - loaddr;
1717 /* Setup the initial guest memory space with ranges gleaned from
1718 * the ELF image that is being loaded.
1720 real_start = init_guest_space(host_start, host_size, loaddr, false);
1721 if (real_start == (unsigned long)-1) {
1722 errmsg = "Unable to find space for application";
1723 goto exit_errmsg;
1725 guest_base = real_start - loaddr;
1727 qemu_log("Relocating guest address space from 0x"
1728 TARGET_ABI_FMT_lx " to 0x%lx\n",
1729 loaddr, real_start);
1731 return;
1733 exit_errmsg:
1734 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1735 exit(-1);
1736 #endif
1740 /* Load an ELF image into the address space.
1742 IMAGE_NAME is the filename of the image, to use in error messages.
1743 IMAGE_FD is the open file descriptor for the image.
1745 BPRM_BUF is a copy of the beginning of the file; this of course
1746 contains the elf file header at offset 0. It is assumed that this
1747 buffer is sufficiently aligned to present no problems to the host
1748 in accessing data at aligned offsets within the buffer.
1750 On return: INFO values will be filled in, as necessary or available. */
1752 static void load_elf_image(const char *image_name, int image_fd,
1753 struct image_info *info, char **pinterp_name,
1754 char bprm_buf[BPRM_BUF_SIZE])
1756 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1757 struct elf_phdr *phdr;
1758 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1759 int i, retval;
1760 const char *errmsg;
1762 /* First of all, some simple consistency checks */
1763 errmsg = "Invalid ELF image for this architecture";
1764 if (!elf_check_ident(ehdr)) {
1765 goto exit_errmsg;
1767 bswap_ehdr(ehdr);
1768 if (!elf_check_ehdr(ehdr)) {
1769 goto exit_errmsg;
1772 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1773 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1774 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
1775 } else {
1776 phdr = (struct elf_phdr *) alloca(i);
1777 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
1778 if (retval != i) {
1779 goto exit_read;
1782 bswap_phdr(phdr, ehdr->e_phnum);
1784 #ifdef CONFIG_USE_FDPIC
1785 info->nsegs = 0;
1786 info->pt_dynamic_addr = 0;
1787 #endif
1789 /* Find the maximum size of the image and allocate an appropriate
1790 amount of memory to handle that. */
1791 loaddr = -1, hiaddr = 0;
1792 for (i = 0; i < ehdr->e_phnum; ++i) {
1793 if (phdr[i].p_type == PT_LOAD) {
1794 abi_ulong a = phdr[i].p_vaddr;
1795 if (a < loaddr) {
1796 loaddr = a;
1798 a += phdr[i].p_memsz;
1799 if (a > hiaddr) {
1800 hiaddr = a;
1802 #ifdef CONFIG_USE_FDPIC
1803 ++info->nsegs;
1804 #endif
1808 load_addr = loaddr;
1809 if (ehdr->e_type == ET_DYN) {
1810 /* The image indicates that it can be loaded anywhere. Find a
1811 location that can hold the memory space required. If the
1812 image is pre-linked, LOADDR will be non-zero. Since we do
1813 not supply MAP_FIXED here we'll use that address if and
1814 only if it remains available. */
1815 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
1816 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
1817 -1, 0);
1818 if (load_addr == -1) {
1819 goto exit_perror;
1821 } else if (pinterp_name != NULL) {
1822 /* This is the main executable. Make sure that the low
1823 address does not conflict with MMAP_MIN_ADDR or the
1824 QEMU application itself. */
1825 probe_guest_base(image_name, loaddr, hiaddr);
1827 load_bias = load_addr - loaddr;
1829 #ifdef CONFIG_USE_FDPIC
1831 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
1832 g_malloc(sizeof(*loadsegs) * info->nsegs);
1834 for (i = 0; i < ehdr->e_phnum; ++i) {
1835 switch (phdr[i].p_type) {
1836 case PT_DYNAMIC:
1837 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
1838 break;
1839 case PT_LOAD:
1840 loadsegs->addr = phdr[i].p_vaddr + load_bias;
1841 loadsegs->p_vaddr = phdr[i].p_vaddr;
1842 loadsegs->p_memsz = phdr[i].p_memsz;
1843 ++loadsegs;
1844 break;
1848 #endif
1850 info->load_bias = load_bias;
1851 info->load_addr = load_addr;
1852 info->entry = ehdr->e_entry + load_bias;
1853 info->start_code = -1;
1854 info->end_code = 0;
1855 info->start_data = -1;
1856 info->end_data = 0;
1857 info->brk = 0;
1858 info->elf_flags = ehdr->e_flags;
1860 for (i = 0; i < ehdr->e_phnum; i++) {
1861 struct elf_phdr *eppnt = phdr + i;
1862 if (eppnt->p_type == PT_LOAD) {
1863 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
1864 int elf_prot = 0;
1866 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1867 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1868 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1870 vaddr = load_bias + eppnt->p_vaddr;
1871 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
1872 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
1874 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
1875 elf_prot, MAP_PRIVATE | MAP_FIXED,
1876 image_fd, eppnt->p_offset - vaddr_po);
1877 if (error == -1) {
1878 goto exit_perror;
1881 vaddr_ef = vaddr + eppnt->p_filesz;
1882 vaddr_em = vaddr + eppnt->p_memsz;
1884 /* If the load segment requests extra zeros (e.g. bss), map it. */
1885 if (vaddr_ef < vaddr_em) {
1886 zero_bss(vaddr_ef, vaddr_em, elf_prot);
1889 /* Find the full program boundaries. */
1890 if (elf_prot & PROT_EXEC) {
1891 if (vaddr < info->start_code) {
1892 info->start_code = vaddr;
1894 if (vaddr_ef > info->end_code) {
1895 info->end_code = vaddr_ef;
1898 if (elf_prot & PROT_WRITE) {
1899 if (vaddr < info->start_data) {
1900 info->start_data = vaddr;
1902 if (vaddr_ef > info->end_data) {
1903 info->end_data = vaddr_ef;
1905 if (vaddr_em > info->brk) {
1906 info->brk = vaddr_em;
1909 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
1910 char *interp_name;
1912 if (*pinterp_name) {
1913 errmsg = "Multiple PT_INTERP entries";
1914 goto exit_errmsg;
1916 interp_name = malloc(eppnt->p_filesz);
1917 if (!interp_name) {
1918 goto exit_perror;
1921 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
1922 memcpy(interp_name, bprm_buf + eppnt->p_offset,
1923 eppnt->p_filesz);
1924 } else {
1925 retval = pread(image_fd, interp_name, eppnt->p_filesz,
1926 eppnt->p_offset);
1927 if (retval != eppnt->p_filesz) {
1928 goto exit_perror;
1931 if (interp_name[eppnt->p_filesz - 1] != 0) {
1932 errmsg = "Invalid PT_INTERP entry";
1933 goto exit_errmsg;
1935 *pinterp_name = interp_name;
1939 if (info->end_data == 0) {
1940 info->start_data = info->end_code;
1941 info->end_data = info->end_code;
1942 info->brk = info->end_code;
1945 if (qemu_log_enabled()) {
1946 load_symbols(ehdr, image_fd, load_bias);
1949 close(image_fd);
1950 return;
1952 exit_read:
1953 if (retval >= 0) {
1954 errmsg = "Incomplete read of file header";
1955 goto exit_errmsg;
1957 exit_perror:
1958 errmsg = strerror(errno);
1959 exit_errmsg:
1960 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1961 exit(-1);
1964 static void load_elf_interp(const char *filename, struct image_info *info,
1965 char bprm_buf[BPRM_BUF_SIZE])
1967 int fd, retval;
1969 fd = open(path(filename), O_RDONLY);
1970 if (fd < 0) {
1971 goto exit_perror;
1974 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
1975 if (retval < 0) {
1976 goto exit_perror;
1978 if (retval < BPRM_BUF_SIZE) {
1979 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
1982 load_elf_image(filename, fd, info, NULL, bprm_buf);
1983 return;
1985 exit_perror:
1986 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
1987 exit(-1);
1990 static int symfind(const void *s0, const void *s1)
1992 target_ulong addr = *(target_ulong *)s0;
1993 struct elf_sym *sym = (struct elf_sym *)s1;
1994 int result = 0;
1995 if (addr < sym->st_value) {
1996 result = -1;
1997 } else if (addr >= sym->st_value + sym->st_size) {
1998 result = 1;
2000 return result;
2003 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2005 #if ELF_CLASS == ELFCLASS32
2006 struct elf_sym *syms = s->disas_symtab.elf32;
2007 #else
2008 struct elf_sym *syms = s->disas_symtab.elf64;
2009 #endif
2011 // binary search
2012 struct elf_sym *sym;
2014 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2015 if (sym != NULL) {
2016 return s->disas_strtab + sym->st_name;
2019 return "";
2022 /* FIXME: This should use elf_ops.h */
2023 static int symcmp(const void *s0, const void *s1)
2025 struct elf_sym *sym0 = (struct elf_sym *)s0;
2026 struct elf_sym *sym1 = (struct elf_sym *)s1;
2027 return (sym0->st_value < sym1->st_value)
2028 ? -1
2029 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2032 /* Best attempt to load symbols from this ELF object. */
2033 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2035 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2036 struct elf_shdr *shdr;
2037 char *strings = NULL;
2038 struct syminfo *s = NULL;
2039 struct elf_sym *new_syms, *syms = NULL;
2041 shnum = hdr->e_shnum;
2042 i = shnum * sizeof(struct elf_shdr);
2043 shdr = (struct elf_shdr *)alloca(i);
2044 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2045 return;
2048 bswap_shdr(shdr, shnum);
2049 for (i = 0; i < shnum; ++i) {
2050 if (shdr[i].sh_type == SHT_SYMTAB) {
2051 sym_idx = i;
2052 str_idx = shdr[i].sh_link;
2053 goto found;
2057 /* There will be no symbol table if the file was stripped. */
2058 return;
2060 found:
2061 /* Now know where the strtab and symtab are. Snarf them. */
2062 s = malloc(sizeof(*s));
2063 if (!s) {
2064 goto give_up;
2067 i = shdr[str_idx].sh_size;
2068 s->disas_strtab = strings = malloc(i);
2069 if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
2070 goto give_up;
2073 i = shdr[sym_idx].sh_size;
2074 syms = malloc(i);
2075 if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
2076 goto give_up;
2079 nsyms = i / sizeof(struct elf_sym);
2080 for (i = 0; i < nsyms; ) {
2081 bswap_sym(syms + i);
2082 /* Throw away entries which we do not need. */
2083 if (syms[i].st_shndx == SHN_UNDEF
2084 || syms[i].st_shndx >= SHN_LORESERVE
2085 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2086 if (i < --nsyms) {
2087 syms[i] = syms[nsyms];
2089 } else {
2090 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2091 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2092 syms[i].st_value &= ~(target_ulong)1;
2093 #endif
2094 syms[i].st_value += load_bias;
2095 i++;
2099 /* No "useful" symbol. */
2100 if (nsyms == 0) {
2101 goto give_up;
2104 /* Attempt to free the storage associated with the local symbols
2105 that we threw away. Whether or not this has any effect on the
2106 memory allocation depends on the malloc implementation and how
2107 many symbols we managed to discard. */
2108 new_syms = realloc(syms, nsyms * sizeof(*syms));
2109 if (new_syms == NULL) {
2110 goto give_up;
2112 syms = new_syms;
2114 qsort(syms, nsyms, sizeof(*syms), symcmp);
2116 s->disas_num_syms = nsyms;
2117 #if ELF_CLASS == ELFCLASS32
2118 s->disas_symtab.elf32 = syms;
2119 #else
2120 s->disas_symtab.elf64 = syms;
2121 #endif
2122 s->lookup_symbol = lookup_symbolxx;
2123 s->next = syminfos;
2124 syminfos = s;
2126 return;
2128 give_up:
2129 free(s);
2130 free(strings);
2131 free(syms);
2134 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2136 struct image_info interp_info;
2137 struct elfhdr elf_ex;
2138 char *elf_interpreter = NULL;
2140 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2141 info->mmap = 0;
2142 info->rss = 0;
2144 load_elf_image(bprm->filename, bprm->fd, info,
2145 &elf_interpreter, bprm->buf);
2147 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2148 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2149 when we load the interpreter. */
2150 elf_ex = *(struct elfhdr *)bprm->buf;
2152 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
2153 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
2154 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
2155 if (!bprm->p) {
2156 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2157 exit(-1);
2160 /* Do this so that we can load the interpreter, if need be. We will
2161 change some of these later */
2162 bprm->p = setup_arg_pages(bprm->p, bprm, info);
2164 if (elf_interpreter) {
2165 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2167 /* If the program interpreter is one of these two, then assume
2168 an iBCS2 image. Otherwise assume a native linux image. */
2170 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2171 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2172 info->personality = PER_SVR4;
2174 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2175 and some applications "depend" upon this behavior. Since
2176 we do not have the power to recompile these, we emulate
2177 the SVr4 behavior. Sigh. */
2178 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2179 MAP_FIXED | MAP_PRIVATE, -1, 0);
2183 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2184 info, (elf_interpreter ? &interp_info : NULL));
2185 info->start_stack = bprm->p;
2187 /* If we have an interpreter, set that as the program's entry point.
2188 Copy the load_bias as well, to help PPC64 interpret the entry
2189 point as a function descriptor. Do this after creating elf tables
2190 so that we copy the original program entry point into the AUXV. */
2191 if (elf_interpreter) {
2192 info->load_bias = interp_info.load_bias;
2193 info->entry = interp_info.entry;
2194 free(elf_interpreter);
2197 #ifdef USE_ELF_CORE_DUMP
2198 bprm->core_dump = &elf_core_dump;
2199 #endif
2201 return 0;
2204 #ifdef USE_ELF_CORE_DUMP
2206 * Definitions to generate Intel SVR4-like core files.
2207 * These mostly have the same names as the SVR4 types with "target_elf_"
2208 * tacked on the front to prevent clashes with linux definitions,
2209 * and the typedef forms have been avoided. This is mostly like
2210 * the SVR4 structure, but more Linuxy, with things that Linux does
2211 * not support and which gdb doesn't really use excluded.
2213 * Fields we don't dump (their contents is zero) in linux-user qemu
2214 * are marked with XXX.
2216 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2218 * Porting ELF coredump for target is (quite) simple process. First you
2219 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2220 * the target resides):
2222 * #define USE_ELF_CORE_DUMP
2224 * Next you define type of register set used for dumping. ELF specification
2225 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2227 * typedef <target_regtype> target_elf_greg_t;
2228 * #define ELF_NREG <number of registers>
2229 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2231 * Last step is to implement target specific function that copies registers
2232 * from given cpu into just specified register set. Prototype is:
2234 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2235 * const CPUArchState *env);
2237 * Parameters:
2238 * regs - copy register values into here (allocated and zeroed by caller)
2239 * env - copy registers from here
2241 * Example for ARM target is provided in this file.
2244 /* An ELF note in memory */
2245 struct memelfnote {
2246 const char *name;
2247 size_t namesz;
2248 size_t namesz_rounded;
2249 int type;
2250 size_t datasz;
2251 size_t datasz_rounded;
2252 void *data;
2253 size_t notesz;
2256 struct target_elf_siginfo {
2257 abi_int si_signo; /* signal number */
2258 abi_int si_code; /* extra code */
2259 abi_int si_errno; /* errno */
2262 struct target_elf_prstatus {
2263 struct target_elf_siginfo pr_info; /* Info associated with signal */
2264 abi_short pr_cursig; /* Current signal */
2265 abi_ulong pr_sigpend; /* XXX */
2266 abi_ulong pr_sighold; /* XXX */
2267 target_pid_t pr_pid;
2268 target_pid_t pr_ppid;
2269 target_pid_t pr_pgrp;
2270 target_pid_t pr_sid;
2271 struct target_timeval pr_utime; /* XXX User time */
2272 struct target_timeval pr_stime; /* XXX System time */
2273 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2274 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2275 target_elf_gregset_t pr_reg; /* GP registers */
2276 abi_int pr_fpvalid; /* XXX */
2279 #define ELF_PRARGSZ (80) /* Number of chars for args */
2281 struct target_elf_prpsinfo {
2282 char pr_state; /* numeric process state */
2283 char pr_sname; /* char for pr_state */
2284 char pr_zomb; /* zombie */
2285 char pr_nice; /* nice val */
2286 abi_ulong pr_flag; /* flags */
2287 target_uid_t pr_uid;
2288 target_gid_t pr_gid;
2289 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2290 /* Lots missing */
2291 char pr_fname[16]; /* filename of executable */
2292 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2295 /* Here is the structure in which status of each thread is captured. */
2296 struct elf_thread_status {
2297 QTAILQ_ENTRY(elf_thread_status) ets_link;
2298 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2299 #if 0
2300 elf_fpregset_t fpu; /* NT_PRFPREG */
2301 struct task_struct *thread;
2302 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2303 #endif
2304 struct memelfnote notes[1];
2305 int num_notes;
2308 struct elf_note_info {
2309 struct memelfnote *notes;
2310 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2311 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2313 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2314 #if 0
2316 * Current version of ELF coredump doesn't support
2317 * dumping fp regs etc.
2319 elf_fpregset_t *fpu;
2320 elf_fpxregset_t *xfpu;
2321 int thread_status_size;
2322 #endif
2323 int notes_size;
2324 int numnote;
2327 struct vm_area_struct {
2328 abi_ulong vma_start; /* start vaddr of memory region */
2329 abi_ulong vma_end; /* end vaddr of memory region */
2330 abi_ulong vma_flags; /* protection etc. flags for the region */
2331 QTAILQ_ENTRY(vm_area_struct) vma_link;
2334 struct mm_struct {
2335 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2336 int mm_count; /* number of mappings */
2339 static struct mm_struct *vma_init(void);
2340 static void vma_delete(struct mm_struct *);
2341 static int vma_add_mapping(struct mm_struct *, abi_ulong,
2342 abi_ulong, abi_ulong);
2343 static int vma_get_mapping_count(const struct mm_struct *);
2344 static struct vm_area_struct *vma_first(const struct mm_struct *);
2345 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2346 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2347 static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
2348 unsigned long flags);
2350 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2351 static void fill_note(struct memelfnote *, const char *, int,
2352 unsigned int, void *);
2353 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2354 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2355 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2356 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2357 static size_t note_size(const struct memelfnote *);
2358 static void free_note_info(struct elf_note_info *);
2359 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2360 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2361 static int core_dump_filename(const TaskState *, char *, size_t);
2363 static int dump_write(int, const void *, size_t);
2364 static int write_note(struct memelfnote *, int);
2365 static int write_note_info(struct elf_note_info *, int);
2367 #ifdef BSWAP_NEEDED
2368 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2370 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2371 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2372 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2373 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2374 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2375 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2376 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2377 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2378 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2379 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2380 /* cpu times are not filled, so we skip them */
2381 /* regs should be in correct format already */
2382 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2385 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2387 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2388 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2389 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2390 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2391 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2392 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2393 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2396 static void bswap_note(struct elf_note *en)
2398 bswap32s(&en->n_namesz);
2399 bswap32s(&en->n_descsz);
2400 bswap32s(&en->n_type);
2402 #else
2403 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2404 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2405 static inline void bswap_note(struct elf_note *en) { }
2406 #endif /* BSWAP_NEEDED */
2409 * Minimal support for linux memory regions. These are needed
2410 * when we are finding out what memory exactly belongs to
2411 * emulated process. No locks needed here, as long as
2412 * thread that received the signal is stopped.
2415 static struct mm_struct *vma_init(void)
2417 struct mm_struct *mm;
2419 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2420 return (NULL);
2422 mm->mm_count = 0;
2423 QTAILQ_INIT(&mm->mm_mmap);
2425 return (mm);
2428 static void vma_delete(struct mm_struct *mm)
2430 struct vm_area_struct *vma;
2432 while ((vma = vma_first(mm)) != NULL) {
2433 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2434 g_free(vma);
2436 g_free(mm);
2439 static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
2440 abi_ulong end, abi_ulong flags)
2442 struct vm_area_struct *vma;
2444 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2445 return (-1);
2447 vma->vma_start = start;
2448 vma->vma_end = end;
2449 vma->vma_flags = flags;
2451 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2452 mm->mm_count++;
2454 return (0);
2457 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2459 return (QTAILQ_FIRST(&mm->mm_mmap));
2462 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2464 return (QTAILQ_NEXT(vma, vma_link));
2467 static int vma_get_mapping_count(const struct mm_struct *mm)
2469 return (mm->mm_count);
2473 * Calculate file (dump) size of given memory region.
2475 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2477 /* if we cannot even read the first page, skip it */
2478 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2479 return (0);
2482 * Usually we don't dump executable pages as they contain
2483 * non-writable code that debugger can read directly from
2484 * target library etc. However, thread stacks are marked
2485 * also executable so we read in first page of given region
2486 * and check whether it contains elf header. If there is
2487 * no elf header, we dump it.
2489 if (vma->vma_flags & PROT_EXEC) {
2490 char page[TARGET_PAGE_SIZE];
2492 copy_from_user(page, vma->vma_start, sizeof (page));
2493 if ((page[EI_MAG0] == ELFMAG0) &&
2494 (page[EI_MAG1] == ELFMAG1) &&
2495 (page[EI_MAG2] == ELFMAG2) &&
2496 (page[EI_MAG3] == ELFMAG3)) {
2498 * Mappings are possibly from ELF binary. Don't dump
2499 * them.
2501 return (0);
2505 return (vma->vma_end - vma->vma_start);
2508 static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
2509 unsigned long flags)
2511 struct mm_struct *mm = (struct mm_struct *)priv;
2513 vma_add_mapping(mm, start, end, flags);
2514 return (0);
2517 static void fill_note(struct memelfnote *note, const char *name, int type,
2518 unsigned int sz, void *data)
2520 unsigned int namesz;
2522 namesz = strlen(name) + 1;
2523 note->name = name;
2524 note->namesz = namesz;
2525 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2526 note->type = type;
2527 note->datasz = sz;
2528 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2530 note->data = data;
2533 * We calculate rounded up note size here as specified by
2534 * ELF document.
2536 note->notesz = sizeof (struct elf_note) +
2537 note->namesz_rounded + note->datasz_rounded;
2540 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2541 uint32_t flags)
2543 (void) memset(elf, 0, sizeof(*elf));
2545 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2546 elf->e_ident[EI_CLASS] = ELF_CLASS;
2547 elf->e_ident[EI_DATA] = ELF_DATA;
2548 elf->e_ident[EI_VERSION] = EV_CURRENT;
2549 elf->e_ident[EI_OSABI] = ELF_OSABI;
2551 elf->e_type = ET_CORE;
2552 elf->e_machine = machine;
2553 elf->e_version = EV_CURRENT;
2554 elf->e_phoff = sizeof(struct elfhdr);
2555 elf->e_flags = flags;
2556 elf->e_ehsize = sizeof(struct elfhdr);
2557 elf->e_phentsize = sizeof(struct elf_phdr);
2558 elf->e_phnum = segs;
2560 bswap_ehdr(elf);
2563 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2565 phdr->p_type = PT_NOTE;
2566 phdr->p_offset = offset;
2567 phdr->p_vaddr = 0;
2568 phdr->p_paddr = 0;
2569 phdr->p_filesz = sz;
2570 phdr->p_memsz = 0;
2571 phdr->p_flags = 0;
2572 phdr->p_align = 0;
2574 bswap_phdr(phdr, 1);
2577 static size_t note_size(const struct memelfnote *note)
2579 return (note->notesz);
2582 static void fill_prstatus(struct target_elf_prstatus *prstatus,
2583 const TaskState *ts, int signr)
2585 (void) memset(prstatus, 0, sizeof (*prstatus));
2586 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2587 prstatus->pr_pid = ts->ts_tid;
2588 prstatus->pr_ppid = getppid();
2589 prstatus->pr_pgrp = getpgrp();
2590 prstatus->pr_sid = getsid(0);
2592 bswap_prstatus(prstatus);
2595 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2597 char *base_filename;
2598 unsigned int i, len;
2600 (void) memset(psinfo, 0, sizeof (*psinfo));
2602 len = ts->info->arg_end - ts->info->arg_start;
2603 if (len >= ELF_PRARGSZ)
2604 len = ELF_PRARGSZ - 1;
2605 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2606 return -EFAULT;
2607 for (i = 0; i < len; i++)
2608 if (psinfo->pr_psargs[i] == 0)
2609 psinfo->pr_psargs[i] = ' ';
2610 psinfo->pr_psargs[len] = 0;
2612 psinfo->pr_pid = getpid();
2613 psinfo->pr_ppid = getppid();
2614 psinfo->pr_pgrp = getpgrp();
2615 psinfo->pr_sid = getsid(0);
2616 psinfo->pr_uid = getuid();
2617 psinfo->pr_gid = getgid();
2619 base_filename = g_path_get_basename(ts->bprm->filename);
2621 * Using strncpy here is fine: at max-length,
2622 * this field is not NUL-terminated.
2624 (void) strncpy(psinfo->pr_fname, base_filename,
2625 sizeof(psinfo->pr_fname));
2627 g_free(base_filename);
2628 bswap_psinfo(psinfo);
2629 return (0);
2632 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2634 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2635 elf_addr_t orig_auxv = auxv;
2636 void *ptr;
2637 int len = ts->info->auxv_len;
2640 * Auxiliary vector is stored in target process stack. It contains
2641 * {type, value} pairs that we need to dump into note. This is not
2642 * strictly necessary but we do it here for sake of completeness.
2645 /* read in whole auxv vector and copy it to memelfnote */
2646 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2647 if (ptr != NULL) {
2648 fill_note(note, "CORE", NT_AUXV, len, ptr);
2649 unlock_user(ptr, auxv, len);
2654 * Constructs name of coredump file. We have following convention
2655 * for the name:
2656 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2658 * Returns 0 in case of success, -1 otherwise (errno is set).
2660 static int core_dump_filename(const TaskState *ts, char *buf,
2661 size_t bufsize)
2663 char timestamp[64];
2664 char *filename = NULL;
2665 char *base_filename = NULL;
2666 struct timeval tv;
2667 struct tm tm;
2669 assert(bufsize >= PATH_MAX);
2671 if (gettimeofday(&tv, NULL) < 0) {
2672 (void) fprintf(stderr, "unable to get current timestamp: %s",
2673 strerror(errno));
2674 return (-1);
2677 filename = strdup(ts->bprm->filename);
2678 base_filename = strdup(basename(filename));
2679 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2680 localtime_r(&tv.tv_sec, &tm));
2681 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2682 base_filename, timestamp, (int)getpid());
2683 free(base_filename);
2684 free(filename);
2686 return (0);
2689 static int dump_write(int fd, const void *ptr, size_t size)
2691 const char *bufp = (const char *)ptr;
2692 ssize_t bytes_written, bytes_left;
2693 struct rlimit dumpsize;
2694 off_t pos;
2696 bytes_written = 0;
2697 getrlimit(RLIMIT_CORE, &dumpsize);
2698 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2699 if (errno == ESPIPE) { /* not a seekable stream */
2700 bytes_left = size;
2701 } else {
2702 return pos;
2704 } else {
2705 if (dumpsize.rlim_cur <= pos) {
2706 return -1;
2707 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2708 bytes_left = size;
2709 } else {
2710 size_t limit_left=dumpsize.rlim_cur - pos;
2711 bytes_left = limit_left >= size ? size : limit_left ;
2716 * In normal conditions, single write(2) should do but
2717 * in case of socket etc. this mechanism is more portable.
2719 do {
2720 bytes_written = write(fd, bufp, bytes_left);
2721 if (bytes_written < 0) {
2722 if (errno == EINTR)
2723 continue;
2724 return (-1);
2725 } else if (bytes_written == 0) { /* eof */
2726 return (-1);
2728 bufp += bytes_written;
2729 bytes_left -= bytes_written;
2730 } while (bytes_left > 0);
2732 return (0);
2735 static int write_note(struct memelfnote *men, int fd)
2737 struct elf_note en;
2739 en.n_namesz = men->namesz;
2740 en.n_type = men->type;
2741 en.n_descsz = men->datasz;
2743 bswap_note(&en);
2745 if (dump_write(fd, &en, sizeof(en)) != 0)
2746 return (-1);
2747 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2748 return (-1);
2749 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
2750 return (-1);
2752 return (0);
2755 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
2757 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2758 TaskState *ts = (TaskState *)cpu->opaque;
2759 struct elf_thread_status *ets;
2761 ets = g_malloc0(sizeof (*ets));
2762 ets->num_notes = 1; /* only prstatus is dumped */
2763 fill_prstatus(&ets->prstatus, ts, 0);
2764 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2765 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2766 &ets->prstatus);
2768 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
2770 info->notes_size += note_size(&ets->notes[0]);
2773 static void init_note_info(struct elf_note_info *info)
2775 /* Initialize the elf_note_info structure so that it is at
2776 * least safe to call free_note_info() on it. Must be
2777 * called before calling fill_note_info().
2779 memset(info, 0, sizeof (*info));
2780 QTAILQ_INIT(&info->thread_list);
2783 static int fill_note_info(struct elf_note_info *info,
2784 long signr, const CPUArchState *env)
2786 #define NUMNOTES 3
2787 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2788 TaskState *ts = (TaskState *)cpu->opaque;
2789 int i;
2791 info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote));
2792 if (info->notes == NULL)
2793 return (-ENOMEM);
2794 info->prstatus = g_malloc0(sizeof (*info->prstatus));
2795 if (info->prstatus == NULL)
2796 return (-ENOMEM);
2797 info->psinfo = g_malloc0(sizeof (*info->psinfo));
2798 if (info->prstatus == NULL)
2799 return (-ENOMEM);
2802 * First fill in status (and registers) of current thread
2803 * including process info & aux vector.
2805 fill_prstatus(info->prstatus, ts, signr);
2806 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2807 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2808 sizeof (*info->prstatus), info->prstatus);
2809 fill_psinfo(info->psinfo, ts);
2810 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2811 sizeof (*info->psinfo), info->psinfo);
2812 fill_auxv_note(&info->notes[2], ts);
2813 info->numnote = 3;
2815 info->notes_size = 0;
2816 for (i = 0; i < info->numnote; i++)
2817 info->notes_size += note_size(&info->notes[i]);
2819 /* read and fill status of all threads */
2820 cpu_list_lock();
2821 CPU_FOREACH(cpu) {
2822 if (cpu == thread_cpu) {
2823 continue;
2825 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
2827 cpu_list_unlock();
2829 return (0);
2832 static void free_note_info(struct elf_note_info *info)
2834 struct elf_thread_status *ets;
2836 while (!QTAILQ_EMPTY(&info->thread_list)) {
2837 ets = QTAILQ_FIRST(&info->thread_list);
2838 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
2839 g_free(ets);
2842 g_free(info->prstatus);
2843 g_free(info->psinfo);
2844 g_free(info->notes);
2847 static int write_note_info(struct elf_note_info *info, int fd)
2849 struct elf_thread_status *ets;
2850 int i, error = 0;
2852 /* write prstatus, psinfo and auxv for current thread */
2853 for (i = 0; i < info->numnote; i++)
2854 if ((error = write_note(&info->notes[i], fd)) != 0)
2855 return (error);
2857 /* write prstatus for each thread */
2858 for (ets = info->thread_list.tqh_first; ets != NULL;
2859 ets = ets->ets_link.tqe_next) {
2860 if ((error = write_note(&ets->notes[0], fd)) != 0)
2861 return (error);
2864 return (0);
2868 * Write out ELF coredump.
2870 * See documentation of ELF object file format in:
2871 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2873 * Coredump format in linux is following:
2875 * 0 +----------------------+ \
2876 * | ELF header | ET_CORE |
2877 * +----------------------+ |
2878 * | ELF program headers | |--- headers
2879 * | - NOTE section | |
2880 * | - PT_LOAD sections | |
2881 * +----------------------+ /
2882 * | NOTEs: |
2883 * | - NT_PRSTATUS |
2884 * | - NT_PRSINFO |
2885 * | - NT_AUXV |
2886 * +----------------------+ <-- aligned to target page
2887 * | Process memory dump |
2888 * : :
2889 * . .
2890 * : :
2891 * | |
2892 * +----------------------+
2894 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2895 * NT_PRSINFO -> struct elf_prpsinfo
2896 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2898 * Format follows System V format as close as possible. Current
2899 * version limitations are as follows:
2900 * - no floating point registers are dumped
2902 * Function returns 0 in case of success, negative errno otherwise.
2904 * TODO: make this work also during runtime: it should be
2905 * possible to force coredump from running process and then
2906 * continue processing. For example qemu could set up SIGUSR2
2907 * handler (provided that target process haven't registered
2908 * handler for that) that does the dump when signal is received.
2910 static int elf_core_dump(int signr, const CPUArchState *env)
2912 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2913 const TaskState *ts = (const TaskState *)cpu->opaque;
2914 struct vm_area_struct *vma = NULL;
2915 char corefile[PATH_MAX];
2916 struct elf_note_info info;
2917 struct elfhdr elf;
2918 struct elf_phdr phdr;
2919 struct rlimit dumpsize;
2920 struct mm_struct *mm = NULL;
2921 off_t offset = 0, data_offset = 0;
2922 int segs = 0;
2923 int fd = -1;
2925 init_note_info(&info);
2927 errno = 0;
2928 getrlimit(RLIMIT_CORE, &dumpsize);
2929 if (dumpsize.rlim_cur == 0)
2930 return 0;
2932 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2933 return (-errno);
2935 if ((fd = open(corefile, O_WRONLY | O_CREAT,
2936 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2937 return (-errno);
2940 * Walk through target process memory mappings and
2941 * set up structure containing this information. After
2942 * this point vma_xxx functions can be used.
2944 if ((mm = vma_init()) == NULL)
2945 goto out;
2947 walk_memory_regions(mm, vma_walker);
2948 segs = vma_get_mapping_count(mm);
2951 * Construct valid coredump ELF header. We also
2952 * add one more segment for notes.
2954 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
2955 if (dump_write(fd, &elf, sizeof (elf)) != 0)
2956 goto out;
2958 /* fill in in-memory version of notes */
2959 if (fill_note_info(&info, signr, env) < 0)
2960 goto out;
2962 offset += sizeof (elf); /* elf header */
2963 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
2965 /* write out notes program header */
2966 fill_elf_note_phdr(&phdr, info.notes_size, offset);
2968 offset += info.notes_size;
2969 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
2970 goto out;
2973 * ELF specification wants data to start at page boundary so
2974 * we align it here.
2976 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
2979 * Write program headers for memory regions mapped in
2980 * the target process.
2982 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2983 (void) memset(&phdr, 0, sizeof (phdr));
2985 phdr.p_type = PT_LOAD;
2986 phdr.p_offset = offset;
2987 phdr.p_vaddr = vma->vma_start;
2988 phdr.p_paddr = 0;
2989 phdr.p_filesz = vma_dump_size(vma);
2990 offset += phdr.p_filesz;
2991 phdr.p_memsz = vma->vma_end - vma->vma_start;
2992 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
2993 if (vma->vma_flags & PROT_WRITE)
2994 phdr.p_flags |= PF_W;
2995 if (vma->vma_flags & PROT_EXEC)
2996 phdr.p_flags |= PF_X;
2997 phdr.p_align = ELF_EXEC_PAGESIZE;
2999 bswap_phdr(&phdr, 1);
3000 dump_write(fd, &phdr, sizeof (phdr));
3004 * Next we write notes just after program headers. No
3005 * alignment needed here.
3007 if (write_note_info(&info, fd) < 0)
3008 goto out;
3010 /* align data to page boundary */
3011 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3012 goto out;
3015 * Finally we can dump process memory into corefile as well.
3017 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3018 abi_ulong addr;
3019 abi_ulong end;
3021 end = vma->vma_start + vma_dump_size(vma);
3023 for (addr = vma->vma_start; addr < end;
3024 addr += TARGET_PAGE_SIZE) {
3025 char page[TARGET_PAGE_SIZE];
3026 int error;
3029 * Read in page from target process memory and
3030 * write it to coredump file.
3032 error = copy_from_user(page, addr, sizeof (page));
3033 if (error != 0) {
3034 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3035 addr);
3036 errno = -error;
3037 goto out;
3039 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3040 goto out;
3044 out:
3045 free_note_info(&info);
3046 if (mm != NULL)
3047 vma_delete(mm);
3048 (void) close(fd);
3050 if (errno != 0)
3051 return (-errno);
3052 return (0);
3054 #endif /* USE_ELF_CORE_DUMP */
3056 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3058 init_thread(regs, infop);