Skip to content

Commit e9e3e9d

Browse files
authored
[llvm] Indirect symbol replacement with GOTPCREL for aarch64 and risc… (#78003)
…v64 ELF This is similar to #67754 but adds support for ELF aarch64 and riscv64 now that GOTPCREL-equivalent relocations have been added for those archs.
1 parent 8947469 commit e9e3e9d

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
2525
SupportDebugThreadLocalLocation = false;
2626
}
2727

28+
const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
29+
const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
30+
int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
31+
int64_t FinalOffset = Offset + MV.getConstant();
32+
const MCExpr *Res =
33+
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
34+
const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
35+
return MCBinaryExpr::createAdd(Res, Off, getContext());
36+
}
37+
2838
AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
2939
SupportGOTPCRelWithOffset = false;
3040
}

llvm/lib/Target/AArch64/AArch64TargetObjectFile.h

+7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
2121
public:
2222
AArch64_ELFTargetObjectFile() {
2323
PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
24+
SupportIndirectSymViaGOTPCRel = true;
2425
}
26+
27+
const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
28+
const MCSymbol *Sym,
29+
const MCValue &MV, int64_t Offset,
30+
MachineModuleInfo *MMI,
31+
MCStreamer &Streamer) const override;
2532
};
2633

2734
/// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.

llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/BinaryFormat/ELF.h"
1313
#include "llvm/MC/MCContext.h"
1414
#include "llvm/MC/MCSectionELF.h"
15+
#include "llvm/MC/MCValue.h"
1516

1617
using namespace llvm;
1718

@@ -25,13 +26,24 @@ void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
2526
TargetLoweringObjectFileELF::Initialize(Ctx, TM);
2627

2728
PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
29+
SupportIndirectSymViaGOTPCRel = true;
2830

2931
SmallDataSection = getContext().getELFSection(
3032
".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
3133
SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
3234
ELF::SHF_WRITE | ELF::SHF_ALLOC);
3335
}
3436

37+
const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
38+
const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
39+
int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
40+
int64_t FinalOffset = Offset + MV.getConstant();
41+
const MCExpr *Res =
42+
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
43+
const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
44+
return MCBinaryExpr::createAdd(Res, Off, getContext());
45+
}
46+
3547
// A address must be loaded from a small section if its size is less than the
3648
// small section size threshold. Data in this section could be addressed by
3749
// using gp_rel operator.

llvm/lib/Target/RISCV/RISCVTargetObjectFile.h

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
4242
void getModuleMetadata(Module &M) override;
4343

4444
bool isInSmallSection(uint64_t Size) const;
45+
46+
const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
47+
const MCSymbol *Sym,
48+
const MCValue &MV, int64_t Offset,
49+
MachineModuleInfo *MMI,
50+
MCStreamer &Streamer) const override;
4551
};
4652

4753
} // end namespace llvm

llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
; RUN: llc %s -mtriple=x86_64-unknown-fuchsia -o - | FileCheck %s
1+
; REQUIRES: x86-registered-target && aarch64-registered-target && riscv-registered-target
2+
; RUN: llc %s -mtriple=x86_64 -o - | FileCheck %s
3+
; RUN: llc %s -mtriple=aarch64 -o - | FileCheck %s
4+
; RUN: llc %s -mtriple=riscv64 -o - | FileCheck %s
25

36
@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
47
@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

0 commit comments

Comments
 (0)