From 75021eff5f37a58278fc93941826914bc1044436 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Sun, 24 Oct 2010 03:03:35 +0200 Subject: [PATCH] adjust addresses to protect by upper 32bit for 64bit modules --- MemoryModule.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/MemoryModule.c b/MemoryModule.c index 6525b1e..c69de46 100644 --- a/MemoryModule.c +++ b/MemoryModule.c @@ -129,6 +129,11 @@ FinalizeSections(PMEMORYMODULE module) { int i; PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(module->headers); +#ifdef _WIN64 + POINTER_TYPE imageOffset = (module->headers->OptionalHeader.ImageBase & 0xffffffff00000000); +#else + #define imageOffset 0 +#endif // loop through all sections and change access flags for (i=0; iheaders->FileHeader.NumberOfSections; i++, section++) @@ -141,7 +146,7 @@ FinalizeSections(PMEMORYMODULE module) if (section->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) { // section is not needed any more and can safely be freed - VirtualFree((LPVOID)(POINTER_TYPE)section->Misc.PhysicalAddress, section->SizeOfRawData, MEM_DECOMMIT); + VirtualFree((LPVOID)((POINTER_TYPE)section->Misc.PhysicalAddress | imageOffset), section->SizeOfRawData, MEM_DECOMMIT); continue; } @@ -163,13 +168,16 @@ FinalizeSections(PMEMORYMODULE module) if (size > 0) { // change memory access flags - if (VirtualProtect((LPVOID)(POINTER_TYPE)section->Misc.PhysicalAddress, section->SizeOfRawData, protect, &oldProtect) == 0) + if (VirtualProtect((LPVOID)((POINTER_TYPE)section->Misc.PhysicalAddress | imageOffset), section->SizeOfRawData, protect, &oldProtect) == 0) #ifdef DEBUG_OUTPUT OutputLastError("Error protecting memory page") #endif ; } } +#ifndef _WIN64 +#undef imageOffset +#endif } static void