2000-11-08 05:33:20 +01:00
|
|
|
/*
|
|
|
|
* Win32 builtin dlls support
|
|
|
|
*
|
|
|
|
* Copyright 2000 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-11-08 05:33:20 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2001-05-14 22:09:37 +02:00
|
|
|
#include "wine/port.h"
|
2000-11-08 05:33:20 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2003-03-21 22:28:33 +01:00
|
|
|
#include <fcntl.h>
|
2007-03-30 12:17:01 +02:00
|
|
|
#include <limits.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2000-11-08 05:33:20 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2000-11-09 00:02:48 +01:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
2006-06-27 17:35:46 +02:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
# include <sys/resource.h>
|
|
|
|
#endif
|
2003-03-21 22:28:33 +01:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2000-11-08 05:33:20 +01:00
|
|
|
|
2004-06-14 19:07:30 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <crt_externs.h>
|
|
|
|
#define environ (*_NSGetEnviron())
|
2010-11-16 08:41:14 +01:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2013-01-07 21:43:31 +01:00
|
|
|
#define LoadResource MacLoadResource
|
|
|
|
#define GetCurrentThread MacGetCurrentThread
|
|
|
|
#include <CoreServices/CoreServices.h>
|
|
|
|
#undef LoadResource
|
|
|
|
#undef GetCurrentThread
|
2010-11-16 08:41:14 +01:00
|
|
|
#include <pthread.h>
|
2004-06-14 19:07:30 +02:00
|
|
|
#else
|
|
|
|
extern char **environ;
|
|
|
|
#endif
|
|
|
|
|
2013-10-01 13:47:40 +02:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include <jni.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-07 21:43:31 +01:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "wine/library.h"
|
|
|
|
|
2002-05-17 01:16:01 +02:00
|
|
|
/* argc/argv for the Windows application */
|
|
|
|
int __wine_main_argc = 0;
|
|
|
|
char **__wine_main_argv = NULL;
|
|
|
|
WCHAR **__wine_main_wargv = NULL;
|
2003-10-11 01:33:21 +02:00
|
|
|
char **__wine_main_environ = NULL;
|
2002-05-17 01:16:01 +02:00
|
|
|
|
2004-01-02 22:08:05 +01:00
|
|
|
struct dll_path_context
|
|
|
|
{
|
2007-01-25 22:14:01 +01:00
|
|
|
unsigned int index; /* current index in the dll path list */
|
|
|
|
char *buffer; /* buffer used for storing path names */
|
|
|
|
char *name; /* start of file name part in buffer (including leading slash) */
|
|
|
|
int namelen; /* length of file name without .so extension */
|
2007-07-30 14:44:05 +02:00
|
|
|
int win16; /* 16-bit dll search */
|
2004-01-02 22:08:05 +01:00
|
|
|
};
|
|
|
|
|
2000-11-08 05:33:20 +01:00
|
|
|
#define MAX_DLLS 100
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
const IMAGE_NT_HEADERS *nt; /* NT header */
|
|
|
|
const char *filename; /* DLL file name */
|
|
|
|
} builtin_dlls[MAX_DLLS];
|
|
|
|
|
|
|
|
static int nb_dlls;
|
|
|
|
|
|
|
|
static const IMAGE_NT_HEADERS *main_exe;
|
|
|
|
|
|
|
|
static load_dll_callback_t load_dll_callback;
|
|
|
|
|
2006-03-17 13:08:08 +01:00
|
|
|
static const char *build_dir;
|
2006-02-11 22:38:44 +01:00
|
|
|
static const char *default_dlldir;
|
2002-03-20 23:19:06 +01:00
|
|
|
static const char **dll_paths;
|
2007-01-25 22:14:01 +01:00
|
|
|
static unsigned int nb_dll_paths;
|
2000-11-08 05:33:20 +01:00
|
|
|
static int dll_path_maxlen;
|
|
|
|
|
2004-05-25 03:29:24 +02:00
|
|
|
extern void mmap_init(void);
|
2015-11-09 03:37:51 +01:00
|
|
|
extern const char *get_dlldir( const char **default_dlldir );
|
2000-11-08 05:33:20 +01:00
|
|
|
|
|
|
|
/* build the dll load path from the WINEDLLPATH variable */
|
|
|
|
static void build_dll_path(void)
|
|
|
|
{
|
2002-03-20 23:19:06 +01:00
|
|
|
int len, count = 0;
|
2000-11-08 05:33:20 +01:00
|
|
|
char *p, *path = getenv( "WINEDLLPATH" );
|
2015-11-09 03:37:51 +01:00
|
|
|
const char *dlldir = get_dlldir( &default_dlldir );
|
2000-11-08 05:33:20 +01:00
|
|
|
|
2002-03-20 23:19:06 +01:00
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
/* count how many path elements we need */
|
|
|
|
path = strdup(path);
|
|
|
|
p = path;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
while (*p == ':') p++;
|
|
|
|
if (!*p) break;
|
|
|
|
count++;
|
|
|
|
while (*p && *p != ':') p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-17 17:39:44 +01:00
|
|
|
dll_paths = malloc( (count+2) * sizeof(*dll_paths) );
|
|
|
|
nb_dll_paths = 0;
|
|
|
|
|
|
|
|
if (dlldir)
|
|
|
|
{
|
|
|
|
dll_path_maxlen = strlen(dlldir);
|
|
|
|
dll_paths[nb_dll_paths++] = dlldir;
|
|
|
|
}
|
2006-03-17 13:08:08 +01:00
|
|
|
else if ((build_dir = wine_get_build_dir()))
|
|
|
|
{
|
|
|
|
dll_path_maxlen = strlen(build_dir) + sizeof("/programs");
|
|
|
|
}
|
2002-03-20 23:19:06 +01:00
|
|
|
|
|
|
|
if (count)
|
2000-11-08 05:33:20 +01:00
|
|
|
{
|
2002-03-20 23:19:06 +01:00
|
|
|
p = path;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
while (*p == ':') *p++ = 0;
|
|
|
|
if (!*p) break;
|
|
|
|
dll_paths[nb_dll_paths] = p;
|
|
|
|
while (*p && *p != ':') p++;
|
|
|
|
if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
|
|
|
|
dll_path_maxlen = p - dll_paths[nb_dll_paths];
|
|
|
|
nb_dll_paths++;
|
|
|
|
}
|
2000-11-08 05:33:20 +01:00
|
|
|
}
|
|
|
|
|
2002-03-20 23:19:06 +01:00
|
|
|
/* append default dll dir (if not empty) to path */
|
2006-02-11 22:38:44 +01:00
|
|
|
if ((len = strlen(default_dlldir)) > 0)
|
2000-11-08 05:33:20 +01:00
|
|
|
{
|
2002-03-20 23:19:06 +01:00
|
|
|
if (len > dll_path_maxlen) dll_path_maxlen = len;
|
2005-09-12 13:20:31 +02:00
|
|
|
dll_paths[nb_dll_paths++] = default_dlldir;
|
2000-11-08 05:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-11 17:29:07 +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
|
|
|
|
}
|
|
|
|
|
2002-04-04 00:13:01 +02:00
|
|
|
/* check if a given file can be opened */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline int file_exists( const char *name )
|
2002-04-04 00:13:01 +02:00
|
|
|
{
|
2009-08-11 17:29:07 +02:00
|
|
|
int ret = 0;
|
2002-04-04 00:13:01 +02:00
|
|
|
int fd = open( name, O_RDONLY );
|
2009-08-11 17:29:07 +02:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
ret = check_library_arch( fd );
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
return ret;
|
2002-04-04 00:13:01 +02:00
|
|
|
}
|
2000-11-08 05:33:20 +01:00
|
|
|
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline char *prepend( char *buffer, const char *str, size_t len )
|
2006-03-17 13:08:08 +01:00
|
|
|
{
|
|
|
|
return memcpy( buffer - len, str, len );
|
|
|
|
}
|
2004-01-02 22:08:05 +01:00
|
|
|
|
|
|
|
/* get a filename from the next entry in the dll path */
|
|
|
|
static char *next_dll_path( struct dll_path_context *context )
|
|
|
|
{
|
2007-01-25 22:14:01 +01:00
|
|
|
unsigned int index = context->index++;
|
2006-03-17 13:08:08 +01:00
|
|
|
int namelen = context->namelen;
|
|
|
|
char *path = context->name;
|
2004-01-02 22:08:05 +01:00
|
|
|
|
2006-03-17 13:08:08 +01:00
|
|
|
switch(index)
|
2004-01-02 22:08:05 +01:00
|
|
|
{
|
2009-02-24 18:11:53 +01:00
|
|
|
case 0: /* try dlls dir with subdir prefix */
|
|
|
|
if (namelen > 4 && !memcmp( context->name + namelen - 4, ".dll", 4 )) namelen -= 4;
|
|
|
|
if (!context->win16) path = prepend( path, context->name, namelen );
|
|
|
|
path = prepend( path, "/dlls", sizeof("/dlls") - 1 );
|
|
|
|
path = prepend( path, build_dir, strlen(build_dir) );
|
|
|
|
return path;
|
|
|
|
case 1: /* try programs dir with subdir prefix */
|
|
|
|
if (!context->win16)
|
2006-03-17 13:08:08 +01:00
|
|
|
{
|
2009-02-24 18:11:53 +01:00
|
|
|
if (namelen > 4 && !memcmp( context->name + namelen - 4, ".exe", 4 )) namelen -= 4;
|
|
|
|
path = prepend( path, context->name, namelen );
|
2006-03-17 13:08:08 +01:00
|
|
|
path = prepend( path, "/programs", sizeof("/programs") - 1 );
|
|
|
|
path = prepend( path, build_dir, strlen(build_dir) );
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
context->index++;
|
|
|
|
/* fall through */
|
|
|
|
default:
|
2007-07-26 11:52:53 +02:00
|
|
|
index -= 2;
|
2013-09-26 14:31:57 +02:00
|
|
|
if (index >= nb_dll_paths) return NULL;
|
|
|
|
path = prepend( path, dll_paths[index], strlen( dll_paths[index] ));
|
|
|
|
return path;
|
2004-01-02 22:08:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get a filename from the first entry in the dll path */
|
2007-07-30 14:44:05 +02:00
|
|
|
static char *first_dll_path( const char *name, int win16, struct dll_path_context *context )
|
2004-01-02 22:08:05 +01:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int namelen = strlen( name );
|
2007-07-30 14:44:05 +02:00
|
|
|
const char *ext = win16 ? "16" : ".so";
|
2004-01-02 22:08:05 +01:00
|
|
|
|
2006-03-17 13:08:08 +01:00
|
|
|
context->buffer = malloc( dll_path_maxlen + 2 * namelen + strlen(ext) + 3 );
|
2007-07-26 11:52:53 +02:00
|
|
|
context->index = build_dir ? 0 : 2; /* if no build dir skip all the build dir magic cases */
|
2006-03-17 13:08:08 +01:00
|
|
|
context->name = context->buffer + dll_path_maxlen + namelen + 1;
|
|
|
|
context->namelen = namelen + 1;
|
2007-07-30 14:44:05 +02:00
|
|
|
context->win16 = win16;
|
2004-01-02 22:08:05 +01:00
|
|
|
|
|
|
|
/* store the name at the end of the buffer, followed by extension */
|
2006-03-17 13:08:08 +01:00
|
|
|
p = context->name;
|
2004-01-02 22:08:05 +01:00
|
|
|
*p++ = '/';
|
|
|
|
memcpy( p, name, namelen );
|
|
|
|
strcpy( p + namelen, ext );
|
|
|
|
return next_dll_path( context );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* free the dll path context created by first_dll_path */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline void free_dll_path( struct dll_path_context *context )
|
2004-01-02 22:08:05 +01:00
|
|
|
{
|
|
|
|
free( context->buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-08 05:33:20 +01:00
|
|
|
/* open a library for a given dll, searching in the dll path
|
|
|
|
* 'name' must be the Windows dll name (e.g. "kernel32.dll") */
|
2003-07-03 20:23:10 +02:00
|
|
|
static void *dlopen_dll( const char *name, char *error, int errorsize,
|
|
|
|
int test_only, int *exists )
|
2000-11-08 05:33:20 +01:00
|
|
|
{
|
2004-01-02 22:08:05 +01:00
|
|
|
struct dll_path_context context;
|
|
|
|
char *path;
|
2000-11-08 05:33:20 +01:00
|
|
|
void *ret = NULL;
|
|
|
|
|
2003-07-03 20:23:10 +02:00
|
|
|
*exists = 0;
|
2007-07-30 14:44:05 +02:00
|
|
|
for (path = first_dll_path( name, 0, &context ); path; path = next_dll_path( &context ))
|
2000-11-08 05:33:20 +01:00
|
|
|
{
|
2004-01-02 22:08:05 +01:00
|
|
|
if (!test_only && (ret = wine_dlopen( path, RTLD_NOW, error, errorsize ))) break;
|
|
|
|
if ((*exists = file_exists( path ))) break; /* exists but cannot be loaded, return the error */
|
2000-11-08 05:33:20 +01:00
|
|
|
}
|
2004-01-02 22:08:05 +01:00
|
|
|
free_dll_path( &context );
|
2000-11-08 05:33:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-09 00:02:48 +01:00
|
|
|
/* adjust an array of pointers to make them into RVAs */
|
2005-09-17 16:39:51 +02:00
|
|
|
static inline void fixup_rva_ptrs( void *array, BYTE *base, unsigned int count )
|
2000-11-09 00:02:48 +01:00
|
|
|
{
|
2005-09-17 16:39:51 +02:00
|
|
|
void **src = (void **)array;
|
|
|
|
DWORD *dst = (DWORD *)array;
|
2000-11-09 00:02:48 +01:00
|
|
|
while (count--)
|
|
|
|
{
|
2005-09-17 16:39:51 +02:00
|
|
|
*dst++ = *src ? (BYTE *)*src - base : 0;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fixup an array of RVAs by adding the specified delta */
|
|
|
|
static inline void fixup_rva_dwords( DWORD *ptr, int delta, unsigned int count )
|
|
|
|
{
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
if (*ptr) *ptr += delta;
|
2000-11-09 00:02:48 +01:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-18 15:41:43 +02:00
|
|
|
/* fixup an array of name/ordinal RVAs by adding the specified delta */
|
|
|
|
static inline void fixup_rva_names( UINT_PTR *ptr, int delta )
|
|
|
|
{
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (!(*ptr & IMAGE_ORDINAL_FLAG)) *ptr += delta;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-28 19:54:31 +02:00
|
|
|
/* fixup RVAs in the import directory */
|
2005-09-17 16:39:51 +02:00
|
|
|
static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, BYTE *base, int delta )
|
2002-07-28 19:54:31 +02:00
|
|
|
{
|
2005-09-17 16:39:51 +02:00
|
|
|
while (dir->Name)
|
2002-07-28 19:54:31 +02:00
|
|
|
{
|
2005-09-17 16:39:51 +02:00
|
|
|
fixup_rva_dwords( &dir->u.OriginalFirstThunk, delta, 1 );
|
|
|
|
fixup_rva_dwords( &dir->Name, delta, 1 );
|
|
|
|
fixup_rva_dwords( &dir->FirstThunk, delta, 1 );
|
2017-09-18 15:41:43 +02:00
|
|
|
if (dir->u.OriginalFirstThunk) fixup_rva_names( (UINT_PTR *)(base + dir->u.OriginalFirstThunk), delta );
|
|
|
|
if (dir->FirstThunk) fixup_rva_names( (UINT_PTR *)(base + dir->FirstThunk), delta );
|
2005-09-17 16:39:51 +02:00
|
|
|
dir++;
|
2002-07-28 19:54:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-17 16:39:51 +02:00
|
|
|
/* fixup RVAs in the export directory */
|
|
|
|
static void fixup_exports( IMAGE_EXPORT_DIRECTORY *dir, BYTE *base, int delta )
|
|
|
|
{
|
|
|
|
fixup_rva_dwords( &dir->Name, delta, 1 );
|
|
|
|
fixup_rva_dwords( &dir->AddressOfFunctions, delta, 1 );
|
|
|
|
fixup_rva_dwords( &dir->AddressOfNames, delta, 1 );
|
|
|
|
fixup_rva_dwords( &dir->AddressOfNameOrdinals, delta, 1 );
|
|
|
|
fixup_rva_dwords( (DWORD *)(base + dir->AddressOfNames), delta, dir->NumberOfNames );
|
|
|
|
fixup_rva_ptrs( (base + dir->AddressOfFunctions), base, dir->NumberOfFunctions );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-09 00:02:48 +01:00
|
|
|
/* fixup RVAs in the resource directory */
|
2005-09-17 16:39:51 +02:00
|
|
|
static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, BYTE *root, int delta )
|
2000-11-09 00:02:48 +01:00
|
|
|
{
|
|
|
|
IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
|
|
|
|
for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
|
|
|
|
{
|
2013-04-29 09:19:17 +02:00
|
|
|
void *ptr = root + entry->u2.s2.OffsetToDirectory;
|
|
|
|
if (entry->u2.s2.DataIsDirectory) fixup_resources( ptr, root, delta );
|
2000-11-09 00:02:48 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
|
2005-09-17 16:39:51 +02:00
|
|
|
fixup_rva_dwords( &data->OffsetToData, delta, 1 );
|
2000-11-09 00:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* map a builtin dll in memory and fixup RVAs */
|
|
|
|
static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
|
|
|
|
{
|
2002-05-15 01:18:23 +02:00
|
|
|
#ifdef HAVE_MMAP
|
2000-11-09 00:02:48 +01:00
|
|
|
IMAGE_DATA_DIRECTORY *dir;
|
|
|
|
IMAGE_DOS_HEADER *dos;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
IMAGE_SECTION_HEADER *sec;
|
2003-11-03 23:19:44 +01:00
|
|
|
BYTE *addr;
|
|
|
|
DWORD code_start, data_start, data_end;
|
2013-01-08 22:02:37 +01:00
|
|
|
const size_t page_size = sysconf( _SC_PAGESIZE );
|
2003-11-03 23:19:44 +01:00
|
|
|
const size_t page_mask = page_size - 1;
|
2007-01-25 22:14:01 +01:00
|
|
|
int delta, nb_sections = 2; /* code + data */
|
|
|
|
unsigned int i;
|
2000-11-09 00:02:48 +01:00
|
|
|
|
|
|
|
size_t size = (sizeof(IMAGE_DOS_HEADER)
|
|
|
|
+ sizeof(IMAGE_NT_HEADERS)
|
|
|
|
+ nb_sections * sizeof(IMAGE_SECTION_HEADER));
|
|
|
|
|
|
|
|
assert( size <= page_size );
|
|
|
|
|
2002-08-14 23:09:55 +02:00
|
|
|
/* module address must be aligned on 64K boundary */
|
|
|
|
addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
|
|
|
|
if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
|
2000-11-09 00:02:48 +01:00
|
|
|
|
|
|
|
dos = (IMAGE_DOS_HEADER *)addr;
|
|
|
|
nt = (IMAGE_NT_HEADERS *)(dos + 1);
|
|
|
|
sec = (IMAGE_SECTION_HEADER *)(nt + 1);
|
|
|
|
|
|
|
|
/* Build the DOS and NT headers */
|
|
|
|
|
2004-09-03 19:28:39 +02:00
|
|
|
dos->e_magic = IMAGE_DOS_SIGNATURE;
|
2007-10-08 19:42:16 +02:00
|
|
|
dos->e_cblp = 0x90;
|
|
|
|
dos->e_cp = 3;
|
2004-09-03 19:28:39 +02:00
|
|
|
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_lfarlc = sizeof(*dos);
|
|
|
|
dos->e_lfanew = sizeof(*dos);
|
2000-11-09 00:02:48 +01:00
|
|
|
|
|
|
|
*nt = *nt_descr;
|
|
|
|
|
2006-08-22 21:21:56 +02:00
|
|
|
delta = (const BYTE *)nt_descr - addr;
|
2003-11-03 23:19:44 +01:00
|
|
|
code_start = page_size;
|
2005-09-17 16:39:51 +02:00
|
|
|
data_start = delta & ~page_mask;
|
|
|
|
data_end = (nt->OptionalHeader.SizeOfImage + delta + page_mask) & ~page_mask;
|
|
|
|
|
|
|
|
fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
|
2003-11-03 23:19:44 +01:00
|
|
|
|
2000-11-09 00:02:48 +01:00
|
|
|
nt->FileHeader.NumberOfSections = nb_sections;
|
2003-11-03 23:19:44 +01:00
|
|
|
nt->OptionalHeader.BaseOfCode = code_start;
|
2005-09-15 17:09:12 +02:00
|
|
|
#ifndef _WIN64
|
2003-11-03 23:19:44 +01:00
|
|
|
nt->OptionalHeader.BaseOfData = data_start;
|
2005-09-15 17:09:12 +02:00
|
|
|
#endif
|
2000-11-09 00:02:48 +01:00
|
|
|
nt->OptionalHeader.SizeOfCode = data_start - code_start;
|
2003-11-03 23:19:44 +01:00
|
|
|
nt->OptionalHeader.SizeOfInitializedData = data_end - data_start;
|
2000-11-09 00:02:48 +01:00
|
|
|
nt->OptionalHeader.SizeOfUninitializedData = 0;
|
2003-11-03 23:19:44 +01:00
|
|
|
nt->OptionalHeader.SizeOfImage = data_end;
|
2005-09-15 17:09:12 +02:00
|
|
|
nt->OptionalHeader.ImageBase = (ULONG_PTR)addr;
|
2000-11-09 00:02:48 +01:00
|
|
|
|
|
|
|
/* Build the code section */
|
|
|
|
|
2005-07-10 19:45:53 +02:00
|
|
|
memcpy( sec->Name, ".text", sizeof(".text") );
|
2000-11-09 00:02:48 +01:00
|
|
|
sec->SizeOfRawData = data_start - code_start;
|
|
|
|
sec->Misc.VirtualSize = sec->SizeOfRawData;
|
2003-11-03 23:19:44 +01:00
|
|
|
sec->VirtualAddress = code_start;
|
|
|
|
sec->PointerToRawData = code_start;
|
2000-11-09 00:02:48 +01:00
|
|
|
sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
|
|
|
|
sec++;
|
|
|
|
|
|
|
|
/* Build the data section */
|
|
|
|
|
2005-07-10 19:45:53 +02:00
|
|
|
memcpy( sec->Name, ".data", sizeof(".data") );
|
2003-11-03 23:19:44 +01:00
|
|
|
sec->SizeOfRawData = data_end - data_start;
|
2000-11-09 00:02:48 +01:00
|
|
|
sec->Misc.VirtualSize = sec->SizeOfRawData;
|
2003-11-03 23:19:44 +01:00
|
|
|
sec->VirtualAddress = data_start;
|
|
|
|
sec->PointerToRawData = data_start;
|
2000-11-09 00:02:48 +01:00
|
|
|
sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
|
|
|
|
IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
|
|
|
|
sec++;
|
|
|
|
|
2005-09-17 16:39:51 +02:00
|
|
|
for (i = 0; i < nt->OptionalHeader.NumberOfRvaAndSizes; i++)
|
|
|
|
fixup_rva_dwords( &nt->OptionalHeader.DataDirectory[i].VirtualAddress, delta, 1 );
|
|
|
|
|
2000-11-09 00:02:48 +01:00
|
|
|
/* Build the import directory */
|
|
|
|
|
|
|
|
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
|
|
|
|
if (dir->Size)
|
|
|
|
{
|
2005-09-17 16:39:51 +02:00
|
|
|
IMAGE_IMPORT_DESCRIPTOR *imports = (void *)(addr + dir->VirtualAddress);
|
|
|
|
fixup_imports( imports, addr, delta );
|
2000-11-09 00:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Build the resource directory */
|
|
|
|
|
|
|
|
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
|
|
|
|
if (dir->Size)
|
|
|
|
{
|
2005-09-17 16:39:51 +02:00
|
|
|
void *ptr = (void *)(addr + dir->VirtualAddress);
|
|
|
|
fixup_resources( ptr, ptr, delta );
|
2000-11-09 00:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Build the export directory */
|
|
|
|
|
|
|
|
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
|
|
|
|
if (dir->Size)
|
|
|
|
{
|
2005-09-17 16:39:51 +02:00
|
|
|
IMAGE_EXPORT_DIRECTORY *exports = (void *)(addr + dir->VirtualAddress);
|
|
|
|
fixup_exports( exports, addr, delta );
|
2000-11-09 00:02:48 +01:00
|
|
|
}
|
|
|
|
return addr;
|
2002-05-15 01:18:23 +02:00
|
|
|
#else /* HAVE_MMAP */
|
|
|
|
return NULL;
|
|
|
|
#endif /* HAVE_MMAP */
|
2000-11-09 00:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-01 14:36:49 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* __wine_get_main_environment
|
|
|
|
*
|
|
|
|
* Return an environment pointer to work around lack of environ variable.
|
|
|
|
* Only exported on Mac OS.
|
|
|
|
*/
|
|
|
|
char **__wine_get_main_environment(void)
|
|
|
|
{
|
|
|
|
return environ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-08 05:33:20 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* __wine_dll_register
|
|
|
|
*
|
|
|
|
* Register a built-in DLL descriptor.
|
|
|
|
*/
|
|
|
|
void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
|
|
|
|
{
|
2000-11-09 00:02:48 +01:00
|
|
|
if (load_dll_callback) load_dll_callback( map_dll(header), filename );
|
2000-11-08 05:33:20 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
|
|
|
|
main_exe = header;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert( nb_dlls < MAX_DLLS );
|
|
|
|
builtin_dlls[nb_dlls].nt = header;
|
|
|
|
builtin_dlls[nb_dlls].filename = filename;
|
|
|
|
nb_dlls++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* wine_dll_set_callback
|
|
|
|
*
|
|
|
|
* Set the callback function for dll loading, and call it
|
|
|
|
* for all dlls that were implicitly loaded already.
|
|
|
|
*/
|
|
|
|
void wine_dll_set_callback( load_dll_callback_t load )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
load_dll_callback = load;
|
|
|
|
for (i = 0; i < nb_dlls; i++)
|
|
|
|
{
|
|
|
|
const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
|
|
|
|
if (!nt) continue;
|
|
|
|
builtin_dlls[i].nt = NULL;
|
2000-11-09 00:02:48 +01:00
|
|
|
load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
|
2000-11-08 05:33:20 +01:00
|
|
|
}
|
|
|
|
nb_dlls = 0;
|
2000-11-09 00:02:48 +01:00
|
|
|
if (main_exe) load_dll_callback( map_dll(main_exe), "" );
|
2000-11-08 05:33:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* wine_dll_load
|
|
|
|
*
|
|
|
|
* Load a builtin dll.
|
|
|
|
*/
|
2003-07-03 20:23:10 +02:00
|
|
|
void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists )
|
2000-11-08 05:33:20 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* callback must have been set already */
|
|
|
|
assert( load_dll_callback );
|
|
|
|
|
|
|
|
/* check if we have it in the list */
|
|
|
|
/* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
|
|
|
|
for (i = 0; i < nb_dlls; i++)
|
|
|
|
{
|
|
|
|
if (!builtin_dlls[i].nt) continue;
|
2000-12-05 22:17:59 +01:00
|
|
|
if (!strcmp( builtin_dlls[i].filename, filename ))
|
2000-11-08 05:33:20 +01:00
|
|
|
{
|
|
|
|
const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
|
|
|
|
builtin_dlls[i].nt = NULL;
|
2000-11-09 00:02:48 +01:00
|
|
|
load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
|
2003-07-03 20:23:10 +02:00
|
|
|
*file_exists = 1;
|
2000-11-08 05:33:20 +01:00
|
|
|
return (void *)1;
|
|
|
|
}
|
|
|
|
}
|
2003-07-03 20:23:10 +02:00
|
|
|
return dlopen_dll( filename, error, errorsize, 0, file_exists );
|
2000-11-08 05:33:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* wine_dll_unload
|
|
|
|
*
|
|
|
|
* Unload a builtin dll.
|
|
|
|
*/
|
|
|
|
void wine_dll_unload( void *handle )
|
|
|
|
{
|
2000-12-13 22:32:55 +01:00
|
|
|
if (handle != (void *)1)
|
|
|
|
wine_dlclose( handle, NULL, 0 );
|
2000-11-08 05:33:20 +01:00
|
|
|
}
|
2000-11-10 02:38:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* wine_dll_load_main_exe
|
|
|
|
*
|
2002-05-22 23:32:49 +02:00
|
|
|
* Try to load the .so for the main exe.
|
2000-11-10 02:38:28 +01:00
|
|
|
*/
|
2003-07-03 20:23:10 +02:00
|
|
|
void *wine_dll_load_main_exe( const char *name, char *error, int errorsize,
|
|
|
|
int test_only, int *file_exists )
|
2000-11-10 02:38:28 +01:00
|
|
|
{
|
2003-07-03 20:23:10 +02:00
|
|
|
return dlopen_dll( name, error, errorsize, test_only, file_exists );
|
2000-11-10 02:38:28 +01:00
|
|
|
}
|
2003-03-20 20:26:18 +01:00
|
|
|
|
|
|
|
|
2006-02-04 17:02:05 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* wine_dll_enum_load_path
|
|
|
|
*
|
|
|
|
* Enumerate the dll load path.
|
|
|
|
*/
|
|
|
|
const char *wine_dll_enum_load_path( unsigned int index )
|
|
|
|
{
|
|
|
|
if (index >= nb_dll_paths) return NULL;
|
|
|
|
return dll_paths[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-02 22:08:05 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* wine_dll_get_owner
|
|
|
|
*
|
|
|
|
* Retrieve the name of the 32-bit owner dll for a 16-bit dll.
|
|
|
|
* Return 0 if OK, -1 on error.
|
|
|
|
*/
|
|
|
|
int wine_dll_get_owner( const char *name, char *buffer, int size, int *exists )
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
char *path;
|
|
|
|
struct dll_path_context context;
|
|
|
|
|
|
|
|
*exists = 0;
|
2006-02-16 13:44:38 +01:00
|
|
|
|
2007-07-30 14:44:05 +02:00
|
|
|
for (path = first_dll_path( name, 1, &context ); path; path = next_dll_path( &context ))
|
2006-02-16 13:44:38 +01:00
|
|
|
{
|
|
|
|
int fd = open( path, O_RDONLY );
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
int res = read( fd, buffer, size - 1 );
|
|
|
|
while (res > 0 && (buffer[res-1] == '\n' || buffer[res-1] == '\r')) res--;
|
|
|
|
buffer[res] = 0;
|
|
|
|
close( fd );
|
|
|
|
*exists = 1;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_dll_path( &context );
|
2004-01-02 22:08:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-27 17:35:46 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* set_max_limit
|
|
|
|
*
|
|
|
|
* Set a user limit to the maximum allowed value.
|
|
|
|
*/
|
|
|
|
static void set_max_limit( int limit )
|
|
|
|
{
|
2006-07-16 16:06:28 +02:00
|
|
|
#ifdef HAVE_SETRLIMIT
|
2006-06-27 17:35:46 +02:00
|
|
|
struct rlimit rlimit;
|
|
|
|
|
|
|
|
if (!getrlimit( limit, &rlimit ))
|
|
|
|
{
|
|
|
|
rlimit.rlim_cur = rlimit.rlim_max;
|
2008-03-22 03:32:49 +01: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
|
|
|
|
}
|
2006-06-27 17:35:46 +02:00
|
|
|
}
|
2006-07-16 16:06:28 +02:00
|
|
|
#endif
|
2006-06-27 17:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-16 08:41:14 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
struct apple_stack_info
|
|
|
|
{
|
|
|
|
void *stack;
|
|
|
|
size_t desired_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* apple_alloc_thread_stack
|
|
|
|
*
|
|
|
|
* Callback for wine_mmap_enum_reserved_areas to allocate space for
|
|
|
|
* the secondary thread's stack.
|
|
|
|
*/
|
|
|
|
static int apple_alloc_thread_stack( void *base, size_t size, void *arg )
|
|
|
|
{
|
|
|
|
struct apple_stack_info *info = arg;
|
|
|
|
|
|
|
|
/* For mysterious reasons, putting the thread stack at the very top
|
|
|
|
* of the address space causes subsequent execs to fail, even on the
|
|
|
|
* child side of a fork. Avoid the top 16MB. */
|
|
|
|
char * const limit = (char*)0xff000000;
|
2010-11-17 15:07:30 +01:00
|
|
|
if ((char *)base >= limit) return 0;
|
2010-11-16 08:41:14 +01:00
|
|
|
if (size > limit - (char*)base)
|
|
|
|
size = limit - (char*)base;
|
|
|
|
if (size < info->desired_size) return 0;
|
|
|
|
info->stack = wine_anon_mmap( (char *)base + size - info->desired_size,
|
|
|
|
info->desired_size, PROT_READ|PROT_WRITE, MAP_FIXED );
|
|
|
|
return (info->stack != (void *)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* 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 *init_func )
|
|
|
|
{
|
|
|
|
int success = 0;
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
|
|
|
if (!pthread_attr_init( &attr ))
|
|
|
|
{
|
|
|
|
struct apple_stack_info info;
|
|
|
|
|
|
|
|
/* Try to put the new thread's stack in the reserved area. If this
|
|
|
|
* fails, just let it go wherever. It'll be a waste of space, but we
|
|
|
|
* can go on. */
|
|
|
|
if (!pthread_attr_getstacksize( &attr, &info.desired_size ) &&
|
|
|
|
wine_mmap_enum_reserved_areas( apple_alloc_thread_stack, &info, 1 ))
|
|
|
|
{
|
|
|
|
wine_mmap_remove_reserved_area( info.stack, info.desired_size, 0 );
|
|
|
|
pthread_attr_setstackaddr( &attr, (char*)info.stack + info.desired_size );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ) &&
|
|
|
|
!pthread_create( &thread, &attr, init_func, NULL ))
|
|
|
|
success = 1;
|
|
|
|
|
|
|
|
pthread_attr_destroy( &attr );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Failure is indicated by returning from wine_init(). Stopping
|
|
|
|
* the run loop allows apple_main_thread() and thus wine_init() to
|
|
|
|
* return. */
|
|
|
|
if (!success)
|
|
|
|
CFRunLoopStop( CFRunLoopGetCurrent() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* 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 (*init_func)(void) )
|
|
|
|
{
|
|
|
|
CFRunLoopSourceContext source_context = { 0 };
|
|
|
|
CFRunLoopSourceRef source;
|
|
|
|
|
2013-04-16 07:37:48 +02:00
|
|
|
if (!pthread_main_np())
|
|
|
|
{
|
|
|
|
init_func();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-07 21:43:31 +01:00
|
|
|
/* 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());
|
|
|
|
|
2010-11-16 08:41:14 +01:00
|
|
|
/* 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.info = init_func;
|
|
|
|
source_context.perform = apple_create_wine_thread;
|
|
|
|
source = CFRunLoopSourceCreate( NULL, 0, &source_context );
|
|
|
|
|
|
|
|
if (source)
|
|
|
|
{
|
|
|
|
CFRunLoopAddSource( CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes );
|
|
|
|
CFRunLoopSourceSignal( source );
|
|
|
|
CFRelease( source );
|
|
|
|
|
|
|
|
CFRunLoopRun(); /* Should never return, except on error. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we get here (i.e. return), that indicates failure to our caller. */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-10-01 13:47:40 +02:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
|
|
|
#ifndef WINE_JAVA_CLASS
|
|
|
|
#define WINE_JAVA_CLASS "org/winehq/wine/WineActivity"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static JavaVM *java_vm;
|
|
|
|
static jobject java_object;
|
|
|
|
|
|
|
|
/* return the Java VM that was used for JNI initialisation */
|
|
|
|
JavaVM *wine_get_java_vm(void)
|
|
|
|
{
|
|
|
|
return java_vm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the Java object that called the wine_init method */
|
|
|
|
jobject wine_get_java_object(void)
|
|
|
|
{
|
|
|
|
return java_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 );
|
2015-10-19 08:43:07 +02:00
|
|
|
(*env)->GetStringUTFRegion( env, str_obj, 0,
|
|
|
|
(*env)->GetStringLength( env, str_obj ), str );
|
2013-10-01 13:47:40 +02:00
|
|
|
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 );
|
2014-07-24 09:24:49 +02:00
|
|
|
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 );
|
|
|
|
}
|
2017-05-30 19:12:35 +02:00
|
|
|
else if (!strcmp( var, "WINEDEBUGLOG" ))
|
|
|
|
{
|
|
|
|
int fd = open( val, O_WRONLY | O_CREAT | O_APPEND, 0666 );
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
dup2( fd, 2 );
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
}
|
2013-10-01 13:47:40 +02:00
|
|
|
(*env)->ReleaseStringUTFChars( env, val_obj, val );
|
|
|
|
}
|
|
|
|
else unsetenv( var );
|
|
|
|
|
|
|
|
(*env)->ReleaseStringUTFChars( env, var_obj, var );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
java_object = (*env)->NewGlobalRef( env, obj );
|
|
|
|
|
2017-04-21 19:25:39 +02:00
|
|
|
#ifdef __i386__
|
|
|
|
{
|
|
|
|
unsigned short java_fs = wine_get_fs();
|
|
|
|
wine_set_fs( 0 );
|
|
|
|
wine_init( argc, argv, error, sizeof(error) );
|
|
|
|
wine_set_fs( java_fs );
|
|
|
|
}
|
|
|
|
#else
|
2013-10-01 13:47:40 +02:00
|
|
|
wine_init( argc, argv, error, sizeof(error) );
|
2017-04-21 19:25:39 +02:00
|
|
|
#endif
|
2013-10-01 13:47:40 +02:00
|
|
|
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__ */
|
|
|
|
|
2003-04-27 02:47:58 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* wine_init
|
|
|
|
*
|
|
|
|
* Main Wine initialisation.
|
|
|
|
*/
|
2004-06-14 19:07:30 +02:00
|
|
|
void wine_init( int argc, char *argv[], char *error, int error_size )
|
2003-04-27 02:47:58 +02:00
|
|
|
{
|
2005-09-12 13:20:31 +02:00
|
|
|
struct dll_path_context context;
|
|
|
|
char *path;
|
|
|
|
void *ntdll = NULL;
|
2003-10-10 02:12:17 +02:00
|
|
|
void (*init_func)(void);
|
2003-04-27 02:47:58 +02:00
|
|
|
|
2006-06-27 17:35:46 +02:00
|
|
|
/* force a few limits that are set too low on some platforms */
|
2006-07-16 16:06:28 +02:00
|
|
|
#ifdef RLIMIT_NOFILE
|
2006-06-27 17:35:46 +02:00
|
|
|
set_max_limit( RLIMIT_NOFILE );
|
2006-07-16 16:06:28 +02:00
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_AS
|
2006-06-27 17:35:46 +02:00
|
|
|
set_max_limit( RLIMIT_AS );
|
2006-07-16 16:06:28 +02:00
|
|
|
#endif
|
2006-06-27 17:35:46 +02:00
|
|
|
|
2003-11-11 23:21:29 +01:00
|
|
|
wine_init_argv0_path( argv[0] );
|
2006-02-17 17:39:44 +01:00
|
|
|
build_dll_path();
|
2003-10-10 02:12:17 +02:00
|
|
|
__wine_main_argc = argc;
|
|
|
|
__wine_main_argv = argv;
|
2009-04-01 14:36:49 +02:00
|
|
|
__wine_main_environ = __wine_get_main_environment();
|
2004-05-25 03:29:24 +02:00
|
|
|
mmap_init();
|
2003-12-05 01:17:41 +01:00
|
|
|
|
2007-07-30 14:44:05 +02:00
|
|
|
for (path = first_dll_path( "ntdll.dll", 0, &context ); path; path = next_dll_path( &context ))
|
2005-09-12 13:20:31 +02:00
|
|
|
{
|
|
|
|
if ((ntdll = wine_dlopen( path, RTLD_NOW, error, error_size )))
|
|
|
|
{
|
|
|
|
/* if we didn't use the default dll dir, remove it from the search path */
|
2007-07-26 11:52:53 +02:00
|
|
|
if (default_dlldir[0] && context.index < nb_dll_paths + 2) nb_dll_paths--;
|
2005-09-12 13:20:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_dll_path( &context );
|
|
|
|
|
|
|
|
if (!ntdll) return;
|
2003-10-10 02:12:17 +02:00
|
|
|
if (!(init_func = wine_dlsym( ntdll, "__wine_process_init", error, error_size ))) return;
|
2010-11-16 08:41:14 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
apple_main_thread( init_func );
|
|
|
|
#else
|
2003-10-10 02:12:17 +02:00
|
|
|
init_func();
|
2010-11-16 08:41:14 +01:00
|
|
|
#endif
|
2003-04-27 02:47:58 +02:00
|
|
|
}
|
|
|
|
|
2003-03-21 22:28:33 +01:00
|
|
|
|
2003-03-20 20:26:18 +01:00
|
|
|
/*
|
|
|
|
* These functions provide wrappers around dlopen() and associated
|
|
|
|
* functions. They work around a bug in glibc 2.1.x where calling
|
|
|
|
* a dl*() function after a previous dl*() function has failed
|
|
|
|
* without a dlerror() call between the two will cause a crash.
|
|
|
|
* They all take a pointer to a buffer that
|
|
|
|
* will receive the error description (from dlerror()). This
|
|
|
|
* parameter may be NULL if the error description is not required.
|
|
|
|
*/
|
|
|
|
|
2005-05-10 17:13:20 +02:00
|
|
|
#ifndef RTLD_FIRST
|
|
|
|
#define RTLD_FIRST 0
|
|
|
|
#endif
|
|
|
|
|
2003-03-20 20:26:18 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* wine_dlopen
|
|
|
|
*/
|
2005-04-25 17:51:45 +02:00
|
|
|
void *wine_dlopen( const char *filename, int flag, char *error, size_t errorsize )
|
2003-03-20 20:26:18 +01:00
|
|
|
{
|
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
void *ret;
|
|
|
|
const char *s;
|
2007-11-29 15:32:49 +01:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
/* the Mac OS loader pretends to be able to load PE files, so avoid them here */
|
|
|
|
unsigned char magic[2];
|
|
|
|
int fd = open( filename, O_RDONLY );
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
if (pread( fd, magic, 2, 0 ) == 2 && magic[0] == 'M' && magic[1] == 'Z')
|
|
|
|
{
|
2013-10-21 06:55:47 +02:00
|
|
|
if (error && errorsize)
|
|
|
|
{
|
|
|
|
static const char msg[] = "MZ format";
|
|
|
|
size_t len = min( errorsize, sizeof(msg) );
|
|
|
|
memcpy( error, msg, len );
|
|
|
|
error[len - 1] = 0;
|
|
|
|
}
|
2007-11-29 15:32:49 +01:00
|
|
|
close( fd );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
#endif
|
2003-03-20 20:26:18 +01:00
|
|
|
dlerror(); dlerror();
|
2007-03-30 12:17:01 +02:00
|
|
|
#ifdef __sun
|
|
|
|
if (strchr( filename, ':' ))
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
/* Solaris' brain damaged dlopen() treats ':' as a path separator */
|
|
|
|
realpath( filename, path );
|
|
|
|
ret = dlopen( path, flag | RTLD_FIRST );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2005-05-10 17:13:20 +02:00
|
|
|
ret = dlopen( filename, flag | RTLD_FIRST );
|
2003-03-20 20:26:18 +01:00
|
|
|
s = dlerror();
|
2005-04-25 17:51:45 +02:00
|
|
|
if (error && errorsize)
|
2003-03-20 20:26:18 +01:00
|
|
|
{
|
2005-04-25 17:51:45 +02:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
size_t len = strlen(s);
|
|
|
|
if (len >= errorsize) len = errorsize - 1;
|
|
|
|
memcpy( error, s, len );
|
|
|
|
error[len] = 0;
|
|
|
|
}
|
|
|
|
else error[0] = 0;
|
2003-03-20 20:26:18 +01:00
|
|
|
}
|
|
|
|
dlerror();
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
if (error)
|
|
|
|
{
|
2005-04-25 17:51:45 +02:00
|
|
|
static const char msg[] = "dlopen interface not detected by configure";
|
|
|
|
size_t len = min( errorsize, sizeof(msg) );
|
|
|
|
memcpy( error, msg, len );
|
|
|
|
error[len - 1] = 0;
|
2003-03-20 20:26:18 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* wine_dlsym
|
|
|
|
*/
|
2005-04-25 17:51:45 +02:00
|
|
|
void *wine_dlsym( void *handle, const char *symbol, char *error, size_t errorsize )
|
2003-03-20 20:26:18 +01:00
|
|
|
{
|
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
void *ret;
|
|
|
|
const char *s;
|
|
|
|
dlerror(); dlerror();
|
|
|
|
ret = dlsym( handle, symbol );
|
|
|
|
s = dlerror();
|
2005-04-25 17:51:45 +02:00
|
|
|
if (error && errorsize)
|
2003-03-20 20:26:18 +01:00
|
|
|
{
|
2005-04-25 17:51:45 +02:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
size_t len = strlen(s);
|
|
|
|
if (len >= errorsize) len = errorsize - 1;
|
|
|
|
memcpy( error, s, len );
|
|
|
|
error[len] = 0;
|
|
|
|
}
|
|
|
|
else error[0] = 0;
|
2003-03-20 20:26:18 +01:00
|
|
|
}
|
|
|
|
dlerror();
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
if (error)
|
|
|
|
{
|
2005-04-25 17:51:45 +02:00
|
|
|
static const char msg[] = "dlopen interface not detected by configure";
|
|
|
|
size_t len = min( errorsize, sizeof(msg) );
|
|
|
|
memcpy( error, msg, len );
|
|
|
|
error[len - 1] = 0;
|
2003-03-20 20:26:18 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* wine_dlclose
|
|
|
|
*/
|
2005-04-25 17:51:45 +02:00
|
|
|
int wine_dlclose( void *handle, char *error, size_t errorsize )
|
2003-03-20 20:26:18 +01:00
|
|
|
{
|
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
int ret;
|
|
|
|
const char *s;
|
|
|
|
dlerror(); dlerror();
|
|
|
|
ret = dlclose( handle );
|
|
|
|
s = dlerror();
|
2005-04-25 17:51:45 +02:00
|
|
|
if (error && errorsize)
|
2003-03-20 20:26:18 +01:00
|
|
|
{
|
2005-04-25 17:51:45 +02:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
size_t len = strlen(s);
|
|
|
|
if (len >= errorsize) len = errorsize - 1;
|
|
|
|
memcpy( error, s, len );
|
|
|
|
error[len] = 0;
|
|
|
|
}
|
|
|
|
else error[0] = 0;
|
2003-03-20 20:26:18 +01:00
|
|
|
}
|
|
|
|
dlerror();
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
if (error)
|
|
|
|
{
|
2005-04-25 17:51:45 +02:00
|
|
|
static const char msg[] = "dlopen interface not detected by configure";
|
|
|
|
size_t len = min( errorsize, sizeof(msg) );
|
|
|
|
memcpy( error, msg, len );
|
|
|
|
error[len - 1] = 0;
|
2003-03-20 20:26:18 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|