]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/elfload.c
target/riscv: Allow debugger to access seed CSR
[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>
30ab9ef2 6#include <sys/shm.h>
31e31b8a 7
3ef693a0 8#include "qemu.h"
3b249d26 9#include "user-internals.h"
db2af69d 10#include "signal-common.h"
3ad0a769 11#include "loader.h"
5423e6d3 12#include "user-mmap.h"
76cad711 13#include "disas/disas.h"
ce543844 14#include "qemu/bitops.h"
f348b6d1 15#include "qemu/path.h"
dc5e9ac7 16#include "qemu/queue.h"
c6a2377f 17#include "qemu/guest-random.h"
6fd59449 18#include "qemu/units.h"
ee947430 19#include "qemu/selfmap.h"
c7f17e7b 20#include "qapi/error.h"
db2af69d 21#include "target_signal.h"
7c10cb38 22#include "accel/tcg/debuginfo.h"
31e31b8a 23
e58ffeb3 24#ifdef _ARCH_PPC64
a6cc84f4 25#undef ARCH_DLINFO
26#undef ELF_PLATFORM
27#undef ELF_HWCAP
ad6919dc 28#undef ELF_HWCAP2
a6cc84f4 29#undef ELF_CLASS
30#undef ELF_DATA
31#undef ELF_ARCH
32#endif
33
edf8e2af
MW
34#define ELF_OSABI ELFOSABI_SYSV
35
cb33da57
BS
36/* from personality.h */
37
38/*
39 * Flags for bug emulation.
40 *
41 * These occupy the top three bytes.
42 */
43enum {
d97ef72e
RH
44 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
45 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
46 descriptors (signal handling) */
47 MMAP_PAGE_ZERO = 0x0100000,
48 ADDR_COMPAT_LAYOUT = 0x0200000,
49 READ_IMPLIES_EXEC = 0x0400000,
50 ADDR_LIMIT_32BIT = 0x0800000,
51 SHORT_INODE = 0x1000000,
52 WHOLE_SECONDS = 0x2000000,
53 STICKY_TIMEOUTS = 0x4000000,
54 ADDR_LIMIT_3GB = 0x8000000,
cb33da57
BS
55};
56
57/*
58 * Personality types.
59 *
60 * These go in the low byte. Avoid using the top bit, it will
61 * conflict with error returns.
62 */
63enum {
d97ef72e
RH
64 PER_LINUX = 0x0000,
65 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
66 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
67 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
68 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
69 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
70 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
71 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
72 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
73 PER_BSD = 0x0006,
74 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
75 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
76 PER_LINUX32 = 0x0008,
77 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
78 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
79 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
80 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
81 PER_RISCOS = 0x000c,
82 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
83 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
84 PER_OSF4 = 0x000f, /* OSF/1 v4 */
85 PER_HPUX = 0x0010,
86 PER_MASK = 0x00ff,
cb33da57
BS
87};
88
89/*
90 * Return the base personality without flags.
91 */
d97ef72e 92#define personality(pers) (pers & PER_MASK)
cb33da57 93
3cb10cfa
CL
94int info_is_fdpic(struct image_info *info)
95{
96 return info->personality == PER_LINUX_FDPIC;
97}
98
83fb7adf
FB
99/* this flag is uneffective under linux too, should be deleted */
100#ifndef MAP_DENYWRITE
101#define MAP_DENYWRITE 0
102#endif
103
104/* should probably go in elf.h */
105#ifndef ELIBBAD
106#define ELIBBAD 80
107#endif
108
ee3eb3a7 109#if TARGET_BIG_ENDIAN
28490231
RH
110#define ELF_DATA ELFDATA2MSB
111#else
112#define ELF_DATA ELFDATA2LSB
113#endif
114
a29f998d 115#ifdef TARGET_ABI_MIPSN32
918fc54c
PB
116typedef abi_ullong target_elf_greg_t;
117#define tswapreg(ptr) tswap64(ptr)
a29f998d
PB
118#else
119typedef abi_ulong target_elf_greg_t;
120#define tswapreg(ptr) tswapal(ptr)
121#endif
122
21e807fa 123#ifdef USE_UID16
1ddd592f
PB
124typedef abi_ushort target_uid_t;
125typedef abi_ushort target_gid_t;
21e807fa 126#else
f8fd4fc4
PB
127typedef abi_uint target_uid_t;
128typedef abi_uint target_gid_t;
21e807fa 129#endif
f8fd4fc4 130typedef abi_int target_pid_t;
21e807fa 131
30ac07d4
FB
132#ifdef TARGET_I386
133
15338fd7
FB
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
9263ba84
RH
149#define ELF_PLATFORM "x86_64"
150
84409ddb
JM
151static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
152{
153 regs->rax = 0;
154 regs->rsp = infop->start_stack;
155 regs->rip = infop->entry;
156}
157
9edc5d79 158#define ELF_NREG 27
c227f099 159typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
9edc5d79
MW
160
161/*
162 * Note that ELF_NREG should be 29 as there should be place for
163 * TRAPNO and ERR "registers" as well but linux doesn't dump
164 * those.
165 *
166 * See linux kernel: arch/x86/include/asm/elf.h
167 */
05390248 168static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
9edc5d79 169{
030912e0
IL
170 (*regs)[0] = tswapreg(env->regs[15]);
171 (*regs)[1] = tswapreg(env->regs[14]);
172 (*regs)[2] = tswapreg(env->regs[13]);
173 (*regs)[3] = tswapreg(env->regs[12]);
174 (*regs)[4] = tswapreg(env->regs[R_EBP]);
175 (*regs)[5] = tswapreg(env->regs[R_EBX]);
176 (*regs)[6] = tswapreg(env->regs[11]);
177 (*regs)[7] = tswapreg(env->regs[10]);
178 (*regs)[8] = tswapreg(env->regs[9]);
179 (*regs)[9] = tswapreg(env->regs[8]);
180 (*regs)[10] = tswapreg(env->regs[R_EAX]);
181 (*regs)[11] = tswapreg(env->regs[R_ECX]);
182 (*regs)[12] = tswapreg(env->regs[R_EDX]);
183 (*regs)[13] = tswapreg(env->regs[R_ESI]);
184 (*regs)[14] = tswapreg(env->regs[R_EDI]);
185 (*regs)[15] = tswapreg(env->regs[R_EAX]); /* XXX */
186 (*regs)[16] = tswapreg(env->eip);
187 (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff);
188 (*regs)[18] = tswapreg(env->eflags);
189 (*regs)[19] = tswapreg(env->regs[R_ESP]);
190 (*regs)[20] = tswapreg(env->segs[R_SS].selector & 0xffff);
191 (*regs)[21] = tswapreg(env->segs[R_FS].selector & 0xffff);
192 (*regs)[22] = tswapreg(env->segs[R_GS].selector & 0xffff);
193 (*regs)[23] = tswapreg(env->segs[R_DS].selector & 0xffff);
194 (*regs)[24] = tswapreg(env->segs[R_ES].selector & 0xffff);
195 (*regs)[25] = tswapreg(env->segs[R_FS].selector & 0xffff);
196 (*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff);
9edc5d79
MW
197}
198
d461b73e
RH
199#if ULONG_MAX > UINT32_MAX
200#define INIT_GUEST_COMMPAGE
201static bool init_guest_commpage(void)
202{
203 /*
204 * The vsyscall page is at a high negative address aka kernel space,
205 * which means that we cannot actually allocate it with target_mmap.
206 * We still should be able to use page_set_flags, unless the user
207 * has specified -R reserved_va, which would trigger an assert().
208 */
209 if (reserved_va != 0 &&
210 TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE >= reserved_va) {
211 error_report("Cannot allocate vsyscall page");
212 exit(EXIT_FAILURE);
213 }
214 page_set_flags(TARGET_VSYSCALL_PAGE,
215 TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE,
216 PAGE_EXEC | PAGE_VALID);
217 return true;
218}
219#endif
84409ddb
JM
220#else
221
30ac07d4
FB
222#define ELF_START_MMAP 0x80000000
223
30ac07d4
FB
224/*
225 * This is used to ensure we don't load something for the wrong architecture.
226 */
227#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
228
229/*
230 * These are used to set parameters in the core dumps.
231 */
d97ef72e 232#define ELF_CLASS ELFCLASS32
d97ef72e 233#define ELF_ARCH EM_386
30ac07d4 234
9263ba84 235#define ELF_PLATFORM get_elf_platform()
872f3d04 236#define EXSTACK_DEFAULT true
9263ba84
RH
237
238static const char *get_elf_platform(void)
239{
240 static char elf_platform[] = "i386";
241 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
242 if (family > 6) {
243 family = 6;
244 }
245 if (family >= 3) {
246 elf_platform[1] = '0' + family;
247 }
248 return elf_platform;
249}
250
d97ef72e
RH
251static inline void init_thread(struct target_pt_regs *regs,
252 struct image_info *infop)
b346ff46
FB
253{
254 regs->esp = infop->start_stack;
255 regs->eip = infop->entry;
e5fe0c52
PB
256
257 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
258 starts %edx contains a pointer to a function which might be
259 registered using `atexit'. This provides a mean for the
260 dynamic linker to call DT_FINI functions for shared libraries
261 that have been loaded before the code runs.
262
263 A value of 0 tells we have no such handler. */
264 regs->edx = 0;
b346ff46 265}
9edc5d79 266
9edc5d79 267#define ELF_NREG 17
c227f099 268typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
9edc5d79
MW
269
270/*
271 * Note that ELF_NREG should be 19 as there should be place for
272 * TRAPNO and ERR "registers" as well but linux doesn't dump
273 * those.
274 *
275 * See linux kernel: arch/x86/include/asm/elf.h
276 */
05390248 277static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
9edc5d79 278{
030912e0
IL
279 (*regs)[0] = tswapreg(env->regs[R_EBX]);
280 (*regs)[1] = tswapreg(env->regs[R_ECX]);
281 (*regs)[2] = tswapreg(env->regs[R_EDX]);
282 (*regs)[3] = tswapreg(env->regs[R_ESI]);
283 (*regs)[4] = tswapreg(env->regs[R_EDI]);
284 (*regs)[5] = tswapreg(env->regs[R_EBP]);
285 (*regs)[6] = tswapreg(env->regs[R_EAX]);
286 (*regs)[7] = tswapreg(env->segs[R_DS].selector & 0xffff);
287 (*regs)[8] = tswapreg(env->segs[R_ES].selector & 0xffff);
288 (*regs)[9] = tswapreg(env->segs[R_FS].selector & 0xffff);
289 (*regs)[10] = tswapreg(env->segs[R_GS].selector & 0xffff);
290 (*regs)[11] = tswapreg(env->regs[R_EAX]); /* XXX */
291 (*regs)[12] = tswapreg(env->eip);
292 (*regs)[13] = tswapreg(env->segs[R_CS].selector & 0xffff);
293 (*regs)[14] = tswapreg(env->eflags);
294 (*regs)[15] = tswapreg(env->regs[R_ESP]);
295 (*regs)[16] = tswapreg(env->segs[R_SS].selector & 0xffff);
9edc5d79 296}
84409ddb 297#endif
b346ff46 298
9edc5d79 299#define USE_ELF_CORE_DUMP
d97ef72e 300#define ELF_EXEC_PAGESIZE 4096
b346ff46
FB
301
302#endif
303
304#ifdef TARGET_ARM
305
24e76ff0
PM
306#ifndef TARGET_AARCH64
307/* 32 bit ARM definitions */
308
b346ff46
FB
309#define ELF_START_MMAP 0x80000000
310
b597c3f7 311#define ELF_ARCH EM_ARM
d97ef72e 312#define ELF_CLASS ELFCLASS32
872f3d04 313#define EXSTACK_DEFAULT true
b346ff46 314
d97ef72e
RH
315static inline void init_thread(struct target_pt_regs *regs,
316 struct image_info *infop)
b346ff46 317{
992f48a0 318 abi_long stack = infop->start_stack;
b346ff46 319 memset(regs, 0, sizeof(*regs));
99033cae 320
167e4cdc
PM
321 regs->uregs[16] = ARM_CPU_MODE_USR;
322 if (infop->entry & 1) {
323 regs->uregs[16] |= CPSR_T;
324 }
325 regs->uregs[15] = infop->entry & 0xfffffffe;
326 regs->uregs[13] = infop->start_stack;
2f619698 327 /* FIXME - what to for failure of get_user()? */
167e4cdc
PM
328 get_user_ual(regs->uregs[2], stack + 8); /* envp */
329 get_user_ual(regs->uregs[1], stack + 4); /* envp */
a1516e92 330 /* XXX: it seems that r0 is zeroed after ! */
167e4cdc 331 regs->uregs[0] = 0;
e5fe0c52 332 /* For uClinux PIC binaries. */
863cf0b7 333 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
167e4cdc 334 regs->uregs[10] = infop->start_data;
3cb10cfa
CL
335
336 /* Support ARM FDPIC. */
337 if (info_is_fdpic(infop)) {
338 /* As described in the ABI document, r7 points to the loadmap info
339 * prepared by the kernel. If an interpreter is needed, r8 points
340 * to the interpreter loadmap and r9 points to the interpreter
341 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
342 * r9 points to the main program PT_DYNAMIC info.
343 */
344 regs->uregs[7] = infop->loadmap_addr;
345 if (infop->interpreter_loadmap_addr) {
346 /* Executable is dynamically loaded. */
347 regs->uregs[8] = infop->interpreter_loadmap_addr;
348 regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
349 } else {
350 regs->uregs[8] = 0;
351 regs->uregs[9] = infop->pt_dynamic_addr;
352 }
353 }
b346ff46
FB
354}
355
edf8e2af 356#define ELF_NREG 18
c227f099 357typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
edf8e2af 358
05390248 359static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
edf8e2af 360{
86cd7b2d
PB
361 (*regs)[0] = tswapreg(env->regs[0]);
362 (*regs)[1] = tswapreg(env->regs[1]);
363 (*regs)[2] = tswapreg(env->regs[2]);
364 (*regs)[3] = tswapreg(env->regs[3]);
365 (*regs)[4] = tswapreg(env->regs[4]);
366 (*regs)[5] = tswapreg(env->regs[5]);
367 (*regs)[6] = tswapreg(env->regs[6]);
368 (*regs)[7] = tswapreg(env->regs[7]);
369 (*regs)[8] = tswapreg(env->regs[8]);
370 (*regs)[9] = tswapreg(env->regs[9]);
371 (*regs)[10] = tswapreg(env->regs[10]);
372 (*regs)[11] = tswapreg(env->regs[11]);
373 (*regs)[12] = tswapreg(env->regs[12]);
374 (*regs)[13] = tswapreg(env->regs[13]);
375 (*regs)[14] = tswapreg(env->regs[14]);
376 (*regs)[15] = tswapreg(env->regs[15]);
377
378 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
379 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
edf8e2af
MW
380}
381
30ac07d4 382#define USE_ELF_CORE_DUMP
d97ef72e 383#define ELF_EXEC_PAGESIZE 4096
30ac07d4 384
afce2927
FB
385enum
386{
d97ef72e
RH
387 ARM_HWCAP_ARM_SWP = 1 << 0,
388 ARM_HWCAP_ARM_HALF = 1 << 1,
389 ARM_HWCAP_ARM_THUMB = 1 << 2,
390 ARM_HWCAP_ARM_26BIT = 1 << 3,
391 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
392 ARM_HWCAP_ARM_FPA = 1 << 5,
393 ARM_HWCAP_ARM_VFP = 1 << 6,
394 ARM_HWCAP_ARM_EDSP = 1 << 7,
395 ARM_HWCAP_ARM_JAVA = 1 << 8,
396 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
43ce393e
PM
397 ARM_HWCAP_ARM_CRUNCH = 1 << 10,
398 ARM_HWCAP_ARM_THUMBEE = 1 << 11,
399 ARM_HWCAP_ARM_NEON = 1 << 12,
400 ARM_HWCAP_ARM_VFPv3 = 1 << 13,
401 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
24682654
PM
402 ARM_HWCAP_ARM_TLS = 1 << 15,
403 ARM_HWCAP_ARM_VFPv4 = 1 << 16,
404 ARM_HWCAP_ARM_IDIVA = 1 << 17,
405 ARM_HWCAP_ARM_IDIVT = 1 << 18,
406 ARM_HWCAP_ARM_VFPD32 = 1 << 19,
407 ARM_HWCAP_ARM_LPAE = 1 << 20,
408 ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
afce2927
FB
409};
410
ad6919dc
PM
411enum {
412 ARM_HWCAP2_ARM_AES = 1 << 0,
413 ARM_HWCAP2_ARM_PMULL = 1 << 1,
414 ARM_HWCAP2_ARM_SHA1 = 1 << 2,
415 ARM_HWCAP2_ARM_SHA2 = 1 << 3,
416 ARM_HWCAP2_ARM_CRC32 = 1 << 4,
417};
418
6b1275ff
PM
419/* The commpage only exists for 32 bit kernels */
420
66346faf 421#define HI_COMMPAGE (intptr_t)0xffff0f00u
806d1021 422
ee947430
AB
423static bool init_guest_commpage(void)
424{
fbd3c4cf
RH
425 abi_ptr commpage = HI_COMMPAGE & -qemu_host_page_size;
426 void *want = g2h_untagged(commpage);
ee947430 427 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
5c3e87f3 428 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
97cc7560 429
ee947430
AB
430 if (addr == MAP_FAILED) {
431 perror("Allocating guest commpage");
432 exit(EXIT_FAILURE);
97cc7560 433 }
ee947430
AB
434 if (addr != want) {
435 return false;
97cc7560
DDAG
436 }
437
ee947430 438 /* Set kernel helper versions; rest of page is 0. */
3e8f1628 439 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
97cc7560 440
ee947430 441 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
97cc7560 442 perror("Protecting guest commpage");
ee947430 443 exit(EXIT_FAILURE);
97cc7560 444 }
fbd3c4cf
RH
445
446 page_set_flags(commpage, commpage + qemu_host_page_size,
447 PAGE_READ | PAGE_EXEC | PAGE_VALID);
ee947430 448 return true;
97cc7560 449}
adf050b1
BC
450
451#define ELF_HWCAP get_elf_hwcap()
ad6919dc 452#define ELF_HWCAP2 get_elf_hwcap2()
adf050b1
BC
453
454static uint32_t get_elf_hwcap(void)
455{
a2247f8e 456 ARMCPU *cpu = ARM_CPU(thread_cpu);
adf050b1
BC
457 uint32_t hwcaps = 0;
458
459 hwcaps |= ARM_HWCAP_ARM_SWP;
460 hwcaps |= ARM_HWCAP_ARM_HALF;
461 hwcaps |= ARM_HWCAP_ARM_THUMB;
462 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
adf050b1
BC
463
464 /* probe for the extra features */
465#define GET_FEATURE(feat, hwcap) \
a2247f8e 466 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
962fcbf2
RH
467
468#define GET_FEATURE_ID(feat, hwcap) \
469 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
470
24682654
PM
471 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
472 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
adf050b1
BC
473 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
474 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
475 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
24682654 476 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
bfa8a370 477 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
873b73c0
PM
478 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
479 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
bfa8a370
RH
480 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
481
482 if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
483 cpu_isar_feature(aa32_fpdp_v3, cpu)) {
484 hwcaps |= ARM_HWCAP_ARM_VFPv3;
485 if (cpu_isar_feature(aa32_simd_r32, cpu)) {
486 hwcaps |= ARM_HWCAP_ARM_VFPD32;
487 } else {
488 hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
489 }
490 }
491 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
adf050b1
BC
492
493 return hwcaps;
494}
afce2927 495
ad6919dc
PM
496static uint32_t get_elf_hwcap2(void)
497{
498 ARMCPU *cpu = ARM_CPU(thread_cpu);
499 uint32_t hwcaps = 0;
500
962fcbf2
RH
501 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
502 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
503 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
504 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
505 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
ad6919dc
PM
506 return hwcaps;
507}
508
509#undef GET_FEATURE
962fcbf2 510#undef GET_FEATURE_ID
ad6919dc 511
13ec4ec3
RH
512#define ELF_PLATFORM get_elf_platform()
513
514static const char *get_elf_platform(void)
515{
516 CPUARMState *env = thread_cpu->env_ptr;
517
ee3eb3a7 518#if TARGET_BIG_ENDIAN
13ec4ec3
RH
519# define END "b"
520#else
521# define END "l"
522#endif
523
524 if (arm_feature(env, ARM_FEATURE_V8)) {
525 return "v8" END;
526 } else if (arm_feature(env, ARM_FEATURE_V7)) {
527 if (arm_feature(env, ARM_FEATURE_M)) {
528 return "v7m" END;
529 } else {
530 return "v7" END;
531 }
532 } else if (arm_feature(env, ARM_FEATURE_V6)) {
533 return "v6" END;
534 } else if (arm_feature(env, ARM_FEATURE_V5)) {
535 return "v5" END;
536 } else {
537 return "v4" END;
538 }
539
540#undef END
541}
542
24e76ff0
PM
543#else
544/* 64 bit ARM definitions */
545#define ELF_START_MMAP 0x80000000
546
b597c3f7 547#define ELF_ARCH EM_AARCH64
24e76ff0 548#define ELF_CLASS ELFCLASS64
ee3eb3a7 549#if TARGET_BIG_ENDIAN
e20e3ec9
RH
550# define ELF_PLATFORM "aarch64_be"
551#else
552# define ELF_PLATFORM "aarch64"
553#endif
24e76ff0
PM
554
555static inline void init_thread(struct target_pt_regs *regs,
556 struct image_info *infop)
557{
558 abi_long stack = infop->start_stack;
559 memset(regs, 0, sizeof(*regs));
560
561 regs->pc = infop->entry & ~0x3ULL;
562 regs->sp = stack;
563}
564
565#define ELF_NREG 34
566typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
567
568static void elf_core_copy_regs(target_elf_gregset_t *regs,
569 const CPUARMState *env)
570{
571 int i;
572
573 for (i = 0; i < 32; i++) {
574 (*regs)[i] = tswapreg(env->xregs[i]);
575 }
576 (*regs)[32] = tswapreg(env->pc);
577 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
578}
579
580#define USE_ELF_CORE_DUMP
581#define ELF_EXEC_PAGESIZE 4096
582
583enum {
584 ARM_HWCAP_A64_FP = 1 << 0,
585 ARM_HWCAP_A64_ASIMD = 1 << 1,
586 ARM_HWCAP_A64_EVTSTRM = 1 << 2,
587 ARM_HWCAP_A64_AES = 1 << 3,
588 ARM_HWCAP_A64_PMULL = 1 << 4,
589 ARM_HWCAP_A64_SHA1 = 1 << 5,
590 ARM_HWCAP_A64_SHA2 = 1 << 6,
591 ARM_HWCAP_A64_CRC32 = 1 << 7,
955f56d4
AB
592 ARM_HWCAP_A64_ATOMICS = 1 << 8,
593 ARM_HWCAP_A64_FPHP = 1 << 9,
594 ARM_HWCAP_A64_ASIMDHP = 1 << 10,
595 ARM_HWCAP_A64_CPUID = 1 << 11,
596 ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
597 ARM_HWCAP_A64_JSCVT = 1 << 13,
598 ARM_HWCAP_A64_FCMA = 1 << 14,
599 ARM_HWCAP_A64_LRCPC = 1 << 15,
600 ARM_HWCAP_A64_DCPOP = 1 << 16,
601 ARM_HWCAP_A64_SHA3 = 1 << 17,
602 ARM_HWCAP_A64_SM3 = 1 << 18,
603 ARM_HWCAP_A64_SM4 = 1 << 19,
604 ARM_HWCAP_A64_ASIMDDP = 1 << 20,
605 ARM_HWCAP_A64_SHA512 = 1 << 21,
606 ARM_HWCAP_A64_SVE = 1 << 22,
0083a1fa
RH
607 ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
608 ARM_HWCAP_A64_DIT = 1 << 24,
609 ARM_HWCAP_A64_USCAT = 1 << 25,
610 ARM_HWCAP_A64_ILRCPC = 1 << 26,
611 ARM_HWCAP_A64_FLAGM = 1 << 27,
612 ARM_HWCAP_A64_SSBS = 1 << 28,
613 ARM_HWCAP_A64_SB = 1 << 29,
614 ARM_HWCAP_A64_PACA = 1 << 30,
615 ARM_HWCAP_A64_PACG = 1UL << 31,
2041df4a
RH
616
617 ARM_HWCAP2_A64_DCPODP = 1 << 0,
618 ARM_HWCAP2_A64_SVE2 = 1 << 1,
619 ARM_HWCAP2_A64_SVEAES = 1 << 2,
620 ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
621 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
622 ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
623 ARM_HWCAP2_A64_SVESM4 = 1 << 6,
624 ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
625 ARM_HWCAP2_A64_FRINT = 1 << 8,
68948d18
RH
626 ARM_HWCAP2_A64_SVEI8MM = 1 << 9,
627 ARM_HWCAP2_A64_SVEF32MM = 1 << 10,
628 ARM_HWCAP2_A64_SVEF64MM = 1 << 11,
629 ARM_HWCAP2_A64_SVEBF16 = 1 << 12,
630 ARM_HWCAP2_A64_I8MM = 1 << 13,
631 ARM_HWCAP2_A64_BF16 = 1 << 14,
632 ARM_HWCAP2_A64_DGH = 1 << 15,
633 ARM_HWCAP2_A64_RNG = 1 << 16,
634 ARM_HWCAP2_A64_BTI = 1 << 17,
635 ARM_HWCAP2_A64_MTE = 1 << 18,
f9982cea
RH
636 ARM_HWCAP2_A64_ECV = 1 << 19,
637 ARM_HWCAP2_A64_AFP = 1 << 20,
638 ARM_HWCAP2_A64_RPRES = 1 << 21,
639 ARM_HWCAP2_A64_MTE3 = 1 << 22,
640 ARM_HWCAP2_A64_SME = 1 << 23,
641 ARM_HWCAP2_A64_SME_I16I64 = 1 << 24,
642 ARM_HWCAP2_A64_SME_F64F64 = 1 << 25,
643 ARM_HWCAP2_A64_SME_I8I32 = 1 << 26,
644 ARM_HWCAP2_A64_SME_F16F32 = 1 << 27,
645 ARM_HWCAP2_A64_SME_B16F32 = 1 << 28,
646 ARM_HWCAP2_A64_SME_F32F32 = 1 << 29,
647 ARM_HWCAP2_A64_SME_FA64 = 1 << 30,
24e76ff0
PM
648};
649
2041df4a
RH
650#define ELF_HWCAP get_elf_hwcap()
651#define ELF_HWCAP2 get_elf_hwcap2()
652
653#define GET_FEATURE_ID(feat, hwcap) \
654 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
24e76ff0
PM
655
656static uint32_t get_elf_hwcap(void)
657{
658 ARMCPU *cpu = ARM_CPU(thread_cpu);
659 uint32_t hwcaps = 0;
660
661 hwcaps |= ARM_HWCAP_A64_FP;
662 hwcaps |= ARM_HWCAP_A64_ASIMD;
37020ff1 663 hwcaps |= ARM_HWCAP_A64_CPUID;
24e76ff0
PM
664
665 /* probe for the extra features */
962fcbf2
RH
666
667 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
668 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
669 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
670 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
671 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
672 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
673 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
674 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
675 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
5763190f 676 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
962fcbf2
RH
677 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
678 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
679 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
680 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
cd208a1c 681 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
29d26ab2 682 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
1c9af3a9
RH
683 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
684 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
9888bd1e 685 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
b89d9c98 686 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
0d57b499 687 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
2677cf9f 688 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
a1229109 689 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
962fcbf2 690
2041df4a
RH
691 return hwcaps;
692}
693
694static uint32_t get_elf_hwcap2(void)
695{
696 ARMCPU *cpu = ARM_CPU(thread_cpu);
697 uint32_t hwcaps = 0;
698
0d57b499 699 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
cdc8d8b2
RH
700 GET_FEATURE_ID(aa64_sve2, ARM_HWCAP2_A64_SVE2);
701 GET_FEATURE_ID(aa64_sve2_aes, ARM_HWCAP2_A64_SVEAES);
702 GET_FEATURE_ID(aa64_sve2_pmull128, ARM_HWCAP2_A64_SVEPMULL);
703 GET_FEATURE_ID(aa64_sve2_bitperm, ARM_HWCAP2_A64_SVEBITPERM);
704 GET_FEATURE_ID(aa64_sve2_sha3, ARM_HWCAP2_A64_SVESHA3);
705 GET_FEATURE_ID(aa64_sve2_sm4, ARM_HWCAP2_A64_SVESM4);
2041df4a
RH
706 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
707 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
cdc8d8b2
RH
708 GET_FEATURE_ID(aa64_sve_i8mm, ARM_HWCAP2_A64_SVEI8MM);
709 GET_FEATURE_ID(aa64_sve_f32mm, ARM_HWCAP2_A64_SVEF32MM);
710 GET_FEATURE_ID(aa64_sve_f64mm, ARM_HWCAP2_A64_SVEF64MM);
6c47a905 711 GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16);
cdc8d8b2 712 GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM);
6c47a905 713 GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16);
68948d18
RH
714 GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG);
715 GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI);
716 GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE);
f9982cea
RH
717 GET_FEATURE_ID(aa64_sme, (ARM_HWCAP2_A64_SME |
718 ARM_HWCAP2_A64_SME_F32F32 |
719 ARM_HWCAP2_A64_SME_B16F32 |
720 ARM_HWCAP2_A64_SME_F16F32 |
721 ARM_HWCAP2_A64_SME_I8I32));
722 GET_FEATURE_ID(aa64_sme_f64f64, ARM_HWCAP2_A64_SME_F64F64);
723 GET_FEATURE_ID(aa64_sme_i16i64, ARM_HWCAP2_A64_SME_I16I64);
724 GET_FEATURE_ID(aa64_sme_fa64, ARM_HWCAP2_A64_SME_FA64);
24e76ff0
PM
725
726 return hwcaps;
727}
728
2041df4a
RH
729#undef GET_FEATURE_ID
730
24e76ff0
PM
731#endif /* not TARGET_AARCH64 */
732#endif /* TARGET_ARM */
30ac07d4 733
853d6f7a 734#ifdef TARGET_SPARC
a315a145 735#ifdef TARGET_SPARC64
853d6f7a
FB
736
737#define ELF_START_MMAP 0x80000000
cf973e46
AT
738#define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
739 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
992f48a0 740#ifndef TARGET_ABI32
cb33da57 741#define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
992f48a0
BS
742#else
743#define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
744#endif
853d6f7a 745
a315a145 746#define ELF_CLASS ELFCLASS64
5ef54116 747#define ELF_ARCH EM_SPARCV9
a315a145
FB
748#else
749#define ELF_START_MMAP 0x80000000
cf973e46
AT
750#define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
751 | HWCAP_SPARC_MULDIV)
853d6f7a 752#define ELF_CLASS ELFCLASS32
853d6f7a 753#define ELF_ARCH EM_SPARC
089a2256 754#endif /* TARGET_SPARC64 */
853d6f7a 755
d97ef72e
RH
756static inline void init_thread(struct target_pt_regs *regs,
757 struct image_info *infop)
853d6f7a 758{
089a2256 759 /* Note that target_cpu_copy_regs does not read psr/tstate. */
f5155289
FB
760 regs->pc = infop->entry;
761 regs->npc = regs->pc + 4;
762 regs->y = 0;
089a2256
RH
763 regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong)
764 - TARGET_STACK_BIAS);
853d6f7a 765}
089a2256 766#endif /* TARGET_SPARC */
853d6f7a 767
67867308
FB
768#ifdef TARGET_PPC
769
4ecd4d16 770#define ELF_MACHINE PPC_ELF_MACHINE
67867308
FB
771#define ELF_START_MMAP 0x80000000
772
74154d7e 773#if defined(TARGET_PPC64)
84409ddb
JM
774
775#define elf_check_arch(x) ( (x) == EM_PPC64 )
776
d97ef72e 777#define ELF_CLASS ELFCLASS64
84409ddb
JM
778
779#else
780
d97ef72e 781#define ELF_CLASS ELFCLASS32
872f3d04 782#define EXSTACK_DEFAULT true
84409ddb
JM
783
784#endif
785
d97ef72e 786#define ELF_ARCH EM_PPC
67867308 787
df84e4f3
NF
788/* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
789 See arch/powerpc/include/asm/cputable.h. */
790enum {
3efa9a67 791 QEMU_PPC_FEATURE_32 = 0x80000000,
792 QEMU_PPC_FEATURE_64 = 0x40000000,
793 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
794 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
795 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
796 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
797 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
798 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
799 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
800 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
801 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
802 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
803 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
804 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
805 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
806 QEMU_PPC_FEATURE_CELL = 0x00010000,
807 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
808 QEMU_PPC_FEATURE_SMT = 0x00004000,
809 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
810 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
811 QEMU_PPC_FEATURE_PA6T = 0x00000800,
812 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
813 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
814 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
815 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
816 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
817
818 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
819 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
a60438dd
TM
820
821 /* Feature definitions in AT_HWCAP2. */
822 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
823 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
824 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
825 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
826 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
827 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
24c373ec
LV
828 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
829 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
be0c46d4 830 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
24c373ec
LV
831 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
832 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
833 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
834 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
96c343cc
JS
835 QEMU_PPC_FEATURE2_ARCH_3_1 = 0x00040000, /* ISA 3.1 */
836 QEMU_PPC_FEATURE2_MMA = 0x00020000, /* Matrix-Multiply Assist */
df84e4f3
NF
837};
838
839#define ELF_HWCAP get_elf_hwcap()
840
841static uint32_t get_elf_hwcap(void)
842{
a2247f8e 843 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
df84e4f3
NF
844 uint32_t features = 0;
845
846 /* We don't have to be terribly complete here; the high points are
847 Altivec/FP/SPE support. Anything else is just a bonus. */
d97ef72e 848#define GET_FEATURE(flag, feature) \
a2247f8e 849 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
58eb5308
MW
850#define GET_FEATURE2(flags, feature) \
851 do { \
852 if ((cpu->env.insns_flags2 & flags) == flags) { \
853 features |= feature; \
854 } \
855 } while (0)
3efa9a67 856 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
857 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
858 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
859 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
860 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
861 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
862 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
863 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
0e019746
TM
864 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
865 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
866 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
867 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
868 QEMU_PPC_FEATURE_ARCH_2_06);
df84e4f3 869#undef GET_FEATURE
0e019746 870#undef GET_FEATURE2
df84e4f3
NF
871
872 return features;
873}
874
a60438dd
TM
875#define ELF_HWCAP2 get_elf_hwcap2()
876
877static uint32_t get_elf_hwcap2(void)
878{
879 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
880 uint32_t features = 0;
881
882#define GET_FEATURE(flag, feature) \
883 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
884#define GET_FEATURE2(flag, feature) \
885 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
886
887 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
888 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
889 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
24c373ec
LV
890 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
891 QEMU_PPC_FEATURE2_VEC_CRYPTO);
892 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
8a589aeb 893 QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128);
96c343cc
JS
894 GET_FEATURE2(PPC2_ISA310, QEMU_PPC_FEATURE2_ARCH_3_1 |
895 QEMU_PPC_FEATURE2_MMA);
a60438dd
TM
896
897#undef GET_FEATURE
898#undef GET_FEATURE2
899
900 return features;
901}
902
f5155289
FB
903/*
904 * The requirements here are:
905 * - keep the final alignment of sp (sp & 0xf)
906 * - make sure the 32-bit value at the first 16 byte aligned position of
907 * AUXV is greater than 16 for glibc compatibility.
908 * AT_IGNOREPPC is used for that.
909 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
910 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
911 */
0bccf03d 912#define DLINFO_ARCH_ITEMS 5
d97ef72e
RH
913#define ARCH_DLINFO \
914 do { \
623e250a 915 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
d97ef72e 916 /* \
82991bed
PM
917 * Handle glibc compatibility: these magic entries must \
918 * be at the lowest addresses in the final auxv. \
d97ef72e
RH
919 */ \
920 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
921 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
82991bed
PM
922 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
923 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
924 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
d97ef72e 925 } while (0)
f5155289 926
67867308
FB
927static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
928{
67867308 929 _regs->gpr[1] = infop->start_stack;
74154d7e 930#if defined(TARGET_PPC64)
d90b94cd 931 if (get_ppc64_abi(infop) < 2) {
2ccf97ec
PM
932 uint64_t val;
933 get_user_u64(val, infop->entry + 8);
934 _regs->gpr[2] = val + infop->load_bias;
935 get_user_u64(val, infop->entry);
936 infop->entry = val + infop->load_bias;
d90b94cd
DK
937 } else {
938 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
939 }
84409ddb 940#endif
67867308
FB
941 _regs->nip = infop->entry;
942}
943
e2f3e741
NF
944/* See linux kernel: arch/powerpc/include/asm/elf.h. */
945#define ELF_NREG 48
946typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
947
05390248 948static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
e2f3e741
NF
949{
950 int i;
951 target_ulong ccr = 0;
952
953 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
86cd7b2d 954 (*regs)[i] = tswapreg(env->gpr[i]);
e2f3e741
NF
955 }
956
86cd7b2d
PB
957 (*regs)[32] = tswapreg(env->nip);
958 (*regs)[33] = tswapreg(env->msr);
959 (*regs)[35] = tswapreg(env->ctr);
960 (*regs)[36] = tswapreg(env->lr);
10de0521 961 (*regs)[37] = tswapreg(cpu_read_xer(env));
e2f3e741
NF
962
963 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
964 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
965 }
86cd7b2d 966 (*regs)[38] = tswapreg(ccr);
e2f3e741
NF
967}
968
969#define USE_ELF_CORE_DUMP
d97ef72e 970#define ELF_EXEC_PAGESIZE 4096
67867308
FB
971
972#endif
973
3418fe25
SG
974#ifdef TARGET_LOONGARCH64
975
976#define ELF_START_MMAP 0x80000000
977
978#define ELF_CLASS ELFCLASS64
979#define ELF_ARCH EM_LOONGARCH
872f3d04 980#define EXSTACK_DEFAULT true
3418fe25
SG
981
982#define elf_check_arch(x) ((x) == EM_LOONGARCH)
983
984static inline void init_thread(struct target_pt_regs *regs,
985 struct image_info *infop)
986{
987 /*Set crmd PG,DA = 1,0 */
988 regs->csr.crmd = 2 << 3;
989 regs->csr.era = infop->entry;
990 regs->regs[3] = infop->start_stack;
991}
992
993/* See linux kernel: arch/loongarch/include/asm/elf.h */
994#define ELF_NREG 45
995typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
996
997enum {
998 TARGET_EF_R0 = 0,
999 TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33,
1000 TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34,
1001};
1002
1003static void elf_core_copy_regs(target_elf_gregset_t *regs,
1004 const CPULoongArchState *env)
1005{
1006 int i;
1007
1008 (*regs)[TARGET_EF_R0] = 0;
1009
1010 for (i = 1; i < ARRAY_SIZE(env->gpr); i++) {
1011 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]);
1012 }
1013
1014 (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc);
1015 (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV);
1016}
1017
1018#define USE_ELF_CORE_DUMP
1019#define ELF_EXEC_PAGESIZE 4096
1020
1021#define ELF_HWCAP get_elf_hwcap()
1022
1023/* See arch/loongarch/include/uapi/asm/hwcap.h */
1024enum {
1025 HWCAP_LOONGARCH_CPUCFG = (1 << 0),
1026 HWCAP_LOONGARCH_LAM = (1 << 1),
1027 HWCAP_LOONGARCH_UAL = (1 << 2),
1028 HWCAP_LOONGARCH_FPU = (1 << 3),
1029 HWCAP_LOONGARCH_LSX = (1 << 4),
1030 HWCAP_LOONGARCH_LASX = (1 << 5),
1031 HWCAP_LOONGARCH_CRC32 = (1 << 6),
1032 HWCAP_LOONGARCH_COMPLEX = (1 << 7),
1033 HWCAP_LOONGARCH_CRYPTO = (1 << 8),
1034 HWCAP_LOONGARCH_LVZ = (1 << 9),
1035 HWCAP_LOONGARCH_LBT_X86 = (1 << 10),
1036 HWCAP_LOONGARCH_LBT_ARM = (1 << 11),
1037 HWCAP_LOONGARCH_LBT_MIPS = (1 << 12),
1038};
1039
1040static uint32_t get_elf_hwcap(void)
1041{
1042 LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu);
1043 uint32_t hwcaps = 0;
1044
1045 hwcaps |= HWCAP_LOONGARCH_CRC32;
1046
1047 if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) {
1048 hwcaps |= HWCAP_LOONGARCH_UAL;
1049 }
1050
1051 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) {
1052 hwcaps |= HWCAP_LOONGARCH_FPU;
1053 }
1054
1055 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) {
1056 hwcaps |= HWCAP_LOONGARCH_LAM;
1057 }
1058
1059 return hwcaps;
1060}
1061
1062#define ELF_PLATFORM "loongarch"
1063
1064#endif /* TARGET_LOONGARCH64 */
1065
048f6b4d
FB
1066#ifdef TARGET_MIPS
1067
1068#define ELF_START_MMAP 0x80000000
1069
388bb21a
TS
1070#ifdef TARGET_MIPS64
1071#define ELF_CLASS ELFCLASS64
1072#else
048f6b4d 1073#define ELF_CLASS ELFCLASS32
388bb21a 1074#endif
048f6b4d 1075#define ELF_ARCH EM_MIPS
872f3d04 1076#define EXSTACK_DEFAULT true
048f6b4d 1077
ace3d654
CMAB
1078#ifdef TARGET_ABI_MIPSN32
1079#define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1080#else
1081#define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1082#endif
1083
fbf47c18
JY
1084#define ELF_BASE_PLATFORM get_elf_base_platform()
1085
1086#define MATCH_PLATFORM_INSN(_flags, _base_platform) \
1087 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1088 { return _base_platform; } } while (0)
1089
1090static const char *get_elf_base_platform(void)
1091{
1092 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1093
1094 /* 64 bit ISAs goes first */
1095 MATCH_PLATFORM_INSN(CPU_MIPS64R6, "mips64r6");
1096 MATCH_PLATFORM_INSN(CPU_MIPS64R5, "mips64r5");
1097 MATCH_PLATFORM_INSN(CPU_MIPS64R2, "mips64r2");
1098 MATCH_PLATFORM_INSN(CPU_MIPS64R1, "mips64");
1099 MATCH_PLATFORM_INSN(CPU_MIPS5, "mips5");
1100 MATCH_PLATFORM_INSN(CPU_MIPS4, "mips4");
1101 MATCH_PLATFORM_INSN(CPU_MIPS3, "mips3");
1102
1103 /* 32 bit ISAs */
1104 MATCH_PLATFORM_INSN(CPU_MIPS32R6, "mips32r6");
1105 MATCH_PLATFORM_INSN(CPU_MIPS32R5, "mips32r5");
1106 MATCH_PLATFORM_INSN(CPU_MIPS32R2, "mips32r2");
1107 MATCH_PLATFORM_INSN(CPU_MIPS32R1, "mips32");
1108 MATCH_PLATFORM_INSN(CPU_MIPS2, "mips2");
1109
1110 /* Fallback */
1111 return "mips";
1112}
1113#undef MATCH_PLATFORM_INSN
1114
d97ef72e
RH
1115static inline void init_thread(struct target_pt_regs *regs,
1116 struct image_info *infop)
048f6b4d 1117{
623a930e 1118 regs->cp0_status = 2 << CP0St_KSU;
048f6b4d
FB
1119 regs->cp0_epc = infop->entry;
1120 regs->regs[29] = infop->start_stack;
1121}
1122
51e52606
NF
1123/* See linux kernel: arch/mips/include/asm/elf.h. */
1124#define ELF_NREG 45
1125typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1126
1127/* See linux kernel: arch/mips/include/asm/reg.h. */
1128enum {
1129#ifdef TARGET_MIPS64
1130 TARGET_EF_R0 = 0,
1131#else
1132 TARGET_EF_R0 = 6,
1133#endif
1134 TARGET_EF_R26 = TARGET_EF_R0 + 26,
1135 TARGET_EF_R27 = TARGET_EF_R0 + 27,
1136 TARGET_EF_LO = TARGET_EF_R0 + 32,
1137 TARGET_EF_HI = TARGET_EF_R0 + 33,
1138 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
1139 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
1140 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
1141 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
1142};
1143
1144/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
05390248 1145static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
51e52606
NF
1146{
1147 int i;
1148
1149 for (i = 0; i < TARGET_EF_R0; i++) {
1150 (*regs)[i] = 0;
1151 }
1152 (*regs)[TARGET_EF_R0] = 0;
1153
1154 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
a29f998d 1155 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
51e52606
NF
1156 }
1157
1158 (*regs)[TARGET_EF_R26] = 0;
1159 (*regs)[TARGET_EF_R27] = 0;
a29f998d
PB
1160 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
1161 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
1162 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
1163 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
1164 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
1165 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
51e52606
NF
1166}
1167
1168#define USE_ELF_CORE_DUMP
388bb21a
TS
1169#define ELF_EXEC_PAGESIZE 4096
1170
46a1ee4f
JC
1171/* See arch/mips/include/uapi/asm/hwcap.h. */
1172enum {
1173 HWCAP_MIPS_R6 = (1 << 0),
1174 HWCAP_MIPS_MSA = (1 << 1),
9ea313ba
PMD
1175 HWCAP_MIPS_CRC32 = (1 << 2),
1176 HWCAP_MIPS_MIPS16 = (1 << 3),
1177 HWCAP_MIPS_MDMX = (1 << 4),
1178 HWCAP_MIPS_MIPS3D = (1 << 5),
1179 HWCAP_MIPS_SMARTMIPS = (1 << 6),
1180 HWCAP_MIPS_DSP = (1 << 7),
1181 HWCAP_MIPS_DSP2 = (1 << 8),
1182 HWCAP_MIPS_DSP3 = (1 << 9),
1183 HWCAP_MIPS_MIPS16E2 = (1 << 10),
1184 HWCAP_LOONGSON_MMI = (1 << 11),
1185 HWCAP_LOONGSON_EXT = (1 << 12),
1186 HWCAP_LOONGSON_EXT2 = (1 << 13),
1187 HWCAP_LOONGSON_CPUCFG = (1 << 14),
46a1ee4f
JC
1188};
1189
1190#define ELF_HWCAP get_elf_hwcap()
1191
7d9a3d96 1192#define GET_FEATURE_INSN(_flag, _hwcap) \
6dd97bfc
PMD
1193 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1194
388765a0
PMD
1195#define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1196 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1197
ce543844
PMD
1198#define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1199 do { \
1200 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1201 hwcaps |= _hwcap; \
1202 } \
1203 } while (0)
1204
46a1ee4f
JC
1205static uint32_t get_elf_hwcap(void)
1206{
1207 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1208 uint32_t hwcaps = 0;
1209
ce543844
PMD
1210 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH,
1211 2, HWCAP_MIPS_R6);
388765a0 1212 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA);
53673d0f
PMD
1213 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI);
1214 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT);
46a1ee4f 1215
46a1ee4f
JC
1216 return hwcaps;
1217}
1218
ce543844 1219#undef GET_FEATURE_REG_EQU
388765a0 1220#undef GET_FEATURE_REG_SET
7d9a3d96 1221#undef GET_FEATURE_INSN
6dd97bfc 1222
048f6b4d
FB
1223#endif /* TARGET_MIPS */
1224
b779e29e
EI
1225#ifdef TARGET_MICROBLAZE
1226
1227#define ELF_START_MMAP 0x80000000
1228
0d5d4699 1229#define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
b779e29e
EI
1230
1231#define ELF_CLASS ELFCLASS32
0d5d4699 1232#define ELF_ARCH EM_MICROBLAZE
b779e29e 1233
d97ef72e
RH
1234static inline void init_thread(struct target_pt_regs *regs,
1235 struct image_info *infop)
b779e29e
EI
1236{
1237 regs->pc = infop->entry;
1238 regs->r1 = infop->start_stack;
1239
1240}
1241
b779e29e
EI
1242#define ELF_EXEC_PAGESIZE 4096
1243
e4cbd44d
EI
1244#define USE_ELF_CORE_DUMP
1245#define ELF_NREG 38
1246typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1247
1248/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
05390248 1249static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
e4cbd44d
EI
1250{
1251 int i, pos = 0;
1252
1253 for (i = 0; i < 32; i++) {
86cd7b2d 1254 (*regs)[pos++] = tswapreg(env->regs[i]);
e4cbd44d
EI
1255 }
1256
af20a93a 1257 (*regs)[pos++] = tswapreg(env->pc);
1074c0fb 1258 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
af20a93a
RH
1259 (*regs)[pos++] = 0;
1260 (*regs)[pos++] = tswapreg(env->ear);
1261 (*regs)[pos++] = 0;
1262 (*regs)[pos++] = tswapreg(env->esr);
e4cbd44d
EI
1263}
1264
b779e29e
EI
1265#endif /* TARGET_MICROBLAZE */
1266
a0a839b6
MV
1267#ifdef TARGET_NIOS2
1268
1269#define ELF_START_MMAP 0x80000000
1270
1271#define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1272
1273#define ELF_CLASS ELFCLASS32
1274#define ELF_ARCH EM_ALTERA_NIOS2
1275
1276static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1277{
1278 regs->ea = infop->entry;
1279 regs->sp = infop->start_stack;
a0a839b6
MV
1280}
1281
f5ef0e51
RH
1282#define LO_COMMPAGE TARGET_PAGE_SIZE
1283
1284static bool init_guest_commpage(void)
1285{
1286 static const uint8_t kuser_page[4 + 2 * 64] = {
1287 /* __kuser_helper_version */
1288 [0x00] = 0x02, 0x00, 0x00, 0x00,
1289
1290 /* __kuser_cmpxchg */
1291 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1292 0x3a, 0x28, 0x00, 0xf8, /* ret */
1293
1294 /* __kuser_sigtramp */
1295 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1296 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1297 };
1298
1299 void *want = g2h_untagged(LO_COMMPAGE & -qemu_host_page_size);
1300 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
1301 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1302
1303 if (addr == MAP_FAILED) {
1304 perror("Allocating guest commpage");
1305 exit(EXIT_FAILURE);
1306 }
1307 if (addr != want) {
1308 return false;
1309 }
1310
1311 memcpy(addr, kuser_page, sizeof(kuser_page));
1312
1313 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
1314 perror("Protecting guest commpage");
1315 exit(EXIT_FAILURE);
1316 }
1317
1318 page_set_flags(LO_COMMPAGE, LO_COMMPAGE + TARGET_PAGE_SIZE,
1319 PAGE_READ | PAGE_EXEC | PAGE_VALID);
1320 return true;
1321}
1322
a0a839b6
MV
1323#define ELF_EXEC_PAGESIZE 4096
1324
1325#define USE_ELF_CORE_DUMP
1326#define ELF_NREG 49
1327typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1328
1329/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1330static void elf_core_copy_regs(target_elf_gregset_t *regs,
1331 const CPUNios2State *env)
1332{
1333 int i;
1334
1335 (*regs)[0] = -1;
1336 for (i = 1; i < 8; i++) /* r0-r7 */
1337 (*regs)[i] = tswapreg(env->regs[i + 7]);
1338
1339 for (i = 8; i < 16; i++) /* r8-r15 */
1340 (*regs)[i] = tswapreg(env->regs[i - 8]);
1341
1342 for (i = 16; i < 24; i++) /* r16-r23 */
1343 (*regs)[i] = tswapreg(env->regs[i + 7]);
1344 (*regs)[24] = -1; /* R_ET */
1345 (*regs)[25] = -1; /* R_BT */
1346 (*regs)[26] = tswapreg(env->regs[R_GP]);
1347 (*regs)[27] = tswapreg(env->regs[R_SP]);
1348 (*regs)[28] = tswapreg(env->regs[R_FP]);
1349 (*regs)[29] = tswapreg(env->regs[R_EA]);
1350 (*regs)[30] = -1; /* R_SSTATUS */
1351 (*regs)[31] = tswapreg(env->regs[R_RA]);
1352
17a406ee 1353 (*regs)[32] = tswapreg(env->pc);
a0a839b6
MV
1354
1355 (*regs)[33] = -1; /* R_STATUS */
1356 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1357
1358 for (i = 35; i < 49; i++) /* ... */
1359 (*regs)[i] = -1;
1360}
1361
1362#endif /* TARGET_NIOS2 */
1363
d962783e
JL
1364#ifdef TARGET_OPENRISC
1365
1366#define ELF_START_MMAP 0x08000000
1367
d962783e
JL
1368#define ELF_ARCH EM_OPENRISC
1369#define ELF_CLASS ELFCLASS32
1370#define ELF_DATA ELFDATA2MSB
1371
1372static inline void init_thread(struct target_pt_regs *regs,
1373 struct image_info *infop)
1374{
1375 regs->pc = infop->entry;
1376 regs->gpr[1] = infop->start_stack;
1377}
1378
1379#define USE_ELF_CORE_DUMP
1380#define ELF_EXEC_PAGESIZE 8192
1381
1382/* See linux kernel arch/openrisc/include/asm/elf.h. */
1383#define ELF_NREG 34 /* gprs and pc, sr */
1384typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1385
1386static void elf_core_copy_regs(target_elf_gregset_t *regs,
1387 const CPUOpenRISCState *env)
1388{
1389 int i;
1390
1391 for (i = 0; i < 32; i++) {
d89e71e8 1392 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
d962783e 1393 }
86cd7b2d 1394 (*regs)[32] = tswapreg(env->pc);
84775c43 1395 (*regs)[33] = tswapreg(cpu_get_sr(env));
d962783e
JL
1396}
1397#define ELF_HWCAP 0
1398#define ELF_PLATFORM NULL
1399
1400#endif /* TARGET_OPENRISC */
1401
fdf9b3e8
FB
1402#ifdef TARGET_SH4
1403
1404#define ELF_START_MMAP 0x80000000
1405
fdf9b3e8 1406#define ELF_CLASS ELFCLASS32
fdf9b3e8
FB
1407#define ELF_ARCH EM_SH
1408
d97ef72e
RH
1409static inline void init_thread(struct target_pt_regs *regs,
1410 struct image_info *infop)
fdf9b3e8 1411{
d97ef72e
RH
1412 /* Check other registers XXXXX */
1413 regs->pc = infop->entry;
1414 regs->regs[15] = infop->start_stack;
fdf9b3e8
FB
1415}
1416
7631c97e
NF
1417/* See linux kernel: arch/sh/include/asm/elf.h. */
1418#define ELF_NREG 23
1419typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1420
1421/* See linux kernel: arch/sh/include/asm/ptrace.h. */
1422enum {
1423 TARGET_REG_PC = 16,
1424 TARGET_REG_PR = 17,
1425 TARGET_REG_SR = 18,
1426 TARGET_REG_GBR = 19,
1427 TARGET_REG_MACH = 20,
1428 TARGET_REG_MACL = 21,
1429 TARGET_REG_SYSCALL = 22
1430};
1431
d97ef72e 1432static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
05390248 1433 const CPUSH4State *env)
7631c97e
NF
1434{
1435 int i;
1436
1437 for (i = 0; i < 16; i++) {
72cd500b 1438 (*regs)[i] = tswapreg(env->gregs[i]);
7631c97e
NF
1439 }
1440
86cd7b2d
PB
1441 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1442 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1443 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1444 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1445 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1446 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
7631c97e
NF
1447 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1448}
1449
1450#define USE_ELF_CORE_DUMP
fdf9b3e8
FB
1451#define ELF_EXEC_PAGESIZE 4096
1452
e42fd944
RH
1453enum {
1454 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1455 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1456 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1457 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1458 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1459 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1460 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1461 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1462 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1463 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1464};
1465
1466#define ELF_HWCAP get_elf_hwcap()
1467
1468static uint32_t get_elf_hwcap(void)
1469{
1470 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1471 uint32_t hwcap = 0;
1472
1473 hwcap |= SH_CPU_HAS_FPU;
1474
1475 if (cpu->env.features & SH_FEATURE_SH4A) {
1476 hwcap |= SH_CPU_HAS_LLSC;
1477 }
1478
1479 return hwcap;
1480}
1481
fdf9b3e8
FB
1482#endif
1483
48733d19
TS
1484#ifdef TARGET_CRIS
1485
1486#define ELF_START_MMAP 0x80000000
1487
48733d19 1488#define ELF_CLASS ELFCLASS32
48733d19
TS
1489#define ELF_ARCH EM_CRIS
1490
d97ef72e
RH
1491static inline void init_thread(struct target_pt_regs *regs,
1492 struct image_info *infop)
48733d19 1493{
d97ef72e 1494 regs->erp = infop->entry;
48733d19
TS
1495}
1496
48733d19
TS
1497#define ELF_EXEC_PAGESIZE 8192
1498
1499#endif
1500
e6e5906b
PB
1501#ifdef TARGET_M68K
1502
1503#define ELF_START_MMAP 0x80000000
1504
d97ef72e 1505#define ELF_CLASS ELFCLASS32
d97ef72e 1506#define ELF_ARCH EM_68K
e6e5906b
PB
1507
1508/* ??? Does this need to do anything?
d97ef72e 1509 #define ELF_PLAT_INIT(_r) */
e6e5906b 1510
d97ef72e
RH
1511static inline void init_thread(struct target_pt_regs *regs,
1512 struct image_info *infop)
e6e5906b
PB
1513{
1514 regs->usp = infop->start_stack;
1515 regs->sr = 0;
1516 regs->pc = infop->entry;
1517}
1518
7a93cc55
NF
1519/* See linux kernel: arch/m68k/include/asm/elf.h. */
1520#define ELF_NREG 20
1521typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1522
05390248 1523static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
7a93cc55 1524{
86cd7b2d
PB
1525 (*regs)[0] = tswapreg(env->dregs[1]);
1526 (*regs)[1] = tswapreg(env->dregs[2]);
1527 (*regs)[2] = tswapreg(env->dregs[3]);
1528 (*regs)[3] = tswapreg(env->dregs[4]);
1529 (*regs)[4] = tswapreg(env->dregs[5]);
1530 (*regs)[5] = tswapreg(env->dregs[6]);
1531 (*regs)[6] = tswapreg(env->dregs[7]);
1532 (*regs)[7] = tswapreg(env->aregs[0]);
1533 (*regs)[8] = tswapreg(env->aregs[1]);
1534 (*regs)[9] = tswapreg(env->aregs[2]);
1535 (*regs)[10] = tswapreg(env->aregs[3]);
1536 (*regs)[11] = tswapreg(env->aregs[4]);
1537 (*regs)[12] = tswapreg(env->aregs[5]);
1538 (*regs)[13] = tswapreg(env->aregs[6]);
1539 (*regs)[14] = tswapreg(env->dregs[0]);
1540 (*regs)[15] = tswapreg(env->aregs[7]);
1541 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1542 (*regs)[17] = tswapreg(env->sr);
1543 (*regs)[18] = tswapreg(env->pc);
7a93cc55
NF
1544 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1545}
1546
1547#define USE_ELF_CORE_DUMP
d97ef72e 1548#define ELF_EXEC_PAGESIZE 8192
e6e5906b
PB
1549
1550#endif
1551
7a3148a9
JM
1552#ifdef TARGET_ALPHA
1553
1554#define ELF_START_MMAP (0x30000000000ULL)
1555
7a3148a9 1556#define ELF_CLASS ELFCLASS64
7a3148a9
JM
1557#define ELF_ARCH EM_ALPHA
1558
d97ef72e
RH
1559static inline void init_thread(struct target_pt_regs *regs,
1560 struct image_info *infop)
7a3148a9
JM
1561{
1562 regs->pc = infop->entry;
1563 regs->ps = 8;
1564 regs->usp = infop->start_stack;
7a3148a9
JM
1565}
1566
7a3148a9
JM
1567#define ELF_EXEC_PAGESIZE 8192
1568
1569#endif /* TARGET_ALPHA */
1570
a4c075f1
UH
1571#ifdef TARGET_S390X
1572
1573#define ELF_START_MMAP (0x20000000000ULL)
1574
a4c075f1
UH
1575#define ELF_CLASS ELFCLASS64
1576#define ELF_DATA ELFDATA2MSB
1577#define ELF_ARCH EM_S390
1578
6d88baf1
DH
1579#include "elf.h"
1580
1581#define ELF_HWCAP get_elf_hwcap()
1582
1583#define GET_FEATURE(_feat, _hwcap) \
1584 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1585
1586static uint32_t get_elf_hwcap(void)
1587{
1588 /*
1589 * Let's assume we always have esan3 and zarch.
1590 * 31-bit processes can use 64-bit registers (high gprs).
1591 */
1592 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1593
1594 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1595 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1596 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1597 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1598 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1599 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1600 hwcap |= HWCAP_S390_ETF3EH;
1601 }
1602 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
da215c23 1603 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT);
6d88baf1
DH
1604
1605 return hwcap;
1606}
1607
a4c075f1
UH
1608static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1609{
1610 regs->psw.addr = infop->entry;
1611 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1612 regs->gprs[15] = infop->start_stack;
1613}
1614
4a1e8931
IL
1615/* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1616#define ELF_NREG 27
1617typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1618
1619enum {
1620 TARGET_REG_PSWM = 0,
1621 TARGET_REG_PSWA = 1,
1622 TARGET_REG_GPRS = 2,
1623 TARGET_REG_ARS = 18,
1624 TARGET_REG_ORIG_R2 = 26,
1625};
1626
1627static void elf_core_copy_regs(target_elf_gregset_t *regs,
1628 const CPUS390XState *env)
1629{
1630 int i;
1631 uint32_t *aregs;
1632
1633 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask);
1634 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr);
1635 for (i = 0; i < 16; i++) {
1636 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]);
1637 }
1638 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]);
1639 for (i = 0; i < 16; i++) {
1640 aregs[i] = tswap32(env->aregs[i]);
1641 }
1642 (*regs)[TARGET_REG_ORIG_R2] = 0;
1643}
1644
1645#define USE_ELF_CORE_DUMP
1646#define ELF_EXEC_PAGESIZE 4096
1647
a4c075f1
UH
1648#endif /* TARGET_S390X */
1649
47ae93cd
MC
1650#ifdef TARGET_RISCV
1651
1652#define ELF_START_MMAP 0x80000000
1653#define ELF_ARCH EM_RISCV
1654
1655#ifdef TARGET_RISCV32
1656#define ELF_CLASS ELFCLASS32
1657#else
1658#define ELF_CLASS ELFCLASS64
1659#endif
1660
cb46938c
KC
1661#define ELF_HWCAP get_elf_hwcap()
1662
1663static uint32_t get_elf_hwcap(void)
1664{
1665#define MISA_BIT(EXT) (1 << (EXT - 'A'))
1666 RISCVCPU *cpu = RISCV_CPU(thread_cpu);
1667 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1668 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C');
1669
e91a7227 1670 return cpu->env.misa_ext & mask;
cb46938c
KC
1671#undef MISA_BIT
1672}
1673
47ae93cd
MC
1674static inline void init_thread(struct target_pt_regs *regs,
1675 struct image_info *infop)
1676{
1677 regs->sepc = infop->entry;
1678 regs->sp = infop->start_stack;
1679}
1680
1681#define ELF_EXEC_PAGESIZE 4096
1682
1683#endif /* TARGET_RISCV */
1684
7c248bcd
RH
1685#ifdef TARGET_HPPA
1686
1687#define ELF_START_MMAP 0x80000000
1688#define ELF_CLASS ELFCLASS32
1689#define ELF_ARCH EM_PARISC
1690#define ELF_PLATFORM "PARISC"
1691#define STACK_GROWS_DOWN 0
1692#define STACK_ALIGNMENT 64
1693
1694static inline void init_thread(struct target_pt_regs *regs,
1695 struct image_info *infop)
1696{
1697 regs->iaoq[0] = infop->entry;
1698 regs->iaoq[1] = infop->entry + 4;
1699 regs->gr[23] = 0;
60f1c801
RH
1700 regs->gr[24] = infop->argv;
1701 regs->gr[25] = infop->argc;
7c248bcd
RH
1702 /* The top-of-stack contains a linkage buffer. */
1703 regs->gr[30] = infop->start_stack + 64;
1704 regs->gr[31] = infop->entry;
1705}
1706
eee816c0
RH
1707#define LO_COMMPAGE 0
1708
1709static bool init_guest_commpage(void)
1710{
1711 void *want = g2h_untagged(LO_COMMPAGE);
1712 void *addr = mmap(want, qemu_host_page_size, PROT_NONE,
1713 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1714
1715 if (addr == MAP_FAILED) {
1716 perror("Allocating guest commpage");
1717 exit(EXIT_FAILURE);
1718 }
1719 if (addr != want) {
1720 return false;
1721 }
1722
1723 /*
1724 * On Linux, page zero is normally marked execute only + gateway.
1725 * Normal read or write is supposed to fail (thus PROT_NONE above),
1726 * but specific offsets have kernel code mapped to raise permissions
1727 * and implement syscalls. Here, simply mark the page executable.
1728 * Special case the entry points during translation (see do_page_zero).
1729 */
1730 page_set_flags(LO_COMMPAGE, LO_COMMPAGE + TARGET_PAGE_SIZE,
1731 PAGE_EXEC | PAGE_VALID);
1732 return true;
1733}
1734
7c248bcd
RH
1735#endif /* TARGET_HPPA */
1736
ba7651fb
MF
1737#ifdef TARGET_XTENSA
1738
1739#define ELF_START_MMAP 0x20000000
1740
1741#define ELF_CLASS ELFCLASS32
1742#define ELF_ARCH EM_XTENSA
1743
1744static inline void init_thread(struct target_pt_regs *regs,
1745 struct image_info *infop)
1746{
1747 regs->windowbase = 0;
1748 regs->windowstart = 1;
1749 regs->areg[1] = infop->start_stack;
1750 regs->pc = infop->entry;
1751}
1752
1753/* See linux kernel: arch/xtensa/include/asm/elf.h. */
1754#define ELF_NREG 128
1755typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1756
1757enum {
1758 TARGET_REG_PC,
1759 TARGET_REG_PS,
1760 TARGET_REG_LBEG,
1761 TARGET_REG_LEND,
1762 TARGET_REG_LCOUNT,
1763 TARGET_REG_SAR,
1764 TARGET_REG_WINDOWSTART,
1765 TARGET_REG_WINDOWBASE,
1766 TARGET_REG_THREADPTR,
1767 TARGET_REG_AR0 = 64,
1768};
1769
1770static void elf_core_copy_regs(target_elf_gregset_t *regs,
1771 const CPUXtensaState *env)
1772{
1773 unsigned i;
1774
1775 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1776 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1777 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1778 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1779 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1780 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1781 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1782 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1783 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1784 xtensa_sync_phys_from_window((CPUXtensaState *)env);
1785 for (i = 0; i < env->config->nareg; ++i) {
1786 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1787 }
1788}
1789
1790#define USE_ELF_CORE_DUMP
1791#define ELF_EXEC_PAGESIZE 4096
1792
1793#endif /* TARGET_XTENSA */
1794
d2a56bd2
TS
1795#ifdef TARGET_HEXAGON
1796
1797#define ELF_START_MMAP 0x20000000
1798
1799#define ELF_CLASS ELFCLASS32
1800#define ELF_ARCH EM_HEXAGON
1801
1802static inline void init_thread(struct target_pt_regs *regs,
1803 struct image_info *infop)
1804{
1805 regs->sepc = infop->entry;
1806 regs->sp = infop->start_stack;
1807}
1808
1809#endif /* TARGET_HEXAGON */
1810
fcdc0ab4
JY
1811#ifndef ELF_BASE_PLATFORM
1812#define ELF_BASE_PLATFORM (NULL)
1813#endif
1814
15338fd7
FB
1815#ifndef ELF_PLATFORM
1816#define ELF_PLATFORM (NULL)
1817#endif
1818
75be901c
PC
1819#ifndef ELF_MACHINE
1820#define ELF_MACHINE ELF_ARCH
1821#endif
1822
d276a604
PC
1823#ifndef elf_check_arch
1824#define elf_check_arch(x) ((x) == ELF_ARCH)
1825#endif
1826
ace3d654
CMAB
1827#ifndef elf_check_abi
1828#define elf_check_abi(x) (1)
1829#endif
1830
15338fd7
FB
1831#ifndef ELF_HWCAP
1832#define ELF_HWCAP 0
1833#endif
1834
7c4ee5bc
RH
1835#ifndef STACK_GROWS_DOWN
1836#define STACK_GROWS_DOWN 1
1837#endif
1838
1839#ifndef STACK_ALIGNMENT
1840#define STACK_ALIGNMENT 16
1841#endif
1842
992f48a0 1843#ifdef TARGET_ABI32
cb33da57 1844#undef ELF_CLASS
992f48a0 1845#define ELF_CLASS ELFCLASS32
cb33da57
BS
1846#undef bswaptls
1847#define bswaptls(ptr) bswap32s(ptr)
1848#endif
1849
872f3d04
RH
1850#ifndef EXSTACK_DEFAULT
1851#define EXSTACK_DEFAULT false
1852#endif
1853
31e31b8a 1854#include "elf.h"
09bfb054 1855
e8384b37
RH
1856/* We must delay the following stanzas until after "elf.h". */
1857#if defined(TARGET_AARCH64)
1858
1859static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1860 const uint32_t *data,
1861 struct image_info *info,
1862 Error **errp)
1863{
1864 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
1865 if (pr_datasz != sizeof(uint32_t)) {
1866 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
1867 return false;
1868 }
1869 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
1870 info->note_flags = *data;
1871 }
1872 return true;
1873}
1874#define ARCH_USE_GNU_PROPERTY 1
1875
1876#else
1877
83f990eb
RH
1878static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1879 const uint32_t *data,
1880 struct image_info *info,
1881 Error **errp)
1882{
1883 g_assert_not_reached();
1884}
1885#define ARCH_USE_GNU_PROPERTY 0
1886
e8384b37
RH
1887#endif
1888
09bfb054
FB
1889struct exec
1890{
d97ef72e
RH
1891 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1892 unsigned int a_text; /* length of text, in bytes */
1893 unsigned int a_data; /* length of data, in bytes */
1894 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1895 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1896 unsigned int a_entry; /* start address */
1897 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1898 unsigned int a_drsize; /* length of relocation info for data, in bytes */
09bfb054
FB
1899};
1900
1901
1902#define N_MAGIC(exec) ((exec).a_info & 0xffff)
1903#define OMAGIC 0407
1904#define NMAGIC 0410
1905#define ZMAGIC 0413
1906#define QMAGIC 0314
1907
31e31b8a 1908/* Necessary parameters */
94894ff2
SB
1909#define TARGET_ELF_EXEC_PAGESIZE \
1910 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1911 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1912#define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
79cb1f1d
YK
1913#define TARGET_ELF_PAGESTART(_v) ((_v) & \
1914 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
54936004 1915#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
31e31b8a 1916
e0d1673d 1917#define DLINFO_ITEMS 16
31e31b8a 1918
09bfb054
FB
1919static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1920{
d97ef72e 1921 memcpy(to, from, n);
09bfb054 1922}
d691f669 1923
31e31b8a 1924#ifdef BSWAP_NEEDED
92a31b1f 1925static void bswap_ehdr(struct elfhdr *ehdr)
31e31b8a 1926{
d97ef72e
RH
1927 bswap16s(&ehdr->e_type); /* Object file type */
1928 bswap16s(&ehdr->e_machine); /* Architecture */
1929 bswap32s(&ehdr->e_version); /* Object file version */
1930 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1931 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1932 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1933 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1934 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1935 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1936 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1937 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1938 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1939 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
31e31b8a
FB
1940}
1941
991f8f0c 1942static void bswap_phdr(struct elf_phdr *phdr, int phnum)
31e31b8a 1943{
991f8f0c
RH
1944 int i;
1945 for (i = 0; i < phnum; ++i, ++phdr) {
1946 bswap32s(&phdr->p_type); /* Segment type */
1947 bswap32s(&phdr->p_flags); /* Segment flags */
1948 bswaptls(&phdr->p_offset); /* Segment file offset */
1949 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1950 bswaptls(&phdr->p_paddr); /* Segment physical address */
1951 bswaptls(&phdr->p_filesz); /* Segment size in file */
1952 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1953 bswaptls(&phdr->p_align); /* Segment alignment */
1954 }
31e31b8a 1955}
689f936f 1956
991f8f0c 1957static void bswap_shdr(struct elf_shdr *shdr, int shnum)
689f936f 1958{
991f8f0c
RH
1959 int i;
1960 for (i = 0; i < shnum; ++i, ++shdr) {
1961 bswap32s(&shdr->sh_name);
1962 bswap32s(&shdr->sh_type);
1963 bswaptls(&shdr->sh_flags);
1964 bswaptls(&shdr->sh_addr);
1965 bswaptls(&shdr->sh_offset);
1966 bswaptls(&shdr->sh_size);
1967 bswap32s(&shdr->sh_link);
1968 bswap32s(&shdr->sh_info);
1969 bswaptls(&shdr->sh_addralign);
1970 bswaptls(&shdr->sh_entsize);
1971 }
689f936f
FB
1972}
1973
7a3148a9 1974static void bswap_sym(struct elf_sym *sym)
689f936f
FB
1975{
1976 bswap32s(&sym->st_name);
7a3148a9
JM
1977 bswaptls(&sym->st_value);
1978 bswaptls(&sym->st_size);
689f936f
FB
1979 bswap16s(&sym->st_shndx);
1980}
5dd0db52
SM
1981
1982#ifdef TARGET_MIPS
1983static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
1984{
1985 bswap16s(&abiflags->version);
1986 bswap32s(&abiflags->ases);
1987 bswap32s(&abiflags->isa_ext);
1988 bswap32s(&abiflags->flags1);
1989 bswap32s(&abiflags->flags2);
1990}
1991#endif
991f8f0c
RH
1992#else
1993static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1994static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1995static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1996static inline void bswap_sym(struct elf_sym *sym) { }
5dd0db52
SM
1997#ifdef TARGET_MIPS
1998static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
1999#endif
31e31b8a
FB
2000#endif
2001
edf8e2af 2002#ifdef USE_ELF_CORE_DUMP
9349b4f9 2003static int elf_core_dump(int, const CPUArchState *);
edf8e2af 2004#endif /* USE_ELF_CORE_DUMP */
682674b8 2005static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
edf8e2af 2006
9058abdd
RH
2007/* Verify the portions of EHDR within E_IDENT for the target.
2008 This can be performed before bswapping the entire header. */
2009static bool elf_check_ident(struct elfhdr *ehdr)
2010{
2011 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
2012 && ehdr->e_ident[EI_MAG1] == ELFMAG1
2013 && ehdr->e_ident[EI_MAG2] == ELFMAG2
2014 && ehdr->e_ident[EI_MAG3] == ELFMAG3
2015 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
2016 && ehdr->e_ident[EI_DATA] == ELF_DATA
2017 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
2018}
2019
2020/* Verify the portions of EHDR outside of E_IDENT for the target.
2021 This has to wait until after bswapping the header. */
2022static bool elf_check_ehdr(struct elfhdr *ehdr)
2023{
2024 return (elf_check_arch(ehdr->e_machine)
ace3d654 2025 && elf_check_abi(ehdr->e_flags)
9058abdd
RH
2026 && ehdr->e_ehsize == sizeof(struct elfhdr)
2027 && ehdr->e_phentsize == sizeof(struct elf_phdr)
9058abdd
RH
2028 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
2029}
2030
31e31b8a 2031/*
e5fe0c52 2032 * 'copy_elf_strings()' copies argument/envelope strings from user
31e31b8a
FB
2033 * memory to free pages in kernel mem. These are in a format ready
2034 * to be put directly into the top of new user memory.
2035 *
2036 */
59baae9a
SB
2037static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
2038 abi_ulong p, abi_ulong stack_limit)
31e31b8a 2039{
59baae9a 2040 char *tmp;
7c4ee5bc 2041 int len, i;
59baae9a 2042 abi_ulong top = p;
31e31b8a
FB
2043
2044 if (!p) {
d97ef72e 2045 return 0; /* bullet-proofing */
31e31b8a 2046 }
59baae9a 2047
7c4ee5bc
RH
2048 if (STACK_GROWS_DOWN) {
2049 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
2050 for (i = argc - 1; i >= 0; --i) {
2051 tmp = argv[i];
2052 if (!tmp) {
2053 fprintf(stderr, "VFS: argc is wrong");
2054 exit(-1);
2055 }
2056 len = strlen(tmp) + 1;
2057 tmp += len;
59baae9a 2058
7c4ee5bc
RH
2059 if (len > (p - stack_limit)) {
2060 return 0;
2061 }
2062 while (len) {
2063 int bytes_to_copy = (len > offset) ? offset : len;
2064 tmp -= bytes_to_copy;
2065 p -= bytes_to_copy;
2066 offset -= bytes_to_copy;
2067 len -= bytes_to_copy;
2068
2069 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
2070
2071 if (offset == 0) {
2072 memcpy_to_target(p, scratch, top - p);
2073 top = p;
2074 offset = TARGET_PAGE_SIZE;
2075 }
2076 }
d97ef72e 2077 }
7c4ee5bc
RH
2078 if (p != top) {
2079 memcpy_to_target(p, scratch + offset, top - p);
d97ef72e 2080 }
7c4ee5bc
RH
2081 } else {
2082 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
2083 for (i = 0; i < argc; ++i) {
2084 tmp = argv[i];
2085 if (!tmp) {
2086 fprintf(stderr, "VFS: argc is wrong");
2087 exit(-1);
2088 }
2089 len = strlen(tmp) + 1;
2090 if (len > (stack_limit - p)) {
2091 return 0;
2092 }
2093 while (len) {
2094 int bytes_to_copy = (len > remaining) ? remaining : len;
2095
2096 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
2097
2098 tmp += bytes_to_copy;
2099 remaining -= bytes_to_copy;
2100 p += bytes_to_copy;
2101 len -= bytes_to_copy;
2102
2103 if (remaining == 0) {
2104 memcpy_to_target(top, scratch, p - top);
2105 top = p;
2106 remaining = TARGET_PAGE_SIZE;
2107 }
d97ef72e
RH
2108 }
2109 }
7c4ee5bc
RH
2110 if (p != top) {
2111 memcpy_to_target(top, scratch, p - top);
2112 }
59baae9a
SB
2113 }
2114
31e31b8a
FB
2115 return p;
2116}
2117
59baae9a
SB
2118/* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2119 * argument/environment space. Newer kernels (>2.6.33) allow more,
2120 * dependent on stack size, but guarantee at least 32 pages for
2121 * backwards compatibility.
2122 */
2123#define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2124
2125static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
992f48a0 2126 struct image_info *info)
53a5960a 2127{
59baae9a 2128 abi_ulong size, error, guard;
872f3d04 2129 int prot;
31e31b8a 2130
703e0e89 2131 size = guest_stack_size;
59baae9a
SB
2132 if (size < STACK_LOWER_LIMIT) {
2133 size = STACK_LOWER_LIMIT;
60dcbcb5 2134 }
f4388205
HD
2135
2136 if (STACK_GROWS_DOWN) {
2137 guard = TARGET_PAGE_SIZE;
2138 if (guard < qemu_real_host_page_size()) {
2139 guard = qemu_real_host_page_size();
2140 }
2141 } else {
2142 /* no guard page for hppa target where stack grows upwards. */
2143 guard = 0;
60dcbcb5
RH
2144 }
2145
872f3d04
RH
2146 prot = PROT_READ | PROT_WRITE;
2147 if (info->exec_stack) {
2148 prot |= PROT_EXEC;
2149 }
2150 error = target_mmap(0, size + guard, prot,
60dcbcb5 2151 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
09bfb054 2152 if (error == -1) {
60dcbcb5 2153 perror("mmap stack");
09bfb054
FB
2154 exit(-1);
2155 }
31e31b8a 2156
60dcbcb5 2157 /* We reserve one extra page at the top of the stack as guard. */
7c4ee5bc
RH
2158 if (STACK_GROWS_DOWN) {
2159 target_mprotect(error, guard, PROT_NONE);
2160 info->stack_limit = error + guard;
2161 return info->stack_limit + size - sizeof(void *);
2162 } else {
7c4ee5bc
RH
2163 info->stack_limit = error + size;
2164 return error;
2165 }
31e31b8a
FB
2166}
2167
cf129f3a
RH
2168/* Map and zero the bss. We need to explicitly zero any fractional pages
2169 after the data section (i.e. bss). */
2170static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
31e31b8a 2171{
cf129f3a
RH
2172 uintptr_t host_start, host_map_start, host_end;
2173
2174 last_bss = TARGET_PAGE_ALIGN(last_bss);
2175
2176 /* ??? There is confusion between qemu_real_host_page_size and
2177 qemu_host_page_size here and elsewhere in target_mmap, which
2178 may lead to the end of the data section mapping from the file
2179 not being mapped. At least there was an explicit test and
2180 comment for that here, suggesting that "the file size must
2181 be known". The comment probably pre-dates the introduction
2182 of the fstat system call in target_mmap which does in fact
2183 find out the size. What isn't clear is if the workaround
2184 here is still actually needed. For now, continue with it,
2185 but merge it with the "normal" mmap that would allocate the bss. */
2186
3e8f1628
RH
2187 host_start = (uintptr_t) g2h_untagged(elf_bss);
2188 host_end = (uintptr_t) g2h_untagged(last_bss);
0c2d70c4 2189 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
cf129f3a
RH
2190
2191 if (host_map_start < host_end) {
2192 void *p = mmap((void *)host_map_start, host_end - host_map_start,
2193 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2194 if (p == MAP_FAILED) {
2195 perror("cannot mmap brk");
2196 exit(-1);
853d6f7a 2197 }
f46e9a0b 2198 }
853d6f7a 2199
f46e9a0b
TM
2200 /* Ensure that the bss page(s) are valid */
2201 if ((page_get_flags(last_bss-1) & prot) != prot) {
2202 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
cf129f3a 2203 }
31e31b8a 2204
cf129f3a
RH
2205 if (host_start < host_map_start) {
2206 memset((void *)host_start, 0, host_map_start - host_start);
2207 }
2208}
53a5960a 2209
cf58affe
CL
2210#ifdef TARGET_ARM
2211static int elf_is_fdpic(struct elfhdr *exec)
2212{
2213 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
2214}
2215#else
a99856cd
CL
2216/* Default implementation, always false. */
2217static int elf_is_fdpic(struct elfhdr *exec)
2218{
2219 return 0;
2220}
cf58affe 2221#endif
a99856cd 2222
1af02e83
MF
2223static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
2224{
2225 uint16_t n;
2226 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
2227
2228 /* elf32_fdpic_loadseg */
2229 n = info->nsegs;
2230 while (n--) {
2231 sp -= 12;
2232 put_user_u32(loadsegs[n].addr, sp+0);
2233 put_user_u32(loadsegs[n].p_vaddr, sp+4);
2234 put_user_u32(loadsegs[n].p_memsz, sp+8);
2235 }
2236
2237 /* elf32_fdpic_loadmap */
2238 sp -= 4;
2239 put_user_u16(0, sp+0); /* version */
2240 put_user_u16(info->nsegs, sp+2); /* nsegs */
2241
2242 info->personality = PER_LINUX_FDPIC;
2243 info->loadmap_addr = sp;
2244
2245 return sp;
2246}
1af02e83 2247
992f48a0 2248static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
8e62a717
RH
2249 struct elfhdr *exec,
2250 struct image_info *info,
2251 struct image_info *interp_info)
31e31b8a 2252{
d97ef72e 2253 abi_ulong sp;
7c4ee5bc 2254 abi_ulong u_argc, u_argv, u_envp, u_auxv;
d97ef72e 2255 int size;
14322bad
LA
2256 int i;
2257 abi_ulong u_rand_bytes;
2258 uint8_t k_rand_bytes[16];
fcdc0ab4
JY
2259 abi_ulong u_platform, u_base_platform;
2260 const char *k_platform, *k_base_platform;
d97ef72e
RH
2261 const int n = sizeof(elf_addr_t);
2262
2263 sp = p;
1af02e83 2264
1af02e83
MF
2265 /* Needs to be before we load the env/argc/... */
2266 if (elf_is_fdpic(exec)) {
2267 /* Need 4 byte alignment for these structs */
2268 sp &= ~3;
2269 sp = loader_build_fdpic_loadmap(info, sp);
2270 info->other_info = interp_info;
2271 if (interp_info) {
2272 interp_info->other_info = info;
2273 sp = loader_build_fdpic_loadmap(interp_info, sp);
3cb10cfa
CL
2274 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
2275 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
2276 } else {
2277 info->interpreter_loadmap_addr = 0;
2278 info->interpreter_pt_dynamic_addr = 0;
1af02e83
MF
2279 }
2280 }
1af02e83 2281
fcdc0ab4
JY
2282 u_base_platform = 0;
2283 k_base_platform = ELF_BASE_PLATFORM;
2284 if (k_base_platform) {
2285 size_t len = strlen(k_base_platform) + 1;
2286 if (STACK_GROWS_DOWN) {
2287 sp -= (len + n - 1) & ~(n - 1);
2288 u_base_platform = sp;
2289 /* FIXME - check return value of memcpy_to_target() for failure */
2290 memcpy_to_target(sp, k_base_platform, len);
2291 } else {
2292 memcpy_to_target(sp, k_base_platform, len);
2293 u_base_platform = sp;
2294 sp += len + 1;
2295 }
2296 }
2297
d97ef72e
RH
2298 u_platform = 0;
2299 k_platform = ELF_PLATFORM;
2300 if (k_platform) {
2301 size_t len = strlen(k_platform) + 1;
7c4ee5bc
RH
2302 if (STACK_GROWS_DOWN) {
2303 sp -= (len + n - 1) & ~(n - 1);
2304 u_platform = sp;
2305 /* FIXME - check return value of memcpy_to_target() for failure */
2306 memcpy_to_target(sp, k_platform, len);
2307 } else {
2308 memcpy_to_target(sp, k_platform, len);
2309 u_platform = sp;
2310 sp += len + 1;
2311 }
2312 }
2313
2314 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2315 * the argv and envp pointers.
2316 */
2317 if (STACK_GROWS_DOWN) {
2318 sp = QEMU_ALIGN_DOWN(sp, 16);
2319 } else {
2320 sp = QEMU_ALIGN_UP(sp, 16);
d97ef72e 2321 }
14322bad
LA
2322
2323 /*
c6a2377f 2324 * Generate 16 random bytes for userspace PRNG seeding.
14322bad 2325 */
c6a2377f 2326 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
7c4ee5bc
RH
2327 if (STACK_GROWS_DOWN) {
2328 sp -= 16;
2329 u_rand_bytes = sp;
2330 /* FIXME - check return value of memcpy_to_target() for failure */
2331 memcpy_to_target(sp, k_rand_bytes, 16);
2332 } else {
2333 memcpy_to_target(sp, k_rand_bytes, 16);
2334 u_rand_bytes = sp;
2335 sp += 16;
2336 }
14322bad 2337
d97ef72e 2338 size = (DLINFO_ITEMS + 1) * 2;
fcdc0ab4
JY
2339 if (k_base_platform)
2340 size += 2;
d97ef72e
RH
2341 if (k_platform)
2342 size += 2;
f5155289 2343#ifdef DLINFO_ARCH_ITEMS
d97ef72e 2344 size += DLINFO_ARCH_ITEMS * 2;
ad6919dc
PM
2345#endif
2346#ifdef ELF_HWCAP2
2347 size += 2;
f5155289 2348#endif
f516511e
PM
2349 info->auxv_len = size * n;
2350
d97ef72e 2351 size += envc + argc + 2;
b9329d4b 2352 size += 1; /* argc itself */
d97ef72e 2353 size *= n;
7c4ee5bc
RH
2354
2355 /* Allocate space and finalize stack alignment for entry now. */
2356 if (STACK_GROWS_DOWN) {
2357 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
2358 sp = u_argc;
2359 } else {
2360 u_argc = sp;
2361 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2362 }
2363
2364 u_argv = u_argc + n;
2365 u_envp = u_argv + (argc + 1) * n;
2366 u_auxv = u_envp + (envc + 1) * n;
2367 info->saved_auxv = u_auxv;
60f1c801
RH
2368 info->argc = argc;
2369 info->envc = envc;
2370 info->argv = u_argv;
2371 info->envp = u_envp;
d97ef72e
RH
2372
2373 /* This is correct because Linux defines
2374 * elf_addr_t as Elf32_Off / Elf64_Off
2375 */
2376#define NEW_AUX_ENT(id, val) do { \
7c4ee5bc
RH
2377 put_user_ual(id, u_auxv); u_auxv += n; \
2378 put_user_ual(val, u_auxv); u_auxv += n; \
d97ef72e
RH
2379 } while(0)
2380
82991bed
PM
2381#ifdef ARCH_DLINFO
2382 /*
2383 * ARCH_DLINFO must come first so platform specific code can enforce
2384 * special alignment requirements on the AUXV if necessary (eg. PPC).
2385 */
2386 ARCH_DLINFO;
2387#endif
f516511e
PM
2388 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2389 * on info->auxv_len will trigger.
2390 */
8e62a717 2391 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
d97ef72e
RH
2392 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2393 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
33143c44
LV
2394 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2395 /* Target doesn't support host page size alignment */
2396 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2397 } else {
2398 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2399 qemu_host_page_size)));
2400 }
8e62a717 2401 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
d97ef72e 2402 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
8e62a717 2403 NEW_AUX_ENT(AT_ENTRY, info->entry);
d97ef72e
RH
2404 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2405 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2406 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2407 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2408 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2409 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
14322bad 2410 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
444cd5c3 2411 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
e0d1673d 2412 NEW_AUX_ENT(AT_EXECFN, info->file_string);
14322bad 2413
ad6919dc
PM
2414#ifdef ELF_HWCAP2
2415 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2416#endif
2417
fcdc0ab4
JY
2418 if (u_base_platform) {
2419 NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform);
2420 }
7c4ee5bc 2421 if (u_platform) {
d97ef72e 2422 NEW_AUX_ENT(AT_PLATFORM, u_platform);
7c4ee5bc 2423 }
7c4ee5bc 2424 NEW_AUX_ENT (AT_NULL, 0);
f5155289
FB
2425#undef NEW_AUX_ENT
2426
f516511e
PM
2427 /* Check that our initial calculation of the auxv length matches how much
2428 * we actually put into it.
2429 */
2430 assert(info->auxv_len == u_auxv - info->saved_auxv);
7c4ee5bc
RH
2431
2432 put_user_ual(argc, u_argc);
2433
2434 p = info->arg_strings;
2435 for (i = 0; i < argc; ++i) {
2436 put_user_ual(p, u_argv);
2437 u_argv += n;
2438 p += target_strlen(p) + 1;
2439 }
2440 put_user_ual(0, u_argv);
2441
2442 p = info->env_strings;
2443 for (i = 0; i < envc; ++i) {
2444 put_user_ual(p, u_envp);
2445 u_envp += n;
2446 p += target_strlen(p) + 1;
2447 }
2448 put_user_ual(0, u_envp);
edf8e2af 2449
d97ef72e 2450 return sp;
31e31b8a
FB
2451}
2452
f5ef0e51 2453#if defined(HI_COMMPAGE)
eee816c0 2454#define LO_COMMPAGE -1
f5ef0e51
RH
2455#elif defined(LO_COMMPAGE)
2456#define HI_COMMPAGE 0
2457#else
66346faf 2458#define HI_COMMPAGE 0
eee816c0 2459#define LO_COMMPAGE -1
d461b73e 2460#ifndef INIT_GUEST_COMMPAGE
ee947430 2461#define init_guest_commpage() true
8756e136 2462#endif
d461b73e 2463#endif
dce10401 2464
ee947430
AB
2465static void pgb_fail_in_use(const char *image_name)
2466{
2467 error_report("%s: requires virtual address space that is in use "
2468 "(omit the -B option or choose a different value)",
2469 image_name);
2470 exit(EXIT_FAILURE);
2471}
dce10401 2472
ee947430
AB
2473static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
2474 abi_ulong guest_hiaddr, long align)
2475{
2476 const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2477 void *addr, *test;
2a53535a 2478
ee947430 2479 if (!QEMU_IS_ALIGNED(guest_base, align)) {
5ca870b9 2480 fprintf(stderr, "Requested guest base %p does not satisfy "
ee947430 2481 "host minimum alignment (0x%lx)\n",
5ca870b9 2482 (void *)guest_base, align);
ee947430
AB
2483 exit(EXIT_FAILURE);
2484 }
2485
2486 /* Sanity check the guest binary. */
2487 if (reserved_va) {
2488 if (guest_hiaddr > reserved_va) {
2489 error_report("%s: requires more than reserved virtual "
2490 "address space (0x%" PRIx64 " > 0x%lx)",
2491 image_name, (uint64_t)guest_hiaddr, reserved_va);
2492 exit(EXIT_FAILURE);
2a53535a 2493 }
ee947430 2494 } else {
a932eec4 2495#if HOST_LONG_BITS < TARGET_ABI_BITS
ee947430
AB
2496 if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) {
2497 error_report("%s: requires more virtual address space "
2498 "than the host can provide (0x%" PRIx64 ")",
2499 image_name, (uint64_t)guest_hiaddr - guest_base);
2500 exit(EXIT_FAILURE);
2a53535a 2501 }
a932eec4 2502#endif
2a53535a 2503 }
2a53535a 2504
ee947430
AB
2505 /*
2506 * Expand the allocation to the entire reserved_va.
2507 * Exclude the mmap_min_addr hole.
2508 */
2509 if (reserved_va) {
2510 guest_loaddr = (guest_base >= mmap_min_addr ? 0
2511 : mmap_min_addr - guest_base);
2512 guest_hiaddr = reserved_va;
2513 }
806d1021 2514
ee947430 2515 /* Reserve the address space for the binary, or reserved_va. */
3e8f1628 2516 test = g2h_untagged(guest_loaddr);
ee947430
AB
2517 addr = mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0);
2518 if (test != addr) {
2519 pgb_fail_in_use(image_name);
2520 }
e7588237
AB
2521 qemu_log_mask(CPU_LOG_PAGE,
2522 "%s: base @ %p for " TARGET_ABI_FMT_ld " bytes\n",
2523 __func__, addr, guest_hiaddr - guest_loaddr);
ee947430
AB
2524}
2525
ad592e37
AB
2526/**
2527 * pgd_find_hole_fallback: potential mmap address
2528 * @guest_size: size of available space
2529 * @brk: location of break
2530 * @align: memory alignment
2531 *
2532 * This is a fallback method for finding a hole in the host address
2533 * space if we don't have the benefit of being able to access
2534 * /proc/self/map. It can potentially take a very long time as we can
2535 * only dumbly iterate up the host address space seeing if the
2536 * allocation would work.
2537 */
5c3e87f3
AB
2538static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
2539 long align, uintptr_t offset)
ad592e37
AB
2540{
2541 uintptr_t base;
2542
2543 /* Start (aligned) at the bottom and work our way up */
2544 base = ROUND_UP(mmap_min_addr, align);
2545
2546 while (true) {
2547 uintptr_t align_start, end;
2548 align_start = ROUND_UP(base, align);
5c3e87f3 2549 end = align_start + guest_size + offset;
ad592e37
AB
2550
2551 /* if brk is anywhere in the range give ourselves some room to grow. */
2552 if (align_start <= brk && brk < end) {
2553 base = brk + (16 * MiB);
2554 continue;
2555 } else if (align_start + guest_size < align_start) {
2556 /* we have run out of space */
2557 return -1;
2558 } else {
2667e069
AB
2559 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE |
2560 MAP_FIXED_NOREPLACE;
ad592e37
AB
2561 void * mmap_start = mmap((void *) align_start, guest_size,
2562 PROT_NONE, flags, -1, 0);
2563 if (mmap_start != MAP_FAILED) {
7e588fbc 2564 munmap(mmap_start, guest_size);
934eed51 2565 if (mmap_start == (void *) align_start) {
e7588237
AB
2566 qemu_log_mask(CPU_LOG_PAGE,
2567 "%s: base @ %p for %" PRIdPTR" bytes\n",
2568 __func__, mmap_start + offset, guest_size);
2667e069
AB
2569 return (uintptr_t) mmap_start + offset;
2570 }
ad592e37
AB
2571 }
2572 base += qemu_host_page_size;
2573 }
2574 }
2575}
2576
ee947430
AB
2577/* Return value for guest_base, or -1 if no hole found. */
2578static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
5c3e87f3 2579 long align, uintptr_t offset)
ee947430
AB
2580{
2581 GSList *maps, *iter;
2582 uintptr_t this_start, this_end, next_start, brk;
2583 intptr_t ret = -1;
2584
2585 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
2586
2587 maps = read_self_maps();
dce10401 2588
ee947430
AB
2589 /* Read brk after we've read the maps, which will malloc. */
2590 brk = (uintptr_t)sbrk(0);
2591
ad592e37 2592 if (!maps) {
190674f3 2593 return pgd_find_hole_fallback(guest_size, brk, align, offset);
ad592e37
AB
2594 }
2595
ee947430
AB
2596 /* The first hole is before the first map entry. */
2597 this_start = mmap_min_addr;
2598
2599 for (iter = maps; iter;
2600 this_start = next_start, iter = g_slist_next(iter)) {
2601 uintptr_t align_start, hole_size;
2602
2603 this_end = ((MapInfo *)iter->data)->start;
2604 next_start = ((MapInfo *)iter->data)->end;
5c3e87f3 2605 align_start = ROUND_UP(this_start + offset, align);
ee947430
AB
2606
2607 /* Skip holes that are too small. */
2608 if (align_start >= this_end) {
2609 continue;
2610 }
2611 hole_size = this_end - align_start;
2612 if (hole_size < guest_size) {
2613 continue;
aac362e4
LS
2614 }
2615
ee947430
AB
2616 /* If this hole contains brk, give ourselves some room to grow. */
2617 if (this_start <= brk && brk < this_end) {
2618 hole_size -= guest_size;
2619 if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) {
2620 align_start += 1 * GiB;
2621 } else if (hole_size >= 16 * MiB) {
2622 align_start += 16 * MiB;
2623 } else {
2624 align_start = (this_end - guest_size) & -align;
2625 if (align_start < this_start) {
2626 continue;
2627 }
806d1021 2628 }
806d1021
MI
2629 }
2630
ee947430
AB
2631 /* Record the lowest successful match. */
2632 if (ret < 0) {
190674f3 2633 ret = align_start;
dce10401 2634 }
ee947430
AB
2635 /* If this hole contains the identity map, select it. */
2636 if (align_start <= guest_loaddr &&
2637 guest_loaddr + guest_size <= this_end) {
2638 ret = 0;
b859040d 2639 }
ee947430
AB
2640 /* If this hole ends above the identity map, stop looking. */
2641 if (this_end >= guest_loaddr) {
2642 break;
dce10401
MI
2643 }
2644 }
ee947430 2645 free_self_maps(maps);
dce10401 2646
e7588237
AB
2647 if (ret != -1) {
2648 qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %" PRIxPTR
2649 " for %" PRIuPTR " bytes\n",
2650 __func__, ret, guest_size);
2651 }
2652
ee947430 2653 return ret;
dce10401
MI
2654}
2655
ee947430
AB
2656static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
2657 abi_ulong orig_hiaddr, long align)
f3ed1f5d 2658{
ee947430
AB
2659 uintptr_t loaddr = orig_loaddr;
2660 uintptr_t hiaddr = orig_hiaddr;
5c3e87f3 2661 uintptr_t offset = 0;
ee947430 2662 uintptr_t addr;
f3ed1f5d 2663
ee947430
AB
2664 if (hiaddr != orig_hiaddr) {
2665 error_report("%s: requires virtual address space that the "
2666 "host cannot provide (0x%" PRIx64 ")",
2667 image_name, (uint64_t)orig_hiaddr);
2668 exit(EXIT_FAILURE);
2669 }
f3ed1f5d 2670
ee947430 2671 loaddr &= -align;
66346faf 2672 if (HI_COMMPAGE) {
ee947430
AB
2673 /*
2674 * Extend the allocation to include the commpage.
5c3e87f3
AB
2675 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2676 * need to ensure there is space bellow the guest_base so we
2677 * can map the commpage in the place needed when the address
2678 * arithmetic wraps around.
ee947430
AB
2679 */
2680 if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
5c3e87f3 2681 hiaddr = (uintptr_t) 4 << 30;
f3ed1f5d 2682 } else {
66346faf 2683 offset = -(HI_COMMPAGE & -align);
f3ed1f5d 2684 }
eee816c0 2685 } else if (LO_COMMPAGE != -1) {
f5ef0e51 2686 loaddr = MIN(loaddr, LO_COMMPAGE & -align);
ee947430 2687 }
dce10401 2688
5c3e87f3 2689 addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
ee947430
AB
2690 if (addr == -1) {
2691 /*
66346faf 2692 * If HI_COMMPAGE, there *might* be a non-consecutive allocation
ee947430
AB
2693 * that can satisfy both. But as the normal arm32 link base address
2694 * is ~32k, and we extend down to include the commpage, making the
2695 * overhead only ~96k, this is unlikely.
dce10401 2696 */
ee947430
AB
2697 error_report("%s: Unable to allocate %#zx bytes of "
2698 "virtual address space", image_name,
2699 (size_t)(hiaddr - loaddr));
2700 exit(EXIT_FAILURE);
2701 }
2702
2703 guest_base = addr;
e7588237
AB
2704
2705 qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %"PRIxPTR" for %" PRIuPTR" bytes\n",
2706 __func__, addr, hiaddr - loaddr);
ee947430 2707}
dce10401 2708
ee947430
AB
2709static void pgb_dynamic(const char *image_name, long align)
2710{
2711 /*
2712 * The executable is dynamic and does not require a fixed address.
2713 * All we need is a commpage that satisfies align.
2714 * If we do not need a commpage, leave guest_base == 0.
2715 */
66346faf 2716 if (HI_COMMPAGE) {
ee947430
AB
2717 uintptr_t addr, commpage;
2718
2719 /* 64-bit hosts should have used reserved_va. */
2720 assert(sizeof(uintptr_t) == 4);
2721
2722 /*
2723 * By putting the commpage at the first hole, that puts guest_base
2724 * just above that, and maximises the positive guest addresses.
2725 */
66346faf 2726 commpage = HI_COMMPAGE & -align;
5c3e87f3 2727 addr = pgb_find_hole(commpage, -commpage, align, 0);
ee947430
AB
2728 assert(addr != -1);
2729 guest_base = addr;
2730 }
2731}
2732
2733static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
2734 abi_ulong guest_hiaddr, long align)
2735{
c1f6ad79 2736 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
ee947430
AB
2737 void *addr, *test;
2738
2739 if (guest_hiaddr > reserved_va) {
2740 error_report("%s: requires more than reserved virtual "
2741 "address space (0x%" PRIx64 " > 0x%lx)",
2742 image_name, (uint64_t)guest_hiaddr, reserved_va);
2743 exit(EXIT_FAILURE);
f3ed1f5d 2744 }
f3ed1f5d 2745
ee947430
AB
2746 /* Widen the "image" to the entire reserved address space. */
2747 pgb_static(image_name, 0, reserved_va, align);
2748
2667e069 2749 /* osdep.h defines this as 0 if it's missing */
c1f6ad79 2750 flags |= MAP_FIXED_NOREPLACE;
c1f6ad79 2751
ee947430
AB
2752 /* Reserve the memory on the host. */
2753 assert(guest_base != 0);
3e8f1628 2754 test = g2h_untagged(0);
ee947430 2755 addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0);
fb730c86 2756 if (addr == MAP_FAILED || addr != test) {
ee947430 2757 error_report("Unable to reserve 0x%lx bytes of virtual address "
87966743 2758 "space at %p (%s) for use as guest address space (check your "
fb730c86
AB
2759 "virtual memory ulimit setting, min_mmap_addr or reserve less "
2760 "using -R option)", reserved_va, test, strerror(errno));
ee947430
AB
2761 exit(EXIT_FAILURE);
2762 }
e7588237
AB
2763
2764 qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %p for %lu bytes\n",
2765 __func__, addr, reserved_va);
f3ed1f5d
PM
2766}
2767
ee947430
AB
2768void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
2769 abi_ulong guest_hiaddr)
2770{
2771 /* In order to use host shmat, we must be able to honor SHMLBA. */
2772 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
2773
2774 if (have_guest_base) {
2775 pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align);
2776 } else if (reserved_va) {
2777 pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align);
2778 } else if (guest_loaddr) {
2779 pgb_static(image_name, guest_loaddr, guest_hiaddr, align);
2780 } else {
2781 pgb_dynamic(image_name, align);
2782 }
2783
2784 /* Reserve and initialize the commpage. */
2785 if (!init_guest_commpage()) {
2786 /*
2787 * With have_guest_base, the user has selected the address and
2788 * we are trying to work with that. Otherwise, we have selected
2789 * free space and init_guest_commpage must succeeded.
2790 */
2791 assert(have_guest_base);
2792 pgb_fail_in_use(image_name);
2793 }
2794
2795 assert(QEMU_IS_ALIGNED(guest_base, align));
2796 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
2797 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
2798}
f3ed1f5d 2799
83f990eb
RH
2800enum {
2801 /* The string "GNU\0" as a magic number. */
2802 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
2803 NOTE_DATA_SZ = 1 * KiB,
2804 NOTE_NAME_SZ = 4,
2805 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
2806};
2807
2808/*
2809 * Process a single gnu_property entry.
2810 * Return false for error.
2811 */
2812static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
2813 struct image_info *info, bool have_prev_type,
2814 uint32_t *prev_type, Error **errp)
2815{
2816 uint32_t pr_type, pr_datasz, step;
2817
2818 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
2819 goto error_data;
2820 }
2821 datasz -= *off;
2822 data += *off / sizeof(uint32_t);
2823
2824 if (datasz < 2 * sizeof(uint32_t)) {
2825 goto error_data;
2826 }
2827 pr_type = data[0];
2828 pr_datasz = data[1];
2829 data += 2;
2830 datasz -= 2 * sizeof(uint32_t);
2831 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
2832 if (step > datasz) {
2833 goto error_data;
2834 }
2835
2836 /* Properties are supposed to be unique and sorted on pr_type. */
2837 if (have_prev_type && pr_type <= *prev_type) {
2838 if (pr_type == *prev_type) {
2839 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
2840 } else {
2841 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
2842 }
2843 return false;
2844 }
2845 *prev_type = pr_type;
2846
2847 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
2848 return false;
2849 }
2850
2851 *off += 2 * sizeof(uint32_t) + step;
2852 return true;
2853
2854 error_data:
2855 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
2856 return false;
2857}
2858
2859/* Process NT_GNU_PROPERTY_TYPE_0. */
2860static bool parse_elf_properties(int image_fd,
2861 struct image_info *info,
2862 const struct elf_phdr *phdr,
2863 char bprm_buf[BPRM_BUF_SIZE],
2864 Error **errp)
2865{
2866 union {
2867 struct elf_note nhdr;
2868 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
2869 } note;
2870
2871 int n, off, datasz;
2872 bool have_prev_type;
2873 uint32_t prev_type;
2874
2875 /* Unless the arch requires properties, ignore them. */
2876 if (!ARCH_USE_GNU_PROPERTY) {
2877 return true;
2878 }
2879
2880 /* If the properties are crazy large, that's too bad. */
2881 n = phdr->p_filesz;
2882 if (n > sizeof(note)) {
2883 error_setg(errp, "PT_GNU_PROPERTY too large");
2884 return false;
2885 }
2886 if (n < sizeof(note.nhdr)) {
2887 error_setg(errp, "PT_GNU_PROPERTY too small");
2888 return false;
2889 }
2890
2891 if (phdr->p_offset + n <= BPRM_BUF_SIZE) {
2892 memcpy(&note, bprm_buf + phdr->p_offset, n);
2893 } else {
2894 ssize_t len = pread(image_fd, &note, n, phdr->p_offset);
2895 if (len != n) {
2896 error_setg_errno(errp, errno, "Error reading file header");
2897 return false;
2898 }
2899 }
2900
2901 /*
2902 * The contents of a valid PT_GNU_PROPERTY is a sequence
2903 * of uint32_t -- swap them all now.
2904 */
2905#ifdef BSWAP_NEEDED
2906 for (int i = 0; i < n / 4; i++) {
2907 bswap32s(note.data + i);
2908 }
2909#endif
2910
2911 /*
2912 * Note that nhdr is 3 words, and that the "name" described by namesz
2913 * immediately follows nhdr and is thus at the 4th word. Further, all
2914 * of the inputs to the kernel's round_up are multiples of 4.
2915 */
2916 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
2917 note.nhdr.n_namesz != NOTE_NAME_SZ ||
2918 note.data[3] != GNU0_MAGIC) {
2919 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
2920 return false;
2921 }
2922 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
2923
2924 datasz = note.nhdr.n_descsz + off;
2925 if (datasz > n) {
2926 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
2927 return false;
2928 }
2929
2930 have_prev_type = false;
2931 prev_type = 0;
2932 while (1) {
2933 if (off == datasz) {
2934 return true; /* end, exit ok */
2935 }
2936 if (!parse_elf_property(note.data, &off, datasz, info,
2937 have_prev_type, &prev_type, errp)) {
2938 return false;
2939 }
2940 have_prev_type = true;
2941 }
2942}
2943
8e62a717 2944/* Load an ELF image into the address space.
31e31b8a 2945
8e62a717
RH
2946 IMAGE_NAME is the filename of the image, to use in error messages.
2947 IMAGE_FD is the open file descriptor for the image.
2948
2949 BPRM_BUF is a copy of the beginning of the file; this of course
2950 contains the elf file header at offset 0. It is assumed that this
2951 buffer is sufficiently aligned to present no problems to the host
2952 in accessing data at aligned offsets within the buffer.
2953
2954 On return: INFO values will be filled in, as necessary or available. */
2955
2956static void load_elf_image(const char *image_name, int image_fd,
bf858897 2957 struct image_info *info, char **pinterp_name,
8e62a717 2958 char bprm_buf[BPRM_BUF_SIZE])
31e31b8a 2959{
8e62a717
RH
2960 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2961 struct elf_phdr *phdr;
2962 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
e8384b37 2963 int i, retval, prot_exec;
c7f17e7b 2964 Error *err = NULL;
5fafdf24 2965
8e62a717 2966 /* First of all, some simple consistency checks */
8e62a717 2967 if (!elf_check_ident(ehdr)) {
c7f17e7b 2968 error_setg(&err, "Invalid ELF image for this architecture");
8e62a717
RH
2969 goto exit_errmsg;
2970 }
2971 bswap_ehdr(ehdr);
2972 if (!elf_check_ehdr(ehdr)) {
c7f17e7b 2973 error_setg(&err, "Invalid ELF image for this architecture");
8e62a717 2974 goto exit_errmsg;
d97ef72e 2975 }
5fafdf24 2976
8e62a717
RH
2977 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2978 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2979 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
9955ffac 2980 } else {
8e62a717
RH
2981 phdr = (struct elf_phdr *) alloca(i);
2982 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
9955ffac 2983 if (retval != i) {
8e62a717 2984 goto exit_read;
9955ffac 2985 }
d97ef72e 2986 }
8e62a717 2987 bswap_phdr(phdr, ehdr->e_phnum);
09bfb054 2988
1af02e83
MF
2989 info->nsegs = 0;
2990 info->pt_dynamic_addr = 0;
1af02e83 2991
98c1076c
AB
2992 mmap_lock();
2993
8a1a5274
RH
2994 /*
2995 * Find the maximum size of the image and allocate an appropriate
2996 * amount of memory to handle that. Locate the interpreter, if any.
2997 */
682674b8 2998 loaddr = -1, hiaddr = 0;
33143c44 2999 info->alignment = 0;
872f3d04 3000 info->exec_stack = EXSTACK_DEFAULT;
8e62a717 3001 for (i = 0; i < ehdr->e_phnum; ++i) {
4d9d535a
RH
3002 struct elf_phdr *eppnt = phdr + i;
3003 if (eppnt->p_type == PT_LOAD) {
3004 abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
682674b8
RH
3005 if (a < loaddr) {
3006 loaddr = a;
3007 }
4d9d535a 3008 a = eppnt->p_vaddr + eppnt->p_memsz;
682674b8
RH
3009 if (a > hiaddr) {
3010 hiaddr = a;
3011 }
1af02e83 3012 ++info->nsegs;
4d9d535a 3013 info->alignment |= eppnt->p_align;
8a1a5274
RH
3014 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
3015 g_autofree char *interp_name = NULL;
3016
3017 if (*pinterp_name) {
c7f17e7b 3018 error_setg(&err, "Multiple PT_INTERP entries");
8a1a5274
RH
3019 goto exit_errmsg;
3020 }
c7f17e7b 3021
8a1a5274 3022 interp_name = g_malloc(eppnt->p_filesz);
8a1a5274
RH
3023
3024 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
3025 memcpy(interp_name, bprm_buf + eppnt->p_offset,
3026 eppnt->p_filesz);
3027 } else {
3028 retval = pread(image_fd, interp_name, eppnt->p_filesz,
3029 eppnt->p_offset);
3030 if (retval != eppnt->p_filesz) {
c7f17e7b 3031 goto exit_read;
8a1a5274
RH
3032 }
3033 }
3034 if (interp_name[eppnt->p_filesz - 1] != 0) {
c7f17e7b 3035 error_setg(&err, "Invalid PT_INTERP entry");
8a1a5274
RH
3036 goto exit_errmsg;
3037 }
3038 *pinterp_name = g_steal_pointer(&interp_name);
83f990eb
RH
3039 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
3040 if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &err)) {
3041 goto exit_errmsg;
3042 }
872f3d04
RH
3043 } else if (eppnt->p_type == PT_GNU_STACK) {
3044 info->exec_stack = eppnt->p_flags & PF_X;
682674b8
RH
3045 }
3046 }
3047
6fd59449
RH
3048 if (pinterp_name != NULL) {
3049 /*
3050 * This is the main executable.
3051 *
3052 * Reserve extra space for brk.
3053 * We hold on to this space while placing the interpreter
3054 * and the stack, lest they be placed immediately after
3055 * the data segment and block allocation from the brk.
3056 *
11d36727
AB
3057 * 16MB is chosen as "large enough" without being so large as
3058 * to allow the result to not fit with a 32-bit guest on a
3059 * 32-bit host. However some 64 bit guests (e.g. s390x)
3060 * attempt to place their heap further ahead and currently
3061 * nothing stops them smashing into QEMUs address space.
6fd59449 3062 */
11d36727
AB
3063#if TARGET_LONG_BITS == 64
3064 info->reserve_brk = 32 * MiB;
3065#else
6fd59449 3066 info->reserve_brk = 16 * MiB;
11d36727 3067#endif
6fd59449
RH
3068 hiaddr += info->reserve_brk;
3069
3070 if (ehdr->e_type == ET_EXEC) {
3071 /*
3072 * Make sure that the low address does not conflict with
3073 * MMAP_MIN_ADDR or the QEMU application itself.
3074 */
3075 probe_guest_base(image_name, loaddr, hiaddr);
ee947430
AB
3076 } else {
3077 /*
3078 * The binary is dynamic, but we still need to
3079 * select guest_base. In this case we pass a size.
3080 */
3081 probe_guest_base(image_name, 0, hiaddr - loaddr);
d97ef72e 3082 }
6fd59449
RH
3083 }
3084
3085 /*
3086 * Reserve address space for all of this.
3087 *
3088 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
3089 * exactly the address range that is required.
3090 *
3091 * Otherwise this is ET_DYN, and we are searching for a location
3092 * that can hold the memory space required. If the image is
3093 * pre-linked, LOADDR will be non-zero, and the kernel should
3094 * honor that address if it happens to be free.
3095 *
3096 * In both cases, we will overwrite pages in this range with mappings
3097 * from the executable.
3098 */
3099 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
3100 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
3101 (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0),
3102 -1, 0);
3103 if (load_addr == -1) {
c7f17e7b 3104 goto exit_mmap;
d97ef72e 3105 }
682674b8 3106 load_bias = load_addr - loaddr;
d97ef72e 3107
a99856cd 3108 if (elf_is_fdpic(ehdr)) {
1af02e83 3109 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
7267c094 3110 g_malloc(sizeof(*loadsegs) * info->nsegs);
1af02e83
MF
3111
3112 for (i = 0; i < ehdr->e_phnum; ++i) {
3113 switch (phdr[i].p_type) {
3114 case PT_DYNAMIC:
3115 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
3116 break;
3117 case PT_LOAD:
3118 loadsegs->addr = phdr[i].p_vaddr + load_bias;
3119 loadsegs->p_vaddr = phdr[i].p_vaddr;
3120 loadsegs->p_memsz = phdr[i].p_memsz;
3121 ++loadsegs;
3122 break;
3123 }
3124 }
3125 }
1af02e83 3126
8e62a717 3127 info->load_bias = load_bias;
dc12567a
JK
3128 info->code_offset = load_bias;
3129 info->data_offset = load_bias;
8e62a717
RH
3130 info->load_addr = load_addr;
3131 info->entry = ehdr->e_entry + load_bias;
3132 info->start_code = -1;
3133 info->end_code = 0;
3134 info->start_data = -1;
3135 info->end_data = 0;
3136 info->brk = 0;
d8fd2954 3137 info->elf_flags = ehdr->e_flags;
8e62a717 3138
e8384b37
RH
3139 prot_exec = PROT_EXEC;
3140#ifdef TARGET_AARCH64
3141 /*
3142 * If the BTI feature is present, this indicates that the executable
3143 * pages of the startup binary should be mapped with PROT_BTI, so that
3144 * branch targets are enforced.
3145 *
3146 * The startup binary is either the interpreter or the static executable.
3147 * The interpreter is responsible for all pages of a dynamic executable.
3148 *
3149 * Elf notes are backward compatible to older cpus.
3150 * Do not enable BTI unless it is supported.
3151 */
3152 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
3153 && (pinterp_name == NULL || *pinterp_name == 0)
3154 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
3155 prot_exec |= TARGET_PROT_BTI;
3156 }
3157#endif
3158
8e62a717
RH
3159 for (i = 0; i < ehdr->e_phnum; i++) {
3160 struct elf_phdr *eppnt = phdr + i;
d97ef72e 3161 if (eppnt->p_type == PT_LOAD) {
94894ff2 3162 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
d97ef72e 3163 int elf_prot = 0;
d97ef72e 3164
e5eaf570
RH
3165 if (eppnt->p_flags & PF_R) {
3166 elf_prot |= PROT_READ;
3167 }
3168 if (eppnt->p_flags & PF_W) {
3169 elf_prot |= PROT_WRITE;
3170 }
3171 if (eppnt->p_flags & PF_X) {
e8384b37 3172 elf_prot |= prot_exec;
e5eaf570 3173 }
d97ef72e 3174
682674b8
RH
3175 vaddr = load_bias + eppnt->p_vaddr;
3176 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
3177 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
22d113b5
GM
3178
3179 vaddr_ef = vaddr + eppnt->p_filesz;
3180 vaddr_em = vaddr + eppnt->p_memsz;
682674b8 3181
d87146bc 3182 /*
22d113b5
GM
3183 * Some segments may be completely empty, with a non-zero p_memsz
3184 * but no backing file segment.
d87146bc
GM
3185 */
3186 if (eppnt->p_filesz != 0) {
22d113b5 3187 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
d87146bc
GM
3188 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
3189 MAP_PRIVATE | MAP_FIXED,
3190 image_fd, eppnt->p_offset - vaddr_po);
3191
3192 if (error == -1) {
c7f17e7b 3193 goto exit_mmap;
d87146bc 3194 }
09bfb054 3195
22d113b5
GM
3196 /*
3197 * If the load segment requests extra zeros (e.g. bss), map it.
3198 */
3199 if (eppnt->p_filesz < eppnt->p_memsz) {
3200 zero_bss(vaddr_ef, vaddr_em, elf_prot);
3201 }
3202 } else if (eppnt->p_memsz != 0) {
3203 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_memsz + vaddr_po);
3204 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
3205 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
3206 -1, 0);
31e31b8a 3207
22d113b5
GM
3208 if (error == -1) {
3209 goto exit_mmap;
3210 }
cf129f3a 3211 }
8e62a717
RH
3212
3213 /* Find the full program boundaries. */
3214 if (elf_prot & PROT_EXEC) {
3215 if (vaddr < info->start_code) {
3216 info->start_code = vaddr;
3217 }
3218 if (vaddr_ef > info->end_code) {
3219 info->end_code = vaddr_ef;
3220 }
3221 }
3222 if (elf_prot & PROT_WRITE) {
3223 if (vaddr < info->start_data) {
3224 info->start_data = vaddr;
3225 }
3226 if (vaddr_ef > info->end_data) {
3227 info->end_data = vaddr_ef;
3228 }
8a045188
TB
3229 }
3230 if (vaddr_em > info->brk) {
3231 info->brk = vaddr_em;
8e62a717 3232 }
5dd0db52
SM
3233#ifdef TARGET_MIPS
3234 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
3235 Mips_elf_abiflags_v0 abiflags;
3236 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
c7f17e7b 3237 error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry");
5dd0db52
SM
3238 goto exit_errmsg;
3239 }
3240 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
3241 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
3242 sizeof(Mips_elf_abiflags_v0));
3243 } else {
3244 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
3245 eppnt->p_offset);
3246 if (retval != sizeof(Mips_elf_abiflags_v0)) {
c7f17e7b 3247 goto exit_read;
5dd0db52
SM
3248 }
3249 }
3250 bswap_mips_abiflags(&abiflags);
c94cb6c9 3251 info->fp_abi = abiflags.fp_abi;
5dd0db52 3252#endif
d97ef72e 3253 }
682674b8 3254 }
5fafdf24 3255
8e62a717
RH
3256 if (info->end_data == 0) {
3257 info->start_data = info->end_code;
3258 info->end_data = info->end_code;
8e62a717
RH
3259 }
3260
682674b8 3261 if (qemu_log_enabled()) {
8e62a717 3262 load_symbols(ehdr, image_fd, load_bias);
682674b8 3263 }
31e31b8a 3264
7c10cb38
IL
3265 debuginfo_report_elf(image_name, image_fd, load_bias);
3266
98c1076c
AB
3267 mmap_unlock();
3268
8e62a717
RH
3269 close(image_fd);
3270 return;
3271
3272 exit_read:
3273 if (retval >= 0) {
c7f17e7b
RH
3274 error_setg(&err, "Incomplete read of file header");
3275 } else {
3276 error_setg_errno(&err, errno, "Error reading file header");
8e62a717 3277 }
c7f17e7b
RH
3278 goto exit_errmsg;
3279 exit_mmap:
3280 error_setg_errno(&err, errno, "Error mapping file");
3281 goto exit_errmsg;
8e62a717 3282 exit_errmsg:
c7f17e7b 3283 error_reportf_err(err, "%s: ", image_name);
8e62a717
RH
3284 exit(-1);
3285}
3286
3287static void load_elf_interp(const char *filename, struct image_info *info,
3288 char bprm_buf[BPRM_BUF_SIZE])
3289{
3290 int fd, retval;
808f6563 3291 Error *err = NULL;
8e62a717
RH
3292
3293 fd = open(path(filename), O_RDONLY);
3294 if (fd < 0) {
808f6563
RH
3295 error_setg_file_open(&err, errno, filename);
3296 error_report_err(err);
3297 exit(-1);
8e62a717 3298 }
31e31b8a 3299
8e62a717
RH
3300 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
3301 if (retval < 0) {
808f6563
RH
3302 error_setg_errno(&err, errno, "Error reading file header");
3303 error_reportf_err(err, "%s: ", filename);
3304 exit(-1);
8e62a717 3305 }
808f6563 3306
8e62a717
RH
3307 if (retval < BPRM_BUF_SIZE) {
3308 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
3309 }
3310
bf858897 3311 load_elf_image(filename, fd, info, NULL, bprm_buf);
31e31b8a
FB
3312}
3313
49918a75
PB
3314static int symfind(const void *s0, const void *s1)
3315{
c7c530cd 3316 target_ulong addr = *(target_ulong *)s0;
49918a75
PB
3317 struct elf_sym *sym = (struct elf_sym *)s1;
3318 int result = 0;
c7c530cd 3319 if (addr < sym->st_value) {
49918a75 3320 result = -1;
c7c530cd 3321 } else if (addr >= sym->st_value + sym->st_size) {
49918a75
PB
3322 result = 1;
3323 }
3324 return result;
3325}
3326
3327static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
3328{
3329#if ELF_CLASS == ELFCLASS32
3330 struct elf_sym *syms = s->disas_symtab.elf32;
3331#else
3332 struct elf_sym *syms = s->disas_symtab.elf64;
3333#endif
3334
3335 // binary search
49918a75
PB
3336 struct elf_sym *sym;
3337
c7c530cd 3338 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
7cba04f6 3339 if (sym != NULL) {
49918a75
PB
3340 return s->disas_strtab + sym->st_name;
3341 }
3342
3343 return "";
3344}
3345
3346/* FIXME: This should use elf_ops.h */
3347static int symcmp(const void *s0, const void *s1)
3348{
3349 struct elf_sym *sym0 = (struct elf_sym *)s0;
3350 struct elf_sym *sym1 = (struct elf_sym *)s1;
3351 return (sym0->st_value < sym1->st_value)
3352 ? -1
3353 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
3354}
3355
689f936f 3356/* Best attempt to load symbols from this ELF object. */
682674b8 3357static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
689f936f 3358{
682674b8 3359 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
1e06262d 3360 uint64_t segsz;
682674b8 3361 struct elf_shdr *shdr;
b9475279
CV
3362 char *strings = NULL;
3363 struct syminfo *s = NULL;
3364 struct elf_sym *new_syms, *syms = NULL;
689f936f 3365
682674b8
RH
3366 shnum = hdr->e_shnum;
3367 i = shnum * sizeof(struct elf_shdr);
3368 shdr = (struct elf_shdr *)alloca(i);
3369 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
3370 return;
3371 }
3372
3373 bswap_shdr(shdr, shnum);
3374 for (i = 0; i < shnum; ++i) {
3375 if (shdr[i].sh_type == SHT_SYMTAB) {
3376 sym_idx = i;
3377 str_idx = shdr[i].sh_link;
49918a75
PB
3378 goto found;
3379 }
689f936f 3380 }
682674b8
RH
3381
3382 /* There will be no symbol table if the file was stripped. */
3383 return;
689f936f
FB
3384
3385 found:
682674b8 3386 /* Now know where the strtab and symtab are. Snarf them. */
0ef9ea29 3387 s = g_try_new(struct syminfo, 1);
682674b8 3388 if (!s) {
b9475279 3389 goto give_up;
682674b8 3390 }
5fafdf24 3391
1e06262d
PM
3392 segsz = shdr[str_idx].sh_size;
3393 s->disas_strtab = strings = g_try_malloc(segsz);
3394 if (!strings ||
3395 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
b9475279 3396 goto give_up;
682674b8 3397 }
49918a75 3398
1e06262d
PM
3399 segsz = shdr[sym_idx].sh_size;
3400 syms = g_try_malloc(segsz);
3401 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
b9475279 3402 goto give_up;
682674b8 3403 }
31e31b8a 3404
1e06262d
PM
3405 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
3406 /* Implausibly large symbol table: give up rather than ploughing
3407 * on with the number of symbols calculation overflowing
3408 */
3409 goto give_up;
3410 }
3411 nsyms = segsz / sizeof(struct elf_sym);
682674b8 3412 for (i = 0; i < nsyms; ) {
49918a75 3413 bswap_sym(syms + i);
682674b8
RH
3414 /* Throw away entries which we do not need. */
3415 if (syms[i].st_shndx == SHN_UNDEF
3416 || syms[i].st_shndx >= SHN_LORESERVE
3417 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3418 if (i < --nsyms) {
49918a75
PB
3419 syms[i] = syms[nsyms];
3420 }
682674b8 3421 } else {
49918a75 3422#if defined(TARGET_ARM) || defined (TARGET_MIPS)
682674b8
RH
3423 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3424 syms[i].st_value &= ~(target_ulong)1;
0774bed1 3425#endif
682674b8
RH
3426 syms[i].st_value += load_bias;
3427 i++;
3428 }
0774bed1 3429 }
49918a75 3430
b9475279
CV
3431 /* No "useful" symbol. */
3432 if (nsyms == 0) {
3433 goto give_up;
3434 }
3435
5d5c9930
RH
3436 /* Attempt to free the storage associated with the local symbols
3437 that we threw away. Whether or not this has any effect on the
3438 memory allocation depends on the malloc implementation and how
3439 many symbols we managed to discard. */
0ef9ea29 3440 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
8d79de6e 3441 if (new_syms == NULL) {
b9475279 3442 goto give_up;
5d5c9930 3443 }
8d79de6e 3444 syms = new_syms;
5d5c9930 3445
49918a75 3446 qsort(syms, nsyms, sizeof(*syms), symcmp);
689f936f 3447
49918a75
PB
3448 s->disas_num_syms = nsyms;
3449#if ELF_CLASS == ELFCLASS32
3450 s->disas_symtab.elf32 = syms;
49918a75
PB
3451#else
3452 s->disas_symtab.elf64 = syms;
49918a75 3453#endif
682674b8 3454 s->lookup_symbol = lookup_symbolxx;
e80cfcfc
FB
3455 s->next = syminfos;
3456 syminfos = s;
b9475279
CV
3457
3458 return;
3459
3460give_up:
0ef9ea29
PM
3461 g_free(s);
3462 g_free(strings);
3463 g_free(syms);
689f936f 3464}
31e31b8a 3465
768fe76e
YS
3466uint32_t get_elf_eflags(int fd)
3467{
3468 struct elfhdr ehdr;
3469 off_t offset;
3470 int ret;
3471
3472 /* Read ELF header */
3473 offset = lseek(fd, 0, SEEK_SET);
3474 if (offset == (off_t) -1) {
3475 return 0;
3476 }
3477 ret = read(fd, &ehdr, sizeof(ehdr));
3478 if (ret < sizeof(ehdr)) {
3479 return 0;
3480 }
3481 offset = lseek(fd, offset, SEEK_SET);
3482 if (offset == (off_t) -1) {
3483 return 0;
3484 }
3485
3486 /* Check ELF signature */
3487 if (!elf_check_ident(&ehdr)) {
3488 return 0;
3489 }
3490
3491 /* check header */
3492 bswap_ehdr(&ehdr);
3493 if (!elf_check_ehdr(&ehdr)) {
3494 return 0;
3495 }
3496
3497 /* return architecture id */
3498 return ehdr.e_flags;
3499}
3500
f0116c54 3501int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
31e31b8a 3502{
8e62a717 3503 struct image_info interp_info;
31e31b8a 3504 struct elfhdr elf_ex;
8e62a717 3505 char *elf_interpreter = NULL;
59baae9a 3506 char *scratch;
31e31b8a 3507
abcac736
DS
3508 memset(&interp_info, 0, sizeof(interp_info));
3509#ifdef TARGET_MIPS
3510 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3511#endif
3512
bf858897 3513 info->start_mmap = (abi_ulong)ELF_START_MMAP;
bf858897
RH
3514
3515 load_elf_image(bprm->filename, bprm->fd, info,
3516 &elf_interpreter, bprm->buf);
31e31b8a 3517
bf858897
RH
3518 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3519 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3520 when we load the interpreter. */
3521 elf_ex = *(struct elfhdr *)bprm->buf;
31e31b8a 3522
59baae9a
SB
3523 /* Do this so that we can load the interpreter, if need be. We will
3524 change some of these later */
3525 bprm->p = setup_arg_pages(bprm, info);
3526
3527 scratch = g_new0(char, TARGET_PAGE_SIZE);
7c4ee5bc
RH
3528 if (STACK_GROWS_DOWN) {
3529 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3530 bprm->p, info->stack_limit);
3531 info->file_string = bprm->p;
3532 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3533 bprm->p, info->stack_limit);
3534 info->env_strings = bprm->p;
3535 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3536 bprm->p, info->stack_limit);
3537 info->arg_strings = bprm->p;
3538 } else {
3539 info->arg_strings = bprm->p;
3540 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3541 bprm->p, info->stack_limit);
3542 info->env_strings = bprm->p;
3543 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3544 bprm->p, info->stack_limit);
3545 info->file_string = bprm->p;
3546 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3547 bprm->p, info->stack_limit);
3548 }
3549
59baae9a
SB
3550 g_free(scratch);
3551
e5fe0c52 3552 if (!bprm->p) {
bf858897
RH
3553 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3554 exit(-1);
379f6698 3555 }
379f6698 3556
8e62a717
RH
3557 if (elf_interpreter) {
3558 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
31e31b8a 3559
8e62a717
RH
3560 /* If the program interpreter is one of these two, then assume
3561 an iBCS2 image. Otherwise assume a native linux image. */
3562
3563 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3564 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3565 info->personality = PER_SVR4;
31e31b8a 3566
8e62a717
RH
3567 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3568 and some applications "depend" upon this behavior. Since
3569 we do not have the power to recompile these, we emulate
3570 the SVr4 behavior. Sigh. */
3571 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
68754b44 3572 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
8e62a717 3573 }
c94cb6c9
SM
3574#ifdef TARGET_MIPS
3575 info->interp_fp_abi = interp_info.fp_abi;
3576#endif
31e31b8a
FB
3577 }
3578
db2af69d
RH
3579 /*
3580 * TODO: load a vdso, which would also contain the signal trampolines.
3581 * Otherwise, allocate a private page to hold them.
3582 */
3583 if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) {
802ae45e
LV
3584 abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE,
3585 PROT_READ | PROT_WRITE,
3586 MAP_PRIVATE | MAP_ANON, -1, 0);
3587 if (tramp_page == -1) {
3588 return -errno;
3589 }
3590
db2af69d
RH
3591 setup_sigtramp(tramp_page);
3592 target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC);
3593 }
3594
8e62a717
RH
3595 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
3596 info, (elf_interpreter ? &interp_info : NULL));
3597 info->start_stack = bprm->p;
3598
3599 /* If we have an interpreter, set that as the program's entry point.
8e78064e 3600 Copy the load_bias as well, to help PPC64 interpret the entry
8e62a717
RH
3601 point as a function descriptor. Do this after creating elf tables
3602 so that we copy the original program entry point into the AUXV. */
3603 if (elf_interpreter) {
8e78064e 3604 info->load_bias = interp_info.load_bias;
8e62a717 3605 info->entry = interp_info.entry;
2b323087 3606 g_free(elf_interpreter);
8e62a717 3607 }
31e31b8a 3608
edf8e2af
MW
3609#ifdef USE_ELF_CORE_DUMP
3610 bprm->core_dump = &elf_core_dump;
3611#endif
3612
6fd59449
RH
3613 /*
3614 * If we reserved extra space for brk, release it now.
3615 * The implementation of do_brk in syscalls.c expects to be able
3616 * to mmap pages in this space.
3617 */
3618 if (info->reserve_brk) {
3619 abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk);
3620 abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk);
3621 target_munmap(start_brk, end_brk - start_brk);
3622 }
3623
31e31b8a
FB
3624 return 0;
3625}
3626
edf8e2af 3627#ifdef USE_ELF_CORE_DUMP
edf8e2af
MW
3628/*
3629 * Definitions to generate Intel SVR4-like core files.
a2547a13 3630 * These mostly have the same names as the SVR4 types with "target_elf_"
edf8e2af
MW
3631 * tacked on the front to prevent clashes with linux definitions,
3632 * and the typedef forms have been avoided. This is mostly like
3633 * the SVR4 structure, but more Linuxy, with things that Linux does
3634 * not support and which gdb doesn't really use excluded.
3635 *
3636 * Fields we don't dump (their contents is zero) in linux-user qemu
3637 * are marked with XXX.
3638 *
3639 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3640 *
3641 * Porting ELF coredump for target is (quite) simple process. First you
dd0a3651 3642 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
edf8e2af
MW
3643 * the target resides):
3644 *
3645 * #define USE_ELF_CORE_DUMP
3646 *
3647 * Next you define type of register set used for dumping. ELF specification
3648 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3649 *
c227f099 3650 * typedef <target_regtype> target_elf_greg_t;
edf8e2af 3651 * #define ELF_NREG <number of registers>
c227f099 3652 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
edf8e2af 3653 *
edf8e2af
MW
3654 * Last step is to implement target specific function that copies registers
3655 * from given cpu into just specified register set. Prototype is:
3656 *
c227f099 3657 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
9349b4f9 3658 * const CPUArchState *env);
edf8e2af
MW
3659 *
3660 * Parameters:
3661 * regs - copy register values into here (allocated and zeroed by caller)
3662 * env - copy registers from here
3663 *
3664 * Example for ARM target is provided in this file.
3665 */
3666
3667/* An ELF note in memory */
3668struct memelfnote {
3669 const char *name;
3670 size_t namesz;
3671 size_t namesz_rounded;
3672 int type;
3673 size_t datasz;
80f5ce75 3674 size_t datasz_rounded;
edf8e2af
MW
3675 void *data;
3676 size_t notesz;
3677};
3678
a2547a13 3679struct target_elf_siginfo {
f8fd4fc4
PB
3680 abi_int si_signo; /* signal number */
3681 abi_int si_code; /* extra code */
3682 abi_int si_errno; /* errno */
edf8e2af
MW
3683};
3684
a2547a13
LD
3685struct target_elf_prstatus {
3686 struct target_elf_siginfo pr_info; /* Info associated with signal */
1ddd592f 3687 abi_short pr_cursig; /* Current signal */
ca98ac83
PB
3688 abi_ulong pr_sigpend; /* XXX */
3689 abi_ulong pr_sighold; /* XXX */
c227f099
AL
3690 target_pid_t pr_pid;
3691 target_pid_t pr_ppid;
3692 target_pid_t pr_pgrp;
3693 target_pid_t pr_sid;
edf8e2af
MW
3694 struct target_timeval pr_utime; /* XXX User time */
3695 struct target_timeval pr_stime; /* XXX System time */
3696 struct target_timeval pr_cutime; /* XXX Cumulative user time */
3697 struct target_timeval pr_cstime; /* XXX Cumulative system time */
c227f099 3698 target_elf_gregset_t pr_reg; /* GP registers */
f8fd4fc4 3699 abi_int pr_fpvalid; /* XXX */
edf8e2af
MW
3700};
3701
3702#define ELF_PRARGSZ (80) /* Number of chars for args */
3703
a2547a13 3704struct target_elf_prpsinfo {
edf8e2af
MW
3705 char pr_state; /* numeric process state */
3706 char pr_sname; /* char for pr_state */
3707 char pr_zomb; /* zombie */
3708 char pr_nice; /* nice val */
ca98ac83 3709 abi_ulong pr_flag; /* flags */
c227f099
AL
3710 target_uid_t pr_uid;
3711 target_gid_t pr_gid;
3712 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
edf8e2af 3713 /* Lots missing */
d7eb2b92 3714 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
edf8e2af
MW
3715 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
3716};
3717
3718/* Here is the structure in which status of each thread is captured. */
3719struct elf_thread_status {
72cf2d4f 3720 QTAILQ_ENTRY(elf_thread_status) ets_link;
a2547a13 3721 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
edf8e2af
MW
3722#if 0
3723 elf_fpregset_t fpu; /* NT_PRFPREG */
3724 struct task_struct *thread;
3725 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
3726#endif
3727 struct memelfnote notes[1];
3728 int num_notes;
3729};
3730
3731struct elf_note_info {
3732 struct memelfnote *notes;
a2547a13
LD
3733 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
3734 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
edf8e2af 3735
b58deb34 3736 QTAILQ_HEAD(, elf_thread_status) thread_list;
edf8e2af
MW
3737#if 0
3738 /*
3739 * Current version of ELF coredump doesn't support
3740 * dumping fp regs etc.
3741 */
3742 elf_fpregset_t *fpu;
3743 elf_fpxregset_t *xfpu;
3744 int thread_status_size;
3745#endif
3746 int notes_size;
3747 int numnote;
3748};
3749
3750struct vm_area_struct {
1a1c4db9
MI
3751 target_ulong vma_start; /* start vaddr of memory region */
3752 target_ulong vma_end; /* end vaddr of memory region */
3753 abi_ulong vma_flags; /* protection etc. flags for the region */
72cf2d4f 3754 QTAILQ_ENTRY(vm_area_struct) vma_link;
edf8e2af
MW
3755};
3756
3757struct mm_struct {
72cf2d4f 3758 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
edf8e2af
MW
3759 int mm_count; /* number of mappings */
3760};
3761
3762static struct mm_struct *vma_init(void);
3763static void vma_delete(struct mm_struct *);
1a1c4db9
MI
3764static int vma_add_mapping(struct mm_struct *, target_ulong,
3765 target_ulong, abi_ulong);
edf8e2af
MW
3766static int vma_get_mapping_count(const struct mm_struct *);
3767static struct vm_area_struct *vma_first(const struct mm_struct *);
3768static struct vm_area_struct *vma_next(struct vm_area_struct *);
3769static abi_ulong vma_dump_size(const struct vm_area_struct *);
1a1c4db9 3770static int vma_walker(void *priv, target_ulong start, target_ulong end,
d97ef72e 3771 unsigned long flags);
edf8e2af
MW
3772
3773static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
3774static void fill_note(struct memelfnote *, const char *, int,
d97ef72e 3775 unsigned int, void *);
a2547a13
LD
3776static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
3777static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
edf8e2af
MW
3778static void fill_auxv_note(struct memelfnote *, const TaskState *);
3779static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
3780static size_t note_size(const struct memelfnote *);
3781static void free_note_info(struct elf_note_info *);
9349b4f9
AF
3782static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
3783static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
edf8e2af
MW
3784
3785static int dump_write(int, const void *, size_t);
3786static int write_note(struct memelfnote *, int);
3787static int write_note_info(struct elf_note_info *, int);
3788
3789#ifdef BSWAP_NEEDED
a2547a13 3790static void bswap_prstatus(struct target_elf_prstatus *prstatus)
edf8e2af 3791{
ca98ac83
PB
3792 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3793 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3794 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
edf8e2af 3795 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
ca98ac83
PB
3796 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3797 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
edf8e2af
MW
3798 prstatus->pr_pid = tswap32(prstatus->pr_pid);
3799 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3800 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3801 prstatus->pr_sid = tswap32(prstatus->pr_sid);
3802 /* cpu times are not filled, so we skip them */
3803 /* regs should be in correct format already */
3804 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3805}
3806
a2547a13 3807static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
edf8e2af 3808{
ca98ac83 3809 psinfo->pr_flag = tswapal(psinfo->pr_flag);
edf8e2af
MW
3810 psinfo->pr_uid = tswap16(psinfo->pr_uid);
3811 psinfo->pr_gid = tswap16(psinfo->pr_gid);
3812 psinfo->pr_pid = tswap32(psinfo->pr_pid);
3813 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
3814 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
3815 psinfo->pr_sid = tswap32(psinfo->pr_sid);
3816}
991f8f0c
RH
3817
3818static void bswap_note(struct elf_note *en)
3819{
3820 bswap32s(&en->n_namesz);
3821 bswap32s(&en->n_descsz);
3822 bswap32s(&en->n_type);
3823}
3824#else
3825static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
3826static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
3827static inline void bswap_note(struct elf_note *en) { }
edf8e2af
MW
3828#endif /* BSWAP_NEEDED */
3829
3830/*
3831 * Minimal support for linux memory regions. These are needed
3832 * when we are finding out what memory exactly belongs to
3833 * emulated process. No locks needed here, as long as
3834 * thread that received the signal is stopped.
3835 */
3836
3837static struct mm_struct *vma_init(void)
3838{
3839 struct mm_struct *mm;
3840
7267c094 3841 if ((mm = g_malloc(sizeof (*mm))) == NULL)
edf8e2af
MW
3842 return (NULL);
3843
3844 mm->mm_count = 0;
72cf2d4f 3845 QTAILQ_INIT(&mm->mm_mmap);
edf8e2af
MW
3846
3847 return (mm);
3848}
3849
3850static void vma_delete(struct mm_struct *mm)
3851{
3852 struct vm_area_struct *vma;
3853
3854 while ((vma = vma_first(mm)) != NULL) {
72cf2d4f 3855 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
7267c094 3856 g_free(vma);
edf8e2af 3857 }
7267c094 3858 g_free(mm);
edf8e2af
MW
3859}
3860
1a1c4db9
MI
3861static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3862 target_ulong end, abi_ulong flags)
edf8e2af
MW
3863{
3864 struct vm_area_struct *vma;
3865
7267c094 3866 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
edf8e2af
MW
3867 return (-1);
3868
3869 vma->vma_start = start;
3870 vma->vma_end = end;
3871 vma->vma_flags = flags;
3872
72cf2d4f 3873 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
edf8e2af
MW
3874 mm->mm_count++;
3875
3876 return (0);
3877}
3878
3879static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3880{
72cf2d4f 3881 return (QTAILQ_FIRST(&mm->mm_mmap));
edf8e2af
MW
3882}
3883
3884static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3885{
72cf2d4f 3886 return (QTAILQ_NEXT(vma, vma_link));
edf8e2af
MW
3887}
3888
3889static int vma_get_mapping_count(const struct mm_struct *mm)
3890{
3891 return (mm->mm_count);
3892}
3893
3894/*
3895 * Calculate file (dump) size of given memory region.
3896 */
3897static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3898{
3899 /* if we cannot even read the first page, skip it */
c7169b02 3900 if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
edf8e2af
MW
3901 return (0);
3902
3903 /*
3904 * Usually we don't dump executable pages as they contain
3905 * non-writable code that debugger can read directly from
3906 * target library etc. However, thread stacks are marked
3907 * also executable so we read in first page of given region
3908 * and check whether it contains elf header. If there is
3909 * no elf header, we dump it.
3910 */
3911 if (vma->vma_flags & PROT_EXEC) {
3912 char page[TARGET_PAGE_SIZE];
3913
022625a8
PM
3914 if (copy_from_user(page, vma->vma_start, sizeof (page))) {
3915 return 0;
3916 }
edf8e2af
MW
3917 if ((page[EI_MAG0] == ELFMAG0) &&
3918 (page[EI_MAG1] == ELFMAG1) &&
3919 (page[EI_MAG2] == ELFMAG2) &&
3920 (page[EI_MAG3] == ELFMAG3)) {
3921 /*
3922 * Mappings are possibly from ELF binary. Don't dump
3923 * them.
3924 */
3925 return (0);
3926 }
3927 }
3928
3929 return (vma->vma_end - vma->vma_start);
3930}
3931
1a1c4db9 3932static int vma_walker(void *priv, target_ulong start, target_ulong end,
d97ef72e 3933 unsigned long flags)
edf8e2af
MW
3934{
3935 struct mm_struct *mm = (struct mm_struct *)priv;
3936
edf8e2af
MW
3937 vma_add_mapping(mm, start, end, flags);
3938 return (0);
3939}
3940
3941static void fill_note(struct memelfnote *note, const char *name, int type,
d97ef72e 3942 unsigned int sz, void *data)
edf8e2af
MW
3943{
3944 unsigned int namesz;
3945
3946 namesz = strlen(name) + 1;
3947 note->name = name;
3948 note->namesz = namesz;
3949 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3950 note->type = type;
80f5ce75
LV
3951 note->datasz = sz;
3952 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3953
edf8e2af
MW
3954 note->data = data;
3955
3956 /*
3957 * We calculate rounded up note size here as specified by
3958 * ELF document.
3959 */
3960 note->notesz = sizeof (struct elf_note) +
80f5ce75 3961 note->namesz_rounded + note->datasz_rounded;
edf8e2af
MW
3962}
3963
3964static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
d97ef72e 3965 uint32_t flags)
edf8e2af
MW
3966{
3967 (void) memset(elf, 0, sizeof(*elf));
3968
3969 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
3970 elf->e_ident[EI_CLASS] = ELF_CLASS;
3971 elf->e_ident[EI_DATA] = ELF_DATA;
3972 elf->e_ident[EI_VERSION] = EV_CURRENT;
3973 elf->e_ident[EI_OSABI] = ELF_OSABI;
3974
3975 elf->e_type = ET_CORE;
3976 elf->e_machine = machine;
3977 elf->e_version = EV_CURRENT;
3978 elf->e_phoff = sizeof(struct elfhdr);
3979 elf->e_flags = flags;
3980 elf->e_ehsize = sizeof(struct elfhdr);
3981 elf->e_phentsize = sizeof(struct elf_phdr);
3982 elf->e_phnum = segs;
3983
edf8e2af 3984 bswap_ehdr(elf);
edf8e2af
MW
3985}
3986
3987static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
3988{
3989 phdr->p_type = PT_NOTE;
3990 phdr->p_offset = offset;
3991 phdr->p_vaddr = 0;
3992 phdr->p_paddr = 0;
3993 phdr->p_filesz = sz;
3994 phdr->p_memsz = 0;
3995 phdr->p_flags = 0;
3996 phdr->p_align = 0;
3997
991f8f0c 3998 bswap_phdr(phdr, 1);
edf8e2af
MW
3999}
4000
4001static size_t note_size(const struct memelfnote *note)
4002{
4003 return (note->notesz);
4004}
4005
a2547a13 4006static void fill_prstatus(struct target_elf_prstatus *prstatus,
d97ef72e 4007 const TaskState *ts, int signr)
edf8e2af
MW
4008{
4009 (void) memset(prstatus, 0, sizeof (*prstatus));
4010 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
4011 prstatus->pr_pid = ts->ts_tid;
4012 prstatus->pr_ppid = getppid();
4013 prstatus->pr_pgrp = getpgrp();
4014 prstatus->pr_sid = getsid(0);
4015
edf8e2af 4016 bswap_prstatus(prstatus);
edf8e2af
MW
4017}
4018
a2547a13 4019static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
edf8e2af 4020{
900cfbca 4021 char *base_filename;
edf8e2af
MW
4022 unsigned int i, len;
4023
4024 (void) memset(psinfo, 0, sizeof (*psinfo));
4025
5f779a3a 4026 len = ts->info->env_strings - ts->info->arg_strings;
edf8e2af
MW
4027 if (len >= ELF_PRARGSZ)
4028 len = ELF_PRARGSZ - 1;
5f779a3a 4029 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) {
edf8e2af 4030 return -EFAULT;
5f779a3a 4031 }
edf8e2af
MW
4032 for (i = 0; i < len; i++)
4033 if (psinfo->pr_psargs[i] == 0)
4034 psinfo->pr_psargs[i] = ' ';
4035 psinfo->pr_psargs[len] = 0;
4036
4037 psinfo->pr_pid = getpid();
4038 psinfo->pr_ppid = getppid();
4039 psinfo->pr_pgrp = getpgrp();
4040 psinfo->pr_sid = getsid(0);
4041 psinfo->pr_uid = getuid();
4042 psinfo->pr_gid = getgid();
4043
900cfbca
JM
4044 base_filename = g_path_get_basename(ts->bprm->filename);
4045 /*
4046 * Using strncpy here is fine: at max-length,
4047 * this field is not NUL-terminated.
4048 */
edf8e2af 4049 (void) strncpy(psinfo->pr_fname, base_filename,
d97ef72e 4050 sizeof(psinfo->pr_fname));
edf8e2af 4051
900cfbca 4052 g_free(base_filename);
edf8e2af 4053 bswap_psinfo(psinfo);
edf8e2af
MW
4054 return (0);
4055}
4056
4057static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
4058{
4059 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
4060 elf_addr_t orig_auxv = auxv;
edf8e2af 4061 void *ptr;
125b0f55 4062 int len = ts->info->auxv_len;
edf8e2af
MW
4063
4064 /*
4065 * Auxiliary vector is stored in target process stack. It contains
4066 * {type, value} pairs that we need to dump into note. This is not
4067 * strictly necessary but we do it here for sake of completeness.
4068 */
4069
edf8e2af
MW
4070 /* read in whole auxv vector and copy it to memelfnote */
4071 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
4072 if (ptr != NULL) {
4073 fill_note(note, "CORE", NT_AUXV, len, ptr);
4074 unlock_user(ptr, auxv, len);
4075 }
4076}
4077
4078/*
4079 * Constructs name of coredump file. We have following convention
4080 * for the name:
4081 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4082 *
68af19ad 4083 * Returns the filename
edf8e2af 4084 */
68af19ad 4085static char *core_dump_filename(const TaskState *ts)
edf8e2af 4086{
68af19ad
DB
4087 g_autoptr(GDateTime) now = g_date_time_new_now_local();
4088 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
4089 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
edf8e2af 4090
68af19ad
DB
4091 return g_strdup_printf("qemu_%s_%s_%d.core",
4092 base_filename, nowstr, (int)getpid());
edf8e2af
MW
4093}
4094
4095static int dump_write(int fd, const void *ptr, size_t size)
4096{
4097 const char *bufp = (const char *)ptr;
4098 ssize_t bytes_written, bytes_left;
4099 struct rlimit dumpsize;
4100 off_t pos;
4101
4102 bytes_written = 0;
4103 getrlimit(RLIMIT_CORE, &dumpsize);
4104 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
4105 if (errno == ESPIPE) { /* not a seekable stream */
4106 bytes_left = size;
4107 } else {
4108 return pos;
4109 }
4110 } else {
4111 if (dumpsize.rlim_cur <= pos) {
4112 return -1;
4113 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
4114 bytes_left = size;
4115 } else {
4116 size_t limit_left=dumpsize.rlim_cur - pos;
4117 bytes_left = limit_left >= size ? size : limit_left ;
4118 }
4119 }
4120
4121 /*
4122 * In normal conditions, single write(2) should do but
4123 * in case of socket etc. this mechanism is more portable.
4124 */
4125 do {
4126 bytes_written = write(fd, bufp, bytes_left);
4127 if (bytes_written < 0) {
4128 if (errno == EINTR)
4129 continue;
4130 return (-1);
4131 } else if (bytes_written == 0) { /* eof */
4132 return (-1);
4133 }
4134 bufp += bytes_written;
4135 bytes_left -= bytes_written;
4136 } while (bytes_left > 0);
4137
4138 return (0);
4139}
4140
4141static int write_note(struct memelfnote *men, int fd)
4142{
4143 struct elf_note en;
4144
4145 en.n_namesz = men->namesz;
4146 en.n_type = men->type;
4147 en.n_descsz = men->datasz;
4148
edf8e2af 4149 bswap_note(&en);
edf8e2af
MW
4150
4151 if (dump_write(fd, &en, sizeof(en)) != 0)
4152 return (-1);
4153 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
4154 return (-1);
80f5ce75 4155 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
edf8e2af
MW
4156 return (-1);
4157
4158 return (0);
4159}
4160
9349b4f9 4161static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
edf8e2af 4162{
29a0af61 4163 CPUState *cpu = env_cpu((CPUArchState *)env);
0429a971 4164 TaskState *ts = (TaskState *)cpu->opaque;
edf8e2af
MW
4165 struct elf_thread_status *ets;
4166
7267c094 4167 ets = g_malloc0(sizeof (*ets));
edf8e2af
MW
4168 ets->num_notes = 1; /* only prstatus is dumped */
4169 fill_prstatus(&ets->prstatus, ts, 0);
4170 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
4171 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
d97ef72e 4172 &ets->prstatus);
edf8e2af 4173
72cf2d4f 4174 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
edf8e2af
MW
4175
4176 info->notes_size += note_size(&ets->notes[0]);
4177}
4178
6afafa86
PM
4179static void init_note_info(struct elf_note_info *info)
4180{
4181 /* Initialize the elf_note_info structure so that it is at
4182 * least safe to call free_note_info() on it. Must be
4183 * called before calling fill_note_info().
4184 */
4185 memset(info, 0, sizeof (*info));
4186 QTAILQ_INIT(&info->thread_list);
4187}
4188
edf8e2af 4189static int fill_note_info(struct elf_note_info *info,
9349b4f9 4190 long signr, const CPUArchState *env)
edf8e2af
MW
4191{
4192#define NUMNOTES 3
29a0af61 4193 CPUState *cpu = env_cpu((CPUArchState *)env);
0429a971 4194 TaskState *ts = (TaskState *)cpu->opaque;
edf8e2af
MW
4195 int i;
4196
c78d65e8 4197 info->notes = g_new0(struct memelfnote, NUMNOTES);
edf8e2af
MW
4198 if (info->notes == NULL)
4199 return (-ENOMEM);
7267c094 4200 info->prstatus = g_malloc0(sizeof (*info->prstatus));
edf8e2af
MW
4201 if (info->prstatus == NULL)
4202 return (-ENOMEM);
7267c094 4203 info->psinfo = g_malloc0(sizeof (*info->psinfo));
edf8e2af
MW
4204 if (info->prstatus == NULL)
4205 return (-ENOMEM);
4206
4207 /*
4208 * First fill in status (and registers) of current thread
4209 * including process info & aux vector.
4210 */
4211 fill_prstatus(info->prstatus, ts, signr);
4212 elf_core_copy_regs(&info->prstatus->pr_reg, env);
4213 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
d97ef72e 4214 sizeof (*info->prstatus), info->prstatus);
edf8e2af
MW
4215 fill_psinfo(info->psinfo, ts);
4216 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
d97ef72e 4217 sizeof (*info->psinfo), info->psinfo);
edf8e2af
MW
4218 fill_auxv_note(&info->notes[2], ts);
4219 info->numnote = 3;
4220
4221 info->notes_size = 0;
4222 for (i = 0; i < info->numnote; i++)
4223 info->notes_size += note_size(&info->notes[i]);
4224
4225 /* read and fill status of all threads */
4226 cpu_list_lock();
bdc44640 4227 CPU_FOREACH(cpu) {
a2247f8e 4228 if (cpu == thread_cpu) {
edf8e2af 4229 continue;
182735ef 4230 }
2f6f4290 4231 fill_thread_info(info, cpu->env_ptr);
edf8e2af
MW
4232 }
4233 cpu_list_unlock();
4234
4235 return (0);
4236}
4237
4238static void free_note_info(struct elf_note_info *info)
4239{
4240 struct elf_thread_status *ets;
4241
72cf2d4f
BS
4242 while (!QTAILQ_EMPTY(&info->thread_list)) {
4243 ets = QTAILQ_FIRST(&info->thread_list);
4244 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
7267c094 4245 g_free(ets);
edf8e2af
MW
4246 }
4247
7267c094
AL
4248 g_free(info->prstatus);
4249 g_free(info->psinfo);
4250 g_free(info->notes);
edf8e2af
MW
4251}
4252
4253static int write_note_info(struct elf_note_info *info, int fd)
4254{
4255 struct elf_thread_status *ets;
4256 int i, error = 0;
4257
4258 /* write prstatus, psinfo and auxv for current thread */
4259 for (i = 0; i < info->numnote; i++)
4260 if ((error = write_note(&info->notes[i], fd)) != 0)
4261 return (error);
4262
4263 /* write prstatus for each thread */
52a53afe 4264 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
edf8e2af
MW
4265 if ((error = write_note(&ets->notes[0], fd)) != 0)
4266 return (error);
4267 }
4268
4269 return (0);
4270}
4271
4272/*
4273 * Write out ELF coredump.
4274 *
4275 * See documentation of ELF object file format in:
4276 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4277 *
4278 * Coredump format in linux is following:
4279 *
4280 * 0 +----------------------+ \
4281 * | ELF header | ET_CORE |
4282 * +----------------------+ |
4283 * | ELF program headers | |--- headers
4284 * | - NOTE section | |
4285 * | - PT_LOAD sections | |
4286 * +----------------------+ /
4287 * | NOTEs: |
4288 * | - NT_PRSTATUS |
4289 * | - NT_PRSINFO |
4290 * | - NT_AUXV |
4291 * +----------------------+ <-- aligned to target page
4292 * | Process memory dump |
4293 * : :
4294 * . .
4295 * : :
4296 * | |
4297 * +----------------------+
4298 *
4299 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4300 * NT_PRSINFO -> struct elf_prpsinfo
4301 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4302 *
4303 * Format follows System V format as close as possible. Current
4304 * version limitations are as follows:
4305 * - no floating point registers are dumped
4306 *
4307 * Function returns 0 in case of success, negative errno otherwise.
4308 *
4309 * TODO: make this work also during runtime: it should be
4310 * possible to force coredump from running process and then
4311 * continue processing. For example qemu could set up SIGUSR2
4312 * handler (provided that target process haven't registered
4313 * handler for that) that does the dump when signal is received.
4314 */
9349b4f9 4315static int elf_core_dump(int signr, const CPUArchState *env)
edf8e2af 4316{
29a0af61 4317 const CPUState *cpu = env_cpu((CPUArchState *)env);
0429a971 4318 const TaskState *ts = (const TaskState *)cpu->opaque;
edf8e2af 4319 struct vm_area_struct *vma = NULL;
68af19ad 4320 g_autofree char *corefile = NULL;
edf8e2af
MW
4321 struct elf_note_info info;
4322 struct elfhdr elf;
4323 struct elf_phdr phdr;
4324 struct rlimit dumpsize;
4325 struct mm_struct *mm = NULL;
4326 off_t offset = 0, data_offset = 0;
4327 int segs = 0;
4328 int fd = -1;
4329
6afafa86
PM
4330 init_note_info(&info);
4331
edf8e2af
MW
4332 errno = 0;
4333 getrlimit(RLIMIT_CORE, &dumpsize);
4334 if (dumpsize.rlim_cur == 0)
d97ef72e 4335 return 0;
edf8e2af 4336
68af19ad 4337 corefile = core_dump_filename(ts);
edf8e2af
MW
4338
4339 if ((fd = open(corefile, O_WRONLY | O_CREAT,
d97ef72e 4340 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
edf8e2af
MW
4341 return (-errno);
4342
4343 /*
4344 * Walk through target process memory mappings and
4345 * set up structure containing this information. After
4346 * this point vma_xxx functions can be used.
4347 */
4348 if ((mm = vma_init()) == NULL)
4349 goto out;
4350
4351 walk_memory_regions(mm, vma_walker);
4352 segs = vma_get_mapping_count(mm);
4353
4354 /*
4355 * Construct valid coredump ELF header. We also
4356 * add one more segment for notes.
4357 */
4358 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
4359 if (dump_write(fd, &elf, sizeof (elf)) != 0)
4360 goto out;
4361
b6af0975 4362 /* fill in the in-memory version of notes */
edf8e2af
MW
4363 if (fill_note_info(&info, signr, env) < 0)
4364 goto out;
4365
4366 offset += sizeof (elf); /* elf header */
4367 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
4368
4369 /* write out notes program header */
4370 fill_elf_note_phdr(&phdr, info.notes_size, offset);
4371
4372 offset += info.notes_size;
4373 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
4374 goto out;
4375
4376 /*
4377 * ELF specification wants data to start at page boundary so
4378 * we align it here.
4379 */
80f5ce75 4380 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
edf8e2af
MW
4381
4382 /*
4383 * Write program headers for memory regions mapped in
4384 * the target process.
4385 */
4386 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4387 (void) memset(&phdr, 0, sizeof (phdr));
4388
4389 phdr.p_type = PT_LOAD;
4390 phdr.p_offset = offset;
4391 phdr.p_vaddr = vma->vma_start;
4392 phdr.p_paddr = 0;
4393 phdr.p_filesz = vma_dump_size(vma);
4394 offset += phdr.p_filesz;
4395 phdr.p_memsz = vma->vma_end - vma->vma_start;
4396 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
4397 if (vma->vma_flags & PROT_WRITE)
4398 phdr.p_flags |= PF_W;
4399 if (vma->vma_flags & PROT_EXEC)
4400 phdr.p_flags |= PF_X;
4401 phdr.p_align = ELF_EXEC_PAGESIZE;
4402
80f5ce75 4403 bswap_phdr(&phdr, 1);
772034b6
PM
4404 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
4405 goto out;
4406 }
edf8e2af
MW
4407 }
4408
4409 /*
4410 * Next we write notes just after program headers. No
4411 * alignment needed here.
4412 */
4413 if (write_note_info(&info, fd) < 0)
4414 goto out;
4415
4416 /* align data to page boundary */
edf8e2af
MW
4417 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4418 goto out;
4419
4420 /*
4421 * Finally we can dump process memory into corefile as well.
4422 */
4423 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4424 abi_ulong addr;
4425 abi_ulong end;
4426
4427 end = vma->vma_start + vma_dump_size(vma);
4428
4429 for (addr = vma->vma_start; addr < end;
d97ef72e 4430 addr += TARGET_PAGE_SIZE) {
edf8e2af
MW
4431 char page[TARGET_PAGE_SIZE];
4432 int error;
4433
4434 /*
4435 * Read in page from target process memory and
4436 * write it to coredump file.
4437 */
4438 error = copy_from_user(page, addr, sizeof (page));
4439 if (error != 0) {
49995e17 4440 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
d97ef72e 4441 addr);
edf8e2af
MW
4442 errno = -error;
4443 goto out;
4444 }
4445 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4446 goto out;
4447 }
4448 }
4449
d97ef72e 4450 out:
edf8e2af
MW
4451 free_note_info(&info);
4452 if (mm != NULL)
4453 vma_delete(mm);
4454 (void) close(fd);
4455
4456 if (errno != 0)
4457 return (-errno);
4458 return (0);
4459}
edf8e2af
MW
4460#endif /* USE_ELF_CORE_DUMP */
4461
e5fe0c52
PB
4462void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4463{
4464 init_thread(regs, infop);
4465}