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