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