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