linux-user: use GDateTime for formatting timestamp for core file
[qemu/ar7.git] / linux-user / elfload.c
blob015eed1a27b4acc4437fb023b16ccc143b588b06
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
3 #include <sys/param.h>
5 #include <sys/resource.h>
6 #include <sys/shm.h>
8 #include "qemu.h"
9 #include "disas/disas.h"
10 #include "qemu/bitops.h"
11 #include "qemu/path.h"
12 #include "qemu/queue.h"
13 #include "qemu/guest-random.h"
14 #include "qemu/units.h"
15 #include "qemu/selfmap.h"
16 #include "qapi/error.h"
18 #ifdef _ARCH_PPC64
19 #undef ARCH_DLINFO
20 #undef ELF_PLATFORM
21 #undef ELF_HWCAP
22 #undef ELF_HWCAP2
23 #undef ELF_CLASS
24 #undef ELF_DATA
25 #undef ELF_ARCH
26 #endif
28 #define ELF_OSABI ELFOSABI_SYSV
30 /* from personality.h */
33 * Flags for bug emulation.
35 * These occupy the top three bytes.
37 enum {
38 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
39 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
40 descriptors (signal handling) */
41 MMAP_PAGE_ZERO = 0x0100000,
42 ADDR_COMPAT_LAYOUT = 0x0200000,
43 READ_IMPLIES_EXEC = 0x0400000,
44 ADDR_LIMIT_32BIT = 0x0800000,
45 SHORT_INODE = 0x1000000,
46 WHOLE_SECONDS = 0x2000000,
47 STICKY_TIMEOUTS = 0x4000000,
48 ADDR_LIMIT_3GB = 0x8000000,
52 * Personality types.
54 * These go in the low byte. Avoid using the top bit, it will
55 * conflict with error returns.
57 enum {
58 PER_LINUX = 0x0000,
59 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
60 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
61 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
62 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
63 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
64 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
65 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
66 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
67 PER_BSD = 0x0006,
68 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
69 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
70 PER_LINUX32 = 0x0008,
71 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
72 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
73 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
74 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
75 PER_RISCOS = 0x000c,
76 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
77 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
78 PER_OSF4 = 0x000f, /* OSF/1 v4 */
79 PER_HPUX = 0x0010,
80 PER_MASK = 0x00ff,
84 * Return the base personality without flags.
86 #define personality(pers) (pers & PER_MASK)
88 int info_is_fdpic(struct image_info *info)
90 return info->personality == PER_LINUX_FDPIC;
93 /* this flag is uneffective under linux too, should be deleted */
94 #ifndef MAP_DENYWRITE
95 #define MAP_DENYWRITE 0
96 #endif
98 /* should probably go in elf.h */
99 #ifndef ELIBBAD
100 #define ELIBBAD 80
101 #endif
103 #ifdef TARGET_WORDS_BIGENDIAN
104 #define ELF_DATA ELFDATA2MSB
105 #else
106 #define ELF_DATA ELFDATA2LSB
107 #endif
109 #ifdef TARGET_ABI_MIPSN32
110 typedef abi_ullong target_elf_greg_t;
111 #define tswapreg(ptr) tswap64(ptr)
112 #else
113 typedef abi_ulong target_elf_greg_t;
114 #define tswapreg(ptr) tswapal(ptr)
115 #endif
117 #ifdef USE_UID16
118 typedef abi_ushort target_uid_t;
119 typedef abi_ushort target_gid_t;
120 #else
121 typedef abi_uint target_uid_t;
122 typedef abi_uint target_gid_t;
123 #endif
124 typedef abi_int target_pid_t;
126 #ifdef TARGET_I386
128 #define ELF_PLATFORM get_elf_platform()
130 static const char *get_elf_platform(void)
132 static char elf_platform[] = "i386";
133 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
134 if (family > 6)
135 family = 6;
136 if (family >= 3)
137 elf_platform[1] = '0' + family;
138 return elf_platform;
141 #define ELF_HWCAP get_elf_hwcap()
143 static uint32_t get_elf_hwcap(void)
145 X86CPU *cpu = X86_CPU(thread_cpu);
147 return cpu->env.features[FEAT_1_EDX];
150 #ifdef TARGET_X86_64
151 #define ELF_START_MMAP 0x2aaaaab000ULL
153 #define ELF_CLASS ELFCLASS64
154 #define ELF_ARCH EM_X86_64
156 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
158 regs->rax = 0;
159 regs->rsp = infop->start_stack;
160 regs->rip = infop->entry;
163 #define ELF_NREG 27
164 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
167 * Note that ELF_NREG should be 29 as there should be place for
168 * TRAPNO and ERR "registers" as well but linux doesn't dump
169 * those.
171 * See linux kernel: arch/x86/include/asm/elf.h
173 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
175 (*regs)[0] = env->regs[15];
176 (*regs)[1] = env->regs[14];
177 (*regs)[2] = env->regs[13];
178 (*regs)[3] = env->regs[12];
179 (*regs)[4] = env->regs[R_EBP];
180 (*regs)[5] = env->regs[R_EBX];
181 (*regs)[6] = env->regs[11];
182 (*regs)[7] = env->regs[10];
183 (*regs)[8] = env->regs[9];
184 (*regs)[9] = env->regs[8];
185 (*regs)[10] = env->regs[R_EAX];
186 (*regs)[11] = env->regs[R_ECX];
187 (*regs)[12] = env->regs[R_EDX];
188 (*regs)[13] = env->regs[R_ESI];
189 (*regs)[14] = env->regs[R_EDI];
190 (*regs)[15] = env->regs[R_EAX]; /* XXX */
191 (*regs)[16] = env->eip;
192 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
193 (*regs)[18] = env->eflags;
194 (*regs)[19] = env->regs[R_ESP];
195 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
196 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
197 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
198 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
199 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
200 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
201 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
204 #else
206 #define ELF_START_MMAP 0x80000000
209 * This is used to ensure we don't load something for the wrong architecture.
211 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
214 * These are used to set parameters in the core dumps.
216 #define ELF_CLASS ELFCLASS32
217 #define ELF_ARCH EM_386
219 static inline void init_thread(struct target_pt_regs *regs,
220 struct image_info *infop)
222 regs->esp = infop->start_stack;
223 regs->eip = infop->entry;
225 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
226 starts %edx contains a pointer to a function which might be
227 registered using `atexit'. This provides a mean for the
228 dynamic linker to call DT_FINI functions for shared libraries
229 that have been loaded before the code runs.
231 A value of 0 tells we have no such handler. */
232 regs->edx = 0;
235 #define ELF_NREG 17
236 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
239 * Note that ELF_NREG should be 19 as there should be place for
240 * TRAPNO and ERR "registers" as well but linux doesn't dump
241 * those.
243 * See linux kernel: arch/x86/include/asm/elf.h
245 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
247 (*regs)[0] = env->regs[R_EBX];
248 (*regs)[1] = env->regs[R_ECX];
249 (*regs)[2] = env->regs[R_EDX];
250 (*regs)[3] = env->regs[R_ESI];
251 (*regs)[4] = env->regs[R_EDI];
252 (*regs)[5] = env->regs[R_EBP];
253 (*regs)[6] = env->regs[R_EAX];
254 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
255 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
256 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
257 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
258 (*regs)[11] = env->regs[R_EAX]; /* XXX */
259 (*regs)[12] = env->eip;
260 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
261 (*regs)[14] = env->eflags;
262 (*regs)[15] = env->regs[R_ESP];
263 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
265 #endif
267 #define USE_ELF_CORE_DUMP
268 #define ELF_EXEC_PAGESIZE 4096
270 #endif
272 #ifdef TARGET_ARM
274 #ifndef TARGET_AARCH64
275 /* 32 bit ARM definitions */
277 #define ELF_START_MMAP 0x80000000
279 #define ELF_ARCH EM_ARM
280 #define ELF_CLASS ELFCLASS32
282 static inline void init_thread(struct target_pt_regs *regs,
283 struct image_info *infop)
285 abi_long stack = infop->start_stack;
286 memset(regs, 0, sizeof(*regs));
288 regs->uregs[16] = ARM_CPU_MODE_USR;
289 if (infop->entry & 1) {
290 regs->uregs[16] |= CPSR_T;
292 regs->uregs[15] = infop->entry & 0xfffffffe;
293 regs->uregs[13] = infop->start_stack;
294 /* FIXME - what to for failure of get_user()? */
295 get_user_ual(regs->uregs[2], stack + 8); /* envp */
296 get_user_ual(regs->uregs[1], stack + 4); /* envp */
297 /* XXX: it seems that r0 is zeroed after ! */
298 regs->uregs[0] = 0;
299 /* For uClinux PIC binaries. */
300 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
301 regs->uregs[10] = infop->start_data;
303 /* Support ARM FDPIC. */
304 if (info_is_fdpic(infop)) {
305 /* As described in the ABI document, r7 points to the loadmap info
306 * prepared by the kernel. If an interpreter is needed, r8 points
307 * to the interpreter loadmap and r9 points to the interpreter
308 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
309 * r9 points to the main program PT_DYNAMIC info.
311 regs->uregs[7] = infop->loadmap_addr;
312 if (infop->interpreter_loadmap_addr) {
313 /* Executable is dynamically loaded. */
314 regs->uregs[8] = infop->interpreter_loadmap_addr;
315 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
316 } else {
317 regs->uregs[8] = 0;
318 regs->uregs[9] = infop->pt_dynamic_addr;
323 #define ELF_NREG 18
324 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
326 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
328 (*regs)[0] = tswapreg(env->regs[0]);
329 (*regs)[1] = tswapreg(env->regs[1]);
330 (*regs)[2] = tswapreg(env->regs[2]);
331 (*regs)[3] = tswapreg(env->regs[3]);
332 (*regs)[4] = tswapreg(env->regs[4]);
333 (*regs)[5] = tswapreg(env->regs[5]);
334 (*regs)[6] = tswapreg(env->regs[6]);
335 (*regs)[7] = tswapreg(env->regs[7]);
336 (*regs)[8] = tswapreg(env->regs[8]);
337 (*regs)[9] = tswapreg(env->regs[9]);
338 (*regs)[10] = tswapreg(env->regs[10]);
339 (*regs)[11] = tswapreg(env->regs[11]);
340 (*regs)[12] = tswapreg(env->regs[12]);
341 (*regs)[13] = tswapreg(env->regs[13]);
342 (*regs)[14] = tswapreg(env->regs[14]);
343 (*regs)[15] = tswapreg(env->regs[15]);
345 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
346 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
349 #define USE_ELF_CORE_DUMP
350 #define ELF_EXEC_PAGESIZE 4096
352 enum
354 ARM_HWCAP_ARM_SWP = 1 << 0,
355 ARM_HWCAP_ARM_HALF = 1 << 1,
356 ARM_HWCAP_ARM_THUMB = 1 << 2,
357 ARM_HWCAP_ARM_26BIT = 1 << 3,
358 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
359 ARM_HWCAP_ARM_FPA = 1 << 5,
360 ARM_HWCAP_ARM_VFP = 1 << 6,
361 ARM_HWCAP_ARM_EDSP = 1 << 7,
362 ARM_HWCAP_ARM_JAVA = 1 << 8,
363 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
364 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
365 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
366 ARM_HWCAP_ARM_NEON = 1 << 12,
367 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
368 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
369 ARM_HWCAP_ARM_TLS = 1 << 15,
370 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
371 ARM_HWCAP_ARM_IDIVA = 1 << 17,
372 ARM_HWCAP_ARM_IDIVT = 1 << 18,
373 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
374 ARM_HWCAP_ARM_LPAE = 1 << 20,
375 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
378 enum {
379 ARM_HWCAP2_ARM_AES = 1 << 0,
380 ARM_HWCAP2_ARM_PMULL = 1 << 1,
381 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
382 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
383 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
386 /* The commpage only exists for 32 bit kernels */
388 #define ARM_COMMPAGE (intptr_t)0xffff0f00u
390 static bool init_guest_commpage(void)
392 void *want = g2h_untagged(ARM_COMMPAGE & -qemu_host_page_size);
393 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
394 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
396 if (addr == MAP_FAILED) {
397 perror("Allocating guest commpage");
398 exit(EXIT_FAILURE);
400 if (addr != want) {
401 return false;
404 /* Set kernel helper versions; rest of page is 0. */
405 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
407 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
408 perror("Protecting guest commpage");
409 exit(EXIT_FAILURE);
411 return true;
414 #define ELF_HWCAP get_elf_hwcap()
415 #define ELF_HWCAP2 get_elf_hwcap2()
417 static uint32_t get_elf_hwcap(void)
419 ARMCPU *cpu = ARM_CPU(thread_cpu);
420 uint32_t hwcaps = 0;
422 hwcaps |= ARM_HWCAP_ARM_SWP;
423 hwcaps |= ARM_HWCAP_ARM_HALF;
424 hwcaps |= ARM_HWCAP_ARM_THUMB;
425 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
427 /* probe for the extra features */
428 #define GET_FEATURE(feat, hwcap) \
429 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
431 #define GET_FEATURE_ID(feat, hwcap) \
432 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
434 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
435 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
436 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
437 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
438 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
439 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
440 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
441 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
442 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
443 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
445 if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
446 cpu_isar_feature(aa32_fpdp_v3, cpu)) {
447 hwcaps |= ARM_HWCAP_ARM_VFPv3;
448 if (cpu_isar_feature(aa32_simd_r32, cpu)) {
449 hwcaps |= ARM_HWCAP_ARM_VFPD32;
450 } else {
451 hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
454 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
456 return hwcaps;
459 static uint32_t get_elf_hwcap2(void)
461 ARMCPU *cpu = ARM_CPU(thread_cpu);
462 uint32_t hwcaps = 0;
464 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
465 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
466 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
467 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
468 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
469 return hwcaps;
472 #undef GET_FEATURE
473 #undef GET_FEATURE_ID
475 #define ELF_PLATFORM get_elf_platform()
477 static const char *get_elf_platform(void)
479 CPUARMState *env = thread_cpu->env_ptr;
481 #ifdef TARGET_WORDS_BIGENDIAN
482 # define END "b"
483 #else
484 # define END "l"
485 #endif
487 if (arm_feature(env, ARM_FEATURE_V8)) {
488 return "v8" END;
489 } else if (arm_feature(env, ARM_FEATURE_V7)) {
490 if (arm_feature(env, ARM_FEATURE_M)) {
491 return "v7m" END;
492 } else {
493 return "v7" END;
495 } else if (arm_feature(env, ARM_FEATURE_V6)) {
496 return "v6" END;
497 } else if (arm_feature(env, ARM_FEATURE_V5)) {
498 return "v5" END;
499 } else {
500 return "v4" END;
503 #undef END
506 #else
507 /* 64 bit ARM definitions */
508 #define ELF_START_MMAP 0x80000000
510 #define ELF_ARCH EM_AARCH64
511 #define ELF_CLASS ELFCLASS64
512 #ifdef TARGET_WORDS_BIGENDIAN
513 # define ELF_PLATFORM "aarch64_be"
514 #else
515 # define ELF_PLATFORM "aarch64"
516 #endif
518 static inline void init_thread(struct target_pt_regs *regs,
519 struct image_info *infop)
521 abi_long stack = infop->start_stack;
522 memset(regs, 0, sizeof(*regs));
524 regs->pc = infop->entry & ~0x3ULL;
525 regs->sp = stack;
528 #define ELF_NREG 34
529 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
531 static void elf_core_copy_regs(target_elf_gregset_t *regs,
532 const CPUARMState *env)
534 int i;
536 for (i = 0; i < 32; i++) {
537 (*regs)[i] = tswapreg(env->xregs[i]);
539 (*regs)[32] = tswapreg(env->pc);
540 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
543 #define USE_ELF_CORE_DUMP
544 #define ELF_EXEC_PAGESIZE 4096
546 enum {
547 ARM_HWCAP_A64_FP = 1 << 0,
548 ARM_HWCAP_A64_ASIMD = 1 << 1,
549 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
550 ARM_HWCAP_A64_AES = 1 << 3,
551 ARM_HWCAP_A64_PMULL = 1 << 4,
552 ARM_HWCAP_A64_SHA1 = 1 << 5,
553 ARM_HWCAP_A64_SHA2 = 1 << 6,
554 ARM_HWCAP_A64_CRC32 = 1 << 7,
555 ARM_HWCAP_A64_ATOMICS = 1 << 8,
556 ARM_HWCAP_A64_FPHP = 1 << 9,
557 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
558 ARM_HWCAP_A64_CPUID = 1 << 11,
559 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
560 ARM_HWCAP_A64_JSCVT = 1 << 13,
561 ARM_HWCAP_A64_FCMA = 1 << 14,
562 ARM_HWCAP_A64_LRCPC = 1 << 15,
563 ARM_HWCAP_A64_DCPOP = 1 << 16,
564 ARM_HWCAP_A64_SHA3 = 1 << 17,
565 ARM_HWCAP_A64_SM3 = 1 << 18,
566 ARM_HWCAP_A64_SM4 = 1 << 19,
567 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
568 ARM_HWCAP_A64_SHA512 = 1 << 21,
569 ARM_HWCAP_A64_SVE = 1 << 22,
570 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
571 ARM_HWCAP_A64_DIT = 1 << 24,
572 ARM_HWCAP_A64_USCAT = 1 << 25,
573 ARM_HWCAP_A64_ILRCPC = 1 << 26,
574 ARM_HWCAP_A64_FLAGM = 1 << 27,
575 ARM_HWCAP_A64_SSBS = 1 << 28,
576 ARM_HWCAP_A64_SB = 1 << 29,
577 ARM_HWCAP_A64_PACA = 1 << 30,
578 ARM_HWCAP_A64_PACG = 1UL << 31,
580 ARM_HWCAP2_A64_DCPODP = 1 << 0,
581 ARM_HWCAP2_A64_SVE2 = 1 << 1,
582 ARM_HWCAP2_A64_SVEAES = 1 << 2,
583 ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
584 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
585 ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
586 ARM_HWCAP2_A64_SVESM4 = 1 << 6,
587 ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
588 ARM_HWCAP2_A64_FRINT = 1 << 8,
589 ARM_HWCAP2_A64_SVEI8MM = 1 << 9,
590 ARM_HWCAP2_A64_SVEF32MM = 1 << 10,
591 ARM_HWCAP2_A64_SVEF64MM = 1 << 11,
592 ARM_HWCAP2_A64_SVEBF16 = 1 << 12,
593 ARM_HWCAP2_A64_I8MM = 1 << 13,
594 ARM_HWCAP2_A64_BF16 = 1 << 14,
595 ARM_HWCAP2_A64_DGH = 1 << 15,
596 ARM_HWCAP2_A64_RNG = 1 << 16,
597 ARM_HWCAP2_A64_BTI = 1 << 17,
598 ARM_HWCAP2_A64_MTE = 1 << 18,
601 #define ELF_HWCAP get_elf_hwcap()
602 #define ELF_HWCAP2 get_elf_hwcap2()
604 #define GET_FEATURE_ID(feat, hwcap) \
605 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
607 static uint32_t get_elf_hwcap(void)
609 ARMCPU *cpu = ARM_CPU(thread_cpu);
610 uint32_t hwcaps = 0;
612 hwcaps |= ARM_HWCAP_A64_FP;
613 hwcaps |= ARM_HWCAP_A64_ASIMD;
614 hwcaps |= ARM_HWCAP_A64_CPUID;
616 /* probe for the extra features */
618 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
619 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
620 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
621 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
622 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
623 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
624 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
625 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
626 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
627 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
628 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
629 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
630 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
631 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
632 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
633 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
634 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
635 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
636 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
637 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
638 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
639 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
640 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
642 return hwcaps;
645 static uint32_t get_elf_hwcap2(void)
647 ARMCPU *cpu = ARM_CPU(thread_cpu);
648 uint32_t hwcaps = 0;
650 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
651 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
652 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
653 GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG);
654 GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI);
655 GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE);
657 return hwcaps;
660 #undef GET_FEATURE_ID
662 #endif /* not TARGET_AARCH64 */
663 #endif /* TARGET_ARM */
665 #ifdef TARGET_SPARC
666 #ifdef TARGET_SPARC64
668 #define ELF_START_MMAP 0x80000000
669 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
670 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
671 #ifndef TARGET_ABI32
672 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
673 #else
674 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
675 #endif
677 #define ELF_CLASS ELFCLASS64
678 #define ELF_ARCH EM_SPARCV9
679 #else
680 #define ELF_START_MMAP 0x80000000
681 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
682 | HWCAP_SPARC_MULDIV)
683 #define ELF_CLASS ELFCLASS32
684 #define ELF_ARCH EM_SPARC
685 #endif /* TARGET_SPARC64 */
687 static inline void init_thread(struct target_pt_regs *regs,
688 struct image_info *infop)
690 /* Note that target_cpu_copy_regs does not read psr/tstate. */
691 regs->pc = infop->entry;
692 regs->npc = regs->pc + 4;
693 regs->y = 0;
694 regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong)
695 - TARGET_STACK_BIAS);
697 #endif /* TARGET_SPARC */
699 #ifdef TARGET_PPC
701 #define ELF_MACHINE PPC_ELF_MACHINE
702 #define ELF_START_MMAP 0x80000000
704 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
706 #define elf_check_arch(x) ( (x) == EM_PPC64 )
708 #define ELF_CLASS ELFCLASS64
710 #else
712 #define ELF_CLASS ELFCLASS32
714 #endif
716 #define ELF_ARCH EM_PPC
718 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
719 See arch/powerpc/include/asm/cputable.h. */
720 enum {
721 QEMU_PPC_FEATURE_32 = 0x80000000,
722 QEMU_PPC_FEATURE_64 = 0x40000000,
723 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
724 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
725 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
726 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
727 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
728 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
729 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
730 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
731 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
732 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
733 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
734 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
735 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
736 QEMU_PPC_FEATURE_CELL = 0x00010000,
737 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
738 QEMU_PPC_FEATURE_SMT = 0x00004000,
739 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
740 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
741 QEMU_PPC_FEATURE_PA6T = 0x00000800,
742 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
743 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
744 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
745 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
746 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
748 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
749 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
751 /* Feature definitions in AT_HWCAP2. */
752 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
753 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
754 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
755 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
756 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
757 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
758 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
759 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
760 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
761 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
762 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
763 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
764 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
767 #define ELF_HWCAP get_elf_hwcap()
769 static uint32_t get_elf_hwcap(void)
771 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
772 uint32_t features = 0;
774 /* We don't have to be terribly complete here; the high points are
775 Altivec/FP/SPE support. Anything else is just a bonus. */
776 #define GET_FEATURE(flag, feature) \
777 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
778 #define GET_FEATURE2(flags, feature) \
779 do { \
780 if ((cpu->env.insns_flags2 & flags) == flags) { \
781 features |= feature; \
783 } while (0)
784 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
785 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
786 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
787 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
788 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
789 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
790 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
791 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
792 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
793 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
794 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
795 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
796 QEMU_PPC_FEATURE_ARCH_2_06);
797 #undef GET_FEATURE
798 #undef GET_FEATURE2
800 return features;
803 #define ELF_HWCAP2 get_elf_hwcap2()
805 static uint32_t get_elf_hwcap2(void)
807 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
808 uint32_t features = 0;
810 #define GET_FEATURE(flag, feature) \
811 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
812 #define GET_FEATURE2(flag, feature) \
813 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
815 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
816 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
817 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
818 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
819 QEMU_PPC_FEATURE2_VEC_CRYPTO);
820 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
821 QEMU_PPC_FEATURE2_DARN);
823 #undef GET_FEATURE
824 #undef GET_FEATURE2
826 return features;
830 * The requirements here are:
831 * - keep the final alignment of sp (sp & 0xf)
832 * - make sure the 32-bit value at the first 16 byte aligned position of
833 * AUXV is greater than 16 for glibc compatibility.
834 * AT_IGNOREPPC is used for that.
835 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
836 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
838 #define DLINFO_ARCH_ITEMS 5
839 #define ARCH_DLINFO \
840 do { \
841 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
842 /* \
843 * Handle glibc compatibility: these magic entries must \
844 * be at the lowest addresses in the final auxv. \
845 */ \
846 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
847 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
848 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
849 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
850 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
851 } while (0)
853 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
855 _regs->gpr[1] = infop->start_stack;
856 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
857 if (get_ppc64_abi(infop) < 2) {
858 uint64_t val;
859 get_user_u64(val, infop->entry + 8);
860 _regs->gpr[2] = val + infop->load_bias;
861 get_user_u64(val, infop->entry);
862 infop->entry = val + infop->load_bias;
863 } else {
864 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
866 #endif
867 _regs->nip = infop->entry;
870 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
871 #define ELF_NREG 48
872 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
874 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
876 int i;
877 target_ulong ccr = 0;
879 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
880 (*regs)[i] = tswapreg(env->gpr[i]);
883 (*regs)[32] = tswapreg(env->nip);
884 (*regs)[33] = tswapreg(env->msr);
885 (*regs)[35] = tswapreg(env->ctr);
886 (*regs)[36] = tswapreg(env->lr);
887 (*regs)[37] = tswapreg(env->xer);
889 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
890 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
892 (*regs)[38] = tswapreg(ccr);
895 #define USE_ELF_CORE_DUMP
896 #define ELF_EXEC_PAGESIZE 4096
898 #endif
900 #ifdef TARGET_MIPS
902 #define ELF_START_MMAP 0x80000000
904 #ifdef TARGET_MIPS64
905 #define ELF_CLASS ELFCLASS64
906 #else
907 #define ELF_CLASS ELFCLASS32
908 #endif
909 #define ELF_ARCH EM_MIPS
911 #define elf_check_arch(x) ((x) == EM_MIPS || (x) == EM_NANOMIPS)
913 #ifdef TARGET_ABI_MIPSN32
914 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
915 #else
916 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
917 #endif
919 static inline void init_thread(struct target_pt_regs *regs,
920 struct image_info *infop)
922 regs->cp0_status = 2 << CP0St_KSU;
923 regs->cp0_epc = infop->entry;
924 regs->regs[29] = infop->start_stack;
927 /* See linux kernel: arch/mips/include/asm/elf.h. */
928 #define ELF_NREG 45
929 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
931 /* See linux kernel: arch/mips/include/asm/reg.h. */
932 enum {
933 #ifdef TARGET_MIPS64
934 TARGET_EF_R0 = 0,
935 #else
936 TARGET_EF_R0 = 6,
937 #endif
938 TARGET_EF_R26 = TARGET_EF_R0 + 26,
939 TARGET_EF_R27 = TARGET_EF_R0 + 27,
940 TARGET_EF_LO = TARGET_EF_R0 + 32,
941 TARGET_EF_HI = TARGET_EF_R0 + 33,
942 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
943 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
944 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
945 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
948 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
949 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
951 int i;
953 for (i = 0; i < TARGET_EF_R0; i++) {
954 (*regs)[i] = 0;
956 (*regs)[TARGET_EF_R0] = 0;
958 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
959 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
962 (*regs)[TARGET_EF_R26] = 0;
963 (*regs)[TARGET_EF_R27] = 0;
964 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
965 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
966 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
967 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
968 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
969 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
972 #define USE_ELF_CORE_DUMP
973 #define ELF_EXEC_PAGESIZE 4096
975 /* See arch/mips/include/uapi/asm/hwcap.h. */
976 enum {
977 HWCAP_MIPS_R6 = (1 << 0),
978 HWCAP_MIPS_MSA = (1 << 1),
979 HWCAP_MIPS_CRC32 = (1 << 2),
980 HWCAP_MIPS_MIPS16 = (1 << 3),
981 HWCAP_MIPS_MDMX = (1 << 4),
982 HWCAP_MIPS_MIPS3D = (1 << 5),
983 HWCAP_MIPS_SMARTMIPS = (1 << 6),
984 HWCAP_MIPS_DSP = (1 << 7),
985 HWCAP_MIPS_DSP2 = (1 << 8),
986 HWCAP_MIPS_DSP3 = (1 << 9),
987 HWCAP_MIPS_MIPS16E2 = (1 << 10),
988 HWCAP_LOONGSON_MMI = (1 << 11),
989 HWCAP_LOONGSON_EXT = (1 << 12),
990 HWCAP_LOONGSON_EXT2 = (1 << 13),
991 HWCAP_LOONGSON_CPUCFG = (1 << 14),
994 #define ELF_HWCAP get_elf_hwcap()
996 #define GET_FEATURE_INSN(_flag, _hwcap) \
997 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
999 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1000 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1002 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1003 do { \
1004 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1005 hwcaps |= _hwcap; \
1007 } while (0)
1009 static uint32_t get_elf_hwcap(void)
1011 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1012 uint32_t hwcaps = 0;
1014 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH,
1015 2, HWCAP_MIPS_R6);
1016 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA);
1017 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI);
1018 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT);
1020 return hwcaps;
1023 #undef GET_FEATURE_REG_EQU
1024 #undef GET_FEATURE_REG_SET
1025 #undef GET_FEATURE_INSN
1027 #endif /* TARGET_MIPS */
1029 #ifdef TARGET_MICROBLAZE
1031 #define ELF_START_MMAP 0x80000000
1033 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1035 #define ELF_CLASS ELFCLASS32
1036 #define ELF_ARCH EM_MICROBLAZE
1038 static inline void init_thread(struct target_pt_regs *regs,
1039 struct image_info *infop)
1041 regs->pc = infop->entry;
1042 regs->r1 = infop->start_stack;
1046 #define ELF_EXEC_PAGESIZE 4096
1048 #define USE_ELF_CORE_DUMP
1049 #define ELF_NREG 38
1050 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1052 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1053 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1055 int i, pos = 0;
1057 for (i = 0; i < 32; i++) {
1058 (*regs)[pos++] = tswapreg(env->regs[i]);
1061 (*regs)[pos++] = tswapreg(env->pc);
1062 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
1063 (*regs)[pos++] = 0;
1064 (*regs)[pos++] = tswapreg(env->ear);
1065 (*regs)[pos++] = 0;
1066 (*regs)[pos++] = tswapreg(env->esr);
1069 #endif /* TARGET_MICROBLAZE */
1071 #ifdef TARGET_NIOS2
1073 #define ELF_START_MMAP 0x80000000
1075 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1077 #define ELF_CLASS ELFCLASS32
1078 #define ELF_ARCH EM_ALTERA_NIOS2
1080 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1082 regs->ea = infop->entry;
1083 regs->sp = infop->start_stack;
1084 regs->estatus = 0x3;
1087 #define ELF_EXEC_PAGESIZE 4096
1089 #define USE_ELF_CORE_DUMP
1090 #define ELF_NREG 49
1091 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1093 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1094 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1095 const CPUNios2State *env)
1097 int i;
1099 (*regs)[0] = -1;
1100 for (i = 1; i < 8; i++) /* r0-r7 */
1101 (*regs)[i] = tswapreg(env->regs[i + 7]);
1103 for (i = 8; i < 16; i++) /* r8-r15 */
1104 (*regs)[i] = tswapreg(env->regs[i - 8]);
1106 for (i = 16; i < 24; i++) /* r16-r23 */
1107 (*regs)[i] = tswapreg(env->regs[i + 7]);
1108 (*regs)[24] = -1; /* R_ET */
1109 (*regs)[25] = -1; /* R_BT */
1110 (*regs)[26] = tswapreg(env->regs[R_GP]);
1111 (*regs)[27] = tswapreg(env->regs[R_SP]);
1112 (*regs)[28] = tswapreg(env->regs[R_FP]);
1113 (*regs)[29] = tswapreg(env->regs[R_EA]);
1114 (*regs)[30] = -1; /* R_SSTATUS */
1115 (*regs)[31] = tswapreg(env->regs[R_RA]);
1117 (*regs)[32] = tswapreg(env->regs[R_PC]);
1119 (*regs)[33] = -1; /* R_STATUS */
1120 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1122 for (i = 35; i < 49; i++) /* ... */
1123 (*regs)[i] = -1;
1126 #endif /* TARGET_NIOS2 */
1128 #ifdef TARGET_OPENRISC
1130 #define ELF_START_MMAP 0x08000000
1132 #define ELF_ARCH EM_OPENRISC
1133 #define ELF_CLASS ELFCLASS32
1134 #define ELF_DATA ELFDATA2MSB
1136 static inline void init_thread(struct target_pt_regs *regs,
1137 struct image_info *infop)
1139 regs->pc = infop->entry;
1140 regs->gpr[1] = infop->start_stack;
1143 #define USE_ELF_CORE_DUMP
1144 #define ELF_EXEC_PAGESIZE 8192
1146 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1147 #define ELF_NREG 34 /* gprs and pc, sr */
1148 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1150 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1151 const CPUOpenRISCState *env)
1153 int i;
1155 for (i = 0; i < 32; i++) {
1156 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1158 (*regs)[32] = tswapreg(env->pc);
1159 (*regs)[33] = tswapreg(cpu_get_sr(env));
1161 #define ELF_HWCAP 0
1162 #define ELF_PLATFORM NULL
1164 #endif /* TARGET_OPENRISC */
1166 #ifdef TARGET_SH4
1168 #define ELF_START_MMAP 0x80000000
1170 #define ELF_CLASS ELFCLASS32
1171 #define ELF_ARCH EM_SH
1173 static inline void init_thread(struct target_pt_regs *regs,
1174 struct image_info *infop)
1176 /* Check other registers XXXXX */
1177 regs->pc = infop->entry;
1178 regs->regs[15] = infop->start_stack;
1181 /* See linux kernel: arch/sh/include/asm/elf.h. */
1182 #define ELF_NREG 23
1183 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1185 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1186 enum {
1187 TARGET_REG_PC = 16,
1188 TARGET_REG_PR = 17,
1189 TARGET_REG_SR = 18,
1190 TARGET_REG_GBR = 19,
1191 TARGET_REG_MACH = 20,
1192 TARGET_REG_MACL = 21,
1193 TARGET_REG_SYSCALL = 22
1196 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1197 const CPUSH4State *env)
1199 int i;
1201 for (i = 0; i < 16; i++) {
1202 (*regs)[i] = tswapreg(env->gregs[i]);
1205 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1206 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1207 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1208 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1209 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1210 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1211 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1214 #define USE_ELF_CORE_DUMP
1215 #define ELF_EXEC_PAGESIZE 4096
1217 enum {
1218 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1219 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1220 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1221 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1222 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1223 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1224 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1225 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1226 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1227 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1230 #define ELF_HWCAP get_elf_hwcap()
1232 static uint32_t get_elf_hwcap(void)
1234 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1235 uint32_t hwcap = 0;
1237 hwcap |= SH_CPU_HAS_FPU;
1239 if (cpu->env.features & SH_FEATURE_SH4A) {
1240 hwcap |= SH_CPU_HAS_LLSC;
1243 return hwcap;
1246 #endif
1248 #ifdef TARGET_CRIS
1250 #define ELF_START_MMAP 0x80000000
1252 #define ELF_CLASS ELFCLASS32
1253 #define ELF_ARCH EM_CRIS
1255 static inline void init_thread(struct target_pt_regs *regs,
1256 struct image_info *infop)
1258 regs->erp = infop->entry;
1261 #define ELF_EXEC_PAGESIZE 8192
1263 #endif
1265 #ifdef TARGET_M68K
1267 #define ELF_START_MMAP 0x80000000
1269 #define ELF_CLASS ELFCLASS32
1270 #define ELF_ARCH EM_68K
1272 /* ??? Does this need to do anything?
1273 #define ELF_PLAT_INIT(_r) */
1275 static inline void init_thread(struct target_pt_regs *regs,
1276 struct image_info *infop)
1278 regs->usp = infop->start_stack;
1279 regs->sr = 0;
1280 regs->pc = infop->entry;
1283 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1284 #define ELF_NREG 20
1285 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1287 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1289 (*regs)[0] = tswapreg(env->dregs[1]);
1290 (*regs)[1] = tswapreg(env->dregs[2]);
1291 (*regs)[2] = tswapreg(env->dregs[3]);
1292 (*regs)[3] = tswapreg(env->dregs[4]);
1293 (*regs)[4] = tswapreg(env->dregs[5]);
1294 (*regs)[5] = tswapreg(env->dregs[6]);
1295 (*regs)[6] = tswapreg(env->dregs[7]);
1296 (*regs)[7] = tswapreg(env->aregs[0]);
1297 (*regs)[8] = tswapreg(env->aregs[1]);
1298 (*regs)[9] = tswapreg(env->aregs[2]);
1299 (*regs)[10] = tswapreg(env->aregs[3]);
1300 (*regs)[11] = tswapreg(env->aregs[4]);
1301 (*regs)[12] = tswapreg(env->aregs[5]);
1302 (*regs)[13] = tswapreg(env->aregs[6]);
1303 (*regs)[14] = tswapreg(env->dregs[0]);
1304 (*regs)[15] = tswapreg(env->aregs[7]);
1305 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1306 (*regs)[17] = tswapreg(env->sr);
1307 (*regs)[18] = tswapreg(env->pc);
1308 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1311 #define USE_ELF_CORE_DUMP
1312 #define ELF_EXEC_PAGESIZE 8192
1314 #endif
1316 #ifdef TARGET_ALPHA
1318 #define ELF_START_MMAP (0x30000000000ULL)
1320 #define ELF_CLASS ELFCLASS64
1321 #define ELF_ARCH EM_ALPHA
1323 static inline void init_thread(struct target_pt_regs *regs,
1324 struct image_info *infop)
1326 regs->pc = infop->entry;
1327 regs->ps = 8;
1328 regs->usp = infop->start_stack;
1331 #define ELF_EXEC_PAGESIZE 8192
1333 #endif /* TARGET_ALPHA */
1335 #ifdef TARGET_S390X
1337 #define ELF_START_MMAP (0x20000000000ULL)
1339 #define ELF_CLASS ELFCLASS64
1340 #define ELF_DATA ELFDATA2MSB
1341 #define ELF_ARCH EM_S390
1343 #include "elf.h"
1345 #define ELF_HWCAP get_elf_hwcap()
1347 #define GET_FEATURE(_feat, _hwcap) \
1348 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1350 static uint32_t get_elf_hwcap(void)
1353 * Let's assume we always have esan3 and zarch.
1354 * 31-bit processes can use 64-bit registers (high gprs).
1356 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1358 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1359 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1360 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1361 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1362 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1363 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1364 hwcap |= HWCAP_S390_ETF3EH;
1366 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1368 return hwcap;
1371 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1373 regs->psw.addr = infop->entry;
1374 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1375 regs->gprs[15] = infop->start_stack;
1378 #endif /* TARGET_S390X */
1380 #ifdef TARGET_RISCV
1382 #define ELF_START_MMAP 0x80000000
1383 #define ELF_ARCH EM_RISCV
1385 #ifdef TARGET_RISCV32
1386 #define ELF_CLASS ELFCLASS32
1387 #else
1388 #define ELF_CLASS ELFCLASS64
1389 #endif
1391 static inline void init_thread(struct target_pt_regs *regs,
1392 struct image_info *infop)
1394 regs->sepc = infop->entry;
1395 regs->sp = infop->start_stack;
1398 #define ELF_EXEC_PAGESIZE 4096
1400 #endif /* TARGET_RISCV */
1402 #ifdef TARGET_HPPA
1404 #define ELF_START_MMAP 0x80000000
1405 #define ELF_CLASS ELFCLASS32
1406 #define ELF_ARCH EM_PARISC
1407 #define ELF_PLATFORM "PARISC"
1408 #define STACK_GROWS_DOWN 0
1409 #define STACK_ALIGNMENT 64
1411 static inline void init_thread(struct target_pt_regs *regs,
1412 struct image_info *infop)
1414 regs->iaoq[0] = infop->entry;
1415 regs->iaoq[1] = infop->entry + 4;
1416 regs->gr[23] = 0;
1417 regs->gr[24] = infop->arg_start;
1418 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1419 /* The top-of-stack contains a linkage buffer. */
1420 regs->gr[30] = infop->start_stack + 64;
1421 regs->gr[31] = infop->entry;
1424 #endif /* TARGET_HPPA */
1426 #ifdef TARGET_XTENSA
1428 #define ELF_START_MMAP 0x20000000
1430 #define ELF_CLASS ELFCLASS32
1431 #define ELF_ARCH EM_XTENSA
1433 static inline void init_thread(struct target_pt_regs *regs,
1434 struct image_info *infop)
1436 regs->windowbase = 0;
1437 regs->windowstart = 1;
1438 regs->areg[1] = infop->start_stack;
1439 regs->pc = infop->entry;
1442 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1443 #define ELF_NREG 128
1444 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1446 enum {
1447 TARGET_REG_PC,
1448 TARGET_REG_PS,
1449 TARGET_REG_LBEG,
1450 TARGET_REG_LEND,
1451 TARGET_REG_LCOUNT,
1452 TARGET_REG_SAR,
1453 TARGET_REG_WINDOWSTART,
1454 TARGET_REG_WINDOWBASE,
1455 TARGET_REG_THREADPTR,
1456 TARGET_REG_AR0 = 64,
1459 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1460 const CPUXtensaState *env)
1462 unsigned i;
1464 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1465 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1466 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1467 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1468 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1469 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1470 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1471 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1472 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1473 xtensa_sync_phys_from_window((CPUXtensaState *)env);
1474 for (i = 0; i < env->config->nareg; ++i) {
1475 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1479 #define USE_ELF_CORE_DUMP
1480 #define ELF_EXEC_PAGESIZE 4096
1482 #endif /* TARGET_XTENSA */
1484 #ifdef TARGET_HEXAGON
1486 #define ELF_START_MMAP 0x20000000
1488 #define ELF_CLASS ELFCLASS32
1489 #define ELF_ARCH EM_HEXAGON
1491 static inline void init_thread(struct target_pt_regs *regs,
1492 struct image_info *infop)
1494 regs->sepc = infop->entry;
1495 regs->sp = infop->start_stack;
1498 #endif /* TARGET_HEXAGON */
1500 #ifndef ELF_PLATFORM
1501 #define ELF_PLATFORM (NULL)
1502 #endif
1504 #ifndef ELF_MACHINE
1505 #define ELF_MACHINE ELF_ARCH
1506 #endif
1508 #ifndef elf_check_arch
1509 #define elf_check_arch(x) ((x) == ELF_ARCH)
1510 #endif
1512 #ifndef elf_check_abi
1513 #define elf_check_abi(x) (1)
1514 #endif
1516 #ifndef ELF_HWCAP
1517 #define ELF_HWCAP 0
1518 #endif
1520 #ifndef STACK_GROWS_DOWN
1521 #define STACK_GROWS_DOWN 1
1522 #endif
1524 #ifndef STACK_ALIGNMENT
1525 #define STACK_ALIGNMENT 16
1526 #endif
1528 #ifdef TARGET_ABI32
1529 #undef ELF_CLASS
1530 #define ELF_CLASS ELFCLASS32
1531 #undef bswaptls
1532 #define bswaptls(ptr) bswap32s(ptr)
1533 #endif
1535 #include "elf.h"
1537 /* We must delay the following stanzas until after "elf.h". */
1538 #if defined(TARGET_AARCH64)
1540 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1541 const uint32_t *data,
1542 struct image_info *info,
1543 Error **errp)
1545 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
1546 if (pr_datasz != sizeof(uint32_t)) {
1547 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
1548 return false;
1550 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
1551 info->note_flags = *data;
1553 return true;
1555 #define ARCH_USE_GNU_PROPERTY 1
1557 #else
1559 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1560 const uint32_t *data,
1561 struct image_info *info,
1562 Error **errp)
1564 g_assert_not_reached();
1566 #define ARCH_USE_GNU_PROPERTY 0
1568 #endif
1570 struct exec
1572 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1573 unsigned int a_text; /* length of text, in bytes */
1574 unsigned int a_data; /* length of data, in bytes */
1575 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1576 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1577 unsigned int a_entry; /* start address */
1578 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1579 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1583 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1584 #define OMAGIC 0407
1585 #define NMAGIC 0410
1586 #define ZMAGIC 0413
1587 #define QMAGIC 0314
1589 /* Necessary parameters */
1590 #define TARGET_ELF_EXEC_PAGESIZE \
1591 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1592 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1593 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1594 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1595 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1596 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1598 #define DLINFO_ITEMS 16
1600 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1602 memcpy(to, from, n);
1605 #ifdef BSWAP_NEEDED
1606 static void bswap_ehdr(struct elfhdr *ehdr)
1608 bswap16s(&ehdr->e_type); /* Object file type */
1609 bswap16s(&ehdr->e_machine); /* Architecture */
1610 bswap32s(&ehdr->e_version); /* Object file version */
1611 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1612 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1613 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1614 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1615 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1616 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1617 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1618 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1619 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1620 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1623 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1625 int i;
1626 for (i = 0; i < phnum; ++i, ++phdr) {
1627 bswap32s(&phdr->p_type); /* Segment type */
1628 bswap32s(&phdr->p_flags); /* Segment flags */
1629 bswaptls(&phdr->p_offset); /* Segment file offset */
1630 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1631 bswaptls(&phdr->p_paddr); /* Segment physical address */
1632 bswaptls(&phdr->p_filesz); /* Segment size in file */
1633 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1634 bswaptls(&phdr->p_align); /* Segment alignment */
1638 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1640 int i;
1641 for (i = 0; i < shnum; ++i, ++shdr) {
1642 bswap32s(&shdr->sh_name);
1643 bswap32s(&shdr->sh_type);
1644 bswaptls(&shdr->sh_flags);
1645 bswaptls(&shdr->sh_addr);
1646 bswaptls(&shdr->sh_offset);
1647 bswaptls(&shdr->sh_size);
1648 bswap32s(&shdr->sh_link);
1649 bswap32s(&shdr->sh_info);
1650 bswaptls(&shdr->sh_addralign);
1651 bswaptls(&shdr->sh_entsize);
1655 static void bswap_sym(struct elf_sym *sym)
1657 bswap32s(&sym->st_name);
1658 bswaptls(&sym->st_value);
1659 bswaptls(&sym->st_size);
1660 bswap16s(&sym->st_shndx);
1663 #ifdef TARGET_MIPS
1664 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
1666 bswap16s(&abiflags->version);
1667 bswap32s(&abiflags->ases);
1668 bswap32s(&abiflags->isa_ext);
1669 bswap32s(&abiflags->flags1);
1670 bswap32s(&abiflags->flags2);
1672 #endif
1673 #else
1674 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1675 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1676 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1677 static inline void bswap_sym(struct elf_sym *sym) { }
1678 #ifdef TARGET_MIPS
1679 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
1680 #endif
1681 #endif
1683 #ifdef USE_ELF_CORE_DUMP
1684 static int elf_core_dump(int, const CPUArchState *);
1685 #endif /* USE_ELF_CORE_DUMP */
1686 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1688 /* Verify the portions of EHDR within E_IDENT for the target.
1689 This can be performed before bswapping the entire header. */
1690 static bool elf_check_ident(struct elfhdr *ehdr)
1692 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1693 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1694 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1695 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1696 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1697 && ehdr->e_ident[EI_DATA] == ELF_DATA
1698 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1701 /* Verify the portions of EHDR outside of E_IDENT for the target.
1702 This has to wait until after bswapping the header. */
1703 static bool elf_check_ehdr(struct elfhdr *ehdr)
1705 return (elf_check_arch(ehdr->e_machine)
1706 && elf_check_abi(ehdr->e_flags)
1707 && ehdr->e_ehsize == sizeof(struct elfhdr)
1708 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1709 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1713 * 'copy_elf_strings()' copies argument/envelope strings from user
1714 * memory to free pages in kernel mem. These are in a format ready
1715 * to be put directly into the top of new user memory.
1718 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1719 abi_ulong p, abi_ulong stack_limit)
1721 char *tmp;
1722 int len, i;
1723 abi_ulong top = p;
1725 if (!p) {
1726 return 0; /* bullet-proofing */
1729 if (STACK_GROWS_DOWN) {
1730 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1731 for (i = argc - 1; i >= 0; --i) {
1732 tmp = argv[i];
1733 if (!tmp) {
1734 fprintf(stderr, "VFS: argc is wrong");
1735 exit(-1);
1737 len = strlen(tmp) + 1;
1738 tmp += len;
1740 if (len > (p - stack_limit)) {
1741 return 0;
1743 while (len) {
1744 int bytes_to_copy = (len > offset) ? offset : len;
1745 tmp -= bytes_to_copy;
1746 p -= bytes_to_copy;
1747 offset -= bytes_to_copy;
1748 len -= bytes_to_copy;
1750 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1752 if (offset == 0) {
1753 memcpy_to_target(p, scratch, top - p);
1754 top = p;
1755 offset = TARGET_PAGE_SIZE;
1759 if (p != top) {
1760 memcpy_to_target(p, scratch + offset, top - p);
1762 } else {
1763 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1764 for (i = 0; i < argc; ++i) {
1765 tmp = argv[i];
1766 if (!tmp) {
1767 fprintf(stderr, "VFS: argc is wrong");
1768 exit(-1);
1770 len = strlen(tmp) + 1;
1771 if (len > (stack_limit - p)) {
1772 return 0;
1774 while (len) {
1775 int bytes_to_copy = (len > remaining) ? remaining : len;
1777 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1779 tmp += bytes_to_copy;
1780 remaining -= bytes_to_copy;
1781 p += bytes_to_copy;
1782 len -= bytes_to_copy;
1784 if (remaining == 0) {
1785 memcpy_to_target(top, scratch, p - top);
1786 top = p;
1787 remaining = TARGET_PAGE_SIZE;
1791 if (p != top) {
1792 memcpy_to_target(top, scratch, p - top);
1796 return p;
1799 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1800 * argument/environment space. Newer kernels (>2.6.33) allow more,
1801 * dependent on stack size, but guarantee at least 32 pages for
1802 * backwards compatibility.
1804 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1806 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1807 struct image_info *info)
1809 abi_ulong size, error, guard;
1811 size = guest_stack_size;
1812 if (size < STACK_LOWER_LIMIT) {
1813 size = STACK_LOWER_LIMIT;
1815 guard = TARGET_PAGE_SIZE;
1816 if (guard < qemu_real_host_page_size) {
1817 guard = qemu_real_host_page_size;
1820 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1821 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1822 if (error == -1) {
1823 perror("mmap stack");
1824 exit(-1);
1827 /* We reserve one extra page at the top of the stack as guard. */
1828 if (STACK_GROWS_DOWN) {
1829 target_mprotect(error, guard, PROT_NONE);
1830 info->stack_limit = error + guard;
1831 return info->stack_limit + size - sizeof(void *);
1832 } else {
1833 target_mprotect(error + size, guard, PROT_NONE);
1834 info->stack_limit = error + size;
1835 return error;
1839 /* Map and zero the bss. We need to explicitly zero any fractional pages
1840 after the data section (i.e. bss). */
1841 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1843 uintptr_t host_start, host_map_start, host_end;
1845 last_bss = TARGET_PAGE_ALIGN(last_bss);
1847 /* ??? There is confusion between qemu_real_host_page_size and
1848 qemu_host_page_size here and elsewhere in target_mmap, which
1849 may lead to the end of the data section mapping from the file
1850 not being mapped. At least there was an explicit test and
1851 comment for that here, suggesting that "the file size must
1852 be known". The comment probably pre-dates the introduction
1853 of the fstat system call in target_mmap which does in fact
1854 find out the size. What isn't clear is if the workaround
1855 here is still actually needed. For now, continue with it,
1856 but merge it with the "normal" mmap that would allocate the bss. */
1858 host_start = (uintptr_t) g2h_untagged(elf_bss);
1859 host_end = (uintptr_t) g2h_untagged(last_bss);
1860 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1862 if (host_map_start < host_end) {
1863 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1864 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1865 if (p == MAP_FAILED) {
1866 perror("cannot mmap brk");
1867 exit(-1);
1871 /* Ensure that the bss page(s) are valid */
1872 if ((page_get_flags(last_bss-1) & prot) != prot) {
1873 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1876 if (host_start < host_map_start) {
1877 memset((void *)host_start, 0, host_map_start - host_start);
1881 #ifdef TARGET_ARM
1882 static int elf_is_fdpic(struct elfhdr *exec)
1884 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
1886 #else
1887 /* Default implementation, always false. */
1888 static int elf_is_fdpic(struct elfhdr *exec)
1890 return 0;
1892 #endif
1894 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1896 uint16_t n;
1897 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1899 /* elf32_fdpic_loadseg */
1900 n = info->nsegs;
1901 while (n--) {
1902 sp -= 12;
1903 put_user_u32(loadsegs[n].addr, sp+0);
1904 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1905 put_user_u32(loadsegs[n].p_memsz, sp+8);
1908 /* elf32_fdpic_loadmap */
1909 sp -= 4;
1910 put_user_u16(0, sp+0); /* version */
1911 put_user_u16(info->nsegs, sp+2); /* nsegs */
1913 info->personality = PER_LINUX_FDPIC;
1914 info->loadmap_addr = sp;
1916 return sp;
1919 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1920 struct elfhdr *exec,
1921 struct image_info *info,
1922 struct image_info *interp_info)
1924 abi_ulong sp;
1925 abi_ulong u_argc, u_argv, u_envp, u_auxv;
1926 int size;
1927 int i;
1928 abi_ulong u_rand_bytes;
1929 uint8_t k_rand_bytes[16];
1930 abi_ulong u_platform;
1931 const char *k_platform;
1932 const int n = sizeof(elf_addr_t);
1934 sp = p;
1936 /* Needs to be before we load the env/argc/... */
1937 if (elf_is_fdpic(exec)) {
1938 /* Need 4 byte alignment for these structs */
1939 sp &= ~3;
1940 sp = loader_build_fdpic_loadmap(info, sp);
1941 info->other_info = interp_info;
1942 if (interp_info) {
1943 interp_info->other_info = info;
1944 sp = loader_build_fdpic_loadmap(interp_info, sp);
1945 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
1946 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
1947 } else {
1948 info->interpreter_loadmap_addr = 0;
1949 info->interpreter_pt_dynamic_addr = 0;
1953 u_platform = 0;
1954 k_platform = ELF_PLATFORM;
1955 if (k_platform) {
1956 size_t len = strlen(k_platform) + 1;
1957 if (STACK_GROWS_DOWN) {
1958 sp -= (len + n - 1) & ~(n - 1);
1959 u_platform = sp;
1960 /* FIXME - check return value of memcpy_to_target() for failure */
1961 memcpy_to_target(sp, k_platform, len);
1962 } else {
1963 memcpy_to_target(sp, k_platform, len);
1964 u_platform = sp;
1965 sp += len + 1;
1969 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1970 * the argv and envp pointers.
1972 if (STACK_GROWS_DOWN) {
1973 sp = QEMU_ALIGN_DOWN(sp, 16);
1974 } else {
1975 sp = QEMU_ALIGN_UP(sp, 16);
1979 * Generate 16 random bytes for userspace PRNG seeding.
1981 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
1982 if (STACK_GROWS_DOWN) {
1983 sp -= 16;
1984 u_rand_bytes = sp;
1985 /* FIXME - check return value of memcpy_to_target() for failure */
1986 memcpy_to_target(sp, k_rand_bytes, 16);
1987 } else {
1988 memcpy_to_target(sp, k_rand_bytes, 16);
1989 u_rand_bytes = sp;
1990 sp += 16;
1993 size = (DLINFO_ITEMS + 1) * 2;
1994 if (k_platform)
1995 size += 2;
1996 #ifdef DLINFO_ARCH_ITEMS
1997 size += DLINFO_ARCH_ITEMS * 2;
1998 #endif
1999 #ifdef ELF_HWCAP2
2000 size += 2;
2001 #endif
2002 info->auxv_len = size * n;
2004 size += envc + argc + 2;
2005 size += 1; /* argc itself */
2006 size *= n;
2008 /* Allocate space and finalize stack alignment for entry now. */
2009 if (STACK_GROWS_DOWN) {
2010 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
2011 sp = u_argc;
2012 } else {
2013 u_argc = sp;
2014 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2017 u_argv = u_argc + n;
2018 u_envp = u_argv + (argc + 1) * n;
2019 u_auxv = u_envp + (envc + 1) * n;
2020 info->saved_auxv = u_auxv;
2021 info->arg_start = u_argv;
2022 info->arg_end = u_argv + argc * n;
2024 /* This is correct because Linux defines
2025 * elf_addr_t as Elf32_Off / Elf64_Off
2027 #define NEW_AUX_ENT(id, val) do { \
2028 put_user_ual(id, u_auxv); u_auxv += n; \
2029 put_user_ual(val, u_auxv); u_auxv += n; \
2030 } while(0)
2032 #ifdef ARCH_DLINFO
2034 * ARCH_DLINFO must come first so platform specific code can enforce
2035 * special alignment requirements on the AUXV if necessary (eg. PPC).
2037 ARCH_DLINFO;
2038 #endif
2039 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2040 * on info->auxv_len will trigger.
2042 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2043 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2044 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2045 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2046 /* Target doesn't support host page size alignment */
2047 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2048 } else {
2049 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2050 qemu_host_page_size)));
2052 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2053 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2054 NEW_AUX_ENT(AT_ENTRY, info->entry);
2055 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2056 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2057 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2058 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2059 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2060 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2061 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2062 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2063 NEW_AUX_ENT(AT_EXECFN, info->file_string);
2065 #ifdef ELF_HWCAP2
2066 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2067 #endif
2069 if (u_platform) {
2070 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2072 NEW_AUX_ENT (AT_NULL, 0);
2073 #undef NEW_AUX_ENT
2075 /* Check that our initial calculation of the auxv length matches how much
2076 * we actually put into it.
2078 assert(info->auxv_len == u_auxv - info->saved_auxv);
2080 put_user_ual(argc, u_argc);
2082 p = info->arg_strings;
2083 for (i = 0; i < argc; ++i) {
2084 put_user_ual(p, u_argv);
2085 u_argv += n;
2086 p += target_strlen(p) + 1;
2088 put_user_ual(0, u_argv);
2090 p = info->env_strings;
2091 for (i = 0; i < envc; ++i) {
2092 put_user_ual(p, u_envp);
2093 u_envp += n;
2094 p += target_strlen(p) + 1;
2096 put_user_ual(0, u_envp);
2098 return sp;
2101 #ifndef ARM_COMMPAGE
2102 #define ARM_COMMPAGE 0
2103 #define init_guest_commpage() true
2104 #endif
2106 static void pgb_fail_in_use(const char *image_name)
2108 error_report("%s: requires virtual address space that is in use "
2109 "(omit the -B option or choose a different value)",
2110 image_name);
2111 exit(EXIT_FAILURE);
2114 static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
2115 abi_ulong guest_hiaddr, long align)
2117 const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2118 void *addr, *test;
2120 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2121 fprintf(stderr, "Requested guest base %p does not satisfy "
2122 "host minimum alignment (0x%lx)\n",
2123 (void *)guest_base, align);
2124 exit(EXIT_FAILURE);
2127 /* Sanity check the guest binary. */
2128 if (reserved_va) {
2129 if (guest_hiaddr > reserved_va) {
2130 error_report("%s: requires more than reserved virtual "
2131 "address space (0x%" PRIx64 " > 0x%lx)",
2132 image_name, (uint64_t)guest_hiaddr, reserved_va);
2133 exit(EXIT_FAILURE);
2135 } else {
2136 #if HOST_LONG_BITS < TARGET_ABI_BITS
2137 if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) {
2138 error_report("%s: requires more virtual address space "
2139 "than the host can provide (0x%" PRIx64 ")",
2140 image_name, (uint64_t)guest_hiaddr - guest_base);
2141 exit(EXIT_FAILURE);
2143 #endif
2147 * Expand the allocation to the entire reserved_va.
2148 * Exclude the mmap_min_addr hole.
2150 if (reserved_va) {
2151 guest_loaddr = (guest_base >= mmap_min_addr ? 0
2152 : mmap_min_addr - guest_base);
2153 guest_hiaddr = reserved_va;
2156 /* Reserve the address space for the binary, or reserved_va. */
2157 test = g2h_untagged(guest_loaddr);
2158 addr = mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0);
2159 if (test != addr) {
2160 pgb_fail_in_use(image_name);
2165 * pgd_find_hole_fallback: potential mmap address
2166 * @guest_size: size of available space
2167 * @brk: location of break
2168 * @align: memory alignment
2170 * This is a fallback method for finding a hole in the host address
2171 * space if we don't have the benefit of being able to access
2172 * /proc/self/map. It can potentially take a very long time as we can
2173 * only dumbly iterate up the host address space seeing if the
2174 * allocation would work.
2176 static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
2177 long align, uintptr_t offset)
2179 uintptr_t base;
2181 /* Start (aligned) at the bottom and work our way up */
2182 base = ROUND_UP(mmap_min_addr, align);
2184 while (true) {
2185 uintptr_t align_start, end;
2186 align_start = ROUND_UP(base, align);
2187 end = align_start + guest_size + offset;
2189 /* if brk is anywhere in the range give ourselves some room to grow. */
2190 if (align_start <= brk && brk < end) {
2191 base = brk + (16 * MiB);
2192 continue;
2193 } else if (align_start + guest_size < align_start) {
2194 /* we have run out of space */
2195 return -1;
2196 } else {
2197 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE |
2198 MAP_FIXED_NOREPLACE;
2199 void * mmap_start = mmap((void *) align_start, guest_size,
2200 PROT_NONE, flags, -1, 0);
2201 if (mmap_start != MAP_FAILED) {
2202 munmap(mmap_start, guest_size);
2203 if (mmap_start == (void *) align_start) {
2204 return (uintptr_t) mmap_start + offset;
2207 base += qemu_host_page_size;
2212 /* Return value for guest_base, or -1 if no hole found. */
2213 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
2214 long align, uintptr_t offset)
2216 GSList *maps, *iter;
2217 uintptr_t this_start, this_end, next_start, brk;
2218 intptr_t ret = -1;
2220 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
2222 maps = read_self_maps();
2224 /* Read brk after we've read the maps, which will malloc. */
2225 brk = (uintptr_t)sbrk(0);
2227 if (!maps) {
2228 ret = pgd_find_hole_fallback(guest_size, brk, align, offset);
2229 return ret == -1 ? -1 : ret - guest_loaddr;
2232 /* The first hole is before the first map entry. */
2233 this_start = mmap_min_addr;
2235 for (iter = maps; iter;
2236 this_start = next_start, iter = g_slist_next(iter)) {
2237 uintptr_t align_start, hole_size;
2239 this_end = ((MapInfo *)iter->data)->start;
2240 next_start = ((MapInfo *)iter->data)->end;
2241 align_start = ROUND_UP(this_start + offset, align);
2243 /* Skip holes that are too small. */
2244 if (align_start >= this_end) {
2245 continue;
2247 hole_size = this_end - align_start;
2248 if (hole_size < guest_size) {
2249 continue;
2252 /* If this hole contains brk, give ourselves some room to grow. */
2253 if (this_start <= brk && brk < this_end) {
2254 hole_size -= guest_size;
2255 if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) {
2256 align_start += 1 * GiB;
2257 } else if (hole_size >= 16 * MiB) {
2258 align_start += 16 * MiB;
2259 } else {
2260 align_start = (this_end - guest_size) & -align;
2261 if (align_start < this_start) {
2262 continue;
2267 /* Record the lowest successful match. */
2268 if (ret < 0) {
2269 ret = align_start - guest_loaddr;
2271 /* If this hole contains the identity map, select it. */
2272 if (align_start <= guest_loaddr &&
2273 guest_loaddr + guest_size <= this_end) {
2274 ret = 0;
2276 /* If this hole ends above the identity map, stop looking. */
2277 if (this_end >= guest_loaddr) {
2278 break;
2281 free_self_maps(maps);
2283 return ret;
2286 static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
2287 abi_ulong orig_hiaddr, long align)
2289 uintptr_t loaddr = orig_loaddr;
2290 uintptr_t hiaddr = orig_hiaddr;
2291 uintptr_t offset = 0;
2292 uintptr_t addr;
2294 if (hiaddr != orig_hiaddr) {
2295 error_report("%s: requires virtual address space that the "
2296 "host cannot provide (0x%" PRIx64 ")",
2297 image_name, (uint64_t)orig_hiaddr);
2298 exit(EXIT_FAILURE);
2301 loaddr &= -align;
2302 if (ARM_COMMPAGE) {
2304 * Extend the allocation to include the commpage.
2305 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2306 * need to ensure there is space bellow the guest_base so we
2307 * can map the commpage in the place needed when the address
2308 * arithmetic wraps around.
2310 if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
2311 hiaddr = (uintptr_t) 4 << 30;
2312 } else {
2313 offset = -(ARM_COMMPAGE & -align);
2317 addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
2318 if (addr == -1) {
2320 * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
2321 * that can satisfy both. But as the normal arm32 link base address
2322 * is ~32k, and we extend down to include the commpage, making the
2323 * overhead only ~96k, this is unlikely.
2325 error_report("%s: Unable to allocate %#zx bytes of "
2326 "virtual address space", image_name,
2327 (size_t)(hiaddr - loaddr));
2328 exit(EXIT_FAILURE);
2331 guest_base = addr;
2334 static void pgb_dynamic(const char *image_name, long align)
2337 * The executable is dynamic and does not require a fixed address.
2338 * All we need is a commpage that satisfies align.
2339 * If we do not need a commpage, leave guest_base == 0.
2341 if (ARM_COMMPAGE) {
2342 uintptr_t addr, commpage;
2344 /* 64-bit hosts should have used reserved_va. */
2345 assert(sizeof(uintptr_t) == 4);
2348 * By putting the commpage at the first hole, that puts guest_base
2349 * just above that, and maximises the positive guest addresses.
2351 commpage = ARM_COMMPAGE & -align;
2352 addr = pgb_find_hole(commpage, -commpage, align, 0);
2353 assert(addr != -1);
2354 guest_base = addr;
2358 static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
2359 abi_ulong guest_hiaddr, long align)
2361 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2362 void *addr, *test;
2364 if (guest_hiaddr > reserved_va) {
2365 error_report("%s: requires more than reserved virtual "
2366 "address space (0x%" PRIx64 " > 0x%lx)",
2367 image_name, (uint64_t)guest_hiaddr, reserved_va);
2368 exit(EXIT_FAILURE);
2371 /* Widen the "image" to the entire reserved address space. */
2372 pgb_static(image_name, 0, reserved_va, align);
2374 /* osdep.h defines this as 0 if it's missing */
2375 flags |= MAP_FIXED_NOREPLACE;
2377 /* Reserve the memory on the host. */
2378 assert(guest_base != 0);
2379 test = g2h_untagged(0);
2380 addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0);
2381 if (addr == MAP_FAILED || addr != test) {
2382 error_report("Unable to reserve 0x%lx bytes of virtual address "
2383 "space at %p (%s) for use as guest address space (check your"
2384 "virtual memory ulimit setting, min_mmap_addr or reserve less "
2385 "using -R option)", reserved_va, test, strerror(errno));
2386 exit(EXIT_FAILURE);
2390 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
2391 abi_ulong guest_hiaddr)
2393 /* In order to use host shmat, we must be able to honor SHMLBA. */
2394 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
2396 if (have_guest_base) {
2397 pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align);
2398 } else if (reserved_va) {
2399 pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align);
2400 } else if (guest_loaddr) {
2401 pgb_static(image_name, guest_loaddr, guest_hiaddr, align);
2402 } else {
2403 pgb_dynamic(image_name, align);
2406 /* Reserve and initialize the commpage. */
2407 if (!init_guest_commpage()) {
2409 * With have_guest_base, the user has selected the address and
2410 * we are trying to work with that. Otherwise, we have selected
2411 * free space and init_guest_commpage must succeeded.
2413 assert(have_guest_base);
2414 pgb_fail_in_use(image_name);
2417 assert(QEMU_IS_ALIGNED(guest_base, align));
2418 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
2419 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
2422 enum {
2423 /* The string "GNU\0" as a magic number. */
2424 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
2425 NOTE_DATA_SZ = 1 * KiB,
2426 NOTE_NAME_SZ = 4,
2427 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
2431 * Process a single gnu_property entry.
2432 * Return false for error.
2434 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
2435 struct image_info *info, bool have_prev_type,
2436 uint32_t *prev_type, Error **errp)
2438 uint32_t pr_type, pr_datasz, step;
2440 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
2441 goto error_data;
2443 datasz -= *off;
2444 data += *off / sizeof(uint32_t);
2446 if (datasz < 2 * sizeof(uint32_t)) {
2447 goto error_data;
2449 pr_type = data[0];
2450 pr_datasz = data[1];
2451 data += 2;
2452 datasz -= 2 * sizeof(uint32_t);
2453 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
2454 if (step > datasz) {
2455 goto error_data;
2458 /* Properties are supposed to be unique and sorted on pr_type. */
2459 if (have_prev_type && pr_type <= *prev_type) {
2460 if (pr_type == *prev_type) {
2461 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
2462 } else {
2463 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
2465 return false;
2467 *prev_type = pr_type;
2469 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
2470 return false;
2473 *off += 2 * sizeof(uint32_t) + step;
2474 return true;
2476 error_data:
2477 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
2478 return false;
2481 /* Process NT_GNU_PROPERTY_TYPE_0. */
2482 static bool parse_elf_properties(int image_fd,
2483 struct image_info *info,
2484 const struct elf_phdr *phdr,
2485 char bprm_buf[BPRM_BUF_SIZE],
2486 Error **errp)
2488 union {
2489 struct elf_note nhdr;
2490 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
2491 } note;
2493 int n, off, datasz;
2494 bool have_prev_type;
2495 uint32_t prev_type;
2497 /* Unless the arch requires properties, ignore them. */
2498 if (!ARCH_USE_GNU_PROPERTY) {
2499 return true;
2502 /* If the properties are crazy large, that's too bad. */
2503 n = phdr->p_filesz;
2504 if (n > sizeof(note)) {
2505 error_setg(errp, "PT_GNU_PROPERTY too large");
2506 return false;
2508 if (n < sizeof(note.nhdr)) {
2509 error_setg(errp, "PT_GNU_PROPERTY too small");
2510 return false;
2513 if (phdr->p_offset + n <= BPRM_BUF_SIZE) {
2514 memcpy(&note, bprm_buf + phdr->p_offset, n);
2515 } else {
2516 ssize_t len = pread(image_fd, &note, n, phdr->p_offset);
2517 if (len != n) {
2518 error_setg_errno(errp, errno, "Error reading file header");
2519 return false;
2524 * The contents of a valid PT_GNU_PROPERTY is a sequence
2525 * of uint32_t -- swap them all now.
2527 #ifdef BSWAP_NEEDED
2528 for (int i = 0; i < n / 4; i++) {
2529 bswap32s(note.data + i);
2531 #endif
2534 * Note that nhdr is 3 words, and that the "name" described by namesz
2535 * immediately follows nhdr and is thus at the 4th word. Further, all
2536 * of the inputs to the kernel's round_up are multiples of 4.
2538 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
2539 note.nhdr.n_namesz != NOTE_NAME_SZ ||
2540 note.data[3] != GNU0_MAGIC) {
2541 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
2542 return false;
2544 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
2546 datasz = note.nhdr.n_descsz + off;
2547 if (datasz > n) {
2548 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
2549 return false;
2552 have_prev_type = false;
2553 prev_type = 0;
2554 while (1) {
2555 if (off == datasz) {
2556 return true; /* end, exit ok */
2558 if (!parse_elf_property(note.data, &off, datasz, info,
2559 have_prev_type, &prev_type, errp)) {
2560 return false;
2562 have_prev_type = true;
2566 /* Load an ELF image into the address space.
2568 IMAGE_NAME is the filename of the image, to use in error messages.
2569 IMAGE_FD is the open file descriptor for the image.
2571 BPRM_BUF is a copy of the beginning of the file; this of course
2572 contains the elf file header at offset 0. It is assumed that this
2573 buffer is sufficiently aligned to present no problems to the host
2574 in accessing data at aligned offsets within the buffer.
2576 On return: INFO values will be filled in, as necessary or available. */
2578 static void load_elf_image(const char *image_name, int image_fd,
2579 struct image_info *info, char **pinterp_name,
2580 char bprm_buf[BPRM_BUF_SIZE])
2582 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2583 struct elf_phdr *phdr;
2584 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
2585 int i, retval, prot_exec;
2586 Error *err = NULL;
2588 /* First of all, some simple consistency checks */
2589 if (!elf_check_ident(ehdr)) {
2590 error_setg(&err, "Invalid ELF image for this architecture");
2591 goto exit_errmsg;
2593 bswap_ehdr(ehdr);
2594 if (!elf_check_ehdr(ehdr)) {
2595 error_setg(&err, "Invalid ELF image for this architecture");
2596 goto exit_errmsg;
2599 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2600 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2601 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
2602 } else {
2603 phdr = (struct elf_phdr *) alloca(i);
2604 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
2605 if (retval != i) {
2606 goto exit_read;
2609 bswap_phdr(phdr, ehdr->e_phnum);
2611 info->nsegs = 0;
2612 info->pt_dynamic_addr = 0;
2614 mmap_lock();
2617 * Find the maximum size of the image and allocate an appropriate
2618 * amount of memory to handle that. Locate the interpreter, if any.
2620 loaddr = -1, hiaddr = 0;
2621 info->alignment = 0;
2622 for (i = 0; i < ehdr->e_phnum; ++i) {
2623 struct elf_phdr *eppnt = phdr + i;
2624 if (eppnt->p_type == PT_LOAD) {
2625 abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
2626 if (a < loaddr) {
2627 loaddr = a;
2629 a = eppnt->p_vaddr + eppnt->p_memsz;
2630 if (a > hiaddr) {
2631 hiaddr = a;
2633 ++info->nsegs;
2634 info->alignment |= eppnt->p_align;
2635 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2636 g_autofree char *interp_name = NULL;
2638 if (*pinterp_name) {
2639 error_setg(&err, "Multiple PT_INTERP entries");
2640 goto exit_errmsg;
2643 interp_name = g_malloc(eppnt->p_filesz);
2645 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2646 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2647 eppnt->p_filesz);
2648 } else {
2649 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2650 eppnt->p_offset);
2651 if (retval != eppnt->p_filesz) {
2652 goto exit_read;
2655 if (interp_name[eppnt->p_filesz - 1] != 0) {
2656 error_setg(&err, "Invalid PT_INTERP entry");
2657 goto exit_errmsg;
2659 *pinterp_name = g_steal_pointer(&interp_name);
2660 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
2661 if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &err)) {
2662 goto exit_errmsg;
2667 if (pinterp_name != NULL) {
2669 * This is the main executable.
2671 * Reserve extra space for brk.
2672 * We hold on to this space while placing the interpreter
2673 * and the stack, lest they be placed immediately after
2674 * the data segment and block allocation from the brk.
2676 * 16MB is chosen as "large enough" without being so large
2677 * as to allow the result to not fit with a 32-bit guest on
2678 * a 32-bit host.
2680 info->reserve_brk = 16 * MiB;
2681 hiaddr += info->reserve_brk;
2683 if (ehdr->e_type == ET_EXEC) {
2685 * Make sure that the low address does not conflict with
2686 * MMAP_MIN_ADDR or the QEMU application itself.
2688 probe_guest_base(image_name, loaddr, hiaddr);
2689 } else {
2691 * The binary is dynamic, but we still need to
2692 * select guest_base. In this case we pass a size.
2694 probe_guest_base(image_name, 0, hiaddr - loaddr);
2699 * Reserve address space for all of this.
2701 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
2702 * exactly the address range that is required.
2704 * Otherwise this is ET_DYN, and we are searching for a location
2705 * that can hold the memory space required. If the image is
2706 * pre-linked, LOADDR will be non-zero, and the kernel should
2707 * honor that address if it happens to be free.
2709 * In both cases, we will overwrite pages in this range with mappings
2710 * from the executable.
2712 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2713 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
2714 (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0),
2715 -1, 0);
2716 if (load_addr == -1) {
2717 goto exit_mmap;
2719 load_bias = load_addr - loaddr;
2721 if (elf_is_fdpic(ehdr)) {
2722 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2723 g_malloc(sizeof(*loadsegs) * info->nsegs);
2725 for (i = 0; i < ehdr->e_phnum; ++i) {
2726 switch (phdr[i].p_type) {
2727 case PT_DYNAMIC:
2728 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2729 break;
2730 case PT_LOAD:
2731 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2732 loadsegs->p_vaddr = phdr[i].p_vaddr;
2733 loadsegs->p_memsz = phdr[i].p_memsz;
2734 ++loadsegs;
2735 break;
2740 info->load_bias = load_bias;
2741 info->code_offset = load_bias;
2742 info->data_offset = load_bias;
2743 info->load_addr = load_addr;
2744 info->entry = ehdr->e_entry + load_bias;
2745 info->start_code = -1;
2746 info->end_code = 0;
2747 info->start_data = -1;
2748 info->end_data = 0;
2749 info->brk = 0;
2750 info->elf_flags = ehdr->e_flags;
2752 prot_exec = PROT_EXEC;
2753 #ifdef TARGET_AARCH64
2755 * If the BTI feature is present, this indicates that the executable
2756 * pages of the startup binary should be mapped with PROT_BTI, so that
2757 * branch targets are enforced.
2759 * The startup binary is either the interpreter or the static executable.
2760 * The interpreter is responsible for all pages of a dynamic executable.
2762 * Elf notes are backward compatible to older cpus.
2763 * Do not enable BTI unless it is supported.
2765 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
2766 && (pinterp_name == NULL || *pinterp_name == 0)
2767 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
2768 prot_exec |= TARGET_PROT_BTI;
2770 #endif
2772 for (i = 0; i < ehdr->e_phnum; i++) {
2773 struct elf_phdr *eppnt = phdr + i;
2774 if (eppnt->p_type == PT_LOAD) {
2775 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
2776 int elf_prot = 0;
2778 if (eppnt->p_flags & PF_R) {
2779 elf_prot |= PROT_READ;
2781 if (eppnt->p_flags & PF_W) {
2782 elf_prot |= PROT_WRITE;
2784 if (eppnt->p_flags & PF_X) {
2785 elf_prot |= prot_exec;
2788 vaddr = load_bias + eppnt->p_vaddr;
2789 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2790 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2792 vaddr_ef = vaddr + eppnt->p_filesz;
2793 vaddr_em = vaddr + eppnt->p_memsz;
2796 * Some segments may be completely empty, with a non-zero p_memsz
2797 * but no backing file segment.
2799 if (eppnt->p_filesz != 0) {
2800 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
2801 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2802 MAP_PRIVATE | MAP_FIXED,
2803 image_fd, eppnt->p_offset - vaddr_po);
2805 if (error == -1) {
2806 goto exit_mmap;
2810 * If the load segment requests extra zeros (e.g. bss), map it.
2812 if (eppnt->p_filesz < eppnt->p_memsz) {
2813 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2815 } else if (eppnt->p_memsz != 0) {
2816 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_memsz + vaddr_po);
2817 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2818 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
2819 -1, 0);
2821 if (error == -1) {
2822 goto exit_mmap;
2826 /* Find the full program boundaries. */
2827 if (elf_prot & PROT_EXEC) {
2828 if (vaddr < info->start_code) {
2829 info->start_code = vaddr;
2831 if (vaddr_ef > info->end_code) {
2832 info->end_code = vaddr_ef;
2835 if (elf_prot & PROT_WRITE) {
2836 if (vaddr < info->start_data) {
2837 info->start_data = vaddr;
2839 if (vaddr_ef > info->end_data) {
2840 info->end_data = vaddr_ef;
2843 if (vaddr_em > info->brk) {
2844 info->brk = vaddr_em;
2846 #ifdef TARGET_MIPS
2847 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
2848 Mips_elf_abiflags_v0 abiflags;
2849 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
2850 error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry");
2851 goto exit_errmsg;
2853 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2854 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
2855 sizeof(Mips_elf_abiflags_v0));
2856 } else {
2857 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
2858 eppnt->p_offset);
2859 if (retval != sizeof(Mips_elf_abiflags_v0)) {
2860 goto exit_read;
2863 bswap_mips_abiflags(&abiflags);
2864 info->fp_abi = abiflags.fp_abi;
2865 #endif
2869 if (info->end_data == 0) {
2870 info->start_data = info->end_code;
2871 info->end_data = info->end_code;
2874 if (qemu_log_enabled()) {
2875 load_symbols(ehdr, image_fd, load_bias);
2878 mmap_unlock();
2880 close(image_fd);
2881 return;
2883 exit_read:
2884 if (retval >= 0) {
2885 error_setg(&err, "Incomplete read of file header");
2886 } else {
2887 error_setg_errno(&err, errno, "Error reading file header");
2889 goto exit_errmsg;
2890 exit_mmap:
2891 error_setg_errno(&err, errno, "Error mapping file");
2892 goto exit_errmsg;
2893 exit_errmsg:
2894 error_reportf_err(err, "%s: ", image_name);
2895 exit(-1);
2898 static void load_elf_interp(const char *filename, struct image_info *info,
2899 char bprm_buf[BPRM_BUF_SIZE])
2901 int fd, retval;
2902 Error *err = NULL;
2904 fd = open(path(filename), O_RDONLY);
2905 if (fd < 0) {
2906 error_setg_file_open(&err, errno, filename);
2907 error_report_err(err);
2908 exit(-1);
2911 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2912 if (retval < 0) {
2913 error_setg_errno(&err, errno, "Error reading file header");
2914 error_reportf_err(err, "%s: ", filename);
2915 exit(-1);
2918 if (retval < BPRM_BUF_SIZE) {
2919 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2922 load_elf_image(filename, fd, info, NULL, bprm_buf);
2925 static int symfind(const void *s0, const void *s1)
2927 target_ulong addr = *(target_ulong *)s0;
2928 struct elf_sym *sym = (struct elf_sym *)s1;
2929 int result = 0;
2930 if (addr < sym->st_value) {
2931 result = -1;
2932 } else if (addr >= sym->st_value + sym->st_size) {
2933 result = 1;
2935 return result;
2938 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2940 #if ELF_CLASS == ELFCLASS32
2941 struct elf_sym *syms = s->disas_symtab.elf32;
2942 #else
2943 struct elf_sym *syms = s->disas_symtab.elf64;
2944 #endif
2946 // binary search
2947 struct elf_sym *sym;
2949 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2950 if (sym != NULL) {
2951 return s->disas_strtab + sym->st_name;
2954 return "";
2957 /* FIXME: This should use elf_ops.h */
2958 static int symcmp(const void *s0, const void *s1)
2960 struct elf_sym *sym0 = (struct elf_sym *)s0;
2961 struct elf_sym *sym1 = (struct elf_sym *)s1;
2962 return (sym0->st_value < sym1->st_value)
2963 ? -1
2964 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2967 /* Best attempt to load symbols from this ELF object. */
2968 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2970 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2971 uint64_t segsz;
2972 struct elf_shdr *shdr;
2973 char *strings = NULL;
2974 struct syminfo *s = NULL;
2975 struct elf_sym *new_syms, *syms = NULL;
2977 shnum = hdr->e_shnum;
2978 i = shnum * sizeof(struct elf_shdr);
2979 shdr = (struct elf_shdr *)alloca(i);
2980 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2981 return;
2984 bswap_shdr(shdr, shnum);
2985 for (i = 0; i < shnum; ++i) {
2986 if (shdr[i].sh_type == SHT_SYMTAB) {
2987 sym_idx = i;
2988 str_idx = shdr[i].sh_link;
2989 goto found;
2993 /* There will be no symbol table if the file was stripped. */
2994 return;
2996 found:
2997 /* Now know where the strtab and symtab are. Snarf them. */
2998 s = g_try_new(struct syminfo, 1);
2999 if (!s) {
3000 goto give_up;
3003 segsz = shdr[str_idx].sh_size;
3004 s->disas_strtab = strings = g_try_malloc(segsz);
3005 if (!strings ||
3006 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
3007 goto give_up;
3010 segsz = shdr[sym_idx].sh_size;
3011 syms = g_try_malloc(segsz);
3012 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
3013 goto give_up;
3016 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
3017 /* Implausibly large symbol table: give up rather than ploughing
3018 * on with the number of symbols calculation overflowing
3020 goto give_up;
3022 nsyms = segsz / sizeof(struct elf_sym);
3023 for (i = 0; i < nsyms; ) {
3024 bswap_sym(syms + i);
3025 /* Throw away entries which we do not need. */
3026 if (syms[i].st_shndx == SHN_UNDEF
3027 || syms[i].st_shndx >= SHN_LORESERVE
3028 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3029 if (i < --nsyms) {
3030 syms[i] = syms[nsyms];
3032 } else {
3033 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3034 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3035 syms[i].st_value &= ~(target_ulong)1;
3036 #endif
3037 syms[i].st_value += load_bias;
3038 i++;
3042 /* No "useful" symbol. */
3043 if (nsyms == 0) {
3044 goto give_up;
3047 /* Attempt to free the storage associated with the local symbols
3048 that we threw away. Whether or not this has any effect on the
3049 memory allocation depends on the malloc implementation and how
3050 many symbols we managed to discard. */
3051 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3052 if (new_syms == NULL) {
3053 goto give_up;
3055 syms = new_syms;
3057 qsort(syms, nsyms, sizeof(*syms), symcmp);
3059 s->disas_num_syms = nsyms;
3060 #if ELF_CLASS == ELFCLASS32
3061 s->disas_symtab.elf32 = syms;
3062 #else
3063 s->disas_symtab.elf64 = syms;
3064 #endif
3065 s->lookup_symbol = lookup_symbolxx;
3066 s->next = syminfos;
3067 syminfos = s;
3069 return;
3071 give_up:
3072 g_free(s);
3073 g_free(strings);
3074 g_free(syms);
3077 uint32_t get_elf_eflags(int fd)
3079 struct elfhdr ehdr;
3080 off_t offset;
3081 int ret;
3083 /* Read ELF header */
3084 offset = lseek(fd, 0, SEEK_SET);
3085 if (offset == (off_t) -1) {
3086 return 0;
3088 ret = read(fd, &ehdr, sizeof(ehdr));
3089 if (ret < sizeof(ehdr)) {
3090 return 0;
3092 offset = lseek(fd, offset, SEEK_SET);
3093 if (offset == (off_t) -1) {
3094 return 0;
3097 /* Check ELF signature */
3098 if (!elf_check_ident(&ehdr)) {
3099 return 0;
3102 /* check header */
3103 bswap_ehdr(&ehdr);
3104 if (!elf_check_ehdr(&ehdr)) {
3105 return 0;
3108 /* return architecture id */
3109 return ehdr.e_flags;
3112 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3114 struct image_info interp_info;
3115 struct elfhdr elf_ex;
3116 char *elf_interpreter = NULL;
3117 char *scratch;
3119 memset(&interp_info, 0, sizeof(interp_info));
3120 #ifdef TARGET_MIPS
3121 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3122 #endif
3124 info->start_mmap = (abi_ulong)ELF_START_MMAP;
3126 load_elf_image(bprm->filename, bprm->fd, info,
3127 &elf_interpreter, bprm->buf);
3129 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3130 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3131 when we load the interpreter. */
3132 elf_ex = *(struct elfhdr *)bprm->buf;
3134 /* Do this so that we can load the interpreter, if need be. We will
3135 change some of these later */
3136 bprm->p = setup_arg_pages(bprm, info);
3138 scratch = g_new0(char, TARGET_PAGE_SIZE);
3139 if (STACK_GROWS_DOWN) {
3140 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3141 bprm->p, info->stack_limit);
3142 info->file_string = bprm->p;
3143 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3144 bprm->p, info->stack_limit);
3145 info->env_strings = bprm->p;
3146 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3147 bprm->p, info->stack_limit);
3148 info->arg_strings = bprm->p;
3149 } else {
3150 info->arg_strings = bprm->p;
3151 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3152 bprm->p, info->stack_limit);
3153 info->env_strings = bprm->p;
3154 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3155 bprm->p, info->stack_limit);
3156 info->file_string = bprm->p;
3157 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3158 bprm->p, info->stack_limit);
3161 g_free(scratch);
3163 if (!bprm->p) {
3164 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3165 exit(-1);
3168 if (elf_interpreter) {
3169 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3171 /* If the program interpreter is one of these two, then assume
3172 an iBCS2 image. Otherwise assume a native linux image. */
3174 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3175 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3176 info->personality = PER_SVR4;
3178 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3179 and some applications "depend" upon this behavior. Since
3180 we do not have the power to recompile these, we emulate
3181 the SVr4 behavior. Sigh. */
3182 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
3183 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3185 #ifdef TARGET_MIPS
3186 info->interp_fp_abi = interp_info.fp_abi;
3187 #endif
3190 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
3191 info, (elf_interpreter ? &interp_info : NULL));
3192 info->start_stack = bprm->p;
3194 /* If we have an interpreter, set that as the program's entry point.
3195 Copy the load_bias as well, to help PPC64 interpret the entry
3196 point as a function descriptor. Do this after creating elf tables
3197 so that we copy the original program entry point into the AUXV. */
3198 if (elf_interpreter) {
3199 info->load_bias = interp_info.load_bias;
3200 info->entry = interp_info.entry;
3201 g_free(elf_interpreter);
3204 #ifdef USE_ELF_CORE_DUMP
3205 bprm->core_dump = &elf_core_dump;
3206 #endif
3209 * If we reserved extra space for brk, release it now.
3210 * The implementation of do_brk in syscalls.c expects to be able
3211 * to mmap pages in this space.
3213 if (info->reserve_brk) {
3214 abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk);
3215 abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk);
3216 target_munmap(start_brk, end_brk - start_brk);
3219 return 0;
3222 #ifdef USE_ELF_CORE_DUMP
3224 * Definitions to generate Intel SVR4-like core files.
3225 * These mostly have the same names as the SVR4 types with "target_elf_"
3226 * tacked on the front to prevent clashes with linux definitions,
3227 * and the typedef forms have been avoided. This is mostly like
3228 * the SVR4 structure, but more Linuxy, with things that Linux does
3229 * not support and which gdb doesn't really use excluded.
3231 * Fields we don't dump (their contents is zero) in linux-user qemu
3232 * are marked with XXX.
3234 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3236 * Porting ELF coredump for target is (quite) simple process. First you
3237 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3238 * the target resides):
3240 * #define USE_ELF_CORE_DUMP
3242 * Next you define type of register set used for dumping. ELF specification
3243 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3245 * typedef <target_regtype> target_elf_greg_t;
3246 * #define ELF_NREG <number of registers>
3247 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3249 * Last step is to implement target specific function that copies registers
3250 * from given cpu into just specified register set. Prototype is:
3252 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3253 * const CPUArchState *env);
3255 * Parameters:
3256 * regs - copy register values into here (allocated and zeroed by caller)
3257 * env - copy registers from here
3259 * Example for ARM target is provided in this file.
3262 /* An ELF note in memory */
3263 struct memelfnote {
3264 const char *name;
3265 size_t namesz;
3266 size_t namesz_rounded;
3267 int type;
3268 size_t datasz;
3269 size_t datasz_rounded;
3270 void *data;
3271 size_t notesz;
3274 struct target_elf_siginfo {
3275 abi_int si_signo; /* signal number */
3276 abi_int si_code; /* extra code */
3277 abi_int si_errno; /* errno */
3280 struct target_elf_prstatus {
3281 struct target_elf_siginfo pr_info; /* Info associated with signal */
3282 abi_short pr_cursig; /* Current signal */
3283 abi_ulong pr_sigpend; /* XXX */
3284 abi_ulong pr_sighold; /* XXX */
3285 target_pid_t pr_pid;
3286 target_pid_t pr_ppid;
3287 target_pid_t pr_pgrp;
3288 target_pid_t pr_sid;
3289 struct target_timeval pr_utime; /* XXX User time */
3290 struct target_timeval pr_stime; /* XXX System time */
3291 struct target_timeval pr_cutime; /* XXX Cumulative user time */
3292 struct target_timeval pr_cstime; /* XXX Cumulative system time */
3293 target_elf_gregset_t pr_reg; /* GP registers */
3294 abi_int pr_fpvalid; /* XXX */
3297 #define ELF_PRARGSZ (80) /* Number of chars for args */
3299 struct target_elf_prpsinfo {
3300 char pr_state; /* numeric process state */
3301 char pr_sname; /* char for pr_state */
3302 char pr_zomb; /* zombie */
3303 char pr_nice; /* nice val */
3304 abi_ulong pr_flag; /* flags */
3305 target_uid_t pr_uid;
3306 target_gid_t pr_gid;
3307 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
3308 /* Lots missing */
3309 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
3310 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
3313 /* Here is the structure in which status of each thread is captured. */
3314 struct elf_thread_status {
3315 QTAILQ_ENTRY(elf_thread_status) ets_link;
3316 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
3317 #if 0
3318 elf_fpregset_t fpu; /* NT_PRFPREG */
3319 struct task_struct *thread;
3320 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
3321 #endif
3322 struct memelfnote notes[1];
3323 int num_notes;
3326 struct elf_note_info {
3327 struct memelfnote *notes;
3328 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
3329 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
3331 QTAILQ_HEAD(, elf_thread_status) thread_list;
3332 #if 0
3334 * Current version of ELF coredump doesn't support
3335 * dumping fp regs etc.
3337 elf_fpregset_t *fpu;
3338 elf_fpxregset_t *xfpu;
3339 int thread_status_size;
3340 #endif
3341 int notes_size;
3342 int numnote;
3345 struct vm_area_struct {
3346 target_ulong vma_start; /* start vaddr of memory region */
3347 target_ulong vma_end; /* end vaddr of memory region */
3348 abi_ulong vma_flags; /* protection etc. flags for the region */
3349 QTAILQ_ENTRY(vm_area_struct) vma_link;
3352 struct mm_struct {
3353 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
3354 int mm_count; /* number of mappings */
3357 static struct mm_struct *vma_init(void);
3358 static void vma_delete(struct mm_struct *);
3359 static int vma_add_mapping(struct mm_struct *, target_ulong,
3360 target_ulong, abi_ulong);
3361 static int vma_get_mapping_count(const struct mm_struct *);
3362 static struct vm_area_struct *vma_first(const struct mm_struct *);
3363 static struct vm_area_struct *vma_next(struct vm_area_struct *);
3364 static abi_ulong vma_dump_size(const struct vm_area_struct *);
3365 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3366 unsigned long flags);
3368 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
3369 static void fill_note(struct memelfnote *, const char *, int,
3370 unsigned int, void *);
3371 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
3372 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
3373 static void fill_auxv_note(struct memelfnote *, const TaskState *);
3374 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
3375 static size_t note_size(const struct memelfnote *);
3376 static void free_note_info(struct elf_note_info *);
3377 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
3378 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
3380 static int dump_write(int, const void *, size_t);
3381 static int write_note(struct memelfnote *, int);
3382 static int write_note_info(struct elf_note_info *, int);
3384 #ifdef BSWAP_NEEDED
3385 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
3387 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3388 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3389 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
3390 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
3391 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3392 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
3393 prstatus->pr_pid = tswap32(prstatus->pr_pid);
3394 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3395 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3396 prstatus->pr_sid = tswap32(prstatus->pr_sid);
3397 /* cpu times are not filled, so we skip them */
3398 /* regs should be in correct format already */
3399 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3402 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
3404 psinfo->pr_flag = tswapal(psinfo->pr_flag);
3405 psinfo->pr_uid = tswap16(psinfo->pr_uid);
3406 psinfo->pr_gid = tswap16(psinfo->pr_gid);
3407 psinfo->pr_pid = tswap32(psinfo->pr_pid);
3408 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
3409 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
3410 psinfo->pr_sid = tswap32(psinfo->pr_sid);
3413 static void bswap_note(struct elf_note *en)
3415 bswap32s(&en->n_namesz);
3416 bswap32s(&en->n_descsz);
3417 bswap32s(&en->n_type);
3419 #else
3420 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
3421 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
3422 static inline void bswap_note(struct elf_note *en) { }
3423 #endif /* BSWAP_NEEDED */
3426 * Minimal support for linux memory regions. These are needed
3427 * when we are finding out what memory exactly belongs to
3428 * emulated process. No locks needed here, as long as
3429 * thread that received the signal is stopped.
3432 static struct mm_struct *vma_init(void)
3434 struct mm_struct *mm;
3436 if ((mm = g_malloc(sizeof (*mm))) == NULL)
3437 return (NULL);
3439 mm->mm_count = 0;
3440 QTAILQ_INIT(&mm->mm_mmap);
3442 return (mm);
3445 static void vma_delete(struct mm_struct *mm)
3447 struct vm_area_struct *vma;
3449 while ((vma = vma_first(mm)) != NULL) {
3450 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
3451 g_free(vma);
3453 g_free(mm);
3456 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3457 target_ulong end, abi_ulong flags)
3459 struct vm_area_struct *vma;
3461 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
3462 return (-1);
3464 vma->vma_start = start;
3465 vma->vma_end = end;
3466 vma->vma_flags = flags;
3468 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
3469 mm->mm_count++;
3471 return (0);
3474 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3476 return (QTAILQ_FIRST(&mm->mm_mmap));
3479 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3481 return (QTAILQ_NEXT(vma, vma_link));
3484 static int vma_get_mapping_count(const struct mm_struct *mm)
3486 return (mm->mm_count);
3490 * Calculate file (dump) size of given memory region.
3492 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3494 /* if we cannot even read the first page, skip it */
3495 if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
3496 return (0);
3499 * Usually we don't dump executable pages as they contain
3500 * non-writable code that debugger can read directly from
3501 * target library etc. However, thread stacks are marked
3502 * also executable so we read in first page of given region
3503 * and check whether it contains elf header. If there is
3504 * no elf header, we dump it.
3506 if (vma->vma_flags & PROT_EXEC) {
3507 char page[TARGET_PAGE_SIZE];
3509 if (copy_from_user(page, vma->vma_start, sizeof (page))) {
3510 return 0;
3512 if ((page[EI_MAG0] == ELFMAG0) &&
3513 (page[EI_MAG1] == ELFMAG1) &&
3514 (page[EI_MAG2] == ELFMAG2) &&
3515 (page[EI_MAG3] == ELFMAG3)) {
3517 * Mappings are possibly from ELF binary. Don't dump
3518 * them.
3520 return (0);
3524 return (vma->vma_end - vma->vma_start);
3527 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3528 unsigned long flags)
3530 struct mm_struct *mm = (struct mm_struct *)priv;
3532 vma_add_mapping(mm, start, end, flags);
3533 return (0);
3536 static void fill_note(struct memelfnote *note, const char *name, int type,
3537 unsigned int sz, void *data)
3539 unsigned int namesz;
3541 namesz = strlen(name) + 1;
3542 note->name = name;
3543 note->namesz = namesz;
3544 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3545 note->type = type;
3546 note->datasz = sz;
3547 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3549 note->data = data;
3552 * We calculate rounded up note size here as specified by
3553 * ELF document.
3555 note->notesz = sizeof (struct elf_note) +
3556 note->namesz_rounded + note->datasz_rounded;
3559 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
3560 uint32_t flags)
3562 (void) memset(elf, 0, sizeof(*elf));
3564 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
3565 elf->e_ident[EI_CLASS] = ELF_CLASS;
3566 elf->e_ident[EI_DATA] = ELF_DATA;
3567 elf->e_ident[EI_VERSION] = EV_CURRENT;
3568 elf->e_ident[EI_OSABI] = ELF_OSABI;
3570 elf->e_type = ET_CORE;
3571 elf->e_machine = machine;
3572 elf->e_version = EV_CURRENT;
3573 elf->e_phoff = sizeof(struct elfhdr);
3574 elf->e_flags = flags;
3575 elf->e_ehsize = sizeof(struct elfhdr);
3576 elf->e_phentsize = sizeof(struct elf_phdr);
3577 elf->e_phnum = segs;
3579 bswap_ehdr(elf);
3582 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
3584 phdr->p_type = PT_NOTE;
3585 phdr->p_offset = offset;
3586 phdr->p_vaddr = 0;
3587 phdr->p_paddr = 0;
3588 phdr->p_filesz = sz;
3589 phdr->p_memsz = 0;
3590 phdr->p_flags = 0;
3591 phdr->p_align = 0;
3593 bswap_phdr(phdr, 1);
3596 static size_t note_size(const struct memelfnote *note)
3598 return (note->notesz);
3601 static void fill_prstatus(struct target_elf_prstatus *prstatus,
3602 const TaskState *ts, int signr)
3604 (void) memset(prstatus, 0, sizeof (*prstatus));
3605 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
3606 prstatus->pr_pid = ts->ts_tid;
3607 prstatus->pr_ppid = getppid();
3608 prstatus->pr_pgrp = getpgrp();
3609 prstatus->pr_sid = getsid(0);
3611 bswap_prstatus(prstatus);
3614 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
3616 char *base_filename;
3617 unsigned int i, len;
3619 (void) memset(psinfo, 0, sizeof (*psinfo));
3621 len = ts->info->arg_end - ts->info->arg_start;
3622 if (len >= ELF_PRARGSZ)
3623 len = ELF_PRARGSZ - 1;
3624 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
3625 return -EFAULT;
3626 for (i = 0; i < len; i++)
3627 if (psinfo->pr_psargs[i] == 0)
3628 psinfo->pr_psargs[i] = ' ';
3629 psinfo->pr_psargs[len] = 0;
3631 psinfo->pr_pid = getpid();
3632 psinfo->pr_ppid = getppid();
3633 psinfo->pr_pgrp = getpgrp();
3634 psinfo->pr_sid = getsid(0);
3635 psinfo->pr_uid = getuid();
3636 psinfo->pr_gid = getgid();
3638 base_filename = g_path_get_basename(ts->bprm->filename);
3640 * Using strncpy here is fine: at max-length,
3641 * this field is not NUL-terminated.
3643 (void) strncpy(psinfo->pr_fname, base_filename,
3644 sizeof(psinfo->pr_fname));
3646 g_free(base_filename);
3647 bswap_psinfo(psinfo);
3648 return (0);
3651 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
3653 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
3654 elf_addr_t orig_auxv = auxv;
3655 void *ptr;
3656 int len = ts->info->auxv_len;
3659 * Auxiliary vector is stored in target process stack. It contains
3660 * {type, value} pairs that we need to dump into note. This is not
3661 * strictly necessary but we do it here for sake of completeness.
3664 /* read in whole auxv vector and copy it to memelfnote */
3665 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
3666 if (ptr != NULL) {
3667 fill_note(note, "CORE", NT_AUXV, len, ptr);
3668 unlock_user(ptr, auxv, len);
3673 * Constructs name of coredump file. We have following convention
3674 * for the name:
3675 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3677 * Returns the filename
3679 static char *core_dump_filename(const TaskState *ts)
3681 g_autoptr(GDateTime) now = g_date_time_new_now_local();
3682 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
3683 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
3685 return g_strdup_printf("qemu_%s_%s_%d.core",
3686 base_filename, nowstr, (int)getpid());
3689 static int dump_write(int fd, const void *ptr, size_t size)
3691 const char *bufp = (const char *)ptr;
3692 ssize_t bytes_written, bytes_left;
3693 struct rlimit dumpsize;
3694 off_t pos;
3696 bytes_written = 0;
3697 getrlimit(RLIMIT_CORE, &dumpsize);
3698 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
3699 if (errno == ESPIPE) { /* not a seekable stream */
3700 bytes_left = size;
3701 } else {
3702 return pos;
3704 } else {
3705 if (dumpsize.rlim_cur <= pos) {
3706 return -1;
3707 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
3708 bytes_left = size;
3709 } else {
3710 size_t limit_left=dumpsize.rlim_cur - pos;
3711 bytes_left = limit_left >= size ? size : limit_left ;
3716 * In normal conditions, single write(2) should do but
3717 * in case of socket etc. this mechanism is more portable.
3719 do {
3720 bytes_written = write(fd, bufp, bytes_left);
3721 if (bytes_written < 0) {
3722 if (errno == EINTR)
3723 continue;
3724 return (-1);
3725 } else if (bytes_written == 0) { /* eof */
3726 return (-1);
3728 bufp += bytes_written;
3729 bytes_left -= bytes_written;
3730 } while (bytes_left > 0);
3732 return (0);
3735 static int write_note(struct memelfnote *men, int fd)
3737 struct elf_note en;
3739 en.n_namesz = men->namesz;
3740 en.n_type = men->type;
3741 en.n_descsz = men->datasz;
3743 bswap_note(&en);
3745 if (dump_write(fd, &en, sizeof(en)) != 0)
3746 return (-1);
3747 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3748 return (-1);
3749 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
3750 return (-1);
3752 return (0);
3755 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
3757 CPUState *cpu = env_cpu((CPUArchState *)env);
3758 TaskState *ts = (TaskState *)cpu->opaque;
3759 struct elf_thread_status *ets;
3761 ets = g_malloc0(sizeof (*ets));
3762 ets->num_notes = 1; /* only prstatus is dumped */
3763 fill_prstatus(&ets->prstatus, ts, 0);
3764 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3765 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
3766 &ets->prstatus);
3768 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
3770 info->notes_size += note_size(&ets->notes[0]);
3773 static void init_note_info(struct elf_note_info *info)
3775 /* Initialize the elf_note_info structure so that it is at
3776 * least safe to call free_note_info() on it. Must be
3777 * called before calling fill_note_info().
3779 memset(info, 0, sizeof (*info));
3780 QTAILQ_INIT(&info->thread_list);
3783 static int fill_note_info(struct elf_note_info *info,
3784 long signr, const CPUArchState *env)
3786 #define NUMNOTES 3
3787 CPUState *cpu = env_cpu((CPUArchState *)env);
3788 TaskState *ts = (TaskState *)cpu->opaque;
3789 int i;
3791 info->notes = g_new0(struct memelfnote, NUMNOTES);
3792 if (info->notes == NULL)
3793 return (-ENOMEM);
3794 info->prstatus = g_malloc0(sizeof (*info->prstatus));
3795 if (info->prstatus == NULL)
3796 return (-ENOMEM);
3797 info->psinfo = g_malloc0(sizeof (*info->psinfo));
3798 if (info->prstatus == NULL)
3799 return (-ENOMEM);
3802 * First fill in status (and registers) of current thread
3803 * including process info & aux vector.
3805 fill_prstatus(info->prstatus, ts, signr);
3806 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3807 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
3808 sizeof (*info->prstatus), info->prstatus);
3809 fill_psinfo(info->psinfo, ts);
3810 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
3811 sizeof (*info->psinfo), info->psinfo);
3812 fill_auxv_note(&info->notes[2], ts);
3813 info->numnote = 3;
3815 info->notes_size = 0;
3816 for (i = 0; i < info->numnote; i++)
3817 info->notes_size += note_size(&info->notes[i]);
3819 /* read and fill status of all threads */
3820 cpu_list_lock();
3821 CPU_FOREACH(cpu) {
3822 if (cpu == thread_cpu) {
3823 continue;
3825 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
3827 cpu_list_unlock();
3829 return (0);
3832 static void free_note_info(struct elf_note_info *info)
3834 struct elf_thread_status *ets;
3836 while (!QTAILQ_EMPTY(&info->thread_list)) {
3837 ets = QTAILQ_FIRST(&info->thread_list);
3838 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
3839 g_free(ets);
3842 g_free(info->prstatus);
3843 g_free(info->psinfo);
3844 g_free(info->notes);
3847 static int write_note_info(struct elf_note_info *info, int fd)
3849 struct elf_thread_status *ets;
3850 int i, error = 0;
3852 /* write prstatus, psinfo and auxv for current thread */
3853 for (i = 0; i < info->numnote; i++)
3854 if ((error = write_note(&info->notes[i], fd)) != 0)
3855 return (error);
3857 /* write prstatus for each thread */
3858 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
3859 if ((error = write_note(&ets->notes[0], fd)) != 0)
3860 return (error);
3863 return (0);
3867 * Write out ELF coredump.
3869 * See documentation of ELF object file format in:
3870 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3872 * Coredump format in linux is following:
3874 * 0 +----------------------+ \
3875 * | ELF header | ET_CORE |
3876 * +----------------------+ |
3877 * | ELF program headers | |--- headers
3878 * | - NOTE section | |
3879 * | - PT_LOAD sections | |
3880 * +----------------------+ /
3881 * | NOTEs: |
3882 * | - NT_PRSTATUS |
3883 * | - NT_PRSINFO |
3884 * | - NT_AUXV |
3885 * +----------------------+ <-- aligned to target page
3886 * | Process memory dump |
3887 * : :
3888 * . .
3889 * : :
3890 * | |
3891 * +----------------------+
3893 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3894 * NT_PRSINFO -> struct elf_prpsinfo
3895 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3897 * Format follows System V format as close as possible. Current
3898 * version limitations are as follows:
3899 * - no floating point registers are dumped
3901 * Function returns 0 in case of success, negative errno otherwise.
3903 * TODO: make this work also during runtime: it should be
3904 * possible to force coredump from running process and then
3905 * continue processing. For example qemu could set up SIGUSR2
3906 * handler (provided that target process haven't registered
3907 * handler for that) that does the dump when signal is received.
3909 static int elf_core_dump(int signr, const CPUArchState *env)
3911 const CPUState *cpu = env_cpu((CPUArchState *)env);
3912 const TaskState *ts = (const TaskState *)cpu->opaque;
3913 struct vm_area_struct *vma = NULL;
3914 g_autofree char *corefile = NULL;
3915 struct elf_note_info info;
3916 struct elfhdr elf;
3917 struct elf_phdr phdr;
3918 struct rlimit dumpsize;
3919 struct mm_struct *mm = NULL;
3920 off_t offset = 0, data_offset = 0;
3921 int segs = 0;
3922 int fd = -1;
3924 init_note_info(&info);
3926 errno = 0;
3927 getrlimit(RLIMIT_CORE, &dumpsize);
3928 if (dumpsize.rlim_cur == 0)
3929 return 0;
3931 corefile = core_dump_filename(ts);
3933 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3934 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3935 return (-errno);
3938 * Walk through target process memory mappings and
3939 * set up structure containing this information. After
3940 * this point vma_xxx functions can be used.
3942 if ((mm = vma_init()) == NULL)
3943 goto out;
3945 walk_memory_regions(mm, vma_walker);
3946 segs = vma_get_mapping_count(mm);
3949 * Construct valid coredump ELF header. We also
3950 * add one more segment for notes.
3952 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3953 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3954 goto out;
3956 /* fill in the in-memory version of notes */
3957 if (fill_note_info(&info, signr, env) < 0)
3958 goto out;
3960 offset += sizeof (elf); /* elf header */
3961 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3963 /* write out notes program header */
3964 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3966 offset += info.notes_size;
3967 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3968 goto out;
3971 * ELF specification wants data to start at page boundary so
3972 * we align it here.
3974 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3977 * Write program headers for memory regions mapped in
3978 * the target process.
3980 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3981 (void) memset(&phdr, 0, sizeof (phdr));
3983 phdr.p_type = PT_LOAD;
3984 phdr.p_offset = offset;
3985 phdr.p_vaddr = vma->vma_start;
3986 phdr.p_paddr = 0;
3987 phdr.p_filesz = vma_dump_size(vma);
3988 offset += phdr.p_filesz;
3989 phdr.p_memsz = vma->vma_end - vma->vma_start;
3990 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3991 if (vma->vma_flags & PROT_WRITE)
3992 phdr.p_flags |= PF_W;
3993 if (vma->vma_flags & PROT_EXEC)
3994 phdr.p_flags |= PF_X;
3995 phdr.p_align = ELF_EXEC_PAGESIZE;
3997 bswap_phdr(&phdr, 1);
3998 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
3999 goto out;
4004 * Next we write notes just after program headers. No
4005 * alignment needed here.
4007 if (write_note_info(&info, fd) < 0)
4008 goto out;
4010 /* align data to page boundary */
4011 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4012 goto out;
4015 * Finally we can dump process memory into corefile as well.
4017 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4018 abi_ulong addr;
4019 abi_ulong end;
4021 end = vma->vma_start + vma_dump_size(vma);
4023 for (addr = vma->vma_start; addr < end;
4024 addr += TARGET_PAGE_SIZE) {
4025 char page[TARGET_PAGE_SIZE];
4026 int error;
4029 * Read in page from target process memory and
4030 * write it to coredump file.
4032 error = copy_from_user(page, addr, sizeof (page));
4033 if (error != 0) {
4034 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
4035 addr);
4036 errno = -error;
4037 goto out;
4039 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4040 goto out;
4044 out:
4045 free_note_info(&info);
4046 if (mm != NULL)
4047 vma_delete(mm);
4048 (void) close(fd);
4050 if (errno != 0)
4051 return (-errno);
4052 return (0);
4054 #endif /* USE_ELF_CORE_DUMP */
4056 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4058 init_thread(regs, infop);