preloader: Fix moving of auxiliary values.
[wine] / loader / main.c
1 /*
2  * Emulator initialisation code
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <pthread.h>
37
38 #include "wine/library.h"
39 #include "main.h"
40
41 #ifdef __APPLE__
42
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];
46
47 static const struct wine_preload_info wine_main_preload_info[] =
48 {
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 */
52 };
53
54 static inline void reserve_area( void *addr, size_t size )
55 {
56     wine_anon_mmap( addr, size, PROT_NONE, MAP_FIXED | MAP_NORESERVE );
57     wine_mmap_add_reserved_area( addr, size );
58 }
59
60 #else  /* __APPLE__ */
61
62 /* the preloader will set this variable */
63 const struct wine_preload_info *wine_main_preload_info = NULL;
64
65 static inline void reserve_area( void *addr, size_t size )
66 {
67     wine_mmap_add_reserved_area( addr, size );
68 }
69
70 #endif  /* __APPLE__ */
71
72 /***********************************************************************
73  *           check_command_line
74  *
75  * Check if command line is one that needs to be handled specially.
76  */
77 static void check_command_line( int argc, char *argv[] )
78 {
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";
83
84     if (argc <= 1)
85     {
86         fprintf( stderr, "%s\n", usage );
87         exit(1);
88     }
89     if (!strcmp( argv[1], "--help" ))
90     {
91         printf( "%s\n", usage );
92         exit(0);
93     }
94     if (!strcmp( argv[1], "--version" ))
95     {
96         printf( "%s\n", wine_get_build_id() );
97         exit(0);
98     }
99 }
100
101
102 #if defined(__linux__) && defined(__i386__)
103
104 /* separate thread to check for NPTL and TLS features */
105 static void *needs_pthread( void *arg )
106 {
107     pid_t tid = gettid();
108     /* check for NPTL */
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 ))
114         fprintf( stderr,
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" );
117     else
118         fprintf( stderr,
119                  "wine: Your C library is too old. You need at least glibc 2.3 with NPTL support.\n" );
120     return 0;
121 }
122
123 /* check if we support the glibc threading model */
124 static void check_threading(void)
125 {
126     pthread_t id;
127     void *ret;
128
129     pthread_create( &id, NULL, needs_pthread, NULL );
130     pthread_join( id, &ret );
131     if (!ret) exit(1);
132 }
133
134 static void check_vmsplit( void *stack )
135 {
136     if (stack < (void *)0x80000000)
137     {
138         /* if the stack is below 0x80000000, assume we can safely try a munmap there */
139         if (munmap( (void *)0x80000000, 1 ) == -1 && errno == EINVAL)
140             fprintf( stderr,
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" );
143     }
144 }
145
146 static void set_max_limit( int limit )
147 {
148     struct rlimit rlimit;
149
150     if (!getrlimit( limit, &rlimit ))
151     {
152         rlimit.rlim_cur = rlimit.rlim_max;
153         setrlimit( limit, &rlimit );
154     }
155 }
156
157 static int pre_exec(void)
158 {
159     int temp;
160
161     check_threading();
162     check_vmsplit( &temp );
163     set_max_limit( RLIMIT_AS );
164     return 1;
165 }
166
167 #elif defined(__FreeBSD__) && defined(__i386__)
168
169 static int pre_exec(void)
170 {
171     struct rlimit rl;
172
173     rl.rlim_cur = 0x02000000;
174     rl.rlim_max = 0x02000000;
175     setrlimit( RLIMIT_DATA, &rl );
176     return 1;
177 }
178
179 #else
180
181 static int pre_exec(void)
182 {
183     return 0;  /* no exec needed */
184 }
185
186 #endif
187
188
189 /**********************************************************************
190  *           main
191  */
192 int main( int argc, char *argv[] )
193 {
194     char error[1024];
195     int i;
196
197     if (!getenv( "WINELOADERNOEXEC" ))  /* first time around */
198     {
199         static char noexec[] = "WINELOADERNOEXEC=1";
200
201         putenv( noexec );
202         check_command_line( argc, argv );
203         if (pre_exec())
204         {
205             wine_init_argv0_path( argv[0] );
206             wine_exec_wine_binary( NULL, argv, getenv( "WINELOADER" ));
207             fprintf( stderr, "wine: could not exec the wine loader\n" );
208             exit(1);
209         }
210     }
211
212     if (wine_main_preload_info)
213     {
214         for (i = 0; wine_main_preload_info[i].size; i++)
215             reserve_area( wine_main_preload_info[i].addr, wine_main_preload_info[i].size );
216     }
217
218     wine_init( argc, argv, error, sizeof(error) );
219     fprintf( stderr, "wine: failed to initialize: %s\n", error );
220     exit(1);
221 }