mirror of
https://github.com/hedge-dev/XenonRecomp.git
synced 2025-07-23 21:43:55 +00:00
Implement ELF support
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
#include "xex.h"
|
||||
#include "image.h"
|
||||
#include <cassert>
|
||||
|
||||
std::unique_ptr<uint8_t[]> Xex2LoadImage(const uint8_t* data)
|
||||
Image Xex2LoadImage(const uint8_t* data)
|
||||
{
|
||||
auto* header = reinterpret_cast<const XEX_HEADER*>(data);
|
||||
auto* security = reinterpret_cast<const XEX2_SECURITY_INFO*>(data + header->AddressOfSecurityInfo);
|
||||
|
||||
const auto* compressionInfo = Xex2FindOptionalHeader<XEX_FILE_FORMAT_INFO>(header, XEX_HEADER_FILE_FORMAT_INFO);
|
||||
|
||||
Image image{};
|
||||
std::unique_ptr<uint8_t[]> result{};
|
||||
size_t imageSize = security->SizeOfImage;
|
||||
|
||||
// Decompress image
|
||||
if (compressionInfo != nullptr)
|
||||
{
|
||||
assert(compressionInfo->CompressionType >= XEX_COMPRESSION_BASIC);
|
||||
@@ -49,5 +52,32 @@ std::unique_ptr<uint8_t[]> Xex2LoadImage(const uint8_t* data)
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
image.data = std::move(result);
|
||||
image.size = imageSize;
|
||||
|
||||
// Map image
|
||||
const auto* dosHeader = reinterpret_cast<IMAGE_DOS_HEADER*>(image.data.get());
|
||||
const auto* ntHeaders = reinterpret_cast<IMAGE_NT_HEADERS32*>(image.data.get() + dosHeader->e_lfanew);
|
||||
|
||||
image.base = ntHeaders->OptionalHeader.ImageBase;
|
||||
image.entry_point = image.base + ntHeaders->OptionalHeader.AddressOfEntryPoint;
|
||||
|
||||
const auto numSections = ntHeaders->FileHeader.NumberOfSections;
|
||||
const auto* sections = reinterpret_cast<const IMAGE_SECTION_HEADER*>(ntHeaders + 1);
|
||||
|
||||
for (size_t i = 0; i < numSections; i++)
|
||||
{
|
||||
const auto& section = sections[i];
|
||||
uint8_t flags{};
|
||||
|
||||
if (section.Characteristics & IMAGE_SCN_CNT_CODE)
|
||||
{
|
||||
flags |= SectionFlags_Code;
|
||||
}
|
||||
|
||||
image.Map(reinterpret_cast<const char*>(section.Name), section.VirtualAddress,
|
||||
section.Misc.VirtualSize, flags, image.data.get() + section.VirtualAddress);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
Reference in New Issue
Block a user