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>
85 #ifdef HAVE_SYS_LINK_H
86 # include <sys/link.h>
92 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
93 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
95 #define MAP_BASE_ADDR(l) 0
98 #define MAP_COPY MAP_PRIVATE
100 #ifndef MAP_NORESERVE
101 #define MAP_NORESERVE 0
104 static struct wine_preload_info preload_info
[] =
106 { (void *)0x00000000, 0x00110000 }, /* DOS area */
107 { (void *)0x80000000, 0x01000000 }, /* shared heap */
108 { (void *)0x00110000, 0x0fef0000 }, /* PE exe range (FIXME) */
109 { 0, 0 } /* end of list */
117 /* older systems may not define these */
122 static unsigned int page_size
, page_mask
;
124 struct wld_link_map
{
127 const ElfW(Phdr
) *l_phdr
;
131 ElfW(Addr
) l_map_start
, l_map_end
;
137 * The _start function is the entry and exit point of this program
139 * It calls wld_start, passing a pointer to the args it receives
140 * then jumps to the address wld_start returns after removing the
141 * first argv[] value, and decrementing argc
145 __ASM_GLOBAL_FUNC(_start
,
154 * wld_printf - just the basics
156 * %x prints a hex number
159 static void wld_vsprintf(char *str
, const char *fmt
, va_list args
)
161 static const char hex_chars
[16] = "0123456789abcdef";
172 unsigned int x
= va_arg( args
, unsigned int );
174 *str
++ = hex_chars
[(x
>>(i
*4))&0xf];
178 char *s
= va_arg( args
, char * );
191 static void wld_printf(const char *fmt
, ... )
196 va_start( args
, fmt
);
197 wld_vsprintf(buffer
, fmt
, args
);
199 write(2, buffer
, strlen(buffer
));
202 static void fatal_error(const char *fmt
, ... )
207 va_start( args
, fmt
);
208 wld_vsprintf(buffer
, fmt
, args
);
210 write(2, buffer
, strlen(buffer
));
216 * Dump interesting bits of the ELF auxv_t structure that is passed
217 * as the 4th parameter to the _start function
219 static void dump_auxiliary( ElfW(auxv_t
) *av
)
221 for ( ; av
->a_type
!= AT_NULL
; av
++)
225 wld_printf("AT_PAGESZ = %x\n",av
->a_un
.a_val
);
228 wld_printf("AT_PHDR = %x\n",av
->a_un
.a_ptr
);
231 wld_printf("AT_PHNUM = %x\n",av
->a_un
.a_val
);
234 wld_printf("AT_ENTRY = %x\n",av
->a_un
.a_val
);
237 wld_printf("AT_BASE = %x\n",av
->a_un
.a_val
);
246 * Set a field of the auxiliary structure
248 static void set_auxiliary( ElfW(auxv_t
) *av
, int type
, long int val
)
250 for ( ; av
->a_type
!= AT_NULL
; av
++)
251 if( av
->a_type
== type
)
253 av
->a_un
.a_val
= val
;
256 wld_printf( "wine-preloader: cannot set auxiliary value %x, please report\n", type
);
262 * Get a field of the auxiliary structure
264 static int get_auxiliary( ElfW(auxv_t
) *av
, int type
, int *val
)
266 for ( ; av
->a_type
!= AT_NULL
; av
++)
267 if( av
->a_type
== type
)
269 *val
= av
->a_un
.a_val
;
278 * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
280 * This function maps the segments from an ELF object, and optionally
281 * stores information about the mapping into the auxv_t structure.
283 static void map_so_lib( const char *name
, struct wld_link_map
*l
)
286 unsigned char buf
[0x800];
287 ElfW(Ehdr
) *header
= (ElfW(Ehdr
)*)buf
;
288 ElfW(Phdr
) *phdr
, *ph
;
289 /* Scan the program header table, collecting its load commands. */
292 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
296 size_t nloadcmds
= 0, maplength
;
298 fd
= open( name
, O_RDONLY
);
299 if (fd
== -1) fatal_error("%s: could not open\n", name
);
301 if (read( fd
, buf
, sizeof(buf
) ) != sizeof(buf
))
302 fatal_error("%s: failed to read ELF header\n", name
);
304 phdr
= (void*) (((unsigned char*)buf
) + header
->e_phoff
);
306 if( ( header
->e_ident
[0] != 0x7f ) ||
307 ( header
->e_ident
[1] != 'E' ) ||
308 ( header
->e_ident
[2] != 'L' ) ||
309 ( header
->e_ident
[3] != 'F' ) )
310 fatal_error( "%s: not an ELF binary... don't know how to load it\n", name
);
312 if( header
->e_machine
!= EM_386
)
313 fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name
);
315 if (header
->e_phnum
> sizeof(loadcmds
)/sizeof(loadcmds
[0]))
316 fatal_error( "%s: oops... not enough space for load commands\n", name
);
318 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
319 if (header
->e_phoff
+ maplength
> sizeof(buf
))
320 fatal_error( "%s: oops... not enough space for ELF headers\n", name
);
325 l
->l_phnum
= header
->e_phnum
;
326 l
->l_entry
= header
->e_entry
;
329 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
333 wld_printf( "ph = %x\n", ph
);
334 wld_printf( " p_type = %x\n", ph
->p_type
);
335 wld_printf( " p_flags = %x\n", ph
->p_flags
);
336 wld_printf( " p_offset = %x\n", ph
->p_offset
);
337 wld_printf( " p_vaddr = %x\n", ph
->p_vaddr
);
338 wld_printf( " p_paddr = %x\n", ph
->p_paddr
);
339 wld_printf( " p_filesz = %x\n", ph
->p_filesz
);
340 wld_printf( " p_memsz = %x\n", ph
->p_memsz
);
341 wld_printf( " p_align = %x\n", ph
->p_align
);
346 /* These entries tell us where to find things once the file's
347 segments are mapped in. We record the addresses it says
348 verbatim, and later correct for the run-time load address. */
350 l
->l_ld
= (void *) ph
->p_vaddr
;
351 l
->l_ldnum
= ph
->p_memsz
/ sizeof (Elf32_Dyn
);
355 l
->l_phdr
= (void *) ph
->p_vaddr
;
360 if ((ph
->p_align
& page_mask
) != 0)
361 fatal_error( "%s: ELF load command alignment not page-aligned\n", name
);
363 if (((ph
->p_vaddr
- ph
->p_offset
) & (ph
->p_align
- 1)) != 0)
364 fatal_error( "%s: ELF load command address/offset not properly aligned\n", name
);
366 c
= &loadcmds
[nloadcmds
++];
367 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
368 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ page_mask
) & ~page_mask
);
369 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
370 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
371 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
374 if (ph
->p_flags
& PF_R
)
375 c
->prot
|= PROT_READ
;
376 if (ph
->p_flags
& PF_W
)
377 c
->prot
|= PROT_WRITE
;
378 if (ph
->p_flags
& PF_X
)
379 c
->prot
|= PROT_EXEC
;
384 l
->l_interp
= ph
->p_vaddr
;
389 * We don't need to set anything up because we're
390 * emulating the kernel, not ld-linux.so.2
391 * The ELF loader will set up the TLS data itself.
400 /* Now process the load commands and map segments into memory. */
403 /* Length of the sections to be loaded. */
404 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
406 if( header
->e_type
== ET_DYN
)
409 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
410 - MAP_BASE_ADDR (l
));
412 /* Remember which part of the address space this object uses. */
413 l
->l_map_start
= (ElfW(Addr
)) mmap ((void *) mappref
, maplength
,
414 c
->prot
, MAP_COPY
| MAP_FILE
,
416 /* wld_printf("set : offset = %x\n", c->mapoff); */
417 /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
419 l
->l_map_end
= l
->l_map_start
+ maplength
;
420 l
->l_addr
= l
->l_map_start
- c
->mapstart
;
422 mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
423 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
429 char *preloader_start
= (char *)_start
- ((unsigned int)_start
& page_mask
);
430 char *preloader_end
= (char *)((unsigned int)(_end
+ page_mask
) & ~page_mask
);
433 if ((char *)c
->mapstart
+ maplength
> preloader_start
&&
434 (char *)c
->mapstart
<= preloader_end
)
435 fatal_error( "%s: binary overlaps preloader (%x-%x)\n",
436 name
, c
->mapstart
, (char *)c
->mapstart
+ maplength
);
438 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
441 /* Remember which part of the address space this object uses. */
442 l
->l_map_start
= c
->mapstart
+ l
->l_addr
;
443 l
->l_map_end
= l
->l_map_start
+ maplength
;
445 while (c
< &loadcmds
[nloadcmds
])
447 if (c
->mapend
> c
->mapstart
)
448 /* Map the segment contents from the file. */
449 mmap ((void *) (l
->l_addr
+ c
->mapstart
),
450 c
->mapend
- c
->mapstart
, c
->prot
,
451 MAP_FIXED
| MAP_COPY
| MAP_FILE
, fd
, c
->mapoff
);
455 && (ElfW(Off
)) c
->mapoff
<= header
->e_phoff
456 && ((size_t) (c
->mapend
- c
->mapstart
+ c
->mapoff
)
457 >= header
->e_phoff
+ header
->e_phnum
* sizeof (ElfW(Phdr
))))
458 /* Found the program header in this segment. */
459 l
->l_phdr
= (void *)(unsigned int) (c
->mapstart
+ header
->e_phoff
- c
->mapoff
);
461 if (c
->allocend
> c
->dataend
)
463 /* Extra zero pages should appear at the end of this segment,
464 after the data mapped from the file. */
465 ElfW(Addr
) zero
, zeroend
, zeropage
;
467 zero
= l
->l_addr
+ c
->dataend
;
468 zeroend
= l
->l_addr
+ c
->allocend
;
469 zeropage
= (zero
+ page_mask
) & ~page_mask
;
472 * This is different from the dl-load load...
473 * ld-linux.so.2 relies on the whole page being zero'ed
475 zeroend
= (zeroend
+ page_mask
) & ~page_mask
;
477 if (zeroend
< zeropage
)
479 /* All the extra data is in the last page of the segment.
480 We can just zero it. */
486 /* Zero the final part of the last page of the segment. */
487 if ((c
->prot
& PROT_WRITE
) == 0)
490 mprotect ((caddr_t
) (zero
& ~page_mask
), page_size
, c
->prot
|PROT_WRITE
);
492 memset ((void *) zero
, '\0', zeropage
- zero
);
493 if ((c
->prot
& PROT_WRITE
) == 0)
494 mprotect ((caddr_t
) (zero
& ~page_mask
), page_size
, c
->prot
);
497 if (zeroend
> zeropage
)
499 /* Map the remaining zero pages in from the zero fill FD. */
501 mapat
= mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
502 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
510 if (l
->l_phdr
== NULL
) fatal_error("no program header\n");
512 l
->l_phdr
= (void *)((ElfW(Addr
))l
->l_phdr
+ l
->l_addr
);
513 l
->l_entry
+= l
->l_addr
;
520 * Find a symbol in the symbol table of the executable loaded
522 static void *find_symbol( const ElfW(Phdr
) *phdr
, int num
, char *var
)
524 const ElfW(Dyn
) *dyn
= NULL
;
525 const ElfW(Phdr
) *ph
;
526 const ElfW(Sym
) *symtab
= NULL
;
527 const char *strings
= NULL
;
528 uint32_t i
, symtabend
= 0;
530 /* check the values */
532 wld_printf("%x %x\n", phdr
, num
);
534 if( ( phdr
== NULL
) || ( num
== 0 ) )
536 wld_printf("could not find PT_DYNAMIC header entry\n");
540 /* parse the (already loaded) ELF executable's header */
541 for (ph
= phdr
; ph
< &phdr
[num
]; ++ph
)
543 if( PT_DYNAMIC
== ph
->p_type
)
545 dyn
= (void *) ph
->p_vaddr
;
546 num
= ph
->p_memsz
/ sizeof (Elf32_Dyn
);
550 if( !dyn
) return NULL
;
554 if( dyn
->d_tag
== DT_STRTAB
)
555 strings
= (const char*) dyn
->d_un
.d_ptr
;
556 if( dyn
->d_tag
== DT_SYMTAB
)
557 symtab
= (const ElfW(Sym
) *)dyn
->d_un
.d_ptr
;
558 if( dyn
->d_tag
== DT_HASH
)
559 symtabend
= *((const uint32_t *)dyn
->d_un
.d_ptr
+ 1);
561 wld_printf("%x %x\n", dyn
->d_tag
, dyn
->d_un
.d_ptr
);
566 if( (!symtab
) || (!strings
) ) return NULL
;
568 for (i
= 0; i
< symtabend
; i
++)
570 if( ( ELF32_ST_BIND(symtab
[i
].st_info
) == STT_OBJECT
) &&
571 ( 0 == strcmp( strings
+symtab
[i
].st_name
, var
) ) )
574 wld_printf("Found %s -> %x\n", strings
+symtab
[i
].st_name
, symtab
[i
].st_value
);
576 return (void*)symtab
[i
].st_value
;
585 * Repeat the actions the kernel would do when loading a dynamically linked .so
586 * Load the binary and then its ELF interpreter.
587 * Note, we assume that the binary is a dynamically linked ELF shared object.
589 void* wld_start( int argc
, ... )
595 struct wld_link_map main_binary_map
, ld_so_map
;
596 struct wine_preload_info
**wine_main_preload_info
;
598 argv
= (char **)&argc
+ 1;
600 /* skip over the parameters */
603 /* skip over the environment */
606 av
= (ElfW(auxv_t
)*) (p
+1);
607 if (!get_auxiliary( av
, AT_PAGESZ
, &page_size
)) page_size
= 4096;
608 page_mask
= page_size
- 1;
611 for( i
= 0; i
<argc
; i
++ ) wld_printf("argv[%x] = %s\n", i
, argv
[i
]);
612 dump_auxiliary( av
);
615 /* reserve memory that Wine needs */
616 for (i
= 0; preload_info
[i
].size
; i
++)
617 mmap( preload_info
[i
].addr
, preload_info
[i
].size
,
618 PROT_NONE
, MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
, -1, 0 );
620 /* load the main binary */
621 map_so_lib( argv
[0], &main_binary_map
);
623 /* load the ELF interpreter */
624 interp
= (char *)main_binary_map
.l_addr
+ main_binary_map
.l_interp
;
625 map_so_lib( interp
, &ld_so_map
);
627 /* store pointer to the preload info into the appropriate main binary variable */
628 wine_main_preload_info
= find_symbol( main_binary_map
.l_phdr
, main_binary_map
.l_phnum
,
629 "wine_main_preload_info" );
630 if (wine_main_preload_info
) *wine_main_preload_info
= preload_info
;
631 else wld_printf( "wine_main_preload_info not found\n" );
633 set_auxiliary( av
, AT_PHDR
, (unsigned long)main_binary_map
.l_phdr
);
634 set_auxiliary( av
, AT_PHNUM
, main_binary_map
.l_phnum
);
635 set_auxiliary( av
, AT_BASE
, ld_so_map
.l_addr
);
636 set_auxiliary( av
, AT_ENTRY
, main_binary_map
.l_entry
);
639 wld_printf("New auxiliary info:\n");
640 dump_auxiliary( av
);
641 wld_printf("jumping to %x\n", ld_so_map
.l_entry
);
644 return (void *)ld_so_map
.l_entry
;