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