xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
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"
110b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
120b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
130b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
140b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
150b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCValue.h"
170b57cec5SDimitry Andric using namespace llvm;
180b57cec5SDimitry Andric using namespace dwarf;
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
210b57cec5SDimitry Andric                                              const TargetMachine &TM) {
220b57cec5SDimitry Andric   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
230b57cec5SDimitry Andric   // AARCH64 ELF ABI does not define static relocation type for TLS offset
240b57cec5SDimitry Andric   // within a module.  Do not generate AT_location for TLS variables.
250b57cec5SDimitry Andric   SupportDebugThreadLocalLocation = false;
260b57cec5SDimitry Andric }
270b57cec5SDimitry Andric 
28*7a6dacacSDimitry Andric const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
29*7a6dacacSDimitry Andric     const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
30*7a6dacacSDimitry Andric     int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
31*7a6dacacSDimitry Andric   int64_t FinalOffset = Offset + MV.getConstant();
32*7a6dacacSDimitry Andric   const MCExpr *Res =
33*7a6dacacSDimitry Andric       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
34*7a6dacacSDimitry Andric   const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
35*7a6dacacSDimitry Andric   return MCBinaryExpr::createAdd(Res, Off, getContext());
36*7a6dacacSDimitry Andric }
37*7a6dacacSDimitry Andric 
3804eeddc0SDimitry Andric AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile() {
390b57cec5SDimitry Andric   SupportGOTPCRelWithOffset = false;
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric const MCExpr *AArch64_MachoTargetObjectFile::getTTypeGlobalReference(
430b57cec5SDimitry Andric     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
440b57cec5SDimitry Andric     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
450b57cec5SDimitry Andric   // On Darwin, we can reference dwarf symbols with foo@GOT-., which
460b57cec5SDimitry Andric   // is an indirect pc-relative reference. The default implementation
470b57cec5SDimitry Andric   // won't reference using the GOT, so we need this target-specific
480b57cec5SDimitry Andric   // version.
490b57cec5SDimitry Andric   if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) {
500b57cec5SDimitry Andric     const MCSymbol *Sym = TM.getSymbol(GV);
510b57cec5SDimitry Andric     const MCExpr *Res =
520b57cec5SDimitry Andric         MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, getContext());
530b57cec5SDimitry Andric     MCSymbol *PCSym = getContext().createTempSymbol();
545ffd83dbSDimitry Andric     Streamer.emitLabel(PCSym);
550b57cec5SDimitry Andric     const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
560b57cec5SDimitry Andric     return MCBinaryExpr::createSub(Res, PC, getContext());
570b57cec5SDimitry Andric   }
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
600b57cec5SDimitry Andric       GV, Encoding, TM, MMI, Streamer);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric MCSymbol *AArch64_MachoTargetObjectFile::getCFIPersonalitySymbol(
640b57cec5SDimitry Andric     const GlobalValue *GV, const TargetMachine &TM,
650b57cec5SDimitry Andric     MachineModuleInfo *MMI) const {
660b57cec5SDimitry Andric   return TM.getSymbol(GV);
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric const MCExpr *AArch64_MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
708bcb0991SDimitry Andric     const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
718bcb0991SDimitry Andric     int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
720b57cec5SDimitry Andric   assert((Offset+MV.getConstant() == 0) &&
730b57cec5SDimitry Andric          "Arch64 does not support GOT PC rel with extra offset");
740b57cec5SDimitry Andric   // On ARM64 Darwin, we can reference symbols with foo@GOT-., which
750b57cec5SDimitry Andric   // is an indirect pc-relative reference.
760b57cec5SDimitry Andric   const MCExpr *Res =
770b57cec5SDimitry Andric       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, getContext());
780b57cec5SDimitry Andric   MCSymbol *PCSym = getContext().createTempSymbol();
795ffd83dbSDimitry Andric   Streamer.emitLabel(PCSym);
800b57cec5SDimitry Andric   const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
810b57cec5SDimitry Andric   return MCBinaryExpr::createSub(Res, PC, getContext());
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric void AArch64_MachoTargetObjectFile::getNameWithPrefix(
850b57cec5SDimitry Andric     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
860b57cec5SDimitry Andric     const TargetMachine &TM) const {
870b57cec5SDimitry Andric   // AArch64 does not use section-relative relocations so any global symbol must
880b57cec5SDimitry Andric   // be accessed via at least a linker-private symbol.
890b57cec5SDimitry Andric   getMangler().getNameWithPrefix(OutName, GV, /* CannotUsePrivateLabel */ true);
900b57cec5SDimitry Andric }
91