blob: ddef23e6dcb87bf6bb3e826b7a201b2fa8753104 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/* This is the Linux kernel elf-loading code, ported into user space */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002#include <sys/time.h>
3#include <sys/param.h>
bellard31e31b82003-02-18 22:55:36 +00004
5#include <stdio.h>
6#include <sys/types.h>
7#include <fcntl.h>
bellard31e31b82003-02-18 22:55:36 +00008#include <errno.h>
9#include <unistd.h>
10#include <sys/mman.h>
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030011#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000012#include <stdlib.h>
13#include <string.h>
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030014#include <time.h>
bellard31e31b82003-02-18 22:55:36 +000015
bellard3ef693a2003-03-23 20:17:16 +000016#include "qemu.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020017#include "disas/disas.h"
bellard31e31b82003-02-18 22:55:36 +000018
malce58ffeb2009-01-14 18:39:49 +000019#ifdef _ARCH_PPC64
malca6cc84f2008-08-20 22:39:28 +000020#undef ARCH_DLINFO
21#undef ELF_PLATFORM
22#undef ELF_HWCAP
23#undef ELF_CLASS
24#undef ELF_DATA
25#undef ELF_ARCH
26#endif
27
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030028#define ELF_OSABI ELFOSABI_SYSV
29
blueswir1cb33da52007-10-09 16:34:29 +000030/* from personality.h */
31
32/*
33 * Flags for bug emulation.
34 *
35 * These occupy the top three bytes.
36 */
37enum {
Richard Hendersond97ef722010-07-27 10:25:29 -070038 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
39 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
40 descriptors (signal handling) */
41 MMAP_PAGE_ZERO = 0x0100000,
42 ADDR_COMPAT_LAYOUT = 0x0200000,
43 READ_IMPLIES_EXEC = 0x0400000,
44 ADDR_LIMIT_32BIT = 0x0800000,
45 SHORT_INODE = 0x1000000,
46 WHOLE_SECONDS = 0x2000000,
47 STICKY_TIMEOUTS = 0x4000000,
48 ADDR_LIMIT_3GB = 0x8000000,
blueswir1cb33da52007-10-09 16:34:29 +000049};
50
51/*
52 * Personality types.
53 *
54 * These go in the low byte. Avoid using the top bit, it will
55 * conflict with error returns.
56 */
57enum {
Richard Hendersond97ef722010-07-27 10:25:29 -070058 PER_LINUX = 0x0000,
59 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
60 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
61 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
62 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
63 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
64 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
65 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
66 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
67 PER_BSD = 0x0006,
68 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
69 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
70 PER_LINUX32 = 0x0008,
71 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
72 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
73 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
74 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
75 PER_RISCOS = 0x000c,
76 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
77 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
78 PER_OSF4 = 0x000f, /* OSF/1 v4 */
79 PER_HPUX = 0x0010,
80 PER_MASK = 0x00ff,
blueswir1cb33da52007-10-09 16:34:29 +000081};
82
83/*
84 * Return the base personality without flags.
85 */
Richard Hendersond97ef722010-07-27 10:25:29 -070086#define personality(pers) (pers & PER_MASK)
blueswir1cb33da52007-10-09 16:34:29 +000087
bellard83fb7ad2004-07-05 21:25:26 +000088/* this flag is uneffective under linux too, should be deleted */
89#ifndef MAP_DENYWRITE
90#define MAP_DENYWRITE 0
91#endif
92
93/* should probably go in elf.h */
94#ifndef ELIBBAD
95#define ELIBBAD 80
96#endif
97
Richard Henderson28490232010-07-27 10:25:31 -070098#ifdef TARGET_WORDS_BIGENDIAN
99#define ELF_DATA ELFDATA2MSB
100#else
101#define ELF_DATA ELFDATA2LSB
102#endif
103
Paolo Bonzinia29f9982013-04-17 16:26:37 +0200104#ifdef TARGET_ABI_MIPSN32
Paolo Bonzini918fc542013-04-17 16:26:42 +0200105typedef abi_ullong target_elf_greg_t;
106#define tswapreg(ptr) tswap64(ptr)
Paolo Bonzinia29f9982013-04-17 16:26:37 +0200107#else
108typedef abi_ulong target_elf_greg_t;
109#define tswapreg(ptr) tswapal(ptr)
110#endif
111
Nathan Froyd21e807f2009-12-11 09:04:46 -0800112#ifdef USE_UID16
Paolo Bonzini1ddd5922013-04-17 16:26:39 +0200113typedef abi_ushort target_uid_t;
114typedef abi_ushort target_gid_t;
Nathan Froyd21e807f2009-12-11 09:04:46 -0800115#else
Paolo Bonzinif8fd4fc2013-04-17 16:26:40 +0200116typedef abi_uint target_uid_t;
117typedef abi_uint target_gid_t;
Nathan Froyd21e807f2009-12-11 09:04:46 -0800118#endif
Paolo Bonzinif8fd4fc2013-04-17 16:26:40 +0200119typedef abi_int target_pid_t;
Nathan Froyd21e807f2009-12-11 09:04:46 -0800120
bellard30ac07d2003-04-07 21:33:03 +0000121#ifdef TARGET_I386
122
bellard15338fd2005-11-26 11:41:16 +0000123#define ELF_PLATFORM get_elf_platform()
124
125static const char *get_elf_platform(void)
126{
127 static char elf_platform[] = "i386";
pbrookd5975362008-06-07 20:50:51 +0000128 int family = (thread_env->cpuid_version >> 8) & 0xff;
bellard15338fd2005-11-26 11:41:16 +0000129 if (family > 6)
130 family = 6;
131 if (family >= 3)
132 elf_platform[1] = '0' + family;
133 return elf_platform;
134}
135
136#define ELF_HWCAP get_elf_hwcap()
137
138static uint32_t get_elf_hwcap(void)
139{
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300140 return thread_env->features[FEAT_1_EDX];
bellard15338fd2005-11-26 11:41:16 +0000141}
142
j_mayer84409dd2007-04-06 08:56:50 +0000143#ifdef TARGET_X86_64
144#define ELF_START_MMAP 0x2aaaaab000ULL
145#define elf_check_arch(x) ( ((x) == ELF_ARCH) )
146
147#define ELF_CLASS ELFCLASS64
j_mayer84409dd2007-04-06 08:56:50 +0000148#define ELF_ARCH EM_X86_64
149
150static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
151{
152 regs->rax = 0;
153 regs->rsp = infop->start_stack;
154 regs->rip = infop->entry;
155}
156
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300157#define ELF_NREG 27
Anthony Liguoric227f092009-10-01 16:12:16 -0500158typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300159
160/*
161 * Note that ELF_NREG should be 29 as there should be place for
162 * TRAPNO and ERR "registers" as well but linux doesn't dump
163 * those.
164 *
165 * See linux kernel: arch/x86/include/asm/elf.h
166 */
Andreas Färber05390242012-02-25 03:37:53 +0100167static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300168{
169 (*regs)[0] = env->regs[15];
170 (*regs)[1] = env->regs[14];
171 (*regs)[2] = env->regs[13];
172 (*regs)[3] = env->regs[12];
173 (*regs)[4] = env->regs[R_EBP];
174 (*regs)[5] = env->regs[R_EBX];
175 (*regs)[6] = env->regs[11];
176 (*regs)[7] = env->regs[10];
177 (*regs)[8] = env->regs[9];
178 (*regs)[9] = env->regs[8];
179 (*regs)[10] = env->regs[R_EAX];
180 (*regs)[11] = env->regs[R_ECX];
181 (*regs)[12] = env->regs[R_EDX];
182 (*regs)[13] = env->regs[R_ESI];
183 (*regs)[14] = env->regs[R_EDI];
184 (*regs)[15] = env->regs[R_EAX]; /* XXX */
185 (*regs)[16] = env->eip;
186 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
187 (*regs)[18] = env->eflags;
188 (*regs)[19] = env->regs[R_ESP];
189 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
190 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
191 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
192 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
193 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
194 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
195 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
196}
197
j_mayer84409dd2007-04-06 08:56:50 +0000198#else
199
bellard30ac07d2003-04-07 21:33:03 +0000200#define ELF_START_MMAP 0x80000000
201
bellard30ac07d2003-04-07 21:33:03 +0000202/*
203 * This is used to ensure we don't load something for the wrong architecture.
204 */
205#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
206
207/*
208 * These are used to set parameters in the core dumps.
209 */
Richard Hendersond97ef722010-07-27 10:25:29 -0700210#define ELF_CLASS ELFCLASS32
Richard Hendersond97ef722010-07-27 10:25:29 -0700211#define ELF_ARCH EM_386
bellard30ac07d2003-04-07 21:33:03 +0000212
Richard Hendersond97ef722010-07-27 10:25:29 -0700213static inline void init_thread(struct target_pt_regs *regs,
214 struct image_info *infop)
bellardb346ff42003-06-15 20:05:50 +0000215{
216 regs->esp = infop->start_stack;
217 regs->eip = infop->entry;
pbrooke5fe0c52006-06-11 13:32:59 +0000218
219 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
220 starts %edx contains a pointer to a function which might be
221 registered using `atexit'. This provides a mean for the
222 dynamic linker to call DT_FINI functions for shared libraries
223 that have been loaded before the code runs.
224
225 A value of 0 tells we have no such handler. */
226 regs->edx = 0;
bellardb346ff42003-06-15 20:05:50 +0000227}
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300228
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300229#define ELF_NREG 17
Anthony Liguoric227f092009-10-01 16:12:16 -0500230typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300231
232/*
233 * Note that ELF_NREG should be 19 as there should be place for
234 * TRAPNO and ERR "registers" as well but linux doesn't dump
235 * those.
236 *
237 * See linux kernel: arch/x86/include/asm/elf.h
238 */
Andreas Färber05390242012-02-25 03:37:53 +0100239static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300240{
241 (*regs)[0] = env->regs[R_EBX];
242 (*regs)[1] = env->regs[R_ECX];
243 (*regs)[2] = env->regs[R_EDX];
244 (*regs)[3] = env->regs[R_ESI];
245 (*regs)[4] = env->regs[R_EDI];
246 (*regs)[5] = env->regs[R_EBP];
247 (*regs)[6] = env->regs[R_EAX];
248 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
249 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
250 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
251 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
252 (*regs)[11] = env->regs[R_EAX]; /* XXX */
253 (*regs)[12] = env->eip;
254 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
255 (*regs)[14] = env->eflags;
256 (*regs)[15] = env->regs[R_ESP];
257 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
258}
j_mayer84409dd2007-04-06 08:56:50 +0000259#endif
bellardb346ff42003-06-15 20:05:50 +0000260
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300261#define USE_ELF_CORE_DUMP
Richard Hendersond97ef722010-07-27 10:25:29 -0700262#define ELF_EXEC_PAGESIZE 4096
bellardb346ff42003-06-15 20:05:50 +0000263
264#endif
265
266#ifdef TARGET_ARM
267
268#define ELF_START_MMAP 0x80000000
269
270#define elf_check_arch(x) ( (x) == EM_ARM )
271
Richard Hendersond97ef722010-07-27 10:25:29 -0700272#define ELF_CLASS ELFCLASS32
Richard Hendersond97ef722010-07-27 10:25:29 -0700273#define ELF_ARCH EM_ARM
bellardb346ff42003-06-15 20:05:50 +0000274
Richard Hendersond97ef722010-07-27 10:25:29 -0700275static inline void init_thread(struct target_pt_regs *regs,
276 struct image_info *infop)
bellardb346ff42003-06-15 20:05:50 +0000277{
blueswir1992f48a2007-10-14 16:27:31 +0000278 abi_long stack = infop->start_stack;
bellardb346ff42003-06-15 20:05:50 +0000279 memset(regs, 0, sizeof(*regs));
280 regs->ARM_cpsr = 0x10;
pbrook0240ded2006-02-04 19:30:51 +0000281 if (infop->entry & 1)
Richard Hendersond97ef722010-07-27 10:25:29 -0700282 regs->ARM_cpsr |= CPSR_T;
pbrook0240ded2006-02-04 19:30:51 +0000283 regs->ARM_pc = infop->entry & 0xfffffffe;
bellardb346ff42003-06-15 20:05:50 +0000284 regs->ARM_sp = infop->start_stack;
bellard2f619692007-11-16 10:46:05 +0000285 /* FIXME - what to for failure of get_user()? */
286 get_user_ual(regs->ARM_r2, stack + 8); /* envp */
287 get_user_ual(regs->ARM_r1, stack + 4); /* envp */
bellarda1516e92003-07-09 17:13:37 +0000288 /* XXX: it seems that r0 is zeroed after ! */
pbrooke5fe0c52006-06-11 13:32:59 +0000289 regs->ARM_r0 = 0;
290 /* For uClinux PIC binaries. */
j_mayer863cf0b2007-10-07 15:59:45 +0000291 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
pbrooke5fe0c52006-06-11 13:32:59 +0000292 regs->ARM_r10 = infop->start_data;
bellardb346ff42003-06-15 20:05:50 +0000293}
294
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300295#define ELF_NREG 18
Anthony Liguoric227f092009-10-01 16:12:16 -0500296typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300297
Andreas Färber05390242012-02-25 03:37:53 +0100298static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300299{
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200300 (*regs)[0] = tswapreg(env->regs[0]);
301 (*regs)[1] = tswapreg(env->regs[1]);
302 (*regs)[2] = tswapreg(env->regs[2]);
303 (*regs)[3] = tswapreg(env->regs[3]);
304 (*regs)[4] = tswapreg(env->regs[4]);
305 (*regs)[5] = tswapreg(env->regs[5]);
306 (*regs)[6] = tswapreg(env->regs[6]);
307 (*regs)[7] = tswapreg(env->regs[7]);
308 (*regs)[8] = tswapreg(env->regs[8]);
309 (*regs)[9] = tswapreg(env->regs[9]);
310 (*regs)[10] = tswapreg(env->regs[10]);
311 (*regs)[11] = tswapreg(env->regs[11]);
312 (*regs)[12] = tswapreg(env->regs[12]);
313 (*regs)[13] = tswapreg(env->regs[13]);
314 (*regs)[14] = tswapreg(env->regs[14]);
315 (*regs)[15] = tswapreg(env->regs[15]);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300316
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200317 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
318 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300319}
320
bellard30ac07d2003-04-07 21:33:03 +0000321#define USE_ELF_CORE_DUMP
Richard Hendersond97ef722010-07-27 10:25:29 -0700322#define ELF_EXEC_PAGESIZE 4096
bellard30ac07d2003-04-07 21:33:03 +0000323
bellardafce2922005-10-30 20:58:30 +0000324enum
325{
Richard Hendersond97ef722010-07-27 10:25:29 -0700326 ARM_HWCAP_ARM_SWP = 1 << 0,
327 ARM_HWCAP_ARM_HALF = 1 << 1,
328 ARM_HWCAP_ARM_THUMB = 1 << 2,
329 ARM_HWCAP_ARM_26BIT = 1 << 3,
330 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
331 ARM_HWCAP_ARM_FPA = 1 << 5,
332 ARM_HWCAP_ARM_VFP = 1 << 6,
333 ARM_HWCAP_ARM_EDSP = 1 << 7,
334 ARM_HWCAP_ARM_JAVA = 1 << 8,
335 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
336 ARM_HWCAP_ARM_THUMBEE = 1 << 10,
337 ARM_HWCAP_ARM_NEON = 1 << 11,
338 ARM_HWCAP_ARM_VFPv3 = 1 << 12,
339 ARM_HWCAP_ARM_VFPv3D16 = 1 << 13,
bellardafce2922005-10-30 20:58:30 +0000340};
341
Meador Inge806d1022012-07-26 16:50:02 +0000342#define TARGET_HAS_VALIDATE_GUEST_SPACE
343/* Return 1 if the proposed guest space is suitable for the guest.
344 * Return 0 if the proposed guest space isn't suitable, but another
345 * address space should be tried.
346 * Return -1 if there is no way the proposed guest space can be
347 * valid regardless of the base.
348 * The guest code may leave a page mapped and populate it if the
349 * address is suitable.
350 */
351static int validate_guest_space(unsigned long guest_base,
352 unsigned long guest_size)
Dr. David Alan Gilbert97cc7562011-08-31 17:24:34 +0100353{
354 unsigned long real_start, test_page_addr;
355
356 /* We need to check that we can force a fault on access to the
357 * commpage at 0xffff0fxx
358 */
359 test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
Meador Inge806d1022012-07-26 16:50:02 +0000360
361 /* If the commpage lies within the already allocated guest space,
362 * then there is no way we can allocate it.
363 */
364 if (test_page_addr >= guest_base
365 && test_page_addr <= (guest_base + guest_size)) {
366 return -1;
367 }
368
Dr. David Alan Gilbert97cc7562011-08-31 17:24:34 +0100369 /* Note it needs to be writeable to let us initialise it */
370 real_start = (unsigned long)
371 mmap((void *)test_page_addr, qemu_host_page_size,
372 PROT_READ | PROT_WRITE,
373 MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
374
375 /* If we can't map it then try another address */
376 if (real_start == -1ul) {
377 return 0;
378 }
379
380 if (real_start != test_page_addr) {
381 /* OS didn't put the page where we asked - unmap and reject */
382 munmap((void *)real_start, qemu_host_page_size);
383 return 0;
384 }
385
386 /* Leave the page mapped
387 * Populate it (mmap should have left it all 0'd)
388 */
389
390 /* Kernel helper versions */
391 __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
392
393 /* Now it's populated make it RO */
394 if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
395 perror("Protecting guest commpage");
396 exit(-1);
397 }
398
399 return 1; /* All good */
400}
401
Benoit Canetadf050b2011-11-09 03:37:23 +0000402
403#define ELF_HWCAP get_elf_hwcap()
404
405static uint32_t get_elf_hwcap(void)
406{
407 CPUARMState *e = thread_env;
408 uint32_t hwcaps = 0;
409
410 hwcaps |= ARM_HWCAP_ARM_SWP;
411 hwcaps |= ARM_HWCAP_ARM_HALF;
412 hwcaps |= ARM_HWCAP_ARM_THUMB;
413 hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
414 hwcaps |= ARM_HWCAP_ARM_FPA;
415
416 /* probe for the extra features */
417#define GET_FEATURE(feat, hwcap) \
418 do {if (arm_feature(e, feat)) { hwcaps |= hwcap; } } while (0)
419 GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
420 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
421 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
422 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
423 GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
424 GET_FEATURE(ARM_FEATURE_VFP_FP16, ARM_HWCAP_ARM_VFPv3D16);
425#undef GET_FEATURE
426
427 return hwcaps;
428}
bellardafce2922005-10-30 20:58:30 +0000429
bellard30ac07d2003-04-07 21:33:03 +0000430#endif
431
Guan Xuetaod2fbca92011-04-12 16:27:03 +0800432#ifdef TARGET_UNICORE32
433
434#define ELF_START_MMAP 0x80000000
435
436#define elf_check_arch(x) ((x) == EM_UNICORE32)
437
438#define ELF_CLASS ELFCLASS32
439#define ELF_DATA ELFDATA2LSB
440#define ELF_ARCH EM_UNICORE32
441
442static inline void init_thread(struct target_pt_regs *regs,
443 struct image_info *infop)
444{
445 abi_long stack = infop->start_stack;
446 memset(regs, 0, sizeof(*regs));
447 regs->UC32_REG_asr = 0x10;
448 regs->UC32_REG_pc = infop->entry & 0xfffffffe;
449 regs->UC32_REG_sp = infop->start_stack;
450 /* FIXME - what to for failure of get_user()? */
451 get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */
452 get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */
453 /* XXX: it seems that r0 is zeroed after ! */
454 regs->UC32_REG_00 = 0;
455}
456
457#define ELF_NREG 34
458typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
459
Andreas Färber05390242012-02-25 03:37:53 +0100460static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
Guan Xuetaod2fbca92011-04-12 16:27:03 +0800461{
462 (*regs)[0] = env->regs[0];
463 (*regs)[1] = env->regs[1];
464 (*regs)[2] = env->regs[2];
465 (*regs)[3] = env->regs[3];
466 (*regs)[4] = env->regs[4];
467 (*regs)[5] = env->regs[5];
468 (*regs)[6] = env->regs[6];
469 (*regs)[7] = env->regs[7];
470 (*regs)[8] = env->regs[8];
471 (*regs)[9] = env->regs[9];
472 (*regs)[10] = env->regs[10];
473 (*regs)[11] = env->regs[11];
474 (*regs)[12] = env->regs[12];
475 (*regs)[13] = env->regs[13];
476 (*regs)[14] = env->regs[14];
477 (*regs)[15] = env->regs[15];
478 (*regs)[16] = env->regs[16];
479 (*regs)[17] = env->regs[17];
480 (*regs)[18] = env->regs[18];
481 (*regs)[19] = env->regs[19];
482 (*regs)[20] = env->regs[20];
483 (*regs)[21] = env->regs[21];
484 (*regs)[22] = env->regs[22];
485 (*regs)[23] = env->regs[23];
486 (*regs)[24] = env->regs[24];
487 (*regs)[25] = env->regs[25];
488 (*regs)[26] = env->regs[26];
489 (*regs)[27] = env->regs[27];
490 (*regs)[28] = env->regs[28];
491 (*regs)[29] = env->regs[29];
492 (*regs)[30] = env->regs[30];
493 (*regs)[31] = env->regs[31];
494
Andreas Färber05390242012-02-25 03:37:53 +0100495 (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
Guan Xuetaod2fbca92011-04-12 16:27:03 +0800496 (*regs)[33] = env->regs[0]; /* XXX */
497}
498
499#define USE_ELF_CORE_DUMP
500#define ELF_EXEC_PAGESIZE 4096
501
502#define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
503
504#endif
505
bellard853d6f72003-09-30 20:58:32 +0000506#ifdef TARGET_SPARC
bellarda315a142005-01-30 22:59:18 +0000507#ifdef TARGET_SPARC64
bellard853d6f72003-09-30 20:58:32 +0000508
509#define ELF_START_MMAP 0x80000000
Artyom Tarasenkocf973e42011-07-14 19:37:06 +0200510#define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
511 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
blueswir1992f48a2007-10-14 16:27:31 +0000512#ifndef TARGET_ABI32
blueswir1cb33da52007-10-09 16:34:29 +0000513#define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
blueswir1992f48a2007-10-14 16:27:31 +0000514#else
515#define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
516#endif
bellard853d6f72003-09-30 20:58:32 +0000517
bellarda315a142005-01-30 22:59:18 +0000518#define ELF_CLASS ELFCLASS64
bellard5ef54112006-07-18 21:14:09 +0000519#define ELF_ARCH EM_SPARCV9
520
Richard Hendersond97ef722010-07-27 10:25:29 -0700521#define STACK_BIAS 2047
bellarda315a142005-01-30 22:59:18 +0000522
Richard Hendersond97ef722010-07-27 10:25:29 -0700523static inline void init_thread(struct target_pt_regs *regs,
524 struct image_info *infop)
bellarda315a142005-01-30 22:59:18 +0000525{
blueswir1992f48a2007-10-14 16:27:31 +0000526#ifndef TARGET_ABI32
bellarda315a142005-01-30 22:59:18 +0000527 regs->tstate = 0;
blueswir1992f48a2007-10-14 16:27:31 +0000528#endif
bellarda315a142005-01-30 22:59:18 +0000529 regs->pc = infop->entry;
530 regs->npc = regs->pc + 4;
531 regs->y = 0;
blueswir1992f48a2007-10-14 16:27:31 +0000532#ifdef TARGET_ABI32
533 regs->u_regs[14] = infop->start_stack - 16 * 4;
534#else
blueswir1cb33da52007-10-09 16:34:29 +0000535 if (personality(infop->personality) == PER_LINUX32)
536 regs->u_regs[14] = infop->start_stack - 16 * 4;
537 else
538 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
blueswir1992f48a2007-10-14 16:27:31 +0000539#endif
bellarda315a142005-01-30 22:59:18 +0000540}
541
542#else
543#define ELF_START_MMAP 0x80000000
Artyom Tarasenkocf973e42011-07-14 19:37:06 +0200544#define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
545 | HWCAP_SPARC_MULDIV)
bellarda315a142005-01-30 22:59:18 +0000546#define elf_check_arch(x) ( (x) == EM_SPARC )
547
bellard853d6f72003-09-30 20:58:32 +0000548#define ELF_CLASS ELFCLASS32
bellard853d6f72003-09-30 20:58:32 +0000549#define ELF_ARCH EM_SPARC
550
Richard Hendersond97ef722010-07-27 10:25:29 -0700551static inline void init_thread(struct target_pt_regs *regs,
552 struct image_info *infop)
bellard853d6f72003-09-30 20:58:32 +0000553{
bellardf5155282004-01-04 15:46:50 +0000554 regs->psr = 0;
555 regs->pc = infop->entry;
556 regs->npc = regs->pc + 4;
557 regs->y = 0;
558 regs->u_regs[14] = infop->start_stack - 16 * 4;
bellard853d6f72003-09-30 20:58:32 +0000559}
560
561#endif
bellarda315a142005-01-30 22:59:18 +0000562#endif
bellard853d6f72003-09-30 20:58:32 +0000563
bellard67867302003-11-23 17:05:30 +0000564#ifdef TARGET_PPC
565
566#define ELF_START_MMAP 0x80000000
567
j_mayere85e7c62007-10-18 19:59:49 +0000568#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
j_mayer84409dd2007-04-06 08:56:50 +0000569
570#define elf_check_arch(x) ( (x) == EM_PPC64 )
571
Richard Hendersond97ef722010-07-27 10:25:29 -0700572#define ELF_CLASS ELFCLASS64
j_mayer84409dd2007-04-06 08:56:50 +0000573
574#else
575
bellard67867302003-11-23 17:05:30 +0000576#define elf_check_arch(x) ( (x) == EM_PPC )
577
Richard Hendersond97ef722010-07-27 10:25:29 -0700578#define ELF_CLASS ELFCLASS32
j_mayer84409dd2007-04-06 08:56:50 +0000579
580#endif
581
Richard Hendersond97ef722010-07-27 10:25:29 -0700582#define ELF_ARCH EM_PPC
bellard67867302003-11-23 17:05:30 +0000583
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700584/* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
585 See arch/powerpc/include/asm/cputable.h. */
586enum {
malc3efa9a62009-07-18 13:10:12 +0400587 QEMU_PPC_FEATURE_32 = 0x80000000,
588 QEMU_PPC_FEATURE_64 = 0x40000000,
589 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
590 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
591 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
592 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
593 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
594 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
595 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
596 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
597 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
598 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
599 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
600 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
601 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
602 QEMU_PPC_FEATURE_CELL = 0x00010000,
603 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
604 QEMU_PPC_FEATURE_SMT = 0x00004000,
605 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
606 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
607 QEMU_PPC_FEATURE_PA6T = 0x00000800,
608 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
609 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
610 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
611 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
612 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700613
malc3efa9a62009-07-18 13:10:12 +0400614 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
615 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700616};
617
618#define ELF_HWCAP get_elf_hwcap()
619
620static uint32_t get_elf_hwcap(void)
621{
Andreas Färber05390242012-02-25 03:37:53 +0100622 CPUPPCState *e = thread_env;
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700623 uint32_t features = 0;
624
625 /* We don't have to be terribly complete here; the high points are
626 Altivec/FP/SPE support. Anything else is just a bonus. */
Richard Hendersond97ef722010-07-27 10:25:29 -0700627#define GET_FEATURE(flag, feature) \
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700628 do {if (e->insns_flags & flag) features |= feature; } while(0)
malc3efa9a62009-07-18 13:10:12 +0400629 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
630 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
631 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
632 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
633 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
634 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
635 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
636 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700637#undef GET_FEATURE
638
639 return features;
640}
641
bellardf5155282004-01-04 15:46:50 +0000642/*
bellardf5155282004-01-04 15:46:50 +0000643 * The requirements here are:
644 * - keep the final alignment of sp (sp & 0xf)
645 * - make sure the 32-bit value at the first 16 byte aligned position of
646 * AUXV is greater than 16 for glibc compatibility.
647 * AT_IGNOREPPC is used for that.
648 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
649 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
650 */
bellard0bccf032005-08-21 10:12:28 +0000651#define DLINFO_ARCH_ITEMS 5
Richard Hendersond97ef722010-07-27 10:25:29 -0700652#define ARCH_DLINFO \
653 do { \
654 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
655 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
656 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
657 /* \
658 * Now handle glibc compatibility. \
659 */ \
660 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
661 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
662 } while (0)
bellardf5155282004-01-04 15:46:50 +0000663
bellard67867302003-11-23 17:05:30 +0000664static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
665{
bellard67867302003-11-23 17:05:30 +0000666 _regs->gpr[1] = infop->start_stack;
j_mayere85e7c62007-10-18 19:59:49 +0000667#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
Richard Henderson8e780642011-10-26 09:59:17 -0700668 _regs->gpr[2] = ldq_raw(infop->entry + 8) + infop->load_bias;
669 infop->entry = ldq_raw(infop->entry) + infop->load_bias;
j_mayer84409dd2007-04-06 08:56:50 +0000670#endif
bellard67867302003-11-23 17:05:30 +0000671 _regs->nip = infop->entry;
672}
673
Nathan Froyde2f3e742009-12-11 09:04:48 -0800674/* See linux kernel: arch/powerpc/include/asm/elf.h. */
675#define ELF_NREG 48
676typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
677
Andreas Färber05390242012-02-25 03:37:53 +0100678static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
Nathan Froyde2f3e742009-12-11 09:04:48 -0800679{
680 int i;
681 target_ulong ccr = 0;
682
683 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200684 (*regs)[i] = tswapreg(env->gpr[i]);
Nathan Froyde2f3e742009-12-11 09:04:48 -0800685 }
686
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200687 (*regs)[32] = tswapreg(env->nip);
688 (*regs)[33] = tswapreg(env->msr);
689 (*regs)[35] = tswapreg(env->ctr);
690 (*regs)[36] = tswapreg(env->lr);
691 (*regs)[37] = tswapreg(env->xer);
Nathan Froyde2f3e742009-12-11 09:04:48 -0800692
693 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
694 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
695 }
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200696 (*regs)[38] = tswapreg(ccr);
Nathan Froyde2f3e742009-12-11 09:04:48 -0800697}
698
699#define USE_ELF_CORE_DUMP
Richard Hendersond97ef722010-07-27 10:25:29 -0700700#define ELF_EXEC_PAGESIZE 4096
bellard67867302003-11-23 17:05:30 +0000701
702#endif
703
bellard048f6b42005-11-26 18:47:20 +0000704#ifdef TARGET_MIPS
705
706#define ELF_START_MMAP 0x80000000
707
708#define elf_check_arch(x) ( (x) == EM_MIPS )
709
ths388bb212007-05-13 13:58:00 +0000710#ifdef TARGET_MIPS64
711#define ELF_CLASS ELFCLASS64
712#else
bellard048f6b42005-11-26 18:47:20 +0000713#define ELF_CLASS ELFCLASS32
ths388bb212007-05-13 13:58:00 +0000714#endif
bellard048f6b42005-11-26 18:47:20 +0000715#define ELF_ARCH EM_MIPS
716
Richard Hendersond97ef722010-07-27 10:25:29 -0700717static inline void init_thread(struct target_pt_regs *regs,
718 struct image_info *infop)
bellard048f6b42005-11-26 18:47:20 +0000719{
ths623a9302007-10-28 19:45:05 +0000720 regs->cp0_status = 2 << CP0St_KSU;
bellard048f6b42005-11-26 18:47:20 +0000721 regs->cp0_epc = infop->entry;
722 regs->regs[29] = infop->start_stack;
723}
724
Nathan Froyd51e52602009-12-11 09:04:49 -0800725/* See linux kernel: arch/mips/include/asm/elf.h. */
726#define ELF_NREG 45
727typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
728
729/* See linux kernel: arch/mips/include/asm/reg.h. */
730enum {
731#ifdef TARGET_MIPS64
732 TARGET_EF_R0 = 0,
733#else
734 TARGET_EF_R0 = 6,
735#endif
736 TARGET_EF_R26 = TARGET_EF_R0 + 26,
737 TARGET_EF_R27 = TARGET_EF_R0 + 27,
738 TARGET_EF_LO = TARGET_EF_R0 + 32,
739 TARGET_EF_HI = TARGET_EF_R0 + 33,
740 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
741 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
742 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
743 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
744};
745
746/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
Andreas Färber05390242012-02-25 03:37:53 +0100747static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
Nathan Froyd51e52602009-12-11 09:04:49 -0800748{
749 int i;
750
751 for (i = 0; i < TARGET_EF_R0; i++) {
752 (*regs)[i] = 0;
753 }
754 (*regs)[TARGET_EF_R0] = 0;
755
756 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
Paolo Bonzinia29f9982013-04-17 16:26:37 +0200757 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
Nathan Froyd51e52602009-12-11 09:04:49 -0800758 }
759
760 (*regs)[TARGET_EF_R26] = 0;
761 (*regs)[TARGET_EF_R27] = 0;
Paolo Bonzinia29f9982013-04-17 16:26:37 +0200762 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
763 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
764 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
765 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
766 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
767 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
Nathan Froyd51e52602009-12-11 09:04:49 -0800768}
769
770#define USE_ELF_CORE_DUMP
ths388bb212007-05-13 13:58:00 +0000771#define ELF_EXEC_PAGESIZE 4096
772
bellard048f6b42005-11-26 18:47:20 +0000773#endif /* TARGET_MIPS */
774
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200775#ifdef TARGET_MICROBLAZE
776
777#define ELF_START_MMAP 0x80000000
778
Edgar E. Iglesias0d5d4692010-05-19 15:24:17 +0200779#define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200780
781#define ELF_CLASS ELFCLASS32
Edgar E. Iglesias0d5d4692010-05-19 15:24:17 +0200782#define ELF_ARCH EM_MICROBLAZE
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200783
Richard Hendersond97ef722010-07-27 10:25:29 -0700784static inline void init_thread(struct target_pt_regs *regs,
785 struct image_info *infop)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200786{
787 regs->pc = infop->entry;
788 regs->r1 = infop->start_stack;
789
790}
791
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200792#define ELF_EXEC_PAGESIZE 4096
793
Edgar E. Iglesiase4cbd442010-05-19 15:09:28 +0200794#define USE_ELF_CORE_DUMP
795#define ELF_NREG 38
796typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
797
798/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
Andreas Färber05390242012-02-25 03:37:53 +0100799static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
Edgar E. Iglesiase4cbd442010-05-19 15:09:28 +0200800{
801 int i, pos = 0;
802
803 for (i = 0; i < 32; i++) {
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200804 (*regs)[pos++] = tswapreg(env->regs[i]);
Edgar E. Iglesiase4cbd442010-05-19 15:09:28 +0200805 }
806
807 for (i = 0; i < 6; i++) {
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200808 (*regs)[pos++] = tswapreg(env->sregs[i]);
Edgar E. Iglesiase4cbd442010-05-19 15:09:28 +0200809 }
810}
811
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200812#endif /* TARGET_MICROBLAZE */
813
Jia Liud9627832012-07-20 15:50:52 +0800814#ifdef TARGET_OPENRISC
815
816#define ELF_START_MMAP 0x08000000
817
818#define elf_check_arch(x) ((x) == EM_OPENRISC)
819
820#define ELF_ARCH EM_OPENRISC
821#define ELF_CLASS ELFCLASS32
822#define ELF_DATA ELFDATA2MSB
823
824static inline void init_thread(struct target_pt_regs *regs,
825 struct image_info *infop)
826{
827 regs->pc = infop->entry;
828 regs->gpr[1] = infop->start_stack;
829}
830
831#define USE_ELF_CORE_DUMP
832#define ELF_EXEC_PAGESIZE 8192
833
834/* See linux kernel arch/openrisc/include/asm/elf.h. */
835#define ELF_NREG 34 /* gprs and pc, sr */
836typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
837
838static void elf_core_copy_regs(target_elf_gregset_t *regs,
839 const CPUOpenRISCState *env)
840{
841 int i;
842
843 for (i = 0; i < 32; i++) {
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200844 (*regs)[i] = tswapreg(env->gpr[i]);
Jia Liud9627832012-07-20 15:50:52 +0800845 }
846
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200847 (*regs)[32] = tswapreg(env->pc);
848 (*regs)[33] = tswapreg(env->sr);
Jia Liud9627832012-07-20 15:50:52 +0800849}
850#define ELF_HWCAP 0
851#define ELF_PLATFORM NULL
852
853#endif /* TARGET_OPENRISC */
854
bellardfdf9b3e2006-04-27 21:07:38 +0000855#ifdef TARGET_SH4
856
857#define ELF_START_MMAP 0x80000000
858
859#define elf_check_arch(x) ( (x) == EM_SH )
860
861#define ELF_CLASS ELFCLASS32
bellardfdf9b3e2006-04-27 21:07:38 +0000862#define ELF_ARCH EM_SH
863
Richard Hendersond97ef722010-07-27 10:25:29 -0700864static inline void init_thread(struct target_pt_regs *regs,
865 struct image_info *infop)
bellardfdf9b3e2006-04-27 21:07:38 +0000866{
Richard Hendersond97ef722010-07-27 10:25:29 -0700867 /* Check other registers XXXXX */
868 regs->pc = infop->entry;
869 regs->regs[15] = infop->start_stack;
bellardfdf9b3e2006-04-27 21:07:38 +0000870}
871
Nathan Froyd7631c972009-12-11 09:04:51 -0800872/* See linux kernel: arch/sh/include/asm/elf.h. */
873#define ELF_NREG 23
874typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
875
876/* See linux kernel: arch/sh/include/asm/ptrace.h. */
877enum {
878 TARGET_REG_PC = 16,
879 TARGET_REG_PR = 17,
880 TARGET_REG_SR = 18,
881 TARGET_REG_GBR = 19,
882 TARGET_REG_MACH = 20,
883 TARGET_REG_MACL = 21,
884 TARGET_REG_SYSCALL = 22
885};
886
Richard Hendersond97ef722010-07-27 10:25:29 -0700887static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
Andreas Färber05390242012-02-25 03:37:53 +0100888 const CPUSH4State *env)
Nathan Froyd7631c972009-12-11 09:04:51 -0800889{
890 int i;
891
892 for (i = 0; i < 16; i++) {
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200893 (*regs[i]) = tswapreg(env->gregs[i]);
Nathan Froyd7631c972009-12-11 09:04:51 -0800894 }
895
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200896 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
897 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
898 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
899 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
900 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
901 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
Nathan Froyd7631c972009-12-11 09:04:51 -0800902 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
903}
904
905#define USE_ELF_CORE_DUMP
bellardfdf9b3e2006-04-27 21:07:38 +0000906#define ELF_EXEC_PAGESIZE 4096
907
908#endif
909
ths48733d12007-10-08 13:36:46 +0000910#ifdef TARGET_CRIS
911
912#define ELF_START_MMAP 0x80000000
913
914#define elf_check_arch(x) ( (x) == EM_CRIS )
915
916#define ELF_CLASS ELFCLASS32
ths48733d12007-10-08 13:36:46 +0000917#define ELF_ARCH EM_CRIS
918
Richard Hendersond97ef722010-07-27 10:25:29 -0700919static inline void init_thread(struct target_pt_regs *regs,
920 struct image_info *infop)
ths48733d12007-10-08 13:36:46 +0000921{
Richard Hendersond97ef722010-07-27 10:25:29 -0700922 regs->erp = infop->entry;
ths48733d12007-10-08 13:36:46 +0000923}
924
ths48733d12007-10-08 13:36:46 +0000925#define ELF_EXEC_PAGESIZE 8192
926
927#endif
928
pbrooke6e59062006-10-22 00:18:54 +0000929#ifdef TARGET_M68K
930
931#define ELF_START_MMAP 0x80000000
932
933#define elf_check_arch(x) ( (x) == EM_68K )
934
Richard Hendersond97ef722010-07-27 10:25:29 -0700935#define ELF_CLASS ELFCLASS32
Richard Hendersond97ef722010-07-27 10:25:29 -0700936#define ELF_ARCH EM_68K
pbrooke6e59062006-10-22 00:18:54 +0000937
938/* ??? Does this need to do anything?
Richard Hendersond97ef722010-07-27 10:25:29 -0700939 #define ELF_PLAT_INIT(_r) */
pbrooke6e59062006-10-22 00:18:54 +0000940
Richard Hendersond97ef722010-07-27 10:25:29 -0700941static inline void init_thread(struct target_pt_regs *regs,
942 struct image_info *infop)
pbrooke6e59062006-10-22 00:18:54 +0000943{
944 regs->usp = infop->start_stack;
945 regs->sr = 0;
946 regs->pc = infop->entry;
947}
948
Nathan Froyd7a93cc52009-12-11 09:04:50 -0800949/* See linux kernel: arch/m68k/include/asm/elf.h. */
950#define ELF_NREG 20
951typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
952
Andreas Färber05390242012-02-25 03:37:53 +0100953static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
Nathan Froyd7a93cc52009-12-11 09:04:50 -0800954{
Paolo Bonzini86cd7b22013-04-17 16:26:38 +0200955 (*regs)[0] = tswapreg(env->dregs[1]);
956 (*regs)[1] = tswapreg(env->dregs[2]);
957 (*regs)[2] = tswapreg(env->dregs[3]);
958 (*regs)[3] = tswapreg(env->dregs[4]);
959 (*regs)[4] = tswapreg(env->dregs[5]);
960 (*regs)[5] = tswapreg(env->dregs[6]);
961 (*regs)[6] = tswapreg(env->dregs[7]);
962 (*regs)[7] = tswapreg(env->aregs[0]);
963 (*regs)[8] = tswapreg(env->aregs[1]);
964 (*regs)[9] = tswapreg(env->aregs[2]);
965 (*regs)[10] = tswapreg(env->aregs[3]);
966 (*regs)[11] = tswapreg(env->aregs[4]);
967 (*regs)[12] = tswapreg(env->aregs[5]);
968 (*regs)[13] = tswapreg(env->aregs[6]);
969 (*regs)[14] = tswapreg(env->dregs[0]);
970 (*regs)[15] = tswapreg(env->aregs[7]);
971 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
972 (*regs)[17] = tswapreg(env->sr);
973 (*regs)[18] = tswapreg(env->pc);
Nathan Froyd7a93cc52009-12-11 09:04:50 -0800974 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
975}
976
977#define USE_ELF_CORE_DUMP
Richard Hendersond97ef722010-07-27 10:25:29 -0700978#define ELF_EXEC_PAGESIZE 8192
pbrooke6e59062006-10-22 00:18:54 +0000979
980#endif
981
j_mayer7a3148a2007-04-05 07:13:51 +0000982#ifdef TARGET_ALPHA
983
984#define ELF_START_MMAP (0x30000000000ULL)
985
986#define elf_check_arch(x) ( (x) == ELF_ARCH )
987
988#define ELF_CLASS ELFCLASS64
j_mayer7a3148a2007-04-05 07:13:51 +0000989#define ELF_ARCH EM_ALPHA
990
Richard Hendersond97ef722010-07-27 10:25:29 -0700991static inline void init_thread(struct target_pt_regs *regs,
992 struct image_info *infop)
j_mayer7a3148a2007-04-05 07:13:51 +0000993{
994 regs->pc = infop->entry;
995 regs->ps = 8;
996 regs->usp = infop->start_stack;
j_mayer7a3148a2007-04-05 07:13:51 +0000997}
998
j_mayer7a3148a2007-04-05 07:13:51 +0000999#define ELF_EXEC_PAGESIZE 8192
1000
1001#endif /* TARGET_ALPHA */
1002
Ulrich Hechta4c075f2009-07-24 16:57:31 +02001003#ifdef TARGET_S390X
1004
1005#define ELF_START_MMAP (0x20000000000ULL)
1006
1007#define elf_check_arch(x) ( (x) == ELF_ARCH )
1008
1009#define ELF_CLASS ELFCLASS64
1010#define ELF_DATA ELFDATA2MSB
1011#define ELF_ARCH EM_S390
1012
1013static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1014{
1015 regs->psw.addr = infop->entry;
1016 regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
1017 regs->gprs[15] = infop->start_stack;
1018}
1019
1020#endif /* TARGET_S390X */
1021
bellard15338fd2005-11-26 11:41:16 +00001022#ifndef ELF_PLATFORM
1023#define ELF_PLATFORM (NULL)
1024#endif
1025
1026#ifndef ELF_HWCAP
1027#define ELF_HWCAP 0
1028#endif
1029
blueswir1992f48a2007-10-14 16:27:31 +00001030#ifdef TARGET_ABI32
blueswir1cb33da52007-10-09 16:34:29 +00001031#undef ELF_CLASS
blueswir1992f48a2007-10-14 16:27:31 +00001032#define ELF_CLASS ELFCLASS32
blueswir1cb33da52007-10-09 16:34:29 +00001033#undef bswaptls
1034#define bswaptls(ptr) bswap32s(ptr)
1035#endif
1036
bellard31e31b82003-02-18 22:55:36 +00001037#include "elf.h"
bellard09bfb052003-04-10 00:03:40 +00001038
bellard09bfb052003-04-10 00:03:40 +00001039struct exec
1040{
Richard Hendersond97ef722010-07-27 10:25:29 -07001041 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
1042 unsigned int a_text; /* length of text, in bytes */
1043 unsigned int a_data; /* length of data, in bytes */
1044 unsigned int a_bss; /* length of uninitialized data area, in bytes */
1045 unsigned int a_syms; /* length of symbol table data in file, in bytes */
1046 unsigned int a_entry; /* start address */
1047 unsigned int a_trsize; /* length of relocation info for text, in bytes */
1048 unsigned int a_drsize; /* length of relocation info for data, in bytes */
bellard09bfb052003-04-10 00:03:40 +00001049};
1050
1051
1052#define N_MAGIC(exec) ((exec).a_info & 0xffff)
1053#define OMAGIC 0407
1054#define NMAGIC 0410
1055#define ZMAGIC 0413
1056#define QMAGIC 0314
1057
bellard31e31b82003-02-18 22:55:36 +00001058/* Necessary parameters */
bellard54936002003-05-13 00:25:15 +00001059#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
1060#define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
1061#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
bellard31e31b82003-02-18 22:55:36 +00001062
Laurent ALFONSI14322ba2011-06-20 08:43:13 +02001063#define DLINFO_ITEMS 13
bellard31e31b82003-02-18 22:55:36 +00001064
bellard09bfb052003-04-10 00:03:40 +00001065static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
1066{
Richard Hendersond97ef722010-07-27 10:25:29 -07001067 memcpy(to, from, n);
bellard09bfb052003-04-10 00:03:40 +00001068}
1069
bellard31e31b82003-02-18 22:55:36 +00001070#ifdef BSWAP_NEEDED
bellard92a31b12005-02-10 22:00:52 +00001071static void bswap_ehdr(struct elfhdr *ehdr)
bellard31e31b82003-02-18 22:55:36 +00001072{
Richard Hendersond97ef722010-07-27 10:25:29 -07001073 bswap16s(&ehdr->e_type); /* Object file type */
1074 bswap16s(&ehdr->e_machine); /* Architecture */
1075 bswap32s(&ehdr->e_version); /* Object file version */
1076 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
1077 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
1078 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
1079 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
1080 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
1081 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
1082 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
1083 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
1084 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
1085 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
bellard31e31b82003-02-18 22:55:36 +00001086}
1087
Richard Henderson991f8f02010-07-27 10:25:32 -07001088static void bswap_phdr(struct elf_phdr *phdr, int phnum)
bellard31e31b82003-02-18 22:55:36 +00001089{
Richard Henderson991f8f02010-07-27 10:25:32 -07001090 int i;
1091 for (i = 0; i < phnum; ++i, ++phdr) {
1092 bswap32s(&phdr->p_type); /* Segment type */
1093 bswap32s(&phdr->p_flags); /* Segment flags */
1094 bswaptls(&phdr->p_offset); /* Segment file offset */
1095 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
1096 bswaptls(&phdr->p_paddr); /* Segment physical address */
1097 bswaptls(&phdr->p_filesz); /* Segment size in file */
1098 bswaptls(&phdr->p_memsz); /* Segment size in memory */
1099 bswaptls(&phdr->p_align); /* Segment alignment */
1100 }
bellard31e31b82003-02-18 22:55:36 +00001101}
bellard689f9362003-04-29 20:40:07 +00001102
Richard Henderson991f8f02010-07-27 10:25:32 -07001103static void bswap_shdr(struct elf_shdr *shdr, int shnum)
bellard689f9362003-04-29 20:40:07 +00001104{
Richard Henderson991f8f02010-07-27 10:25:32 -07001105 int i;
1106 for (i = 0; i < shnum; ++i, ++shdr) {
1107 bswap32s(&shdr->sh_name);
1108 bswap32s(&shdr->sh_type);
1109 bswaptls(&shdr->sh_flags);
1110 bswaptls(&shdr->sh_addr);
1111 bswaptls(&shdr->sh_offset);
1112 bswaptls(&shdr->sh_size);
1113 bswap32s(&shdr->sh_link);
1114 bswap32s(&shdr->sh_info);
1115 bswaptls(&shdr->sh_addralign);
1116 bswaptls(&shdr->sh_entsize);
1117 }
bellard689f9362003-04-29 20:40:07 +00001118}
1119
j_mayer7a3148a2007-04-05 07:13:51 +00001120static void bswap_sym(struct elf_sym *sym)
bellard689f9362003-04-29 20:40:07 +00001121{
1122 bswap32s(&sym->st_name);
j_mayer7a3148a2007-04-05 07:13:51 +00001123 bswaptls(&sym->st_value);
1124 bswaptls(&sym->st_size);
bellard689f9362003-04-29 20:40:07 +00001125 bswap16s(&sym->st_shndx);
1126}
Richard Henderson991f8f02010-07-27 10:25:32 -07001127#else
1128static inline void bswap_ehdr(struct elfhdr *ehdr) { }
1129static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
1130static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
1131static inline void bswap_sym(struct elf_sym *sym) { }
bellard31e31b82003-02-18 22:55:36 +00001132#endif
1133
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001134#ifdef USE_ELF_CORE_DUMP
Andreas Färber9349b4f2012-03-14 01:38:32 +01001135static int elf_core_dump(int, const CPUArchState *);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001136#endif /* USE_ELF_CORE_DUMP */
Richard Henderson682674b2010-07-27 10:25:33 -07001137static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001138
Richard Henderson9058abd2010-07-27 10:25:34 -07001139/* Verify the portions of EHDR within E_IDENT for the target.
1140 This can be performed before bswapping the entire header. */
1141static bool elf_check_ident(struct elfhdr *ehdr)
1142{
1143 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
1144 && ehdr->e_ident[EI_MAG1] == ELFMAG1
1145 && ehdr->e_ident[EI_MAG2] == ELFMAG2
1146 && ehdr->e_ident[EI_MAG3] == ELFMAG3
1147 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
1148 && ehdr->e_ident[EI_DATA] == ELF_DATA
1149 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
1150}
1151
1152/* Verify the portions of EHDR outside of E_IDENT for the target.
1153 This has to wait until after bswapping the header. */
1154static bool elf_check_ehdr(struct elfhdr *ehdr)
1155{
1156 return (elf_check_arch(ehdr->e_machine)
1157 && ehdr->e_ehsize == sizeof(struct elfhdr)
1158 && ehdr->e_phentsize == sizeof(struct elf_phdr)
1159 && ehdr->e_shentsize == sizeof(struct elf_shdr)
1160 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
1161}
1162
bellard31e31b82003-02-18 22:55:36 +00001163/*
pbrooke5fe0c52006-06-11 13:32:59 +00001164 * 'copy_elf_strings()' copies argument/envelope strings from user
bellard31e31b82003-02-18 22:55:36 +00001165 * memory to free pages in kernel mem. These are in a format ready
1166 * to be put directly into the top of new user memory.
1167 *
1168 */
blueswir1992f48a2007-10-14 16:27:31 +00001169static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
1170 abi_ulong p)
bellard31e31b82003-02-18 22:55:36 +00001171{
1172 char *tmp, *tmp1, *pag = NULL;
1173 int len, offset = 0;
1174
1175 if (!p) {
Richard Hendersond97ef722010-07-27 10:25:29 -07001176 return 0; /* bullet-proofing */
bellard31e31b82003-02-18 22:55:36 +00001177 }
1178 while (argc-- > 0) {
bellardedf779f2004-02-22 13:40:13 +00001179 tmp = argv[argc];
1180 if (!tmp) {
Richard Hendersond97ef722010-07-27 10:25:29 -07001181 fprintf(stderr, "VFS: argc is wrong");
1182 exit(-1);
1183 }
bellardedf779f2004-02-22 13:40:13 +00001184 tmp1 = tmp;
Richard Hendersond97ef722010-07-27 10:25:29 -07001185 while (*tmp++);
1186 len = tmp - tmp1;
1187 if (p < len) { /* this shouldn't happen - 128kB */
1188 return 0;
1189 }
1190 while (len) {
1191 --p; --tmp; --len;
1192 if (--offset < 0) {
1193 offset = p % TARGET_PAGE_SIZE;
pbrook53a59602006-03-25 19:31:22 +00001194 pag = (char *)page[p/TARGET_PAGE_SIZE];
bellard44a91ca2004-01-18 22:05:44 +00001195 if (!pag) {
Peter Maydell7dd47662011-11-09 19:22:11 +00001196 pag = g_try_malloc0(TARGET_PAGE_SIZE);
pbrook53a59602006-03-25 19:31:22 +00001197 page[p/TARGET_PAGE_SIZE] = pag;
bellard44a91ca2004-01-18 22:05:44 +00001198 if (!pag)
1199 return 0;
Richard Hendersond97ef722010-07-27 10:25:29 -07001200 }
1201 }
1202 if (len == 0 || offset == 0) {
1203 *(pag + offset) = *tmp;
1204 }
1205 else {
1206 int bytes_to_copy = (len > offset) ? offset : len;
1207 tmp -= bytes_to_copy;
1208 p -= bytes_to_copy;
1209 offset -= bytes_to_copy;
1210 len -= bytes_to_copy;
1211 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
1212 }
1213 }
bellard31e31b82003-02-18 22:55:36 +00001214 }
1215 return p;
1216}
1217
blueswir1992f48a2007-10-14 16:27:31 +00001218static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
1219 struct image_info *info)
pbrook53a59602006-03-25 19:31:22 +00001220{
Richard Henderson60dcbcb2010-07-27 10:25:35 -07001221 abi_ulong stack_base, size, error, guard;
bellard31e31b82003-02-18 22:55:36 +00001222 int i;
bellard31e31b82003-02-18 22:55:36 +00001223
1224 /* Create enough stack to hold everything. If we don't use
Richard Henderson60dcbcb2010-07-27 10:25:35 -07001225 it for args, we'll use it for something else. */
Richard Henderson703e0e82010-03-19 14:21:13 -07001226 size = guest_stack_size;
Richard Henderson60dcbcb2010-07-27 10:25:35 -07001227 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) {
bellard54936002003-05-13 00:25:15 +00001228 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
Richard Henderson60dcbcb2010-07-27 10:25:35 -07001229 }
1230 guard = TARGET_PAGE_SIZE;
1231 if (guard < qemu_real_host_page_size) {
1232 guard = qemu_real_host_page_size;
1233 }
1234
1235 error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
1236 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
bellard09bfb052003-04-10 00:03:40 +00001237 if (error == -1) {
Richard Henderson60dcbcb2010-07-27 10:25:35 -07001238 perror("mmap stack");
bellard09bfb052003-04-10 00:03:40 +00001239 exit(-1);
bellard31e31b82003-02-18 22:55:36 +00001240 }
bellard09bfb052003-04-10 00:03:40 +00001241
Richard Henderson60dcbcb2010-07-27 10:25:35 -07001242 /* We reserve one extra page at the top of the stack as guard. */
1243 target_mprotect(error, guard, PROT_NONE);
1244
1245 info->stack_limit = error + guard;
1246 stack_base = info->stack_limit + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
bellard09bfb052003-04-10 00:03:40 +00001247 p += stack_base;
1248
bellard31e31b82003-02-18 22:55:36 +00001249 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
Richard Hendersond97ef722010-07-27 10:25:29 -07001250 if (bprm->page[i]) {
1251 info->rss++;
bellard579a97f2007-11-11 14:26:47 +00001252 /* FIXME - check return value of memcpy_to_target() for failure */
Richard Hendersond97ef722010-07-27 10:25:29 -07001253 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
Peter Maydell7dd47662011-11-09 19:22:11 +00001254 g_free(bprm->page[i]);
Richard Hendersond97ef722010-07-27 10:25:29 -07001255 }
pbrook53a59602006-03-25 19:31:22 +00001256 stack_base += TARGET_PAGE_SIZE;
bellard31e31b82003-02-18 22:55:36 +00001257 }
1258 return p;
1259}
1260
Richard Hendersoncf129f32010-07-27 10:25:27 -07001261/* Map and zero the bss. We need to explicitly zero any fractional pages
1262 after the data section (i.e. bss). */
1263static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
bellard31e31b82003-02-18 22:55:36 +00001264{
Richard Hendersoncf129f32010-07-27 10:25:27 -07001265 uintptr_t host_start, host_map_start, host_end;
bellard31e31b82003-02-18 22:55:36 +00001266
Richard Hendersoncf129f32010-07-27 10:25:27 -07001267 last_bss = TARGET_PAGE_ALIGN(last_bss);
bellard31e31b82003-02-18 22:55:36 +00001268
Richard Hendersoncf129f32010-07-27 10:25:27 -07001269 /* ??? There is confusion between qemu_real_host_page_size and
1270 qemu_host_page_size here and elsewhere in target_mmap, which
1271 may lead to the end of the data section mapping from the file
1272 not being mapped. At least there was an explicit test and
1273 comment for that here, suggesting that "the file size must
1274 be known". The comment probably pre-dates the introduction
1275 of the fstat system call in target_mmap which does in fact
1276 find out the size. What isn't clear is if the workaround
1277 here is still actually needed. For now, continue with it,
1278 but merge it with the "normal" mmap that would allocate the bss. */
bellard31e31b82003-02-18 22:55:36 +00001279
Richard Hendersoncf129f32010-07-27 10:25:27 -07001280 host_start = (uintptr_t) g2h(elf_bss);
1281 host_end = (uintptr_t) g2h(last_bss);
1282 host_map_start = (host_start + qemu_real_host_page_size - 1);
1283 host_map_start &= -qemu_real_host_page_size;
ths768a4a32006-12-14 13:32:11 +00001284
Richard Hendersoncf129f32010-07-27 10:25:27 -07001285 if (host_map_start < host_end) {
1286 void *p = mmap((void *)host_map_start, host_end - host_map_start,
1287 prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1288 if (p == MAP_FAILED) {
1289 perror("cannot mmap brk");
1290 exit(-1);
bellard853d6f72003-09-30 20:58:32 +00001291 }
1292
Richard Hendersoncf129f32010-07-27 10:25:27 -07001293 /* Since we didn't use target_mmap, make sure to record
1294 the validity of the pages with qemu. */
1295 page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot|PAGE_VALID);
1296 }
bellard31e31b82003-02-18 22:55:36 +00001297
Richard Hendersoncf129f32010-07-27 10:25:27 -07001298 if (host_start < host_map_start) {
1299 memset((void *)host_start, 0, host_map_start - host_start);
1300 }
1301}
bellardedf779f2004-02-22 13:40:13 +00001302
Mike Frysinger1af02e82011-02-07 01:05:50 -05001303#ifdef CONFIG_USE_FDPIC
1304static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
1305{
1306 uint16_t n;
1307 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
1308
1309 /* elf32_fdpic_loadseg */
1310 n = info->nsegs;
1311 while (n--) {
1312 sp -= 12;
1313 put_user_u32(loadsegs[n].addr, sp+0);
1314 put_user_u32(loadsegs[n].p_vaddr, sp+4);
1315 put_user_u32(loadsegs[n].p_memsz, sp+8);
1316 }
1317
1318 /* elf32_fdpic_loadmap */
1319 sp -= 4;
1320 put_user_u16(0, sp+0); /* version */
1321 put_user_u16(info->nsegs, sp+2); /* nsegs */
1322
1323 info->personality = PER_LINUX_FDPIC;
1324 info->loadmap_addr = sp;
1325
1326 return sp;
1327}
1328#endif
1329
blueswir1992f48a2007-10-14 16:27:31 +00001330static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
Richard Henderson8e62a712010-07-27 10:25:37 -07001331 struct elfhdr *exec,
1332 struct image_info *info,
1333 struct image_info *interp_info)
pbrook53a59602006-03-25 19:31:22 +00001334{
Richard Hendersond97ef722010-07-27 10:25:29 -07001335 abi_ulong sp;
Alexander Graf125b0f52012-01-28 21:12:14 +02001336 abi_ulong sp_auxv;
Richard Hendersond97ef722010-07-27 10:25:29 -07001337 int size;
Laurent ALFONSI14322ba2011-06-20 08:43:13 +02001338 int i;
1339 abi_ulong u_rand_bytes;
1340 uint8_t k_rand_bytes[16];
Richard Hendersond97ef722010-07-27 10:25:29 -07001341 abi_ulong u_platform;
1342 const char *k_platform;
1343 const int n = sizeof(elf_addr_t);
pbrook53a59602006-03-25 19:31:22 +00001344
Richard Hendersond97ef722010-07-27 10:25:29 -07001345 sp = p;
Mike Frysinger1af02e82011-02-07 01:05:50 -05001346
1347#ifdef CONFIG_USE_FDPIC
1348 /* Needs to be before we load the env/argc/... */
1349 if (elf_is_fdpic(exec)) {
1350 /* Need 4 byte alignment for these structs */
1351 sp &= ~3;
1352 sp = loader_build_fdpic_loadmap(info, sp);
1353 info->other_info = interp_info;
1354 if (interp_info) {
1355 interp_info->other_info = info;
1356 sp = loader_build_fdpic_loadmap(interp_info, sp);
1357 }
1358 }
1359#endif
1360
Richard Hendersond97ef722010-07-27 10:25:29 -07001361 u_platform = 0;
1362 k_platform = ELF_PLATFORM;
1363 if (k_platform) {
1364 size_t len = strlen(k_platform) + 1;
1365 sp -= (len + n - 1) & ~(n - 1);
1366 u_platform = sp;
1367 /* FIXME - check return value of memcpy_to_target() for failure */
1368 memcpy_to_target(sp, k_platform, len);
1369 }
Laurent ALFONSI14322ba2011-06-20 08:43:13 +02001370
1371 /*
1372 * Generate 16 random bytes for userspace PRNG seeding (not
1373 * cryptically secure but it's not the aim of QEMU).
1374 */
1375 srand((unsigned int) time(NULL));
1376 for (i = 0; i < 16; i++) {
1377 k_rand_bytes[i] = rand();
1378 }
1379 sp -= 16;
1380 u_rand_bytes = sp;
1381 /* FIXME - check return value of memcpy_to_target() for failure */
1382 memcpy_to_target(sp, k_rand_bytes, 16);
1383
Richard Hendersond97ef722010-07-27 10:25:29 -07001384 /*
1385 * Force 16 byte _final_ alignment here for generality.
1386 */
1387 sp = sp &~ (abi_ulong)15;
1388 size = (DLINFO_ITEMS + 1) * 2;
1389 if (k_platform)
1390 size += 2;
bellardf5155282004-01-04 15:46:50 +00001391#ifdef DLINFO_ARCH_ITEMS
Richard Hendersond97ef722010-07-27 10:25:29 -07001392 size += DLINFO_ARCH_ITEMS * 2;
bellardf5155282004-01-04 15:46:50 +00001393#endif
Richard Hendersond97ef722010-07-27 10:25:29 -07001394 size += envc + argc + 2;
Richard Hendersonb9329d42010-07-27 10:25:36 -07001395 size += 1; /* argc itself */
Richard Hendersond97ef722010-07-27 10:25:29 -07001396 size *= n;
1397 if (size & 15)
1398 sp -= 16 - (size & 15);
ths3b46e622007-09-17 08:09:54 +00001399
Richard Hendersond97ef722010-07-27 10:25:29 -07001400 /* This is correct because Linux defines
1401 * elf_addr_t as Elf32_Off / Elf64_Off
1402 */
1403#define NEW_AUX_ENT(id, val) do { \
1404 sp -= n; put_user_ual(val, sp); \
1405 sp -= n; put_user_ual(id, sp); \
1406 } while(0)
bellard2f619692007-11-16 10:46:05 +00001407
Alexander Graf125b0f52012-01-28 21:12:14 +02001408 sp_auxv = sp;
Richard Hendersond97ef722010-07-27 10:25:29 -07001409 NEW_AUX_ENT (AT_NULL, 0);
bellardf5155282004-01-04 15:46:50 +00001410
Richard Hendersond97ef722010-07-27 10:25:29 -07001411 /* There must be exactly DLINFO_ITEMS entries here. */
Richard Henderson8e62a712010-07-27 10:25:37 -07001412 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
Richard Hendersond97ef722010-07-27 10:25:29 -07001413 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1414 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1415 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
Richard Henderson8e62a712010-07-27 10:25:37 -07001416 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
Richard Hendersond97ef722010-07-27 10:25:29 -07001417 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
Richard Henderson8e62a712010-07-27 10:25:37 -07001418 NEW_AUX_ENT(AT_ENTRY, info->entry);
Richard Hendersond97ef722010-07-27 10:25:29 -07001419 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1420 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1421 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1422 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1423 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
1424 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
Laurent ALFONSI14322ba2011-06-20 08:43:13 +02001425 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
1426
Richard Hendersond97ef722010-07-27 10:25:29 -07001427 if (k_platform)
1428 NEW_AUX_ENT(AT_PLATFORM, u_platform);
bellardf5155282004-01-04 15:46:50 +00001429#ifdef ARCH_DLINFO
Richard Hendersond97ef722010-07-27 10:25:29 -07001430 /*
1431 * ARCH_DLINFO must come last so platform specific code can enforce
1432 * special alignment requirements on the AUXV if necessary (eg. PPC).
1433 */
1434 ARCH_DLINFO;
bellardf5155282004-01-04 15:46:50 +00001435#endif
1436#undef NEW_AUX_ENT
1437
Richard Hendersond97ef722010-07-27 10:25:29 -07001438 info->saved_auxv = sp;
Alexander Graf125b0f52012-01-28 21:12:14 +02001439 info->auxv_len = sp_auxv - sp;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001440
Richard Hendersonb9329d42010-07-27 10:25:36 -07001441 sp = loader_build_argptr(envc, argc, sp, p, 0);
Richard Hendersond97ef722010-07-27 10:25:29 -07001442 return sp;
bellard31e31b82003-02-18 22:55:36 +00001443}
1444
Meador Inge806d1022012-07-26 16:50:02 +00001445#ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
Dr. David Alan Gilbert97cc7562011-08-31 17:24:34 +01001446/* If the guest doesn't have a validation function just agree */
Meador Inge806d1022012-07-26 16:50:02 +00001447static int validate_guest_space(unsigned long guest_base,
1448 unsigned long guest_size)
Dr. David Alan Gilbert97cc7562011-08-31 17:24:34 +01001449{
1450 return 1;
1451}
1452#endif
1453
Meador Ingedce10402012-07-26 16:50:01 +00001454unsigned long init_guest_space(unsigned long host_start,
1455 unsigned long host_size,
1456 unsigned long guest_start,
1457 bool fixed)
1458{
1459 unsigned long current_start, real_start;
1460 int flags;
1461
1462 assert(host_start || host_size);
1463
1464 /* If just a starting address is given, then just verify that
1465 * address. */
1466 if (host_start && !host_size) {
Meador Inge806d1022012-07-26 16:50:02 +00001467 if (validate_guest_space(host_start, host_size) == 1) {
Meador Ingedce10402012-07-26 16:50:01 +00001468 return host_start;
1469 } else {
1470 return (unsigned long)-1;
1471 }
1472 }
1473
1474 /* Setup the initial flags and start address. */
1475 current_start = host_start & qemu_host_page_mask;
1476 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
1477 if (fixed) {
1478 flags |= MAP_FIXED;
1479 }
1480
1481 /* Otherwise, a non-zero size region of memory needs to be mapped
1482 * and validated. */
1483 while (1) {
Meador Inge806d1022012-07-26 16:50:02 +00001484 unsigned long real_size = host_size;
1485
Meador Ingedce10402012-07-26 16:50:01 +00001486 /* Do not use mmap_find_vma here because that is limited to the
1487 * guest address space. We are going to make the
1488 * guest address space fit whatever we're given.
1489 */
1490 real_start = (unsigned long)
1491 mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
1492 if (real_start == (unsigned long)-1) {
1493 return (unsigned long)-1;
1494 }
1495
Meador Inge806d1022012-07-26 16:50:02 +00001496 /* Ensure the address is properly aligned. */
1497 if (real_start & ~qemu_host_page_mask) {
1498 munmap((void *)real_start, host_size);
1499 real_size = host_size + qemu_host_page_size;
1500 real_start = (unsigned long)
1501 mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
1502 if (real_start == (unsigned long)-1) {
1503 return (unsigned long)-1;
1504 }
1505 real_start = HOST_PAGE_ALIGN(real_start);
1506 }
1507
1508 /* Check to see if the address is valid. */
1509 if (!host_start || real_start == current_start) {
1510 int valid = validate_guest_space(real_start - guest_start,
1511 real_size);
1512 if (valid == 1) {
1513 break;
1514 } else if (valid == -1) {
1515 return (unsigned long)-1;
1516 }
1517 /* valid == 0, so try again. */
Meador Ingedce10402012-07-26 16:50:01 +00001518 }
1519
1520 /* That address didn't work. Unmap and try a different one.
1521 * The address the host picked because is typically right at
1522 * the top of the host address space and leaves the guest with
1523 * no usable address space. Resort to a linear search. We
1524 * already compensated for mmap_min_addr, so this should not
1525 * happen often. Probably means we got unlucky and host
1526 * address space randomization put a shared library somewhere
1527 * inconvenient.
1528 */
1529 munmap((void *)real_start, host_size);
1530 current_start += qemu_host_page_size;
1531 if (host_start == current_start) {
1532 /* Theoretically possible if host doesn't have any suitably
1533 * aligned areas. Normally the first mmap will fail.
1534 */
1535 return (unsigned long)-1;
1536 }
1537 }
1538
Meador Inge806d1022012-07-26 16:50:02 +00001539 qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size);
1540
Meador Ingedce10402012-07-26 16:50:01 +00001541 return real_start;
1542}
1543
Peter Maydellf3ed1f52011-05-17 13:34:46 +01001544static void probe_guest_base(const char *image_name,
1545 abi_ulong loaddr, abi_ulong hiaddr)
1546{
1547 /* Probe for a suitable guest base address, if the user has not set
1548 * it explicitly, and set guest_base appropriately.
1549 * In case of error we will print a suitable message and exit.
1550 */
1551#if defined(CONFIG_USE_GUEST_BASE)
1552 const char *errmsg;
1553 if (!have_guest_base && !reserved_va) {
1554 unsigned long host_start, real_start, host_size;
1555
1556 /* Round addresses to page boundaries. */
1557 loaddr &= qemu_host_page_mask;
1558 hiaddr = HOST_PAGE_ALIGN(hiaddr);
1559
1560 if (loaddr < mmap_min_addr) {
1561 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1562 } else {
1563 host_start = loaddr;
1564 if (host_start != loaddr) {
1565 errmsg = "Address overflow loading ELF binary";
1566 goto exit_errmsg;
1567 }
1568 }
1569 host_size = hiaddr - loaddr;
Meador Ingedce10402012-07-26 16:50:01 +00001570
1571 /* Setup the initial guest memory space with ranges gleaned from
1572 * the ELF image that is being loaded.
1573 */
1574 real_start = init_guest_space(host_start, host_size, loaddr, false);
1575 if (real_start == (unsigned long)-1) {
1576 errmsg = "Unable to find space for application";
1577 goto exit_errmsg;
Peter Maydellf3ed1f52011-05-17 13:34:46 +01001578 }
Meador Ingedce10402012-07-26 16:50:01 +00001579 guest_base = real_start - loaddr;
1580
Peter Maydellf3ed1f52011-05-17 13:34:46 +01001581 qemu_log("Relocating guest address space from 0x"
1582 TARGET_ABI_FMT_lx " to 0x%lx\n",
1583 loaddr, real_start);
Peter Maydellf3ed1f52011-05-17 13:34:46 +01001584 }
1585 return;
1586
Peter Maydellf3ed1f52011-05-17 13:34:46 +01001587exit_errmsg:
1588 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1589 exit(-1);
1590#endif
1591}
1592
1593
Richard Henderson8e62a712010-07-27 10:25:37 -07001594/* Load an ELF image into the address space.
bellard31e31b82003-02-18 22:55:36 +00001595
Richard Henderson8e62a712010-07-27 10:25:37 -07001596 IMAGE_NAME is the filename of the image, to use in error messages.
1597 IMAGE_FD is the open file descriptor for the image.
1598
1599 BPRM_BUF is a copy of the beginning of the file; this of course
1600 contains the elf file header at offset 0. It is assumed that this
1601 buffer is sufficiently aligned to present no problems to the host
1602 in accessing data at aligned offsets within the buffer.
1603
1604 On return: INFO values will be filled in, as necessary or available. */
1605
1606static void load_elf_image(const char *image_name, int image_fd,
Richard Hendersonbf858892010-07-27 10:25:38 -07001607 struct image_info *info, char **pinterp_name,
Richard Henderson8e62a712010-07-27 10:25:37 -07001608 char bprm_buf[BPRM_BUF_SIZE])
bellard31e31b82003-02-18 22:55:36 +00001609{
Richard Henderson8e62a712010-07-27 10:25:37 -07001610 struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
1611 struct elf_phdr *phdr;
1612 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
1613 int i, retval;
1614 const char *errmsg;
ths5fafdf22007-09-16 21:08:06 +00001615
Richard Henderson8e62a712010-07-27 10:25:37 -07001616 /* First of all, some simple consistency checks */
1617 errmsg = "Invalid ELF image for this architecture";
1618 if (!elf_check_ident(ehdr)) {
1619 goto exit_errmsg;
1620 }
1621 bswap_ehdr(ehdr);
1622 if (!elf_check_ehdr(ehdr)) {
1623 goto exit_errmsg;
Richard Hendersond97ef722010-07-27 10:25:29 -07001624 }
ths5fafdf22007-09-16 21:08:06 +00001625
Richard Henderson8e62a712010-07-27 10:25:37 -07001626 i = ehdr->e_phnum * sizeof(struct elf_phdr);
1627 if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
1628 phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
Richard Henderson9955ffa2010-07-27 10:25:30 -07001629 } else {
Richard Henderson8e62a712010-07-27 10:25:37 -07001630 phdr = (struct elf_phdr *) alloca(i);
1631 retval = pread(image_fd, phdr, i, ehdr->e_phoff);
Richard Henderson9955ffa2010-07-27 10:25:30 -07001632 if (retval != i) {
Richard Henderson8e62a712010-07-27 10:25:37 -07001633 goto exit_read;
Richard Henderson9955ffa2010-07-27 10:25:30 -07001634 }
Richard Hendersond97ef722010-07-27 10:25:29 -07001635 }
Richard Henderson8e62a712010-07-27 10:25:37 -07001636 bswap_phdr(phdr, ehdr->e_phnum);
bellard09bfb052003-04-10 00:03:40 +00001637
Mike Frysinger1af02e82011-02-07 01:05:50 -05001638#ifdef CONFIG_USE_FDPIC
1639 info->nsegs = 0;
1640 info->pt_dynamic_addr = 0;
1641#endif
1642
Richard Henderson682674b2010-07-27 10:25:33 -07001643 /* Find the maximum size of the image and allocate an appropriate
1644 amount of memory to handle that. */
1645 loaddr = -1, hiaddr = 0;
Richard Henderson8e62a712010-07-27 10:25:37 -07001646 for (i = 0; i < ehdr->e_phnum; ++i) {
1647 if (phdr[i].p_type == PT_LOAD) {
1648 abi_ulong a = phdr[i].p_vaddr;
Richard Henderson682674b2010-07-27 10:25:33 -07001649 if (a < loaddr) {
1650 loaddr = a;
1651 }
Richard Henderson8e62a712010-07-27 10:25:37 -07001652 a += phdr[i].p_memsz;
Richard Henderson682674b2010-07-27 10:25:33 -07001653 if (a > hiaddr) {
1654 hiaddr = a;
1655 }
Mike Frysinger1af02e82011-02-07 01:05:50 -05001656#ifdef CONFIG_USE_FDPIC
1657 ++info->nsegs;
1658#endif
Richard Henderson682674b2010-07-27 10:25:33 -07001659 }
1660 }
1661
1662 load_addr = loaddr;
Richard Henderson8e62a712010-07-27 10:25:37 -07001663 if (ehdr->e_type == ET_DYN) {
Richard Henderson682674b2010-07-27 10:25:33 -07001664 /* The image indicates that it can be loaded anywhere. Find a
1665 location that can hold the memory space required. If the
1666 image is pre-linked, LOADDR will be non-zero. Since we do
1667 not supply MAP_FIXED here we'll use that address if and
1668 only if it remains available. */
1669 load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
1670 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
1671 -1, 0);
1672 if (load_addr == -1) {
Richard Henderson8e62a712010-07-27 10:25:37 -07001673 goto exit_perror;
Richard Hendersond97ef722010-07-27 10:25:29 -07001674 }
Richard Hendersonbf858892010-07-27 10:25:38 -07001675 } else if (pinterp_name != NULL) {
1676 /* This is the main executable. Make sure that the low
1677 address does not conflict with MMAP_MIN_ADDR or the
1678 QEMU application itself. */
Peter Maydellf3ed1f52011-05-17 13:34:46 +01001679 probe_guest_base(image_name, loaddr, hiaddr);
Richard Hendersond97ef722010-07-27 10:25:29 -07001680 }
Richard Henderson682674b2010-07-27 10:25:33 -07001681 load_bias = load_addr - loaddr;
Richard Hendersond97ef722010-07-27 10:25:29 -07001682
Mike Frysinger1af02e82011-02-07 01:05:50 -05001683#ifdef CONFIG_USE_FDPIC
1684 {
1685 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
Anthony Liguori7267c092011-08-20 22:09:37 -05001686 g_malloc(sizeof(*loadsegs) * info->nsegs);
Mike Frysinger1af02e82011-02-07 01:05:50 -05001687
1688 for (i = 0; i < ehdr->e_phnum; ++i) {
1689 switch (phdr[i].p_type) {
1690 case PT_DYNAMIC:
1691 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
1692 break;
1693 case PT_LOAD:
1694 loadsegs->addr = phdr[i].p_vaddr + load_bias;
1695 loadsegs->p_vaddr = phdr[i].p_vaddr;
1696 loadsegs->p_memsz = phdr[i].p_memsz;
1697 ++loadsegs;
1698 break;
1699 }
1700 }
1701 }
1702#endif
1703
Richard Henderson8e62a712010-07-27 10:25:37 -07001704 info->load_bias = load_bias;
1705 info->load_addr = load_addr;
1706 info->entry = ehdr->e_entry + load_bias;
1707 info->start_code = -1;
1708 info->end_code = 0;
1709 info->start_data = -1;
1710 info->end_data = 0;
1711 info->brk = 0;
Paul Brookd8fd2952012-03-30 18:02:50 +01001712 info->elf_flags = ehdr->e_flags;
Richard Henderson8e62a712010-07-27 10:25:37 -07001713
1714 for (i = 0; i < ehdr->e_phnum; i++) {
1715 struct elf_phdr *eppnt = phdr + i;
Richard Hendersond97ef722010-07-27 10:25:29 -07001716 if (eppnt->p_type == PT_LOAD) {
Richard Henderson682674b2010-07-27 10:25:33 -07001717 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
Richard Hendersond97ef722010-07-27 10:25:29 -07001718 int elf_prot = 0;
Richard Hendersond97ef722010-07-27 10:25:29 -07001719
1720 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1721 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1722 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
bellard09bfb052003-04-10 00:03:40 +00001723
Richard Henderson682674b2010-07-27 10:25:33 -07001724 vaddr = load_bias + eppnt->p_vaddr;
1725 vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
1726 vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
1727
1728 error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
1729 elf_prot, MAP_PRIVATE | MAP_FIXED,
Richard Henderson8e62a712010-07-27 10:25:37 -07001730 image_fd, eppnt->p_offset - vaddr_po);
Richard Hendersond97ef722010-07-27 10:25:29 -07001731 if (error == -1) {
Richard Henderson8e62a712010-07-27 10:25:37 -07001732 goto exit_perror;
Richard Hendersond97ef722010-07-27 10:25:29 -07001733 }
bellard31e31b82003-02-18 22:55:36 +00001734
Richard Henderson682674b2010-07-27 10:25:33 -07001735 vaddr_ef = vaddr + eppnt->p_filesz;
1736 vaddr_em = vaddr + eppnt->p_memsz;
bellard31e31b82003-02-18 22:55:36 +00001737
Richard Hendersoncf129f32010-07-27 10:25:27 -07001738 /* If the load segment requests extra zeros (e.g. bss), map it. */
Richard Henderson682674b2010-07-27 10:25:33 -07001739 if (vaddr_ef < vaddr_em) {
1740 zero_bss(vaddr_ef, vaddr_em, elf_prot);
Richard Hendersoncf129f32010-07-27 10:25:27 -07001741 }
Richard Henderson8e62a712010-07-27 10:25:37 -07001742
1743 /* Find the full program boundaries. */
1744 if (elf_prot & PROT_EXEC) {
1745 if (vaddr < info->start_code) {
1746 info->start_code = vaddr;
1747 }
1748 if (vaddr_ef > info->end_code) {
1749 info->end_code = vaddr_ef;
1750 }
1751 }
1752 if (elf_prot & PROT_WRITE) {
1753 if (vaddr < info->start_data) {
1754 info->start_data = vaddr;
1755 }
1756 if (vaddr_ef > info->end_data) {
1757 info->end_data = vaddr_ef;
1758 }
1759 if (vaddr_em > info->brk) {
1760 info->brk = vaddr_em;
1761 }
1762 }
Richard Hendersonbf858892010-07-27 10:25:38 -07001763 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
1764 char *interp_name;
1765
1766 if (*pinterp_name) {
1767 errmsg = "Multiple PT_INTERP entries";
1768 goto exit_errmsg;
1769 }
1770 interp_name = malloc(eppnt->p_filesz);
1771 if (!interp_name) {
1772 goto exit_perror;
1773 }
1774
1775 if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
1776 memcpy(interp_name, bprm_buf + eppnt->p_offset,
1777 eppnt->p_filesz);
1778 } else {
1779 retval = pread(image_fd, interp_name, eppnt->p_filesz,
1780 eppnt->p_offset);
1781 if (retval != eppnt->p_filesz) {
1782 goto exit_perror;
1783 }
1784 }
1785 if (interp_name[eppnt->p_filesz - 1] != 0) {
1786 errmsg = "Invalid PT_INTERP entry";
1787 goto exit_errmsg;
1788 }
1789 *pinterp_name = interp_name;
Richard Hendersond97ef722010-07-27 10:25:29 -07001790 }
Richard Henderson682674b2010-07-27 10:25:33 -07001791 }
ths5fafdf22007-09-16 21:08:06 +00001792
Richard Henderson8e62a712010-07-27 10:25:37 -07001793 if (info->end_data == 0) {
1794 info->start_data = info->end_code;
1795 info->end_data = info->end_code;
1796 info->brk = info->end_code;
Richard Henderson682674b2010-07-27 10:25:33 -07001797 }
bellard31e31b82003-02-18 22:55:36 +00001798
Richard Henderson8e62a712010-07-27 10:25:37 -07001799 if (qemu_log_enabled()) {
1800 load_symbols(ehdr, image_fd, load_bias);
1801 }
bellard31e31b82003-02-18 22:55:36 +00001802
Richard Henderson8e62a712010-07-27 10:25:37 -07001803 close(image_fd);
1804 return;
1805
1806 exit_read:
1807 if (retval >= 0) {
1808 errmsg = "Incomplete read of file header";
1809 goto exit_errmsg;
1810 }
1811 exit_perror:
1812 errmsg = strerror(errno);
1813 exit_errmsg:
1814 fprintf(stderr, "%s: %s\n", image_name, errmsg);
1815 exit(-1);
1816}
1817
1818static void load_elf_interp(const char *filename, struct image_info *info,
1819 char bprm_buf[BPRM_BUF_SIZE])
1820{
1821 int fd, retval;
1822
1823 fd = open(path(filename), O_RDONLY);
1824 if (fd < 0) {
1825 goto exit_perror;
1826 }
1827
1828 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
1829 if (retval < 0) {
1830 goto exit_perror;
1831 }
1832 if (retval < BPRM_BUF_SIZE) {
1833 memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
1834 }
1835
Richard Hendersonbf858892010-07-27 10:25:38 -07001836 load_elf_image(filename, fd, info, NULL, bprm_buf);
Richard Henderson8e62a712010-07-27 10:25:37 -07001837 return;
1838
1839 exit_perror:
1840 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
1841 exit(-1);
bellard31e31b82003-02-18 22:55:36 +00001842}
1843
pbrook49918a72008-10-22 15:11:31 +00001844static int symfind(const void *s0, const void *s1)
1845{
Stefan Weilc7c530c2012-01-05 15:39:39 +01001846 target_ulong addr = *(target_ulong *)s0;
pbrook49918a72008-10-22 15:11:31 +00001847 struct elf_sym *sym = (struct elf_sym *)s1;
1848 int result = 0;
Stefan Weilc7c530c2012-01-05 15:39:39 +01001849 if (addr < sym->st_value) {
pbrook49918a72008-10-22 15:11:31 +00001850 result = -1;
Stefan Weilc7c530c2012-01-05 15:39:39 +01001851 } else if (addr >= sym->st_value + sym->st_size) {
pbrook49918a72008-10-22 15:11:31 +00001852 result = 1;
1853 }
1854 return result;
1855}
1856
1857static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
1858{
1859#if ELF_CLASS == ELFCLASS32
1860 struct elf_sym *syms = s->disas_symtab.elf32;
1861#else
1862 struct elf_sym *syms = s->disas_symtab.elf64;
1863#endif
1864
1865 // binary search
pbrook49918a72008-10-22 15:11:31 +00001866 struct elf_sym *sym;
1867
Stefan Weilc7c530c2012-01-05 15:39:39 +01001868 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
Blue Swirl7cba04f2009-08-01 10:13:20 +00001869 if (sym != NULL) {
pbrook49918a72008-10-22 15:11:31 +00001870 return s->disas_strtab + sym->st_name;
1871 }
1872
1873 return "";
1874}
1875
1876/* FIXME: This should use elf_ops.h */
1877static int symcmp(const void *s0, const void *s1)
1878{
1879 struct elf_sym *sym0 = (struct elf_sym *)s0;
1880 struct elf_sym *sym1 = (struct elf_sym *)s1;
1881 return (sym0->st_value < sym1->st_value)
1882 ? -1
1883 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
1884}
1885
bellard689f9362003-04-29 20:40:07 +00001886/* Best attempt to load symbols from this ELF object. */
Richard Henderson682674b2010-07-27 10:25:33 -07001887static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
bellard689f9362003-04-29 20:40:07 +00001888{
Richard Henderson682674b2010-07-27 10:25:33 -07001889 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
1890 struct elf_shdr *shdr;
Cédric VINCENTb9475272011-06-01 14:36:38 +02001891 char *strings = NULL;
1892 struct syminfo *s = NULL;
1893 struct elf_sym *new_syms, *syms = NULL;
bellard31e31b82003-02-18 22:55:36 +00001894
Richard Henderson682674b2010-07-27 10:25:33 -07001895 shnum = hdr->e_shnum;
1896 i = shnum * sizeof(struct elf_shdr);
1897 shdr = (struct elf_shdr *)alloca(i);
1898 if (pread(fd, shdr, i, hdr->e_shoff) != i) {
1899 return;
1900 }
1901
1902 bswap_shdr(shdr, shnum);
1903 for (i = 0; i < shnum; ++i) {
1904 if (shdr[i].sh_type == SHT_SYMTAB) {
1905 sym_idx = i;
1906 str_idx = shdr[i].sh_link;
pbrook49918a72008-10-22 15:11:31 +00001907 goto found;
1908 }
bellard689f9362003-04-29 20:40:07 +00001909 }
Richard Henderson682674b2010-07-27 10:25:33 -07001910
1911 /* There will be no symbol table if the file was stripped. */
1912 return;
bellard689f9362003-04-29 20:40:07 +00001913
1914 found:
Richard Henderson682674b2010-07-27 10:25:33 -07001915 /* Now know where the strtab and symtab are. Snarf them. */
bellarde80cfcf2004-12-19 23:18:01 +00001916 s = malloc(sizeof(*s));
Richard Henderson682674b2010-07-27 10:25:33 -07001917 if (!s) {
Cédric VINCENTb9475272011-06-01 14:36:38 +02001918 goto give_up;
Richard Henderson682674b2010-07-27 10:25:33 -07001919 }
1920
1921 i = shdr[str_idx].sh_size;
1922 s->disas_strtab = strings = malloc(i);
1923 if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
Cédric VINCENTb9475272011-06-01 14:36:38 +02001924 goto give_up;
Richard Henderson682674b2010-07-27 10:25:33 -07001925 }
ths5fafdf22007-09-16 21:08:06 +00001926
Richard Henderson682674b2010-07-27 10:25:33 -07001927 i = shdr[sym_idx].sh_size;
1928 syms = malloc(i);
1929 if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
Cédric VINCENTb9475272011-06-01 14:36:38 +02001930 goto give_up;
Richard Henderson682674b2010-07-27 10:25:33 -07001931 }
bellard689f9362003-04-29 20:40:07 +00001932
Richard Henderson682674b2010-07-27 10:25:33 -07001933 nsyms = i / sizeof(struct elf_sym);
1934 for (i = 0; i < nsyms; ) {
pbrook49918a72008-10-22 15:11:31 +00001935 bswap_sym(syms + i);
Richard Henderson682674b2010-07-27 10:25:33 -07001936 /* Throw away entries which we do not need. */
1937 if (syms[i].st_shndx == SHN_UNDEF
1938 || syms[i].st_shndx >= SHN_LORESERVE
1939 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
1940 if (i < --nsyms) {
pbrook49918a72008-10-22 15:11:31 +00001941 syms[i] = syms[nsyms];
1942 }
Richard Henderson682674b2010-07-27 10:25:33 -07001943 } else {
pbrook49918a72008-10-22 15:11:31 +00001944#if defined(TARGET_ARM) || defined (TARGET_MIPS)
Richard Henderson682674b2010-07-27 10:25:33 -07001945 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
1946 syms[i].st_value &= ~(target_ulong)1;
blueswir10774bed2007-07-05 13:23:29 +00001947#endif
Richard Henderson682674b2010-07-27 10:25:33 -07001948 syms[i].st_value += load_bias;
1949 i++;
1950 }
blueswir10774bed2007-07-05 13:23:29 +00001951 }
bellard689f9362003-04-29 20:40:07 +00001952
Cédric VINCENTb9475272011-06-01 14:36:38 +02001953 /* No "useful" symbol. */
1954 if (nsyms == 0) {
1955 goto give_up;
1956 }
1957
Richard Henderson5d5c9932010-07-29 09:37:01 -07001958 /* Attempt to free the storage associated with the local symbols
1959 that we threw away. Whether or not this has any effect on the
1960 memory allocation depends on the malloc implementation and how
1961 many symbols we managed to discard. */
Stefan Weil8d79de62011-01-17 21:36:06 +01001962 new_syms = realloc(syms, nsyms * sizeof(*syms));
1963 if (new_syms == NULL) {
Cédric VINCENTb9475272011-06-01 14:36:38 +02001964 goto give_up;
Richard Henderson5d5c9932010-07-29 09:37:01 -07001965 }
Stefan Weil8d79de62011-01-17 21:36:06 +01001966 syms = new_syms;
Richard Henderson5d5c9932010-07-29 09:37:01 -07001967
pbrook49918a72008-10-22 15:11:31 +00001968 qsort(syms, nsyms, sizeof(*syms), symcmp);
1969
pbrook49918a72008-10-22 15:11:31 +00001970 s->disas_num_syms = nsyms;
1971#if ELF_CLASS == ELFCLASS32
1972 s->disas_symtab.elf32 = syms;
pbrook49918a72008-10-22 15:11:31 +00001973#else
1974 s->disas_symtab.elf64 = syms;
pbrook49918a72008-10-22 15:11:31 +00001975#endif
Richard Henderson682674b2010-07-27 10:25:33 -07001976 s->lookup_symbol = lookup_symbolxx;
bellarde80cfcf2004-12-19 23:18:01 +00001977 s->next = syminfos;
1978 syminfos = s;
Cédric VINCENTb9475272011-06-01 14:36:38 +02001979
1980 return;
1981
1982give_up:
1983 free(s);
1984 free(strings);
1985 free(syms);
bellard689f9362003-04-29 20:40:07 +00001986}
bellard31e31b82003-02-18 22:55:36 +00001987
pbrooke5fe0c52006-06-11 13:32:59 +00001988int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
1989 struct image_info * info)
bellard31e31b82003-02-18 22:55:36 +00001990{
Richard Henderson8e62a712010-07-27 10:25:37 -07001991 struct image_info interp_info;
bellard31e31b82003-02-18 22:55:36 +00001992 struct elfhdr elf_ex;
Richard Henderson8e62a712010-07-27 10:25:37 -07001993 char *elf_interpreter = NULL;
bellard31e31b82003-02-18 22:55:36 +00001994
Richard Hendersonbf858892010-07-27 10:25:38 -07001995 info->start_mmap = (abi_ulong)ELF_START_MMAP;
1996 info->mmap = 0;
1997 info->rss = 0;
bellard31e31b82003-02-18 22:55:36 +00001998
Richard Hendersonbf858892010-07-27 10:25:38 -07001999 load_elf_image(bprm->filename, bprm->fd, info,
2000 &elf_interpreter, bprm->buf);
2001
2002 /* ??? We need a copy of the elf header for passing to create_elf_tables.
2003 If we do nothing, we'll have overwritten this when we re-use bprm->buf
2004 when we load the interpreter. */
2005 elf_ex = *(struct elfhdr *)bprm->buf;
bellard31e31b82003-02-18 22:55:36 +00002006
pbrooke5fe0c52006-06-11 13:32:59 +00002007 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
2008 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
2009 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
2010 if (!bprm->p) {
Richard Hendersonbf858892010-07-27 10:25:38 -07002011 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
2012 exit(-1);
pbrooke5fe0c52006-06-11 13:32:59 +00002013 }
2014
bellard31e31b82003-02-18 22:55:36 +00002015 /* Do this so that we can load the interpreter, if need be. We will
2016 change some of these later */
bellard31e31b82003-02-18 22:55:36 +00002017 bprm->p = setup_arg_pages(bprm->p, bprm, info);
bellard31e31b82003-02-18 22:55:36 +00002018
Richard Henderson8e62a712010-07-27 10:25:37 -07002019 if (elf_interpreter) {
2020 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
bellard31e31b82003-02-18 22:55:36 +00002021
Richard Henderson8e62a712010-07-27 10:25:37 -07002022 /* If the program interpreter is one of these two, then assume
2023 an iBCS2 image. Otherwise assume a native linux image. */
bellard31e31b82003-02-18 22:55:36 +00002024
Richard Henderson8e62a712010-07-27 10:25:37 -07002025 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
2026 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
2027 info->personality = PER_SVR4;
2028
2029 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
2030 and some applications "depend" upon this behavior. Since
2031 we do not have the power to recompile these, we emulate
2032 the SVr4 behavior. Sigh. */
2033 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
2034 MAP_FIXED | MAP_PRIVATE, -1, 0);
2035 }
bellard31e31b82003-02-18 22:55:36 +00002036 }
2037
Richard Henderson8e62a712010-07-27 10:25:37 -07002038 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
2039 info, (elf_interpreter ? &interp_info : NULL));
2040 info->start_stack = bprm->p;
2041
2042 /* If we have an interpreter, set that as the program's entry point.
Richard Henderson8e780642011-10-26 09:59:17 -07002043 Copy the load_bias as well, to help PPC64 interpret the entry
Richard Henderson8e62a712010-07-27 10:25:37 -07002044 point as a function descriptor. Do this after creating elf tables
2045 so that we copy the original program entry point into the AUXV. */
2046 if (elf_interpreter) {
Richard Henderson8e780642011-10-26 09:59:17 -07002047 info->load_bias = interp_info.load_bias;
Richard Henderson8e62a712010-07-27 10:25:37 -07002048 info->entry = interp_info.entry;
Richard Hendersonbf858892010-07-27 10:25:38 -07002049 free(elf_interpreter);
Richard Henderson8e62a712010-07-27 10:25:37 -07002050 }
bellard31e31b82003-02-18 22:55:36 +00002051
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002052#ifdef USE_ELF_CORE_DUMP
2053 bprm->core_dump = &elf_core_dump;
2054#endif
2055
bellard31e31b82003-02-18 22:55:36 +00002056 return 0;
2057}
2058
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002059#ifdef USE_ELF_CORE_DUMP
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002060/*
2061 * Definitions to generate Intel SVR4-like core files.
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002062 * These mostly have the same names as the SVR4 types with "target_elf_"
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002063 * tacked on the front to prevent clashes with linux definitions,
2064 * and the typedef forms have been avoided. This is mostly like
2065 * the SVR4 structure, but more Linuxy, with things that Linux does
2066 * not support and which gdb doesn't really use excluded.
2067 *
2068 * Fields we don't dump (their contents is zero) in linux-user qemu
2069 * are marked with XXX.
2070 *
2071 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
2072 *
2073 * Porting ELF coredump for target is (quite) simple process. First you
Nathan Froyddd0a3652009-12-11 09:04:45 -08002074 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002075 * the target resides):
2076 *
2077 * #define USE_ELF_CORE_DUMP
2078 *
2079 * Next you define type of register set used for dumping. ELF specification
2080 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
2081 *
Anthony Liguoric227f092009-10-01 16:12:16 -05002082 * typedef <target_regtype> target_elf_greg_t;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002083 * #define ELF_NREG <number of registers>
Anthony Liguoric227f092009-10-01 16:12:16 -05002084 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002085 *
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002086 * Last step is to implement target specific function that copies registers
2087 * from given cpu into just specified register set. Prototype is:
2088 *
Anthony Liguoric227f092009-10-01 16:12:16 -05002089 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
Andreas Färber9349b4f2012-03-14 01:38:32 +01002090 * const CPUArchState *env);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002091 *
2092 * Parameters:
2093 * regs - copy register values into here (allocated and zeroed by caller)
2094 * env - copy registers from here
2095 *
2096 * Example for ARM target is provided in this file.
2097 */
2098
2099/* An ELF note in memory */
2100struct memelfnote {
2101 const char *name;
2102 size_t namesz;
2103 size_t namesz_rounded;
2104 int type;
2105 size_t datasz;
Laurent Vivier80f5ce72011-02-13 23:37:35 +01002106 size_t datasz_rounded;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002107 void *data;
2108 size_t notesz;
2109};
2110
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002111struct target_elf_siginfo {
Paolo Bonzinif8fd4fc2013-04-17 16:26:40 +02002112 abi_int si_signo; /* signal number */
2113 abi_int si_code; /* extra code */
2114 abi_int si_errno; /* errno */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002115};
2116
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002117struct target_elf_prstatus {
2118 struct target_elf_siginfo pr_info; /* Info associated with signal */
Paolo Bonzini1ddd5922013-04-17 16:26:39 +02002119 abi_short pr_cursig; /* Current signal */
Paolo Bonzinica98ac82013-04-17 16:26:36 +02002120 abi_ulong pr_sigpend; /* XXX */
2121 abi_ulong pr_sighold; /* XXX */
Anthony Liguoric227f092009-10-01 16:12:16 -05002122 target_pid_t pr_pid;
2123 target_pid_t pr_ppid;
2124 target_pid_t pr_pgrp;
2125 target_pid_t pr_sid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002126 struct target_timeval pr_utime; /* XXX User time */
2127 struct target_timeval pr_stime; /* XXX System time */
2128 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2129 struct target_timeval pr_cstime; /* XXX Cumulative system time */
Anthony Liguoric227f092009-10-01 16:12:16 -05002130 target_elf_gregset_t pr_reg; /* GP registers */
Paolo Bonzinif8fd4fc2013-04-17 16:26:40 +02002131 abi_int pr_fpvalid; /* XXX */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002132};
2133
2134#define ELF_PRARGSZ (80) /* Number of chars for args */
2135
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002136struct target_elf_prpsinfo {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002137 char pr_state; /* numeric process state */
2138 char pr_sname; /* char for pr_state */
2139 char pr_zomb; /* zombie */
2140 char pr_nice; /* nice val */
Paolo Bonzinica98ac82013-04-17 16:26:36 +02002141 abi_ulong pr_flag; /* flags */
Anthony Liguoric227f092009-10-01 16:12:16 -05002142 target_uid_t pr_uid;
2143 target_gid_t pr_gid;
2144 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002145 /* Lots missing */
2146 char pr_fname[16]; /* filename of executable */
2147 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2148};
2149
2150/* Here is the structure in which status of each thread is captured. */
2151struct elf_thread_status {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002152 QTAILQ_ENTRY(elf_thread_status) ets_link;
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002153 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002154#if 0
2155 elf_fpregset_t fpu; /* NT_PRFPREG */
2156 struct task_struct *thread;
2157 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2158#endif
2159 struct memelfnote notes[1];
2160 int num_notes;
2161};
2162
2163struct elf_note_info {
2164 struct memelfnote *notes;
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002165 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2166 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002167
Blue Swirl72cf2d42009-09-12 07:36:22 +00002168 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002169#if 0
2170 /*
2171 * Current version of ELF coredump doesn't support
2172 * dumping fp regs etc.
2173 */
2174 elf_fpregset_t *fpu;
2175 elf_fpxregset_t *xfpu;
2176 int thread_status_size;
2177#endif
2178 int notes_size;
2179 int numnote;
2180};
2181
2182struct vm_area_struct {
2183 abi_ulong vma_start; /* start vaddr of memory region */
2184 abi_ulong vma_end; /* end vaddr of memory region */
2185 abi_ulong vma_flags; /* protection etc. flags for the region */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002186 QTAILQ_ENTRY(vm_area_struct) vma_link;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002187};
2188
2189struct mm_struct {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002190 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002191 int mm_count; /* number of mappings */
2192};
2193
2194static struct mm_struct *vma_init(void);
2195static void vma_delete(struct mm_struct *);
2196static int vma_add_mapping(struct mm_struct *, abi_ulong,
Richard Hendersond97ef722010-07-27 10:25:29 -07002197 abi_ulong, abi_ulong);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002198static int vma_get_mapping_count(const struct mm_struct *);
2199static struct vm_area_struct *vma_first(const struct mm_struct *);
2200static struct vm_area_struct *vma_next(struct vm_area_struct *);
2201static abi_ulong vma_dump_size(const struct vm_area_struct *);
Paul Brookb480d9b2010-03-12 23:23:29 +00002202static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
Richard Hendersond97ef722010-07-27 10:25:29 -07002203 unsigned long flags);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002204
2205static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2206static void fill_note(struct memelfnote *, const char *, int,
Richard Hendersond97ef722010-07-27 10:25:29 -07002207 unsigned int, void *);
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002208static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2209static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002210static void fill_auxv_note(struct memelfnote *, const TaskState *);
2211static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2212static size_t note_size(const struct memelfnote *);
2213static void free_note_info(struct elf_note_info *);
Andreas Färber9349b4f2012-03-14 01:38:32 +01002214static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
2215static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002216static int core_dump_filename(const TaskState *, char *, size_t);
2217
2218static int dump_write(int, const void *, size_t);
2219static int write_note(struct memelfnote *, int);
2220static int write_note_info(struct elf_note_info *, int);
2221
2222#ifdef BSWAP_NEEDED
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002223static void bswap_prstatus(struct target_elf_prstatus *prstatus)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002224{
Paolo Bonzinica98ac82013-04-17 16:26:36 +02002225 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
2226 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
2227 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002228 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
Paolo Bonzinica98ac82013-04-17 16:26:36 +02002229 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
2230 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002231 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2232 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2233 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2234 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2235 /* cpu times are not filled, so we skip them */
2236 /* regs should be in correct format already */
2237 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2238}
2239
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002240static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002241{
Paolo Bonzinica98ac82013-04-17 16:26:36 +02002242 psinfo->pr_flag = tswapal(psinfo->pr_flag);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002243 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2244 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2245 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2246 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2247 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2248 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2249}
Richard Henderson991f8f02010-07-27 10:25:32 -07002250
2251static void bswap_note(struct elf_note *en)
2252{
2253 bswap32s(&en->n_namesz);
2254 bswap32s(&en->n_descsz);
2255 bswap32s(&en->n_type);
2256}
2257#else
2258static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
2259static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
2260static inline void bswap_note(struct elf_note *en) { }
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002261#endif /* BSWAP_NEEDED */
2262
2263/*
2264 * Minimal support for linux memory regions. These are needed
2265 * when we are finding out what memory exactly belongs to
2266 * emulated process. No locks needed here, as long as
2267 * thread that received the signal is stopped.
2268 */
2269
2270static struct mm_struct *vma_init(void)
2271{
2272 struct mm_struct *mm;
2273
Anthony Liguori7267c092011-08-20 22:09:37 -05002274 if ((mm = g_malloc(sizeof (*mm))) == NULL)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002275 return (NULL);
2276
2277 mm->mm_count = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002278 QTAILQ_INIT(&mm->mm_mmap);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002279
2280 return (mm);
2281}
2282
2283static void vma_delete(struct mm_struct *mm)
2284{
2285 struct vm_area_struct *vma;
2286
2287 while ((vma = vma_first(mm)) != NULL) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002288 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
Anthony Liguori7267c092011-08-20 22:09:37 -05002289 g_free(vma);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002290 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002291 g_free(mm);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002292}
2293
2294static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
Richard Hendersond97ef722010-07-27 10:25:29 -07002295 abi_ulong end, abi_ulong flags)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002296{
2297 struct vm_area_struct *vma;
2298
Anthony Liguori7267c092011-08-20 22:09:37 -05002299 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002300 return (-1);
2301
2302 vma->vma_start = start;
2303 vma->vma_end = end;
2304 vma->vma_flags = flags;
2305
Blue Swirl72cf2d42009-09-12 07:36:22 +00002306 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002307 mm->mm_count++;
2308
2309 return (0);
2310}
2311
2312static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2313{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002314 return (QTAILQ_FIRST(&mm->mm_mmap));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002315}
2316
2317static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2318{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002319 return (QTAILQ_NEXT(vma, vma_link));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002320}
2321
2322static int vma_get_mapping_count(const struct mm_struct *mm)
2323{
2324 return (mm->mm_count);
2325}
2326
2327/*
2328 * Calculate file (dump) size of given memory region.
2329 */
2330static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2331{
2332 /* if we cannot even read the first page, skip it */
2333 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2334 return (0);
2335
2336 /*
2337 * Usually we don't dump executable pages as they contain
2338 * non-writable code that debugger can read directly from
2339 * target library etc. However, thread stacks are marked
2340 * also executable so we read in first page of given region
2341 * and check whether it contains elf header. If there is
2342 * no elf header, we dump it.
2343 */
2344 if (vma->vma_flags & PROT_EXEC) {
2345 char page[TARGET_PAGE_SIZE];
2346
2347 copy_from_user(page, vma->vma_start, sizeof (page));
2348 if ((page[EI_MAG0] == ELFMAG0) &&
2349 (page[EI_MAG1] == ELFMAG1) &&
2350 (page[EI_MAG2] == ELFMAG2) &&
2351 (page[EI_MAG3] == ELFMAG3)) {
2352 /*
2353 * Mappings are possibly from ELF binary. Don't dump
2354 * them.
2355 */
2356 return (0);
2357 }
2358 }
2359
2360 return (vma->vma_end - vma->vma_start);
2361}
2362
Paul Brookb480d9b2010-03-12 23:23:29 +00002363static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
Richard Hendersond97ef722010-07-27 10:25:29 -07002364 unsigned long flags)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002365{
2366 struct mm_struct *mm = (struct mm_struct *)priv;
2367
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002368 vma_add_mapping(mm, start, end, flags);
2369 return (0);
2370}
2371
2372static void fill_note(struct memelfnote *note, const char *name, int type,
Richard Hendersond97ef722010-07-27 10:25:29 -07002373 unsigned int sz, void *data)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002374{
2375 unsigned int namesz;
2376
2377 namesz = strlen(name) + 1;
2378 note->name = name;
2379 note->namesz = namesz;
2380 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2381 note->type = type;
Laurent Vivier80f5ce72011-02-13 23:37:35 +01002382 note->datasz = sz;
2383 note->datasz_rounded = roundup(sz, sizeof (int32_t));
2384
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002385 note->data = data;
2386
2387 /*
2388 * We calculate rounded up note size here as specified by
2389 * ELF document.
2390 */
2391 note->notesz = sizeof (struct elf_note) +
Laurent Vivier80f5ce72011-02-13 23:37:35 +01002392 note->namesz_rounded + note->datasz_rounded;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002393}
2394
2395static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
Richard Hendersond97ef722010-07-27 10:25:29 -07002396 uint32_t flags)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002397{
2398 (void) memset(elf, 0, sizeof(*elf));
2399
2400 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2401 elf->e_ident[EI_CLASS] = ELF_CLASS;
2402 elf->e_ident[EI_DATA] = ELF_DATA;
2403 elf->e_ident[EI_VERSION] = EV_CURRENT;
2404 elf->e_ident[EI_OSABI] = ELF_OSABI;
2405
2406 elf->e_type = ET_CORE;
2407 elf->e_machine = machine;
2408 elf->e_version = EV_CURRENT;
2409 elf->e_phoff = sizeof(struct elfhdr);
2410 elf->e_flags = flags;
2411 elf->e_ehsize = sizeof(struct elfhdr);
2412 elf->e_phentsize = sizeof(struct elf_phdr);
2413 elf->e_phnum = segs;
2414
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002415 bswap_ehdr(elf);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002416}
2417
2418static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2419{
2420 phdr->p_type = PT_NOTE;
2421 phdr->p_offset = offset;
2422 phdr->p_vaddr = 0;
2423 phdr->p_paddr = 0;
2424 phdr->p_filesz = sz;
2425 phdr->p_memsz = 0;
2426 phdr->p_flags = 0;
2427 phdr->p_align = 0;
2428
Richard Henderson991f8f02010-07-27 10:25:32 -07002429 bswap_phdr(phdr, 1);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002430}
2431
2432static size_t note_size(const struct memelfnote *note)
2433{
2434 return (note->notesz);
2435}
2436
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002437static void fill_prstatus(struct target_elf_prstatus *prstatus,
Richard Hendersond97ef722010-07-27 10:25:29 -07002438 const TaskState *ts, int signr)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002439{
2440 (void) memset(prstatus, 0, sizeof (*prstatus));
2441 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2442 prstatus->pr_pid = ts->ts_tid;
2443 prstatus->pr_ppid = getppid();
2444 prstatus->pr_pgrp = getpgrp();
2445 prstatus->pr_sid = getsid(0);
2446
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002447 bswap_prstatus(prstatus);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002448}
2449
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002450static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002451{
Jim Meyering900cfbc2012-10-04 13:09:53 +02002452 char *base_filename;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002453 unsigned int i, len;
2454
2455 (void) memset(psinfo, 0, sizeof (*psinfo));
2456
2457 len = ts->info->arg_end - ts->info->arg_start;
2458 if (len >= ELF_PRARGSZ)
2459 len = ELF_PRARGSZ - 1;
2460 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2461 return -EFAULT;
2462 for (i = 0; i < len; i++)
2463 if (psinfo->pr_psargs[i] == 0)
2464 psinfo->pr_psargs[i] = ' ';
2465 psinfo->pr_psargs[len] = 0;
2466
2467 psinfo->pr_pid = getpid();
2468 psinfo->pr_ppid = getppid();
2469 psinfo->pr_pgrp = getpgrp();
2470 psinfo->pr_sid = getsid(0);
2471 psinfo->pr_uid = getuid();
2472 psinfo->pr_gid = getgid();
2473
Jim Meyering900cfbc2012-10-04 13:09:53 +02002474 base_filename = g_path_get_basename(ts->bprm->filename);
2475 /*
2476 * Using strncpy here is fine: at max-length,
2477 * this field is not NUL-terminated.
2478 */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002479 (void) strncpy(psinfo->pr_fname, base_filename,
Richard Hendersond97ef722010-07-27 10:25:29 -07002480 sizeof(psinfo->pr_fname));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002481
Jim Meyering900cfbc2012-10-04 13:09:53 +02002482 g_free(base_filename);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002483 bswap_psinfo(psinfo);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002484 return (0);
2485}
2486
2487static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2488{
2489 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2490 elf_addr_t orig_auxv = auxv;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002491 void *ptr;
Alexander Graf125b0f52012-01-28 21:12:14 +02002492 int len = ts->info->auxv_len;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002493
2494 /*
2495 * Auxiliary vector is stored in target process stack. It contains
2496 * {type, value} pairs that we need to dump into note. This is not
2497 * strictly necessary but we do it here for sake of completeness.
2498 */
2499
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002500 /* read in whole auxv vector and copy it to memelfnote */
2501 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2502 if (ptr != NULL) {
2503 fill_note(note, "CORE", NT_AUXV, len, ptr);
2504 unlock_user(ptr, auxv, len);
2505 }
2506}
2507
2508/*
2509 * Constructs name of coredump file. We have following convention
2510 * for the name:
2511 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2512 *
2513 * Returns 0 in case of success, -1 otherwise (errno is set).
2514 */
2515static int core_dump_filename(const TaskState *ts, char *buf,
Richard Hendersond97ef722010-07-27 10:25:29 -07002516 size_t bufsize)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002517{
2518 char timestamp[64];
2519 char *filename = NULL;
2520 char *base_filename = NULL;
2521 struct timeval tv;
2522 struct tm tm;
2523
2524 assert(bufsize >= PATH_MAX);
2525
2526 if (gettimeofday(&tv, NULL) < 0) {
2527 (void) fprintf(stderr, "unable to get current timestamp: %s",
Richard Hendersond97ef722010-07-27 10:25:29 -07002528 strerror(errno));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002529 return (-1);
2530 }
2531
2532 filename = strdup(ts->bprm->filename);
2533 base_filename = strdup(basename(filename));
2534 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
Richard Hendersond97ef722010-07-27 10:25:29 -07002535 localtime_r(&tv.tv_sec, &tm));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002536 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
Richard Hendersond97ef722010-07-27 10:25:29 -07002537 base_filename, timestamp, (int)getpid());
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002538 free(base_filename);
2539 free(filename);
2540
2541 return (0);
2542}
2543
2544static int dump_write(int fd, const void *ptr, size_t size)
2545{
2546 const char *bufp = (const char *)ptr;
2547 ssize_t bytes_written, bytes_left;
2548 struct rlimit dumpsize;
2549 off_t pos;
2550
2551 bytes_written = 0;
2552 getrlimit(RLIMIT_CORE, &dumpsize);
2553 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2554 if (errno == ESPIPE) { /* not a seekable stream */
2555 bytes_left = size;
2556 } else {
2557 return pos;
2558 }
2559 } else {
2560 if (dumpsize.rlim_cur <= pos) {
2561 return -1;
2562 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2563 bytes_left = size;
2564 } else {
2565 size_t limit_left=dumpsize.rlim_cur - pos;
2566 bytes_left = limit_left >= size ? size : limit_left ;
2567 }
2568 }
2569
2570 /*
2571 * In normal conditions, single write(2) should do but
2572 * in case of socket etc. this mechanism is more portable.
2573 */
2574 do {
2575 bytes_written = write(fd, bufp, bytes_left);
2576 if (bytes_written < 0) {
2577 if (errno == EINTR)
2578 continue;
2579 return (-1);
2580 } else if (bytes_written == 0) { /* eof */
2581 return (-1);
2582 }
2583 bufp += bytes_written;
2584 bytes_left -= bytes_written;
2585 } while (bytes_left > 0);
2586
2587 return (0);
2588}
2589
2590static int write_note(struct memelfnote *men, int fd)
2591{
2592 struct elf_note en;
2593
2594 en.n_namesz = men->namesz;
2595 en.n_type = men->type;
2596 en.n_descsz = men->datasz;
2597
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002598 bswap_note(&en);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002599
2600 if (dump_write(fd, &en, sizeof(en)) != 0)
2601 return (-1);
2602 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2603 return (-1);
Laurent Vivier80f5ce72011-02-13 23:37:35 +01002604 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002605 return (-1);
2606
2607 return (0);
2608}
2609
Andreas Färber9349b4f2012-03-14 01:38:32 +01002610static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002611{
2612 TaskState *ts = (TaskState *)env->opaque;
2613 struct elf_thread_status *ets;
2614
Anthony Liguori7267c092011-08-20 22:09:37 -05002615 ets = g_malloc0(sizeof (*ets));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002616 ets->num_notes = 1; /* only prstatus is dumped */
2617 fill_prstatus(&ets->prstatus, ts, 0);
2618 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2619 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
Richard Hendersond97ef722010-07-27 10:25:29 -07002620 &ets->prstatus);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002621
Blue Swirl72cf2d42009-09-12 07:36:22 +00002622 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002623
2624 info->notes_size += note_size(&ets->notes[0]);
2625}
2626
2627static int fill_note_info(struct elf_note_info *info,
Andreas Färber9349b4f2012-03-14 01:38:32 +01002628 long signr, const CPUArchState *env)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002629{
2630#define NUMNOTES 3
Andreas Färber9349b4f2012-03-14 01:38:32 +01002631 CPUArchState *cpu = NULL;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002632 TaskState *ts = (TaskState *)env->opaque;
2633 int i;
2634
2635 (void) memset(info, 0, sizeof (*info));
2636
Blue Swirl72cf2d42009-09-12 07:36:22 +00002637 QTAILQ_INIT(&info->thread_list);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002638
Anthony Liguori7267c092011-08-20 22:09:37 -05002639 info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002640 if (info->notes == NULL)
2641 return (-ENOMEM);
Anthony Liguori7267c092011-08-20 22:09:37 -05002642 info->prstatus = g_malloc0(sizeof (*info->prstatus));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002643 if (info->prstatus == NULL)
2644 return (-ENOMEM);
Anthony Liguori7267c092011-08-20 22:09:37 -05002645 info->psinfo = g_malloc0(sizeof (*info->psinfo));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002646 if (info->prstatus == NULL)
2647 return (-ENOMEM);
2648
2649 /*
2650 * First fill in status (and registers) of current thread
2651 * including process info & aux vector.
2652 */
2653 fill_prstatus(info->prstatus, ts, signr);
2654 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2655 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
Richard Hendersond97ef722010-07-27 10:25:29 -07002656 sizeof (*info->prstatus), info->prstatus);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002657 fill_psinfo(info->psinfo, ts);
2658 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
Richard Hendersond97ef722010-07-27 10:25:29 -07002659 sizeof (*info->psinfo), info->psinfo);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002660 fill_auxv_note(&info->notes[2], ts);
2661 info->numnote = 3;
2662
2663 info->notes_size = 0;
2664 for (i = 0; i < info->numnote; i++)
2665 info->notes_size += note_size(&info->notes[i]);
2666
2667 /* read and fill status of all threads */
2668 cpu_list_lock();
2669 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2670 if (cpu == thread_env)
2671 continue;
2672 fill_thread_info(info, cpu);
2673 }
2674 cpu_list_unlock();
2675
2676 return (0);
2677}
2678
2679static void free_note_info(struct elf_note_info *info)
2680{
2681 struct elf_thread_status *ets;
2682
Blue Swirl72cf2d42009-09-12 07:36:22 +00002683 while (!QTAILQ_EMPTY(&info->thread_list)) {
2684 ets = QTAILQ_FIRST(&info->thread_list);
2685 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
Anthony Liguori7267c092011-08-20 22:09:37 -05002686 g_free(ets);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002687 }
2688
Anthony Liguori7267c092011-08-20 22:09:37 -05002689 g_free(info->prstatus);
2690 g_free(info->psinfo);
2691 g_free(info->notes);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002692}
2693
2694static int write_note_info(struct elf_note_info *info, int fd)
2695{
2696 struct elf_thread_status *ets;
2697 int i, error = 0;
2698
2699 /* write prstatus, psinfo and auxv for current thread */
2700 for (i = 0; i < info->numnote; i++)
2701 if ((error = write_note(&info->notes[i], fd)) != 0)
2702 return (error);
2703
2704 /* write prstatus for each thread */
2705 for (ets = info->thread_list.tqh_first; ets != NULL;
Richard Hendersond97ef722010-07-27 10:25:29 -07002706 ets = ets->ets_link.tqe_next) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002707 if ((error = write_note(&ets->notes[0], fd)) != 0)
2708 return (error);
2709 }
2710
2711 return (0);
2712}
2713
2714/*
2715 * Write out ELF coredump.
2716 *
2717 * See documentation of ELF object file format in:
2718 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2719 *
2720 * Coredump format in linux is following:
2721 *
2722 * 0 +----------------------+ \
2723 * | ELF header | ET_CORE |
2724 * +----------------------+ |
2725 * | ELF program headers | |--- headers
2726 * | - NOTE section | |
2727 * | - PT_LOAD sections | |
2728 * +----------------------+ /
2729 * | NOTEs: |
2730 * | - NT_PRSTATUS |
2731 * | - NT_PRSINFO |
2732 * | - NT_AUXV |
2733 * +----------------------+ <-- aligned to target page
2734 * | Process memory dump |
2735 * : :
2736 * . .
2737 * : :
2738 * | |
2739 * +----------------------+
2740 *
2741 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2742 * NT_PRSINFO -> struct elf_prpsinfo
2743 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2744 *
2745 * Format follows System V format as close as possible. Current
2746 * version limitations are as follows:
2747 * - no floating point registers are dumped
2748 *
2749 * Function returns 0 in case of success, negative errno otherwise.
2750 *
2751 * TODO: make this work also during runtime: it should be
2752 * possible to force coredump from running process and then
2753 * continue processing. For example qemu could set up SIGUSR2
2754 * handler (provided that target process haven't registered
2755 * handler for that) that does the dump when signal is received.
2756 */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002757static int elf_core_dump(int signr, const CPUArchState *env)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002758{
2759 const TaskState *ts = (const TaskState *)env->opaque;
2760 struct vm_area_struct *vma = NULL;
2761 char corefile[PATH_MAX];
2762 struct elf_note_info info;
2763 struct elfhdr elf;
2764 struct elf_phdr phdr;
2765 struct rlimit dumpsize;
2766 struct mm_struct *mm = NULL;
2767 off_t offset = 0, data_offset = 0;
2768 int segs = 0;
2769 int fd = -1;
2770
2771 errno = 0;
2772 getrlimit(RLIMIT_CORE, &dumpsize);
2773 if (dumpsize.rlim_cur == 0)
Richard Hendersond97ef722010-07-27 10:25:29 -07002774 return 0;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002775
2776 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2777 return (-errno);
2778
2779 if ((fd = open(corefile, O_WRONLY | O_CREAT,
Richard Hendersond97ef722010-07-27 10:25:29 -07002780 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002781 return (-errno);
2782
2783 /*
2784 * Walk through target process memory mappings and
2785 * set up structure containing this information. After
2786 * this point vma_xxx functions can be used.
2787 */
2788 if ((mm = vma_init()) == NULL)
2789 goto out;
2790
2791 walk_memory_regions(mm, vma_walker);
2792 segs = vma_get_mapping_count(mm);
2793
2794 /*
2795 * Construct valid coredump ELF header. We also
2796 * add one more segment for notes.
2797 */
2798 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
2799 if (dump_write(fd, &elf, sizeof (elf)) != 0)
2800 goto out;
2801
2802 /* fill in in-memory version of notes */
2803 if (fill_note_info(&info, signr, env) < 0)
2804 goto out;
2805
2806 offset += sizeof (elf); /* elf header */
2807 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
2808
2809 /* write out notes program header */
2810 fill_elf_note_phdr(&phdr, info.notes_size, offset);
2811
2812 offset += info.notes_size;
2813 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
2814 goto out;
2815
2816 /*
2817 * ELF specification wants data to start at page boundary so
2818 * we align it here.
2819 */
Laurent Vivier80f5ce72011-02-13 23:37:35 +01002820 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002821
2822 /*
2823 * Write program headers for memory regions mapped in
2824 * the target process.
2825 */
2826 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2827 (void) memset(&phdr, 0, sizeof (phdr));
2828
2829 phdr.p_type = PT_LOAD;
2830 phdr.p_offset = offset;
2831 phdr.p_vaddr = vma->vma_start;
2832 phdr.p_paddr = 0;
2833 phdr.p_filesz = vma_dump_size(vma);
2834 offset += phdr.p_filesz;
2835 phdr.p_memsz = vma->vma_end - vma->vma_start;
2836 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
2837 if (vma->vma_flags & PROT_WRITE)
2838 phdr.p_flags |= PF_W;
2839 if (vma->vma_flags & PROT_EXEC)
2840 phdr.p_flags |= PF_X;
2841 phdr.p_align = ELF_EXEC_PAGESIZE;
2842
Laurent Vivier80f5ce72011-02-13 23:37:35 +01002843 bswap_phdr(&phdr, 1);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002844 dump_write(fd, &phdr, sizeof (phdr));
2845 }
2846
2847 /*
2848 * Next we write notes just after program headers. No
2849 * alignment needed here.
2850 */
2851 if (write_note_info(&info, fd) < 0)
2852 goto out;
2853
2854 /* align data to page boundary */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002855 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
2856 goto out;
2857
2858 /*
2859 * Finally we can dump process memory into corefile as well.
2860 */
2861 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2862 abi_ulong addr;
2863 abi_ulong end;
2864
2865 end = vma->vma_start + vma_dump_size(vma);
2866
2867 for (addr = vma->vma_start; addr < end;
Richard Hendersond97ef722010-07-27 10:25:29 -07002868 addr += TARGET_PAGE_SIZE) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002869 char page[TARGET_PAGE_SIZE];
2870 int error;
2871
2872 /*
2873 * Read in page from target process memory and
2874 * write it to coredump file.
2875 */
2876 error = copy_from_user(page, addr, sizeof (page));
2877 if (error != 0) {
Aurelien Jarno49995e12009-12-19 20:28:23 +01002878 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
Richard Hendersond97ef722010-07-27 10:25:29 -07002879 addr);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002880 errno = -error;
2881 goto out;
2882 }
2883 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
2884 goto out;
2885 }
2886 }
2887
Richard Hendersond97ef722010-07-27 10:25:29 -07002888 out:
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002889 free_note_info(&info);
2890 if (mm != NULL)
2891 vma_delete(mm);
2892 (void) close(fd);
2893
2894 if (errno != 0)
2895 return (-errno);
2896 return (0);
2897}
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002898#endif /* USE_ELF_CORE_DUMP */
2899
pbrooke5fe0c52006-06-11 13:32:59 +00002900void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
2901{
2902 init_thread(regs, infop);
2903}