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
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
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
64 #include "wine/port.h"
70 #include <sys/types.h>
71 #ifdef HAVE_SYS_STAT_H
72 # include <sys/stat.h>
75 #ifdef HAVE_SYS_MMAN_H
76 # include <sys/mman.h>
78 #ifdef HAVE_SYS_SYSCALL_H
79 # include <sys/syscall.h>
90 #ifdef HAVE_SYS_LINK_H
91 # include <sys/link.h>
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
103 #define MAP_COPY MAP_PRIVATE
105 #ifndef MAP_NORESERVE
106 #define MAP_NORESERVE 0
109 static struct wine_preload_info preload_info[] =
111 { (void *)0x00000000, 0x60000000 }, /* low memory area */
112 { (void *)0x7f000000, 0x02000000 }, /* top-down allocations + shared heap */
113 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
114 { 0, 0 } /* end of list */
122 /* older systems may not define these */
128 #define AT_SYSINFO 32
130 #ifndef AT_SYSINFO_EHDR
131 #define AT_SYSINFO_EHDR 33
135 #define DT_GNU_HASH 0x6ffffef5
138 static unsigned int page_size, page_mask;
139 static char *preloader_start, *preloader_end;
141 struct wld_link_map {
148 ElfW(Addr) l_map_start, l_map_end;
154 * The __bb_init_func is an empty function only called when file is
155 * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
156 * function is normally provided by libc's startup files, but since we
157 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
158 * provide our own (empty) version, otherwise linker fails.
160 void __bb_init_func(void) { return; }
162 /* similar to the above but for -fstack-protector */
163 void *__stack_chk_guard = 0;
164 void __stack_chk_fail(void) { return; }
166 /* data for setting up the glibc-style thread-local storage in %gs */
168 static int thread_data[256];
172 /* this is the kernel modify_ldt struct */
173 unsigned int entry_number;
174 unsigned long base_addr;
176 unsigned int seg_32bit : 1;
177 unsigned int contents : 2;
178 unsigned int read_exec_only : 1;
179 unsigned int limit_in_pages : 1;
180 unsigned int seg_not_present : 1;
181 unsigned int useable : 1;
182 unsigned int garbage : 25;
183 } thread_ldt = { -1, (unsigned long)thread_data, 0xfffff, 1, 0, 0, 1, 0, 1, 0 };
187 * The _start function is the entry and exit point of this program
189 * It calls wld_start, passing a pointer to the args it receives
190 * then jumps to the address wld_start returns.
194 __ASM_GLOBAL_FUNC(_start,
195 "\tmovl $243,%eax\n" /* SYS_set_thread_area */
196 "\tmovl $thread_ldt,%ebx\n"
197 "\tint $0x80\n" /* allocate gs segment */
200 "\tmovl thread_ldt,%eax\n" /* thread_ldt.entry_number */
204 "\tmov %ax,%fs\n" /* set %fs too so libwine can retrieve it later on */
205 "1:\tmovl %esp,%eax\n"
206 "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */
207 "\tpushl %eax\n" /* orig stack pointer */
208 "\tpushl %esp\n" /* ptr to orig stack pointer */
210 "\tpopl %ecx\n" /* remove ptr to stack pointer */
211 "\tpopl %esp\n" /* new stack pointer */
212 "\tpush %eax\n" /* ELF interpreter entry point */
218 /* wrappers for Linux system calls */
220 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
222 static inline __attribute__((noreturn)) void wld_exit( int code )
224 for (;;) /* avoid warning */
225 __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
226 : : "a" (SYS_exit), "r" (code) );
229 static inline int wld_open( const char *name, int flags )
232 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
233 : "=a" (ret) : "0" (SYS_open), "r" (name), "c" (flags) );
234 return SYSCALL_RET(ret);
237 static inline int wld_close( int fd )
240 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
241 : "=a" (ret) : "0" (SYS_close), "r" (fd) );
242 return SYSCALL_RET(ret);
245 static inline ssize_t wld_read( int fd, void *buffer, size_t len )
248 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
250 : "0" (SYS_read), "r" (fd), "c" (buffer), "d" (len)
252 return SYSCALL_RET(ret);
255 static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
258 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
259 : "=a" (ret) : "0" (SYS_write), "r" (fd), "c" (buffer), "d" (len) );
260 return SYSCALL_RET(ret);
263 static inline int wld_mprotect( const void *addr, size_t len, int prot )
266 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
267 : "=a" (ret) : "0" (SYS_mprotect), "r" (addr), "c" (len), "d" (prot) );
268 return SYSCALL_RET(ret);
271 static void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset )
290 args.offset = offset;
291 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
292 : "=a" (ret) : "0" (SYS_mmap), "q" (&args) : "memory" );
293 return (void *)SYSCALL_RET(ret);
296 static inline uid_t wld_getuid(void)
299 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getuid) );
303 static inline uid_t wld_geteuid(void)
306 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_geteuid) );
310 static inline gid_t wld_getgid(void)
313 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getgid) );
317 static inline gid_t wld_getegid(void)
320 __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getegid) );
324 static inline int wld_prctl( int code, int arg )
327 __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
328 : "=a" (ret) : "0" (SYS_prctl), "r" (code), "c" (arg) );
329 return SYSCALL_RET(ret);
333 /* replacement for libc functions */
335 static int wld_strcmp( const char *str1, const char *str2 )
337 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
338 return *str1 - *str2;
341 static int wld_strncmp( const char *str1, const char *str2, size_t len )
343 if (len <= 0) return 0;
344 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
345 return *str1 - *str2;
348 static inline void *wld_memset( void *dest, int val, size_t len )
351 while (len--) *dst++ = val;
356 * wld_printf - just the basics
358 * %x prints a hex number
361 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
363 static const char hex_chars[16] = "0123456789abcdef";
375 unsigned int x = va_arg( args, unsigned int );
377 *str++ = hex_chars[(x>>(i*4))&0xf];
381 char *s = va_arg( args, char * );
395 static void wld_printf(const char *fmt, ... )
401 va_start( args, fmt );
402 len = wld_vsprintf(buffer, fmt, args );
404 wld_write(2, buffer, len);
407 static __attribute__((noreturn)) void fatal_error(const char *fmt, ... )
413 va_start( args, fmt );
414 len = wld_vsprintf(buffer, fmt, args );
416 wld_write(2, buffer, len);
422 * Dump interesting bits of the ELF auxv_t structure that is passed
423 * as the 4th parameter to the _start function
425 static void dump_auxiliary( ElfW(auxv_t) *av )
427 #define NAME(at) { at, #at }
428 static const struct { int val; const char *name; } names[] =
444 NAME(AT_SYSINFO_EHDR),
452 for ( ; av->a_type != AT_NULL; av++)
454 for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
455 if (names[i].name) wld_printf("%s = %x\n", names[i].name, av->a_un.a_val);
456 else wld_printf( "%x = %x\n", av->a_type, av->a_un.a_val );
462 * set_auxiliary_values
464 * Set the new auxiliary values
466 static void set_auxiliary_values( ElfW(auxv_t) *av, const ElfW(auxv_t) *new_av,
467 const ElfW(auxv_t) *delete_av, void **stack )
469 int i, j, av_count = 0, new_count = 0, delete_count = 0;
472 /* count how many aux values we have already */
473 while (av[av_count].a_type != AT_NULL) av_count++;
475 /* delete unwanted values */
476 for (j = 0; delete_av[j].a_type != AT_NULL; j++)
478 for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
480 av[i].a_type = av[av_count-1].a_type;
481 av[i].a_un.a_val = av[av_count-1].a_un.a_val;
482 av[--av_count].a_type = AT_NULL;
488 /* count how many values we have in new_av that aren't in av */
489 for (j = 0; new_av[j].a_type != AT_NULL; j++)
491 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
492 if (i == av_count) new_count++;
495 src = (char *)*stack;
496 dst = src - (new_count - delete_count) * sizeof(*av);
497 if (new_count > delete_count) /* need to make room for the extra values */
499 int len = (char *)(av + av_count + 1) - src;
500 for (i = 0; i < len; i++) dst[i] = src[i];
502 else if (new_count < delete_count) /* get rid of unused values */
504 int len = (char *)(av + av_count + 1) - dst;
505 for (i = len - 1; i >= 0; i--) dst[i] = src[i];
508 av -= (new_count - delete_count);
510 /* now set the values */
511 for (j = 0; new_av[j].a_type != AT_NULL; j++)
513 for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
514 if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
517 av[av_count].a_type = new_av[j].a_type;
518 av[av_count].a_un.a_val = new_av[j].a_un.a_val;
524 wld_printf("New auxiliary info:\n");
525 dump_auxiliary( av );
532 * Get a field of the auxiliary structure
534 static int get_auxiliary( ElfW(auxv_t) *av, int type, int def_val )
536 for ( ; av->a_type != AT_NULL; av++)
537 if( av->a_type == type ) return av->a_un.a_val;
544 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
546 * This function maps the segments from an ELF object, and optionally
547 * stores information about the mapping into the auxv_t structure.
549 static void map_so_lib( const char *name, struct wld_link_map *l)
552 unsigned char buf[0x800];
553 ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
554 ElfW(Phdr) *phdr, *ph;
555 /* Scan the program header table, collecting its load commands. */
558 ElfW(Addr) mapstart, mapend, dataend, allocend;
562 size_t nloadcmds = 0, maplength;
564 fd = wld_open( name, O_RDONLY );
565 if (fd == -1) fatal_error("%s: could not open\n", name );
567 if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
568 fatal_error("%s: failed to read ELF header\n", name);
570 phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
572 if( ( header->e_ident[0] != 0x7f ) ||
573 ( header->e_ident[1] != 'E' ) ||
574 ( header->e_ident[2] != 'L' ) ||
575 ( header->e_ident[3] != 'F' ) )
576 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
578 if( header->e_machine != EM_386 )
579 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
581 if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
582 fatal_error( "%s: oops... not enough space for load commands\n", name );
584 maplength = header->e_phnum * sizeof (ElfW(Phdr));
585 if (header->e_phoff + maplength > sizeof(buf))
586 fatal_error( "%s: oops... not enough space for ELF headers\n", name );
591 l->l_phnum = header->e_phnum;
592 l->l_entry = header->e_entry;
595 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
599 wld_printf( "ph = %x\n", ph );
600 wld_printf( " p_type = %x\n", ph->p_type );
601 wld_printf( " p_flags = %x\n", ph->p_flags );
602 wld_printf( " p_offset = %x\n", ph->p_offset );
603 wld_printf( " p_vaddr = %x\n", ph->p_vaddr );
604 wld_printf( " p_paddr = %x\n", ph->p_paddr );
605 wld_printf( " p_filesz = %x\n", ph->p_filesz );
606 wld_printf( " p_memsz = %x\n", ph->p_memsz );
607 wld_printf( " p_align = %x\n", ph->p_align );
612 /* These entries tell us where to find things once the file's
613 segments are mapped in. We record the addresses it says
614 verbatim, and later correct for the run-time load address. */
616 l->l_ld = (void *) ph->p_vaddr;
617 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
621 l->l_phdr = (void *) ph->p_vaddr;
626 if ((ph->p_align & page_mask) != 0)
627 fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
629 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
630 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
632 c = &loadcmds[nloadcmds++];
633 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
634 c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
635 c->dataend = ph->p_vaddr + ph->p_filesz;
636 c->allocend = ph->p_vaddr + ph->p_memsz;
637 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
640 if (ph->p_flags & PF_R)
641 c->prot |= PROT_READ;
642 if (ph->p_flags & PF_W)
643 c->prot |= PROT_WRITE;
644 if (ph->p_flags & PF_X)
645 c->prot |= PROT_EXEC;
650 l->l_interp = ph->p_vaddr;
655 * We don't need to set anything up because we're
656 * emulating the kernel, not ld-linux.so.2
657 * The ELF loader will set up the TLS data itself.
666 /* Now process the load commands and map segments into memory. */
669 /* Length of the sections to be loaded. */
670 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
672 if( header->e_type == ET_DYN )
675 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
676 - MAP_BASE_ADDR (l));
678 /* Remember which part of the address space this object uses. */
679 l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
680 c->prot, MAP_COPY | MAP_FILE,
682 /* wld_printf("set : offset = %x\n", c->mapoff); */
683 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
685 l->l_map_end = l->l_map_start + maplength;
686 l->l_addr = l->l_map_start - c->mapstart;
688 wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
689 loadcmds[nloadcmds - 1].allocend - c->mapend,
696 if ((char *)c->mapstart + maplength > preloader_start &&
697 (char *)c->mapstart <= preloader_end)
698 fatal_error( "%s: binary overlaps preloader (%x-%x)\n",
699 name, c->mapstart, (char *)c->mapstart + maplength );
701 ELF_FIXED_ADDRESS (loader, c->mapstart);
704 /* Remember which part of the address space this object uses. */
705 l->l_map_start = c->mapstart + l->l_addr;
706 l->l_map_end = l->l_map_start + maplength;
708 while (c < &loadcmds[nloadcmds])
710 if (c->mapend > c->mapstart)
711 /* Map the segment contents from the file. */
712 wld_mmap ((void *) (l->l_addr + c->mapstart),
713 c->mapend - c->mapstart, c->prot,
714 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
718 && (ElfW(Off)) c->mapoff <= header->e_phoff
719 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
720 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
721 /* Found the program header in this segment. */
722 l->l_phdr = (void *)(unsigned int) (c->mapstart + header->e_phoff - c->mapoff);
724 if (c->allocend > c->dataend)
726 /* Extra zero pages should appear at the end of this segment,
727 after the data mapped from the file. */
728 ElfW(Addr) zero, zeroend, zeropage;
730 zero = l->l_addr + c->dataend;
731 zeroend = l->l_addr + c->allocend;
732 zeropage = (zero + page_mask) & ~page_mask;
735 * This is different from the dl-load load...
736 * ld-linux.so.2 relies on the whole page being zero'ed
738 zeroend = (zeroend + page_mask) & ~page_mask;
740 if (zeroend < zeropage)
742 /* All the extra data is in the last page of the segment.
743 We can just zero it. */
749 /* Zero the final part of the last page of the segment. */
750 if ((c->prot & PROT_WRITE) == 0)
753 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
755 wld_memset ((void *) zero, '\0', zeropage - zero);
756 if ((c->prot & PROT_WRITE) == 0)
757 wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
760 if (zeroend > zeropage)
762 /* Map the remaining zero pages in from the zero fill FD. */
764 mapat = wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
765 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
773 if (l->l_phdr == NULL) fatal_error("no program header\n");
775 l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
776 l->l_entry += l->l_addr;
782 static unsigned int elf_hash( const char *name )
784 unsigned int hi, hash = 0;
787 hash = (hash << 4) + (unsigned char)*name++;
788 hi = hash & 0xf0000000;
795 static unsigned int gnu_hash( const char *name )
797 unsigned int h = 5381;
798 while (*name) h = h * 33 + (unsigned char)*name++;
803 * Find a symbol in the symbol table of the executable loaded
805 static void *find_symbol( const ElfW(Phdr) *phdr, int num, const char *var, int type )
807 const ElfW(Dyn) *dyn = NULL;
808 const ElfW(Phdr) *ph;
809 const ElfW(Sym) *symtab = NULL;
810 const Elf_Symndx *hashtab = NULL;
811 const Elf32_Word *gnu_hashtab = NULL;
812 const char *strings = NULL;
815 /* check the values */
817 wld_printf("%x %x\n", phdr, num );
819 if( ( phdr == NULL ) || ( num == 0 ) )
821 wld_printf("could not find PT_DYNAMIC header entry\n");
825 /* parse the (already loaded) ELF executable's header */
826 for (ph = phdr; ph < &phdr[num]; ++ph)
828 if( PT_DYNAMIC == ph->p_type )
830 dyn = (void *) ph->p_vaddr;
831 num = ph->p_memsz / sizeof (Elf32_Dyn);
835 if( !dyn ) return NULL;
839 if( dyn->d_tag == DT_STRTAB )
840 strings = (const char*) dyn->d_un.d_ptr;
841 if( dyn->d_tag == DT_SYMTAB )
842 symtab = (const ElfW(Sym) *)dyn->d_un.d_ptr;
843 if( dyn->d_tag == DT_HASH )
844 hashtab = (const Elf_Symndx *)dyn->d_un.d_ptr;
845 if( dyn->d_tag == DT_GNU_HASH )
846 gnu_hashtab = (const Elf32_Word *)dyn->d_un.d_ptr;
848 wld_printf("%x %x\n", dyn->d_tag, dyn->d_un.d_ptr );
853 if( (!symtab) || (!strings) ) return NULL;
855 if (gnu_hashtab) /* new style hash table */
857 const unsigned int hash = gnu_hash(var);
858 const Elf32_Word nbuckets = gnu_hashtab[0];
859 const Elf32_Word symbias = gnu_hashtab[1];
860 const Elf32_Word nwords = gnu_hashtab[2];
861 const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
862 const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
863 const Elf32_Word *chains = buckets + nbuckets - symbias;
865 if (!(idx = buckets[hash % nbuckets])) return NULL;
868 if ((chains[idx] & ~1u) == (hash & ~1u) &&
869 symtab[idx].st_info == ELF32_ST_INFO( STB_GLOBAL, type ) &&
870 !wld_strcmp( strings + symtab[idx].st_name, var ))
872 } while (!(chains[idx++] & 1u));
874 else if (hashtab) /* old style hash table */
876 const unsigned int hash = elf_hash(var);
877 const Elf_Symndx nbuckets = hashtab[0];
878 const Elf_Symndx *buckets = hashtab + 2;
879 const Elf_Symndx *chains = buckets + nbuckets;
881 for (idx = buckets[hash % nbuckets]; idx != STN_UNDEF; idx = chains[idx])
883 if (symtab[idx].st_info == ELF32_ST_INFO( STB_GLOBAL, type ) &&
884 !wld_strcmp( strings + symtab[idx].st_name, var ))
892 wld_printf("Found %s -> %x\n", strings + symtab[idx].st_name, symtab[idx].st_value );
894 return (void *)symtab[idx].st_value;
900 * Reserve a range specified in string format
902 static void preload_reserve( const char *str )
905 unsigned long result = 0;
906 void *start = NULL, *end = NULL;
909 for (p = str; *p; p++)
911 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
912 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
913 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
916 if (!first) goto error;
917 start = (void *)(result & ~page_mask);
923 if (!first) end = (void *)((result + page_mask) & ~page_mask);
924 else if (result) goto error; /* single value '0' is allowed */
927 if (end <= start) start = end = NULL;
928 else if ((char *)end > preloader_start &&
929 (char *)start <= preloader_end)
931 wld_printf( "WINEPRELOADRESERVE range %x-%x overlaps preloader %x-%x\n",
932 start, end, preloader_start, preloader_end );
936 /* check for overlap with low memory area */
937 if ((char *)end <= (char *)preload_info[0].addr + preload_info[0].size)
939 else if ((char *)start < (char *)preload_info[0].addr + preload_info[0].size)
940 start = (char *)preload_info[0].addr + preload_info[0].size;
942 /* entry 2 is for the PE exe */
943 preload_info[2].addr = start;
944 preload_info[2].size = (char *)end - (char *)start;
948 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
952 * is_in_preload_range
954 * Check if address of the given aux value is in one of the reserved ranges
956 static int is_in_preload_range( const ElfW(auxv_t) *av, int type )
960 while (av->a_type != type && av->a_type != AT_NULL) av++;
962 if (av->a_type == type)
964 for (i = 0; preload_info[i].size; i++)
966 if ((char *)av->a_un.a_val >= (char *)preload_info[i].addr &&
967 (char *)av->a_un.a_val < (char *)preload_info[i].addr + preload_info[i].size)
974 /* set the process name if supported */
975 static void set_process_name( int argc, char *argv[] )
978 char *p, *name, *end;
980 /* set the process short name */
981 for (p = name = argv[1]; *p; p++) if (p[0] == '/' && p[1]) name = p + 1;
982 if (wld_prctl( 15 /* PR_SET_NAME */, (int)name ) == -1) return;
984 /* find the end of the argv array and move everything down */
985 end = argv[argc - 1];
987 off = argv[1] - argv[0];
988 for (p = argv[1]; p <= end; p++) *(p - off) = *p;
989 wld_memset( end - off, 0, off );
990 for (i = 1; i < argc; i++) argv[i] -= off;
997 * Repeat the actions the kernel would do when loading a dynamically linked .so
998 * Load the binary and then its ELF interpreter.
999 * Note, we assume that the binary is a dynamically linked ELF shared object.
1001 void* wld_start( void **stack )
1005 char *interp, *reserve = NULL;
1006 ElfW(auxv_t) new_av[12], delete_av[3], *av;
1007 struct wld_link_map main_binary_map, ld_so_map;
1008 struct wine_preload_info **wine_main_preload_info;
1011 argv = (char **)pargc + 1;
1012 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
1014 /* skip over the parameters */
1015 p = argv + *pargc + 1;
1017 /* skip over the environment */
1020 static const char res[] = "WINEPRELOADRESERVE=";
1021 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
1025 av = (ElfW(auxv_t)*) (p+1);
1026 page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
1027 page_mask = page_size - 1;
1029 preloader_start = (char *)_start - ((unsigned int)_start & page_mask);
1030 preloader_end = (char *)((unsigned int)(_end + page_mask) & ~page_mask);
1032 #ifdef DUMP_AUX_INFO
1033 wld_printf( "stack = %x\n", *stack );
1034 for( i = 0; i < *pargc; i++ ) wld_printf("argv[%x] = %s\n", i, argv[i]);
1035 dump_auxiliary( av );
1038 /* reserve memory that Wine needs */
1039 if (reserve) preload_reserve( reserve );
1040 for (i = 0; preload_info[i].size; i++)
1041 wld_mmap( preload_info[i].addr, preload_info[i].size,
1042 PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 );
1044 /* add an executable page at the top of the address space to defeat
1045 * broken no-exec protections that play with the code selector limit */
1046 wld_mprotect( (char *)0x80000000 - page_size, page_size, PROT_EXEC | PROT_READ );
1048 /* load the main binary */
1049 map_so_lib( argv[1], &main_binary_map );
1051 /* load the ELF interpreter */
1052 interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
1053 map_so_lib( interp, &ld_so_map );
1055 /* store pointer to the preload info into the appropriate main binary variable */
1056 wine_main_preload_info = find_symbol( main_binary_map.l_phdr, main_binary_map.l_phnum,
1057 "wine_main_preload_info", STT_OBJECT );
1058 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
1059 else wld_printf( "wine_main_preload_info not found\n" );
1061 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
1062 SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
1063 SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
1064 SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
1065 SET_NEW_AV( 3, AT_PAGESZ, page_size );
1066 SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
1067 SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
1068 SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
1069 SET_NEW_AV( 7, AT_UID, get_auxiliary( av, AT_UID, wld_getuid() ) );
1070 SET_NEW_AV( 8, AT_EUID, get_auxiliary( av, AT_EUID, wld_geteuid() ) );
1071 SET_NEW_AV( 9, AT_GID, get_auxiliary( av, AT_GID, wld_getgid() ) );
1072 SET_NEW_AV(10, AT_EGID, get_auxiliary( av, AT_EGID, wld_getegid() ) );
1073 SET_NEW_AV(11, AT_NULL, 0 );
1077 /* delete sysinfo values if addresses conflict */
1078 if (is_in_preload_range( av, AT_SYSINFO )) delete_av[i++].a_type = AT_SYSINFO;
1079 if (is_in_preload_range( av, AT_SYSINFO_EHDR )) delete_av[i++].a_type = AT_SYSINFO_EHDR;
1080 delete_av[i].a_type = AT_NULL;
1082 /* get rid of first argument */
1083 set_process_name( *pargc, argv );
1084 pargc[1] = pargc[0] - 1;
1087 set_auxiliary_values( av, new_av, delete_av, stack );
1089 #ifdef DUMP_AUX_INFO
1090 wld_printf("new stack = %x\n", *stack);
1091 wld_printf("jumping to %x\n", ld_so_map.l_entry);
1094 return (void *)ld_so_map.l_entry;