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