1 //===-- AArch64TargetObjectFile.cpp - AArch64 Object Info -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "AArch64TargetObjectFile.h"
10 #include "AArch64TargetMachine.h"
11 #include "MCTargetDesc/AArch64MCExpr.h"
12 #include "llvm/BinaryFormat/Dwarf.h"
13 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
14 #include "llvm/IR/Mangler.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCValue.h"
21 using namespace dwarf
;
23 void AArch64_ELFTargetObjectFile::Initialize(MCContext
&Ctx
,
24 const TargetMachine
&TM
) {
25 TargetLoweringObjectFileELF::Initialize(Ctx
, TM
);
26 // AARCH64 ELF ABI does not define static relocation type for TLS offset
27 // within a module. Do not generate AT_location for TLS variables.
28 SupportDebugThreadLocalLocation
= false;
31 const MCExpr
*AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
32 const GlobalValue
*GV
, const MCSymbol
*Sym
, const MCValue
&MV
,
33 int64_t Offset
, MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
34 int64_t FinalOffset
= Offset
+ MV
.getConstant();
36 MCSymbolRefExpr::create(Sym
, MCSymbolRefExpr::VK_GOTPCREL
, getContext());
37 const MCExpr
*Off
= MCConstantExpr::create(FinalOffset
, getContext());
38 return MCBinaryExpr::createAdd(Res
, Off
, getContext());
41 AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
42 SupportGOTPCRelWithOffset
= false;
45 const MCExpr
*AArch64_MachoTargetObjectFile::getTTypeGlobalReference(
46 const GlobalValue
*GV
, unsigned Encoding
, const TargetMachine
&TM
,
47 MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
48 // On Darwin, we can reference dwarf symbols with foo@GOT-., which
49 // is an indirect pc-relative reference. The default implementation
50 // won't reference using the GOT, so we need this target-specific
52 if (Encoding
& (DW_EH_PE_indirect
| DW_EH_PE_pcrel
)) {
53 const MCSymbol
*Sym
= TM
.getSymbol(GV
);
55 MCSymbolRefExpr::create(Sym
, MCSymbolRefExpr::VK_GOT
, getContext());
56 MCSymbol
*PCSym
= getContext().createTempSymbol();
57 Streamer
.emitLabel(PCSym
);
58 const MCExpr
*PC
= MCSymbolRefExpr::create(PCSym
, getContext());
59 return MCBinaryExpr::createSub(Res
, PC
, getContext());
62 return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
63 GV
, Encoding
, TM
, MMI
, Streamer
);
66 MCSymbol
*AArch64_MachoTargetObjectFile::getCFIPersonalitySymbol(
67 const GlobalValue
*GV
, const TargetMachine
&TM
,
68 MachineModuleInfo
*MMI
) const {
69 return TM
.getSymbol(GV
);
72 const MCExpr
*AArch64_MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
73 const GlobalValue
*GV
, const MCSymbol
*Sym
, const MCValue
&MV
,
74 int64_t Offset
, MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
75 assert((Offset
+MV
.getConstant() == 0) &&
76 "Arch64 does not support GOT PC rel with extra offset");
77 // On ARM64 Darwin, we can reference symbols with foo@GOT-., which
78 // is an indirect pc-relative reference.
80 MCSymbolRefExpr::create(Sym
, MCSymbolRefExpr::VK_GOT
, getContext());
81 MCSymbol
*PCSym
= getContext().createTempSymbol();
82 Streamer
.emitLabel(PCSym
);
83 const MCExpr
*PC
= MCSymbolRefExpr::create(PCSym
, getContext());
84 return MCBinaryExpr::createSub(Res
, PC
, getContext());
87 void AArch64_MachoTargetObjectFile::getNameWithPrefix(
88 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
89 const TargetMachine
&TM
) const {
90 // AArch64 does not use section-relative relocations so any global symbol must
91 // be accessed via at least a linker-private symbol.
92 getMangler().getNameWithPrefix(OutName
, GV
, /* CannotUsePrivateLabel */ true);
95 template <typename MachineModuleInfoTarget
>
96 static MCSymbol
*getAuthPtrSlotSymbolHelper(
97 MCContext
&Ctx
, const TargetMachine
&TM
, MachineModuleInfo
*MMI
,
98 MachineModuleInfoTarget
&TargetMMI
, const MCSymbol
*RawSym
,
99 AArch64PACKey::ID Key
, uint16_t Discriminator
) {
100 const DataLayout
&DL
= MMI
->getModule()->getDataLayout();
102 MCSymbol
*StubSym
= Ctx
.getOrCreateSymbol(
103 DL
.getLinkerPrivateGlobalPrefix() + RawSym
->getName() +
104 Twine("$auth_ptr$") + AArch64PACKeyIDToString(Key
) + Twine('$') +
105 Twine(Discriminator
));
107 const MCExpr
*&StubAuthPtrRef
= TargetMMI
.getAuthPtrStubEntry(StubSym
);
112 const MCExpr
*Sym
= MCSymbolRefExpr::create(RawSym
, Ctx
);
115 AArch64AuthMCExpr::create(Sym
, Discriminator
, Key
,
116 /*HasAddressDiversity=*/false, Ctx
);
120 MCSymbol
*AArch64_ELFTargetObjectFile::getAuthPtrSlotSymbol(
121 const TargetMachine
&TM
, MachineModuleInfo
*MMI
, const MCSymbol
*RawSym
,
122 AArch64PACKey::ID Key
, uint16_t Discriminator
) const {
123 auto &ELFMMI
= MMI
->getObjFileInfo
<MachineModuleInfoELF
>();
124 return getAuthPtrSlotSymbolHelper(getContext(), TM
, MMI
, ELFMMI
, RawSym
, Key
,
128 MCSymbol
*AArch64_MachoTargetObjectFile::getAuthPtrSlotSymbol(
129 const TargetMachine
&TM
, MachineModuleInfo
*MMI
, const MCSymbol
*RawSym
,
130 AArch64PACKey::ID Key
, uint16_t Discriminator
) const {
131 auto &MachOMMI
= MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
132 return getAuthPtrSlotSymbolHelper(getContext(), TM
, MMI
, MachOMMI
, RawSym
,