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