blob: 6bc51da9572bf9b48b0689138138ee4e128ec63e [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"
bellard689f9362003-04-29 20:40:07 +000017#include "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 {
38 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
39 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to descriptors
40 * (signal handling)
41 */
42 MMAP_PAGE_ZERO = 0x0100000,
43 ADDR_COMPAT_LAYOUT = 0x0200000,
44 READ_IMPLIES_EXEC = 0x0400000,
45 ADDR_LIMIT_32BIT = 0x0800000,
46 SHORT_INODE = 0x1000000,
47 WHOLE_SECONDS = 0x2000000,
48 STICKY_TIMEOUTS = 0x4000000,
49 ADDR_LIMIT_3GB = 0x8000000,
50};
51
52/*
53 * Personality types.
54 *
55 * These go in the low byte. Avoid using the top bit, it will
56 * conflict with error returns.
57 */
58enum {
59 PER_LINUX = 0x0000,
60 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
61 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
62 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
63 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
64 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS |
65 WHOLE_SECONDS | SHORT_INODE,
66 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
67 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
68 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
69 PER_BSD = 0x0006,
70 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
71 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
72 PER_LINUX32 = 0x0008,
73 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
74 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
75 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
76 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
77 PER_RISCOS = 0x000c,
78 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
79 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
80 PER_OSF4 = 0x000f, /* OSF/1 v4 */
81 PER_HPUX = 0x0010,
82 PER_MASK = 0x00ff,
83};
84
85/*
86 * Return the base personality without flags.
87 */
88#define personality(pers) (pers & PER_MASK)
89
bellard83fb7ad2004-07-05 21:25:26 +000090/* this flag is uneffective under linux too, should be deleted */
91#ifndef MAP_DENYWRITE
92#define MAP_DENYWRITE 0
93#endif
94
95/* should probably go in elf.h */
96#ifndef ELIBBAD
97#define ELIBBAD 80
98#endif
99
Nathan Froyd21e807f2009-12-11 09:04:46 -0800100typedef target_ulong target_elf_greg_t;
101#ifdef USE_UID16
102typedef uint16_t target_uid_t;
103typedef uint16_t target_gid_t;
104#else
105typedef uint32_t target_uid_t;
106typedef uint32_t target_gid_t;
107#endif
108typedef int32_t target_pid_t;
109
bellard30ac07d2003-04-07 21:33:03 +0000110#ifdef TARGET_I386
111
bellard15338fd2005-11-26 11:41:16 +0000112#define ELF_PLATFORM get_elf_platform()
113
114static const char *get_elf_platform(void)
115{
116 static char elf_platform[] = "i386";
pbrookd5975362008-06-07 20:50:51 +0000117 int family = (thread_env->cpuid_version >> 8) & 0xff;
bellard15338fd2005-11-26 11:41:16 +0000118 if (family > 6)
119 family = 6;
120 if (family >= 3)
121 elf_platform[1] = '0' + family;
122 return elf_platform;
123}
124
125#define ELF_HWCAP get_elf_hwcap()
126
127static uint32_t get_elf_hwcap(void)
128{
pbrookd5975362008-06-07 20:50:51 +0000129 return thread_env->cpuid_features;
bellard15338fd2005-11-26 11:41:16 +0000130}
131
j_mayer84409dd2007-04-06 08:56:50 +0000132#ifdef TARGET_X86_64
133#define ELF_START_MMAP 0x2aaaaab000ULL
134#define elf_check_arch(x) ( ((x) == ELF_ARCH) )
135
136#define ELF_CLASS ELFCLASS64
137#define ELF_DATA ELFDATA2LSB
138#define ELF_ARCH EM_X86_64
139
140static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
141{
142 regs->rax = 0;
143 regs->rsp = infop->start_stack;
144 regs->rip = infop->entry;
145}
146
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300147#define ELF_NREG 27
Anthony Liguoric227f092009-10-01 16:12:16 -0500148typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300149
150/*
151 * Note that ELF_NREG should be 29 as there should be place for
152 * TRAPNO and ERR "registers" as well but linux doesn't dump
153 * those.
154 *
155 * See linux kernel: arch/x86/include/asm/elf.h
156 */
Anthony Liguoric227f092009-10-01 16:12:16 -0500157static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300158{
159 (*regs)[0] = env->regs[15];
160 (*regs)[1] = env->regs[14];
161 (*regs)[2] = env->regs[13];
162 (*regs)[3] = env->regs[12];
163 (*regs)[4] = env->regs[R_EBP];
164 (*regs)[5] = env->regs[R_EBX];
165 (*regs)[6] = env->regs[11];
166 (*regs)[7] = env->regs[10];
167 (*regs)[8] = env->regs[9];
168 (*regs)[9] = env->regs[8];
169 (*regs)[10] = env->regs[R_EAX];
170 (*regs)[11] = env->regs[R_ECX];
171 (*regs)[12] = env->regs[R_EDX];
172 (*regs)[13] = env->regs[R_ESI];
173 (*regs)[14] = env->regs[R_EDI];
174 (*regs)[15] = env->regs[R_EAX]; /* XXX */
175 (*regs)[16] = env->eip;
176 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
177 (*regs)[18] = env->eflags;
178 (*regs)[19] = env->regs[R_ESP];
179 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
180 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
181 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
182 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
183 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
184 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
185 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
186}
187
j_mayer84409dd2007-04-06 08:56:50 +0000188#else
189
bellard30ac07d2003-04-07 21:33:03 +0000190#define ELF_START_MMAP 0x80000000
191
bellard30ac07d2003-04-07 21:33:03 +0000192/*
193 * This is used to ensure we don't load something for the wrong architecture.
194 */
195#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
196
197/*
198 * These are used to set parameters in the core dumps.
199 */
200#define ELF_CLASS ELFCLASS32
201#define ELF_DATA ELFDATA2LSB
202#define ELF_ARCH EM_386
203
bellardb346ff42003-06-15 20:05:50 +0000204static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
205{
206 regs->esp = infop->start_stack;
207 regs->eip = infop->entry;
pbrooke5fe0c52006-06-11 13:32:59 +0000208
209 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
210 starts %edx contains a pointer to a function which might be
211 registered using `atexit'. This provides a mean for the
212 dynamic linker to call DT_FINI functions for shared libraries
213 that have been loaded before the code runs.
214
215 A value of 0 tells we have no such handler. */
216 regs->edx = 0;
bellardb346ff42003-06-15 20:05:50 +0000217}
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300218
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300219#define ELF_NREG 17
Anthony Liguoric227f092009-10-01 16:12:16 -0500220typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300221
222/*
223 * Note that ELF_NREG should be 19 as there should be place for
224 * TRAPNO and ERR "registers" as well but linux doesn't dump
225 * those.
226 *
227 * See linux kernel: arch/x86/include/asm/elf.h
228 */
Anthony Liguoric227f092009-10-01 16:12:16 -0500229static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300230{
231 (*regs)[0] = env->regs[R_EBX];
232 (*regs)[1] = env->regs[R_ECX];
233 (*regs)[2] = env->regs[R_EDX];
234 (*regs)[3] = env->regs[R_ESI];
235 (*regs)[4] = env->regs[R_EDI];
236 (*regs)[5] = env->regs[R_EBP];
237 (*regs)[6] = env->regs[R_EAX];
238 (*regs)[7] = env->segs[R_DS].selector & 0xffff;
239 (*regs)[8] = env->segs[R_ES].selector & 0xffff;
240 (*regs)[9] = env->segs[R_FS].selector & 0xffff;
241 (*regs)[10] = env->segs[R_GS].selector & 0xffff;
242 (*regs)[11] = env->regs[R_EAX]; /* XXX */
243 (*regs)[12] = env->eip;
244 (*regs)[13] = env->segs[R_CS].selector & 0xffff;
245 (*regs)[14] = env->eflags;
246 (*regs)[15] = env->regs[R_ESP];
247 (*regs)[16] = env->segs[R_SS].selector & 0xffff;
248}
j_mayer84409dd2007-04-06 08:56:50 +0000249#endif
bellardb346ff42003-06-15 20:05:50 +0000250
Mika Westerberg9edc5d72009-04-07 09:57:59 +0300251#define USE_ELF_CORE_DUMP
bellardb346ff42003-06-15 20:05:50 +0000252#define ELF_EXEC_PAGESIZE 4096
253
254#endif
255
256#ifdef TARGET_ARM
257
258#define ELF_START_MMAP 0x80000000
259
260#define elf_check_arch(x) ( (x) == EM_ARM )
261
262#define ELF_CLASS ELFCLASS32
263#ifdef TARGET_WORDS_BIGENDIAN
264#define ELF_DATA ELFDATA2MSB
265#else
266#define ELF_DATA ELFDATA2LSB
267#endif
268#define ELF_ARCH EM_ARM
269
bellardb346ff42003-06-15 20:05:50 +0000270static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
271{
blueswir1992f48a2007-10-14 16:27:31 +0000272 abi_long stack = infop->start_stack;
bellardb346ff42003-06-15 20:05:50 +0000273 memset(regs, 0, sizeof(*regs));
274 regs->ARM_cpsr = 0x10;
pbrook0240ded2006-02-04 19:30:51 +0000275 if (infop->entry & 1)
276 regs->ARM_cpsr |= CPSR_T;
277 regs->ARM_pc = infop->entry & 0xfffffffe;
bellardb346ff42003-06-15 20:05:50 +0000278 regs->ARM_sp = infop->start_stack;
bellard2f619692007-11-16 10:46:05 +0000279 /* FIXME - what to for failure of get_user()? */
280 get_user_ual(regs->ARM_r2, stack + 8); /* envp */
281 get_user_ual(regs->ARM_r1, stack + 4); /* envp */
bellarda1516e92003-07-09 17:13:37 +0000282 /* XXX: it seems that r0 is zeroed after ! */
pbrooke5fe0c52006-06-11 13:32:59 +0000283 regs->ARM_r0 = 0;
284 /* For uClinux PIC binaries. */
j_mayer863cf0b2007-10-07 15:59:45 +0000285 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
pbrooke5fe0c52006-06-11 13:32:59 +0000286 regs->ARM_r10 = infop->start_data;
bellardb346ff42003-06-15 20:05:50 +0000287}
288
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300289#define ELF_NREG 18
Anthony Liguoric227f092009-10-01 16:12:16 -0500290typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300291
Anthony Liguoric227f092009-10-01 16:12:16 -0500292static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300293{
Nathan Froydd049e622009-12-11 09:04:47 -0800294 (*regs)[0] = tswapl(env->regs[0]);
295 (*regs)[1] = tswapl(env->regs[1]);
296 (*regs)[2] = tswapl(env->regs[2]);
297 (*regs)[3] = tswapl(env->regs[3]);
298 (*regs)[4] = tswapl(env->regs[4]);
299 (*regs)[5] = tswapl(env->regs[5]);
300 (*regs)[6] = tswapl(env->regs[6]);
301 (*regs)[7] = tswapl(env->regs[7]);
302 (*regs)[8] = tswapl(env->regs[8]);
303 (*regs)[9] = tswapl(env->regs[9]);
304 (*regs)[10] = tswapl(env->regs[10]);
305 (*regs)[11] = tswapl(env->regs[11]);
306 (*regs)[12] = tswapl(env->regs[12]);
307 (*regs)[13] = tswapl(env->regs[13]);
308 (*regs)[14] = tswapl(env->regs[14]);
309 (*regs)[15] = tswapl(env->regs[15]);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300310
Nathan Froydd049e622009-12-11 09:04:47 -0800311 (*regs)[16] = tswapl(cpsr_read((CPUState *)env));
312 (*regs)[17] = tswapl(env->regs[0]); /* XXX */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300313}
314
bellard30ac07d2003-04-07 21:33:03 +0000315#define USE_ELF_CORE_DUMP
316#define ELF_EXEC_PAGESIZE 4096
317
bellardafce2922005-10-30 20:58:30 +0000318enum
319{
320 ARM_HWCAP_ARM_SWP = 1 << 0,
321 ARM_HWCAP_ARM_HALF = 1 << 1,
322 ARM_HWCAP_ARM_THUMB = 1 << 2,
323 ARM_HWCAP_ARM_26BIT = 1 << 3,
324 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
325 ARM_HWCAP_ARM_FPA = 1 << 5,
326 ARM_HWCAP_ARM_VFP = 1 << 6,
327 ARM_HWCAP_ARM_EDSP = 1 << 7,
Riku Voipiocf6de342009-09-30 11:44:34 +0300328 ARM_HWCAP_ARM_JAVA = 1 << 8,
329 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
330 ARM_HWCAP_ARM_THUMBEE = 1 << 10,
331 ARM_HWCAP_ARM_NEON = 1 << 11,
332 ARM_HWCAP_ARM_VFPv3 = 1 << 12,
333 ARM_HWCAP_ARM_VFPv3D16 = 1 << 13,
bellardafce2922005-10-30 20:58:30 +0000334};
335
bellard15338fd2005-11-26 11:41:16 +0000336#define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF \
bellardafce2922005-10-30 20:58:30 +0000337 | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT \
Riku Voipiocf6de342009-09-30 11:44:34 +0300338 | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP \
339 | ARM_HWCAP_ARM_NEON | ARM_HWCAP_ARM_VFPv3 )
bellardafce2922005-10-30 20:58:30 +0000340
bellard30ac07d2003-04-07 21:33:03 +0000341#endif
342
bellard853d6f72003-09-30 20:58:32 +0000343#ifdef TARGET_SPARC
bellarda315a142005-01-30 22:59:18 +0000344#ifdef TARGET_SPARC64
bellard853d6f72003-09-30 20:58:32 +0000345
346#define ELF_START_MMAP 0x80000000
347
blueswir1992f48a2007-10-14 16:27:31 +0000348#ifndef TARGET_ABI32
blueswir1cb33da52007-10-09 16:34:29 +0000349#define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
blueswir1992f48a2007-10-14 16:27:31 +0000350#else
351#define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
352#endif
bellard853d6f72003-09-30 20:58:32 +0000353
bellarda315a142005-01-30 22:59:18 +0000354#define ELF_CLASS ELFCLASS64
355#define ELF_DATA ELFDATA2MSB
bellard5ef54112006-07-18 21:14:09 +0000356#define ELF_ARCH EM_SPARCV9
357
358#define STACK_BIAS 2047
bellarda315a142005-01-30 22:59:18 +0000359
bellarda315a142005-01-30 22:59:18 +0000360static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
361{
blueswir1992f48a2007-10-14 16:27:31 +0000362#ifndef TARGET_ABI32
bellarda315a142005-01-30 22:59:18 +0000363 regs->tstate = 0;
blueswir1992f48a2007-10-14 16:27:31 +0000364#endif
bellarda315a142005-01-30 22:59:18 +0000365 regs->pc = infop->entry;
366 regs->npc = regs->pc + 4;
367 regs->y = 0;
blueswir1992f48a2007-10-14 16:27:31 +0000368#ifdef TARGET_ABI32
369 regs->u_regs[14] = infop->start_stack - 16 * 4;
370#else
blueswir1cb33da52007-10-09 16:34:29 +0000371 if (personality(infop->personality) == PER_LINUX32)
372 regs->u_regs[14] = infop->start_stack - 16 * 4;
373 else
374 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
blueswir1992f48a2007-10-14 16:27:31 +0000375#endif
bellarda315a142005-01-30 22:59:18 +0000376}
377
378#else
379#define ELF_START_MMAP 0x80000000
380
381#define elf_check_arch(x) ( (x) == EM_SPARC )
382
bellard853d6f72003-09-30 20:58:32 +0000383#define ELF_CLASS ELFCLASS32
384#define ELF_DATA ELFDATA2MSB
385#define ELF_ARCH EM_SPARC
386
bellard853d6f72003-09-30 20:58:32 +0000387static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
388{
bellardf5155282004-01-04 15:46:50 +0000389 regs->psr = 0;
390 regs->pc = infop->entry;
391 regs->npc = regs->pc + 4;
392 regs->y = 0;
393 regs->u_regs[14] = infop->start_stack - 16 * 4;
bellard853d6f72003-09-30 20:58:32 +0000394}
395
396#endif
bellarda315a142005-01-30 22:59:18 +0000397#endif
bellard853d6f72003-09-30 20:58:32 +0000398
bellard67867302003-11-23 17:05:30 +0000399#ifdef TARGET_PPC
400
401#define ELF_START_MMAP 0x80000000
402
j_mayere85e7c62007-10-18 19:59:49 +0000403#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
j_mayer84409dd2007-04-06 08:56:50 +0000404
405#define elf_check_arch(x) ( (x) == EM_PPC64 )
406
407#define ELF_CLASS ELFCLASS64
408
409#else
410
bellard67867302003-11-23 17:05:30 +0000411#define elf_check_arch(x) ( (x) == EM_PPC )
412
413#define ELF_CLASS ELFCLASS32
j_mayer84409dd2007-04-06 08:56:50 +0000414
415#endif
416
bellard67867302003-11-23 17:05:30 +0000417#ifdef TARGET_WORDS_BIGENDIAN
418#define ELF_DATA ELFDATA2MSB
419#else
420#define ELF_DATA ELFDATA2LSB
421#endif
422#define ELF_ARCH EM_PPC
423
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700424/* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
425 See arch/powerpc/include/asm/cputable.h. */
426enum {
malc3efa9a62009-07-18 13:10:12 +0400427 QEMU_PPC_FEATURE_32 = 0x80000000,
428 QEMU_PPC_FEATURE_64 = 0x40000000,
429 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
430 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
431 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
432 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
433 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
434 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
435 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
436 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
437 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
438 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
439 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
440 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
441 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
442 QEMU_PPC_FEATURE_CELL = 0x00010000,
443 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
444 QEMU_PPC_FEATURE_SMT = 0x00004000,
445 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
446 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
447 QEMU_PPC_FEATURE_PA6T = 0x00000800,
448 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
449 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
450 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
451 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
452 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700453
malc3efa9a62009-07-18 13:10:12 +0400454 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
455 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700456};
457
458#define ELF_HWCAP get_elf_hwcap()
459
460static uint32_t get_elf_hwcap(void)
461{
462 CPUState *e = thread_env;
463 uint32_t features = 0;
464
465 /* We don't have to be terribly complete here; the high points are
466 Altivec/FP/SPE support. Anything else is just a bonus. */
467#define GET_FEATURE(flag, feature) \
468 do {if (e->insns_flags & flag) features |= feature; } while(0)
malc3efa9a62009-07-18 13:10:12 +0400469 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
470 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
471 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
472 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
473 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
474 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
475 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
476 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
Nathan Froyddf84e4f2009-05-12 12:26:59 -0700477#undef GET_FEATURE
478
479 return features;
480}
481
bellardf5155282004-01-04 15:46:50 +0000482/*
483 * We need to put in some extra aux table entries to tell glibc what
484 * the cache block size is, so it can use the dcbz instruction safely.
485 */
486#define AT_DCACHEBSIZE 19
487#define AT_ICACHEBSIZE 20
488#define AT_UCACHEBSIZE 21
489/* A special ignored type value for PPC, for glibc compatibility. */
490#define AT_IGNOREPPC 22
491/*
492 * The requirements here are:
493 * - keep the final alignment of sp (sp & 0xf)
494 * - make sure the 32-bit value at the first 16 byte aligned position of
495 * AUXV is greater than 16 for glibc compatibility.
496 * AT_IGNOREPPC is used for that.
497 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
498 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
499 */
bellard0bccf032005-08-21 10:12:28 +0000500#define DLINFO_ARCH_ITEMS 5
bellardf5155282004-01-04 15:46:50 +0000501#define ARCH_DLINFO \
502do { \
bellard0bccf032005-08-21 10:12:28 +0000503 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
504 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
505 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
bellardf5155282004-01-04 15:46:50 +0000506 /* \
507 * Now handle glibc compatibility. \
508 */ \
bellard0bccf032005-08-21 10:12:28 +0000509 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
510 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
bellardf5155282004-01-04 15:46:50 +0000511 } while (0)
512
bellard67867302003-11-23 17:05:30 +0000513static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
514{
bellard67867302003-11-23 17:05:30 +0000515 _regs->gpr[1] = infop->start_stack;
j_mayere85e7c62007-10-18 19:59:49 +0000516#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
Rob Landley7983f432010-03-28 16:51:43 +0200517 _regs->gpr[2] = ldq_raw(infop->entry + 8) + infop->load_addr;
518 infop->entry = ldq_raw(infop->entry) + infop->load_addr;
j_mayer84409dd2007-04-06 08:56:50 +0000519#endif
bellard67867302003-11-23 17:05:30 +0000520 _regs->nip = infop->entry;
521}
522
Nathan Froyde2f3e742009-12-11 09:04:48 -0800523/* See linux kernel: arch/powerpc/include/asm/elf.h. */
524#define ELF_NREG 48
525typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
526
527static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
528{
529 int i;
530 target_ulong ccr = 0;
531
532 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
533 (*regs)[i] = tswapl(env->gpr[i]);
534 }
535
536 (*regs)[32] = tswapl(env->nip);
537 (*regs)[33] = tswapl(env->msr);
538 (*regs)[35] = tswapl(env->ctr);
539 (*regs)[36] = tswapl(env->lr);
540 (*regs)[37] = tswapl(env->xer);
541
542 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
543 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
544 }
545 (*regs)[38] = tswapl(ccr);
546}
547
548#define USE_ELF_CORE_DUMP
bellard67867302003-11-23 17:05:30 +0000549#define ELF_EXEC_PAGESIZE 4096
550
551#endif
552
bellard048f6b42005-11-26 18:47:20 +0000553#ifdef TARGET_MIPS
554
555#define ELF_START_MMAP 0x80000000
556
557#define elf_check_arch(x) ( (x) == EM_MIPS )
558
ths388bb212007-05-13 13:58:00 +0000559#ifdef TARGET_MIPS64
560#define ELF_CLASS ELFCLASS64
561#else
bellard048f6b42005-11-26 18:47:20 +0000562#define ELF_CLASS ELFCLASS32
ths388bb212007-05-13 13:58:00 +0000563#endif
bellard048f6b42005-11-26 18:47:20 +0000564#ifdef TARGET_WORDS_BIGENDIAN
565#define ELF_DATA ELFDATA2MSB
566#else
567#define ELF_DATA ELFDATA2LSB
568#endif
569#define ELF_ARCH EM_MIPS
570
bellard048f6b42005-11-26 18:47:20 +0000571static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
572{
ths623a9302007-10-28 19:45:05 +0000573 regs->cp0_status = 2 << CP0St_KSU;
bellard048f6b42005-11-26 18:47:20 +0000574 regs->cp0_epc = infop->entry;
575 regs->regs[29] = infop->start_stack;
576}
577
Nathan Froyd51e52602009-12-11 09:04:49 -0800578/* See linux kernel: arch/mips/include/asm/elf.h. */
579#define ELF_NREG 45
580typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
581
582/* See linux kernel: arch/mips/include/asm/reg.h. */
583enum {
584#ifdef TARGET_MIPS64
585 TARGET_EF_R0 = 0,
586#else
587 TARGET_EF_R0 = 6,
588#endif
589 TARGET_EF_R26 = TARGET_EF_R0 + 26,
590 TARGET_EF_R27 = TARGET_EF_R0 + 27,
591 TARGET_EF_LO = TARGET_EF_R0 + 32,
592 TARGET_EF_HI = TARGET_EF_R0 + 33,
593 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
594 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
595 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
596 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
597};
598
599/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
600static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
601{
602 int i;
603
604 for (i = 0; i < TARGET_EF_R0; i++) {
605 (*regs)[i] = 0;
606 }
607 (*regs)[TARGET_EF_R0] = 0;
608
609 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
610 (*regs)[TARGET_EF_R0 + i] = tswapl(env->active_tc.gpr[i]);
611 }
612
613 (*regs)[TARGET_EF_R26] = 0;
614 (*regs)[TARGET_EF_R27] = 0;
615 (*regs)[TARGET_EF_LO] = tswapl(env->active_tc.LO[0]);
616 (*regs)[TARGET_EF_HI] = tswapl(env->active_tc.HI[0]);
617 (*regs)[TARGET_EF_CP0_EPC] = tswapl(env->active_tc.PC);
618 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapl(env->CP0_BadVAddr);
619 (*regs)[TARGET_EF_CP0_STATUS] = tswapl(env->CP0_Status);
620 (*regs)[TARGET_EF_CP0_CAUSE] = tswapl(env->CP0_Cause);
621}
622
623#define USE_ELF_CORE_DUMP
ths388bb212007-05-13 13:58:00 +0000624#define ELF_EXEC_PAGESIZE 4096
625
bellard048f6b42005-11-26 18:47:20 +0000626#endif /* TARGET_MIPS */
627
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200628#ifdef TARGET_MICROBLAZE
629
630#define ELF_START_MMAP 0x80000000
631
632#define elf_check_arch(x) ( (x) == EM_XILINX_MICROBLAZE )
633
634#define ELF_CLASS ELFCLASS32
635#define ELF_DATA ELFDATA2MSB
Mike Frysinger0ddbc962010-01-17 01:15:05 -0500636#define ELF_ARCH EM_XILINX_MICROBLAZE
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200637
638static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
639{
640 regs->pc = infop->entry;
641 regs->r1 = infop->start_stack;
642
643}
644
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200645#define ELF_EXEC_PAGESIZE 4096
646
Edgar E. Iglesiase4cbd442010-05-19 15:09:28 +0200647#define USE_ELF_CORE_DUMP
648#define ELF_NREG 38
649typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
650
651/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
652static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
653{
654 int i, pos = 0;
655
656 for (i = 0; i < 32; i++) {
657 (*regs)[pos++] = tswapl(env->regs[i]);
658 }
659
660 for (i = 0; i < 6; i++) {
661 (*regs)[pos++] = tswapl(env->sregs[i]);
662 }
663}
664
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200665#endif /* TARGET_MICROBLAZE */
666
bellardfdf9b3e2006-04-27 21:07:38 +0000667#ifdef TARGET_SH4
668
669#define ELF_START_MMAP 0x80000000
670
671#define elf_check_arch(x) ( (x) == EM_SH )
672
673#define ELF_CLASS ELFCLASS32
674#define ELF_DATA ELFDATA2LSB
675#define ELF_ARCH EM_SH
676
bellardfdf9b3e2006-04-27 21:07:38 +0000677static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
678{
679 /* Check other registers XXXXX */
680 regs->pc = infop->entry;
ths072ae842007-06-22 10:13:51 +0000681 regs->regs[15] = infop->start_stack;
bellardfdf9b3e2006-04-27 21:07:38 +0000682}
683
Nathan Froyd7631c972009-12-11 09:04:51 -0800684/* See linux kernel: arch/sh/include/asm/elf.h. */
685#define ELF_NREG 23
686typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
687
688/* See linux kernel: arch/sh/include/asm/ptrace.h. */
689enum {
690 TARGET_REG_PC = 16,
691 TARGET_REG_PR = 17,
692 TARGET_REG_SR = 18,
693 TARGET_REG_GBR = 19,
694 TARGET_REG_MACH = 20,
695 TARGET_REG_MACL = 21,
696 TARGET_REG_SYSCALL = 22
697};
698
699static inline void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
700{
701 int i;
702
703 for (i = 0; i < 16; i++) {
704 (*regs[i]) = tswapl(env->gregs[i]);
705 }
706
707 (*regs)[TARGET_REG_PC] = tswapl(env->pc);
708 (*regs)[TARGET_REG_PR] = tswapl(env->pr);
709 (*regs)[TARGET_REG_SR] = tswapl(env->sr);
710 (*regs)[TARGET_REG_GBR] = tswapl(env->gbr);
711 (*regs)[TARGET_REG_MACH] = tswapl(env->mach);
712 (*regs)[TARGET_REG_MACL] = tswapl(env->macl);
713 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
714}
715
716#define USE_ELF_CORE_DUMP
bellardfdf9b3e2006-04-27 21:07:38 +0000717#define ELF_EXEC_PAGESIZE 4096
718
719#endif
720
ths48733d12007-10-08 13:36:46 +0000721#ifdef TARGET_CRIS
722
723#define ELF_START_MMAP 0x80000000
724
725#define elf_check_arch(x) ( (x) == EM_CRIS )
726
727#define ELF_CLASS ELFCLASS32
728#define ELF_DATA ELFDATA2LSB
729#define ELF_ARCH EM_CRIS
730
731static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
732{
733 regs->erp = infop->entry;
734}
735
ths48733d12007-10-08 13:36:46 +0000736#define ELF_EXEC_PAGESIZE 8192
737
738#endif
739
pbrooke6e59062006-10-22 00:18:54 +0000740#ifdef TARGET_M68K
741
742#define ELF_START_MMAP 0x80000000
743
744#define elf_check_arch(x) ( (x) == EM_68K )
745
746#define ELF_CLASS ELFCLASS32
747#define ELF_DATA ELFDATA2MSB
748#define ELF_ARCH EM_68K
749
750/* ??? Does this need to do anything?
751#define ELF_PLAT_INIT(_r) */
752
753static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
754{
755 regs->usp = infop->start_stack;
756 regs->sr = 0;
757 regs->pc = infop->entry;
758}
759
Nathan Froyd7a93cc52009-12-11 09:04:50 -0800760/* See linux kernel: arch/m68k/include/asm/elf.h. */
761#define ELF_NREG 20
762typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
763
764static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
765{
766 (*regs)[0] = tswapl(env->dregs[1]);
767 (*regs)[1] = tswapl(env->dregs[2]);
768 (*regs)[2] = tswapl(env->dregs[3]);
769 (*regs)[3] = tswapl(env->dregs[4]);
770 (*regs)[4] = tswapl(env->dregs[5]);
771 (*regs)[5] = tswapl(env->dregs[6]);
772 (*regs)[6] = tswapl(env->dregs[7]);
773 (*regs)[7] = tswapl(env->aregs[0]);
774 (*regs)[8] = tswapl(env->aregs[1]);
775 (*regs)[9] = tswapl(env->aregs[2]);
776 (*regs)[10] = tswapl(env->aregs[3]);
777 (*regs)[11] = tswapl(env->aregs[4]);
778 (*regs)[12] = tswapl(env->aregs[5]);
779 (*regs)[13] = tswapl(env->aregs[6]);
780 (*regs)[14] = tswapl(env->dregs[0]);
781 (*regs)[15] = tswapl(env->aregs[7]);
782 (*regs)[16] = tswapl(env->dregs[0]); /* FIXME: orig_d0 */
783 (*regs)[17] = tswapl(env->sr);
784 (*regs)[18] = tswapl(env->pc);
785 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
786}
787
788#define USE_ELF_CORE_DUMP
pbrooke6e59062006-10-22 00:18:54 +0000789#define ELF_EXEC_PAGESIZE 8192
790
791#endif
792
j_mayer7a3148a2007-04-05 07:13:51 +0000793#ifdef TARGET_ALPHA
794
795#define ELF_START_MMAP (0x30000000000ULL)
796
797#define elf_check_arch(x) ( (x) == ELF_ARCH )
798
799#define ELF_CLASS ELFCLASS64
800#define ELF_DATA ELFDATA2MSB
801#define ELF_ARCH EM_ALPHA
802
803static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
804{
805 regs->pc = infop->entry;
806 regs->ps = 8;
807 regs->usp = infop->start_stack;
j_mayer7a3148a2007-04-05 07:13:51 +0000808}
809
j_mayer7a3148a2007-04-05 07:13:51 +0000810#define ELF_EXEC_PAGESIZE 8192
811
812#endif /* TARGET_ALPHA */
813
bellard15338fd2005-11-26 11:41:16 +0000814#ifndef ELF_PLATFORM
815#define ELF_PLATFORM (NULL)
816#endif
817
818#ifndef ELF_HWCAP
819#define ELF_HWCAP 0
820#endif
821
blueswir1992f48a2007-10-14 16:27:31 +0000822#ifdef TARGET_ABI32
blueswir1cb33da52007-10-09 16:34:29 +0000823#undef ELF_CLASS
blueswir1992f48a2007-10-14 16:27:31 +0000824#define ELF_CLASS ELFCLASS32
blueswir1cb33da52007-10-09 16:34:29 +0000825#undef bswaptls
826#define bswaptls(ptr) bswap32s(ptr)
827#endif
828
bellard31e31b82003-02-18 22:55:36 +0000829#include "elf.h"
bellard09bfb052003-04-10 00:03:40 +0000830
bellard09bfb052003-04-10 00:03:40 +0000831struct exec
832{
833 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
834 unsigned int a_text; /* length of text, in bytes */
835 unsigned int a_data; /* length of data, in bytes */
836 unsigned int a_bss; /* length of uninitialized data area, in bytes */
837 unsigned int a_syms; /* length of symbol table data in file, in bytes */
838 unsigned int a_entry; /* start address */
839 unsigned int a_trsize; /* length of relocation info for text, in bytes */
840 unsigned int a_drsize; /* length of relocation info for data, in bytes */
841};
842
843
844#define N_MAGIC(exec) ((exec).a_info & 0xffff)
845#define OMAGIC 0407
846#define NMAGIC 0410
847#define ZMAGIC 0413
848#define QMAGIC 0314
849
bellard09bfb052003-04-10 00:03:40 +0000850/* max code+data+bss space allocated to elf interpreter */
851#define INTERP_MAP_SIZE (32 * 1024 * 1024)
852
853/* max code+data+bss+brk space allocated to ET_DYN executables */
854#define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
855
bellard31e31b82003-02-18 22:55:36 +0000856/* Necessary parameters */
bellard54936002003-05-13 00:25:15 +0000857#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
858#define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
859#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
bellard31e31b82003-02-18 22:55:36 +0000860
861#define INTERPRETER_NONE 0
862#define INTERPRETER_AOUT 1
863#define INTERPRETER_ELF 2
864
bellard15338fd2005-11-26 11:41:16 +0000865#define DLINFO_ITEMS 12
bellard31e31b82003-02-18 22:55:36 +0000866
bellard09bfb052003-04-10 00:03:40 +0000867static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
868{
869 memcpy(to, from, n);
870}
871
bellard31e31b82003-02-18 22:55:36 +0000872static int load_aout_interp(void * exptr, int interp_fd);
873
874#ifdef BSWAP_NEEDED
bellard92a31b12005-02-10 22:00:52 +0000875static void bswap_ehdr(struct elfhdr *ehdr)
bellard31e31b82003-02-18 22:55:36 +0000876{
877 bswap16s(&ehdr->e_type); /* Object file type */
878 bswap16s(&ehdr->e_machine); /* Architecture */
879 bswap32s(&ehdr->e_version); /* Object file version */
bellard92a31b12005-02-10 22:00:52 +0000880 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
881 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
882 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
bellard31e31b82003-02-18 22:55:36 +0000883 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
884 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
885 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
886 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
887 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
888 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
889 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
890}
891
bellard92a31b12005-02-10 22:00:52 +0000892static void bswap_phdr(struct elf_phdr *phdr)
bellard31e31b82003-02-18 22:55:36 +0000893{
894 bswap32s(&phdr->p_type); /* Segment type */
bellard92a31b12005-02-10 22:00:52 +0000895 bswaptls(&phdr->p_offset); /* Segment file offset */
896 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
897 bswaptls(&phdr->p_paddr); /* Segment physical address */
898 bswaptls(&phdr->p_filesz); /* Segment size in file */
899 bswaptls(&phdr->p_memsz); /* Segment size in memory */
bellard31e31b82003-02-18 22:55:36 +0000900 bswap32s(&phdr->p_flags); /* Segment flags */
bellard92a31b12005-02-10 22:00:52 +0000901 bswaptls(&phdr->p_align); /* Segment alignment */
bellard31e31b82003-02-18 22:55:36 +0000902}
bellard689f9362003-04-29 20:40:07 +0000903
bellard92a31b12005-02-10 22:00:52 +0000904static void bswap_shdr(struct elf_shdr *shdr)
bellard689f9362003-04-29 20:40:07 +0000905{
906 bswap32s(&shdr->sh_name);
907 bswap32s(&shdr->sh_type);
bellard92a31b12005-02-10 22:00:52 +0000908 bswaptls(&shdr->sh_flags);
909 bswaptls(&shdr->sh_addr);
910 bswaptls(&shdr->sh_offset);
911 bswaptls(&shdr->sh_size);
bellard689f9362003-04-29 20:40:07 +0000912 bswap32s(&shdr->sh_link);
913 bswap32s(&shdr->sh_info);
bellard92a31b12005-02-10 22:00:52 +0000914 bswaptls(&shdr->sh_addralign);
915 bswaptls(&shdr->sh_entsize);
bellard689f9362003-04-29 20:40:07 +0000916}
917
j_mayer7a3148a2007-04-05 07:13:51 +0000918static void bswap_sym(struct elf_sym *sym)
bellard689f9362003-04-29 20:40:07 +0000919{
920 bswap32s(&sym->st_name);
j_mayer7a3148a2007-04-05 07:13:51 +0000921 bswaptls(&sym->st_value);
922 bswaptls(&sym->st_size);
bellard689f9362003-04-29 20:40:07 +0000923 bswap16s(&sym->st_shndx);
924}
bellard31e31b82003-02-18 22:55:36 +0000925#endif
926
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300927#ifdef USE_ELF_CORE_DUMP
928static int elf_core_dump(int, const CPUState *);
929
930#ifdef BSWAP_NEEDED
931static void bswap_note(struct elf_note *en)
932{
malc9fdca5a2009-07-18 13:12:20 +0400933 bswap32s(&en->n_namesz);
934 bswap32s(&en->n_descsz);
935 bswap32s(&en->n_type);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300936}
937#endif /* BSWAP_NEEDED */
938
939#endif /* USE_ELF_CORE_DUMP */
940
bellard31e31b82003-02-18 22:55:36 +0000941/*
pbrooke5fe0c52006-06-11 13:32:59 +0000942 * 'copy_elf_strings()' copies argument/envelope strings from user
bellard31e31b82003-02-18 22:55:36 +0000943 * memory to free pages in kernel mem. These are in a format ready
944 * to be put directly into the top of new user memory.
945 *
946 */
blueswir1992f48a2007-10-14 16:27:31 +0000947static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
948 abi_ulong p)
bellard31e31b82003-02-18 22:55:36 +0000949{
950 char *tmp, *tmp1, *pag = NULL;
951 int len, offset = 0;
952
953 if (!p) {
954 return 0; /* bullet-proofing */
955 }
956 while (argc-- > 0) {
bellardedf779f2004-02-22 13:40:13 +0000957 tmp = argv[argc];
958 if (!tmp) {
bellard31e31b82003-02-18 22:55:36 +0000959 fprintf(stderr, "VFS: argc is wrong");
960 exit(-1);
961 }
bellardedf779f2004-02-22 13:40:13 +0000962 tmp1 = tmp;
963 while (*tmp++);
bellard31e31b82003-02-18 22:55:36 +0000964 len = tmp - tmp1;
965 if (p < len) { /* this shouldn't happen - 128kB */
966 return 0;
967 }
968 while (len) {
969 --p; --tmp; --len;
970 if (--offset < 0) {
bellard54936002003-05-13 00:25:15 +0000971 offset = p % TARGET_PAGE_SIZE;
pbrook53a59602006-03-25 19:31:22 +0000972 pag = (char *)page[p/TARGET_PAGE_SIZE];
bellard44a91ca2004-01-18 22:05:44 +0000973 if (!pag) {
pbrook53a59602006-03-25 19:31:22 +0000974 pag = (char *)malloc(TARGET_PAGE_SIZE);
j_mayer4118a972007-09-27 04:10:43 +0000975 memset(pag, 0, TARGET_PAGE_SIZE);
pbrook53a59602006-03-25 19:31:22 +0000976 page[p/TARGET_PAGE_SIZE] = pag;
bellard44a91ca2004-01-18 22:05:44 +0000977 if (!pag)
978 return 0;
bellard31e31b82003-02-18 22:55:36 +0000979 }
980 }
981 if (len == 0 || offset == 0) {
bellardedf779f2004-02-22 13:40:13 +0000982 *(pag + offset) = *tmp;
bellard31e31b82003-02-18 22:55:36 +0000983 }
984 else {
985 int bytes_to_copy = (len > offset) ? offset : len;
986 tmp -= bytes_to_copy;
987 p -= bytes_to_copy;
988 offset -= bytes_to_copy;
989 len -= bytes_to_copy;
990 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
991 }
992 }
993 }
994 return p;
995}
996
blueswir1992f48a2007-10-14 16:27:31 +0000997static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
998 struct image_info *info)
pbrook53a59602006-03-25 19:31:22 +0000999{
blueswir1992f48a2007-10-14 16:27:31 +00001000 abi_ulong stack_base, size, error;
bellard31e31b82003-02-18 22:55:36 +00001001 int i;
bellard31e31b82003-02-18 22:55:36 +00001002
1003 /* Create enough stack to hold everything. If we don't use
1004 * it for args, we'll use it for something else...
1005 */
Richard Henderson703e0e82010-03-19 14:21:13 -07001006 size = guest_stack_size;
bellard54936002003-05-13 00:25:15 +00001007 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
1008 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
ths5fafdf22007-09-16 21:08:06 +00001009 error = target_mmap(0,
bellard83fb7ad2004-07-05 21:25:26 +00001010 size + qemu_host_page_size,
bellard54936002003-05-13 00:25:15 +00001011 PROT_READ | PROT_WRITE,
1012 MAP_PRIVATE | MAP_ANONYMOUS,
1013 -1, 0);
bellard09bfb052003-04-10 00:03:40 +00001014 if (error == -1) {
1015 perror("stk mmap");
1016 exit(-1);
bellard31e31b82003-02-18 22:55:36 +00001017 }
bellard09bfb052003-04-10 00:03:40 +00001018 /* we reserve one extra page at the top of the stack as guard */
bellard83fb7ad2004-07-05 21:25:26 +00001019 target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
bellard09bfb052003-04-10 00:03:40 +00001020
bellard54936002003-05-13 00:25:15 +00001021 stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
bellard09bfb052003-04-10 00:03:40 +00001022 p += stack_base;
1023
bellard31e31b82003-02-18 22:55:36 +00001024 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1025 if (bprm->page[i]) {
1026 info->rss++;
bellard579a97f2007-11-11 14:26:47 +00001027 /* FIXME - check return value of memcpy_to_target() for failure */
pbrook53a59602006-03-25 19:31:22 +00001028 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
1029 free(bprm->page[i]);
bellard31e31b82003-02-18 22:55:36 +00001030 }
pbrook53a59602006-03-25 19:31:22 +00001031 stack_base += TARGET_PAGE_SIZE;
bellard31e31b82003-02-18 22:55:36 +00001032 }
1033 return p;
1034}
1035
blueswir1992f48a2007-10-14 16:27:31 +00001036static void set_brk(abi_ulong start, abi_ulong end)
bellard31e31b82003-02-18 22:55:36 +00001037{
1038 /* page-align the start and end addresses... */
bellard54936002003-05-13 00:25:15 +00001039 start = HOST_PAGE_ALIGN(start);
1040 end = HOST_PAGE_ALIGN(end);
bellard31e31b82003-02-18 22:55:36 +00001041 if (end <= start)
1042 return;
bellard54936002003-05-13 00:25:15 +00001043 if(target_mmap(start, end - start,
1044 PROT_READ | PROT_WRITE | PROT_EXEC,
1045 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
bellard31e31b82003-02-18 22:55:36 +00001046 perror("cannot mmap brk");
1047 exit(-1);
1048 }
1049}
1050
1051
bellard853d6f72003-09-30 20:58:32 +00001052/* We need to explicitly zero any fractional pages after the data
1053 section (i.e. bss). This would contain the junk from the file that
1054 should not be in memory. */
blueswir1992f48a2007-10-14 16:27:31 +00001055static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
bellard31e31b82003-02-18 22:55:36 +00001056{
blueswir1992f48a2007-10-14 16:27:31 +00001057 abi_ulong nbyte;
bellard31e31b82003-02-18 22:55:36 +00001058
ths768a4a32006-12-14 13:32:11 +00001059 if (elf_bss >= last_bss)
1060 return;
1061
bellard853d6f72003-09-30 20:58:32 +00001062 /* XXX: this is really a hack : if the real host page size is
1063 smaller than the target page size, some pages after the end
1064 of the file may not be mapped. A better fix would be to
1065 patch target_mmap(), but it is more complicated as the file
1066 size must be known */
bellard83fb7ad2004-07-05 21:25:26 +00001067 if (qemu_real_host_page_size < qemu_host_page_size) {
blueswir1992f48a2007-10-14 16:27:31 +00001068 abi_ulong end_addr, end_addr1;
ths5fafdf22007-09-16 21:08:06 +00001069 end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
bellard83fb7ad2004-07-05 21:25:26 +00001070 ~(qemu_real_host_page_size - 1);
bellard853d6f72003-09-30 20:58:32 +00001071 end_addr = HOST_PAGE_ALIGN(elf_bss);
1072 if (end_addr1 < end_addr) {
j_mayer863cf0b2007-10-07 15:59:45 +00001073 mmap((void *)g2h(end_addr1), end_addr - end_addr1,
bellard853d6f72003-09-30 20:58:32 +00001074 PROT_READ|PROT_WRITE|PROT_EXEC,
1075 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1076 }
1077 }
1078
bellard83fb7ad2004-07-05 21:25:26 +00001079 nbyte = elf_bss & (qemu_host_page_size-1);
bellard31e31b82003-02-18 22:55:36 +00001080 if (nbyte) {
bellard83fb7ad2004-07-05 21:25:26 +00001081 nbyte = qemu_host_page_size - nbyte;
bellard31e31b82003-02-18 22:55:36 +00001082 do {
bellard2f619692007-11-16 10:46:05 +00001083 /* FIXME - what to do if put_user() fails? */
1084 put_user_u8(0, elf_bss);
pbrook53a59602006-03-25 19:31:22 +00001085 elf_bss++;
bellard31e31b82003-02-18 22:55:36 +00001086 } while (--nbyte);
1087 }
1088}
1089
bellardedf779f2004-02-22 13:40:13 +00001090
blueswir1992f48a2007-10-14 16:27:31 +00001091static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1092 struct elfhdr * exec,
1093 abi_ulong load_addr,
1094 abi_ulong load_bias,
1095 abi_ulong interp_load_addr, int ibcs,
1096 struct image_info *info)
pbrook53a59602006-03-25 19:31:22 +00001097{
blueswir1992f48a2007-10-14 16:27:31 +00001098 abi_ulong sp;
pbrook53a59602006-03-25 19:31:22 +00001099 int size;
blueswir1992f48a2007-10-14 16:27:31 +00001100 abi_ulong u_platform;
pbrook53a59602006-03-25 19:31:22 +00001101 const char *k_platform;
j_mayer863cf0b2007-10-07 15:59:45 +00001102 const int n = sizeof(elf_addr_t);
pbrook53a59602006-03-25 19:31:22 +00001103
1104 sp = p;
1105 u_platform = 0;
bellard15338fd2005-11-26 11:41:16 +00001106 k_platform = ELF_PLATFORM;
1107 if (k_platform) {
1108 size_t len = strlen(k_platform) + 1;
pbrook53a59602006-03-25 19:31:22 +00001109 sp -= (len + n - 1) & ~(n - 1);
1110 u_platform = sp;
bellard579a97f2007-11-11 14:26:47 +00001111 /* FIXME - check return value of memcpy_to_target() for failure */
pbrook53a59602006-03-25 19:31:22 +00001112 memcpy_to_target(sp, k_platform, len);
bellard15338fd2005-11-26 11:41:16 +00001113 }
pbrook53a59602006-03-25 19:31:22 +00001114 /*
1115 * Force 16 byte _final_ alignment here for generality.
1116 */
blueswir1992f48a2007-10-14 16:27:31 +00001117 sp = sp &~ (abi_ulong)15;
pbrook53a59602006-03-25 19:31:22 +00001118 size = (DLINFO_ITEMS + 1) * 2;
bellard15338fd2005-11-26 11:41:16 +00001119 if (k_platform)
pbrook53a59602006-03-25 19:31:22 +00001120 size += 2;
bellardf5155282004-01-04 15:46:50 +00001121#ifdef DLINFO_ARCH_ITEMS
pbrook53a59602006-03-25 19:31:22 +00001122 size += DLINFO_ARCH_ITEMS * 2;
bellardf5155282004-01-04 15:46:50 +00001123#endif
pbrook53a59602006-03-25 19:31:22 +00001124 size += envc + argc + 2;
1125 size += (!ibcs ? 3 : 1); /* argc itself */
1126 size *= n;
1127 if (size & 15)
1128 sp -= 16 - (size & 15);
ths3b46e622007-09-17 08:09:54 +00001129
j_mayer863cf0b2007-10-07 15:59:45 +00001130 /* This is correct because Linux defines
1131 * elf_addr_t as Elf32_Off / Elf64_Off
1132 */
bellard2f619692007-11-16 10:46:05 +00001133#define NEW_AUX_ENT(id, val) do { \
1134 sp -= n; put_user_ual(val, sp); \
1135 sp -= n; put_user_ual(id, sp); \
pbrook53a59602006-03-25 19:31:22 +00001136 } while(0)
bellard2f619692007-11-16 10:46:05 +00001137
bellard0bccf032005-08-21 10:12:28 +00001138 NEW_AUX_ENT (AT_NULL, 0);
bellardf5155282004-01-04 15:46:50 +00001139
bellard0bccf032005-08-21 10:12:28 +00001140 /* There must be exactly DLINFO_ITEMS entries here. */
blueswir1992f48a2007-10-14 16:27:31 +00001141 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
1142 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1143 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1144 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
1145 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
1146 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
bellard0bccf032005-08-21 10:12:28 +00001147 NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
blueswir1992f48a2007-10-14 16:27:31 +00001148 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1149 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1150 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1151 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1152 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
pbrooka07c67d2008-03-26 23:31:55 +00001153 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
bellard15338fd2005-11-26 11:41:16 +00001154 if (k_platform)
pbrook53a59602006-03-25 19:31:22 +00001155 NEW_AUX_ENT(AT_PLATFORM, u_platform);
bellardf5155282004-01-04 15:46:50 +00001156#ifdef ARCH_DLINFO
ths5fafdf22007-09-16 21:08:06 +00001157 /*
bellardf5155282004-01-04 15:46:50 +00001158 * ARCH_DLINFO must come last so platform specific code can enforce
1159 * special alignment requirements on the AUXV if necessary (eg. PPC).
1160 */
1161 ARCH_DLINFO;
1162#endif
1163#undef NEW_AUX_ENT
1164
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001165 info->saved_auxv = sp;
1166
pbrooke5fe0c52006-06-11 13:32:59 +00001167 sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
bellard31e31b82003-02-18 22:55:36 +00001168 return sp;
1169}
1170
1171
blueswir1992f48a2007-10-14 16:27:31 +00001172static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
1173 int interpreter_fd,
1174 abi_ulong *interp_load_addr)
bellard31e31b82003-02-18 22:55:36 +00001175{
1176 struct elf_phdr *elf_phdata = NULL;
1177 struct elf_phdr *eppnt;
blueswir1992f48a2007-10-14 16:27:31 +00001178 abi_ulong load_addr = 0;
bellard31e31b82003-02-18 22:55:36 +00001179 int load_addr_set = 0;
1180 int retval;
blueswir1992f48a2007-10-14 16:27:31 +00001181 abi_ulong last_bss, elf_bss;
1182 abi_ulong error;
bellard31e31b82003-02-18 22:55:36 +00001183 int i;
ths5fafdf22007-09-16 21:08:06 +00001184
bellard31e31b82003-02-18 22:55:36 +00001185 elf_bss = 0;
1186 last_bss = 0;
1187 error = 0;
1188
bellard644c4332003-03-24 23:00:36 +00001189#ifdef BSWAP_NEEDED
1190 bswap_ehdr(interp_elf_ex);
1191#endif
bellard31e31b82003-02-18 22:55:36 +00001192 /* First of all, some simple consistency checks */
ths5fafdf22007-09-16 21:08:06 +00001193 if ((interp_elf_ex->e_type != ET_EXEC &&
1194 interp_elf_ex->e_type != ET_DYN) ||
bellard31e31b82003-02-18 22:55:36 +00001195 !elf_check_arch(interp_elf_ex->e_machine)) {
blueswir1992f48a2007-10-14 16:27:31 +00001196 return ~((abi_ulong)0UL);
bellard31e31b82003-02-18 22:55:36 +00001197 }
ths5fafdf22007-09-16 21:08:06 +00001198
bellard644c4332003-03-24 23:00:36 +00001199
bellard31e31b82003-02-18 22:55:36 +00001200 /* Now read in all of the header information */
ths5fafdf22007-09-16 21:08:06 +00001201
bellard54936002003-05-13 00:25:15 +00001202 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
blueswir1992f48a2007-10-14 16:27:31 +00001203 return ~(abi_ulong)0UL;
ths5fafdf22007-09-16 21:08:06 +00001204
1205 elf_phdata = (struct elf_phdr *)
bellard31e31b82003-02-18 22:55:36 +00001206 malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1207
1208 if (!elf_phdata)
blueswir1992f48a2007-10-14 16:27:31 +00001209 return ~((abi_ulong)0UL);
ths5fafdf22007-09-16 21:08:06 +00001210
bellard31e31b82003-02-18 22:55:36 +00001211 /*
1212 * If the size of this structure has changed, then punt, since
1213 * we will be doing the wrong thing.
1214 */
bellard09bfb052003-04-10 00:03:40 +00001215 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
bellard31e31b82003-02-18 22:55:36 +00001216 free(elf_phdata);
blueswir1992f48a2007-10-14 16:27:31 +00001217 return ~((abi_ulong)0UL);
bellard09bfb052003-04-10 00:03:40 +00001218 }
bellard31e31b82003-02-18 22:55:36 +00001219
1220 retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
1221 if(retval >= 0) {
1222 retval = read(interpreter_fd,
1223 (char *) elf_phdata,
1224 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1225 }
bellard31e31b82003-02-18 22:55:36 +00001226 if (retval < 0) {
1227 perror("load_elf_interp");
1228 exit(-1);
1229 free (elf_phdata);
1230 return retval;
1231 }
1232#ifdef BSWAP_NEEDED
1233 eppnt = elf_phdata;
1234 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
1235 bswap_phdr(eppnt);
1236 }
1237#endif
bellard09bfb052003-04-10 00:03:40 +00001238
1239 if (interp_elf_ex->e_type == ET_DYN) {
thse91c8a72007-06-03 13:35:16 +00001240 /* in order to avoid hardcoding the interpreter load
bellard09bfb052003-04-10 00:03:40 +00001241 address in qemu, we allocate a big enough memory zone */
bellard54936002003-05-13 00:25:15 +00001242 error = target_mmap(0, INTERP_MAP_SIZE,
ths5fafdf22007-09-16 21:08:06 +00001243 PROT_NONE, MAP_PRIVATE | MAP_ANON,
bellard54936002003-05-13 00:25:15 +00001244 -1, 0);
bellard09bfb052003-04-10 00:03:40 +00001245 if (error == -1) {
1246 perror("mmap");
1247 exit(-1);
1248 }
1249 load_addr = error;
1250 load_addr_set = 1;
1251 }
1252
bellard31e31b82003-02-18 22:55:36 +00001253 eppnt = elf_phdata;
1254 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
1255 if (eppnt->p_type == PT_LOAD) {
1256 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
1257 int elf_prot = 0;
blueswir1992f48a2007-10-14 16:27:31 +00001258 abi_ulong vaddr = 0;
1259 abi_ulong k;
bellard31e31b82003-02-18 22:55:36 +00001260
1261 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1262 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1263 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1264 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
1265 elf_type |= MAP_FIXED;
1266 vaddr = eppnt->p_vaddr;
1267 }
bellard54936002003-05-13 00:25:15 +00001268 error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
1269 eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
bellard31e31b82003-02-18 22:55:36 +00001270 elf_prot,
1271 elf_type,
1272 interpreter_fd,
bellard54936002003-05-13 00:25:15 +00001273 eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
ths3b46e622007-09-17 08:09:54 +00001274
pbrooke89f07d2006-02-04 20:46:24 +00001275 if (error == -1) {
bellard31e31b82003-02-18 22:55:36 +00001276 /* Real error */
1277 close(interpreter_fd);
1278 free(elf_phdata);
blueswir1992f48a2007-10-14 16:27:31 +00001279 return ~((abi_ulong)0UL);
bellard31e31b82003-02-18 22:55:36 +00001280 }
1281
1282 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
1283 load_addr = error;
1284 load_addr_set = 1;
1285 }
1286
1287 /*
1288 * Find the end of the file mapping for this phdr, and keep
1289 * track of the largest address we see for this.
1290 */
1291 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
1292 if (k > elf_bss) elf_bss = k;
1293
1294 /*
1295 * Do the same thing for the memory mapping - between
1296 * elf_bss and last_bss is the bss section.
1297 */
1298 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
1299 if (k > last_bss) last_bss = k;
1300 }
ths5fafdf22007-09-16 21:08:06 +00001301
bellard31e31b82003-02-18 22:55:36 +00001302 /* Now use mmap to map the library into memory. */
1303
1304 close(interpreter_fd);
1305
1306 /*
1307 * Now fill out the bss section. First pad the last page up
1308 * to the page boundary, and then perform a mmap to make sure
1309 * that there are zeromapped pages up to and including the last
1310 * bss page.
1311 */
ths768a4a32006-12-14 13:32:11 +00001312 padzero(elf_bss, last_bss);
bellard83fb7ad2004-07-05 21:25:26 +00001313 elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
bellard31e31b82003-02-18 22:55:36 +00001314
1315 /* Map the last of the bss segment */
1316 if (last_bss > elf_bss) {
bellard54936002003-05-13 00:25:15 +00001317 target_mmap(elf_bss, last_bss-elf_bss,
1318 PROT_READ|PROT_WRITE|PROT_EXEC,
1319 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
bellard31e31b82003-02-18 22:55:36 +00001320 }
1321 free(elf_phdata);
1322
1323 *interp_load_addr = load_addr;
blueswir1992f48a2007-10-14 16:27:31 +00001324 return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
bellard31e31b82003-02-18 22:55:36 +00001325}
1326
pbrook49918a72008-10-22 15:11:31 +00001327static int symfind(const void *s0, const void *s1)
1328{
1329 struct elf_sym *key = (struct elf_sym *)s0;
1330 struct elf_sym *sym = (struct elf_sym *)s1;
1331 int result = 0;
1332 if (key->st_value < sym->st_value) {
1333 result = -1;
Laurent Desnoguesec822002009-07-30 19:23:49 +02001334 } else if (key->st_value >= sym->st_value + sym->st_size) {
pbrook49918a72008-10-22 15:11:31 +00001335 result = 1;
1336 }
1337 return result;
1338}
1339
1340static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
1341{
1342#if ELF_CLASS == ELFCLASS32
1343 struct elf_sym *syms = s->disas_symtab.elf32;
1344#else
1345 struct elf_sym *syms = s->disas_symtab.elf64;
1346#endif
1347
1348 // binary search
1349 struct elf_sym key;
1350 struct elf_sym *sym;
1351
1352 key.st_value = orig_addr;
1353
1354 sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), symfind);
Blue Swirl7cba04f2009-08-01 10:13:20 +00001355 if (sym != NULL) {
pbrook49918a72008-10-22 15:11:31 +00001356 return s->disas_strtab + sym->st_name;
1357 }
1358
1359 return "";
1360}
1361
1362/* FIXME: This should use elf_ops.h */
1363static int symcmp(const void *s0, const void *s1)
1364{
1365 struct elf_sym *sym0 = (struct elf_sym *)s0;
1366 struct elf_sym *sym1 = (struct elf_sym *)s1;
1367 return (sym0->st_value < sym1->st_value)
1368 ? -1
1369 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
1370}
1371
bellard689f9362003-04-29 20:40:07 +00001372/* Best attempt to load symbols from this ELF object. */
1373static void load_symbols(struct elfhdr *hdr, int fd)
1374{
pbrook49918a72008-10-22 15:11:31 +00001375 unsigned int i, nsyms;
bellard689f9362003-04-29 20:40:07 +00001376 struct elf_shdr sechdr, symtab, strtab;
1377 char *strings;
bellarde80cfcf2004-12-19 23:18:01 +00001378 struct syminfo *s;
pbrook49918a72008-10-22 15:11:31 +00001379 struct elf_sym *syms;
bellard31e31b82003-02-18 22:55:36 +00001380
bellard689f9362003-04-29 20:40:07 +00001381 lseek(fd, hdr->e_shoff, SEEK_SET);
1382 for (i = 0; i < hdr->e_shnum; i++) {
pbrook49918a72008-10-22 15:11:31 +00001383 if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
1384 return;
bellard689f9362003-04-29 20:40:07 +00001385#ifdef BSWAP_NEEDED
pbrook49918a72008-10-22 15:11:31 +00001386 bswap_shdr(&sechdr);
bellard689f9362003-04-29 20:40:07 +00001387#endif
pbrook49918a72008-10-22 15:11:31 +00001388 if (sechdr.sh_type == SHT_SYMTAB) {
1389 symtab = sechdr;
1390 lseek(fd, hdr->e_shoff
1391 + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
1392 if (read(fd, &strtab, sizeof(strtab))
1393 != sizeof(strtab))
1394 return;
bellard689f9362003-04-29 20:40:07 +00001395#ifdef BSWAP_NEEDED
pbrook49918a72008-10-22 15:11:31 +00001396 bswap_shdr(&strtab);
bellard689f9362003-04-29 20:40:07 +00001397#endif
pbrook49918a72008-10-22 15:11:31 +00001398 goto found;
1399 }
bellard689f9362003-04-29 20:40:07 +00001400 }
1401 return; /* Shouldn't happen... */
1402
1403 found:
1404 /* Now know where the strtab and symtab are. Snarf them. */
bellarde80cfcf2004-12-19 23:18:01 +00001405 s = malloc(sizeof(*s));
pbrook49918a72008-10-22 15:11:31 +00001406 syms = malloc(symtab.sh_size);
1407 if (!syms)
1408 return;
bellarde80cfcf2004-12-19 23:18:01 +00001409 s->disas_strtab = strings = malloc(strtab.sh_size);
pbrook49918a72008-10-22 15:11:31 +00001410 if (!s->disas_strtab)
1411 return;
ths5fafdf22007-09-16 21:08:06 +00001412
bellard689f9362003-04-29 20:40:07 +00001413 lseek(fd, symtab.sh_offset, SEEK_SET);
pbrook49918a72008-10-22 15:11:31 +00001414 if (read(fd, syms, symtab.sh_size) != symtab.sh_size)
1415 return;
bellard689f9362003-04-29 20:40:07 +00001416
pbrook49918a72008-10-22 15:11:31 +00001417 nsyms = symtab.sh_size / sizeof(struct elf_sym);
1418
1419 i = 0;
1420 while (i < nsyms) {
bellard689f9362003-04-29 20:40:07 +00001421#ifdef BSWAP_NEEDED
pbrook49918a72008-10-22 15:11:31 +00001422 bswap_sym(syms + i);
bellard689f9362003-04-29 20:40:07 +00001423#endif
pbrook49918a72008-10-22 15:11:31 +00001424 // Throw away entries which we do not need.
1425 if (syms[i].st_shndx == SHN_UNDEF ||
1426 syms[i].st_shndx >= SHN_LORESERVE ||
1427 ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
1428 nsyms--;
1429 if (i < nsyms) {
1430 syms[i] = syms[nsyms];
1431 }
1432 continue;
1433 }
1434#if defined(TARGET_ARM) || defined (TARGET_MIPS)
1435 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
1436 syms[i].st_value &= ~(target_ulong)1;
blueswir10774bed2007-07-05 13:23:29 +00001437#endif
pbrook49918a72008-10-22 15:11:31 +00001438 i++;
blueswir10774bed2007-07-05 13:23:29 +00001439 }
pbrook49918a72008-10-22 15:11:31 +00001440 syms = realloc(syms, nsyms * sizeof(*syms));
bellard689f9362003-04-29 20:40:07 +00001441
pbrook49918a72008-10-22 15:11:31 +00001442 qsort(syms, nsyms, sizeof(*syms), symcmp);
1443
bellard689f9362003-04-29 20:40:07 +00001444 lseek(fd, strtab.sh_offset, SEEK_SET);
1445 if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
pbrook49918a72008-10-22 15:11:31 +00001446 return;
1447 s->disas_num_syms = nsyms;
1448#if ELF_CLASS == ELFCLASS32
1449 s->disas_symtab.elf32 = syms;
Paul Brook9f9f0302010-03-01 03:55:48 +00001450 s->lookup_symbol = lookup_symbolxx;
pbrook49918a72008-10-22 15:11:31 +00001451#else
1452 s->disas_symtab.elf64 = syms;
Paul Brook9f9f0302010-03-01 03:55:48 +00001453 s->lookup_symbol = lookup_symbolxx;
pbrook49918a72008-10-22 15:11:31 +00001454#endif
bellarde80cfcf2004-12-19 23:18:01 +00001455 s->next = syminfos;
1456 syminfos = s;
bellard689f9362003-04-29 20:40:07 +00001457}
bellard31e31b82003-02-18 22:55:36 +00001458
pbrooke5fe0c52006-06-11 13:32:59 +00001459int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
1460 struct image_info * info)
bellard31e31b82003-02-18 22:55:36 +00001461{
1462 struct elfhdr elf_ex;
1463 struct elfhdr interp_elf_ex;
1464 struct exec interp_ex;
1465 int interpreter_fd = -1; /* avoid warning */
blueswir1992f48a2007-10-14 16:27:31 +00001466 abi_ulong load_addr, load_bias;
bellard31e31b82003-02-18 22:55:36 +00001467 int load_addr_set = 0;
1468 unsigned int interpreter_type = INTERPRETER_NONE;
1469 unsigned char ibcs2_interpreter;
1470 int i;
blueswir1992f48a2007-10-14 16:27:31 +00001471 abi_ulong mapped_addr;
bellard31e31b82003-02-18 22:55:36 +00001472 struct elf_phdr * elf_ppnt;
1473 struct elf_phdr *elf_phdata;
blueswir1992f48a2007-10-14 16:27:31 +00001474 abi_ulong elf_bss, k, elf_brk;
bellard31e31b82003-02-18 22:55:36 +00001475 int retval;
1476 char * elf_interpreter;
blueswir1992f48a2007-10-14 16:27:31 +00001477 abi_ulong elf_entry, interp_load_addr = 0;
bellard31e31b82003-02-18 22:55:36 +00001478 int status;
blueswir1992f48a2007-10-14 16:27:31 +00001479 abi_ulong start_code, end_code, start_data, end_data;
1480 abi_ulong reloc_func_desc = 0;
1481 abi_ulong elf_stack;
bellard31e31b82003-02-18 22:55:36 +00001482 char passed_fileno[6];
1483
1484 ibcs2_interpreter = 0;
1485 status = 0;
1486 load_addr = 0;
bellard09bfb052003-04-10 00:03:40 +00001487 load_bias = 0;
bellard31e31b82003-02-18 22:55:36 +00001488 elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
1489#ifdef BSWAP_NEEDED
1490 bswap_ehdr(&elf_ex);
1491#endif
1492
bellard31e31b82003-02-18 22:55:36 +00001493 /* First of all, some simple consistency checks */
1494 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
1495 (! elf_check_arch(elf_ex.e_machine))) {
1496 return -ENOEXEC;
1497 }
1498
pbrooke5fe0c52006-06-11 13:32:59 +00001499 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
1500 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
1501 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
1502 if (!bprm->p) {
1503 retval = -E2BIG;
1504 }
1505
bellard31e31b82003-02-18 22:55:36 +00001506 /* Now read in all of the header information */
bellard31e31b82003-02-18 22:55:36 +00001507 elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
1508 if (elf_phdata == NULL) {
1509 return -ENOMEM;
1510 }
1511
1512 retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
1513 if(retval > 0) {
ths5fafdf22007-09-16 21:08:06 +00001514 retval = read(bprm->fd, (char *) elf_phdata,
bellard31e31b82003-02-18 22:55:36 +00001515 elf_ex.e_phentsize * elf_ex.e_phnum);
1516 }
1517
1518 if (retval < 0) {
1519 perror("load_elf_binary");
1520 exit(-1);
1521 free (elf_phdata);
1522 return -errno;
1523 }
1524
bellardb17780d2003-02-18 23:32:15 +00001525#ifdef BSWAP_NEEDED
1526 elf_ppnt = elf_phdata;
1527 for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
1528 bswap_phdr(elf_ppnt);
1529 }
1530#endif
bellard31e31b82003-02-18 22:55:36 +00001531 elf_ppnt = elf_phdata;
1532
1533 elf_bss = 0;
1534 elf_brk = 0;
1535
1536
blueswir1992f48a2007-10-14 16:27:31 +00001537 elf_stack = ~((abi_ulong)0UL);
bellard31e31b82003-02-18 22:55:36 +00001538 elf_interpreter = NULL;
blueswir1992f48a2007-10-14 16:27:31 +00001539 start_code = ~((abi_ulong)0UL);
bellard31e31b82003-02-18 22:55:36 +00001540 end_code = 0;
j_mayer863cf0b2007-10-07 15:59:45 +00001541 start_data = 0;
bellard31e31b82003-02-18 22:55:36 +00001542 end_data = 0;
blueswir198448f52008-09-30 18:16:09 +00001543 interp_ex.a_info = 0;
bellard31e31b82003-02-18 22:55:36 +00001544
1545 for(i=0;i < elf_ex.e_phnum; i++) {
1546 if (elf_ppnt->p_type == PT_INTERP) {
1547 if ( elf_interpreter != NULL )
1548 {
1549 free (elf_phdata);
1550 free(elf_interpreter);
1551 close(bprm->fd);
1552 return -EINVAL;
1553 }
1554
1555 /* This is the program interpreter used for
1556 * shared libraries - for now assume that this
1557 * is an a.out format binary
1558 */
1559
bellard32ce6332003-04-11 00:16:16 +00001560 elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
bellard31e31b82003-02-18 22:55:36 +00001561
1562 if (elf_interpreter == NULL) {
1563 free (elf_phdata);
1564 close(bprm->fd);
1565 return -ENOMEM;
1566 }
1567
bellard31e31b82003-02-18 22:55:36 +00001568 retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
1569 if(retval >= 0) {
bellard32ce6332003-04-11 00:16:16 +00001570 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
bellard31e31b82003-02-18 22:55:36 +00001571 }
1572 if(retval < 0) {
1573 perror("load_elf_binary2");
1574 exit(-1);
ths5fafdf22007-09-16 21:08:06 +00001575 }
bellard31e31b82003-02-18 22:55:36 +00001576
1577 /* If the program interpreter is one of these two,
1578 then assume an iBCS2 image. Otherwise assume
1579 a native linux image. */
1580
1581 /* JRP - Need to add X86 lib dir stuff here... */
1582
1583 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
1584 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
1585 ibcs2_interpreter = 1;
1586 }
1587
1588#if 0
Paul Bolle3bc0bdc2009-10-02 14:06:47 +02001589 printf("Using ELF interpreter %s\n", path(elf_interpreter));
bellard31e31b82003-02-18 22:55:36 +00001590#endif
1591 if (retval >= 0) {
bellard32ce6332003-04-11 00:16:16 +00001592 retval = open(path(elf_interpreter), O_RDONLY);
bellard31e31b82003-02-18 22:55:36 +00001593 if(retval >= 0) {
1594 interpreter_fd = retval;
1595 }
1596 else {
1597 perror(elf_interpreter);
1598 exit(-1);
1599 /* retval = -errno; */
1600 }
1601 }
1602
1603 if (retval >= 0) {
1604 retval = lseek(interpreter_fd, 0, SEEK_SET);
1605 if(retval >= 0) {
1606 retval = read(interpreter_fd,bprm->buf,128);
1607 }
1608 }
1609 if (retval >= 0) {
1610 interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
Michael S. Tsirkin6ece4df2009-09-30 19:43:38 +02001611 interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */
bellard31e31b82003-02-18 22:55:36 +00001612 }
1613 if (retval < 0) {
1614 perror("load_elf_binary3");
1615 exit(-1);
1616 free (elf_phdata);
1617 free(elf_interpreter);
1618 close(bprm->fd);
1619 return retval;
1620 }
1621 }
1622 elf_ppnt++;
1623 }
1624
1625 /* Some simple consistency checks for the interpreter */
1626 if (elf_interpreter){
1627 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
1628
1629 /* Now figure out which format our binary is */
1630 if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
1631 (N_MAGIC(interp_ex) != QMAGIC)) {
1632 interpreter_type = INTERPRETER_ELF;
1633 }
1634
1635 if (interp_elf_ex.e_ident[0] != 0x7f ||
blueswir1b55266b2008-09-20 08:07:15 +00001636 strncmp((char *)&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
bellard31e31b82003-02-18 22:55:36 +00001637 interpreter_type &= ~INTERPRETER_ELF;
1638 }
1639
1640 if (!interpreter_type) {
1641 free(elf_interpreter);
1642 free(elf_phdata);
1643 close(bprm->fd);
1644 return -ELIBBAD;
1645 }
1646 }
1647
1648 /* OK, we are done with that, now set up the arg stuff,
1649 and then start this sucker up */
1650
pbrooke5fe0c52006-06-11 13:32:59 +00001651 {
bellard31e31b82003-02-18 22:55:36 +00001652 char * passed_p;
1653
1654 if (interpreter_type == INTERPRETER_AOUT) {
bellardeba2af62004-06-19 17:23:39 +00001655 snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
bellard31e31b82003-02-18 22:55:36 +00001656 passed_p = passed_fileno;
1657
1658 if (elf_interpreter) {
pbrooke5fe0c52006-06-11 13:32:59 +00001659 bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
bellard31e31b82003-02-18 22:55:36 +00001660 bprm->argc++;
1661 }
1662 }
1663 if (!bprm->p) {
1664 if (elf_interpreter) {
1665 free(elf_interpreter);
1666 }
1667 free (elf_phdata);
1668 close(bprm->fd);
1669 return -E2BIG;
1670 }
1671 }
1672
1673 /* OK, This is the point of no return */
1674 info->end_data = 0;
1675 info->end_code = 0;
blueswir1992f48a2007-10-14 16:27:31 +00001676 info->start_mmap = (abi_ulong)ELF_START_MMAP;
bellard31e31b82003-02-18 22:55:36 +00001677 info->mmap = 0;
blueswir1992f48a2007-10-14 16:27:31 +00001678 elf_entry = (abi_ulong) elf_ex.e_entry;
bellard31e31b82003-02-18 22:55:36 +00001679
Paul Brook379f6692009-07-17 12:48:08 +01001680#if defined(CONFIG_USE_GUEST_BASE)
1681 /*
1682 * In case where user has not explicitly set the guest_base, we
1683 * probe here that should we set it automatically.
1684 */
1685 if (!have_guest_base) {
1686 /*
Paul Brookc581ded2010-05-05 16:32:59 +01001687 * Go through ELF program header table and find the address
1688 * range used by loadable segments. Check that this is available on
1689 * the host, and if not find a suitable value for guest_base. */
1690 abi_ulong app_start = ~0;
1691 abi_ulong app_end = 0;
1692 abi_ulong addr;
1693 unsigned long host_start;
1694 unsigned long real_start;
1695 unsigned long host_size;
Paul Brook379f6692009-07-17 12:48:08 +01001696 for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum;
1697 i++, elf_ppnt++) {
1698 if (elf_ppnt->p_type != PT_LOAD)
1699 continue;
Paul Brookc581ded2010-05-05 16:32:59 +01001700 addr = elf_ppnt->p_vaddr;
1701 if (addr < app_start) {
1702 app_start = addr;
1703 }
1704 addr += elf_ppnt->p_memsz;
1705 if (addr > app_end) {
1706 app_end = addr;
Paul Brook379f6692009-07-17 12:48:08 +01001707 }
1708 }
Paul Brookc581ded2010-05-05 16:32:59 +01001709
1710 /* If we don't have any loadable segments then something
1711 is very wrong. */
1712 assert(app_start < app_end);
1713
1714 /* Round addresses to page boundaries. */
1715 app_start = app_start & qemu_host_page_mask;
1716 app_end = HOST_PAGE_ALIGN(app_end);
1717 if (app_start < mmap_min_addr) {
1718 host_start = HOST_PAGE_ALIGN(mmap_min_addr);
1719 } else {
1720 host_start = app_start;
1721 if (host_start != app_start) {
1722 fprintf(stderr, "qemu: Address overflow loading ELF binary\n");
1723 abort();
1724 }
1725 }
1726 host_size = app_end - app_start;
1727 while (1) {
1728 /* Do not use mmap_find_vma here because that is limited to the
1729 guest address space. We are going to make the
1730 guest address space fit whatever we're given. */
1731 real_start = (unsigned long)mmap((void *)host_start, host_size,
1732 PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
1733 if (real_start == (unsigned long)-1) {
1734 fprintf(stderr, "qemu: Virtual memory exausted\n");
1735 abort();
1736 }
1737 if (real_start == host_start) {
1738 break;
1739 }
1740 /* That address didn't work. Unmap and try a different one.
1741 The address the host picked because is typically
1742 right at the top of the host address space and leaves the
1743 guest with no usable address space. Resort to a linear search.
1744 We already compensated for mmap_min_addr, so this should not
1745 happen often. Probably means we got unlucky and host address
1746 space randomization put a shared library somewhere
1747 inconvenient. */
1748 munmap((void *)real_start, host_size);
1749 host_start += qemu_host_page_size;
1750 if (host_start == app_start) {
1751 /* Theoretically possible if host doesn't have any
1752 suitably aligned areas. Normally the first mmap will
1753 fail. */
1754 fprintf(stderr, "qemu: Unable to find space for application\n");
1755 abort();
1756 }
1757 }
1758 qemu_log("Relocating guest address space from 0x" TARGET_ABI_FMT_lx
1759 " to 0x%lx\n", app_start, real_start);
1760 guest_base = real_start - app_start;
Paul Brook379f6692009-07-17 12:48:08 +01001761 }
1762#endif /* CONFIG_USE_GUEST_BASE */
1763
bellard31e31b82003-02-18 22:55:36 +00001764 /* Do this so that we can load the interpreter, if need be. We will
1765 change some of these later */
1766 info->rss = 0;
1767 bprm->p = setup_arg_pages(bprm->p, bprm, info);
1768 info->start_stack = bprm->p;
1769
1770 /* Now we do a little grungy work by mmaping the ELF image into
1771 * the correct location in memory. At this point, we assume that
1772 * the image should be loaded at fixed address, not at a variable
1773 * address.
1774 */
1775
bellard31e31b82003-02-18 22:55:36 +00001776 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
bellard09bfb052003-04-10 00:03:40 +00001777 int elf_prot = 0;
1778 int elf_flags = 0;
blueswir1992f48a2007-10-14 16:27:31 +00001779 abi_ulong error;
ths3b46e622007-09-17 08:09:54 +00001780
bellard09bfb052003-04-10 00:03:40 +00001781 if (elf_ppnt->p_type != PT_LOAD)
1782 continue;
ths3b46e622007-09-17 08:09:54 +00001783
bellard09bfb052003-04-10 00:03:40 +00001784 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1785 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1786 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1787 elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
1788 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
1789 elf_flags |= MAP_FIXED;
1790 } else if (elf_ex.e_type == ET_DYN) {
1791 /* Try and get dynamic programs out of the way of the default mmap
1792 base, as well as whatever program they might try to exec. This
1793 is because the brk will follow the loader, and is not movable. */
1794 /* NOTE: for qemu, we do a big mmap to get enough space
thse91c8a72007-06-03 13:35:16 +00001795 without hardcoding any address */
bellard54936002003-05-13 00:25:15 +00001796 error = target_mmap(0, ET_DYN_MAP_SIZE,
ths5fafdf22007-09-16 21:08:06 +00001797 PROT_NONE, MAP_PRIVATE | MAP_ANON,
bellard54936002003-05-13 00:25:15 +00001798 -1, 0);
bellard09bfb052003-04-10 00:03:40 +00001799 if (error == -1) {
1800 perror("mmap");
1801 exit(-1);
1802 }
bellard54936002003-05-13 00:25:15 +00001803 load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
bellard09bfb052003-04-10 00:03:40 +00001804 }
ths3b46e622007-09-17 08:09:54 +00001805
bellard54936002003-05-13 00:25:15 +00001806 error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1807 (elf_ppnt->p_filesz +
1808 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1809 elf_prot,
1810 (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1811 bprm->fd,
ths5fafdf22007-09-16 21:08:06 +00001812 (elf_ppnt->p_offset -
bellard54936002003-05-13 00:25:15 +00001813 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
bellard09bfb052003-04-10 00:03:40 +00001814 if (error == -1) {
1815 perror("mmap");
1816 exit(-1);
1817 }
bellard31e31b82003-02-18 22:55:36 +00001818
1819#ifdef LOW_ELF_STACK
bellard54936002003-05-13 00:25:15 +00001820 if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1821 elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
bellard31e31b82003-02-18 22:55:36 +00001822#endif
ths3b46e622007-09-17 08:09:54 +00001823
bellard09bfb052003-04-10 00:03:40 +00001824 if (!load_addr_set) {
1825 load_addr_set = 1;
1826 load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1827 if (elf_ex.e_type == ET_DYN) {
1828 load_bias += error -
bellard54936002003-05-13 00:25:15 +00001829 TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
bellard09bfb052003-04-10 00:03:40 +00001830 load_addr += load_bias;
j_mayer84409dd2007-04-06 08:56:50 +00001831 reloc_func_desc = load_bias;
bellard09bfb052003-04-10 00:03:40 +00001832 }
1833 }
1834 k = elf_ppnt->p_vaddr;
ths5fafdf22007-09-16 21:08:06 +00001835 if (k < start_code)
bellard09bfb052003-04-10 00:03:40 +00001836 start_code = k;
j_mayer863cf0b2007-10-07 15:59:45 +00001837 if (start_data < k)
1838 start_data = k;
bellard09bfb052003-04-10 00:03:40 +00001839 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
ths5fafdf22007-09-16 21:08:06 +00001840 if (k > elf_bss)
bellard09bfb052003-04-10 00:03:40 +00001841 elf_bss = k;
1842 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
1843 end_code = k;
ths5fafdf22007-09-16 21:08:06 +00001844 if (end_data < k)
bellard09bfb052003-04-10 00:03:40 +00001845 end_data = k;
1846 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1847 if (k > elf_brk) elf_brk = k;
bellard31e31b82003-02-18 22:55:36 +00001848 }
1849
bellard09bfb052003-04-10 00:03:40 +00001850 elf_entry += load_bias;
1851 elf_bss += load_bias;
1852 elf_brk += load_bias;
1853 start_code += load_bias;
1854 end_code += load_bias;
j_mayer863cf0b2007-10-07 15:59:45 +00001855 start_data += load_bias;
bellard09bfb052003-04-10 00:03:40 +00001856 end_data += load_bias;
1857
bellard31e31b82003-02-18 22:55:36 +00001858 if (elf_interpreter) {
1859 if (interpreter_type & 1) {
1860 elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1861 }
1862 else if (interpreter_type & 2) {
1863 elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1864 &interp_load_addr);
1865 }
j_mayer84409dd2007-04-06 08:56:50 +00001866 reloc_func_desc = interp_load_addr;
bellard31e31b82003-02-18 22:55:36 +00001867
1868 close(interpreter_fd);
1869 free(elf_interpreter);
1870
blueswir1992f48a2007-10-14 16:27:31 +00001871 if (elf_entry == ~((abi_ulong)0UL)) {
bellard31e31b82003-02-18 22:55:36 +00001872 printf("Unable to load interpreter\n");
1873 free(elf_phdata);
1874 exit(-1);
1875 return 0;
1876 }
1877 }
1878
1879 free(elf_phdata);
1880
aliguori93fcfe32009-01-15 22:34:14 +00001881 if (qemu_log_enabled())
bellard689f9362003-04-29 20:40:07 +00001882 load_symbols(&elf_ex, bprm->fd);
1883
bellard31e31b82003-02-18 22:55:36 +00001884 if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
1885 info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
1886
1887#ifdef LOW_ELF_STACK
1888 info->start_stack = bprm->p = elf_stack - 4;
1889#endif
pbrook53a59602006-03-25 19:31:22 +00001890 bprm->p = create_elf_tables(bprm->p,
bellard31e31b82003-02-18 22:55:36 +00001891 bprm->argc,
1892 bprm->envc,
bellarda1516e92003-07-09 17:13:37 +00001893 &elf_ex,
bellard09bfb052003-04-10 00:03:40 +00001894 load_addr, load_bias,
bellard31e31b82003-02-18 22:55:36 +00001895 interp_load_addr,
1896 (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1897 info);
j_mayer92a343d2007-09-27 01:14:15 +00001898 info->load_addr = reloc_func_desc;
bellard31e31b82003-02-18 22:55:36 +00001899 info->start_brk = info->brk = elf_brk;
1900 info->end_code = end_code;
1901 info->start_code = start_code;
j_mayer863cf0b2007-10-07 15:59:45 +00001902 info->start_data = start_data;
bellard31e31b82003-02-18 22:55:36 +00001903 info->end_data = end_data;
1904 info->start_stack = bprm->p;
1905
1906 /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1907 sections */
1908 set_brk(elf_bss, elf_brk);
1909
ths768a4a32006-12-14 13:32:11 +00001910 padzero(elf_bss, elf_brk);
bellard31e31b82003-02-18 22:55:36 +00001911
1912#if 0
1913 printf("(start_brk) %x\n" , info->start_brk);
1914 printf("(end_code) %x\n" , info->end_code);
1915 printf("(start_code) %x\n" , info->start_code);
1916 printf("(end_data) %x\n" , info->end_data);
1917 printf("(start_stack) %x\n" , info->start_stack);
1918 printf("(brk) %x\n" , info->brk);
1919#endif
1920
1921 if ( info->personality == PER_SVR4 )
1922 {
1923 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1924 and some applications "depend" upon this behavior.
1925 Since we do not have the power to recompile these, we
1926 emulate the SVr4 behavior. Sigh. */
bellard83fb7ad2004-07-05 21:25:26 +00001927 mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
bellard54936002003-05-13 00:25:15 +00001928 MAP_FIXED | MAP_PRIVATE, -1, 0);
bellard31e31b82003-02-18 22:55:36 +00001929 }
1930
bellard31e31b82003-02-18 22:55:36 +00001931 info->entry = elf_entry;
1932
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001933#ifdef USE_ELF_CORE_DUMP
1934 bprm->core_dump = &elf_core_dump;
1935#endif
1936
bellard31e31b82003-02-18 22:55:36 +00001937 return 0;
1938}
1939
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001940#ifdef USE_ELF_CORE_DUMP
1941
1942/*
1943 * Definitions to generate Intel SVR4-like core files.
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001944 * These mostly have the same names as the SVR4 types with "target_elf_"
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001945 * tacked on the front to prevent clashes with linux definitions,
1946 * and the typedef forms have been avoided. This is mostly like
1947 * the SVR4 structure, but more Linuxy, with things that Linux does
1948 * not support and which gdb doesn't really use excluded.
1949 *
1950 * Fields we don't dump (their contents is zero) in linux-user qemu
1951 * are marked with XXX.
1952 *
1953 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
1954 *
1955 * Porting ELF coredump for target is (quite) simple process. First you
Nathan Froyddd0a3652009-12-11 09:04:45 -08001956 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001957 * the target resides):
1958 *
1959 * #define USE_ELF_CORE_DUMP
1960 *
1961 * Next you define type of register set used for dumping. ELF specification
1962 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
1963 *
Anthony Liguoric227f092009-10-01 16:12:16 -05001964 * typedef <target_regtype> target_elf_greg_t;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001965 * #define ELF_NREG <number of registers>
Anthony Liguoric227f092009-10-01 16:12:16 -05001966 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001967 *
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001968 * Last step is to implement target specific function that copies registers
1969 * from given cpu into just specified register set. Prototype is:
1970 *
Anthony Liguoric227f092009-10-01 16:12:16 -05001971 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001972 * const CPUState *env);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001973 *
1974 * Parameters:
1975 * regs - copy register values into here (allocated and zeroed by caller)
1976 * env - copy registers from here
1977 *
1978 * Example for ARM target is provided in this file.
1979 */
1980
1981/* An ELF note in memory */
1982struct memelfnote {
1983 const char *name;
1984 size_t namesz;
1985 size_t namesz_rounded;
1986 int type;
1987 size_t datasz;
1988 void *data;
1989 size_t notesz;
1990};
1991
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001992struct target_elf_siginfo {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001993 int si_signo; /* signal number */
1994 int si_code; /* extra code */
1995 int si_errno; /* errno */
1996};
1997
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001998struct target_elf_prstatus {
1999 struct target_elf_siginfo pr_info; /* Info associated with signal */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002000 short pr_cursig; /* Current signal */
2001 target_ulong pr_sigpend; /* XXX */
2002 target_ulong pr_sighold; /* XXX */
Anthony Liguoric227f092009-10-01 16:12:16 -05002003 target_pid_t pr_pid;
2004 target_pid_t pr_ppid;
2005 target_pid_t pr_pgrp;
2006 target_pid_t pr_sid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002007 struct target_timeval pr_utime; /* XXX User time */
2008 struct target_timeval pr_stime; /* XXX System time */
2009 struct target_timeval pr_cutime; /* XXX Cumulative user time */
2010 struct target_timeval pr_cstime; /* XXX Cumulative system time */
Anthony Liguoric227f092009-10-01 16:12:16 -05002011 target_elf_gregset_t pr_reg; /* GP registers */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002012 int pr_fpvalid; /* XXX */
2013};
2014
2015#define ELF_PRARGSZ (80) /* Number of chars for args */
2016
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002017struct target_elf_prpsinfo {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002018 char pr_state; /* numeric process state */
2019 char pr_sname; /* char for pr_state */
2020 char pr_zomb; /* zombie */
2021 char pr_nice; /* nice val */
2022 target_ulong pr_flag; /* flags */
Anthony Liguoric227f092009-10-01 16:12:16 -05002023 target_uid_t pr_uid;
2024 target_gid_t pr_gid;
2025 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002026 /* Lots missing */
2027 char pr_fname[16]; /* filename of executable */
2028 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
2029};
2030
2031/* Here is the structure in which status of each thread is captured. */
2032struct elf_thread_status {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002033 QTAILQ_ENTRY(elf_thread_status) ets_link;
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002034 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002035#if 0
2036 elf_fpregset_t fpu; /* NT_PRFPREG */
2037 struct task_struct *thread;
2038 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
2039#endif
2040 struct memelfnote notes[1];
2041 int num_notes;
2042};
2043
2044struct elf_note_info {
2045 struct memelfnote *notes;
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002046 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
2047 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002048
Blue Swirl72cf2d42009-09-12 07:36:22 +00002049 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002050#if 0
2051 /*
2052 * Current version of ELF coredump doesn't support
2053 * dumping fp regs etc.
2054 */
2055 elf_fpregset_t *fpu;
2056 elf_fpxregset_t *xfpu;
2057 int thread_status_size;
2058#endif
2059 int notes_size;
2060 int numnote;
2061};
2062
2063struct vm_area_struct {
2064 abi_ulong vma_start; /* start vaddr of memory region */
2065 abi_ulong vma_end; /* end vaddr of memory region */
2066 abi_ulong vma_flags; /* protection etc. flags for the region */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002067 QTAILQ_ENTRY(vm_area_struct) vma_link;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002068};
2069
2070struct mm_struct {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002071 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002072 int mm_count; /* number of mappings */
2073};
2074
2075static struct mm_struct *vma_init(void);
2076static void vma_delete(struct mm_struct *);
2077static int vma_add_mapping(struct mm_struct *, abi_ulong,
2078 abi_ulong, abi_ulong);
2079static int vma_get_mapping_count(const struct mm_struct *);
2080static struct vm_area_struct *vma_first(const struct mm_struct *);
2081static struct vm_area_struct *vma_next(struct vm_area_struct *);
2082static abi_ulong vma_dump_size(const struct vm_area_struct *);
Paul Brookb480d9b2010-03-12 23:23:29 +00002083static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002084 unsigned long flags);
2085
2086static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2087static void fill_note(struct memelfnote *, const char *, int,
2088 unsigned int, void *);
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002089static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2090static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002091static void fill_auxv_note(struct memelfnote *, const TaskState *);
2092static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2093static size_t note_size(const struct memelfnote *);
2094static void free_note_info(struct elf_note_info *);
2095static int fill_note_info(struct elf_note_info *, long, const CPUState *);
2096static void fill_thread_info(struct elf_note_info *, const CPUState *);
2097static int core_dump_filename(const TaskState *, char *, size_t);
2098
2099static int dump_write(int, const void *, size_t);
2100static int write_note(struct memelfnote *, int);
2101static int write_note_info(struct elf_note_info *, int);
2102
2103#ifdef BSWAP_NEEDED
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002104static void bswap_prstatus(struct target_elf_prstatus *);
2105static void bswap_psinfo(struct target_elf_prpsinfo *);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002106
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002107static void bswap_prstatus(struct target_elf_prstatus *prstatus)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002108{
2109 prstatus->pr_info.si_signo = tswapl(prstatus->pr_info.si_signo);
2110 prstatus->pr_info.si_code = tswapl(prstatus->pr_info.si_code);
2111 prstatus->pr_info.si_errno = tswapl(prstatus->pr_info.si_errno);
2112 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2113 prstatus->pr_sigpend = tswapl(prstatus->pr_sigpend);
2114 prstatus->pr_sighold = tswapl(prstatus->pr_sighold);
2115 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2116 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2117 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2118 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2119 /* cpu times are not filled, so we skip them */
2120 /* regs should be in correct format already */
2121 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2122}
2123
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002124static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002125{
2126 psinfo->pr_flag = tswapl(psinfo->pr_flag);
2127 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2128 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2129 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2130 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2131 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2132 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2133}
2134#endif /* BSWAP_NEEDED */
2135
2136/*
2137 * Minimal support for linux memory regions. These are needed
2138 * when we are finding out what memory exactly belongs to
2139 * emulated process. No locks needed here, as long as
2140 * thread that received the signal is stopped.
2141 */
2142
2143static struct mm_struct *vma_init(void)
2144{
2145 struct mm_struct *mm;
2146
2147 if ((mm = qemu_malloc(sizeof (*mm))) == NULL)
2148 return (NULL);
2149
2150 mm->mm_count = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002151 QTAILQ_INIT(&mm->mm_mmap);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002152
2153 return (mm);
2154}
2155
2156static void vma_delete(struct mm_struct *mm)
2157{
2158 struct vm_area_struct *vma;
2159
2160 while ((vma = vma_first(mm)) != NULL) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002161 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002162 qemu_free(vma);
2163 }
2164 qemu_free(mm);
2165}
2166
2167static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
2168 abi_ulong end, abi_ulong flags)
2169{
2170 struct vm_area_struct *vma;
2171
2172 if ((vma = qemu_mallocz(sizeof (*vma))) == NULL)
2173 return (-1);
2174
2175 vma->vma_start = start;
2176 vma->vma_end = end;
2177 vma->vma_flags = flags;
2178
Blue Swirl72cf2d42009-09-12 07:36:22 +00002179 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002180 mm->mm_count++;
2181
2182 return (0);
2183}
2184
2185static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2186{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002187 return (QTAILQ_FIRST(&mm->mm_mmap));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002188}
2189
2190static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2191{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002192 return (QTAILQ_NEXT(vma, vma_link));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002193}
2194
2195static int vma_get_mapping_count(const struct mm_struct *mm)
2196{
2197 return (mm->mm_count);
2198}
2199
2200/*
2201 * Calculate file (dump) size of given memory region.
2202 */
2203static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2204{
2205 /* if we cannot even read the first page, skip it */
2206 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2207 return (0);
2208
2209 /*
2210 * Usually we don't dump executable pages as they contain
2211 * non-writable code that debugger can read directly from
2212 * target library etc. However, thread stacks are marked
2213 * also executable so we read in first page of given region
2214 * and check whether it contains elf header. If there is
2215 * no elf header, we dump it.
2216 */
2217 if (vma->vma_flags & PROT_EXEC) {
2218 char page[TARGET_PAGE_SIZE];
2219
2220 copy_from_user(page, vma->vma_start, sizeof (page));
2221 if ((page[EI_MAG0] == ELFMAG0) &&
2222 (page[EI_MAG1] == ELFMAG1) &&
2223 (page[EI_MAG2] == ELFMAG2) &&
2224 (page[EI_MAG3] == ELFMAG3)) {
2225 /*
2226 * Mappings are possibly from ELF binary. Don't dump
2227 * them.
2228 */
2229 return (0);
2230 }
2231 }
2232
2233 return (vma->vma_end - vma->vma_start);
2234}
2235
Paul Brookb480d9b2010-03-12 23:23:29 +00002236static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002237 unsigned long flags)
2238{
2239 struct mm_struct *mm = (struct mm_struct *)priv;
2240
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002241 vma_add_mapping(mm, start, end, flags);
2242 return (0);
2243}
2244
2245static void fill_note(struct memelfnote *note, const char *name, int type,
2246 unsigned int sz, void *data)
2247{
2248 unsigned int namesz;
2249
2250 namesz = strlen(name) + 1;
2251 note->name = name;
2252 note->namesz = namesz;
2253 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2254 note->type = type;
2255 note->datasz = roundup(sz, sizeof (int32_t));;
2256 note->data = data;
2257
2258 /*
2259 * We calculate rounded up note size here as specified by
2260 * ELF document.
2261 */
2262 note->notesz = sizeof (struct elf_note) +
2263 note->namesz_rounded + note->datasz;
2264}
2265
2266static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2267 uint32_t flags)
2268{
2269 (void) memset(elf, 0, sizeof(*elf));
2270
2271 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2272 elf->e_ident[EI_CLASS] = ELF_CLASS;
2273 elf->e_ident[EI_DATA] = ELF_DATA;
2274 elf->e_ident[EI_VERSION] = EV_CURRENT;
2275 elf->e_ident[EI_OSABI] = ELF_OSABI;
2276
2277 elf->e_type = ET_CORE;
2278 elf->e_machine = machine;
2279 elf->e_version = EV_CURRENT;
2280 elf->e_phoff = sizeof(struct elfhdr);
2281 elf->e_flags = flags;
2282 elf->e_ehsize = sizeof(struct elfhdr);
2283 elf->e_phentsize = sizeof(struct elf_phdr);
2284 elf->e_phnum = segs;
2285
2286#ifdef BSWAP_NEEDED
2287 bswap_ehdr(elf);
2288#endif
2289}
2290
2291static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2292{
2293 phdr->p_type = PT_NOTE;
2294 phdr->p_offset = offset;
2295 phdr->p_vaddr = 0;
2296 phdr->p_paddr = 0;
2297 phdr->p_filesz = sz;
2298 phdr->p_memsz = 0;
2299 phdr->p_flags = 0;
2300 phdr->p_align = 0;
2301
2302#ifdef BSWAP_NEEDED
2303 bswap_phdr(phdr);
2304#endif
2305}
2306
2307static size_t note_size(const struct memelfnote *note)
2308{
2309 return (note->notesz);
2310}
2311
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002312static void fill_prstatus(struct target_elf_prstatus *prstatus,
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002313 const TaskState *ts, int signr)
2314{
2315 (void) memset(prstatus, 0, sizeof (*prstatus));
2316 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2317 prstatus->pr_pid = ts->ts_tid;
2318 prstatus->pr_ppid = getppid();
2319 prstatus->pr_pgrp = getpgrp();
2320 prstatus->pr_sid = getsid(0);
2321
2322#ifdef BSWAP_NEEDED
2323 bswap_prstatus(prstatus);
2324#endif
2325}
2326
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002327static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002328{
2329 char *filename, *base_filename;
2330 unsigned int i, len;
2331
2332 (void) memset(psinfo, 0, sizeof (*psinfo));
2333
2334 len = ts->info->arg_end - ts->info->arg_start;
2335 if (len >= ELF_PRARGSZ)
2336 len = ELF_PRARGSZ - 1;
2337 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2338 return -EFAULT;
2339 for (i = 0; i < len; i++)
2340 if (psinfo->pr_psargs[i] == 0)
2341 psinfo->pr_psargs[i] = ' ';
2342 psinfo->pr_psargs[len] = 0;
2343
2344 psinfo->pr_pid = getpid();
2345 psinfo->pr_ppid = getppid();
2346 psinfo->pr_pgrp = getpgrp();
2347 psinfo->pr_sid = getsid(0);
2348 psinfo->pr_uid = getuid();
2349 psinfo->pr_gid = getgid();
2350
2351 filename = strdup(ts->bprm->filename);
2352 base_filename = strdup(basename(filename));
2353 (void) strncpy(psinfo->pr_fname, base_filename,
2354 sizeof(psinfo->pr_fname));
2355 free(base_filename);
2356 free(filename);
2357
2358#ifdef BSWAP_NEEDED
2359 bswap_psinfo(psinfo);
2360#endif
2361 return (0);
2362}
2363
2364static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2365{
2366 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2367 elf_addr_t orig_auxv = auxv;
2368 abi_ulong val;
2369 void *ptr;
2370 int i, len;
2371
2372 /*
2373 * Auxiliary vector is stored in target process stack. It contains
2374 * {type, value} pairs that we need to dump into note. This is not
2375 * strictly necessary but we do it here for sake of completeness.
2376 */
2377
2378 /* find out lenght of the vector, AT_NULL is terminator */
2379 i = len = 0;
2380 do {
2381 get_user_ual(val, auxv);
2382 i += 2;
2383 auxv += 2 * sizeof (elf_addr_t);
2384 } while (val != AT_NULL);
2385 len = i * sizeof (elf_addr_t);
2386
2387 /* read in whole auxv vector and copy it to memelfnote */
2388 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2389 if (ptr != NULL) {
2390 fill_note(note, "CORE", NT_AUXV, len, ptr);
2391 unlock_user(ptr, auxv, len);
2392 }
2393}
2394
2395/*
2396 * Constructs name of coredump file. We have following convention
2397 * for the name:
2398 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2399 *
2400 * Returns 0 in case of success, -1 otherwise (errno is set).
2401 */
2402static int core_dump_filename(const TaskState *ts, char *buf,
2403 size_t bufsize)
2404{
2405 char timestamp[64];
2406 char *filename = NULL;
2407 char *base_filename = NULL;
2408 struct timeval tv;
2409 struct tm tm;
2410
2411 assert(bufsize >= PATH_MAX);
2412
2413 if (gettimeofday(&tv, NULL) < 0) {
2414 (void) fprintf(stderr, "unable to get current timestamp: %s",
2415 strerror(errno));
2416 return (-1);
2417 }
2418
2419 filename = strdup(ts->bprm->filename);
2420 base_filename = strdup(basename(filename));
2421 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2422 localtime_r(&tv.tv_sec, &tm));
2423 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2424 base_filename, timestamp, (int)getpid());
2425 free(base_filename);
2426 free(filename);
2427
2428 return (0);
2429}
2430
2431static int dump_write(int fd, const void *ptr, size_t size)
2432{
2433 const char *bufp = (const char *)ptr;
2434 ssize_t bytes_written, bytes_left;
2435 struct rlimit dumpsize;
2436 off_t pos;
2437
2438 bytes_written = 0;
2439 getrlimit(RLIMIT_CORE, &dumpsize);
2440 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2441 if (errno == ESPIPE) { /* not a seekable stream */
2442 bytes_left = size;
2443 } else {
2444 return pos;
2445 }
2446 } else {
2447 if (dumpsize.rlim_cur <= pos) {
2448 return -1;
2449 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2450 bytes_left = size;
2451 } else {
2452 size_t limit_left=dumpsize.rlim_cur - pos;
2453 bytes_left = limit_left >= size ? size : limit_left ;
2454 }
2455 }
2456
2457 /*
2458 * In normal conditions, single write(2) should do but
2459 * in case of socket etc. this mechanism is more portable.
2460 */
2461 do {
2462 bytes_written = write(fd, bufp, bytes_left);
2463 if (bytes_written < 0) {
2464 if (errno == EINTR)
2465 continue;
2466 return (-1);
2467 } else if (bytes_written == 0) { /* eof */
2468 return (-1);
2469 }
2470 bufp += bytes_written;
2471 bytes_left -= bytes_written;
2472 } while (bytes_left > 0);
2473
2474 return (0);
2475}
2476
2477static int write_note(struct memelfnote *men, int fd)
2478{
2479 struct elf_note en;
2480
2481 en.n_namesz = men->namesz;
2482 en.n_type = men->type;
2483 en.n_descsz = men->datasz;
2484
2485#ifdef BSWAP_NEEDED
2486 bswap_note(&en);
2487#endif
2488
2489 if (dump_write(fd, &en, sizeof(en)) != 0)
2490 return (-1);
2491 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2492 return (-1);
2493 if (dump_write(fd, men->data, men->datasz) != 0)
2494 return (-1);
2495
2496 return (0);
2497}
2498
2499static void fill_thread_info(struct elf_note_info *info, const CPUState *env)
2500{
2501 TaskState *ts = (TaskState *)env->opaque;
2502 struct elf_thread_status *ets;
2503
2504 ets = qemu_mallocz(sizeof (*ets));
2505 ets->num_notes = 1; /* only prstatus is dumped */
2506 fill_prstatus(&ets->prstatus, ts, 0);
2507 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2508 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2509 &ets->prstatus);
2510
Blue Swirl72cf2d42009-09-12 07:36:22 +00002511 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002512
2513 info->notes_size += note_size(&ets->notes[0]);
2514}
2515
2516static int fill_note_info(struct elf_note_info *info,
2517 long signr, const CPUState *env)
2518{
2519#define NUMNOTES 3
2520 CPUState *cpu = NULL;
2521 TaskState *ts = (TaskState *)env->opaque;
2522 int i;
2523
2524 (void) memset(info, 0, sizeof (*info));
2525
Blue Swirl72cf2d42009-09-12 07:36:22 +00002526 QTAILQ_INIT(&info->thread_list);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002527
2528 info->notes = qemu_mallocz(NUMNOTES * sizeof (struct memelfnote));
2529 if (info->notes == NULL)
2530 return (-ENOMEM);
2531 info->prstatus = qemu_mallocz(sizeof (*info->prstatus));
2532 if (info->prstatus == NULL)
2533 return (-ENOMEM);
2534 info->psinfo = qemu_mallocz(sizeof (*info->psinfo));
2535 if (info->prstatus == NULL)
2536 return (-ENOMEM);
2537
2538 /*
2539 * First fill in status (and registers) of current thread
2540 * including process info & aux vector.
2541 */
2542 fill_prstatus(info->prstatus, ts, signr);
2543 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2544 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2545 sizeof (*info->prstatus), info->prstatus);
2546 fill_psinfo(info->psinfo, ts);
2547 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2548 sizeof (*info->psinfo), info->psinfo);
2549 fill_auxv_note(&info->notes[2], ts);
2550 info->numnote = 3;
2551
2552 info->notes_size = 0;
2553 for (i = 0; i < info->numnote; i++)
2554 info->notes_size += note_size(&info->notes[i]);
2555
2556 /* read and fill status of all threads */
2557 cpu_list_lock();
2558 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2559 if (cpu == thread_env)
2560 continue;
2561 fill_thread_info(info, cpu);
2562 }
2563 cpu_list_unlock();
2564
2565 return (0);
2566}
2567
2568static void free_note_info(struct elf_note_info *info)
2569{
2570 struct elf_thread_status *ets;
2571
Blue Swirl72cf2d42009-09-12 07:36:22 +00002572 while (!QTAILQ_EMPTY(&info->thread_list)) {
2573 ets = QTAILQ_FIRST(&info->thread_list);
2574 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002575 qemu_free(ets);
2576 }
2577
2578 qemu_free(info->prstatus);
2579 qemu_free(info->psinfo);
2580 qemu_free(info->notes);
2581}
2582
2583static int write_note_info(struct elf_note_info *info, int fd)
2584{
2585 struct elf_thread_status *ets;
2586 int i, error = 0;
2587
2588 /* write prstatus, psinfo and auxv for current thread */
2589 for (i = 0; i < info->numnote; i++)
2590 if ((error = write_note(&info->notes[i], fd)) != 0)
2591 return (error);
2592
2593 /* write prstatus for each thread */
2594 for (ets = info->thread_list.tqh_first; ets != NULL;
2595 ets = ets->ets_link.tqe_next) {
2596 if ((error = write_note(&ets->notes[0], fd)) != 0)
2597 return (error);
2598 }
2599
2600 return (0);
2601}
2602
2603/*
2604 * Write out ELF coredump.
2605 *
2606 * See documentation of ELF object file format in:
2607 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2608 *
2609 * Coredump format in linux is following:
2610 *
2611 * 0 +----------------------+ \
2612 * | ELF header | ET_CORE |
2613 * +----------------------+ |
2614 * | ELF program headers | |--- headers
2615 * | - NOTE section | |
2616 * | - PT_LOAD sections | |
2617 * +----------------------+ /
2618 * | NOTEs: |
2619 * | - NT_PRSTATUS |
2620 * | - NT_PRSINFO |
2621 * | - NT_AUXV |
2622 * +----------------------+ <-- aligned to target page
2623 * | Process memory dump |
2624 * : :
2625 * . .
2626 * : :
2627 * | |
2628 * +----------------------+
2629 *
2630 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2631 * NT_PRSINFO -> struct elf_prpsinfo
2632 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2633 *
2634 * Format follows System V format as close as possible. Current
2635 * version limitations are as follows:
2636 * - no floating point registers are dumped
2637 *
2638 * Function returns 0 in case of success, negative errno otherwise.
2639 *
2640 * TODO: make this work also during runtime: it should be
2641 * possible to force coredump from running process and then
2642 * continue processing. For example qemu could set up SIGUSR2
2643 * handler (provided that target process haven't registered
2644 * handler for that) that does the dump when signal is received.
2645 */
2646static int elf_core_dump(int signr, const CPUState *env)
2647{
2648 const TaskState *ts = (const TaskState *)env->opaque;
2649 struct vm_area_struct *vma = NULL;
2650 char corefile[PATH_MAX];
2651 struct elf_note_info info;
2652 struct elfhdr elf;
2653 struct elf_phdr phdr;
2654 struct rlimit dumpsize;
2655 struct mm_struct *mm = NULL;
2656 off_t offset = 0, data_offset = 0;
2657 int segs = 0;
2658 int fd = -1;
2659
2660 errno = 0;
2661 getrlimit(RLIMIT_CORE, &dumpsize);
2662 if (dumpsize.rlim_cur == 0)
2663 return 0;
2664
2665 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2666 return (-errno);
2667
2668 if ((fd = open(corefile, O_WRONLY | O_CREAT,
2669 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2670 return (-errno);
2671
2672 /*
2673 * Walk through target process memory mappings and
2674 * set up structure containing this information. After
2675 * this point vma_xxx functions can be used.
2676 */
2677 if ((mm = vma_init()) == NULL)
2678 goto out;
2679
2680 walk_memory_regions(mm, vma_walker);
2681 segs = vma_get_mapping_count(mm);
2682
2683 /*
2684 * Construct valid coredump ELF header. We also
2685 * add one more segment for notes.
2686 */
2687 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
2688 if (dump_write(fd, &elf, sizeof (elf)) != 0)
2689 goto out;
2690
2691 /* fill in in-memory version of notes */
2692 if (fill_note_info(&info, signr, env) < 0)
2693 goto out;
2694
2695 offset += sizeof (elf); /* elf header */
2696 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
2697
2698 /* write out notes program header */
2699 fill_elf_note_phdr(&phdr, info.notes_size, offset);
2700
2701 offset += info.notes_size;
2702 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
2703 goto out;
2704
2705 /*
2706 * ELF specification wants data to start at page boundary so
2707 * we align it here.
2708 */
2709 offset = roundup(offset, ELF_EXEC_PAGESIZE);
2710
2711 /*
2712 * Write program headers for memory regions mapped in
2713 * the target process.
2714 */
2715 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2716 (void) memset(&phdr, 0, sizeof (phdr));
2717
2718 phdr.p_type = PT_LOAD;
2719 phdr.p_offset = offset;
2720 phdr.p_vaddr = vma->vma_start;
2721 phdr.p_paddr = 0;
2722 phdr.p_filesz = vma_dump_size(vma);
2723 offset += phdr.p_filesz;
2724 phdr.p_memsz = vma->vma_end - vma->vma_start;
2725 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
2726 if (vma->vma_flags & PROT_WRITE)
2727 phdr.p_flags |= PF_W;
2728 if (vma->vma_flags & PROT_EXEC)
2729 phdr.p_flags |= PF_X;
2730 phdr.p_align = ELF_EXEC_PAGESIZE;
2731
2732 dump_write(fd, &phdr, sizeof (phdr));
2733 }
2734
2735 /*
2736 * Next we write notes just after program headers. No
2737 * alignment needed here.
2738 */
2739 if (write_note_info(&info, fd) < 0)
2740 goto out;
2741
2742 /* align data to page boundary */
2743 data_offset = lseek(fd, 0, SEEK_CUR);
2744 data_offset = TARGET_PAGE_ALIGN(data_offset);
2745 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
2746 goto out;
2747
2748 /*
2749 * Finally we can dump process memory into corefile as well.
2750 */
2751 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2752 abi_ulong addr;
2753 abi_ulong end;
2754
2755 end = vma->vma_start + vma_dump_size(vma);
2756
2757 for (addr = vma->vma_start; addr < end;
2758 addr += TARGET_PAGE_SIZE) {
2759 char page[TARGET_PAGE_SIZE];
2760 int error;
2761
2762 /*
2763 * Read in page from target process memory and
2764 * write it to coredump file.
2765 */
2766 error = copy_from_user(page, addr, sizeof (page));
2767 if (error != 0) {
Aurelien Jarno49995e12009-12-19 20:28:23 +01002768 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002769 addr);
2770 errno = -error;
2771 goto out;
2772 }
2773 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
2774 goto out;
2775 }
2776 }
2777
2778out:
2779 free_note_info(&info);
2780 if (mm != NULL)
2781 vma_delete(mm);
2782 (void) close(fd);
2783
2784 if (errno != 0)
2785 return (-errno);
2786 return (0);
2787}
2788
2789#endif /* USE_ELF_CORE_DUMP */
2790
bellard31e31b82003-02-18 22:55:36 +00002791static int load_aout_interp(void * exptr, int interp_fd)
2792{
2793 printf("a.out interpreter not yet supported\n");
2794 return(0);
2795}
2796
pbrooke5fe0c52006-06-11 13:32:59 +00002797void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
2798{
2799 init_thread(regs, infop);
2800}