[AsmPrinter][TLOF] ARM64 MachO support for replacing GOT equivalents
[oota-llvm.git] / lib / Target / AArch64 / AArch64TargetObjectFile.cpp
1 //===-- AArch64TargetObjectFile.cpp - AArch64 Object Info -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "AArch64TargetObjectFile.h"
11 #include "AArch64TargetMachine.h"
12 #include "llvm/IR/Mangler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Support/Dwarf.h"
17 using namespace llvm;
18 using namespace dwarf;
19
20 void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
21                                              const TargetMachine &TM) {
22   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
23   InitializeELF(TM.Options.UseInitArray);
24 }
25
26 AArch64_MachoTargetObjectFile::AArch64_MachoTargetObjectFile()
27   : TargetLoweringObjectFileMachO() {
28   SupportIndirectSymViaGOTPCRel = true;
29   SupportGOTPCRelWithOffset = false;
30 }
31
32 const MCExpr *AArch64_MachoTargetObjectFile::getTTypeGlobalReference(
33     const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
34     const TargetMachine &TM, MachineModuleInfo *MMI,
35     MCStreamer &Streamer) const {
36   // On Darwin, we can reference dwarf symbols with foo@GOT-., which
37   // is an indirect pc-relative reference. The default implementation
38   // won't reference using the GOT, so we need this target-specific
39   // version.
40   if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) {
41     const MCSymbol *Sym = TM.getSymbol(GV, Mang);
42     const MCExpr *Res =
43         MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOT, getContext());
44     MCSymbol *PCSym = getContext().CreateTempSymbol();
45     Streamer.EmitLabel(PCSym);
46     const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
47     return MCBinaryExpr::CreateSub(Res, PC, getContext());
48   }
49
50   return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
51       GV, Encoding, Mang, TM, MMI, Streamer);
52 }
53
54 MCSymbol *AArch64_MachoTargetObjectFile::getCFIPersonalitySymbol(
55     const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
56     MachineModuleInfo *MMI) const {
57   return TM.getSymbol(GV, Mang);
58 }
59
60 const MCExpr *AArch64_MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
61     const MCSymbol *Sym, int64_t Offset, MCStreamer &Streamer) const {
62   // On ARM64 Darwin, we can reference symbols with foo@GOT-., which
63   // is an indirect pc-relative reference.
64   const MCExpr *Res =
65       MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOT, getContext());
66   MCSymbol *PCSym = getContext().CreateTempSymbol();
67   Streamer.EmitLabel(PCSym);
68   const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
69   return MCBinaryExpr::CreateSub(Res, PC, getContext());
70 }