]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/elfload.c
linux-user: elfload: Provide default for elf_check_arch
[mirror_qemu.git] / linux-user / elfload.c
1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include <sys/time.h>
3 #include <sys/param.h>
4
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <sys/mman.h>
11 #include <sys/resource.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15
16 #include "qemu.h"
17 #include "disas/disas.h"
18
19 #ifdef _ARCH_PPC64
20 #undef ARCH_DLINFO
21 #undef ELF_PLATFORM
22 #undef ELF_HWCAP
23 #undef ELF_HWCAP2
24 #undef ELF_CLASS
25 #undef ELF_DATA
26 #undef ELF_ARCH
27 #endif
28
29 #define ELF_OSABI ELFOSABI_SYSV
30
31 /* from personality.h */
32
33 /*
34 * Flags for bug emulation.
35 *
36 * These occupy the top three bytes.
37 */
38 enum {
39 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
40 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
41 descriptors (signal handling) */
42 MMAP_PAGE_ZERO = 0x0100000,
43 ADDR_COMPAT_LAYOUT = 0x0200000,
44 READ_IMPLIES_EXEC = 0x0400000,
45 ADDR_LIMIT_32BIT = 0x0800000,
46 SHORT_INODE = 0x1000000,
47 WHOLE_SECONDS = 0x2000000,
48 STICKY_TIMEOUTS = 0x4000000,
49 ADDR_LIMIT_3GB = 0x8000000,
50 };
51
52 /*
53 * Personality types.
54 *
55 * These go in the low byte. Avoid using the top bit, it will
56 * conflict with error returns.
57 */
58 enum {
59 PER_LINUX = 0x0000,
60 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
61 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
62 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
63 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
64 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
65 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
66 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
67 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
68 PER_BSD = 0x0006,
69 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
70 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
71 PER_LINUX32 = 0x0008,
72 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
73 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
74 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
75 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
76 PER_RISCOS = 0x000c,
77 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
78 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
79 PER_OSF4 = 0x000f, /* OSF/1 v4 */
80 PER_HPUX = 0x0010,
81 PER_MASK = 0x00ff,
82 };
83
84 /*
85 * Return the base personality without flags.
86 */
87 #define personality(pers) (pers & PER_MASK)
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 ELF_MACHINE
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->ARM_cpsr = 0x10;
285 if (infop->entry & 1)
286 regs->ARM_cpsr |= CPSR_T;
287 regs->ARM_pc = infop->entry & 0xfffffffe;
288 regs->ARM_sp = infop->start_stack;
289 /* FIXME - what to for failure of get_user()? */
290 get_user_ual(regs->ARM_r2, stack + 8); /* envp */
291 get_user_ual(regs->ARM_r1, stack + 4); /* envp */
292 /* XXX: it seems that r0 is zeroed after ! */
293 regs->ARM_r0 = 0;
294 /* For uClinux PIC binaries. */
295 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
296 regs->ARM_r10 = infop->start_data;
297 }
298
299 #define ELF_NREG 18
300 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
301
302 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
303 {
304 (*regs)[0] = tswapreg(env->regs[0]);
305 (*regs)[1] = tswapreg(env->regs[1]);
306 (*regs)[2] = tswapreg(env->regs[2]);
307 (*regs)[3] = tswapreg(env->regs[3]);
308 (*regs)[4] = tswapreg(env->regs[4]);
309 (*regs)[5] = tswapreg(env->regs[5]);
310 (*regs)[6] = tswapreg(env->regs[6]);
311 (*regs)[7] = tswapreg(env->regs[7]);
312 (*regs)[8] = tswapreg(env->regs[8]);
313 (*regs)[9] = tswapreg(env->regs[9]);
314 (*regs)[10] = tswapreg(env->regs[10]);
315 (*regs)[11] = tswapreg(env->regs[11]);
316 (*regs)[12] = tswapreg(env->regs[12]);
317 (*regs)[13] = tswapreg(env->regs[13]);
318 (*regs)[14] = tswapreg(env->regs[14]);
319 (*regs)[15] = tswapreg(env->regs[15]);
320
321 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
322 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
323 }
324
325 #define USE_ELF_CORE_DUMP
326 #define ELF_EXEC_PAGESIZE 4096
327
328 enum
329 {
330 ARM_HWCAP_ARM_SWP = 1 << 0,
331 ARM_HWCAP_ARM_HALF = 1 << 1,
332 ARM_HWCAP_ARM_THUMB = 1 << 2,
333 ARM_HWCAP_ARM_26BIT = 1 << 3,
334 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
335 ARM_HWCAP_ARM_FPA = 1 << 5,
336 ARM_HWCAP_ARM_VFP = 1 << 6,
337 ARM_HWCAP_ARM_EDSP = 1 << 7,
338 ARM_HWCAP_ARM_JAVA = 1 << 8,
339 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
340 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
341 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
342 ARM_HWCAP_ARM_NEON = 1 << 12,
343 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
344 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
345 ARM_HWCAP_ARM_TLS = 1 << 15,
346 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
347 ARM_HWCAP_ARM_IDIVA = 1 << 17,
348 ARM_HWCAP_ARM_IDIVT = 1 << 18,
349 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
350 ARM_HWCAP_ARM_LPAE = 1 << 20,
351 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
352 };
353
354 enum {
355 ARM_HWCAP2_ARM_AES = 1 << 0,
356 ARM_HWCAP2_ARM_PMULL = 1 << 1,
357 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
358 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
359 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
360 };
361
362 /* The commpage only exists for 32 bit kernels */
363
364 #define TARGET_HAS_VALIDATE_GUEST_SPACE
365 /* Return 1 if the proposed guest space is suitable for the guest.
366 * Return 0 if the proposed guest space isn't suitable, but another
367 * address space should be tried.
368 * Return -1 if there is no way the proposed guest space can be
369 * valid regardless of the base.
370 * The guest code may leave a page mapped and populate it if the
371 * address is suitable.
372 */
373 static int validate_guest_space(unsigned long guest_base,
374 unsigned long guest_size)
375 {
376 unsigned long real_start, test_page_addr;
377
378 /* We need to check that we can force a fault on access to the
379 * commpage at 0xffff0fxx
380 */
381 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
382
383 /* If the commpage lies within the already allocated guest space,
384 * then there is no way we can allocate it.
385 */
386 if (test_page_addr >= guest_base
387 && test_page_addr <= (guest_base + guest_size)) {
388 return -1;
389 }
390
391 /* Note it needs to be writeable to let us initialise it */
392 real_start = (unsigned long)
393 mmap((void *)test_page_addr, qemu_host_page_size,
394 PROT_READ | PROT_WRITE,
395 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
396
397 /* If we can't map it then try another address */
398 if (real_start == -1ul) {
399 return 0;
400 }
401
402 if (real_start != test_page_addr) {
403 /* OS didn't put the page where we asked - unmap and reject */
404 munmap((void *)real_start, qemu_host_page_size);
405 return 0;
406 }
407
408 /* Leave the page mapped
409 * Populate it (mmap should have left it all 0'd)
410 */
411
412 /* Kernel helper versions */
413 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
414
415 /* Now it's populated make it RO */
416 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
417 perror("Protecting guest commpage");
418 exit(-1);
419 }
420
421 return 1; /* All good */
422 }
423
424 #define ELF_HWCAP get_elf_hwcap()
425 #define ELF_HWCAP2 get_elf_hwcap2()
426
427 static uint32_t get_elf_hwcap(void)
428 {
429 ARMCPU *cpu = ARM_CPU(thread_cpu);
430 uint32_t hwcaps = 0;
431
432 hwcaps |= ARM_HWCAP_ARM_SWP;
433 hwcaps |= ARM_HWCAP_ARM_HALF;
434 hwcaps |= ARM_HWCAP_ARM_THUMB;
435 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
436
437 /* probe for the extra features */
438 #define GET_FEATURE(feat, hwcap) \
439 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
440 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
441 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
442 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
443 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
444 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
445 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
446 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
447 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
448 GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);
449 GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA);
450 GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT);
451 /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.
452 * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of
453 * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated
454 * to our VFP_FP16 feature bit.
455 */
456 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);
457 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
458
459 return hwcaps;
460 }
461
462 static uint32_t get_elf_hwcap2(void)
463 {
464 ARMCPU *cpu = ARM_CPU(thread_cpu);
465 uint32_t hwcaps = 0;
466
467 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP2_ARM_AES);
468 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP2_ARM_PMULL);
469 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP2_ARM_SHA1);
470 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP2_ARM_SHA2);
471 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP2_ARM_CRC32);
472 return hwcaps;
473 }
474
475 #undef GET_FEATURE
476
477 #else
478 /* 64 bit ARM definitions */
479 #define ELF_START_MMAP 0x80000000
480
481 #define ELF_ARCH ELF_MACHINE
482 #define ELF_CLASS ELFCLASS64
483 #define ELF_PLATFORM "aarch64"
484
485 static inline void init_thread(struct target_pt_regs *regs,
486 struct image_info *infop)
487 {
488 abi_long stack = infop->start_stack;
489 memset(regs, 0, sizeof(*regs));
490
491 regs->pc = infop->entry & ~0x3ULL;
492 regs->sp = stack;
493 }
494
495 #define ELF_NREG 34
496 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
497
498 static void elf_core_copy_regs(target_elf_gregset_t *regs,
499 const CPUARMState *env)
500 {
501 int i;
502
503 for (i = 0; i < 32; i++) {
504 (*regs)[i] = tswapreg(env->xregs[i]);
505 }
506 (*regs)[32] = tswapreg(env->pc);
507 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
508 }
509
510 #define USE_ELF_CORE_DUMP
511 #define ELF_EXEC_PAGESIZE 4096
512
513 enum {
514 ARM_HWCAP_A64_FP = 1 << 0,
515 ARM_HWCAP_A64_ASIMD = 1 << 1,
516 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
517 ARM_HWCAP_A64_AES = 1 << 3,
518 ARM_HWCAP_A64_PMULL = 1 << 4,
519 ARM_HWCAP_A64_SHA1 = 1 << 5,
520 ARM_HWCAP_A64_SHA2 = 1 << 6,
521 ARM_HWCAP_A64_CRC32 = 1 << 7,
522 };
523
524 #define ELF_HWCAP get_elf_hwcap()
525
526 static uint32_t get_elf_hwcap(void)
527 {
528 ARMCPU *cpu = ARM_CPU(thread_cpu);
529 uint32_t hwcaps = 0;
530
531 hwcaps |= ARM_HWCAP_A64_FP;
532 hwcaps |= ARM_HWCAP_A64_ASIMD;
533
534 /* probe for the extra features */
535 #define GET_FEATURE(feat, hwcap) \
536 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
537 GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP_A64_AES);
538 GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP_A64_PMULL);
539 GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP_A64_SHA1);
540 GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP_A64_SHA2);
541 GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP_A64_CRC32);
542 #undef GET_FEATURE
543
544 return hwcaps;
545 }
546
547 #endif /* not TARGET_AARCH64 */
548 #endif /* TARGET_ARM */
549
550 #ifdef TARGET_UNICORE32
551
552 #define ELF_START_MMAP 0x80000000
553
554 #define ELF_CLASS ELFCLASS32
555 #define ELF_DATA ELFDATA2LSB
556 #define ELF_ARCH EM_UNICORE32
557
558 static inline void init_thread(struct target_pt_regs *regs,
559 struct image_info *infop)
560 {
561 abi_long stack = infop->start_stack;
562 memset(regs, 0, sizeof(*regs));
563 regs->UC32_REG_asr = 0x10;
564 regs->UC32_REG_pc = infop->entry & 0xfffffffe;
565 regs->UC32_REG_sp = infop->start_stack;
566 /* FIXME - what to for failure of get_user()? */
567 get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */
568 get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */
569 /* XXX: it seems that r0 is zeroed after ! */
570 regs->UC32_REG_00 = 0;
571 }
572
573 #define ELF_NREG 34
574 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
575
576 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
577 {
578 (*regs)[0] = env->regs[0];
579 (*regs)[1] = env->regs[1];
580 (*regs)[2] = env->regs[2];
581 (*regs)[3] = env->regs[3];
582 (*regs)[4] = env->regs[4];
583 (*regs)[5] = env->regs[5];
584 (*regs)[6] = env->regs[6];
585 (*regs)[7] = env->regs[7];
586 (*regs)[8] = env->regs[8];
587 (*regs)[9] = env->regs[9];
588 (*regs)[10] = env->regs[10];
589 (*regs)[11] = env->regs[11];
590 (*regs)[12] = env->regs[12];
591 (*regs)[13] = env->regs[13];
592 (*regs)[14] = env->regs[14];
593 (*regs)[15] = env->regs[15];
594 (*regs)[16] = env->regs[16];
595 (*regs)[17] = env->regs[17];
596 (*regs)[18] = env->regs[18];
597 (*regs)[19] = env->regs[19];
598 (*regs)[20] = env->regs[20];
599 (*regs)[21] = env->regs[21];
600 (*regs)[22] = env->regs[22];
601 (*regs)[23] = env->regs[23];
602 (*regs)[24] = env->regs[24];
603 (*regs)[25] = env->regs[25];
604 (*regs)[26] = env->regs[26];
605 (*regs)[27] = env->regs[27];
606 (*regs)[28] = env->regs[28];
607 (*regs)[29] = env->regs[29];
608 (*regs)[30] = env->regs[30];
609 (*regs)[31] = env->regs[31];
610
611 (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
612 (*regs)[33] = env->regs[0]; /* XXX */
613 }
614
615 #define USE_ELF_CORE_DUMP
616 #define ELF_EXEC_PAGESIZE 4096
617
618 #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
619
620 #endif
621
622 #ifdef TARGET_SPARC
623 #ifdef TARGET_SPARC64
624
625 #define ELF_START_MMAP 0x80000000
626 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
627 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
628 #ifndef TARGET_ABI32
629 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
630 #else
631 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
632 #endif
633
634 #define ELF_CLASS ELFCLASS64
635 #define ELF_ARCH EM_SPARCV9
636
637 #define STACK_BIAS 2047
638
639 static inline void init_thread(struct target_pt_regs *regs,
640 struct image_info *infop)
641 {
642 #ifndef TARGET_ABI32
643 regs->tstate = 0;
644 #endif
645 regs->pc = infop->entry;
646 regs->npc = regs->pc + 4;
647 regs->y = 0;
648 #ifdef TARGET_ABI32
649 regs->u_regs[14] = infop->start_stack - 16 * 4;
650 #else
651 if (personality(infop->personality) == PER_LINUX32)
652 regs->u_regs[14] = infop->start_stack - 16 * 4;
653 else
654 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
655 #endif
656 }
657
658 #else
659 #define ELF_START_MMAP 0x80000000
660 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
661 | HWCAP_SPARC_MULDIV)
662
663 #define ELF_CLASS ELFCLASS32
664 #define ELF_ARCH EM_SPARC
665
666 static inline void init_thread(struct target_pt_regs *regs,
667 struct image_info *infop)
668 {
669 regs->psr = 0;
670 regs->pc = infop->entry;
671 regs->npc = regs->pc + 4;
672 regs->y = 0;
673 regs->u_regs[14] = infop->start_stack - 16 * 4;
674 }
675
676 #endif
677 #endif
678
679 #ifdef TARGET_PPC
680
681 #define ELF_START_MMAP 0x80000000
682
683 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
684
685 #define elf_check_arch(x) ( (x) == EM_PPC64 )
686
687 #define ELF_CLASS ELFCLASS64
688
689 #else
690
691 #define ELF_CLASS ELFCLASS32
692
693 #endif
694
695 #define ELF_ARCH EM_PPC
696
697 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
698 See arch/powerpc/include/asm/cputable.h. */
699 enum {
700 QEMU_PPC_FEATURE_32 = 0x80000000,
701 QEMU_PPC_FEATURE_64 = 0x40000000,
702 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
703 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
704 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
705 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
706 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
707 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
708 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
709 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
710 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
711 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
712 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
713 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
714 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
715 QEMU_PPC_FEATURE_CELL = 0x00010000,
716 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
717 QEMU_PPC_FEATURE_SMT = 0x00004000,
718 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
719 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
720 QEMU_PPC_FEATURE_PA6T = 0x00000800,
721 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
722 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
723 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
724 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
725 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
726
727 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
728 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
729
730 /* Feature definitions in AT_HWCAP2. */
731 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
732 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
733 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
734 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
735 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
736 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
737 };
738
739 #define ELF_HWCAP get_elf_hwcap()
740
741 static uint32_t get_elf_hwcap(void)
742 {
743 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
744 uint32_t features = 0;
745
746 /* We don't have to be terribly complete here; the high points are
747 Altivec/FP/SPE support. Anything else is just a bonus. */
748 #define GET_FEATURE(flag, feature) \
749 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
750 #define GET_FEATURE2(flag, feature) \
751 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
752 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
753 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
754 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
755 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
756 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
757 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
758 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
759 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
760 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
761 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
762 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
763 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
764 QEMU_PPC_FEATURE_ARCH_2_06);
765 #undef GET_FEATURE
766 #undef GET_FEATURE2
767
768 return features;
769 }
770
771 #define ELF_HWCAP2 get_elf_hwcap2()
772
773 static uint32_t get_elf_hwcap2(void)
774 {
775 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
776 uint32_t features = 0;
777
778 #define GET_FEATURE(flag, feature) \
779 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
780 #define GET_FEATURE2(flag, feature) \
781 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
782
783 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
784 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
785 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
786 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07);
787
788 #undef GET_FEATURE
789 #undef GET_FEATURE2
790
791 return features;
792 }
793
794 /*
795 * The requirements here are:
796 * - keep the final alignment of sp (sp & 0xf)
797 * - make sure the 32-bit value at the first 16 byte aligned position of
798 * AUXV is greater than 16 for glibc compatibility.
799 * AT_IGNOREPPC is used for that.
800 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
801 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
802 */
803 #define DLINFO_ARCH_ITEMS 5
804 #define ARCH_DLINFO \
805 do { \
806 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
807 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
808 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
809 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
810 /* \
811 * Now handle glibc compatibility. \
812 */ \
813 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
814 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
815 } while (0)
816
817 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
818 {
819 _regs->gpr[1] = infop->start_stack;
820 #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
821 if (get_ppc64_abi(infop) < 2) {
822 uint64_t val;
823 get_user_u64(val, infop->entry + 8);
824 _regs->gpr[2] = val + infop->load_bias;
825 get_user_u64(val, infop->entry);
826 infop->entry = val + infop->load_bias;
827 } else {
828 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
829 }
830 #endif
831 _regs->nip = infop->entry;
832 }
833
834 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
835 #define ELF_NREG 48
836 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
837
838 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
839 {
840 int i;
841 target_ulong ccr = 0;
842
843 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
844 (*regs)[i] = tswapreg(env->gpr[i]);
845 }
846
847 (*regs)[32] = tswapreg(env->nip);
848 (*regs)[33] = tswapreg(env->msr);
849 (*regs)[35] = tswapreg(env->ctr);
850 (*regs)[36] = tswapreg(env->lr);
851 (*regs)[37] = tswapreg(env->xer);
852
853 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
854 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
855 }
856 (*regs)[38] = tswapreg(ccr);
857 }
858
859 #define USE_ELF_CORE_DUMP
860 #define ELF_EXEC_PAGESIZE 4096
861
862 #endif
863
864 #ifdef TARGET_MIPS
865
866 #define ELF_START_MMAP 0x80000000
867
868 #ifdef TARGET_MIPS64
869 #define ELF_CLASS ELFCLASS64
870 #else
871 #define ELF_CLASS ELFCLASS32
872 #endif
873 #define ELF_ARCH EM_MIPS
874
875 static inline void init_thread(struct target_pt_regs *regs,
876 struct image_info *infop)
877 {
878 regs->cp0_status = 2 << CP0St_KSU;
879 regs->cp0_epc = infop->entry;
880 regs->regs[29] = infop->start_stack;
881 }
882
883 /* See linux kernel: arch/mips/include/asm/elf.h. */
884 #define ELF_NREG 45
885 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
886
887 /* See linux kernel: arch/mips/include/asm/reg.h. */
888 enum {
889 #ifdef TARGET_MIPS64
890 TARGET_EF_R0 = 0,
891 #else
892 TARGET_EF_R0 = 6,
893 #endif
894 TARGET_EF_R26 = TARGET_EF_R0 + 26,
895 TARGET_EF_R27 = TARGET_EF_R0 + 27,
896 TARGET_EF_LO = TARGET_EF_R0 + 32,
897 TARGET_EF_HI = TARGET_EF_R0 + 33,
898 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
899 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
900 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
901 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
902 };
903
904 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
905 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
906 {
907 int i;
908
909 for (i = 0; i < TARGET_EF_R0; i++) {
910 (*regs)[i] = 0;
911 }
912 (*regs)[TARGET_EF_R0] = 0;
913
914 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
915 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
916 }
917
918 (*regs)[TARGET_EF_R26] = 0;
919 (*regs)[TARGET_EF_R27] = 0;
920 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
921 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
922 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
923 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
924 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
925 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
926 }
927
928 #define USE_ELF_CORE_DUMP
929 #define ELF_EXEC_PAGESIZE 4096
930
931 #endif /* TARGET_MIPS */
932
933 #ifdef TARGET_MICROBLAZE
934
935 #define ELF_START_MMAP 0x80000000
936
937 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
938
939 #define ELF_CLASS ELFCLASS32
940 #define ELF_ARCH EM_MICROBLAZE
941
942 static inline void init_thread(struct target_pt_regs *regs,
943 struct image_info *infop)
944 {
945 regs->pc = infop->entry;
946 regs->r1 = infop->start_stack;
947
948 }
949
950 #define ELF_EXEC_PAGESIZE 4096
951
952 #define USE_ELF_CORE_DUMP
953 #define ELF_NREG 38
954 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
955
956 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
957 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
958 {
959 int i, pos = 0;
960
961 for (i = 0; i < 32; i++) {
962 (*regs)[pos++] = tswapreg(env->regs[i]);
963 }
964
965 for (i = 0; i < 6; i++) {
966 (*regs)[pos++] = tswapreg(env->sregs[i]);
967 }
968 }
969
970 #endif /* TARGET_MICROBLAZE */
971
972 #ifdef TARGET_OPENRISC
973
974 #define ELF_START_MMAP 0x08000000
975
976 #define ELF_ARCH EM_OPENRISC
977 #define ELF_CLASS ELFCLASS32
978 #define ELF_DATA ELFDATA2MSB
979
980 static inline void init_thread(struct target_pt_regs *regs,
981 struct image_info *infop)
982 {
983 regs->pc = infop->entry;
984 regs->gpr[1] = infop->start_stack;
985 }
986
987 #define USE_ELF_CORE_DUMP
988 #define ELF_EXEC_PAGESIZE 8192
989
990 /* See linux kernel arch/openrisc/include/asm/elf.h. */
991 #define ELF_NREG 34 /* gprs and pc, sr */
992 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
993
994 static void elf_core_copy_regs(target_elf_gregset_t *regs,
995 const CPUOpenRISCState *env)
996 {
997 int i;
998
999 for (i = 0; i < 32; i++) {
1000 (*regs)[i] = tswapreg(env->gpr[i]);
1001 }
1002
1003 (*regs)[32] = tswapreg(env->pc);
1004 (*regs)[33] = tswapreg(env->sr);
1005 }
1006 #define ELF_HWCAP 0
1007 #define ELF_PLATFORM NULL
1008
1009 #endif /* TARGET_OPENRISC */
1010
1011 #ifdef TARGET_SH4
1012
1013 #define ELF_START_MMAP 0x80000000
1014
1015 #define ELF_CLASS ELFCLASS32
1016 #define ELF_ARCH EM_SH
1017
1018 static inline void init_thread(struct target_pt_regs *regs,
1019 struct image_info *infop)
1020 {
1021 /* Check other registers XXXXX */
1022 regs->pc = infop->entry;
1023 regs->regs[15] = infop->start_stack;
1024 }
1025
1026 /* See linux kernel: arch/sh/include/asm/elf.h. */
1027 #define ELF_NREG 23
1028 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1029
1030 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1031 enum {
1032 TARGET_REG_PC = 16,
1033 TARGET_REG_PR = 17,
1034 TARGET_REG_SR = 18,
1035 TARGET_REG_GBR = 19,
1036 TARGET_REG_MACH = 20,
1037 TARGET_REG_MACL = 21,
1038 TARGET_REG_SYSCALL = 22
1039 };
1040
1041 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1042 const CPUSH4State *env)
1043 {
1044 int i;
1045
1046 for (i = 0; i < 16; i++) {
1047 (*regs[i]) = tswapreg(env->gregs[i]);
1048 }
1049
1050 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1051 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1052 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1053 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1054 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1055 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1056 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1057 }
1058
1059 #define USE_ELF_CORE_DUMP
1060 #define ELF_EXEC_PAGESIZE 4096
1061
1062 enum {
1063 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1064 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1065 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1066 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1067 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1068 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1069 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1070 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1071 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1072 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1073 };
1074
1075 #define ELF_HWCAP get_elf_hwcap()
1076
1077 static uint32_t get_elf_hwcap(void)
1078 {
1079 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1080 uint32_t hwcap = 0;
1081
1082 hwcap |= SH_CPU_HAS_FPU;
1083
1084 if (cpu->env.features & SH_FEATURE_SH4A) {
1085 hwcap |= SH_CPU_HAS_LLSC;
1086 }
1087
1088 return hwcap;
1089 }
1090
1091 #endif
1092
1093 #ifdef TARGET_CRIS
1094
1095 #define ELF_START_MMAP 0x80000000
1096
1097 #define ELF_CLASS ELFCLASS32
1098 #define ELF_ARCH EM_CRIS
1099
1100 static inline void init_thread(struct target_pt_regs *regs,
1101 struct image_info *infop)
1102 {
1103 regs->erp = infop->entry;
1104 }
1105
1106 #define ELF_EXEC_PAGESIZE 8192
1107
1108 #endif
1109
1110 #ifdef TARGET_M68K
1111
1112 #define ELF_START_MMAP 0x80000000
1113
1114 #define ELF_CLASS ELFCLASS32
1115 #define ELF_ARCH EM_68K
1116
1117 /* ??? Does this need to do anything?
1118 #define ELF_PLAT_INIT(_r) */
1119
1120 static inline void init_thread(struct target_pt_regs *regs,
1121 struct image_info *infop)
1122 {
1123 regs->usp = infop->start_stack;
1124 regs->sr = 0;
1125 regs->pc = infop->entry;
1126 }
1127
1128 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1129 #define ELF_NREG 20
1130 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1131
1132 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1133 {
1134 (*regs)[0] = tswapreg(env->dregs[1]);
1135 (*regs)[1] = tswapreg(env->dregs[2]);
1136 (*regs)[2] = tswapreg(env->dregs[3]);
1137 (*regs)[3] = tswapreg(env->dregs[4]);
1138 (*regs)[4] = tswapreg(env->dregs[5]);
1139 (*regs)[5] = tswapreg(env->dregs[6]);
1140 (*regs)[6] = tswapreg(env->dregs[7]);
1141 (*regs)[7] = tswapreg(env->aregs[0]);
1142 (*regs)[8] = tswapreg(env->aregs[1]);
1143 (*regs)[9] = tswapreg(env->aregs[2]);
1144 (*regs)[10] = tswapreg(env->aregs[3]);
1145 (*regs)[11] = tswapreg(env->aregs[4]);
1146 (*regs)[12] = tswapreg(env->aregs[5]);
1147 (*regs)[13] = tswapreg(env->aregs[6]);
1148 (*regs)[14] = tswapreg(env->dregs[0]);
1149 (*regs)[15] = tswapreg(env->aregs[7]);
1150 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1151 (*regs)[17] = tswapreg(env->sr);
1152 (*regs)[18] = tswapreg(env->pc);
1153 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1154 }
1155
1156 #define USE_ELF_CORE_DUMP
1157 #define ELF_EXEC_PAGESIZE 8192
1158
1159 #endif
1160
1161 #ifdef TARGET_ALPHA
1162
1163 #define ELF_START_MMAP (0x30000000000ULL)
1164
1165 #define ELF_CLASS ELFCLASS64
1166 #define ELF_ARCH EM_ALPHA
1167
1168 static inline void init_thread(struct target_pt_regs *regs,
1169 struct image_info *infop)
1170 {
1171 regs->pc = infop->entry;
1172 regs->ps = 8;
1173 regs->usp = infop->start_stack;
1174 }
1175
1176 #define ELF_EXEC_PAGESIZE 8192
1177
1178 #endif /* TARGET_ALPHA */
1179
1180 #ifdef TARGET_S390X
1181
1182 #define ELF_START_MMAP (0x20000000000ULL)
1183
1184 #define ELF_CLASS ELFCLASS64
1185 #define ELF_DATA ELFDATA2MSB
1186 #define ELF_ARCH EM_S390
1187
1188 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1189 {
1190 regs->psw.addr = infop->entry;
1191 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1192 regs->gprs[15] = infop->start_stack;
1193 }
1194
1195 #endif /* TARGET_S390X */
1196
1197 #ifdef TARGET_TILEGX
1198
1199 /* 42 bits real used address, a half for user mode */
1200 #define ELF_START_MMAP (0x00000020000000000ULL)
1201
1202 #define elf_check_arch(x) ((x) == EM_TILEGX)
1203
1204 #define ELF_CLASS ELFCLASS64
1205 #define ELF_DATA ELFDATA2LSB
1206 #define ELF_ARCH EM_TILEGX
1207
1208 static inline void init_thread(struct target_pt_regs *regs,
1209 struct image_info *infop)
1210 {
1211 regs->pc = infop->entry;
1212 regs->sp = infop->start_stack;
1213
1214 }
1215
1216 #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1217
1218 #endif /* TARGET_TILEGX */
1219
1220 #ifndef ELF_PLATFORM
1221 #define ELF_PLATFORM (NULL)
1222 #endif
1223
1224 #ifndef ELF_MACHINE
1225 #define ELF_MACHINE ELF_ARCH
1226 #endif
1227
1228 #ifndef elf_check_arch
1229 #define elf_check_arch(x) ((x) == ELF_ARCH)
1230 #endif
1231
1232 #ifndef ELF_HWCAP
1233 #define ELF_HWCAP 0
1234 #endif
1235
1236 #ifdef TARGET_ABI32
1237 #undef ELF_CLASS
1238 #define ELF_CLASS ELFCLASS32
1239 #undef bswaptls
1240 #define bswaptls(ptr) bswap32s(ptr)
1241 #endif
1242
1243 #include "elf.h"
1244
1245 struct exec
1246 {
1247 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1248 unsigned int a_text; /* length of text, in bytes */
1249 unsigned int a_data; /* length of data, in bytes */
1250 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1251 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1252 unsigned int a_entry; /* start address */
1253 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1254 unsigned int a_drsize; /* length of relocation info for data, in bytes */
1255 };
1256
1257
1258 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1259 #define OMAGIC 0407
1260 #define NMAGIC 0410
1261 #define ZMAGIC 0413
1262 #define QMAGIC 0314
1263
1264 /* Necessary parameters */
1265 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1266 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1267 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1268 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1269
1270 #define DLINFO_ITEMS 14
1271
1272 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1273 {
1274 memcpy(to, from, n);
1275 }
1276
1277 #ifdef BSWAP_NEEDED
1278 static void bswap_ehdr(struct elfhdr *ehdr)
1279 {
1280 bswap16s(&ehdr->e_type); /* Object file type */
1281 bswap16s(&ehdr->e_machine); /* Architecture */
1282 bswap32s(&ehdr->e_version); /* Object file version */
1283 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1284 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1285 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1286 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1287 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1288 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1289 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1290 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1291 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1292 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
1293 }
1294
1295 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
1296 {
1297 int i;
1298 for (i = 0; i < phnum; ++i, ++phdr) {
1299 bswap32s(&phdr->p_type); /* Segment type */
1300 bswap32s(&phdr->p_flags); /* Segment flags */
1301 bswaptls(&phdr->p_offset); /* Segment file offset */
1302 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1303 bswaptls(&phdr->p_paddr); /* Segment physical address */
1304 bswaptls(&phdr->p_filesz); /* Segment size in file */
1305 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1306 bswaptls(&phdr->p_align); /* Segment alignment */
1307 }
1308 }
1309
1310 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
1311 {
1312 int i;
1313 for (i = 0; i < shnum; ++i, ++shdr) {
1314 bswap32s(&shdr->sh_name);
1315 bswap32s(&shdr->sh_type);
1316 bswaptls(&shdr->sh_flags);
1317 bswaptls(&shdr->sh_addr);
1318 bswaptls(&shdr->sh_offset);
1319 bswaptls(&shdr->sh_size);
1320 bswap32s(&shdr->sh_link);
1321 bswap32s(&shdr->sh_info);
1322 bswaptls(&shdr->sh_addralign);
1323 bswaptls(&shdr->sh_entsize);
1324 }
1325 }
1326
1327 static void bswap_sym(struct elf_sym *sym)
1328 {
1329 bswap32s(&sym->st_name);
1330 bswaptls(&sym->st_value);
1331 bswaptls(&sym->st_size);
1332 bswap16s(&sym->st_shndx);
1333 }
1334 #else
1335 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1336 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1337 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1338 static inline void bswap_sym(struct elf_sym *sym) { }
1339 #endif
1340
1341 #ifdef USE_ELF_CORE_DUMP
1342 static int elf_core_dump(int, const CPUArchState *);
1343 #endif /* USE_ELF_CORE_DUMP */
1344 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
1345
1346 /* Verify the portions of EHDR within E_IDENT for the target.
1347 This can be performed before bswapping the entire header. */
1348 static bool elf_check_ident(struct elfhdr *ehdr)
1349 {
1350 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1351 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1352 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1353 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1354 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1355 && ehdr->e_ident[EI_DATA] == ELF_DATA
1356 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1357 }
1358
1359 /* Verify the portions of EHDR outside of E_IDENT for the target.
1360 This has to wait until after bswapping the header. */
1361 static bool elf_check_ehdr(struct elfhdr *ehdr)
1362 {
1363 return (elf_check_arch(ehdr->e_machine)
1364 && ehdr->e_ehsize == sizeof(struct elfhdr)
1365 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1366 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1367 }
1368
1369 /*
1370 * 'copy_elf_strings()' copies argument/envelope strings from user
1371 * memory to free pages in kernel mem. These are in a format ready
1372 * to be put directly into the top of new user memory.
1373 *
1374 */
1375 static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
1376 abi_ulong p)
1377 {
1378 char *tmp, *tmp1, *pag = NULL;
1379 int len, offset = 0;
1380
1381 if (!p) {
1382 return 0; /* bullet-proofing */
1383 }
1384 while (argc-- > 0) {
1385 tmp = argv[argc];
1386 if (!tmp) {
1387 fprintf(stderr, "VFS: argc is wrong");
1388 exit(-1);
1389 }
1390 tmp1 = tmp;
1391 while (*tmp++);
1392 len = tmp - tmp1;
1393 if (p < len) { /* this shouldn't happen - 128kB */
1394 return 0;
1395 }
1396 while (len) {
1397 --p; --tmp; --len;
1398 if (--offset < 0) {
1399 offset = p % TARGET_PAGE_SIZE;
1400 pag = (char *)page[p/TARGET_PAGE_SIZE];
1401 if (!pag) {
1402 pag = g_try_malloc0(TARGET_PAGE_SIZE);
1403 page[p/TARGET_PAGE_SIZE] = pag;
1404 if (!pag)
1405 return 0;
1406 }
1407 }
1408 if (len == 0 || offset == 0) {
1409 *(pag + offset) = *tmp;
1410 }
1411 else {
1412 int bytes_to_copy = (len > offset) ? offset : len;
1413 tmp -= bytes_to_copy;
1414 p -= bytes_to_copy;
1415 offset -= bytes_to_copy;
1416 len -= bytes_to_copy;
1417 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
1418 }
1419 }
1420 }
1421 return p;
1422 }
1423
1424 static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
1425 struct image_info *info)
1426 {
1427 abi_ulong stack_base, size, error, guard;
1428 int i;
1429
1430 /* Create enough stack to hold everything. If we don't use
1431 it for args, we'll use it for something else. */
1432 size = guest_stack_size;
1433 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) {
1434 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1435 }
1436 guard = TARGET_PAGE_SIZE;
1437 if (guard < qemu_real_host_page_size) {
1438 guard = qemu_real_host_page_size;
1439 }
1440
1441 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1442 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1443 if (error == -1) {
1444 perror("mmap stack");
1445 exit(-1);
1446 }
1447
1448 /* We reserve one extra page at the top of the stack as guard. */
1449 target_mprotect(error, guard, PROT_NONE);
1450
1451 info->stack_limit = error + guard;
1452 stack_base = info->stack_limit + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
1453 p += stack_base;
1454
1455 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1456 if (bprm->page[i]) {
1457 info->rss++;
1458 /* FIXME - check return value of memcpy_to_target() for failure */
1459 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
1460 g_free(bprm->page[i]);
1461 }
1462 stack_base += TARGET_PAGE_SIZE;
1463 }
1464 return p;
1465 }
1466
1467 /* Map and zero the bss. We need to explicitly zero any fractional pages
1468 after the data section (i.e. bss). */
1469 static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
1470 {
1471 uintptr_t host_start, host_map_start, host_end;
1472
1473 last_bss = TARGET_PAGE_ALIGN(last_bss);
1474
1475 /* ??? There is confusion between qemu_real_host_page_size and
1476 qemu_host_page_size here and elsewhere in target_mmap, which
1477 may lead to the end of the data section mapping from the file
1478 not being mapped. At least there was an explicit test and
1479 comment for that here, suggesting that "the file size must
1480 be known". The comment probably pre-dates the introduction
1481 of the fstat system call in target_mmap which does in fact
1482 find out the size. What isn't clear is if the workaround
1483 here is still actually needed. For now, continue with it,
1484 but merge it with the "normal" mmap that would allocate the bss. */
1485
1486 host_start = (uintptr_t) g2h(elf_bss);
1487 host_end = (uintptr_t) g2h(last_bss);
1488 host_map_start = (host_start + qemu_real_host_page_size - 1);
1489 host_map_start &= -qemu_real_host_page_size;
1490
1491 if (host_map_start < host_end) {
1492 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1493 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1494 if (p == MAP_FAILED) {
1495 perror("cannot mmap brk");
1496 exit(-1);
1497 }
1498 }
1499
1500 /* Ensure that the bss page(s) are valid */
1501 if ((page_get_flags(last_bss-1) & prot) != prot) {
1502 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
1503 }
1504
1505 if (host_start < host_map_start) {
1506 memset((void *)host_start, 0, host_map_start - host_start);
1507 }
1508 }
1509
1510 #ifdef CONFIG_USE_FDPIC
1511 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1512 {
1513 uint16_t n;
1514 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1515
1516 /* elf32_fdpic_loadseg */
1517 n = info->nsegs;
1518 while (n--) {
1519 sp -= 12;
1520 put_user_u32(loadsegs[n].addr, sp+0);
1521 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1522 put_user_u32(loadsegs[n].p_memsz, sp+8);
1523 }
1524
1525 /* elf32_fdpic_loadmap */
1526 sp -= 4;
1527 put_user_u16(0, sp+0); /* version */
1528 put_user_u16(info->nsegs, sp+2); /* nsegs */
1529
1530 info->personality = PER_LINUX_FDPIC;
1531 info->loadmap_addr = sp;
1532
1533 return sp;
1534 }
1535 #endif
1536
1537 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1538 struct elfhdr *exec,
1539 struct image_info *info,
1540 struct image_info *interp_info)
1541 {
1542 abi_ulong sp;
1543 abi_ulong sp_auxv;
1544 int size;
1545 int i;
1546 abi_ulong u_rand_bytes;
1547 uint8_t k_rand_bytes[16];
1548 abi_ulong u_platform;
1549 const char *k_platform;
1550 const int n = sizeof(elf_addr_t);
1551
1552 sp = p;
1553
1554 #ifdef CONFIG_USE_FDPIC
1555 /* Needs to be before we load the env/argc/... */
1556 if (elf_is_fdpic(exec)) {
1557 /* Need 4 byte alignment for these structs */
1558 sp &= ~3;
1559 sp = loader_build_fdpic_loadmap(info, sp);
1560 info->other_info = interp_info;
1561 if (interp_info) {
1562 interp_info->other_info = info;
1563 sp = loader_build_fdpic_loadmap(interp_info, sp);
1564 }
1565 }
1566 #endif
1567
1568 u_platform = 0;
1569 k_platform = ELF_PLATFORM;
1570 if (k_platform) {
1571 size_t len = strlen(k_platform) + 1;
1572 sp -= (len + n - 1) & ~(n - 1);
1573 u_platform = sp;
1574 /* FIXME - check return value of memcpy_to_target() for failure */
1575 memcpy_to_target(sp, k_platform, len);
1576 }
1577
1578 /*
1579 * Generate 16 random bytes for userspace PRNG seeding (not
1580 * cryptically secure but it's not the aim of QEMU).
1581 */
1582 for (i = 0; i < 16; i++) {
1583 k_rand_bytes[i] = rand();
1584 }
1585 sp -= 16;
1586 u_rand_bytes = sp;
1587 /* FIXME - check return value of memcpy_to_target() for failure */
1588 memcpy_to_target(sp, k_rand_bytes, 16);
1589
1590 /*
1591 * Force 16 byte _final_ alignment here for generality.
1592 */
1593 sp = sp &~ (abi_ulong)15;
1594 size = (DLINFO_ITEMS + 1) * 2;
1595 if (k_platform)
1596 size += 2;
1597 #ifdef DLINFO_ARCH_ITEMS
1598 size += DLINFO_ARCH_ITEMS * 2;
1599 #endif
1600 #ifdef ELF_HWCAP2
1601 size += 2;
1602 #endif
1603 size += envc + argc + 2;
1604 size += 1; /* argc itself */
1605 size *= n;
1606 if (size & 15)
1607 sp -= 16 - (size & 15);
1608
1609 /* This is correct because Linux defines
1610 * elf_addr_t as Elf32_Off / Elf64_Off
1611 */
1612 #define NEW_AUX_ENT(id, val) do { \
1613 sp -= n; put_user_ual(val, sp); \
1614 sp -= n; put_user_ual(id, sp); \
1615 } while(0)
1616
1617 sp_auxv = sp;
1618 NEW_AUX_ENT (AT_NULL, 0);
1619
1620 /* There must be exactly DLINFO_ITEMS entries here. */
1621 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
1622 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1623 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1624 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize())));
1625 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
1626 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
1627 NEW_AUX_ENT(AT_ENTRY, info->entry);
1628 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1629 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1630 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1631 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1632 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1633 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
1634 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1635
1636 #ifdef ELF_HWCAP2
1637 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
1638 #endif
1639
1640 if (k_platform)
1641 NEW_AUX_ENT(AT_PLATFORM, u_platform);
1642 #ifdef ARCH_DLINFO
1643 /*
1644 * ARCH_DLINFO must come last so platform specific code can enforce
1645 * special alignment requirements on the AUXV if necessary (eg. PPC).
1646 */
1647 ARCH_DLINFO;
1648 #endif
1649 #undef NEW_AUX_ENT
1650
1651 info->saved_auxv = sp;
1652 info->auxv_len = sp_auxv - sp;
1653
1654 sp = loader_build_argptr(envc, argc, sp, p, 0);
1655 /* Check the right amount of stack was allocated for auxvec, envp & argv. */
1656 assert(sp_auxv - sp == size);
1657 return sp;
1658 }
1659
1660 #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
1661 /* If the guest doesn't have a validation function just agree */
1662 static int validate_guest_space(unsigned long guest_base,
1663 unsigned long guest_size)
1664 {
1665 return 1;
1666 }
1667 #endif
1668
1669 unsigned long init_guest_space(unsigned long host_start,
1670 unsigned long host_size,
1671 unsigned long guest_start,
1672 bool fixed)
1673 {
1674 unsigned long current_start, real_start;
1675 int flags;
1676
1677 assert(host_start || host_size);
1678
1679 /* If just a starting address is given, then just verify that
1680 * address. */
1681 if (host_start && !host_size) {
1682 if (validate_guest_space(host_start, host_size) == 1) {
1683 return host_start;
1684 } else {
1685 return (unsigned long)-1;
1686 }
1687 }
1688
1689 /* Setup the initial flags and start address. */
1690 current_start = host_start & qemu_host_page_mask;
1691 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1692 if (fixed) {
1693 flags |= MAP_FIXED;
1694 }
1695
1696 /* Otherwise, a non-zero size region of memory needs to be mapped
1697 * and validated. */
1698 while (1) {
1699 unsigned long real_size = host_size;
1700
1701 /* Do not use mmap_find_vma here because that is limited to the
1702 * guest address space. We are going to make the
1703 * guest address space fit whatever we're given.
1704 */
1705 real_start = (unsigned long)
1706 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1707 if (real_start == (unsigned long)-1) {
1708 return (unsigned long)-1;
1709 }
1710
1711 /* Ensure the address is properly aligned. */
1712 if (real_start & ~qemu_host_page_mask) {
1713 munmap((void *)real_start, host_size);
1714 real_size = host_size + qemu_host_page_size;
1715 real_start = (unsigned long)
1716 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1717 if (real_start == (unsigned long)-1) {
1718 return (unsigned long)-1;
1719 }
1720 real_start = HOST_PAGE_ALIGN(real_start);
1721 }
1722
1723 /* Check to see if the address is valid. */
1724 if (!host_start || real_start == current_start) {
1725 int valid = validate_guest_space(real_start - guest_start,
1726 real_size);
1727 if (valid == 1) {
1728 break;
1729 } else if (valid == -1) {
1730 return (unsigned long)-1;
1731 }
1732 /* valid == 0, so try again. */
1733 }
1734
1735 /* That address didn't work. Unmap and try a different one.
1736 * The address the host picked because is typically right at
1737 * the top of the host address space and leaves the guest with
1738 * no usable address space. Resort to a linear search. We
1739 * already compensated for mmap_min_addr, so this should not
1740 * happen often. Probably means we got unlucky and host
1741 * address space randomization put a shared library somewhere
1742 * inconvenient.
1743 */
1744 munmap((void *)real_start, host_size);
1745 current_start += qemu_host_page_size;
1746 if (host_start == current_start) {
1747 /* Theoretically possible if host doesn't have any suitably
1748 * aligned areas. Normally the first mmap will fail.
1749 */
1750 return (unsigned long)-1;
1751 }
1752 }
1753
1754 qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size);
1755
1756 return real_start;
1757 }
1758
1759 static void probe_guest_base(const char *image_name,
1760 abi_ulong loaddr, abi_ulong hiaddr)
1761 {
1762 /* Probe for a suitable guest base address, if the user has not set
1763 * it explicitly, and set guest_base appropriately.
1764 * In case of error we will print a suitable message and exit.
1765 */
1766 const char *errmsg;
1767 if (!have_guest_base && !reserved_va) {
1768 unsigned long host_start, real_start, host_size;
1769
1770 /* Round addresses to page boundaries. */
1771 loaddr &= qemu_host_page_mask;
1772 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1773
1774 if (loaddr < mmap_min_addr) {
1775 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1776 } else {
1777 host_start = loaddr;
1778 if (host_start != loaddr) {
1779 errmsg = "Address overflow loading ELF binary";
1780 goto exit_errmsg;
1781 }
1782 }
1783 host_size = hiaddr - loaddr;
1784
1785 /* Setup the initial guest memory space with ranges gleaned from
1786 * the ELF image that is being loaded.
1787 */
1788 real_start = init_guest_space(host_start, host_size, loaddr, false);
1789 if (real_start == (unsigned long)-1) {
1790 errmsg = "Unable to find space for application";
1791 goto exit_errmsg;
1792 }
1793 guest_base = real_start - loaddr;
1794
1795 qemu_log("Relocating guest address space from 0x"
1796 TARGET_ABI_FMT_lx " to 0x%lx\n",
1797 loaddr, real_start);
1798 }
1799 return;
1800
1801 exit_errmsg:
1802 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1803 exit(-1);
1804 }
1805
1806
1807 /* Load an ELF image into the address space.
1808
1809 IMAGE_NAME is the filename of the image, to use in error messages.
1810 IMAGE_FD is the open file descriptor for the image.
1811
1812 BPRM_BUF is a copy of the beginning of the file; this of course
1813 contains the elf file header at offset 0. It is assumed that this
1814 buffer is sufficiently aligned to present no problems to the host
1815 in accessing data at aligned offsets within the buffer.
1816
1817 On return: INFO values will be filled in, as necessary or available. */
1818
1819 static void load_elf_image(const char *image_name, int image_fd,
1820 struct image_info *info, char **pinterp_name,
1821 char bprm_buf[BPRM_BUF_SIZE])
1822 {
1823 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1824 struct elf_phdr *phdr;
1825 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1826 int i, retval;
1827 const char *errmsg;
1828
1829 /* First of all, some simple consistency checks */
1830 errmsg = "Invalid ELF image for this architecture";
1831 if (!elf_check_ident(ehdr)) {
1832 goto exit_errmsg;
1833 }
1834 bswap_ehdr(ehdr);
1835 if (!elf_check_ehdr(ehdr)) {
1836 goto exit_errmsg;
1837 }
1838
1839 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1840 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1841 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
1842 } else {
1843 phdr = (struct elf_phdr *) alloca(i);
1844 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
1845 if (retval != i) {
1846 goto exit_read;
1847 }
1848 }
1849 bswap_phdr(phdr, ehdr->e_phnum);
1850
1851 #ifdef CONFIG_USE_FDPIC
1852 info->nsegs = 0;
1853 info->pt_dynamic_addr = 0;
1854 #endif
1855
1856 /* Find the maximum size of the image and allocate an appropriate
1857 amount of memory to handle that. */
1858 loaddr = -1, hiaddr = 0;
1859 for (i = 0; i < ehdr->e_phnum; ++i) {
1860 if (phdr[i].p_type == PT_LOAD) {
1861 abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
1862 if (a < loaddr) {
1863 loaddr = a;
1864 }
1865 a = phdr[i].p_vaddr + phdr[i].p_memsz;
1866 if (a > hiaddr) {
1867 hiaddr = a;
1868 }
1869 #ifdef CONFIG_USE_FDPIC
1870 ++info->nsegs;
1871 #endif
1872 }
1873 }
1874
1875 load_addr = loaddr;
1876 if (ehdr->e_type == ET_DYN) {
1877 /* The image indicates that it can be loaded anywhere. Find a
1878 location that can hold the memory space required. If the
1879 image is pre-linked, LOADDR will be non-zero. Since we do
1880 not supply MAP_FIXED here we'll use that address if and
1881 only if it remains available. */
1882 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
1883 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
1884 -1, 0);
1885 if (load_addr == -1) {
1886 goto exit_perror;
1887 }
1888 } else if (pinterp_name != NULL) {
1889 /* This is the main executable. Make sure that the low
1890 address does not conflict with MMAP_MIN_ADDR or the
1891 QEMU application itself. */
1892 probe_guest_base(image_name, loaddr, hiaddr);
1893 }
1894 load_bias = load_addr - loaddr;
1895
1896 #ifdef CONFIG_USE_FDPIC
1897 {
1898 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
1899 g_malloc(sizeof(*loadsegs) * info->nsegs);
1900
1901 for (i = 0; i < ehdr->e_phnum; ++i) {
1902 switch (phdr[i].p_type) {
1903 case PT_DYNAMIC:
1904 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
1905 break;
1906 case PT_LOAD:
1907 loadsegs->addr = phdr[i].p_vaddr + load_bias;
1908 loadsegs->p_vaddr = phdr[i].p_vaddr;
1909 loadsegs->p_memsz = phdr[i].p_memsz;
1910 ++loadsegs;
1911 break;
1912 }
1913 }
1914 }
1915 #endif
1916
1917 info->load_bias = load_bias;
1918 info->load_addr = load_addr;
1919 info->entry = ehdr->e_entry + load_bias;
1920 info->start_code = -1;
1921 info->end_code = 0;
1922 info->start_data = -1;
1923 info->end_data = 0;
1924 info->brk = 0;
1925 info->elf_flags = ehdr->e_flags;
1926
1927 for (i = 0; i < ehdr->e_phnum; i++) {
1928 struct elf_phdr *eppnt = phdr + i;
1929 if (eppnt->p_type == PT_LOAD) {
1930 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
1931 int elf_prot = 0;
1932
1933 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1934 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1935 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1936
1937 vaddr = load_bias + eppnt->p_vaddr;
1938 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
1939 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
1940
1941 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
1942 elf_prot, MAP_PRIVATE | MAP_FIXED,
1943 image_fd, eppnt->p_offset - vaddr_po);
1944 if (error == -1) {
1945 goto exit_perror;
1946 }
1947
1948 vaddr_ef = vaddr + eppnt->p_filesz;
1949 vaddr_em = vaddr + eppnt->p_memsz;
1950
1951 /* If the load segment requests extra zeros (e.g. bss), map it. */
1952 if (vaddr_ef < vaddr_em) {
1953 zero_bss(vaddr_ef, vaddr_em, elf_prot);
1954 }
1955
1956 /* Find the full program boundaries. */
1957 if (elf_prot & PROT_EXEC) {
1958 if (vaddr < info->start_code) {
1959 info->start_code = vaddr;
1960 }
1961 if (vaddr_ef > info->end_code) {
1962 info->end_code = vaddr_ef;
1963 }
1964 }
1965 if (elf_prot & PROT_WRITE) {
1966 if (vaddr < info->start_data) {
1967 info->start_data = vaddr;
1968 }
1969 if (vaddr_ef > info->end_data) {
1970 info->end_data = vaddr_ef;
1971 }
1972 if (vaddr_em > info->brk) {
1973 info->brk = vaddr_em;
1974 }
1975 }
1976 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
1977 char *interp_name;
1978
1979 if (*pinterp_name) {
1980 errmsg = "Multiple PT_INTERP entries";
1981 goto exit_errmsg;
1982 }
1983 interp_name = malloc(eppnt->p_filesz);
1984 if (!interp_name) {
1985 goto exit_perror;
1986 }
1987
1988 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
1989 memcpy(interp_name, bprm_buf + eppnt->p_offset,
1990 eppnt->p_filesz);
1991 } else {
1992 retval = pread(image_fd, interp_name, eppnt->p_filesz,
1993 eppnt->p_offset);
1994 if (retval != eppnt->p_filesz) {
1995 goto exit_perror;
1996 }
1997 }
1998 if (interp_name[eppnt->p_filesz - 1] != 0) {
1999 errmsg = "Invalid PT_INTERP entry";
2000 goto exit_errmsg;
2001 }
2002 *pinterp_name = interp_name;
2003 }
2004 }
2005
2006 if (info->end_data == 0) {
2007 info->start_data = info->end_code;
2008 info->end_data = info->end_code;
2009 info->brk = info->end_code;
2010 }
2011
2012 if (qemu_log_enabled()) {
2013 load_symbols(ehdr, image_fd, load_bias);
2014 }
2015
2016 close(image_fd);
2017 return;
2018
2019 exit_read:
2020 if (retval >= 0) {
2021 errmsg = "Incomplete read of file header";
2022 goto exit_errmsg;
2023 }
2024 exit_perror:
2025 errmsg = strerror(errno);
2026 exit_errmsg:
2027 fprintf(stderr, "%s: %s\n", image_name, errmsg);
2028 exit(-1);
2029 }
2030
2031 static void load_elf_interp(const char *filename, struct image_info *info,
2032 char bprm_buf[BPRM_BUF_SIZE])
2033 {
2034 int fd, retval;
2035
2036 fd = open(path(filename), O_RDONLY);
2037 if (fd < 0) {
2038 goto exit_perror;
2039 }
2040
2041 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2042 if (retval < 0) {
2043 goto exit_perror;
2044 }
2045 if (retval < BPRM_BUF_SIZE) {
2046 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2047 }
2048
2049 load_elf_image(filename, fd, info, NULL, bprm_buf);
2050 return;
2051
2052 exit_perror:
2053 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
2054 exit(-1);
2055 }
2056
2057 static int symfind(const void *s0, const void *s1)
2058 {
2059 target_ulong addr = *(target_ulong *)s0;
2060 struct elf_sym *sym = (struct elf_sym *)s1;
2061 int result = 0;
2062 if (addr < sym->st_value) {
2063 result = -1;
2064 } else if (addr >= sym->st_value + sym->st_size) {
2065 result = 1;
2066 }
2067 return result;
2068 }
2069
2070 static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2071 {
2072 #if ELF_CLASS == ELFCLASS32
2073 struct elf_sym *syms = s->disas_symtab.elf32;
2074 #else
2075 struct elf_sym *syms = s->disas_symtab.elf64;
2076 #endif
2077
2078 // binary search
2079 struct elf_sym *sym;
2080
2081 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
2082 if (sym != NULL) {
2083 return s->disas_strtab + sym->st_name;
2084 }
2085
2086 return "";
2087 }
2088
2089 /* FIXME: This should use elf_ops.h */
2090 static int symcmp(const void *s0, const void *s1)
2091 {
2092 struct elf_sym *sym0 = (struct elf_sym *)s0;
2093 struct elf_sym *sym1 = (struct elf_sym *)s1;
2094 return (sym0->st_value < sym1->st_value)
2095 ? -1
2096 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2097 }
2098
2099 /* Best attempt to load symbols from this ELF object. */
2100 static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
2101 {
2102 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
2103 struct elf_shdr *shdr;
2104 char *strings = NULL;
2105 struct syminfo *s = NULL;
2106 struct elf_sym *new_syms, *syms = NULL;
2107
2108 shnum = hdr->e_shnum;
2109 i = shnum * sizeof(struct elf_shdr);
2110 shdr = (struct elf_shdr *)alloca(i);
2111 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2112 return;
2113 }
2114
2115 bswap_shdr(shdr, shnum);
2116 for (i = 0; i < shnum; ++i) {
2117 if (shdr[i].sh_type == SHT_SYMTAB) {
2118 sym_idx = i;
2119 str_idx = shdr[i].sh_link;
2120 goto found;
2121 }
2122 }
2123
2124 /* There will be no symbol table if the file was stripped. */
2125 return;
2126
2127 found:
2128 /* Now know where the strtab and symtab are. Snarf them. */
2129 s = malloc(sizeof(*s));
2130 if (!s) {
2131 goto give_up;
2132 }
2133
2134 i = shdr[str_idx].sh_size;
2135 s->disas_strtab = strings = malloc(i);
2136 if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
2137 goto give_up;
2138 }
2139
2140 i = shdr[sym_idx].sh_size;
2141 syms = malloc(i);
2142 if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
2143 goto give_up;
2144 }
2145
2146 nsyms = i / sizeof(struct elf_sym);
2147 for (i = 0; i < nsyms; ) {
2148 bswap_sym(syms + i);
2149 /* Throw away entries which we do not need. */
2150 if (syms[i].st_shndx == SHN_UNDEF
2151 || syms[i].st_shndx >= SHN_LORESERVE
2152 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
2153 if (i < --nsyms) {
2154 syms[i] = syms[nsyms];
2155 }
2156 } else {
2157 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
2158 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
2159 syms[i].st_value &= ~(target_ulong)1;
2160 #endif
2161 syms[i].st_value += load_bias;
2162 i++;
2163 }
2164 }
2165
2166 /* No "useful" symbol. */
2167 if (nsyms == 0) {
2168 goto give_up;
2169 }
2170
2171 /* Attempt to free the storage associated with the local symbols
2172 that we threw away. Whether or not this has any effect on the
2173 memory allocation depends on the malloc implementation and how
2174 many symbols we managed to discard. */
2175 new_syms = realloc(syms, nsyms * sizeof(*syms));
2176 if (new_syms == NULL) {
2177 goto give_up;
2178 }
2179 syms = new_syms;
2180
2181 qsort(syms, nsyms, sizeof(*syms), symcmp);
2182
2183 s->disas_num_syms = nsyms;
2184 #if ELF_CLASS == ELFCLASS32
2185 s->disas_symtab.elf32 = syms;
2186 #else
2187 s->disas_symtab.elf64 = syms;
2188 #endif
2189 s->lookup_symbol = lookup_symbolxx;
2190 s->next = syminfos;
2191 syminfos = s;
2192
2193 return;
2194
2195 give_up:
2196 free(s);
2197 free(strings);
2198 free(syms);
2199 }
2200
2201 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
2202 {
2203 struct image_info interp_info;
2204 struct elfhdr elf_ex;
2205 char *elf_interpreter = NULL;
2206
2207 info->start_mmap = (abi_ulong)ELF_START_MMAP;
2208 info->mmap = 0;
2209 info->rss = 0;
2210
2211 load_elf_image(bprm->filename, bprm->fd, info,
2212 &elf_interpreter, bprm->buf);
2213
2214 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2215 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2216 when we load the interpreter. */
2217 elf_ex = *(struct elfhdr *)bprm->buf;
2218
2219 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
2220 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
2221 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
2222 if (!bprm->p) {
2223 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2224 exit(-1);
2225 }
2226
2227 /* Do this so that we can load the interpreter, if need be. We will
2228 change some of these later */
2229 bprm->p = setup_arg_pages(bprm->p, bprm, info);
2230
2231 if (elf_interpreter) {
2232 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
2233
2234 /* If the program interpreter is one of these two, then assume
2235 an iBCS2 image. Otherwise assume a native linux image. */
2236
2237 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2238 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2239 info->personality = PER_SVR4;
2240
2241 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2242 and some applications "depend" upon this behavior. Since
2243 we do not have the power to recompile these, we emulate
2244 the SVr4 behavior. Sigh. */
2245 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2246 MAP_FIXED | MAP_PRIVATE, -1, 0);
2247 }
2248 }
2249
2250 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2251 info, (elf_interpreter ? &interp_info : NULL));
2252 info->start_stack = bprm->p;
2253
2254 /* If we have an interpreter, set that as the program's entry point.
2255 Copy the load_bias as well, to help PPC64 interpret the entry
2256 point as a function descriptor. Do this after creating elf tables
2257 so that we copy the original program entry point into the AUXV. */
2258 if (elf_interpreter) {
2259 info->load_bias = interp_info.load_bias;
2260 info->entry = interp_info.entry;
2261 free(elf_interpreter);
2262 }
2263
2264 #ifdef USE_ELF_CORE_DUMP
2265 bprm->core_dump = &elf_core_dump;
2266 #endif
2267
2268 return 0;
2269 }
2270
2271 #ifdef USE_ELF_CORE_DUMP
2272 /*
2273 * Definitions to generate Intel SVR4-like core files.
2274 * These mostly have the same names as the SVR4 types with "target_elf_"
2275 * tacked on the front to prevent clashes with linux definitions,
2276 * and the typedef forms have been avoided. This is mostly like
2277 * the SVR4 structure, but more Linuxy, with things that Linux does
2278 * not support and which gdb doesn't really use excluded.
2279 *
2280 * Fields we don't dump (their contents is zero) in linux-user qemu
2281 * are marked with XXX.
2282 *
2283 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2284 *
2285 * Porting ELF coredump for target is (quite) simple process. First you
2286 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
2287 * the target resides):
2288 *
2289 * #define USE_ELF_CORE_DUMP
2290 *
2291 * Next you define type of register set used for dumping. ELF specification
2292 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2293 *
2294 * typedef <target_regtype> target_elf_greg_t;
2295 * #define ELF_NREG <number of registers>
2296 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
2297 *
2298 * Last step is to implement target specific function that copies registers
2299 * from given cpu into just specified register set. Prototype is:
2300 *
2301 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
2302 * const CPUArchState *env);
2303 *
2304 * Parameters:
2305 * regs - copy register values into here (allocated and zeroed by caller)
2306 * env - copy registers from here
2307 *
2308 * Example for ARM target is provided in this file.
2309 */
2310
2311 /* An ELF note in memory */
2312 struct memelfnote {
2313 const char *name;
2314 size_t namesz;
2315 size_t namesz_rounded;
2316 int type;
2317 size_t datasz;
2318 size_t datasz_rounded;
2319 void *data;
2320 size_t notesz;
2321 };
2322
2323 struct target_elf_siginfo {
2324 abi_int si_signo; /* signal number */
2325 abi_int si_code; /* extra code */
2326 abi_int si_errno; /* errno */
2327 };
2328
2329 struct target_elf_prstatus {
2330 struct target_elf_siginfo pr_info; /* Info associated with signal */
2331 abi_short pr_cursig; /* Current signal */
2332 abi_ulong pr_sigpend; /* XXX */
2333 abi_ulong pr_sighold; /* XXX */
2334 target_pid_t pr_pid;
2335 target_pid_t pr_ppid;
2336 target_pid_t pr_pgrp;
2337 target_pid_t pr_sid;
2338 struct target_timeval pr_utime; /* XXX User time */
2339 struct target_timeval pr_stime; /* XXX System time */
2340 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2341 struct target_timeval pr_cstime; /* XXX Cumulative system time */
2342 target_elf_gregset_t pr_reg; /* GP registers */
2343 abi_int pr_fpvalid; /* XXX */
2344 };
2345
2346 #define ELF_PRARGSZ (80) /* Number of chars for args */
2347
2348 struct target_elf_prpsinfo {
2349 char pr_state; /* numeric process state */
2350 char pr_sname; /* char for pr_state */
2351 char pr_zomb; /* zombie */
2352 char pr_nice; /* nice val */
2353 abi_ulong pr_flag; /* flags */
2354 target_uid_t pr_uid;
2355 target_gid_t pr_gid;
2356 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
2357 /* Lots missing */
2358 char pr_fname[16]; /* filename of executable */
2359 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2360 };
2361
2362 /* Here is the structure in which status of each thread is captured. */
2363 struct elf_thread_status {
2364 QTAILQ_ENTRY(elf_thread_status) ets_link;
2365 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
2366 #if 0
2367 elf_fpregset_t fpu; /* NT_PRFPREG */
2368 struct task_struct *thread;
2369 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2370 #endif
2371 struct memelfnote notes[1];
2372 int num_notes;
2373 };
2374
2375 struct elf_note_info {
2376 struct memelfnote *notes;
2377 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2378 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
2379
2380 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
2381 #if 0
2382 /*
2383 * Current version of ELF coredump doesn't support
2384 * dumping fp regs etc.
2385 */
2386 elf_fpregset_t *fpu;
2387 elf_fpxregset_t *xfpu;
2388 int thread_status_size;
2389 #endif
2390 int notes_size;
2391 int numnote;
2392 };
2393
2394 struct vm_area_struct {
2395 target_ulong vma_start; /* start vaddr of memory region */
2396 target_ulong vma_end; /* end vaddr of memory region */
2397 abi_ulong vma_flags; /* protection etc. flags for the region */
2398 QTAILQ_ENTRY(vm_area_struct) vma_link;
2399 };
2400
2401 struct mm_struct {
2402 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
2403 int mm_count; /* number of mappings */
2404 };
2405
2406 static struct mm_struct *vma_init(void);
2407 static void vma_delete(struct mm_struct *);
2408 static int vma_add_mapping(struct mm_struct *, target_ulong,
2409 target_ulong, abi_ulong);
2410 static int vma_get_mapping_count(const struct mm_struct *);
2411 static struct vm_area_struct *vma_first(const struct mm_struct *);
2412 static struct vm_area_struct *vma_next(struct vm_area_struct *);
2413 static abi_ulong vma_dump_size(const struct vm_area_struct *);
2414 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2415 unsigned long flags);
2416
2417 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2418 static void fill_note(struct memelfnote *, const char *, int,
2419 unsigned int, void *);
2420 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2421 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
2422 static void fill_auxv_note(struct memelfnote *, const TaskState *);
2423 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2424 static size_t note_size(const struct memelfnote *);
2425 static void free_note_info(struct elf_note_info *);
2426 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2427 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
2428 static int core_dump_filename(const TaskState *, char *, size_t);
2429
2430 static int dump_write(int, const void *, size_t);
2431 static int write_note(struct memelfnote *, int);
2432 static int write_note_info(struct elf_note_info *, int);
2433
2434 #ifdef BSWAP_NEEDED
2435 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
2436 {
2437 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2438 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2439 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
2440 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2441 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2442 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
2443 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2444 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2445 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2446 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2447 /* cpu times are not filled, so we skip them */
2448 /* regs should be in correct format already */
2449 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2450 }
2451
2452 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
2453 {
2454 psinfo->pr_flag = tswapal(psinfo->pr_flag);
2455 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2456 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2457 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2458 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2459 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2460 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2461 }
2462
2463 static void bswap_note(struct elf_note *en)
2464 {
2465 bswap32s(&en->n_namesz);
2466 bswap32s(&en->n_descsz);
2467 bswap32s(&en->n_type);
2468 }
2469 #else
2470 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2471 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2472 static inline void bswap_note(struct elf_note *en) { }
2473 #endif /* BSWAP_NEEDED */
2474
2475 /*
2476 * Minimal support for linux memory regions. These are needed
2477 * when we are finding out what memory exactly belongs to
2478 * emulated process. No locks needed here, as long as
2479 * thread that received the signal is stopped.
2480 */
2481
2482 static struct mm_struct *vma_init(void)
2483 {
2484 struct mm_struct *mm;
2485
2486 if ((mm = g_malloc(sizeof (*mm))) == NULL)
2487 return (NULL);
2488
2489 mm->mm_count = 0;
2490 QTAILQ_INIT(&mm->mm_mmap);
2491
2492 return (mm);
2493 }
2494
2495 static void vma_delete(struct mm_struct *mm)
2496 {
2497 struct vm_area_struct *vma;
2498
2499 while ((vma = vma_first(mm)) != NULL) {
2500 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
2501 g_free(vma);
2502 }
2503 g_free(mm);
2504 }
2505
2506 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
2507 target_ulong end, abi_ulong flags)
2508 {
2509 struct vm_area_struct *vma;
2510
2511 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
2512 return (-1);
2513
2514 vma->vma_start = start;
2515 vma->vma_end = end;
2516 vma->vma_flags = flags;
2517
2518 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
2519 mm->mm_count++;
2520
2521 return (0);
2522 }
2523
2524 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2525 {
2526 return (QTAILQ_FIRST(&mm->mm_mmap));
2527 }
2528
2529 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2530 {
2531 return (QTAILQ_NEXT(vma, vma_link));
2532 }
2533
2534 static int vma_get_mapping_count(const struct mm_struct *mm)
2535 {
2536 return (mm->mm_count);
2537 }
2538
2539 /*
2540 * Calculate file (dump) size of given memory region.
2541 */
2542 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2543 {
2544 /* if we cannot even read the first page, skip it */
2545 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2546 return (0);
2547
2548 /*
2549 * Usually we don't dump executable pages as they contain
2550 * non-writable code that debugger can read directly from
2551 * target library etc. However, thread stacks are marked
2552 * also executable so we read in first page of given region
2553 * and check whether it contains elf header. If there is
2554 * no elf header, we dump it.
2555 */
2556 if (vma->vma_flags & PROT_EXEC) {
2557 char page[TARGET_PAGE_SIZE];
2558
2559 copy_from_user(page, vma->vma_start, sizeof (page));
2560 if ((page[EI_MAG0] == ELFMAG0) &&
2561 (page[EI_MAG1] == ELFMAG1) &&
2562 (page[EI_MAG2] == ELFMAG2) &&
2563 (page[EI_MAG3] == ELFMAG3)) {
2564 /*
2565 * Mappings are possibly from ELF binary. Don't dump
2566 * them.
2567 */
2568 return (0);
2569 }
2570 }
2571
2572 return (vma->vma_end - vma->vma_start);
2573 }
2574
2575 static int vma_walker(void *priv, target_ulong start, target_ulong end,
2576 unsigned long flags)
2577 {
2578 struct mm_struct *mm = (struct mm_struct *)priv;
2579
2580 vma_add_mapping(mm, start, end, flags);
2581 return (0);
2582 }
2583
2584 static void fill_note(struct memelfnote *note, const char *name, int type,
2585 unsigned int sz, void *data)
2586 {
2587 unsigned int namesz;
2588
2589 namesz = strlen(name) + 1;
2590 note->name = name;
2591 note->namesz = namesz;
2592 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2593 note->type = type;
2594 note->datasz = sz;
2595 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2596
2597 note->data = data;
2598
2599 /*
2600 * We calculate rounded up note size here as specified by
2601 * ELF document.
2602 */
2603 note->notesz = sizeof (struct elf_note) +
2604 note->namesz_rounded + note->datasz_rounded;
2605 }
2606
2607 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2608 uint32_t flags)
2609 {
2610 (void) memset(elf, 0, sizeof(*elf));
2611
2612 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2613 elf->e_ident[EI_CLASS] = ELF_CLASS;
2614 elf->e_ident[EI_DATA] = ELF_DATA;
2615 elf->e_ident[EI_VERSION] = EV_CURRENT;
2616 elf->e_ident[EI_OSABI] = ELF_OSABI;
2617
2618 elf->e_type = ET_CORE;
2619 elf->e_machine = machine;
2620 elf->e_version = EV_CURRENT;
2621 elf->e_phoff = sizeof(struct elfhdr);
2622 elf->e_flags = flags;
2623 elf->e_ehsize = sizeof(struct elfhdr);
2624 elf->e_phentsize = sizeof(struct elf_phdr);
2625 elf->e_phnum = segs;
2626
2627 bswap_ehdr(elf);
2628 }
2629
2630 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2631 {
2632 phdr->p_type = PT_NOTE;
2633 phdr->p_offset = offset;
2634 phdr->p_vaddr = 0;
2635 phdr->p_paddr = 0;
2636 phdr->p_filesz = sz;
2637 phdr->p_memsz = 0;
2638 phdr->p_flags = 0;
2639 phdr->p_align = 0;
2640
2641 bswap_phdr(phdr, 1);
2642 }
2643
2644 static size_t note_size(const struct memelfnote *note)
2645 {
2646 return (note->notesz);
2647 }
2648
2649 static void fill_prstatus(struct target_elf_prstatus *prstatus,
2650 const TaskState *ts, int signr)
2651 {
2652 (void) memset(prstatus, 0, sizeof (*prstatus));
2653 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2654 prstatus->pr_pid = ts->ts_tid;
2655 prstatus->pr_ppid = getppid();
2656 prstatus->pr_pgrp = getpgrp();
2657 prstatus->pr_sid = getsid(0);
2658
2659 bswap_prstatus(prstatus);
2660 }
2661
2662 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
2663 {
2664 char *base_filename;
2665 unsigned int i, len;
2666
2667 (void) memset(psinfo, 0, sizeof (*psinfo));
2668
2669 len = ts->info->arg_end - ts->info->arg_start;
2670 if (len >= ELF_PRARGSZ)
2671 len = ELF_PRARGSZ - 1;
2672 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2673 return -EFAULT;
2674 for (i = 0; i < len; i++)
2675 if (psinfo->pr_psargs[i] == 0)
2676 psinfo->pr_psargs[i] = ' ';
2677 psinfo->pr_psargs[len] = 0;
2678
2679 psinfo->pr_pid = getpid();
2680 psinfo->pr_ppid = getppid();
2681 psinfo->pr_pgrp = getpgrp();
2682 psinfo->pr_sid = getsid(0);
2683 psinfo->pr_uid = getuid();
2684 psinfo->pr_gid = getgid();
2685
2686 base_filename = g_path_get_basename(ts->bprm->filename);
2687 /*
2688 * Using strncpy here is fine: at max-length,
2689 * this field is not NUL-terminated.
2690 */
2691 (void) strncpy(psinfo->pr_fname, base_filename,
2692 sizeof(psinfo->pr_fname));
2693
2694 g_free(base_filename);
2695 bswap_psinfo(psinfo);
2696 return (0);
2697 }
2698
2699 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2700 {
2701 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2702 elf_addr_t orig_auxv = auxv;
2703 void *ptr;
2704 int len = ts->info->auxv_len;
2705
2706 /*
2707 * Auxiliary vector is stored in target process stack. It contains
2708 * {type, value} pairs that we need to dump into note. This is not
2709 * strictly necessary but we do it here for sake of completeness.
2710 */
2711
2712 /* read in whole auxv vector and copy it to memelfnote */
2713 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2714 if (ptr != NULL) {
2715 fill_note(note, "CORE", NT_AUXV, len, ptr);
2716 unlock_user(ptr, auxv, len);
2717 }
2718 }
2719
2720 /*
2721 * Constructs name of coredump file. We have following convention
2722 * for the name:
2723 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2724 *
2725 * Returns 0 in case of success, -1 otherwise (errno is set).
2726 */
2727 static int core_dump_filename(const TaskState *ts, char *buf,
2728 size_t bufsize)
2729 {
2730 char timestamp[64];
2731 char *filename = NULL;
2732 char *base_filename = NULL;
2733 struct timeval tv;
2734 struct tm tm;
2735
2736 assert(bufsize >= PATH_MAX);
2737
2738 if (gettimeofday(&tv, NULL) < 0) {
2739 (void) fprintf(stderr, "unable to get current timestamp: %s",
2740 strerror(errno));
2741 return (-1);
2742 }
2743
2744 filename = strdup(ts->bprm->filename);
2745 base_filename = strdup(basename(filename));
2746 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2747 localtime_r(&tv.tv_sec, &tm));
2748 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2749 base_filename, timestamp, (int)getpid());
2750 free(base_filename);
2751 free(filename);
2752
2753 return (0);
2754 }
2755
2756 static int dump_write(int fd, const void *ptr, size_t size)
2757 {
2758 const char *bufp = (const char *)ptr;
2759 ssize_t bytes_written, bytes_left;
2760 struct rlimit dumpsize;
2761 off_t pos;
2762
2763 bytes_written = 0;
2764 getrlimit(RLIMIT_CORE, &dumpsize);
2765 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2766 if (errno == ESPIPE) { /* not a seekable stream */
2767 bytes_left = size;
2768 } else {
2769 return pos;
2770 }
2771 } else {
2772 if (dumpsize.rlim_cur <= pos) {
2773 return -1;
2774 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2775 bytes_left = size;
2776 } else {
2777 size_t limit_left=dumpsize.rlim_cur - pos;
2778 bytes_left = limit_left >= size ? size : limit_left ;
2779 }
2780 }
2781
2782 /*
2783 * In normal conditions, single write(2) should do but
2784 * in case of socket etc. this mechanism is more portable.
2785 */
2786 do {
2787 bytes_written = write(fd, bufp, bytes_left);
2788 if (bytes_written < 0) {
2789 if (errno == EINTR)
2790 continue;
2791 return (-1);
2792 } else if (bytes_written == 0) { /* eof */
2793 return (-1);
2794 }
2795 bufp += bytes_written;
2796 bytes_left -= bytes_written;
2797 } while (bytes_left > 0);
2798
2799 return (0);
2800 }
2801
2802 static int write_note(struct memelfnote *men, int fd)
2803 {
2804 struct elf_note en;
2805
2806 en.n_namesz = men->namesz;
2807 en.n_type = men->type;
2808 en.n_descsz = men->datasz;
2809
2810 bswap_note(&en);
2811
2812 if (dump_write(fd, &en, sizeof(en)) != 0)
2813 return (-1);
2814 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2815 return (-1);
2816 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
2817 return (-1);
2818
2819 return (0);
2820 }
2821
2822 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
2823 {
2824 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2825 TaskState *ts = (TaskState *)cpu->opaque;
2826 struct elf_thread_status *ets;
2827
2828 ets = g_malloc0(sizeof (*ets));
2829 ets->num_notes = 1; /* only prstatus is dumped */
2830 fill_prstatus(&ets->prstatus, ts, 0);
2831 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2832 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2833 &ets->prstatus);
2834
2835 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
2836
2837 info->notes_size += note_size(&ets->notes[0]);
2838 }
2839
2840 static void init_note_info(struct elf_note_info *info)
2841 {
2842 /* Initialize the elf_note_info structure so that it is at
2843 * least safe to call free_note_info() on it. Must be
2844 * called before calling fill_note_info().
2845 */
2846 memset(info, 0, sizeof (*info));
2847 QTAILQ_INIT(&info->thread_list);
2848 }
2849
2850 static int fill_note_info(struct elf_note_info *info,
2851 long signr, const CPUArchState *env)
2852 {
2853 #define NUMNOTES 3
2854 CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2855 TaskState *ts = (TaskState *)cpu->opaque;
2856 int i;
2857
2858 info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote));
2859 if (info->notes == NULL)
2860 return (-ENOMEM);
2861 info->prstatus = g_malloc0(sizeof (*info->prstatus));
2862 if (info->prstatus == NULL)
2863 return (-ENOMEM);
2864 info->psinfo = g_malloc0(sizeof (*info->psinfo));
2865 if (info->prstatus == NULL)
2866 return (-ENOMEM);
2867
2868 /*
2869 * First fill in status (and registers) of current thread
2870 * including process info & aux vector.
2871 */
2872 fill_prstatus(info->prstatus, ts, signr);
2873 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2874 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2875 sizeof (*info->prstatus), info->prstatus);
2876 fill_psinfo(info->psinfo, ts);
2877 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2878 sizeof (*info->psinfo), info->psinfo);
2879 fill_auxv_note(&info->notes[2], ts);
2880 info->numnote = 3;
2881
2882 info->notes_size = 0;
2883 for (i = 0; i < info->numnote; i++)
2884 info->notes_size += note_size(&info->notes[i]);
2885
2886 /* read and fill status of all threads */
2887 cpu_list_lock();
2888 CPU_FOREACH(cpu) {
2889 if (cpu == thread_cpu) {
2890 continue;
2891 }
2892 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
2893 }
2894 cpu_list_unlock();
2895
2896 return (0);
2897 }
2898
2899 static void free_note_info(struct elf_note_info *info)
2900 {
2901 struct elf_thread_status *ets;
2902
2903 while (!QTAILQ_EMPTY(&info->thread_list)) {
2904 ets = QTAILQ_FIRST(&info->thread_list);
2905 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
2906 g_free(ets);
2907 }
2908
2909 g_free(info->prstatus);
2910 g_free(info->psinfo);
2911 g_free(info->notes);
2912 }
2913
2914 static int write_note_info(struct elf_note_info *info, int fd)
2915 {
2916 struct elf_thread_status *ets;
2917 int i, error = 0;
2918
2919 /* write prstatus, psinfo and auxv for current thread */
2920 for (i = 0; i < info->numnote; i++)
2921 if ((error = write_note(&info->notes[i], fd)) != 0)
2922 return (error);
2923
2924 /* write prstatus for each thread */
2925 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
2926 if ((error = write_note(&ets->notes[0], fd)) != 0)
2927 return (error);
2928 }
2929
2930 return (0);
2931 }
2932
2933 /*
2934 * Write out ELF coredump.
2935 *
2936 * See documentation of ELF object file format in:
2937 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2938 *
2939 * Coredump format in linux is following:
2940 *
2941 * 0 +----------------------+ \
2942 * | ELF header | ET_CORE |
2943 * +----------------------+ |
2944 * | ELF program headers | |--- headers
2945 * | - NOTE section | |
2946 * | - PT_LOAD sections | |
2947 * +----------------------+ /
2948 * | NOTEs: |
2949 * | - NT_PRSTATUS |
2950 * | - NT_PRSINFO |
2951 * | - NT_AUXV |
2952 * +----------------------+ <-- aligned to target page
2953 * | Process memory dump |
2954 * : :
2955 * . .
2956 * : :
2957 * | |
2958 * +----------------------+
2959 *
2960 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2961 * NT_PRSINFO -> struct elf_prpsinfo
2962 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2963 *
2964 * Format follows System V format as close as possible. Current
2965 * version limitations are as follows:
2966 * - no floating point registers are dumped
2967 *
2968 * Function returns 0 in case of success, negative errno otherwise.
2969 *
2970 * TODO: make this work also during runtime: it should be
2971 * possible to force coredump from running process and then
2972 * continue processing. For example qemu could set up SIGUSR2
2973 * handler (provided that target process haven't registered
2974 * handler for that) that does the dump when signal is received.
2975 */
2976 static int elf_core_dump(int signr, const CPUArchState *env)
2977 {
2978 const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env);
2979 const TaskState *ts = (const TaskState *)cpu->opaque;
2980 struct vm_area_struct *vma = NULL;
2981 char corefile[PATH_MAX];
2982 struct elf_note_info info;
2983 struct elfhdr elf;
2984 struct elf_phdr phdr;
2985 struct rlimit dumpsize;
2986 struct mm_struct *mm = NULL;
2987 off_t offset = 0, data_offset = 0;
2988 int segs = 0;
2989 int fd = -1;
2990
2991 init_note_info(&info);
2992
2993 errno = 0;
2994 getrlimit(RLIMIT_CORE, &dumpsize);
2995 if (dumpsize.rlim_cur == 0)
2996 return 0;
2997
2998 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2999 return (-errno);
3000
3001 if ((fd = open(corefile, O_WRONLY | O_CREAT,
3002 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3003 return (-errno);
3004
3005 /*
3006 * Walk through target process memory mappings and
3007 * set up structure containing this information. After
3008 * this point vma_xxx functions can be used.
3009 */
3010 if ((mm = vma_init()) == NULL)
3011 goto out;
3012
3013 walk_memory_regions(mm, vma_walker);
3014 segs = vma_get_mapping_count(mm);
3015
3016 /*
3017 * Construct valid coredump ELF header. We also
3018 * add one more segment for notes.
3019 */
3020 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3021 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3022 goto out;
3023
3024 /* fill in the in-memory version of notes */
3025 if (fill_note_info(&info, signr, env) < 0)
3026 goto out;
3027
3028 offset += sizeof (elf); /* elf header */
3029 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3030
3031 /* write out notes program header */
3032 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3033
3034 offset += info.notes_size;
3035 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3036 goto out;
3037
3038 /*
3039 * ELF specification wants data to start at page boundary so
3040 * we align it here.
3041 */
3042 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
3043
3044 /*
3045 * Write program headers for memory regions mapped in
3046 * the target process.
3047 */
3048 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3049 (void) memset(&phdr, 0, sizeof (phdr));
3050
3051 phdr.p_type = PT_LOAD;
3052 phdr.p_offset = offset;
3053 phdr.p_vaddr = vma->vma_start;
3054 phdr.p_paddr = 0;
3055 phdr.p_filesz = vma_dump_size(vma);
3056 offset += phdr.p_filesz;
3057 phdr.p_memsz = vma->vma_end - vma->vma_start;
3058 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3059 if (vma->vma_flags & PROT_WRITE)
3060 phdr.p_flags |= PF_W;
3061 if (vma->vma_flags & PROT_EXEC)
3062 phdr.p_flags |= PF_X;
3063 phdr.p_align = ELF_EXEC_PAGESIZE;
3064
3065 bswap_phdr(&phdr, 1);
3066 dump_write(fd, &phdr, sizeof (phdr));
3067 }
3068
3069 /*
3070 * Next we write notes just after program headers. No
3071 * alignment needed here.
3072 */
3073 if (write_note_info(&info, fd) < 0)
3074 goto out;
3075
3076 /* align data to page boundary */
3077 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
3078 goto out;
3079
3080 /*
3081 * Finally we can dump process memory into corefile as well.
3082 */
3083 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3084 abi_ulong addr;
3085 abi_ulong end;
3086
3087 end = vma->vma_start + vma_dump_size(vma);
3088
3089 for (addr = vma->vma_start; addr < end;
3090 addr += TARGET_PAGE_SIZE) {
3091 char page[TARGET_PAGE_SIZE];
3092 int error;
3093
3094 /*
3095 * Read in page from target process memory and
3096 * write it to coredump file.
3097 */
3098 error = copy_from_user(page, addr, sizeof (page));
3099 if (error != 0) {
3100 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
3101 addr);
3102 errno = -error;
3103 goto out;
3104 }
3105 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
3106 goto out;
3107 }
3108 }
3109
3110 out:
3111 free_note_info(&info);
3112 if (mm != NULL)
3113 vma_delete(mm);
3114 (void) close(fd);
3115
3116 if (errno != 0)
3117 return (-errno);
3118 return (0);
3119 }
3120 #endif /* USE_ELF_CORE_DUMP */
3121
3122 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
3123 {
3124 init_thread(regs, infop);
3125 }