Skip to content

Commit 1511335

Browse files
committed
[llvm] Support indirect symbol replacement with GOTPCREL for x86_64 ELF
There's an existing check in LLVM that can replace an offset to a DSO-local symbol that is just a pointer to another symbol with a GOTPCREL reloc. This is exactly what the RTTI proxy in the relative vtables ABI is. This feature is supported for different macho platforms, but not for ELF. This extends that support.
1 parent a409002 commit 1511335

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

llvm/lib/Target/X86/X86TargetObjectFile.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,13 @@ const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
5656
const MCSymbol *Sym) const {
5757
return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
5858
}
59+
60+
const MCExpr *X86ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
61+
const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
62+
int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
63+
int64_t FinalOffset = Offset + MV.getConstant();
64+
const MCExpr *Res =
65+
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
66+
const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
67+
return MCBinaryExpr::createAdd(Res, Off, getContext());
68+
}

llvm/lib/Target/X86/X86TargetObjectFile.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ namespace llvm {
4242
public:
4343
X86ELFTargetObjectFile() {
4444
PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
45+
SupportIndirectSymViaGOTPCRel = true;
4546
}
4647
/// Describe a TLS variable address within debug info.
4748
const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const override;
49+
50+
const MCExpr *
51+
getIndirectSymViaGOTPCRel(const GlobalValue *GV, const MCSymbol *Sym,
52+
const MCValue &MV, int64_t Offset,
53+
MachineModuleInfo *MMI,
54+
MCStreamer &Streamer) const override;
4855
};
4956

5057
} // end namespace llvm
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: llc %s -mtriple=x86_64-unknown-fuchsia -o - | FileCheck %s
2+
3+
@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
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
5+
@vtable_with_negative_offset = dso_local unnamed_addr constant [2 x i32] [
6+
i32 trunc (
7+
i64 sub (
8+
i64 ptrtoint (ptr @rtti.proxy to i64),
9+
i64 ptrtoint (ptr getelementptr inbounds ([2 x i32], ptr @vtable_with_negative_offset, i32 0, i32 1) to i64)
10+
)
11+
to i32),
12+
i32 0
13+
], align 4
14+
@rtti = external global i8, align 8
15+
@rtti.proxy = linkonce_odr hidden unnamed_addr constant ptr @rtti
16+
17+
; CHECK-NOT: rtti.proxy
18+
; CHECK-LABEL: vtable:
19+
; CHECK-NEXT: .{{word|long}} rtti@GOTPCREL+0{{$}}
20+
21+
; CHECK-LABEL: vtable_with_offset:
22+
; CHECK-NEXT: .{{word|long}} 0
23+
; CHECK-NEXT: .{{word|long}} rtti@GOTPCREL+4{{$}}
24+
25+
; CHECK-LABEL: vtable_with_negative_offset:
26+
; CHECK-NEXT: .{{word|long}} rtti@GOTPCREL-4{{$}}
27+
; CHECK-NEXT: .{{word|long}} 0

0 commit comments

Comments
 (0)