[llvm] [X86] Fix RTTI proxy emission for 32-bit (PR #78622)

Shoaib Meenai via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 18 13:05:42 PST 2024


https://github.com/smeenai updated https://github.com/llvm/llvm-project/pull/78622

>From 45b4dbcb0adb39ff2b976f08e6a2d31492b0fd0e Mon Sep 17 00:00:00 2001
From: Shoaib Meenai <smeenai at fb.com>
Date: Thu, 18 Jan 2024 12:53:29 -0800
Subject: [PATCH 1/2] [X86] Fix RTTI proxy emission for 32-bit

32-bit x86 doesn't have an appropriate relocation type we can use to
elide the RTTI proxies, so we need to emit them. This would previously
cause crashes when using the relative vtable ABI for 32-bit x86.
---
 llvm/lib/Target/X86/X86TargetMachine.cpp    |  3 +++
 llvm/lib/Target/X86/X86TargetObjectFile.cpp |  2 +-
 llvm/lib/Target/X86/X86TargetObjectFile.h   | 14 +++++++++++---
 llvm/test/MC/ELF/rtti-proxy-i686.ll         | 20 ++++++++++++++++++++
 4 files changed, 35 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/MC/ELF/rtti-proxy-i686.ll

diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index b92bffbe6239bb..9e4cf1ea99682a 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -113,6 +113,9 @@ static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
 
   if (TT.isOSBinFormatCOFF())
     return std::make_unique<TargetLoweringObjectFileCOFF>();
+
+  if (TT.getArch() == Triple::x86_64)
+    return std::make_unique<X86_64ELFTargetObjectFile>();
   return std::make_unique<X86ELFTargetObjectFile>();
 }
 
diff --git a/llvm/lib/Target/X86/X86TargetObjectFile.cpp b/llvm/lib/Target/X86/X86TargetObjectFile.cpp
index 53c692060f08cf..e2ddf43e398cb0 100644
--- a/llvm/lib/Target/X86/X86TargetObjectFile.cpp
+++ b/llvm/lib/Target/X86/X86TargetObjectFile.cpp
@@ -57,7 +57,7 @@ const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
   return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
 }
 
-const MCExpr *X86ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
+const MCExpr *X86_64ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
     const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
     int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
   int64_t FinalOffset = Offset + MV.getConstant();
diff --git a/llvm/lib/Target/X86/X86TargetObjectFile.h b/llvm/lib/Target/X86/X86TargetObjectFile.h
index ed9390d1fad1a2..1b8d7f2727f520 100644
--- a/llvm/lib/Target/X86/X86TargetObjectFile.h
+++ b/llvm/lib/Target/X86/X86TargetObjectFile.h
@@ -36,16 +36,24 @@ namespace llvm {
                                             MCStreamer &Streamer) const override;
   };
 
-  /// This implementation is used for X86 ELF targets that don't
-  /// have a further specialization.
+  /// This implementation is used for X86 ELF targets that don't have a further
+  /// specialization (and as a base class for X86_64, which does).
   class X86ELFTargetObjectFile : public TargetLoweringObjectFileELF {
   public:
     X86ELFTargetObjectFile() {
       PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
-      SupportIndirectSymViaGOTPCRel = true;
     }
     /// Describe a TLS variable address within debug info.
     const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const override;
+  };
+
+  /// This implementation is used for X86_64 ELF targets, and defers to
+  /// X86ELFTargetObjectFile for commonalities with 32-bit targets.
+  class X86_64ELFTargetObjectFile : public X86ELFTargetObjectFile {
+  public:
+    X86_64ELFTargetObjectFile() {
+      SupportIndirectSymViaGOTPCRel = true;
+    }
 
     const MCExpr *
     getIndirectSymViaGOTPCRel(const GlobalValue *GV, const MCSymbol *Sym,
diff --git a/llvm/test/MC/ELF/rtti-proxy-i686.ll b/llvm/test/MC/ELF/rtti-proxy-i686.ll
new file mode 100644
index 00000000000000..afa62ebea4d77d
--- /dev/null
+++ b/llvm/test/MC/ELF/rtti-proxy-i686.ll
@@ -0,0 +1,20 @@
+; REQUIRES: x86-registered-target
+
+;; Validate that we produce RTTI proxies for 32-bit x86.
+; RUN: llc %s -mtriple=i686-elf -o - | FileCheck %s
+
+;; Validate that we produce a valid object file.
+; RUN: llc %s -mtriple=i686-elf --filetype=obj -o %t.o
+; RUN: llvm-readobj --relocs %t.o | FileCheck --check-prefix=RELOCS %s
+
+ at 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
+ at rtti = external global i8, align 8
+ at rtti.proxy = linkonce_odr hidden unnamed_addr constant ptr @rtti
+
+; CHECK-LABEL: vtable:
+; CHECK-NEXT:    .long   rtti.proxy-vtable
+
+; CHECK-LABEL: rtti.proxy:
+; CHECK-NEXT:    .long   rtti
+
+; RELOCS: R_386_32 rtti

>From 3e80088d1d3b255725c44b6c22c97ecf4150ea0e Mon Sep 17 00:00:00 2001
From: Shoaib Meenai <smeenai at fb.com>
Date: Thu, 18 Jan 2024 13:05:31 -0800
Subject: [PATCH 2/2] Formatting

---
 llvm/lib/Target/X86/X86TargetObjectFile.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/Target/X86/X86TargetObjectFile.h b/llvm/lib/Target/X86/X86TargetObjectFile.h
index 1b8d7f2727f520..a0e1762a2cb50c 100644
--- a/llvm/lib/Target/X86/X86TargetObjectFile.h
+++ b/llvm/lib/Target/X86/X86TargetObjectFile.h
@@ -51,9 +51,7 @@ namespace llvm {
   /// X86ELFTargetObjectFile for commonalities with 32-bit targets.
   class X86_64ELFTargetObjectFile : public X86ELFTargetObjectFile {
   public:
-    X86_64ELFTargetObjectFile() {
-      SupportIndirectSymViaGOTPCRel = true;
-    }
+    X86_64ELFTargetObjectFile() { SupportIndirectSymViaGOTPCRel = true; }
 
     const MCExpr *
     getIndirectSymViaGOTPCRel(const GlobalValue *GV, const MCSymbol *Sym,



More information about the llvm-commits mailing list