[llvm] 8ec96cc - [LoongArch] Move STT_TLS setting from fixELFSymbolsInTLSFixups to getRelocType

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 22 19:55:20 PDT 2025


Author: Fangrui Song
Date: 2025-03-22T19:55:15-07:00
New Revision: 8ec96cc8866769b6bd17648a922ad1c0b9a39e69

URL: https://github.com/llvm/llvm-project/commit/8ec96cc8866769b6bd17648a922ad1c0b9a39e69
DIFF: https://github.com/llvm/llvm-project/commit/8ec96cc8866769b6bd17648a922ad1c0b9a39e69.diff

LOG: [LoongArch] Move STT_TLS setting from fixELFSymbolsInTLSFixups to getRelocType

The legacy generic code uses `ELFObjectWriter::fixSymbolsInTLSFixups` to
set `STT_TLS` (and use an unnecessary expression walk). The better way
is to do this in `getRelocType`, which I have done for AArch64, PowerPC,
RISC-V, and X86.

Added: 
    

Modified: 
    llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
    llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp
    llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
index 1dec816f34733..7af832647f657 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
@@ -7,12 +7,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "MCTargetDesc/LoongArchFixupKinds.h"
+#include "MCTargetDesc/LoongArchMCExpr.h"
 #include "MCTargetDesc/LoongArchMCTargetDesc.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCELFObjectWriter.h"
 #include "llvm/MC/MCFixup.h"
 #include "llvm/MC/MCObjectWriter.h"
+#include "llvm/MC/MCValue.h"
 #include "llvm/Support/ErrorHandling.h"
 
 using namespace llvm;
@@ -48,6 +50,27 @@ unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,
                                                 const MCValue &Target,
                                                 const MCFixup &Fixup,
                                                 bool IsPCRel) const {
+  switch (Target.getRefKind()) {
+  case LoongArchMCExpr::VK_TLS_LE_HI20:
+  case LoongArchMCExpr::VK_TLS_IE_PC_HI20:
+  case LoongArchMCExpr::VK_TLS_IE_HI20:
+  case LoongArchMCExpr::VK_TLS_LD_PC_HI20:
+  case LoongArchMCExpr::VK_TLS_LD_HI20:
+  case LoongArchMCExpr::VK_TLS_GD_PC_HI20:
+  case LoongArchMCExpr::VK_TLS_GD_HI20:
+  case LoongArchMCExpr::VK_TLS_DESC_PC_HI20:
+  case LoongArchMCExpr::VK_TLS_DESC_HI20:
+  case LoongArchMCExpr::VK_TLS_LE_HI20_R:
+  case LoongArchMCExpr::VK_TLS_LD_PCREL20_S2:
+  case LoongArchMCExpr::VK_TLS_GD_PCREL20_S2:
+  case LoongArchMCExpr::VK_TLS_DESC_PCREL20_S2:
+    if (auto *S = Target.getSymA())
+      cast<MCSymbolELF>(S->getSymbol()).setType(ELF::STT_TLS);
+    break;
+  default:
+    break;
+  }
+
   // Determine the type of the relocation
   unsigned Kind = Fixup.getTargetKind();
 

diff  --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp
index 6ad516097dc61..2504ef8520a5c 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp
@@ -232,51 +232,3 @@ LoongArchMCExpr::Specifier LoongArchMCExpr::parseSpecifier(StringRef name) {
       .Case("desc_pcrel_20", VK_TLS_DESC_PCREL20_S2)
       .Default(VK_None);
 }
-
-static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
-  switch (Expr->getKind()) {
-  case MCExpr::Target:
-    llvm_unreachable("Can't handle nested target expression");
-    break;
-  case MCExpr::Constant:
-    break;
-  case MCExpr::Unary:
-    fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
-    break;
-  case MCExpr::Binary: {
-    const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
-    fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
-    fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
-    break;
-  }
-  case MCExpr::SymbolRef: {
-    // We're known to be under a TLS fixup, so any symbol should be
-    // modified. There should be only one.
-    const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
-    cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
-    break;
-  }
-  }
-}
-
-void LoongArchMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
-  switch (specifier) {
-  default:
-    return;
-  case VK_TLS_LE_HI20:
-  case VK_TLS_IE_PC_HI20:
-  case VK_TLS_IE_HI20:
-  case VK_TLS_LD_PC_HI20:
-  case VK_TLS_LD_HI20:
-  case VK_TLS_GD_PC_HI20:
-  case VK_TLS_GD_HI20:
-  case VK_TLS_DESC_PC_HI20:
-  case VK_TLS_DESC_HI20:
-  case VK_TLS_LE_HI20_R:
-  case VK_TLS_LD_PCREL20_S2:
-  case VK_TLS_GD_PCREL20_S2:
-  case VK_TLS_DESC_PCREL20_S2:
-    break;
-  }
-  fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
-}

diff  --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h
index 39b912d6728f3..4bc0b384bfe23 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h
@@ -105,8 +105,6 @@ class LoongArchMCExpr : public MCTargetExpr {
     return getSubExpr()->findAssociatedFragment();
   }
 
-  void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override;
-
   static bool classof(const MCExpr *E) {
     return E->getKind() == MCExpr::Target;
   }


        


More information about the llvm-commits mailing list