Move alignment from MCSectionData to MCSection.
[oota-llvm.git] / 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/Dwarf.h"
20 #include "llvm/Target/TargetLowering.h"
21
22 using namespace llvm;
23 using namespace dwarf;
24
25 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
26     const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
27     const TargetMachine &TM, MachineModuleInfo *MMI,
28     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, Mang);
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, Mang, TM, MMI, Streamer);
42 }
43
44 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
45     const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
46     MachineModuleInfo *MMI) const {
47   return TM.getSymbol(GV, Mang);
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 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
70                                          const TargetMachine &TM) {
71   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
72   InitializeELF(TM.Options.UseInitArray);
73 }
74
75 const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol(
76     const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const {
77   // We are looking for the difference of two symbols, need a subtraction
78   // operation.
79   const SubOperator *Sub = dyn_cast<SubOperator>(CE);
80   if (!Sub)
81     return nullptr;
82
83   // Symbols must first be numbers before we can subtract them, we need to see a
84   // ptrtoint on both subtraction operands.
85   const PtrToIntOperator *SubLHS =
86       dyn_cast<PtrToIntOperator>(Sub->getOperand(0));
87   const PtrToIntOperator *SubRHS =
88       dyn_cast<PtrToIntOperator>(Sub->getOperand(1));
89   if (!SubLHS || !SubRHS)
90     return nullptr;
91
92   // Our symbols should exist in address space zero, cowardly no-op if
93   // otherwise.
94   if (SubLHS->getPointerAddressSpace() != 0 ||
95       SubRHS->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   const auto *GOLHS = dyn_cast<GlobalObject>(SubLHS->getPointerOperand());
102   const auto *GVRHS = dyn_cast<GlobalVariable>(SubRHS->getPointerOperand());
103   if (!GOLHS || !GVRHS)
104     return nullptr;
105
106   // We expect __ImageBase to be a global variable without a section, externally
107   // defined.
108   //
109   // It should look something like this: @__ImageBase = external constant i8
110   if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" ||
111       !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() ||
112       GVRHS->hasSection())
113     return nullptr;
114
115   // An image-relative, thread-local, symbol makes no sense.
116   if (GOLHS->isThreadLocal())
117     return nullptr;
118
119   return MCSymbolRefExpr::Create(TM.getSymbol(GOLHS, Mang),
120                                  MCSymbolRefExpr::VK_COFF_IMGREL32,
121                                  getContext());
122 }
123
124 static std::string APIntToHexString(const APInt &AI) {
125   unsigned Width = (AI.getBitWidth() / 8) * 2;
126   std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
127   unsigned Size = HexString.size();
128   assert(Width >= Size && "hex string is too large!");
129   HexString.insert(HexString.begin(), Width - Size, '0');
130
131   return HexString;
132 }
133
134
135 static std::string scalarConstantToHexString(const Constant *C) {
136   Type *Ty = C->getType();
137   APInt AI;
138   if (isa<UndefValue>(C)) {
139     AI = APInt(Ty->getPrimitiveSizeInBits(), /*val=*/0);
140   } else if (Ty->isFloatTy() || Ty->isDoubleTy()) {
141     const auto *CFP = cast<ConstantFP>(C);
142     AI = CFP->getValueAPF().bitcastToAPInt();
143   } else if (Ty->isIntegerTy()) {
144     const auto *CI = cast<ConstantInt>(C);
145     AI = CI->getValue();
146   } else {
147     llvm_unreachable("unexpected constant pool element type!");
148   }
149   return APIntToHexString(AI);
150 }
151
152 MCSection *
153 X86WindowsTargetObjectFile::getSectionForConstant(SectionKind Kind,
154                                                   const Constant *C) const {
155   if (Kind.isReadOnly()) {
156     if (C) {
157       Type *Ty = C->getType();
158       SmallString<32> COMDATSymName;
159       if (Ty->isFloatTy() || Ty->isDoubleTy()) {
160         COMDATSymName = "__real@";
161         COMDATSymName += scalarConstantToHexString(C);
162       } else if (const auto *VTy = dyn_cast<VectorType>(Ty)) {
163         uint64_t NumBits = VTy->getBitWidth();
164         if (NumBits == 128 || NumBits == 256) {
165           COMDATSymName = NumBits == 128 ? "__xmm@" : "__ymm@";
166           for (int I = VTy->getNumElements() - 1, E = -1; I != E; --I)
167             COMDATSymName +=
168                 scalarConstantToHexString(C->getAggregateElement(I));
169         }
170       }
171       if (!COMDATSymName.empty()) {
172         unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
173                                    COFF::IMAGE_SCN_MEM_READ |
174                                    COFF::IMAGE_SCN_LNK_COMDAT;
175         return getContext().getCOFFSection(".rdata", Characteristics, Kind,
176                                            COMDATSymName,
177                                            COFF::IMAGE_COMDAT_SELECT_ANY);
178       }
179     }
180   }
181
182   return TargetLoweringObjectFile::getSectionForConstant(Kind, C);
183 }