Skip to content

Commit

Permalink
[X86] Fix RTTI proxy emission for 32-bit (llvm#78622)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
smeenai authored Jan 18, 2024
1 parent 160a750 commit 741b836
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions llvm/lib/Target/X86/X86TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86TargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 9 additions & 3 deletions llvm/lib/Target/X86/X86TargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ 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,
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/MC/ELF/rtti-proxy-i686.ll
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 741b836

Please sign in to comment.