4 * Copyright (C) 1995,96,97,98,99,2000,2001,2002 Free Software Foundation, Inc.
5 * Copyright (C) 2004 Mike McCormack for CodeWeavers
6 * Copyright (C) 2004 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * The goal of this program is to be a workaround for exec-shield, as used
27 * by the Linux kernel distributed with Fedora Core and other distros.
29 * To do this, we implement our own shared object loader that reserves memory
30 * that is important to Wine, and then loads the main binary and its ELF
33 * We will try to set up the stack and memory area so that the program that
34 * loads after us (eg. the wine binary) never knows we were here, except that
35 * areas of memory it needs are already magically reserved.
37 * The following memory areas are important to Wine:
38 * 0x00000000 - 0x00110000 the DOS area
39 * 0x80000000 - 0x81000000 the shared heap
40 * ??? - ??? the PE binary load address (usually starting at 0x00400000)
42 * If this program is used as the shared object loader, the only difference
43 * that the loaded programs should see is that this loader will be mapped
44 * into memory when it starts.
48 * References (things I consulted to understand how ELF loading works):
50 * glibc 2.3.2 elf/dl-load.c
51 * http://www.gnu.org/directory/glibc.html
53 * Linux 2.6.4 fs/binfmt_elf.c
54 * ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.4.tar.bz2
56 * Userland exec, by <grugq@hcunix.net>
57 * http://cert.uni-stuttgart.de/archive/bugtraq/2004/01/msg00002.html
59 * The ELF specification:
60 * http://www.linuxbase.org/spec/booksets/LSB-Embedded/LSB-Embedded/book387.html
66 #include "wine/port.h"
72 #include <sys/types.h>
73 #ifdef HAVE_SYS_STAT_H
74 # include <sys/stat.h>
77 #ifdef HAVE_SYS_MMAN_H
78 # include <sys/mman.h>
80 #ifdef HAVE_SYS_SYSCALL_H
81 # include <sys/syscall.h>
92 #ifdef HAVE_SYS_LINK_H
93 # include <sys/link.h>
99 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
100 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
102 #define MAP_BASE_ADDR(l) 0
105 #define MAP_COPY MAP_PRIVATE
107 #ifndef MAP_NORESERVE
108 #define MAP_NORESERVE 0
111 static struct wine_preload_info preload_info
[] =
114 { (void *)0x00000000, 0x00010000 }, /* low 64k */
115 { (void *)0x00010000, 0x00100000 }, /* DOS area */
116 { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
117 { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
119 { (void *)0x000000010000, 0x00100000 }, /* DOS area */
120 { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
121 { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
122 { (void *)0x7ffffe000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
124 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
125 { 0, 0 } /* end of list */
134 /* older systems may not define these */
140 #define AT_SYSINFO 32
142 #ifndef AT_SYSINFO_EHDR
143 #define AT_SYSINFO_EHDR 33
147 #define DT_GNU_HASH 0x6ffffef5
150 static size_t page_size
, page_mask
;
151 static char *preloader_start
, *preloader_end
;
153 struct wld_link_map
{
160 ElfW(Addr
) l_map_start
, l_map_end
;
174 * The __bb_init_func is an empty function only called when file is
175 * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
176 * function is normally provided by libc's startup files, but since we
177 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
178 * provide our own (empty) version, otherwise linker fails.
180 void __bb_init_func(void) { return; }
182 /* similar to the above but for -fstack-protector */
183 void *__stack_chk_guard
= 0;
184 void __stack_chk_fail_local(void) { return; }
185 void __stack_chk_fail(void) { return; }
189 /* data for setting up the glibc-style thread-local storage in %gs */
191 static int thread_data
[256];
195 /* this is the kernel modify_ldt struct */
196 unsigned int entry_number
;
197 unsigned long base_addr
;
199 unsigned int seg_32bit
: 1;
200 unsigned int contents
: 2;
201 unsigned int read_exec_only
: 1;
202 unsigned int limit_in_pages
: 1;
203 unsigned int seg_not_present
: 1;
204 unsigned int usable
: 1;
205 unsigned int garbage
: 25;
206 } thread_ldt
= { -1, (unsigned long)thread_data
, 0xfffff, 1, 0, 0, 1, 0, 1, 0 };
210 * The _start function is the entry and exit point of this program
212 * It calls wld_start, passing a pointer to the args it receives
213 * then jumps to the address wld_start returns.
217 __ASM_GLOBAL_FUNC(_start
,
218 __ASM_CFI("\t.cfi_undefined %eip\n")
219 "\tmovl $243,%eax\n" /* SYS_set_thread_area */
220 "\tmovl $thread_ldt,%ebx\n"
221 "\tint $0x80\n" /* allocate gs segment */
224 "\tmovl thread_ldt,%eax\n" /* thread_ldt.entry_number */
228 "\tmov %ax,%fs\n" /* set %fs too so libwine can retrieve it later on */
229 "1:\tmovl %esp,%eax\n"
230 "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */
231 "\tpushl %eax\n" /* orig stack pointer */
232 "\tpushl %esp\n" /* ptr to orig stack pointer */
234 "\tpopl %ecx\n" /* remove ptr to stack pointer */
235 "\tpopl %esp\n" /* new stack pointer */
236 "\tpush %eax\n" /* ELF interpreter entry point */
240 "\tmov %ax,%gs\n" /* clear %gs again */
243 /* wrappers for Linux system calls */
245 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
247 static inline __attribute__((noreturn
)) void wld_exit( int code
)
249 for (;;) /* avoid warning */
250 __asm__
__volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
251 : : "a" (1 /* SYS_exit */), "r" (code
) );
254 static inline int wld_open( const char *name
, int flags
)
257 __asm__
__volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
258 : "=a" (ret
) : "0" (5 /* SYS_open */), "r" (name
), "c" (flags
) );
259 return SYSCALL_RET(ret
);
262 static inline int wld_close( int fd
)
265 __asm__
__volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
266 : "=a" (ret
) : "0" (6 /* SYS_close */), "r" (fd
) );
267 return SYSCALL_RET(ret
);
270 static inline ssize_t
wld_read( int fd
, void *buffer
, size_t len
)
273 __asm__
__volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
275 : "0" (3 /* SYS_read */), "r" (fd
), "c" (buffer
), "d" (len
)
277 return SYSCALL_RET(ret
);
280 static inline ssize_t
wld_write( int fd
, const void *buffer
, size_t len
)
283 __asm__
__volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
284 : "=a" (ret
) : "0" (4 /* SYS_write */), "r" (fd
), "c" (buffer
), "d" (len
) );
285 return SYSCALL_RET(ret
);
288 static inline int wld_mprotect( const void *addr
, size_t len
, int prot
)
291 __asm__
__volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
292 : "=a" (ret
) : "0" (125 /* SYS_mprotect */), "r" (addr
), "c" (len
), "d" (prot
) );
293 return SYSCALL_RET(ret
);
296 void *wld_mmap( void *start
, size_t len
, int prot
, int flags
, int fd
, unsigned int offset
);
297 __ASM_GLOBAL_FUNC(wld_mmap
,
299 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
301 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
303 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
305 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
306 "\tmovl $192,%eax\n" /* SYS_mmap2 */
307 "\tmovl 20(%esp),%ebx\n" /* start */
308 "\tmovl 24(%esp),%ecx\n" /* len */
309 "\tmovl 28(%esp),%edx\n" /* prot */
310 "\tmovl 32(%esp),%esi\n" /* flags */
311 "\tmovl 36(%esp),%edi\n" /* fd */
312 "\tmovl 40(%esp),%ebp\n" /* offset */
315 "\tcmpl $-4096,%eax\n"
317 "\tcmpl $-38,%eax\n" /* ENOSYS */
319 "\tmovl $90,%eax\n" /* SYS_mmap */
320 "\tleal 20(%esp),%ebx\n"
322 "\tcmpl $-4096,%eax\n"
324 "1:\tmovl $-1,%eax\n"
326 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
328 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
330 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
332 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
335 static inline int wld_prctl( int code
, long arg
)
338 __asm__
__volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
339 : "=a" (ret
) : "0" (172 /* SYS_prctl */), "r" (code
), "c" (arg
) );
340 return SYSCALL_RET(ret
);
343 #elif defined(__x86_64__)
345 void *thread_data
[256];
348 * The _start function is the entry and exit point of this program
350 * It calls wld_start, passing a pointer to the args it receives
351 * then jumps to the address wld_start returns.
355 __ASM_GLOBAL_FUNC(_start
,
356 __ASM_CFI(".cfi_undefined %rip\n\t")
358 "leaq -144(%rsp),%rsp\n\t" /* allocate some space for extra aux values */
359 "movq %rax,(%rsp)\n\t" /* orig stack pointer */
360 "movq $thread_data,%rsi\n\t"
361 "movq $0x1002,%rdi\n\t" /* ARCH_SET_FS */
362 "movq $158,%rax\n\t" /* SYS_arch_prctl */
364 "movq %rsp,%rdi\n\t" /* ptr to orig stack pointer */
366 "movq (%rsp),%rsp\n\t" /* new stack pointer */
367 "pushq %rax\n\t" /* ELF interpreter entry point */
379 #define SYSCALL_FUNC( name, nr ) \
380 __ASM_GLOBAL_FUNC( name, \
381 "movq $" #nr ",%rax\n\t" \
382 "movq %rcx,%r10\n\t" \
384 "leaq 4096(%rax),%rcx\n\t" \
385 "movq $-1,%rdx\n\t" \
386 "cmp $4096,%rcx\n\t" \
387 "cmovb %rdx,%rax\n\t" \
390 #define SYSCALL_NOERR( name, nr ) \
391 __ASM_GLOBAL_FUNC( name, \
392 "movq $" #nr ",%rax\n\t" \
396 void wld_exit( int code
) __attribute__((noreturn
));
397 SYSCALL_NOERR( wld_exit
, 60 /* SYS_exit */ );
399 ssize_t
wld_read( int fd
, void *buffer
, size_t len
);
400 SYSCALL_FUNC( wld_read
, 0 /* SYS_read */ );
402 ssize_t
wld_write( int fd
, const void *buffer
, size_t len
);
403 SYSCALL_FUNC( wld_write
, 1 /* SYS_write */ );
405 int wld_open( const char *name
, int flags
);
406 SYSCALL_FUNC( wld_open
, 2 /* SYS_open */ );
408 int wld_close( int fd
);
409 SYSCALL_FUNC( wld_close
, 3 /* SYS_close */ );
411 void *wld_mmap( void *start
, size_t len
, int prot
, int flags
, int fd
, off_t offset
);
412 SYSCALL_FUNC( wld_mmap
, 9 /* SYS_mmap */ );
414 int wld_mprotect( const void *addr
, size_t len
, int prot
);
415 SYSCALL_FUNC( wld_mprotect
, 10 /* SYS_mprotect */ );
417 int wld_prctl( int code
, long arg
);
418 SYSCALL_FUNC( wld_prctl
, 157 /* SYS_prctl */ );
420 uid_t
wld_getuid(void);
421 SYSCALL_NOERR( wld_getuid
, 102 /* SYS_getuid */ );
423 gid_t
wld_getgid(void);
424 SYSCALL_NOERR( wld_getgid
, 104 /* SYS_getgid */ );
426 uid_t
wld_geteuid(void);
427 SYSCALL_NOERR( wld_geteuid
, 107 /* SYS_geteuid */ );
429 gid_t
wld_getegid(void);
430 SYSCALL_NOERR( wld_getegid
, 108 /* SYS_getegid */ );
432 #elif defined(__aarch64__)
434 void *thread_data
[256];
437 * The _start function is the entry and exit point of this program
439 * It calls wld_start, passing a pointer to the args it receives
440 * then jumps to the address wld_start returns.
444 __ASM_GLOBAL_FUNC(_start
,
446 "sub SP, SP, #144\n\t" /* allocate some space for extra aux values */
447 "str x0, [SP]\n\t" /* orig stack pointer */
448 "ldr x0, =thread_data\n\t"
449 "msr tpidr_el0, x0\n\t"
450 "mov x0, SP\n\t" /* ptr to orig stack pointer */
452 "ldr x1, [SP]\n\t" /* new stack pointer */
476 #define SYSCALL_FUNC( name, nr ) \
477 __ASM_GLOBAL_FUNC( name, \
478 "stp x8, x9, [SP, #-16]!\n\t" \
479 "mov x8, #" #nr "\n\t" \
481 "ldp x8, x9, [SP], #16\n\t" \
482 "cmn x0, #1, lsl#12\n\t" \
483 "cinv x0, x0, hi\n\t" \
486 "1: mov x0, #-1\n\t" \
489 #define SYSCALL_NOERR( name, nr ) \
490 __ASM_GLOBAL_FUNC( name, \
491 "stp x8, x9, [SP, #-16]!\n\t" \
492 "mov x8, #" #nr "\n\t" \
494 "ldp x8, x9, [SP], #16\n\t" \
497 void wld_exit( int code
) __attribute__((noreturn
));
498 SYSCALL_NOERR( wld_exit
, 93 /* SYS_exit */ );
500 ssize_t
wld_read( int fd
, void *buffer
, size_t len
);
501 SYSCALL_FUNC( wld_read
, 63 /* SYS_read */ );
503 ssize_t
wld_write( int fd
, const void *buffer
, size_t len
);
504 SYSCALL_FUNC( wld_write
, 64 /* SYS_write */ );
506 int wld_openat( int dirfd
, const char *name
, int flags
);
507 SYSCALL_FUNC( wld_openat
, 56 /* SYS_openat */ );
509 int wld_open( const char *name
, int flags
)
511 return wld_openat(-100 /* AT_FDCWD */, name
, flags
);
514 int wld_close( int fd
);
515 SYSCALL_FUNC( wld_close
, 57 /* SYS_close */ );
517 void *wld_mmap( void *start
, size_t len
, int prot
, int flags
, int fd
, off_t offset
);
518 SYSCALL_FUNC( wld_mmap
, 222 /* SYS_mmap */ );
520 int wld_mprotect( const void *addr
, size_t len
, int prot
);
521 SYSCALL_FUNC( wld_mprotect
, 226 /* SYS_mprotect */ );
523 int wld_prctl( int code
, long arg
);
524 SYSCALL_FUNC( wld_prctl
, 167 /* SYS_prctl */ );
526 uid_t
wld_getuid(void);
527 SYSCALL_NOERR( wld_getuid
, 174 /* SYS_getuid */ );
529 gid_t
wld_getgid(void);
530 SYSCALL_NOERR( wld_getgid
, 176 /* SYS_getgid */ );
532 uid_t
wld_geteuid(void);
533 SYSCALL_NOERR( wld_geteuid
, 175 /* SYS_geteuid */ );
535 gid_t
wld_getegid(void);
536 SYSCALL_NOERR( wld_getegid
, 177 /* SYS_getegid */ );
539 #error preloader not implemented for this CPU
542 /* replacement for libc functions */
544 static int wld_strcmp( const char *str1
, const char *str2
)
546 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
547 return *str1
- *str2
;
550 static int wld_strncmp( const char *str1
, const char *str2
, size_t len
)
552 if (len
<= 0) return 0;
553 while ((--len
> 0) && *str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
554 return *str1
- *str2
;
557 static inline void *wld_memset( void *dest
, int val
, size_t len
)
560 while (len
--) *dst
++ = val
;
565 * wld_printf - just the basics
567 * %x prints a hex number
569 * %p prints a pointer
571 static int wld_vsprintf(char *buffer
, const char *fmt
, va_list args
)
573 static const char hex_chars
[16] = "0123456789abcdef";
585 unsigned int x
= va_arg( args
, unsigned int );
586 for (i
= 2*sizeof(x
) - 1; i
>= 0; i
--)
587 *str
++ = hex_chars
[(x
>>(i
*4))&0xf];
589 else if (p
[0] == 'l' && p
[1] == 'x')
591 unsigned long x
= va_arg( args
, unsigned long );
592 for (i
= 2*sizeof(x
) - 1; i
>= 0; i
--)
593 *str
++ = hex_chars
[(x
>>(i
*4))&0xf];
598 unsigned long x
= (unsigned long)va_arg( args
, void * );
599 for (i
= 2*sizeof(x
) - 1; i
>= 0; i
--)
600 *str
++ = hex_chars
[(x
>>(i
*4))&0xf];
604 char *s
= va_arg( args
, char * );
618 static __attribute__((format(printf
,1,2))) void wld_printf(const char *fmt
, ... )
624 va_start( args
, fmt
);
625 len
= wld_vsprintf(buffer
, fmt
, args
);
627 wld_write(2, buffer
, len
);
630 static __attribute__((noreturn
,format(printf
,1,2))) void fatal_error(const char *fmt
, ... )
636 va_start( args
, fmt
);
637 len
= wld_vsprintf(buffer
, fmt
, args
);
639 wld_write(2, buffer
, len
);
645 * Dump interesting bits of the ELF auxv_t structure that is passed
646 * as the 4th parameter to the _start function
648 static void dump_auxiliary( struct wld_auxv
*av
)
650 #define NAME(at) { at, #at }
651 static const struct { int val
; const char *name
; } names
[] =
667 NAME(AT_SYSINFO_EHDR
),
675 for ( ; av
->a_type
!= AT_NULL
; av
++)
677 for (i
= 0; names
[i
].name
; i
++) if (names
[i
].val
== av
->a_type
) break;
678 if (names
[i
].name
) wld_printf("%s = %lx\n", names
[i
].name
, (unsigned long)av
->a_un
.a_val
);
679 else wld_printf( "%lx = %lx\n", (unsigned long)av
->a_type
, (unsigned long)av
->a_un
.a_val
);
685 * set_auxiliary_values
687 * Set the new auxiliary values
689 static void set_auxiliary_values( struct wld_auxv
*av
, const struct wld_auxv
*new_av
,
690 const struct wld_auxv
*delete_av
, void **stack
)
692 int i
, j
, av_count
= 0, new_count
= 0, delete_count
= 0;
695 /* count how many aux values we have already */
696 while (av
[av_count
].a_type
!= AT_NULL
) av_count
++;
698 /* delete unwanted values */
699 for (j
= 0; delete_av
[j
].a_type
!= AT_NULL
; j
++)
701 for (i
= 0; i
< av_count
; i
++) if (av
[i
].a_type
== delete_av
[j
].a_type
)
703 av
[i
].a_type
= av
[av_count
-1].a_type
;
704 av
[i
].a_un
.a_val
= av
[av_count
-1].a_un
.a_val
;
705 av
[--av_count
].a_type
= AT_NULL
;
711 /* count how many values we have in new_av that aren't in av */
712 for (j
= 0; new_av
[j
].a_type
!= AT_NULL
; j
++)
714 for (i
= 0; i
< av_count
; i
++) if (av
[i
].a_type
== new_av
[j
].a_type
) break;
715 if (i
== av_count
) new_count
++;
718 src
= (char *)*stack
;
719 dst
= src
- (new_count
- delete_count
) * sizeof(*av
);
720 dst
= (char *)((unsigned long)dst
& ~15);
721 if (dst
< src
) /* need to make room for the extra values */
723 int len
= (char *)(av
+ av_count
+ 1) - src
;
724 for (i
= 0; i
< len
; i
++) dst
[i
] = src
[i
];
726 else if (dst
> src
) /* get rid of unused values */
728 int len
= (char *)(av
+ av_count
+ 1) - src
;
729 for (i
= len
- 1; i
>= 0; i
--) dst
[i
] = src
[i
];
732 av
= (struct wld_auxv
*)((char *)av
+ (dst
- src
));
734 /* now set the values */
735 for (j
= 0; new_av
[j
].a_type
!= AT_NULL
; j
++)
737 for (i
= 0; i
< av_count
; i
++) if (av
[i
].a_type
== new_av
[j
].a_type
) break;
738 if (i
< av_count
) av
[i
].a_un
.a_val
= new_av
[j
].a_un
.a_val
;
741 av
[av_count
].a_type
= new_av
[j
].a_type
;
742 av
[av_count
].a_un
.a_val
= new_av
[j
].a_un
.a_val
;
748 wld_printf("New auxiliary info:\n");
749 dump_auxiliary( av
);
756 * Get a field of the auxiliary structure
758 static int get_auxiliary( struct wld_auxv
*av
, int type
, int def_val
)
760 for ( ; av
->a_type
!= AT_NULL
; av
++)
761 if( av
->a_type
== type
) return av
->a_un
.a_val
;
768 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
770 * This function maps the segments from an ELF object, and optionally
771 * stores information about the mapping into the auxv_t structure.
773 static void map_so_lib( const char *name
, struct wld_link_map
*l
)
776 unsigned char buf
[0x800];
777 ElfW(Ehdr
) *header
= (ElfW(Ehdr
)*)buf
;
778 ElfW(Phdr
) *phdr
, *ph
;
779 /* Scan the program header table, collecting its load commands. */
782 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
786 size_t nloadcmds
= 0, maplength
;
788 fd
= wld_open( name
, O_RDONLY
);
789 if (fd
== -1) fatal_error("%s: could not open\n", name
);
791 if (wld_read( fd
, buf
, sizeof(buf
) ) != sizeof(buf
))
792 fatal_error("%s: failed to read ELF header\n", name
);
794 phdr
= (void*) (((unsigned char*)buf
) + header
->e_phoff
);
796 if( ( header
->e_ident
[0] != 0x7f ) ||
797 ( header
->e_ident
[1] != 'E' ) ||
798 ( header
->e_ident
[2] != 'L' ) ||
799 ( header
->e_ident
[3] != 'F' ) )
800 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name
);
803 if( header
->e_machine
!= EM_386
)
804 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name
);
805 #elif defined(__x86_64__)
806 if( header
->e_machine
!= EM_X86_64
)
807 fatal_error("%s: not an x86-64 ELF binary... don't know how to load it\n", name
);
808 #elif defined(__aarch64__)
809 if( header
->e_machine
!= EM_AARCH64
)
810 fatal_error("%s: not an aarch64 ELF binary... don't know how to load it\n", name
);
813 if (header
->e_phnum
> sizeof(loadcmds
)/sizeof(loadcmds
[0]))
814 fatal_error( "%s: oops... not enough space for load commands\n", name
);
816 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
817 if (header
->e_phoff
+ maplength
> sizeof(buf
))
818 fatal_error( "%s: oops... not enough space for ELF headers\n", name
);
823 l
->l_phnum
= header
->e_phnum
;
824 l
->l_entry
= header
->e_entry
;
827 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
831 wld_printf( "ph = %p\n", ph
);
832 wld_printf( " p_type = %lx\n", (unsigned long)ph
->p_type
);
833 wld_printf( " p_flags = %lx\n", (unsigned long)ph
->p_flags
);
834 wld_printf( " p_offset = %lx\n", (unsigned long)ph
->p_offset
);
835 wld_printf( " p_vaddr = %lx\n", (unsigned long)ph
->p_vaddr
);
836 wld_printf( " p_paddr = %lx\n", (unsigned long)ph
->p_paddr
);
837 wld_printf( " p_filesz = %lx\n", (unsigned long)ph
->p_filesz
);
838 wld_printf( " p_memsz = %lx\n", (unsigned long)ph
->p_memsz
);
839 wld_printf( " p_align = %lx\n", (unsigned long)ph
->p_align
);
844 /* These entries tell us where to find things once the file's
845 segments are mapped in. We record the addresses it says
846 verbatim, and later correct for the run-time load address. */
848 l
->l_ld
= (void *) ph
->p_vaddr
;
849 l
->l_ldnum
= ph
->p_memsz
/ sizeof (Elf32_Dyn
);
853 l
->l_phdr
= (void *) ph
->p_vaddr
;
858 if ((ph
->p_align
& page_mask
) != 0)
859 fatal_error( "%s: ELF load command alignment not page-aligned\n", name
);
861 if (((ph
->p_vaddr
- ph
->p_offset
) & (ph
->p_align
- 1)) != 0)
862 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name
);
864 c
= &loadcmds
[nloadcmds
++];
865 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
866 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ page_mask
) & ~page_mask
);
867 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
868 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
869 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
872 if (ph
->p_flags
& PF_R
)
873 c
->prot
|= PROT_READ
;
874 if (ph
->p_flags
& PF_W
)
875 c
->prot
|= PROT_WRITE
;
876 if (ph
->p_flags
& PF_X
)
877 c
->prot
|= PROT_EXEC
;
882 l
->l_interp
= ph
->p_vaddr
;
887 * We don't need to set anything up because we're
888 * emulating the kernel, not ld-linux.so.2
889 * The ELF loader will set up the TLS data itself.
898 /* Now process the load commands and map segments into memory. */
900 fatal_error( "%s: no segments to load\n", name
);
903 /* Length of the sections to be loaded. */
904 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
906 if( header
->e_type
== ET_DYN
)
909 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
910 - MAP_BASE_ADDR (l
));
912 /* Remember which part of the address space this object uses. */
913 l
->l_map_start
= (ElfW(Addr
)) wld_mmap ((void *) mappref
, maplength
,
914 c
->prot
, MAP_COPY
| MAP_FILE
,
916 /* wld_printf("set : offset = %x\n", c->mapoff); */
917 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
919 l
->l_map_end
= l
->l_map_start
+ maplength
;
920 l
->l_addr
= l
->l_map_start
- c
->mapstart
;
922 wld_mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
923 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
930 if ((char *)c
->mapstart
+ maplength
> preloader_start
&&
931 (char *)c
->mapstart
<= preloader_end
)
932 fatal_error( "%s: binary overlaps preloader (%p-%p)\n",
933 name
, (char *)c
->mapstart
, (char *)c
->mapstart
+ maplength
);
935 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
938 /* Remember which part of the address space this object uses. */
939 l
->l_map_start
= c
->mapstart
+ l
->l_addr
;
940 l
->l_map_end
= l
->l_map_start
+ maplength
;
942 while (c
< &loadcmds
[nloadcmds
])
944 if (c
->mapend
> c
->mapstart
)
945 /* Map the segment contents from the file. */
946 wld_mmap ((void *) (l
->l_addr
+ c
->mapstart
),
947 c
->mapend
- c
->mapstart
, c
->prot
,
948 MAP_FIXED
| MAP_COPY
| MAP_FILE
, fd
, c
->mapoff
);
952 && (ElfW(Off
)) c
->mapoff
<= header
->e_phoff
953 && ((size_t) (c
->mapend
- c
->mapstart
+ c
->mapoff
)
954 >= header
->e_phoff
+ header
->e_phnum
* sizeof (ElfW(Phdr
))))
955 /* Found the program header in this segment. */
956 l
->l_phdr
= (void *)(unsigned long)(c
->mapstart
+ header
->e_phoff
- c
->mapoff
);
958 if (c
->allocend
> c
->dataend
)
960 /* Extra zero pages should appear at the end of this segment,
961 after the data mapped from the file. */
962 ElfW(Addr
) zero
, zeroend
, zeropage
;
964 zero
= l
->l_addr
+ c
->dataend
;
965 zeroend
= l
->l_addr
+ c
->allocend
;
966 zeropage
= (zero
+ page_mask
) & ~page_mask
;
969 * This is different from the dl-load load...
970 * ld-linux.so.2 relies on the whole page being zero'ed
972 zeroend
= (zeroend
+ page_mask
) & ~page_mask
;
974 if (zeroend
< zeropage
)
976 /* All the extra data is in the last page of the segment.
977 We can just zero it. */
983 /* Zero the final part of the last page of the segment. */
984 if ((c
->prot
& PROT_WRITE
) == 0)
987 wld_mprotect ((caddr_t
) (zero
& ~page_mask
), page_size
, c
->prot
|PROT_WRITE
);
989 wld_memset ((void *) zero
, '\0', zeropage
- zero
);
990 if ((c
->prot
& PROT_WRITE
) == 0)
991 wld_mprotect ((caddr_t
) (zero
& ~page_mask
), page_size
, c
->prot
);
994 if (zeroend
> zeropage
)
996 /* Map the remaining zero pages in from the zero fill FD. */
997 wld_mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
998 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
1006 if (l
->l_phdr
== NULL
) fatal_error("no program header\n");
1008 l
->l_phdr
= (void *)((ElfW(Addr
))l
->l_phdr
+ l
->l_addr
);
1009 l
->l_entry
+= l
->l_addr
;
1015 static unsigned int wld_elf_hash( const char *name
)
1017 unsigned int hi
, hash
= 0;
1020 hash
= (hash
<< 4) + (unsigned char)*name
++;
1021 hi
= hash
& 0xf0000000;
1028 static unsigned int gnu_hash( const char *name
)
1030 unsigned int h
= 5381;
1031 while (*name
) h
= h
* 33 + (unsigned char)*name
++;
1036 * Find a symbol in the symbol table of the executable loaded
1038 static void *find_symbol( const struct wld_link_map
*map
, const char *var
, int type
)
1040 const ElfW(Dyn
) *dyn
= NULL
;
1041 const ElfW(Phdr
) *ph
;
1042 const ElfW(Sym
) *symtab
= NULL
;
1043 const Elf32_Word
*hashtab
= NULL
;
1044 const Elf32_Word
*gnu_hashtab
= NULL
;
1045 const char *strings
= NULL
;
1048 /* check the values */
1050 wld_printf("%p %x\n", map
->l_phdr
, map
->l_phnum
);
1052 /* parse the (already loaded) ELF executable's header */
1053 for (ph
= map
->l_phdr
; ph
< &map
->l_phdr
[map
->l_phnum
]; ++ph
)
1055 if( PT_DYNAMIC
== ph
->p_type
)
1057 dyn
= (void *)(ph
->p_vaddr
+ map
->l_addr
);
1061 if( !dyn
) return NULL
;
1065 if( dyn
->d_tag
== DT_STRTAB
)
1066 strings
= (const char*)(dyn
->d_un
.d_ptr
+ map
->l_addr
);
1067 if( dyn
->d_tag
== DT_SYMTAB
)
1068 symtab
= (const ElfW(Sym
) *)(dyn
->d_un
.d_ptr
+ map
->l_addr
);
1069 if( dyn
->d_tag
== DT_HASH
)
1070 hashtab
= (const Elf32_Word
*)(dyn
->d_un
.d_ptr
+ map
->l_addr
);
1071 if( dyn
->d_tag
== DT_GNU_HASH
)
1072 gnu_hashtab
= (const Elf32_Word
*)(dyn
->d_un
.d_ptr
+ map
->l_addr
);
1074 wld_printf("%lx %p\n", (unsigned long)dyn
->d_tag
, (void *)dyn
->d_un
.d_ptr
);
1079 if( (!symtab
) || (!strings
) ) return NULL
;
1081 if (gnu_hashtab
) /* new style hash table */
1083 const unsigned int hash
= gnu_hash(var
);
1084 const Elf32_Word nbuckets
= gnu_hashtab
[0];
1085 const Elf32_Word symbias
= gnu_hashtab
[1];
1086 const Elf32_Word nwords
= gnu_hashtab
[2];
1087 const ElfW(Addr
) *bitmask
= (const ElfW(Addr
) *)(gnu_hashtab
+ 4);
1088 const Elf32_Word
*buckets
= (const Elf32_Word
*)(bitmask
+ nwords
);
1089 const Elf32_Word
*chains
= buckets
+ nbuckets
- symbias
;
1091 if (!(idx
= buckets
[hash
% nbuckets
])) return NULL
;
1094 if ((chains
[idx
] & ~1u) == (hash
& ~1u) &&
1095 ELF32_ST_BIND(symtab
[idx
].st_info
) == STB_GLOBAL
&&
1096 ELF32_ST_TYPE(symtab
[idx
].st_info
) == type
&&
1097 !wld_strcmp( strings
+ symtab
[idx
].st_name
, var
))
1099 } while (!(chains
[idx
++] & 1u));
1101 else if (hashtab
) /* old style hash table */
1103 const unsigned int hash
= wld_elf_hash(var
);
1104 const Elf32_Word nbuckets
= hashtab
[0];
1105 const Elf32_Word
*buckets
= hashtab
+ 2;
1106 const Elf32_Word
*chains
= buckets
+ nbuckets
;
1108 for (idx
= buckets
[hash
% nbuckets
]; idx
; idx
= chains
[idx
])
1110 if (ELF32_ST_BIND(symtab
[idx
].st_info
) == STB_GLOBAL
&&
1111 ELF32_ST_TYPE(symtab
[idx
].st_info
) == type
&&
1112 !wld_strcmp( strings
+ symtab
[idx
].st_name
, var
))
1120 wld_printf("Found %s -> %p\n", strings
+ symtab
[idx
].st_name
, (void *)symtab
[idx
].st_value
);
1122 return (void *)(symtab
[idx
].st_value
+ map
->l_addr
);
1128 * Reserve a range specified in string format
1130 static void preload_reserve( const char *str
)
1133 unsigned long result
= 0;
1134 void *start
= NULL
, *end
= NULL
;
1137 for (p
= str
; *p
; p
++)
1139 if (*p
>= '0' && *p
<= '9') result
= result
* 16 + *p
- '0';
1140 else if (*p
>= 'a' && *p
<= 'f') result
= result
* 16 + *p
- 'a' + 10;
1141 else if (*p
>= 'A' && *p
<= 'F') result
= result
* 16 + *p
- 'A' + 10;
1144 if (!first
) goto error
;
1145 start
= (void *)(result
& ~page_mask
);
1151 if (!first
) end
= (void *)((result
+ page_mask
) & ~page_mask
);
1152 else if (result
) goto error
; /* single value '0' is allowed */
1155 if (end
<= start
) start
= end
= NULL
;
1156 else if ((char *)end
> preloader_start
&&
1157 (char *)start
<= preloader_end
)
1159 wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %p-%p\n",
1160 start
, end
, preloader_start
, preloader_end
);
1164 /* check for overlap with low memory areas */
1165 for (i
= 0; preload_info
[i
].size
; i
++)
1167 if ((char *)preload_info
[i
].addr
> (char *)0x00110000) break;
1168 if ((char *)end
<= (char *)preload_info
[i
].addr
+ preload_info
[i
].size
)
1173 if ((char *)start
< (char *)preload_info
[i
].addr
+ preload_info
[i
].size
)
1174 start
= (char *)preload_info
[i
].addr
+ preload_info
[i
].size
;
1177 while (preload_info
[i
].size
) i
++;
1178 preload_info
[i
].addr
= start
;
1179 preload_info
[i
].size
= (char *)end
- (char *)start
;
1183 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str
);
1186 /* check if address is in one of the reserved ranges */
1187 static int is_addr_reserved( const void *addr
)
1191 for (i
= 0; preload_info
[i
].size
; i
++)
1193 if ((const char *)addr
>= (const char *)preload_info
[i
].addr
&&
1194 (const char *)addr
< (const char *)preload_info
[i
].addr
+ preload_info
[i
].size
)
1200 /* remove a range from the preload list */
1201 static void remove_preload_range( int i
)
1203 while (preload_info
[i
].size
)
1205 preload_info
[i
].addr
= preload_info
[i
+1].addr
;
1206 preload_info
[i
].size
= preload_info
[i
+1].size
;
1212 * is_in_preload_range
1214 * Check if address of the given aux value is in one of the reserved ranges
1216 static int is_in_preload_range( const struct wld_auxv
*av
, int type
)
1218 while (av
->a_type
!= AT_NULL
)
1220 if (av
->a_type
== type
) return is_addr_reserved( (const void *)av
->a_un
.a_val
);
1226 /* set the process name if supported */
1227 static void set_process_name( int argc
, char *argv
[] )
1231 char *p
, *name
, *end
;
1233 /* set the process short name */
1234 for (p
= name
= argv
[1]; *p
; p
++) if (p
[0] == '/' && p
[1]) name
= p
+ 1;
1235 if (wld_prctl( 15 /* PR_SET_NAME */, (long)name
) == -1) return;
1237 /* find the end of the argv array and move everything down */
1238 end
= argv
[argc
- 1];
1240 off
= argv
[1] - argv
[0];
1241 for (p
= argv
[1]; p
<= end
; p
++) *(p
- off
) = *p
;
1242 wld_memset( end
- off
, 0, off
);
1243 for (i
= 1; i
< argc
; i
++) argv
[i
] -= off
;
1250 * Repeat the actions the kernel would do when loading a dynamically linked .so
1251 * Load the binary and then its ELF interpreter.
1252 * Note, we assume that the binary is a dynamically linked ELF shared object.
1254 void* wld_start( void **stack
)
1258 char *interp
, *reserve
= NULL
;
1259 struct wld_auxv new_av
[8], delete_av
[3], *av
;
1260 struct wld_link_map main_binary_map
, ld_so_map
;
1261 struct wine_preload_info
**wine_main_preload_info
;
1264 argv
= (char **)pargc
+ 1;
1265 if (*pargc
< 2) fatal_error( "Usage: %s wine_binary [args]\n", argv
[0] );
1267 /* skip over the parameters */
1268 p
= argv
+ *pargc
+ 1;
1270 /* skip over the environment */
1273 static const char res
[] = "WINEPRELOADRESERVE=";
1274 if (!wld_strncmp( *p
, res
, sizeof(res
)-1 )) reserve
= *p
+ sizeof(res
) - 1;
1278 av
= (struct wld_auxv
*)(p
+1);
1279 page_size
= get_auxiliary( av
, AT_PAGESZ
, 4096 );
1280 page_mask
= page_size
- 1;
1282 preloader_start
= (char *)_start
- ((unsigned long)_start
& page_mask
);
1283 preloader_end
= (char *)((unsigned long)(_end
+ page_mask
) & ~page_mask
);
1285 #ifdef DUMP_AUX_INFO
1286 wld_printf( "stack = %p\n", *stack
);
1287 for( i
= 0; i
< *pargc
; i
++ ) wld_printf("argv[%lx] = %s\n", i
, argv
[i
]);
1288 dump_auxiliary( av
);
1291 /* reserve memory that Wine needs */
1292 if (reserve
) preload_reserve( reserve
);
1293 for (i
= 0; preload_info
[i
].size
; i
++)
1295 if ((char *)av
>= (char *)preload_info
[i
].addr
&&
1296 (char *)pargc
<= (char *)preload_info
[i
].addr
+ preload_info
[i
].size
)
1298 remove_preload_range( i
);
1301 else if (wld_mmap( preload_info
[i
].addr
, preload_info
[i
].size
, PROT_NONE
,
1302 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
, -1, 0 ) == (void *)-1)
1304 /* don't warn for low 64k */
1305 if (preload_info
[i
].addr
>= (void *)0x10000
1307 && preload_info
[i
].addr
< (void *)0x7fffffffff /* ARM64 address space might end here*/
1310 wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
1311 preload_info
[i
].addr
, (char *)preload_info
[i
].addr
+ preload_info
[i
].size
);
1312 remove_preload_range( i
);
1317 /* add an executable page at the top of the address space to defeat
1318 * broken no-exec protections that play with the code selector limit */
1319 if (is_addr_reserved( (char *)0x80000000 - page_size
))
1320 wld_mprotect( (char *)0x80000000 - page_size
, page_size
, PROT_EXEC
| PROT_READ
);
1322 /* load the main binary */
1323 map_so_lib( argv
[1], &main_binary_map
);
1325 /* load the ELF interpreter */
1326 interp
= (char *)main_binary_map
.l_addr
+ main_binary_map
.l_interp
;
1327 map_so_lib( interp
, &ld_so_map
);
1329 /* store pointer to the preload info into the appropriate main binary variable */
1330 wine_main_preload_info
= find_symbol( &main_binary_map
, "wine_main_preload_info", STT_OBJECT
);
1331 if (wine_main_preload_info
) *wine_main_preload_info
= preload_info
;
1332 else wld_printf( "wine_main_preload_info not found\n" );
1334 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
1335 SET_NEW_AV( 0, AT_PHDR
, (unsigned long)main_binary_map
.l_phdr
);
1336 SET_NEW_AV( 1, AT_PHENT
, sizeof(ElfW(Phdr
)) );
1337 SET_NEW_AV( 2, AT_PHNUM
, main_binary_map
.l_phnum
);
1338 SET_NEW_AV( 3, AT_PAGESZ
, page_size
);
1339 SET_NEW_AV( 4, AT_BASE
, ld_so_map
.l_addr
);
1340 SET_NEW_AV( 5, AT_FLAGS
, get_auxiliary( av
, AT_FLAGS
, 0 ) );
1341 SET_NEW_AV( 6, AT_ENTRY
, main_binary_map
.l_entry
);
1342 SET_NEW_AV( 7, AT_NULL
, 0 );
1346 /* delete sysinfo values if addresses conflict */
1347 if (is_in_preload_range( av
, AT_SYSINFO
) || is_in_preload_range( av
, AT_SYSINFO_EHDR
))
1349 delete_av
[i
++].a_type
= AT_SYSINFO
;
1350 delete_av
[i
++].a_type
= AT_SYSINFO_EHDR
;
1352 delete_av
[i
].a_type
= AT_NULL
;
1354 /* get rid of first argument */
1355 set_process_name( *pargc
, argv
);
1356 pargc
[1] = pargc
[0] - 1;
1359 set_auxiliary_values( av
, new_av
, delete_av
, stack
);
1361 #ifdef DUMP_AUX_INFO
1362 wld_printf("new stack = %p\n", *stack
);
1363 wld_printf("jumping to %p\n", (void *)ld_so_map
.l_entry
);
1368 int len
, fd
= wld_open( "/proc/self/maps", O_RDONLY
);
1371 while ((len
= wld_read( fd
, buffer
, sizeof(buffer
) )) > 0) wld_write( 2, buffer
, len
);
1377 return (void *)ld_so_map
.l_entry
;
1380 #endif /* __linux__ */