[llvm] MCAsmBackend: Merge addReloc into applyFixup (PR #146820)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 2 22:41:00 PDT 2025
https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/146820
>From 83242c815b3089ee7295b3cdc8125b3660f466a6 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Wed, 2 Jul 2025 22:00:23 -0700
Subject: [PATCH] MCAsmBackend: Merge addReloc into applyFixup
Follow-up to #141333. MCAssembler.cpp calls both addReloc and
applyFixup, with the default addReloc calling shouldForceRelocation.
The three virtual calls are inefficient.
This change changes applyFixup to potentially add a relocation, allowing
targets to remove `maybeAddReloc` two and eliminate two virtual calls.
maybeAddReloc is the previous default `addReloc`.
Refactor targets overridding `addReloc` to call their customized
`addReloc` instead.
---
llvm/include/llvm/MC/MCAsmBackend.h | 10 ++----
llvm/lib/MC/MCAsmBackend.cpp | 11 +++----
llvm/lib/MC/MCAssembler.cpp | 1 -
.../MCTargetDesc/AArch64AsmBackend.cpp | 8 +++--
.../AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp | 7 ++--
.../Target/ARM/MCTargetDesc/ARMAsmBackend.cpp | 3 ++
.../Target/ARM/MCTargetDesc/ARMAsmBackend.h | 3 +-
.../Target/AVR/MCTargetDesc/AVRAsmBackend.cpp | 19 ++++-------
.../Target/AVR/MCTargetDesc/AVRAsmBackend.h | 3 --
.../Target/BPF/MCTargetDesc/BPFAsmBackend.cpp | 3 +-
.../CSKY/MCTargetDesc/CSKYAsmBackend.cpp | 6 +++-
.../Target/CSKY/MCTargetDesc/CSKYAsmBackend.h | 3 +-
.../MCTargetDesc/HexagonAsmBackend.cpp | 8 +++--
.../Lanai/MCTargetDesc/LanaiAsmBackend.cpp | 9 +++--
.../MCTargetDesc/LoongArchAsmBackend.cpp | 8 +++--
.../MCTargetDesc/LoongArchAsmBackend.h | 5 ++-
.../M68k/MCTargetDesc/M68kAsmBackend.cpp | 12 ++++---
.../MSP430/MCTargetDesc/MSP430AsmBackend.cpp | 3 +-
.../Mips/MCTargetDesc/MipsAsmBackend.cpp | 5 ++-
.../Target/Mips/MCTargetDesc/MipsAsmBackend.h | 3 +-
.../PowerPC/MCTargetDesc/PPCAsmBackend.cpp | 28 ++++++++--------
.../RISCV/MCTargetDesc/RISCVAsmBackend.cpp | 3 +-
.../RISCV/MCTargetDesc/RISCVAsmBackend.h | 2 +-
.../Sparc/MCTargetDesc/SparcAsmBackend.cpp | 3 +-
.../MCTargetDesc/SystemZMCAsmBackend.cpp | 7 ++--
.../Target/VE/MCTargetDesc/VEAsmBackend.cpp | 33 +++----------------
.../MCTargetDesc/WebAssemblyAsmBackend.cpp | 9 +++--
.../Target/X86/MCTargetDesc/X86AsmBackend.cpp | 12 ++++---
.../Xtensa/MCTargetDesc/XtensaAsmBackend.cpp | 3 +-
29 files changed, 115 insertions(+), 115 deletions(-)
diff --git a/llvm/include/llvm/MC/MCAsmBackend.h b/llvm/include/llvm/MC/MCAsmBackend.h
index e49e786a10f58..54e43b89b7b00 100644
--- a/llvm/include/llvm/MC/MCAsmBackend.h
+++ b/llvm/include/llvm/MC/MCAsmBackend.h
@@ -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.
@@ -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
diff --git a/llvm/lib/MC/MCAsmBackend.cpp b/llvm/lib/MC/MCAsmBackend.cpp
index c69e42dfc9fe6..b38e027033378 100644
--- a/llvm/lib/MC/MCAsmBackend.cpp
+++ b/llvm/lib/MC/MCAsmBackend.cpp
@@ -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 {
diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index 5dd5a6b7b9fab..98225c0061284 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -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;
}
diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
index 0ff51c7161539..128adcd77de3d 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
@@ -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
@@ -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;
diff --git a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
index 78bd8de07306f..545308c734ae6 100644
--- a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
+++ b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
@@ -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
@@ -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;
diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
index f43fdae554b8b..5a8080db12629 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
@@ -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;
diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.h b/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.h
index 5efc969641b90..fce97c4291c51 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.h
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.h
@@ -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,
diff --git a/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp b/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp
index 41341387b42c2..418bf181a5451 100644
--- a/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp
+++ b/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp
@@ -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());
diff --git a/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.h b/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.h
index 283f937e1ad87..68c839ec8432a 100644
--- a/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.h
+++ b/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.h
@@ -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;
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
index 1704fecaff031..4972c51ea8d7e 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
@@ -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.
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
index 47d2728915707..bf57bd6837f61 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
@@ -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;
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.h b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.h
index 099aaa6e67cd7..c0eff853d87bd 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.h
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.h
@@ -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;
diff --git a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
index 509b02927686a..65f27327f2a15 100644
--- a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
+++ b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
@@ -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!");
@@ -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)
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp
index a0bc4f17ad884..ee60543f166bb 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp
@@ -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"
@@ -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
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
index 64192ed6632de..18b59b436657e 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
@@ -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.
@@ -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()) {
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h
index 56554c5c664eb..ae5a758dd407c 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h
@@ -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,
@@ -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;
diff --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kAsmBackend.cpp b/llvm/lib/Target/M68k/MCTargetDesc/M68kAsmBackend.cpp
index 48e130f99a60c..d2d96c812e33c 100644
--- a/llvm/lib/Target/M68k/MCTargetDesc/M68kAsmBackend.cpp
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kAsmBackend.cpp
@@ -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"
@@ -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
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430AsmBackend.cpp b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430AsmBackend.cpp
index 54fd008790f90..852fa6b338e99 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430AsmBackend.cpp
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430AsmBackend.cpp
@@ -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)
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
index 39d20f0e3c155..5b4bc9cc569e4 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
@@ -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);
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
index 8661339d3b4ab..d6bae15dea2c6 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
@@ -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
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
index a9e3ed79d6a56..2bd2092dfeda6 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
@@ -93,23 +93,11 @@ class PPCAsmBackend : public MCAsmBackend {
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
- bool addReloc(const MCFragment &F, const MCFixup &Fixup,
- const MCValue &TargetVal, uint64_t &FixedValue,
- bool IsResolved) override {
- // In PPC64 ELFv1, .quad .TOC. at tocbase in the .opd section is expected to
- // reference the null symbol.
- auto Target = TargetVal;
- if (Target.getSpecifier() == PPC::S_TOCBASE)
- Target.setAddSym(nullptr);
- return MCAsmBackend::addReloc(F, Fixup, Target, FixedValue, IsResolved);
- }
-
void applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved) override;
- bool shouldForceRelocation(const MCFixup &Fixup,
- const MCValue &Target) override {
+ bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target) {
// If there is a @ specifier, unless it is optimized out (e.g. constant @l),
// force a relocation.
if (Target.getSpecifier())
@@ -201,10 +189,20 @@ MCFixupKindInfo PPCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
: InfosBE)[Kind - FirstTargetFixupKind];
}
-void PPCAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
- const MCValue &Target,
+void PPCAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
+ const MCValue &TargetVal,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
+ // In PPC64 ELFv1, .quad .TOC. at tocbase in the .opd section is expected to
+ // reference the null symbol.
+ auto Target = TargetVal;
+ if (Target.getSpecifier() == PPC::S_TOCBASE)
+ Target.setAddSym(nullptr);
+ if (IsResolved && shouldForceRelocation(Fixup, Target))
+ IsResolved = false;
+ if (!IsResolved)
+ Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
+
MCFixupKind Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
index 57f1a6295a704..f23581efff427 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -815,10 +815,11 @@ bool RISCVAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
return false;
}
-void RISCVAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
+void RISCVAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
+ IsResolved = addReloc(F, Fixup, Target, Value, IsResolved);
MCFixupKind Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h
index 91efd44547509..d98361ea5857f 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h
@@ -51,7 +51,7 @@ class RISCVAsmBackend : public MCAsmBackend {
uint64_t &Value) override;
bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
- uint64_t &FixedValue, bool IsResolved) override;
+ uint64_t &FixedValue, bool IsResolved);
void maybeAddVendorReloc(const MCFragment &, const MCFixup &);
diff --git a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
index cc1b7e93b31cb..e6822399a5f3f 100644
--- a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
+++ b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
@@ -253,10 +253,11 @@ MCFixupKindInfo SparcAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
return Info;
}
-void SparcAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
+void SparcAsmBackend::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 (!IsResolved)
return;
Value = adjustFixupValue(Fixup.getKind(), Value);
diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
index 4ec988338c1ad..d0d0052c9be3d 100644
--- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
+++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
@@ -113,7 +113,7 @@ class SystemZMCAsmBackend : public MCAsmBackend {
// Override 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 &);
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;
@@ -158,10 +158,13 @@ bool SystemZMCAsmBackend::shouldForceRelocation(const MCFixup &,
return Target.getSpecifier();
}
-void SystemZMCAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
+void SystemZMCAsmBackend::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;
diff --git a/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp
index 41c6b49e8ed1a..b8380618db681 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp
@@ -122,8 +122,7 @@ class VEAsmBackend : public MCAsmBackend {
return Infos[Kind - FirstTargetFixupKind];
}
- bool shouldForceRelocation(const MCFixup &Fixup,
- const MCValue &Target) override {
+ bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target) {
switch ((VE::Fixups)Fixup.getKind()) {
default:
return false;
@@ -173,31 +172,6 @@ class ELFVEAsmBackend : public VEAsmBackend {
ELFVEAsmBackend(const Target &T, Triple::OSType OSType)
: VEAsmBackend(T), OSType(OSType) {}
- void applyFixup(const MCFragment &, const MCFixup &Fixup,
- const MCValue &Target, MutableArrayRef<char> Data,
- uint64_t Value, bool IsResolved) override {
- Value = adjustFixupValue(Fixup.getKind(), Value);
- if (!Value)
- return; // Doesn't change encoding.
-
- MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
-
- // Shift the value into position.
- Value <<= Info.TargetOffset;
-
- unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
- unsigned Offset = Fixup.getOffset();
- assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
- // For each byte of the fragment that the fixup touches, mask in the bits
- // from the fixup value. The Value has been "split up" into the
- // appropriate bitfields above.
- for (unsigned i = 0; i != NumBytes; ++i) {
- unsigned Idx =
- Endian == llvm::endianness::little ? i : (NumBytes - 1) - i;
- Data[Offset + Idx] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);
- }
- }
-
std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override {
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
@@ -206,9 +180,12 @@ class ELFVEAsmBackend : public VEAsmBackend {
};
} // end anonymous namespace
-void VEAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
+void VEAsmBackend::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);
Value = adjustFixupValue(Fixup.getKind(), Value);
if (!Value)
return; // Doesn't change encoding.
diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyAsmBackend.cpp b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyAsmBackend.cpp
index 7bc672c069476..4d0244064fd4c 100644
--- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyAsmBackend.cpp
+++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyAsmBackend.cpp
@@ -20,6 +20,7 @@
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCSymbol.h"
+#include "llvm/MC/MCValue.h"
#include "llvm/MC/MCWasmObjectWriter.h"
#include "llvm/Support/raw_ostream.h"
@@ -78,10 +79,14 @@ bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
return true;
}
-void WebAssemblyAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
+void WebAssemblyAsmBackend::applyFixup(const MCFragment &F,
+ const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data,
- uint64_t Value, bool) {
+ uint64_t Value, bool IsResolved) {
+ if (!IsResolved)
+ Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
+
MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
index 77e2011361162..964954e60c12e 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
@@ -169,7 +169,7 @@ class X86AsmBackend : public MCAsmBackend {
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
- bool shouldForceRelocation(const MCFixup &, const MCValue &) override;
+ bool shouldForceRelocation(const MCFixup &, const MCValue &);
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
@@ -694,9 +694,13 @@ bool X86AsmBackend::shouldForceRelocation(const MCFixup &,
return Target.getSpecifier();
}
-void X86AsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
- const MCValue &, MutableArrayRef<char> Data,
- uint64_t Value, bool IsResolved) {
+void X86AsmBackend::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;
diff --git a/llvm/lib/Target/Xtensa/MCTargetDesc/XtensaAsmBackend.cpp b/llvm/lib/Target/Xtensa/MCTargetDesc/XtensaAsmBackend.cpp
index deefb22e47a01..3343617daaba4 100644
--- a/llvm/lib/Target/Xtensa/MCTargetDesc/XtensaAsmBackend.cpp
+++ b/llvm/lib/Target/Xtensa/MCTargetDesc/XtensaAsmBackend.cpp
@@ -143,10 +143,11 @@ static unsigned getSize(unsigned Kind) {
}
}
-void XtensaAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
+void XtensaAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
+ maybeAddReloc(F, Fixup, Target, Value, IsResolved);
MCContext &Ctx = getContext();
MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
More information about the llvm-commits
mailing list