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