[llvm] 39b1fec - [LoongArch] Implement MCTargetExpr::fixELFSymbolsInTLSFixups hook
via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 12 01:33:23 PST 2022
Author: wanglei
Date: 2022-11-12T17:23:51+08:00
New Revision: 39b1fec15d23d9b9d5cc4e5048918043576f914b
URL: https://github.com/llvm/llvm-project/commit/39b1fec15d23d9b9d5cc4e5048918043576f914b
DIFF: https://github.com/llvm/llvm-project/commit/39b1fec15d23d9b9d5cc4e5048918043576f914b.diff
LOG: [LoongArch] Implement MCTargetExpr::fixELFSymbolsInTLSFixups hook
Reviewed By: SixWeining, MaskRay
Differential Revision: https://reviews.llvm.org/D137628
Added:
llvm/test/MC/LoongArch/Misc/tls-symbols.s
Modified:
llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp
llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h
Removed:
################################################################################
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp
index 6760d3e7cbb5b..993111552a314 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.cpp
@@ -16,7 +16,9 @@
#include "LoongArchFixupKinds.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCStreamer.h"
+#include "llvm/MC/MCSymbolELF.h"
#include "llvm/MC/MCValue.h"
+#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm;
@@ -179,3 +181,45 @@ LoongArchMCExpr::getVariantKindForName(StringRef name) {
.Case("gd_hi20", VK_LoongArch_TLS_GD_HI20)
.Default(VK_LoongArch_Invalid);
}
+
+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 (getKind()) {
+ default:
+ return;
+ case VK_LoongArch_TLS_LE_HI20:
+ case VK_LoongArch_TLS_IE_PC_HI20:
+ case VK_LoongArch_TLS_IE_HI20:
+ case VK_LoongArch_TLS_LD_PC_HI20:
+ case VK_LoongArch_TLS_LD_HI20:
+ case VK_LoongArch_TLS_GD_PC_HI20:
+ case VK_LoongArch_TLS_GD_HI20:
+ break;
+ }
+ fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
+}
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h
index 4706c2e65fea3..0945cf82db865 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCExpr.h
@@ -86,7 +86,7 @@ class LoongArchMCExpr : public MCTargetExpr {
return getSubExpr()->findAssociatedFragment();
}
- void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override {}
+ void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override;
static bool classof(const MCExpr *E) {
return E->getKind() == MCExpr::Target;
diff --git a/llvm/test/MC/LoongArch/Misc/tls-symbols.s b/llvm/test/MC/LoongArch/Misc/tls-symbols.s
new file mode 100644
index 0000000000000..2f91cbe004d27
--- /dev/null
+++ b/llvm/test/MC/LoongArch/Misc/tls-symbols.s
@@ -0,0 +1,79 @@
+# RUN: llvm-mc --triple=loongarch64 --filetype=obj %s -o %t
+# RUN: llvm-readobj -s %t | FileCheck %s
+
+lu12i.w $a1, %gd_hi20(gd_abs)
+# CHECK: Symbol {
+# CHECK: Name: gd_abs
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: TLS
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
+
+pcalau12i $a1, %gd_pc_hi20(gd_pcrel)
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: gd_pcrel
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: TLS
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
+
+lu12i.w $a1, %ld_hi20(ld_abs)
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: ld_abs
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: TLS
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
+
+pcalau12i $a1, %ld_pc_hi20(ld_pcrel)
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: ld_pcrel
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: TLS
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
+
+lu12i.w $a1, %ie_hi20(ie_abs)
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: ie_abs
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: TLS
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
+
+pcalau12i $a1, %ie_pc_hi20(ie_pcrel)
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: ie_pcrel
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: TLS
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
+
+lu12i.w $a1, %le_hi20(le)
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: le
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: TLS
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
More information about the llvm-commits
mailing list