shell32/tests: Fix some leaks (Valgrind).
[wine.git] / loader / preloader.c
blob13a6ad86b20919d474b925fd6bd683cf431642aa
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 "main.h"
98 /* ELF definitions */
99 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
100 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
102 #define MAP_BASE_ADDR(l) 0
104 #ifndef MAP_COPY
105 #define MAP_COPY MAP_PRIVATE
106 #endif
107 #ifndef MAP_NORESERVE
108 #define MAP_NORESERVE 0
109 #endif
111 static struct wine_preload_info preload_info[] =
113 #ifdef __i386__
114 { (void *)0x00000000, 0x00010000 }, /* low 64k */
115 { (void *)0x00010000, 0x00100000 }, /* DOS area */
116 { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
117 { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
118 #else
119 { (void *)0x000000010000, 0x00100000 }, /* DOS area */
120 { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
121 { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
122 { (void *)0x7ffffe000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
123 #endif
124 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
125 { 0, 0 } /* end of list */
128 /* debugging */
129 #undef DUMP_SEGMENTS
130 #undef DUMP_AUX_INFO
131 #undef DUMP_SYMS
132 #undef DUMP_MAPS
134 /* older systems may not define these */
135 #ifndef PT_TLS
136 #define PT_TLS 7
137 #endif
139 #ifndef AT_SYSINFO
140 #define AT_SYSINFO 32
141 #endif
142 #ifndef AT_SYSINFO_EHDR
143 #define AT_SYSINFO_EHDR 33
144 #endif
146 #ifndef DT_GNU_HASH
147 #define DT_GNU_HASH 0x6ffffef5
148 #endif
150 static size_t page_size, page_mask;
151 static char *preloader_start, *preloader_end;
153 struct wld_link_map {
154 ElfW(Addr) l_addr;
155 ElfW(Dyn) *l_ld;
156 ElfW(Phdr)*l_phdr;
157 ElfW(Addr) l_entry;
158 ElfW(Half) l_ldnum;
159 ElfW(Half) l_phnum;
160 ElfW(Addr) l_map_start, l_map_end;
161 ElfW(Addr) l_interp;
164 struct wld_auxv
166 ElfW(Addr) a_type;
167 union
169 ElfW(Addr) a_val;
170 } a_un;
174 * The __bb_init_func is an empty function only called when file is
175 * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
176 * function is normally provided by libc's startup files, but since we
177 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
178 * provide our own (empty) version, otherwise linker fails.
180 void __bb_init_func(void) { return; }
182 /* similar to the above but for -fstack-protector */
183 void *__stack_chk_guard = 0;
184 void __stack_chk_fail_local(void) { return; }
185 void __stack_chk_fail(void) { return; }
187 #ifdef __i386__
189 /* data for setting up the glibc-style thread-local storage in %gs */
191 static int thread_data[256];
193 struct
195 /* this is the kernel modify_ldt struct */
196 unsigned int entry_number;
197 unsigned long base_addr;
198 unsigned int limit;
199 unsigned int seg_32bit : 1;
200 unsigned int contents : 2;
201 unsigned int read_exec_only : 1;
202 unsigned int limit_in_pages : 1;
203 unsigned int seg_not_present : 1;
204 unsigned int usable : 1;
205 unsigned int garbage : 25;
206 } thread_ldt = { -1, (unsigned long)thread_data, 0xfffff, 1, 0, 0, 1, 0, 1, 0 };
210 * The _start function is the entry and exit point of this program
212 * It calls wld_start, passing a pointer to the args it receives
213 * then jumps to the address wld_start returns.
215 void _start(void);
216 extern char _end[];
217 __ASM_GLOBAL_FUNC(_start,
218 __ASM_CFI("\t.cfi_undefined %eip\n")
219 "\tmovl $243,%eax\n" /* SYS_set_thread_area */
220 "\tmovl $thread_ldt,%ebx\n"
221 "\tint $0x80\n" /* allocate gs segment */
222 "\torl %eax,%eax\n"
223 "\tjl 1f\n"
224 "\tmovl thread_ldt,%eax\n" /* thread_ldt.entry_number */
225 "\tshl $3,%eax\n"
226 "\torl $3,%eax\n"
227 "\tmov %ax,%gs\n"
228 "\tmov %ax,%fs\n" /* set %fs too so libwine can retrieve it later on */
229 "1:\tmovl %esp,%eax\n"
230 "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */
231 "\tpushl %eax\n" /* orig stack pointer */
232 "\tpushl %esp\n" /* ptr to orig stack pointer */
233 "\tcall wld_start\n"
234 "\tpopl %ecx\n" /* remove ptr to stack pointer */
235 "\tpopl %esp\n" /* new stack pointer */
236 "\tpush %eax\n" /* ELF interpreter entry point */
237 "\txor %eax,%eax\n"
238 "\txor %ecx,%ecx\n"
239 "\txor %edx,%edx\n"
240 "\tmov %ax,%gs\n" /* clear %gs again */
241 "\tret\n")
243 /* wrappers for Linux system calls */
245 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
247 static inline __attribute__((noreturn)) void wld_exit( int code )
249 for (;;) /* avoid warning */
250 __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
251 : : "a" (1 /* SYS_exit */), "r" (code) );
254 static inline int wld_open( const char *name, int flags )
256 int ret;
257 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
258 : "=a" (ret) : "0" (5 /* SYS_open */), "r" (name), "c" (flags) );
259 return SYSCALL_RET(ret);
262 static inline int wld_close( int fd )
264 int ret;
265 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
266 : "=a" (ret) : "0" (6 /* SYS_close */), "r" (fd) );
267 return SYSCALL_RET(ret);
270 static inline ssize_t wld_read( int fd, void *buffer, size_t len )
272 int ret;
273 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
274 : "=a" (ret)
275 : "0" (3 /* SYS_read */), "r" (fd), "c" (buffer), "d" (len)
276 : "memory" );
277 return SYSCALL_RET(ret);
280 static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
282 int ret;
283 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
284 : "=a" (ret) : "0" (4 /* SYS_write */), "r" (fd), "c" (buffer), "d" (len) );
285 return SYSCALL_RET(ret);
288 static inline int wld_mprotect( const void *addr, size_t len, int prot )
290 int ret;
291 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
292 : "=a" (ret) : "0" (125 /* SYS_mprotect */), "r" (addr), "c" (len), "d" (prot) );
293 return SYSCALL_RET(ret);
296 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, unsigned int offset );
297 __ASM_GLOBAL_FUNC(wld_mmap,
298 "\tpushl %ebp\n"
299 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
300 "\tpushl %ebx\n"
301 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
302 "\tpushl %esi\n"
303 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
304 "\tpushl %edi\n"
305 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
306 "\tmovl $192,%eax\n" /* SYS_mmap2 */
307 "\tmovl 20(%esp),%ebx\n" /* start */
308 "\tmovl 24(%esp),%ecx\n" /* len */
309 "\tmovl 28(%esp),%edx\n" /* prot */
310 "\tmovl 32(%esp),%esi\n" /* flags */
311 "\tmovl 36(%esp),%edi\n" /* fd */
312 "\tmovl 40(%esp),%ebp\n" /* offset */
313 "\tshrl $12,%ebp\n"
314 "\tint $0x80\n"
315 "\tcmpl $-4096,%eax\n"
316 "\tjbe 2f\n"
317 "\tcmpl $-38,%eax\n" /* ENOSYS */
318 "\tjne 1f\n"
319 "\tmovl $90,%eax\n" /* SYS_mmap */
320 "\tleal 20(%esp),%ebx\n"
321 "\tint $0x80\n"
322 "\tcmpl $-4096,%eax\n"
323 "\tjbe 2f\n"
324 "1:\tmovl $-1,%eax\n"
325 "2:\tpopl %edi\n"
326 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
327 "\tpopl %esi\n"
328 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
329 "\tpopl %ebx\n"
330 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
331 "\tpopl %ebp\n"
332 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
333 "\tret\n" )
335 static inline int wld_prctl( int code, long arg )
337 int ret;
338 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
339 : "=a" (ret) : "0" (172 /* SYS_prctl */), "r" (code), "c" (arg) );
340 return SYSCALL_RET(ret);
343 #elif defined(__x86_64__)
345 void *thread_data[256];
348 * The _start function is the entry and exit point of this program
350 * It calls wld_start, passing a pointer to the args it receives
351 * then jumps to the address wld_start returns.
353 void _start(void);
354 extern char _end[];
355 __ASM_GLOBAL_FUNC(_start,
356 __ASM_CFI(".cfi_undefined %rip\n\t")
357 "movq %rsp,%rax\n\t"
358 "leaq -144(%rsp),%rsp\n\t" /* allocate some space for extra aux values */
359 "movq %rax,(%rsp)\n\t" /* orig stack pointer */
360 "movq $thread_data,%rsi\n\t"
361 "movq $0x1002,%rdi\n\t" /* ARCH_SET_FS */
362 "movq $158,%rax\n\t" /* SYS_arch_prctl */
363 "syscall\n\t"
364 "movq %rsp,%rdi\n\t" /* ptr to orig stack pointer */
365 "call wld_start\n\t"
366 "movq (%rsp),%rsp\n\t" /* new stack pointer */
367 "pushq %rax\n\t" /* ELF interpreter entry point */
368 "xorq %rax,%rax\n\t"
369 "xorq %rcx,%rcx\n\t"
370 "xorq %rdx,%rdx\n\t"
371 "xorq %rsi,%rsi\n\t"
372 "xorq %rdi,%rdi\n\t"
373 "xorq %r8,%r8\n\t"
374 "xorq %r9,%r9\n\t"
375 "xorq %r10,%r10\n\t"
376 "xorq %r11,%r11\n\t"
377 "ret")
379 #define SYSCALL_FUNC( name, nr ) \
380 __ASM_GLOBAL_FUNC( name, \
381 "movq $" #nr ",%rax\n\t" \
382 "movq %rcx,%r10\n\t" \
383 "syscall\n\t" \
384 "leaq 4096(%rax),%rcx\n\t" \
385 "movq $-1,%rdx\n\t" \
386 "cmp $4096,%rcx\n\t" \
387 "cmovb %rdx,%rax\n\t" \
388 "ret" )
390 #define SYSCALL_NOERR( name, nr ) \
391 __ASM_GLOBAL_FUNC( name, \
392 "movq $" #nr ",%rax\n\t" \
393 "syscall\n\t" \
394 "ret" )
396 void wld_exit( int code ) __attribute__((noreturn));
397 SYSCALL_NOERR( wld_exit, 60 /* SYS_exit */ );
399 ssize_t wld_read( int fd, void *buffer, size_t len );
400 SYSCALL_FUNC( wld_read, 0 /* SYS_read */ );
402 ssize_t wld_write( int fd, const void *buffer, size_t len );
403 SYSCALL_FUNC( wld_write, 1 /* SYS_write */ );
405 int wld_open( const char *name, int flags );
406 SYSCALL_FUNC( wld_open, 2 /* SYS_open */ );
408 int wld_close( int fd );
409 SYSCALL_FUNC( wld_close, 3 /* SYS_close */ );
411 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
412 SYSCALL_FUNC( wld_mmap, 9 /* SYS_mmap */ );
414 int wld_mprotect( const void *addr, size_t len, int prot );
415 SYSCALL_FUNC( wld_mprotect, 10 /* SYS_mprotect */ );
417 int wld_prctl( int code, long arg );
418 SYSCALL_FUNC( wld_prctl, 157 /* SYS_prctl */ );
420 uid_t wld_getuid(void);
421 SYSCALL_NOERR( wld_getuid, 102 /* SYS_getuid */ );
423 gid_t wld_getgid(void);
424 SYSCALL_NOERR( wld_getgid, 104 /* SYS_getgid */ );
426 uid_t wld_geteuid(void);
427 SYSCALL_NOERR( wld_geteuid, 107 /* SYS_geteuid */ );
429 gid_t wld_getegid(void);
430 SYSCALL_NOERR( wld_getegid, 108 /* SYS_getegid */ );
432 #elif defined(__aarch64__)
434 void *thread_data[256];
437 * The _start function is the entry and exit point of this program
439 * It calls wld_start, passing a pointer to the args it receives
440 * then jumps to the address wld_start returns.
442 void _start(void);
443 extern char _end[];
444 __ASM_GLOBAL_FUNC(_start,
445 "mov x0, SP\n\t"
446 "sub SP, SP, #144\n\t" /* allocate some space for extra aux values */
447 "str x0, [SP]\n\t" /* orig stack pointer */
448 "ldr x0, =thread_data\n\t"
449 "msr tpidr_el0, x0\n\t"
450 "mov x0, SP\n\t" /* ptr to orig stack pointer */
451 "bl wld_start\n\t"
452 "ldr x1, [SP]\n\t" /* new stack pointer */
453 "mov SP, x1\n\t"
454 "mov x30, x0\n\t"
455 "mov x0, #0\n\t"
456 "mov x1, #0\n\t"
457 "mov x2, #0\n\t"
458 "mov x3, #0\n\t"
459 "mov x4, #0\n\t"
460 "mov x5, #0\n\t"
461 "mov x6, #0\n\t"
462 "mov x7, #0\n\t"
463 "mov x8, #0\n\t"
464 "mov x9, #0\n\t"
465 "mov x10, #0\n\t"
466 "mov x11, #0\n\t"
467 "mov x12, #0\n\t"
468 "mov x13, #0\n\t"
469 "mov x14, #0\n\t"
470 "mov x15, #0\n\t"
471 "mov x16, #0\n\t"
472 "mov x17, #0\n\t"
473 "mov x18, #0\n\t"
474 "ret")
476 #define SYSCALL_FUNC( name, nr ) \
477 __ASM_GLOBAL_FUNC( name, \
478 "stp x8, x9, [SP, #-16]!\n\t" \
479 "mov x8, #" #nr "\n\t" \
480 "svc #0\n\t" \
481 "ldp x8, x9, [SP], #16\n\t" \
482 "cmn x0, #1, lsl#12\n\t" \
483 "cinv x0, x0, hi\n\t" \
484 "b.hi 1f\n\t" \
485 "ret\n\t" \
486 "1: mov x0, #-1\n\t" \
487 "ret" )
489 #define SYSCALL_NOERR( name, nr ) \
490 __ASM_GLOBAL_FUNC( name, \
491 "stp x8, x9, [SP, #-16]!\n\t" \
492 "mov x8, #" #nr "\n\t" \
493 "svc #0\n\t" \
494 "ldp x8, x9, [SP], #16\n\t" \
495 "ret" )
497 void wld_exit( int code ) __attribute__((noreturn));
498 SYSCALL_NOERR( wld_exit, 93 /* SYS_exit */ );
500 ssize_t wld_read( int fd, void *buffer, size_t len );
501 SYSCALL_FUNC( wld_read, 63 /* SYS_read */ );
503 ssize_t wld_write( int fd, const void *buffer, size_t len );
504 SYSCALL_FUNC( wld_write, 64 /* SYS_write */ );
506 int wld_openat( int dirfd, const char *name, int flags );
507 SYSCALL_FUNC( wld_openat, 56 /* SYS_openat */ );
509 int wld_open( const char *name, int flags )
511 return wld_openat(-100 /* AT_FDCWD */, name, flags);
514 int wld_close( int fd );
515 SYSCALL_FUNC( wld_close, 57 /* SYS_close */ );
517 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
518 SYSCALL_FUNC( wld_mmap, 222 /* SYS_mmap */ );
520 int wld_mprotect( const void *addr, size_t len, int prot );
521 SYSCALL_FUNC( wld_mprotect, 226 /* SYS_mprotect */ );
523 int wld_prctl( int code, long arg );
524 SYSCALL_FUNC( wld_prctl, 167 /* SYS_prctl */ );
526 uid_t wld_getuid(void);
527 SYSCALL_NOERR( wld_getuid, 174 /* SYS_getuid */ );
529 gid_t wld_getgid(void);
530 SYSCALL_NOERR( wld_getgid, 176 /* SYS_getgid */ );
532 uid_t wld_geteuid(void);
533 SYSCALL_NOERR( wld_geteuid, 175 /* SYS_geteuid */ );
535 gid_t wld_getegid(void);
536 SYSCALL_NOERR( wld_getegid, 177 /* SYS_getegid */ );
538 #else
539 #error preloader not implemented for this CPU
540 #endif
542 /* replacement for libc functions */
544 static int wld_strcmp( const char *str1, const char *str2 )
546 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
547 return *str1 - *str2;
550 static int wld_strncmp( const char *str1, const char *str2, size_t len )
552 if (len <= 0) return 0;
553 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
554 return *str1 - *str2;
557 static inline void *wld_memset( void *dest, int val, size_t len )
559 char *dst = dest;
560 while (len--) *dst++ = val;
561 return dest;
565 * wld_printf - just the basics
567 * %x prints a hex number
568 * %s prints a string
569 * %p prints a pointer
571 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
573 static const char hex_chars[16] = "0123456789abcdef";
574 const char *p = fmt;
575 char *str = buffer;
576 int i;
578 while( *p )
580 if( *p == '%' )
582 p++;
583 if( *p == 'x' )
585 unsigned int x = va_arg( args, unsigned int );
586 for (i = 2*sizeof(x) - 1; i >= 0; i--)
587 *str++ = hex_chars[(x>>(i*4))&0xf];
589 else if (p[0] == 'l' && p[1] == 'x')
591 unsigned long x = va_arg( args, unsigned long );
592 for (i = 2*sizeof(x) - 1; i >= 0; i--)
593 *str++ = hex_chars[(x>>(i*4))&0xf];
594 p++;
596 else if( *p == 'p' )
598 unsigned long x = (unsigned long)va_arg( args, void * );
599 for (i = 2*sizeof(x) - 1; i >= 0; i--)
600 *str++ = hex_chars[(x>>(i*4))&0xf];
602 else if( *p == 's' )
604 char *s = va_arg( args, char * );
605 while(*s)
606 *str++ = *s++;
608 else if( *p == 0 )
609 break;
610 p++;
612 *str++ = *p++;
614 *str = 0;
615 return str - buffer;
618 static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
620 va_list args;
621 char buffer[256];
622 int len;
624 va_start( args, fmt );
625 len = wld_vsprintf(buffer, fmt, args );
626 va_end( args );
627 wld_write(2, buffer, len);
630 static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
632 va_list args;
633 char buffer[256];
634 int len;
636 va_start( args, fmt );
637 len = wld_vsprintf(buffer, fmt, args );
638 va_end( args );
639 wld_write(2, buffer, len);
640 wld_exit(1);
643 #ifdef DUMP_AUX_INFO
645 * Dump interesting bits of the ELF auxv_t structure that is passed
646 * as the 4th parameter to the _start function
648 static void dump_auxiliary( struct wld_auxv *av )
650 #define NAME(at) { at, #at }
651 static const struct { int val; const char *name; } names[] =
653 NAME(AT_BASE),
654 NAME(AT_CLKTCK),
655 NAME(AT_EGID),
656 NAME(AT_ENTRY),
657 NAME(AT_EUID),
658 NAME(AT_FLAGS),
659 NAME(AT_GID),
660 NAME(AT_HWCAP),
661 NAME(AT_PAGESZ),
662 NAME(AT_PHDR),
663 NAME(AT_PHENT),
664 NAME(AT_PHNUM),
665 NAME(AT_PLATFORM),
666 NAME(AT_SYSINFO),
667 NAME(AT_SYSINFO_EHDR),
668 NAME(AT_UID),
669 { 0, NULL }
671 #undef NAME
673 int i;
675 for ( ; av->a_type != AT_NULL; av++)
677 for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
678 if (names[i].name) wld_printf("%s = %lx\n", names[i].name, (unsigned long)av->a_un.a_val);
679 else wld_printf( "%lx = %lx\n", (unsigned long)av->a_type, (unsigned long)av->a_un.a_val );
682 #endif
685 * set_auxiliary_values
687 * Set the new auxiliary values
689 static void set_auxiliary_values( struct wld_auxv *av, const struct wld_auxv *new_av,
690 const struct wld_auxv *delete_av, void **stack )
692 int i, j, av_count = 0, new_count = 0, delete_count = 0;
693 char *src, *dst;
695 /* count how many aux values we have already */
696 while (av[av_count].a_type != AT_NULL) av_count++;
698 /* delete unwanted values */
699 for (j = 0; delete_av[j].a_type != AT_NULL; j++)
701 for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
703 av[i].a_type = av[av_count-1].a_type;
704 av[i].a_un.a_val = av[av_count-1].a_un.a_val;
705 av[--av_count].a_type = AT_NULL;
706 delete_count++;
707 break;
711 /* count how many values we have in new_av that aren't in av */
712 for (j = 0; new_av[j].a_type != AT_NULL; j++)
714 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
715 if (i == av_count) new_count++;
718 src = (char *)*stack;
719 dst = src - (new_count - delete_count) * sizeof(*av);
720 dst = (char *)((unsigned long)dst & ~15);
721 if (dst < src) /* need to make room for the extra values */
723 int len = (char *)(av + av_count + 1) - src;
724 for (i = 0; i < len; i++) dst[i] = src[i];
726 else if (dst > src) /* get rid of unused values */
728 int len = (char *)(av + av_count + 1) - src;
729 for (i = len - 1; i >= 0; i--) dst[i] = src[i];
731 *stack = dst;
732 av = (struct wld_auxv *)((char *)av + (dst - src));
734 /* now set the values */
735 for (j = 0; new_av[j].a_type != AT_NULL; j++)
737 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
738 if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
739 else
741 av[av_count].a_type = new_av[j].a_type;
742 av[av_count].a_un.a_val = new_av[j].a_un.a_val;
743 av_count++;
747 #ifdef DUMP_AUX_INFO
748 wld_printf("New auxiliary info:\n");
749 dump_auxiliary( av );
750 #endif
754 * get_auxiliary
756 * Get a field of the auxiliary structure
758 static int get_auxiliary( struct wld_auxv *av, int type, int def_val )
760 for ( ; av->a_type != AT_NULL; av++)
761 if( av->a_type == type ) return av->a_un.a_val;
762 return def_val;
766 * map_so_lib
768 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
770 * This function maps the segments from an ELF object, and optionally
771 * stores information about the mapping into the auxv_t structure.
773 static void map_so_lib( const char *name, struct wld_link_map *l)
775 int fd;
776 unsigned char buf[0x800];
777 ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
778 ElfW(Phdr) *phdr, *ph;
779 /* Scan the program header table, collecting its load commands. */
780 struct loadcmd
782 ElfW(Addr) mapstart, mapend, dataend, allocend;
783 off_t mapoff;
784 int prot;
785 } loadcmds[16], *c;
786 size_t nloadcmds = 0, maplength;
788 fd = wld_open( name, O_RDONLY );
789 if (fd == -1) fatal_error("%s: could not open\n", name );
791 if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
792 fatal_error("%s: failed to read ELF header\n", name);
794 phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
796 if( ( header->e_ident[0] != 0x7f ) ||
797 ( header->e_ident[1] != 'E' ) ||
798 ( header->e_ident[2] != 'L' ) ||
799 ( header->e_ident[3] != 'F' ) )
800 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
802 #ifdef __i386__
803 if( header->e_machine != EM_386 )
804 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
805 #elif defined(__x86_64__)
806 if( header->e_machine != EM_X86_64 )
807 fatal_error("%s: not an x86-64 ELF binary... don't know how to load it\n", name );
808 #elif defined(__aarch64__)
809 if( header->e_machine != EM_AARCH64 )
810 fatal_error("%s: not an aarch64 ELF binary... don't know how to load it\n", name );
811 #endif
813 if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
814 fatal_error( "%s: oops... not enough space for load commands\n", name );
816 maplength = header->e_phnum * sizeof (ElfW(Phdr));
817 if (header->e_phoff + maplength > sizeof(buf))
818 fatal_error( "%s: oops... not enough space for ELF headers\n", name );
820 l->l_ld = 0;
821 l->l_addr = 0;
822 l->l_phdr = 0;
823 l->l_phnum = header->e_phnum;
824 l->l_entry = header->e_entry;
825 l->l_interp = 0;
827 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
830 #ifdef DUMP_SEGMENTS
831 wld_printf( "ph = %p\n", ph );
832 wld_printf( " p_type = %lx\n", (unsigned long)ph->p_type );
833 wld_printf( " p_flags = %lx\n", (unsigned long)ph->p_flags );
834 wld_printf( " p_offset = %lx\n", (unsigned long)ph->p_offset );
835 wld_printf( " p_vaddr = %lx\n", (unsigned long)ph->p_vaddr );
836 wld_printf( " p_paddr = %lx\n", (unsigned long)ph->p_paddr );
837 wld_printf( " p_filesz = %lx\n", (unsigned long)ph->p_filesz );
838 wld_printf( " p_memsz = %lx\n", (unsigned long)ph->p_memsz );
839 wld_printf( " p_align = %lx\n", (unsigned long)ph->p_align );
840 #endif
842 switch (ph->p_type)
844 /* These entries tell us where to find things once the file's
845 segments are mapped in. We record the addresses it says
846 verbatim, and later correct for the run-time load address. */
847 case PT_DYNAMIC:
848 l->l_ld = (void *) ph->p_vaddr;
849 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
850 break;
852 case PT_PHDR:
853 l->l_phdr = (void *) ph->p_vaddr;
854 break;
856 case PT_LOAD:
858 if ((ph->p_align & page_mask) != 0)
859 fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
861 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
862 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
864 c = &loadcmds[nloadcmds++];
865 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
866 c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
867 c->dataend = ph->p_vaddr + ph->p_filesz;
868 c->allocend = ph->p_vaddr + ph->p_memsz;
869 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
871 c->prot = 0;
872 if (ph->p_flags & PF_R)
873 c->prot |= PROT_READ;
874 if (ph->p_flags & PF_W)
875 c->prot |= PROT_WRITE;
876 if (ph->p_flags & PF_X)
877 c->prot |= PROT_EXEC;
879 break;
881 case PT_INTERP:
882 l->l_interp = ph->p_vaddr;
883 break;
885 case PT_TLS:
887 * We don't need to set anything up because we're
888 * emulating the kernel, not ld-linux.so.2
889 * The ELF loader will set up the TLS data itself.
891 case PT_SHLIB:
892 case PT_NOTE:
893 default:
894 break;
898 /* Now process the load commands and map segments into memory. */
899 if (!nloadcmds)
900 fatal_error( "%s: no segments to load\n", name );
901 c = loadcmds;
903 /* Length of the sections to be loaded. */
904 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
906 if( header->e_type == ET_DYN )
908 ElfW(Addr) mappref;
909 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
910 - MAP_BASE_ADDR (l));
912 /* Remember which part of the address space this object uses. */
913 l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
914 c->prot, MAP_COPY | MAP_FILE,
915 fd, c->mapoff);
916 /* wld_printf("set : offset = %x\n", c->mapoff); */
917 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
919 l->l_map_end = l->l_map_start + maplength;
920 l->l_addr = l->l_map_start - c->mapstart;
922 wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
923 loadcmds[nloadcmds - 1].allocend - c->mapend,
924 PROT_NONE);
925 goto postmap;
927 else
929 /* sanity check */
930 if ((char *)c->mapstart + maplength > preloader_start &&
931 (char *)c->mapstart <= preloader_end)
932 fatal_error( "%s: binary overlaps preloader (%p-%p)\n",
933 name, (char *)c->mapstart, (char *)c->mapstart + maplength );
935 ELF_FIXED_ADDRESS (loader, c->mapstart);
938 /* Remember which part of the address space this object uses. */
939 l->l_map_start = c->mapstart + l->l_addr;
940 l->l_map_end = l->l_map_start + maplength;
942 while (c < &loadcmds[nloadcmds])
944 if (c->mapend > c->mapstart)
945 /* Map the segment contents from the file. */
946 wld_mmap ((void *) (l->l_addr + c->mapstart),
947 c->mapend - c->mapstart, c->prot,
948 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
950 postmap:
951 if (l->l_phdr == 0
952 && (ElfW(Off)) c->mapoff <= header->e_phoff
953 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
954 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
955 /* Found the program header in this segment. */
956 l->l_phdr = (void *)(unsigned long)(c->mapstart + header->e_phoff - c->mapoff);
958 if (c->allocend > c->dataend)
960 /* Extra zero pages should appear at the end of this segment,
961 after the data mapped from the file. */
962 ElfW(Addr) zero, zeroend, zeropage;
964 zero = l->l_addr + c->dataend;
965 zeroend = l->l_addr + c->allocend;
966 zeropage = (zero + page_mask) & ~page_mask;
969 * This is different from the dl-load load...
970 * ld-linux.so.2 relies on the whole page being zero'ed
972 zeroend = (zeroend + page_mask) & ~page_mask;
974 if (zeroend < zeropage)
976 /* All the extra data is in the last page of the segment.
977 We can just zero it. */
978 zeropage = zeroend;
981 if (zeropage > zero)
983 /* Zero the final part of the last page of the segment. */
984 if ((c->prot & PROT_WRITE) == 0)
986 /* Dag nab it. */
987 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
989 wld_memset ((void *) zero, '\0', zeropage - zero);
990 if ((c->prot & PROT_WRITE) == 0)
991 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
994 if (zeroend > zeropage)
996 /* Map the remaining zero pages in from the zero fill FD. */
997 wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
998 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
999 -1, 0);
1003 ++c;
1006 if (l->l_phdr == NULL) fatal_error("no program header\n");
1008 l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
1009 l->l_entry += l->l_addr;
1011 wld_close( fd );
1015 static unsigned int wld_elf_hash( const char *name )
1017 unsigned int hi, hash = 0;
1018 while (*name)
1020 hash = (hash << 4) + (unsigned char)*name++;
1021 hi = hash & 0xf0000000;
1022 hash ^= hi;
1023 hash ^= hi >> 24;
1025 return hash;
1028 static unsigned int gnu_hash( const char *name )
1030 unsigned int h = 5381;
1031 while (*name) h = h * 33 + (unsigned char)*name++;
1032 return h;
1036 * Find a symbol in the symbol table of the executable loaded
1038 static void *find_symbol( const struct wld_link_map *map, const char *var, int type )
1040 const ElfW(Dyn) *dyn = NULL;
1041 const ElfW(Phdr) *ph;
1042 const ElfW(Sym) *symtab = NULL;
1043 const Elf32_Word *hashtab = NULL;
1044 const Elf32_Word *gnu_hashtab = NULL;
1045 const char *strings = NULL;
1046 Elf32_Word idx;
1048 /* check the values */
1049 #ifdef DUMP_SYMS
1050 wld_printf("%p %x\n", map->l_phdr, map->l_phnum );
1051 #endif
1052 /* parse the (already loaded) ELF executable's header */
1053 for (ph = map->l_phdr; ph < &map->l_phdr[map->l_phnum]; ++ph)
1055 if( PT_DYNAMIC == ph->p_type )
1057 dyn = (void *)(ph->p_vaddr + map->l_addr);
1058 break;
1061 if( !dyn ) return NULL;
1063 while( dyn->d_tag )
1065 if( dyn->d_tag == DT_STRTAB )
1066 strings = (const char*)(dyn->d_un.d_ptr + map->l_addr);
1067 if( dyn->d_tag == DT_SYMTAB )
1068 symtab = (const ElfW(Sym) *)(dyn->d_un.d_ptr + map->l_addr);
1069 if( dyn->d_tag == DT_HASH )
1070 hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
1071 if( dyn->d_tag == DT_GNU_HASH )
1072 gnu_hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
1073 #ifdef DUMP_SYMS
1074 wld_printf("%lx %p\n", (unsigned long)dyn->d_tag, (void *)dyn->d_un.d_ptr );
1075 #endif
1076 dyn++;
1079 if( (!symtab) || (!strings) ) return NULL;
1081 if (gnu_hashtab) /* new style hash table */
1083 const unsigned int hash = gnu_hash(var);
1084 const Elf32_Word nbuckets = gnu_hashtab[0];
1085 const Elf32_Word symbias = gnu_hashtab[1];
1086 const Elf32_Word nwords = gnu_hashtab[2];
1087 const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
1088 const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
1089 const Elf32_Word *chains = buckets + nbuckets - symbias;
1091 if (!(idx = buckets[hash % nbuckets])) return NULL;
1094 if ((chains[idx] & ~1u) == (hash & ~1u) &&
1095 ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
1096 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
1097 !wld_strcmp( strings + symtab[idx].st_name, var ))
1098 goto found;
1099 } while (!(chains[idx++] & 1u));
1101 else if (hashtab) /* old style hash table */
1103 const unsigned int hash = wld_elf_hash(var);
1104 const Elf32_Word nbuckets = hashtab[0];
1105 const Elf32_Word *buckets = hashtab + 2;
1106 const Elf32_Word *chains = buckets + nbuckets;
1108 for (idx = buckets[hash % nbuckets]; idx; idx = chains[idx])
1110 if (ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
1111 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
1112 !wld_strcmp( strings + symtab[idx].st_name, var ))
1113 goto found;
1116 return NULL;
1118 found:
1119 #ifdef DUMP_SYMS
1120 wld_printf("Found %s -> %p\n", strings + symtab[idx].st_name, (void *)symtab[idx].st_value );
1121 #endif
1122 return (void *)(symtab[idx].st_value + map->l_addr);
1126 * preload_reserve
1128 * Reserve a range specified in string format
1130 static void preload_reserve( const char *str )
1132 const char *p;
1133 unsigned long result = 0;
1134 void *start = NULL, *end = NULL;
1135 int i, first = 1;
1137 for (p = str; *p; p++)
1139 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
1140 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
1141 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
1142 else if (*p == '-')
1144 if (!first) goto error;
1145 start = (void *)(result & ~page_mask);
1146 result = 0;
1147 first = 0;
1149 else goto error;
1151 if (!first) end = (void *)((result + page_mask) & ~page_mask);
1152 else if (result) goto error; /* single value '0' is allowed */
1154 /* sanity checks */
1155 if (end <= start) start = end = NULL;
1156 else if ((char *)end > preloader_start &&
1157 (char *)start <= preloader_end)
1159 wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %p-%p\n",
1160 start, end, preloader_start, preloader_end );
1161 start = end = NULL;
1164 /* check for overlap with low memory areas */
1165 for (i = 0; preload_info[i].size; i++)
1167 if ((char *)preload_info[i].addr > (char *)0x00110000) break;
1168 if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size)
1170 start = end = NULL;
1171 break;
1173 if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size)
1174 start = (char *)preload_info[i].addr + preload_info[i].size;
1177 while (preload_info[i].size) i++;
1178 preload_info[i].addr = start;
1179 preload_info[i].size = (char *)end - (char *)start;
1180 return;
1182 error:
1183 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
1186 /* check if address is in one of the reserved ranges */
1187 static int is_addr_reserved( const void *addr )
1189 int i;
1191 for (i = 0; preload_info[i].size; i++)
1193 if ((const char *)addr >= (const char *)preload_info[i].addr &&
1194 (const char *)addr < (const char *)preload_info[i].addr + preload_info[i].size)
1195 return 1;
1197 return 0;
1200 /* remove a range from the preload list */
1201 static void remove_preload_range( int i )
1203 while (preload_info[i].size)
1205 preload_info[i].addr = preload_info[i+1].addr;
1206 preload_info[i].size = preload_info[i+1].size;
1207 i++;
1212 * is_in_preload_range
1214 * Check if address of the given aux value is in one of the reserved ranges
1216 static int is_in_preload_range( const struct wld_auxv *av, int type )
1218 while (av->a_type != AT_NULL)
1220 if (av->a_type == type) return is_addr_reserved( (const void *)av->a_un.a_val );
1221 av++;
1223 return 0;
1226 /* set the process name if supported */
1227 static void set_process_name( int argc, char *argv[] )
1229 int i;
1230 unsigned int off;
1231 char *p, *name, *end;
1233 /* set the process short name */
1234 for (p = name = argv[1]; *p; p++) if (p[0] == '/' && p[1]) name = p + 1;
1235 if (wld_prctl( 15 /* PR_SET_NAME */, (long)name ) == -1) return;
1237 /* find the end of the argv array and move everything down */
1238 end = argv[argc - 1];
1239 while (*end) end++;
1240 off = argv[1] - argv[0];
1241 for (p = argv[1]; p <= end; p++) *(p - off) = *p;
1242 wld_memset( end - off, 0, off );
1243 for (i = 1; i < argc; i++) argv[i] -= off;
1248 * wld_start
1250 * Repeat the actions the kernel would do when loading a dynamically linked .so
1251 * Load the binary and then its ELF interpreter.
1252 * Note, we assume that the binary is a dynamically linked ELF shared object.
1254 void* wld_start( void **stack )
1256 long i, *pargc;
1257 char **argv, **p;
1258 char *interp, *reserve = NULL;
1259 struct wld_auxv new_av[8], delete_av[3], *av;
1260 struct wld_link_map main_binary_map, ld_so_map;
1261 struct wine_preload_info **wine_main_preload_info;
1263 pargc = *stack;
1264 argv = (char **)pargc + 1;
1265 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
1267 /* skip over the parameters */
1268 p = argv + *pargc + 1;
1270 /* skip over the environment */
1271 while (*p)
1273 static const char res[] = "WINEPRELOADRESERVE=";
1274 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
1275 p++;
1278 av = (struct wld_auxv *)(p+1);
1279 page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
1280 page_mask = page_size - 1;
1282 preloader_start = (char *)_start - ((unsigned long)_start & page_mask);
1283 preloader_end = (char *)((unsigned long)(_end + page_mask) & ~page_mask);
1285 #ifdef DUMP_AUX_INFO
1286 wld_printf( "stack = %p\n", *stack );
1287 for( i = 0; i < *pargc; i++ ) wld_printf("argv[%lx] = %s\n", i, argv[i]);
1288 dump_auxiliary( av );
1289 #endif
1291 /* reserve memory that Wine needs */
1292 if (reserve) preload_reserve( reserve );
1293 for (i = 0; preload_info[i].size; i++)
1295 if ((char *)av >= (char *)preload_info[i].addr &&
1296 (char *)pargc <= (char *)preload_info[i].addr + preload_info[i].size)
1298 remove_preload_range( i );
1299 i--;
1301 else if (wld_mmap( preload_info[i].addr, preload_info[i].size, PROT_NONE,
1302 MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 ) == (void *)-1)
1304 /* don't warn for low 64k */
1305 if (preload_info[i].addr >= (void *)0x10000
1306 #ifdef __aarch64__
1307 && preload_info[i].addr < (void *)0x7fffffffff /* ARM64 address space might end here*/
1308 #endif
1310 wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
1311 preload_info[i].addr, (char *)preload_info[i].addr + preload_info[i].size );
1312 remove_preload_range( i );
1313 i--;
1317 /* add an executable page at the top of the address space to defeat
1318 * broken no-exec protections that play with the code selector limit */
1319 if (is_addr_reserved( (char *)0x80000000 - page_size ))
1320 wld_mprotect( (char *)0x80000000 - page_size, page_size, PROT_EXEC | PROT_READ );
1322 /* load the main binary */
1323 map_so_lib( argv[1], &main_binary_map );
1325 /* load the ELF interpreter */
1326 interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
1327 map_so_lib( interp, &ld_so_map );
1329 /* store pointer to the preload info into the appropriate main binary variable */
1330 wine_main_preload_info = find_symbol( &main_binary_map, "wine_main_preload_info", STT_OBJECT );
1331 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
1332 else wld_printf( "wine_main_preload_info not found\n" );
1334 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
1335 SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
1336 SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
1337 SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
1338 SET_NEW_AV( 3, AT_PAGESZ, page_size );
1339 SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
1340 SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
1341 SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
1342 SET_NEW_AV( 7, AT_NULL, 0 );
1343 #undef SET_NEW_AV
1345 i = 0;
1346 /* delete sysinfo values if addresses conflict */
1347 if (is_in_preload_range( av, AT_SYSINFO ) || is_in_preload_range( av, AT_SYSINFO_EHDR ))
1349 delete_av[i++].a_type = AT_SYSINFO;
1350 delete_av[i++].a_type = AT_SYSINFO_EHDR;
1352 delete_av[i].a_type = AT_NULL;
1354 /* get rid of first argument */
1355 set_process_name( *pargc, argv );
1356 pargc[1] = pargc[0] - 1;
1357 *stack = pargc + 1;
1359 set_auxiliary_values( av, new_av, delete_av, stack );
1361 #ifdef DUMP_AUX_INFO
1362 wld_printf("new stack = %p\n", *stack);
1363 wld_printf("jumping to %p\n", (void *)ld_so_map.l_entry);
1364 #endif
1365 #ifdef DUMP_MAPS
1367 char buffer[1024];
1368 int len, fd = wld_open( "/proc/self/maps", O_RDONLY );
1369 if (fd != -1)
1371 while ((len = wld_read( fd, buffer, sizeof(buffer) )) > 0) wld_write( 2, buffer, len );
1372 wld_close( fd );
1375 #endif
1377 return (void *)ld_so_map.l_entry;
1380 #endif /* __linux__ */