wined3d: Respect the BO buffer offset in wined3d_context_gl_copy_bo_address().
[wine.git] / loader / preloader.c
blob7bd6afaeb66593e8360f18c0921b69a2751b68b8
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 #ifdef __linux__
65 #include "config.h"
67 #include <stdarg.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <sys/types.h>
72 #include <sys/stat.h>
73 #include <fcntl.h>
74 #ifdef HAVE_SYS_MMAN_H
75 # include <sys/mman.h>
76 #endif
77 #ifdef HAVE_SYS_SYSCALL_H
78 # include <sys/syscall.h>
79 #endif
80 #include <unistd.h>
81 #ifdef HAVE_ELF_H
82 # include <elf.h>
83 #endif
84 #ifdef HAVE_LINK_H
85 # include <link.h>
86 #endif
87 #ifdef HAVE_SYS_LINK_H
88 # include <sys/link.h>
89 #endif
91 #include "wine/asm.h"
92 #include "main.h"
94 /* ELF definitions */
95 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
96 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
98 #define MAP_BASE_ADDR(l) 0
100 #ifndef MAP_COPY
101 #define MAP_COPY MAP_PRIVATE
102 #endif
103 #ifndef MAP_NORESERVE
104 #define MAP_NORESERVE 0
105 #endif
107 static struct wine_preload_info preload_info[] =
109 #if defined(__i386__) || defined(__arm__)
110 { (void *)0x00000000, 0x00010000 }, /* low 64k */
111 { (void *)0x00010000, 0x00100000 }, /* DOS area */
112 { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
113 { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
114 #else
115 { (void *)0x000000010000, 0x00100000 }, /* DOS area */
116 { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
117 { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
118 { (void *)0x7ffffe000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
119 #endif
120 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
121 { 0, 0 } /* end of list */
124 /* debugging */
125 #undef DUMP_SEGMENTS
126 #undef DUMP_AUX_INFO
127 #undef DUMP_SYMS
128 #undef DUMP_MAPS
130 /* older systems may not define these */
131 #ifndef PT_TLS
132 #define PT_TLS 7
133 #endif
135 #ifndef AT_SYSINFO
136 #define AT_SYSINFO 32
137 #endif
138 #ifndef AT_SYSINFO_EHDR
139 #define AT_SYSINFO_EHDR 33
140 #endif
142 #ifndef DT_GNU_HASH
143 #define DT_GNU_HASH 0x6ffffef5
144 #endif
146 static size_t page_size, page_mask;
147 static char *preloader_start, *preloader_end;
149 struct wld_link_map {
150 ElfW(Addr) l_addr;
151 ElfW(Dyn) *l_ld;
152 ElfW(Phdr)*l_phdr;
153 ElfW(Addr) l_entry;
154 ElfW(Half) l_ldnum;
155 ElfW(Half) l_phnum;
156 ElfW(Addr) l_map_start, l_map_end;
157 ElfW(Addr) l_interp;
160 struct wld_auxv
162 ElfW(Addr) a_type;
163 union
165 ElfW(Addr) a_val;
166 } a_un;
170 * The __bb_init_func is an empty function only called when file is
171 * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
172 * function is normally provided by libc's startup files, but since we
173 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
174 * provide our own (empty) version, otherwise linker fails.
176 void __bb_init_func(void) { return; }
178 /* similar to the above but for -fstack-protector */
179 void *__stack_chk_guard = 0;
180 void __stack_chk_fail_local(void) { return; }
181 void __stack_chk_fail(void) { return; }
183 #ifdef __i386__
185 /* data for setting up the glibc-style thread-local storage in %gs */
187 static int thread_data[256];
189 struct
191 /* this is the kernel modify_ldt struct */
192 unsigned int entry_number;
193 unsigned long base_addr;
194 unsigned int limit;
195 unsigned int seg_32bit : 1;
196 unsigned int contents : 2;
197 unsigned int read_exec_only : 1;
198 unsigned int limit_in_pages : 1;
199 unsigned int seg_not_present : 1;
200 unsigned int usable : 1;
201 unsigned int garbage : 25;
202 } thread_ldt = { -1, (unsigned long)thread_data, 0xfffff, 1, 0, 0, 1, 0, 1, 0 };
206 * The _start function is the entry and exit point of this program
208 * It calls wld_start, passing a pointer to the args it receives
209 * then jumps to the address wld_start returns.
211 void _start(void);
212 extern char _end[];
213 __ASM_GLOBAL_FUNC(_start,
214 __ASM_CFI("\t.cfi_undefined %eip\n")
215 "\tmovl $243,%eax\n" /* SYS_set_thread_area */
216 "\tmovl $thread_ldt,%ebx\n"
217 "\tint $0x80\n" /* allocate gs segment */
218 "\torl %eax,%eax\n"
219 "\tjl 1f\n"
220 "\tmovl thread_ldt,%eax\n" /* thread_ldt.entry_number */
221 "\tshl $3,%eax\n"
222 "\torl $3,%eax\n"
223 "\tmov %ax,%gs\n"
224 "\tmov %ax,%fs\n" /* set %fs too so libwine can retrieve it later on */
225 "1:\tmovl %esp,%eax\n"
226 "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */
227 "\tpushl %eax\n" /* orig stack pointer */
228 "\tpushl %esp\n" /* ptr to orig stack pointer */
229 "\tcall wld_start\n"
230 "\tpopl %ecx\n" /* remove ptr to stack pointer */
231 "\tpopl %esp\n" /* new stack pointer */
232 "\tpush %eax\n" /* ELF interpreter entry point */
233 "\txor %eax,%eax\n"
234 "\txor %ecx,%ecx\n"
235 "\txor %edx,%edx\n"
236 "\tmov %ax,%gs\n" /* clear %gs again */
237 "\tret\n")
239 /* wrappers for Linux system calls */
241 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
243 static inline __attribute__((noreturn)) void wld_exit( int code )
245 for (;;) /* avoid warning */
246 __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
247 : : "a" (1 /* SYS_exit */), "r" (code) );
250 static inline int wld_open( const char *name, int flags )
252 int ret;
253 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
254 : "=a" (ret) : "0" (5 /* SYS_open */), "r" (name), "c" (flags) );
255 return SYSCALL_RET(ret);
258 static inline int wld_close( int fd )
260 int ret;
261 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
262 : "=a" (ret) : "0" (6 /* SYS_close */), "r" (fd) );
263 return SYSCALL_RET(ret);
266 static inline ssize_t wld_read( int fd, void *buffer, size_t len )
268 int ret;
269 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
270 : "=a" (ret)
271 : "0" (3 /* SYS_read */), "r" (fd), "c" (buffer), "d" (len)
272 : "memory" );
273 return SYSCALL_RET(ret);
276 static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
278 int ret;
279 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
280 : "=a" (ret) : "0" (4 /* SYS_write */), "r" (fd), "c" (buffer), "d" (len) );
281 return SYSCALL_RET(ret);
284 static inline int wld_mprotect( const void *addr, size_t len, int prot )
286 int ret;
287 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
288 : "=a" (ret) : "0" (125 /* SYS_mprotect */), "r" (addr), "c" (len), "d" (prot) );
289 return SYSCALL_RET(ret);
292 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, unsigned int offset );
293 __ASM_GLOBAL_FUNC(wld_mmap,
294 "\tpushl %ebp\n"
295 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
296 "\tpushl %ebx\n"
297 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
298 "\tpushl %esi\n"
299 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
300 "\tpushl %edi\n"
301 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
302 "\tmovl $192,%eax\n" /* SYS_mmap2 */
303 "\tmovl 20(%esp),%ebx\n" /* start */
304 "\tmovl 24(%esp),%ecx\n" /* len */
305 "\tmovl 28(%esp),%edx\n" /* prot */
306 "\tmovl 32(%esp),%esi\n" /* flags */
307 "\tmovl 36(%esp),%edi\n" /* fd */
308 "\tmovl 40(%esp),%ebp\n" /* offset */
309 "\tshrl $12,%ebp\n"
310 "\tint $0x80\n"
311 "\tcmpl $-4096,%eax\n"
312 "\tjbe 2f\n"
313 "\tcmpl $-38,%eax\n" /* ENOSYS */
314 "\tjne 1f\n"
315 "\tmovl $90,%eax\n" /* SYS_mmap */
316 "\tleal 20(%esp),%ebx\n"
317 "\tint $0x80\n"
318 "\tcmpl $-4096,%eax\n"
319 "\tjbe 2f\n"
320 "1:\tmovl $-1,%eax\n"
321 "2:\tpopl %edi\n"
322 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
323 "\tpopl %esi\n"
324 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
325 "\tpopl %ebx\n"
326 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
327 "\tpopl %ebp\n"
328 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
329 "\tret\n" )
331 static inline int wld_prctl( int code, long arg )
333 int ret;
334 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
335 : "=a" (ret) : "0" (172 /* SYS_prctl */), "r" (code), "c" (arg) );
336 return SYSCALL_RET(ret);
339 #elif defined(__x86_64__)
341 void *thread_data[256];
344 * The _start function is the entry and exit point of this program
346 * It calls wld_start, passing a pointer to the args it receives
347 * then jumps to the address wld_start returns.
349 void _start(void);
350 extern char _end[];
351 __ASM_GLOBAL_FUNC(_start,
352 __ASM_CFI(".cfi_undefined %rip\n\t")
353 "movq %rsp,%rax\n\t"
354 "leaq -144(%rsp),%rsp\n\t" /* allocate some space for extra aux values */
355 "movq %rax,(%rsp)\n\t" /* orig stack pointer */
356 "movq $thread_data,%rsi\n\t"
357 "movq $0x1002,%rdi\n\t" /* ARCH_SET_FS */
358 "movq $158,%rax\n\t" /* SYS_arch_prctl */
359 "syscall\n\t"
360 "movq %rsp,%rdi\n\t" /* ptr to orig stack pointer */
361 "call wld_start\n\t"
362 "movq (%rsp),%rsp\n\t" /* new stack pointer */
363 "pushq %rax\n\t" /* ELF interpreter entry point */
364 "xorq %rax,%rax\n\t"
365 "xorq %rcx,%rcx\n\t"
366 "xorq %rdx,%rdx\n\t"
367 "xorq %rsi,%rsi\n\t"
368 "xorq %rdi,%rdi\n\t"
369 "xorq %r8,%r8\n\t"
370 "xorq %r9,%r9\n\t"
371 "xorq %r10,%r10\n\t"
372 "xorq %r11,%r11\n\t"
373 "ret")
375 #define SYSCALL_FUNC( name, nr ) \
376 __ASM_GLOBAL_FUNC( name, \
377 "movq $" #nr ",%rax\n\t" \
378 "movq %rcx,%r10\n\t" \
379 "syscall\n\t" \
380 "leaq 4096(%rax),%rcx\n\t" \
381 "movq $-1,%rdx\n\t" \
382 "cmp $4096,%rcx\n\t" \
383 "cmovb %rdx,%rax\n\t" \
384 "ret" )
386 #define SYSCALL_NOERR( name, nr ) \
387 __ASM_GLOBAL_FUNC( name, \
388 "movq $" #nr ",%rax\n\t" \
389 "syscall\n\t" \
390 "ret" )
392 void wld_exit( int code ) __attribute__((noreturn));
393 SYSCALL_NOERR( wld_exit, 60 /* SYS_exit */ );
395 ssize_t wld_read( int fd, void *buffer, size_t len );
396 SYSCALL_FUNC( wld_read, 0 /* SYS_read */ );
398 ssize_t wld_write( int fd, const void *buffer, size_t len );
399 SYSCALL_FUNC( wld_write, 1 /* SYS_write */ );
401 int wld_open( const char *name, int flags );
402 SYSCALL_FUNC( wld_open, 2 /* SYS_open */ );
404 int wld_close( int fd );
405 SYSCALL_FUNC( wld_close, 3 /* SYS_close */ );
407 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
408 SYSCALL_FUNC( wld_mmap, 9 /* SYS_mmap */ );
410 int wld_mprotect( const void *addr, size_t len, int prot );
411 SYSCALL_FUNC( wld_mprotect, 10 /* SYS_mprotect */ );
413 int wld_prctl( int code, long arg );
414 SYSCALL_FUNC( wld_prctl, 157 /* SYS_prctl */ );
416 uid_t wld_getuid(void);
417 SYSCALL_NOERR( wld_getuid, 102 /* SYS_getuid */ );
419 gid_t wld_getgid(void);
420 SYSCALL_NOERR( wld_getgid, 104 /* SYS_getgid */ );
422 uid_t wld_geteuid(void);
423 SYSCALL_NOERR( wld_geteuid, 107 /* SYS_geteuid */ );
425 gid_t wld_getegid(void);
426 SYSCALL_NOERR( wld_getegid, 108 /* SYS_getegid */ );
428 #elif defined(__aarch64__)
430 void *thread_data[256];
433 * The _start function is the entry and exit point of this program
435 * It calls wld_start, passing a pointer to the args it receives
436 * then jumps to the address wld_start returns.
438 void _start(void);
439 extern char _end[];
440 __ASM_GLOBAL_FUNC(_start,
441 "mov x0, SP\n\t"
442 "sub SP, SP, #144\n\t" /* allocate some space for extra aux values */
443 "str x0, [SP]\n\t" /* orig stack pointer */
444 "ldr x0, =thread_data\n\t"
445 "msr tpidr_el0, x0\n\t"
446 "mov x0, SP\n\t" /* ptr to orig stack pointer */
447 "bl wld_start\n\t"
448 "ldr x1, [SP]\n\t" /* new stack pointer */
449 "mov SP, x1\n\t"
450 "mov x30, x0\n\t"
451 "mov x0, #0\n\t"
452 "mov x1, #0\n\t"
453 "mov x2, #0\n\t"
454 "mov x3, #0\n\t"
455 "mov x4, #0\n\t"
456 "mov x5, #0\n\t"
457 "mov x6, #0\n\t"
458 "mov x7, #0\n\t"
459 "mov x8, #0\n\t"
460 "mov x9, #0\n\t"
461 "mov x10, #0\n\t"
462 "mov x11, #0\n\t"
463 "mov x12, #0\n\t"
464 "mov x13, #0\n\t"
465 "mov x14, #0\n\t"
466 "mov x15, #0\n\t"
467 "mov x16, #0\n\t"
468 "mov x17, #0\n\t"
469 "mov x18, #0\n\t"
470 "ret")
472 #define SYSCALL_FUNC( name, nr ) \
473 __ASM_GLOBAL_FUNC( name, \
474 "stp x8, x9, [SP, #-16]!\n\t" \
475 "mov x8, #" #nr "\n\t" \
476 "svc #0\n\t" \
477 "ldp x8, x9, [SP], #16\n\t" \
478 "cmn x0, #1, lsl#12\n\t" \
479 "cinv x0, x0, hi\n\t" \
480 "b.hi 1f\n\t" \
481 "ret\n\t" \
482 "1: mov x0, #-1\n\t" \
483 "ret" )
485 #define SYSCALL_NOERR( name, nr ) \
486 __ASM_GLOBAL_FUNC( name, \
487 "stp x8, x9, [SP, #-16]!\n\t" \
488 "mov x8, #" #nr "\n\t" \
489 "svc #0\n\t" \
490 "ldp x8, x9, [SP], #16\n\t" \
491 "ret" )
493 void wld_exit( int code ) __attribute__((noreturn));
494 SYSCALL_NOERR( wld_exit, 93 /* SYS_exit */ );
496 ssize_t wld_read( int fd, void *buffer, size_t len );
497 SYSCALL_FUNC( wld_read, 63 /* SYS_read */ );
499 ssize_t wld_write( int fd, const void *buffer, size_t len );
500 SYSCALL_FUNC( wld_write, 64 /* SYS_write */ );
502 int wld_openat( int dirfd, const char *name, int flags );
503 SYSCALL_FUNC( wld_openat, 56 /* SYS_openat */ );
505 int wld_open( const char *name, int flags )
507 return wld_openat(-100 /* AT_FDCWD */, name, flags);
510 int wld_close( int fd );
511 SYSCALL_FUNC( wld_close, 57 /* SYS_close */ );
513 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
514 SYSCALL_FUNC( wld_mmap, 222 /* SYS_mmap */ );
516 int wld_mprotect( const void *addr, size_t len, int prot );
517 SYSCALL_FUNC( wld_mprotect, 226 /* SYS_mprotect */ );
519 int wld_prctl( int code, long arg );
520 SYSCALL_FUNC( wld_prctl, 167 /* SYS_prctl */ );
522 uid_t wld_getuid(void);
523 SYSCALL_NOERR( wld_getuid, 174 /* SYS_getuid */ );
525 gid_t wld_getgid(void);
526 SYSCALL_NOERR( wld_getgid, 176 /* SYS_getgid */ );
528 uid_t wld_geteuid(void);
529 SYSCALL_NOERR( wld_geteuid, 175 /* SYS_geteuid */ );
531 gid_t wld_getegid(void);
532 SYSCALL_NOERR( wld_getegid, 177 /* SYS_getegid */ );
534 #elif defined(__arm__)
536 void *thread_data[256];
539 * The _start function is the entry and exit point of this program
541 * It calls wld_start, passing a pointer to the args it receives
542 * then jumps to the address wld_start returns.
544 void _start(void);
545 extern char _end[];
546 __ASM_GLOBAL_FUNC(_start,
547 "mov r0, sp\n\t"
548 "sub sp, sp, #144\n\t" /* allocate some space for extra aux values */
549 "str r0, [sp]\n\t" /* orig stack pointer */
550 "ldr r0, =thread_data\n\t"
551 "movw r7, #0x0005\n\t" /* __ARM_NR_set_tls */
552 "movt r7, #0xf\n\t" /* __ARM_NR_set_tls */
553 "svc #0\n\t"
554 "mov r0, sp\n\t" /* ptr to orig stack pointer */
555 "bl wld_start\n\t"
556 "ldr r1, [sp]\n\t" /* new stack pointer */
557 "mov sp, r1\n\t"
558 "mov lr, r0\n\t"
559 "mov r0, #0\n\t"
560 "mov r1, #0\n\t"
561 "mov r2, #0\n\t"
562 "mov r3, #0\n\t"
563 "mov r12, #0\n\t"
564 "bx lr\n\t"
565 ".ltorg\n\t")
567 #define SYSCALL_FUNC( name, nr ) \
568 __ASM_GLOBAL_FUNC( name, \
569 "push {r4-r5,r7,lr}\n\t" \
570 "ldr r4, [sp, #16]\n\t" \
571 "ldr r5, [sp, #20]\n\t" \
572 "mov r7, #" #nr "\n\t" \
573 "svc #0\n\t" \
574 "cmn r0, #4096\n\t" \
575 "it hi\n\t" \
576 "movhi r0, #-1\n\t" \
577 "pop {r4-r5,r7,pc}\n\t" )
579 #define SYSCALL_NOERR( name, nr ) \
580 __ASM_GLOBAL_FUNC( name, \
581 "push {r7,lr}\n\t" \
582 "mov r7, #" #nr "\n\t" \
583 "svc #0\n\t" \
584 "pop {r7,pc}\n\t" )
586 void wld_exit( int code ) __attribute__((noreturn));
587 SYSCALL_NOERR( wld_exit, 1 /* SYS_exit */ );
589 ssize_t wld_read( int fd, void *buffer, size_t len );
590 SYSCALL_FUNC( wld_read, 3 /* SYS_read */ );
592 ssize_t wld_write( int fd, const void *buffer, size_t len );
593 SYSCALL_FUNC( wld_write, 4 /* SYS_write */ );
595 int wld_openat( int dirfd, const char *name, int flags );
596 SYSCALL_FUNC( wld_openat, 322 /* SYS_openat */ );
598 int wld_open( const char *name, int flags )
600 return wld_openat(-100 /* AT_FDCWD */, name, flags);
603 int wld_close( int fd );
604 SYSCALL_FUNC( wld_close, 6 /* SYS_close */ );
606 void *wld_mmap2( void *start, size_t len, int prot, int flags, int fd, int offset );
607 SYSCALL_FUNC( wld_mmap2, 192 /* SYS_mmap2 */ );
609 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset )
611 return wld_mmap2(start, len, prot, flags, fd, offset >> 12);
614 int wld_mprotect( const void *addr, size_t len, int prot );
615 SYSCALL_FUNC( wld_mprotect, 125 /* SYS_mprotect */ );
617 int wld_prctl( int code, long arg );
618 SYSCALL_FUNC( wld_prctl, 172 /* SYS_prctl */ );
620 uid_t wld_getuid(void);
621 SYSCALL_NOERR( wld_getuid, 24 /* SYS_getuid */ );
623 gid_t wld_getgid(void);
624 SYSCALL_NOERR( wld_getgid, 47 /* SYS_getgid */ );
626 uid_t wld_geteuid(void);
627 SYSCALL_NOERR( wld_geteuid, 49 /* SYS_geteuid */ );
629 gid_t wld_getegid(void);
630 SYSCALL_NOERR( wld_getegid, 50 /* SYS_getegid */ );
632 unsigned long long __aeabi_uidivmod(unsigned int num, unsigned int den)
634 unsigned int bit = 1;
635 unsigned int quota = 0;
636 if (!den)
637 wld_exit(1);
638 while (den < num && !(den & 0x80000000)) {
639 den <<= 1;
640 bit <<= 1;
642 do {
643 if (den <= num) {
644 quota |= bit;
645 num -= den;
647 bit >>= 1;
648 den >>= 1;
649 } while (bit);
650 return ((unsigned long long)num << 32) | quota;
653 #else
654 #error preloader not implemented for this CPU
655 #endif
657 /* replacement for libc functions */
659 static int wld_strcmp( const char *str1, const char *str2 )
661 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
662 return *str1 - *str2;
665 static int wld_strncmp( const char *str1, const char *str2, size_t len )
667 if (len <= 0) return 0;
668 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
669 return *str1 - *str2;
672 static inline void *wld_memset( void *dest, int val, size_t len )
674 char *dst = dest;
675 while (len--) *dst++ = val;
676 return dest;
680 * wld_printf - just the basics
682 * %x prints a hex number
683 * %s prints a string
684 * %p prints a pointer
686 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
688 static const char hex_chars[16] = "0123456789abcdef";
689 const char *p = fmt;
690 char *str = buffer;
691 int i;
693 while( *p )
695 if( *p == '%' )
697 p++;
698 if( *p == 'x' )
700 unsigned int x = va_arg( args, unsigned int );
701 for (i = 2*sizeof(x) - 1; i >= 0; i--)
702 *str++ = hex_chars[(x>>(i*4))&0xf];
704 else if (p[0] == 'l' && p[1] == 'x')
706 unsigned long x = va_arg( args, unsigned long );
707 for (i = 2*sizeof(x) - 1; i >= 0; i--)
708 *str++ = hex_chars[(x>>(i*4))&0xf];
709 p++;
711 else if( *p == 'p' )
713 unsigned long x = (unsigned long)va_arg( args, void * );
714 for (i = 2*sizeof(x) - 1; i >= 0; i--)
715 *str++ = hex_chars[(x>>(i*4))&0xf];
717 else if( *p == 's' )
719 char *s = va_arg( args, char * );
720 while(*s)
721 *str++ = *s++;
723 else if( *p == 0 )
724 break;
725 p++;
727 *str++ = *p++;
729 *str = 0;
730 return str - buffer;
733 static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
735 va_list args;
736 char buffer[256];
737 int len;
739 va_start( args, fmt );
740 len = wld_vsprintf(buffer, fmt, args );
741 va_end( args );
742 wld_write(2, buffer, len);
745 static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
747 va_list args;
748 char buffer[256];
749 int len;
751 va_start( args, fmt );
752 len = wld_vsprintf(buffer, fmt, args );
753 va_end( args );
754 wld_write(2, buffer, len);
755 wld_exit(1);
758 #ifdef DUMP_AUX_INFO
760 * Dump interesting bits of the ELF auxv_t structure that is passed
761 * as the 4th parameter to the _start function
763 static void dump_auxiliary( struct wld_auxv *av )
765 #define NAME(at) { at, #at }
766 static const struct { int val; const char *name; } names[] =
768 NAME(AT_BASE),
769 NAME(AT_CLKTCK),
770 NAME(AT_EGID),
771 NAME(AT_ENTRY),
772 NAME(AT_EUID),
773 NAME(AT_FLAGS),
774 NAME(AT_GID),
775 NAME(AT_HWCAP),
776 NAME(AT_PAGESZ),
777 NAME(AT_PHDR),
778 NAME(AT_PHENT),
779 NAME(AT_PHNUM),
780 NAME(AT_PLATFORM),
781 NAME(AT_SYSINFO),
782 NAME(AT_SYSINFO_EHDR),
783 NAME(AT_UID),
784 { 0, NULL }
786 #undef NAME
788 int i;
790 for ( ; av->a_type != AT_NULL; av++)
792 for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
793 if (names[i].name) wld_printf("%s = %lx\n", names[i].name, (unsigned long)av->a_un.a_val);
794 else wld_printf( "%lx = %lx\n", (unsigned long)av->a_type, (unsigned long)av->a_un.a_val );
797 #endif
800 * set_auxiliary_values
802 * Set the new auxiliary values
804 static void set_auxiliary_values( struct wld_auxv *av, const struct wld_auxv *new_av,
805 const struct wld_auxv *delete_av, void **stack )
807 int i, j, av_count = 0, new_count = 0, delete_count = 0;
808 char *src, *dst;
810 /* count how many aux values we have already */
811 while (av[av_count].a_type != AT_NULL) av_count++;
813 /* delete unwanted values */
814 for (j = 0; delete_av[j].a_type != AT_NULL; j++)
816 for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
818 av[i].a_type = av[av_count-1].a_type;
819 av[i].a_un.a_val = av[av_count-1].a_un.a_val;
820 av[--av_count].a_type = AT_NULL;
821 delete_count++;
822 break;
826 /* count how many values we have in new_av that aren't in av */
827 for (j = 0; new_av[j].a_type != AT_NULL; j++)
829 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
830 if (i == av_count) new_count++;
833 src = (char *)*stack;
834 dst = src - (new_count - delete_count) * sizeof(*av);
835 dst = (char *)((unsigned long)dst & ~15);
836 if (dst < src) /* need to make room for the extra values */
838 int len = (char *)(av + av_count + 1) - src;
839 for (i = 0; i < len; i++) dst[i] = src[i];
841 else if (dst > src) /* get rid of unused values */
843 int len = (char *)(av + av_count + 1) - src;
844 for (i = len - 1; i >= 0; i--) dst[i] = src[i];
846 *stack = dst;
847 av = (struct wld_auxv *)((char *)av + (dst - src));
849 /* now set the values */
850 for (j = 0; new_av[j].a_type != AT_NULL; j++)
852 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
853 if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
854 else
856 av[av_count].a_type = new_av[j].a_type;
857 av[av_count].a_un.a_val = new_av[j].a_un.a_val;
858 av_count++;
862 #ifdef DUMP_AUX_INFO
863 wld_printf("New auxiliary info:\n");
864 dump_auxiliary( av );
865 #endif
869 * get_auxiliary
871 * Get a field of the auxiliary structure
873 static int get_auxiliary( struct wld_auxv *av, int type, int def_val )
875 for ( ; av->a_type != AT_NULL; av++)
876 if( av->a_type == type ) return av->a_un.a_val;
877 return def_val;
881 * map_so_lib
883 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
885 * This function maps the segments from an ELF object, and optionally
886 * stores information about the mapping into the auxv_t structure.
888 static void map_so_lib( const char *name, struct wld_link_map *l)
890 int fd;
891 unsigned char buf[0x800];
892 ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
893 ElfW(Phdr) *phdr, *ph;
894 /* Scan the program header table, collecting its load commands. */
895 struct loadcmd
897 ElfW(Addr) mapstart, mapend, dataend, allocend;
898 off_t mapoff;
899 int prot;
900 } loadcmds[16], *c;
901 size_t nloadcmds = 0, maplength;
903 fd = wld_open( name, O_RDONLY );
904 if (fd == -1) fatal_error("%s: could not open\n", name );
906 if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
907 fatal_error("%s: failed to read ELF header\n", name);
909 phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
911 if( ( header->e_ident[0] != 0x7f ) ||
912 ( header->e_ident[1] != 'E' ) ||
913 ( header->e_ident[2] != 'L' ) ||
914 ( header->e_ident[3] != 'F' ) )
915 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
917 #ifdef __i386__
918 if( header->e_machine != EM_386 )
919 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
920 #elif defined(__x86_64__)
921 if( header->e_machine != EM_X86_64 )
922 fatal_error("%s: not an x86-64 ELF binary... don't know how to load it\n", name );
923 #elif defined(__aarch64__)
924 if( header->e_machine != EM_AARCH64 )
925 fatal_error("%s: not an aarch64 ELF binary... don't know how to load it\n", name );
926 #elif defined(__arm__)
927 if( header->e_machine != EM_ARM )
928 fatal_error("%s: not an arm ELF binary... don't know how to load it\n", name );
929 #endif
931 if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
932 fatal_error( "%s: oops... not enough space for load commands\n", name );
934 maplength = header->e_phnum * sizeof (ElfW(Phdr));
935 if (header->e_phoff + maplength > sizeof(buf))
936 fatal_error( "%s: oops... not enough space for ELF headers\n", name );
938 l->l_ld = 0;
939 l->l_addr = 0;
940 l->l_phdr = 0;
941 l->l_phnum = header->e_phnum;
942 l->l_entry = header->e_entry;
943 l->l_interp = 0;
945 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
948 #ifdef DUMP_SEGMENTS
949 wld_printf( "ph = %p\n", ph );
950 wld_printf( " p_type = %lx\n", (unsigned long)ph->p_type );
951 wld_printf( " p_flags = %lx\n", (unsigned long)ph->p_flags );
952 wld_printf( " p_offset = %lx\n", (unsigned long)ph->p_offset );
953 wld_printf( " p_vaddr = %lx\n", (unsigned long)ph->p_vaddr );
954 wld_printf( " p_paddr = %lx\n", (unsigned long)ph->p_paddr );
955 wld_printf( " p_filesz = %lx\n", (unsigned long)ph->p_filesz );
956 wld_printf( " p_memsz = %lx\n", (unsigned long)ph->p_memsz );
957 wld_printf( " p_align = %lx\n", (unsigned long)ph->p_align );
958 #endif
960 switch (ph->p_type)
962 /* These entries tell us where to find things once the file's
963 segments are mapped in. We record the addresses it says
964 verbatim, and later correct for the run-time load address. */
965 case PT_DYNAMIC:
966 l->l_ld = (void *) ph->p_vaddr;
967 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
968 break;
970 case PT_PHDR:
971 l->l_phdr = (void *) ph->p_vaddr;
972 break;
974 case PT_LOAD:
976 if ((ph->p_align & page_mask) != 0)
977 fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
979 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
980 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
982 c = &loadcmds[nloadcmds++];
983 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
984 c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
985 c->dataend = ph->p_vaddr + ph->p_filesz;
986 c->allocend = ph->p_vaddr + ph->p_memsz;
987 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
989 c->prot = 0;
990 if (ph->p_flags & PF_R)
991 c->prot |= PROT_READ;
992 if (ph->p_flags & PF_W)
993 c->prot |= PROT_WRITE;
994 if (ph->p_flags & PF_X)
995 c->prot |= PROT_EXEC;
997 break;
999 case PT_INTERP:
1000 l->l_interp = ph->p_vaddr;
1001 break;
1003 case PT_TLS:
1005 * We don't need to set anything up because we're
1006 * emulating the kernel, not ld-linux.so.2
1007 * The ELF loader will set up the TLS data itself.
1009 case PT_SHLIB:
1010 case PT_NOTE:
1011 default:
1012 break;
1016 /* Now process the load commands and map segments into memory. */
1017 if (!nloadcmds)
1018 fatal_error( "%s: no segments to load\n", name );
1019 c = loadcmds;
1021 /* Length of the sections to be loaded. */
1022 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
1024 if( header->e_type == ET_DYN )
1026 ElfW(Addr) mappref;
1027 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
1028 - MAP_BASE_ADDR (l));
1030 /* Remember which part of the address space this object uses. */
1031 l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
1032 c->prot, MAP_COPY | MAP_FILE,
1033 fd, c->mapoff);
1034 /* wld_printf("set : offset = %x\n", c->mapoff); */
1035 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
1037 l->l_map_end = l->l_map_start + maplength;
1038 l->l_addr = l->l_map_start - c->mapstart;
1040 wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
1041 loadcmds[nloadcmds - 1].allocend - c->mapend,
1042 PROT_NONE);
1043 goto postmap;
1045 else
1047 /* sanity check */
1048 if ((char *)c->mapstart + maplength > preloader_start &&
1049 (char *)c->mapstart <= preloader_end)
1050 fatal_error( "%s: binary overlaps preloader (%p-%p)\n",
1051 name, (char *)c->mapstart, (char *)c->mapstart + maplength );
1053 ELF_FIXED_ADDRESS (loader, c->mapstart);
1056 /* Remember which part of the address space this object uses. */
1057 l->l_map_start = c->mapstart + l->l_addr;
1058 l->l_map_end = l->l_map_start + maplength;
1060 while (c < &loadcmds[nloadcmds])
1062 if (c->mapend > c->mapstart)
1063 /* Map the segment contents from the file. */
1064 wld_mmap ((void *) (l->l_addr + c->mapstart),
1065 c->mapend - c->mapstart, c->prot,
1066 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
1068 postmap:
1069 if (l->l_phdr == 0
1070 && (ElfW(Off)) c->mapoff <= header->e_phoff
1071 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
1072 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
1073 /* Found the program header in this segment. */
1074 l->l_phdr = (void *)(unsigned long)(c->mapstart + header->e_phoff - c->mapoff);
1076 if (c->allocend > c->dataend)
1078 /* Extra zero pages should appear at the end of this segment,
1079 after the data mapped from the file. */
1080 ElfW(Addr) zero, zeroend, zeropage;
1082 zero = l->l_addr + c->dataend;
1083 zeroend = l->l_addr + c->allocend;
1084 zeropage = (zero + page_mask) & ~page_mask;
1087 * This is different from the dl-load load...
1088 * ld-linux.so.2 relies on the whole page being zero'ed
1090 zeroend = (zeroend + page_mask) & ~page_mask;
1092 if (zeroend < zeropage)
1094 /* All the extra data is in the last page of the segment.
1095 We can just zero it. */
1096 zeropage = zeroend;
1099 if (zeropage > zero)
1101 /* Zero the final part of the last page of the segment. */
1102 if ((c->prot & PROT_WRITE) == 0)
1104 /* Dag nab it. */
1105 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
1107 wld_memset ((void *) zero, '\0', zeropage - zero);
1108 if ((c->prot & PROT_WRITE) == 0)
1109 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
1112 if (zeroend > zeropage)
1114 /* Map the remaining zero pages in from the zero fill FD. */
1115 wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
1116 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1117 -1, 0);
1121 ++c;
1124 if (l->l_phdr == NULL) fatal_error("no program header\n");
1126 l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
1127 l->l_entry += l->l_addr;
1129 wld_close( fd );
1133 static unsigned int wld_elf_hash( const char *name )
1135 unsigned int hi, hash = 0;
1136 while (*name)
1138 hash = (hash << 4) + (unsigned char)*name++;
1139 hi = hash & 0xf0000000;
1140 hash ^= hi;
1141 hash ^= hi >> 24;
1143 return hash;
1146 static unsigned int gnu_hash( const char *name )
1148 unsigned int h = 5381;
1149 while (*name) h = h * 33 + (unsigned char)*name++;
1150 return h;
1154 * Find a symbol in the symbol table of the executable loaded
1156 static void *find_symbol( const struct wld_link_map *map, const char *var, int type )
1158 const ElfW(Dyn) *dyn = NULL;
1159 const ElfW(Phdr) *ph;
1160 const ElfW(Sym) *symtab = NULL;
1161 const Elf32_Word *hashtab = NULL;
1162 const Elf32_Word *gnu_hashtab = NULL;
1163 const char *strings = NULL;
1164 Elf32_Word idx;
1166 /* check the values */
1167 #ifdef DUMP_SYMS
1168 wld_printf("%p %x\n", map->l_phdr, map->l_phnum );
1169 #endif
1170 /* parse the (already loaded) ELF executable's header */
1171 for (ph = map->l_phdr; ph < &map->l_phdr[map->l_phnum]; ++ph)
1173 if( PT_DYNAMIC == ph->p_type )
1175 dyn = (void *)(ph->p_vaddr + map->l_addr);
1176 break;
1179 if( !dyn ) return NULL;
1181 while( dyn->d_tag )
1183 if( dyn->d_tag == DT_STRTAB )
1184 strings = (const char*)(dyn->d_un.d_ptr + map->l_addr);
1185 if( dyn->d_tag == DT_SYMTAB )
1186 symtab = (const ElfW(Sym) *)(dyn->d_un.d_ptr + map->l_addr);
1187 if( dyn->d_tag == DT_HASH )
1188 hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
1189 if( dyn->d_tag == DT_GNU_HASH )
1190 gnu_hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
1191 #ifdef DUMP_SYMS
1192 wld_printf("%lx %p\n", (unsigned long)dyn->d_tag, (void *)dyn->d_un.d_ptr );
1193 #endif
1194 dyn++;
1197 if( (!symtab) || (!strings) ) return NULL;
1199 if (gnu_hashtab) /* new style hash table */
1201 const unsigned int hash = gnu_hash(var);
1202 const Elf32_Word nbuckets = gnu_hashtab[0];
1203 const Elf32_Word symbias = gnu_hashtab[1];
1204 const Elf32_Word nwords = gnu_hashtab[2];
1205 const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
1206 const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
1207 const Elf32_Word *chains = buckets + nbuckets - symbias;
1209 if (!(idx = buckets[hash % nbuckets])) return NULL;
1212 if ((chains[idx] & ~1u) == (hash & ~1u) &&
1213 ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
1214 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
1215 !wld_strcmp( strings + symtab[idx].st_name, var ))
1216 goto found;
1217 } while (!(chains[idx++] & 1u));
1219 else if (hashtab) /* old style hash table */
1221 const unsigned int hash = wld_elf_hash(var);
1222 const Elf32_Word nbuckets = hashtab[0];
1223 const Elf32_Word *buckets = hashtab + 2;
1224 const Elf32_Word *chains = buckets + nbuckets;
1226 for (idx = buckets[hash % nbuckets]; idx; idx = chains[idx])
1228 if (ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
1229 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
1230 !wld_strcmp( strings + symtab[idx].st_name, var ))
1231 goto found;
1234 return NULL;
1236 found:
1237 #ifdef DUMP_SYMS
1238 wld_printf("Found %s -> %p\n", strings + symtab[idx].st_name, (void *)symtab[idx].st_value );
1239 #endif
1240 return (void *)(symtab[idx].st_value + map->l_addr);
1244 * preload_reserve
1246 * Reserve a range specified in string format
1248 static void preload_reserve( const char *str )
1250 const char *p;
1251 unsigned long result = 0;
1252 void *start = NULL, *end = NULL;
1253 int i, first = 1;
1255 for (p = str; *p; p++)
1257 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
1258 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
1259 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
1260 else if (*p == '-')
1262 if (!first) goto error;
1263 start = (void *)(result & ~page_mask);
1264 result = 0;
1265 first = 0;
1267 else goto error;
1269 if (!first) end = (void *)((result + page_mask) & ~page_mask);
1270 else if (result) goto error; /* single value '0' is allowed */
1272 /* sanity checks */
1273 if (end <= start) start = end = NULL;
1274 else if ((char *)end > preloader_start &&
1275 (char *)start <= preloader_end)
1277 wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %p-%p\n",
1278 start, end, preloader_start, preloader_end );
1279 start = end = NULL;
1282 /* check for overlap with low memory areas */
1283 for (i = 0; preload_info[i].size; i++)
1285 if ((char *)preload_info[i].addr > (char *)0x00110000) break;
1286 if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size)
1288 start = end = NULL;
1289 break;
1291 if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size)
1292 start = (char *)preload_info[i].addr + preload_info[i].size;
1295 while (preload_info[i].size) i++;
1296 preload_info[i].addr = start;
1297 preload_info[i].size = (char *)end - (char *)start;
1298 return;
1300 error:
1301 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
1304 /* check if address is in one of the reserved ranges */
1305 static int is_addr_reserved( const void *addr )
1307 int i;
1309 for (i = 0; preload_info[i].size; i++)
1311 if ((const char *)addr >= (const char *)preload_info[i].addr &&
1312 (const char *)addr < (const char *)preload_info[i].addr + preload_info[i].size)
1313 return 1;
1315 return 0;
1318 /* remove a range from the preload list */
1319 static void remove_preload_range( int i )
1321 while (preload_info[i].size)
1323 preload_info[i].addr = preload_info[i+1].addr;
1324 preload_info[i].size = preload_info[i+1].size;
1325 i++;
1330 * is_in_preload_range
1332 * Check if address of the given aux value is in one of the reserved ranges
1334 static int is_in_preload_range( const struct wld_auxv *av, int type )
1336 while (av->a_type != AT_NULL)
1338 if (av->a_type == type) return is_addr_reserved( (const void *)av->a_un.a_val );
1339 av++;
1341 return 0;
1344 /* set the process name if supported */
1345 static void set_process_name( int argc, char *argv[] )
1347 int i;
1348 unsigned int off;
1349 char *p, *name, *end;
1351 /* set the process short name */
1352 for (p = name = argv[1]; *p; p++) if (p[0] == '/' && p[1]) name = p + 1;
1353 if (wld_prctl( 15 /* PR_SET_NAME */, (long)name ) == -1) return;
1355 /* find the end of the argv array and move everything down */
1356 end = argv[argc - 1];
1357 while (*end) end++;
1358 off = argv[1] - argv[0];
1359 for (p = argv[1]; p <= end; p++) *(p - off) = *p;
1360 wld_memset( end - off, 0, off );
1361 for (i = 1; i < argc; i++) argv[i] -= off;
1366 * wld_start
1368 * Repeat the actions the kernel would do when loading a dynamically linked .so
1369 * Load the binary and then its ELF interpreter.
1370 * Note, we assume that the binary is a dynamically linked ELF shared object.
1372 void* wld_start( void **stack )
1374 long i, *pargc;
1375 char **argv, **p;
1376 char *interp, *reserve = NULL;
1377 struct wld_auxv new_av[8], delete_av[3], *av;
1378 struct wld_link_map main_binary_map, ld_so_map;
1379 struct wine_preload_info **wine_main_preload_info;
1381 pargc = *stack;
1382 argv = (char **)pargc + 1;
1383 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
1385 /* skip over the parameters */
1386 p = argv + *pargc + 1;
1388 /* skip over the environment */
1389 while (*p)
1391 static const char res[] = "WINEPRELOADRESERVE=";
1392 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
1393 p++;
1396 av = (struct wld_auxv *)(p+1);
1397 page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
1398 page_mask = page_size - 1;
1400 preloader_start = (char *)_start - ((unsigned long)_start & page_mask);
1401 preloader_end = (char *)((unsigned long)(_end + page_mask) & ~page_mask);
1403 #ifdef DUMP_AUX_INFO
1404 wld_printf( "stack = %p\n", *stack );
1405 for( i = 0; i < *pargc; i++ ) wld_printf("argv[%lx] = %s\n", i, argv[i]);
1406 dump_auxiliary( av );
1407 #endif
1409 /* reserve memory that Wine needs */
1410 if (reserve) preload_reserve( reserve );
1411 for (i = 0; preload_info[i].size; i++)
1413 if ((char *)av >= (char *)preload_info[i].addr &&
1414 (char *)pargc <= (char *)preload_info[i].addr + preload_info[i].size)
1416 remove_preload_range( i );
1417 i--;
1419 else if (wld_mmap( preload_info[i].addr, preload_info[i].size, PROT_NONE,
1420 MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 ) == (void *)-1)
1422 /* don't warn for low 64k */
1423 if (preload_info[i].addr >= (void *)0x10000
1424 #ifdef __aarch64__
1425 && preload_info[i].addr < (void *)0x7fffffffff /* ARM64 address space might end here*/
1426 #endif
1428 wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
1429 preload_info[i].addr, (char *)preload_info[i].addr + preload_info[i].size );
1430 remove_preload_range( i );
1431 i--;
1435 /* add an executable page at the top of the address space to defeat
1436 * broken no-exec protections that play with the code selector limit */
1437 if (is_addr_reserved( (char *)0x80000000 - page_size ))
1438 wld_mprotect( (char *)0x80000000 - page_size, page_size, PROT_EXEC | PROT_READ );
1440 /* load the main binary */
1441 map_so_lib( argv[1], &main_binary_map );
1443 /* load the ELF interpreter */
1444 interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
1445 map_so_lib( interp, &ld_so_map );
1447 /* store pointer to the preload info into the appropriate main binary variable */
1448 wine_main_preload_info = find_symbol( &main_binary_map, "wine_main_preload_info", STT_OBJECT );
1449 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
1450 else wld_printf( "wine_main_preload_info not found\n" );
1452 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
1453 SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
1454 SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
1455 SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
1456 SET_NEW_AV( 3, AT_PAGESZ, page_size );
1457 SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
1458 SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
1459 SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
1460 SET_NEW_AV( 7, AT_NULL, 0 );
1461 #undef SET_NEW_AV
1463 i = 0;
1464 /* delete sysinfo values if addresses conflict */
1465 if (is_in_preload_range( av, AT_SYSINFO ) || is_in_preload_range( av, AT_SYSINFO_EHDR ))
1467 delete_av[i++].a_type = AT_SYSINFO;
1468 delete_av[i++].a_type = AT_SYSINFO_EHDR;
1470 delete_av[i].a_type = AT_NULL;
1472 /* get rid of first argument */
1473 set_process_name( *pargc, argv );
1474 pargc[1] = pargc[0] - 1;
1475 *stack = pargc + 1;
1477 set_auxiliary_values( av, new_av, delete_av, stack );
1479 #ifdef DUMP_AUX_INFO
1480 wld_printf("new stack = %p\n", *stack);
1481 wld_printf("jumping to %p\n", (void *)ld_so_map.l_entry);
1482 #endif
1483 #ifdef DUMP_MAPS
1485 char buffer[1024];
1486 int len, fd = wld_open( "/proc/self/maps", O_RDONLY );
1487 if (fd != -1)
1489 while ((len = wld_read( fd, buffer, sizeof(buffer) )) > 0) wld_write( 2, buffer, len );
1490 wld_close( fd );
1493 #endif
1495 return (void *)ld_so_map.l_entry;
1498 #endif /* __linux__ */