]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/elfload.c
PPC TCG Fixes
[mirror_qemu.git] / linux-user / elfload.c
CommitLineData
31e31b8a
FB
1/* This is the Linux kernel elf-loading code, ported into user space */
2
3#include <stdio.h>
4#include <sys/types.h>
5#include <fcntl.h>
31e31b8a
FB
6#include <errno.h>
7#include <unistd.h>
8#include <sys/mman.h>
9#include <stdlib.h>
10#include <string.h>
11
3ef693a0 12#include "qemu.h"
689f936f 13#include "disas.h"
31e31b8a 14
cb33da57
BS
15/* from personality.h */
16
17/*
18 * Flags for bug emulation.
19 *
20 * These occupy the top three bytes.
21 */
22enum {
23 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
24 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to descriptors
25 * (signal handling)
26 */
27 MMAP_PAGE_ZERO = 0x0100000,
28 ADDR_COMPAT_LAYOUT = 0x0200000,
29 READ_IMPLIES_EXEC = 0x0400000,
30 ADDR_LIMIT_32BIT = 0x0800000,
31 SHORT_INODE = 0x1000000,
32 WHOLE_SECONDS = 0x2000000,
33 STICKY_TIMEOUTS = 0x4000000,
34 ADDR_LIMIT_3GB = 0x8000000,
35};
36
37/*
38 * Personality types.
39 *
40 * These go in the low byte. Avoid using the top bit, it will
41 * conflict with error returns.
42 */
43enum {
44 PER_LINUX = 0x0000,
45 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
46 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
47 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
48 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
49 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS |
50 WHOLE_SECONDS | SHORT_INODE,
51 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
52 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
53 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
54 PER_BSD = 0x0006,
55 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
56 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
57 PER_LINUX32 = 0x0008,
58 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
59 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
60 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
61 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
62 PER_RISCOS = 0x000c,
63 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
64 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
65 PER_OSF4 = 0x000f, /* OSF/1 v4 */
66 PER_HPUX = 0x0010,
67 PER_MASK = 0x00ff,
68};
69
70/*
71 * Return the base personality without flags.
72 */
73#define personality(pers) (pers & PER_MASK)
74
83fb7adf
FB
75/* this flag is uneffective under linux too, should be deleted */
76#ifndef MAP_DENYWRITE
77#define MAP_DENYWRITE 0
78#endif
79
80/* should probably go in elf.h */
81#ifndef ELIBBAD
82#define ELIBBAD 80
83#endif
84
30ac07d4
FB
85#ifdef TARGET_I386
86
15338fd7
FB
87#define ELF_PLATFORM get_elf_platform()
88
89static const char *get_elf_platform(void)
90{
91 static char elf_platform[] = "i386";
92 int family = (global_env->cpuid_version >> 8) & 0xff;
93 if (family > 6)
94 family = 6;
95 if (family >= 3)
96 elf_platform[1] = '0' + family;
97 return elf_platform;
98}
99
100#define ELF_HWCAP get_elf_hwcap()
101
102static uint32_t get_elf_hwcap(void)
103{
104 return global_env->cpuid_features;
105}
106
84409ddb
JM
107#ifdef TARGET_X86_64
108#define ELF_START_MMAP 0x2aaaaab000ULL
109#define elf_check_arch(x) ( ((x) == ELF_ARCH) )
110
111#define ELF_CLASS ELFCLASS64
112#define ELF_DATA ELFDATA2LSB
113#define ELF_ARCH EM_X86_64
114
115static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
116{
117 regs->rax = 0;
118 regs->rsp = infop->start_stack;
119 regs->rip = infop->entry;
120}
121
122#else
123
30ac07d4
FB
124#define ELF_START_MMAP 0x80000000
125
30ac07d4
FB
126/*
127 * This is used to ensure we don't load something for the wrong architecture.
128 */
129#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
130
131/*
132 * These are used to set parameters in the core dumps.
133 */
134#define ELF_CLASS ELFCLASS32
135#define ELF_DATA ELFDATA2LSB
136#define ELF_ARCH EM_386
137
b346ff46
FB
138static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
139{
140 regs->esp = infop->start_stack;
141 regs->eip = infop->entry;
e5fe0c52
PB
142
143 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
144 starts %edx contains a pointer to a function which might be
145 registered using `atexit'. This provides a mean for the
146 dynamic linker to call DT_FINI functions for shared libraries
147 that have been loaded before the code runs.
148
149 A value of 0 tells we have no such handler. */
150 regs->edx = 0;
b346ff46 151}
84409ddb 152#endif
b346ff46
FB
153
154#define USE_ELF_CORE_DUMP
155#define ELF_EXEC_PAGESIZE 4096
156
157#endif
158
159#ifdef TARGET_ARM
160
161#define ELF_START_MMAP 0x80000000
162
163#define elf_check_arch(x) ( (x) == EM_ARM )
164
165#define ELF_CLASS ELFCLASS32
166#ifdef TARGET_WORDS_BIGENDIAN
167#define ELF_DATA ELFDATA2MSB
168#else
169#define ELF_DATA ELFDATA2LSB
170#endif
171#define ELF_ARCH EM_ARM
172
b346ff46
FB
173static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
174{
992f48a0 175 abi_long stack = infop->start_stack;
b346ff46
FB
176 memset(regs, 0, sizeof(*regs));
177 regs->ARM_cpsr = 0x10;
0240ded8
PB
178 if (infop->entry & 1)
179 regs->ARM_cpsr |= CPSR_T;
180 regs->ARM_pc = infop->entry & 0xfffffffe;
b346ff46 181 regs->ARM_sp = infop->start_stack;
2f619698
FB
182 /* FIXME - what to for failure of get_user()? */
183 get_user_ual(regs->ARM_r2, stack + 8); /* envp */
184 get_user_ual(regs->ARM_r1, stack + 4); /* envp */
a1516e92 185 /* XXX: it seems that r0 is zeroed after ! */
e5fe0c52
PB
186 regs->ARM_r0 = 0;
187 /* For uClinux PIC binaries. */
863cf0b7 188 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
e5fe0c52 189 regs->ARM_r10 = infop->start_data;
b346ff46
FB
190}
191
30ac07d4
FB
192#define USE_ELF_CORE_DUMP
193#define ELF_EXEC_PAGESIZE 4096
194
afce2927
FB
195enum
196{
197 ARM_HWCAP_ARM_SWP = 1 << 0,
198 ARM_HWCAP_ARM_HALF = 1 << 1,
199 ARM_HWCAP_ARM_THUMB = 1 << 2,
200 ARM_HWCAP_ARM_26BIT = 1 << 3,
201 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
202 ARM_HWCAP_ARM_FPA = 1 << 5,
203 ARM_HWCAP_ARM_VFP = 1 << 6,
204 ARM_HWCAP_ARM_EDSP = 1 << 7,
205};
206
15338fd7 207#define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF \
afce2927
FB
208 | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT \
209 | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP)
210
30ac07d4
FB
211#endif
212
853d6f7a 213#ifdef TARGET_SPARC
a315a145 214#ifdef TARGET_SPARC64
853d6f7a
FB
215
216#define ELF_START_MMAP 0x80000000
217
992f48a0 218#ifndef TARGET_ABI32
cb33da57 219#define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
992f48a0
BS
220#else
221#define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
222#endif
853d6f7a 223
a315a145
FB
224#define ELF_CLASS ELFCLASS64
225#define ELF_DATA ELFDATA2MSB
5ef54116
FB
226#define ELF_ARCH EM_SPARCV9
227
228#define STACK_BIAS 2047
a315a145 229
a315a145
FB
230static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
231{
992f48a0 232#ifndef TARGET_ABI32
a315a145 233 regs->tstate = 0;
992f48a0 234#endif
a315a145
FB
235 regs->pc = infop->entry;
236 regs->npc = regs->pc + 4;
237 regs->y = 0;
992f48a0
BS
238#ifdef TARGET_ABI32
239 regs->u_regs[14] = infop->start_stack - 16 * 4;
240#else
cb33da57
BS
241 if (personality(infop->personality) == PER_LINUX32)
242 regs->u_regs[14] = infop->start_stack - 16 * 4;
243 else
244 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
992f48a0 245#endif
a315a145
FB
246}
247
248#else
249#define ELF_START_MMAP 0x80000000
250
251#define elf_check_arch(x) ( (x) == EM_SPARC )
252
853d6f7a
FB
253#define ELF_CLASS ELFCLASS32
254#define ELF_DATA ELFDATA2MSB
255#define ELF_ARCH EM_SPARC
256
853d6f7a
FB
257static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
258{
f5155289
FB
259 regs->psr = 0;
260 regs->pc = infop->entry;
261 regs->npc = regs->pc + 4;
262 regs->y = 0;
263 regs->u_regs[14] = infop->start_stack - 16 * 4;
853d6f7a
FB
264}
265
a315a145 266#endif
853d6f7a
FB
267#endif
268
67867308
FB
269#ifdef TARGET_PPC
270
271#define ELF_START_MMAP 0x80000000
272
e85e7c6e 273#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
84409ddb
JM
274
275#define elf_check_arch(x) ( (x) == EM_PPC64 )
276
277#define ELF_CLASS ELFCLASS64
278
279#else
280
67867308
FB
281#define elf_check_arch(x) ( (x) == EM_PPC )
282
283#define ELF_CLASS ELFCLASS32
84409ddb
JM
284
285#endif
286
67867308
FB
287#ifdef TARGET_WORDS_BIGENDIAN
288#define ELF_DATA ELFDATA2MSB
289#else
290#define ELF_DATA ELFDATA2LSB
291#endif
292#define ELF_ARCH EM_PPC
293
f5155289
FB
294/*
295 * We need to put in some extra aux table entries to tell glibc what
296 * the cache block size is, so it can use the dcbz instruction safely.
297 */
298#define AT_DCACHEBSIZE 19
299#define AT_ICACHEBSIZE 20
300#define AT_UCACHEBSIZE 21
301/* A special ignored type value for PPC, for glibc compatibility. */
302#define AT_IGNOREPPC 22
303/*
304 * The requirements here are:
305 * - keep the final alignment of sp (sp & 0xf)
306 * - make sure the 32-bit value at the first 16 byte aligned position of
307 * AUXV is greater than 16 for glibc compatibility.
308 * AT_IGNOREPPC is used for that.
309 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
310 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
311 */
0bccf03d 312#define DLINFO_ARCH_ITEMS 5
f5155289
FB
313#define ARCH_DLINFO \
314do { \
0bccf03d
FB
315 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
316 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
317 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
f5155289
FB
318 /* \
319 * Now handle glibc compatibility. \
320 */ \
0bccf03d
FB
321 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
322 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
f5155289
FB
323 } while (0)
324
67867308
FB
325static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
326{
992f48a0
BS
327 abi_ulong pos = infop->start_stack;
328 abi_ulong tmp;
e85e7c6e 329#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
992f48a0 330 abi_ulong entry, toc;
84409ddb 331#endif
e5fe0c52 332
67867308 333 _regs->gpr[1] = infop->start_stack;
e85e7c6e 334#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
84409ddb
JM
335 entry = ldq_raw(infop->entry) + infop->load_addr;
336 toc = ldq_raw(infop->entry + 8) + infop->load_addr;
337 _regs->gpr[2] = toc;
338 infop->entry = entry;
339#endif
67867308 340 _regs->nip = infop->entry;
e5fe0c52
PB
341 /* Note that isn't exactly what regular kernel does
342 * but this is what the ABI wants and is needed to allow
343 * execution of PPC BSD programs.
344 */
2f619698
FB
345 /* FIXME - what to for failure of get_user()? */
346 get_user_ual(_regs->gpr[3], pos);
992f48a0 347 pos += sizeof(abi_ulong);
e5fe0c52 348 _regs->gpr[4] = pos;
992f48a0 349 for (tmp = 1; tmp != 0; pos += sizeof(abi_ulong))
e5fe0c52
PB
350 tmp = ldl(pos);
351 _regs->gpr[5] = pos;
67867308
FB
352}
353
354#define USE_ELF_CORE_DUMP
355#define ELF_EXEC_PAGESIZE 4096
356
357#endif
358
048f6b4d
FB
359#ifdef TARGET_MIPS
360
361#define ELF_START_MMAP 0x80000000
362
363#define elf_check_arch(x) ( (x) == EM_MIPS )
364
388bb21a
TS
365#ifdef TARGET_MIPS64
366#define ELF_CLASS ELFCLASS64
367#else
048f6b4d 368#define ELF_CLASS ELFCLASS32
388bb21a 369#endif
048f6b4d
FB
370#ifdef TARGET_WORDS_BIGENDIAN
371#define ELF_DATA ELFDATA2MSB
372#else
373#define ELF_DATA ELFDATA2LSB
374#endif
375#define ELF_ARCH EM_MIPS
376
048f6b4d
FB
377static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
378{
623a930e 379 regs->cp0_status = 2 << CP0St_KSU;
048f6b4d
FB
380 regs->cp0_epc = infop->entry;
381 regs->regs[29] = infop->start_stack;
382}
383
388bb21a
TS
384#define USE_ELF_CORE_DUMP
385#define ELF_EXEC_PAGESIZE 4096
386
048f6b4d
FB
387#endif /* TARGET_MIPS */
388
fdf9b3e8
FB
389#ifdef TARGET_SH4
390
391#define ELF_START_MMAP 0x80000000
392
393#define elf_check_arch(x) ( (x) == EM_SH )
394
395#define ELF_CLASS ELFCLASS32
396#define ELF_DATA ELFDATA2LSB
397#define ELF_ARCH EM_SH
398
fdf9b3e8
FB
399static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
400{
401 /* Check other registers XXXXX */
402 regs->pc = infop->entry;
072ae847 403 regs->regs[15] = infop->start_stack;
fdf9b3e8
FB
404}
405
406#define USE_ELF_CORE_DUMP
407#define ELF_EXEC_PAGESIZE 4096
408
409#endif
410
48733d19
TS
411#ifdef TARGET_CRIS
412
413#define ELF_START_MMAP 0x80000000
414
415#define elf_check_arch(x) ( (x) == EM_CRIS )
416
417#define ELF_CLASS ELFCLASS32
418#define ELF_DATA ELFDATA2LSB
419#define ELF_ARCH EM_CRIS
420
421static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
422{
423 regs->erp = infop->entry;
424}
425
426#define USE_ELF_CORE_DUMP
427#define ELF_EXEC_PAGESIZE 8192
428
429#endif
430
e6e5906b
PB
431#ifdef TARGET_M68K
432
433#define ELF_START_MMAP 0x80000000
434
435#define elf_check_arch(x) ( (x) == EM_68K )
436
437#define ELF_CLASS ELFCLASS32
438#define ELF_DATA ELFDATA2MSB
439#define ELF_ARCH EM_68K
440
441/* ??? Does this need to do anything?
442#define ELF_PLAT_INIT(_r) */
443
444static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
445{
446 regs->usp = infop->start_stack;
447 regs->sr = 0;
448 regs->pc = infop->entry;
449}
450
451#define USE_ELF_CORE_DUMP
452#define ELF_EXEC_PAGESIZE 8192
453
454#endif
455
7a3148a9
JM
456#ifdef TARGET_ALPHA
457
458#define ELF_START_MMAP (0x30000000000ULL)
459
460#define elf_check_arch(x) ( (x) == ELF_ARCH )
461
462#define ELF_CLASS ELFCLASS64
463#define ELF_DATA ELFDATA2MSB
464#define ELF_ARCH EM_ALPHA
465
466static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
467{
468 regs->pc = infop->entry;
469 regs->ps = 8;
470 regs->usp = infop->start_stack;
471 regs->unique = infop->start_data; /* ? */
472 printf("Set unique value to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n",
473 regs->unique, infop->start_data);
474}
475
476#define USE_ELF_CORE_DUMP
477#define ELF_EXEC_PAGESIZE 8192
478
479#endif /* TARGET_ALPHA */
480
15338fd7
FB
481#ifndef ELF_PLATFORM
482#define ELF_PLATFORM (NULL)
483#endif
484
485#ifndef ELF_HWCAP
486#define ELF_HWCAP 0
487#endif
488
992f48a0 489#ifdef TARGET_ABI32
cb33da57 490#undef ELF_CLASS
992f48a0 491#define ELF_CLASS ELFCLASS32
cb33da57
BS
492#undef bswaptls
493#define bswaptls(ptr) bswap32s(ptr)
494#endif
495
31e31b8a 496#include "elf.h"
09bfb054 497
09bfb054
FB
498struct exec
499{
500 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
501 unsigned int a_text; /* length of text, in bytes */
502 unsigned int a_data; /* length of data, in bytes */
503 unsigned int a_bss; /* length of uninitialized data area, in bytes */
504 unsigned int a_syms; /* length of symbol table data in file, in bytes */
505 unsigned int a_entry; /* start address */
506 unsigned int a_trsize; /* length of relocation info for text, in bytes */
507 unsigned int a_drsize; /* length of relocation info for data, in bytes */
508};
509
510
511#define N_MAGIC(exec) ((exec).a_info & 0xffff)
512#define OMAGIC 0407
513#define NMAGIC 0410
514#define ZMAGIC 0413
515#define QMAGIC 0314
516
09bfb054
FB
517/* max code+data+bss space allocated to elf interpreter */
518#define INTERP_MAP_SIZE (32 * 1024 * 1024)
519
520/* max code+data+bss+brk space allocated to ET_DYN executables */
521#define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
522
31e31b8a 523/* Necessary parameters */
54936004
FB
524#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
525#define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
526#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
31e31b8a
FB
527
528#define INTERPRETER_NONE 0
529#define INTERPRETER_AOUT 1
530#define INTERPRETER_ELF 2
531
15338fd7 532#define DLINFO_ITEMS 12
31e31b8a 533
09bfb054
FB
534static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
535{
536 memcpy(to, from, n);
537}
d691f669 538
31e31b8a
FB
539extern unsigned long x86_stack_size;
540
541static int load_aout_interp(void * exptr, int interp_fd);
542
543#ifdef BSWAP_NEEDED
92a31b1f 544static void bswap_ehdr(struct elfhdr *ehdr)
31e31b8a
FB
545{
546 bswap16s(&ehdr->e_type); /* Object file type */
547 bswap16s(&ehdr->e_machine); /* Architecture */
548 bswap32s(&ehdr->e_version); /* Object file version */
92a31b1f
FB
549 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
550 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
551 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
31e31b8a
FB
552 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
553 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
554 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
555 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
556 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
557 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
558 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
559}
560
92a31b1f 561static void bswap_phdr(struct elf_phdr *phdr)
31e31b8a
FB
562{
563 bswap32s(&phdr->p_type); /* Segment type */
92a31b1f
FB
564 bswaptls(&phdr->p_offset); /* Segment file offset */
565 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
566 bswaptls(&phdr->p_paddr); /* Segment physical address */
567 bswaptls(&phdr->p_filesz); /* Segment size in file */
568 bswaptls(&phdr->p_memsz); /* Segment size in memory */
31e31b8a 569 bswap32s(&phdr->p_flags); /* Segment flags */
92a31b1f 570 bswaptls(&phdr->p_align); /* Segment alignment */
31e31b8a 571}
689f936f 572
92a31b1f 573static void bswap_shdr(struct elf_shdr *shdr)
689f936f
FB
574{
575 bswap32s(&shdr->sh_name);
576 bswap32s(&shdr->sh_type);
92a31b1f
FB
577 bswaptls(&shdr->sh_flags);
578 bswaptls(&shdr->sh_addr);
579 bswaptls(&shdr->sh_offset);
580 bswaptls(&shdr->sh_size);
689f936f
FB
581 bswap32s(&shdr->sh_link);
582 bswap32s(&shdr->sh_info);
92a31b1f
FB
583 bswaptls(&shdr->sh_addralign);
584 bswaptls(&shdr->sh_entsize);
689f936f
FB
585}
586
7a3148a9 587static void bswap_sym(struct elf_sym *sym)
689f936f
FB
588{
589 bswap32s(&sym->st_name);
7a3148a9
JM
590 bswaptls(&sym->st_value);
591 bswaptls(&sym->st_size);
689f936f
FB
592 bswap16s(&sym->st_shndx);
593}
31e31b8a
FB
594#endif
595
31e31b8a 596/*
e5fe0c52 597 * 'copy_elf_strings()' copies argument/envelope strings from user
31e31b8a
FB
598 * memory to free pages in kernel mem. These are in a format ready
599 * to be put directly into the top of new user memory.
600 *
601 */
992f48a0
BS
602static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
603 abi_ulong p)
31e31b8a
FB
604{
605 char *tmp, *tmp1, *pag = NULL;
606 int len, offset = 0;
607
608 if (!p) {
609 return 0; /* bullet-proofing */
610 }
611 while (argc-- > 0) {
edf779ff
FB
612 tmp = argv[argc];
613 if (!tmp) {
31e31b8a
FB
614 fprintf(stderr, "VFS: argc is wrong");
615 exit(-1);
616 }
edf779ff
FB
617 tmp1 = tmp;
618 while (*tmp++);
31e31b8a
FB
619 len = tmp - tmp1;
620 if (p < len) { /* this shouldn't happen - 128kB */
621 return 0;
622 }
623 while (len) {
624 --p; --tmp; --len;
625 if (--offset < 0) {
54936004 626 offset = p % TARGET_PAGE_SIZE;
53a5960a 627 pag = (char *)page[p/TARGET_PAGE_SIZE];
44a91cae 628 if (!pag) {
53a5960a 629 pag = (char *)malloc(TARGET_PAGE_SIZE);
4118a970 630 memset(pag, 0, TARGET_PAGE_SIZE);
53a5960a 631 page[p/TARGET_PAGE_SIZE] = pag;
44a91cae
FB
632 if (!pag)
633 return 0;
31e31b8a
FB
634 }
635 }
636 if (len == 0 || offset == 0) {
edf779ff 637 *(pag + offset) = *tmp;
31e31b8a
FB
638 }
639 else {
640 int bytes_to_copy = (len > offset) ? offset : len;
641 tmp -= bytes_to_copy;
642 p -= bytes_to_copy;
643 offset -= bytes_to_copy;
644 len -= bytes_to_copy;
645 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
646 }
647 }
648 }
649 return p;
650}
651
992f48a0
BS
652static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
653 struct image_info *info)
53a5960a 654{
992f48a0 655 abi_ulong stack_base, size, error;
31e31b8a 656 int i;
31e31b8a 657
09bfb054
FB
658 /* Create enough stack to hold everything. If we don't use
659 * it for args, we'll use it for something else...
660 */
661 size = x86_stack_size;
54936004
FB
662 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
663 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
5fafdf24 664 error = target_mmap(0,
83fb7adf 665 size + qemu_host_page_size,
54936004
FB
666 PROT_READ | PROT_WRITE,
667 MAP_PRIVATE | MAP_ANONYMOUS,
668 -1, 0);
09bfb054
FB
669 if (error == -1) {
670 perror("stk mmap");
671 exit(-1);
672 }
673 /* we reserve one extra page at the top of the stack as guard */
83fb7adf 674 target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
31e31b8a 675
54936004 676 stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
31e31b8a 677 p += stack_base;
09bfb054 678
31e31b8a
FB
679 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
680 if (bprm->page[i]) {
681 info->rss++;
579a97f7 682 /* FIXME - check return value of memcpy_to_target() for failure */
53a5960a
PB
683 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
684 free(bprm->page[i]);
31e31b8a 685 }
53a5960a 686 stack_base += TARGET_PAGE_SIZE;
31e31b8a
FB
687 }
688 return p;
689}
690
992f48a0 691static void set_brk(abi_ulong start, abi_ulong end)
31e31b8a
FB
692{
693 /* page-align the start and end addresses... */
54936004
FB
694 start = HOST_PAGE_ALIGN(start);
695 end = HOST_PAGE_ALIGN(end);
31e31b8a
FB
696 if (end <= start)
697 return;
54936004
FB
698 if(target_mmap(start, end - start,
699 PROT_READ | PROT_WRITE | PROT_EXEC,
700 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
31e31b8a
FB
701 perror("cannot mmap brk");
702 exit(-1);
703 }
704}
705
706
853d6f7a
FB
707/* We need to explicitly zero any fractional pages after the data
708 section (i.e. bss). This would contain the junk from the file that
709 should not be in memory. */
992f48a0 710static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
31e31b8a 711{
992f48a0 712 abi_ulong nbyte;
31e31b8a 713
768a4a36
TS
714 if (elf_bss >= last_bss)
715 return;
716
853d6f7a
FB
717 /* XXX: this is really a hack : if the real host page size is
718 smaller than the target page size, some pages after the end
719 of the file may not be mapped. A better fix would be to
720 patch target_mmap(), but it is more complicated as the file
721 size must be known */
83fb7adf 722 if (qemu_real_host_page_size < qemu_host_page_size) {
992f48a0 723 abi_ulong end_addr, end_addr1;
5fafdf24 724 end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
83fb7adf 725 ~(qemu_real_host_page_size - 1);
853d6f7a
FB
726 end_addr = HOST_PAGE_ALIGN(elf_bss);
727 if (end_addr1 < end_addr) {
863cf0b7 728 mmap((void *)g2h(end_addr1), end_addr - end_addr1,
853d6f7a
FB
729 PROT_READ|PROT_WRITE|PROT_EXEC,
730 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
731 }
732 }
733
83fb7adf 734 nbyte = elf_bss & (qemu_host_page_size-1);
31e31b8a 735 if (nbyte) {
83fb7adf 736 nbyte = qemu_host_page_size - nbyte;
31e31b8a 737 do {
2f619698
FB
738 /* FIXME - what to do if put_user() fails? */
739 put_user_u8(0, elf_bss);
53a5960a 740 elf_bss++;
31e31b8a
FB
741 } while (--nbyte);
742 }
743}
744
53a5960a 745
992f48a0
BS
746static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
747 struct elfhdr * exec,
748 abi_ulong load_addr,
749 abi_ulong load_bias,
750 abi_ulong interp_load_addr, int ibcs,
751 struct image_info *info)
31e31b8a 752{
992f48a0 753 abi_ulong sp;
53a5960a 754 int size;
992f48a0 755 abi_ulong u_platform;
15338fd7 756 const char *k_platform;
863cf0b7 757 const int n = sizeof(elf_addr_t);
edf779ff 758
53a5960a
PB
759 sp = p;
760 u_platform = 0;
15338fd7
FB
761 k_platform = ELF_PLATFORM;
762 if (k_platform) {
763 size_t len = strlen(k_platform) + 1;
53a5960a
PB
764 sp -= (len + n - 1) & ~(n - 1);
765 u_platform = sp;
579a97f7 766 /* FIXME - check return value of memcpy_to_target() for failure */
53a5960a 767 memcpy_to_target(sp, k_platform, len);
15338fd7 768 }
53a5960a
PB
769 /*
770 * Force 16 byte _final_ alignment here for generality.
771 */
992f48a0 772 sp = sp &~ (abi_ulong)15;
53a5960a 773 size = (DLINFO_ITEMS + 1) * 2;
15338fd7 774 if (k_platform)
53a5960a 775 size += 2;
f5155289 776#ifdef DLINFO_ARCH_ITEMS
53a5960a 777 size += DLINFO_ARCH_ITEMS * 2;
f5155289 778#endif
53a5960a
PB
779 size += envc + argc + 2;
780 size += (!ibcs ? 3 : 1); /* argc itself */
781 size *= n;
782 if (size & 15)
783 sp -= 16 - (size & 15);
3b46e624 784
863cf0b7
JM
785 /* This is correct because Linux defines
786 * elf_addr_t as Elf32_Off / Elf64_Off
787 */
2f619698
FB
788#define NEW_AUX_ENT(id, val) do { \
789 sp -= n; put_user_ual(val, sp); \
790 sp -= n; put_user_ual(id, sp); \
53a5960a 791 } while(0)
2f619698 792
0bccf03d
FB
793 NEW_AUX_ENT (AT_NULL, 0);
794
795 /* There must be exactly DLINFO_ITEMS entries here. */
992f48a0
BS
796 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
797 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
798 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
799 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
800 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
801 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
0bccf03d 802 NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
992f48a0
BS
803 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
804 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
805 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
806 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
807 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
a07c67df 808 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
15338fd7 809 if (k_platform)
53a5960a 810 NEW_AUX_ENT(AT_PLATFORM, u_platform);
f5155289 811#ifdef ARCH_DLINFO
5fafdf24 812 /*
f5155289
FB
813 * ARCH_DLINFO must come last so platform specific code can enforce
814 * special alignment requirements on the AUXV if necessary (eg. PPC).
815 */
816 ARCH_DLINFO;
817#endif
818#undef NEW_AUX_ENT
819
e5fe0c52 820 sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
31e31b8a
FB
821 return sp;
822}
823
824
992f48a0
BS
825static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
826 int interpreter_fd,
827 abi_ulong *interp_load_addr)
31e31b8a
FB
828{
829 struct elf_phdr *elf_phdata = NULL;
830 struct elf_phdr *eppnt;
992f48a0 831 abi_ulong load_addr = 0;
31e31b8a
FB
832 int load_addr_set = 0;
833 int retval;
992f48a0
BS
834 abi_ulong last_bss, elf_bss;
835 abi_ulong error;
31e31b8a 836 int i;
5fafdf24 837
31e31b8a
FB
838 elf_bss = 0;
839 last_bss = 0;
840 error = 0;
841
644c433c
FB
842#ifdef BSWAP_NEEDED
843 bswap_ehdr(interp_elf_ex);
844#endif
31e31b8a 845 /* First of all, some simple consistency checks */
5fafdf24
TS
846 if ((interp_elf_ex->e_type != ET_EXEC &&
847 interp_elf_ex->e_type != ET_DYN) ||
31e31b8a 848 !elf_check_arch(interp_elf_ex->e_machine)) {
992f48a0 849 return ~((abi_ulong)0UL);
31e31b8a 850 }
5fafdf24 851
644c433c 852
31e31b8a 853 /* Now read in all of the header information */
5fafdf24 854
54936004 855 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
992f48a0 856 return ~(abi_ulong)0UL;
5fafdf24
TS
857
858 elf_phdata = (struct elf_phdr *)
31e31b8a
FB
859 malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
860
861 if (!elf_phdata)
992f48a0 862 return ~((abi_ulong)0UL);
5fafdf24 863
31e31b8a
FB
864 /*
865 * If the size of this structure has changed, then punt, since
866 * we will be doing the wrong thing.
867 */
09bfb054 868 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
31e31b8a 869 free(elf_phdata);
992f48a0 870 return ~((abi_ulong)0UL);
09bfb054 871 }
31e31b8a
FB
872
873 retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
874 if(retval >= 0) {
875 retval = read(interpreter_fd,
876 (char *) elf_phdata,
877 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
878 }
31e31b8a
FB
879 if (retval < 0) {
880 perror("load_elf_interp");
881 exit(-1);
882 free (elf_phdata);
883 return retval;
884 }
885#ifdef BSWAP_NEEDED
886 eppnt = elf_phdata;
887 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
888 bswap_phdr(eppnt);
889 }
890#endif
09bfb054
FB
891
892 if (interp_elf_ex->e_type == ET_DYN) {
e91c8a77 893 /* in order to avoid hardcoding the interpreter load
09bfb054 894 address in qemu, we allocate a big enough memory zone */
54936004 895 error = target_mmap(0, INTERP_MAP_SIZE,
5fafdf24 896 PROT_NONE, MAP_PRIVATE | MAP_ANON,
54936004 897 -1, 0);
09bfb054
FB
898 if (error == -1) {
899 perror("mmap");
900 exit(-1);
901 }
902 load_addr = error;
903 load_addr_set = 1;
904 }
905
31e31b8a
FB
906 eppnt = elf_phdata;
907 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
908 if (eppnt->p_type == PT_LOAD) {
909 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
910 int elf_prot = 0;
992f48a0
BS
911 abi_ulong vaddr = 0;
912 abi_ulong k;
31e31b8a
FB
913
914 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
915 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
916 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
917 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
918 elf_type |= MAP_FIXED;
919 vaddr = eppnt->p_vaddr;
920 }
54936004
FB
921 error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
922 eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
31e31b8a
FB
923 elf_prot,
924 elf_type,
925 interpreter_fd,
54936004 926 eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
3b46e624 927
e89f07d3 928 if (error == -1) {
31e31b8a
FB
929 /* Real error */
930 close(interpreter_fd);
931 free(elf_phdata);
992f48a0 932 return ~((abi_ulong)0UL);
31e31b8a
FB
933 }
934
935 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
936 load_addr = error;
937 load_addr_set = 1;
938 }
939
940 /*
941 * Find the end of the file mapping for this phdr, and keep
942 * track of the largest address we see for this.
943 */
944 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
945 if (k > elf_bss) elf_bss = k;
946
947 /*
948 * Do the same thing for the memory mapping - between
949 * elf_bss and last_bss is the bss section.
950 */
951 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
952 if (k > last_bss) last_bss = k;
953 }
5fafdf24 954
31e31b8a
FB
955 /* Now use mmap to map the library into memory. */
956
957 close(interpreter_fd);
958
959 /*
960 * Now fill out the bss section. First pad the last page up
961 * to the page boundary, and then perform a mmap to make sure
962 * that there are zeromapped pages up to and including the last
963 * bss page.
964 */
768a4a36 965 padzero(elf_bss, last_bss);
83fb7adf 966 elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
31e31b8a
FB
967
968 /* Map the last of the bss segment */
969 if (last_bss > elf_bss) {
54936004
FB
970 target_mmap(elf_bss, last_bss-elf_bss,
971 PROT_READ|PROT_WRITE|PROT_EXEC,
972 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
31e31b8a
FB
973 }
974 free(elf_phdata);
975
976 *interp_load_addr = load_addr;
992f48a0 977 return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
31e31b8a
FB
978}
979
689f936f
FB
980/* Best attempt to load symbols from this ELF object. */
981static void load_symbols(struct elfhdr *hdr, int fd)
982{
983 unsigned int i;
984 struct elf_shdr sechdr, symtab, strtab;
985 char *strings;
e80cfcfc 986 struct syminfo *s;
0774bed1
BS
987#if (ELF_CLASS == ELFCLASS64)
988 // Disas uses 32 bit symbols
989 struct elf32_sym *syms32 = NULL;
990 struct elf_sym *sym;
991#endif
689f936f
FB
992
993 lseek(fd, hdr->e_shoff, SEEK_SET);
994 for (i = 0; i < hdr->e_shnum; i++) {
995 if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
996 return;
997#ifdef BSWAP_NEEDED
998 bswap_shdr(&sechdr);
999#endif
1000 if (sechdr.sh_type == SHT_SYMTAB) {
1001 symtab = sechdr;
1002 lseek(fd, hdr->e_shoff
1003 + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
1004 if (read(fd, &strtab, sizeof(strtab))
1005 != sizeof(strtab))
1006 return;
1007#ifdef BSWAP_NEEDED
1008 bswap_shdr(&strtab);
1009#endif
1010 goto found;
1011 }
1012 }
1013 return; /* Shouldn't happen... */
1014
1015 found:
1016 /* Now know where the strtab and symtab are. Snarf them. */
e80cfcfc
FB
1017 s = malloc(sizeof(*s));
1018 s->disas_symtab = malloc(symtab.sh_size);
0774bed1
BS
1019#if (ELF_CLASS == ELFCLASS64)
1020 syms32 = malloc(symtab.sh_size / sizeof(struct elf_sym)
1021 * sizeof(struct elf32_sym));
1022#endif
e80cfcfc
FB
1023 s->disas_strtab = strings = malloc(strtab.sh_size);
1024 if (!s->disas_symtab || !s->disas_strtab)
689f936f 1025 return;
5fafdf24 1026
689f936f 1027 lseek(fd, symtab.sh_offset, SEEK_SET);
e80cfcfc 1028 if (read(fd, s->disas_symtab, symtab.sh_size) != symtab.sh_size)
689f936f 1029 return;
31e31b8a 1030
0774bed1 1031 for (i = 0; i < symtab.sh_size / sizeof(struct elf_sym); i++) {
689f936f 1032#ifdef BSWAP_NEEDED
e80cfcfc 1033 bswap_sym(s->disas_symtab + sizeof(struct elf_sym)*i);
689f936f 1034#endif
0774bed1
BS
1035#if (ELF_CLASS == ELFCLASS64)
1036 sym = s->disas_symtab + sizeof(struct elf_sym)*i;
1037 syms32[i].st_name = sym->st_name;
1038 syms32[i].st_info = sym->st_info;
1039 syms32[i].st_other = sym->st_other;
1040 syms32[i].st_shndx = sym->st_shndx;
1041 syms32[i].st_value = sym->st_value & 0xffffffff;
1042 syms32[i].st_size = sym->st_size & 0xffffffff;
1043#endif
1044 }
689f936f 1045
0774bed1
BS
1046#if (ELF_CLASS == ELFCLASS64)
1047 free(s->disas_symtab);
1048 s->disas_symtab = syms32;
1049#endif
689f936f
FB
1050 lseek(fd, strtab.sh_offset, SEEK_SET);
1051 if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
1052 return;
e80cfcfc
FB
1053 s->disas_num_syms = symtab.sh_size / sizeof(struct elf_sym);
1054 s->next = syminfos;
1055 syminfos = s;
689f936f 1056}
31e31b8a 1057
e5fe0c52
PB
1058int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
1059 struct image_info * info)
31e31b8a
FB
1060{
1061 struct elfhdr elf_ex;
1062 struct elfhdr interp_elf_ex;
1063 struct exec interp_ex;
1064 int interpreter_fd = -1; /* avoid warning */
992f48a0 1065 abi_ulong load_addr, load_bias;
31e31b8a
FB
1066 int load_addr_set = 0;
1067 unsigned int interpreter_type = INTERPRETER_NONE;
1068 unsigned char ibcs2_interpreter;
1069 int i;
992f48a0 1070 abi_ulong mapped_addr;
31e31b8a
FB
1071 struct elf_phdr * elf_ppnt;
1072 struct elf_phdr *elf_phdata;
992f48a0 1073 abi_ulong elf_bss, k, elf_brk;
31e31b8a
FB
1074 int retval;
1075 char * elf_interpreter;
992f48a0 1076 abi_ulong elf_entry, interp_load_addr = 0;
31e31b8a 1077 int status;
992f48a0
BS
1078 abi_ulong start_code, end_code, start_data, end_data;
1079 abi_ulong reloc_func_desc = 0;
1080 abi_ulong elf_stack;
31e31b8a
FB
1081 char passed_fileno[6];
1082
1083 ibcs2_interpreter = 0;
1084 status = 0;
1085 load_addr = 0;
09bfb054 1086 load_bias = 0;
31e31b8a
FB
1087 elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
1088#ifdef BSWAP_NEEDED
1089 bswap_ehdr(&elf_ex);
1090#endif
1091
31e31b8a
FB
1092 /* First of all, some simple consistency checks */
1093 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
1094 (! elf_check_arch(elf_ex.e_machine))) {
1095 return -ENOEXEC;
1096 }
1097
e5fe0c52
PB
1098 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
1099 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
1100 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
1101 if (!bprm->p) {
1102 retval = -E2BIG;
1103 }
1104
31e31b8a 1105 /* Now read in all of the header information */
31e31b8a
FB
1106 elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
1107 if (elf_phdata == NULL) {
1108 return -ENOMEM;
1109 }
1110
1111 retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
1112 if(retval > 0) {
5fafdf24 1113 retval = read(bprm->fd, (char *) elf_phdata,
31e31b8a
FB
1114 elf_ex.e_phentsize * elf_ex.e_phnum);
1115 }
1116
1117 if (retval < 0) {
1118 perror("load_elf_binary");
1119 exit(-1);
1120 free (elf_phdata);
1121 return -errno;
1122 }
1123
b17780d5
FB
1124#ifdef BSWAP_NEEDED
1125 elf_ppnt = elf_phdata;
1126 for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
1127 bswap_phdr(elf_ppnt);
1128 }
1129#endif
31e31b8a
FB
1130 elf_ppnt = elf_phdata;
1131
1132 elf_bss = 0;
1133 elf_brk = 0;
1134
1135
992f48a0 1136 elf_stack = ~((abi_ulong)0UL);
31e31b8a 1137 elf_interpreter = NULL;
992f48a0 1138 start_code = ~((abi_ulong)0UL);
31e31b8a 1139 end_code = 0;
863cf0b7 1140 start_data = 0;
31e31b8a
FB
1141 end_data = 0;
1142
1143 for(i=0;i < elf_ex.e_phnum; i++) {
1144 if (elf_ppnt->p_type == PT_INTERP) {
1145 if ( elf_interpreter != NULL )
1146 {
1147 free (elf_phdata);
1148 free(elf_interpreter);
1149 close(bprm->fd);
1150 return -EINVAL;
1151 }
1152
1153 /* This is the program interpreter used for
1154 * shared libraries - for now assume that this
1155 * is an a.out format binary
1156 */
1157
32ce6337 1158 elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
31e31b8a
FB
1159
1160 if (elf_interpreter == NULL) {
1161 free (elf_phdata);
1162 close(bprm->fd);
1163 return -ENOMEM;
1164 }
1165
31e31b8a
FB
1166 retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
1167 if(retval >= 0) {
32ce6337 1168 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
31e31b8a
FB
1169 }
1170 if(retval < 0) {
1171 perror("load_elf_binary2");
1172 exit(-1);
5fafdf24 1173 }
31e31b8a
FB
1174
1175 /* If the program interpreter is one of these two,
1176 then assume an iBCS2 image. Otherwise assume
1177 a native linux image. */
1178
1179 /* JRP - Need to add X86 lib dir stuff here... */
1180
1181 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
1182 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
1183 ibcs2_interpreter = 1;
1184 }
1185
1186#if 0
1187 printf("Using ELF interpreter %s\n", elf_interpreter);
1188#endif
1189 if (retval >= 0) {
32ce6337 1190 retval = open(path(elf_interpreter), O_RDONLY);
31e31b8a
FB
1191 if(retval >= 0) {
1192 interpreter_fd = retval;
1193 }
1194 else {
1195 perror(elf_interpreter);
1196 exit(-1);
1197 /* retval = -errno; */
1198 }
1199 }
1200
1201 if (retval >= 0) {
1202 retval = lseek(interpreter_fd, 0, SEEK_SET);
1203 if(retval >= 0) {
1204 retval = read(interpreter_fd,bprm->buf,128);
1205 }
1206 }
1207 if (retval >= 0) {
1208 interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
1209 interp_elf_ex=*((struct elfhdr *) bprm->buf); /* elf exec-header */
1210 }
1211 if (retval < 0) {
1212 perror("load_elf_binary3");
1213 exit(-1);
1214 free (elf_phdata);
1215 free(elf_interpreter);
1216 close(bprm->fd);
1217 return retval;
1218 }
1219 }
1220 elf_ppnt++;
1221 }
1222
1223 /* Some simple consistency checks for the interpreter */
1224 if (elf_interpreter){
1225 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
1226
1227 /* Now figure out which format our binary is */
1228 if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
1229 (N_MAGIC(interp_ex) != QMAGIC)) {
1230 interpreter_type = INTERPRETER_ELF;
1231 }
1232
1233 if (interp_elf_ex.e_ident[0] != 0x7f ||
1234 strncmp(&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
1235 interpreter_type &= ~INTERPRETER_ELF;
1236 }
1237
1238 if (!interpreter_type) {
1239 free(elf_interpreter);
1240 free(elf_phdata);
1241 close(bprm->fd);
1242 return -ELIBBAD;
1243 }
1244 }
1245
1246 /* OK, we are done with that, now set up the arg stuff,
1247 and then start this sucker up */
1248
e5fe0c52 1249 {
31e31b8a
FB
1250 char * passed_p;
1251
1252 if (interpreter_type == INTERPRETER_AOUT) {
eba2af63 1253 snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
31e31b8a
FB
1254 passed_p = passed_fileno;
1255
1256 if (elf_interpreter) {
e5fe0c52 1257 bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
31e31b8a
FB
1258 bprm->argc++;
1259 }
1260 }
1261 if (!bprm->p) {
1262 if (elf_interpreter) {
1263 free(elf_interpreter);
1264 }
1265 free (elf_phdata);
1266 close(bprm->fd);
1267 return -E2BIG;
1268 }
1269 }
1270
1271 /* OK, This is the point of no return */
1272 info->end_data = 0;
1273 info->end_code = 0;
992f48a0 1274 info->start_mmap = (abi_ulong)ELF_START_MMAP;
31e31b8a 1275 info->mmap = 0;
992f48a0 1276 elf_entry = (abi_ulong) elf_ex.e_entry;
31e31b8a
FB
1277
1278 /* Do this so that we can load the interpreter, if need be. We will
1279 change some of these later */
1280 info->rss = 0;
1281 bprm->p = setup_arg_pages(bprm->p, bprm, info);
1282 info->start_stack = bprm->p;
1283
1284 /* Now we do a little grungy work by mmaping the ELF image into
1285 * the correct location in memory. At this point, we assume that
1286 * the image should be loaded at fixed address, not at a variable
1287 * address.
1288 */
1289
31e31b8a 1290 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
09bfb054
FB
1291 int elf_prot = 0;
1292 int elf_flags = 0;
992f48a0 1293 abi_ulong error;
3b46e624 1294
09bfb054
FB
1295 if (elf_ppnt->p_type != PT_LOAD)
1296 continue;
3b46e624 1297
09bfb054
FB
1298 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1299 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1300 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1301 elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
1302 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
1303 elf_flags |= MAP_FIXED;
1304 } else if (elf_ex.e_type == ET_DYN) {
1305 /* Try and get dynamic programs out of the way of the default mmap
1306 base, as well as whatever program they might try to exec. This
1307 is because the brk will follow the loader, and is not movable. */
1308 /* NOTE: for qemu, we do a big mmap to get enough space
e91c8a77 1309 without hardcoding any address */
54936004 1310 error = target_mmap(0, ET_DYN_MAP_SIZE,
5fafdf24 1311 PROT_NONE, MAP_PRIVATE | MAP_ANON,
54936004 1312 -1, 0);
09bfb054
FB
1313 if (error == -1) {
1314 perror("mmap");
1315 exit(-1);
1316 }
54936004 1317 load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
09bfb054 1318 }
3b46e624 1319
54936004
FB
1320 error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1321 (elf_ppnt->p_filesz +
1322 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1323 elf_prot,
1324 (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1325 bprm->fd,
5fafdf24 1326 (elf_ppnt->p_offset -
54936004 1327 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
09bfb054
FB
1328 if (error == -1) {
1329 perror("mmap");
1330 exit(-1);
1331 }
31e31b8a
FB
1332
1333#ifdef LOW_ELF_STACK
54936004
FB
1334 if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1335 elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
31e31b8a 1336#endif
3b46e624 1337
09bfb054
FB
1338 if (!load_addr_set) {
1339 load_addr_set = 1;
1340 load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1341 if (elf_ex.e_type == ET_DYN) {
1342 load_bias += error -
54936004 1343 TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
09bfb054 1344 load_addr += load_bias;
84409ddb 1345 reloc_func_desc = load_bias;
09bfb054
FB
1346 }
1347 }
1348 k = elf_ppnt->p_vaddr;
5fafdf24 1349 if (k < start_code)
09bfb054 1350 start_code = k;
863cf0b7
JM
1351 if (start_data < k)
1352 start_data = k;
09bfb054 1353 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
5fafdf24 1354 if (k > elf_bss)
09bfb054
FB
1355 elf_bss = k;
1356 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
1357 end_code = k;
5fafdf24 1358 if (end_data < k)
09bfb054
FB
1359 end_data = k;
1360 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1361 if (k > elf_brk) elf_brk = k;
31e31b8a
FB
1362 }
1363
09bfb054
FB
1364 elf_entry += load_bias;
1365 elf_bss += load_bias;
1366 elf_brk += load_bias;
1367 start_code += load_bias;
1368 end_code += load_bias;
863cf0b7 1369 start_data += load_bias;
09bfb054
FB
1370 end_data += load_bias;
1371
31e31b8a
FB
1372 if (elf_interpreter) {
1373 if (interpreter_type & 1) {
1374 elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1375 }
1376 else if (interpreter_type & 2) {
1377 elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1378 &interp_load_addr);
1379 }
84409ddb 1380 reloc_func_desc = interp_load_addr;
31e31b8a
FB
1381
1382 close(interpreter_fd);
1383 free(elf_interpreter);
1384
992f48a0 1385 if (elf_entry == ~((abi_ulong)0UL)) {
31e31b8a
FB
1386 printf("Unable to load interpreter\n");
1387 free(elf_phdata);
1388 exit(-1);
1389 return 0;
1390 }
1391 }
1392
1393 free(elf_phdata);
1394
689f936f
FB
1395 if (loglevel)
1396 load_symbols(&elf_ex, bprm->fd);
1397
31e31b8a
FB
1398 if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
1399 info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
1400
1401#ifdef LOW_ELF_STACK
1402 info->start_stack = bprm->p = elf_stack - 4;
1403#endif
53a5960a 1404 bprm->p = create_elf_tables(bprm->p,
31e31b8a
FB
1405 bprm->argc,
1406 bprm->envc,
a1516e92 1407 &elf_ex,
09bfb054 1408 load_addr, load_bias,
31e31b8a
FB
1409 interp_load_addr,
1410 (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1411 info);
92a343da 1412 info->load_addr = reloc_func_desc;
31e31b8a
FB
1413 info->start_brk = info->brk = elf_brk;
1414 info->end_code = end_code;
1415 info->start_code = start_code;
863cf0b7 1416 info->start_data = start_data;
31e31b8a
FB
1417 info->end_data = end_data;
1418 info->start_stack = bprm->p;
1419
1420 /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1421 sections */
1422 set_brk(elf_bss, elf_brk);
1423
768a4a36 1424 padzero(elf_bss, elf_brk);
31e31b8a
FB
1425
1426#if 0
1427 printf("(start_brk) %x\n" , info->start_brk);
1428 printf("(end_code) %x\n" , info->end_code);
1429 printf("(start_code) %x\n" , info->start_code);
1430 printf("(end_data) %x\n" , info->end_data);
1431 printf("(start_stack) %x\n" , info->start_stack);
1432 printf("(brk) %x\n" , info->brk);
1433#endif
1434
1435 if ( info->personality == PER_SVR4 )
1436 {
1437 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1438 and some applications "depend" upon this behavior.
1439 Since we do not have the power to recompile these, we
1440 emulate the SVr4 behavior. Sigh. */
83fb7adf 1441 mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
54936004 1442 MAP_FIXED | MAP_PRIVATE, -1, 0);
31e31b8a
FB
1443 }
1444
31e31b8a
FB
1445 info->entry = elf_entry;
1446
1447 return 0;
1448}
1449
31e31b8a
FB
1450static int load_aout_interp(void * exptr, int interp_fd)
1451{
1452 printf("a.out interpreter not yet supported\n");
1453 return(0);
1454}
1455
e5fe0c52
PB
1456void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
1457{
1458 init_thread(regs, infop);
1459}