Skip to content

MCAsmBackend: Merge addReloc into applyFixup #146820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions llvm/include/llvm/MC/MCAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ class LLVM_ABI MCAsmBackend {
/// Get information on a fixup kind.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const;

// Hook used by the default `addReloc` to check if a relocation is needed.
virtual bool shouldForceRelocation(const MCFixup &, const MCValue &) {
return false;
}

/// Hook to check if extra nop bytes must be inserted for alignment directive.
/// For some targets this may be necessary in order to support linker
/// relaxation. The number of bytes to insert are returned in Size.
Expand All @@ -116,9 +111,10 @@ class LLVM_ABI MCAsmBackend {
llvm_unreachable("Need to implement hook if target has custom fixups");
}

virtual bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
uint64_t &FixedValue, bool IsResolved);
void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &,
uint64_t &Value, bool IsResolved);

/// Determine if a relocation is required. In addition,
/// Apply the \p Value for given \p Fixup into the provided data fragment, at
/// the offset specified by the fixup and following the fixup kind as
/// appropriate. Errors (such as an out of range fixup value) should be
Expand Down
11 changes: 4 additions & 7 deletions llvm/lib/MC/MCAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,11 @@ bool MCAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
return fixupNeedsRelaxation(Fixup, Value);
}

bool MCAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target, uint64_t &FixedValue,
bool IsResolved) {
if (IsResolved && shouldForceRelocation(Fixup, Target))
IsResolved = false;
void MCAsmBackend::maybeAddReloc(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target, uint64_t &Value,
bool IsResolved) {
if (!IsResolved)
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
return IsResolved;
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
}

bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const {
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/MC/MCAssembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,

if (IsResolved && mc::isRelocRelocation(Fixup.getKind()))
IsResolved = false;
IsResolved = getBackend().addReloc(F, Fixup, Target, Value, IsResolved);
getBackend().applyFixup(F, Fixup, Target, Contents, Value, IsResolved);
return true;
}
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ class AArch64AsmBackend : public MCAsmBackend {

unsigned getFixupKindContainereSizeInBytes(unsigned Kind) const;

bool shouldForceRelocation(const MCFixup &Fixup,
const MCValue &Target) override;
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
};

} // end anonymous namespace
Expand Down Expand Up @@ -412,10 +411,13 @@ unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) con
}
}

void AArch64AsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void AArch64AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
if (IsResolved && shouldForceRelocation(Fixup, Target))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
MCFixupKind Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AMDGPUAsmBackend : public MCAsmBackend {

std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
bool shouldForceRelocation(const MCFixup &, const MCValue &) override;
bool shouldForceRelocation(const MCFixup &, const MCValue &);
};

} //End anonymous namespace
Expand Down Expand Up @@ -130,10 +130,13 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
}
}

void AMDGPUAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void AMDGPUAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
if (IsResolved && shouldForceRelocation(Fixup, Target))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
if (mc::isRelocation(Fixup.getKind()))
return;

Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,9 @@ void ARMAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
if (IsResolved && shouldForceRelocation(Fixup, Target))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
auto Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class ARMAsmBackend : public MCAsmBackend {

MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;

bool shouldForceRelocation(const MCFixup &Fixup,
const MCValue &Target) override;
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);

unsigned adjustFixupValue(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, uint64_t Value,
Expand Down
19 changes: 7 additions & 12 deletions llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,26 +368,21 @@ AVRAsmBackend::createObjectTargetWriter() const {
return createAVRELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType));
}

bool AVRAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target, uint64_t &FixedValue,
bool IsResolved) {
void AVRAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
// AVR sets the fixup value to bypass the assembly time overflow with a
// relocation.
if (IsResolved) {
auto TargetVal = MCValue::get(Target.getAddSym(), Target.getSubSym(),
FixedValue, Target.getSpecifier());
auto TargetVal = MCValue::get(Target.getAddSym(), Target.getSubSym(), Value,
Target.getSpecifier());
if (forceRelocation(F, Fixup, TargetVal))
IsResolved = false;
}
if (!IsResolved)
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
return IsResolved;
}
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);

void AVRAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
if (mc::isRelocation(Fixup.getKind()))
return;
adjustFixupValue(Fixup, Target, Value, &getContext());
Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ class AVRAsmBackend : public MCAsmBackend {
std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override;

bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
uint64_t &FixedValue, bool IsResolved) override;

void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
return true;
}

void BPFAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void BPFAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
if (Fixup.getKind() == FK_SecRel_8) {
// The Value is 0 for global variables, and the in-section offset
// for static variables. Write to the immediate field of the inst.
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,14 @@ bool CSKYAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
}
}

void CSKYAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void CSKYAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
if (IsResolved && shouldForceRelocation(Fixup, Target))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);

MCFixupKind Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class CSKYAsmBackend : public MCAsmBackend {
bool writeNopData(raw_ostream &OS, uint64_t Count,
const MCSubtargetInfo *STI) const override;

bool shouldForceRelocation(const MCFixup &Fixup,
const MCValue &Target) override;
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);

std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override;
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ class HexagonAsmBackend : public MCAsmBackend {
return Infos[Kind - FirstTargetFixupKind];
}

bool shouldForceRelocation(const MCFixup &Fixup,
const MCValue &Target) override {
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target) {
switch(Fixup.getTargetKind()) {
default:
llvm_unreachable("Unknown Fixup Kind!");
Expand Down Expand Up @@ -653,10 +652,13 @@ class HexagonAsmBackend : public MCAsmBackend {

} // namespace

void HexagonAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void HexagonAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data,
uint64_t FixupValue, bool IsResolved) {
if (IsResolved && shouldForceRelocation(Fixup, Target))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, FixupValue, IsResolved);
// When FixupValue is 0 the relocation is external and there
// is nothing for us to do.
if (!FixupValue)
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"

Expand Down Expand Up @@ -71,13 +72,15 @@ bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
return true;
}

void LanaiAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void LanaiAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool) {
bool IsResolved) {
if (!IsResolved)
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);

MCFixupKind Kind = Fixup.getKind();
Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);

if (!Value)
return; // This value doesn't change the encoding

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,13 @@ static void fixupLeb128(MCContext &Ctx, const MCFixup &Fixup,
Ctx.reportError(Fixup.getLoc(), "Invalid uleb128 value!");
}

void LoongArchAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void LoongArchAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
if (IsResolved && shouldForceRelocation(Fixup, Target))
IsResolved = false;
IsResolved = addReloc(F, Fixup, Target, Value, IsResolved);
if (!Value)
return; // Doesn't change encoding.

Expand Down Expand Up @@ -453,7 +456,8 @@ bool LoongArchAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target, uint64_t &FixedValue,
bool IsResolved) {
auto Fallback = [&]() {
return MCAsmBackend::addReloc(F, Fixup, Target, FixedValue, IsResolved);
MCAsmBackend::maybeAddReloc(F, Fixup, Target, FixedValue, IsResolved);
return true;
};
uint64_t FixedValueA, FixedValueB;
if (Target.getSubSym()) {
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LoongArchAsmBackend : public MCAsmBackend {
const MCTargetOptions &Options);

bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
uint64_t &FixedValue, bool IsResolved) override;
uint64_t &FixedValue, bool IsResolved);

void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
Expand All @@ -54,8 +54,7 @@ class LoongArchAsmBackend : public MCAsmBackend {
bool shouldInsertFixupForCodeAlign(MCAssembler &Asm,
MCAlignFragment &AF) override;

bool shouldForceRelocation(const MCFixup &Fixup,
const MCValue &Target) override;
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);

std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;

Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/Target/M68k/MCTargetDesc/M68kAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixupKindInfo.h"
Expand Down Expand Up @@ -77,11 +78,14 @@ class M68kAsmBackend : public MCAsmBackend {
};
} // end anonymous namespace

void M68kAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &, MutableArrayRef<char> Data,
uint64_t Value, bool) {
unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
void M68kAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
if (!IsResolved)
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);

unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
assert(Fixup.getOffset() + Size <= Data.size() && "Invalid fixup offset!");
// Check that uppper bits are either all zeros or all ones.
// Specifically ignore overflow/underflow as long as the leakage is
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/MSP430/MCTargetDesc/MSP430AsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup,
}
}

void MSP430AsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void MSP430AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
Value = adjustFixupValue(Fixup, Value, getContext());
MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
if (!Value)
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,13 @@ static unsigned calculateMMLEIndex(unsigned i) {
/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
/// data fragment, at the offset specified by the fixup and following the
/// fixup kind as appropriate.
void MipsAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
void MipsAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
if (IsResolved && shouldForceRelocation(Fixup, Target))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
MCFixupKind Kind = Fixup.getKind();
MCContext &Ctx = getContext();
Value = adjustFixupValue(Fixup, Value, Ctx);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class MipsAsmBackend : public MCAsmBackend {
bool writeNopData(raw_ostream &OS, uint64_t Count,
const MCSubtargetInfo *STI) const override;

bool shouldForceRelocation(const MCFixup &Fixup,
const MCValue &Target) override;
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
}; // class MipsAsmBackend

} // namespace
Expand Down
Loading
Loading