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