From 7efb166788803f284e70f84f9a2bb970587c5288 Mon Sep 17 00:00:00 2001 From: gmh5225 <2315157@qq.com> Date: Thu, 14 Apr 2022 15:02:35 +0800 Subject: [PATCH] Support /FIXED --- src/x86application.cpp | 65 ++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/src/x86application.cpp b/src/x86application.cpp index f297b20..a929818 100644 --- a/src/x86application.cpp +++ b/src/x86application.cpp @@ -37,11 +37,14 @@ X86BA_TEMPLATE void X86BinaryApplication::loadFromFile(std::string_view _originalRelocs.clear(); - _peFile.getRelocDir().forEachEntry( - [this](pe::BlockEntry const& entry) - { - _originalRelocs.insert(entry.getRva()); - }); + if (_peFile.getRelocDir().isPresent()) + { + _peFile.getRelocDir().forEachEntry( + [this](pe::BlockEntry const& entry) + { + _originalRelocs.insert(entry.getRva()); + }); + } // printf("* - Number of relocations: 0x%x\n", _originalRelocs.size()); } @@ -714,7 +717,10 @@ X86BA_TEMPLATE bool X86BinaryApplication::transformRoutines() } // Guesstimate.. Fix this.. - _peFile.getRelocDir().extend(((_routines.size() + 0x3) & ~0x3) * 0x100); + if (_peFile.getRelocDir().isPresent()) + { + _peFile.getRelocDir().extend(((_routines.size() + 0x3) & ~0x3) * 0x100); + } for (auto& rtn : _routines) { @@ -856,36 +862,39 @@ X86BA_TEMPLATE void X86BinaryApplication::compile() // // Append all relocations - for (auto it = _relocBlocks.begin(); it != _relocBlocks.end(); ++it) + if (_peFile.getRelocDir().isPresent()) { - u32 rva = it->first; - std::vector const &entries = it->second; + for (auto it = _relocBlocks.begin(); it != _relocBlocks.end(); ++it) + { + u32 rva = it->first; + std::vector const& entries = it->second; - if (entries.empty()) - continue; + if (entries.empty()) + continue; - // Pad the amount of relocs to ensure 32bit alignment - size_t relocEntrySize = (entries.size() + 0x3) & ~0x3; - size_t numHandled = 0ull; + // Pad the amount of relocs to ensure 32bit alignment + size_t relocEntrySize = (entries.size() + 0x3) & ~0x3; + size_t numHandled = 0ull; - // Create the BlockStream and add all relocatables. - pe::BlockStream bs = _peFile.getRelocDir().createBlock(rva, relocEntrySize); + // Create the BlockStream and add all relocatables. + pe::BlockStream bs = _peFile.getRelocDir().createBlock(rva, relocEntrySize); - if (!bs.valid()) - continue; + if (!bs.valid()) + continue; - // printf("relocEntrySize: 0x%llx entries\n", relocEntrySize); + // printf("relocEntrySize: 0x%llx entries\n", relocEntrySize); - for (auto& entry : entries) - { - bs.append(entry.type, entry.offset); - ++numHandled; - } + for (auto& entry : entries) + { + bs.append(entry.type, entry.offset); + ++numHandled; + } - // Pad. - for (size_t handled = numHandled; handled < relocEntrySize; ++handled) - { - bs.append(pe::RelocationType::REL_BASED_ABSOLUTE, 0); + // Pad. + for (size_t handled = numHandled; handled < relocEntrySize; ++handled) + { + bs.append(pe::RelocationType::REL_BASED_ABSOLUTE, 0); + } } }