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