Skip to content

Commit

Permalink
Add back reloc patch
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Feb 22, 2024
1 parent febcc79 commit 4f444df
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ static void WINAPI StartupInfoW(LPSTARTUPINFOA lpStartupInfo)

static void Initialize()
{
#ifdef _DEBUG
SetProcessDEPPolicy(PROCESS_DEP_ENABLE);
#endif

// Initialize MinHook
MH_Initialize();

Expand Down
4 changes: 4 additions & 0 deletions src/modules/Patches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "util/Hooking.h"
#include "game/Game.h"
#include "MainMenu.h"
#include "patches/Reloc.h"

// Instance of patches so we can get it in our hooks without calling GetModule<T>
static Patches* s_patches;
Expand Down Expand Up @@ -86,6 +87,9 @@ Patches::Patches()
// Insert hooks
MH_CreateHook((void*)GET_ADDRESS(0x40CA80, 0x43AB40, 0x000000), RenderG2_MotionBlur, (void**)&s_RenderG2_MotionBlur);
MH_CreateHook((void*)GET_ADDRESS(0x450430, 0x452A90, 0x000000), GAMELOOP_HandleScreenWipes, (void**)&s_GAMELOOP_HandleScreenWipes);

// Insert reloc hook
MH_CreateHook((void*)GET_ADDRESS(0x4642F0, 0x467E60, 0x000000), MakePeHandle, nullptr);
#endif

// Insert DeathState hooks
Expand Down
72 changes: 72 additions & 0 deletions src/modules/patches/Reloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "Reloc.h"

int MakePeHandle(IMAGE_DOS_HEADER* peData, PEHANDLE* pe)
{
pe->lpImage = peData;

if (peData->e_magic != IMAGE_DOS_SIGNATURE)
{
return RELOC_NON_EXECUTABLE;
}

auto header = (IMAGE_NT_HEADERS*)((char*)peData + peData->e_lfanew);
pe->lpHeader = header;
pe->firstReloc = 1;

// Check for the PE signature or the relocated signature
if (header->Signature != IMAGE_NT_SIGNATURE)
{
if (header->Signature != 0x4551)
{
return RELOC_NON_EXECUTABLE;
}

pe->firstReloc = 0;
}

if (header->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
{
return RELOC_NON_EXECUTABLE;
}

auto flags = header->FileHeader.Characteristics;

if ((flags & IMAGE_FILE_EXECUTABLE_IMAGE) == 0 || (flags & IMAGE_FILE_DLL) == 0)
{
return RELOC_NON_EXECUTABLE;
}

// Read the sections
pe->lpSectionTable = IMAGE_FIRST_SECTION(header);

if (header->FileHeader.NumberOfSections <= 0)
{
return RELOC_SUCCESS;
}

for (int i = 0; i < header->FileHeader.NumberOfSections; i++)
{
auto section = pe->lpSectionTable[i];

if (section.Misc.VirtualSize > section.SizeOfRawData)
{
// Since there is no virtual memory allocated for the sections the raw data size
// cannot be smaller than the virtual size
return RELOC_INVALID_SECTION;
}

// Check if the section is executable
if (pe->firstReloc && (section.Characteristics & IMAGE_SCN_CNT_CODE) != 0)
{
// Change the protection of the memory region to executable to allow code to execute
// even with the Data Execution Prevention (DEP) enabled in Windows.
DWORD oldProtect;
VirtualProtect((void*)((char*)peData + section.PointerToRawData), section.SizeOfRawData, PAGE_EXECUTE_READWRITE, &oldProtect);
}

// Set the virtual size to the raw size since there's no virtual memory allocated
pe->lpSectionTable[i].Misc.VirtualSize = section.SizeOfRawData;
}

return RELOC_SUCCESS;
}
21 changes: 21 additions & 0 deletions src/modules/patches/Reloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <Windows.h>

struct PEHANDLE
{
IMAGE_NT_HEADERS* lpHeader;
IMAGE_SECTION_HEADER* lpSectionTable;
int firstReloc;
IMAGE_DOS_HEADER* lpImage;
};

// This enum does not exist in the PDB, therefore the names are guessed
enum RelocCode
{
RELOC_INVALID_SECTION = -1,
RELOC_SUCCESS = 0,
RELOC_NON_EXECUTABLE = 3,
};

int MakePeHandle(IMAGE_DOS_HEADER* peData, PEHANDLE* pe);

0 comments on commit 4f444df

Please sign in to comment.