Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Preloader for ld.so |
| 3 | * |
| 4 | * Copyright (C) 1995,96,97,98,99,2000,2001,2002 Free Software Foundation, Inc. |
Alexandre Julliard | c319392 | 2004-06-15 20:31:06 +0000 | [diff] [blame] | 5 | * Copyright (C) 2004 Mike McCormack for CodeWeavers |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 2004 Alexandre Julliard |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Design notes |
| 25 | * |
| 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. |
| 28 | * |
| 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. |
| 32 | * |
| 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. |
| 36 | * |
| 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) |
| 41 | * |
| 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. |
| 45 | */ |
| 46 | |
| 47 | /* |
| 48 | * References (things I consulted to understand how ELF loading works): |
| 49 | * |
| 50 | * glibc 2.3.2 elf/dl-load.c |
| 51 | * http://www.gnu.org/directory/glibc.html |
| 52 | * |
| 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 |
| 55 | * |
| 56 | * Userland exec, by <grugq@hcunix.net> |
| 57 | * http://cert.uni-stuttgart.de/archive/bugtraq/2004/01/msg00002.html |
| 58 | * |
| 59 | * The ELF specification: |
| 60 | * http://www.linuxbase.org/spec/booksets/LSB-Embedded/LSB-Embedded/book387.html |
| 61 | */ |
| 62 | |
| 63 | #include "config.h" |
| 64 | #include "wine/port.h" |
| 65 | |
| 66 | #include <stdarg.h> |
| 67 | #include <stdio.h> |
| 68 | #include <stdlib.h> |
| 69 | #include <string.h> |
| 70 | #include <sys/types.h> |
Francois Gouget | 821d4c4 | 2005-05-06 16:22:54 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYS_STAT_H |
| 72 | # include <sys/stat.h> |
| 73 | #endif |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 74 | #include <fcntl.h> |
| 75 | #ifdef HAVE_SYS_MMAN_H |
| 76 | # include <sys/mman.h> |
| 77 | #endif |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SYS_SYSCALL_H |
| 79 | # include <sys/syscall.h> |
| 80 | #endif |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 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 |
| 93 | |
| 94 | #include "main.h" |
| 95 | |
| 96 | /* ELF definitions */ |
| 97 | #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref) |
| 98 | #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0) |
| 99 | |
| 100 | #define MAP_BASE_ADDR(l) 0 |
| 101 | |
| 102 | #ifndef MAP_COPY |
| 103 | #define MAP_COPY MAP_PRIVATE |
| 104 | #endif |
| 105 | #ifndef MAP_NORESERVE |
| 106 | #define MAP_NORESERVE 0 |
| 107 | #endif |
| 108 | |
| 109 | static struct wine_preload_info preload_info[] = |
| 110 | { |
Alexandre Julliard | 195ca1e | 2008-04-14 20:38:17 +0200 | [diff] [blame] | 111 | { (void *)0x00000000, 0x00010000 }, /* low 64k */ |
| 112 | { (void *)0x00010000, 0x00100000 }, /* DOS area */ |
Alexandre Julliard | 4d35f3f | 2009-07-01 16:02:55 +0200 | [diff] [blame] | 113 | { (void *)0x00110000, 0x67ef0000 }, /* low memory area */ |
Alexandre Julliard | 912e4d4 | 2008-11-07 11:05:38 +0100 | [diff] [blame] | 114 | { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */ |
Alexandre Julliard | e325efb | 2006-07-24 15:25:39 +0200 | [diff] [blame] | 115 | { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */ |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 116 | { 0, 0 } /* end of list */ |
| 117 | }; |
| 118 | |
| 119 | /* debugging */ |
| 120 | #undef DUMP_SEGMENTS |
| 121 | #undef DUMP_AUX_INFO |
| 122 | #undef DUMP_SYMS |
| 123 | |
| 124 | /* older systems may not define these */ |
| 125 | #ifndef PT_TLS |
| 126 | #define PT_TLS 7 |
| 127 | #endif |
| 128 | |
Mike McCormack | 2113bd4 | 2004-06-28 20:28:05 +0000 | [diff] [blame] | 129 | #ifndef AT_SYSINFO |
| 130 | #define AT_SYSINFO 32 |
| 131 | #endif |
| 132 | #ifndef AT_SYSINFO_EHDR |
| 133 | #define AT_SYSINFO_EHDR 33 |
| 134 | #endif |
| 135 | |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 136 | #ifndef DT_GNU_HASH |
| 137 | #define DT_GNU_HASH 0x6ffffef5 |
| 138 | #endif |
| 139 | |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 140 | static unsigned int page_size, page_mask; |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 141 | static char *preloader_start, *preloader_end; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 142 | |
| 143 | struct wld_link_map { |
| 144 | ElfW(Addr) l_addr; |
| 145 | ElfW(Dyn) *l_ld; |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 146 | ElfW(Phdr)*l_phdr; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 147 | ElfW(Addr) l_entry; |
| 148 | ElfW(Half) l_ldnum; |
| 149 | ElfW(Half) l_phnum; |
| 150 | ElfW(Addr) l_map_start, l_map_end; |
| 151 | ElfW(Addr) l_interp; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | /* |
Aaron Arvey | a31a752 | 2005-04-15 16:13:49 +0000 | [diff] [blame] | 156 | * The __bb_init_func is an empty function only called when file is |
| 157 | * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This |
| 158 | * function is normally provided by libc's startup files, but since we |
| 159 | * build the preloader with "-nostartfiles -nodefaultlibs", we have to |
| 160 | * provide our own (empty) version, otherwise linker fails. |
| 161 | */ |
Eric Pouech | e66e227 | 2006-02-06 13:13:28 +0100 | [diff] [blame] | 162 | void __bb_init_func(void) { return; } |
Aaron Arvey | a31a752 | 2005-04-15 16:13:49 +0000 | [diff] [blame] | 163 | |
Mike McCormack | 7cd9a8f | 2006-01-26 13:21:54 +0100 | [diff] [blame] | 164 | /* similar to the above but for -fstack-protector */ |
| 165 | void *__stack_chk_guard = 0; |
Stefan Reimer | 3dcd128 | 2009-01-02 18:40:17 +0100 | [diff] [blame] | 166 | void __stack_chk_fail_local(void) { return; } |
Mike McCormack | 7cd9a8f | 2006-01-26 13:21:54 +0100 | [diff] [blame] | 167 | void __stack_chk_fail(void) { return; } |
Aaron Arvey | a31a752 | 2005-04-15 16:13:49 +0000 | [diff] [blame] | 168 | |
Alexandre Julliard | a68d293 | 2006-11-03 13:55:41 +0100 | [diff] [blame] | 169 | /* data for setting up the glibc-style thread-local storage in %gs */ |
| 170 | |
| 171 | static int thread_data[256]; |
| 172 | |
| 173 | struct |
| 174 | { |
| 175 | /* this is the kernel modify_ldt struct */ |
| 176 | unsigned int entry_number; |
| 177 | unsigned long base_addr; |
| 178 | unsigned int limit; |
| 179 | unsigned int seg_32bit : 1; |
| 180 | unsigned int contents : 2; |
| 181 | unsigned int read_exec_only : 1; |
| 182 | unsigned int limit_in_pages : 1; |
| 183 | unsigned int seg_not_present : 1; |
Francois Gouget | d8c41a2 | 2008-04-14 01:03:24 +0200 | [diff] [blame] | 184 | unsigned int usable : 1; |
Alexandre Julliard | a68d293 | 2006-11-03 13:55:41 +0100 | [diff] [blame] | 185 | unsigned int garbage : 25; |
| 186 | } thread_ldt = { -1, (unsigned long)thread_data, 0xfffff, 1, 0, 0, 1, 0, 1, 0 }; |
| 187 | |
| 188 | |
Aaron Arvey | a31a752 | 2005-04-15 16:13:49 +0000 | [diff] [blame] | 189 | /* |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 190 | * The _start function is the entry and exit point of this program |
| 191 | * |
| 192 | * It calls wld_start, passing a pointer to the args it receives |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 193 | * then jumps to the address wld_start returns. |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 194 | */ |
Alexandre Julliard | fa6ffb4 | 2009-10-07 12:24:53 +0200 | [diff] [blame] | 195 | void _start(void); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 196 | extern char _end[]; |
| 197 | __ASM_GLOBAL_FUNC(_start, |
Alexandre Julliard | a68d293 | 2006-11-03 13:55:41 +0100 | [diff] [blame] | 198 | "\tmovl $243,%eax\n" /* SYS_set_thread_area */ |
| 199 | "\tmovl $thread_ldt,%ebx\n" |
| 200 | "\tint $0x80\n" /* allocate gs segment */ |
| 201 | "\torl %eax,%eax\n" |
| 202 | "\tjl 1f\n" |
| 203 | "\tmovl thread_ldt,%eax\n" /* thread_ldt.entry_number */ |
| 204 | "\tshl $3,%eax\n" |
| 205 | "\torl $3,%eax\n" |
| 206 | "\tmov %ax,%gs\n" |
| 207 | "\tmov %ax,%fs\n" /* set %fs too so libwine can retrieve it later on */ |
| 208 | "1:\tmovl %esp,%eax\n" |
Alexandre Julliard | cc01e8f | 2005-11-23 19:55:06 +0100 | [diff] [blame] | 209 | "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */ |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 210 | "\tpushl %eax\n" /* orig stack pointer */ |
| 211 | "\tpushl %esp\n" /* ptr to orig stack pointer */ |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 212 | "\tcall wld_start\n" |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 213 | "\tpopl %ecx\n" /* remove ptr to stack pointer */ |
| 214 | "\tpopl %esp\n" /* new stack pointer */ |
| 215 | "\tpush %eax\n" /* ELF interpreter entry point */ |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 216 | "\txor %eax,%eax\n" |
| 217 | "\txor %ecx,%ecx\n" |
| 218 | "\txor %edx,%edx\n" |
Alexandre Julliard | 4c4094e | 2006-11-04 20:25:06 +0100 | [diff] [blame] | 219 | "\tmov %ax,%gs\n" /* clear %gs again */ |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 220 | "\tret\n") |
| 221 | |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 222 | /* wrappers for Linux system calls */ |
| 223 | |
| 224 | #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret)) |
| 225 | |
| 226 | static inline __attribute__((noreturn)) void wld_exit( int code ) |
| 227 | { |
| 228 | for (;;) /* avoid warning */ |
Alexandre Julliard | e9f2eb5 | 2004-09-24 00:25:32 +0000 | [diff] [blame] | 229 | __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx" |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 230 | : : "a" (SYS_exit), "r" (code) ); |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static inline int wld_open( const char *name, int flags ) |
| 234 | { |
| 235 | int ret; |
Alexandre Julliard | e9f2eb5 | 2004-09-24 00:25:32 +0000 | [diff] [blame] | 236 | __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 237 | : "=a" (ret) : "0" (SYS_open), "r" (name), "c" (flags) ); |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 238 | return SYSCALL_RET(ret); |
| 239 | } |
| 240 | |
| 241 | static inline int wld_close( int fd ) |
| 242 | { |
| 243 | int ret; |
Alexandre Julliard | e9f2eb5 | 2004-09-24 00:25:32 +0000 | [diff] [blame] | 244 | __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 245 | : "=a" (ret) : "0" (SYS_close), "r" (fd) ); |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 246 | return SYSCALL_RET(ret); |
| 247 | } |
| 248 | |
| 249 | static inline ssize_t wld_read( int fd, void *buffer, size_t len ) |
| 250 | { |
| 251 | int ret; |
Alexandre Julliard | e9f2eb5 | 2004-09-24 00:25:32 +0000 | [diff] [blame] | 252 | __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
| 253 | : "=a" (ret) |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 254 | : "0" (SYS_read), "r" (fd), "c" (buffer), "d" (len) |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 255 | : "memory" ); |
| 256 | return SYSCALL_RET(ret); |
| 257 | } |
| 258 | |
| 259 | static inline ssize_t wld_write( int fd, const void *buffer, size_t len ) |
| 260 | { |
| 261 | int ret; |
Alexandre Julliard | e9f2eb5 | 2004-09-24 00:25:32 +0000 | [diff] [blame] | 262 | __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 263 | : "=a" (ret) : "0" (SYS_write), "r" (fd), "c" (buffer), "d" (len) ); |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 264 | return SYSCALL_RET(ret); |
| 265 | } |
| 266 | |
| 267 | static inline int wld_mprotect( const void *addr, size_t len, int prot ) |
| 268 | { |
| 269 | int ret; |
Alexandre Julliard | e9f2eb5 | 2004-09-24 00:25:32 +0000 | [diff] [blame] | 270 | __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 271 | : "=a" (ret) : "0" (SYS_mprotect), "r" (addr), "c" (len), "d" (prot) ); |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 272 | return SYSCALL_RET(ret); |
| 273 | } |
| 274 | |
| 275 | static void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset ) |
| 276 | { |
| 277 | int ret; |
| 278 | |
| 279 | struct |
| 280 | { |
| 281 | void *addr; |
| 282 | unsigned int length; |
| 283 | unsigned int prot; |
| 284 | unsigned int flags; |
| 285 | unsigned int fd; |
| 286 | unsigned int offset; |
| 287 | } args; |
| 288 | |
| 289 | args.addr = start; |
| 290 | args.length = len; |
| 291 | args.prot = prot; |
| 292 | args.flags = flags; |
| 293 | args.fd = fd; |
| 294 | args.offset = offset; |
Alexandre Julliard | e9f2eb5 | 2004-09-24 00:25:32 +0000 | [diff] [blame] | 295 | __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 296 | : "=a" (ret) : "0" (SYS_mmap), "q" (&args) : "memory" ); |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 297 | return (void *)SYSCALL_RET(ret); |
| 298 | } |
| 299 | |
| 300 | static inline uid_t wld_getuid(void) |
| 301 | { |
| 302 | uid_t ret; |
| 303 | __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getuid) ); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static inline uid_t wld_geteuid(void) |
| 308 | { |
| 309 | uid_t ret; |
| 310 | __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_geteuid) ); |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | static inline gid_t wld_getgid(void) |
| 315 | { |
| 316 | gid_t ret; |
| 317 | __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getgid) ); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static inline gid_t wld_getegid(void) |
| 322 | { |
| 323 | gid_t ret; |
| 324 | __asm__( "int $0x80" : "=a" (ret) : "0" (SYS_getegid) ); |
| 325 | return ret; |
| 326 | } |
| 327 | |
Alexandre Julliard | c20d6c4 | 2006-03-31 19:16:22 +0200 | [diff] [blame] | 328 | static inline int wld_prctl( int code, int arg ) |
| 329 | { |
| 330 | int ret; |
| 331 | __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
| 332 | : "=a" (ret) : "0" (SYS_prctl), "r" (code), "c" (arg) ); |
| 333 | return SYSCALL_RET(ret); |
| 334 | } |
| 335 | |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 336 | |
| 337 | /* replacement for libc functions */ |
| 338 | |
| 339 | static int wld_strcmp( const char *str1, const char *str2 ) |
| 340 | { |
| 341 | while (*str1 && (*str1 == *str2)) { str1++; str2++; } |
| 342 | return *str1 - *str2; |
| 343 | } |
| 344 | |
| 345 | static int wld_strncmp( const char *str1, const char *str2, size_t len ) |
| 346 | { |
| 347 | if (len <= 0) return 0; |
| 348 | while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; } |
| 349 | return *str1 - *str2; |
| 350 | } |
| 351 | |
| 352 | static inline void *wld_memset( void *dest, int val, size_t len ) |
| 353 | { |
| 354 | char *dst = dest; |
| 355 | while (len--) *dst++ = val; |
| 356 | return dest; |
| 357 | } |
| 358 | |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 359 | /* |
| 360 | * wld_printf - just the basics |
| 361 | * |
| 362 | * %x prints a hex number |
| 363 | * %s prints a string |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 364 | * %p prints a pointer |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 365 | */ |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 366 | static int wld_vsprintf(char *buffer, const char *fmt, va_list args ) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 367 | { |
Alexandre Julliard | 6d2099f | 2004-06-01 19:47:11 +0000 | [diff] [blame] | 368 | static const char hex_chars[16] = "0123456789abcdef"; |
| 369 | const char *p = fmt; |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 370 | char *str = buffer; |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 371 | int i; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 372 | |
| 373 | while( *p ) |
| 374 | { |
| 375 | if( *p == '%' ) |
| 376 | { |
| 377 | p++; |
| 378 | if( *p == 'x' ) |
| 379 | { |
Alexandre Julliard | 6d2099f | 2004-06-01 19:47:11 +0000 | [diff] [blame] | 380 | unsigned int x = va_arg( args, unsigned int ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 381 | for(i=7; i>=0; i--) |
Alexandre Julliard | 6d2099f | 2004-06-01 19:47:11 +0000 | [diff] [blame] | 382 | *str++ = hex_chars[(x>>(i*4))&0xf]; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 383 | } |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 384 | else if (p[0] == 'l' && p[1] == 'x') |
| 385 | { |
| 386 | unsigned long x = va_arg( args, unsigned long ); |
| 387 | for(i=7; i>=0; i--) |
| 388 | *str++ = hex_chars[(x>>(i*4))&0xf]; |
| 389 | p++; |
| 390 | } |
| 391 | else if( *p == 'p' ) |
| 392 | { |
| 393 | unsigned long x = (unsigned long)va_arg( args, void * ); |
| 394 | for(i=7; i>=0; i--) |
| 395 | *str++ = hex_chars[(x>>(i*4))&0xf]; |
| 396 | } |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 397 | else if( *p == 's' ) |
| 398 | { |
| 399 | char *s = va_arg( args, char * ); |
| 400 | while(*s) |
| 401 | *str++ = *s++; |
| 402 | } |
| 403 | else if( *p == 0 ) |
| 404 | break; |
| 405 | p++; |
| 406 | } |
| 407 | *str++ = *p++; |
| 408 | } |
| 409 | *str = 0; |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 410 | return str - buffer; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 413 | static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... ) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 414 | { |
| 415 | va_list args; |
| 416 | char buffer[256]; |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 417 | int len; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 418 | |
| 419 | va_start( args, fmt ); |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 420 | len = wld_vsprintf(buffer, fmt, args ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 421 | va_end( args ); |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 422 | wld_write(2, buffer, len); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 425 | static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... ) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 426 | { |
| 427 | va_list args; |
| 428 | char buffer[256]; |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 429 | int len; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 430 | |
| 431 | va_start( args, fmt ); |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 432 | len = wld_vsprintf(buffer, fmt, args ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 433 | va_end( args ); |
Alexandre Julliard | 0c8d690 | 2004-07-06 18:46:05 +0000 | [diff] [blame] | 434 | wld_write(2, buffer, len); |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 435 | wld_exit(1); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | #ifdef DUMP_AUX_INFO |
| 439 | /* |
| 440 | * Dump interesting bits of the ELF auxv_t structure that is passed |
| 441 | * as the 4th parameter to the _start function |
| 442 | */ |
| 443 | static void dump_auxiliary( ElfW(auxv_t) *av ) |
| 444 | { |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 445 | #define NAME(at) { at, #at } |
| 446 | static const struct { int val; const char *name; } names[] = |
| 447 | { |
| 448 | NAME(AT_BASE), |
| 449 | NAME(AT_CLKTCK), |
| 450 | NAME(AT_EGID), |
| 451 | NAME(AT_ENTRY), |
| 452 | NAME(AT_EUID), |
| 453 | NAME(AT_FLAGS), |
| 454 | NAME(AT_GID), |
| 455 | NAME(AT_HWCAP), |
| 456 | NAME(AT_PAGESZ), |
| 457 | NAME(AT_PHDR), |
| 458 | NAME(AT_PHENT), |
| 459 | NAME(AT_PHNUM), |
| 460 | NAME(AT_PLATFORM), |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 461 | NAME(AT_SYSINFO), |
| 462 | NAME(AT_SYSINFO_EHDR), |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 463 | NAME(AT_UID), |
| 464 | { 0, NULL } |
| 465 | }; |
| 466 | #undef NAME |
| 467 | |
| 468 | int i; |
| 469 | |
| 470 | for ( ; av->a_type != AT_NULL; av++) |
| 471 | { |
| 472 | for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break; |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 473 | if (names[i].name) wld_printf("%s = %lx\n", names[i].name, av->a_un.a_val); |
| 474 | else wld_printf( "%x = %lx\n", av->a_type, av->a_un.a_val ); |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 475 | } |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 476 | } |
| 477 | #endif |
| 478 | |
| 479 | /* |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 480 | * set_auxiliary_values |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 481 | * |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 482 | * Set the new auxiliary values |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 483 | */ |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 484 | static void set_auxiliary_values( ElfW(auxv_t) *av, const ElfW(auxv_t) *new_av, |
| 485 | const ElfW(auxv_t) *delete_av, void **stack ) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 486 | { |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 487 | int i, j, av_count = 0, new_count = 0, delete_count = 0; |
| 488 | char *src, *dst; |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 489 | |
| 490 | /* count how many aux values we have already */ |
| 491 | while (av[av_count].a_type != AT_NULL) av_count++; |
| 492 | |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 493 | /* delete unwanted values */ |
| 494 | for (j = 0; delete_av[j].a_type != AT_NULL; j++) |
| 495 | { |
| 496 | for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type) |
| 497 | { |
| 498 | av[i].a_type = av[av_count-1].a_type; |
| 499 | av[i].a_un.a_val = av[av_count-1].a_un.a_val; |
| 500 | av[--av_count].a_type = AT_NULL; |
| 501 | delete_count++; |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 506 | /* count how many values we have in new_av that aren't in av */ |
| 507 | for (j = 0; new_av[j].a_type != AT_NULL; j++) |
Alexandre Julliard | 6d2099f | 2004-06-01 19:47:11 +0000 | [diff] [blame] | 508 | { |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 509 | for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break; |
| 510 | if (i == av_count) new_count++; |
Alexandre Julliard | 6d2099f | 2004-06-01 19:47:11 +0000 | [diff] [blame] | 511 | } |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 512 | |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 513 | src = (char *)*stack; |
| 514 | dst = src - (new_count - delete_count) * sizeof(*av); |
| 515 | if (new_count > delete_count) /* need to make room for the extra values */ |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 516 | { |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 517 | int len = (char *)(av + av_count + 1) - src; |
| 518 | for (i = 0; i < len; i++) dst[i] = src[i]; |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 519 | } |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 520 | else if (new_count < delete_count) /* get rid of unused values */ |
| 521 | { |
Harald Hoyer | 2bea45d | 2010-07-29 12:02:06 +0200 | [diff] [blame] | 522 | int len = (char *)(av + av_count + 1) - src; |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 523 | for (i = len - 1; i >= 0; i--) dst[i] = src[i]; |
| 524 | } |
| 525 | *stack = dst; |
| 526 | av -= (new_count - delete_count); |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 527 | |
| 528 | /* now set the values */ |
| 529 | for (j = 0; new_av[j].a_type != AT_NULL; j++) |
| 530 | { |
| 531 | for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break; |
| 532 | if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val; |
| 533 | else |
| 534 | { |
| 535 | av[av_count].a_type = new_av[j].a_type; |
| 536 | av[av_count].a_un.a_val = new_av[j].a_un.a_val; |
| 537 | av_count++; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | #ifdef DUMP_AUX_INFO |
| 542 | wld_printf("New auxiliary info:\n"); |
| 543 | dump_auxiliary( av ); |
| 544 | #endif |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
| 548 | * get_auxiliary |
| 549 | * |
| 550 | * Get a field of the auxiliary structure |
| 551 | */ |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 552 | static int get_auxiliary( ElfW(auxv_t) *av, int type, int def_val ) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 553 | { |
| 554 | for ( ; av->a_type != AT_NULL; av++) |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 555 | if( av->a_type == type ) return av->a_un.a_val; |
| 556 | return def_val; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* |
| 560 | * map_so_lib |
| 561 | * |
| 562 | * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c |
| 563 | * |
| 564 | * This function maps the segments from an ELF object, and optionally |
| 565 | * stores information about the mapping into the auxv_t structure. |
| 566 | */ |
| 567 | static void map_so_lib( const char *name, struct wld_link_map *l) |
| 568 | { |
| 569 | int fd; |
| 570 | unsigned char buf[0x800]; |
| 571 | ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf; |
| 572 | ElfW(Phdr) *phdr, *ph; |
| 573 | /* Scan the program header table, collecting its load commands. */ |
| 574 | struct loadcmd |
| 575 | { |
| 576 | ElfW(Addr) mapstart, mapend, dataend, allocend; |
| 577 | off_t mapoff; |
| 578 | int prot; |
| 579 | } loadcmds[16], *c; |
| 580 | size_t nloadcmds = 0, maplength; |
| 581 | |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 582 | fd = wld_open( name, O_RDONLY ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 583 | if (fd == -1) fatal_error("%s: could not open\n", name ); |
| 584 | |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 585 | if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf)) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 586 | fatal_error("%s: failed to read ELF header\n", name); |
| 587 | |
| 588 | phdr = (void*) (((unsigned char*)buf) + header->e_phoff); |
| 589 | |
| 590 | if( ( header->e_ident[0] != 0x7f ) || |
| 591 | ( header->e_ident[1] != 'E' ) || |
| 592 | ( header->e_ident[2] != 'L' ) || |
| 593 | ( header->e_ident[3] != 'F' ) ) |
| 594 | fatal_error( "%s: not an ELF binary... don't know how to load it\n", name ); |
| 595 | |
| 596 | if( header->e_machine != EM_386 ) |
| 597 | fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name ); |
| 598 | |
| 599 | if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0])) |
| 600 | fatal_error( "%s: oops... not enough space for load commands\n", name ); |
| 601 | |
| 602 | maplength = header->e_phnum * sizeof (ElfW(Phdr)); |
| 603 | if (header->e_phoff + maplength > sizeof(buf)) |
| 604 | fatal_error( "%s: oops... not enough space for ELF headers\n", name ); |
| 605 | |
| 606 | l->l_ld = 0; |
| 607 | l->l_addr = 0; |
| 608 | l->l_phdr = 0; |
| 609 | l->l_phnum = header->e_phnum; |
| 610 | l->l_entry = header->e_entry; |
| 611 | l->l_interp = 0; |
| 612 | |
| 613 | for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph) |
| 614 | { |
| 615 | |
| 616 | #ifdef DUMP_SEGMENTS |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 617 | wld_printf( "ph = %p\n", ph ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 618 | wld_printf( " p_type = %x\n", ph->p_type ); |
| 619 | wld_printf( " p_flags = %x\n", ph->p_flags ); |
| 620 | wld_printf( " p_offset = %x\n", ph->p_offset ); |
| 621 | wld_printf( " p_vaddr = %x\n", ph->p_vaddr ); |
| 622 | wld_printf( " p_paddr = %x\n", ph->p_paddr ); |
| 623 | wld_printf( " p_filesz = %x\n", ph->p_filesz ); |
| 624 | wld_printf( " p_memsz = %x\n", ph->p_memsz ); |
| 625 | wld_printf( " p_align = %x\n", ph->p_align ); |
| 626 | #endif |
| 627 | |
| 628 | switch (ph->p_type) |
| 629 | { |
| 630 | /* These entries tell us where to find things once the file's |
| 631 | segments are mapped in. We record the addresses it says |
| 632 | verbatim, and later correct for the run-time load address. */ |
| 633 | case PT_DYNAMIC: |
| 634 | l->l_ld = (void *) ph->p_vaddr; |
| 635 | l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn); |
| 636 | break; |
| 637 | |
| 638 | case PT_PHDR: |
| 639 | l->l_phdr = (void *) ph->p_vaddr; |
| 640 | break; |
| 641 | |
| 642 | case PT_LOAD: |
| 643 | { |
| 644 | if ((ph->p_align & page_mask) != 0) |
| 645 | fatal_error( "%s: ELF load command alignment not page-aligned\n", name ); |
| 646 | |
| 647 | if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0) |
| 648 | fatal_error( "%s: ELF load command address/offset not properly aligned\n", name ); |
| 649 | |
| 650 | c = &loadcmds[nloadcmds++]; |
| 651 | c->mapstart = ph->p_vaddr & ~(ph->p_align - 1); |
| 652 | c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask); |
| 653 | c->dataend = ph->p_vaddr + ph->p_filesz; |
| 654 | c->allocend = ph->p_vaddr + ph->p_memsz; |
| 655 | c->mapoff = ph->p_offset & ~(ph->p_align - 1); |
| 656 | |
| 657 | c->prot = 0; |
| 658 | if (ph->p_flags & PF_R) |
| 659 | c->prot |= PROT_READ; |
| 660 | if (ph->p_flags & PF_W) |
| 661 | c->prot |= PROT_WRITE; |
| 662 | if (ph->p_flags & PF_X) |
| 663 | c->prot |= PROT_EXEC; |
| 664 | } |
| 665 | break; |
| 666 | |
| 667 | case PT_INTERP: |
| 668 | l->l_interp = ph->p_vaddr; |
| 669 | break; |
| 670 | |
| 671 | case PT_TLS: |
| 672 | /* |
| 673 | * We don't need to set anything up because we're |
| 674 | * emulating the kernel, not ld-linux.so.2 |
| 675 | * The ELF loader will set up the TLS data itself. |
| 676 | */ |
| 677 | case PT_SHLIB: |
| 678 | case PT_NOTE: |
| 679 | default: |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /* Now process the load commands and map segments into memory. */ |
| 685 | c = loadcmds; |
| 686 | |
| 687 | /* Length of the sections to be loaded. */ |
| 688 | maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart; |
| 689 | |
| 690 | if( header->e_type == ET_DYN ) |
| 691 | { |
| 692 | ElfW(Addr) mappref; |
| 693 | mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart) |
| 694 | - MAP_BASE_ADDR (l)); |
| 695 | |
| 696 | /* Remember which part of the address space this object uses. */ |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 697 | l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength, |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 698 | c->prot, MAP_COPY | MAP_FILE, |
| 699 | fd, c->mapoff); |
| 700 | /* wld_printf("set : offset = %x\n", c->mapoff); */ |
| 701 | /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */ |
| 702 | |
| 703 | l->l_map_end = l->l_map_start + maplength; |
| 704 | l->l_addr = l->l_map_start - c->mapstart; |
| 705 | |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 706 | wld_mprotect ((caddr_t) (l->l_addr + c->mapend), |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 707 | loadcmds[nloadcmds - 1].allocend - c->mapend, |
| 708 | PROT_NONE); |
| 709 | goto postmap; |
| 710 | } |
| 711 | else |
| 712 | { |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 713 | /* sanity check */ |
| 714 | if ((char *)c->mapstart + maplength > preloader_start && |
| 715 | (char *)c->mapstart <= preloader_end) |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 716 | fatal_error( "%s: binary overlaps preloader (%p-%p)\n", |
| 717 | name, (char *)c->mapstart, (char *)c->mapstart + maplength ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 718 | |
| 719 | ELF_FIXED_ADDRESS (loader, c->mapstart); |
| 720 | } |
| 721 | |
| 722 | /* Remember which part of the address space this object uses. */ |
| 723 | l->l_map_start = c->mapstart + l->l_addr; |
| 724 | l->l_map_end = l->l_map_start + maplength; |
| 725 | |
| 726 | while (c < &loadcmds[nloadcmds]) |
| 727 | { |
| 728 | if (c->mapend > c->mapstart) |
| 729 | /* Map the segment contents from the file. */ |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 730 | wld_mmap ((void *) (l->l_addr + c->mapstart), |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 731 | c->mapend - c->mapstart, c->prot, |
| 732 | MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff); |
| 733 | |
| 734 | postmap: |
| 735 | if (l->l_phdr == 0 |
| 736 | && (ElfW(Off)) c->mapoff <= header->e_phoff |
| 737 | && ((size_t) (c->mapend - c->mapstart + c->mapoff) |
| 738 | >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr)))) |
| 739 | /* Found the program header in this segment. */ |
| 740 | l->l_phdr = (void *)(unsigned int) (c->mapstart + header->e_phoff - c->mapoff); |
| 741 | |
| 742 | if (c->allocend > c->dataend) |
| 743 | { |
| 744 | /* Extra zero pages should appear at the end of this segment, |
| 745 | after the data mapped from the file. */ |
| 746 | ElfW(Addr) zero, zeroend, zeropage; |
| 747 | |
| 748 | zero = l->l_addr + c->dataend; |
| 749 | zeroend = l->l_addr + c->allocend; |
| 750 | zeropage = (zero + page_mask) & ~page_mask; |
| 751 | |
| 752 | /* |
| 753 | * This is different from the dl-load load... |
| 754 | * ld-linux.so.2 relies on the whole page being zero'ed |
| 755 | */ |
| 756 | zeroend = (zeroend + page_mask) & ~page_mask; |
| 757 | |
| 758 | if (zeroend < zeropage) |
| 759 | { |
| 760 | /* All the extra data is in the last page of the segment. |
| 761 | We can just zero it. */ |
| 762 | zeropage = zeroend; |
| 763 | } |
| 764 | |
| 765 | if (zeropage > zero) |
| 766 | { |
| 767 | /* Zero the final part of the last page of the segment. */ |
| 768 | if ((c->prot & PROT_WRITE) == 0) |
| 769 | { |
| 770 | /* Dag nab it. */ |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 771 | wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 772 | } |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 773 | wld_memset ((void *) zero, '\0', zeropage - zero); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 774 | if ((c->prot & PROT_WRITE) == 0) |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 775 | wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | if (zeroend > zeropage) |
| 779 | { |
| 780 | /* Map the remaining zero pages in from the zero fill FD. */ |
Austin English | df9fda7 | 2010-05-27 11:12:34 -0500 | [diff] [blame] | 781 | wld_mmap ((caddr_t) zeropage, zeroend - zeropage, |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 782 | c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED, |
| 783 | -1, 0); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | ++c; |
| 788 | } |
| 789 | |
| 790 | if (l->l_phdr == NULL) fatal_error("no program header\n"); |
| 791 | |
| 792 | l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr); |
| 793 | l->l_entry += l->l_addr; |
| 794 | |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 795 | wld_close( fd ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | |
Alexandre Julliard | 8aab67d | 2006-07-31 15:48:32 +0200 | [diff] [blame] | 799 | static unsigned int elf_hash( const char *name ) |
| 800 | { |
| 801 | unsigned int hi, hash = 0; |
| 802 | while (*name) |
| 803 | { |
| 804 | hash = (hash << 4) + (unsigned char)*name++; |
| 805 | hi = hash & 0xf0000000; |
| 806 | hash ^= hi; |
| 807 | hash ^= hi >> 24; |
| 808 | } |
| 809 | return hash; |
| 810 | } |
| 811 | |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 812 | static unsigned int gnu_hash( const char *name ) |
| 813 | { |
| 814 | unsigned int h = 5381; |
| 815 | while (*name) h = h * 33 + (unsigned char)*name++; |
| 816 | return h; |
| 817 | } |
| 818 | |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 819 | /* |
| 820 | * Find a symbol in the symbol table of the executable loaded |
| 821 | */ |
Alexandre Julliard | 51d8482 | 2006-07-31 16:44:40 +0200 | [diff] [blame] | 822 | static void *find_symbol( const ElfW(Phdr) *phdr, int num, const char *var, int type ) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 823 | { |
| 824 | const ElfW(Dyn) *dyn = NULL; |
| 825 | const ElfW(Phdr) *ph; |
| 826 | const ElfW(Sym) *symtab = NULL; |
Alexandre Julliard | 8aab67d | 2006-07-31 15:48:32 +0200 | [diff] [blame] | 827 | const Elf_Symndx *hashtab = NULL; |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 828 | const Elf32_Word *gnu_hashtab = NULL; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 829 | const char *strings = NULL; |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 830 | Elf_Symndx idx; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 831 | |
| 832 | /* check the values */ |
| 833 | #ifdef DUMP_SYMS |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 834 | wld_printf("%p %x\n", phdr, num ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 835 | #endif |
| 836 | if( ( phdr == NULL ) || ( num == 0 ) ) |
| 837 | { |
| 838 | wld_printf("could not find PT_DYNAMIC header entry\n"); |
| 839 | return NULL; |
| 840 | } |
| 841 | |
| 842 | /* parse the (already loaded) ELF executable's header */ |
| 843 | for (ph = phdr; ph < &phdr[num]; ++ph) |
| 844 | { |
| 845 | if( PT_DYNAMIC == ph->p_type ) |
| 846 | { |
| 847 | dyn = (void *) ph->p_vaddr; |
| 848 | num = ph->p_memsz / sizeof (Elf32_Dyn); |
| 849 | break; |
| 850 | } |
| 851 | } |
| 852 | if( !dyn ) return NULL; |
| 853 | |
| 854 | while( dyn->d_tag ) |
| 855 | { |
| 856 | if( dyn->d_tag == DT_STRTAB ) |
| 857 | strings = (const char*) dyn->d_un.d_ptr; |
| 858 | if( dyn->d_tag == DT_SYMTAB ) |
| 859 | symtab = (const ElfW(Sym) *)dyn->d_un.d_ptr; |
| 860 | if( dyn->d_tag == DT_HASH ) |
Alexandre Julliard | 8aab67d | 2006-07-31 15:48:32 +0200 | [diff] [blame] | 861 | hashtab = (const Elf_Symndx *)dyn->d_un.d_ptr; |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 862 | if( dyn->d_tag == DT_GNU_HASH ) |
| 863 | gnu_hashtab = (const Elf32_Word *)dyn->d_un.d_ptr; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 864 | #ifdef DUMP_SYMS |
| 865 | wld_printf("%x %x\n", dyn->d_tag, dyn->d_un.d_ptr ); |
| 866 | #endif |
| 867 | dyn++; |
| 868 | } |
| 869 | |
| 870 | if( (!symtab) || (!strings) ) return NULL; |
| 871 | |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 872 | if (gnu_hashtab) /* new style hash table */ |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 873 | { |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 874 | const unsigned int hash = gnu_hash(var); |
| 875 | const Elf32_Word nbuckets = gnu_hashtab[0]; |
| 876 | const Elf32_Word symbias = gnu_hashtab[1]; |
| 877 | const Elf32_Word nwords = gnu_hashtab[2]; |
| 878 | const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4); |
| 879 | const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords); |
| 880 | const Elf32_Word *chains = buckets + nbuckets - symbias; |
Alexandre Julliard | 8aab67d | 2006-07-31 15:48:32 +0200 | [diff] [blame] | 881 | |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 882 | if (!(idx = buckets[hash % nbuckets])) return NULL; |
| 883 | do |
| 884 | { |
| 885 | if ((chains[idx] & ~1u) == (hash & ~1u) && |
| 886 | symtab[idx].st_info == ELF32_ST_INFO( STB_GLOBAL, type ) && |
| 887 | !wld_strcmp( strings + symtab[idx].st_name, var )) |
| 888 | goto found; |
| 889 | } while (!(chains[idx++] & 1u)); |
| 890 | } |
| 891 | else if (hashtab) /* old style hash table */ |
| 892 | { |
| 893 | const unsigned int hash = elf_hash(var); |
| 894 | const Elf_Symndx nbuckets = hashtab[0]; |
| 895 | const Elf_Symndx *buckets = hashtab + 2; |
| 896 | const Elf_Symndx *chains = buckets + nbuckets; |
| 897 | |
| 898 | for (idx = buckets[hash % nbuckets]; idx != STN_UNDEF; idx = chains[idx]) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 899 | { |
Alexandre Julliard | 51d8482 | 2006-07-31 16:44:40 +0200 | [diff] [blame] | 900 | if (symtab[idx].st_info == ELF32_ST_INFO( STB_GLOBAL, type ) && |
Alexandre Julliard | 8aab67d | 2006-07-31 15:48:32 +0200 | [diff] [blame] | 901 | !wld_strcmp( strings + symtab[idx].st_name, var )) |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 902 | goto found; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | return NULL; |
Alexandre Julliard | 30a3866 | 2006-07-31 21:02:38 +0200 | [diff] [blame] | 906 | |
| 907 | found: |
| 908 | #ifdef DUMP_SYMS |
| 909 | wld_printf("Found %s -> %x\n", strings + symtab[idx].st_name, symtab[idx].st_value ); |
| 910 | #endif |
| 911 | return (void *)symtab[idx].st_value; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /* |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 915 | * preload_reserve |
| 916 | * |
| 917 | * Reserve a range specified in string format |
| 918 | */ |
| 919 | static void preload_reserve( const char *str ) |
| 920 | { |
| 921 | const char *p; |
| 922 | unsigned long result = 0; |
| 923 | void *start = NULL, *end = NULL; |
Alexandre Julliard | 195ca1e | 2008-04-14 20:38:17 +0200 | [diff] [blame] | 924 | int i, first = 1; |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 925 | |
| 926 | for (p = str; *p; p++) |
| 927 | { |
| 928 | if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0'; |
| 929 | else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10; |
| 930 | else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10; |
| 931 | else if (*p == '-') |
| 932 | { |
| 933 | if (!first) goto error; |
| 934 | start = (void *)(result & ~page_mask); |
| 935 | result = 0; |
| 936 | first = 0; |
| 937 | } |
| 938 | else goto error; |
| 939 | } |
| 940 | if (!first) end = (void *)((result + page_mask) & ~page_mask); |
| 941 | else if (result) goto error; /* single value '0' is allowed */ |
| 942 | |
| 943 | /* sanity checks */ |
| 944 | if (end <= start) start = end = NULL; |
| 945 | else if ((char *)end > preloader_start && |
| 946 | (char *)start <= preloader_end) |
| 947 | { |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 948 | wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %p-%p\n", |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 949 | start, end, preloader_start, preloader_end ); |
| 950 | start = end = NULL; |
| 951 | } |
| 952 | |
Alexandre Julliard | 195ca1e | 2008-04-14 20:38:17 +0200 | [diff] [blame] | 953 | /* check for overlap with low memory areas */ |
| 954 | for (i = 0; preload_info[i].size; i++) |
| 955 | { |
| 956 | if ((char *)preload_info[i].addr > (char *)0x00110000) break; |
| 957 | if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size) |
| 958 | { |
| 959 | start = end = NULL; |
| 960 | break; |
| 961 | } |
| 962 | if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size) |
| 963 | start = (char *)preload_info[i].addr + preload_info[i].size; |
| 964 | } |
Alexandre Julliard | 875b5db | 2006-07-19 15:06:16 +0200 | [diff] [blame] | 965 | |
Alexandre Julliard | 195ca1e | 2008-04-14 20:38:17 +0200 | [diff] [blame] | 966 | while (preload_info[i].size) i++; |
| 967 | preload_info[i].addr = start; |
| 968 | preload_info[i].size = (char *)end - (char *)start; |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 969 | return; |
| 970 | |
| 971 | error: |
| 972 | fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str ); |
| 973 | } |
| 974 | |
Alexandre Julliard | 13029a2 | 2007-01-11 12:35:42 +0100 | [diff] [blame] | 975 | /* check if address is in one of the reserved ranges */ |
| 976 | static int is_addr_reserved( const void *addr ) |
| 977 | { |
| 978 | int i; |
| 979 | |
| 980 | for (i = 0; preload_info[i].size; i++) |
| 981 | { |
| 982 | if ((const char *)addr >= (const char *)preload_info[i].addr && |
| 983 | (const char *)addr < (const char *)preload_info[i].addr + preload_info[i].size) |
| 984 | return 1; |
| 985 | } |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | /* remove a range from the preload list */ |
| 990 | static void remove_preload_range( int i ) |
| 991 | { |
| 992 | while (preload_info[i].size) |
| 993 | { |
| 994 | preload_info[i].addr = preload_info[i+1].addr; |
| 995 | preload_info[i].size = preload_info[i+1].size; |
| 996 | i++; |
| 997 | } |
| 998 | } |
| 999 | |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 1000 | /* |
| 1001 | * is_in_preload_range |
| 1002 | * |
| 1003 | * Check if address of the given aux value is in one of the reserved ranges |
| 1004 | */ |
| 1005 | static int is_in_preload_range( const ElfW(auxv_t) *av, int type ) |
| 1006 | { |
Alexandre Julliard | 13029a2 | 2007-01-11 12:35:42 +0100 | [diff] [blame] | 1007 | while (av->a_type != AT_NULL) |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 1008 | { |
Alexandre Julliard | 13029a2 | 2007-01-11 12:35:42 +0100 | [diff] [blame] | 1009 | if (av->a_type == type) return is_addr_reserved( (const void *)av->a_un.a_val ); |
| 1010 | av++; |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 1011 | } |
| 1012 | return 0; |
| 1013 | } |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 1014 | |
Alexandre Julliard | c20d6c4 | 2006-03-31 19:16:22 +0200 | [diff] [blame] | 1015 | /* set the process name if supported */ |
| 1016 | static void set_process_name( int argc, char *argv[] ) |
| 1017 | { |
Joris Huizer | e1e54d1 | 2007-03-03 05:30:55 -0800 | [diff] [blame] | 1018 | int i; |
| 1019 | unsigned int off; |
Alexandre Julliard | c20d6c4 | 2006-03-31 19:16:22 +0200 | [diff] [blame] | 1020 | char *p, *name, *end; |
| 1021 | |
| 1022 | /* set the process short name */ |
| 1023 | for (p = name = argv[1]; *p; p++) if (p[0] == '/' && p[1]) name = p + 1; |
| 1024 | if (wld_prctl( 15 /* PR_SET_NAME */, (int)name ) == -1) return; |
| 1025 | |
| 1026 | /* find the end of the argv array and move everything down */ |
| 1027 | end = argv[argc - 1]; |
| 1028 | while (*end) end++; |
| 1029 | off = argv[1] - argv[0]; |
| 1030 | for (p = argv[1]; p <= end; p++) *(p - off) = *p; |
| 1031 | wld_memset( end - off, 0, off ); |
| 1032 | for (i = 1; i < argc; i++) argv[i] -= off; |
| 1033 | } |
| 1034 | |
| 1035 | |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 1036 | /* |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1037 | * wld_start |
| 1038 | * |
| 1039 | * Repeat the actions the kernel would do when loading a dynamically linked .so |
| 1040 | * Load the binary and then its ELF interpreter. |
| 1041 | * Note, we assume that the binary is a dynamically linked ELF shared object. |
| 1042 | */ |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 1043 | void* wld_start( void **stack ) |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1044 | { |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 1045 | int i, *pargc; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1046 | char **argv, **p; |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 1047 | char *interp, *reserve = NULL; |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 1048 | ElfW(auxv_t) new_av[12], delete_av[3], *av; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1049 | struct wld_link_map main_binary_map, ld_so_map; |
| 1050 | struct wine_preload_info **wine_main_preload_info; |
| 1051 | |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 1052 | pargc = *stack; |
| 1053 | argv = (char **)pargc + 1; |
Alexandre Julliard | c319392 | 2004-06-15 20:31:06 +0000 | [diff] [blame] | 1054 | if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1055 | |
| 1056 | /* skip over the parameters */ |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 1057 | p = argv + *pargc + 1; |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1058 | |
| 1059 | /* skip over the environment */ |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 1060 | while (*p) |
| 1061 | { |
| 1062 | static const char res[] = "WINEPRELOADRESERVE="; |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 1063 | if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1; |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 1064 | p++; |
| 1065 | } |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1066 | |
| 1067 | av = (ElfW(auxv_t)*) (p+1); |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 1068 | page_size = get_auxiliary( av, AT_PAGESZ, 4096 ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1069 | page_mask = page_size - 1; |
| 1070 | |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 1071 | preloader_start = (char *)_start - ((unsigned int)_start & page_mask); |
| 1072 | preloader_end = (char *)((unsigned int)(_end + page_mask) & ~page_mask); |
| 1073 | |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1074 | #ifdef DUMP_AUX_INFO |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 1075 | wld_printf( "stack = %p\n", *stack ); |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 1076 | for( i = 0; i < *pargc; i++ ) wld_printf("argv[%x] = %s\n", i, argv[i]); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1077 | dump_auxiliary( av ); |
| 1078 | #endif |
| 1079 | |
| 1080 | /* reserve memory that Wine needs */ |
Alexandre Julliard | 92425aa | 2004-06-02 21:40:00 +0000 | [diff] [blame] | 1081 | if (reserve) preload_reserve( reserve ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1082 | for (i = 0; preload_info[i].size; i++) |
Alexandre Julliard | 13029a2 | 2007-01-11 12:35:42 +0100 | [diff] [blame] | 1083 | { |
| 1084 | if (wld_mmap( preload_info[i].addr, preload_info[i].size, PROT_NONE, |
| 1085 | MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 ) == (void *)-1) |
| 1086 | { |
Alexandre Julliard | 7a4d575 | 2008-05-05 17:19:24 +0200 | [diff] [blame] | 1087 | /* don't warn for low 64k */ |
| 1088 | if (preload_info[i].addr >= (void *)0x10000) |
| 1089 | wld_printf( "preloader: Warning: failed to reserve range %p-%p\n", |
| 1090 | preload_info[i].addr, (char *)preload_info[i].addr + preload_info[i].size ); |
Alexandre Julliard | 13029a2 | 2007-01-11 12:35:42 +0100 | [diff] [blame] | 1091 | remove_preload_range( i ); |
| 1092 | i--; |
| 1093 | } |
| 1094 | } |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1095 | |
Alexandre Julliard | 0216c40 | 2006-04-12 19:53:16 +0200 | [diff] [blame] | 1096 | /* add an executable page at the top of the address space to defeat |
| 1097 | * broken no-exec protections that play with the code selector limit */ |
Alexandre Julliard | 13029a2 | 2007-01-11 12:35:42 +0100 | [diff] [blame] | 1098 | if (is_addr_reserved( (char *)0x80000000 - page_size )) |
| 1099 | wld_mprotect( (char *)0x80000000 - page_size, page_size, PROT_EXEC | PROT_READ ); |
Alexandre Julliard | 0216c40 | 2006-04-12 19:53:16 +0200 | [diff] [blame] | 1100 | |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1101 | /* load the main binary */ |
Alexandre Julliard | c319392 | 2004-06-15 20:31:06 +0000 | [diff] [blame] | 1102 | map_so_lib( argv[1], &main_binary_map ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1103 | |
| 1104 | /* load the ELF interpreter */ |
| 1105 | interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp; |
| 1106 | map_so_lib( interp, &ld_so_map ); |
| 1107 | |
| 1108 | /* store pointer to the preload info into the appropriate main binary variable */ |
| 1109 | wine_main_preload_info = find_symbol( main_binary_map.l_phdr, main_binary_map.l_phnum, |
Alexandre Julliard | 51d8482 | 2006-07-31 16:44:40 +0200 | [diff] [blame] | 1110 | "wine_main_preload_info", STT_OBJECT ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1111 | if (wine_main_preload_info) *wine_main_preload_info = preload_info; |
| 1112 | else wld_printf( "wine_main_preload_info not found\n" ); |
| 1113 | |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 1114 | #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val); |
| 1115 | SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr ); |
| 1116 | SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) ); |
| 1117 | SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum ); |
| 1118 | SET_NEW_AV( 3, AT_PAGESZ, page_size ); |
| 1119 | SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr ); |
| 1120 | SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) ); |
| 1121 | SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry ); |
Alexandre Julliard | bfac60b | 2004-06-22 02:42:05 +0000 | [diff] [blame] | 1122 | SET_NEW_AV( 7, AT_UID, get_auxiliary( av, AT_UID, wld_getuid() ) ); |
| 1123 | SET_NEW_AV( 8, AT_EUID, get_auxiliary( av, AT_EUID, wld_geteuid() ) ); |
| 1124 | SET_NEW_AV( 9, AT_GID, get_auxiliary( av, AT_GID, wld_getgid() ) ); |
| 1125 | SET_NEW_AV(10, AT_EGID, get_auxiliary( av, AT_EGID, wld_getegid() ) ); |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 1126 | SET_NEW_AV(11, AT_NULL, 0 ); |
Alexandre Julliard | 9f33a4b | 2004-06-03 23:36:01 +0000 | [diff] [blame] | 1127 | #undef SET_NEW_AV |
| 1128 | |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 1129 | i = 0; |
| 1130 | /* delete sysinfo values if addresses conflict */ |
Alexandre Julliard | 4d9f3df | 2007-01-02 12:21:45 +0100 | [diff] [blame] | 1131 | if (is_in_preload_range( av, AT_SYSINFO ) || is_in_preload_range( av, AT_SYSINFO_EHDR )) |
| 1132 | { |
| 1133 | delete_av[i++].a_type = AT_SYSINFO; |
| 1134 | delete_av[i++].a_type = AT_SYSINFO_EHDR; |
| 1135 | } |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 1136 | delete_av[i].a_type = AT_NULL; |
| 1137 | |
Alexandre Julliard | c319392 | 2004-06-15 20:31:06 +0000 | [diff] [blame] | 1138 | /* get rid of first argument */ |
Alexandre Julliard | c20d6c4 | 2006-03-31 19:16:22 +0200 | [diff] [blame] | 1139 | set_process_name( *pargc, argv ); |
Alexandre Julliard | c319392 | 2004-06-15 20:31:06 +0000 | [diff] [blame] | 1140 | pargc[1] = pargc[0] - 1; |
| 1141 | *stack = pargc + 1; |
| 1142 | |
Alexandre Julliard | f558741 | 2004-06-26 00:12:20 +0000 | [diff] [blame] | 1143 | set_auxiliary_values( av, new_av, delete_av, stack ); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1144 | |
| 1145 | #ifdef DUMP_AUX_INFO |
Alexandre Julliard | 092ac1f | 2007-01-11 12:46:06 +0100 | [diff] [blame] | 1146 | wld_printf("new stack = %p\n", *stack); |
Alexandre Julliard | b1abca8 | 2004-05-28 20:59:22 +0000 | [diff] [blame] | 1147 | wld_printf("jumping to %x\n", ld_so_map.l_entry); |
| 1148 | #endif |
| 1149 | |
| 1150 | return (void *)ld_so_map.l_entry; |
| 1151 | } |