[llvm] 5ae8f25 - [RISCV] Move fixELFSymbolsInTLSFixups to getRelocType

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 15 11:00:15 PDT 2025


Author: Fangrui Song
Date: 2025-03-15T11:00:10-07:00
New Revision: 5ae8f25dcc89dcd0238a6e38e63bffe67eeaabb8

URL: https://github.com/llvm/llvm-project/commit/5ae8f25dcc89dcd0238a6e38e63bffe67eeaabb8
DIFF: https://github.com/llvm/llvm-project/commit/5ae8f25dcc89dcd0238a6e38e63bffe67eeaabb8.diff

LOG: [RISCV] Move fixELFSymbolsInTLSFixups to getRelocType

fixELFSymbolsInTLSFixups walks the expression tree, which is complex and
unnecessary. As the expression must be relocatable, we can move the code
to getRelocType and just set SymA. The behavior is similar to GNU
assembler.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
index 2343c5fb2535a..db0047191ea76 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
@@ -55,6 +55,17 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
   unsigned Kind = Fixup.getTargetKind();
   if (Kind >= FirstLiteralRelocationKind)
     return Kind - FirstLiteralRelocationKind;
+
+  switch (Target.getRefKind()) {
+  case RISCVMCExpr::VK_RISCV_TPREL_HI:
+  case RISCVMCExpr::VK_RISCV_TLS_GOT_HI:
+  case RISCVMCExpr::VK_RISCV_TLS_GD_HI:
+  case RISCVMCExpr::VK_RISCV_TLSDESC_HI:
+    if (auto *S = Target.getSymA())
+      cast<MCSymbolELF>(S->getSymbol()).setType(ELF::STT_TLS);
+    break;
+  }
+
   if (IsPCRel) {
     switch (Kind) {
     default:

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
index 4f56e3cbfe1b9..99a31054a4e81 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
@@ -169,49 +169,6 @@ StringRef RISCVMCExpr::getVariantKindName(VariantKind Kind) {
   llvm_unreachable("Invalid ELF symbol kind");
 }
 
-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::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;
-  }
-
-  case MCExpr::Unary:
-    fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
-    break;
-  }
-}
-
-void RISCVMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
-  switch (getKind()) {
-  default:
-    return;
-  case VK_RISCV_TPREL_HI:
-  case VK_RISCV_TLS_GOT_HI:
-  case VK_RISCV_TLS_GD_HI:
-  case VK_RISCV_TLSDESC_HI:
-    break;
-  }
-
-  fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
-}
-
 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
   MCValue Value;
   if (Kind != VK_RISCV_LO && Kind != VK_RISCV_HI)

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h
index d1445480f38fb..6a87e00b6b40d 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h
@@ -76,8 +76,6 @@ class RISCVMCExpr : public MCTargetExpr {
     return getSubExpr()->findAssociatedFragment();
   }
 
-  void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override;
-
   bool evaluateAsConstant(int64_t &Res) const;
 
   static bool classof(const MCExpr *E) {


        


More information about the llvm-commits mailing list