Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / linux-user / elfload.c
blob6b2ce853f16dc038079445d689211980c1283b84
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 #ifdef TARGET_TILEGX
1217 /* 42 bits real used address, a half for user mode */
1218 #define ELF_START_MMAP (0x00000020000000000ULL)
1220 #define elf_check_arch(x) ((x) == EM_TILEGX)
1222 #define ELF_CLASS ELFCLASS64
1223 #define ELF_DATA ELFDATA2LSB
1224 #define ELF_ARCH EM_TILEGX
1226 static inline void init_thread(struct target_pt_regs *regs,
1227 struct image_info *infop)
1229 regs->pc = infop->entry;
1230 regs->sp = infop->start_stack;
1234 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1236 #endif /* TARGET_TILEGX */
1238 #ifndef ELF_PLATFORM
1239 #define ELF_PLATFORM (NULL)
1240 #endif
1242 #ifndef ELF_HWCAP
1243 #define ELF_HWCAP 0
1244 #endif
1246 #ifdef TARGET_ABI32
1247 #undef ELF_CLASS
1248 #define ELF_CLASS ELFCLASS32
1249 #undef bswaptls
1250 #define bswaptls(ptr) bswap32s(ptr)
1251 #endif
1253 #include "elf.h"
1255 struct exec
1257 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1258 unsigned int a_text; /* length of text, in bytes */
1259 unsigned int a_data; /* length of data, in bytes */
1260 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1261 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1262 unsigned int a_entry; /* start address */
1263 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1264 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1268 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1269 #define OMAGIC 0407
1270 #define NMAGIC 0410
1271 #define ZMAGIC 0413
1272 #define QMAGIC 0314
1274 /* Necessary parameters */
1275 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1276 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1277 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1278 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1280 #define DLINFO_ITEMS 14
1282 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1284 memcpy(to, from, n);
1287 #ifdef BSWAP_NEEDED
1288 static void bswap_ehdr(struct elfhdr *ehdr)
1290 bswap16s(&ehdr->e_type); /* Object file type */
1291 bswap16s(&ehdr->e_machine); /* Architecture */
1292 bswap32s(&ehdr->e_version); /* Object file version */
1293 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1294 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1295 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1296 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1297 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1298 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1299 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1300 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1301 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1302 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1305 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1307 int i;
1308 for (i = 0; i < phnum; ++i, ++phdr) {
1309 bswap32s(&phdr->p_type); /* Segment type */
1310 bswap32s(&phdr->p_flags); /* Segment flags */
1311 bswaptls(&phdr->p_offset); /* Segment file offset */
1312 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1313 bswaptls(&phdr->p_paddr); /* Segment physical address */
1314 bswaptls(&phdr->p_filesz); /* Segment size in file */
1315 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1316 bswaptls(&phdr->p_align); /* Segment alignment */
1320 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1322 int i;
1323 for (i = 0; i < shnum; ++i, ++shdr) {
1324 bswap32s(&shdr->sh_name);
1325 bswap32s(&shdr->sh_type);
1326 bswaptls(&shdr->sh_flags);
1327 bswaptls(&shdr->sh_addr);
1328 bswaptls(&shdr->sh_offset);
1329 bswaptls(&shdr->sh_size);
1330 bswap32s(&shdr->sh_link);
1331 bswap32s(&shdr->sh_info);
1332 bswaptls(&shdr->sh_addralign);
1333 bswaptls(&shdr->sh_entsize);
1337 static void bswap_sym(struct elf_sym *sym)
1339 bswap32s(&sym->st_name);
1340 bswaptls(&sym->st_value);
1341 bswaptls(&sym->st_size);
1342 bswap16s(&sym->st_shndx);
1344 #else
1345 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1346 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1347 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1348 static inline void bswap_sym(struct elf_sym *sym) { }
1349 #endif
1351 #ifdef USE_ELF_CORE_DUMP
1352 static int elf_core_dump(int, const CPUArchState *);
1353 #endif /* USE_ELF_CORE_DUMP */
1354 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1356 /* Verify the portions of EHDR within E_IDENT for the target.
1357 This can be performed before bswapping the entire header. */
1358 static bool elf_check_ident(struct elfhdr *ehdr)
1360 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1361 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1362 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1363 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1364 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1365 && ehdr->e_ident[EI_DATA] == ELF_DATA
1366 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1369 /* Verify the portions of EHDR outside of E_IDENT for the target.
1370 This has to wait until after bswapping the header. */
1371 static bool elf_check_ehdr(struct elfhdr *ehdr)
1373 return (elf_check_arch(ehdr->e_machine)
1374 && ehdr->e_ehsize == sizeof(struct elfhdr)
1375 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1376 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1380 * 'copy_elf_strings()' copies argument/envelope strings from user
1381 * memory to free pages in kernel mem. These are in a format ready
1382 * to be put directly into the top of new user memory.
1385 static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
1386 abi_ulong p)
1388 char *tmp, *tmp1, *pag = NULL;
1389 int len, offset = 0;
1391 if (!p) {
1392 return 0; /* bullet-proofing */
1394 while (argc-- > 0) {
1395 tmp = argv[argc];
1396 if (!tmp) {
1397 fprintf(stderr, "VFS: argc is wrong");
1398 exit(-1);
1400 tmp1 = tmp;
1401 while (*tmp++);
1402 len = tmp - tmp1;
1403 if (p < len) { /* this shouldn't happen - 128kB */
1404 return 0;
1406 while (len) {
1407 --p; --tmp; --len;
1408 if (--offset < 0) {
1409 offset = p % TARGET_PAGE_SIZE;
1410 pag = (char *)page[p/TARGET_PAGE_SIZE];
1411 if (!pag) {
1412 pag = g_try_malloc0(TARGET_PAGE_SIZE);
1413 page[p/TARGET_PAGE_SIZE] = pag;
1414 if (!pag)
1415 return 0;
1418 if (len == 0 || offset == 0) {
1419 *(pag + offset) = *tmp;
1421 else {
1422 int bytes_to_copy = (len > offset) ? offset : len;
1423 tmp -= bytes_to_copy;
1424 p -= bytes_to_copy;
1425 offset -= bytes_to_copy;
1426 len -= bytes_to_copy;
1427 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
1431 return p;
1434 static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
1435 struct image_info *info)
1437 abi_ulong stack_base, size, error, guard;
1438 int i;
1440 /* Create enough stack to hold everything. If we don't use
1441 it for args, we'll use it for something else. */
1442 size = guest_stack_size;
1443 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) {
1444 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1446 guard = TARGET_PAGE_SIZE;
1447 if (guard < qemu_real_host_page_size) {
1448 guard = qemu_real_host_page_size;
1451 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1452 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1453 if (error == -1) {
1454 perror("mmap stack");
1455 exit(-1);
1458 /* We reserve one extra page at the top of the stack as guard. */
1459 target_mprotect(error, guard, PROT_NONE);
1461 info->stack_limit = error + guard;
1462 stack_base = info->stack_limit + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1463 p += stack_base;
1465 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1466 if (bprm->page[i]) {
1467 info->rss++;
1468 /* FIXME - check return value of memcpy_to_target() for failure */
1469 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
1470 g_free(bprm->page[i]);
1472 stack_base += TARGET_PAGE_SIZE;
1474 return p;
1477 /* Map and zero the bss. We need to explicitly zero any fractional pages
1478 after the data section (i.e. bss). */
1479 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1481 uintptr_t host_start, host_map_start, host_end;
1483 last_bss = TARGET_PAGE_ALIGN(last_bss);
1485 /* ??? There is confusion between qemu_real_host_page_size and
1486 qemu_host_page_size here and elsewhere in target_mmap, which
1487 may lead to the end of the data section mapping from the file
1488 not being mapped. At least there was an explicit test and
1489 comment for that here, suggesting that "the file size must
1490 be known". The comment probably pre-dates the introduction
1491 of the fstat system call in target_mmap which does in fact
1492 find out the size. What isn't clear is if the workaround
1493 here is still actually needed. For now, continue with it,
1494 but merge it with the "normal" mmap that would allocate the bss. */
1496 host_start = (uintptr_t) g2h(elf_bss);
1497 host_end = (uintptr_t) g2h(last_bss);
1498 host_map_start = (host_start + qemu_real_host_page_size - 1);
1499 host_map_start &= -qemu_real_host_page_size;
1501 if (host_map_start < host_end) {
1502 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1503 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1504 if (p == MAP_FAILED) {
1505 perror("cannot mmap brk");
1506 exit(-1);
1510 /* Ensure that the bss page(s) are valid */
1511 if ((page_get_flags(last_bss-1) & prot) != prot) {
1512 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1515 if (host_start < host_map_start) {
1516 memset((void *)host_start, 0, host_map_start - host_start);
1520 #ifdef CONFIG_USE_FDPIC
1521 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1523 uint16_t n;
1524 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1526 /* elf32_fdpic_loadseg */
1527 n = info->nsegs;
1528 while (n--) {
1529 sp -= 12;
1530 put_user_u32(loadsegs[n].addr, sp+0);
1531 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1532 put_user_u32(loadsegs[n].p_memsz, sp+8);
1535 /* elf32_fdpic_loadmap */
1536 sp -= 4;
1537 put_user_u16(0, sp+0); /* version */
1538 put_user_u16(info->nsegs, sp+2); /* nsegs */
1540 info->personality = PER_LINUX_FDPIC;
1541 info->loadmap_addr = sp;
1543 return sp;
1545 #endif
1547 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1548 struct elfhdr *exec,
1549 struct image_info *info,
1550 struct image_info *interp_info)
1552 abi_ulong sp;
1553 abi_ulong sp_auxv;
1554 int size;
1555 int i;
1556 abi_ulong u_rand_bytes;
1557 uint8_t k_rand_bytes[16];
1558 abi_ulong u_platform;
1559 const char *k_platform;
1560 const int n = sizeof(elf_addr_t);
1562 sp = p;
1564 #ifdef CONFIG_USE_FDPIC
1565 /* Needs to be before we load the env/argc/... */
1566 if (elf_is_fdpic(exec)) {
1567 /* Need 4 byte alignment for these structs */
1568 sp &= ~3;
1569 sp = loader_build_fdpic_loadmap(info, sp);
1570 info->other_info = interp_info;
1571 if (interp_info) {
1572 interp_info->other_info = info;
1573 sp = loader_build_fdpic_loadmap(interp_info, sp);
1576 #endif
1578 u_platform = 0;
1579 k_platform = ELF_PLATFORM;
1580 if (k_platform) {
1581 size_t len = strlen(k_platform) + 1;
1582 sp -= (len + n - 1) & ~(n - 1);
1583 u_platform = sp;
1584 /* FIXME - check return value of memcpy_to_target() for failure */
1585 memcpy_to_target(sp, k_platform, len);
1589 * Generate 16 random bytes for userspace PRNG seeding (not
1590 * cryptically secure but it's not the aim of QEMU).
1592 for (i = 0; i < 16; i++) {
1593 k_rand_bytes[i] = rand();
1595 sp -= 16;
1596 u_rand_bytes = sp;
1597 /* FIXME - check return value of memcpy_to_target() for failure */
1598 memcpy_to_target(sp, k_rand_bytes, 16);
1601 * Force 16 byte _final_ alignment here for generality.
1603 sp = sp &~ (abi_ulong)15;
1604 size = (DLINFO_ITEMS + 1) * 2;
1605 if (k_platform)
1606 size += 2;
1607 #ifdef DLINFO_ARCH_ITEMS
1608 size += DLINFO_ARCH_ITEMS * 2;
1609 #endif
1610 #ifdef ELF_HWCAP2
1611 size += 2;
1612 #endif
1613 size += envc + argc + 2;
1614 size += 1; /* argc itself */
1615 size *= n;
1616 if (size & 15)
1617 sp -= 16 - (size & 15);
1619 /* This is correct because Linux defines
1620 * elf_addr_t as Elf32_Off / Elf64_Off
1622 #define NEW_AUX_ENT(id, val) do { \
1623 sp -= n; put_user_ual(val, sp); \
1624 sp -= n; put_user_ual(id, sp); \
1625 } while(0)
1627 sp_auxv = sp;
1628 NEW_AUX_ENT (AT_NULL, 0);
1630 /* There must be exactly DLINFO_ITEMS entries here. */
1631 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1632 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1633 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1634 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize())));
1635 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1636 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1637 NEW_AUX_ENT(AT_ENTRY, info->entry);
1638 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1639 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1640 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1641 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1642 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1643 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1644 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1646 #ifdef ELF_HWCAP2
1647 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1648 #endif
1650 if (k_platform)
1651 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1652 #ifdef ARCH_DLINFO
1654 * ARCH_DLINFO must come last so platform specific code can enforce
1655 * special alignment requirements on the AUXV if necessary (eg. PPC).
1657 ARCH_DLINFO;
1658 #endif
1659 #undef NEW_AUX_ENT
1661 info->saved_auxv = sp;
1662 info->auxv_len = sp_auxv - sp;
1664 sp = loader_build_argptr(envc, argc, sp, p, 0);
1665 /* Check the right amount of stack was allocated for auxvec, envp & argv. */
1666 assert(sp_auxv - sp == size);
1667 return sp;
1670 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1671 /* If the guest doesn't have a validation function just agree */
1672 static int validate_guest_space(unsigned long guest_base,
1673 unsigned long guest_size)
1675 return 1;
1677 #endif
1679 unsigned long init_guest_space(unsigned long host_start,
1680 unsigned long host_size,
1681 unsigned long guest_start,
1682 bool fixed)
1684 unsigned long current_start, real_start;
1685 int flags;
1687 assert(host_start || host_size);
1689 /* If just a starting address is given, then just verify that
1690 * address. */
1691 if (host_start && !host_size) {
1692 if (validate_guest_space(host_start, host_size) == 1) {
1693 return host_start;
1694 } else {
1695 return (unsigned long)-1;
1699 /* Setup the initial flags and start address. */
1700 current_start = host_start & qemu_host_page_mask;
1701 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1702 if (fixed) {
1703 flags |= MAP_FIXED;
1706 /* Otherwise, a non-zero size region of memory needs to be mapped
1707 * and validated. */
1708 while (1) {
1709 unsigned long real_size = host_size;
1711 /* Do not use mmap_find_vma here because that is limited to the
1712 * guest address space. We are going to make the
1713 * guest address space fit whatever we're given.
1715 real_start = (unsigned long)
1716 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1717 if (real_start == (unsigned long)-1) {
1718 return (unsigned long)-1;
1721 /* Ensure the address is properly aligned. */
1722 if (real_start & ~qemu_host_page_mask) {
1723 munmap((void *)real_start, host_size);
1724 real_size = host_size + qemu_host_page_size;
1725 real_start = (unsigned long)
1726 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1727 if (real_start == (unsigned long)-1) {
1728 return (unsigned long)-1;
1730 real_start = HOST_PAGE_ALIGN(real_start);
1733 /* Check to see if the address is valid. */
1734 if (!host_start || real_start == current_start) {
1735 int valid = validate_guest_space(real_start - guest_start,
1736 real_size);
1737 if (valid == 1) {
1738 break;
1739 } else if (valid == -1) {
1740 return (unsigned long)-1;
1742 /* valid == 0, so try again. */
1745 /* That address didn't work. Unmap and try a different one.
1746 * The address the host picked because is typically right at
1747 * the top of the host address space and leaves the guest with
1748 * no usable address space. Resort to a linear search. We
1749 * already compensated for mmap_min_addr, so this should not
1750 * happen often. Probably means we got unlucky and host
1751 * address space randomization put a shared library somewhere
1752 * inconvenient.
1754 munmap((void *)real_start, host_size);
1755 current_start += qemu_host_page_size;
1756 if (host_start == current_start) {
1757 /* Theoretically possible if host doesn't have any suitably
1758 * aligned areas. Normally the first mmap will fail.
1760 return (unsigned long)-1;
1764 qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size);
1766 return real_start;
1769 static void probe_guest_base(const char *image_name,
1770 abi_ulong loaddr, abi_ulong hiaddr)
1772 /* Probe for a suitable guest base address, if the user has not set
1773 * it explicitly, and set guest_base appropriately.
1774 * In case of error we will print a suitable message and exit.
1776 const char *errmsg;
1777 if (!have_guest_base && !reserved_va) {
1778 unsigned long host_start, real_start, host_size;
1780 /* Round addresses to page boundaries. */
1781 loaddr &= qemu_host_page_mask;
1782 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1784 if (loaddr < mmap_min_addr) {
1785 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1786 } else {
1787 host_start = loaddr;
1788 if (host_start != loaddr) {
1789 errmsg = "Address overflow loading ELF binary";
1790 goto exit_errmsg;
1793 host_size = hiaddr - loaddr;
1795 /* Setup the initial guest memory space with ranges gleaned from
1796 * the ELF image that is being loaded.
1798 real_start = init_guest_space(host_start, host_size, loaddr, false);
1799 if (real_start == (unsigned long)-1) {
1800 errmsg = "Unable to find space for application";
1801 goto exit_errmsg;
1803 guest_base = real_start - loaddr;
1805 qemu_log("Relocating guest address space from 0x"
1806 TARGET_ABI_FMT_lx " to 0x%lx\n",
1807 loaddr, real_start);
1809 return;
1811 exit_errmsg:
1812 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1813 exit(-1);
1817 /* Load an ELF image into the address space.
1819 IMAGE_NAME is the filename of the image, to use in error messages.
1820 IMAGE_FD is the open file descriptor for the image.
1822 BPRM_BUF is a copy of the beginning of the file; this of course
1823 contains the elf file header at offset 0. It is assumed that this
1824 buffer is sufficiently aligned to present no problems to the host
1825 in accessing data at aligned offsets within the buffer.
1827 On return: INFO values will be filled in, as necessary or available. */
1829 static void load_elf_image(const char *image_name, int image_fd,
1830 struct image_info *info, char **pinterp_name,
1831 char bprm_buf[BPRM_BUF_SIZE])
1833 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1834 struct elf_phdr *phdr;
1835 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1836 int i, retval;
1837 const char *errmsg;
1839 /* First of all, some simple consistency checks */
1840 errmsg = "Invalid ELF image for this architecture";
1841 if (!elf_check_ident(ehdr)) {
1842 goto exit_errmsg;
1844 bswap_ehdr(ehdr);
1845 if (!elf_check_ehdr(ehdr)) {
1846 goto exit_errmsg;
1849 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1850 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1851 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
1852 } else {
1853 phdr = (struct elf_phdr *) alloca(i);
1854 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
1855 if (retval != i) {
1856 goto exit_read;
1859 bswap_phdr(phdr, ehdr->e_phnum);
1861 #ifdef CONFIG_USE_FDPIC
1862 info->nsegs = 0;
1863 info->pt_dynamic_addr = 0;
1864 #endif
1866 /* Find the maximum size of the image and allocate an appropriate
1867 amount of memory to handle that. */
1868 loaddr = -1, hiaddr = 0;
1869 for (i = 0; i < ehdr->e_phnum; ++i) {
1870 if (phdr[i].p_type == PT_LOAD) {
1871 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
1872 if (a < loaddr) {
1873 loaddr = a;
1875 a = phdr[i].p_vaddr + phdr[i].p_memsz;
1876 if (a > hiaddr) {
1877 hiaddr = a;
1879 #ifdef CONFIG_USE_FDPIC
1880 ++info->nsegs;
1881 #endif
1885 load_addr = loaddr;
1886 if (ehdr->e_type == ET_DYN) {
1887 /* The image indicates that it can be loaded anywhere. Find a
1888 location that can hold the memory space required. If the
1889 image is pre-linked, LOADDR will be non-zero. Since we do
1890 not supply MAP_FIXED here we'll use that address if and
1891 only if it remains available. */
1892 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
1893 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
1894 -1, 0);
1895 if (load_addr == -1) {
1896 goto exit_perror;
1898 } else if (pinterp_name != NULL) {
1899 /* This is the main executable. Make sure that the low
1900 address does not conflict with MMAP_MIN_ADDR or the
1901 QEMU application itself. */
1902 probe_guest_base(image_name, loaddr, hiaddr);
1904 load_bias = load_addr - loaddr;
1906 #ifdef CONFIG_USE_FDPIC
1908 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
1909 g_malloc(sizeof(*loadsegs) * info->nsegs);
1911 for (i = 0; i < ehdr->e_phnum; ++i) {
1912 switch (phdr[i].p_type) {
1913 case PT_DYNAMIC:
1914 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
1915 break;
1916 case PT_LOAD:
1917 loadsegs->addr = phdr[i].p_vaddr + load_bias;
1918 loadsegs->p_vaddr = phdr[i].p_vaddr;
1919 loadsegs->p_memsz = phdr[i].p_memsz;
1920 ++loadsegs;
1921 break;
1925 #endif
1927 info->load_bias = load_bias;
1928 info->load_addr = load_addr;
1929 info->entry = ehdr->e_entry + load_bias;
1930 info->start_code = -1;
1931 info->end_code = 0;
1932 info->start_data = -1;
1933 info->end_data = 0;
1934 info->brk = 0;
1935 info->elf_flags = ehdr->e_flags;
1937 for (i = 0; i < ehdr->e_phnum; i++) {
1938 struct elf_phdr *eppnt = phdr + i;
1939 if (eppnt->p_type == PT_LOAD) {
1940 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
1941 int elf_prot = 0;
1943 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1944 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1945 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1947 vaddr = load_bias + eppnt->p_vaddr;
1948 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
1949 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
1951 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
1952 elf_prot, MAP_PRIVATE | MAP_FIXED,
1953 image_fd, eppnt->p_offset - vaddr_po);
1954 if (error == -1) {
1955 goto exit_perror;
1958 vaddr_ef = vaddr + eppnt->p_filesz;
1959 vaddr_em = vaddr + eppnt->p_memsz;
1961 /* If the load segment requests extra zeros (e.g. bss), map it. */
1962 if (vaddr_ef < vaddr_em) {
1963 zero_bss(vaddr_ef, vaddr_em, elf_prot);
1966 /* Find the full program boundaries. */
1967 if (elf_prot & PROT_EXEC) {
1968 if (vaddr < info->start_code) {
1969 info->start_code = vaddr;
1971 if (vaddr_ef > info->end_code) {
1972 info->end_code = vaddr_ef;
1975 if (elf_prot & PROT_WRITE) {
1976 if (vaddr < info->start_data) {
1977 info->start_data = vaddr;
1979 if (vaddr_ef > info->end_data) {
1980 info->end_data = vaddr_ef;
1982 if (vaddr_em > info->brk) {
1983 info->brk = vaddr_em;
1986 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
1987 char *interp_name;
1989 if (*pinterp_name) {
1990 errmsg = "Multiple PT_INTERP entries";
1991 goto exit_errmsg;
1993 interp_name = malloc(eppnt->p_filesz);
1994 if (!interp_name) {
1995 goto exit_perror;
1998 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
1999 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2000 eppnt->p_filesz);
2001 } else {
2002 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2003 eppnt->p_offset);
2004 if (retval != eppnt->p_filesz) {
2005 goto exit_perror;
2008 if (interp_name[eppnt->p_filesz - 1] != 0) {
2009 errmsg = "Invalid PT_INTERP entry";
2010 goto exit_errmsg;
2012 *pinterp_name = interp_name;
2016 if (info->end_data == 0) {
2017 info->start_data = info->end_code;
2018 info->end_data = info->end_code;
2019 info->brk = info->end_code;
2022 if (qemu_log_enabled()) {
2023 load_symbols(ehdr, image_fd, load_bias);
2026 close(image_fd);
2027 return;
2029 exit_read:
2030 if (retval >= 0) {
2031 errmsg = "Incomplete read of file header";
2032 goto exit_errmsg;
2034 exit_perror:
2035 errmsg = strerror(errno);
2036 exit_errmsg:
2037 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2038 exit(-1);
2041 static void load_elf_interp(const char *filename, struct image_info *info,
2042 char bprm_buf[BPRM_BUF_SIZE])
2044 int fd, retval;
2046 fd = open(path(filename), O_RDONLY);
2047 if (fd < 0) {
2048 goto exit_perror;
2051 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2052 if (retval < 0) {
2053 goto exit_perror;
2055 if (retval < BPRM_BUF_SIZE) {
2056 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2059 load_elf_image(filename, fd, info, NULL, bprm_buf);
2060 return;
2062 exit_perror:
2063 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2064 exit(-1);
2067 static int symfind(const void *s0, const void *s1)
2069 target_ulong addr = *(target_ulong *)s0;
2070 struct elf_sym *sym = (struct elf_sym *)s1;
2071 int result = 0;
2072 if (addr < sym->st_value) {
2073 result = -1;
2074 } else if (addr >= sym->st_value + sym->st_size) {
2075 result = 1;
2077 return result;
2080 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2082 #if ELF_CLASS == ELFCLASS32
2083 struct elf_sym *syms = s->disas_symtab.elf32;
2084 #else
2085 struct elf_sym *syms = s->disas_symtab.elf64;
2086 #endif
2088 // binary search
2089 struct elf_sym *sym;
2091 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2092 if (sym != NULL) {
2093 return s->disas_strtab + sym->st_name;
2096 return "";
2099 /* FIXME: This should use elf_ops.h */
2100 static int symcmp(const void *s0, const void *s1)
2102 struct elf_sym *sym0 = (struct elf_sym *)s0;
2103 struct elf_sym *sym1 = (struct elf_sym *)s1;
2104 return (sym0->st_value < sym1->st_value)
2105 ? -1
2106 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2109 /* Best attempt to load symbols from this ELF object. */
2110 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2112 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2113 struct elf_shdr *shdr;
2114 char *strings = NULL;
2115 struct syminfo *s = NULL;
2116 struct elf_sym *new_syms, *syms = NULL;
2118 shnum = hdr->e_shnum;
2119 i = shnum * sizeof(struct elf_shdr);
2120 shdr = (struct elf_shdr *)alloca(i);
2121 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2122 return;
2125 bswap_shdr(shdr, shnum);
2126 for (i = 0; i < shnum; ++i) {
2127 if (shdr[i].sh_type == SHT_SYMTAB) {
2128 sym_idx = i;
2129 str_idx = shdr[i].sh_link;
2130 goto found;
2134 /* There will be no symbol table if the file was stripped. */
2135 return;
2137 found:
2138 /* Now know where the strtab and symtab are. Snarf them. */
2139 s = malloc(sizeof(*s));
2140 if (!s) {
2141 goto give_up;
2144 i = shdr[str_idx].sh_size;
2145 s->disas_strtab = strings = malloc(i);
2146 if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
2147 goto give_up;
2150 i = shdr[sym_idx].sh_size;
2151 syms = malloc(i);
2152 if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
2153 goto give_up;
2156 nsyms = i / sizeof(struct elf_sym);
2157 for (i = 0; i < nsyms; ) {
2158 bswap_sym(syms + i);
2159 /* Throw away entries which we do not need. */
2160 if (syms[i].st_shndx == SHN_UNDEF
2161 || syms[i].st_shndx >= SHN_LORESERVE
2162 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2163 if (i < --nsyms) {
2164 syms[i] = syms[nsyms];
2166 } else {
2167 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2168 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2169 syms[i].st_value &= ~(target_ulong)1;
2170 #endif
2171 syms[i].st_value += load_bias;
2172 i++;
2176 /* No "useful" symbol. */
2177 if (nsyms == 0) {
2178 goto give_up;
2181 /* Attempt to free the storage associated with the local symbols
2182 that we threw away. Whether or not this has any effect on the
2183 memory allocation depends on the malloc implementation and how
2184 many symbols we managed to discard. */
2185 new_syms = realloc(syms, nsyms * sizeof(*syms));
2186 if (new_syms == NULL) {
2187 goto give_up;
2189 syms = new_syms;
2191 qsort(syms, nsyms, sizeof(*syms), symcmp);
2193 s->disas_num_syms = nsyms;
2194 #if ELF_CLASS == ELFCLASS32
2195 s->disas_symtab.elf32 = syms;
2196 #else
2197 s->disas_symtab.elf64 = syms;
2198 #endif
2199 s->lookup_symbol = lookup_symbolxx;
2200 s->next = syminfos;
2201 syminfos = s;
2203 return;
2205 give_up:
2206 free(s);
2207 free(strings);
2208 free(syms);
2211 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2213 struct image_info interp_info;
2214 struct elfhdr elf_ex;
2215 char *elf_interpreter = NULL;
2217 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2218 info->mmap = 0;
2219 info->rss = 0;
2221 load_elf_image(bprm->filename, bprm->fd, info,
2222 &elf_interpreter, bprm->buf);
2224 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2225 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2226 when we load the interpreter. */
2227 elf_ex = *(struct elfhdr *)bprm->buf;
2229 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
2230 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
2231 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
2232 if (!bprm->p) {
2233 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2234 exit(-1);
2237 /* Do this so that we can load the interpreter, if need be. We will
2238 change some of these later */
2239 bprm->p = setup_arg_pages(bprm->p, bprm, info);
2241 if (elf_interpreter) {
2242 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2244 /* If the program interpreter is one of these two, then assume
2245 an iBCS2 image. Otherwise assume a native linux image. */
2247 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2248 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2249 info->personality = PER_SVR4;
2251 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2252 and some applications "depend" upon this behavior. Since
2253 we do not have the power to recompile these, we emulate
2254 the SVr4 behavior. Sigh. */
2255 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2256 MAP_FIXED | MAP_PRIVATE, -1, 0);
2260 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2261 info, (elf_interpreter ? &interp_info : NULL));
2262 info->start_stack = bprm->p;
2264 /* If we have an interpreter, set that as the program's entry point.
2265 Copy the load_bias as well, to help PPC64 interpret the entry
2266 point as a function descriptor. Do this after creating elf tables
2267 so that we copy the original program entry point into the AUXV. */
2268 if (elf_interpreter) {
2269 info->load_bias = interp_info.load_bias;
2270 info->entry = interp_info.entry;
2271 free(elf_interpreter);
2274 #ifdef USE_ELF_CORE_DUMP
2275 bprm->core_dump = &elf_core_dump;
2276 #endif
2278 return 0;
2281 #ifdef USE_ELF_CORE_DUMP
2283 * Definitions to generate Intel SVR4-like core files.
2284 * These mostly have the same names as the SVR4 types with "target_elf_"
2285 * tacked on the front to prevent clashes with linux definitions,
2286 * and the typedef forms have been avoided. This is mostly like
2287 * the SVR4 structure, but more Linuxy, with things that Linux does
2288 * not support and which gdb doesn't really use excluded.
2290 * Fields we don't dump (their contents is zero) in linux-user qemu
2291 * are marked with XXX.
2293 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2295 * Porting ELF coredump for target is (quite) simple process. First you
2296 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2297 * the target resides):
2299 * #define USE_ELF_CORE_DUMP
2301 * Next you define type of register set used for dumping. ELF specification
2302 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2304 * typedef <target_regtype> target_elf_greg_t;
2305 * #define ELF_NREG <number of registers>
2306 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2308 * Last step is to implement target specific function that copies registers
2309 * from given cpu into just specified register set. Prototype is:
2311 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2312 * const CPUArchState *env);
2314 * Parameters:
2315 * regs - copy register values into here (allocated and zeroed by caller)
2316 * env - copy registers from here
2318 * Example for ARM target is provided in this file.
2321 /* An ELF note in memory */
2322 struct memelfnote {
2323 const char *name;
2324 size_t namesz;
2325 size_t namesz_rounded;
2326 int type;
2327 size_t datasz;
2328 size_t datasz_rounded;
2329 void *data;
2330 size_t notesz;
2333 struct target_elf_siginfo {
2334 abi_int si_signo; /* signal number */
2335 abi_int si_code; /* extra code */
2336 abi_int si_errno; /* errno */
2339 struct target_elf_prstatus {
2340 struct target_elf_siginfo pr_info; /* Info associated with signal */
2341 abi_short pr_cursig; /* Current signal */
2342 abi_ulong pr_sigpend; /* XXX */
2343 abi_ulong pr_sighold; /* XXX */
2344 target_pid_t pr_pid;
2345 target_pid_t pr_ppid;
2346 target_pid_t pr_pgrp;
2347 target_pid_t pr_sid;
2348 struct target_timeval pr_utime; /* XXX User time */
2349 struct target_timeval pr_stime; /* XXX System time */
2350 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2351 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2352 target_elf_gregset_t pr_reg; /* GP registers */
2353 abi_int pr_fpvalid; /* XXX */
2356 #define ELF_PRARGSZ (80) /* Number of chars for args */
2358 struct target_elf_prpsinfo {
2359 char pr_state; /* numeric process state */
2360 char pr_sname; /* char for pr_state */
2361 char pr_zomb; /* zombie */
2362 char pr_nice; /* nice val */
2363 abi_ulong pr_flag; /* flags */
2364 target_uid_t pr_uid;
2365 target_gid_t pr_gid;
2366 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2367 /* Lots missing */
2368 char pr_fname[16]; /* filename of executable */
2369 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2372 /* Here is the structure in which status of each thread is captured. */
2373 struct elf_thread_status {
2374 QTAILQ_ENTRY(elf_thread_status) ets_link;
2375 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2376 #if 0
2377 elf_fpregset_t fpu; /* NT_PRFPREG */
2378 struct task_struct *thread;
2379 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2380 #endif
2381 struct memelfnote notes[1];
2382 int num_notes;
2385 struct elf_note_info {
2386 struct memelfnote *notes;
2387 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2388 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2390 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2391 #if 0
2393 * Current version of ELF coredump doesn't support
2394 * dumping fp regs etc.
2396 elf_fpregset_t *fpu;
2397 elf_fpxregset_t *xfpu;
2398 int thread_status_size;
2399 #endif
2400 int notes_size;
2401 int numnote;
2404 struct vm_area_struct {
2405 target_ulong vma_start; /* start vaddr of memory region */
2406 target_ulong vma_end; /* end vaddr of memory region */
2407 abi_ulong vma_flags; /* protection etc. flags for the region */
2408 QTAILQ_ENTRY(vm_area_struct) vma_link;
2411 struct mm_struct {
2412 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2413 int mm_count; /* number of mappings */
2416 static struct mm_struct *vma_init(void);
2417 static void vma_delete(struct mm_struct *);
2418 static int vma_add_mapping(struct mm_struct *, target_ulong,
2419 target_ulong, abi_ulong);
2420 static int vma_get_mapping_count(const struct mm_struct *);
2421 static struct vm_area_struct *vma_first(const struct mm_struct *);
2422 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2423 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2424 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2425 abi_ulong flags);
2427 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2428 static void fill_note(struct memelfnote *, const char *, int,
2429 unsigned int, void *);
2430 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2431 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2432 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2433 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2434 static size_t note_size(const struct memelfnote *);
2435 static void free_note_info(struct elf_note_info *);
2436 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2437 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2438 static int core_dump_filename(const TaskState *, char *, size_t);
2440 static int dump_write(int, const void *, size_t);
2441 static int write_note(struct memelfnote *, int);
2442 static int write_note_info(struct elf_note_info *, int);
2444 #ifdef BSWAP_NEEDED
2445 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2447 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2448 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2449 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2450 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2451 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2452 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2453 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2454 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2455 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2456 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2457 /* cpu times are not filled, so we skip them */
2458 /* regs should be in correct format already */
2459 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2462 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2464 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2465 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2466 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2467 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2468 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2469 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2470 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2473 static void bswap_note(struct elf_note *en)
2475 bswap32s(&en->n_namesz);
2476 bswap32s(&en->n_descsz);
2477 bswap32s(&en->n_type);
2479 #else
2480 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2481 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2482 static inline void bswap_note(struct elf_note *en) { }
2483 #endif /* BSWAP_NEEDED */
2486 * Minimal support for linux memory regions. These are needed
2487 * when we are finding out what memory exactly belongs to
2488 * emulated process. No locks needed here, as long as
2489 * thread that received the signal is stopped.
2492 static struct mm_struct *vma_init(void)
2494 struct mm_struct *mm;
2496 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2497 return (NULL);
2499 mm->mm_count = 0;
2500 QTAILQ_INIT(&mm->mm_mmap);
2502 return (mm);
2505 static void vma_delete(struct mm_struct *mm)
2507 struct vm_area_struct *vma;
2509 while ((vma = vma_first(mm)) != NULL) {
2510 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2511 g_free(vma);
2513 g_free(mm);
2516 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
2517 target_ulong end, abi_ulong flags)
2519 struct vm_area_struct *vma;
2521 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2522 return (-1);
2524 vma->vma_start = start;
2525 vma->vma_end = end;
2526 vma->vma_flags = flags;
2528 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2529 mm->mm_count++;
2531 return (0);
2534 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2536 return (QTAILQ_FIRST(&mm->mm_mmap));
2539 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2541 return (QTAILQ_NEXT(vma, vma_link));
2544 static int vma_get_mapping_count(const struct mm_struct *mm)
2546 return (mm->mm_count);
2550 * Calculate file (dump) size of given memory region.
2552 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2554 /* if we cannot even read the first page, skip it */
2555 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2556 return (0);
2559 * Usually we don't dump executable pages as they contain
2560 * non-writable code that debugger can read directly from
2561 * target library etc. However, thread stacks are marked
2562 * also executable so we read in first page of given region
2563 * and check whether it contains elf header. If there is
2564 * no elf header, we dump it.
2566 if (vma->vma_flags & PROT_EXEC) {
2567 char page[TARGET_PAGE_SIZE];
2569 copy_from_user(page, vma->vma_start, sizeof (page));
2570 if ((page[EI_MAG0] == ELFMAG0) &&
2571 (page[EI_MAG1] == ELFMAG1) &&
2572 (page[EI_MAG2] == ELFMAG2) &&
2573 (page[EI_MAG3] == ELFMAG3)) {
2575 * Mappings are possibly from ELF binary. Don't dump
2576 * them.
2578 return (0);
2582 return (vma->vma_end - vma->vma_start);
2585 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2586 abi_ulong flags)
2588 struct mm_struct *mm = (struct mm_struct *)priv;
2590 vma_add_mapping(mm, start, end, flags);
2591 return (0);
2594 static void fill_note(struct memelfnote *note, const char *name, int type,
2595 unsigned int sz, void *data)
2597 unsigned int namesz;
2599 namesz = strlen(name) + 1;
2600 note->name = name;
2601 note->namesz = namesz;
2602 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2603 note->type = type;
2604 note->datasz = sz;
2605 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2607 note->data = data;
2610 * We calculate rounded up note size here as specified by
2611 * ELF document.
2613 note->notesz = sizeof (struct elf_note) +
2614 note->namesz_rounded + note->datasz_rounded;
2617 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2618 uint32_t flags)
2620 (void) memset(elf, 0, sizeof(*elf));
2622 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2623 elf->e_ident[EI_CLASS] = ELF_CLASS;
2624 elf->e_ident[EI_DATA] = ELF_DATA;
2625 elf->e_ident[EI_VERSION] = EV_CURRENT;
2626 elf->e_ident[EI_OSABI] = ELF_OSABI;
2628 elf->e_type = ET_CORE;
2629 elf->e_machine = machine;
2630 elf->e_version = EV_CURRENT;
2631 elf->e_phoff = sizeof(struct elfhdr);
2632 elf->e_flags = flags;
2633 elf->e_ehsize = sizeof(struct elfhdr);
2634 elf->e_phentsize = sizeof(struct elf_phdr);
2635 elf->e_phnum = segs;
2637 bswap_ehdr(elf);
2640 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2642 phdr->p_type = PT_NOTE;
2643 phdr->p_offset = offset;
2644 phdr->p_vaddr = 0;
2645 phdr->p_paddr = 0;
2646 phdr->p_filesz = sz;
2647 phdr->p_memsz = 0;
2648 phdr->p_flags = 0;
2649 phdr->p_align = 0;
2651 bswap_phdr(phdr, 1);
2654 static size_t note_size(const struct memelfnote *note)
2656 return (note->notesz);
2659 static void fill_prstatus(struct target_elf_prstatus *prstatus,
2660 const TaskState *ts, int signr)
2662 (void) memset(prstatus, 0, sizeof (*prstatus));
2663 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2664 prstatus->pr_pid = ts->ts_tid;
2665 prstatus->pr_ppid = getppid();
2666 prstatus->pr_pgrp = getpgrp();
2667 prstatus->pr_sid = getsid(0);
2669 bswap_prstatus(prstatus);
2672 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2674 char *base_filename;
2675 unsigned int i, len;
2677 (void) memset(psinfo, 0, sizeof (*psinfo));
2679 len = ts->info->arg_end - ts->info->arg_start;
2680 if (len >= ELF_PRARGSZ)
2681 len = ELF_PRARGSZ - 1;
2682 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2683 return -EFAULT;
2684 for (i = 0; i < len; i++)
2685 if (psinfo->pr_psargs[i] == 0)
2686 psinfo->pr_psargs[i] = ' ';
2687 psinfo->pr_psargs[len] = 0;
2689 psinfo->pr_pid = getpid();
2690 psinfo->pr_ppid = getppid();
2691 psinfo->pr_pgrp = getpgrp();
2692 psinfo->pr_sid = getsid(0);
2693 psinfo->pr_uid = getuid();
2694 psinfo->pr_gid = getgid();
2696 base_filename = g_path_get_basename(ts->bprm->filename);
2698 * Using strncpy here is fine: at max-length,
2699 * this field is not NUL-terminated.
2701 (void) strncpy(psinfo->pr_fname, base_filename,
2702 sizeof(psinfo->pr_fname));
2704 g_free(base_filename);
2705 bswap_psinfo(psinfo);
2706 return (0);
2709 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2711 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2712 elf_addr_t orig_auxv = auxv;
2713 void *ptr;
2714 int len = ts->info->auxv_len;
2717 * Auxiliary vector is stored in target process stack. It contains
2718 * {type, value} pairs that we need to dump into note. This is not
2719 * strictly necessary but we do it here for sake of completeness.
2722 /* read in whole auxv vector and copy it to memelfnote */
2723 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2724 if (ptr != NULL) {
2725 fill_note(note, "CORE", NT_AUXV, len, ptr);
2726 unlock_user(ptr, auxv, len);
2731 * Constructs name of coredump file. We have following convention
2732 * for the name:
2733 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2735 * Returns 0 in case of success, -1 otherwise (errno is set).
2737 static int core_dump_filename(const TaskState *ts, char *buf,
2738 size_t bufsize)
2740 char timestamp[64];
2741 char *filename = NULL;
2742 char *base_filename = NULL;
2743 struct timeval tv;
2744 struct tm tm;
2746 assert(bufsize >= PATH_MAX);
2748 if (gettimeofday(&tv, NULL) < 0) {
2749 (void) fprintf(stderr, "unable to get current timestamp: %s",
2750 strerror(errno));
2751 return (-1);
2754 filename = strdup(ts->bprm->filename);
2755 base_filename = strdup(basename(filename));
2756 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2757 localtime_r(&tv.tv_sec, &tm));
2758 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2759 base_filename, timestamp, (int)getpid());
2760 free(base_filename);
2761 free(filename);
2763 return (0);
2766 static int dump_write(int fd, const void *ptr, size_t size)
2768 const char *bufp = (const char *)ptr;
2769 ssize_t bytes_written, bytes_left;
2770 struct rlimit dumpsize;
2771 off_t pos;
2773 bytes_written = 0;
2774 getrlimit(RLIMIT_CORE, &dumpsize);
2775 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2776 if (errno == ESPIPE) { /* not a seekable stream */
2777 bytes_left = size;
2778 } else {
2779 return pos;
2781 } else {
2782 if (dumpsize.rlim_cur <= pos) {
2783 return -1;
2784 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2785 bytes_left = size;
2786 } else {
2787 size_t limit_left=dumpsize.rlim_cur - pos;
2788 bytes_left = limit_left >= size ? size : limit_left ;
2793 * In normal conditions, single write(2) should do but
2794 * in case of socket etc. this mechanism is more portable.
2796 do {
2797 bytes_written = write(fd, bufp, bytes_left);
2798 if (bytes_written < 0) {
2799 if (errno == EINTR)
2800 continue;
2801 return (-1);
2802 } else if (bytes_written == 0) { /* eof */
2803 return (-1);
2805 bufp += bytes_written;
2806 bytes_left -= bytes_written;
2807 } while (bytes_left > 0);
2809 return (0);
2812 static int write_note(struct memelfnote *men, int fd)
2814 struct elf_note en;
2816 en.n_namesz = men->namesz;
2817 en.n_type = men->type;
2818 en.n_descsz = men->datasz;
2820 bswap_note(&en);
2822 if (dump_write(fd, &en, sizeof(en)) != 0)
2823 return (-1);
2824 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2825 return (-1);
2826 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
2827 return (-1);
2829 return (0);
2832 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
2834 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2835 TaskState *ts = (TaskState *)cpu->opaque;
2836 struct elf_thread_status *ets;
2838 ets = g_malloc0(sizeof (*ets));
2839 ets->num_notes = 1; /* only prstatus is dumped */
2840 fill_prstatus(&ets->prstatus, ts, 0);
2841 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2842 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2843 &ets->prstatus);
2845 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
2847 info->notes_size += note_size(&ets->notes[0]);
2850 static void init_note_info(struct elf_note_info *info)
2852 /* Initialize the elf_note_info structure so that it is at
2853 * least safe to call free_note_info() on it. Must be
2854 * called before calling fill_note_info().
2856 memset(info, 0, sizeof (*info));
2857 QTAILQ_INIT(&info->thread_list);
2860 static int fill_note_info(struct elf_note_info *info,
2861 long signr, const CPUArchState *env)
2863 #define NUMNOTES 3
2864 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2865 TaskState *ts = (TaskState *)cpu->opaque;
2866 int i;
2868 info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote));
2869 if (info->notes == NULL)
2870 return (-ENOMEM);
2871 info->prstatus = g_malloc0(sizeof (*info->prstatus));
2872 if (info->prstatus == NULL)
2873 return (-ENOMEM);
2874 info->psinfo = g_malloc0(sizeof (*info->psinfo));
2875 if (info->prstatus == NULL)
2876 return (-ENOMEM);
2879 * First fill in status (and registers) of current thread
2880 * including process info & aux vector.
2882 fill_prstatus(info->prstatus, ts, signr);
2883 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2884 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2885 sizeof (*info->prstatus), info->prstatus);
2886 fill_psinfo(info->psinfo, ts);
2887 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2888 sizeof (*info->psinfo), info->psinfo);
2889 fill_auxv_note(&info->notes[2], ts);
2890 info->numnote = 3;
2892 info->notes_size = 0;
2893 for (i = 0; i < info->numnote; i++)
2894 info->notes_size += note_size(&info->notes[i]);
2896 /* read and fill status of all threads */
2897 cpu_list_lock();
2898 CPU_FOREACH(cpu) {
2899 if (cpu == thread_cpu) {
2900 continue;
2902 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
2904 cpu_list_unlock();
2906 return (0);
2909 static void free_note_info(struct elf_note_info *info)
2911 struct elf_thread_status *ets;
2913 while (!QTAILQ_EMPTY(&info->thread_list)) {
2914 ets = QTAILQ_FIRST(&info->thread_list);
2915 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
2916 g_free(ets);
2919 g_free(info->prstatus);
2920 g_free(info->psinfo);
2921 g_free(info->notes);
2924 static int write_note_info(struct elf_note_info *info, int fd)
2926 struct elf_thread_status *ets;
2927 int i, error = 0;
2929 /* write prstatus, psinfo and auxv for current thread */
2930 for (i = 0; i < info->numnote; i++)
2931 if ((error = write_note(&info->notes[i], fd)) != 0)
2932 return (error);
2934 /* write prstatus for each thread */
2935 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
2936 if ((error = write_note(&ets->notes[0], fd)) != 0)
2937 return (error);
2940 return (0);
2944 * Write out ELF coredump.
2946 * See documentation of ELF object file format in:
2947 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2949 * Coredump format in linux is following:
2951 * 0 +----------------------+ \
2952 * | ELF header | ET_CORE |
2953 * +----------------------+ |
2954 * | ELF program headers | |--- headers
2955 * | - NOTE section | |
2956 * | - PT_LOAD sections | |
2957 * +----------------------+ /
2958 * | NOTEs: |
2959 * | - NT_PRSTATUS |
2960 * | - NT_PRSINFO |
2961 * | - NT_AUXV |
2962 * +----------------------+ <-- aligned to target page
2963 * | Process memory dump |
2964 * : :
2965 * . .
2966 * : :
2967 * | |
2968 * +----------------------+
2970 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2971 * NT_PRSINFO -> struct elf_prpsinfo
2972 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2974 * Format follows System V format as close as possible. Current
2975 * version limitations are as follows:
2976 * - no floating point registers are dumped
2978 * Function returns 0 in case of success, negative errno otherwise.
2980 * TODO: make this work also during runtime: it should be
2981 * possible to force coredump from running process and then
2982 * continue processing. For example qemu could set up SIGUSR2
2983 * handler (provided that target process haven't registered
2984 * handler for that) that does the dump when signal is received.
2986 static int elf_core_dump(int signr, const CPUArchState *env)
2988 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2989 const TaskState *ts = (const TaskState *)cpu->opaque;
2990 struct vm_area_struct *vma = NULL;
2991 char corefile[PATH_MAX];
2992 struct elf_note_info info;
2993 struct elfhdr elf;
2994 struct elf_phdr phdr;
2995 struct rlimit dumpsize;
2996 struct mm_struct *mm = NULL;
2997 off_t offset = 0, data_offset = 0;
2998 int segs = 0;
2999 int fd = -1;
3001 init_note_info(&info);
3003 errno = 0;
3004 getrlimit(RLIMIT_CORE, &dumpsize);
3005 if (dumpsize.rlim_cur == 0)
3006 return 0;
3008 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
3009 return (-errno);
3011 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3012 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3013 return (-errno);
3016 * Walk through target process memory mappings and
3017 * set up structure containing this information. After
3018 * this point vma_xxx functions can be used.
3020 if ((mm = vma_init()) == NULL)
3021 goto out;
3023 walk_memory_regions(mm, vma_walker);
3024 segs = vma_get_mapping_count(mm);
3027 * Construct valid coredump ELF header. We also
3028 * add one more segment for notes.
3030 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3031 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3032 goto out;
3034 /* fill in the in-memory version of notes */
3035 if (fill_note_info(&info, signr, env) < 0)
3036 goto out;
3038 offset += sizeof (elf); /* elf header */
3039 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3041 /* write out notes program header */
3042 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3044 offset += info.notes_size;
3045 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3046 goto out;
3049 * ELF specification wants data to start at page boundary so
3050 * we align it here.
3052 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3055 * Write program headers for memory regions mapped in
3056 * the target process.
3058 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3059 (void) memset(&phdr, 0, sizeof (phdr));
3061 phdr.p_type = PT_LOAD;
3062 phdr.p_offset = offset;
3063 phdr.p_vaddr = vma->vma_start;
3064 phdr.p_paddr = 0;
3065 phdr.p_filesz = vma_dump_size(vma);
3066 offset += phdr.p_filesz;
3067 phdr.p_memsz = vma->vma_end - vma->vma_start;
3068 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3069 if (vma->vma_flags & PROT_WRITE)
3070 phdr.p_flags |= PF_W;
3071 if (vma->vma_flags & PROT_EXEC)
3072 phdr.p_flags |= PF_X;
3073 phdr.p_align = ELF_EXEC_PAGESIZE;
3075 bswap_phdr(&phdr, 1);
3076 dump_write(fd, &phdr, sizeof (phdr));
3080 * Next we write notes just after program headers. No
3081 * alignment needed here.
3083 if (write_note_info(&info, fd) < 0)
3084 goto out;
3086 /* align data to page boundary */
3087 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3088 goto out;
3091 * Finally we can dump process memory into corefile as well.
3093 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3094 abi_ulong addr;
3095 abi_ulong end;
3097 end = vma->vma_start + vma_dump_size(vma);
3099 for (addr = vma->vma_start; addr < end;
3100 addr += TARGET_PAGE_SIZE) {
3101 char page[TARGET_PAGE_SIZE];
3102 int error;
3105 * Read in page from target process memory and
3106 * write it to coredump file.
3108 error = copy_from_user(page, addr, sizeof (page));
3109 if (error != 0) {
3110 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3111 addr);
3112 errno = -error;
3113 goto out;
3115 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3116 goto out;
3120 out:
3121 free_note_info(&info);
3122 if (mm != NULL)
3123 vma_delete(mm);
3124 (void) close(fd);
3126 if (errno != 0)
3127 return (-errno);
3128 return (0);
3130 #endif /* USE_ELF_CORE_DUMP */
3132 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3134 init_thread(regs, infop);