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