mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / loader / preloader.c
blobbb16ecd96a43a34c0e4ff36c3587c021752129f6
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"
66 #include "wine/port.h"
68 #include <stdarg.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <sys/types.h>
73 #ifdef HAVE_SYS_STAT_H
74 # include <sys/stat.h>
75 #endif
76 #include <fcntl.h>
77 #ifdef HAVE_SYS_MMAN_H
78 # include <sys/mman.h>
79 #endif
80 #ifdef HAVE_SYS_SYSCALL_H
81 # include <sys/syscall.h>
82 #endif
83 #ifdef HAVE_UNISTD_H
84 # include <unistd.h>
85 #endif
86 #ifdef HAVE_ELF_H
87 # include <elf.h>
88 #endif
89 #ifdef HAVE_LINK_H
90 # include <link.h>
91 #endif
92 #ifdef HAVE_SYS_LINK_H
93 # include <sys/link.h>
94 #endif
96 #include "wine/asm.h"
97 #include "main.h"
99 /* ELF definitions */
100 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
101 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
103 #define MAP_BASE_ADDR(l) 0
105 #ifndef MAP_COPY
106 #define MAP_COPY MAP_PRIVATE
107 #endif
108 #ifndef MAP_NORESERVE
109 #define MAP_NORESERVE 0
110 #endif
112 static struct wine_preload_info preload_info[] =
114 #ifdef __i386__
115 { (void *)0x00000000, 0x00010000 }, /* low 64k */
116 { (void *)0x00010000, 0x00100000 }, /* DOS area */
117 { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
118 { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
119 #else
120 { (void *)0x000000010000, 0x00100000 }, /* DOS area */
121 { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
122 { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
123 { (void *)0x7ffffe000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
124 #endif
125 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
126 { 0, 0 } /* end of list */
129 /* debugging */
130 #undef DUMP_SEGMENTS
131 #undef DUMP_AUX_INFO
132 #undef DUMP_SYMS
133 #undef DUMP_MAPS
135 /* older systems may not define these */
136 #ifndef PT_TLS
137 #define PT_TLS 7
138 #endif
140 #ifndef AT_SYSINFO
141 #define AT_SYSINFO 32
142 #endif
143 #ifndef AT_SYSINFO_EHDR
144 #define AT_SYSINFO_EHDR 33
145 #endif
147 #ifndef DT_GNU_HASH
148 #define DT_GNU_HASH 0x6ffffef5
149 #endif
151 static size_t page_size, page_mask;
152 static char *preloader_start, *preloader_end;
154 struct wld_link_map {
155 ElfW(Addr) l_addr;
156 ElfW(Dyn) *l_ld;
157 ElfW(Phdr)*l_phdr;
158 ElfW(Addr) l_entry;
159 ElfW(Half) l_ldnum;
160 ElfW(Half) l_phnum;
161 ElfW(Addr) l_map_start, l_map_end;
162 ElfW(Addr) l_interp;
165 struct wld_auxv
167 ElfW(Addr) a_type;
168 union
170 ElfW(Addr) a_val;
171 } a_un;
175 * The __bb_init_func is an empty function only called when file is
176 * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
177 * function is normally provided by libc's startup files, but since we
178 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
179 * provide our own (empty) version, otherwise linker fails.
181 void __bb_init_func(void) { return; }
183 /* similar to the above but for -fstack-protector */
184 void *__stack_chk_guard = 0;
185 void __stack_chk_fail_local(void) { return; }
186 void __stack_chk_fail(void) { return; }
188 #ifdef __i386__
190 /* data for setting up the glibc-style thread-local storage in %gs */
192 static int thread_data[256];
194 struct
196 /* this is the kernel modify_ldt struct */
197 unsigned int entry_number;
198 unsigned long base_addr;
199 unsigned int limit;
200 unsigned int seg_32bit : 1;
201 unsigned int contents : 2;
202 unsigned int read_exec_only : 1;
203 unsigned int limit_in_pages : 1;
204 unsigned int seg_not_present : 1;
205 unsigned int usable : 1;
206 unsigned int garbage : 25;
207 } thread_ldt = { -1, (unsigned long)thread_data, 0xfffff, 1, 0, 0, 1, 0, 1, 0 };
211 * The _start function is the entry and exit point of this program
213 * It calls wld_start, passing a pointer to the args it receives
214 * then jumps to the address wld_start returns.
216 void _start(void);
217 extern char _end[];
218 __ASM_GLOBAL_FUNC(_start,
219 __ASM_CFI("\t.cfi_undefined %eip\n")
220 "\tmovl $243,%eax\n" /* SYS_set_thread_area */
221 "\tmovl $thread_ldt,%ebx\n"
222 "\tint $0x80\n" /* allocate gs segment */
223 "\torl %eax,%eax\n"
224 "\tjl 1f\n"
225 "\tmovl thread_ldt,%eax\n" /* thread_ldt.entry_number */
226 "\tshl $3,%eax\n"
227 "\torl $3,%eax\n"
228 "\tmov %ax,%gs\n"
229 "\tmov %ax,%fs\n" /* set %fs too so libwine can retrieve it later on */
230 "1:\tmovl %esp,%eax\n"
231 "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */
232 "\tpushl %eax\n" /* orig stack pointer */
233 "\tpushl %esp\n" /* ptr to orig stack pointer */
234 "\tcall wld_start\n"
235 "\tpopl %ecx\n" /* remove ptr to stack pointer */
236 "\tpopl %esp\n" /* new stack pointer */
237 "\tpush %eax\n" /* ELF interpreter entry point */
238 "\txor %eax,%eax\n"
239 "\txor %ecx,%ecx\n"
240 "\txor %edx,%edx\n"
241 "\tmov %ax,%gs\n" /* clear %gs again */
242 "\tret\n")
244 /* wrappers for Linux system calls */
246 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
248 static inline __attribute__((noreturn)) void wld_exit( int code )
250 for (;;) /* avoid warning */
251 __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
252 : : "a" (1 /* SYS_exit */), "r" (code) );
255 static inline int wld_open( const char *name, int flags )
257 int ret;
258 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
259 : "=a" (ret) : "0" (5 /* SYS_open */), "r" (name), "c" (flags) );
260 return SYSCALL_RET(ret);
263 static inline int wld_close( int fd )
265 int ret;
266 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
267 : "=a" (ret) : "0" (6 /* SYS_close */), "r" (fd) );
268 return SYSCALL_RET(ret);
271 static inline ssize_t wld_read( int fd, void *buffer, size_t len )
273 int ret;
274 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
275 : "=a" (ret)
276 : "0" (3 /* SYS_read */), "r" (fd), "c" (buffer), "d" (len)
277 : "memory" );
278 return SYSCALL_RET(ret);
281 static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
283 int ret;
284 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
285 : "=a" (ret) : "0" (4 /* SYS_write */), "r" (fd), "c" (buffer), "d" (len) );
286 return SYSCALL_RET(ret);
289 static inline int wld_mprotect( const void *addr, size_t len, int prot )
291 int ret;
292 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
293 : "=a" (ret) : "0" (125 /* SYS_mprotect */), "r" (addr), "c" (len), "d" (prot) );
294 return SYSCALL_RET(ret);
297 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, unsigned int offset );
298 __ASM_GLOBAL_FUNC(wld_mmap,
299 "\tpushl %ebp\n"
300 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
301 "\tpushl %ebx\n"
302 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
303 "\tpushl %esi\n"
304 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
305 "\tpushl %edi\n"
306 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
307 "\tmovl $192,%eax\n" /* SYS_mmap2 */
308 "\tmovl 20(%esp),%ebx\n" /* start */
309 "\tmovl 24(%esp),%ecx\n" /* len */
310 "\tmovl 28(%esp),%edx\n" /* prot */
311 "\tmovl 32(%esp),%esi\n" /* flags */
312 "\tmovl 36(%esp),%edi\n" /* fd */
313 "\tmovl 40(%esp),%ebp\n" /* offset */
314 "\tshrl $12,%ebp\n"
315 "\tint $0x80\n"
316 "\tcmpl $-4096,%eax\n"
317 "\tjbe 2f\n"
318 "\tcmpl $-38,%eax\n" /* ENOSYS */
319 "\tjne 1f\n"
320 "\tmovl $90,%eax\n" /* SYS_mmap */
321 "\tleal 20(%esp),%ebx\n"
322 "\tint $0x80\n"
323 "\tcmpl $-4096,%eax\n"
324 "\tjbe 2f\n"
325 "1:\tmovl $-1,%eax\n"
326 "2:\tpopl %edi\n"
327 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
328 "\tpopl %esi\n"
329 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
330 "\tpopl %ebx\n"
331 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
332 "\tpopl %ebp\n"
333 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
334 "\tret\n" )
336 static inline int wld_prctl( int code, long arg )
338 int ret;
339 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
340 : "=a" (ret) : "0" (172 /* SYS_prctl */), "r" (code), "c" (arg) );
341 return SYSCALL_RET(ret);
344 #elif defined(__x86_64__)
346 void *thread_data[256];
349 * The _start function is the entry and exit point of this program
351 * It calls wld_start, passing a pointer to the args it receives
352 * then jumps to the address wld_start returns.
354 void _start(void);
355 extern char _end[];
356 __ASM_GLOBAL_FUNC(_start,
357 __ASM_CFI(".cfi_undefined %rip\n\t")
358 "movq %rsp,%rax\n\t"
359 "leaq -144(%rsp),%rsp\n\t" /* allocate some space for extra aux values */
360 "movq %rax,(%rsp)\n\t" /* orig stack pointer */
361 "movq $thread_data,%rsi\n\t"
362 "movq $0x1002,%rdi\n\t" /* ARCH_SET_FS */
363 "movq $158,%rax\n\t" /* SYS_arch_prctl */
364 "syscall\n\t"
365 "movq %rsp,%rdi\n\t" /* ptr to orig stack pointer */
366 "call wld_start\n\t"
367 "movq (%rsp),%rsp\n\t" /* new stack pointer */
368 "pushq %rax\n\t" /* ELF interpreter entry point */
369 "xorq %rax,%rax\n\t"
370 "xorq %rcx,%rcx\n\t"
371 "xorq %rdx,%rdx\n\t"
372 "xorq %rsi,%rsi\n\t"
373 "xorq %rdi,%rdi\n\t"
374 "xorq %r8,%r8\n\t"
375 "xorq %r9,%r9\n\t"
376 "xorq %r10,%r10\n\t"
377 "xorq %r11,%r11\n\t"
378 "ret")
380 #define SYSCALL_FUNC( name, nr ) \
381 __ASM_GLOBAL_FUNC( name, \
382 "movq $" #nr ",%rax\n\t" \
383 "movq %rcx,%r10\n\t" \
384 "syscall\n\t" \
385 "leaq 4096(%rax),%rcx\n\t" \
386 "movq $-1,%rdx\n\t" \
387 "cmp $4096,%rcx\n\t" \
388 "cmovb %rdx,%rax\n\t" \
389 "ret" )
391 #define SYSCALL_NOERR( name, nr ) \
392 __ASM_GLOBAL_FUNC( name, \
393 "movq $" #nr ",%rax\n\t" \
394 "syscall\n\t" \
395 "ret" )
397 void wld_exit( int code ) __attribute__((noreturn));
398 SYSCALL_NOERR( wld_exit, 60 /* SYS_exit */ );
400 ssize_t wld_read( int fd, void *buffer, size_t len );
401 SYSCALL_FUNC( wld_read, 0 /* SYS_read */ );
403 ssize_t wld_write( int fd, const void *buffer, size_t len );
404 SYSCALL_FUNC( wld_write, 1 /* SYS_write */ );
406 int wld_open( const char *name, int flags );
407 SYSCALL_FUNC( wld_open, 2 /* SYS_open */ );
409 int wld_close( int fd );
410 SYSCALL_FUNC( wld_close, 3 /* SYS_close */ );
412 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
413 SYSCALL_FUNC( wld_mmap, 9 /* SYS_mmap */ );
415 int wld_mprotect( const void *addr, size_t len, int prot );
416 SYSCALL_FUNC( wld_mprotect, 10 /* SYS_mprotect */ );
418 int wld_prctl( int code, long arg );
419 SYSCALL_FUNC( wld_prctl, 157 /* SYS_prctl */ );
421 uid_t wld_getuid(void);
422 SYSCALL_NOERR( wld_getuid, 102 /* SYS_getuid */ );
424 gid_t wld_getgid(void);
425 SYSCALL_NOERR( wld_getgid, 104 /* SYS_getgid */ );
427 uid_t wld_geteuid(void);
428 SYSCALL_NOERR( wld_geteuid, 107 /* SYS_geteuid */ );
430 gid_t wld_getegid(void);
431 SYSCALL_NOERR( wld_getegid, 108 /* SYS_getegid */ );
433 #elif defined(__aarch64__)
435 void *thread_data[256];
438 * The _start function is the entry and exit point of this program
440 * It calls wld_start, passing a pointer to the args it receives
441 * then jumps to the address wld_start returns.
443 void _start(void);
444 extern char _end[];
445 __ASM_GLOBAL_FUNC(_start,
446 "mov x0, SP\n\t"
447 "sub SP, SP, #144\n\t" /* allocate some space for extra aux values */
448 "str x0, [SP]\n\t" /* orig stack pointer */
449 "ldr x0, =thread_data\n\t"
450 "msr tpidr_el0, x0\n\t"
451 "mov x0, SP\n\t" /* ptr to orig stack pointer */
452 "bl wld_start\n\t"
453 "ldr x1, [SP]\n\t" /* new stack pointer */
454 "mov SP, x1\n\t"
455 "mov x30, x0\n\t"
456 "mov x0, #0\n\t"
457 "mov x1, #0\n\t"
458 "mov x2, #0\n\t"
459 "mov x3, #0\n\t"
460 "mov x4, #0\n\t"
461 "mov x5, #0\n\t"
462 "mov x6, #0\n\t"
463 "mov x7, #0\n\t"
464 "mov x8, #0\n\t"
465 "mov x9, #0\n\t"
466 "mov x10, #0\n\t"
467 "mov x11, #0\n\t"
468 "mov x12, #0\n\t"
469 "mov x13, #0\n\t"
470 "mov x14, #0\n\t"
471 "mov x15, #0\n\t"
472 "mov x16, #0\n\t"
473 "mov x17, #0\n\t"
474 "mov x18, #0\n\t"
475 "ret")
477 #define SYSCALL_FUNC( name, nr ) \
478 __ASM_GLOBAL_FUNC( name, \
479 "stp x8, x9, [SP, #-16]!\n\t" \
480 "mov x8, #" #nr "\n\t" \
481 "svc #0\n\t" \
482 "ldp x8, x9, [SP], #16\n\t" \
483 "cmn x0, #1, lsl#12\n\t" \
484 "cinv x0, x0, hi\n\t" \
485 "b.hi 1f\n\t" \
486 "ret\n\t" \
487 "1: mov x0, #-1\n\t" \
488 "ret" )
490 #define SYSCALL_NOERR( name, nr ) \
491 __ASM_GLOBAL_FUNC( name, \
492 "stp x8, x9, [SP, #-16]!\n\t" \
493 "mov x8, #" #nr "\n\t" \
494 "svc #0\n\t" \
495 "ldp x8, x9, [SP], #16\n\t" \
496 "ret" )
498 void wld_exit( int code ) __attribute__((noreturn));
499 SYSCALL_NOERR( wld_exit, 93 /* SYS_exit */ );
501 ssize_t wld_read( int fd, void *buffer, size_t len );
502 SYSCALL_FUNC( wld_read, 63 /* SYS_read */ );
504 ssize_t wld_write( int fd, const void *buffer, size_t len );
505 SYSCALL_FUNC( wld_write, 64 /* SYS_write */ );
507 int wld_openat( int dirfd, const char *name, int flags );
508 SYSCALL_FUNC( wld_openat, 56 /* SYS_openat */ );
510 int wld_open( const char *name, int flags )
512 return wld_openat(-100 /* AT_FDCWD */, name, flags);
515 int wld_close( int fd );
516 SYSCALL_FUNC( wld_close, 57 /* SYS_close */ );
518 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
519 SYSCALL_FUNC( wld_mmap, 222 /* SYS_mmap */ );
521 int wld_mprotect( const void *addr, size_t len, int prot );
522 SYSCALL_FUNC( wld_mprotect, 226 /* SYS_mprotect */ );
524 int wld_prctl( int code, long arg );
525 SYSCALL_FUNC( wld_prctl, 167 /* SYS_prctl */ );
527 uid_t wld_getuid(void);
528 SYSCALL_NOERR( wld_getuid, 174 /* SYS_getuid */ );
530 gid_t wld_getgid(void);
531 SYSCALL_NOERR( wld_getgid, 176 /* SYS_getgid */ );
533 uid_t wld_geteuid(void);
534 SYSCALL_NOERR( wld_geteuid, 175 /* SYS_geteuid */ );
536 gid_t wld_getegid(void);
537 SYSCALL_NOERR( wld_getegid, 177 /* SYS_getegid */ );
539 #else
540 #error preloader not implemented for this CPU
541 #endif
543 /* replacement for libc functions */
545 static int wld_strcmp( const char *str1, const char *str2 )
547 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
548 return *str1 - *str2;
551 static int wld_strncmp( const char *str1, const char *str2, size_t len )
553 if (len <= 0) return 0;
554 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
555 return *str1 - *str2;
558 static inline void *wld_memset( void *dest, int val, size_t len )
560 char *dst = dest;
561 while (len--) *dst++ = val;
562 return dest;
566 * wld_printf - just the basics
568 * %x prints a hex number
569 * %s prints a string
570 * %p prints a pointer
572 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
574 static const char hex_chars[16] = "0123456789abcdef";
575 const char *p = fmt;
576 char *str = buffer;
577 int i;
579 while( *p )
581 if( *p == '%' )
583 p++;
584 if( *p == 'x' )
586 unsigned int x = va_arg( args, unsigned int );
587 for (i = 2*sizeof(x) - 1; i >= 0; i--)
588 *str++ = hex_chars[(x>>(i*4))&0xf];
590 else if (p[0] == 'l' && p[1] == 'x')
592 unsigned long x = va_arg( args, unsigned long );
593 for (i = 2*sizeof(x) - 1; i >= 0; i--)
594 *str++ = hex_chars[(x>>(i*4))&0xf];
595 p++;
597 else if( *p == 'p' )
599 unsigned long x = (unsigned long)va_arg( args, void * );
600 for (i = 2*sizeof(x) - 1; i >= 0; i--)
601 *str++ = hex_chars[(x>>(i*4))&0xf];
603 else if( *p == 's' )
605 char *s = va_arg( args, char * );
606 while(*s)
607 *str++ = *s++;
609 else if( *p == 0 )
610 break;
611 p++;
613 *str++ = *p++;
615 *str = 0;
616 return str - buffer;
619 static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
621 va_list args;
622 char buffer[256];
623 int len;
625 va_start( args, fmt );
626 len = wld_vsprintf(buffer, fmt, args );
627 va_end( args );
628 wld_write(2, buffer, len);
631 static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
633 va_list args;
634 char buffer[256];
635 int len;
637 va_start( args, fmt );
638 len = wld_vsprintf(buffer, fmt, args );
639 va_end( args );
640 wld_write(2, buffer, len);
641 wld_exit(1);
644 #ifdef DUMP_AUX_INFO
646 * Dump interesting bits of the ELF auxv_t structure that is passed
647 * as the 4th parameter to the _start function
649 static void dump_auxiliary( struct wld_auxv *av )
651 #define NAME(at) { at, #at }
652 static const struct { int val; const char *name; } names[] =
654 NAME(AT_BASE),
655 NAME(AT_CLKTCK),
656 NAME(AT_EGID),
657 NAME(AT_ENTRY),
658 NAME(AT_EUID),
659 NAME(AT_FLAGS),
660 NAME(AT_GID),
661 NAME(AT_HWCAP),
662 NAME(AT_PAGESZ),
663 NAME(AT_PHDR),
664 NAME(AT_PHENT),
665 NAME(AT_PHNUM),
666 NAME(AT_PLATFORM),
667 NAME(AT_SYSINFO),
668 NAME(AT_SYSINFO_EHDR),
669 NAME(AT_UID),
670 { 0, NULL }
672 #undef NAME
674 int i;
676 for ( ; av->a_type != AT_NULL; av++)
678 for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
679 if (names[i].name) wld_printf("%s = %lx\n", names[i].name, (unsigned long)av->a_un.a_val);
680 else wld_printf( "%lx = %lx\n", (unsigned long)av->a_type, (unsigned long)av->a_un.a_val );
683 #endif
686 * set_auxiliary_values
688 * Set the new auxiliary values
690 static void set_auxiliary_values( struct wld_auxv *av, const struct wld_auxv *new_av,
691 const struct wld_auxv *delete_av, void **stack )
693 int i, j, av_count = 0, new_count = 0, delete_count = 0;
694 char *src, *dst;
696 /* count how many aux values we have already */
697 while (av[av_count].a_type != AT_NULL) av_count++;
699 /* delete unwanted values */
700 for (j = 0; delete_av[j].a_type != AT_NULL; j++)
702 for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
704 av[i].a_type = av[av_count-1].a_type;
705 av[i].a_un.a_val = av[av_count-1].a_un.a_val;
706 av[--av_count].a_type = AT_NULL;
707 delete_count++;
708 break;
712 /* count how many values we have in new_av that aren't in av */
713 for (j = 0; new_av[j].a_type != AT_NULL; j++)
715 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
716 if (i == av_count) new_count++;
719 src = (char *)*stack;
720 dst = src - (new_count - delete_count) * sizeof(*av);
721 dst = (char *)((unsigned long)dst & ~15);
722 if (dst < src) /* need to make room for the extra values */
724 int len = (char *)(av + av_count + 1) - src;
725 for (i = 0; i < len; i++) dst[i] = src[i];
727 else if (dst > src) /* get rid of unused values */
729 int len = (char *)(av + av_count + 1) - src;
730 for (i = len - 1; i >= 0; i--) dst[i] = src[i];
732 *stack = dst;
733 av = (struct wld_auxv *)((char *)av + (dst - src));
735 /* now set the values */
736 for (j = 0; new_av[j].a_type != AT_NULL; j++)
738 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
739 if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
740 else
742 av[av_count].a_type = new_av[j].a_type;
743 av[av_count].a_un.a_val = new_av[j].a_un.a_val;
744 av_count++;
748 #ifdef DUMP_AUX_INFO
749 wld_printf("New auxiliary info:\n");
750 dump_auxiliary( av );
751 #endif
755 * get_auxiliary
757 * Get a field of the auxiliary structure
759 static int get_auxiliary( struct wld_auxv *av, int type, int def_val )
761 for ( ; av->a_type != AT_NULL; av++)
762 if( av->a_type == type ) return av->a_un.a_val;
763 return def_val;
767 * map_so_lib
769 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
771 * This function maps the segments from an ELF object, and optionally
772 * stores information about the mapping into the auxv_t structure.
774 static void map_so_lib( const char *name, struct wld_link_map *l)
776 int fd;
777 unsigned char buf[0x800];
778 ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
779 ElfW(Phdr) *phdr, *ph;
780 /* Scan the program header table, collecting its load commands. */
781 struct loadcmd
783 ElfW(Addr) mapstart, mapend, dataend, allocend;
784 off_t mapoff;
785 int prot;
786 } loadcmds[16], *c;
787 size_t nloadcmds = 0, maplength;
789 fd = wld_open( name, O_RDONLY );
790 if (fd == -1) fatal_error("%s: could not open\n", name );
792 if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
793 fatal_error("%s: failed to read ELF header\n", name);
795 phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
797 if( ( header->e_ident[0] != 0x7f ) ||
798 ( header->e_ident[1] != 'E' ) ||
799 ( header->e_ident[2] != 'L' ) ||
800 ( header->e_ident[3] != 'F' ) )
801 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
803 #ifdef __i386__
804 if( header->e_machine != EM_386 )
805 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
806 #elif defined(__x86_64__)
807 if( header->e_machine != EM_X86_64 )
808 fatal_error("%s: not an x86-64 ELF binary... don't know how to load it\n", name );
809 #elif defined(__aarch64__)
810 if( header->e_machine != EM_AARCH64 )
811 fatal_error("%s: not an aarch64 ELF binary... don't know how to load it\n", name );
812 #endif
814 if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
815 fatal_error( "%s: oops... not enough space for load commands\n", name );
817 maplength = header->e_phnum * sizeof (ElfW(Phdr));
818 if (header->e_phoff + maplength > sizeof(buf))
819 fatal_error( "%s: oops... not enough space for ELF headers\n", name );
821 l->l_ld = 0;
822 l->l_addr = 0;
823 l->l_phdr = 0;
824 l->l_phnum = header->e_phnum;
825 l->l_entry = header->e_entry;
826 l->l_interp = 0;
828 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
831 #ifdef DUMP_SEGMENTS
832 wld_printf( "ph = %p\n", ph );
833 wld_printf( " p_type = %lx\n", (unsigned long)ph->p_type );
834 wld_printf( " p_flags = %lx\n", (unsigned long)ph->p_flags );
835 wld_printf( " p_offset = %lx\n", (unsigned long)ph->p_offset );
836 wld_printf( " p_vaddr = %lx\n", (unsigned long)ph->p_vaddr );
837 wld_printf( " p_paddr = %lx\n", (unsigned long)ph->p_paddr );
838 wld_printf( " p_filesz = %lx\n", (unsigned long)ph->p_filesz );
839 wld_printf( " p_memsz = %lx\n", (unsigned long)ph->p_memsz );
840 wld_printf( " p_align = %lx\n", (unsigned long)ph->p_align );
841 #endif
843 switch (ph->p_type)
845 /* These entries tell us where to find things once the file's
846 segments are mapped in. We record the addresses it says
847 verbatim, and later correct for the run-time load address. */
848 case PT_DYNAMIC:
849 l->l_ld = (void *) ph->p_vaddr;
850 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
851 break;
853 case PT_PHDR:
854 l->l_phdr = (void *) ph->p_vaddr;
855 break;
857 case PT_LOAD:
859 if ((ph->p_align & page_mask) != 0)
860 fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
862 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
863 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
865 c = &loadcmds[nloadcmds++];
866 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
867 c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
868 c->dataend = ph->p_vaddr + ph->p_filesz;
869 c->allocend = ph->p_vaddr + ph->p_memsz;
870 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
872 c->prot = 0;
873 if (ph->p_flags & PF_R)
874 c->prot |= PROT_READ;
875 if (ph->p_flags & PF_W)
876 c->prot |= PROT_WRITE;
877 if (ph->p_flags & PF_X)
878 c->prot |= PROT_EXEC;
880 break;
882 case PT_INTERP:
883 l->l_interp = ph->p_vaddr;
884 break;
886 case PT_TLS:
888 * We don't need to set anything up because we're
889 * emulating the kernel, not ld-linux.so.2
890 * The ELF loader will set up the TLS data itself.
892 case PT_SHLIB:
893 case PT_NOTE:
894 default:
895 break;
899 /* Now process the load commands and map segments into memory. */
900 if (!nloadcmds)
901 fatal_error( "%s: no segments to load\n", name );
902 c = loadcmds;
904 /* Length of the sections to be loaded. */
905 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
907 if( header->e_type == ET_DYN )
909 ElfW(Addr) mappref;
910 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
911 - MAP_BASE_ADDR (l));
913 /* Remember which part of the address space this object uses. */
914 l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
915 c->prot, MAP_COPY | MAP_FILE,
916 fd, c->mapoff);
917 /* wld_printf("set : offset = %x\n", c->mapoff); */
918 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
920 l->l_map_end = l->l_map_start + maplength;
921 l->l_addr = l->l_map_start - c->mapstart;
923 wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
924 loadcmds[nloadcmds - 1].allocend - c->mapend,
925 PROT_NONE);
926 goto postmap;
928 else
930 /* sanity check */
931 if ((char *)c->mapstart + maplength > preloader_start &&
932 (char *)c->mapstart <= preloader_end)
933 fatal_error( "%s: binary overlaps preloader (%p-%p)\n",
934 name, (char *)c->mapstart, (char *)c->mapstart + maplength );
936 ELF_FIXED_ADDRESS (loader, c->mapstart);
939 /* Remember which part of the address space this object uses. */
940 l->l_map_start = c->mapstart + l->l_addr;
941 l->l_map_end = l->l_map_start + maplength;
943 while (c < &loadcmds[nloadcmds])
945 if (c->mapend > c->mapstart)
946 /* Map the segment contents from the file. */
947 wld_mmap ((void *) (l->l_addr + c->mapstart),
948 c->mapend - c->mapstart, c->prot,
949 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
951 postmap:
952 if (l->l_phdr == 0
953 && (ElfW(Off)) c->mapoff <= header->e_phoff
954 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
955 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
956 /* Found the program header in this segment. */
957 l->l_phdr = (void *)(unsigned long)(c->mapstart + header->e_phoff - c->mapoff);
959 if (c->allocend > c->dataend)
961 /* Extra zero pages should appear at the end of this segment,
962 after the data mapped from the file. */
963 ElfW(Addr) zero, zeroend, zeropage;
965 zero = l->l_addr + c->dataend;
966 zeroend = l->l_addr + c->allocend;
967 zeropage = (zero + page_mask) & ~page_mask;
970 * This is different from the dl-load load...
971 * ld-linux.so.2 relies on the whole page being zero'ed
973 zeroend = (zeroend + page_mask) & ~page_mask;
975 if (zeroend < zeropage)
977 /* All the extra data is in the last page of the segment.
978 We can just zero it. */
979 zeropage = zeroend;
982 if (zeropage > zero)
984 /* Zero the final part of the last page of the segment. */
985 if ((c->prot & PROT_WRITE) == 0)
987 /* Dag nab it. */
988 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
990 wld_memset ((void *) zero, '\0', zeropage - zero);
991 if ((c->prot & PROT_WRITE) == 0)
992 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
995 if (zeroend > zeropage)
997 /* Map the remaining zero pages in from the zero fill FD. */
998 wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
999 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1000 -1, 0);
1004 ++c;
1007 if (l->l_phdr == NULL) fatal_error("no program header\n");
1009 l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
1010 l->l_entry += l->l_addr;
1012 wld_close( fd );
1016 static unsigned int wld_elf_hash( const char *name )
1018 unsigned int hi, hash = 0;
1019 while (*name)
1021 hash = (hash << 4) + (unsigned char)*name++;
1022 hi = hash & 0xf0000000;
1023 hash ^= hi;
1024 hash ^= hi >> 24;
1026 return hash;
1029 static unsigned int gnu_hash( const char *name )
1031 unsigned int h = 5381;
1032 while (*name) h = h * 33 + (unsigned char)*name++;
1033 return h;
1037 * Find a symbol in the symbol table of the executable loaded
1039 static void *find_symbol( const struct wld_link_map *map, const char *var, int type )
1041 const ElfW(Dyn) *dyn = NULL;
1042 const ElfW(Phdr) *ph;
1043 const ElfW(Sym) *symtab = NULL;
1044 const Elf32_Word *hashtab = NULL;
1045 const Elf32_Word *gnu_hashtab = NULL;
1046 const char *strings = NULL;
1047 Elf32_Word idx;
1049 /* check the values */
1050 #ifdef DUMP_SYMS
1051 wld_printf("%p %x\n", map->l_phdr, map->l_phnum );
1052 #endif
1053 /* parse the (already loaded) ELF executable's header */
1054 for (ph = map->l_phdr; ph < &map->l_phdr[map->l_phnum]; ++ph)
1056 if( PT_DYNAMIC == ph->p_type )
1058 dyn = (void *)(ph->p_vaddr + map->l_addr);
1059 break;
1062 if( !dyn ) return NULL;
1064 while( dyn->d_tag )
1066 if( dyn->d_tag == DT_STRTAB )
1067 strings = (const char*)(dyn->d_un.d_ptr + map->l_addr);
1068 if( dyn->d_tag == DT_SYMTAB )
1069 symtab = (const ElfW(Sym) *)(dyn->d_un.d_ptr + map->l_addr);
1070 if( dyn->d_tag == DT_HASH )
1071 hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
1072 if( dyn->d_tag == DT_GNU_HASH )
1073 gnu_hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
1074 #ifdef DUMP_SYMS
1075 wld_printf("%lx %p\n", (unsigned long)dyn->d_tag, (void *)dyn->d_un.d_ptr );
1076 #endif
1077 dyn++;
1080 if( (!symtab) || (!strings) ) return NULL;
1082 if (gnu_hashtab) /* new style hash table */
1084 const unsigned int hash = gnu_hash(var);
1085 const Elf32_Word nbuckets = gnu_hashtab[0];
1086 const Elf32_Word symbias = gnu_hashtab[1];
1087 const Elf32_Word nwords = gnu_hashtab[2];
1088 const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
1089 const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
1090 const Elf32_Word *chains = buckets + nbuckets - symbias;
1092 if (!(idx = buckets[hash % nbuckets])) return NULL;
1095 if ((chains[idx] & ~1u) == (hash & ~1u) &&
1096 ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
1097 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
1098 !wld_strcmp( strings + symtab[idx].st_name, var ))
1099 goto found;
1100 } while (!(chains[idx++] & 1u));
1102 else if (hashtab) /* old style hash table */
1104 const unsigned int hash = wld_elf_hash(var);
1105 const Elf32_Word nbuckets = hashtab[0];
1106 const Elf32_Word *buckets = hashtab + 2;
1107 const Elf32_Word *chains = buckets + nbuckets;
1109 for (idx = buckets[hash % nbuckets]; idx; idx = chains[idx])
1111 if (ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
1112 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
1113 !wld_strcmp( strings + symtab[idx].st_name, var ))
1114 goto found;
1117 return NULL;
1119 found:
1120 #ifdef DUMP_SYMS
1121 wld_printf("Found %s -> %p\n", strings + symtab[idx].st_name, (void *)symtab[idx].st_value );
1122 #endif
1123 return (void *)(symtab[idx].st_value + map->l_addr);
1127 * preload_reserve
1129 * Reserve a range specified in string format
1131 static void preload_reserve( const char *str )
1133 const char *p;
1134 unsigned long result = 0;
1135 void *start = NULL, *end = NULL;
1136 int i, first = 1;
1138 for (p = str; *p; p++)
1140 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
1141 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
1142 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
1143 else if (*p == '-')
1145 if (!first) goto error;
1146 start = (void *)(result & ~page_mask);
1147 result = 0;
1148 first = 0;
1150 else goto error;
1152 if (!first) end = (void *)((result + page_mask) & ~page_mask);
1153 else if (result) goto error; /* single value '0' is allowed */
1155 /* sanity checks */
1156 if (end <= start) start = end = NULL;
1157 else if ((char *)end > preloader_start &&
1158 (char *)start <= preloader_end)
1160 wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %p-%p\n",
1161 start, end, preloader_start, preloader_end );
1162 start = end = NULL;
1165 /* check for overlap with low memory areas */
1166 for (i = 0; preload_info[i].size; i++)
1168 if ((char *)preload_info[i].addr > (char *)0x00110000) break;
1169 if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size)
1171 start = end = NULL;
1172 break;
1174 if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size)
1175 start = (char *)preload_info[i].addr + preload_info[i].size;
1178 while (preload_info[i].size) i++;
1179 preload_info[i].addr = start;
1180 preload_info[i].size = (char *)end - (char *)start;
1181 return;
1183 error:
1184 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
1187 /* check if address is in one of the reserved ranges */
1188 static int is_addr_reserved( const void *addr )
1190 int i;
1192 for (i = 0; preload_info[i].size; i++)
1194 if ((const char *)addr >= (const char *)preload_info[i].addr &&
1195 (const char *)addr < (const char *)preload_info[i].addr + preload_info[i].size)
1196 return 1;
1198 return 0;
1201 /* remove a range from the preload list */
1202 static void remove_preload_range( int i )
1204 while (preload_info[i].size)
1206 preload_info[i].addr = preload_info[i+1].addr;
1207 preload_info[i].size = preload_info[i+1].size;
1208 i++;
1213 * is_in_preload_range
1215 * Check if address of the given aux value is in one of the reserved ranges
1217 static int is_in_preload_range( const struct wld_auxv *av, int type )
1219 while (av->a_type != AT_NULL)
1221 if (av->a_type == type) return is_addr_reserved( (const void *)av->a_un.a_val );
1222 av++;
1224 return 0;
1227 /* set the process name if supported */
1228 static void set_process_name( int argc, char *argv[] )
1230 int i;
1231 unsigned int off;
1232 char *p, *name, *end;
1234 /* set the process short name */
1235 for (p = name = argv[1]; *p; p++) if (p[0] == '/' && p[1]) name = p + 1;
1236 if (wld_prctl( 15 /* PR_SET_NAME */, (long)name ) == -1) return;
1238 /* find the end of the argv array and move everything down */
1239 end = argv[argc - 1];
1240 while (*end) end++;
1241 off = argv[1] - argv[0];
1242 for (p = argv[1]; p <= end; p++) *(p - off) = *p;
1243 wld_memset( end - off, 0, off );
1244 for (i = 1; i < argc; i++) argv[i] -= off;
1249 * wld_start
1251 * Repeat the actions the kernel would do when loading a dynamically linked .so
1252 * Load the binary and then its ELF interpreter.
1253 * Note, we assume that the binary is a dynamically linked ELF shared object.
1255 void* wld_start( void **stack )
1257 long i, *pargc;
1258 char **argv, **p;
1259 char *interp, *reserve = NULL;
1260 struct wld_auxv new_av[8], delete_av[3], *av;
1261 struct wld_link_map main_binary_map, ld_so_map;
1262 struct wine_preload_info **wine_main_preload_info;
1264 pargc = *stack;
1265 argv = (char **)pargc + 1;
1266 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
1268 /* skip over the parameters */
1269 p = argv + *pargc + 1;
1271 /* skip over the environment */
1272 while (*p)
1274 static const char res[] = "WINEPRELOADRESERVE=";
1275 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
1276 p++;
1279 av = (struct wld_auxv *)(p+1);
1280 page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
1281 page_mask = page_size - 1;
1283 preloader_start = (char *)_start - ((unsigned long)_start & page_mask);
1284 preloader_end = (char *)((unsigned long)(_end + page_mask) & ~page_mask);
1286 #ifdef DUMP_AUX_INFO
1287 wld_printf( "stack = %p\n", *stack );
1288 for( i = 0; i < *pargc; i++ ) wld_printf("argv[%lx] = %s\n", i, argv[i]);
1289 dump_auxiliary( av );
1290 #endif
1292 /* reserve memory that Wine needs */
1293 if (reserve) preload_reserve( reserve );
1294 for (i = 0; preload_info[i].size; i++)
1296 if ((char *)av >= (char *)preload_info[i].addr &&
1297 (char *)pargc <= (char *)preload_info[i].addr + preload_info[i].size)
1299 remove_preload_range( i );
1300 i--;
1302 else if (wld_mmap( preload_info[i].addr, preload_info[i].size, PROT_NONE,
1303 MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 ) == (void *)-1)
1305 /* don't warn for low 64k */
1306 if (preload_info[i].addr >= (void *)0x10000
1307 #ifdef __aarch64__
1308 && preload_info[i].addr < (void *)0x7fffffffff /* ARM64 address space might end here*/
1309 #endif
1311 wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
1312 preload_info[i].addr, (char *)preload_info[i].addr + preload_info[i].size );
1313 remove_preload_range( i );
1314 i--;
1318 /* add an executable page at the top of the address space to defeat
1319 * broken no-exec protections that play with the code selector limit */
1320 if (is_addr_reserved( (char *)0x80000000 - page_size ))
1321 wld_mprotect( (char *)0x80000000 - page_size, page_size, PROT_EXEC | PROT_READ );
1323 /* load the main binary */
1324 map_so_lib( argv[1], &main_binary_map );
1326 /* load the ELF interpreter */
1327 interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
1328 map_so_lib( interp, &ld_so_map );
1330 /* store pointer to the preload info into the appropriate main binary variable */
1331 wine_main_preload_info = find_symbol( &main_binary_map, "wine_main_preload_info", STT_OBJECT );
1332 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
1333 else wld_printf( "wine_main_preload_info not found\n" );
1335 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
1336 SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
1337 SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
1338 SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
1339 SET_NEW_AV( 3, AT_PAGESZ, page_size );
1340 SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
1341 SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
1342 SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
1343 SET_NEW_AV( 7, AT_NULL, 0 );
1344 #undef SET_NEW_AV
1346 i = 0;
1347 /* delete sysinfo values if addresses conflict */
1348 if (is_in_preload_range( av, AT_SYSINFO ) || is_in_preload_range( av, AT_SYSINFO_EHDR ))
1350 delete_av[i++].a_type = AT_SYSINFO;
1351 delete_av[i++].a_type = AT_SYSINFO_EHDR;
1353 delete_av[i].a_type = AT_NULL;
1355 /* get rid of first argument */
1356 set_process_name( *pargc, argv );
1357 pargc[1] = pargc[0] - 1;
1358 *stack = pargc + 1;
1360 set_auxiliary_values( av, new_av, delete_av, stack );
1362 #ifdef DUMP_AUX_INFO
1363 wld_printf("new stack = %p\n", *stack);
1364 wld_printf("jumping to %p\n", (void *)ld_so_map.l_entry);
1365 #endif
1366 #ifdef DUMP_MAPS
1368 char buffer[1024];
1369 int len, fd = wld_open( "/proc/self/maps", O_RDONLY );
1370 if (fd != -1)
1372 while ((len = wld_read( fd, buffer, sizeof(buffer) )) > 0) wld_write( 2, buffer, len );
1373 wld_close( fd );
1376 #endif
1378 return (void *)ld_so_map.l_entry;
1381 #endif /* __linux__ */