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
29 #include <sys/types.h>
34 #ifdef HAVE_SYS_SYSCTL_H
35 # include <sys/sysctl.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)
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);
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)
87 /* canonicalize path and return its directory name */
88 static char *realpath_dirname( const char *name
)
90 char *p
, *fullpath
= realpath( name
, NULL
);
94 p
= strrchr( fullpath
, '/' );
95 if (p
== fullpath
) p
++;
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
);
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;
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
);
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
)
133 unsigned int dotdots
= 0;
137 while (*from
== '/') from
++;
138 while (*dest
== '/') dest
++;
139 start
= dest
; /* save start of next path element */
142 while (*from
&& *from
!= '/' && *from
== *dest
) { from
++; dest
++; }
143 if ((!*from
|| *from
== '/') && (!*dest
|| *dest
== '/')) continue;
145 do /* count remaining elements in 'from' */
148 while (*from
&& *from
!= '/') from
++;
149 while (*from
== '/') from
++;
155 ret
= malloc( strlen(base
) + 3 * dotdots
+ strlen(start
) + 2 );
157 while (dotdots
--) strcat( ret
, "/.." );
159 if (!start
[0]) return ret
;
161 strcat( ret
, start
);
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 ))
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
))
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
);
207 static void *load_ntdll( char *argv0
)
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/"
220 const char *self
= get_self_exe( argv0
);
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" );
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" );
246 if (!handle
&& !self
) handle
= try_dlopen( LIBDIR
, "wine/" SO_DIR
"ntdll.so" );
252 /**********************************************************************
255 int main( int argc
, char *argv
[] )
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" );
269 fprintf( stderr
, "wine: could not load ntdll.so: %s\n", dlerror() );
270 pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */