1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*
* Emulator initialisation code
*
* Copyright 2000 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dlfcn.h>
#include <limits.h>
#ifdef HAVE_SYS_SYSCTL_H
# include <sys/sysctl.h>
#endif
#include "main.h"
#if defined(__APPLE__) && defined(__x86_64__) && !defined(HAVE_WINE_PRELOADER)
/* Not using the preloader on x86_64:
* Reserve the same areas as the preloader does, but using zero-fill sections
* (the only way to prevent system frameworks from using them, including allocations
* before main() runs).
*/
__asm__(".zerofill WINE_RESERVE,WINE_RESERVE");
static char __wine_reserve[0x1fffff000] __attribute__((section("WINE_RESERVE, WINE_RESERVE")));
__asm__(".zerofill WINE_TOP_DOWN,WINE_TOP_DOWN");
static char __wine_top_down[0x001ff0000] __attribute__((section("WINE_TOP_DOWN, WINE_TOP_DOWN")));
static const struct wine_preload_info preload_info[] =
{
{ __wine_reserve, sizeof(__wine_reserve) }, /* 0x1000 - 0x200000000: low 8GB */
{ __wine_top_down, sizeof(__wine_top_down) }, /* 0x7ff000000000 - 0x7ff001ff0000: top-down allocations + virtual heap */
{ 0, 0 } /* end of list */
};
const __attribute((visibility("default"))) struct wine_preload_info *wine_main_preload_info = preload_info;
static void init_reserved_areas(void)
{
int i;
for (i = 0; wine_main_preload_info[i].size != 0; i++)
{
/* Match how the preloader maps reserved areas: */
mmap(wine_main_preload_info[i].addr, wine_main_preload_info[i].size, PROT_NONE,
MAP_FIXED | MAP_NORESERVE | MAP_PRIVATE | MAP_ANON, -1, 0);
}
}
#else
/* the preloader will set this variable */
const __attribute((visibility("default"))) struct wine_preload_info *wine_main_preload_info = NULL;
static void init_reserved_areas(void)
{
}
#endif
/* canonicalize path and return its directory name */
static char *realpath_dirname( const char *name )
{
char *p, *fullpath = realpath( name, NULL );
if (fullpath)
{
p = strrchr( fullpath, '/' );
if (p == fullpath) p++;
if (p) *p = 0;
}
return fullpath;
}
/* if string ends with tail, remove it */
static char *remove_tail( const char *str, const char *tail )
{
size_t len = strlen( str );
size_t tail_len = strlen( tail );
char *ret;
if (len < tail_len) return NULL;
if (strcmp( str + len - tail_len, tail )) return NULL;
ret = malloc( len - tail_len + 1 );
memcpy( ret, str, len - tail_len );
ret[len - tail_len] = 0;
return ret;
}
/* build a path from the specified dir and name */
static char *build_path( const char *dir, const char *name )
{
size_t len = strlen( dir );
char *ret = malloc( len + strlen( name ) + 2 );
memcpy( ret, dir, len );
if (len && ret[len - 1] != '/') ret[len++] = '/';
strcpy( ret + len, name );
return ret;
}
/* build a path with the relative dir from 'from' to 'dest' appended to base */
static char *build_relative_path( const char *base, const char *from, const char *dest )
{
const char *start;
char *ret;
unsigned int dotdots = 0;
for (;;)
{
while (*from == '/') from++;
while (*dest == '/') dest++;
start = dest; /* save start of next path element */
if (!*from) break;
while (*from && *from != '/' && *from == *dest) { from++; dest++; }
if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
do /* count remaining elements in 'from' */
{
dotdots++;
while (*from && *from != '/') from++;
while (*from == '/') from++;
}
while (*from);
break;
}
ret = malloc( strlen(base) + 3 * dotdots + strlen(start) + 2 );
strcpy( ret, base );
while (dotdots--) strcat( ret, "/.." );
if (!start[0]) return ret;
strcat( ret, "/" );
strcat( ret, start );
return ret;
}
static const char *get_self_exe( char *argv0 )
{
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
return "/proc/self/exe";
#elif defined (__FreeBSD__) || defined(__DragonFly__)
static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
size_t path_size = PATH_MAX;
char *path = malloc( path_size );
if (path && !sysctl( pathname, sizeof(pathname)/sizeof(pathname[0]), path, &path_size, NULL, 0 ))
return path;
free( path );
#endif
if (!strchr( argv0, '/' )) /* search in PATH */
{
char *p, *path = getenv( "PATH" );
if (!path || !(path = strdup(path))) return NULL;
for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
{
char *name = build_path( p, argv0 );
if (!access( name, X_OK ))
{
free( path );
return name;
}
free( name );
}
free( path );
return NULL;
}
return argv0;
}
static void *try_dlopen( const char *dir, const char *name )
{
char *path = build_path( dir, name );
void *handle = dlopen( path, RTLD_NOW );
free( path );
return handle;
}
static void *load_ntdll( char *argv0 )
{
#ifdef __i386__
#define SO_DIR "i386-unix/"
#elif defined(__x86_64__)
#define SO_DIR "x86_64-unix/"
#elif defined(__arm__)
#define SO_DIR "arm-unix/"
#elif defined(__aarch64__)
#define SO_DIR "aarch64-unix/"
#else
#define SO_DIR ""
#endif
const char *self = get_self_exe( argv0 );
char *path, *p;
void *handle = NULL;
if (self && ((path = realpath_dirname( self ))))
{
if ((p = remove_tail( path, "/loader" )))
handle = try_dlopen( p, "dlls/ntdll/ntdll.so" );
else if ((p = build_relative_path( path, BINDIR, LIBDIR )))
handle = try_dlopen( p, SO_DIR "ntdll.so" );
free( p );
free( path );
}
if (!handle && (path = getenv( "WINEDLLPATH" )))
{
path = strdup( path );
for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
{
handle = try_dlopen( p, SO_DIR "ntdll.so" );
if (!handle) handle = try_dlopen( p, "ntdll.so" );
if (handle) break;
}
free( path );
}
if (!handle && !self) handle = try_dlopen( LIBDIR, SO_DIR "ntdll.so" );
return handle;
}
/**********************************************************************
* main
*/
int main( int argc, char *argv[] )
{
void *handle;
init_reserved_areas();
if ((handle = load_ntdll( argv[0] )))
{
void (*init_func)(int, char **) = dlsym( handle, "__wine_main" );
if (init_func) init_func( argc, argv );
fprintf( stderr, "wine: __wine_main function not found in ntdll.so\n" );
exit(1);
}
fprintf( stderr, "wine: could not load ntdll.so: %s\n", dlerror() );
pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */
exit(1);
}
|