#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
#ifdef __APPLE__
# include <mach-o/dyld.h>
#endif
#include "main.h"
#if defined(__APPLE__) && defined(__x86_64__) && !defined(HAVE_WINE_PRELOADER)
__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) },
{ __wine_top_down, sizeof(__wine_top_down) },
{ 0, 0 }
};
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++)
{
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
const __attribute((visibility("default"))) struct wine_preload_info *wine_main_preload_info = NULL;
static void init_reserved_areas(void)
{
}
#endif
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;
}
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;
}
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;
}
static const char *get_self_exe(void)
{
#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 );
#elif defined(__APPLE__)
uint32_t path_size = PATH_MAX;
char *path = malloc( path_size );
if (path && !_NSGetExecutablePath( path, &path_size ))
return path;
free( path );
#endif
return NULL;
}
static void *try_dlopen( const char *argv0 )
{
char *dir, *path, *p;
void *handle;
if (!argv0) return NULL;
if (!(dir = realpath_dirname( argv0 ))) return NULL;
if ((p = remove_tail( dir, "/loader" )))
path = build_path( p, "dlls/ntdll/ntdll.so" );
else
path = build_path( dir, "ntdll.so" );
handle = dlopen( path, RTLD_NOW );
free( p );
free( dir );
free( path );
return handle;
}
int main( int argc, char *argv[] )
{
void *handle;
init_reserved_areas();
if ((handle = try_dlopen( get_self_exe() )) ||
(handle = try_dlopen( 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() );
exit(1);
}