Skip to content

Commit 364dcbd

Browse files
committed
Fixed entry point in relocated libraries.
1 parent 5a448ce commit 364dcbd

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

MemoryModule.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ CopySections(const unsigned char *data, PIMAGE_NT_HEADERS old_headers, PMEMORYMO
121121
}
122122
}
123123

124+
static SIZE_T
125+
GetTextSectionPointerToRawData(PMEMORYMODULE module)
126+
{
127+
int i;
128+
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(module->headers);
129+
for (i = 0; i < module->headers->FileHeader.NumberOfSections; i++, section++) {
130+
if (section->SizeOfRawData != 0 && strcmp(section->Name, ".text") == 0) {
131+
return section->PointerToRawData;
132+
}
133+
}
134+
return 0;
135+
}
136+
124137
// Protection flags for memory pages (Executable, Readable, Writeable)
125138
static int ProtectionFlags[2][2][2] = {
126139
{
@@ -377,7 +390,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
377390
}
378391

379392
#ifdef _WIN64
380-
if (old_header->FileHeader.Machine == IMAGE_FILE_MACHINE_I386) {
393+
if (old_header->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) {
381394
#else
382395
if (old_header->FileHeader.Machine != IMAGE_FILE_MACHINE_I386) {
383396
#endif
@@ -461,7 +474,15 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
461474
// get entry point of loaded library
462475
if (result->headers->OptionalHeader.AddressOfEntryPoint != 0) {
463476
if (result->isDLL) {
464-
DllEntryProc DllEntry = (DllEntryProc) (code + result->headers->OptionalHeader.AddressOfEntryPoint);
477+
DllEntryProc DllEntry;
478+
if (result->isRelocated == TRUE) {
479+
DllEntry = (DllEntryProc)(code + result->headers->OptionalHeader.BaseOfCode
480+
+ GetTextSectionPointerToRawData(result)
481+
+ result->headers->OptionalHeader.FileAlignment);
482+
}
483+
else {
484+
DllEntry = (DllEntryProc)(code + result->headers->OptionalHeader.AddressOfEntryPoint);
485+
}
465486
// notify library about attaching to process
466487
successfull = (*DllEntry)((HINSTANCE)code, DLL_PROCESS_ATTACH, 0);
467488
if (!successfull) {

0 commit comments

Comments
 (0)