From 0cdf92372d00abcf44ce7d2b0180be388a8f3849 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Wed, 27 Nov 2024 11:27:29 +0000 Subject: [PATCH] Fix [https://github.com/fancycode/MemoryModule/pull/91|Added x64 exception handling] --- win/MemoryModule.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/win/MemoryModule.c b/win/MemoryModule.c index 8c97caeeb1f..c5aae3cbad7 100644 --- a/win/MemoryModule.c +++ b/win/MemoryModule.c @@ -441,6 +441,17 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta) return TRUE; } +#ifdef _WIN64 +static BOOL +RegisterExceptionHandling(PMEMORYMODULE module) +{ + PIMAGE_DATA_DIRECTORY pDir = GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION); + PRUNTIME_FUNCTION pEntry = (PRUNTIME_FUNCTION)(module->codeBase + pDir->VirtualAddress); + UINT count = (pDir->Size / sizeof(IMAGE_RUNTIME_FUNCTION_ENTRY)) - 1; + return RtlAddFunctionTable(pEntry, count, (DWORD64)module->codeBase); +} +#endif + static BOOL BuildImportTable(PMEMORYMODULE module) { @@ -729,6 +740,12 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size, goto error; } +#ifdef _WIN64 + if (!RegisterExceptionHandling(result)) { + goto error; + } +#endif + // mark memory pages depending on section headers and release // sections that are marked as "discardable" if (!FinalizeSections(result)) {