2 * Emulator initialisation code
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
27 #ifdef HAVE_SYS_RESOURCE_H
28 # include <sys/resource.h>
30 #ifdef HAVE_SYS_SYSCALL_H
31 # include <sys/syscall.h>
38 #include "wine/library.h"
43 __asm__(".zerofill WINE_DOS, WINE_DOS, ___wine_dos, 0x40000000");
44 __asm__(".zerofill WINE_SHAREDHEAP, WINE_SHAREDHEAP, ___wine_shared_heap, 0x03000000");
45 extern char __wine_dos
[0x40000000], __wine_shared_heap
[0x03000000];
47 static const struct wine_preload_info wine_main_preload_info
[] =
49 { __wine_dos
, sizeof(__wine_dos
) }, /* DOS area + PE exe */
50 { __wine_shared_heap
, sizeof(__wine_shared_heap
) }, /* shared user data + shared heap */
51 { 0, 0 } /* end of list */
54 static inline void reserve_area( void *addr
, size_t size
)
56 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_FIXED
| MAP_NORESERVE
);
57 wine_mmap_add_reserved_area( addr
, size
);
62 /* the preloader will set this variable */
63 const struct wine_preload_info
*wine_main_preload_info
= NULL
;
65 static inline void reserve_area( void *addr
, size_t size
)
67 wine_mmap_add_reserved_area( addr
, size
);
70 #endif /* __APPLE__ */
72 /***********************************************************************
75 * Check if command line is one that needs to be handled specially.
77 static void check_command_line( int argc
, char *argv
[] )
79 static const char usage
[] =
80 "Usage: wine PROGRAM [ARGUMENTS...] Run the specified program\n"
81 " wine --help Display this help and exit\n"
82 " wine --version Output version information and exit";
86 fprintf( stderr
, "%s\n", usage
);
89 if (!strcmp( argv
[1], "--help" ))
91 printf( "%s\n", usage
);
94 if (!strcmp( argv
[1], "--version" ))
96 printf( "%s\n", wine_get_build_id() );
102 #if defined(__linux__) && defined(__i386__)
104 /* separate thread to check for NPTL and TLS features */
105 static void *needs_pthread( void *arg
)
107 pid_t tid
= syscall( SYS_gettid
);
109 if (tid
!= -1 && tid
!= getpid()) return (void *)1;
110 /* check for TLS glibc */
111 if (wine_get_gs() != 0) return (void *)1;
112 /* check for exported epoll_create to detect new glibc versions without TLS */
113 if (wine_dlsym( RTLD_DEFAULT
, "epoll_create", NULL
, 0 ))
115 "wine: glibc >= 2.3 without NPTL or TLS is not a supported combination.\n"
116 " Please upgrade to a glibc with NPTL support.\n" );
119 "wine: Your C library is too old. You need at least glibc 2.3 with NPTL support.\n" );
123 /* check if we support the glibc threading model */
124 static void check_threading(void)
129 pthread_create( &id
, NULL
, needs_pthread
, NULL
);
130 pthread_join( id
, &ret
);
134 static void check_vmsplit( void *stack
)
136 if (stack
< (void *)0x80000000)
138 /* if the stack is below 0x80000000, assume we can safely try a munmap there */
139 if (wine_munmap( (void *)0x80000000, 1 ) == -1 && errno
== EINVAL
)
141 "Warning: memory above 0x80000000 doesn't seem to be accessible.\n"
142 "Wine requires a 3G/1G user/kernel memory split to work properly.\n" );
146 static void set_max_limit( int limit
)
148 struct rlimit rlimit
;
150 if (!getrlimit( limit
, &rlimit
))
152 rlimit
.rlim_cur
= rlimit
.rlim_max
;
153 setrlimit( limit
, &rlimit
);
157 static int pre_exec(void)
162 check_vmsplit( &temp
);
163 set_max_limit( RLIMIT_AS
);
167 #elif defined(__linux__) && defined(__x86_64__)
169 static int pre_exec(void)
171 return 1; /* we have a preloader on x86-64 */
174 #elif (defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)) && defined(__i386__)
176 static int pre_exec(void)
180 rl
.rlim_cur
= 0x02000000;
181 rl
.rlim_max
= 0x02000000;
182 setrlimit( RLIMIT_DATA
, &rl
);
188 static int pre_exec(void)
190 return 0; /* no exec needed */
196 /***********************************************************************
197 * overrides for mmap, mmap64, munmap
199 /* Because off_t can change size depending on _FILE_OFFSET_BITS=64 being set,
200 * we can't rely on it here. In most cases, the default off_t type follows the
201 * same pattern as long (4 bytes on 32-bit, 8 on 64).
202 * On FreeBSD, however, off_t is always a 64-bit type.
204 #if defined(__FreeBSD__)
205 typedef off_t offset_t
;
207 typedef long offset_t
;
210 void *mmap( void *addr
, size_t size
, int prot
, int flags
, int fd
, offset_t offset
)
212 return wine_hook_mmap( addr
, size
, prot
, flags
, fd
, offset
);
215 void *mmap64( void *addr
, size_t size
, int prot
, int flags
, int fd
, off64_t offset
)
217 return wine_hook_mmap( addr
, size
, prot
, flags
, fd
, offset
);
220 int munmap( void *addr
, size_t size
)
222 return wine_hook_munmap( addr
, size
);
226 /* Linux also has a mremap function, which allows changing the size and address
227 * of a particular mapping */
228 void *mremap( void *addr
, size_t old_len
, size_t new_len
, int flags
, ... )
230 if((flags
& 2)) /* MREMAP_FIXED */
235 va_start(args
, flags
);
236 extra
= va_arg(args
, void*);
239 return wine_hook_mremap(addr
, old_len
, new_len
, flags
, extra
);
241 return wine_hook_mremap(addr
, old_len
, new_len
, flags
);
245 /**********************************************************************
246 * init_mmap_functions
248 static void init_mmap_functions(void)
250 if (sizeof(offset_t
) == sizeof(LONGLONG
))
251 wine_hook_mmap
= wine_mmap
= wine_dlsym( RTLD_NEXT
, "mmap", NULL
, 0 );
253 wine_hook_mmap
= wine_mmap
= wine_dlsym( RTLD_NEXT
, "mmap64", NULL
, 0 );
254 wine_hook_munmap
= wine_munmap
= wine_dlsym( RTLD_NEXT
, "munmap", NULL
, 0 );
256 if (wine_mmap
== NULL
)
258 fprintf( stderr
, "Could not find mmap function\n" );
261 if (wine_munmap
== NULL
)
263 fprintf( stderr
, "Could not find munmap function\n" );
268 wine_hook_mremap
= wine_mremap
= wine_dlsym( RTLD_NEXT
, "mremap", NULL
, 0 );
269 if (wine_mremap
== NULL
)
271 fprintf( stderr
, "Could not find mremap function\n" );
278 /**********************************************************************
281 int main( int argc
, char *argv
[] )
286 init_mmap_functions();
288 if (!getenv( "WINELOADERNOEXEC" )) /* first time around */
290 static char noexec
[] = "WINELOADERNOEXEC=1";
293 check_command_line( argc
, argv
);
296 wine_init_argv0_path( argv
[0] );
297 wine_exec_wine_binary( NULL
, argv
, getenv( "WINELOADER" ));
298 fprintf( stderr
, "wine: could not exec the wine loader\n" );
304 if (wine_main_preload_info
)
307 for (i
= 0; wine_main_preload_info
[i
].size
; i
++)
308 reserve_area( wine_main_preload_info
[i
].addr
, wine_main_preload_info
[i
].size
);
311 wine_init( argc
, argv
, error
, sizeof(error
) );
312 fprintf( stderr
, "wine: failed to initialize: %s\n", error
);