TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / loader / main.c
blobbb5000684d33ca2136498df353288974aa6857d0
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_SYS_MMAN_H
28 # include <sys/mman.h>
29 #endif
30 #ifdef HAVE_SYS_RESOURCE_H
31 # include <sys/resource.h>
32 #endif
33 #ifdef HAVE_SYS_SYSCALL_H
34 # include <sys/syscall.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <pthread.h>
41 #include "wine/library.h"
42 #include "main.h"
44 #ifdef __APPLE__
46 #ifndef __clang__
47 __asm__(".zerofill WINE_DOS, WINE_DOS, ___wine_dos, 0x40000000");
48 __asm__(".zerofill WINE_SHAREDHEAP, WINE_SHAREDHEAP, ___wine_shared_heap, 0x03000000");
49 extern char __wine_dos[0x40000000], __wine_shared_heap[0x03000000];
50 #else
51 __asm__(".zerofill WINE_DOS, WINE_DOS");
52 __asm__(".zerofill WINE_SHAREDHEAP, WINE_SHAREDHEAP");
53 static char __wine_dos[0x40000000] __attribute__((section("WINE_DOS, WINE_DOS")));
54 static char __wine_shared_heap[0x03000000] __attribute__((section("WINE_SHAREDHEAP, WINE_SHAREDHEAP")));
55 #endif
57 static const struct wine_preload_info wine_main_preload_info[] =
59 { __wine_dos, sizeof(__wine_dos) }, /* DOS area + PE exe */
60 { __wine_shared_heap, sizeof(__wine_shared_heap) }, /* shared user data + shared heap */
61 { 0, 0 } /* end of list */
64 static inline void reserve_area( void *addr, size_t size )
66 wine_anon_mmap( addr, size, PROT_NONE, MAP_FIXED | MAP_NORESERVE );
67 wine_mmap_add_reserved_area( addr, size );
70 #else /* __APPLE__ */
72 /* the preloader will set this variable */
73 const struct wine_preload_info *wine_main_preload_info = NULL;
75 static inline void reserve_area( void *addr, size_t size )
77 wine_mmap_add_reserved_area( addr, size );
80 #endif /* __APPLE__ */
82 /***********************************************************************
83 * check_command_line
85 * Check if command line is one that needs to be handled specially.
87 static void check_command_line( int argc, char *argv[] )
89 static const char usage[] =
90 "Usage: wine PROGRAM [ARGUMENTS...] Run the specified program\n"
91 " wine --help Display this help and exit\n"
92 " wine --version Output version information and exit";
94 if (argc <= 1)
96 fprintf( stderr, "%s\n", usage );
97 exit(1);
99 if (!strcmp( argv[1], "--help" ))
101 printf( "%s\n", usage );
102 exit(0);
104 if (!strcmp( argv[1], "--version" ))
106 printf( "%s\n", wine_get_build_id() );
107 exit(0);
112 #if defined(__linux__) && (defined(__i386__) || defined(__arm__))
114 #ifdef __i386__
115 /* separate thread to check for NPTL and TLS features */
116 static void *needs_pthread( void *arg )
118 pid_t tid = syscall( 224 /* SYS_gettid */ );
119 /* check for NPTL */
120 if (tid != -1 && tid != getpid()) return (void *)1;
121 /* check for TLS glibc */
122 if (wine_get_gs() != 0) return (void *)1;
123 /* check for exported epoll_create to detect new glibc versions without TLS */
124 if (wine_dlsym( RTLD_DEFAULT, "epoll_create", NULL, 0 ))
125 fprintf( stderr,
126 "wine: glibc >= 2.3 without NPTL or TLS is not a supported combination.\n"
127 " Please upgrade to a glibc with NPTL support.\n" );
128 else
129 fprintf( stderr,
130 "wine: Your C library is too old. You need at least glibc 2.3 with NPTL support.\n" );
131 return 0;
134 /* check if we support the glibc threading model */
135 static void check_threading(void)
137 pthread_t id;
138 void *ret;
140 pthread_create( &id, NULL, needs_pthread, NULL );
141 pthread_join( id, &ret );
142 if (!ret) exit(1);
144 #else
145 static void check_threading(void)
148 #endif
150 static void check_vmsplit( void *stack )
152 if (stack < (void *)0x80000000)
154 /* if the stack is below 0x80000000, assume we can safely try a munmap there */
155 if (munmap( (void *)0x80000000, 1 ) == -1 && errno == EINVAL)
156 fprintf( stderr,
157 "Warning: memory above 0x80000000 doesn't seem to be accessible.\n"
158 "Wine requires a 3G/1G user/kernel memory split to work properly.\n" );
162 static void set_max_limit( int limit )
164 struct rlimit rlimit;
166 if (!getrlimit( limit, &rlimit ))
168 rlimit.rlim_cur = rlimit.rlim_max;
169 setrlimit( limit, &rlimit );
173 static int pre_exec(void)
175 int temp;
177 check_threading();
178 check_vmsplit( &temp );
179 set_max_limit( RLIMIT_AS );
180 #ifdef __i386__
181 return 1; /* we have a preloader on x86 */
182 #else
183 return 0;
184 #endif
187 #elif defined(__linux__) && defined(__x86_64__)
189 static int pre_exec(void)
191 return 1; /* we have a preloader on x86-64 */
194 #elif (defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__))
196 static int pre_exec(void)
198 struct rlimit rl;
200 rl.rlim_cur = 0x02000000;
201 rl.rlim_max = 0x02000000;
202 setrlimit( RLIMIT_DATA, &rl );
203 return 1;
206 #else
208 static int pre_exec(void)
210 return 0; /* no exec needed */
213 #endif
215 #ifdef __linux__
217 extern typeof(pthread_create) *call_pthread_create, *__glob_pthread_create;
218 extern typeof(pthread_detach) *call_pthread_detach, *__glob_pthread_detach;
219 extern typeof(pthread_join) *call_pthread_join, *__glob_pthread_join;
221 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
222 void *(*start_routine) (void *), void *arg)
224 return call_pthread_create(thread, attr, start_routine, arg);
227 int pthread_detach(pthread_t thread)
229 return call_pthread_detach(thread);
232 int pthread_join(pthread_t thread, void **retval)
234 return call_pthread_join(thread, retval);
237 static void init_thread_hook(void) {
238 call_pthread_create = __glob_pthread_create = dlvsym(RTLD_NEXT, "pthread_create", "GLIBC_2.2.5");
239 if (!__glob_pthread_create)
240 call_pthread_create = __glob_pthread_create = dlvsym(RTLD_NEXT, "pthread_create", "GLIBC_2.1");
242 call_pthread_detach = __glob_pthread_detach = dlsym(RTLD_NEXT, "pthread_detach");
243 call_pthread_join = __glob_pthread_join = dlsym(RTLD_NEXT, "pthread_join");
245 /* Call a function from libpthread to ensure being linked against it */
246 pthread_yield();
249 #else
251 #define init_thread_hook()
253 #endif
255 /**********************************************************************
256 * main
258 int main( int argc, char *argv[] )
260 char error[1024];
261 int i;
263 init_thread_hook();
265 if (!getenv( "WINELOADERNOEXEC" )) /* first time around */
267 static char noexec[] = "WINELOADERNOEXEC=1";
269 putenv( noexec );
270 check_command_line( argc, argv );
271 if (pre_exec())
273 wine_init_argv0_path( argv[0] );
274 wine_exec_wine_binary( NULL, argv, getenv( "WINELOADER" ));
275 fprintf( stderr, "wine: could not exec the wine loader\n" );
276 exit(1);
280 #ifndef __APPLE__
281 if (wine_main_preload_info)
282 #endif
284 for (i = 0; wine_main_preload_info[i].size; i++)
285 reserve_area( wine_main_preload_info[i].addr, wine_main_preload_info[i].size );
288 wine_init( argc, argv, error, sizeof(error) );
289 fprintf( stderr, "wine: failed to initialize: %s\n", error );
290 exit(1);