]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/X86/X86TargetObjectFile.cpp
Merge llvm, clang, lld and lldb trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / X86 / X86TargetObjectFile.cpp
1 //===-- X86TargetObjectFile.cpp - X86 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 "X86TargetObjectFile.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/IR/Mangler.h"
13 #include "llvm/IR/Operator.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCSectionCOFF.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/COFF.h"
20 #include "llvm/Support/Dwarf.h"
21 #include "llvm/Target/TargetLowering.h"
22
23 using namespace llvm;
24 using namespace dwarf;
25
26 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
27     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
28     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
29
30   // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
31   // is an indirect pc-relative reference.
32   if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
33     const MCSymbol *Sym = TM.getSymbol(GV);
34     const MCExpr *Res =
35       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
36     const MCExpr *Four = MCConstantExpr::create(4, getContext());
37     return MCBinaryExpr::createAdd(Res, Four, getContext());
38   }
39
40   return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
41       GV, Encoding, TM, MMI, Streamer);
42 }
43
44 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
45     const GlobalValue *GV, const TargetMachine &TM,
46     MachineModuleInfo *MMI) const {
47   return TM.getSymbol(GV);
48 }
49
50 const MCExpr *X86_64MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
51     const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
52     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
53   // On Darwin/X86-64, we need to use foo@GOTPCREL+4 to access the got entry
54   // from a data section. In case there's an additional offset, then use
55   // foo@GOTPCREL+4+<offset>.
56   unsigned FinalOff = Offset+MV.getConstant()+4;
57   const MCExpr *Res =
58     MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
59   const MCExpr *Off = MCConstantExpr::create(FinalOff, getContext());
60   return MCBinaryExpr::createAdd(Res, Off, getContext());
61 }
62
63 const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
64     const MCSymbol *Sym) const {
65   return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
66 }
67
68 void
69 X86FreeBSDTargetObjectFile::Initialize(MCContext &Ctx,
70                                        const TargetMachine &TM) {
71   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
72   InitializeELF(TM.Options.UseInitArray);
73 }
74
75 void
76 X86FuchsiaTargetObjectFile::Initialize(MCContext &Ctx,
77                                        const TargetMachine &TM) {
78   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
79   InitializeELF(TM.Options.UseInitArray);
80 }
81
82 void
83 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
84                                          const TargetMachine &TM) {
85   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
86   InitializeELF(TM.Options.UseInitArray);
87 }
88
89 const MCExpr *X86WindowsTargetObjectFile::lowerRelativeReference(
90     const GlobalValue *LHS, const GlobalValue *RHS,
91     const TargetMachine &TM) const {
92   // Our symbols should exist in address space zero, cowardly no-op if
93   // otherwise.
94   if (LHS->getType()->getPointerAddressSpace() != 0 ||
95       RHS->getType()->getPointerAddressSpace() != 0)
96     return nullptr;
97
98   // Both ptrtoint instructions must wrap global objects:
99   // - Only global variables are eligible for image relative relocations.
100   // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
101   // We expect __ImageBase to be a global variable without a section, externally
102   // defined.
103   //
104   // It should look something like this: @__ImageBase = external constant i8
105   if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
106       LHS->isThreadLocal() || RHS->isThreadLocal() ||
107       RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
108       cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
109     return nullptr;
110
111   return MCSymbolRefExpr::create(TM.getSymbol(LHS),
112                                  MCSymbolRefExpr::VK_COFF_IMGREL32,
113                                  getContext());
114 }
115
116 static std::string APIntToHexString(const APInt &AI) {
117   unsigned Width = (AI.getBitWidth() / 8) * 2;
118   std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
119   unsigned Size = HexString.size();
120   assert(Width >= Size && "hex string is too large!");
121   HexString.insert(HexString.begin(), Width - Size, '0');
122
123   return HexString;
124 }
125
126 static std::string scalarConstantToHexString(const Constant *C) {
127   Type *Ty = C->getType();
128   if (isa<UndefValue>(C)) {
129     return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
130   } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
131     return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
132   } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
133     return APIntToHexString(CI->getValue());
134   } else {
135     unsigned NumElements;
136     if (isa<VectorType>(Ty))
137       NumElements = Ty->getVectorNumElements();
138     else
139       NumElements = Ty->getArrayNumElements();
140     std::string HexString;
141     for (int I = NumElements - 1, E = -1; I != E; --I)
142       HexString += scalarConstantToHexString(C->getAggregateElement(I));
143     return HexString;
144   }
145 }
146
147 MCSection *X86WindowsTargetObjectFile::getSectionForConstant(
148     const DataLayout &DL, SectionKind Kind, const Constant *C,
149     unsigned &Align) const {
150   if (Kind.isMergeableConst() && C) {
151     const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
152                                      COFF::IMAGE_SCN_MEM_READ |
153                                      COFF::IMAGE_SCN_LNK_COMDAT;
154     std::string COMDATSymName;
155     if (Kind.isMergeableConst4()) {
156       if (Align <= 4) {
157         COMDATSymName = "__real@" + scalarConstantToHexString(C);
158         Align = 4;
159       }
160     } else if (Kind.isMergeableConst8()) {
161       if (Align <= 8) {
162         COMDATSymName = "__real@" + scalarConstantToHexString(C);
163         Align = 8;
164       }
165     } else if (Kind.isMergeableConst16()) {
166       if (Align <= 16) {
167         COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
168         Align = 16;
169       }
170     } else if (Kind.isMergeableConst32()) {
171       if (Align <= 32) {
172         COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
173         Align = 32;
174       }
175     }
176
177     if (!COMDATSymName.empty())
178       return getContext().getCOFFSection(".rdata", Characteristics, Kind,
179                                          COMDATSymName,
180                                          COFF::IMAGE_COMDAT_SELECT_ANY);
181   }
182
183   return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
184 }