]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/elfload.c
linux-user: Add AT_HWCAP2 for aarch64-linux-user
[mirror_qemu.git] / linux-user / elfload.c
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
3 #include <sys/param.h>
4
5 #include <sys/resource.h>
6 #include <sys/shm.h>
7
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
14 #ifdef _ARCH_PPC64
15 #undef ARCH_DLINFO
16 #undef ELF_PLATFORM
17 #undef ELF_HWCAP
18 #undef ELF_HWCAP2
19 #undef ELF_CLASS
20 #undef ELF_DATA
21 #undef ELF_ARCH
22 #endif
23
24 #define ELF_OSABI ELFOSABI_SYSV
25
26 /* from personality.h */
27
28 /*
29 * Flags for bug emulation.
30 *
31 * These occupy the top three bytes.
32 */
33 enum {
34 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
35 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
36 descriptors (signal handling) */
37 MMAP_PAGE_ZERO = 0x0100000,
38 ADDR_COMPAT_LAYOUT = 0x0200000,
39 READ_IMPLIES_EXEC = 0x0400000,
40 ADDR_LIMIT_32BIT = 0x0800000,
41 SHORT_INODE = 0x1000000,
42 WHOLE_SECONDS = 0x2000000,
43 STICKY_TIMEOUTS = 0x4000000,
44 ADDR_LIMIT_3GB = 0x8000000,
45 };
46
47 /*
48 * Personality types.
49 *
50 * These go in the low byte. Avoid using the top bit, it will
51 * conflict with error returns.
52 */
53 enum {
54 PER_LINUX = 0x0000,
55 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
56 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
57 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
58 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
59 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
60 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
61 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
62 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
63 PER_BSD = 0x0006,
64 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
65 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
66 PER_LINUX32 = 0x0008,
67 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
68 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
69 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
70 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
71 PER_RISCOS = 0x000c,
72 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
73 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
74 PER_OSF4 = 0x000f, /* OSF/1 v4 */
75 PER_HPUX = 0x0010,
76 PER_MASK = 0x00ff,
77 };
78
79 /*
80 * Return the base personality without flags.
81 */
82 #define personality(pers) (pers & PER_MASK)
83
84 int info_is_fdpic(struct image_info *info)
85 {
86 return info->personality == PER_LINUX_FDPIC;
87 }
88
89 /* this flag is uneffective under linux too, should be deleted */
90 #ifndef MAP_DENYWRITE
91 #define MAP_DENYWRITE 0
92 #endif
93
94 /* should probably go in elf.h */
95 #ifndef ELIBBAD
96 #define ELIBBAD 80
97 #endif
98
99 #ifdef TARGET_WORDS_BIGENDIAN
100 #define ELF_DATA ELFDATA2MSB
101 #else
102 #define ELF_DATA ELFDATA2LSB
103 #endif
104
105 #ifdef TARGET_ABI_MIPSN32
106 typedef abi_ullong target_elf_greg_t;
107 #define tswapreg(ptr) tswap64(ptr)
108 #else
109 typedef abi_ulong target_elf_greg_t;
110 #define tswapreg(ptr) tswapal(ptr)
111 #endif
112
113 #ifdef USE_UID16
114 typedef abi_ushort target_uid_t;
115 typedef abi_ushort target_gid_t;
116 #else
117 typedef abi_uint target_uid_t;
118 typedef abi_uint target_gid_t;
119 #endif
120 typedef abi_int target_pid_t;
121
122 #ifdef TARGET_I386
123
124 #define ELF_PLATFORM get_elf_platform()
125
126 static const char *get_elf_platform(void)
127 {
128 static char elf_platform[] = "i386";
129 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
130 if (family > 6)
131 family = 6;
132 if (family >= 3)
133 elf_platform[1] = '0' + family;
134 return elf_platform;
135 }
136
137 #define ELF_HWCAP get_elf_hwcap()
138
139 static uint32_t get_elf_hwcap(void)
140 {
141 X86CPU *cpu = X86_CPU(thread_cpu);
142
143 return cpu->env.features[FEAT_1_EDX];
144 }
145
146 #ifdef TARGET_X86_64
147 #define ELF_START_MMAP 0x2aaaaab000ULL
148
149 #define ELF_CLASS ELFCLASS64
150 #define ELF_ARCH EM_X86_64
151
152 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
153 {
154 regs->rax = 0;
155 regs->rsp = infop->start_stack;
156 regs->rip = infop->entry;
157 }
158
159 #define ELF_NREG 27
160 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
161
162 /*
163 * Note that ELF_NREG should be 29 as there should be place for
164 * TRAPNO and ERR "registers" as well but linux doesn't dump
165 * those.
166 *
167 * See linux kernel: arch/x86/include/asm/elf.h
168 */
169 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
170 {
171 (*regs)[0] = env->regs[15];
172 (*regs)[1] = env->regs[14];
173 (*regs)[2] = env->regs[13];
174 (*regs)[3] = env->regs[12];
175 (*regs)[4] = env->regs[R_EBP];
176 (*regs)[5] = env->regs[R_EBX];
177 (*regs)[6] = env->regs[11];
178 (*regs)[7] = env->regs[10];
179 (*regs)[8] = env->regs[9];
180 (*regs)[9] = env->regs[8];
181 (*regs)[10] = env->regs[R_EAX];
182 (*regs)[11] = env->regs[R_ECX];
183 (*regs)[12] = env->regs[R_EDX];
184 (*regs)[13] = env->regs[R_ESI];
185 (*regs)[14] = env->regs[R_EDI];
186 (*regs)[15] = env->regs[R_EAX]; /* XXX */
187 (*regs)[16] = env->eip;
188 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
189 (*regs)[18] = env->eflags;
190 (*regs)[19] = env->regs[R_ESP];
191 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
192 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
193 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
194 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
195 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
196 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
197 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
198 }
199
200 #else
201
202 #define ELF_START_MMAP 0x80000000
203
204 /*
205 * This is used to ensure we don't load something for the wrong architecture.
206 */
207 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
208
209 /*
210 * These are used to set parameters in the core dumps.
211 */
212 #define ELF_CLASS ELFCLASS32
213 #define ELF_ARCH EM_386
214
215 static inline void init_thread(struct target_pt_regs *regs,
216 struct image_info *infop)
217 {
218 regs->esp = infop->start_stack;
219 regs->eip = infop->entry;
220
221 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
222 starts %edx contains a pointer to a function which might be
223 registered using `atexit'. This provides a mean for the
224 dynamic linker to call DT_FINI functions for shared libraries
225 that have been loaded before the code runs.
226
227 A value of 0 tells we have no such handler. */
228 regs->edx = 0;
229 }
230
231 #define ELF_NREG 17
232 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
233
234 /*
235 * Note that ELF_NREG should be 19 as there should be place for
236 * TRAPNO and ERR "registers" as well but linux doesn't dump
237 * those.
238 *
239 * See linux kernel: arch/x86/include/asm/elf.h
240 */
241 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
242 {
243 (*regs)[0] = env->regs[R_EBX];
244 (*regs)[1] = env->regs[R_ECX];
245 (*regs)[2] = env->regs[R_EDX];
246 (*regs)[3] = env->regs[R_ESI];
247 (*regs)[4] = env->regs[R_EDI];
248 (*regs)[5] = env->regs[R_EBP];
249 (*regs)[6] = env->regs[R_EAX];
250 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
251 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
252 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
253 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
254 (*regs)[11] = env->regs[R_EAX]; /* XXX */
255 (*regs)[12] = env->eip;
256 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
257 (*regs)[14] = env->eflags;
258 (*regs)[15] = env->regs[R_ESP];
259 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
260 }
261 #endif
262
263 #define USE_ELF_CORE_DUMP
264 #define ELF_EXEC_PAGESIZE 4096
265
266 #endif
267
268 #ifdef TARGET_ARM
269
270 #ifndef TARGET_AARCH64
271 /* 32 bit ARM definitions */
272
273 #define ELF_START_MMAP 0x80000000
274
275 #define ELF_ARCH EM_ARM
276 #define ELF_CLASS ELFCLASS32
277
278 static inline void init_thread(struct target_pt_regs *regs,
279 struct image_info *infop)
280 {
281 abi_long stack = infop->start_stack;
282 memset(regs, 0, sizeof(*regs));
283
284 regs->uregs[16] = ARM_CPU_MODE_USR;
285 if (infop->entry & 1) {
286 regs->uregs[16] |= CPSR_T;
287 }
288 regs->uregs[15] = infop->entry & 0xfffffffe;
289 regs->uregs[13] = infop->start_stack;
290 /* FIXME - what to for failure of get_user()? */
291 get_user_ual(regs->uregs[2], stack + 8); /* envp */
292 get_user_ual(regs->uregs[1], stack + 4); /* envp */
293 /* XXX: it seems that r0 is zeroed after ! */
294 regs->uregs[0] = 0;
295 /* For uClinux PIC binaries. */
296 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
297 regs->uregs[10] = infop->start_data;
298
299 /* Support ARM FDPIC. */
300 if (info_is_fdpic(infop)) {
301 /* As described in the ABI document, r7 points to the loadmap info
302 * prepared by the kernel. If an interpreter is needed, r8 points
303 * to the interpreter loadmap and r9 points to the interpreter
304 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
305 * r9 points to the main program PT_DYNAMIC info.
306 */
307 regs->uregs[7] = infop->loadmap_addr;
308 if (infop->interpreter_loadmap_addr) {
309 /* Executable is dynamically loaded. */
310 regs->uregs[8] = infop->interpreter_loadmap_addr;
311 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
312 } else {
313 regs->uregs[8] = 0;
314 regs->uregs[9] = infop->pt_dynamic_addr;
315 }
316 }
317 }
318
319 #define ELF_NREG 18
320 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
321
322 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
323 {
324 (*regs)[0] = tswapreg(env->regs[0]);
325 (*regs)[1] = tswapreg(env->regs[1]);
326 (*regs)[2] = tswapreg(env->regs[2]);
327 (*regs)[3] = tswapreg(env->regs[3]);
328 (*regs)[4] = tswapreg(env->regs[4]);
329 (*regs)[5] = tswapreg(env->regs[5]);
330 (*regs)[6] = tswapreg(env->regs[6]);
331 (*regs)[7] = tswapreg(env->regs[7]);
332 (*regs)[8] = tswapreg(env->regs[8]);
333 (*regs)[9] = tswapreg(env->regs[9]);
334 (*regs)[10] = tswapreg(env->regs[10]);
335 (*regs)[11] = tswapreg(env->regs[11]);
336 (*regs)[12] = tswapreg(env->regs[12]);
337 (*regs)[13] = tswapreg(env->regs[13]);
338 (*regs)[14] = tswapreg(env->regs[14]);
339 (*regs)[15] = tswapreg(env->regs[15]);
340
341 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
342 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
343 }
344
345 #define USE_ELF_CORE_DUMP
346 #define ELF_EXEC_PAGESIZE 4096
347
348 enum
349 {
350 ARM_HWCAP_ARM_SWP = 1 << 0,
351 ARM_HWCAP_ARM_HALF = 1 << 1,
352 ARM_HWCAP_ARM_THUMB = 1 << 2,
353 ARM_HWCAP_ARM_26BIT = 1 << 3,
354 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
355 ARM_HWCAP_ARM_FPA = 1 << 5,
356 ARM_HWCAP_ARM_VFP = 1 << 6,
357 ARM_HWCAP_ARM_EDSP = 1 << 7,
358 ARM_HWCAP_ARM_JAVA = 1 << 8,
359 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
360 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
361 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
362 ARM_HWCAP_ARM_NEON = 1 << 12,
363 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
364 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
365 ARM_HWCAP_ARM_TLS = 1 << 15,
366 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
367 ARM_HWCAP_ARM_IDIVA = 1 << 17,
368 ARM_HWCAP_ARM_IDIVT = 1 << 18,
369 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
370 ARM_HWCAP_ARM_LPAE = 1 << 20,
371 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
372 };
373
374 enum {
375 ARM_HWCAP2_ARM_AES = 1 << 0,
376 ARM_HWCAP2_ARM_PMULL = 1 << 1,
377 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
378 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
379 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
380 };
381
382 /* The commpage only exists for 32 bit kernels */
383
384 /* Return 1 if the proposed guest space is suitable for the guest.
385 * Return 0 if the proposed guest space isn't suitable, but another
386 * address space should be tried.
387 * Return -1 if there is no way the proposed guest space can be
388 * valid regardless of the base.
389 * The guest code may leave a page mapped and populate it if the
390 * address is suitable.
391 */
392 static int init_guest_commpage(unsigned long guest_base,
393 unsigned long guest_size)
394 {
395 unsigned long real_start, test_page_addr;
396
397 /* We need to check that we can force a fault on access to the
398 * commpage at 0xffff0fxx
399 */
400 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
401
402 /* If the commpage lies within the already allocated guest space,
403 * then there is no way we can allocate it.
404 *
405 * You may be thinking that that this check is redundant because
406 * we already validated the guest size against MAX_RESERVED_VA;
407 * but if qemu_host_page_mask is unusually large, then
408 * test_page_addr may be lower.
409 */
410 if (test_page_addr >= guest_base
411 && test_page_addr < (guest_base + guest_size)) {
412 return -1;
413 }
414
415 /* Note it needs to be writeable to let us initialise it */
416 real_start = (unsigned long)
417 mmap((void *)test_page_addr, qemu_host_page_size,
418 PROT_READ | PROT_WRITE,
419 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
420
421 /* If we can't map it then try another address */
422 if (real_start == -1ul) {
423 return 0;
424 }
425
426 if (real_start != test_page_addr) {
427 /* OS didn't put the page where we asked - unmap and reject */
428 munmap((void *)real_start, qemu_host_page_size);
429 return 0;
430 }
431
432 /* Leave the page mapped
433 * Populate it (mmap should have left it all 0'd)
434 */
435
436 /* Kernel helper versions */
437 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
438
439 /* Now it's populated make it RO */
440 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
441 perror("Protecting guest commpage");
442 exit(-1);
443 }
444
445 return 1; /* All good */
446 }
447
448 #define ELF_HWCAP get_elf_hwcap()
449 #define ELF_HWCAP2 get_elf_hwcap2()
450
451 static uint32_t get_elf_hwcap(void)
452 {
453 ARMCPU *cpu = ARM_CPU(thread_cpu);
454 uint32_t hwcaps = 0;
455
456 hwcaps |= ARM_HWCAP_ARM_SWP;
457 hwcaps |= ARM_HWCAP_ARM_HALF;
458 hwcaps |= ARM_HWCAP_ARM_THUMB;
459 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
460
461 /* probe for the extra features */
462 #define GET_FEATURE(feat, hwcap) \
463 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
464
465 #define GET_FEATURE_ID(feat, hwcap) \
466 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
467
468 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
469 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
470 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
471 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
472 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
473 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
474 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
475 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
476 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
477 GET_FEATURE_ID(arm_div, ARM_HWCAP_ARM_IDIVA);
478 GET_FEATURE_ID(thumb_div, ARM_HWCAP_ARM_IDIVT);
479 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
480 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
481 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
482 * to our VFP_FP16 feature bit.
483 */
484 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
485 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
486
487 return hwcaps;
488 }
489
490 static uint32_t get_elf_hwcap2(void)
491 {
492 ARMCPU *cpu = ARM_CPU(thread_cpu);
493 uint32_t hwcaps = 0;
494
495 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
496 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
497 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
498 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
499 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
500 return hwcaps;
501 }
502
503 #undef GET_FEATURE
504 #undef GET_FEATURE_ID
505
506 #define ELF_PLATFORM get_elf_platform()
507
508 static const char *get_elf_platform(void)
509 {
510 CPUARMState *env = thread_cpu->env_ptr;
511
512 #ifdef TARGET_WORDS_BIGENDIAN
513 # define END "b"
514 #else
515 # define END "l"
516 #endif
517
518 if (arm_feature(env, ARM_FEATURE_V8)) {
519 return "v8" END;
520 } else if (arm_feature(env, ARM_FEATURE_V7)) {
521 if (arm_feature(env, ARM_FEATURE_M)) {
522 return "v7m" END;
523 } else {
524 return "v7" END;
525 }
526 } else if (arm_feature(env, ARM_FEATURE_V6)) {
527 return "v6" END;
528 } else if (arm_feature(env, ARM_FEATURE_V5)) {
529 return "v5" END;
530 } else {
531 return "v4" END;
532 }
533
534 #undef END
535 }
536
537 #else
538 /* 64 bit ARM definitions */
539 #define ELF_START_MMAP 0x80000000
540
541 #define ELF_ARCH EM_AARCH64
542 #define ELF_CLASS ELFCLASS64
543 #ifdef TARGET_WORDS_BIGENDIAN
544 # define ELF_PLATFORM "aarch64_be"
545 #else
546 # define ELF_PLATFORM "aarch64"
547 #endif
548
549 static inline void init_thread(struct target_pt_regs *regs,
550 struct image_info *infop)
551 {
552 abi_long stack = infop->start_stack;
553 memset(regs, 0, sizeof(*regs));
554
555 regs->pc = infop->entry & ~0x3ULL;
556 regs->sp = stack;
557 }
558
559 #define ELF_NREG 34
560 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
561
562 static void elf_core_copy_regs(target_elf_gregset_t *regs,
563 const CPUARMState *env)
564 {
565 int i;
566
567 for (i = 0; i < 32; i++) {
568 (*regs)[i] = tswapreg(env->xregs[i]);
569 }
570 (*regs)[32] = tswapreg(env->pc);
571 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
572 }
573
574 #define USE_ELF_CORE_DUMP
575 #define ELF_EXEC_PAGESIZE 4096
576
577 enum {
578 ARM_HWCAP_A64_FP = 1 << 0,
579 ARM_HWCAP_A64_ASIMD = 1 << 1,
580 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
581 ARM_HWCAP_A64_AES = 1 << 3,
582 ARM_HWCAP_A64_PMULL = 1 << 4,
583 ARM_HWCAP_A64_SHA1 = 1 << 5,
584 ARM_HWCAP_A64_SHA2 = 1 << 6,
585 ARM_HWCAP_A64_CRC32 = 1 << 7,
586 ARM_HWCAP_A64_ATOMICS = 1 << 8,
587 ARM_HWCAP_A64_FPHP = 1 << 9,
588 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
589 ARM_HWCAP_A64_CPUID = 1 << 11,
590 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
591 ARM_HWCAP_A64_JSCVT = 1 << 13,
592 ARM_HWCAP_A64_FCMA = 1 << 14,
593 ARM_HWCAP_A64_LRCPC = 1 << 15,
594 ARM_HWCAP_A64_DCPOP = 1 << 16,
595 ARM_HWCAP_A64_SHA3 = 1 << 17,
596 ARM_HWCAP_A64_SM3 = 1 << 18,
597 ARM_HWCAP_A64_SM4 = 1 << 19,
598 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
599 ARM_HWCAP_A64_SHA512 = 1 << 21,
600 ARM_HWCAP_A64_SVE = 1 << 22,
601 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
602 ARM_HWCAP_A64_DIT = 1 << 24,
603 ARM_HWCAP_A64_USCAT = 1 << 25,
604 ARM_HWCAP_A64_ILRCPC = 1 << 26,
605 ARM_HWCAP_A64_FLAGM = 1 << 27,
606 ARM_HWCAP_A64_SSBS = 1 << 28,
607 ARM_HWCAP_A64_SB = 1 << 29,
608 ARM_HWCAP_A64_PACA = 1 << 30,
609 ARM_HWCAP_A64_PACG = 1UL << 31,
610
611 ARM_HWCAP2_A64_DCPODP = 1 << 0,
612 ARM_HWCAP2_A64_SVE2 = 1 << 1,
613 ARM_HWCAP2_A64_SVEAES = 1 << 2,
614 ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
615 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
616 ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
617 ARM_HWCAP2_A64_SVESM4 = 1 << 6,
618 ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
619 ARM_HWCAP2_A64_FRINT = 1 << 8,
620 };
621
622 #define ELF_HWCAP get_elf_hwcap()
623 #define ELF_HWCAP2 get_elf_hwcap2()
624
625 #define GET_FEATURE_ID(feat, hwcap) \
626 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
627
628 static uint32_t get_elf_hwcap(void)
629 {
630 ARMCPU *cpu = ARM_CPU(thread_cpu);
631 uint32_t hwcaps = 0;
632
633 hwcaps |= ARM_HWCAP_A64_FP;
634 hwcaps |= ARM_HWCAP_A64_ASIMD;
635 hwcaps |= ARM_HWCAP_A64_CPUID;
636
637 /* probe for the extra features */
638
639 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
640 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
641 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
642 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
643 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
644 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
645 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
646 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
647 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
648 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
649 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
650 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
651 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
652 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
653 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
654 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
655 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
656 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
657 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
658 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
659
660 return hwcaps;
661 }
662
663 static uint32_t get_elf_hwcap2(void)
664 {
665 ARMCPU *cpu = ARM_CPU(thread_cpu);
666 uint32_t hwcaps = 0;
667
668 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
669 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
670
671 return hwcaps;
672 }
673
674 #undef GET_FEATURE_ID
675
676 #endif /* not TARGET_AARCH64 */
677 #endif /* TARGET_ARM */
678
679 #ifdef TARGET_SPARC
680 #ifdef TARGET_SPARC64
681
682 #define ELF_START_MMAP 0x80000000
683 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
684 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
685 #ifndef TARGET_ABI32
686 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
687 #else
688 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
689 #endif
690
691 #define ELF_CLASS ELFCLASS64
692 #define ELF_ARCH EM_SPARCV9
693
694 #define STACK_BIAS 2047
695
696 static inline void init_thread(struct target_pt_regs *regs,
697 struct image_info *infop)
698 {
699 #ifndef TARGET_ABI32
700 regs->tstate = 0;
701 #endif
702 regs->pc = infop->entry;
703 regs->npc = regs->pc + 4;
704 regs->y = 0;
705 #ifdef TARGET_ABI32
706 regs->u_regs[14] = infop->start_stack - 16 * 4;
707 #else
708 if (personality(infop->personality) == PER_LINUX32)
709 regs->u_regs[14] = infop->start_stack - 16 * 4;
710 else
711 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
712 #endif
713 }
714
715 #else
716 #define ELF_START_MMAP 0x80000000
717 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
718 | HWCAP_SPARC_MULDIV)
719
720 #define ELF_CLASS ELFCLASS32
721 #define ELF_ARCH EM_SPARC
722
723 static inline void init_thread(struct target_pt_regs *regs,
724 struct image_info *infop)
725 {
726 regs->psr = 0;
727 regs->pc = infop->entry;
728 regs->npc = regs->pc + 4;
729 regs->y = 0;
730 regs->u_regs[14] = infop->start_stack - 16 * 4;
731 }
732
733 #endif
734 #endif
735
736 #ifdef TARGET_PPC
737
738 #define ELF_MACHINE PPC_ELF_MACHINE
739 #define ELF_START_MMAP 0x80000000
740
741 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
742
743 #define elf_check_arch(x) ( (x) == EM_PPC64 )
744
745 #define ELF_CLASS ELFCLASS64
746
747 #else
748
749 #define ELF_CLASS ELFCLASS32
750
751 #endif
752
753 #define ELF_ARCH EM_PPC
754
755 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
756 See arch/powerpc/include/asm/cputable.h. */
757 enum {
758 QEMU_PPC_FEATURE_32 = 0x80000000,
759 QEMU_PPC_FEATURE_64 = 0x40000000,
760 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
761 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
762 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
763 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
764 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
765 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
766 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
767 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
768 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
769 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
770 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
771 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
772 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
773 QEMU_PPC_FEATURE_CELL = 0x00010000,
774 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
775 QEMU_PPC_FEATURE_SMT = 0x00004000,
776 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
777 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
778 QEMU_PPC_FEATURE_PA6T = 0x00000800,
779 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
780 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
781 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
782 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
783 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
784
785 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
786 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
787
788 /* Feature definitions in AT_HWCAP2. */
789 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
790 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
791 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
792 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
793 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
794 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
795 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
796 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
797 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
798 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
799 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
800 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
801 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
802 };
803
804 #define ELF_HWCAP get_elf_hwcap()
805
806 static uint32_t get_elf_hwcap(void)
807 {
808 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
809 uint32_t features = 0;
810
811 /* We don't have to be terribly complete here; the high points are
812 Altivec/FP/SPE support. Anything else is just a bonus. */
813 #define GET_FEATURE(flag, feature) \
814 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
815 #define GET_FEATURE2(flags, feature) \
816 do { \
817 if ((cpu->env.insns_flags2 & flags) == flags) { \
818 features |= feature; \
819 } \
820 } while (0)
821 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
822 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
823 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
824 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
825 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
826 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
827 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
828 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
829 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
830 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
831 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
832 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
833 QEMU_PPC_FEATURE_ARCH_2_06);
834 #undef GET_FEATURE
835 #undef GET_FEATURE2
836
837 return features;
838 }
839
840 #define ELF_HWCAP2 get_elf_hwcap2()
841
842 static uint32_t get_elf_hwcap2(void)
843 {
844 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
845 uint32_t features = 0;
846
847 #define GET_FEATURE(flag, feature) \
848 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
849 #define GET_FEATURE2(flag, feature) \
850 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
851
852 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
853 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
854 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
855 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
856 QEMU_PPC_FEATURE2_VEC_CRYPTO);
857 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
858 QEMU_PPC_FEATURE2_DARN);
859
860 #undef GET_FEATURE
861 #undef GET_FEATURE2
862
863 return features;
864 }
865
866 /*
867 * The requirements here are:
868 * - keep the final alignment of sp (sp & 0xf)
869 * - make sure the 32-bit value at the first 16 byte aligned position of
870 * AUXV is greater than 16 for glibc compatibility.
871 * AT_IGNOREPPC is used for that.
872 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
873 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
874 */
875 #define DLINFO_ARCH_ITEMS 5
876 #define ARCH_DLINFO \
877 do { \
878 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
879 /* \
880 * Handle glibc compatibility: these magic entries must \
881 * be at the lowest addresses in the final auxv. \
882 */ \
883 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
884 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
885 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
886 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
887 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
888 } while (0)
889
890 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
891 {
892 _regs->gpr[1] = infop->start_stack;
893 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
894 if (get_ppc64_abi(infop) < 2) {
895 uint64_t val;
896 get_user_u64(val, infop->entry + 8);
897 _regs->gpr[2] = val + infop->load_bias;
898 get_user_u64(val, infop->entry);
899 infop->entry = val + infop->load_bias;
900 } else {
901 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
902 }
903 #endif
904 _regs->nip = infop->entry;
905 }
906
907 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
908 #define ELF_NREG 48
909 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
910
911 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
912 {
913 int i;
914 target_ulong ccr = 0;
915
916 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
917 (*regs)[i] = tswapreg(env->gpr[i]);
918 }
919
920 (*regs)[32] = tswapreg(env->nip);
921 (*regs)[33] = tswapreg(env->msr);
922 (*regs)[35] = tswapreg(env->ctr);
923 (*regs)[36] = tswapreg(env->lr);
924 (*regs)[37] = tswapreg(env->xer);
925
926 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
927 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
928 }
929 (*regs)[38] = tswapreg(ccr);
930 }
931
932 #define USE_ELF_CORE_DUMP
933 #define ELF_EXEC_PAGESIZE 4096
934
935 #endif
936
937 #ifdef TARGET_MIPS
938
939 #define ELF_START_MMAP 0x80000000
940
941 #ifdef TARGET_MIPS64
942 #define ELF_CLASS ELFCLASS64
943 #else
944 #define ELF_CLASS ELFCLASS32
945 #endif
946 #define ELF_ARCH EM_MIPS
947
948 #define elf_check_arch(x) ((x) == EM_MIPS || (x) == EM_NANOMIPS)
949
950 static inline void init_thread(struct target_pt_regs *regs,
951 struct image_info *infop)
952 {
953 regs->cp0_status = 2 << CP0St_KSU;
954 regs->cp0_epc = infop->entry;
955 regs->regs[29] = infop->start_stack;
956 }
957
958 /* See linux kernel: arch/mips/include/asm/elf.h. */
959 #define ELF_NREG 45
960 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
961
962 /* See linux kernel: arch/mips/include/asm/reg.h. */
963 enum {
964 #ifdef TARGET_MIPS64
965 TARGET_EF_R0 = 0,
966 #else
967 TARGET_EF_R0 = 6,
968 #endif
969 TARGET_EF_R26 = TARGET_EF_R0 + 26,
970 TARGET_EF_R27 = TARGET_EF_R0 + 27,
971 TARGET_EF_LO = TARGET_EF_R0 + 32,
972 TARGET_EF_HI = TARGET_EF_R0 + 33,
973 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
974 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
975 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
976 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
977 };
978
979 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
980 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
981 {
982 int i;
983
984 for (i = 0; i < TARGET_EF_R0; i++) {
985 (*regs)[i] = 0;
986 }
987 (*regs)[TARGET_EF_R0] = 0;
988
989 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
990 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
991 }
992
993 (*regs)[TARGET_EF_R26] = 0;
994 (*regs)[TARGET_EF_R27] = 0;
995 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
996 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
997 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
998 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
999 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
1000 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
1001 }
1002
1003 #define USE_ELF_CORE_DUMP
1004 #define ELF_EXEC_PAGESIZE 4096
1005
1006 /* See arch/mips/include/uapi/asm/hwcap.h. */
1007 enum {
1008 HWCAP_MIPS_R6 = (1 << 0),
1009 HWCAP_MIPS_MSA = (1 << 1),
1010 };
1011
1012 #define ELF_HWCAP get_elf_hwcap()
1013
1014 static uint32_t get_elf_hwcap(void)
1015 {
1016 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1017 uint32_t hwcaps = 0;
1018
1019 #define GET_FEATURE(flag, hwcap) \
1020 do { if (cpu->env.insn_flags & (flag)) { hwcaps |= hwcap; } } while (0)
1021
1022 GET_FEATURE(ISA_MIPS32R6 | ISA_MIPS64R6, HWCAP_MIPS_R6);
1023 GET_FEATURE(ASE_MSA, HWCAP_MIPS_MSA);
1024
1025 #undef GET_FEATURE
1026
1027 return hwcaps;
1028 }
1029
1030 #endif /* TARGET_MIPS */
1031
1032 #ifdef TARGET_MICROBLAZE
1033
1034 #define ELF_START_MMAP 0x80000000
1035
1036 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1037
1038 #define ELF_CLASS ELFCLASS32
1039 #define ELF_ARCH EM_MICROBLAZE
1040
1041 static inline void init_thread(struct target_pt_regs *regs,
1042 struct image_info *infop)
1043 {
1044 regs->pc = infop->entry;
1045 regs->r1 = infop->start_stack;
1046
1047 }
1048
1049 #define ELF_EXEC_PAGESIZE 4096
1050
1051 #define USE_ELF_CORE_DUMP
1052 #define ELF_NREG 38
1053 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1054
1055 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1056 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1057 {
1058 int i, pos = 0;
1059
1060 for (i = 0; i < 32; i++) {
1061 (*regs)[pos++] = tswapreg(env->regs[i]);
1062 }
1063
1064 for (i = 0; i < 6; i++) {
1065 (*regs)[pos++] = tswapreg(env->sregs[i]);
1066 }
1067 }
1068
1069 #endif /* TARGET_MICROBLAZE */
1070
1071 #ifdef TARGET_NIOS2
1072
1073 #define ELF_START_MMAP 0x80000000
1074
1075 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1076
1077 #define ELF_CLASS ELFCLASS32
1078 #define ELF_ARCH EM_ALTERA_NIOS2
1079
1080 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1081 {
1082 regs->ea = infop->entry;
1083 regs->sp = infop->start_stack;
1084 regs->estatus = 0x3;
1085 }
1086
1087 #define ELF_EXEC_PAGESIZE 4096
1088
1089 #define USE_ELF_CORE_DUMP
1090 #define ELF_NREG 49
1091 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1092
1093 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1094 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1095 const CPUNios2State *env)
1096 {
1097 int i;
1098
1099 (*regs)[0] = -1;
1100 for (i = 1; i < 8; i++) /* r0-r7 */
1101 (*regs)[i] = tswapreg(env->regs[i + 7]);
1102
1103 for (i = 8; i < 16; i++) /* r8-r15 */
1104 (*regs)[i] = tswapreg(env->regs[i - 8]);
1105
1106 for (i = 16; i < 24; i++) /* r16-r23 */
1107 (*regs)[i] = tswapreg(env->regs[i + 7]);
1108 (*regs)[24] = -1; /* R_ET */
1109 (*regs)[25] = -1; /* R_BT */
1110 (*regs)[26] = tswapreg(env->regs[R_GP]);
1111 (*regs)[27] = tswapreg(env->regs[R_SP]);
1112 (*regs)[28] = tswapreg(env->regs[R_FP]);
1113 (*regs)[29] = tswapreg(env->regs[R_EA]);
1114 (*regs)[30] = -1; /* R_SSTATUS */
1115 (*regs)[31] = tswapreg(env->regs[R_RA]);
1116
1117 (*regs)[32] = tswapreg(env->regs[R_PC]);
1118
1119 (*regs)[33] = -1; /* R_STATUS */
1120 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1121
1122 for (i = 35; i < 49; i++) /* ... */
1123 (*regs)[i] = -1;
1124 }
1125
1126 #endif /* TARGET_NIOS2 */
1127
1128 #ifdef TARGET_OPENRISC
1129
1130 #define ELF_START_MMAP 0x08000000
1131
1132 #define ELF_ARCH EM_OPENRISC
1133 #define ELF_CLASS ELFCLASS32
1134 #define ELF_DATA ELFDATA2MSB
1135
1136 static inline void init_thread(struct target_pt_regs *regs,
1137 struct image_info *infop)
1138 {
1139 regs->pc = infop->entry;
1140 regs->gpr[1] = infop->start_stack;
1141 }
1142
1143 #define USE_ELF_CORE_DUMP
1144 #define ELF_EXEC_PAGESIZE 8192
1145
1146 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1147 #define ELF_NREG 34 /* gprs and pc, sr */
1148 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1149
1150 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1151 const CPUOpenRISCState *env)
1152 {
1153 int i;
1154
1155 for (i = 0; i < 32; i++) {
1156 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1157 }
1158 (*regs)[32] = tswapreg(env->pc);
1159 (*regs)[33] = tswapreg(cpu_get_sr(env));
1160 }
1161 #define ELF_HWCAP 0
1162 #define ELF_PLATFORM NULL
1163
1164 #endif /* TARGET_OPENRISC */
1165
1166 #ifdef TARGET_SH4
1167
1168 #define ELF_START_MMAP 0x80000000
1169
1170 #define ELF_CLASS ELFCLASS32
1171 #define ELF_ARCH EM_SH
1172
1173 static inline void init_thread(struct target_pt_regs *regs,
1174 struct image_info *infop)
1175 {
1176 /* Check other registers XXXXX */
1177 regs->pc = infop->entry;
1178 regs->regs[15] = infop->start_stack;
1179 }
1180
1181 /* See linux kernel: arch/sh/include/asm/elf.h. */
1182 #define ELF_NREG 23
1183 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1184
1185 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1186 enum {
1187 TARGET_REG_PC = 16,
1188 TARGET_REG_PR = 17,
1189 TARGET_REG_SR = 18,
1190 TARGET_REG_GBR = 19,
1191 TARGET_REG_MACH = 20,
1192 TARGET_REG_MACL = 21,
1193 TARGET_REG_SYSCALL = 22
1194 };
1195
1196 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1197 const CPUSH4State *env)
1198 {
1199 int i;
1200
1201 for (i = 0; i < 16; i++) {
1202 (*regs)[i] = tswapreg(env->gregs[i]);
1203 }
1204
1205 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1206 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1207 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1208 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1209 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1210 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1211 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1212 }
1213
1214 #define USE_ELF_CORE_DUMP
1215 #define ELF_EXEC_PAGESIZE 4096
1216
1217 enum {
1218 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1219 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1220 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1221 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1222 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1223 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1224 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1225 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1226 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1227 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1228 };
1229
1230 #define ELF_HWCAP get_elf_hwcap()
1231
1232 static uint32_t get_elf_hwcap(void)
1233 {
1234 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1235 uint32_t hwcap = 0;
1236
1237 hwcap |= SH_CPU_HAS_FPU;
1238
1239 if (cpu->env.features & SH_FEATURE_SH4A) {
1240 hwcap |= SH_CPU_HAS_LLSC;
1241 }
1242
1243 return hwcap;
1244 }
1245
1246 #endif
1247
1248 #ifdef TARGET_CRIS
1249
1250 #define ELF_START_MMAP 0x80000000
1251
1252 #define ELF_CLASS ELFCLASS32
1253 #define ELF_ARCH EM_CRIS
1254
1255 static inline void init_thread(struct target_pt_regs *regs,
1256 struct image_info *infop)
1257 {
1258 regs->erp = infop->entry;
1259 }
1260
1261 #define ELF_EXEC_PAGESIZE 8192
1262
1263 #endif
1264
1265 #ifdef TARGET_M68K
1266
1267 #define ELF_START_MMAP 0x80000000
1268
1269 #define ELF_CLASS ELFCLASS32
1270 #define ELF_ARCH EM_68K
1271
1272 /* ??? Does this need to do anything?
1273 #define ELF_PLAT_INIT(_r) */
1274
1275 static inline void init_thread(struct target_pt_regs *regs,
1276 struct image_info *infop)
1277 {
1278 regs->usp = infop->start_stack;
1279 regs->sr = 0;
1280 regs->pc = infop->entry;
1281 }
1282
1283 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1284 #define ELF_NREG 20
1285 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1286
1287 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1288 {
1289 (*regs)[0] = tswapreg(env->dregs[1]);
1290 (*regs)[1] = tswapreg(env->dregs[2]);
1291 (*regs)[2] = tswapreg(env->dregs[3]);
1292 (*regs)[3] = tswapreg(env->dregs[4]);
1293 (*regs)[4] = tswapreg(env->dregs[5]);
1294 (*regs)[5] = tswapreg(env->dregs[6]);
1295 (*regs)[6] = tswapreg(env->dregs[7]);
1296 (*regs)[7] = tswapreg(env->aregs[0]);
1297 (*regs)[8] = tswapreg(env->aregs[1]);
1298 (*regs)[9] = tswapreg(env->aregs[2]);
1299 (*regs)[10] = tswapreg(env->aregs[3]);
1300 (*regs)[11] = tswapreg(env->aregs[4]);
1301 (*regs)[12] = tswapreg(env->aregs[5]);
1302 (*regs)[13] = tswapreg(env->aregs[6]);
1303 (*regs)[14] = tswapreg(env->dregs[0]);
1304 (*regs)[15] = tswapreg(env->aregs[7]);
1305 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1306 (*regs)[17] = tswapreg(env->sr);
1307 (*regs)[18] = tswapreg(env->pc);
1308 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1309 }
1310
1311 #define USE_ELF_CORE_DUMP
1312 #define ELF_EXEC_PAGESIZE 8192
1313
1314 #endif
1315
1316 #ifdef TARGET_ALPHA
1317
1318 #define ELF_START_MMAP (0x30000000000ULL)
1319
1320 #define ELF_CLASS ELFCLASS64
1321 #define ELF_ARCH EM_ALPHA
1322
1323 static inline void init_thread(struct target_pt_regs *regs,
1324 struct image_info *infop)
1325 {
1326 regs->pc = infop->entry;
1327 regs->ps = 8;
1328 regs->usp = infop->start_stack;
1329 }
1330
1331 #define ELF_EXEC_PAGESIZE 8192
1332
1333 #endif /* TARGET_ALPHA */
1334
1335 #ifdef TARGET_S390X
1336
1337 #define ELF_START_MMAP (0x20000000000ULL)
1338
1339 #define ELF_CLASS ELFCLASS64
1340 #define ELF_DATA ELFDATA2MSB
1341 #define ELF_ARCH EM_S390
1342
1343 #include "elf.h"
1344
1345 #define ELF_HWCAP get_elf_hwcap()
1346
1347 #define GET_FEATURE(_feat, _hwcap) \
1348 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1349
1350 static uint32_t get_elf_hwcap(void)
1351 {
1352 /*
1353 * Let's assume we always have esan3 and zarch.
1354 * 31-bit processes can use 64-bit registers (high gprs).
1355 */
1356 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1357
1358 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1359 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1360 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1361 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1362 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1363 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1364 hwcap |= HWCAP_S390_ETF3EH;
1365 }
1366 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1367
1368 return hwcap;
1369 }
1370
1371 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1372 {
1373 regs->psw.addr = infop->entry;
1374 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1375 regs->gprs[15] = infop->start_stack;
1376 }
1377
1378 #endif /* TARGET_S390X */
1379
1380 #ifdef TARGET_TILEGX
1381
1382 /* 42 bits real used address, a half for user mode */
1383 #define ELF_START_MMAP (0x00000020000000000ULL)
1384
1385 #define elf_check_arch(x) ((x) == EM_TILEGX)
1386
1387 #define ELF_CLASS ELFCLASS64
1388 #define ELF_DATA ELFDATA2LSB
1389 #define ELF_ARCH EM_TILEGX
1390
1391 static inline void init_thread(struct target_pt_regs *regs,
1392 struct image_info *infop)
1393 {
1394 regs->pc = infop->entry;
1395 regs->sp = infop->start_stack;
1396
1397 }
1398
1399 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1400
1401 #endif /* TARGET_TILEGX */
1402
1403 #ifdef TARGET_RISCV
1404
1405 #define ELF_START_MMAP 0x80000000
1406 #define ELF_ARCH EM_RISCV
1407
1408 #ifdef TARGET_RISCV32
1409 #define ELF_CLASS ELFCLASS32
1410 #else
1411 #define ELF_CLASS ELFCLASS64
1412 #endif
1413
1414 static inline void init_thread(struct target_pt_regs *regs,
1415 struct image_info *infop)
1416 {
1417 regs->sepc = infop->entry;
1418 regs->sp = infop->start_stack;
1419 }
1420
1421 #define ELF_EXEC_PAGESIZE 4096
1422
1423 #endif /* TARGET_RISCV */
1424
1425 #ifdef TARGET_HPPA
1426
1427 #define ELF_START_MMAP 0x80000000
1428 #define ELF_CLASS ELFCLASS32
1429 #define ELF_ARCH EM_PARISC
1430 #define ELF_PLATFORM "PARISC"
1431 #define STACK_GROWS_DOWN 0
1432 #define STACK_ALIGNMENT 64
1433
1434 static inline void init_thread(struct target_pt_regs *regs,
1435 struct image_info *infop)
1436 {
1437 regs->iaoq[0] = infop->entry;
1438 regs->iaoq[1] = infop->entry + 4;
1439 regs->gr[23] = 0;
1440 regs->gr[24] = infop->arg_start;
1441 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1442 /* The top-of-stack contains a linkage buffer. */
1443 regs->gr[30] = infop->start_stack + 64;
1444 regs->gr[31] = infop->entry;
1445 }
1446
1447 #endif /* TARGET_HPPA */
1448
1449 #ifdef TARGET_XTENSA
1450
1451 #define ELF_START_MMAP 0x20000000
1452
1453 #define ELF_CLASS ELFCLASS32
1454 #define ELF_ARCH EM_XTENSA
1455
1456 static inline void init_thread(struct target_pt_regs *regs,
1457 struct image_info *infop)
1458 {
1459 regs->windowbase = 0;
1460 regs->windowstart = 1;
1461 regs->areg[1] = infop->start_stack;
1462 regs->pc = infop->entry;
1463 }
1464
1465 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1466 #define ELF_NREG 128
1467 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1468
1469 enum {
1470 TARGET_REG_PC,
1471 TARGET_REG_PS,
1472 TARGET_REG_LBEG,
1473 TARGET_REG_LEND,
1474 TARGET_REG_LCOUNT,
1475 TARGET_REG_SAR,
1476 TARGET_REG_WINDOWSTART,
1477 TARGET_REG_WINDOWBASE,
1478 TARGET_REG_THREADPTR,
1479 TARGET_REG_AR0 = 64,
1480 };
1481
1482 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1483 const CPUXtensaState *env)
1484 {
1485 unsigned i;
1486
1487 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1488 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1489 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1490 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1491 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1492 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1493 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1494 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1495 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1496 xtensa_sync_phys_from_window((CPUXtensaState *)env);
1497 for (i = 0; i < env->config->nareg; ++i) {
1498 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1499 }
1500 }
1501
1502 #define USE_ELF_CORE_DUMP
1503 #define ELF_EXEC_PAGESIZE 4096
1504
1505 #endif /* TARGET_XTENSA */
1506
1507 #ifndef ELF_PLATFORM
1508 #define ELF_PLATFORM (NULL)
1509 #endif
1510
1511 #ifndef ELF_MACHINE
1512 #define ELF_MACHINE ELF_ARCH
1513 #endif
1514
1515 #ifndef elf_check_arch
1516 #define elf_check_arch(x) ((x) == ELF_ARCH)
1517 #endif
1518
1519 #ifndef ELF_HWCAP
1520 #define ELF_HWCAP 0
1521 #endif
1522
1523 #ifndef STACK_GROWS_DOWN
1524 #define STACK_GROWS_DOWN 1
1525 #endif
1526
1527 #ifndef STACK_ALIGNMENT
1528 #define STACK_ALIGNMENT 16
1529 #endif
1530
1531 #ifdef TARGET_ABI32
1532 #undef ELF_CLASS
1533 #define ELF_CLASS ELFCLASS32
1534 #undef bswaptls
1535 #define bswaptls(ptr) bswap32s(ptr)
1536 #endif
1537
1538 #include "elf.h"
1539
1540 struct exec
1541 {
1542 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1543 unsigned int a_text; /* length of text, in bytes */
1544 unsigned int a_data; /* length of data, in bytes */
1545 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1546 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1547 unsigned int a_entry; /* start address */
1548 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1549 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1550 };
1551
1552
1553 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1554 #define OMAGIC 0407
1555 #define NMAGIC 0410
1556 #define ZMAGIC 0413
1557 #define QMAGIC 0314
1558
1559 /* Necessary parameters */
1560 #define TARGET_ELF_EXEC_PAGESIZE \
1561 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1562 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1563 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1564 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1565 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1566 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1567
1568 #define DLINFO_ITEMS 15
1569
1570 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1571 {
1572 memcpy(to, from, n);
1573 }
1574
1575 #ifdef BSWAP_NEEDED
1576 static void bswap_ehdr(struct elfhdr *ehdr)
1577 {
1578 bswap16s(&ehdr->e_type); /* Object file type */
1579 bswap16s(&ehdr->e_machine); /* Architecture */
1580 bswap32s(&ehdr->e_version); /* Object file version */
1581 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1582 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1583 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1584 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1585 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1586 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1587 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1588 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1589 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1590 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1591 }
1592
1593 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1594 {
1595 int i;
1596 for (i = 0; i < phnum; ++i, ++phdr) {
1597 bswap32s(&phdr->p_type); /* Segment type */
1598 bswap32s(&phdr->p_flags); /* Segment flags */
1599 bswaptls(&phdr->p_offset); /* Segment file offset */
1600 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1601 bswaptls(&phdr->p_paddr); /* Segment physical address */
1602 bswaptls(&phdr->p_filesz); /* Segment size in file */
1603 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1604 bswaptls(&phdr->p_align); /* Segment alignment */
1605 }
1606 }
1607
1608 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1609 {
1610 int i;
1611 for (i = 0; i < shnum; ++i, ++shdr) {
1612 bswap32s(&shdr->sh_name);
1613 bswap32s(&shdr->sh_type);
1614 bswaptls(&shdr->sh_flags);
1615 bswaptls(&shdr->sh_addr);
1616 bswaptls(&shdr->sh_offset);
1617 bswaptls(&shdr->sh_size);
1618 bswap32s(&shdr->sh_link);
1619 bswap32s(&shdr->sh_info);
1620 bswaptls(&shdr->sh_addralign);
1621 bswaptls(&shdr->sh_entsize);
1622 }
1623 }
1624
1625 static void bswap_sym(struct elf_sym *sym)
1626 {
1627 bswap32s(&sym->st_name);
1628 bswaptls(&sym->st_value);
1629 bswaptls(&sym->st_size);
1630 bswap16s(&sym->st_shndx);
1631 }
1632
1633 #ifdef TARGET_MIPS
1634 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
1635 {
1636 bswap16s(&abiflags->version);
1637 bswap32s(&abiflags->ases);
1638 bswap32s(&abiflags->isa_ext);
1639 bswap32s(&abiflags->flags1);
1640 bswap32s(&abiflags->flags2);
1641 }
1642 #endif
1643 #else
1644 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1645 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1646 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1647 static inline void bswap_sym(struct elf_sym *sym) { }
1648 #ifdef TARGET_MIPS
1649 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
1650 #endif
1651 #endif
1652
1653 #ifdef USE_ELF_CORE_DUMP
1654 static int elf_core_dump(int, const CPUArchState *);
1655 #endif /* USE_ELF_CORE_DUMP */
1656 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1657
1658 /* Verify the portions of EHDR within E_IDENT for the target.
1659 This can be performed before bswapping the entire header. */
1660 static bool elf_check_ident(struct elfhdr *ehdr)
1661 {
1662 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1663 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1664 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1665 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1666 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1667 && ehdr->e_ident[EI_DATA] == ELF_DATA
1668 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1669 }
1670
1671 /* Verify the portions of EHDR outside of E_IDENT for the target.
1672 This has to wait until after bswapping the header. */
1673 static bool elf_check_ehdr(struct elfhdr *ehdr)
1674 {
1675 return (elf_check_arch(ehdr->e_machine)
1676 && ehdr->e_ehsize == sizeof(struct elfhdr)
1677 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1678 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1679 }
1680
1681 /*
1682 * 'copy_elf_strings()' copies argument/envelope strings from user
1683 * memory to free pages in kernel mem. These are in a format ready
1684 * to be put directly into the top of new user memory.
1685 *
1686 */
1687 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1688 abi_ulong p, abi_ulong stack_limit)
1689 {
1690 char *tmp;
1691 int len, i;
1692 abi_ulong top = p;
1693
1694 if (!p) {
1695 return 0; /* bullet-proofing */
1696 }
1697
1698 if (STACK_GROWS_DOWN) {
1699 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1700 for (i = argc - 1; i >= 0; --i) {
1701 tmp = argv[i];
1702 if (!tmp) {
1703 fprintf(stderr, "VFS: argc is wrong");
1704 exit(-1);
1705 }
1706 len = strlen(tmp) + 1;
1707 tmp += len;
1708
1709 if (len > (p - stack_limit)) {
1710 return 0;
1711 }
1712 while (len) {
1713 int bytes_to_copy = (len > offset) ? offset : len;
1714 tmp -= bytes_to_copy;
1715 p -= bytes_to_copy;
1716 offset -= bytes_to_copy;
1717 len -= bytes_to_copy;
1718
1719 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1720
1721 if (offset == 0) {
1722 memcpy_to_target(p, scratch, top - p);
1723 top = p;
1724 offset = TARGET_PAGE_SIZE;
1725 }
1726 }
1727 }
1728 if (p != top) {
1729 memcpy_to_target(p, scratch + offset, top - p);
1730 }
1731 } else {
1732 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1733 for (i = 0; i < argc; ++i) {
1734 tmp = argv[i];
1735 if (!tmp) {
1736 fprintf(stderr, "VFS: argc is wrong");
1737 exit(-1);
1738 }
1739 len = strlen(tmp) + 1;
1740 if (len > (stack_limit - p)) {
1741 return 0;
1742 }
1743 while (len) {
1744 int bytes_to_copy = (len > remaining) ? remaining : len;
1745
1746 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1747
1748 tmp += bytes_to_copy;
1749 remaining -= bytes_to_copy;
1750 p += bytes_to_copy;
1751 len -= bytes_to_copy;
1752
1753 if (remaining == 0) {
1754 memcpy_to_target(top, scratch, p - top);
1755 top = p;
1756 remaining = TARGET_PAGE_SIZE;
1757 }
1758 }
1759 }
1760 if (p != top) {
1761 memcpy_to_target(top, scratch, p - top);
1762 }
1763 }
1764
1765 return p;
1766 }
1767
1768 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1769 * argument/environment space. Newer kernels (>2.6.33) allow more,
1770 * dependent on stack size, but guarantee at least 32 pages for
1771 * backwards compatibility.
1772 */
1773 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1774
1775 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
1776 struct image_info *info)
1777 {
1778 abi_ulong size, error, guard;
1779
1780 size = guest_stack_size;
1781 if (size < STACK_LOWER_LIMIT) {
1782 size = STACK_LOWER_LIMIT;
1783 }
1784 guard = TARGET_PAGE_SIZE;
1785 if (guard < qemu_real_host_page_size) {
1786 guard = qemu_real_host_page_size;
1787 }
1788
1789 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1790 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1791 if (error == -1) {
1792 perror("mmap stack");
1793 exit(-1);
1794 }
1795
1796 /* We reserve one extra page at the top of the stack as guard. */
1797 if (STACK_GROWS_DOWN) {
1798 target_mprotect(error, guard, PROT_NONE);
1799 info->stack_limit = error + guard;
1800 return info->stack_limit + size - sizeof(void *);
1801 } else {
1802 target_mprotect(error + size, guard, PROT_NONE);
1803 info->stack_limit = error + size;
1804 return error;
1805 }
1806 }
1807
1808 /* Map and zero the bss. We need to explicitly zero any fractional pages
1809 after the data section (i.e. bss). */
1810 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1811 {
1812 uintptr_t host_start, host_map_start, host_end;
1813
1814 last_bss = TARGET_PAGE_ALIGN(last_bss);
1815
1816 /* ??? There is confusion between qemu_real_host_page_size and
1817 qemu_host_page_size here and elsewhere in target_mmap, which
1818 may lead to the end of the data section mapping from the file
1819 not being mapped. At least there was an explicit test and
1820 comment for that here, suggesting that "the file size must
1821 be known". The comment probably pre-dates the introduction
1822 of the fstat system call in target_mmap which does in fact
1823 find out the size. What isn't clear is if the workaround
1824 here is still actually needed. For now, continue with it,
1825 but merge it with the "normal" mmap that would allocate the bss. */
1826
1827 host_start = (uintptr_t) g2h(elf_bss);
1828 host_end = (uintptr_t) g2h(last_bss);
1829 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
1830
1831 if (host_map_start < host_end) {
1832 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1833 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1834 if (p == MAP_FAILED) {
1835 perror("cannot mmap brk");
1836 exit(-1);
1837 }
1838 }
1839
1840 /* Ensure that the bss page(s) are valid */
1841 if ((page_get_flags(last_bss-1) & prot) != prot) {
1842 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1843 }
1844
1845 if (host_start < host_map_start) {
1846 memset((void *)host_start, 0, host_map_start - host_start);
1847 }
1848 }
1849
1850 #ifdef TARGET_ARM
1851 static int elf_is_fdpic(struct elfhdr *exec)
1852 {
1853 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
1854 }
1855 #else
1856 /* Default implementation, always false. */
1857 static int elf_is_fdpic(struct elfhdr *exec)
1858 {
1859 return 0;
1860 }
1861 #endif
1862
1863 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1864 {
1865 uint16_t n;
1866 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1867
1868 /* elf32_fdpic_loadseg */
1869 n = info->nsegs;
1870 while (n--) {
1871 sp -= 12;
1872 put_user_u32(loadsegs[n].addr, sp+0);
1873 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1874 put_user_u32(loadsegs[n].p_memsz, sp+8);
1875 }
1876
1877 /* elf32_fdpic_loadmap */
1878 sp -= 4;
1879 put_user_u16(0, sp+0); /* version */
1880 put_user_u16(info->nsegs, sp+2); /* nsegs */
1881
1882 info->personality = PER_LINUX_FDPIC;
1883 info->loadmap_addr = sp;
1884
1885 return sp;
1886 }
1887
1888 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1889 struct elfhdr *exec,
1890 struct image_info *info,
1891 struct image_info *interp_info)
1892 {
1893 abi_ulong sp;
1894 abi_ulong u_argc, u_argv, u_envp, u_auxv;
1895 int size;
1896 int i;
1897 abi_ulong u_rand_bytes;
1898 uint8_t k_rand_bytes[16];
1899 abi_ulong u_platform;
1900 const char *k_platform;
1901 const int n = sizeof(elf_addr_t);
1902
1903 sp = p;
1904
1905 /* Needs to be before we load the env/argc/... */
1906 if (elf_is_fdpic(exec)) {
1907 /* Need 4 byte alignment for these structs */
1908 sp &= ~3;
1909 sp = loader_build_fdpic_loadmap(info, sp);
1910 info->other_info = interp_info;
1911 if (interp_info) {
1912 interp_info->other_info = info;
1913 sp = loader_build_fdpic_loadmap(interp_info, sp);
1914 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
1915 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
1916 } else {
1917 info->interpreter_loadmap_addr = 0;
1918 info->interpreter_pt_dynamic_addr = 0;
1919 }
1920 }
1921
1922 u_platform = 0;
1923 k_platform = ELF_PLATFORM;
1924 if (k_platform) {
1925 size_t len = strlen(k_platform) + 1;
1926 if (STACK_GROWS_DOWN) {
1927 sp -= (len + n - 1) & ~(n - 1);
1928 u_platform = sp;
1929 /* FIXME - check return value of memcpy_to_target() for failure */
1930 memcpy_to_target(sp, k_platform, len);
1931 } else {
1932 memcpy_to_target(sp, k_platform, len);
1933 u_platform = sp;
1934 sp += len + 1;
1935 }
1936 }
1937
1938 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1939 * the argv and envp pointers.
1940 */
1941 if (STACK_GROWS_DOWN) {
1942 sp = QEMU_ALIGN_DOWN(sp, 16);
1943 } else {
1944 sp = QEMU_ALIGN_UP(sp, 16);
1945 }
1946
1947 /*
1948 * Generate 16 random bytes for userspace PRNG seeding.
1949 */
1950 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
1951 if (STACK_GROWS_DOWN) {
1952 sp -= 16;
1953 u_rand_bytes = sp;
1954 /* FIXME - check return value of memcpy_to_target() for failure */
1955 memcpy_to_target(sp, k_rand_bytes, 16);
1956 } else {
1957 memcpy_to_target(sp, k_rand_bytes, 16);
1958 u_rand_bytes = sp;
1959 sp += 16;
1960 }
1961
1962 size = (DLINFO_ITEMS + 1) * 2;
1963 if (k_platform)
1964 size += 2;
1965 #ifdef DLINFO_ARCH_ITEMS
1966 size += DLINFO_ARCH_ITEMS * 2;
1967 #endif
1968 #ifdef ELF_HWCAP2
1969 size += 2;
1970 #endif
1971 info->auxv_len = size * n;
1972
1973 size += envc + argc + 2;
1974 size += 1; /* argc itself */
1975 size *= n;
1976
1977 /* Allocate space and finalize stack alignment for entry now. */
1978 if (STACK_GROWS_DOWN) {
1979 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
1980 sp = u_argc;
1981 } else {
1982 u_argc = sp;
1983 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
1984 }
1985
1986 u_argv = u_argc + n;
1987 u_envp = u_argv + (argc + 1) * n;
1988 u_auxv = u_envp + (envc + 1) * n;
1989 info->saved_auxv = u_auxv;
1990 info->arg_start = u_argv;
1991 info->arg_end = u_argv + argc * n;
1992
1993 /* This is correct because Linux defines
1994 * elf_addr_t as Elf32_Off / Elf64_Off
1995 */
1996 #define NEW_AUX_ENT(id, val) do { \
1997 put_user_ual(id, u_auxv); u_auxv += n; \
1998 put_user_ual(val, u_auxv); u_auxv += n; \
1999 } while(0)
2000
2001 #ifdef ARCH_DLINFO
2002 /*
2003 * ARCH_DLINFO must come first so platform specific code can enforce
2004 * special alignment requirements on the AUXV if necessary (eg. PPC).
2005 */
2006 ARCH_DLINFO;
2007 #endif
2008 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2009 * on info->auxv_len will trigger.
2010 */
2011 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2012 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2013 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2014 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2015 /* Target doesn't support host page size alignment */
2016 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2017 } else {
2018 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2019 qemu_host_page_size)));
2020 }
2021 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2022 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2023 NEW_AUX_ENT(AT_ENTRY, info->entry);
2024 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2025 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2026 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2027 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2028 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2029 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2030 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2031 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2032
2033 #ifdef ELF_HWCAP2
2034 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2035 #endif
2036
2037 if (u_platform) {
2038 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2039 }
2040 NEW_AUX_ENT (AT_NULL, 0);
2041 #undef NEW_AUX_ENT
2042
2043 /* Check that our initial calculation of the auxv length matches how much
2044 * we actually put into it.
2045 */
2046 assert(info->auxv_len == u_auxv - info->saved_auxv);
2047
2048 put_user_ual(argc, u_argc);
2049
2050 p = info->arg_strings;
2051 for (i = 0; i < argc; ++i) {
2052 put_user_ual(p, u_argv);
2053 u_argv += n;
2054 p += target_strlen(p) + 1;
2055 }
2056 put_user_ual(0, u_argv);
2057
2058 p = info->env_strings;
2059 for (i = 0; i < envc; ++i) {
2060 put_user_ual(p, u_envp);
2061 u_envp += n;
2062 p += target_strlen(p) + 1;
2063 }
2064 put_user_ual(0, u_envp);
2065
2066 return sp;
2067 }
2068
2069 unsigned long init_guest_space(unsigned long host_start,
2070 unsigned long host_size,
2071 unsigned long guest_start,
2072 bool fixed)
2073 {
2074 /* In order to use host shmat, we must be able to honor SHMLBA. */
2075 unsigned long align = MAX(SHMLBA, qemu_host_page_size);
2076 unsigned long current_start, aligned_start;
2077 int flags;
2078
2079 assert(host_start || host_size);
2080
2081 /* If just a starting address is given, then just verify that
2082 * address. */
2083 if (host_start && !host_size) {
2084 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2085 if (init_guest_commpage(host_start, host_size) != 1) {
2086 return (unsigned long)-1;
2087 }
2088 #endif
2089 return host_start;
2090 }
2091
2092 /* Setup the initial flags and start address. */
2093 current_start = host_start & -align;
2094 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2095 if (fixed) {
2096 flags |= MAP_FIXED;
2097 }
2098
2099 /* Otherwise, a non-zero size region of memory needs to be mapped
2100 * and validated. */
2101
2102 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2103 /* On 32-bit ARM, we need to map not just the usable memory, but
2104 * also the commpage. Try to find a suitable place by allocating
2105 * a big chunk for all of it. If host_start, then the naive
2106 * strategy probably does good enough.
2107 */
2108 if (!host_start) {
2109 unsigned long guest_full_size, host_full_size, real_start;
2110
2111 guest_full_size =
2112 (0xffff0f00 & qemu_host_page_mask) + qemu_host_page_size;
2113 host_full_size = guest_full_size - guest_start;
2114 real_start = (unsigned long)
2115 mmap(NULL, host_full_size, PROT_NONE, flags, -1, 0);
2116 if (real_start == (unsigned long)-1) {
2117 if (host_size < host_full_size - qemu_host_page_size) {
2118 /* We failed to map a continous segment, but we're
2119 * allowed to have a gap between the usable memory and
2120 * the commpage where other things can be mapped.
2121 * This sparseness gives us more flexibility to find
2122 * an address range.
2123 */
2124 goto naive;
2125 }
2126 return (unsigned long)-1;
2127 }
2128 munmap((void *)real_start, host_full_size);
2129 if (real_start & (align - 1)) {
2130 /* The same thing again, but with extra
2131 * so that we can shift around alignment.
2132 */
2133 unsigned long real_size = host_full_size + qemu_host_page_size;
2134 real_start = (unsigned long)
2135 mmap(NULL, real_size, PROT_NONE, flags, -1, 0);
2136 if (real_start == (unsigned long)-1) {
2137 if (host_size < host_full_size - qemu_host_page_size) {
2138 goto naive;
2139 }
2140 return (unsigned long)-1;
2141 }
2142 munmap((void *)real_start, real_size);
2143 real_start = ROUND_UP(real_start, align);
2144 }
2145 current_start = real_start;
2146 }
2147 naive:
2148 #endif
2149
2150 while (1) {
2151 unsigned long real_start, real_size, aligned_size;
2152 aligned_size = real_size = host_size;
2153
2154 /* Do not use mmap_find_vma here because that is limited to the
2155 * guest address space. We are going to make the
2156 * guest address space fit whatever we're given.
2157 */
2158 real_start = (unsigned long)
2159 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
2160 if (real_start == (unsigned long)-1) {
2161 return (unsigned long)-1;
2162 }
2163
2164 /* Check to see if the address is valid. */
2165 if (host_start && real_start != current_start) {
2166 goto try_again;
2167 }
2168
2169 /* Ensure the address is properly aligned. */
2170 if (real_start & (align - 1)) {
2171 /* Ideally, we adjust like
2172 *
2173 * pages: [ ][ ][ ][ ][ ]
2174 * old: [ real ]
2175 * [ aligned ]
2176 * new: [ real ]
2177 * [ aligned ]
2178 *
2179 * But if there is something else mapped right after it,
2180 * then obviously it won't have room to grow, and the
2181 * kernel will put the new larger real someplace else with
2182 * unknown alignment (if we made it to here, then
2183 * fixed=false). Which is why we grow real by a full page
2184 * size, instead of by part of one; so that even if we get
2185 * moved, we can still guarantee alignment. But this does
2186 * mean that there is a padding of < 1 page both before
2187 * and after the aligned range; the "after" could could
2188 * cause problems for ARM emulation where it could butt in
2189 * to where we need to put the commpage.
2190 */
2191 munmap((void *)real_start, host_size);
2192 real_size = aligned_size + qemu_host_page_size;
2193 real_start = (unsigned long)
2194 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
2195 if (real_start == (unsigned long)-1) {
2196 return (unsigned long)-1;
2197 }
2198 aligned_start = ROUND_UP(real_start, align);
2199 } else {
2200 aligned_start = real_start;
2201 }
2202
2203 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
2204 /* On 32-bit ARM, we need to also be able to map the commpage. */
2205 int valid = init_guest_commpage(aligned_start - guest_start,
2206 aligned_size + guest_start);
2207 if (valid == -1) {
2208 munmap((void *)real_start, real_size);
2209 return (unsigned long)-1;
2210 } else if (valid == 0) {
2211 goto try_again;
2212 }
2213 #endif
2214
2215 /* If nothing has said `return -1` or `goto try_again` yet,
2216 * then the address we have is good.
2217 */
2218 break;
2219
2220 try_again:
2221 /* That address didn't work. Unmap and try a different one.
2222 * The address the host picked because is typically right at
2223 * the top of the host address space and leaves the guest with
2224 * no usable address space. Resort to a linear search. We
2225 * already compensated for mmap_min_addr, so this should not
2226 * happen often. Probably means we got unlucky and host
2227 * address space randomization put a shared library somewhere
2228 * inconvenient.
2229 *
2230 * This is probably a good strategy if host_start, but is
2231 * probably a bad strategy if not, which means we got here
2232 * because of trouble with ARM commpage setup.
2233 */
2234 munmap((void *)real_start, real_size);
2235 current_start += align;
2236 if (host_start == current_start) {
2237 /* Theoretically possible if host doesn't have any suitably
2238 * aligned areas. Normally the first mmap will fail.
2239 */
2240 return (unsigned long)-1;
2241 }
2242 }
2243
2244 qemu_log_mask(CPU_LOG_PAGE, "Reserved 0x%lx bytes of guest address space\n", host_size);
2245
2246 return aligned_start;
2247 }
2248
2249 static void probe_guest_base(const char *image_name,
2250 abi_ulong loaddr, abi_ulong hiaddr)
2251 {
2252 /* Probe for a suitable guest base address, if the user has not set
2253 * it explicitly, and set guest_base appropriately.
2254 * In case of error we will print a suitable message and exit.
2255 */
2256 const char *errmsg;
2257 if (!have_guest_base && !reserved_va) {
2258 unsigned long host_start, real_start, host_size;
2259
2260 /* Round addresses to page boundaries. */
2261 loaddr &= qemu_host_page_mask;
2262 hiaddr = HOST_PAGE_ALIGN(hiaddr);
2263
2264 if (loaddr < mmap_min_addr) {
2265 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
2266 } else {
2267 host_start = loaddr;
2268 if (host_start != loaddr) {
2269 errmsg = "Address overflow loading ELF binary";
2270 goto exit_errmsg;
2271 }
2272 }
2273 host_size = hiaddr - loaddr;
2274
2275 /* Setup the initial guest memory space with ranges gleaned from
2276 * the ELF image that is being loaded.
2277 */
2278 real_start = init_guest_space(host_start, host_size, loaddr, false);
2279 if (real_start == (unsigned long)-1) {
2280 errmsg = "Unable to find space for application";
2281 goto exit_errmsg;
2282 }
2283 guest_base = real_start - loaddr;
2284
2285 qemu_log_mask(CPU_LOG_PAGE, "Relocating guest address space from 0x"
2286 TARGET_ABI_FMT_lx " to 0x%lx\n",
2287 loaddr, real_start);
2288 }
2289 return;
2290
2291 exit_errmsg:
2292 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2293 exit(-1);
2294 }
2295
2296
2297 /* Load an ELF image into the address space.
2298
2299 IMAGE_NAME is the filename of the image, to use in error messages.
2300 IMAGE_FD is the open file descriptor for the image.
2301
2302 BPRM_BUF is a copy of the beginning of the file; this of course
2303 contains the elf file header at offset 0. It is assumed that this
2304 buffer is sufficiently aligned to present no problems to the host
2305 in accessing data at aligned offsets within the buffer.
2306
2307 On return: INFO values will be filled in, as necessary or available. */
2308
2309 static void load_elf_image(const char *image_name, int image_fd,
2310 struct image_info *info, char **pinterp_name,
2311 char bprm_buf[BPRM_BUF_SIZE])
2312 {
2313 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2314 struct elf_phdr *phdr;
2315 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
2316 int i, retval;
2317 const char *errmsg;
2318
2319 /* First of all, some simple consistency checks */
2320 errmsg = "Invalid ELF image for this architecture";
2321 if (!elf_check_ident(ehdr)) {
2322 goto exit_errmsg;
2323 }
2324 bswap_ehdr(ehdr);
2325 if (!elf_check_ehdr(ehdr)) {
2326 goto exit_errmsg;
2327 }
2328
2329 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2330 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2331 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
2332 } else {
2333 phdr = (struct elf_phdr *) alloca(i);
2334 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
2335 if (retval != i) {
2336 goto exit_read;
2337 }
2338 }
2339 bswap_phdr(phdr, ehdr->e_phnum);
2340
2341 info->nsegs = 0;
2342 info->pt_dynamic_addr = 0;
2343
2344 mmap_lock();
2345
2346 /* Find the maximum size of the image and allocate an appropriate
2347 amount of memory to handle that. */
2348 loaddr = -1, hiaddr = 0;
2349 info->alignment = 0;
2350 for (i = 0; i < ehdr->e_phnum; ++i) {
2351 if (phdr[i].p_type == PT_LOAD) {
2352 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
2353 if (a < loaddr) {
2354 loaddr = a;
2355 }
2356 a = phdr[i].p_vaddr + phdr[i].p_memsz;
2357 if (a > hiaddr) {
2358 hiaddr = a;
2359 }
2360 ++info->nsegs;
2361 info->alignment |= phdr[i].p_align;
2362 }
2363 }
2364
2365 load_addr = loaddr;
2366 if (ehdr->e_type == ET_DYN) {
2367 /* The image indicates that it can be loaded anywhere. Find a
2368 location that can hold the memory space required. If the
2369 image is pre-linked, LOADDR will be non-zero. Since we do
2370 not supply MAP_FIXED here we'll use that address if and
2371 only if it remains available. */
2372 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2373 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
2374 -1, 0);
2375 if (load_addr == -1) {
2376 goto exit_perror;
2377 }
2378 } else if (pinterp_name != NULL) {
2379 /* This is the main executable. Make sure that the low
2380 address does not conflict with MMAP_MIN_ADDR or the
2381 QEMU application itself. */
2382 probe_guest_base(image_name, loaddr, hiaddr);
2383 }
2384 load_bias = load_addr - loaddr;
2385
2386 if (elf_is_fdpic(ehdr)) {
2387 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
2388 g_malloc(sizeof(*loadsegs) * info->nsegs);
2389
2390 for (i = 0; i < ehdr->e_phnum; ++i) {
2391 switch (phdr[i].p_type) {
2392 case PT_DYNAMIC:
2393 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2394 break;
2395 case PT_LOAD:
2396 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2397 loadsegs->p_vaddr = phdr[i].p_vaddr;
2398 loadsegs->p_memsz = phdr[i].p_memsz;
2399 ++loadsegs;
2400 break;
2401 }
2402 }
2403 }
2404
2405 info->load_bias = load_bias;
2406 info->load_addr = load_addr;
2407 info->entry = ehdr->e_entry + load_bias;
2408 info->start_code = -1;
2409 info->end_code = 0;
2410 info->start_data = -1;
2411 info->end_data = 0;
2412 info->brk = 0;
2413 info->elf_flags = ehdr->e_flags;
2414
2415 for (i = 0; i < ehdr->e_phnum; i++) {
2416 struct elf_phdr *eppnt = phdr + i;
2417 if (eppnt->p_type == PT_LOAD) {
2418 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
2419 int elf_prot = 0;
2420
2421 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
2422 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
2423 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
2424
2425 vaddr = load_bias + eppnt->p_vaddr;
2426 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2427 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
2428 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
2429
2430 /*
2431 * Some segments may be completely empty without any backing file
2432 * segment, in that case just let zero_bss allocate an empty buffer
2433 * for it.
2434 */
2435 if (eppnt->p_filesz != 0) {
2436 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2437 MAP_PRIVATE | MAP_FIXED,
2438 image_fd, eppnt->p_offset - vaddr_po);
2439
2440 if (error == -1) {
2441 goto exit_perror;
2442 }
2443 }
2444
2445 vaddr_ef = vaddr + eppnt->p_filesz;
2446 vaddr_em = vaddr + eppnt->p_memsz;
2447
2448 /* If the load segment requests extra zeros (e.g. bss), map it. */
2449 if (vaddr_ef < vaddr_em) {
2450 zero_bss(vaddr_ef, vaddr_em, elf_prot);
2451 }
2452
2453 /* Find the full program boundaries. */
2454 if (elf_prot & PROT_EXEC) {
2455 if (vaddr < info->start_code) {
2456 info->start_code = vaddr;
2457 }
2458 if (vaddr_ef > info->end_code) {
2459 info->end_code = vaddr_ef;
2460 }
2461 }
2462 if (elf_prot & PROT_WRITE) {
2463 if (vaddr < info->start_data) {
2464 info->start_data = vaddr;
2465 }
2466 if (vaddr_ef > info->end_data) {
2467 info->end_data = vaddr_ef;
2468 }
2469 if (vaddr_em > info->brk) {
2470 info->brk = vaddr_em;
2471 }
2472 }
2473 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2474 char *interp_name;
2475
2476 if (*pinterp_name) {
2477 errmsg = "Multiple PT_INTERP entries";
2478 goto exit_errmsg;
2479 }
2480 interp_name = malloc(eppnt->p_filesz);
2481 if (!interp_name) {
2482 goto exit_perror;
2483 }
2484
2485 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2486 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2487 eppnt->p_filesz);
2488 } else {
2489 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2490 eppnt->p_offset);
2491 if (retval != eppnt->p_filesz) {
2492 goto exit_perror;
2493 }
2494 }
2495 if (interp_name[eppnt->p_filesz - 1] != 0) {
2496 errmsg = "Invalid PT_INTERP entry";
2497 goto exit_errmsg;
2498 }
2499 *pinterp_name = interp_name;
2500 #ifdef TARGET_MIPS
2501 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
2502 Mips_elf_abiflags_v0 abiflags;
2503 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
2504 errmsg = "Invalid PT_MIPS_ABIFLAGS entry";
2505 goto exit_errmsg;
2506 }
2507 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2508 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
2509 sizeof(Mips_elf_abiflags_v0));
2510 } else {
2511 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
2512 eppnt->p_offset);
2513 if (retval != sizeof(Mips_elf_abiflags_v0)) {
2514 goto exit_perror;
2515 }
2516 }
2517 bswap_mips_abiflags(&abiflags);
2518 info->fp_abi = abiflags.fp_abi;
2519 #endif
2520 }
2521 }
2522
2523 if (info->end_data == 0) {
2524 info->start_data = info->end_code;
2525 info->end_data = info->end_code;
2526 info->brk = info->end_code;
2527 }
2528
2529 if (qemu_log_enabled()) {
2530 load_symbols(ehdr, image_fd, load_bias);
2531 }
2532
2533 mmap_unlock();
2534
2535 close(image_fd);
2536 return;
2537
2538 exit_read:
2539 if (retval >= 0) {
2540 errmsg = "Incomplete read of file header";
2541 goto exit_errmsg;
2542 }
2543 exit_perror:
2544 errmsg = strerror(errno);
2545 exit_errmsg:
2546 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2547 exit(-1);
2548 }
2549
2550 static void load_elf_interp(const char *filename, struct image_info *info,
2551 char bprm_buf[BPRM_BUF_SIZE])
2552 {
2553 int fd, retval;
2554
2555 fd = open(path(filename), O_RDONLY);
2556 if (fd < 0) {
2557 goto exit_perror;
2558 }
2559
2560 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2561 if (retval < 0) {
2562 goto exit_perror;
2563 }
2564 if (retval < BPRM_BUF_SIZE) {
2565 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2566 }
2567
2568 load_elf_image(filename, fd, info, NULL, bprm_buf);
2569 return;
2570
2571 exit_perror:
2572 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2573 exit(-1);
2574 }
2575
2576 static int symfind(const void *s0, const void *s1)
2577 {
2578 target_ulong addr = *(target_ulong *)s0;
2579 struct elf_sym *sym = (struct elf_sym *)s1;
2580 int result = 0;
2581 if (addr < sym->st_value) {
2582 result = -1;
2583 } else if (addr >= sym->st_value + sym->st_size) {
2584 result = 1;
2585 }
2586 return result;
2587 }
2588
2589 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2590 {
2591 #if ELF_CLASS == ELFCLASS32
2592 struct elf_sym *syms = s->disas_symtab.elf32;
2593 #else
2594 struct elf_sym *syms = s->disas_symtab.elf64;
2595 #endif
2596
2597 // binary search
2598 struct elf_sym *sym;
2599
2600 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2601 if (sym != NULL) {
2602 return s->disas_strtab + sym->st_name;
2603 }
2604
2605 return "";
2606 }
2607
2608 /* FIXME: This should use elf_ops.h */
2609 static int symcmp(const void *s0, const void *s1)
2610 {
2611 struct elf_sym *sym0 = (struct elf_sym *)s0;
2612 struct elf_sym *sym1 = (struct elf_sym *)s1;
2613 return (sym0->st_value < sym1->st_value)
2614 ? -1
2615 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2616 }
2617
2618 /* Best attempt to load symbols from this ELF object. */
2619 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2620 {
2621 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2622 uint64_t segsz;
2623 struct elf_shdr *shdr;
2624 char *strings = NULL;
2625 struct syminfo *s = NULL;
2626 struct elf_sym *new_syms, *syms = NULL;
2627
2628 shnum = hdr->e_shnum;
2629 i = shnum * sizeof(struct elf_shdr);
2630 shdr = (struct elf_shdr *)alloca(i);
2631 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2632 return;
2633 }
2634
2635 bswap_shdr(shdr, shnum);
2636 for (i = 0; i < shnum; ++i) {
2637 if (shdr[i].sh_type == SHT_SYMTAB) {
2638 sym_idx = i;
2639 str_idx = shdr[i].sh_link;
2640 goto found;
2641 }
2642 }
2643
2644 /* There will be no symbol table if the file was stripped. */
2645 return;
2646
2647 found:
2648 /* Now know where the strtab and symtab are. Snarf them. */
2649 s = g_try_new(struct syminfo, 1);
2650 if (!s) {
2651 goto give_up;
2652 }
2653
2654 segsz = shdr[str_idx].sh_size;
2655 s->disas_strtab = strings = g_try_malloc(segsz);
2656 if (!strings ||
2657 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
2658 goto give_up;
2659 }
2660
2661 segsz = shdr[sym_idx].sh_size;
2662 syms = g_try_malloc(segsz);
2663 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
2664 goto give_up;
2665 }
2666
2667 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
2668 /* Implausibly large symbol table: give up rather than ploughing
2669 * on with the number of symbols calculation overflowing
2670 */
2671 goto give_up;
2672 }
2673 nsyms = segsz / sizeof(struct elf_sym);
2674 for (i = 0; i < nsyms; ) {
2675 bswap_sym(syms + i);
2676 /* Throw away entries which we do not need. */
2677 if (syms[i].st_shndx == SHN_UNDEF
2678 || syms[i].st_shndx >= SHN_LORESERVE
2679 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2680 if (i < --nsyms) {
2681 syms[i] = syms[nsyms];
2682 }
2683 } else {
2684 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2685 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2686 syms[i].st_value &= ~(target_ulong)1;
2687 #endif
2688 syms[i].st_value += load_bias;
2689 i++;
2690 }
2691 }
2692
2693 /* No "useful" symbol. */
2694 if (nsyms == 0) {
2695 goto give_up;
2696 }
2697
2698 /* Attempt to free the storage associated with the local symbols
2699 that we threw away. Whether or not this has any effect on the
2700 memory allocation depends on the malloc implementation and how
2701 many symbols we managed to discard. */
2702 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
2703 if (new_syms == NULL) {
2704 goto give_up;
2705 }
2706 syms = new_syms;
2707
2708 qsort(syms, nsyms, sizeof(*syms), symcmp);
2709
2710 s->disas_num_syms = nsyms;
2711 #if ELF_CLASS == ELFCLASS32
2712 s->disas_symtab.elf32 = syms;
2713 #else
2714 s->disas_symtab.elf64 = syms;
2715 #endif
2716 s->lookup_symbol = lookup_symbolxx;
2717 s->next = syminfos;
2718 syminfos = s;
2719
2720 return;
2721
2722 give_up:
2723 g_free(s);
2724 g_free(strings);
2725 g_free(syms);
2726 }
2727
2728 uint32_t get_elf_eflags(int fd)
2729 {
2730 struct elfhdr ehdr;
2731 off_t offset;
2732 int ret;
2733
2734 /* Read ELF header */
2735 offset = lseek(fd, 0, SEEK_SET);
2736 if (offset == (off_t) -1) {
2737 return 0;
2738 }
2739 ret = read(fd, &ehdr, sizeof(ehdr));
2740 if (ret < sizeof(ehdr)) {
2741 return 0;
2742 }
2743 offset = lseek(fd, offset, SEEK_SET);
2744 if (offset == (off_t) -1) {
2745 return 0;
2746 }
2747
2748 /* Check ELF signature */
2749 if (!elf_check_ident(&ehdr)) {
2750 return 0;
2751 }
2752
2753 /* check header */
2754 bswap_ehdr(&ehdr);
2755 if (!elf_check_ehdr(&ehdr)) {
2756 return 0;
2757 }
2758
2759 /* return architecture id */
2760 return ehdr.e_flags;
2761 }
2762
2763 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2764 {
2765 struct image_info interp_info;
2766 struct elfhdr elf_ex;
2767 char *elf_interpreter = NULL;
2768 char *scratch;
2769
2770 memset(&interp_info, 0, sizeof(interp_info));
2771 #ifdef TARGET_MIPS
2772 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
2773 #endif
2774
2775 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2776
2777 load_elf_image(bprm->filename, bprm->fd, info,
2778 &elf_interpreter, bprm->buf);
2779
2780 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2781 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2782 when we load the interpreter. */
2783 elf_ex = *(struct elfhdr *)bprm->buf;
2784
2785 /* Do this so that we can load the interpreter, if need be. We will
2786 change some of these later */
2787 bprm->p = setup_arg_pages(bprm, info);
2788
2789 scratch = g_new0(char, TARGET_PAGE_SIZE);
2790 if (STACK_GROWS_DOWN) {
2791 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2792 bprm->p, info->stack_limit);
2793 info->file_string = bprm->p;
2794 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2795 bprm->p, info->stack_limit);
2796 info->env_strings = bprm->p;
2797 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2798 bprm->p, info->stack_limit);
2799 info->arg_strings = bprm->p;
2800 } else {
2801 info->arg_strings = bprm->p;
2802 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
2803 bprm->p, info->stack_limit);
2804 info->env_strings = bprm->p;
2805 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
2806 bprm->p, info->stack_limit);
2807 info->file_string = bprm->p;
2808 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
2809 bprm->p, info->stack_limit);
2810 }
2811
2812 g_free(scratch);
2813
2814 if (!bprm->p) {
2815 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2816 exit(-1);
2817 }
2818
2819 if (elf_interpreter) {
2820 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2821
2822 /* If the program interpreter is one of these two, then assume
2823 an iBCS2 image. Otherwise assume a native linux image. */
2824
2825 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2826 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2827 info->personality = PER_SVR4;
2828
2829 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2830 and some applications "depend" upon this behavior. Since
2831 we do not have the power to recompile these, we emulate
2832 the SVr4 behavior. Sigh. */
2833 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2834 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2835 }
2836 #ifdef TARGET_MIPS
2837 info->interp_fp_abi = interp_info.fp_abi;
2838 #endif
2839 }
2840
2841 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2842 info, (elf_interpreter ? &interp_info : NULL));
2843 info->start_stack = bprm->p;
2844
2845 /* If we have an interpreter, set that as the program's entry point.
2846 Copy the load_bias as well, to help PPC64 interpret the entry
2847 point as a function descriptor. Do this after creating elf tables
2848 so that we copy the original program entry point into the AUXV. */
2849 if (elf_interpreter) {
2850 info->load_bias = interp_info.load_bias;
2851 info->entry = interp_info.entry;
2852 free(elf_interpreter);
2853 }
2854
2855 #ifdef USE_ELF_CORE_DUMP
2856 bprm->core_dump = &elf_core_dump;
2857 #endif
2858
2859 return 0;
2860 }
2861
2862 #ifdef USE_ELF_CORE_DUMP
2863 /*
2864 * Definitions to generate Intel SVR4-like core files.
2865 * These mostly have the same names as the SVR4 types with "target_elf_"
2866 * tacked on the front to prevent clashes with linux definitions,
2867 * and the typedef forms have been avoided. This is mostly like
2868 * the SVR4 structure, but more Linuxy, with things that Linux does
2869 * not support and which gdb doesn't really use excluded.
2870 *
2871 * Fields we don't dump (their contents is zero) in linux-user qemu
2872 * are marked with XXX.
2873 *
2874 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2875 *
2876 * Porting ELF coredump for target is (quite) simple process. First you
2877 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2878 * the target resides):
2879 *
2880 * #define USE_ELF_CORE_DUMP
2881 *
2882 * Next you define type of register set used for dumping. ELF specification
2883 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2884 *
2885 * typedef <target_regtype> target_elf_greg_t;
2886 * #define ELF_NREG <number of registers>
2887 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2888 *
2889 * Last step is to implement target specific function that copies registers
2890 * from given cpu into just specified register set. Prototype is:
2891 *
2892 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2893 * const CPUArchState *env);
2894 *
2895 * Parameters:
2896 * regs - copy register values into here (allocated and zeroed by caller)
2897 * env - copy registers from here
2898 *
2899 * Example for ARM target is provided in this file.
2900 */
2901
2902 /* An ELF note in memory */
2903 struct memelfnote {
2904 const char *name;
2905 size_t namesz;
2906 size_t namesz_rounded;
2907 int type;
2908 size_t datasz;
2909 size_t datasz_rounded;
2910 void *data;
2911 size_t notesz;
2912 };
2913
2914 struct target_elf_siginfo {
2915 abi_int si_signo; /* signal number */
2916 abi_int si_code; /* extra code */
2917 abi_int si_errno; /* errno */
2918 };
2919
2920 struct target_elf_prstatus {
2921 struct target_elf_siginfo pr_info; /* Info associated with signal */
2922 abi_short pr_cursig; /* Current signal */
2923 abi_ulong pr_sigpend; /* XXX */
2924 abi_ulong pr_sighold; /* XXX */
2925 target_pid_t pr_pid;
2926 target_pid_t pr_ppid;
2927 target_pid_t pr_pgrp;
2928 target_pid_t pr_sid;
2929 struct target_timeval pr_utime; /* XXX User time */
2930 struct target_timeval pr_stime; /* XXX System time */
2931 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2932 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2933 target_elf_gregset_t pr_reg; /* GP registers */
2934 abi_int pr_fpvalid; /* XXX */
2935 };
2936
2937 #define ELF_PRARGSZ (80) /* Number of chars for args */
2938
2939 struct target_elf_prpsinfo {
2940 char pr_state; /* numeric process state */
2941 char pr_sname; /* char for pr_state */
2942 char pr_zomb; /* zombie */
2943 char pr_nice; /* nice val */
2944 abi_ulong pr_flag; /* flags */
2945 target_uid_t pr_uid;
2946 target_gid_t pr_gid;
2947 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2948 /* Lots missing */
2949 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
2950 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2951 };
2952
2953 /* Here is the structure in which status of each thread is captured. */
2954 struct elf_thread_status {
2955 QTAILQ_ENTRY(elf_thread_status) ets_link;
2956 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2957 #if 0
2958 elf_fpregset_t fpu; /* NT_PRFPREG */
2959 struct task_struct *thread;
2960 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2961 #endif
2962 struct memelfnote notes[1];
2963 int num_notes;
2964 };
2965
2966 struct elf_note_info {
2967 struct memelfnote *notes;
2968 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2969 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2970
2971 QTAILQ_HEAD(, elf_thread_status) thread_list;
2972 #if 0
2973 /*
2974 * Current version of ELF coredump doesn't support
2975 * dumping fp regs etc.
2976 */
2977 elf_fpregset_t *fpu;
2978 elf_fpxregset_t *xfpu;
2979 int thread_status_size;
2980 #endif
2981 int notes_size;
2982 int numnote;
2983 };
2984
2985 struct vm_area_struct {
2986 target_ulong vma_start; /* start vaddr of memory region */
2987 target_ulong vma_end; /* end vaddr of memory region */
2988 abi_ulong vma_flags; /* protection etc. flags for the region */
2989 QTAILQ_ENTRY(vm_area_struct) vma_link;
2990 };
2991
2992 struct mm_struct {
2993 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2994 int mm_count; /* number of mappings */
2995 };
2996
2997 static struct mm_struct *vma_init(void);
2998 static void vma_delete(struct mm_struct *);
2999 static int vma_add_mapping(struct mm_struct *, target_ulong,
3000 target_ulong, abi_ulong);
3001 static int vma_get_mapping_count(const struct mm_struct *);
3002 static struct vm_area_struct *vma_first(const struct mm_struct *);
3003 static struct vm_area_struct *vma_next(struct vm_area_struct *);
3004 static abi_ulong vma_dump_size(const struct vm_area_struct *);
3005 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3006 unsigned long flags);
3007
3008 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
3009 static void fill_note(struct memelfnote *, const char *, int,
3010 unsigned int, void *);
3011 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
3012 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
3013 static void fill_auxv_note(struct memelfnote *, const TaskState *);
3014 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
3015 static size_t note_size(const struct memelfnote *);
3016 static void free_note_info(struct elf_note_info *);
3017 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
3018 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
3019 static int core_dump_filename(const TaskState *, char *, size_t);
3020
3021 static int dump_write(int, const void *, size_t);
3022 static int write_note(struct memelfnote *, int);
3023 static int write_note_info(struct elf_note_info *, int);
3024
3025 #ifdef BSWAP_NEEDED
3026 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
3027 {
3028 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3029 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3030 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
3031 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
3032 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3033 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
3034 prstatus->pr_pid = tswap32(prstatus->pr_pid);
3035 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3036 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3037 prstatus->pr_sid = tswap32(prstatus->pr_sid);
3038 /* cpu times are not filled, so we skip them */
3039 /* regs should be in correct format already */
3040 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3041 }
3042
3043 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
3044 {
3045 psinfo->pr_flag = tswapal(psinfo->pr_flag);
3046 psinfo->pr_uid = tswap16(psinfo->pr_uid);
3047 psinfo->pr_gid = tswap16(psinfo->pr_gid);
3048 psinfo->pr_pid = tswap32(psinfo->pr_pid);
3049 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
3050 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
3051 psinfo->pr_sid = tswap32(psinfo->pr_sid);
3052 }
3053
3054 static void bswap_note(struct elf_note *en)
3055 {
3056 bswap32s(&en->n_namesz);
3057 bswap32s(&en->n_descsz);
3058 bswap32s(&en->n_type);
3059 }
3060 #else
3061 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
3062 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
3063 static inline void bswap_note(struct elf_note *en) { }
3064 #endif /* BSWAP_NEEDED */
3065
3066 /*
3067 * Minimal support for linux memory regions. These are needed
3068 * when we are finding out what memory exactly belongs to
3069 * emulated process. No locks needed here, as long as
3070 * thread that received the signal is stopped.
3071 */
3072
3073 static struct mm_struct *vma_init(void)
3074 {
3075 struct mm_struct *mm;
3076
3077 if ((mm = g_malloc(sizeof (*mm))) == NULL)
3078 return (NULL);
3079
3080 mm->mm_count = 0;
3081 QTAILQ_INIT(&mm->mm_mmap);
3082
3083 return (mm);
3084 }
3085
3086 static void vma_delete(struct mm_struct *mm)
3087 {
3088 struct vm_area_struct *vma;
3089
3090 while ((vma = vma_first(mm)) != NULL) {
3091 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
3092 g_free(vma);
3093 }
3094 g_free(mm);
3095 }
3096
3097 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3098 target_ulong end, abi_ulong flags)
3099 {
3100 struct vm_area_struct *vma;
3101
3102 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
3103 return (-1);
3104
3105 vma->vma_start = start;
3106 vma->vma_end = end;
3107 vma->vma_flags = flags;
3108
3109 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
3110 mm->mm_count++;
3111
3112 return (0);
3113 }
3114
3115 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3116 {
3117 return (QTAILQ_FIRST(&mm->mm_mmap));
3118 }
3119
3120 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3121 {
3122 return (QTAILQ_NEXT(vma, vma_link));
3123 }
3124
3125 static int vma_get_mapping_count(const struct mm_struct *mm)
3126 {
3127 return (mm->mm_count);
3128 }
3129
3130 /*
3131 * Calculate file (dump) size of given memory region.
3132 */
3133 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3134 {
3135 /* if we cannot even read the first page, skip it */
3136 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
3137 return (0);
3138
3139 /*
3140 * Usually we don't dump executable pages as they contain
3141 * non-writable code that debugger can read directly from
3142 * target library etc. However, thread stacks are marked
3143 * also executable so we read in first page of given region
3144 * and check whether it contains elf header. If there is
3145 * no elf header, we dump it.
3146 */
3147 if (vma->vma_flags & PROT_EXEC) {
3148 char page[TARGET_PAGE_SIZE];
3149
3150 copy_from_user(page, vma->vma_start, sizeof (page));
3151 if ((page[EI_MAG0] == ELFMAG0) &&
3152 (page[EI_MAG1] == ELFMAG1) &&
3153 (page[EI_MAG2] == ELFMAG2) &&
3154 (page[EI_MAG3] == ELFMAG3)) {
3155 /*
3156 * Mappings are possibly from ELF binary. Don't dump
3157 * them.
3158 */
3159 return (0);
3160 }
3161 }
3162
3163 return (vma->vma_end - vma->vma_start);
3164 }
3165
3166 static int vma_walker(void *priv, target_ulong start, target_ulong end,
3167 unsigned long flags)
3168 {
3169 struct mm_struct *mm = (struct mm_struct *)priv;
3170
3171 vma_add_mapping(mm, start, end, flags);
3172 return (0);
3173 }
3174
3175 static void fill_note(struct memelfnote *note, const char *name, int type,
3176 unsigned int sz, void *data)
3177 {
3178 unsigned int namesz;
3179
3180 namesz = strlen(name) + 1;
3181 note->name = name;
3182 note->namesz = namesz;
3183 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3184 note->type = type;
3185 note->datasz = sz;
3186 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3187
3188 note->data = data;
3189
3190 /*
3191 * We calculate rounded up note size here as specified by
3192 * ELF document.
3193 */
3194 note->notesz = sizeof (struct elf_note) +
3195 note->namesz_rounded + note->datasz_rounded;
3196 }
3197
3198 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
3199 uint32_t flags)
3200 {
3201 (void) memset(elf, 0, sizeof(*elf));
3202
3203 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
3204 elf->e_ident[EI_CLASS] = ELF_CLASS;
3205 elf->e_ident[EI_DATA] = ELF_DATA;
3206 elf->e_ident[EI_VERSION] = EV_CURRENT;
3207 elf->e_ident[EI_OSABI] = ELF_OSABI;
3208
3209 elf->e_type = ET_CORE;
3210 elf->e_machine = machine;
3211 elf->e_version = EV_CURRENT;
3212 elf->e_phoff = sizeof(struct elfhdr);
3213 elf->e_flags = flags;
3214 elf->e_ehsize = sizeof(struct elfhdr);
3215 elf->e_phentsize = sizeof(struct elf_phdr);
3216 elf->e_phnum = segs;
3217
3218 bswap_ehdr(elf);
3219 }
3220
3221 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
3222 {
3223 phdr->p_type = PT_NOTE;
3224 phdr->p_offset = offset;
3225 phdr->p_vaddr = 0;
3226 phdr->p_paddr = 0;
3227 phdr->p_filesz = sz;
3228 phdr->p_memsz = 0;
3229 phdr->p_flags = 0;
3230 phdr->p_align = 0;
3231
3232 bswap_phdr(phdr, 1);
3233 }
3234
3235 static size_t note_size(const struct memelfnote *note)
3236 {
3237 return (note->notesz);
3238 }
3239
3240 static void fill_prstatus(struct target_elf_prstatus *prstatus,
3241 const TaskState *ts, int signr)
3242 {
3243 (void) memset(prstatus, 0, sizeof (*prstatus));
3244 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
3245 prstatus->pr_pid = ts->ts_tid;
3246 prstatus->pr_ppid = getppid();
3247 prstatus->pr_pgrp = getpgrp();
3248 prstatus->pr_sid = getsid(0);
3249
3250 bswap_prstatus(prstatus);
3251 }
3252
3253 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
3254 {
3255 char *base_filename;
3256 unsigned int i, len;
3257
3258 (void) memset(psinfo, 0, sizeof (*psinfo));
3259
3260 len = ts->info->arg_end - ts->info->arg_start;
3261 if (len >= ELF_PRARGSZ)
3262 len = ELF_PRARGSZ - 1;
3263 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
3264 return -EFAULT;
3265 for (i = 0; i < len; i++)
3266 if (psinfo->pr_psargs[i] == 0)
3267 psinfo->pr_psargs[i] = ' ';
3268 psinfo->pr_psargs[len] = 0;
3269
3270 psinfo->pr_pid = getpid();
3271 psinfo->pr_ppid = getppid();
3272 psinfo->pr_pgrp = getpgrp();
3273 psinfo->pr_sid = getsid(0);
3274 psinfo->pr_uid = getuid();
3275 psinfo->pr_gid = getgid();
3276
3277 base_filename = g_path_get_basename(ts->bprm->filename);
3278 /*
3279 * Using strncpy here is fine: at max-length,
3280 * this field is not NUL-terminated.
3281 */
3282 (void) strncpy(psinfo->pr_fname, base_filename,
3283 sizeof(psinfo->pr_fname));
3284
3285 g_free(base_filename);
3286 bswap_psinfo(psinfo);
3287 return (0);
3288 }
3289
3290 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
3291 {
3292 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
3293 elf_addr_t orig_auxv = auxv;
3294 void *ptr;
3295 int len = ts->info->auxv_len;
3296
3297 /*
3298 * Auxiliary vector is stored in target process stack. It contains
3299 * {type, value} pairs that we need to dump into note. This is not
3300 * strictly necessary but we do it here for sake of completeness.
3301 */
3302
3303 /* read in whole auxv vector and copy it to memelfnote */
3304 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
3305 if (ptr != NULL) {
3306 fill_note(note, "CORE", NT_AUXV, len, ptr);
3307 unlock_user(ptr, auxv, len);
3308 }
3309 }
3310
3311 /*
3312 * Constructs name of coredump file. We have following convention
3313 * for the name:
3314 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3315 *
3316 * Returns 0 in case of success, -1 otherwise (errno is set).
3317 */
3318 static int core_dump_filename(const TaskState *ts, char *buf,
3319 size_t bufsize)
3320 {
3321 char timestamp[64];
3322 char *base_filename = NULL;
3323 struct timeval tv;
3324 struct tm tm;
3325
3326 assert(bufsize >= PATH_MAX);
3327
3328 if (gettimeofday(&tv, NULL) < 0) {
3329 (void) fprintf(stderr, "unable to get current timestamp: %s",
3330 strerror(errno));
3331 return (-1);
3332 }
3333
3334 base_filename = g_path_get_basename(ts->bprm->filename);
3335 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
3336 localtime_r(&tv.tv_sec, &tm));
3337 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
3338 base_filename, timestamp, (int)getpid());
3339 g_free(base_filename);
3340
3341 return (0);
3342 }
3343
3344 static int dump_write(int fd, const void *ptr, size_t size)
3345 {
3346 const char *bufp = (const char *)ptr;
3347 ssize_t bytes_written, bytes_left;
3348 struct rlimit dumpsize;
3349 off_t pos;
3350
3351 bytes_written = 0;
3352 getrlimit(RLIMIT_CORE, &dumpsize);
3353 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
3354 if (errno == ESPIPE) { /* not a seekable stream */
3355 bytes_left = size;
3356 } else {
3357 return pos;
3358 }
3359 } else {
3360 if (dumpsize.rlim_cur <= pos) {
3361 return -1;
3362 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
3363 bytes_left = size;
3364 } else {
3365 size_t limit_left=dumpsize.rlim_cur - pos;
3366 bytes_left = limit_left >= size ? size : limit_left ;
3367 }
3368 }
3369
3370 /*
3371 * In normal conditions, single write(2) should do but
3372 * in case of socket etc. this mechanism is more portable.
3373 */
3374 do {
3375 bytes_written = write(fd, bufp, bytes_left);
3376 if (bytes_written < 0) {
3377 if (errno == EINTR)
3378 continue;
3379 return (-1);
3380 } else if (bytes_written == 0) { /* eof */
3381 return (-1);
3382 }
3383 bufp += bytes_written;
3384 bytes_left -= bytes_written;
3385 } while (bytes_left > 0);
3386
3387 return (0);
3388 }
3389
3390 static int write_note(struct memelfnote *men, int fd)
3391 {
3392 struct elf_note en;
3393
3394 en.n_namesz = men->namesz;
3395 en.n_type = men->type;
3396 en.n_descsz = men->datasz;
3397
3398 bswap_note(&en);
3399
3400 if (dump_write(fd, &en, sizeof(en)) != 0)
3401 return (-1);
3402 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3403 return (-1);
3404 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
3405 return (-1);
3406
3407 return (0);
3408 }
3409
3410 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
3411 {
3412 CPUState *cpu = env_cpu((CPUArchState *)env);
3413 TaskState *ts = (TaskState *)cpu->opaque;
3414 struct elf_thread_status *ets;
3415
3416 ets = g_malloc0(sizeof (*ets));
3417 ets->num_notes = 1; /* only prstatus is dumped */
3418 fill_prstatus(&ets->prstatus, ts, 0);
3419 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3420 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
3421 &ets->prstatus);
3422
3423 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
3424
3425 info->notes_size += note_size(&ets->notes[0]);
3426 }
3427
3428 static void init_note_info(struct elf_note_info *info)
3429 {
3430 /* Initialize the elf_note_info structure so that it is at
3431 * least safe to call free_note_info() on it. Must be
3432 * called before calling fill_note_info().
3433 */
3434 memset(info, 0, sizeof (*info));
3435 QTAILQ_INIT(&info->thread_list);
3436 }
3437
3438 static int fill_note_info(struct elf_note_info *info,
3439 long signr, const CPUArchState *env)
3440 {
3441 #define NUMNOTES 3
3442 CPUState *cpu = env_cpu((CPUArchState *)env);
3443 TaskState *ts = (TaskState *)cpu->opaque;
3444 int i;
3445
3446 info->notes = g_new0(struct memelfnote, NUMNOTES);
3447 if (info->notes == NULL)
3448 return (-ENOMEM);
3449 info->prstatus = g_malloc0(sizeof (*info->prstatus));
3450 if (info->prstatus == NULL)
3451 return (-ENOMEM);
3452 info->psinfo = g_malloc0(sizeof (*info->psinfo));
3453 if (info->prstatus == NULL)
3454 return (-ENOMEM);
3455
3456 /*
3457 * First fill in status (and registers) of current thread
3458 * including process info & aux vector.
3459 */
3460 fill_prstatus(info->prstatus, ts, signr);
3461 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3462 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
3463 sizeof (*info->prstatus), info->prstatus);
3464 fill_psinfo(info->psinfo, ts);
3465 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
3466 sizeof (*info->psinfo), info->psinfo);
3467 fill_auxv_note(&info->notes[2], ts);
3468 info->numnote = 3;
3469
3470 info->notes_size = 0;
3471 for (i = 0; i < info->numnote; i++)
3472 info->notes_size += note_size(&info->notes[i]);
3473
3474 /* read and fill status of all threads */
3475 cpu_list_lock();
3476 CPU_FOREACH(cpu) {
3477 if (cpu == thread_cpu) {
3478 continue;
3479 }
3480 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
3481 }
3482 cpu_list_unlock();
3483
3484 return (0);
3485 }
3486
3487 static void free_note_info(struct elf_note_info *info)
3488 {
3489 struct elf_thread_status *ets;
3490
3491 while (!QTAILQ_EMPTY(&info->thread_list)) {
3492 ets = QTAILQ_FIRST(&info->thread_list);
3493 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
3494 g_free(ets);
3495 }
3496
3497 g_free(info->prstatus);
3498 g_free(info->psinfo);
3499 g_free(info->notes);
3500 }
3501
3502 static int write_note_info(struct elf_note_info *info, int fd)
3503 {
3504 struct elf_thread_status *ets;
3505 int i, error = 0;
3506
3507 /* write prstatus, psinfo and auxv for current thread */
3508 for (i = 0; i < info->numnote; i++)
3509 if ((error = write_note(&info->notes[i], fd)) != 0)
3510 return (error);
3511
3512 /* write prstatus for each thread */
3513 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
3514 if ((error = write_note(&ets->notes[0], fd)) != 0)
3515 return (error);
3516 }
3517
3518 return (0);
3519 }
3520
3521 /*
3522 * Write out ELF coredump.
3523 *
3524 * See documentation of ELF object file format in:
3525 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3526 *
3527 * Coredump format in linux is following:
3528 *
3529 * 0 +----------------------+ \
3530 * | ELF header | ET_CORE |
3531 * +----------------------+ |
3532 * | ELF program headers | |--- headers
3533 * | - NOTE section | |
3534 * | - PT_LOAD sections | |
3535 * +----------------------+ /
3536 * | NOTEs: |
3537 * | - NT_PRSTATUS |
3538 * | - NT_PRSINFO |
3539 * | - NT_AUXV |
3540 * +----------------------+ <-- aligned to target page
3541 * | Process memory dump |
3542 * : :
3543 * . .
3544 * : :
3545 * | |
3546 * +----------------------+
3547 *
3548 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3549 * NT_PRSINFO -> struct elf_prpsinfo
3550 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3551 *
3552 * Format follows System V format as close as possible. Current
3553 * version limitations are as follows:
3554 * - no floating point registers are dumped
3555 *
3556 * Function returns 0 in case of success, negative errno otherwise.
3557 *
3558 * TODO: make this work also during runtime: it should be
3559 * possible to force coredump from running process and then
3560 * continue processing. For example qemu could set up SIGUSR2
3561 * handler (provided that target process haven't registered
3562 * handler for that) that does the dump when signal is received.
3563 */
3564 static int elf_core_dump(int signr, const CPUArchState *env)
3565 {
3566 const CPUState *cpu = env_cpu((CPUArchState *)env);
3567 const TaskState *ts = (const TaskState *)cpu->opaque;
3568 struct vm_area_struct *vma = NULL;
3569 char corefile[PATH_MAX];
3570 struct elf_note_info info;
3571 struct elfhdr elf;
3572 struct elf_phdr phdr;
3573 struct rlimit dumpsize;
3574 struct mm_struct *mm = NULL;
3575 off_t offset = 0, data_offset = 0;
3576 int segs = 0;
3577 int fd = -1;
3578
3579 init_note_info(&info);
3580
3581 errno = 0;
3582 getrlimit(RLIMIT_CORE, &dumpsize);
3583 if (dumpsize.rlim_cur == 0)
3584 return 0;
3585
3586 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
3587 return (-errno);
3588
3589 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3590 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3591 return (-errno);
3592
3593 /*
3594 * Walk through target process memory mappings and
3595 * set up structure containing this information. After
3596 * this point vma_xxx functions can be used.
3597 */
3598 if ((mm = vma_init()) == NULL)
3599 goto out;
3600
3601 walk_memory_regions(mm, vma_walker);
3602 segs = vma_get_mapping_count(mm);
3603
3604 /*
3605 * Construct valid coredump ELF header. We also
3606 * add one more segment for notes.
3607 */
3608 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3609 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3610 goto out;
3611
3612 /* fill in the in-memory version of notes */
3613 if (fill_note_info(&info, signr, env) < 0)
3614 goto out;
3615
3616 offset += sizeof (elf); /* elf header */
3617 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3618
3619 /* write out notes program header */
3620 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3621
3622 offset += info.notes_size;
3623 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3624 goto out;
3625
3626 /*
3627 * ELF specification wants data to start at page boundary so
3628 * we align it here.
3629 */
3630 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3631
3632 /*
3633 * Write program headers for memory regions mapped in
3634 * the target process.
3635 */
3636 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3637 (void) memset(&phdr, 0, sizeof (phdr));
3638
3639 phdr.p_type = PT_LOAD;
3640 phdr.p_offset = offset;
3641 phdr.p_vaddr = vma->vma_start;
3642 phdr.p_paddr = 0;
3643 phdr.p_filesz = vma_dump_size(vma);
3644 offset += phdr.p_filesz;
3645 phdr.p_memsz = vma->vma_end - vma->vma_start;
3646 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3647 if (vma->vma_flags & PROT_WRITE)
3648 phdr.p_flags |= PF_W;
3649 if (vma->vma_flags & PROT_EXEC)
3650 phdr.p_flags |= PF_X;
3651 phdr.p_align = ELF_EXEC_PAGESIZE;
3652
3653 bswap_phdr(&phdr, 1);
3654 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
3655 goto out;
3656 }
3657 }
3658
3659 /*
3660 * Next we write notes just after program headers. No
3661 * alignment needed here.
3662 */
3663 if (write_note_info(&info, fd) < 0)
3664 goto out;
3665
3666 /* align data to page boundary */
3667 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3668 goto out;
3669
3670 /*
3671 * Finally we can dump process memory into corefile as well.
3672 */
3673 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3674 abi_ulong addr;
3675 abi_ulong end;
3676
3677 end = vma->vma_start + vma_dump_size(vma);
3678
3679 for (addr = vma->vma_start; addr < end;
3680 addr += TARGET_PAGE_SIZE) {
3681 char page[TARGET_PAGE_SIZE];
3682 int error;
3683
3684 /*
3685 * Read in page from target process memory and
3686 * write it to coredump file.
3687 */
3688 error = copy_from_user(page, addr, sizeof (page));
3689 if (error != 0) {
3690 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3691 addr);
3692 errno = -error;
3693 goto out;
3694 }
3695 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3696 goto out;
3697 }
3698 }
3699
3700 out:
3701 free_note_info(&info);
3702 if (mm != NULL)
3703 vma_delete(mm);
3704 (void) close(fd);
3705
3706 if (errno != 0)
3707 return (-errno);
3708 return (0);
3709 }
3710 #endif /* USE_ELF_CORE_DUMP */
3711
3712 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3713 {
3714 init_thread(regs, infop);
3715 }