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