2002-03-10 00:29:33 +01:00
|
|
|
/*
|
2003-03-20 05:55:59 +01:00
|
|
|
* Loader functions
|
|
|
|
*
|
2003-04-05 07:16:11 +02:00
|
|
|
* Copyright 1995, 2003 Alexandre Julliard
|
2003-09-08 21:38:45 +02:00
|
|
|
* Copyright 2002 Dmitry Timoshkov for CodeWeavers
|
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
|
2002-03-10 00:29:33 +01:00
|
|
|
*/
|
|
|
|
|
2003-11-27 01:59:36 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2003-03-18 19:29:13 +01:00
|
|
|
#include <assert.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2003-03-18 19:29:13 +01:00
|
|
|
|
2005-03-17 20:00:08 +01:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2002-01-29 19:30:16 +01:00
|
|
|
#include "winnt.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-06-10 04:28:00 +02:00
|
|
|
|
2002-08-16 00:09:53 +02:00
|
|
|
#include "wine/exception.h"
|
2002-12-13 00:34:01 +01:00
|
|
|
#include "excpt.h"
|
2005-05-18 20:27:54 +02:00
|
|
|
#include "wine/library.h"
|
2003-09-26 01:55:35 +02:00
|
|
|
#include "wine/unicode.h"
|
2002-06-10 04:28:00 +02:00
|
|
|
#include "wine/debug.h"
|
2003-03-14 05:00:52 +01:00
|
|
|
#include "wine/server.h"
|
|
|
|
#include "ntdll_misc.h"
|
2002-06-10 04:28:00 +02:00
|
|
|
|
2003-04-04 21:50:17 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(module);
|
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(relay);
|
2003-04-05 07:16:11 +02:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(snoop);
|
2003-03-14 05:00:52 +01:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(loaddll);
|
2005-05-18 11:51:46 +02:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(imports);
|
2003-03-14 05:00:52 +01:00
|
|
|
|
2003-04-04 21:50:17 +02:00
|
|
|
typedef DWORD (CALLBACK *DLLENTRYPROC)(HMODULE,DWORD,LPVOID);
|
|
|
|
|
2003-03-20 05:55:59 +01:00
|
|
|
static int process_detaching = 0; /* set on process detach to avoid deadlocks with thread detach */
|
2003-03-18 19:29:13 +01:00
|
|
|
static int free_lib_count; /* recursion depth of LdrUnloadDll calls */
|
2002-01-29 19:30:16 +01:00
|
|
|
|
2003-05-08 06:13:26 +02:00
|
|
|
static const char * const reason_names[] =
|
|
|
|
{
|
|
|
|
"PROCESS_DETACH",
|
|
|
|
"PROCESS_ATTACH",
|
|
|
|
"THREAD_ATTACH",
|
2006-03-21 15:10:22 +01:00
|
|
|
"THREAD_DETACH",
|
|
|
|
NULL, NULL, NULL, NULL,
|
|
|
|
"WINE_PREATTACH"
|
2003-05-08 06:13:26 +02:00
|
|
|
};
|
|
|
|
|
2003-09-26 01:55:35 +02:00
|
|
|
static const WCHAR dllW[] = {'.','d','l','l',0};
|
|
|
|
|
|
|
|
/* internal representation of 32bit modules. per process. */
|
2003-10-06 23:18:51 +02:00
|
|
|
typedef struct _wine_modref
|
2003-09-26 01:55:35 +02:00
|
|
|
{
|
|
|
|
LDR_MODULE ldr;
|
|
|
|
int nDeps;
|
|
|
|
struct _wine_modref **deps;
|
2003-10-06 23:18:51 +02:00
|
|
|
} WINE_MODREF;
|
2003-09-26 01:55:35 +02:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
/* info about the current builtin dll load */
|
|
|
|
/* used to keep track of things across the register_dll constructor call */
|
|
|
|
struct builtin_load_info
|
|
|
|
{
|
|
|
|
const WCHAR *load_path;
|
2006-02-07 21:16:25 +01:00
|
|
|
const WCHAR *filename;
|
2003-10-08 00:56:34 +02:00
|
|
|
NTSTATUS status;
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
};
|
|
|
|
|
2003-10-10 02:12:17 +02:00
|
|
|
static struct builtin_load_info default_load_info;
|
|
|
|
static struct builtin_load_info *builtin_load_info = &default_load_info;
|
2003-10-08 00:56:34 +02:00
|
|
|
|
2006-02-22 09:57:19 +01:00
|
|
|
static HANDLE main_exe_file;
|
2003-05-14 21:51:14 +02:00
|
|
|
static UINT tls_module_count; /* number of modules with TLS directory */
|
|
|
|
static UINT tls_total_size; /* total size of TLS storage */
|
|
|
|
static const IMAGE_TLS_DIRECTORY **tls_dirs; /* array of TLS directories */
|
|
|
|
|
2004-07-16 00:07:21 +02:00
|
|
|
UNICODE_STRING system_dir = { 0, 0, NULL }; /* system directory */
|
2003-10-08 00:56:34 +02:00
|
|
|
|
2005-06-25 20:00:57 +02:00
|
|
|
static RTL_CRITICAL_SECTION loader_section;
|
|
|
|
static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
|
2003-08-13 01:50:54 +02:00
|
|
|
{
|
|
|
|
0, 0, &loader_section,
|
|
|
|
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
|
2005-09-09 12:19:44 +02:00
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": loader_section") }
|
2003-08-13 01:50:54 +02:00
|
|
|
};
|
2005-06-25 20:00:57 +02:00
|
|
|
static RTL_CRITICAL_SECTION loader_section = { &critsect_debug, -1, 0, 0, 0, 0 };
|
2003-08-13 01:50:54 +02:00
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
static WINE_MODREF *cached_modref;
|
2003-04-08 01:27:54 +02:00
|
|
|
static WINE_MODREF *current_modref;
|
2005-01-11 17:01:31 +01:00
|
|
|
static WINE_MODREF *last_failed_modref;
|
2003-04-05 07:16:11 +02:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
static NTSTATUS load_dll( LPCWSTR load_path, LPCWSTR libname, DWORD flags, WINE_MODREF** pwm );
|
2004-06-15 02:47:00 +02:00
|
|
|
static FARPROC find_named_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
|
2003-04-05 07:16:11 +02:00
|
|
|
DWORD exp_size, const char *name, int hint );
|
|
|
|
|
|
|
|
/* convert PE image VirtualAddress to Real Address */
|
|
|
|
inline static void *get_rva( HMODULE module, DWORD va )
|
|
|
|
{
|
|
|
|
return (void *)((char *)module + va);
|
|
|
|
}
|
2003-03-18 19:29:13 +01:00
|
|
|
|
2003-09-29 22:42:24 +02:00
|
|
|
/* check whether the file name contains a path */
|
|
|
|
inline static int contains_path( LPCWSTR name )
|
|
|
|
{
|
|
|
|
return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert from straight ASCII to Unicode without depending on the current codepage */
|
|
|
|
inline static void ascii_to_unicode( WCHAR *dst, const char *src, size_t len )
|
|
|
|
{
|
|
|
|
while (len--) *dst++ = (unsigned char)*src++;
|
|
|
|
}
|
|
|
|
|
2004-03-19 03:07:16 +01:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* call_dll_entry_point
|
|
|
|
*
|
|
|
|
* Some brain-damaged dlls (ir32_32.dll for instance) modify ebx in
|
|
|
|
* their entry point, so we need a small asm wrapper.
|
|
|
|
*/
|
|
|
|
#ifdef __i386__
|
|
|
|
extern BOOL call_dll_entry_point( DLLENTRYPROC proc, void *module, UINT reason, void *reserved );
|
|
|
|
__ASM_GLOBAL_FUNC(call_dll_entry_point,
|
|
|
|
"pushl %ebp\n\t"
|
|
|
|
"movl %esp,%ebp\n\t"
|
|
|
|
"pushl %ebx\n\t"
|
2005-11-23 19:55:06 +01:00
|
|
|
"subl $8,%esp\n\t"
|
2004-03-19 03:07:16 +01:00
|
|
|
"pushl 20(%ebp)\n\t"
|
|
|
|
"pushl 16(%ebp)\n\t"
|
|
|
|
"pushl 12(%ebp)\n\t"
|
|
|
|
"movl 8(%ebp),%eax\n\t"
|
|
|
|
"call *%eax\n\t"
|
|
|
|
"leal -4(%ebp),%esp\n\t"
|
|
|
|
"popl %ebx\n\t"
|
|
|
|
"popl %ebp\n\t"
|
|
|
|
"ret" );
|
|
|
|
#else /* __i386__ */
|
|
|
|
static inline BOOL call_dll_entry_point( DLLENTRYPROC proc, void *module,
|
|
|
|
UINT reason, void *reserved )
|
|
|
|
{
|
|
|
|
return proc( module, reason, reserved );
|
|
|
|
}
|
|
|
|
#endif /* __i386__ */
|
|
|
|
|
|
|
|
|
2004-12-14 21:03:23 +01:00
|
|
|
#ifdef __i386__
|
|
|
|
/*************************************************************************
|
|
|
|
* stub_entry_point
|
|
|
|
*
|
|
|
|
* Entry point for stub functions.
|
|
|
|
*/
|
|
|
|
static void stub_entry_point( const char *dll, const char *name, ... )
|
|
|
|
{
|
|
|
|
EXCEPTION_RECORD rec;
|
|
|
|
|
|
|
|
rec.ExceptionCode = EXCEPTION_WINE_STUB;
|
|
|
|
rec.ExceptionFlags = EH_NONCONTINUABLE;
|
|
|
|
rec.ExceptionRecord = NULL;
|
|
|
|
#ifdef __GNUC__
|
|
|
|
rec.ExceptionAddress = __builtin_return_address(0);
|
|
|
|
#else
|
|
|
|
rec.ExceptionAddress = *((void **)&dll - 1);
|
|
|
|
#endif
|
|
|
|
rec.NumberParameters = 2;
|
2005-02-24 14:15:36 +01:00
|
|
|
rec.ExceptionInformation[0] = (ULONG_PTR)dll;
|
|
|
|
rec.ExceptionInformation[1] = (ULONG_PTR)name;
|
2004-12-14 21:03:23 +01:00
|
|
|
for (;;) RtlRaiseException( &rec );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-21 11:52:26 +01:00
|
|
|
#include "pshpack1.h"
|
2004-12-14 21:03:23 +01:00
|
|
|
struct stub
|
|
|
|
{
|
|
|
|
BYTE popl_eax; /* popl %eax */
|
|
|
|
BYTE pushl1; /* pushl $name */
|
|
|
|
const char *name;
|
|
|
|
BYTE pushl2; /* pushl $dll */
|
|
|
|
const char *dll;
|
|
|
|
BYTE pushl_eax; /* pushl %eax */
|
|
|
|
BYTE jmp; /* jmp stub_entry_point */
|
|
|
|
DWORD entry;
|
|
|
|
};
|
2005-03-21 11:52:26 +01:00
|
|
|
#include "poppack.h"
|
2004-12-14 21:03:23 +01:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* allocate_stub
|
|
|
|
*
|
|
|
|
* Allocate a stub entry point.
|
|
|
|
*/
|
2005-07-19 13:44:32 +02:00
|
|
|
static ULONG_PTR allocate_stub( const char *dll, const char *name )
|
2004-12-14 21:03:23 +01:00
|
|
|
{
|
|
|
|
#define MAX_SIZE 65536
|
|
|
|
static struct stub *stubs;
|
|
|
|
static unsigned int nb_stubs;
|
|
|
|
struct stub *stub;
|
|
|
|
|
2005-07-19 13:44:32 +02:00
|
|
|
if (nb_stubs >= MAX_SIZE / sizeof(*stub)) return 0xdeadbeef;
|
2004-12-14 21:03:23 +01:00
|
|
|
|
|
|
|
if (!stubs)
|
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
SIZE_T size = MAX_SIZE;
|
2005-02-22 20:33:50 +01:00
|
|
|
if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&stubs, 0, &size,
|
2004-12-14 21:03:23 +01:00
|
|
|
MEM_COMMIT, PAGE_EXECUTE_WRITECOPY ) != STATUS_SUCCESS)
|
2005-07-19 13:44:32 +02:00
|
|
|
return 0xdeadbeef;
|
2004-12-14 21:03:23 +01:00
|
|
|
}
|
|
|
|
stub = &stubs[nb_stubs++];
|
|
|
|
stub->popl_eax = 0x58; /* popl %eax */
|
|
|
|
stub->pushl1 = 0x68; /* pushl $name */
|
|
|
|
stub->name = name;
|
|
|
|
stub->pushl2 = 0x68; /* pushl $dll */
|
|
|
|
stub->dll = dll;
|
|
|
|
stub->pushl_eax = 0x50; /* pushl %eax */
|
|
|
|
stub->jmp = 0xe9; /* jmp stub_entry_point */
|
|
|
|
stub->entry = (BYTE *)stub_entry_point - (BYTE *)(&stub->entry + 1);
|
2005-07-19 13:44:32 +02:00
|
|
|
return (ULONG_PTR)stub;
|
2004-12-14 21:03:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* __i386__ */
|
2005-07-19 13:44:32 +02:00
|
|
|
static inline ULONG_PTR allocate_stub( const char *dll, const char *name ) { return 0xdeadbeef; }
|
2004-12-14 21:03:23 +01:00
|
|
|
#endif /* __i386__ */
|
|
|
|
|
|
|
|
|
2003-03-18 19:29:13 +01:00
|
|
|
/*************************************************************************
|
2003-04-05 07:16:11 +02:00
|
|
|
* get_modref
|
|
|
|
*
|
|
|
|
* Looks for the referenced HMODULE in the current process
|
|
|
|
* The loader_section must be locked while calling this function.
|
2003-03-18 19:29:13 +01:00
|
|
|
*/
|
2003-04-05 07:16:11 +02:00
|
|
|
static WINE_MODREF *get_modref( HMODULE hmod )
|
2003-03-18 19:29:13 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
PLDR_MODULE mod;
|
2003-03-18 19:29:13 +01:00
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
if (cached_modref && cached_modref->ldr.BaseAddress == hmod) return cached_modref;
|
2003-03-18 19:29:13 +01:00
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
2003-04-05 07:16:11 +02:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
|
|
|
|
if (mod->BaseAddress == hmod)
|
|
|
|
return cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
|
|
|
|
if (mod->BaseAddress > (void*)hmod) break;
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
2003-07-01 05:38:56 +02:00
|
|
|
return NULL;
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-19 06:04:57 +02:00
|
|
|
/**********************************************************************
|
2003-10-08 00:56:34 +02:00
|
|
|
* find_basename_module
|
2003-09-19 06:04:57 +02:00
|
|
|
*
|
2003-10-08 00:56:34 +02:00
|
|
|
* Find a module from its base name.
|
2003-09-19 06:04:57 +02:00
|
|
|
* The loader_section must be locked while calling this function
|
|
|
|
*/
|
2003-10-08 00:56:34 +02:00
|
|
|
static WINE_MODREF *find_basename_module( LPCWSTR name )
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
if (cached_modref && !strcmpiW( name, cached_modref->ldr.BaseDllName.Buffer ))
|
|
|
|
return cached_modref;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
2003-10-08 00:56:34 +02:00
|
|
|
LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
if (!strcmpiW( name, mod->BaseDllName.Buffer ))
|
|
|
|
{
|
|
|
|
cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
|
|
|
|
return cached_modref;
|
|
|
|
}
|
2003-09-19 06:04:57 +02:00
|
|
|
}
|
2003-10-08 00:56:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* find_fullname_module
|
|
|
|
*
|
|
|
|
* Find a module from its full path name.
|
|
|
|
* The loader_section must be locked while calling this function
|
|
|
|
*/
|
|
|
|
static WINE_MODREF *find_fullname_module( LPCWSTR name )
|
|
|
|
{
|
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
|
|
|
|
if (cached_modref && !strcmpiW( name, cached_modref->ldr.FullDllName.Buffer ))
|
|
|
|
return cached_modref;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
{
|
2003-10-08 00:56:34 +02:00
|
|
|
LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
if (!strcmpiW( name, mod->FullDllName.Buffer ))
|
|
|
|
{
|
|
|
|
cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
|
|
|
|
return cached_modref;
|
|
|
|
}
|
2003-09-19 06:04:57 +02:00
|
|
|
}
|
2003-10-08 00:56:34 +02:00
|
|
|
return NULL;
|
2003-09-19 06:04:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* find_forwarded_export
|
|
|
|
*
|
|
|
|
* Find the final function pointer for a forwarded function.
|
|
|
|
* The loader_section must be locked while calling this function.
|
|
|
|
*/
|
|
|
|
static FARPROC find_forwarded_export( HMODULE module, const char *forward )
|
|
|
|
{
|
2004-06-15 02:47:00 +02:00
|
|
|
const IMAGE_EXPORT_DIRECTORY *exports;
|
2003-04-05 07:16:11 +02:00
|
|
|
DWORD exp_size;
|
|
|
|
WINE_MODREF *wm;
|
2003-10-08 00:56:34 +02:00
|
|
|
WCHAR mod_name[32];
|
2006-12-04 13:37:32 +01:00
|
|
|
const char *end = strrchr(forward, '.');
|
2003-04-05 07:16:11 +02:00
|
|
|
FARPROC proc = NULL;
|
|
|
|
|
|
|
|
if (!end) return NULL;
|
2006-12-04 13:37:32 +01:00
|
|
|
if ((end - forward) * sizeof(WCHAR) >= sizeof(mod_name)) return NULL;
|
2003-09-29 22:42:24 +02:00
|
|
|
ascii_to_unicode( mod_name, forward, end - forward );
|
2006-12-04 13:37:32 +01:00
|
|
|
mod_name[end - forward] = 0;
|
|
|
|
if (!strchrW( mod_name, '.' ))
|
|
|
|
{
|
|
|
|
if ((end - forward) * sizeof(WCHAR) >= sizeof(mod_name) - sizeof(dllW)) return NULL;
|
|
|
|
memcpy( mod_name + (end - forward), dllW, sizeof(dllW) );
|
|
|
|
}
|
2003-04-05 07:16:11 +02:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
if (!(wm = find_basename_module( mod_name )))
|
2003-04-05 07:16:11 +02:00
|
|
|
{
|
2003-09-29 22:42:24 +02:00
|
|
|
ERR("module not found for forward '%s' used by %s\n",
|
|
|
|
forward, debugstr_w(get_modref(module)->ldr.FullDllName.Buffer) );
|
2003-04-05 07:16:11 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((exports = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
|
|
|
|
IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
|
|
|
|
proc = find_named_export( wm->ldr.BaseAddress, exports, exp_size, end + 1, -1 );
|
|
|
|
|
|
|
|
if (!proc)
|
|
|
|
{
|
2003-09-29 22:42:24 +02:00
|
|
|
ERR("function not found for forward '%s' used by %s."
|
|
|
|
" If you are using builtin %s, try using the native one instead.\n",
|
|
|
|
forward, debugstr_w(get_modref(module)->ldr.FullDllName.Buffer),
|
|
|
|
debugstr_w(get_modref(module)->ldr.BaseDllName.Buffer) );
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* find_ordinal_export
|
|
|
|
*
|
|
|
|
* Find an exported function by ordinal.
|
|
|
|
* The exports base must have been subtracted from the ordinal already.
|
|
|
|
* The loader_section must be locked while calling this function.
|
|
|
|
*/
|
2004-06-15 02:47:00 +02:00
|
|
|
static FARPROC find_ordinal_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
|
2005-12-18 10:45:01 +01:00
|
|
|
DWORD exp_size, DWORD ordinal )
|
2003-04-05 07:16:11 +02:00
|
|
|
{
|
|
|
|
FARPROC proc;
|
2004-06-15 02:47:00 +02:00
|
|
|
const DWORD *functions = get_rva( module, exports->AddressOfFunctions );
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
if (ordinal >= exports->NumberOfFunctions)
|
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE(" ordinal %d out of range!\n", ordinal + exports->Base );
|
2003-04-05 07:16:11 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!functions[ordinal]) return NULL;
|
|
|
|
|
|
|
|
proc = get_rva( module, functions[ordinal] );
|
|
|
|
|
|
|
|
/* if the address falls into the export dir, it's a forward */
|
2004-06-15 02:47:00 +02:00
|
|
|
if (((const char *)proc >= (const char *)exports) &&
|
|
|
|
((const char *)proc < (const char *)exports + exp_size))
|
|
|
|
return find_forwarded_export( module, (const char *)proc );
|
2003-04-05 07:16:11 +02:00
|
|
|
|
2004-08-03 00:25:01 +02:00
|
|
|
if (TRACE_ON(snoop))
|
2003-04-05 07:16:11 +02:00
|
|
|
{
|
2004-08-03 00:25:01 +02:00
|
|
|
const WCHAR *user = current_modref ? current_modref->ldr.BaseDllName.Buffer : NULL;
|
|
|
|
proc = SNOOP_GetProcAddress( module, exports, exp_size, proc, ordinal, user );
|
2003-04-08 01:27:54 +02:00
|
|
|
}
|
2004-08-03 00:25:01 +02:00
|
|
|
if (TRACE_ON(relay))
|
2003-04-08 01:27:54 +02:00
|
|
|
{
|
2004-08-03 00:25:01 +02:00
|
|
|
const WCHAR *user = current_modref ? current_modref->ldr.BaseDllName.Buffer : NULL;
|
2005-12-18 10:45:01 +01:00
|
|
|
proc = RELAY_GetProcAddress( module, exports, exp_size, proc, ordinal, user );
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* find_named_export
|
|
|
|
*
|
|
|
|
* Find an exported function by name.
|
|
|
|
* The loader_section must be locked while calling this function.
|
|
|
|
*/
|
2004-06-15 02:47:00 +02:00
|
|
|
static FARPROC find_named_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
|
2003-04-05 07:16:11 +02:00
|
|
|
DWORD exp_size, const char *name, int hint )
|
|
|
|
{
|
2004-06-15 02:47:00 +02:00
|
|
|
const WORD *ordinals = get_rva( module, exports->AddressOfNameOrdinals );
|
|
|
|
const DWORD *names = get_rva( module, exports->AddressOfNames );
|
2003-04-05 07:16:11 +02:00
|
|
|
int min = 0, max = exports->NumberOfNames - 1;
|
|
|
|
|
|
|
|
/* first check the hint */
|
|
|
|
if (hint >= 0 && hint <= max)
|
|
|
|
{
|
|
|
|
char *ename = get_rva( module, names[hint] );
|
|
|
|
if (!strcmp( ename, name ))
|
|
|
|
return find_ordinal_export( module, exports, exp_size, ordinals[hint] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* then do a binary search */
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
int res, pos = (min + max) / 2;
|
|
|
|
char *ename = get_rva( module, names[pos] );
|
|
|
|
if (!(res = strcmp( ename, name )))
|
|
|
|
return find_ordinal_export( module, exports, exp_size, ordinals[pos] );
|
|
|
|
if (res > 0) max = pos - 1;
|
|
|
|
else min = pos + 1;
|
|
|
|
}
|
2003-03-18 19:29:13 +01:00
|
|
|
return NULL;
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* import_dll
|
|
|
|
*
|
|
|
|
* Import the dll specified by the given import descriptor.
|
|
|
|
* The loader_section must be locked while calling this function.
|
|
|
|
*/
|
2004-06-15 02:47:00 +02:00
|
|
|
static WINE_MODREF *import_dll( HMODULE module, const IMAGE_IMPORT_DESCRIPTOR *descr, LPCWSTR load_path )
|
2003-04-05 07:16:11 +02:00
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
WINE_MODREF *wmImp;
|
|
|
|
HMODULE imp_mod;
|
2004-06-15 02:47:00 +02:00
|
|
|
const IMAGE_EXPORT_DIRECTORY *exports;
|
2003-04-05 07:16:11 +02:00
|
|
|
DWORD exp_size;
|
2004-06-15 02:47:00 +02:00
|
|
|
const IMAGE_THUNK_DATA *import_list;
|
|
|
|
IMAGE_THUNK_DATA *thunk_list;
|
2003-09-29 22:42:24 +02:00
|
|
|
WCHAR buffer[32];
|
2004-06-15 02:47:00 +02:00
|
|
|
const char *name = get_rva( module, descr->Name );
|
2006-02-20 14:08:26 +01:00
|
|
|
DWORD len = strlen(name);
|
2004-06-22 04:07:47 +02:00
|
|
|
PVOID protect_base;
|
2005-09-12 12:36:06 +02:00
|
|
|
SIZE_T protect_size = 0;
|
2004-06-22 04:07:47 +02:00
|
|
|
DWORD protect_old;
|
2003-09-29 22:42:24 +02:00
|
|
|
|
2003-10-11 01:28:51 +02:00
|
|
|
thunk_list = get_rva( module, (DWORD)descr->FirstThunk );
|
|
|
|
if (descr->u.OriginalFirstThunk)
|
|
|
|
import_list = get_rva( module, (DWORD)descr->u.OriginalFirstThunk );
|
|
|
|
else
|
|
|
|
import_list = thunk_list;
|
|
|
|
|
2006-02-20 14:08:26 +01:00
|
|
|
while (len && name[len-1] == ' ') len--; /* remove trailing spaces */
|
|
|
|
|
|
|
|
if (len * sizeof(WCHAR) < sizeof(buffer))
|
2003-09-29 22:42:24 +02:00
|
|
|
{
|
|
|
|
ascii_to_unicode( buffer, name, len );
|
2006-02-20 14:08:26 +01:00
|
|
|
buffer[len] = 0;
|
2003-10-08 00:56:34 +02:00
|
|
|
status = load_dll( load_path, buffer, 0, &wmImp );
|
2003-09-29 22:42:24 +02:00
|
|
|
}
|
|
|
|
else /* need to allocate a larger buffer */
|
|
|
|
{
|
2006-02-20 14:08:26 +01:00
|
|
|
WCHAR *ptr = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
|
2003-09-29 22:42:24 +02:00
|
|
|
if (!ptr) return NULL;
|
|
|
|
ascii_to_unicode( ptr, name, len );
|
2006-02-20 14:08:26 +01:00
|
|
|
ptr[len] = 0;
|
2003-10-08 00:56:34 +02:00
|
|
|
status = load_dll( load_path, ptr, 0, &wmImp );
|
2003-09-29 22:42:24 +02:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, ptr );
|
|
|
|
}
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
2003-09-29 22:42:24 +02:00
|
|
|
if (status == STATUS_DLL_NOT_FOUND)
|
2004-02-24 02:25:03 +01:00
|
|
|
ERR("Library %s (which is needed by %s) not found\n",
|
2003-09-29 22:42:24 +02:00
|
|
|
name, debugstr_w(current_modref->ldr.FullDllName.Buffer));
|
2003-04-05 07:16:11 +02:00
|
|
|
else
|
2006-10-16 13:49:06 +02:00
|
|
|
ERR("Loading library %s (which is needed by %s) failed (error %x).\n",
|
2003-09-29 22:42:24 +02:00
|
|
|
name, debugstr_w(current_modref->ldr.FullDllName.Buffer), status);
|
2004-02-24 02:25:03 +01:00
|
|
|
return NULL;
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
|
|
|
|
2004-06-22 04:07:47 +02:00
|
|
|
/* unprotect the import address table since it can be located in
|
|
|
|
* readonly section */
|
|
|
|
while (import_list[protect_size].u1.Ordinal) protect_size++;
|
|
|
|
protect_base = thunk_list;
|
|
|
|
protect_size *= sizeof(*thunk_list);
|
2005-02-22 20:33:50 +01:00
|
|
|
NtProtectVirtualMemory( NtCurrentProcess(), &protect_base,
|
2004-06-22 04:07:47 +02:00
|
|
|
&protect_size, PAGE_WRITECOPY, &protect_old );
|
|
|
|
|
2004-02-24 02:25:03 +01:00
|
|
|
imp_mod = wmImp->ldr.BaseAddress;
|
|
|
|
exports = RtlImageDirectoryEntryToData( imp_mod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size );
|
|
|
|
|
2003-10-11 01:28:51 +02:00
|
|
|
if (!exports)
|
|
|
|
{
|
|
|
|
/* set all imported function to deadbeef */
|
|
|
|
while (import_list->u1.Ordinal)
|
|
|
|
{
|
|
|
|
if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal))
|
|
|
|
{
|
2004-12-14 21:03:23 +01:00
|
|
|
int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
|
|
|
|
WARN("No implementation for %s.%d", name, ordinal );
|
|
|
|
thunk_list->u1.Function = allocate_stub( name, (const char *)ordinal );
|
2003-10-11 01:28:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IMAGE_IMPORT_BY_NAME *pe_name = get_rva( module, (DWORD)import_list->u1.AddressOfData );
|
2004-12-14 21:03:23 +01:00
|
|
|
WARN("No implementation for %s.%s", name, pe_name->Name );
|
2005-09-02 16:47:36 +02:00
|
|
|
thunk_list->u1.Function = allocate_stub( name, (const char*)pe_name->Name );
|
2003-10-11 01:28:51 +02:00
|
|
|
}
|
2004-12-14 21:03:23 +01:00
|
|
|
WARN(" imported from %s, allocating stub %p\n",
|
2005-07-19 13:44:32 +02:00
|
|
|
debugstr_w(current_modref->ldr.FullDllName.Buffer),
|
|
|
|
(void *)thunk_list->u1.Function );
|
2003-10-11 01:28:51 +02:00
|
|
|
import_list++;
|
|
|
|
thunk_list++;
|
|
|
|
}
|
2004-06-22 04:07:47 +02:00
|
|
|
goto done;
|
2003-10-11 01:28:51 +02:00
|
|
|
}
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
while (import_list->u1.Ordinal)
|
|
|
|
{
|
|
|
|
if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal))
|
|
|
|
{
|
|
|
|
int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
|
|
|
|
|
2005-07-19 13:44:32 +02:00
|
|
|
thunk_list->u1.Function = (ULONG_PTR)find_ordinal_export( imp_mod, exports, exp_size,
|
|
|
|
ordinal - exports->Base );
|
2003-04-05 07:16:11 +02:00
|
|
|
if (!thunk_list->u1.Function)
|
|
|
|
{
|
2004-12-14 21:03:23 +01:00
|
|
|
thunk_list->u1.Function = allocate_stub( name, (const char *)ordinal );
|
|
|
|
WARN("No implementation for %s.%d imported from %s, setting to %p\n",
|
2005-07-19 13:44:32 +02:00
|
|
|
name, ordinal, debugstr_w(current_modref->ldr.FullDllName.Buffer),
|
|
|
|
(void *)thunk_list->u1.Function );
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
2005-07-19 13:44:32 +02:00
|
|
|
TRACE_(imports)("--- Ordinal %s.%d = %p\n", name, ordinal, (void *)thunk_list->u1.Function );
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
|
|
|
else /* import by name */
|
|
|
|
{
|
|
|
|
IMAGE_IMPORT_BY_NAME *pe_name;
|
|
|
|
pe_name = get_rva( module, (DWORD)import_list->u1.AddressOfData );
|
2005-07-19 13:44:32 +02:00
|
|
|
thunk_list->u1.Function = (ULONG_PTR)find_named_export( imp_mod, exports, exp_size,
|
2005-09-02 16:47:36 +02:00
|
|
|
(const char*)pe_name->Name, pe_name->Hint );
|
2003-04-05 07:16:11 +02:00
|
|
|
if (!thunk_list->u1.Function)
|
|
|
|
{
|
2005-09-02 16:47:36 +02:00
|
|
|
thunk_list->u1.Function = allocate_stub( name, (const char*)pe_name->Name );
|
2004-12-14 21:03:23 +01:00
|
|
|
WARN("No implementation for %s.%s imported from %s, setting to %p\n",
|
2005-07-19 13:44:32 +02:00
|
|
|
name, pe_name->Name, debugstr_w(current_modref->ldr.FullDllName.Buffer),
|
|
|
|
(void *)thunk_list->u1.Function );
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
2005-07-19 13:44:32 +02:00
|
|
|
TRACE_(imports)("--- %s %s.%d = %p\n",
|
|
|
|
pe_name->Name, name, pe_name->Hint, (void *)thunk_list->u1.Function);
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
|
|
|
import_list++;
|
|
|
|
thunk_list++;
|
|
|
|
}
|
2004-06-22 04:07:47 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
/* restore old protection of the import address table */
|
2005-02-22 20:33:50 +01:00
|
|
|
NtProtectVirtualMemory( NtCurrentProcess(), &protect_base, &protect_size, protect_old, NULL );
|
2003-04-05 07:16:11 +02:00
|
|
|
return wmImp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
2003-09-19 06:04:57 +02:00
|
|
|
* fixup_imports
|
2003-04-05 07:16:11 +02:00
|
|
|
*
|
|
|
|
* Fixup all imports of a given module.
|
|
|
|
* The loader_section must be locked while calling this function.
|
|
|
|
*/
|
2003-10-08 00:56:34 +02:00
|
|
|
static NTSTATUS fixup_imports( WINE_MODREF *wm, LPCWSTR load_path )
|
2003-04-05 07:16:11 +02:00
|
|
|
{
|
|
|
|
int i, nb_imports;
|
2004-06-15 02:47:00 +02:00
|
|
|
const IMAGE_IMPORT_DESCRIPTOR *imports;
|
2003-04-08 01:27:54 +02:00
|
|
|
WINE_MODREF *prev;
|
2003-04-05 07:16:11 +02:00
|
|
|
DWORD size;
|
2003-10-11 01:28:51 +02:00
|
|
|
NTSTATUS status;
|
2003-04-05 07:16:11 +02:00
|
|
|
|
2006-02-21 19:32:26 +01:00
|
|
|
if (!(wm->ldr.Flags & LDR_DONT_RESOLVE_REFS)) return STATUS_SUCCESS; /* already done */
|
|
|
|
wm->ldr.Flags &= ~LDR_DONT_RESOLVE_REFS;
|
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
if (!(imports = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
|
|
|
|
IMAGE_DIRECTORY_ENTRY_IMPORT, &size )))
|
2003-09-19 06:04:57 +02:00
|
|
|
return STATUS_SUCCESS;
|
2003-04-05 07:16:11 +02:00
|
|
|
|
2005-10-04 17:54:55 +02:00
|
|
|
nb_imports = 0;
|
|
|
|
while (imports[nb_imports].Name && imports[nb_imports].FirstThunk) nb_imports++;
|
|
|
|
|
2003-09-19 06:04:57 +02:00
|
|
|
if (!nb_imports) return STATUS_SUCCESS; /* no imports */
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
/* Allocate module dependency list */
|
|
|
|
wm->nDeps = nb_imports;
|
2004-03-12 02:59:35 +01:00
|
|
|
wm->deps = RtlAllocateHeap( GetProcessHeap(), 0, nb_imports*sizeof(WINE_MODREF *) );
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
/* load the imported modules. They are automatically
|
|
|
|
* added to the modref list of the process.
|
|
|
|
*/
|
2003-04-08 01:27:54 +02:00
|
|
|
prev = current_modref;
|
|
|
|
current_modref = wm;
|
2003-10-11 01:28:51 +02:00
|
|
|
status = STATUS_SUCCESS;
|
2003-04-05 07:16:11 +02:00
|
|
|
for (i = 0; i < nb_imports; i++)
|
|
|
|
{
|
2003-10-11 01:28:51 +02:00
|
|
|
if (!(wm->deps[i] = import_dll( wm->ldr.BaseAddress, &imports[i], load_path )))
|
|
|
|
status = STATUS_DLL_NOT_FOUND;
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
2003-04-08 01:27:54 +02:00
|
|
|
current_modref = prev;
|
2003-10-11 01:28:51 +02:00
|
|
|
return status;
|
2003-03-18 19:29:13 +01:00
|
|
|
}
|
2002-08-16 00:09:53 +02:00
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
|
2003-03-14 06:01:20 +01:00
|
|
|
/*************************************************************************
|
2003-09-29 22:42:24 +02:00
|
|
|
* alloc_module
|
2003-03-14 06:01:20 +01:00
|
|
|
*
|
|
|
|
* Allocate a WINE_MODREF structure and add it to the process list
|
2003-04-05 07:16:11 +02:00
|
|
|
* The loader_section must be locked while calling this function.
|
2003-03-14 06:01:20 +01:00
|
|
|
*/
|
2003-09-29 22:42:24 +02:00
|
|
|
static WINE_MODREF *alloc_module( HMODULE hModule, LPCWSTR filename )
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
2004-06-15 02:47:00 +02:00
|
|
|
const WCHAR *p;
|
|
|
|
const IMAGE_NT_HEADERS *nt = RtlImageNtHeader(hModule);
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY entry, mark;
|
|
|
|
|
2003-12-03 21:28:41 +01:00
|
|
|
if (!(wm = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*wm) ))) return NULL;
|
2003-07-01 05:38:56 +02:00
|
|
|
|
2003-09-29 22:42:24 +02:00
|
|
|
wm->nDeps = 0;
|
|
|
|
wm->deps = NULL;
|
|
|
|
|
|
|
|
wm->ldr.BaseAddress = hModule;
|
2003-11-25 06:28:57 +01:00
|
|
|
wm->ldr.EntryPoint = NULL;
|
2003-09-29 22:42:24 +02:00
|
|
|
wm->ldr.SizeOfImage = nt->OptionalHeader.SizeOfImage;
|
2006-02-21 19:32:26 +01:00
|
|
|
wm->ldr.Flags = LDR_DONT_RESOLVE_REFS;
|
2006-07-31 21:03:01 +02:00
|
|
|
wm->ldr.LoadCount = 1;
|
2003-09-29 22:42:24 +02:00
|
|
|
wm->ldr.TlsIndex = -1;
|
|
|
|
wm->ldr.SectionHandle = NULL;
|
|
|
|
wm->ldr.CheckSum = 0;
|
|
|
|
wm->ldr.TimeDateStamp = 0;
|
|
|
|
|
|
|
|
RtlCreateUnicodeString( &wm->ldr.FullDllName, filename );
|
|
|
|
if ((p = strrchrW( wm->ldr.FullDllName.Buffer, '\\' ))) p++;
|
|
|
|
else p = wm->ldr.FullDllName.Buffer;
|
|
|
|
RtlInitUnicodeString( &wm->ldr.BaseDllName, p );
|
|
|
|
|
2003-11-25 06:28:57 +01:00
|
|
|
if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
|
2003-09-29 22:42:24 +02:00
|
|
|
{
|
2003-11-25 06:28:57 +01:00
|
|
|
wm->ldr.Flags |= LDR_IMAGE_IS_DLL;
|
|
|
|
if (nt->OptionalHeader.AddressOfEntryPoint)
|
|
|
|
wm->ldr.EntryPoint = (char *)hModule + nt->OptionalHeader.AddressOfEntryPoint;
|
2003-09-29 22:42:24 +02:00
|
|
|
}
|
|
|
|
|
2003-11-25 06:28:57 +01:00
|
|
|
InsertTailList(&NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList,
|
|
|
|
&wm->ldr.InLoadOrderModuleList);
|
2003-07-01 05:38:56 +02:00
|
|
|
|
2003-09-29 22:42:24 +02:00
|
|
|
/* insert module in MemoryList, sorted in increasing base addresses */
|
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
{
|
|
|
|
if (CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList)->BaseAddress > wm->ldr.BaseAddress)
|
|
|
|
break;
|
2003-03-14 06:01:20 +01:00
|
|
|
}
|
2003-09-29 22:42:24 +02:00
|
|
|
entry->Blink->Flink = &wm->ldr.InMemoryOrderModuleList;
|
|
|
|
wm->ldr.InMemoryOrderModuleList.Blink = entry->Blink;
|
|
|
|
wm->ldr.InMemoryOrderModuleList.Flink = entry;
|
|
|
|
entry->Blink = &wm->ldr.InMemoryOrderModuleList;
|
|
|
|
|
|
|
|
/* wait until init is called for inserting into this list */
|
|
|
|
wm->ldr.InInitializationOrderModuleList.Flink = NULL;
|
|
|
|
wm->ldr.InInitializationOrderModuleList.Blink = NULL;
|
2006-12-07 20:52:16 +01:00
|
|
|
|
|
|
|
if (!(nt->OptionalHeader.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
|
|
|
|
{
|
|
|
|
WARN( "disabling no-exec because of %s\n", debugstr_w(wm->ldr.BaseDllName.Buffer) );
|
|
|
|
VIRTUAL_SetForceExec( TRUE );
|
|
|
|
}
|
2003-09-29 22:42:24 +02:00
|
|
|
return wm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-14 21:51:14 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* alloc_process_tls
|
|
|
|
*
|
|
|
|
* Allocate the process-wide structure for module TLS storage.
|
|
|
|
*/
|
|
|
|
static NTSTATUS alloc_process_tls(void)
|
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
PLDR_MODULE mod;
|
2004-06-15 02:47:00 +02:00
|
|
|
const IMAGE_TLS_DIRECTORY *dir;
|
2003-05-14 21:51:14 +02:00
|
|
|
ULONG size, i;
|
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
2003-05-14 21:51:14 +02:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
|
|
|
|
if (!(dir = RtlImageDirectoryEntryToData( mod->BaseAddress, TRUE,
|
2003-05-14 21:51:14 +02:00
|
|
|
IMAGE_DIRECTORY_ENTRY_TLS, &size )))
|
|
|
|
continue;
|
|
|
|
size = (dir->EndAddressOfRawData - dir->StartAddressOfRawData) + dir->SizeOfZeroFill;
|
|
|
|
if (!size) continue;
|
|
|
|
tls_total_size += size;
|
|
|
|
tls_module_count++;
|
|
|
|
}
|
|
|
|
if (!tls_module_count) return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
TRACE( "count %u size %u\n", tls_module_count, tls_total_size );
|
|
|
|
|
2004-03-12 02:59:35 +01:00
|
|
|
tls_dirs = RtlAllocateHeap( GetProcessHeap(), 0, tls_module_count * sizeof(*tls_dirs) );
|
2003-05-14 21:51:14 +02:00
|
|
|
if (!tls_dirs) return STATUS_NO_MEMORY;
|
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
for (i = 0, entry = mark->Flink; entry != mark; entry = entry->Flink)
|
2003-05-14 21:51:14 +02:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
|
|
|
|
if (!(dir = RtlImageDirectoryEntryToData( mod->BaseAddress, TRUE,
|
2003-05-14 21:51:14 +02:00
|
|
|
IMAGE_DIRECTORY_ENTRY_TLS, &size )))
|
|
|
|
continue;
|
|
|
|
tls_dirs[i] = dir;
|
2005-07-19 13:44:32 +02:00
|
|
|
*(DWORD *)dir->AddressOfIndex = i;
|
2003-07-01 05:38:56 +02:00
|
|
|
mod->TlsIndex = i;
|
|
|
|
mod->LoadCount = -1; /* can't unload it */
|
2003-05-14 21:51:14 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* alloc_thread_tls
|
|
|
|
*
|
|
|
|
* Allocate the per-thread structure for module TLS storage.
|
|
|
|
*/
|
|
|
|
static NTSTATUS alloc_thread_tls(void)
|
|
|
|
{
|
|
|
|
void **pointers;
|
|
|
|
char *data;
|
|
|
|
UINT i;
|
|
|
|
|
|
|
|
if (!tls_module_count) return STATUS_SUCCESS;
|
|
|
|
|
2004-03-12 02:59:35 +01:00
|
|
|
if (!(pointers = RtlAllocateHeap( GetProcessHeap(), 0,
|
2003-05-14 21:51:14 +02:00
|
|
|
tls_module_count * sizeof(*pointers) )))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
2004-03-12 02:59:35 +01:00
|
|
|
if (!(data = RtlAllocateHeap( GetProcessHeap(), 0, tls_total_size )))
|
2003-05-14 21:51:14 +02:00
|
|
|
{
|
2004-03-12 02:59:35 +01:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, pointers );
|
2003-05-14 21:51:14 +02:00
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < tls_module_count; i++)
|
|
|
|
{
|
|
|
|
const IMAGE_TLS_DIRECTORY *dir = tls_dirs[i];
|
|
|
|
ULONG size = dir->EndAddressOfRawData - dir->StartAddressOfRawData;
|
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE( "thread %04x idx %d: %d/%d bytes from %p to %p\n",
|
2003-05-14 21:51:14 +02:00
|
|
|
GetCurrentThreadId(), i, size, dir->SizeOfZeroFill,
|
|
|
|
(void *)dir->StartAddressOfRawData, data );
|
|
|
|
|
|
|
|
pointers[i] = data;
|
|
|
|
memcpy( data, (void *)dir->StartAddressOfRawData, size );
|
|
|
|
data += size;
|
|
|
|
memset( data, 0, dir->SizeOfZeroFill );
|
|
|
|
data += dir->SizeOfZeroFill;
|
|
|
|
}
|
2003-08-28 01:14:29 +02:00
|
|
|
NtCurrentTeb()->ThreadLocalStoragePointer = pointers;
|
2003-05-14 21:51:14 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-08 06:13:26 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* call_tls_callbacks
|
|
|
|
*/
|
|
|
|
static void call_tls_callbacks( HMODULE module, UINT reason )
|
|
|
|
{
|
|
|
|
const IMAGE_TLS_DIRECTORY *dir;
|
|
|
|
const PIMAGE_TLS_CALLBACK *callback;
|
|
|
|
ULONG dirsize;
|
|
|
|
|
|
|
|
dir = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_TLS, &dirsize );
|
|
|
|
if (!dir || !dir->AddressOfCallBacks) return;
|
|
|
|
|
2005-07-19 13:44:32 +02:00
|
|
|
for (callback = (const PIMAGE_TLS_CALLBACK *)dir->AddressOfCallBacks; *callback; callback++)
|
2003-05-08 06:13:26 +02:00
|
|
|
{
|
|
|
|
if (TRACE_ON(relay))
|
2006-10-16 13:49:06 +02:00
|
|
|
DPRINTF("%04x:Call TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
|
2003-05-08 06:13:26 +02:00
|
|
|
GetCurrentThreadId(), *callback, module, reason_names[reason] );
|
|
|
|
(*callback)( module, reason, NULL );
|
|
|
|
if (TRACE_ON(relay))
|
2006-10-16 13:49:06 +02:00
|
|
|
DPRINTF("%04x:Ret TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
|
2003-05-08 06:13:26 +02:00
|
|
|
GetCurrentThreadId(), *callback, module, reason_names[reason] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-20 05:55:59 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* MODULE_InitDLL
|
|
|
|
*/
|
2003-05-08 06:13:26 +02:00
|
|
|
static BOOL MODULE_InitDLL( WINE_MODREF *wm, UINT reason, LPVOID lpReserved )
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
2003-09-29 22:42:24 +02:00
|
|
|
WCHAR mod_name[32];
|
2003-03-20 05:55:59 +01:00
|
|
|
BOOL retv = TRUE;
|
2003-04-04 21:50:17 +02:00
|
|
|
DLLENTRYPROC entry = wm->ldr.EntryPoint;
|
|
|
|
void *module = wm->ldr.BaseAddress;
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
/* Skip calls for modules loaded with special load flags */
|
|
|
|
|
2003-03-21 00:40:08 +01:00
|
|
|
if (wm->ldr.Flags & LDR_DONT_RESOLVE_REFS) return TRUE;
|
2003-05-08 06:13:26 +02:00
|
|
|
if (wm->ldr.TlsIndex != -1) call_tls_callbacks( wm->ldr.BaseAddress, reason );
|
2003-11-25 06:28:57 +01:00
|
|
|
if (!entry) return TRUE;
|
2003-04-04 21:50:17 +02:00
|
|
|
|
|
|
|
if (TRACE_ON(relay))
|
2003-04-08 01:27:54 +02:00
|
|
|
{
|
2003-10-24 02:32:36 +02:00
|
|
|
size_t len = min( wm->ldr.BaseDllName.Length, sizeof(mod_name)-sizeof(WCHAR) );
|
2003-09-29 22:42:24 +02:00
|
|
|
memcpy( mod_name, wm->ldr.BaseDllName.Buffer, len );
|
|
|
|
mod_name[len / sizeof(WCHAR)] = 0;
|
2006-10-16 13:49:06 +02:00
|
|
|
DPRINTF("%04x:Call PE DLL (proc=%p,module=%p %s,reason=%s,res=%p)\n",
|
2003-09-29 22:42:24 +02:00
|
|
|
GetCurrentThreadId(), entry, module, debugstr_w(mod_name),
|
|
|
|
reason_names[reason], lpReserved );
|
2003-04-08 01:27:54 +02:00
|
|
|
}
|
2003-09-29 22:42:24 +02:00
|
|
|
else TRACE("(%p %s,%s,%p) - CALL\n", module, debugstr_w(wm->ldr.BaseDllName.Buffer),
|
|
|
|
reason_names[reason], lpReserved );
|
2003-03-20 05:55:59 +01:00
|
|
|
|
2004-03-19 03:07:16 +01:00
|
|
|
retv = call_dll_entry_point( entry, module, reason, lpReserved );
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
/* The state of the module list may have changed due to the call
|
2003-04-04 21:50:17 +02:00
|
|
|
to the dll. We cannot assume that this module has not been
|
2003-03-20 05:55:59 +01:00
|
|
|
deleted. */
|
2003-04-08 01:27:54 +02:00
|
|
|
if (TRACE_ON(relay))
|
2006-10-16 13:49:06 +02:00
|
|
|
DPRINTF("%04x:Ret PE DLL (proc=%p,module=%p %s,reason=%s,res=%p) retval=%x\n",
|
2003-09-29 22:42:24 +02:00
|
|
|
GetCurrentThreadId(), entry, module, debugstr_w(mod_name),
|
|
|
|
reason_names[reason], lpReserved, retv );
|
2003-05-08 06:13:26 +02:00
|
|
|
else TRACE("(%p,%s,%p) - RETURN %d\n", module, reason_names[reason], lpReserved, retv );
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
2003-10-06 23:18:51 +02:00
|
|
|
* process_attach
|
2003-03-20 05:55:59 +01:00
|
|
|
*
|
|
|
|
* Send the process attach notification to all DLLs the given module
|
|
|
|
* depends on (recursively). This is somewhat complicated due to the fact that
|
|
|
|
*
|
|
|
|
* - we have to respect the module dependencies, i.e. modules implicitly
|
|
|
|
* referenced by another module have to be initialized before the module
|
|
|
|
* itself can be initialized
|
|
|
|
*
|
|
|
|
* - the initialization routine of a DLL can itself call LoadLibrary,
|
|
|
|
* thereby introducing a whole new set of dependencies (even involving
|
|
|
|
* the 'old' modules) at any time during the whole process
|
|
|
|
*
|
|
|
|
* (Note that this routine can be recursively entered not only directly
|
|
|
|
* from itself, but also via LoadLibrary from one of the called initialization
|
|
|
|
* routines.)
|
|
|
|
*
|
|
|
|
* Furthermore, we need to rearrange the main WINE_MODREF list to allow
|
|
|
|
* the process *detach* notifications to be sent in the correct order.
|
|
|
|
* This must not only take into account module dependencies, but also
|
|
|
|
* 'hidden' dependencies created by modules calling LoadLibrary in their
|
|
|
|
* attach notification routine.
|
|
|
|
*
|
|
|
|
* The strategy is rather simple: we move a WINE_MODREF to the head of the
|
|
|
|
* list after the attach notification has returned. This implies that the
|
|
|
|
* detach notifications are called in the reverse of the sequence the attach
|
|
|
|
* notifications *returned*.
|
2003-10-06 23:18:51 +02:00
|
|
|
*
|
|
|
|
* The loader_section must be locked while calling this function.
|
2003-03-20 05:55:59 +01:00
|
|
|
*/
|
2003-10-06 23:18:51 +02:00
|
|
|
static NTSTATUS process_attach( WINE_MODREF *wm, LPVOID lpReserved )
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
2003-05-14 21:51:14 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2003-03-20 05:55:59 +01:00
|
|
|
int i;
|
|
|
|
|
2005-08-09 22:45:46 +02:00
|
|
|
if (process_detaching) return status;
|
|
|
|
|
2003-03-20 05:55:59 +01:00
|
|
|
/* prevent infinite recursion in case of cyclical dependencies */
|
2003-03-21 00:40:08 +01:00
|
|
|
if ( ( wm->ldr.Flags & LDR_LOAD_IN_PROGRESS )
|
|
|
|
|| ( wm->ldr.Flags & LDR_PROCESS_ATTACHED ) )
|
2003-10-06 23:18:51 +02:00
|
|
|
return status;
|
2003-03-20 05:55:59 +01:00
|
|
|
|
2003-09-29 22:42:24 +02:00
|
|
|
TRACE("(%s,%p) - START\n", debugstr_w(wm->ldr.BaseDllName.Buffer), lpReserved );
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
/* Tag current MODREF to prevent recursive loop */
|
2003-03-21 00:40:08 +01:00
|
|
|
wm->ldr.Flags |= LDR_LOAD_IN_PROGRESS;
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
/* Recursively attach all DLLs this one depends on */
|
2003-05-14 21:51:14 +02:00
|
|
|
for ( i = 0; i < wm->nDeps; i++ )
|
|
|
|
{
|
|
|
|
if (!wm->deps[i]) continue;
|
2003-10-06 23:18:51 +02:00
|
|
|
if ((status = process_attach( wm->deps[i], lpReserved )) != STATUS_SUCCESS) break;
|
2003-05-14 21:51:14 +02:00
|
|
|
}
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
/* Call DLL entry point */
|
2003-05-14 21:51:14 +02:00
|
|
|
if (status == STATUS_SUCCESS)
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
2003-04-08 01:27:54 +02:00
|
|
|
WINE_MODREF *prev = current_modref;
|
|
|
|
current_modref = wm;
|
2003-05-14 21:51:14 +02:00
|
|
|
if (MODULE_InitDLL( wm, DLL_PROCESS_ATTACH, lpReserved ))
|
2005-01-11 17:01:31 +01:00
|
|
|
{
|
2003-05-14 21:51:14 +02:00
|
|
|
wm->ldr.Flags |= LDR_PROCESS_ATTACHED;
|
2005-01-11 17:01:31 +01:00
|
|
|
}
|
2003-05-14 21:51:14 +02:00
|
|
|
else
|
2005-01-11 17:01:31 +01:00
|
|
|
{
|
|
|
|
/* point to the name so LdrInitializeThunk can print it */
|
|
|
|
last_failed_modref = wm;
|
|
|
|
WARN("Initialization of %s failed\n", debugstr_w(wm->ldr.BaseDllName.Buffer));
|
2003-05-14 21:51:14 +02:00
|
|
|
status = STATUS_DLL_INIT_FAILED;
|
2005-01-11 17:01:31 +01:00
|
|
|
}
|
2003-04-08 01:27:54 +02:00
|
|
|
current_modref = prev;
|
2003-03-20 05:55:59 +01:00
|
|
|
}
|
|
|
|
|
2005-08-09 22:45:46 +02:00
|
|
|
if (!wm->ldr.InInitializationOrderModuleList.Flink)
|
|
|
|
InsertTailList(&NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList,
|
|
|
|
&wm->ldr.InInitializationOrderModuleList);
|
2003-10-06 23:18:51 +02:00
|
|
|
|
2003-03-20 05:55:59 +01:00
|
|
|
/* Remove recursion flag */
|
2003-03-21 00:40:08 +01:00
|
|
|
wm->ldr.Flags &= ~LDR_LOAD_IN_PROGRESS;
|
2003-03-20 05:55:59 +01:00
|
|
|
|
2003-09-29 22:42:24 +02:00
|
|
|
TRACE("(%s,%p) - END\n", debugstr_w(wm->ldr.BaseDllName.Buffer), lpReserved );
|
2003-05-14 21:51:14 +02:00
|
|
|
return status;
|
2003-03-20 05:55:59 +01:00
|
|
|
}
|
|
|
|
|
2005-10-26 14:12:44 +02:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* attach_implicitly_loaded_dlls
|
|
|
|
*
|
|
|
|
* Attach to the (builtin) dlls that have been implicitly loaded because
|
|
|
|
* of a dependency at the Unix level, but not imported at the Win32 level.
|
|
|
|
*/
|
|
|
|
static void attach_implicitly_loaded_dlls( LPVOID reserved )
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
|
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
{
|
|
|
|
LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
|
|
|
|
if (mod->Flags & (LDR_LOAD_IN_PROGRESS | LDR_PROCESS_ATTACHED)) continue;
|
|
|
|
TRACE( "found implicitly loaded %s, attaching to it\n",
|
|
|
|
debugstr_w(mod->BaseDllName.Buffer));
|
2006-01-04 14:55:33 +01:00
|
|
|
mod->LoadCount = -1; /* we can't unload it anyway */
|
2005-10-26 14:12:44 +02:00
|
|
|
process_attach( CONTAINING_RECORD(mod, WINE_MODREF, ldr), reserved );
|
|
|
|
break; /* restart the search from the start */
|
|
|
|
}
|
|
|
|
if (entry == mark) break; /* nothing found */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-20 05:55:59 +01:00
|
|
|
/*************************************************************************
|
2003-10-06 23:18:51 +02:00
|
|
|
* process_detach
|
2003-03-20 05:55:59 +01:00
|
|
|
*
|
|
|
|
* Send DLL process detach notifications. See the comment about calling
|
2003-10-06 23:18:51 +02:00
|
|
|
* sequence at process_attach. Unless the bForceDetach flag
|
2003-03-20 05:55:59 +01:00
|
|
|
* is set, only DLLs with zero refcount are notified.
|
|
|
|
*/
|
2003-10-06 23:18:51 +02:00
|
|
|
static void process_detach( BOOL bForceDetach, LPVOID lpReserved )
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
PLDR_MODULE mod;
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
if (bForceDetach) process_detaching = 1;
|
2003-07-01 05:38:56 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
|
2003-03-20 05:55:59 +01:00
|
|
|
do
|
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
for (entry = mark->Blink; entry != mark; entry = entry->Blink)
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE,
|
|
|
|
InInitializationOrderModuleList);
|
2003-03-20 05:55:59 +01:00
|
|
|
/* Check whether to detach this DLL */
|
2003-07-01 05:38:56 +02:00
|
|
|
if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
|
2003-03-20 05:55:59 +01:00
|
|
|
continue;
|
2003-07-01 05:38:56 +02:00
|
|
|
if ( mod->LoadCount && !bForceDetach )
|
2003-03-20 05:55:59 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Call detach notification */
|
2003-07-01 05:38:56 +02:00
|
|
|
mod->Flags &= ~LDR_PROCESS_ATTACHED;
|
|
|
|
MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
|
|
|
|
DLL_PROCESS_DETACH, lpReserved );
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
/* Restart at head of WINE_MODREF list, as entries might have
|
|
|
|
been added and/or removed while performing the call ... */
|
|
|
|
break;
|
|
|
|
}
|
2003-07-01 05:38:56 +02:00
|
|
|
} while (entry != mark);
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* MODULE_DllThreadAttach
|
|
|
|
*
|
|
|
|
* Send DLL thread attach notifications. These are sent in the
|
|
|
|
* reverse sequence of process detach notification.
|
|
|
|
*
|
|
|
|
*/
|
2003-05-14 21:51:14 +02:00
|
|
|
NTSTATUS MODULE_DllThreadAttach( LPVOID lpReserved )
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
PLDR_MODULE mod;
|
|
|
|
NTSTATUS status;
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
/* don't do any attach calls if process is exiting */
|
2003-05-14 21:51:14 +02:00
|
|
|
if (process_detaching) return STATUS_SUCCESS;
|
2003-03-20 05:55:59 +01:00
|
|
|
/* FIXME: there is still a race here */
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
2003-05-14 21:51:14 +02:00
|
|
|
if ((status = alloc_thread_tls()) != STATUS_SUCCESS) goto done;
|
2003-03-20 05:55:59 +01:00
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE,
|
|
|
|
InInitializationOrderModuleList);
|
|
|
|
if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
|
2003-03-20 05:55:59 +01:00
|
|
|
continue;
|
2003-07-01 05:38:56 +02:00
|
|
|
if ( mod->Flags & LDR_NO_DLL_CALLS )
|
2003-03-20 05:55:59 +01:00
|
|
|
continue;
|
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
|
|
|
|
DLL_THREAD_ATTACH, lpReserved );
|
2003-03-20 05:55:59 +01:00
|
|
|
}
|
|
|
|
|
2003-05-14 21:51:14 +02:00
|
|
|
done:
|
2003-03-20 05:55:59 +01:00
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
2003-05-14 21:51:14 +02:00
|
|
|
return status;
|
2003-03-20 05:55:59 +01:00
|
|
|
}
|
|
|
|
|
2003-03-04 05:36:56 +01:00
|
|
|
/******************************************************************
|
|
|
|
* LdrDisableThreadCalloutsForDll (NTDLL.@)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HMODULE hModule)
|
2002-01-29 19:30:16 +01:00
|
|
|
{
|
2003-03-04 05:36:56 +01:00
|
|
|
WINE_MODREF *wm;
|
|
|
|
NTSTATUS ret = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
wm = get_modref( hModule );
|
2003-05-08 06:13:26 +02:00
|
|
|
if (!wm || wm->ldr.TlsIndex != -1)
|
2003-03-04 05:36:56 +01:00
|
|
|
ret = STATUS_DLL_NOT_FOUND;
|
2002-01-29 19:30:16 +01:00
|
|
|
else
|
2003-03-21 00:40:08 +01:00
|
|
|
wm->ldr.Flags |= LDR_NO_DLL_CALLS;
|
2003-03-04 05:36:56 +01:00
|
|
|
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
return ret;
|
2002-01-29 19:30:16 +01:00
|
|
|
}
|
2002-06-10 04:28:00 +02:00
|
|
|
|
2003-03-18 19:29:13 +01:00
|
|
|
/******************************************************************
|
|
|
|
* LdrFindEntryForAddress (NTDLL.@)
|
|
|
|
*
|
|
|
|
* The loader_section must be locked while calling this function
|
|
|
|
*/
|
2003-07-01 05:38:56 +02:00
|
|
|
NTSTATUS WINAPI LdrFindEntryForAddress(const void* addr, PLDR_MODULE* pmod)
|
2003-03-18 19:29:13 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
PLDR_MODULE mod;
|
2003-03-18 19:29:13 +01:00
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
2003-03-18 19:29:13 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
|
|
|
|
if ((const void *)mod->BaseAddress <= addr &&
|
2004-06-15 02:47:00 +02:00
|
|
|
(const char *)addr < (char*)mod->BaseAddress + mod->SizeOfImage)
|
2003-03-18 19:29:13 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
*pmod = mod;
|
2003-03-18 19:29:13 +01:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2003-07-01 05:38:56 +02:00
|
|
|
if ((const void *)mod->BaseAddress > addr) break;
|
2003-03-18 19:29:13 +01:00
|
|
|
}
|
|
|
|
return STATUS_NO_MORE_ENTRIES;
|
|
|
|
}
|
|
|
|
|
2003-03-20 05:29:21 +01:00
|
|
|
/******************************************************************
|
|
|
|
* LdrLockLoaderLock (NTDLL.@)
|
|
|
|
*
|
|
|
|
* Note: flags are not implemented.
|
|
|
|
* Flag 0x01 is used to raise exceptions on errors.
|
|
|
|
* Flag 0x02 is used to avoid waiting on the section (does RtlTryEnterCriticalSection instead).
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI LdrLockLoaderLock( ULONG flags, ULONG *result, ULONG *magic )
|
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
if (flags) FIXME( "flags %x not supported\n", flags );
|
2003-03-20 05:29:21 +01:00
|
|
|
|
|
|
|
if (result) *result = 1;
|
|
|
|
if (!magic) return STATUS_INVALID_PARAMETER_3;
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
*magic = GetCurrentThreadId();
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* LdrUnlockLoaderUnlock (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI LdrUnlockLoaderLock( ULONG flags, ULONG magic )
|
|
|
|
{
|
|
|
|
if (magic)
|
|
|
|
{
|
|
|
|
if (magic != GetCurrentThreadId()) return STATUS_INVALID_PARAMETER_2;
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-07 00:41:37 +01:00
|
|
|
/******************************************************************
|
|
|
|
* LdrGetDllHandle (NTDLL.@)
|
|
|
|
*/
|
2003-09-29 22:42:24 +02:00
|
|
|
NTSTATUS WINAPI LdrGetDllHandle(ULONG x, ULONG y, const UNICODE_STRING *name, HMODULE *base)
|
2002-06-10 04:28:00 +02:00
|
|
|
{
|
2003-09-26 01:55:35 +02:00
|
|
|
NTSTATUS status = STATUS_DLL_NOT_FOUND;
|
|
|
|
WCHAR dllname[MAX_PATH+4], *p;
|
|
|
|
UNICODE_STRING str;
|
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
PLDR_MODULE mod;
|
2002-06-10 04:28:00 +02:00
|
|
|
|
2003-03-07 00:41:37 +01:00
|
|
|
if (x != 0 || y != 0)
|
|
|
|
FIXME("Unknown behavior, please report\n");
|
|
|
|
|
2003-09-26 01:55:35 +02:00
|
|
|
/* Append .DLL to name if no extension present */
|
|
|
|
if (!(p = strrchrW( name->Buffer, '.')) || strchrW( p, '/' ) || strchrW( p, '\\'))
|
2003-03-07 00:41:37 +01:00
|
|
|
{
|
2003-09-26 01:55:35 +02:00
|
|
|
if (name->Length >= MAX_PATH) return STATUS_NAME_TOO_LONG;
|
|
|
|
strcpyW( dllname, name->Buffer );
|
|
|
|
strcatW( dllname, dllW );
|
|
|
|
RtlInitUnicodeString( &str, dllname );
|
|
|
|
name = &str;
|
2003-03-07 00:41:37 +01:00
|
|
|
}
|
2002-06-10 04:28:00 +02:00
|
|
|
|
2003-09-26 01:55:35 +02:00
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
if (cached_modref)
|
|
|
|
{
|
|
|
|
if (RtlEqualUnicodeString( name, &cached_modref->ldr.FullDllName, TRUE ) ||
|
|
|
|
RtlEqualUnicodeString( name, &cached_modref->ldr.BaseDllName, TRUE ))
|
|
|
|
{
|
|
|
|
*base = cached_modref->ldr.BaseAddress;
|
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2003-04-04 21:50:17 +02:00
|
|
|
|
2003-09-26 01:55:35 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
{
|
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
2003-04-04 21:50:17 +02:00
|
|
|
|
2003-09-26 01:55:35 +02:00
|
|
|
if (RtlEqualUnicodeString( name, &mod->FullDllName, TRUE ) ||
|
|
|
|
RtlEqualUnicodeString( name, &mod->BaseDllName, TRUE ))
|
|
|
|
{
|
|
|
|
cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
|
|
|
|
*base = mod->BaseAddress;
|
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("%x %x %s -> %p\n", x, y, debugstr_us(name), status ? NULL : *base);
|
2003-09-26 01:55:35 +02:00
|
|
|
return status;
|
2002-06-10 04:28:00 +02:00
|
|
|
}
|
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* LdrGetProcedureAddress (NTDLL.@)
|
2003-03-07 00:41:37 +01:00
|
|
|
*/
|
2003-09-29 22:42:24 +02:00
|
|
|
NTSTATUS WINAPI LdrGetProcedureAddress(HMODULE module, const ANSI_STRING *name,
|
|
|
|
ULONG ord, PVOID *address)
|
2003-03-07 00:41:37 +01:00
|
|
|
{
|
2003-04-05 07:16:11 +02:00
|
|
|
IMAGE_EXPORT_DIRECTORY *exports;
|
|
|
|
DWORD exp_size;
|
|
|
|
NTSTATUS ret = STATUS_PROCEDURE_NOT_FOUND;
|
2002-06-10 04:28:00 +02:00
|
|
|
|
2003-03-07 00:41:37 +01:00
|
|
|
RtlEnterCriticalSection( &loader_section );
|
2003-04-05 07:16:11 +02:00
|
|
|
|
2005-11-29 17:10:30 +01:00
|
|
|
/* check if the module itself is invalid to return the proper error */
|
|
|
|
if (!get_modref( module )) ret = STATUS_DLL_NOT_FOUND;
|
|
|
|
else if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
|
|
|
|
IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
|
2003-03-07 00:41:37 +01:00
|
|
|
{
|
2003-04-05 07:16:11 +02:00
|
|
|
void *proc = name ? find_named_export( module, exports, exp_size, name->Buffer, -1 )
|
|
|
|
: find_ordinal_export( module, exports, exp_size, ord - exports->Base );
|
|
|
|
if (proc)
|
|
|
|
{
|
|
|
|
*address = proc;
|
|
|
|
ret = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
2003-03-07 00:41:37 +01:00
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
return ret;
|
2002-06-10 04:28:00 +02:00
|
|
|
}
|
2002-08-09 21:57:38 +02:00
|
|
|
|
|
|
|
|
2006-03-21 11:31:23 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* is_fake_dll
|
|
|
|
*
|
|
|
|
* Check if a loaded native dll is a Wine fake dll.
|
|
|
|
*/
|
|
|
|
static BOOL is_fake_dll( const void *base )
|
|
|
|
{
|
|
|
|
static const char fakedll_signature[] = "Wine placeholder DLL";
|
|
|
|
const IMAGE_DOS_HEADER *dos = base;
|
|
|
|
|
|
|
|
if (dos->e_lfanew >= sizeof(*dos) + sizeof(fakedll_signature) &&
|
|
|
|
!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) )) return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-07 21:16:25 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* get_builtin_fullname
|
|
|
|
*
|
|
|
|
* Build the full pathname for a builtin dll.
|
|
|
|
*/
|
|
|
|
static WCHAR *get_builtin_fullname( const WCHAR *path, const char *filename )
|
|
|
|
{
|
|
|
|
static const WCHAR soW[] = {'.','s','o',0};
|
|
|
|
WCHAR *p, *fullname;
|
|
|
|
size_t i, len = strlen(filename);
|
|
|
|
|
|
|
|
/* check if path can correspond to the dll we have */
|
|
|
|
if (path && (p = strrchrW( path, '\\' )))
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
if (tolowerW(p[i]) != tolowerW( (WCHAR)filename[i]) ) break;
|
|
|
|
if (i == len && (!p[len] || !strcmpiW( p + len, soW )))
|
|
|
|
{
|
|
|
|
/* the filename matches, use path as the full path */
|
|
|
|
len += p - path;
|
|
|
|
if ((fullname = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
|
|
|
|
{
|
|
|
|
memcpy( fullname, path, len * sizeof(WCHAR) );
|
|
|
|
fullname[len] = 0;
|
|
|
|
}
|
|
|
|
return fullname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fullname = RtlAllocateHeap( GetProcessHeap(), 0,
|
|
|
|
system_dir.MaximumLength + (len + 1) * sizeof(WCHAR) )))
|
|
|
|
{
|
|
|
|
memcpy( fullname, system_dir.Buffer, system_dir.Length );
|
|
|
|
p = fullname + system_dir.Length / sizeof(WCHAR);
|
|
|
|
if (p > fullname && p[-1] != '\\') *p++ = '\\';
|
|
|
|
ascii_to_unicode( p, filename, len + 1 );
|
|
|
|
}
|
|
|
|
return fullname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-19 06:04:57 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* load_builtin_callback
|
|
|
|
*
|
|
|
|
* Load a library in memory; callback function for wine_dll_register
|
|
|
|
*/
|
|
|
|
static void load_builtin_callback( void *module, const char *filename )
|
|
|
|
{
|
2003-10-10 02:12:17 +02:00
|
|
|
static const WCHAR emptyW[1];
|
2003-11-03 23:23:09 +01:00
|
|
|
void *addr;
|
2003-09-19 06:04:57 +02:00
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
WINE_MODREF *wm;
|
2006-02-07 21:16:25 +01:00
|
|
|
WCHAR *fullname;
|
2003-10-10 02:12:17 +02:00
|
|
|
const WCHAR *load_path;
|
2006-10-16 13:49:06 +02:00
|
|
|
SIZE_T size;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
|
|
|
if (!module)
|
|
|
|
{
|
|
|
|
ERR("could not map image for %s\n", filename ? filename : "main exe" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(nt = RtlImageNtHeader( module )))
|
|
|
|
{
|
|
|
|
ERR( "bad module for %s\n", filename ? filename : "main exe" );
|
2003-10-08 00:56:34 +02:00
|
|
|
builtin_load_info->status = STATUS_INVALID_IMAGE_FORMAT;
|
2003-09-19 06:04:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2005-06-05 20:22:02 +02:00
|
|
|
addr = module;
|
2006-10-16 13:49:06 +02:00
|
|
|
size = nt->OptionalHeader.SizeOfImage;
|
|
|
|
NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &size,
|
2005-06-05 20:22:02 +02:00
|
|
|
MEM_SYSTEM | MEM_IMAGE, PAGE_EXECUTE_WRITECOPY );
|
2003-09-19 06:04:57 +02:00
|
|
|
/* create the MODREF */
|
|
|
|
|
2006-02-07 21:16:25 +01:00
|
|
|
if (!(fullname = get_builtin_fullname( builtin_load_info->filename, filename )))
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
|
|
|
ERR( "can't load %s\n", filename );
|
2003-10-08 00:56:34 +02:00
|
|
|
builtin_load_info->status = STATUS_NO_MEMORY;
|
2003-09-19 06:04:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-09-29 22:42:24 +02:00
|
|
|
wm = alloc_module( module, fullname );
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, fullname );
|
2003-09-19 06:04:57 +02:00
|
|
|
if (!wm)
|
|
|
|
{
|
|
|
|
ERR( "can't load %s\n", filename );
|
2003-10-08 00:56:34 +02:00
|
|
|
builtin_load_info->status = STATUS_NO_MEMORY;
|
2003-09-19 06:04:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
wm->ldr.Flags |= LDR_WINE_INTERNAL;
|
|
|
|
|
2006-02-21 19:48:18 +01:00
|
|
|
if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL) &&
|
|
|
|
!NtCurrentTeb()->Peb->ImageBaseAddress) /* if we already have an executable, ignore this one */
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
2006-02-21 19:48:18 +01:00
|
|
|
NtCurrentTeb()->Peb->ImageBaseAddress = module;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* fixup imports */
|
|
|
|
|
|
|
|
load_path = builtin_load_info->load_path;
|
|
|
|
if (!load_path) load_path = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
|
|
|
|
if (!load_path) load_path = emptyW;
|
|
|
|
if (fixup_imports( wm, load_path ) != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
/* the module has only be inserted in the load & memory order lists */
|
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
/* FIXME: free the modref */
|
|
|
|
builtin_load_info->status = STATUS_DLL_NOT_FOUND;
|
|
|
|
return;
|
|
|
|
}
|
2003-09-19 06:04:57 +02:00
|
|
|
}
|
2006-02-21 19:48:18 +01:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
builtin_load_info->wm = wm;
|
2003-09-19 06:04:57 +02:00
|
|
|
TRACE( "loaded %s %p %p\n", filename, wm, module );
|
|
|
|
|
|
|
|
/* send the DLL load event */
|
|
|
|
|
|
|
|
SERVER_START_REQ( load_dll )
|
|
|
|
{
|
|
|
|
req->handle = 0;
|
|
|
|
req->base = module;
|
|
|
|
req->size = nt->OptionalHeader.SizeOfImage;
|
|
|
|
req->dbg_offset = nt->FileHeader.PointerToSymbolTable;
|
|
|
|
req->dbg_size = nt->FileHeader.NumberOfSymbols;
|
2003-09-30 03:04:19 +02:00
|
|
|
req->name = &wm->ldr.FullDllName.Buffer;
|
|
|
|
wine_server_add_data( req, wm->ldr.FullDllName.Buffer, wm->ldr.FullDllName.Length );
|
2003-09-19 06:04:57 +02:00
|
|
|
wine_server_call( req );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
/* setup relay debugging entry points */
|
|
|
|
if (TRACE_ON(relay)) RELAY_SetupDLL( module );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* load_native_dll (internal)
|
|
|
|
*/
|
2003-10-08 00:56:34 +02:00
|
|
|
static NTSTATUS load_native_dll( LPCWSTR load_path, LPCWSTR name, HANDLE file,
|
|
|
|
DWORD flags, WINE_MODREF** pwm )
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
|
|
|
void *module;
|
2003-10-02 06:58:49 +02:00
|
|
|
HANDLE mapping;
|
2003-09-19 06:04:57 +02:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
LARGE_INTEGER size;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
2005-09-12 12:36:06 +02:00
|
|
|
SIZE_T len = 0;
|
2003-09-19 06:04:57 +02:00
|
|
|
WINE_MODREF *wm;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2006-02-21 11:56:16 +01:00
|
|
|
TRACE("Trying native dll %s\n", debugstr_w(name));
|
2003-09-19 06:04:57 +02:00
|
|
|
|
|
|
|
attr.Length = sizeof(attr);
|
|
|
|
attr.RootDirectory = 0;
|
|
|
|
attr.ObjectName = NULL;
|
|
|
|
attr.Attributes = 0;
|
|
|
|
attr.SecurityDescriptor = NULL;
|
|
|
|
attr.SecurityQualityOfService = NULL;
|
|
|
|
size.QuadPart = 0;
|
|
|
|
|
|
|
|
status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
|
|
|
|
&attr, &size, 0, SEC_IMAGE, file );
|
2003-10-02 06:58:49 +02:00
|
|
|
if (status != STATUS_SUCCESS) return status;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
|
|
|
module = NULL;
|
2005-02-22 20:33:50 +01:00
|
|
|
status = NtMapViewOfSection( mapping, NtCurrentProcess(),
|
2003-09-19 06:04:57 +02:00
|
|
|
&module, 0, 0, &size, &len, ViewShare, 0, PAGE_READONLY );
|
|
|
|
NtClose( mapping );
|
2003-10-02 06:58:49 +02:00
|
|
|
if (status != STATUS_SUCCESS) return status;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
2006-03-21 11:31:23 +01:00
|
|
|
if (is_fake_dll( module ))
|
|
|
|
{
|
|
|
|
TRACE( "%s is a fake dll, not loading it\n", debugstr_w(name) );
|
|
|
|
NtUnmapViewOfSection( NtCurrentProcess(), module );
|
|
|
|
return STATUS_DLL_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2003-09-19 06:04:57 +02:00
|
|
|
/* create the MODREF */
|
|
|
|
|
2003-10-02 06:58:49 +02:00
|
|
|
if (!(wm = alloc_module( module, name ))) return STATUS_NO_MEMORY;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
|
|
|
/* fixup imports */
|
|
|
|
|
|
|
|
if (!(flags & DONT_RESOLVE_DLL_REFERENCES))
|
|
|
|
{
|
2003-10-08 00:56:34 +02:00
|
|
|
if ((status = fixup_imports( wm, load_path )) != STATUS_SUCCESS)
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
|
|
|
/* the module has only be inserted in the load & memory order lists */
|
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
|
|
|
|
/* FIXME: there are several more dangling references
|
|
|
|
* left. Including dlls loaded by this dll before the
|
|
|
|
* failed one. Unrolling is rather difficult with the
|
|
|
|
* current structure and we can leave them lying
|
|
|
|
* around with no problems, so we don't care.
|
|
|
|
* As these might reference our wm, we don't free it.
|
|
|
|
*/
|
2003-10-02 06:58:49 +02:00
|
|
|
return status;
|
2003-09-19 06:04:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send DLL load event */
|
|
|
|
|
|
|
|
nt = RtlImageNtHeader( module );
|
|
|
|
|
2004-04-07 01:13:47 +02:00
|
|
|
/* don't keep the file open if the mapping is from removable media */
|
|
|
|
if (!VIRTUAL_HasMapping( module )) file = 0;
|
|
|
|
|
2003-09-19 06:04:57 +02:00
|
|
|
SERVER_START_REQ( load_dll )
|
|
|
|
{
|
|
|
|
req->handle = file;
|
|
|
|
req->base = module;
|
|
|
|
req->size = nt->OptionalHeader.SizeOfImage;
|
|
|
|
req->dbg_offset = nt->FileHeader.PointerToSymbolTable;
|
|
|
|
req->dbg_size = nt->FileHeader.NumberOfSymbols;
|
2003-09-30 03:04:19 +02:00
|
|
|
req->name = &wm->ldr.FullDllName.Buffer;
|
|
|
|
wine_server_add_data( req, wm->ldr.FullDllName.Buffer, wm->ldr.FullDllName.Length );
|
2003-09-19 06:04:57 +02:00
|
|
|
wine_server_call( req );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
if (TRACE_ON(snoop)) SNOOP_SetupDLL( module );
|
|
|
|
|
2005-05-23 12:33:34 +02:00
|
|
|
TRACE_(loaddll)( " Loaded module %s : native\n", debugstr_w(wm->ldr.FullDllName.Buffer) );
|
|
|
|
|
2006-02-28 12:41:25 +01:00
|
|
|
wm->ldr.LoadCount = 1;
|
2003-09-19 06:04:57 +02:00
|
|
|
*pwm = wm;
|
2003-10-02 06:58:49 +02:00
|
|
|
return STATUS_SUCCESS;
|
2003-09-29 22:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-19 06:04:57 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* load_builtin_dll
|
|
|
|
*/
|
2006-02-07 21:16:25 +01:00
|
|
|
static NTSTATUS load_builtin_dll( LPCWSTR load_path, LPCWSTR path, HANDLE file,
|
|
|
|
DWORD flags, WINE_MODREF** pwm )
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
2003-09-29 22:42:24 +02:00
|
|
|
char error[256], dllname[MAX_PATH];
|
|
|
|
const WCHAR *name, *p;
|
|
|
|
DWORD len, i;
|
2006-02-07 21:16:25 +01:00
|
|
|
void *handle = NULL;
|
2003-10-08 00:56:34 +02:00
|
|
|
struct builtin_load_info info, *prev_info;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
|
|
|
/* Fix the name in case we have a full path and extension */
|
|
|
|
name = path;
|
2003-09-29 22:42:24 +02:00
|
|
|
if ((p = strrchrW( name, '\\' ))) name = p + 1;
|
|
|
|
if ((p = strrchrW( name, '/' ))) name = p + 1;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
/* load_library will modify info.status. Note also that load_library can be
|
2003-09-19 06:04:57 +02:00
|
|
|
* called several times, if the .so file we're loading has dependencies.
|
2003-10-08 00:56:34 +02:00
|
|
|
* info.status will gather all the errors we may get while loading all these
|
2003-09-19 06:04:57 +02:00
|
|
|
* libraries
|
|
|
|
*/
|
2003-10-08 00:56:34 +02:00
|
|
|
info.load_path = load_path;
|
2006-02-07 21:16:25 +01:00
|
|
|
info.filename = NULL;
|
2003-10-08 00:56:34 +02:00
|
|
|
info.status = STATUS_SUCCESS;
|
|
|
|
info.wm = NULL;
|
2006-02-07 21:16:25 +01:00
|
|
|
|
|
|
|
if (file) /* we have a real file, try to load it */
|
|
|
|
{
|
|
|
|
UNICODE_STRING nt_name;
|
|
|
|
ANSI_STRING unix_name;
|
|
|
|
|
2006-02-21 11:56:16 +01:00
|
|
|
TRACE("Trying built-in %s\n", debugstr_w(path));
|
|
|
|
|
2006-02-07 21:16:25 +01:00
|
|
|
if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
|
|
|
|
return STATUS_DLL_NOT_FOUND;
|
|
|
|
|
2006-02-09 12:15:57 +01:00
|
|
|
if (wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE ))
|
2006-02-07 21:16:25 +01:00
|
|
|
{
|
2006-02-09 12:15:57 +01:00
|
|
|
RtlFreeUnicodeString( &nt_name );
|
|
|
|
return STATUS_DLL_NOT_FOUND;
|
2006-02-07 21:16:25 +01:00
|
|
|
}
|
2006-02-09 12:15:57 +01:00
|
|
|
prev_info = builtin_load_info;
|
|
|
|
info.filename = nt_name.Buffer + 4; /* skip \??\ */
|
|
|
|
builtin_load_info = &info;
|
|
|
|
handle = wine_dlopen( unix_name.Buffer, RTLD_NOW, error, sizeof(error) );
|
|
|
|
builtin_load_info = prev_info;
|
2006-02-07 21:16:25 +01:00
|
|
|
RtlFreeUnicodeString( &nt_name );
|
2006-02-09 12:15:57 +01:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, unix_name.Buffer );
|
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
WARN( "failed to load .so lib for builtin %s: %s\n", debugstr_w(path), error );
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
2006-02-07 21:16:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-09 12:15:57 +01:00
|
|
|
int file_exists;
|
|
|
|
|
2006-02-21 11:56:16 +01:00
|
|
|
TRACE("Trying built-in %s\n", debugstr_w(name));
|
|
|
|
|
2006-02-07 21:16:25 +01:00
|
|
|
/* we don't want to depend on the current codepage here */
|
|
|
|
len = strlenW( name ) + 1;
|
|
|
|
if (len >= sizeof(dllname)) return STATUS_NAME_TOO_LONG;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (name[i] > 127) return STATUS_DLL_NOT_FOUND;
|
|
|
|
dllname[i] = (char)name[i];
|
|
|
|
if (dllname[i] >= 'A' && dllname[i] <= 'Z') dllname[i] += 'a' - 'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_info = builtin_load_info;
|
|
|
|
builtin_load_info = &info;
|
|
|
|
handle = wine_dll_load( dllname, error, sizeof(error), &file_exists );
|
|
|
|
builtin_load_info = prev_info;
|
2006-02-09 12:15:57 +01:00
|
|
|
if (!handle)
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
2006-02-09 12:15:57 +01:00
|
|
|
if (!file_exists)
|
|
|
|
{
|
|
|
|
/* The file does not exist -> WARN() */
|
|
|
|
WARN("cannot open .so lib for builtin %s: %s\n", debugstr_w(name), error);
|
|
|
|
return STATUS_DLL_NOT_FOUND;
|
|
|
|
}
|
|
|
|
/* ERR() for all other errors (missing functions, ...) */
|
|
|
|
ERR("failed to load .so lib for builtin %s: %s\n", debugstr_w(name), error );
|
|
|
|
return STATUS_PROCEDURE_NOT_FOUND;
|
2003-09-19 06:04:57 +02:00
|
|
|
}
|
|
|
|
}
|
2006-02-09 12:15:57 +01:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
if (info.status != STATUS_SUCCESS) return info.status;
|
2003-09-19 06:04:57 +02:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
if (!info.wm)
|
|
|
|
{
|
2006-02-21 19:43:22 +01:00
|
|
|
PLIST_ENTRY mark, entry;
|
2003-10-08 00:56:34 +02:00
|
|
|
|
2006-02-21 19:43:22 +01:00
|
|
|
/* The constructor wasn't called, this means the .so is already
|
|
|
|
* loaded under a different name. Try to find the wm for it. */
|
2005-05-23 12:33:34 +02:00
|
|
|
|
2006-02-21 19:43:22 +01:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
2006-02-07 21:16:25 +01:00
|
|
|
{
|
2006-02-21 19:43:22 +01:00
|
|
|
LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
if (mod->Flags & LDR_WINE_INTERNAL && mod->SectionHandle == handle)
|
|
|
|
{
|
|
|
|
info.wm = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
|
|
|
|
TRACE( "Found already loaded module %s for builtin %s\n",
|
|
|
|
debugstr_w(info.wm->ldr.FullDllName.Buffer), debugstr_w(path) );
|
|
|
|
break;
|
|
|
|
}
|
2006-02-07 21:16:25 +01:00
|
|
|
}
|
2006-02-21 19:43:22 +01:00
|
|
|
if (!info.wm) return STATUS_INVALID_IMAGE_FORMAT;
|
2006-02-28 12:41:25 +01:00
|
|
|
if (info.wm->ldr.LoadCount != -1) info.wm->ldr.LoadCount++;
|
2006-02-21 19:43:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE_(loaddll)( "Loaded module %s : builtin\n", debugstr_w(info.wm->ldr.FullDllName.Buffer) );
|
2006-02-28 12:41:25 +01:00
|
|
|
info.wm->ldr.LoadCount = 1;
|
2006-02-21 19:43:22 +01:00
|
|
|
info.wm->ldr.SectionHandle = handle;
|
2003-09-19 06:04:57 +02:00
|
|
|
}
|
2006-02-21 19:43:22 +01:00
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
*pwm = info.wm;
|
2003-09-19 06:04:57 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-07 05:48:42 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* find_dll_file
|
|
|
|
*
|
|
|
|
* Find the file (or already loaded module) for a given dll name.
|
|
|
|
*/
|
|
|
|
static NTSTATUS find_dll_file( const WCHAR *load_path, const WCHAR *libname,
|
|
|
|
WCHAR *filename, ULONG *size, WINE_MODREF **pwm, HANDLE *handle )
|
|
|
|
{
|
2004-04-13 01:31:09 +02:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
UNICODE_STRING nt_name;
|
2004-11-23 15:03:49 +01:00
|
|
|
WCHAR *file_part, *ext, *dllname;
|
2003-10-07 05:48:42 +02:00
|
|
|
ULONG len;
|
|
|
|
|
2004-11-23 15:03:49 +01:00
|
|
|
/* first append .dll if needed */
|
|
|
|
|
|
|
|
dllname = NULL;
|
|
|
|
if (!(ext = strrchrW( libname, '.')) || strchrW( ext, '/' ) || strchrW( ext, '\\'))
|
|
|
|
{
|
|
|
|
if (!(dllname = RtlAllocateHeap( GetProcessHeap(), 0,
|
|
|
|
(strlenW(libname) * sizeof(WCHAR)) + sizeof(dllW) )))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
strcpyW( dllname, libname );
|
|
|
|
strcatW( dllname, dllW );
|
|
|
|
libname = dllname;
|
|
|
|
}
|
|
|
|
|
2004-04-13 01:31:09 +02:00
|
|
|
nt_name.Buffer = NULL;
|
2006-01-18 14:22:19 +01:00
|
|
|
|
|
|
|
if (!contains_path( libname ))
|
|
|
|
{
|
|
|
|
if ((*pwm = find_basename_module( libname )) != NULL) goto found;
|
|
|
|
}
|
|
|
|
|
2003-10-07 05:48:42 +02:00
|
|
|
if (RtlDetermineDosPathNameType_U( libname ) == RELATIVE_PATH)
|
|
|
|
{
|
|
|
|
/* we need to search for it */
|
2004-11-23 15:03:49 +01:00
|
|
|
len = RtlDosSearchPath_U( load_path, libname, NULL, *size, filename, &file_part );
|
2003-10-07 05:48:42 +02:00
|
|
|
if (len)
|
|
|
|
{
|
2004-11-23 15:03:49 +01:00
|
|
|
if (len >= *size) goto overflow;
|
|
|
|
if ((*pwm = find_fullname_module( filename )) != NULL) goto found;
|
2003-10-07 05:48:42 +02:00
|
|
|
|
2004-04-13 01:31:09 +02:00
|
|
|
if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, NULL, NULL ))
|
2004-11-23 15:03:49 +01:00
|
|
|
{
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, dllname );
|
2004-04-13 01:31:09 +02:00
|
|
|
return STATUS_NO_MEMORY;
|
2004-11-23 15:03:49 +01:00
|
|
|
}
|
2004-04-13 01:31:09 +02:00
|
|
|
attr.Length = sizeof(attr);
|
|
|
|
attr.RootDirectory = 0;
|
|
|
|
attr.Attributes = OBJ_CASE_INSENSITIVE;
|
|
|
|
attr.ObjectName = &nt_name;
|
|
|
|
attr.SecurityDescriptor = NULL;
|
|
|
|
attr.SecurityQualityOfService = NULL;
|
2004-09-15 20:02:49 +02:00
|
|
|
if (NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_DELETE, 0 )) *handle = 0;
|
2004-11-23 15:03:49 +01:00
|
|
|
goto found;
|
2003-10-07 05:48:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
|
|
|
|
if (!contains_path( libname ))
|
|
|
|
{
|
|
|
|
/* if libname doesn't contain a path at all, we simply return the name as is,
|
|
|
|
* to be loaded as builtin */
|
|
|
|
len = strlenW(libname) * sizeof(WCHAR);
|
|
|
|
if (len >= *size) goto overflow;
|
|
|
|
strcpyW( filename, libname );
|
2004-11-23 15:03:49 +01:00
|
|
|
goto found;
|
2003-10-07 05:48:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* absolute path name, or relative path name but not found above */
|
|
|
|
|
2004-04-13 01:31:09 +02:00
|
|
|
if (!RtlDosPathNameToNtPathName_U( libname, &nt_name, &file_part, NULL ))
|
2004-11-23 15:03:49 +01:00
|
|
|
{
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, dllname );
|
2004-04-13 01:31:09 +02:00
|
|
|
return STATUS_NO_MEMORY;
|
2004-11-23 15:03:49 +01:00
|
|
|
}
|
2004-04-13 01:31:09 +02:00
|
|
|
len = nt_name.Length - 4*sizeof(WCHAR); /* for \??\ prefix */
|
2003-10-07 05:48:42 +02:00
|
|
|
if (len >= *size) goto overflow;
|
2004-04-13 01:31:09 +02:00
|
|
|
memcpy( filename, nt_name.Buffer + 4, len + sizeof(WCHAR) );
|
|
|
|
if (!(*pwm = find_fullname_module( filename )))
|
|
|
|
{
|
|
|
|
attr.Length = sizeof(attr);
|
|
|
|
attr.RootDirectory = 0;
|
|
|
|
attr.Attributes = OBJ_CASE_INSENSITIVE;
|
|
|
|
attr.ObjectName = &nt_name;
|
|
|
|
attr.SecurityDescriptor = NULL;
|
|
|
|
attr.SecurityQualityOfService = NULL;
|
2004-09-15 20:02:49 +02:00
|
|
|
if (NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_DELETE, 0 )) *handle = 0;
|
2004-04-13 01:31:09 +02:00
|
|
|
}
|
2004-11-23 15:03:49 +01:00
|
|
|
found:
|
2004-04-13 01:31:09 +02:00
|
|
|
RtlFreeUnicodeString( &nt_name );
|
2004-11-23 15:03:49 +01:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, dllname );
|
2003-10-07 05:48:42 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
overflow:
|
2004-04-13 01:31:09 +02:00
|
|
|
RtlFreeUnicodeString( &nt_name );
|
2004-11-23 15:03:49 +01:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, dllname );
|
2003-10-07 05:48:42 +02:00
|
|
|
*size = len + sizeof(WCHAR);
|
|
|
|
return STATUS_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-14 06:01:20 +01:00
|
|
|
/***********************************************************************
|
2003-04-05 07:16:11 +02:00
|
|
|
* load_dll (internal)
|
2003-03-14 06:01:20 +01:00
|
|
|
*
|
|
|
|
* Load a PE style module according to the load order.
|
2003-09-29 22:42:24 +02:00
|
|
|
* The loader_section must be locked while calling this function.
|
2003-03-14 06:01:20 +01:00
|
|
|
*/
|
2003-10-08 00:56:34 +02:00
|
|
|
static NTSTATUS load_dll( LPCWSTR load_path, LPCWSTR libname, DWORD flags, WINE_MODREF** pwm )
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
2006-02-22 16:30:05 +01:00
|
|
|
enum loadorder loadorder;
|
2003-10-07 05:48:42 +02:00
|
|
|
WCHAR buffer[32];
|
|
|
|
WCHAR *filename;
|
|
|
|
ULONG size;
|
2003-09-25 22:33:41 +02:00
|
|
|
WINE_MODREF *main_exe;
|
2004-04-13 01:31:09 +02:00
|
|
|
HANDLE handle = 0;
|
2003-10-07 05:48:42 +02:00
|
|
|
NTSTATUS nts;
|
2003-03-14 06:01:20 +01:00
|
|
|
|
2003-10-02 06:58:49 +02:00
|
|
|
TRACE( "looking for %s in %s\n", debugstr_w(libname), debugstr_w(load_path) );
|
2003-03-14 06:01:20 +01:00
|
|
|
|
2003-10-07 05:48:42 +02:00
|
|
|
filename = buffer;
|
|
|
|
size = sizeof(buffer);
|
|
|
|
for (;;)
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
2003-10-07 05:48:42 +02:00
|
|
|
nts = find_dll_file( load_path, libname, filename, &size, pwm, &handle );
|
|
|
|
if (nts == STATUS_SUCCESS) break;
|
|
|
|
if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
|
|
|
|
if (nts != STATUS_BUFFER_TOO_SMALL) return nts;
|
|
|
|
/* grow the buffer and retry */
|
|
|
|
if (!(filename = RtlAllocateHeap( GetProcessHeap(), 0, size ))) return STATUS_NO_MEMORY;
|
2003-03-14 06:01:20 +01:00
|
|
|
}
|
2003-10-07 05:48:42 +02:00
|
|
|
|
|
|
|
if (*pwm) /* found already loaded module */
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
2003-10-07 05:48:42 +02:00
|
|
|
if ((*pwm)->ldr.LoadCount != -1) (*pwm)->ldr.LoadCount++;
|
|
|
|
|
2006-02-21 19:32:26 +01:00
|
|
|
if (!(flags & DONT_RESOLVE_DLL_REFERENCES)) fixup_imports( *pwm, load_path );
|
|
|
|
|
2003-10-07 05:48:42 +02:00
|
|
|
TRACE("Found loaded module %s for %s at %p, count=%d\n",
|
|
|
|
debugstr_w((*pwm)->ldr.FullDllName.Buffer), debugstr_w(libname),
|
|
|
|
(*pwm)->ldr.BaseAddress, (*pwm)->ldr.LoadCount);
|
|
|
|
if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
|
|
|
|
return STATUS_SUCCESS;
|
2003-03-14 06:01:20 +01:00
|
|
|
}
|
|
|
|
|
2003-09-25 22:33:41 +02:00
|
|
|
main_exe = get_modref( NtCurrentTeb()->Peb->ImageBaseAddress );
|
2006-02-22 16:30:05 +01:00
|
|
|
loadorder = get_load_order( main_exe ? main_exe->ldr.BaseDllName.Buffer : NULL, filename );
|
2003-03-14 06:01:20 +01:00
|
|
|
|
2006-02-22 16:30:05 +01:00
|
|
|
switch(loadorder)
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
2006-02-22 16:30:05 +01:00
|
|
|
case LO_INVALID:
|
|
|
|
nts = STATUS_NO_MEMORY;
|
|
|
|
break;
|
|
|
|
case LO_DISABLED:
|
|
|
|
nts = STATUS_DLL_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
case LO_NATIVE:
|
|
|
|
case LO_NATIVE_BUILTIN:
|
|
|
|
if (!handle) nts = STATUS_DLL_NOT_FOUND;
|
|
|
|
else
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
2003-10-08 00:56:34 +02:00
|
|
|
nts = load_native_dll( load_path, filename, handle, flags, pwm );
|
2006-02-07 21:16:25 +01:00
|
|
|
if (nts == STATUS_INVALID_FILE_FOR_SECTION)
|
|
|
|
/* not in PE format, maybe it's a builtin */
|
|
|
|
nts = load_builtin_dll( load_path, filename, handle, flags, pwm );
|
2003-03-14 06:01:20 +01:00
|
|
|
}
|
2006-02-22 16:30:05 +01:00
|
|
|
if (nts == STATUS_DLL_NOT_FOUND && loadorder == LO_NATIVE_BUILTIN)
|
2006-02-21 11:56:16 +01:00
|
|
|
nts = load_builtin_dll( load_path, filename, 0, flags, pwm );
|
|
|
|
break;
|
2006-02-22 16:30:05 +01:00
|
|
|
case LO_BUILTIN:
|
|
|
|
case LO_BUILTIN_NATIVE:
|
|
|
|
case LO_DEFAULT: /* default is builtin,native */
|
2006-02-21 11:56:16 +01:00
|
|
|
nts = load_builtin_dll( load_path, filename, handle, flags, pwm );
|
|
|
|
if (!handle) break; /* nothing else we can try */
|
|
|
|
/* file is not a builtin library, try without using the specified file */
|
2006-03-21 15:10:22 +01:00
|
|
|
if (nts != STATUS_SUCCESS)
|
|
|
|
nts = load_builtin_dll( load_path, filename, 0, flags, pwm );
|
|
|
|
if (nts == STATUS_SUCCESS && loadorder == LO_DEFAULT &&
|
|
|
|
!MODULE_InitDLL( *pwm, DLL_WINE_PREATTACH, NULL ))
|
|
|
|
{
|
|
|
|
/* stub-only dll, try native */
|
|
|
|
TRACE( "%s pre-attach returned FALSE, preferring native\n", debugstr_w(filename) );
|
|
|
|
LdrUnloadDll( (*pwm)->ldr.BaseAddress );
|
|
|
|
nts = STATUS_DLL_NOT_FOUND;
|
|
|
|
}
|
2006-02-22 16:30:05 +01:00
|
|
|
if (nts == STATUS_DLL_NOT_FOUND && loadorder != LO_BUILTIN)
|
2006-02-21 11:56:16 +01:00
|
|
|
nts = load_native_dll( load_path, filename, handle, flags, pwm );
|
|
|
|
break;
|
|
|
|
}
|
2003-03-14 06:01:20 +01:00
|
|
|
|
2006-02-21 11:56:16 +01:00
|
|
|
if (nts == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Initialize DLL just loaded */
|
|
|
|
TRACE("Loaded module %s (%s) at %p\n", debugstr_w(filename),
|
|
|
|
((*pwm)->ldr.Flags & LDR_WINE_INTERNAL) ? "builtin" : "native",
|
|
|
|
(*pwm)->ldr.BaseAddress);
|
|
|
|
if (handle) NtClose( handle );
|
|
|
|
if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
|
|
|
|
return nts;
|
2003-03-14 06:01:20 +01:00
|
|
|
}
|
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
WARN("Failed to load module %s; status=%x\n", debugstr_w(libname), nts);
|
2004-04-13 01:31:09 +02:00
|
|
|
if (handle) NtClose( handle );
|
2003-10-07 05:48:42 +02:00
|
|
|
if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
|
2003-10-02 06:58:49 +02:00
|
|
|
return nts;
|
2003-03-14 06:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* LdrLoadDll (NTDLL.@)
|
|
|
|
*/
|
2003-09-29 22:42:24 +02:00
|
|
|
NTSTATUS WINAPI LdrLoadDll(LPCWSTR path_name, DWORD flags,
|
|
|
|
const UNICODE_STRING *libname, HMODULE* hModule)
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
2003-09-29 22:42:24 +02:00
|
|
|
NTSTATUS nts;
|
2003-03-14 06:01:20 +01:00
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
if (!path_name) path_name = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
|
|
|
|
nts = load_dll( path_name, libname->Buffer, flags, &wm );
|
2003-09-29 22:42:24 +02:00
|
|
|
|
|
|
|
if (nts == STATUS_SUCCESS && !(wm->ldr.Flags & LDR_DONT_RESOLVE_REFS))
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
2003-10-06 23:18:51 +02:00
|
|
|
nts = process_attach( wm, NULL );
|
2003-05-14 21:51:14 +02:00
|
|
|
if (nts != STATUS_SUCCESS)
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
2003-03-20 23:05:26 +01:00
|
|
|
LdrUnloadDll(wm->ldr.BaseAddress);
|
2003-03-14 06:01:20 +01:00
|
|
|
wm = NULL;
|
|
|
|
}
|
|
|
|
}
|
2003-03-20 23:05:26 +01:00
|
|
|
*hModule = (wm) ? wm->ldr.BaseAddress : NULL;
|
2003-03-14 06:01:20 +01:00
|
|
|
|
2003-09-29 22:42:24 +02:00
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
2003-03-14 06:01:20 +01:00
|
|
|
return nts;
|
|
|
|
}
|
|
|
|
|
2003-03-21 01:34:36 +01:00
|
|
|
/******************************************************************
|
|
|
|
* LdrQueryProcessModuleInformation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI LdrQueryProcessModuleInformation(PSYSTEM_MODULE_INFORMATION smi,
|
|
|
|
ULONG buf_size, ULONG* req_size)
|
|
|
|
{
|
|
|
|
SYSTEM_MODULE* sm = &smi->Modules[0];
|
|
|
|
ULONG size = sizeof(ULONG);
|
|
|
|
NTSTATUS nts = STATUS_SUCCESS;
|
|
|
|
ANSI_STRING str;
|
|
|
|
char* ptr;
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
PLDR_MODULE mod;
|
2003-03-21 01:34:36 +01:00
|
|
|
|
|
|
|
smi->ModulesCount = 0;
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
2003-07-01 05:38:56 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
2003-03-21 01:34:36 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
2003-03-21 01:34:36 +01:00
|
|
|
size += sizeof(*sm);
|
|
|
|
if (size <= buf_size)
|
|
|
|
{
|
|
|
|
sm->Reserved1 = 0; /* FIXME */
|
|
|
|
sm->Reserved2 = 0; /* FIXME */
|
2003-07-01 05:38:56 +02:00
|
|
|
sm->ImageBaseAddress = mod->BaseAddress;
|
|
|
|
sm->ImageSize = mod->SizeOfImage;
|
|
|
|
sm->Flags = mod->Flags;
|
2003-03-21 01:34:36 +01:00
|
|
|
sm->Id = 0; /* FIXME */
|
|
|
|
sm->Rank = 0; /* FIXME */
|
|
|
|
sm->Unknown = 0; /* FIXME */
|
|
|
|
str.Length = 0;
|
|
|
|
str.MaximumLength = MAXIMUM_FILENAME_LENGTH;
|
2005-09-02 16:47:36 +02:00
|
|
|
str.Buffer = (char*)sm->Name;
|
2003-07-01 05:38:56 +02:00
|
|
|
RtlUnicodeStringToAnsiString(&str, &mod->FullDllName, FALSE);
|
2005-09-02 16:47:36 +02:00
|
|
|
ptr = strrchr(str.Buffer, '\\');
|
|
|
|
sm->NameOffset = (ptr != NULL) ? (ptr - str.Buffer + 1) : 0;
|
2003-03-21 01:34:36 +01:00
|
|
|
|
|
|
|
smi->ModulesCount++;
|
|
|
|
sm++;
|
|
|
|
}
|
|
|
|
else nts = STATUS_INFO_LENGTH_MISMATCH;
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
if (req_size) *req_size = size;
|
|
|
|
|
|
|
|
return nts;
|
|
|
|
}
|
|
|
|
|
2006-11-03 13:52:02 +01:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* RtlDllShutdownInProgress (NTDLL.@)
|
|
|
|
*/
|
|
|
|
BOOLEAN WINAPI RtlDllShutdownInProgress(void)
|
|
|
|
{
|
|
|
|
return process_detaching;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-04 05:36:56 +01:00
|
|
|
/******************************************************************
|
|
|
|
* LdrShutdownProcess (NTDLL.@)
|
|
|
|
*
|
|
|
|
*/
|
2003-05-08 06:13:26 +02:00
|
|
|
void WINAPI LdrShutdownProcess(void)
|
2003-03-04 05:36:56 +01:00
|
|
|
{
|
|
|
|
TRACE("()\n");
|
2003-10-06 23:18:51 +02:00
|
|
|
process_detach( TRUE, (LPVOID)1 );
|
2003-03-04 05:36:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* LdrShutdownThread (NTDLL.@)
|
|
|
|
*
|
|
|
|
*/
|
2003-05-08 06:13:26 +02:00
|
|
|
void WINAPI LdrShutdownThread(void)
|
2003-03-04 05:36:56 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
PLDR_MODULE mod;
|
|
|
|
|
2003-03-04 05:36:56 +01:00
|
|
|
TRACE("()\n");
|
|
|
|
|
|
|
|
/* don't do any detach calls if process is exiting */
|
2003-05-08 06:13:26 +02:00
|
|
|
if (process_detaching) return;
|
2003-03-04 05:36:56 +01:00
|
|
|
/* FIXME: there is still a race here */
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
|
|
|
|
for (entry = mark->Blink; entry != mark; entry = entry->Blink)
|
2003-03-04 05:36:56 +01:00
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE,
|
|
|
|
InInitializationOrderModuleList);
|
|
|
|
if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
|
2003-03-04 05:36:56 +01:00
|
|
|
continue;
|
2003-07-01 05:38:56 +02:00
|
|
|
if ( mod->Flags & LDR_NO_DLL_CALLS )
|
2003-03-04 05:36:56 +01:00
|
|
|
continue;
|
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
|
|
|
|
DLL_THREAD_DETACH, NULL );
|
2003-03-04 05:36:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
2006-05-26 12:10:41 +02:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->ThreadLocalStoragePointer );
|
2003-03-04 05:36:56 +01:00
|
|
|
}
|
|
|
|
|
2006-03-18 18:41:21 +01:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* free_modref
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void free_modref( WINE_MODREF *wm )
|
|
|
|
{
|
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
if (wm->ldr.InInitializationOrderModuleList.Flink)
|
|
|
|
RemoveEntryList(&wm->ldr.InInitializationOrderModuleList);
|
|
|
|
|
|
|
|
TRACE(" unloading %s\n", debugstr_w(wm->ldr.FullDllName.Buffer));
|
|
|
|
if (!TRACE_ON(module))
|
|
|
|
TRACE_(loaddll)("Unloaded module %s : %s\n",
|
|
|
|
debugstr_w(wm->ldr.FullDllName.Buffer),
|
|
|
|
(wm->ldr.Flags & LDR_WINE_INTERNAL) ? "builtin" : "native" );
|
|
|
|
|
|
|
|
SERVER_START_REQ( unload_dll )
|
|
|
|
{
|
|
|
|
req->base = wm->ldr.BaseAddress;
|
|
|
|
wine_server_call( req );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
NtUnmapViewOfSection( NtCurrentProcess(), wm->ldr.BaseAddress );
|
|
|
|
if (wm->ldr.Flags & LDR_WINE_INTERNAL) wine_dll_unload( wm->ldr.SectionHandle );
|
|
|
|
if (cached_modref == wm) cached_modref = NULL;
|
|
|
|
RtlFreeUnicodeString( &wm->ldr.FullDllName );
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, wm->deps );
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, wm );
|
|
|
|
}
|
|
|
|
|
2003-03-14 05:00:52 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* MODULE_FlushModrefs
|
|
|
|
*
|
|
|
|
* Remove all unused modrefs and call the internal unloading routines
|
|
|
|
* for the library type.
|
2003-04-05 07:16:11 +02:00
|
|
|
*
|
|
|
|
* The loader_section must be locked while calling this function.
|
2003-03-14 05:00:52 +01:00
|
|
|
*/
|
|
|
|
static void MODULE_FlushModrefs(void)
|
|
|
|
{
|
2003-07-01 05:38:56 +02:00
|
|
|
PLIST_ENTRY mark, entry, prev;
|
|
|
|
PLDR_MODULE mod;
|
|
|
|
WINE_MODREF*wm;
|
2003-03-14 05:00:52 +01:00
|
|
|
|
2003-07-01 05:38:56 +02:00
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
|
|
|
|
for (entry = mark->Blink; entry != mark; entry = prev)
|
2003-03-14 05:00:52 +01:00
|
|
|
{
|
2006-03-18 18:41:21 +01:00
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE, InInitializationOrderModuleList);
|
2003-07-01 05:38:56 +02:00
|
|
|
wm = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
|
|
|
|
prev = entry->Blink;
|
2006-03-18 18:41:21 +01:00
|
|
|
if (!mod->LoadCount) free_modref( wm );
|
|
|
|
}
|
2003-03-14 05:00:52 +01:00
|
|
|
|
2006-03-18 18:41:21 +01:00
|
|
|
/* check load order list too for modules that haven't been initialized yet */
|
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
for (entry = mark->Blink; entry != mark; entry = prev)
|
|
|
|
{
|
|
|
|
mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
wm = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
|
|
|
|
prev = entry->Blink;
|
|
|
|
if (!mod->LoadCount) free_modref( wm );
|
2003-03-14 05:00:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* MODULE_DecRefCount
|
|
|
|
*
|
2003-04-05 07:16:11 +02:00
|
|
|
* The loader_section must be locked while calling this function.
|
2003-03-14 05:00:52 +01:00
|
|
|
*/
|
|
|
|
static void MODULE_DecRefCount( WINE_MODREF *wm )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2003-03-21 00:40:08 +01:00
|
|
|
if ( wm->ldr.Flags & LDR_UNLOAD_IN_PROGRESS )
|
2003-03-14 05:00:52 +01:00
|
|
|
return;
|
|
|
|
|
2003-03-20 23:05:26 +01:00
|
|
|
if ( wm->ldr.LoadCount <= 0 )
|
2003-03-14 05:00:52 +01:00
|
|
|
return;
|
|
|
|
|
2003-03-20 23:05:26 +01:00
|
|
|
--wm->ldr.LoadCount;
|
2003-09-29 22:42:24 +02:00
|
|
|
TRACE("(%s) ldr.LoadCount: %d\n", debugstr_w(wm->ldr.BaseDllName.Buffer), wm->ldr.LoadCount );
|
2003-03-14 05:00:52 +01:00
|
|
|
|
2003-03-20 23:05:26 +01:00
|
|
|
if ( wm->ldr.LoadCount == 0 )
|
2003-03-14 05:00:52 +01:00
|
|
|
{
|
2003-03-21 00:40:08 +01:00
|
|
|
wm->ldr.Flags |= LDR_UNLOAD_IN_PROGRESS;
|
2003-03-14 05:00:52 +01:00
|
|
|
|
|
|
|
for ( i = 0; i < wm->nDeps; i++ )
|
|
|
|
if ( wm->deps[i] )
|
|
|
|
MODULE_DecRefCount( wm->deps[i] );
|
|
|
|
|
2003-03-21 00:40:08 +01:00
|
|
|
wm->ldr.Flags &= ~LDR_UNLOAD_IN_PROGRESS;
|
2003-03-14 05:00:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* LdrUnloadDll (NTDLL.@)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI LdrUnloadDll( HMODULE hModule )
|
|
|
|
{
|
|
|
|
NTSTATUS retv = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
TRACE("(%p)\n", hModule);
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
/* if we're stopping the whole process (and forcing the removal of all
|
|
|
|
* DLLs) the library will be freed anyway
|
|
|
|
*/
|
|
|
|
if (!process_detaching)
|
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
|
|
|
|
free_lib_count++;
|
2003-04-05 07:16:11 +02:00
|
|
|
if ((wm = get_modref( hModule )) != NULL)
|
2003-03-14 05:00:52 +01:00
|
|
|
{
|
2003-09-29 22:42:24 +02:00
|
|
|
TRACE("(%s) - START\n", debugstr_w(wm->ldr.BaseDllName.Buffer));
|
2003-03-14 05:00:52 +01:00
|
|
|
|
|
|
|
/* Recursively decrement reference counts */
|
|
|
|
MODULE_DecRefCount( wm );
|
|
|
|
|
|
|
|
/* Call process detach notifications */
|
|
|
|
if ( free_lib_count <= 1 )
|
|
|
|
{
|
2003-10-06 23:18:51 +02:00
|
|
|
process_detach( FALSE, NULL );
|
2003-03-14 05:00:52 +01:00
|
|
|
MODULE_FlushModrefs();
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("END\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
retv = STATUS_DLL_NOT_FOUND;
|
|
|
|
|
|
|
|
free_lib_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2002-08-09 21:57:38 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* RtlImageNtHeader (NTDLL.@)
|
|
|
|
*/
|
|
|
|
PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
|
|
|
|
{
|
2002-08-16 00:09:53 +02:00
|
|
|
IMAGE_NT_HEADERS *ret;
|
2002-08-09 21:57:38 +02:00
|
|
|
|
2002-08-16 00:09:53 +02:00
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)hModule;
|
|
|
|
|
|
|
|
ret = NULL;
|
|
|
|
if (dos->e_magic == IMAGE_DOS_SIGNATURE)
|
|
|
|
{
|
|
|
|
ret = (IMAGE_NT_HEADERS *)((char *)dos + dos->e_lfanew);
|
|
|
|
if (ret->Signature != IMAGE_NT_SIGNATURE) ret = NULL;
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2002-08-09 21:57:38 +02:00
|
|
|
{
|
2002-08-16 00:09:53 +02:00
|
|
|
return NULL;
|
2002-08-09 21:57:38 +02:00
|
|
|
}
|
2002-08-16 00:09:53 +02:00
|
|
|
__ENDTRY
|
2002-08-09 21:57:38 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-06 23:18:51 +02:00
|
|
|
/******************************************************************
|
|
|
|
* LdrInitializeThunk (NTDLL.@)
|
|
|
|
*
|
|
|
|
*/
|
2006-02-08 15:22:24 +01:00
|
|
|
void WINAPI LdrInitializeThunk( ULONG unknown1, ULONG unknown2, ULONG unknown3, ULONG unknown4 )
|
2003-10-06 23:18:51 +02:00
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
WINE_MODREF *wm;
|
2003-10-08 00:56:34 +02:00
|
|
|
LPCWSTR load_path;
|
2003-10-06 23:18:51 +02:00
|
|
|
PEB *peb = NtCurrentTeb()->Peb;
|
|
|
|
IMAGE_NT_HEADERS *nt = RtlImageNtHeader( peb->ImageBaseAddress );
|
|
|
|
|
2006-02-22 09:57:19 +01:00
|
|
|
if (main_exe_file) NtClose( main_exe_file ); /* at this point the main module is created */
|
|
|
|
|
2006-02-08 15:10:56 +01:00
|
|
|
/* allocate the modref for the main exe (if not already done) */
|
2006-02-21 19:48:18 +01:00
|
|
|
wm = get_modref( peb->ImageBaseAddress );
|
|
|
|
assert( wm );
|
|
|
|
if (wm->ldr.Flags & LDR_IMAGE_IS_DLL)
|
2003-10-06 23:18:51 +02:00
|
|
|
{
|
2006-02-21 19:48:18 +01:00
|
|
|
ERR("%s is a dll, not an executable\n", debugstr_w(wm->ldr.FullDllName.Buffer) );
|
|
|
|
exit(1);
|
2003-10-06 23:18:51 +02:00
|
|
|
}
|
|
|
|
wm->ldr.LoadCount = -1; /* can't unload main exe */
|
2006-02-08 15:10:56 +01:00
|
|
|
|
|
|
|
peb->ProcessParameters->ImagePathName = wm->ldr.FullDllName;
|
|
|
|
version_init( wm->ldr.FullDllName.Buffer );
|
2003-10-06 23:18:51 +02:00
|
|
|
|
2003-11-25 06:28:57 +01:00
|
|
|
/* the main exe needs to be the first in the load order list */
|
|
|
|
RemoveEntryList( &wm->ldr.InLoadOrderModuleList );
|
|
|
|
InsertHeadList( &peb->LdrData->InLoadOrderModuleList, &wm->ldr.InLoadOrderModuleList );
|
|
|
|
|
2003-10-06 23:18:51 +02:00
|
|
|
/* Install signal handlers; this cannot be done before, since we cannot
|
|
|
|
* send exceptions to the debugger before the create process event that
|
|
|
|
* is sent by REQ_INIT_PROCESS_DONE.
|
|
|
|
* We do need the handlers in place by the time the request is over, so
|
|
|
|
* we set them up here. If we segfault between here and the server call
|
|
|
|
* something is very wrong... */
|
|
|
|
if (!SIGNAL_Init()) exit(1);
|
|
|
|
|
|
|
|
/* Signal the parent process to continue */
|
|
|
|
SERVER_START_REQ( init_process_done )
|
|
|
|
{
|
2006-07-13 14:58:04 +02:00
|
|
|
req->module = peb->ImageBaseAddress;
|
2006-07-31 21:25:29 +02:00
|
|
|
req->entry = (char *)peb->ImageBaseAddress + nt->OptionalHeader.AddressOfEntryPoint;
|
2006-07-13 14:58:04 +02:00
|
|
|
req->gui = (nt->OptionalHeader.Subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI);
|
2006-02-21 20:08:19 +01:00
|
|
|
status = wine_server_call( req );
|
2003-10-06 23:18:51 +02:00
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
2006-02-21 20:08:19 +01:00
|
|
|
if (status != STATUS_SUCCESS) goto error;
|
|
|
|
|
2003-10-06 23:18:51 +02:00
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
2003-10-08 00:56:34 +02:00
|
|
|
load_path = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
|
|
|
|
if ((status = fixup_imports( wm, load_path )) != STATUS_SUCCESS) goto error;
|
2003-10-06 23:18:51 +02:00
|
|
|
if ((status = alloc_process_tls()) != STATUS_SUCCESS) goto error;
|
|
|
|
if ((status = alloc_thread_tls()) != STATUS_SUCCESS) goto error;
|
2005-01-11 17:01:31 +01:00
|
|
|
if ((status = process_attach( wm, (LPVOID)1 )) != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
if (last_failed_modref)
|
|
|
|
ERR( "%s failed to initialize, aborting\n", debugstr_w(last_failed_modref->ldr.BaseDllName.Buffer) + 1 );
|
|
|
|
goto error;
|
|
|
|
}
|
2005-10-26 14:12:44 +02:00
|
|
|
attach_implicitly_loaded_dlls( (LPVOID)1 );
|
2003-10-06 23:18:51 +02:00
|
|
|
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
2004-06-18 02:26:57 +02:00
|
|
|
|
|
|
|
if (nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE) VIRTUAL_UseLargeAddressSpace();
|
2006-07-31 21:25:29 +02:00
|
|
|
return;
|
2003-10-06 23:18:51 +02:00
|
|
|
|
|
|
|
error:
|
2006-10-16 13:49:06 +02:00
|
|
|
ERR( "Main exe initialization for %s failed, status %x\n",
|
2006-02-08 15:10:56 +01:00
|
|
|
debugstr_w(peb->ProcessParameters->ImagePathName.Buffer), status );
|
2003-10-06 23:18:51 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-09 21:57:38 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* RtlImageDirectoryEntryToData (NTDLL.@)
|
|
|
|
*/
|
|
|
|
PVOID WINAPI RtlImageDirectoryEntryToData( HMODULE module, BOOL image, WORD dir, ULONG *size )
|
|
|
|
{
|
|
|
|
const IMAGE_NT_HEADERS *nt;
|
|
|
|
DWORD addr;
|
|
|
|
|
|
|
|
if ((ULONG_PTR)module & 1) /* mapped as data file */
|
|
|
|
{
|
|
|
|
module = (HMODULE)((ULONG_PTR)module & ~1);
|
|
|
|
image = FALSE;
|
|
|
|
}
|
|
|
|
if (!(nt = RtlImageNtHeader( module ))) return NULL;
|
|
|
|
if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
|
|
|
|
if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
|
|
|
|
*size = nt->OptionalHeader.DataDirectory[dir].Size;
|
|
|
|
if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)module + addr;
|
|
|
|
|
|
|
|
/* not mapped as image, need to find the section containing the virtual address */
|
|
|
|
return RtlImageRvaToVa( nt, module, addr, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlImageRvaToSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
PIMAGE_SECTION_HEADER WINAPI RtlImageRvaToSection( const IMAGE_NT_HEADERS *nt,
|
|
|
|
HMODULE module, DWORD rva )
|
|
|
|
{
|
|
|
|
int i;
|
2004-06-15 02:47:00 +02:00
|
|
|
const IMAGE_SECTION_HEADER *sec;
|
|
|
|
|
|
|
|
sec = (const IMAGE_SECTION_HEADER*)((const char*)&nt->OptionalHeader +
|
|
|
|
nt->FileHeader.SizeOfOptionalHeader);
|
2002-08-09 21:57:38 +02:00
|
|
|
for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
|
|
|
|
{
|
|
|
|
if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
|
2004-06-15 02:47:00 +02:00
|
|
|
return (PIMAGE_SECTION_HEADER)sec;
|
2002-08-09 21:57:38 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlImageRvaToVa (NTDLL.@)
|
|
|
|
*/
|
|
|
|
PVOID WINAPI RtlImageRvaToVa( const IMAGE_NT_HEADERS *nt, HMODULE module,
|
|
|
|
DWORD rva, IMAGE_SECTION_HEADER **section )
|
|
|
|
{
|
|
|
|
IMAGE_SECTION_HEADER *sec;
|
|
|
|
|
|
|
|
if (section && *section) /* try this section first */
|
|
|
|
{
|
|
|
|
sec = *section;
|
|
|
|
if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
if (!(sec = RtlImageRvaToSection( nt, module, rva ))) return NULL;
|
|
|
|
found:
|
|
|
|
if (section) *section = sec;
|
|
|
|
return (char *)module + sec->PointerToRawData + (rva - sec->VirtualAddress);
|
|
|
|
}
|
2003-09-19 06:04:57 +02:00
|
|
|
|
|
|
|
|
2006-05-27 10:55:48 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* RtlPcToFileHeader (NTDLL.@)
|
|
|
|
*/
|
|
|
|
PVOID WINAPI RtlPcToFileHeader( PVOID pc, PVOID *address )
|
|
|
|
{
|
|
|
|
LDR_MODULE *module;
|
|
|
|
PVOID ret = NULL;
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
if (!LdrFindEntryForAddress( pc, &module )) ret = module->BaseAddress;
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
*address = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-24 13:49:53 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* NtLoadDriver (NTDLL.@)
|
|
|
|
* ZwLoadDriver (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtLoadDriver( const UNICODE_STRING *DriverServiceName )
|
|
|
|
{
|
|
|
|
FIXME("(%p), stub!\n",DriverServiceName);
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtUnloadDriver (NTDLL.@)
|
|
|
|
* ZwUnloadDriver (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtUnloadDriver( const UNICODE_STRING *DriverServiceName )
|
|
|
|
{
|
|
|
|
FIXME("(%p), stub!\n",DriverServiceName);
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-29 17:11:37 +02:00
|
|
|
/******************************************************************
|
|
|
|
* DllMain (NTDLL.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
|
|
|
|
{
|
|
|
|
if (reason == DLL_PROCESS_ATTACH) LdrDisableThreadCalloutsForDll( inst );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-16 00:07:21 +02:00
|
|
|
/******************************************************************
|
|
|
|
* __wine_init_windows_dir (NTDLL.@)
|
|
|
|
*
|
|
|
|
* Windows and system dir initialization once kernel32 has been loaded.
|
|
|
|
*/
|
|
|
|
void __wine_init_windows_dir( const WCHAR *windir, const WCHAR *sysdir )
|
|
|
|
{
|
|
|
|
PLIST_ENTRY mark, entry;
|
|
|
|
LPWSTR buffer, p;
|
|
|
|
|
|
|
|
RtlCreateUnicodeString( &system_dir, sysdir );
|
|
|
|
|
|
|
|
/* prepend the system dir to the name of the already created modules */
|
|
|
|
mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
{
|
|
|
|
LDR_MODULE *mod = CONTAINING_RECORD( entry, LDR_MODULE, InLoadOrderModuleList );
|
|
|
|
|
|
|
|
assert( mod->Flags & LDR_WINE_INTERNAL );
|
|
|
|
|
|
|
|
buffer = RtlAllocateHeap( GetProcessHeap(), 0,
|
|
|
|
system_dir.Length + mod->FullDllName.Length + 2*sizeof(WCHAR) );
|
|
|
|
if (!buffer) continue;
|
|
|
|
strcpyW( buffer, system_dir.Buffer );
|
|
|
|
p = buffer + strlenW( buffer );
|
|
|
|
if (p > buffer && p[-1] != '\\') *p++ = '\\';
|
|
|
|
strcpyW( p, mod->FullDllName.Buffer );
|
|
|
|
RtlInitUnicodeString( &mod->FullDllName, buffer );
|
|
|
|
RtlInitUnicodeString( &mod->BaseDllName, p );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-19 06:04:57 +02:00
|
|
|
/***********************************************************************
|
2003-10-10 02:12:17 +02:00
|
|
|
* __wine_process_init
|
2003-09-19 06:04:57 +02:00
|
|
|
*/
|
2006-04-03 12:07:20 +02:00
|
|
|
void __wine_process_init(void)
|
2003-09-19 06:04:57 +02:00
|
|
|
{
|
2003-10-10 02:12:17 +02:00
|
|
|
static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
|
2003-10-08 00:56:34 +02:00
|
|
|
|
2003-10-10 02:12:17 +02:00
|
|
|
WINE_MODREF *wm;
|
|
|
|
NTSTATUS status;
|
|
|
|
ANSI_STRING func_name;
|
2006-02-06 13:13:28 +01:00
|
|
|
void (* DECLSPEC_NORETURN init_func)(void);
|
2004-04-20 02:36:29 +02:00
|
|
|
extern mode_t FILE_umask;
|
2003-10-10 02:12:17 +02:00
|
|
|
|
2006-02-22 09:57:19 +01:00
|
|
|
main_exe_file = thread_init();
|
2003-10-10 02:12:17 +02:00
|
|
|
|
2004-04-20 02:36:29 +02:00
|
|
|
/* retrieve current umask */
|
|
|
|
FILE_umask = umask(0777);
|
|
|
|
umask( FILE_umask );
|
|
|
|
|
2003-10-10 02:12:17 +02:00
|
|
|
/* setup the load callback and create ntdll modref */
|
2003-09-19 06:04:57 +02:00
|
|
|
wine_dll_set_callback( load_builtin_callback );
|
2003-10-10 02:12:17 +02:00
|
|
|
|
2006-02-07 21:16:25 +01:00
|
|
|
if ((status = load_builtin_dll( NULL, kernel32W, 0, 0, &wm )) != STATUS_SUCCESS)
|
2003-10-10 02:12:17 +02:00
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
MESSAGE( "wine: could not load kernel32.dll, status %x\n", status );
|
2003-10-10 02:12:17 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
RtlInitAnsiString( &func_name, "__wine_kernel_init" );
|
|
|
|
if ((status = LdrGetProcedureAddress( wm->ldr.BaseAddress, &func_name,
|
|
|
|
0, (void **)&init_func )) != STATUS_SUCCESS)
|
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
MESSAGE( "wine: could not find __wine_kernel_init in kernel32.dll, status %x\n", status );
|
2003-10-10 02:12:17 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
init_func();
|
2003-09-19 06:04:57 +02:00
|
|
|
}
|