From 880a4cd0378b8d37e4198f5cfc4f8f33cb480bd8 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 31 Aug 2024 15:14:38 +0200 Subject: [PATCH] Optimize relocation handling --- src/windows_emulator/module_mapper.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/windows_emulator/module_mapper.cpp b/src/windows_emulator/module_mapper.cpp index ee47405b..053fde73 100644 --- a/src/windows_emulator/module_mapper.cpp +++ b/src/windows_emulator/module_mapper.cpp @@ -53,10 +53,12 @@ namespace emulator_object relocation_object{emu, binary.image_base + directory->VirtualAddress}; const auto end_address = relocation_object.value() + directory->Size; + std::vector relocations{}; + while (relocation_object.value() < end_address) { const auto relocation = relocation_object.read(); - if (relocation.VirtualAddress <= 0) + if (relocation.VirtualAddress <= 0 || relocation.SizeOfBlock <= 0) { break; } @@ -66,12 +68,14 @@ namespace const auto data_size = relocation.SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION); const auto entry_count = data_size / sizeof(uint16_t); - std::vector relocations{}; - relocations.resize(entry_count); + if (relocations.size() < entry_count) + { + relocations.resize(entry_count); + } emu.read_memory(relocation_object.value() + relocation_object.size(), relocations.data(), data_size); - for (const auto entry : relocations) + for (const auto entry : std::span(relocations.data(), entry_count)) { const int type = entry >> 12; const int offset = entry & 0xfff;