[llvm] 68472a3 - [MC] Restore MCAsmBackend::shouldForceRelocation to false

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu May 22 09:49:32 PDT 2025


Author: Fangrui Song
Date: 2025-05-22T09:49:27-07:00
New Revision: 68472a39a0fbf38f5da7bb4ebe43e2ca87ff8308

URL: https://github.com/llvm/llvm-project/commit/68472a39a0fbf38f5da7bb4ebe43e2ca87ff8308
DIFF: https://github.com/llvm/llvm-project/commit/68472a39a0fbf38f5da7bb4ebe43e2ca87ff8308.diff

LOG: [MC] Restore MCAsmBackend::shouldForceRelocation to false

For IsPCRel fixups, we had the `A->getKind() != MCSymbolRefExpr::VK_None`
condition to force relocations. The condition has then been changed to
`Target.getSpecifier()` (086af836889436baffc71c743c7c8259bad8ed60).

38c3ad36be1facbe6db2dede7e93c0f12fb4e1dc updated
shouldForceRelocation to test `Target.getSpecifier`.
It is not a good fit as many targets with %lo/%hi style specifiers
(SPARC, MIPS, PowerPC, RISC-V) do not force relocations for these
specifiers.

Revert the Target.getSpecifier implementation
(38c3ad36be1facbe6db2dede7e93c0f12fb4e1dc) and update
targets that need it (SystemZAsmBackend/X86AsmBackend) instead.
Targets need customization might define addReloc instead.

Note: The X86AsmBackend implementation is too conservative.
GNU Assembler doesn't emit a relocation for `call local at plt; local`
a3d39316764726ed9fd939550c7781199b1eb77e added missing coverage
for GOT/PLT related relocations.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCAsmBackend.h
    llvm/lib/MC/MCAsmBackend.cpp
    llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
    llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
    llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCAsmBackend.h b/llvm/include/llvm/MC/MCAsmBackend.h
index 27fdd23c88cf3..9eabacf2f7f10 100644
--- a/llvm/include/llvm/MC/MCAsmBackend.h
+++ b/llvm/include/llvm/MC/MCAsmBackend.h
@@ -88,10 +88,11 @@ class MCAsmBackend {
   /// Get information on a fixup kind.
   virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const;
 
-  // Hook to check if a relocation is needed. The default implementation tests
-  // whether the MCValue has a relocation specifier.
+  // Hook used by the default `addReloc` to check if a relocation is needed.
   virtual bool shouldForceRelocation(const MCAssembler &, const MCFixup &,
-                                     const MCValue &, const MCSubtargetInfo *);
+                                     const MCValue &, const MCSubtargetInfo *) {
+    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

diff  --git a/llvm/lib/MC/MCAsmBackend.cpp b/llvm/lib/MC/MCAsmBackend.cpp
index 0c43b2e473559..2dae9c0266722 100644
--- a/llvm/lib/MC/MCAsmBackend.cpp
+++ b/llvm/lib/MC/MCAsmBackend.cpp
@@ -109,12 +109,6 @@ MCFixupKindInfo MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
   return Builtins[Kind - FK_NONE];
 }
 
-bool MCAsmBackend::shouldForceRelocation(const MCAssembler &, const MCFixup &,
-                                         const MCValue &Target,
-                                         const MCSubtargetInfo *) {
-  return Target.getSpecifier();
-}
-
 bool MCAsmBackend::fixupNeedsRelaxationAdvanced(const MCAssembler &,
                                                 const MCFixup &Fixup,
                                                 const MCValue &, uint64_t Value,

diff  --git a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
index 3ea8bede91ea4..9ef335a5af4a3 100644
--- a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
+++ b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
@@ -200,12 +200,6 @@ namespace {
       return Info;
     }
 
-    bool shouldForceRelocation(const MCAssembler &, const MCFixup &,
-                               const MCValue &,
-                               const MCSubtargetInfo *) override {
-      return false;
-    }
-
     void relaxInstruction(MCInst &Inst,
                           const MCSubtargetInfo &STI) const override {
       // FIXME.

diff  --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
index 58bb7baea1d47..bbb405ff77068 100644
--- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
+++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
@@ -17,6 +17,7 @@
 #include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCObjectWriter.h"
 #include "llvm/MC/MCSubtargetInfo.h"
+#include "llvm/MC/MCValue.h"
 
 using namespace llvm;
 
@@ -112,6 +113,8 @@ class SystemZMCAsmBackend : public MCAsmBackend {
   // Override MCAsmBackend
   std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
   MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
+  bool shouldForceRelocation(const MCAssembler &, const MCFixup &,
+                             const MCValue &, const MCSubtargetInfo *) override;
   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
                   const MCValue &Target, MutableArrayRef<char> Data,
                   uint64_t Value, bool IsResolved,
@@ -152,6 +155,13 @@ MCFixupKindInfo SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
   return SystemZ::MCFixupKindInfos[Kind - FirstTargetFixupKind];
 }
 
+bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler &,
+                                                const MCFixup &,
+                                                const MCValue &Target,
+                                                const MCSubtargetInfo *) {
+  return Target.getSpecifier();
+}
+
 void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm,
                                      const MCFixup &Fixup,
                                      const MCValue &Target,

diff  --git a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
index 3cee53a886ebf..81bdd8cb24b88 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
@@ -169,6 +169,9 @@ class X86AsmBackend : public MCAsmBackend {
 
   MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
 
+  bool shouldForceRelocation(const MCAssembler &, const MCFixup &,
+                             const MCValue &, const MCSubtargetInfo *) override;
+
   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
                   const MCValue &Target, MutableArrayRef<char> Data,
                   uint64_t Value, bool IsResolved,
@@ -687,6 +690,14 @@ static unsigned getFixupKindSize(unsigned Kind) {
   }
 }
 
+// Force relocation when there is a specifier. This might be too conservative -
+// GAS doesn't emit a relocation for call local at plt; local:.
+bool X86AsmBackend::shouldForceRelocation(const MCAssembler &, const MCFixup &,
+                                          const MCValue &Target,
+                                          const MCSubtargetInfo *) {
+  return Target.getSpecifier();
+}
+
 void X86AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
                                const MCValue &, MutableArrayRef<char> Data,
                                uint64_t Value, bool IsResolved,


        


More information about the llvm-commits mailing list