blob: 2a7bd703c51720978713e12a781441ae2152f0cf [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/* 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>
bellard31e31b82003-02-18 22:55:36 +00006#include <errno.h>
7#include <unistd.h>
8#include <sys/mman.h>
9#include <stdlib.h>
10#include <string.h>
11
bellard3ef693a2003-03-23 20:17:16 +000012#include "qemu.h"
bellard689f9362003-04-29 20:40:07 +000013#include "disas.h"
bellard31e31b82003-02-18 22:55:36 +000014
bellard83fb7ad2004-07-05 21:25:26 +000015/* this flag is uneffective under linux too, should be deleted */
16#ifndef MAP_DENYWRITE
17#define MAP_DENYWRITE 0
18#endif
19
20/* should probably go in elf.h */
21#ifndef ELIBBAD
22#define ELIBBAD 80
23#endif
24
bellard30ac07d2003-04-07 21:33:03 +000025#ifdef TARGET_I386
26
bellard15338fd2005-11-26 11:41:16 +000027#define ELF_PLATFORM get_elf_platform()
28
29static const char *get_elf_platform(void)
30{
31 static char elf_platform[] = "i386";
32 int family = (global_env->cpuid_version >> 8) & 0xff;
33 if (family > 6)
34 family = 6;
35 if (family >= 3)
36 elf_platform[1] = '0' + family;
37 return elf_platform;
38}
39
40#define ELF_HWCAP get_elf_hwcap()
41
42static uint32_t get_elf_hwcap(void)
43{
44 return global_env->cpuid_features;
45}
46
j_mayer84409dd2007-04-06 08:56:50 +000047#ifdef TARGET_X86_64
48#define ELF_START_MMAP 0x2aaaaab000ULL
49#define elf_check_arch(x) ( ((x) == ELF_ARCH) )
50
51#define ELF_CLASS ELFCLASS64
52#define ELF_DATA ELFDATA2LSB
53#define ELF_ARCH EM_X86_64
54
55static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
56{
57 regs->rax = 0;
58 regs->rsp = infop->start_stack;
59 regs->rip = infop->entry;
60}
61
62#else
63
bellard30ac07d2003-04-07 21:33:03 +000064#define ELF_START_MMAP 0x80000000
65
bellard30ac07d2003-04-07 21:33:03 +000066/*
67 * This is used to ensure we don't load something for the wrong architecture.
68 */
69#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
70
71/*
72 * These are used to set parameters in the core dumps.
73 */
74#define ELF_CLASS ELFCLASS32
75#define ELF_DATA ELFDATA2LSB
76#define ELF_ARCH EM_386
77
bellardb346ff42003-06-15 20:05:50 +000078static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
79{
80 regs->esp = infop->start_stack;
81 regs->eip = infop->entry;
pbrooke5fe0c52006-06-11 13:32:59 +000082
83 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
84 starts %edx contains a pointer to a function which might be
85 registered using `atexit'. This provides a mean for the
86 dynamic linker to call DT_FINI functions for shared libraries
87 that have been loaded before the code runs.
88
89 A value of 0 tells we have no such handler. */
90 regs->edx = 0;
bellardb346ff42003-06-15 20:05:50 +000091}
j_mayer84409dd2007-04-06 08:56:50 +000092#endif
bellardb346ff42003-06-15 20:05:50 +000093
94#define USE_ELF_CORE_DUMP
95#define ELF_EXEC_PAGESIZE 4096
96
97#endif
98
99#ifdef TARGET_ARM
100
101#define ELF_START_MMAP 0x80000000
102
103#define elf_check_arch(x) ( (x) == EM_ARM )
104
105#define ELF_CLASS ELFCLASS32
106#ifdef TARGET_WORDS_BIGENDIAN
107#define ELF_DATA ELFDATA2MSB
108#else
109#define ELF_DATA ELFDATA2LSB
110#endif
111#define ELF_ARCH EM_ARM
112
bellardb346ff42003-06-15 20:05:50 +0000113static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
114{
pbrook53a59602006-03-25 19:31:22 +0000115 target_long stack = infop->start_stack;
bellardb346ff42003-06-15 20:05:50 +0000116 memset(regs, 0, sizeof(*regs));
117 regs->ARM_cpsr = 0x10;
pbrook0240ded2006-02-04 19:30:51 +0000118 if (infop->entry & 1)
119 regs->ARM_cpsr |= CPSR_T;
120 regs->ARM_pc = infop->entry & 0xfffffffe;
bellardb346ff42003-06-15 20:05:50 +0000121 regs->ARM_sp = infop->start_stack;
pbrook53a59602006-03-25 19:31:22 +0000122 regs->ARM_r2 = tgetl(stack + 8); /* envp */
123 regs->ARM_r1 = tgetl(stack + 4); /* envp */
bellarda1516e92003-07-09 17:13:37 +0000124 /* XXX: it seems that r0 is zeroed after ! */
pbrooke5fe0c52006-06-11 13:32:59 +0000125 regs->ARM_r0 = 0;
126 /* For uClinux PIC binaries. */
127 regs->ARM_r10 = infop->start_data;
bellardb346ff42003-06-15 20:05:50 +0000128}
129
bellard30ac07d2003-04-07 21:33:03 +0000130#define USE_ELF_CORE_DUMP
131#define ELF_EXEC_PAGESIZE 4096
132
bellardafce2922005-10-30 20:58:30 +0000133enum
134{
135 ARM_HWCAP_ARM_SWP = 1 << 0,
136 ARM_HWCAP_ARM_HALF = 1 << 1,
137 ARM_HWCAP_ARM_THUMB = 1 << 2,
138 ARM_HWCAP_ARM_26BIT = 1 << 3,
139 ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
140 ARM_HWCAP_ARM_FPA = 1 << 5,
141 ARM_HWCAP_ARM_VFP = 1 << 6,
142 ARM_HWCAP_ARM_EDSP = 1 << 7,
143};
144
bellard15338fd2005-11-26 11:41:16 +0000145#define ELF_HWCAP (ARM_HWCAP_ARM_SWP | ARM_HWCAP_ARM_HALF \
bellardafce2922005-10-30 20:58:30 +0000146 | ARM_HWCAP_ARM_THUMB | ARM_HWCAP_ARM_FAST_MULT \
147 | ARM_HWCAP_ARM_FPA | ARM_HWCAP_ARM_VFP)
148
bellard30ac07d2003-04-07 21:33:03 +0000149#endif
150
bellard853d6f72003-09-30 20:58:32 +0000151#ifdef TARGET_SPARC
bellarda315a142005-01-30 22:59:18 +0000152#ifdef TARGET_SPARC64
bellard853d6f72003-09-30 20:58:32 +0000153
154#define ELF_START_MMAP 0x80000000
155
bellard5ef54112006-07-18 21:14:09 +0000156#define elf_check_arch(x) ( (x) == EM_SPARCV9 )
bellard853d6f72003-09-30 20:58:32 +0000157
bellarda315a142005-01-30 22:59:18 +0000158#define ELF_CLASS ELFCLASS64
159#define ELF_DATA ELFDATA2MSB
bellard5ef54112006-07-18 21:14:09 +0000160#define ELF_ARCH EM_SPARCV9
161
162#define STACK_BIAS 2047
bellarda315a142005-01-30 22:59:18 +0000163
bellarda315a142005-01-30 22:59:18 +0000164static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
165{
166 regs->tstate = 0;
167 regs->pc = infop->entry;
168 regs->npc = regs->pc + 4;
169 regs->y = 0;
bellard5ef54112006-07-18 21:14:09 +0000170 regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
bellarda315a142005-01-30 22:59:18 +0000171}
172
173#else
174#define ELF_START_MMAP 0x80000000
175
176#define elf_check_arch(x) ( (x) == EM_SPARC )
177
bellard853d6f72003-09-30 20:58:32 +0000178#define ELF_CLASS ELFCLASS32
179#define ELF_DATA ELFDATA2MSB
180#define ELF_ARCH EM_SPARC
181
bellard853d6f72003-09-30 20:58:32 +0000182static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
183{
bellardf5155282004-01-04 15:46:50 +0000184 regs->psr = 0;
185 regs->pc = infop->entry;
186 regs->npc = regs->pc + 4;
187 regs->y = 0;
188 regs->u_regs[14] = infop->start_stack - 16 * 4;
bellard853d6f72003-09-30 20:58:32 +0000189}
190
191#endif
bellarda315a142005-01-30 22:59:18 +0000192#endif
bellard853d6f72003-09-30 20:58:32 +0000193
bellard67867302003-11-23 17:05:30 +0000194#ifdef TARGET_PPC
195
196#define ELF_START_MMAP 0x80000000
197
j_mayer84409dd2007-04-06 08:56:50 +0000198#ifdef TARGET_PPC64
199
200#define elf_check_arch(x) ( (x) == EM_PPC64 )
201
202#define ELF_CLASS ELFCLASS64
203
204#else
205
bellard67867302003-11-23 17:05:30 +0000206#define elf_check_arch(x) ( (x) == EM_PPC )
207
208#define ELF_CLASS ELFCLASS32
j_mayer84409dd2007-04-06 08:56:50 +0000209
210#endif
211
bellard67867302003-11-23 17:05:30 +0000212#ifdef TARGET_WORDS_BIGENDIAN
213#define ELF_DATA ELFDATA2MSB
214#else
215#define ELF_DATA ELFDATA2LSB
216#endif
217#define ELF_ARCH EM_PPC
218
bellardf5155282004-01-04 15:46:50 +0000219/*
220 * We need to put in some extra aux table entries to tell glibc what
221 * the cache block size is, so it can use the dcbz instruction safely.
222 */
223#define AT_DCACHEBSIZE 19
224#define AT_ICACHEBSIZE 20
225#define AT_UCACHEBSIZE 21
226/* A special ignored type value for PPC, for glibc compatibility. */
227#define AT_IGNOREPPC 22
228/*
229 * The requirements here are:
230 * - keep the final alignment of sp (sp & 0xf)
231 * - make sure the 32-bit value at the first 16 byte aligned position of
232 * AUXV is greater than 16 for glibc compatibility.
233 * AT_IGNOREPPC is used for that.
234 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
235 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
236 */
bellard0bccf032005-08-21 10:12:28 +0000237#define DLINFO_ARCH_ITEMS 5
bellardf5155282004-01-04 15:46:50 +0000238#define ARCH_DLINFO \
239do { \
bellard0bccf032005-08-21 10:12:28 +0000240 NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
241 NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
242 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
bellardf5155282004-01-04 15:46:50 +0000243 /* \
244 * Now handle glibc compatibility. \
245 */ \
bellard0bccf032005-08-21 10:12:28 +0000246 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
247 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
bellardf5155282004-01-04 15:46:50 +0000248 } while (0)
249
bellard67867302003-11-23 17:05:30 +0000250static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
251{
pbrooke5fe0c52006-06-11 13:32:59 +0000252 target_ulong pos = infop->start_stack;
253 target_ulong tmp;
j_mayer84409dd2007-04-06 08:56:50 +0000254#ifdef TARGET_PPC64
255 target_ulong entry, toc;
256#endif
pbrooke5fe0c52006-06-11 13:32:59 +0000257
bellard67867302003-11-23 17:05:30 +0000258 _regs->msr = 1 << MSR_PR; /* Set user mode */
259 _regs->gpr[1] = infop->start_stack;
j_mayer84409dd2007-04-06 08:56:50 +0000260#ifdef TARGET_PPC64
261 entry = ldq_raw(infop->entry) + infop->load_addr;
262 toc = ldq_raw(infop->entry + 8) + infop->load_addr;
263 _regs->gpr[2] = toc;
264 infop->entry = entry;
265#endif
bellard67867302003-11-23 17:05:30 +0000266 _regs->nip = infop->entry;
pbrooke5fe0c52006-06-11 13:32:59 +0000267 /* Note that isn't exactly what regular kernel does
268 * but this is what the ABI wants and is needed to allow
269 * execution of PPC BSD programs.
270 */
271 _regs->gpr[3] = tgetl(pos);
272 pos += sizeof(target_ulong);
273 _regs->gpr[4] = pos;
274 for (tmp = 1; tmp != 0; pos += sizeof(target_ulong))
275 tmp = ldl(pos);
276 _regs->gpr[5] = pos;
bellard67867302003-11-23 17:05:30 +0000277}
278
279#define USE_ELF_CORE_DUMP
280#define ELF_EXEC_PAGESIZE 4096
281
282#endif
283
bellard048f6b42005-11-26 18:47:20 +0000284#ifdef TARGET_MIPS
285
286#define ELF_START_MMAP 0x80000000
287
288#define elf_check_arch(x) ( (x) == EM_MIPS )
289
ths388bb212007-05-13 13:58:00 +0000290#ifdef TARGET_MIPS64
291#define ELF_CLASS ELFCLASS64
292#else
bellard048f6b42005-11-26 18:47:20 +0000293#define ELF_CLASS ELFCLASS32
ths388bb212007-05-13 13:58:00 +0000294#endif
bellard048f6b42005-11-26 18:47:20 +0000295#ifdef TARGET_WORDS_BIGENDIAN
296#define ELF_DATA ELFDATA2MSB
297#else
298#define ELF_DATA ELFDATA2LSB
299#endif
300#define ELF_ARCH EM_MIPS
301
bellard048f6b42005-11-26 18:47:20 +0000302static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
303{
304 regs->cp0_status = CP0St_UM;
305 regs->cp0_epc = infop->entry;
306 regs->regs[29] = infop->start_stack;
307}
308
ths388bb212007-05-13 13:58:00 +0000309#define USE_ELF_CORE_DUMP
310#define ELF_EXEC_PAGESIZE 4096
311
bellard048f6b42005-11-26 18:47:20 +0000312#endif /* TARGET_MIPS */
313
bellardfdf9b3e2006-04-27 21:07:38 +0000314#ifdef TARGET_SH4
315
316#define ELF_START_MMAP 0x80000000
317
318#define elf_check_arch(x) ( (x) == EM_SH )
319
320#define ELF_CLASS ELFCLASS32
321#define ELF_DATA ELFDATA2LSB
322#define ELF_ARCH EM_SH
323
bellardfdf9b3e2006-04-27 21:07:38 +0000324static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
325{
326 /* Check other registers XXXXX */
327 regs->pc = infop->entry;
ths072ae842007-06-22 10:13:51 +0000328 regs->regs[15] = infop->start_stack;
bellardfdf9b3e2006-04-27 21:07:38 +0000329}
330
331#define USE_ELF_CORE_DUMP
332#define ELF_EXEC_PAGESIZE 4096
333
334#endif
335
pbrooke6e59062006-10-22 00:18:54 +0000336#ifdef TARGET_M68K
337
338#define ELF_START_MMAP 0x80000000
339
340#define elf_check_arch(x) ( (x) == EM_68K )
341
342#define ELF_CLASS ELFCLASS32
343#define ELF_DATA ELFDATA2MSB
344#define ELF_ARCH EM_68K
345
346/* ??? Does this need to do anything?
347#define ELF_PLAT_INIT(_r) */
348
349static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
350{
351 regs->usp = infop->start_stack;
352 regs->sr = 0;
353 regs->pc = infop->entry;
354}
355
356#define USE_ELF_CORE_DUMP
357#define ELF_EXEC_PAGESIZE 8192
358
359#endif
360
j_mayer7a3148a2007-04-05 07:13:51 +0000361#ifdef TARGET_ALPHA
362
363#define ELF_START_MMAP (0x30000000000ULL)
364
365#define elf_check_arch(x) ( (x) == ELF_ARCH )
366
367#define ELF_CLASS ELFCLASS64
368#define ELF_DATA ELFDATA2MSB
369#define ELF_ARCH EM_ALPHA
370
371static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
372{
373 regs->pc = infop->entry;
374 regs->ps = 8;
375 regs->usp = infop->start_stack;
376 regs->unique = infop->start_data; /* ? */
377 printf("Set unique value to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n",
378 regs->unique, infop->start_data);
379}
380
381#define USE_ELF_CORE_DUMP
382#define ELF_EXEC_PAGESIZE 8192
383
384#endif /* TARGET_ALPHA */
385
bellard15338fd2005-11-26 11:41:16 +0000386#ifndef ELF_PLATFORM
387#define ELF_PLATFORM (NULL)
388#endif
389
390#ifndef ELF_HWCAP
391#define ELF_HWCAP 0
392#endif
393
bellard31e31b82003-02-18 22:55:36 +0000394#include "elf.h"
bellard09bfb052003-04-10 00:03:40 +0000395
bellard09bfb052003-04-10 00:03:40 +0000396struct exec
397{
398 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
399 unsigned int a_text; /* length of text, in bytes */
400 unsigned int a_data; /* length of data, in bytes */
401 unsigned int a_bss; /* length of uninitialized data area, in bytes */
402 unsigned int a_syms; /* length of symbol table data in file, in bytes */
403 unsigned int a_entry; /* start address */
404 unsigned int a_trsize; /* length of relocation info for text, in bytes */
405 unsigned int a_drsize; /* length of relocation info for data, in bytes */
406};
407
408
409#define N_MAGIC(exec) ((exec).a_info & 0xffff)
410#define OMAGIC 0407
411#define NMAGIC 0410
412#define ZMAGIC 0413
413#define QMAGIC 0314
414
bellard09bfb052003-04-10 00:03:40 +0000415/* max code+data+bss space allocated to elf interpreter */
416#define INTERP_MAP_SIZE (32 * 1024 * 1024)
417
418/* max code+data+bss+brk space allocated to ET_DYN executables */
419#define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
420
421/* from personality.h */
422
423/* Flags for bug emulation. These occupy the top three bytes. */
424#define STICKY_TIMEOUTS 0x4000000
425#define WHOLE_SECONDS 0x2000000
426
427/* Personality types. These go in the low byte. Avoid using the top bit,
428 * it will conflict with error returns.
429 */
430#define PER_MASK (0x00ff)
431#define PER_LINUX (0x0000)
432#define PER_SVR4 (0x0001 | STICKY_TIMEOUTS)
433#define PER_SVR3 (0x0002 | STICKY_TIMEOUTS)
434#define PER_SCOSVR3 (0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS)
435#define PER_WYSEV386 (0x0004 | STICKY_TIMEOUTS)
436#define PER_ISCR4 (0x0005 | STICKY_TIMEOUTS)
437#define PER_BSD (0x0006)
438#define PER_XENIX (0x0007 | STICKY_TIMEOUTS)
bellard31e31b82003-02-18 22:55:36 +0000439
440/* Necessary parameters */
bellard54936002003-05-13 00:25:15 +0000441#define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
442#define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
443#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
bellard31e31b82003-02-18 22:55:36 +0000444
445#define INTERPRETER_NONE 0
446#define INTERPRETER_AOUT 1
447#define INTERPRETER_ELF 2
448
bellard15338fd2005-11-26 11:41:16 +0000449#define DLINFO_ITEMS 12
bellard31e31b82003-02-18 22:55:36 +0000450
bellard09bfb052003-04-10 00:03:40 +0000451static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
452{
453 memcpy(to, from, n);
454}
455
bellard31e31b82003-02-18 22:55:36 +0000456extern unsigned long x86_stack_size;
457
458static int load_aout_interp(void * exptr, int interp_fd);
459
460#ifdef BSWAP_NEEDED
bellard92a31b12005-02-10 22:00:52 +0000461static void bswap_ehdr(struct elfhdr *ehdr)
bellard31e31b82003-02-18 22:55:36 +0000462{
463 bswap16s(&ehdr->e_type); /* Object file type */
464 bswap16s(&ehdr->e_machine); /* Architecture */
465 bswap32s(&ehdr->e_version); /* Object file version */
bellard92a31b12005-02-10 22:00:52 +0000466 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
467 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
468 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
bellard31e31b82003-02-18 22:55:36 +0000469 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
470 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
471 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
472 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
473 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
474 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
475 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
476}
477
bellard92a31b12005-02-10 22:00:52 +0000478static void bswap_phdr(struct elf_phdr *phdr)
bellard31e31b82003-02-18 22:55:36 +0000479{
480 bswap32s(&phdr->p_type); /* Segment type */
bellard92a31b12005-02-10 22:00:52 +0000481 bswaptls(&phdr->p_offset); /* Segment file offset */
482 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
483 bswaptls(&phdr->p_paddr); /* Segment physical address */
484 bswaptls(&phdr->p_filesz); /* Segment size in file */
485 bswaptls(&phdr->p_memsz); /* Segment size in memory */
bellard31e31b82003-02-18 22:55:36 +0000486 bswap32s(&phdr->p_flags); /* Segment flags */
bellard92a31b12005-02-10 22:00:52 +0000487 bswaptls(&phdr->p_align); /* Segment alignment */
bellard31e31b82003-02-18 22:55:36 +0000488}
bellard689f9362003-04-29 20:40:07 +0000489
bellard92a31b12005-02-10 22:00:52 +0000490static void bswap_shdr(struct elf_shdr *shdr)
bellard689f9362003-04-29 20:40:07 +0000491{
492 bswap32s(&shdr->sh_name);
493 bswap32s(&shdr->sh_type);
bellard92a31b12005-02-10 22:00:52 +0000494 bswaptls(&shdr->sh_flags);
495 bswaptls(&shdr->sh_addr);
496 bswaptls(&shdr->sh_offset);
497 bswaptls(&shdr->sh_size);
bellard689f9362003-04-29 20:40:07 +0000498 bswap32s(&shdr->sh_link);
499 bswap32s(&shdr->sh_info);
bellard92a31b12005-02-10 22:00:52 +0000500 bswaptls(&shdr->sh_addralign);
501 bswaptls(&shdr->sh_entsize);
bellard689f9362003-04-29 20:40:07 +0000502}
503
j_mayer7a3148a2007-04-05 07:13:51 +0000504static void bswap_sym(struct elf_sym *sym)
bellard689f9362003-04-29 20:40:07 +0000505{
506 bswap32s(&sym->st_name);
j_mayer7a3148a2007-04-05 07:13:51 +0000507 bswaptls(&sym->st_value);
508 bswaptls(&sym->st_size);
bellard689f9362003-04-29 20:40:07 +0000509 bswap16s(&sym->st_shndx);
510}
bellard31e31b82003-02-18 22:55:36 +0000511#endif
512
bellard31e31b82003-02-18 22:55:36 +0000513/*
pbrooke5fe0c52006-06-11 13:32:59 +0000514 * 'copy_elf_strings()' copies argument/envelope strings from user
bellard31e31b82003-02-18 22:55:36 +0000515 * memory to free pages in kernel mem. These are in a format ready
516 * to be put directly into the top of new user memory.
517 *
518 */
pbrooke5fe0c52006-06-11 13:32:59 +0000519static unsigned long copy_elf_strings(int argc,char ** argv, void **page,
520 unsigned long p)
bellard31e31b82003-02-18 22:55:36 +0000521{
522 char *tmp, *tmp1, *pag = NULL;
523 int len, offset = 0;
524
525 if (!p) {
526 return 0; /* bullet-proofing */
527 }
528 while (argc-- > 0) {
bellardedf779f2004-02-22 13:40:13 +0000529 tmp = argv[argc];
530 if (!tmp) {
bellard31e31b82003-02-18 22:55:36 +0000531 fprintf(stderr, "VFS: argc is wrong");
532 exit(-1);
533 }
bellardedf779f2004-02-22 13:40:13 +0000534 tmp1 = tmp;
535 while (*tmp++);
bellard31e31b82003-02-18 22:55:36 +0000536 len = tmp - tmp1;
537 if (p < len) { /* this shouldn't happen - 128kB */
538 return 0;
539 }
540 while (len) {
541 --p; --tmp; --len;
542 if (--offset < 0) {
bellard54936002003-05-13 00:25:15 +0000543 offset = p % TARGET_PAGE_SIZE;
pbrook53a59602006-03-25 19:31:22 +0000544 pag = (char *)page[p/TARGET_PAGE_SIZE];
bellard44a91ca2004-01-18 22:05:44 +0000545 if (!pag) {
pbrook53a59602006-03-25 19:31:22 +0000546 pag = (char *)malloc(TARGET_PAGE_SIZE);
547 page[p/TARGET_PAGE_SIZE] = pag;
bellard44a91ca2004-01-18 22:05:44 +0000548 if (!pag)
549 return 0;
bellard31e31b82003-02-18 22:55:36 +0000550 }
551 }
552 if (len == 0 || offset == 0) {
bellardedf779f2004-02-22 13:40:13 +0000553 *(pag + offset) = *tmp;
bellard31e31b82003-02-18 22:55:36 +0000554 }
555 else {
556 int bytes_to_copy = (len > offset) ? offset : len;
557 tmp -= bytes_to_copy;
558 p -= bytes_to_copy;
559 offset -= bytes_to_copy;
560 len -= bytes_to_copy;
561 memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
562 }
563 }
564 }
565 return p;
566}
567
pbrook53a59602006-03-25 19:31:22 +0000568unsigned long setup_arg_pages(target_ulong p, struct linux_binprm * bprm,
569 struct image_info * info)
570{
571 target_ulong stack_base, size, error;
bellard31e31b82003-02-18 22:55:36 +0000572 int i;
bellard31e31b82003-02-18 22:55:36 +0000573
574 /* Create enough stack to hold everything. If we don't use
575 * it for args, we'll use it for something else...
576 */
bellard09bfb052003-04-10 00:03:40 +0000577 size = x86_stack_size;
bellard54936002003-05-13 00:25:15 +0000578 if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE)
579 size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
580 error = target_mmap(0,
bellard83fb7ad2004-07-05 21:25:26 +0000581 size + qemu_host_page_size,
bellard54936002003-05-13 00:25:15 +0000582 PROT_READ | PROT_WRITE,
583 MAP_PRIVATE | MAP_ANONYMOUS,
584 -1, 0);
bellard09bfb052003-04-10 00:03:40 +0000585 if (error == -1) {
586 perror("stk mmap");
587 exit(-1);
bellard31e31b82003-02-18 22:55:36 +0000588 }
bellard09bfb052003-04-10 00:03:40 +0000589 /* we reserve one extra page at the top of the stack as guard */
bellard83fb7ad2004-07-05 21:25:26 +0000590 target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
bellard09bfb052003-04-10 00:03:40 +0000591
bellard54936002003-05-13 00:25:15 +0000592 stack_base = error + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
bellard09bfb052003-04-10 00:03:40 +0000593 p += stack_base;
594
bellard31e31b82003-02-18 22:55:36 +0000595 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
596 if (bprm->page[i]) {
597 info->rss++;
598
pbrook53a59602006-03-25 19:31:22 +0000599 memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
600 free(bprm->page[i]);
bellard31e31b82003-02-18 22:55:36 +0000601 }
pbrook53a59602006-03-25 19:31:22 +0000602 stack_base += TARGET_PAGE_SIZE;
bellard31e31b82003-02-18 22:55:36 +0000603 }
604 return p;
605}
606
607static void set_brk(unsigned long start, unsigned long end)
608{
609 /* page-align the start and end addresses... */
bellard54936002003-05-13 00:25:15 +0000610 start = HOST_PAGE_ALIGN(start);
611 end = HOST_PAGE_ALIGN(end);
bellard31e31b82003-02-18 22:55:36 +0000612 if (end <= start)
613 return;
bellard54936002003-05-13 00:25:15 +0000614 if(target_mmap(start, end - start,
615 PROT_READ | PROT_WRITE | PROT_EXEC,
616 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
bellard31e31b82003-02-18 22:55:36 +0000617 perror("cannot mmap brk");
618 exit(-1);
619 }
620}
621
622
bellard853d6f72003-09-30 20:58:32 +0000623/* We need to explicitly zero any fractional pages after the data
624 section (i.e. bss). This would contain the junk from the file that
625 should not be in memory. */
ths768a4a32006-12-14 13:32:11 +0000626static void padzero(unsigned long elf_bss, unsigned long last_bss)
bellard31e31b82003-02-18 22:55:36 +0000627{
628 unsigned long nbyte;
bellard31e31b82003-02-18 22:55:36 +0000629
ths768a4a32006-12-14 13:32:11 +0000630 if (elf_bss >= last_bss)
631 return;
632
bellard853d6f72003-09-30 20:58:32 +0000633 /* XXX: this is really a hack : if the real host page size is
634 smaller than the target page size, some pages after the end
635 of the file may not be mapped. A better fix would be to
636 patch target_mmap(), but it is more complicated as the file
637 size must be known */
bellard83fb7ad2004-07-05 21:25:26 +0000638 if (qemu_real_host_page_size < qemu_host_page_size) {
bellard853d6f72003-09-30 20:58:32 +0000639 unsigned long end_addr, end_addr1;
bellard83fb7ad2004-07-05 21:25:26 +0000640 end_addr1 = (elf_bss + qemu_real_host_page_size - 1) &
641 ~(qemu_real_host_page_size - 1);
bellard853d6f72003-09-30 20:58:32 +0000642 end_addr = HOST_PAGE_ALIGN(elf_bss);
643 if (end_addr1 < end_addr) {
644 mmap((void *)end_addr1, end_addr - end_addr1,
645 PROT_READ|PROT_WRITE|PROT_EXEC,
646 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
647 }
648 }
649
bellard83fb7ad2004-07-05 21:25:26 +0000650 nbyte = elf_bss & (qemu_host_page_size-1);
bellard31e31b82003-02-18 22:55:36 +0000651 if (nbyte) {
bellard83fb7ad2004-07-05 21:25:26 +0000652 nbyte = qemu_host_page_size - nbyte;
bellard31e31b82003-02-18 22:55:36 +0000653 do {
pbrook53a59602006-03-25 19:31:22 +0000654 tput8(elf_bss, 0);
655 elf_bss++;
bellard31e31b82003-02-18 22:55:36 +0000656 } while (--nbyte);
657 }
658}
659
bellardedf779f2004-02-22 13:40:13 +0000660
pbrook53a59602006-03-25 19:31:22 +0000661static unsigned long create_elf_tables(target_ulong p, int argc, int envc,
662 struct elfhdr * exec,
663 unsigned long load_addr,
664 unsigned long load_bias,
665 unsigned long interp_load_addr, int ibcs,
666 struct image_info *info)
667{
pbrook53a59602006-03-25 19:31:22 +0000668 target_ulong sp;
669 int size;
670 target_ulong u_platform;
671 const char *k_platform;
672 const int n = sizeof(target_ulong);
673
674 sp = p;
675 u_platform = 0;
bellard15338fd2005-11-26 11:41:16 +0000676 k_platform = ELF_PLATFORM;
677 if (k_platform) {
678 size_t len = strlen(k_platform) + 1;
pbrook53a59602006-03-25 19:31:22 +0000679 sp -= (len + n - 1) & ~(n - 1);
680 u_platform = sp;
681 memcpy_to_target(sp, k_platform, len);
bellard15338fd2005-11-26 11:41:16 +0000682 }
pbrook53a59602006-03-25 19:31:22 +0000683 /*
684 * Force 16 byte _final_ alignment here for generality.
685 */
686 sp = sp &~ (target_ulong)15;
687 size = (DLINFO_ITEMS + 1) * 2;
bellard15338fd2005-11-26 11:41:16 +0000688 if (k_platform)
pbrook53a59602006-03-25 19:31:22 +0000689 size += 2;
bellardf5155282004-01-04 15:46:50 +0000690#ifdef DLINFO_ARCH_ITEMS
pbrook53a59602006-03-25 19:31:22 +0000691 size += DLINFO_ARCH_ITEMS * 2;
bellardf5155282004-01-04 15:46:50 +0000692#endif
pbrook53a59602006-03-25 19:31:22 +0000693 size += envc + argc + 2;
694 size += (!ibcs ? 3 : 1); /* argc itself */
695 size *= n;
696 if (size & 15)
697 sp -= 16 - (size & 15);
bellardf5155282004-01-04 15:46:50 +0000698
pbrook53a59602006-03-25 19:31:22 +0000699#define NEW_AUX_ENT(id, val) do { \
700 sp -= n; tputl(sp, val); \
701 sp -= n; tputl(sp, id); \
702 } while(0)
bellard0bccf032005-08-21 10:12:28 +0000703 NEW_AUX_ENT (AT_NULL, 0);
bellardf5155282004-01-04 15:46:50 +0000704
bellard0bccf032005-08-21 10:12:28 +0000705 /* There must be exactly DLINFO_ITEMS entries here. */
706 NEW_AUX_ENT(AT_PHDR, (target_ulong)(load_addr + exec->e_phoff));
707 NEW_AUX_ENT(AT_PHENT, (target_ulong)(sizeof (struct elf_phdr)));
708 NEW_AUX_ENT(AT_PHNUM, (target_ulong)(exec->e_phnum));
709 NEW_AUX_ENT(AT_PAGESZ, (target_ulong)(TARGET_PAGE_SIZE));
710 NEW_AUX_ENT(AT_BASE, (target_ulong)(interp_load_addr));
711 NEW_AUX_ENT(AT_FLAGS, (target_ulong)0);
712 NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
713 NEW_AUX_ENT(AT_UID, (target_ulong) getuid());
714 NEW_AUX_ENT(AT_EUID, (target_ulong) geteuid());
715 NEW_AUX_ENT(AT_GID, (target_ulong) getgid());
716 NEW_AUX_ENT(AT_EGID, (target_ulong) getegid());
bellard15338fd2005-11-26 11:41:16 +0000717 NEW_AUX_ENT(AT_HWCAP, (target_ulong) ELF_HWCAP);
718 if (k_platform)
pbrook53a59602006-03-25 19:31:22 +0000719 NEW_AUX_ENT(AT_PLATFORM, u_platform);
bellardf5155282004-01-04 15:46:50 +0000720#ifdef ARCH_DLINFO
721 /*
722 * ARCH_DLINFO must come last so platform specific code can enforce
723 * special alignment requirements on the AUXV if necessary (eg. PPC).
724 */
725 ARCH_DLINFO;
726#endif
727#undef NEW_AUX_ENT
728
pbrooke5fe0c52006-06-11 13:32:59 +0000729 sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
bellard31e31b82003-02-18 22:55:36 +0000730 return sp;
731}
732
733
bellard31e31b82003-02-18 22:55:36 +0000734static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
735 int interpreter_fd,
736 unsigned long *interp_load_addr)
737{
738 struct elf_phdr *elf_phdata = NULL;
739 struct elf_phdr *eppnt;
bellard09bfb052003-04-10 00:03:40 +0000740 unsigned long load_addr = 0;
bellard31e31b82003-02-18 22:55:36 +0000741 int load_addr_set = 0;
742 int retval;
743 unsigned long last_bss, elf_bss;
744 unsigned long error;
745 int i;
746
747 elf_bss = 0;
748 last_bss = 0;
749 error = 0;
750
bellard644c4332003-03-24 23:00:36 +0000751#ifdef BSWAP_NEEDED
752 bswap_ehdr(interp_elf_ex);
753#endif
bellard31e31b82003-02-18 22:55:36 +0000754 /* First of all, some simple consistency checks */
755 if ((interp_elf_ex->e_type != ET_EXEC &&
bellard09bfb052003-04-10 00:03:40 +0000756 interp_elf_ex->e_type != ET_DYN) ||
bellard31e31b82003-02-18 22:55:36 +0000757 !elf_check_arch(interp_elf_ex->e_machine)) {
758 return ~0UL;
759 }
760
bellard644c4332003-03-24 23:00:36 +0000761
bellard31e31b82003-02-18 22:55:36 +0000762 /* Now read in all of the header information */
763
bellard54936002003-05-13 00:25:15 +0000764 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
bellard31e31b82003-02-18 22:55:36 +0000765 return ~0UL;
766
767 elf_phdata = (struct elf_phdr *)
768 malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
769
770 if (!elf_phdata)
771 return ~0UL;
772
773 /*
774 * If the size of this structure has changed, then punt, since
775 * we will be doing the wrong thing.
776 */
bellard09bfb052003-04-10 00:03:40 +0000777 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
bellard31e31b82003-02-18 22:55:36 +0000778 free(elf_phdata);
779 return ~0UL;
bellard09bfb052003-04-10 00:03:40 +0000780 }
bellard31e31b82003-02-18 22:55:36 +0000781
782 retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
783 if(retval >= 0) {
784 retval = read(interpreter_fd,
785 (char *) elf_phdata,
786 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
787 }
bellard31e31b82003-02-18 22:55:36 +0000788 if (retval < 0) {
789 perror("load_elf_interp");
790 exit(-1);
791 free (elf_phdata);
792 return retval;
793 }
794#ifdef BSWAP_NEEDED
795 eppnt = elf_phdata;
796 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
797 bswap_phdr(eppnt);
798 }
799#endif
bellard09bfb052003-04-10 00:03:40 +0000800
801 if (interp_elf_ex->e_type == ET_DYN) {
thse91c8a72007-06-03 13:35:16 +0000802 /* in order to avoid hardcoding the interpreter load
bellard09bfb052003-04-10 00:03:40 +0000803 address in qemu, we allocate a big enough memory zone */
bellard54936002003-05-13 00:25:15 +0000804 error = target_mmap(0, INTERP_MAP_SIZE,
805 PROT_NONE, MAP_PRIVATE | MAP_ANON,
806 -1, 0);
bellard09bfb052003-04-10 00:03:40 +0000807 if (error == -1) {
808 perror("mmap");
809 exit(-1);
810 }
811 load_addr = error;
812 load_addr_set = 1;
813 }
814
bellard31e31b82003-02-18 22:55:36 +0000815 eppnt = elf_phdata;
816 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
817 if (eppnt->p_type == PT_LOAD) {
818 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
819 int elf_prot = 0;
820 unsigned long vaddr = 0;
821 unsigned long k;
822
823 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
824 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
825 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
826 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
827 elf_type |= MAP_FIXED;
828 vaddr = eppnt->p_vaddr;
829 }
bellard54936002003-05-13 00:25:15 +0000830 error = target_mmap(load_addr+TARGET_ELF_PAGESTART(vaddr),
831 eppnt->p_filesz + TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr),
bellard31e31b82003-02-18 22:55:36 +0000832 elf_prot,
833 elf_type,
834 interpreter_fd,
bellard54936002003-05-13 00:25:15 +0000835 eppnt->p_offset - TARGET_ELF_PAGEOFFSET(eppnt->p_vaddr));
bellard31e31b82003-02-18 22:55:36 +0000836
pbrooke89f07d2006-02-04 20:46:24 +0000837 if (error == -1) {
bellard31e31b82003-02-18 22:55:36 +0000838 /* Real error */
839 close(interpreter_fd);
840 free(elf_phdata);
841 return ~0UL;
842 }
843
844 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
845 load_addr = error;
846 load_addr_set = 1;
847 }
848
849 /*
850 * Find the end of the file mapping for this phdr, and keep
851 * track of the largest address we see for this.
852 */
853 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
854 if (k > elf_bss) elf_bss = k;
855
856 /*
857 * Do the same thing for the memory mapping - between
858 * elf_bss and last_bss is the bss section.
859 */
860 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
861 if (k > last_bss) last_bss = k;
862 }
863
864 /* Now use mmap to map the library into memory. */
865
866 close(interpreter_fd);
867
868 /*
869 * Now fill out the bss section. First pad the last page up
870 * to the page boundary, and then perform a mmap to make sure
871 * that there are zeromapped pages up to and including the last
872 * bss page.
873 */
ths768a4a32006-12-14 13:32:11 +0000874 padzero(elf_bss, last_bss);
bellard83fb7ad2004-07-05 21:25:26 +0000875 elf_bss = TARGET_ELF_PAGESTART(elf_bss + qemu_host_page_size - 1); /* What we have mapped so far */
bellard31e31b82003-02-18 22:55:36 +0000876
877 /* Map the last of the bss segment */
878 if (last_bss > elf_bss) {
bellard54936002003-05-13 00:25:15 +0000879 target_mmap(elf_bss, last_bss-elf_bss,
880 PROT_READ|PROT_WRITE|PROT_EXEC,
881 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
bellard31e31b82003-02-18 22:55:36 +0000882 }
883 free(elf_phdata);
884
885 *interp_load_addr = load_addr;
886 return ((unsigned long) interp_elf_ex->e_entry) + load_addr;
887}
888
bellard689f9362003-04-29 20:40:07 +0000889/* Best attempt to load symbols from this ELF object. */
890static void load_symbols(struct elfhdr *hdr, int fd)
891{
892 unsigned int i;
893 struct elf_shdr sechdr, symtab, strtab;
894 char *strings;
bellarde80cfcf2004-12-19 23:18:01 +0000895 struct syminfo *s;
blueswir10774bed2007-07-05 13:23:29 +0000896#if (ELF_CLASS == ELFCLASS64)
897 // Disas uses 32 bit symbols
898 struct elf32_sym *syms32 = NULL;
899 struct elf_sym *sym;
900#endif
bellard31e31b82003-02-18 22:55:36 +0000901
bellard689f9362003-04-29 20:40:07 +0000902 lseek(fd, hdr->e_shoff, SEEK_SET);
903 for (i = 0; i < hdr->e_shnum; i++) {
904 if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
905 return;
906#ifdef BSWAP_NEEDED
907 bswap_shdr(&sechdr);
908#endif
909 if (sechdr.sh_type == SHT_SYMTAB) {
910 symtab = sechdr;
911 lseek(fd, hdr->e_shoff
912 + sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
913 if (read(fd, &strtab, sizeof(strtab))
914 != sizeof(strtab))
915 return;
916#ifdef BSWAP_NEEDED
917 bswap_shdr(&strtab);
918#endif
919 goto found;
920 }
921 }
922 return; /* Shouldn't happen... */
923
924 found:
925 /* Now know where the strtab and symtab are. Snarf them. */
bellarde80cfcf2004-12-19 23:18:01 +0000926 s = malloc(sizeof(*s));
927 s->disas_symtab = malloc(symtab.sh_size);
blueswir10774bed2007-07-05 13:23:29 +0000928#if (ELF_CLASS == ELFCLASS64)
929 syms32 = malloc(symtab.sh_size / sizeof(struct elf_sym)
930 * sizeof(struct elf32_sym));
931#endif
bellarde80cfcf2004-12-19 23:18:01 +0000932 s->disas_strtab = strings = malloc(strtab.sh_size);
933 if (!s->disas_symtab || !s->disas_strtab)
bellard689f9362003-04-29 20:40:07 +0000934 return;
935
936 lseek(fd, symtab.sh_offset, SEEK_SET);
bellarde80cfcf2004-12-19 23:18:01 +0000937 if (read(fd, s->disas_symtab, symtab.sh_size) != symtab.sh_size)
bellard689f9362003-04-29 20:40:07 +0000938 return;
939
blueswir10774bed2007-07-05 13:23:29 +0000940 for (i = 0; i < symtab.sh_size / sizeof(struct elf_sym); i++) {
bellard689f9362003-04-29 20:40:07 +0000941#ifdef BSWAP_NEEDED
bellarde80cfcf2004-12-19 23:18:01 +0000942 bswap_sym(s->disas_symtab + sizeof(struct elf_sym)*i);
bellard689f9362003-04-29 20:40:07 +0000943#endif
blueswir10774bed2007-07-05 13:23:29 +0000944#if (ELF_CLASS == ELFCLASS64)
945 sym = s->disas_symtab + sizeof(struct elf_sym)*i;
946 syms32[i].st_name = sym->st_name;
947 syms32[i].st_info = sym->st_info;
948 syms32[i].st_other = sym->st_other;
949 syms32[i].st_shndx = sym->st_shndx;
950 syms32[i].st_value = sym->st_value & 0xffffffff;
951 syms32[i].st_size = sym->st_size & 0xffffffff;
952#endif
953 }
bellard689f9362003-04-29 20:40:07 +0000954
blueswir10774bed2007-07-05 13:23:29 +0000955#if (ELF_CLASS == ELFCLASS64)
956 free(s->disas_symtab);
957 s->disas_symtab = syms32;
958#endif
bellard689f9362003-04-29 20:40:07 +0000959 lseek(fd, strtab.sh_offset, SEEK_SET);
960 if (read(fd, strings, strtab.sh_size) != strtab.sh_size)
961 return;
bellarde80cfcf2004-12-19 23:18:01 +0000962 s->disas_num_syms = symtab.sh_size / sizeof(struct elf_sym);
963 s->next = syminfos;
964 syminfos = s;
bellard689f9362003-04-29 20:40:07 +0000965}
bellard31e31b82003-02-18 22:55:36 +0000966
pbrooke5fe0c52006-06-11 13:32:59 +0000967int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
968 struct image_info * info)
bellard31e31b82003-02-18 22:55:36 +0000969{
970 struct elfhdr elf_ex;
971 struct elfhdr interp_elf_ex;
972 struct exec interp_ex;
973 int interpreter_fd = -1; /* avoid warning */
bellard09bfb052003-04-10 00:03:40 +0000974 unsigned long load_addr, load_bias;
bellard31e31b82003-02-18 22:55:36 +0000975 int load_addr_set = 0;
976 unsigned int interpreter_type = INTERPRETER_NONE;
977 unsigned char ibcs2_interpreter;
978 int i;
bellard54936002003-05-13 00:25:15 +0000979 unsigned long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000980 struct elf_phdr * elf_ppnt;
981 struct elf_phdr *elf_phdata;
982 unsigned long elf_bss, k, elf_brk;
983 int retval;
984 char * elf_interpreter;
985 unsigned long elf_entry, interp_load_addr = 0;
986 int status;
987 unsigned long start_code, end_code, end_data;
j_mayer84409dd2007-04-06 08:56:50 +0000988 unsigned long reloc_func_desc = 0;
bellard31e31b82003-02-18 22:55:36 +0000989 unsigned long elf_stack;
990 char passed_fileno[6];
991
992 ibcs2_interpreter = 0;
993 status = 0;
994 load_addr = 0;
bellard09bfb052003-04-10 00:03:40 +0000995 load_bias = 0;
bellard31e31b82003-02-18 22:55:36 +0000996 elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
997#ifdef BSWAP_NEEDED
998 bswap_ehdr(&elf_ex);
999#endif
1000
bellard31e31b82003-02-18 22:55:36 +00001001 /* First of all, some simple consistency checks */
1002 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
1003 (! elf_check_arch(elf_ex.e_machine))) {
1004 return -ENOEXEC;
1005 }
1006
pbrooke5fe0c52006-06-11 13:32:59 +00001007 bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
1008 bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
1009 bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
1010 if (!bprm->p) {
1011 retval = -E2BIG;
1012 }
1013
bellard31e31b82003-02-18 22:55:36 +00001014 /* Now read in all of the header information */
bellard31e31b82003-02-18 22:55:36 +00001015 elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
1016 if (elf_phdata == NULL) {
1017 return -ENOMEM;
1018 }
1019
1020 retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
1021 if(retval > 0) {
1022 retval = read(bprm->fd, (char *) elf_phdata,
1023 elf_ex.e_phentsize * elf_ex.e_phnum);
1024 }
1025
1026 if (retval < 0) {
1027 perror("load_elf_binary");
1028 exit(-1);
1029 free (elf_phdata);
1030 return -errno;
1031 }
1032
bellardb17780d2003-02-18 23:32:15 +00001033#ifdef BSWAP_NEEDED
1034 elf_ppnt = elf_phdata;
1035 for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) {
1036 bswap_phdr(elf_ppnt);
1037 }
1038#endif
bellard31e31b82003-02-18 22:55:36 +00001039 elf_ppnt = elf_phdata;
1040
1041 elf_bss = 0;
1042 elf_brk = 0;
1043
1044
1045 elf_stack = ~0UL;
1046 elf_interpreter = NULL;
1047 start_code = ~0UL;
1048 end_code = 0;
1049 end_data = 0;
1050
1051 for(i=0;i < elf_ex.e_phnum; i++) {
1052 if (elf_ppnt->p_type == PT_INTERP) {
1053 if ( elf_interpreter != NULL )
1054 {
1055 free (elf_phdata);
1056 free(elf_interpreter);
1057 close(bprm->fd);
1058 return -EINVAL;
1059 }
1060
1061 /* This is the program interpreter used for
1062 * shared libraries - for now assume that this
1063 * is an a.out format binary
1064 */
1065
bellard32ce6332003-04-11 00:16:16 +00001066 elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
bellard31e31b82003-02-18 22:55:36 +00001067
1068 if (elf_interpreter == NULL) {
1069 free (elf_phdata);
1070 close(bprm->fd);
1071 return -ENOMEM;
1072 }
1073
bellard31e31b82003-02-18 22:55:36 +00001074 retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
1075 if(retval >= 0) {
bellard32ce6332003-04-11 00:16:16 +00001076 retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
bellard31e31b82003-02-18 22:55:36 +00001077 }
1078 if(retval < 0) {
1079 perror("load_elf_binary2");
1080 exit(-1);
1081 }
1082
1083 /* If the program interpreter is one of these two,
1084 then assume an iBCS2 image. Otherwise assume
1085 a native linux image. */
1086
1087 /* JRP - Need to add X86 lib dir stuff here... */
1088
1089 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
1090 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
1091 ibcs2_interpreter = 1;
1092 }
1093
1094#if 0
1095 printf("Using ELF interpreter %s\n", elf_interpreter);
1096#endif
1097 if (retval >= 0) {
bellard32ce6332003-04-11 00:16:16 +00001098 retval = open(path(elf_interpreter), O_RDONLY);
bellard31e31b82003-02-18 22:55:36 +00001099 if(retval >= 0) {
1100 interpreter_fd = retval;
1101 }
1102 else {
1103 perror(elf_interpreter);
1104 exit(-1);
1105 /* retval = -errno; */
1106 }
1107 }
1108
1109 if (retval >= 0) {
1110 retval = lseek(interpreter_fd, 0, SEEK_SET);
1111 if(retval >= 0) {
1112 retval = read(interpreter_fd,bprm->buf,128);
1113 }
1114 }
1115 if (retval >= 0) {
1116 interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
1117 interp_elf_ex=*((struct elfhdr *) bprm->buf); /* elf exec-header */
1118 }
1119 if (retval < 0) {
1120 perror("load_elf_binary3");
1121 exit(-1);
1122 free (elf_phdata);
1123 free(elf_interpreter);
1124 close(bprm->fd);
1125 return retval;
1126 }
1127 }
1128 elf_ppnt++;
1129 }
1130
1131 /* Some simple consistency checks for the interpreter */
1132 if (elf_interpreter){
1133 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
1134
1135 /* Now figure out which format our binary is */
1136 if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
1137 (N_MAGIC(interp_ex) != QMAGIC)) {
1138 interpreter_type = INTERPRETER_ELF;
1139 }
1140
1141 if (interp_elf_ex.e_ident[0] != 0x7f ||
1142 strncmp(&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
1143 interpreter_type &= ~INTERPRETER_ELF;
1144 }
1145
1146 if (!interpreter_type) {
1147 free(elf_interpreter);
1148 free(elf_phdata);
1149 close(bprm->fd);
1150 return -ELIBBAD;
1151 }
1152 }
1153
1154 /* OK, we are done with that, now set up the arg stuff,
1155 and then start this sucker up */
1156
pbrooke5fe0c52006-06-11 13:32:59 +00001157 {
bellard31e31b82003-02-18 22:55:36 +00001158 char * passed_p;
1159
1160 if (interpreter_type == INTERPRETER_AOUT) {
bellardeba2af62004-06-19 17:23:39 +00001161 snprintf(passed_fileno, sizeof(passed_fileno), "%d", bprm->fd);
bellard31e31b82003-02-18 22:55:36 +00001162 passed_p = passed_fileno;
1163
1164 if (elf_interpreter) {
pbrooke5fe0c52006-06-11 13:32:59 +00001165 bprm->p = copy_elf_strings(1,&passed_p,bprm->page,bprm->p);
bellard31e31b82003-02-18 22:55:36 +00001166 bprm->argc++;
1167 }
1168 }
1169 if (!bprm->p) {
1170 if (elf_interpreter) {
1171 free(elf_interpreter);
1172 }
1173 free (elf_phdata);
1174 close(bprm->fd);
1175 return -E2BIG;
1176 }
1177 }
1178
1179 /* OK, This is the point of no return */
1180 info->end_data = 0;
1181 info->end_code = 0;
1182 info->start_mmap = (unsigned long)ELF_START_MMAP;
1183 info->mmap = 0;
1184 elf_entry = (unsigned long) elf_ex.e_entry;
1185
1186 /* Do this so that we can load the interpreter, if need be. We will
1187 change some of these later */
1188 info->rss = 0;
1189 bprm->p = setup_arg_pages(bprm->p, bprm, info);
1190 info->start_stack = bprm->p;
1191
1192 /* Now we do a little grungy work by mmaping the ELF image into
1193 * the correct location in memory. At this point, we assume that
1194 * the image should be loaded at fixed address, not at a variable
1195 * address.
1196 */
1197
bellard31e31b82003-02-18 22:55:36 +00001198 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
bellard09bfb052003-04-10 00:03:40 +00001199 int elf_prot = 0;
1200 int elf_flags = 0;
1201 unsigned long error;
1202
1203 if (elf_ppnt->p_type != PT_LOAD)
1204 continue;
1205
1206 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
1207 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
1208 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
1209 elf_flags = MAP_PRIVATE | MAP_DENYWRITE;
1210 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
1211 elf_flags |= MAP_FIXED;
1212 } else if (elf_ex.e_type == ET_DYN) {
1213 /* Try and get dynamic programs out of the way of the default mmap
1214 base, as well as whatever program they might try to exec. This
1215 is because the brk will follow the loader, and is not movable. */
1216 /* NOTE: for qemu, we do a big mmap to get enough space
thse91c8a72007-06-03 13:35:16 +00001217 without hardcoding any address */
bellard54936002003-05-13 00:25:15 +00001218 error = target_mmap(0, ET_DYN_MAP_SIZE,
1219 PROT_NONE, MAP_PRIVATE | MAP_ANON,
1220 -1, 0);
bellard09bfb052003-04-10 00:03:40 +00001221 if (error == -1) {
1222 perror("mmap");
1223 exit(-1);
1224 }
bellard54936002003-05-13 00:25:15 +00001225 load_bias = TARGET_ELF_PAGESTART(error - elf_ppnt->p_vaddr);
bellard09bfb052003-04-10 00:03:40 +00001226 }
1227
bellard54936002003-05-13 00:25:15 +00001228 error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
1229 (elf_ppnt->p_filesz +
1230 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
1231 elf_prot,
1232 (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
1233 bprm->fd,
1234 (elf_ppnt->p_offset -
1235 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
bellard09bfb052003-04-10 00:03:40 +00001236 if (error == -1) {
1237 perror("mmap");
1238 exit(-1);
1239 }
bellard31e31b82003-02-18 22:55:36 +00001240
1241#ifdef LOW_ELF_STACK
bellard54936002003-05-13 00:25:15 +00001242 if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
1243 elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
bellard31e31b82003-02-18 22:55:36 +00001244#endif
bellard09bfb052003-04-10 00:03:40 +00001245
1246 if (!load_addr_set) {
1247 load_addr_set = 1;
1248 load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
1249 if (elf_ex.e_type == ET_DYN) {
1250 load_bias += error -
bellard54936002003-05-13 00:25:15 +00001251 TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr);
bellard09bfb052003-04-10 00:03:40 +00001252 load_addr += load_bias;
j_mayer84409dd2007-04-06 08:56:50 +00001253 reloc_func_desc = load_bias;
bellard09bfb052003-04-10 00:03:40 +00001254 }
1255 }
1256 k = elf_ppnt->p_vaddr;
1257 if (k < start_code)
1258 start_code = k;
1259 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
1260 if (k > elf_bss)
1261 elf_bss = k;
1262 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
1263 end_code = k;
1264 if (end_data < k)
1265 end_data = k;
1266 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1267 if (k > elf_brk) elf_brk = k;
bellard31e31b82003-02-18 22:55:36 +00001268 }
1269
bellard09bfb052003-04-10 00:03:40 +00001270 elf_entry += load_bias;
1271 elf_bss += load_bias;
1272 elf_brk += load_bias;
1273 start_code += load_bias;
1274 end_code += load_bias;
1275 // start_data += load_bias;
1276 end_data += load_bias;
1277
bellard31e31b82003-02-18 22:55:36 +00001278 if (elf_interpreter) {
1279 if (interpreter_type & 1) {
1280 elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
1281 }
1282 else if (interpreter_type & 2) {
1283 elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
1284 &interp_load_addr);
1285 }
j_mayer84409dd2007-04-06 08:56:50 +00001286 reloc_func_desc = interp_load_addr;
bellard31e31b82003-02-18 22:55:36 +00001287
1288 close(interpreter_fd);
1289 free(elf_interpreter);
1290
1291 if (elf_entry == ~0UL) {
1292 printf("Unable to load interpreter\n");
1293 free(elf_phdata);
1294 exit(-1);
1295 return 0;
1296 }
1297 }
1298
1299 free(elf_phdata);
1300
bellard689f9362003-04-29 20:40:07 +00001301 if (loglevel)
1302 load_symbols(&elf_ex, bprm->fd);
1303
bellard31e31b82003-02-18 22:55:36 +00001304 if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
1305 info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
1306
1307#ifdef LOW_ELF_STACK
1308 info->start_stack = bprm->p = elf_stack - 4;
1309#endif
pbrook53a59602006-03-25 19:31:22 +00001310 bprm->p = create_elf_tables(bprm->p,
bellard31e31b82003-02-18 22:55:36 +00001311 bprm->argc,
1312 bprm->envc,
bellarda1516e92003-07-09 17:13:37 +00001313 &elf_ex,
bellard09bfb052003-04-10 00:03:40 +00001314 load_addr, load_bias,
bellard31e31b82003-02-18 22:55:36 +00001315 interp_load_addr,
1316 (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
1317 info);
bellard31e31b82003-02-18 22:55:36 +00001318 info->start_brk = info->brk = elf_brk;
1319 info->end_code = end_code;
1320 info->start_code = start_code;
pbrooke5fe0c52006-06-11 13:32:59 +00001321 info->start_data = end_code;
bellard31e31b82003-02-18 22:55:36 +00001322 info->end_data = end_data;
1323 info->start_stack = bprm->p;
1324
1325 /* Calling set_brk effectively mmaps the pages that we need for the bss and break
1326 sections */
1327 set_brk(elf_bss, elf_brk);
1328
ths768a4a32006-12-14 13:32:11 +00001329 padzero(elf_bss, elf_brk);
bellard31e31b82003-02-18 22:55:36 +00001330
1331#if 0
1332 printf("(start_brk) %x\n" , info->start_brk);
1333 printf("(end_code) %x\n" , info->end_code);
1334 printf("(start_code) %x\n" , info->start_code);
1335 printf("(end_data) %x\n" , info->end_data);
1336 printf("(start_stack) %x\n" , info->start_stack);
1337 printf("(brk) %x\n" , info->brk);
1338#endif
1339
1340 if ( info->personality == PER_SVR4 )
1341 {
1342 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1343 and some applications "depend" upon this behavior.
1344 Since we do not have the power to recompile these, we
1345 emulate the SVr4 behavior. Sigh. */
bellard83fb7ad2004-07-05 21:25:26 +00001346 mapped_addr = target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
bellard54936002003-05-13 00:25:15 +00001347 MAP_FIXED | MAP_PRIVATE, -1, 0);
bellard31e31b82003-02-18 22:55:36 +00001348 }
1349
bellard31e31b82003-02-18 22:55:36 +00001350 info->entry = elf_entry;
1351
1352 return 0;
1353}
1354
bellard31e31b82003-02-18 22:55:36 +00001355static int load_aout_interp(void * exptr, int interp_fd)
1356{
1357 printf("a.out interpreter not yet supported\n");
1358 return(0);
1359}
1360
pbrooke5fe0c52006-06-11 13:32:59 +00001361void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
1362{
1363 init_thread(regs, infop);
1364}