]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/elfload.c
Merge remote-tracking branch 'remotes/berrange-gitlab/tags/misc-next-pull-request...
[mirror_qemu.git] / linux-user / elfload.c
CommitLineData
31e31b8a 1/* This is the Linux kernel elf-loading code, ported into user space */
d39594e9 2#include "qemu/osdep.h"
edf8e2af 3#include <sys/param.h>
31e31b8a 4
edf8e2af 5#include <sys/resource.h>
30ab9ef2 6#include <sys/shm.h>
31e31b8a 7
3ef693a0 8#include "qemu.h"
76cad711 9#include "disas/disas.h"
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
992static uint32_t get_elf_hwcap(void)
993{
994 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
995 uint32_t hwcaps = 0;
996
997#define GET_FEATURE(flag, hwcap) \
998 do { if (cpu->env.insn_flags & (flag)) { hwcaps |= hwcap; } } while (0)
999
1000 GET_FEATURE(ISA_MIPS32R6 | ISA_MIPS64R6, HWCAP_MIPS_R6);
1001 GET_FEATURE(ASE_MSA, HWCAP_MIPS_MSA);
1002
1003#undef GET_FEATURE
1004
1005 return hwcaps;
1006}
1007
048f6b4d
FB
1008#endif /* TARGET_MIPS */
1009
b779e29e
EI
1010#ifdef TARGET_MICROBLAZE
1011
1012#define ELF_START_MMAP 0x80000000
1013
0d5d4699 1014#define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
b779e29e
EI
1015
1016#define ELF_CLASS ELFCLASS32
0d5d4699 1017#define ELF_ARCH EM_MICROBLAZE
b779e29e 1018
d97ef72e
RH
1019static inline void init_thread(struct target_pt_regs *regs,
1020 struct image_info *infop)
b779e29e
EI
1021{
1022 regs->pc = infop->entry;
1023 regs->r1 = infop->start_stack;
1024
1025}
1026
b779e29e
EI
1027#define ELF_EXEC_PAGESIZE 4096
1028
e4cbd44d
EI
1029#define USE_ELF_CORE_DUMP
1030#define ELF_NREG 38
1031typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1032
1033/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
05390248 1034static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
e4cbd44d
EI
1035{
1036 int i, pos = 0;
1037
1038 for (i = 0; i < 32; i++) {
86cd7b2d 1039 (*regs)[pos++] = tswapreg(env->regs[i]);
e4cbd44d
EI
1040 }
1041
af20a93a 1042 (*regs)[pos++] = tswapreg(env->pc);
1074c0fb 1043 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
af20a93a
RH
1044 (*regs)[pos++] = 0;
1045 (*regs)[pos++] = tswapreg(env->ear);
1046 (*regs)[pos++] = 0;
1047 (*regs)[pos++] = tswapreg(env->esr);
e4cbd44d
EI
1048}
1049
b779e29e
EI
1050#endif /* TARGET_MICROBLAZE */
1051
a0a839b6
MV
1052#ifdef TARGET_NIOS2
1053
1054#define ELF_START_MMAP 0x80000000
1055
1056#define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1057
1058#define ELF_CLASS ELFCLASS32
1059#define ELF_ARCH EM_ALTERA_NIOS2
1060
1061static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1062{
1063 regs->ea = infop->entry;
1064 regs->sp = infop->start_stack;
1065 regs->estatus = 0x3;
1066}
1067
1068#define ELF_EXEC_PAGESIZE 4096
1069
1070#define USE_ELF_CORE_DUMP
1071#define ELF_NREG 49
1072typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1073
1074/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1075static void elf_core_copy_regs(target_elf_gregset_t *regs,
1076 const CPUNios2State *env)
1077{
1078 int i;
1079
1080 (*regs)[0] = -1;
1081 for (i = 1; i < 8; i++) /* r0-r7 */
1082 (*regs)[i] = tswapreg(env->regs[i + 7]);
1083
1084 for (i = 8; i < 16; i++) /* r8-r15 */
1085 (*regs)[i] = tswapreg(env->regs[i - 8]);
1086
1087 for (i = 16; i < 24; i++) /* r16-r23 */
1088 (*regs)[i] = tswapreg(env->regs[i + 7]);
1089 (*regs)[24] = -1; /* R_ET */
1090 (*regs)[25] = -1; /* R_BT */
1091 (*regs)[26] = tswapreg(env->regs[R_GP]);
1092 (*regs)[27] = tswapreg(env->regs[R_SP]);
1093 (*regs)[28] = tswapreg(env->regs[R_FP]);
1094 (*regs)[29] = tswapreg(env->regs[R_EA]);
1095 (*regs)[30] = -1; /* R_SSTATUS */
1096 (*regs)[31] = tswapreg(env->regs[R_RA]);
1097
1098 (*regs)[32] = tswapreg(env->regs[R_PC]);
1099
1100 (*regs)[33] = -1; /* R_STATUS */
1101 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1102
1103 for (i = 35; i < 49; i++) /* ... */
1104 (*regs)[i] = -1;
1105}
1106
1107#endif /* TARGET_NIOS2 */
1108
d962783e
JL
1109#ifdef TARGET_OPENRISC
1110
1111#define ELF_START_MMAP 0x08000000
1112
d962783e
JL
1113#define ELF_ARCH EM_OPENRISC
1114#define ELF_CLASS ELFCLASS32
1115#define ELF_DATA ELFDATA2MSB
1116
1117static inline void init_thread(struct target_pt_regs *regs,
1118 struct image_info *infop)
1119{
1120 regs->pc = infop->entry;
1121 regs->gpr[1] = infop->start_stack;
1122}
1123
1124#define USE_ELF_CORE_DUMP
1125#define ELF_EXEC_PAGESIZE 8192
1126
1127/* See linux kernel arch/openrisc/include/asm/elf.h. */
1128#define ELF_NREG 34 /* gprs and pc, sr */
1129typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1130
1131static void elf_core_copy_regs(target_elf_gregset_t *regs,
1132 const CPUOpenRISCState *env)
1133{
1134 int i;
1135
1136 for (i = 0; i < 32; i++) {
d89e71e8 1137 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
d962783e 1138 }
86cd7b2d 1139 (*regs)[32] = tswapreg(env->pc);
84775c43 1140 (*regs)[33] = tswapreg(cpu_get_sr(env));
d962783e
JL
1141}
1142#define ELF_HWCAP 0
1143#define ELF_PLATFORM NULL
1144
1145#endif /* TARGET_OPENRISC */
1146
fdf9b3e8
FB
1147#ifdef TARGET_SH4
1148
1149#define ELF_START_MMAP 0x80000000
1150
fdf9b3e8 1151#define ELF_CLASS ELFCLASS32
fdf9b3e8
FB
1152#define ELF_ARCH EM_SH
1153
d97ef72e
RH
1154static inline void init_thread(struct target_pt_regs *regs,
1155 struct image_info *infop)
fdf9b3e8 1156{
d97ef72e
RH
1157 /* Check other registers XXXXX */
1158 regs->pc = infop->entry;
1159 regs->regs[15] = infop->start_stack;
fdf9b3e8
FB
1160}
1161
7631c97e
NF
1162/* See linux kernel: arch/sh/include/asm/elf.h. */
1163#define ELF_NREG 23
1164typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1165
1166/* See linux kernel: arch/sh/include/asm/ptrace.h. */
1167enum {
1168 TARGET_REG_PC = 16,
1169 TARGET_REG_PR = 17,
1170 TARGET_REG_SR = 18,
1171 TARGET_REG_GBR = 19,
1172 TARGET_REG_MACH = 20,
1173 TARGET_REG_MACL = 21,
1174 TARGET_REG_SYSCALL = 22
1175};
1176
d97ef72e 1177static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
05390248 1178 const CPUSH4State *env)
7631c97e
NF
1179{
1180 int i;
1181
1182 for (i = 0; i < 16; i++) {
72cd500b 1183 (*regs)[i] = tswapreg(env->gregs[i]);
7631c97e
NF
1184 }
1185
86cd7b2d
PB
1186 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1187 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1188 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1189 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1190 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1191 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
7631c97e
NF
1192 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1193}
1194
1195#define USE_ELF_CORE_DUMP
fdf9b3e8
FB
1196#define ELF_EXEC_PAGESIZE 4096
1197
e42fd944
RH
1198enum {
1199 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1200 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1201 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1202 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1203 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1204 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1205 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1206 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1207 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1208 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1209};
1210
1211#define ELF_HWCAP get_elf_hwcap()
1212
1213static uint32_t get_elf_hwcap(void)
1214{
1215 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1216 uint32_t hwcap = 0;
1217
1218 hwcap |= SH_CPU_HAS_FPU;
1219
1220 if (cpu->env.features & SH_FEATURE_SH4A) {
1221 hwcap |= SH_CPU_HAS_LLSC;
1222 }
1223
1224 return hwcap;
1225}
1226
fdf9b3e8
FB
1227#endif
1228
48733d19
TS
1229#ifdef TARGET_CRIS
1230
1231#define ELF_START_MMAP 0x80000000
1232
48733d19 1233#define ELF_CLASS ELFCLASS32
48733d19
TS
1234#define ELF_ARCH EM_CRIS
1235
d97ef72e
RH
1236static inline void init_thread(struct target_pt_regs *regs,
1237 struct image_info *infop)
48733d19 1238{
d97ef72e 1239 regs->erp = infop->entry;
48733d19
TS
1240}
1241
48733d19
TS
1242#define ELF_EXEC_PAGESIZE 8192
1243
1244#endif
1245
e6e5906b
PB
1246#ifdef TARGET_M68K
1247
1248#define ELF_START_MMAP 0x80000000
1249
d97ef72e 1250#define ELF_CLASS ELFCLASS32
d97ef72e 1251#define ELF_ARCH EM_68K
e6e5906b
PB
1252
1253/* ??? Does this need to do anything?
d97ef72e 1254 #define ELF_PLAT_INIT(_r) */
e6e5906b 1255
d97ef72e
RH
1256static inline void init_thread(struct target_pt_regs *regs,
1257 struct image_info *infop)
e6e5906b
PB
1258{
1259 regs->usp = infop->start_stack;
1260 regs->sr = 0;
1261 regs->pc = infop->entry;
1262}
1263
7a93cc55
NF
1264/* See linux kernel: arch/m68k/include/asm/elf.h. */
1265#define ELF_NREG 20
1266typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1267
05390248 1268static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
7a93cc55 1269{
86cd7b2d
PB
1270 (*regs)[0] = tswapreg(env->dregs[1]);
1271 (*regs)[1] = tswapreg(env->dregs[2]);
1272 (*regs)[2] = tswapreg(env->dregs[3]);
1273 (*regs)[3] = tswapreg(env->dregs[4]);
1274 (*regs)[4] = tswapreg(env->dregs[5]);
1275 (*regs)[5] = tswapreg(env->dregs[6]);
1276 (*regs)[6] = tswapreg(env->dregs[7]);
1277 (*regs)[7] = tswapreg(env->aregs[0]);
1278 (*regs)[8] = tswapreg(env->aregs[1]);
1279 (*regs)[9] = tswapreg(env->aregs[2]);
1280 (*regs)[10] = tswapreg(env->aregs[3]);
1281 (*regs)[11] = tswapreg(env->aregs[4]);
1282 (*regs)[12] = tswapreg(env->aregs[5]);
1283 (*regs)[13] = tswapreg(env->aregs[6]);
1284 (*regs)[14] = tswapreg(env->dregs[0]);
1285 (*regs)[15] = tswapreg(env->aregs[7]);
1286 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1287 (*regs)[17] = tswapreg(env->sr);
1288 (*regs)[18] = tswapreg(env->pc);
7a93cc55
NF
1289 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1290}
1291
1292#define USE_ELF_CORE_DUMP
d97ef72e 1293#define ELF_EXEC_PAGESIZE 8192
e6e5906b
PB
1294
1295#endif
1296
7a3148a9
JM
1297#ifdef TARGET_ALPHA
1298
1299#define ELF_START_MMAP (0x30000000000ULL)
1300
7a3148a9 1301#define ELF_CLASS ELFCLASS64
7a3148a9
JM
1302#define ELF_ARCH EM_ALPHA
1303
d97ef72e
RH
1304static inline void init_thread(struct target_pt_regs *regs,
1305 struct image_info *infop)
7a3148a9
JM
1306{
1307 regs->pc = infop->entry;
1308 regs->ps = 8;
1309 regs->usp = infop->start_stack;
7a3148a9
JM
1310}
1311
7a3148a9
JM
1312#define ELF_EXEC_PAGESIZE 8192
1313
1314#endif /* TARGET_ALPHA */
1315
a4c075f1
UH
1316#ifdef TARGET_S390X
1317
1318#define ELF_START_MMAP (0x20000000000ULL)
1319
a4c075f1
UH
1320#define ELF_CLASS ELFCLASS64
1321#define ELF_DATA ELFDATA2MSB
1322#define ELF_ARCH EM_S390
1323
6d88baf1
DH
1324#include "elf.h"
1325
1326#define ELF_HWCAP get_elf_hwcap()
1327
1328#define GET_FEATURE(_feat, _hwcap) \
1329 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1330
1331static uint32_t get_elf_hwcap(void)
1332{
1333 /*
1334 * Let's assume we always have esan3 and zarch.
1335 * 31-bit processes can use 64-bit registers (high gprs).
1336 */
1337 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1338
1339 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1340 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1341 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1342 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1343 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1344 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1345 hwcap |= HWCAP_S390_ETF3EH;
1346 }
1347 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1348
1349 return hwcap;
1350}
1351
a4c075f1
UH
1352static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1353{
1354 regs->psw.addr = infop->entry;
1355 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1356 regs->gprs[15] = infop->start_stack;
1357}
1358
1359#endif /* TARGET_S390X */
1360
b16189b2
CG
1361#ifdef TARGET_TILEGX
1362
1363/* 42 bits real used address, a half for user mode */
1364#define ELF_START_MMAP (0x00000020000000000ULL)
1365
1366#define elf_check_arch(x) ((x) == EM_TILEGX)
1367
1368#define ELF_CLASS ELFCLASS64
1369#define ELF_DATA ELFDATA2LSB
1370#define ELF_ARCH EM_TILEGX
1371
1372static inline void init_thread(struct target_pt_regs *regs,
1373 struct image_info *infop)
1374{
1375 regs->pc = infop->entry;
1376 regs->sp = infop->start_stack;
1377
1378}
1379
1380#define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
1381
1382#endif /* TARGET_TILEGX */
1383
47ae93cd
MC
1384#ifdef TARGET_RISCV
1385
1386#define ELF_START_MMAP 0x80000000
1387#define ELF_ARCH EM_RISCV
1388
1389#ifdef TARGET_RISCV32
1390#define ELF_CLASS ELFCLASS32
1391#else
1392#define ELF_CLASS ELFCLASS64
1393#endif
1394
1395static inline void init_thread(struct target_pt_regs *regs,
1396 struct image_info *infop)
1397{
1398 regs->sepc = infop->entry;
1399 regs->sp = infop->start_stack;
1400}
1401
1402#define ELF_EXEC_PAGESIZE 4096
1403
1404#endif /* TARGET_RISCV */
1405
7c248bcd
RH
1406#ifdef TARGET_HPPA
1407
1408#define ELF_START_MMAP 0x80000000
1409#define ELF_CLASS ELFCLASS32
1410#define ELF_ARCH EM_PARISC
1411#define ELF_PLATFORM "PARISC"
1412#define STACK_GROWS_DOWN 0
1413#define STACK_ALIGNMENT 64
1414
1415static inline void init_thread(struct target_pt_regs *regs,
1416 struct image_info *infop)
1417{
1418 regs->iaoq[0] = infop->entry;
1419 regs->iaoq[1] = infop->entry + 4;
1420 regs->gr[23] = 0;
1421 regs->gr[24] = infop->arg_start;
1422 regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
1423 /* The top-of-stack contains a linkage buffer. */
1424 regs->gr[30] = infop->start_stack + 64;
1425 regs->gr[31] = infop->entry;
1426}
1427
1428#endif /* TARGET_HPPA */
1429
ba7651fb
MF
1430#ifdef TARGET_XTENSA
1431
1432#define ELF_START_MMAP 0x20000000
1433
1434#define ELF_CLASS ELFCLASS32
1435#define ELF_ARCH EM_XTENSA
1436
1437static inline void init_thread(struct target_pt_regs *regs,
1438 struct image_info *infop)
1439{
1440 regs->windowbase = 0;
1441 regs->windowstart = 1;
1442 regs->areg[1] = infop->start_stack;
1443 regs->pc = infop->entry;
1444}
1445
1446/* See linux kernel: arch/xtensa/include/asm/elf.h. */
1447#define ELF_NREG 128
1448typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1449
1450enum {
1451 TARGET_REG_PC,
1452 TARGET_REG_PS,
1453 TARGET_REG_LBEG,
1454 TARGET_REG_LEND,
1455 TARGET_REG_LCOUNT,
1456 TARGET_REG_SAR,
1457 TARGET_REG_WINDOWSTART,
1458 TARGET_REG_WINDOWBASE,
1459 TARGET_REG_THREADPTR,
1460 TARGET_REG_AR0 = 64,
1461};
1462
1463static void elf_core_copy_regs(target_elf_gregset_t *regs,
1464 const CPUXtensaState *env)
1465{
1466 unsigned i;
1467
1468 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1469 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
1470 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
1471 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
1472 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
1473 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
1474 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
1475 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
1476 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
1477 xtensa_sync_phys_from_window((CPUXtensaState *)env);
1478 for (i = 0; i < env->config->nareg; ++i) {
1479 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
1480 }
1481}
1482
1483#define USE_ELF_CORE_DUMP
1484#define ELF_EXEC_PAGESIZE 4096
1485
1486#endif /* TARGET_XTENSA */
1487
15338fd7
FB
1488#ifndef ELF_PLATFORM
1489#define ELF_PLATFORM (NULL)
1490#endif
1491
75be901c
PC
1492#ifndef ELF_MACHINE
1493#define ELF_MACHINE ELF_ARCH
1494#endif
1495
d276a604
PC
1496#ifndef elf_check_arch
1497#define elf_check_arch(x) ((x) == ELF_ARCH)
1498#endif
1499
ace3d654
CMAB
1500#ifndef elf_check_abi
1501#define elf_check_abi(x) (1)
1502#endif
1503
15338fd7
FB
1504#ifndef ELF_HWCAP
1505#define ELF_HWCAP 0
1506#endif
1507
7c4ee5bc
RH
1508#ifndef STACK_GROWS_DOWN
1509#define STACK_GROWS_DOWN 1
1510#endif
1511
1512#ifndef STACK_ALIGNMENT
1513#define STACK_ALIGNMENT 16
1514#endif
1515
992f48a0 1516#ifdef TARGET_ABI32
cb33da57 1517#undef ELF_CLASS
992f48a0 1518#define ELF_CLASS ELFCLASS32
cb33da57
BS
1519#undef bswaptls
1520#define bswaptls(ptr) bswap32s(ptr)
1521#endif
1522
31e31b8a 1523#include "elf.h"
09bfb054 1524
e8384b37
RH
1525/* We must delay the following stanzas until after "elf.h". */
1526#if defined(TARGET_AARCH64)
1527
1528static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1529 const uint32_t *data,
1530 struct image_info *info,
1531 Error **errp)
1532{
1533 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
1534 if (pr_datasz != sizeof(uint32_t)) {
1535 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
1536 return false;
1537 }
1538 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
1539 info->note_flags = *data;
1540 }
1541 return true;
1542}
1543#define ARCH_USE_GNU_PROPERTY 1
1544
1545#else
1546
83f990eb
RH
1547static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
1548 const uint32_t *data,
1549 struct image_info *info,
1550 Error **errp)
1551{
1552 g_assert_not_reached();
1553}
1554#define ARCH_USE_GNU_PROPERTY 0
1555
e8384b37
RH
1556#endif
1557
09bfb054
FB
1558struct exec
1559{
d97ef72e
RH
1560 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1561 unsigned int a_text; /* length of text, in bytes */
1562 unsigned int a_data; /* length of data, in bytes */
1563 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1564 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1565 unsigned int a_entry; /* start address */
1566 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1567 unsigned int a_drsize; /* length of relocation info for data, in bytes */
09bfb054
FB
1568};
1569
1570
1571#define N_MAGIC(exec) ((exec).a_info & 0xffff)
1572#define OMAGIC 0407
1573#define NMAGIC 0410
1574#define ZMAGIC 0413
1575#define QMAGIC 0314
1576
31e31b8a 1577/* Necessary parameters */
94894ff2
SB
1578#define TARGET_ELF_EXEC_PAGESIZE \
1579 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1580 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1581#define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
79cb1f1d
YK
1582#define TARGET_ELF_PAGESTART(_v) ((_v) & \
1583 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
54936004 1584#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
31e31b8a 1585
e0d1673d 1586#define DLINFO_ITEMS 16
31e31b8a 1587
09bfb054
FB
1588static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1589{
d97ef72e 1590 memcpy(to, from, n);
09bfb054 1591}
d691f669 1592
31e31b8a 1593#ifdef BSWAP_NEEDED
92a31b1f 1594static void bswap_ehdr(struct elfhdr *ehdr)
31e31b8a 1595{
d97ef72e
RH
1596 bswap16s(&ehdr->e_type); /* Object file type */
1597 bswap16s(&ehdr->e_machine); /* Architecture */
1598 bswap32s(&ehdr->e_version); /* Object file version */
1599 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1600 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1601 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1602 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1603 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1604 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1605 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1606 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1607 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1608 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
31e31b8a
FB
1609}
1610
991f8f0c 1611static void bswap_phdr(struct elf_phdr *phdr, int phnum)
31e31b8a 1612{
991f8f0c
RH
1613 int i;
1614 for (i = 0; i < phnum; ++i, ++phdr) {
1615 bswap32s(&phdr->p_type); /* Segment type */
1616 bswap32s(&phdr->p_flags); /* Segment flags */
1617 bswaptls(&phdr->p_offset); /* Segment file offset */
1618 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1619 bswaptls(&phdr->p_paddr); /* Segment physical address */
1620 bswaptls(&phdr->p_filesz); /* Segment size in file */
1621 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1622 bswaptls(&phdr->p_align); /* Segment alignment */
1623 }
31e31b8a 1624}
689f936f 1625
991f8f0c 1626static void bswap_shdr(struct elf_shdr *shdr, int shnum)
689f936f 1627{
991f8f0c
RH
1628 int i;
1629 for (i = 0; i < shnum; ++i, ++shdr) {
1630 bswap32s(&shdr->sh_name);
1631 bswap32s(&shdr->sh_type);
1632 bswaptls(&shdr->sh_flags);
1633 bswaptls(&shdr->sh_addr);
1634 bswaptls(&shdr->sh_offset);
1635 bswaptls(&shdr->sh_size);
1636 bswap32s(&shdr->sh_link);
1637 bswap32s(&shdr->sh_info);
1638 bswaptls(&shdr->sh_addralign);
1639 bswaptls(&shdr->sh_entsize);
1640 }
689f936f
FB
1641}
1642
7a3148a9 1643static void bswap_sym(struct elf_sym *sym)
689f936f
FB
1644{
1645 bswap32s(&sym->st_name);
7a3148a9
JM
1646 bswaptls(&sym->st_value);
1647 bswaptls(&sym->st_size);
689f936f
FB
1648 bswap16s(&sym->st_shndx);
1649}
5dd0db52
SM
1650
1651#ifdef TARGET_MIPS
1652static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
1653{
1654 bswap16s(&abiflags->version);
1655 bswap32s(&abiflags->ases);
1656 bswap32s(&abiflags->isa_ext);
1657 bswap32s(&abiflags->flags1);
1658 bswap32s(&abiflags->flags2);
1659}
1660#endif
991f8f0c
RH
1661#else
1662static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1663static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1664static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1665static inline void bswap_sym(struct elf_sym *sym) { }
5dd0db52
SM
1666#ifdef TARGET_MIPS
1667static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
1668#endif
31e31b8a
FB
1669#endif
1670
edf8e2af 1671#ifdef USE_ELF_CORE_DUMP
9349b4f9 1672static int elf_core_dump(int, const CPUArchState *);
edf8e2af 1673#endif /* USE_ELF_CORE_DUMP */
682674b8 1674static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
edf8e2af 1675
9058abdd
RH
1676/* Verify the portions of EHDR within E_IDENT for the target.
1677 This can be performed before bswapping the entire header. */
1678static bool elf_check_ident(struct elfhdr *ehdr)
1679{
1680 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1681 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1682 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1683 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1684 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1685 && ehdr->e_ident[EI_DATA] == ELF_DATA
1686 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1687}
1688
1689/* Verify the portions of EHDR outside of E_IDENT for the target.
1690 This has to wait until after bswapping the header. */
1691static bool elf_check_ehdr(struct elfhdr *ehdr)
1692{
1693 return (elf_check_arch(ehdr->e_machine)
ace3d654 1694 && elf_check_abi(ehdr->e_flags)
9058abdd
RH
1695 && ehdr->e_ehsize == sizeof(struct elfhdr)
1696 && ehdr->e_phentsize == sizeof(struct elf_phdr)
9058abdd
RH
1697 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1698}
1699
31e31b8a 1700/*
e5fe0c52 1701 * 'copy_elf_strings()' copies argument/envelope strings from user
31e31b8a
FB
1702 * memory to free pages in kernel mem. These are in a format ready
1703 * to be put directly into the top of new user memory.
1704 *
1705 */
59baae9a
SB
1706static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
1707 abi_ulong p, abi_ulong stack_limit)
31e31b8a 1708{
59baae9a 1709 char *tmp;
7c4ee5bc 1710 int len, i;
59baae9a 1711 abi_ulong top = p;
31e31b8a
FB
1712
1713 if (!p) {
d97ef72e 1714 return 0; /* bullet-proofing */
31e31b8a 1715 }
59baae9a 1716
7c4ee5bc
RH
1717 if (STACK_GROWS_DOWN) {
1718 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
1719 for (i = argc - 1; i >= 0; --i) {
1720 tmp = argv[i];
1721 if (!tmp) {
1722 fprintf(stderr, "VFS: argc is wrong");
1723 exit(-1);
1724 }
1725 len = strlen(tmp) + 1;
1726 tmp += len;
59baae9a 1727
7c4ee5bc
RH
1728 if (len > (p - stack_limit)) {
1729 return 0;
1730 }
1731 while (len) {
1732 int bytes_to_copy = (len > offset) ? offset : len;
1733 tmp -= bytes_to_copy;
1734 p -= bytes_to_copy;
1735 offset -= bytes_to_copy;
1736 len -= bytes_to_copy;
1737
1738 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
1739
1740 if (offset == 0) {
1741 memcpy_to_target(p, scratch, top - p);
1742 top = p;
1743 offset = TARGET_PAGE_SIZE;
1744 }
1745 }
d97ef72e 1746 }
7c4ee5bc
RH
1747 if (p != top) {
1748 memcpy_to_target(p, scratch + offset, top - p);
d97ef72e 1749 }
7c4ee5bc
RH
1750 } else {
1751 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
1752 for (i = 0; i < argc; ++i) {
1753 tmp = argv[i];
1754 if (!tmp) {
1755 fprintf(stderr, "VFS: argc is wrong");
1756 exit(-1);
1757 }
1758 len = strlen(tmp) + 1;
1759 if (len > (stack_limit - p)) {
1760 return 0;
1761 }
1762 while (len) {
1763 int bytes_to_copy = (len > remaining) ? remaining : len;
1764
1765 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
1766
1767 tmp += bytes_to_copy;
1768 remaining -= bytes_to_copy;
1769 p += bytes_to_copy;
1770 len -= bytes_to_copy;
1771
1772 if (remaining == 0) {
1773 memcpy_to_target(top, scratch, p - top);
1774 top = p;
1775 remaining = TARGET_PAGE_SIZE;
1776 }
d97ef72e
RH
1777 }
1778 }
7c4ee5bc
RH
1779 if (p != top) {
1780 memcpy_to_target(top, scratch, p - top);
1781 }
59baae9a
SB
1782 }
1783
31e31b8a
FB
1784 return p;
1785}
1786
59baae9a
SB
1787/* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
1788 * argument/environment space. Newer kernels (>2.6.33) allow more,
1789 * dependent on stack size, but guarantee at least 32 pages for
1790 * backwards compatibility.
1791 */
1792#define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
1793
1794static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
992f48a0 1795 struct image_info *info)
53a5960a 1796{
59baae9a 1797 abi_ulong size, error, guard;
31e31b8a 1798
703e0e89 1799 size = guest_stack_size;
59baae9a
SB
1800 if (size < STACK_LOWER_LIMIT) {
1801 size = STACK_LOWER_LIMIT;
60dcbcb5
RH
1802 }
1803 guard = TARGET_PAGE_SIZE;
1804 if (guard < qemu_real_host_page_size) {
1805 guard = qemu_real_host_page_size;
1806 }
1807
1808 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1809 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
09bfb054 1810 if (error == -1) {
60dcbcb5 1811 perror("mmap stack");
09bfb054
FB
1812 exit(-1);
1813 }
31e31b8a 1814
60dcbcb5 1815 /* We reserve one extra page at the top of the stack as guard. */
7c4ee5bc
RH
1816 if (STACK_GROWS_DOWN) {
1817 target_mprotect(error, guard, PROT_NONE);
1818 info->stack_limit = error + guard;
1819 return info->stack_limit + size - sizeof(void *);
1820 } else {
1821 target_mprotect(error + size, guard, PROT_NONE);
1822 info->stack_limit = error + size;
1823 return error;
1824 }
31e31b8a
FB
1825}
1826
cf129f3a
RH
1827/* Map and zero the bss. We need to explicitly zero any fractional pages
1828 after the data section (i.e. bss). */
1829static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
31e31b8a 1830{
cf129f3a
RH
1831 uintptr_t host_start, host_map_start, host_end;
1832
1833 last_bss = TARGET_PAGE_ALIGN(last_bss);
1834
1835 /* ??? There is confusion between qemu_real_host_page_size and
1836 qemu_host_page_size here and elsewhere in target_mmap, which
1837 may lead to the end of the data section mapping from the file
1838 not being mapped. At least there was an explicit test and
1839 comment for that here, suggesting that "the file size must
1840 be known". The comment probably pre-dates the introduction
1841 of the fstat system call in target_mmap which does in fact
1842 find out the size. What isn't clear is if the workaround
1843 here is still actually needed. For now, continue with it,
1844 but merge it with the "normal" mmap that would allocate the bss. */
1845
1846 host_start = (uintptr_t) g2h(elf_bss);
1847 host_end = (uintptr_t) g2h(last_bss);
0c2d70c4 1848 host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
cf129f3a
RH
1849
1850 if (host_map_start < host_end) {
1851 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1852 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1853 if (p == MAP_FAILED) {
1854 perror("cannot mmap brk");
1855 exit(-1);
853d6f7a 1856 }
f46e9a0b 1857 }
853d6f7a 1858
f46e9a0b
TM
1859 /* Ensure that the bss page(s) are valid */
1860 if ((page_get_flags(last_bss-1) & prot) != prot) {
1861 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
cf129f3a 1862 }
31e31b8a 1863
cf129f3a
RH
1864 if (host_start < host_map_start) {
1865 memset((void *)host_start, 0, host_map_start - host_start);
1866 }
1867}
53a5960a 1868
cf58affe
CL
1869#ifdef TARGET_ARM
1870static int elf_is_fdpic(struct elfhdr *exec)
1871{
1872 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
1873}
1874#else
a99856cd
CL
1875/* Default implementation, always false. */
1876static int elf_is_fdpic(struct elfhdr *exec)
1877{
1878 return 0;
1879}
cf58affe 1880#endif
a99856cd 1881
1af02e83
MF
1882static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1883{
1884 uint16_t n;
1885 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1886
1887 /* elf32_fdpic_loadseg */
1888 n = info->nsegs;
1889 while (n--) {
1890 sp -= 12;
1891 put_user_u32(loadsegs[n].addr, sp+0);
1892 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1893 put_user_u32(loadsegs[n].p_memsz, sp+8);
1894 }
1895
1896 /* elf32_fdpic_loadmap */
1897 sp -= 4;
1898 put_user_u16(0, sp+0); /* version */
1899 put_user_u16(info->nsegs, sp+2); /* nsegs */
1900
1901 info->personality = PER_LINUX_FDPIC;
1902 info->loadmap_addr = sp;
1903
1904 return sp;
1905}
1af02e83 1906
992f48a0 1907static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
8e62a717
RH
1908 struct elfhdr *exec,
1909 struct image_info *info,
1910 struct image_info *interp_info)
31e31b8a 1911{
d97ef72e 1912 abi_ulong sp;
7c4ee5bc 1913 abi_ulong u_argc, u_argv, u_envp, u_auxv;
d97ef72e 1914 int size;
14322bad
LA
1915 int i;
1916 abi_ulong u_rand_bytes;
1917 uint8_t k_rand_bytes[16];
d97ef72e
RH
1918 abi_ulong u_platform;
1919 const char *k_platform;
1920 const int n = sizeof(elf_addr_t);
1921
1922 sp = p;
1af02e83 1923
1af02e83
MF
1924 /* Needs to be before we load the env/argc/... */
1925 if (elf_is_fdpic(exec)) {
1926 /* Need 4 byte alignment for these structs */
1927 sp &= ~3;
1928 sp = loader_build_fdpic_loadmap(info, sp);
1929 info->other_info = interp_info;
1930 if (interp_info) {
1931 interp_info->other_info = info;
1932 sp = loader_build_fdpic_loadmap(interp_info, sp);
3cb10cfa
CL
1933 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
1934 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
1935 } else {
1936 info->interpreter_loadmap_addr = 0;
1937 info->interpreter_pt_dynamic_addr = 0;
1af02e83
MF
1938 }
1939 }
1af02e83 1940
d97ef72e
RH
1941 u_platform = 0;
1942 k_platform = ELF_PLATFORM;
1943 if (k_platform) {
1944 size_t len = strlen(k_platform) + 1;
7c4ee5bc
RH
1945 if (STACK_GROWS_DOWN) {
1946 sp -= (len + n - 1) & ~(n - 1);
1947 u_platform = sp;
1948 /* FIXME - check return value of memcpy_to_target() for failure */
1949 memcpy_to_target(sp, k_platform, len);
1950 } else {
1951 memcpy_to_target(sp, k_platform, len);
1952 u_platform = sp;
1953 sp += len + 1;
1954 }
1955 }
1956
1957 /* Provide 16 byte alignment for the PRNG, and basic alignment for
1958 * the argv and envp pointers.
1959 */
1960 if (STACK_GROWS_DOWN) {
1961 sp = QEMU_ALIGN_DOWN(sp, 16);
1962 } else {
1963 sp = QEMU_ALIGN_UP(sp, 16);
d97ef72e 1964 }
14322bad
LA
1965
1966 /*
c6a2377f 1967 * Generate 16 random bytes for userspace PRNG seeding.
14322bad 1968 */
c6a2377f 1969 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
7c4ee5bc
RH
1970 if (STACK_GROWS_DOWN) {
1971 sp -= 16;
1972 u_rand_bytes = sp;
1973 /* FIXME - check return value of memcpy_to_target() for failure */
1974 memcpy_to_target(sp, k_rand_bytes, 16);
1975 } else {
1976 memcpy_to_target(sp, k_rand_bytes, 16);
1977 u_rand_bytes = sp;
1978 sp += 16;
1979 }
14322bad 1980
d97ef72e
RH
1981 size = (DLINFO_ITEMS + 1) * 2;
1982 if (k_platform)
1983 size += 2;
f5155289 1984#ifdef DLINFO_ARCH_ITEMS
d97ef72e 1985 size += DLINFO_ARCH_ITEMS * 2;
ad6919dc
PM
1986#endif
1987#ifdef ELF_HWCAP2
1988 size += 2;
f5155289 1989#endif
f516511e
PM
1990 info->auxv_len = size * n;
1991
d97ef72e 1992 size += envc + argc + 2;
b9329d4b 1993 size += 1; /* argc itself */
d97ef72e 1994 size *= n;
7c4ee5bc
RH
1995
1996 /* Allocate space and finalize stack alignment for entry now. */
1997 if (STACK_GROWS_DOWN) {
1998 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
1999 sp = u_argc;
2000 } else {
2001 u_argc = sp;
2002 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2003 }
2004
2005 u_argv = u_argc + n;
2006 u_envp = u_argv + (argc + 1) * n;
2007 u_auxv = u_envp + (envc + 1) * n;
2008 info->saved_auxv = u_auxv;
2009 info->arg_start = u_argv;
2010 info->arg_end = u_argv + argc * n;
d97ef72e
RH
2011
2012 /* This is correct because Linux defines
2013 * elf_addr_t as Elf32_Off / Elf64_Off
2014 */
2015#define NEW_AUX_ENT(id, val) do { \
7c4ee5bc
RH
2016 put_user_ual(id, u_auxv); u_auxv += n; \
2017 put_user_ual(val, u_auxv); u_auxv += n; \
d97ef72e
RH
2018 } while(0)
2019
82991bed
PM
2020#ifdef ARCH_DLINFO
2021 /*
2022 * ARCH_DLINFO must come first so platform specific code can enforce
2023 * special alignment requirements on the AUXV if necessary (eg. PPC).
2024 */
2025 ARCH_DLINFO;
2026#endif
f516511e
PM
2027 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2028 * on info->auxv_len will trigger.
2029 */
8e62a717 2030 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
d97ef72e
RH
2031 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2032 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
33143c44
LV
2033 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2034 /* Target doesn't support host page size alignment */
2035 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2036 } else {
2037 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2038 qemu_host_page_size)));
2039 }
8e62a717 2040 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
d97ef72e 2041 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
8e62a717 2042 NEW_AUX_ENT(AT_ENTRY, info->entry);
d97ef72e
RH
2043 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2044 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2045 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2046 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2047 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2048 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
14322bad 2049 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
444cd5c3 2050 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
e0d1673d 2051 NEW_AUX_ENT(AT_EXECFN, info->file_string);
14322bad 2052
ad6919dc
PM
2053#ifdef ELF_HWCAP2
2054 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2055#endif
2056
7c4ee5bc 2057 if (u_platform) {
d97ef72e 2058 NEW_AUX_ENT(AT_PLATFORM, u_platform);
7c4ee5bc 2059 }
7c4ee5bc 2060 NEW_AUX_ENT (AT_NULL, 0);
f5155289
FB
2061#undef NEW_AUX_ENT
2062
f516511e
PM
2063 /* Check that our initial calculation of the auxv length matches how much
2064 * we actually put into it.
2065 */
2066 assert(info->auxv_len == u_auxv - info->saved_auxv);
7c4ee5bc
RH
2067
2068 put_user_ual(argc, u_argc);
2069
2070 p = info->arg_strings;
2071 for (i = 0; i < argc; ++i) {
2072 put_user_ual(p, u_argv);
2073 u_argv += n;
2074 p += target_strlen(p) + 1;
2075 }
2076 put_user_ual(0, u_argv);
2077
2078 p = info->env_strings;
2079 for (i = 0; i < envc; ++i) {
2080 put_user_ual(p, u_envp);
2081 u_envp += n;
2082 p += target_strlen(p) + 1;
2083 }
2084 put_user_ual(0, u_envp);
edf8e2af 2085
d97ef72e 2086 return sp;
31e31b8a
FB
2087}
2088
ee947430
AB
2089#ifndef ARM_COMMPAGE
2090#define ARM_COMMPAGE 0
2091#define init_guest_commpage() true
8756e136 2092#endif
dce10401 2093
ee947430
AB
2094static void pgb_fail_in_use(const char *image_name)
2095{
2096 error_report("%s: requires virtual address space that is in use "
2097 "(omit the -B option or choose a different value)",
2098 image_name);
2099 exit(EXIT_FAILURE);
2100}
dce10401 2101
ee947430
AB
2102static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
2103 abi_ulong guest_hiaddr, long align)
2104{
2105 const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
2106 void *addr, *test;
2a53535a 2107
ee947430
AB
2108 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2109 fprintf(stderr, "Requested guest base 0x%lx does not satisfy "
2110 "host minimum alignment (0x%lx)\n",
2111 guest_base, align);
2112 exit(EXIT_FAILURE);
2113 }
2114
2115 /* Sanity check the guest binary. */
2116 if (reserved_va) {
2117 if (guest_hiaddr > reserved_va) {
2118 error_report("%s: requires more than reserved virtual "
2119 "address space (0x%" PRIx64 " > 0x%lx)",
2120 image_name, (uint64_t)guest_hiaddr, reserved_va);
2121 exit(EXIT_FAILURE);
2a53535a 2122 }
ee947430 2123 } else {
a932eec4 2124#if HOST_LONG_BITS < TARGET_ABI_BITS
ee947430
AB
2125 if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) {
2126 error_report("%s: requires more virtual address space "
2127 "than the host can provide (0x%" PRIx64 ")",
2128 image_name, (uint64_t)guest_hiaddr - guest_base);
2129 exit(EXIT_FAILURE);
2a53535a 2130 }
a932eec4 2131#endif
2a53535a 2132 }
2a53535a 2133
ee947430
AB
2134 /*
2135 * Expand the allocation to the entire reserved_va.
2136 * Exclude the mmap_min_addr hole.
2137 */
2138 if (reserved_va) {
2139 guest_loaddr = (guest_base >= mmap_min_addr ? 0
2140 : mmap_min_addr - guest_base);
2141 guest_hiaddr = reserved_va;
2142 }
806d1021 2143
ee947430
AB
2144 /* Reserve the address space for the binary, or reserved_va. */
2145 test = g2h(guest_loaddr);
2146 addr = mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0);
2147 if (test != addr) {
2148 pgb_fail_in_use(image_name);
2149 }
2150}
2151
ad592e37
AB
2152/**
2153 * pgd_find_hole_fallback: potential mmap address
2154 * @guest_size: size of available space
2155 * @brk: location of break
2156 * @align: memory alignment
2157 *
2158 * This is a fallback method for finding a hole in the host address
2159 * space if we don't have the benefit of being able to access
2160 * /proc/self/map. It can potentially take a very long time as we can
2161 * only dumbly iterate up the host address space seeing if the
2162 * allocation would work.
2163 */
5c3e87f3
AB
2164static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
2165 long align, uintptr_t offset)
ad592e37
AB
2166{
2167 uintptr_t base;
2168
2169 /* Start (aligned) at the bottom and work our way up */
2170 base = ROUND_UP(mmap_min_addr, align);
2171
2172 while (true) {
2173 uintptr_t align_start, end;
2174 align_start = ROUND_UP(base, align);
5c3e87f3 2175 end = align_start + guest_size + offset;
ad592e37
AB
2176
2177 /* if brk is anywhere in the range give ourselves some room to grow. */
2178 if (align_start <= brk && brk < end) {
2179 base = brk + (16 * MiB);
2180 continue;
2181 } else if (align_start + guest_size < align_start) {
2182 /* we have run out of space */
2183 return -1;
2184 } else {
2667e069
AB
2185 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE |
2186 MAP_FIXED_NOREPLACE;
ad592e37
AB
2187 void * mmap_start = mmap((void *) align_start, guest_size,
2188 PROT_NONE, flags, -1, 0);
2189 if (mmap_start != MAP_FAILED) {
2190 munmap((void *) align_start, guest_size);
2667e069
AB
2191 if (MAP_FIXED_NOREPLACE || mmap_start == (void *) align_start) {
2192 return (uintptr_t) mmap_start + offset;
2193 }
ad592e37
AB
2194 }
2195 base += qemu_host_page_size;
2196 }
2197 }
2198}
2199
ee947430
AB
2200/* Return value for guest_base, or -1 if no hole found. */
2201static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
5c3e87f3 2202 long align, uintptr_t offset)
ee947430
AB
2203{
2204 GSList *maps, *iter;
2205 uintptr_t this_start, this_end, next_start, brk;
2206 intptr_t ret = -1;
2207
2208 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
2209
2210 maps = read_self_maps();
dce10401 2211
ee947430
AB
2212 /* Read brk after we've read the maps, which will malloc. */
2213 brk = (uintptr_t)sbrk(0);
2214
ad592e37 2215 if (!maps) {
5c3e87f3 2216 return pgd_find_hole_fallback(guest_size, brk, align, offset);
ad592e37
AB
2217 }
2218
ee947430
AB
2219 /* The first hole is before the first map entry. */
2220 this_start = mmap_min_addr;
2221
2222 for (iter = maps; iter;
2223 this_start = next_start, iter = g_slist_next(iter)) {
2224 uintptr_t align_start, hole_size;
2225
2226 this_end = ((MapInfo *)iter->data)->start;
2227 next_start = ((MapInfo *)iter->data)->end;
5c3e87f3 2228 align_start = ROUND_UP(this_start + offset, align);
ee947430
AB
2229
2230 /* Skip holes that are too small. */
2231 if (align_start >= this_end) {
2232 continue;
2233 }
2234 hole_size = this_end - align_start;
2235 if (hole_size < guest_size) {
2236 continue;
aac362e4
LS
2237 }
2238
ee947430
AB
2239 /* If this hole contains brk, give ourselves some room to grow. */
2240 if (this_start <= brk && brk < this_end) {
2241 hole_size -= guest_size;
2242 if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) {
2243 align_start += 1 * GiB;
2244 } else if (hole_size >= 16 * MiB) {
2245 align_start += 16 * MiB;
2246 } else {
2247 align_start = (this_end - guest_size) & -align;
2248 if (align_start < this_start) {
2249 continue;
2250 }
806d1021 2251 }
806d1021
MI
2252 }
2253
ee947430
AB
2254 /* Record the lowest successful match. */
2255 if (ret < 0) {
2256 ret = align_start - guest_loaddr;
dce10401 2257 }
ee947430
AB
2258 /* If this hole contains the identity map, select it. */
2259 if (align_start <= guest_loaddr &&
2260 guest_loaddr + guest_size <= this_end) {
2261 ret = 0;
b859040d 2262 }
ee947430
AB
2263 /* If this hole ends above the identity map, stop looking. */
2264 if (this_end >= guest_loaddr) {
2265 break;
dce10401
MI
2266 }
2267 }
ee947430 2268 free_self_maps(maps);
dce10401 2269
ee947430 2270 return ret;
dce10401
MI
2271}
2272
ee947430
AB
2273static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
2274 abi_ulong orig_hiaddr, long align)
f3ed1f5d 2275{
ee947430
AB
2276 uintptr_t loaddr = orig_loaddr;
2277 uintptr_t hiaddr = orig_hiaddr;
5c3e87f3 2278 uintptr_t offset = 0;
ee947430 2279 uintptr_t addr;
f3ed1f5d 2280
ee947430
AB
2281 if (hiaddr != orig_hiaddr) {
2282 error_report("%s: requires virtual address space that the "
2283 "host cannot provide (0x%" PRIx64 ")",
2284 image_name, (uint64_t)orig_hiaddr);
2285 exit(EXIT_FAILURE);
2286 }
f3ed1f5d 2287
ee947430
AB
2288 loaddr &= -align;
2289 if (ARM_COMMPAGE) {
2290 /*
2291 * Extend the allocation to include the commpage.
5c3e87f3
AB
2292 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2293 * need to ensure there is space bellow the guest_base so we
2294 * can map the commpage in the place needed when the address
2295 * arithmetic wraps around.
ee947430
AB
2296 */
2297 if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
5c3e87f3 2298 hiaddr = (uintptr_t) 4 << 30;
f3ed1f5d 2299 } else {
5c3e87f3 2300 offset = -(ARM_COMMPAGE & -align);
f3ed1f5d 2301 }
ee947430 2302 }
dce10401 2303
5c3e87f3 2304 addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
ee947430
AB
2305 if (addr == -1) {
2306 /*
2307 * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
2308 * that can satisfy both. But as the normal arm32 link base address
2309 * is ~32k, and we extend down to include the commpage, making the
2310 * overhead only ~96k, this is unlikely.
dce10401 2311 */
ee947430
AB
2312 error_report("%s: Unable to allocate %#zx bytes of "
2313 "virtual address space", image_name,
2314 (size_t)(hiaddr - loaddr));
2315 exit(EXIT_FAILURE);
2316 }
2317
2318 guest_base = addr;
2319}
dce10401 2320
ee947430
AB
2321static void pgb_dynamic(const char *image_name, long align)
2322{
2323 /*
2324 * The executable is dynamic and does not require a fixed address.
2325 * All we need is a commpage that satisfies align.
2326 * If we do not need a commpage, leave guest_base == 0.
2327 */
2328 if (ARM_COMMPAGE) {
2329 uintptr_t addr, commpage;
2330
2331 /* 64-bit hosts should have used reserved_va. */
2332 assert(sizeof(uintptr_t) == 4);
2333
2334 /*
2335 * By putting the commpage at the first hole, that puts guest_base
2336 * just above that, and maximises the positive guest addresses.
2337 */
2338 commpage = ARM_COMMPAGE & -align;
5c3e87f3 2339 addr = pgb_find_hole(commpage, -commpage, align, 0);
ee947430
AB
2340 assert(addr != -1);
2341 guest_base = addr;
2342 }
2343}
2344
2345static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
2346 abi_ulong guest_hiaddr, long align)
2347{
c1f6ad79 2348 int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
ee947430
AB
2349 void *addr, *test;
2350
2351 if (guest_hiaddr > reserved_va) {
2352 error_report("%s: requires more than reserved virtual "
2353 "address space (0x%" PRIx64 " > 0x%lx)",
2354 image_name, (uint64_t)guest_hiaddr, reserved_va);
2355 exit(EXIT_FAILURE);
f3ed1f5d 2356 }
f3ed1f5d 2357
ee947430
AB
2358 /* Widen the "image" to the entire reserved address space. */
2359 pgb_static(image_name, 0, reserved_va, align);
2360
2667e069 2361 /* osdep.h defines this as 0 if it's missing */
c1f6ad79 2362 flags |= MAP_FIXED_NOREPLACE;
c1f6ad79 2363
ee947430
AB
2364 /* Reserve the memory on the host. */
2365 assert(guest_base != 0);
2366 test = g2h(0);
2367 addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0);
fb730c86 2368 if (addr == MAP_FAILED || addr != test) {
ee947430 2369 error_report("Unable to reserve 0x%lx bytes of virtual address "
fb730c86
AB
2370 "space at %p (%s) for use as guest address space (check your"
2371 "virtual memory ulimit setting, min_mmap_addr or reserve less "
2372 "using -R option)", reserved_va, test, strerror(errno));
ee947430
AB
2373 exit(EXIT_FAILURE);
2374 }
f3ed1f5d
PM
2375}
2376
ee947430
AB
2377void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
2378 abi_ulong guest_hiaddr)
2379{
2380 /* In order to use host shmat, we must be able to honor SHMLBA. */
2381 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
2382
2383 if (have_guest_base) {
2384 pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align);
2385 } else if (reserved_va) {
2386 pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align);
2387 } else if (guest_loaddr) {
2388 pgb_static(image_name, guest_loaddr, guest_hiaddr, align);
2389 } else {
2390 pgb_dynamic(image_name, align);
2391 }
2392
2393 /* Reserve and initialize the commpage. */
2394 if (!init_guest_commpage()) {
2395 /*
2396 * With have_guest_base, the user has selected the address and
2397 * we are trying to work with that. Otherwise, we have selected
2398 * free space and init_guest_commpage must succeeded.
2399 */
2400 assert(have_guest_base);
2401 pgb_fail_in_use(image_name);
2402 }
2403
2404 assert(QEMU_IS_ALIGNED(guest_base, align));
2405 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
2406 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
2407}
f3ed1f5d 2408
83f990eb
RH
2409enum {
2410 /* The string "GNU\0" as a magic number. */
2411 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
2412 NOTE_DATA_SZ = 1 * KiB,
2413 NOTE_NAME_SZ = 4,
2414 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
2415};
2416
2417/*
2418 * Process a single gnu_property entry.
2419 * Return false for error.
2420 */
2421static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
2422 struct image_info *info, bool have_prev_type,
2423 uint32_t *prev_type, Error **errp)
2424{
2425 uint32_t pr_type, pr_datasz, step;
2426
2427 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
2428 goto error_data;
2429 }
2430 datasz -= *off;
2431 data += *off / sizeof(uint32_t);
2432
2433 if (datasz < 2 * sizeof(uint32_t)) {
2434 goto error_data;
2435 }
2436 pr_type = data[0];
2437 pr_datasz = data[1];
2438 data += 2;
2439 datasz -= 2 * sizeof(uint32_t);
2440 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
2441 if (step > datasz) {
2442 goto error_data;
2443 }
2444
2445 /* Properties are supposed to be unique and sorted on pr_type. */
2446 if (have_prev_type && pr_type <= *prev_type) {
2447 if (pr_type == *prev_type) {
2448 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
2449 } else {
2450 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
2451 }
2452 return false;
2453 }
2454 *prev_type = pr_type;
2455
2456 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
2457 return false;
2458 }
2459
2460 *off += 2 * sizeof(uint32_t) + step;
2461 return true;
2462
2463 error_data:
2464 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
2465 return false;
2466}
2467
2468/* Process NT_GNU_PROPERTY_TYPE_0. */
2469static bool parse_elf_properties(int image_fd,
2470 struct image_info *info,
2471 const struct elf_phdr *phdr,
2472 char bprm_buf[BPRM_BUF_SIZE],
2473 Error **errp)
2474{
2475 union {
2476 struct elf_note nhdr;
2477 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
2478 } note;
2479
2480 int n, off, datasz;
2481 bool have_prev_type;
2482 uint32_t prev_type;
2483
2484 /* Unless the arch requires properties, ignore them. */
2485 if (!ARCH_USE_GNU_PROPERTY) {
2486 return true;
2487 }
2488
2489 /* If the properties are crazy large, that's too bad. */
2490 n = phdr->p_filesz;
2491 if (n > sizeof(note)) {
2492 error_setg(errp, "PT_GNU_PROPERTY too large");
2493 return false;
2494 }
2495 if (n < sizeof(note.nhdr)) {
2496 error_setg(errp, "PT_GNU_PROPERTY too small");
2497 return false;
2498 }
2499
2500 if (phdr->p_offset + n <= BPRM_BUF_SIZE) {
2501 memcpy(&note, bprm_buf + phdr->p_offset, n);
2502 } else {
2503 ssize_t len = pread(image_fd, &note, n, phdr->p_offset);
2504 if (len != n) {
2505 error_setg_errno(errp, errno, "Error reading file header");
2506 return false;
2507 }
2508 }
2509
2510 /*
2511 * The contents of a valid PT_GNU_PROPERTY is a sequence
2512 * of uint32_t -- swap them all now.
2513 */
2514#ifdef BSWAP_NEEDED
2515 for (int i = 0; i < n / 4; i++) {
2516 bswap32s(note.data + i);
2517 }
2518#endif
2519
2520 /*
2521 * Note that nhdr is 3 words, and that the "name" described by namesz
2522 * immediately follows nhdr and is thus at the 4th word. Further, all
2523 * of the inputs to the kernel's round_up are multiples of 4.
2524 */
2525 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
2526 note.nhdr.n_namesz != NOTE_NAME_SZ ||
2527 note.data[3] != GNU0_MAGIC) {
2528 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
2529 return false;
2530 }
2531 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
2532
2533 datasz = note.nhdr.n_descsz + off;
2534 if (datasz > n) {
2535 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
2536 return false;
2537 }
2538
2539 have_prev_type = false;
2540 prev_type = 0;
2541 while (1) {
2542 if (off == datasz) {
2543 return true; /* end, exit ok */
2544 }
2545 if (!parse_elf_property(note.data, &off, datasz, info,
2546 have_prev_type, &prev_type, errp)) {
2547 return false;
2548 }
2549 have_prev_type = true;
2550 }
2551}
2552
8e62a717 2553/* Load an ELF image into the address space.
31e31b8a 2554
8e62a717
RH
2555 IMAGE_NAME is the filename of the image, to use in error messages.
2556 IMAGE_FD is the open file descriptor for the image.
2557
2558 BPRM_BUF is a copy of the beginning of the file; this of course
2559 contains the elf file header at offset 0. It is assumed that this
2560 buffer is sufficiently aligned to present no problems to the host
2561 in accessing data at aligned offsets within the buffer.
2562
2563 On return: INFO values will be filled in, as necessary or available. */
2564
2565static void load_elf_image(const char *image_name, int image_fd,
bf858897 2566 struct image_info *info, char **pinterp_name,
8e62a717 2567 char bprm_buf[BPRM_BUF_SIZE])
31e31b8a 2568{
8e62a717
RH
2569 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
2570 struct elf_phdr *phdr;
2571 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
e8384b37 2572 int i, retval, prot_exec;
c7f17e7b 2573 Error *err = NULL;
5fafdf24 2574
8e62a717 2575 /* First of all, some simple consistency checks */
8e62a717 2576 if (!elf_check_ident(ehdr)) {
c7f17e7b 2577 error_setg(&err, "Invalid ELF image for this architecture");
8e62a717
RH
2578 goto exit_errmsg;
2579 }
2580 bswap_ehdr(ehdr);
2581 if (!elf_check_ehdr(ehdr)) {
c7f17e7b 2582 error_setg(&err, "Invalid ELF image for this architecture");
8e62a717 2583 goto exit_errmsg;
d97ef72e 2584 }
5fafdf24 2585
8e62a717
RH
2586 i = ehdr->e_phnum * sizeof(struct elf_phdr);
2587 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
2588 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
9955ffac 2589 } else {
8e62a717
RH
2590 phdr = (struct elf_phdr *) alloca(i);
2591 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
9955ffac 2592 if (retval != i) {
8e62a717 2593 goto exit_read;
9955ffac 2594 }
d97ef72e 2595 }
8e62a717 2596 bswap_phdr(phdr, ehdr->e_phnum);
09bfb054 2597
1af02e83
MF
2598 info->nsegs = 0;
2599 info->pt_dynamic_addr = 0;
1af02e83 2600
98c1076c
AB
2601 mmap_lock();
2602
8a1a5274
RH
2603 /*
2604 * Find the maximum size of the image and allocate an appropriate
2605 * amount of memory to handle that. Locate the interpreter, if any.
2606 */
682674b8 2607 loaddr = -1, hiaddr = 0;
33143c44 2608 info->alignment = 0;
8e62a717 2609 for (i = 0; i < ehdr->e_phnum; ++i) {
4d9d535a
RH
2610 struct elf_phdr *eppnt = phdr + i;
2611 if (eppnt->p_type == PT_LOAD) {
2612 abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
682674b8
RH
2613 if (a < loaddr) {
2614 loaddr = a;
2615 }
4d9d535a 2616 a = eppnt->p_vaddr + eppnt->p_memsz;
682674b8
RH
2617 if (a > hiaddr) {
2618 hiaddr = a;
2619 }
1af02e83 2620 ++info->nsegs;
4d9d535a 2621 info->alignment |= eppnt->p_align;
8a1a5274
RH
2622 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
2623 g_autofree char *interp_name = NULL;
2624
2625 if (*pinterp_name) {
c7f17e7b 2626 error_setg(&err, "Multiple PT_INTERP entries");
8a1a5274
RH
2627 goto exit_errmsg;
2628 }
c7f17e7b 2629
8a1a5274 2630 interp_name = g_malloc(eppnt->p_filesz);
8a1a5274
RH
2631
2632 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2633 memcpy(interp_name, bprm_buf + eppnt->p_offset,
2634 eppnt->p_filesz);
2635 } else {
2636 retval = pread(image_fd, interp_name, eppnt->p_filesz,
2637 eppnt->p_offset);
2638 if (retval != eppnt->p_filesz) {
c7f17e7b 2639 goto exit_read;
8a1a5274
RH
2640 }
2641 }
2642 if (interp_name[eppnt->p_filesz - 1] != 0) {
c7f17e7b 2643 error_setg(&err, "Invalid PT_INTERP entry");
8a1a5274
RH
2644 goto exit_errmsg;
2645 }
2646 *pinterp_name = g_steal_pointer(&interp_name);
83f990eb
RH
2647 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
2648 if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &err)) {
2649 goto exit_errmsg;
2650 }
682674b8
RH
2651 }
2652 }
2653
6fd59449
RH
2654 if (pinterp_name != NULL) {
2655 /*
2656 * This is the main executable.
2657 *
2658 * Reserve extra space for brk.
2659 * We hold on to this space while placing the interpreter
2660 * and the stack, lest they be placed immediately after
2661 * the data segment and block allocation from the brk.
2662 *
2663 * 16MB is chosen as "large enough" without being so large
2664 * as to allow the result to not fit with a 32-bit guest on
2665 * a 32-bit host.
2666 */
2667 info->reserve_brk = 16 * MiB;
2668 hiaddr += info->reserve_brk;
2669
2670 if (ehdr->e_type == ET_EXEC) {
2671 /*
2672 * Make sure that the low address does not conflict with
2673 * MMAP_MIN_ADDR or the QEMU application itself.
2674 */
2675 probe_guest_base(image_name, loaddr, hiaddr);
ee947430
AB
2676 } else {
2677 /*
2678 * The binary is dynamic, but we still need to
2679 * select guest_base. In this case we pass a size.
2680 */
2681 probe_guest_base(image_name, 0, hiaddr - loaddr);
d97ef72e 2682 }
6fd59449
RH
2683 }
2684
2685 /*
2686 * Reserve address space for all of this.
2687 *
2688 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
2689 * exactly the address range that is required.
2690 *
2691 * Otherwise this is ET_DYN, and we are searching for a location
2692 * that can hold the memory space required. If the image is
2693 * pre-linked, LOADDR will be non-zero, and the kernel should
2694 * honor that address if it happens to be free.
2695 *
2696 * In both cases, we will overwrite pages in this range with mappings
2697 * from the executable.
2698 */
2699 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
2700 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
2701 (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0),
2702 -1, 0);
2703 if (load_addr == -1) {
c7f17e7b 2704 goto exit_mmap;
d97ef72e 2705 }
682674b8 2706 load_bias = load_addr - loaddr;
d97ef72e 2707
a99856cd 2708 if (elf_is_fdpic(ehdr)) {
1af02e83 2709 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
7267c094 2710 g_malloc(sizeof(*loadsegs) * info->nsegs);
1af02e83
MF
2711
2712 for (i = 0; i < ehdr->e_phnum; ++i) {
2713 switch (phdr[i].p_type) {
2714 case PT_DYNAMIC:
2715 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
2716 break;
2717 case PT_LOAD:
2718 loadsegs->addr = phdr[i].p_vaddr + load_bias;
2719 loadsegs->p_vaddr = phdr[i].p_vaddr;
2720 loadsegs->p_memsz = phdr[i].p_memsz;
2721 ++loadsegs;
2722 break;
2723 }
2724 }
2725 }
1af02e83 2726
8e62a717 2727 info->load_bias = load_bias;
dc12567a
JK
2728 info->code_offset = load_bias;
2729 info->data_offset = load_bias;
8e62a717
RH
2730 info->load_addr = load_addr;
2731 info->entry = ehdr->e_entry + load_bias;
2732 info->start_code = -1;
2733 info->end_code = 0;
2734 info->start_data = -1;
2735 info->end_data = 0;
2736 info->brk = 0;
d8fd2954 2737 info->elf_flags = ehdr->e_flags;
8e62a717 2738
e8384b37
RH
2739 prot_exec = PROT_EXEC;
2740#ifdef TARGET_AARCH64
2741 /*
2742 * If the BTI feature is present, this indicates that the executable
2743 * pages of the startup binary should be mapped with PROT_BTI, so that
2744 * branch targets are enforced.
2745 *
2746 * The startup binary is either the interpreter or the static executable.
2747 * The interpreter is responsible for all pages of a dynamic executable.
2748 *
2749 * Elf notes are backward compatible to older cpus.
2750 * Do not enable BTI unless it is supported.
2751 */
2752 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
2753 && (pinterp_name == NULL || *pinterp_name == 0)
2754 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
2755 prot_exec |= TARGET_PROT_BTI;
2756 }
2757#endif
2758
8e62a717
RH
2759 for (i = 0; i < ehdr->e_phnum; i++) {
2760 struct elf_phdr *eppnt = phdr + i;
d97ef72e 2761 if (eppnt->p_type == PT_LOAD) {
94894ff2 2762 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
d97ef72e 2763 int elf_prot = 0;
d97ef72e 2764
e5eaf570
RH
2765 if (eppnt->p_flags & PF_R) {
2766 elf_prot |= PROT_READ;
2767 }
2768 if (eppnt->p_flags & PF_W) {
2769 elf_prot |= PROT_WRITE;
2770 }
2771 if (eppnt->p_flags & PF_X) {
e8384b37 2772 elf_prot |= prot_exec;
e5eaf570 2773 }
d97ef72e 2774
682674b8
RH
2775 vaddr = load_bias + eppnt->p_vaddr;
2776 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
2777 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
94894ff2 2778 vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
682674b8 2779
d87146bc
GM
2780 /*
2781 * Some segments may be completely empty without any backing file
2782 * segment, in that case just let zero_bss allocate an empty buffer
2783 * for it.
2784 */
2785 if (eppnt->p_filesz != 0) {
2786 error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
2787 MAP_PRIVATE | MAP_FIXED,
2788 image_fd, eppnt->p_offset - vaddr_po);
2789
2790 if (error == -1) {
c7f17e7b 2791 goto exit_mmap;
d87146bc 2792 }
09bfb054 2793 }
09bfb054 2794
682674b8
RH
2795 vaddr_ef = vaddr + eppnt->p_filesz;
2796 vaddr_em = vaddr + eppnt->p_memsz;
31e31b8a 2797
cf129f3a 2798 /* If the load segment requests extra zeros (e.g. bss), map it. */
682674b8
RH
2799 if (vaddr_ef < vaddr_em) {
2800 zero_bss(vaddr_ef, vaddr_em, elf_prot);
cf129f3a 2801 }
8e62a717
RH
2802
2803 /* Find the full program boundaries. */
2804 if (elf_prot & PROT_EXEC) {
2805 if (vaddr < info->start_code) {
2806 info->start_code = vaddr;
2807 }
2808 if (vaddr_ef > info->end_code) {
2809 info->end_code = vaddr_ef;
2810 }
2811 }
2812 if (elf_prot & PROT_WRITE) {
2813 if (vaddr < info->start_data) {
2814 info->start_data = vaddr;
2815 }
2816 if (vaddr_ef > info->end_data) {
2817 info->end_data = vaddr_ef;
2818 }
8a045188
TB
2819 }
2820 if (vaddr_em > info->brk) {
2821 info->brk = vaddr_em;
8e62a717 2822 }
5dd0db52
SM
2823#ifdef TARGET_MIPS
2824 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
2825 Mips_elf_abiflags_v0 abiflags;
2826 if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
c7f17e7b 2827 error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry");
5dd0db52
SM
2828 goto exit_errmsg;
2829 }
2830 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
2831 memcpy(&abiflags, bprm_buf + eppnt->p_offset,
2832 sizeof(Mips_elf_abiflags_v0));
2833 } else {
2834 retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
2835 eppnt->p_offset);
2836 if (retval != sizeof(Mips_elf_abiflags_v0)) {
c7f17e7b 2837 goto exit_read;
5dd0db52
SM
2838 }
2839 }
2840 bswap_mips_abiflags(&abiflags);
c94cb6c9 2841 info->fp_abi = abiflags.fp_abi;
5dd0db52 2842#endif
d97ef72e 2843 }
682674b8 2844 }
5fafdf24 2845
8e62a717
RH
2846 if (info->end_data == 0) {
2847 info->start_data = info->end_code;
2848 info->end_data = info->end_code;
8e62a717
RH
2849 }
2850
682674b8 2851 if (qemu_log_enabled()) {
8e62a717 2852 load_symbols(ehdr, image_fd, load_bias);
682674b8 2853 }
31e31b8a 2854
98c1076c
AB
2855 mmap_unlock();
2856
8e62a717
RH
2857 close(image_fd);
2858 return;
2859
2860 exit_read:
2861 if (retval >= 0) {
c7f17e7b
RH
2862 error_setg(&err, "Incomplete read of file header");
2863 } else {
2864 error_setg_errno(&err, errno, "Error reading file header");
8e62a717 2865 }
c7f17e7b
RH
2866 goto exit_errmsg;
2867 exit_mmap:
2868 error_setg_errno(&err, errno, "Error mapping file");
2869 goto exit_errmsg;
8e62a717 2870 exit_errmsg:
c7f17e7b 2871 error_reportf_err(err, "%s: ", image_name);
8e62a717
RH
2872 exit(-1);
2873}
2874
2875static void load_elf_interp(const char *filename, struct image_info *info,
2876 char bprm_buf[BPRM_BUF_SIZE])
2877{
2878 int fd, retval;
808f6563 2879 Error *err = NULL;
8e62a717
RH
2880
2881 fd = open(path(filename), O_RDONLY);
2882 if (fd < 0) {
808f6563
RH
2883 error_setg_file_open(&err, errno, filename);
2884 error_report_err(err);
2885 exit(-1);
8e62a717 2886 }
31e31b8a 2887
8e62a717
RH
2888 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
2889 if (retval < 0) {
808f6563
RH
2890 error_setg_errno(&err, errno, "Error reading file header");
2891 error_reportf_err(err, "%s: ", filename);
2892 exit(-1);
8e62a717 2893 }
808f6563 2894
8e62a717
RH
2895 if (retval < BPRM_BUF_SIZE) {
2896 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
2897 }
2898
bf858897 2899 load_elf_image(filename, fd, info, NULL, bprm_buf);
31e31b8a
FB
2900}
2901
49918a75
PB
2902static int symfind(const void *s0, const void *s1)
2903{
c7c530cd 2904 target_ulong addr = *(target_ulong *)s0;
49918a75
PB
2905 struct elf_sym *sym = (struct elf_sym *)s1;
2906 int result = 0;
c7c530cd 2907 if (addr < sym->st_value) {
49918a75 2908 result = -1;
c7c530cd 2909 } else if (addr >= sym->st_value + sym->st_size) {
49918a75
PB
2910 result = 1;
2911 }
2912 return result;
2913}
2914
2915static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
2916{
2917#if ELF_CLASS == ELFCLASS32
2918 struct elf_sym *syms = s->disas_symtab.elf32;
2919#else
2920 struct elf_sym *syms = s->disas_symtab.elf64;
2921#endif
2922
2923 // binary search
49918a75
PB
2924 struct elf_sym *sym;
2925
c7c530cd 2926 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
7cba04f6 2927 if (sym != NULL) {
49918a75
PB
2928 return s->disas_strtab + sym->st_name;
2929 }
2930
2931 return "";
2932}
2933
2934/* FIXME: This should use elf_ops.h */
2935static int symcmp(const void *s0, const void *s1)
2936{
2937 struct elf_sym *sym0 = (struct elf_sym *)s0;
2938 struct elf_sym *sym1 = (struct elf_sym *)s1;
2939 return (sym0->st_value < sym1->st_value)
2940 ? -1
2941 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
2942}
2943
689f936f 2944/* Best attempt to load symbols from this ELF object. */
682674b8 2945static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
689f936f 2946{
682674b8 2947 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
1e06262d 2948 uint64_t segsz;
682674b8 2949 struct elf_shdr *shdr;
b9475279
CV
2950 char *strings = NULL;
2951 struct syminfo *s = NULL;
2952 struct elf_sym *new_syms, *syms = NULL;
689f936f 2953
682674b8
RH
2954 shnum = hdr->e_shnum;
2955 i = shnum * sizeof(struct elf_shdr);
2956 shdr = (struct elf_shdr *)alloca(i);
2957 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
2958 return;
2959 }
2960
2961 bswap_shdr(shdr, shnum);
2962 for (i = 0; i < shnum; ++i) {
2963 if (shdr[i].sh_type == SHT_SYMTAB) {
2964 sym_idx = i;
2965 str_idx = shdr[i].sh_link;
49918a75
PB
2966 goto found;
2967 }
689f936f 2968 }
682674b8
RH
2969
2970 /* There will be no symbol table if the file was stripped. */
2971 return;
689f936f
FB
2972
2973 found:
682674b8 2974 /* Now know where the strtab and symtab are. Snarf them. */
0ef9ea29 2975 s = g_try_new(struct syminfo, 1);
682674b8 2976 if (!s) {
b9475279 2977 goto give_up;
682674b8 2978 }
5fafdf24 2979
1e06262d
PM
2980 segsz = shdr[str_idx].sh_size;
2981 s->disas_strtab = strings = g_try_malloc(segsz);
2982 if (!strings ||
2983 pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
b9475279 2984 goto give_up;
682674b8 2985 }
49918a75 2986
1e06262d
PM
2987 segsz = shdr[sym_idx].sh_size;
2988 syms = g_try_malloc(segsz);
2989 if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
b9475279 2990 goto give_up;
682674b8 2991 }
31e31b8a 2992
1e06262d
PM
2993 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
2994 /* Implausibly large symbol table: give up rather than ploughing
2995 * on with the number of symbols calculation overflowing
2996 */
2997 goto give_up;
2998 }
2999 nsyms = segsz / sizeof(struct elf_sym);
682674b8 3000 for (i = 0; i < nsyms; ) {
49918a75 3001 bswap_sym(syms + i);
682674b8
RH
3002 /* Throw away entries which we do not need. */
3003 if (syms[i].st_shndx == SHN_UNDEF
3004 || syms[i].st_shndx >= SHN_LORESERVE
3005 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3006 if (i < --nsyms) {
49918a75
PB
3007 syms[i] = syms[nsyms];
3008 }
682674b8 3009 } else {
49918a75 3010#if defined(TARGET_ARM) || defined (TARGET_MIPS)
682674b8
RH
3011 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3012 syms[i].st_value &= ~(target_ulong)1;
0774bed1 3013#endif
682674b8
RH
3014 syms[i].st_value += load_bias;
3015 i++;
3016 }
0774bed1 3017 }
49918a75 3018
b9475279
CV
3019 /* No "useful" symbol. */
3020 if (nsyms == 0) {
3021 goto give_up;
3022 }
3023
5d5c9930
RH
3024 /* Attempt to free the storage associated with the local symbols
3025 that we threw away. Whether or not this has any effect on the
3026 memory allocation depends on the malloc implementation and how
3027 many symbols we managed to discard. */
0ef9ea29 3028 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
8d79de6e 3029 if (new_syms == NULL) {
b9475279 3030 goto give_up;
5d5c9930 3031 }
8d79de6e 3032 syms = new_syms;
5d5c9930 3033
49918a75 3034 qsort(syms, nsyms, sizeof(*syms), symcmp);
689f936f 3035
49918a75
PB
3036 s->disas_num_syms = nsyms;
3037#if ELF_CLASS == ELFCLASS32
3038 s->disas_symtab.elf32 = syms;
49918a75
PB
3039#else
3040 s->disas_symtab.elf64 = syms;
49918a75 3041#endif
682674b8 3042 s->lookup_symbol = lookup_symbolxx;
e80cfcfc
FB
3043 s->next = syminfos;
3044 syminfos = s;
b9475279
CV
3045
3046 return;
3047
3048give_up:
0ef9ea29
PM
3049 g_free(s);
3050 g_free(strings);
3051 g_free(syms);
689f936f 3052}
31e31b8a 3053
768fe76e
YS
3054uint32_t get_elf_eflags(int fd)
3055{
3056 struct elfhdr ehdr;
3057 off_t offset;
3058 int ret;
3059
3060 /* Read ELF header */
3061 offset = lseek(fd, 0, SEEK_SET);
3062 if (offset == (off_t) -1) {
3063 return 0;
3064 }
3065 ret = read(fd, &ehdr, sizeof(ehdr));
3066 if (ret < sizeof(ehdr)) {
3067 return 0;
3068 }
3069 offset = lseek(fd, offset, SEEK_SET);
3070 if (offset == (off_t) -1) {
3071 return 0;
3072 }
3073
3074 /* Check ELF signature */
3075 if (!elf_check_ident(&ehdr)) {
3076 return 0;
3077 }
3078
3079 /* check header */
3080 bswap_ehdr(&ehdr);
3081 if (!elf_check_ehdr(&ehdr)) {
3082 return 0;
3083 }
3084
3085 /* return architecture id */
3086 return ehdr.e_flags;
3087}
3088
f0116c54 3089int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
31e31b8a 3090{
8e62a717 3091 struct image_info interp_info;
31e31b8a 3092 struct elfhdr elf_ex;
8e62a717 3093 char *elf_interpreter = NULL;
59baae9a 3094 char *scratch;
31e31b8a 3095
abcac736
DS
3096 memset(&interp_info, 0, sizeof(interp_info));
3097#ifdef TARGET_MIPS
3098 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3099#endif
3100
bf858897 3101 info->start_mmap = (abi_ulong)ELF_START_MMAP;
bf858897
RH
3102
3103 load_elf_image(bprm->filename, bprm->fd, info,
3104 &elf_interpreter, bprm->buf);
31e31b8a 3105
bf858897
RH
3106 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3107 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3108 when we load the interpreter. */
3109 elf_ex = *(struct elfhdr *)bprm->buf;
31e31b8a 3110
59baae9a
SB
3111 /* Do this so that we can load the interpreter, if need be. We will
3112 change some of these later */
3113 bprm->p = setup_arg_pages(bprm, info);
3114
3115 scratch = g_new0(char, TARGET_PAGE_SIZE);
7c4ee5bc
RH
3116 if (STACK_GROWS_DOWN) {
3117 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3118 bprm->p, info->stack_limit);
3119 info->file_string = bprm->p;
3120 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3121 bprm->p, info->stack_limit);
3122 info->env_strings = bprm->p;
3123 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3124 bprm->p, info->stack_limit);
3125 info->arg_strings = bprm->p;
3126 } else {
3127 info->arg_strings = bprm->p;
3128 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3129 bprm->p, info->stack_limit);
3130 info->env_strings = bprm->p;
3131 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3132 bprm->p, info->stack_limit);
3133 info->file_string = bprm->p;
3134 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3135 bprm->p, info->stack_limit);
3136 }
3137
59baae9a
SB
3138 g_free(scratch);
3139
e5fe0c52 3140 if (!bprm->p) {
bf858897
RH
3141 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3142 exit(-1);
379f6698 3143 }
379f6698 3144
8e62a717
RH
3145 if (elf_interpreter) {
3146 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
31e31b8a 3147
8e62a717
RH
3148 /* If the program interpreter is one of these two, then assume
3149 an iBCS2 image. Otherwise assume a native linux image. */
3150
3151 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3152 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3153 info->personality = PER_SVR4;
31e31b8a 3154
8e62a717
RH
3155 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3156 and some applications "depend" upon this behavior. Since
3157 we do not have the power to recompile these, we emulate
3158 the SVr4 behavior. Sigh. */
3159 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
68754b44 3160 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
8e62a717 3161 }
c94cb6c9
SM
3162#ifdef TARGET_MIPS
3163 info->interp_fp_abi = interp_info.fp_abi;
3164#endif
31e31b8a
FB
3165 }
3166
8e62a717
RH
3167 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
3168 info, (elf_interpreter ? &interp_info : NULL));
3169 info->start_stack = bprm->p;
3170
3171 /* If we have an interpreter, set that as the program's entry point.
8e78064e 3172 Copy the load_bias as well, to help PPC64 interpret the entry
8e62a717
RH
3173 point as a function descriptor. Do this after creating elf tables
3174 so that we copy the original program entry point into the AUXV. */
3175 if (elf_interpreter) {
8e78064e 3176 info->load_bias = interp_info.load_bias;
8e62a717 3177 info->entry = interp_info.entry;
2b323087 3178 g_free(elf_interpreter);
8e62a717 3179 }
31e31b8a 3180
edf8e2af
MW
3181#ifdef USE_ELF_CORE_DUMP
3182 bprm->core_dump = &elf_core_dump;
3183#endif
3184
6fd59449
RH
3185 /*
3186 * If we reserved extra space for brk, release it now.
3187 * The implementation of do_brk in syscalls.c expects to be able
3188 * to mmap pages in this space.
3189 */
3190 if (info->reserve_brk) {
3191 abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk);
3192 abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk);
3193 target_munmap(start_brk, end_brk - start_brk);
3194 }
3195
31e31b8a
FB
3196 return 0;
3197}
3198
edf8e2af 3199#ifdef USE_ELF_CORE_DUMP
edf8e2af
MW
3200/*
3201 * Definitions to generate Intel SVR4-like core files.
a2547a13 3202 * These mostly have the same names as the SVR4 types with "target_elf_"
edf8e2af
MW
3203 * tacked on the front to prevent clashes with linux definitions,
3204 * and the typedef forms have been avoided. This is mostly like
3205 * the SVR4 structure, but more Linuxy, with things that Linux does
3206 * not support and which gdb doesn't really use excluded.
3207 *
3208 * Fields we don't dump (their contents is zero) in linux-user qemu
3209 * are marked with XXX.
3210 *
3211 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3212 *
3213 * Porting ELF coredump for target is (quite) simple process. First you
dd0a3651 3214 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
edf8e2af
MW
3215 * the target resides):
3216 *
3217 * #define USE_ELF_CORE_DUMP
3218 *
3219 * Next you define type of register set used for dumping. ELF specification
3220 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3221 *
c227f099 3222 * typedef <target_regtype> target_elf_greg_t;
edf8e2af 3223 * #define ELF_NREG <number of registers>
c227f099 3224 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
edf8e2af 3225 *
edf8e2af
MW
3226 * Last step is to implement target specific function that copies registers
3227 * from given cpu into just specified register set. Prototype is:
3228 *
c227f099 3229 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
9349b4f9 3230 * const CPUArchState *env);
edf8e2af
MW
3231 *
3232 * Parameters:
3233 * regs - copy register values into here (allocated and zeroed by caller)
3234 * env - copy registers from here
3235 *
3236 * Example for ARM target is provided in this file.
3237 */
3238
3239/* An ELF note in memory */
3240struct memelfnote {
3241 const char *name;
3242 size_t namesz;
3243 size_t namesz_rounded;
3244 int type;
3245 size_t datasz;
80f5ce75 3246 size_t datasz_rounded;
edf8e2af
MW
3247 void *data;
3248 size_t notesz;
3249};
3250
a2547a13 3251struct target_elf_siginfo {
f8fd4fc4
PB
3252 abi_int si_signo; /* signal number */
3253 abi_int si_code; /* extra code */
3254 abi_int si_errno; /* errno */
edf8e2af
MW
3255};
3256
a2547a13
LD
3257struct target_elf_prstatus {
3258 struct target_elf_siginfo pr_info; /* Info associated with signal */
1ddd592f 3259 abi_short pr_cursig; /* Current signal */
ca98ac83
PB
3260 abi_ulong pr_sigpend; /* XXX */
3261 abi_ulong pr_sighold; /* XXX */
c227f099
AL
3262 target_pid_t pr_pid;
3263 target_pid_t pr_ppid;
3264 target_pid_t pr_pgrp;
3265 target_pid_t pr_sid;
edf8e2af
MW
3266 struct target_timeval pr_utime; /* XXX User time */
3267 struct target_timeval pr_stime; /* XXX System time */
3268 struct target_timeval pr_cutime; /* XXX Cumulative user time */
3269 struct target_timeval pr_cstime; /* XXX Cumulative system time */
c227f099 3270 target_elf_gregset_t pr_reg; /* GP registers */
f8fd4fc4 3271 abi_int pr_fpvalid; /* XXX */
edf8e2af
MW
3272};
3273
3274#define ELF_PRARGSZ (80) /* Number of chars for args */
3275
a2547a13 3276struct target_elf_prpsinfo {
edf8e2af
MW
3277 char pr_state; /* numeric process state */
3278 char pr_sname; /* char for pr_state */
3279 char pr_zomb; /* zombie */
3280 char pr_nice; /* nice val */
ca98ac83 3281 abi_ulong pr_flag; /* flags */
c227f099
AL
3282 target_uid_t pr_uid;
3283 target_gid_t pr_gid;
3284 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
edf8e2af 3285 /* Lots missing */
d7eb2b92 3286 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
edf8e2af
MW
3287 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
3288};
3289
3290/* Here is the structure in which status of each thread is captured. */
3291struct elf_thread_status {
72cf2d4f 3292 QTAILQ_ENTRY(elf_thread_status) ets_link;
a2547a13 3293 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
edf8e2af
MW
3294#if 0
3295 elf_fpregset_t fpu; /* NT_PRFPREG */
3296 struct task_struct *thread;
3297 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
3298#endif
3299 struct memelfnote notes[1];
3300 int num_notes;
3301};
3302
3303struct elf_note_info {
3304 struct memelfnote *notes;
a2547a13
LD
3305 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
3306 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
edf8e2af 3307
b58deb34 3308 QTAILQ_HEAD(, elf_thread_status) thread_list;
edf8e2af
MW
3309#if 0
3310 /*
3311 * Current version of ELF coredump doesn't support
3312 * dumping fp regs etc.
3313 */
3314 elf_fpregset_t *fpu;
3315 elf_fpxregset_t *xfpu;
3316 int thread_status_size;
3317#endif
3318 int notes_size;
3319 int numnote;
3320};
3321
3322struct vm_area_struct {
1a1c4db9
MI
3323 target_ulong vma_start; /* start vaddr of memory region */
3324 target_ulong vma_end; /* end vaddr of memory region */
3325 abi_ulong vma_flags; /* protection etc. flags for the region */
72cf2d4f 3326 QTAILQ_ENTRY(vm_area_struct) vma_link;
edf8e2af
MW
3327};
3328
3329struct mm_struct {
72cf2d4f 3330 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
edf8e2af
MW
3331 int mm_count; /* number of mappings */
3332};
3333
3334static struct mm_struct *vma_init(void);
3335static void vma_delete(struct mm_struct *);
1a1c4db9
MI
3336static int vma_add_mapping(struct mm_struct *, target_ulong,
3337 target_ulong, abi_ulong);
edf8e2af
MW
3338static int vma_get_mapping_count(const struct mm_struct *);
3339static struct vm_area_struct *vma_first(const struct mm_struct *);
3340static struct vm_area_struct *vma_next(struct vm_area_struct *);
3341static abi_ulong vma_dump_size(const struct vm_area_struct *);
1a1c4db9 3342static int vma_walker(void *priv, target_ulong start, target_ulong end,
d97ef72e 3343 unsigned long flags);
edf8e2af
MW
3344
3345static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
3346static void fill_note(struct memelfnote *, const char *, int,
d97ef72e 3347 unsigned int, void *);
a2547a13
LD
3348static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
3349static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
edf8e2af
MW
3350static void fill_auxv_note(struct memelfnote *, const TaskState *);
3351static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
3352static size_t note_size(const struct memelfnote *);
3353static void free_note_info(struct elf_note_info *);
9349b4f9
AF
3354static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
3355static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
edf8e2af
MW
3356static int core_dump_filename(const TaskState *, char *, size_t);
3357
3358static int dump_write(int, const void *, size_t);
3359static int write_note(struct memelfnote *, int);
3360static int write_note_info(struct elf_note_info *, int);
3361
3362#ifdef BSWAP_NEEDED
a2547a13 3363static void bswap_prstatus(struct target_elf_prstatus *prstatus)
edf8e2af 3364{
ca98ac83
PB
3365 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
3366 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
3367 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
edf8e2af 3368 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
ca98ac83
PB
3369 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
3370 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
edf8e2af
MW
3371 prstatus->pr_pid = tswap32(prstatus->pr_pid);
3372 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
3373 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
3374 prstatus->pr_sid = tswap32(prstatus->pr_sid);
3375 /* cpu times are not filled, so we skip them */
3376 /* regs should be in correct format already */
3377 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
3378}
3379
a2547a13 3380static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
edf8e2af 3381{
ca98ac83 3382 psinfo->pr_flag = tswapal(psinfo->pr_flag);
edf8e2af
MW
3383 psinfo->pr_uid = tswap16(psinfo->pr_uid);
3384 psinfo->pr_gid = tswap16(psinfo->pr_gid);
3385 psinfo->pr_pid = tswap32(psinfo->pr_pid);
3386 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
3387 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
3388 psinfo->pr_sid = tswap32(psinfo->pr_sid);
3389}
991f8f0c
RH
3390
3391static void bswap_note(struct elf_note *en)
3392{
3393 bswap32s(&en->n_namesz);
3394 bswap32s(&en->n_descsz);
3395 bswap32s(&en->n_type);
3396}
3397#else
3398static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
3399static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
3400static inline void bswap_note(struct elf_note *en) { }
edf8e2af
MW
3401#endif /* BSWAP_NEEDED */
3402
3403/*
3404 * Minimal support for linux memory regions. These are needed
3405 * when we are finding out what memory exactly belongs to
3406 * emulated process. No locks needed here, as long as
3407 * thread that received the signal is stopped.
3408 */
3409
3410static struct mm_struct *vma_init(void)
3411{
3412 struct mm_struct *mm;
3413
7267c094 3414 if ((mm = g_malloc(sizeof (*mm))) == NULL)
edf8e2af
MW
3415 return (NULL);
3416
3417 mm->mm_count = 0;
72cf2d4f 3418 QTAILQ_INIT(&mm->mm_mmap);
edf8e2af
MW
3419
3420 return (mm);
3421}
3422
3423static void vma_delete(struct mm_struct *mm)
3424{
3425 struct vm_area_struct *vma;
3426
3427 while ((vma = vma_first(mm)) != NULL) {
72cf2d4f 3428 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
7267c094 3429 g_free(vma);
edf8e2af 3430 }
7267c094 3431 g_free(mm);
edf8e2af
MW
3432}
3433
1a1c4db9
MI
3434static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
3435 target_ulong end, abi_ulong flags)
edf8e2af
MW
3436{
3437 struct vm_area_struct *vma;
3438
7267c094 3439 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
edf8e2af
MW
3440 return (-1);
3441
3442 vma->vma_start = start;
3443 vma->vma_end = end;
3444 vma->vma_flags = flags;
3445
72cf2d4f 3446 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
edf8e2af
MW
3447 mm->mm_count++;
3448
3449 return (0);
3450}
3451
3452static struct vm_area_struct *vma_first(const struct mm_struct *mm)
3453{
72cf2d4f 3454 return (QTAILQ_FIRST(&mm->mm_mmap));
edf8e2af
MW
3455}
3456
3457static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
3458{
72cf2d4f 3459 return (QTAILQ_NEXT(vma, vma_link));
edf8e2af
MW
3460}
3461
3462static int vma_get_mapping_count(const struct mm_struct *mm)
3463{
3464 return (mm->mm_count);
3465}
3466
3467/*
3468 * Calculate file (dump) size of given memory region.
3469 */
3470static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
3471{
3472 /* if we cannot even read the first page, skip it */
3473 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
3474 return (0);
3475
3476 /*
3477 * Usually we don't dump executable pages as they contain
3478 * non-writable code that debugger can read directly from
3479 * target library etc. However, thread stacks are marked
3480 * also executable so we read in first page of given region
3481 * and check whether it contains elf header. If there is
3482 * no elf header, we dump it.
3483 */
3484 if (vma->vma_flags & PROT_EXEC) {
3485 char page[TARGET_PAGE_SIZE];
3486
3487 copy_from_user(page, vma->vma_start, sizeof (page));
3488 if ((page[EI_MAG0] == ELFMAG0) &&
3489 (page[EI_MAG1] == ELFMAG1) &&
3490 (page[EI_MAG2] == ELFMAG2) &&
3491 (page[EI_MAG3] == ELFMAG3)) {
3492 /*
3493 * Mappings are possibly from ELF binary. Don't dump
3494 * them.
3495 */
3496 return (0);
3497 }
3498 }
3499
3500 return (vma->vma_end - vma->vma_start);
3501}
3502
1a1c4db9 3503static int vma_walker(void *priv, target_ulong start, target_ulong end,
d97ef72e 3504 unsigned long flags)
edf8e2af
MW
3505{
3506 struct mm_struct *mm = (struct mm_struct *)priv;
3507
edf8e2af
MW
3508 vma_add_mapping(mm, start, end, flags);
3509 return (0);
3510}
3511
3512static void fill_note(struct memelfnote *note, const char *name, int type,
d97ef72e 3513 unsigned int sz, void *data)
edf8e2af
MW
3514{
3515 unsigned int namesz;
3516
3517 namesz = strlen(name) + 1;
3518 note->name = name;
3519 note->namesz = namesz;
3520 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
3521 note->type = type;
80f5ce75
LV
3522 note->datasz = sz;
3523 note->datasz_rounded = roundup(sz, sizeof (int32_t));
3524
edf8e2af
MW
3525 note->data = data;
3526
3527 /*
3528 * We calculate rounded up note size here as specified by
3529 * ELF document.
3530 */
3531 note->notesz = sizeof (struct elf_note) +
80f5ce75 3532 note->namesz_rounded + note->datasz_rounded;
edf8e2af
MW
3533}
3534
3535static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
d97ef72e 3536 uint32_t flags)
edf8e2af
MW
3537{
3538 (void) memset(elf, 0, sizeof(*elf));
3539
3540 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
3541 elf->e_ident[EI_CLASS] = ELF_CLASS;
3542 elf->e_ident[EI_DATA] = ELF_DATA;
3543 elf->e_ident[EI_VERSION] = EV_CURRENT;
3544 elf->e_ident[EI_OSABI] = ELF_OSABI;
3545
3546 elf->e_type = ET_CORE;
3547 elf->e_machine = machine;
3548 elf->e_version = EV_CURRENT;
3549 elf->e_phoff = sizeof(struct elfhdr);
3550 elf->e_flags = flags;
3551 elf->e_ehsize = sizeof(struct elfhdr);
3552 elf->e_phentsize = sizeof(struct elf_phdr);
3553 elf->e_phnum = segs;
3554
edf8e2af 3555 bswap_ehdr(elf);
edf8e2af
MW
3556}
3557
3558static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
3559{
3560 phdr->p_type = PT_NOTE;
3561 phdr->p_offset = offset;
3562 phdr->p_vaddr = 0;
3563 phdr->p_paddr = 0;
3564 phdr->p_filesz = sz;
3565 phdr->p_memsz = 0;
3566 phdr->p_flags = 0;
3567 phdr->p_align = 0;
3568
991f8f0c 3569 bswap_phdr(phdr, 1);
edf8e2af
MW
3570}
3571
3572static size_t note_size(const struct memelfnote *note)
3573{
3574 return (note->notesz);
3575}
3576
a2547a13 3577static void fill_prstatus(struct target_elf_prstatus *prstatus,
d97ef72e 3578 const TaskState *ts, int signr)
edf8e2af
MW
3579{
3580 (void) memset(prstatus, 0, sizeof (*prstatus));
3581 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
3582 prstatus->pr_pid = ts->ts_tid;
3583 prstatus->pr_ppid = getppid();
3584 prstatus->pr_pgrp = getpgrp();
3585 prstatus->pr_sid = getsid(0);
3586
edf8e2af 3587 bswap_prstatus(prstatus);
edf8e2af
MW
3588}
3589
a2547a13 3590static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
edf8e2af 3591{
900cfbca 3592 char *base_filename;
edf8e2af
MW
3593 unsigned int i, len;
3594
3595 (void) memset(psinfo, 0, sizeof (*psinfo));
3596
3597 len = ts->info->arg_end - ts->info->arg_start;
3598 if (len >= ELF_PRARGSZ)
3599 len = ELF_PRARGSZ - 1;
3600 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
3601 return -EFAULT;
3602 for (i = 0; i < len; i++)
3603 if (psinfo->pr_psargs[i] == 0)
3604 psinfo->pr_psargs[i] = ' ';
3605 psinfo->pr_psargs[len] = 0;
3606
3607 psinfo->pr_pid = getpid();
3608 psinfo->pr_ppid = getppid();
3609 psinfo->pr_pgrp = getpgrp();
3610 psinfo->pr_sid = getsid(0);
3611 psinfo->pr_uid = getuid();
3612 psinfo->pr_gid = getgid();
3613
900cfbca
JM
3614 base_filename = g_path_get_basename(ts->bprm->filename);
3615 /*
3616 * Using strncpy here is fine: at max-length,
3617 * this field is not NUL-terminated.
3618 */
edf8e2af 3619 (void) strncpy(psinfo->pr_fname, base_filename,
d97ef72e 3620 sizeof(psinfo->pr_fname));
edf8e2af 3621
900cfbca 3622 g_free(base_filename);
edf8e2af 3623 bswap_psinfo(psinfo);
edf8e2af
MW
3624 return (0);
3625}
3626
3627static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
3628{
3629 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
3630 elf_addr_t orig_auxv = auxv;
edf8e2af 3631 void *ptr;
125b0f55 3632 int len = ts->info->auxv_len;
edf8e2af
MW
3633
3634 /*
3635 * Auxiliary vector is stored in target process stack. It contains
3636 * {type, value} pairs that we need to dump into note. This is not
3637 * strictly necessary but we do it here for sake of completeness.
3638 */
3639
edf8e2af
MW
3640 /* read in whole auxv vector and copy it to memelfnote */
3641 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
3642 if (ptr != NULL) {
3643 fill_note(note, "CORE", NT_AUXV, len, ptr);
3644 unlock_user(ptr, auxv, len);
3645 }
3646}
3647
3648/*
3649 * Constructs name of coredump file. We have following convention
3650 * for the name:
3651 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3652 *
3653 * Returns 0 in case of success, -1 otherwise (errno is set).
3654 */
3655static int core_dump_filename(const TaskState *ts, char *buf,
d97ef72e 3656 size_t bufsize)
edf8e2af
MW
3657{
3658 char timestamp[64];
edf8e2af
MW
3659 char *base_filename = NULL;
3660 struct timeval tv;
3661 struct tm tm;
3662
3663 assert(bufsize >= PATH_MAX);
3664
3665 if (gettimeofday(&tv, NULL) < 0) {
3666 (void) fprintf(stderr, "unable to get current timestamp: %s",
d97ef72e 3667 strerror(errno));
edf8e2af
MW
3668 return (-1);
3669 }
3670
b8da57fa 3671 base_filename = g_path_get_basename(ts->bprm->filename);
edf8e2af 3672 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
d97ef72e 3673 localtime_r(&tv.tv_sec, &tm));
edf8e2af 3674 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
d97ef72e 3675 base_filename, timestamp, (int)getpid());
b8da57fa 3676 g_free(base_filename);
edf8e2af
MW
3677
3678 return (0);
3679}
3680
3681static int dump_write(int fd, const void *ptr, size_t size)
3682{
3683 const char *bufp = (const char *)ptr;
3684 ssize_t bytes_written, bytes_left;
3685 struct rlimit dumpsize;
3686 off_t pos;
3687
3688 bytes_written = 0;
3689 getrlimit(RLIMIT_CORE, &dumpsize);
3690 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
3691 if (errno == ESPIPE) { /* not a seekable stream */
3692 bytes_left = size;
3693 } else {
3694 return pos;
3695 }
3696 } else {
3697 if (dumpsize.rlim_cur <= pos) {
3698 return -1;
3699 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
3700 bytes_left = size;
3701 } else {
3702 size_t limit_left=dumpsize.rlim_cur - pos;
3703 bytes_left = limit_left >= size ? size : limit_left ;
3704 }
3705 }
3706
3707 /*
3708 * In normal conditions, single write(2) should do but
3709 * in case of socket etc. this mechanism is more portable.
3710 */
3711 do {
3712 bytes_written = write(fd, bufp, bytes_left);
3713 if (bytes_written < 0) {
3714 if (errno == EINTR)
3715 continue;
3716 return (-1);
3717 } else if (bytes_written == 0) { /* eof */
3718 return (-1);
3719 }
3720 bufp += bytes_written;
3721 bytes_left -= bytes_written;
3722 } while (bytes_left > 0);
3723
3724 return (0);
3725}
3726
3727static int write_note(struct memelfnote *men, int fd)
3728{
3729 struct elf_note en;
3730
3731 en.n_namesz = men->namesz;
3732 en.n_type = men->type;
3733 en.n_descsz = men->datasz;
3734
edf8e2af 3735 bswap_note(&en);
edf8e2af
MW
3736
3737 if (dump_write(fd, &en, sizeof(en)) != 0)
3738 return (-1);
3739 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
3740 return (-1);
80f5ce75 3741 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
edf8e2af
MW
3742 return (-1);
3743
3744 return (0);
3745}
3746
9349b4f9 3747static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
edf8e2af 3748{
29a0af61 3749 CPUState *cpu = env_cpu((CPUArchState *)env);
0429a971 3750 TaskState *ts = (TaskState *)cpu->opaque;
edf8e2af
MW
3751 struct elf_thread_status *ets;
3752
7267c094 3753 ets = g_malloc0(sizeof (*ets));
edf8e2af
MW
3754 ets->num_notes = 1; /* only prstatus is dumped */
3755 fill_prstatus(&ets->prstatus, ts, 0);
3756 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
3757 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
d97ef72e 3758 &ets->prstatus);
edf8e2af 3759
72cf2d4f 3760 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
edf8e2af
MW
3761
3762 info->notes_size += note_size(&ets->notes[0]);
3763}
3764
6afafa86
PM
3765static void init_note_info(struct elf_note_info *info)
3766{
3767 /* Initialize the elf_note_info structure so that it is at
3768 * least safe to call free_note_info() on it. Must be
3769 * called before calling fill_note_info().
3770 */
3771 memset(info, 0, sizeof (*info));
3772 QTAILQ_INIT(&info->thread_list);
3773}
3774
edf8e2af 3775static int fill_note_info(struct elf_note_info *info,
9349b4f9 3776 long signr, const CPUArchState *env)
edf8e2af
MW
3777{
3778#define NUMNOTES 3
29a0af61 3779 CPUState *cpu = env_cpu((CPUArchState *)env);
0429a971 3780 TaskState *ts = (TaskState *)cpu->opaque;
edf8e2af
MW
3781 int i;
3782
c78d65e8 3783 info->notes = g_new0(struct memelfnote, NUMNOTES);
edf8e2af
MW
3784 if (info->notes == NULL)
3785 return (-ENOMEM);
7267c094 3786 info->prstatus = g_malloc0(sizeof (*info->prstatus));
edf8e2af
MW
3787 if (info->prstatus == NULL)
3788 return (-ENOMEM);
7267c094 3789 info->psinfo = g_malloc0(sizeof (*info->psinfo));
edf8e2af
MW
3790 if (info->prstatus == NULL)
3791 return (-ENOMEM);
3792
3793 /*
3794 * First fill in status (and registers) of current thread
3795 * including process info & aux vector.
3796 */
3797 fill_prstatus(info->prstatus, ts, signr);
3798 elf_core_copy_regs(&info->prstatus->pr_reg, env);
3799 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
d97ef72e 3800 sizeof (*info->prstatus), info->prstatus);
edf8e2af
MW
3801 fill_psinfo(info->psinfo, ts);
3802 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
d97ef72e 3803 sizeof (*info->psinfo), info->psinfo);
edf8e2af
MW
3804 fill_auxv_note(&info->notes[2], ts);
3805 info->numnote = 3;
3806
3807 info->notes_size = 0;
3808 for (i = 0; i < info->numnote; i++)
3809 info->notes_size += note_size(&info->notes[i]);
3810
3811 /* read and fill status of all threads */
3812 cpu_list_lock();
bdc44640 3813 CPU_FOREACH(cpu) {
a2247f8e 3814 if (cpu == thread_cpu) {
edf8e2af 3815 continue;
182735ef
AF
3816 }
3817 fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
edf8e2af
MW
3818 }
3819 cpu_list_unlock();
3820
3821 return (0);
3822}
3823
3824static void free_note_info(struct elf_note_info *info)
3825{
3826 struct elf_thread_status *ets;
3827
72cf2d4f
BS
3828 while (!QTAILQ_EMPTY(&info->thread_list)) {
3829 ets = QTAILQ_FIRST(&info->thread_list);
3830 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
7267c094 3831 g_free(ets);
edf8e2af
MW
3832 }
3833
7267c094
AL
3834 g_free(info->prstatus);
3835 g_free(info->psinfo);
3836 g_free(info->notes);
edf8e2af
MW
3837}
3838
3839static int write_note_info(struct elf_note_info *info, int fd)
3840{
3841 struct elf_thread_status *ets;
3842 int i, error = 0;
3843
3844 /* write prstatus, psinfo and auxv for current thread */
3845 for (i = 0; i < info->numnote; i++)
3846 if ((error = write_note(&info->notes[i], fd)) != 0)
3847 return (error);
3848
3849 /* write prstatus for each thread */
52a53afe 3850 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
edf8e2af
MW
3851 if ((error = write_note(&ets->notes[0], fd)) != 0)
3852 return (error);
3853 }
3854
3855 return (0);
3856}
3857
3858/*
3859 * Write out ELF coredump.
3860 *
3861 * See documentation of ELF object file format in:
3862 * http://www.caldera.com/developers/devspecs/gabi41.pdf
3863 *
3864 * Coredump format in linux is following:
3865 *
3866 * 0 +----------------------+ \
3867 * | ELF header | ET_CORE |
3868 * +----------------------+ |
3869 * | ELF program headers | |--- headers
3870 * | - NOTE section | |
3871 * | - PT_LOAD sections | |
3872 * +----------------------+ /
3873 * | NOTEs: |
3874 * | - NT_PRSTATUS |
3875 * | - NT_PRSINFO |
3876 * | - NT_AUXV |
3877 * +----------------------+ <-- aligned to target page
3878 * | Process memory dump |
3879 * : :
3880 * . .
3881 * : :
3882 * | |
3883 * +----------------------+
3884 *
3885 * NT_PRSTATUS -> struct elf_prstatus (per thread)
3886 * NT_PRSINFO -> struct elf_prpsinfo
3887 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
3888 *
3889 * Format follows System V format as close as possible. Current
3890 * version limitations are as follows:
3891 * - no floating point registers are dumped
3892 *
3893 * Function returns 0 in case of success, negative errno otherwise.
3894 *
3895 * TODO: make this work also during runtime: it should be
3896 * possible to force coredump from running process and then
3897 * continue processing. For example qemu could set up SIGUSR2
3898 * handler (provided that target process haven't registered
3899 * handler for that) that does the dump when signal is received.
3900 */
9349b4f9 3901static int elf_core_dump(int signr, const CPUArchState *env)
edf8e2af 3902{
29a0af61 3903 const CPUState *cpu = env_cpu((CPUArchState *)env);
0429a971 3904 const TaskState *ts = (const TaskState *)cpu->opaque;
edf8e2af
MW
3905 struct vm_area_struct *vma = NULL;
3906 char corefile[PATH_MAX];
3907 struct elf_note_info info;
3908 struct elfhdr elf;
3909 struct elf_phdr phdr;
3910 struct rlimit dumpsize;
3911 struct mm_struct *mm = NULL;
3912 off_t offset = 0, data_offset = 0;
3913 int segs = 0;
3914 int fd = -1;
3915
6afafa86
PM
3916 init_note_info(&info);
3917
edf8e2af
MW
3918 errno = 0;
3919 getrlimit(RLIMIT_CORE, &dumpsize);
3920 if (dumpsize.rlim_cur == 0)
d97ef72e 3921 return 0;
edf8e2af
MW
3922
3923 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
3924 return (-errno);
3925
3926 if ((fd = open(corefile, O_WRONLY | O_CREAT,
d97ef72e 3927 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
edf8e2af
MW
3928 return (-errno);
3929
3930 /*
3931 * Walk through target process memory mappings and
3932 * set up structure containing this information. After
3933 * this point vma_xxx functions can be used.
3934 */
3935 if ((mm = vma_init()) == NULL)
3936 goto out;
3937
3938 walk_memory_regions(mm, vma_walker);
3939 segs = vma_get_mapping_count(mm);
3940
3941 /*
3942 * Construct valid coredump ELF header. We also
3943 * add one more segment for notes.
3944 */
3945 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
3946 if (dump_write(fd, &elf, sizeof (elf)) != 0)
3947 goto out;
3948
b6af0975 3949 /* fill in the in-memory version of notes */
edf8e2af
MW
3950 if (fill_note_info(&info, signr, env) < 0)
3951 goto out;
3952
3953 offset += sizeof (elf); /* elf header */
3954 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
3955
3956 /* write out notes program header */
3957 fill_elf_note_phdr(&phdr, info.notes_size, offset);
3958
3959 offset += info.notes_size;
3960 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
3961 goto out;
3962
3963 /*
3964 * ELF specification wants data to start at page boundary so
3965 * we align it here.
3966 */
80f5ce75 3967 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
edf8e2af
MW
3968
3969 /*
3970 * Write program headers for memory regions mapped in
3971 * the target process.
3972 */
3973 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
3974 (void) memset(&phdr, 0, sizeof (phdr));
3975
3976 phdr.p_type = PT_LOAD;
3977 phdr.p_offset = offset;
3978 phdr.p_vaddr = vma->vma_start;
3979 phdr.p_paddr = 0;
3980 phdr.p_filesz = vma_dump_size(vma);
3981 offset += phdr.p_filesz;
3982 phdr.p_memsz = vma->vma_end - vma->vma_start;
3983 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
3984 if (vma->vma_flags & PROT_WRITE)
3985 phdr.p_flags |= PF_W;
3986 if (vma->vma_flags & PROT_EXEC)
3987 phdr.p_flags |= PF_X;
3988 phdr.p_align = ELF_EXEC_PAGESIZE;
3989
80f5ce75 3990 bswap_phdr(&phdr, 1);
772034b6
PM
3991 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
3992 goto out;
3993 }
edf8e2af
MW
3994 }
3995
3996 /*
3997 * Next we write notes just after program headers. No
3998 * alignment needed here.
3999 */
4000 if (write_note_info(&info, fd) < 0)
4001 goto out;
4002
4003 /* align data to page boundary */
edf8e2af
MW
4004 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4005 goto out;
4006
4007 /*
4008 * Finally we can dump process memory into corefile as well.
4009 */
4010 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4011 abi_ulong addr;
4012 abi_ulong end;
4013
4014 end = vma->vma_start + vma_dump_size(vma);
4015
4016 for (addr = vma->vma_start; addr < end;
d97ef72e 4017 addr += TARGET_PAGE_SIZE) {
edf8e2af
MW
4018 char page[TARGET_PAGE_SIZE];
4019 int error;
4020
4021 /*
4022 * Read in page from target process memory and
4023 * write it to coredump file.
4024 */
4025 error = copy_from_user(page, addr, sizeof (page));
4026 if (error != 0) {
49995e17 4027 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
d97ef72e 4028 addr);
edf8e2af
MW
4029 errno = -error;
4030 goto out;
4031 }
4032 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4033 goto out;
4034 }
4035 }
4036
d97ef72e 4037 out:
edf8e2af
MW
4038 free_note_info(&info);
4039 if (mm != NULL)
4040 vma_delete(mm);
4041 (void) close(fd);
4042
4043 if (errno != 0)
4044 return (-errno);
4045 return (0);
4046}
edf8e2af
MW
4047#endif /* USE_ELF_CORE_DUMP */
4048
e5fe0c52
PB
4049void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4050{
4051 init_thread(regs, infop);
4052}