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
|
2002-03-10 00:29:33 +01:00
|
|
|
* Copyright 2002 Dmitry Timoshkov for Codeweavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2003-03-18 19:29:13 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2002-01-29 19:30:16 +01:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnt.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-06-10 04:28:00 +02:00
|
|
|
|
|
|
|
#include "module.h"
|
2003-03-07 00:41:37 +01:00
|
|
|
#include "file.h"
|
2002-08-16 00:09:53 +02:00
|
|
|
#include "wine/exception.h"
|
2002-12-13 00:34:01 +01:00
|
|
|
#include "excpt.h"
|
2003-04-05 07:16:11 +02:00
|
|
|
#include "snoop.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);
|
|
|
|
|
2003-04-04 21:50:17 +02:00
|
|
|
typedef DWORD (CALLBACK *DLLENTRYPROC)(HMODULE,DWORD,LPVOID);
|
|
|
|
|
2003-03-20 05:55:59 +01:00
|
|
|
WINE_MODREF *MODULE_modref_list = NULL;
|
|
|
|
|
|
|
|
static WINE_MODREF *exe_modref;
|
|
|
|
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
|
|
|
|
2002-08-16 00:09:53 +02:00
|
|
|
/* filter for page-fault exceptions */
|
|
|
|
static WINE_EXCEPTION_FILTER(page_fault)
|
|
|
|
{
|
|
|
|
if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
2003-05-08 06:13:26 +02:00
|
|
|
static const char * const reason_names[] =
|
|
|
|
{
|
|
|
|
"PROCESS_DETACH",
|
|
|
|
"PROCESS_ATTACH",
|
|
|
|
"THREAD_ATTACH",
|
|
|
|
"THREAD_DETACH"
|
|
|
|
};
|
|
|
|
|
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 */
|
|
|
|
|
2003-03-20 05:55:59 +01:00
|
|
|
static CRITICAL_SECTION loader_section = CRITICAL_SECTION_INIT( "loader_section" );
|
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;
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
static NTSTATUS load_dll( LPCSTR libname, DWORD flags, WINE_MODREF** pwm );
|
|
|
|
static FARPROC find_named_export( HMODULE module, IMAGE_EXPORT_DIRECTORY *exports,
|
|
|
|
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-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-04-05 07:16:11 +02:00
|
|
|
WINE_MODREF *wm;
|
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
|
|
|
|
|
|
|
for ( wm = MODULE_modref_list; wm; wm=wm->next )
|
2003-04-05 07:16:11 +02:00
|
|
|
{
|
|
|
|
if (wm->ldr.BaseAddress == hmod)
|
|
|
|
{
|
|
|
|
cached_modref = wm;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* 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 )
|
|
|
|
{
|
|
|
|
IMAGE_EXPORT_DIRECTORY *exports;
|
|
|
|
DWORD exp_size;
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
char mod_name[256];
|
|
|
|
char *end = strchr(forward, '.');
|
|
|
|
FARPROC proc = NULL;
|
|
|
|
|
|
|
|
if (!end) return NULL;
|
|
|
|
if (end - forward >= sizeof(mod_name)) return NULL;
|
|
|
|
memcpy( mod_name, forward, end - forward );
|
|
|
|
mod_name[end-forward] = 0;
|
|
|
|
|
|
|
|
if (!(wm = MODULE_FindModule( mod_name )))
|
|
|
|
{
|
2003-05-13 02:28:25 +02:00
|
|
|
ERR("module not found for forward '%s' used by '%s'\n",
|
|
|
|
forward, get_modref(module)->filename );
|
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)
|
|
|
|
{
|
|
|
|
ERR("function not found for forward '%s' used by '%s'."
|
|
|
|
" If you are using builtin '%s', try using the native one instead.\n",
|
2003-05-13 02:28:25 +02:00
|
|
|
forward, get_modref(module)->filename, get_modref(module)->modname );
|
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.
|
|
|
|
*/
|
|
|
|
static FARPROC find_ordinal_export( HMODULE module, IMAGE_EXPORT_DIRECTORY *exports,
|
|
|
|
DWORD exp_size, int ordinal )
|
|
|
|
{
|
|
|
|
FARPROC proc;
|
|
|
|
DWORD *functions = get_rva( module, exports->AddressOfFunctions );
|
|
|
|
|
|
|
|
if (ordinal >= exports->NumberOfFunctions)
|
|
|
|
{
|
|
|
|
TRACE(" ordinal %ld out of range!\n", ordinal + exports->Base );
|
|
|
|
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 */
|
|
|
|
if (((char *)proc >= (char *)exports) && ((char *)proc < (char *)exports + exp_size))
|
|
|
|
return find_forwarded_export( module, (char *)proc );
|
|
|
|
|
|
|
|
if (TRACE_ON(snoop))
|
|
|
|
{
|
2003-04-08 01:27:54 +02:00
|
|
|
proc = SNOOP_GetProcAddress( module, exports, exp_size, proc, ordinal );
|
|
|
|
}
|
|
|
|
if (TRACE_ON(relay) && current_modref)
|
|
|
|
{
|
|
|
|
proc = RELAY_GetProcAddress( module, exports, exp_size, proc, current_modref->modname );
|
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.
|
|
|
|
*/
|
|
|
|
static FARPROC find_named_export( HMODULE module, IMAGE_EXPORT_DIRECTORY *exports,
|
|
|
|
DWORD exp_size, const char *name, int hint )
|
|
|
|
{
|
|
|
|
WORD *ordinals = get_rva( module, exports->AddressOfNameOrdinals );
|
|
|
|
DWORD *names = get_rva( module, exports->AddressOfNames );
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static WINE_MODREF *import_dll( HMODULE module, IMAGE_IMPORT_DESCRIPTOR *descr )
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
WINE_MODREF *wmImp;
|
|
|
|
HMODULE imp_mod;
|
|
|
|
IMAGE_EXPORT_DIRECTORY *exports;
|
|
|
|
DWORD exp_size;
|
|
|
|
IMAGE_THUNK_DATA *import_list, *thunk_list;
|
|
|
|
char *name = get_rva( module, descr->Name );
|
|
|
|
|
|
|
|
status = load_dll( name, 0, &wmImp );
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
if (status == STATUS_NO_SUCH_FILE)
|
2003-04-08 01:27:54 +02:00
|
|
|
ERR("Module (file) %s (which is needed by %s) not found\n",
|
|
|
|
name, current_modref->filename);
|
2003-04-05 07:16:11 +02:00
|
|
|
else
|
|
|
|
ERR("Loading module (file) %s (which is needed by %s) failed (error %ld).\n",
|
2003-04-08 01:27:54 +02:00
|
|
|
name, current_modref->filename, status);
|
2003-04-05 07:16:11 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
imp_mod = wmImp->ldr.BaseAddress;
|
|
|
|
if (!(exports = RtlImageDirectoryEntryToData( imp_mod, TRUE,
|
|
|
|
IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
while (import_list->u1.Ordinal)
|
|
|
|
{
|
|
|
|
if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal))
|
|
|
|
{
|
|
|
|
int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
|
|
|
|
|
|
|
|
TRACE("--- Ordinal %s,%d\n", name, ordinal);
|
|
|
|
thunk_list->u1.Function = (PDWORD)find_ordinal_export( imp_mod, exports, exp_size,
|
|
|
|
ordinal - exports->Base );
|
|
|
|
if (!thunk_list->u1.Function)
|
|
|
|
{
|
|
|
|
ERR("No implementation for %s.%d imported from %s, setting to 0xdeadbeef\n",
|
2003-04-08 01:27:54 +02:00
|
|
|
name, ordinal, current_modref->filename );
|
2003-04-05 07:16:11 +02:00
|
|
|
thunk_list->u1.Function = (PDWORD)0xdeadbeef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* import by name */
|
|
|
|
{
|
|
|
|
IMAGE_IMPORT_BY_NAME *pe_name;
|
|
|
|
pe_name = get_rva( module, (DWORD)import_list->u1.AddressOfData );
|
|
|
|
TRACE("--- %s %s.%d\n", pe_name->Name, name, pe_name->Hint);
|
|
|
|
thunk_list->u1.Function = (PDWORD)find_named_export( imp_mod, exports, exp_size,
|
|
|
|
pe_name->Name, pe_name->Hint );
|
|
|
|
if (!thunk_list->u1.Function)
|
|
|
|
{
|
|
|
|
ERR("No implementation for %s.%s imported from %s, setting to 0xdeadbeef\n",
|
2003-04-08 01:27:54 +02:00
|
|
|
name, pe_name->Name, current_modref->filename );
|
2003-04-05 07:16:11 +02:00
|
|
|
thunk_list->u1.Function = (PDWORD)0xdeadbeef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
import_list++;
|
|
|
|
thunk_list++;
|
|
|
|
}
|
|
|
|
return wmImp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* PE_fixup_imports
|
|
|
|
*
|
|
|
|
* Fixup all imports of a given module.
|
|
|
|
* The loader_section must be locked while calling this function.
|
|
|
|
*/
|
|
|
|
DWORD PE_fixup_imports( WINE_MODREF *wm )
|
|
|
|
{
|
|
|
|
int i, nb_imports;
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!(imports = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
|
|
|
|
IMAGE_DIRECTORY_ENTRY_IMPORT, &size )))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nb_imports = size / sizeof(*imports);
|
|
|
|
for (i = 0; i < nb_imports; i++)
|
|
|
|
{
|
|
|
|
if (!imports[i].Name)
|
|
|
|
{
|
|
|
|
nb_imports = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!nb_imports) return 0; /* no imports */
|
|
|
|
|
|
|
|
/* Allocate module dependency list */
|
|
|
|
wm->nDeps = nb_imports;
|
|
|
|
wm->deps = RtlAllocateHeap( ntdll_get_process_heap(), 0, nb_imports*sizeof(WINE_MODREF *) );
|
|
|
|
|
|
|
|
/* 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-04-05 07:16:11 +02:00
|
|
|
for (i = 0; i < nb_imports; i++)
|
|
|
|
{
|
2003-04-08 01:27:54 +02:00
|
|
|
if (!(wm->deps[i] = import_dll( wm->ldr.BaseAddress, &imports[i] ))) break;
|
2003-04-05 07:16:11 +02:00
|
|
|
}
|
2003-04-08 01:27:54 +02:00
|
|
|
current_modref = prev;
|
|
|
|
return (i < nb_imports);
|
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
|
|
|
/*************************************************************************
|
|
|
|
* MODULE_AllocModRef
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
WINE_MODREF *MODULE_AllocModRef( HMODULE hModule, LPCSTR filename )
|
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
2003-03-18 19:29:13 +01:00
|
|
|
IMAGE_NT_HEADERS *nt = RtlImageNtHeader(hModule);
|
2003-03-14 06:01:20 +01:00
|
|
|
|
|
|
|
DWORD long_len = strlen( filename );
|
|
|
|
DWORD short_len = GetShortPathNameA( filename, NULL, 0 );
|
|
|
|
|
|
|
|
if ((wm = RtlAllocateHeap( ntdll_get_process_heap(), HEAP_ZERO_MEMORY,
|
|
|
|
sizeof(*wm) + long_len + short_len + 1 )))
|
|
|
|
{
|
|
|
|
wm->filename = wm->data;
|
|
|
|
memcpy( wm->filename, filename, long_len + 1 );
|
|
|
|
if ((wm->modname = strrchr( wm->filename, '\\' ))) wm->modname++;
|
|
|
|
else wm->modname = wm->filename;
|
|
|
|
|
|
|
|
wm->short_filename = wm->filename + long_len + 1;
|
|
|
|
GetShortPathNameA( wm->filename, wm->short_filename, short_len + 1 );
|
|
|
|
if ((wm->short_modname = strrchr( wm->short_filename, '\\' ))) wm->short_modname++;
|
|
|
|
else wm->short_modname = wm->short_filename;
|
|
|
|
|
|
|
|
wm->next = MODULE_modref_list;
|
|
|
|
if (wm->next) wm->next->prev = wm;
|
|
|
|
MODULE_modref_list = wm;
|
|
|
|
|
2003-03-18 19:29:13 +01:00
|
|
|
wm->ldr.InLoadOrderModuleList.Flink = NULL;
|
|
|
|
wm->ldr.InLoadOrderModuleList.Blink = NULL;
|
|
|
|
wm->ldr.InMemoryOrderModuleList.Flink = NULL;
|
|
|
|
wm->ldr.InMemoryOrderModuleList.Blink = NULL;
|
|
|
|
wm->ldr.InInitializationOrderModuleList.Flink = NULL;
|
|
|
|
wm->ldr.InInitializationOrderModuleList.Blink = NULL;
|
|
|
|
wm->ldr.BaseAddress = hModule;
|
|
|
|
wm->ldr.EntryPoint = (nt->OptionalHeader.AddressOfEntryPoint) ?
|
|
|
|
((char *)hModule + nt->OptionalHeader.AddressOfEntryPoint) : 0;
|
|
|
|
wm->ldr.SizeOfImage = nt->OptionalHeader.SizeOfImage;
|
|
|
|
RtlCreateUnicodeStringFromAsciiz( &wm->ldr.FullDllName, wm->filename);
|
|
|
|
RtlCreateUnicodeStringFromAsciiz( &wm->ldr.BaseDllName, wm->modname);
|
|
|
|
wm->ldr.Flags = 0;
|
|
|
|
wm->ldr.LoadCount = 0;
|
2003-03-22 21:54:40 +01:00
|
|
|
wm->ldr.TlsIndex = -1;
|
2003-03-18 19:29:13 +01:00
|
|
|
wm->ldr.SectionHandle = NULL;
|
|
|
|
wm->ldr.CheckSum = 0;
|
|
|
|
wm->ldr.TimeDateStamp = 0;
|
|
|
|
|
|
|
|
if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
|
|
|
if (!exe_modref) exe_modref = wm;
|
|
|
|
else FIXME( "Trying to load second .EXE file: %s\n", filename );
|
|
|
|
}
|
2003-03-21 00:40:08 +01:00
|
|
|
else wm->ldr.Flags |= LDR_IMAGE_IS_DLL;
|
2003-03-14 06:01:20 +01:00
|
|
|
}
|
|
|
|
return wm;
|
|
|
|
}
|
|
|
|
|
2003-05-08 06:13:26 +02:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
IMAGE_TLS_DIRECTORY *dir;
|
|
|
|
ULONG size, i;
|
|
|
|
|
|
|
|
for (wm = MODULE_modref_list; wm; wm = wm->next)
|
|
|
|
{
|
|
|
|
if (!(dir = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
|
|
|
|
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 );
|
|
|
|
|
|
|
|
tls_dirs = RtlAllocateHeap( ntdll_get_process_heap(), 0, tls_module_count * sizeof(*tls_dirs) );
|
|
|
|
if (!tls_dirs) return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
for (i = 0, wm = MODULE_modref_list; wm; wm = wm->next)
|
|
|
|
{
|
|
|
|
if (!(dir = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
|
|
|
|
IMAGE_DIRECTORY_ENTRY_TLS, &size )))
|
|
|
|
continue;
|
|
|
|
tls_dirs[i] = dir;
|
|
|
|
*dir->AddressOfIndex = i;
|
|
|
|
wm->ldr.TlsIndex = i;
|
|
|
|
wm->ldr.LoadCount = -1; /* can't unload it */
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!(pointers = RtlAllocateHeap( ntdll_get_process_heap(), 0,
|
|
|
|
tls_module_count * sizeof(*pointers) )))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
if (!(data = RtlAllocateHeap( ntdll_get_process_heap(), 0, tls_total_size )))
|
|
|
|
{
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, pointers );
|
|
|
|
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;
|
|
|
|
|
|
|
|
TRACE( "thread %04lx idx %d: %ld/%ld bytes from %p to %p\n",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
NtCurrentTeb()->tls_ptr = pointers;
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (callback = dir->AddressOfCallBacks; *callback; callback++)
|
|
|
|
{
|
|
|
|
if (TRACE_ON(relay))
|
|
|
|
DPRINTF("%04lx:Call TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
|
|
|
|
GetCurrentThreadId(), *callback, module, reason_names[reason] );
|
|
|
|
(*callback)( module, reason, NULL );
|
|
|
|
if (TRACE_ON(relay))
|
|
|
|
DPRINTF("%04lx:Ret TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
|
|
|
|
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-04-08 01:27:54 +02:00
|
|
|
char 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-04-04 21:50:17 +02:00
|
|
|
if (!entry || !(wm->ldr.Flags & LDR_IMAGE_IS_DLL)) return TRUE;
|
|
|
|
|
|
|
|
if (TRACE_ON(relay))
|
2003-04-08 01:27:54 +02:00
|
|
|
{
|
|
|
|
size_t len = max( strlen(wm->modname), sizeof(mod_name)-1 );
|
|
|
|
memcpy( mod_name, wm->modname, len );
|
|
|
|
mod_name[len] = 0;
|
2003-05-08 06:13:26 +02:00
|
|
|
DPRINTF("%04lx:Call PE DLL (proc=%p,module=%p (%s),reason=%s,res=%p)\n",
|
|
|
|
GetCurrentThreadId(), entry, module, mod_name, reason_names[reason], lpReserved );
|
2003-04-08 01:27:54 +02:00
|
|
|
}
|
2003-05-08 06:13:26 +02:00
|
|
|
else TRACE("(%p (%s),%s,%p) - CALL\n", module, wm->modname, reason_names[reason], lpReserved );
|
2003-03-20 05:55:59 +01:00
|
|
|
|
2003-05-08 06:13:26 +02:00
|
|
|
retv = 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))
|
2003-05-08 06:13:26 +02:00
|
|
|
DPRINTF("%04lx:Ret PE DLL (proc=%p,module=%p (%s),reason=%s,res=%p) retval=%x\n",
|
|
|
|
GetCurrentThreadId(), entry, module, mod_name, reason_names[reason], lpReserved, retv );
|
|
|
|
else TRACE("(%p,%s,%p) - RETURN %d\n", module, reason_names[reason], lpReserved, retv );
|
2003-03-20 05:55:59 +01:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* MODULE_DllProcessAttach
|
|
|
|
*
|
|
|
|
* 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-05-14 21:51:14 +02:00
|
|
|
NTSTATUS MODULE_DllProcessAttach( 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;
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
if (!wm)
|
|
|
|
{
|
|
|
|
wm = exe_modref;
|
2003-05-14 21:51:14 +02:00
|
|
|
wm->ldr.LoadCount = -1; /* can't unload main exe */
|
|
|
|
if ((status = alloc_process_tls()) != STATUS_SUCCESS) goto done;
|
|
|
|
if ((status = alloc_thread_tls()) != STATUS_SUCCESS) goto done;
|
2003-03-20 05:55:59 +01:00
|
|
|
}
|
|
|
|
assert( wm );
|
|
|
|
|
|
|
|
/* 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-03-20 05:55:59 +01:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
if ((status = MODULE_DllProcessAttach( wm->deps[i], lpReserved )) != STATUS_SUCCESS) break;
|
|
|
|
}
|
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 ))
|
|
|
|
wm->ldr.Flags |= LDR_PROCESS_ATTACHED;
|
|
|
|
else
|
|
|
|
status = STATUS_DLL_INIT_FAILED;
|
2003-04-08 01:27:54 +02:00
|
|
|
current_modref = prev;
|
2003-03-20 05:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-insert MODREF at head of list */
|
2003-05-14 21:51:14 +02:00
|
|
|
if (status == STATUS_SUCCESS && wm->prev )
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
|
|
|
wm->prev->next = wm->next;
|
|
|
|
if ( wm->next ) wm->next->prev = wm->prev;
|
|
|
|
|
|
|
|
wm->prev = NULL;
|
|
|
|
wm->next = MODULE_modref_list;
|
|
|
|
MODULE_modref_list = wm->next->prev = wm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
|
|
|
|
|
|
|
|
done:
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
2003-05-14 21:51:14 +02:00
|
|
|
return status;
|
2003-03-20 05:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* MODULE_DllProcessDetach
|
|
|
|
*
|
|
|
|
* Send DLL process detach notifications. See the comment about calling
|
|
|
|
* sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
|
|
|
|
* is set, only DLLs with zero refcount are notified.
|
|
|
|
*/
|
2003-05-08 06:13:26 +02:00
|
|
|
static void MODULE_DllProcessDetach( BOOL bForceDetach, LPVOID lpReserved )
|
2003-03-20 05:55:59 +01:00
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
if (bForceDetach) process_detaching = 1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
for ( wm = MODULE_modref_list; wm; wm = wm->next )
|
|
|
|
{
|
|
|
|
/* Check whether to detach this DLL */
|
2003-03-21 00:40:08 +01:00
|
|
|
if ( !(wm->ldr.Flags & LDR_PROCESS_ATTACHED) )
|
2003-03-20 05:55:59 +01:00
|
|
|
continue;
|
2003-05-14 21:51:14 +02:00
|
|
|
if ( wm->ldr.LoadCount && !bForceDetach )
|
2003-03-20 05:55:59 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Call detach notification */
|
2003-03-21 00:40:08 +01:00
|
|
|
wm->ldr.Flags &= ~LDR_PROCESS_ATTACHED;
|
2003-03-20 05:55:59 +01:00
|
|
|
MODULE_InitDLL( wm, DLL_PROCESS_DETACH, lpReserved );
|
|
|
|
|
|
|
|
/* Restart at head of WINE_MODREF list, as entries might have
|
|
|
|
been added and/or removed while performing the call ... */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ( wm );
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
2003-05-14 21:51:14 +02:00
|
|
|
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
|
|
|
|
|
|
|
for ( wm = MODULE_modref_list; wm; wm = wm->next )
|
|
|
|
if ( !wm->next )
|
|
|
|
break;
|
|
|
|
|
|
|
|
for ( ; wm; wm = wm->prev )
|
|
|
|
{
|
2003-03-21 00:40:08 +01:00
|
|
|
if ( !(wm->ldr.Flags & LDR_PROCESS_ATTACHED) )
|
2003-03-20 05:55:59 +01:00
|
|
|
continue;
|
2003-03-21 00:40:08 +01:00
|
|
|
if ( wm->ldr.Flags & LDR_NO_DLL_CALLS )
|
2003-03-20 05:55:59 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
MODULE_InitDLL( wm, DLL_THREAD_ATTACH, lpReserved );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI LdrFindEntryForAddress(const void* addr, PLDR_MODULE* mod)
|
|
|
|
{
|
|
|
|
WINE_MODREF* wm;
|
|
|
|
|
|
|
|
for ( wm = MODULE_modref_list; wm; wm = wm->next )
|
|
|
|
{
|
2003-03-20 23:05:26 +01:00
|
|
|
if ((const void *)wm->ldr.BaseAddress <= addr &&
|
|
|
|
(char *)addr < (char*)wm->ldr.BaseAddress + wm->ldr.SizeOfImage)
|
2003-03-18 19:29:13 +01:00
|
|
|
{
|
|
|
|
*mod = &wm->ldr;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return STATUS_NO_MORE_ENTRIES;
|
|
|
|
}
|
|
|
|
|
2003-03-07 00:41:37 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* MODULE_FindModule
|
|
|
|
*
|
|
|
|
* Find a (loaded) win32 module depending on path
|
|
|
|
* LPCSTR path: [in] pathname of module/library to be found
|
|
|
|
*
|
|
|
|
* The loader_section must be locked while calling this function
|
|
|
|
* RETURNS
|
|
|
|
* the module handle if found
|
|
|
|
* 0 if not
|
|
|
|
*/
|
|
|
|
WINE_MODREF *MODULE_FindModule(LPCSTR path)
|
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
char dllname[260], *p;
|
|
|
|
|
|
|
|
/* Append .DLL to name if no extension present */
|
|
|
|
strcpy( dllname, path );
|
|
|
|
if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
|
|
|
|
strcat( dllname, ".DLL" );
|
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
if ((wm = cached_modref) != NULL)
|
2003-03-07 00:41:37 +01:00
|
|
|
{
|
2003-04-05 07:16:11 +02:00
|
|
|
if ( !FILE_strcasecmp( dllname, wm->modname ) ) return wm;
|
|
|
|
if ( !FILE_strcasecmp( dllname, wm->filename ) ) return wm;
|
|
|
|
if ( !FILE_strcasecmp( dllname, wm->short_modname ) ) return wm;
|
|
|
|
if ( !FILE_strcasecmp( dllname, wm->short_filename ) ) return wm;
|
2003-03-07 00:41:37 +01:00
|
|
|
}
|
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
for ( wm = MODULE_modref_list; wm; wm = wm->next )
|
|
|
|
{
|
|
|
|
if ( !FILE_strcasecmp( dllname, wm->modname ) ) break;
|
|
|
|
if ( !FILE_strcasecmp( dllname, wm->filename ) ) break;
|
|
|
|
if ( !FILE_strcasecmp( dllname, wm->short_modname ) ) break;
|
|
|
|
if ( !FILE_strcasecmp( dllname, wm->short_filename ) ) break;
|
|
|
|
}
|
|
|
|
cached_modref = wm;
|
2003-03-07 00:41:37 +01:00
|
|
|
return wm;
|
|
|
|
}
|
2002-06-10 04:28:00 +02:00
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
if (flags) FIXME( "flags %lx not supported\n", flags );
|
|
|
|
|
|
|
|
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-03-04 05:36:56 +01:00
|
|
|
NTSTATUS WINAPI LdrGetDllHandle(ULONG x, ULONG y, PUNICODE_STRING name, HMODULE *base)
|
2002-06-10 04:28:00 +02:00
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
2003-04-05 07:16:11 +02:00
|
|
|
STRING str;
|
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");
|
|
|
|
|
|
|
|
/* FIXME: we should store module name information as unicode */
|
2003-04-05 07:16:11 +02:00
|
|
|
RtlUnicodeStringToAnsiString( &str, name, TRUE );
|
|
|
|
wm = MODULE_FindModule( str.Buffer );
|
|
|
|
RtlFreeAnsiString( &str );
|
2002-06-10 04:28:00 +02:00
|
|
|
|
2003-03-07 00:41:37 +01:00
|
|
|
if (!wm)
|
|
|
|
{
|
|
|
|
*base = 0;
|
2002-06-10 04:28:00 +02:00
|
|
|
return STATUS_DLL_NOT_FOUND;
|
2003-03-07 00:41:37 +01:00
|
|
|
}
|
2002-06-10 04:28:00 +02:00
|
|
|
|
2003-03-20 23:05:26 +01:00
|
|
|
*base = wm->ldr.BaseAddress;
|
2003-04-04 21:50:17 +02:00
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
TRACE("%lx %lx %s -> %p\n", x, y, debugstr_us(name), *base);
|
2003-04-04 21:50:17 +02:00
|
|
|
|
2002-06-10 04:28:00 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* LdrGetProcedureAddress (NTDLL.@)
|
2003-03-07 00:41:37 +01:00
|
|
|
*/
|
2003-04-05 07:16:11 +02:00
|
|
|
NTSTATUS WINAPI LdrGetProcedureAddress(HMODULE module, PANSI_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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* check if the module itself is invalid to return the proper error */
|
|
|
|
if (!get_modref( module )) ret = STATUS_DLL_NOT_FOUND;
|
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
|
|
|
|
|
|
|
|
2003-03-14 06:01:20 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* allocate_lib_dir
|
|
|
|
*
|
|
|
|
* helper for MODULE_LoadLibraryExA. Allocate space to hold the directory
|
|
|
|
* portion of the provided name and put the name in it.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static LPCSTR allocate_lib_dir(LPCSTR libname)
|
|
|
|
{
|
|
|
|
LPCSTR p, pmax;
|
|
|
|
LPSTR result;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
pmax = libname;
|
|
|
|
if ((p = strrchr( pmax, '\\' ))) pmax = p + 1;
|
|
|
|
if ((p = strrchr( pmax, '/' ))) pmax = p + 1; /* Naughty. MSDN says don't */
|
|
|
|
if (pmax == libname && pmax[0] && pmax[1] == ':') pmax += 2;
|
|
|
|
|
|
|
|
length = pmax - libname;
|
|
|
|
|
|
|
|
result = RtlAllocateHeap (ntdll_get_process_heap(), 0, length+1);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
strncpy (result, libname, length);
|
|
|
|
result [length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
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.
|
|
|
|
*
|
|
|
|
* libdir is used to support LOAD_WITH_ALTERED_SEARCH_PATH during the recursion
|
|
|
|
* on this function. When first called from LoadLibraryExA it will be
|
|
|
|
* NULL but thereafter it may point to a buffer containing the path
|
|
|
|
* portion of the library name. Note that the recursion all occurs
|
|
|
|
* within a Critical section (see LoadLibraryExA) so the use of a
|
|
|
|
* static is acceptable.
|
|
|
|
* (We have to use a static variable at some point anyway, to pass the
|
|
|
|
* information from BUILTIN32_dlopen through dlopen and the builtin's
|
|
|
|
* init function into load_library).
|
|
|
|
* allocated_libdir is TRUE in the stack frame that allocated libdir
|
|
|
|
*/
|
2003-04-05 07:16:11 +02:00
|
|
|
static NTSTATUS load_dll( LPCSTR libname, DWORD flags, WINE_MODREF** pwm )
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
enum loadorder_type loadorder[LOADORDER_NTYPES];
|
|
|
|
LPSTR filename;
|
|
|
|
const char *filetype = "";
|
|
|
|
DWORD found;
|
|
|
|
BOOL allocated_libdir = FALSE;
|
|
|
|
static LPCSTR libdir = NULL; /* See above */
|
|
|
|
NTSTATUS nts = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
*pwm = NULL;
|
|
|
|
if ( !libname ) return STATUS_DLL_NOT_FOUND; /* FIXME ? */
|
|
|
|
|
|
|
|
filename = RtlAllocateHeap ( ntdll_get_process_heap(), 0, MAX_PATH + 1 );
|
|
|
|
if ( !filename ) return STATUS_NO_MEMORY;
|
|
|
|
*filename = 0; /* Just in case we don't set it before goto error */
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
if ((flags & LOAD_WITH_ALTERED_SEARCH_PATH) && FILE_contains_path(libname))
|
|
|
|
{
|
|
|
|
if (!(libdir = allocate_lib_dir(libname)))
|
|
|
|
{
|
|
|
|
nts = STATUS_NO_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
allocated_libdir = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!libdir || allocated_libdir)
|
|
|
|
found = SearchPathA(NULL, libname, ".dll", MAX_PATH, filename, NULL);
|
|
|
|
else
|
|
|
|
found = DIR_SearchAlternatePath(libdir, libname, ".dll", MAX_PATH, filename, NULL);
|
|
|
|
|
|
|
|
/* build the modules filename */
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
if (!MODULE_GetBuiltinPath( libname, ".dll", filename, MAX_PATH ))
|
|
|
|
{
|
|
|
|
nts = STATUS_INTERNAL_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for already loaded module */
|
|
|
|
if (!(*pwm = MODULE_FindModule(filename)) && !FILE_contains_path(libname))
|
|
|
|
{
|
|
|
|
LPSTR fn = RtlAllocateHeap ( ntdll_get_process_heap(), 0, MAX_PATH + 1 );
|
|
|
|
if (fn)
|
|
|
|
{
|
|
|
|
/* since the default loading mechanism uses a more detailed algorithm
|
|
|
|
* than SearchPath (like using PATH, which can even be modified between
|
|
|
|
* two attempts of loading the same DLL), the look-up above (with
|
|
|
|
* SearchPath) can have put the file in system directory, whereas it
|
|
|
|
* has already been loaded but with a different path. So do a specific
|
|
|
|
* look-up with filename (without any path)
|
|
|
|
*/
|
|
|
|
strcpy ( fn, libname );
|
|
|
|
/* if the filename doesn't have an extension append .DLL */
|
|
|
|
if (!strrchr( fn, '.')) strcat( fn, ".dll" );
|
|
|
|
if ((*pwm = MODULE_FindModule( fn )) != NULL)
|
|
|
|
strcpy( filename, fn );
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, fn );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*pwm)
|
|
|
|
{
|
2003-05-14 21:51:14 +02:00
|
|
|
if ((*pwm)->ldr.LoadCount != -1) (*pwm)->ldr.LoadCount++;
|
|
|
|
|
2003-03-21 00:40:08 +01:00
|
|
|
if (((*pwm)->ldr.Flags & LDR_DONT_RESOLVE_REFS) &&
|
2003-03-14 06:01:20 +01:00
|
|
|
!(flags & DONT_RESOLVE_DLL_REFERENCES))
|
|
|
|
{
|
2003-03-21 00:40:08 +01:00
|
|
|
(*pwm)->ldr.Flags &= ~LDR_DONT_RESOLVE_REFS;
|
2003-03-14 06:01:20 +01:00
|
|
|
PE_fixup_imports( *pwm );
|
|
|
|
}
|
2003-03-20 23:05:26 +01:00
|
|
|
TRACE("Already loaded module '%s' at %p, count=%d\n", filename, (*pwm)->ldr.BaseAddress, (*pwm)->ldr.LoadCount);
|
2003-03-14 06:01:20 +01:00
|
|
|
if (allocated_libdir)
|
|
|
|
{
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, (LPSTR)libdir );
|
|
|
|
libdir = NULL;
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, filename );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_GetLoadOrder( loadorder, filename, TRUE);
|
|
|
|
|
|
|
|
for (i = 0; i < LOADORDER_NTYPES; i++)
|
|
|
|
{
|
|
|
|
if (loadorder[i] == LOADORDER_INVALID) break;
|
|
|
|
|
|
|
|
switch (loadorder[i])
|
|
|
|
{
|
|
|
|
case LOADORDER_DLL:
|
|
|
|
TRACE("Trying native dll '%s'\n", filename);
|
|
|
|
nts = PE_LoadLibraryExA(filename, flags, pwm);
|
|
|
|
filetype = "native";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOADORDER_BI:
|
|
|
|
TRACE("Trying built-in '%s'\n", filename);
|
|
|
|
nts = BUILTIN32_LoadLibraryExA(filename, flags, pwm);
|
|
|
|
filetype = "builtin";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
nts = STATUS_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nts == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Initialize DLL just loaded */
|
2003-04-04 21:50:17 +02:00
|
|
|
TRACE("Loaded module '%s' (%s) at %p\n", filename, filetype, (*pwm)->ldr.BaseAddress);
|
2003-03-14 06:01:20 +01:00
|
|
|
if (!TRACE_ON(module))
|
|
|
|
TRACE_(loaddll)("Loaded module '%s' : %s\n", filename, filetype);
|
2003-03-20 23:05:26 +01:00
|
|
|
/* Set the ldr.LoadCount here so that an attach failure will */
|
2003-03-14 06:01:20 +01:00
|
|
|
/* decrement the dependencies through the MODULE_FreeLibrary call. */
|
2003-03-20 23:05:26 +01:00
|
|
|
(*pwm)->ldr.LoadCount = 1;
|
2003-03-14 06:01:20 +01:00
|
|
|
|
|
|
|
if (allocated_libdir)
|
|
|
|
{
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, (LPSTR)libdir );
|
|
|
|
libdir = NULL;
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, filename );
|
|
|
|
return nts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nts != STATUS_NO_SUCH_FILE)
|
|
|
|
{
|
|
|
|
WARN("Loading of %s DLL %s failed (status %ld).\n",
|
|
|
|
filetype, filename, nts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (allocated_libdir)
|
|
|
|
{
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, (LPSTR)libdir );
|
|
|
|
libdir = NULL;
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
WARN("Failed to load module '%s'; status=%ld\n", filename, nts);
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, filename );
|
|
|
|
return nts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* LdrLoadDll (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI LdrLoadDll(LPCWSTR path_name, DWORD flags, PUNICODE_STRING libname, HMODULE* hModule)
|
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
NTSTATUS nts = STATUS_SUCCESS;
|
|
|
|
STRING str;
|
|
|
|
|
|
|
|
RtlUnicodeStringToAnsiString(&str, libname, TRUE);
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
|
2003-04-05 07:16:11 +02:00
|
|
|
switch (nts = load_dll( str.Buffer, flags, &wm ))
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
|
|
|
case STATUS_SUCCESS:
|
2003-05-14 21:51:14 +02:00
|
|
|
nts = MODULE_DllProcessAttach( wm, NULL );
|
|
|
|
if (nts != STATUS_SUCCESS)
|
2003-03-14 06:01:20 +01:00
|
|
|
{
|
2003-04-04 21:50:17 +02:00
|
|
|
WARN("Attach failed for module '%s'.\n", str.Buffer);
|
2003-03-20 23:05:26 +01:00
|
|
|
LdrUnloadDll(wm->ldr.BaseAddress);
|
2003-03-14 06:01:20 +01:00
|
|
|
wm = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATUS_NO_SUCH_FILE:
|
|
|
|
nts = STATUS_DLL_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
default: /* keep error code as it is (memory...) */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-03-20 23:05:26 +01:00
|
|
|
*hModule = (wm) ? wm->ldr.BaseAddress : NULL;
|
2003-03-14 06:01:20 +01:00
|
|
|
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
RtlFreeAnsiString(&str);
|
|
|
|
|
|
|
|
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;
|
|
|
|
WINE_MODREF* wm;
|
|
|
|
|
|
|
|
smi->ModulesCount = 0;
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &loader_section );
|
|
|
|
for ( wm = MODULE_modref_list; wm; wm = wm->next )
|
|
|
|
{
|
|
|
|
size += sizeof(*sm);
|
|
|
|
if (size <= buf_size)
|
|
|
|
{
|
|
|
|
sm->Reserved1 = 0; /* FIXME */
|
|
|
|
sm->Reserved2 = 0; /* FIXME */
|
|
|
|
sm->ImageBaseAddress = wm->ldr.BaseAddress;
|
|
|
|
sm->ImageSize = wm->ldr.SizeOfImage;
|
|
|
|
sm->Flags = wm->ldr.Flags;
|
|
|
|
sm->Id = 0; /* FIXME */
|
|
|
|
sm->Rank = 0; /* FIXME */
|
|
|
|
sm->Unknown = 0; /* FIXME */
|
|
|
|
str.Length = 0;
|
|
|
|
str.MaximumLength = MAXIMUM_FILENAME_LENGTH;
|
|
|
|
str.Buffer = sm->Name;
|
|
|
|
RtlUnicodeStringToAnsiString(&str, &wm->ldr.FullDllName, FALSE);
|
|
|
|
ptr = strrchr(sm->Name, '\\');
|
|
|
|
sm->NameOffset = (ptr != NULL) ? (ptr - (char*)sm->Name + 1) : 0;
|
|
|
|
|
|
|
|
smi->ModulesCount++;
|
|
|
|
sm++;
|
|
|
|
}
|
|
|
|
else nts = STATUS_INFO_LENGTH_MISMATCH;
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
|
|
|
|
if (req_size) *req_size = size;
|
|
|
|
|
|
|
|
return nts;
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* LdrShutdownThread (NTDLL.@)
|
|
|
|
*
|
|
|
|
*/
|
2003-05-08 06:13:26 +02:00
|
|
|
void WINAPI LdrShutdownThread(void)
|
2003-03-04 05:36:56 +01:00
|
|
|
{
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
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 );
|
|
|
|
|
|
|
|
for ( wm = MODULE_modref_list; wm; wm = wm->next )
|
|
|
|
{
|
2003-03-21 00:40:08 +01:00
|
|
|
if ( !(wm->ldr.Flags & LDR_PROCESS_ATTACHED) )
|
2003-03-04 05:36:56 +01:00
|
|
|
continue;
|
2003-03-21 00:40:08 +01:00
|
|
|
if ( wm->ldr.Flags & LDR_NO_DLL_CALLS )
|
2003-03-04 05:36:56 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
MODULE_InitDLL( wm, DLL_THREAD_DETACH, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlLeaveCriticalSection( &loader_section );
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
WINE_MODREF *wm, *next;
|
|
|
|
|
|
|
|
for (wm = MODULE_modref_list; wm; wm = next)
|
|
|
|
{
|
|
|
|
next = wm->next;
|
|
|
|
|
2003-03-20 23:05:26 +01:00
|
|
|
if (wm->ldr.LoadCount)
|
2003-03-14 05:00:52 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Unlink this modref from the chain */
|
|
|
|
if (wm->next)
|
|
|
|
wm->next->prev = wm->prev;
|
|
|
|
if (wm->prev)
|
|
|
|
wm->prev->next = wm->next;
|
|
|
|
if (wm == MODULE_modref_list)
|
|
|
|
MODULE_modref_list = wm->next;
|
|
|
|
|
|
|
|
TRACE(" unloading %s\n", wm->filename);
|
|
|
|
if (!TRACE_ON(module))
|
|
|
|
TRACE_(loaddll)("Unloaded module '%s' : %s\n", wm->filename,
|
|
|
|
wm->dlhandle ? "builtin" : "native" );
|
|
|
|
|
|
|
|
SERVER_START_REQ( unload_dll )
|
|
|
|
{
|
2003-03-20 23:05:26 +01:00
|
|
|
req->base = wm->ldr.BaseAddress;
|
2003-03-14 05:00:52 +01:00
|
|
|
wine_server_call( req );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
if (wm->dlhandle) wine_dll_unload( wm->dlhandle );
|
2003-03-20 23:05:26 +01:00
|
|
|
else NtUnmapViewOfSection( GetCurrentProcess(), wm->ldr.BaseAddress );
|
2003-03-14 05:00:52 +01:00
|
|
|
FreeLibrary16( wm->hDummyMod );
|
2003-04-05 07:16:11 +02:00
|
|
|
if (cached_modref == wm) cached_modref = NULL;
|
2003-03-14 05:00:52 +01:00
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, wm->deps );
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, wm );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* 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;
|
|
|
|
TRACE("(%s) ldr.LoadCount: %d\n", wm->modname, 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
|
|
|
{
|
|
|
|
TRACE("(%s) - START\n", wm->modname);
|
|
|
|
|
|
|
|
/* Recursively decrement reference counts */
|
|
|
|
MODULE_DecRefCount( wm );
|
|
|
|
|
|
|
|
/* Call process detach notifications */
|
|
|
|
if ( free_lib_count <= 1 )
|
|
|
|
{
|
|
|
|
MODULE_DllProcessDetach( FALSE, NULL );
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* 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;
|
|
|
|
IMAGE_SECTION_HEADER *sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader +
|
|
|
|
nt->FileHeader.SizeOfOptionalHeader);
|
|
|
|
for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
|
|
|
|
{
|
|
|
|
if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
|
|
|
|
return sec;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|