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