10b57cec5SDimitry Andric //===-- AArch64TargetObjectFile.cpp - AArch64 Object Info -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "AArch64TargetObjectFile.h"
100b57cec5SDimitry Andric #include "AArch64TargetMachine.h"
11*0fca6ea1SDimitry Andric #include "MCTargetDesc/AArch64MCExpr.h"
12*0fca6ea1SDimitry Andric #include "llvm/ADT/StringExtras.h"
130b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
14*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
150b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
16*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCValue.h"
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric using namespace dwarf;
230b57cec5SDimitry Andric
Initialize(MCContext & Ctx,const TargetMachine & TM)240b57cec5SDimitry Andric void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
250b57cec5SDimitry Andric const TargetMachine &TM) {
260b57cec5SDimitry Andric TargetLoweringObjectFileELF::Initialize(Ctx, TM);
270b57cec5SDimitry Andric // AARCH64 ELF ABI does not define static relocation type for TLS offset
280b57cec5SDimitry Andric // within a module. Do not generate AT_location for TLS variables.
290b57cec5SDimitry Andric SupportDebugThreadLocalLocation = false;
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric
getIndirectSymViaGOTPCRel(const GlobalValue * GV,const MCSymbol * Sym,const MCValue & MV,int64_t Offset,MachineModuleInfo * MMI,MCStreamer & Streamer) const327a6dacacSDimitry Andric const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
337a6dacacSDimitry Andric const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
347a6dacacSDimitry Andric int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
357a6dacacSDimitry Andric int64_t FinalOffset = Offset + MV.getConstant();
367a6dacacSDimitry Andric const MCExpr *Res =
377a6dacacSDimitry Andric MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
387a6dacacSDimitry Andric const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
397a6dacacSDimitry Andric return MCBinaryExpr::createAdd(Res, Off, getContext());
407a6dacacSDimitry Andric }
417a6dacacSDimitry Andric
AArch64_MachoTargetObjectFile()4204eeddc0SDimitry Andric AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
430b57cec5SDimitry Andric SupportGOTPCRelWithOffset = false;
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric
getTTypeGlobalReference(const GlobalValue * GV,unsigned Encoding,const TargetMachine & TM,MachineModuleInfo * MMI,MCStreamer & Streamer) const460b57cec5SDimitry Andric const MCExpr *AArch64_MachoTargetObjectFile::getTTypeGlobalReference(
470b57cec5SDimitry Andric const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
480b57cec5SDimitry Andric MachineModuleInfo *MMI, MCStreamer &Streamer) const {
490b57cec5SDimitry Andric // On Darwin, we can reference dwarf symbols with foo@GOT-., which
500b57cec5SDimitry Andric // is an indirect pc-relative reference. The default implementation
510b57cec5SDimitry Andric // won't reference using the GOT, so we need this target-specific
520b57cec5SDimitry Andric // version.
530b57cec5SDimitry Andric if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) {
540b57cec5SDimitry Andric const MCSymbol *Sym = TM.getSymbol(GV);
550b57cec5SDimitry Andric const MCExpr *Res =
560b57cec5SDimitry Andric MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, getContext());
570b57cec5SDimitry Andric MCSymbol *PCSym = getContext().createTempSymbol();
585ffd83dbSDimitry Andric Streamer.emitLabel(PCSym);
590b57cec5SDimitry Andric const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
600b57cec5SDimitry Andric return MCBinaryExpr::createSub(Res, PC, getContext());
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
640b57cec5SDimitry Andric GV, Encoding, TM, MMI, Streamer);
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric
getCFIPersonalitySymbol(const GlobalValue * GV,const TargetMachine & TM,MachineModuleInfo * MMI) const670b57cec5SDimitry Andric MCSymbol *AArch64_MachoTargetObjectFile::getCFIPersonalitySymbol(
680b57cec5SDimitry Andric const GlobalValue *GV, const TargetMachine &TM,
690b57cec5SDimitry Andric MachineModuleInfo *MMI) const {
700b57cec5SDimitry Andric return TM.getSymbol(GV);
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric
getIndirectSymViaGOTPCRel(const GlobalValue * GV,const MCSymbol * Sym,const MCValue & MV,int64_t Offset,MachineModuleInfo * MMI,MCStreamer & Streamer) const730b57cec5SDimitry Andric const MCExpr *AArch64_MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
748bcb0991SDimitry Andric const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
758bcb0991SDimitry Andric int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
760b57cec5SDimitry Andric assert((Offset+MV.getConstant() == 0) &&
770b57cec5SDimitry Andric "Arch64 does not support GOT PC rel with extra offset");
780b57cec5SDimitry Andric // On ARM64 Darwin, we can reference symbols with foo@GOT-., which
790b57cec5SDimitry Andric // is an indirect pc-relative reference.
800b57cec5SDimitry Andric const MCExpr *Res =
810b57cec5SDimitry Andric MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, getContext());
820b57cec5SDimitry Andric MCSymbol *PCSym = getContext().createTempSymbol();
835ffd83dbSDimitry Andric Streamer.emitLabel(PCSym);
840b57cec5SDimitry Andric const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
850b57cec5SDimitry Andric return MCBinaryExpr::createSub(Res, PC, getContext());
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric
getNameWithPrefix(SmallVectorImpl<char> & OutName,const GlobalValue * GV,const TargetMachine & TM) const880b57cec5SDimitry Andric void AArch64_MachoTargetObjectFile::getNameWithPrefix(
890b57cec5SDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV,
900b57cec5SDimitry Andric const TargetMachine &TM) const {
910b57cec5SDimitry Andric // AArch64 does not use section-relative relocations so any global symbol must
920b57cec5SDimitry Andric // be accessed via at least a linker-private symbol.
930b57cec5SDimitry Andric getMangler().getNameWithPrefix(OutName, GV, /* CannotUsePrivateLabel */ true);
940b57cec5SDimitry Andric }
95*0fca6ea1SDimitry Andric
96*0fca6ea1SDimitry Andric template <typename MachineModuleInfoTarget>
getAuthPtrSlotSymbolHelper(MCContext & Ctx,const TargetMachine & TM,MachineModuleInfo * MMI,MachineModuleInfoTarget & TargetMMI,const MCSymbol * RawSym,AArch64PACKey::ID Key,uint16_t Discriminator)97*0fca6ea1SDimitry Andric static MCSymbol *getAuthPtrSlotSymbolHelper(
98*0fca6ea1SDimitry Andric MCContext &Ctx, const TargetMachine &TM, MachineModuleInfo *MMI,
99*0fca6ea1SDimitry Andric MachineModuleInfoTarget &TargetMMI, const MCSymbol *RawSym,
100*0fca6ea1SDimitry Andric AArch64PACKey::ID Key, uint16_t Discriminator) {
101*0fca6ea1SDimitry Andric const DataLayout &DL = MMI->getModule()->getDataLayout();
102*0fca6ea1SDimitry Andric
103*0fca6ea1SDimitry Andric MCSymbol *StubSym = Ctx.getOrCreateSymbol(
104*0fca6ea1SDimitry Andric DL.getLinkerPrivateGlobalPrefix() + RawSym->getName() +
105*0fca6ea1SDimitry Andric Twine("$auth_ptr$") + AArch64PACKeyIDToString(Key) + Twine('$') +
106*0fca6ea1SDimitry Andric Twine(Discriminator));
107*0fca6ea1SDimitry Andric
108*0fca6ea1SDimitry Andric const MCExpr *&StubAuthPtrRef = TargetMMI.getAuthPtrStubEntry(StubSym);
109*0fca6ea1SDimitry Andric
110*0fca6ea1SDimitry Andric if (StubAuthPtrRef)
111*0fca6ea1SDimitry Andric return StubSym;
112*0fca6ea1SDimitry Andric
113*0fca6ea1SDimitry Andric const MCExpr *Sym = MCSymbolRefExpr::create(RawSym, Ctx);
114*0fca6ea1SDimitry Andric
115*0fca6ea1SDimitry Andric StubAuthPtrRef =
116*0fca6ea1SDimitry Andric AArch64AuthMCExpr::create(Sym, Discriminator, Key,
117*0fca6ea1SDimitry Andric /*HasAddressDiversity=*/false, Ctx);
118*0fca6ea1SDimitry Andric return StubSym;
119*0fca6ea1SDimitry Andric }
120*0fca6ea1SDimitry Andric
getAuthPtrSlotSymbol(const TargetMachine & TM,MachineModuleInfo * MMI,const MCSymbol * RawSym,AArch64PACKey::ID Key,uint16_t Discriminator) const121*0fca6ea1SDimitry Andric MCSymbol *AArch64_ELFTargetObjectFile::getAuthPtrSlotSymbol(
122*0fca6ea1SDimitry Andric const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym,
123*0fca6ea1SDimitry Andric AArch64PACKey::ID Key, uint16_t Discriminator) const {
124*0fca6ea1SDimitry Andric auto &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
125*0fca6ea1SDimitry Andric return getAuthPtrSlotSymbolHelper(getContext(), TM, MMI, ELFMMI, RawSym, Key,
126*0fca6ea1SDimitry Andric Discriminator);
127*0fca6ea1SDimitry Andric }
128*0fca6ea1SDimitry Andric
getAuthPtrSlotSymbol(const TargetMachine & TM,MachineModuleInfo * MMI,const MCSymbol * RawSym,AArch64PACKey::ID Key,uint16_t Discriminator) const129*0fca6ea1SDimitry Andric MCSymbol *AArch64_MachoTargetObjectFile::getAuthPtrSlotSymbol(
130*0fca6ea1SDimitry Andric const TargetMachine &TM, MachineModuleInfo *MMI, const MCSymbol *RawSym,
131*0fca6ea1SDimitry Andric AArch64PACKey::ID Key, uint16_t Discriminator) const {
132*0fca6ea1SDimitry Andric auto &MachOMMI = MMI->getObjFileInfo<MachineModuleInfoMachO>();
133*0fca6ea1SDimitry Andric return getAuthPtrSlotSymbolHelper(getContext(), TM, MMI, MachOMMI, RawSym,
134*0fca6ea1SDimitry Andric Key, Discriminator);
135*0fca6ea1SDimitry Andric }
136