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
9#include "AArch64TargetObjectFile.h"
10#include "AArch64TargetMachine.h"
11#include "MCTargetDesc/AArch64MCAsmInfo.h"
12#include "MCTargetDesc/AArch64TargetStreamer.h"
13#include "llvm/BinaryFormat/Dwarf.h"
14#include "llvm/CodeGen/MachineModuleInfoImpls.h"
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/MCSectionELF.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) {
27 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
28 PLTPCRelativeSpecifier = AArch64::S_PLT;
29 SupportIndirectSymViaGOTPCRel = true;
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.
33 SupportDebugThreadLocalLocation = false;
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(Feature: AArch64::FeatureExecuteOnly)) {
39 auto *Text = static_cast<MCSectionELF *>(TextSection);
40 Text->setFlags(Text->getFlags() | ELF::SHF_AARCH64_PURECODE);
41 }
42}
43
44void AArch64_ELFTargetObjectFile::emitPersonalityValueImpl(
45 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym,
46 const MachineModuleInfo *MMI) const {
47 if (!MMI->getObjFileInfo<MachineModuleInfoELF>().hasSignedPersonality()) {
48 TargetLoweringObjectFileELF::emitPersonalityValueImpl(Streamer, DL, Sym,
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(Expr: MCSymbolRefExpr::create(Symbol: Sym, Ctx&: getContext()), Discriminator,
56 Key: AArch64PACKey::IA, /*HasAddressDiversity=*/true);
57}
58
59const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
60 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
61 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
62 auto &Ctx = getContext();
63 const MCExpr *Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx);
64 if (int64_t FinalOffset = Offset + MV.getConstant())
65 Res = MCBinaryExpr::createAdd(LHS: Res, RHS: MCConstantExpr::create(Value: FinalOffset, Ctx),
66 Ctx);
67 return MCSpecifierExpr::create(Expr: Res, S: AArch64::S_GOTPCREL, Ctx);
68}
69
70AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
71 SupportGOTPCRelWithOffset = false;
72}
73
74const MCExpr *AArch64_MachoTargetObjectFile::getTTypeGlobalReference(
75 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
76 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
77 // On Darwin, we can reference dwarf symbols with foo@GOT-., which
78 // is an indirect pc-relative reference. The default implementation
79 // won't reference using the GOT, so we need this target-specific
80 // version.
81 if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) {
82 const MCSymbol *Sym = TM.getSymbol(GV);
83 const MCExpr *Res =
84 MCSymbolRefExpr::create(Symbol: Sym, specifier: AArch64::S_MACHO_GOT, Ctx&: getContext());
85 MCSymbol *PCSym = getContext().createTempSymbol();
86 Streamer.emitLabel(Symbol: PCSym);
87 const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: getContext());
88 return MCBinaryExpr::createSub(LHS: Res, RHS: PC, Ctx&: getContext());
89 }
90
91 return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
92 GV, Encoding, TM, MMI, Streamer);
93}
94
95MCSymbol *AArch64_MachoTargetObjectFile::getCFIPersonalitySymbol(
96 const GlobalValue *GV, const TargetMachine &TM,
97 MachineModuleInfo *MMI) const {
98 return TM.getSymbol(GV);
99}
100
101const MCExpr *AArch64_MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
102 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
103 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
104 assert((Offset+MV.getConstant() == 0) &&
105 "Arch64 does not support GOT PC rel with extra offset");
106 // On ARM64 Darwin, we can reference symbols with foo@GOT-., which
107 // is an indirect pc-relative reference.
108 const MCExpr *Res =
109 MCSymbolRefExpr::create(Symbol: Sym, specifier: AArch64::S_MACHO_GOT, Ctx&: getContext());
110 MCSymbol *PCSym = getContext().createTempSymbol();
111 Streamer.emitLabel(Symbol: PCSym);
112 const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: getContext());
113 return MCBinaryExpr::createSub(LHS: Res, RHS: PC, Ctx&: getContext());
114}
115
116void AArch64_MachoTargetObjectFile::getNameWithPrefix(
117 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
118 const TargetMachine &TM) const {
119 // AArch64 does not use section-relative relocations so any global symbol must
120 // be accessed via at least a linker-private symbol.
121 getMangler().getNameWithPrefix(OutName, GV, /* CannotUsePrivateLabel */ true);
122}
123
124template <typename MachineModuleInfoTarget>
125static MCSymbol *getAuthPtrSlotSymbolHelper(
126 MCContext &Ctx, const TargetMachine &TM, MachineModuleInfo *MMI,
127 MachineModuleInfoTarget &TargetMMI, const MCSymbol *RawSym,
128 AArch64PACKey::ID Key, uint16_t Discriminator) {
129 const DataLayout &DL = MMI->getModule()->getDataLayout();
130
131 MCSymbol *StubSym = Ctx.getOrCreateSymbol(
132 Name: DL.getLinkerPrivateGlobalPrefix() + RawSym->getName() +
133 Twine("$auth_ptr$") + AArch64PACKeyIDToString(KeyID: Key) + Twine('$') +
134 Twine(Discriminator));
135
136 const MCExpr *&StubAuthPtrRef = TargetMMI.getAuthPtrStubEntry(StubSym);
137
138 if (StubAuthPtrRef)
139 return StubSym;
140
141 const MCExpr *Sym = MCSymbolRefExpr::create(Symbol: RawSym, Ctx);
142
143 StubAuthPtrRef =
144 AArch64AuthMCExpr::create(Expr: Sym, Discriminator, Key,
145 /*HasAddressDiversity=*/false, Ctx);
146 return StubSym;
147}
148
149MCSymbol *AArch64_ELFTargetObjectFile::getAuthPtrSlotSymbol(
150 const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym,
151 AArch64PACKey::ID Key, uint16_t Discriminator) const {
152 auto &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
153 return getAuthPtrSlotSymbolHelper(Ctx&: getContext(), TM, MMI, TargetMMI&: ELFMMI, RawSym, Key,
154 Discriminator);
155}
156
157MCSymbol *AArch64_MachoTargetObjectFile::getAuthPtrSlotSymbol(
158 const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym,
159 AArch64PACKey::ID Key, uint16_t Discriminator) const {
160 auto &MachOMMI = MMI->getObjFileInfo<MachineModuleInfoMachO>();
161 return getAuthPtrSlotSymbolHelper(Ctx&: getContext(), TM, MMI, TargetMMI&: MachOMMI, RawSym,
162 Key, Discriminator);
163}
164
165static bool isExecuteOnlyFunction(const GlobalObject *GO, SectionKind Kind,
166 const TargetMachine &TM) {
167 if (const Function *F = dyn_cast<Function>(Val: GO))
168 if (TM.getSubtarget<AArch64Subtarget>(F: *F).genExecuteOnly() && Kind.isText())
169 return true;
170 return false;
171}
172
173MCSection *AArch64_ELFTargetObjectFile::getExplicitSectionGlobal(
174 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
175 // Set execute-only access for the explicit section
176 if (isExecuteOnlyFunction(GO, Kind, TM))
177 Kind = SectionKind::getExecuteOnly();
178
179 return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind, TM);
180}
181
182MCSection *AArch64_ELFTargetObjectFile::SelectSectionForGlobal(
183 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
184 // Set execute-only access for the explicit section
185 if (isExecuteOnlyFunction(GO, Kind, TM))
186 Kind = SectionKind::getExecuteOnly();
187
188 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
189}
190