linux-user: Use "!= 0" when checking if MAP_FIXED_NOREPLACE is non-zero
[qemu/ar7.git] / linux-user / elfload.c
blobcae41d504d36e22c293b9d1500a07c274413afa4
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 != 0 ||
2192 mmap_start == (void *) align_start) {
2193 return (uintptr_t) mmap_start + offset;
2196 base += qemu_host_page_size;
2201 /* Return value for guest_base, or -1 if no hole found. */
2202 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
2203 long align, uintptr_t offset)
2205 GSList *maps, *iter;
2206 uintptr_t this_start, this_end, next_start, brk;
2207 intptr_t ret = -1;
2209 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
2211 maps = read_self_maps();
2213 /* Read brk after we've read the maps, which will malloc. */
2214 brk = (uintptr_t)sbrk(0);
2216 if (!maps) {
2217 return pgd_find_hole_fallback(guest_size, brk, align, offset);
2220 /* The first hole is before the first map entry. */
2221 this_start = mmap_min_addr;
2223 for (iter = maps; iter;
2224 this_start = next_start, iter = g_slist_next(iter)) {
2225 uintptr_t align_start, hole_size;
2227 this_end = ((MapInfo *)iter->data)->start;
2228 next_start = ((MapInfo *)iter->data)->end;
2229 align_start = ROUND_UP(this_start + offset, align);
2231 /* Skip holes that are too small. */
2232 if (align_start >= this_end) {
2233 continue;
2235 hole_size = this_end - align_start;
2236 if (hole_size < guest_size) {
2237 continue;
2240 /* If this hole contains brk, give ourselves some room to grow. */
2241 if (this_start <= brk && brk < this_end) {
2242 hole_size -= guest_size;
2243 if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) {
2244 align_start += 1 * GiB;
2245 } else if (hole_size >= 16 * MiB) {
2246 align_start += 16 * MiB;
2247 } else {
2248 align_start = (this_end - guest_size) & -align;
2249 if (align_start < this_start) {
2250 continue;
2255 /* Record the lowest successful match. */
2256 if (ret < 0) {
2257 ret = align_start - guest_loaddr;
2259 /* If this hole contains the identity map, select it. */
2260 if (align_start <= guest_loaddr &&
2261 guest_loaddr + guest_size <= this_end) {
2262 ret = 0;
2264 /* If this hole ends above the identity map, stop looking. */
2265 if (this_end >= guest_loaddr) {
2266 break;
2269 free_self_maps(maps);
2271 return ret;
2274 static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
2275 abi_ulong orig_hiaddr, long align)
2277 uintptr_t loaddr = orig_loaddr;
2278 uintptr_t hiaddr = orig_hiaddr;
2279 uintptr_t offset = 0;
2280 uintptr_t addr;
2282 if (hiaddr != orig_hiaddr) {
2283 error_report("%s: requires virtual address space that the "
2284 "host cannot provide (0x%" PRIx64 ")",
2285 image_name, (uint64_t)orig_hiaddr);
2286 exit(EXIT_FAILURE);
2289 loaddr &= -align;
2290 if (ARM_COMMPAGE) {
2292 * Extend the allocation to include the commpage.
2293 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2294 * need to ensure there is space bellow the guest_base so we
2295 * can map the commpage in the place needed when the address
2296 * arithmetic wraps around.
2298 if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
2299 hiaddr = (uintptr_t) 4 << 30;
2300 } else {
2301 offset = -(ARM_COMMPAGE & -align);
2305 addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
2306 if (addr == -1) {
2308 * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
2309 * that can satisfy both. But as the normal arm32 link base address
2310 * is ~32k, and we extend down to include the commpage, making the
2311 * overhead only ~96k, this is unlikely.
2313 error_report("%s: Unable to allocate %#zx bytes of "
2314 "virtual address space", image_name,
2315 (size_t)(hiaddr - loaddr));
2316 exit(EXIT_FAILURE);
2319 guest_base = addr;
2322 static void pgb_dynamic(const char *image_name, long align)
2325 * The executable is dynamic and does not require a fixed address.
2326 * All we need is a commpage that satisfies align.
2327 * If we do not need a commpage, leave guest_base == 0.
2329 if (ARM_COMMPAGE) {
2330 uintptr_t addr, commpage;
2332 /* 64-bit hosts should have used reserved_va. */
2333 assert(sizeof(uintptr_t) == 4);
2336 * By putting the commpage at the first hole, that puts guest_base
2337 * just above that, and maximises the positive guest addresses.
2339 commpage = ARM_COMMPAGE & -align;
2340 addr = pgb_find_hole(commpage, -commpage, align, 0);
2341 assert(addr != -1);
2342 guest_base = addr;
2346 static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
2347 abi_ulong guest_hiaddr, long align)
2349 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2350 void *addr, *test;
2352 if (guest_hiaddr > reserved_va) {
2353 error_report("%s: requires more than reserved virtual "
2354 "address space (0x%" PRIx64 " > 0x%lx)",
2355 image_name, (uint64_t)guest_hiaddr, reserved_va);
2356 exit(EXIT_FAILURE);
2359 /* Widen the "image" to the entire reserved address space. */
2360 pgb_static(image_name, 0, reserved_va, align);
2362 /* osdep.h defines this as 0 if it's missing */
2363 flags |= MAP_FIXED_NOREPLACE;
2365 /* Reserve the memory on the host. */
2366 assert(guest_base != 0);
2367 test = g2h(0);
2368 addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0);
2369 if (addr == MAP_FAILED || addr != test) {
2370 error_report("Unable to reserve 0x%lx bytes of virtual address "
2371 "space at %p (%s) for use as guest address space (check your"
2372 "virtual memory ulimit setting, min_mmap_addr or reserve less "
2373 "using -R option)", reserved_va, test, strerror(errno));
2374 exit(EXIT_FAILURE);
2378 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
2379 abi_ulong guest_hiaddr)
2381 /* In order to use host shmat, we must be able to honor SHMLBA. */
2382 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
2384 if (have_guest_base) {
2385 pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align);
2386 } else if (reserved_va) {
2387 pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align);
2388 } else if (guest_loaddr) {
2389 pgb_static(image_name, guest_loaddr, guest_hiaddr, align);
2390 } else {
2391 pgb_dynamic(image_name, align);
2394 /* Reserve and initialize the commpage. */
2395 if (!init_guest_commpage()) {
2397 * With have_guest_base, the user has selected the address and
2398 * we are trying to work with that. Otherwise, we have selected
2399 * free space and init_guest_commpage must succeeded.
2401 assert(have_guest_base);
2402 pgb_fail_in_use(image_name);
2405 assert(QEMU_IS_ALIGNED(guest_base, align));
2406 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
2407 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
2410 enum {
2411 /* The string "GNU\0" as a magic number. */
2412 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
2413 NOTE_DATA_SZ = 1 * KiB,
2414 NOTE_NAME_SZ = 4,
2415 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
2419 * Process a single gnu_property entry.
2420 * Return false for error.
2422 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
2423 struct image_info *info, bool have_prev_type,
2424 uint32_t *prev_type, Error **errp)
2426 uint32_t pr_type, pr_datasz, step;
2428 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
2429 goto error_data;
2431 datasz -= *off;
2432 data += *off / sizeof(uint32_t);
2434 if (datasz < 2 * sizeof(uint32_t)) {
2435 goto error_data;
2437 pr_type = data[0];
2438 pr_datasz = data[1];
2439 data += 2;
2440 datasz -= 2 * sizeof(uint32_t);
2441 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
2442 if (step > datasz) {
2443 goto error_data;
2446 /* Properties are supposed to be unique and sorted on pr_type. */
2447 if (have_prev_type && pr_type <= *prev_type) {
2448 if (pr_type == *prev_type) {
2449 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
2450 } else {
2451 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
2453 return false;
2455 *prev_type = pr_type;
2457 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
2458 return false;
2461 *off += 2 * sizeof(uint32_t) + step;
2462 return true;
2464 error_data:
2465 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
2466 return false;
2469 /* Process NT_GNU_PROPERTY_TYPE_0. */
2470 static bool parse_elf_properties(int image_fd,
2471 struct image_info *info,
2472 const struct elf_phdr *phdr,
2473 char bprm_buf[BPRM_BUF_SIZE],
2474 Error **errp)
2476 union {
2477 struct elf_note nhdr;
2478 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
2479 } note;
2481 int n, off, datasz;
2482 bool have_prev_type;
2483 uint32_t prev_type;
2485 /* Unless the arch requires properties, ignore them. */
2486 if (!ARCH_USE_GNU_PROPERTY) {
2487 return true;
2490 /* If the properties are crazy large, that's too bad. */
2491 n = phdr->p_filesz;
2492 if (n > sizeof(note)) {
2493 error_setg(errp, "PT_GNU_PROPERTY too large");
2494 return false;
2496 if (n < sizeof(note.nhdr)) {
2497 error_setg(errp, "PT_GNU_PROPERTY too small");
2498 return false;
2501 if (phdr->p_offset + n <= BPRM_BUF_SIZE) {
2502 memcpy(&note, bprm_buf + phdr->p_offset, n);
2503 } else {
2504 ssize_t len = pread(image_fd, &note, n, phdr->p_offset);
2505 if (len != n) {
2506 error_setg_errno(errp, errno, "Error reading file header");
2507 return false;
2512 * The contents of a valid PT_GNU_PROPERTY is a sequence
2513 * of uint32_t -- swap them all now.
2515 #ifdef BSWAP_NEEDED
2516 for (int i = 0; i < n / 4; i++) {
2517 bswap32s(note.data + i);
2519 #endif
2522 * Note that nhdr is 3 words, and that the "name" described by namesz
2523 * immediately follows nhdr and is thus at the 4th word. Further, all
2524 * of the inputs to the kernel's round_up are multiples of 4.
2526 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
2527 note.nhdr.n_namesz != NOTE_NAME_SZ ||
2528 note.data[3] != GNU0_MAGIC) {
2529 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
2530 return false;
2532 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
2534 datasz = note.nhdr.n_descsz + off;
2535 if (datasz > n) {
2536 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
2537 return false;
2540 have_prev_type = false;
2541 prev_type = 0;
2542 while (1) {
2543 if (off == datasz) {
2544 return true; /* end, exit ok */
2546 if (!parse_elf_property(note.data, &off, datasz, info,
2547 have_prev_type, &prev_type, errp)) {
2548 return false;
2550 have_prev_type = true;
2554 /* Load an ELF image into the address space.
2556 IMAGE_NAME is the filename of the image, to use in error messages.
2557 IMAGE_FD is the open file descriptor for the image.
2559 BPRM_BUF is a copy of the beginning of the file; this of course
2560 contains the elf file header at offset 0. It is assumed that this
2561 buffer is sufficiently aligned to present no problems to the host
2562 in accessing data at aligned offsets within the buffer.
2564 On return: INFO values will be filled in, as necessary or available. */
2566 static void load_elf_image(const char *image_name, int image_fd,
2567 struct image_info *info, char **pinterp_name,
2568 char bprm_buf[BPRM_BUF_SIZE])
2570 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2571 struct elf_phdr *phdr;
2572 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
2573 int i, retval, prot_exec;
2574 Error *err = NULL;
2576 /* First of all, some simple consistency checks */
2577 if (!elf_check_ident(ehdr)) {
2578 error_setg(&err, "Invalid ELF image for this architecture");
2579 goto exit_errmsg;
2581 bswap_ehdr(ehdr);
2582 if (!elf_check_ehdr(ehdr)) {
2583 error_setg(&err, "Invalid ELF image for this architecture");
2584 goto exit_errmsg;
2587 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2588 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2589 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
2590 } else {
2591 phdr = (struct elf_phdr *) alloca(i);
2592 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
2593 if (retval != i) {
2594 goto exit_read;
2597 bswap_phdr(phdr, ehdr->e_phnum);
2599 info->nsegs = 0;
2600 info->pt_dynamic_addr = 0;
2602 mmap_lock();
2605 * Find the maximum size of the image and allocate an appropriate
2606 * amount of memory to handle that. Locate the interpreter, if any.
2608 loaddr = -1, hiaddr = 0;
2609 info->alignment = 0;
2610 for (i = 0; i < ehdr->e_phnum; ++i) {
2611 struct elf_phdr *eppnt = phdr + i;
2612 if (eppnt->p_type == PT_LOAD) {
2613 abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
2614 if (a < loaddr) {
2615 loaddr = a;
2617 a = eppnt->p_vaddr + eppnt->p_memsz;
2618 if (a > hiaddr) {
2619 hiaddr = a;
2621 ++info->nsegs;
2622 info->alignment |= eppnt->p_align;
2623 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2624 g_autofree char *interp_name = NULL;
2626 if (*pinterp_name) {
2627 error_setg(&err, "Multiple PT_INTERP entries");
2628 goto exit_errmsg;
2631 interp_name = g_malloc(eppnt->p_filesz);
2633 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2634 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2635 eppnt->p_filesz);
2636 } else {
2637 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2638 eppnt->p_offset);
2639 if (retval != eppnt->p_filesz) {
2640 goto exit_read;
2643 if (interp_name[eppnt->p_filesz - 1] != 0) {
2644 error_setg(&err, "Invalid PT_INTERP entry");
2645 goto exit_errmsg;
2647 *pinterp_name = g_steal_pointer(&interp_name);
2648 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
2649 if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &err)) {
2650 goto exit_errmsg;
2655 if (pinterp_name != NULL) {
2657 * This is the main executable.
2659 * Reserve extra space for brk.
2660 * We hold on to this space while placing the interpreter
2661 * and the stack, lest they be placed immediately after
2662 * the data segment and block allocation from the brk.
2664 * 16MB is chosen as "large enough" without being so large
2665 * as to allow the result to not fit with a 32-bit guest on
2666 * a 32-bit host.
2668 info->reserve_brk = 16 * MiB;
2669 hiaddr += info->reserve_brk;
2671 if (ehdr->e_type == ET_EXEC) {
2673 * Make sure that the low address does not conflict with
2674 * MMAP_MIN_ADDR or the QEMU application itself.
2676 probe_guest_base(image_name, loaddr, hiaddr);
2677 } else {
2679 * The binary is dynamic, but we still need to
2680 * select guest_base. In this case we pass a size.
2682 probe_guest_base(image_name, 0, hiaddr - loaddr);
2687 * Reserve address space for all of this.
2689 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
2690 * exactly the address range that is required.
2692 * Otherwise this is ET_DYN, and we are searching for a location
2693 * that can hold the memory space required. If the image is
2694 * pre-linked, LOADDR will be non-zero, and the kernel should
2695 * honor that address if it happens to be free.
2697 * In both cases, we will overwrite pages in this range with mappings
2698 * from the executable.
2700 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2701 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
2702 (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0),
2703 -1, 0);
2704 if (load_addr == -1) {
2705 goto exit_mmap;
2707 load_bias = load_addr - loaddr;
2709 if (elf_is_fdpic(ehdr)) {
2710 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2711 g_malloc(sizeof(*loadsegs) * info->nsegs);
2713 for (i = 0; i < ehdr->e_phnum; ++i) {
2714 switch (phdr[i].p_type) {
2715 case PT_DYNAMIC:
2716 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2717 break;
2718 case PT_LOAD:
2719 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2720 loadsegs->p_vaddr = phdr[i].p_vaddr;
2721 loadsegs->p_memsz = phdr[i].p_memsz;
2722 ++loadsegs;
2723 break;
2728 info->load_bias = load_bias;
2729 info->code_offset = load_bias;
2730 info->data_offset = load_bias;
2731 info->load_addr = load_addr;
2732 info->entry = ehdr->e_entry + load_bias;
2733 info->start_code = -1;
2734 info->end_code = 0;
2735 info->start_data = -1;
2736 info->end_data = 0;
2737 info->brk = 0;
2738 info->elf_flags = ehdr->e_flags;
2740 prot_exec = PROT_EXEC;
2741 #ifdef TARGET_AARCH64
2743 * If the BTI feature is present, this indicates that the executable
2744 * pages of the startup binary should be mapped with PROT_BTI, so that
2745 * branch targets are enforced.
2747 * The startup binary is either the interpreter or the static executable.
2748 * The interpreter is responsible for all pages of a dynamic executable.
2750 * Elf notes are backward compatible to older cpus.
2751 * Do not enable BTI unless it is supported.
2753 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
2754 && (pinterp_name == NULL || *pinterp_name == 0)
2755 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
2756 prot_exec |= TARGET_PROT_BTI;
2758 #endif
2760 for (i = 0; i < ehdr->e_phnum; i++) {
2761 struct elf_phdr *eppnt = phdr + i;
2762 if (eppnt->p_type == PT_LOAD) {
2763 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
2764 int elf_prot = 0;
2766 if (eppnt->p_flags & PF_R) {
2767 elf_prot |= PROT_READ;
2769 if (eppnt->p_flags & PF_W) {
2770 elf_prot |= PROT_WRITE;
2772 if (eppnt->p_flags & PF_X) {
2773 elf_prot |= prot_exec;
2776 vaddr = load_bias + eppnt->p_vaddr;
2777 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2778 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2779 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
2782 * Some segments may be completely empty without any backing file
2783 * segment, in that case just let zero_bss allocate an empty buffer
2784 * for it.
2786 if (eppnt->p_filesz != 0) {
2787 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2788 MAP_PRIVATE | MAP_FIXED,
2789 image_fd, eppnt->p_offset - vaddr_po);
2791 if (error == -1) {
2792 goto exit_mmap;
2796 vaddr_ef = vaddr + eppnt->p_filesz;
2797 vaddr_em = vaddr + eppnt->p_memsz;
2799 /* If the load segment requests extra zeros (e.g. bss), map it. */
2800 if (vaddr_ef < vaddr_em) {
2801 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2804 /* Find the full program boundaries. */
2805 if (elf_prot & PROT_EXEC) {
2806 if (vaddr < info->start_code) {
2807 info->start_code = vaddr;
2809 if (vaddr_ef > info->end_code) {
2810 info->end_code = vaddr_ef;
2813 if (elf_prot & PROT_WRITE) {
2814 if (vaddr < info->start_data) {
2815 info->start_data = vaddr;
2817 if (vaddr_ef > info->end_data) {
2818 info->end_data = vaddr_ef;
2821 if (vaddr_em > info->brk) {
2822 info->brk = vaddr_em;
2824 #ifdef TARGET_MIPS
2825 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
2826 Mips_elf_abiflags_v0 abiflags;
2827 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
2828 error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry");
2829 goto exit_errmsg;
2831 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2832 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
2833 sizeof(Mips_elf_abiflags_v0));
2834 } else {
2835 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
2836 eppnt->p_offset);
2837 if (retval != sizeof(Mips_elf_abiflags_v0)) {
2838 goto exit_read;
2841 bswap_mips_abiflags(&abiflags);
2842 info->fp_abi = abiflags.fp_abi;
2843 #endif
2847 if (info->end_data == 0) {
2848 info->start_data = info->end_code;
2849 info->end_data = info->end_code;
2852 if (qemu_log_enabled()) {
2853 load_symbols(ehdr, image_fd, load_bias);
2856 mmap_unlock();
2858 close(image_fd);
2859 return;
2861 exit_read:
2862 if (retval >= 0) {
2863 error_setg(&err, "Incomplete read of file header");
2864 } else {
2865 error_setg_errno(&err, errno, "Error reading file header");
2867 goto exit_errmsg;
2868 exit_mmap:
2869 error_setg_errno(&err, errno, "Error mapping file");
2870 goto exit_errmsg;
2871 exit_errmsg:
2872 error_reportf_err(err, "%s: ", image_name);
2873 exit(-1);
2876 static void load_elf_interp(const char *filename, struct image_info *info,
2877 char bprm_buf[BPRM_BUF_SIZE])
2879 int fd, retval;
2880 Error *err = NULL;
2882 fd = open(path(filename), O_RDONLY);
2883 if (fd < 0) {
2884 error_setg_file_open(&err, errno, filename);
2885 error_report_err(err);
2886 exit(-1);
2889 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2890 if (retval < 0) {
2891 error_setg_errno(&err, errno, "Error reading file header");
2892 error_reportf_err(err, "%s: ", filename);
2893 exit(-1);
2896 if (retval < BPRM_BUF_SIZE) {
2897 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2900 load_elf_image(filename, fd, info, NULL, bprm_buf);
2903 static int symfind(const void *s0, const void *s1)
2905 target_ulong addr = *(target_ulong *)s0;
2906 struct elf_sym *sym = (struct elf_sym *)s1;
2907 int result = 0;
2908 if (addr < sym->st_value) {
2909 result = -1;
2910 } else if (addr >= sym->st_value + sym->st_size) {
2911 result = 1;
2913 return result;
2916 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2918 #if ELF_CLASS == ELFCLASS32
2919 struct elf_sym *syms = s->disas_symtab.elf32;
2920 #else
2921 struct elf_sym *syms = s->disas_symtab.elf64;
2922 #endif
2924 // binary search
2925 struct elf_sym *sym;
2927 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2928 if (sym != NULL) {
2929 return s->disas_strtab + sym->st_name;
2932 return "";
2935 /* FIXME: This should use elf_ops.h */
2936 static int symcmp(const void *s0, const void *s1)
2938 struct elf_sym *sym0 = (struct elf_sym *)s0;
2939 struct elf_sym *sym1 = (struct elf_sym *)s1;
2940 return (sym0->st_value < sym1->st_value)
2941 ? -1
2942 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2945 /* Best attempt to load symbols from this ELF object. */
2946 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2948 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2949 uint64_t segsz;
2950 struct elf_shdr *shdr;
2951 char *strings = NULL;
2952 struct syminfo *s = NULL;
2953 struct elf_sym *new_syms, *syms = NULL;
2955 shnum = hdr->e_shnum;
2956 i = shnum * sizeof(struct elf_shdr);
2957 shdr = (struct elf_shdr *)alloca(i);
2958 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2959 return;
2962 bswap_shdr(shdr, shnum);
2963 for (i = 0; i < shnum; ++i) {
2964 if (shdr[i].sh_type == SHT_SYMTAB) {
2965 sym_idx = i;
2966 str_idx = shdr[i].sh_link;
2967 goto found;
2971 /* There will be no symbol table if the file was stripped. */
2972 return;
2974 found:
2975 /* Now know where the strtab and symtab are. Snarf them. */
2976 s = g_try_new(struct syminfo, 1);
2977 if (!s) {
2978 goto give_up;
2981 segsz = shdr[str_idx].sh_size;
2982 s->disas_strtab = strings = g_try_malloc(segsz);
2983 if (!strings ||
2984 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
2985 goto give_up;
2988 segsz = shdr[sym_idx].sh_size;
2989 syms = g_try_malloc(segsz);
2990 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
2991 goto give_up;
2994 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
2995 /* Implausibly large symbol table: give up rather than ploughing
2996 * on with the number of symbols calculation overflowing
2998 goto give_up;
3000 nsyms = segsz / sizeof(struct elf_sym);
3001 for (i = 0; i < nsyms; ) {
3002 bswap_sym(syms + i);
3003 /* Throw away entries which we do not need. */
3004 if (syms[i].st_shndx == SHN_UNDEF
3005 || syms[i].st_shndx >= SHN_LORESERVE
3006 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3007 if (i < --nsyms) {
3008 syms[i] = syms[nsyms];
3010 } else {
3011 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3012 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3013 syms[i].st_value &= ~(target_ulong)1;
3014 #endif
3015 syms[i].st_value += load_bias;
3016 i++;
3020 /* No "useful" symbol. */
3021 if (nsyms == 0) {
3022 goto give_up;
3025 /* Attempt to free the storage associated with the local symbols
3026 that we threw away. Whether or not this has any effect on the
3027 memory allocation depends on the malloc implementation and how
3028 many symbols we managed to discard. */
3029 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3030 if (new_syms == NULL) {
3031 goto give_up;
3033 syms = new_syms;
3035 qsort(syms, nsyms, sizeof(*syms), symcmp);
3037 s->disas_num_syms = nsyms;
3038 #if ELF_CLASS == ELFCLASS32
3039 s->disas_symtab.elf32 = syms;
3040 #else
3041 s->disas_symtab.elf64 = syms;
3042 #endif
3043 s->lookup_symbol = lookup_symbolxx;
3044 s->next = syminfos;
3045 syminfos = s;
3047 return;
3049 give_up:
3050 g_free(s);
3051 g_free(strings);
3052 g_free(syms);
3055 uint32_t get_elf_eflags(int fd)
3057 struct elfhdr ehdr;
3058 off_t offset;
3059 int ret;
3061 /* Read ELF header */
3062 offset = lseek(fd, 0, SEEK_SET);
3063 if (offset == (off_t) -1) {
3064 return 0;
3066 ret = read(fd, &ehdr, sizeof(ehdr));
3067 if (ret < sizeof(ehdr)) {
3068 return 0;
3070 offset = lseek(fd, offset, SEEK_SET);
3071 if (offset == (off_t) -1) {
3072 return 0;
3075 /* Check ELF signature */
3076 if (!elf_check_ident(&ehdr)) {
3077 return 0;
3080 /* check header */
3081 bswap_ehdr(&ehdr);
3082 if (!elf_check_ehdr(&ehdr)) {
3083 return 0;
3086 /* return architecture id */
3087 return ehdr.e_flags;
3090 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3092 struct image_info interp_info;
3093 struct elfhdr elf_ex;
3094 char *elf_interpreter = NULL;
3095 char *scratch;
3097 memset(&interp_info, 0, sizeof(interp_info));
3098 #ifdef TARGET_MIPS
3099 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3100 #endif
3102 info->start_mmap = (abi_ulong)ELF_START_MMAP;
3104 load_elf_image(bprm->filename, bprm->fd, info,
3105 &elf_interpreter, bprm->buf);
3107 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3108 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3109 when we load the interpreter. */
3110 elf_ex = *(struct elfhdr *)bprm->buf;
3112 /* Do this so that we can load the interpreter, if need be. We will
3113 change some of these later */
3114 bprm->p = setup_arg_pages(bprm, info);
3116 scratch = g_new0(char, TARGET_PAGE_SIZE);
3117 if (STACK_GROWS_DOWN) {
3118 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3119 bprm->p, info->stack_limit);
3120 info->file_string = bprm->p;
3121 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3122 bprm->p, info->stack_limit);
3123 info->env_strings = bprm->p;
3124 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3125 bprm->p, info->stack_limit);
3126 info->arg_strings = bprm->p;
3127 } else {
3128 info->arg_strings = bprm->p;
3129 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3130 bprm->p, info->stack_limit);
3131 info->env_strings = bprm->p;
3132 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3133 bprm->p, info->stack_limit);
3134 info->file_string = bprm->p;
3135 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3136 bprm->p, info->stack_limit);
3139 g_free(scratch);
3141 if (!bprm->p) {
3142 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3143 exit(-1);
3146 if (elf_interpreter) {
3147 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3149 /* If the program interpreter is one of these two, then assume
3150 an iBCS2 image. Otherwise assume a native linux image. */
3152 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3153 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3154 info->personality = PER_SVR4;
3156 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3157 and some applications "depend" upon this behavior. Since
3158 we do not have the power to recompile these, we emulate
3159 the SVr4 behavior. Sigh. */
3160 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
3161 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3163 #ifdef TARGET_MIPS
3164 info->interp_fp_abi = interp_info.fp_abi;
3165 #endif
3168 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
3169 info, (elf_interpreter ? &interp_info : NULL));
3170 info->start_stack = bprm->p;
3172 /* If we have an interpreter, set that as the program's entry point.
3173 Copy the load_bias as well, to help PPC64 interpret the entry
3174 point as a function descriptor. Do this after creating elf tables
3175 so that we copy the original program entry point into the AUXV. */
3176 if (elf_interpreter) {
3177 info->load_bias = interp_info.load_bias;
3178 info->entry = interp_info.entry;
3179 g_free(elf_interpreter);
3182 #ifdef USE_ELF_CORE_DUMP
3183 bprm->core_dump = &elf_core_dump;
3184 #endif
3187 * If we reserved extra space for brk, release it now.
3188 * The implementation of do_brk in syscalls.c expects to be able
3189 * to mmap pages in this space.
3191 if (info->reserve_brk) {
3192 abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk);
3193 abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk);
3194 target_munmap(start_brk, end_brk - start_brk);
3197 return 0;
3200 #ifdef USE_ELF_CORE_DUMP
3202 * Definitions to generate Intel SVR4-like core files.
3203 * These mostly have the same names as the SVR4 types with "target_elf_"
3204 * tacked on the front to prevent clashes with linux definitions,
3205 * and the typedef forms have been avoided. This is mostly like
3206 * the SVR4 structure, but more Linuxy, with things that Linux does
3207 * not support and which gdb doesn't really use excluded.
3209 * Fields we don't dump (their contents is zero) in linux-user qemu
3210 * are marked with XXX.
3212 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3214 * Porting ELF coredump for target is (quite) simple process. First you
3215 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3216 * the target resides):
3218 * #define USE_ELF_CORE_DUMP
3220 * Next you define type of register set used for dumping. ELF specification
3221 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3223 * typedef <target_regtype> target_elf_greg_t;
3224 * #define ELF_NREG <number of registers>
3225 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3227 * Last step is to implement target specific function that copies registers
3228 * from given cpu into just specified register set. Prototype is:
3230 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3231 * const CPUArchState *env);
3233 * Parameters:
3234 * regs - copy register values into here (allocated and zeroed by caller)
3235 * env - copy registers from here
3237 * Example for ARM target is provided in this file.
3240 /* An ELF note in memory */
3241 struct memelfnote {
3242 const char *name;
3243 size_t namesz;
3244 size_t namesz_rounded;
3245 int type;
3246 size_t datasz;
3247 size_t datasz_rounded;
3248 void *data;
3249 size_t notesz;
3252 struct target_elf_siginfo {
3253 abi_int si_signo; /* signal number */
3254 abi_int si_code; /* extra code */
3255 abi_int si_errno; /* errno */
3258 struct target_elf_prstatus {
3259 struct target_elf_siginfo pr_info; /* Info associated with signal */
3260 abi_short pr_cursig; /* Current signal */
3261 abi_ulong pr_sigpend; /* XXX */
3262 abi_ulong pr_sighold; /* XXX */
3263 target_pid_t pr_pid;
3264 target_pid_t pr_ppid;
3265 target_pid_t pr_pgrp;
3266 target_pid_t pr_sid;
3267 struct target_timeval pr_utime; /* XXX User time */
3268 struct target_timeval pr_stime; /* XXX System time */
3269 struct target_timeval pr_cutime; /* XXX Cumulative user time */
3270 struct target_timeval pr_cstime; /* XXX Cumulative system time */
3271 target_elf_gregset_t pr_reg; /* GP registers */
3272 abi_int pr_fpvalid; /* XXX */
3275 #define ELF_PRARGSZ (80) /* Number of chars for args */
3277 struct target_elf_prpsinfo {
3278 char pr_state; /* numeric process state */
3279 char pr_sname; /* char for pr_state */
3280 char pr_zomb; /* zombie */
3281 char pr_nice; /* nice val */
3282 abi_ulong pr_flag; /* flags */
3283 target_uid_t pr_uid;
3284 target_gid_t pr_gid;
3285 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
3286 /* Lots missing */
3287 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
3288 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
3291 /* Here is the structure in which status of each thread is captured. */
3292 struct elf_thread_status {
3293 QTAILQ_ENTRY(elf_thread_status) ets_link;
3294 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
3295 #if 0
3296 elf_fpregset_t fpu; /* NT_PRFPREG */
3297 struct task_struct *thread;
3298 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
3299 #endif
3300 struct memelfnote notes[1];
3301 int num_notes;
3304 struct elf_note_info {
3305 struct memelfnote *notes;
3306 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
3307 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
3309 QTAILQ_HEAD(, elf_thread_status) thread_list;
3310 #if 0
3312 * Current version of ELF coredump doesn't support
3313 * dumping fp regs etc.
3315 elf_fpregset_t *fpu;
3316 elf_fpxregset_t *xfpu;
3317 int thread_status_size;
3318 #endif
3319 int notes_size;
3320 int numnote;
3323 struct vm_area_struct {
3324 target_ulong vma_start; /* start vaddr of memory region */
3325 target_ulong vma_end; /* end vaddr of memory region */
3326 abi_ulong vma_flags; /* protection etc. flags for the region */
3327 QTAILQ_ENTRY(vm_area_struct) vma_link;
3330 struct mm_struct {
3331 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
3332 int mm_count; /* number of mappings */
3335 static struct mm_struct *vma_init(void);
3336 static void vma_delete(struct mm_struct *);
3337 static int vma_add_mapping(struct mm_struct *, target_ulong,
3338 target_ulong, abi_ulong);
3339 static int vma_get_mapping_count(const struct mm_struct *);
3340 static struct vm_area_struct *vma_first(const struct mm_struct *);
3341 static struct vm_area_struct *vma_next(struct vm_area_struct *);
3342 static abi_ulong vma_dump_size(const struct vm_area_struct *);
3343 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3344 unsigned long flags);
3346 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
3347 static void fill_note(struct memelfnote *, const char *, int,
3348 unsigned int, void *);
3349 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
3350 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
3351 static void fill_auxv_note(struct memelfnote *, const TaskState *);
3352 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
3353 static size_t note_size(const struct memelfnote *);
3354 static void free_note_info(struct elf_note_info *);
3355 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
3356 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
3357 static int core_dump_filename(const TaskState *, char *, size_t);
3359 static int dump_write(int, const void *, size_t);
3360 static int write_note(struct memelfnote *, int);
3361 static int write_note_info(struct elf_note_info *, int);
3363 #ifdef BSWAP_NEEDED
3364 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
3366 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3367 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3368 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
3369 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
3370 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3371 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
3372 prstatus->pr_pid = tswap32(prstatus->pr_pid);
3373 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3374 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3375 prstatus->pr_sid = tswap32(prstatus->pr_sid);
3376 /* cpu times are not filled, so we skip them */
3377 /* regs should be in correct format already */
3378 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3381 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
3383 psinfo->pr_flag = tswapal(psinfo->pr_flag);
3384 psinfo->pr_uid = tswap16(psinfo->pr_uid);
3385 psinfo->pr_gid = tswap16(psinfo->pr_gid);
3386 psinfo->pr_pid = tswap32(psinfo->pr_pid);
3387 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
3388 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
3389 psinfo->pr_sid = tswap32(psinfo->pr_sid);
3392 static void bswap_note(struct elf_note *en)
3394 bswap32s(&en->n_namesz);
3395 bswap32s(&en->n_descsz);
3396 bswap32s(&en->n_type);
3398 #else
3399 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
3400 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
3401 static inline void bswap_note(struct elf_note *en) { }
3402 #endif /* BSWAP_NEEDED */
3405 * Minimal support for linux memory regions. These are needed
3406 * when we are finding out what memory exactly belongs to
3407 * emulated process. No locks needed here, as long as
3408 * thread that received the signal is stopped.
3411 static struct mm_struct *vma_init(void)
3413 struct mm_struct *mm;
3415 if ((mm = g_malloc(sizeof (*mm))) == NULL)
3416 return (NULL);
3418 mm->mm_count = 0;
3419 QTAILQ_INIT(&mm->mm_mmap);
3421 return (mm);
3424 static void vma_delete(struct mm_struct *mm)
3426 struct vm_area_struct *vma;
3428 while ((vma = vma_first(mm)) != NULL) {
3429 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
3430 g_free(vma);
3432 g_free(mm);
3435 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3436 target_ulong end, abi_ulong flags)
3438 struct vm_area_struct *vma;
3440 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
3441 return (-1);
3443 vma->vma_start = start;
3444 vma->vma_end = end;
3445 vma->vma_flags = flags;
3447 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
3448 mm->mm_count++;
3450 return (0);
3453 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3455 return (QTAILQ_FIRST(&mm->mm_mmap));
3458 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3460 return (QTAILQ_NEXT(vma, vma_link));
3463 static int vma_get_mapping_count(const struct mm_struct *mm)
3465 return (mm->mm_count);
3469 * Calculate file (dump) size of given memory region.
3471 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3473 /* if we cannot even read the first page, skip it */
3474 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
3475 return (0);
3478 * Usually we don't dump executable pages as they contain
3479 * non-writable code that debugger can read directly from
3480 * target library etc. However, thread stacks are marked
3481 * also executable so we read in first page of given region
3482 * and check whether it contains elf header. If there is
3483 * no elf header, we dump it.
3485 if (vma->vma_flags & PROT_EXEC) {
3486 char page[TARGET_PAGE_SIZE];
3488 copy_from_user(page, vma->vma_start, sizeof (page));
3489 if ((page[EI_MAG0] == ELFMAG0) &&
3490 (page[EI_MAG1] == ELFMAG1) &&
3491 (page[EI_MAG2] == ELFMAG2) &&
3492 (page[EI_MAG3] == ELFMAG3)) {
3494 * Mappings are possibly from ELF binary. Don't dump
3495 * them.
3497 return (0);
3501 return (vma->vma_end - vma->vma_start);
3504 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3505 unsigned long flags)
3507 struct mm_struct *mm = (struct mm_struct *)priv;
3509 vma_add_mapping(mm, start, end, flags);
3510 return (0);
3513 static void fill_note(struct memelfnote *note, const char *name, int type,
3514 unsigned int sz, void *data)
3516 unsigned int namesz;
3518 namesz = strlen(name) + 1;
3519 note->name = name;
3520 note->namesz = namesz;
3521 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3522 note->type = type;
3523 note->datasz = sz;
3524 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3526 note->data = data;
3529 * We calculate rounded up note size here as specified by
3530 * ELF document.
3532 note->notesz = sizeof (struct elf_note) +
3533 note->namesz_rounded + note->datasz_rounded;
3536 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
3537 uint32_t flags)
3539 (void) memset(elf, 0, sizeof(*elf));
3541 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
3542 elf->e_ident[EI_CLASS] = ELF_CLASS;
3543 elf->e_ident[EI_DATA] = ELF_DATA;
3544 elf->e_ident[EI_VERSION] = EV_CURRENT;
3545 elf->e_ident[EI_OSABI] = ELF_OSABI;
3547 elf->e_type = ET_CORE;
3548 elf->e_machine = machine;
3549 elf->e_version = EV_CURRENT;
3550 elf->e_phoff = sizeof(struct elfhdr);
3551 elf->e_flags = flags;
3552 elf->e_ehsize = sizeof(struct elfhdr);
3553 elf->e_phentsize = sizeof(struct elf_phdr);
3554 elf->e_phnum = segs;
3556 bswap_ehdr(elf);
3559 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
3561 phdr->p_type = PT_NOTE;
3562 phdr->p_offset = offset;
3563 phdr->p_vaddr = 0;
3564 phdr->p_paddr = 0;
3565 phdr->p_filesz = sz;
3566 phdr->p_memsz = 0;
3567 phdr->p_flags = 0;
3568 phdr->p_align = 0;
3570 bswap_phdr(phdr, 1);
3573 static size_t note_size(const struct memelfnote *note)
3575 return (note->notesz);
3578 static void fill_prstatus(struct target_elf_prstatus *prstatus,
3579 const TaskState *ts, int signr)
3581 (void) memset(prstatus, 0, sizeof (*prstatus));
3582 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
3583 prstatus->pr_pid = ts->ts_tid;
3584 prstatus->pr_ppid = getppid();
3585 prstatus->pr_pgrp = getpgrp();
3586 prstatus->pr_sid = getsid(0);
3588 bswap_prstatus(prstatus);
3591 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
3593 char *base_filename;
3594 unsigned int i, len;
3596 (void) memset(psinfo, 0, sizeof (*psinfo));
3598 len = ts->info->arg_end - ts->info->arg_start;
3599 if (len >= ELF_PRARGSZ)
3600 len = ELF_PRARGSZ - 1;
3601 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
3602 return -EFAULT;
3603 for (i = 0; i < len; i++)
3604 if (psinfo->pr_psargs[i] == 0)
3605 psinfo->pr_psargs[i] = ' ';
3606 psinfo->pr_psargs[len] = 0;
3608 psinfo->pr_pid = getpid();
3609 psinfo->pr_ppid = getppid();
3610 psinfo->pr_pgrp = getpgrp();
3611 psinfo->pr_sid = getsid(0);
3612 psinfo->pr_uid = getuid();
3613 psinfo->pr_gid = getgid();
3615 base_filename = g_path_get_basename(ts->bprm->filename);
3617 * Using strncpy here is fine: at max-length,
3618 * this field is not NUL-terminated.
3620 (void) strncpy(psinfo->pr_fname, base_filename,
3621 sizeof(psinfo->pr_fname));
3623 g_free(base_filename);
3624 bswap_psinfo(psinfo);
3625 return (0);
3628 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
3630 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
3631 elf_addr_t orig_auxv = auxv;
3632 void *ptr;
3633 int len = ts->info->auxv_len;
3636 * Auxiliary vector is stored in target process stack. It contains
3637 * {type, value} pairs that we need to dump into note. This is not
3638 * strictly necessary but we do it here for sake of completeness.
3641 /* read in whole auxv vector and copy it to memelfnote */
3642 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
3643 if (ptr != NULL) {
3644 fill_note(note, "CORE", NT_AUXV, len, ptr);
3645 unlock_user(ptr, auxv, len);
3650 * Constructs name of coredump file. We have following convention
3651 * for the name:
3652 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3654 * Returns 0 in case of success, -1 otherwise (errno is set).
3656 static int core_dump_filename(const TaskState *ts, char *buf,
3657 size_t bufsize)
3659 char timestamp[64];
3660 char *base_filename = NULL;
3661 struct timeval tv;
3662 struct tm tm;
3664 assert(bufsize >= PATH_MAX);
3666 if (gettimeofday(&tv, NULL) < 0) {
3667 (void) fprintf(stderr, "unable to get current timestamp: %s",
3668 strerror(errno));
3669 return (-1);
3672 base_filename = g_path_get_basename(ts->bprm->filename);
3673 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
3674 localtime_r(&tv.tv_sec, &tm));
3675 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
3676 base_filename, timestamp, (int)getpid());
3677 g_free(base_filename);
3679 return (0);
3682 static int dump_write(int fd, const void *ptr, size_t size)
3684 const char *bufp = (const char *)ptr;
3685 ssize_t bytes_written, bytes_left;
3686 struct rlimit dumpsize;
3687 off_t pos;
3689 bytes_written = 0;
3690 getrlimit(RLIMIT_CORE, &dumpsize);
3691 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
3692 if (errno == ESPIPE) { /* not a seekable stream */
3693 bytes_left = size;
3694 } else {
3695 return pos;
3697 } else {
3698 if (dumpsize.rlim_cur <= pos) {
3699 return -1;
3700 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
3701 bytes_left = size;
3702 } else {
3703 size_t limit_left=dumpsize.rlim_cur - pos;
3704 bytes_left = limit_left >= size ? size : limit_left ;
3709 * In normal conditions, single write(2) should do but
3710 * in case of socket etc. this mechanism is more portable.
3712 do {
3713 bytes_written = write(fd, bufp, bytes_left);
3714 if (bytes_written < 0) {
3715 if (errno == EINTR)
3716 continue;
3717 return (-1);
3718 } else if (bytes_written == 0) { /* eof */
3719 return (-1);
3721 bufp += bytes_written;
3722 bytes_left -= bytes_written;
3723 } while (bytes_left > 0);
3725 return (0);
3728 static int write_note(struct memelfnote *men, int fd)
3730 struct elf_note en;
3732 en.n_namesz = men->namesz;
3733 en.n_type = men->type;
3734 en.n_descsz = men->datasz;
3736 bswap_note(&en);
3738 if (dump_write(fd, &en, sizeof(en)) != 0)
3739 return (-1);
3740 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3741 return (-1);
3742 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
3743 return (-1);
3745 return (0);
3748 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
3750 CPUState *cpu = env_cpu((CPUArchState *)env);
3751 TaskState *ts = (TaskState *)cpu->opaque;
3752 struct elf_thread_status *ets;
3754 ets = g_malloc0(sizeof (*ets));
3755 ets->num_notes = 1; /* only prstatus is dumped */
3756 fill_prstatus(&ets->prstatus, ts, 0);
3757 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3758 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
3759 &ets->prstatus);
3761 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
3763 info->notes_size += note_size(&ets->notes[0]);
3766 static void init_note_info(struct elf_note_info *info)
3768 /* Initialize the elf_note_info structure so that it is at
3769 * least safe to call free_note_info() on it. Must be
3770 * called before calling fill_note_info().
3772 memset(info, 0, sizeof (*info));
3773 QTAILQ_INIT(&info->thread_list);
3776 static int fill_note_info(struct elf_note_info *info,
3777 long signr, const CPUArchState *env)
3779 #define NUMNOTES 3
3780 CPUState *cpu = env_cpu((CPUArchState *)env);
3781 TaskState *ts = (TaskState *)cpu->opaque;
3782 int i;
3784 info->notes = g_new0(struct memelfnote, NUMNOTES);
3785 if (info->notes == NULL)
3786 return (-ENOMEM);
3787 info->prstatus = g_malloc0(sizeof (*info->prstatus));
3788 if (info->prstatus == NULL)
3789 return (-ENOMEM);
3790 info->psinfo = g_malloc0(sizeof (*info->psinfo));
3791 if (info->prstatus == NULL)
3792 return (-ENOMEM);
3795 * First fill in status (and registers) of current thread
3796 * including process info & aux vector.
3798 fill_prstatus(info->prstatus, ts, signr);
3799 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3800 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
3801 sizeof (*info->prstatus), info->prstatus);
3802 fill_psinfo(info->psinfo, ts);
3803 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
3804 sizeof (*info->psinfo), info->psinfo);
3805 fill_auxv_note(&info->notes[2], ts);
3806 info->numnote = 3;
3808 info->notes_size = 0;
3809 for (i = 0; i < info->numnote; i++)
3810 info->notes_size += note_size(&info->notes[i]);
3812 /* read and fill status of all threads */
3813 cpu_list_lock();
3814 CPU_FOREACH(cpu) {
3815 if (cpu == thread_cpu) {
3816 continue;
3818 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
3820 cpu_list_unlock();
3822 return (0);
3825 static void free_note_info(struct elf_note_info *info)
3827 struct elf_thread_status *ets;
3829 while (!QTAILQ_EMPTY(&info->thread_list)) {
3830 ets = QTAILQ_FIRST(&info->thread_list);
3831 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
3832 g_free(ets);
3835 g_free(info->prstatus);
3836 g_free(info->psinfo);
3837 g_free(info->notes);
3840 static int write_note_info(struct elf_note_info *info, int fd)
3842 struct elf_thread_status *ets;
3843 int i, error = 0;
3845 /* write prstatus, psinfo and auxv for current thread */
3846 for (i = 0; i < info->numnote; i++)
3847 if ((error = write_note(&info->notes[i], fd)) != 0)
3848 return (error);
3850 /* write prstatus for each thread */
3851 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
3852 if ((error = write_note(&ets->notes[0], fd)) != 0)
3853 return (error);
3856 return (0);
3860 * Write out ELF coredump.
3862 * See documentation of ELF object file format in:
3863 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3865 * Coredump format in linux is following:
3867 * 0 +----------------------+ \
3868 * | ELF header | ET_CORE |
3869 * +----------------------+ |
3870 * | ELF program headers | |--- headers
3871 * | - NOTE section | |
3872 * | - PT_LOAD sections | |
3873 * +----------------------+ /
3874 * | NOTEs: |
3875 * | - NT_PRSTATUS |
3876 * | - NT_PRSINFO |
3877 * | - NT_AUXV |
3878 * +----------------------+ <-- aligned to target page
3879 * | Process memory dump |
3880 * : :
3881 * . .
3882 * : :
3883 * | |
3884 * +----------------------+
3886 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3887 * NT_PRSINFO -> struct elf_prpsinfo
3888 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3890 * Format follows System V format as close as possible. Current
3891 * version limitations are as follows:
3892 * - no floating point registers are dumped
3894 * Function returns 0 in case of success, negative errno otherwise.
3896 * TODO: make this work also during runtime: it should be
3897 * possible to force coredump from running process and then
3898 * continue processing. For example qemu could set up SIGUSR2
3899 * handler (provided that target process haven't registered
3900 * handler for that) that does the dump when signal is received.
3902 static int elf_core_dump(int signr, const CPUArchState *env)
3904 const CPUState *cpu = env_cpu((CPUArchState *)env);
3905 const TaskState *ts = (const TaskState *)cpu->opaque;
3906 struct vm_area_struct *vma = NULL;
3907 char corefile[PATH_MAX];
3908 struct elf_note_info info;
3909 struct elfhdr elf;
3910 struct elf_phdr phdr;
3911 struct rlimit dumpsize;
3912 struct mm_struct *mm = NULL;
3913 off_t offset = 0, data_offset = 0;
3914 int segs = 0;
3915 int fd = -1;
3917 init_note_info(&info);
3919 errno = 0;
3920 getrlimit(RLIMIT_CORE, &dumpsize);
3921 if (dumpsize.rlim_cur == 0)
3922 return 0;
3924 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
3925 return (-errno);
3927 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3928 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3929 return (-errno);
3932 * Walk through target process memory mappings and
3933 * set up structure containing this information. After
3934 * this point vma_xxx functions can be used.
3936 if ((mm = vma_init()) == NULL)
3937 goto out;
3939 walk_memory_regions(mm, vma_walker);
3940 segs = vma_get_mapping_count(mm);
3943 * Construct valid coredump ELF header. We also
3944 * add one more segment for notes.
3946 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3947 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3948 goto out;
3950 /* fill in the in-memory version of notes */
3951 if (fill_note_info(&info, signr, env) < 0)
3952 goto out;
3954 offset += sizeof (elf); /* elf header */
3955 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3957 /* write out notes program header */
3958 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3960 offset += info.notes_size;
3961 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3962 goto out;
3965 * ELF specification wants data to start at page boundary so
3966 * we align it here.
3968 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3971 * Write program headers for memory regions mapped in
3972 * the target process.
3974 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3975 (void) memset(&phdr, 0, sizeof (phdr));
3977 phdr.p_type = PT_LOAD;
3978 phdr.p_offset = offset;
3979 phdr.p_vaddr = vma->vma_start;
3980 phdr.p_paddr = 0;
3981 phdr.p_filesz = vma_dump_size(vma);
3982 offset += phdr.p_filesz;
3983 phdr.p_memsz = vma->vma_end - vma->vma_start;
3984 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3985 if (vma->vma_flags & PROT_WRITE)
3986 phdr.p_flags |= PF_W;
3987 if (vma->vma_flags & PROT_EXEC)
3988 phdr.p_flags |= PF_X;
3989 phdr.p_align = ELF_EXEC_PAGESIZE;
3991 bswap_phdr(&phdr, 1);
3992 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
3993 goto out;
3998 * Next we write notes just after program headers. No
3999 * alignment needed here.
4001 if (write_note_info(&info, fd) < 0)
4002 goto out;
4004 /* align data to page boundary */
4005 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4006 goto out;
4009 * Finally we can dump process memory into corefile as well.
4011 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4012 abi_ulong addr;
4013 abi_ulong end;
4015 end = vma->vma_start + vma_dump_size(vma);
4017 for (addr = vma->vma_start; addr < end;
4018 addr += TARGET_PAGE_SIZE) {
4019 char page[TARGET_PAGE_SIZE];
4020 int error;
4023 * Read in page from target process memory and
4024 * write it to coredump file.
4026 error = copy_from_user(page, addr, sizeof (page));
4027 if (error != 0) {
4028 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
4029 addr);
4030 errno = -error;
4031 goto out;
4033 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4034 goto out;
4038 out:
4039 free_note_info(&info);
4040 if (mm != NULL)
4041 vma_delete(mm);
4042 (void) close(fd);
4044 if (errno != 0)
4045 return (-errno);
4046 return (0);
4048 #endif /* USE_ELF_CORE_DUMP */
4050 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4052 init_thread(regs, infop);