Skip to content

Commit 4f444df

Browse files
committed
Add back reloc patch
1 parent febcc79 commit 4f444df

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

src/Main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ static void WINAPI StartupInfoW(LPSTARTUPINFOA lpStartupInfo)
2121

2222
static void Initialize()
2323
{
24+
#ifdef _DEBUG
25+
SetProcessDEPPolicy(PROCESS_DEP_ENABLE);
26+
#endif
27+
2428
// Initialize MinHook
2529
MH_Initialize();
2630

src/modules/Patches.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "util/Hooking.h"
66
#include "game/Game.h"
77
#include "MainMenu.h"
8+
#include "patches/Reloc.h"
89

910
// Instance of patches so we can get it in our hooks without calling GetModule<T>
1011
static Patches* s_patches;
@@ -86,6 +87,9 @@ Patches::Patches()
8687
// Insert hooks
8788
MH_CreateHook((void*)GET_ADDRESS(0x40CA80, 0x43AB40, 0x000000), RenderG2_MotionBlur, (void**)&s_RenderG2_MotionBlur);
8889
MH_CreateHook((void*)GET_ADDRESS(0x450430, 0x452A90, 0x000000), GAMELOOP_HandleScreenWipes, (void**)&s_GAMELOOP_HandleScreenWipes);
90+
91+
// Insert reloc hook
92+
MH_CreateHook((void*)GET_ADDRESS(0x4642F0, 0x467E60, 0x000000), MakePeHandle, nullptr);
8993
#endif
9094

9195
// Insert DeathState hooks

src/modules/patches/Reloc.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "Reloc.h"
2+
3+
int MakePeHandle(IMAGE_DOS_HEADER* peData, PEHANDLE* pe)
4+
{
5+
pe->lpImage = peData;
6+
7+
if (peData->e_magic != IMAGE_DOS_SIGNATURE)
8+
{
9+
return RELOC_NON_EXECUTABLE;
10+
}
11+
12+
auto header = (IMAGE_NT_HEADERS*)((char*)peData + peData->e_lfanew);
13+
pe->lpHeader = header;
14+
pe->firstReloc = 1;
15+
16+
// Check for the PE signature or the relocated signature
17+
if (header->Signature != IMAGE_NT_SIGNATURE)
18+
{
19+
if (header->Signature != 0x4551)
20+
{
21+
return RELOC_NON_EXECUTABLE;
22+
}
23+
24+
pe->firstReloc = 0;
25+
}
26+
27+
if (header->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
28+
{
29+
return RELOC_NON_EXECUTABLE;
30+
}
31+
32+
auto flags = header->FileHeader.Characteristics;
33+
34+
if ((flags & IMAGE_FILE_EXECUTABLE_IMAGE) == 0 || (flags & IMAGE_FILE_DLL) == 0)
35+
{
36+
return RELOC_NON_EXECUTABLE;
37+
}
38+
39+
// Read the sections
40+
pe->lpSectionTable = IMAGE_FIRST_SECTION(header);
41+
42+
if (header->FileHeader.NumberOfSections <= 0)
43+
{
44+
return RELOC_SUCCESS;
45+
}
46+
47+
for (int i = 0; i < header->FileHeader.NumberOfSections; i++)
48+
{
49+
auto section = pe->lpSectionTable[i];
50+
51+
if (section.Misc.VirtualSize > section.SizeOfRawData)
52+
{
53+
// Since there is no virtual memory allocated for the sections the raw data size
54+
// cannot be smaller than the virtual size
55+
return RELOC_INVALID_SECTION;
56+
}
57+
58+
// Check if the section is executable
59+
if (pe->firstReloc && (section.Characteristics & IMAGE_SCN_CNT_CODE) != 0)
60+
{
61+
// Change the protection of the memory region to executable to allow code to execute
62+
// even with the Data Execution Prevention (DEP) enabled in Windows.
63+
DWORD oldProtect;
64+
VirtualProtect((void*)((char*)peData + section.PointerToRawData), section.SizeOfRawData, PAGE_EXECUTE_READWRITE, &oldProtect);
65+
}
66+
67+
// Set the virtual size to the raw size since there's no virtual memory allocated
68+
pe->lpSectionTable[i].Misc.VirtualSize = section.SizeOfRawData;
69+
}
70+
71+
return RELOC_SUCCESS;
72+
}

src/modules/patches/Reloc.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <Windows.h>
4+
5+
struct PEHANDLE
6+
{
7+
IMAGE_NT_HEADERS* lpHeader;
8+
IMAGE_SECTION_HEADER* lpSectionTable;
9+
int firstReloc;
10+
IMAGE_DOS_HEADER* lpImage;
11+
};
12+
13+
// This enum does not exist in the PDB, therefore the names are guessed
14+
enum RelocCode
15+
{
16+
RELOC_INVALID_SECTION = -1,
17+
RELOC_SUCCESS = 0,
18+
RELOC_NON_EXECUTABLE = 3,
19+
};
20+
21+
int MakePeHandle(IMAGE_DOS_HEADER* peData, PEHANDLE* pe);

0 commit comments

Comments
 (0)