Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 741b836

Browse files
authoredJan 18, 2024
[X86] Fix RTTI proxy emission for 32-bit (#78622)
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.
1 parent 160a750 commit 741b836

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed
 

‎llvm/lib/Target/X86/X86TargetMachine.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
113113

114114
if (TT.isOSBinFormatCOFF())
115115
return std::make_unique<TargetLoweringObjectFileCOFF>();
116+
117+
if (TT.getArch() == Triple::x86_64)
118+
return std::make_unique<X86_64ELFTargetObjectFile>();
116119
return std::make_unique<X86ELFTargetObjectFile>();
117120
}
118121

‎llvm/lib/Target/X86/X86TargetObjectFile.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
5757
return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
5858
}
5959

60-
const MCExpr *X86ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
60+
const MCExpr *X86_64ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
6161
const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
6262
int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
6363
int64_t FinalOffset = Offset + MV.getConstant();

‎llvm/lib/Target/X86/X86TargetObjectFile.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,22 @@ namespace llvm {
3636
MCStreamer &Streamer) const override;
3737
};
3838

39-
/// This implementation is used for X86 ELF targets that don't
40-
/// have a further specialization.
39+
/// This implementation is used for X86 ELF targets that don't have a further
40+
/// specialization (and as a base class for X86_64, which does).
4141
class X86ELFTargetObjectFile : public TargetLoweringObjectFileELF {
4242
public:
4343
X86ELFTargetObjectFile() {
4444
PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
45-
SupportIndirectSymViaGOTPCRel = true;
4645
}
4746
/// Describe a TLS variable address within debug info.
4847
const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const override;
48+
};
49+
50+
/// This implementation is used for X86_64 ELF targets, and defers to
51+
/// X86ELFTargetObjectFile for commonalities with 32-bit targets.
52+
class X86_64ELFTargetObjectFile : public X86ELFTargetObjectFile {
53+
public:
54+
X86_64ELFTargetObjectFile() { SupportIndirectSymViaGOTPCRel = true; }
4955

5056
const MCExpr *
5157
getIndirectSymViaGOTPCRel(const GlobalValue *GV, const MCSymbol *Sym,

‎llvm/test/MC/ELF/rtti-proxy-i686.ll

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; REQUIRES: x86-registered-target
2+
3+
;; Validate that we produce RTTI proxies for 32-bit x86.
4+
; RUN: llc %s -mtriple=i686-elf -o - | FileCheck %s
5+
6+
;; Validate that we produce a valid object file.
7+
; RUN: llc %s -mtriple=i686-elf --filetype=obj -o %t.o
8+
; RUN: llvm-readobj --relocs %t.o | FileCheck --check-prefix=RELOCS %s
9+
10+
@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
11+
@rtti = external global i8, align 8
12+
@rtti.proxy = linkonce_odr hidden unnamed_addr constant ptr @rtti
13+
14+
; CHECK-LABEL: vtable:
15+
; CHECK-NEXT: .long rtti.proxy-vtable
16+
17+
; CHECK-LABEL: rtti.proxy:
18+
; CHECK-NEXT: .long rtti
19+
20+
; RELOCS: R_386_32 rtti

0 commit comments

Comments
 (0)
Please sign in to comment.