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

[X86] Fix RTTI proxy emission for 32-bit #78622

Merged
merged 2 commits into from
Jan 18, 2024
Merged

[X86] Fix RTTI proxy emission for 32-bit #78622

merged 2 commits into from
Jan 18, 2024

Conversation

smeenai
Copy link
Collaborator

@smeenai smeenai commented Jan 18, 2024

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.

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.
@llvmbot llvmbot added backend:X86 mc Machine (object) code labels Jan 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 18, 2024

@llvm/pr-subscribers-mc

Author: Shoaib Meenai (smeenai)

Changes

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.


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

4 Files Affected:

  • (modified) llvm/lib/Target/X86/X86TargetMachine.cpp (+3)
  • (modified) llvm/lib/Target/X86/X86TargetObjectFile.cpp (+1-1)
  • (modified) llvm/lib/Target/X86/X86TargetObjectFile.h (+11-3)
  • (added) llvm/test/MC/ELF/rtti-proxy-i686.ll (+20)
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index b92bffbe6239bb3..9e4cf1ea99682a0 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 53c692060f08cf1..e2ddf43e398cb00 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 ed9390d1fad1a26..1b8d7f2727f520c 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 000000000000000..afa62ebea4d77d1
--- /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
+
+@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
+@rtti = external global i8, align 8
+@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

@llvmbot
Copy link
Member

llvmbot commented Jan 18, 2024

@llvm/pr-subscribers-backend-x86

Author: Shoaib Meenai (smeenai)

Changes

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.


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

4 Files Affected:

  • (modified) llvm/lib/Target/X86/X86TargetMachine.cpp (+3)
  • (modified) llvm/lib/Target/X86/X86TargetObjectFile.cpp (+1-1)
  • (modified) llvm/lib/Target/X86/X86TargetObjectFile.h (+11-3)
  • (added) llvm/test/MC/ELF/rtti-proxy-i686.ll (+20)
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
+
+@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
+@rtti = external global i8, align 8
+@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

Copy link

github-actions bot commented Jan 18, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@PiJoules PiJoules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@smeenai smeenai merged commit 741b836 into llvm:main Jan 18, 2024
3 checks passed
@smeenai smeenai deleted the fix-x86 branch January 18, 2024 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants