diff --git a/core/codegen/include/codegen/CodegenTypes.hpp b/core/codegen/include/codegen/CodegenTypes.hpp index 2a0bc4b46..5f5e971c6 100644 --- a/core/codegen/include/codegen/CodegenTypes.hpp +++ b/core/codegen/include/codegen/CodegenTypes.hpp @@ -52,6 +52,9 @@ struct DecodedInstructionInfo { std::uint8_t ModRmByte; bool HasBranchTarget; std::int64_t BranchDisp; + bool IsTwoByteOpcode; + std::uint8_t Opcode; + std::uint8_t ModRmRegField; }; } diff --git a/core/codegen/src/x86/X64InstructionDecoder.cpp b/core/codegen/src/x86/X64InstructionDecoder.cpp index 51adb0929..906d49956 100644 --- a/core/codegen/src/x86/X64InstructionDecoder.cpp +++ b/core/codegen/src/x86/X64InstructionDecoder.cpp @@ -356,6 +356,9 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction( op = data[pos++]; } + info.Opcode = op; + info.IsTwoByteOpcode = twoByteOpcode; + if (!twoByteOpcode) { if (op == OneByteCallRel32) { std::int32_t disp = 0; @@ -400,6 +403,7 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction( info.HasModRm = true; info.ModRmByte = modrm; std::uint8_t reg = (modrm >> ModRmRegShift) & ModRmRegMask; + info.ModRmRegField = reg; std::uint8_t mod = (modrm >> ModRmModShift) & ModRmModMask; std::uint8_t rm = modrm & ModRmRmMask; if (reg == 2) { @@ -493,6 +497,7 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction( std::uint8_t modrm = data[pos]; info.HasModRm = true; info.ModRmByte = modrm; + info.ModRmRegField = (modrm >> ModRmRegShift) & ModRmRegMask; std::uint8_t mod = (modrm >> ModRmModShift) & ModRmModMask; std::uint8_t rm = modrm & ModRmRmMask; if (mod == ModRmModIndirect && rm == ModRmRmRipRelative) { diff --git a/core/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp b/core/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp index cf9f0b94d..9ca3e74e7 100644 --- a/core/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp +++ b/core/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp @@ -1,10 +1,28 @@ #include #include +#include #include #include namespace Relinker::UnusedNidFilter { +namespace { + +constexpr std::uint8_t OneByteMovLoadRm8 = 0x8A; +constexpr std::uint8_t OneByteMovLoadRm = 0x8B; +constexpr std::uint8_t Grp5RegCallIndirect = 2; +constexpr std::uint8_t Grp5RegJmpIndirect = 4; + +bool ReadsMemoryOperandAsPointer(const Codegen::DecodedInstructionInfo& info) { + if (info.IsTwoByteOpcode) return false; + if (info.Opcode == OneByteMovLoadRm8 || info.Opcode == OneByteMovLoadRm) return true; + if (info.Opcode == Codegen::X64OpcodeConstants::OneByteGrp5Rm) + return info.ModRmRegField == Grp5RegCallIndirect || info.ModRmRegField == Grp5RegJmpIndirect; + return false; +} + +} + class GotAccessIndex : public IGotAccessIndex { public: explicit GotAccessIndex(std::unordered_set accessed) @@ -35,6 +53,7 @@ std::unique_ptr BuildGotAccessIndex( Codegen::DecodedInstructionInfo info = decoder.DecodeInstruction(text.data() + bufOff, available); if (!info.HasRipRelativeDisp) continue; + if (!ReadsMemoryOperandAsPointer(info)) continue; VirtualAddress nextVaddr = va + static_cast(info.Length); std::int32_t disp = 0;