include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / loader / main.c
blobe5de475001552fb8b5664786583ef2ac1892d008
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"
23 #include <fcntl.h>
24 #include <pthread.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <dlfcn.h>
33 #include <limits.h>
34 #ifdef HAVE_SYS_SYSCTL_H
35 # include <sys/sysctl.h>
36 #endif
38 #include "main.h"
40 extern char **environ;
42 #if defined(__APPLE__) && defined(__x86_64__) && !defined(HAVE_WINE_PRELOADER)
44 /* Not using the preloader on x86_64:
45 * Reserve the same areas as the preloader does, but using zero-fill sections
46 * (the only way to prevent system frameworks from using them, including allocations
47 * before main() runs).
49 __asm__(".zerofill WINE_RESERVE,WINE_RESERVE");
50 static char __wine_reserve[0x1fffff000] __attribute__((section("WINE_RESERVE, WINE_RESERVE")));
52 __asm__(".zerofill WINE_TOP_DOWN,WINE_TOP_DOWN");
53 static char __wine_top_down[0x001ff0000] __attribute__((section("WINE_TOP_DOWN, WINE_TOP_DOWN")));
55 static const struct wine_preload_info preload_info[] =
57 { __wine_reserve, sizeof(__wine_reserve) }, /* 0x1000 - 0x200000000: low 8GB */
58 { __wine_top_down, sizeof(__wine_top_down) }, /* 0x7ff000000000 - 0x7ff001ff0000: top-down allocations + virtual heap */
59 { 0, 0 } /* end of list */
62 const __attribute((visibility("default"))) struct wine_preload_info *wine_main_preload_info = preload_info;
64 static void init_reserved_areas(void)
66 int i;
68 for (i = 0; wine_main_preload_info[i].size != 0; i++)
70 /* Match how the preloader maps reserved areas: */
71 mmap(wine_main_preload_info[i].addr, wine_main_preload_info[i].size, PROT_NONE,
72 MAP_FIXED | MAP_NORESERVE | MAP_PRIVATE | MAP_ANON, -1, 0);
76 #else
78 /* the preloader will set this variable */
79 const __attribute((visibility("default"))) struct wine_preload_info *wine_main_preload_info = NULL;
81 static void init_reserved_areas(void)
85 #endif
87 /* canonicalize path and return its directory name */
88 static char *realpath_dirname( const char *name )
90 char *p, *fullpath = realpath( name, NULL );
92 if (fullpath)
94 p = strrchr( fullpath, '/' );
95 if (p == fullpath) p++;
96 if (p) *p = 0;
98 return fullpath;
101 /* if string ends with tail, remove it */
102 static char *remove_tail( const char *str, const char *tail )
104 size_t len = strlen( str );
105 size_t tail_len = strlen( tail );
106 char *ret;
108 if (len < tail_len) return NULL;
109 if (strcmp( str + len - tail_len, tail )) return NULL;
110 ret = malloc( len - tail_len + 1 );
111 memcpy( ret, str, len - tail_len );
112 ret[len - tail_len] = 0;
113 return ret;
116 /* build a path from the specified dir and name */
117 static char *build_path( const char *dir, const char *name )
119 size_t len = strlen( dir );
120 char *ret = malloc( len + strlen( name ) + 2 );
122 memcpy( ret, dir, len );
123 if (len && ret[len - 1] != '/') ret[len++] = '/';
124 strcpy( ret + len, name );
125 return ret;
128 /* build a path with the relative dir from 'from' to 'dest' appended to base */
129 static char *build_relative_path( const char *base, const char *from, const char *dest )
131 const char *start;
132 char *ret;
133 unsigned int dotdots = 0;
135 for (;;)
137 while (*from == '/') from++;
138 while (*dest == '/') dest++;
139 start = dest; /* save start of next path element */
140 if (!*from) break;
142 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
143 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
145 do /* count remaining elements in 'from' */
147 dotdots++;
148 while (*from && *from != '/') from++;
149 while (*from == '/') from++;
151 while (*from);
152 break;
155 ret = malloc( strlen(base) + 3 * dotdots + strlen(start) + 2 );
156 strcpy( ret, base );
157 while (dotdots--) strcat( ret, "/.." );
159 if (!start[0]) return ret;
160 strcat( ret, "/" );
161 strcat( ret, start );
162 return ret;
165 static const char *get_self_exe( char *argv0 )
167 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
168 return "/proc/self/exe";
169 #elif defined (__FreeBSD__) || defined(__DragonFly__)
170 static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
171 size_t path_size = PATH_MAX;
172 char *path = malloc( path_size );
173 if (path && !sysctl( pathname, sizeof(pathname)/sizeof(pathname[0]), path, &path_size, NULL, 0 ))
174 return path;
175 free( path );
176 #endif
178 if (!strchr( argv0, '/' )) /* search in PATH */
180 char *p, *path = getenv( "PATH" );
182 if (!path || !(path = strdup(path))) return NULL;
183 for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
185 char *name = build_path( p, argv0 );
186 if (!access( name, X_OK ))
188 free( path );
189 return name;
191 free( name );
193 free( path );
194 return NULL;
196 return argv0;
199 static void *try_dlopen( const char *dir, const char *name )
201 char *path = build_path( dir, name );
202 void *handle = dlopen( path, RTLD_NOW );
203 free( path );
204 return handle;
207 static void *load_ntdll( char *argv0 )
209 #ifdef __i386__
210 #define SO_DIR "i386-unix/"
211 #elif defined(__x86_64__)
212 #define SO_DIR "x86_64-unix/"
213 #elif defined(__arm__)
214 #define SO_DIR "arm-unix/"
215 #elif defined(__aarch64__)
216 #define SO_DIR "aarch64-unix/"
217 #else
218 #define SO_DIR ""
219 #endif
220 const char *self = get_self_exe( argv0 );
221 char *path, *p;
222 void *handle = NULL;
224 if (self && ((path = realpath_dirname( self ))))
226 if ((p = remove_tail( path, "/loader" )))
227 handle = try_dlopen( p, "dlls/ntdll/ntdll.so" );
228 else if ((p = build_relative_path( path, BINDIR, LIBDIR )))
229 handle = try_dlopen( p, "wine/" SO_DIR "ntdll.so" );
230 free( p );
231 free( path );
234 if (!handle && (path = getenv( "WINEDLLPATH" )))
236 path = strdup( path );
237 for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
239 handle = try_dlopen( p, SO_DIR "ntdll.so" );
240 if (!handle) handle = try_dlopen( p, "ntdll.so" );
241 if (handle) break;
243 free( path );
246 if (!handle && !self) handle = try_dlopen( LIBDIR, "wine/" SO_DIR "ntdll.so" );
248 return handle;
252 /**********************************************************************
253 * main
255 int main( int argc, char *argv[] )
257 void *handle;
259 init_reserved_areas();
261 if ((handle = load_ntdll( argv[0] )))
263 void (*init_func)(int, char **, char **) = dlsym( handle, "__wine_main" );
264 if (init_func) init_func( argc, argv, environ );
265 fprintf( stderr, "wine: __wine_main function not found in ntdll.so\n" );
266 exit(1);
269 fprintf( stderr, "wine: could not load ntdll.so: %s\n", dlerror() );
270 pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */
271 exit(1);