Added check for static libc.
[wine/multimedia.git] / loader / preloader.c
blob40f4e8afc7042371d7ca7df8bae7d710bce15c95
1 /*
2 * Preloader for ld.so
4 * Copyright (C) 1995,96,97,98,99,2000,2001,2002 Free Software Foundation, Inc.
5 * Copyright (C) 2004 Mike McCormack for Codeweavers
6 * Copyright (C) 2004 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Design notes
26 * The goal of this program is to be a workaround for exec-shield, as used
27 * by the Linux kernel distributed with Fedora Core and other distros.
29 * To do this, we implement our own shared object loader that reserves memory
30 * that is important to Wine, and then loads the main binary and its ELF
31 * interpreter.
33 * We will try to set up the stack and memory area so that the program that
34 * loads after us (eg. the wine binary) never knows we were here, except that
35 * areas of memory it needs are already magically reserved.
37 * The following memory areas are important to Wine:
38 * 0x00000000 - 0x00110000 the DOS area
39 * 0x80000000 - 0x81000000 the shared heap
40 * ??? - ??? the PE binary load address (usually starting at 0x00400000)
42 * If this program is used as the shared object loader, the only difference
43 * that the loaded programs should see is that this loader will be mapped
44 * into memory when it starts.
48 * References (things I consulted to understand how ELF loading works):
50 * glibc 2.3.2 elf/dl-load.c
51 * http://www.gnu.org/directory/glibc.html
53 * Linux 2.6.4 fs/binfmt_elf.c
54 * ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.4.tar.bz2
56 * Userland exec, by <grugq@hcunix.net>
57 * http://cert.uni-stuttgart.de/archive/bugtraq/2004/01/msg00002.html
59 * The ELF specification:
60 * http://www.linuxbase.org/spec/booksets/LSB-Embedded/LSB-Embedded/book387.html
63 #include "config.h"
64 #include "wine/port.h"
66 #include <stdarg.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 #include <fcntl.h>
73 #ifdef HAVE_SYS_MMAN_H
74 # include <sys/mman.h>
75 #endif
76 #ifdef HAVE_UNISTD_H
77 # include <unistd.h>
78 #endif
79 #ifdef HAVE_ELF_H
80 # include <elf.h>
81 #endif
82 #ifdef HAVE_LINK_H
83 # include <link.h>
84 #endif
85 #ifdef HAVE_SYS_LINK_H
86 # include <sys/link.h>
87 #endif
89 #include "main.h"
91 /* ELF definitions */
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
97 #ifndef MAP_COPY
98 #define MAP_COPY MAP_PRIVATE
99 #endif
100 #ifndef MAP_NORESERVE
101 #define MAP_NORESERVE 0
102 #endif
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 */
112 /* debugging */
113 #undef DUMP_SEGMENTS
114 #undef DUMP_AUX_INFO
115 #undef DUMP_SYMS
117 /* older systems may not define these */
118 #ifndef PT_TLS
119 #define PT_TLS 7
120 #endif
122 static unsigned int page_size, page_mask;
124 struct wld_link_map {
125 ElfW(Addr) l_addr;
126 ElfW(Dyn) *l_ld;
127 const ElfW(Phdr) *l_phdr;
128 ElfW(Addr) l_entry;
129 ElfW(Half) l_ldnum;
130 ElfW(Half) l_phnum;
131 ElfW(Addr) l_map_start, l_map_end;
132 ElfW(Addr) l_interp;
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
143 void _start();
144 extern char _end[];
145 __ASM_GLOBAL_FUNC(_start,
146 "\tcall wld_start\n"
147 "\tpush %eax\n"
148 "\txor %eax,%eax\n"
149 "\txor %ecx,%ecx\n"
150 "\txor %edx,%edx\n"
151 "\tret\n")
154 * wld_printf - just the basics
156 * %x prints a hex number
157 * %s prints a string
159 static void wld_vsprintf(char *str, const char *fmt, va_list args )
161 static const char hex_chars[16] = "0123456789abcdef";
162 const char *p = fmt;
164 while( *p )
166 if( *p == '%' )
168 p++;
169 if( *p == 'x' )
171 int i;
172 unsigned int x = va_arg( args, unsigned int );
173 for(i=7; i>=0; i--)
174 *str++ = hex_chars[(x>>(i*4))&0xf];
176 else if( *p == 's' )
178 char *s = va_arg( args, char * );
179 while(*s)
180 *str++ = *s++;
182 else if( *p == 0 )
183 break;
184 p++;
186 *str++ = *p++;
188 *str = 0;
191 static void wld_printf(const char *fmt, ... )
193 va_list args;
194 char buffer[256];
196 va_start( args, fmt );
197 wld_vsprintf(buffer, fmt, args );
198 va_end( args );
199 write(2, buffer, strlen(buffer));
202 static void fatal_error(const char *fmt, ... )
204 va_list args;
205 char buffer[256];
207 va_start( args, fmt );
208 wld_vsprintf(buffer, fmt, args );
209 va_end( args );
210 write(2, buffer, strlen(buffer));
211 _exit(1);
214 #ifdef DUMP_AUX_INFO
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++)
222 switch (av->a_type)
224 case AT_PAGESZ:
225 wld_printf("AT_PAGESZ = %x\n",av->a_un.a_val);
226 break;
227 case AT_PHDR:
228 wld_printf("AT_PHDR = %x\n",av->a_un.a_ptr);
229 break;
230 case AT_PHNUM:
231 wld_printf("AT_PHNUM = %x\n",av->a_un.a_val);
232 break;
233 case AT_ENTRY:
234 wld_printf("AT_ENTRY = %x\n",av->a_un.a_val);
235 break;
236 case AT_BASE:
237 wld_printf("AT_BASE = %x\n",av->a_un.a_val);
238 break;
241 #endif
244 * set_auxiliary
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;
254 return;
256 wld_printf( "wine-preloader: cannot set auxiliary value %x, please report\n", type );
260 * get_auxiliary
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;
270 return 1;
272 return 0;
276 * map_so_lib
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)
285 int fd;
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. */
290 struct loadcmd
292 ElfW(Addr) mapstart, mapend, dataend, allocend;
293 off_t mapoff;
294 int prot;
295 } loadcmds[16], *c;
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 );
322 l->l_ld = 0;
323 l->l_addr = 0;
324 l->l_phdr = 0;
325 l->l_phnum = header->e_phnum;
326 l->l_entry = header->e_entry;
327 l->l_interp = 0;
329 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
332 #ifdef DUMP_SEGMENTS
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 );
342 #endif
344 switch (ph->p_type)
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. */
349 case PT_DYNAMIC:
350 l->l_ld = (void *) ph->p_vaddr;
351 l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
352 break;
354 case PT_PHDR:
355 l->l_phdr = (void *) ph->p_vaddr;
356 break;
358 case PT_LOAD:
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);
373 c->prot = 0;
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;
381 break;
383 case PT_INTERP:
384 l->l_interp = ph->p_vaddr;
385 break;
387 case PT_TLS:
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.
393 case PT_SHLIB:
394 case PT_NOTE:
395 default:
396 break;
400 /* Now process the load commands and map segments into memory. */
401 c = loadcmds;
403 /* Length of the sections to be loaded. */
404 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
406 if( header->e_type == ET_DYN )
408 ElfW(Addr) mappref;
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,
415 fd, c->mapoff);
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,
424 PROT_NONE);
425 goto postmap;
427 else
429 char *preloader_start = (char *)_start - ((unsigned int)_start & page_mask);
430 char *preloader_end = (char *)((unsigned int)(_end + page_mask) & ~page_mask);
432 /* sanity check */
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);
453 postmap:
454 if (l->l_phdr == 0
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. */
481 zeropage = zeroend;
484 if (zeropage > zero)
486 /* Zero the final part of the last page of the segment. */
487 if ((c->prot & PROT_WRITE) == 0)
489 /* Dag nab it. */
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. */
500 caddr_t mapat;
501 mapat = mmap ((caddr_t) zeropage, zeroend - zeropage,
502 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
503 -1, 0);
507 ++c;
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;
515 close( fd );
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 */
531 #ifdef DUMP_SYMS
532 wld_printf("%x %x\n", phdr, num );
533 #endif
534 if( ( phdr == NULL ) || ( num == 0 ) )
536 wld_printf("could not find PT_DYNAMIC header entry\n");
537 return NULL;
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);
547 break;
550 if( !dyn ) return NULL;
552 while( dyn->d_tag )
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);
560 #ifdef DUMP_SYMS
561 wld_printf("%x %x\n", dyn->d_tag, dyn->d_un.d_ptr );
562 #endif
563 dyn++;
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 ) ) )
573 #ifdef DUMP_SYMS
574 wld_printf("Found %s -> %x\n", strings+symtab[i].st_name, symtab[i].st_value );
575 #endif
576 return (void*)symtab[i].st_value;
579 return NULL;
583 * wld_start
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, ... )
591 int i;
592 char **argv, **p;
593 char *interp;
594 ElfW(auxv_t)* av;
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 */
601 p = argv + argc + 1;
603 /* skip over the environment */
604 while (*p) p++;
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;
610 #ifdef DUMP_AUX_INFO
611 for( i = 0; i<argc; i++ ) wld_printf("argv[%x] = %s\n", i, argv[i]);
612 dump_auxiliary( av );
613 #endif
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 );
638 #ifdef DUMP_AUX_INFO
639 wld_printf("New auxiliary info:\n");
640 dump_auxiliary( av );
641 wld_printf("jumping to %x\n", ld_so_map.l_entry);
642 #endif
644 return (void *)ld_so_map.l_entry;