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