2018-05-17 18:31:57 -07:00
|
|
|
From 225b86d4fb6f51854f173076564f6bea9a9e5ac0 Mon Sep 17 00:00:00 2001
|
2017-04-08 05:23:17 -07:00
|
|
|
From: =?UTF-8?q?Michael=20M=C3=BCller?= <michael@fds-team.de>
|
|
|
|
Date: Mon, 3 Apr 2017 05:30:27 +0200
|
2018-02-28 17:28:01 -08:00
|
|
|
Subject: [PATCH] ntdll: Implement HashLinks field in LDR module data.
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
dlls/kernel32/tests/loader.c | 75 ++++++++++++++++++++++++++++++++++++++++++++
|
2018-02-28 17:28:01 -08:00
|
|
|
dlls/ntdll/loader.c | 65 ++++++++++++++++++++++++++++++++++++++
|
2017-04-08 05:23:17 -07:00
|
|
|
include/winternl.h | 6 ++--
|
2018-02-28 17:28:01 -08:00
|
|
|
3 files changed, 144 insertions(+), 2 deletions(-)
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
diff --git a/dlls/kernel32/tests/loader.c b/dlls/kernel32/tests/loader.c
|
2018-05-17 18:31:57 -07:00
|
|
|
index ac62d28..0f5ddf1 100644
|
2017-04-08 05:23:17 -07:00
|
|
|
--- a/dlls/kernel32/tests/loader.c
|
|
|
|
+++ b/dlls/kernel32/tests/loader.c
|
2018-02-28 17:28:01 -08:00
|
|
|
@@ -29,6 +29,7 @@
|
2017-04-08 05:23:17 -07:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
+#include "winuser.h"
|
|
|
|
#include "wine/test.h"
|
|
|
|
#include "delayloadhandler.h"
|
|
|
|
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -3573,6 +3574,79 @@ static void test_InMemoryOrderModuleList(void)
|
2017-04-08 05:23:17 -07:00
|
|
|
ok(entry2 == mark2, "expected entry2 == mark2, got %p and %p\n", entry2, mark2);
|
|
|
|
}
|
|
|
|
|
|
|
|
+static inline WCHAR toupperW(WCHAR c)
|
|
|
|
+{
|
|
|
|
+ WCHAR tmp = c;
|
|
|
|
+ CharUpperBuffW(&tmp, 1);
|
|
|
|
+ return tmp;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static ULONG hash_basename(const WCHAR *basename)
|
|
|
|
+{
|
|
|
|
+ WORD version = MAKEWORD(NtCurrentTeb()->Peb->OSMinorVersion,
|
|
|
|
+ NtCurrentTeb()->Peb->OSMajorVersion);
|
|
|
|
+ ULONG hash = 0;
|
|
|
|
+
|
|
|
|
+ if (version >= 0x0602)
|
|
|
|
+ {
|
|
|
|
+ for (; *basename; basename++)
|
|
|
|
+ hash = hash * 65599 + toupperW(*basename);
|
|
|
|
+ }
|
|
|
|
+ else if (version == 0x0601)
|
|
|
|
+ {
|
|
|
|
+ for (; *basename; basename++)
|
|
|
|
+ hash = hash + 65599 * toupperW(*basename);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ hash = toupperW(basename[0]) - 'A';
|
|
|
|
+
|
|
|
|
+ return hash & 31;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void test_HashLinks(void)
|
|
|
|
+{
|
|
|
|
+ static WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0};
|
|
|
|
+ static WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
|
|
|
|
+
|
|
|
|
+ LIST_ENTRY *hash_map, *entry, *mark;
|
|
|
|
+ LDR_MODULE *module;
|
|
|
|
+ BOOL found;
|
|
|
|
+
|
|
|
|
+ entry = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
+ entry = entry->Flink;
|
|
|
|
+
|
|
|
|
+ module = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
+ entry = module->HashLinks.Blink;
|
|
|
|
+
|
|
|
|
+ hash_map = entry - hash_basename(module->BaseDllName.Buffer);
|
|
|
|
+
|
|
|
|
+ mark = &hash_map[hash_basename(ntdllW)];
|
|
|
|
+ found = FALSE;
|
|
|
|
+ for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
+ {
|
|
|
|
+ module = CONTAINING_RECORD(entry, LDR_MODULE, HashLinks);
|
|
|
|
+ if (!lstrcmpiW(module->BaseDllName.Buffer, ntdllW))
|
|
|
|
+ {
|
|
|
|
+ found = TRUE;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ok(found, "Could not find ntdll\n");
|
|
|
|
+
|
|
|
|
+ mark = &hash_map[hash_basename(kernel32W)];
|
|
|
|
+ found = FALSE;
|
|
|
|
+ for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
+ {
|
|
|
|
+ module = CONTAINING_RECORD(entry, LDR_MODULE, HashLinks);
|
|
|
|
+ if (!lstrcmpiW(module->BaseDllName.Buffer, kernel32W))
|
|
|
|
+ {
|
|
|
|
+ found = TRUE;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ok(found, "Could not find kernel32\n");
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
START_TEST(loader)
|
|
|
|
{
|
|
|
|
int argc;
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -3637,4 +3711,5 @@ START_TEST(loader)
|
2017-04-08 05:23:17 -07:00
|
|
|
test_import_resolution();
|
|
|
|
test_ExitProcess();
|
|
|
|
test_InMemoryOrderModuleList();
|
|
|
|
+ test_HashLinks();
|
|
|
|
}
|
|
|
|
diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c
|
2018-05-17 18:31:57 -07:00
|
|
|
index 06915aa..722ab44 100644
|
2017-04-08 05:23:17 -07:00
|
|
|
--- a/dlls/ntdll/loader.c
|
|
|
|
+++ b/dlls/ntdll/loader.c
|
2018-02-28 17:28:01 -08:00
|
|
|
@@ -86,6 +86,9 @@ static const char * const reason_names[] =
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
static const WCHAR dllW[] = {'.','d','l','l',0};
|
|
|
|
|
|
|
|
+#define HASH_MAP_SIZE 32
|
|
|
|
+static LIST_ENTRY hash_table[HASH_MAP_SIZE];
|
|
|
|
+
|
|
|
|
/* internal representation of 32bit modules. per process. */
|
|
|
|
typedef struct _wine_modref
|
|
|
|
{
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -349,6 +352,52 @@ static inline ULONG_PTR allocate_stub( const char *dll, const char *name ) { ret
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
|
2018-05-17 18:31:57 -07:00
|
|
|
/*************************************************************************
|
2017-04-08 05:23:17 -07:00
|
|
|
+ * hash_basename
|
|
|
|
+ *
|
|
|
|
+ * Calculates the bucket index of a dll using the basename.
|
|
|
|
+ */
|
|
|
|
+static ULONG hash_basename(const WCHAR *basename)
|
|
|
|
+{
|
|
|
|
+ WORD version = MAKEWORD(NtCurrentTeb()->Peb->OSMinorVersion,
|
|
|
|
+ NtCurrentTeb()->Peb->OSMajorVersion);
|
|
|
|
+ ULONG hash = 0;
|
|
|
|
+
|
|
|
|
+ if (version >= 0x0602)
|
|
|
|
+ {
|
|
|
|
+ for (; *basename; basename++)
|
|
|
|
+ hash = hash * 65599 + toupperW(*basename);
|
|
|
|
+ }
|
|
|
|
+ else if (version == 0x0601)
|
|
|
|
+ {
|
|
|
|
+ for (; *basename; basename++)
|
|
|
|
+ hash = hash + 65599 * toupperW(*basename);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ hash = toupperW(basename[0]) - 'A';
|
|
|
|
+
|
|
|
|
+ return hash & (HASH_MAP_SIZE-1);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*************************************************************************
|
|
|
|
+ * recompute_hash_maps
|
|
|
|
+ *
|
|
|
|
+ * Recomputes the LDR hash map (necessary when windows version changes).
|
|
|
|
+ */
|
|
|
|
+static void recompute_hash_map(void)
|
|
|
|
+{
|
|
|
|
+ PLIST_ENTRY mark, entry;
|
|
|
|
+ PLDR_MODULE mod;
|
|
|
|
+
|
|
|
|
+ mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
+ for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
+ {
|
|
|
|
+ mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
+ RemoveEntryList( &mod->HashLinks );
|
|
|
|
+ InsertTailList( &hash_table[hash_basename(mod->BaseDllName.Buffer)], &mod->HashLinks );
|
|
|
|
+ }
|
|
|
|
+}
|
2018-02-28 17:28:01 -08:00
|
|
|
+
|
2018-05-17 18:31:57 -07:00
|
|
|
+/*************************************************************************
|
2017-04-08 05:23:17 -07:00
|
|
|
* get_modref
|
2018-02-17 19:09:37 -08:00
|
|
|
*
|
2018-05-17 18:31:57 -07:00
|
|
|
* Looks for the referenced HMODULE in the current process
|
|
|
|
@@ -1067,7 +1116,12 @@ static WINE_MODREF *alloc_module( HMODULE hModule, LPCWSTR filename, LPCWSTR fak
|
2017-04-08 05:23:17 -07:00
|
|
|
&wm->ldr.InLoadOrderModuleList);
|
|
|
|
InsertTailList(&NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList,
|
|
|
|
&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
+ InsertTailList(&hash_table[hash_basename(wm->ldr.BaseDllName.Buffer)],
|
|
|
|
+ &wm->ldr.HashLinks);
|
2018-02-28 17:28:01 -08:00
|
|
|
+
|
|
|
|
/* wait until init is called for inserting into InInitializationOrderModuleList */
|
|
|
|
+ wm->ldr.InInitializationOrderModuleList.Flink = NULL;
|
|
|
|
+ wm->ldr.InInitializationOrderModuleList.Blink = NULL;
|
2017-04-08 05:23:17 -07:00
|
|
|
|
2018-02-28 17:28:01 -08:00
|
|
|
if (!(nt->OptionalHeader.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
|
|
|
|
{
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -1795,6 +1849,7 @@ static void load_builtin_callback( void *module, const char *filename )
|
2017-04-08 05:23:17 -07:00
|
|
|
/* the module has only be inserted in the load & memory order lists */
|
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
+ RemoveEntryList(&wm->ldr.HashLinks);
|
|
|
|
/* FIXME: free the modref */
|
|
|
|
builtin_load_info->status = STATUS_DLL_NOT_FOUND;
|
|
|
|
return;
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -2055,6 +2110,7 @@ static NTSTATUS load_native_dll( LPCWSTR load_path, LPCWSTR name, LPCWSTR fakemo
|
2017-04-08 05:23:17 -07:00
|
|
|
/* the module has only be inserted in the load & memory order lists */
|
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
+ RemoveEntryList(&wm->ldr.HashLinks);
|
|
|
|
|
|
|
|
/* FIXME: there are several more dangling references
|
|
|
|
* left. Including dlls loaded by this dll before the
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -3230,6 +3286,7 @@ static void free_modref( WINE_MODREF *wm )
|
2017-04-08 05:23:17 -07:00
|
|
|
{
|
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
+ RemoveEntryList(&wm->ldr.HashLinks);
|
|
|
|
if (wm->ldr.InInitializationOrderModuleList.Flink)
|
|
|
|
RemoveEntryList(&wm->ldr.InInitializationOrderModuleList);
|
|
|
|
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -3627,6 +3684,9 @@ void WINAPI LdrInitializeThunk( void *kernel_start, ULONG_PTR unknown2,
|
2017-04-08 05:23:17 -07:00
|
|
|
RemoveEntryList( &wm->ldr.InMemoryOrderModuleList );
|
|
|
|
InsertHeadList( &peb->LdrData->InMemoryOrderModuleList, &wm->ldr.InMemoryOrderModuleList );
|
|
|
|
|
|
|
|
+ /* the windows version was not set yet when ntdll and kernel32 were loaded */
|
|
|
|
+ recompute_hash_map();
|
|
|
|
+
|
2018-02-17 19:09:37 -08:00
|
|
|
if ((status = virtual_alloc_thread_stack( NtCurrentTeb(), 0, 0, NULL )) != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR( "Main exe initialization for %s failed, status %x\n",
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -3773,6 +3833,7 @@ void __wine_process_init(void)
|
2017-04-08 05:23:17 -07:00
|
|
|
NTSTATUS status;
|
|
|
|
ANSI_STRING func_name;
|
|
|
|
void (* DECLSPEC_NORETURN CDECL init_func)(void);
|
|
|
|
+ DWORD i;
|
|
|
|
|
|
|
|
main_exe_file = thread_init();
|
|
|
|
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -3782,6 +3843,10 @@ void __wine_process_init(void)
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
load_global_options();
|
2018-02-28 17:28:01 -08:00
|
|
|
|
2017-04-08 05:23:17 -07:00
|
|
|
+ /* initialize hash table */
|
|
|
|
+ for (i = 0; i < HASH_MAP_SIZE; i++)
|
|
|
|
+ InitializeListHead(&hash_table[i]);
|
2018-02-28 17:28:01 -08:00
|
|
|
+
|
2017-04-08 05:23:17 -07:00
|
|
|
/* setup the load callback and create ntdll modref */
|
|
|
|
wine_dll_set_callback( load_builtin_callback );
|
2018-02-28 17:28:01 -08:00
|
|
|
|
2017-04-08 05:23:17 -07:00
|
|
|
diff --git a/include/winternl.h b/include/winternl.h
|
2018-05-17 18:31:57 -07:00
|
|
|
index 3659db1..bd6a8af 100644
|
2017-04-08 05:23:17 -07:00
|
|
|
--- a/include/winternl.h
|
|
|
|
+++ b/include/winternl.h
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -2164,8 +2164,7 @@ typedef struct _LDR_MODULE
|
2017-04-08 05:23:17 -07:00
|
|
|
ULONG Flags;
|
|
|
|
SHORT LoadCount;
|
|
|
|
SHORT TlsIndex;
|
|
|
|
- HANDLE SectionHandle;
|
|
|
|
- ULONG CheckSum;
|
|
|
|
+ LIST_ENTRY HashLinks;
|
|
|
|
ULONG TimeDateStamp;
|
|
|
|
HANDLE ActivationContext;
|
|
|
|
PVOID PatchInformation;
|
2018-05-17 18:31:57 -07:00
|
|
|
@@ -2175,6 +2174,9 @@ typedef struct _LDR_MODULE
|
2017-04-08 05:23:17 -07:00
|
|
|
PVOID ContextInformation;
|
|
|
|
ULONG_PTR OriginalBase;
|
|
|
|
LARGE_INTEGER LoadTime;
|
|
|
|
+
|
|
|
|
+ /* Not part of Win7 but used by Wine */
|
|
|
|
+ HANDLE SectionHandle;
|
|
|
|
} LDR_MODULE, *PLDR_MODULE;
|
|
|
|
|
2018-05-17 18:31:57 -07:00
|
|
|
typedef struct _LDR_DLL_LOADED_NOTIFICATION_DATA
|
2017-04-08 05:23:17 -07:00
|
|
|
--
|
2018-05-17 18:31:57 -07:00
|
|
|
1.9.1
|
2018-02-28 17:28:01 -08:00
|
|
|
|