Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm] Indirect symbol replacement with GOTPCREL for aarch64 and risc… #78003

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

PiJoules
Copy link
Contributor

…v64 ELF

This is similar to #67754 but adds support for ELF aarch64 and riscv64 now that GOTPCREL-equivalent relocations have been added for those archs.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 13, 2024

@llvm/pr-subscribers-backend-risc-v

@llvm/pr-subscribers-backend-aarch64

Author: None (PiJoules)

Changes

…v64 ELF

This is similar to #67754 but adds support for ELF aarch64 and riscv64 now that GOTPCREL-equivalent relocations have been added for those archs.


Full diff: https://github.com/llvm/llvm-project/pull/78003.diff

5 Files Affected:

  • (modified) llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp (+10)
  • (modified) llvm/lib/Target/AArch64/AArch64TargetObjectFile.h (+7)
  • (modified) llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp (+12)
  • (modified) llvm/lib/Target/RISCV/RISCVTargetObjectFile.h (+6)
  • (modified) llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll (+3-1)
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
index 7ed934cfabc0d5..e5c6b6d60d068d 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
@@ -25,6 +25,16 @@ void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
   SupportDebugThreadLocalLocation = false;
 }
 
+const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
+    const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
+    int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
+  int64_t FinalOffset = Offset + MV.getConstant();
+  const MCExpr *Res =
+      MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
+  const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
+  return MCBinaryExpr::createAdd(Res, Off, getContext());
+}
+
 AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
   SupportGOTPCRelWithOffset = false;
 }
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
index 9f098230bbd78f..7cd56fc9a189d8 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
@@ -21,7 +21,14 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
 public:
   AArch64_ELFTargetObjectFile() {
     PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
+    SupportIndirectSymViaGOTPCRel = true;
   }
+
+  const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
+                                          const MCSymbol *Sym,
+                                          const MCValue &MV, int64_t Offset,
+                                          MachineModuleInfo *MMI,
+                                          MCStreamer &Streamer) const override;
 };
 
 /// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.
diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index 7c9e57e6eef3ce..1535149b919b55 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -12,6 +12,7 @@
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCSectionELF.h"
+#include "llvm/MC/MCValue.h"
 
 using namespace llvm;
 
@@ -25,6 +26,7 @@ void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
 
   PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
+  SupportIndirectSymViaGOTPCRel = true;
 
   SmallDataSection = getContext().getELFSection(
       ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
@@ -32,6 +34,16 @@ void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
                                                ELF::SHF_WRITE | ELF::SHF_ALLOC);
 }
 
+const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
+    const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
+    int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
+  int64_t FinalOffset = Offset + MV.getConstant();
+  const MCExpr *Res =
+      MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
+  const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
+  return MCBinaryExpr::createAdd(Res, Off, getContext());
+}
+
 // A address must be loaded from a small section if its size is less than the
 // small section size threshold. Data in this section could be addressed by
 // using gp_rel operator.
diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
index 890effd07320a6..0910fbd3d95041 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
@@ -42,6 +42,12 @@ class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
   void getModuleMetadata(Module &M) override;
 
   bool isInSmallSection(uint64_t Size) const;
+
+  const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
+                                          const MCSymbol *Sym,
+                                          const MCValue &MV, int64_t Offset,
+                                          MachineModuleInfo *MMI,
+                                          MCStreamer &Streamer) const override;
 };
 
 } // end namespace llvm
diff --git a/llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll b/llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll
index c58bb5b77c6c5d..b6d6633e7cad95 100644
--- a/llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll
+++ b/llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll
@@ -1,4 +1,6 @@
-; RUN: llc %s -mtriple=x86_64-unknown-fuchsia  -o - | FileCheck %s
+; RUN: llc %s -mtriple=x86_64-unknown-fuchsia -o - | FileCheck %s
+; RUN: llc %s -mtriple=aarch64-unknown-fuchsia -o - | FileCheck %s
+; RUN: llc %s -mtriple=riscv64-unknown-fuchsia -o - | FileCheck %s
 
 @vtable = dso_local unnamed_addr constant i32 trunc (i64 sub (i64 ptrtoint (ptr @rtti.proxy to i64), i64 ptrtoint (ptr @vtable to i64)) to i32), align 4
 @vtable_with_offset = dso_local unnamed_addr constant [2 x i32] [i32 0, i32 trunc (i64 sub (i64 ptrtoint (ptr @rtti.proxy to i64), i64 ptrtoint (ptr @vtable_with_offset to i64)) to i32)], align 4

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 13, 2024

@llvm/pr-subscribers-mc

Author: None (PiJoules)

Changes

…v64 ELF

This is similar to #67754 but adds support for ELF aarch64 and riscv64 now that GOTPCREL-equivalent relocations have been added for those archs.


Full diff: https://github.com/llvm/llvm-project/pull/78003.diff

5 Files Affected:

  • (modified) llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp (+10)
  • (modified) llvm/lib/Target/AArch64/AArch64TargetObjectFile.h (+7)
  • (modified) llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp (+12)
  • (modified) llvm/lib/Target/RISCV/RISCVTargetObjectFile.h (+6)
  • (modified) llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll (+3-1)
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
index 7ed934cfabc0d5..e5c6b6d60d068d 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
@@ -25,6 +25,16 @@ void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
   SupportDebugThreadLocalLocation = false;
 }
 
+const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
+    const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
+    int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
+  int64_t FinalOffset = Offset + MV.getConstant();
+  const MCExpr *Res =
+      MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
+  const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
+  return MCBinaryExpr::createAdd(Res, Off, getContext());
+}
+
 AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
   SupportGOTPCRelWithOffset = false;
 }
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
index 9f098230bbd78f..7cd56fc9a189d8 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
@@ -21,7 +21,14 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
 public:
   AArch64_ELFTargetObjectFile() {
     PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
+    SupportIndirectSymViaGOTPCRel = true;
   }
+
+  const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
+                                          const MCSymbol *Sym,
+                                          const MCValue &MV, int64_t Offset,
+                                          MachineModuleInfo *MMI,
+                                          MCStreamer &Streamer) const override;
 };
 
 /// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.
diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index 7c9e57e6eef3ce..1535149b919b55 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -12,6 +12,7 @@
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCSectionELF.h"
+#include "llvm/MC/MCValue.h"
 
 using namespace llvm;
 
@@ -25,6 +26,7 @@ void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
 
   PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
+  SupportIndirectSymViaGOTPCRel = true;
 
   SmallDataSection = getContext().getELFSection(
       ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
@@ -32,6 +34,16 @@ void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
                                                ELF::SHF_WRITE | ELF::SHF_ALLOC);
 }
 
+const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
+    const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
+    int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
+  int64_t FinalOffset = Offset + MV.getConstant();
+  const MCExpr *Res =
+      MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
+  const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
+  return MCBinaryExpr::createAdd(Res, Off, getContext());
+}
+
 // A address must be loaded from a small section if its size is less than the
 // small section size threshold. Data in this section could be addressed by
 // using gp_rel operator.
diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
index 890effd07320a6..0910fbd3d95041 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
@@ -42,6 +42,12 @@ class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
   void getModuleMetadata(Module &M) override;
 
   bool isInSmallSection(uint64_t Size) const;
+
+  const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
+                                          const MCSymbol *Sym,
+                                          const MCValue &MV, int64_t Offset,
+                                          MachineModuleInfo *MMI,
+                                          MCStreamer &Streamer) const override;
 };
 
 } // end namespace llvm
diff --git a/llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll b/llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll
index c58bb5b77c6c5d..b6d6633e7cad95 100644
--- a/llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll
+++ b/llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll
@@ -1,4 +1,6 @@
-; RUN: llc %s -mtriple=x86_64-unknown-fuchsia  -o - | FileCheck %s
+; RUN: llc %s -mtriple=x86_64-unknown-fuchsia -o - | FileCheck %s
+; RUN: llc %s -mtriple=aarch64-unknown-fuchsia -o - | FileCheck %s
+; RUN: llc %s -mtriple=riscv64-unknown-fuchsia -o - | FileCheck %s
 
 @vtable = dso_local unnamed_addr constant i32 trunc (i64 sub (i64 ptrtoint (ptr @rtti.proxy to i64), i64 ptrtoint (ptr @vtable to i64)) to i32), align 4
 @vtable_with_offset = dso_local unnamed_addr constant [2 x i32] [i32 0, i32 trunc (i64 sub (i64 ptrtoint (ptr @rtti.proxy to i64), i64 ptrtoint (ptr @vtable_with_offset to i64)) to i32)], align 4

llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll Show resolved Hide resolved
llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll Outdated Show resolved Hide resolved
…v64 ELF

This is similar to llvm#67754 but
adds support for ELF aarch64 and riscv64 now that GOTPCREL-equivalent
relocations have been added for those archs.
@PiJoules PiJoules force-pushed the support-gotpcrel-replacement-arm-riscv branch from 712b302 to 1a385a7 Compare January 16, 2024 23:19
@PiJoules PiJoules merged commit e9e3e9d into llvm:main Jan 16, 2024
3 of 4 checks passed
@PiJoules PiJoules deleted the support-gotpcrel-replacement-arm-riscv branch January 16, 2024 23:32
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
llvm#78003)

…v64 ELF

This is similar to llvm#67754 but
adds support for ELF aarch64 and riscv64 now that GOTPCREL-equivalent
relocations have been added for those archs.
smeenai added a commit that referenced this pull request Feb 16, 2024
R_ARM_GOT_PREL is equivalent to GOTPCREL on other architectures, so we
can use it for indirect symbol replacement the same way. This is the
equivalent of #67754 for x86_64
and #78003 for AArch64 and
64-bit RISC-V.
qihangkong pushed a commit to rvgpu/llvm that referenced this pull request Apr 18, 2024
R_ARM_GOT_PREL is equivalent to GOTPCREL on other architectures, so we
can use it for indirect symbol replacement the same way. This is the
equivalent of llvm/llvm-project#67754 for x86_64
and llvm/llvm-project#78003 for AArch64 and
64-bit RISC-V.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants