2020-05-14 15:17:06 +02:00
|
|
|
/*
|
|
|
|
* Unix interface for loader functions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#pragma makedep unix
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2020-05-17 11:11:28 +02:00
|
|
|
#include <errno.h>
|
2020-05-14 15:17:06 +02:00
|
|
|
#include <stdarg.h>
|
2020-05-14 15:26:32 +02:00
|
|
|
#include <stdio.h>
|
2020-05-19 14:08:16 +02:00
|
|
|
#include <signal.h>
|
2020-06-23 19:39:12 +02:00
|
|
|
#ifdef HAVE_LINK_H
|
|
|
|
# include <link.h>
|
|
|
|
#endif
|
2020-05-19 12:35:54 +02:00
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
# include <pwd.h>
|
|
|
|
#endif
|
2020-06-18 14:14:19 +02:00
|
|
|
#ifdef HAVE_ELF_H
|
|
|
|
# include <elf.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LINK_H
|
|
|
|
# include <link.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_AUXV_H
|
|
|
|
# include <sys/auxv.h>
|
|
|
|
#endif
|
2020-05-14 15:17:06 +02:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
# include <sys/mman.h>
|
|
|
|
#endif
|
2020-05-17 11:11:28 +02:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
# include <sys/resource.h>
|
|
|
|
#endif
|
2020-05-19 17:37:08 +02:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
2020-05-14 15:17:06 +02:00
|
|
|
#ifdef __APPLE__
|
2020-05-14 15:26:32 +02:00
|
|
|
# include <CoreFoundation/CoreFoundation.h>
|
|
|
|
# define LoadResource MacLoadResource
|
|
|
|
# define GetCurrentThread MacGetCurrentThread
|
|
|
|
# include <CoreServices/CoreServices.h>
|
|
|
|
# undef LoadResource
|
|
|
|
# undef GetCurrentThread
|
|
|
|
# include <pthread.h>
|
2020-06-18 14:14:19 +02:00
|
|
|
# include <mach/mach.h>
|
|
|
|
# include <mach/mach_error.h>
|
2020-05-14 15:17:06 +02:00
|
|
|
# include <mach-o/getsect.h>
|
2020-05-19 14:08:16 +02:00
|
|
|
# include <crt_externs.h>
|
|
|
|
# include <spawn.h>
|
|
|
|
# ifndef _POSIX_SPAWN_DISABLE_ASLR
|
|
|
|
# define _POSIX_SPAWN_DISABLE_ASLR 0x0100
|
|
|
|
# endif
|
2020-05-14 15:17:06 +02:00
|
|
|
#endif
|
2020-12-08 12:45:18 +01:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
# include <jni.h>
|
|
|
|
#endif
|
2020-05-14 15:17:06 +02:00
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winnt.h"
|
2020-05-20 14:04:32 +02:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnls.h"
|
2020-06-23 12:32:43 +02:00
|
|
|
#include "winioctl.h"
|
2020-05-14 15:17:06 +02:00
|
|
|
#include "winternl.h"
|
2020-05-14 15:33:36 +02:00
|
|
|
#include "unix_private.h"
|
2020-06-23 19:39:12 +02:00
|
|
|
#include "wine/list.h"
|
2020-05-17 11:07:02 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2020-06-23 19:39:12 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(module);
|
2020-05-14 15:17:06 +02:00
|
|
|
|
2021-04-28 11:40:39 +02:00
|
|
|
#ifdef __i386__
|
|
|
|
static const char so_dir[] = "/i386-unix";
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
static const char so_dir[] = "/x86_64-unix";
|
|
|
|
#elif defined(__arm__)
|
|
|
|
static const char so_dir[] = "/arm-unix";
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
static const char so_dir[] = "/aarch64-unix";
|
|
|
|
#else
|
|
|
|
static const char so_dir[] = "";
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 11:57:32 +02:00
|
|
|
void (WINAPI *pDbgUiRemoteBreakin)( void *arg ) = NULL;
|
2020-08-11 15:25:55 +02:00
|
|
|
NTSTATUS (WINAPI *pKiRaiseUserExceptionDispatcher)(void) = NULL;
|
2020-08-06 19:31:24 +02:00
|
|
|
void (WINAPI *pKiUserApcDispatcher)(CONTEXT*,ULONG_PTR,ULONG_PTR,ULONG_PTR,PNTAPCFUNC) = NULL;
|
2020-06-17 11:57:32 +02:00
|
|
|
NTSTATUS (WINAPI *pKiUserExceptionDispatcher)(EXCEPTION_RECORD*,CONTEXT*) = NULL;
|
|
|
|
void (WINAPI *pLdrInitializeThunk)(CONTEXT*,void**,ULONG_PTR,ULONG_PTR) = NULL;
|
|
|
|
void (WINAPI *pRtlUserThreadStart)( PRTL_THREAD_START_ROUTINE entry, void *arg ) = NULL;
|
2021-06-09 15:19:43 +02:00
|
|
|
void (WINAPI *p__wine_ctrl_routine)(void*);
|
2021-07-07 11:39:57 +02:00
|
|
|
SYSTEM_DLL_INIT_BLOCK *pLdrSystemDllInitBlock = NULL;
|
2020-06-17 11:57:32 +02:00
|
|
|
|
2020-09-02 15:06:05 +02:00
|
|
|
static NTSTATUS (CDECL *p__wine_set_unix_funcs)( int version, const struct unix_funcs *funcs );
|
2020-05-14 15:17:06 +02:00
|
|
|
|
2020-05-19 12:35:54 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
static void fatal_error( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
|
|
|
|
#endif
|
|
|
|
|
2020-05-19 14:08:16 +02:00
|
|
|
#if defined(linux) || defined(__APPLE__)
|
|
|
|
static const BOOL use_preloader = TRUE;
|
|
|
|
#else
|
|
|
|
static const BOOL use_preloader = FALSE;
|
|
|
|
#endif
|
|
|
|
|
2020-05-19 12:35:54 +02:00
|
|
|
static char *argv0;
|
|
|
|
static const char *bin_dir;
|
|
|
|
static const char *dll_dir;
|
2021-04-28 11:40:39 +02:00
|
|
|
static const char *ntdll_dir;
|
2020-05-19 12:35:54 +02:00
|
|
|
static SIZE_T dll_path_maxlen;
|
2021-04-19 20:08:13 +02:00
|
|
|
static int *p___wine_main_argc;
|
|
|
|
static char ***p___wine_main_argv;
|
|
|
|
static WCHAR ***p___wine_main_wargv;
|
2020-05-17 11:44:16 +02:00
|
|
|
|
2020-06-24 09:20:33 +02:00
|
|
|
const char *home_dir = NULL;
|
2020-05-29 14:17:55 +02:00
|
|
|
const char *data_dir = NULL;
|
|
|
|
const char *build_dir = NULL;
|
|
|
|
const char *config_dir = NULL;
|
2020-06-24 09:21:48 +02:00
|
|
|
const char **dll_paths = NULL;
|
2020-06-24 09:20:33 +02:00
|
|
|
const char *user_name = NULL;
|
2021-04-07 10:46:34 +02:00
|
|
|
SECTION_IMAGE_INFORMATION main_image_info = { NULL };
|
2020-09-21 13:41:28 +02:00
|
|
|
static HMODULE ntdll_module;
|
|
|
|
static const IMAGE_EXPORT_DIRECTORY *ntdll_exports;
|
2020-06-23 19:39:12 +02:00
|
|
|
|
2020-05-14 15:17:06 +02:00
|
|
|
/* adjust an array of pointers to make them into RVAs */
|
|
|
|
static inline void fixup_rva_ptrs( void *array, BYTE *base, unsigned int count )
|
|
|
|
{
|
|
|
|
BYTE **src = array;
|
|
|
|
DWORD *dst = array;
|
|
|
|
|
|
|
|
for ( ; count; count--, src++, dst++) *dst = *src ? *src - base : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fixup an array of RVAs by adding the specified delta */
|
|
|
|
static inline void fixup_rva_dwords( DWORD *ptr, int delta, unsigned int count )
|
|
|
|
{
|
|
|
|
for ( ; count; count--, ptr++) if (*ptr) *ptr += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* fixup an array of name/ordinal RVAs by adding the specified delta */
|
|
|
|
static inline void fixup_rva_names( UINT_PTR *ptr, int delta )
|
|
|
|
{
|
|
|
|
for ( ; *ptr; ptr++) if (!(*ptr & IMAGE_ORDINAL_FLAG)) *ptr += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* fixup RVAs in the resource directory */
|
|
|
|
static void fixup_so_resources( IMAGE_RESOURCE_DIRECTORY *dir, BYTE *root, int delta )
|
|
|
|
{
|
|
|
|
IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
|
|
|
|
{
|
|
|
|
void *ptr = root + entry->u2.s2.OffsetToDirectory;
|
|
|
|
if (entry->u2.s2.DataIsDirectory) fixup_so_resources( ptr, root, delta );
|
|
|
|
else fixup_rva_dwords( &((IMAGE_RESOURCE_DATA_ENTRY *)ptr)->OffsetToData, delta, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:35:54 +02:00
|
|
|
/* die on a fatal error; use only during initialization */
|
|
|
|
static void fatal_error( const char *err, ... )
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start( args, err );
|
|
|
|
fprintf( stderr, "wine: " );
|
|
|
|
vfprintf( stderr, err, args );
|
|
|
|
va_end( args );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-05-19 14:08:30 +02:00
|
|
|
static void set_max_limit( int limit )
|
|
|
|
{
|
|
|
|
struct rlimit rlimit;
|
|
|
|
|
|
|
|
if (!getrlimit( limit, &rlimit ))
|
|
|
|
{
|
|
|
|
rlimit.rlim_cur = rlimit.rlim_max;
|
2020-09-14 14:59:02 +02:00
|
|
|
if (setrlimit( limit, &rlimit ) != 0)
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__) && defined(RLIMIT_NOFILE) && defined(OPEN_MAX)
|
|
|
|
/* On Leopard, setrlimit(RLIMIT_NOFILE, ...) fails on attempts to set
|
|
|
|
* rlim_cur above OPEN_MAX (even if rlim_max > OPEN_MAX). */
|
|
|
|
if (limit == RLIMIT_NOFILE && rlimit.rlim_cur > OPEN_MAX)
|
|
|
|
{
|
|
|
|
rlimit.rlim_cur = OPEN_MAX;
|
|
|
|
setrlimit( limit, &rlimit );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-05-19 14:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:35:54 +02:00
|
|
|
/* 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++] = '/';
|
2021-04-26 12:43:10 +02:00
|
|
|
if (name[0] == '/') name++;
|
2020-05-19 12:35:54 +02:00
|
|
|
strcpy( ret + len, name );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-26 12:43:10 +02:00
|
|
|
|
|
|
|
static const char *get_pe_dir( WORD machine )
|
|
|
|
{
|
|
|
|
if (!machine) machine = current_machine;
|
|
|
|
|
|
|
|
switch(machine)
|
|
|
|
{
|
|
|
|
case IMAGE_FILE_MACHINE_I386: return "/i386-windows";
|
|
|
|
case IMAGE_FILE_MACHINE_AMD64: return "/x86_64-windows";
|
|
|
|
case IMAGE_FILE_MACHINE_ARMNT: return "/arm-windows";
|
|
|
|
case IMAGE_FILE_MACHINE_ARM64: return "/aarch64-windows";
|
|
|
|
default: return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-19 12:35:54 +02:00
|
|
|
static void set_dll_path(void)
|
|
|
|
{
|
|
|
|
char *p, *path = getenv( "WINEDLLPATH" );
|
|
|
|
int i, count = 0;
|
|
|
|
|
|
|
|
if (path) for (p = path, count = 1; *p; p++) if (*p == ':') count++;
|
|
|
|
|
|
|
|
dll_paths = malloc( (count + 2) * sizeof(*dll_paths) );
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
if (!build_dir) dll_paths[count++] = dll_dir;
|
|
|
|
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
path = strdup(path);
|
|
|
|
for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" )) dll_paths[count++] = strdup( p );
|
|
|
|
free( path );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) dll_path_maxlen = max( dll_path_maxlen, strlen(dll_paths[i]) );
|
|
|
|
dll_paths[count] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-24 09:20:33 +02:00
|
|
|
static void set_home_dir(void)
|
|
|
|
{
|
|
|
|
const char *home = getenv( "HOME" );
|
|
|
|
const char *name = getenv( "USER" );
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (!home || !name)
|
|
|
|
{
|
|
|
|
struct passwd *pwd = getpwuid( getuid() );
|
|
|
|
if (pwd)
|
|
|
|
{
|
|
|
|
if (!home) home = pwd->pw_dir;
|
|
|
|
if (!name) name = pwd->pw_name;
|
|
|
|
}
|
|
|
|
if (!name) name = "wine";
|
|
|
|
}
|
|
|
|
if ((p = strrchr( name, '/' ))) name = p + 1;
|
|
|
|
if ((p = strrchr( name, '\\' ))) name = p + 1;
|
|
|
|
home_dir = strdup( home );
|
|
|
|
user_name = strdup( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-19 12:35:54 +02:00
|
|
|
static void set_config_dir(void)
|
|
|
|
{
|
|
|
|
char *p, *dir;
|
|
|
|
const char *prefix = getenv( "WINEPREFIX" );
|
|
|
|
|
|
|
|
if (prefix)
|
|
|
|
{
|
|
|
|
if (prefix[0] != '/')
|
|
|
|
fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
|
|
|
|
config_dir = dir = strdup( prefix );
|
|
|
|
for (p = dir + strlen(dir) - 1; p > dir && *p == '/'; p--) *p = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-24 09:20:33 +02:00
|
|
|
if (!home_dir) fatal_error( "could not determine your home directory\n" );
|
|
|
|
if (home_dir[0] != '/') fatal_error( "the home directory %s is not an absolute path\n", home_dir );
|
|
|
|
config_dir = build_path( home_dir, ".wine" );
|
2020-05-19 12:35:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:41:20 +01:00
|
|
|
static void init_paths( char *argv[] )
|
2020-05-19 12:35:54 +02:00
|
|
|
{
|
|
|
|
Dl_info info;
|
|
|
|
|
|
|
|
argv0 = strdup( argv[0] );
|
|
|
|
|
2021-04-28 11:40:39 +02:00
|
|
|
if (!dladdr( init_paths, &info ) || !(ntdll_dir = realpath_dirname( info.dli_fname )))
|
2020-05-19 12:35:54 +02:00
|
|
|
fatal_error( "cannot get path to ntdll.so\n" );
|
|
|
|
|
2021-04-28 11:40:39 +02:00
|
|
|
if (!(build_dir = remove_tail( ntdll_dir, "/dlls/ntdll" )))
|
2020-05-19 12:35:54 +02:00
|
|
|
{
|
2021-04-28 11:40:39 +02:00
|
|
|
if (!(dll_dir = remove_tail( ntdll_dir, so_dir ))) dll_dir = ntdll_dir;
|
2020-12-08 12:41:20 +01:00
|
|
|
#if (defined(__linux__) && !defined(__ANDROID__)) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
|
2020-05-19 12:35:54 +02:00
|
|
|
bin_dir = realpath_dirname( "/proc/self/exe" );
|
|
|
|
#elif defined (__FreeBSD__) || defined(__DragonFly__)
|
|
|
|
bin_dir = realpath_dirname( "/proc/curproc/file" );
|
|
|
|
#else
|
|
|
|
bin_dir = realpath_dirname( argv0 );
|
|
|
|
#endif
|
|
|
|
if (!bin_dir) bin_dir = build_path( dll_dir, DLL_TO_BINDIR );
|
|
|
|
data_dir = build_path( bin_dir, BIN_TO_DATADIR );
|
|
|
|
}
|
|
|
|
|
|
|
|
set_dll_path();
|
2020-06-24 09:20:33 +02:00
|
|
|
set_home_dir();
|
2020-05-19 12:35:54 +02:00
|
|
|
set_config_dir();
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:17:06 +02:00
|
|
|
|
2020-05-19 14:08:16 +02:00
|
|
|
static void preloader_exec( char **argv )
|
|
|
|
{
|
|
|
|
if (use_preloader)
|
|
|
|
{
|
|
|
|
static const char *preloader = "wine-preloader";
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!(p = strrchr( argv[1], '/' ))) p = argv[1];
|
|
|
|
else p++;
|
|
|
|
|
|
|
|
if (strlen(p) > 2 && !strcmp( p + strlen(p) - 2, "64" )) preloader = "wine64-preloader";
|
|
|
|
argv[0] = malloc( p - argv[1] + strlen(preloader) + 1 );
|
|
|
|
memcpy( argv[0], argv[1], p - argv[1] );
|
|
|
|
strcpy( argv[0] + (p - argv[1]), preloader );
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
{
|
|
|
|
posix_spawnattr_t attr;
|
|
|
|
posix_spawnattr_init( &attr );
|
|
|
|
posix_spawnattr_setflags( &attr, POSIX_SPAWN_SETEXEC | _POSIX_SPAWN_DISABLE_ASLR );
|
|
|
|
posix_spawn( NULL, argv[0], NULL, &attr, argv, *_NSGetEnviron() );
|
|
|
|
posix_spawnattr_destroy( &attr );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
execv( argv[0], argv );
|
|
|
|
free( argv[0] );
|
|
|
|
}
|
|
|
|
execv( argv[1], argv + 1 );
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:38:43 +01:00
|
|
|
static NTSTATUS loader_exec( const char *loader, char **argv, WORD machine )
|
2020-05-19 14:08:16 +02:00
|
|
|
{
|
|
|
|
char *p, *path;
|
|
|
|
|
|
|
|
if (build_dir)
|
|
|
|
{
|
2021-03-17 10:38:43 +01:00
|
|
|
argv[1] = build_path( build_dir, (machine == IMAGE_FILE_MACHINE_AMD64) ? "loader/wine64" : "loader/wine" );
|
2020-05-19 14:08:16 +02:00
|
|
|
preloader_exec( argv );
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p = strrchr( loader, '/' ))) loader = p + 1;
|
|
|
|
|
|
|
|
argv[1] = build_path( bin_dir, loader );
|
|
|
|
preloader_exec( argv );
|
|
|
|
|
|
|
|
argv[1] = getenv( "WINELOADER" );
|
|
|
|
if (argv[1]) preloader_exec( argv );
|
|
|
|
|
|
|
|
if ((path = getenv( "PATH" )))
|
|
|
|
{
|
|
|
|
for (p = strtok( strdup( path ), ":" ); p; p = strtok( NULL, ":" ))
|
|
|
|
{
|
|
|
|
argv[1] = build_path( p, loader );
|
|
|
|
preloader_exec( argv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argv[1] = build_path( BINDIR, loader );
|
|
|
|
preloader_exec( argv );
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* exec_wineloader
|
|
|
|
*
|
|
|
|
* argv[0] and argv[1] must be reserved for the preloader and loader respectively.
|
|
|
|
*/
|
2020-07-03 19:42:45 +02:00
|
|
|
NTSTATUS exec_wineloader( char **argv, int socketfd, const pe_image_info_t *pe_info )
|
2020-05-19 14:08:16 +02:00
|
|
|
{
|
2021-03-19 22:01:42 +01:00
|
|
|
WORD machine = pe_info->machine;
|
2020-07-03 19:42:45 +02:00
|
|
|
ULONGLONG res_start = pe_info->base;
|
|
|
|
ULONGLONG res_end = pe_info->base + pe_info->map_size;
|
2020-05-19 14:08:16 +02:00
|
|
|
const char *loader = argv0;
|
|
|
|
const char *loader_env = getenv( "WINELOADER" );
|
|
|
|
char preloader_reserve[64], socket_env[64];
|
2021-05-03 11:02:21 +02:00
|
|
|
BOOL is_child_64bit;
|
2020-05-19 14:08:16 +02:00
|
|
|
|
2021-04-19 18:28:33 +02:00
|
|
|
if (pe_info->image_flags & IMAGE_FLAGS_WineFakeDll) res_start = res_end = 0;
|
2021-05-03 11:02:21 +02:00
|
|
|
if (pe_info->image_flags & IMAGE_FLAGS_ComPlusNativeReady) machine = native_machine;
|
2021-04-19 18:28:33 +02:00
|
|
|
|
2021-05-03 11:02:21 +02:00
|
|
|
is_child_64bit = is_machine_64bit( machine );
|
2021-03-19 22:01:42 +01:00
|
|
|
|
2020-05-19 14:08:16 +02:00
|
|
|
if (!is_win64 ^ !is_child_64bit)
|
|
|
|
{
|
|
|
|
/* remap WINELOADER to the alternate 32/64-bit version if necessary */
|
|
|
|
if (loader_env)
|
|
|
|
{
|
|
|
|
int len = strlen( loader_env );
|
|
|
|
char *env = malloc( sizeof("WINELOADER=") + len + 2 );
|
|
|
|
|
|
|
|
if (!env) return STATUS_NO_MEMORY;
|
|
|
|
strcpy( env, "WINELOADER=" );
|
|
|
|
strcat( env, loader_env );
|
|
|
|
if (is_child_64bit)
|
|
|
|
{
|
|
|
|
strcat( env, "64" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len += sizeof("WINELOADER=") - 1;
|
|
|
|
if (!strcmp( env + len - 2, "64" )) env[len - 2] = 0;
|
|
|
|
}
|
|
|
|
loader = env;
|
|
|
|
putenv( env );
|
|
|
|
}
|
|
|
|
else loader = is_child_64bit ? "wine64" : "wine";
|
|
|
|
}
|
|
|
|
|
|
|
|
signal( SIGPIPE, SIG_DFL );
|
|
|
|
|
|
|
|
sprintf( socket_env, "WINESERVERSOCKET=%u", socketfd );
|
|
|
|
sprintf( preloader_reserve, "WINEPRELOADRESERVE=%x%08x-%x%08x",
|
|
|
|
(ULONG)(res_start >> 32), (ULONG)res_start, (ULONG)(res_end >> 32), (ULONG)res_end );
|
|
|
|
|
|
|
|
putenv( preloader_reserve );
|
|
|
|
putenv( socket_env );
|
|
|
|
|
2021-03-19 22:01:42 +01:00
|
|
|
return loader_exec( loader, argv, machine );
|
2020-05-19 14:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-19 17:37:08 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* exec_wineserver
|
|
|
|
*
|
|
|
|
* Exec a new wine server.
|
|
|
|
*/
|
|
|
|
static void exec_wineserver( char **argv )
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
if (build_dir)
|
|
|
|
{
|
|
|
|
if (!is_win64) /* look for 64-bit server */
|
|
|
|
{
|
|
|
|
char *loader = realpath_dirname( build_path( build_dir, "loader/wine64" ));
|
|
|
|
if (loader)
|
|
|
|
{
|
|
|
|
argv[0] = build_path( loader, "../server/wineserver" );
|
|
|
|
execv( argv[0], argv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv[0] = build_path( build_dir, "server/wineserver" );
|
|
|
|
execv( argv[0], argv );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
argv[0] = build_path( bin_dir, "wineserver" );
|
|
|
|
execv( argv[0], argv );
|
|
|
|
|
|
|
|
argv[0] = getenv( "WINESERVER" );
|
|
|
|
if (argv[0]) execv( argv[0], argv );
|
|
|
|
|
|
|
|
if ((path = getenv( "PATH" )))
|
|
|
|
{
|
|
|
|
for (path = strtok( strdup( path ), ":" ); path; path = strtok( NULL, ":" ))
|
|
|
|
{
|
|
|
|
argv[0] = build_path( path, "wineserver" );
|
|
|
|
execvp( argv[0], argv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argv[0] = build_path( BINDIR, "wineserver" );
|
|
|
|
execv( argv[0], argv );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* start_server
|
|
|
|
*
|
|
|
|
* Start a new wine server.
|
|
|
|
*/
|
2020-05-29 14:17:55 +02:00
|
|
|
void start_server( BOOL debug )
|
2020-05-19 17:37:08 +02:00
|
|
|
{
|
|
|
|
static BOOL started; /* we only try once */
|
|
|
|
char *argv[3];
|
|
|
|
static char debug_flag[] = "-d";
|
|
|
|
|
|
|
|
if (!started)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
int pid = fork();
|
|
|
|
if (pid == -1) fatal_error( "fork: %s", strerror(errno) );
|
|
|
|
if (!pid)
|
|
|
|
{
|
|
|
|
argv[1] = debug ? debug_flag : NULL;
|
|
|
|
argv[2] = NULL;
|
|
|
|
exec_wineserver( argv );
|
|
|
|
fatal_error( "could not exec wineserver\n" );
|
|
|
|
}
|
|
|
|
waitpid( pid, &status, 0 );
|
|
|
|
status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
|
|
|
|
if (status == 2) return; /* server lock held by someone else, will retry later */
|
|
|
|
if (status) exit(status); /* server failed */
|
|
|
|
started = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-14 15:17:06 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* map_so_dll
|
|
|
|
*
|
|
|
|
* Map a builtin dll in memory and fixup RVAs.
|
|
|
|
*/
|
2020-06-23 19:39:12 +02:00
|
|
|
static NTSTATUS map_so_dll( const IMAGE_NT_HEADERS *nt_descr, HMODULE module )
|
2020-05-14 15:17:06 +02:00
|
|
|
{
|
|
|
|
static const char builtin_signature[32] = "Wine builtin DLL";
|
|
|
|
IMAGE_DATA_DIRECTORY *dir;
|
|
|
|
IMAGE_DOS_HEADER *dos;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
IMAGE_SECTION_HEADER *sec;
|
|
|
|
BYTE *addr = (BYTE *)module;
|
|
|
|
DWORD code_start, code_end, data_start, data_end, align_mask;
|
|
|
|
int delta, nb_sections = 2; /* code + data */
|
|
|
|
unsigned int i;
|
|
|
|
DWORD size = (sizeof(IMAGE_DOS_HEADER)
|
|
|
|
+ sizeof(builtin_signature)
|
|
|
|
+ sizeof(IMAGE_NT_HEADERS)
|
|
|
|
+ nb_sections * sizeof(IMAGE_SECTION_HEADER));
|
|
|
|
|
2020-09-03 10:51:09 +02:00
|
|
|
if (anon_mmap_fixed( addr, size, PROT_READ | PROT_WRITE, 0 ) != addr) return STATUS_NO_MEMORY;
|
2020-05-14 15:17:06 +02:00
|
|
|
|
|
|
|
dos = (IMAGE_DOS_HEADER *)addr;
|
|
|
|
nt = (IMAGE_NT_HEADERS *)((BYTE *)(dos + 1) + sizeof(builtin_signature));
|
|
|
|
sec = (IMAGE_SECTION_HEADER *)(nt + 1);
|
|
|
|
|
|
|
|
/* build the DOS and NT headers */
|
|
|
|
|
|
|
|
dos->e_magic = IMAGE_DOS_SIGNATURE;
|
|
|
|
dos->e_cblp = 0x90;
|
|
|
|
dos->e_cp = 3;
|
|
|
|
dos->e_cparhdr = (sizeof(*dos) + 0xf) / 0x10;
|
|
|
|
dos->e_minalloc = 0;
|
|
|
|
dos->e_maxalloc = 0xffff;
|
|
|
|
dos->e_ss = 0x0000;
|
|
|
|
dos->e_sp = 0x00b8;
|
|
|
|
dos->e_lfanew = sizeof(*dos) + sizeof(builtin_signature);
|
2021-03-16 20:12:46 +01:00
|
|
|
memcpy( dos + 1, builtin_signature, sizeof(builtin_signature) );
|
2020-05-14 15:17:06 +02:00
|
|
|
|
|
|
|
*nt = *nt_descr;
|
|
|
|
|
|
|
|
delta = (const BYTE *)nt_descr - addr;
|
|
|
|
align_mask = nt->OptionalHeader.SectionAlignment - 1;
|
|
|
|
code_start = (size + align_mask) & ~align_mask;
|
|
|
|
data_start = delta & ~align_mask;
|
|
|
|
#ifdef __APPLE__
|
|
|
|
{
|
|
|
|
Dl_info dli;
|
|
|
|
unsigned long data_size;
|
|
|
|
/* need the mach_header, not the PE header, to give to getsegmentdata(3) */
|
|
|
|
dladdr(addr, &dli);
|
|
|
|
code_end = getsegmentdata(dli.dli_fbase, "__DATA", &data_size) - addr;
|
|
|
|
data_end = (code_end + data_size + align_mask) & ~align_mask;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
code_end = data_start;
|
|
|
|
data_end = (nt->OptionalHeader.SizeOfImage + delta + align_mask) & ~align_mask;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
|
|
|
|
|
|
|
|
nt->FileHeader.NumberOfSections = nb_sections;
|
|
|
|
nt->OptionalHeader.BaseOfCode = code_start;
|
|
|
|
#ifndef _WIN64
|
|
|
|
nt->OptionalHeader.BaseOfData = data_start;
|
|
|
|
#endif
|
|
|
|
nt->OptionalHeader.SizeOfCode = code_end - code_start;
|
|
|
|
nt->OptionalHeader.SizeOfInitializedData = data_end - data_start;
|
|
|
|
nt->OptionalHeader.SizeOfUninitializedData = 0;
|
|
|
|
nt->OptionalHeader.SizeOfImage = data_end;
|
|
|
|
nt->OptionalHeader.ImageBase = (ULONG_PTR)addr;
|
|
|
|
|
|
|
|
/* build the code section */
|
|
|
|
|
|
|
|
memcpy( sec->Name, ".text", sizeof(".text") );
|
|
|
|
sec->SizeOfRawData = code_end - code_start;
|
|
|
|
sec->Misc.VirtualSize = sec->SizeOfRawData;
|
|
|
|
sec->VirtualAddress = code_start;
|
|
|
|
sec->PointerToRawData = code_start;
|
|
|
|
sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
|
|
|
|
sec++;
|
|
|
|
|
|
|
|
/* build the data section */
|
|
|
|
|
|
|
|
memcpy( sec->Name, ".data", sizeof(".data") );
|
|
|
|
sec->SizeOfRawData = data_end - data_start;
|
|
|
|
sec->Misc.VirtualSize = sec->SizeOfRawData;
|
|
|
|
sec->VirtualAddress = data_start;
|
|
|
|
sec->PointerToRawData = data_start;
|
|
|
|
sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
|
|
|
|
IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
|
|
|
|
sec++;
|
|
|
|
|
|
|
|
for (i = 0; i < nt->OptionalHeader.NumberOfRvaAndSizes; i++)
|
|
|
|
fixup_rva_dwords( &nt->OptionalHeader.DataDirectory[i].VirtualAddress, delta, 1 );
|
|
|
|
|
|
|
|
/* build the import directory */
|
|
|
|
|
|
|
|
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
|
|
|
|
if (dir->Size)
|
|
|
|
{
|
|
|
|
IMAGE_IMPORT_DESCRIPTOR *imports = (IMAGE_IMPORT_DESCRIPTOR *)(addr + dir->VirtualAddress);
|
|
|
|
|
|
|
|
while (imports->Name)
|
|
|
|
{
|
|
|
|
fixup_rva_dwords( &imports->u.OriginalFirstThunk, delta, 1 );
|
|
|
|
fixup_rva_dwords( &imports->Name, delta, 1 );
|
|
|
|
fixup_rva_dwords( &imports->FirstThunk, delta, 1 );
|
|
|
|
if (imports->u.OriginalFirstThunk)
|
|
|
|
fixup_rva_names( (UINT_PTR *)(addr + imports->u.OriginalFirstThunk), delta );
|
|
|
|
if (imports->FirstThunk)
|
|
|
|
fixup_rva_names( (UINT_PTR *)(addr + imports->FirstThunk), delta );
|
|
|
|
imports++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the resource directory */
|
|
|
|
|
|
|
|
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
|
|
|
|
if (dir->Size)
|
|
|
|
{
|
|
|
|
void *ptr = addr + dir->VirtualAddress;
|
|
|
|
fixup_so_resources( ptr, ptr, delta );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the export directory */
|
|
|
|
|
|
|
|
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
|
|
|
|
if (dir->Size)
|
|
|
|
{
|
|
|
|
IMAGE_EXPORT_DIRECTORY *exports = (IMAGE_EXPORT_DIRECTORY *)(addr + dir->VirtualAddress);
|
|
|
|
|
|
|
|
fixup_rva_dwords( &exports->Name, delta, 1 );
|
|
|
|
fixup_rva_dwords( &exports->AddressOfFunctions, delta, 1 );
|
|
|
|
fixup_rva_dwords( &exports->AddressOfNames, delta, 1 );
|
|
|
|
fixup_rva_dwords( &exports->AddressOfNameOrdinals, delta, 1 );
|
|
|
|
fixup_rva_dwords( (DWORD *)(addr + exports->AddressOfNames), delta, exports->NumberOfNames );
|
|
|
|
fixup_rva_ptrs( addr + exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG_PTR find_ordinal_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports, DWORD ordinal )
|
|
|
|
{
|
|
|
|
const DWORD *functions = (const DWORD *)((BYTE *)module + exports->AddressOfFunctions);
|
|
|
|
|
|
|
|
if (ordinal >= exports->NumberOfFunctions) return 0;
|
|
|
|
if (!functions[ordinal]) return 0;
|
|
|
|
return (ULONG_PTR)module + functions[ordinal];
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG_PTR find_named_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
|
2020-06-17 11:57:32 +02:00
|
|
|
const char *name )
|
2020-05-14 15:17:06 +02:00
|
|
|
{
|
|
|
|
const WORD *ordinals = (const WORD *)((BYTE *)module + exports->AddressOfNameOrdinals);
|
|
|
|
const DWORD *names = (const DWORD *)((BYTE *)module + exports->AddressOfNames);
|
|
|
|
int min = 0, max = exports->NumberOfNames - 1;
|
|
|
|
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
int res, pos = (min + max) / 2;
|
|
|
|
char *ename = (char *)module + names[pos];
|
2020-06-17 11:57:32 +02:00
|
|
|
if (!(res = strcmp( ename, name ))) return find_ordinal_export( module, exports, ordinals[pos] );
|
2020-05-14 15:17:06 +02:00
|
|
|
if (res > 0) max = pos - 1;
|
|
|
|
else min = pos + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-21 13:41:28 +02:00
|
|
|
static ULONG_PTR find_pe_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
|
|
|
|
const IMAGE_IMPORT_BY_NAME *name )
|
|
|
|
{
|
|
|
|
const WORD *ordinals = (const WORD *)((BYTE *)module + exports->AddressOfNameOrdinals);
|
|
|
|
const DWORD *names = (const DWORD *)((BYTE *)module + exports->AddressOfNames);
|
|
|
|
|
|
|
|
if (name->Hint < exports->NumberOfNames)
|
|
|
|
{
|
|
|
|
char *ename = (char *)module + names[name->Hint];
|
|
|
|
if (!strcmp( ename, (char *)name->Name ))
|
|
|
|
return find_ordinal_export( module, exports, ordinals[name->Hint] );
|
|
|
|
}
|
|
|
|
return find_named_export( module, exports, (char *)name->Name );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *get_rva( void *module, ULONG_PTR addr )
|
|
|
|
{
|
|
|
|
return (BYTE *)module + addr;
|
|
|
|
}
|
|
|
|
|
2021-07-07 11:39:24 +02:00
|
|
|
static const void *get_module_data_dir( HMODULE module, ULONG dir, ULONG *size )
|
|
|
|
{
|
|
|
|
const IMAGE_NT_HEADERS *nt = get_rva( module, ((IMAGE_DOS_HEADER *)module)->e_lfanew );
|
|
|
|
const IMAGE_DATA_DIRECTORY *data;
|
|
|
|
|
|
|
|
if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
|
|
|
|
data = &((const IMAGE_NT_HEADERS64 *)nt)->OptionalHeader.DataDirectory[dir];
|
|
|
|
else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
|
|
|
|
data = &((const IMAGE_NT_HEADERS32 *)nt)->OptionalHeader.DataDirectory[dir];
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
if (!data->VirtualAddress || !data->Size) return NULL;
|
|
|
|
if (size) *size = data->Size;
|
|
|
|
return get_rva( module, data->VirtualAddress );
|
|
|
|
}
|
|
|
|
|
2020-09-21 13:41:28 +02:00
|
|
|
static NTSTATUS fixup_ntdll_imports( const char *name, HMODULE module )
|
|
|
|
{
|
|
|
|
const IMAGE_IMPORT_DESCRIPTOR *descr;
|
|
|
|
const IMAGE_THUNK_DATA *import_list;
|
|
|
|
IMAGE_THUNK_DATA *thunk_list;
|
|
|
|
|
2021-07-07 11:39:24 +02:00
|
|
|
if (!(descr = get_module_data_dir( module, IMAGE_FILE_IMPORT_DIRECTORY, NULL ))) return STATUS_SUCCESS;
|
2020-09-21 13:41:28 +02:00
|
|
|
for (; descr->Name && descr->FirstThunk; descr++)
|
|
|
|
{
|
|
|
|
thunk_list = get_rva( module, descr->FirstThunk );
|
|
|
|
|
|
|
|
/* ntdll must be the only import */
|
|
|
|
if (strcmp( get_rva( module, descr->Name ), "ntdll.dll" ))
|
|
|
|
{
|
|
|
|
ERR( "module %s is importing %s\n", debugstr_a(name), (char *)get_rva( module, descr->Name ));
|
|
|
|
return STATUS_PROCEDURE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
if (descr->u.OriginalFirstThunk)
|
|
|
|
import_list = get_rva( module, descr->u.OriginalFirstThunk );
|
|
|
|
else
|
|
|
|
import_list = thunk_list;
|
|
|
|
|
|
|
|
while (import_list->u1.Ordinal)
|
|
|
|
{
|
|
|
|
if (IMAGE_SNAP_BY_ORDINAL( import_list->u1.Ordinal ))
|
|
|
|
{
|
|
|
|
int ordinal = IMAGE_ORDINAL( import_list->u1.Ordinal ) - ntdll_exports->Base;
|
|
|
|
thunk_list->u1.Function = find_ordinal_export( ntdll_module, ntdll_exports, ordinal );
|
|
|
|
if (!thunk_list->u1.Function) ERR( "%s: ntdll.%u not found\n", debugstr_a(name), ordinal );
|
|
|
|
}
|
|
|
|
else /* import by name */
|
|
|
|
{
|
|
|
|
IMAGE_IMPORT_BY_NAME *pe_name = get_rva( module, import_list->u1.AddressOfData );
|
|
|
|
thunk_list->u1.Function = find_pe_export( ntdll_module, ntdll_exports, pe_name );
|
|
|
|
if (!thunk_list->u1.Function) ERR( "%s: ntdll.%s not found\n", debugstr_a(name), pe_name->Name );
|
|
|
|
}
|
|
|
|
import_list++;
|
|
|
|
thunk_list++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-08-24 13:25:26 +02:00
|
|
|
static void load_ntdll_functions( HMODULE module )
|
2020-05-14 15:17:06 +02:00
|
|
|
{
|
2020-07-07 10:47:02 +02:00
|
|
|
void **ptr;
|
2020-05-14 15:17:06 +02:00
|
|
|
|
2021-07-07 11:39:24 +02:00
|
|
|
ntdll_exports = get_module_data_dir( module, IMAGE_FILE_EXPORT_DIRECTORY, NULL );
|
2020-05-14 15:17:06 +02:00
|
|
|
assert( ntdll_exports );
|
|
|
|
|
2020-06-17 11:57:32 +02:00
|
|
|
#define GET_FUNC(name) \
|
2020-08-24 13:25:26 +02:00
|
|
|
if (!(p##name = (void *)find_named_export( module, ntdll_exports, #name ))) \
|
2020-06-17 11:57:32 +02:00
|
|
|
ERR( "%s not found\n", #name )
|
|
|
|
|
|
|
|
GET_FUNC( DbgUiRemoteBreakin );
|
2020-07-21 15:50:03 +02:00
|
|
|
GET_FUNC( KiRaiseUserExceptionDispatcher );
|
2020-06-17 11:57:32 +02:00
|
|
|
GET_FUNC( KiUserExceptionDispatcher );
|
2020-08-06 19:31:24 +02:00
|
|
|
GET_FUNC( KiUserApcDispatcher );
|
2020-06-17 11:57:32 +02:00
|
|
|
GET_FUNC( LdrInitializeThunk );
|
2021-07-07 11:39:57 +02:00
|
|
|
GET_FUNC( LdrSystemDllInitBlock );
|
2020-06-17 11:57:32 +02:00
|
|
|
GET_FUNC( RtlUserThreadStart );
|
2021-06-09 15:19:43 +02:00
|
|
|
GET_FUNC( __wine_ctrl_routine );
|
2020-06-17 11:57:32 +02:00
|
|
|
GET_FUNC( __wine_set_unix_funcs );
|
2020-07-07 10:47:02 +02:00
|
|
|
#undef GET_FUNC
|
|
|
|
#define SET_PTR(name,val) \
|
2020-08-24 13:25:26 +02:00
|
|
|
if ((ptr = (void *)find_named_export( module, ntdll_exports, #name ))) *ptr = val; \
|
2020-07-07 10:47:02 +02:00
|
|
|
else ERR( "%s not found\n", #name )
|
|
|
|
|
2021-06-02 12:54:34 +02:00
|
|
|
SET_PTR( __wine_syscall_dispatcher, __wine_syscall_dispatcher );
|
2020-06-29 12:10:00 +02:00
|
|
|
#ifdef __i386__
|
2020-07-07 10:47:02 +02:00
|
|
|
SET_PTR( __wine_ldt_copy, &__wine_ldt_copy );
|
2020-06-29 12:10:00 +02:00
|
|
|
#endif
|
2020-07-07 10:47:02 +02:00
|
|
|
#undef SET_PTR
|
2020-05-14 15:17:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-07 11:39:57 +02:00
|
|
|
static void load_ntdll_wow64_functions( HMODULE module )
|
|
|
|
{
|
|
|
|
const IMAGE_EXPORT_DIRECTORY *exports;
|
|
|
|
|
|
|
|
exports = get_module_data_dir( module, IMAGE_FILE_EXPORT_DIRECTORY, NULL );
|
|
|
|
assert( exports );
|
|
|
|
|
|
|
|
pLdrSystemDllInitBlock->ntdll_handle = (ULONG_PTR)module;
|
|
|
|
|
|
|
|
#define GET_FUNC(name) pLdrSystemDllInitBlock->p##name = find_named_export( module, exports, #name )
|
|
|
|
GET_FUNC( KiUserApcDispatcher );
|
|
|
|
GET_FUNC( KiUserCallbackDispatcher );
|
|
|
|
GET_FUNC( KiUserExceptionDispatcher );
|
|
|
|
GET_FUNC( LdrInitializeThunk );
|
|
|
|
GET_FUNC( LdrSystemDllInitBlock );
|
|
|
|
GET_FUNC( RtlUserThreadStart );
|
|
|
|
GET_FUNC( RtlpFreezeTimeBias );
|
|
|
|
GET_FUNC( RtlpQueryProcessDebugInformationRemote );
|
|
|
|
#undef GET_FUNC
|
|
|
|
|
|
|
|
/* also set the 32-bit LdrSystemDllInitBlock */
|
|
|
|
memcpy( (void *)(ULONG_PTR)pLdrSystemDllInitBlock->pLdrSystemDllInitBlock,
|
|
|
|
pLdrSystemDllInitBlock, sizeof(*pLdrSystemDllInitBlock) );
|
|
|
|
}
|
|
|
|
|
2021-02-25 12:37:37 +01:00
|
|
|
/* reimplementation of LdrProcessRelocationBlock */
|
2021-07-07 11:39:24 +02:00
|
|
|
static const IMAGE_BASE_RELOCATION *process_relocation_block( void *module, const IMAGE_BASE_RELOCATION *rel,
|
|
|
|
INT_PTR delta )
|
2021-02-25 12:37:37 +01:00
|
|
|
{
|
|
|
|
char *page = get_rva( module, rel->VirtualAddress );
|
|
|
|
UINT count = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
|
|
|
|
USHORT *relocs = (USHORT *)(rel + 1);
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
USHORT offset = *relocs & 0xfff;
|
|
|
|
switch (*relocs >> 12)
|
|
|
|
{
|
|
|
|
case IMAGE_REL_BASED_ABSOLUTE:
|
|
|
|
break;
|
|
|
|
case IMAGE_REL_BASED_HIGH:
|
|
|
|
*(short *)(page + offset) += HIWORD(delta);
|
|
|
|
break;
|
|
|
|
case IMAGE_REL_BASED_LOW:
|
|
|
|
*(short *)(page + offset) += LOWORD(delta);
|
|
|
|
break;
|
|
|
|
case IMAGE_REL_BASED_HIGHLOW:
|
|
|
|
*(int *)(page + offset) += delta;
|
|
|
|
break;
|
|
|
|
case IMAGE_REL_BASED_DIR64:
|
2021-07-07 11:39:24 +02:00
|
|
|
*(INT64 *)(page + offset) += delta;
|
2021-02-25 12:37:37 +01:00
|
|
|
break;
|
|
|
|
case IMAGE_REL_BASED_THUMB_MOV32:
|
|
|
|
{
|
|
|
|
DWORD *inst = (DWORD *)(page + offset);
|
|
|
|
WORD lo = ((inst[0] << 1) & 0x0800) + ((inst[0] << 12) & 0xf000) +
|
|
|
|
((inst[0] >> 20) & 0x0700) + ((inst[0] >> 16) & 0x00ff);
|
|
|
|
WORD hi = ((inst[1] << 1) & 0x0800) + ((inst[1] << 12) & 0xf000) +
|
|
|
|
((inst[1] >> 20) & 0x0700) + ((inst[1] >> 16) & 0x00ff);
|
|
|
|
DWORD imm = MAKELONG( lo, hi ) + delta;
|
|
|
|
|
|
|
|
lo = LOWORD( imm );
|
|
|
|
hi = HIWORD( imm );
|
|
|
|
inst[0] = (inst[0] & 0x8f00fbf0) + ((lo >> 1) & 0x0400) + ((lo >> 12) & 0x000f) +
|
|
|
|
((lo << 20) & 0x70000000) + ((lo << 16) & 0xff0000);
|
|
|
|
inst[1] = (inst[1] & 0x8f00fbf0) + ((hi >> 1) & 0x0400) + ((hi >> 12) & 0x000f) +
|
|
|
|
((hi << 20) & 0x70000000) + ((hi << 16) & 0xff0000);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
FIXME("Unknown/unsupported relocation %x\n", *relocs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
relocs++;
|
|
|
|
}
|
|
|
|
return (IMAGE_BASE_RELOCATION *)relocs; /* return address of next block */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void relocate_ntdll( void *module )
|
|
|
|
{
|
2021-07-07 11:39:24 +02:00
|
|
|
const IMAGE_NT_HEADERS *nt = get_rva( module, ((IMAGE_DOS_HEADER *)module)->e_lfanew );
|
|
|
|
const IMAGE_BASE_RELOCATION *rel, *end;
|
|
|
|
const IMAGE_SECTION_HEADER *sec;
|
|
|
|
ULONG protect_old[96], i, size;
|
2021-02-25 12:37:37 +01:00
|
|
|
INT_PTR delta;
|
|
|
|
|
2021-07-07 11:39:24 +02:00
|
|
|
ERR( "ntdll could not be mapped at preferred address (%p), expect trouble\n", module );
|
2021-02-25 12:37:37 +01:00
|
|
|
|
2021-07-07 11:39:24 +02:00
|
|
|
if (!(rel = get_module_data_dir( module, IMAGE_DIRECTORY_ENTRY_BASERELOC, &size ))) return;
|
2021-02-25 12:37:37 +01:00
|
|
|
|
|
|
|
sec = (IMAGE_SECTION_HEADER *)((char *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader);
|
|
|
|
for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
|
|
|
|
{
|
|
|
|
void *addr = get_rva( module, sec[i].VirtualAddress );
|
|
|
|
SIZE_T size = sec[i].SizeOfRawData;
|
|
|
|
NtProtectVirtualMemory( NtCurrentProcess(), &addr, &size, PAGE_READWRITE, &protect_old[i] );
|
|
|
|
}
|
|
|
|
|
2021-07-07 11:39:24 +02:00
|
|
|
end = (IMAGE_BASE_RELOCATION *)((const char *)rel + size);
|
2021-02-25 12:37:37 +01:00
|
|
|
delta = (char *)module - (char *)nt->OptionalHeader.ImageBase;
|
|
|
|
while (rel && rel < end - 1 && rel->SizeOfBlock) rel = process_relocation_block( module, rel, delta );
|
|
|
|
|
|
|
|
for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
|
|
|
|
{
|
|
|
|
void *addr = get_rva( module, sec[i].VirtualAddress );
|
|
|
|
SIZE_T size = sec[i].SizeOfRawData;
|
|
|
|
NtProtectVirtualMemory( NtCurrentProcess(), &addr, &size, protect_old[i], &protect_old[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:39:12 +02:00
|
|
|
|
|
|
|
static void *callback_module;
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* load_builtin_callback
|
|
|
|
*
|
|
|
|
* Load a library in memory; callback function for wine_dll_register
|
|
|
|
*/
|
|
|
|
static void load_builtin_callback( void *module, const char *filename )
|
|
|
|
{
|
|
|
|
callback_module = module;
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:16:52 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* load_libwine
|
|
|
|
*/
|
|
|
|
static void load_libwine(void)
|
|
|
|
{
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define LIBWINE "libwine.1.dylib"
|
|
|
|
#else
|
|
|
|
#define LIBWINE "libwine.so.1"
|
|
|
|
#endif
|
|
|
|
typedef void (*load_dll_callback_t)( void *, const char * );
|
2021-04-19 20:08:13 +02:00
|
|
|
void (*p_wine_dll_set_callback)( load_dll_callback_t load );
|
|
|
|
char ***p___wine_main_environ;
|
2020-09-03 12:16:52 +02:00
|
|
|
|
|
|
|
char *path;
|
|
|
|
void *handle;
|
|
|
|
|
|
|
|
if (build_dir) path = build_path( build_dir, "libs/wine/" LIBWINE );
|
2021-04-28 11:41:27 +02:00
|
|
|
else path = build_path( ntdll_dir, LIBWINE );
|
2020-09-03 12:16:52 +02:00
|
|
|
|
2020-10-04 18:10:24 +02:00
|
|
|
handle = dlopen( path, RTLD_NOW );
|
|
|
|
free( path );
|
|
|
|
if (!handle && !(handle = dlopen( LIBWINE, RTLD_NOW ))) return;
|
2020-09-03 12:16:52 +02:00
|
|
|
|
|
|
|
p_wine_dll_set_callback = dlsym( handle, "wine_dll_set_callback" );
|
|
|
|
p___wine_main_argc = dlsym( handle, "__wine_main_argc" );
|
|
|
|
p___wine_main_argv = dlsym( handle, "__wine_main_argv" );
|
|
|
|
p___wine_main_wargv = dlsym( handle, "__wine_main_wargv" );
|
|
|
|
p___wine_main_environ = dlsym( handle, "__wine_main_environ" );
|
|
|
|
|
|
|
|
if (p_wine_dll_set_callback) p_wine_dll_set_callback( load_builtin_callback );
|
|
|
|
if (p___wine_main_environ) *p___wine_main_environ = main_envp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-26 11:55:02 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* fill_builtin_image_info
|
|
|
|
*/
|
|
|
|
static void fill_builtin_image_info( void *module, pe_image_info_t *info )
|
|
|
|
{
|
|
|
|
const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER *)module;
|
|
|
|
const IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)((const BYTE *)dos + dos->e_lfanew);
|
|
|
|
|
|
|
|
info->base = nt->OptionalHeader.ImageBase;
|
|
|
|
info->entry_point = info->base + nt->OptionalHeader.AddressOfEntryPoint;
|
|
|
|
info->map_size = nt->OptionalHeader.SizeOfImage;
|
|
|
|
info->stack_size = nt->OptionalHeader.SizeOfStackReserve;
|
|
|
|
info->stack_commit = nt->OptionalHeader.SizeOfStackCommit;
|
|
|
|
info->zerobits = 0;
|
|
|
|
info->subsystem = nt->OptionalHeader.Subsystem;
|
|
|
|
info->subsystem_minor = nt->OptionalHeader.MinorSubsystemVersion;
|
|
|
|
info->subsystem_major = nt->OptionalHeader.MajorSubsystemVersion;
|
|
|
|
info->osversion_major = nt->OptionalHeader.MajorOperatingSystemVersion;
|
|
|
|
info->osversion_minor = nt->OptionalHeader.MinorOperatingSystemVersion;
|
|
|
|
info->image_charact = nt->FileHeader.Characteristics;
|
|
|
|
info->dll_charact = nt->OptionalHeader.DllCharacteristics;
|
|
|
|
info->machine = nt->FileHeader.Machine;
|
|
|
|
info->contains_code = TRUE;
|
|
|
|
info->image_flags = IMAGE_FLAGS_WineBuiltin;
|
|
|
|
info->loader_flags = 0;
|
|
|
|
info->header_size = nt->OptionalHeader.SizeOfHeaders;
|
|
|
|
info->file_size = nt->OptionalHeader.SizeOfImage;
|
|
|
|
info->checksum = nt->OptionalHeader.CheckSum;
|
2021-03-08 09:14:18 +01:00
|
|
|
info->dbg_offset = 0;
|
|
|
|
info->dbg_size = 0;
|
2021-01-26 11:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 19:39:12 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* dlopen_dll
|
|
|
|
*/
|
2021-02-18 11:11:22 +01:00
|
|
|
static NTSTATUS dlopen_dll( const char *so_name, UNICODE_STRING *nt_name, void **ret_module,
|
|
|
|
pe_image_info_t *image_info, BOOL prefer_native )
|
2020-06-23 19:39:12 +02:00
|
|
|
{
|
|
|
|
void *module, *handle;
|
|
|
|
const IMAGE_NT_HEADERS *nt;
|
|
|
|
|
|
|
|
callback_module = (void *)1;
|
|
|
|
handle = dlopen( so_name, RTLD_NOW );
|
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
WARN( "failed to load .so lib %s: %s\n", debugstr_a(so_name), dlerror() );
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
if (callback_module != (void *)1) /* callback was called */
|
|
|
|
{
|
|
|
|
if (!callback_module) return STATUS_NO_MEMORY;
|
|
|
|
WARN( "got old-style builtin library %s, constructors won't work\n", debugstr_a(so_name) );
|
|
|
|
module = callback_module;
|
2021-03-16 10:58:14 +01:00
|
|
|
if (get_builtin_so_handle( module )) goto already_loaded;
|
2020-06-23 19:39:12 +02:00
|
|
|
}
|
|
|
|
else if ((nt = dlsym( handle, "__wine_spec_nt_header" )))
|
|
|
|
{
|
|
|
|
module = (HMODULE)((nt->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
|
2021-03-16 10:58:14 +01:00
|
|
|
if (get_builtin_so_handle( module )) goto already_loaded;
|
2020-06-23 19:39:12 +02:00
|
|
|
if (map_so_dll( nt, module ))
|
|
|
|
{
|
|
|
|
dlclose( handle );
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* already loaded .so */
|
|
|
|
{
|
|
|
|
WARN( "%s already loaded?\n", debugstr_a(so_name));
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:55:02 +01:00
|
|
|
fill_builtin_image_info( module, image_info );
|
2021-02-18 11:11:22 +01:00
|
|
|
if (prefer_native && (image_info->dll_charact & IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE))
|
|
|
|
{
|
|
|
|
TRACE( "%s has prefer-native flag, ignoring builtin\n", debugstr_a(so_name) );
|
|
|
|
dlclose( handle );
|
|
|
|
return STATUS_IMAGE_ALREADY_LOADED;
|
|
|
|
}
|
2021-01-26 11:55:02 +01:00
|
|
|
|
2021-03-16 10:58:14 +01:00
|
|
|
if (virtual_create_builtin_view( module, nt_name, image_info, handle ))
|
2020-06-23 19:39:12 +02:00
|
|
|
{
|
|
|
|
dlclose( handle );
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
*ret_module = module;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
already_loaded:
|
2021-03-16 10:58:14 +01:00
|
|
|
fill_builtin_image_info( module, image_info );
|
|
|
|
*ret_module = module;
|
2020-06-23 19:39:12 +02:00
|
|
|
dlclose( handle );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-21 13:41:28 +02:00
|
|
|
/***********************************************************************
|
2021-03-22 16:03:22 +01:00
|
|
|
* init_unix_lib
|
2020-09-21 13:41:28 +02:00
|
|
|
*/
|
2021-03-22 16:03:22 +01:00
|
|
|
static NTSTATUS CDECL init_unix_lib( void *module, DWORD reason, const void *ptr_in, void *ptr_out )
|
2020-09-21 13:41:28 +02:00
|
|
|
{
|
2021-03-22 16:03:22 +01:00
|
|
|
NTSTATUS (CDECL *init_func)( HMODULE, DWORD, const void *, void * );
|
2020-09-21 13:41:28 +02:00
|
|
|
const IMAGE_NT_HEADERS *nt;
|
2021-03-22 16:03:22 +01:00
|
|
|
const char *name;
|
|
|
|
void *handle, *entry, *unix_module;
|
|
|
|
NTSTATUS status;
|
2020-09-21 13:41:28 +02:00
|
|
|
|
2021-03-22 16:03:22 +01:00
|
|
|
if ((status = get_builtin_unix_info( module, &name, &handle, &entry ))) return status;
|
2020-09-21 13:41:28 +02:00
|
|
|
|
2021-03-22 16:03:22 +01:00
|
|
|
if (!entry)
|
2020-09-21 13:41:28 +02:00
|
|
|
{
|
2021-03-22 16:03:22 +01:00
|
|
|
if (!name) return STATUS_DLL_NOT_FOUND;
|
|
|
|
if (!(handle = dlopen( name, RTLD_NOW ))) return STATUS_DLL_NOT_FOUND;
|
|
|
|
|
|
|
|
if (!(nt = dlsym( handle, "__wine_spec_nt_header" )) ||
|
|
|
|
!(entry = dlsym( handle, "__wine_init_unix_lib" )))
|
|
|
|
{
|
|
|
|
dlclose( handle );
|
|
|
|
set_builtin_unix_info( module, NULL, NULL, NULL );
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
TRACE( "loaded %s for %p\n", debugstr_a(name), module );
|
|
|
|
unix_module = (void *)((nt->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
|
2021-03-16 10:58:14 +01:00
|
|
|
map_so_dll( nt, unix_module );
|
|
|
|
fixup_ntdll_imports( name, unix_module );
|
2021-03-22 16:03:22 +01:00
|
|
|
set_builtin_unix_info( module, NULL, handle, entry );
|
2020-09-21 13:41:28 +02:00
|
|
|
}
|
2021-03-22 16:03:22 +01:00
|
|
|
init_func = entry;
|
|
|
|
return init_func( module, reason, ptr_in, ptr_out );
|
2020-09-21 13:41:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-28 19:03:09 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* __wine_unix_call
|
|
|
|
*/
|
|
|
|
NTSTATUS CDECL __wine_unix_call( UINT64 handle, unsigned int code, void *args )
|
|
|
|
{
|
|
|
|
return ((unixlib_entry_t*)(UINT_PTR)handle)[code]( args );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 19:39:12 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* load_so_dll
|
|
|
|
*/
|
|
|
|
static NTSTATUS CDECL load_so_dll( UNICODE_STRING *nt_name, void **module )
|
|
|
|
{
|
|
|
|
static const WCHAR soW[] = {'.','s','o',0};
|
2021-04-13 16:26:54 +02:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
2021-04-15 12:58:22 +02:00
|
|
|
UNICODE_STRING redir;
|
2021-01-26 11:55:02 +01:00
|
|
|
pe_image_info_t info;
|
2020-07-09 09:57:57 +02:00
|
|
|
char *unix_name;
|
2020-06-23 19:39:12 +02:00
|
|
|
NTSTATUS status;
|
|
|
|
DWORD len;
|
|
|
|
|
2021-03-26 16:15:01 +01:00
|
|
|
if (get_load_order( nt_name ) == LO_DISABLED) return STATUS_DLL_NOT_FOUND;
|
2021-04-13 16:26:54 +02:00
|
|
|
InitializeObjectAttributes( &attr, nt_name, OBJ_CASE_INSENSITIVE, 0, 0 );
|
2021-04-15 12:58:22 +02:00
|
|
|
get_redirect( &attr, &redir );
|
|
|
|
|
|
|
|
if (nt_to_unix_file_name( &attr, &unix_name, FILE_OPEN ))
|
|
|
|
{
|
|
|
|
free( redir.Buffer );
|
|
|
|
return STATUS_DLL_NOT_FOUND;
|
|
|
|
}
|
2020-06-23 19:39:12 +02:00
|
|
|
|
|
|
|
/* remove .so extension from Windows name */
|
|
|
|
len = nt_name->Length / sizeof(WCHAR);
|
|
|
|
if (len > 3 && !wcsicmp( nt_name->Buffer + len - 3, soW )) nt_name->Length -= 3 * sizeof(WCHAR);
|
|
|
|
|
2021-02-18 11:11:22 +01:00
|
|
|
status = dlopen_dll( unix_name, nt_name, module, &info, FALSE );
|
2020-07-14 10:32:31 +02:00
|
|
|
free( unix_name );
|
2021-04-15 12:58:22 +02:00
|
|
|
free( redir.Buffer );
|
2020-06-23 19:39:12 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 12:32:43 +02:00
|
|
|
/* check if the library is the correct architecture */
|
|
|
|
/* only returns false for a valid library of the wrong arch */
|
|
|
|
static int check_library_arch( int fd )
|
|
|
|
{
|
|
|
|
#ifdef __APPLE__
|
|
|
|
struct /* Mach-O header */
|
|
|
|
{
|
|
|
|
unsigned int magic;
|
|
|
|
unsigned int cputype;
|
|
|
|
} header;
|
|
|
|
|
|
|
|
if (read( fd, &header, sizeof(header) ) != sizeof(header)) return 1;
|
|
|
|
if (header.magic != 0xfeedface) return 1;
|
|
|
|
if (sizeof(void *) == sizeof(int)) return !(header.cputype >> 24);
|
|
|
|
else return (header.cputype >> 24) == 1; /* CPU_ARCH_ABI64 */
|
|
|
|
#else
|
|
|
|
struct /* ELF header */
|
|
|
|
{
|
|
|
|
unsigned char magic[4];
|
|
|
|
unsigned char class;
|
|
|
|
unsigned char data;
|
|
|
|
unsigned char version;
|
|
|
|
} header;
|
|
|
|
|
|
|
|
if (read( fd, &header, sizeof(header) ) != sizeof(header)) return 1;
|
|
|
|
if (memcmp( header.magic, "\177ELF", 4 )) return 1;
|
|
|
|
if (header.version != 1 /* EV_CURRENT */) return 1;
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
if (header.data != 2 /* ELFDATA2MSB */) return 1;
|
|
|
|
#else
|
|
|
|
if (header.data != 1 /* ELFDATA2LSB */) return 1;
|
|
|
|
#endif
|
|
|
|
if (sizeof(void *) == sizeof(int)) return header.class == 1; /* ELFCLASS32 */
|
|
|
|
else return header.class == 2; /* ELFCLASS64 */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *prepend( char *buffer, const char *str, size_t len )
|
|
|
|
{
|
|
|
|
return memcpy( buffer - len, str, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* open_dll_file
|
|
|
|
*
|
2021-04-27 14:34:36 +02:00
|
|
|
* Open a file for a new dll. Helper for open_builtin_pe_file.
|
2020-06-23 12:32:43 +02:00
|
|
|
*/
|
2021-03-24 10:53:53 +01:00
|
|
|
static NTSTATUS open_dll_file( const char *name, OBJECT_ATTRIBUTES *attr, HANDLE *mapping )
|
2020-06-23 12:32:43 +02:00
|
|
|
{
|
|
|
|
LARGE_INTEGER size;
|
|
|
|
NTSTATUS status;
|
2021-03-24 10:53:53 +01:00
|
|
|
HANDLE handle;
|
2020-06-23 12:32:43 +02:00
|
|
|
|
2021-02-09 13:23:17 +01:00
|
|
|
if ((status = open_unix_file( &handle, name, GENERIC_READ | SYNCHRONIZE, attr, 0,
|
2020-07-10 07:56:02 +02:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_DELETE, FILE_OPEN,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE, NULL, 0 )))
|
2020-06-23 12:32:43 +02:00
|
|
|
{
|
2020-07-10 07:56:02 +02:00
|
|
|
if (status != STATUS_OBJECT_PATH_NOT_FOUND && status != STATUS_OBJECT_NAME_NOT_FOUND)
|
2020-06-23 12:32:43 +02:00
|
|
|
{
|
|
|
|
/* if the file exists but failed to open, report the error */
|
2021-03-16 10:58:14 +01:00
|
|
|
struct stat st;
|
|
|
|
if (!stat( name, &st )) return status;
|
2020-06-23 12:32:43 +02:00
|
|
|
}
|
|
|
|
/* otherwise continue searching */
|
|
|
|
return STATUS_DLL_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
size.QuadPart = 0;
|
2021-03-24 10:53:53 +01:00
|
|
|
status = NtCreateSection( mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY |
|
2020-06-23 12:32:43 +02:00
|
|
|
SECTION_MAP_READ | SECTION_MAP_EXECUTE,
|
|
|
|
NULL, &size, PAGE_EXECUTE_READ, SEC_IMAGE, handle );
|
|
|
|
NtClose( handle );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2021-04-27 14:34:36 +02:00
|
|
|
* open_builtin_pe_file
|
2020-06-23 12:32:43 +02:00
|
|
|
*/
|
2021-04-27 14:34:36 +02:00
|
|
|
static NTSTATUS open_builtin_pe_file( const char *name, OBJECT_ATTRIBUTES *attr, void **module,
|
|
|
|
SIZE_T *size, SECTION_IMAGE_INFORMATION *image_info,
|
|
|
|
WORD machine, BOOL prefer_native )
|
2020-06-23 12:32:43 +02:00
|
|
|
{
|
|
|
|
NTSTATUS status;
|
2021-03-24 10:53:53 +01:00
|
|
|
HANDLE mapping;
|
2020-06-23 12:32:43 +02:00
|
|
|
|
2021-03-16 10:58:14 +01:00
|
|
|
*module = NULL;
|
2021-03-24 10:53:53 +01:00
|
|
|
status = open_dll_file( name, attr, &mapping );
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
status = virtual_map_builtin_module( mapping, module, size, image_info, machine, prefer_native );
|
|
|
|
NtClose( mapping );
|
|
|
|
}
|
2021-04-27 14:34:36 +02:00
|
|
|
return status;
|
|
|
|
}
|
2020-06-23 12:32:43 +02:00
|
|
|
|
|
|
|
|
2021-04-27 14:34:36 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* open_builtin_so_file
|
|
|
|
*/
|
|
|
|
static NTSTATUS open_builtin_so_file( const char *name, OBJECT_ATTRIBUTES *attr, void **module,
|
|
|
|
SECTION_IMAGE_INFORMATION *image_info, BOOL prefer_native )
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
*module = NULL;
|
|
|
|
if ((fd = open( name, O_RDONLY )) == -1) return STATUS_DLL_NOT_FOUND;
|
|
|
|
|
|
|
|
if (check_library_arch( fd ))
|
2020-06-23 12:32:43 +02:00
|
|
|
{
|
2021-04-27 14:34:36 +02:00
|
|
|
pe_image_info_t info;
|
2021-01-26 11:55:02 +01:00
|
|
|
|
2021-04-27 14:34:36 +02:00
|
|
|
status = dlopen_dll( name, attr->ObjectName, module, &info, prefer_native );
|
|
|
|
if (!status) virtual_fill_image_information( &info, image_info );
|
|
|
|
else if (status != STATUS_IMAGE_ALREADY_LOADED)
|
|
|
|
{
|
|
|
|
ERR( "failed to load .so lib %s\n", debugstr_a(name) );
|
|
|
|
status = STATUS_PROCEDURE_NOT_FOUND;
|
2020-06-23 12:32:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-27 14:34:36 +02:00
|
|
|
else status = STATUS_IMAGE_MACHINE_TYPE_MISMATCH;
|
|
|
|
|
|
|
|
close( fd );
|
2020-06-23 12:32:43 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 19:39:12 +02:00
|
|
|
/***********************************************************************
|
2021-03-22 09:56:48 +01:00
|
|
|
* find_builtin_dll
|
2020-06-23 19:39:12 +02:00
|
|
|
*/
|
2021-03-22 09:56:48 +01:00
|
|
|
static NTSTATUS find_builtin_dll( UNICODE_STRING *nt_name, void **module, SIZE_T *size_ptr,
|
2021-03-22 09:57:03 +01:00
|
|
|
SECTION_IMAGE_INFORMATION *image_info, WORD machine, BOOL prefer_native )
|
2020-06-23 19:39:12 +02:00
|
|
|
{
|
2021-02-09 13:23:17 +01:00
|
|
|
unsigned int i, pos, namepos, namelen, maxlen = 0;
|
|
|
|
unsigned int len = nt_name->Length / sizeof(WCHAR);
|
2020-09-21 13:41:28 +02:00
|
|
|
char *ptr = NULL, *file, *ext = NULL;
|
2021-04-26 12:43:10 +02:00
|
|
|
const char *pe_dir = get_pe_dir( machine );
|
2021-02-09 13:23:17 +01:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
2020-06-23 12:32:43 +02:00
|
|
|
NTSTATUS status = STATUS_DLL_NOT_FOUND;
|
|
|
|
BOOL found_image = FALSE;
|
|
|
|
|
2021-02-09 13:23:17 +01:00
|
|
|
for (i = namepos = 0; i < len; i++)
|
|
|
|
if (nt_name->Buffer[i] == '/' || nt_name->Buffer[i] == '\\') namepos = i + 1;
|
|
|
|
len -= namepos;
|
|
|
|
if (!len) return STATUS_DLL_NOT_FOUND;
|
|
|
|
InitializeObjectAttributes( &attr, nt_name, 0, 0, NULL );
|
|
|
|
|
2020-06-23 12:32:43 +02:00
|
|
|
if (build_dir) maxlen = strlen(build_dir) + sizeof("/programs/") + len;
|
2021-04-26 12:43:10 +02:00
|
|
|
maxlen = max( maxlen, dll_path_maxlen + 1 ) + len + sizeof("/aarch64-windows") + sizeof(".so");
|
2020-06-23 12:32:43 +02:00
|
|
|
|
|
|
|
if (!(file = malloc( maxlen ))) return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
pos = maxlen - len - sizeof(".so");
|
|
|
|
/* we don't want to depend on the current codepage here */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2021-02-09 13:23:17 +01:00
|
|
|
if (nt_name->Buffer[namepos + i] > 127) goto done;
|
|
|
|
file[pos + i] = (char)nt_name->Buffer[namepos + i];
|
2020-06-23 12:32:43 +02:00
|
|
|
if (file[pos + i] >= 'A' && file[pos + i] <= 'Z') file[pos + i] += 'a' - 'A';
|
2020-09-21 13:41:28 +02:00
|
|
|
else if (file[pos + i] == '.') ext = file + pos + i;
|
2020-06-23 12:32:43 +02:00
|
|
|
}
|
|
|
|
file[--pos] = '/';
|
|
|
|
|
|
|
|
if (build_dir)
|
|
|
|
{
|
|
|
|
/* try as a dll */
|
|
|
|
ptr = file + pos;
|
|
|
|
namelen = len + 1;
|
|
|
|
file[pos + len + 1] = 0;
|
2020-09-21 13:41:28 +02:00
|
|
|
if (ext && !strcmp( ext, ".dll" )) namelen -= 4;
|
2020-06-23 12:32:43 +02:00
|
|
|
ptr = prepend( ptr, ptr, namelen );
|
|
|
|
ptr = prepend( ptr, "/dlls", sizeof("/dlls") - 1 );
|
|
|
|
ptr = prepend( ptr, build_dir, strlen(build_dir) );
|
2021-04-27 14:34:36 +02:00
|
|
|
status = open_builtin_pe_file( ptr, &attr, module, size_ptr, image_info, machine, prefer_native );
|
|
|
|
if (status != STATUS_DLL_NOT_FOUND) goto done;
|
|
|
|
strcpy( file + pos + len + 1, ".so" );
|
|
|
|
status = open_builtin_so_file( ptr, &attr, module, image_info, prefer_native );
|
2020-06-23 12:32:43 +02:00
|
|
|
if (status != STATUS_DLL_NOT_FOUND) goto done;
|
|
|
|
|
|
|
|
/* now as a program */
|
|
|
|
ptr = file + pos;
|
|
|
|
namelen = len + 1;
|
|
|
|
file[pos + len + 1] = 0;
|
2020-09-21 13:41:28 +02:00
|
|
|
if (ext && !strcmp( ext, ".exe" )) namelen -= 4;
|
2020-06-23 12:32:43 +02:00
|
|
|
ptr = prepend( ptr, ptr, namelen );
|
|
|
|
ptr = prepend( ptr, "/programs", sizeof("/programs") - 1 );
|
|
|
|
ptr = prepend( ptr, build_dir, strlen(build_dir) );
|
2021-04-27 14:34:36 +02:00
|
|
|
status = open_builtin_pe_file( ptr, &attr, module, size_ptr, image_info, machine, prefer_native );
|
|
|
|
if (status != STATUS_DLL_NOT_FOUND) goto done;
|
|
|
|
strcpy( file + pos + len + 1, ".so" );
|
|
|
|
status = open_builtin_so_file( ptr, &attr, module, image_info, prefer_native );
|
2020-06-23 12:32:43 +02:00
|
|
|
if (status != STATUS_DLL_NOT_FOUND) goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; dll_paths[i]; i++)
|
|
|
|
{
|
2021-04-26 12:43:10 +02:00
|
|
|
ptr = file + pos;
|
|
|
|
file[pos + len + 1] = 0;
|
|
|
|
ptr = prepend( ptr, pe_dir, strlen(pe_dir) );
|
|
|
|
ptr = prepend( ptr, dll_paths[i], strlen(dll_paths[i]) );
|
2021-04-27 14:34:36 +02:00
|
|
|
status = open_builtin_pe_file( ptr, &attr, module, size_ptr, image_info, machine, prefer_native );
|
2021-04-26 12:43:10 +02:00
|
|
|
/* use so dir for unix lib */
|
2021-04-28 11:40:39 +02:00
|
|
|
ptr = file + pos;
|
|
|
|
ptr = prepend( ptr, so_dir, strlen(so_dir) );
|
|
|
|
ptr = prepend( ptr, dll_paths[i], strlen(dll_paths[i]) );
|
|
|
|
if (status != STATUS_DLL_NOT_FOUND) goto done;
|
|
|
|
strcpy( file + pos + len + 1, ".so" );
|
|
|
|
status = open_builtin_so_file( ptr, &attr, module, image_info, prefer_native );
|
|
|
|
if (status != STATUS_DLL_NOT_FOUND) goto done;
|
|
|
|
file[pos + len + 1] = 0;
|
2020-06-23 12:32:43 +02:00
|
|
|
ptr = prepend( file + pos, dll_paths[i], strlen(dll_paths[i]) );
|
2021-04-26 12:43:10 +02:00
|
|
|
if (status != STATUS_DLL_NOT_FOUND) goto done;
|
2021-04-27 14:34:36 +02:00
|
|
|
status = open_builtin_pe_file( ptr, &attr, module, size_ptr, image_info, machine, prefer_native );
|
|
|
|
if (status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH)
|
|
|
|
{
|
|
|
|
found_image = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (status != STATUS_DLL_NOT_FOUND) goto done;
|
|
|
|
strcpy( file + pos + len + 1, ".so" );
|
|
|
|
status = open_builtin_so_file( ptr, &attr, module, image_info, prefer_native );
|
2020-06-23 12:32:43 +02:00
|
|
|
if (status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH) found_image = TRUE;
|
|
|
|
else if (status != STATUS_DLL_NOT_FOUND) goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_image) status = STATUS_IMAGE_MACHINE_TYPE_MISMATCH;
|
2021-02-09 13:23:17 +01:00
|
|
|
WARN( "cannot find builtin library for %s\n", debugstr_us(nt_name) );
|
2020-06-23 12:32:43 +02:00
|
|
|
done:
|
2021-03-22 09:56:48 +01:00
|
|
|
if (status >= 0 && ext)
|
2020-09-21 13:41:28 +02:00
|
|
|
{
|
|
|
|
strcpy( ext, ".so" );
|
2021-03-22 16:03:22 +01:00
|
|
|
set_builtin_unix_info( *module, ptr, NULL, NULL );
|
2020-09-21 13:41:28 +02:00
|
|
|
}
|
2020-06-23 12:32:43 +02:00
|
|
|
free( file );
|
|
|
|
return status;
|
2020-06-23 19:39:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-22 09:56:57 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* load_builtin
|
|
|
|
*
|
|
|
|
* Load the builtin dll if specified by load order configuration.
|
|
|
|
* Return STATUS_IMAGE_ALREADY_LOADED if we should keep the native one that we have found.
|
|
|
|
*/
|
2021-03-30 10:06:44 +02:00
|
|
|
NTSTATUS load_builtin( const pe_image_info_t *image_info, WCHAR *filename,
|
2021-03-22 09:56:57 +01:00
|
|
|
void **module, SIZE_T *size )
|
|
|
|
{
|
2021-03-22 09:57:03 +01:00
|
|
|
WORD machine = image_info->machine; /* request same machine as the native one */
|
2021-03-22 09:56:57 +01:00
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING nt_name;
|
|
|
|
SECTION_IMAGE_INFORMATION info;
|
|
|
|
enum loadorder loadorder;
|
|
|
|
|
2021-03-30 10:06:44 +02:00
|
|
|
/* remove .fake extension if present */
|
|
|
|
if (image_info->image_flags & IMAGE_FLAGS_WineFakeDll)
|
|
|
|
{
|
|
|
|
static const WCHAR fakeW[] = {'.','f','a','k','e',0};
|
|
|
|
WCHAR *ext = wcsrchr( filename, '.' );
|
|
|
|
|
|
|
|
TRACE( "%s is a fake Wine dll\n", debugstr_w(filename) );
|
|
|
|
if (ext && !wcsicmp( ext, fakeW )) *ext = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:56:57 +01:00
|
|
|
init_unicode_string( &nt_name, filename );
|
2021-03-26 16:04:22 +01:00
|
|
|
loadorder = get_load_order( &nt_name );
|
2021-03-22 09:56:57 +01:00
|
|
|
|
2021-03-30 10:06:44 +02:00
|
|
|
if (loadorder == LO_DISABLED) return STATUS_DLL_NOT_FOUND;
|
|
|
|
|
2021-03-22 09:56:57 +01:00
|
|
|
if (image_info->image_flags & IMAGE_FLAGS_WineBuiltin)
|
|
|
|
{
|
2021-03-30 10:06:44 +02:00
|
|
|
if (loadorder == LO_NATIVE) return STATUS_DLL_NOT_FOUND;
|
|
|
|
loadorder = LO_BUILTIN_NATIVE; /* load builtin, then fallback to the file we found */
|
2021-03-22 09:56:57 +01:00
|
|
|
}
|
2021-03-30 10:06:44 +02:00
|
|
|
else if (image_info->image_flags & IMAGE_FLAGS_WineFakeDll)
|
2021-03-22 09:56:57 +01:00
|
|
|
{
|
2021-03-30 10:06:44 +02:00
|
|
|
if (loadorder == LO_NATIVE) return STATUS_DLL_NOT_FOUND;
|
|
|
|
loadorder = LO_BUILTIN; /* builtin with no fallback since mapping a fake dll is not useful */
|
2021-03-22 09:56:57 +01:00
|
|
|
}
|
2021-03-30 10:06:44 +02:00
|
|
|
|
2021-03-22 09:56:57 +01:00
|
|
|
switch (loadorder)
|
|
|
|
{
|
|
|
|
case LO_NATIVE:
|
|
|
|
case LO_NATIVE_BUILTIN:
|
|
|
|
return STATUS_IMAGE_ALREADY_LOADED;
|
|
|
|
case LO_BUILTIN:
|
2021-03-22 09:57:03 +01:00
|
|
|
return find_builtin_dll( &nt_name, module, size, &info, machine, FALSE );
|
2021-03-30 10:06:44 +02:00
|
|
|
default:
|
2021-03-22 09:57:03 +01:00
|
|
|
status = find_builtin_dll( &nt_name, module, size, &info, machine, (loadorder == LO_DEFAULT) );
|
2021-04-06 12:21:40 +02:00
|
|
|
if (status == STATUS_DLL_NOT_FOUND || status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH)
|
|
|
|
return STATUS_IMAGE_ALREADY_LOADED;
|
2021-03-22 09:56:57 +01:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-28 10:03:04 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* get_machine_wow64_dir
|
|
|
|
*
|
|
|
|
* cf. GetSystemWow64Directory2.
|
|
|
|
*/
|
2021-05-26 17:25:27 +02:00
|
|
|
static const WCHAR *get_machine_wow64_dir( WORD machine )
|
2021-04-28 10:03:04 +02:00
|
|
|
{
|
|
|
|
static const WCHAR system32[] = {'\\','?','?','\\','C',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m','3','2','\\',0};
|
|
|
|
static const WCHAR syswow64[] = {'\\','?','?','\\','C',':','\\','w','i','n','d','o','w','s','\\','s','y','s','w','o','w','6','4','\\',0};
|
|
|
|
static const WCHAR sysarm32[] = {'\\','?','?','\\','C',':','\\','w','i','n','d','o','w','s','\\','s','y','s','a','r','m','3','2','\\',0};
|
|
|
|
static const WCHAR sysx8664[] = {'\\','?','?','\\','C',':','\\','w','i','n','d','o','w','s','\\','s','y','s','x','8','6','6','4','\\',0};
|
|
|
|
static const WCHAR sysarm64[] = {'\\','?','?','\\','C',':','\\','w','i','n','d','o','w','s','\\','s','y','s','a','r','m','6','4','\\',0};
|
|
|
|
|
2021-05-03 11:02:21 +02:00
|
|
|
if (machine == native_machine) machine = IMAGE_FILE_MACHINE_TARGET_HOST;
|
2021-04-28 10:03:04 +02:00
|
|
|
|
|
|
|
switch (machine)
|
|
|
|
{
|
|
|
|
case IMAGE_FILE_MACHINE_TARGET_HOST: return system32;
|
|
|
|
case IMAGE_FILE_MACHINE_I386: return syswow64;
|
|
|
|
case IMAGE_FILE_MACHINE_ARMNT: return sysarm32;
|
|
|
|
case IMAGE_FILE_MACHINE_AMD64: return sysx8664;
|
|
|
|
case IMAGE_FILE_MACHINE_ARM64: return sysarm64;
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-26 11:34:36 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* is_builtin_path
|
2021-04-28 10:03:04 +02:00
|
|
|
*
|
|
|
|
* Check if path is inside a system directory, to support loading builtins
|
|
|
|
* when the corresponding file doesn't exist yet.
|
2021-03-26 11:34:36 +01:00
|
|
|
*/
|
|
|
|
BOOL is_builtin_path( const UNICODE_STRING *path, WORD *machine )
|
|
|
|
{
|
2021-04-28 10:03:04 +02:00
|
|
|
unsigned int i, len = path->Length / sizeof(WCHAR), dirlen;
|
|
|
|
const WCHAR *sysdir, *p = path->Buffer;
|
2021-04-08 12:22:25 +02:00
|
|
|
|
|
|
|
/* only fake builtin existence during prefix bootstrap */
|
|
|
|
if (!is_prefix_bootstrap) return FALSE;
|
2021-03-30 09:59:01 +02:00
|
|
|
|
2021-04-28 10:03:04 +02:00
|
|
|
for (i = 0; i < supported_machines_count; i++)
|
2021-03-26 11:34:36 +01:00
|
|
|
{
|
2021-04-28 10:03:04 +02:00
|
|
|
sysdir = get_machine_wow64_dir( supported_machines[i] );
|
|
|
|
dirlen = wcslen( sysdir );
|
|
|
|
if (len <= dirlen) continue;
|
|
|
|
if (wcsnicmp( p, sysdir, dirlen )) continue;
|
|
|
|
/* check for remaining path components */
|
|
|
|
for (p += dirlen, len -= dirlen; len; p++, len--) if (*p == '\\') return FALSE;
|
|
|
|
*machine = supported_machines[i];
|
|
|
|
return TRUE;
|
2021-03-26 11:34:36 +01:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-24 10:53:53 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* open_main_image
|
|
|
|
*/
|
2021-04-12 09:20:55 +02:00
|
|
|
static NTSTATUS open_main_image( WCHAR *image, void **module, SECTION_IMAGE_INFORMATION *info,
|
|
|
|
enum loadorder loadorder )
|
2021-03-24 10:53:53 +01:00
|
|
|
{
|
|
|
|
static const WCHAR soW[] = {'.','s','o',0};
|
|
|
|
UNICODE_STRING nt_name;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
pe_image_info_t pe_info;
|
|
|
|
SIZE_T size = 0;
|
|
|
|
char *unix_name;
|
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE mapping;
|
|
|
|
WCHAR *p;
|
|
|
|
|
2021-04-12 09:20:55 +02:00
|
|
|
if (loadorder == LO_DISABLED) NtTerminateProcess( GetCurrentProcess(), STATUS_DLL_NOT_FOUND );
|
|
|
|
|
2021-03-24 10:53:53 +01:00
|
|
|
init_unicode_string( &nt_name, image );
|
|
|
|
InitializeObjectAttributes( &attr, &nt_name, OBJ_CASE_INSENSITIVE, 0, NULL );
|
2021-04-15 12:58:22 +02:00
|
|
|
if (nt_to_unix_file_name( &attr, &unix_name, FILE_OPEN )) return STATUS_DLL_NOT_FOUND;
|
2021-03-24 10:53:53 +01:00
|
|
|
|
|
|
|
status = open_dll_file( unix_name, &attr, &mapping );
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
*module = NULL;
|
|
|
|
status = NtMapViewOfSection( mapping, NtCurrentProcess(), module, 0, 0, NULL, &size,
|
|
|
|
ViewShare, 0, PAGE_EXECUTE_READ );
|
|
|
|
if (!status) NtQuerySection( mapping, SectionImageInformation, info, sizeof(*info), NULL );
|
|
|
|
NtClose( mapping );
|
|
|
|
}
|
2021-04-12 09:20:55 +02:00
|
|
|
else if (status == STATUS_INVALID_IMAGE_NOT_MZ && loadorder != LO_NATIVE)
|
2021-03-24 10:53:53 +01:00
|
|
|
{
|
|
|
|
/* remove .so extension from Windows name */
|
|
|
|
p = image + wcslen(image);
|
|
|
|
if (p - image > 3 && !wcsicmp( p - 3, soW ))
|
|
|
|
{
|
|
|
|
p[-3] = 0;
|
|
|
|
nt_name.Length -= 3 * sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
status = dlopen_dll( unix_name, &nt_name, module, &pe_info, FALSE );
|
|
|
|
if (!status) virtual_fill_image_information( &pe_info, info );
|
|
|
|
}
|
|
|
|
free( unix_name );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* load_main_exe
|
|
|
|
*/
|
2021-03-30 09:56:32 +02:00
|
|
|
NTSTATUS load_main_exe( const WCHAR *dos_name, const char *unix_name, const WCHAR *curdir,
|
2021-04-07 10:46:34 +02:00
|
|
|
WCHAR **image, void **module )
|
2021-03-24 10:53:53 +01:00
|
|
|
{
|
2021-04-12 09:20:55 +02:00
|
|
|
enum loadorder loadorder = LO_INVALID;
|
2021-03-24 10:53:53 +01:00
|
|
|
UNICODE_STRING nt_name;
|
2021-03-30 09:56:32 +02:00
|
|
|
WCHAR *tmp = NULL;
|
|
|
|
BOOL contains_path;
|
2021-03-24 10:53:53 +01:00
|
|
|
NTSTATUS status;
|
|
|
|
SIZE_T size;
|
|
|
|
struct stat st;
|
2021-03-26 11:34:36 +01:00
|
|
|
WORD machine;
|
2021-03-24 10:53:53 +01:00
|
|
|
|
|
|
|
/* special case for Unix file name */
|
2021-03-25 11:24:35 +01:00
|
|
|
if (unix_name && unix_name[0] == '/' && !stat( unix_name, &st ))
|
2021-03-24 10:53:53 +01:00
|
|
|
{
|
2021-03-25 11:24:35 +01:00
|
|
|
if ((status = unix_to_nt_file_name( unix_name, image ))) goto failed;
|
2021-04-13 05:59:32 +02:00
|
|
|
init_unicode_string( &nt_name, *image );
|
2021-04-12 09:20:55 +02:00
|
|
|
loadorder = get_load_order( &nt_name );
|
|
|
|
status = open_main_image( *image, module, &main_image_info, loadorder );
|
2021-03-24 10:53:53 +01:00
|
|
|
if (status != STATUS_DLL_NOT_FOUND) return status;
|
|
|
|
free( *image );
|
|
|
|
}
|
|
|
|
|
2021-03-30 09:56:32 +02:00
|
|
|
if (!dos_name)
|
|
|
|
{
|
|
|
|
dos_name = tmp = malloc( (strlen(unix_name) + 1) * sizeof(WCHAR) );
|
|
|
|
ntdll_umbstowcs( unix_name, strlen(unix_name) + 1, tmp, strlen(unix_name) + 1 );
|
|
|
|
}
|
|
|
|
contains_path = (wcschr( dos_name, '/' ) ||
|
|
|
|
wcschr( dos_name, '\\' ) ||
|
|
|
|
(dos_name[0] && dos_name[1] == ':'));
|
|
|
|
|
|
|
|
if ((status = get_full_path( dos_name, curdir, image ))) goto failed;
|
|
|
|
free( tmp );
|
|
|
|
|
2021-04-12 09:20:55 +02:00
|
|
|
init_unicode_string( &nt_name, *image );
|
|
|
|
if (loadorder == LO_INVALID) loadorder = get_load_order( &nt_name );
|
|
|
|
|
|
|
|
status = open_main_image( *image, module, &main_image_info, loadorder );
|
2021-03-24 10:53:53 +01:00
|
|
|
if (status != STATUS_DLL_NOT_FOUND) return status;
|
|
|
|
|
|
|
|
/* if path is in system dir, we can load the builtin even if the file itself doesn't exist */
|
2021-04-12 09:20:55 +02:00
|
|
|
if (loadorder != LO_NATIVE && is_builtin_path( &nt_name, &machine ))
|
2021-03-24 10:53:53 +01:00
|
|
|
{
|
2021-04-07 10:46:34 +02:00
|
|
|
status = find_builtin_dll( &nt_name, module, &size, &main_image_info, machine, FALSE );
|
2021-03-24 10:53:53 +01:00
|
|
|
if (status != STATUS_DLL_NOT_FOUND) return status;
|
|
|
|
}
|
2021-03-30 09:56:32 +02:00
|
|
|
if (!contains_path) return STATUS_DLL_NOT_FOUND;
|
2021-03-24 10:53:53 +01:00
|
|
|
|
|
|
|
failed:
|
2021-03-30 09:56:32 +02:00
|
|
|
MESSAGE( "wine: failed to open %s: %x\n",
|
|
|
|
unix_name ? debugstr_a(unix_name) : debugstr_w(dos_name), status );
|
2021-03-24 10:53:53 +01:00
|
|
|
NtTerminateProcess( GetCurrentProcess(), status );
|
|
|
|
return status; /* unreached */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-25 11:29:37 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* load_start_exe
|
|
|
|
*
|
|
|
|
* Load start.exe as main image.
|
|
|
|
*/
|
2021-04-07 10:46:34 +02:00
|
|
|
NTSTATUS load_start_exe( WCHAR **image, void **module )
|
2021-03-25 11:29:37 +01:00
|
|
|
{
|
2021-04-28 10:03:04 +02:00
|
|
|
static const WCHAR startW[] = {'s','t','a','r','t','.','e','x','e',0};
|
2021-03-25 11:29:37 +01:00
|
|
|
UNICODE_STRING nt_name;
|
|
|
|
NTSTATUS status;
|
|
|
|
SIZE_T size;
|
|
|
|
|
2021-05-05 11:04:40 +02:00
|
|
|
*image = malloc( sizeof("\\??\\C:\\windows\\system32\\start.exe") * sizeof(WCHAR) );
|
|
|
|
wcscpy( *image, get_machine_wow64_dir( current_machine ));
|
|
|
|
wcscat( *image, startW );
|
|
|
|
init_unicode_string( &nt_name, *image );
|
2021-04-07 10:46:34 +02:00
|
|
|
status = find_builtin_dll( &nt_name, module, &size, &main_image_info, current_machine, FALSE );
|
2021-03-25 11:29:37 +01:00
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
MESSAGE( "wine: failed to load start.exe: %x\n", status );
|
|
|
|
NtTerminateProcess( GetCurrentProcess(), status );
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 19:39:12 +02:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
/* The PT_LOAD segments are sorted in increasing order, and the first
|
|
|
|
* starts at the beginning of the ELF file. By parsing the file, we can
|
|
|
|
* find that first PT_LOAD segment, from which we can find the base
|
|
|
|
* address it wanted, and knowing mapbase where the binary was actually
|
|
|
|
* loaded, use them to work out the relocbase offset. */
|
|
|
|
static BOOL get_relocbase(caddr_t mapbase, caddr_t *relocbase)
|
|
|
|
{
|
|
|
|
Elf_Half i;
|
|
|
|
#ifdef _WIN64
|
|
|
|
const Elf64_Ehdr *elf_header = (Elf64_Ehdr*) mapbase;
|
|
|
|
#else
|
|
|
|
const Elf32_Ehdr *elf_header = (Elf32_Ehdr*) mapbase;
|
|
|
|
#endif
|
|
|
|
const Elf_Phdr *prog_header = (const Elf_Phdr *)(mapbase + elf_header->e_phoff);
|
|
|
|
|
|
|
|
for (i = 0; i < elf_header->e_phnum; i++)
|
|
|
|
{
|
|
|
|
if (prog_header->p_type == PT_LOAD)
|
|
|
|
{
|
|
|
|
caddr_t desired_base = (caddr_t)((prog_header->p_vaddr / prog_header->p_align) * prog_header->p_align);
|
|
|
|
*relocbase = (caddr_t) (mapbase - desired_base);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
prog_header++;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* init_builtin_dll
|
|
|
|
*/
|
|
|
|
static void CDECL init_builtin_dll( void *module )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_DLINFO
|
2021-03-16 10:58:14 +01:00
|
|
|
void *handle = NULL;
|
2020-06-23 19:39:12 +02:00
|
|
|
struct link_map *map;
|
|
|
|
void (*init_func)(int, char **, char **) = NULL;
|
|
|
|
void (**init_array)(int, char **, char **) = NULL;
|
|
|
|
ULONG_PTR i, init_arraysz = 0;
|
|
|
|
#ifdef _WIN64
|
|
|
|
const Elf64_Dyn *dyn;
|
|
|
|
#else
|
|
|
|
const Elf32_Dyn *dyn;
|
|
|
|
#endif
|
|
|
|
|
2021-03-16 10:58:14 +01:00
|
|
|
if (!(handle = get_builtin_so_handle( module ))) return;
|
2021-03-22 16:11:51 +01:00
|
|
|
if (dlinfo( handle, RTLD_DI_LINKMAP, &map )) map = NULL;
|
|
|
|
release_builtin_module( module );
|
|
|
|
if (!map) return;
|
2020-06-23 19:39:12 +02:00
|
|
|
|
|
|
|
for (dyn = map->l_ld; dyn->d_tag; dyn++)
|
|
|
|
{
|
|
|
|
caddr_t relocbase = (caddr_t)map->l_addr;
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
/* On older FreeBSD versions, l_addr was the absolute load address, now it's the relocation offset. */
|
2020-11-30 19:08:01 +01:00
|
|
|
if (offsetof(struct link_map, l_addr) == 0)
|
2020-06-23 19:39:12 +02:00
|
|
|
if (!get_relocbase(map->l_addr, &relocbase)) return;
|
|
|
|
#endif
|
|
|
|
switch (dyn->d_tag)
|
|
|
|
{
|
|
|
|
case 0x60009990: init_array = (void *)(relocbase + dyn->d_un.d_val); break;
|
|
|
|
case 0x60009991: init_arraysz = dyn->d_un.d_val; break;
|
|
|
|
case 0x60009992: init_func = (void *)(relocbase + dyn->d_un.d_val); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE( "%p: got init_func %p init_array %p %lu\n", module, init_func, init_array, init_arraysz );
|
|
|
|
|
|
|
|
if (init_func) init_func( main_argc, main_argv, main_envp );
|
|
|
|
|
|
|
|
if (init_array)
|
|
|
|
for (i = 0; i < init_arraysz / sizeof(*init_array); i++)
|
|
|
|
init_array[i]( main_argc, main_argv, main_envp );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-14 15:26:32 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* load_ntdll
|
|
|
|
*/
|
2020-08-24 13:25:26 +02:00
|
|
|
static void load_ntdll(void)
|
2020-05-14 15:26:32 +02:00
|
|
|
{
|
2021-02-09 13:23:17 +01:00
|
|
|
static WCHAR path[] = {'\\','?','?','\\','C',':','\\','w','i','n','d','o','w','s','\\',
|
|
|
|
's','y','s','t','e','m','3','2','\\','n','t','d','l','l','.','d','l','l',0};
|
2021-04-27 14:34:36 +02:00
|
|
|
const char *pe_dir = get_pe_dir( current_machine );
|
2020-06-30 12:00:57 +02:00
|
|
|
NTSTATUS status;
|
2020-09-28 19:38:23 +02:00
|
|
|
SECTION_IMAGE_INFORMATION info;
|
2021-02-09 13:23:17 +01:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
UNICODE_STRING str;
|
2021-01-21 14:27:52 +01:00
|
|
|
void *module;
|
2021-03-22 09:56:48 +01:00
|
|
|
SIZE_T size = 0;
|
2021-04-26 12:43:10 +02:00
|
|
|
char *name;
|
|
|
|
|
2021-03-03 09:50:34 +01:00
|
|
|
init_unicode_string( &str, path );
|
2021-02-09 13:23:17 +01:00
|
|
|
InitializeObjectAttributes( &attr, &str, 0, 0, NULL );
|
2021-04-27 14:34:36 +02:00
|
|
|
|
2021-04-28 11:40:39 +02:00
|
|
|
name = malloc( strlen( ntdll_dir ) + strlen( pe_dir ) + sizeof("/ntdll.dll.so") );
|
|
|
|
if (build_dir) sprintf( name, "%s/ntdll.dll", ntdll_dir );
|
|
|
|
else sprintf( name, "%s%s/ntdll.dll", dll_dir, pe_dir );
|
2021-04-27 14:34:36 +02:00
|
|
|
status = open_builtin_pe_file( name, &attr, &module, &size, &info, current_machine, FALSE );
|
|
|
|
if (status == STATUS_DLL_NOT_FOUND)
|
2021-04-26 12:43:10 +02:00
|
|
|
{
|
2021-04-28 11:40:39 +02:00
|
|
|
sprintf( name, "%s/ntdll.dll.so", ntdll_dir );
|
2021-04-27 14:34:36 +02:00
|
|
|
status = open_builtin_so_file( name, &attr, &module, &info, FALSE );
|
2021-04-26 12:43:10 +02:00
|
|
|
}
|
2021-04-27 14:34:36 +02:00
|
|
|
if (status == STATUS_IMAGE_NOT_AT_BASE) relocate_ntdll( module );
|
2021-03-16 10:58:14 +01:00
|
|
|
else if (status) fatal_error( "failed to load %s error %x\n", name, status );
|
2020-05-14 15:26:32 +02:00
|
|
|
free( name );
|
2020-08-24 13:25:26 +02:00
|
|
|
load_ntdll_functions( module );
|
2020-09-21 13:41:28 +02:00
|
|
|
ntdll_module = module;
|
2020-05-14 15:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-07 11:39:41 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* load_wow64_ntdll
|
|
|
|
*/
|
|
|
|
static void load_wow64_ntdll( USHORT machine )
|
|
|
|
{
|
|
|
|
static const WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0};
|
|
|
|
SECTION_IMAGE_INFORMATION info;
|
|
|
|
UNICODE_STRING nt_name;
|
|
|
|
void *module;
|
|
|
|
NTSTATUS status;
|
|
|
|
SIZE_T size;
|
|
|
|
WCHAR *path = malloc( sizeof("\\??\\C:\\windows\\system32\\ntdll.dll") * sizeof(WCHAR) );
|
|
|
|
|
|
|
|
wcscpy( path, get_machine_wow64_dir( machine ));
|
|
|
|
wcscat( path, ntdllW );
|
|
|
|
init_unicode_string( &nt_name, path );
|
|
|
|
status = find_builtin_dll( &nt_name, &module, &size, &info, machine, FALSE );
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case STATUS_IMAGE_NOT_AT_BASE:
|
|
|
|
relocate_ntdll( module );
|
|
|
|
/* fall through */
|
|
|
|
case STATUS_SUCCESS:
|
2021-07-07 11:39:57 +02:00
|
|
|
load_ntdll_wow64_functions( module );
|
2021-07-07 11:39:41 +02:00
|
|
|
TRACE("loaded %s at %p\n", debugstr_w(path), module );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR( "failed to load %s error %x\n", debugstr_w(path), status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free( path );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-18 14:14:19 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* get_image_address
|
|
|
|
*/
|
2021-06-15 12:40:01 +02:00
|
|
|
static ULONG_PTR get_image_address(void)
|
2020-06-18 14:14:19 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_GETAUXVAL
|
|
|
|
ULONG_PTR size, num, phdr_addr = getauxval( AT_PHDR );
|
|
|
|
ElfW(Phdr) *phdr;
|
|
|
|
|
|
|
|
if (!phdr_addr) return 0;
|
|
|
|
phdr = (ElfW(Phdr) *)phdr_addr;
|
|
|
|
size = getauxval( AT_PHENT );
|
|
|
|
num = getauxval( AT_PHNUM );
|
|
|
|
while (num--)
|
|
|
|
{
|
|
|
|
if (phdr->p_type == PT_PHDR) return phdr_addr - phdr->p_offset;
|
|
|
|
phdr = (ElfW(Phdr) *)((char *)phdr + size);
|
|
|
|
}
|
|
|
|
#elif defined(__APPLE__) && defined(TASK_DYLD_INFO)
|
|
|
|
struct task_dyld_info dyld_info;
|
|
|
|
mach_msg_type_number_t size = TASK_DYLD_INFO_COUNT;
|
|
|
|
|
|
|
|
if (task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&dyld_info, &size) == KERN_SUCCESS)
|
|
|
|
return dyld_info.all_image_info_addr;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-26 09:12:11 +02:00
|
|
|
/* math function wrappers */
|
|
|
|
static double CDECL ntdll_atan( double d ) { return atan( d ); }
|
|
|
|
static double CDECL ntdll_ceil( double d ) { return ceil( d ); }
|
|
|
|
static double CDECL ntdll_cos( double d ) { return cos( d ); }
|
|
|
|
static double CDECL ntdll_fabs( double d ) { return fabs( d ); }
|
|
|
|
static double CDECL ntdll_floor( double d ) { return floor( d ); }
|
|
|
|
static double CDECL ntdll_log( double d ) { return log( d ); }
|
|
|
|
static double CDECL ntdll_pow( double x, double y ) { return pow( x, y ); }
|
|
|
|
static double CDECL ntdll_sin( double d ) { return sin( d ); }
|
|
|
|
static double CDECL ntdll_sqrt( double d ) { return sqrt( d ); }
|
|
|
|
static double CDECL ntdll_tan( double d ) { return tan( d ); }
|
|
|
|
|
|
|
|
|
2020-05-14 15:17:06 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* unix_funcs
|
|
|
|
*/
|
|
|
|
static struct unix_funcs unix_funcs =
|
|
|
|
{
|
2021-01-21 10:37:54 +01:00
|
|
|
#ifdef __aarch64__
|
2020-06-01 12:52:05 +02:00
|
|
|
NtCurrentTeb,
|
2021-01-21 10:37:54 +01:00
|
|
|
#endif
|
2020-06-05 16:54:17 +02:00
|
|
|
DbgUiIssueRemoteBreakin,
|
2020-06-25 18:07:38 +02:00
|
|
|
RtlGetSystemTimePrecise,
|
2020-06-09 12:39:49 +02:00
|
|
|
RtlWaitOnAddress,
|
|
|
|
RtlWakeAddressAll,
|
|
|
|
RtlWakeAddressSingle,
|
2020-06-09 13:05:58 +02:00
|
|
|
fast_RtlpWaitForCriticalSection,
|
|
|
|
fast_RtlpUnWaitCriticalSection,
|
|
|
|
fast_RtlDeleteCriticalSection,
|
2020-06-09 12:35:01 +02:00
|
|
|
fast_RtlTryAcquireSRWLockExclusive,
|
|
|
|
fast_RtlAcquireSRWLockExclusive,
|
|
|
|
fast_RtlTryAcquireSRWLockShared,
|
|
|
|
fast_RtlAcquireSRWLockShared,
|
|
|
|
fast_RtlReleaseSRWLockExclusive,
|
|
|
|
fast_RtlReleaseSRWLockShared,
|
2020-06-09 12:38:37 +02:00
|
|
|
fast_RtlWakeConditionVariable,
|
2020-08-20 22:38:42 +02:00
|
|
|
fast_wait_cv,
|
2020-06-26 09:12:11 +02:00
|
|
|
ntdll_atan,
|
|
|
|
ntdll_ceil,
|
|
|
|
ntdll_cos,
|
|
|
|
ntdll_fabs,
|
|
|
|
ntdll_floor,
|
|
|
|
ntdll_log,
|
|
|
|
ntdll_pow,
|
|
|
|
ntdll_sin,
|
|
|
|
ntdll_sqrt,
|
|
|
|
ntdll_tan,
|
2020-06-02 17:08:59 +02:00
|
|
|
virtual_release_address_space,
|
2020-06-23 19:39:12 +02:00
|
|
|
load_so_dll,
|
|
|
|
init_builtin_dll,
|
2021-03-16 11:10:15 +01:00
|
|
|
init_unix_lib,
|
2020-06-25 10:32:47 +02:00
|
|
|
unwind_builtin_dll,
|
2020-05-14 15:17:06 +02:00
|
|
|
};
|
|
|
|
|
2020-05-14 15:26:32 +02:00
|
|
|
|
2020-06-29 12:19:13 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* start_main_thread
|
|
|
|
*/
|
|
|
|
static void start_main_thread(void)
|
|
|
|
{
|
2020-09-02 15:06:05 +02:00
|
|
|
NTSTATUS status;
|
2020-06-29 12:19:13 +02:00
|
|
|
TEB *teb = virtual_alloc_first_teb();
|
|
|
|
|
|
|
|
signal_init_threading();
|
|
|
|
signal_alloc_thread( teb );
|
|
|
|
signal_init_thread( teb );
|
|
|
|
dbg_init();
|
2021-02-02 10:18:26 +01:00
|
|
|
startup_info_size = server_init_process();
|
2020-06-29 12:19:13 +02:00
|
|
|
virtual_map_user_shared_data();
|
|
|
|
init_cpu_info();
|
|
|
|
init_files();
|
2021-04-19 20:08:13 +02:00
|
|
|
load_libwine();
|
2021-02-15 12:30:28 +01:00
|
|
|
init_startup_info();
|
2021-04-19 20:08:13 +02:00
|
|
|
if (p___wine_main_argc) *p___wine_main_argc = main_argc;
|
|
|
|
if (p___wine_main_argv) *p___wine_main_argv = main_argv;
|
|
|
|
if (p___wine_main_wargv) *p___wine_main_wargv = main_wargv;
|
2021-06-15 12:40:01 +02:00
|
|
|
*(ULONG_PTR *)&peb->CloudFileFlags = get_image_address();
|
2021-05-05 10:55:36 +02:00
|
|
|
set_load_order_app_name( main_wargv[0] );
|
2021-06-11 10:56:36 +02:00
|
|
|
init_thread_stack( teb, is_win64 ? 0x7fffffff : 0, 0, 0 );
|
2020-06-29 12:19:13 +02:00
|
|
|
NtCreateKeyedEvent( &keyed_event, GENERIC_READ | GENERIC_WRITE, NULL, 0 );
|
2020-09-28 19:38:17 +02:00
|
|
|
load_ntdll();
|
2021-07-07 11:39:41 +02:00
|
|
|
if (main_image_info.Machine != current_machine) load_wow64_ntdll( main_image_info.Machine );
|
2020-09-02 15:06:05 +02:00
|
|
|
status = p__wine_set_unix_funcs( NTDLL_UNIXLIB_VERSION, &unix_funcs );
|
2021-03-24 10:53:59 +01:00
|
|
|
if (status == STATUS_REVISION_MISMATCH)
|
|
|
|
{
|
|
|
|
ERR( "ntdll library version mismatch\n" );
|
|
|
|
NtTerminateProcess( GetCurrentProcess(), status );
|
|
|
|
}
|
2020-09-01 13:03:44 +02:00
|
|
|
server_init_process_done();
|
2020-06-29 12:19:13 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 12:45:18 +01:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
|
|
|
#ifndef WINE_JAVA_CLASS
|
|
|
|
#define WINE_JAVA_CLASS "org/winehq/wine/WineActivity"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JavaVM *java_vm = NULL;
|
|
|
|
jobject java_object = 0;
|
|
|
|
unsigned short java_gdt_sel = 0;
|
|
|
|
|
|
|
|
/* main Wine initialisation */
|
|
|
|
static jstring wine_init_jni( JNIEnv *env, jobject obj, jobjectArray cmdline, jobjectArray environment )
|
|
|
|
{
|
|
|
|
char **argv;
|
|
|
|
char *str;
|
|
|
|
char error[1024];
|
|
|
|
int i, argc, length;
|
|
|
|
|
|
|
|
/* get the command line array */
|
|
|
|
|
|
|
|
argc = (*env)->GetArrayLength( env, cmdline );
|
|
|
|
for (i = length = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
jobject str_obj = (*env)->GetObjectArrayElement( env, cmdline, i );
|
|
|
|
length += (*env)->GetStringUTFLength( env, str_obj ) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
argv = malloc( (argc + 1) * sizeof(*argv) + length );
|
|
|
|
str = (char *)(argv + argc + 1);
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
jobject str_obj = (*env)->GetObjectArrayElement( env, cmdline, i );
|
|
|
|
length = (*env)->GetStringUTFLength( env, str_obj );
|
|
|
|
(*env)->GetStringUTFRegion( env, str_obj, 0,
|
|
|
|
(*env)->GetStringLength( env, str_obj ), str );
|
|
|
|
argv[i] = str;
|
|
|
|
str[length] = 0;
|
|
|
|
str += length + 1;
|
|
|
|
}
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
/* set the environment variables */
|
|
|
|
|
|
|
|
if (environment)
|
|
|
|
{
|
|
|
|
int count = (*env)->GetArrayLength( env, environment );
|
|
|
|
for (i = 0; i < count - 1; i += 2)
|
|
|
|
{
|
|
|
|
jobject var_obj = (*env)->GetObjectArrayElement( env, environment, i );
|
|
|
|
jobject val_obj = (*env)->GetObjectArrayElement( env, environment, i + 1 );
|
|
|
|
const char *var = (*env)->GetStringUTFChars( env, var_obj, NULL );
|
|
|
|
|
|
|
|
if (val_obj)
|
|
|
|
{
|
|
|
|
const char *val = (*env)->GetStringUTFChars( env, val_obj, NULL );
|
|
|
|
setenv( var, val, 1 );
|
|
|
|
if (!strcmp( var, "LD_LIBRARY_PATH" ))
|
|
|
|
{
|
|
|
|
void (*update_func)( const char * ) = dlsym( RTLD_DEFAULT,
|
|
|
|
"android_update_LD_LIBRARY_PATH" );
|
|
|
|
if (update_func) update_func( val );
|
|
|
|
}
|
|
|
|
else if (!strcmp( var, "WINEDEBUGLOG" ))
|
|
|
|
{
|
|
|
|
int fd = open( val, O_WRONLY | O_CREAT | O_APPEND, 0666 );
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
dup2( fd, 2 );
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*env)->ReleaseStringUTFChars( env, val_obj, val );
|
|
|
|
}
|
|
|
|
else unsetenv( var );
|
|
|
|
|
|
|
|
(*env)->ReleaseStringUTFChars( env, var_obj, var );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
java_object = (*env)->NewGlobalRef( env, obj );
|
|
|
|
|
|
|
|
init_paths( argv );
|
|
|
|
virtual_init();
|
|
|
|
init_environment( argc, argv, environ );
|
|
|
|
|
|
|
|
#ifdef __i386__
|
|
|
|
{
|
|
|
|
unsigned short java_fs;
|
|
|
|
__asm__( "mov %%fs,%0" : "=r" (java_fs) );
|
2020-12-08 12:46:17 +01:00
|
|
|
if (!(java_fs & 4)) java_gdt_sel = java_fs;
|
2020-12-08 12:45:18 +01:00
|
|
|
__asm__( "mov %0,%%fs" :: "r" (0) );
|
|
|
|
start_main_thread();
|
|
|
|
__asm__( "mov %0,%%fs" :: "r" (java_fs) );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
start_main_thread();
|
|
|
|
#endif
|
|
|
|
return (*env)->NewStringUTF( env, error );
|
|
|
|
}
|
|
|
|
|
|
|
|
jint JNI_OnLoad( JavaVM *vm, void *reserved )
|
|
|
|
{
|
|
|
|
static const JNINativeMethod method =
|
|
|
|
{
|
|
|
|
"wine_init", "([Ljava/lang/String;[Ljava/lang/String;)Ljava/lang/String;", wine_init_jni
|
|
|
|
};
|
|
|
|
|
|
|
|
JNIEnv *env;
|
|
|
|
jclass class;
|
|
|
|
|
|
|
|
java_vm = vm;
|
|
|
|
if ((*vm)->AttachCurrentThread( vm, &env, NULL ) != JNI_OK) return JNI_ERR;
|
|
|
|
if (!(class = (*env)->FindClass( env, WINE_JAVA_CLASS ))) return JNI_ERR;
|
|
|
|
(*env)->RegisterNatives( env, class, &method, 1 );
|
|
|
|
return JNI_VERSION_1_6;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __ANDROID__ */
|
2020-06-29 12:19:13 +02:00
|
|
|
|
2020-05-14 15:26:32 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
static void *apple_wine_thread( void *arg )
|
|
|
|
{
|
2020-06-29 12:19:13 +02:00
|
|
|
start_main_thread();
|
2020-05-14 15:26:32 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* apple_create_wine_thread
|
|
|
|
*
|
|
|
|
* Spin off a secondary thread to complete Wine initialization, leaving
|
|
|
|
* the original thread for the Mac frameworks.
|
|
|
|
*
|
|
|
|
* Invoked as a CFRunLoopSource perform callback.
|
|
|
|
*/
|
|
|
|
static void apple_create_wine_thread( void *arg )
|
|
|
|
{
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
2020-09-02 16:29:35 +02:00
|
|
|
pthread_attr_init( &attr );
|
|
|
|
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
|
|
|
|
if (pthread_create( &thread, &attr, apple_wine_thread, NULL )) exit(1);
|
|
|
|
pthread_attr_destroy( &attr );
|
2020-05-14 15:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* apple_main_thread
|
|
|
|
*
|
|
|
|
* Park the process's original thread in a Core Foundation run loop for
|
|
|
|
* use by the Mac frameworks, especially receiving and handling
|
|
|
|
* distributed notifications. Spin off a new thread for the rest of the
|
|
|
|
* Wine initialization.
|
|
|
|
*/
|
|
|
|
static void apple_main_thread(void)
|
|
|
|
{
|
|
|
|
CFRunLoopSourceContext source_context = { 0 };
|
|
|
|
CFRunLoopSourceRef source;
|
|
|
|
|
|
|
|
if (!pthread_main_np()) return;
|
|
|
|
|
|
|
|
/* Multi-processing Services can get confused about the main thread if the
|
|
|
|
* first time it's used is on a secondary thread. Use it here to make sure
|
|
|
|
* that doesn't happen. */
|
|
|
|
MPTaskIsPreemptive(MPCurrentTaskID());
|
|
|
|
|
|
|
|
/* Give ourselves the best chance of having the distributed notification
|
|
|
|
* center scheduled on this thread's run loop. In theory, it's scheduled
|
|
|
|
* in the first thread to ask for it. */
|
|
|
|
CFNotificationCenterGetDistributedCenter();
|
|
|
|
|
|
|
|
/* We use this run loop source for two purposes. First, a run loop exits
|
|
|
|
* if it has no more sources scheduled. So, we need at least one source
|
|
|
|
* to keep the run loop running. Second, although it's not critical, it's
|
|
|
|
* preferable for the Wine initialization to not proceed until we know
|
|
|
|
* the run loop is running. So, we signal our source immediately after
|
|
|
|
* adding it and have its callback spin off the Wine thread. */
|
|
|
|
source_context.perform = apple_create_wine_thread;
|
|
|
|
source = CFRunLoopSourceCreate( NULL, 0, &source_context );
|
|
|
|
CFRunLoopAddSource( CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes );
|
|
|
|
CFRunLoopSourceSignal( source );
|
|
|
|
CFRelease( source );
|
|
|
|
CFRunLoopRun(); /* Should never return, except on error. */
|
|
|
|
}
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
2020-05-17 11:11:28 +02:00
|
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
|
|
|
static int pre_exec(void)
|
|
|
|
{
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
return 1; /* we have a preloader */
|
|
|
|
#else
|
|
|
|
return 0; /* no exec needed */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__linux__) && (defined(__i386__) || defined(__arm__))
|
|
|
|
|
|
|
|
static void check_vmsplit( void *stack )
|
|
|
|
{
|
|
|
|
if (stack < (void *)0x80000000)
|
|
|
|
{
|
|
|
|
/* if the stack is below 0x80000000, assume we can safely try a munmap there */
|
|
|
|
if (munmap( (void *)0x80000000, 1 ) == -1 && errno == EINVAL)
|
|
|
|
ERR( "Warning: memory above 0x80000000 doesn't seem to be accessible.\n"
|
|
|
|
"Wine requires a 3G/1G user/kernel memory split to work properly.\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pre_exec(void)
|
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
|
|
|
|
check_vmsplit( &temp );
|
|
|
|
#ifdef __i386__
|
|
|
|
return 1; /* we have a preloader on x86 */
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__linux__) && (defined(__x86_64__) || defined(__aarch64__))
|
|
|
|
|
|
|
|
static int pre_exec(void)
|
|
|
|
{
|
|
|
|
return 1; /* we have a preloader on x86-64/arm64 */
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__))
|
|
|
|
|
|
|
|
static int pre_exec(void)
|
|
|
|
{
|
|
|
|
return 1; /* we have a preloader */
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif (defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__))
|
|
|
|
|
|
|
|
static int pre_exec(void)
|
|
|
|
{
|
|
|
|
struct rlimit rl;
|
|
|
|
|
|
|
|
rl.rlim_cur = 0x02000000;
|
|
|
|
rl.rlim_max = 0x02000000;
|
|
|
|
setrlimit( RLIMIT_DATA, &rl );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int pre_exec(void)
|
|
|
|
{
|
|
|
|
return 0; /* no exec needed */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* check_command_line
|
|
|
|
*
|
|
|
|
* Check if command line is one that needs to be handled specially.
|
|
|
|
*/
|
|
|
|
static void check_command_line( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
static const char usage[] =
|
|
|
|
"Usage: wine PROGRAM [ARGUMENTS...] Run the specified program\n"
|
|
|
|
" wine --help Display this help and exit\n"
|
|
|
|
" wine --version Output version information and exit";
|
|
|
|
|
|
|
|
if (argc <= 1)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s\n", usage );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!strcmp( argv[1], "--help" ))
|
|
|
|
{
|
|
|
|
printf( "%s\n", usage );
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (!strcmp( argv[1], "--version" ))
|
|
|
|
{
|
2021-04-05 13:05:44 +02:00
|
|
|
extern const char wine_build[];
|
|
|
|
printf( "%s\n", wine_build );
|
2020-05-17 11:11:28 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-14 15:26:32 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* __wine_main
|
|
|
|
*
|
|
|
|
* Main entry point called by the wine loader.
|
|
|
|
*/
|
|
|
|
void __wine_main( int argc, char *argv[], char *envp[] )
|
|
|
|
{
|
2020-12-08 12:41:20 +01:00
|
|
|
init_paths( argv );
|
2020-05-17 11:11:28 +02:00
|
|
|
|
|
|
|
if (!getenv( "WINELOADERNOEXEC" )) /* first time around */
|
|
|
|
{
|
|
|
|
check_command_line( argc, argv );
|
|
|
|
if (pre_exec())
|
|
|
|
{
|
2020-08-14 13:34:51 +02:00
|
|
|
static char noexec[] = "WINELOADERNOEXEC=1";
|
2020-05-19 14:08:30 +02:00
|
|
|
char **new_argv = malloc( (argc + 2) * sizeof(*argv) );
|
2020-08-14 13:34:51 +02:00
|
|
|
|
2020-05-19 14:08:30 +02:00
|
|
|
memcpy( new_argv + 1, argv, (argc + 1) * sizeof(*argv) );
|
2020-08-14 13:34:51 +02:00
|
|
|
putenv( noexec );
|
2021-03-17 10:38:43 +01:00
|
|
|
loader_exec( argv0, new_argv, current_machine );
|
2020-05-19 12:35:54 +02:00
|
|
|
fatal_error( "could not exec the wine loader\n" );
|
2020-05-17 11:11:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 14:08:30 +02:00
|
|
|
#ifdef RLIMIT_NOFILE
|
|
|
|
set_max_limit( RLIMIT_NOFILE );
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
set_max_limit( RLIMIT_AS );
|
|
|
|
#endif
|
|
|
|
|
2020-05-14 15:33:36 +02:00
|
|
|
virtual_init();
|
2020-06-11 10:50:11 +02:00
|
|
|
init_environment( argc, argv, envp );
|
2020-05-20 14:04:32 +02:00
|
|
|
|
2020-05-14 15:26:32 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
apple_main_thread();
|
|
|
|
#endif
|
2020-06-29 12:19:13 +02:00
|
|
|
start_main_thread();
|
2020-05-14 15:26:32 +02:00
|
|
|
}
|