target-ppc: Fix invalid SPR read/write warnings
[qemu/agraf.git] / linux-user / elfload.c
blobddef23e6dcb87bf6bb3e826b7a201b2fa8753104
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include <sys/time.h>
3 #include <sys/param.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <sys/mman.h>
11 #include <sys/resource.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
16 #include "qemu.h"
17 #include "disas/disas.h"
19 #ifdef _ARCH_PPC64
20 #undef ARCH_DLINFO
21 #undef ELF_PLATFORM
22 #undef ELF_HWCAP
23 #undef ELF_CLASS
24 #undef ELF_DATA
25 #undef ELF_ARCH
26 #endif
28 #define ELF_OSABI ELFOSABI_SYSV
30 /* from personality.h */
33 * Flags for bug emulation.
35 * These occupy the top three bytes.
37 enum {
38 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
39 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
40 descriptors (signal handling) */
41 MMAP_PAGE_ZERO = 0x0100000,
42 ADDR_COMPAT_LAYOUT = 0x0200000,
43 READ_IMPLIES_EXEC = 0x0400000,
44 ADDR_LIMIT_32BIT = 0x0800000,
45 SHORT_INODE = 0x1000000,
46 WHOLE_SECONDS = 0x2000000,
47 STICKY_TIMEOUTS = 0x4000000,
48 ADDR_LIMIT_3GB = 0x8000000,
52 * Personality types.
54 * These go in the low byte. Avoid using the top bit, it will
55 * conflict with error returns.
57 enum {
58 PER_LINUX = 0x0000,
59 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
60 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
61 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
62 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
63 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
64 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
65 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
66 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
67 PER_BSD = 0x0006,
68 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
69 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
70 PER_LINUX32 = 0x0008,
71 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
72 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
73 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
74 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
75 PER_RISCOS = 0x000c,
76 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
77 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
78 PER_OSF4 = 0x000f, /* OSF/1 v4 */
79 PER_HPUX = 0x0010,
80 PER_MASK = 0x00ff,
84 * Return the base personality without flags.
86 #define personality(pers) (pers & PER_MASK)
88 /* this flag is uneffective under linux too, should be deleted */
89 #ifndef MAP_DENYWRITE
90 #define MAP_DENYWRITE 0
91 #endif
93 /* should probably go in elf.h */
94 #ifndef ELIBBAD
95 #define ELIBBAD 80
96 #endif
98 #ifdef TARGET_WORDS_BIGENDIAN
99 #define ELF_DATA ELFDATA2MSB
100 #else
101 #define ELF_DATA ELFDATA2LSB
102 #endif
104 #ifdef TARGET_ABI_MIPSN32
105 typedef abi_ullong target_elf_greg_t;
106 #define tswapreg(ptr) tswap64(ptr)
107 #else
108 typedef abi_ulong target_elf_greg_t;
109 #define tswapreg(ptr) tswapal(ptr)
110 #endif
112 #ifdef USE_UID16
113 typedef abi_ushort target_uid_t;
114 typedef abi_ushort target_gid_t;
115 #else
116 typedef abi_uint target_uid_t;
117 typedef abi_uint target_gid_t;
118 #endif
119 typedef abi_int target_pid_t;
121 #ifdef TARGET_I386
123 #define ELF_PLATFORM get_elf_platform()
125 static const char *get_elf_platform(void)
127 static char elf_platform[] = "i386";
128 int family = (thread_env->cpuid_version >> 8) & 0xff;
129 if (family > 6)
130 family = 6;
131 if (family >= 3)
132 elf_platform[1] = '0' + family;
133 return elf_platform;
136 #define ELF_HWCAP get_elf_hwcap()
138 static uint32_t get_elf_hwcap(void)
140 return thread_env->features[FEAT_1_EDX];
143 #ifdef TARGET_X86_64
144 #define ELF_START_MMAP 0x2aaaaab000ULL
145 #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
147 #define ELF_CLASS ELFCLASS64
148 #define ELF_ARCH EM_X86_64
150 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
152 regs->rax = 0;
153 regs->rsp = infop->start_stack;
154 regs->rip = infop->entry;
157 #define ELF_NREG 27
158 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
161 * Note that ELF_NREG should be 29 as there should be place for
162 * TRAPNO and ERR "registers" as well but linux doesn't dump
163 * those.
165 * See linux kernel: arch/x86/include/asm/elf.h
167 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
169 (*regs)[0] = env->regs[15];
170 (*regs)[1] = env->regs[14];
171 (*regs)[2] = env->regs[13];
172 (*regs)[3] = env->regs[12];
173 (*regs)[4] = env->regs[R_EBP];
174 (*regs)[5] = env->regs[R_EBX];
175 (*regs)[6] = env->regs[11];
176 (*regs)[7] = env->regs[10];
177 (*regs)[8] = env->regs[9];
178 (*regs)[9] = env->regs[8];
179 (*regs)[10] = env->regs[R_EAX];
180 (*regs)[11] = env->regs[R_ECX];
181 (*regs)[12] = env->regs[R_EDX];
182 (*regs)[13] = env->regs[R_ESI];
183 (*regs)[14] = env->regs[R_EDI];
184 (*regs)[15] = env->regs[R_EAX]; /* XXX */
185 (*regs)[16] = env->eip;
186 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
187 (*regs)[18] = env->eflags;
188 (*regs)[19] = env->regs[R_ESP];
189 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
190 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
191 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
192 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
193 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
194 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
195 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
198 #else
200 #define ELF_START_MMAP 0x80000000
203 * This is used to ensure we don't load something for the wrong architecture.
205 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
208 * These are used to set parameters in the core dumps.
210 #define ELF_CLASS ELFCLASS32
211 #define ELF_ARCH EM_386
213 static inline void init_thread(struct target_pt_regs *regs,
214 struct image_info *infop)
216 regs->esp = infop->start_stack;
217 regs->eip = infop->entry;
219 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
220 starts %edx contains a pointer to a function which might be
221 registered using `atexit'. This provides a mean for the
222 dynamic linker to call DT_FINI functions for shared libraries
223 that have been loaded before the code runs.
225 A value of 0 tells we have no such handler. */
226 regs->edx = 0;
229 #define ELF_NREG 17
230 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
233 * Note that ELF_NREG should be 19 as there should be place for
234 * TRAPNO and ERR "registers" as well but linux doesn't dump
235 * those.
237 * See linux kernel: arch/x86/include/asm/elf.h
239 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
241 (*regs)[0] = env->regs[R_EBX];
242 (*regs)[1] = env->regs[R_ECX];
243 (*regs)[2] = env->regs[R_EDX];
244 (*regs)[3] = env->regs[R_ESI];
245 (*regs)[4] = env->regs[R_EDI];
246 (*regs)[5] = env->regs[R_EBP];
247 (*regs)[6] = env->regs[R_EAX];
248 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
249 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
250 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
251 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
252 (*regs)[11] = env->regs[R_EAX]; /* XXX */
253 (*regs)[12] = env->eip;
254 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
255 (*regs)[14] = env->eflags;
256 (*regs)[15] = env->regs[R_ESP];
257 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
259 #endif
261 #define USE_ELF_CORE_DUMP
262 #define ELF_EXEC_PAGESIZE 4096
264 #endif
266 #ifdef TARGET_ARM
268 #define ELF_START_MMAP 0x80000000
270 #define elf_check_arch(x) ( (x) == EM_ARM )
272 #define ELF_CLASS ELFCLASS32
273 #define ELF_ARCH EM_ARM
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));
280 regs->ARM_cpsr = 0x10;
281 if (infop->entry & 1)
282 regs->ARM_cpsr |= CPSR_T;
283 regs->ARM_pc = infop->entry & 0xfffffffe;
284 regs->ARM_sp = infop->start_stack;
285 /* FIXME - what to for failure of get_user()? */
286 get_user_ual(regs->ARM_r2, stack + 8); /* envp */
287 get_user_ual(regs->ARM_r1, stack + 4); /* envp */
288 /* XXX: it seems that r0 is zeroed after ! */
289 regs->ARM_r0 = 0;
290 /* For uClinux PIC binaries. */
291 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
292 regs->ARM_r10 = infop->start_data;
295 #define ELF_NREG 18
296 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
298 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
300 (*regs)[0] = tswapreg(env->regs[0]);
301 (*regs)[1] = tswapreg(env->regs[1]);
302 (*regs)[2] = tswapreg(env->regs[2]);
303 (*regs)[3] = tswapreg(env->regs[3]);
304 (*regs)[4] = tswapreg(env->regs[4]);
305 (*regs)[5] = tswapreg(env->regs[5]);
306 (*regs)[6] = tswapreg(env->regs[6]);
307 (*regs)[7] = tswapreg(env->regs[7]);
308 (*regs)[8] = tswapreg(env->regs[8]);
309 (*regs)[9] = tswapreg(env->regs[9]);
310 (*regs)[10] = tswapreg(env->regs[10]);
311 (*regs)[11] = tswapreg(env->regs[11]);
312 (*regs)[12] = tswapreg(env->regs[12]);
313 (*regs)[13] = tswapreg(env->regs[13]);
314 (*regs)[14] = tswapreg(env->regs[14]);
315 (*regs)[15] = tswapreg(env->regs[15]);
317 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
318 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
321 #define USE_ELF_CORE_DUMP
322 #define ELF_EXEC_PAGESIZE 4096
324 enum
326 ARM_HWCAP_ARM_SWP = 1 << 0,
327 ARM_HWCAP_ARM_HALF = 1 << 1,
328 ARM_HWCAP_ARM_THUMB = 1 << 2,
329 ARM_HWCAP_ARM_26BIT = 1 << 3,
330 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
331 ARM_HWCAP_ARM_FPA = 1 << 5,
332 ARM_HWCAP_ARM_VFP = 1 << 6,
333 ARM_HWCAP_ARM_EDSP = 1 << 7,
334 ARM_HWCAP_ARM_JAVA = 1 << 8,
335 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
336 ARM_HWCAP_ARM_THUMBEE = 1 << 10,
337 ARM_HWCAP_ARM_NEON = 1 << 11,
338 ARM_HWCAP_ARM_VFPv3 = 1 << 12,
339 ARM_HWCAP_ARM_VFPv3D16 = 1 << 13,
342 #define TARGET_HAS_VALIDATE_GUEST_SPACE
343 /* Return 1 if the proposed guest space is suitable for the guest.
344 * Return 0 if the proposed guest space isn't suitable, but another
345 * address space should be tried.
346 * Return -1 if there is no way the proposed guest space can be
347 * valid regardless of the base.
348 * The guest code may leave a page mapped and populate it if the
349 * address is suitable.
351 static int validate_guest_space(unsigned long guest_base,
352 unsigned long guest_size)
354 unsigned long real_start, test_page_addr;
356 /* We need to check that we can force a fault on access to the
357 * commpage at 0xffff0fxx
359 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
361 /* If the commpage lies within the already allocated guest space,
362 * then there is no way we can allocate it.
364 if (test_page_addr >= guest_base
365 && test_page_addr <= (guest_base + guest_size)) {
366 return -1;
369 /* Note it needs to be writeable to let us initialise it */
370 real_start = (unsigned long)
371 mmap((void *)test_page_addr, qemu_host_page_size,
372 PROT_READ | PROT_WRITE,
373 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
375 /* If we can't map it then try another address */
376 if (real_start == -1ul) {
377 return 0;
380 if (real_start != test_page_addr) {
381 /* OS didn't put the page where we asked - unmap and reject */
382 munmap((void *)real_start, qemu_host_page_size);
383 return 0;
386 /* Leave the page mapped
387 * Populate it (mmap should have left it all 0'd)
390 /* Kernel helper versions */
391 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
393 /* Now it's populated make it RO */
394 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
395 perror("Protecting guest commpage");
396 exit(-1);
399 return 1; /* All good */
403 #define ELF_HWCAP get_elf_hwcap()
405 static uint32_t get_elf_hwcap(void)
407 CPUARMState *e = thread_env;
408 uint32_t hwcaps = 0;
410 hwcaps |= ARM_HWCAP_ARM_SWP;
411 hwcaps |= ARM_HWCAP_ARM_HALF;
412 hwcaps |= ARM_HWCAP_ARM_THUMB;
413 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
414 hwcaps |= ARM_HWCAP_ARM_FPA;
416 /* probe for the extra features */
417 #define GET_FEATURE(feat, hwcap) \
418 do {if (arm_feature(e, feat)) { hwcaps |= hwcap; } } while (0)
419 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
420 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
421 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
422 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
423 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
424 GET_FEATURE(ARM_FEATURE_VFP_FP16, ARM_HWCAP_ARM_VFPv3D16);
425 #undef GET_FEATURE
427 return hwcaps;
430 #endif
432 #ifdef TARGET_UNICORE32
434 #define ELF_START_MMAP 0x80000000
436 #define elf_check_arch(x) ((x) == EM_UNICORE32)
438 #define ELF_CLASS ELFCLASS32
439 #define ELF_DATA ELFDATA2LSB
440 #define ELF_ARCH EM_UNICORE32
442 static inline void init_thread(struct target_pt_regs *regs,
443 struct image_info *infop)
445 abi_long stack = infop->start_stack;
446 memset(regs, 0, sizeof(*regs));
447 regs->UC32_REG_asr = 0x10;
448 regs->UC32_REG_pc = infop->entry & 0xfffffffe;
449 regs->UC32_REG_sp = infop->start_stack;
450 /* FIXME - what to for failure of get_user()? */
451 get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */
452 get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */
453 /* XXX: it seems that r0 is zeroed after ! */
454 regs->UC32_REG_00 = 0;
457 #define ELF_NREG 34
458 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
460 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
462 (*regs)[0] = env->regs[0];
463 (*regs)[1] = env->regs[1];
464 (*regs)[2] = env->regs[2];
465 (*regs)[3] = env->regs[3];
466 (*regs)[4] = env->regs[4];
467 (*regs)[5] = env->regs[5];
468 (*regs)[6] = env->regs[6];
469 (*regs)[7] = env->regs[7];
470 (*regs)[8] = env->regs[8];
471 (*regs)[9] = env->regs[9];
472 (*regs)[10] = env->regs[10];
473 (*regs)[11] = env->regs[11];
474 (*regs)[12] = env->regs[12];
475 (*regs)[13] = env->regs[13];
476 (*regs)[14] = env->regs[14];
477 (*regs)[15] = env->regs[15];
478 (*regs)[16] = env->regs[16];
479 (*regs)[17] = env->regs[17];
480 (*regs)[18] = env->regs[18];
481 (*regs)[19] = env->regs[19];
482 (*regs)[20] = env->regs[20];
483 (*regs)[21] = env->regs[21];
484 (*regs)[22] = env->regs[22];
485 (*regs)[23] = env->regs[23];
486 (*regs)[24] = env->regs[24];
487 (*regs)[25] = env->regs[25];
488 (*regs)[26] = env->regs[26];
489 (*regs)[27] = env->regs[27];
490 (*regs)[28] = env->regs[28];
491 (*regs)[29] = env->regs[29];
492 (*regs)[30] = env->regs[30];
493 (*regs)[31] = env->regs[31];
495 (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
496 (*regs)[33] = env->regs[0]; /* XXX */
499 #define USE_ELF_CORE_DUMP
500 #define ELF_EXEC_PAGESIZE 4096
502 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
504 #endif
506 #ifdef TARGET_SPARC
507 #ifdef TARGET_SPARC64
509 #define ELF_START_MMAP 0x80000000
510 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
511 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
512 #ifndef TARGET_ABI32
513 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
514 #else
515 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
516 #endif
518 #define ELF_CLASS ELFCLASS64
519 #define ELF_ARCH EM_SPARCV9
521 #define STACK_BIAS 2047
523 static inline void init_thread(struct target_pt_regs *regs,
524 struct image_info *infop)
526 #ifndef TARGET_ABI32
527 regs->tstate = 0;
528 #endif
529 regs->pc = infop->entry;
530 regs->npc = regs->pc + 4;
531 regs->y = 0;
532 #ifdef TARGET_ABI32
533 regs->u_regs[14] = infop->start_stack - 16 * 4;
534 #else
535 if (personality(infop->personality) == PER_LINUX32)
536 regs->u_regs[14] = infop->start_stack - 16 * 4;
537 else
538 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
539 #endif
542 #else
543 #define ELF_START_MMAP 0x80000000
544 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
545 | HWCAP_SPARC_MULDIV)
546 #define elf_check_arch(x) ( (x) == EM_SPARC )
548 #define ELF_CLASS ELFCLASS32
549 #define ELF_ARCH EM_SPARC
551 static inline void init_thread(struct target_pt_regs *regs,
552 struct image_info *infop)
554 regs->psr = 0;
555 regs->pc = infop->entry;
556 regs->npc = regs->pc + 4;
557 regs->y = 0;
558 regs->u_regs[14] = infop->start_stack - 16 * 4;
561 #endif
562 #endif
564 #ifdef TARGET_PPC
566 #define ELF_START_MMAP 0x80000000
568 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
570 #define elf_check_arch(x) ( (x) == EM_PPC64 )
572 #define ELF_CLASS ELFCLASS64
574 #else
576 #define elf_check_arch(x) ( (x) == EM_PPC )
578 #define ELF_CLASS ELFCLASS32
580 #endif
582 #define ELF_ARCH EM_PPC
584 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
585 See arch/powerpc/include/asm/cputable.h. */
586 enum {
587 QEMU_PPC_FEATURE_32 = 0x80000000,
588 QEMU_PPC_FEATURE_64 = 0x40000000,
589 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
590 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
591 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
592 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
593 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
594 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
595 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
596 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
597 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
598 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
599 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
600 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
601 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
602 QEMU_PPC_FEATURE_CELL = 0x00010000,
603 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
604 QEMU_PPC_FEATURE_SMT = 0x00004000,
605 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
606 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
607 QEMU_PPC_FEATURE_PA6T = 0x00000800,
608 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
609 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
610 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
611 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
612 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
614 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
615 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
618 #define ELF_HWCAP get_elf_hwcap()
620 static uint32_t get_elf_hwcap(void)
622 CPUPPCState *e = thread_env;
623 uint32_t features = 0;
625 /* We don't have to be terribly complete here; the high points are
626 Altivec/FP/SPE support. Anything else is just a bonus. */
627 #define GET_FEATURE(flag, feature) \
628 do {if (e->insns_flags & flag) features |= feature; } while(0)
629 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
630 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
631 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
632 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
633 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
634 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
635 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
636 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
637 #undef GET_FEATURE
639 return features;
643 * The requirements here are:
644 * - keep the final alignment of sp (sp & 0xf)
645 * - make sure the 32-bit value at the first 16 byte aligned position of
646 * AUXV is greater than 16 for glibc compatibility.
647 * AT_IGNOREPPC is used for that.
648 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
649 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
651 #define DLINFO_ARCH_ITEMS 5
652 #define ARCH_DLINFO \
653 do { \
654 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
655 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
656 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
657 /* \
658 * Now handle glibc compatibility. \
659 */ \
660 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
661 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
662 } while (0)
664 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
666 _regs->gpr[1] = infop->start_stack;
667 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
668 _regs->gpr[2] = ldq_raw(infop->entry + 8) + infop->load_bias;
669 infop->entry = ldq_raw(infop->entry) + infop->load_bias;
670 #endif
671 _regs->nip = infop->entry;
674 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
675 #define ELF_NREG 48
676 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
678 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
680 int i;
681 target_ulong ccr = 0;
683 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
684 (*regs)[i] = tswapreg(env->gpr[i]);
687 (*regs)[32] = tswapreg(env->nip);
688 (*regs)[33] = tswapreg(env->msr);
689 (*regs)[35] = tswapreg(env->ctr);
690 (*regs)[36] = tswapreg(env->lr);
691 (*regs)[37] = tswapreg(env->xer);
693 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
694 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
696 (*regs)[38] = tswapreg(ccr);
699 #define USE_ELF_CORE_DUMP
700 #define ELF_EXEC_PAGESIZE 4096
702 #endif
704 #ifdef TARGET_MIPS
706 #define ELF_START_MMAP 0x80000000
708 #define elf_check_arch(x) ( (x) == EM_MIPS )
710 #ifdef TARGET_MIPS64
711 #define ELF_CLASS ELFCLASS64
712 #else
713 #define ELF_CLASS ELFCLASS32
714 #endif
715 #define ELF_ARCH EM_MIPS
717 static inline void init_thread(struct target_pt_regs *regs,
718 struct image_info *infop)
720 regs->cp0_status = 2 << CP0St_KSU;
721 regs->cp0_epc = infop->entry;
722 regs->regs[29] = infop->start_stack;
725 /* See linux kernel: arch/mips/include/asm/elf.h. */
726 #define ELF_NREG 45
727 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
729 /* See linux kernel: arch/mips/include/asm/reg.h. */
730 enum {
731 #ifdef TARGET_MIPS64
732 TARGET_EF_R0 = 0,
733 #else
734 TARGET_EF_R0 = 6,
735 #endif
736 TARGET_EF_R26 = TARGET_EF_R0 + 26,
737 TARGET_EF_R27 = TARGET_EF_R0 + 27,
738 TARGET_EF_LO = TARGET_EF_R0 + 32,
739 TARGET_EF_HI = TARGET_EF_R0 + 33,
740 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
741 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
742 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
743 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
746 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
747 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
749 int i;
751 for (i = 0; i < TARGET_EF_R0; i++) {
752 (*regs)[i] = 0;
754 (*regs)[TARGET_EF_R0] = 0;
756 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
757 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
760 (*regs)[TARGET_EF_R26] = 0;
761 (*regs)[TARGET_EF_R27] = 0;
762 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
763 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
764 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
765 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
766 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
767 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
770 #define USE_ELF_CORE_DUMP
771 #define ELF_EXEC_PAGESIZE 4096
773 #endif /* TARGET_MIPS */
775 #ifdef TARGET_MICROBLAZE
777 #define ELF_START_MMAP 0x80000000
779 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
781 #define ELF_CLASS ELFCLASS32
782 #define ELF_ARCH EM_MICROBLAZE
784 static inline void init_thread(struct target_pt_regs *regs,
785 struct image_info *infop)
787 regs->pc = infop->entry;
788 regs->r1 = infop->start_stack;
792 #define ELF_EXEC_PAGESIZE 4096
794 #define USE_ELF_CORE_DUMP
795 #define ELF_NREG 38
796 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
798 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
799 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
801 int i, pos = 0;
803 for (i = 0; i < 32; i++) {
804 (*regs)[pos++] = tswapreg(env->regs[i]);
807 for (i = 0; i < 6; i++) {
808 (*regs)[pos++] = tswapreg(env->sregs[i]);
812 #endif /* TARGET_MICROBLAZE */
814 #ifdef TARGET_OPENRISC
816 #define ELF_START_MMAP 0x08000000
818 #define elf_check_arch(x) ((x) == EM_OPENRISC)
820 #define ELF_ARCH EM_OPENRISC
821 #define ELF_CLASS ELFCLASS32
822 #define ELF_DATA ELFDATA2MSB
824 static inline void init_thread(struct target_pt_regs *regs,
825 struct image_info *infop)
827 regs->pc = infop->entry;
828 regs->gpr[1] = infop->start_stack;
831 #define USE_ELF_CORE_DUMP
832 #define ELF_EXEC_PAGESIZE 8192
834 /* See linux kernel arch/openrisc/include/asm/elf.h. */
835 #define ELF_NREG 34 /* gprs and pc, sr */
836 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
838 static void elf_core_copy_regs(target_elf_gregset_t *regs,
839 const CPUOpenRISCState *env)
841 int i;
843 for (i = 0; i < 32; i++) {
844 (*regs)[i] = tswapreg(env->gpr[i]);
847 (*regs)[32] = tswapreg(env->pc);
848 (*regs)[33] = tswapreg(env->sr);
850 #define ELF_HWCAP 0
851 #define ELF_PLATFORM NULL
853 #endif /* TARGET_OPENRISC */
855 #ifdef TARGET_SH4
857 #define ELF_START_MMAP 0x80000000
859 #define elf_check_arch(x) ( (x) == EM_SH )
861 #define ELF_CLASS ELFCLASS32
862 #define ELF_ARCH EM_SH
864 static inline void init_thread(struct target_pt_regs *regs,
865 struct image_info *infop)
867 /* Check other registers XXXXX */
868 regs->pc = infop->entry;
869 regs->regs[15] = infop->start_stack;
872 /* See linux kernel: arch/sh/include/asm/elf.h. */
873 #define ELF_NREG 23
874 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
876 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
877 enum {
878 TARGET_REG_PC = 16,
879 TARGET_REG_PR = 17,
880 TARGET_REG_SR = 18,
881 TARGET_REG_GBR = 19,
882 TARGET_REG_MACH = 20,
883 TARGET_REG_MACL = 21,
884 TARGET_REG_SYSCALL = 22
887 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
888 const CPUSH4State *env)
890 int i;
892 for (i = 0; i < 16; i++) {
893 (*regs[i]) = tswapreg(env->gregs[i]);
896 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
897 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
898 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
899 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
900 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
901 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
902 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
905 #define USE_ELF_CORE_DUMP
906 #define ELF_EXEC_PAGESIZE 4096
908 #endif
910 #ifdef TARGET_CRIS
912 #define ELF_START_MMAP 0x80000000
914 #define elf_check_arch(x) ( (x) == EM_CRIS )
916 #define ELF_CLASS ELFCLASS32
917 #define ELF_ARCH EM_CRIS
919 static inline void init_thread(struct target_pt_regs *regs,
920 struct image_info *infop)
922 regs->erp = infop->entry;
925 #define ELF_EXEC_PAGESIZE 8192
927 #endif
929 #ifdef TARGET_M68K
931 #define ELF_START_MMAP 0x80000000
933 #define elf_check_arch(x) ( (x) == EM_68K )
935 #define ELF_CLASS ELFCLASS32
936 #define ELF_ARCH EM_68K
938 /* ??? Does this need to do anything?
939 #define ELF_PLAT_INIT(_r) */
941 static inline void init_thread(struct target_pt_regs *regs,
942 struct image_info *infop)
944 regs->usp = infop->start_stack;
945 regs->sr = 0;
946 regs->pc = infop->entry;
949 /* See linux kernel: arch/m68k/include/asm/elf.h. */
950 #define ELF_NREG 20
951 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
953 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
955 (*regs)[0] = tswapreg(env->dregs[1]);
956 (*regs)[1] = tswapreg(env->dregs[2]);
957 (*regs)[2] = tswapreg(env->dregs[3]);
958 (*regs)[3] = tswapreg(env->dregs[4]);
959 (*regs)[4] = tswapreg(env->dregs[5]);
960 (*regs)[5] = tswapreg(env->dregs[6]);
961 (*regs)[6] = tswapreg(env->dregs[7]);
962 (*regs)[7] = tswapreg(env->aregs[0]);
963 (*regs)[8] = tswapreg(env->aregs[1]);
964 (*regs)[9] = tswapreg(env->aregs[2]);
965 (*regs)[10] = tswapreg(env->aregs[3]);
966 (*regs)[11] = tswapreg(env->aregs[4]);
967 (*regs)[12] = tswapreg(env->aregs[5]);
968 (*regs)[13] = tswapreg(env->aregs[6]);
969 (*regs)[14] = tswapreg(env->dregs[0]);
970 (*regs)[15] = tswapreg(env->aregs[7]);
971 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
972 (*regs)[17] = tswapreg(env->sr);
973 (*regs)[18] = tswapreg(env->pc);
974 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
977 #define USE_ELF_CORE_DUMP
978 #define ELF_EXEC_PAGESIZE 8192
980 #endif
982 #ifdef TARGET_ALPHA
984 #define ELF_START_MMAP (0x30000000000ULL)
986 #define elf_check_arch(x) ( (x) == ELF_ARCH )
988 #define ELF_CLASS ELFCLASS64
989 #define ELF_ARCH EM_ALPHA
991 static inline void init_thread(struct target_pt_regs *regs,
992 struct image_info *infop)
994 regs->pc = infop->entry;
995 regs->ps = 8;
996 regs->usp = infop->start_stack;
999 #define ELF_EXEC_PAGESIZE 8192
1001 #endif /* TARGET_ALPHA */
1003 #ifdef TARGET_S390X
1005 #define ELF_START_MMAP (0x20000000000ULL)
1007 #define elf_check_arch(x) ( (x) == ELF_ARCH )
1009 #define ELF_CLASS ELFCLASS64
1010 #define ELF_DATA ELFDATA2MSB
1011 #define ELF_ARCH EM_S390
1013 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1015 regs->psw.addr = infop->entry;
1016 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1017 regs->gprs[15] = infop->start_stack;
1020 #endif /* TARGET_S390X */
1022 #ifndef ELF_PLATFORM
1023 #define ELF_PLATFORM (NULL)
1024 #endif
1026 #ifndef ELF_HWCAP
1027 #define ELF_HWCAP 0
1028 #endif
1030 #ifdef TARGET_ABI32
1031 #undef ELF_CLASS
1032 #define ELF_CLASS ELFCLASS32
1033 #undef bswaptls
1034 #define bswaptls(ptr) bswap32s(ptr)
1035 #endif
1037 #include "elf.h"
1039 struct exec
1041 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1042 unsigned int a_text; /* length of text, in bytes */
1043 unsigned int a_data; /* length of data, in bytes */
1044 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1045 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1046 unsigned int a_entry; /* start address */
1047 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1048 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1052 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1053 #define OMAGIC 0407
1054 #define NMAGIC 0410
1055 #define ZMAGIC 0413
1056 #define QMAGIC 0314
1058 /* Necessary parameters */
1059 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1060 #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
1061 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1063 #define DLINFO_ITEMS 13
1065 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1067 memcpy(to, from, n);
1070 #ifdef BSWAP_NEEDED
1071 static void bswap_ehdr(struct elfhdr *ehdr)
1073 bswap16s(&ehdr->e_type); /* Object file type */
1074 bswap16s(&ehdr->e_machine); /* Architecture */
1075 bswap32s(&ehdr->e_version); /* Object file version */
1076 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1077 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1078 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1079 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1080 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1081 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1082 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1083 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1084 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1085 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1088 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1090 int i;
1091 for (i = 0; i < phnum; ++i, ++phdr) {
1092 bswap32s(&phdr->p_type); /* Segment type */
1093 bswap32s(&phdr->p_flags); /* Segment flags */
1094 bswaptls(&phdr->p_offset); /* Segment file offset */
1095 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1096 bswaptls(&phdr->p_paddr); /* Segment physical address */
1097 bswaptls(&phdr->p_filesz); /* Segment size in file */
1098 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1099 bswaptls(&phdr->p_align); /* Segment alignment */
1103 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1105 int i;
1106 for (i = 0; i < shnum; ++i, ++shdr) {
1107 bswap32s(&shdr->sh_name);
1108 bswap32s(&shdr->sh_type);
1109 bswaptls(&shdr->sh_flags);
1110 bswaptls(&shdr->sh_addr);
1111 bswaptls(&shdr->sh_offset);
1112 bswaptls(&shdr->sh_size);
1113 bswap32s(&shdr->sh_link);
1114 bswap32s(&shdr->sh_info);
1115 bswaptls(&shdr->sh_addralign);
1116 bswaptls(&shdr->sh_entsize);
1120 static void bswap_sym(struct elf_sym *sym)
1122 bswap32s(&sym->st_name);
1123 bswaptls(&sym->st_value);
1124 bswaptls(&sym->st_size);
1125 bswap16s(&sym->st_shndx);
1127 #else
1128 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1129 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1130 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1131 static inline void bswap_sym(struct elf_sym *sym) { }
1132 #endif
1134 #ifdef USE_ELF_CORE_DUMP
1135 static int elf_core_dump(int, const CPUArchState *);
1136 #endif /* USE_ELF_CORE_DUMP */
1137 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1139 /* Verify the portions of EHDR within E_IDENT for the target.
1140 This can be performed before bswapping the entire header. */
1141 static bool elf_check_ident(struct elfhdr *ehdr)
1143 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1144 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1145 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1146 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1147 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1148 && ehdr->e_ident[EI_DATA] == ELF_DATA
1149 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1152 /* Verify the portions of EHDR outside of E_IDENT for the target.
1153 This has to wait until after bswapping the header. */
1154 static bool elf_check_ehdr(struct elfhdr *ehdr)
1156 return (elf_check_arch(ehdr->e_machine)
1157 && ehdr->e_ehsize == sizeof(struct elfhdr)
1158 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1159 && ehdr->e_shentsize == sizeof(struct elf_shdr)
1160 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1164 * 'copy_elf_strings()' copies argument/envelope strings from user
1165 * memory to free pages in kernel mem. These are in a format ready
1166 * to be put directly into the top of new user memory.
1169 static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
1170 abi_ulong p)
1172 char *tmp, *tmp1, *pag = NULL;
1173 int len, offset = 0;
1175 if (!p) {
1176 return 0; /* bullet-proofing */
1178 while (argc-- > 0) {
1179 tmp = argv[argc];
1180 if (!tmp) {
1181 fprintf(stderr, "VFS: argc is wrong");
1182 exit(-1);
1184 tmp1 = tmp;
1185 while (*tmp++);
1186 len = tmp - tmp1;
1187 if (p < len) { /* this shouldn't happen - 128kB */
1188 return 0;
1190 while (len) {
1191 --p; --tmp; --len;
1192 if (--offset < 0) {
1193 offset = p % TARGET_PAGE_SIZE;
1194 pag = (char *)page[p/TARGET_PAGE_SIZE];
1195 if (!pag) {
1196 pag = g_try_malloc0(TARGET_PAGE_SIZE);
1197 page[p/TARGET_PAGE_SIZE] = pag;
1198 if (!pag)
1199 return 0;
1202 if (len == 0 || offset == 0) {
1203 *(pag + offset) = *tmp;
1205 else {
1206 int bytes_to_copy = (len > offset) ? offset : len;
1207 tmp -= bytes_to_copy;
1208 p -= bytes_to_copy;
1209 offset -= bytes_to_copy;
1210 len -= bytes_to_copy;
1211 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
1215 return p;
1218 static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
1219 struct image_info *info)
1221 abi_ulong stack_base, size, error, guard;
1222 int i;
1224 /* Create enough stack to hold everything. If we don't use
1225 it for args, we'll use it for something else. */
1226 size = guest_stack_size;
1227 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) {
1228 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1230 guard = TARGET_PAGE_SIZE;
1231 if (guard < qemu_real_host_page_size) {
1232 guard = qemu_real_host_page_size;
1235 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1236 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1237 if (error == -1) {
1238 perror("mmap stack");
1239 exit(-1);
1242 /* We reserve one extra page at the top of the stack as guard. */
1243 target_mprotect(error, guard, PROT_NONE);
1245 info->stack_limit = error + guard;
1246 stack_base = info->stack_limit + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1247 p += stack_base;
1249 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1250 if (bprm->page[i]) {
1251 info->rss++;
1252 /* FIXME - check return value of memcpy_to_target() for failure */
1253 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
1254 g_free(bprm->page[i]);
1256 stack_base += TARGET_PAGE_SIZE;
1258 return p;
1261 /* Map and zero the bss. We need to explicitly zero any fractional pages
1262 after the data section (i.e. bss). */
1263 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1265 uintptr_t host_start, host_map_start, host_end;
1267 last_bss = TARGET_PAGE_ALIGN(last_bss);
1269 /* ??? There is confusion between qemu_real_host_page_size and
1270 qemu_host_page_size here and elsewhere in target_mmap, which
1271 may lead to the end of the data section mapping from the file
1272 not being mapped. At least there was an explicit test and
1273 comment for that here, suggesting that "the file size must
1274 be known". The comment probably pre-dates the introduction
1275 of the fstat system call in target_mmap which does in fact
1276 find out the size. What isn't clear is if the workaround
1277 here is still actually needed. For now, continue with it,
1278 but merge it with the "normal" mmap that would allocate the bss. */
1280 host_start = (uintptr_t) g2h(elf_bss);
1281 host_end = (uintptr_t) g2h(last_bss);
1282 host_map_start = (host_start + qemu_real_host_page_size - 1);
1283 host_map_start &= -qemu_real_host_page_size;
1285 if (host_map_start < host_end) {
1286 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1287 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1288 if (p == MAP_FAILED) {
1289 perror("cannot mmap brk");
1290 exit(-1);
1293 /* Since we didn't use target_mmap, make sure to record
1294 the validity of the pages with qemu. */
1295 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot|PAGE_VALID);
1298 if (host_start < host_map_start) {
1299 memset((void *)host_start, 0, host_map_start - host_start);
1303 #ifdef CONFIG_USE_FDPIC
1304 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1306 uint16_t n;
1307 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1309 /* elf32_fdpic_loadseg */
1310 n = info->nsegs;
1311 while (n--) {
1312 sp -= 12;
1313 put_user_u32(loadsegs[n].addr, sp+0);
1314 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1315 put_user_u32(loadsegs[n].p_memsz, sp+8);
1318 /* elf32_fdpic_loadmap */
1319 sp -= 4;
1320 put_user_u16(0, sp+0); /* version */
1321 put_user_u16(info->nsegs, sp+2); /* nsegs */
1323 info->personality = PER_LINUX_FDPIC;
1324 info->loadmap_addr = sp;
1326 return sp;
1328 #endif
1330 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1331 struct elfhdr *exec,
1332 struct image_info *info,
1333 struct image_info *interp_info)
1335 abi_ulong sp;
1336 abi_ulong sp_auxv;
1337 int size;
1338 int i;
1339 abi_ulong u_rand_bytes;
1340 uint8_t k_rand_bytes[16];
1341 abi_ulong u_platform;
1342 const char *k_platform;
1343 const int n = sizeof(elf_addr_t);
1345 sp = p;
1347 #ifdef CONFIG_USE_FDPIC
1348 /* Needs to be before we load the env/argc/... */
1349 if (elf_is_fdpic(exec)) {
1350 /* Need 4 byte alignment for these structs */
1351 sp &= ~3;
1352 sp = loader_build_fdpic_loadmap(info, sp);
1353 info->other_info = interp_info;
1354 if (interp_info) {
1355 interp_info->other_info = info;
1356 sp = loader_build_fdpic_loadmap(interp_info, sp);
1359 #endif
1361 u_platform = 0;
1362 k_platform = ELF_PLATFORM;
1363 if (k_platform) {
1364 size_t len = strlen(k_platform) + 1;
1365 sp -= (len + n - 1) & ~(n - 1);
1366 u_platform = sp;
1367 /* FIXME - check return value of memcpy_to_target() for failure */
1368 memcpy_to_target(sp, k_platform, len);
1372 * Generate 16 random bytes for userspace PRNG seeding (not
1373 * cryptically secure but it's not the aim of QEMU).
1375 srand((unsigned int) time(NULL));
1376 for (i = 0; i < 16; i++) {
1377 k_rand_bytes[i] = rand();
1379 sp -= 16;
1380 u_rand_bytes = sp;
1381 /* FIXME - check return value of memcpy_to_target() for failure */
1382 memcpy_to_target(sp, k_rand_bytes, 16);
1385 * Force 16 byte _final_ alignment here for generality.
1387 sp = sp &~ (abi_ulong)15;
1388 size = (DLINFO_ITEMS + 1) * 2;
1389 if (k_platform)
1390 size += 2;
1391 #ifdef DLINFO_ARCH_ITEMS
1392 size += DLINFO_ARCH_ITEMS * 2;
1393 #endif
1394 size += envc + argc + 2;
1395 size += 1; /* argc itself */
1396 size *= n;
1397 if (size & 15)
1398 sp -= 16 - (size & 15);
1400 /* This is correct because Linux defines
1401 * elf_addr_t as Elf32_Off / Elf64_Off
1403 #define NEW_AUX_ENT(id, val) do { \
1404 sp -= n; put_user_ual(val, sp); \
1405 sp -= n; put_user_ual(id, sp); \
1406 } while(0)
1408 sp_auxv = sp;
1409 NEW_AUX_ENT (AT_NULL, 0);
1411 /* There must be exactly DLINFO_ITEMS entries here. */
1412 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1413 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1414 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1415 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
1416 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1417 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1418 NEW_AUX_ENT(AT_ENTRY, info->entry);
1419 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1420 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1421 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1422 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1423 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1424 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1425 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1427 if (k_platform)
1428 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1429 #ifdef ARCH_DLINFO
1431 * ARCH_DLINFO must come last so platform specific code can enforce
1432 * special alignment requirements on the AUXV if necessary (eg. PPC).
1434 ARCH_DLINFO;
1435 #endif
1436 #undef NEW_AUX_ENT
1438 info->saved_auxv = sp;
1439 info->auxv_len = sp_auxv - sp;
1441 sp = loader_build_argptr(envc, argc, sp, p, 0);
1442 return sp;
1445 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1446 /* If the guest doesn't have a validation function just agree */
1447 static int validate_guest_space(unsigned long guest_base,
1448 unsigned long guest_size)
1450 return 1;
1452 #endif
1454 unsigned long init_guest_space(unsigned long host_start,
1455 unsigned long host_size,
1456 unsigned long guest_start,
1457 bool fixed)
1459 unsigned long current_start, real_start;
1460 int flags;
1462 assert(host_start || host_size);
1464 /* If just a starting address is given, then just verify that
1465 * address. */
1466 if (host_start && !host_size) {
1467 if (validate_guest_space(host_start, host_size) == 1) {
1468 return host_start;
1469 } else {
1470 return (unsigned long)-1;
1474 /* Setup the initial flags and start address. */
1475 current_start = host_start & qemu_host_page_mask;
1476 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1477 if (fixed) {
1478 flags |= MAP_FIXED;
1481 /* Otherwise, a non-zero size region of memory needs to be mapped
1482 * and validated. */
1483 while (1) {
1484 unsigned long real_size = host_size;
1486 /* Do not use mmap_find_vma here because that is limited to the
1487 * guest address space. We are going to make the
1488 * guest address space fit whatever we're given.
1490 real_start = (unsigned long)
1491 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1492 if (real_start == (unsigned long)-1) {
1493 return (unsigned long)-1;
1496 /* Ensure the address is properly aligned. */
1497 if (real_start & ~qemu_host_page_mask) {
1498 munmap((void *)real_start, host_size);
1499 real_size = host_size + qemu_host_page_size;
1500 real_start = (unsigned long)
1501 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1502 if (real_start == (unsigned long)-1) {
1503 return (unsigned long)-1;
1505 real_start = HOST_PAGE_ALIGN(real_start);
1508 /* Check to see if the address is valid. */
1509 if (!host_start || real_start == current_start) {
1510 int valid = validate_guest_space(real_start - guest_start,
1511 real_size);
1512 if (valid == 1) {
1513 break;
1514 } else if (valid == -1) {
1515 return (unsigned long)-1;
1517 /* valid == 0, so try again. */
1520 /* That address didn't work. Unmap and try a different one.
1521 * The address the host picked because is typically right at
1522 * the top of the host address space and leaves the guest with
1523 * no usable address space. Resort to a linear search. We
1524 * already compensated for mmap_min_addr, so this should not
1525 * happen often. Probably means we got unlucky and host
1526 * address space randomization put a shared library somewhere
1527 * inconvenient.
1529 munmap((void *)real_start, host_size);
1530 current_start += qemu_host_page_size;
1531 if (host_start == current_start) {
1532 /* Theoretically possible if host doesn't have any suitably
1533 * aligned areas. Normally the first mmap will fail.
1535 return (unsigned long)-1;
1539 qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size);
1541 return real_start;
1544 static void probe_guest_base(const char *image_name,
1545 abi_ulong loaddr, abi_ulong hiaddr)
1547 /* Probe for a suitable guest base address, if the user has not set
1548 * it explicitly, and set guest_base appropriately.
1549 * In case of error we will print a suitable message and exit.
1551 #if defined(CONFIG_USE_GUEST_BASE)
1552 const char *errmsg;
1553 if (!have_guest_base && !reserved_va) {
1554 unsigned long host_start, real_start, host_size;
1556 /* Round addresses to page boundaries. */
1557 loaddr &= qemu_host_page_mask;
1558 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1560 if (loaddr < mmap_min_addr) {
1561 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1562 } else {
1563 host_start = loaddr;
1564 if (host_start != loaddr) {
1565 errmsg = "Address overflow loading ELF binary";
1566 goto exit_errmsg;
1569 host_size = hiaddr - loaddr;
1571 /* Setup the initial guest memory space with ranges gleaned from
1572 * the ELF image that is being loaded.
1574 real_start = init_guest_space(host_start, host_size, loaddr, false);
1575 if (real_start == (unsigned long)-1) {
1576 errmsg = "Unable to find space for application";
1577 goto exit_errmsg;
1579 guest_base = real_start - loaddr;
1581 qemu_log("Relocating guest address space from 0x"
1582 TARGET_ABI_FMT_lx " to 0x%lx\n",
1583 loaddr, real_start);
1585 return;
1587 exit_errmsg:
1588 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1589 exit(-1);
1590 #endif
1594 /* Load an ELF image into the address space.
1596 IMAGE_NAME is the filename of the image, to use in error messages.
1597 IMAGE_FD is the open file descriptor for the image.
1599 BPRM_BUF is a copy of the beginning of the file; this of course
1600 contains the elf file header at offset 0. It is assumed that this
1601 buffer is sufficiently aligned to present no problems to the host
1602 in accessing data at aligned offsets within the buffer.
1604 On return: INFO values will be filled in, as necessary or available. */
1606 static void load_elf_image(const char *image_name, int image_fd,
1607 struct image_info *info, char **pinterp_name,
1608 char bprm_buf[BPRM_BUF_SIZE])
1610 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1611 struct elf_phdr *phdr;
1612 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1613 int i, retval;
1614 const char *errmsg;
1616 /* First of all, some simple consistency checks */
1617 errmsg = "Invalid ELF image for this architecture";
1618 if (!elf_check_ident(ehdr)) {
1619 goto exit_errmsg;
1621 bswap_ehdr(ehdr);
1622 if (!elf_check_ehdr(ehdr)) {
1623 goto exit_errmsg;
1626 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1627 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1628 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
1629 } else {
1630 phdr = (struct elf_phdr *) alloca(i);
1631 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
1632 if (retval != i) {
1633 goto exit_read;
1636 bswap_phdr(phdr, ehdr->e_phnum);
1638 #ifdef CONFIG_USE_FDPIC
1639 info->nsegs = 0;
1640 info->pt_dynamic_addr = 0;
1641 #endif
1643 /* Find the maximum size of the image and allocate an appropriate
1644 amount of memory to handle that. */
1645 loaddr = -1, hiaddr = 0;
1646 for (i = 0; i < ehdr->e_phnum; ++i) {
1647 if (phdr[i].p_type == PT_LOAD) {
1648 abi_ulong a = phdr[i].p_vaddr;
1649 if (a < loaddr) {
1650 loaddr = a;
1652 a += phdr[i].p_memsz;
1653 if (a > hiaddr) {
1654 hiaddr = a;
1656 #ifdef CONFIG_USE_FDPIC
1657 ++info->nsegs;
1658 #endif
1662 load_addr = loaddr;
1663 if (ehdr->e_type == ET_DYN) {
1664 /* The image indicates that it can be loaded anywhere. Find a
1665 location that can hold the memory space required. If the
1666 image is pre-linked, LOADDR will be non-zero. Since we do
1667 not supply MAP_FIXED here we'll use that address if and
1668 only if it remains available. */
1669 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
1670 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
1671 -1, 0);
1672 if (load_addr == -1) {
1673 goto exit_perror;
1675 } else if (pinterp_name != NULL) {
1676 /* This is the main executable. Make sure that the low
1677 address does not conflict with MMAP_MIN_ADDR or the
1678 QEMU application itself. */
1679 probe_guest_base(image_name, loaddr, hiaddr);
1681 load_bias = load_addr - loaddr;
1683 #ifdef CONFIG_USE_FDPIC
1685 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
1686 g_malloc(sizeof(*loadsegs) * info->nsegs);
1688 for (i = 0; i < ehdr->e_phnum; ++i) {
1689 switch (phdr[i].p_type) {
1690 case PT_DYNAMIC:
1691 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
1692 break;
1693 case PT_LOAD:
1694 loadsegs->addr = phdr[i].p_vaddr + load_bias;
1695 loadsegs->p_vaddr = phdr[i].p_vaddr;
1696 loadsegs->p_memsz = phdr[i].p_memsz;
1697 ++loadsegs;
1698 break;
1702 #endif
1704 info->load_bias = load_bias;
1705 info->load_addr = load_addr;
1706 info->entry = ehdr->e_entry + load_bias;
1707 info->start_code = -1;
1708 info->end_code = 0;
1709 info->start_data = -1;
1710 info->end_data = 0;
1711 info->brk = 0;
1712 info->elf_flags = ehdr->e_flags;
1714 for (i = 0; i < ehdr->e_phnum; i++) {
1715 struct elf_phdr *eppnt = phdr + i;
1716 if (eppnt->p_type == PT_LOAD) {
1717 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
1718 int elf_prot = 0;
1720 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1721 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1722 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1724 vaddr = load_bias + eppnt->p_vaddr;
1725 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
1726 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
1728 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
1729 elf_prot, MAP_PRIVATE | MAP_FIXED,
1730 image_fd, eppnt->p_offset - vaddr_po);
1731 if (error == -1) {
1732 goto exit_perror;
1735 vaddr_ef = vaddr + eppnt->p_filesz;
1736 vaddr_em = vaddr + eppnt->p_memsz;
1738 /* If the load segment requests extra zeros (e.g. bss), map it. */
1739 if (vaddr_ef < vaddr_em) {
1740 zero_bss(vaddr_ef, vaddr_em, elf_prot);
1743 /* Find the full program boundaries. */
1744 if (elf_prot & PROT_EXEC) {
1745 if (vaddr < info->start_code) {
1746 info->start_code = vaddr;
1748 if (vaddr_ef > info->end_code) {
1749 info->end_code = vaddr_ef;
1752 if (elf_prot & PROT_WRITE) {
1753 if (vaddr < info->start_data) {
1754 info->start_data = vaddr;
1756 if (vaddr_ef > info->end_data) {
1757 info->end_data = vaddr_ef;
1759 if (vaddr_em > info->brk) {
1760 info->brk = vaddr_em;
1763 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
1764 char *interp_name;
1766 if (*pinterp_name) {
1767 errmsg = "Multiple PT_INTERP entries";
1768 goto exit_errmsg;
1770 interp_name = malloc(eppnt->p_filesz);
1771 if (!interp_name) {
1772 goto exit_perror;
1775 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
1776 memcpy(interp_name, bprm_buf + eppnt->p_offset,
1777 eppnt->p_filesz);
1778 } else {
1779 retval = pread(image_fd, interp_name, eppnt->p_filesz,
1780 eppnt->p_offset);
1781 if (retval != eppnt->p_filesz) {
1782 goto exit_perror;
1785 if (interp_name[eppnt->p_filesz - 1] != 0) {
1786 errmsg = "Invalid PT_INTERP entry";
1787 goto exit_errmsg;
1789 *pinterp_name = interp_name;
1793 if (info->end_data == 0) {
1794 info->start_data = info->end_code;
1795 info->end_data = info->end_code;
1796 info->brk = info->end_code;
1799 if (qemu_log_enabled()) {
1800 load_symbols(ehdr, image_fd, load_bias);
1803 close(image_fd);
1804 return;
1806 exit_read:
1807 if (retval >= 0) {
1808 errmsg = "Incomplete read of file header";
1809 goto exit_errmsg;
1811 exit_perror:
1812 errmsg = strerror(errno);
1813 exit_errmsg:
1814 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1815 exit(-1);
1818 static void load_elf_interp(const char *filename, struct image_info *info,
1819 char bprm_buf[BPRM_BUF_SIZE])
1821 int fd, retval;
1823 fd = open(path(filename), O_RDONLY);
1824 if (fd < 0) {
1825 goto exit_perror;
1828 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
1829 if (retval < 0) {
1830 goto exit_perror;
1832 if (retval < BPRM_BUF_SIZE) {
1833 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
1836 load_elf_image(filename, fd, info, NULL, bprm_buf);
1837 return;
1839 exit_perror:
1840 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
1841 exit(-1);
1844 static int symfind(const void *s0, const void *s1)
1846 target_ulong addr = *(target_ulong *)s0;
1847 struct elf_sym *sym = (struct elf_sym *)s1;
1848 int result = 0;
1849 if (addr < sym->st_value) {
1850 result = -1;
1851 } else if (addr >= sym->st_value + sym->st_size) {
1852 result = 1;
1854 return result;
1857 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
1859 #if ELF_CLASS == ELFCLASS32
1860 struct elf_sym *syms = s->disas_symtab.elf32;
1861 #else
1862 struct elf_sym *syms = s->disas_symtab.elf64;
1863 #endif
1865 // binary search
1866 struct elf_sym *sym;
1868 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
1869 if (sym != NULL) {
1870 return s->disas_strtab + sym->st_name;
1873 return "";
1876 /* FIXME: This should use elf_ops.h */
1877 static int symcmp(const void *s0, const void *s1)
1879 struct elf_sym *sym0 = (struct elf_sym *)s0;
1880 struct elf_sym *sym1 = (struct elf_sym *)s1;
1881 return (sym0->st_value < sym1->st_value)
1882 ? -1
1883 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
1886 /* Best attempt to load symbols from this ELF object. */
1887 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
1889 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
1890 struct elf_shdr *shdr;
1891 char *strings = NULL;
1892 struct syminfo *s = NULL;
1893 struct elf_sym *new_syms, *syms = NULL;
1895 shnum = hdr->e_shnum;
1896 i = shnum * sizeof(struct elf_shdr);
1897 shdr = (struct elf_shdr *)alloca(i);
1898 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
1899 return;
1902 bswap_shdr(shdr, shnum);
1903 for (i = 0; i < shnum; ++i) {
1904 if (shdr[i].sh_type == SHT_SYMTAB) {
1905 sym_idx = i;
1906 str_idx = shdr[i].sh_link;
1907 goto found;
1911 /* There will be no symbol table if the file was stripped. */
1912 return;
1914 found:
1915 /* Now know where the strtab and symtab are. Snarf them. */
1916 s = malloc(sizeof(*s));
1917 if (!s) {
1918 goto give_up;
1921 i = shdr[str_idx].sh_size;
1922 s->disas_strtab = strings = malloc(i);
1923 if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
1924 goto give_up;
1927 i = shdr[sym_idx].sh_size;
1928 syms = malloc(i);
1929 if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
1930 goto give_up;
1933 nsyms = i / sizeof(struct elf_sym);
1934 for (i = 0; i < nsyms; ) {
1935 bswap_sym(syms + i);
1936 /* Throw away entries which we do not need. */
1937 if (syms[i].st_shndx == SHN_UNDEF
1938 || syms[i].st_shndx >= SHN_LORESERVE
1939 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
1940 if (i < --nsyms) {
1941 syms[i] = syms[nsyms];
1943 } else {
1944 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
1945 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
1946 syms[i].st_value &= ~(target_ulong)1;
1947 #endif
1948 syms[i].st_value += load_bias;
1949 i++;
1953 /* No "useful" symbol. */
1954 if (nsyms == 0) {
1955 goto give_up;
1958 /* Attempt to free the storage associated with the local symbols
1959 that we threw away. Whether or not this has any effect on the
1960 memory allocation depends on the malloc implementation and how
1961 many symbols we managed to discard. */
1962 new_syms = realloc(syms, nsyms * sizeof(*syms));
1963 if (new_syms == NULL) {
1964 goto give_up;
1966 syms = new_syms;
1968 qsort(syms, nsyms, sizeof(*syms), symcmp);
1970 s->disas_num_syms = nsyms;
1971 #if ELF_CLASS == ELFCLASS32
1972 s->disas_symtab.elf32 = syms;
1973 #else
1974 s->disas_symtab.elf64 = syms;
1975 #endif
1976 s->lookup_symbol = lookup_symbolxx;
1977 s->next = syminfos;
1978 syminfos = s;
1980 return;
1982 give_up:
1983 free(s);
1984 free(strings);
1985 free(syms);
1988 int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
1989 struct image_info * info)
1991 struct image_info interp_info;
1992 struct elfhdr elf_ex;
1993 char *elf_interpreter = NULL;
1995 info->start_mmap = (abi_ulong)ELF_START_MMAP;
1996 info->mmap = 0;
1997 info->rss = 0;
1999 load_elf_image(bprm->filename, bprm->fd, info,
2000 &elf_interpreter, bprm->buf);
2002 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2003 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2004 when we load the interpreter. */
2005 elf_ex = *(struct elfhdr *)bprm->buf;
2007 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
2008 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
2009 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
2010 if (!bprm->p) {
2011 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2012 exit(-1);
2015 /* Do this so that we can load the interpreter, if need be. We will
2016 change some of these later */
2017 bprm->p = setup_arg_pages(bprm->p, bprm, info);
2019 if (elf_interpreter) {
2020 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2022 /* If the program interpreter is one of these two, then assume
2023 an iBCS2 image. Otherwise assume a native linux image. */
2025 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2026 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2027 info->personality = PER_SVR4;
2029 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2030 and some applications "depend" upon this behavior. Since
2031 we do not have the power to recompile these, we emulate
2032 the SVr4 behavior. Sigh. */
2033 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2034 MAP_FIXED | MAP_PRIVATE, -1, 0);
2038 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2039 info, (elf_interpreter ? &interp_info : NULL));
2040 info->start_stack = bprm->p;
2042 /* If we have an interpreter, set that as the program's entry point.
2043 Copy the load_bias as well, to help PPC64 interpret the entry
2044 point as a function descriptor. Do this after creating elf tables
2045 so that we copy the original program entry point into the AUXV. */
2046 if (elf_interpreter) {
2047 info->load_bias = interp_info.load_bias;
2048 info->entry = interp_info.entry;
2049 free(elf_interpreter);
2052 #ifdef USE_ELF_CORE_DUMP
2053 bprm->core_dump = &elf_core_dump;
2054 #endif
2056 return 0;
2059 #ifdef USE_ELF_CORE_DUMP
2061 * Definitions to generate Intel SVR4-like core files.
2062 * These mostly have the same names as the SVR4 types with "target_elf_"
2063 * tacked on the front to prevent clashes with linux definitions,
2064 * and the typedef forms have been avoided. This is mostly like
2065 * the SVR4 structure, but more Linuxy, with things that Linux does
2066 * not support and which gdb doesn't really use excluded.
2068 * Fields we don't dump (their contents is zero) in linux-user qemu
2069 * are marked with XXX.
2071 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2073 * Porting ELF coredump for target is (quite) simple process. First you
2074 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2075 * the target resides):
2077 * #define USE_ELF_CORE_DUMP
2079 * Next you define type of register set used for dumping. ELF specification
2080 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2082 * typedef <target_regtype> target_elf_greg_t;
2083 * #define ELF_NREG <number of registers>
2084 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2086 * Last step is to implement target specific function that copies registers
2087 * from given cpu into just specified register set. Prototype is:
2089 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2090 * const CPUArchState *env);
2092 * Parameters:
2093 * regs - copy register values into here (allocated and zeroed by caller)
2094 * env - copy registers from here
2096 * Example for ARM target is provided in this file.
2099 /* An ELF note in memory */
2100 struct memelfnote {
2101 const char *name;
2102 size_t namesz;
2103 size_t namesz_rounded;
2104 int type;
2105 size_t datasz;
2106 size_t datasz_rounded;
2107 void *data;
2108 size_t notesz;
2111 struct target_elf_siginfo {
2112 abi_int si_signo; /* signal number */
2113 abi_int si_code; /* extra code */
2114 abi_int si_errno; /* errno */
2117 struct target_elf_prstatus {
2118 struct target_elf_siginfo pr_info; /* Info associated with signal */
2119 abi_short pr_cursig; /* Current signal */
2120 abi_ulong pr_sigpend; /* XXX */
2121 abi_ulong pr_sighold; /* XXX */
2122 target_pid_t pr_pid;
2123 target_pid_t pr_ppid;
2124 target_pid_t pr_pgrp;
2125 target_pid_t pr_sid;
2126 struct target_timeval pr_utime; /* XXX User time */
2127 struct target_timeval pr_stime; /* XXX System time */
2128 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2129 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2130 target_elf_gregset_t pr_reg; /* GP registers */
2131 abi_int pr_fpvalid; /* XXX */
2134 #define ELF_PRARGSZ (80) /* Number of chars for args */
2136 struct target_elf_prpsinfo {
2137 char pr_state; /* numeric process state */
2138 char pr_sname; /* char for pr_state */
2139 char pr_zomb; /* zombie */
2140 char pr_nice; /* nice val */
2141 abi_ulong pr_flag; /* flags */
2142 target_uid_t pr_uid;
2143 target_gid_t pr_gid;
2144 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2145 /* Lots missing */
2146 char pr_fname[16]; /* filename of executable */
2147 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2150 /* Here is the structure in which status of each thread is captured. */
2151 struct elf_thread_status {
2152 QTAILQ_ENTRY(elf_thread_status) ets_link;
2153 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2154 #if 0
2155 elf_fpregset_t fpu; /* NT_PRFPREG */
2156 struct task_struct *thread;
2157 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2158 #endif
2159 struct memelfnote notes[1];
2160 int num_notes;
2163 struct elf_note_info {
2164 struct memelfnote *notes;
2165 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2166 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2168 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2169 #if 0
2171 * Current version of ELF coredump doesn't support
2172 * dumping fp regs etc.
2174 elf_fpregset_t *fpu;
2175 elf_fpxregset_t *xfpu;
2176 int thread_status_size;
2177 #endif
2178 int notes_size;
2179 int numnote;
2182 struct vm_area_struct {
2183 abi_ulong vma_start; /* start vaddr of memory region */
2184 abi_ulong vma_end; /* end vaddr of memory region */
2185 abi_ulong vma_flags; /* protection etc. flags for the region */
2186 QTAILQ_ENTRY(vm_area_struct) vma_link;
2189 struct mm_struct {
2190 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2191 int mm_count; /* number of mappings */
2194 static struct mm_struct *vma_init(void);
2195 static void vma_delete(struct mm_struct *);
2196 static int vma_add_mapping(struct mm_struct *, abi_ulong,
2197 abi_ulong, abi_ulong);
2198 static int vma_get_mapping_count(const struct mm_struct *);
2199 static struct vm_area_struct *vma_first(const struct mm_struct *);
2200 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2201 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2202 static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
2203 unsigned long flags);
2205 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2206 static void fill_note(struct memelfnote *, const char *, int,
2207 unsigned int, void *);
2208 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2209 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2210 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2211 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2212 static size_t note_size(const struct memelfnote *);
2213 static void free_note_info(struct elf_note_info *);
2214 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2215 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2216 static int core_dump_filename(const TaskState *, char *, size_t);
2218 static int dump_write(int, const void *, size_t);
2219 static int write_note(struct memelfnote *, int);
2220 static int write_note_info(struct elf_note_info *, int);
2222 #ifdef BSWAP_NEEDED
2223 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2225 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2226 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2227 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2228 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2229 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2230 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2231 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2232 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2233 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2234 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2235 /* cpu times are not filled, so we skip them */
2236 /* regs should be in correct format already */
2237 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2240 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2242 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2243 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2244 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2245 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2246 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2247 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2248 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2251 static void bswap_note(struct elf_note *en)
2253 bswap32s(&en->n_namesz);
2254 bswap32s(&en->n_descsz);
2255 bswap32s(&en->n_type);
2257 #else
2258 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2259 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2260 static inline void bswap_note(struct elf_note *en) { }
2261 #endif /* BSWAP_NEEDED */
2264 * Minimal support for linux memory regions. These are needed
2265 * when we are finding out what memory exactly belongs to
2266 * emulated process. No locks needed here, as long as
2267 * thread that received the signal is stopped.
2270 static struct mm_struct *vma_init(void)
2272 struct mm_struct *mm;
2274 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2275 return (NULL);
2277 mm->mm_count = 0;
2278 QTAILQ_INIT(&mm->mm_mmap);
2280 return (mm);
2283 static void vma_delete(struct mm_struct *mm)
2285 struct vm_area_struct *vma;
2287 while ((vma = vma_first(mm)) != NULL) {
2288 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2289 g_free(vma);
2291 g_free(mm);
2294 static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
2295 abi_ulong end, abi_ulong flags)
2297 struct vm_area_struct *vma;
2299 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2300 return (-1);
2302 vma->vma_start = start;
2303 vma->vma_end = end;
2304 vma->vma_flags = flags;
2306 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2307 mm->mm_count++;
2309 return (0);
2312 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2314 return (QTAILQ_FIRST(&mm->mm_mmap));
2317 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2319 return (QTAILQ_NEXT(vma, vma_link));
2322 static int vma_get_mapping_count(const struct mm_struct *mm)
2324 return (mm->mm_count);
2328 * Calculate file (dump) size of given memory region.
2330 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2332 /* if we cannot even read the first page, skip it */
2333 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2334 return (0);
2337 * Usually we don't dump executable pages as they contain
2338 * non-writable code that debugger can read directly from
2339 * target library etc. However, thread stacks are marked
2340 * also executable so we read in first page of given region
2341 * and check whether it contains elf header. If there is
2342 * no elf header, we dump it.
2344 if (vma->vma_flags & PROT_EXEC) {
2345 char page[TARGET_PAGE_SIZE];
2347 copy_from_user(page, vma->vma_start, sizeof (page));
2348 if ((page[EI_MAG0] == ELFMAG0) &&
2349 (page[EI_MAG1] == ELFMAG1) &&
2350 (page[EI_MAG2] == ELFMAG2) &&
2351 (page[EI_MAG3] == ELFMAG3)) {
2353 * Mappings are possibly from ELF binary. Don't dump
2354 * them.
2356 return (0);
2360 return (vma->vma_end - vma->vma_start);
2363 static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
2364 unsigned long flags)
2366 struct mm_struct *mm = (struct mm_struct *)priv;
2368 vma_add_mapping(mm, start, end, flags);
2369 return (0);
2372 static void fill_note(struct memelfnote *note, const char *name, int type,
2373 unsigned int sz, void *data)
2375 unsigned int namesz;
2377 namesz = strlen(name) + 1;
2378 note->name = name;
2379 note->namesz = namesz;
2380 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2381 note->type = type;
2382 note->datasz = sz;
2383 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2385 note->data = data;
2388 * We calculate rounded up note size here as specified by
2389 * ELF document.
2391 note->notesz = sizeof (struct elf_note) +
2392 note->namesz_rounded + note->datasz_rounded;
2395 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2396 uint32_t flags)
2398 (void) memset(elf, 0, sizeof(*elf));
2400 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2401 elf->e_ident[EI_CLASS] = ELF_CLASS;
2402 elf->e_ident[EI_DATA] = ELF_DATA;
2403 elf->e_ident[EI_VERSION] = EV_CURRENT;
2404 elf->e_ident[EI_OSABI] = ELF_OSABI;
2406 elf->e_type = ET_CORE;
2407 elf->e_machine = machine;
2408 elf->e_version = EV_CURRENT;
2409 elf->e_phoff = sizeof(struct elfhdr);
2410 elf->e_flags = flags;
2411 elf->e_ehsize = sizeof(struct elfhdr);
2412 elf->e_phentsize = sizeof(struct elf_phdr);
2413 elf->e_phnum = segs;
2415 bswap_ehdr(elf);
2418 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2420 phdr->p_type = PT_NOTE;
2421 phdr->p_offset = offset;
2422 phdr->p_vaddr = 0;
2423 phdr->p_paddr = 0;
2424 phdr->p_filesz = sz;
2425 phdr->p_memsz = 0;
2426 phdr->p_flags = 0;
2427 phdr->p_align = 0;
2429 bswap_phdr(phdr, 1);
2432 static size_t note_size(const struct memelfnote *note)
2434 return (note->notesz);
2437 static void fill_prstatus(struct target_elf_prstatus *prstatus,
2438 const TaskState *ts, int signr)
2440 (void) memset(prstatus, 0, sizeof (*prstatus));
2441 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2442 prstatus->pr_pid = ts->ts_tid;
2443 prstatus->pr_ppid = getppid();
2444 prstatus->pr_pgrp = getpgrp();
2445 prstatus->pr_sid = getsid(0);
2447 bswap_prstatus(prstatus);
2450 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2452 char *base_filename;
2453 unsigned int i, len;
2455 (void) memset(psinfo, 0, sizeof (*psinfo));
2457 len = ts->info->arg_end - ts->info->arg_start;
2458 if (len >= ELF_PRARGSZ)
2459 len = ELF_PRARGSZ - 1;
2460 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2461 return -EFAULT;
2462 for (i = 0; i < len; i++)
2463 if (psinfo->pr_psargs[i] == 0)
2464 psinfo->pr_psargs[i] = ' ';
2465 psinfo->pr_psargs[len] = 0;
2467 psinfo->pr_pid = getpid();
2468 psinfo->pr_ppid = getppid();
2469 psinfo->pr_pgrp = getpgrp();
2470 psinfo->pr_sid = getsid(0);
2471 psinfo->pr_uid = getuid();
2472 psinfo->pr_gid = getgid();
2474 base_filename = g_path_get_basename(ts->bprm->filename);
2476 * Using strncpy here is fine: at max-length,
2477 * this field is not NUL-terminated.
2479 (void) strncpy(psinfo->pr_fname, base_filename,
2480 sizeof(psinfo->pr_fname));
2482 g_free(base_filename);
2483 bswap_psinfo(psinfo);
2484 return (0);
2487 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2489 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2490 elf_addr_t orig_auxv = auxv;
2491 void *ptr;
2492 int len = ts->info->auxv_len;
2495 * Auxiliary vector is stored in target process stack. It contains
2496 * {type, value} pairs that we need to dump into note. This is not
2497 * strictly necessary but we do it here for sake of completeness.
2500 /* read in whole auxv vector and copy it to memelfnote */
2501 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2502 if (ptr != NULL) {
2503 fill_note(note, "CORE", NT_AUXV, len, ptr);
2504 unlock_user(ptr, auxv, len);
2509 * Constructs name of coredump file. We have following convention
2510 * for the name:
2511 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2513 * Returns 0 in case of success, -1 otherwise (errno is set).
2515 static int core_dump_filename(const TaskState *ts, char *buf,
2516 size_t bufsize)
2518 char timestamp[64];
2519 char *filename = NULL;
2520 char *base_filename = NULL;
2521 struct timeval tv;
2522 struct tm tm;
2524 assert(bufsize >= PATH_MAX);
2526 if (gettimeofday(&tv, NULL) < 0) {
2527 (void) fprintf(stderr, "unable to get current timestamp: %s",
2528 strerror(errno));
2529 return (-1);
2532 filename = strdup(ts->bprm->filename);
2533 base_filename = strdup(basename(filename));
2534 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2535 localtime_r(&tv.tv_sec, &tm));
2536 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2537 base_filename, timestamp, (int)getpid());
2538 free(base_filename);
2539 free(filename);
2541 return (0);
2544 static int dump_write(int fd, const void *ptr, size_t size)
2546 const char *bufp = (const char *)ptr;
2547 ssize_t bytes_written, bytes_left;
2548 struct rlimit dumpsize;
2549 off_t pos;
2551 bytes_written = 0;
2552 getrlimit(RLIMIT_CORE, &dumpsize);
2553 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2554 if (errno == ESPIPE) { /* not a seekable stream */
2555 bytes_left = size;
2556 } else {
2557 return pos;
2559 } else {
2560 if (dumpsize.rlim_cur <= pos) {
2561 return -1;
2562 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2563 bytes_left = size;
2564 } else {
2565 size_t limit_left=dumpsize.rlim_cur - pos;
2566 bytes_left = limit_left >= size ? size : limit_left ;
2571 * In normal conditions, single write(2) should do but
2572 * in case of socket etc. this mechanism is more portable.
2574 do {
2575 bytes_written = write(fd, bufp, bytes_left);
2576 if (bytes_written < 0) {
2577 if (errno == EINTR)
2578 continue;
2579 return (-1);
2580 } else if (bytes_written == 0) { /* eof */
2581 return (-1);
2583 bufp += bytes_written;
2584 bytes_left -= bytes_written;
2585 } while (bytes_left > 0);
2587 return (0);
2590 static int write_note(struct memelfnote *men, int fd)
2592 struct elf_note en;
2594 en.n_namesz = men->namesz;
2595 en.n_type = men->type;
2596 en.n_descsz = men->datasz;
2598 bswap_note(&en);
2600 if (dump_write(fd, &en, sizeof(en)) != 0)
2601 return (-1);
2602 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2603 return (-1);
2604 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
2605 return (-1);
2607 return (0);
2610 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
2612 TaskState *ts = (TaskState *)env->opaque;
2613 struct elf_thread_status *ets;
2615 ets = g_malloc0(sizeof (*ets));
2616 ets->num_notes = 1; /* only prstatus is dumped */
2617 fill_prstatus(&ets->prstatus, ts, 0);
2618 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2619 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2620 &ets->prstatus);
2622 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
2624 info->notes_size += note_size(&ets->notes[0]);
2627 static int fill_note_info(struct elf_note_info *info,
2628 long signr, const CPUArchState *env)
2630 #define NUMNOTES 3
2631 CPUArchState *cpu = NULL;
2632 TaskState *ts = (TaskState *)env->opaque;
2633 int i;
2635 (void) memset(info, 0, sizeof (*info));
2637 QTAILQ_INIT(&info->thread_list);
2639 info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote));
2640 if (info->notes == NULL)
2641 return (-ENOMEM);
2642 info->prstatus = g_malloc0(sizeof (*info->prstatus));
2643 if (info->prstatus == NULL)
2644 return (-ENOMEM);
2645 info->psinfo = g_malloc0(sizeof (*info->psinfo));
2646 if (info->prstatus == NULL)
2647 return (-ENOMEM);
2650 * First fill in status (and registers) of current thread
2651 * including process info & aux vector.
2653 fill_prstatus(info->prstatus, ts, signr);
2654 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2655 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2656 sizeof (*info->prstatus), info->prstatus);
2657 fill_psinfo(info->psinfo, ts);
2658 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2659 sizeof (*info->psinfo), info->psinfo);
2660 fill_auxv_note(&info->notes[2], ts);
2661 info->numnote = 3;
2663 info->notes_size = 0;
2664 for (i = 0; i < info->numnote; i++)
2665 info->notes_size += note_size(&info->notes[i]);
2667 /* read and fill status of all threads */
2668 cpu_list_lock();
2669 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2670 if (cpu == thread_env)
2671 continue;
2672 fill_thread_info(info, cpu);
2674 cpu_list_unlock();
2676 return (0);
2679 static void free_note_info(struct elf_note_info *info)
2681 struct elf_thread_status *ets;
2683 while (!QTAILQ_EMPTY(&info->thread_list)) {
2684 ets = QTAILQ_FIRST(&info->thread_list);
2685 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
2686 g_free(ets);
2689 g_free(info->prstatus);
2690 g_free(info->psinfo);
2691 g_free(info->notes);
2694 static int write_note_info(struct elf_note_info *info, int fd)
2696 struct elf_thread_status *ets;
2697 int i, error = 0;
2699 /* write prstatus, psinfo and auxv for current thread */
2700 for (i = 0; i < info->numnote; i++)
2701 if ((error = write_note(&info->notes[i], fd)) != 0)
2702 return (error);
2704 /* write prstatus for each thread */
2705 for (ets = info->thread_list.tqh_first; ets != NULL;
2706 ets = ets->ets_link.tqe_next) {
2707 if ((error = write_note(&ets->notes[0], fd)) != 0)
2708 return (error);
2711 return (0);
2715 * Write out ELF coredump.
2717 * See documentation of ELF object file format in:
2718 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2720 * Coredump format in linux is following:
2722 * 0 +----------------------+ \
2723 * | ELF header | ET_CORE |
2724 * +----------------------+ |
2725 * | ELF program headers | |--- headers
2726 * | - NOTE section | |
2727 * | - PT_LOAD sections | |
2728 * +----------------------+ /
2729 * | NOTEs: |
2730 * | - NT_PRSTATUS |
2731 * | - NT_PRSINFO |
2732 * | - NT_AUXV |
2733 * +----------------------+ <-- aligned to target page
2734 * | Process memory dump |
2735 * : :
2736 * . .
2737 * : :
2738 * | |
2739 * +----------------------+
2741 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2742 * NT_PRSINFO -> struct elf_prpsinfo
2743 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2745 * Format follows System V format as close as possible. Current
2746 * version limitations are as follows:
2747 * - no floating point registers are dumped
2749 * Function returns 0 in case of success, negative errno otherwise.
2751 * TODO: make this work also during runtime: it should be
2752 * possible to force coredump from running process and then
2753 * continue processing. For example qemu could set up SIGUSR2
2754 * handler (provided that target process haven't registered
2755 * handler for that) that does the dump when signal is received.
2757 static int elf_core_dump(int signr, const CPUArchState *env)
2759 const TaskState *ts = (const TaskState *)env->opaque;
2760 struct vm_area_struct *vma = NULL;
2761 char corefile[PATH_MAX];
2762 struct elf_note_info info;
2763 struct elfhdr elf;
2764 struct elf_phdr phdr;
2765 struct rlimit dumpsize;
2766 struct mm_struct *mm = NULL;
2767 off_t offset = 0, data_offset = 0;
2768 int segs = 0;
2769 int fd = -1;
2771 errno = 0;
2772 getrlimit(RLIMIT_CORE, &dumpsize);
2773 if (dumpsize.rlim_cur == 0)
2774 return 0;
2776 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2777 return (-errno);
2779 if ((fd = open(corefile, O_WRONLY | O_CREAT,
2780 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2781 return (-errno);
2784 * Walk through target process memory mappings and
2785 * set up structure containing this information. After
2786 * this point vma_xxx functions can be used.
2788 if ((mm = vma_init()) == NULL)
2789 goto out;
2791 walk_memory_regions(mm, vma_walker);
2792 segs = vma_get_mapping_count(mm);
2795 * Construct valid coredump ELF header. We also
2796 * add one more segment for notes.
2798 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
2799 if (dump_write(fd, &elf, sizeof (elf)) != 0)
2800 goto out;
2802 /* fill in in-memory version of notes */
2803 if (fill_note_info(&info, signr, env) < 0)
2804 goto out;
2806 offset += sizeof (elf); /* elf header */
2807 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
2809 /* write out notes program header */
2810 fill_elf_note_phdr(&phdr, info.notes_size, offset);
2812 offset += info.notes_size;
2813 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
2814 goto out;
2817 * ELF specification wants data to start at page boundary so
2818 * we align it here.
2820 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
2823 * Write program headers for memory regions mapped in
2824 * the target process.
2826 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2827 (void) memset(&phdr, 0, sizeof (phdr));
2829 phdr.p_type = PT_LOAD;
2830 phdr.p_offset = offset;
2831 phdr.p_vaddr = vma->vma_start;
2832 phdr.p_paddr = 0;
2833 phdr.p_filesz = vma_dump_size(vma);
2834 offset += phdr.p_filesz;
2835 phdr.p_memsz = vma->vma_end - vma->vma_start;
2836 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
2837 if (vma->vma_flags & PROT_WRITE)
2838 phdr.p_flags |= PF_W;
2839 if (vma->vma_flags & PROT_EXEC)
2840 phdr.p_flags |= PF_X;
2841 phdr.p_align = ELF_EXEC_PAGESIZE;
2843 bswap_phdr(&phdr, 1);
2844 dump_write(fd, &phdr, sizeof (phdr));
2848 * Next we write notes just after program headers. No
2849 * alignment needed here.
2851 if (write_note_info(&info, fd) < 0)
2852 goto out;
2854 /* align data to page boundary */
2855 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
2856 goto out;
2859 * Finally we can dump process memory into corefile as well.
2861 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2862 abi_ulong addr;
2863 abi_ulong end;
2865 end = vma->vma_start + vma_dump_size(vma);
2867 for (addr = vma->vma_start; addr < end;
2868 addr += TARGET_PAGE_SIZE) {
2869 char page[TARGET_PAGE_SIZE];
2870 int error;
2873 * Read in page from target process memory and
2874 * write it to coredump file.
2876 error = copy_from_user(page, addr, sizeof (page));
2877 if (error != 0) {
2878 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
2879 addr);
2880 errno = -error;
2881 goto out;
2883 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
2884 goto out;
2888 out:
2889 free_note_info(&info);
2890 if (mm != NULL)
2891 vma_delete(mm);
2892 (void) close(fd);
2894 if (errno != 0)
2895 return (-errno);
2896 return (0);
2898 #endif /* USE_ELF_CORE_DUMP */
2900 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
2902 init_thread(regs, infop);