fix(relinker): exact opcode instead of any rip-relative disp in unused nid filter

This commit is contained in:
boykopovar
2026-09-09 10:14:45 +03:00
parent a81a1bf1b5
commit 3b5ae707cd
3 changed files with 27 additions and 0 deletions
@@ -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;
};
}
@@ -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) {
@@ -1,10 +1,28 @@
#include <relinker/analysis/UnusedNidFilter/IGotAccessIndex.hpp>
#include <codegen/x86/X64InstructionDecoder.hpp>
#include <codegen/x86/X64OpcodeConstants.hpp>
#include <unordered_set>
#include <cstring>
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<VirtualAddress> accessed)
@@ -35,6 +53,7 @@ std::unique_ptr<IGotAccessIndex> BuildGotAccessIndex(
Codegen::DecodedInstructionInfo info = decoder.DecodeInstruction(text.data() + bufOff, available);
if (!info.HasRipRelativeDisp) continue;
if (!ReadsMemoryOperandAsPointer(info)) continue;
VirtualAddress nextVaddr = va + static_cast<VirtualAddress>(info.Length);
std::int32_t disp = 0;