wined3d: Fix lookup for LOOP register.
[wine/multimedia.git] / loader / preloader.c
blob274d8580411bb3cb9aa1894c976da285bc5a7277
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 #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 *)0x7ffe0000, 0x01020000 }, /* shared user data + 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(void) { return; }
158 /* similar to the above but for -fstack-protector */
159 void *__stack_chk_guard = 0;
160 void __stack_chk_fail(void) { return; }
163 * The _start function is the entry and exit point of this program
165 * It calls wld_start, passing a pointer to the args it receives
166 * then jumps to the address wld_start returns.
168 void _start();
169 extern char _end[];
170 __ASM_GLOBAL_FUNC(_start,
171 "\tmovl %esp,%eax\n"
172 "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */
173 "\tpushl %eax\n" /* orig stack pointer */
174 "\tpushl %esp\n" /* ptr to orig stack pointer */
175 "\tcall wld_start\n"
176 "\tpopl %ecx\n" /* remove ptr to stack pointer */
177 "\tpopl %esp\n" /* new stack pointer */
178 "\tpush %eax\n" /* ELF interpreter entry point */
179 "\txor %eax,%eax\n"
180 "\txor %ecx,%ecx\n"
181 "\txor %edx,%edx\n"
182 "\tret\n")
184 /* wrappers for Linux system calls */
186 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
188 static inline __attribute__((noreturn)) void wld_exit( int code )
190 for (;;) /* avoid warning */
191 __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
192 : : "a" (SYS_exit), "r" (code) );
195 static inline int wld_open( const char *name, int flags )
197 int ret;
198 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
199 : "=a" (ret) : "0" (SYS_open), "r" (name), "c" (flags) );
200 return SYSCALL_RET(ret);
203 static inline int wld_close( int fd )
205 int ret;
206 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
207 : "=a" (ret) : "0" (SYS_close), "r" (fd) );
208 return SYSCALL_RET(ret);
211 static inline ssize_t wld_read( int fd, void *buffer, size_t len )
213 int ret;
214 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
215 : "=a" (ret)
216 : "0" (SYS_read), "r" (fd), "c" (buffer), "d" (len)
217 : "memory" );
218 return SYSCALL_RET(ret);
221 static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
223 int ret;
224 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
225 : "=a" (ret) : "0" (SYS_write), "r" (fd), "c" (buffer), "d" (len) );
226 return SYSCALL_RET(ret);
229 static inline int wld_mprotect( const void *addr, size_t len, int prot )
231 int ret;
232 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
233 : "=a" (ret) : "0" (SYS_mprotect), "r" (addr), "c" (len), "d" (prot) );
234 return SYSCALL_RET(ret);
237 static void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset )
239 int ret;
241 struct
243 void *addr;
244 unsigned int length;
245 unsigned int prot;
246 unsigned int flags;
247 unsigned int fd;
248 unsigned int offset;
249 } args;
251 args.addr = start;
252 args.length = len;
253 args.prot = prot;
254 args.flags = flags;
255 args.fd = fd;
256 args.offset = offset;
257 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
258 : "=a" (ret) : "0" (SYS_mmap), "q" (&args) : "memory" );
259 return (void *)SYSCALL_RET(ret);
262 static inline uid_t wld_getuid(void)
264 uid_t ret;
265 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getuid) );
266 return ret;
269 static inline uid_t wld_geteuid(void)
271 uid_t ret;
272 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_geteuid) );
273 return ret;
276 static inline gid_t wld_getgid(void)
278 gid_t ret;
279 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getgid) );
280 return ret;
283 static inline gid_t wld_getegid(void)
285 gid_t ret;
286 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getegid) );
287 return ret;
290 static inline int wld_prctl( int code, int arg )
292 int ret;
293 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
294 : "=a" (ret) : "0" (SYS_prctl), "r" (code), "c" (arg) );
295 return SYSCALL_RET(ret);
299 /* replacement for libc functions */
301 static int wld_strcmp( const char *str1, const char *str2 )
303 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
304 return *str1 - *str2;
307 static int wld_strncmp( const char *str1, const char *str2, size_t len )
309 if (len <= 0) return 0;
310 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
311 return *str1 - *str2;
314 static inline void *wld_memset( void *dest, int val, size_t len )
316 char *dst = dest;
317 while (len--) *dst++ = val;
318 return dest;
322 * wld_printf - just the basics
324 * %x prints a hex number
325 * %s prints a string
327 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
329 static const char hex_chars[16] = "0123456789abcdef";
330 const char *p = fmt;
331 char *str = buffer;
333 while( *p )
335 if( *p == '%' )
337 p++;
338 if( *p == 'x' )
340 int i;
341 unsigned int x = va_arg( args, unsigned int );
342 for(i=7; i>=0; i--)
343 *str++ = hex_chars[(x>>(i*4))&0xf];
345 else if( *p == 's' )
347 char *s = va_arg( args, char * );
348 while(*s)
349 *str++ = *s++;
351 else if( *p == 0 )
352 break;
353 p++;
355 *str++ = *p++;
357 *str = 0;
358 return str - buffer;
361 static void wld_printf(const char *fmt, ... )
363 va_list args;
364 char buffer[256];
365 int len;
367 va_start( args, fmt );
368 len = wld_vsprintf(buffer, fmt, args );
369 va_end( args );
370 wld_write(2, buffer, len);
373 static __attribute__((noreturn)) void fatal_error(const char *fmt, ... )
375 va_list args;
376 char buffer[256];
377 int len;
379 va_start( args, fmt );
380 len = wld_vsprintf(buffer, fmt, args );
381 va_end( args );
382 wld_write(2, buffer, len);
383 wld_exit(1);
386 #ifdef DUMP_AUX_INFO
388 * Dump interesting bits of the ELF auxv_t structure that is passed
389 * as the 4th parameter to the _start function
391 static void dump_auxiliary( ElfW(auxv_t) *av )
393 #define NAME(at) { at, #at }
394 static const struct { int val; const char *name; } names[] =
396 NAME(AT_BASE),
397 NAME(AT_CLKTCK),
398 NAME(AT_EGID),
399 NAME(AT_ENTRY),
400 NAME(AT_EUID),
401 NAME(AT_FLAGS),
402 NAME(AT_GID),
403 NAME(AT_HWCAP),
404 NAME(AT_PAGESZ),
405 NAME(AT_PHDR),
406 NAME(AT_PHENT),
407 NAME(AT_PHNUM),
408 NAME(AT_PLATFORM),
409 NAME(AT_SYSINFO),
410 NAME(AT_SYSINFO_EHDR),
411 NAME(AT_UID),
412 { 0, NULL }
414 #undef NAME
416 int i;
418 for ( ; av->a_type != AT_NULL; av++)
420 for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
421 if (names[i].name) wld_printf("%s = %x\n", names[i].name, av->a_un.a_val);
422 else wld_printf( "%x = %x\n", av->a_type, av->a_un.a_val );
425 #endif
428 * set_auxiliary_values
430 * Set the new auxiliary values
432 static void set_auxiliary_values( ElfW(auxv_t) *av, const ElfW(auxv_t) *new_av,
433 const ElfW(auxv_t) *delete_av, void **stack )
435 int i, j, av_count = 0, new_count = 0, delete_count = 0;
436 char *src, *dst;
438 /* count how many aux values we have already */
439 while (av[av_count].a_type != AT_NULL) av_count++;
441 /* delete unwanted values */
442 for (j = 0; delete_av[j].a_type != AT_NULL; j++)
444 for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
446 av[i].a_type = av[av_count-1].a_type;
447 av[i].a_un.a_val = av[av_count-1].a_un.a_val;
448 av[--av_count].a_type = AT_NULL;
449 delete_count++;
450 break;
454 /* count how many values we have in new_av that aren't in av */
455 for (j = 0; new_av[j].a_type != AT_NULL; j++)
457 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
458 if (i == av_count) new_count++;
461 src = (char *)*stack;
462 dst = src - (new_count - delete_count) * sizeof(*av);
463 if (new_count > delete_count) /* need to make room for the extra values */
465 int len = (char *)(av + av_count + 1) - src;
466 for (i = 0; i < len; i++) dst[i] = src[i];
468 else if (new_count < delete_count) /* get rid of unused values */
470 int len = (char *)(av + av_count + 1) - dst;
471 for (i = len - 1; i >= 0; i--) dst[i] = src[i];
473 *stack = dst;
474 av -= (new_count - delete_count);
476 /* now set the values */
477 for (j = 0; new_av[j].a_type != AT_NULL; j++)
479 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
480 if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
481 else
483 av[av_count].a_type = new_av[j].a_type;
484 av[av_count].a_un.a_val = new_av[j].a_un.a_val;
485 av_count++;
489 #ifdef DUMP_AUX_INFO
490 wld_printf("New auxiliary info:\n");
491 dump_auxiliary( av );
492 #endif
496 * get_auxiliary
498 * Get a field of the auxiliary structure
500 static int get_auxiliary( ElfW(auxv_t) *av, int type, int def_val )
502 for ( ; av->a_type != AT_NULL; av++)
503 if( av->a_type == type ) return av->a_un.a_val;
504 return def_val;
508 * map_so_lib
510 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
512 * This function maps the segments from an ELF object, and optionally
513 * stores information about the mapping into the auxv_t structure.
515 static void map_so_lib( const char *name, struct wld_link_map *l)
517 int fd;
518 unsigned char buf[0x800];
519 ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
520 ElfW(Phdr) *phdr, *ph;
521 /* Scan the program header table, collecting its load commands. */
522 struct loadcmd
524 ElfW(Addr) mapstart, mapend, dataend, allocend;
525 off_t mapoff;
526 int prot;
527 } loadcmds[16], *c;
528 size_t nloadcmds = 0, maplength;
530 fd = wld_open( name, O_RDONLY );
531 if (fd == -1) fatal_error("%s: could not open\n", name );
533 if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
534 fatal_error("%s: failed to read ELF header\n", name);
536 phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
538 if( ( header->e_ident[0] != 0x7f ) ||
539 ( header->e_ident[1] != 'E' ) ||
540 ( header->e_ident[2] != 'L' ) ||
541 ( header->e_ident[3] != 'F' ) )
542 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
544 if( header->e_machine != EM_386 )
545 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
547 if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
548 fatal_error( "%s: oops... not enough space for load commands\n", name );
550 maplength = header->e_phnum * sizeof (ElfW(Phdr));
551 if (header->e_phoff + maplength > sizeof(buf))
552 fatal_error( "%s: oops... not enough space for ELF headers\n", name );
554 l->l_ld = 0;
555 l->l_addr = 0;
556 l->l_phdr = 0;
557 l->l_phnum = header->e_phnum;
558 l->l_entry = header->e_entry;
559 l->l_interp = 0;
561 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
564 #ifdef DUMP_SEGMENTS
565 wld_printf( "ph = %x\n", ph );
566 wld_printf( " p_type = %x\n", ph->p_type );
567 wld_printf( " p_flags = %x\n", ph->p_flags );
568 wld_printf( " p_offset = %x\n", ph->p_offset );
569 wld_printf( " p_vaddr = %x\n", ph->p_vaddr );
570 wld_printf( " p_paddr = %x\n", ph->p_paddr );
571 wld_printf( " p_filesz = %x\n", ph->p_filesz );
572 wld_printf( " p_memsz = %x\n", ph->p_memsz );
573 wld_printf( " p_align = %x\n", ph->p_align );
574 #endif
576 switch (ph->p_type)
578 /* These entries tell us where to find things once the file's
579 segments are mapped in. We record the addresses it says
580 verbatim, and later correct for the run-time load address. */
581 case PT_DYNAMIC:
582 l->l_ld = (void *) ph->p_vaddr;
583 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
584 break;
586 case PT_PHDR:
587 l->l_phdr = (void *) ph->p_vaddr;
588 break;
590 case PT_LOAD:
592 if ((ph->p_align & page_mask) != 0)
593 fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
595 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
596 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
598 c = &loadcmds[nloadcmds++];
599 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
600 c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
601 c->dataend = ph->p_vaddr + ph->p_filesz;
602 c->allocend = ph->p_vaddr + ph->p_memsz;
603 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
605 c->prot = 0;
606 if (ph->p_flags & PF_R)
607 c->prot |= PROT_READ;
608 if (ph->p_flags & PF_W)
609 c->prot |= PROT_WRITE;
610 if (ph->p_flags & PF_X)
611 c->prot |= PROT_EXEC;
613 break;
615 case PT_INTERP:
616 l->l_interp = ph->p_vaddr;
617 break;
619 case PT_TLS:
621 * We don't need to set anything up because we're
622 * emulating the kernel, not ld-linux.so.2
623 * The ELF loader will set up the TLS data itself.
625 case PT_SHLIB:
626 case PT_NOTE:
627 default:
628 break;
632 /* Now process the load commands and map segments into memory. */
633 c = loadcmds;
635 /* Length of the sections to be loaded. */
636 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
638 if( header->e_type == ET_DYN )
640 ElfW(Addr) mappref;
641 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
642 - MAP_BASE_ADDR (l));
644 /* Remember which part of the address space this object uses. */
645 l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
646 c->prot, MAP_COPY | MAP_FILE,
647 fd, c->mapoff);
648 /* wld_printf("set : offset = %x\n", c->mapoff); */
649 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
651 l->l_map_end = l->l_map_start + maplength;
652 l->l_addr = l->l_map_start - c->mapstart;
654 wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
655 loadcmds[nloadcmds - 1].allocend - c->mapend,
656 PROT_NONE);
657 goto postmap;
659 else
661 /* sanity check */
662 if ((char *)c->mapstart + maplength > preloader_start &&
663 (char *)c->mapstart <= preloader_end)
664 fatal_error( "%s: binary overlaps preloader (%x-%x)\n",
665 name, c->mapstart, (char *)c->mapstart + maplength );
667 ELF_FIXED_ADDRESS (loader, c->mapstart);
670 /* Remember which part of the address space this object uses. */
671 l->l_map_start = c->mapstart + l->l_addr;
672 l->l_map_end = l->l_map_start + maplength;
674 while (c < &loadcmds[nloadcmds])
676 if (c->mapend > c->mapstart)
677 /* Map the segment contents from the file. */
678 wld_mmap ((void *) (l->l_addr + c->mapstart),
679 c->mapend - c->mapstart, c->prot,
680 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
682 postmap:
683 if (l->l_phdr == 0
684 && (ElfW(Off)) c->mapoff <= header->e_phoff
685 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
686 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
687 /* Found the program header in this segment. */
688 l->l_phdr = (void *)(unsigned int) (c->mapstart + header->e_phoff - c->mapoff);
690 if (c->allocend > c->dataend)
692 /* Extra zero pages should appear at the end of this segment,
693 after the data mapped from the file. */
694 ElfW(Addr) zero, zeroend, zeropage;
696 zero = l->l_addr + c->dataend;
697 zeroend = l->l_addr + c->allocend;
698 zeropage = (zero + page_mask) & ~page_mask;
701 * This is different from the dl-load load...
702 * ld-linux.so.2 relies on the whole page being zero'ed
704 zeroend = (zeroend + page_mask) & ~page_mask;
706 if (zeroend < zeropage)
708 /* All the extra data is in the last page of the segment.
709 We can just zero it. */
710 zeropage = zeroend;
713 if (zeropage > zero)
715 /* Zero the final part of the last page of the segment. */
716 if ((c->prot & PROT_WRITE) == 0)
718 /* Dag nab it. */
719 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
721 wld_memset ((void *) zero, '\0', zeropage - zero);
722 if ((c->prot & PROT_WRITE) == 0)
723 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
726 if (zeroend > zeropage)
728 /* Map the remaining zero pages in from the zero fill FD. */
729 caddr_t mapat;
730 mapat = wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
731 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
732 -1, 0);
736 ++c;
739 if (l->l_phdr == NULL) fatal_error("no program header\n");
741 l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
742 l->l_entry += l->l_addr;
744 wld_close( fd );
749 * Find a symbol in the symbol table of the executable loaded
751 static void *find_symbol( const ElfW(Phdr) *phdr, int num, const char *var )
753 const ElfW(Dyn) *dyn = NULL;
754 const ElfW(Phdr) *ph;
755 const ElfW(Sym) *symtab = NULL;
756 const char *strings = NULL;
757 uint32_t i, symtabend = 0;
759 /* check the values */
760 #ifdef DUMP_SYMS
761 wld_printf("%x %x\n", phdr, num );
762 #endif
763 if( ( phdr == NULL ) || ( num == 0 ) )
765 wld_printf("could not find PT_DYNAMIC header entry\n");
766 return NULL;
769 /* parse the (already loaded) ELF executable's header */
770 for (ph = phdr; ph < &phdr[num]; ++ph)
772 if( PT_DYNAMIC == ph->p_type )
774 dyn = (void *) ph->p_vaddr;
775 num = ph->p_memsz / sizeof (Elf32_Dyn);
776 break;
779 if( !dyn ) return NULL;
781 while( dyn->d_tag )
783 if( dyn->d_tag == DT_STRTAB )
784 strings = (const char*) dyn->d_un.d_ptr;
785 if( dyn->d_tag == DT_SYMTAB )
786 symtab = (const ElfW(Sym) *)dyn->d_un.d_ptr;
787 if( dyn->d_tag == DT_HASH )
788 symtabend = *((const uint32_t *)dyn->d_un.d_ptr + 1);
789 #ifdef DUMP_SYMS
790 wld_printf("%x %x\n", dyn->d_tag, dyn->d_un.d_ptr );
791 #endif
792 dyn++;
795 if( (!symtab) || (!strings) ) return NULL;
797 for (i = 0; i < symtabend; i++)
799 if( ( ELF32_ST_BIND(symtab[i].st_info) == STT_OBJECT ) &&
800 ( 0 == wld_strcmp( strings+symtab[i].st_name, var ) ) )
802 #ifdef DUMP_SYMS
803 wld_printf("Found %s -> %x\n", strings+symtab[i].st_name, symtab[i].st_value );
804 #endif
805 return (void*)symtab[i].st_value;
808 return NULL;
812 * preload_reserve
814 * Reserve a range specified in string format
816 static void preload_reserve( const char *str )
818 const char *p;
819 unsigned long result = 0;
820 void *start = NULL, *end = NULL;
821 int first = 1;
823 for (p = str; *p; p++)
825 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
826 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
827 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
828 else if (*p == '-')
830 if (!first) goto error;
831 start = (void *)(result & ~page_mask);
832 result = 0;
833 first = 0;
835 else goto error;
837 if (!first) end = (void *)((result + page_mask) & ~page_mask);
838 else if (result) goto error; /* single value '0' is allowed */
840 /* sanity checks */
841 if (end <= start) start = end = NULL;
842 else if ((char *)end > preloader_start &&
843 (char *)start <= preloader_end)
845 wld_printf( "WINEPRELOADRESERVE range %x-%x overlaps preloader %x-%x\n",
846 start, end, preloader_start, preloader_end );
847 start = end = NULL;
850 /* entry 2 is for the PE exe */
851 preload_info[2].addr = start;
852 preload_info[2].size = (char *)end - (char *)start;
853 return;
855 error:
856 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
860 * is_in_preload_range
862 * Check if address of the given aux value is in one of the reserved ranges
864 static int is_in_preload_range( const ElfW(auxv_t) *av, int type )
866 int i;
868 while (av->a_type != type && av->a_type != AT_NULL) av++;
870 if (av->a_type == type)
872 for (i = 0; preload_info[i].size; i++)
874 if ((char *)av->a_un.a_val >= (char *)preload_info[i].addr &&
875 (char *)av->a_un.a_val < (char *)preload_info[i].addr + preload_info[i].size)
876 return 1;
879 return 0;
882 /* set the process name if supported */
883 static void set_process_name( int argc, char *argv[] )
885 unsigned int i, off;
886 char *p, *name, *end;
888 /* set the process short name */
889 for (p = name = argv[1]; *p; p++) if (p[0] == '/' && p[1]) name = p + 1;
890 if (wld_prctl( 15 /* PR_SET_NAME */, (int)name ) == -1) return;
892 /* find the end of the argv array and move everything down */
893 end = argv[argc - 1];
894 while (*end) end++;
895 off = argv[1] - argv[0];
896 for (p = argv[1]; p <= end; p++) *(p - off) = *p;
897 wld_memset( end - off, 0, off );
898 for (i = 1; i < argc; i++) argv[i] -= off;
903 * wld_start
905 * Repeat the actions the kernel would do when loading a dynamically linked .so
906 * Load the binary and then its ELF interpreter.
907 * Note, we assume that the binary is a dynamically linked ELF shared object.
909 void* wld_start( void **stack )
911 int i, *pargc;
912 char **argv, **p;
913 char *interp, *reserve = NULL;
914 ElfW(auxv_t) new_av[12], delete_av[3], *av;
915 struct wld_link_map main_binary_map, ld_so_map;
916 struct wine_preload_info **wine_main_preload_info;
918 pargc = *stack;
919 argv = (char **)pargc + 1;
920 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
922 /* skip over the parameters */
923 p = argv + *pargc + 1;
925 /* skip over the environment */
926 while (*p)
928 static const char res[] = "WINEPRELOADRESERVE=";
929 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
930 p++;
933 av = (ElfW(auxv_t)*) (p+1);
934 page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
935 page_mask = page_size - 1;
937 preloader_start = (char *)_start - ((unsigned int)_start & page_mask);
938 preloader_end = (char *)((unsigned int)(_end + page_mask) & ~page_mask);
940 #ifdef DUMP_AUX_INFO
941 wld_printf( "stack = %x\n", *stack );
942 for( i = 0; i < *pargc; i++ ) wld_printf("argv[%x] = %s\n", i, argv[i]);
943 dump_auxiliary( av );
944 #endif
946 /* reserve memory that Wine needs */
947 if (reserve) preload_reserve( reserve );
948 for (i = 0; preload_info[i].size; i++)
949 wld_mmap( preload_info[i].addr, preload_info[i].size,
950 PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 );
952 /* add an executable page at the top of the address space to defeat
953 * broken no-exec protections that play with the code selector limit */
954 wld_mprotect( (char *)0x80000000 - page_size, page_size, PROT_EXEC | PROT_READ );
956 /* load the main binary */
957 map_so_lib( argv[1], &main_binary_map );
959 /* load the ELF interpreter */
960 interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
961 map_so_lib( interp, &ld_so_map );
963 /* store pointer to the preload info into the appropriate main binary variable */
964 wine_main_preload_info = find_symbol( main_binary_map.l_phdr, main_binary_map.l_phnum,
965 "wine_main_preload_info" );
966 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
967 else wld_printf( "wine_main_preload_info not found\n" );
969 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
970 SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
971 SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
972 SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
973 SET_NEW_AV( 3, AT_PAGESZ, page_size );
974 SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
975 SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
976 SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
977 SET_NEW_AV( 7, AT_UID, get_auxiliary( av, AT_UID, wld_getuid() ) );
978 SET_NEW_AV( 8, AT_EUID, get_auxiliary( av, AT_EUID, wld_geteuid() ) );
979 SET_NEW_AV( 9, AT_GID, get_auxiliary( av, AT_GID, wld_getgid() ) );
980 SET_NEW_AV(10, AT_EGID, get_auxiliary( av, AT_EGID, wld_getegid() ) );
981 SET_NEW_AV(11, AT_NULL, 0 );
982 #undef SET_NEW_AV
984 i = 0;
985 /* delete sysinfo values if addresses conflict */
986 if (is_in_preload_range( av, AT_SYSINFO )) delete_av[i++].a_type = AT_SYSINFO;
987 if (is_in_preload_range( av, AT_SYSINFO_EHDR )) delete_av[i++].a_type = AT_SYSINFO_EHDR;
988 delete_av[i].a_type = AT_NULL;
990 /* get rid of first argument */
991 set_process_name( *pargc, argv );
992 pargc[1] = pargc[0] - 1;
993 *stack = pargc + 1;
995 set_auxiliary_values( av, new_av, delete_av, stack );
997 #ifdef DUMP_AUX_INFO
998 wld_printf("new stack = %x\n", *stack);
999 wld_printf("jumping to %x\n", ld_so_map.l_entry);
1000 #endif
1002 return (void *)ld_so_map.l_entry;