Forward CLSID_FilterGraphNoThread to CLSID_FilterGraph.
[wine/hacks.git] / loader / preloader.c
bloba10558bc92958c0fc1456e20cef9914605683e9f
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Design notes
26 * The goal of this program is to be a workaround for exec-shield, as used
27 * by the Linux kernel distributed with Fedora Core and other distros.
29 * To do this, we implement our own shared object loader that reserves memory
30 * that is important to Wine, and then loads the main binary and its ELF
31 * interpreter.
33 * We will try to set up the stack and memory area so that the program that
34 * loads after us (eg. the wine binary) never knows we were here, except that
35 * areas of memory it needs are already magically reserved.
37 * The following memory areas are important to Wine:
38 * 0x00000000 - 0x00110000 the DOS area
39 * 0x80000000 - 0x81000000 the shared heap
40 * ??? - ??? the PE binary load address (usually starting at 0x00400000)
42 * If this program is used as the shared object loader, the only difference
43 * that the loaded programs should see is that this loader will be mapped
44 * into memory when it starts.
48 * References (things I consulted to understand how ELF loading works):
50 * glibc 2.3.2 elf/dl-load.c
51 * http://www.gnu.org/directory/glibc.html
53 * Linux 2.6.4 fs/binfmt_elf.c
54 * ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.4.tar.bz2
56 * Userland exec, by <grugq@hcunix.net>
57 * http://cert.uni-stuttgart.de/archive/bugtraq/2004/01/msg00002.html
59 * The ELF specification:
60 * http://www.linuxbase.org/spec/booksets/LSB-Embedded/LSB-Embedded/book387.html
63 #include "config.h"
64 #include "wine/port.h"
66 #include <stdarg.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 #include <fcntl.h>
73 #ifdef HAVE_SYS_MMAN_H
74 # include <sys/mman.h>
75 #endif
76 #ifdef HAVE_SYS_SYSCALL_H
77 # include <sys/syscall.h>
78 #endif
79 #ifdef HAVE_UNISTD_H
80 # include <unistd.h>
81 #endif
82 #ifdef HAVE_ELF_H
83 # include <elf.h>
84 #endif
85 #ifdef HAVE_LINK_H
86 # include <link.h>
87 #endif
88 #ifdef HAVE_SYS_LINK_H
89 # include <sys/link.h>
90 #endif
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 { (void *)0x00000000, 0x00110000 }, /* DOS area */
110 { (void *)0x80000000, 0x01000000 }, /* shared heap */
111 { (void *)0x00110000, 0x1fef0000 }, /* PE exe range (may be set with WINEPRELOADRESERVE), defaults to 512mb */
112 { 0, 0 } /* end of list */
115 /* debugging */
116 #undef DUMP_SEGMENTS
117 #undef DUMP_AUX_INFO
118 #undef DUMP_SYMS
120 /* older systems may not define these */
121 #ifndef PT_TLS
122 #define PT_TLS 7
123 #endif
125 #ifndef AT_SYSINFO
126 #define AT_SYSINFO 32
127 #endif
128 #ifndef AT_SYSINFO_EHDR
129 #define AT_SYSINFO_EHDR 33
130 #endif
132 static unsigned int page_size, page_mask;
133 static char *preloader_start, *preloader_end;
135 struct wld_link_map {
136 ElfW(Addr) l_addr;
137 ElfW(Dyn) *l_ld;
138 ElfW(Phdr)*l_phdr;
139 ElfW(Addr) l_entry;
140 ElfW(Half) l_ldnum;
141 ElfW(Half) l_phnum;
142 ElfW(Addr) l_map_start, l_map_end;
143 ElfW(Addr) l_interp;
148 * The __bb_init_func is an empty function only called when file is
149 * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
150 * function is normally provided by libc's startup files, but since we
151 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
152 * provide our own (empty) version, otherwise linker fails.
154 void __bb_init_func() { return; }
158 * The _start function is the entry and exit point of this program
160 * It calls wld_start, passing a pointer to the args it receives
161 * then jumps to the address wld_start returns.
163 void _start();
164 extern char _end[];
165 __ASM_GLOBAL_FUNC(_start,
166 "\tmovl %esp,%eax\n"
167 "\tleal -128(%esp),%esp\n" /* allocate some space for extra aux values */
168 "\tpushl %eax\n" /* orig stack pointer */
169 "\tpushl %esp\n" /* ptr to orig stack pointer */
170 "\tcall wld_start\n"
171 "\tpopl %ecx\n" /* remove ptr to stack pointer */
172 "\tpopl %esp\n" /* new stack pointer */
173 "\tpush %eax\n" /* ELF interpreter entry point */
174 "\txor %eax,%eax\n"
175 "\txor %ecx,%ecx\n"
176 "\txor %edx,%edx\n"
177 "\tret\n")
179 /* wrappers for Linux system calls */
181 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
183 static inline __attribute__((noreturn)) void wld_exit( int code )
185 for (;;) /* avoid warning */
186 __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
187 : : "a" (SYS_exit), "r" (code) );
190 static inline int wld_open( const char *name, int flags )
192 int ret;
193 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
194 : "=a" (ret) : "0" (SYS_open), "r" (name), "c" (flags) );
195 return SYSCALL_RET(ret);
198 static inline int wld_close( int fd )
200 int ret;
201 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
202 : "=a" (ret) : "0" (SYS_close), "r" (fd) );
203 return SYSCALL_RET(ret);
206 static inline ssize_t wld_read( int fd, void *buffer, size_t len )
208 int ret;
209 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
210 : "=a" (ret)
211 : "0" (SYS_read), "r" (fd), "c" (buffer), "d" (len)
212 : "memory" );
213 return SYSCALL_RET(ret);
216 static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
218 int ret;
219 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
220 : "=a" (ret) : "0" (SYS_write), "r" (fd), "c" (buffer), "d" (len) );
221 return SYSCALL_RET(ret);
224 static inline int wld_mprotect( const void *addr, size_t len, int prot )
226 int ret;
227 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
228 : "=a" (ret) : "0" (SYS_mprotect), "r" (addr), "c" (len), "d" (prot) );
229 return SYSCALL_RET(ret);
232 static void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset )
234 int ret;
236 struct
238 void *addr;
239 unsigned int length;
240 unsigned int prot;
241 unsigned int flags;
242 unsigned int fd;
243 unsigned int offset;
244 } args;
246 args.addr = start;
247 args.length = len;
248 args.prot = prot;
249 args.flags = flags;
250 args.fd = fd;
251 args.offset = offset;
252 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
253 : "=a" (ret) : "0" (SYS_mmap), "q" (&args) : "memory" );
254 return (void *)SYSCALL_RET(ret);
257 static inline uid_t wld_getuid(void)
259 uid_t ret;
260 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getuid) );
261 return ret;
264 static inline uid_t wld_geteuid(void)
266 uid_t ret;
267 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_geteuid) );
268 return ret;
271 static inline gid_t wld_getgid(void)
273 gid_t ret;
274 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getgid) );
275 return ret;
278 static inline gid_t wld_getegid(void)
280 gid_t ret;
281 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getegid) );
282 return ret;
286 /* replacement for libc functions */
288 static int wld_strcmp( const char *str1, const char *str2 )
290 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
291 return *str1 - *str2;
294 static int wld_strncmp( const char *str1, const char *str2, size_t len )
296 if (len <= 0) return 0;
297 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
298 return *str1 - *str2;
301 static inline void *wld_memset( void *dest, int val, size_t len )
303 char *dst = dest;
304 while (len--) *dst++ = val;
305 return dest;
309 * wld_printf - just the basics
311 * %x prints a hex number
312 * %s prints a string
314 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
316 static const char hex_chars[16] = "0123456789abcdef";
317 const char *p = fmt;
318 char *str = buffer;
320 while( *p )
322 if( *p == '%' )
324 p++;
325 if( *p == 'x' )
327 int i;
328 unsigned int x = va_arg( args, unsigned int );
329 for(i=7; i>=0; i--)
330 *str++ = hex_chars[(x>>(i*4))&0xf];
332 else if( *p == 's' )
334 char *s = va_arg( args, char * );
335 while(*s)
336 *str++ = *s++;
338 else if( *p == 0 )
339 break;
340 p++;
342 *str++ = *p++;
344 *str = 0;
345 return str - buffer;
348 static void wld_printf(const char *fmt, ... )
350 va_list args;
351 char buffer[256];
352 int len;
354 va_start( args, fmt );
355 len = wld_vsprintf(buffer, fmt, args );
356 va_end( args );
357 wld_write(2, buffer, len);
360 static __attribute__((noreturn)) void fatal_error(const char *fmt, ... )
362 va_list args;
363 char buffer[256];
364 int len;
366 va_start( args, fmt );
367 len = wld_vsprintf(buffer, fmt, args );
368 va_end( args );
369 wld_write(2, buffer, len);
370 wld_exit(1);
373 #ifdef DUMP_AUX_INFO
375 * Dump interesting bits of the ELF auxv_t structure that is passed
376 * as the 4th parameter to the _start function
378 static void dump_auxiliary( ElfW(auxv_t) *av )
380 #define NAME(at) { at, #at }
381 static const struct { int val; const char *name; } names[] =
383 NAME(AT_BASE),
384 NAME(AT_CLKTCK),
385 NAME(AT_EGID),
386 NAME(AT_ENTRY),
387 NAME(AT_EUID),
388 NAME(AT_FLAGS),
389 NAME(AT_GID),
390 NAME(AT_HWCAP),
391 NAME(AT_PAGESZ),
392 NAME(AT_PHDR),
393 NAME(AT_PHENT),
394 NAME(AT_PHNUM),
395 NAME(AT_PLATFORM),
396 NAME(AT_SYSINFO),
397 NAME(AT_SYSINFO_EHDR),
398 NAME(AT_UID),
399 { 0, NULL }
401 #undef NAME
403 int i;
405 for ( ; av->a_type != AT_NULL; av++)
407 for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
408 if (names[i].name) wld_printf("%s = %x\n", names[i].name, av->a_un.a_val);
409 else wld_printf( "%x = %x\n", av->a_type, av->a_un.a_val );
412 #endif
415 * set_auxiliary_values
417 * Set the new auxiliary values
419 static void set_auxiliary_values( ElfW(auxv_t) *av, const ElfW(auxv_t) *new_av,
420 const ElfW(auxv_t) *delete_av, void **stack )
422 int i, j, av_count = 0, new_count = 0, delete_count = 0;
423 char *src, *dst;
425 /* count how many aux values we have already */
426 while (av[av_count].a_type != AT_NULL) av_count++;
428 /* delete unwanted values */
429 for (j = 0; delete_av[j].a_type != AT_NULL; j++)
431 for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
433 av[i].a_type = av[av_count-1].a_type;
434 av[i].a_un.a_val = av[av_count-1].a_un.a_val;
435 av[--av_count].a_type = AT_NULL;
436 delete_count++;
437 break;
441 /* count how many values we have in new_av that aren't in av */
442 for (j = 0; new_av[j].a_type != AT_NULL; j++)
444 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
445 if (i == av_count) new_count++;
448 src = (char *)*stack;
449 dst = src - (new_count - delete_count) * sizeof(*av);
450 if (new_count > delete_count) /* need to make room for the extra values */
452 int len = (char *)(av + av_count + 1) - src;
453 for (i = 0; i < len; i++) dst[i] = src[i];
455 else if (new_count < delete_count) /* get rid of unused values */
457 int len = (char *)(av + av_count + 1) - dst;
458 for (i = len - 1; i >= 0; i--) dst[i] = src[i];
460 *stack = dst;
461 av -= (new_count - delete_count);
463 /* now set the values */
464 for (j = 0; new_av[j].a_type != AT_NULL; j++)
466 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
467 if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
468 else
470 av[av_count].a_type = new_av[j].a_type;
471 av[av_count].a_un.a_val = new_av[j].a_un.a_val;
472 av_count++;
476 #ifdef DUMP_AUX_INFO
477 wld_printf("New auxiliary info:\n");
478 dump_auxiliary( av );
479 #endif
483 * get_auxiliary
485 * Get a field of the auxiliary structure
487 static int get_auxiliary( ElfW(auxv_t) *av, int type, int def_val )
489 for ( ; av->a_type != AT_NULL; av++)
490 if( av->a_type == type ) return av->a_un.a_val;
491 return def_val;
495 * map_so_lib
497 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
499 * This function maps the segments from an ELF object, and optionally
500 * stores information about the mapping into the auxv_t structure.
502 static void map_so_lib( const char *name, struct wld_link_map *l)
504 int fd;
505 unsigned char buf[0x800];
506 ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
507 ElfW(Phdr) *phdr, *ph;
508 /* Scan the program header table, collecting its load commands. */
509 struct loadcmd
511 ElfW(Addr) mapstart, mapend, dataend, allocend;
512 off_t mapoff;
513 int prot;
514 } loadcmds[16], *c;
515 size_t nloadcmds = 0, maplength;
517 fd = wld_open( name, O_RDONLY );
518 if (fd == -1) fatal_error("%s: could not open\n", name );
520 if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
521 fatal_error("%s: failed to read ELF header\n", name);
523 phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
525 if( ( header->e_ident[0] != 0x7f ) ||
526 ( header->e_ident[1] != 'E' ) ||
527 ( header->e_ident[2] != 'L' ) ||
528 ( header->e_ident[3] != 'F' ) )
529 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
531 if( header->e_machine != EM_386 )
532 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
534 if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
535 fatal_error( "%s: oops... not enough space for load commands\n", name );
537 maplength = header->e_phnum * sizeof (ElfW(Phdr));
538 if (header->e_phoff + maplength > sizeof(buf))
539 fatal_error( "%s: oops... not enough space for ELF headers\n", name );
541 l->l_ld = 0;
542 l->l_addr = 0;
543 l->l_phdr = 0;
544 l->l_phnum = header->e_phnum;
545 l->l_entry = header->e_entry;
546 l->l_interp = 0;
548 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
551 #ifdef DUMP_SEGMENTS
552 wld_printf( "ph = %x\n", ph );
553 wld_printf( " p_type = %x\n", ph->p_type );
554 wld_printf( " p_flags = %x\n", ph->p_flags );
555 wld_printf( " p_offset = %x\n", ph->p_offset );
556 wld_printf( " p_vaddr = %x\n", ph->p_vaddr );
557 wld_printf( " p_paddr = %x\n", ph->p_paddr );
558 wld_printf( " p_filesz = %x\n", ph->p_filesz );
559 wld_printf( " p_memsz = %x\n", ph->p_memsz );
560 wld_printf( " p_align = %x\n", ph->p_align );
561 #endif
563 switch (ph->p_type)
565 /* These entries tell us where to find things once the file's
566 segments are mapped in. We record the addresses it says
567 verbatim, and later correct for the run-time load address. */
568 case PT_DYNAMIC:
569 l->l_ld = (void *) ph->p_vaddr;
570 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
571 break;
573 case PT_PHDR:
574 l->l_phdr = (void *) ph->p_vaddr;
575 break;
577 case PT_LOAD:
579 if ((ph->p_align & page_mask) != 0)
580 fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
582 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
583 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
585 c = &loadcmds[nloadcmds++];
586 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
587 c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
588 c->dataend = ph->p_vaddr + ph->p_filesz;
589 c->allocend = ph->p_vaddr + ph->p_memsz;
590 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
592 c->prot = 0;
593 if (ph->p_flags & PF_R)
594 c->prot |= PROT_READ;
595 if (ph->p_flags & PF_W)
596 c->prot |= PROT_WRITE;
597 if (ph->p_flags & PF_X)
598 c->prot |= PROT_EXEC;
600 break;
602 case PT_INTERP:
603 l->l_interp = ph->p_vaddr;
604 break;
606 case PT_TLS:
608 * We don't need to set anything up because we're
609 * emulating the kernel, not ld-linux.so.2
610 * The ELF loader will set up the TLS data itself.
612 case PT_SHLIB:
613 case PT_NOTE:
614 default:
615 break;
619 /* Now process the load commands and map segments into memory. */
620 c = loadcmds;
622 /* Length of the sections to be loaded. */
623 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
625 if( header->e_type == ET_DYN )
627 ElfW(Addr) mappref;
628 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
629 - MAP_BASE_ADDR (l));
631 /* Remember which part of the address space this object uses. */
632 l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
633 c->prot, MAP_COPY | MAP_FILE,
634 fd, c->mapoff);
635 /* wld_printf("set : offset = %x\n", c->mapoff); */
636 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
638 l->l_map_end = l->l_map_start + maplength;
639 l->l_addr = l->l_map_start - c->mapstart;
641 wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
642 loadcmds[nloadcmds - 1].allocend - c->mapend,
643 PROT_NONE);
644 goto postmap;
646 else
648 /* sanity check */
649 if ((char *)c->mapstart + maplength > preloader_start &&
650 (char *)c->mapstart <= preloader_end)
651 fatal_error( "%s: binary overlaps preloader (%x-%x)\n",
652 name, c->mapstart, (char *)c->mapstart + maplength );
654 ELF_FIXED_ADDRESS (loader, c->mapstart);
657 /* Remember which part of the address space this object uses. */
658 l->l_map_start = c->mapstart + l->l_addr;
659 l->l_map_end = l->l_map_start + maplength;
661 while (c < &loadcmds[nloadcmds])
663 if (c->mapend > c->mapstart)
664 /* Map the segment contents from the file. */
665 wld_mmap ((void *) (l->l_addr + c->mapstart),
666 c->mapend - c->mapstart, c->prot,
667 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
669 postmap:
670 if (l->l_phdr == 0
671 && (ElfW(Off)) c->mapoff <= header->e_phoff
672 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
673 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
674 /* Found the program header in this segment. */
675 l->l_phdr = (void *)(unsigned int) (c->mapstart + header->e_phoff - c->mapoff);
677 if (c->allocend > c->dataend)
679 /* Extra zero pages should appear at the end of this segment,
680 after the data mapped from the file. */
681 ElfW(Addr) zero, zeroend, zeropage;
683 zero = l->l_addr + c->dataend;
684 zeroend = l->l_addr + c->allocend;
685 zeropage = (zero + page_mask) & ~page_mask;
688 * This is different from the dl-load load...
689 * ld-linux.so.2 relies on the whole page being zero'ed
691 zeroend = (zeroend + page_mask) & ~page_mask;
693 if (zeroend < zeropage)
695 /* All the extra data is in the last page of the segment.
696 We can just zero it. */
697 zeropage = zeroend;
700 if (zeropage > zero)
702 /* Zero the final part of the last page of the segment. */
703 if ((c->prot & PROT_WRITE) == 0)
705 /* Dag nab it. */
706 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
708 wld_memset ((void *) zero, '\0', zeropage - zero);
709 if ((c->prot & PROT_WRITE) == 0)
710 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
713 if (zeroend > zeropage)
715 /* Map the remaining zero pages in from the zero fill FD. */
716 caddr_t mapat;
717 mapat = wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
718 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
719 -1, 0);
723 ++c;
726 if (l->l_phdr == NULL) fatal_error("no program header\n");
728 l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
729 l->l_entry += l->l_addr;
731 wld_close( fd );
736 * Find a symbol in the symbol table of the executable loaded
738 static void *find_symbol( const ElfW(Phdr) *phdr, int num, const char *var )
740 const ElfW(Dyn) *dyn = NULL;
741 const ElfW(Phdr) *ph;
742 const ElfW(Sym) *symtab = NULL;
743 const char *strings = NULL;
744 uint32_t i, symtabend = 0;
746 /* check the values */
747 #ifdef DUMP_SYMS
748 wld_printf("%x %x\n", phdr, num );
749 #endif
750 if( ( phdr == NULL ) || ( num == 0 ) )
752 wld_printf("could not find PT_DYNAMIC header entry\n");
753 return NULL;
756 /* parse the (already loaded) ELF executable's header */
757 for (ph = phdr; ph < &phdr[num]; ++ph)
759 if( PT_DYNAMIC == ph->p_type )
761 dyn = (void *) ph->p_vaddr;
762 num = ph->p_memsz / sizeof (Elf32_Dyn);
763 break;
766 if( !dyn ) return NULL;
768 while( dyn->d_tag )
770 if( dyn->d_tag == DT_STRTAB )
771 strings = (const char*) dyn->d_un.d_ptr;
772 if( dyn->d_tag == DT_SYMTAB )
773 symtab = (const ElfW(Sym) *)dyn->d_un.d_ptr;
774 if( dyn->d_tag == DT_HASH )
775 symtabend = *((const uint32_t *)dyn->d_un.d_ptr + 1);
776 #ifdef DUMP_SYMS
777 wld_printf("%x %x\n", dyn->d_tag, dyn->d_un.d_ptr );
778 #endif
779 dyn++;
782 if( (!symtab) || (!strings) ) return NULL;
784 for (i = 0; i < symtabend; i++)
786 if( ( ELF32_ST_BIND(symtab[i].st_info) == STT_OBJECT ) &&
787 ( 0 == wld_strcmp( strings+symtab[i].st_name, var ) ) )
789 #ifdef DUMP_SYMS
790 wld_printf("Found %s -> %x\n", strings+symtab[i].st_name, symtab[i].st_value );
791 #endif
792 return (void*)symtab[i].st_value;
795 return NULL;
799 * preload_reserve
801 * Reserve a range specified in string format
803 static void preload_reserve( const char *str )
805 const char *p;
806 unsigned long result = 0;
807 void *start = NULL, *end = NULL;
808 int first = 1;
810 for (p = str; *p; p++)
812 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
813 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
814 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
815 else if (*p == '-')
817 if (!first) goto error;
818 start = (void *)(result & ~page_mask);
819 result = 0;
820 first = 0;
822 else goto error;
824 if (!first) end = (void *)((result + page_mask) & ~page_mask);
825 else if (result) goto error; /* single value '0' is allowed */
827 /* sanity checks */
828 if (end <= start) start = end = NULL;
829 else if ((char *)end > preloader_start &&
830 (char *)start <= preloader_end)
832 wld_printf( "WINEPRELOADRESERVE range %x-%x overlaps preloader %x-%x\n",
833 start, end, preloader_start, preloader_end );
834 start = end = NULL;
837 /* entry 2 is for the PE exe */
838 preload_info[2].addr = start;
839 preload_info[2].size = (char *)end - (char *)start;
840 return;
842 error:
843 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
847 * is_in_preload_range
849 * Check if address of the given aux value is in one of the reserved ranges
851 static int is_in_preload_range( const ElfW(auxv_t) *av, int type )
853 int i;
855 while (av->a_type != type && av->a_type != AT_NULL) av++;
857 if (av->a_type == type)
859 for (i = 0; preload_info[i].size; i++)
861 if ((char *)av->a_un.a_ptr >= (char *)preload_info[i].addr &&
862 (char *)av->a_un.a_ptr < (char *)preload_info[i].addr + preload_info[i].size)
863 return 1;
866 return 0;
870 * wld_start
872 * Repeat the actions the kernel would do when loading a dynamically linked .so
873 * Load the binary and then its ELF interpreter.
874 * Note, we assume that the binary is a dynamically linked ELF shared object.
876 void* wld_start( void **stack )
878 int i, *pargc;
879 char **argv, **p;
880 char *interp, *reserve = NULL;
881 ElfW(auxv_t) new_av[12], delete_av[3], *av;
882 struct wld_link_map main_binary_map, ld_so_map;
883 struct wine_preload_info **wine_main_preload_info;
885 pargc = *stack;
886 argv = (char **)pargc + 1;
887 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
889 /* skip over the parameters */
890 p = argv + *pargc + 1;
892 /* skip over the environment */
893 while (*p)
895 static const char res[] = "WINEPRELOADRESERVE=";
896 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
897 p++;
900 av = (ElfW(auxv_t)*) (p+1);
901 page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
902 page_mask = page_size - 1;
904 preloader_start = (char *)_start - ((unsigned int)_start & page_mask);
905 preloader_end = (char *)((unsigned int)(_end + page_mask) & ~page_mask);
907 #ifdef DUMP_AUX_INFO
908 wld_printf( "stack = %x\n", *stack );
909 for( i = 0; i < *pargc; i++ ) wld_printf("argv[%x] = %s\n", i, argv[i]);
910 dump_auxiliary( av );
911 #endif
913 /* reserve memory that Wine needs */
914 if (reserve) preload_reserve( reserve );
915 for (i = 0; preload_info[i].size; i++)
916 wld_mmap( preload_info[i].addr, preload_info[i].size,
917 PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 );
919 /* load the main binary */
920 map_so_lib( argv[1], &main_binary_map );
922 /* load the ELF interpreter */
923 interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
924 map_so_lib( interp, &ld_so_map );
926 /* store pointer to the preload info into the appropriate main binary variable */
927 wine_main_preload_info = find_symbol( main_binary_map.l_phdr, main_binary_map.l_phnum,
928 "wine_main_preload_info" );
929 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
930 else wld_printf( "wine_main_preload_info not found\n" );
932 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
933 SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
934 SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
935 SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
936 SET_NEW_AV( 3, AT_PAGESZ, page_size );
937 SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
938 SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
939 SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
940 SET_NEW_AV( 7, AT_UID, get_auxiliary( av, AT_UID, wld_getuid() ) );
941 SET_NEW_AV( 8, AT_EUID, get_auxiliary( av, AT_EUID, wld_geteuid() ) );
942 SET_NEW_AV( 9, AT_GID, get_auxiliary( av, AT_GID, wld_getgid() ) );
943 SET_NEW_AV(10, AT_EGID, get_auxiliary( av, AT_EGID, wld_getegid() ) );
944 SET_NEW_AV(11, AT_NULL, 0 );
945 #undef SET_NEW_AV
947 i = 0;
948 /* delete sysinfo values if addresses conflict */
949 if (is_in_preload_range( av, AT_SYSINFO )) delete_av[i++].a_type = AT_SYSINFO;
950 if (is_in_preload_range( av, AT_SYSINFO_EHDR )) delete_av[i++].a_type = AT_SYSINFO_EHDR;
951 delete_av[i].a_type = AT_NULL;
953 /* get rid of first argument */
954 pargc[1] = pargc[0] - 1;
955 *stack = pargc + 1;
957 set_auxiliary_values( av, new_av, delete_av, stack );
959 #ifdef DUMP_AUX_INFO
960 wld_printf("new stack = %x\n", *stack);
961 wld_printf("jumping to %x\n", ld_so_map.l_entry);
962 #endif
964 return (void *)ld_so_map.l_entry;