Implement xex imports

This commit is contained in:
Sajid 2024-09-13 16:13:19 +06:00
parent cc212f4058
commit 5cef220e9f
2 changed files with 81 additions and 3 deletions

View File

@ -1,6 +1,7 @@
#include "xex.h"
#include "image.h"
#include <cassert>
#include <vector>
Image Xex2LoadImage(const uint8_t* data)
{
@ -79,5 +80,35 @@ Image Xex2LoadImage(const uint8_t* data)
section.Misc.VirtualSize, flags, image.data.get() + section.VirtualAddress);
}
auto* imports = Xex2FindOptionalHeader<XEX_IMPORT_HEADER>(header, XEX_HEADER_IMPORT_LIBRARIES);
if (imports != nullptr)
{
std::vector<std::string_view> stringTable;
auto* pStrTable = reinterpret_cast<const char*>(imports + 1);
for (size_t i = 0; i < imports->NumImports; i++)
{
stringTable.emplace_back(pStrTable);
pStrTable += strlen(pStrTable) + 1;
}
auto* library = (XEX_IMPORT_LIBRARY*)(((char*)imports) + sizeof(XEX_IMPORT_HEADER) + imports->SizeOfStringTable);
for (size_t i = 0; i < stringTable.size(); i++)
{
auto* descriptors = (XEX_IMPORT_DESCRIPTOR*)(library + 1);
for (size_t im = 0; im < library->NumberOfImports; im++)
{
auto originalThunk = (XEX_THUNK_DATA*)image.Find(descriptors[im].FirstThunk);
auto thunkType = originalThunk->Function >> 24;
if (thunkType == 1)
{
uint32_t thunk[4] = { 0x00000060, 0x00000060, 0x00000060, 0x2000804E };
memcpy(originalThunk, thunk, sizeof(thunk));
}
}
}
}
return image;
}

View File

@ -7,6 +7,12 @@
#define XEX_ENCRYPTION_NONE 0
enum _XEX_THUNK_TYPES
{
XEX_THUNK_VARIABLE = 0,
XEX_THUNK_FUNCTION = 1,
};
enum _XEX_OPTIONAL_HEADER_TYPES
{
XEX_HEADER_RESOURCE_INFO = 0x000002FF,
@ -54,10 +60,51 @@ typedef struct _XEX_BASIC_FILE_COMPRESSION_INFO
be<uint32_t> SizeOfPadding;
} XEX_BASIC_FILE_COMPRESSION_INFO;
typedef struct _XEX_THUNK_DATA {
union
{
struct
{
WORD Ordinal : 16;
WORD Hint : 8;
WORD Type : 8;
} OriginalData;
XDWORD Ordinal;
XDWORD Function;
XDWORD AddressOfData;
// For easier swapping
DWORD Data;
};
} XEX_THUNK_DATA;
typedef struct _XEX_IMPORT_HEADER {
XDWORD SizeOfHeader;
XDWORD SizeOfStringTable;
XDWORD NumImports;
} XEX_IMPORT_HEADER;
typedef struct _XEX_IMPORT_LIBRARY {
XDWORD Size;
char NextImportDigest[0x14];
XDWORD ID;
XDWORD Version;
XDWORD MinVersion;
XWORD Name;
XWORD NumberOfImports;
} XEX_IMPORT_LIBRARY;
static_assert(sizeof(XEX_IMPORT_LIBRARY) == 0x28);
typedef struct _XEX_IMPORT_DESCRIPTOR {
XDWORD FirstThunk; // VA XEX_THUNK_DATA
} XEX_IMPORT_DESCRIPTOR;
typedef struct _XEX_OPTIONAL_HEADER
{
be<uint32_t> Type;
be<uint32_t> Address;
XDWORD Type;
XDWORD Address;
} XEX_OPTIONAL_HEADER;
typedef struct _XEX2_SECURITY_INFO