Skip to content

Commit

Permalink
[llvm] Indirect symbol replacement with GOTPCREL for aarch64 and risc…
Browse files Browse the repository at this point in the history
…v64 ELF

This is similar to llvm#67754 but
adds support for ELF aarch64 and riscv64 now that GOTPCREL-equivalent
relocations have been added for those archs.
  • Loading branch information
PiJoules committed Jan 13, 2024
1 parent 85b7d54 commit 712b302
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
10 changes: 10 additions & 0 deletions llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
SupportDebugThreadLocalLocation = false;
}

const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
int64_t FinalOffset = Offset + MV.getConstant();
const MCExpr *Res =
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
return MCBinaryExpr::createAdd(Res, Off, getContext());
}

AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
SupportGOTPCRelWithOffset = false;
}
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
public:
AArch64_ELFTargetObjectFile() {
PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
SupportIndirectSymViaGOTPCRel = true;
}

const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
const MCSymbol *Sym,
const MCValue &MV, int64_t Offset,
MachineModuleInfo *MMI,
MCStreamer &Streamer) const override;
};

/// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.
Expand Down
12 changes: 12 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCValue.h"

using namespace llvm;

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

PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
SupportIndirectSymViaGOTPCRel = true;

SmallDataSection = getContext().getELFSection(
".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
ELF::SHF_WRITE | ELF::SHF_ALLOC);
}

const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
int64_t FinalOffset = Offset + MV.getConstant();
const MCExpr *Res =
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
return MCBinaryExpr::createAdd(Res, Off, getContext());
}

// A address must be loaded from a small section if its size is less than the
// small section size threshold. Data in this section could be addressed by
// using gp_rel operator.
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
void getModuleMetadata(Module &M) override;

bool isInSmallSection(uint64_t Size) const;

const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
const MCSymbol *Sym,
const MCValue &MV, int64_t Offset,
MachineModuleInfo *MMI,
MCStreamer &Streamer) const override;
};

} // end namespace llvm
Expand Down
4 changes: 3 additions & 1 deletion llvm/test/MC/ELF/rtti-proxy-gotpcrel.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
; RUN: llc %s -mtriple=x86_64-unknown-fuchsia -o - | FileCheck %s
; RUN: llc %s -mtriple=x86_64-unknown-fuchsia -o - | FileCheck %s
; RUN: llc %s -mtriple=aarch64-unknown-fuchsia -o - | FileCheck %s
; RUN: llc %s -mtriple=riscv64-unknown-fuchsia -o - | FileCheck %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
@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
Expand Down

0 comments on commit 712b302

Please sign in to comment.