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