memory_hotplug.c: send DEVICE_UNPLUG_GUEST_ERROR in acpi_memory_hotplug_write()
[qemu.git] / linux-user / elfload.c
blob5f9e2141ad1e4acadd7b9099ffd06fd1cac7d066
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 "user-internals.h"
10 #include "loader.h"
11 #include "user-mmap.h"
12 #include "disas/disas.h"
13 #include "qemu/bitops.h"
14 #include "qemu/path.h"
15 #include "qemu/queue.h"
16 #include "qemu/guest-random.h"
17 #include "qemu/units.h"
18 #include "qemu/selfmap.h"
19 #include "qapi/error.h"
21 #ifdef _ARCH_PPC64
22 #undef ARCH_DLINFO
23 #undef ELF_PLATFORM
24 #undef ELF_HWCAP
25 #undef ELF_HWCAP2
26 #undef ELF_CLASS
27 #undef ELF_DATA
28 #undef ELF_ARCH
29 #endif
31 #define ELF_OSABI ELFOSABI_SYSV
33 /* from personality.h */
36 * Flags for bug emulation.
38 * These occupy the top three bytes.
40 enum {
41 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
42 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
43 descriptors (signal handling) */
44 MMAP_PAGE_ZERO = 0x0100000,
45 ADDR_COMPAT_LAYOUT = 0x0200000,
46 READ_IMPLIES_EXEC = 0x0400000,
47 ADDR_LIMIT_32BIT = 0x0800000,
48 SHORT_INODE = 0x1000000,
49 WHOLE_SECONDS = 0x2000000,
50 STICKY_TIMEOUTS = 0x4000000,
51 ADDR_LIMIT_3GB = 0x8000000,
55 * Personality types.
57 * These go in the low byte. Avoid using the top bit, it will
58 * conflict with error returns.
60 enum {
61 PER_LINUX = 0x0000,
62 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
63 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
64 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
65 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
66 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
67 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
68 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
69 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
70 PER_BSD = 0x0006,
71 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
72 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
73 PER_LINUX32 = 0x0008,
74 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
75 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
76 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
77 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
78 PER_RISCOS = 0x000c,
79 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
80 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
81 PER_OSF4 = 0x000f, /* OSF/1 v4 */
82 PER_HPUX = 0x0010,
83 PER_MASK = 0x00ff,
87 * Return the base personality without flags.
89 #define personality(pers) (pers & PER_MASK)
91 int info_is_fdpic(struct image_info *info)
93 return info->personality == PER_LINUX_FDPIC;
96 /* this flag is uneffective under linux too, should be deleted */
97 #ifndef MAP_DENYWRITE
98 #define MAP_DENYWRITE 0
99 #endif
101 /* should probably go in elf.h */
102 #ifndef ELIBBAD
103 #define ELIBBAD 80
104 #endif
106 #ifdef TARGET_WORDS_BIGENDIAN
107 #define ELF_DATA ELFDATA2MSB
108 #else
109 #define ELF_DATA ELFDATA2LSB
110 #endif
112 #ifdef TARGET_ABI_MIPSN32
113 typedef abi_ullong target_elf_greg_t;
114 #define tswapreg(ptr) tswap64(ptr)
115 #else
116 typedef abi_ulong target_elf_greg_t;
117 #define tswapreg(ptr) tswapal(ptr)
118 #endif
120 #ifdef USE_UID16
121 typedef abi_ushort target_uid_t;
122 typedef abi_ushort target_gid_t;
123 #else
124 typedef abi_uint target_uid_t;
125 typedef abi_uint target_gid_t;
126 #endif
127 typedef abi_int target_pid_t;
129 #ifdef TARGET_I386
131 #define ELF_PLATFORM get_elf_platform()
133 static const char *get_elf_platform(void)
135 static char elf_platform[] = "i386";
136 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
137 if (family > 6)
138 family = 6;
139 if (family >= 3)
140 elf_platform[1] = '0' + family;
141 return elf_platform;
144 #define ELF_HWCAP get_elf_hwcap()
146 static uint32_t get_elf_hwcap(void)
148 X86CPU *cpu = X86_CPU(thread_cpu);
150 return cpu->env.features[FEAT_1_EDX];
153 #ifdef TARGET_X86_64
154 #define ELF_START_MMAP 0x2aaaaab000ULL
156 #define ELF_CLASS ELFCLASS64
157 #define ELF_ARCH EM_X86_64
159 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
161 regs->rax = 0;
162 regs->rsp = infop->start_stack;
163 regs->rip = infop->entry;
166 #define ELF_NREG 27
167 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
170 * Note that ELF_NREG should be 29 as there should be place for
171 * TRAPNO and ERR "registers" as well but linux doesn't dump
172 * those.
174 * See linux kernel: arch/x86/include/asm/elf.h
176 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
178 (*regs)[0] = tswapreg(env->regs[15]);
179 (*regs)[1] = tswapreg(env->regs[14]);
180 (*regs)[2] = tswapreg(env->regs[13]);
181 (*regs)[3] = tswapreg(env->regs[12]);
182 (*regs)[4] = tswapreg(env->regs[R_EBP]);
183 (*regs)[5] = tswapreg(env->regs[R_EBX]);
184 (*regs)[6] = tswapreg(env->regs[11]);
185 (*regs)[7] = tswapreg(env->regs[10]);
186 (*regs)[8] = tswapreg(env->regs[9]);
187 (*regs)[9] = tswapreg(env->regs[8]);
188 (*regs)[10] = tswapreg(env->regs[R_EAX]);
189 (*regs)[11] = tswapreg(env->regs[R_ECX]);
190 (*regs)[12] = tswapreg(env->regs[R_EDX]);
191 (*regs)[13] = tswapreg(env->regs[R_ESI]);
192 (*regs)[14] = tswapreg(env->regs[R_EDI]);
193 (*regs)[15] = tswapreg(env->regs[R_EAX]); /* XXX */
194 (*regs)[16] = tswapreg(env->eip);
195 (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff);
196 (*regs)[18] = tswapreg(env->eflags);
197 (*regs)[19] = tswapreg(env->regs[R_ESP]);
198 (*regs)[20] = tswapreg(env->segs[R_SS].selector & 0xffff);
199 (*regs)[21] = tswapreg(env->segs[R_FS].selector & 0xffff);
200 (*regs)[22] = tswapreg(env->segs[R_GS].selector & 0xffff);
201 (*regs)[23] = tswapreg(env->segs[R_DS].selector & 0xffff);
202 (*regs)[24] = tswapreg(env->segs[R_ES].selector & 0xffff);
203 (*regs)[25] = tswapreg(env->segs[R_FS].selector & 0xffff);
204 (*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff);
207 #else
209 #define ELF_START_MMAP 0x80000000
212 * This is used to ensure we don't load something for the wrong architecture.
214 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
217 * These are used to set parameters in the core dumps.
219 #define ELF_CLASS ELFCLASS32
220 #define ELF_ARCH EM_386
222 static inline void init_thread(struct target_pt_regs *regs,
223 struct image_info *infop)
225 regs->esp = infop->start_stack;
226 regs->eip = infop->entry;
228 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
229 starts %edx contains a pointer to a function which might be
230 registered using `atexit'. This provides a mean for the
231 dynamic linker to call DT_FINI functions for shared libraries
232 that have been loaded before the code runs.
234 A value of 0 tells we have no such handler. */
235 regs->edx = 0;
238 #define ELF_NREG 17
239 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
242 * Note that ELF_NREG should be 19 as there should be place for
243 * TRAPNO and ERR "registers" as well but linux doesn't dump
244 * those.
246 * See linux kernel: arch/x86/include/asm/elf.h
248 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
250 (*regs)[0] = tswapreg(env->regs[R_EBX]);
251 (*regs)[1] = tswapreg(env->regs[R_ECX]);
252 (*regs)[2] = tswapreg(env->regs[R_EDX]);
253 (*regs)[3] = tswapreg(env->regs[R_ESI]);
254 (*regs)[4] = tswapreg(env->regs[R_EDI]);
255 (*regs)[5] = tswapreg(env->regs[R_EBP]);
256 (*regs)[6] = tswapreg(env->regs[R_EAX]);
257 (*regs)[7] = tswapreg(env->segs[R_DS].selector & 0xffff);
258 (*regs)[8] = tswapreg(env->segs[R_ES].selector & 0xffff);
259 (*regs)[9] = tswapreg(env->segs[R_FS].selector & 0xffff);
260 (*regs)[10] = tswapreg(env->segs[R_GS].selector & 0xffff);
261 (*regs)[11] = tswapreg(env->regs[R_EAX]); /* XXX */
262 (*regs)[12] = tswapreg(env->eip);
263 (*regs)[13] = tswapreg(env->segs[R_CS].selector & 0xffff);
264 (*regs)[14] = tswapreg(env->eflags);
265 (*regs)[15] = tswapreg(env->regs[R_ESP]);
266 (*regs)[16] = tswapreg(env->segs[R_SS].selector & 0xffff);
268 #endif
270 #define USE_ELF_CORE_DUMP
271 #define ELF_EXEC_PAGESIZE 4096
273 #endif
275 #ifdef TARGET_ARM
277 #ifndef TARGET_AARCH64
278 /* 32 bit ARM definitions */
280 #define ELF_START_MMAP 0x80000000
282 #define ELF_ARCH EM_ARM
283 #define ELF_CLASS ELFCLASS32
285 static inline void init_thread(struct target_pt_regs *regs,
286 struct image_info *infop)
288 abi_long stack = infop->start_stack;
289 memset(regs, 0, sizeof(*regs));
291 regs->uregs[16] = ARM_CPU_MODE_USR;
292 if (infop->entry & 1) {
293 regs->uregs[16] |= CPSR_T;
295 regs->uregs[15] = infop->entry & 0xfffffffe;
296 regs->uregs[13] = infop->start_stack;
297 /* FIXME - what to for failure of get_user()? */
298 get_user_ual(regs->uregs[2], stack + 8); /* envp */
299 get_user_ual(regs->uregs[1], stack + 4); /* envp */
300 /* XXX: it seems that r0 is zeroed after ! */
301 regs->uregs[0] = 0;
302 /* For uClinux PIC binaries. */
303 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
304 regs->uregs[10] = infop->start_data;
306 /* Support ARM FDPIC. */
307 if (info_is_fdpic(infop)) {
308 /* As described in the ABI document, r7 points to the loadmap info
309 * prepared by the kernel. If an interpreter is needed, r8 points
310 * to the interpreter loadmap and r9 points to the interpreter
311 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
312 * r9 points to the main program PT_DYNAMIC info.
314 regs->uregs[7] = infop->loadmap_addr;
315 if (infop->interpreter_loadmap_addr) {
316 /* Executable is dynamically loaded. */
317 regs->uregs[8] = infop->interpreter_loadmap_addr;
318 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
319 } else {
320 regs->uregs[8] = 0;
321 regs->uregs[9] = infop->pt_dynamic_addr;
326 #define ELF_NREG 18
327 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
329 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
331 (*regs)[0] = tswapreg(env->regs[0]);
332 (*regs)[1] = tswapreg(env->regs[1]);
333 (*regs)[2] = tswapreg(env->regs[2]);
334 (*regs)[3] = tswapreg(env->regs[3]);
335 (*regs)[4] = tswapreg(env->regs[4]);
336 (*regs)[5] = tswapreg(env->regs[5]);
337 (*regs)[6] = tswapreg(env->regs[6]);
338 (*regs)[7] = tswapreg(env->regs[7]);
339 (*regs)[8] = tswapreg(env->regs[8]);
340 (*regs)[9] = tswapreg(env->regs[9]);
341 (*regs)[10] = tswapreg(env->regs[10]);
342 (*regs)[11] = tswapreg(env->regs[11]);
343 (*regs)[12] = tswapreg(env->regs[12]);
344 (*regs)[13] = tswapreg(env->regs[13]);
345 (*regs)[14] = tswapreg(env->regs[14]);
346 (*regs)[15] = tswapreg(env->regs[15]);
348 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
349 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
352 #define USE_ELF_CORE_DUMP
353 #define ELF_EXEC_PAGESIZE 4096
355 enum
357 ARM_HWCAP_ARM_SWP = 1 << 0,
358 ARM_HWCAP_ARM_HALF = 1 << 1,
359 ARM_HWCAP_ARM_THUMB = 1 << 2,
360 ARM_HWCAP_ARM_26BIT = 1 << 3,
361 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
362 ARM_HWCAP_ARM_FPA = 1 << 5,
363 ARM_HWCAP_ARM_VFP = 1 << 6,
364 ARM_HWCAP_ARM_EDSP = 1 << 7,
365 ARM_HWCAP_ARM_JAVA = 1 << 8,
366 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
367 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
368 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
369 ARM_HWCAP_ARM_NEON = 1 << 12,
370 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
371 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
372 ARM_HWCAP_ARM_TLS = 1 << 15,
373 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
374 ARM_HWCAP_ARM_IDIVA = 1 << 17,
375 ARM_HWCAP_ARM_IDIVT = 1 << 18,
376 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
377 ARM_HWCAP_ARM_LPAE = 1 << 20,
378 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
381 enum {
382 ARM_HWCAP2_ARM_AES = 1 << 0,
383 ARM_HWCAP2_ARM_PMULL = 1 << 1,
384 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
385 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
386 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
389 /* The commpage only exists for 32 bit kernels */
391 #define ARM_COMMPAGE (intptr_t)0xffff0f00u
393 static bool init_guest_commpage(void)
395 void *want = g2h_untagged(ARM_COMMPAGE & -qemu_host_page_size);
396 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
397 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
399 if (addr == MAP_FAILED) {
400 perror("Allocating guest commpage");
401 exit(EXIT_FAILURE);
403 if (addr != want) {
404 return false;
407 /* Set kernel helper versions; rest of page is 0. */
408 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
410 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
411 perror("Protecting guest commpage");
412 exit(EXIT_FAILURE);
414 return true;
417 #define ELF_HWCAP get_elf_hwcap()
418 #define ELF_HWCAP2 get_elf_hwcap2()
420 static uint32_t get_elf_hwcap(void)
422 ARMCPU *cpu = ARM_CPU(thread_cpu);
423 uint32_t hwcaps = 0;
425 hwcaps |= ARM_HWCAP_ARM_SWP;
426 hwcaps |= ARM_HWCAP_ARM_HALF;
427 hwcaps |= ARM_HWCAP_ARM_THUMB;
428 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
430 /* probe for the extra features */
431 #define GET_FEATURE(feat, hwcap) \
432 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
434 #define GET_FEATURE_ID(feat, hwcap) \
435 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
437 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
438 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
439 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
440 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
441 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
442 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
443 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
444 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
445 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
446 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
448 if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
449 cpu_isar_feature(aa32_fpdp_v3, cpu)) {
450 hwcaps |= ARM_HWCAP_ARM_VFPv3;
451 if (cpu_isar_feature(aa32_simd_r32, cpu)) {
452 hwcaps |= ARM_HWCAP_ARM_VFPD32;
453 } else {
454 hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
457 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
459 return hwcaps;
462 static uint32_t get_elf_hwcap2(void)
464 ARMCPU *cpu = ARM_CPU(thread_cpu);
465 uint32_t hwcaps = 0;
467 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
468 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
469 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
470 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
471 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
472 return hwcaps;
475 #undef GET_FEATURE
476 #undef GET_FEATURE_ID
478 #define ELF_PLATFORM get_elf_platform()
480 static const char *get_elf_platform(void)
482 CPUARMState *env = thread_cpu->env_ptr;
484 #ifdef TARGET_WORDS_BIGENDIAN
485 # define END "b"
486 #else
487 # define END "l"
488 #endif
490 if (arm_feature(env, ARM_FEATURE_V8)) {
491 return "v8" END;
492 } else if (arm_feature(env, ARM_FEATURE_V7)) {
493 if (arm_feature(env, ARM_FEATURE_M)) {
494 return "v7m" END;
495 } else {
496 return "v7" END;
498 } else if (arm_feature(env, ARM_FEATURE_V6)) {
499 return "v6" END;
500 } else if (arm_feature(env, ARM_FEATURE_V5)) {
501 return "v5" END;
502 } else {
503 return "v4" END;
506 #undef END
509 #else
510 /* 64 bit ARM definitions */
511 #define ELF_START_MMAP 0x80000000
513 #define ELF_ARCH EM_AARCH64
514 #define ELF_CLASS ELFCLASS64
515 #ifdef TARGET_WORDS_BIGENDIAN
516 # define ELF_PLATFORM "aarch64_be"
517 #else
518 # define ELF_PLATFORM "aarch64"
519 #endif
521 static inline void init_thread(struct target_pt_regs *regs,
522 struct image_info *infop)
524 abi_long stack = infop->start_stack;
525 memset(regs, 0, sizeof(*regs));
527 regs->pc = infop->entry & ~0x3ULL;
528 regs->sp = stack;
531 #define ELF_NREG 34
532 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
534 static void elf_core_copy_regs(target_elf_gregset_t *regs,
535 const CPUARMState *env)
537 int i;
539 for (i = 0; i < 32; i++) {
540 (*regs)[i] = tswapreg(env->xregs[i]);
542 (*regs)[32] = tswapreg(env->pc);
543 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
546 #define USE_ELF_CORE_DUMP
547 #define ELF_EXEC_PAGESIZE 4096
549 enum {
550 ARM_HWCAP_A64_FP = 1 << 0,
551 ARM_HWCAP_A64_ASIMD = 1 << 1,
552 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
553 ARM_HWCAP_A64_AES = 1 << 3,
554 ARM_HWCAP_A64_PMULL = 1 << 4,
555 ARM_HWCAP_A64_SHA1 = 1 << 5,
556 ARM_HWCAP_A64_SHA2 = 1 << 6,
557 ARM_HWCAP_A64_CRC32 = 1 << 7,
558 ARM_HWCAP_A64_ATOMICS = 1 << 8,
559 ARM_HWCAP_A64_FPHP = 1 << 9,
560 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
561 ARM_HWCAP_A64_CPUID = 1 << 11,
562 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
563 ARM_HWCAP_A64_JSCVT = 1 << 13,
564 ARM_HWCAP_A64_FCMA = 1 << 14,
565 ARM_HWCAP_A64_LRCPC = 1 << 15,
566 ARM_HWCAP_A64_DCPOP = 1 << 16,
567 ARM_HWCAP_A64_SHA3 = 1 << 17,
568 ARM_HWCAP_A64_SM3 = 1 << 18,
569 ARM_HWCAP_A64_SM4 = 1 << 19,
570 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
571 ARM_HWCAP_A64_SHA512 = 1 << 21,
572 ARM_HWCAP_A64_SVE = 1 << 22,
573 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
574 ARM_HWCAP_A64_DIT = 1 << 24,
575 ARM_HWCAP_A64_USCAT = 1 << 25,
576 ARM_HWCAP_A64_ILRCPC = 1 << 26,
577 ARM_HWCAP_A64_FLAGM = 1 << 27,
578 ARM_HWCAP_A64_SSBS = 1 << 28,
579 ARM_HWCAP_A64_SB = 1 << 29,
580 ARM_HWCAP_A64_PACA = 1 << 30,
581 ARM_HWCAP_A64_PACG = 1UL << 31,
583 ARM_HWCAP2_A64_DCPODP = 1 << 0,
584 ARM_HWCAP2_A64_SVE2 = 1 << 1,
585 ARM_HWCAP2_A64_SVEAES = 1 << 2,
586 ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
587 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
588 ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
589 ARM_HWCAP2_A64_SVESM4 = 1 << 6,
590 ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
591 ARM_HWCAP2_A64_FRINT = 1 << 8,
592 ARM_HWCAP2_A64_SVEI8MM = 1 << 9,
593 ARM_HWCAP2_A64_SVEF32MM = 1 << 10,
594 ARM_HWCAP2_A64_SVEF64MM = 1 << 11,
595 ARM_HWCAP2_A64_SVEBF16 = 1 << 12,
596 ARM_HWCAP2_A64_I8MM = 1 << 13,
597 ARM_HWCAP2_A64_BF16 = 1 << 14,
598 ARM_HWCAP2_A64_DGH = 1 << 15,
599 ARM_HWCAP2_A64_RNG = 1 << 16,
600 ARM_HWCAP2_A64_BTI = 1 << 17,
601 ARM_HWCAP2_A64_MTE = 1 << 18,
604 #define ELF_HWCAP get_elf_hwcap()
605 #define ELF_HWCAP2 get_elf_hwcap2()
607 #define GET_FEATURE_ID(feat, hwcap) \
608 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
610 static uint32_t get_elf_hwcap(void)
612 ARMCPU *cpu = ARM_CPU(thread_cpu);
613 uint32_t hwcaps = 0;
615 hwcaps |= ARM_HWCAP_A64_FP;
616 hwcaps |= ARM_HWCAP_A64_ASIMD;
617 hwcaps |= ARM_HWCAP_A64_CPUID;
619 /* probe for the extra features */
621 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
622 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
623 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
624 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
625 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
626 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
627 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
628 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
629 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
630 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
631 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
632 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
633 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
634 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
635 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
636 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
637 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
638 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
639 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
640 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
641 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
642 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
643 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
645 return hwcaps;
648 static uint32_t get_elf_hwcap2(void)
650 ARMCPU *cpu = ARM_CPU(thread_cpu);
651 uint32_t hwcaps = 0;
653 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
654 GET_FEATURE_ID(aa64_sve2, ARM_HWCAP2_A64_SVE2);
655 GET_FEATURE_ID(aa64_sve2_aes, ARM_HWCAP2_A64_SVEAES);
656 GET_FEATURE_ID(aa64_sve2_pmull128, ARM_HWCAP2_A64_SVEPMULL);
657 GET_FEATURE_ID(aa64_sve2_bitperm, ARM_HWCAP2_A64_SVEBITPERM);
658 GET_FEATURE_ID(aa64_sve2_sha3, ARM_HWCAP2_A64_SVESHA3);
659 GET_FEATURE_ID(aa64_sve2_sm4, ARM_HWCAP2_A64_SVESM4);
660 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
661 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
662 GET_FEATURE_ID(aa64_sve_i8mm, ARM_HWCAP2_A64_SVEI8MM);
663 GET_FEATURE_ID(aa64_sve_f32mm, ARM_HWCAP2_A64_SVEF32MM);
664 GET_FEATURE_ID(aa64_sve_f64mm, ARM_HWCAP2_A64_SVEF64MM);
665 GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16);
666 GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM);
667 GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16);
668 GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG);
669 GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI);
670 GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE);
672 return hwcaps;
675 #undef GET_FEATURE_ID
677 #endif /* not TARGET_AARCH64 */
678 #endif /* TARGET_ARM */
680 #ifdef TARGET_SPARC
681 #ifdef TARGET_SPARC64
683 #define ELF_START_MMAP 0x80000000
684 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
685 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
686 #ifndef TARGET_ABI32
687 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
688 #else
689 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
690 #endif
692 #define ELF_CLASS ELFCLASS64
693 #define ELF_ARCH EM_SPARCV9
694 #else
695 #define ELF_START_MMAP 0x80000000
696 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
697 | HWCAP_SPARC_MULDIV)
698 #define ELF_CLASS ELFCLASS32
699 #define ELF_ARCH EM_SPARC
700 #endif /* TARGET_SPARC64 */
702 static inline void init_thread(struct target_pt_regs *regs,
703 struct image_info *infop)
705 /* Note that target_cpu_copy_regs does not read psr/tstate. */
706 regs->pc = infop->entry;
707 regs->npc = regs->pc + 4;
708 regs->y = 0;
709 regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong)
710 - TARGET_STACK_BIAS);
712 #endif /* TARGET_SPARC */
714 #ifdef TARGET_PPC
716 #define ELF_MACHINE PPC_ELF_MACHINE
717 #define ELF_START_MMAP 0x80000000
719 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
721 #define elf_check_arch(x) ( (x) == EM_PPC64 )
723 #define ELF_CLASS ELFCLASS64
725 #else
727 #define ELF_CLASS ELFCLASS32
729 #endif
731 #define ELF_ARCH EM_PPC
733 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
734 See arch/powerpc/include/asm/cputable.h. */
735 enum {
736 QEMU_PPC_FEATURE_32 = 0x80000000,
737 QEMU_PPC_FEATURE_64 = 0x40000000,
738 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
739 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
740 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
741 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
742 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
743 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
744 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
745 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
746 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
747 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
748 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
749 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
750 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
751 QEMU_PPC_FEATURE_CELL = 0x00010000,
752 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
753 QEMU_PPC_FEATURE_SMT = 0x00004000,
754 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
755 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
756 QEMU_PPC_FEATURE_PA6T = 0x00000800,
757 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
758 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
759 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
760 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
761 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
763 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
764 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
766 /* Feature definitions in AT_HWCAP2. */
767 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
768 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
769 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
770 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
771 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
772 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
773 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
774 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
775 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
776 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
777 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
778 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
779 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
782 #define ELF_HWCAP get_elf_hwcap()
784 static uint32_t get_elf_hwcap(void)
786 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
787 uint32_t features = 0;
789 /* We don't have to be terribly complete here; the high points are
790 Altivec/FP/SPE support. Anything else is just a bonus. */
791 #define GET_FEATURE(flag, feature) \
792 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
793 #define GET_FEATURE2(flags, feature) \
794 do { \
795 if ((cpu->env.insns_flags2 & flags) == flags) { \
796 features |= feature; \
798 } while (0)
799 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
800 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
801 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
802 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
803 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
804 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
805 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
806 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
807 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
808 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
809 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
810 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
811 QEMU_PPC_FEATURE_ARCH_2_06);
812 #undef GET_FEATURE
813 #undef GET_FEATURE2
815 return features;
818 #define ELF_HWCAP2 get_elf_hwcap2()
820 static uint32_t get_elf_hwcap2(void)
822 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
823 uint32_t features = 0;
825 #define GET_FEATURE(flag, feature) \
826 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
827 #define GET_FEATURE2(flag, feature) \
828 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
830 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
831 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
832 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
833 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
834 QEMU_PPC_FEATURE2_VEC_CRYPTO);
835 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
836 QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128);
838 #undef GET_FEATURE
839 #undef GET_FEATURE2
841 return features;
845 * The requirements here are:
846 * - keep the final alignment of sp (sp & 0xf)
847 * - make sure the 32-bit value at the first 16 byte aligned position of
848 * AUXV is greater than 16 for glibc compatibility.
849 * AT_IGNOREPPC is used for that.
850 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
851 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
853 #define DLINFO_ARCH_ITEMS 5
854 #define ARCH_DLINFO \
855 do { \
856 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
857 /* \
858 * Handle glibc compatibility: these magic entries must \
859 * be at the lowest addresses in the final auxv. \
860 */ \
861 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
862 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
863 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
864 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
865 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
866 } while (0)
868 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
870 _regs->gpr[1] = infop->start_stack;
871 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
872 if (get_ppc64_abi(infop) < 2) {
873 uint64_t val;
874 get_user_u64(val, infop->entry + 8);
875 _regs->gpr[2] = val + infop->load_bias;
876 get_user_u64(val, infop->entry);
877 infop->entry = val + infop->load_bias;
878 } else {
879 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
881 #endif
882 _regs->nip = infop->entry;
885 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
886 #define ELF_NREG 48
887 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
889 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
891 int i;
892 target_ulong ccr = 0;
894 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
895 (*regs)[i] = tswapreg(env->gpr[i]);
898 (*regs)[32] = tswapreg(env->nip);
899 (*regs)[33] = tswapreg(env->msr);
900 (*regs)[35] = tswapreg(env->ctr);
901 (*regs)[36] = tswapreg(env->lr);
902 (*regs)[37] = tswapreg(env->xer);
904 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
905 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
907 (*regs)[38] = tswapreg(ccr);
910 #define USE_ELF_CORE_DUMP
911 #define ELF_EXEC_PAGESIZE 4096
913 #endif
915 #ifdef TARGET_MIPS
917 #define ELF_START_MMAP 0x80000000
919 #ifdef TARGET_MIPS64
920 #define ELF_CLASS ELFCLASS64
921 #else
922 #define ELF_CLASS ELFCLASS32
923 #endif
924 #define ELF_ARCH EM_MIPS
926 #define elf_check_arch(x) ((x) == EM_MIPS || (x) == EM_NANOMIPS)
928 #ifdef TARGET_ABI_MIPSN32
929 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
930 #else
931 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
932 #endif
934 static inline void init_thread(struct target_pt_regs *regs,
935 struct image_info *infop)
937 regs->cp0_status = 2 << CP0St_KSU;
938 regs->cp0_epc = infop->entry;
939 regs->regs[29] = infop->start_stack;
942 /* See linux kernel: arch/mips/include/asm/elf.h. */
943 #define ELF_NREG 45
944 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
946 /* See linux kernel: arch/mips/include/asm/reg.h. */
947 enum {
948 #ifdef TARGET_MIPS64
949 TARGET_EF_R0 = 0,
950 #else
951 TARGET_EF_R0 = 6,
952 #endif
953 TARGET_EF_R26 = TARGET_EF_R0 + 26,
954 TARGET_EF_R27 = TARGET_EF_R0 + 27,
955 TARGET_EF_LO = TARGET_EF_R0 + 32,
956 TARGET_EF_HI = TARGET_EF_R0 + 33,
957 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
958 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
959 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
960 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
963 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
964 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
966 int i;
968 for (i = 0; i < TARGET_EF_R0; i++) {
969 (*regs)[i] = 0;
971 (*regs)[TARGET_EF_R0] = 0;
973 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
974 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
977 (*regs)[TARGET_EF_R26] = 0;
978 (*regs)[TARGET_EF_R27] = 0;
979 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
980 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
981 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
982 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
983 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
984 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
987 #define USE_ELF_CORE_DUMP
988 #define ELF_EXEC_PAGESIZE 4096
990 /* See arch/mips/include/uapi/asm/hwcap.h. */
991 enum {
992 HWCAP_MIPS_R6 = (1 << 0),
993 HWCAP_MIPS_MSA = (1 << 1),
994 HWCAP_MIPS_CRC32 = (1 << 2),
995 HWCAP_MIPS_MIPS16 = (1 << 3),
996 HWCAP_MIPS_MDMX = (1 << 4),
997 HWCAP_MIPS_MIPS3D = (1 << 5),
998 HWCAP_MIPS_SMARTMIPS = (1 << 6),
999 HWCAP_MIPS_DSP = (1 << 7),
1000 HWCAP_MIPS_DSP2 = (1 << 8),
1001 HWCAP_MIPS_DSP3 = (1 << 9),
1002 HWCAP_MIPS_MIPS16E2 = (1 << 10),
1003 HWCAP_LOONGSON_MMI = (1 << 11),
1004 HWCAP_LOONGSON_EXT = (1 << 12),
1005 HWCAP_LOONGSON_EXT2 = (1 << 13),
1006 HWCAP_LOONGSON_CPUCFG = (1 << 14),
1009 #define ELF_HWCAP get_elf_hwcap()
1011 #define GET_FEATURE_INSN(_flag, _hwcap) \
1012 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1014 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1015 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1017 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1018 do { \
1019 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1020 hwcaps |= _hwcap; \
1022 } while (0)
1024 static uint32_t get_elf_hwcap(void)
1026 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1027 uint32_t hwcaps = 0;
1029 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH,
1030 2, HWCAP_MIPS_R6);
1031 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA);
1032 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI);
1033 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT);
1035 return hwcaps;
1038 #undef GET_FEATURE_REG_EQU
1039 #undef GET_FEATURE_REG_SET
1040 #undef GET_FEATURE_INSN
1042 #endif /* TARGET_MIPS */
1044 #ifdef TARGET_MICROBLAZE
1046 #define ELF_START_MMAP 0x80000000
1048 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1050 #define ELF_CLASS ELFCLASS32
1051 #define ELF_ARCH EM_MICROBLAZE
1053 static inline void init_thread(struct target_pt_regs *regs,
1054 struct image_info *infop)
1056 regs->pc = infop->entry;
1057 regs->r1 = infop->start_stack;
1061 #define ELF_EXEC_PAGESIZE 4096
1063 #define USE_ELF_CORE_DUMP
1064 #define ELF_NREG 38
1065 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1067 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1068 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1070 int i, pos = 0;
1072 for (i = 0; i < 32; i++) {
1073 (*regs)[pos++] = tswapreg(env->regs[i]);
1076 (*regs)[pos++] = tswapreg(env->pc);
1077 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
1078 (*regs)[pos++] = 0;
1079 (*regs)[pos++] = tswapreg(env->ear);
1080 (*regs)[pos++] = 0;
1081 (*regs)[pos++] = tswapreg(env->esr);
1084 #endif /* TARGET_MICROBLAZE */
1086 #ifdef TARGET_NIOS2
1088 #define ELF_START_MMAP 0x80000000
1090 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1092 #define ELF_CLASS ELFCLASS32
1093 #define ELF_ARCH EM_ALTERA_NIOS2
1095 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1097 regs->ea = infop->entry;
1098 regs->sp = infop->start_stack;
1099 regs->estatus = 0x3;
1102 #define ELF_EXEC_PAGESIZE 4096
1104 #define USE_ELF_CORE_DUMP
1105 #define ELF_NREG 49
1106 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1108 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1109 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1110 const CPUNios2State *env)
1112 int i;
1114 (*regs)[0] = -1;
1115 for (i = 1; i < 8; i++) /* r0-r7 */
1116 (*regs)[i] = tswapreg(env->regs[i + 7]);
1118 for (i = 8; i < 16; i++) /* r8-r15 */
1119 (*regs)[i] = tswapreg(env->regs[i - 8]);
1121 for (i = 16; i < 24; i++) /* r16-r23 */
1122 (*regs)[i] = tswapreg(env->regs[i + 7]);
1123 (*regs)[24] = -1; /* R_ET */
1124 (*regs)[25] = -1; /* R_BT */
1125 (*regs)[26] = tswapreg(env->regs[R_GP]);
1126 (*regs)[27] = tswapreg(env->regs[R_SP]);
1127 (*regs)[28] = tswapreg(env->regs[R_FP]);
1128 (*regs)[29] = tswapreg(env->regs[R_EA]);
1129 (*regs)[30] = -1; /* R_SSTATUS */
1130 (*regs)[31] = tswapreg(env->regs[R_RA]);
1132 (*regs)[32] = tswapreg(env->regs[R_PC]);
1134 (*regs)[33] = -1; /* R_STATUS */
1135 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1137 for (i = 35; i < 49; i++) /* ... */
1138 (*regs)[i] = -1;
1141 #endif /* TARGET_NIOS2 */
1143 #ifdef TARGET_OPENRISC
1145 #define ELF_START_MMAP 0x08000000
1147 #define ELF_ARCH EM_OPENRISC
1148 #define ELF_CLASS ELFCLASS32
1149 #define ELF_DATA ELFDATA2MSB
1151 static inline void init_thread(struct target_pt_regs *regs,
1152 struct image_info *infop)
1154 regs->pc = infop->entry;
1155 regs->gpr[1] = infop->start_stack;
1158 #define USE_ELF_CORE_DUMP
1159 #define ELF_EXEC_PAGESIZE 8192
1161 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1162 #define ELF_NREG 34 /* gprs and pc, sr */
1163 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1165 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1166 const CPUOpenRISCState *env)
1168 int i;
1170 for (i = 0; i < 32; i++) {
1171 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1173 (*regs)[32] = tswapreg(env->pc);
1174 (*regs)[33] = tswapreg(cpu_get_sr(env));
1176 #define ELF_HWCAP 0
1177 #define ELF_PLATFORM NULL
1179 #endif /* TARGET_OPENRISC */
1181 #ifdef TARGET_SH4
1183 #define ELF_START_MMAP 0x80000000
1185 #define ELF_CLASS ELFCLASS32
1186 #define ELF_ARCH EM_SH
1188 static inline void init_thread(struct target_pt_regs *regs,
1189 struct image_info *infop)
1191 /* Check other registers XXXXX */
1192 regs->pc = infop->entry;
1193 regs->regs[15] = infop->start_stack;
1196 /* See linux kernel: arch/sh/include/asm/elf.h. */
1197 #define ELF_NREG 23
1198 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1200 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1201 enum {
1202 TARGET_REG_PC = 16,
1203 TARGET_REG_PR = 17,
1204 TARGET_REG_SR = 18,
1205 TARGET_REG_GBR = 19,
1206 TARGET_REG_MACH = 20,
1207 TARGET_REG_MACL = 21,
1208 TARGET_REG_SYSCALL = 22
1211 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1212 const CPUSH4State *env)
1214 int i;
1216 for (i = 0; i < 16; i++) {
1217 (*regs)[i] = tswapreg(env->gregs[i]);
1220 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1221 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1222 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1223 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1224 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1225 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1226 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1229 #define USE_ELF_CORE_DUMP
1230 #define ELF_EXEC_PAGESIZE 4096
1232 enum {
1233 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1234 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1235 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1236 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1237 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1238 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1239 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1240 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1241 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1242 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1245 #define ELF_HWCAP get_elf_hwcap()
1247 static uint32_t get_elf_hwcap(void)
1249 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1250 uint32_t hwcap = 0;
1252 hwcap |= SH_CPU_HAS_FPU;
1254 if (cpu->env.features & SH_FEATURE_SH4A) {
1255 hwcap |= SH_CPU_HAS_LLSC;
1258 return hwcap;
1261 #endif
1263 #ifdef TARGET_CRIS
1265 #define ELF_START_MMAP 0x80000000
1267 #define ELF_CLASS ELFCLASS32
1268 #define ELF_ARCH EM_CRIS
1270 static inline void init_thread(struct target_pt_regs *regs,
1271 struct image_info *infop)
1273 regs->erp = infop->entry;
1276 #define ELF_EXEC_PAGESIZE 8192
1278 #endif
1280 #ifdef TARGET_M68K
1282 #define ELF_START_MMAP 0x80000000
1284 #define ELF_CLASS ELFCLASS32
1285 #define ELF_ARCH EM_68K
1287 /* ??? Does this need to do anything?
1288 #define ELF_PLAT_INIT(_r) */
1290 static inline void init_thread(struct target_pt_regs *regs,
1291 struct image_info *infop)
1293 regs->usp = infop->start_stack;
1294 regs->sr = 0;
1295 regs->pc = infop->entry;
1298 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1299 #define ELF_NREG 20
1300 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1302 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1304 (*regs)[0] = tswapreg(env->dregs[1]);
1305 (*regs)[1] = tswapreg(env->dregs[2]);
1306 (*regs)[2] = tswapreg(env->dregs[3]);
1307 (*regs)[3] = tswapreg(env->dregs[4]);
1308 (*regs)[4] = tswapreg(env->dregs[5]);
1309 (*regs)[5] = tswapreg(env->dregs[6]);
1310 (*regs)[6] = tswapreg(env->dregs[7]);
1311 (*regs)[7] = tswapreg(env->aregs[0]);
1312 (*regs)[8] = tswapreg(env->aregs[1]);
1313 (*regs)[9] = tswapreg(env->aregs[2]);
1314 (*regs)[10] = tswapreg(env->aregs[3]);
1315 (*regs)[11] = tswapreg(env->aregs[4]);
1316 (*regs)[12] = tswapreg(env->aregs[5]);
1317 (*regs)[13] = tswapreg(env->aregs[6]);
1318 (*regs)[14] = tswapreg(env->dregs[0]);
1319 (*regs)[15] = tswapreg(env->aregs[7]);
1320 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1321 (*regs)[17] = tswapreg(env->sr);
1322 (*regs)[18] = tswapreg(env->pc);
1323 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1326 #define USE_ELF_CORE_DUMP
1327 #define ELF_EXEC_PAGESIZE 8192
1329 #endif
1331 #ifdef TARGET_ALPHA
1333 #define ELF_START_MMAP (0x30000000000ULL)
1335 #define ELF_CLASS ELFCLASS64
1336 #define ELF_ARCH EM_ALPHA
1338 static inline void init_thread(struct target_pt_regs *regs,
1339 struct image_info *infop)
1341 regs->pc = infop->entry;
1342 regs->ps = 8;
1343 regs->usp = infop->start_stack;
1346 #define ELF_EXEC_PAGESIZE 8192
1348 #endif /* TARGET_ALPHA */
1350 #ifdef TARGET_S390X
1352 #define ELF_START_MMAP (0x20000000000ULL)
1354 #define ELF_CLASS ELFCLASS64
1355 #define ELF_DATA ELFDATA2MSB
1356 #define ELF_ARCH EM_S390
1358 #include "elf.h"
1360 #define ELF_HWCAP get_elf_hwcap()
1362 #define GET_FEATURE(_feat, _hwcap) \
1363 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1365 static uint32_t get_elf_hwcap(void)
1368 * Let's assume we always have esan3 and zarch.
1369 * 31-bit processes can use 64-bit registers (high gprs).
1371 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1373 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1374 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1375 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1376 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1377 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1378 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1379 hwcap |= HWCAP_S390_ETF3EH;
1381 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1382 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT);
1384 return hwcap;
1387 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1389 regs->psw.addr = infop->entry;
1390 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1391 regs->gprs[15] = infop->start_stack;
1394 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1395 #define ELF_NREG 27
1396 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1398 enum {
1399 TARGET_REG_PSWM = 0,
1400 TARGET_REG_PSWA = 1,
1401 TARGET_REG_GPRS = 2,
1402 TARGET_REG_ARS = 18,
1403 TARGET_REG_ORIG_R2 = 26,
1406 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1407 const CPUS390XState *env)
1409 int i;
1410 uint32_t *aregs;
1412 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask);
1413 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr);
1414 for (i = 0; i < 16; i++) {
1415 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]);
1417 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]);
1418 for (i = 0; i < 16; i++) {
1419 aregs[i] = tswap32(env->aregs[i]);
1421 (*regs)[TARGET_REG_ORIG_R2] = 0;
1424 #define USE_ELF_CORE_DUMP
1425 #define ELF_EXEC_PAGESIZE 4096
1427 #endif /* TARGET_S390X */
1429 #ifdef TARGET_RISCV
1431 #define ELF_START_MMAP 0x80000000
1432 #define ELF_ARCH EM_RISCV
1434 #ifdef TARGET_RISCV32
1435 #define ELF_CLASS ELFCLASS32
1436 #else
1437 #define ELF_CLASS ELFCLASS64
1438 #endif
1440 #define ELF_HWCAP get_elf_hwcap()
1442 static uint32_t get_elf_hwcap(void)
1444 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1445 RISCVCPU *cpu = RISCV_CPU(thread_cpu);
1446 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1447 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C');
1449 return cpu->env.misa & mask;
1450 #undef MISA_BIT
1453 static inline void init_thread(struct target_pt_regs *regs,
1454 struct image_info *infop)
1456 regs->sepc = infop->entry;
1457 regs->sp = infop->start_stack;
1460 #define ELF_EXEC_PAGESIZE 4096
1462 #endif /* TARGET_RISCV */
1464 #ifdef TARGET_HPPA
1466 #define ELF_START_MMAP 0x80000000
1467 #define ELF_CLASS ELFCLASS32
1468 #define ELF_ARCH EM_PARISC
1469 #define ELF_PLATFORM "PARISC"
1470 #define STACK_GROWS_DOWN 0
1471 #define STACK_ALIGNMENT 64
1473 static inline void init_thread(struct target_pt_regs *regs,
1474 struct image_info *infop)
1476 regs->iaoq[0] = infop->entry;
1477 regs->iaoq[1] = infop->entry + 4;
1478 regs->gr[23] = 0;
1479 regs->gr[24] = infop->arg_start;
1480 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1481 /* The top-of-stack contains a linkage buffer. */
1482 regs->gr[30] = infop->start_stack + 64;
1483 regs->gr[31] = infop->entry;
1486 #endif /* TARGET_HPPA */
1488 #ifdef TARGET_XTENSA
1490 #define ELF_START_MMAP 0x20000000
1492 #define ELF_CLASS ELFCLASS32
1493 #define ELF_ARCH EM_XTENSA
1495 static inline void init_thread(struct target_pt_regs *regs,
1496 struct image_info *infop)
1498 regs->windowbase = 0;
1499 regs->windowstart = 1;
1500 regs->areg[1] = infop->start_stack;
1501 regs->pc = infop->entry;
1504 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1505 #define ELF_NREG 128
1506 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1508 enum {
1509 TARGET_REG_PC,
1510 TARGET_REG_PS,
1511 TARGET_REG_LBEG,
1512 TARGET_REG_LEND,
1513 TARGET_REG_LCOUNT,
1514 TARGET_REG_SAR,
1515 TARGET_REG_WINDOWSTART,
1516 TARGET_REG_WINDOWBASE,
1517 TARGET_REG_THREADPTR,
1518 TARGET_REG_AR0 = 64,
1521 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1522 const CPUXtensaState *env)
1524 unsigned i;
1526 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1527 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1528 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1529 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1530 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1531 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1532 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1533 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1534 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1535 xtensa_sync_phys_from_window((CPUXtensaState *)env);
1536 for (i = 0; i < env->config->nareg; ++i) {
1537 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1541 #define USE_ELF_CORE_DUMP
1542 #define ELF_EXEC_PAGESIZE 4096
1544 #endif /* TARGET_XTENSA */
1546 #ifdef TARGET_HEXAGON
1548 #define ELF_START_MMAP 0x20000000
1550 #define ELF_CLASS ELFCLASS32
1551 #define ELF_ARCH EM_HEXAGON
1553 static inline void init_thread(struct target_pt_regs *regs,
1554 struct image_info *infop)
1556 regs->sepc = infop->entry;
1557 regs->sp = infop->start_stack;
1560 #endif /* TARGET_HEXAGON */
1562 #ifndef ELF_PLATFORM
1563 #define ELF_PLATFORM (NULL)
1564 #endif
1566 #ifndef ELF_MACHINE
1567 #define ELF_MACHINE ELF_ARCH
1568 #endif
1570 #ifndef elf_check_arch
1571 #define elf_check_arch(x) ((x) == ELF_ARCH)
1572 #endif
1574 #ifndef elf_check_abi
1575 #define elf_check_abi(x) (1)
1576 #endif
1578 #ifndef ELF_HWCAP
1579 #define ELF_HWCAP 0
1580 #endif
1582 #ifndef STACK_GROWS_DOWN
1583 #define STACK_GROWS_DOWN 1
1584 #endif
1586 #ifndef STACK_ALIGNMENT
1587 #define STACK_ALIGNMENT 16
1588 #endif
1590 #ifdef TARGET_ABI32
1591 #undef ELF_CLASS
1592 #define ELF_CLASS ELFCLASS32
1593 #undef bswaptls
1594 #define bswaptls(ptr) bswap32s(ptr)
1595 #endif
1597 #include "elf.h"
1599 /* We must delay the following stanzas until after "elf.h". */
1600 #if defined(TARGET_AARCH64)
1602 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1603 const uint32_t *data,
1604 struct image_info *info,
1605 Error **errp)
1607 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
1608 if (pr_datasz != sizeof(uint32_t)) {
1609 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
1610 return false;
1612 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
1613 info->note_flags = *data;
1615 return true;
1617 #define ARCH_USE_GNU_PROPERTY 1
1619 #else
1621 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1622 const uint32_t *data,
1623 struct image_info *info,
1624 Error **errp)
1626 g_assert_not_reached();
1628 #define ARCH_USE_GNU_PROPERTY 0
1630 #endif
1632 struct exec
1634 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1635 unsigned int a_text; /* length of text, in bytes */
1636 unsigned int a_data; /* length of data, in bytes */
1637 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1638 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1639 unsigned int a_entry; /* start address */
1640 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1641 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1645 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1646 #define OMAGIC 0407
1647 #define NMAGIC 0410
1648 #define ZMAGIC 0413
1649 #define QMAGIC 0314
1651 /* Necessary parameters */
1652 #define TARGET_ELF_EXEC_PAGESIZE \
1653 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1654 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1655 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1656 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1657 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1658 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1660 #define DLINFO_ITEMS 16
1662 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1664 memcpy(to, from, n);
1667 #ifdef BSWAP_NEEDED
1668 static void bswap_ehdr(struct elfhdr *ehdr)
1670 bswap16s(&ehdr->e_type); /* Object file type */
1671 bswap16s(&ehdr->e_machine); /* Architecture */
1672 bswap32s(&ehdr->e_version); /* Object file version */
1673 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1674 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1675 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1676 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1677 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1678 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1679 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1680 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1681 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1682 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1685 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1687 int i;
1688 for (i = 0; i < phnum; ++i, ++phdr) {
1689 bswap32s(&phdr->p_type); /* Segment type */
1690 bswap32s(&phdr->p_flags); /* Segment flags */
1691 bswaptls(&phdr->p_offset); /* Segment file offset */
1692 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1693 bswaptls(&phdr->p_paddr); /* Segment physical address */
1694 bswaptls(&phdr->p_filesz); /* Segment size in file */
1695 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1696 bswaptls(&phdr->p_align); /* Segment alignment */
1700 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1702 int i;
1703 for (i = 0; i < shnum; ++i, ++shdr) {
1704 bswap32s(&shdr->sh_name);
1705 bswap32s(&shdr->sh_type);
1706 bswaptls(&shdr->sh_flags);
1707 bswaptls(&shdr->sh_addr);
1708 bswaptls(&shdr->sh_offset);
1709 bswaptls(&shdr->sh_size);
1710 bswap32s(&shdr->sh_link);
1711 bswap32s(&shdr->sh_info);
1712 bswaptls(&shdr->sh_addralign);
1713 bswaptls(&shdr->sh_entsize);
1717 static void bswap_sym(struct elf_sym *sym)
1719 bswap32s(&sym->st_name);
1720 bswaptls(&sym->st_value);
1721 bswaptls(&sym->st_size);
1722 bswap16s(&sym->st_shndx);
1725 #ifdef TARGET_MIPS
1726 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
1728 bswap16s(&abiflags->version);
1729 bswap32s(&abiflags->ases);
1730 bswap32s(&abiflags->isa_ext);
1731 bswap32s(&abiflags->flags1);
1732 bswap32s(&abiflags->flags2);
1734 #endif
1735 #else
1736 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1737 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1738 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1739 static inline void bswap_sym(struct elf_sym *sym) { }
1740 #ifdef TARGET_MIPS
1741 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
1742 #endif
1743 #endif
1745 #ifdef USE_ELF_CORE_DUMP
1746 static int elf_core_dump(int, const CPUArchState *);
1747 #endif /* USE_ELF_CORE_DUMP */
1748 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1750 /* Verify the portions of EHDR within E_IDENT for the target.
1751 This can be performed before bswapping the entire header. */
1752 static bool elf_check_ident(struct elfhdr *ehdr)
1754 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1755 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1756 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1757 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1758 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1759 && ehdr->e_ident[EI_DATA] == ELF_DATA
1760 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1763 /* Verify the portions of EHDR outside of E_IDENT for the target.
1764 This has to wait until after bswapping the header. */
1765 static bool elf_check_ehdr(struct elfhdr *ehdr)
1767 return (elf_check_arch(ehdr->e_machine)
1768 && elf_check_abi(ehdr->e_flags)
1769 && ehdr->e_ehsize == sizeof(struct elfhdr)
1770 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1771 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1775 * 'copy_elf_strings()' copies argument/envelope strings from user
1776 * memory to free pages in kernel mem. These are in a format ready
1777 * to be put directly into the top of new user memory.
1780 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1781 abi_ulong p, abi_ulong stack_limit)
1783 char *tmp;
1784 int len, i;
1785 abi_ulong top = p;
1787 if (!p) {
1788 return 0; /* bullet-proofing */
1791 if (STACK_GROWS_DOWN) {
1792 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1793 for (i = argc - 1; i >= 0; --i) {
1794 tmp = argv[i];
1795 if (!tmp) {
1796 fprintf(stderr, "VFS: argc is wrong");
1797 exit(-1);
1799 len = strlen(tmp) + 1;
1800 tmp += len;
1802 if (len > (p - stack_limit)) {
1803 return 0;
1805 while (len) {
1806 int bytes_to_copy = (len > offset) ? offset : len;
1807 tmp -= bytes_to_copy;
1808 p -= bytes_to_copy;
1809 offset -= bytes_to_copy;
1810 len -= bytes_to_copy;
1812 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1814 if (offset == 0) {
1815 memcpy_to_target(p, scratch, top - p);
1816 top = p;
1817 offset = TARGET_PAGE_SIZE;
1821 if (p != top) {
1822 memcpy_to_target(p, scratch + offset, top - p);
1824 } else {
1825 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1826 for (i = 0; i < argc; ++i) {
1827 tmp = argv[i];
1828 if (!tmp) {
1829 fprintf(stderr, "VFS: argc is wrong");
1830 exit(-1);
1832 len = strlen(tmp) + 1;
1833 if (len > (stack_limit - p)) {
1834 return 0;
1836 while (len) {
1837 int bytes_to_copy = (len > remaining) ? remaining : len;
1839 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1841 tmp += bytes_to_copy;
1842 remaining -= bytes_to_copy;
1843 p += bytes_to_copy;
1844 len -= bytes_to_copy;
1846 if (remaining == 0) {
1847 memcpy_to_target(top, scratch, p - top);
1848 top = p;
1849 remaining = TARGET_PAGE_SIZE;
1853 if (p != top) {
1854 memcpy_to_target(top, scratch, p - top);
1858 return p;
1861 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1862 * argument/environment space. Newer kernels (>2.6.33) allow more,
1863 * dependent on stack size, but guarantee at least 32 pages for
1864 * backwards compatibility.
1866 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1868 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1869 struct image_info *info)
1871 abi_ulong size, error, guard;
1873 size = guest_stack_size;
1874 if (size < STACK_LOWER_LIMIT) {
1875 size = STACK_LOWER_LIMIT;
1877 guard = TARGET_PAGE_SIZE;
1878 if (guard < qemu_real_host_page_size) {
1879 guard = qemu_real_host_page_size;
1882 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1883 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1884 if (error == -1) {
1885 perror("mmap stack");
1886 exit(-1);
1889 /* We reserve one extra page at the top of the stack as guard. */
1890 if (STACK_GROWS_DOWN) {
1891 target_mprotect(error, guard, PROT_NONE);
1892 info->stack_limit = error + guard;
1893 return info->stack_limit + size - sizeof(void *);
1894 } else {
1895 target_mprotect(error + size, guard, PROT_NONE);
1896 info->stack_limit = error + size;
1897 return error;
1901 /* Map and zero the bss. We need to explicitly zero any fractional pages
1902 after the data section (i.e. bss). */
1903 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1905 uintptr_t host_start, host_map_start, host_end;
1907 last_bss = TARGET_PAGE_ALIGN(last_bss);
1909 /* ??? There is confusion between qemu_real_host_page_size and
1910 qemu_host_page_size here and elsewhere in target_mmap, which
1911 may lead to the end of the data section mapping from the file
1912 not being mapped. At least there was an explicit test and
1913 comment for that here, suggesting that "the file size must
1914 be known". The comment probably pre-dates the introduction
1915 of the fstat system call in target_mmap which does in fact
1916 find out the size. What isn't clear is if the workaround
1917 here is still actually needed. For now, continue with it,
1918 but merge it with the "normal" mmap that would allocate the bss. */
1920 host_start = (uintptr_t) g2h_untagged(elf_bss);
1921 host_end = (uintptr_t) g2h_untagged(last_bss);
1922 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1924 if (host_map_start < host_end) {
1925 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1926 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1927 if (p == MAP_FAILED) {
1928 perror("cannot mmap brk");
1929 exit(-1);
1933 /* Ensure that the bss page(s) are valid */
1934 if ((page_get_flags(last_bss-1) & prot) != prot) {
1935 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1938 if (host_start < host_map_start) {
1939 memset((void *)host_start, 0, host_map_start - host_start);
1943 #ifdef TARGET_ARM
1944 static int elf_is_fdpic(struct elfhdr *exec)
1946 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
1948 #else
1949 /* Default implementation, always false. */
1950 static int elf_is_fdpic(struct elfhdr *exec)
1952 return 0;
1954 #endif
1956 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1958 uint16_t n;
1959 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1961 /* elf32_fdpic_loadseg */
1962 n = info->nsegs;
1963 while (n--) {
1964 sp -= 12;
1965 put_user_u32(loadsegs[n].addr, sp+0);
1966 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1967 put_user_u32(loadsegs[n].p_memsz, sp+8);
1970 /* elf32_fdpic_loadmap */
1971 sp -= 4;
1972 put_user_u16(0, sp+0); /* version */
1973 put_user_u16(info->nsegs, sp+2); /* nsegs */
1975 info->personality = PER_LINUX_FDPIC;
1976 info->loadmap_addr = sp;
1978 return sp;
1981 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1982 struct elfhdr *exec,
1983 struct image_info *info,
1984 struct image_info *interp_info)
1986 abi_ulong sp;
1987 abi_ulong u_argc, u_argv, u_envp, u_auxv;
1988 int size;
1989 int i;
1990 abi_ulong u_rand_bytes;
1991 uint8_t k_rand_bytes[16];
1992 abi_ulong u_platform;
1993 const char *k_platform;
1994 const int n = sizeof(elf_addr_t);
1996 sp = p;
1998 /* Needs to be before we load the env/argc/... */
1999 if (elf_is_fdpic(exec)) {
2000 /* Need 4 byte alignment for these structs */
2001 sp &= ~3;
2002 sp = loader_build_fdpic_loadmap(info, sp);
2003 info->other_info = interp_info;
2004 if (interp_info) {
2005 interp_info->other_info = info;
2006 sp = loader_build_fdpic_loadmap(interp_info, sp);
2007 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
2008 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
2009 } else {
2010 info->interpreter_loadmap_addr = 0;
2011 info->interpreter_pt_dynamic_addr = 0;
2015 u_platform = 0;
2016 k_platform = ELF_PLATFORM;
2017 if (k_platform) {
2018 size_t len = strlen(k_platform) + 1;
2019 if (STACK_GROWS_DOWN) {
2020 sp -= (len + n - 1) & ~(n - 1);
2021 u_platform = sp;
2022 /* FIXME - check return value of memcpy_to_target() for failure */
2023 memcpy_to_target(sp, k_platform, len);
2024 } else {
2025 memcpy_to_target(sp, k_platform, len);
2026 u_platform = sp;
2027 sp += len + 1;
2031 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2032 * the argv and envp pointers.
2034 if (STACK_GROWS_DOWN) {
2035 sp = QEMU_ALIGN_DOWN(sp, 16);
2036 } else {
2037 sp = QEMU_ALIGN_UP(sp, 16);
2041 * Generate 16 random bytes for userspace PRNG seeding.
2043 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
2044 if (STACK_GROWS_DOWN) {
2045 sp -= 16;
2046 u_rand_bytes = sp;
2047 /* FIXME - check return value of memcpy_to_target() for failure */
2048 memcpy_to_target(sp, k_rand_bytes, 16);
2049 } else {
2050 memcpy_to_target(sp, k_rand_bytes, 16);
2051 u_rand_bytes = sp;
2052 sp += 16;
2055 size = (DLINFO_ITEMS + 1) * 2;
2056 if (k_platform)
2057 size += 2;
2058 #ifdef DLINFO_ARCH_ITEMS
2059 size += DLINFO_ARCH_ITEMS * 2;
2060 #endif
2061 #ifdef ELF_HWCAP2
2062 size += 2;
2063 #endif
2064 info->auxv_len = size * n;
2066 size += envc + argc + 2;
2067 size += 1; /* argc itself */
2068 size *= n;
2070 /* Allocate space and finalize stack alignment for entry now. */
2071 if (STACK_GROWS_DOWN) {
2072 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
2073 sp = u_argc;
2074 } else {
2075 u_argc = sp;
2076 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2079 u_argv = u_argc + n;
2080 u_envp = u_argv + (argc + 1) * n;
2081 u_auxv = u_envp + (envc + 1) * n;
2082 info->saved_auxv = u_auxv;
2083 info->arg_start = u_argv;
2084 info->arg_end = u_argv + argc * n;
2086 /* This is correct because Linux defines
2087 * elf_addr_t as Elf32_Off / Elf64_Off
2089 #define NEW_AUX_ENT(id, val) do { \
2090 put_user_ual(id, u_auxv); u_auxv += n; \
2091 put_user_ual(val, u_auxv); u_auxv += n; \
2092 } while(0)
2094 #ifdef ARCH_DLINFO
2096 * ARCH_DLINFO must come first so platform specific code can enforce
2097 * special alignment requirements on the AUXV if necessary (eg. PPC).
2099 ARCH_DLINFO;
2100 #endif
2101 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2102 * on info->auxv_len will trigger.
2104 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2105 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2106 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2107 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2108 /* Target doesn't support host page size alignment */
2109 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2110 } else {
2111 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2112 qemu_host_page_size)));
2114 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2115 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2116 NEW_AUX_ENT(AT_ENTRY, info->entry);
2117 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2118 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2119 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2120 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2121 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2122 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2123 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2124 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2125 NEW_AUX_ENT(AT_EXECFN, info->file_string);
2127 #ifdef ELF_HWCAP2
2128 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2129 #endif
2131 if (u_platform) {
2132 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2134 NEW_AUX_ENT (AT_NULL, 0);
2135 #undef NEW_AUX_ENT
2137 /* Check that our initial calculation of the auxv length matches how much
2138 * we actually put into it.
2140 assert(info->auxv_len == u_auxv - info->saved_auxv);
2142 put_user_ual(argc, u_argc);
2144 p = info->arg_strings;
2145 for (i = 0; i < argc; ++i) {
2146 put_user_ual(p, u_argv);
2147 u_argv += n;
2148 p += target_strlen(p) + 1;
2150 put_user_ual(0, u_argv);
2152 p = info->env_strings;
2153 for (i = 0; i < envc; ++i) {
2154 put_user_ual(p, u_envp);
2155 u_envp += n;
2156 p += target_strlen(p) + 1;
2158 put_user_ual(0, u_envp);
2160 return sp;
2163 #ifndef ARM_COMMPAGE
2164 #define ARM_COMMPAGE 0
2165 #define init_guest_commpage() true
2166 #endif
2168 static void pgb_fail_in_use(const char *image_name)
2170 error_report("%s: requires virtual address space that is in use "
2171 "(omit the -B option or choose a different value)",
2172 image_name);
2173 exit(EXIT_FAILURE);
2176 static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
2177 abi_ulong guest_hiaddr, long align)
2179 const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2180 void *addr, *test;
2182 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2183 fprintf(stderr, "Requested guest base %p does not satisfy "
2184 "host minimum alignment (0x%lx)\n",
2185 (void *)guest_base, align);
2186 exit(EXIT_FAILURE);
2189 /* Sanity check the guest binary. */
2190 if (reserved_va) {
2191 if (guest_hiaddr > reserved_va) {
2192 error_report("%s: requires more than reserved virtual "
2193 "address space (0x%" PRIx64 " > 0x%lx)",
2194 image_name, (uint64_t)guest_hiaddr, reserved_va);
2195 exit(EXIT_FAILURE);
2197 } else {
2198 #if HOST_LONG_BITS < TARGET_ABI_BITS
2199 if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) {
2200 error_report("%s: requires more virtual address space "
2201 "than the host can provide (0x%" PRIx64 ")",
2202 image_name, (uint64_t)guest_hiaddr - guest_base);
2203 exit(EXIT_FAILURE);
2205 #endif
2209 * Expand the allocation to the entire reserved_va.
2210 * Exclude the mmap_min_addr hole.
2212 if (reserved_va) {
2213 guest_loaddr = (guest_base >= mmap_min_addr ? 0
2214 : mmap_min_addr - guest_base);
2215 guest_hiaddr = reserved_va;
2218 /* Reserve the address space for the binary, or reserved_va. */
2219 test = g2h_untagged(guest_loaddr);
2220 addr = mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0);
2221 if (test != addr) {
2222 pgb_fail_in_use(image_name);
2227 * pgd_find_hole_fallback: potential mmap address
2228 * @guest_size: size of available space
2229 * @brk: location of break
2230 * @align: memory alignment
2232 * This is a fallback method for finding a hole in the host address
2233 * space if we don't have the benefit of being able to access
2234 * /proc/self/map. It can potentially take a very long time as we can
2235 * only dumbly iterate up the host address space seeing if the
2236 * allocation would work.
2238 static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
2239 long align, uintptr_t offset)
2241 uintptr_t base;
2243 /* Start (aligned) at the bottom and work our way up */
2244 base = ROUND_UP(mmap_min_addr, align);
2246 while (true) {
2247 uintptr_t align_start, end;
2248 align_start = ROUND_UP(base, align);
2249 end = align_start + guest_size + offset;
2251 /* if brk is anywhere in the range give ourselves some room to grow. */
2252 if (align_start <= brk && brk < end) {
2253 base = brk + (16 * MiB);
2254 continue;
2255 } else if (align_start + guest_size < align_start) {
2256 /* we have run out of space */
2257 return -1;
2258 } else {
2259 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE |
2260 MAP_FIXED_NOREPLACE;
2261 void * mmap_start = mmap((void *) align_start, guest_size,
2262 PROT_NONE, flags, -1, 0);
2263 if (mmap_start != MAP_FAILED) {
2264 munmap(mmap_start, guest_size);
2265 if (mmap_start == (void *) align_start) {
2266 return (uintptr_t) mmap_start + offset;
2269 base += qemu_host_page_size;
2274 /* Return value for guest_base, or -1 if no hole found. */
2275 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
2276 long align, uintptr_t offset)
2278 GSList *maps, *iter;
2279 uintptr_t this_start, this_end, next_start, brk;
2280 intptr_t ret = -1;
2282 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
2284 maps = read_self_maps();
2286 /* Read brk after we've read the maps, which will malloc. */
2287 brk = (uintptr_t)sbrk(0);
2289 if (!maps) {
2290 ret = pgd_find_hole_fallback(guest_size, brk, align, offset);
2291 return ret == -1 ? -1 : ret - guest_loaddr;
2294 /* The first hole is before the first map entry. */
2295 this_start = mmap_min_addr;
2297 for (iter = maps; iter;
2298 this_start = next_start, iter = g_slist_next(iter)) {
2299 uintptr_t align_start, hole_size;
2301 this_end = ((MapInfo *)iter->data)->start;
2302 next_start = ((MapInfo *)iter->data)->end;
2303 align_start = ROUND_UP(this_start + offset, align);
2305 /* Skip holes that are too small. */
2306 if (align_start >= this_end) {
2307 continue;
2309 hole_size = this_end - align_start;
2310 if (hole_size < guest_size) {
2311 continue;
2314 /* If this hole contains brk, give ourselves some room to grow. */
2315 if (this_start <= brk && brk < this_end) {
2316 hole_size -= guest_size;
2317 if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) {
2318 align_start += 1 * GiB;
2319 } else if (hole_size >= 16 * MiB) {
2320 align_start += 16 * MiB;
2321 } else {
2322 align_start = (this_end - guest_size) & -align;
2323 if (align_start < this_start) {
2324 continue;
2329 /* Record the lowest successful match. */
2330 if (ret < 0) {
2331 ret = align_start - guest_loaddr;
2333 /* If this hole contains the identity map, select it. */
2334 if (align_start <= guest_loaddr &&
2335 guest_loaddr + guest_size <= this_end) {
2336 ret = 0;
2338 /* If this hole ends above the identity map, stop looking. */
2339 if (this_end >= guest_loaddr) {
2340 break;
2343 free_self_maps(maps);
2345 return ret;
2348 static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
2349 abi_ulong orig_hiaddr, long align)
2351 uintptr_t loaddr = orig_loaddr;
2352 uintptr_t hiaddr = orig_hiaddr;
2353 uintptr_t offset = 0;
2354 uintptr_t addr;
2356 if (hiaddr != orig_hiaddr) {
2357 error_report("%s: requires virtual address space that the "
2358 "host cannot provide (0x%" PRIx64 ")",
2359 image_name, (uint64_t)orig_hiaddr);
2360 exit(EXIT_FAILURE);
2363 loaddr &= -align;
2364 if (ARM_COMMPAGE) {
2366 * Extend the allocation to include the commpage.
2367 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2368 * need to ensure there is space bellow the guest_base so we
2369 * can map the commpage in the place needed when the address
2370 * arithmetic wraps around.
2372 if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
2373 hiaddr = (uintptr_t) 4 << 30;
2374 } else {
2375 offset = -(ARM_COMMPAGE & -align);
2379 addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
2380 if (addr == -1) {
2382 * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
2383 * that can satisfy both. But as the normal arm32 link base address
2384 * is ~32k, and we extend down to include the commpage, making the
2385 * overhead only ~96k, this is unlikely.
2387 error_report("%s: Unable to allocate %#zx bytes of "
2388 "virtual address space", image_name,
2389 (size_t)(hiaddr - loaddr));
2390 exit(EXIT_FAILURE);
2393 guest_base = addr;
2396 static void pgb_dynamic(const char *image_name, long align)
2399 * The executable is dynamic and does not require a fixed address.
2400 * All we need is a commpage that satisfies align.
2401 * If we do not need a commpage, leave guest_base == 0.
2403 if (ARM_COMMPAGE) {
2404 uintptr_t addr, commpage;
2406 /* 64-bit hosts should have used reserved_va. */
2407 assert(sizeof(uintptr_t) == 4);
2410 * By putting the commpage at the first hole, that puts guest_base
2411 * just above that, and maximises the positive guest addresses.
2413 commpage = ARM_COMMPAGE & -align;
2414 addr = pgb_find_hole(commpage, -commpage, align, 0);
2415 assert(addr != -1);
2416 guest_base = addr;
2420 static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
2421 abi_ulong guest_hiaddr, long align)
2423 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2424 void *addr, *test;
2426 if (guest_hiaddr > reserved_va) {
2427 error_report("%s: requires more than reserved virtual "
2428 "address space (0x%" PRIx64 " > 0x%lx)",
2429 image_name, (uint64_t)guest_hiaddr, reserved_va);
2430 exit(EXIT_FAILURE);
2433 /* Widen the "image" to the entire reserved address space. */
2434 pgb_static(image_name, 0, reserved_va, align);
2436 /* osdep.h defines this as 0 if it's missing */
2437 flags |= MAP_FIXED_NOREPLACE;
2439 /* Reserve the memory on the host. */
2440 assert(guest_base != 0);
2441 test = g2h_untagged(0);
2442 addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0);
2443 if (addr == MAP_FAILED || addr != test) {
2444 error_report("Unable to reserve 0x%lx bytes of virtual address "
2445 "space at %p (%s) for use as guest address space (check your"
2446 "virtual memory ulimit setting, min_mmap_addr or reserve less "
2447 "using -R option)", reserved_va, test, strerror(errno));
2448 exit(EXIT_FAILURE);
2452 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
2453 abi_ulong guest_hiaddr)
2455 /* In order to use host shmat, we must be able to honor SHMLBA. */
2456 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
2458 if (have_guest_base) {
2459 pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align);
2460 } else if (reserved_va) {
2461 pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align);
2462 } else if (guest_loaddr) {
2463 pgb_static(image_name, guest_loaddr, guest_hiaddr, align);
2464 } else {
2465 pgb_dynamic(image_name, align);
2468 /* Reserve and initialize the commpage. */
2469 if (!init_guest_commpage()) {
2471 * With have_guest_base, the user has selected the address and
2472 * we are trying to work with that. Otherwise, we have selected
2473 * free space and init_guest_commpage must succeeded.
2475 assert(have_guest_base);
2476 pgb_fail_in_use(image_name);
2479 assert(QEMU_IS_ALIGNED(guest_base, align));
2480 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
2481 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
2484 enum {
2485 /* The string "GNU\0" as a magic number. */
2486 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
2487 NOTE_DATA_SZ = 1 * KiB,
2488 NOTE_NAME_SZ = 4,
2489 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
2493 * Process a single gnu_property entry.
2494 * Return false for error.
2496 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
2497 struct image_info *info, bool have_prev_type,
2498 uint32_t *prev_type, Error **errp)
2500 uint32_t pr_type, pr_datasz, step;
2502 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
2503 goto error_data;
2505 datasz -= *off;
2506 data += *off / sizeof(uint32_t);
2508 if (datasz < 2 * sizeof(uint32_t)) {
2509 goto error_data;
2511 pr_type = data[0];
2512 pr_datasz = data[1];
2513 data += 2;
2514 datasz -= 2 * sizeof(uint32_t);
2515 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
2516 if (step > datasz) {
2517 goto error_data;
2520 /* Properties are supposed to be unique and sorted on pr_type. */
2521 if (have_prev_type && pr_type <= *prev_type) {
2522 if (pr_type == *prev_type) {
2523 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
2524 } else {
2525 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
2527 return false;
2529 *prev_type = pr_type;
2531 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
2532 return false;
2535 *off += 2 * sizeof(uint32_t) + step;
2536 return true;
2538 error_data:
2539 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
2540 return false;
2543 /* Process NT_GNU_PROPERTY_TYPE_0. */
2544 static bool parse_elf_properties(int image_fd,
2545 struct image_info *info,
2546 const struct elf_phdr *phdr,
2547 char bprm_buf[BPRM_BUF_SIZE],
2548 Error **errp)
2550 union {
2551 struct elf_note nhdr;
2552 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
2553 } note;
2555 int n, off, datasz;
2556 bool have_prev_type;
2557 uint32_t prev_type;
2559 /* Unless the arch requires properties, ignore them. */
2560 if (!ARCH_USE_GNU_PROPERTY) {
2561 return true;
2564 /* If the properties are crazy large, that's too bad. */
2565 n = phdr->p_filesz;
2566 if (n > sizeof(note)) {
2567 error_setg(errp, "PT_GNU_PROPERTY too large");
2568 return false;
2570 if (n < sizeof(note.nhdr)) {
2571 error_setg(errp, "PT_GNU_PROPERTY too small");
2572 return false;
2575 if (phdr->p_offset + n <= BPRM_BUF_SIZE) {
2576 memcpy(&note, bprm_buf + phdr->p_offset, n);
2577 } else {
2578 ssize_t len = pread(image_fd, &note, n, phdr->p_offset);
2579 if (len != n) {
2580 error_setg_errno(errp, errno, "Error reading file header");
2581 return false;
2586 * The contents of a valid PT_GNU_PROPERTY is a sequence
2587 * of uint32_t -- swap them all now.
2589 #ifdef BSWAP_NEEDED
2590 for (int i = 0; i < n / 4; i++) {
2591 bswap32s(note.data + i);
2593 #endif
2596 * Note that nhdr is 3 words, and that the "name" described by namesz
2597 * immediately follows nhdr and is thus at the 4th word. Further, all
2598 * of the inputs to the kernel's round_up are multiples of 4.
2600 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
2601 note.nhdr.n_namesz != NOTE_NAME_SZ ||
2602 note.data[3] != GNU0_MAGIC) {
2603 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
2604 return false;
2606 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
2608 datasz = note.nhdr.n_descsz + off;
2609 if (datasz > n) {
2610 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
2611 return false;
2614 have_prev_type = false;
2615 prev_type = 0;
2616 while (1) {
2617 if (off == datasz) {
2618 return true; /* end, exit ok */
2620 if (!parse_elf_property(note.data, &off, datasz, info,
2621 have_prev_type, &prev_type, errp)) {
2622 return false;
2624 have_prev_type = true;
2628 /* Load an ELF image into the address space.
2630 IMAGE_NAME is the filename of the image, to use in error messages.
2631 IMAGE_FD is the open file descriptor for the image.
2633 BPRM_BUF is a copy of the beginning of the file; this of course
2634 contains the elf file header at offset 0. It is assumed that this
2635 buffer is sufficiently aligned to present no problems to the host
2636 in accessing data at aligned offsets within the buffer.
2638 On return: INFO values will be filled in, as necessary or available. */
2640 static void load_elf_image(const char *image_name, int image_fd,
2641 struct image_info *info, char **pinterp_name,
2642 char bprm_buf[BPRM_BUF_SIZE])
2644 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2645 struct elf_phdr *phdr;
2646 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
2647 int i, retval, prot_exec;
2648 Error *err = NULL;
2650 /* First of all, some simple consistency checks */
2651 if (!elf_check_ident(ehdr)) {
2652 error_setg(&err, "Invalid ELF image for this architecture");
2653 goto exit_errmsg;
2655 bswap_ehdr(ehdr);
2656 if (!elf_check_ehdr(ehdr)) {
2657 error_setg(&err, "Invalid ELF image for this architecture");
2658 goto exit_errmsg;
2661 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2662 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2663 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
2664 } else {
2665 phdr = (struct elf_phdr *) alloca(i);
2666 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
2667 if (retval != i) {
2668 goto exit_read;
2671 bswap_phdr(phdr, ehdr->e_phnum);
2673 info->nsegs = 0;
2674 info->pt_dynamic_addr = 0;
2676 mmap_lock();
2679 * Find the maximum size of the image and allocate an appropriate
2680 * amount of memory to handle that. Locate the interpreter, if any.
2682 loaddr = -1, hiaddr = 0;
2683 info->alignment = 0;
2684 for (i = 0; i < ehdr->e_phnum; ++i) {
2685 struct elf_phdr *eppnt = phdr + i;
2686 if (eppnt->p_type == PT_LOAD) {
2687 abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
2688 if (a < loaddr) {
2689 loaddr = a;
2691 a = eppnt->p_vaddr + eppnt->p_memsz;
2692 if (a > hiaddr) {
2693 hiaddr = a;
2695 ++info->nsegs;
2696 info->alignment |= eppnt->p_align;
2697 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2698 g_autofree char *interp_name = NULL;
2700 if (*pinterp_name) {
2701 error_setg(&err, "Multiple PT_INTERP entries");
2702 goto exit_errmsg;
2705 interp_name = g_malloc(eppnt->p_filesz);
2707 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2708 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2709 eppnt->p_filesz);
2710 } else {
2711 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2712 eppnt->p_offset);
2713 if (retval != eppnt->p_filesz) {
2714 goto exit_read;
2717 if (interp_name[eppnt->p_filesz - 1] != 0) {
2718 error_setg(&err, "Invalid PT_INTERP entry");
2719 goto exit_errmsg;
2721 *pinterp_name = g_steal_pointer(&interp_name);
2722 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
2723 if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &err)) {
2724 goto exit_errmsg;
2729 if (pinterp_name != NULL) {
2731 * This is the main executable.
2733 * Reserve extra space for brk.
2734 * We hold on to this space while placing the interpreter
2735 * and the stack, lest they be placed immediately after
2736 * the data segment and block allocation from the brk.
2738 * 16MB is chosen as "large enough" without being so large
2739 * as to allow the result to not fit with a 32-bit guest on
2740 * a 32-bit host.
2742 info->reserve_brk = 16 * MiB;
2743 hiaddr += info->reserve_brk;
2745 if (ehdr->e_type == ET_EXEC) {
2747 * Make sure that the low address does not conflict with
2748 * MMAP_MIN_ADDR or the QEMU application itself.
2750 probe_guest_base(image_name, loaddr, hiaddr);
2751 } else {
2753 * The binary is dynamic, but we still need to
2754 * select guest_base. In this case we pass a size.
2756 probe_guest_base(image_name, 0, hiaddr - loaddr);
2761 * Reserve address space for all of this.
2763 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
2764 * exactly the address range that is required.
2766 * Otherwise this is ET_DYN, and we are searching for a location
2767 * that can hold the memory space required. If the image is
2768 * pre-linked, LOADDR will be non-zero, and the kernel should
2769 * honor that address if it happens to be free.
2771 * In both cases, we will overwrite pages in this range with mappings
2772 * from the executable.
2774 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2775 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
2776 (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0),
2777 -1, 0);
2778 if (load_addr == -1) {
2779 goto exit_mmap;
2781 load_bias = load_addr - loaddr;
2783 if (elf_is_fdpic(ehdr)) {
2784 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2785 g_malloc(sizeof(*loadsegs) * info->nsegs);
2787 for (i = 0; i < ehdr->e_phnum; ++i) {
2788 switch (phdr[i].p_type) {
2789 case PT_DYNAMIC:
2790 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2791 break;
2792 case PT_LOAD:
2793 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2794 loadsegs->p_vaddr = phdr[i].p_vaddr;
2795 loadsegs->p_memsz = phdr[i].p_memsz;
2796 ++loadsegs;
2797 break;
2802 info->load_bias = load_bias;
2803 info->code_offset = load_bias;
2804 info->data_offset = load_bias;
2805 info->load_addr = load_addr;
2806 info->entry = ehdr->e_entry + load_bias;
2807 info->start_code = -1;
2808 info->end_code = 0;
2809 info->start_data = -1;
2810 info->end_data = 0;
2811 info->brk = 0;
2812 info->elf_flags = ehdr->e_flags;
2814 prot_exec = PROT_EXEC;
2815 #ifdef TARGET_AARCH64
2817 * If the BTI feature is present, this indicates that the executable
2818 * pages of the startup binary should be mapped with PROT_BTI, so that
2819 * branch targets are enforced.
2821 * The startup binary is either the interpreter or the static executable.
2822 * The interpreter is responsible for all pages of a dynamic executable.
2824 * Elf notes are backward compatible to older cpus.
2825 * Do not enable BTI unless it is supported.
2827 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
2828 && (pinterp_name == NULL || *pinterp_name == 0)
2829 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
2830 prot_exec |= TARGET_PROT_BTI;
2832 #endif
2834 for (i = 0; i < ehdr->e_phnum; i++) {
2835 struct elf_phdr *eppnt = phdr + i;
2836 if (eppnt->p_type == PT_LOAD) {
2837 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
2838 int elf_prot = 0;
2840 if (eppnt->p_flags & PF_R) {
2841 elf_prot |= PROT_READ;
2843 if (eppnt->p_flags & PF_W) {
2844 elf_prot |= PROT_WRITE;
2846 if (eppnt->p_flags & PF_X) {
2847 elf_prot |= prot_exec;
2850 vaddr = load_bias + eppnt->p_vaddr;
2851 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2852 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2854 vaddr_ef = vaddr + eppnt->p_filesz;
2855 vaddr_em = vaddr + eppnt->p_memsz;
2858 * Some segments may be completely empty, with a non-zero p_memsz
2859 * but no backing file segment.
2861 if (eppnt->p_filesz != 0) {
2862 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
2863 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2864 MAP_PRIVATE | MAP_FIXED,
2865 image_fd, eppnt->p_offset - vaddr_po);
2867 if (error == -1) {
2868 goto exit_mmap;
2872 * If the load segment requests extra zeros (e.g. bss), map it.
2874 if (eppnt->p_filesz < eppnt->p_memsz) {
2875 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2877 } else if (eppnt->p_memsz != 0) {
2878 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_memsz + vaddr_po);
2879 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2880 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
2881 -1, 0);
2883 if (error == -1) {
2884 goto exit_mmap;
2888 /* Find the full program boundaries. */
2889 if (elf_prot & PROT_EXEC) {
2890 if (vaddr < info->start_code) {
2891 info->start_code = vaddr;
2893 if (vaddr_ef > info->end_code) {
2894 info->end_code = vaddr_ef;
2897 if (elf_prot & PROT_WRITE) {
2898 if (vaddr < info->start_data) {
2899 info->start_data = vaddr;
2901 if (vaddr_ef > info->end_data) {
2902 info->end_data = vaddr_ef;
2905 if (vaddr_em > info->brk) {
2906 info->brk = vaddr_em;
2908 #ifdef TARGET_MIPS
2909 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
2910 Mips_elf_abiflags_v0 abiflags;
2911 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
2912 error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry");
2913 goto exit_errmsg;
2915 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2916 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
2917 sizeof(Mips_elf_abiflags_v0));
2918 } else {
2919 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
2920 eppnt->p_offset);
2921 if (retval != sizeof(Mips_elf_abiflags_v0)) {
2922 goto exit_read;
2925 bswap_mips_abiflags(&abiflags);
2926 info->fp_abi = abiflags.fp_abi;
2927 #endif
2931 if (info->end_data == 0) {
2932 info->start_data = info->end_code;
2933 info->end_data = info->end_code;
2936 if (qemu_log_enabled()) {
2937 load_symbols(ehdr, image_fd, load_bias);
2940 mmap_unlock();
2942 close(image_fd);
2943 return;
2945 exit_read:
2946 if (retval >= 0) {
2947 error_setg(&err, "Incomplete read of file header");
2948 } else {
2949 error_setg_errno(&err, errno, "Error reading file header");
2951 goto exit_errmsg;
2952 exit_mmap:
2953 error_setg_errno(&err, errno, "Error mapping file");
2954 goto exit_errmsg;
2955 exit_errmsg:
2956 error_reportf_err(err, "%s: ", image_name);
2957 exit(-1);
2960 static void load_elf_interp(const char *filename, struct image_info *info,
2961 char bprm_buf[BPRM_BUF_SIZE])
2963 int fd, retval;
2964 Error *err = NULL;
2966 fd = open(path(filename), O_RDONLY);
2967 if (fd < 0) {
2968 error_setg_file_open(&err, errno, filename);
2969 error_report_err(err);
2970 exit(-1);
2973 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2974 if (retval < 0) {
2975 error_setg_errno(&err, errno, "Error reading file header");
2976 error_reportf_err(err, "%s: ", filename);
2977 exit(-1);
2980 if (retval < BPRM_BUF_SIZE) {
2981 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2984 load_elf_image(filename, fd, info, NULL, bprm_buf);
2987 static int symfind(const void *s0, const void *s1)
2989 target_ulong addr = *(target_ulong *)s0;
2990 struct elf_sym *sym = (struct elf_sym *)s1;
2991 int result = 0;
2992 if (addr < sym->st_value) {
2993 result = -1;
2994 } else if (addr >= sym->st_value + sym->st_size) {
2995 result = 1;
2997 return result;
3000 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
3002 #if ELF_CLASS == ELFCLASS32
3003 struct elf_sym *syms = s->disas_symtab.elf32;
3004 #else
3005 struct elf_sym *syms = s->disas_symtab.elf64;
3006 #endif
3008 // binary search
3009 struct elf_sym *sym;
3011 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
3012 if (sym != NULL) {
3013 return s->disas_strtab + sym->st_name;
3016 return "";
3019 /* FIXME: This should use elf_ops.h */
3020 static int symcmp(const void *s0, const void *s1)
3022 struct elf_sym *sym0 = (struct elf_sym *)s0;
3023 struct elf_sym *sym1 = (struct elf_sym *)s1;
3024 return (sym0->st_value < sym1->st_value)
3025 ? -1
3026 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
3029 /* Best attempt to load symbols from this ELF object. */
3030 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
3032 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
3033 uint64_t segsz;
3034 struct elf_shdr *shdr;
3035 char *strings = NULL;
3036 struct syminfo *s = NULL;
3037 struct elf_sym *new_syms, *syms = NULL;
3039 shnum = hdr->e_shnum;
3040 i = shnum * sizeof(struct elf_shdr);
3041 shdr = (struct elf_shdr *)alloca(i);
3042 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
3043 return;
3046 bswap_shdr(shdr, shnum);
3047 for (i = 0; i < shnum; ++i) {
3048 if (shdr[i].sh_type == SHT_SYMTAB) {
3049 sym_idx = i;
3050 str_idx = shdr[i].sh_link;
3051 goto found;
3055 /* There will be no symbol table if the file was stripped. */
3056 return;
3058 found:
3059 /* Now know where the strtab and symtab are. Snarf them. */
3060 s = g_try_new(struct syminfo, 1);
3061 if (!s) {
3062 goto give_up;
3065 segsz = shdr[str_idx].sh_size;
3066 s->disas_strtab = strings = g_try_malloc(segsz);
3067 if (!strings ||
3068 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
3069 goto give_up;
3072 segsz = shdr[sym_idx].sh_size;
3073 syms = g_try_malloc(segsz);
3074 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
3075 goto give_up;
3078 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
3079 /* Implausibly large symbol table: give up rather than ploughing
3080 * on with the number of symbols calculation overflowing
3082 goto give_up;
3084 nsyms = segsz / sizeof(struct elf_sym);
3085 for (i = 0; i < nsyms; ) {
3086 bswap_sym(syms + i);
3087 /* Throw away entries which we do not need. */
3088 if (syms[i].st_shndx == SHN_UNDEF
3089 || syms[i].st_shndx >= SHN_LORESERVE
3090 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3091 if (i < --nsyms) {
3092 syms[i] = syms[nsyms];
3094 } else {
3095 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3096 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3097 syms[i].st_value &= ~(target_ulong)1;
3098 #endif
3099 syms[i].st_value += load_bias;
3100 i++;
3104 /* No "useful" symbol. */
3105 if (nsyms == 0) {
3106 goto give_up;
3109 /* Attempt to free the storage associated with the local symbols
3110 that we threw away. Whether or not this has any effect on the
3111 memory allocation depends on the malloc implementation and how
3112 many symbols we managed to discard. */
3113 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3114 if (new_syms == NULL) {
3115 goto give_up;
3117 syms = new_syms;
3119 qsort(syms, nsyms, sizeof(*syms), symcmp);
3121 s->disas_num_syms = nsyms;
3122 #if ELF_CLASS == ELFCLASS32
3123 s->disas_symtab.elf32 = syms;
3124 #else
3125 s->disas_symtab.elf64 = syms;
3126 #endif
3127 s->lookup_symbol = lookup_symbolxx;
3128 s->next = syminfos;
3129 syminfos = s;
3131 return;
3133 give_up:
3134 g_free(s);
3135 g_free(strings);
3136 g_free(syms);
3139 uint32_t get_elf_eflags(int fd)
3141 struct elfhdr ehdr;
3142 off_t offset;
3143 int ret;
3145 /* Read ELF header */
3146 offset = lseek(fd, 0, SEEK_SET);
3147 if (offset == (off_t) -1) {
3148 return 0;
3150 ret = read(fd, &ehdr, sizeof(ehdr));
3151 if (ret < sizeof(ehdr)) {
3152 return 0;
3154 offset = lseek(fd, offset, SEEK_SET);
3155 if (offset == (off_t) -1) {
3156 return 0;
3159 /* Check ELF signature */
3160 if (!elf_check_ident(&ehdr)) {
3161 return 0;
3164 /* check header */
3165 bswap_ehdr(&ehdr);
3166 if (!elf_check_ehdr(&ehdr)) {
3167 return 0;
3170 /* return architecture id */
3171 return ehdr.e_flags;
3174 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3176 struct image_info interp_info;
3177 struct elfhdr elf_ex;
3178 char *elf_interpreter = NULL;
3179 char *scratch;
3181 memset(&interp_info, 0, sizeof(interp_info));
3182 #ifdef TARGET_MIPS
3183 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3184 #endif
3186 info->start_mmap = (abi_ulong)ELF_START_MMAP;
3188 load_elf_image(bprm->filename, bprm->fd, info,
3189 &elf_interpreter, bprm->buf);
3191 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3192 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3193 when we load the interpreter. */
3194 elf_ex = *(struct elfhdr *)bprm->buf;
3196 /* Do this so that we can load the interpreter, if need be. We will
3197 change some of these later */
3198 bprm->p = setup_arg_pages(bprm, info);
3200 scratch = g_new0(char, TARGET_PAGE_SIZE);
3201 if (STACK_GROWS_DOWN) {
3202 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3203 bprm->p, info->stack_limit);
3204 info->file_string = bprm->p;
3205 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3206 bprm->p, info->stack_limit);
3207 info->env_strings = bprm->p;
3208 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3209 bprm->p, info->stack_limit);
3210 info->arg_strings = bprm->p;
3211 } else {
3212 info->arg_strings = bprm->p;
3213 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3214 bprm->p, info->stack_limit);
3215 info->env_strings = bprm->p;
3216 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3217 bprm->p, info->stack_limit);
3218 info->file_string = bprm->p;
3219 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3220 bprm->p, info->stack_limit);
3223 g_free(scratch);
3225 if (!bprm->p) {
3226 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3227 exit(-1);
3230 if (elf_interpreter) {
3231 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3233 /* If the program interpreter is one of these two, then assume
3234 an iBCS2 image. Otherwise assume a native linux image. */
3236 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3237 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3238 info->personality = PER_SVR4;
3240 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3241 and some applications "depend" upon this behavior. Since
3242 we do not have the power to recompile these, we emulate
3243 the SVr4 behavior. Sigh. */
3244 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
3245 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3247 #ifdef TARGET_MIPS
3248 info->interp_fp_abi = interp_info.fp_abi;
3249 #endif
3252 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
3253 info, (elf_interpreter ? &interp_info : NULL));
3254 info->start_stack = bprm->p;
3256 /* If we have an interpreter, set that as the program's entry point.
3257 Copy the load_bias as well, to help PPC64 interpret the entry
3258 point as a function descriptor. Do this after creating elf tables
3259 so that we copy the original program entry point into the AUXV. */
3260 if (elf_interpreter) {
3261 info->load_bias = interp_info.load_bias;
3262 info->entry = interp_info.entry;
3263 g_free(elf_interpreter);
3266 #ifdef USE_ELF_CORE_DUMP
3267 bprm->core_dump = &elf_core_dump;
3268 #endif
3271 * If we reserved extra space for brk, release it now.
3272 * The implementation of do_brk in syscalls.c expects to be able
3273 * to mmap pages in this space.
3275 if (info->reserve_brk) {
3276 abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk);
3277 abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk);
3278 target_munmap(start_brk, end_brk - start_brk);
3281 return 0;
3284 #ifdef USE_ELF_CORE_DUMP
3286 * Definitions to generate Intel SVR4-like core files.
3287 * These mostly have the same names as the SVR4 types with "target_elf_"
3288 * tacked on the front to prevent clashes with linux definitions,
3289 * and the typedef forms have been avoided. This is mostly like
3290 * the SVR4 structure, but more Linuxy, with things that Linux does
3291 * not support and which gdb doesn't really use excluded.
3293 * Fields we don't dump (their contents is zero) in linux-user qemu
3294 * are marked with XXX.
3296 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3298 * Porting ELF coredump for target is (quite) simple process. First you
3299 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3300 * the target resides):
3302 * #define USE_ELF_CORE_DUMP
3304 * Next you define type of register set used for dumping. ELF specification
3305 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3307 * typedef <target_regtype> target_elf_greg_t;
3308 * #define ELF_NREG <number of registers>
3309 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3311 * Last step is to implement target specific function that copies registers
3312 * from given cpu into just specified register set. Prototype is:
3314 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3315 * const CPUArchState *env);
3317 * Parameters:
3318 * regs - copy register values into here (allocated and zeroed by caller)
3319 * env - copy registers from here
3321 * Example for ARM target is provided in this file.
3324 /* An ELF note in memory */
3325 struct memelfnote {
3326 const char *name;
3327 size_t namesz;
3328 size_t namesz_rounded;
3329 int type;
3330 size_t datasz;
3331 size_t datasz_rounded;
3332 void *data;
3333 size_t notesz;
3336 struct target_elf_siginfo {
3337 abi_int si_signo; /* signal number */
3338 abi_int si_code; /* extra code */
3339 abi_int si_errno; /* errno */
3342 struct target_elf_prstatus {
3343 struct target_elf_siginfo pr_info; /* Info associated with signal */
3344 abi_short pr_cursig; /* Current signal */
3345 abi_ulong pr_sigpend; /* XXX */
3346 abi_ulong pr_sighold; /* XXX */
3347 target_pid_t pr_pid;
3348 target_pid_t pr_ppid;
3349 target_pid_t pr_pgrp;
3350 target_pid_t pr_sid;
3351 struct target_timeval pr_utime; /* XXX User time */
3352 struct target_timeval pr_stime; /* XXX System time */
3353 struct target_timeval pr_cutime; /* XXX Cumulative user time */
3354 struct target_timeval pr_cstime; /* XXX Cumulative system time */
3355 target_elf_gregset_t pr_reg; /* GP registers */
3356 abi_int pr_fpvalid; /* XXX */
3359 #define ELF_PRARGSZ (80) /* Number of chars for args */
3361 struct target_elf_prpsinfo {
3362 char pr_state; /* numeric process state */
3363 char pr_sname; /* char for pr_state */
3364 char pr_zomb; /* zombie */
3365 char pr_nice; /* nice val */
3366 abi_ulong pr_flag; /* flags */
3367 target_uid_t pr_uid;
3368 target_gid_t pr_gid;
3369 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
3370 /* Lots missing */
3371 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
3372 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
3375 /* Here is the structure in which status of each thread is captured. */
3376 struct elf_thread_status {
3377 QTAILQ_ENTRY(elf_thread_status) ets_link;
3378 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
3379 #if 0
3380 elf_fpregset_t fpu; /* NT_PRFPREG */
3381 struct task_struct *thread;
3382 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
3383 #endif
3384 struct memelfnote notes[1];
3385 int num_notes;
3388 struct elf_note_info {
3389 struct memelfnote *notes;
3390 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
3391 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
3393 QTAILQ_HEAD(, elf_thread_status) thread_list;
3394 #if 0
3396 * Current version of ELF coredump doesn't support
3397 * dumping fp regs etc.
3399 elf_fpregset_t *fpu;
3400 elf_fpxregset_t *xfpu;
3401 int thread_status_size;
3402 #endif
3403 int notes_size;
3404 int numnote;
3407 struct vm_area_struct {
3408 target_ulong vma_start; /* start vaddr of memory region */
3409 target_ulong vma_end; /* end vaddr of memory region */
3410 abi_ulong vma_flags; /* protection etc. flags for the region */
3411 QTAILQ_ENTRY(vm_area_struct) vma_link;
3414 struct mm_struct {
3415 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
3416 int mm_count; /* number of mappings */
3419 static struct mm_struct *vma_init(void);
3420 static void vma_delete(struct mm_struct *);
3421 static int vma_add_mapping(struct mm_struct *, target_ulong,
3422 target_ulong, abi_ulong);
3423 static int vma_get_mapping_count(const struct mm_struct *);
3424 static struct vm_area_struct *vma_first(const struct mm_struct *);
3425 static struct vm_area_struct *vma_next(struct vm_area_struct *);
3426 static abi_ulong vma_dump_size(const struct vm_area_struct *);
3427 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3428 unsigned long flags);
3430 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
3431 static void fill_note(struct memelfnote *, const char *, int,
3432 unsigned int, void *);
3433 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
3434 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
3435 static void fill_auxv_note(struct memelfnote *, const TaskState *);
3436 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
3437 static size_t note_size(const struct memelfnote *);
3438 static void free_note_info(struct elf_note_info *);
3439 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
3440 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
3442 static int dump_write(int, const void *, size_t);
3443 static int write_note(struct memelfnote *, int);
3444 static int write_note_info(struct elf_note_info *, int);
3446 #ifdef BSWAP_NEEDED
3447 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
3449 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3450 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3451 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
3452 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
3453 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3454 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
3455 prstatus->pr_pid = tswap32(prstatus->pr_pid);
3456 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3457 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3458 prstatus->pr_sid = tswap32(prstatus->pr_sid);
3459 /* cpu times are not filled, so we skip them */
3460 /* regs should be in correct format already */
3461 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3464 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
3466 psinfo->pr_flag = tswapal(psinfo->pr_flag);
3467 psinfo->pr_uid = tswap16(psinfo->pr_uid);
3468 psinfo->pr_gid = tswap16(psinfo->pr_gid);
3469 psinfo->pr_pid = tswap32(psinfo->pr_pid);
3470 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
3471 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
3472 psinfo->pr_sid = tswap32(psinfo->pr_sid);
3475 static void bswap_note(struct elf_note *en)
3477 bswap32s(&en->n_namesz);
3478 bswap32s(&en->n_descsz);
3479 bswap32s(&en->n_type);
3481 #else
3482 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
3483 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
3484 static inline void bswap_note(struct elf_note *en) { }
3485 #endif /* BSWAP_NEEDED */
3488 * Minimal support for linux memory regions. These are needed
3489 * when we are finding out what memory exactly belongs to
3490 * emulated process. No locks needed here, as long as
3491 * thread that received the signal is stopped.
3494 static struct mm_struct *vma_init(void)
3496 struct mm_struct *mm;
3498 if ((mm = g_malloc(sizeof (*mm))) == NULL)
3499 return (NULL);
3501 mm->mm_count = 0;
3502 QTAILQ_INIT(&mm->mm_mmap);
3504 return (mm);
3507 static void vma_delete(struct mm_struct *mm)
3509 struct vm_area_struct *vma;
3511 while ((vma = vma_first(mm)) != NULL) {
3512 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
3513 g_free(vma);
3515 g_free(mm);
3518 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3519 target_ulong end, abi_ulong flags)
3521 struct vm_area_struct *vma;
3523 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
3524 return (-1);
3526 vma->vma_start = start;
3527 vma->vma_end = end;
3528 vma->vma_flags = flags;
3530 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
3531 mm->mm_count++;
3533 return (0);
3536 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3538 return (QTAILQ_FIRST(&mm->mm_mmap));
3541 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3543 return (QTAILQ_NEXT(vma, vma_link));
3546 static int vma_get_mapping_count(const struct mm_struct *mm)
3548 return (mm->mm_count);
3552 * Calculate file (dump) size of given memory region.
3554 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3556 /* if we cannot even read the first page, skip it */
3557 if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
3558 return (0);
3561 * Usually we don't dump executable pages as they contain
3562 * non-writable code that debugger can read directly from
3563 * target library etc. However, thread stacks are marked
3564 * also executable so we read in first page of given region
3565 * and check whether it contains elf header. If there is
3566 * no elf header, we dump it.
3568 if (vma->vma_flags & PROT_EXEC) {
3569 char page[TARGET_PAGE_SIZE];
3571 if (copy_from_user(page, vma->vma_start, sizeof (page))) {
3572 return 0;
3574 if ((page[EI_MAG0] == ELFMAG0) &&
3575 (page[EI_MAG1] == ELFMAG1) &&
3576 (page[EI_MAG2] == ELFMAG2) &&
3577 (page[EI_MAG3] == ELFMAG3)) {
3579 * Mappings are possibly from ELF binary. Don't dump
3580 * them.
3582 return (0);
3586 return (vma->vma_end - vma->vma_start);
3589 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3590 unsigned long flags)
3592 struct mm_struct *mm = (struct mm_struct *)priv;
3594 vma_add_mapping(mm, start, end, flags);
3595 return (0);
3598 static void fill_note(struct memelfnote *note, const char *name, int type,
3599 unsigned int sz, void *data)
3601 unsigned int namesz;
3603 namesz = strlen(name) + 1;
3604 note->name = name;
3605 note->namesz = namesz;
3606 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3607 note->type = type;
3608 note->datasz = sz;
3609 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3611 note->data = data;
3614 * We calculate rounded up note size here as specified by
3615 * ELF document.
3617 note->notesz = sizeof (struct elf_note) +
3618 note->namesz_rounded + note->datasz_rounded;
3621 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
3622 uint32_t flags)
3624 (void) memset(elf, 0, sizeof(*elf));
3626 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
3627 elf->e_ident[EI_CLASS] = ELF_CLASS;
3628 elf->e_ident[EI_DATA] = ELF_DATA;
3629 elf->e_ident[EI_VERSION] = EV_CURRENT;
3630 elf->e_ident[EI_OSABI] = ELF_OSABI;
3632 elf->e_type = ET_CORE;
3633 elf->e_machine = machine;
3634 elf->e_version = EV_CURRENT;
3635 elf->e_phoff = sizeof(struct elfhdr);
3636 elf->e_flags = flags;
3637 elf->e_ehsize = sizeof(struct elfhdr);
3638 elf->e_phentsize = sizeof(struct elf_phdr);
3639 elf->e_phnum = segs;
3641 bswap_ehdr(elf);
3644 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
3646 phdr->p_type = PT_NOTE;
3647 phdr->p_offset = offset;
3648 phdr->p_vaddr = 0;
3649 phdr->p_paddr = 0;
3650 phdr->p_filesz = sz;
3651 phdr->p_memsz = 0;
3652 phdr->p_flags = 0;
3653 phdr->p_align = 0;
3655 bswap_phdr(phdr, 1);
3658 static size_t note_size(const struct memelfnote *note)
3660 return (note->notesz);
3663 static void fill_prstatus(struct target_elf_prstatus *prstatus,
3664 const TaskState *ts, int signr)
3666 (void) memset(prstatus, 0, sizeof (*prstatus));
3667 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
3668 prstatus->pr_pid = ts->ts_tid;
3669 prstatus->pr_ppid = getppid();
3670 prstatus->pr_pgrp = getpgrp();
3671 prstatus->pr_sid = getsid(0);
3673 bswap_prstatus(prstatus);
3676 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
3678 char *base_filename;
3679 unsigned int i, len;
3681 (void) memset(psinfo, 0, sizeof (*psinfo));
3683 len = ts->info->env_strings - ts->info->arg_strings;
3684 if (len >= ELF_PRARGSZ)
3685 len = ELF_PRARGSZ - 1;
3686 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) {
3687 return -EFAULT;
3689 for (i = 0; i < len; i++)
3690 if (psinfo->pr_psargs[i] == 0)
3691 psinfo->pr_psargs[i] = ' ';
3692 psinfo->pr_psargs[len] = 0;
3694 psinfo->pr_pid = getpid();
3695 psinfo->pr_ppid = getppid();
3696 psinfo->pr_pgrp = getpgrp();
3697 psinfo->pr_sid = getsid(0);
3698 psinfo->pr_uid = getuid();
3699 psinfo->pr_gid = getgid();
3701 base_filename = g_path_get_basename(ts->bprm->filename);
3703 * Using strncpy here is fine: at max-length,
3704 * this field is not NUL-terminated.
3706 (void) strncpy(psinfo->pr_fname, base_filename,
3707 sizeof(psinfo->pr_fname));
3709 g_free(base_filename);
3710 bswap_psinfo(psinfo);
3711 return (0);
3714 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
3716 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
3717 elf_addr_t orig_auxv = auxv;
3718 void *ptr;
3719 int len = ts->info->auxv_len;
3722 * Auxiliary vector is stored in target process stack. It contains
3723 * {type, value} pairs that we need to dump into note. This is not
3724 * strictly necessary but we do it here for sake of completeness.
3727 /* read in whole auxv vector and copy it to memelfnote */
3728 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
3729 if (ptr != NULL) {
3730 fill_note(note, "CORE", NT_AUXV, len, ptr);
3731 unlock_user(ptr, auxv, len);
3736 * Constructs name of coredump file. We have following convention
3737 * for the name:
3738 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3740 * Returns the filename
3742 static char *core_dump_filename(const TaskState *ts)
3744 g_autoptr(GDateTime) now = g_date_time_new_now_local();
3745 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
3746 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
3748 return g_strdup_printf("qemu_%s_%s_%d.core",
3749 base_filename, nowstr, (int)getpid());
3752 static int dump_write(int fd, const void *ptr, size_t size)
3754 const char *bufp = (const char *)ptr;
3755 ssize_t bytes_written, bytes_left;
3756 struct rlimit dumpsize;
3757 off_t pos;
3759 bytes_written = 0;
3760 getrlimit(RLIMIT_CORE, &dumpsize);
3761 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
3762 if (errno == ESPIPE) { /* not a seekable stream */
3763 bytes_left = size;
3764 } else {
3765 return pos;
3767 } else {
3768 if (dumpsize.rlim_cur <= pos) {
3769 return -1;
3770 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
3771 bytes_left = size;
3772 } else {
3773 size_t limit_left=dumpsize.rlim_cur - pos;
3774 bytes_left = limit_left >= size ? size : limit_left ;
3779 * In normal conditions, single write(2) should do but
3780 * in case of socket etc. this mechanism is more portable.
3782 do {
3783 bytes_written = write(fd, bufp, bytes_left);
3784 if (bytes_written < 0) {
3785 if (errno == EINTR)
3786 continue;
3787 return (-1);
3788 } else if (bytes_written == 0) { /* eof */
3789 return (-1);
3791 bufp += bytes_written;
3792 bytes_left -= bytes_written;
3793 } while (bytes_left > 0);
3795 return (0);
3798 static int write_note(struct memelfnote *men, int fd)
3800 struct elf_note en;
3802 en.n_namesz = men->namesz;
3803 en.n_type = men->type;
3804 en.n_descsz = men->datasz;
3806 bswap_note(&en);
3808 if (dump_write(fd, &en, sizeof(en)) != 0)
3809 return (-1);
3810 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3811 return (-1);
3812 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
3813 return (-1);
3815 return (0);
3818 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
3820 CPUState *cpu = env_cpu((CPUArchState *)env);
3821 TaskState *ts = (TaskState *)cpu->opaque;
3822 struct elf_thread_status *ets;
3824 ets = g_malloc0(sizeof (*ets));
3825 ets->num_notes = 1; /* only prstatus is dumped */
3826 fill_prstatus(&ets->prstatus, ts, 0);
3827 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3828 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
3829 &ets->prstatus);
3831 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
3833 info->notes_size += note_size(&ets->notes[0]);
3836 static void init_note_info(struct elf_note_info *info)
3838 /* Initialize the elf_note_info structure so that it is at
3839 * least safe to call free_note_info() on it. Must be
3840 * called before calling fill_note_info().
3842 memset(info, 0, sizeof (*info));
3843 QTAILQ_INIT(&info->thread_list);
3846 static int fill_note_info(struct elf_note_info *info,
3847 long signr, const CPUArchState *env)
3849 #define NUMNOTES 3
3850 CPUState *cpu = env_cpu((CPUArchState *)env);
3851 TaskState *ts = (TaskState *)cpu->opaque;
3852 int i;
3854 info->notes = g_new0(struct memelfnote, NUMNOTES);
3855 if (info->notes == NULL)
3856 return (-ENOMEM);
3857 info->prstatus = g_malloc0(sizeof (*info->prstatus));
3858 if (info->prstatus == NULL)
3859 return (-ENOMEM);
3860 info->psinfo = g_malloc0(sizeof (*info->psinfo));
3861 if (info->prstatus == NULL)
3862 return (-ENOMEM);
3865 * First fill in status (and registers) of current thread
3866 * including process info & aux vector.
3868 fill_prstatus(info->prstatus, ts, signr);
3869 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3870 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
3871 sizeof (*info->prstatus), info->prstatus);
3872 fill_psinfo(info->psinfo, ts);
3873 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
3874 sizeof (*info->psinfo), info->psinfo);
3875 fill_auxv_note(&info->notes[2], ts);
3876 info->numnote = 3;
3878 info->notes_size = 0;
3879 for (i = 0; i < info->numnote; i++)
3880 info->notes_size += note_size(&info->notes[i]);
3882 /* read and fill status of all threads */
3883 cpu_list_lock();
3884 CPU_FOREACH(cpu) {
3885 if (cpu == thread_cpu) {
3886 continue;
3888 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
3890 cpu_list_unlock();
3892 return (0);
3895 static void free_note_info(struct elf_note_info *info)
3897 struct elf_thread_status *ets;
3899 while (!QTAILQ_EMPTY(&info->thread_list)) {
3900 ets = QTAILQ_FIRST(&info->thread_list);
3901 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
3902 g_free(ets);
3905 g_free(info->prstatus);
3906 g_free(info->psinfo);
3907 g_free(info->notes);
3910 static int write_note_info(struct elf_note_info *info, int fd)
3912 struct elf_thread_status *ets;
3913 int i, error = 0;
3915 /* write prstatus, psinfo and auxv for current thread */
3916 for (i = 0; i < info->numnote; i++)
3917 if ((error = write_note(&info->notes[i], fd)) != 0)
3918 return (error);
3920 /* write prstatus for each thread */
3921 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
3922 if ((error = write_note(&ets->notes[0], fd)) != 0)
3923 return (error);
3926 return (0);
3930 * Write out ELF coredump.
3932 * See documentation of ELF object file format in:
3933 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3935 * Coredump format in linux is following:
3937 * 0 +----------------------+ \
3938 * | ELF header | ET_CORE |
3939 * +----------------------+ |
3940 * | ELF program headers | |--- headers
3941 * | - NOTE section | |
3942 * | - PT_LOAD sections | |
3943 * +----------------------+ /
3944 * | NOTEs: |
3945 * | - NT_PRSTATUS |
3946 * | - NT_PRSINFO |
3947 * | - NT_AUXV |
3948 * +----------------------+ <-- aligned to target page
3949 * | Process memory dump |
3950 * : :
3951 * . .
3952 * : :
3953 * | |
3954 * +----------------------+
3956 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3957 * NT_PRSINFO -> struct elf_prpsinfo
3958 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3960 * Format follows System V format as close as possible. Current
3961 * version limitations are as follows:
3962 * - no floating point registers are dumped
3964 * Function returns 0 in case of success, negative errno otherwise.
3966 * TODO: make this work also during runtime: it should be
3967 * possible to force coredump from running process and then
3968 * continue processing. For example qemu could set up SIGUSR2
3969 * handler (provided that target process haven't registered
3970 * handler for that) that does the dump when signal is received.
3972 static int elf_core_dump(int signr, const CPUArchState *env)
3974 const CPUState *cpu = env_cpu((CPUArchState *)env);
3975 const TaskState *ts = (const TaskState *)cpu->opaque;
3976 struct vm_area_struct *vma = NULL;
3977 g_autofree char *corefile = NULL;
3978 struct elf_note_info info;
3979 struct elfhdr elf;
3980 struct elf_phdr phdr;
3981 struct rlimit dumpsize;
3982 struct mm_struct *mm = NULL;
3983 off_t offset = 0, data_offset = 0;
3984 int segs = 0;
3985 int fd = -1;
3987 init_note_info(&info);
3989 errno = 0;
3990 getrlimit(RLIMIT_CORE, &dumpsize);
3991 if (dumpsize.rlim_cur == 0)
3992 return 0;
3994 corefile = core_dump_filename(ts);
3996 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3997 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3998 return (-errno);
4001 * Walk through target process memory mappings and
4002 * set up structure containing this information. After
4003 * this point vma_xxx functions can be used.
4005 if ((mm = vma_init()) == NULL)
4006 goto out;
4008 walk_memory_regions(mm, vma_walker);
4009 segs = vma_get_mapping_count(mm);
4012 * Construct valid coredump ELF header. We also
4013 * add one more segment for notes.
4015 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
4016 if (dump_write(fd, &elf, sizeof (elf)) != 0)
4017 goto out;
4019 /* fill in the in-memory version of notes */
4020 if (fill_note_info(&info, signr, env) < 0)
4021 goto out;
4023 offset += sizeof (elf); /* elf header */
4024 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
4026 /* write out notes program header */
4027 fill_elf_note_phdr(&phdr, info.notes_size, offset);
4029 offset += info.notes_size;
4030 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
4031 goto out;
4034 * ELF specification wants data to start at page boundary so
4035 * we align it here.
4037 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
4040 * Write program headers for memory regions mapped in
4041 * the target process.
4043 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4044 (void) memset(&phdr, 0, sizeof (phdr));
4046 phdr.p_type = PT_LOAD;
4047 phdr.p_offset = offset;
4048 phdr.p_vaddr = vma->vma_start;
4049 phdr.p_paddr = 0;
4050 phdr.p_filesz = vma_dump_size(vma);
4051 offset += phdr.p_filesz;
4052 phdr.p_memsz = vma->vma_end - vma->vma_start;
4053 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
4054 if (vma->vma_flags & PROT_WRITE)
4055 phdr.p_flags |= PF_W;
4056 if (vma->vma_flags & PROT_EXEC)
4057 phdr.p_flags |= PF_X;
4058 phdr.p_align = ELF_EXEC_PAGESIZE;
4060 bswap_phdr(&phdr, 1);
4061 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
4062 goto out;
4067 * Next we write notes just after program headers. No
4068 * alignment needed here.
4070 if (write_note_info(&info, fd) < 0)
4071 goto out;
4073 /* align data to page boundary */
4074 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4075 goto out;
4078 * Finally we can dump process memory into corefile as well.
4080 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4081 abi_ulong addr;
4082 abi_ulong end;
4084 end = vma->vma_start + vma_dump_size(vma);
4086 for (addr = vma->vma_start; addr < end;
4087 addr += TARGET_PAGE_SIZE) {
4088 char page[TARGET_PAGE_SIZE];
4089 int error;
4092 * Read in page from target process memory and
4093 * write it to coredump file.
4095 error = copy_from_user(page, addr, sizeof (page));
4096 if (error != 0) {
4097 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
4098 addr);
4099 errno = -error;
4100 goto out;
4102 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4103 goto out;
4107 out:
4108 free_note_info(&info);
4109 if (mm != NULL)
4110 vma_delete(mm);
4111 (void) close(fd);
4113 if (errno != 0)
4114 return (-errno);
4115 return (0);
4117 #endif /* USE_ELF_CORE_DUMP */
4119 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4121 init_thread(regs, infop);