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