1f4a2713aSLionel Sambuc //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "X86TargetObjectFile.h"
11*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringExtras.h"
12*0a6a1f1dSLionel Sambuc #include "llvm/IR/Mangler.h"
13*0a6a1f1dSLionel Sambuc #include "llvm/IR/Operator.h"
14f4a2713aSLionel Sambuc #include "llvm/MC/MCContext.h"
15f4a2713aSLionel Sambuc #include "llvm/MC/MCExpr.h"
16*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCSectionCOFF.h"
17f4a2713aSLionel Sambuc #include "llvm/MC/MCSectionELF.h"
18f4a2713aSLionel Sambuc #include "llvm/Support/Dwarf.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetLowering.h"
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc using namespace llvm;
22f4a2713aSLionel Sambuc using namespace dwarf;
23f4a2713aSLionel Sambuc 
getTTypeGlobalReference(const GlobalValue * GV,unsigned Encoding,Mangler & Mang,const TargetMachine & TM,MachineModuleInfo * MMI,MCStreamer & Streamer) const24*0a6a1f1dSLionel Sambuc const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
25*0a6a1f1dSLionel Sambuc     const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
26*0a6a1f1dSLionel Sambuc     const TargetMachine &TM, MachineModuleInfo *MMI,
27f4a2713aSLionel Sambuc     MCStreamer &Streamer) const {
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc   // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
30f4a2713aSLionel Sambuc   // is an indirect pc-relative reference.
31*0a6a1f1dSLionel Sambuc   if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
32*0a6a1f1dSLionel Sambuc     const MCSymbol *Sym = TM.getSymbol(GV, Mang);
33f4a2713aSLionel Sambuc     const MCExpr *Res =
34f4a2713aSLionel Sambuc       MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
35f4a2713aSLionel Sambuc     const MCExpr *Four = MCConstantExpr::Create(4, getContext());
36f4a2713aSLionel Sambuc     return MCBinaryExpr::CreateAdd(Res, Four, getContext());
37f4a2713aSLionel Sambuc   }
38f4a2713aSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc   return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
40*0a6a1f1dSLionel Sambuc       GV, Encoding, Mang, TM, MMI, Streamer);
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc 
getCFIPersonalitySymbol(const GlobalValue * GV,Mangler & Mang,const TargetMachine & TM,MachineModuleInfo * MMI) const43*0a6a1f1dSLionel Sambuc MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
44*0a6a1f1dSLionel Sambuc     const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
45f4a2713aSLionel Sambuc     MachineModuleInfo *MMI) const {
46*0a6a1f1dSLionel Sambuc   return TM.getSymbol(GV, Mang);
47f4a2713aSLionel Sambuc }
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc void
Initialize(MCContext & Ctx,const TargetMachine & TM)50f4a2713aSLionel Sambuc X86LinuxTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) {
51f4a2713aSLionel Sambuc   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
52f4a2713aSLionel Sambuc   InitializeELF(TM.Options.UseInitArray);
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc const MCExpr *
getDebugThreadLocalSymbol(const MCSymbol * Sym) const56f4a2713aSLionel Sambuc X86LinuxTargetObjectFile::getDebugThreadLocalSymbol(
57f4a2713aSLionel Sambuc     const MCSymbol *Sym) const {
58f4a2713aSLionel Sambuc   return MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
59f4a2713aSLionel Sambuc }
60*0a6a1f1dSLionel Sambuc 
getExecutableRelativeSymbol(const ConstantExpr * CE,Mangler & Mang,const TargetMachine & TM) const61*0a6a1f1dSLionel Sambuc const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol(
62*0a6a1f1dSLionel Sambuc     const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const {
63*0a6a1f1dSLionel Sambuc   // We are looking for the difference of two symbols, need a subtraction
64*0a6a1f1dSLionel Sambuc   // operation.
65*0a6a1f1dSLionel Sambuc   const SubOperator *Sub = dyn_cast<SubOperator>(CE);
66*0a6a1f1dSLionel Sambuc   if (!Sub)
67*0a6a1f1dSLionel Sambuc     return nullptr;
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc   // Symbols must first be numbers before we can subtract them, we need to see a
70*0a6a1f1dSLionel Sambuc   // ptrtoint on both subtraction operands.
71*0a6a1f1dSLionel Sambuc   const PtrToIntOperator *SubLHS =
72*0a6a1f1dSLionel Sambuc       dyn_cast<PtrToIntOperator>(Sub->getOperand(0));
73*0a6a1f1dSLionel Sambuc   const PtrToIntOperator *SubRHS =
74*0a6a1f1dSLionel Sambuc       dyn_cast<PtrToIntOperator>(Sub->getOperand(1));
75*0a6a1f1dSLionel Sambuc   if (!SubLHS || !SubRHS)
76*0a6a1f1dSLionel Sambuc     return nullptr;
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc   // Our symbols should exist in address space zero, cowardly no-op if
79*0a6a1f1dSLionel Sambuc   // otherwise.
80*0a6a1f1dSLionel Sambuc   if (SubLHS->getPointerAddressSpace() != 0 ||
81*0a6a1f1dSLionel Sambuc       SubRHS->getPointerAddressSpace() != 0)
82*0a6a1f1dSLionel Sambuc     return nullptr;
83*0a6a1f1dSLionel Sambuc 
84*0a6a1f1dSLionel Sambuc   // Both ptrtoint instructions must wrap global variables:
85*0a6a1f1dSLionel Sambuc   // - Only global variables are eligible for image relative relocations.
86*0a6a1f1dSLionel Sambuc   // - The subtrahend refers to the special symbol __ImageBase, a global.
87*0a6a1f1dSLionel Sambuc   const GlobalVariable *GVLHS =
88*0a6a1f1dSLionel Sambuc       dyn_cast<GlobalVariable>(SubLHS->getPointerOperand());
89*0a6a1f1dSLionel Sambuc   const GlobalVariable *GVRHS =
90*0a6a1f1dSLionel Sambuc       dyn_cast<GlobalVariable>(SubRHS->getPointerOperand());
91*0a6a1f1dSLionel Sambuc   if (!GVLHS || !GVRHS)
92*0a6a1f1dSLionel Sambuc     return nullptr;
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc   // We expect __ImageBase to be a global variable without a section, externally
95*0a6a1f1dSLionel Sambuc   // defined.
96*0a6a1f1dSLionel Sambuc   //
97*0a6a1f1dSLionel Sambuc   // It should look something like this: @__ImageBase = external constant i8
98*0a6a1f1dSLionel Sambuc   if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" ||
99*0a6a1f1dSLionel Sambuc       !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() ||
100*0a6a1f1dSLionel Sambuc       GVRHS->hasSection())
101*0a6a1f1dSLionel Sambuc     return nullptr;
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc   // An image-relative, thread-local, symbol makes no sense.
104*0a6a1f1dSLionel Sambuc   if (GVLHS->isThreadLocal())
105*0a6a1f1dSLionel Sambuc     return nullptr;
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc   return MCSymbolRefExpr::Create(TM.getSymbol(GVLHS, Mang),
108*0a6a1f1dSLionel Sambuc                                  MCSymbolRefExpr::VK_COFF_IMGREL32,
109*0a6a1f1dSLionel Sambuc                                  getContext());
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc 
APIntToHexString(const APInt & AI)112*0a6a1f1dSLionel Sambuc static std::string APIntToHexString(const APInt &AI) {
113*0a6a1f1dSLionel Sambuc   unsigned Width = (AI.getBitWidth() / 8) * 2;
114*0a6a1f1dSLionel Sambuc   std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
115*0a6a1f1dSLionel Sambuc   unsigned Size = HexString.size();
116*0a6a1f1dSLionel Sambuc   assert(Width >= Size && "hex string is too large!");
117*0a6a1f1dSLionel Sambuc   HexString.insert(HexString.begin(), Width - Size, '0');
118*0a6a1f1dSLionel Sambuc 
119*0a6a1f1dSLionel Sambuc   return HexString;
120*0a6a1f1dSLionel Sambuc }
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc 
scalarConstantToHexString(const Constant * C)123*0a6a1f1dSLionel Sambuc static std::string scalarConstantToHexString(const Constant *C) {
124*0a6a1f1dSLionel Sambuc   Type *Ty = C->getType();
125*0a6a1f1dSLionel Sambuc   APInt AI;
126*0a6a1f1dSLionel Sambuc   if (isa<UndefValue>(C)) {
127*0a6a1f1dSLionel Sambuc     AI = APInt(Ty->getPrimitiveSizeInBits(), /*val=*/0);
128*0a6a1f1dSLionel Sambuc   } else if (Ty->isFloatTy() || Ty->isDoubleTy()) {
129*0a6a1f1dSLionel Sambuc     const auto *CFP = cast<ConstantFP>(C);
130*0a6a1f1dSLionel Sambuc     AI = CFP->getValueAPF().bitcastToAPInt();
131*0a6a1f1dSLionel Sambuc   } else if (Ty->isIntegerTy()) {
132*0a6a1f1dSLionel Sambuc     const auto *CI = cast<ConstantInt>(C);
133*0a6a1f1dSLionel Sambuc     AI = CI->getValue();
134*0a6a1f1dSLionel Sambuc   } else {
135*0a6a1f1dSLionel Sambuc     llvm_unreachable("unexpected constant pool element type!");
136*0a6a1f1dSLionel Sambuc   }
137*0a6a1f1dSLionel Sambuc   return APIntToHexString(AI);
138*0a6a1f1dSLionel Sambuc }
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc const MCSection *
getSectionForConstant(SectionKind Kind,const Constant * C) const141*0a6a1f1dSLionel Sambuc X86WindowsTargetObjectFile::getSectionForConstant(SectionKind Kind,
142*0a6a1f1dSLionel Sambuc                                                   const Constant *C) const {
143*0a6a1f1dSLionel Sambuc   if (Kind.isReadOnly()) {
144*0a6a1f1dSLionel Sambuc     if (C) {
145*0a6a1f1dSLionel Sambuc       Type *Ty = C->getType();
146*0a6a1f1dSLionel Sambuc       SmallString<32> COMDATSymName;
147*0a6a1f1dSLionel Sambuc       if (Ty->isFloatTy() || Ty->isDoubleTy()) {
148*0a6a1f1dSLionel Sambuc         COMDATSymName = "__real@";
149*0a6a1f1dSLionel Sambuc         COMDATSymName += scalarConstantToHexString(C);
150*0a6a1f1dSLionel Sambuc       } else if (const auto *VTy = dyn_cast<VectorType>(Ty)) {
151*0a6a1f1dSLionel Sambuc         uint64_t NumBits = VTy->getBitWidth();
152*0a6a1f1dSLionel Sambuc         if (NumBits == 128 || NumBits == 256) {
153*0a6a1f1dSLionel Sambuc           COMDATSymName = NumBits == 128 ? "__xmm@" : "__ymm@";
154*0a6a1f1dSLionel Sambuc           for (int I = VTy->getNumElements() - 1, E = -1; I != E; --I)
155*0a6a1f1dSLionel Sambuc             COMDATSymName +=
156*0a6a1f1dSLionel Sambuc                 scalarConstantToHexString(C->getAggregateElement(I));
157*0a6a1f1dSLionel Sambuc         }
158*0a6a1f1dSLionel Sambuc       }
159*0a6a1f1dSLionel Sambuc       if (!COMDATSymName.empty()) {
160*0a6a1f1dSLionel Sambuc         unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
161*0a6a1f1dSLionel Sambuc                                    COFF::IMAGE_SCN_MEM_READ |
162*0a6a1f1dSLionel Sambuc                                    COFF::IMAGE_SCN_LNK_COMDAT;
163*0a6a1f1dSLionel Sambuc         return getContext().getCOFFSection(".rdata", Characteristics, Kind,
164*0a6a1f1dSLionel Sambuc                                            COMDATSymName,
165*0a6a1f1dSLionel Sambuc                                            COFF::IMAGE_COMDAT_SELECT_ANY);
166*0a6a1f1dSLionel Sambuc       }
167*0a6a1f1dSLionel Sambuc     }
168*0a6a1f1dSLionel Sambuc   }
169*0a6a1f1dSLionel Sambuc 
170*0a6a1f1dSLionel Sambuc   return TargetLoweringObjectFile::getSectionForConstant(Kind, C);
171*0a6a1f1dSLionel Sambuc }
172