LLVM 20.0.0git
AArch64TargetObjectFile.cpp
Go to the documentation of this file.
1//===-- AArch64TargetObjectFile.cpp - AArch64 Object Info -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
15#include "llvm/IR/Mangler.h"
16#include "llvm/IR/Module.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCValue.h"
21using namespace llvm;
22using namespace dwarf;
23
24void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
25 const TargetMachine &TM) {
27 // AARCH64 ELF ABI does not define static relocation type for TLS offset
28 // within a module. Do not generate AT_location for TLS variables.
30}
31
33 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym,
34 const MachineModuleInfo *MMI) const {
37 MMI);
38 return;
39 }
40 auto *TS = static_cast<AArch64TargetStreamer *>(Streamer.getTargetStreamer());
41 // The value is ptrauth_string_discriminator("personality")
42 constexpr uint16_t Discriminator = 0x7EAD;
43 TS->emitAuthValue(MCSymbolRefExpr::create(Sym, getContext()), Discriminator,
44 AArch64PACKey::IA, /*HasAddressDiversity=*/true);
45}
46
48 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
49 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
50 int64_t FinalOffset = Offset + MV.getConstant();
51 const MCExpr *Res =
53 const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
54 return MCBinaryExpr::createAdd(Res, Off, getContext());
55}
56
59}
60
62 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
63 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
64 // On Darwin, we can reference dwarf symbols with foo@GOT-., which
65 // is an indirect pc-relative reference. The default implementation
66 // won't reference using the GOT, so we need this target-specific
67 // version.
68 if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) {
69 const MCSymbol *Sym = TM.getSymbol(GV);
70 const MCExpr *Res =
73 Streamer.emitLabel(PCSym);
74 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
75 return MCBinaryExpr::createSub(Res, PC, getContext());
76 }
77
79 GV, Encoding, TM, MMI, Streamer);
80}
81
83 const GlobalValue *GV, const TargetMachine &TM,
84 MachineModuleInfo *MMI) const {
85 return TM.getSymbol(GV);
86}
87
89 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
90 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
91 assert((Offset+MV.getConstant() == 0) &&
92 "Arch64 does not support GOT PC rel with extra offset");
93 // On ARM64 Darwin, we can reference symbols with foo@GOT-., which
94 // is an indirect pc-relative reference.
95 const MCExpr *Res =
98 Streamer.emitLabel(PCSym);
99 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
100 return MCBinaryExpr::createSub(Res, PC, getContext());
101}
102
104 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
105 const TargetMachine &TM) const {
106 // AArch64 does not use section-relative relocations so any global symbol must
107 // be accessed via at least a linker-private symbol.
108 getMangler().getNameWithPrefix(OutName, GV, /* CannotUsePrivateLabel */ true);
109}
110
111template <typename MachineModuleInfoTarget>
113 MCContext &Ctx, const TargetMachine &TM, MachineModuleInfo *MMI,
114 MachineModuleInfoTarget &TargetMMI, const MCSymbol *RawSym,
115 AArch64PACKey::ID Key, uint16_t Discriminator) {
116 const DataLayout &DL = MMI->getModule()->getDataLayout();
117
118 MCSymbol *StubSym = Ctx.getOrCreateSymbol(
119 DL.getLinkerPrivateGlobalPrefix() + RawSym->getName() +
120 Twine("$auth_ptr$") + AArch64PACKeyIDToString(Key) + Twine('$') +
121 Twine(Discriminator));
122
123 const MCExpr *&StubAuthPtrRef = TargetMMI.getAuthPtrStubEntry(StubSym);
124
125 if (StubAuthPtrRef)
126 return StubSym;
127
128 const MCExpr *Sym = MCSymbolRefExpr::create(RawSym, Ctx);
129
130 StubAuthPtrRef =
131 AArch64AuthMCExpr::create(Sym, Discriminator, Key,
132 /*HasAddressDiversity=*/false, Ctx);
133 return StubSym;
134}
135
137 const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym,
138 AArch64PACKey::ID Key, uint16_t Discriminator) const {
139 auto &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
140 return getAuthPtrSlotSymbolHelper(getContext(), TM, MMI, ELFMMI, RawSym, Key,
141 Discriminator);
142}
143
145 const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym,
146 AArch64PACKey::ID Key, uint16_t Discriminator) const {
147 auto &MachOMMI = MMI->getObjFileInfo<MachineModuleInfoMachO>();
148 return getAuthPtrSlotSymbolHelper(getContext(), TM, MMI, MachOMMI, RawSym,
149 Key, Discriminator);
150}
static MCSymbol * getAuthPtrSlotSymbolHelper(MCContext &Ctx, const TargetMachine &TM, MachineModuleInfo *MMI, MachineModuleInfoTarget &TargetMMI, const MCSymbol *RawSym, AArch64PACKey::ID Key, uint16_t Discriminator)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains constants used for implementing Dwarf debug support.
Symbol * Sym
Definition: ELF_riscv.cpp:479
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const AArch64AuthMCExpr * create(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key, bool HasAddressDiversity, MCContext &Ctx)
void emitPersonalityValueImpl(MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym, const MachineModuleInfo *MMI) const override
const MCExpr * getIndirectSymViaGOTPCRel(const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
Get the target specific PC relative GOT entry relocation.
MCSymbol * getAuthPtrSlotSymbol(const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym, AArch64PACKey::ID Key, uint16_t Discriminator) const
MCSymbol * getAuthPtrSlotSymbol(const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym, AArch64PACKey::ID Key, uint16_t Discriminator) const
const MCExpr * getIndirectSymViaGOTPCRel(const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
Get MachO PC relative GOT entry relocation.
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, const TargetMachine &TM) const override
MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const override
const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
The mach-o version of this method defaults to returning a stub reference.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:537
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:622
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
Context object for machine code objects.
Definition: MCContext.h:83
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:345
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:212
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
MCContext & getContext() const
Streaming machine code generation interface.
Definition: MCStreamer.h:213
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:420
MCTargetStreamer * getTargetStreamer()
Definition: MCStreamer.h:309
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
This represents an "assembler immediate".
Definition: MCValue.h:36
int64_t getConstant() const
Definition: MCValue.h:43
MachineModuleInfoELF - This is a MachineModuleInfoImpl implementation for ELF targets.
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets.
This class contains meta information specific to a module.
const Module * getModule() const
Ty & getObjFileInfo()
Keep track of various per-module pieces of information for backends that would like to do so.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:121
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:294
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
virtual void emitPersonalityValueImpl(MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym, const MachineModuleInfo *MMI) const
const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
The mach-o version of this method defaults to returning a stub reference.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
MCSymbol * getSymbol(const GlobalValue *GV) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
@ DW_EH_PE_pcrel
Definition: Dwarf.h:858
@ DW_EH_PE_indirect
Definition: Dwarf.h:863
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
static StringRef AArch64PACKeyIDToString(AArch64PACKey::ID KeyID)
Return 2-letter identifier string for numeric key ID.