msvcrt: Don't read past end of string in _strnicoll/_wcsnicoll.
[wine.git] / loader / preloader.c
blob725926ed8334264f4e1b16e79b7bc24f808953bc
1 /*
2 * Preloader for ld.so
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
24 * Design notes
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
31 * interpreter.
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
63 #include "config.h"
64 #include "wine/port.h"
66 #include <stdarg.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <sys/types.h>
71 #ifdef HAVE_SYS_STAT_H
72 # include <sys/stat.h>
73 #endif
74 #include <fcntl.h>
75 #ifdef HAVE_SYS_MMAN_H
76 # include <sys/mman.h>
77 #endif
78 #ifdef HAVE_SYS_SYSCALL_H
79 # include <sys/syscall.h>
80 #endif
81 #ifdef HAVE_UNISTD_H
82 # include <unistd.h>
83 #endif
84 #ifdef HAVE_ELF_H
85 # include <elf.h>
86 #endif
87 #ifdef HAVE_LINK_H
88 # include <link.h>
89 #endif
90 #ifdef HAVE_SYS_LINK_H
91 # include <sys/link.h>
92 #endif
94 #include "main.h"
96 /* ELF definitions */
97 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
98 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
100 #define MAP_BASE_ADDR(l) 0
102 #ifndef MAP_COPY
103 #define MAP_COPY MAP_PRIVATE
104 #endif
105 #ifndef MAP_NORESERVE
106 #define MAP_NORESERVE 0
107 #endif
109 static struct wine_preload_info preload_info[] =
111 #ifdef __i386__
112 { (void *)0x00000000, 0x00010000 }, /* low 64k */
113 { (void *)0x00010000, 0x00100000 }, /* DOS area */
114 { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
115 { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
116 #else
117 { (void *)0x000000010000, 0x00100000 }, /* DOS area */
118 { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
119 { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
120 { (void *)0x7ffffe000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
121 #endif
122 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
123 { 0, 0 } /* end of list */
126 /* debugging */
127 #undef DUMP_SEGMENTS
128 #undef DUMP_AUX_INFO
129 #undef DUMP_SYMS
130 #undef DUMP_MAPS
132 /* older systems may not define these */
133 #ifndef PT_TLS
134 #define PT_TLS 7
135 #endif
137 #ifndef AT_SYSINFO
138 #define AT_SYSINFO 32
139 #endif
140 #ifndef AT_SYSINFO_EHDR
141 #define AT_SYSINFO_EHDR 33
142 #endif
144 #ifndef DT_GNU_HASH
145 #define DT_GNU_HASH 0x6ffffef5
146 #endif
148 static size_t page_size, page_mask;
149 static char *preloader_start, *preloader_end;
151 struct wld_link_map {
152 ElfW(Addr) l_addr;
153 ElfW(Dyn) *l_ld;
154 ElfW(Phdr)*l_phdr;
155 ElfW(Addr) l_entry;
156 ElfW(Half) l_ldnum;
157 ElfW(Half) l_phnum;
158 ElfW(Addr) l_map_start, l_map_end;
159 ElfW(Addr) l_interp;
162 struct wld_auxv
164 ElfW(Addr) a_type;
165 union
167 ElfW(Addr) a_val;
168 } a_un;
172 * The __bb_init_func is an empty function only called when file is
173 * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
174 * function is normally provided by libc's startup files, but since we
175 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
176 * provide our own (empty) version, otherwise linker fails.
178 void __bb_init_func(void) { return; }
180 /* similar to the above but for -fstack-protector */
181 void *__stack_chk_guard = 0;
182 void __stack_chk_fail_local(void) { return; }
183 void __stack_chk_fail(void) { return; }
185 #ifdef __i386__
187 /* data for setting up the glibc-style thread-local storage in %gs */
189 static int thread_data[256];
191 struct
193 /* this is the kernel modify_ldt struct */
194 unsigned int entry_number;
195 unsigned long base_addr;
196 unsigned int limit;
197 unsigned int seg_32bit : 1;
198 unsigned int contents : 2;
199 unsigned int read_exec_only : 1;
200 unsigned int limit_in_pages : 1;
201 unsigned int seg_not_present : 1;
202 unsigned int usable : 1;
203 unsigned int garbage : 25;
204 } thread_ldt = { -1, (unsigned long)thread_data, 0xfffff, 1, 0, 0, 1, 0, 1, 0 };
208 * The _start function is the entry and exit point of this program
210 * It calls wld_start, passing a pointer to the args it receives
211 * then jumps to the address wld_start returns.
213 void _start(void);
214 extern char _end[];
215 __ASM_GLOBAL_FUNC(_start,
216 __ASM_CFI("\t.cfi_undefined %eip\n")
217 "\tmovl $243,%eax\n" /* SYS_set_thread_area */
218 "\tmovl $thread_ldt,%ebx\n"
219 "\tint $0x80\n" /* allocate gs segment */
220 "\torl %eax,%eax\n"
221 "\tjl 1f\n"
222 "\tmovl thread_ldt,%eax\n" /* thread_ldt.entry_number */
223 "\tshl $3,%eax\n"
224 "\torl $3,%eax\n"
225 "\tmov %ax,%gs\n"
226 "\tmov %ax,%fs\n" /* set %fs too so libwine can retrieve it later on */
227 "1:\tmovl %esp,%eax\n"
228 "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */
229 "\tpushl %eax\n" /* orig stack pointer */
230 "\tpushl %esp\n" /* ptr to orig stack pointer */
231 "\tcall wld_start\n"
232 "\tpopl %ecx\n" /* remove ptr to stack pointer */
233 "\tpopl %esp\n" /* new stack pointer */
234 "\tpush %eax\n" /* ELF interpreter entry point */
235 "\txor %eax,%eax\n"
236 "\txor %ecx,%ecx\n"
237 "\txor %edx,%edx\n"
238 "\tmov %ax,%gs\n" /* clear %gs again */
239 "\tret\n")
241 /* wrappers for Linux system calls */
243 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
245 static inline __attribute__((noreturn)) void wld_exit( int code )
247 for (;;) /* avoid warning */
248 __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
249 : : "a" (1 /* SYS_exit */), "r" (code) );
252 static inline int wld_open( const char *name, int flags )
254 int ret;
255 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
256 : "=a" (ret) : "0" (5 /* SYS_open */), "r" (name), "c" (flags) );
257 return SYSCALL_RET(ret);
260 static inline int wld_close( int fd )
262 int ret;
263 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
264 : "=a" (ret) : "0" (6 /* SYS_close */), "r" (fd) );
265 return SYSCALL_RET(ret);
268 static inline ssize_t wld_read( int fd, void *buffer, size_t len )
270 int ret;
271 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
272 : "=a" (ret)
273 : "0" (3 /* SYS_read */), "r" (fd), "c" (buffer), "d" (len)
274 : "memory" );
275 return SYSCALL_RET(ret);
278 static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
280 int ret;
281 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
282 : "=a" (ret) : "0" (4 /* SYS_write */), "r" (fd), "c" (buffer), "d" (len) );
283 return SYSCALL_RET(ret);
286 static inline int wld_mprotect( const void *addr, size_t len, int prot )
288 int ret;
289 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
290 : "=a" (ret) : "0" (125 /* SYS_mprotect */), "r" (addr), "c" (len), "d" (prot) );
291 return SYSCALL_RET(ret);
294 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, unsigned int offset );
295 __ASM_GLOBAL_FUNC(wld_mmap,
296 "\tpushl %ebp\n"
297 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
298 "\tpushl %ebx\n"
299 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
300 "\tpushl %esi\n"
301 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
302 "\tpushl %edi\n"
303 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
304 "\tmovl $192,%eax\n" /* SYS_mmap2 */
305 "\tmovl 20(%esp),%ebx\n" /* start */
306 "\tmovl 24(%esp),%ecx\n" /* len */
307 "\tmovl 28(%esp),%edx\n" /* prot */
308 "\tmovl 32(%esp),%esi\n" /* flags */
309 "\tmovl 36(%esp),%edi\n" /* fd */
310 "\tmovl 40(%esp),%ebp\n" /* offset */
311 "\tshrl $12,%ebp\n"
312 "\tint $0x80\n"
313 "\tcmpl $-4096,%eax\n"
314 "\tjbe 2f\n"
315 "\tcmpl $-38,%eax\n" /* ENOSYS */
316 "\tjne 1f\n"
317 "\tmovl $90,%eax\n" /* SYS_mmap */
318 "\tleal 20(%esp),%ebx\n"
319 "\tint $0x80\n"
320 "\tcmpl $-4096,%eax\n"
321 "\tjbe 2f\n"
322 "1:\tmovl $-1,%eax\n"
323 "2:\tpopl %edi\n"
324 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
325 "\tpopl %esi\n"
326 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
327 "\tpopl %ebx\n"
328 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
329 "\tpopl %ebp\n"
330 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
331 "\tret\n" )
333 static inline uid_t wld_getuid(void)
335 uid_t ret;
336 __asm__( "int $0x80" : "=a" (ret) : "0" (24 /* SYS_getuid */) );
337 return ret;
340 static inline uid_t wld_geteuid(void)
342 uid_t ret;
343 __asm__( "int $0x80" : "=a" (ret) : "0" (49 /* SYS_geteuid */) );
344 return ret;
347 static inline gid_t wld_getgid(void)
349 gid_t ret;
350 __asm__( "int $0x80" : "=a" (ret) : "0" (47 /* SYS_getgid */) );
351 return ret;
354 static inline gid_t wld_getegid(void)
356 gid_t ret;
357 __asm__( "int $0x80" : "=a" (ret) : "0" (50 /* SYS_getegid */) );
358 return ret;
361 static inline int wld_prctl( int code, long arg )
363 int ret;
364 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
365 : "=a" (ret) : "0" (172 /* SYS_prctl */), "r" (code), "c" (arg) );
366 return SYSCALL_RET(ret);
369 #elif defined(__x86_64__)
371 void *thread_data[256];
374 * The _start function is the entry and exit point of this program
376 * It calls wld_start, passing a pointer to the args it receives
377 * then jumps to the address wld_start returns.
379 void _start(void);
380 extern char _end[];
381 __ASM_GLOBAL_FUNC(_start,
382 __ASM_CFI(".cfi_undefined %rip\n\t")
383 "movq %rsp,%rax\n\t"
384 "leaq -144(%rsp),%rsp\n\t" /* allocate some space for extra aux values */
385 "movq %rax,(%rsp)\n\t" /* orig stack pointer */
386 "movq $thread_data,%rsi\n\t"
387 "movq $0x1002,%rdi\n\t" /* ARCH_SET_FS */
388 "movq $158,%rax\n\t" /* SYS_arch_prctl */
389 "syscall\n\t"
390 "movq %rsp,%rdi\n\t" /* ptr to orig stack pointer */
391 "call wld_start\n\t"
392 "movq (%rsp),%rsp\n\t" /* new stack pointer */
393 "pushq %rax\n\t" /* ELF interpreter entry point */
394 "xorq %rax,%rax\n\t"
395 "xorq %rcx,%rcx\n\t"
396 "xorq %rdx,%rdx\n\t"
397 "xorq %rsi,%rsi\n\t"
398 "xorq %rdi,%rdi\n\t"
399 "xorq %r8,%r8\n\t"
400 "xorq %r9,%r9\n\t"
401 "xorq %r10,%r10\n\t"
402 "xorq %r11,%r11\n\t"
403 "ret")
405 #define SYSCALL_FUNC( name, nr ) \
406 __ASM_GLOBAL_FUNC( name, \
407 "movq $" #nr ",%rax\n\t" \
408 "movq %rcx,%r10\n\t" \
409 "syscall\n\t" \
410 "leaq 4096(%rax),%rcx\n\t" \
411 "movq $-1,%rdx\n\t" \
412 "cmp $4096,%rcx\n\t" \
413 "cmovb %rdx,%rax\n\t" \
414 "ret" )
416 #define SYSCALL_NOERR( name, nr ) \
417 __ASM_GLOBAL_FUNC( name, \
418 "movq $" #nr ",%rax\n\t" \
419 "syscall\n\t" \
420 "ret" )
422 void wld_exit( int code ) __attribute__((noreturn));
423 SYSCALL_NOERR( wld_exit, 60 /* SYS_exit */ );
425 ssize_t wld_read( int fd, void *buffer, size_t len );
426 SYSCALL_FUNC( wld_read, 0 /* SYS_read */ );
428 ssize_t wld_write( int fd, const void *buffer, size_t len );
429 SYSCALL_FUNC( wld_write, 1 /* SYS_write */ );
431 int wld_open( const char *name, int flags );
432 SYSCALL_FUNC( wld_open, 2 /* SYS_open */ );
434 int wld_close( int fd );
435 SYSCALL_FUNC( wld_close, 3 /* SYS_close */ );
437 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
438 SYSCALL_FUNC( wld_mmap, 9 /* SYS_mmap */ );
440 int wld_mprotect( const void *addr, size_t len, int prot );
441 SYSCALL_FUNC( wld_mprotect, 10 /* SYS_mprotect */ );
443 int wld_prctl( int code, long arg );
444 SYSCALL_FUNC( wld_prctl, 157 /* SYS_prctl */ );
446 uid_t wld_getuid(void);
447 SYSCALL_NOERR( wld_getuid, 102 /* SYS_getuid */ );
449 gid_t wld_getgid(void);
450 SYSCALL_NOERR( wld_getgid, 104 /* SYS_getgid */ );
452 uid_t wld_geteuid(void);
453 SYSCALL_NOERR( wld_geteuid, 107 /* SYS_geteuid */ );
455 gid_t wld_getegid(void);
456 SYSCALL_NOERR( wld_getegid, 108 /* SYS_getegid */ );
458 #elif defined(__aarch64__)
460 void *thread_data[256];
463 * The _start function is the entry and exit point of this program
465 * It calls wld_start, passing a pointer to the args it receives
466 * then jumps to the address wld_start returns.
468 void _start(void);
469 extern char _end[];
470 __ASM_GLOBAL_FUNC(_start,
471 "mov x0, SP\n\t"
472 "sub SP, SP, #144\n\t" /* allocate some space for extra aux values */
473 "str x0, [SP]\n\t" /* orig stack pointer */
474 "ldr x0, =thread_data\n\t"
475 "msr tpidr_el0, x0\n\t"
476 "mov x0, SP\n\t" /* ptr to orig stack pointer */
477 "bl wld_start\n\t"
478 "ldr x1, [SP]\n\t" /* new stack pointer */
479 "mov SP, x1\n\t"
480 "mov x30, x0\n\t"
481 "mov x0, #0\n\t"
482 "mov x1, #0\n\t"
483 "mov x2, #0\n\t"
484 "mov x3, #0\n\t"
485 "mov x4, #0\n\t"
486 "mov x5, #0\n\t"
487 "mov x6, #0\n\t"
488 "mov x7, #0\n\t"
489 "mov x8, #0\n\t"
490 "mov x9, #0\n\t"
491 "mov x10, #0\n\t"
492 "mov x11, #0\n\t"
493 "mov x12, #0\n\t"
494 "mov x13, #0\n\t"
495 "mov x14, #0\n\t"
496 "mov x15, #0\n\t"
497 "mov x16, #0\n\t"
498 "mov x17, #0\n\t"
499 "mov x18, #0\n\t"
500 "ret")
502 #define SYSCALL_FUNC( name, nr ) \
503 __ASM_GLOBAL_FUNC( name, \
504 "stp x8, x9, [SP, #-16]!\n\t" \
505 "mov x8, #" #nr "\n\t" \
506 "svc #0\n\t" \
507 "ldp x8, x9, [SP], #16\n\t" \
508 "cmn x0, #1, lsl#12\n\t" \
509 "cinv x0, x0, hi\n\t" \
510 "b.hi 1f\n\t" \
511 "ret\n\t" \
512 "1: mov x0, #-1\n\t" \
513 "ret" )
515 #define SYSCALL_NOERR( name, nr ) \
516 __ASM_GLOBAL_FUNC( name, \
517 "stp x8, x9, [SP, #-16]!\n\t" \
518 "mov x8, #" #nr "\n\t" \
519 "svc #0\n\t" \
520 "ldp x8, x9, [SP], #16\n\t" \
521 "ret" )
523 void wld_exit( int code ) __attribute__((noreturn));
524 SYSCALL_NOERR( wld_exit, 93 /* SYS_exit */ );
526 ssize_t wld_read( int fd, void *buffer, size_t len );
527 SYSCALL_FUNC( wld_read, 63 /* SYS_read */ );
529 ssize_t wld_write( int fd, const void *buffer, size_t len );
530 SYSCALL_FUNC( wld_write, 64 /* SYS_write */ );
532 int wld_openat( int dirfd, const char *name, int flags );
533 SYSCALL_FUNC( wld_openat, 56 /* SYS_openat */ );
535 int wld_open( const char *name, int flags )
537 return wld_openat(-100 /* AT_FDCWD */, name, flags);
540 int wld_close( int fd );
541 SYSCALL_FUNC( wld_close, 57 /* SYS_close */ );
543 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
544 SYSCALL_FUNC( wld_mmap, 222 /* SYS_mmap */ );
546 int wld_mprotect( const void *addr, size_t len, int prot );
547 SYSCALL_FUNC( wld_mprotect, 226 /* SYS_mprotect */ );
549 int wld_prctl( int code, long arg );
550 SYSCALL_FUNC( wld_prctl, 167 /* SYS_prctl */ );
552 uid_t wld_getuid(void);
553 SYSCALL_NOERR( wld_getuid, 174 /* SYS_getuid */ );
555 gid_t wld_getgid(void);
556 SYSCALL_NOERR( wld_getgid, 176 /* SYS_getgid */ );
558 uid_t wld_geteuid(void);
559 SYSCALL_NOERR( wld_geteuid, 175 /* SYS_geteuid */ );
561 gid_t wld_getegid(void);
562 SYSCALL_NOERR( wld_getegid, 177 /* SYS_getegid */ );
564 #else
565 #error preloader not implemented for this CPU
566 #endif
568 /* replacement for libc functions */
570 static int wld_strcmp( const char *str1, const char *str2 )
572 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
573 return *str1 - *str2;
576 static int wld_strncmp( const char *str1, const char *str2, size_t len )
578 if (len <= 0) return 0;
579 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
580 return *str1 - *str2;
583 static inline void *wld_memset( void *dest, int val, size_t len )
585 char *dst = dest;
586 while (len--) *dst++ = val;
587 return dest;
591 * wld_printf - just the basics
593 * %x prints a hex number
594 * %s prints a string
595 * %p prints a pointer
597 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
599 static const char hex_chars[16] = "0123456789abcdef";
600 const char *p = fmt;
601 char *str = buffer;
602 int i;
604 while( *p )
606 if( *p == '%' )
608 p++;
609 if( *p == 'x' )
611 unsigned int x = va_arg( args, unsigned int );
612 for (i = 2*sizeof(x) - 1; i >= 0; i--)
613 *str++ = hex_chars[(x>>(i*4))&0xf];
615 else if (p[0] == 'l' && p[1] == 'x')
617 unsigned long x = va_arg( args, unsigned long );
618 for (i = 2*sizeof(x) - 1; i >= 0; i--)
619 *str++ = hex_chars[(x>>(i*4))&0xf];
620 p++;
622 else if( *p == 'p' )
624 unsigned long x = (unsigned long)va_arg( args, void * );
625 for (i = 2*sizeof(x) - 1; i >= 0; i--)
626 *str++ = hex_chars[(x>>(i*4))&0xf];
628 else if( *p == 's' )
630 char *s = va_arg( args, char * );
631 while(*s)
632 *str++ = *s++;
634 else if( *p == 0 )
635 break;
636 p++;
638 *str++ = *p++;
640 *str = 0;
641 return str - buffer;
644 static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
646 va_list args;
647 char buffer[256];
648 int len;
650 va_start( args, fmt );
651 len = wld_vsprintf(buffer, fmt, args );
652 va_end( args );
653 wld_write(2, buffer, len);
656 static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
658 va_list args;
659 char buffer[256];
660 int len;
662 va_start( args, fmt );
663 len = wld_vsprintf(buffer, fmt, args );
664 va_end( args );
665 wld_write(2, buffer, len);
666 wld_exit(1);
669 #ifdef DUMP_AUX_INFO
671 * Dump interesting bits of the ELF auxv_t structure that is passed
672 * as the 4th parameter to the _start function
674 static void dump_auxiliary( struct wld_auxv *av )
676 #define NAME(at) { at, #at }
677 static const struct { int val; const char *name; } names[] =
679 NAME(AT_BASE),
680 NAME(AT_CLKTCK),
681 NAME(AT_EGID),
682 NAME(AT_ENTRY),
683 NAME(AT_EUID),
684 NAME(AT_FLAGS),
685 NAME(AT_GID),
686 NAME(AT_HWCAP),
687 NAME(AT_PAGESZ),
688 NAME(AT_PHDR),
689 NAME(AT_PHENT),
690 NAME(AT_PHNUM),
691 NAME(AT_PLATFORM),
692 NAME(AT_SYSINFO),
693 NAME(AT_SYSINFO_EHDR),
694 NAME(AT_UID),
695 { 0, NULL }
697 #undef NAME
699 int i;
701 for ( ; av->a_type != AT_NULL; av++)
703 for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
704 if (names[i].name) wld_printf("%s = %lx\n", names[i].name, (unsigned long)av->a_un.a_val);
705 else wld_printf( "%lx = %lx\n", (unsigned long)av->a_type, (unsigned long)av->a_un.a_val );
708 #endif
711 * set_auxiliary_values
713 * Set the new auxiliary values
715 static void set_auxiliary_values( struct wld_auxv *av, const struct wld_auxv *new_av,
716 const struct wld_auxv *delete_av, void **stack )
718 int i, j, av_count = 0, new_count = 0, delete_count = 0;
719 char *src, *dst;
721 /* count how many aux values we have already */
722 while (av[av_count].a_type != AT_NULL) av_count++;
724 /* delete unwanted values */
725 for (j = 0; delete_av[j].a_type != AT_NULL; j++)
727 for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
729 av[i].a_type = av[av_count-1].a_type;
730 av[i].a_un.a_val = av[av_count-1].a_un.a_val;
731 av[--av_count].a_type = AT_NULL;
732 delete_count++;
733 break;
737 /* count how many values we have in new_av that aren't in av */
738 for (j = 0; new_av[j].a_type != AT_NULL; j++)
740 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
741 if (i == av_count) new_count++;
744 src = (char *)*stack;
745 dst = src - (new_count - delete_count) * sizeof(*av);
746 dst = (char *)((unsigned long)dst & ~15);
747 if (dst < src) /* need to make room for the extra values */
749 int len = (char *)(av + av_count + 1) - src;
750 for (i = 0; i < len; i++) dst[i] = src[i];
752 else if (dst > src) /* get rid of unused values */
754 int len = (char *)(av + av_count + 1) - src;
755 for (i = len - 1; i >= 0; i--) dst[i] = src[i];
757 *stack = dst;
758 av = (struct wld_auxv *)((char *)av + (dst - src));
760 /* now set the values */
761 for (j = 0; new_av[j].a_type != AT_NULL; j++)
763 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
764 if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
765 else
767 av[av_count].a_type = new_av[j].a_type;
768 av[av_count].a_un.a_val = new_av[j].a_un.a_val;
769 av_count++;
773 #ifdef DUMP_AUX_INFO
774 wld_printf("New auxiliary info:\n");
775 dump_auxiliary( av );
776 #endif
780 * get_auxiliary
782 * Get a field of the auxiliary structure
784 static int get_auxiliary( struct wld_auxv *av, int type, int def_val )
786 for ( ; av->a_type != AT_NULL; av++)
787 if( av->a_type == type ) return av->a_un.a_val;
788 return def_val;
792 * map_so_lib
794 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
796 * This function maps the segments from an ELF object, and optionally
797 * stores information about the mapping into the auxv_t structure.
799 static void map_so_lib( const char *name, struct wld_link_map *l)
801 int fd;
802 unsigned char buf[0x800];
803 ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
804 ElfW(Phdr) *phdr, *ph;
805 /* Scan the program header table, collecting its load commands. */
806 struct loadcmd
808 ElfW(Addr) mapstart, mapend, dataend, allocend;
809 off_t mapoff;
810 int prot;
811 } loadcmds[16], *c;
812 size_t nloadcmds = 0, maplength;
814 fd = wld_open( name, O_RDONLY );
815 if (fd == -1) fatal_error("%s: could not open\n", name );
817 if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
818 fatal_error("%s: failed to read ELF header\n", name);
820 phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
822 if( ( header->e_ident[0] != 0x7f ) ||
823 ( header->e_ident[1] != 'E' ) ||
824 ( header->e_ident[2] != 'L' ) ||
825 ( header->e_ident[3] != 'F' ) )
826 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
828 #ifdef __i386__
829 if( header->e_machine != EM_386 )
830 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
831 #elif defined(__x86_64__)
832 if( header->e_machine != EM_X86_64 )
833 fatal_error("%s: not an x86-64 ELF binary... don't know how to load it\n", name );
834 #elif defined(__aarch64__)
835 if( header->e_machine != EM_AARCH64 )
836 fatal_error("%s: not an aarch64 ELF binary... don't know how to load it\n", name );
837 #endif
839 if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
840 fatal_error( "%s: oops... not enough space for load commands\n", name );
842 maplength = header->e_phnum * sizeof (ElfW(Phdr));
843 if (header->e_phoff + maplength > sizeof(buf))
844 fatal_error( "%s: oops... not enough space for ELF headers\n", name );
846 l->l_ld = 0;
847 l->l_addr = 0;
848 l->l_phdr = 0;
849 l->l_phnum = header->e_phnum;
850 l->l_entry = header->e_entry;
851 l->l_interp = 0;
853 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
856 #ifdef DUMP_SEGMENTS
857 wld_printf( "ph = %p\n", ph );
858 wld_printf( " p_type = %lx\n", (unsigned long)ph->p_type );
859 wld_printf( " p_flags = %lx\n", (unsigned long)ph->p_flags );
860 wld_printf( " p_offset = %lx\n", (unsigned long)ph->p_offset );
861 wld_printf( " p_vaddr = %lx\n", (unsigned long)ph->p_vaddr );
862 wld_printf( " p_paddr = %lx\n", (unsigned long)ph->p_paddr );
863 wld_printf( " p_filesz = %lx\n", (unsigned long)ph->p_filesz );
864 wld_printf( " p_memsz = %lx\n", (unsigned long)ph->p_memsz );
865 wld_printf( " p_align = %lx\n", (unsigned long)ph->p_align );
866 #endif
868 switch (ph->p_type)
870 /* These entries tell us where to find things once the file's
871 segments are mapped in. We record the addresses it says
872 verbatim, and later correct for the run-time load address. */
873 case PT_DYNAMIC:
874 l->l_ld = (void *) ph->p_vaddr;
875 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
876 break;
878 case PT_PHDR:
879 l->l_phdr = (void *) ph->p_vaddr;
880 break;
882 case PT_LOAD:
884 if ((ph->p_align & page_mask) != 0)
885 fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
887 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
888 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
890 c = &loadcmds[nloadcmds++];
891 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
892 c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
893 c->dataend = ph->p_vaddr + ph->p_filesz;
894 c->allocend = ph->p_vaddr + ph->p_memsz;
895 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
897 c->prot = 0;
898 if (ph->p_flags & PF_R)
899 c->prot |= PROT_READ;
900 if (ph->p_flags & PF_W)
901 c->prot |= PROT_WRITE;
902 if (ph->p_flags & PF_X)
903 c->prot |= PROT_EXEC;
905 break;
907 case PT_INTERP:
908 l->l_interp = ph->p_vaddr;
909 break;
911 case PT_TLS:
913 * We don't need to set anything up because we're
914 * emulating the kernel, not ld-linux.so.2
915 * The ELF loader will set up the TLS data itself.
917 case PT_SHLIB:
918 case PT_NOTE:
919 default:
920 break;
924 /* Now process the load commands and map segments into memory. */
925 if (!nloadcmds)
926 fatal_error( "%s: no segments to load\n", name );
927 c = loadcmds;
929 /* Length of the sections to be loaded. */
930 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
932 if( header->e_type == ET_DYN )
934 ElfW(Addr) mappref;
935 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
936 - MAP_BASE_ADDR (l));
938 /* Remember which part of the address space this object uses. */
939 l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
940 c->prot, MAP_COPY | MAP_FILE,
941 fd, c->mapoff);
942 /* wld_printf("set : offset = %x\n", c->mapoff); */
943 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
945 l->l_map_end = l->l_map_start + maplength;
946 l->l_addr = l->l_map_start - c->mapstart;
948 wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
949 loadcmds[nloadcmds - 1].allocend - c->mapend,
950 PROT_NONE);
951 goto postmap;
953 else
955 /* sanity check */
956 if ((char *)c->mapstart + maplength > preloader_start &&
957 (char *)c->mapstart <= preloader_end)
958 fatal_error( "%s: binary overlaps preloader (%p-%p)\n",
959 name, (char *)c->mapstart, (char *)c->mapstart + maplength );
961 ELF_FIXED_ADDRESS (loader, c->mapstart);
964 /* Remember which part of the address space this object uses. */
965 l->l_map_start = c->mapstart + l->l_addr;
966 l->l_map_end = l->l_map_start + maplength;
968 while (c < &loadcmds[nloadcmds])
970 if (c->mapend > c->mapstart)
971 /* Map the segment contents from the file. */
972 wld_mmap ((void *) (l->l_addr + c->mapstart),
973 c->mapend - c->mapstart, c->prot,
974 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
976 postmap:
977 if (l->l_phdr == 0
978 && (ElfW(Off)) c->mapoff <= header->e_phoff
979 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
980 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
981 /* Found the program header in this segment. */
982 l->l_phdr = (void *)(unsigned long)(c->mapstart + header->e_phoff - c->mapoff);
984 if (c->allocend > c->dataend)
986 /* Extra zero pages should appear at the end of this segment,
987 after the data mapped from the file. */
988 ElfW(Addr) zero, zeroend, zeropage;
990 zero = l->l_addr + c->dataend;
991 zeroend = l->l_addr + c->allocend;
992 zeropage = (zero + page_mask) & ~page_mask;
995 * This is different from the dl-load load...
996 * ld-linux.so.2 relies on the whole page being zero'ed
998 zeroend = (zeroend + page_mask) & ~page_mask;
1000 if (zeroend < zeropage)
1002 /* All the extra data is in the last page of the segment.
1003 We can just zero it. */
1004 zeropage = zeroend;
1007 if (zeropage > zero)
1009 /* Zero the final part of the last page of the segment. */
1010 if ((c->prot & PROT_WRITE) == 0)
1012 /* Dag nab it. */
1013 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
1015 wld_memset ((void *) zero, '\0', zeropage - zero);
1016 if ((c->prot & PROT_WRITE) == 0)
1017 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
1020 if (zeroend > zeropage)
1022 /* Map the remaining zero pages in from the zero fill FD. */
1023 wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
1024 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1025 -1, 0);
1029 ++c;
1032 if (l->l_phdr == NULL) fatal_error("no program header\n");
1034 l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
1035 l->l_entry += l->l_addr;
1037 wld_close( fd );
1041 static unsigned int wld_elf_hash( const char *name )
1043 unsigned int hi, hash = 0;
1044 while (*name)
1046 hash = (hash << 4) + (unsigned char)*name++;
1047 hi = hash & 0xf0000000;
1048 hash ^= hi;
1049 hash ^= hi >> 24;
1051 return hash;
1054 static unsigned int gnu_hash( const char *name )
1056 unsigned int h = 5381;
1057 while (*name) h = h * 33 + (unsigned char)*name++;
1058 return h;
1062 * Find a symbol in the symbol table of the executable loaded
1064 static void *find_symbol( const struct wld_link_map *map, const char *var, int type )
1066 const ElfW(Dyn) *dyn = NULL;
1067 const ElfW(Phdr) *ph;
1068 const ElfW(Sym) *symtab = NULL;
1069 const Elf32_Word *hashtab = NULL;
1070 const Elf32_Word *gnu_hashtab = NULL;
1071 const char *strings = NULL;
1072 Elf32_Word idx;
1074 /* check the values */
1075 #ifdef DUMP_SYMS
1076 wld_printf("%p %x\n", map->l_phdr, map->l_phnum );
1077 #endif
1078 /* parse the (already loaded) ELF executable's header */
1079 for (ph = map->l_phdr; ph < &map->l_phdr[map->l_phnum]; ++ph)
1081 if( PT_DYNAMIC == ph->p_type )
1083 dyn = (void *)(ph->p_vaddr + map->l_addr);
1084 break;
1087 if( !dyn ) return NULL;
1089 while( dyn->d_tag )
1091 if( dyn->d_tag == DT_STRTAB )
1092 strings = (const char*)(dyn->d_un.d_ptr + map->l_addr);
1093 if( dyn->d_tag == DT_SYMTAB )
1094 symtab = (const ElfW(Sym) *)(dyn->d_un.d_ptr + map->l_addr);
1095 if( dyn->d_tag == DT_HASH )
1096 hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
1097 if( dyn->d_tag == DT_GNU_HASH )
1098 gnu_hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
1099 #ifdef DUMP_SYMS
1100 wld_printf("%lx %p\n", (unsigned long)dyn->d_tag, (void *)dyn->d_un.d_ptr );
1101 #endif
1102 dyn++;
1105 if( (!symtab) || (!strings) ) return NULL;
1107 if (gnu_hashtab) /* new style hash table */
1109 const unsigned int hash = gnu_hash(var);
1110 const Elf32_Word nbuckets = gnu_hashtab[0];
1111 const Elf32_Word symbias = gnu_hashtab[1];
1112 const Elf32_Word nwords = gnu_hashtab[2];
1113 const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
1114 const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
1115 const Elf32_Word *chains = buckets + nbuckets - symbias;
1117 if (!(idx = buckets[hash % nbuckets])) return NULL;
1120 if ((chains[idx] & ~1u) == (hash & ~1u) &&
1121 ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
1122 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
1123 !wld_strcmp( strings + symtab[idx].st_name, var ))
1124 goto found;
1125 } while (!(chains[idx++] & 1u));
1127 else if (hashtab) /* old style hash table */
1129 const unsigned int hash = wld_elf_hash(var);
1130 const Elf32_Word nbuckets = hashtab[0];
1131 const Elf32_Word *buckets = hashtab + 2;
1132 const Elf32_Word *chains = buckets + nbuckets;
1134 for (idx = buckets[hash % nbuckets]; idx; idx = chains[idx])
1136 if (ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
1137 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
1138 !wld_strcmp( strings + symtab[idx].st_name, var ))
1139 goto found;
1142 return NULL;
1144 found:
1145 #ifdef DUMP_SYMS
1146 wld_printf("Found %s -> %p\n", strings + symtab[idx].st_name, (void *)symtab[idx].st_value );
1147 #endif
1148 return (void *)(symtab[idx].st_value + map->l_addr);
1152 * preload_reserve
1154 * Reserve a range specified in string format
1156 static void preload_reserve( const char *str )
1158 const char *p;
1159 unsigned long result = 0;
1160 void *start = NULL, *end = NULL;
1161 int i, first = 1;
1163 for (p = str; *p; p++)
1165 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
1166 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
1167 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
1168 else if (*p == '-')
1170 if (!first) goto error;
1171 start = (void *)(result & ~page_mask);
1172 result = 0;
1173 first = 0;
1175 else goto error;
1177 if (!first) end = (void *)((result + page_mask) & ~page_mask);
1178 else if (result) goto error; /* single value '0' is allowed */
1180 /* sanity checks */
1181 if (end <= start) start = end = NULL;
1182 else if ((char *)end > preloader_start &&
1183 (char *)start <= preloader_end)
1185 wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %p-%p\n",
1186 start, end, preloader_start, preloader_end );
1187 start = end = NULL;
1190 /* check for overlap with low memory areas */
1191 for (i = 0; preload_info[i].size; i++)
1193 if ((char *)preload_info[i].addr > (char *)0x00110000) break;
1194 if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size)
1196 start = end = NULL;
1197 break;
1199 if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size)
1200 start = (char *)preload_info[i].addr + preload_info[i].size;
1203 while (preload_info[i].size) i++;
1204 preload_info[i].addr = start;
1205 preload_info[i].size = (char *)end - (char *)start;
1206 return;
1208 error:
1209 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
1212 /* check if address is in one of the reserved ranges */
1213 static int is_addr_reserved( const void *addr )
1215 int i;
1217 for (i = 0; preload_info[i].size; i++)
1219 if ((const char *)addr >= (const char *)preload_info[i].addr &&
1220 (const char *)addr < (const char *)preload_info[i].addr + preload_info[i].size)
1221 return 1;
1223 return 0;
1226 /* remove a range from the preload list */
1227 static void remove_preload_range( int i )
1229 while (preload_info[i].size)
1231 preload_info[i].addr = preload_info[i+1].addr;
1232 preload_info[i].size = preload_info[i+1].size;
1233 i++;
1238 * is_in_preload_range
1240 * Check if address of the given aux value is in one of the reserved ranges
1242 static int is_in_preload_range( const struct wld_auxv *av, int type )
1244 while (av->a_type != AT_NULL)
1246 if (av->a_type == type) return is_addr_reserved( (const void *)av->a_un.a_val );
1247 av++;
1249 return 0;
1252 /* set the process name if supported */
1253 static void set_process_name( int argc, char *argv[] )
1255 int i;
1256 unsigned int off;
1257 char *p, *name, *end;
1259 /* set the process short name */
1260 for (p = name = argv[1]; *p; p++) if (p[0] == '/' && p[1]) name = p + 1;
1261 if (wld_prctl( 15 /* PR_SET_NAME */, (long)name ) == -1) return;
1263 /* find the end of the argv array and move everything down */
1264 end = argv[argc - 1];
1265 while (*end) end++;
1266 off = argv[1] - argv[0];
1267 for (p = argv[1]; p <= end; p++) *(p - off) = *p;
1268 wld_memset( end - off, 0, off );
1269 for (i = 1; i < argc; i++) argv[i] -= off;
1274 * wld_start
1276 * Repeat the actions the kernel would do when loading a dynamically linked .so
1277 * Load the binary and then its ELF interpreter.
1278 * Note, we assume that the binary is a dynamically linked ELF shared object.
1280 void* wld_start( void **stack )
1282 long i, *pargc;
1283 char **argv, **p;
1284 char *interp, *reserve = NULL;
1285 struct wld_auxv new_av[12], delete_av[3], *av;
1286 struct wld_link_map main_binary_map, ld_so_map;
1287 struct wine_preload_info **wine_main_preload_info;
1289 pargc = *stack;
1290 argv = (char **)pargc + 1;
1291 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
1293 /* skip over the parameters */
1294 p = argv + *pargc + 1;
1296 /* skip over the environment */
1297 while (*p)
1299 static const char res[] = "WINEPRELOADRESERVE=";
1300 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
1301 p++;
1304 av = (struct wld_auxv *)(p+1);
1305 page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
1306 page_mask = page_size - 1;
1308 preloader_start = (char *)_start - ((unsigned long)_start & page_mask);
1309 preloader_end = (char *)((unsigned long)(_end + page_mask) & ~page_mask);
1311 #ifdef DUMP_AUX_INFO
1312 wld_printf( "stack = %p\n", *stack );
1313 for( i = 0; i < *pargc; i++ ) wld_printf("argv[%lx] = %s\n", i, argv[i]);
1314 dump_auxiliary( av );
1315 #endif
1317 /* reserve memory that Wine needs */
1318 if (reserve) preload_reserve( reserve );
1319 for (i = 0; preload_info[i].size; i++)
1321 if ((char *)av >= (char *)preload_info[i].addr &&
1322 (char *)pargc <= (char *)preload_info[i].addr + preload_info[i].size)
1324 remove_preload_range( i );
1325 i--;
1327 else if (wld_mmap( preload_info[i].addr, preload_info[i].size, PROT_NONE,
1328 MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 ) == (void *)-1)
1330 /* don't warn for low 64k */
1331 if (preload_info[i].addr >= (void *)0x10000
1332 #ifdef __aarch64__
1333 && preload_info[i].addr < (void *)0x7fffffffff /* ARM64 address space might end here*/
1334 #endif
1336 wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
1337 preload_info[i].addr, (char *)preload_info[i].addr + preload_info[i].size );
1338 remove_preload_range( i );
1339 i--;
1343 /* add an executable page at the top of the address space to defeat
1344 * broken no-exec protections that play with the code selector limit */
1345 if (is_addr_reserved( (char *)0x80000000 - page_size ))
1346 wld_mprotect( (char *)0x80000000 - page_size, page_size, PROT_EXEC | PROT_READ );
1348 /* load the main binary */
1349 map_so_lib( argv[1], &main_binary_map );
1351 /* load the ELF interpreter */
1352 interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
1353 map_so_lib( interp, &ld_so_map );
1355 /* store pointer to the preload info into the appropriate main binary variable */
1356 wine_main_preload_info = find_symbol( &main_binary_map, "wine_main_preload_info", STT_OBJECT );
1357 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
1358 else wld_printf( "wine_main_preload_info not found\n" );
1360 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
1361 SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
1362 SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
1363 SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
1364 SET_NEW_AV( 3, AT_PAGESZ, page_size );
1365 SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
1366 SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
1367 SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
1368 SET_NEW_AV( 7, AT_UID, get_auxiliary( av, AT_UID, wld_getuid() ) );
1369 SET_NEW_AV( 8, AT_EUID, get_auxiliary( av, AT_EUID, wld_geteuid() ) );
1370 SET_NEW_AV( 9, AT_GID, get_auxiliary( av, AT_GID, wld_getgid() ) );
1371 SET_NEW_AV(10, AT_EGID, get_auxiliary( av, AT_EGID, wld_getegid() ) );
1372 SET_NEW_AV(11, AT_NULL, 0 );
1373 #undef SET_NEW_AV
1375 i = 0;
1376 /* delete sysinfo values if addresses conflict */
1377 if (is_in_preload_range( av, AT_SYSINFO ) || is_in_preload_range( av, AT_SYSINFO_EHDR ))
1379 delete_av[i++].a_type = AT_SYSINFO;
1380 delete_av[i++].a_type = AT_SYSINFO_EHDR;
1382 delete_av[i].a_type = AT_NULL;
1384 /* get rid of first argument */
1385 set_process_name( *pargc, argv );
1386 pargc[1] = pargc[0] - 1;
1387 *stack = pargc + 1;
1389 set_auxiliary_values( av, new_av, delete_av, stack );
1391 #ifdef DUMP_AUX_INFO
1392 wld_printf("new stack = %p\n", *stack);
1393 wld_printf("jumping to %p\n", (void *)ld_so_map.l_entry);
1394 #endif
1395 #ifdef DUMP_MAPS
1397 char buffer[1024];
1398 int len, fd = wld_open( "/proc/self/maps", O_RDONLY );
1399 if (fd != -1)
1401 while ((len = wld_read( fd, buffer, sizeof(buffer) )) > 0) wld_write( 2, buffer, len );
1402 wld_close( fd );
1405 #endif
1407 return (void *)ld_so_map.l_entry;