xref: /llvm-project/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp (revision f65a21a4ecc2e712c700c59842b6b9a1d2a9a060)
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/AArch64MCExpr.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/MCStreamer.h"
20 #include "llvm/MC/MCValue.h"
21 using namespace llvm;
22 using namespace dwarf;
23 
24 void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
25                                              const TargetMachine &TM) {
26   TargetLoweringObjectFileELF::Initialize(Ctx, 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.
29   SupportDebugThreadLocalLocation = false;
30 }
31 
32 void AArch64_ELFTargetObjectFile::emitPersonalityValueImpl(
33     MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym,
34     const MachineModuleInfo *MMI) const {
35   if (!MMI->getObjFileInfo<MachineModuleInfoELF>().hasSignedPersonality()) {
36     TargetLoweringObjectFileELF::emitPersonalityValueImpl(Streamer, DL, Sym,
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 
47 const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
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 =
52       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
53   const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
54   return MCBinaryExpr::createAdd(Res, Off, getContext());
55 }
56 
57 AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
58   SupportGOTPCRelWithOffset = false;
59 }
60 
61 const MCExpr *AArch64_MachoTargetObjectFile::getTTypeGlobalReference(
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 =
71         MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, getContext());
72     MCSymbol *PCSym = getContext().createTempSymbol();
73     Streamer.emitLabel(PCSym);
74     const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
75     return MCBinaryExpr::createSub(Res, PC, getContext());
76   }
77 
78   return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
79       GV, Encoding, TM, MMI, Streamer);
80 }
81 
82 MCSymbol *AArch64_MachoTargetObjectFile::getCFIPersonalitySymbol(
83     const GlobalValue *GV, const TargetMachine &TM,
84     MachineModuleInfo *MMI) const {
85   return TM.getSymbol(GV);
86 }
87 
88 const MCExpr *AArch64_MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
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 =
96       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, getContext());
97   MCSymbol *PCSym = getContext().createTempSymbol();
98   Streamer.emitLabel(PCSym);
99   const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
100   return MCBinaryExpr::createSub(Res, PC, getContext());
101 }
102 
103 void AArch64_MachoTargetObjectFile::getNameWithPrefix(
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 
111 template <typename MachineModuleInfoTarget>
112 static MCSymbol *getAuthPtrSlotSymbolHelper(
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 
136 MCSymbol *AArch64_ELFTargetObjectFile::getAuthPtrSlotSymbol(
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 
144 MCSymbol *AArch64_MachoTargetObjectFile::getAuthPtrSlotSymbol(
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 }
151