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