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