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
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>
73 #ifdef HAVE_SYS_MMAN_H
74 # include <sys/mman.h>
76 #ifdef HAVE_SYS_SYSCALL_H
77 # include <sys/syscall.h>
88 #ifdef HAVE_SYS_LINK_H
89 # include <sys/link.h>
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
101 #define MAP_COPY MAP_PRIVATE
103 #ifndef MAP_NORESERVE
104 #define MAP_NORESERVE 0
107 static struct wine_preload_info preload_info
[] =
109 { (void *)0x00000000, 0x00110000 }, /* DOS area */
110 { (void *)0x80000000, 0x01000000 }, /* shared heap */
111 { (void *)0x00110000, 0x0fef0000 }, /* default PE exe range (may be set with WINEPRELOADRESERVE) */
112 { 0, 0 } /* end of list */
120 /* older systems may not define these */
126 #define AT_SYSINFO 32
128 #ifndef AT_SYSINFO_EHDR
129 #define AT_SYSINFO_EHDR 33
132 static unsigned int page_size
, page_mask
;
133 static char *preloader_start
, *preloader_end
;
135 struct wld_link_map
{
142 ElfW(Addr
) l_map_start
, l_map_end
;
148 * The _start function is the entry and exit point of this program
150 * It calls wld_start, passing a pointer to the args it receives
151 * then jumps to the address wld_start returns.
155 __ASM_GLOBAL_FUNC(_start
,
157 "\tleal -128(%esp),%esp\n" /* allocate some space for extra aux values */
158 "\tpushl %eax\n" /* orig stack pointer */
159 "\tpushl %esp\n" /* ptr to orig stack pointer */
161 "\tpopl %ecx\n" /* remove ptr to stack pointer */
162 "\tpopl %esp\n" /* new stack pointer */
163 "\tpush %eax\n" /* ELF interpreter entry point */
169 /* wrappers for Linux system calls */
171 #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
173 static inline __attribute__((noreturn
)) void wld_exit( int code
)
175 for (;;) /* avoid warning */
176 __asm__
__volatile__( "int $0x80" : : "a" (SYS_exit
), "b" (code
) );
179 static inline int wld_open( const char *name
, int flags
)
182 __asm__
__volatile__( "int $0x80" : "=a" (ret
) : "0" (SYS_open
), "b" (name
), "c" (flags
) );
183 return SYSCALL_RET(ret
);
186 static inline int wld_close( int fd
)
189 __asm__
__volatile__( "int $0x80" : "=a" (ret
) : "0" (SYS_close
), "b" (fd
) );
190 return SYSCALL_RET(ret
);
193 static inline ssize_t
wld_read( int fd
, void *buffer
, size_t len
)
196 __asm__
__volatile__( "int $0x80" : "=a" (ret
)
197 : "0" (SYS_read
), "b" (fd
), "c" (buffer
), "d" (len
)
199 return SYSCALL_RET(ret
);
202 static inline ssize_t
wld_write( int fd
, const void *buffer
, size_t len
)
205 __asm__
__volatile__( "int $0x80" : "=a" (ret
)
206 : "0" (SYS_write
), "b" (fd
), "c" (buffer
), "d" (len
) );
207 return SYSCALL_RET(ret
);
210 static inline int wld_mprotect( const void *addr
, size_t len
, int prot
)
213 __asm__
__volatile__( "int $0x80" : "=a" (ret
) : "0" (SYS_mprotect
), "b" (addr
), "c" (len
), "d" (prot
) );
214 return SYSCALL_RET(ret
);
217 static void *wld_mmap( void *start
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
236 args
.offset
= offset
;
237 __asm__
__volatile__( "int $0x80" : "=a" (ret
) : "0" (SYS_mmap
), "b" (&args
) : "memory" );
238 return (void *)SYSCALL_RET(ret
);
241 static inline uid_t
wld_getuid(void)
244 __asm__( "int $0x80" : "=a" (ret
) : "0" (SYS_getuid
) );
248 static inline uid_t
wld_geteuid(void)
251 __asm__( "int $0x80" : "=a" (ret
) : "0" (SYS_geteuid
) );
255 static inline gid_t
wld_getgid(void)
258 __asm__( "int $0x80" : "=a" (ret
) : "0" (SYS_getgid
) );
262 static inline gid_t
wld_getegid(void)
265 __asm__( "int $0x80" : "=a" (ret
) : "0" (SYS_getegid
) );
270 /* replacement for libc functions */
272 static int wld_strcmp( const char *str1
, const char *str2
)
274 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
275 return *str1
- *str2
;
278 static int wld_strncmp( const char *str1
, const char *str2
, size_t len
)
280 if (len
<= 0) return 0;
281 while ((--len
> 0) && *str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
282 return *str1
- *str2
;
285 static inline void *wld_memset( void *dest
, int val
, size_t len
)
288 while (len
--) *dst
++ = val
;
293 * wld_printf - just the basics
295 * %x prints a hex number
298 static int wld_vsprintf(char *buffer
, const char *fmt
, va_list args
)
300 static const char hex_chars
[16] = "0123456789abcdef";
312 unsigned int x
= va_arg( args
, unsigned int );
314 *str
++ = hex_chars
[(x
>>(i
*4))&0xf];
318 char *s
= va_arg( args
, char * );
332 static void wld_printf(const char *fmt
, ... )
338 va_start( args
, fmt
);
339 len
= wld_vsprintf(buffer
, fmt
, args
);
341 wld_write(2, buffer
, len
);
344 static __attribute__((noreturn
)) void fatal_error(const char *fmt
, ... )
350 va_start( args
, fmt
);
351 len
= wld_vsprintf(buffer
, fmt
, args
);
353 wld_write(2, buffer
, len
);
359 * Dump interesting bits of the ELF auxv_t structure that is passed
360 * as the 4th parameter to the _start function
362 static void dump_auxiliary( ElfW(auxv_t
) *av
)
364 #define NAME(at) { at, #at }
365 static const struct { int val
; const char *name
; } names
[] =
381 NAME(AT_SYSINFO_EHDR
),
389 for ( ; av
->a_type
!= AT_NULL
; av
++)
391 for (i
= 0; names
[i
].name
; i
++) if (names
[i
].val
== av
->a_type
) break;
392 if (names
[i
].name
) wld_printf("%s = %x\n", names
[i
].name
, av
->a_un
.a_val
);
393 else wld_printf( "%x = %x\n", av
->a_type
, av
->a_un
.a_val
);
399 * set_auxiliary_values
401 * Set the new auxiliary values
403 static void set_auxiliary_values( ElfW(auxv_t
) *av
, const ElfW(auxv_t
) *new_av
,
404 const ElfW(auxv_t
) *delete_av
, void **stack
)
406 int i
, j
, av_count
= 0, new_count
= 0, delete_count
= 0;
409 /* count how many aux values we have already */
410 while (av
[av_count
].a_type
!= AT_NULL
) av_count
++;
412 /* delete unwanted values */
413 for (j
= 0; delete_av
[j
].a_type
!= AT_NULL
; j
++)
415 for (i
= 0; i
< av_count
; i
++) if (av
[i
].a_type
== delete_av
[j
].a_type
)
417 av
[i
].a_type
= av
[av_count
-1].a_type
;
418 av
[i
].a_un
.a_val
= av
[av_count
-1].a_un
.a_val
;
419 av
[--av_count
].a_type
= AT_NULL
;
425 /* count how many values we have in new_av that aren't in av */
426 for (j
= 0; new_av
[j
].a_type
!= AT_NULL
; j
++)
428 for (i
= 0; i
< av_count
; i
++) if (av
[i
].a_type
== new_av
[j
].a_type
) break;
429 if (i
== av_count
) new_count
++;
432 src
= (char *)*stack
;
433 dst
= src
- (new_count
- delete_count
) * sizeof(*av
);
434 if (new_count
> delete_count
) /* need to make room for the extra values */
436 int len
= (char *)(av
+ av_count
+ 1) - src
;
437 for (i
= 0; i
< len
; i
++) dst
[i
] = src
[i
];
439 else if (new_count
< delete_count
) /* get rid of unused values */
441 int len
= (char *)(av
+ av_count
+ 1) - dst
;
442 for (i
= len
- 1; i
>= 0; i
--) dst
[i
] = src
[i
];
445 av
-= (new_count
- delete_count
);
447 /* now set the values */
448 for (j
= 0; new_av
[j
].a_type
!= AT_NULL
; j
++)
450 for (i
= 0; i
< av_count
; i
++) if (av
[i
].a_type
== new_av
[j
].a_type
) break;
451 if (i
< av_count
) av
[i
].a_un
.a_val
= new_av
[j
].a_un
.a_val
;
454 av
[av_count
].a_type
= new_av
[j
].a_type
;
455 av
[av_count
].a_un
.a_val
= new_av
[j
].a_un
.a_val
;
461 wld_printf("New auxiliary info:\n");
462 dump_auxiliary( av
);
469 * Get a field of the auxiliary structure
471 static int get_auxiliary( ElfW(auxv_t
) *av
, int type
, int def_val
)
473 for ( ; av
->a_type
!= AT_NULL
; av
++)
474 if( av
->a_type
== type
) return av
->a_un
.a_val
;
481 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
483 * This function maps the segments from an ELF object, and optionally
484 * stores information about the mapping into the auxv_t structure.
486 static void map_so_lib( const char *name
, struct wld_link_map
*l
)
489 unsigned char buf
[0x800];
490 ElfW(Ehdr
) *header
= (ElfW(Ehdr
)*)buf
;
491 ElfW(Phdr
) *phdr
, *ph
;
492 /* Scan the program header table, collecting its load commands. */
495 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
499 size_t nloadcmds
= 0, maplength
;
501 fd
= wld_open( name
, O_RDONLY
);
502 if (fd
== -1) fatal_error("%s: could not open\n", name
);
504 if (wld_read( fd
, buf
, sizeof(buf
) ) != sizeof(buf
))
505 fatal_error("%s: failed to read ELF header\n", name
);
507 phdr
= (void*) (((unsigned char*)buf
) + header
->e_phoff
);
509 if( ( header
->e_ident
[0] != 0x7f ) ||
510 ( header
->e_ident
[1] != 'E' ) ||
511 ( header
->e_ident
[2] != 'L' ) ||
512 ( header
->e_ident
[3] != 'F' ) )
513 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name
);
515 if( header
->e_machine
!= EM_386
)
516 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name
);
518 if (header
->e_phnum
> sizeof(loadcmds
)/sizeof(loadcmds
[0]))
519 fatal_error( "%s: oops... not enough space for load commands\n", name
);
521 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
522 if (header
->e_phoff
+ maplength
> sizeof(buf
))
523 fatal_error( "%s: oops... not enough space for ELF headers\n", name
);
528 l
->l_phnum
= header
->e_phnum
;
529 l
->l_entry
= header
->e_entry
;
532 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
536 wld_printf( "ph = %x\n", ph
);
537 wld_printf( " p_type = %x\n", ph
->p_type
);
538 wld_printf( " p_flags = %x\n", ph
->p_flags
);
539 wld_printf( " p_offset = %x\n", ph
->p_offset
);
540 wld_printf( " p_vaddr = %x\n", ph
->p_vaddr
);
541 wld_printf( " p_paddr = %x\n", ph
->p_paddr
);
542 wld_printf( " p_filesz = %x\n", ph
->p_filesz
);
543 wld_printf( " p_memsz = %x\n", ph
->p_memsz
);
544 wld_printf( " p_align = %x\n", ph
->p_align
);
549 /* These entries tell us where to find things once the file's
550 segments are mapped in. We record the addresses it says
551 verbatim, and later correct for the run-time load address. */
553 l
->l_ld
= (void *) ph
->p_vaddr
;
554 l
->l_ldnum
= ph
->p_memsz
/ sizeof (Elf32_Dyn
);
558 l
->l_phdr
= (void *) ph
->p_vaddr
;
563 if ((ph
->p_align
& page_mask
) != 0)
564 fatal_error( "%s: ELF load command alignment not page-aligned\n", name
);
566 if (((ph
->p_vaddr
- ph
->p_offset
) & (ph
->p_align
- 1)) != 0)
567 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name
);
569 c
= &loadcmds
[nloadcmds
++];
570 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
571 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ page_mask
) & ~page_mask
);
572 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
573 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
574 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
577 if (ph
->p_flags
& PF_R
)
578 c
->prot
|= PROT_READ
;
579 if (ph
->p_flags
& PF_W
)
580 c
->prot
|= PROT_WRITE
;
581 if (ph
->p_flags
& PF_X
)
582 c
->prot
|= PROT_EXEC
;
587 l
->l_interp
= ph
->p_vaddr
;
592 * We don't need to set anything up because we're
593 * emulating the kernel, not ld-linux.so.2
594 * The ELF loader will set up the TLS data itself.
603 /* Now process the load commands and map segments into memory. */
606 /* Length of the sections to be loaded. */
607 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
609 if( header
->e_type
== ET_DYN
)
612 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
613 - MAP_BASE_ADDR (l
));
615 /* Remember which part of the address space this object uses. */
616 l
->l_map_start
= (ElfW(Addr
)) wld_mmap ((void *) mappref
, maplength
,
617 c
->prot
, MAP_COPY
| MAP_FILE
,
619 /* wld_printf("set : offset = %x\n", c->mapoff); */
620 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
622 l
->l_map_end
= l
->l_map_start
+ maplength
;
623 l
->l_addr
= l
->l_map_start
- c
->mapstart
;
625 wld_mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
626 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
633 if ((char *)c
->mapstart
+ maplength
> preloader_start
&&
634 (char *)c
->mapstart
<= preloader_end
)
635 fatal_error( "%s: binary overlaps preloader (%x-%x)\n",
636 name
, c
->mapstart
, (char *)c
->mapstart
+ maplength
);
638 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
641 /* Remember which part of the address space this object uses. */
642 l
->l_map_start
= c
->mapstart
+ l
->l_addr
;
643 l
->l_map_end
= l
->l_map_start
+ maplength
;
645 while (c
< &loadcmds
[nloadcmds
])
647 if (c
->mapend
> c
->mapstart
)
648 /* Map the segment contents from the file. */
649 wld_mmap ((void *) (l
->l_addr
+ c
->mapstart
),
650 c
->mapend
- c
->mapstart
, c
->prot
,
651 MAP_FIXED
| MAP_COPY
| MAP_FILE
, fd
, c
->mapoff
);
655 && (ElfW(Off
)) c
->mapoff
<= header
->e_phoff
656 && ((size_t) (c
->mapend
- c
->mapstart
+ c
->mapoff
)
657 >= header
->e_phoff
+ header
->e_phnum
* sizeof (ElfW(Phdr
))))
658 /* Found the program header in this segment. */
659 l
->l_phdr
= (void *)(unsigned int) (c
->mapstart
+ header
->e_phoff
- c
->mapoff
);
661 if (c
->allocend
> c
->dataend
)
663 /* Extra zero pages should appear at the end of this segment,
664 after the data mapped from the file. */
665 ElfW(Addr
) zero
, zeroend
, zeropage
;
667 zero
= l
->l_addr
+ c
->dataend
;
668 zeroend
= l
->l_addr
+ c
->allocend
;
669 zeropage
= (zero
+ page_mask
) & ~page_mask
;
672 * This is different from the dl-load load...
673 * ld-linux.so.2 relies on the whole page being zero'ed
675 zeroend
= (zeroend
+ page_mask
) & ~page_mask
;
677 if (zeroend
< zeropage
)
679 /* All the extra data is in the last page of the segment.
680 We can just zero it. */
686 /* Zero the final part of the last page of the segment. */
687 if ((c
->prot
& PROT_WRITE
) == 0)
690 wld_mprotect ((caddr_t
) (zero
& ~page_mask
), page_size
, c
->prot
|PROT_WRITE
);
692 wld_memset ((void *) zero
, '\0', zeropage
- zero
);
693 if ((c
->prot
& PROT_WRITE
) == 0)
694 wld_mprotect ((caddr_t
) (zero
& ~page_mask
), page_size
, c
->prot
);
697 if (zeroend
> zeropage
)
699 /* Map the remaining zero pages in from the zero fill FD. */
701 mapat
= wld_mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
702 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
710 if (l
->l_phdr
== NULL
) fatal_error("no program header\n");
712 l
->l_phdr
= (void *)((ElfW(Addr
))l
->l_phdr
+ l
->l_addr
);
713 l
->l_entry
+= l
->l_addr
;
720 * Find a symbol in the symbol table of the executable loaded
722 static void *find_symbol( const ElfW(Phdr
) *phdr
, int num
, char *var
)
724 const ElfW(Dyn
) *dyn
= NULL
;
725 const ElfW(Phdr
) *ph
;
726 const ElfW(Sym
) *symtab
= NULL
;
727 const char *strings
= NULL
;
728 uint32_t i
, symtabend
= 0;
730 /* check the values */
732 wld_printf("%x %x\n", phdr
, num
);
734 if( ( phdr
== NULL
) || ( num
== 0 ) )
736 wld_printf("could not find PT_DYNAMIC header entry\n");
740 /* parse the (already loaded) ELF executable's header */
741 for (ph
= phdr
; ph
< &phdr
[num
]; ++ph
)
743 if( PT_DYNAMIC
== ph
->p_type
)
745 dyn
= (void *) ph
->p_vaddr
;
746 num
= ph
->p_memsz
/ sizeof (Elf32_Dyn
);
750 if( !dyn
) return NULL
;
754 if( dyn
->d_tag
== DT_STRTAB
)
755 strings
= (const char*) dyn
->d_un
.d_ptr
;
756 if( dyn
->d_tag
== DT_SYMTAB
)
757 symtab
= (const ElfW(Sym
) *)dyn
->d_un
.d_ptr
;
758 if( dyn
->d_tag
== DT_HASH
)
759 symtabend
= *((const uint32_t *)dyn
->d_un
.d_ptr
+ 1);
761 wld_printf("%x %x\n", dyn
->d_tag
, dyn
->d_un
.d_ptr
);
766 if( (!symtab
) || (!strings
) ) return NULL
;
768 for (i
= 0; i
< symtabend
; i
++)
770 if( ( ELF32_ST_BIND(symtab
[i
].st_info
) == STT_OBJECT
) &&
771 ( 0 == wld_strcmp( strings
+symtab
[i
].st_name
, var
) ) )
774 wld_printf("Found %s -> %x\n", strings
+symtab
[i
].st_name
, symtab
[i
].st_value
);
776 return (void*)symtab
[i
].st_value
;
785 * Reserve a range specified in string format
787 static void preload_reserve( const char *str
)
790 unsigned long result
= 0;
791 void *start
= NULL
, *end
= NULL
;
794 for (p
= str
; *p
; p
++)
796 if (*p
>= '0' && *p
<= '9') result
= result
* 16 + *p
- '0';
797 else if (*p
>= 'a' && *p
<= 'f') result
= result
* 16 + *p
- 'a' + 10;
798 else if (*p
>= 'A' && *p
<= 'F') result
= result
* 16 + *p
- 'A' + 10;
801 if (!first
) goto error
;
802 start
= (void *)(result
& ~page_mask
);
808 if (!first
) end
= (void *)((result
+ page_mask
) & ~page_mask
);
809 else if (result
) goto error
; /* single value '0' is allowed */
812 if (end
<= start
) start
= end
= NULL
;
813 else if ((char *)end
> preloader_start
&&
814 (char *)start
<= preloader_end
)
816 wld_printf( "WINEPRELOADRESERVE range %x-%x overlaps preloader %x-%x\n",
817 start
, end
, preloader_start
, preloader_end
);
821 /* entry 2 is for the PE exe */
822 preload_info
[2].addr
= start
;
823 preload_info
[2].size
= (char *)end
- (char *)start
;
827 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str
);
831 * is_in_preload_range
833 * Check if address of the given aux value is in one of the reserved ranges
835 static int is_in_preload_range( const ElfW(auxv_t
) *av
, int type
)
839 while (av
->a_type
!= type
&& av
->a_type
!= AT_NULL
) av
++;
841 if (av
->a_type
== type
)
843 for (i
= 0; preload_info
[i
].size
; i
++)
845 if ((char *)av
->a_un
.a_ptr
>= (char *)preload_info
[i
].addr
&&
846 (char *)av
->a_un
.a_ptr
< (char *)preload_info
[i
].addr
+ preload_info
[i
].size
)
856 * Repeat the actions the kernel would do when loading a dynamically linked .so
857 * Load the binary and then its ELF interpreter.
858 * Note, we assume that the binary is a dynamically linked ELF shared object.
860 void* wld_start( void **stack
)
864 char *interp
, *reserve
= NULL
;
865 ElfW(auxv_t
) new_av
[12], delete_av
[3], *av
;
866 struct wld_link_map main_binary_map
, ld_so_map
;
867 struct wine_preload_info
**wine_main_preload_info
;
870 argv
= (char **)pargc
+ 1;
871 if (*pargc
< 2) fatal_error( "Usage: %s wine_binary [args]\n", argv
[0] );
873 /* skip over the parameters */
874 p
= argv
+ *pargc
+ 1;
876 /* skip over the environment */
879 static const char res
[] = "WINEPRELOADRESERVE=";
880 if (!wld_strncmp( *p
, res
, sizeof(res
)-1 )) reserve
= *p
+ sizeof(res
) - 1;
884 av
= (ElfW(auxv_t
)*) (p
+1);
885 page_size
= get_auxiliary( av
, AT_PAGESZ
, 4096 );
886 page_mask
= page_size
- 1;
888 preloader_start
= (char *)_start
- ((unsigned int)_start
& page_mask
);
889 preloader_end
= (char *)((unsigned int)(_end
+ page_mask
) & ~page_mask
);
892 wld_printf( "stack = %x\n", *stack
);
893 for( i
= 0; i
< *pargc
; i
++ ) wld_printf("argv[%x] = %s\n", i
, argv
[i
]);
894 dump_auxiliary( av
);
897 /* reserve memory that Wine needs */
898 if (reserve
) preload_reserve( reserve
);
899 for (i
= 0; preload_info
[i
].size
; i
++)
900 wld_mmap( preload_info
[i
].addr
, preload_info
[i
].size
,
901 PROT_NONE
, MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
, -1, 0 );
903 /* load the main binary */
904 map_so_lib( argv
[1], &main_binary_map
);
906 /* load the ELF interpreter */
907 interp
= (char *)main_binary_map
.l_addr
+ main_binary_map
.l_interp
;
908 map_so_lib( interp
, &ld_so_map
);
910 /* store pointer to the preload info into the appropriate main binary variable */
911 wine_main_preload_info
= find_symbol( main_binary_map
.l_phdr
, main_binary_map
.l_phnum
,
912 "wine_main_preload_info" );
913 if (wine_main_preload_info
) *wine_main_preload_info
= preload_info
;
914 else wld_printf( "wine_main_preload_info not found\n" );
916 #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
917 SET_NEW_AV( 0, AT_PHDR
, (unsigned long)main_binary_map
.l_phdr
);
918 SET_NEW_AV( 1, AT_PHENT
, sizeof(ElfW(Phdr
)) );
919 SET_NEW_AV( 2, AT_PHNUM
, main_binary_map
.l_phnum
);
920 SET_NEW_AV( 3, AT_PAGESZ
, page_size
);
921 SET_NEW_AV( 4, AT_BASE
, ld_so_map
.l_addr
);
922 SET_NEW_AV( 5, AT_FLAGS
, get_auxiliary( av
, AT_FLAGS
, 0 ) );
923 SET_NEW_AV( 6, AT_ENTRY
, main_binary_map
.l_entry
);
924 SET_NEW_AV( 7, AT_UID
, get_auxiliary( av
, AT_UID
, wld_getuid() ) );
925 SET_NEW_AV( 8, AT_EUID
, get_auxiliary( av
, AT_EUID
, wld_geteuid() ) );
926 SET_NEW_AV( 9, AT_GID
, get_auxiliary( av
, AT_GID
, wld_getgid() ) );
927 SET_NEW_AV(10, AT_EGID
, get_auxiliary( av
, AT_EGID
, wld_getegid() ) );
928 SET_NEW_AV(11, AT_NULL
, 0 );
932 /* delete sysinfo values if addresses conflict */
933 if (is_in_preload_range( av
, AT_SYSINFO
)) delete_av
[i
++].a_type
= AT_SYSINFO
;
934 if (is_in_preload_range( av
, AT_SYSINFO_EHDR
)) delete_av
[i
++].a_type
= AT_SYSINFO_EHDR
;
935 delete_av
[i
].a_type
= AT_NULL
;
937 /* get rid of first argument */
938 pargc
[1] = pargc
[0] - 1;
941 set_auxiliary_values( av
, new_av
, delete_av
, stack
);
944 wld_printf("new stack = %x\n", *stack
);
945 wld_printf("jumping to %x\n", ld_so_map
.l_entry
);
948 return (void *)ld_so_map
.l_entry
;