LLVM 22.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"
20#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCValue.h"
22using namespace llvm;
23using namespace dwarf;
24
25void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
26 const TargetMachine &TM) {
30
31 // AARCH64 ELF ABI does not define static relocation type for TLS offset
32 // within a module. Do not generate AT_location for TLS variables.
34
35 // Make sure the implicitly created empty .text section has the
36 // SHF_AARCH64_PURECODE flag set if the "+execute-only" target feature is
37 // present.
38 if (TM.getMCSubtargetInfo()->hasFeature(AArch64::FeatureExecuteOnly)) {
39 auto *Text = static_cast<MCSectionELF *>(TextSection);
40 Text->setFlags(Text->getFlags() | ELF::SHF_AARCH64_PURECODE);
41 }
42}
43
45 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym,
46 const MachineModuleInfo *MMI) const {
49 MMI);
50 return;
51 }
52 auto *TS = static_cast<AArch64TargetStreamer *>(Streamer.getTargetStreamer());
53 // The value is ptrauth_string_discriminator("personality")
54 constexpr uint16_t Discriminator = 0x7EAD;
55 TS->emitAuthValue(MCSymbolRefExpr::create(Sym, getContext()), Discriminator,
56 AArch64PACKey::IA, /*HasAddressDiversity=*/true);
57}
58
60 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
61 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
62 int64_t FinalOffset = Offset + MV.getConstant();
63 const MCExpr *Res =
65 const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
66 return MCBinaryExpr::createAdd(Res, Off, getContext());
67}
68
71}
72
74 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
75 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
76 // On Darwin, we can reference dwarf symbols with foo@GOT-., which
77 // is an indirect pc-relative reference. The default implementation
78 // won't reference using the GOT, so we need this target-specific
79 // version.
80 if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) {
81 const MCSymbol *Sym = TM.getSymbol(GV);
82 const MCExpr *Res =
85 Streamer.emitLabel(PCSym);
86 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
87 return MCBinaryExpr::createSub(Res, PC, getContext());
88 }
89
91 GV, Encoding, TM, MMI, Streamer);
92}
93
95 const GlobalValue *GV, const TargetMachine &TM,
96 MachineModuleInfo *MMI) const {
97 return TM.getSymbol(GV);
98}
99
101 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
102 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
103 assert((Offset+MV.getConstant() == 0) &&
104 "Arch64 does not support GOT PC rel with extra offset");
105 // On ARM64 Darwin, we can reference symbols with foo@GOT-., which
106 // is an indirect pc-relative reference.
107 const MCExpr *Res =
110 Streamer.emitLabel(PCSym);
111 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
112 return MCBinaryExpr::createSub(Res, PC, getContext());
113}
114
116 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
117 const TargetMachine &TM) const {
118 // AArch64 does not use section-relative relocations so any global symbol must
119 // be accessed via at least a linker-private symbol.
120 getMangler().getNameWithPrefix(OutName, GV, /* CannotUsePrivateLabel */ true);
121}
122
123template <typename MachineModuleInfoTarget>
125 MCContext &Ctx, const TargetMachine &TM, MachineModuleInfo *MMI,
126 MachineModuleInfoTarget &TargetMMI, const MCSymbol *RawSym,
127 AArch64PACKey::ID Key, uint16_t Discriminator) {
128 const DataLayout &DL = MMI->getModule()->getDataLayout();
129
130 MCSymbol *StubSym = Ctx.getOrCreateSymbol(
131 DL.getLinkerPrivateGlobalPrefix() + RawSym->getName() +
132 Twine("$auth_ptr$") + AArch64PACKeyIDToString(Key) + Twine('$') +
133 Twine(Discriminator));
134
135 const MCExpr *&StubAuthPtrRef = TargetMMI.getAuthPtrStubEntry(StubSym);
136
137 if (StubAuthPtrRef)
138 return StubSym;
139
140 const MCExpr *Sym = MCSymbolRefExpr::create(RawSym, Ctx);
141
142 StubAuthPtrRef =
143 AArch64AuthMCExpr::create(Sym, Discriminator, Key,
144 /*HasAddressDiversity=*/false, Ctx);
145 return StubSym;
146}
147
149 const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym,
150 AArch64PACKey::ID Key, uint16_t Discriminator) const {
151 auto &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
152 return getAuthPtrSlotSymbolHelper(getContext(), TM, MMI, ELFMMI, RawSym, Key,
153 Discriminator);
154}
155
157 const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym,
158 AArch64PACKey::ID Key, uint16_t Discriminator) const {
159 auto &MachOMMI = MMI->getObjFileInfo<MachineModuleInfoMachO>();
160 return getAuthPtrSlotSymbolHelper(getContext(), TM, MMI, MachOMMI, RawSym,
161 Key, Discriminator);
162}
163
165 const TargetMachine &TM) {
166 if (const Function *F = dyn_cast<Function>(GO))
167 if (TM.getSubtarget<AArch64Subtarget>(*F).genExecuteOnly() && Kind.isText())
168 return true;
169 return false;
170}
171
173 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
174 // Set execute-only access for the explicit section
175 if (isExecuteOnlyFunction(GO, Kind, TM))
177
179}
180
182 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
183 // Set execute-only access for the explicit section
184 if (isExecuteOnlyFunction(GO, Kind, TM))
186
188}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static MCSymbol * getAuthPtrSlotSymbolHelper(MCContext &Ctx, const TargetMachine &TM, MachineModuleInfo *MMI, MachineModuleInfoTarget &TargetMMI, const MCSymbol *RawSym, AArch64PACKey::ID Key, uint16_t Discriminator)
static bool isExecuteOnlyFunction(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM)
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.
#define F(x, y, z)
Definition: MD5.cpp:55
static const AArch64AuthMCExpr * create(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key, bool HasAddressDiversity, MCContext &Ctx, SMLoc Loc=SMLoc())
MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
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.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
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, SMLoc Loc=SMLoc())
Definition: MCExpr.h:343
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:428
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:212
Context object for machine code objects.
Definition: MCContext.h:83
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:386
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:203
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
MCSection * TextSection
Section directive for standard text.
MCContext & getContext() const
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
Streaming machine code generation interface.
Definition: MCStreamer.h:220
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:395
MCTargetStreamer * getTargetStreamer()
Definition: MCStreamer.h:324
bool hasFeature(unsigned Feature) const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:188
int64_t getConstant() const
Definition: MCValue.h:44
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.
LLVM_ABI 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:278
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
static SectionKind getExecuteOnly()
Definition: SectionKind.h:191
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
virtual void emitPersonalityValueImpl(MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym, const MachineModuleInfo *MMI) const
MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
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:83
const MCSubtargetInfo * getMCSubtargetInfo() const
MCSymbol * getSymbol(const GlobalValue *GV) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
@ SHF_AARCH64_PURECODE
Definition: ELF.h:1338
@ DW_EH_PE_pcrel
Definition: Dwarf.h:865
@ DW_EH_PE_indirect
Definition: Dwarf.h:870
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
static StringRef AArch64PACKeyIDToString(AArch64PACKey::ID KeyID)
Return 2-letter identifier string for numeric key ID.