Improve log message for unaligned i/o
[qemu/ar7.git] / linux-user / elfload.c
blob5a7cdfd7b440333f6052ba7eb7d462d3b58f4aa2
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 <sys/types.h>
6 #include <sys/mman.h>
7 #include <sys/resource.h>
9 #include "qemu-common.h"
10 #include "qemu.h"
11 #include "disas/disas.h"
13 #ifdef _ARCH_PPC64
14 #undef ARCH_DLINFO
15 #undef ELF_PLATFORM
16 #undef ELF_HWCAP
17 #undef ELF_HWCAP2
18 #undef ELF_CLASS
19 #undef ELF_DATA
20 #undef ELF_ARCH
21 #endif
23 #define ELF_OSABI ELFOSABI_SYSV
25 /* from personality.h */
28 * Flags for bug emulation.
30 * These occupy the top three bytes.
32 enum {
33 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
34 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
35 descriptors (signal handling) */
36 MMAP_PAGE_ZERO = 0x0100000,
37 ADDR_COMPAT_LAYOUT = 0x0200000,
38 READ_IMPLIES_EXEC = 0x0400000,
39 ADDR_LIMIT_32BIT = 0x0800000,
40 SHORT_INODE = 0x1000000,
41 WHOLE_SECONDS = 0x2000000,
42 STICKY_TIMEOUTS = 0x4000000,
43 ADDR_LIMIT_3GB = 0x8000000,
47 * Personality types.
49 * These go in the low byte. Avoid using the top bit, it will
50 * conflict with error returns.
52 enum {
53 PER_LINUX = 0x0000,
54 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
55 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
56 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
57 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
58 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
59 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
60 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
61 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
62 PER_BSD = 0x0006,
63 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
64 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
65 PER_LINUX32 = 0x0008,
66 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
67 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
68 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
69 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
70 PER_RISCOS = 0x000c,
71 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
72 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
73 PER_OSF4 = 0x000f, /* OSF/1 v4 */
74 PER_HPUX = 0x0010,
75 PER_MASK = 0x00ff,
79 * Return the base personality without flags.
81 #define personality(pers) (pers & PER_MASK)
83 /* this flag is uneffective under linux too, should be deleted */
84 #ifndef MAP_DENYWRITE
85 #define MAP_DENYWRITE 0
86 #endif
88 /* should probably go in elf.h */
89 #ifndef ELIBBAD
90 #define ELIBBAD 80
91 #endif
93 #ifdef TARGET_WORDS_BIGENDIAN
94 #define ELF_DATA ELFDATA2MSB
95 #else
96 #define ELF_DATA ELFDATA2LSB
97 #endif
99 #ifdef TARGET_ABI_MIPSN32
100 typedef abi_ullong target_elf_greg_t;
101 #define tswapreg(ptr) tswap64(ptr)
102 #else
103 typedef abi_ulong target_elf_greg_t;
104 #define tswapreg(ptr) tswapal(ptr)
105 #endif
107 #ifdef USE_UID16
108 typedef abi_ushort target_uid_t;
109 typedef abi_ushort target_gid_t;
110 #else
111 typedef abi_uint target_uid_t;
112 typedef abi_uint target_gid_t;
113 #endif
114 typedef abi_int target_pid_t;
116 #ifdef TARGET_I386
118 #define ELF_PLATFORM get_elf_platform()
120 static const char *get_elf_platform(void)
122 static char elf_platform[] = "i386";
123 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
124 if (family > 6)
125 family = 6;
126 if (family >= 3)
127 elf_platform[1] = '0' + family;
128 return elf_platform;
131 #define ELF_HWCAP get_elf_hwcap()
133 static uint32_t get_elf_hwcap(void)
135 X86CPU *cpu = X86_CPU(thread_cpu);
137 return cpu->env.features[FEAT_1_EDX];
140 #ifdef TARGET_X86_64
141 #define ELF_START_MMAP 0x2aaaaab000ULL
142 #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
144 #define ELF_CLASS ELFCLASS64
145 #define ELF_ARCH EM_X86_64
147 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
149 regs->rax = 0;
150 regs->rsp = infop->start_stack;
151 regs->rip = infop->entry;
154 #define ELF_NREG 27
155 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
158 * Note that ELF_NREG should be 29 as there should be place for
159 * TRAPNO and ERR "registers" as well but linux doesn't dump
160 * those.
162 * See linux kernel: arch/x86/include/asm/elf.h
164 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
166 (*regs)[0] = env->regs[15];
167 (*regs)[1] = env->regs[14];
168 (*regs)[2] = env->regs[13];
169 (*regs)[3] = env->regs[12];
170 (*regs)[4] = env->regs[R_EBP];
171 (*regs)[5] = env->regs[R_EBX];
172 (*regs)[6] = env->regs[11];
173 (*regs)[7] = env->regs[10];
174 (*regs)[8] = env->regs[9];
175 (*regs)[9] = env->regs[8];
176 (*regs)[10] = env->regs[R_EAX];
177 (*regs)[11] = env->regs[R_ECX];
178 (*regs)[12] = env->regs[R_EDX];
179 (*regs)[13] = env->regs[R_ESI];
180 (*regs)[14] = env->regs[R_EDI];
181 (*regs)[15] = env->regs[R_EAX]; /* XXX */
182 (*regs)[16] = env->eip;
183 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
184 (*regs)[18] = env->eflags;
185 (*regs)[19] = env->regs[R_ESP];
186 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
187 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
188 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
189 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
190 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
191 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
192 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
195 #else
197 #define ELF_START_MMAP 0x80000000
200 * This is used to ensure we don't load something for the wrong architecture.
202 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
205 * These are used to set parameters in the core dumps.
207 #define ELF_CLASS ELFCLASS32
208 #define ELF_ARCH EM_386
210 static inline void init_thread(struct target_pt_regs *regs,
211 struct image_info *infop)
213 regs->esp = infop->start_stack;
214 regs->eip = infop->entry;
216 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
217 starts %edx contains a pointer to a function which might be
218 registered using `atexit'. This provides a mean for the
219 dynamic linker to call DT_FINI functions for shared libraries
220 that have been loaded before the code runs.
222 A value of 0 tells we have no such handler. */
223 regs->edx = 0;
226 #define ELF_NREG 17
227 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
230 * Note that ELF_NREG should be 19 as there should be place for
231 * TRAPNO and ERR "registers" as well but linux doesn't dump
232 * those.
234 * See linux kernel: arch/x86/include/asm/elf.h
236 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
238 (*regs)[0] = env->regs[R_EBX];
239 (*regs)[1] = env->regs[R_ECX];
240 (*regs)[2] = env->regs[R_EDX];
241 (*regs)[3] = env->regs[R_ESI];
242 (*regs)[4] = env->regs[R_EDI];
243 (*regs)[5] = env->regs[R_EBP];
244 (*regs)[6] = env->regs[R_EAX];
245 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
246 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
247 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
248 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
249 (*regs)[11] = env->regs[R_EAX]; /* XXX */
250 (*regs)[12] = env->eip;
251 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
252 (*regs)[14] = env->eflags;
253 (*regs)[15] = env->regs[R_ESP];
254 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
256 #endif
258 #define USE_ELF_CORE_DUMP
259 #define ELF_EXEC_PAGESIZE 4096
261 #endif
263 #ifdef TARGET_ARM
265 #ifndef TARGET_AARCH64
266 /* 32 bit ARM definitions */
268 #define ELF_START_MMAP 0x80000000
270 #define elf_check_arch(x) ((x) == ELF_MACHINE)
272 #define ELF_ARCH ELF_MACHINE
273 #define ELF_CLASS ELFCLASS32
275 static inline void init_thread(struct target_pt_regs *regs,
276 struct image_info *infop)
278 abi_long stack = infop->start_stack;
279 memset(regs, 0, sizeof(*regs));
281 regs->ARM_cpsr = 0x10;
282 if (infop->entry & 1)
283 regs->ARM_cpsr |= CPSR_T;
284 regs->ARM_pc = infop->entry & 0xfffffffe;
285 regs->ARM_sp = infop->start_stack;
286 /* FIXME - what to for failure of get_user()? */
287 get_user_ual(regs->ARM_r2, stack + 8); /* envp */
288 get_user_ual(regs->ARM_r1, stack + 4); /* envp */
289 /* XXX: it seems that r0 is zeroed after ! */
290 regs->ARM_r0 = 0;
291 /* For uClinux PIC binaries. */
292 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
293 regs->ARM_r10 = infop->start_data;
296 #define ELF_NREG 18
297 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
299 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
301 (*regs)[0] = tswapreg(env->regs[0]);
302 (*regs)[1] = tswapreg(env->regs[1]);
303 (*regs)[2] = tswapreg(env->regs[2]);
304 (*regs)[3] = tswapreg(env->regs[3]);
305 (*regs)[4] = tswapreg(env->regs[4]);
306 (*regs)[5] = tswapreg(env->regs[5]);
307 (*regs)[6] = tswapreg(env->regs[6]);
308 (*regs)[7] = tswapreg(env->regs[7]);
309 (*regs)[8] = tswapreg(env->regs[8]);
310 (*regs)[9] = tswapreg(env->regs[9]);
311 (*regs)[10] = tswapreg(env->regs[10]);
312 (*regs)[11] = tswapreg(env->regs[11]);
313 (*regs)[12] = tswapreg(env->regs[12]);
314 (*regs)[13] = tswapreg(env->regs[13]);
315 (*regs)[14] = tswapreg(env->regs[14]);
316 (*regs)[15] = tswapreg(env->regs[15]);
318 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
319 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
322 #define USE_ELF_CORE_DUMP
323 #define ELF_EXEC_PAGESIZE 4096
325 enum
327 ARM_HWCAP_ARM_SWP = 1 << 0,
328 ARM_HWCAP_ARM_HALF = 1 << 1,
329 ARM_HWCAP_ARM_THUMB = 1 << 2,
330 ARM_HWCAP_ARM_26BIT = 1 << 3,
331 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
332 ARM_HWCAP_ARM_FPA = 1 << 5,
333 ARM_HWCAP_ARM_VFP = 1 << 6,
334 ARM_HWCAP_ARM_EDSP = 1 << 7,
335 ARM_HWCAP_ARM_JAVA = 1 << 8,
336 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
337 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
338 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
339 ARM_HWCAP_ARM_NEON = 1 << 12,
340 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
341 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
342 ARM_HWCAP_ARM_TLS = 1 << 15,
343 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
344 ARM_HWCAP_ARM_IDIVA = 1 << 17,
345 ARM_HWCAP_ARM_IDIVT = 1 << 18,
346 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
347 ARM_HWCAP_ARM_LPAE = 1 << 20,
348 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
351 enum {
352 ARM_HWCAP2_ARM_AES = 1 << 0,
353 ARM_HWCAP2_ARM_PMULL = 1 << 1,
354 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
355 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
356 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
359 /* The commpage only exists for 32 bit kernels */
361 #define TARGET_HAS_VALIDATE_GUEST_SPACE
362 /* Return 1 if the proposed guest space is suitable for the guest.
363 * Return 0 if the proposed guest space isn't suitable, but another
364 * address space should be tried.
365 * Return -1 if there is no way the proposed guest space can be
366 * valid regardless of the base.
367 * The guest code may leave a page mapped and populate it if the
368 * address is suitable.
370 static int validate_guest_space(unsigned long guest_base,
371 unsigned long guest_size)
373 unsigned long real_start, test_page_addr;
375 /* We need to check that we can force a fault on access to the
376 * commpage at 0xffff0fxx
378 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
380 /* If the commpage lies within the already allocated guest space,
381 * then there is no way we can allocate it.
383 if (test_page_addr >= guest_base
384 && test_page_addr <= (guest_base + guest_size)) {
385 return -1;
388 /* Note it needs to be writeable to let us initialise it */
389 real_start = (unsigned long)
390 mmap((void *)test_page_addr, qemu_host_page_size,
391 PROT_READ | PROT_WRITE,
392 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
394 /* If we can't map it then try another address */
395 if (real_start == -1ul) {
396 return 0;
399 if (real_start != test_page_addr) {
400 /* OS didn't put the page where we asked - unmap and reject */
401 munmap((void *)real_start, qemu_host_page_size);
402 return 0;
405 /* Leave the page mapped
406 * Populate it (mmap should have left it all 0'd)
409 /* Kernel helper versions */
410 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
412 /* Now it's populated make it RO */
413 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
414 perror("Protecting guest commpage");
415 exit(-1);
418 return 1; /* All good */
421 #define ELF_HWCAP get_elf_hwcap()
422 #define ELF_HWCAP2 get_elf_hwcap2()
424 static uint32_t get_elf_hwcap(void)
426 ARMCPU *cpu = ARM_CPU(thread_cpu);
427 uint32_t hwcaps = 0;
429 hwcaps |= ARM_HWCAP_ARM_SWP;
430 hwcaps |= ARM_HWCAP_ARM_HALF;
431 hwcaps |= ARM_HWCAP_ARM_THUMB;
432 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
434 /* probe for the extra features */
435 #define GET_FEATURE(feat, hwcap) \
436 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
437 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
438 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
439 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
440 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
441 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
442 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
443 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
444 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
445 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
446 GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA);
447 GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT);
448 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
449 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
450 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
451 * to our VFP_FP16 feature bit.
453 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
454 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
456 return hwcaps;
459 static uint32_t get_elf_hwcap2(void)
461 ARMCPU *cpu = ARM_CPU(thread_cpu);
462 uint32_t hwcaps = 0;
464 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP2_ARM_AES);
465 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP2_ARM_PMULL);
466 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP2_ARM_SHA1);
467 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP2_ARM_SHA2);
468 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP2_ARM_CRC32);
469 return hwcaps;
472 #undef GET_FEATURE
474 #else
475 /* 64 bit ARM definitions */
476 #define ELF_START_MMAP 0x80000000
478 #define elf_check_arch(x) ((x) == ELF_MACHINE)
480 #define ELF_ARCH ELF_MACHINE
481 #define ELF_CLASS ELFCLASS64
482 #define ELF_PLATFORM "aarch64"
484 static inline void init_thread(struct target_pt_regs *regs,
485 struct image_info *infop)
487 abi_long stack = infop->start_stack;
488 memset(regs, 0, sizeof(*regs));
490 regs->pc = infop->entry & ~0x3ULL;
491 regs->sp = stack;
494 #define ELF_NREG 34
495 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
497 static void elf_core_copy_regs(target_elf_gregset_t *regs,
498 const CPUARMState *env)
500 int i;
502 for (i = 0; i < 32; i++) {
503 (*regs)[i] = tswapreg(env->xregs[i]);
505 (*regs)[32] = tswapreg(env->pc);
506 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
509 #define USE_ELF_CORE_DUMP
510 #define ELF_EXEC_PAGESIZE 4096
512 enum {
513 ARM_HWCAP_A64_FP = 1 << 0,
514 ARM_HWCAP_A64_ASIMD = 1 << 1,
515 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
516 ARM_HWCAP_A64_AES = 1 << 3,
517 ARM_HWCAP_A64_PMULL = 1 << 4,
518 ARM_HWCAP_A64_SHA1 = 1 << 5,
519 ARM_HWCAP_A64_SHA2 = 1 << 6,
520 ARM_HWCAP_A64_CRC32 = 1 << 7,
523 #define ELF_HWCAP get_elf_hwcap()
525 static uint32_t get_elf_hwcap(void)
527 ARMCPU *cpu = ARM_CPU(thread_cpu);
528 uint32_t hwcaps = 0;
530 hwcaps |= ARM_HWCAP_A64_FP;
531 hwcaps |= ARM_HWCAP_A64_ASIMD;
533 /* probe for the extra features */
534 #define GET_FEATURE(feat, hwcap) \
535 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
536 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP_A64_AES);
537 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP_A64_PMULL);
538 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP_A64_SHA1);
539 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP_A64_SHA2);
540 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP_A64_CRC32);
541 #undef GET_FEATURE
543 return hwcaps;
546 #endif /* not TARGET_AARCH64 */
547 #endif /* TARGET_ARM */
549 #ifdef TARGET_UNICORE32
551 #define ELF_START_MMAP 0x80000000
553 #define elf_check_arch(x) ((x) == EM_UNICORE32)
555 #define ELF_CLASS ELFCLASS32
556 #define ELF_DATA ELFDATA2LSB
557 #define ELF_ARCH EM_UNICORE32
559 static inline void init_thread(struct target_pt_regs *regs,
560 struct image_info *infop)
562 abi_long stack = infop->start_stack;
563 memset(regs, 0, sizeof(*regs));
564 regs->UC32_REG_asr = 0x10;
565 regs->UC32_REG_pc = infop->entry & 0xfffffffe;
566 regs->UC32_REG_sp = infop->start_stack;
567 /* FIXME - what to for failure of get_user()? */
568 get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */
569 get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */
570 /* XXX: it seems that r0 is zeroed after ! */
571 regs->UC32_REG_00 = 0;
574 #define ELF_NREG 34
575 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
577 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
579 (*regs)[0] = env->regs[0];
580 (*regs)[1] = env->regs[1];
581 (*regs)[2] = env->regs[2];
582 (*regs)[3] = env->regs[3];
583 (*regs)[4] = env->regs[4];
584 (*regs)[5] = env->regs[5];
585 (*regs)[6] = env->regs[6];
586 (*regs)[7] = env->regs[7];
587 (*regs)[8] = env->regs[8];
588 (*regs)[9] = env->regs[9];
589 (*regs)[10] = env->regs[10];
590 (*regs)[11] = env->regs[11];
591 (*regs)[12] = env->regs[12];
592 (*regs)[13] = env->regs[13];
593 (*regs)[14] = env->regs[14];
594 (*regs)[15] = env->regs[15];
595 (*regs)[16] = env->regs[16];
596 (*regs)[17] = env->regs[17];
597 (*regs)[18] = env->regs[18];
598 (*regs)[19] = env->regs[19];
599 (*regs)[20] = env->regs[20];
600 (*regs)[21] = env->regs[21];
601 (*regs)[22] = env->regs[22];
602 (*regs)[23] = env->regs[23];
603 (*regs)[24] = env->regs[24];
604 (*regs)[25] = env->regs[25];
605 (*regs)[26] = env->regs[26];
606 (*regs)[27] = env->regs[27];
607 (*regs)[28] = env->regs[28];
608 (*regs)[29] = env->regs[29];
609 (*regs)[30] = env->regs[30];
610 (*regs)[31] = env->regs[31];
612 (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
613 (*regs)[33] = env->regs[0]; /* XXX */
616 #define USE_ELF_CORE_DUMP
617 #define ELF_EXEC_PAGESIZE 4096
619 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
621 #endif
623 #ifdef TARGET_SPARC
624 #ifdef TARGET_SPARC64
626 #define ELF_START_MMAP 0x80000000
627 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
628 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
629 #ifndef TARGET_ABI32
630 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
631 #else
632 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
633 #endif
635 #define ELF_CLASS ELFCLASS64
636 #define ELF_ARCH EM_SPARCV9
638 #define STACK_BIAS 2047
640 static inline void init_thread(struct target_pt_regs *regs,
641 struct image_info *infop)
643 #ifndef TARGET_ABI32
644 regs->tstate = 0;
645 #endif
646 regs->pc = infop->entry;
647 regs->npc = regs->pc + 4;
648 regs->y = 0;
649 #ifdef TARGET_ABI32
650 regs->u_regs[14] = infop->start_stack - 16 * 4;
651 #else
652 if (personality(infop->personality) == PER_LINUX32)
653 regs->u_regs[14] = infop->start_stack - 16 * 4;
654 else
655 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
656 #endif
659 #else
660 #define ELF_START_MMAP 0x80000000
661 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
662 | HWCAP_SPARC_MULDIV)
663 #define elf_check_arch(x) ( (x) == EM_SPARC )
665 #define ELF_CLASS ELFCLASS32
666 #define ELF_ARCH EM_SPARC
668 static inline void init_thread(struct target_pt_regs *regs,
669 struct image_info *infop)
671 regs->psr = 0;
672 regs->pc = infop->entry;
673 regs->npc = regs->pc + 4;
674 regs->y = 0;
675 regs->u_regs[14] = infop->start_stack - 16 * 4;
678 #endif
679 #endif
681 #ifdef TARGET_PPC
683 #define ELF_START_MMAP 0x80000000
685 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
687 #define elf_check_arch(x) ( (x) == EM_PPC64 )
689 #define ELF_CLASS ELFCLASS64
691 #else
693 #define elf_check_arch(x) ( (x) == EM_PPC )
695 #define ELF_CLASS ELFCLASS32
697 #endif
699 #define ELF_ARCH EM_PPC
701 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
702 See arch/powerpc/include/asm/cputable.h. */
703 enum {
704 QEMU_PPC_FEATURE_32 = 0x80000000,
705 QEMU_PPC_FEATURE_64 = 0x40000000,
706 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
707 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
708 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
709 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
710 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
711 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
712 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
713 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
714 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
715 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
716 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
717 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
718 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
719 QEMU_PPC_FEATURE_CELL = 0x00010000,
720 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
721 QEMU_PPC_FEATURE_SMT = 0x00004000,
722 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
723 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
724 QEMU_PPC_FEATURE_PA6T = 0x00000800,
725 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
726 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
727 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
728 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
729 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
731 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
732 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
734 /* Feature definitions in AT_HWCAP2. */
735 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
736 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
737 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
738 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
739 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
740 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
743 #define ELF_HWCAP get_elf_hwcap()
745 static uint32_t get_elf_hwcap(void)
747 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
748 uint32_t features = 0;
750 /* We don't have to be terribly complete here; the high points are
751 Altivec/FP/SPE support. Anything else is just a bonus. */
752 #define GET_FEATURE(flag, feature) \
753 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
754 #define GET_FEATURE2(flag, feature) \
755 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
756 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
757 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
758 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
759 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
760 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
761 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
762 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
763 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
764 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
765 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
766 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
767 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
768 QEMU_PPC_FEATURE_ARCH_2_06);
769 #undef GET_FEATURE
770 #undef GET_FEATURE2
772 return features;
775 #define ELF_HWCAP2 get_elf_hwcap2()
777 static uint32_t get_elf_hwcap2(void)
779 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
780 uint32_t features = 0;
782 #define GET_FEATURE(flag, feature) \
783 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
784 #define GET_FEATURE2(flag, feature) \
785 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
787 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
788 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
789 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
790 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07);
792 #undef GET_FEATURE
793 #undef GET_FEATURE2
795 return features;
799 * The requirements here are:
800 * - keep the final alignment of sp (sp & 0xf)
801 * - make sure the 32-bit value at the first 16 byte aligned position of
802 * AUXV is greater than 16 for glibc compatibility.
803 * AT_IGNOREPPC is used for that.
804 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
805 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
807 #define DLINFO_ARCH_ITEMS 5
808 #define ARCH_DLINFO \
809 do { \
810 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
811 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
812 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
813 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
814 /* \
815 * Now handle glibc compatibility. \
816 */ \
817 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
818 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
819 } while (0)
821 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
823 _regs->gpr[1] = infop->start_stack;
824 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
825 if (get_ppc64_abi(infop) < 2) {
826 uint64_t val;
827 get_user_u64(val, infop->entry + 8);
828 _regs->gpr[2] = val + infop->load_bias;
829 get_user_u64(val, infop->entry);
830 infop->entry = val + infop->load_bias;
831 } else {
832 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
834 #endif
835 _regs->nip = infop->entry;
838 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
839 #define ELF_NREG 48
840 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
842 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
844 int i;
845 target_ulong ccr = 0;
847 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
848 (*regs)[i] = tswapreg(env->gpr[i]);
851 (*regs)[32] = tswapreg(env->nip);
852 (*regs)[33] = tswapreg(env->msr);
853 (*regs)[35] = tswapreg(env->ctr);
854 (*regs)[36] = tswapreg(env->lr);
855 (*regs)[37] = tswapreg(env->xer);
857 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
858 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
860 (*regs)[38] = tswapreg(ccr);
863 #define USE_ELF_CORE_DUMP
864 #define ELF_EXEC_PAGESIZE 4096
866 #endif
868 #ifdef TARGET_MIPS
870 #define ELF_START_MMAP 0x80000000
872 #define elf_check_arch(x) ( (x) == EM_MIPS )
874 #ifdef TARGET_MIPS64
875 #define ELF_CLASS ELFCLASS64
876 #else
877 #define ELF_CLASS ELFCLASS32
878 #endif
879 #define ELF_ARCH EM_MIPS
881 static inline void init_thread(struct target_pt_regs *regs,
882 struct image_info *infop)
884 regs->cp0_status = 2 << CP0St_KSU;
885 regs->cp0_epc = infop->entry;
886 regs->regs[29] = infop->start_stack;
889 /* See linux kernel: arch/mips/include/asm/elf.h. */
890 #define ELF_NREG 45
891 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
893 /* See linux kernel: arch/mips/include/asm/reg.h. */
894 enum {
895 #ifdef TARGET_MIPS64
896 TARGET_EF_R0 = 0,
897 #else
898 TARGET_EF_R0 = 6,
899 #endif
900 TARGET_EF_R26 = TARGET_EF_R0 + 26,
901 TARGET_EF_R27 = TARGET_EF_R0 + 27,
902 TARGET_EF_LO = TARGET_EF_R0 + 32,
903 TARGET_EF_HI = TARGET_EF_R0 + 33,
904 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
905 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
906 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
907 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
910 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
911 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
913 int i;
915 for (i = 0; i < TARGET_EF_R0; i++) {
916 (*regs)[i] = 0;
918 (*regs)[TARGET_EF_R0] = 0;
920 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
921 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
924 (*regs)[TARGET_EF_R26] = 0;
925 (*regs)[TARGET_EF_R27] = 0;
926 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
927 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
928 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
929 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
930 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
931 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
934 #define USE_ELF_CORE_DUMP
935 #define ELF_EXEC_PAGESIZE 4096
937 #endif /* TARGET_MIPS */
939 #ifdef TARGET_MICROBLAZE
941 #define ELF_START_MMAP 0x80000000
943 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
945 #define ELF_CLASS ELFCLASS32
946 #define ELF_ARCH EM_MICROBLAZE
948 static inline void init_thread(struct target_pt_regs *regs,
949 struct image_info *infop)
951 regs->pc = infop->entry;
952 regs->r1 = infop->start_stack;
956 #define ELF_EXEC_PAGESIZE 4096
958 #define USE_ELF_CORE_DUMP
959 #define ELF_NREG 38
960 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
962 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
963 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
965 int i, pos = 0;
967 for (i = 0; i < 32; i++) {
968 (*regs)[pos++] = tswapreg(env->regs[i]);
971 for (i = 0; i < 6; i++) {
972 (*regs)[pos++] = tswapreg(env->sregs[i]);
976 #endif /* TARGET_MICROBLAZE */
978 #ifdef TARGET_OPENRISC
980 #define ELF_START_MMAP 0x08000000
982 #define elf_check_arch(x) ((x) == EM_OPENRISC)
984 #define ELF_ARCH EM_OPENRISC
985 #define ELF_CLASS ELFCLASS32
986 #define ELF_DATA ELFDATA2MSB
988 static inline void init_thread(struct target_pt_regs *regs,
989 struct image_info *infop)
991 regs->pc = infop->entry;
992 regs->gpr[1] = infop->start_stack;
995 #define USE_ELF_CORE_DUMP
996 #define ELF_EXEC_PAGESIZE 8192
998 /* See linux kernel arch/openrisc/include/asm/elf.h. */
999 #define ELF_NREG 34 /* gprs and pc, sr */
1000 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1002 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1003 const CPUOpenRISCState *env)
1005 int i;
1007 for (i = 0; i < 32; i++) {
1008 (*regs)[i] = tswapreg(env->gpr[i]);
1011 (*regs)[32] = tswapreg(env->pc);
1012 (*regs)[33] = tswapreg(env->sr);
1014 #define ELF_HWCAP 0
1015 #define ELF_PLATFORM NULL
1017 #endif /* TARGET_OPENRISC */
1019 #ifdef TARGET_SH4
1021 #define ELF_START_MMAP 0x80000000
1023 #define elf_check_arch(x) ( (x) == EM_SH )
1025 #define ELF_CLASS ELFCLASS32
1026 #define ELF_ARCH EM_SH
1028 static inline void init_thread(struct target_pt_regs *regs,
1029 struct image_info *infop)
1031 /* Check other registers XXXXX */
1032 regs->pc = infop->entry;
1033 regs->regs[15] = infop->start_stack;
1036 /* See linux kernel: arch/sh/include/asm/elf.h. */
1037 #define ELF_NREG 23
1038 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1040 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1041 enum {
1042 TARGET_REG_PC = 16,
1043 TARGET_REG_PR = 17,
1044 TARGET_REG_SR = 18,
1045 TARGET_REG_GBR = 19,
1046 TARGET_REG_MACH = 20,
1047 TARGET_REG_MACL = 21,
1048 TARGET_REG_SYSCALL = 22
1051 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1052 const CPUSH4State *env)
1054 int i;
1056 for (i = 0; i < 16; i++) {
1057 (*regs[i]) = tswapreg(env->gregs[i]);
1060 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1061 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1062 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1063 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1064 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1065 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1066 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1069 #define USE_ELF_CORE_DUMP
1070 #define ELF_EXEC_PAGESIZE 4096
1072 enum {
1073 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1074 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1075 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1076 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1077 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1078 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1079 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1080 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1081 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1082 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1085 #define ELF_HWCAP get_elf_hwcap()
1087 static uint32_t get_elf_hwcap(void)
1089 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1090 uint32_t hwcap = 0;
1092 hwcap |= SH_CPU_HAS_FPU;
1094 if (cpu->env.features & SH_FEATURE_SH4A) {
1095 hwcap |= SH_CPU_HAS_LLSC;
1098 return hwcap;
1101 #endif
1103 #ifdef TARGET_CRIS
1105 #define ELF_START_MMAP 0x80000000
1107 #define elf_check_arch(x) ( (x) == EM_CRIS )
1109 #define ELF_CLASS ELFCLASS32
1110 #define ELF_ARCH EM_CRIS
1112 static inline void init_thread(struct target_pt_regs *regs,
1113 struct image_info *infop)
1115 regs->erp = infop->entry;
1118 #define ELF_EXEC_PAGESIZE 8192
1120 #endif
1122 #ifdef TARGET_M68K
1124 #define ELF_START_MMAP 0x80000000
1126 #define elf_check_arch(x) ( (x) == EM_68K )
1128 #define ELF_CLASS ELFCLASS32
1129 #define ELF_ARCH EM_68K
1131 /* ??? Does this need to do anything?
1132 #define ELF_PLAT_INIT(_r) */
1134 static inline void init_thread(struct target_pt_regs *regs,
1135 struct image_info *infop)
1137 regs->usp = infop->start_stack;
1138 regs->sr = 0;
1139 regs->pc = infop->entry;
1142 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1143 #define ELF_NREG 20
1144 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1146 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1148 (*regs)[0] = tswapreg(env->dregs[1]);
1149 (*regs)[1] = tswapreg(env->dregs[2]);
1150 (*regs)[2] = tswapreg(env->dregs[3]);
1151 (*regs)[3] = tswapreg(env->dregs[4]);
1152 (*regs)[4] = tswapreg(env->dregs[5]);
1153 (*regs)[5] = tswapreg(env->dregs[6]);
1154 (*regs)[6] = tswapreg(env->dregs[7]);
1155 (*regs)[7] = tswapreg(env->aregs[0]);
1156 (*regs)[8] = tswapreg(env->aregs[1]);
1157 (*regs)[9] = tswapreg(env->aregs[2]);
1158 (*regs)[10] = tswapreg(env->aregs[3]);
1159 (*regs)[11] = tswapreg(env->aregs[4]);
1160 (*regs)[12] = tswapreg(env->aregs[5]);
1161 (*regs)[13] = tswapreg(env->aregs[6]);
1162 (*regs)[14] = tswapreg(env->dregs[0]);
1163 (*regs)[15] = tswapreg(env->aregs[7]);
1164 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1165 (*regs)[17] = tswapreg(env->sr);
1166 (*regs)[18] = tswapreg(env->pc);
1167 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1170 #define USE_ELF_CORE_DUMP
1171 #define ELF_EXEC_PAGESIZE 8192
1173 #endif
1175 #ifdef TARGET_ALPHA
1177 #define ELF_START_MMAP (0x30000000000ULL)
1179 #define elf_check_arch(x) ( (x) == ELF_ARCH )
1181 #define ELF_CLASS ELFCLASS64
1182 #define ELF_ARCH EM_ALPHA
1184 static inline void init_thread(struct target_pt_regs *regs,
1185 struct image_info *infop)
1187 regs->pc = infop->entry;
1188 regs->ps = 8;
1189 regs->usp = infop->start_stack;
1192 #define ELF_EXEC_PAGESIZE 8192
1194 #endif /* TARGET_ALPHA */
1196 #ifdef TARGET_S390X
1198 #define ELF_START_MMAP (0x20000000000ULL)
1200 #define elf_check_arch(x) ( (x) == ELF_ARCH )
1202 #define ELF_CLASS ELFCLASS64
1203 #define ELF_DATA ELFDATA2MSB
1204 #define ELF_ARCH EM_S390
1206 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1208 regs->psw.addr = infop->entry;
1209 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1210 regs->gprs[15] = infop->start_stack;
1213 #endif /* TARGET_S390X */
1215 #ifndef ELF_PLATFORM
1216 #define ELF_PLATFORM (NULL)
1217 #endif
1219 #ifndef ELF_HWCAP
1220 #define ELF_HWCAP 0
1221 #endif
1223 #ifdef TARGET_ABI32
1224 #undef ELF_CLASS
1225 #define ELF_CLASS ELFCLASS32
1226 #undef bswaptls
1227 #define bswaptls(ptr) bswap32s(ptr)
1228 #endif
1230 #include "elf.h"
1232 struct exec
1234 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1235 unsigned int a_text; /* length of text, in bytes */
1236 unsigned int a_data; /* length of data, in bytes */
1237 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1238 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1239 unsigned int a_entry; /* start address */
1240 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1241 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1245 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1246 #define OMAGIC 0407
1247 #define NMAGIC 0410
1248 #define ZMAGIC 0413
1249 #define QMAGIC 0314
1251 /* Necessary parameters */
1252 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1253 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1254 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1255 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1257 #define DLINFO_ITEMS 14
1259 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1261 memcpy(to, from, n);
1264 #ifdef BSWAP_NEEDED
1265 static void bswap_ehdr(struct elfhdr *ehdr)
1267 bswap16s(&ehdr->e_type); /* Object file type */
1268 bswap16s(&ehdr->e_machine); /* Architecture */
1269 bswap32s(&ehdr->e_version); /* Object file version */
1270 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1271 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1272 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1273 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1274 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1275 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1276 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1277 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1278 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1279 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1282 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1284 int i;
1285 for (i = 0; i < phnum; ++i, ++phdr) {
1286 bswap32s(&phdr->p_type); /* Segment type */
1287 bswap32s(&phdr->p_flags); /* Segment flags */
1288 bswaptls(&phdr->p_offset); /* Segment file offset */
1289 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1290 bswaptls(&phdr->p_paddr); /* Segment physical address */
1291 bswaptls(&phdr->p_filesz); /* Segment size in file */
1292 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1293 bswaptls(&phdr->p_align); /* Segment alignment */
1297 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1299 int i;
1300 for (i = 0; i < shnum; ++i, ++shdr) {
1301 bswap32s(&shdr->sh_name);
1302 bswap32s(&shdr->sh_type);
1303 bswaptls(&shdr->sh_flags);
1304 bswaptls(&shdr->sh_addr);
1305 bswaptls(&shdr->sh_offset);
1306 bswaptls(&shdr->sh_size);
1307 bswap32s(&shdr->sh_link);
1308 bswap32s(&shdr->sh_info);
1309 bswaptls(&shdr->sh_addralign);
1310 bswaptls(&shdr->sh_entsize);
1314 static void bswap_sym(struct elf_sym *sym)
1316 bswap32s(&sym->st_name);
1317 bswaptls(&sym->st_value);
1318 bswaptls(&sym->st_size);
1319 bswap16s(&sym->st_shndx);
1321 #else
1322 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1323 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1324 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1325 static inline void bswap_sym(struct elf_sym *sym) { }
1326 #endif
1328 #ifdef USE_ELF_CORE_DUMP
1329 static int elf_core_dump(int, const CPUArchState *);
1330 #endif /* USE_ELF_CORE_DUMP */
1331 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1333 /* Verify the portions of EHDR within E_IDENT for the target.
1334 This can be performed before bswapping the entire header. */
1335 static bool elf_check_ident(struct elfhdr *ehdr)
1337 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1338 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1339 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1340 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1341 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1342 && ehdr->e_ident[EI_DATA] == ELF_DATA
1343 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1346 /* Verify the portions of EHDR outside of E_IDENT for the target.
1347 This has to wait until after bswapping the header. */
1348 static bool elf_check_ehdr(struct elfhdr *ehdr)
1350 return (elf_check_arch(ehdr->e_machine)
1351 && ehdr->e_ehsize == sizeof(struct elfhdr)
1352 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1353 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1357 * 'copy_elf_strings()' copies argument/envelope strings from user
1358 * memory to free pages in kernel mem. These are in a format ready
1359 * to be put directly into the top of new user memory.
1362 static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
1363 abi_ulong p)
1365 char *tmp, *tmp1, *pag = NULL;
1366 int len, offset = 0;
1368 if (!p) {
1369 return 0; /* bullet-proofing */
1371 while (argc-- > 0) {
1372 tmp = argv[argc];
1373 if (!tmp) {
1374 fprintf(stderr, "VFS: argc is wrong");
1375 exit(-1);
1377 tmp1 = tmp;
1378 while (*tmp++);
1379 len = tmp - tmp1;
1380 if (p < len) { /* this shouldn't happen - 128kB */
1381 return 0;
1383 while (len) {
1384 --p; --tmp; --len;
1385 if (--offset < 0) {
1386 offset = p % TARGET_PAGE_SIZE;
1387 pag = (char *)page[p/TARGET_PAGE_SIZE];
1388 if (!pag) {
1389 pag = g_try_malloc0(TARGET_PAGE_SIZE);
1390 page[p/TARGET_PAGE_SIZE] = pag;
1391 if (!pag)
1392 return 0;
1395 if (len == 0 || offset == 0) {
1396 *(pag + offset) = *tmp;
1398 else {
1399 int bytes_to_copy = (len > offset) ? offset : len;
1400 tmp -= bytes_to_copy;
1401 p -= bytes_to_copy;
1402 offset -= bytes_to_copy;
1403 len -= bytes_to_copy;
1404 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
1408 return p;
1411 static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
1412 struct image_info *info)
1414 abi_ulong stack_base, size, error, guard;
1415 int i;
1417 /* Create enough stack to hold everything. If we don't use
1418 it for args, we'll use it for something else. */
1419 size = guest_stack_size;
1420 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) {
1421 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1423 guard = TARGET_PAGE_SIZE;
1424 if (guard < qemu_real_host_page_size) {
1425 guard = qemu_real_host_page_size;
1428 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1429 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1430 if (error == -1) {
1431 perror("mmap stack");
1432 exit(-1);
1435 /* We reserve one extra page at the top of the stack as guard. */
1436 target_mprotect(error, guard, PROT_NONE);
1438 info->stack_limit = error + guard;
1439 stack_base = info->stack_limit + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1440 p += stack_base;
1442 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1443 if (bprm->page[i]) {
1444 info->rss++;
1445 /* FIXME - check return value of memcpy_to_target() for failure */
1446 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
1447 g_free(bprm->page[i]);
1449 stack_base += TARGET_PAGE_SIZE;
1451 return p;
1454 /* Map and zero the bss. We need to explicitly zero any fractional pages
1455 after the data section (i.e. bss). */
1456 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1458 uintptr_t host_start, host_map_start, host_end;
1460 last_bss = TARGET_PAGE_ALIGN(last_bss);
1462 /* ??? There is confusion between qemu_real_host_page_size and
1463 qemu_host_page_size here and elsewhere in target_mmap, which
1464 may lead to the end of the data section mapping from the file
1465 not being mapped. At least there was an explicit test and
1466 comment for that here, suggesting that "the file size must
1467 be known". The comment probably pre-dates the introduction
1468 of the fstat system call in target_mmap which does in fact
1469 find out the size. What isn't clear is if the workaround
1470 here is still actually needed. For now, continue with it,
1471 but merge it with the "normal" mmap that would allocate the bss. */
1473 host_start = (uintptr_t) g2h(elf_bss);
1474 host_end = (uintptr_t) g2h(last_bss);
1475 host_map_start = (host_start + qemu_real_host_page_size - 1);
1476 host_map_start &= -qemu_real_host_page_size;
1478 if (host_map_start < host_end) {
1479 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1480 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1481 if (p == MAP_FAILED) {
1482 perror("cannot mmap brk");
1483 exit(-1);
1487 /* Ensure that the bss page(s) are valid */
1488 if ((page_get_flags(last_bss-1) & prot) != prot) {
1489 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1492 if (host_start < host_map_start) {
1493 memset((void *)host_start, 0, host_map_start - host_start);
1497 #ifdef CONFIG_USE_FDPIC
1498 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1500 uint16_t n;
1501 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1503 /* elf32_fdpic_loadseg */
1504 n = info->nsegs;
1505 while (n--) {
1506 sp -= 12;
1507 put_user_u32(loadsegs[n].addr, sp+0);
1508 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1509 put_user_u32(loadsegs[n].p_memsz, sp+8);
1512 /* elf32_fdpic_loadmap */
1513 sp -= 4;
1514 put_user_u16(0, sp+0); /* version */
1515 put_user_u16(info->nsegs, sp+2); /* nsegs */
1517 info->personality = PER_LINUX_FDPIC;
1518 info->loadmap_addr = sp;
1520 return sp;
1522 #endif
1524 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1525 struct elfhdr *exec,
1526 struct image_info *info,
1527 struct image_info *interp_info)
1529 abi_ulong sp;
1530 abi_ulong sp_auxv;
1531 int size;
1532 int i;
1533 abi_ulong u_rand_bytes;
1534 uint8_t k_rand_bytes[16];
1535 abi_ulong u_platform;
1536 const char *k_platform;
1537 const int n = sizeof(elf_addr_t);
1539 sp = p;
1541 #ifdef CONFIG_USE_FDPIC
1542 /* Needs to be before we load the env/argc/... */
1543 if (elf_is_fdpic(exec)) {
1544 /* Need 4 byte alignment for these structs */
1545 sp &= ~3;
1546 sp = loader_build_fdpic_loadmap(info, sp);
1547 info->other_info = interp_info;
1548 if (interp_info) {
1549 interp_info->other_info = info;
1550 sp = loader_build_fdpic_loadmap(interp_info, sp);
1553 #endif
1555 u_platform = 0;
1556 k_platform = ELF_PLATFORM;
1557 if (k_platform) {
1558 size_t len = strlen(k_platform) + 1;
1559 sp -= (len + n - 1) & ~(n - 1);
1560 u_platform = sp;
1561 /* FIXME - check return value of memcpy_to_target() for failure */
1562 memcpy_to_target(sp, k_platform, len);
1566 * Generate 16 random bytes for userspace PRNG seeding (not
1567 * cryptically secure but it's not the aim of QEMU).
1569 for (i = 0; i < 16; i++) {
1570 k_rand_bytes[i] = rand();
1572 sp -= 16;
1573 u_rand_bytes = sp;
1574 /* FIXME - check return value of memcpy_to_target() for failure */
1575 memcpy_to_target(sp, k_rand_bytes, 16);
1578 * Force 16 byte _final_ alignment here for generality.
1580 sp = sp &~ (abi_ulong)15;
1581 size = (DLINFO_ITEMS + 1) * 2;
1582 if (k_platform)
1583 size += 2;
1584 #ifdef DLINFO_ARCH_ITEMS
1585 size += DLINFO_ARCH_ITEMS * 2;
1586 #endif
1587 #ifdef ELF_HWCAP2
1588 size += 2;
1589 #endif
1590 size += envc + argc + 2;
1591 size += 1; /* argc itself */
1592 size *= n;
1593 if (size & 15)
1594 sp -= 16 - (size & 15);
1596 /* This is correct because Linux defines
1597 * elf_addr_t as Elf32_Off / Elf64_Off
1599 #define NEW_AUX_ENT(id, val) do { \
1600 sp -= n; put_user_ual(val, sp); \
1601 sp -= n; put_user_ual(id, sp); \
1602 } while(0)
1604 sp_auxv = sp;
1605 NEW_AUX_ENT (AT_NULL, 0);
1607 /* There must be exactly DLINFO_ITEMS entries here. */
1608 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1609 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1610 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1611 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize())));
1612 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1613 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1614 NEW_AUX_ENT(AT_ENTRY, info->entry);
1615 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1616 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1617 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1618 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1619 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1620 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1621 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1623 #ifdef ELF_HWCAP2
1624 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1625 #endif
1627 if (k_platform)
1628 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1629 #ifdef ARCH_DLINFO
1631 * ARCH_DLINFO must come last so platform specific code can enforce
1632 * special alignment requirements on the AUXV if necessary (eg. PPC).
1634 ARCH_DLINFO;
1635 #endif
1636 #undef NEW_AUX_ENT
1638 info->saved_auxv = sp;
1639 info->auxv_len = sp_auxv - sp;
1641 sp = loader_build_argptr(envc, argc, sp, p, 0);
1642 /* Check the right amount of stack was allocated for auxvec, envp & argv. */
1643 assert(sp_auxv - sp == size);
1644 return sp;
1647 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1648 /* If the guest doesn't have a validation function just agree */
1649 static int validate_guest_space(unsigned long guest_base,
1650 unsigned long guest_size)
1652 return 1;
1654 #endif
1656 unsigned long init_guest_space(unsigned long host_start,
1657 unsigned long host_size,
1658 unsigned long guest_start,
1659 bool fixed)
1661 unsigned long current_start, real_start;
1662 int flags;
1664 assert(host_start || host_size);
1666 /* If just a starting address is given, then just verify that
1667 * address. */
1668 if (host_start && !host_size) {
1669 if (validate_guest_space(host_start, host_size) == 1) {
1670 return host_start;
1671 } else {
1672 return (unsigned long)-1;
1676 /* Setup the initial flags and start address. */
1677 current_start = host_start & qemu_host_page_mask;
1678 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1679 if (fixed) {
1680 flags |= MAP_FIXED;
1683 /* Otherwise, a non-zero size region of memory needs to be mapped
1684 * and validated. */
1685 while (1) {
1686 unsigned long real_size = host_size;
1688 /* Do not use mmap_find_vma here because that is limited to the
1689 * guest address space. We are going to make the
1690 * guest address space fit whatever we're given.
1692 real_start = (unsigned long)
1693 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1694 if (real_start == (unsigned long)-1) {
1695 return (unsigned long)-1;
1698 /* Ensure the address is properly aligned. */
1699 if (real_start & ~qemu_host_page_mask) {
1700 munmap((void *)real_start, host_size);
1701 real_size = host_size + qemu_host_page_size;
1702 real_start = (unsigned long)
1703 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1704 if (real_start == (unsigned long)-1) {
1705 return (unsigned long)-1;
1707 real_start = HOST_PAGE_ALIGN(real_start);
1710 /* Check to see if the address is valid. */
1711 if (!host_start || real_start == current_start) {
1712 int valid = validate_guest_space(real_start - guest_start,
1713 real_size);
1714 if (valid == 1) {
1715 break;
1716 } else if (valid == -1) {
1717 return (unsigned long)-1;
1719 /* valid == 0, so try again. */
1722 /* That address didn't work. Unmap and try a different one.
1723 * The address the host picked because is typically right at
1724 * the top of the host address space and leaves the guest with
1725 * no usable address space. Resort to a linear search. We
1726 * already compensated for mmap_min_addr, so this should not
1727 * happen often. Probably means we got unlucky and host
1728 * address space randomization put a shared library somewhere
1729 * inconvenient.
1731 munmap((void *)real_start, host_size);
1732 current_start += qemu_host_page_size;
1733 if (host_start == current_start) {
1734 /* Theoretically possible if host doesn't have any suitably
1735 * aligned areas. Normally the first mmap will fail.
1737 return (unsigned long)-1;
1741 qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size);
1743 return real_start;
1746 static void probe_guest_base(const char *image_name,
1747 abi_ulong loaddr, abi_ulong hiaddr)
1749 /* Probe for a suitable guest base address, if the user has not set
1750 * it explicitly, and set guest_base appropriately.
1751 * In case of error we will print a suitable message and exit.
1753 #if defined(CONFIG_USE_GUEST_BASE)
1754 const char *errmsg;
1755 if (!have_guest_base && !reserved_va) {
1756 unsigned long host_start, real_start, host_size;
1758 /* Round addresses to page boundaries. */
1759 loaddr &= qemu_host_page_mask;
1760 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1762 if (loaddr < mmap_min_addr) {
1763 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1764 } else {
1765 host_start = loaddr;
1766 if (host_start != loaddr) {
1767 errmsg = "Address overflow loading ELF binary";
1768 goto exit_errmsg;
1771 host_size = hiaddr - loaddr;
1773 /* Setup the initial guest memory space with ranges gleaned from
1774 * the ELF image that is being loaded.
1776 real_start = init_guest_space(host_start, host_size, loaddr, false);
1777 if (real_start == (unsigned long)-1) {
1778 errmsg = "Unable to find space for application";
1779 goto exit_errmsg;
1781 guest_base = real_start - loaddr;
1783 qemu_log("Relocating guest address space from 0x"
1784 TARGET_ABI_FMT_lx " to 0x%lx\n",
1785 loaddr, real_start);
1787 return;
1789 exit_errmsg:
1790 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1791 exit(-1);
1792 #endif
1796 /* Load an ELF image into the address space.
1798 IMAGE_NAME is the filename of the image, to use in error messages.
1799 IMAGE_FD is the open file descriptor for the image.
1801 BPRM_BUF is a copy of the beginning of the file; this of course
1802 contains the elf file header at offset 0. It is assumed that this
1803 buffer is sufficiently aligned to present no problems to the host
1804 in accessing data at aligned offsets within the buffer.
1806 On return: INFO values will be filled in, as necessary or available. */
1808 static void load_elf_image(const char *image_name, int image_fd,
1809 struct image_info *info, char **pinterp_name,
1810 char bprm_buf[BPRM_BUF_SIZE])
1812 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1813 struct elf_phdr *phdr;
1814 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1815 int i, retval;
1816 const char *errmsg;
1818 /* First of all, some simple consistency checks */
1819 errmsg = "Invalid ELF image for this architecture";
1820 if (!elf_check_ident(ehdr)) {
1821 goto exit_errmsg;
1823 bswap_ehdr(ehdr);
1824 if (!elf_check_ehdr(ehdr)) {
1825 goto exit_errmsg;
1828 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1829 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1830 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
1831 } else {
1832 phdr = (struct elf_phdr *) alloca(i);
1833 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
1834 if (retval != i) {
1835 goto exit_read;
1838 bswap_phdr(phdr, ehdr->e_phnum);
1840 #ifdef CONFIG_USE_FDPIC
1841 info->nsegs = 0;
1842 info->pt_dynamic_addr = 0;
1843 #endif
1845 /* Find the maximum size of the image and allocate an appropriate
1846 amount of memory to handle that. */
1847 loaddr = -1, hiaddr = 0;
1848 for (i = 0; i < ehdr->e_phnum; ++i) {
1849 if (phdr[i].p_type == PT_LOAD) {
1850 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
1851 if (a < loaddr) {
1852 loaddr = a;
1854 a = phdr[i].p_vaddr + phdr[i].p_memsz;
1855 if (a > hiaddr) {
1856 hiaddr = a;
1858 #ifdef CONFIG_USE_FDPIC
1859 ++info->nsegs;
1860 #endif
1864 load_addr = loaddr;
1865 if (ehdr->e_type == ET_DYN) {
1866 /* The image indicates that it can be loaded anywhere. Find a
1867 location that can hold the memory space required. If the
1868 image is pre-linked, LOADDR will be non-zero. Since we do
1869 not supply MAP_FIXED here we'll use that address if and
1870 only if it remains available. */
1871 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
1872 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
1873 -1, 0);
1874 if (load_addr == -1) {
1875 goto exit_perror;
1877 } else if (pinterp_name != NULL) {
1878 /* This is the main executable. Make sure that the low
1879 address does not conflict with MMAP_MIN_ADDR or the
1880 QEMU application itself. */
1881 probe_guest_base(image_name, loaddr, hiaddr);
1883 load_bias = load_addr - loaddr;
1885 #ifdef CONFIG_USE_FDPIC
1887 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
1888 g_malloc(sizeof(*loadsegs) * info->nsegs);
1890 for (i = 0; i < ehdr->e_phnum; ++i) {
1891 switch (phdr[i].p_type) {
1892 case PT_DYNAMIC:
1893 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
1894 break;
1895 case PT_LOAD:
1896 loadsegs->addr = phdr[i].p_vaddr + load_bias;
1897 loadsegs->p_vaddr = phdr[i].p_vaddr;
1898 loadsegs->p_memsz = phdr[i].p_memsz;
1899 ++loadsegs;
1900 break;
1904 #endif
1906 info->load_bias = load_bias;
1907 info->load_addr = load_addr;
1908 info->entry = ehdr->e_entry + load_bias;
1909 info->start_code = -1;
1910 info->end_code = 0;
1911 info->start_data = -1;
1912 info->end_data = 0;
1913 info->brk = 0;
1914 info->elf_flags = ehdr->e_flags;
1916 for (i = 0; i < ehdr->e_phnum; i++) {
1917 struct elf_phdr *eppnt = phdr + i;
1918 if (eppnt->p_type == PT_LOAD) {
1919 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
1920 int elf_prot = 0;
1922 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1923 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1924 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1926 vaddr = load_bias + eppnt->p_vaddr;
1927 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
1928 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
1930 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
1931 elf_prot, MAP_PRIVATE | MAP_FIXED,
1932 image_fd, eppnt->p_offset - vaddr_po);
1933 if (error == -1) {
1934 goto exit_perror;
1937 vaddr_ef = vaddr + eppnt->p_filesz;
1938 vaddr_em = vaddr + eppnt->p_memsz;
1940 /* If the load segment requests extra zeros (e.g. bss), map it. */
1941 if (vaddr_ef < vaddr_em) {
1942 zero_bss(vaddr_ef, vaddr_em, elf_prot);
1945 /* Find the full program boundaries. */
1946 if (elf_prot & PROT_EXEC) {
1947 if (vaddr < info->start_code) {
1948 info->start_code = vaddr;
1950 if (vaddr_ef > info->end_code) {
1951 info->end_code = vaddr_ef;
1954 if (elf_prot & PROT_WRITE) {
1955 if (vaddr < info->start_data) {
1956 info->start_data = vaddr;
1958 if (vaddr_ef > info->end_data) {
1959 info->end_data = vaddr_ef;
1961 if (vaddr_em > info->brk) {
1962 info->brk = vaddr_em;
1965 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
1966 char *interp_name;
1968 if (*pinterp_name) {
1969 errmsg = "Multiple PT_INTERP entries";
1970 goto exit_errmsg;
1972 interp_name = malloc(eppnt->p_filesz);
1973 if (!interp_name) {
1974 goto exit_perror;
1977 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
1978 memcpy(interp_name, bprm_buf + eppnt->p_offset,
1979 eppnt->p_filesz);
1980 } else {
1981 retval = pread(image_fd, interp_name, eppnt->p_filesz,
1982 eppnt->p_offset);
1983 if (retval != eppnt->p_filesz) {
1984 goto exit_perror;
1987 if (interp_name[eppnt->p_filesz - 1] != 0) {
1988 errmsg = "Invalid PT_INTERP entry";
1989 goto exit_errmsg;
1991 *pinterp_name = interp_name;
1995 if (info->end_data == 0) {
1996 info->start_data = info->end_code;
1997 info->end_data = info->end_code;
1998 info->brk = info->end_code;
2001 if (qemu_log_enabled()) {
2002 load_symbols(ehdr, image_fd, load_bias);
2005 close(image_fd);
2006 return;
2008 exit_read:
2009 if (retval >= 0) {
2010 errmsg = "Incomplete read of file header";
2011 goto exit_errmsg;
2013 exit_perror:
2014 errmsg = strerror(errno);
2015 exit_errmsg:
2016 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2017 exit(-1);
2020 static void load_elf_interp(const char *filename, struct image_info *info,
2021 char bprm_buf[BPRM_BUF_SIZE])
2023 int fd, retval;
2025 fd = open(path(filename), O_RDONLY);
2026 if (fd < 0) {
2027 goto exit_perror;
2030 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2031 if (retval < 0) {
2032 goto exit_perror;
2034 if (retval < BPRM_BUF_SIZE) {
2035 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2038 load_elf_image(filename, fd, info, NULL, bprm_buf);
2039 return;
2041 exit_perror:
2042 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2043 exit(-1);
2046 static int symfind(const void *s0, const void *s1)
2048 target_ulong addr = *(target_ulong *)s0;
2049 struct elf_sym *sym = (struct elf_sym *)s1;
2050 int result = 0;
2051 if (addr < sym->st_value) {
2052 result = -1;
2053 } else if (addr >= sym->st_value + sym->st_size) {
2054 result = 1;
2056 return result;
2059 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2061 #if ELF_CLASS == ELFCLASS32
2062 struct elf_sym *syms = s->disas_symtab.elf32;
2063 #else
2064 struct elf_sym *syms = s->disas_symtab.elf64;
2065 #endif
2067 // binary search
2068 struct elf_sym *sym;
2070 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2071 if (sym != NULL) {
2072 return s->disas_strtab + sym->st_name;
2075 return "";
2078 /* FIXME: This should use elf_ops.h */
2079 static int symcmp(const void *s0, const void *s1)
2081 struct elf_sym *sym0 = (struct elf_sym *)s0;
2082 struct elf_sym *sym1 = (struct elf_sym *)s1;
2083 return (sym0->st_value < sym1->st_value)
2084 ? -1
2085 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2088 /* Best attempt to load symbols from this ELF object. */
2089 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2091 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2092 struct elf_shdr *shdr;
2093 char *strings = NULL;
2094 struct syminfo *s = NULL;
2095 struct elf_sym *new_syms, *syms = NULL;
2097 shnum = hdr->e_shnum;
2098 i = shnum * sizeof(struct elf_shdr);
2099 shdr = (struct elf_shdr *)alloca(i);
2100 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2101 return;
2104 bswap_shdr(shdr, shnum);
2105 for (i = 0; i < shnum; ++i) {
2106 if (shdr[i].sh_type == SHT_SYMTAB) {
2107 sym_idx = i;
2108 str_idx = shdr[i].sh_link;
2109 goto found;
2113 /* There will be no symbol table if the file was stripped. */
2114 return;
2116 found:
2117 /* Now know where the strtab and symtab are. Snarf them. */
2118 s = malloc(sizeof(*s));
2119 if (!s) {
2120 goto give_up;
2123 i = shdr[str_idx].sh_size;
2124 s->disas_strtab = strings = malloc(i);
2125 if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
2126 goto give_up;
2129 i = shdr[sym_idx].sh_size;
2130 syms = malloc(i);
2131 if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
2132 goto give_up;
2135 nsyms = i / sizeof(struct elf_sym);
2136 for (i = 0; i < nsyms; ) {
2137 bswap_sym(syms + i);
2138 /* Throw away entries which we do not need. */
2139 if (syms[i].st_shndx == SHN_UNDEF
2140 || syms[i].st_shndx >= SHN_LORESERVE
2141 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2142 if (i < --nsyms) {
2143 syms[i] = syms[nsyms];
2145 } else {
2146 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2147 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2148 syms[i].st_value &= ~(target_ulong)1;
2149 #endif
2150 syms[i].st_value += load_bias;
2151 i++;
2155 /* No "useful" symbol. */
2156 if (nsyms == 0) {
2157 goto give_up;
2160 /* Attempt to free the storage associated with the local symbols
2161 that we threw away. Whether or not this has any effect on the
2162 memory allocation depends on the malloc implementation and how
2163 many symbols we managed to discard. */
2164 new_syms = realloc(syms, nsyms * sizeof(*syms));
2165 if (new_syms == NULL) {
2166 goto give_up;
2168 syms = new_syms;
2170 qsort(syms, nsyms, sizeof(*syms), symcmp);
2172 s->disas_num_syms = nsyms;
2173 #if ELF_CLASS == ELFCLASS32
2174 s->disas_symtab.elf32 = syms;
2175 #else
2176 s->disas_symtab.elf64 = syms;
2177 #endif
2178 s->lookup_symbol = lookup_symbolxx;
2179 s->next = syminfos;
2180 syminfos = s;
2182 return;
2184 give_up:
2185 free(s);
2186 free(strings);
2187 free(syms);
2190 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2192 struct image_info interp_info;
2193 struct elfhdr elf_ex;
2194 char *elf_interpreter = NULL;
2196 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2197 info->mmap = 0;
2198 info->rss = 0;
2200 load_elf_image(bprm->filename, bprm->fd, info,
2201 &elf_interpreter, bprm->buf);
2203 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2204 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2205 when we load the interpreter. */
2206 elf_ex = *(struct elfhdr *)bprm->buf;
2208 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
2209 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
2210 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
2211 if (!bprm->p) {
2212 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2213 exit(-1);
2216 /* Do this so that we can load the interpreter, if need be. We will
2217 change some of these later */
2218 bprm->p = setup_arg_pages(bprm->p, bprm, info);
2220 if (elf_interpreter) {
2221 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2223 /* If the program interpreter is one of these two, then assume
2224 an iBCS2 image. Otherwise assume a native linux image. */
2226 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2227 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2228 info->personality = PER_SVR4;
2230 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2231 and some applications "depend" upon this behavior. Since
2232 we do not have the power to recompile these, we emulate
2233 the SVr4 behavior. Sigh. */
2234 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2235 MAP_FIXED | MAP_PRIVATE, -1, 0);
2239 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2240 info, (elf_interpreter ? &interp_info : NULL));
2241 info->start_stack = bprm->p;
2243 /* If we have an interpreter, set that as the program's entry point.
2244 Copy the load_bias as well, to help PPC64 interpret the entry
2245 point as a function descriptor. Do this after creating elf tables
2246 so that we copy the original program entry point into the AUXV. */
2247 if (elf_interpreter) {
2248 info->load_bias = interp_info.load_bias;
2249 info->entry = interp_info.entry;
2250 free(elf_interpreter);
2253 #ifdef USE_ELF_CORE_DUMP
2254 bprm->core_dump = &elf_core_dump;
2255 #endif
2257 return 0;
2260 #ifdef USE_ELF_CORE_DUMP
2262 * Definitions to generate Intel SVR4-like core files.
2263 * These mostly have the same names as the SVR4 types with "target_elf_"
2264 * tacked on the front to prevent clashes with linux definitions,
2265 * and the typedef forms have been avoided. This is mostly like
2266 * the SVR4 structure, but more Linuxy, with things that Linux does
2267 * not support and which gdb doesn't really use excluded.
2269 * Fields we don't dump (their contents is zero) in linux-user qemu
2270 * are marked with XXX.
2272 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2274 * Porting ELF coredump for target is (quite) simple process. First you
2275 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2276 * the target resides):
2278 * #define USE_ELF_CORE_DUMP
2280 * Next you define type of register set used for dumping. ELF specification
2281 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2283 * typedef <target_regtype> target_elf_greg_t;
2284 * #define ELF_NREG <number of registers>
2285 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2287 * Last step is to implement target specific function that copies registers
2288 * from given cpu into just specified register set. Prototype is:
2290 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2291 * const CPUArchState *env);
2293 * Parameters:
2294 * regs - copy register values into here (allocated and zeroed by caller)
2295 * env - copy registers from here
2297 * Example for ARM target is provided in this file.
2300 /* An ELF note in memory */
2301 struct memelfnote {
2302 const char *name;
2303 size_t namesz;
2304 size_t namesz_rounded;
2305 int type;
2306 size_t datasz;
2307 size_t datasz_rounded;
2308 void *data;
2309 size_t notesz;
2312 struct target_elf_siginfo {
2313 abi_int si_signo; /* signal number */
2314 abi_int si_code; /* extra code */
2315 abi_int si_errno; /* errno */
2318 struct target_elf_prstatus {
2319 struct target_elf_siginfo pr_info; /* Info associated with signal */
2320 abi_short pr_cursig; /* Current signal */
2321 abi_ulong pr_sigpend; /* XXX */
2322 abi_ulong pr_sighold; /* XXX */
2323 target_pid_t pr_pid;
2324 target_pid_t pr_ppid;
2325 target_pid_t pr_pgrp;
2326 target_pid_t pr_sid;
2327 struct target_timeval pr_utime; /* XXX User time */
2328 struct target_timeval pr_stime; /* XXX System time */
2329 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2330 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2331 target_elf_gregset_t pr_reg; /* GP registers */
2332 abi_int pr_fpvalid; /* XXX */
2335 #define ELF_PRARGSZ (80) /* Number of chars for args */
2337 struct target_elf_prpsinfo {
2338 char pr_state; /* numeric process state */
2339 char pr_sname; /* char for pr_state */
2340 char pr_zomb; /* zombie */
2341 char pr_nice; /* nice val */
2342 abi_ulong pr_flag; /* flags */
2343 target_uid_t pr_uid;
2344 target_gid_t pr_gid;
2345 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2346 /* Lots missing */
2347 char pr_fname[16]; /* filename of executable */
2348 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2351 /* Here is the structure in which status of each thread is captured. */
2352 struct elf_thread_status {
2353 QTAILQ_ENTRY(elf_thread_status) ets_link;
2354 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2355 #if 0
2356 elf_fpregset_t fpu; /* NT_PRFPREG */
2357 struct task_struct *thread;
2358 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2359 #endif
2360 struct memelfnote notes[1];
2361 int num_notes;
2364 struct elf_note_info {
2365 struct memelfnote *notes;
2366 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2367 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2369 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2370 #if 0
2372 * Current version of ELF coredump doesn't support
2373 * dumping fp regs etc.
2375 elf_fpregset_t *fpu;
2376 elf_fpxregset_t *xfpu;
2377 int thread_status_size;
2378 #endif
2379 int notes_size;
2380 int numnote;
2383 struct vm_area_struct {
2384 target_ulong vma_start; /* start vaddr of memory region */
2385 target_ulong vma_end; /* end vaddr of memory region */
2386 abi_ulong vma_flags; /* protection etc. flags for the region */
2387 QTAILQ_ENTRY(vm_area_struct) vma_link;
2390 struct mm_struct {
2391 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2392 int mm_count; /* number of mappings */
2395 static struct mm_struct *vma_init(void);
2396 static void vma_delete(struct mm_struct *);
2397 static int vma_add_mapping(struct mm_struct *, target_ulong,
2398 target_ulong, abi_ulong);
2399 static int vma_get_mapping_count(const struct mm_struct *);
2400 static struct vm_area_struct *vma_first(const struct mm_struct *);
2401 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2402 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2403 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2404 abi_ulong flags);
2406 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2407 static void fill_note(struct memelfnote *, const char *, int,
2408 unsigned int, void *);
2409 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2410 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2411 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2412 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2413 static size_t note_size(const struct memelfnote *);
2414 static void free_note_info(struct elf_note_info *);
2415 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2416 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2417 static int core_dump_filename(const TaskState *, char *, size_t);
2419 static int dump_write(int, const void *, size_t);
2420 static int write_note(struct memelfnote *, int);
2421 static int write_note_info(struct elf_note_info *, int);
2423 #ifdef BSWAP_NEEDED
2424 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2426 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2427 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2428 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2429 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2430 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2431 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2432 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2433 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2434 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2435 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2436 /* cpu times are not filled, so we skip them */
2437 /* regs should be in correct format already */
2438 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2441 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2443 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2444 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2445 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2446 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2447 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2448 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2449 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2452 static void bswap_note(struct elf_note *en)
2454 bswap32s(&en->n_namesz);
2455 bswap32s(&en->n_descsz);
2456 bswap32s(&en->n_type);
2458 #else
2459 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2460 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2461 static inline void bswap_note(struct elf_note *en) { }
2462 #endif /* BSWAP_NEEDED */
2465 * Minimal support for linux memory regions. These are needed
2466 * when we are finding out what memory exactly belongs to
2467 * emulated process. No locks needed here, as long as
2468 * thread that received the signal is stopped.
2471 static struct mm_struct *vma_init(void)
2473 struct mm_struct *mm;
2475 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2476 return (NULL);
2478 mm->mm_count = 0;
2479 QTAILQ_INIT(&mm->mm_mmap);
2481 return (mm);
2484 static void vma_delete(struct mm_struct *mm)
2486 struct vm_area_struct *vma;
2488 while ((vma = vma_first(mm)) != NULL) {
2489 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2490 g_free(vma);
2492 g_free(mm);
2495 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
2496 target_ulong end, abi_ulong flags)
2498 struct vm_area_struct *vma;
2500 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2501 return (-1);
2503 vma->vma_start = start;
2504 vma->vma_end = end;
2505 vma->vma_flags = flags;
2507 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2508 mm->mm_count++;
2510 return (0);
2513 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2515 return (QTAILQ_FIRST(&mm->mm_mmap));
2518 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2520 return (QTAILQ_NEXT(vma, vma_link));
2523 static int vma_get_mapping_count(const struct mm_struct *mm)
2525 return (mm->mm_count);
2529 * Calculate file (dump) size of given memory region.
2531 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2533 /* if we cannot even read the first page, skip it */
2534 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2535 return (0);
2538 * Usually we don't dump executable pages as they contain
2539 * non-writable code that debugger can read directly from
2540 * target library etc. However, thread stacks are marked
2541 * also executable so we read in first page of given region
2542 * and check whether it contains elf header. If there is
2543 * no elf header, we dump it.
2545 if (vma->vma_flags & PROT_EXEC) {
2546 char page[TARGET_PAGE_SIZE];
2548 copy_from_user(page, vma->vma_start, sizeof (page));
2549 if ((page[EI_MAG0] == ELFMAG0) &&
2550 (page[EI_MAG1] == ELFMAG1) &&
2551 (page[EI_MAG2] == ELFMAG2) &&
2552 (page[EI_MAG3] == ELFMAG3)) {
2554 * Mappings are possibly from ELF binary. Don't dump
2555 * them.
2557 return (0);
2561 return (vma->vma_end - vma->vma_start);
2564 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2565 abi_ulong flags)
2567 struct mm_struct *mm = (struct mm_struct *)priv;
2569 vma_add_mapping(mm, start, end, flags);
2570 return (0);
2573 static void fill_note(struct memelfnote *note, const char *name, int type,
2574 unsigned int sz, void *data)
2576 unsigned int namesz;
2578 namesz = strlen(name) + 1;
2579 note->name = name;
2580 note->namesz = namesz;
2581 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2582 note->type = type;
2583 note->datasz = sz;
2584 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2586 note->data = data;
2589 * We calculate rounded up note size here as specified by
2590 * ELF document.
2592 note->notesz = sizeof (struct elf_note) +
2593 note->namesz_rounded + note->datasz_rounded;
2596 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2597 uint32_t flags)
2599 (void) memset(elf, 0, sizeof(*elf));
2601 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2602 elf->e_ident[EI_CLASS] = ELF_CLASS;
2603 elf->e_ident[EI_DATA] = ELF_DATA;
2604 elf->e_ident[EI_VERSION] = EV_CURRENT;
2605 elf->e_ident[EI_OSABI] = ELF_OSABI;
2607 elf->e_type = ET_CORE;
2608 elf->e_machine = machine;
2609 elf->e_version = EV_CURRENT;
2610 elf->e_phoff = sizeof(struct elfhdr);
2611 elf->e_flags = flags;
2612 elf->e_ehsize = sizeof(struct elfhdr);
2613 elf->e_phentsize = sizeof(struct elf_phdr);
2614 elf->e_phnum = segs;
2616 bswap_ehdr(elf);
2619 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2621 phdr->p_type = PT_NOTE;
2622 phdr->p_offset = offset;
2623 phdr->p_vaddr = 0;
2624 phdr->p_paddr = 0;
2625 phdr->p_filesz = sz;
2626 phdr->p_memsz = 0;
2627 phdr->p_flags = 0;
2628 phdr->p_align = 0;
2630 bswap_phdr(phdr, 1);
2633 static size_t note_size(const struct memelfnote *note)
2635 return (note->notesz);
2638 static void fill_prstatus(struct target_elf_prstatus *prstatus,
2639 const TaskState *ts, int signr)
2641 (void) memset(prstatus, 0, sizeof (*prstatus));
2642 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2643 prstatus->pr_pid = ts->ts_tid;
2644 prstatus->pr_ppid = getppid();
2645 prstatus->pr_pgrp = getpgrp();
2646 prstatus->pr_sid = getsid(0);
2648 bswap_prstatus(prstatus);
2651 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2653 char *base_filename;
2654 unsigned int i, len;
2656 (void) memset(psinfo, 0, sizeof (*psinfo));
2658 len = ts->info->arg_end - ts->info->arg_start;
2659 if (len >= ELF_PRARGSZ)
2660 len = ELF_PRARGSZ - 1;
2661 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2662 return -EFAULT;
2663 for (i = 0; i < len; i++)
2664 if (psinfo->pr_psargs[i] == 0)
2665 psinfo->pr_psargs[i] = ' ';
2666 psinfo->pr_psargs[len] = 0;
2668 psinfo->pr_pid = getpid();
2669 psinfo->pr_ppid = getppid();
2670 psinfo->pr_pgrp = getpgrp();
2671 psinfo->pr_sid = getsid(0);
2672 psinfo->pr_uid = getuid();
2673 psinfo->pr_gid = getgid();
2675 base_filename = g_path_get_basename(ts->bprm->filename);
2677 * Using strncpy here is fine: at max-length,
2678 * this field is not NUL-terminated.
2680 (void) strncpy(psinfo->pr_fname, base_filename,
2681 sizeof(psinfo->pr_fname));
2683 g_free(base_filename);
2684 bswap_psinfo(psinfo);
2685 return (0);
2688 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2690 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2691 elf_addr_t orig_auxv = auxv;
2692 void *ptr;
2693 int len = ts->info->auxv_len;
2696 * Auxiliary vector is stored in target process stack. It contains
2697 * {type, value} pairs that we need to dump into note. This is not
2698 * strictly necessary but we do it here for sake of completeness.
2701 /* read in whole auxv vector and copy it to memelfnote */
2702 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2703 if (ptr != NULL) {
2704 fill_note(note, "CORE", NT_AUXV, len, ptr);
2705 unlock_user(ptr, auxv, len);
2710 * Constructs name of coredump file. We have following convention
2711 * for the name:
2712 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2714 * Returns 0 in case of success, -1 otherwise (errno is set).
2716 static int core_dump_filename(const TaskState *ts, char *buf,
2717 size_t bufsize)
2719 char timestamp[64];
2720 char *filename = NULL;
2721 char *base_filename = NULL;
2722 struct timeval tv;
2723 struct tm tm;
2725 assert(bufsize >= PATH_MAX);
2727 if (gettimeofday(&tv, NULL) < 0) {
2728 (void) fprintf(stderr, "unable to get current timestamp: %s",
2729 strerror(errno));
2730 return (-1);
2733 filename = strdup(ts->bprm->filename);
2734 base_filename = strdup(basename(filename));
2735 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2736 localtime_r(&tv.tv_sec, &tm));
2737 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2738 base_filename, timestamp, (int)getpid());
2739 free(base_filename);
2740 free(filename);
2742 return (0);
2745 static int dump_write(int fd, const void *ptr, size_t size)
2747 const char *bufp = (const char *)ptr;
2748 ssize_t bytes_written, bytes_left;
2749 struct rlimit dumpsize;
2750 off_t pos;
2752 bytes_written = 0;
2753 getrlimit(RLIMIT_CORE, &dumpsize);
2754 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2755 if (errno == ESPIPE) { /* not a seekable stream */
2756 bytes_left = size;
2757 } else {
2758 return pos;
2760 } else {
2761 if (dumpsize.rlim_cur <= pos) {
2762 return -1;
2763 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2764 bytes_left = size;
2765 } else {
2766 size_t limit_left=dumpsize.rlim_cur - pos;
2767 bytes_left = limit_left >= size ? size : limit_left ;
2772 * In normal conditions, single write(2) should do but
2773 * in case of socket etc. this mechanism is more portable.
2775 do {
2776 bytes_written = write(fd, bufp, bytes_left);
2777 if (bytes_written < 0) {
2778 if (errno == EINTR)
2779 continue;
2780 return (-1);
2781 } else if (bytes_written == 0) { /* eof */
2782 return (-1);
2784 bufp += bytes_written;
2785 bytes_left -= bytes_written;
2786 } while (bytes_left > 0);
2788 return (0);
2791 static int write_note(struct memelfnote *men, int fd)
2793 struct elf_note en;
2795 en.n_namesz = men->namesz;
2796 en.n_type = men->type;
2797 en.n_descsz = men->datasz;
2799 bswap_note(&en);
2801 if (dump_write(fd, &en, sizeof(en)) != 0)
2802 return (-1);
2803 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2804 return (-1);
2805 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
2806 return (-1);
2808 return (0);
2811 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
2813 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2814 TaskState *ts = (TaskState *)cpu->opaque;
2815 struct elf_thread_status *ets;
2817 ets = g_malloc0(sizeof (*ets));
2818 ets->num_notes = 1; /* only prstatus is dumped */
2819 fill_prstatus(&ets->prstatus, ts, 0);
2820 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2821 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2822 &ets->prstatus);
2824 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
2826 info->notes_size += note_size(&ets->notes[0]);
2829 static void init_note_info(struct elf_note_info *info)
2831 /* Initialize the elf_note_info structure so that it is at
2832 * least safe to call free_note_info() on it. Must be
2833 * called before calling fill_note_info().
2835 memset(info, 0, sizeof (*info));
2836 QTAILQ_INIT(&info->thread_list);
2839 static int fill_note_info(struct elf_note_info *info,
2840 long signr, const CPUArchState *env)
2842 #define NUMNOTES 3
2843 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2844 TaskState *ts = (TaskState *)cpu->opaque;
2845 int i;
2847 info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote));
2848 if (info->notes == NULL)
2849 return (-ENOMEM);
2850 info->prstatus = g_malloc0(sizeof (*info->prstatus));
2851 if (info->prstatus == NULL)
2852 return (-ENOMEM);
2853 info->psinfo = g_malloc0(sizeof (*info->psinfo));
2854 if (info->prstatus == NULL)
2855 return (-ENOMEM);
2858 * First fill in status (and registers) of current thread
2859 * including process info & aux vector.
2861 fill_prstatus(info->prstatus, ts, signr);
2862 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2863 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2864 sizeof (*info->prstatus), info->prstatus);
2865 fill_psinfo(info->psinfo, ts);
2866 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2867 sizeof (*info->psinfo), info->psinfo);
2868 fill_auxv_note(&info->notes[2], ts);
2869 info->numnote = 3;
2871 info->notes_size = 0;
2872 for (i = 0; i < info->numnote; i++)
2873 info->notes_size += note_size(&info->notes[i]);
2875 /* read and fill status of all threads */
2876 cpu_list_lock();
2877 CPU_FOREACH(cpu) {
2878 if (cpu == thread_cpu) {
2879 continue;
2881 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
2883 cpu_list_unlock();
2885 return (0);
2888 static void free_note_info(struct elf_note_info *info)
2890 struct elf_thread_status *ets;
2892 while (!QTAILQ_EMPTY(&info->thread_list)) {
2893 ets = QTAILQ_FIRST(&info->thread_list);
2894 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
2895 g_free(ets);
2898 g_free(info->prstatus);
2899 g_free(info->psinfo);
2900 g_free(info->notes);
2903 static int write_note_info(struct elf_note_info *info, int fd)
2905 struct elf_thread_status *ets;
2906 int i, error = 0;
2908 /* write prstatus, psinfo and auxv for current thread */
2909 for (i = 0; i < info->numnote; i++)
2910 if ((error = write_note(&info->notes[i], fd)) != 0)
2911 return (error);
2913 /* write prstatus for each thread */
2914 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
2915 if ((error = write_note(&ets->notes[0], fd)) != 0)
2916 return (error);
2919 return (0);
2923 * Write out ELF coredump.
2925 * See documentation of ELF object file format in:
2926 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2928 * Coredump format in linux is following:
2930 * 0 +----------------------+ \
2931 * | ELF header | ET_CORE |
2932 * +----------------------+ |
2933 * | ELF program headers | |--- headers
2934 * | - NOTE section | |
2935 * | - PT_LOAD sections | |
2936 * +----------------------+ /
2937 * | NOTEs: |
2938 * | - NT_PRSTATUS |
2939 * | - NT_PRSINFO |
2940 * | - NT_AUXV |
2941 * +----------------------+ <-- aligned to target page
2942 * | Process memory dump |
2943 * : :
2944 * . .
2945 * : :
2946 * | |
2947 * +----------------------+
2949 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2950 * NT_PRSINFO -> struct elf_prpsinfo
2951 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2953 * Format follows System V format as close as possible. Current
2954 * version limitations are as follows:
2955 * - no floating point registers are dumped
2957 * Function returns 0 in case of success, negative errno otherwise.
2959 * TODO: make this work also during runtime: it should be
2960 * possible to force coredump from running process and then
2961 * continue processing. For example qemu could set up SIGUSR2
2962 * handler (provided that target process haven't registered
2963 * handler for that) that does the dump when signal is received.
2965 static int elf_core_dump(int signr, const CPUArchState *env)
2967 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2968 const TaskState *ts = (const TaskState *)cpu->opaque;
2969 struct vm_area_struct *vma = NULL;
2970 char corefile[PATH_MAX];
2971 struct elf_note_info info;
2972 struct elfhdr elf;
2973 struct elf_phdr phdr;
2974 struct rlimit dumpsize;
2975 struct mm_struct *mm = NULL;
2976 off_t offset = 0, data_offset = 0;
2977 int segs = 0;
2978 int fd = -1;
2980 init_note_info(&info);
2982 errno = 0;
2983 getrlimit(RLIMIT_CORE, &dumpsize);
2984 if (dumpsize.rlim_cur == 0)
2985 return 0;
2987 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2988 return (-errno);
2990 if ((fd = open(corefile, O_WRONLY | O_CREAT,
2991 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2992 return (-errno);
2995 * Walk through target process memory mappings and
2996 * set up structure containing this information. After
2997 * this point vma_xxx functions can be used.
2999 if ((mm = vma_init()) == NULL)
3000 goto out;
3002 walk_memory_regions(mm, vma_walker);
3003 segs = vma_get_mapping_count(mm);
3006 * Construct valid coredump ELF header. We also
3007 * add one more segment for notes.
3009 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3010 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3011 goto out;
3013 /* fill in in-memory version of notes */
3014 if (fill_note_info(&info, signr, env) < 0)
3015 goto out;
3017 offset += sizeof (elf); /* elf header */
3018 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3020 /* write out notes program header */
3021 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3023 offset += info.notes_size;
3024 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3025 goto out;
3028 * ELF specification wants data to start at page boundary so
3029 * we align it here.
3031 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3034 * Write program headers for memory regions mapped in
3035 * the target process.
3037 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3038 (void) memset(&phdr, 0, sizeof (phdr));
3040 phdr.p_type = PT_LOAD;
3041 phdr.p_offset = offset;
3042 phdr.p_vaddr = vma->vma_start;
3043 phdr.p_paddr = 0;
3044 phdr.p_filesz = vma_dump_size(vma);
3045 offset += phdr.p_filesz;
3046 phdr.p_memsz = vma->vma_end - vma->vma_start;
3047 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3048 if (vma->vma_flags & PROT_WRITE)
3049 phdr.p_flags |= PF_W;
3050 if (vma->vma_flags & PROT_EXEC)
3051 phdr.p_flags |= PF_X;
3052 phdr.p_align = ELF_EXEC_PAGESIZE;
3054 bswap_phdr(&phdr, 1);
3055 dump_write(fd, &phdr, sizeof (phdr));
3059 * Next we write notes just after program headers. No
3060 * alignment needed here.
3062 if (write_note_info(&info, fd) < 0)
3063 goto out;
3065 /* align data to page boundary */
3066 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3067 goto out;
3070 * Finally we can dump process memory into corefile as well.
3072 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3073 abi_ulong addr;
3074 abi_ulong end;
3076 end = vma->vma_start + vma_dump_size(vma);
3078 for (addr = vma->vma_start; addr < end;
3079 addr += TARGET_PAGE_SIZE) {
3080 char page[TARGET_PAGE_SIZE];
3081 int error;
3084 * Read in page from target process memory and
3085 * write it to coredump file.
3087 error = copy_from_user(page, addr, sizeof (page));
3088 if (error != 0) {
3089 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3090 addr);
3091 errno = -error;
3092 goto out;
3094 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3095 goto out;
3099 out:
3100 free_note_info(&info);
3101 if (mm != NULL)
3102 vma_delete(mm);
3103 (void) close(fd);
3105 if (errno != 0)
3106 return (-errno);
3107 return (0);
3109 #endif /* USE_ELF_CORE_DUMP */
3111 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3113 init_thread(regs, infop);