Skip to content

Commit

Permalink
Correctly store PE's entry point and SizeOfImage for 32-bit buid
Browse files Browse the repository at this point in the history
We can't rely on IntPtr as it's stored as a 32-bit value for x86 builds,
which can still handle 64-bit PE.
  • Loading branch information
lucasg committed Jul 6, 2018
1 parent 6925e68 commit 4a77d0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 3 additions & 3 deletions ClrPhlib/include/ClrPhlib.h
Expand Up @@ -105,9 +105,9 @@ namespace System {
DateTime ^ Time;
Int16 Magic;

IntPtr ImageBase;
Int64 ImageBase;
Int32 SizeOfImage;
IntPtr EntryPoint;
Int64 EntryPoint;


Int32 Checksum;
Expand Down Expand Up @@ -163,7 +163,7 @@ namespace System {
!PE();

// Initalize PeProperties struct once the PE has been loaded into memory
void InitProperties();
bool InitProperties();

private:

Expand Down
26 changes: 18 additions & 8 deletions ClrPhlib/src/managed/PE.cpp
Expand Up @@ -31,13 +31,22 @@ PE::!PE() {

bool PE::Load()
{
// Load PE as mapped section
wchar_t* PvFilepath = (wchar_t*)(Marshal::StringToHGlobalUni(Filepath)).ToPointer();
this->LoadSuccessful = m_Impl->LoadPE(PvFilepath);
Marshal::FreeHGlobal(IntPtr((void*)PvFilepath));

if (LoadSuccessful)
InitProperties();
if (!LoadSuccessful) {
return false;
}

// Parse PE
LoadSuccessful &= InitProperties();
if (!LoadSuccessful) {
m_Impl->UnloadPE();
return false;
}

Marshal::FreeHGlobal(IntPtr((void*)PvFilepath));

return LoadSuccessful;
}
Expand All @@ -48,7 +57,7 @@ void PE::Unload()
m_Impl->UnloadPE();
}

void PE::InitProperties()
bool PE::InitProperties()
{
LARGE_INTEGER time;
SYSTEMTIME systemTime;
Expand All @@ -69,17 +78,17 @@ void PE::InitProperties()
{
PIMAGE_OPTIONAL_HEADER32 OptionalHeader = (PIMAGE_OPTIONAL_HEADER32) &PvMappedImage.NtHeaders->OptionalHeader;

Properties->ImageBase = (IntPtr) (Int32) OptionalHeader->ImageBase;
Properties->ImageBase = (Int64) OptionalHeader->ImageBase;
Properties->SizeOfImage = OptionalHeader->SizeOfImage;
Properties->EntryPoint = (IntPtr) (Int32) OptionalHeader->AddressOfEntryPoint;
Properties->EntryPoint = (Int64) OptionalHeader->AddressOfEntryPoint;
}
else
{
PIMAGE_OPTIONAL_HEADER64 OptionalHeader = (PIMAGE_OPTIONAL_HEADER64)&PvMappedImage.NtHeaders->OptionalHeader;

Properties->ImageBase = (IntPtr)(Int64)OptionalHeader->ImageBase;
Properties->ImageBase = (Int64)OptionalHeader->ImageBase;
Properties->SizeOfImage = OptionalHeader->SizeOfImage;
Properties->EntryPoint = (IntPtr)(Int64)OptionalHeader->AddressOfEntryPoint;
Properties->EntryPoint = (Int64)OptionalHeader->AddressOfEntryPoint;

}

Expand All @@ -91,6 +100,7 @@ void PE::InitProperties()
Properties->DllCharacteristics = PvMappedImage.NtHeaders->OptionalHeader.DllCharacteristics;

Properties->FileSize = PvMappedImage.Size;
return true;
}


Expand Down

0 comments on commit 4a77d0d

Please sign in to comment.