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