Sync msvcmaker with make_ctests to generate valid code.
[wine.git] / loader / preloader.c
blobf2898b2df0c68eb3bcf4217edc007757488c35a6
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 #ifdef HAVE_SYS_STAT_H
72 # include <sys/stat.h>
73 #endif
74 #include <fcntl.h>
75 #ifdef HAVE_SYS_MMAN_H
76 # include <sys/mman.h>
77 #endif
78 #ifdef HAVE_SYS_SYSCALL_H
79 # include <sys/syscall.h>
80 #endif
81 #ifdef HAVE_UNISTD_H
82 # include <unistd.h>
83 #endif
84 #ifdef HAVE_ELF_H
85 # include <elf.h>
86 #endif
87 #ifdef HAVE_LINK_H
88 # include <link.h>
89 #endif
90 #ifdef HAVE_SYS_LINK_H
91 # include <sys/link.h>
92 #endif
94 #include "main.h"
96 /* ELF definitions */
97 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
98 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
100 #define MAP_BASE_ADDR(l) 0
102 #ifndef MAP_COPY
103 #define MAP_COPY MAP_PRIVATE
104 #endif
105 #ifndef MAP_NORESERVE
106 #define MAP_NORESERVE 0
107 #endif
109 static struct wine_preload_info preload_info[] =
111 { (void *)0x00000000, 0x00110000 }, /* DOS area */
112 { (void *)0x80000000, 0x01000000 }, /* shared heap */
113 { (void *)0x00110000, 0x1fef0000 }, /* PE exe range (may be set with WINEPRELOADRESERVE), defaults to 512mb */
114 { 0, 0 } /* end of list */
117 /* debugging */
118 #undef DUMP_SEGMENTS
119 #undef DUMP_AUX_INFO
120 #undef DUMP_SYMS
122 /* older systems may not define these */
123 #ifndef PT_TLS
124 #define PT_TLS 7
125 #endif
127 #ifndef AT_SYSINFO
128 #define AT_SYSINFO 32
129 #endif
130 #ifndef AT_SYSINFO_EHDR
131 #define AT_SYSINFO_EHDR 33
132 #endif
134 static unsigned int page_size, page_mask;
135 static char *preloader_start, *preloader_end;
137 struct wld_link_map {
138 ElfW(Addr) l_addr;
139 ElfW(Dyn) *l_ld;
140 ElfW(Phdr)*l_phdr;
141 ElfW(Addr) l_entry;
142 ElfW(Half) l_ldnum;
143 ElfW(Half) l_phnum;
144 ElfW(Addr) l_map_start, l_map_end;
145 ElfW(Addr) l_interp;
150 * The __bb_init_func is an empty function only called when file is
151 * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
152 * function is normally provided by libc's startup files, but since we
153 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
154 * provide our own (empty) version, otherwise linker fails.
156 void __bb_init_func() { return; }
160 * The _start function is the entry and exit point of this program
162 * It calls wld_start, passing a pointer to the args it receives
163 * then jumps to the address wld_start returns.
165 void _start();
166 extern char _end[];
167 __ASM_GLOBAL_FUNC(_start,
168 "\tmovl %esp,%eax\n"
169 "\tleal -128(%esp),%esp\n" /* allocate some space for extra aux values */
170 "\tpushl %eax\n" /* orig stack pointer */
171 "\tpushl %esp\n" /* ptr to orig stack pointer */
172 "\tcall wld_start\n"
173 "\tpopl %ecx\n" /* remove ptr to stack pointer */
174 "\tpopl %esp\n" /* new stack pointer */
175 "\tpush %eax\n" /* ELF interpreter entry point */
176 "\txor %eax,%eax\n"
177 "\txor %ecx,%ecx\n"
178 "\txor %edx,%edx\n"
179 "\tret\n")
181 /* wrappers for Linux system calls */
183 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
185 static inline __attribute__((noreturn)) void wld_exit( int code )
187 for (;;) /* avoid warning */
188 __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
189 : : "a" (SYS_exit), "r" (code) );
192 static inline int wld_open( const char *name, int flags )
194 int ret;
195 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
196 : "=a" (ret) : "0" (SYS_open), "r" (name), "c" (flags) );
197 return SYSCALL_RET(ret);
200 static inline int wld_close( int fd )
202 int ret;
203 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
204 : "=a" (ret) : "0" (SYS_close), "r" (fd) );
205 return SYSCALL_RET(ret);
208 static inline ssize_t wld_read( int fd, void *buffer, size_t len )
210 int ret;
211 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
212 : "=a" (ret)
213 : "0" (SYS_read), "r" (fd), "c" (buffer), "d" (len)
214 : "memory" );
215 return SYSCALL_RET(ret);
218 static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
220 int ret;
221 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
222 : "=a" (ret) : "0" (SYS_write), "r" (fd), "c" (buffer), "d" (len) );
223 return SYSCALL_RET(ret);
226 static inline int wld_mprotect( const void *addr, size_t len, int prot )
228 int ret;
229 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
230 : "=a" (ret) : "0" (SYS_mprotect), "r" (addr), "c" (len), "d" (prot) );
231 return SYSCALL_RET(ret);
234 static void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset )
236 int ret;
238 struct
240 void *addr;
241 unsigned int length;
242 unsigned int prot;
243 unsigned int flags;
244 unsigned int fd;
245 unsigned int offset;
246 } args;
248 args.addr = start;
249 args.length = len;
250 args.prot = prot;
251 args.flags = flags;
252 args.fd = fd;
253 args.offset = offset;
254 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
255 : "=a" (ret) : "0" (SYS_mmap), "q" (&args) : "memory" );
256 return (void *)SYSCALL_RET(ret);
259 static inline uid_t wld_getuid(void)
261 uid_t ret;
262 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getuid) );
263 return ret;
266 static inline uid_t wld_geteuid(void)
268 uid_t ret;
269 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_geteuid) );
270 return ret;
273 static inline gid_t wld_getgid(void)
275 gid_t ret;
276 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getgid) );
277 return ret;
280 static inline gid_t wld_getegid(void)
282 gid_t ret;
283 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getegid) );
284 return ret;
288 /* replacement for libc functions */
290 static int wld_strcmp( const char *str1, const char *str2 )
292 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
293 return *str1 - *str2;
296 static int wld_strncmp( const char *str1, const char *str2, size_t len )
298 if (len <= 0) return 0;
299 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
300 return *str1 - *str2;
303 static inline void *wld_memset( void *dest, int val, size_t len )
305 char *dst = dest;
306 while (len--) *dst++ = val;
307 return dest;
311 * wld_printf - just the basics
313 * %x prints a hex number
314 * %s prints a string
316 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
318 static const char hex_chars[16] = "0123456789abcdef";
319 const char *p = fmt;
320 char *str = buffer;
322 while( *p )
324 if( *p == '%' )
326 p++;
327 if( *p == 'x' )
329 int i;
330 unsigned int x = va_arg( args, unsigned int );
331 for(i=7; i>=0; i--)
332 *str++ = hex_chars[(x>>(i*4))&0xf];
334 else if( *p == 's' )
336 char *s = va_arg( args, char * );
337 while(*s)
338 *str++ = *s++;
340 else if( *p == 0 )
341 break;
342 p++;
344 *str++ = *p++;
346 *str = 0;
347 return str - buffer;
350 static void wld_printf(const char *fmt, ... )
352 va_list args;
353 char buffer[256];
354 int len;
356 va_start( args, fmt );
357 len = wld_vsprintf(buffer, fmt, args );
358 va_end( args );
359 wld_write(2, buffer, len);
362 static __attribute__((noreturn)) void fatal_error(const char *fmt, ... )
364 va_list args;
365 char buffer[256];
366 int len;
368 va_start( args, fmt );
369 len = wld_vsprintf(buffer, fmt, args );
370 va_end( args );
371 wld_write(2, buffer, len);
372 wld_exit(1);
375 #ifdef DUMP_AUX_INFO
377 * Dump interesting bits of the ELF auxv_t structure that is passed
378 * as the 4th parameter to the _start function
380 static void dump_auxiliary( ElfW(auxv_t) *av )
382 #define NAME(at) { at, #at }
383 static const struct { int val; const char *name; } names[] =
385 NAME(AT_BASE),
386 NAME(AT_CLKTCK),
387 NAME(AT_EGID),
388 NAME(AT_ENTRY),
389 NAME(AT_EUID),
390 NAME(AT_FLAGS),
391 NAME(AT_GID),
392 NAME(AT_HWCAP),
393 NAME(AT_PAGESZ),
394 NAME(AT_PHDR),
395 NAME(AT_PHENT),
396 NAME(AT_PHNUM),
397 NAME(AT_PLATFORM),
398 NAME(AT_SYSINFO),
399 NAME(AT_SYSINFO_EHDR),
400 NAME(AT_UID),
401 { 0, NULL }
403 #undef NAME
405 int i;
407 for ( ; av->a_type != AT_NULL; av++)
409 for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
410 if (names[i].name) wld_printf("%s = %x\n", names[i].name, av->a_un.a_val);
411 else wld_printf( "%x = %x\n", av->a_type, av->a_un.a_val );
414 #endif
417 * set_auxiliary_values
419 * Set the new auxiliary values
421 static void set_auxiliary_values( ElfW(auxv_t) *av, const ElfW(auxv_t) *new_av,
422 const ElfW(auxv_t) *delete_av, void **stack )
424 int i, j, av_count = 0, new_count = 0, delete_count = 0;
425 char *src, *dst;
427 /* count how many aux values we have already */
428 while (av[av_count].a_type != AT_NULL) av_count++;
430 /* delete unwanted values */
431 for (j = 0; delete_av[j].a_type != AT_NULL; j++)
433 for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
435 av[i].a_type = av[av_count-1].a_type;
436 av[i].a_un.a_val = av[av_count-1].a_un.a_val;
437 av[--av_count].a_type = AT_NULL;
438 delete_count++;
439 break;
443 /* count how many values we have in new_av that aren't in av */
444 for (j = 0; new_av[j].a_type != AT_NULL; j++)
446 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
447 if (i == av_count) new_count++;
450 src = (char *)*stack;
451 dst = src - (new_count - delete_count) * sizeof(*av);
452 if (new_count > delete_count) /* need to make room for the extra values */
454 int len = (char *)(av + av_count + 1) - src;
455 for (i = 0; i < len; i++) dst[i] = src[i];
457 else if (new_count < delete_count) /* get rid of unused values */
459 int len = (char *)(av + av_count + 1) - dst;
460 for (i = len - 1; i >= 0; i--) dst[i] = src[i];
462 *stack = dst;
463 av -= (new_count - delete_count);
465 /* now set the values */
466 for (j = 0; new_av[j].a_type != AT_NULL; j++)
468 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
469 if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
470 else
472 av[av_count].a_type = new_av[j].a_type;
473 av[av_count].a_un.a_val = new_av[j].a_un.a_val;
474 av_count++;
478 #ifdef DUMP_AUX_INFO
479 wld_printf("New auxiliary info:\n");
480 dump_auxiliary( av );
481 #endif
485 * get_auxiliary
487 * Get a field of the auxiliary structure
489 static int get_auxiliary( ElfW(auxv_t) *av, int type, int def_val )
491 for ( ; av->a_type != AT_NULL; av++)
492 if( av->a_type == type ) return av->a_un.a_val;
493 return def_val;
497 * map_so_lib
499 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
501 * This function maps the segments from an ELF object, and optionally
502 * stores information about the mapping into the auxv_t structure.
504 static void map_so_lib( const char *name, struct wld_link_map *l)
506 int fd;
507 unsigned char buf[0x800];
508 ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
509 ElfW(Phdr) *phdr, *ph;
510 /* Scan the program header table, collecting its load commands. */
511 struct loadcmd
513 ElfW(Addr) mapstart, mapend, dataend, allocend;
514 off_t mapoff;
515 int prot;
516 } loadcmds[16], *c;
517 size_t nloadcmds = 0, maplength;
519 fd = wld_open( name, O_RDONLY );
520 if (fd == -1) fatal_error("%s: could not open\n", name );
522 if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
523 fatal_error("%s: failed to read ELF header\n", name);
525 phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
527 if( ( header->e_ident[0] != 0x7f ) ||
528 ( header->e_ident[1] != 'E' ) ||
529 ( header->e_ident[2] != 'L' ) ||
530 ( header->e_ident[3] != 'F' ) )
531 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
533 if( header->e_machine != EM_386 )
534 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
536 if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
537 fatal_error( "%s: oops... not enough space for load commands\n", name );
539 maplength = header->e_phnum * sizeof (ElfW(Phdr));
540 if (header->e_phoff + maplength > sizeof(buf))
541 fatal_error( "%s: oops... not enough space for ELF headers\n", name );
543 l->l_ld = 0;
544 l->l_addr = 0;
545 l->l_phdr = 0;
546 l->l_phnum = header->e_phnum;
547 l->l_entry = header->e_entry;
548 l->l_interp = 0;
550 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
553 #ifdef DUMP_SEGMENTS
554 wld_printf( "ph = %x\n", ph );
555 wld_printf( " p_type = %x\n", ph->p_type );
556 wld_printf( " p_flags = %x\n", ph->p_flags );
557 wld_printf( " p_offset = %x\n", ph->p_offset );
558 wld_printf( " p_vaddr = %x\n", ph->p_vaddr );
559 wld_printf( " p_paddr = %x\n", ph->p_paddr );
560 wld_printf( " p_filesz = %x\n", ph->p_filesz );
561 wld_printf( " p_memsz = %x\n", ph->p_memsz );
562 wld_printf( " p_align = %x\n", ph->p_align );
563 #endif
565 switch (ph->p_type)
567 /* These entries tell us where to find things once the file's
568 segments are mapped in. We record the addresses it says
569 verbatim, and later correct for the run-time load address. */
570 case PT_DYNAMIC:
571 l->l_ld = (void *) ph->p_vaddr;
572 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
573 break;
575 case PT_PHDR:
576 l->l_phdr = (void *) ph->p_vaddr;
577 break;
579 case PT_LOAD:
581 if ((ph->p_align & page_mask) != 0)
582 fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
584 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
585 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
587 c = &loadcmds[nloadcmds++];
588 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
589 c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
590 c->dataend = ph->p_vaddr + ph->p_filesz;
591 c->allocend = ph->p_vaddr + ph->p_memsz;
592 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
594 c->prot = 0;
595 if (ph->p_flags & PF_R)
596 c->prot |= PROT_READ;
597 if (ph->p_flags & PF_W)
598 c->prot |= PROT_WRITE;
599 if (ph->p_flags & PF_X)
600 c->prot |= PROT_EXEC;
602 break;
604 case PT_INTERP:
605 l->l_interp = ph->p_vaddr;
606 break;
608 case PT_TLS:
610 * We don't need to set anything up because we're
611 * emulating the kernel, not ld-linux.so.2
612 * The ELF loader will set up the TLS data itself.
614 case PT_SHLIB:
615 case PT_NOTE:
616 default:
617 break;
621 /* Now process the load commands and map segments into memory. */
622 c = loadcmds;
624 /* Length of the sections to be loaded. */
625 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
627 if( header->e_type == ET_DYN )
629 ElfW(Addr) mappref;
630 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
631 - MAP_BASE_ADDR (l));
633 /* Remember which part of the address space this object uses. */
634 l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
635 c->prot, MAP_COPY | MAP_FILE,
636 fd, c->mapoff);
637 /* wld_printf("set : offset = %x\n", c->mapoff); */
638 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
640 l->l_map_end = l->l_map_start + maplength;
641 l->l_addr = l->l_map_start - c->mapstart;
643 wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
644 loadcmds[nloadcmds - 1].allocend - c->mapend,
645 PROT_NONE);
646 goto postmap;
648 else
650 /* sanity check */
651 if ((char *)c->mapstart + maplength > preloader_start &&
652 (char *)c->mapstart <= preloader_end)
653 fatal_error( "%s: binary overlaps preloader (%x-%x)\n",
654 name, c->mapstart, (char *)c->mapstart + maplength );
656 ELF_FIXED_ADDRESS (loader, c->mapstart);
659 /* Remember which part of the address space this object uses. */
660 l->l_map_start = c->mapstart + l->l_addr;
661 l->l_map_end = l->l_map_start + maplength;
663 while (c < &loadcmds[nloadcmds])
665 if (c->mapend > c->mapstart)
666 /* Map the segment contents from the file. */
667 wld_mmap ((void *) (l->l_addr + c->mapstart),
668 c->mapend - c->mapstart, c->prot,
669 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
671 postmap:
672 if (l->l_phdr == 0
673 && (ElfW(Off)) c->mapoff <= header->e_phoff
674 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
675 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
676 /* Found the program header in this segment. */
677 l->l_phdr = (void *)(unsigned int) (c->mapstart + header->e_phoff - c->mapoff);
679 if (c->allocend > c->dataend)
681 /* Extra zero pages should appear at the end of this segment,
682 after the data mapped from the file. */
683 ElfW(Addr) zero, zeroend, zeropage;
685 zero = l->l_addr + c->dataend;
686 zeroend = l->l_addr + c->allocend;
687 zeropage = (zero + page_mask) & ~page_mask;
690 * This is different from the dl-load load...
691 * ld-linux.so.2 relies on the whole page being zero'ed
693 zeroend = (zeroend + page_mask) & ~page_mask;
695 if (zeroend < zeropage)
697 /* All the extra data is in the last page of the segment.
698 We can just zero it. */
699 zeropage = zeroend;
702 if (zeropage > zero)
704 /* Zero the final part of the last page of the segment. */
705 if ((c->prot & PROT_WRITE) == 0)
707 /* Dag nab it. */
708 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
710 wld_memset ((void *) zero, '\0', zeropage - zero);
711 if ((c->prot & PROT_WRITE) == 0)
712 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
715 if (zeroend > zeropage)
717 /* Map the remaining zero pages in from the zero fill FD. */
718 caddr_t mapat;
719 mapat = wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
720 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
721 -1, 0);
725 ++c;
728 if (l->l_phdr == NULL) fatal_error("no program header\n");
730 l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
731 l->l_entry += l->l_addr;
733 wld_close( fd );
738 * Find a symbol in the symbol table of the executable loaded
740 static void *find_symbol( const ElfW(Phdr) *phdr, int num, const char *var )
742 const ElfW(Dyn) *dyn = NULL;
743 const ElfW(Phdr) *ph;
744 const ElfW(Sym) *symtab = NULL;
745 const char *strings = NULL;
746 uint32_t i, symtabend = 0;
748 /* check the values */
749 #ifdef DUMP_SYMS
750 wld_printf("%x %x\n", phdr, num );
751 #endif
752 if( ( phdr == NULL ) || ( num == 0 ) )
754 wld_printf("could not find PT_DYNAMIC header entry\n");
755 return NULL;
758 /* parse the (already loaded) ELF executable's header */
759 for (ph = phdr; ph < &phdr[num]; ++ph)
761 if( PT_DYNAMIC == ph->p_type )
763 dyn = (void *) ph->p_vaddr;
764 num = ph->p_memsz / sizeof (Elf32_Dyn);
765 break;
768 if( !dyn ) return NULL;
770 while( dyn->d_tag )
772 if( dyn->d_tag == DT_STRTAB )
773 strings = (const char*) dyn->d_un.d_ptr;
774 if( dyn->d_tag == DT_SYMTAB )
775 symtab = (const ElfW(Sym) *)dyn->d_un.d_ptr;
776 if( dyn->d_tag == DT_HASH )
777 symtabend = *((const uint32_t *)dyn->d_un.d_ptr + 1);
778 #ifdef DUMP_SYMS
779 wld_printf("%x %x\n", dyn->d_tag, dyn->d_un.d_ptr );
780 #endif
781 dyn++;
784 if( (!symtab) || (!strings) ) return NULL;
786 for (i = 0; i < symtabend; i++)
788 if( ( ELF32_ST_BIND(symtab[i].st_info) == STT_OBJECT ) &&
789 ( 0 == wld_strcmp( strings+symtab[i].st_name, var ) ) )
791 #ifdef DUMP_SYMS
792 wld_printf("Found %s -> %x\n", strings+symtab[i].st_name, symtab[i].st_value );
793 #endif
794 return (void*)symtab[i].st_value;
797 return NULL;
801 * preload_reserve
803 * Reserve a range specified in string format
805 static void preload_reserve( const char *str )
807 const char *p;
808 unsigned long result = 0;
809 void *start = NULL, *end = NULL;
810 int first = 1;
812 for (p = str; *p; p++)
814 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
815 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
816 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
817 else if (*p == '-')
819 if (!first) goto error;
820 start = (void *)(result & ~page_mask);
821 result = 0;
822 first = 0;
824 else goto error;
826 if (!first) end = (void *)((result + page_mask) & ~page_mask);
827 else if (result) goto error; /* single value '0' is allowed */
829 /* sanity checks */
830 if (end <= start) start = end = NULL;
831 else if ((char *)end > preloader_start &&
832 (char *)start <= preloader_end)
834 wld_printf( "WINEPRELOADRESERVE range %x-%x overlaps preloader %x-%x\n",
835 start, end, preloader_start, preloader_end );
836 start = end = NULL;
839 /* entry 2 is for the PE exe */
840 preload_info[2].addr = start;
841 preload_info[2].size = (char *)end - (char *)start;
842 return;
844 error:
845 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
849 * is_in_preload_range
851 * Check if address of the given aux value is in one of the reserved ranges
853 static int is_in_preload_range( const ElfW(auxv_t) *av, int type )
855 int i;
857 while (av->a_type != type && av->a_type != AT_NULL) av++;
859 if (av->a_type == type)
861 for (i = 0; preload_info[i].size; i++)
863 if ((char *)av->a_un.a_val >= (char *)preload_info[i].addr &&
864 (char *)av->a_un.a_val < (char *)preload_info[i].addr + preload_info[i].size)
865 return 1;
868 return 0;
872 * wld_start
874 * Repeat the actions the kernel would do when loading a dynamically linked .so
875 * Load the binary and then its ELF interpreter.
876 * Note, we assume that the binary is a dynamically linked ELF shared object.
878 void* wld_start( void **stack )
880 int i, *pargc;
881 char **argv, **p;
882 char *interp, *reserve = NULL;
883 ElfW(auxv_t) new_av[12], delete_av[3], *av;
884 struct wld_link_map main_binary_map, ld_so_map;
885 struct wine_preload_info **wine_main_preload_info;
887 pargc = *stack;
888 argv = (char **)pargc + 1;
889 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
891 /* skip over the parameters */
892 p = argv + *pargc + 1;
894 /* skip over the environment */
895 while (*p)
897 static const char res[] = "WINEPRELOADRESERVE=";
898 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
899 p++;
902 av = (ElfW(auxv_t)*) (p+1);
903 page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
904 page_mask = page_size - 1;
906 preloader_start = (char *)_start - ((unsigned int)_start & page_mask);
907 preloader_end = (char *)((unsigned int)(_end + page_mask) & ~page_mask);
909 #ifdef DUMP_AUX_INFO
910 wld_printf( "stack = %x\n", *stack );
911 for( i = 0; i < *pargc; i++ ) wld_printf("argv[%x] = %s\n", i, argv[i]);
912 dump_auxiliary( av );
913 #endif
915 /* reserve memory that Wine needs */
916 if (reserve) preload_reserve( reserve );
917 for (i = 0; preload_info[i].size; i++)
918 wld_mmap( preload_info[i].addr, preload_info[i].size,
919 PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 );
921 /* load the main binary */
922 map_so_lib( argv[1], &main_binary_map );
924 /* load the ELF interpreter */
925 interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
926 map_so_lib( interp, &ld_so_map );
928 /* store pointer to the preload info into the appropriate main binary variable */
929 wine_main_preload_info = find_symbol( main_binary_map.l_phdr, main_binary_map.l_phnum,
930 "wine_main_preload_info" );
931 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
932 else wld_printf( "wine_main_preload_info not found\n" );
934 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
935 SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
936 SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
937 SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
938 SET_NEW_AV( 3, AT_PAGESZ, page_size );
939 SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
940 SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
941 SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
942 SET_NEW_AV( 7, AT_UID, get_auxiliary( av, AT_UID, wld_getuid() ) );
943 SET_NEW_AV( 8, AT_EUID, get_auxiliary( av, AT_EUID, wld_geteuid() ) );
944 SET_NEW_AV( 9, AT_GID, get_auxiliary( av, AT_GID, wld_getgid() ) );
945 SET_NEW_AV(10, AT_EGID, get_auxiliary( av, AT_EGID, wld_getegid() ) );
946 SET_NEW_AV(11, AT_NULL, 0 );
947 #undef SET_NEW_AV
949 i = 0;
950 /* delete sysinfo values if addresses conflict */
951 if (is_in_preload_range( av, AT_SYSINFO )) delete_av[i++].a_type = AT_SYSINFO;
952 if (is_in_preload_range( av, AT_SYSINFO_EHDR )) delete_av[i++].a_type = AT_SYSINFO_EHDR;
953 delete_av[i].a_type = AT_NULL;
955 /* get rid of first argument */
956 pargc[1] = pargc[0] - 1;
957 *stack = pargc + 1;
959 set_auxiliary_values( av, new_av, delete_av, stack );
961 #ifdef DUMP_AUX_INFO
962 wld_printf("new stack = %x\n", *stack);
963 wld_printf("jumping to %x\n", ld_so_map.l_entry);
964 #endif
966 return (void *)ld_so_map.l_entry;