blob: 71ed2d60d1cba2e30d4c2cff0d2cacf33fc1da97 [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{
blueswir1992f48a2007-10-14 16:27:31 +0000515 abi_ulong pos = infop->start_stack;
516 abi_ulong tmp;
j_mayere85e7c62007-10-18 19:59:49 +0000517#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
blueswir1992f48a2007-10-14 16:27:31 +0000518 abi_ulong entry, toc;
j_mayer84409dd2007-04-06 08:56:50 +0000519#endif
pbrooke5fe0c52006-06-11 13:32:59 +0000520
bellard67867302003-11-23 17:05:30 +0000521 _regs->gpr[1] = infop->start_stack;
j_mayere85e7c62007-10-18 19:59:49 +0000522#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
j_mayer84409dd2007-04-06 08:56:50 +0000523 entry = ldq_raw(infop->entry) + infop->load_addr;
524 toc = ldq_raw(infop->entry + 8) + infop->load_addr;
525 _regs->gpr[2] = toc;
526 infop->entry = entry;
527#endif
bellard67867302003-11-23 17:05:30 +0000528 _regs->nip = infop->entry;
pbrooke5fe0c52006-06-11 13:32:59 +0000529 /* Note that isn't exactly what regular kernel does
530 * but this is what the ABI wants and is needed to allow
531 * execution of PPC BSD programs.
532 */
bellard2f619692007-11-16 10:46:05 +0000533 /* FIXME - what to for failure of get_user()? */
534 get_user_ual(_regs->gpr[3], pos);
blueswir1992f48a2007-10-14 16:27:31 +0000535 pos += sizeof(abi_ulong);
pbrooke5fe0c52006-06-11 13:32:59 +0000536 _regs->gpr[4] = pos;
blueswir1992f48a2007-10-14 16:27:31 +0000537 for (tmp = 1; tmp != 0; pos += sizeof(abi_ulong))
pbrooke5fe0c52006-06-11 13:32:59 +0000538 tmp = ldl(pos);
539 _regs->gpr[5] = pos;
bellard67867302003-11-23 17:05:30 +0000540}
541
Nathan Froyde2f3e742009-12-11 09:04:48 -0800542/* See linux kernel: arch/powerpc/include/asm/elf.h. */
543#define ELF_NREG 48
544typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
545
546static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
547{
548 int i;
549 target_ulong ccr = 0;
550
551 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
552 (*regs)[i] = tswapl(env->gpr[i]);
553 }
554
555 (*regs)[32] = tswapl(env->nip);
556 (*regs)[33] = tswapl(env->msr);
557 (*regs)[35] = tswapl(env->ctr);
558 (*regs)[36] = tswapl(env->lr);
559 (*regs)[37] = tswapl(env->xer);
560
561 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
562 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
563 }
564 (*regs)[38] = tswapl(ccr);
565}
566
567#define USE_ELF_CORE_DUMP
bellard67867302003-11-23 17:05:30 +0000568#define ELF_EXEC_PAGESIZE 4096
569
570#endif
571
bellard048f6b42005-11-26 18:47:20 +0000572#ifdef TARGET_MIPS
573
574#define ELF_START_MMAP 0x80000000
575
576#define elf_check_arch(x) ( (x) == EM_MIPS )
577
ths388bb212007-05-13 13:58:00 +0000578#ifdef TARGET_MIPS64
579#define ELF_CLASS ELFCLASS64
580#else
bellard048f6b42005-11-26 18:47:20 +0000581#define ELF_CLASS ELFCLASS32
ths388bb212007-05-13 13:58:00 +0000582#endif
bellard048f6b42005-11-26 18:47:20 +0000583#ifdef TARGET_WORDS_BIGENDIAN
584#define ELF_DATA ELFDATA2MSB
585#else
586#define ELF_DATA ELFDATA2LSB
587#endif
588#define ELF_ARCH EM_MIPS
589
bellard048f6b42005-11-26 18:47:20 +0000590static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
591{
ths623a9302007-10-28 19:45:05 +0000592 regs->cp0_status = 2 << CP0St_KSU;
bellard048f6b42005-11-26 18:47:20 +0000593 regs->cp0_epc = infop->entry;
594 regs->regs[29] = infop->start_stack;
595}
596
Nathan Froyd51e52602009-12-11 09:04:49 -0800597/* See linux kernel: arch/mips/include/asm/elf.h. */
598#define ELF_NREG 45
599typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
600
601/* See linux kernel: arch/mips/include/asm/reg.h. */
602enum {
603#ifdef TARGET_MIPS64
604 TARGET_EF_R0 = 0,
605#else
606 TARGET_EF_R0 = 6,
607#endif
608 TARGET_EF_R26 = TARGET_EF_R0 + 26,
609 TARGET_EF_R27 = TARGET_EF_R0 + 27,
610 TARGET_EF_LO = TARGET_EF_R0 + 32,
611 TARGET_EF_HI = TARGET_EF_R0 + 33,
612 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
613 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
614 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
615 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
616};
617
618/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
619static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
620{
621 int i;
622
623 for (i = 0; i < TARGET_EF_R0; i++) {
624 (*regs)[i] = 0;
625 }
626 (*regs)[TARGET_EF_R0] = 0;
627
628 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
629 (*regs)[TARGET_EF_R0 + i] = tswapl(env->active_tc.gpr[i]);
630 }
631
632 (*regs)[TARGET_EF_R26] = 0;
633 (*regs)[TARGET_EF_R27] = 0;
634 (*regs)[TARGET_EF_LO] = tswapl(env->active_tc.LO[0]);
635 (*regs)[TARGET_EF_HI] = tswapl(env->active_tc.HI[0]);
636 (*regs)[TARGET_EF_CP0_EPC] = tswapl(env->active_tc.PC);
637 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapl(env->CP0_BadVAddr);
638 (*regs)[TARGET_EF_CP0_STATUS] = tswapl(env->CP0_Status);
639 (*regs)[TARGET_EF_CP0_CAUSE] = tswapl(env->CP0_Cause);
640}
641
642#define USE_ELF_CORE_DUMP
ths388bb212007-05-13 13:58:00 +0000643#define ELF_EXEC_PAGESIZE 4096
644
bellard048f6b42005-11-26 18:47:20 +0000645#endif /* TARGET_MIPS */
646
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200647#ifdef TARGET_MICROBLAZE
648
649#define ELF_START_MMAP 0x80000000
650
651#define elf_check_arch(x) ( (x) == EM_XILINX_MICROBLAZE )
652
653#define ELF_CLASS ELFCLASS32
654#define ELF_DATA ELFDATA2MSB
Mike Frysinger0ddbc962010-01-17 01:15:05 -0500655#define ELF_ARCH EM_XILINX_MICROBLAZE
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200656
657static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
658{
659 regs->pc = infop->entry;
660 regs->r1 = infop->start_stack;
661
662}
663
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +0200664#define ELF_EXEC_PAGESIZE 4096
665
666#endif /* TARGET_MICROBLAZE */
667
bellardfdf9b3e2006-04-27 21:07:38 +0000668#ifdef TARGET_SH4
669
670#define ELF_START_MMAP 0x80000000
671
672#define elf_check_arch(x) ( (x) == EM_SH )
673
674#define ELF_CLASS ELFCLASS32
675#define ELF_DATA ELFDATA2LSB
676#define ELF_ARCH EM_SH
677
bellardfdf9b3e2006-04-27 21:07:38 +0000678static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
679{
680 /* Check other registers XXXXX */
681 regs->pc = infop->entry;
ths072ae842007-06-22 10:13:51 +0000682 regs->regs[15] = infop->start_stack;
bellardfdf9b3e2006-04-27 21:07:38 +0000683}
684
Nathan Froyd7631c972009-12-11 09:04:51 -0800685/* See linux kernel: arch/sh/include/asm/elf.h. */
686#define ELF_NREG 23
687typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
688
689/* See linux kernel: arch/sh/include/asm/ptrace.h. */
690enum {
691 TARGET_REG_PC = 16,
692 TARGET_REG_PR = 17,
693 TARGET_REG_SR = 18,
694 TARGET_REG_GBR = 19,
695 TARGET_REG_MACH = 20,
696 TARGET_REG_MACL = 21,
697 TARGET_REG_SYSCALL = 22
698};
699
700static inline void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
701{
702 int i;
703
704 for (i = 0; i < 16; i++) {
705 (*regs[i]) = tswapl(env->gregs[i]);
706 }
707
708 (*regs)[TARGET_REG_PC] = tswapl(env->pc);
709 (*regs)[TARGET_REG_PR] = tswapl(env->pr);
710 (*regs)[TARGET_REG_SR] = tswapl(env->sr);
711 (*regs)[TARGET_REG_GBR] = tswapl(env->gbr);
712 (*regs)[TARGET_REG_MACH] = tswapl(env->mach);
713 (*regs)[TARGET_REG_MACL] = tswapl(env->macl);
714 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
715}
716
717#define USE_ELF_CORE_DUMP
bellardfdf9b3e2006-04-27 21:07:38 +0000718#define ELF_EXEC_PAGESIZE 4096
719
720#endif
721
ths48733d12007-10-08 13:36:46 +0000722#ifdef TARGET_CRIS
723
724#define ELF_START_MMAP 0x80000000
725
726#define elf_check_arch(x) ( (x) == EM_CRIS )
727
728#define ELF_CLASS ELFCLASS32
729#define ELF_DATA ELFDATA2LSB
730#define ELF_ARCH EM_CRIS
731
732static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
733{
734 regs->erp = infop->entry;
735}
736
ths48733d12007-10-08 13:36:46 +0000737#define ELF_EXEC_PAGESIZE 8192
738
739#endif
740
pbrooke6e59062006-10-22 00:18:54 +0000741#ifdef TARGET_M68K
742
743#define ELF_START_MMAP 0x80000000
744
745#define elf_check_arch(x) ( (x) == EM_68K )
746
747#define ELF_CLASS ELFCLASS32
748#define ELF_DATA ELFDATA2MSB
749#define ELF_ARCH EM_68K
750
751/* ??? Does this need to do anything?
752#define ELF_PLAT_INIT(_r) */
753
754static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
755{
756 regs->usp = infop->start_stack;
757 regs->sr = 0;
758 regs->pc = infop->entry;
759}
760
Nathan Froyd7a93cc52009-12-11 09:04:50 -0800761/* See linux kernel: arch/m68k/include/asm/elf.h. */
762#define ELF_NREG 20
763typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
764
765static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
766{
767 (*regs)[0] = tswapl(env->dregs[1]);
768 (*regs)[1] = tswapl(env->dregs[2]);
769 (*regs)[2] = tswapl(env->dregs[3]);
770 (*regs)[3] = tswapl(env->dregs[4]);
771 (*regs)[4] = tswapl(env->dregs[5]);
772 (*regs)[5] = tswapl(env->dregs[6]);
773 (*regs)[6] = tswapl(env->dregs[7]);
774 (*regs)[7] = tswapl(env->aregs[0]);
775 (*regs)[8] = tswapl(env->aregs[1]);
776 (*regs)[9] = tswapl(env->aregs[2]);
777 (*regs)[10] = tswapl(env->aregs[3]);
778 (*regs)[11] = tswapl(env->aregs[4]);
779 (*regs)[12] = tswapl(env->aregs[5]);
780 (*regs)[13] = tswapl(env->aregs[6]);
781 (*regs)[14] = tswapl(env->dregs[0]);
782 (*regs)[15] = tswapl(env->aregs[7]);
783 (*regs)[16] = tswapl(env->dregs[0]); /* FIXME: orig_d0 */
784 (*regs)[17] = tswapl(env->sr);
785 (*regs)[18] = tswapl(env->pc);
786 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
787}
788
789#define USE_ELF_CORE_DUMP
pbrooke6e59062006-10-22 00:18:54 +0000790#define ELF_EXEC_PAGESIZE 8192
791
792#endif
793
j_mayer7a3148a2007-04-05 07:13:51 +0000794#ifdef TARGET_ALPHA
795
796#define ELF_START_MMAP (0x30000000000ULL)
797
798#define elf_check_arch(x) ( (x) == ELF_ARCH )
799
800#define ELF_CLASS ELFCLASS64
801#define ELF_DATA ELFDATA2MSB
802#define ELF_ARCH EM_ALPHA
803
804static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
805{
806 regs->pc = infop->entry;
807 regs->ps = 8;
808 regs->usp = infop->start_stack;
j_mayer7a3148a2007-04-05 07:13:51 +0000809}
810
j_mayer7a3148a2007-04-05 07:13:51 +0000811#define ELF_EXEC_PAGESIZE 8192
812
813#endif /* TARGET_ALPHA */
814
bellard15338fd2005-11-26 11:41:16 +0000815#ifndef ELF_PLATFORM
816#define ELF_PLATFORM (NULL)
817#endif
818
819#ifndef ELF_HWCAP
820#define ELF_HWCAP 0
821#endif
822
blueswir1992f48a2007-10-14 16:27:31 +0000823#ifdef TARGET_ABI32
blueswir1cb33da52007-10-09 16:34:29 +0000824#undef ELF_CLASS
blueswir1992f48a2007-10-14 16:27:31 +0000825#define ELF_CLASS ELFCLASS32
blueswir1cb33da52007-10-09 16:34:29 +0000826#undef bswaptls
827#define bswaptls(ptr) bswap32s(ptr)
828#endif
829
bellard31e31b82003-02-18 22:55:36 +0000830#include "elf.h"
bellard09bfb052003-04-10 00:03:40 +0000831
bellard09bfb052003-04-10 00:03:40 +0000832struct exec
833{
834 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
835 unsigned int a_text; /* length of text, in bytes */
836 unsigned int a_data; /* length of data, in bytes */
837 unsigned int a_bss; /* length of uninitialized data area, in bytes */
838 unsigned int a_syms; /* length of symbol table data in file, in bytes */
839 unsigned int a_entry; /* start address */
840 unsigned int a_trsize; /* length of relocation info for text, in bytes */
841 unsigned int a_drsize; /* length of relocation info for data, in bytes */
842};
843
844
845#define N_MAGIC(exec) ((exec).a_info & 0xffff)
846#define OMAGIC 0407
847#define NMAGIC 0410
848#define ZMAGIC 0413
849#define QMAGIC 0314
850
bellard09bfb052003-04-10 00:03:40 +0000851/* max code+data+bss space allocated to elf interpreter */
852#define INTERP_MAP_SIZE (32 * 1024 * 1024)
853
854/* max code+data+bss+brk space allocated to ET_DYN executables */
855#define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
856
bellard31e31b82003-02-18 22:55:36 +0000857/* Necessary parameters */
bellard54936002003-05-13 00:25:15 +0000858#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
859#define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
860#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
bellard31e31b82003-02-18 22:55:36 +0000861
862#define INTERPRETER_NONE 0
863#define INTERPRETER_AOUT 1
864#define INTERPRETER_ELF 2
865
bellard15338fd2005-11-26 11:41:16 +0000866#define DLINFO_ITEMS 12
bellard31e31b82003-02-18 22:55:36 +0000867
bellard09bfb052003-04-10 00:03:40 +0000868static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
869{
870 memcpy(to, from, n);
871}
872
bellard31e31b82003-02-18 22:55:36 +0000873static int load_aout_interp(void * exptr, int interp_fd);
874
875#ifdef BSWAP_NEEDED
bellard92a31b12005-02-10 22:00:52 +0000876static void bswap_ehdr(struct elfhdr *ehdr)
bellard31e31b82003-02-18 22:55:36 +0000877{
878 bswap16s(&ehdr->e_type); /* Object file type */
879 bswap16s(&ehdr->e_machine); /* Architecture */
880 bswap32s(&ehdr->e_version); /* Object file version */
bellard92a31b12005-02-10 22:00:52 +0000881 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
882 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
883 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
bellard31e31b82003-02-18 22:55:36 +0000884 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
885 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
886 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
887 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
888 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
889 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
890 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
891}
892
bellard92a31b12005-02-10 22:00:52 +0000893static void bswap_phdr(struct elf_phdr *phdr)
bellard31e31b82003-02-18 22:55:36 +0000894{
895 bswap32s(&phdr->p_type); /* Segment type */
bellard92a31b12005-02-10 22:00:52 +0000896 bswaptls(&phdr->p_offset); /* Segment file offset */
897 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
898 bswaptls(&phdr->p_paddr); /* Segment physical address */
899 bswaptls(&phdr->p_filesz); /* Segment size in file */
900 bswaptls(&phdr->p_memsz); /* Segment size in memory */
bellard31e31b82003-02-18 22:55:36 +0000901 bswap32s(&phdr->p_flags); /* Segment flags */
bellard92a31b12005-02-10 22:00:52 +0000902 bswaptls(&phdr->p_align); /* Segment alignment */
bellard31e31b82003-02-18 22:55:36 +0000903}
bellard689f9362003-04-29 20:40:07 +0000904
bellard92a31b12005-02-10 22:00:52 +0000905static void bswap_shdr(struct elf_shdr *shdr)
bellard689f9362003-04-29 20:40:07 +0000906{
907 bswap32s(&shdr->sh_name);
908 bswap32s(&shdr->sh_type);
bellard92a31b12005-02-10 22:00:52 +0000909 bswaptls(&shdr->sh_flags);
910 bswaptls(&shdr->sh_addr);
911 bswaptls(&shdr->sh_offset);
912 bswaptls(&shdr->sh_size);
bellard689f9362003-04-29 20:40:07 +0000913 bswap32s(&shdr->sh_link);
914 bswap32s(&shdr->sh_info);
bellard92a31b12005-02-10 22:00:52 +0000915 bswaptls(&shdr->sh_addralign);
916 bswaptls(&shdr->sh_entsize);
bellard689f9362003-04-29 20:40:07 +0000917}
918
j_mayer7a3148a2007-04-05 07:13:51 +0000919static void bswap_sym(struct elf_sym *sym)
bellard689f9362003-04-29 20:40:07 +0000920{
921 bswap32s(&sym->st_name);
j_mayer7a3148a2007-04-05 07:13:51 +0000922 bswaptls(&sym->st_value);
923 bswaptls(&sym->st_size);
bellard689f9362003-04-29 20:40:07 +0000924 bswap16s(&sym->st_shndx);
925}
bellard31e31b82003-02-18 22:55:36 +0000926#endif
927
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300928#ifdef USE_ELF_CORE_DUMP
929static int elf_core_dump(int, const CPUState *);
930
931#ifdef BSWAP_NEEDED
932static void bswap_note(struct elf_note *en)
933{
malc9fdca5a2009-07-18 13:12:20 +0400934 bswap32s(&en->n_namesz);
935 bswap32s(&en->n_descsz);
936 bswap32s(&en->n_type);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300937}
938#endif /* BSWAP_NEEDED */
939
940#endif /* USE_ELF_CORE_DUMP */
941
bellard31e31b82003-02-18 22:55:36 +0000942/*
pbrooke5fe0c52006-06-11 13:32:59 +0000943 * 'copy_elf_strings()' copies argument/envelope strings from user
bellard31e31b82003-02-18 22:55:36 +0000944 * memory to free pages in kernel mem. These are in a format ready
945 * to be put directly into the top of new user memory.
946 *
947 */
blueswir1992f48a2007-10-14 16:27:31 +0000948static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
949 abi_ulong p)
bellard31e31b82003-02-18 22:55:36 +0000950{
951 char *tmp, *tmp1, *pag = NULL;
952 int len, offset = 0;
953
954 if (!p) {
955 return 0; /* bullet-proofing */
956 }
957 while (argc-- > 0) {
bellardedf779f2004-02-22 13:40:13 +0000958 tmp = argv[argc];
959 if (!tmp) {
bellard31e31b82003-02-18 22:55:36 +0000960 fprintf(stderr, "VFS: argc is wrong");
961 exit(-1);
962 }
bellardedf779f2004-02-22 13:40:13 +0000963 tmp1 = tmp;
964 while (*tmp++);
bellard31e31b82003-02-18 22:55:36 +0000965 len = tmp - tmp1;
966 if (p < len) { /* this shouldn't happen - 128kB */
967 return 0;
968 }
969 while (len) {
970 --p; --tmp; --len;
971 if (--offset < 0) {
bellard54936002003-05-13 00:25:15 +0000972 offset = p % TARGET_PAGE_SIZE;
pbrook53a59602006-03-25 19:31:22 +0000973 pag = (char *)page[p/TARGET_PAGE_SIZE];
bellard44a91ca2004-01-18 22:05:44 +0000974 if (!pag) {
pbrook53a59602006-03-25 19:31:22 +0000975 pag = (char *)malloc(TARGET_PAGE_SIZE);
j_mayer4118a972007-09-27 04:10:43 +0000976 memset(pag, 0, TARGET_PAGE_SIZE);
pbrook53a59602006-03-25 19:31:22 +0000977 page[p/TARGET_PAGE_SIZE] = pag;
bellard44a91ca2004-01-18 22:05:44 +0000978 if (!pag)
979 return 0;
bellard31e31b82003-02-18 22:55:36 +0000980 }
981 }
982 if (len == 0 || offset == 0) {
bellardedf779f2004-02-22 13:40:13 +0000983 *(pag + offset) = *tmp;
bellard31e31b82003-02-18 22:55:36 +0000984 }
985 else {
986 int bytes_to_copy = (len > offset) ? offset : len;
987 tmp -= bytes_to_copy;
988 p -= bytes_to_copy;
989 offset -= bytes_to_copy;
990 len -= bytes_to_copy;
991 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
992 }
993 }
994 }
995 return p;
996}
997
blueswir1992f48a2007-10-14 16:27:31 +0000998static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
999 struct image_info *info)
pbrook53a59602006-03-25 19:31:22 +00001000{
blueswir1992f48a2007-10-14 16:27:31 +00001001 abi_ulong stack_base, size, error;
bellard31e31b82003-02-18 22:55:36 +00001002 int i;
bellard31e31b82003-02-18 22:55:36 +00001003
1004 /* Create enough stack to hold everything. If we don't use
1005 * it for args, we'll use it for something else...
1006 */
bellard09bfb052003-04-10 00:03:40 +00001007 size = x86_stack_size;
bellard54936002003-05-13 00:25:15 +00001008 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
1009 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
ths5fafdf22007-09-16 21:08:06 +00001010 error = target_mmap(0,
bellard83fb7ad2004-07-05 21:25:26 +00001011 size + qemu_host_page_size,
bellard54936002003-05-13 00:25:15 +00001012 PROT_READ | PROT_WRITE,
1013 MAP_PRIVATE | MAP_ANONYMOUS,
1014 -1, 0);
bellard09bfb052003-04-10 00:03:40 +00001015 if (error == -1) {
1016 perror("stk mmap");
1017 exit(-1);
bellard31e31b82003-02-18 22:55:36 +00001018 }
bellard09bfb052003-04-10 00:03:40 +00001019 /* we reserve one extra page at the top of the stack as guard */
bellard83fb7ad2004-07-05 21:25:26 +00001020 target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
bellard09bfb052003-04-10 00:03:40 +00001021
bellard54936002003-05-13 00:25:15 +00001022 stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
bellard09bfb052003-04-10 00:03:40 +00001023 p += stack_base;
1024
bellard31e31b82003-02-18 22:55:36 +00001025 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1026 if (bprm->page[i]) {
1027 info->rss++;
bellard579a97f2007-11-11 14:26:47 +00001028 /* FIXME - check return value of memcpy_to_target() for failure */
pbrook53a59602006-03-25 19:31:22 +00001029 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
1030 free(bprm->page[i]);
bellard31e31b82003-02-18 22:55:36 +00001031 }
pbrook53a59602006-03-25 19:31:22 +00001032 stack_base += TARGET_PAGE_SIZE;
bellard31e31b82003-02-18 22:55:36 +00001033 }
1034 return p;
1035}
1036
blueswir1992f48a2007-10-14 16:27:31 +00001037static void set_brk(abi_ulong start, abi_ulong end)
bellard31e31b82003-02-18 22:55:36 +00001038{
1039 /* page-align the start and end addresses... */
bellard54936002003-05-13 00:25:15 +00001040 start = HOST_PAGE_ALIGN(start);
1041 end = HOST_PAGE_ALIGN(end);
bellard31e31b82003-02-18 22:55:36 +00001042 if (end <= start)
1043 return;
bellard54936002003-05-13 00:25:15 +00001044 if(target_mmap(start, end - start,
1045 PROT_READ | PROT_WRITE | PROT_EXEC,
1046 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
bellard31e31b82003-02-18 22:55:36 +00001047 perror("cannot mmap brk");
1048 exit(-1);
1049 }
1050}
1051
1052
bellard853d6f72003-09-30 20:58:32 +00001053/* We need to explicitly zero any fractional pages after the data
1054 section (i.e. bss). This would contain the junk from the file that
1055 should not be in memory. */
blueswir1992f48a2007-10-14 16:27:31 +00001056static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
bellard31e31b82003-02-18 22:55:36 +00001057{
blueswir1992f48a2007-10-14 16:27:31 +00001058 abi_ulong nbyte;
bellard31e31b82003-02-18 22:55:36 +00001059
ths768a4a32006-12-14 13:32:11 +00001060 if (elf_bss >= last_bss)
1061 return;
1062
bellard853d6f72003-09-30 20:58:32 +00001063 /* XXX: this is really a hack : if the real host page size is
1064 smaller than the target page size, some pages after the end
1065 of the file may not be mapped. A better fix would be to
1066 patch target_mmap(), but it is more complicated as the file
1067 size must be known */
bellard83fb7ad2004-07-05 21:25:26 +00001068 if (qemu_real_host_page_size < qemu_host_page_size) {
blueswir1992f48a2007-10-14 16:27:31 +00001069 abi_ulong end_addr, end_addr1;
ths5fafdf22007-09-16 21:08:06 +00001070 end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
bellard83fb7ad2004-07-05 21:25:26 +00001071 ~(qemu_real_host_page_size - 1);
bellard853d6f72003-09-30 20:58:32 +00001072 end_addr = HOST_PAGE_ALIGN(elf_bss);
1073 if (end_addr1 < end_addr) {
j_mayer863cf0b2007-10-07 15:59:45 +00001074 mmap((void *)g2h(end_addr1), end_addr - end_addr1,
bellard853d6f72003-09-30 20:58:32 +00001075 PROT_READ|PROT_WRITE|PROT_EXEC,
1076 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1077 }
1078 }
1079
bellard83fb7ad2004-07-05 21:25:26 +00001080 nbyte = elf_bss & (qemu_host_page_size-1);
bellard31e31b82003-02-18 22:55:36 +00001081 if (nbyte) {
bellard83fb7ad2004-07-05 21:25:26 +00001082 nbyte = qemu_host_page_size - nbyte;
bellard31e31b82003-02-18 22:55:36 +00001083 do {
bellard2f619692007-11-16 10:46:05 +00001084 /* FIXME - what to do if put_user() fails? */
1085 put_user_u8(0, elf_bss);
pbrook53a59602006-03-25 19:31:22 +00001086 elf_bss++;
bellard31e31b82003-02-18 22:55:36 +00001087 } while (--nbyte);
1088 }
1089}
1090
bellardedf779f2004-02-22 13:40:13 +00001091
blueswir1992f48a2007-10-14 16:27:31 +00001092static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
1093 struct elfhdr * exec,
1094 abi_ulong load_addr,
1095 abi_ulong load_bias,
1096 abi_ulong interp_load_addr, int ibcs,
1097 struct image_info *info)
pbrook53a59602006-03-25 19:31:22 +00001098{
blueswir1992f48a2007-10-14 16:27:31 +00001099 abi_ulong sp;
pbrook53a59602006-03-25 19:31:22 +00001100 int size;
blueswir1992f48a2007-10-14 16:27:31 +00001101 abi_ulong u_platform;
pbrook53a59602006-03-25 19:31:22 +00001102 const char *k_platform;
j_mayer863cf0b2007-10-07 15:59:45 +00001103 const int n = sizeof(elf_addr_t);
pbrook53a59602006-03-25 19:31:22 +00001104
1105 sp = p;
1106 u_platform = 0;
bellard15338fd2005-11-26 11:41:16 +00001107 k_platform = ELF_PLATFORM;
1108 if (k_platform) {
1109 size_t len = strlen(k_platform) + 1;
pbrook53a59602006-03-25 19:31:22 +00001110 sp -= (len + n - 1) & ~(n - 1);
1111 u_platform = sp;
bellard579a97f2007-11-11 14:26:47 +00001112 /* FIXME - check return value of memcpy_to_target() for failure */
pbrook53a59602006-03-25 19:31:22 +00001113 memcpy_to_target(sp, k_platform, len);
bellard15338fd2005-11-26 11:41:16 +00001114 }
pbrook53a59602006-03-25 19:31:22 +00001115 /*
1116 * Force 16 byte _final_ alignment here for generality.
1117 */
blueswir1992f48a2007-10-14 16:27:31 +00001118 sp = sp &~ (abi_ulong)15;
pbrook53a59602006-03-25 19:31:22 +00001119 size = (DLINFO_ITEMS + 1) * 2;
bellard15338fd2005-11-26 11:41:16 +00001120 if (k_platform)
pbrook53a59602006-03-25 19:31:22 +00001121 size += 2;
bellardf5155282004-01-04 15:46:50 +00001122#ifdef DLINFO_ARCH_ITEMS
pbrook53a59602006-03-25 19:31:22 +00001123 size += DLINFO_ARCH_ITEMS * 2;
bellardf5155282004-01-04 15:46:50 +00001124#endif
pbrook53a59602006-03-25 19:31:22 +00001125 size += envc + argc + 2;
1126 size += (!ibcs ? 3 : 1); /* argc itself */
1127 size *= n;
1128 if (size & 15)
1129 sp -= 16 - (size & 15);
ths3b46e622007-09-17 08:09:54 +00001130
j_mayer863cf0b2007-10-07 15:59:45 +00001131 /* This is correct because Linux defines
1132 * elf_addr_t as Elf32_Off / Elf64_Off
1133 */
bellard2f619692007-11-16 10:46:05 +00001134#define NEW_AUX_ENT(id, val) do { \
1135 sp -= n; put_user_ual(val, sp); \
1136 sp -= n; put_user_ual(id, sp); \
pbrook53a59602006-03-25 19:31:22 +00001137 } while(0)
bellard2f619692007-11-16 10:46:05 +00001138
bellard0bccf032005-08-21 10:12:28 +00001139 NEW_AUX_ENT (AT_NULL, 0);
bellardf5155282004-01-04 15:46:50 +00001140
bellard0bccf032005-08-21 10:12:28 +00001141 /* There must be exactly DLINFO_ITEMS entries here. */
blueswir1992f48a2007-10-14 16:27:31 +00001142 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
1143 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1144 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1145 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
1146 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
1147 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
bellard0bccf032005-08-21 10:12:28 +00001148 NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
blueswir1992f48a2007-10-14 16:27:31 +00001149 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1150 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1151 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1152 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1153 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
pbrooka07c67d2008-03-26 23:31:55 +00001154 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
bellard15338fd2005-11-26 11:41:16 +00001155 if (k_platform)
pbrook53a59602006-03-25 19:31:22 +00001156 NEW_AUX_ENT(AT_PLATFORM, u_platform);
bellardf5155282004-01-04 15:46:50 +00001157#ifdef ARCH_DLINFO
ths5fafdf22007-09-16 21:08:06 +00001158 /*
bellardf5155282004-01-04 15:46:50 +00001159 * ARCH_DLINFO must come last so platform specific code can enforce
1160 * special alignment requirements on the AUXV if necessary (eg. PPC).
1161 */
1162 ARCH_DLINFO;
1163#endif
1164#undef NEW_AUX_ENT
1165
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001166 info->saved_auxv = sp;
1167
pbrooke5fe0c52006-06-11 13:32:59 +00001168 sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
bellard31e31b82003-02-18 22:55:36 +00001169 return sp;
1170}
1171
1172
blueswir1992f48a2007-10-14 16:27:31 +00001173static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
1174 int interpreter_fd,
1175 abi_ulong *interp_load_addr)
bellard31e31b82003-02-18 22:55:36 +00001176{
1177 struct elf_phdr *elf_phdata = NULL;
1178 struct elf_phdr *eppnt;
blueswir1992f48a2007-10-14 16:27:31 +00001179 abi_ulong load_addr = 0;
bellard31e31b82003-02-18 22:55:36 +00001180 int load_addr_set = 0;
1181 int retval;
blueswir1992f48a2007-10-14 16:27:31 +00001182 abi_ulong last_bss, elf_bss;
1183 abi_ulong error;
bellard31e31b82003-02-18 22:55:36 +00001184 int i;
ths5fafdf22007-09-16 21:08:06 +00001185
bellard31e31b82003-02-18 22:55:36 +00001186 elf_bss = 0;
1187 last_bss = 0;
1188 error = 0;
1189
bellard644c4332003-03-24 23:00:36 +00001190#ifdef BSWAP_NEEDED
1191 bswap_ehdr(interp_elf_ex);
1192#endif
bellard31e31b82003-02-18 22:55:36 +00001193 /* First of all, some simple consistency checks */
ths5fafdf22007-09-16 21:08:06 +00001194 if ((interp_elf_ex->e_type != ET_EXEC &&
1195 interp_elf_ex->e_type != ET_DYN) ||
bellard31e31b82003-02-18 22:55:36 +00001196 !elf_check_arch(interp_elf_ex->e_machine)) {
blueswir1992f48a2007-10-14 16:27:31 +00001197 return ~((abi_ulong)0UL);
bellard31e31b82003-02-18 22:55:36 +00001198 }
ths5fafdf22007-09-16 21:08:06 +00001199
bellard644c4332003-03-24 23:00:36 +00001200
bellard31e31b82003-02-18 22:55:36 +00001201 /* Now read in all of the header information */
ths5fafdf22007-09-16 21:08:06 +00001202
bellard54936002003-05-13 00:25:15 +00001203 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
blueswir1992f48a2007-10-14 16:27:31 +00001204 return ~(abi_ulong)0UL;
ths5fafdf22007-09-16 21:08:06 +00001205
1206 elf_phdata = (struct elf_phdr *)
bellard31e31b82003-02-18 22:55:36 +00001207 malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1208
1209 if (!elf_phdata)
blueswir1992f48a2007-10-14 16:27:31 +00001210 return ~((abi_ulong)0UL);
ths5fafdf22007-09-16 21:08:06 +00001211
bellard31e31b82003-02-18 22:55:36 +00001212 /*
1213 * If the size of this structure has changed, then punt, since
1214 * we will be doing the wrong thing.
1215 */
bellard09bfb052003-04-10 00:03:40 +00001216 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
bellard31e31b82003-02-18 22:55:36 +00001217 free(elf_phdata);
blueswir1992f48a2007-10-14 16:27:31 +00001218 return ~((abi_ulong)0UL);
bellard09bfb052003-04-10 00:03:40 +00001219 }
bellard31e31b82003-02-18 22:55:36 +00001220
1221 retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
1222 if(retval >= 0) {
1223 retval = read(interpreter_fd,
1224 (char *) elf_phdata,
1225 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1226 }
bellard31e31b82003-02-18 22:55:36 +00001227 if (retval < 0) {
1228 perror("load_elf_interp");
1229 exit(-1);
1230 free (elf_phdata);
1231 return retval;
1232 }
1233#ifdef BSWAP_NEEDED
1234 eppnt = elf_phdata;
1235 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
1236 bswap_phdr(eppnt);
1237 }
1238#endif
bellard09bfb052003-04-10 00:03:40 +00001239
1240 if (interp_elf_ex->e_type == ET_DYN) {
thse91c8a72007-06-03 13:35:16 +00001241 /* in order to avoid hardcoding the interpreter load
bellard09bfb052003-04-10 00:03:40 +00001242 address in qemu, we allocate a big enough memory zone */
bellard54936002003-05-13 00:25:15 +00001243 error = target_mmap(0, INTERP_MAP_SIZE,
ths5fafdf22007-09-16 21:08:06 +00001244 PROT_NONE, MAP_PRIVATE | MAP_ANON,
bellard54936002003-05-13 00:25:15 +00001245 -1, 0);
bellard09bfb052003-04-10 00:03:40 +00001246 if (error == -1) {
1247 perror("mmap");
1248 exit(-1);
1249 }
1250 load_addr = error;
1251 load_addr_set = 1;
1252 }
1253
bellard31e31b82003-02-18 22:55:36 +00001254 eppnt = elf_phdata;
1255 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
1256 if (eppnt->p_type == PT_LOAD) {
1257 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
1258 int elf_prot = 0;
blueswir1992f48a2007-10-14 16:27:31 +00001259 abi_ulong vaddr = 0;
1260 abi_ulong k;
bellard31e31b82003-02-18 22:55:36 +00001261
1262 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1263 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1264 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1265 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
1266 elf_type |= MAP_FIXED;
1267 vaddr = eppnt->p_vaddr;
1268 }
bellard54936002003-05-13 00:25:15 +00001269 error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
1270 eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
bellard31e31b82003-02-18 22:55:36 +00001271 elf_prot,
1272 elf_type,
1273 interpreter_fd,
bellard54936002003-05-13 00:25:15 +00001274 eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
ths3b46e622007-09-17 08:09:54 +00001275
pbrooke89f07d2006-02-04 20:46:24 +00001276 if (error == -1) {
bellard31e31b82003-02-18 22:55:36 +00001277 /* Real error */
1278 close(interpreter_fd);
1279 free(elf_phdata);
blueswir1992f48a2007-10-14 16:27:31 +00001280 return ~((abi_ulong)0UL);
bellard31e31b82003-02-18 22:55:36 +00001281 }
1282
1283 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
1284 load_addr = error;
1285 load_addr_set = 1;
1286 }
1287
1288 /*
1289 * Find the end of the file mapping for this phdr, and keep
1290 * track of the largest address we see for this.
1291 */
1292 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
1293 if (k > elf_bss) elf_bss = k;
1294
1295 /*
1296 * Do the same thing for the memory mapping - between
1297 * elf_bss and last_bss is the bss section.
1298 */
1299 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
1300 if (k > last_bss) last_bss = k;
1301 }
ths5fafdf22007-09-16 21:08:06 +00001302
bellard31e31b82003-02-18 22:55:36 +00001303 /* Now use mmap to map the library into memory. */
1304
1305 close(interpreter_fd);
1306
1307 /*
1308 * Now fill out the bss section. First pad the last page up
1309 * to the page boundary, and then perform a mmap to make sure
1310 * that there are zeromapped pages up to and including the last
1311 * bss page.
1312 */
ths768a4a32006-12-14 13:32:11 +00001313 padzero(elf_bss, last_bss);
bellard83fb7ad2004-07-05 21:25:26 +00001314 elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
bellard31e31b82003-02-18 22:55:36 +00001315
1316 /* Map the last of the bss segment */
1317 if (last_bss > elf_bss) {
bellard54936002003-05-13 00:25:15 +00001318 target_mmap(elf_bss, last_bss-elf_bss,
1319 PROT_READ|PROT_WRITE|PROT_EXEC,
1320 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
bellard31e31b82003-02-18 22:55:36 +00001321 }
1322 free(elf_phdata);
1323
1324 *interp_load_addr = load_addr;
blueswir1992f48a2007-10-14 16:27:31 +00001325 return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
bellard31e31b82003-02-18 22:55:36 +00001326}
1327
pbrook49918a72008-10-22 15:11:31 +00001328static int symfind(const void *s0, const void *s1)
1329{
1330 struct elf_sym *key = (struct elf_sym *)s0;
1331 struct elf_sym *sym = (struct elf_sym *)s1;
1332 int result = 0;
1333 if (key->st_value < sym->st_value) {
1334 result = -1;
Laurent Desnoguesec822002009-07-30 19:23:49 +02001335 } else if (key->st_value >= sym->st_value + sym->st_size) {
pbrook49918a72008-10-22 15:11:31 +00001336 result = 1;
1337 }
1338 return result;
1339}
1340
1341static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
1342{
1343#if ELF_CLASS == ELFCLASS32
1344 struct elf_sym *syms = s->disas_symtab.elf32;
1345#else
1346 struct elf_sym *syms = s->disas_symtab.elf64;
1347#endif
1348
1349 // binary search
1350 struct elf_sym key;
1351 struct elf_sym *sym;
1352
1353 key.st_value = orig_addr;
1354
1355 sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), symfind);
Blue Swirl7cba04f2009-08-01 10:13:20 +00001356 if (sym != NULL) {
pbrook49918a72008-10-22 15:11:31 +00001357 return s->disas_strtab + sym->st_name;
1358 }
1359
1360 return "";
1361}
1362
1363/* FIXME: This should use elf_ops.h */
1364static int symcmp(const void *s0, const void *s1)
1365{
1366 struct elf_sym *sym0 = (struct elf_sym *)s0;
1367 struct elf_sym *sym1 = (struct elf_sym *)s1;
1368 return (sym0->st_value < sym1->st_value)
1369 ? -1
1370 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
1371}
1372
bellard689f9362003-04-29 20:40:07 +00001373/* Best attempt to load symbols from this ELF object. */
1374static void load_symbols(struct elfhdr *hdr, int fd)
1375{
pbrook49918a72008-10-22 15:11:31 +00001376 unsigned int i, nsyms;
bellard689f9362003-04-29 20:40:07 +00001377 struct elf_shdr sechdr, symtab, strtab;
1378 char *strings;
bellarde80cfcf2004-12-19 23:18:01 +00001379 struct syminfo *s;
pbrook49918a72008-10-22 15:11:31 +00001380 struct elf_sym *syms;
bellard31e31b82003-02-18 22:55:36 +00001381
bellard689f9362003-04-29 20:40:07 +00001382 lseek(fd, hdr->e_shoff, SEEK_SET);
1383 for (i = 0; i < hdr->e_shnum; i++) {
pbrook49918a72008-10-22 15:11:31 +00001384 if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
1385 return;
bellard689f9362003-04-29 20:40:07 +00001386#ifdef BSWAP_NEEDED
pbrook49918a72008-10-22 15:11:31 +00001387 bswap_shdr(&sechdr);
bellard689f9362003-04-29 20:40:07 +00001388#endif
pbrook49918a72008-10-22 15:11:31 +00001389 if (sechdr.sh_type == SHT_SYMTAB) {
1390 symtab = sechdr;
1391 lseek(fd, hdr->e_shoff
1392 + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
1393 if (read(fd, &strtab, sizeof(strtab))
1394 != sizeof(strtab))
1395 return;
bellard689f9362003-04-29 20:40:07 +00001396#ifdef BSWAP_NEEDED
pbrook49918a72008-10-22 15:11:31 +00001397 bswap_shdr(&strtab);
bellard689f9362003-04-29 20:40:07 +00001398#endif
pbrook49918a72008-10-22 15:11:31 +00001399 goto found;
1400 }
bellard689f9362003-04-29 20:40:07 +00001401 }
1402 return; /* Shouldn't happen... */
1403
1404 found:
1405 /* Now know where the strtab and symtab are. Snarf them. */
bellarde80cfcf2004-12-19 23:18:01 +00001406 s = malloc(sizeof(*s));
pbrook49918a72008-10-22 15:11:31 +00001407 syms = malloc(symtab.sh_size);
1408 if (!syms)
1409 return;
bellarde80cfcf2004-12-19 23:18:01 +00001410 s->disas_strtab = strings = malloc(strtab.sh_size);
pbrook49918a72008-10-22 15:11:31 +00001411 if (!s->disas_strtab)
1412 return;
ths5fafdf22007-09-16 21:08:06 +00001413
bellard689f9362003-04-29 20:40:07 +00001414 lseek(fd, symtab.sh_offset, SEEK_SET);
pbrook49918a72008-10-22 15:11:31 +00001415 if (read(fd, syms, symtab.sh_size) != symtab.sh_size)
1416 return;
bellard689f9362003-04-29 20:40:07 +00001417
pbrook49918a72008-10-22 15:11:31 +00001418 nsyms = symtab.sh_size / sizeof(struct elf_sym);
1419
1420 i = 0;
1421 while (i < nsyms) {
bellard689f9362003-04-29 20:40:07 +00001422#ifdef BSWAP_NEEDED
pbrook49918a72008-10-22 15:11:31 +00001423 bswap_sym(syms + i);
bellard689f9362003-04-29 20:40:07 +00001424#endif
pbrook49918a72008-10-22 15:11:31 +00001425 // Throw away entries which we do not need.
1426 if (syms[i].st_shndx == SHN_UNDEF ||
1427 syms[i].st_shndx >= SHN_LORESERVE ||
1428 ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
1429 nsyms--;
1430 if (i < nsyms) {
1431 syms[i] = syms[nsyms];
1432 }
1433 continue;
1434 }
1435#if defined(TARGET_ARM) || defined (TARGET_MIPS)
1436 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
1437 syms[i].st_value &= ~(target_ulong)1;
blueswir10774bed2007-07-05 13:23:29 +00001438#endif
pbrook49918a72008-10-22 15:11:31 +00001439 i++;
blueswir10774bed2007-07-05 13:23:29 +00001440 }
pbrook49918a72008-10-22 15:11:31 +00001441 syms = realloc(syms, nsyms * sizeof(*syms));
bellard689f9362003-04-29 20:40:07 +00001442
pbrook49918a72008-10-22 15:11:31 +00001443 qsort(syms, nsyms, sizeof(*syms), symcmp);
1444
bellard689f9362003-04-29 20:40:07 +00001445 lseek(fd, strtab.sh_offset, SEEK_SET);
1446 if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
pbrook49918a72008-10-22 15:11:31 +00001447 return;
1448 s->disas_num_syms = nsyms;
1449#if ELF_CLASS == ELFCLASS32
1450 s->disas_symtab.elf32 = syms;
Paul Brook9f9f0302010-03-01 03:55:48 +00001451 s->lookup_symbol = lookup_symbolxx;
pbrook49918a72008-10-22 15:11:31 +00001452#else
1453 s->disas_symtab.elf64 = syms;
Paul Brook9f9f0302010-03-01 03:55:48 +00001454 s->lookup_symbol = lookup_symbolxx;
pbrook49918a72008-10-22 15:11:31 +00001455#endif
bellarde80cfcf2004-12-19 23:18:01 +00001456 s->next = syminfos;
1457 syminfos = s;
bellard689f9362003-04-29 20:40:07 +00001458}
bellard31e31b82003-02-18 22:55:36 +00001459
pbrooke5fe0c52006-06-11 13:32:59 +00001460int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
1461 struct image_info * info)
bellard31e31b82003-02-18 22:55:36 +00001462{
1463 struct elfhdr elf_ex;
1464 struct elfhdr interp_elf_ex;
1465 struct exec interp_ex;
1466 int interpreter_fd = -1; /* avoid warning */
blueswir1992f48a2007-10-14 16:27:31 +00001467 abi_ulong load_addr, load_bias;
bellard31e31b82003-02-18 22:55:36 +00001468 int load_addr_set = 0;
1469 unsigned int interpreter_type = INTERPRETER_NONE;
1470 unsigned char ibcs2_interpreter;
1471 int i;
blueswir1992f48a2007-10-14 16:27:31 +00001472 abi_ulong mapped_addr;
bellard31e31b82003-02-18 22:55:36 +00001473 struct elf_phdr * elf_ppnt;
1474 struct elf_phdr *elf_phdata;
blueswir1992f48a2007-10-14 16:27:31 +00001475 abi_ulong elf_bss, k, elf_brk;
bellard31e31b82003-02-18 22:55:36 +00001476 int retval;
1477 char * elf_interpreter;
blueswir1992f48a2007-10-14 16:27:31 +00001478 abi_ulong elf_entry, interp_load_addr = 0;
bellard31e31b82003-02-18 22:55:36 +00001479 int status;
blueswir1992f48a2007-10-14 16:27:31 +00001480 abi_ulong start_code, end_code, start_data, end_data;
1481 abi_ulong reloc_func_desc = 0;
1482 abi_ulong elf_stack;
bellard31e31b82003-02-18 22:55:36 +00001483 char passed_fileno[6];
1484
1485 ibcs2_interpreter = 0;
1486 status = 0;
1487 load_addr = 0;
bellard09bfb052003-04-10 00:03:40 +00001488 load_bias = 0;
bellard31e31b82003-02-18 22:55:36 +00001489 elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
1490#ifdef BSWAP_NEEDED
1491 bswap_ehdr(&elf_ex);
1492#endif
1493
bellard31e31b82003-02-18 22:55:36 +00001494 /* First of all, some simple consistency checks */
1495 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
1496 (! elf_check_arch(elf_ex.e_machine))) {
1497 return -ENOEXEC;
1498 }
1499
pbrooke5fe0c52006-06-11 13:32:59 +00001500 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
1501 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
1502 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
1503 if (!bprm->p) {
1504 retval = -E2BIG;
1505 }
1506
bellard31e31b82003-02-18 22:55:36 +00001507 /* Now read in all of the header information */
bellard31e31b82003-02-18 22:55:36 +00001508 elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
1509 if (elf_phdata == NULL) {
1510 return -ENOMEM;
1511 }
1512
1513 retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
1514 if(retval > 0) {
ths5fafdf22007-09-16 21:08:06 +00001515 retval = read(bprm->fd, (char *) elf_phdata,
bellard31e31b82003-02-18 22:55:36 +00001516 elf_ex.e_phentsize * elf_ex.e_phnum);
1517 }
1518
1519 if (retval < 0) {
1520 perror("load_elf_binary");
1521 exit(-1);
1522 free (elf_phdata);
1523 return -errno;
1524 }
1525
bellardb17780d2003-02-18 23:32:15 +00001526#ifdef BSWAP_NEEDED
1527 elf_ppnt = elf_phdata;
1528 for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
1529 bswap_phdr(elf_ppnt);
1530 }
1531#endif
bellard31e31b82003-02-18 22:55:36 +00001532 elf_ppnt = elf_phdata;
1533
1534 elf_bss = 0;
1535 elf_brk = 0;
1536
1537
blueswir1992f48a2007-10-14 16:27:31 +00001538 elf_stack = ~((abi_ulong)0UL);
bellard31e31b82003-02-18 22:55:36 +00001539 elf_interpreter = NULL;
blueswir1992f48a2007-10-14 16:27:31 +00001540 start_code = ~((abi_ulong)0UL);
bellard31e31b82003-02-18 22:55:36 +00001541 end_code = 0;
j_mayer863cf0b2007-10-07 15:59:45 +00001542 start_data = 0;
bellard31e31b82003-02-18 22:55:36 +00001543 end_data = 0;
blueswir198448f52008-09-30 18:16:09 +00001544 interp_ex.a_info = 0;
bellard31e31b82003-02-18 22:55:36 +00001545
1546 for(i=0;i < elf_ex.e_phnum; i++) {
1547 if (elf_ppnt->p_type == PT_INTERP) {
1548 if ( elf_interpreter != NULL )
1549 {
1550 free (elf_phdata);
1551 free(elf_interpreter);
1552 close(bprm->fd);
1553 return -EINVAL;
1554 }
1555
1556 /* This is the program interpreter used for
1557 * shared libraries - for now assume that this
1558 * is an a.out format binary
1559 */
1560
bellard32ce6332003-04-11 00:16:16 +00001561 elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
bellard31e31b82003-02-18 22:55:36 +00001562
1563 if (elf_interpreter == NULL) {
1564 free (elf_phdata);
1565 close(bprm->fd);
1566 return -ENOMEM;
1567 }
1568
bellard31e31b82003-02-18 22:55:36 +00001569 retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
1570 if(retval >= 0) {
bellard32ce6332003-04-11 00:16:16 +00001571 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
bellard31e31b82003-02-18 22:55:36 +00001572 }
1573 if(retval < 0) {
1574 perror("load_elf_binary2");
1575 exit(-1);
ths5fafdf22007-09-16 21:08:06 +00001576 }
bellard31e31b82003-02-18 22:55:36 +00001577
1578 /* If the program interpreter is one of these two,
1579 then assume an iBCS2 image. Otherwise assume
1580 a native linux image. */
1581
1582 /* JRP - Need to add X86 lib dir stuff here... */
1583
1584 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
1585 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
1586 ibcs2_interpreter = 1;
1587 }
1588
1589#if 0
Paul Bolle3bc0bdc2009-10-02 14:06:47 +02001590 printf("Using ELF interpreter %s\n", path(elf_interpreter));
bellard31e31b82003-02-18 22:55:36 +00001591#endif
1592 if (retval >= 0) {
bellard32ce6332003-04-11 00:16:16 +00001593 retval = open(path(elf_interpreter), O_RDONLY);
bellard31e31b82003-02-18 22:55:36 +00001594 if(retval >= 0) {
1595 interpreter_fd = retval;
1596 }
1597 else {
1598 perror(elf_interpreter);
1599 exit(-1);
1600 /* retval = -errno; */
1601 }
1602 }
1603
1604 if (retval >= 0) {
1605 retval = lseek(interpreter_fd, 0, SEEK_SET);
1606 if(retval >= 0) {
1607 retval = read(interpreter_fd,bprm->buf,128);
1608 }
1609 }
1610 if (retval >= 0) {
1611 interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
Michael S. Tsirkin6ece4df2009-09-30 19:43:38 +02001612 interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */
bellard31e31b82003-02-18 22:55:36 +00001613 }
1614 if (retval < 0) {
1615 perror("load_elf_binary3");
1616 exit(-1);
1617 free (elf_phdata);
1618 free(elf_interpreter);
1619 close(bprm->fd);
1620 return retval;
1621 }
1622 }
1623 elf_ppnt++;
1624 }
1625
1626 /* Some simple consistency checks for the interpreter */
1627 if (elf_interpreter){
1628 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
1629
1630 /* Now figure out which format our binary is */
1631 if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
1632 (N_MAGIC(interp_ex) != QMAGIC)) {
1633 interpreter_type = INTERPRETER_ELF;
1634 }
1635
1636 if (interp_elf_ex.e_ident[0] != 0x7f ||
blueswir1b55266b2008-09-20 08:07:15 +00001637 strncmp((char *)&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
bellard31e31b82003-02-18 22:55:36 +00001638 interpreter_type &= ~INTERPRETER_ELF;
1639 }
1640
1641 if (!interpreter_type) {
1642 free(elf_interpreter);
1643 free(elf_phdata);
1644 close(bprm->fd);
1645 return -ELIBBAD;
1646 }
1647 }
1648
1649 /* OK, we are done with that, now set up the arg stuff,
1650 and then start this sucker up */
1651
pbrooke5fe0c52006-06-11 13:32:59 +00001652 {
bellard31e31b82003-02-18 22:55:36 +00001653 char * passed_p;
1654
1655 if (interpreter_type == INTERPRETER_AOUT) {
bellardeba2af62004-06-19 17:23:39 +00001656 snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
bellard31e31b82003-02-18 22:55:36 +00001657 passed_p = passed_fileno;
1658
1659 if (elf_interpreter) {
pbrooke5fe0c52006-06-11 13:32:59 +00001660 bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
bellard31e31b82003-02-18 22:55:36 +00001661 bprm->argc++;
1662 }
1663 }
1664 if (!bprm->p) {
1665 if (elf_interpreter) {
1666 free(elf_interpreter);
1667 }
1668 free (elf_phdata);
1669 close(bprm->fd);
1670 return -E2BIG;
1671 }
1672 }
1673
1674 /* OK, This is the point of no return */
1675 info->end_data = 0;
1676 info->end_code = 0;
blueswir1992f48a2007-10-14 16:27:31 +00001677 info->start_mmap = (abi_ulong)ELF_START_MMAP;
bellard31e31b82003-02-18 22:55:36 +00001678 info->mmap = 0;
blueswir1992f48a2007-10-14 16:27:31 +00001679 elf_entry = (abi_ulong) elf_ex.e_entry;
bellard31e31b82003-02-18 22:55:36 +00001680
Paul Brook379f6692009-07-17 12:48:08 +01001681#if defined(CONFIG_USE_GUEST_BASE)
1682 /*
1683 * In case where user has not explicitly set the guest_base, we
1684 * probe here that should we set it automatically.
1685 */
1686 if (!have_guest_base) {
1687 /*
1688 * Go through ELF program header table and find out whether
1689 * any of the segments drop below our current mmap_min_addr and
1690 * in that case set guest_base to corresponding address.
1691 */
1692 for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum;
1693 i++, elf_ppnt++) {
1694 if (elf_ppnt->p_type != PT_LOAD)
1695 continue;
1696 if (HOST_PAGE_ALIGN(elf_ppnt->p_vaddr) < mmap_min_addr) {
1697 guest_base = HOST_PAGE_ALIGN(mmap_min_addr);
1698 break;
1699 }
1700 }
1701 }
1702#endif /* CONFIG_USE_GUEST_BASE */
1703
bellard31e31b82003-02-18 22:55:36 +00001704 /* Do this so that we can load the interpreter, if need be. We will
1705 change some of these later */
1706 info->rss = 0;
1707 bprm->p = setup_arg_pages(bprm->p, bprm, info);
1708 info->start_stack = bprm->p;
1709
1710 /* Now we do a little grungy work by mmaping the ELF image into
1711 * the correct location in memory. At this point, we assume that
1712 * the image should be loaded at fixed address, not at a variable
1713 * address.
1714 */
1715
bellard31e31b82003-02-18 22:55:36 +00001716 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
bellard09bfb052003-04-10 00:03:40 +00001717 int elf_prot = 0;
1718 int elf_flags = 0;
blueswir1992f48a2007-10-14 16:27:31 +00001719 abi_ulong error;
ths3b46e622007-09-17 08:09:54 +00001720
bellard09bfb052003-04-10 00:03:40 +00001721 if (elf_ppnt->p_type != PT_LOAD)
1722 continue;
ths3b46e622007-09-17 08:09:54 +00001723
bellard09bfb052003-04-10 00:03:40 +00001724 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1725 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1726 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1727 elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
1728 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
1729 elf_flags |= MAP_FIXED;
1730 } else if (elf_ex.e_type == ET_DYN) {
1731 /* Try and get dynamic programs out of the way of the default mmap
1732 base, as well as whatever program they might try to exec. This
1733 is because the brk will follow the loader, and is not movable. */
1734 /* NOTE: for qemu, we do a big mmap to get enough space
thse91c8a72007-06-03 13:35:16 +00001735 without hardcoding any address */
bellard54936002003-05-13 00:25:15 +00001736 error = target_mmap(0, ET_DYN_MAP_SIZE,
ths5fafdf22007-09-16 21:08:06 +00001737 PROT_NONE, MAP_PRIVATE | MAP_ANON,
bellard54936002003-05-13 00:25:15 +00001738 -1, 0);
bellard09bfb052003-04-10 00:03:40 +00001739 if (error == -1) {
1740 perror("mmap");
1741 exit(-1);
1742 }
bellard54936002003-05-13 00:25:15 +00001743 load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
bellard09bfb052003-04-10 00:03:40 +00001744 }
ths3b46e622007-09-17 08:09:54 +00001745
bellard54936002003-05-13 00:25:15 +00001746 error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1747 (elf_ppnt->p_filesz +
1748 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1749 elf_prot,
1750 (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1751 bprm->fd,
ths5fafdf22007-09-16 21:08:06 +00001752 (elf_ppnt->p_offset -
bellard54936002003-05-13 00:25:15 +00001753 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
bellard09bfb052003-04-10 00:03:40 +00001754 if (error == -1) {
1755 perror("mmap");
1756 exit(-1);
1757 }
bellard31e31b82003-02-18 22:55:36 +00001758
1759#ifdef LOW_ELF_STACK
bellard54936002003-05-13 00:25:15 +00001760 if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1761 elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
bellard31e31b82003-02-18 22:55:36 +00001762#endif
ths3b46e622007-09-17 08:09:54 +00001763
bellard09bfb052003-04-10 00:03:40 +00001764 if (!load_addr_set) {
1765 load_addr_set = 1;
1766 load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1767 if (elf_ex.e_type == ET_DYN) {
1768 load_bias += error -
bellard54936002003-05-13 00:25:15 +00001769 TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
bellard09bfb052003-04-10 00:03:40 +00001770 load_addr += load_bias;
j_mayer84409dd2007-04-06 08:56:50 +00001771 reloc_func_desc = load_bias;
bellard09bfb052003-04-10 00:03:40 +00001772 }
1773 }
1774 k = elf_ppnt->p_vaddr;
ths5fafdf22007-09-16 21:08:06 +00001775 if (k < start_code)
bellard09bfb052003-04-10 00:03:40 +00001776 start_code = k;
j_mayer863cf0b2007-10-07 15:59:45 +00001777 if (start_data < k)
1778 start_data = k;
bellard09bfb052003-04-10 00:03:40 +00001779 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
ths5fafdf22007-09-16 21:08:06 +00001780 if (k > elf_bss)
bellard09bfb052003-04-10 00:03:40 +00001781 elf_bss = k;
1782 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
1783 end_code = k;
ths5fafdf22007-09-16 21:08:06 +00001784 if (end_data < k)
bellard09bfb052003-04-10 00:03:40 +00001785 end_data = k;
1786 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1787 if (k > elf_brk) elf_brk = k;
bellard31e31b82003-02-18 22:55:36 +00001788 }
1789
bellard09bfb052003-04-10 00:03:40 +00001790 elf_entry += load_bias;
1791 elf_bss += load_bias;
1792 elf_brk += load_bias;
1793 start_code += load_bias;
1794 end_code += load_bias;
j_mayer863cf0b2007-10-07 15:59:45 +00001795 start_data += load_bias;
bellard09bfb052003-04-10 00:03:40 +00001796 end_data += load_bias;
1797
bellard31e31b82003-02-18 22:55:36 +00001798 if (elf_interpreter) {
1799 if (interpreter_type & 1) {
1800 elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1801 }
1802 else if (interpreter_type & 2) {
1803 elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1804 &interp_load_addr);
1805 }
j_mayer84409dd2007-04-06 08:56:50 +00001806 reloc_func_desc = interp_load_addr;
bellard31e31b82003-02-18 22:55:36 +00001807
1808 close(interpreter_fd);
1809 free(elf_interpreter);
1810
blueswir1992f48a2007-10-14 16:27:31 +00001811 if (elf_entry == ~((abi_ulong)0UL)) {
bellard31e31b82003-02-18 22:55:36 +00001812 printf("Unable to load interpreter\n");
1813 free(elf_phdata);
1814 exit(-1);
1815 return 0;
1816 }
1817 }
1818
1819 free(elf_phdata);
1820
aliguori93fcfe32009-01-15 22:34:14 +00001821 if (qemu_log_enabled())
bellard689f9362003-04-29 20:40:07 +00001822 load_symbols(&elf_ex, bprm->fd);
1823
bellard31e31b82003-02-18 22:55:36 +00001824 if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
1825 info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
1826
1827#ifdef LOW_ELF_STACK
1828 info->start_stack = bprm->p = elf_stack - 4;
1829#endif
pbrook53a59602006-03-25 19:31:22 +00001830 bprm->p = create_elf_tables(bprm->p,
bellard31e31b82003-02-18 22:55:36 +00001831 bprm->argc,
1832 bprm->envc,
bellarda1516e92003-07-09 17:13:37 +00001833 &elf_ex,
bellard09bfb052003-04-10 00:03:40 +00001834 load_addr, load_bias,
bellard31e31b82003-02-18 22:55:36 +00001835 interp_load_addr,
1836 (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1837 info);
j_mayer92a343d2007-09-27 01:14:15 +00001838 info->load_addr = reloc_func_desc;
bellard31e31b82003-02-18 22:55:36 +00001839 info->start_brk = info->brk = elf_brk;
1840 info->end_code = end_code;
1841 info->start_code = start_code;
j_mayer863cf0b2007-10-07 15:59:45 +00001842 info->start_data = start_data;
bellard31e31b82003-02-18 22:55:36 +00001843 info->end_data = end_data;
1844 info->start_stack = bprm->p;
1845
1846 /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1847 sections */
1848 set_brk(elf_bss, elf_brk);
1849
ths768a4a32006-12-14 13:32:11 +00001850 padzero(elf_bss, elf_brk);
bellard31e31b82003-02-18 22:55:36 +00001851
1852#if 0
1853 printf("(start_brk) %x\n" , info->start_brk);
1854 printf("(end_code) %x\n" , info->end_code);
1855 printf("(start_code) %x\n" , info->start_code);
1856 printf("(end_data) %x\n" , info->end_data);
1857 printf("(start_stack) %x\n" , info->start_stack);
1858 printf("(brk) %x\n" , info->brk);
1859#endif
1860
1861 if ( info->personality == PER_SVR4 )
1862 {
1863 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1864 and some applications "depend" upon this behavior.
1865 Since we do not have the power to recompile these, we
1866 emulate the SVr4 behavior. Sigh. */
bellard83fb7ad2004-07-05 21:25:26 +00001867 mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
bellard54936002003-05-13 00:25:15 +00001868 MAP_FIXED | MAP_PRIVATE, -1, 0);
bellard31e31b82003-02-18 22:55:36 +00001869 }
1870
bellard31e31b82003-02-18 22:55:36 +00001871 info->entry = elf_entry;
1872
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001873#ifdef USE_ELF_CORE_DUMP
1874 bprm->core_dump = &elf_core_dump;
1875#endif
1876
bellard31e31b82003-02-18 22:55:36 +00001877 return 0;
1878}
1879
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001880#ifdef USE_ELF_CORE_DUMP
1881
1882/*
1883 * Definitions to generate Intel SVR4-like core files.
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001884 * These mostly have the same names as the SVR4 types with "target_elf_"
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001885 * tacked on the front to prevent clashes with linux definitions,
1886 * and the typedef forms have been avoided. This is mostly like
1887 * the SVR4 structure, but more Linuxy, with things that Linux does
1888 * not support and which gdb doesn't really use excluded.
1889 *
1890 * Fields we don't dump (their contents is zero) in linux-user qemu
1891 * are marked with XXX.
1892 *
1893 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
1894 *
1895 * Porting ELF coredump for target is (quite) simple process. First you
Nathan Froyddd0a3652009-12-11 09:04:45 -08001896 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001897 * the target resides):
1898 *
1899 * #define USE_ELF_CORE_DUMP
1900 *
1901 * Next you define type of register set used for dumping. ELF specification
1902 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
1903 *
Anthony Liguoric227f092009-10-01 16:12:16 -05001904 * typedef <target_regtype> target_elf_greg_t;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001905 * #define ELF_NREG <number of registers>
Anthony Liguoric227f092009-10-01 16:12:16 -05001906 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001907 *
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001908 * Last step is to implement target specific function that copies registers
1909 * from given cpu into just specified register set. Prototype is:
1910 *
Anthony Liguoric227f092009-10-01 16:12:16 -05001911 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001912 * const CPUState *env);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001913 *
1914 * Parameters:
1915 * regs - copy register values into here (allocated and zeroed by caller)
1916 * env - copy registers from here
1917 *
1918 * Example for ARM target is provided in this file.
1919 */
1920
1921/* An ELF note in memory */
1922struct memelfnote {
1923 const char *name;
1924 size_t namesz;
1925 size_t namesz_rounded;
1926 int type;
1927 size_t datasz;
1928 void *data;
1929 size_t notesz;
1930};
1931
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001932struct target_elf_siginfo {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001933 int si_signo; /* signal number */
1934 int si_code; /* extra code */
1935 int si_errno; /* errno */
1936};
1937
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001938struct target_elf_prstatus {
1939 struct target_elf_siginfo pr_info; /* Info associated with signal */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001940 short pr_cursig; /* Current signal */
1941 target_ulong pr_sigpend; /* XXX */
1942 target_ulong pr_sighold; /* XXX */
Anthony Liguoric227f092009-10-01 16:12:16 -05001943 target_pid_t pr_pid;
1944 target_pid_t pr_ppid;
1945 target_pid_t pr_pgrp;
1946 target_pid_t pr_sid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001947 struct target_timeval pr_utime; /* XXX User time */
1948 struct target_timeval pr_stime; /* XXX System time */
1949 struct target_timeval pr_cutime; /* XXX Cumulative user time */
1950 struct target_timeval pr_cstime; /* XXX Cumulative system time */
Anthony Liguoric227f092009-10-01 16:12:16 -05001951 target_elf_gregset_t pr_reg; /* GP registers */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001952 int pr_fpvalid; /* XXX */
1953};
1954
1955#define ELF_PRARGSZ (80) /* Number of chars for args */
1956
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001957struct target_elf_prpsinfo {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001958 char pr_state; /* numeric process state */
1959 char pr_sname; /* char for pr_state */
1960 char pr_zomb; /* zombie */
1961 char pr_nice; /* nice val */
1962 target_ulong pr_flag; /* flags */
Anthony Liguoric227f092009-10-01 16:12:16 -05001963 target_uid_t pr_uid;
1964 target_gid_t pr_gid;
1965 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001966 /* Lots missing */
1967 char pr_fname[16]; /* filename of executable */
1968 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
1969};
1970
1971/* Here is the structure in which status of each thread is captured. */
1972struct elf_thread_status {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001973 QTAILQ_ENTRY(elf_thread_status) ets_link;
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001974 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001975#if 0
1976 elf_fpregset_t fpu; /* NT_PRFPREG */
1977 struct task_struct *thread;
1978 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
1979#endif
1980 struct memelfnote notes[1];
1981 int num_notes;
1982};
1983
1984struct elf_note_info {
1985 struct memelfnote *notes;
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01001986 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
1987 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001988
Blue Swirl72cf2d42009-09-12 07:36:22 +00001989 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03001990#if 0
1991 /*
1992 * Current version of ELF coredump doesn't support
1993 * dumping fp regs etc.
1994 */
1995 elf_fpregset_t *fpu;
1996 elf_fpxregset_t *xfpu;
1997 int thread_status_size;
1998#endif
1999 int notes_size;
2000 int numnote;
2001};
2002
2003struct vm_area_struct {
2004 abi_ulong vma_start; /* start vaddr of memory region */
2005 abi_ulong vma_end; /* end vaddr of memory region */
2006 abi_ulong vma_flags; /* protection etc. flags for the region */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002007 QTAILQ_ENTRY(vm_area_struct) vma_link;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002008};
2009
2010struct mm_struct {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002011 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002012 int mm_count; /* number of mappings */
2013};
2014
2015static struct mm_struct *vma_init(void);
2016static void vma_delete(struct mm_struct *);
2017static int vma_add_mapping(struct mm_struct *, abi_ulong,
2018 abi_ulong, abi_ulong);
2019static int vma_get_mapping_count(const struct mm_struct *);
2020static struct vm_area_struct *vma_first(const struct mm_struct *);
2021static struct vm_area_struct *vma_next(struct vm_area_struct *);
2022static abi_ulong vma_dump_size(const struct vm_area_struct *);
2023static int vma_walker(void *priv, unsigned long start, unsigned long end,
2024 unsigned long flags);
2025
2026static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
2027static void fill_note(struct memelfnote *, const char *, int,
2028 unsigned int, void *);
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002029static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
2030static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002031static void fill_auxv_note(struct memelfnote *, const TaskState *);
2032static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
2033static size_t note_size(const struct memelfnote *);
2034static void free_note_info(struct elf_note_info *);
2035static int fill_note_info(struct elf_note_info *, long, const CPUState *);
2036static void fill_thread_info(struct elf_note_info *, const CPUState *);
2037static int core_dump_filename(const TaskState *, char *, size_t);
2038
2039static int dump_write(int, const void *, size_t);
2040static int write_note(struct memelfnote *, int);
2041static int write_note_info(struct elf_note_info *, int);
2042
2043#ifdef BSWAP_NEEDED
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002044static void bswap_prstatus(struct target_elf_prstatus *);
2045static void bswap_psinfo(struct target_elf_prpsinfo *);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002046
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002047static void bswap_prstatus(struct target_elf_prstatus *prstatus)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002048{
2049 prstatus->pr_info.si_signo = tswapl(prstatus->pr_info.si_signo);
2050 prstatus->pr_info.si_code = tswapl(prstatus->pr_info.si_code);
2051 prstatus->pr_info.si_errno = tswapl(prstatus->pr_info.si_errno);
2052 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
2053 prstatus->pr_sigpend = tswapl(prstatus->pr_sigpend);
2054 prstatus->pr_sighold = tswapl(prstatus->pr_sighold);
2055 prstatus->pr_pid = tswap32(prstatus->pr_pid);
2056 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
2057 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
2058 prstatus->pr_sid = tswap32(prstatus->pr_sid);
2059 /* cpu times are not filled, so we skip them */
2060 /* regs should be in correct format already */
2061 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
2062}
2063
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002064static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002065{
2066 psinfo->pr_flag = tswapl(psinfo->pr_flag);
2067 psinfo->pr_uid = tswap16(psinfo->pr_uid);
2068 psinfo->pr_gid = tswap16(psinfo->pr_gid);
2069 psinfo->pr_pid = tswap32(psinfo->pr_pid);
2070 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
2071 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
2072 psinfo->pr_sid = tswap32(psinfo->pr_sid);
2073}
2074#endif /* BSWAP_NEEDED */
2075
2076/*
2077 * Minimal support for linux memory regions. These are needed
2078 * when we are finding out what memory exactly belongs to
2079 * emulated process. No locks needed here, as long as
2080 * thread that received the signal is stopped.
2081 */
2082
2083static struct mm_struct *vma_init(void)
2084{
2085 struct mm_struct *mm;
2086
2087 if ((mm = qemu_malloc(sizeof (*mm))) == NULL)
2088 return (NULL);
2089
2090 mm->mm_count = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002091 QTAILQ_INIT(&mm->mm_mmap);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002092
2093 return (mm);
2094}
2095
2096static void vma_delete(struct mm_struct *mm)
2097{
2098 struct vm_area_struct *vma;
2099
2100 while ((vma = vma_first(mm)) != NULL) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002101 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002102 qemu_free(vma);
2103 }
2104 qemu_free(mm);
2105}
2106
2107static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
2108 abi_ulong end, abi_ulong flags)
2109{
2110 struct vm_area_struct *vma;
2111
2112 if ((vma = qemu_mallocz(sizeof (*vma))) == NULL)
2113 return (-1);
2114
2115 vma->vma_start = start;
2116 vma->vma_end = end;
2117 vma->vma_flags = flags;
2118
Blue Swirl72cf2d42009-09-12 07:36:22 +00002119 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002120 mm->mm_count++;
2121
2122 return (0);
2123}
2124
2125static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2126{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002127 return (QTAILQ_FIRST(&mm->mm_mmap));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002128}
2129
2130static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2131{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002132 return (QTAILQ_NEXT(vma, vma_link));
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002133}
2134
2135static int vma_get_mapping_count(const struct mm_struct *mm)
2136{
2137 return (mm->mm_count);
2138}
2139
2140/*
2141 * Calculate file (dump) size of given memory region.
2142 */
2143static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2144{
2145 /* if we cannot even read the first page, skip it */
2146 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2147 return (0);
2148
2149 /*
2150 * Usually we don't dump executable pages as they contain
2151 * non-writable code that debugger can read directly from
2152 * target library etc. However, thread stacks are marked
2153 * also executable so we read in first page of given region
2154 * and check whether it contains elf header. If there is
2155 * no elf header, we dump it.
2156 */
2157 if (vma->vma_flags & PROT_EXEC) {
2158 char page[TARGET_PAGE_SIZE];
2159
2160 copy_from_user(page, vma->vma_start, sizeof (page));
2161 if ((page[EI_MAG0] == ELFMAG0) &&
2162 (page[EI_MAG1] == ELFMAG1) &&
2163 (page[EI_MAG2] == ELFMAG2) &&
2164 (page[EI_MAG3] == ELFMAG3)) {
2165 /*
2166 * Mappings are possibly from ELF binary. Don't dump
2167 * them.
2168 */
2169 return (0);
2170 }
2171 }
2172
2173 return (vma->vma_end - vma->vma_start);
2174}
2175
2176static int vma_walker(void *priv, unsigned long start, unsigned long end,
2177 unsigned long flags)
2178{
2179 struct mm_struct *mm = (struct mm_struct *)priv;
2180
2181 /*
2182 * Don't dump anything that qemu has reserved for internal use.
2183 */
2184 if (flags & PAGE_RESERVED)
2185 return (0);
2186
2187 vma_add_mapping(mm, start, end, flags);
2188 return (0);
2189}
2190
2191static void fill_note(struct memelfnote *note, const char *name, int type,
2192 unsigned int sz, void *data)
2193{
2194 unsigned int namesz;
2195
2196 namesz = strlen(name) + 1;
2197 note->name = name;
2198 note->namesz = namesz;
2199 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2200 note->type = type;
2201 note->datasz = roundup(sz, sizeof (int32_t));;
2202 note->data = data;
2203
2204 /*
2205 * We calculate rounded up note size here as specified by
2206 * ELF document.
2207 */
2208 note->notesz = sizeof (struct elf_note) +
2209 note->namesz_rounded + note->datasz;
2210}
2211
2212static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2213 uint32_t flags)
2214{
2215 (void) memset(elf, 0, sizeof(*elf));
2216
2217 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2218 elf->e_ident[EI_CLASS] = ELF_CLASS;
2219 elf->e_ident[EI_DATA] = ELF_DATA;
2220 elf->e_ident[EI_VERSION] = EV_CURRENT;
2221 elf->e_ident[EI_OSABI] = ELF_OSABI;
2222
2223 elf->e_type = ET_CORE;
2224 elf->e_machine = machine;
2225 elf->e_version = EV_CURRENT;
2226 elf->e_phoff = sizeof(struct elfhdr);
2227 elf->e_flags = flags;
2228 elf->e_ehsize = sizeof(struct elfhdr);
2229 elf->e_phentsize = sizeof(struct elf_phdr);
2230 elf->e_phnum = segs;
2231
2232#ifdef BSWAP_NEEDED
2233 bswap_ehdr(elf);
2234#endif
2235}
2236
2237static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2238{
2239 phdr->p_type = PT_NOTE;
2240 phdr->p_offset = offset;
2241 phdr->p_vaddr = 0;
2242 phdr->p_paddr = 0;
2243 phdr->p_filesz = sz;
2244 phdr->p_memsz = 0;
2245 phdr->p_flags = 0;
2246 phdr->p_align = 0;
2247
2248#ifdef BSWAP_NEEDED
2249 bswap_phdr(phdr);
2250#endif
2251}
2252
2253static size_t note_size(const struct memelfnote *note)
2254{
2255 return (note->notesz);
2256}
2257
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002258static void fill_prstatus(struct target_elf_prstatus *prstatus,
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002259 const TaskState *ts, int signr)
2260{
2261 (void) memset(prstatus, 0, sizeof (*prstatus));
2262 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2263 prstatus->pr_pid = ts->ts_tid;
2264 prstatus->pr_ppid = getppid();
2265 prstatus->pr_pgrp = getpgrp();
2266 prstatus->pr_sid = getsid(0);
2267
2268#ifdef BSWAP_NEEDED
2269 bswap_prstatus(prstatus);
2270#endif
2271}
2272
Laurent Desnoguesa2547a12009-07-17 13:33:41 +01002273static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002274{
2275 char *filename, *base_filename;
2276 unsigned int i, len;
2277
2278 (void) memset(psinfo, 0, sizeof (*psinfo));
2279
2280 len = ts->info->arg_end - ts->info->arg_start;
2281 if (len >= ELF_PRARGSZ)
2282 len = ELF_PRARGSZ - 1;
2283 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2284 return -EFAULT;
2285 for (i = 0; i < len; i++)
2286 if (psinfo->pr_psargs[i] == 0)
2287 psinfo->pr_psargs[i] = ' ';
2288 psinfo->pr_psargs[len] = 0;
2289
2290 psinfo->pr_pid = getpid();
2291 psinfo->pr_ppid = getppid();
2292 psinfo->pr_pgrp = getpgrp();
2293 psinfo->pr_sid = getsid(0);
2294 psinfo->pr_uid = getuid();
2295 psinfo->pr_gid = getgid();
2296
2297 filename = strdup(ts->bprm->filename);
2298 base_filename = strdup(basename(filename));
2299 (void) strncpy(psinfo->pr_fname, base_filename,
2300 sizeof(psinfo->pr_fname));
2301 free(base_filename);
2302 free(filename);
2303
2304#ifdef BSWAP_NEEDED
2305 bswap_psinfo(psinfo);
2306#endif
2307 return (0);
2308}
2309
2310static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2311{
2312 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2313 elf_addr_t orig_auxv = auxv;
2314 abi_ulong val;
2315 void *ptr;
2316 int i, len;
2317
2318 /*
2319 * Auxiliary vector is stored in target process stack. It contains
2320 * {type, value} pairs that we need to dump into note. This is not
2321 * strictly necessary but we do it here for sake of completeness.
2322 */
2323
2324 /* find out lenght of the vector, AT_NULL is terminator */
2325 i = len = 0;
2326 do {
2327 get_user_ual(val, auxv);
2328 i += 2;
2329 auxv += 2 * sizeof (elf_addr_t);
2330 } while (val != AT_NULL);
2331 len = i * sizeof (elf_addr_t);
2332
2333 /* read in whole auxv vector and copy it to memelfnote */
2334 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2335 if (ptr != NULL) {
2336 fill_note(note, "CORE", NT_AUXV, len, ptr);
2337 unlock_user(ptr, auxv, len);
2338 }
2339}
2340
2341/*
2342 * Constructs name of coredump file. We have following convention
2343 * for the name:
2344 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2345 *
2346 * Returns 0 in case of success, -1 otherwise (errno is set).
2347 */
2348static int core_dump_filename(const TaskState *ts, char *buf,
2349 size_t bufsize)
2350{
2351 char timestamp[64];
2352 char *filename = NULL;
2353 char *base_filename = NULL;
2354 struct timeval tv;
2355 struct tm tm;
2356
2357 assert(bufsize >= PATH_MAX);
2358
2359 if (gettimeofday(&tv, NULL) < 0) {
2360 (void) fprintf(stderr, "unable to get current timestamp: %s",
2361 strerror(errno));
2362 return (-1);
2363 }
2364
2365 filename = strdup(ts->bprm->filename);
2366 base_filename = strdup(basename(filename));
2367 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2368 localtime_r(&tv.tv_sec, &tm));
2369 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2370 base_filename, timestamp, (int)getpid());
2371 free(base_filename);
2372 free(filename);
2373
2374 return (0);
2375}
2376
2377static int dump_write(int fd, const void *ptr, size_t size)
2378{
2379 const char *bufp = (const char *)ptr;
2380 ssize_t bytes_written, bytes_left;
2381 struct rlimit dumpsize;
2382 off_t pos;
2383
2384 bytes_written = 0;
2385 getrlimit(RLIMIT_CORE, &dumpsize);
2386 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2387 if (errno == ESPIPE) { /* not a seekable stream */
2388 bytes_left = size;
2389 } else {
2390 return pos;
2391 }
2392 } else {
2393 if (dumpsize.rlim_cur <= pos) {
2394 return -1;
2395 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2396 bytes_left = size;
2397 } else {
2398 size_t limit_left=dumpsize.rlim_cur - pos;
2399 bytes_left = limit_left >= size ? size : limit_left ;
2400 }
2401 }
2402
2403 /*
2404 * In normal conditions, single write(2) should do but
2405 * in case of socket etc. this mechanism is more portable.
2406 */
2407 do {
2408 bytes_written = write(fd, bufp, bytes_left);
2409 if (bytes_written < 0) {
2410 if (errno == EINTR)
2411 continue;
2412 return (-1);
2413 } else if (bytes_written == 0) { /* eof */
2414 return (-1);
2415 }
2416 bufp += bytes_written;
2417 bytes_left -= bytes_written;
2418 } while (bytes_left > 0);
2419
2420 return (0);
2421}
2422
2423static int write_note(struct memelfnote *men, int fd)
2424{
2425 struct elf_note en;
2426
2427 en.n_namesz = men->namesz;
2428 en.n_type = men->type;
2429 en.n_descsz = men->datasz;
2430
2431#ifdef BSWAP_NEEDED
2432 bswap_note(&en);
2433#endif
2434
2435 if (dump_write(fd, &en, sizeof(en)) != 0)
2436 return (-1);
2437 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2438 return (-1);
2439 if (dump_write(fd, men->data, men->datasz) != 0)
2440 return (-1);
2441
2442 return (0);
2443}
2444
2445static void fill_thread_info(struct elf_note_info *info, const CPUState *env)
2446{
2447 TaskState *ts = (TaskState *)env->opaque;
2448 struct elf_thread_status *ets;
2449
2450 ets = qemu_mallocz(sizeof (*ets));
2451 ets->num_notes = 1; /* only prstatus is dumped */
2452 fill_prstatus(&ets->prstatus, ts, 0);
2453 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2454 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2455 &ets->prstatus);
2456
Blue Swirl72cf2d42009-09-12 07:36:22 +00002457 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002458
2459 info->notes_size += note_size(&ets->notes[0]);
2460}
2461
2462static int fill_note_info(struct elf_note_info *info,
2463 long signr, const CPUState *env)
2464{
2465#define NUMNOTES 3
2466 CPUState *cpu = NULL;
2467 TaskState *ts = (TaskState *)env->opaque;
2468 int i;
2469
2470 (void) memset(info, 0, sizeof (*info));
2471
Blue Swirl72cf2d42009-09-12 07:36:22 +00002472 QTAILQ_INIT(&info->thread_list);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002473
2474 info->notes = qemu_mallocz(NUMNOTES * sizeof (struct memelfnote));
2475 if (info->notes == NULL)
2476 return (-ENOMEM);
2477 info->prstatus = qemu_mallocz(sizeof (*info->prstatus));
2478 if (info->prstatus == NULL)
2479 return (-ENOMEM);
2480 info->psinfo = qemu_mallocz(sizeof (*info->psinfo));
2481 if (info->prstatus == NULL)
2482 return (-ENOMEM);
2483
2484 /*
2485 * First fill in status (and registers) of current thread
2486 * including process info & aux vector.
2487 */
2488 fill_prstatus(info->prstatus, ts, signr);
2489 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2490 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2491 sizeof (*info->prstatus), info->prstatus);
2492 fill_psinfo(info->psinfo, ts);
2493 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2494 sizeof (*info->psinfo), info->psinfo);
2495 fill_auxv_note(&info->notes[2], ts);
2496 info->numnote = 3;
2497
2498 info->notes_size = 0;
2499 for (i = 0; i < info->numnote; i++)
2500 info->notes_size += note_size(&info->notes[i]);
2501
2502 /* read and fill status of all threads */
2503 cpu_list_lock();
2504 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2505 if (cpu == thread_env)
2506 continue;
2507 fill_thread_info(info, cpu);
2508 }
2509 cpu_list_unlock();
2510
2511 return (0);
2512}
2513
2514static void free_note_info(struct elf_note_info *info)
2515{
2516 struct elf_thread_status *ets;
2517
Blue Swirl72cf2d42009-09-12 07:36:22 +00002518 while (!QTAILQ_EMPTY(&info->thread_list)) {
2519 ets = QTAILQ_FIRST(&info->thread_list);
2520 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002521 qemu_free(ets);
2522 }
2523
2524 qemu_free(info->prstatus);
2525 qemu_free(info->psinfo);
2526 qemu_free(info->notes);
2527}
2528
2529static int write_note_info(struct elf_note_info *info, int fd)
2530{
2531 struct elf_thread_status *ets;
2532 int i, error = 0;
2533
2534 /* write prstatus, psinfo and auxv for current thread */
2535 for (i = 0; i < info->numnote; i++)
2536 if ((error = write_note(&info->notes[i], fd)) != 0)
2537 return (error);
2538
2539 /* write prstatus for each thread */
2540 for (ets = info->thread_list.tqh_first; ets != NULL;
2541 ets = ets->ets_link.tqe_next) {
2542 if ((error = write_note(&ets->notes[0], fd)) != 0)
2543 return (error);
2544 }
2545
2546 return (0);
2547}
2548
2549/*
2550 * Write out ELF coredump.
2551 *
2552 * See documentation of ELF object file format in:
2553 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2554 *
2555 * Coredump format in linux is following:
2556 *
2557 * 0 +----------------------+ \
2558 * | ELF header | ET_CORE |
2559 * +----------------------+ |
2560 * | ELF program headers | |--- headers
2561 * | - NOTE section | |
2562 * | - PT_LOAD sections | |
2563 * +----------------------+ /
2564 * | NOTEs: |
2565 * | - NT_PRSTATUS |
2566 * | - NT_PRSINFO |
2567 * | - NT_AUXV |
2568 * +----------------------+ <-- aligned to target page
2569 * | Process memory dump |
2570 * : :
2571 * . .
2572 * : :
2573 * | |
2574 * +----------------------+
2575 *
2576 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2577 * NT_PRSINFO -> struct elf_prpsinfo
2578 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2579 *
2580 * Format follows System V format as close as possible. Current
2581 * version limitations are as follows:
2582 * - no floating point registers are dumped
2583 *
2584 * Function returns 0 in case of success, negative errno otherwise.
2585 *
2586 * TODO: make this work also during runtime: it should be
2587 * possible to force coredump from running process and then
2588 * continue processing. For example qemu could set up SIGUSR2
2589 * handler (provided that target process haven't registered
2590 * handler for that) that does the dump when signal is received.
2591 */
2592static int elf_core_dump(int signr, const CPUState *env)
2593{
2594 const TaskState *ts = (const TaskState *)env->opaque;
2595 struct vm_area_struct *vma = NULL;
2596 char corefile[PATH_MAX];
2597 struct elf_note_info info;
2598 struct elfhdr elf;
2599 struct elf_phdr phdr;
2600 struct rlimit dumpsize;
2601 struct mm_struct *mm = NULL;
2602 off_t offset = 0, data_offset = 0;
2603 int segs = 0;
2604 int fd = -1;
2605
2606 errno = 0;
2607 getrlimit(RLIMIT_CORE, &dumpsize);
2608 if (dumpsize.rlim_cur == 0)
2609 return 0;
2610
2611 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2612 return (-errno);
2613
2614 if ((fd = open(corefile, O_WRONLY | O_CREAT,
2615 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2616 return (-errno);
2617
2618 /*
2619 * Walk through target process memory mappings and
2620 * set up structure containing this information. After
2621 * this point vma_xxx functions can be used.
2622 */
2623 if ((mm = vma_init()) == NULL)
2624 goto out;
2625
2626 walk_memory_regions(mm, vma_walker);
2627 segs = vma_get_mapping_count(mm);
2628
2629 /*
2630 * Construct valid coredump ELF header. We also
2631 * add one more segment for notes.
2632 */
2633 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
2634 if (dump_write(fd, &elf, sizeof (elf)) != 0)
2635 goto out;
2636
2637 /* fill in in-memory version of notes */
2638 if (fill_note_info(&info, signr, env) < 0)
2639 goto out;
2640
2641 offset += sizeof (elf); /* elf header */
2642 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
2643
2644 /* write out notes program header */
2645 fill_elf_note_phdr(&phdr, info.notes_size, offset);
2646
2647 offset += info.notes_size;
2648 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
2649 goto out;
2650
2651 /*
2652 * ELF specification wants data to start at page boundary so
2653 * we align it here.
2654 */
2655 offset = roundup(offset, ELF_EXEC_PAGESIZE);
2656
2657 /*
2658 * Write program headers for memory regions mapped in
2659 * the target process.
2660 */
2661 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2662 (void) memset(&phdr, 0, sizeof (phdr));
2663
2664 phdr.p_type = PT_LOAD;
2665 phdr.p_offset = offset;
2666 phdr.p_vaddr = vma->vma_start;
2667 phdr.p_paddr = 0;
2668 phdr.p_filesz = vma_dump_size(vma);
2669 offset += phdr.p_filesz;
2670 phdr.p_memsz = vma->vma_end - vma->vma_start;
2671 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
2672 if (vma->vma_flags & PROT_WRITE)
2673 phdr.p_flags |= PF_W;
2674 if (vma->vma_flags & PROT_EXEC)
2675 phdr.p_flags |= PF_X;
2676 phdr.p_align = ELF_EXEC_PAGESIZE;
2677
2678 dump_write(fd, &phdr, sizeof (phdr));
2679 }
2680
2681 /*
2682 * Next we write notes just after program headers. No
2683 * alignment needed here.
2684 */
2685 if (write_note_info(&info, fd) < 0)
2686 goto out;
2687
2688 /* align data to page boundary */
2689 data_offset = lseek(fd, 0, SEEK_CUR);
2690 data_offset = TARGET_PAGE_ALIGN(data_offset);
2691 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
2692 goto out;
2693
2694 /*
2695 * Finally we can dump process memory into corefile as well.
2696 */
2697 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2698 abi_ulong addr;
2699 abi_ulong end;
2700
2701 end = vma->vma_start + vma_dump_size(vma);
2702
2703 for (addr = vma->vma_start; addr < end;
2704 addr += TARGET_PAGE_SIZE) {
2705 char page[TARGET_PAGE_SIZE];
2706 int error;
2707
2708 /*
2709 * Read in page from target process memory and
2710 * write it to coredump file.
2711 */
2712 error = copy_from_user(page, addr, sizeof (page));
2713 if (error != 0) {
Aurelien Jarno49995e12009-12-19 20:28:23 +01002714 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002715 addr);
2716 errno = -error;
2717 goto out;
2718 }
2719 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
2720 goto out;
2721 }
2722 }
2723
2724out:
2725 free_note_info(&info);
2726 if (mm != NULL)
2727 vma_delete(mm);
2728 (void) close(fd);
2729
2730 if (errno != 0)
2731 return (-errno);
2732 return (0);
2733}
2734
2735#endif /* USE_ELF_CORE_DUMP */
2736
bellard31e31b82003-02-18 22:55:36 +00002737static int load_aout_interp(void * exptr, int interp_fd)
2738{
2739 printf("a.out interpreter not yet supported\n");
2740 return(0);
2741}
2742
pbrooke5fe0c52006-06-11 13:32:59 +00002743void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
2744{
2745 init_thread(regs, infop);
2746}