1 /* This is the Linux kernel elf-loading code, ported into user space */
11 #include <sys/resource.h>
17 #include "disas/disas.h"
29 #define ELF_OSABI ELFOSABI_SYSV
31 /* from personality.h */
34 * Flags for bug emulation.
36 * These occupy the top three bytes.
39 ADDR_NO_RANDOMIZE
= 0x0040000, /* disable randomization of VA space */
40 FDPIC_FUNCPTRS
= 0x0080000, /* userspace function ptrs point to
41 descriptors (signal handling) */
42 MMAP_PAGE_ZERO
= 0x0100000,
43 ADDR_COMPAT_LAYOUT
= 0x0200000,
44 READ_IMPLIES_EXEC
= 0x0400000,
45 ADDR_LIMIT_32BIT
= 0x0800000,
46 SHORT_INODE
= 0x1000000,
47 WHOLE_SECONDS
= 0x2000000,
48 STICKY_TIMEOUTS
= 0x4000000,
49 ADDR_LIMIT_3GB
= 0x8000000,
55 * These go in the low byte. Avoid using the top bit, it will
56 * conflict with error returns.
60 PER_LINUX_32BIT
= 0x0000 | ADDR_LIMIT_32BIT
,
61 PER_LINUX_FDPIC
= 0x0000 | FDPIC_FUNCPTRS
,
62 PER_SVR4
= 0x0001 | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
63 PER_SVR3
= 0x0002 | STICKY_TIMEOUTS
| SHORT_INODE
,
64 PER_SCOSVR3
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
| SHORT_INODE
,
65 PER_OSR5
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
,
66 PER_WYSEV386
= 0x0004 | STICKY_TIMEOUTS
| SHORT_INODE
,
67 PER_ISCR4
= 0x0005 | STICKY_TIMEOUTS
,
69 PER_SUNOS
= 0x0006 | STICKY_TIMEOUTS
,
70 PER_XENIX
= 0x0007 | STICKY_TIMEOUTS
| SHORT_INODE
,
72 PER_LINUX32_3GB
= 0x0008 | ADDR_LIMIT_3GB
,
73 PER_IRIX32
= 0x0009 | STICKY_TIMEOUTS
,/* IRIX5 32-bit */
74 PER_IRIXN32
= 0x000a | STICKY_TIMEOUTS
,/* IRIX6 new 32-bit */
75 PER_IRIX64
= 0x000b | STICKY_TIMEOUTS
,/* IRIX6 64-bit */
77 PER_SOLARIS
= 0x000d | STICKY_TIMEOUTS
,
78 PER_UW7
= 0x000e | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
79 PER_OSF4
= 0x000f, /* OSF/1 v4 */
85 * Return the base personality without flags.
87 #define personality(pers) (pers & PER_MASK)
89 /* this flag is uneffective under linux too, should be deleted */
91 #define MAP_DENYWRITE 0
94 /* should probably go in elf.h */
99 #ifdef TARGET_WORDS_BIGENDIAN
100 #define ELF_DATA ELFDATA2MSB
102 #define ELF_DATA ELFDATA2LSB
105 #ifdef TARGET_ABI_MIPSN32
106 typedef abi_ullong target_elf_greg_t
;
107 #define tswapreg(ptr) tswap64(ptr)
109 typedef abi_ulong target_elf_greg_t
;
110 #define tswapreg(ptr) tswapal(ptr)
114 typedef abi_ushort target_uid_t
;
115 typedef abi_ushort target_gid_t
;
117 typedef abi_uint target_uid_t
;
118 typedef abi_uint target_gid_t
;
120 typedef abi_int target_pid_t
;
124 #define ELF_PLATFORM get_elf_platform()
126 static const char *get_elf_platform(void)
128 static char elf_platform
[] = "i386";
129 int family
= object_property_get_int(OBJECT(thread_cpu
), "family", NULL
);
133 elf_platform
[1] = '0' + family
;
137 #define ELF_HWCAP get_elf_hwcap()
139 static uint32_t get_elf_hwcap(void)
141 X86CPU
*cpu
= X86_CPU(thread_cpu
);
143 return cpu
->env
.features
[FEAT_1_EDX
];
147 #define ELF_START_MMAP 0x2aaaaab000ULL
148 #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
150 #define ELF_CLASS ELFCLASS64
151 #define ELF_ARCH EM_X86_64
153 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
156 regs
->rsp
= infop
->start_stack
;
157 regs
->rip
= infop
->entry
;
161 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
164 * Note that ELF_NREG should be 29 as there should be place for
165 * TRAPNO and ERR "registers" as well but linux doesn't dump
168 * See linux kernel: arch/x86/include/asm/elf.h
170 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
172 (*regs
)[0] = env
->regs
[15];
173 (*regs
)[1] = env
->regs
[14];
174 (*regs
)[2] = env
->regs
[13];
175 (*regs
)[3] = env
->regs
[12];
176 (*regs
)[4] = env
->regs
[R_EBP
];
177 (*regs
)[5] = env
->regs
[R_EBX
];
178 (*regs
)[6] = env
->regs
[11];
179 (*regs
)[7] = env
->regs
[10];
180 (*regs
)[8] = env
->regs
[9];
181 (*regs
)[9] = env
->regs
[8];
182 (*regs
)[10] = env
->regs
[R_EAX
];
183 (*regs
)[11] = env
->regs
[R_ECX
];
184 (*regs
)[12] = env
->regs
[R_EDX
];
185 (*regs
)[13] = env
->regs
[R_ESI
];
186 (*regs
)[14] = env
->regs
[R_EDI
];
187 (*regs
)[15] = env
->regs
[R_EAX
]; /* XXX */
188 (*regs
)[16] = env
->eip
;
189 (*regs
)[17] = env
->segs
[R_CS
].selector
& 0xffff;
190 (*regs
)[18] = env
->eflags
;
191 (*regs
)[19] = env
->regs
[R_ESP
];
192 (*regs
)[20] = env
->segs
[R_SS
].selector
& 0xffff;
193 (*regs
)[21] = env
->segs
[R_FS
].selector
& 0xffff;
194 (*regs
)[22] = env
->segs
[R_GS
].selector
& 0xffff;
195 (*regs
)[23] = env
->segs
[R_DS
].selector
& 0xffff;
196 (*regs
)[24] = env
->segs
[R_ES
].selector
& 0xffff;
197 (*regs
)[25] = env
->segs
[R_FS
].selector
& 0xffff;
198 (*regs
)[26] = env
->segs
[R_GS
].selector
& 0xffff;
203 #define ELF_START_MMAP 0x80000000
206 * This is used to ensure we don't load something for the wrong architecture.
208 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
211 * These are used to set parameters in the core dumps.
213 #define ELF_CLASS ELFCLASS32
214 #define ELF_ARCH EM_386
216 static inline void init_thread(struct target_pt_regs
*regs
,
217 struct image_info
*infop
)
219 regs
->esp
= infop
->start_stack
;
220 regs
->eip
= infop
->entry
;
222 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
223 starts %edx contains a pointer to a function which might be
224 registered using `atexit'. This provides a mean for the
225 dynamic linker to call DT_FINI functions for shared libraries
226 that have been loaded before the code runs.
228 A value of 0 tells we have no such handler. */
233 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
236 * Note that ELF_NREG should be 19 as there should be place for
237 * TRAPNO and ERR "registers" as well but linux doesn't dump
240 * See linux kernel: arch/x86/include/asm/elf.h
242 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
244 (*regs
)[0] = env
->regs
[R_EBX
];
245 (*regs
)[1] = env
->regs
[R_ECX
];
246 (*regs
)[2] = env
->regs
[R_EDX
];
247 (*regs
)[3] = env
->regs
[R_ESI
];
248 (*regs
)[4] = env
->regs
[R_EDI
];
249 (*regs
)[5] = env
->regs
[R_EBP
];
250 (*regs
)[6] = env
->regs
[R_EAX
];
251 (*regs
)[7] = env
->segs
[R_DS
].selector
& 0xffff;
252 (*regs
)[8] = env
->segs
[R_ES
].selector
& 0xffff;
253 (*regs
)[9] = env
->segs
[R_FS
].selector
& 0xffff;
254 (*regs
)[10] = env
->segs
[R_GS
].selector
& 0xffff;
255 (*regs
)[11] = env
->regs
[R_EAX
]; /* XXX */
256 (*regs
)[12] = env
->eip
;
257 (*regs
)[13] = env
->segs
[R_CS
].selector
& 0xffff;
258 (*regs
)[14] = env
->eflags
;
259 (*regs
)[15] = env
->regs
[R_ESP
];
260 (*regs
)[16] = env
->segs
[R_SS
].selector
& 0xffff;
264 #define USE_ELF_CORE_DUMP
265 #define ELF_EXEC_PAGESIZE 4096
271 #ifndef TARGET_AARCH64
272 /* 32 bit ARM definitions */
274 #define ELF_START_MMAP 0x80000000
276 #define elf_check_arch(x) ((x) == ELF_MACHINE)
278 #define ELF_ARCH ELF_MACHINE
279 #define ELF_CLASS ELFCLASS32
281 static inline void init_thread(struct target_pt_regs
*regs
,
282 struct image_info
*infop
)
284 abi_long stack
= infop
->start_stack
;
285 memset(regs
, 0, sizeof(*regs
));
287 regs
->ARM_cpsr
= 0x10;
288 if (infop
->entry
& 1)
289 regs
->ARM_cpsr
|= CPSR_T
;
290 regs
->ARM_pc
= infop
->entry
& 0xfffffffe;
291 regs
->ARM_sp
= infop
->start_stack
;
292 /* FIXME - what to for failure of get_user()? */
293 get_user_ual(regs
->ARM_r2
, stack
+ 8); /* envp */
294 get_user_ual(regs
->ARM_r1
, stack
+ 4); /* envp */
295 /* XXX: it seems that r0 is zeroed after ! */
297 /* For uClinux PIC binaries. */
298 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
299 regs
->ARM_r10
= infop
->start_data
;
303 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
305 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUARMState
*env
)
307 (*regs
)[0] = tswapreg(env
->regs
[0]);
308 (*regs
)[1] = tswapreg(env
->regs
[1]);
309 (*regs
)[2] = tswapreg(env
->regs
[2]);
310 (*regs
)[3] = tswapreg(env
->regs
[3]);
311 (*regs
)[4] = tswapreg(env
->regs
[4]);
312 (*regs
)[5] = tswapreg(env
->regs
[5]);
313 (*regs
)[6] = tswapreg(env
->regs
[6]);
314 (*regs
)[7] = tswapreg(env
->regs
[7]);
315 (*regs
)[8] = tswapreg(env
->regs
[8]);
316 (*regs
)[9] = tswapreg(env
->regs
[9]);
317 (*regs
)[10] = tswapreg(env
->regs
[10]);
318 (*regs
)[11] = tswapreg(env
->regs
[11]);
319 (*regs
)[12] = tswapreg(env
->regs
[12]);
320 (*regs
)[13] = tswapreg(env
->regs
[13]);
321 (*regs
)[14] = tswapreg(env
->regs
[14]);
322 (*regs
)[15] = tswapreg(env
->regs
[15]);
324 (*regs
)[16] = tswapreg(cpsr_read((CPUARMState
*)env
));
325 (*regs
)[17] = tswapreg(env
->regs
[0]); /* XXX */
328 #define USE_ELF_CORE_DUMP
329 #define ELF_EXEC_PAGESIZE 4096
333 ARM_HWCAP_ARM_SWP
= 1 << 0,
334 ARM_HWCAP_ARM_HALF
= 1 << 1,
335 ARM_HWCAP_ARM_THUMB
= 1 << 2,
336 ARM_HWCAP_ARM_26BIT
= 1 << 3,
337 ARM_HWCAP_ARM_FAST_MULT
= 1 << 4,
338 ARM_HWCAP_ARM_FPA
= 1 << 5,
339 ARM_HWCAP_ARM_VFP
= 1 << 6,
340 ARM_HWCAP_ARM_EDSP
= 1 << 7,
341 ARM_HWCAP_ARM_JAVA
= 1 << 8,
342 ARM_HWCAP_ARM_IWMMXT
= 1 << 9,
343 ARM_HWCAP_ARM_CRUNCH
= 1 << 10,
344 ARM_HWCAP_ARM_THUMBEE
= 1 << 11,
345 ARM_HWCAP_ARM_NEON
= 1 << 12,
346 ARM_HWCAP_ARM_VFPv3
= 1 << 13,
347 ARM_HWCAP_ARM_VFPv3D16
= 1 << 14,
348 ARM_HWCAP_ARM_TLS
= 1 << 15,
349 ARM_HWCAP_ARM_VFPv4
= 1 << 16,
350 ARM_HWCAP_ARM_IDIVA
= 1 << 17,
351 ARM_HWCAP_ARM_IDIVT
= 1 << 18,
352 ARM_HWCAP_ARM_VFPD32
= 1 << 19,
353 ARM_HWCAP_ARM_LPAE
= 1 << 20,
354 ARM_HWCAP_ARM_EVTSTRM
= 1 << 21,
358 ARM_HWCAP2_ARM_AES
= 1 << 0,
359 ARM_HWCAP2_ARM_PMULL
= 1 << 1,
360 ARM_HWCAP2_ARM_SHA1
= 1 << 2,
361 ARM_HWCAP2_ARM_SHA2
= 1 << 3,
362 ARM_HWCAP2_ARM_CRC32
= 1 << 4,
365 /* The commpage only exists for 32 bit kernels */
367 #define TARGET_HAS_VALIDATE_GUEST_SPACE
368 /* Return 1 if the proposed guest space is suitable for the guest.
369 * Return 0 if the proposed guest space isn't suitable, but another
370 * address space should be tried.
371 * Return -1 if there is no way the proposed guest space can be
372 * valid regardless of the base.
373 * The guest code may leave a page mapped and populate it if the
374 * address is suitable.
376 static int validate_guest_space(unsigned long guest_base
,
377 unsigned long guest_size
)
379 unsigned long real_start
, test_page_addr
;
381 /* We need to check that we can force a fault on access to the
382 * commpage at 0xffff0fxx
384 test_page_addr
= guest_base
+ (0xffff0f00 & qemu_host_page_mask
);
386 /* If the commpage lies within the already allocated guest space,
387 * then there is no way we can allocate it.
389 if (test_page_addr
>= guest_base
390 && test_page_addr
<= (guest_base
+ guest_size
)) {
394 /* Note it needs to be writeable to let us initialise it */
395 real_start
= (unsigned long)
396 mmap((void *)test_page_addr
, qemu_host_page_size
,
397 PROT_READ
| PROT_WRITE
,
398 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
400 /* If we can't map it then try another address */
401 if (real_start
== -1ul) {
405 if (real_start
!= test_page_addr
) {
406 /* OS didn't put the page where we asked - unmap and reject */
407 munmap((void *)real_start
, qemu_host_page_size
);
411 /* Leave the page mapped
412 * Populate it (mmap should have left it all 0'd)
415 /* Kernel helper versions */
416 __put_user(5, (uint32_t *)g2h(0xffff0ffcul
));
418 /* Now it's populated make it RO */
419 if (mprotect((void *)test_page_addr
, qemu_host_page_size
, PROT_READ
)) {
420 perror("Protecting guest commpage");
424 return 1; /* All good */
427 #define ELF_HWCAP get_elf_hwcap()
428 #define ELF_HWCAP2 get_elf_hwcap2()
430 static uint32_t get_elf_hwcap(void)
432 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
435 hwcaps
|= ARM_HWCAP_ARM_SWP
;
436 hwcaps
|= ARM_HWCAP_ARM_HALF
;
437 hwcaps
|= ARM_HWCAP_ARM_THUMB
;
438 hwcaps
|= ARM_HWCAP_ARM_FAST_MULT
;
440 /* probe for the extra features */
441 #define GET_FEATURE(feat, hwcap) \
442 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
443 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
444 GET_FEATURE(ARM_FEATURE_V5
, ARM_HWCAP_ARM_EDSP
);
445 GET_FEATURE(ARM_FEATURE_VFP
, ARM_HWCAP_ARM_VFP
);
446 GET_FEATURE(ARM_FEATURE_IWMMXT
, ARM_HWCAP_ARM_IWMMXT
);
447 GET_FEATURE(ARM_FEATURE_THUMB2EE
, ARM_HWCAP_ARM_THUMBEE
);
448 GET_FEATURE(ARM_FEATURE_NEON
, ARM_HWCAP_ARM_NEON
);
449 GET_FEATURE(ARM_FEATURE_VFP3
, ARM_HWCAP_ARM_VFPv3
);
450 GET_FEATURE(ARM_FEATURE_V6K
, ARM_HWCAP_ARM_TLS
);
451 GET_FEATURE(ARM_FEATURE_VFP4
, ARM_HWCAP_ARM_VFPv4
);
452 GET_FEATURE(ARM_FEATURE_ARM_DIV
, ARM_HWCAP_ARM_IDIVA
);
453 GET_FEATURE(ARM_FEATURE_THUMB_DIV
, ARM_HWCAP_ARM_IDIVT
);
454 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
455 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
456 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
457 * to our VFP_FP16 feature bit.
459 GET_FEATURE(ARM_FEATURE_VFP3
, ARM_HWCAP_ARM_VFPD32
);
460 GET_FEATURE(ARM_FEATURE_LPAE
, ARM_HWCAP_ARM_LPAE
);
465 static uint32_t get_elf_hwcap2(void)
467 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
470 GET_FEATURE(ARM_FEATURE_V8_AES
, ARM_HWCAP2_ARM_AES
);
471 GET_FEATURE(ARM_FEATURE_V8_SHA1
, ARM_HWCAP2_ARM_SHA1
);
472 GET_FEATURE(ARM_FEATURE_V8_SHA256
, ARM_HWCAP2_ARM_SHA2
);
473 GET_FEATURE(ARM_FEATURE_CRC
, ARM_HWCAP2_ARM_CRC32
);
480 /* 64 bit ARM definitions */
481 #define ELF_START_MMAP 0x80000000
483 #define elf_check_arch(x) ((x) == ELF_MACHINE)
485 #define ELF_ARCH ELF_MACHINE
486 #define ELF_CLASS ELFCLASS64
487 #define ELF_PLATFORM "aarch64"
489 static inline void init_thread(struct target_pt_regs
*regs
,
490 struct image_info
*infop
)
492 abi_long stack
= infop
->start_stack
;
493 memset(regs
, 0, sizeof(*regs
));
495 regs
->pc
= infop
->entry
& ~0x3ULL
;
500 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
502 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
503 const CPUARMState
*env
)
507 for (i
= 0; i
< 32; i
++) {
508 (*regs
)[i
] = tswapreg(env
->xregs
[i
]);
510 (*regs
)[32] = tswapreg(env
->pc
);
511 (*regs
)[33] = tswapreg(pstate_read((CPUARMState
*)env
));
514 #define USE_ELF_CORE_DUMP
515 #define ELF_EXEC_PAGESIZE 4096
518 ARM_HWCAP_A64_FP
= 1 << 0,
519 ARM_HWCAP_A64_ASIMD
= 1 << 1,
520 ARM_HWCAP_A64_EVTSTRM
= 1 << 2,
521 ARM_HWCAP_A64_AES
= 1 << 3,
522 ARM_HWCAP_A64_PMULL
= 1 << 4,
523 ARM_HWCAP_A64_SHA1
= 1 << 5,
524 ARM_HWCAP_A64_SHA2
= 1 << 6,
525 ARM_HWCAP_A64_CRC32
= 1 << 7,
528 #define ELF_HWCAP get_elf_hwcap()
530 static uint32_t get_elf_hwcap(void)
532 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
535 hwcaps
|= ARM_HWCAP_A64_FP
;
536 hwcaps
|= ARM_HWCAP_A64_ASIMD
;
538 /* probe for the extra features */
539 #define GET_FEATURE(feat, hwcap) \
540 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
541 GET_FEATURE(ARM_FEATURE_V8_AES
, ARM_HWCAP_A64_PMULL
);
547 #endif /* not TARGET_AARCH64 */
548 #endif /* TARGET_ARM */
550 #ifdef TARGET_UNICORE32
552 #define ELF_START_MMAP 0x80000000
554 #define elf_check_arch(x) ((x) == EM_UNICORE32)
556 #define ELF_CLASS ELFCLASS32
557 #define ELF_DATA ELFDATA2LSB
558 #define ELF_ARCH EM_UNICORE32
560 static inline void init_thread(struct target_pt_regs
*regs
,
561 struct image_info
*infop
)
563 abi_long stack
= infop
->start_stack
;
564 memset(regs
, 0, sizeof(*regs
));
565 regs
->UC32_REG_asr
= 0x10;
566 regs
->UC32_REG_pc
= infop
->entry
& 0xfffffffe;
567 regs
->UC32_REG_sp
= infop
->start_stack
;
568 /* FIXME - what to for failure of get_user()? */
569 get_user_ual(regs
->UC32_REG_02
, stack
+ 8); /* envp */
570 get_user_ual(regs
->UC32_REG_01
, stack
+ 4); /* envp */
571 /* XXX: it seems that r0 is zeroed after ! */
572 regs
->UC32_REG_00
= 0;
576 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
578 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUUniCore32State
*env
)
580 (*regs
)[0] = env
->regs
[0];
581 (*regs
)[1] = env
->regs
[1];
582 (*regs
)[2] = env
->regs
[2];
583 (*regs
)[3] = env
->regs
[3];
584 (*regs
)[4] = env
->regs
[4];
585 (*regs
)[5] = env
->regs
[5];
586 (*regs
)[6] = env
->regs
[6];
587 (*regs
)[7] = env
->regs
[7];
588 (*regs
)[8] = env
->regs
[8];
589 (*regs
)[9] = env
->regs
[9];
590 (*regs
)[10] = env
->regs
[10];
591 (*regs
)[11] = env
->regs
[11];
592 (*regs
)[12] = env
->regs
[12];
593 (*regs
)[13] = env
->regs
[13];
594 (*regs
)[14] = env
->regs
[14];
595 (*regs
)[15] = env
->regs
[15];
596 (*regs
)[16] = env
->regs
[16];
597 (*regs
)[17] = env
->regs
[17];
598 (*regs
)[18] = env
->regs
[18];
599 (*regs
)[19] = env
->regs
[19];
600 (*regs
)[20] = env
->regs
[20];
601 (*regs
)[21] = env
->regs
[21];
602 (*regs
)[22] = env
->regs
[22];
603 (*regs
)[23] = env
->regs
[23];
604 (*regs
)[24] = env
->regs
[24];
605 (*regs
)[25] = env
->regs
[25];
606 (*regs
)[26] = env
->regs
[26];
607 (*regs
)[27] = env
->regs
[27];
608 (*regs
)[28] = env
->regs
[28];
609 (*regs
)[29] = env
->regs
[29];
610 (*regs
)[30] = env
->regs
[30];
611 (*regs
)[31] = env
->regs
[31];
613 (*regs
)[32] = cpu_asr_read((CPUUniCore32State
*)env
);
614 (*regs
)[33] = env
->regs
[0]; /* XXX */
617 #define USE_ELF_CORE_DUMP
618 #define ELF_EXEC_PAGESIZE 4096
620 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
625 #ifdef TARGET_SPARC64
627 #define ELF_START_MMAP 0x80000000
628 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
629 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
631 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
633 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
636 #define ELF_CLASS ELFCLASS64
637 #define ELF_ARCH EM_SPARCV9
639 #define STACK_BIAS 2047
641 static inline void init_thread(struct target_pt_regs
*regs
,
642 struct image_info
*infop
)
647 regs
->pc
= infop
->entry
;
648 regs
->npc
= regs
->pc
+ 4;
651 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
653 if (personality(infop
->personality
) == PER_LINUX32
)
654 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
656 regs
->u_regs
[14] = infop
->start_stack
- 16 * 8 - STACK_BIAS
;
661 #define ELF_START_MMAP 0x80000000
662 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
663 | HWCAP_SPARC_MULDIV)
664 #define elf_check_arch(x) ( (x) == EM_SPARC )
666 #define ELF_CLASS ELFCLASS32
667 #define ELF_ARCH EM_SPARC
669 static inline void init_thread(struct target_pt_regs
*regs
,
670 struct image_info
*infop
)
673 regs
->pc
= infop
->entry
;
674 regs
->npc
= regs
->pc
+ 4;
676 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
684 #define ELF_START_MMAP 0x80000000
686 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
688 #define elf_check_arch(x) ( (x) == EM_PPC64 )
690 #define ELF_CLASS ELFCLASS64
694 #define elf_check_arch(x) ( (x) == EM_PPC )
696 #define ELF_CLASS ELFCLASS32
700 #define ELF_ARCH EM_PPC
702 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
703 See arch/powerpc/include/asm/cputable.h. */
705 QEMU_PPC_FEATURE_32
= 0x80000000,
706 QEMU_PPC_FEATURE_64
= 0x40000000,
707 QEMU_PPC_FEATURE_601_INSTR
= 0x20000000,
708 QEMU_PPC_FEATURE_HAS_ALTIVEC
= 0x10000000,
709 QEMU_PPC_FEATURE_HAS_FPU
= 0x08000000,
710 QEMU_PPC_FEATURE_HAS_MMU
= 0x04000000,
711 QEMU_PPC_FEATURE_HAS_4xxMAC
= 0x02000000,
712 QEMU_PPC_FEATURE_UNIFIED_CACHE
= 0x01000000,
713 QEMU_PPC_FEATURE_HAS_SPE
= 0x00800000,
714 QEMU_PPC_FEATURE_HAS_EFP_SINGLE
= 0x00400000,
715 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
= 0x00200000,
716 QEMU_PPC_FEATURE_NO_TB
= 0x00100000,
717 QEMU_PPC_FEATURE_POWER4
= 0x00080000,
718 QEMU_PPC_FEATURE_POWER5
= 0x00040000,
719 QEMU_PPC_FEATURE_POWER5_PLUS
= 0x00020000,
720 QEMU_PPC_FEATURE_CELL
= 0x00010000,
721 QEMU_PPC_FEATURE_BOOKE
= 0x00008000,
722 QEMU_PPC_FEATURE_SMT
= 0x00004000,
723 QEMU_PPC_FEATURE_ICACHE_SNOOP
= 0x00002000,
724 QEMU_PPC_FEATURE_ARCH_2_05
= 0x00001000,
725 QEMU_PPC_FEATURE_PA6T
= 0x00000800,
726 QEMU_PPC_FEATURE_HAS_DFP
= 0x00000400,
727 QEMU_PPC_FEATURE_POWER6_EXT
= 0x00000200,
728 QEMU_PPC_FEATURE_ARCH_2_06
= 0x00000100,
729 QEMU_PPC_FEATURE_HAS_VSX
= 0x00000080,
730 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT
= 0x00000040,
732 QEMU_PPC_FEATURE_TRUE_LE
= 0x00000002,
733 QEMU_PPC_FEATURE_PPC_LE
= 0x00000001,
736 #define ELF_HWCAP get_elf_hwcap()
738 static uint32_t get_elf_hwcap(void)
740 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
741 uint32_t features
= 0;
743 /* We don't have to be terribly complete here; the high points are
744 Altivec/FP/SPE support. Anything else is just a bonus. */
745 #define GET_FEATURE(flag, feature) \
746 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
747 GET_FEATURE(PPC_64B
, QEMU_PPC_FEATURE_64
);
748 GET_FEATURE(PPC_FLOAT
, QEMU_PPC_FEATURE_HAS_FPU
);
749 GET_FEATURE(PPC_ALTIVEC
, QEMU_PPC_FEATURE_HAS_ALTIVEC
);
750 GET_FEATURE(PPC_SPE
, QEMU_PPC_FEATURE_HAS_SPE
);
751 GET_FEATURE(PPC_SPE_SINGLE
, QEMU_PPC_FEATURE_HAS_EFP_SINGLE
);
752 GET_FEATURE(PPC_SPE_DOUBLE
, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
);
753 GET_FEATURE(PPC_BOOKE
, QEMU_PPC_FEATURE_BOOKE
);
754 GET_FEATURE(PPC_405_MAC
, QEMU_PPC_FEATURE_HAS_4xxMAC
);
761 * The requirements here are:
762 * - keep the final alignment of sp (sp & 0xf)
763 * - make sure the 32-bit value at the first 16 byte aligned position of
764 * AUXV is greater than 16 for glibc compatibility.
765 * AT_IGNOREPPC is used for that.
766 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
767 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
769 #define DLINFO_ARCH_ITEMS 5
770 #define ARCH_DLINFO \
772 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
773 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
774 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
776 * Now handle glibc compatibility. \
778 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
779 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
782 static inline void init_thread(struct target_pt_regs
*_regs
, struct image_info
*infop
)
784 _regs
->gpr
[1] = infop
->start_stack
;
785 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
786 _regs
->gpr
[2] = ldq_raw(infop
->entry
+ 8) + infop
->load_bias
;
787 infop
->entry
= ldq_raw(infop
->entry
) + infop
->load_bias
;
789 _regs
->nip
= infop
->entry
;
792 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
794 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
796 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUPPCState
*env
)
799 target_ulong ccr
= 0;
801 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
802 (*regs
)[i
] = tswapreg(env
->gpr
[i
]);
805 (*regs
)[32] = tswapreg(env
->nip
);
806 (*regs
)[33] = tswapreg(env
->msr
);
807 (*regs
)[35] = tswapreg(env
->ctr
);
808 (*regs
)[36] = tswapreg(env
->lr
);
809 (*regs
)[37] = tswapreg(env
->xer
);
811 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
812 ccr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
814 (*regs
)[38] = tswapreg(ccr
);
817 #define USE_ELF_CORE_DUMP
818 #define ELF_EXEC_PAGESIZE 4096
824 #define ELF_START_MMAP 0x80000000
826 #define elf_check_arch(x) ( (x) == EM_MIPS )
829 #define ELF_CLASS ELFCLASS64
831 #define ELF_CLASS ELFCLASS32
833 #define ELF_ARCH EM_MIPS
835 static inline void init_thread(struct target_pt_regs
*regs
,
836 struct image_info
*infop
)
838 regs
->cp0_status
= 2 << CP0St_KSU
;
839 regs
->cp0_epc
= infop
->entry
;
840 regs
->regs
[29] = infop
->start_stack
;
843 /* See linux kernel: arch/mips/include/asm/elf.h. */
845 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
847 /* See linux kernel: arch/mips/include/asm/reg.h. */
854 TARGET_EF_R26
= TARGET_EF_R0
+ 26,
855 TARGET_EF_R27
= TARGET_EF_R0
+ 27,
856 TARGET_EF_LO
= TARGET_EF_R0
+ 32,
857 TARGET_EF_HI
= TARGET_EF_R0
+ 33,
858 TARGET_EF_CP0_EPC
= TARGET_EF_R0
+ 34,
859 TARGET_EF_CP0_BADVADDR
= TARGET_EF_R0
+ 35,
860 TARGET_EF_CP0_STATUS
= TARGET_EF_R0
+ 36,
861 TARGET_EF_CP0_CAUSE
= TARGET_EF_R0
+ 37
864 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
865 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMIPSState
*env
)
869 for (i
= 0; i
< TARGET_EF_R0
; i
++) {
872 (*regs
)[TARGET_EF_R0
] = 0;
874 for (i
= 1; i
< ARRAY_SIZE(env
->active_tc
.gpr
); i
++) {
875 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->active_tc
.gpr
[i
]);
878 (*regs
)[TARGET_EF_R26
] = 0;
879 (*regs
)[TARGET_EF_R27
] = 0;
880 (*regs
)[TARGET_EF_LO
] = tswapreg(env
->active_tc
.LO
[0]);
881 (*regs
)[TARGET_EF_HI
] = tswapreg(env
->active_tc
.HI
[0]);
882 (*regs
)[TARGET_EF_CP0_EPC
] = tswapreg(env
->active_tc
.PC
);
883 (*regs
)[TARGET_EF_CP0_BADVADDR
] = tswapreg(env
->CP0_BadVAddr
);
884 (*regs
)[TARGET_EF_CP0_STATUS
] = tswapreg(env
->CP0_Status
);
885 (*regs
)[TARGET_EF_CP0_CAUSE
] = tswapreg(env
->CP0_Cause
);
888 #define USE_ELF_CORE_DUMP
889 #define ELF_EXEC_PAGESIZE 4096
891 #endif /* TARGET_MIPS */
893 #ifdef TARGET_MICROBLAZE
895 #define ELF_START_MMAP 0x80000000
897 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
899 #define ELF_CLASS ELFCLASS32
900 #define ELF_ARCH EM_MICROBLAZE
902 static inline void init_thread(struct target_pt_regs
*regs
,
903 struct image_info
*infop
)
905 regs
->pc
= infop
->entry
;
906 regs
->r1
= infop
->start_stack
;
910 #define ELF_EXEC_PAGESIZE 4096
912 #define USE_ELF_CORE_DUMP
914 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
916 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
917 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMBState
*env
)
921 for (i
= 0; i
< 32; i
++) {
922 (*regs
)[pos
++] = tswapreg(env
->regs
[i
]);
925 for (i
= 0; i
< 6; i
++) {
926 (*regs
)[pos
++] = tswapreg(env
->sregs
[i
]);
930 #endif /* TARGET_MICROBLAZE */
932 #ifdef TARGET_OPENRISC
934 #define ELF_START_MMAP 0x08000000
936 #define elf_check_arch(x) ((x) == EM_OPENRISC)
938 #define ELF_ARCH EM_OPENRISC
939 #define ELF_CLASS ELFCLASS32
940 #define ELF_DATA ELFDATA2MSB
942 static inline void init_thread(struct target_pt_regs
*regs
,
943 struct image_info
*infop
)
945 regs
->pc
= infop
->entry
;
946 regs
->gpr
[1] = infop
->start_stack
;
949 #define USE_ELF_CORE_DUMP
950 #define ELF_EXEC_PAGESIZE 8192
952 /* See linux kernel arch/openrisc/include/asm/elf.h. */
953 #define ELF_NREG 34 /* gprs and pc, sr */
954 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
956 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
957 const CPUOpenRISCState
*env
)
961 for (i
= 0; i
< 32; i
++) {
962 (*regs
)[i
] = tswapreg(env
->gpr
[i
]);
965 (*regs
)[32] = tswapreg(env
->pc
);
966 (*regs
)[33] = tswapreg(env
->sr
);
969 #define ELF_PLATFORM NULL
971 #endif /* TARGET_OPENRISC */
975 #define ELF_START_MMAP 0x80000000
977 #define elf_check_arch(x) ( (x) == EM_SH )
979 #define ELF_CLASS ELFCLASS32
980 #define ELF_ARCH EM_SH
982 static inline void init_thread(struct target_pt_regs
*regs
,
983 struct image_info
*infop
)
985 /* Check other registers XXXXX */
986 regs
->pc
= infop
->entry
;
987 regs
->regs
[15] = infop
->start_stack
;
990 /* See linux kernel: arch/sh/include/asm/elf.h. */
992 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
994 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1000 TARGET_REG_MACH
= 20,
1001 TARGET_REG_MACL
= 21,
1002 TARGET_REG_SYSCALL
= 22
1005 static inline void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1006 const CPUSH4State
*env
)
1010 for (i
= 0; i
< 16; i
++) {
1011 (*regs
[i
]) = tswapreg(env
->gregs
[i
]);
1014 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1015 (*regs
)[TARGET_REG_PR
] = tswapreg(env
->pr
);
1016 (*regs
)[TARGET_REG_SR
] = tswapreg(env
->sr
);
1017 (*regs
)[TARGET_REG_GBR
] = tswapreg(env
->gbr
);
1018 (*regs
)[TARGET_REG_MACH
] = tswapreg(env
->mach
);
1019 (*regs
)[TARGET_REG_MACL
] = tswapreg(env
->macl
);
1020 (*regs
)[TARGET_REG_SYSCALL
] = 0; /* FIXME */
1023 #define USE_ELF_CORE_DUMP
1024 #define ELF_EXEC_PAGESIZE 4096
1030 #define ELF_START_MMAP 0x80000000
1032 #define elf_check_arch(x) ( (x) == EM_CRIS )
1034 #define ELF_CLASS ELFCLASS32
1035 #define ELF_ARCH EM_CRIS
1037 static inline void init_thread(struct target_pt_regs
*regs
,
1038 struct image_info
*infop
)
1040 regs
->erp
= infop
->entry
;
1043 #define ELF_EXEC_PAGESIZE 8192
1049 #define ELF_START_MMAP 0x80000000
1051 #define elf_check_arch(x) ( (x) == EM_68K )
1053 #define ELF_CLASS ELFCLASS32
1054 #define ELF_ARCH EM_68K
1056 /* ??? Does this need to do anything?
1057 #define ELF_PLAT_INIT(_r) */
1059 static inline void init_thread(struct target_pt_regs
*regs
,
1060 struct image_info
*infop
)
1062 regs
->usp
= infop
->start_stack
;
1064 regs
->pc
= infop
->entry
;
1067 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1069 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1071 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUM68KState
*env
)
1073 (*regs
)[0] = tswapreg(env
->dregs
[1]);
1074 (*regs
)[1] = tswapreg(env
->dregs
[2]);
1075 (*regs
)[2] = tswapreg(env
->dregs
[3]);
1076 (*regs
)[3] = tswapreg(env
->dregs
[4]);
1077 (*regs
)[4] = tswapreg(env
->dregs
[5]);
1078 (*regs
)[5] = tswapreg(env
->dregs
[6]);
1079 (*regs
)[6] = tswapreg(env
->dregs
[7]);
1080 (*regs
)[7] = tswapreg(env
->aregs
[0]);
1081 (*regs
)[8] = tswapreg(env
->aregs
[1]);
1082 (*regs
)[9] = tswapreg(env
->aregs
[2]);
1083 (*regs
)[10] = tswapreg(env
->aregs
[3]);
1084 (*regs
)[11] = tswapreg(env
->aregs
[4]);
1085 (*regs
)[12] = tswapreg(env
->aregs
[5]);
1086 (*regs
)[13] = tswapreg(env
->aregs
[6]);
1087 (*regs
)[14] = tswapreg(env
->dregs
[0]);
1088 (*regs
)[15] = tswapreg(env
->aregs
[7]);
1089 (*regs
)[16] = tswapreg(env
->dregs
[0]); /* FIXME: orig_d0 */
1090 (*regs
)[17] = tswapreg(env
->sr
);
1091 (*regs
)[18] = tswapreg(env
->pc
);
1092 (*regs
)[19] = 0; /* FIXME: regs->format | regs->vector */
1095 #define USE_ELF_CORE_DUMP
1096 #define ELF_EXEC_PAGESIZE 8192
1102 #define ELF_START_MMAP (0x30000000000ULL)
1104 #define elf_check_arch(x) ( (x) == ELF_ARCH )
1106 #define ELF_CLASS ELFCLASS64
1107 #define ELF_ARCH EM_ALPHA
1109 static inline void init_thread(struct target_pt_regs
*regs
,
1110 struct image_info
*infop
)
1112 regs
->pc
= infop
->entry
;
1114 regs
->usp
= infop
->start_stack
;
1117 #define ELF_EXEC_PAGESIZE 8192
1119 #endif /* TARGET_ALPHA */
1123 #define ELF_START_MMAP (0x20000000000ULL)
1125 #define elf_check_arch(x) ( (x) == ELF_ARCH )
1127 #define ELF_CLASS ELFCLASS64
1128 #define ELF_DATA ELFDATA2MSB
1129 #define ELF_ARCH EM_S390
1131 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1133 regs
->psw
.addr
= infop
->entry
;
1134 regs
->psw
.mask
= PSW_MASK_64
| PSW_MASK_32
;
1135 regs
->gprs
[15] = infop
->start_stack
;
1138 #endif /* TARGET_S390X */
1140 #ifndef ELF_PLATFORM
1141 #define ELF_PLATFORM (NULL)
1150 #define ELF_CLASS ELFCLASS32
1152 #define bswaptls(ptr) bswap32s(ptr)
1159 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
1160 unsigned int a_text
; /* length of text, in bytes */
1161 unsigned int a_data
; /* length of data, in bytes */
1162 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
1163 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
1164 unsigned int a_entry
; /* start address */
1165 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
1166 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
1170 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1176 /* Necessary parameters */
1177 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1178 #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
1179 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1181 #define DLINFO_ITEMS 14
1183 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
1185 memcpy(to
, from
, n
);
1189 static void bswap_ehdr(struct elfhdr
*ehdr
)
1191 bswap16s(&ehdr
->e_type
); /* Object file type */
1192 bswap16s(&ehdr
->e_machine
); /* Architecture */
1193 bswap32s(&ehdr
->e_version
); /* Object file version */
1194 bswaptls(&ehdr
->e_entry
); /* Entry point virtual address */
1195 bswaptls(&ehdr
->e_phoff
); /* Program header table file offset */
1196 bswaptls(&ehdr
->e_shoff
); /* Section header table file offset */
1197 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
1198 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
1199 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
1200 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
1201 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
1202 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
1203 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
1206 static void bswap_phdr(struct elf_phdr
*phdr
, int phnum
)
1209 for (i
= 0; i
< phnum
; ++i
, ++phdr
) {
1210 bswap32s(&phdr
->p_type
); /* Segment type */
1211 bswap32s(&phdr
->p_flags
); /* Segment flags */
1212 bswaptls(&phdr
->p_offset
); /* Segment file offset */
1213 bswaptls(&phdr
->p_vaddr
); /* Segment virtual address */
1214 bswaptls(&phdr
->p_paddr
); /* Segment physical address */
1215 bswaptls(&phdr
->p_filesz
); /* Segment size in file */
1216 bswaptls(&phdr
->p_memsz
); /* Segment size in memory */
1217 bswaptls(&phdr
->p_align
); /* Segment alignment */
1221 static void bswap_shdr(struct elf_shdr
*shdr
, int shnum
)
1224 for (i
= 0; i
< shnum
; ++i
, ++shdr
) {
1225 bswap32s(&shdr
->sh_name
);
1226 bswap32s(&shdr
->sh_type
);
1227 bswaptls(&shdr
->sh_flags
);
1228 bswaptls(&shdr
->sh_addr
);
1229 bswaptls(&shdr
->sh_offset
);
1230 bswaptls(&shdr
->sh_size
);
1231 bswap32s(&shdr
->sh_link
);
1232 bswap32s(&shdr
->sh_info
);
1233 bswaptls(&shdr
->sh_addralign
);
1234 bswaptls(&shdr
->sh_entsize
);
1238 static void bswap_sym(struct elf_sym
*sym
)
1240 bswap32s(&sym
->st_name
);
1241 bswaptls(&sym
->st_value
);
1242 bswaptls(&sym
->st_size
);
1243 bswap16s(&sym
->st_shndx
);
1246 static inline void bswap_ehdr(struct elfhdr
*ehdr
) { }
1247 static inline void bswap_phdr(struct elf_phdr
*phdr
, int phnum
) { }
1248 static inline void bswap_shdr(struct elf_shdr
*shdr
, int shnum
) { }
1249 static inline void bswap_sym(struct elf_sym
*sym
) { }
1252 #ifdef USE_ELF_CORE_DUMP
1253 static int elf_core_dump(int, const CPUArchState
*);
1254 #endif /* USE_ELF_CORE_DUMP */
1255 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
);
1257 /* Verify the portions of EHDR within E_IDENT for the target.
1258 This can be performed before bswapping the entire header. */
1259 static bool elf_check_ident(struct elfhdr
*ehdr
)
1261 return (ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
1262 && ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
1263 && ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
1264 && ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
1265 && ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
1266 && ehdr
->e_ident
[EI_DATA
] == ELF_DATA
1267 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
);
1270 /* Verify the portions of EHDR outside of E_IDENT for the target.
1271 This has to wait until after bswapping the header. */
1272 static bool elf_check_ehdr(struct elfhdr
*ehdr
)
1274 return (elf_check_arch(ehdr
->e_machine
)
1275 && ehdr
->e_ehsize
== sizeof(struct elfhdr
)
1276 && ehdr
->e_phentsize
== sizeof(struct elf_phdr
)
1277 && ehdr
->e_shentsize
== sizeof(struct elf_shdr
)
1278 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
));
1282 * 'copy_elf_strings()' copies argument/envelope strings from user
1283 * memory to free pages in kernel mem. These are in a format ready
1284 * to be put directly into the top of new user memory.
1287 static abi_ulong
copy_elf_strings(int argc
,char ** argv
, void **page
,
1290 char *tmp
, *tmp1
, *pag
= NULL
;
1291 int len
, offset
= 0;
1294 return 0; /* bullet-proofing */
1296 while (argc
-- > 0) {
1299 fprintf(stderr
, "VFS: argc is wrong");
1305 if (p
< len
) { /* this shouldn't happen - 128kB */
1311 offset
= p
% TARGET_PAGE_SIZE
;
1312 pag
= (char *)page
[p
/TARGET_PAGE_SIZE
];
1314 pag
= g_try_malloc0(TARGET_PAGE_SIZE
);
1315 page
[p
/TARGET_PAGE_SIZE
] = pag
;
1320 if (len
== 0 || offset
== 0) {
1321 *(pag
+ offset
) = *tmp
;
1324 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
1325 tmp
-= bytes_to_copy
;
1327 offset
-= bytes_to_copy
;
1328 len
-= bytes_to_copy
;
1329 memcpy_fromfs(pag
+ offset
, tmp
, bytes_to_copy
+ 1);
1336 static abi_ulong
setup_arg_pages(abi_ulong p
, struct linux_binprm
*bprm
,
1337 struct image_info
*info
)
1339 abi_ulong stack_base
, size
, error
, guard
;
1342 /* Create enough stack to hold everything. If we don't use
1343 it for args, we'll use it for something else. */
1344 size
= guest_stack_size
;
1345 if (size
< MAX_ARG_PAGES
*TARGET_PAGE_SIZE
) {
1346 size
= MAX_ARG_PAGES
*TARGET_PAGE_SIZE
;
1348 guard
= TARGET_PAGE_SIZE
;
1349 if (guard
< qemu_real_host_page_size
) {
1350 guard
= qemu_real_host_page_size
;
1353 error
= target_mmap(0, size
+ guard
, PROT_READ
| PROT_WRITE
,
1354 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1356 perror("mmap stack");
1360 /* We reserve one extra page at the top of the stack as guard. */
1361 target_mprotect(error
, guard
, PROT_NONE
);
1363 info
->stack_limit
= error
+ guard
;
1364 stack_base
= info
->stack_limit
+ size
- MAX_ARG_PAGES
*TARGET_PAGE_SIZE
;
1367 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1368 if (bprm
->page
[i
]) {
1370 /* FIXME - check return value of memcpy_to_target() for failure */
1371 memcpy_to_target(stack_base
, bprm
->page
[i
], TARGET_PAGE_SIZE
);
1372 g_free(bprm
->page
[i
]);
1374 stack_base
+= TARGET_PAGE_SIZE
;
1379 /* Map and zero the bss. We need to explicitly zero any fractional pages
1380 after the data section (i.e. bss). */
1381 static void zero_bss(abi_ulong elf_bss
, abi_ulong last_bss
, int prot
)
1383 uintptr_t host_start
, host_map_start
, host_end
;
1385 last_bss
= TARGET_PAGE_ALIGN(last_bss
);
1387 /* ??? There is confusion between qemu_real_host_page_size and
1388 qemu_host_page_size here and elsewhere in target_mmap, which
1389 may lead to the end of the data section mapping from the file
1390 not being mapped. At least there was an explicit test and
1391 comment for that here, suggesting that "the file size must
1392 be known". The comment probably pre-dates the introduction
1393 of the fstat system call in target_mmap which does in fact
1394 find out the size. What isn't clear is if the workaround
1395 here is still actually needed. For now, continue with it,
1396 but merge it with the "normal" mmap that would allocate the bss. */
1398 host_start
= (uintptr_t) g2h(elf_bss
);
1399 host_end
= (uintptr_t) g2h(last_bss
);
1400 host_map_start
= (host_start
+ qemu_real_host_page_size
- 1);
1401 host_map_start
&= -qemu_real_host_page_size
;
1403 if (host_map_start
< host_end
) {
1404 void *p
= mmap((void *)host_map_start
, host_end
- host_map_start
,
1405 prot
, MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1406 if (p
== MAP_FAILED
) {
1407 perror("cannot mmap brk");
1411 /* Since we didn't use target_mmap, make sure to record
1412 the validity of the pages with qemu. */
1413 page_set_flags(elf_bss
& TARGET_PAGE_MASK
, last_bss
, prot
|PAGE_VALID
);
1416 if (host_start
< host_map_start
) {
1417 memset((void *)host_start
, 0, host_map_start
- host_start
);
1421 #ifdef CONFIG_USE_FDPIC
1422 static abi_ulong
loader_build_fdpic_loadmap(struct image_info
*info
, abi_ulong sp
)
1425 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
;
1427 /* elf32_fdpic_loadseg */
1431 put_user_u32(loadsegs
[n
].addr
, sp
+0);
1432 put_user_u32(loadsegs
[n
].p_vaddr
, sp
+4);
1433 put_user_u32(loadsegs
[n
].p_memsz
, sp
+8);
1436 /* elf32_fdpic_loadmap */
1438 put_user_u16(0, sp
+0); /* version */
1439 put_user_u16(info
->nsegs
, sp
+2); /* nsegs */
1441 info
->personality
= PER_LINUX_FDPIC
;
1442 info
->loadmap_addr
= sp
;
1448 static abi_ulong
create_elf_tables(abi_ulong p
, int argc
, int envc
,
1449 struct elfhdr
*exec
,
1450 struct image_info
*info
,
1451 struct image_info
*interp_info
)
1457 abi_ulong u_rand_bytes
;
1458 uint8_t k_rand_bytes
[16];
1459 abi_ulong u_platform
;
1460 const char *k_platform
;
1461 const int n
= sizeof(elf_addr_t
);
1465 #ifdef CONFIG_USE_FDPIC
1466 /* Needs to be before we load the env/argc/... */
1467 if (elf_is_fdpic(exec
)) {
1468 /* Need 4 byte alignment for these structs */
1470 sp
= loader_build_fdpic_loadmap(info
, sp
);
1471 info
->other_info
= interp_info
;
1473 interp_info
->other_info
= info
;
1474 sp
= loader_build_fdpic_loadmap(interp_info
, sp
);
1480 k_platform
= ELF_PLATFORM
;
1482 size_t len
= strlen(k_platform
) + 1;
1483 sp
-= (len
+ n
- 1) & ~(n
- 1);
1485 /* FIXME - check return value of memcpy_to_target() for failure */
1486 memcpy_to_target(sp
, k_platform
, len
);
1490 * Generate 16 random bytes for userspace PRNG seeding (not
1491 * cryptically secure but it's not the aim of QEMU).
1493 srand((unsigned int) time(NULL
));
1494 for (i
= 0; i
< 16; i
++) {
1495 k_rand_bytes
[i
] = rand();
1499 /* FIXME - check return value of memcpy_to_target() for failure */
1500 memcpy_to_target(sp
, k_rand_bytes
, 16);
1503 * Force 16 byte _final_ alignment here for generality.
1505 sp
= sp
&~ (abi_ulong
)15;
1506 size
= (DLINFO_ITEMS
+ 1) * 2;
1509 #ifdef DLINFO_ARCH_ITEMS
1510 size
+= DLINFO_ARCH_ITEMS
* 2;
1515 size
+= envc
+ argc
+ 2;
1516 size
+= 1; /* argc itself */
1519 sp
-= 16 - (size
& 15);
1521 /* This is correct because Linux defines
1522 * elf_addr_t as Elf32_Off / Elf64_Off
1524 #define NEW_AUX_ENT(id, val) do { \
1525 sp -= n; put_user_ual(val, sp); \
1526 sp -= n; put_user_ual(id, sp); \
1530 NEW_AUX_ENT (AT_NULL
, 0);
1532 /* There must be exactly DLINFO_ITEMS entries here. */
1533 NEW_AUX_ENT(AT_PHDR
, (abi_ulong
)(info
->load_addr
+ exec
->e_phoff
));
1534 NEW_AUX_ENT(AT_PHENT
, (abi_ulong
)(sizeof (struct elf_phdr
)));
1535 NEW_AUX_ENT(AT_PHNUM
, (abi_ulong
)(exec
->e_phnum
));
1536 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(TARGET_PAGE_SIZE
));
1537 NEW_AUX_ENT(AT_BASE
, (abi_ulong
)(interp_info
? interp_info
->load_addr
: 0));
1538 NEW_AUX_ENT(AT_FLAGS
, (abi_ulong
)0);
1539 NEW_AUX_ENT(AT_ENTRY
, info
->entry
);
1540 NEW_AUX_ENT(AT_UID
, (abi_ulong
) getuid());
1541 NEW_AUX_ENT(AT_EUID
, (abi_ulong
) geteuid());
1542 NEW_AUX_ENT(AT_GID
, (abi_ulong
) getgid());
1543 NEW_AUX_ENT(AT_EGID
, (abi_ulong
) getegid());
1544 NEW_AUX_ENT(AT_HWCAP
, (abi_ulong
) ELF_HWCAP
);
1545 NEW_AUX_ENT(AT_CLKTCK
, (abi_ulong
) sysconf(_SC_CLK_TCK
));
1546 NEW_AUX_ENT(AT_RANDOM
, (abi_ulong
) u_rand_bytes
);
1549 NEW_AUX_ENT(AT_HWCAP2
, (abi_ulong
) ELF_HWCAP2
);
1553 NEW_AUX_ENT(AT_PLATFORM
, u_platform
);
1556 * ARCH_DLINFO must come last so platform specific code can enforce
1557 * special alignment requirements on the AUXV if necessary (eg. PPC).
1563 info
->saved_auxv
= sp
;
1564 info
->auxv_len
= sp_auxv
- sp
;
1566 sp
= loader_build_argptr(envc
, argc
, sp
, p
, 0);
1567 /* Check the right amount of stack was allocated for auxvec, envp & argv. */
1568 assert(sp_auxv
- sp
== size
);
1572 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1573 /* If the guest doesn't have a validation function just agree */
1574 static int validate_guest_space(unsigned long guest_base
,
1575 unsigned long guest_size
)
1581 unsigned long init_guest_space(unsigned long host_start
,
1582 unsigned long host_size
,
1583 unsigned long guest_start
,
1586 unsigned long current_start
, real_start
;
1589 assert(host_start
|| host_size
);
1591 /* If just a starting address is given, then just verify that
1593 if (host_start
&& !host_size
) {
1594 if (validate_guest_space(host_start
, host_size
) == 1) {
1597 return (unsigned long)-1;
1601 /* Setup the initial flags and start address. */
1602 current_start
= host_start
& qemu_host_page_mask
;
1603 flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
;
1608 /* Otherwise, a non-zero size region of memory needs to be mapped
1611 unsigned long real_size
= host_size
;
1613 /* Do not use mmap_find_vma here because that is limited to the
1614 * guest address space. We are going to make the
1615 * guest address space fit whatever we're given.
1617 real_start
= (unsigned long)
1618 mmap((void *)current_start
, host_size
, PROT_NONE
, flags
, -1, 0);
1619 if (real_start
== (unsigned long)-1) {
1620 return (unsigned long)-1;
1623 /* Ensure the address is properly aligned. */
1624 if (real_start
& ~qemu_host_page_mask
) {
1625 munmap((void *)real_start
, host_size
);
1626 real_size
= host_size
+ qemu_host_page_size
;
1627 real_start
= (unsigned long)
1628 mmap((void *)real_start
, real_size
, PROT_NONE
, flags
, -1, 0);
1629 if (real_start
== (unsigned long)-1) {
1630 return (unsigned long)-1;
1632 real_start
= HOST_PAGE_ALIGN(real_start
);
1635 /* Check to see if the address is valid. */
1636 if (!host_start
|| real_start
== current_start
) {
1637 int valid
= validate_guest_space(real_start
- guest_start
,
1641 } else if (valid
== -1) {
1642 return (unsigned long)-1;
1644 /* valid == 0, so try again. */
1647 /* That address didn't work. Unmap and try a different one.
1648 * The address the host picked because is typically right at
1649 * the top of the host address space and leaves the guest with
1650 * no usable address space. Resort to a linear search. We
1651 * already compensated for mmap_min_addr, so this should not
1652 * happen often. Probably means we got unlucky and host
1653 * address space randomization put a shared library somewhere
1656 munmap((void *)real_start
, host_size
);
1657 current_start
+= qemu_host_page_size
;
1658 if (host_start
== current_start
) {
1659 /* Theoretically possible if host doesn't have any suitably
1660 * aligned areas. Normally the first mmap will fail.
1662 return (unsigned long)-1;
1666 qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size
);
1671 static void probe_guest_base(const char *image_name
,
1672 abi_ulong loaddr
, abi_ulong hiaddr
)
1674 /* Probe for a suitable guest base address, if the user has not set
1675 * it explicitly, and set guest_base appropriately.
1676 * In case of error we will print a suitable message and exit.
1678 #if defined(CONFIG_USE_GUEST_BASE)
1680 if (!have_guest_base
&& !reserved_va
) {
1681 unsigned long host_start
, real_start
, host_size
;
1683 /* Round addresses to page boundaries. */
1684 loaddr
&= qemu_host_page_mask
;
1685 hiaddr
= HOST_PAGE_ALIGN(hiaddr
);
1687 if (loaddr
< mmap_min_addr
) {
1688 host_start
= HOST_PAGE_ALIGN(mmap_min_addr
);
1690 host_start
= loaddr
;
1691 if (host_start
!= loaddr
) {
1692 errmsg
= "Address overflow loading ELF binary";
1696 host_size
= hiaddr
- loaddr
;
1698 /* Setup the initial guest memory space with ranges gleaned from
1699 * the ELF image that is being loaded.
1701 real_start
= init_guest_space(host_start
, host_size
, loaddr
, false);
1702 if (real_start
== (unsigned long)-1) {
1703 errmsg
= "Unable to find space for application";
1706 guest_base
= real_start
- loaddr
;
1708 qemu_log("Relocating guest address space from 0x"
1709 TARGET_ABI_FMT_lx
" to 0x%lx\n",
1710 loaddr
, real_start
);
1715 fprintf(stderr
, "%s: %s\n", image_name
, errmsg
);
1721 /* Load an ELF image into the address space.
1723 IMAGE_NAME is the filename of the image, to use in error messages.
1724 IMAGE_FD is the open file descriptor for the image.
1726 BPRM_BUF is a copy of the beginning of the file; this of course
1727 contains the elf file header at offset 0. It is assumed that this
1728 buffer is sufficiently aligned to present no problems to the host
1729 in accessing data at aligned offsets within the buffer.
1731 On return: INFO values will be filled in, as necessary or available. */
1733 static void load_elf_image(const char *image_name
, int image_fd
,
1734 struct image_info
*info
, char **pinterp_name
,
1735 char bprm_buf
[BPRM_BUF_SIZE
])
1737 struct elfhdr
*ehdr
= (struct elfhdr
*)bprm_buf
;
1738 struct elf_phdr
*phdr
;
1739 abi_ulong load_addr
, load_bias
, loaddr
, hiaddr
, error
;
1743 /* First of all, some simple consistency checks */
1744 errmsg
= "Invalid ELF image for this architecture";
1745 if (!elf_check_ident(ehdr
)) {
1749 if (!elf_check_ehdr(ehdr
)) {
1753 i
= ehdr
->e_phnum
* sizeof(struct elf_phdr
);
1754 if (ehdr
->e_phoff
+ i
<= BPRM_BUF_SIZE
) {
1755 phdr
= (struct elf_phdr
*)(bprm_buf
+ ehdr
->e_phoff
);
1757 phdr
= (struct elf_phdr
*) alloca(i
);
1758 retval
= pread(image_fd
, phdr
, i
, ehdr
->e_phoff
);
1763 bswap_phdr(phdr
, ehdr
->e_phnum
);
1765 #ifdef CONFIG_USE_FDPIC
1767 info
->pt_dynamic_addr
= 0;
1770 /* Find the maximum size of the image and allocate an appropriate
1771 amount of memory to handle that. */
1772 loaddr
= -1, hiaddr
= 0;
1773 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
1774 if (phdr
[i
].p_type
== PT_LOAD
) {
1775 abi_ulong a
= phdr
[i
].p_vaddr
;
1779 a
+= phdr
[i
].p_memsz
;
1783 #ifdef CONFIG_USE_FDPIC
1790 if (ehdr
->e_type
== ET_DYN
) {
1791 /* The image indicates that it can be loaded anywhere. Find a
1792 location that can hold the memory space required. If the
1793 image is pre-linked, LOADDR will be non-zero. Since we do
1794 not supply MAP_FIXED here we'll use that address if and
1795 only if it remains available. */
1796 load_addr
= target_mmap(loaddr
, hiaddr
- loaddr
, PROT_NONE
,
1797 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
,
1799 if (load_addr
== -1) {
1802 } else if (pinterp_name
!= NULL
) {
1803 /* This is the main executable. Make sure that the low
1804 address does not conflict with MMAP_MIN_ADDR or the
1805 QEMU application itself. */
1806 probe_guest_base(image_name
, loaddr
, hiaddr
);
1808 load_bias
= load_addr
- loaddr
;
1810 #ifdef CONFIG_USE_FDPIC
1812 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
=
1813 g_malloc(sizeof(*loadsegs
) * info
->nsegs
);
1815 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
1816 switch (phdr
[i
].p_type
) {
1818 info
->pt_dynamic_addr
= phdr
[i
].p_vaddr
+ load_bias
;
1821 loadsegs
->addr
= phdr
[i
].p_vaddr
+ load_bias
;
1822 loadsegs
->p_vaddr
= phdr
[i
].p_vaddr
;
1823 loadsegs
->p_memsz
= phdr
[i
].p_memsz
;
1831 info
->load_bias
= load_bias
;
1832 info
->load_addr
= load_addr
;
1833 info
->entry
= ehdr
->e_entry
+ load_bias
;
1834 info
->start_code
= -1;
1836 info
->start_data
= -1;
1839 info
->elf_flags
= ehdr
->e_flags
;
1841 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
1842 struct elf_phdr
*eppnt
= phdr
+ i
;
1843 if (eppnt
->p_type
== PT_LOAD
) {
1844 abi_ulong vaddr
, vaddr_po
, vaddr_ps
, vaddr_ef
, vaddr_em
;
1847 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
1848 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
1849 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
1851 vaddr
= load_bias
+ eppnt
->p_vaddr
;
1852 vaddr_po
= TARGET_ELF_PAGEOFFSET(vaddr
);
1853 vaddr_ps
= TARGET_ELF_PAGESTART(vaddr
);
1855 error
= target_mmap(vaddr_ps
, eppnt
->p_filesz
+ vaddr_po
,
1856 elf_prot
, MAP_PRIVATE
| MAP_FIXED
,
1857 image_fd
, eppnt
->p_offset
- vaddr_po
);
1862 vaddr_ef
= vaddr
+ eppnt
->p_filesz
;
1863 vaddr_em
= vaddr
+ eppnt
->p_memsz
;
1865 /* If the load segment requests extra zeros (e.g. bss), map it. */
1866 if (vaddr_ef
< vaddr_em
) {
1867 zero_bss(vaddr_ef
, vaddr_em
, elf_prot
);
1870 /* Find the full program boundaries. */
1871 if (elf_prot
& PROT_EXEC
) {
1872 if (vaddr
< info
->start_code
) {
1873 info
->start_code
= vaddr
;
1875 if (vaddr_ef
> info
->end_code
) {
1876 info
->end_code
= vaddr_ef
;
1879 if (elf_prot
& PROT_WRITE
) {
1880 if (vaddr
< info
->start_data
) {
1881 info
->start_data
= vaddr
;
1883 if (vaddr_ef
> info
->end_data
) {
1884 info
->end_data
= vaddr_ef
;
1886 if (vaddr_em
> info
->brk
) {
1887 info
->brk
= vaddr_em
;
1890 } else if (eppnt
->p_type
== PT_INTERP
&& pinterp_name
) {
1893 if (*pinterp_name
) {
1894 errmsg
= "Multiple PT_INTERP entries";
1897 interp_name
= malloc(eppnt
->p_filesz
);
1902 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
1903 memcpy(interp_name
, bprm_buf
+ eppnt
->p_offset
,
1906 retval
= pread(image_fd
, interp_name
, eppnt
->p_filesz
,
1908 if (retval
!= eppnt
->p_filesz
) {
1912 if (interp_name
[eppnt
->p_filesz
- 1] != 0) {
1913 errmsg
= "Invalid PT_INTERP entry";
1916 *pinterp_name
= interp_name
;
1920 if (info
->end_data
== 0) {
1921 info
->start_data
= info
->end_code
;
1922 info
->end_data
= info
->end_code
;
1923 info
->brk
= info
->end_code
;
1926 if (qemu_log_enabled()) {
1927 load_symbols(ehdr
, image_fd
, load_bias
);
1935 errmsg
= "Incomplete read of file header";
1939 errmsg
= strerror(errno
);
1941 fprintf(stderr
, "%s: %s\n", image_name
, errmsg
);
1945 static void load_elf_interp(const char *filename
, struct image_info
*info
,
1946 char bprm_buf
[BPRM_BUF_SIZE
])
1950 fd
= open(path(filename
), O_RDONLY
);
1955 retval
= read(fd
, bprm_buf
, BPRM_BUF_SIZE
);
1959 if (retval
< BPRM_BUF_SIZE
) {
1960 memset(bprm_buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
1963 load_elf_image(filename
, fd
, info
, NULL
, bprm_buf
);
1967 fprintf(stderr
, "%s: %s\n", filename
, strerror(errno
));
1971 static int symfind(const void *s0
, const void *s1
)
1973 target_ulong addr
= *(target_ulong
*)s0
;
1974 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
1976 if (addr
< sym
->st_value
) {
1978 } else if (addr
>= sym
->st_value
+ sym
->st_size
) {
1984 static const char *lookup_symbolxx(struct syminfo
*s
, target_ulong orig_addr
)
1986 #if ELF_CLASS == ELFCLASS32
1987 struct elf_sym
*syms
= s
->disas_symtab
.elf32
;
1989 struct elf_sym
*syms
= s
->disas_symtab
.elf64
;
1993 struct elf_sym
*sym
;
1995 sym
= bsearch(&orig_addr
, syms
, s
->disas_num_syms
, sizeof(*syms
), symfind
);
1997 return s
->disas_strtab
+ sym
->st_name
;
2003 /* FIXME: This should use elf_ops.h */
2004 static int symcmp(const void *s0
, const void *s1
)
2006 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
2007 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
2008 return (sym0
->st_value
< sym1
->st_value
)
2010 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
2013 /* Best attempt to load symbols from this ELF object. */
2014 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
)
2016 int i
, shnum
, nsyms
, sym_idx
= 0, str_idx
= 0;
2017 struct elf_shdr
*shdr
;
2018 char *strings
= NULL
;
2019 struct syminfo
*s
= NULL
;
2020 struct elf_sym
*new_syms
, *syms
= NULL
;
2022 shnum
= hdr
->e_shnum
;
2023 i
= shnum
* sizeof(struct elf_shdr
);
2024 shdr
= (struct elf_shdr
*)alloca(i
);
2025 if (pread(fd
, shdr
, i
, hdr
->e_shoff
) != i
) {
2029 bswap_shdr(shdr
, shnum
);
2030 for (i
= 0; i
< shnum
; ++i
) {
2031 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
2033 str_idx
= shdr
[i
].sh_link
;
2038 /* There will be no symbol table if the file was stripped. */
2042 /* Now know where the strtab and symtab are. Snarf them. */
2043 s
= malloc(sizeof(*s
));
2048 i
= shdr
[str_idx
].sh_size
;
2049 s
->disas_strtab
= strings
= malloc(i
);
2050 if (!strings
|| pread(fd
, strings
, i
, shdr
[str_idx
].sh_offset
) != i
) {
2054 i
= shdr
[sym_idx
].sh_size
;
2056 if (!syms
|| pread(fd
, syms
, i
, shdr
[sym_idx
].sh_offset
) != i
) {
2060 nsyms
= i
/ sizeof(struct elf_sym
);
2061 for (i
= 0; i
< nsyms
; ) {
2062 bswap_sym(syms
+ i
);
2063 /* Throw away entries which we do not need. */
2064 if (syms
[i
].st_shndx
== SHN_UNDEF
2065 || syms
[i
].st_shndx
>= SHN_LORESERVE
2066 || ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
2068 syms
[i
] = syms
[nsyms
];
2071 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2072 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2073 syms
[i
].st_value
&= ~(target_ulong
)1;
2075 syms
[i
].st_value
+= load_bias
;
2080 /* No "useful" symbol. */
2085 /* Attempt to free the storage associated with the local symbols
2086 that we threw away. Whether or not this has any effect on the
2087 memory allocation depends on the malloc implementation and how
2088 many symbols we managed to discard. */
2089 new_syms
= realloc(syms
, nsyms
* sizeof(*syms
));
2090 if (new_syms
== NULL
) {
2095 qsort(syms
, nsyms
, sizeof(*syms
), symcmp
);
2097 s
->disas_num_syms
= nsyms
;
2098 #if ELF_CLASS == ELFCLASS32
2099 s
->disas_symtab
.elf32
= syms
;
2101 s
->disas_symtab
.elf64
= syms
;
2103 s
->lookup_symbol
= lookup_symbolxx
;
2115 int load_elf_binary(struct linux_binprm
*bprm
, struct image_info
*info
)
2117 struct image_info interp_info
;
2118 struct elfhdr elf_ex
;
2119 char *elf_interpreter
= NULL
;
2121 info
->start_mmap
= (abi_ulong
)ELF_START_MMAP
;
2125 load_elf_image(bprm
->filename
, bprm
->fd
, info
,
2126 &elf_interpreter
, bprm
->buf
);
2128 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2129 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2130 when we load the interpreter. */
2131 elf_ex
= *(struct elfhdr
*)bprm
->buf
;
2133 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, bprm
->page
, bprm
->p
);
2134 bprm
->p
= copy_elf_strings(bprm
->envc
,bprm
->envp
,bprm
->page
,bprm
->p
);
2135 bprm
->p
= copy_elf_strings(bprm
->argc
,bprm
->argv
,bprm
->page
,bprm
->p
);
2137 fprintf(stderr
, "%s: %s\n", bprm
->filename
, strerror(E2BIG
));
2141 /* Do this so that we can load the interpreter, if need be. We will
2142 change some of these later */
2143 bprm
->p
= setup_arg_pages(bprm
->p
, bprm
, info
);
2145 if (elf_interpreter
) {
2146 load_elf_interp(elf_interpreter
, &interp_info
, bprm
->buf
);
2148 /* If the program interpreter is one of these two, then assume
2149 an iBCS2 image. Otherwise assume a native linux image. */
2151 if (strcmp(elf_interpreter
, "/usr/lib/libc.so.1") == 0
2152 || strcmp(elf_interpreter
, "/usr/lib/ld.so.1") == 0) {
2153 info
->personality
= PER_SVR4
;
2155 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2156 and some applications "depend" upon this behavior. Since
2157 we do not have the power to recompile these, we emulate
2158 the SVr4 behavior. Sigh. */
2159 target_mmap(0, qemu_host_page_size
, PROT_READ
| PROT_EXEC
,
2160 MAP_FIXED
| MAP_PRIVATE
, -1, 0);
2164 bprm
->p
= create_elf_tables(bprm
->p
, bprm
->argc
, bprm
->envc
, &elf_ex
,
2165 info
, (elf_interpreter
? &interp_info
: NULL
));
2166 info
->start_stack
= bprm
->p
;
2168 /* If we have an interpreter, set that as the program's entry point.
2169 Copy the load_bias as well, to help PPC64 interpret the entry
2170 point as a function descriptor. Do this after creating elf tables
2171 so that we copy the original program entry point into the AUXV. */
2172 if (elf_interpreter
) {
2173 info
->load_bias
= interp_info
.load_bias
;
2174 info
->entry
= interp_info
.entry
;
2175 free(elf_interpreter
);
2178 #ifdef USE_ELF_CORE_DUMP
2179 bprm
->core_dump
= &elf_core_dump
;
2185 #ifdef USE_ELF_CORE_DUMP
2187 * Definitions to generate Intel SVR4-like core files.
2188 * These mostly have the same names as the SVR4 types with "target_elf_"
2189 * tacked on the front to prevent clashes with linux definitions,
2190 * and the typedef forms have been avoided. This is mostly like
2191 * the SVR4 structure, but more Linuxy, with things that Linux does
2192 * not support and which gdb doesn't really use excluded.
2194 * Fields we don't dump (their contents is zero) in linux-user qemu
2195 * are marked with XXX.
2197 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2199 * Porting ELF coredump for target is (quite) simple process. First you
2200 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2201 * the target resides):
2203 * #define USE_ELF_CORE_DUMP
2205 * Next you define type of register set used for dumping. ELF specification
2206 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2208 * typedef <target_regtype> target_elf_greg_t;
2209 * #define ELF_NREG <number of registers>
2210 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2212 * Last step is to implement target specific function that copies registers
2213 * from given cpu into just specified register set. Prototype is:
2215 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2216 * const CPUArchState *env);
2219 * regs - copy register values into here (allocated and zeroed by caller)
2220 * env - copy registers from here
2222 * Example for ARM target is provided in this file.
2225 /* An ELF note in memory */
2229 size_t namesz_rounded
;
2232 size_t datasz_rounded
;
2237 struct target_elf_siginfo
{
2238 abi_int si_signo
; /* signal number */
2239 abi_int si_code
; /* extra code */
2240 abi_int si_errno
; /* errno */
2243 struct target_elf_prstatus
{
2244 struct target_elf_siginfo pr_info
; /* Info associated with signal */
2245 abi_short pr_cursig
; /* Current signal */
2246 abi_ulong pr_sigpend
; /* XXX */
2247 abi_ulong pr_sighold
; /* XXX */
2248 target_pid_t pr_pid
;
2249 target_pid_t pr_ppid
;
2250 target_pid_t pr_pgrp
;
2251 target_pid_t pr_sid
;
2252 struct target_timeval pr_utime
; /* XXX User time */
2253 struct target_timeval pr_stime
; /* XXX System time */
2254 struct target_timeval pr_cutime
; /* XXX Cumulative user time */
2255 struct target_timeval pr_cstime
; /* XXX Cumulative system time */
2256 target_elf_gregset_t pr_reg
; /* GP registers */
2257 abi_int pr_fpvalid
; /* XXX */
2260 #define ELF_PRARGSZ (80) /* Number of chars for args */
2262 struct target_elf_prpsinfo
{
2263 char pr_state
; /* numeric process state */
2264 char pr_sname
; /* char for pr_state */
2265 char pr_zomb
; /* zombie */
2266 char pr_nice
; /* nice val */
2267 abi_ulong pr_flag
; /* flags */
2268 target_uid_t pr_uid
;
2269 target_gid_t pr_gid
;
2270 target_pid_t pr_pid
, pr_ppid
, pr_pgrp
, pr_sid
;
2272 char pr_fname
[16]; /* filename of executable */
2273 char pr_psargs
[ELF_PRARGSZ
]; /* initial part of arg list */
2276 /* Here is the structure in which status of each thread is captured. */
2277 struct elf_thread_status
{
2278 QTAILQ_ENTRY(elf_thread_status
) ets_link
;
2279 struct target_elf_prstatus prstatus
; /* NT_PRSTATUS */
2281 elf_fpregset_t fpu
; /* NT_PRFPREG */
2282 struct task_struct
*thread
;
2283 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
2285 struct memelfnote notes
[1];
2289 struct elf_note_info
{
2290 struct memelfnote
*notes
;
2291 struct target_elf_prstatus
*prstatus
; /* NT_PRSTATUS */
2292 struct target_elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
2294 QTAILQ_HEAD(thread_list_head
, elf_thread_status
) thread_list
;
2297 * Current version of ELF coredump doesn't support
2298 * dumping fp regs etc.
2300 elf_fpregset_t
*fpu
;
2301 elf_fpxregset_t
*xfpu
;
2302 int thread_status_size
;
2308 struct vm_area_struct
{
2309 abi_ulong vma_start
; /* start vaddr of memory region */
2310 abi_ulong vma_end
; /* end vaddr of memory region */
2311 abi_ulong vma_flags
; /* protection etc. flags for the region */
2312 QTAILQ_ENTRY(vm_area_struct
) vma_link
;
2316 QTAILQ_HEAD(, vm_area_struct
) mm_mmap
;
2317 int mm_count
; /* number of mappings */
2320 static struct mm_struct
*vma_init(void);
2321 static void vma_delete(struct mm_struct
*);
2322 static int vma_add_mapping(struct mm_struct
*, abi_ulong
,
2323 abi_ulong
, abi_ulong
);
2324 static int vma_get_mapping_count(const struct mm_struct
*);
2325 static struct vm_area_struct
*vma_first(const struct mm_struct
*);
2326 static struct vm_area_struct
*vma_next(struct vm_area_struct
*);
2327 static abi_ulong
vma_dump_size(const struct vm_area_struct
*);
2328 static int vma_walker(void *priv
, abi_ulong start
, abi_ulong end
,
2329 unsigned long flags
);
2331 static void fill_elf_header(struct elfhdr
*, int, uint16_t, uint32_t);
2332 static void fill_note(struct memelfnote
*, const char *, int,
2333 unsigned int, void *);
2334 static void fill_prstatus(struct target_elf_prstatus
*, const TaskState
*, int);
2335 static int fill_psinfo(struct target_elf_prpsinfo
*, const TaskState
*);
2336 static void fill_auxv_note(struct memelfnote
*, const TaskState
*);
2337 static void fill_elf_note_phdr(struct elf_phdr
*, int, off_t
);
2338 static size_t note_size(const struct memelfnote
*);
2339 static void free_note_info(struct elf_note_info
*);
2340 static int fill_note_info(struct elf_note_info
*, long, const CPUArchState
*);
2341 static void fill_thread_info(struct elf_note_info
*, const CPUArchState
*);
2342 static int core_dump_filename(const TaskState
*, char *, size_t);
2344 static int dump_write(int, const void *, size_t);
2345 static int write_note(struct memelfnote
*, int);
2346 static int write_note_info(struct elf_note_info
*, int);
2349 static void bswap_prstatus(struct target_elf_prstatus
*prstatus
)
2351 prstatus
->pr_info
.si_signo
= tswap32(prstatus
->pr_info
.si_signo
);
2352 prstatus
->pr_info
.si_code
= tswap32(prstatus
->pr_info
.si_code
);
2353 prstatus
->pr_info
.si_errno
= tswap32(prstatus
->pr_info
.si_errno
);
2354 prstatus
->pr_cursig
= tswap16(prstatus
->pr_cursig
);
2355 prstatus
->pr_sigpend
= tswapal(prstatus
->pr_sigpend
);
2356 prstatus
->pr_sighold
= tswapal(prstatus
->pr_sighold
);
2357 prstatus
->pr_pid
= tswap32(prstatus
->pr_pid
);
2358 prstatus
->pr_ppid
= tswap32(prstatus
->pr_ppid
);
2359 prstatus
->pr_pgrp
= tswap32(prstatus
->pr_pgrp
);
2360 prstatus
->pr_sid
= tswap32(prstatus
->pr_sid
);
2361 /* cpu times are not filled, so we skip them */
2362 /* regs should be in correct format already */
2363 prstatus
->pr_fpvalid
= tswap32(prstatus
->pr_fpvalid
);
2366 static void bswap_psinfo(struct target_elf_prpsinfo
*psinfo
)
2368 psinfo
->pr_flag
= tswapal(psinfo
->pr_flag
);
2369 psinfo
->pr_uid
= tswap16(psinfo
->pr_uid
);
2370 psinfo
->pr_gid
= tswap16(psinfo
->pr_gid
);
2371 psinfo
->pr_pid
= tswap32(psinfo
->pr_pid
);
2372 psinfo
->pr_ppid
= tswap32(psinfo
->pr_ppid
);
2373 psinfo
->pr_pgrp
= tswap32(psinfo
->pr_pgrp
);
2374 psinfo
->pr_sid
= tswap32(psinfo
->pr_sid
);
2377 static void bswap_note(struct elf_note
*en
)
2379 bswap32s(&en
->n_namesz
);
2380 bswap32s(&en
->n_descsz
);
2381 bswap32s(&en
->n_type
);
2384 static inline void bswap_prstatus(struct target_elf_prstatus
*p
) { }
2385 static inline void bswap_psinfo(struct target_elf_prpsinfo
*p
) {}
2386 static inline void bswap_note(struct elf_note
*en
) { }
2387 #endif /* BSWAP_NEEDED */
2390 * Minimal support for linux memory regions. These are needed
2391 * when we are finding out what memory exactly belongs to
2392 * emulated process. No locks needed here, as long as
2393 * thread that received the signal is stopped.
2396 static struct mm_struct
*vma_init(void)
2398 struct mm_struct
*mm
;
2400 if ((mm
= g_malloc(sizeof (*mm
))) == NULL
)
2404 QTAILQ_INIT(&mm
->mm_mmap
);
2409 static void vma_delete(struct mm_struct
*mm
)
2411 struct vm_area_struct
*vma
;
2413 while ((vma
= vma_first(mm
)) != NULL
) {
2414 QTAILQ_REMOVE(&mm
->mm_mmap
, vma
, vma_link
);
2420 static int vma_add_mapping(struct mm_struct
*mm
, abi_ulong start
,
2421 abi_ulong end
, abi_ulong flags
)
2423 struct vm_area_struct
*vma
;
2425 if ((vma
= g_malloc0(sizeof (*vma
))) == NULL
)
2428 vma
->vma_start
= start
;
2430 vma
->vma_flags
= flags
;
2432 QTAILQ_INSERT_TAIL(&mm
->mm_mmap
, vma
, vma_link
);
2438 static struct vm_area_struct
*vma_first(const struct mm_struct
*mm
)
2440 return (QTAILQ_FIRST(&mm
->mm_mmap
));
2443 static struct vm_area_struct
*vma_next(struct vm_area_struct
*vma
)
2445 return (QTAILQ_NEXT(vma
, vma_link
));
2448 static int vma_get_mapping_count(const struct mm_struct
*mm
)
2450 return (mm
->mm_count
);
2454 * Calculate file (dump) size of given memory region.
2456 static abi_ulong
vma_dump_size(const struct vm_area_struct
*vma
)
2458 /* if we cannot even read the first page, skip it */
2459 if (!access_ok(VERIFY_READ
, vma
->vma_start
, TARGET_PAGE_SIZE
))
2463 * Usually we don't dump executable pages as they contain
2464 * non-writable code that debugger can read directly from
2465 * target library etc. However, thread stacks are marked
2466 * also executable so we read in first page of given region
2467 * and check whether it contains elf header. If there is
2468 * no elf header, we dump it.
2470 if (vma
->vma_flags
& PROT_EXEC
) {
2471 char page
[TARGET_PAGE_SIZE
];
2473 copy_from_user(page
, vma
->vma_start
, sizeof (page
));
2474 if ((page
[EI_MAG0
] == ELFMAG0
) &&
2475 (page
[EI_MAG1
] == ELFMAG1
) &&
2476 (page
[EI_MAG2
] == ELFMAG2
) &&
2477 (page
[EI_MAG3
] == ELFMAG3
)) {
2479 * Mappings are possibly from ELF binary. Don't dump
2486 return (vma
->vma_end
- vma
->vma_start
);
2489 static int vma_walker(void *priv
, abi_ulong start
, abi_ulong end
,
2490 unsigned long flags
)
2492 struct mm_struct
*mm
= (struct mm_struct
*)priv
;
2494 vma_add_mapping(mm
, start
, end
, flags
);
2498 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
2499 unsigned int sz
, void *data
)
2501 unsigned int namesz
;
2503 namesz
= strlen(name
) + 1;
2505 note
->namesz
= namesz
;
2506 note
->namesz_rounded
= roundup(namesz
, sizeof (int32_t));
2509 note
->datasz_rounded
= roundup(sz
, sizeof (int32_t));
2514 * We calculate rounded up note size here as specified by
2517 note
->notesz
= sizeof (struct elf_note
) +
2518 note
->namesz_rounded
+ note
->datasz_rounded
;
2521 static void fill_elf_header(struct elfhdr
*elf
, int segs
, uint16_t machine
,
2524 (void) memset(elf
, 0, sizeof(*elf
));
2526 (void) memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
2527 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
2528 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
2529 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
2530 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
2532 elf
->e_type
= ET_CORE
;
2533 elf
->e_machine
= machine
;
2534 elf
->e_version
= EV_CURRENT
;
2535 elf
->e_phoff
= sizeof(struct elfhdr
);
2536 elf
->e_flags
= flags
;
2537 elf
->e_ehsize
= sizeof(struct elfhdr
);
2538 elf
->e_phentsize
= sizeof(struct elf_phdr
);
2539 elf
->e_phnum
= segs
;
2544 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
2546 phdr
->p_type
= PT_NOTE
;
2547 phdr
->p_offset
= offset
;
2550 phdr
->p_filesz
= sz
;
2555 bswap_phdr(phdr
, 1);
2558 static size_t note_size(const struct memelfnote
*note
)
2560 return (note
->notesz
);
2563 static void fill_prstatus(struct target_elf_prstatus
*prstatus
,
2564 const TaskState
*ts
, int signr
)
2566 (void) memset(prstatus
, 0, sizeof (*prstatus
));
2567 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
2568 prstatus
->pr_pid
= ts
->ts_tid
;
2569 prstatus
->pr_ppid
= getppid();
2570 prstatus
->pr_pgrp
= getpgrp();
2571 prstatus
->pr_sid
= getsid(0);
2573 bswap_prstatus(prstatus
);
2576 static int fill_psinfo(struct target_elf_prpsinfo
*psinfo
, const TaskState
*ts
)
2578 char *base_filename
;
2579 unsigned int i
, len
;
2581 (void) memset(psinfo
, 0, sizeof (*psinfo
));
2583 len
= ts
->info
->arg_end
- ts
->info
->arg_start
;
2584 if (len
>= ELF_PRARGSZ
)
2585 len
= ELF_PRARGSZ
- 1;
2586 if (copy_from_user(&psinfo
->pr_psargs
, ts
->info
->arg_start
, len
))
2588 for (i
= 0; i
< len
; i
++)
2589 if (psinfo
->pr_psargs
[i
] == 0)
2590 psinfo
->pr_psargs
[i
] = ' ';
2591 psinfo
->pr_psargs
[len
] = 0;
2593 psinfo
->pr_pid
= getpid();
2594 psinfo
->pr_ppid
= getppid();
2595 psinfo
->pr_pgrp
= getpgrp();
2596 psinfo
->pr_sid
= getsid(0);
2597 psinfo
->pr_uid
= getuid();
2598 psinfo
->pr_gid
= getgid();
2600 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
2602 * Using strncpy here is fine: at max-length,
2603 * this field is not NUL-terminated.
2605 (void) strncpy(psinfo
->pr_fname
, base_filename
,
2606 sizeof(psinfo
->pr_fname
));
2608 g_free(base_filename
);
2609 bswap_psinfo(psinfo
);
2613 static void fill_auxv_note(struct memelfnote
*note
, const TaskState
*ts
)
2615 elf_addr_t auxv
= (elf_addr_t
)ts
->info
->saved_auxv
;
2616 elf_addr_t orig_auxv
= auxv
;
2618 int len
= ts
->info
->auxv_len
;
2621 * Auxiliary vector is stored in target process stack. It contains
2622 * {type, value} pairs that we need to dump into note. This is not
2623 * strictly necessary but we do it here for sake of completeness.
2626 /* read in whole auxv vector and copy it to memelfnote */
2627 ptr
= lock_user(VERIFY_READ
, orig_auxv
, len
, 0);
2629 fill_note(note
, "CORE", NT_AUXV
, len
, ptr
);
2630 unlock_user(ptr
, auxv
, len
);
2635 * Constructs name of coredump file. We have following convention
2637 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2639 * Returns 0 in case of success, -1 otherwise (errno is set).
2641 static int core_dump_filename(const TaskState
*ts
, char *buf
,
2645 char *filename
= NULL
;
2646 char *base_filename
= NULL
;
2650 assert(bufsize
>= PATH_MAX
);
2652 if (gettimeofday(&tv
, NULL
) < 0) {
2653 (void) fprintf(stderr
, "unable to get current timestamp: %s",
2658 filename
= strdup(ts
->bprm
->filename
);
2659 base_filename
= strdup(basename(filename
));
2660 (void) strftime(timestamp
, sizeof (timestamp
), "%Y%m%d-%H%M%S",
2661 localtime_r(&tv
.tv_sec
, &tm
));
2662 (void) snprintf(buf
, bufsize
, "qemu_%s_%s_%d.core",
2663 base_filename
, timestamp
, (int)getpid());
2664 free(base_filename
);
2670 static int dump_write(int fd
, const void *ptr
, size_t size
)
2672 const char *bufp
= (const char *)ptr
;
2673 ssize_t bytes_written
, bytes_left
;
2674 struct rlimit dumpsize
;
2678 getrlimit(RLIMIT_CORE
, &dumpsize
);
2679 if ((pos
= lseek(fd
, 0, SEEK_CUR
))==-1) {
2680 if (errno
== ESPIPE
) { /* not a seekable stream */
2686 if (dumpsize
.rlim_cur
<= pos
) {
2688 } else if (dumpsize
.rlim_cur
== RLIM_INFINITY
) {
2691 size_t limit_left
=dumpsize
.rlim_cur
- pos
;
2692 bytes_left
= limit_left
>= size
? size
: limit_left
;
2697 * In normal conditions, single write(2) should do but
2698 * in case of socket etc. this mechanism is more portable.
2701 bytes_written
= write(fd
, bufp
, bytes_left
);
2702 if (bytes_written
< 0) {
2706 } else if (bytes_written
== 0) { /* eof */
2709 bufp
+= bytes_written
;
2710 bytes_left
-= bytes_written
;
2711 } while (bytes_left
> 0);
2716 static int write_note(struct memelfnote
*men
, int fd
)
2720 en
.n_namesz
= men
->namesz
;
2721 en
.n_type
= men
->type
;
2722 en
.n_descsz
= men
->datasz
;
2726 if (dump_write(fd
, &en
, sizeof(en
)) != 0)
2728 if (dump_write(fd
, men
->name
, men
->namesz_rounded
) != 0)
2730 if (dump_write(fd
, men
->data
, men
->datasz_rounded
) != 0)
2736 static void fill_thread_info(struct elf_note_info
*info
, const CPUArchState
*env
)
2738 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)env
);
2739 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
2740 struct elf_thread_status
*ets
;
2742 ets
= g_malloc0(sizeof (*ets
));
2743 ets
->num_notes
= 1; /* only prstatus is dumped */
2744 fill_prstatus(&ets
->prstatus
, ts
, 0);
2745 elf_core_copy_regs(&ets
->prstatus
.pr_reg
, env
);
2746 fill_note(&ets
->notes
[0], "CORE", NT_PRSTATUS
, sizeof (ets
->prstatus
),
2749 QTAILQ_INSERT_TAIL(&info
->thread_list
, ets
, ets_link
);
2751 info
->notes_size
+= note_size(&ets
->notes
[0]);
2754 static void init_note_info(struct elf_note_info
*info
)
2756 /* Initialize the elf_note_info structure so that it is at
2757 * least safe to call free_note_info() on it. Must be
2758 * called before calling fill_note_info().
2760 memset(info
, 0, sizeof (*info
));
2761 QTAILQ_INIT(&info
->thread_list
);
2764 static int fill_note_info(struct elf_note_info
*info
,
2765 long signr
, const CPUArchState
*env
)
2768 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)env
);
2769 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
2772 info
->notes
= g_malloc0(NUMNOTES
* sizeof (struct memelfnote
));
2773 if (info
->notes
== NULL
)
2775 info
->prstatus
= g_malloc0(sizeof (*info
->prstatus
));
2776 if (info
->prstatus
== NULL
)
2778 info
->psinfo
= g_malloc0(sizeof (*info
->psinfo
));
2779 if (info
->prstatus
== NULL
)
2783 * First fill in status (and registers) of current thread
2784 * including process info & aux vector.
2786 fill_prstatus(info
->prstatus
, ts
, signr
);
2787 elf_core_copy_regs(&info
->prstatus
->pr_reg
, env
);
2788 fill_note(&info
->notes
[0], "CORE", NT_PRSTATUS
,
2789 sizeof (*info
->prstatus
), info
->prstatus
);
2790 fill_psinfo(info
->psinfo
, ts
);
2791 fill_note(&info
->notes
[1], "CORE", NT_PRPSINFO
,
2792 sizeof (*info
->psinfo
), info
->psinfo
);
2793 fill_auxv_note(&info
->notes
[2], ts
);
2796 info
->notes_size
= 0;
2797 for (i
= 0; i
< info
->numnote
; i
++)
2798 info
->notes_size
+= note_size(&info
->notes
[i
]);
2800 /* read and fill status of all threads */
2803 if (cpu
== thread_cpu
) {
2806 fill_thread_info(info
, (CPUArchState
*)cpu
->env_ptr
);
2813 static void free_note_info(struct elf_note_info
*info
)
2815 struct elf_thread_status
*ets
;
2817 while (!QTAILQ_EMPTY(&info
->thread_list
)) {
2818 ets
= QTAILQ_FIRST(&info
->thread_list
);
2819 QTAILQ_REMOVE(&info
->thread_list
, ets
, ets_link
);
2823 g_free(info
->prstatus
);
2824 g_free(info
->psinfo
);
2825 g_free(info
->notes
);
2828 static int write_note_info(struct elf_note_info
*info
, int fd
)
2830 struct elf_thread_status
*ets
;
2833 /* write prstatus, psinfo and auxv for current thread */
2834 for (i
= 0; i
< info
->numnote
; i
++)
2835 if ((error
= write_note(&info
->notes
[i
], fd
)) != 0)
2838 /* write prstatus for each thread */
2839 for (ets
= info
->thread_list
.tqh_first
; ets
!= NULL
;
2840 ets
= ets
->ets_link
.tqe_next
) {
2841 if ((error
= write_note(&ets
->notes
[0], fd
)) != 0)
2849 * Write out ELF coredump.
2851 * See documentation of ELF object file format in:
2852 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2854 * Coredump format in linux is following:
2856 * 0 +----------------------+ \
2857 * | ELF header | ET_CORE |
2858 * +----------------------+ |
2859 * | ELF program headers | |--- headers
2860 * | - NOTE section | |
2861 * | - PT_LOAD sections | |
2862 * +----------------------+ /
2867 * +----------------------+ <-- aligned to target page
2868 * | Process memory dump |
2873 * +----------------------+
2875 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2876 * NT_PRSINFO -> struct elf_prpsinfo
2877 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2879 * Format follows System V format as close as possible. Current
2880 * version limitations are as follows:
2881 * - no floating point registers are dumped
2883 * Function returns 0 in case of success, negative errno otherwise.
2885 * TODO: make this work also during runtime: it should be
2886 * possible to force coredump from running process and then
2887 * continue processing. For example qemu could set up SIGUSR2
2888 * handler (provided that target process haven't registered
2889 * handler for that) that does the dump when signal is received.
2891 static int elf_core_dump(int signr
, const CPUArchState
*env
)
2893 const CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)env
);
2894 const TaskState
*ts
= (const TaskState
*)cpu
->opaque
;
2895 struct vm_area_struct
*vma
= NULL
;
2896 char corefile
[PATH_MAX
];
2897 struct elf_note_info info
;
2899 struct elf_phdr phdr
;
2900 struct rlimit dumpsize
;
2901 struct mm_struct
*mm
= NULL
;
2902 off_t offset
= 0, data_offset
= 0;
2906 init_note_info(&info
);
2909 getrlimit(RLIMIT_CORE
, &dumpsize
);
2910 if (dumpsize
.rlim_cur
== 0)
2913 if (core_dump_filename(ts
, corefile
, sizeof (corefile
)) < 0)
2916 if ((fd
= open(corefile
, O_WRONLY
| O_CREAT
,
2917 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) < 0)
2921 * Walk through target process memory mappings and
2922 * set up structure containing this information. After
2923 * this point vma_xxx functions can be used.
2925 if ((mm
= vma_init()) == NULL
)
2928 walk_memory_regions(mm
, vma_walker
);
2929 segs
= vma_get_mapping_count(mm
);
2932 * Construct valid coredump ELF header. We also
2933 * add one more segment for notes.
2935 fill_elf_header(&elf
, segs
+ 1, ELF_MACHINE
, 0);
2936 if (dump_write(fd
, &elf
, sizeof (elf
)) != 0)
2939 /* fill in in-memory version of notes */
2940 if (fill_note_info(&info
, signr
, env
) < 0)
2943 offset
+= sizeof (elf
); /* elf header */
2944 offset
+= (segs
+ 1) * sizeof (struct elf_phdr
); /* program headers */
2946 /* write out notes program header */
2947 fill_elf_note_phdr(&phdr
, info
.notes_size
, offset
);
2949 offset
+= info
.notes_size
;
2950 if (dump_write(fd
, &phdr
, sizeof (phdr
)) != 0)
2954 * ELF specification wants data to start at page boundary so
2957 data_offset
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
2960 * Write program headers for memory regions mapped in
2961 * the target process.
2963 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
2964 (void) memset(&phdr
, 0, sizeof (phdr
));
2966 phdr
.p_type
= PT_LOAD
;
2967 phdr
.p_offset
= offset
;
2968 phdr
.p_vaddr
= vma
->vma_start
;
2970 phdr
.p_filesz
= vma_dump_size(vma
);
2971 offset
+= phdr
.p_filesz
;
2972 phdr
.p_memsz
= vma
->vma_end
- vma
->vma_start
;
2973 phdr
.p_flags
= vma
->vma_flags
& PROT_READ
? PF_R
: 0;
2974 if (vma
->vma_flags
& PROT_WRITE
)
2975 phdr
.p_flags
|= PF_W
;
2976 if (vma
->vma_flags
& PROT_EXEC
)
2977 phdr
.p_flags
|= PF_X
;
2978 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
2980 bswap_phdr(&phdr
, 1);
2981 dump_write(fd
, &phdr
, sizeof (phdr
));
2985 * Next we write notes just after program headers. No
2986 * alignment needed here.
2988 if (write_note_info(&info
, fd
) < 0)
2991 /* align data to page boundary */
2992 if (lseek(fd
, data_offset
, SEEK_SET
) != data_offset
)
2996 * Finally we can dump process memory into corefile as well.
2998 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
3002 end
= vma
->vma_start
+ vma_dump_size(vma
);
3004 for (addr
= vma
->vma_start
; addr
< end
;
3005 addr
+= TARGET_PAGE_SIZE
) {
3006 char page
[TARGET_PAGE_SIZE
];
3010 * Read in page from target process memory and
3011 * write it to coredump file.
3013 error
= copy_from_user(page
, addr
, sizeof (page
));
3015 (void) fprintf(stderr
, "unable to dump " TARGET_ABI_FMT_lx
"\n",
3020 if (dump_write(fd
, page
, TARGET_PAGE_SIZE
) < 0)
3026 free_note_info(&info
);
3035 #endif /* USE_ELF_CORE_DUMP */
3037 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
3039 init_thread(regs
, infop
);