hw/mips/boston: Fix Lesser GPL version number
[qemu/ar7.git] / linux-user / elfload.c
blobbf8c1bd25330f9d55b491859504c87c8489e11ed
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
3 #include <sys/param.h>
5 #include <sys/resource.h>
6 #include <sys/shm.h>
8 #include "qemu.h"
9 #include "disas/disas.h"
10 #include "qemu/path.h"
11 #include "qemu/queue.h"
12 #include "qemu/guest-random.h"
13 #include "qemu/units.h"
14 #include "qemu/selfmap.h"
15 #include "qapi/error.h"
17 #ifdef _ARCH_PPC64
18 #undef ARCH_DLINFO
19 #undef ELF_PLATFORM
20 #undef ELF_HWCAP
21 #undef ELF_HWCAP2
22 #undef ELF_CLASS
23 #undef ELF_DATA
24 #undef ELF_ARCH
25 #endif
27 #define ELF_OSABI ELFOSABI_SYSV
29 /* from personality.h */
32 * Flags for bug emulation.
34 * These occupy the top three bytes.
36 enum {
37 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
38 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
39 descriptors (signal handling) */
40 MMAP_PAGE_ZERO = 0x0100000,
41 ADDR_COMPAT_LAYOUT = 0x0200000,
42 READ_IMPLIES_EXEC = 0x0400000,
43 ADDR_LIMIT_32BIT = 0x0800000,
44 SHORT_INODE = 0x1000000,
45 WHOLE_SECONDS = 0x2000000,
46 STICKY_TIMEOUTS = 0x4000000,
47 ADDR_LIMIT_3GB = 0x8000000,
51 * Personality types.
53 * These go in the low byte. Avoid using the top bit, it will
54 * conflict with error returns.
56 enum {
57 PER_LINUX = 0x0000,
58 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
59 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
60 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
61 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
62 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
63 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
64 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
65 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
66 PER_BSD = 0x0006,
67 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
68 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
69 PER_LINUX32 = 0x0008,
70 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
71 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
72 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
73 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
74 PER_RISCOS = 0x000c,
75 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
76 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
77 PER_OSF4 = 0x000f, /* OSF/1 v4 */
78 PER_HPUX = 0x0010,
79 PER_MASK = 0x00ff,
83 * Return the base personality without flags.
85 #define personality(pers) (pers & PER_MASK)
87 int info_is_fdpic(struct image_info *info)
89 return info->personality == PER_LINUX_FDPIC;
92 /* this flag is uneffective under linux too, should be deleted */
93 #ifndef MAP_DENYWRITE
94 #define MAP_DENYWRITE 0
95 #endif
97 /* should probably go in elf.h */
98 #ifndef ELIBBAD
99 #define ELIBBAD 80
100 #endif
102 #ifdef TARGET_WORDS_BIGENDIAN
103 #define ELF_DATA ELFDATA2MSB
104 #else
105 #define ELF_DATA ELFDATA2LSB
106 #endif
108 #ifdef TARGET_ABI_MIPSN32
109 typedef abi_ullong target_elf_greg_t;
110 #define tswapreg(ptr) tswap64(ptr)
111 #else
112 typedef abi_ulong target_elf_greg_t;
113 #define tswapreg(ptr) tswapal(ptr)
114 #endif
116 #ifdef USE_UID16
117 typedef abi_ushort target_uid_t;
118 typedef abi_ushort target_gid_t;
119 #else
120 typedef abi_uint target_uid_t;
121 typedef abi_uint target_gid_t;
122 #endif
123 typedef abi_int target_pid_t;
125 #ifdef TARGET_I386
127 #define ELF_PLATFORM get_elf_platform()
129 static const char *get_elf_platform(void)
131 static char elf_platform[] = "i386";
132 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
133 if (family > 6)
134 family = 6;
135 if (family >= 3)
136 elf_platform[1] = '0' + family;
137 return elf_platform;
140 #define ELF_HWCAP get_elf_hwcap()
142 static uint32_t get_elf_hwcap(void)
144 X86CPU *cpu = X86_CPU(thread_cpu);
146 return cpu->env.features[FEAT_1_EDX];
149 #ifdef TARGET_X86_64
150 #define ELF_START_MMAP 0x2aaaaab000ULL
152 #define ELF_CLASS ELFCLASS64
153 #define ELF_ARCH EM_X86_64
155 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
157 regs->rax = 0;
158 regs->rsp = infop->start_stack;
159 regs->rip = infop->entry;
162 #define ELF_NREG 27
163 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
166 * Note that ELF_NREG should be 29 as there should be place for
167 * TRAPNO and ERR "registers" as well but linux doesn't dump
168 * those.
170 * See linux kernel: arch/x86/include/asm/elf.h
172 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
174 (*regs)[0] = env->regs[15];
175 (*regs)[1] = env->regs[14];
176 (*regs)[2] = env->regs[13];
177 (*regs)[3] = env->regs[12];
178 (*regs)[4] = env->regs[R_EBP];
179 (*regs)[5] = env->regs[R_EBX];
180 (*regs)[6] = env->regs[11];
181 (*regs)[7] = env->regs[10];
182 (*regs)[8] = env->regs[9];
183 (*regs)[9] = env->regs[8];
184 (*regs)[10] = env->regs[R_EAX];
185 (*regs)[11] = env->regs[R_ECX];
186 (*regs)[12] = env->regs[R_EDX];
187 (*regs)[13] = env->regs[R_ESI];
188 (*regs)[14] = env->regs[R_EDI];
189 (*regs)[15] = env->regs[R_EAX]; /* XXX */
190 (*regs)[16] = env->eip;
191 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
192 (*regs)[18] = env->eflags;
193 (*regs)[19] = env->regs[R_ESP];
194 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
195 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
196 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
197 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
198 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
199 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
200 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
203 #else
205 #define ELF_START_MMAP 0x80000000
208 * This is used to ensure we don't load something for the wrong architecture.
210 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
213 * These are used to set parameters in the core dumps.
215 #define ELF_CLASS ELFCLASS32
216 #define ELF_ARCH EM_386
218 static inline void init_thread(struct target_pt_regs *regs,
219 struct image_info *infop)
221 regs->esp = infop->start_stack;
222 regs->eip = infop->entry;
224 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
225 starts %edx contains a pointer to a function which might be
226 registered using `atexit'. This provides a mean for the
227 dynamic linker to call DT_FINI functions for shared libraries
228 that have been loaded before the code runs.
230 A value of 0 tells we have no such handler. */
231 regs->edx = 0;
234 #define ELF_NREG 17
235 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
238 * Note that ELF_NREG should be 19 as there should be place for
239 * TRAPNO and ERR "registers" as well but linux doesn't dump
240 * those.
242 * See linux kernel: arch/x86/include/asm/elf.h
244 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
246 (*regs)[0] = env->regs[R_EBX];
247 (*regs)[1] = env->regs[R_ECX];
248 (*regs)[2] = env->regs[R_EDX];
249 (*regs)[3] = env->regs[R_ESI];
250 (*regs)[4] = env->regs[R_EDI];
251 (*regs)[5] = env->regs[R_EBP];
252 (*regs)[6] = env->regs[R_EAX];
253 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
254 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
255 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
256 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
257 (*regs)[11] = env->regs[R_EAX]; /* XXX */
258 (*regs)[12] = env->eip;
259 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
260 (*regs)[14] = env->eflags;
261 (*regs)[15] = env->regs[R_ESP];
262 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
264 #endif
266 #define USE_ELF_CORE_DUMP
267 #define ELF_EXEC_PAGESIZE 4096
269 #endif
271 #ifdef TARGET_ARM
273 #ifndef TARGET_AARCH64
274 /* 32 bit ARM definitions */
276 #define ELF_START_MMAP 0x80000000
278 #define ELF_ARCH EM_ARM
279 #define ELF_CLASS ELFCLASS32
281 static inline void init_thread(struct target_pt_regs *regs,
282 struct image_info *infop)
284 abi_long stack = infop->start_stack;
285 memset(regs, 0, sizeof(*regs));
287 regs->uregs[16] = ARM_CPU_MODE_USR;
288 if (infop->entry & 1) {
289 regs->uregs[16] |= CPSR_T;
291 regs->uregs[15] = infop->entry & 0xfffffffe;
292 regs->uregs[13] = infop->start_stack;
293 /* FIXME - what to for failure of get_user()? */
294 get_user_ual(regs->uregs[2], stack + 8); /* envp */
295 get_user_ual(regs->uregs[1], stack + 4); /* envp */
296 /* XXX: it seems that r0 is zeroed after ! */
297 regs->uregs[0] = 0;
298 /* For uClinux PIC binaries. */
299 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
300 regs->uregs[10] = infop->start_data;
302 /* Support ARM FDPIC. */
303 if (info_is_fdpic(infop)) {
304 /* As described in the ABI document, r7 points to the loadmap info
305 * prepared by the kernel. If an interpreter is needed, r8 points
306 * to the interpreter loadmap and r9 points to the interpreter
307 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
308 * r9 points to the main program PT_DYNAMIC info.
310 regs->uregs[7] = infop->loadmap_addr;
311 if (infop->interpreter_loadmap_addr) {
312 /* Executable is dynamically loaded. */
313 regs->uregs[8] = infop->interpreter_loadmap_addr;
314 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
315 } else {
316 regs->uregs[8] = 0;
317 regs->uregs[9] = infop->pt_dynamic_addr;
322 #define ELF_NREG 18
323 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
325 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
327 (*regs)[0] = tswapreg(env->regs[0]);
328 (*regs)[1] = tswapreg(env->regs[1]);
329 (*regs)[2] = tswapreg(env->regs[2]);
330 (*regs)[3] = tswapreg(env->regs[3]);
331 (*regs)[4] = tswapreg(env->regs[4]);
332 (*regs)[5] = tswapreg(env->regs[5]);
333 (*regs)[6] = tswapreg(env->regs[6]);
334 (*regs)[7] = tswapreg(env->regs[7]);
335 (*regs)[8] = tswapreg(env->regs[8]);
336 (*regs)[9] = tswapreg(env->regs[9]);
337 (*regs)[10] = tswapreg(env->regs[10]);
338 (*regs)[11] = tswapreg(env->regs[11]);
339 (*regs)[12] = tswapreg(env->regs[12]);
340 (*regs)[13] = tswapreg(env->regs[13]);
341 (*regs)[14] = tswapreg(env->regs[14]);
342 (*regs)[15] = tswapreg(env->regs[15]);
344 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
345 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
348 #define USE_ELF_CORE_DUMP
349 #define ELF_EXEC_PAGESIZE 4096
351 enum
353 ARM_HWCAP_ARM_SWP = 1 << 0,
354 ARM_HWCAP_ARM_HALF = 1 << 1,
355 ARM_HWCAP_ARM_THUMB = 1 << 2,
356 ARM_HWCAP_ARM_26BIT = 1 << 3,
357 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
358 ARM_HWCAP_ARM_FPA = 1 << 5,
359 ARM_HWCAP_ARM_VFP = 1 << 6,
360 ARM_HWCAP_ARM_EDSP = 1 << 7,
361 ARM_HWCAP_ARM_JAVA = 1 << 8,
362 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
363 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
364 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
365 ARM_HWCAP_ARM_NEON = 1 << 12,
366 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
367 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
368 ARM_HWCAP_ARM_TLS = 1 << 15,
369 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
370 ARM_HWCAP_ARM_IDIVA = 1 << 17,
371 ARM_HWCAP_ARM_IDIVT = 1 << 18,
372 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
373 ARM_HWCAP_ARM_LPAE = 1 << 20,
374 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
377 enum {
378 ARM_HWCAP2_ARM_AES = 1 << 0,
379 ARM_HWCAP2_ARM_PMULL = 1 << 1,
380 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
381 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
382 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
385 /* The commpage only exists for 32 bit kernels */
387 #define ARM_COMMPAGE (intptr_t)0xffff0f00u
389 static bool init_guest_commpage(void)
391 void *want = g2h(ARM_COMMPAGE & -qemu_host_page_size);
392 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
393 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
395 if (addr == MAP_FAILED) {
396 perror("Allocating guest commpage");
397 exit(EXIT_FAILURE);
399 if (addr != want) {
400 return false;
403 /* Set kernel helper versions; rest of page is 0. */
404 __put_user(5, (uint32_t *)g2h(0xffff0ffcu));
406 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
407 perror("Protecting guest commpage");
408 exit(EXIT_FAILURE);
410 return true;
413 #define ELF_HWCAP get_elf_hwcap()
414 #define ELF_HWCAP2 get_elf_hwcap2()
416 static uint32_t get_elf_hwcap(void)
418 ARMCPU *cpu = ARM_CPU(thread_cpu);
419 uint32_t hwcaps = 0;
421 hwcaps |= ARM_HWCAP_ARM_SWP;
422 hwcaps |= ARM_HWCAP_ARM_HALF;
423 hwcaps |= ARM_HWCAP_ARM_THUMB;
424 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
426 /* probe for the extra features */
427 #define GET_FEATURE(feat, hwcap) \
428 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
430 #define GET_FEATURE_ID(feat, hwcap) \
431 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
433 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
434 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
435 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
436 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
437 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
438 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
439 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
440 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
441 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
442 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
444 if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
445 cpu_isar_feature(aa32_fpdp_v3, cpu)) {
446 hwcaps |= ARM_HWCAP_ARM_VFPv3;
447 if (cpu_isar_feature(aa32_simd_r32, cpu)) {
448 hwcaps |= ARM_HWCAP_ARM_VFPD32;
449 } else {
450 hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
453 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
455 return hwcaps;
458 static uint32_t get_elf_hwcap2(void)
460 ARMCPU *cpu = ARM_CPU(thread_cpu);
461 uint32_t hwcaps = 0;
463 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
464 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
465 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
466 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
467 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
468 return hwcaps;
471 #undef GET_FEATURE
472 #undef GET_FEATURE_ID
474 #define ELF_PLATFORM get_elf_platform()
476 static const char *get_elf_platform(void)
478 CPUARMState *env = thread_cpu->env_ptr;
480 #ifdef TARGET_WORDS_BIGENDIAN
481 # define END "b"
482 #else
483 # define END "l"
484 #endif
486 if (arm_feature(env, ARM_FEATURE_V8)) {
487 return "v8" END;
488 } else if (arm_feature(env, ARM_FEATURE_V7)) {
489 if (arm_feature(env, ARM_FEATURE_M)) {
490 return "v7m" END;
491 } else {
492 return "v7" END;
494 } else if (arm_feature(env, ARM_FEATURE_V6)) {
495 return "v6" END;
496 } else if (arm_feature(env, ARM_FEATURE_V5)) {
497 return "v5" END;
498 } else {
499 return "v4" END;
502 #undef END
505 #else
506 /* 64 bit ARM definitions */
507 #define ELF_START_MMAP 0x80000000
509 #define ELF_ARCH EM_AARCH64
510 #define ELF_CLASS ELFCLASS64
511 #ifdef TARGET_WORDS_BIGENDIAN
512 # define ELF_PLATFORM "aarch64_be"
513 #else
514 # define ELF_PLATFORM "aarch64"
515 #endif
517 static inline void init_thread(struct target_pt_regs *regs,
518 struct image_info *infop)
520 abi_long stack = infop->start_stack;
521 memset(regs, 0, sizeof(*regs));
523 regs->pc = infop->entry & ~0x3ULL;
524 regs->sp = stack;
527 #define ELF_NREG 34
528 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
530 static void elf_core_copy_regs(target_elf_gregset_t *regs,
531 const CPUARMState *env)
533 int i;
535 for (i = 0; i < 32; i++) {
536 (*regs)[i] = tswapreg(env->xregs[i]);
538 (*regs)[32] = tswapreg(env->pc);
539 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
542 #define USE_ELF_CORE_DUMP
543 #define ELF_EXEC_PAGESIZE 4096
545 enum {
546 ARM_HWCAP_A64_FP = 1 << 0,
547 ARM_HWCAP_A64_ASIMD = 1 << 1,
548 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
549 ARM_HWCAP_A64_AES = 1 << 3,
550 ARM_HWCAP_A64_PMULL = 1 << 4,
551 ARM_HWCAP_A64_SHA1 = 1 << 5,
552 ARM_HWCAP_A64_SHA2 = 1 << 6,
553 ARM_HWCAP_A64_CRC32 = 1 << 7,
554 ARM_HWCAP_A64_ATOMICS = 1 << 8,
555 ARM_HWCAP_A64_FPHP = 1 << 9,
556 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
557 ARM_HWCAP_A64_CPUID = 1 << 11,
558 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
559 ARM_HWCAP_A64_JSCVT = 1 << 13,
560 ARM_HWCAP_A64_FCMA = 1 << 14,
561 ARM_HWCAP_A64_LRCPC = 1 << 15,
562 ARM_HWCAP_A64_DCPOP = 1 << 16,
563 ARM_HWCAP_A64_SHA3 = 1 << 17,
564 ARM_HWCAP_A64_SM3 = 1 << 18,
565 ARM_HWCAP_A64_SM4 = 1 << 19,
566 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
567 ARM_HWCAP_A64_SHA512 = 1 << 21,
568 ARM_HWCAP_A64_SVE = 1 << 22,
569 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
570 ARM_HWCAP_A64_DIT = 1 << 24,
571 ARM_HWCAP_A64_USCAT = 1 << 25,
572 ARM_HWCAP_A64_ILRCPC = 1 << 26,
573 ARM_HWCAP_A64_FLAGM = 1 << 27,
574 ARM_HWCAP_A64_SSBS = 1 << 28,
575 ARM_HWCAP_A64_SB = 1 << 29,
576 ARM_HWCAP_A64_PACA = 1 << 30,
577 ARM_HWCAP_A64_PACG = 1UL << 31,
579 ARM_HWCAP2_A64_DCPODP = 1 << 0,
580 ARM_HWCAP2_A64_SVE2 = 1 << 1,
581 ARM_HWCAP2_A64_SVEAES = 1 << 2,
582 ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
583 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
584 ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
585 ARM_HWCAP2_A64_SVESM4 = 1 << 6,
586 ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
587 ARM_HWCAP2_A64_FRINT = 1 << 8,
590 #define ELF_HWCAP get_elf_hwcap()
591 #define ELF_HWCAP2 get_elf_hwcap2()
593 #define GET_FEATURE_ID(feat, hwcap) \
594 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
596 static uint32_t get_elf_hwcap(void)
598 ARMCPU *cpu = ARM_CPU(thread_cpu);
599 uint32_t hwcaps = 0;
601 hwcaps |= ARM_HWCAP_A64_FP;
602 hwcaps |= ARM_HWCAP_A64_ASIMD;
603 hwcaps |= ARM_HWCAP_A64_CPUID;
605 /* probe for the extra features */
607 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
608 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
609 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
610 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
611 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
612 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
613 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
614 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
615 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
616 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
617 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
618 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
619 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
620 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
621 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
622 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
623 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
624 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
625 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
626 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
627 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
628 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
629 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
631 return hwcaps;
634 static uint32_t get_elf_hwcap2(void)
636 ARMCPU *cpu = ARM_CPU(thread_cpu);
637 uint32_t hwcaps = 0;
639 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
640 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
641 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
643 return hwcaps;
646 #undef GET_FEATURE_ID
648 #endif /* not TARGET_AARCH64 */
649 #endif /* TARGET_ARM */
651 #ifdef TARGET_SPARC
652 #ifdef TARGET_SPARC64
654 #define ELF_START_MMAP 0x80000000
655 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
656 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
657 #ifndef TARGET_ABI32
658 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
659 #else
660 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
661 #endif
663 #define ELF_CLASS ELFCLASS64
664 #define ELF_ARCH EM_SPARCV9
666 #define STACK_BIAS 2047
668 static inline void init_thread(struct target_pt_regs *regs,
669 struct image_info *infop)
671 #ifndef TARGET_ABI32
672 regs->tstate = 0;
673 #endif
674 regs->pc = infop->entry;
675 regs->npc = regs->pc + 4;
676 regs->y = 0;
677 #ifdef TARGET_ABI32
678 regs->u_regs[14] = infop->start_stack - 16 * 4;
679 #else
680 if (personality(infop->personality) == PER_LINUX32)
681 regs->u_regs[14] = infop->start_stack - 16 * 4;
682 else
683 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
684 #endif
687 #else
688 #define ELF_START_MMAP 0x80000000
689 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
690 | HWCAP_SPARC_MULDIV)
692 #define ELF_CLASS ELFCLASS32
693 #define ELF_ARCH EM_SPARC
695 static inline void init_thread(struct target_pt_regs *regs,
696 struct image_info *infop)
698 regs->psr = 0;
699 regs->pc = infop->entry;
700 regs->npc = regs->pc + 4;
701 regs->y = 0;
702 regs->u_regs[14] = infop->start_stack - 16 * 4;
705 #endif
706 #endif
708 #ifdef TARGET_PPC
710 #define ELF_MACHINE PPC_ELF_MACHINE
711 #define ELF_START_MMAP 0x80000000
713 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
715 #define elf_check_arch(x) ( (x) == EM_PPC64 )
717 #define ELF_CLASS ELFCLASS64
719 #else
721 #define ELF_CLASS ELFCLASS32
723 #endif
725 #define ELF_ARCH EM_PPC
727 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
728 See arch/powerpc/include/asm/cputable.h. */
729 enum {
730 QEMU_PPC_FEATURE_32 = 0x80000000,
731 QEMU_PPC_FEATURE_64 = 0x40000000,
732 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
733 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
734 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
735 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
736 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
737 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
738 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
739 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
740 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
741 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
742 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
743 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
744 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
745 QEMU_PPC_FEATURE_CELL = 0x00010000,
746 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
747 QEMU_PPC_FEATURE_SMT = 0x00004000,
748 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
749 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
750 QEMU_PPC_FEATURE_PA6T = 0x00000800,
751 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
752 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
753 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
754 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
755 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
757 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
758 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
760 /* Feature definitions in AT_HWCAP2. */
761 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
762 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
763 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
764 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
765 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
766 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
767 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
768 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
769 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
770 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
771 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
772 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
773 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
776 #define ELF_HWCAP get_elf_hwcap()
778 static uint32_t get_elf_hwcap(void)
780 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
781 uint32_t features = 0;
783 /* We don't have to be terribly complete here; the high points are
784 Altivec/FP/SPE support. Anything else is just a bonus. */
785 #define GET_FEATURE(flag, feature) \
786 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
787 #define GET_FEATURE2(flags, feature) \
788 do { \
789 if ((cpu->env.insns_flags2 & flags) == flags) { \
790 features |= feature; \
792 } while (0)
793 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
794 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
795 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
796 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
797 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
798 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
799 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
800 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
801 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
802 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
803 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
804 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
805 QEMU_PPC_FEATURE_ARCH_2_06);
806 #undef GET_FEATURE
807 #undef GET_FEATURE2
809 return features;
812 #define ELF_HWCAP2 get_elf_hwcap2()
814 static uint32_t get_elf_hwcap2(void)
816 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
817 uint32_t features = 0;
819 #define GET_FEATURE(flag, feature) \
820 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
821 #define GET_FEATURE2(flag, feature) \
822 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
824 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
825 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
826 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
827 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
828 QEMU_PPC_FEATURE2_VEC_CRYPTO);
829 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
830 QEMU_PPC_FEATURE2_DARN);
832 #undef GET_FEATURE
833 #undef GET_FEATURE2
835 return features;
839 * The requirements here are:
840 * - keep the final alignment of sp (sp & 0xf)
841 * - make sure the 32-bit value at the first 16 byte aligned position of
842 * AUXV is greater than 16 for glibc compatibility.
843 * AT_IGNOREPPC is used for that.
844 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
845 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
847 #define DLINFO_ARCH_ITEMS 5
848 #define ARCH_DLINFO \
849 do { \
850 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
851 /* \
852 * Handle glibc compatibility: these magic entries must \
853 * be at the lowest addresses in the final auxv. \
854 */ \
855 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
856 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
857 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
858 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
859 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
860 } while (0)
862 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
864 _regs->gpr[1] = infop->start_stack;
865 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
866 if (get_ppc64_abi(infop) < 2) {
867 uint64_t val;
868 get_user_u64(val, infop->entry + 8);
869 _regs->gpr[2] = val + infop->load_bias;
870 get_user_u64(val, infop->entry);
871 infop->entry = val + infop->load_bias;
872 } else {
873 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
875 #endif
876 _regs->nip = infop->entry;
879 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
880 #define ELF_NREG 48
881 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
883 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
885 int i;
886 target_ulong ccr = 0;
888 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
889 (*regs)[i] = tswapreg(env->gpr[i]);
892 (*regs)[32] = tswapreg(env->nip);
893 (*regs)[33] = tswapreg(env->msr);
894 (*regs)[35] = tswapreg(env->ctr);
895 (*regs)[36] = tswapreg(env->lr);
896 (*regs)[37] = tswapreg(env->xer);
898 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
899 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
901 (*regs)[38] = tswapreg(ccr);
904 #define USE_ELF_CORE_DUMP
905 #define ELF_EXEC_PAGESIZE 4096
907 #endif
909 #ifdef TARGET_MIPS
911 #define ELF_START_MMAP 0x80000000
913 #ifdef TARGET_MIPS64
914 #define ELF_CLASS ELFCLASS64
915 #else
916 #define ELF_CLASS ELFCLASS32
917 #endif
918 #define ELF_ARCH EM_MIPS
920 #define elf_check_arch(x) ((x) == EM_MIPS || (x) == EM_NANOMIPS)
922 #ifdef TARGET_ABI_MIPSN32
923 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
924 #else
925 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
926 #endif
928 static inline void init_thread(struct target_pt_regs *regs,
929 struct image_info *infop)
931 regs->cp0_status = 2 << CP0St_KSU;
932 regs->cp0_epc = infop->entry;
933 regs->regs[29] = infop->start_stack;
936 /* See linux kernel: arch/mips/include/asm/elf.h. */
937 #define ELF_NREG 45
938 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
940 /* See linux kernel: arch/mips/include/asm/reg.h. */
941 enum {
942 #ifdef TARGET_MIPS64
943 TARGET_EF_R0 = 0,
944 #else
945 TARGET_EF_R0 = 6,
946 #endif
947 TARGET_EF_R26 = TARGET_EF_R0 + 26,
948 TARGET_EF_R27 = TARGET_EF_R0 + 27,
949 TARGET_EF_LO = TARGET_EF_R0 + 32,
950 TARGET_EF_HI = TARGET_EF_R0 + 33,
951 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
952 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
953 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
954 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
957 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
958 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
960 int i;
962 for (i = 0; i < TARGET_EF_R0; i++) {
963 (*regs)[i] = 0;
965 (*regs)[TARGET_EF_R0] = 0;
967 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
968 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
971 (*regs)[TARGET_EF_R26] = 0;
972 (*regs)[TARGET_EF_R27] = 0;
973 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
974 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
975 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
976 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
977 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
978 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
981 #define USE_ELF_CORE_DUMP
982 #define ELF_EXEC_PAGESIZE 4096
984 /* See arch/mips/include/uapi/asm/hwcap.h. */
985 enum {
986 HWCAP_MIPS_R6 = (1 << 0),
987 HWCAP_MIPS_MSA = (1 << 1),
990 #define ELF_HWCAP get_elf_hwcap()
992 static uint32_t get_elf_hwcap(void)
994 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
995 uint32_t hwcaps = 0;
997 #define GET_FEATURE(flag, hwcap) \
998 do { if (cpu->env.insn_flags & (flag)) { hwcaps |= hwcap; } } while (0)
1000 GET_FEATURE(ISA_MIPS32R6 | ISA_MIPS64R6, HWCAP_MIPS_R6);
1001 GET_FEATURE(ASE_MSA, HWCAP_MIPS_MSA);
1003 #undef GET_FEATURE
1005 return hwcaps;
1008 #endif /* TARGET_MIPS */
1010 #ifdef TARGET_MICROBLAZE
1012 #define ELF_START_MMAP 0x80000000
1014 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1016 #define ELF_CLASS ELFCLASS32
1017 #define ELF_ARCH EM_MICROBLAZE
1019 static inline void init_thread(struct target_pt_regs *regs,
1020 struct image_info *infop)
1022 regs->pc = infop->entry;
1023 regs->r1 = infop->start_stack;
1027 #define ELF_EXEC_PAGESIZE 4096
1029 #define USE_ELF_CORE_DUMP
1030 #define ELF_NREG 38
1031 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1033 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1034 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1036 int i, pos = 0;
1038 for (i = 0; i < 32; i++) {
1039 (*regs)[pos++] = tswapreg(env->regs[i]);
1042 (*regs)[pos++] = tswapreg(env->pc);
1043 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
1044 (*regs)[pos++] = 0;
1045 (*regs)[pos++] = tswapreg(env->ear);
1046 (*regs)[pos++] = 0;
1047 (*regs)[pos++] = tswapreg(env->esr);
1050 #endif /* TARGET_MICROBLAZE */
1052 #ifdef TARGET_NIOS2
1054 #define ELF_START_MMAP 0x80000000
1056 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1058 #define ELF_CLASS ELFCLASS32
1059 #define ELF_ARCH EM_ALTERA_NIOS2
1061 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1063 regs->ea = infop->entry;
1064 regs->sp = infop->start_stack;
1065 regs->estatus = 0x3;
1068 #define ELF_EXEC_PAGESIZE 4096
1070 #define USE_ELF_CORE_DUMP
1071 #define ELF_NREG 49
1072 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1074 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1075 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1076 const CPUNios2State *env)
1078 int i;
1080 (*regs)[0] = -1;
1081 for (i = 1; i < 8; i++) /* r0-r7 */
1082 (*regs)[i] = tswapreg(env->regs[i + 7]);
1084 for (i = 8; i < 16; i++) /* r8-r15 */
1085 (*regs)[i] = tswapreg(env->regs[i - 8]);
1087 for (i = 16; i < 24; i++) /* r16-r23 */
1088 (*regs)[i] = tswapreg(env->regs[i + 7]);
1089 (*regs)[24] = -1; /* R_ET */
1090 (*regs)[25] = -1; /* R_BT */
1091 (*regs)[26] = tswapreg(env->regs[R_GP]);
1092 (*regs)[27] = tswapreg(env->regs[R_SP]);
1093 (*regs)[28] = tswapreg(env->regs[R_FP]);
1094 (*regs)[29] = tswapreg(env->regs[R_EA]);
1095 (*regs)[30] = -1; /* R_SSTATUS */
1096 (*regs)[31] = tswapreg(env->regs[R_RA]);
1098 (*regs)[32] = tswapreg(env->regs[R_PC]);
1100 (*regs)[33] = -1; /* R_STATUS */
1101 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1103 for (i = 35; i < 49; i++) /* ... */
1104 (*regs)[i] = -1;
1107 #endif /* TARGET_NIOS2 */
1109 #ifdef TARGET_OPENRISC
1111 #define ELF_START_MMAP 0x08000000
1113 #define ELF_ARCH EM_OPENRISC
1114 #define ELF_CLASS ELFCLASS32
1115 #define ELF_DATA ELFDATA2MSB
1117 static inline void init_thread(struct target_pt_regs *regs,
1118 struct image_info *infop)
1120 regs->pc = infop->entry;
1121 regs->gpr[1] = infop->start_stack;
1124 #define USE_ELF_CORE_DUMP
1125 #define ELF_EXEC_PAGESIZE 8192
1127 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1128 #define ELF_NREG 34 /* gprs and pc, sr */
1129 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1131 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1132 const CPUOpenRISCState *env)
1134 int i;
1136 for (i = 0; i < 32; i++) {
1137 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1139 (*regs)[32] = tswapreg(env->pc);
1140 (*regs)[33] = tswapreg(cpu_get_sr(env));
1142 #define ELF_HWCAP 0
1143 #define ELF_PLATFORM NULL
1145 #endif /* TARGET_OPENRISC */
1147 #ifdef TARGET_SH4
1149 #define ELF_START_MMAP 0x80000000
1151 #define ELF_CLASS ELFCLASS32
1152 #define ELF_ARCH EM_SH
1154 static inline void init_thread(struct target_pt_regs *regs,
1155 struct image_info *infop)
1157 /* Check other registers XXXXX */
1158 regs->pc = infop->entry;
1159 regs->regs[15] = infop->start_stack;
1162 /* See linux kernel: arch/sh/include/asm/elf.h. */
1163 #define ELF_NREG 23
1164 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1166 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1167 enum {
1168 TARGET_REG_PC = 16,
1169 TARGET_REG_PR = 17,
1170 TARGET_REG_SR = 18,
1171 TARGET_REG_GBR = 19,
1172 TARGET_REG_MACH = 20,
1173 TARGET_REG_MACL = 21,
1174 TARGET_REG_SYSCALL = 22
1177 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1178 const CPUSH4State *env)
1180 int i;
1182 for (i = 0; i < 16; i++) {
1183 (*regs)[i] = tswapreg(env->gregs[i]);
1186 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1187 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1188 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1189 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1190 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1191 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1192 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1195 #define USE_ELF_CORE_DUMP
1196 #define ELF_EXEC_PAGESIZE 4096
1198 enum {
1199 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1200 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1201 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1202 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1203 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1204 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1205 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1206 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1207 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1208 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1211 #define ELF_HWCAP get_elf_hwcap()
1213 static uint32_t get_elf_hwcap(void)
1215 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1216 uint32_t hwcap = 0;
1218 hwcap |= SH_CPU_HAS_FPU;
1220 if (cpu->env.features & SH_FEATURE_SH4A) {
1221 hwcap |= SH_CPU_HAS_LLSC;
1224 return hwcap;
1227 #endif
1229 #ifdef TARGET_CRIS
1231 #define ELF_START_MMAP 0x80000000
1233 #define ELF_CLASS ELFCLASS32
1234 #define ELF_ARCH EM_CRIS
1236 static inline void init_thread(struct target_pt_regs *regs,
1237 struct image_info *infop)
1239 regs->erp = infop->entry;
1242 #define ELF_EXEC_PAGESIZE 8192
1244 #endif
1246 #ifdef TARGET_M68K
1248 #define ELF_START_MMAP 0x80000000
1250 #define ELF_CLASS ELFCLASS32
1251 #define ELF_ARCH EM_68K
1253 /* ??? Does this need to do anything?
1254 #define ELF_PLAT_INIT(_r) */
1256 static inline void init_thread(struct target_pt_regs *regs,
1257 struct image_info *infop)
1259 regs->usp = infop->start_stack;
1260 regs->sr = 0;
1261 regs->pc = infop->entry;
1264 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1265 #define ELF_NREG 20
1266 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1268 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1270 (*regs)[0] = tswapreg(env->dregs[1]);
1271 (*regs)[1] = tswapreg(env->dregs[2]);
1272 (*regs)[2] = tswapreg(env->dregs[3]);
1273 (*regs)[3] = tswapreg(env->dregs[4]);
1274 (*regs)[4] = tswapreg(env->dregs[5]);
1275 (*regs)[5] = tswapreg(env->dregs[6]);
1276 (*regs)[6] = tswapreg(env->dregs[7]);
1277 (*regs)[7] = tswapreg(env->aregs[0]);
1278 (*regs)[8] = tswapreg(env->aregs[1]);
1279 (*regs)[9] = tswapreg(env->aregs[2]);
1280 (*regs)[10] = tswapreg(env->aregs[3]);
1281 (*regs)[11] = tswapreg(env->aregs[4]);
1282 (*regs)[12] = tswapreg(env->aregs[5]);
1283 (*regs)[13] = tswapreg(env->aregs[6]);
1284 (*regs)[14] = tswapreg(env->dregs[0]);
1285 (*regs)[15] = tswapreg(env->aregs[7]);
1286 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1287 (*regs)[17] = tswapreg(env->sr);
1288 (*regs)[18] = tswapreg(env->pc);
1289 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1292 #define USE_ELF_CORE_DUMP
1293 #define ELF_EXEC_PAGESIZE 8192
1295 #endif
1297 #ifdef TARGET_ALPHA
1299 #define ELF_START_MMAP (0x30000000000ULL)
1301 #define ELF_CLASS ELFCLASS64
1302 #define ELF_ARCH EM_ALPHA
1304 static inline void init_thread(struct target_pt_regs *regs,
1305 struct image_info *infop)
1307 regs->pc = infop->entry;
1308 regs->ps = 8;
1309 regs->usp = infop->start_stack;
1312 #define ELF_EXEC_PAGESIZE 8192
1314 #endif /* TARGET_ALPHA */
1316 #ifdef TARGET_S390X
1318 #define ELF_START_MMAP (0x20000000000ULL)
1320 #define ELF_CLASS ELFCLASS64
1321 #define ELF_DATA ELFDATA2MSB
1322 #define ELF_ARCH EM_S390
1324 #include "elf.h"
1326 #define ELF_HWCAP get_elf_hwcap()
1328 #define GET_FEATURE(_feat, _hwcap) \
1329 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1331 static uint32_t get_elf_hwcap(void)
1334 * Let's assume we always have esan3 and zarch.
1335 * 31-bit processes can use 64-bit registers (high gprs).
1337 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1339 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1340 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1341 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1342 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1343 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1344 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1345 hwcap |= HWCAP_S390_ETF3EH;
1347 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1349 return hwcap;
1352 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1354 regs->psw.addr = infop->entry;
1355 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1356 regs->gprs[15] = infop->start_stack;
1359 #endif /* TARGET_S390X */
1361 #ifdef TARGET_TILEGX
1363 /* 42 bits real used address, a half for user mode */
1364 #define ELF_START_MMAP (0x00000020000000000ULL)
1366 #define elf_check_arch(x) ((x) == EM_TILEGX)
1368 #define ELF_CLASS ELFCLASS64
1369 #define ELF_DATA ELFDATA2LSB
1370 #define ELF_ARCH EM_TILEGX
1372 static inline void init_thread(struct target_pt_regs *regs,
1373 struct image_info *infop)
1375 regs->pc = infop->entry;
1376 regs->sp = infop->start_stack;
1380 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1382 #endif /* TARGET_TILEGX */
1384 #ifdef TARGET_RISCV
1386 #define ELF_START_MMAP 0x80000000
1387 #define ELF_ARCH EM_RISCV
1389 #ifdef TARGET_RISCV32
1390 #define ELF_CLASS ELFCLASS32
1391 #else
1392 #define ELF_CLASS ELFCLASS64
1393 #endif
1395 static inline void init_thread(struct target_pt_regs *regs,
1396 struct image_info *infop)
1398 regs->sepc = infop->entry;
1399 regs->sp = infop->start_stack;
1402 #define ELF_EXEC_PAGESIZE 4096
1404 #endif /* TARGET_RISCV */
1406 #ifdef TARGET_HPPA
1408 #define ELF_START_MMAP 0x80000000
1409 #define ELF_CLASS ELFCLASS32
1410 #define ELF_ARCH EM_PARISC
1411 #define ELF_PLATFORM "PARISC"
1412 #define STACK_GROWS_DOWN 0
1413 #define STACK_ALIGNMENT 64
1415 static inline void init_thread(struct target_pt_regs *regs,
1416 struct image_info *infop)
1418 regs->iaoq[0] = infop->entry;
1419 regs->iaoq[1] = infop->entry + 4;
1420 regs->gr[23] = 0;
1421 regs->gr[24] = infop->arg_start;
1422 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1423 /* The top-of-stack contains a linkage buffer. */
1424 regs->gr[30] = infop->start_stack + 64;
1425 regs->gr[31] = infop->entry;
1428 #endif /* TARGET_HPPA */
1430 #ifdef TARGET_XTENSA
1432 #define ELF_START_MMAP 0x20000000
1434 #define ELF_CLASS ELFCLASS32
1435 #define ELF_ARCH EM_XTENSA
1437 static inline void init_thread(struct target_pt_regs *regs,
1438 struct image_info *infop)
1440 regs->windowbase = 0;
1441 regs->windowstart = 1;
1442 regs->areg[1] = infop->start_stack;
1443 regs->pc = infop->entry;
1446 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1447 #define ELF_NREG 128
1448 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1450 enum {
1451 TARGET_REG_PC,
1452 TARGET_REG_PS,
1453 TARGET_REG_LBEG,
1454 TARGET_REG_LEND,
1455 TARGET_REG_LCOUNT,
1456 TARGET_REG_SAR,
1457 TARGET_REG_WINDOWSTART,
1458 TARGET_REG_WINDOWBASE,
1459 TARGET_REG_THREADPTR,
1460 TARGET_REG_AR0 = 64,
1463 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1464 const CPUXtensaState *env)
1466 unsigned i;
1468 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1469 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1470 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1471 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1472 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1473 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1474 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1475 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1476 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1477 xtensa_sync_phys_from_window((CPUXtensaState *)env);
1478 for (i = 0; i < env->config->nareg; ++i) {
1479 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1483 #define USE_ELF_CORE_DUMP
1484 #define ELF_EXEC_PAGESIZE 4096
1486 #endif /* TARGET_XTENSA */
1488 #ifndef ELF_PLATFORM
1489 #define ELF_PLATFORM (NULL)
1490 #endif
1492 #ifndef ELF_MACHINE
1493 #define ELF_MACHINE ELF_ARCH
1494 #endif
1496 #ifndef elf_check_arch
1497 #define elf_check_arch(x) ((x) == ELF_ARCH)
1498 #endif
1500 #ifndef elf_check_abi
1501 #define elf_check_abi(x) (1)
1502 #endif
1504 #ifndef ELF_HWCAP
1505 #define ELF_HWCAP 0
1506 #endif
1508 #ifndef STACK_GROWS_DOWN
1509 #define STACK_GROWS_DOWN 1
1510 #endif
1512 #ifndef STACK_ALIGNMENT
1513 #define STACK_ALIGNMENT 16
1514 #endif
1516 #ifdef TARGET_ABI32
1517 #undef ELF_CLASS
1518 #define ELF_CLASS ELFCLASS32
1519 #undef bswaptls
1520 #define bswaptls(ptr) bswap32s(ptr)
1521 #endif
1523 #include "elf.h"
1525 /* We must delay the following stanzas until after "elf.h". */
1526 #if defined(TARGET_AARCH64)
1528 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1529 const uint32_t *data,
1530 struct image_info *info,
1531 Error **errp)
1533 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
1534 if (pr_datasz != sizeof(uint32_t)) {
1535 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
1536 return false;
1538 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
1539 info->note_flags = *data;
1541 return true;
1543 #define ARCH_USE_GNU_PROPERTY 1
1545 #else
1547 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1548 const uint32_t *data,
1549 struct image_info *info,
1550 Error **errp)
1552 g_assert_not_reached();
1554 #define ARCH_USE_GNU_PROPERTY 0
1556 #endif
1558 struct exec
1560 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1561 unsigned int a_text; /* length of text, in bytes */
1562 unsigned int a_data; /* length of data, in bytes */
1563 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1564 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1565 unsigned int a_entry; /* start address */
1566 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1567 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1571 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1572 #define OMAGIC 0407
1573 #define NMAGIC 0410
1574 #define ZMAGIC 0413
1575 #define QMAGIC 0314
1577 /* Necessary parameters */
1578 #define TARGET_ELF_EXEC_PAGESIZE \
1579 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1580 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1581 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1582 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1583 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1584 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1586 #define DLINFO_ITEMS 16
1588 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1590 memcpy(to, from, n);
1593 #ifdef BSWAP_NEEDED
1594 static void bswap_ehdr(struct elfhdr *ehdr)
1596 bswap16s(&ehdr->e_type); /* Object file type */
1597 bswap16s(&ehdr->e_machine); /* Architecture */
1598 bswap32s(&ehdr->e_version); /* Object file version */
1599 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1600 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1601 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1602 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1603 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1604 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1605 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1606 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1607 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1608 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1611 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1613 int i;
1614 for (i = 0; i < phnum; ++i, ++phdr) {
1615 bswap32s(&phdr->p_type); /* Segment type */
1616 bswap32s(&phdr->p_flags); /* Segment flags */
1617 bswaptls(&phdr->p_offset); /* Segment file offset */
1618 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1619 bswaptls(&phdr->p_paddr); /* Segment physical address */
1620 bswaptls(&phdr->p_filesz); /* Segment size in file */
1621 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1622 bswaptls(&phdr->p_align); /* Segment alignment */
1626 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1628 int i;
1629 for (i = 0; i < shnum; ++i, ++shdr) {
1630 bswap32s(&shdr->sh_name);
1631 bswap32s(&shdr->sh_type);
1632 bswaptls(&shdr->sh_flags);
1633 bswaptls(&shdr->sh_addr);
1634 bswaptls(&shdr->sh_offset);
1635 bswaptls(&shdr->sh_size);
1636 bswap32s(&shdr->sh_link);
1637 bswap32s(&shdr->sh_info);
1638 bswaptls(&shdr->sh_addralign);
1639 bswaptls(&shdr->sh_entsize);
1643 static void bswap_sym(struct elf_sym *sym)
1645 bswap32s(&sym->st_name);
1646 bswaptls(&sym->st_value);
1647 bswaptls(&sym->st_size);
1648 bswap16s(&sym->st_shndx);
1651 #ifdef TARGET_MIPS
1652 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
1654 bswap16s(&abiflags->version);
1655 bswap32s(&abiflags->ases);
1656 bswap32s(&abiflags->isa_ext);
1657 bswap32s(&abiflags->flags1);
1658 bswap32s(&abiflags->flags2);
1660 #endif
1661 #else
1662 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1663 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1664 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1665 static inline void bswap_sym(struct elf_sym *sym) { }
1666 #ifdef TARGET_MIPS
1667 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
1668 #endif
1669 #endif
1671 #ifdef USE_ELF_CORE_DUMP
1672 static int elf_core_dump(int, const CPUArchState *);
1673 #endif /* USE_ELF_CORE_DUMP */
1674 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1676 /* Verify the portions of EHDR within E_IDENT for the target.
1677 This can be performed before bswapping the entire header. */
1678 static bool elf_check_ident(struct elfhdr *ehdr)
1680 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1681 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1682 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1683 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1684 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1685 && ehdr->e_ident[EI_DATA] == ELF_DATA
1686 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1689 /* Verify the portions of EHDR outside of E_IDENT for the target.
1690 This has to wait until after bswapping the header. */
1691 static bool elf_check_ehdr(struct elfhdr *ehdr)
1693 return (elf_check_arch(ehdr->e_machine)
1694 && elf_check_abi(ehdr->e_flags)
1695 && ehdr->e_ehsize == sizeof(struct elfhdr)
1696 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1697 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1701 * 'copy_elf_strings()' copies argument/envelope strings from user
1702 * memory to free pages in kernel mem. These are in a format ready
1703 * to be put directly into the top of new user memory.
1706 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1707 abi_ulong p, abi_ulong stack_limit)
1709 char *tmp;
1710 int len, i;
1711 abi_ulong top = p;
1713 if (!p) {
1714 return 0; /* bullet-proofing */
1717 if (STACK_GROWS_DOWN) {
1718 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1719 for (i = argc - 1; i >= 0; --i) {
1720 tmp = argv[i];
1721 if (!tmp) {
1722 fprintf(stderr, "VFS: argc is wrong");
1723 exit(-1);
1725 len = strlen(tmp) + 1;
1726 tmp += len;
1728 if (len > (p - stack_limit)) {
1729 return 0;
1731 while (len) {
1732 int bytes_to_copy = (len > offset) ? offset : len;
1733 tmp -= bytes_to_copy;
1734 p -= bytes_to_copy;
1735 offset -= bytes_to_copy;
1736 len -= bytes_to_copy;
1738 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1740 if (offset == 0) {
1741 memcpy_to_target(p, scratch, top - p);
1742 top = p;
1743 offset = TARGET_PAGE_SIZE;
1747 if (p != top) {
1748 memcpy_to_target(p, scratch + offset, top - p);
1750 } else {
1751 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1752 for (i = 0; i < argc; ++i) {
1753 tmp = argv[i];
1754 if (!tmp) {
1755 fprintf(stderr, "VFS: argc is wrong");
1756 exit(-1);
1758 len = strlen(tmp) + 1;
1759 if (len > (stack_limit - p)) {
1760 return 0;
1762 while (len) {
1763 int bytes_to_copy = (len > remaining) ? remaining : len;
1765 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1767 tmp += bytes_to_copy;
1768 remaining -= bytes_to_copy;
1769 p += bytes_to_copy;
1770 len -= bytes_to_copy;
1772 if (remaining == 0) {
1773 memcpy_to_target(top, scratch, p - top);
1774 top = p;
1775 remaining = TARGET_PAGE_SIZE;
1779 if (p != top) {
1780 memcpy_to_target(top, scratch, p - top);
1784 return p;
1787 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1788 * argument/environment space. Newer kernels (>2.6.33) allow more,
1789 * dependent on stack size, but guarantee at least 32 pages for
1790 * backwards compatibility.
1792 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1794 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1795 struct image_info *info)
1797 abi_ulong size, error, guard;
1799 size = guest_stack_size;
1800 if (size < STACK_LOWER_LIMIT) {
1801 size = STACK_LOWER_LIMIT;
1803 guard = TARGET_PAGE_SIZE;
1804 if (guard < qemu_real_host_page_size) {
1805 guard = qemu_real_host_page_size;
1808 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1809 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1810 if (error == -1) {
1811 perror("mmap stack");
1812 exit(-1);
1815 /* We reserve one extra page at the top of the stack as guard. */
1816 if (STACK_GROWS_DOWN) {
1817 target_mprotect(error, guard, PROT_NONE);
1818 info->stack_limit = error + guard;
1819 return info->stack_limit + size - sizeof(void *);
1820 } else {
1821 target_mprotect(error + size, guard, PROT_NONE);
1822 info->stack_limit = error + size;
1823 return error;
1827 /* Map and zero the bss. We need to explicitly zero any fractional pages
1828 after the data section (i.e. bss). */
1829 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1831 uintptr_t host_start, host_map_start, host_end;
1833 last_bss = TARGET_PAGE_ALIGN(last_bss);
1835 /* ??? There is confusion between qemu_real_host_page_size and
1836 qemu_host_page_size here and elsewhere in target_mmap, which
1837 may lead to the end of the data section mapping from the file
1838 not being mapped. At least there was an explicit test and
1839 comment for that here, suggesting that "the file size must
1840 be known". The comment probably pre-dates the introduction
1841 of the fstat system call in target_mmap which does in fact
1842 find out the size. What isn't clear is if the workaround
1843 here is still actually needed. For now, continue with it,
1844 but merge it with the "normal" mmap that would allocate the bss. */
1846 host_start = (uintptr_t) g2h(elf_bss);
1847 host_end = (uintptr_t) g2h(last_bss);
1848 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1850 if (host_map_start < host_end) {
1851 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1852 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1853 if (p == MAP_FAILED) {
1854 perror("cannot mmap brk");
1855 exit(-1);
1859 /* Ensure that the bss page(s) are valid */
1860 if ((page_get_flags(last_bss-1) & prot) != prot) {
1861 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1864 if (host_start < host_map_start) {
1865 memset((void *)host_start, 0, host_map_start - host_start);
1869 #ifdef TARGET_ARM
1870 static int elf_is_fdpic(struct elfhdr *exec)
1872 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
1874 #else
1875 /* Default implementation, always false. */
1876 static int elf_is_fdpic(struct elfhdr *exec)
1878 return 0;
1880 #endif
1882 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1884 uint16_t n;
1885 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1887 /* elf32_fdpic_loadseg */
1888 n = info->nsegs;
1889 while (n--) {
1890 sp -= 12;
1891 put_user_u32(loadsegs[n].addr, sp+0);
1892 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1893 put_user_u32(loadsegs[n].p_memsz, sp+8);
1896 /* elf32_fdpic_loadmap */
1897 sp -= 4;
1898 put_user_u16(0, sp+0); /* version */
1899 put_user_u16(info->nsegs, sp+2); /* nsegs */
1901 info->personality = PER_LINUX_FDPIC;
1902 info->loadmap_addr = sp;
1904 return sp;
1907 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1908 struct elfhdr *exec,
1909 struct image_info *info,
1910 struct image_info *interp_info)
1912 abi_ulong sp;
1913 abi_ulong u_argc, u_argv, u_envp, u_auxv;
1914 int size;
1915 int i;
1916 abi_ulong u_rand_bytes;
1917 uint8_t k_rand_bytes[16];
1918 abi_ulong u_platform;
1919 const char *k_platform;
1920 const int n = sizeof(elf_addr_t);
1922 sp = p;
1924 /* Needs to be before we load the env/argc/... */
1925 if (elf_is_fdpic(exec)) {
1926 /* Need 4 byte alignment for these structs */
1927 sp &= ~3;
1928 sp = loader_build_fdpic_loadmap(info, sp);
1929 info->other_info = interp_info;
1930 if (interp_info) {
1931 interp_info->other_info = info;
1932 sp = loader_build_fdpic_loadmap(interp_info, sp);
1933 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
1934 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
1935 } else {
1936 info->interpreter_loadmap_addr = 0;
1937 info->interpreter_pt_dynamic_addr = 0;
1941 u_platform = 0;
1942 k_platform = ELF_PLATFORM;
1943 if (k_platform) {
1944 size_t len = strlen(k_platform) + 1;
1945 if (STACK_GROWS_DOWN) {
1946 sp -= (len + n - 1) & ~(n - 1);
1947 u_platform = sp;
1948 /* FIXME - check return value of memcpy_to_target() for failure */
1949 memcpy_to_target(sp, k_platform, len);
1950 } else {
1951 memcpy_to_target(sp, k_platform, len);
1952 u_platform = sp;
1953 sp += len + 1;
1957 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1958 * the argv and envp pointers.
1960 if (STACK_GROWS_DOWN) {
1961 sp = QEMU_ALIGN_DOWN(sp, 16);
1962 } else {
1963 sp = QEMU_ALIGN_UP(sp, 16);
1967 * Generate 16 random bytes for userspace PRNG seeding.
1969 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
1970 if (STACK_GROWS_DOWN) {
1971 sp -= 16;
1972 u_rand_bytes = sp;
1973 /* FIXME - check return value of memcpy_to_target() for failure */
1974 memcpy_to_target(sp, k_rand_bytes, 16);
1975 } else {
1976 memcpy_to_target(sp, k_rand_bytes, 16);
1977 u_rand_bytes = sp;
1978 sp += 16;
1981 size = (DLINFO_ITEMS + 1) * 2;
1982 if (k_platform)
1983 size += 2;
1984 #ifdef DLINFO_ARCH_ITEMS
1985 size += DLINFO_ARCH_ITEMS * 2;
1986 #endif
1987 #ifdef ELF_HWCAP2
1988 size += 2;
1989 #endif
1990 info->auxv_len = size * n;
1992 size += envc + argc + 2;
1993 size += 1; /* argc itself */
1994 size *= n;
1996 /* Allocate space and finalize stack alignment for entry now. */
1997 if (STACK_GROWS_DOWN) {
1998 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
1999 sp = u_argc;
2000 } else {
2001 u_argc = sp;
2002 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2005 u_argv = u_argc + n;
2006 u_envp = u_argv + (argc + 1) * n;
2007 u_auxv = u_envp + (envc + 1) * n;
2008 info->saved_auxv = u_auxv;
2009 info->arg_start = u_argv;
2010 info->arg_end = u_argv + argc * n;
2012 /* This is correct because Linux defines
2013 * elf_addr_t as Elf32_Off / Elf64_Off
2015 #define NEW_AUX_ENT(id, val) do { \
2016 put_user_ual(id, u_auxv); u_auxv += n; \
2017 put_user_ual(val, u_auxv); u_auxv += n; \
2018 } while(0)
2020 #ifdef ARCH_DLINFO
2022 * ARCH_DLINFO must come first so platform specific code can enforce
2023 * special alignment requirements on the AUXV if necessary (eg. PPC).
2025 ARCH_DLINFO;
2026 #endif
2027 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2028 * on info->auxv_len will trigger.
2030 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2031 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2032 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2033 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2034 /* Target doesn't support host page size alignment */
2035 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2036 } else {
2037 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2038 qemu_host_page_size)));
2040 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2041 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2042 NEW_AUX_ENT(AT_ENTRY, info->entry);
2043 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2044 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2045 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2046 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2047 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2048 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2049 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2050 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2051 NEW_AUX_ENT(AT_EXECFN, info->file_string);
2053 #ifdef ELF_HWCAP2
2054 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2055 #endif
2057 if (u_platform) {
2058 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2060 NEW_AUX_ENT (AT_NULL, 0);
2061 #undef NEW_AUX_ENT
2063 /* Check that our initial calculation of the auxv length matches how much
2064 * we actually put into it.
2066 assert(info->auxv_len == u_auxv - info->saved_auxv);
2068 put_user_ual(argc, u_argc);
2070 p = info->arg_strings;
2071 for (i = 0; i < argc; ++i) {
2072 put_user_ual(p, u_argv);
2073 u_argv += n;
2074 p += target_strlen(p) + 1;
2076 put_user_ual(0, u_argv);
2078 p = info->env_strings;
2079 for (i = 0; i < envc; ++i) {
2080 put_user_ual(p, u_envp);
2081 u_envp += n;
2082 p += target_strlen(p) + 1;
2084 put_user_ual(0, u_envp);
2086 return sp;
2089 #ifndef ARM_COMMPAGE
2090 #define ARM_COMMPAGE 0
2091 #define init_guest_commpage() true
2092 #endif
2094 static void pgb_fail_in_use(const char *image_name)
2096 error_report("%s: requires virtual address space that is in use "
2097 "(omit the -B option or choose a different value)",
2098 image_name);
2099 exit(EXIT_FAILURE);
2102 static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
2103 abi_ulong guest_hiaddr, long align)
2105 const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2106 void *addr, *test;
2108 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2109 fprintf(stderr, "Requested guest base 0x%lx does not satisfy "
2110 "host minimum alignment (0x%lx)\n",
2111 guest_base, align);
2112 exit(EXIT_FAILURE);
2115 /* Sanity check the guest binary. */
2116 if (reserved_va) {
2117 if (guest_hiaddr > reserved_va) {
2118 error_report("%s: requires more than reserved virtual "
2119 "address space (0x%" PRIx64 " > 0x%lx)",
2120 image_name, (uint64_t)guest_hiaddr, reserved_va);
2121 exit(EXIT_FAILURE);
2123 } else {
2124 #if HOST_LONG_BITS < TARGET_ABI_BITS
2125 if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) {
2126 error_report("%s: requires more virtual address space "
2127 "than the host can provide (0x%" PRIx64 ")",
2128 image_name, (uint64_t)guest_hiaddr - guest_base);
2129 exit(EXIT_FAILURE);
2131 #endif
2135 * Expand the allocation to the entire reserved_va.
2136 * Exclude the mmap_min_addr hole.
2138 if (reserved_va) {
2139 guest_loaddr = (guest_base >= mmap_min_addr ? 0
2140 : mmap_min_addr - guest_base);
2141 guest_hiaddr = reserved_va;
2144 /* Reserve the address space for the binary, or reserved_va. */
2145 test = g2h(guest_loaddr);
2146 addr = mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0);
2147 if (test != addr) {
2148 pgb_fail_in_use(image_name);
2153 * pgd_find_hole_fallback: potential mmap address
2154 * @guest_size: size of available space
2155 * @brk: location of break
2156 * @align: memory alignment
2158 * This is a fallback method for finding a hole in the host address
2159 * space if we don't have the benefit of being able to access
2160 * /proc/self/map. It can potentially take a very long time as we can
2161 * only dumbly iterate up the host address space seeing if the
2162 * allocation would work.
2164 static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
2165 long align, uintptr_t offset)
2167 uintptr_t base;
2169 /* Start (aligned) at the bottom and work our way up */
2170 base = ROUND_UP(mmap_min_addr, align);
2172 while (true) {
2173 uintptr_t align_start, end;
2174 align_start = ROUND_UP(base, align);
2175 end = align_start + guest_size + offset;
2177 /* if brk is anywhere in the range give ourselves some room to grow. */
2178 if (align_start <= brk && brk < end) {
2179 base = brk + (16 * MiB);
2180 continue;
2181 } else if (align_start + guest_size < align_start) {
2182 /* we have run out of space */
2183 return -1;
2184 } else {
2185 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE |
2186 MAP_FIXED_NOREPLACE;
2187 void * mmap_start = mmap((void *) align_start, guest_size,
2188 PROT_NONE, flags, -1, 0);
2189 if (mmap_start != MAP_FAILED) {
2190 munmap((void *) align_start, guest_size);
2191 if (MAP_FIXED_NOREPLACE || mmap_start == (void *) align_start) {
2192 return (uintptr_t) mmap_start + offset;
2195 base += qemu_host_page_size;
2200 /* Return value for guest_base, or -1 if no hole found. */
2201 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
2202 long align, uintptr_t offset)
2204 GSList *maps, *iter;
2205 uintptr_t this_start, this_end, next_start, brk;
2206 intptr_t ret = -1;
2208 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
2210 maps = read_self_maps();
2212 /* Read brk after we've read the maps, which will malloc. */
2213 brk = (uintptr_t)sbrk(0);
2215 if (!maps) {
2216 return pgd_find_hole_fallback(guest_size, brk, align, offset);
2219 /* The first hole is before the first map entry. */
2220 this_start = mmap_min_addr;
2222 for (iter = maps; iter;
2223 this_start = next_start, iter = g_slist_next(iter)) {
2224 uintptr_t align_start, hole_size;
2226 this_end = ((MapInfo *)iter->data)->start;
2227 next_start = ((MapInfo *)iter->data)->end;
2228 align_start = ROUND_UP(this_start + offset, align);
2230 /* Skip holes that are too small. */
2231 if (align_start >= this_end) {
2232 continue;
2234 hole_size = this_end - align_start;
2235 if (hole_size < guest_size) {
2236 continue;
2239 /* If this hole contains brk, give ourselves some room to grow. */
2240 if (this_start <= brk && brk < this_end) {
2241 hole_size -= guest_size;
2242 if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) {
2243 align_start += 1 * GiB;
2244 } else if (hole_size >= 16 * MiB) {
2245 align_start += 16 * MiB;
2246 } else {
2247 align_start = (this_end - guest_size) & -align;
2248 if (align_start < this_start) {
2249 continue;
2254 /* Record the lowest successful match. */
2255 if (ret < 0) {
2256 ret = align_start - guest_loaddr;
2258 /* If this hole contains the identity map, select it. */
2259 if (align_start <= guest_loaddr &&
2260 guest_loaddr + guest_size <= this_end) {
2261 ret = 0;
2263 /* If this hole ends above the identity map, stop looking. */
2264 if (this_end >= guest_loaddr) {
2265 break;
2268 free_self_maps(maps);
2270 return ret;
2273 static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
2274 abi_ulong orig_hiaddr, long align)
2276 uintptr_t loaddr = orig_loaddr;
2277 uintptr_t hiaddr = orig_hiaddr;
2278 uintptr_t offset = 0;
2279 uintptr_t addr;
2281 if (hiaddr != orig_hiaddr) {
2282 error_report("%s: requires virtual address space that the "
2283 "host cannot provide (0x%" PRIx64 ")",
2284 image_name, (uint64_t)orig_hiaddr);
2285 exit(EXIT_FAILURE);
2288 loaddr &= -align;
2289 if (ARM_COMMPAGE) {
2291 * Extend the allocation to include the commpage.
2292 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2293 * need to ensure there is space bellow the guest_base so we
2294 * can map the commpage in the place needed when the address
2295 * arithmetic wraps around.
2297 if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
2298 hiaddr = (uintptr_t) 4 << 30;
2299 } else {
2300 offset = -(ARM_COMMPAGE & -align);
2304 addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
2305 if (addr == -1) {
2307 * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
2308 * that can satisfy both. But as the normal arm32 link base address
2309 * is ~32k, and we extend down to include the commpage, making the
2310 * overhead only ~96k, this is unlikely.
2312 error_report("%s: Unable to allocate %#zx bytes of "
2313 "virtual address space", image_name,
2314 (size_t)(hiaddr - loaddr));
2315 exit(EXIT_FAILURE);
2318 guest_base = addr;
2321 static void pgb_dynamic(const char *image_name, long align)
2324 * The executable is dynamic and does not require a fixed address.
2325 * All we need is a commpage that satisfies align.
2326 * If we do not need a commpage, leave guest_base == 0.
2328 if (ARM_COMMPAGE) {
2329 uintptr_t addr, commpage;
2331 /* 64-bit hosts should have used reserved_va. */
2332 assert(sizeof(uintptr_t) == 4);
2335 * By putting the commpage at the first hole, that puts guest_base
2336 * just above that, and maximises the positive guest addresses.
2338 commpage = ARM_COMMPAGE & -align;
2339 addr = pgb_find_hole(commpage, -commpage, align, 0);
2340 assert(addr != -1);
2341 guest_base = addr;
2345 static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
2346 abi_ulong guest_hiaddr, long align)
2348 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2349 void *addr, *test;
2351 if (guest_hiaddr > reserved_va) {
2352 error_report("%s: requires more than reserved virtual "
2353 "address space (0x%" PRIx64 " > 0x%lx)",
2354 image_name, (uint64_t)guest_hiaddr, reserved_va);
2355 exit(EXIT_FAILURE);
2358 /* Widen the "image" to the entire reserved address space. */
2359 pgb_static(image_name, 0, reserved_va, align);
2361 /* osdep.h defines this as 0 if it's missing */
2362 flags |= MAP_FIXED_NOREPLACE;
2364 /* Reserve the memory on the host. */
2365 assert(guest_base != 0);
2366 test = g2h(0);
2367 addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0);
2368 if (addr == MAP_FAILED || addr != test) {
2369 error_report("Unable to reserve 0x%lx bytes of virtual address "
2370 "space at %p (%s) for use as guest address space (check your"
2371 "virtual memory ulimit setting, min_mmap_addr or reserve less "
2372 "using -R option)", reserved_va, test, strerror(errno));
2373 exit(EXIT_FAILURE);
2377 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
2378 abi_ulong guest_hiaddr)
2380 /* In order to use host shmat, we must be able to honor SHMLBA. */
2381 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
2383 if (have_guest_base) {
2384 pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align);
2385 } else if (reserved_va) {
2386 pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align);
2387 } else if (guest_loaddr) {
2388 pgb_static(image_name, guest_loaddr, guest_hiaddr, align);
2389 } else {
2390 pgb_dynamic(image_name, align);
2393 /* Reserve and initialize the commpage. */
2394 if (!init_guest_commpage()) {
2396 * With have_guest_base, the user has selected the address and
2397 * we are trying to work with that. Otherwise, we have selected
2398 * free space and init_guest_commpage must succeeded.
2400 assert(have_guest_base);
2401 pgb_fail_in_use(image_name);
2404 assert(QEMU_IS_ALIGNED(guest_base, align));
2405 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
2406 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
2409 enum {
2410 /* The string "GNU\0" as a magic number. */
2411 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
2412 NOTE_DATA_SZ = 1 * KiB,
2413 NOTE_NAME_SZ = 4,
2414 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
2418 * Process a single gnu_property entry.
2419 * Return false for error.
2421 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
2422 struct image_info *info, bool have_prev_type,
2423 uint32_t *prev_type, Error **errp)
2425 uint32_t pr_type, pr_datasz, step;
2427 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
2428 goto error_data;
2430 datasz -= *off;
2431 data += *off / sizeof(uint32_t);
2433 if (datasz < 2 * sizeof(uint32_t)) {
2434 goto error_data;
2436 pr_type = data[0];
2437 pr_datasz = data[1];
2438 data += 2;
2439 datasz -= 2 * sizeof(uint32_t);
2440 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
2441 if (step > datasz) {
2442 goto error_data;
2445 /* Properties are supposed to be unique and sorted on pr_type. */
2446 if (have_prev_type && pr_type <= *prev_type) {
2447 if (pr_type == *prev_type) {
2448 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
2449 } else {
2450 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
2452 return false;
2454 *prev_type = pr_type;
2456 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
2457 return false;
2460 *off += 2 * sizeof(uint32_t) + step;
2461 return true;
2463 error_data:
2464 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
2465 return false;
2468 /* Process NT_GNU_PROPERTY_TYPE_0. */
2469 static bool parse_elf_properties(int image_fd,
2470 struct image_info *info,
2471 const struct elf_phdr *phdr,
2472 char bprm_buf[BPRM_BUF_SIZE],
2473 Error **errp)
2475 union {
2476 struct elf_note nhdr;
2477 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
2478 } note;
2480 int n, off, datasz;
2481 bool have_prev_type;
2482 uint32_t prev_type;
2484 /* Unless the arch requires properties, ignore them. */
2485 if (!ARCH_USE_GNU_PROPERTY) {
2486 return true;
2489 /* If the properties are crazy large, that's too bad. */
2490 n = phdr->p_filesz;
2491 if (n > sizeof(note)) {
2492 error_setg(errp, "PT_GNU_PROPERTY too large");
2493 return false;
2495 if (n < sizeof(note.nhdr)) {
2496 error_setg(errp, "PT_GNU_PROPERTY too small");
2497 return false;
2500 if (phdr->p_offset + n <= BPRM_BUF_SIZE) {
2501 memcpy(&note, bprm_buf + phdr->p_offset, n);
2502 } else {
2503 ssize_t len = pread(image_fd, &note, n, phdr->p_offset);
2504 if (len != n) {
2505 error_setg_errno(errp, errno, "Error reading file header");
2506 return false;
2511 * The contents of a valid PT_GNU_PROPERTY is a sequence
2512 * of uint32_t -- swap them all now.
2514 #ifdef BSWAP_NEEDED
2515 for (int i = 0; i < n / 4; i++) {
2516 bswap32s(note.data + i);
2518 #endif
2521 * Note that nhdr is 3 words, and that the "name" described by namesz
2522 * immediately follows nhdr and is thus at the 4th word. Further, all
2523 * of the inputs to the kernel's round_up are multiples of 4.
2525 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
2526 note.nhdr.n_namesz != NOTE_NAME_SZ ||
2527 note.data[3] != GNU0_MAGIC) {
2528 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
2529 return false;
2531 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
2533 datasz = note.nhdr.n_descsz + off;
2534 if (datasz > n) {
2535 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
2536 return false;
2539 have_prev_type = false;
2540 prev_type = 0;
2541 while (1) {
2542 if (off == datasz) {
2543 return true; /* end, exit ok */
2545 if (!parse_elf_property(note.data, &off, datasz, info,
2546 have_prev_type, &prev_type, errp)) {
2547 return false;
2549 have_prev_type = true;
2553 /* Load an ELF image into the address space.
2555 IMAGE_NAME is the filename of the image, to use in error messages.
2556 IMAGE_FD is the open file descriptor for the image.
2558 BPRM_BUF is a copy of the beginning of the file; this of course
2559 contains the elf file header at offset 0. It is assumed that this
2560 buffer is sufficiently aligned to present no problems to the host
2561 in accessing data at aligned offsets within the buffer.
2563 On return: INFO values will be filled in, as necessary or available. */
2565 static void load_elf_image(const char *image_name, int image_fd,
2566 struct image_info *info, char **pinterp_name,
2567 char bprm_buf[BPRM_BUF_SIZE])
2569 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2570 struct elf_phdr *phdr;
2571 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
2572 int i, retval, prot_exec;
2573 Error *err = NULL;
2575 /* First of all, some simple consistency checks */
2576 if (!elf_check_ident(ehdr)) {
2577 error_setg(&err, "Invalid ELF image for this architecture");
2578 goto exit_errmsg;
2580 bswap_ehdr(ehdr);
2581 if (!elf_check_ehdr(ehdr)) {
2582 error_setg(&err, "Invalid ELF image for this architecture");
2583 goto exit_errmsg;
2586 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2587 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2588 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
2589 } else {
2590 phdr = (struct elf_phdr *) alloca(i);
2591 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
2592 if (retval != i) {
2593 goto exit_read;
2596 bswap_phdr(phdr, ehdr->e_phnum);
2598 info->nsegs = 0;
2599 info->pt_dynamic_addr = 0;
2601 mmap_lock();
2604 * Find the maximum size of the image and allocate an appropriate
2605 * amount of memory to handle that. Locate the interpreter, if any.
2607 loaddr = -1, hiaddr = 0;
2608 info->alignment = 0;
2609 for (i = 0; i < ehdr->e_phnum; ++i) {
2610 struct elf_phdr *eppnt = phdr + i;
2611 if (eppnt->p_type == PT_LOAD) {
2612 abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
2613 if (a < loaddr) {
2614 loaddr = a;
2616 a = eppnt->p_vaddr + eppnt->p_memsz;
2617 if (a > hiaddr) {
2618 hiaddr = a;
2620 ++info->nsegs;
2621 info->alignment |= eppnt->p_align;
2622 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2623 g_autofree char *interp_name = NULL;
2625 if (*pinterp_name) {
2626 error_setg(&err, "Multiple PT_INTERP entries");
2627 goto exit_errmsg;
2630 interp_name = g_malloc(eppnt->p_filesz);
2632 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2633 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2634 eppnt->p_filesz);
2635 } else {
2636 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2637 eppnt->p_offset);
2638 if (retval != eppnt->p_filesz) {
2639 goto exit_read;
2642 if (interp_name[eppnt->p_filesz - 1] != 0) {
2643 error_setg(&err, "Invalid PT_INTERP entry");
2644 goto exit_errmsg;
2646 *pinterp_name = g_steal_pointer(&interp_name);
2647 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
2648 if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &err)) {
2649 goto exit_errmsg;
2654 if (pinterp_name != NULL) {
2656 * This is the main executable.
2658 * Reserve extra space for brk.
2659 * We hold on to this space while placing the interpreter
2660 * and the stack, lest they be placed immediately after
2661 * the data segment and block allocation from the brk.
2663 * 16MB is chosen as "large enough" without being so large
2664 * as to allow the result to not fit with a 32-bit guest on
2665 * a 32-bit host.
2667 info->reserve_brk = 16 * MiB;
2668 hiaddr += info->reserve_brk;
2670 if (ehdr->e_type == ET_EXEC) {
2672 * Make sure that the low address does not conflict with
2673 * MMAP_MIN_ADDR or the QEMU application itself.
2675 probe_guest_base(image_name, loaddr, hiaddr);
2676 } else {
2678 * The binary is dynamic, but we still need to
2679 * select guest_base. In this case we pass a size.
2681 probe_guest_base(image_name, 0, hiaddr - loaddr);
2686 * Reserve address space for all of this.
2688 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
2689 * exactly the address range that is required.
2691 * Otherwise this is ET_DYN, and we are searching for a location
2692 * that can hold the memory space required. If the image is
2693 * pre-linked, LOADDR will be non-zero, and the kernel should
2694 * honor that address if it happens to be free.
2696 * In both cases, we will overwrite pages in this range with mappings
2697 * from the executable.
2699 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2700 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
2701 (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0),
2702 -1, 0);
2703 if (load_addr == -1) {
2704 goto exit_mmap;
2706 load_bias = load_addr - loaddr;
2708 if (elf_is_fdpic(ehdr)) {
2709 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2710 g_malloc(sizeof(*loadsegs) * info->nsegs);
2712 for (i = 0; i < ehdr->e_phnum; ++i) {
2713 switch (phdr[i].p_type) {
2714 case PT_DYNAMIC:
2715 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2716 break;
2717 case PT_LOAD:
2718 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2719 loadsegs->p_vaddr = phdr[i].p_vaddr;
2720 loadsegs->p_memsz = phdr[i].p_memsz;
2721 ++loadsegs;
2722 break;
2727 info->load_bias = load_bias;
2728 info->code_offset = load_bias;
2729 info->data_offset = load_bias;
2730 info->load_addr = load_addr;
2731 info->entry = ehdr->e_entry + load_bias;
2732 info->start_code = -1;
2733 info->end_code = 0;
2734 info->start_data = -1;
2735 info->end_data = 0;
2736 info->brk = 0;
2737 info->elf_flags = ehdr->e_flags;
2739 prot_exec = PROT_EXEC;
2740 #ifdef TARGET_AARCH64
2742 * If the BTI feature is present, this indicates that the executable
2743 * pages of the startup binary should be mapped with PROT_BTI, so that
2744 * branch targets are enforced.
2746 * The startup binary is either the interpreter or the static executable.
2747 * The interpreter is responsible for all pages of a dynamic executable.
2749 * Elf notes are backward compatible to older cpus.
2750 * Do not enable BTI unless it is supported.
2752 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
2753 && (pinterp_name == NULL || *pinterp_name == 0)
2754 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
2755 prot_exec |= TARGET_PROT_BTI;
2757 #endif
2759 for (i = 0; i < ehdr->e_phnum; i++) {
2760 struct elf_phdr *eppnt = phdr + i;
2761 if (eppnt->p_type == PT_LOAD) {
2762 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
2763 int elf_prot = 0;
2765 if (eppnt->p_flags & PF_R) {
2766 elf_prot |= PROT_READ;
2768 if (eppnt->p_flags & PF_W) {
2769 elf_prot |= PROT_WRITE;
2771 if (eppnt->p_flags & PF_X) {
2772 elf_prot |= prot_exec;
2775 vaddr = load_bias + eppnt->p_vaddr;
2776 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2777 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2778 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
2781 * Some segments may be completely empty without any backing file
2782 * segment, in that case just let zero_bss allocate an empty buffer
2783 * for it.
2785 if (eppnt->p_filesz != 0) {
2786 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2787 MAP_PRIVATE | MAP_FIXED,
2788 image_fd, eppnt->p_offset - vaddr_po);
2790 if (error == -1) {
2791 goto exit_mmap;
2795 vaddr_ef = vaddr + eppnt->p_filesz;
2796 vaddr_em = vaddr + eppnt->p_memsz;
2798 /* If the load segment requests extra zeros (e.g. bss), map it. */
2799 if (vaddr_ef < vaddr_em) {
2800 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2803 /* Find the full program boundaries. */
2804 if (elf_prot & PROT_EXEC) {
2805 if (vaddr < info->start_code) {
2806 info->start_code = vaddr;
2808 if (vaddr_ef > info->end_code) {
2809 info->end_code = vaddr_ef;
2812 if (elf_prot & PROT_WRITE) {
2813 if (vaddr < info->start_data) {
2814 info->start_data = vaddr;
2816 if (vaddr_ef > info->end_data) {
2817 info->end_data = vaddr_ef;
2820 if (vaddr_em > info->brk) {
2821 info->brk = vaddr_em;
2823 #ifdef TARGET_MIPS
2824 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
2825 Mips_elf_abiflags_v0 abiflags;
2826 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
2827 error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry");
2828 goto exit_errmsg;
2830 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2831 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
2832 sizeof(Mips_elf_abiflags_v0));
2833 } else {
2834 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
2835 eppnt->p_offset);
2836 if (retval != sizeof(Mips_elf_abiflags_v0)) {
2837 goto exit_read;
2840 bswap_mips_abiflags(&abiflags);
2841 info->fp_abi = abiflags.fp_abi;
2842 #endif
2846 if (info->end_data == 0) {
2847 info->start_data = info->end_code;
2848 info->end_data = info->end_code;
2851 if (qemu_log_enabled()) {
2852 load_symbols(ehdr, image_fd, load_bias);
2855 mmap_unlock();
2857 close(image_fd);
2858 return;
2860 exit_read:
2861 if (retval >= 0) {
2862 error_setg(&err, "Incomplete read of file header");
2863 } else {
2864 error_setg_errno(&err, errno, "Error reading file header");
2866 goto exit_errmsg;
2867 exit_mmap:
2868 error_setg_errno(&err, errno, "Error mapping file");
2869 goto exit_errmsg;
2870 exit_errmsg:
2871 error_reportf_err(err, "%s: ", image_name);
2872 exit(-1);
2875 static void load_elf_interp(const char *filename, struct image_info *info,
2876 char bprm_buf[BPRM_BUF_SIZE])
2878 int fd, retval;
2879 Error *err = NULL;
2881 fd = open(path(filename), O_RDONLY);
2882 if (fd < 0) {
2883 error_setg_file_open(&err, errno, filename);
2884 error_report_err(err);
2885 exit(-1);
2888 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2889 if (retval < 0) {
2890 error_setg_errno(&err, errno, "Error reading file header");
2891 error_reportf_err(err, "%s: ", filename);
2892 exit(-1);
2895 if (retval < BPRM_BUF_SIZE) {
2896 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2899 load_elf_image(filename, fd, info, NULL, bprm_buf);
2902 static int symfind(const void *s0, const void *s1)
2904 target_ulong addr = *(target_ulong *)s0;
2905 struct elf_sym *sym = (struct elf_sym *)s1;
2906 int result = 0;
2907 if (addr < sym->st_value) {
2908 result = -1;
2909 } else if (addr >= sym->st_value + sym->st_size) {
2910 result = 1;
2912 return result;
2915 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2917 #if ELF_CLASS == ELFCLASS32
2918 struct elf_sym *syms = s->disas_symtab.elf32;
2919 #else
2920 struct elf_sym *syms = s->disas_symtab.elf64;
2921 #endif
2923 // binary search
2924 struct elf_sym *sym;
2926 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2927 if (sym != NULL) {
2928 return s->disas_strtab + sym->st_name;
2931 return "";
2934 /* FIXME: This should use elf_ops.h */
2935 static int symcmp(const void *s0, const void *s1)
2937 struct elf_sym *sym0 = (struct elf_sym *)s0;
2938 struct elf_sym *sym1 = (struct elf_sym *)s1;
2939 return (sym0->st_value < sym1->st_value)
2940 ? -1
2941 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2944 /* Best attempt to load symbols from this ELF object. */
2945 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2947 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2948 uint64_t segsz;
2949 struct elf_shdr *shdr;
2950 char *strings = NULL;
2951 struct syminfo *s = NULL;
2952 struct elf_sym *new_syms, *syms = NULL;
2954 shnum = hdr->e_shnum;
2955 i = shnum * sizeof(struct elf_shdr);
2956 shdr = (struct elf_shdr *)alloca(i);
2957 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2958 return;
2961 bswap_shdr(shdr, shnum);
2962 for (i = 0; i < shnum; ++i) {
2963 if (shdr[i].sh_type == SHT_SYMTAB) {
2964 sym_idx = i;
2965 str_idx = shdr[i].sh_link;
2966 goto found;
2970 /* There will be no symbol table if the file was stripped. */
2971 return;
2973 found:
2974 /* Now know where the strtab and symtab are. Snarf them. */
2975 s = g_try_new(struct syminfo, 1);
2976 if (!s) {
2977 goto give_up;
2980 segsz = shdr[str_idx].sh_size;
2981 s->disas_strtab = strings = g_try_malloc(segsz);
2982 if (!strings ||
2983 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
2984 goto give_up;
2987 segsz = shdr[sym_idx].sh_size;
2988 syms = g_try_malloc(segsz);
2989 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
2990 goto give_up;
2993 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
2994 /* Implausibly large symbol table: give up rather than ploughing
2995 * on with the number of symbols calculation overflowing
2997 goto give_up;
2999 nsyms = segsz / sizeof(struct elf_sym);
3000 for (i = 0; i < nsyms; ) {
3001 bswap_sym(syms + i);
3002 /* Throw away entries which we do not need. */
3003 if (syms[i].st_shndx == SHN_UNDEF
3004 || syms[i].st_shndx >= SHN_LORESERVE
3005 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3006 if (i < --nsyms) {
3007 syms[i] = syms[nsyms];
3009 } else {
3010 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3011 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3012 syms[i].st_value &= ~(target_ulong)1;
3013 #endif
3014 syms[i].st_value += load_bias;
3015 i++;
3019 /* No "useful" symbol. */
3020 if (nsyms == 0) {
3021 goto give_up;
3024 /* Attempt to free the storage associated with the local symbols
3025 that we threw away. Whether or not this has any effect on the
3026 memory allocation depends on the malloc implementation and how
3027 many symbols we managed to discard. */
3028 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3029 if (new_syms == NULL) {
3030 goto give_up;
3032 syms = new_syms;
3034 qsort(syms, nsyms, sizeof(*syms), symcmp);
3036 s->disas_num_syms = nsyms;
3037 #if ELF_CLASS == ELFCLASS32
3038 s->disas_symtab.elf32 = syms;
3039 #else
3040 s->disas_symtab.elf64 = syms;
3041 #endif
3042 s->lookup_symbol = lookup_symbolxx;
3043 s->next = syminfos;
3044 syminfos = s;
3046 return;
3048 give_up:
3049 g_free(s);
3050 g_free(strings);
3051 g_free(syms);
3054 uint32_t get_elf_eflags(int fd)
3056 struct elfhdr ehdr;
3057 off_t offset;
3058 int ret;
3060 /* Read ELF header */
3061 offset = lseek(fd, 0, SEEK_SET);
3062 if (offset == (off_t) -1) {
3063 return 0;
3065 ret = read(fd, &ehdr, sizeof(ehdr));
3066 if (ret < sizeof(ehdr)) {
3067 return 0;
3069 offset = lseek(fd, offset, SEEK_SET);
3070 if (offset == (off_t) -1) {
3071 return 0;
3074 /* Check ELF signature */
3075 if (!elf_check_ident(&ehdr)) {
3076 return 0;
3079 /* check header */
3080 bswap_ehdr(&ehdr);
3081 if (!elf_check_ehdr(&ehdr)) {
3082 return 0;
3085 /* return architecture id */
3086 return ehdr.e_flags;
3089 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3091 struct image_info interp_info;
3092 struct elfhdr elf_ex;
3093 char *elf_interpreter = NULL;
3094 char *scratch;
3096 memset(&interp_info, 0, sizeof(interp_info));
3097 #ifdef TARGET_MIPS
3098 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3099 #endif
3101 info->start_mmap = (abi_ulong)ELF_START_MMAP;
3103 load_elf_image(bprm->filename, bprm->fd, info,
3104 &elf_interpreter, bprm->buf);
3106 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3107 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3108 when we load the interpreter. */
3109 elf_ex = *(struct elfhdr *)bprm->buf;
3111 /* Do this so that we can load the interpreter, if need be. We will
3112 change some of these later */
3113 bprm->p = setup_arg_pages(bprm, info);
3115 scratch = g_new0(char, TARGET_PAGE_SIZE);
3116 if (STACK_GROWS_DOWN) {
3117 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3118 bprm->p, info->stack_limit);
3119 info->file_string = bprm->p;
3120 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3121 bprm->p, info->stack_limit);
3122 info->env_strings = bprm->p;
3123 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3124 bprm->p, info->stack_limit);
3125 info->arg_strings = bprm->p;
3126 } else {
3127 info->arg_strings = bprm->p;
3128 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3129 bprm->p, info->stack_limit);
3130 info->env_strings = bprm->p;
3131 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3132 bprm->p, info->stack_limit);
3133 info->file_string = bprm->p;
3134 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3135 bprm->p, info->stack_limit);
3138 g_free(scratch);
3140 if (!bprm->p) {
3141 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3142 exit(-1);
3145 if (elf_interpreter) {
3146 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3148 /* If the program interpreter is one of these two, then assume
3149 an iBCS2 image. Otherwise assume a native linux image. */
3151 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3152 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3153 info->personality = PER_SVR4;
3155 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3156 and some applications "depend" upon this behavior. Since
3157 we do not have the power to recompile these, we emulate
3158 the SVr4 behavior. Sigh. */
3159 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
3160 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3162 #ifdef TARGET_MIPS
3163 info->interp_fp_abi = interp_info.fp_abi;
3164 #endif
3167 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
3168 info, (elf_interpreter ? &interp_info : NULL));
3169 info->start_stack = bprm->p;
3171 /* If we have an interpreter, set that as the program's entry point.
3172 Copy the load_bias as well, to help PPC64 interpret the entry
3173 point as a function descriptor. Do this after creating elf tables
3174 so that we copy the original program entry point into the AUXV. */
3175 if (elf_interpreter) {
3176 info->load_bias = interp_info.load_bias;
3177 info->entry = interp_info.entry;
3178 g_free(elf_interpreter);
3181 #ifdef USE_ELF_CORE_DUMP
3182 bprm->core_dump = &elf_core_dump;
3183 #endif
3186 * If we reserved extra space for brk, release it now.
3187 * The implementation of do_brk in syscalls.c expects to be able
3188 * to mmap pages in this space.
3190 if (info->reserve_brk) {
3191 abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk);
3192 abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk);
3193 target_munmap(start_brk, end_brk - start_brk);
3196 return 0;
3199 #ifdef USE_ELF_CORE_DUMP
3201 * Definitions to generate Intel SVR4-like core files.
3202 * These mostly have the same names as the SVR4 types with "target_elf_"
3203 * tacked on the front to prevent clashes with linux definitions,
3204 * and the typedef forms have been avoided. This is mostly like
3205 * the SVR4 structure, but more Linuxy, with things that Linux does
3206 * not support and which gdb doesn't really use excluded.
3208 * Fields we don't dump (their contents is zero) in linux-user qemu
3209 * are marked with XXX.
3211 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3213 * Porting ELF coredump for target is (quite) simple process. First you
3214 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3215 * the target resides):
3217 * #define USE_ELF_CORE_DUMP
3219 * Next you define type of register set used for dumping. ELF specification
3220 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3222 * typedef <target_regtype> target_elf_greg_t;
3223 * #define ELF_NREG <number of registers>
3224 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3226 * Last step is to implement target specific function that copies registers
3227 * from given cpu into just specified register set. Prototype is:
3229 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3230 * const CPUArchState *env);
3232 * Parameters:
3233 * regs - copy register values into here (allocated and zeroed by caller)
3234 * env - copy registers from here
3236 * Example for ARM target is provided in this file.
3239 /* An ELF note in memory */
3240 struct memelfnote {
3241 const char *name;
3242 size_t namesz;
3243 size_t namesz_rounded;
3244 int type;
3245 size_t datasz;
3246 size_t datasz_rounded;
3247 void *data;
3248 size_t notesz;
3251 struct target_elf_siginfo {
3252 abi_int si_signo; /* signal number */
3253 abi_int si_code; /* extra code */
3254 abi_int si_errno; /* errno */
3257 struct target_elf_prstatus {
3258 struct target_elf_siginfo pr_info; /* Info associated with signal */
3259 abi_short pr_cursig; /* Current signal */
3260 abi_ulong pr_sigpend; /* XXX */
3261 abi_ulong pr_sighold; /* XXX */
3262 target_pid_t pr_pid;
3263 target_pid_t pr_ppid;
3264 target_pid_t pr_pgrp;
3265 target_pid_t pr_sid;
3266 struct target_timeval pr_utime; /* XXX User time */
3267 struct target_timeval pr_stime; /* XXX System time */
3268 struct target_timeval pr_cutime; /* XXX Cumulative user time */
3269 struct target_timeval pr_cstime; /* XXX Cumulative system time */
3270 target_elf_gregset_t pr_reg; /* GP registers */
3271 abi_int pr_fpvalid; /* XXX */
3274 #define ELF_PRARGSZ (80) /* Number of chars for args */
3276 struct target_elf_prpsinfo {
3277 char pr_state; /* numeric process state */
3278 char pr_sname; /* char for pr_state */
3279 char pr_zomb; /* zombie */
3280 char pr_nice; /* nice val */
3281 abi_ulong pr_flag; /* flags */
3282 target_uid_t pr_uid;
3283 target_gid_t pr_gid;
3284 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
3285 /* Lots missing */
3286 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
3287 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
3290 /* Here is the structure in which status of each thread is captured. */
3291 struct elf_thread_status {
3292 QTAILQ_ENTRY(elf_thread_status) ets_link;
3293 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
3294 #if 0
3295 elf_fpregset_t fpu; /* NT_PRFPREG */
3296 struct task_struct *thread;
3297 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
3298 #endif
3299 struct memelfnote notes[1];
3300 int num_notes;
3303 struct elf_note_info {
3304 struct memelfnote *notes;
3305 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
3306 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
3308 QTAILQ_HEAD(, elf_thread_status) thread_list;
3309 #if 0
3311 * Current version of ELF coredump doesn't support
3312 * dumping fp regs etc.
3314 elf_fpregset_t *fpu;
3315 elf_fpxregset_t *xfpu;
3316 int thread_status_size;
3317 #endif
3318 int notes_size;
3319 int numnote;
3322 struct vm_area_struct {
3323 target_ulong vma_start; /* start vaddr of memory region */
3324 target_ulong vma_end; /* end vaddr of memory region */
3325 abi_ulong vma_flags; /* protection etc. flags for the region */
3326 QTAILQ_ENTRY(vm_area_struct) vma_link;
3329 struct mm_struct {
3330 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
3331 int mm_count; /* number of mappings */
3334 static struct mm_struct *vma_init(void);
3335 static void vma_delete(struct mm_struct *);
3336 static int vma_add_mapping(struct mm_struct *, target_ulong,
3337 target_ulong, abi_ulong);
3338 static int vma_get_mapping_count(const struct mm_struct *);
3339 static struct vm_area_struct *vma_first(const struct mm_struct *);
3340 static struct vm_area_struct *vma_next(struct vm_area_struct *);
3341 static abi_ulong vma_dump_size(const struct vm_area_struct *);
3342 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3343 unsigned long flags);
3345 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
3346 static void fill_note(struct memelfnote *, const char *, int,
3347 unsigned int, void *);
3348 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
3349 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
3350 static void fill_auxv_note(struct memelfnote *, const TaskState *);
3351 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
3352 static size_t note_size(const struct memelfnote *);
3353 static void free_note_info(struct elf_note_info *);
3354 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
3355 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
3356 static int core_dump_filename(const TaskState *, char *, size_t);
3358 static int dump_write(int, const void *, size_t);
3359 static int write_note(struct memelfnote *, int);
3360 static int write_note_info(struct elf_note_info *, int);
3362 #ifdef BSWAP_NEEDED
3363 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
3365 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3366 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3367 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
3368 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
3369 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3370 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
3371 prstatus->pr_pid = tswap32(prstatus->pr_pid);
3372 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3373 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3374 prstatus->pr_sid = tswap32(prstatus->pr_sid);
3375 /* cpu times are not filled, so we skip them */
3376 /* regs should be in correct format already */
3377 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3380 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
3382 psinfo->pr_flag = tswapal(psinfo->pr_flag);
3383 psinfo->pr_uid = tswap16(psinfo->pr_uid);
3384 psinfo->pr_gid = tswap16(psinfo->pr_gid);
3385 psinfo->pr_pid = tswap32(psinfo->pr_pid);
3386 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
3387 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
3388 psinfo->pr_sid = tswap32(psinfo->pr_sid);
3391 static void bswap_note(struct elf_note *en)
3393 bswap32s(&en->n_namesz);
3394 bswap32s(&en->n_descsz);
3395 bswap32s(&en->n_type);
3397 #else
3398 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
3399 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
3400 static inline void bswap_note(struct elf_note *en) { }
3401 #endif /* BSWAP_NEEDED */
3404 * Minimal support for linux memory regions. These are needed
3405 * when we are finding out what memory exactly belongs to
3406 * emulated process. No locks needed here, as long as
3407 * thread that received the signal is stopped.
3410 static struct mm_struct *vma_init(void)
3412 struct mm_struct *mm;
3414 if ((mm = g_malloc(sizeof (*mm))) == NULL)
3415 return (NULL);
3417 mm->mm_count = 0;
3418 QTAILQ_INIT(&mm->mm_mmap);
3420 return (mm);
3423 static void vma_delete(struct mm_struct *mm)
3425 struct vm_area_struct *vma;
3427 while ((vma = vma_first(mm)) != NULL) {
3428 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
3429 g_free(vma);
3431 g_free(mm);
3434 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3435 target_ulong end, abi_ulong flags)
3437 struct vm_area_struct *vma;
3439 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
3440 return (-1);
3442 vma->vma_start = start;
3443 vma->vma_end = end;
3444 vma->vma_flags = flags;
3446 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
3447 mm->mm_count++;
3449 return (0);
3452 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3454 return (QTAILQ_FIRST(&mm->mm_mmap));
3457 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3459 return (QTAILQ_NEXT(vma, vma_link));
3462 static int vma_get_mapping_count(const struct mm_struct *mm)
3464 return (mm->mm_count);
3468 * Calculate file (dump) size of given memory region.
3470 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3472 /* if we cannot even read the first page, skip it */
3473 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
3474 return (0);
3477 * Usually we don't dump executable pages as they contain
3478 * non-writable code that debugger can read directly from
3479 * target library etc. However, thread stacks are marked
3480 * also executable so we read in first page of given region
3481 * and check whether it contains elf header. If there is
3482 * no elf header, we dump it.
3484 if (vma->vma_flags & PROT_EXEC) {
3485 char page[TARGET_PAGE_SIZE];
3487 copy_from_user(page, vma->vma_start, sizeof (page));
3488 if ((page[EI_MAG0] == ELFMAG0) &&
3489 (page[EI_MAG1] == ELFMAG1) &&
3490 (page[EI_MAG2] == ELFMAG2) &&
3491 (page[EI_MAG3] == ELFMAG3)) {
3493 * Mappings are possibly from ELF binary. Don't dump
3494 * them.
3496 return (0);
3500 return (vma->vma_end - vma->vma_start);
3503 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3504 unsigned long flags)
3506 struct mm_struct *mm = (struct mm_struct *)priv;
3508 vma_add_mapping(mm, start, end, flags);
3509 return (0);
3512 static void fill_note(struct memelfnote *note, const char *name, int type,
3513 unsigned int sz, void *data)
3515 unsigned int namesz;
3517 namesz = strlen(name) + 1;
3518 note->name = name;
3519 note->namesz = namesz;
3520 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3521 note->type = type;
3522 note->datasz = sz;
3523 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3525 note->data = data;
3528 * We calculate rounded up note size here as specified by
3529 * ELF document.
3531 note->notesz = sizeof (struct elf_note) +
3532 note->namesz_rounded + note->datasz_rounded;
3535 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
3536 uint32_t flags)
3538 (void) memset(elf, 0, sizeof(*elf));
3540 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
3541 elf->e_ident[EI_CLASS] = ELF_CLASS;
3542 elf->e_ident[EI_DATA] = ELF_DATA;
3543 elf->e_ident[EI_VERSION] = EV_CURRENT;
3544 elf->e_ident[EI_OSABI] = ELF_OSABI;
3546 elf->e_type = ET_CORE;
3547 elf->e_machine = machine;
3548 elf->e_version = EV_CURRENT;
3549 elf->e_phoff = sizeof(struct elfhdr);
3550 elf->e_flags = flags;
3551 elf->e_ehsize = sizeof(struct elfhdr);
3552 elf->e_phentsize = sizeof(struct elf_phdr);
3553 elf->e_phnum = segs;
3555 bswap_ehdr(elf);
3558 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
3560 phdr->p_type = PT_NOTE;
3561 phdr->p_offset = offset;
3562 phdr->p_vaddr = 0;
3563 phdr->p_paddr = 0;
3564 phdr->p_filesz = sz;
3565 phdr->p_memsz = 0;
3566 phdr->p_flags = 0;
3567 phdr->p_align = 0;
3569 bswap_phdr(phdr, 1);
3572 static size_t note_size(const struct memelfnote *note)
3574 return (note->notesz);
3577 static void fill_prstatus(struct target_elf_prstatus *prstatus,
3578 const TaskState *ts, int signr)
3580 (void) memset(prstatus, 0, sizeof (*prstatus));
3581 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
3582 prstatus->pr_pid = ts->ts_tid;
3583 prstatus->pr_ppid = getppid();
3584 prstatus->pr_pgrp = getpgrp();
3585 prstatus->pr_sid = getsid(0);
3587 bswap_prstatus(prstatus);
3590 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
3592 char *base_filename;
3593 unsigned int i, len;
3595 (void) memset(psinfo, 0, sizeof (*psinfo));
3597 len = ts->info->arg_end - ts->info->arg_start;
3598 if (len >= ELF_PRARGSZ)
3599 len = ELF_PRARGSZ - 1;
3600 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
3601 return -EFAULT;
3602 for (i = 0; i < len; i++)
3603 if (psinfo->pr_psargs[i] == 0)
3604 psinfo->pr_psargs[i] = ' ';
3605 psinfo->pr_psargs[len] = 0;
3607 psinfo->pr_pid = getpid();
3608 psinfo->pr_ppid = getppid();
3609 psinfo->pr_pgrp = getpgrp();
3610 psinfo->pr_sid = getsid(0);
3611 psinfo->pr_uid = getuid();
3612 psinfo->pr_gid = getgid();
3614 base_filename = g_path_get_basename(ts->bprm->filename);
3616 * Using strncpy here is fine: at max-length,
3617 * this field is not NUL-terminated.
3619 (void) strncpy(psinfo->pr_fname, base_filename,
3620 sizeof(psinfo->pr_fname));
3622 g_free(base_filename);
3623 bswap_psinfo(psinfo);
3624 return (0);
3627 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
3629 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
3630 elf_addr_t orig_auxv = auxv;
3631 void *ptr;
3632 int len = ts->info->auxv_len;
3635 * Auxiliary vector is stored in target process stack. It contains
3636 * {type, value} pairs that we need to dump into note. This is not
3637 * strictly necessary but we do it here for sake of completeness.
3640 /* read in whole auxv vector and copy it to memelfnote */
3641 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
3642 if (ptr != NULL) {
3643 fill_note(note, "CORE", NT_AUXV, len, ptr);
3644 unlock_user(ptr, auxv, len);
3649 * Constructs name of coredump file. We have following convention
3650 * for the name:
3651 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3653 * Returns 0 in case of success, -1 otherwise (errno is set).
3655 static int core_dump_filename(const TaskState *ts, char *buf,
3656 size_t bufsize)
3658 char timestamp[64];
3659 char *base_filename = NULL;
3660 struct timeval tv;
3661 struct tm tm;
3663 assert(bufsize >= PATH_MAX);
3665 if (gettimeofday(&tv, NULL) < 0) {
3666 (void) fprintf(stderr, "unable to get current timestamp: %s",
3667 strerror(errno));
3668 return (-1);
3671 base_filename = g_path_get_basename(ts->bprm->filename);
3672 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
3673 localtime_r(&tv.tv_sec, &tm));
3674 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
3675 base_filename, timestamp, (int)getpid());
3676 g_free(base_filename);
3678 return (0);
3681 static int dump_write(int fd, const void *ptr, size_t size)
3683 const char *bufp = (const char *)ptr;
3684 ssize_t bytes_written, bytes_left;
3685 struct rlimit dumpsize;
3686 off_t pos;
3688 bytes_written = 0;
3689 getrlimit(RLIMIT_CORE, &dumpsize);
3690 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
3691 if (errno == ESPIPE) { /* not a seekable stream */
3692 bytes_left = size;
3693 } else {
3694 return pos;
3696 } else {
3697 if (dumpsize.rlim_cur <= pos) {
3698 return -1;
3699 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
3700 bytes_left = size;
3701 } else {
3702 size_t limit_left=dumpsize.rlim_cur - pos;
3703 bytes_left = limit_left >= size ? size : limit_left ;
3708 * In normal conditions, single write(2) should do but
3709 * in case of socket etc. this mechanism is more portable.
3711 do {
3712 bytes_written = write(fd, bufp, bytes_left);
3713 if (bytes_written < 0) {
3714 if (errno == EINTR)
3715 continue;
3716 return (-1);
3717 } else if (bytes_written == 0) { /* eof */
3718 return (-1);
3720 bufp += bytes_written;
3721 bytes_left -= bytes_written;
3722 } while (bytes_left > 0);
3724 return (0);
3727 static int write_note(struct memelfnote *men, int fd)
3729 struct elf_note en;
3731 en.n_namesz = men->namesz;
3732 en.n_type = men->type;
3733 en.n_descsz = men->datasz;
3735 bswap_note(&en);
3737 if (dump_write(fd, &en, sizeof(en)) != 0)
3738 return (-1);
3739 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3740 return (-1);
3741 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
3742 return (-1);
3744 return (0);
3747 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
3749 CPUState *cpu = env_cpu((CPUArchState *)env);
3750 TaskState *ts = (TaskState *)cpu->opaque;
3751 struct elf_thread_status *ets;
3753 ets = g_malloc0(sizeof (*ets));
3754 ets->num_notes = 1; /* only prstatus is dumped */
3755 fill_prstatus(&ets->prstatus, ts, 0);
3756 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3757 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
3758 &ets->prstatus);
3760 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
3762 info->notes_size += note_size(&ets->notes[0]);
3765 static void init_note_info(struct elf_note_info *info)
3767 /* Initialize the elf_note_info structure so that it is at
3768 * least safe to call free_note_info() on it. Must be
3769 * called before calling fill_note_info().
3771 memset(info, 0, sizeof (*info));
3772 QTAILQ_INIT(&info->thread_list);
3775 static int fill_note_info(struct elf_note_info *info,
3776 long signr, const CPUArchState *env)
3778 #define NUMNOTES 3
3779 CPUState *cpu = env_cpu((CPUArchState *)env);
3780 TaskState *ts = (TaskState *)cpu->opaque;
3781 int i;
3783 info->notes = g_new0(struct memelfnote, NUMNOTES);
3784 if (info->notes == NULL)
3785 return (-ENOMEM);
3786 info->prstatus = g_malloc0(sizeof (*info->prstatus));
3787 if (info->prstatus == NULL)
3788 return (-ENOMEM);
3789 info->psinfo = g_malloc0(sizeof (*info->psinfo));
3790 if (info->prstatus == NULL)
3791 return (-ENOMEM);
3794 * First fill in status (and registers) of current thread
3795 * including process info & aux vector.
3797 fill_prstatus(info->prstatus, ts, signr);
3798 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3799 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
3800 sizeof (*info->prstatus), info->prstatus);
3801 fill_psinfo(info->psinfo, ts);
3802 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
3803 sizeof (*info->psinfo), info->psinfo);
3804 fill_auxv_note(&info->notes[2], ts);
3805 info->numnote = 3;
3807 info->notes_size = 0;
3808 for (i = 0; i < info->numnote; i++)
3809 info->notes_size += note_size(&info->notes[i]);
3811 /* read and fill status of all threads */
3812 cpu_list_lock();
3813 CPU_FOREACH(cpu) {
3814 if (cpu == thread_cpu) {
3815 continue;
3817 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
3819 cpu_list_unlock();
3821 return (0);
3824 static void free_note_info(struct elf_note_info *info)
3826 struct elf_thread_status *ets;
3828 while (!QTAILQ_EMPTY(&info->thread_list)) {
3829 ets = QTAILQ_FIRST(&info->thread_list);
3830 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
3831 g_free(ets);
3834 g_free(info->prstatus);
3835 g_free(info->psinfo);
3836 g_free(info->notes);
3839 static int write_note_info(struct elf_note_info *info, int fd)
3841 struct elf_thread_status *ets;
3842 int i, error = 0;
3844 /* write prstatus, psinfo and auxv for current thread */
3845 for (i = 0; i < info->numnote; i++)
3846 if ((error = write_note(&info->notes[i], fd)) != 0)
3847 return (error);
3849 /* write prstatus for each thread */
3850 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
3851 if ((error = write_note(&ets->notes[0], fd)) != 0)
3852 return (error);
3855 return (0);
3859 * Write out ELF coredump.
3861 * See documentation of ELF object file format in:
3862 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3864 * Coredump format in linux is following:
3866 * 0 +----------------------+ \
3867 * | ELF header | ET_CORE |
3868 * +----------------------+ |
3869 * | ELF program headers | |--- headers
3870 * | - NOTE section | |
3871 * | - PT_LOAD sections | |
3872 * +----------------------+ /
3873 * | NOTEs: |
3874 * | - NT_PRSTATUS |
3875 * | - NT_PRSINFO |
3876 * | - NT_AUXV |
3877 * +----------------------+ <-- aligned to target page
3878 * | Process memory dump |
3879 * : :
3880 * . .
3881 * : :
3882 * | |
3883 * +----------------------+
3885 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3886 * NT_PRSINFO -> struct elf_prpsinfo
3887 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3889 * Format follows System V format as close as possible. Current
3890 * version limitations are as follows:
3891 * - no floating point registers are dumped
3893 * Function returns 0 in case of success, negative errno otherwise.
3895 * TODO: make this work also during runtime: it should be
3896 * possible to force coredump from running process and then
3897 * continue processing. For example qemu could set up SIGUSR2
3898 * handler (provided that target process haven't registered
3899 * handler for that) that does the dump when signal is received.
3901 static int elf_core_dump(int signr, const CPUArchState *env)
3903 const CPUState *cpu = env_cpu((CPUArchState *)env);
3904 const TaskState *ts = (const TaskState *)cpu->opaque;
3905 struct vm_area_struct *vma = NULL;
3906 char corefile[PATH_MAX];
3907 struct elf_note_info info;
3908 struct elfhdr elf;
3909 struct elf_phdr phdr;
3910 struct rlimit dumpsize;
3911 struct mm_struct *mm = NULL;
3912 off_t offset = 0, data_offset = 0;
3913 int segs = 0;
3914 int fd = -1;
3916 init_note_info(&info);
3918 errno = 0;
3919 getrlimit(RLIMIT_CORE, &dumpsize);
3920 if (dumpsize.rlim_cur == 0)
3921 return 0;
3923 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
3924 return (-errno);
3926 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3927 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3928 return (-errno);
3931 * Walk through target process memory mappings and
3932 * set up structure containing this information. After
3933 * this point vma_xxx functions can be used.
3935 if ((mm = vma_init()) == NULL)
3936 goto out;
3938 walk_memory_regions(mm, vma_walker);
3939 segs = vma_get_mapping_count(mm);
3942 * Construct valid coredump ELF header. We also
3943 * add one more segment for notes.
3945 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3946 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3947 goto out;
3949 /* fill in the in-memory version of notes */
3950 if (fill_note_info(&info, signr, env) < 0)
3951 goto out;
3953 offset += sizeof (elf); /* elf header */
3954 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3956 /* write out notes program header */
3957 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3959 offset += info.notes_size;
3960 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3961 goto out;
3964 * ELF specification wants data to start at page boundary so
3965 * we align it here.
3967 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3970 * Write program headers for memory regions mapped in
3971 * the target process.
3973 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3974 (void) memset(&phdr, 0, sizeof (phdr));
3976 phdr.p_type = PT_LOAD;
3977 phdr.p_offset = offset;
3978 phdr.p_vaddr = vma->vma_start;
3979 phdr.p_paddr = 0;
3980 phdr.p_filesz = vma_dump_size(vma);
3981 offset += phdr.p_filesz;
3982 phdr.p_memsz = vma->vma_end - vma->vma_start;
3983 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3984 if (vma->vma_flags & PROT_WRITE)
3985 phdr.p_flags |= PF_W;
3986 if (vma->vma_flags & PROT_EXEC)
3987 phdr.p_flags |= PF_X;
3988 phdr.p_align = ELF_EXEC_PAGESIZE;
3990 bswap_phdr(&phdr, 1);
3991 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
3992 goto out;
3997 * Next we write notes just after program headers. No
3998 * alignment needed here.
4000 if (write_note_info(&info, fd) < 0)
4001 goto out;
4003 /* align data to page boundary */
4004 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4005 goto out;
4008 * Finally we can dump process memory into corefile as well.
4010 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4011 abi_ulong addr;
4012 abi_ulong end;
4014 end = vma->vma_start + vma_dump_size(vma);
4016 for (addr = vma->vma_start; addr < end;
4017 addr += TARGET_PAGE_SIZE) {
4018 char page[TARGET_PAGE_SIZE];
4019 int error;
4022 * Read in page from target process memory and
4023 * write it to coredump file.
4025 error = copy_from_user(page, addr, sizeof (page));
4026 if (error != 0) {
4027 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
4028 addr);
4029 errno = -error;
4030 goto out;
4032 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4033 goto out;
4037 out:
4038 free_note_info(&info);
4039 if (mm != NULL)
4040 vma_delete(mm);
4041 (void) close(fd);
4043 if (errno != 0)
4044 return (-errno);
4045 return (0);
4047 #endif /* USE_ELF_CORE_DUMP */
4049 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4051 init_thread(regs, infop);