]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/elfload.c
linux-user: fix ELF_USE_CORE_DUMP/USE_ELF_CORE_DUMP confusion
[mirror_qemu.git] / linux-user / elfload.c
CommitLineData
31e31b8a 1/* This is the Linux kernel elf-loading code, ported into user space */
edf8e2af
MW
2#include <sys/time.h>
3#include <sys/param.h>
31e31b8a
FB
4
5#include <stdio.h>
6#include <sys/types.h>
7#include <fcntl.h>
31e31b8a
FB
8#include <errno.h>
9#include <unistd.h>
10#include <sys/mman.h>
edf8e2af 11#include <sys/resource.h>
31e31b8a
FB
12#include <stdlib.h>
13#include <string.h>
edf8e2af 14#include <time.h>
31e31b8a 15
3ef693a0 16#include "qemu.h"
689f936f 17#include "disas.h"
31e31b8a 18
e58ffeb3 19#ifdef _ARCH_PPC64
a6cc84f4 20#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
edf8e2af
MW
28#define ELF_OSABI ELFOSABI_SYSV
29
cb33da57
BS
30/* 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
83fb7adf
FB
90/* 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
30ac07d4
FB
100#ifdef TARGET_I386
101
15338fd7
FB
102#define ELF_PLATFORM get_elf_platform()
103
104static const char *get_elf_platform(void)
105{
106 static char elf_platform[] = "i386";
d5975363 107 int family = (thread_env->cpuid_version >> 8) & 0xff;
15338fd7
FB
108 if (family > 6)
109 family = 6;
110 if (family >= 3)
111 elf_platform[1] = '0' + family;
112 return elf_platform;
113}
114
115#define ELF_HWCAP get_elf_hwcap()
116
117static uint32_t get_elf_hwcap(void)
118{
d5975363 119 return thread_env->cpuid_features;
15338fd7
FB
120}
121
84409ddb
JM
122#ifdef TARGET_X86_64
123#define ELF_START_MMAP 0x2aaaaab000ULL
124#define elf_check_arch(x) ( ((x) == ELF_ARCH) )
125
126#define ELF_CLASS ELFCLASS64
127#define ELF_DATA ELFDATA2LSB
128#define ELF_ARCH EM_X86_64
129
130static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
131{
132 regs->rax = 0;
133 regs->rsp = infop->start_stack;
134 regs->rip = infop->entry;
135}
136
c227f099
AL
137typedef target_ulong target_elf_greg_t;
138typedef uint32_t target_uid_t;
139typedef uint32_t target_gid_t;
140typedef int32_t target_pid_t;
9edc5d79
MW
141
142#define ELF_NREG 27
c227f099 143typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
9edc5d79
MW
144
145/*
146 * Note that ELF_NREG should be 29 as there should be place for
147 * TRAPNO and ERR "registers" as well but linux doesn't dump
148 * those.
149 *
150 * See linux kernel: arch/x86/include/asm/elf.h
151 */
c227f099 152static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
9edc5d79
MW
153{
154 (*regs)[0] = env->regs[15];
155 (*regs)[1] = env->regs[14];
156 (*regs)[2] = env->regs[13];
157 (*regs)[3] = env->regs[12];
158 (*regs)[4] = env->regs[R_EBP];
159 (*regs)[5] = env->regs[R_EBX];
160 (*regs)[6] = env->regs[11];
161 (*regs)[7] = env->regs[10];
162 (*regs)[8] = env->regs[9];
163 (*regs)[9] = env->regs[8];
164 (*regs)[10] = env->regs[R_EAX];
165 (*regs)[11] = env->regs[R_ECX];
166 (*regs)[12] = env->regs[R_EDX];
167 (*regs)[13] = env->regs[R_ESI];
168 (*regs)[14] = env->regs[R_EDI];
169 (*regs)[15] = env->regs[R_EAX]; /* XXX */
170 (*regs)[16] = env->eip;
171 (*regs)[17] = env->segs[R_CS].selector & 0xffff;
172 (*regs)[18] = env->eflags;
173 (*regs)[19] = env->regs[R_ESP];
174 (*regs)[20] = env->segs[R_SS].selector & 0xffff;
175 (*regs)[21] = env->segs[R_FS].selector & 0xffff;
176 (*regs)[22] = env->segs[R_GS].selector & 0xffff;
177 (*regs)[23] = env->segs[R_DS].selector & 0xffff;
178 (*regs)[24] = env->segs[R_ES].selector & 0xffff;
179 (*regs)[25] = env->segs[R_FS].selector & 0xffff;
180 (*regs)[26] = env->segs[R_GS].selector & 0xffff;
181}
182
84409ddb
JM
183#else
184
30ac07d4
FB
185#define ELF_START_MMAP 0x80000000
186
30ac07d4
FB
187/*
188 * This is used to ensure we don't load something for the wrong architecture.
189 */
190#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
191
192/*
193 * These are used to set parameters in the core dumps.
194 */
195#define ELF_CLASS ELFCLASS32
196#define ELF_DATA ELFDATA2LSB
197#define ELF_ARCH EM_386
198
b346ff46
FB
199static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
200{
201 regs->esp = infop->start_stack;
202 regs->eip = infop->entry;
e5fe0c52
PB
203
204 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
205 starts %edx contains a pointer to a function which might be
206 registered using `atexit'. This provides a mean for the
207 dynamic linker to call DT_FINI functions for shared libraries
208 that have been loaded before the code runs.
209
210 A value of 0 tells we have no such handler. */
211 regs->edx = 0;
b346ff46 212}
9edc5d79 213
c227f099
AL
214typedef target_ulong target_elf_greg_t;
215typedef uint16_t target_uid_t;
216typedef uint16_t target_gid_t;
217typedef int32_t target_pid_t;
9edc5d79
MW
218
219#define ELF_NREG 17
c227f099 220typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
9edc5d79
MW
221
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 */
c227f099 229static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
9edc5d79
MW
230{
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}
84409ddb 249#endif
b346ff46 250
9edc5d79 251#define USE_ELF_CORE_DUMP
b346ff46
FB
252#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
b346ff46
FB
270static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
271{
992f48a0 272 abi_long stack = infop->start_stack;
b346ff46
FB
273 memset(regs, 0, sizeof(*regs));
274 regs->ARM_cpsr = 0x10;
0240ded8
PB
275 if (infop->entry & 1)
276 regs->ARM_cpsr |= CPSR_T;
277 regs->ARM_pc = infop->entry & 0xfffffffe;
b346ff46 278 regs->ARM_sp = infop->start_stack;
2f619698
FB
279 /* 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 */
a1516e92 282 /* XXX: it seems that r0 is zeroed after ! */
e5fe0c52
PB
283 regs->ARM_r0 = 0;
284 /* For uClinux PIC binaries. */
863cf0b7 285 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
e5fe0c52 286 regs->ARM_r10 = infop->start_data;
b346ff46
FB
287}
288
c227f099
AL
289typedef uint32_t target_elf_greg_t;
290typedef uint16_t target_uid_t;
291typedef uint16_t target_gid_t;
292typedef int32_t target_pid_t;
edf8e2af
MW
293
294#define ELF_NREG 18
c227f099 295typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
edf8e2af 296
c227f099 297static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUState *env)
edf8e2af
MW
298{
299 (*regs)[0] = env->regs[0];
300 (*regs)[1] = env->regs[1];
301 (*regs)[2] = env->regs[2];
302 (*regs)[3] = env->regs[3];
303 (*regs)[4] = env->regs[4];
304 (*regs)[5] = env->regs[5];
305 (*regs)[6] = env->regs[6];
306 (*regs)[7] = env->regs[7];
307 (*regs)[8] = env->regs[8];
308 (*regs)[9] = env->regs[9];
309 (*regs)[10] = env->regs[10];
310 (*regs)[11] = env->regs[11];
311 (*regs)[12] = env->regs[12];
312 (*regs)[13] = env->regs[13];
313 (*regs)[14] = env->regs[14];
314 (*regs)[15] = env->regs[15];
315
316 (*regs)[16] = cpsr_read((CPUState *)env);
317 (*regs)[17] = env->regs[0]; /* XXX */
318}
319
30ac07d4
FB
320#define USE_ELF_CORE_DUMP
321#define ELF_EXEC_PAGESIZE 4096
322
afce2927
FB
323enum
324{
325 ARM_HWCAP_ARM_SWP = 1 << 0,
326 ARM_HWCAP_ARM_HALF = 1 << 1,
327 ARM_HWCAP_ARM_THUMB = 1 << 2,
328 ARM_HWCAP_ARM_26BIT = 1 << 3,
329 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
330 ARM_HWCAP_ARM_FPA = 1 << 5,
331 ARM_HWCAP_ARM_VFP = 1 << 6,
332 ARM_HWCAP_ARM_EDSP = 1 << 7,
cf6de34a
RV
333 ARM_HWCAP_ARM_JAVA = 1 << 8,
334 ARM_HWCAP_ARM_IWMMXT = 1 << 9,
335 ARM_HWCAP_ARM_THUMBEE = 1 << 10,
336 ARM_HWCAP_ARM_NEON = 1 << 11,
337 ARM_HWCAP_ARM_VFPv3 = 1 << 12,
338 ARM_HWCAP_ARM_VFPv3D16 = 1 << 13,
afce2927
FB
339};
340
15338fd7 341#define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF \
afce2927 342 | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT \
cf6de34a
RV
343 | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP \
344 | ARM_HWCAP_ARM_NEON | ARM_HWCAP_ARM_VFPv3 )
afce2927 345
30ac07d4
FB
346#endif
347
853d6f7a 348#ifdef TARGET_SPARC
a315a145 349#ifdef TARGET_SPARC64
853d6f7a
FB
350
351#define ELF_START_MMAP 0x80000000
352
992f48a0 353#ifndef TARGET_ABI32
cb33da57 354#define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
992f48a0
BS
355#else
356#define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
357#endif
853d6f7a 358
a315a145
FB
359#define ELF_CLASS ELFCLASS64
360#define ELF_DATA ELFDATA2MSB
5ef54116
FB
361#define ELF_ARCH EM_SPARCV9
362
363#define STACK_BIAS 2047
a315a145 364
a315a145
FB
365static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
366{
992f48a0 367#ifndef TARGET_ABI32
a315a145 368 regs->tstate = 0;
992f48a0 369#endif
a315a145
FB
370 regs->pc = infop->entry;
371 regs->npc = regs->pc + 4;
372 regs->y = 0;
992f48a0
BS
373#ifdef TARGET_ABI32
374 regs->u_regs[14] = infop->start_stack - 16 * 4;
375#else
cb33da57
BS
376 if (personality(infop->personality) == PER_LINUX32)
377 regs->u_regs[14] = infop->start_stack - 16 * 4;
378 else
379 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
992f48a0 380#endif
a315a145
FB
381}
382
383#else
384#define ELF_START_MMAP 0x80000000
385
386#define elf_check_arch(x) ( (x) == EM_SPARC )
387
853d6f7a
FB
388#define ELF_CLASS ELFCLASS32
389#define ELF_DATA ELFDATA2MSB
390#define ELF_ARCH EM_SPARC
391
853d6f7a
FB
392static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
393{
f5155289
FB
394 regs->psr = 0;
395 regs->pc = infop->entry;
396 regs->npc = regs->pc + 4;
397 regs->y = 0;
398 regs->u_regs[14] = infop->start_stack - 16 * 4;
853d6f7a
FB
399}
400
a315a145 401#endif
853d6f7a
FB
402#endif
403
67867308
FB
404#ifdef TARGET_PPC
405
406#define ELF_START_MMAP 0x80000000
407
e85e7c6e 408#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
84409ddb
JM
409
410#define elf_check_arch(x) ( (x) == EM_PPC64 )
411
412#define ELF_CLASS ELFCLASS64
413
414#else
415
67867308
FB
416#define elf_check_arch(x) ( (x) == EM_PPC )
417
418#define ELF_CLASS ELFCLASS32
84409ddb
JM
419
420#endif
421
67867308
FB
422#ifdef TARGET_WORDS_BIGENDIAN
423#define ELF_DATA ELFDATA2MSB
424#else
425#define ELF_DATA ELFDATA2LSB
426#endif
427#define ELF_ARCH EM_PPC
428
df84e4f3
NF
429/* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
430 See arch/powerpc/include/asm/cputable.h. */
431enum {
3efa9a67 432 QEMU_PPC_FEATURE_32 = 0x80000000,
433 QEMU_PPC_FEATURE_64 = 0x40000000,
434 QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
435 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
436 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
437 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
438 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
439 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
440 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
441 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
442 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
443 QEMU_PPC_FEATURE_NO_TB = 0x00100000,
444 QEMU_PPC_FEATURE_POWER4 = 0x00080000,
445 QEMU_PPC_FEATURE_POWER5 = 0x00040000,
446 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
447 QEMU_PPC_FEATURE_CELL = 0x00010000,
448 QEMU_PPC_FEATURE_BOOKE = 0x00008000,
449 QEMU_PPC_FEATURE_SMT = 0x00004000,
450 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
451 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
452 QEMU_PPC_FEATURE_PA6T = 0x00000800,
453 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
454 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
455 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
456 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
457 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
458
459 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
460 QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
df84e4f3
NF
461};
462
463#define ELF_HWCAP get_elf_hwcap()
464
465static uint32_t get_elf_hwcap(void)
466{
467 CPUState *e = thread_env;
468 uint32_t features = 0;
469
470 /* We don't have to be terribly complete here; the high points are
471 Altivec/FP/SPE support. Anything else is just a bonus. */
472#define GET_FEATURE(flag, feature) \
473 do {if (e->insns_flags & flag) features |= feature; } while(0)
3efa9a67 474 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
475 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
476 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
477 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
478 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
479 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
480 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
481 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
df84e4f3
NF
482#undef GET_FEATURE
483
484 return features;
485}
486
f5155289
FB
487/*
488 * We need to put in some extra aux table entries to tell glibc what
489 * the cache block size is, so it can use the dcbz instruction safely.
490 */
491#define AT_DCACHEBSIZE 19
492#define AT_ICACHEBSIZE 20
493#define AT_UCACHEBSIZE 21
494/* A special ignored type value for PPC, for glibc compatibility. */
495#define AT_IGNOREPPC 22
496/*
497 * The requirements here are:
498 * - keep the final alignment of sp (sp & 0xf)
499 * - make sure the 32-bit value at the first 16 byte aligned position of
500 * AUXV is greater than 16 for glibc compatibility.
501 * AT_IGNOREPPC is used for that.
502 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
503 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
504 */
0bccf03d 505#define DLINFO_ARCH_ITEMS 5
f5155289
FB
506#define ARCH_DLINFO \
507do { \
0bccf03d
FB
508 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
509 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
510 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
f5155289
FB
511 /* \
512 * Now handle glibc compatibility. \
513 */ \
0bccf03d
FB
514 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
515 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
f5155289
FB
516 } while (0)
517
67867308
FB
518static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
519{
992f48a0
BS
520 abi_ulong pos = infop->start_stack;
521 abi_ulong tmp;
e85e7c6e 522#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
992f48a0 523 abi_ulong entry, toc;
84409ddb 524#endif
e5fe0c52 525
67867308 526 _regs->gpr[1] = infop->start_stack;
e85e7c6e 527#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
84409ddb
JM
528 entry = ldq_raw(infop->entry) + infop->load_addr;
529 toc = ldq_raw(infop->entry + 8) + infop->load_addr;
530 _regs->gpr[2] = toc;
531 infop->entry = entry;
532#endif
67867308 533 _regs->nip = infop->entry;
e5fe0c52
PB
534 /* Note that isn't exactly what regular kernel does
535 * but this is what the ABI wants and is needed to allow
536 * execution of PPC BSD programs.
537 */
2f619698
FB
538 /* FIXME - what to for failure of get_user()? */
539 get_user_ual(_regs->gpr[3], pos);
992f48a0 540 pos += sizeof(abi_ulong);
e5fe0c52 541 _regs->gpr[4] = pos;
992f48a0 542 for (tmp = 1; tmp != 0; pos += sizeof(abi_ulong))
e5fe0c52
PB
543 tmp = ldl(pos);
544 _regs->gpr[5] = pos;
67867308
FB
545}
546
67867308
FB
547#define ELF_EXEC_PAGESIZE 4096
548
549#endif
550
048f6b4d
FB
551#ifdef TARGET_MIPS
552
553#define ELF_START_MMAP 0x80000000
554
555#define elf_check_arch(x) ( (x) == EM_MIPS )
556
388bb21a
TS
557#ifdef TARGET_MIPS64
558#define ELF_CLASS ELFCLASS64
559#else
048f6b4d 560#define ELF_CLASS ELFCLASS32
388bb21a 561#endif
048f6b4d
FB
562#ifdef TARGET_WORDS_BIGENDIAN
563#define ELF_DATA ELFDATA2MSB
564#else
565#define ELF_DATA ELFDATA2LSB
566#endif
567#define ELF_ARCH EM_MIPS
568
048f6b4d
FB
569static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
570{
623a930e 571 regs->cp0_status = 2 << CP0St_KSU;
048f6b4d
FB
572 regs->cp0_epc = infop->entry;
573 regs->regs[29] = infop->start_stack;
574}
575
388bb21a
TS
576#define ELF_EXEC_PAGESIZE 4096
577
048f6b4d
FB
578#endif /* TARGET_MIPS */
579
b779e29e
EI
580#ifdef TARGET_MICROBLAZE
581
582#define ELF_START_MMAP 0x80000000
583
584#define elf_check_arch(x) ( (x) == EM_XILINX_MICROBLAZE )
585
586#define ELF_CLASS ELFCLASS32
587#define ELF_DATA ELFDATA2MSB
588#define ELF_ARCH EM_MIPS
589
590static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
591{
592 regs->pc = infop->entry;
593 regs->r1 = infop->start_stack;
594
595}
596
b779e29e
EI
597#define ELF_EXEC_PAGESIZE 4096
598
599#endif /* TARGET_MICROBLAZE */
600
fdf9b3e8
FB
601#ifdef TARGET_SH4
602
603#define ELF_START_MMAP 0x80000000
604
605#define elf_check_arch(x) ( (x) == EM_SH )
606
607#define ELF_CLASS ELFCLASS32
608#define ELF_DATA ELFDATA2LSB
609#define ELF_ARCH EM_SH
610
fdf9b3e8
FB
611static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
612{
613 /* Check other registers XXXXX */
614 regs->pc = infop->entry;
072ae847 615 regs->regs[15] = infop->start_stack;
fdf9b3e8
FB
616}
617
fdf9b3e8
FB
618#define ELF_EXEC_PAGESIZE 4096
619
620#endif
621
48733d19
TS
622#ifdef TARGET_CRIS
623
624#define ELF_START_MMAP 0x80000000
625
626#define elf_check_arch(x) ( (x) == EM_CRIS )
627
628#define ELF_CLASS ELFCLASS32
629#define ELF_DATA ELFDATA2LSB
630#define ELF_ARCH EM_CRIS
631
632static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
633{
634 regs->erp = infop->entry;
635}
636
48733d19
TS
637#define ELF_EXEC_PAGESIZE 8192
638
639#endif
640
e6e5906b
PB
641#ifdef TARGET_M68K
642
643#define ELF_START_MMAP 0x80000000
644
645#define elf_check_arch(x) ( (x) == EM_68K )
646
647#define ELF_CLASS ELFCLASS32
648#define ELF_DATA ELFDATA2MSB
649#define ELF_ARCH EM_68K
650
651/* ??? Does this need to do anything?
652#define ELF_PLAT_INIT(_r) */
653
654static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
655{
656 regs->usp = infop->start_stack;
657 regs->sr = 0;
658 regs->pc = infop->entry;
659}
660
e6e5906b
PB
661#define ELF_EXEC_PAGESIZE 8192
662
663#endif
664
7a3148a9
JM
665#ifdef TARGET_ALPHA
666
667#define ELF_START_MMAP (0x30000000000ULL)
668
669#define elf_check_arch(x) ( (x) == ELF_ARCH )
670
671#define ELF_CLASS ELFCLASS64
672#define ELF_DATA ELFDATA2MSB
673#define ELF_ARCH EM_ALPHA
674
675static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
676{
677 regs->pc = infop->entry;
678 regs->ps = 8;
679 regs->usp = infop->start_stack;
7a3148a9
JM
680}
681
7a3148a9
JM
682#define ELF_EXEC_PAGESIZE 8192
683
684#endif /* TARGET_ALPHA */
685
15338fd7
FB
686#ifndef ELF_PLATFORM
687#define ELF_PLATFORM (NULL)
688#endif
689
690#ifndef ELF_HWCAP
691#define ELF_HWCAP 0
692#endif
693
992f48a0 694#ifdef TARGET_ABI32
cb33da57 695#undef ELF_CLASS
992f48a0 696#define ELF_CLASS ELFCLASS32
cb33da57
BS
697#undef bswaptls
698#define bswaptls(ptr) bswap32s(ptr)
699#endif
700
31e31b8a 701#include "elf.h"
09bfb054 702
09bfb054
FB
703struct exec
704{
705 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
706 unsigned int a_text; /* length of text, in bytes */
707 unsigned int a_data; /* length of data, in bytes */
708 unsigned int a_bss; /* length of uninitialized data area, in bytes */
709 unsigned int a_syms; /* length of symbol table data in file, in bytes */
710 unsigned int a_entry; /* start address */
711 unsigned int a_trsize; /* length of relocation info for text, in bytes */
712 unsigned int a_drsize; /* length of relocation info for data, in bytes */
713};
714
715
716#define N_MAGIC(exec) ((exec).a_info & 0xffff)
717#define OMAGIC 0407
718#define NMAGIC 0410
719#define ZMAGIC 0413
720#define QMAGIC 0314
721
09bfb054
FB
722/* max code+data+bss space allocated to elf interpreter */
723#define INTERP_MAP_SIZE (32 * 1024 * 1024)
724
725/* max code+data+bss+brk space allocated to ET_DYN executables */
726#define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
727
31e31b8a 728/* Necessary parameters */
54936004
FB
729#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
730#define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
731#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
31e31b8a
FB
732
733#define INTERPRETER_NONE 0
734#define INTERPRETER_AOUT 1
735#define INTERPRETER_ELF 2
736
15338fd7 737#define DLINFO_ITEMS 12
31e31b8a 738
09bfb054
FB
739static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
740{
741 memcpy(to, from, n);
742}
d691f669 743
31e31b8a
FB
744static int load_aout_interp(void * exptr, int interp_fd);
745
746#ifdef BSWAP_NEEDED
92a31b1f 747static void bswap_ehdr(struct elfhdr *ehdr)
31e31b8a
FB
748{
749 bswap16s(&ehdr->e_type); /* Object file type */
750 bswap16s(&ehdr->e_machine); /* Architecture */
751 bswap32s(&ehdr->e_version); /* Object file version */
92a31b1f
FB
752 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
753 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
754 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
31e31b8a
FB
755 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
756 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
757 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
758 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
759 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
760 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
761 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
762}
763
92a31b1f 764static void bswap_phdr(struct elf_phdr *phdr)
31e31b8a
FB
765{
766 bswap32s(&phdr->p_type); /* Segment type */
92a31b1f
FB
767 bswaptls(&phdr->p_offset); /* Segment file offset */
768 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
769 bswaptls(&phdr->p_paddr); /* Segment physical address */
770 bswaptls(&phdr->p_filesz); /* Segment size in file */
771 bswaptls(&phdr->p_memsz); /* Segment size in memory */
31e31b8a 772 bswap32s(&phdr->p_flags); /* Segment flags */
92a31b1f 773 bswaptls(&phdr->p_align); /* Segment alignment */
31e31b8a 774}
689f936f 775
92a31b1f 776static void bswap_shdr(struct elf_shdr *shdr)
689f936f
FB
777{
778 bswap32s(&shdr->sh_name);
779 bswap32s(&shdr->sh_type);
92a31b1f
FB
780 bswaptls(&shdr->sh_flags);
781 bswaptls(&shdr->sh_addr);
782 bswaptls(&shdr->sh_offset);
783 bswaptls(&shdr->sh_size);
689f936f
FB
784 bswap32s(&shdr->sh_link);
785 bswap32s(&shdr->sh_info);
92a31b1f
FB
786 bswaptls(&shdr->sh_addralign);
787 bswaptls(&shdr->sh_entsize);
689f936f
FB
788}
789
7a3148a9 790static void bswap_sym(struct elf_sym *sym)
689f936f
FB
791{
792 bswap32s(&sym->st_name);
7a3148a9
JM
793 bswaptls(&sym->st_value);
794 bswaptls(&sym->st_size);
689f936f
FB
795 bswap16s(&sym->st_shndx);
796}
31e31b8a
FB
797#endif
798
edf8e2af
MW
799#ifdef USE_ELF_CORE_DUMP
800static int elf_core_dump(int, const CPUState *);
801
802#ifdef BSWAP_NEEDED
803static void bswap_note(struct elf_note *en)
804{
9fdca5aa 805 bswap32s(&en->n_namesz);
806 bswap32s(&en->n_descsz);
807 bswap32s(&en->n_type);
edf8e2af
MW
808}
809#endif /* BSWAP_NEEDED */
810
811#endif /* USE_ELF_CORE_DUMP */
812
31e31b8a 813/*
e5fe0c52 814 * 'copy_elf_strings()' copies argument/envelope strings from user
31e31b8a
FB
815 * memory to free pages in kernel mem. These are in a format ready
816 * to be put directly into the top of new user memory.
817 *
818 */
992f48a0
BS
819static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
820 abi_ulong p)
31e31b8a
FB
821{
822 char *tmp, *tmp1, *pag = NULL;
823 int len, offset = 0;
824
825 if (!p) {
826 return 0; /* bullet-proofing */
827 }
828 while (argc-- > 0) {
edf779ff
FB
829 tmp = argv[argc];
830 if (!tmp) {
31e31b8a
FB
831 fprintf(stderr, "VFS: argc is wrong");
832 exit(-1);
833 }
edf779ff
FB
834 tmp1 = tmp;
835 while (*tmp++);
31e31b8a
FB
836 len = tmp - tmp1;
837 if (p < len) { /* this shouldn't happen - 128kB */
838 return 0;
839 }
840 while (len) {
841 --p; --tmp; --len;
842 if (--offset < 0) {
54936004 843 offset = p % TARGET_PAGE_SIZE;
53a5960a 844 pag = (char *)page[p/TARGET_PAGE_SIZE];
44a91cae 845 if (!pag) {
53a5960a 846 pag = (char *)malloc(TARGET_PAGE_SIZE);
4118a970 847 memset(pag, 0, TARGET_PAGE_SIZE);
53a5960a 848 page[p/TARGET_PAGE_SIZE] = pag;
44a91cae
FB
849 if (!pag)
850 return 0;
31e31b8a
FB
851 }
852 }
853 if (len == 0 || offset == 0) {
edf779ff 854 *(pag + offset) = *tmp;
31e31b8a
FB
855 }
856 else {
857 int bytes_to_copy = (len > offset) ? offset : len;
858 tmp -= bytes_to_copy;
859 p -= bytes_to_copy;
860 offset -= bytes_to_copy;
861 len -= bytes_to_copy;
862 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
863 }
864 }
865 }
866 return p;
867}
868
992f48a0
BS
869static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
870 struct image_info *info)
53a5960a 871{
992f48a0 872 abi_ulong stack_base, size, error;
31e31b8a 873 int i;
31e31b8a 874
09bfb054
FB
875 /* Create enough stack to hold everything. If we don't use
876 * it for args, we'll use it for something else...
877 */
878 size = x86_stack_size;
54936004
FB
879 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
880 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
5fafdf24 881 error = target_mmap(0,
83fb7adf 882 size + qemu_host_page_size,
54936004
FB
883 PROT_READ | PROT_WRITE,
884 MAP_PRIVATE | MAP_ANONYMOUS,
885 -1, 0);
09bfb054
FB
886 if (error == -1) {
887 perror("stk mmap");
888 exit(-1);
889 }
890 /* we reserve one extra page at the top of the stack as guard */
83fb7adf 891 target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
31e31b8a 892
54936004 893 stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
31e31b8a 894 p += stack_base;
09bfb054 895
31e31b8a
FB
896 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
897 if (bprm->page[i]) {
898 info->rss++;
579a97f7 899 /* FIXME - check return value of memcpy_to_target() for failure */
53a5960a
PB
900 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
901 free(bprm->page[i]);
31e31b8a 902 }
53a5960a 903 stack_base += TARGET_PAGE_SIZE;
31e31b8a
FB
904 }
905 return p;
906}
907
992f48a0 908static void set_brk(abi_ulong start, abi_ulong end)
31e31b8a
FB
909{
910 /* page-align the start and end addresses... */
54936004
FB
911 start = HOST_PAGE_ALIGN(start);
912 end = HOST_PAGE_ALIGN(end);
31e31b8a
FB
913 if (end <= start)
914 return;
54936004
FB
915 if(target_mmap(start, end - start,
916 PROT_READ | PROT_WRITE | PROT_EXEC,
917 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
31e31b8a
FB
918 perror("cannot mmap brk");
919 exit(-1);
920 }
921}
922
923
853d6f7a
FB
924/* We need to explicitly zero any fractional pages after the data
925 section (i.e. bss). This would contain the junk from the file that
926 should not be in memory. */
992f48a0 927static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
31e31b8a 928{
992f48a0 929 abi_ulong nbyte;
31e31b8a 930
768a4a36
TS
931 if (elf_bss >= last_bss)
932 return;
933
853d6f7a
FB
934 /* XXX: this is really a hack : if the real host page size is
935 smaller than the target page size, some pages after the end
936 of the file may not be mapped. A better fix would be to
937 patch target_mmap(), but it is more complicated as the file
938 size must be known */
83fb7adf 939 if (qemu_real_host_page_size < qemu_host_page_size) {
992f48a0 940 abi_ulong end_addr, end_addr1;
5fafdf24 941 end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
83fb7adf 942 ~(qemu_real_host_page_size - 1);
853d6f7a
FB
943 end_addr = HOST_PAGE_ALIGN(elf_bss);
944 if (end_addr1 < end_addr) {
863cf0b7 945 mmap((void *)g2h(end_addr1), end_addr - end_addr1,
853d6f7a
FB
946 PROT_READ|PROT_WRITE|PROT_EXEC,
947 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
948 }
949 }
950
83fb7adf 951 nbyte = elf_bss & (qemu_host_page_size-1);
31e31b8a 952 if (nbyte) {
83fb7adf 953 nbyte = qemu_host_page_size - nbyte;
31e31b8a 954 do {
2f619698
FB
955 /* FIXME - what to do if put_user() fails? */
956 put_user_u8(0, elf_bss);
53a5960a 957 elf_bss++;
31e31b8a
FB
958 } while (--nbyte);
959 }
960}
961
53a5960a 962
992f48a0
BS
963static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
964 struct elfhdr * exec,
965 abi_ulong load_addr,
966 abi_ulong load_bias,
967 abi_ulong interp_load_addr, int ibcs,
968 struct image_info *info)
31e31b8a 969{
992f48a0 970 abi_ulong sp;
53a5960a 971 int size;
992f48a0 972 abi_ulong u_platform;
15338fd7 973 const char *k_platform;
863cf0b7 974 const int n = sizeof(elf_addr_t);
edf779ff 975
53a5960a
PB
976 sp = p;
977 u_platform = 0;
15338fd7
FB
978 k_platform = ELF_PLATFORM;
979 if (k_platform) {
980 size_t len = strlen(k_platform) + 1;
53a5960a
PB
981 sp -= (len + n - 1) & ~(n - 1);
982 u_platform = sp;
579a97f7 983 /* FIXME - check return value of memcpy_to_target() for failure */
53a5960a 984 memcpy_to_target(sp, k_platform, len);
15338fd7 985 }
53a5960a
PB
986 /*
987 * Force 16 byte _final_ alignment here for generality.
988 */
992f48a0 989 sp = sp &~ (abi_ulong)15;
53a5960a 990 size = (DLINFO_ITEMS + 1) * 2;
15338fd7 991 if (k_platform)
53a5960a 992 size += 2;
f5155289 993#ifdef DLINFO_ARCH_ITEMS
53a5960a 994 size += DLINFO_ARCH_ITEMS * 2;
f5155289 995#endif
53a5960a
PB
996 size += envc + argc + 2;
997 size += (!ibcs ? 3 : 1); /* argc itself */
998 size *= n;
999 if (size & 15)
1000 sp -= 16 - (size & 15);
3b46e624 1001
863cf0b7
JM
1002 /* This is correct because Linux defines
1003 * elf_addr_t as Elf32_Off / Elf64_Off
1004 */
2f619698
FB
1005#define NEW_AUX_ENT(id, val) do { \
1006 sp -= n; put_user_ual(val, sp); \
1007 sp -= n; put_user_ual(id, sp); \
53a5960a 1008 } while(0)
2f619698 1009
0bccf03d
FB
1010 NEW_AUX_ENT (AT_NULL, 0);
1011
1012 /* There must be exactly DLINFO_ITEMS entries here. */
992f48a0
BS
1013 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
1014 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
1015 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
1016 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
1017 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
1018 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
0bccf03d 1019 NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
992f48a0
BS
1020 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
1021 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
1022 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
1023 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
1024 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
a07c67df 1025 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
15338fd7 1026 if (k_platform)
53a5960a 1027 NEW_AUX_ENT(AT_PLATFORM, u_platform);
f5155289 1028#ifdef ARCH_DLINFO
5fafdf24 1029 /*
f5155289
FB
1030 * ARCH_DLINFO must come last so platform specific code can enforce
1031 * special alignment requirements on the AUXV if necessary (eg. PPC).
1032 */
1033 ARCH_DLINFO;
1034#endif
1035#undef NEW_AUX_ENT
1036
edf8e2af
MW
1037 info->saved_auxv = sp;
1038
e5fe0c52 1039 sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
31e31b8a
FB
1040 return sp;
1041}
1042
1043
992f48a0
BS
1044static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
1045 int interpreter_fd,
1046 abi_ulong *interp_load_addr)
31e31b8a
FB
1047{
1048 struct elf_phdr *elf_phdata = NULL;
1049 struct elf_phdr *eppnt;
992f48a0 1050 abi_ulong load_addr = 0;
31e31b8a
FB
1051 int load_addr_set = 0;
1052 int retval;
992f48a0
BS
1053 abi_ulong last_bss, elf_bss;
1054 abi_ulong error;
31e31b8a 1055 int i;
5fafdf24 1056
31e31b8a
FB
1057 elf_bss = 0;
1058 last_bss = 0;
1059 error = 0;
1060
644c433c
FB
1061#ifdef BSWAP_NEEDED
1062 bswap_ehdr(interp_elf_ex);
1063#endif
31e31b8a 1064 /* First of all, some simple consistency checks */
5fafdf24
TS
1065 if ((interp_elf_ex->e_type != ET_EXEC &&
1066 interp_elf_ex->e_type != ET_DYN) ||
31e31b8a 1067 !elf_check_arch(interp_elf_ex->e_machine)) {
992f48a0 1068 return ~((abi_ulong)0UL);
31e31b8a 1069 }
5fafdf24 1070
644c433c 1071
31e31b8a 1072 /* Now read in all of the header information */
5fafdf24 1073
54936004 1074 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
992f48a0 1075 return ~(abi_ulong)0UL;
5fafdf24
TS
1076
1077 elf_phdata = (struct elf_phdr *)
31e31b8a
FB
1078 malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1079
1080 if (!elf_phdata)
992f48a0 1081 return ~((abi_ulong)0UL);
5fafdf24 1082
31e31b8a
FB
1083 /*
1084 * If the size of this structure has changed, then punt, since
1085 * we will be doing the wrong thing.
1086 */
09bfb054 1087 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
31e31b8a 1088 free(elf_phdata);
992f48a0 1089 return ~((abi_ulong)0UL);
09bfb054 1090 }
31e31b8a
FB
1091
1092 retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
1093 if(retval >= 0) {
1094 retval = read(interpreter_fd,
1095 (char *) elf_phdata,
1096 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
1097 }
31e31b8a
FB
1098 if (retval < 0) {
1099 perror("load_elf_interp");
1100 exit(-1);
1101 free (elf_phdata);
1102 return retval;
1103 }
1104#ifdef BSWAP_NEEDED
1105 eppnt = elf_phdata;
1106 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
1107 bswap_phdr(eppnt);
1108 }
1109#endif
09bfb054
FB
1110
1111 if (interp_elf_ex->e_type == ET_DYN) {
e91c8a77 1112 /* in order to avoid hardcoding the interpreter load
09bfb054 1113 address in qemu, we allocate a big enough memory zone */
54936004 1114 error = target_mmap(0, INTERP_MAP_SIZE,
5fafdf24 1115 PROT_NONE, MAP_PRIVATE | MAP_ANON,
54936004 1116 -1, 0);
09bfb054
FB
1117 if (error == -1) {
1118 perror("mmap");
1119 exit(-1);
1120 }
1121 load_addr = error;
1122 load_addr_set = 1;
1123 }
1124
31e31b8a
FB
1125 eppnt = elf_phdata;
1126 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
1127 if (eppnt->p_type == PT_LOAD) {
1128 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
1129 int elf_prot = 0;
992f48a0
BS
1130 abi_ulong vaddr = 0;
1131 abi_ulong k;
31e31b8a
FB
1132
1133 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
1134 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1135 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1136 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
1137 elf_type |= MAP_FIXED;
1138 vaddr = eppnt->p_vaddr;
1139 }
54936004
FB
1140 error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
1141 eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
31e31b8a
FB
1142 elf_prot,
1143 elf_type,
1144 interpreter_fd,
54936004 1145 eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
3b46e624 1146
e89f07d3 1147 if (error == -1) {
31e31b8a
FB
1148 /* Real error */
1149 close(interpreter_fd);
1150 free(elf_phdata);
992f48a0 1151 return ~((abi_ulong)0UL);
31e31b8a
FB
1152 }
1153
1154 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
1155 load_addr = error;
1156 load_addr_set = 1;
1157 }
1158
1159 /*
1160 * Find the end of the file mapping for this phdr, and keep
1161 * track of the largest address we see for this.
1162 */
1163 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
1164 if (k > elf_bss) elf_bss = k;
1165
1166 /*
1167 * Do the same thing for the memory mapping - between
1168 * elf_bss and last_bss is the bss section.
1169 */
1170 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
1171 if (k > last_bss) last_bss = k;
1172 }
5fafdf24 1173
31e31b8a
FB
1174 /* Now use mmap to map the library into memory. */
1175
1176 close(interpreter_fd);
1177
1178 /*
1179 * Now fill out the bss section. First pad the last page up
1180 * to the page boundary, and then perform a mmap to make sure
1181 * that there are zeromapped pages up to and including the last
1182 * bss page.
1183 */
768a4a36 1184 padzero(elf_bss, last_bss);
83fb7adf 1185 elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
31e31b8a
FB
1186
1187 /* Map the last of the bss segment */
1188 if (last_bss > elf_bss) {
54936004
FB
1189 target_mmap(elf_bss, last_bss-elf_bss,
1190 PROT_READ|PROT_WRITE|PROT_EXEC,
1191 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
31e31b8a
FB
1192 }
1193 free(elf_phdata);
1194
1195 *interp_load_addr = load_addr;
992f48a0 1196 return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
31e31b8a
FB
1197}
1198
49918a75
PB
1199static int symfind(const void *s0, const void *s1)
1200{
1201 struct elf_sym *key = (struct elf_sym *)s0;
1202 struct elf_sym *sym = (struct elf_sym *)s1;
1203 int result = 0;
1204 if (key->st_value < sym->st_value) {
1205 result = -1;
ec822001 1206 } else if (key->st_value >= sym->st_value + sym->st_size) {
49918a75
PB
1207 result = 1;
1208 }
1209 return result;
1210}
1211
1212static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
1213{
1214#if ELF_CLASS == ELFCLASS32
1215 struct elf_sym *syms = s->disas_symtab.elf32;
1216#else
1217 struct elf_sym *syms = s->disas_symtab.elf64;
1218#endif
1219
1220 // binary search
1221 struct elf_sym key;
1222 struct elf_sym *sym;
1223
1224 key.st_value = orig_addr;
1225
1226 sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), symfind);
7cba04f6 1227 if (sym != NULL) {
49918a75
PB
1228 return s->disas_strtab + sym->st_name;
1229 }
1230
1231 return "";
1232}
1233
1234/* FIXME: This should use elf_ops.h */
1235static int symcmp(const void *s0, const void *s1)
1236{
1237 struct elf_sym *sym0 = (struct elf_sym *)s0;
1238 struct elf_sym *sym1 = (struct elf_sym *)s1;
1239 return (sym0->st_value < sym1->st_value)
1240 ? -1
1241 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
1242}
1243
689f936f
FB
1244/* Best attempt to load symbols from this ELF object. */
1245static void load_symbols(struct elfhdr *hdr, int fd)
1246{
49918a75 1247 unsigned int i, nsyms;
689f936f
FB
1248 struct elf_shdr sechdr, symtab, strtab;
1249 char *strings;
e80cfcfc 1250 struct syminfo *s;
49918a75 1251 struct elf_sym *syms;
689f936f
FB
1252
1253 lseek(fd, hdr->e_shoff, SEEK_SET);
1254 for (i = 0; i < hdr->e_shnum; i++) {
49918a75
PB
1255 if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
1256 return;
689f936f 1257#ifdef BSWAP_NEEDED
49918a75 1258 bswap_shdr(&sechdr);
689f936f 1259#endif
49918a75
PB
1260 if (sechdr.sh_type == SHT_SYMTAB) {
1261 symtab = sechdr;
1262 lseek(fd, hdr->e_shoff
1263 + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
1264 if (read(fd, &strtab, sizeof(strtab))
1265 != sizeof(strtab))
1266 return;
689f936f 1267#ifdef BSWAP_NEEDED
49918a75 1268 bswap_shdr(&strtab);
689f936f 1269#endif
49918a75
PB
1270 goto found;
1271 }
689f936f
FB
1272 }
1273 return; /* Shouldn't happen... */
1274
1275 found:
1276 /* Now know where the strtab and symtab are. Snarf them. */
e80cfcfc 1277 s = malloc(sizeof(*s));
49918a75
PB
1278 syms = malloc(symtab.sh_size);
1279 if (!syms)
1280 return;
e80cfcfc 1281 s->disas_strtab = strings = malloc(strtab.sh_size);
49918a75
PB
1282 if (!s->disas_strtab)
1283 return;
5fafdf24 1284
689f936f 1285 lseek(fd, symtab.sh_offset, SEEK_SET);
49918a75
PB
1286 if (read(fd, syms, symtab.sh_size) != symtab.sh_size)
1287 return;
1288
1289 nsyms = symtab.sh_size / sizeof(struct elf_sym);
31e31b8a 1290
49918a75
PB
1291 i = 0;
1292 while (i < nsyms) {
689f936f 1293#ifdef BSWAP_NEEDED
49918a75 1294 bswap_sym(syms + i);
689f936f 1295#endif
49918a75
PB
1296 // Throw away entries which we do not need.
1297 if (syms[i].st_shndx == SHN_UNDEF ||
1298 syms[i].st_shndx >= SHN_LORESERVE ||
1299 ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
1300 nsyms--;
1301 if (i < nsyms) {
1302 syms[i] = syms[nsyms];
1303 }
1304 continue;
1305 }
1306#if defined(TARGET_ARM) || defined (TARGET_MIPS)
1307 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
1308 syms[i].st_value &= ~(target_ulong)1;
0774bed1 1309#endif
49918a75 1310 i++;
0774bed1 1311 }
49918a75
PB
1312 syms = realloc(syms, nsyms * sizeof(*syms));
1313
1314 qsort(syms, nsyms, sizeof(*syms), symcmp);
689f936f
FB
1315
1316 lseek(fd, strtab.sh_offset, SEEK_SET);
1317 if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
49918a75
PB
1318 return;
1319 s->disas_num_syms = nsyms;
1320#if ELF_CLASS == ELFCLASS32
1321 s->disas_symtab.elf32 = syms;
ca20cf32 1322 s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
49918a75
PB
1323#else
1324 s->disas_symtab.elf64 = syms;
ca20cf32 1325 s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
49918a75 1326#endif
e80cfcfc
FB
1327 s->next = syminfos;
1328 syminfos = s;
689f936f 1329}
31e31b8a 1330
e5fe0c52
PB
1331int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
1332 struct image_info * info)
31e31b8a
FB
1333{
1334 struct elfhdr elf_ex;
1335 struct elfhdr interp_elf_ex;
1336 struct exec interp_ex;
1337 int interpreter_fd = -1; /* avoid warning */
992f48a0 1338 abi_ulong load_addr, load_bias;
31e31b8a
FB
1339 int load_addr_set = 0;
1340 unsigned int interpreter_type = INTERPRETER_NONE;
1341 unsigned char ibcs2_interpreter;
1342 int i;
992f48a0 1343 abi_ulong mapped_addr;
31e31b8a
FB
1344 struct elf_phdr * elf_ppnt;
1345 struct elf_phdr *elf_phdata;
992f48a0 1346 abi_ulong elf_bss, k, elf_brk;
31e31b8a
FB
1347 int retval;
1348 char * elf_interpreter;
992f48a0 1349 abi_ulong elf_entry, interp_load_addr = 0;
31e31b8a 1350 int status;
992f48a0
BS
1351 abi_ulong start_code, end_code, start_data, end_data;
1352 abi_ulong reloc_func_desc = 0;
1353 abi_ulong elf_stack;
31e31b8a
FB
1354 char passed_fileno[6];
1355
1356 ibcs2_interpreter = 0;
1357 status = 0;
1358 load_addr = 0;
09bfb054 1359 load_bias = 0;
31e31b8a
FB
1360 elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
1361#ifdef BSWAP_NEEDED
1362 bswap_ehdr(&elf_ex);
1363#endif
1364
31e31b8a
FB
1365 /* First of all, some simple consistency checks */
1366 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
1367 (! elf_check_arch(elf_ex.e_machine))) {
1368 return -ENOEXEC;
1369 }
1370
e5fe0c52
PB
1371 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
1372 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
1373 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
1374 if (!bprm->p) {
1375 retval = -E2BIG;
1376 }
1377
31e31b8a 1378 /* Now read in all of the header information */
31e31b8a
FB
1379 elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
1380 if (elf_phdata == NULL) {
1381 return -ENOMEM;
1382 }
1383
1384 retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
1385 if(retval > 0) {
5fafdf24 1386 retval = read(bprm->fd, (char *) elf_phdata,
31e31b8a
FB
1387 elf_ex.e_phentsize * elf_ex.e_phnum);
1388 }
1389
1390 if (retval < 0) {
1391 perror("load_elf_binary");
1392 exit(-1);
1393 free (elf_phdata);
1394 return -errno;
1395 }
1396
b17780d5
FB
1397#ifdef BSWAP_NEEDED
1398 elf_ppnt = elf_phdata;
1399 for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
1400 bswap_phdr(elf_ppnt);
1401 }
1402#endif
31e31b8a
FB
1403 elf_ppnt = elf_phdata;
1404
1405 elf_bss = 0;
1406 elf_brk = 0;
1407
1408
992f48a0 1409 elf_stack = ~((abi_ulong)0UL);
31e31b8a 1410 elf_interpreter = NULL;
992f48a0 1411 start_code = ~((abi_ulong)0UL);
31e31b8a 1412 end_code = 0;
863cf0b7 1413 start_data = 0;
31e31b8a 1414 end_data = 0;
98448f58 1415 interp_ex.a_info = 0;
31e31b8a
FB
1416
1417 for(i=0;i < elf_ex.e_phnum; i++) {
1418 if (elf_ppnt->p_type == PT_INTERP) {
1419 if ( elf_interpreter != NULL )
1420 {
1421 free (elf_phdata);
1422 free(elf_interpreter);
1423 close(bprm->fd);
1424 return -EINVAL;
1425 }
1426
1427 /* This is the program interpreter used for
1428 * shared libraries - for now assume that this
1429 * is an a.out format binary
1430 */
1431
32ce6337 1432 elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
31e31b8a
FB
1433
1434 if (elf_interpreter == NULL) {
1435 free (elf_phdata);
1436 close(bprm->fd);
1437 return -ENOMEM;
1438 }
1439
31e31b8a
FB
1440 retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
1441 if(retval >= 0) {
32ce6337 1442 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
31e31b8a
FB
1443 }
1444 if(retval < 0) {
1445 perror("load_elf_binary2");
1446 exit(-1);
5fafdf24 1447 }
31e31b8a
FB
1448
1449 /* If the program interpreter is one of these two,
1450 then assume an iBCS2 image. Otherwise assume
1451 a native linux image. */
1452
1453 /* JRP - Need to add X86 lib dir stuff here... */
1454
1455 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
1456 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
1457 ibcs2_interpreter = 1;
1458 }
1459
1460#if 0
3bc0bdca 1461 printf("Using ELF interpreter %s\n", path(elf_interpreter));
31e31b8a
FB
1462#endif
1463 if (retval >= 0) {
32ce6337 1464 retval = open(path(elf_interpreter), O_RDONLY);
31e31b8a
FB
1465 if(retval >= 0) {
1466 interpreter_fd = retval;
1467 }
1468 else {
1469 perror(elf_interpreter);
1470 exit(-1);
1471 /* retval = -errno; */
1472 }
1473 }
1474
1475 if (retval >= 0) {
1476 retval = lseek(interpreter_fd, 0, SEEK_SET);
1477 if(retval >= 0) {
1478 retval = read(interpreter_fd,bprm->buf,128);
1479 }
1480 }
1481 if (retval >= 0) {
1482 interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
6ece4df6 1483 interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */
31e31b8a
FB
1484 }
1485 if (retval < 0) {
1486 perror("load_elf_binary3");
1487 exit(-1);
1488 free (elf_phdata);
1489 free(elf_interpreter);
1490 close(bprm->fd);
1491 return retval;
1492 }
1493 }
1494 elf_ppnt++;
1495 }
1496
1497 /* Some simple consistency checks for the interpreter */
1498 if (elf_interpreter){
1499 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
1500
1501 /* Now figure out which format our binary is */
1502 if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
1503 (N_MAGIC(interp_ex) != QMAGIC)) {
1504 interpreter_type = INTERPRETER_ELF;
1505 }
1506
1507 if (interp_elf_ex.e_ident[0] != 0x7f ||
b55266b5 1508 strncmp((char *)&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
31e31b8a
FB
1509 interpreter_type &= ~INTERPRETER_ELF;
1510 }
1511
1512 if (!interpreter_type) {
1513 free(elf_interpreter);
1514 free(elf_phdata);
1515 close(bprm->fd);
1516 return -ELIBBAD;
1517 }
1518 }
1519
1520 /* OK, we are done with that, now set up the arg stuff,
1521 and then start this sucker up */
1522
e5fe0c52 1523 {
31e31b8a
FB
1524 char * passed_p;
1525
1526 if (interpreter_type == INTERPRETER_AOUT) {
eba2af63 1527 snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
31e31b8a
FB
1528 passed_p = passed_fileno;
1529
1530 if (elf_interpreter) {
e5fe0c52 1531 bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
31e31b8a
FB
1532 bprm->argc++;
1533 }
1534 }
1535 if (!bprm->p) {
1536 if (elf_interpreter) {
1537 free(elf_interpreter);
1538 }
1539 free (elf_phdata);
1540 close(bprm->fd);
1541 return -E2BIG;
1542 }
1543 }
1544
1545 /* OK, This is the point of no return */
1546 info->end_data = 0;
1547 info->end_code = 0;
992f48a0 1548 info->start_mmap = (abi_ulong)ELF_START_MMAP;
31e31b8a 1549 info->mmap = 0;
992f48a0 1550 elf_entry = (abi_ulong) elf_ex.e_entry;
31e31b8a 1551
379f6698
PB
1552#if defined(CONFIG_USE_GUEST_BASE)
1553 /*
1554 * In case where user has not explicitly set the guest_base, we
1555 * probe here that should we set it automatically.
1556 */
1557 if (!have_guest_base) {
1558 /*
1559 * Go through ELF program header table and find out whether
1560 * any of the segments drop below our current mmap_min_addr and
1561 * in that case set guest_base to corresponding address.
1562 */
1563 for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum;
1564 i++, elf_ppnt++) {
1565 if (elf_ppnt->p_type != PT_LOAD)
1566 continue;
1567 if (HOST_PAGE_ALIGN(elf_ppnt->p_vaddr) < mmap_min_addr) {
1568 guest_base = HOST_PAGE_ALIGN(mmap_min_addr);
1569 break;
1570 }
1571 }
1572 }
1573#endif /* CONFIG_USE_GUEST_BASE */
1574
31e31b8a
FB
1575 /* Do this so that we can load the interpreter, if need be. We will
1576 change some of these later */
1577 info->rss = 0;
1578 bprm->p = setup_arg_pages(bprm->p, bprm, info);
1579 info->start_stack = bprm->p;
1580
1581 /* Now we do a little grungy work by mmaping the ELF image into
1582 * the correct location in memory. At this point, we assume that
1583 * the image should be loaded at fixed address, not at a variable
1584 * address.
1585 */
1586
31e31b8a 1587 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
09bfb054
FB
1588 int elf_prot = 0;
1589 int elf_flags = 0;
992f48a0 1590 abi_ulong error;
3b46e624 1591
09bfb054
FB
1592 if (elf_ppnt->p_type != PT_LOAD)
1593 continue;
3b46e624 1594
09bfb054
FB
1595 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1596 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1597 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1598 elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
1599 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
1600 elf_flags |= MAP_FIXED;
1601 } else if (elf_ex.e_type == ET_DYN) {
1602 /* Try and get dynamic programs out of the way of the default mmap
1603 base, as well as whatever program they might try to exec. This
1604 is because the brk will follow the loader, and is not movable. */
1605 /* NOTE: for qemu, we do a big mmap to get enough space
e91c8a77 1606 without hardcoding any address */
54936004 1607 error = target_mmap(0, ET_DYN_MAP_SIZE,
5fafdf24 1608 PROT_NONE, MAP_PRIVATE | MAP_ANON,
54936004 1609 -1, 0);
09bfb054
FB
1610 if (error == -1) {
1611 perror("mmap");
1612 exit(-1);
1613 }
54936004 1614 load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
09bfb054 1615 }
3b46e624 1616
54936004
FB
1617 error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1618 (elf_ppnt->p_filesz +
1619 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1620 elf_prot,
1621 (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1622 bprm->fd,
5fafdf24 1623 (elf_ppnt->p_offset -
54936004 1624 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
09bfb054
FB
1625 if (error == -1) {
1626 perror("mmap");
1627 exit(-1);
1628 }
31e31b8a
FB
1629
1630#ifdef LOW_ELF_STACK
54936004
FB
1631 if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1632 elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
31e31b8a 1633#endif
3b46e624 1634
09bfb054
FB
1635 if (!load_addr_set) {
1636 load_addr_set = 1;
1637 load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1638 if (elf_ex.e_type == ET_DYN) {
1639 load_bias += error -
54936004 1640 TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
09bfb054 1641 load_addr += load_bias;
84409ddb 1642 reloc_func_desc = load_bias;
09bfb054
FB
1643 }
1644 }
1645 k = elf_ppnt->p_vaddr;
5fafdf24 1646 if (k < start_code)
09bfb054 1647 start_code = k;
863cf0b7
JM
1648 if (start_data < k)
1649 start_data = k;
09bfb054 1650 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
5fafdf24 1651 if (k > elf_bss)
09bfb054
FB
1652 elf_bss = k;
1653 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
1654 end_code = k;
5fafdf24 1655 if (end_data < k)
09bfb054
FB
1656 end_data = k;
1657 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1658 if (k > elf_brk) elf_brk = k;
31e31b8a
FB
1659 }
1660
09bfb054
FB
1661 elf_entry += load_bias;
1662 elf_bss += load_bias;
1663 elf_brk += load_bias;
1664 start_code += load_bias;
1665 end_code += load_bias;
863cf0b7 1666 start_data += load_bias;
09bfb054
FB
1667 end_data += load_bias;
1668
31e31b8a
FB
1669 if (elf_interpreter) {
1670 if (interpreter_type & 1) {
1671 elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1672 }
1673 else if (interpreter_type & 2) {
1674 elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1675 &interp_load_addr);
1676 }
84409ddb 1677 reloc_func_desc = interp_load_addr;
31e31b8a
FB
1678
1679 close(interpreter_fd);
1680 free(elf_interpreter);
1681
992f48a0 1682 if (elf_entry == ~((abi_ulong)0UL)) {
31e31b8a
FB
1683 printf("Unable to load interpreter\n");
1684 free(elf_phdata);
1685 exit(-1);
1686 return 0;
1687 }
1688 }
1689
1690 free(elf_phdata);
1691
93fcfe39 1692 if (qemu_log_enabled())
689f936f
FB
1693 load_symbols(&elf_ex, bprm->fd);
1694
31e31b8a
FB
1695 if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
1696 info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
1697
1698#ifdef LOW_ELF_STACK
1699 info->start_stack = bprm->p = elf_stack - 4;
1700#endif
53a5960a 1701 bprm->p = create_elf_tables(bprm->p,
31e31b8a
FB
1702 bprm->argc,
1703 bprm->envc,
a1516e92 1704 &elf_ex,
09bfb054 1705 load_addr, load_bias,
31e31b8a
FB
1706 interp_load_addr,
1707 (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1708 info);
92a343da 1709 info->load_addr = reloc_func_desc;
31e31b8a
FB
1710 info->start_brk = info->brk = elf_brk;
1711 info->end_code = end_code;
1712 info->start_code = start_code;
863cf0b7 1713 info->start_data = start_data;
31e31b8a
FB
1714 info->end_data = end_data;
1715 info->start_stack = bprm->p;
1716
1717 /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1718 sections */
1719 set_brk(elf_bss, elf_brk);
1720
768a4a36 1721 padzero(elf_bss, elf_brk);
31e31b8a
FB
1722
1723#if 0
1724 printf("(start_brk) %x\n" , info->start_brk);
1725 printf("(end_code) %x\n" , info->end_code);
1726 printf("(start_code) %x\n" , info->start_code);
1727 printf("(end_data) %x\n" , info->end_data);
1728 printf("(start_stack) %x\n" , info->start_stack);
1729 printf("(brk) %x\n" , info->brk);
1730#endif
1731
1732 if ( info->personality == PER_SVR4 )
1733 {
1734 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1735 and some applications "depend" upon this behavior.
1736 Since we do not have the power to recompile these, we
1737 emulate the SVr4 behavior. Sigh. */
83fb7adf 1738 mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
54936004 1739 MAP_FIXED | MAP_PRIVATE, -1, 0);
31e31b8a
FB
1740 }
1741
31e31b8a
FB
1742 info->entry = elf_entry;
1743
edf8e2af
MW
1744#ifdef USE_ELF_CORE_DUMP
1745 bprm->core_dump = &elf_core_dump;
1746#endif
1747
31e31b8a
FB
1748 return 0;
1749}
1750
edf8e2af
MW
1751#ifdef USE_ELF_CORE_DUMP
1752
1753/*
1754 * Definitions to generate Intel SVR4-like core files.
a2547a13 1755 * These mostly have the same names as the SVR4 types with "target_elf_"
edf8e2af
MW
1756 * tacked on the front to prevent clashes with linux definitions,
1757 * and the typedef forms have been avoided. This is mostly like
1758 * the SVR4 structure, but more Linuxy, with things that Linux does
1759 * not support and which gdb doesn't really use excluded.
1760 *
1761 * Fields we don't dump (their contents is zero) in linux-user qemu
1762 * are marked with XXX.
1763 *
1764 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
1765 *
1766 * Porting ELF coredump for target is (quite) simple process. First you
dd0a3651 1767 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
edf8e2af
MW
1768 * the target resides):
1769 *
1770 * #define USE_ELF_CORE_DUMP
1771 *
1772 * Next you define type of register set used for dumping. ELF specification
1773 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
1774 *
c227f099 1775 * typedef <target_regtype> target_elf_greg_t;
edf8e2af 1776 * #define ELF_NREG <number of registers>
c227f099 1777 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
edf8e2af
MW
1778 *
1779 * Then define following types to match target types. Actual types can
1780 * be found from linux kernel (arch/<ARCH>/include/asm/posix_types.h):
1781 *
c227f099
AL
1782 * typedef <target_uid_type> target_uid_t;
1783 * typedef <target_gid_type> target_gid_t;
1784 * typedef <target_pid_type> target_pid_t;
edf8e2af
MW
1785 *
1786 * Last step is to implement target specific function that copies registers
1787 * from given cpu into just specified register set. Prototype is:
1788 *
c227f099 1789 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
a2547a13 1790 * const CPUState *env);
edf8e2af
MW
1791 *
1792 * Parameters:
1793 * regs - copy register values into here (allocated and zeroed by caller)
1794 * env - copy registers from here
1795 *
1796 * Example for ARM target is provided in this file.
1797 */
1798
1799/* An ELF note in memory */
1800struct memelfnote {
1801 const char *name;
1802 size_t namesz;
1803 size_t namesz_rounded;
1804 int type;
1805 size_t datasz;
1806 void *data;
1807 size_t notesz;
1808};
1809
a2547a13 1810struct target_elf_siginfo {
edf8e2af
MW
1811 int si_signo; /* signal number */
1812 int si_code; /* extra code */
1813 int si_errno; /* errno */
1814};
1815
a2547a13
LD
1816struct target_elf_prstatus {
1817 struct target_elf_siginfo pr_info; /* Info associated with signal */
edf8e2af
MW
1818 short pr_cursig; /* Current signal */
1819 target_ulong pr_sigpend; /* XXX */
1820 target_ulong pr_sighold; /* XXX */
c227f099
AL
1821 target_pid_t pr_pid;
1822 target_pid_t pr_ppid;
1823 target_pid_t pr_pgrp;
1824 target_pid_t pr_sid;
edf8e2af
MW
1825 struct target_timeval pr_utime; /* XXX User time */
1826 struct target_timeval pr_stime; /* XXX System time */
1827 struct target_timeval pr_cutime; /* XXX Cumulative user time */
1828 struct target_timeval pr_cstime; /* XXX Cumulative system time */
c227f099 1829 target_elf_gregset_t pr_reg; /* GP registers */
edf8e2af
MW
1830 int pr_fpvalid; /* XXX */
1831};
1832
1833#define ELF_PRARGSZ (80) /* Number of chars for args */
1834
a2547a13 1835struct target_elf_prpsinfo {
edf8e2af
MW
1836 char pr_state; /* numeric process state */
1837 char pr_sname; /* char for pr_state */
1838 char pr_zomb; /* zombie */
1839 char pr_nice; /* nice val */
1840 target_ulong pr_flag; /* flags */
c227f099
AL
1841 target_uid_t pr_uid;
1842 target_gid_t pr_gid;
1843 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
edf8e2af
MW
1844 /* Lots missing */
1845 char pr_fname[16]; /* filename of executable */
1846 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
1847};
1848
1849/* Here is the structure in which status of each thread is captured. */
1850struct elf_thread_status {
72cf2d4f 1851 QTAILQ_ENTRY(elf_thread_status) ets_link;
a2547a13 1852 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
edf8e2af
MW
1853#if 0
1854 elf_fpregset_t fpu; /* NT_PRFPREG */
1855 struct task_struct *thread;
1856 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
1857#endif
1858 struct memelfnote notes[1];
1859 int num_notes;
1860};
1861
1862struct elf_note_info {
1863 struct memelfnote *notes;
a2547a13
LD
1864 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
1865 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
edf8e2af 1866
72cf2d4f 1867 QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
edf8e2af
MW
1868#if 0
1869 /*
1870 * Current version of ELF coredump doesn't support
1871 * dumping fp regs etc.
1872 */
1873 elf_fpregset_t *fpu;
1874 elf_fpxregset_t *xfpu;
1875 int thread_status_size;
1876#endif
1877 int notes_size;
1878 int numnote;
1879};
1880
1881struct vm_area_struct {
1882 abi_ulong vma_start; /* start vaddr of memory region */
1883 abi_ulong vma_end; /* end vaddr of memory region */
1884 abi_ulong vma_flags; /* protection etc. flags for the region */
72cf2d4f 1885 QTAILQ_ENTRY(vm_area_struct) vma_link;
edf8e2af
MW
1886};
1887
1888struct mm_struct {
72cf2d4f 1889 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
edf8e2af
MW
1890 int mm_count; /* number of mappings */
1891};
1892
1893static struct mm_struct *vma_init(void);
1894static void vma_delete(struct mm_struct *);
1895static int vma_add_mapping(struct mm_struct *, abi_ulong,
1896 abi_ulong, abi_ulong);
1897static int vma_get_mapping_count(const struct mm_struct *);
1898static struct vm_area_struct *vma_first(const struct mm_struct *);
1899static struct vm_area_struct *vma_next(struct vm_area_struct *);
1900static abi_ulong vma_dump_size(const struct vm_area_struct *);
1901static int vma_walker(void *priv, unsigned long start, unsigned long end,
1902 unsigned long flags);
1903
1904static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
1905static void fill_note(struct memelfnote *, const char *, int,
1906 unsigned int, void *);
a2547a13
LD
1907static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
1908static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
edf8e2af
MW
1909static void fill_auxv_note(struct memelfnote *, const TaskState *);
1910static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
1911static size_t note_size(const struct memelfnote *);
1912static void free_note_info(struct elf_note_info *);
1913static int fill_note_info(struct elf_note_info *, long, const CPUState *);
1914static void fill_thread_info(struct elf_note_info *, const CPUState *);
1915static int core_dump_filename(const TaskState *, char *, size_t);
1916
1917static int dump_write(int, const void *, size_t);
1918static int write_note(struct memelfnote *, int);
1919static int write_note_info(struct elf_note_info *, int);
1920
1921#ifdef BSWAP_NEEDED
a2547a13
LD
1922static void bswap_prstatus(struct target_elf_prstatus *);
1923static void bswap_psinfo(struct target_elf_prpsinfo *);
edf8e2af 1924
a2547a13 1925static void bswap_prstatus(struct target_elf_prstatus *prstatus)
edf8e2af
MW
1926{
1927 prstatus->pr_info.si_signo = tswapl(prstatus->pr_info.si_signo);
1928 prstatus->pr_info.si_code = tswapl(prstatus->pr_info.si_code);
1929 prstatus->pr_info.si_errno = tswapl(prstatus->pr_info.si_errno);
1930 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
1931 prstatus->pr_sigpend = tswapl(prstatus->pr_sigpend);
1932 prstatus->pr_sighold = tswapl(prstatus->pr_sighold);
1933 prstatus->pr_pid = tswap32(prstatus->pr_pid);
1934 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
1935 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
1936 prstatus->pr_sid = tswap32(prstatus->pr_sid);
1937 /* cpu times are not filled, so we skip them */
1938 /* regs should be in correct format already */
1939 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
1940}
1941
a2547a13 1942static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
edf8e2af
MW
1943{
1944 psinfo->pr_flag = tswapl(psinfo->pr_flag);
1945 psinfo->pr_uid = tswap16(psinfo->pr_uid);
1946 psinfo->pr_gid = tswap16(psinfo->pr_gid);
1947 psinfo->pr_pid = tswap32(psinfo->pr_pid);
1948 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
1949 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
1950 psinfo->pr_sid = tswap32(psinfo->pr_sid);
1951}
1952#endif /* BSWAP_NEEDED */
1953
1954/*
1955 * Minimal support for linux memory regions. These are needed
1956 * when we are finding out what memory exactly belongs to
1957 * emulated process. No locks needed here, as long as
1958 * thread that received the signal is stopped.
1959 */
1960
1961static struct mm_struct *vma_init(void)
1962{
1963 struct mm_struct *mm;
1964
1965 if ((mm = qemu_malloc(sizeof (*mm))) == NULL)
1966 return (NULL);
1967
1968 mm->mm_count = 0;
72cf2d4f 1969 QTAILQ_INIT(&mm->mm_mmap);
edf8e2af
MW
1970
1971 return (mm);
1972}
1973
1974static void vma_delete(struct mm_struct *mm)
1975{
1976 struct vm_area_struct *vma;
1977
1978 while ((vma = vma_first(mm)) != NULL) {
72cf2d4f 1979 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
edf8e2af
MW
1980 qemu_free(vma);
1981 }
1982 qemu_free(mm);
1983}
1984
1985static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
1986 abi_ulong end, abi_ulong flags)
1987{
1988 struct vm_area_struct *vma;
1989
1990 if ((vma = qemu_mallocz(sizeof (*vma))) == NULL)
1991 return (-1);
1992
1993 vma->vma_start = start;
1994 vma->vma_end = end;
1995 vma->vma_flags = flags;
1996
72cf2d4f 1997 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
edf8e2af
MW
1998 mm->mm_count++;
1999
2000 return (0);
2001}
2002
2003static struct vm_area_struct *vma_first(const struct mm_struct *mm)
2004{
72cf2d4f 2005 return (QTAILQ_FIRST(&mm->mm_mmap));
edf8e2af
MW
2006}
2007
2008static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
2009{
72cf2d4f 2010 return (QTAILQ_NEXT(vma, vma_link));
edf8e2af
MW
2011}
2012
2013static int vma_get_mapping_count(const struct mm_struct *mm)
2014{
2015 return (mm->mm_count);
2016}
2017
2018/*
2019 * Calculate file (dump) size of given memory region.
2020 */
2021static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
2022{
2023 /* if we cannot even read the first page, skip it */
2024 if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
2025 return (0);
2026
2027 /*
2028 * Usually we don't dump executable pages as they contain
2029 * non-writable code that debugger can read directly from
2030 * target library etc. However, thread stacks are marked
2031 * also executable so we read in first page of given region
2032 * and check whether it contains elf header. If there is
2033 * no elf header, we dump it.
2034 */
2035 if (vma->vma_flags & PROT_EXEC) {
2036 char page[TARGET_PAGE_SIZE];
2037
2038 copy_from_user(page, vma->vma_start, sizeof (page));
2039 if ((page[EI_MAG0] == ELFMAG0) &&
2040 (page[EI_MAG1] == ELFMAG1) &&
2041 (page[EI_MAG2] == ELFMAG2) &&
2042 (page[EI_MAG3] == ELFMAG3)) {
2043 /*
2044 * Mappings are possibly from ELF binary. Don't dump
2045 * them.
2046 */
2047 return (0);
2048 }
2049 }
2050
2051 return (vma->vma_end - vma->vma_start);
2052}
2053
2054static int vma_walker(void *priv, unsigned long start, unsigned long end,
2055 unsigned long flags)
2056{
2057 struct mm_struct *mm = (struct mm_struct *)priv;
2058
2059 /*
2060 * Don't dump anything that qemu has reserved for internal use.
2061 */
2062 if (flags & PAGE_RESERVED)
2063 return (0);
2064
2065 vma_add_mapping(mm, start, end, flags);
2066 return (0);
2067}
2068
2069static void fill_note(struct memelfnote *note, const char *name, int type,
2070 unsigned int sz, void *data)
2071{
2072 unsigned int namesz;
2073
2074 namesz = strlen(name) + 1;
2075 note->name = name;
2076 note->namesz = namesz;
2077 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
2078 note->type = type;
2079 note->datasz = roundup(sz, sizeof (int32_t));;
2080 note->data = data;
2081
2082 /*
2083 * We calculate rounded up note size here as specified by
2084 * ELF document.
2085 */
2086 note->notesz = sizeof (struct elf_note) +
2087 note->namesz_rounded + note->datasz;
2088}
2089
2090static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
2091 uint32_t flags)
2092{
2093 (void) memset(elf, 0, sizeof(*elf));
2094
2095 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
2096 elf->e_ident[EI_CLASS] = ELF_CLASS;
2097 elf->e_ident[EI_DATA] = ELF_DATA;
2098 elf->e_ident[EI_VERSION] = EV_CURRENT;
2099 elf->e_ident[EI_OSABI] = ELF_OSABI;
2100
2101 elf->e_type = ET_CORE;
2102 elf->e_machine = machine;
2103 elf->e_version = EV_CURRENT;
2104 elf->e_phoff = sizeof(struct elfhdr);
2105 elf->e_flags = flags;
2106 elf->e_ehsize = sizeof(struct elfhdr);
2107 elf->e_phentsize = sizeof(struct elf_phdr);
2108 elf->e_phnum = segs;
2109
2110#ifdef BSWAP_NEEDED
2111 bswap_ehdr(elf);
2112#endif
2113}
2114
2115static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
2116{
2117 phdr->p_type = PT_NOTE;
2118 phdr->p_offset = offset;
2119 phdr->p_vaddr = 0;
2120 phdr->p_paddr = 0;
2121 phdr->p_filesz = sz;
2122 phdr->p_memsz = 0;
2123 phdr->p_flags = 0;
2124 phdr->p_align = 0;
2125
2126#ifdef BSWAP_NEEDED
2127 bswap_phdr(phdr);
2128#endif
2129}
2130
2131static size_t note_size(const struct memelfnote *note)
2132{
2133 return (note->notesz);
2134}
2135
a2547a13 2136static void fill_prstatus(struct target_elf_prstatus *prstatus,
edf8e2af
MW
2137 const TaskState *ts, int signr)
2138{
2139 (void) memset(prstatus, 0, sizeof (*prstatus));
2140 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
2141 prstatus->pr_pid = ts->ts_tid;
2142 prstatus->pr_ppid = getppid();
2143 prstatus->pr_pgrp = getpgrp();
2144 prstatus->pr_sid = getsid(0);
2145
2146#ifdef BSWAP_NEEDED
2147 bswap_prstatus(prstatus);
2148#endif
2149}
2150
a2547a13 2151static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
edf8e2af
MW
2152{
2153 char *filename, *base_filename;
2154 unsigned int i, len;
2155
2156 (void) memset(psinfo, 0, sizeof (*psinfo));
2157
2158 len = ts->info->arg_end - ts->info->arg_start;
2159 if (len >= ELF_PRARGSZ)
2160 len = ELF_PRARGSZ - 1;
2161 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
2162 return -EFAULT;
2163 for (i = 0; i < len; i++)
2164 if (psinfo->pr_psargs[i] == 0)
2165 psinfo->pr_psargs[i] = ' ';
2166 psinfo->pr_psargs[len] = 0;
2167
2168 psinfo->pr_pid = getpid();
2169 psinfo->pr_ppid = getppid();
2170 psinfo->pr_pgrp = getpgrp();
2171 psinfo->pr_sid = getsid(0);
2172 psinfo->pr_uid = getuid();
2173 psinfo->pr_gid = getgid();
2174
2175 filename = strdup(ts->bprm->filename);
2176 base_filename = strdup(basename(filename));
2177 (void) strncpy(psinfo->pr_fname, base_filename,
2178 sizeof(psinfo->pr_fname));
2179 free(base_filename);
2180 free(filename);
2181
2182#ifdef BSWAP_NEEDED
2183 bswap_psinfo(psinfo);
2184#endif
2185 return (0);
2186}
2187
2188static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
2189{
2190 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
2191 elf_addr_t orig_auxv = auxv;
2192 abi_ulong val;
2193 void *ptr;
2194 int i, len;
2195
2196 /*
2197 * Auxiliary vector is stored in target process stack. It contains
2198 * {type, value} pairs that we need to dump into note. This is not
2199 * strictly necessary but we do it here for sake of completeness.
2200 */
2201
2202 /* find out lenght of the vector, AT_NULL is terminator */
2203 i = len = 0;
2204 do {
2205 get_user_ual(val, auxv);
2206 i += 2;
2207 auxv += 2 * sizeof (elf_addr_t);
2208 } while (val != AT_NULL);
2209 len = i * sizeof (elf_addr_t);
2210
2211 /* read in whole auxv vector and copy it to memelfnote */
2212 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
2213 if (ptr != NULL) {
2214 fill_note(note, "CORE", NT_AUXV, len, ptr);
2215 unlock_user(ptr, auxv, len);
2216 }
2217}
2218
2219/*
2220 * Constructs name of coredump file. We have following convention
2221 * for the name:
2222 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
2223 *
2224 * Returns 0 in case of success, -1 otherwise (errno is set).
2225 */
2226static int core_dump_filename(const TaskState *ts, char *buf,
2227 size_t bufsize)
2228{
2229 char timestamp[64];
2230 char *filename = NULL;
2231 char *base_filename = NULL;
2232 struct timeval tv;
2233 struct tm tm;
2234
2235 assert(bufsize >= PATH_MAX);
2236
2237 if (gettimeofday(&tv, NULL) < 0) {
2238 (void) fprintf(stderr, "unable to get current timestamp: %s",
2239 strerror(errno));
2240 return (-1);
2241 }
2242
2243 filename = strdup(ts->bprm->filename);
2244 base_filename = strdup(basename(filename));
2245 (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
2246 localtime_r(&tv.tv_sec, &tm));
2247 (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
2248 base_filename, timestamp, (int)getpid());
2249 free(base_filename);
2250 free(filename);
2251
2252 return (0);
2253}
2254
2255static int dump_write(int fd, const void *ptr, size_t size)
2256{
2257 const char *bufp = (const char *)ptr;
2258 ssize_t bytes_written, bytes_left;
2259 struct rlimit dumpsize;
2260 off_t pos;
2261
2262 bytes_written = 0;
2263 getrlimit(RLIMIT_CORE, &dumpsize);
2264 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
2265 if (errno == ESPIPE) { /* not a seekable stream */
2266 bytes_left = size;
2267 } else {
2268 return pos;
2269 }
2270 } else {
2271 if (dumpsize.rlim_cur <= pos) {
2272 return -1;
2273 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
2274 bytes_left = size;
2275 } else {
2276 size_t limit_left=dumpsize.rlim_cur - pos;
2277 bytes_left = limit_left >= size ? size : limit_left ;
2278 }
2279 }
2280
2281 /*
2282 * In normal conditions, single write(2) should do but
2283 * in case of socket etc. this mechanism is more portable.
2284 */
2285 do {
2286 bytes_written = write(fd, bufp, bytes_left);
2287 if (bytes_written < 0) {
2288 if (errno == EINTR)
2289 continue;
2290 return (-1);
2291 } else if (bytes_written == 0) { /* eof */
2292 return (-1);
2293 }
2294 bufp += bytes_written;
2295 bytes_left -= bytes_written;
2296 } while (bytes_left > 0);
2297
2298 return (0);
2299}
2300
2301static int write_note(struct memelfnote *men, int fd)
2302{
2303 struct elf_note en;
2304
2305 en.n_namesz = men->namesz;
2306 en.n_type = men->type;
2307 en.n_descsz = men->datasz;
2308
2309#ifdef BSWAP_NEEDED
2310 bswap_note(&en);
2311#endif
2312
2313 if (dump_write(fd, &en, sizeof(en)) != 0)
2314 return (-1);
2315 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
2316 return (-1);
2317 if (dump_write(fd, men->data, men->datasz) != 0)
2318 return (-1);
2319
2320 return (0);
2321}
2322
2323static void fill_thread_info(struct elf_note_info *info, const CPUState *env)
2324{
2325 TaskState *ts = (TaskState *)env->opaque;
2326 struct elf_thread_status *ets;
2327
2328 ets = qemu_mallocz(sizeof (*ets));
2329 ets->num_notes = 1; /* only prstatus is dumped */
2330 fill_prstatus(&ets->prstatus, ts, 0);
2331 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
2332 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
2333 &ets->prstatus);
2334
72cf2d4f 2335 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
edf8e2af
MW
2336
2337 info->notes_size += note_size(&ets->notes[0]);
2338}
2339
2340static int fill_note_info(struct elf_note_info *info,
2341 long signr, const CPUState *env)
2342{
2343#define NUMNOTES 3
2344 CPUState *cpu = NULL;
2345 TaskState *ts = (TaskState *)env->opaque;
2346 int i;
2347
2348 (void) memset(info, 0, sizeof (*info));
2349
72cf2d4f 2350 QTAILQ_INIT(&info->thread_list);
edf8e2af
MW
2351
2352 info->notes = qemu_mallocz(NUMNOTES * sizeof (struct memelfnote));
2353 if (info->notes == NULL)
2354 return (-ENOMEM);
2355 info->prstatus = qemu_mallocz(sizeof (*info->prstatus));
2356 if (info->prstatus == NULL)
2357 return (-ENOMEM);
2358 info->psinfo = qemu_mallocz(sizeof (*info->psinfo));
2359 if (info->prstatus == NULL)
2360 return (-ENOMEM);
2361
2362 /*
2363 * First fill in status (and registers) of current thread
2364 * including process info & aux vector.
2365 */
2366 fill_prstatus(info->prstatus, ts, signr);
2367 elf_core_copy_regs(&info->prstatus->pr_reg, env);
2368 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
2369 sizeof (*info->prstatus), info->prstatus);
2370 fill_psinfo(info->psinfo, ts);
2371 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
2372 sizeof (*info->psinfo), info->psinfo);
2373 fill_auxv_note(&info->notes[2], ts);
2374 info->numnote = 3;
2375
2376 info->notes_size = 0;
2377 for (i = 0; i < info->numnote; i++)
2378 info->notes_size += note_size(&info->notes[i]);
2379
2380 /* read and fill status of all threads */
2381 cpu_list_lock();
2382 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2383 if (cpu == thread_env)
2384 continue;
2385 fill_thread_info(info, cpu);
2386 }
2387 cpu_list_unlock();
2388
2389 return (0);
2390}
2391
2392static void free_note_info(struct elf_note_info *info)
2393{
2394 struct elf_thread_status *ets;
2395
72cf2d4f
BS
2396 while (!QTAILQ_EMPTY(&info->thread_list)) {
2397 ets = QTAILQ_FIRST(&info->thread_list);
2398 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
edf8e2af
MW
2399 qemu_free(ets);
2400 }
2401
2402 qemu_free(info->prstatus);
2403 qemu_free(info->psinfo);
2404 qemu_free(info->notes);
2405}
2406
2407static int write_note_info(struct elf_note_info *info, int fd)
2408{
2409 struct elf_thread_status *ets;
2410 int i, error = 0;
2411
2412 /* write prstatus, psinfo and auxv for current thread */
2413 for (i = 0; i < info->numnote; i++)
2414 if ((error = write_note(&info->notes[i], fd)) != 0)
2415 return (error);
2416
2417 /* write prstatus for each thread */
2418 for (ets = info->thread_list.tqh_first; ets != NULL;
2419 ets = ets->ets_link.tqe_next) {
2420 if ((error = write_note(&ets->notes[0], fd)) != 0)
2421 return (error);
2422 }
2423
2424 return (0);
2425}
2426
2427/*
2428 * Write out ELF coredump.
2429 *
2430 * See documentation of ELF object file format in:
2431 * http://www.caldera.com/developers/devspecs/gabi41.pdf
2432 *
2433 * Coredump format in linux is following:
2434 *
2435 * 0 +----------------------+ \
2436 * | ELF header | ET_CORE |
2437 * +----------------------+ |
2438 * | ELF program headers | |--- headers
2439 * | - NOTE section | |
2440 * | - PT_LOAD sections | |
2441 * +----------------------+ /
2442 * | NOTEs: |
2443 * | - NT_PRSTATUS |
2444 * | - NT_PRSINFO |
2445 * | - NT_AUXV |
2446 * +----------------------+ <-- aligned to target page
2447 * | Process memory dump |
2448 * : :
2449 * . .
2450 * : :
2451 * | |
2452 * +----------------------+
2453 *
2454 * NT_PRSTATUS -> struct elf_prstatus (per thread)
2455 * NT_PRSINFO -> struct elf_prpsinfo
2456 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
2457 *
2458 * Format follows System V format as close as possible. Current
2459 * version limitations are as follows:
2460 * - no floating point registers are dumped
2461 *
2462 * Function returns 0 in case of success, negative errno otherwise.
2463 *
2464 * TODO: make this work also during runtime: it should be
2465 * possible to force coredump from running process and then
2466 * continue processing. For example qemu could set up SIGUSR2
2467 * handler (provided that target process haven't registered
2468 * handler for that) that does the dump when signal is received.
2469 */
2470static int elf_core_dump(int signr, const CPUState *env)
2471{
2472 const TaskState *ts = (const TaskState *)env->opaque;
2473 struct vm_area_struct *vma = NULL;
2474 char corefile[PATH_MAX];
2475 struct elf_note_info info;
2476 struct elfhdr elf;
2477 struct elf_phdr phdr;
2478 struct rlimit dumpsize;
2479 struct mm_struct *mm = NULL;
2480 off_t offset = 0, data_offset = 0;
2481 int segs = 0;
2482 int fd = -1;
2483
2484 errno = 0;
2485 getrlimit(RLIMIT_CORE, &dumpsize);
2486 if (dumpsize.rlim_cur == 0)
2487 return 0;
2488
2489 if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
2490 return (-errno);
2491
2492 if ((fd = open(corefile, O_WRONLY | O_CREAT,
2493 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
2494 return (-errno);
2495
2496 /*
2497 * Walk through target process memory mappings and
2498 * set up structure containing this information. After
2499 * this point vma_xxx functions can be used.
2500 */
2501 if ((mm = vma_init()) == NULL)
2502 goto out;
2503
2504 walk_memory_regions(mm, vma_walker);
2505 segs = vma_get_mapping_count(mm);
2506
2507 /*
2508 * Construct valid coredump ELF header. We also
2509 * add one more segment for notes.
2510 */
2511 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
2512 if (dump_write(fd, &elf, sizeof (elf)) != 0)
2513 goto out;
2514
2515 /* fill in in-memory version of notes */
2516 if (fill_note_info(&info, signr, env) < 0)
2517 goto out;
2518
2519 offset += sizeof (elf); /* elf header */
2520 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
2521
2522 /* write out notes program header */
2523 fill_elf_note_phdr(&phdr, info.notes_size, offset);
2524
2525 offset += info.notes_size;
2526 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
2527 goto out;
2528
2529 /*
2530 * ELF specification wants data to start at page boundary so
2531 * we align it here.
2532 */
2533 offset = roundup(offset, ELF_EXEC_PAGESIZE);
2534
2535 /*
2536 * Write program headers for memory regions mapped in
2537 * the target process.
2538 */
2539 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2540 (void) memset(&phdr, 0, sizeof (phdr));
2541
2542 phdr.p_type = PT_LOAD;
2543 phdr.p_offset = offset;
2544 phdr.p_vaddr = vma->vma_start;
2545 phdr.p_paddr = 0;
2546 phdr.p_filesz = vma_dump_size(vma);
2547 offset += phdr.p_filesz;
2548 phdr.p_memsz = vma->vma_end - vma->vma_start;
2549 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
2550 if (vma->vma_flags & PROT_WRITE)
2551 phdr.p_flags |= PF_W;
2552 if (vma->vma_flags & PROT_EXEC)
2553 phdr.p_flags |= PF_X;
2554 phdr.p_align = ELF_EXEC_PAGESIZE;
2555
2556 dump_write(fd, &phdr, sizeof (phdr));
2557 }
2558
2559 /*
2560 * Next we write notes just after program headers. No
2561 * alignment needed here.
2562 */
2563 if (write_note_info(&info, fd) < 0)
2564 goto out;
2565
2566 /* align data to page boundary */
2567 data_offset = lseek(fd, 0, SEEK_CUR);
2568 data_offset = TARGET_PAGE_ALIGN(data_offset);
2569 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
2570 goto out;
2571
2572 /*
2573 * Finally we can dump process memory into corefile as well.
2574 */
2575 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
2576 abi_ulong addr;
2577 abi_ulong end;
2578
2579 end = vma->vma_start + vma_dump_size(vma);
2580
2581 for (addr = vma->vma_start; addr < end;
2582 addr += TARGET_PAGE_SIZE) {
2583 char page[TARGET_PAGE_SIZE];
2584 int error;
2585
2586 /*
2587 * Read in page from target process memory and
2588 * write it to coredump file.
2589 */
2590 error = copy_from_user(page, addr, sizeof (page));
2591 if (error != 0) {
49995e17 2592 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
edf8e2af
MW
2593 addr);
2594 errno = -error;
2595 goto out;
2596 }
2597 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
2598 goto out;
2599 }
2600 }
2601
2602out:
2603 free_note_info(&info);
2604 if (mm != NULL)
2605 vma_delete(mm);
2606 (void) close(fd);
2607
2608 if (errno != 0)
2609 return (-errno);
2610 return (0);
2611}
2612
2613#endif /* USE_ELF_CORE_DUMP */
2614
31e31b8a
FB
2615static int load_aout_interp(void * exptr, int interp_fd)
2616{
2617 printf("a.out interpreter not yet supported\n");
2618 return(0);
2619}
2620
e5fe0c52
PB
2621void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
2622{
2623 init_thread(regs, infop);
2624}