From 768f2341f19d1e2e9b8fd05d1d667f46ab21dc71 Mon Sep 17 00:00:00 2001 From: IcePixelx <41352111+PixieCore@users.noreply.github.com> Date: Tue, 17 Aug 2021 23:02:14 +0200 Subject: [PATCH] Added separate function to resolve relative addresses in address.h --- shared/include/address.h | 48 +++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/shared/include/address.h b/shared/include/address.h index 9a9d88b6..a595bc55 100644 --- a/shared/include/address.h +++ b/shared/include/address.h @@ -276,26 +276,21 @@ public: MemoryAddress FollowNearCall(std::ptrdiff_t opcodeOffset = 0x1, std::ptrdiff_t nextInstructionOffset = 0x5) { - // Skip E9 opcode. - std::uintptr_t skipOpCode = ptr + opcodeOffset; - - // Get 4-byte long relative address. - std::int32_t relativeAddress = *reinterpret_cast(skipOpCode); - - // Get location of next instruction. - std::uintptr_t nextInstruction = ptr + nextInstructionOffset; - - // Get function location via adding relative address to next instruction. - return MemoryAddress(nextInstruction + relativeAddress); + return ResolveRelativeAddress(opcodeOffset, nextInstructionOffset); } MemoryAddress FollowNearCallSelf(std::ptrdiff_t opcodeOffset = 0x1, std::ptrdiff_t nextInstructionOffset = 0x5) { - // Skip E9 opcode. - std::uintptr_t skipOpCode = ptr + opcodeOffset; + return ResolveRelativeAddressSelf(opcodeOffset, nextInstructionOffset); + } + + MemoryAddress ResolveRelativeAddressSelf(std::ptrdiff_t registerOffset = 0x1, std::ptrdiff_t nextInstructionOffset = 0x4) + { + // Skip register. + std::uintptr_t skipRegister = ptr + registerOffset; // Get 4-byte long relative address. - std::int32_t relativeAddress = *reinterpret_cast(skipOpCode); + std::int32_t relativeAddress = *reinterpret_cast(skipRegister); // Get location of next instruction. std::uintptr_t nextInstruction = ptr + nextInstructionOffset; @@ -304,7 +299,22 @@ public: ptr = nextInstruction + relativeAddress; return *this; } - + + MemoryAddress ResolveRelativeAddress(std::ptrdiff_t registerOffset = 0x1, std::ptrdiff_t nextInstructionOffset = 0x4) + { + // Skip register. + std::uintptr_t skipRegister = ptr + registerOffset; + + // Get 4-byte long relative address. + std::int32_t relativeAddress = *reinterpret_cast(skipRegister); + + // Get location of next instruction. + std::uintptr_t nextInstruction = ptr + nextInstructionOffset; + + // Get function location via adding relative address to next instruction. + return MemoryAddress(nextInstruction + relativeAddress); + } + private: std::uintptr_t ptr = 0; }; @@ -339,14 +349,6 @@ public: return ModuleSections(); } - void PrintSections() - { - for (ModuleSections& currentSection : moduleSections) - { - printf(" [+Module: %s+]%s, %p\n", moduleName.c_str(), currentSection.sectionName.c_str(), reinterpret_cast(currentSection.sectionStartAddress)); - } - } - Module() = default; Module(std::string moduleName) : moduleName(moduleName) {