1 /* This is the Linux kernel elf-loading code, ported into user space */
11 #include <sys/resource.h>
28 #define ELF_OSABI ELFOSABI_SYSV
30 /* from personality.h */
33 * Flags for bug emulation.
35 * These occupy the top three bytes.
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,
54 * These go in the low byte. Avoid using the top bit, it will
55 * conflict with error returns.
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
,
68 PER_SUNOS
= 0x0006 | STICKY_TIMEOUTS
,
69 PER_XENIX
= 0x0007 | STICKY_TIMEOUTS
| SHORT_INODE
,
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 */
76 PER_SOLARIS
= 0x000d | STICKY_TIMEOUTS
,
77 PER_UW7
= 0x000e | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
78 PER_OSF4
= 0x000f, /* OSF/1 v4 */
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 */
90 #define MAP_DENYWRITE 0
93 /* should probably go in elf.h */
98 #ifdef TARGET_WORDS_BIGENDIAN
99 #define ELF_DATA ELFDATA2MSB
101 #define ELF_DATA ELFDATA2LSB
104 typedef target_ulong target_elf_greg_t
;
106 typedef target_ushort target_uid_t
;
107 typedef target_ushort target_gid_t
;
109 typedef target_uint target_uid_t
;
110 typedef target_uint target_gid_t
;
112 typedef target_int target_pid_t
;
116 #define ELF_PLATFORM get_elf_platform()
118 static const char *get_elf_platform(void)
120 static char elf_platform
[] = "i386";
121 int family
= (thread_env
->cpuid_version
>> 8) & 0xff;
125 elf_platform
[1] = '0' + family
;
129 #define ELF_HWCAP get_elf_hwcap()
131 static uint32_t get_elf_hwcap(void)
133 return thread_env
->cpuid_features
;
137 #define ELF_START_MMAP 0x2aaaaab000ULL
138 #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
140 #define ELF_CLASS ELFCLASS64
141 #define ELF_ARCH EM_X86_64
143 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
146 regs
->rsp
= infop
->start_stack
;
147 regs
->rip
= infop
->entry
;
151 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
154 * Note that ELF_NREG should be 29 as there should be place for
155 * TRAPNO and ERR "registers" as well but linux doesn't dump
158 * See linux kernel: arch/x86/include/asm/elf.h
160 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUState
*env
)
162 (*regs
)[0] = env
->regs
[15];
163 (*regs
)[1] = env
->regs
[14];
164 (*regs
)[2] = env
->regs
[13];
165 (*regs
)[3] = env
->regs
[12];
166 (*regs
)[4] = env
->regs
[R_EBP
];
167 (*regs
)[5] = env
->regs
[R_EBX
];
168 (*regs
)[6] = env
->regs
[11];
169 (*regs
)[7] = env
->regs
[10];
170 (*regs
)[8] = env
->regs
[9];
171 (*regs
)[9] = env
->regs
[8];
172 (*regs
)[10] = env
->regs
[R_EAX
];
173 (*regs
)[11] = env
->regs
[R_ECX
];
174 (*regs
)[12] = env
->regs
[R_EDX
];
175 (*regs
)[13] = env
->regs
[R_ESI
];
176 (*regs
)[14] = env
->regs
[R_EDI
];
177 (*regs
)[15] = env
->regs
[R_EAX
]; /* XXX */
178 (*regs
)[16] = env
->eip
;
179 (*regs
)[17] = env
->segs
[R_CS
].selector
& 0xffff;
180 (*regs
)[18] = env
->eflags
;
181 (*regs
)[19] = env
->regs
[R_ESP
];
182 (*regs
)[20] = env
->segs
[R_SS
].selector
& 0xffff;
183 (*regs
)[21] = env
->segs
[R_FS
].selector
& 0xffff;
184 (*regs
)[22] = env
->segs
[R_GS
].selector
& 0xffff;
185 (*regs
)[23] = env
->segs
[R_DS
].selector
& 0xffff;
186 (*regs
)[24] = env
->segs
[R_ES
].selector
& 0xffff;
187 (*regs
)[25] = env
->segs
[R_FS
].selector
& 0xffff;
188 (*regs
)[26] = env
->segs
[R_GS
].selector
& 0xffff;
193 #define ELF_START_MMAP 0x80000000
196 * This is used to ensure we don't load something for the wrong architecture.
198 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
201 * These are used to set parameters in the core dumps.
203 #define ELF_CLASS ELFCLASS32
204 #define ELF_ARCH EM_386
206 static inline void init_thread(struct target_pt_regs
*regs
,
207 struct image_info
*infop
)
209 regs
->esp
= infop
->start_stack
;
210 regs
->eip
= infop
->entry
;
212 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
213 starts %edx contains a pointer to a function which might be
214 registered using `atexit'. This provides a mean for the
215 dynamic linker to call DT_FINI functions for shared libraries
216 that have been loaded before the code runs.
218 A value of 0 tells we have no such handler. */
223 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
226 * Note that ELF_NREG should be 19 as there should be place for
227 * TRAPNO and ERR "registers" as well but linux doesn't dump
230 * See linux kernel: arch/x86/include/asm/elf.h
232 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUState
*env
)
234 (*regs
)[0] = env
->regs
[R_EBX
];
235 (*regs
)[1] = env
->regs
[R_ECX
];
236 (*regs
)[2] = env
->regs
[R_EDX
];
237 (*regs
)[3] = env
->regs
[R_ESI
];
238 (*regs
)[4] = env
->regs
[R_EDI
];
239 (*regs
)[5] = env
->regs
[R_EBP
];
240 (*regs
)[6] = env
->regs
[R_EAX
];
241 (*regs
)[7] = env
->segs
[R_DS
].selector
& 0xffff;
242 (*regs
)[8] = env
->segs
[R_ES
].selector
& 0xffff;
243 (*regs
)[9] = env
->segs
[R_FS
].selector
& 0xffff;
244 (*regs
)[10] = env
->segs
[R_GS
].selector
& 0xffff;
245 (*regs
)[11] = env
->regs
[R_EAX
]; /* XXX */
246 (*regs
)[12] = env
->eip
;
247 (*regs
)[13] = env
->segs
[R_CS
].selector
& 0xffff;
248 (*regs
)[14] = env
->eflags
;
249 (*regs
)[15] = env
->regs
[R_ESP
];
250 (*regs
)[16] = env
->segs
[R_SS
].selector
& 0xffff;
254 #define USE_ELF_CORE_DUMP
255 #define ELF_EXEC_PAGESIZE 4096
261 #define ELF_START_MMAP 0x80000000
263 #define elf_check_arch(x) ( (x) == EM_ARM )
265 #define ELF_CLASS ELFCLASS32
266 #define ELF_ARCH EM_ARM
268 static inline void init_thread(struct target_pt_regs
*regs
,
269 struct image_info
*infop
)
271 abi_long stack
= infop
->start_stack
;
272 memset(regs
, 0, sizeof(*regs
));
273 regs
->ARM_cpsr
= 0x10;
274 if (infop
->entry
& 1)
275 regs
->ARM_cpsr
|= CPSR_T
;
276 regs
->ARM_pc
= infop
->entry
& 0xfffffffe;
277 regs
->ARM_sp
= infop
->start_stack
;
278 /* FIXME - what to for failure of get_user()? */
279 get_user_ual(regs
->ARM_r2
, stack
+ 8); /* envp */
280 get_user_ual(regs
->ARM_r1
, stack
+ 4); /* envp */
281 /* XXX: it seems that r0 is zeroed after ! */
283 /* For uClinux PIC binaries. */
284 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
285 regs
->ARM_r10
= infop
->start_data
;
289 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
291 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUState
*env
)
293 (*regs
)[0] = tswapl(env
->regs
[0]);
294 (*regs
)[1] = tswapl(env
->regs
[1]);
295 (*regs
)[2] = tswapl(env
->regs
[2]);
296 (*regs
)[3] = tswapl(env
->regs
[3]);
297 (*regs
)[4] = tswapl(env
->regs
[4]);
298 (*regs
)[5] = tswapl(env
->regs
[5]);
299 (*regs
)[6] = tswapl(env
->regs
[6]);
300 (*regs
)[7] = tswapl(env
->regs
[7]);
301 (*regs
)[8] = tswapl(env
->regs
[8]);
302 (*regs
)[9] = tswapl(env
->regs
[9]);
303 (*regs
)[10] = tswapl(env
->regs
[10]);
304 (*regs
)[11] = tswapl(env
->regs
[11]);
305 (*regs
)[12] = tswapl(env
->regs
[12]);
306 (*regs
)[13] = tswapl(env
->regs
[13]);
307 (*regs
)[14] = tswapl(env
->regs
[14]);
308 (*regs
)[15] = tswapl(env
->regs
[15]);
310 (*regs
)[16] = tswapl(cpsr_read((CPUState
*)env
));
311 (*regs
)[17] = tswapl(env
->regs
[0]); /* XXX */
314 #define USE_ELF_CORE_DUMP
315 #define ELF_EXEC_PAGESIZE 4096
319 ARM_HWCAP_ARM_SWP
= 1 << 0,
320 ARM_HWCAP_ARM_HALF
= 1 << 1,
321 ARM_HWCAP_ARM_THUMB
= 1 << 2,
322 ARM_HWCAP_ARM_26BIT
= 1 << 3,
323 ARM_HWCAP_ARM_FAST_MULT
= 1 << 4,
324 ARM_HWCAP_ARM_FPA
= 1 << 5,
325 ARM_HWCAP_ARM_VFP
= 1 << 6,
326 ARM_HWCAP_ARM_EDSP
= 1 << 7,
327 ARM_HWCAP_ARM_JAVA
= 1 << 8,
328 ARM_HWCAP_ARM_IWMMXT
= 1 << 9,
329 ARM_HWCAP_ARM_THUMBEE
= 1 << 10,
330 ARM_HWCAP_ARM_NEON
= 1 << 11,
331 ARM_HWCAP_ARM_VFPv3
= 1 << 12,
332 ARM_HWCAP_ARM_VFPv3D16
= 1 << 13,
335 #define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF \
336 | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT \
337 | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP \
338 | ARM_HWCAP_ARM_NEON | ARM_HWCAP_ARM_VFPv3 )
342 #ifdef TARGET_UNICORE32
344 #define ELF_START_MMAP 0x80000000
346 #define elf_check_arch(x) ((x) == EM_UNICORE32)
348 #define ELF_CLASS ELFCLASS32
349 #define ELF_DATA ELFDATA2LSB
350 #define ELF_ARCH EM_UNICORE32
352 static inline void init_thread(struct target_pt_regs
*regs
,
353 struct image_info
*infop
)
355 abi_long stack
= infop
->start_stack
;
356 memset(regs
, 0, sizeof(*regs
));
357 regs
->UC32_REG_asr
= 0x10;
358 regs
->UC32_REG_pc
= infop
->entry
& 0xfffffffe;
359 regs
->UC32_REG_sp
= infop
->start_stack
;
360 /* FIXME - what to for failure of get_user()? */
361 get_user_ual(regs
->UC32_REG_02
, stack
+ 8); /* envp */
362 get_user_ual(regs
->UC32_REG_01
, stack
+ 4); /* envp */
363 /* XXX: it seems that r0 is zeroed after ! */
364 regs
->UC32_REG_00
= 0;
368 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
370 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUState
*env
)
372 (*regs
)[0] = env
->regs
[0];
373 (*regs
)[1] = env
->regs
[1];
374 (*regs
)[2] = env
->regs
[2];
375 (*regs
)[3] = env
->regs
[3];
376 (*regs
)[4] = env
->regs
[4];
377 (*regs
)[5] = env
->regs
[5];
378 (*regs
)[6] = env
->regs
[6];
379 (*regs
)[7] = env
->regs
[7];
380 (*regs
)[8] = env
->regs
[8];
381 (*regs
)[9] = env
->regs
[9];
382 (*regs
)[10] = env
->regs
[10];
383 (*regs
)[11] = env
->regs
[11];
384 (*regs
)[12] = env
->regs
[12];
385 (*regs
)[13] = env
->regs
[13];
386 (*regs
)[14] = env
->regs
[14];
387 (*regs
)[15] = env
->regs
[15];
388 (*regs
)[16] = env
->regs
[16];
389 (*regs
)[17] = env
->regs
[17];
390 (*regs
)[18] = env
->regs
[18];
391 (*regs
)[19] = env
->regs
[19];
392 (*regs
)[20] = env
->regs
[20];
393 (*regs
)[21] = env
->regs
[21];
394 (*regs
)[22] = env
->regs
[22];
395 (*regs
)[23] = env
->regs
[23];
396 (*regs
)[24] = env
->regs
[24];
397 (*regs
)[25] = env
->regs
[25];
398 (*regs
)[26] = env
->regs
[26];
399 (*regs
)[27] = env
->regs
[27];
400 (*regs
)[28] = env
->regs
[28];
401 (*regs
)[29] = env
->regs
[29];
402 (*regs
)[30] = env
->regs
[30];
403 (*regs
)[31] = env
->regs
[31];
405 (*regs
)[32] = cpu_asr_read((CPUState
*)env
);
406 (*regs
)[33] = env
->regs
[0]; /* XXX */
409 #define USE_ELF_CORE_DUMP
410 #define ELF_EXEC_PAGESIZE 4096
412 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
417 #ifdef TARGET_SPARC64
419 #define ELF_START_MMAP 0x80000000
422 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
424 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
427 #define ELF_CLASS ELFCLASS64
428 #define ELF_ARCH EM_SPARCV9
430 #define STACK_BIAS 2047
432 static inline void init_thread(struct target_pt_regs
*regs
,
433 struct image_info
*infop
)
438 regs
->pc
= infop
->entry
;
439 regs
->npc
= regs
->pc
+ 4;
442 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
444 if (personality(infop
->personality
) == PER_LINUX32
)
445 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
447 regs
->u_regs
[14] = infop
->start_stack
- 16 * 8 - STACK_BIAS
;
452 #define ELF_START_MMAP 0x80000000
454 #define elf_check_arch(x) ( (x) == EM_SPARC )
456 #define ELF_CLASS ELFCLASS32
457 #define ELF_ARCH EM_SPARC
459 static inline void init_thread(struct target_pt_regs
*regs
,
460 struct image_info
*infop
)
463 regs
->pc
= infop
->entry
;
464 regs
->npc
= regs
->pc
+ 4;
466 regs
->u_regs
[14] = infop
->start_stack
- 16 * 4;
474 #define ELF_START_MMAP 0x80000000
476 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
478 #define elf_check_arch(x) ( (x) == EM_PPC64 )
480 #define ELF_CLASS ELFCLASS64
484 #define elf_check_arch(x) ( (x) == EM_PPC )
486 #define ELF_CLASS ELFCLASS32
490 #define ELF_ARCH EM_PPC
492 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
493 See arch/powerpc/include/asm/cputable.h. */
495 QEMU_PPC_FEATURE_32
= 0x80000000,
496 QEMU_PPC_FEATURE_64
= 0x40000000,
497 QEMU_PPC_FEATURE_601_INSTR
= 0x20000000,
498 QEMU_PPC_FEATURE_HAS_ALTIVEC
= 0x10000000,
499 QEMU_PPC_FEATURE_HAS_FPU
= 0x08000000,
500 QEMU_PPC_FEATURE_HAS_MMU
= 0x04000000,
501 QEMU_PPC_FEATURE_HAS_4xxMAC
= 0x02000000,
502 QEMU_PPC_FEATURE_UNIFIED_CACHE
= 0x01000000,
503 QEMU_PPC_FEATURE_HAS_SPE
= 0x00800000,
504 QEMU_PPC_FEATURE_HAS_EFP_SINGLE
= 0x00400000,
505 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
= 0x00200000,
506 QEMU_PPC_FEATURE_NO_TB
= 0x00100000,
507 QEMU_PPC_FEATURE_POWER4
= 0x00080000,
508 QEMU_PPC_FEATURE_POWER5
= 0x00040000,
509 QEMU_PPC_FEATURE_POWER5_PLUS
= 0x00020000,
510 QEMU_PPC_FEATURE_CELL
= 0x00010000,
511 QEMU_PPC_FEATURE_BOOKE
= 0x00008000,
512 QEMU_PPC_FEATURE_SMT
= 0x00004000,
513 QEMU_PPC_FEATURE_ICACHE_SNOOP
= 0x00002000,
514 QEMU_PPC_FEATURE_ARCH_2_05
= 0x00001000,
515 QEMU_PPC_FEATURE_PA6T
= 0x00000800,
516 QEMU_PPC_FEATURE_HAS_DFP
= 0x00000400,
517 QEMU_PPC_FEATURE_POWER6_EXT
= 0x00000200,
518 QEMU_PPC_FEATURE_ARCH_2_06
= 0x00000100,
519 QEMU_PPC_FEATURE_HAS_VSX
= 0x00000080,
520 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT
= 0x00000040,
522 QEMU_PPC_FEATURE_TRUE_LE
= 0x00000002,
523 QEMU_PPC_FEATURE_PPC_LE
= 0x00000001,
526 #define ELF_HWCAP get_elf_hwcap()
528 static uint32_t get_elf_hwcap(void)
530 CPUState
*e
= thread_env
;
531 uint32_t features
= 0;
533 /* We don't have to be terribly complete here; the high points are
534 Altivec/FP/SPE support. Anything else is just a bonus. */
535 #define GET_FEATURE(flag, feature) \
536 do {if (e->insns_flags & flag) features |= feature; } while(0)
537 GET_FEATURE(PPC_64B
, QEMU_PPC_FEATURE_64
);
538 GET_FEATURE(PPC_FLOAT
, QEMU_PPC_FEATURE_HAS_FPU
);
539 GET_FEATURE(PPC_ALTIVEC
, QEMU_PPC_FEATURE_HAS_ALTIVEC
);
540 GET_FEATURE(PPC_SPE
, QEMU_PPC_FEATURE_HAS_SPE
);
541 GET_FEATURE(PPC_SPE_SINGLE
, QEMU_PPC_FEATURE_HAS_EFP_SINGLE
);
542 GET_FEATURE(PPC_SPE_DOUBLE
, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
);
543 GET_FEATURE(PPC_BOOKE
, QEMU_PPC_FEATURE_BOOKE
);
544 GET_FEATURE(PPC_405_MAC
, QEMU_PPC_FEATURE_HAS_4xxMAC
);
551 * The requirements here are:
552 * - keep the final alignment of sp (sp & 0xf)
553 * - make sure the 32-bit value at the first 16 byte aligned position of
554 * AUXV is greater than 16 for glibc compatibility.
555 * AT_IGNOREPPC is used for that.
556 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
557 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
559 #define DLINFO_ARCH_ITEMS 5
560 #define ARCH_DLINFO \
562 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
563 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
564 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
566 * Now handle glibc compatibility. \
568 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
569 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
572 static inline void init_thread(struct target_pt_regs
*_regs
, struct image_info
*infop
)
574 _regs
->gpr
[1] = infop
->start_stack
;
575 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
576 _regs
->gpr
[2] = ldq_raw(infop
->entry
+ 8) + infop
->load_addr
;
577 infop
->entry
= ldq_raw(infop
->entry
) + infop
->load_addr
;
579 _regs
->nip
= infop
->entry
;
582 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
584 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
586 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUState
*env
)
589 target_ulong ccr
= 0;
591 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
592 (*regs
)[i
] = tswapl(env
->gpr
[i
]);
595 (*regs
)[32] = tswapl(env
->nip
);
596 (*regs
)[33] = tswapl(env
->msr
);
597 (*regs
)[35] = tswapl(env
->ctr
);
598 (*regs
)[36] = tswapl(env
->lr
);
599 (*regs
)[37] = tswapl(env
->xer
);
601 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
602 ccr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
604 (*regs
)[38] = tswapl(ccr
);
607 #define USE_ELF_CORE_DUMP
608 #define ELF_EXEC_PAGESIZE 4096
614 #define ELF_START_MMAP 0x80000000
616 #define elf_check_arch(x) ( (x) == EM_MIPS )
619 #define ELF_CLASS ELFCLASS64
621 #define ELF_CLASS ELFCLASS32
623 #define ELF_ARCH EM_MIPS
625 static inline void init_thread(struct target_pt_regs
*regs
,
626 struct image_info
*infop
)
628 regs
->cp0_status
= 2 << CP0St_KSU
;
629 regs
->cp0_epc
= infop
->entry
;
630 regs
->regs
[29] = infop
->start_stack
;
633 /* See linux kernel: arch/mips/include/asm/elf.h. */
635 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
637 /* See linux kernel: arch/mips/include/asm/reg.h. */
644 TARGET_EF_R26
= TARGET_EF_R0
+ 26,
645 TARGET_EF_R27
= TARGET_EF_R0
+ 27,
646 TARGET_EF_LO
= TARGET_EF_R0
+ 32,
647 TARGET_EF_HI
= TARGET_EF_R0
+ 33,
648 TARGET_EF_CP0_EPC
= TARGET_EF_R0
+ 34,
649 TARGET_EF_CP0_BADVADDR
= TARGET_EF_R0
+ 35,
650 TARGET_EF_CP0_STATUS
= TARGET_EF_R0
+ 36,
651 TARGET_EF_CP0_CAUSE
= TARGET_EF_R0
+ 37
654 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
655 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUState
*env
)
659 for (i
= 0; i
< TARGET_EF_R0
; i
++) {
662 (*regs
)[TARGET_EF_R0
] = 0;
664 for (i
= 1; i
< ARRAY_SIZE(env
->active_tc
.gpr
); i
++) {
665 (*regs
)[TARGET_EF_R0
+ i
] = tswapl(env
->active_tc
.gpr
[i
]);
668 (*regs
)[TARGET_EF_R26
] = 0;
669 (*regs
)[TARGET_EF_R27
] = 0;
670 (*regs
)[TARGET_EF_LO
] = tswapl(env
->active_tc
.LO
[0]);
671 (*regs
)[TARGET_EF_HI
] = tswapl(env
->active_tc
.HI
[0]);
672 (*regs
)[TARGET_EF_CP0_EPC
] = tswapl(env
->active_tc
.PC
);
673 (*regs
)[TARGET_EF_CP0_BADVADDR
] = tswapl(env
->CP0_BadVAddr
);
674 (*regs
)[TARGET_EF_CP0_STATUS
] = tswapl(env
->CP0_Status
);
675 (*regs
)[TARGET_EF_CP0_CAUSE
] = tswapl(env
->CP0_Cause
);
678 #define USE_ELF_CORE_DUMP
679 #define ELF_EXEC_PAGESIZE 4096
681 #endif /* TARGET_MIPS */
683 #ifdef TARGET_MICROBLAZE
685 #define ELF_START_MMAP 0x80000000
687 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
689 #define ELF_CLASS ELFCLASS32
690 #define ELF_ARCH EM_MICROBLAZE
692 static inline void init_thread(struct target_pt_regs
*regs
,
693 struct image_info
*infop
)
695 regs
->pc
= infop
->entry
;
696 regs
->r1
= infop
->start_stack
;
700 #define ELF_EXEC_PAGESIZE 4096
702 #define USE_ELF_CORE_DUMP
704 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
706 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
707 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUState
*env
)
711 for (i
= 0; i
< 32; i
++) {
712 (*regs
)[pos
++] = tswapl(env
->regs
[i
]);
715 for (i
= 0; i
< 6; i
++) {
716 (*regs
)[pos
++] = tswapl(env
->sregs
[i
]);
720 #endif /* TARGET_MICROBLAZE */
724 #define ELF_START_MMAP 0x80000000
726 #define elf_check_arch(x) ( (x) == EM_SH )
728 #define ELF_CLASS ELFCLASS32
729 #define ELF_ARCH EM_SH
731 static inline void init_thread(struct target_pt_regs
*regs
,
732 struct image_info
*infop
)
734 /* Check other registers XXXXX */
735 regs
->pc
= infop
->entry
;
736 regs
->regs
[15] = infop
->start_stack
;
739 /* See linux kernel: arch/sh/include/asm/elf.h. */
741 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
743 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
749 TARGET_REG_MACH
= 20,
750 TARGET_REG_MACL
= 21,
751 TARGET_REG_SYSCALL
= 22
754 static inline void elf_core_copy_regs(target_elf_gregset_t
*regs
,
759 for (i
= 0; i
< 16; i
++) {
760 (*regs
[i
]) = tswapl(env
->gregs
[i
]);
763 (*regs
)[TARGET_REG_PC
] = tswapl(env
->pc
);
764 (*regs
)[TARGET_REG_PR
] = tswapl(env
->pr
);
765 (*regs
)[TARGET_REG_SR
] = tswapl(env
->sr
);
766 (*regs
)[TARGET_REG_GBR
] = tswapl(env
->gbr
);
767 (*regs
)[TARGET_REG_MACH
] = tswapl(env
->mach
);
768 (*regs
)[TARGET_REG_MACL
] = tswapl(env
->macl
);
769 (*regs
)[TARGET_REG_SYSCALL
] = 0; /* FIXME */
772 #define USE_ELF_CORE_DUMP
773 #define ELF_EXEC_PAGESIZE 4096
779 #define ELF_START_MMAP 0x80000000
781 #define elf_check_arch(x) ( (x) == EM_CRIS )
783 #define ELF_CLASS ELFCLASS32
784 #define ELF_ARCH EM_CRIS
786 static inline void init_thread(struct target_pt_regs
*regs
,
787 struct image_info
*infop
)
789 regs
->erp
= infop
->entry
;
792 #define ELF_EXEC_PAGESIZE 8192
798 #define ELF_START_MMAP 0x80000000
800 #define elf_check_arch(x) ( (x) == EM_68K )
802 #define ELF_CLASS ELFCLASS32
803 #define ELF_ARCH EM_68K
805 /* ??? Does this need to do anything?
806 #define ELF_PLAT_INIT(_r) */
808 static inline void init_thread(struct target_pt_regs
*regs
,
809 struct image_info
*infop
)
811 regs
->usp
= infop
->start_stack
;
813 regs
->pc
= infop
->entry
;
816 /* See linux kernel: arch/m68k/include/asm/elf.h. */
818 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
820 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUState
*env
)
822 (*regs
)[0] = tswapl(env
->dregs
[1]);
823 (*regs
)[1] = tswapl(env
->dregs
[2]);
824 (*regs
)[2] = tswapl(env
->dregs
[3]);
825 (*regs
)[3] = tswapl(env
->dregs
[4]);
826 (*regs
)[4] = tswapl(env
->dregs
[5]);
827 (*regs
)[5] = tswapl(env
->dregs
[6]);
828 (*regs
)[6] = tswapl(env
->dregs
[7]);
829 (*regs
)[7] = tswapl(env
->aregs
[0]);
830 (*regs
)[8] = tswapl(env
->aregs
[1]);
831 (*regs
)[9] = tswapl(env
->aregs
[2]);
832 (*regs
)[10] = tswapl(env
->aregs
[3]);
833 (*regs
)[11] = tswapl(env
->aregs
[4]);
834 (*regs
)[12] = tswapl(env
->aregs
[5]);
835 (*regs
)[13] = tswapl(env
->aregs
[6]);
836 (*regs
)[14] = tswapl(env
->dregs
[0]);
837 (*regs
)[15] = tswapl(env
->aregs
[7]);
838 (*regs
)[16] = tswapl(env
->dregs
[0]); /* FIXME: orig_d0 */
839 (*regs
)[17] = tswapl(env
->sr
);
840 (*regs
)[18] = tswapl(env
->pc
);
841 (*regs
)[19] = 0; /* FIXME: regs->format | regs->vector */
844 #define USE_ELF_CORE_DUMP
845 #define ELF_EXEC_PAGESIZE 8192
851 #define ELF_START_MMAP (0x30000000000ULL)
853 #define elf_check_arch(x) ( (x) == ELF_ARCH )
855 #define ELF_CLASS ELFCLASS64
856 #define ELF_ARCH EM_ALPHA
858 static inline void init_thread(struct target_pt_regs
*regs
,
859 struct image_info
*infop
)
861 regs
->pc
= infop
->entry
;
863 regs
->usp
= infop
->start_stack
;
866 #define ELF_EXEC_PAGESIZE 8192
868 #endif /* TARGET_ALPHA */
872 #define ELF_START_MMAP (0x20000000000ULL)
874 #define elf_check_arch(x) ( (x) == ELF_ARCH )
876 #define ELF_CLASS ELFCLASS64
877 #define ELF_DATA ELFDATA2MSB
878 #define ELF_ARCH EM_S390
880 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
882 regs
->psw
.addr
= infop
->entry
;
883 regs
->psw
.mask
= PSW_MASK_64
| PSW_MASK_32
;
884 regs
->gprs
[15] = infop
->start_stack
;
887 #endif /* TARGET_S390X */
890 #define ELF_PLATFORM (NULL)
899 #define ELF_CLASS ELFCLASS32
901 #define bswaptls(ptr) bswap32s(ptr)
908 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
909 unsigned int a_text
; /* length of text, in bytes */
910 unsigned int a_data
; /* length of data, in bytes */
911 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
912 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
913 unsigned int a_entry
; /* start address */
914 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
915 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
919 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
925 /* Necessary parameters */
926 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
927 #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
928 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
930 #define DLINFO_ITEMS 12
932 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
938 static void bswap_ehdr(struct elfhdr
*ehdr
)
940 bswap16s(&ehdr
->e_type
); /* Object file type */
941 bswap16s(&ehdr
->e_machine
); /* Architecture */
942 bswap32s(&ehdr
->e_version
); /* Object file version */
943 bswaptls(&ehdr
->e_entry
); /* Entry point virtual address */
944 bswaptls(&ehdr
->e_phoff
); /* Program header table file offset */
945 bswaptls(&ehdr
->e_shoff
); /* Section header table file offset */
946 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
947 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
948 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
949 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
950 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
951 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
952 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
955 static void bswap_phdr(struct elf_phdr
*phdr
, int phnum
)
958 for (i
= 0; i
< phnum
; ++i
, ++phdr
) {
959 bswap32s(&phdr
->p_type
); /* Segment type */
960 bswap32s(&phdr
->p_flags
); /* Segment flags */
961 bswaptls(&phdr
->p_offset
); /* Segment file offset */
962 bswaptls(&phdr
->p_vaddr
); /* Segment virtual address */
963 bswaptls(&phdr
->p_paddr
); /* Segment physical address */
964 bswaptls(&phdr
->p_filesz
); /* Segment size in file */
965 bswaptls(&phdr
->p_memsz
); /* Segment size in memory */
966 bswaptls(&phdr
->p_align
); /* Segment alignment */
970 static void bswap_shdr(struct elf_shdr
*shdr
, int shnum
)
973 for (i
= 0; i
< shnum
; ++i
, ++shdr
) {
974 bswap32s(&shdr
->sh_name
);
975 bswap32s(&shdr
->sh_type
);
976 bswaptls(&shdr
->sh_flags
);
977 bswaptls(&shdr
->sh_addr
);
978 bswaptls(&shdr
->sh_offset
);
979 bswaptls(&shdr
->sh_size
);
980 bswap32s(&shdr
->sh_link
);
981 bswap32s(&shdr
->sh_info
);
982 bswaptls(&shdr
->sh_addralign
);
983 bswaptls(&shdr
->sh_entsize
);
987 static void bswap_sym(struct elf_sym
*sym
)
989 bswap32s(&sym
->st_name
);
990 bswaptls(&sym
->st_value
);
991 bswaptls(&sym
->st_size
);
992 bswap16s(&sym
->st_shndx
);
995 static inline void bswap_ehdr(struct elfhdr
*ehdr
) { }
996 static inline void bswap_phdr(struct elf_phdr
*phdr
, int phnum
) { }
997 static inline void bswap_shdr(struct elf_shdr
*shdr
, int shnum
) { }
998 static inline void bswap_sym(struct elf_sym
*sym
) { }
1001 #ifdef USE_ELF_CORE_DUMP
1002 static int elf_core_dump(int, const CPUState
*);
1003 #endif /* USE_ELF_CORE_DUMP */
1004 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
);
1006 /* Verify the portions of EHDR within E_IDENT for the target.
1007 This can be performed before bswapping the entire header. */
1008 static bool elf_check_ident(struct elfhdr
*ehdr
)
1010 return (ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
1011 && ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
1012 && ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
1013 && ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
1014 && ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
1015 && ehdr
->e_ident
[EI_DATA
] == ELF_DATA
1016 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
);
1019 /* Verify the portions of EHDR outside of E_IDENT for the target.
1020 This has to wait until after bswapping the header. */
1021 static bool elf_check_ehdr(struct elfhdr
*ehdr
)
1023 return (elf_check_arch(ehdr
->e_machine
)
1024 && ehdr
->e_ehsize
== sizeof(struct elfhdr
)
1025 && ehdr
->e_phentsize
== sizeof(struct elf_phdr
)
1026 && ehdr
->e_shentsize
== sizeof(struct elf_shdr
)
1027 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
));
1031 * 'copy_elf_strings()' copies argument/envelope strings from user
1032 * memory to free pages in kernel mem. These are in a format ready
1033 * to be put directly into the top of new user memory.
1036 static abi_ulong
copy_elf_strings(int argc
,char ** argv
, void **page
,
1039 char *tmp
, *tmp1
, *pag
= NULL
;
1040 int len
, offset
= 0;
1043 return 0; /* bullet-proofing */
1045 while (argc
-- > 0) {
1048 fprintf(stderr
, "VFS: argc is wrong");
1054 if (p
< len
) { /* this shouldn't happen - 128kB */
1060 offset
= p
% TARGET_PAGE_SIZE
;
1061 pag
= (char *)page
[p
/TARGET_PAGE_SIZE
];
1063 pag
= (char *)malloc(TARGET_PAGE_SIZE
);
1064 memset(pag
, 0, TARGET_PAGE_SIZE
);
1065 page
[p
/TARGET_PAGE_SIZE
] = pag
;
1070 if (len
== 0 || offset
== 0) {
1071 *(pag
+ offset
) = *tmp
;
1074 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
1075 tmp
-= bytes_to_copy
;
1077 offset
-= bytes_to_copy
;
1078 len
-= bytes_to_copy
;
1079 memcpy_fromfs(pag
+ offset
, tmp
, bytes_to_copy
+ 1);
1086 static abi_ulong
setup_arg_pages(abi_ulong p
, struct linux_binprm
*bprm
,
1087 struct image_info
*info
)
1089 abi_ulong stack_base
, size
, error
, guard
;
1092 /* Create enough stack to hold everything. If we don't use
1093 it for args, we'll use it for something else. */
1094 size
= guest_stack_size
;
1095 if (size
< MAX_ARG_PAGES
*TARGET_PAGE_SIZE
) {
1096 size
= MAX_ARG_PAGES
*TARGET_PAGE_SIZE
;
1098 guard
= TARGET_PAGE_SIZE
;
1099 if (guard
< qemu_real_host_page_size
) {
1100 guard
= qemu_real_host_page_size
;
1103 error
= target_mmap(0, size
+ guard
, PROT_READ
| PROT_WRITE
,
1104 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1106 perror("mmap stack");
1110 /* We reserve one extra page at the top of the stack as guard. */
1111 target_mprotect(error
, guard
, PROT_NONE
);
1113 info
->stack_limit
= error
+ guard
;
1114 stack_base
= info
->stack_limit
+ size
- MAX_ARG_PAGES
*TARGET_PAGE_SIZE
;
1117 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1118 if (bprm
->page
[i
]) {
1120 /* FIXME - check return value of memcpy_to_target() for failure */
1121 memcpy_to_target(stack_base
, bprm
->page
[i
], TARGET_PAGE_SIZE
);
1122 free(bprm
->page
[i
]);
1124 stack_base
+= TARGET_PAGE_SIZE
;
1129 /* Map and zero the bss. We need to explicitly zero any fractional pages
1130 after the data section (i.e. bss). */
1131 static void zero_bss(abi_ulong elf_bss
, abi_ulong last_bss
, int prot
)
1133 uintptr_t host_start
, host_map_start
, host_end
;
1135 last_bss
= TARGET_PAGE_ALIGN(last_bss
);
1137 /* ??? There is confusion between qemu_real_host_page_size and
1138 qemu_host_page_size here and elsewhere in target_mmap, which
1139 may lead to the end of the data section mapping from the file
1140 not being mapped. At least there was an explicit test and
1141 comment for that here, suggesting that "the file size must
1142 be known". The comment probably pre-dates the introduction
1143 of the fstat system call in target_mmap which does in fact
1144 find out the size. What isn't clear is if the workaround
1145 here is still actually needed. For now, continue with it,
1146 but merge it with the "normal" mmap that would allocate the bss. */
1148 host_start
= (uintptr_t) g2h(elf_bss
);
1149 host_end
= (uintptr_t) g2h(last_bss
);
1150 host_map_start
= (host_start
+ qemu_real_host_page_size
- 1);
1151 host_map_start
&= -qemu_real_host_page_size
;
1153 if (host_map_start
< host_end
) {
1154 void *p
= mmap((void *)host_map_start
, host_end
- host_map_start
,
1155 prot
, MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1156 if (p
== MAP_FAILED
) {
1157 perror("cannot mmap brk");
1161 /* Since we didn't use target_mmap, make sure to record
1162 the validity of the pages with qemu. */
1163 page_set_flags(elf_bss
& TARGET_PAGE_MASK
, last_bss
, prot
|PAGE_VALID
);
1166 if (host_start
< host_map_start
) {
1167 memset((void *)host_start
, 0, host_map_start
- host_start
);
1171 #ifdef CONFIG_USE_FDPIC
1172 static abi_ulong
loader_build_fdpic_loadmap(struct image_info
*info
, abi_ulong sp
)
1175 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
;
1177 /* elf32_fdpic_loadseg */
1181 put_user_u32(loadsegs
[n
].addr
, sp
+0);
1182 put_user_u32(loadsegs
[n
].p_vaddr
, sp
+4);
1183 put_user_u32(loadsegs
[n
].p_memsz
, sp
+8);
1186 /* elf32_fdpic_loadmap */
1188 put_user_u16(0, sp
+0); /* version */
1189 put_user_u16(info
->nsegs
, sp
+2); /* nsegs */
1191 info
->personality
= PER_LINUX_FDPIC
;
1192 info
->loadmap_addr
= sp
;
1198 static abi_ulong
create_elf_tables(abi_ulong p
, int argc
, int envc
,
1199 struct elfhdr
*exec
,
1200 struct image_info
*info
,
1201 struct image_info
*interp_info
)
1205 abi_ulong u_platform
;
1206 const char *k_platform
;
1207 const int n
= sizeof(elf_addr_t
);
1211 #ifdef CONFIG_USE_FDPIC
1212 /* Needs to be before we load the env/argc/... */
1213 if (elf_is_fdpic(exec
)) {
1214 /* Need 4 byte alignment for these structs */
1216 sp
= loader_build_fdpic_loadmap(info
, sp
);
1217 info
->other_info
= interp_info
;
1219 interp_info
->other_info
= info
;
1220 sp
= loader_build_fdpic_loadmap(interp_info
, sp
);
1226 k_platform
= ELF_PLATFORM
;
1228 size_t len
= strlen(k_platform
) + 1;
1229 sp
-= (len
+ n
- 1) & ~(n
- 1);
1231 /* FIXME - check return value of memcpy_to_target() for failure */
1232 memcpy_to_target(sp
, k_platform
, len
);
1235 * Force 16 byte _final_ alignment here for generality.
1237 sp
= sp
&~ (abi_ulong
)15;
1238 size
= (DLINFO_ITEMS
+ 1) * 2;
1241 #ifdef DLINFO_ARCH_ITEMS
1242 size
+= DLINFO_ARCH_ITEMS
* 2;
1244 size
+= envc
+ argc
+ 2;
1245 size
+= 1; /* argc itself */
1248 sp
-= 16 - (size
& 15);
1250 /* This is correct because Linux defines
1251 * elf_addr_t as Elf32_Off / Elf64_Off
1253 #define NEW_AUX_ENT(id, val) do { \
1254 sp -= n; put_user_ual(val, sp); \
1255 sp -= n; put_user_ual(id, sp); \
1258 NEW_AUX_ENT (AT_NULL
, 0);
1260 /* There must be exactly DLINFO_ITEMS entries here. */
1261 NEW_AUX_ENT(AT_PHDR
, (abi_ulong
)(info
->load_addr
+ exec
->e_phoff
));
1262 NEW_AUX_ENT(AT_PHENT
, (abi_ulong
)(sizeof (struct elf_phdr
)));
1263 NEW_AUX_ENT(AT_PHNUM
, (abi_ulong
)(exec
->e_phnum
));
1264 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(TARGET_PAGE_SIZE
));
1265 NEW_AUX_ENT(AT_BASE
, (abi_ulong
)(interp_info
? interp_info
->load_addr
: 0));
1266 NEW_AUX_ENT(AT_FLAGS
, (abi_ulong
)0);
1267 NEW_AUX_ENT(AT_ENTRY
, info
->entry
);
1268 NEW_AUX_ENT(AT_UID
, (abi_ulong
) getuid());
1269 NEW_AUX_ENT(AT_EUID
, (abi_ulong
) geteuid());
1270 NEW_AUX_ENT(AT_GID
, (abi_ulong
) getgid());
1271 NEW_AUX_ENT(AT_EGID
, (abi_ulong
) getegid());
1272 NEW_AUX_ENT(AT_HWCAP
, (abi_ulong
) ELF_HWCAP
);
1273 NEW_AUX_ENT(AT_CLKTCK
, (abi_ulong
) sysconf(_SC_CLK_TCK
));
1275 NEW_AUX_ENT(AT_PLATFORM
, u_platform
);
1278 * ARCH_DLINFO must come last so platform specific code can enforce
1279 * special alignment requirements on the AUXV if necessary (eg. PPC).
1285 info
->saved_auxv
= sp
;
1287 sp
= loader_build_argptr(envc
, argc
, sp
, p
, 0);
1291 static void probe_guest_base(const char *image_name
,
1292 abi_ulong loaddr
, abi_ulong hiaddr
)
1294 /* Probe for a suitable guest base address, if the user has not set
1295 * it explicitly, and set guest_base appropriately.
1296 * In case of error we will print a suitable message and exit.
1298 #if defined(CONFIG_USE_GUEST_BASE)
1300 if (!have_guest_base
&& !reserved_va
) {
1301 unsigned long host_start
, real_start
, host_size
;
1303 /* Round addresses to page boundaries. */
1304 loaddr
&= qemu_host_page_mask
;
1305 hiaddr
= HOST_PAGE_ALIGN(hiaddr
);
1307 if (loaddr
< mmap_min_addr
) {
1308 host_start
= HOST_PAGE_ALIGN(mmap_min_addr
);
1310 host_start
= loaddr
;
1311 if (host_start
!= loaddr
) {
1312 errmsg
= "Address overflow loading ELF binary";
1316 host_size
= hiaddr
- loaddr
;
1318 /* Do not use mmap_find_vma here because that is limited to the
1319 guest address space. We are going to make the
1320 guest address space fit whatever we're given. */
1321 real_start
= (unsigned long)
1322 mmap((void *)host_start
, host_size
, PROT_NONE
,
1323 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
, -1, 0);
1324 if (real_start
== (unsigned long)-1) {
1327 if (real_start
== host_start
) {
1330 /* That address didn't work. Unmap and try a different one.
1331 The address the host picked because is typically right at
1332 the top of the host address space and leaves the guest with
1333 no usable address space. Resort to a linear search. We
1334 already compensated for mmap_min_addr, so this should not
1335 happen often. Probably means we got unlucky and host
1336 address space randomization put a shared library somewhere
1338 munmap((void *)real_start
, host_size
);
1339 host_start
+= qemu_host_page_size
;
1340 if (host_start
== loaddr
) {
1341 /* Theoretically possible if host doesn't have any suitably
1342 aligned areas. Normally the first mmap will fail. */
1343 errmsg
= "Unable to find space for application";
1347 qemu_log("Relocating guest address space from 0x"
1348 TARGET_ABI_FMT_lx
" to 0x%lx\n",
1349 loaddr
, real_start
);
1350 guest_base
= real_start
- loaddr
;
1355 errmsg
= strerror(errno
);
1357 fprintf(stderr
, "%s: %s\n", image_name
, errmsg
);
1363 /* Load an ELF image into the address space.
1365 IMAGE_NAME is the filename of the image, to use in error messages.
1366 IMAGE_FD is the open file descriptor for the image.
1368 BPRM_BUF is a copy of the beginning of the file; this of course
1369 contains the elf file header at offset 0. It is assumed that this
1370 buffer is sufficiently aligned to present no problems to the host
1371 in accessing data at aligned offsets within the buffer.
1373 On return: INFO values will be filled in, as necessary or available. */
1375 static void load_elf_image(const char *image_name
, int image_fd
,
1376 struct image_info
*info
, char **pinterp_name
,
1377 char bprm_buf
[BPRM_BUF_SIZE
])
1379 struct elfhdr
*ehdr
= (struct elfhdr
*)bprm_buf
;
1380 struct elf_phdr
*phdr
;
1381 abi_ulong load_addr
, load_bias
, loaddr
, hiaddr
, error
;
1385 /* First of all, some simple consistency checks */
1386 errmsg
= "Invalid ELF image for this architecture";
1387 if (!elf_check_ident(ehdr
)) {
1391 if (!elf_check_ehdr(ehdr
)) {
1395 i
= ehdr
->e_phnum
* sizeof(struct elf_phdr
);
1396 if (ehdr
->e_phoff
+ i
<= BPRM_BUF_SIZE
) {
1397 phdr
= (struct elf_phdr
*)(bprm_buf
+ ehdr
->e_phoff
);
1399 phdr
= (struct elf_phdr
*) alloca(i
);
1400 retval
= pread(image_fd
, phdr
, i
, ehdr
->e_phoff
);
1405 bswap_phdr(phdr
, ehdr
->e_phnum
);
1407 #ifdef CONFIG_USE_FDPIC
1409 info
->pt_dynamic_addr
= 0;
1412 /* Find the maximum size of the image and allocate an appropriate
1413 amount of memory to handle that. */
1414 loaddr
= -1, hiaddr
= 0;
1415 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
1416 if (phdr
[i
].p_type
== PT_LOAD
) {
1417 abi_ulong a
= phdr
[i
].p_vaddr
;
1421 a
+= phdr
[i
].p_memsz
;
1425 #ifdef CONFIG_USE_FDPIC
1432 if (ehdr
->e_type
== ET_DYN
) {
1433 /* The image indicates that it can be loaded anywhere. Find a
1434 location that can hold the memory space required. If the
1435 image is pre-linked, LOADDR will be non-zero. Since we do
1436 not supply MAP_FIXED here we'll use that address if and
1437 only if it remains available. */
1438 load_addr
= target_mmap(loaddr
, hiaddr
- loaddr
, PROT_NONE
,
1439 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
,
1441 if (load_addr
== -1) {
1444 } else if (pinterp_name
!= NULL
) {
1445 /* This is the main executable. Make sure that the low
1446 address does not conflict with MMAP_MIN_ADDR or the
1447 QEMU application itself. */
1448 probe_guest_base(image_name
, loaddr
, hiaddr
);
1450 load_bias
= load_addr
- loaddr
;
1452 #ifdef CONFIG_USE_FDPIC
1454 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
=
1455 qemu_malloc(sizeof(*loadsegs
) * info
->nsegs
);
1457 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
1458 switch (phdr
[i
].p_type
) {
1460 info
->pt_dynamic_addr
= phdr
[i
].p_vaddr
+ load_bias
;
1463 loadsegs
->addr
= phdr
[i
].p_vaddr
+ load_bias
;
1464 loadsegs
->p_vaddr
= phdr
[i
].p_vaddr
;
1465 loadsegs
->p_memsz
= phdr
[i
].p_memsz
;
1473 info
->load_bias
= load_bias
;
1474 info
->load_addr
= load_addr
;
1475 info
->entry
= ehdr
->e_entry
+ load_bias
;
1476 info
->start_code
= -1;
1478 info
->start_data
= -1;
1482 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
1483 struct elf_phdr
*eppnt
= phdr
+ i
;
1484 if (eppnt
->p_type
== PT_LOAD
) {
1485 abi_ulong vaddr
, vaddr_po
, vaddr_ps
, vaddr_ef
, vaddr_em
;
1488 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
1489 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
1490 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
1492 vaddr
= load_bias
+ eppnt
->p_vaddr
;
1493 vaddr_po
= TARGET_ELF_PAGEOFFSET(vaddr
);
1494 vaddr_ps
= TARGET_ELF_PAGESTART(vaddr
);
1496 error
= target_mmap(vaddr_ps
, eppnt
->p_filesz
+ vaddr_po
,
1497 elf_prot
, MAP_PRIVATE
| MAP_FIXED
,
1498 image_fd
, eppnt
->p_offset
- vaddr_po
);
1503 vaddr_ef
= vaddr
+ eppnt
->p_filesz
;
1504 vaddr_em
= vaddr
+ eppnt
->p_memsz
;
1506 /* If the load segment requests extra zeros (e.g. bss), map it. */
1507 if (vaddr_ef
< vaddr_em
) {
1508 zero_bss(vaddr_ef
, vaddr_em
, elf_prot
);
1511 /* Find the full program boundaries. */
1512 if (elf_prot
& PROT_EXEC
) {
1513 if (vaddr
< info
->start_code
) {
1514 info
->start_code
= vaddr
;
1516 if (vaddr_ef
> info
->end_code
) {
1517 info
->end_code
= vaddr_ef
;
1520 if (elf_prot
& PROT_WRITE
) {
1521 if (vaddr
< info
->start_data
) {
1522 info
->start_data
= vaddr
;
1524 if (vaddr_ef
> info
->end_data
) {
1525 info
->end_data
= vaddr_ef
;
1527 if (vaddr_em
> info
->brk
) {
1528 info
->brk
= vaddr_em
;
1531 } else if (eppnt
->p_type
== PT_INTERP
&& pinterp_name
) {
1534 if (*pinterp_name
) {
1535 errmsg
= "Multiple PT_INTERP entries";
1538 interp_name
= malloc(eppnt
->p_filesz
);
1543 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
1544 memcpy(interp_name
, bprm_buf
+ eppnt
->p_offset
,
1547 retval
= pread(image_fd
, interp_name
, eppnt
->p_filesz
,
1549 if (retval
!= eppnt
->p_filesz
) {
1553 if (interp_name
[eppnt
->p_filesz
- 1] != 0) {
1554 errmsg
= "Invalid PT_INTERP entry";
1557 *pinterp_name
= interp_name
;
1561 if (info
->end_data
== 0) {
1562 info
->start_data
= info
->end_code
;
1563 info
->end_data
= info
->end_code
;
1564 info
->brk
= info
->end_code
;
1567 if (qemu_log_enabled()) {
1568 load_symbols(ehdr
, image_fd
, load_bias
);
1576 errmsg
= "Incomplete read of file header";
1580 errmsg
= strerror(errno
);
1582 fprintf(stderr
, "%s: %s\n", image_name
, errmsg
);
1586 static void load_elf_interp(const char *filename
, struct image_info
*info
,
1587 char bprm_buf
[BPRM_BUF_SIZE
])
1591 fd
= open(path(filename
), O_RDONLY
);
1596 retval
= read(fd
, bprm_buf
, BPRM_BUF_SIZE
);
1600 if (retval
< BPRM_BUF_SIZE
) {
1601 memset(bprm_buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
1604 load_elf_image(filename
, fd
, info
, NULL
, bprm_buf
);
1608 fprintf(stderr
, "%s: %s\n", filename
, strerror(errno
));
1612 static int symfind(const void *s0
, const void *s1
)
1614 struct elf_sym
*key
= (struct elf_sym
*)s0
;
1615 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
1617 if (key
->st_value
< sym
->st_value
) {
1619 } else if (key
->st_value
>= sym
->st_value
+ sym
->st_size
) {
1625 static const char *lookup_symbolxx(struct syminfo
*s
, target_ulong orig_addr
)
1627 #if ELF_CLASS == ELFCLASS32
1628 struct elf_sym
*syms
= s
->disas_symtab
.elf32
;
1630 struct elf_sym
*syms
= s
->disas_symtab
.elf64
;
1635 struct elf_sym
*sym
;
1637 key
.st_value
= orig_addr
;
1639 sym
= bsearch(&key
, syms
, s
->disas_num_syms
, sizeof(*syms
), symfind
);
1641 return s
->disas_strtab
+ sym
->st_name
;
1647 /* FIXME: This should use elf_ops.h */
1648 static int symcmp(const void *s0
, const void *s1
)
1650 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
1651 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
1652 return (sym0
->st_value
< sym1
->st_value
)
1654 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
1657 /* Best attempt to load symbols from this ELF object. */
1658 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
)
1660 int i
, shnum
, nsyms
, sym_idx
= 0, str_idx
= 0;
1661 struct elf_shdr
*shdr
;
1662 char *strings
= NULL
;
1663 struct syminfo
*s
= NULL
;
1664 struct elf_sym
*new_syms
, *syms
= NULL
;
1666 shnum
= hdr
->e_shnum
;
1667 i
= shnum
* sizeof(struct elf_shdr
);
1668 shdr
= (struct elf_shdr
*)alloca(i
);
1669 if (pread(fd
, shdr
, i
, hdr
->e_shoff
) != i
) {
1673 bswap_shdr(shdr
, shnum
);
1674 for (i
= 0; i
< shnum
; ++i
) {
1675 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
1677 str_idx
= shdr
[i
].sh_link
;
1682 /* There will be no symbol table if the file was stripped. */
1686 /* Now know where the strtab and symtab are. Snarf them. */
1687 s
= malloc(sizeof(*s
));
1692 i
= shdr
[str_idx
].sh_size
;
1693 s
->disas_strtab
= strings
= malloc(i
);
1694 if (!strings
|| pread(fd
, strings
, i
, shdr
[str_idx
].sh_offset
) != i
) {
1698 i
= shdr
[sym_idx
].sh_size
;
1700 if (!syms
|| pread(fd
, syms
, i
, shdr
[sym_idx
].sh_offset
) != i
) {
1704 nsyms
= i
/ sizeof(struct elf_sym
);
1705 for (i
= 0; i
< nsyms
; ) {
1706 bswap_sym(syms
+ i
);
1707 /* Throw away entries which we do not need. */
1708 if (syms
[i
].st_shndx
== SHN_UNDEF
1709 || syms
[i
].st_shndx
>= SHN_LORESERVE
1710 || ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
1712 syms
[i
] = syms
[nsyms
];
1715 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
1716 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
1717 syms
[i
].st_value
&= ~(target_ulong
)1;
1719 syms
[i
].st_value
+= load_bias
;
1724 /* No "useful" symbol. */
1729 /* Attempt to free the storage associated with the local symbols
1730 that we threw away. Whether or not this has any effect on the
1731 memory allocation depends on the malloc implementation and how
1732 many symbols we managed to discard. */
1733 new_syms
= realloc(syms
, nsyms
* sizeof(*syms
));
1734 if (new_syms
== NULL
) {
1739 qsort(syms
, nsyms
, sizeof(*syms
), symcmp
);
1741 s
->disas_num_syms
= nsyms
;
1742 #if ELF_CLASS == ELFCLASS32
1743 s
->disas_symtab
.elf32
= syms
;
1745 s
->disas_symtab
.elf64
= syms
;
1747 s
->lookup_symbol
= lookup_symbolxx
;
1759 int load_elf_binary(struct linux_binprm
* bprm
, struct target_pt_regs
* regs
,
1760 struct image_info
* info
)
1762 struct image_info interp_info
;
1763 struct elfhdr elf_ex
;
1764 char *elf_interpreter
= NULL
;
1766 info
->start_mmap
= (abi_ulong
)ELF_START_MMAP
;
1770 load_elf_image(bprm
->filename
, bprm
->fd
, info
,
1771 &elf_interpreter
, bprm
->buf
);
1773 /* ??? We need a copy of the elf header for passing to create_elf_tables.
1774 If we do nothing, we'll have overwritten this when we re-use bprm->buf
1775 when we load the interpreter. */
1776 elf_ex
= *(struct elfhdr
*)bprm
->buf
;
1778 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, bprm
->page
, bprm
->p
);
1779 bprm
->p
= copy_elf_strings(bprm
->envc
,bprm
->envp
,bprm
->page
,bprm
->p
);
1780 bprm
->p
= copy_elf_strings(bprm
->argc
,bprm
->argv
,bprm
->page
,bprm
->p
);
1782 fprintf(stderr
, "%s: %s\n", bprm
->filename
, strerror(E2BIG
));
1786 /* Do this so that we can load the interpreter, if need be. We will
1787 change some of these later */
1788 bprm
->p
= setup_arg_pages(bprm
->p
, bprm
, info
);
1790 if (elf_interpreter
) {
1791 load_elf_interp(elf_interpreter
, &interp_info
, bprm
->buf
);
1793 /* If the program interpreter is one of these two, then assume
1794 an iBCS2 image. Otherwise assume a native linux image. */
1796 if (strcmp(elf_interpreter
, "/usr/lib/libc.so.1") == 0
1797 || strcmp(elf_interpreter
, "/usr/lib/ld.so.1") == 0) {
1798 info
->personality
= PER_SVR4
;
1800 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1801 and some applications "depend" upon this behavior. Since
1802 we do not have the power to recompile these, we emulate
1803 the SVr4 behavior. Sigh. */
1804 target_mmap(0, qemu_host_page_size
, PROT_READ
| PROT_EXEC
,
1805 MAP_FIXED
| MAP_PRIVATE
, -1, 0);
1809 bprm
->p
= create_elf_tables(bprm
->p
, bprm
->argc
, bprm
->envc
, &elf_ex
,
1810 info
, (elf_interpreter
? &interp_info
: NULL
));
1811 info
->start_stack
= bprm
->p
;
1813 /* If we have an interpreter, set that as the program's entry point.
1814 Copy the load_addr as well, to help PPC64 interpret the entry
1815 point as a function descriptor. Do this after creating elf tables
1816 so that we copy the original program entry point into the AUXV. */
1817 if (elf_interpreter
) {
1818 info
->load_addr
= interp_info
.load_addr
;
1819 info
->entry
= interp_info
.entry
;
1820 free(elf_interpreter
);
1823 #ifdef USE_ELF_CORE_DUMP
1824 bprm
->core_dump
= &elf_core_dump
;
1830 #ifdef USE_ELF_CORE_DUMP
1832 * Definitions to generate Intel SVR4-like core files.
1833 * These mostly have the same names as the SVR4 types with "target_elf_"
1834 * tacked on the front to prevent clashes with linux definitions,
1835 * and the typedef forms have been avoided. This is mostly like
1836 * the SVR4 structure, but more Linuxy, with things that Linux does
1837 * not support and which gdb doesn't really use excluded.
1839 * Fields we don't dump (their contents is zero) in linux-user qemu
1840 * are marked with XXX.
1842 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
1844 * Porting ELF coredump for target is (quite) simple process. First you
1845 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
1846 * the target resides):
1848 * #define USE_ELF_CORE_DUMP
1850 * Next you define type of register set used for dumping. ELF specification
1851 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
1853 * typedef <target_regtype> target_elf_greg_t;
1854 * #define ELF_NREG <number of registers>
1855 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
1857 * Last step is to implement target specific function that copies registers
1858 * from given cpu into just specified register set. Prototype is:
1860 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
1861 * const CPUState *env);
1864 * regs - copy register values into here (allocated and zeroed by caller)
1865 * env - copy registers from here
1867 * Example for ARM target is provided in this file.
1870 /* An ELF note in memory */
1874 size_t namesz_rounded
;
1877 size_t datasz_rounded
;
1882 struct target_elf_siginfo
{
1883 target_int si_signo
; /* signal number */
1884 target_int si_code
; /* extra code */
1885 target_int si_errno
; /* errno */
1888 struct target_elf_prstatus
{
1889 struct target_elf_siginfo pr_info
; /* Info associated with signal */
1890 target_short pr_cursig
; /* Current signal */
1891 target_ulong pr_sigpend
; /* XXX */
1892 target_ulong pr_sighold
; /* XXX */
1893 target_pid_t pr_pid
;
1894 target_pid_t pr_ppid
;
1895 target_pid_t pr_pgrp
;
1896 target_pid_t pr_sid
;
1897 struct target_timeval pr_utime
; /* XXX User time */
1898 struct target_timeval pr_stime
; /* XXX System time */
1899 struct target_timeval pr_cutime
; /* XXX Cumulative user time */
1900 struct target_timeval pr_cstime
; /* XXX Cumulative system time */
1901 target_elf_gregset_t pr_reg
; /* GP registers */
1902 target_int pr_fpvalid
; /* XXX */
1905 #define ELF_PRARGSZ (80) /* Number of chars for args */
1907 struct target_elf_prpsinfo
{
1908 char pr_state
; /* numeric process state */
1909 char pr_sname
; /* char for pr_state */
1910 char pr_zomb
; /* zombie */
1911 char pr_nice
; /* nice val */
1912 target_ulong pr_flag
; /* flags */
1913 target_uid_t pr_uid
;
1914 target_gid_t pr_gid
;
1915 target_pid_t pr_pid
, pr_ppid
, pr_pgrp
, pr_sid
;
1917 char pr_fname
[16]; /* filename of executable */
1918 char pr_psargs
[ELF_PRARGSZ
]; /* initial part of arg list */
1921 /* Here is the structure in which status of each thread is captured. */
1922 struct elf_thread_status
{
1923 QTAILQ_ENTRY(elf_thread_status
) ets_link
;
1924 struct target_elf_prstatus prstatus
; /* NT_PRSTATUS */
1926 elf_fpregset_t fpu
; /* NT_PRFPREG */
1927 struct task_struct
*thread
;
1928 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
1930 struct memelfnote notes
[1];
1934 struct elf_note_info
{
1935 struct memelfnote
*notes
;
1936 struct target_elf_prstatus
*prstatus
; /* NT_PRSTATUS */
1937 struct target_elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
1939 QTAILQ_HEAD(thread_list_head
, elf_thread_status
) thread_list
;
1942 * Current version of ELF coredump doesn't support
1943 * dumping fp regs etc.
1945 elf_fpregset_t
*fpu
;
1946 elf_fpxregset_t
*xfpu
;
1947 int thread_status_size
;
1953 struct vm_area_struct
{
1954 abi_ulong vma_start
; /* start vaddr of memory region */
1955 abi_ulong vma_end
; /* end vaddr of memory region */
1956 abi_ulong vma_flags
; /* protection etc. flags for the region */
1957 QTAILQ_ENTRY(vm_area_struct
) vma_link
;
1961 QTAILQ_HEAD(, vm_area_struct
) mm_mmap
;
1962 int mm_count
; /* number of mappings */
1965 static struct mm_struct
*vma_init(void);
1966 static void vma_delete(struct mm_struct
*);
1967 static int vma_add_mapping(struct mm_struct
*, abi_ulong
,
1968 abi_ulong
, abi_ulong
);
1969 static int vma_get_mapping_count(const struct mm_struct
*);
1970 static struct vm_area_struct
*vma_first(const struct mm_struct
*);
1971 static struct vm_area_struct
*vma_next(struct vm_area_struct
*);
1972 static abi_ulong
vma_dump_size(const struct vm_area_struct
*);
1973 static int vma_walker(void *priv
, abi_ulong start
, abi_ulong end
,
1974 unsigned long flags
);
1976 static void fill_elf_header(struct elfhdr
*, int, uint16_t, uint32_t);
1977 static void fill_note(struct memelfnote
*, const char *, int,
1978 unsigned int, void *);
1979 static void fill_prstatus(struct target_elf_prstatus
*, const TaskState
*, int);
1980 static int fill_psinfo(struct target_elf_prpsinfo
*, const TaskState
*);
1981 static void fill_auxv_note(struct memelfnote
*, const TaskState
*);
1982 static void fill_elf_note_phdr(struct elf_phdr
*, int, off_t
);
1983 static size_t note_size(const struct memelfnote
*);
1984 static void free_note_info(struct elf_note_info
*);
1985 static int fill_note_info(struct elf_note_info
*, long, const CPUState
*);
1986 static void fill_thread_info(struct elf_note_info
*, const CPUState
*);
1987 static int core_dump_filename(const TaskState
*, char *, size_t);
1989 static int dump_write(int, const void *, size_t);
1990 static int write_note(struct memelfnote
*, int);
1991 static int write_note_info(struct elf_note_info
*, int);
1994 static void bswap_prstatus(struct target_elf_prstatus
*prstatus
)
1996 prstatus
->pr_info
.si_signo
= tswapl(prstatus
->pr_info
.si_signo
);
1997 prstatus
->pr_info
.si_code
= tswapl(prstatus
->pr_info
.si_code
);
1998 prstatus
->pr_info
.si_errno
= tswapl(prstatus
->pr_info
.si_errno
);
1999 prstatus
->pr_cursig
= tswap16(prstatus
->pr_cursig
);
2000 prstatus
->pr_sigpend
= tswapl(prstatus
->pr_sigpend
);
2001 prstatus
->pr_sighold
= tswapl(prstatus
->pr_sighold
);
2002 prstatus
->pr_pid
= tswap32(prstatus
->pr_pid
);
2003 prstatus
->pr_ppid
= tswap32(prstatus
->pr_ppid
);
2004 prstatus
->pr_pgrp
= tswap32(prstatus
->pr_pgrp
);
2005 prstatus
->pr_sid
= tswap32(prstatus
->pr_sid
);
2006 /* cpu times are not filled, so we skip them */
2007 /* regs should be in correct format already */
2008 prstatus
->pr_fpvalid
= tswap32(prstatus
->pr_fpvalid
);
2011 static void bswap_psinfo(struct target_elf_prpsinfo
*psinfo
)
2013 psinfo
->pr_flag
= tswapl(psinfo
->pr_flag
);
2014 psinfo
->pr_uid
= tswap16(psinfo
->pr_uid
);
2015 psinfo
->pr_gid
= tswap16(psinfo
->pr_gid
);
2016 psinfo
->pr_pid
= tswap32(psinfo
->pr_pid
);
2017 psinfo
->pr_ppid
= tswap32(psinfo
->pr_ppid
);
2018 psinfo
->pr_pgrp
= tswap32(psinfo
->pr_pgrp
);
2019 psinfo
->pr_sid
= tswap32(psinfo
->pr_sid
);
2022 static void bswap_note(struct elf_note
*en
)
2024 bswap32s(&en
->n_namesz
);
2025 bswap32s(&en
->n_descsz
);
2026 bswap32s(&en
->n_type
);
2029 static inline void bswap_prstatus(struct target_elf_prstatus
*p
) { }
2030 static inline void bswap_psinfo(struct target_elf_prpsinfo
*p
) {}
2031 static inline void bswap_note(struct elf_note
*en
) { }
2032 #endif /* BSWAP_NEEDED */
2035 * Minimal support for linux memory regions. These are needed
2036 * when we are finding out what memory exactly belongs to
2037 * emulated process. No locks needed here, as long as
2038 * thread that received the signal is stopped.
2041 static struct mm_struct
*vma_init(void)
2043 struct mm_struct
*mm
;
2045 if ((mm
= qemu_malloc(sizeof (*mm
))) == NULL
)
2049 QTAILQ_INIT(&mm
->mm_mmap
);
2054 static void vma_delete(struct mm_struct
*mm
)
2056 struct vm_area_struct
*vma
;
2058 while ((vma
= vma_first(mm
)) != NULL
) {
2059 QTAILQ_REMOVE(&mm
->mm_mmap
, vma
, vma_link
);
2065 static int vma_add_mapping(struct mm_struct
*mm
, abi_ulong start
,
2066 abi_ulong end
, abi_ulong flags
)
2068 struct vm_area_struct
*vma
;
2070 if ((vma
= qemu_mallocz(sizeof (*vma
))) == NULL
)
2073 vma
->vma_start
= start
;
2075 vma
->vma_flags
= flags
;
2077 QTAILQ_INSERT_TAIL(&mm
->mm_mmap
, vma
, vma_link
);
2083 static struct vm_area_struct
*vma_first(const struct mm_struct
*mm
)
2085 return (QTAILQ_FIRST(&mm
->mm_mmap
));
2088 static struct vm_area_struct
*vma_next(struct vm_area_struct
*vma
)
2090 return (QTAILQ_NEXT(vma
, vma_link
));
2093 static int vma_get_mapping_count(const struct mm_struct
*mm
)
2095 return (mm
->mm_count
);
2099 * Calculate file (dump) size of given memory region.
2101 static abi_ulong
vma_dump_size(const struct vm_area_struct
*vma
)
2103 /* if we cannot even read the first page, skip it */
2104 if (!access_ok(VERIFY_READ
, vma
->vma_start
, TARGET_PAGE_SIZE
))
2108 * Usually we don't dump executable pages as they contain
2109 * non-writable code that debugger can read directly from
2110 * target library etc. However, thread stacks are marked
2111 * also executable so we read in first page of given region
2112 * and check whether it contains elf header. If there is
2113 * no elf header, we dump it.
2115 if (vma
->vma_flags
& PROT_EXEC
) {
2116 char page
[TARGET_PAGE_SIZE
];
2118 copy_from_user(page
, vma
->vma_start
, sizeof (page
));
2119 if ((page
[EI_MAG0
] == ELFMAG0
) &&
2120 (page
[EI_MAG1
] == ELFMAG1
) &&
2121 (page
[EI_MAG2
] == ELFMAG2
) &&
2122 (page
[EI_MAG3
] == ELFMAG3
)) {
2124 * Mappings are possibly from ELF binary. Don't dump
2131 return (vma
->vma_end
- vma
->vma_start
);
2134 static int vma_walker(void *priv
, abi_ulong start
, abi_ulong end
,
2135 unsigned long flags
)
2137 struct mm_struct
*mm
= (struct mm_struct
*)priv
;
2139 vma_add_mapping(mm
, start
, end
, flags
);
2143 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
2144 unsigned int sz
, void *data
)
2146 unsigned int namesz
;
2148 namesz
= strlen(name
) + 1;
2150 note
->namesz
= namesz
;
2151 note
->namesz_rounded
= roundup(namesz
, sizeof (int32_t));
2154 note
->datasz_rounded
= roundup(sz
, sizeof (int32_t));
2159 * We calculate rounded up note size here as specified by
2162 note
->notesz
= sizeof (struct elf_note
) +
2163 note
->namesz_rounded
+ note
->datasz_rounded
;
2166 static void fill_elf_header(struct elfhdr
*elf
, int segs
, uint16_t machine
,
2169 (void) memset(elf
, 0, sizeof(*elf
));
2171 (void) memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
2172 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
2173 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
2174 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
2175 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
2177 elf
->e_type
= ET_CORE
;
2178 elf
->e_machine
= machine
;
2179 elf
->e_version
= EV_CURRENT
;
2180 elf
->e_phoff
= sizeof(struct elfhdr
);
2181 elf
->e_flags
= flags
;
2182 elf
->e_ehsize
= sizeof(struct elfhdr
);
2183 elf
->e_phentsize
= sizeof(struct elf_phdr
);
2184 elf
->e_phnum
= segs
;
2189 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
2191 phdr
->p_type
= PT_NOTE
;
2192 phdr
->p_offset
= offset
;
2195 phdr
->p_filesz
= sz
;
2200 bswap_phdr(phdr
, 1);
2203 static size_t note_size(const struct memelfnote
*note
)
2205 return (note
->notesz
);
2208 static void fill_prstatus(struct target_elf_prstatus
*prstatus
,
2209 const TaskState
*ts
, int signr
)
2211 (void) memset(prstatus
, 0, sizeof (*prstatus
));
2212 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
2213 prstatus
->pr_pid
= ts
->ts_tid
;
2214 prstatus
->pr_ppid
= getppid();
2215 prstatus
->pr_pgrp
= getpgrp();
2216 prstatus
->pr_sid
= getsid(0);
2218 bswap_prstatus(prstatus
);
2221 static int fill_psinfo(struct target_elf_prpsinfo
*psinfo
, const TaskState
*ts
)
2223 char *filename
, *base_filename
;
2224 unsigned int i
, len
;
2226 (void) memset(psinfo
, 0, sizeof (*psinfo
));
2228 len
= ts
->info
->arg_end
- ts
->info
->arg_start
;
2229 if (len
>= ELF_PRARGSZ
)
2230 len
= ELF_PRARGSZ
- 1;
2231 if (copy_from_user(&psinfo
->pr_psargs
, ts
->info
->arg_start
, len
))
2233 for (i
= 0; i
< len
; i
++)
2234 if (psinfo
->pr_psargs
[i
] == 0)
2235 psinfo
->pr_psargs
[i
] = ' ';
2236 psinfo
->pr_psargs
[len
] = 0;
2238 psinfo
->pr_pid
= getpid();
2239 psinfo
->pr_ppid
= getppid();
2240 psinfo
->pr_pgrp
= getpgrp();
2241 psinfo
->pr_sid
= getsid(0);
2242 psinfo
->pr_uid
= getuid();
2243 psinfo
->pr_gid
= getgid();
2245 filename
= strdup(ts
->bprm
->filename
);
2246 base_filename
= strdup(basename(filename
));
2247 (void) strncpy(psinfo
->pr_fname
, base_filename
,
2248 sizeof(psinfo
->pr_fname
));
2249 free(base_filename
);
2252 bswap_psinfo(psinfo
);
2256 static void fill_auxv_note(struct memelfnote
*note
, const TaskState
*ts
)
2258 elf_addr_t auxv
= (elf_addr_t
)ts
->info
->saved_auxv
;
2259 elf_addr_t orig_auxv
= auxv
;
2265 * Auxiliary vector is stored in target process stack. It contains
2266 * {type, value} pairs that we need to dump into note. This is not
2267 * strictly necessary but we do it here for sake of completeness.
2270 /* find out lenght of the vector, AT_NULL is terminator */
2273 get_user_ual(val
, auxv
);
2275 auxv
+= 2 * sizeof (elf_addr_t
);
2276 } while (val
!= AT_NULL
);
2277 len
= i
* sizeof (elf_addr_t
);
2279 /* read in whole auxv vector and copy it to memelfnote */
2280 ptr
= lock_user(VERIFY_READ
, orig_auxv
, len
, 0);
2282 fill_note(note
, "CORE", NT_AUXV
, len
, ptr
);
2283 unlock_user(ptr
, auxv
, len
);
2288 * Constructs name of coredump file. We have following convention
2290 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2292 * Returns 0 in case of success, -1 otherwise (errno is set).
2294 static int core_dump_filename(const TaskState
*ts
, char *buf
,
2298 char *filename
= NULL
;
2299 char *base_filename
= NULL
;
2303 assert(bufsize
>= PATH_MAX
);
2305 if (gettimeofday(&tv
, NULL
) < 0) {
2306 (void) fprintf(stderr
, "unable to get current timestamp: %s",
2311 filename
= strdup(ts
->bprm
->filename
);
2312 base_filename
= strdup(basename(filename
));
2313 (void) strftime(timestamp
, sizeof (timestamp
), "%Y%m%d-%H%M%S",
2314 localtime_r(&tv
.tv_sec
, &tm
));
2315 (void) snprintf(buf
, bufsize
, "qemu_%s_%s_%d.core",
2316 base_filename
, timestamp
, (int)getpid());
2317 free(base_filename
);
2323 static int dump_write(int fd
, const void *ptr
, size_t size
)
2325 const char *bufp
= (const char *)ptr
;
2326 ssize_t bytes_written
, bytes_left
;
2327 struct rlimit dumpsize
;
2331 getrlimit(RLIMIT_CORE
, &dumpsize
);
2332 if ((pos
= lseek(fd
, 0, SEEK_CUR
))==-1) {
2333 if (errno
== ESPIPE
) { /* not a seekable stream */
2339 if (dumpsize
.rlim_cur
<= pos
) {
2341 } else if (dumpsize
.rlim_cur
== RLIM_INFINITY
) {
2344 size_t limit_left
=dumpsize
.rlim_cur
- pos
;
2345 bytes_left
= limit_left
>= size
? size
: limit_left
;
2350 * In normal conditions, single write(2) should do but
2351 * in case of socket etc. this mechanism is more portable.
2354 bytes_written
= write(fd
, bufp
, bytes_left
);
2355 if (bytes_written
< 0) {
2359 } else if (bytes_written
== 0) { /* eof */
2362 bufp
+= bytes_written
;
2363 bytes_left
-= bytes_written
;
2364 } while (bytes_left
> 0);
2369 static int write_note(struct memelfnote
*men
, int fd
)
2373 en
.n_namesz
= men
->namesz
;
2374 en
.n_type
= men
->type
;
2375 en
.n_descsz
= men
->datasz
;
2379 if (dump_write(fd
, &en
, sizeof(en
)) != 0)
2381 if (dump_write(fd
, men
->name
, men
->namesz_rounded
) != 0)
2383 if (dump_write(fd
, men
->data
, men
->datasz_rounded
) != 0)
2389 static void fill_thread_info(struct elf_note_info
*info
, const CPUState
*env
)
2391 TaskState
*ts
= (TaskState
*)env
->opaque
;
2392 struct elf_thread_status
*ets
;
2394 ets
= qemu_mallocz(sizeof (*ets
));
2395 ets
->num_notes
= 1; /* only prstatus is dumped */
2396 fill_prstatus(&ets
->prstatus
, ts
, 0);
2397 elf_core_copy_regs(&ets
->prstatus
.pr_reg
, env
);
2398 fill_note(&ets
->notes
[0], "CORE", NT_PRSTATUS
, sizeof (ets
->prstatus
),
2401 QTAILQ_INSERT_TAIL(&info
->thread_list
, ets
, ets_link
);
2403 info
->notes_size
+= note_size(&ets
->notes
[0]);
2406 static int fill_note_info(struct elf_note_info
*info
,
2407 long signr
, const CPUState
*env
)
2410 CPUState
*cpu
= NULL
;
2411 TaskState
*ts
= (TaskState
*)env
->opaque
;
2414 (void) memset(info
, 0, sizeof (*info
));
2416 QTAILQ_INIT(&info
->thread_list
);
2418 info
->notes
= qemu_mallocz(NUMNOTES
* sizeof (struct memelfnote
));
2419 if (info
->notes
== NULL
)
2421 info
->prstatus
= qemu_mallocz(sizeof (*info
->prstatus
));
2422 if (info
->prstatus
== NULL
)
2424 info
->psinfo
= qemu_mallocz(sizeof (*info
->psinfo
));
2425 if (info
->prstatus
== NULL
)
2429 * First fill in status (and registers) of current thread
2430 * including process info & aux vector.
2432 fill_prstatus(info
->prstatus
, ts
, signr
);
2433 elf_core_copy_regs(&info
->prstatus
->pr_reg
, env
);
2434 fill_note(&info
->notes
[0], "CORE", NT_PRSTATUS
,
2435 sizeof (*info
->prstatus
), info
->prstatus
);
2436 fill_psinfo(info
->psinfo
, ts
);
2437 fill_note(&info
->notes
[1], "CORE", NT_PRPSINFO
,
2438 sizeof (*info
->psinfo
), info
->psinfo
);
2439 fill_auxv_note(&info
->notes
[2], ts
);
2442 info
->notes_size
= 0;
2443 for (i
= 0; i
< info
->numnote
; i
++)
2444 info
->notes_size
+= note_size(&info
->notes
[i
]);
2446 /* read and fill status of all threads */
2448 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
2449 if (cpu
== thread_env
)
2451 fill_thread_info(info
, cpu
);
2458 static void free_note_info(struct elf_note_info
*info
)
2460 struct elf_thread_status
*ets
;
2462 while (!QTAILQ_EMPTY(&info
->thread_list
)) {
2463 ets
= QTAILQ_FIRST(&info
->thread_list
);
2464 QTAILQ_REMOVE(&info
->thread_list
, ets
, ets_link
);
2468 qemu_free(info
->prstatus
);
2469 qemu_free(info
->psinfo
);
2470 qemu_free(info
->notes
);
2473 static int write_note_info(struct elf_note_info
*info
, int fd
)
2475 struct elf_thread_status
*ets
;
2478 /* write prstatus, psinfo and auxv for current thread */
2479 for (i
= 0; i
< info
->numnote
; i
++)
2480 if ((error
= write_note(&info
->notes
[i
], fd
)) != 0)
2483 /* write prstatus for each thread */
2484 for (ets
= info
->thread_list
.tqh_first
; ets
!= NULL
;
2485 ets
= ets
->ets_link
.tqe_next
) {
2486 if ((error
= write_note(&ets
->notes
[0], fd
)) != 0)
2494 * Write out ELF coredump.
2496 * See documentation of ELF object file format in:
2497 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2499 * Coredump format in linux is following:
2501 * 0 +----------------------+ \
2502 * | ELF header | ET_CORE |
2503 * +----------------------+ |
2504 * | ELF program headers | |--- headers
2505 * | - NOTE section | |
2506 * | - PT_LOAD sections | |
2507 * +----------------------+ /
2512 * +----------------------+ <-- aligned to target page
2513 * | Process memory dump |
2518 * +----------------------+
2520 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2521 * NT_PRSINFO -> struct elf_prpsinfo
2522 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2524 * Format follows System V format as close as possible. Current
2525 * version limitations are as follows:
2526 * - no floating point registers are dumped
2528 * Function returns 0 in case of success, negative errno otherwise.
2530 * TODO: make this work also during runtime: it should be
2531 * possible to force coredump from running process and then
2532 * continue processing. For example qemu could set up SIGUSR2
2533 * handler (provided that target process haven't registered
2534 * handler for that) that does the dump when signal is received.
2536 static int elf_core_dump(int signr
, const CPUState
*env
)
2538 const TaskState
*ts
= (const TaskState
*)env
->opaque
;
2539 struct vm_area_struct
*vma
= NULL
;
2540 char corefile
[PATH_MAX
];
2541 struct elf_note_info info
;
2543 struct elf_phdr phdr
;
2544 struct rlimit dumpsize
;
2545 struct mm_struct
*mm
= NULL
;
2546 off_t offset
= 0, data_offset
= 0;
2551 getrlimit(RLIMIT_CORE
, &dumpsize
);
2552 if (dumpsize
.rlim_cur
== 0)
2555 if (core_dump_filename(ts
, corefile
, sizeof (corefile
)) < 0)
2558 if ((fd
= open(corefile
, O_WRONLY
| O_CREAT
,
2559 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) < 0)
2563 * Walk through target process memory mappings and
2564 * set up structure containing this information. After
2565 * this point vma_xxx functions can be used.
2567 if ((mm
= vma_init()) == NULL
)
2570 walk_memory_regions(mm
, vma_walker
);
2571 segs
= vma_get_mapping_count(mm
);
2574 * Construct valid coredump ELF header. We also
2575 * add one more segment for notes.
2577 fill_elf_header(&elf
, segs
+ 1, ELF_MACHINE
, 0);
2578 if (dump_write(fd
, &elf
, sizeof (elf
)) != 0)
2581 /* fill in in-memory version of notes */
2582 if (fill_note_info(&info
, signr
, env
) < 0)
2585 offset
+= sizeof (elf
); /* elf header */
2586 offset
+= (segs
+ 1) * sizeof (struct elf_phdr
); /* program headers */
2588 /* write out notes program header */
2589 fill_elf_note_phdr(&phdr
, info
.notes_size
, offset
);
2591 offset
+= info
.notes_size
;
2592 if (dump_write(fd
, &phdr
, sizeof (phdr
)) != 0)
2596 * ELF specification wants data to start at page boundary so
2599 data_offset
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
2602 * Write program headers for memory regions mapped in
2603 * the target process.
2605 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
2606 (void) memset(&phdr
, 0, sizeof (phdr
));
2608 phdr
.p_type
= PT_LOAD
;
2609 phdr
.p_offset
= offset
;
2610 phdr
.p_vaddr
= vma
->vma_start
;
2612 phdr
.p_filesz
= vma_dump_size(vma
);
2613 offset
+= phdr
.p_filesz
;
2614 phdr
.p_memsz
= vma
->vma_end
- vma
->vma_start
;
2615 phdr
.p_flags
= vma
->vma_flags
& PROT_READ
? PF_R
: 0;
2616 if (vma
->vma_flags
& PROT_WRITE
)
2617 phdr
.p_flags
|= PF_W
;
2618 if (vma
->vma_flags
& PROT_EXEC
)
2619 phdr
.p_flags
|= PF_X
;
2620 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
2622 bswap_phdr(&phdr
, 1);
2623 dump_write(fd
, &phdr
, sizeof (phdr
));
2627 * Next we write notes just after program headers. No
2628 * alignment needed here.
2630 if (write_note_info(&info
, fd
) < 0)
2633 /* align data to page boundary */
2634 if (lseek(fd
, data_offset
, SEEK_SET
) != data_offset
)
2638 * Finally we can dump process memory into corefile as well.
2640 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
2644 end
= vma
->vma_start
+ vma_dump_size(vma
);
2646 for (addr
= vma
->vma_start
; addr
< end
;
2647 addr
+= TARGET_PAGE_SIZE
) {
2648 char page
[TARGET_PAGE_SIZE
];
2652 * Read in page from target process memory and
2653 * write it to coredump file.
2655 error
= copy_from_user(page
, addr
, sizeof (page
));
2657 (void) fprintf(stderr
, "unable to dump " TARGET_ABI_FMT_lx
"\n",
2662 if (dump_write(fd
, page
, TARGET_PAGE_SIZE
) < 0)
2668 free_note_info(&info
);
2677 #endif /* USE_ELF_CORE_DUMP */
2679 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
2681 init_thread(regs
, infop
);