xref: /llvm-project/llvm/lib/Target/X86/X86TargetObjectFile.cpp (revision a9f85d68ccabdcbb06c4f506c0fe6c992d45481f)
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, getMangler());
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, getMangler());
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 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
77                                          const TargetMachine &TM) {
78   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
79   InitializeELF(TM.Options.UseInitArray);
80 }
81 
82 const MCExpr *X86WindowsTargetObjectFile::lowerRelativeReference(
83     const GlobalValue *LHS, const GlobalValue *RHS,
84     const TargetMachine &TM) const {
85   // Our symbols should exist in address space zero, cowardly no-op if
86   // otherwise.
87   if (LHS->getType()->getPointerAddressSpace() != 0 ||
88       RHS->getType()->getPointerAddressSpace() != 0)
89     return nullptr;
90 
91   // Both ptrtoint instructions must wrap global objects:
92   // - Only global variables are eligible for image relative relocations.
93   // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
94   // We expect __ImageBase to be a global variable without a section, externally
95   // defined.
96   //
97   // It should look something like this: @__ImageBase = external constant i8
98   if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
99       LHS->isThreadLocal() || RHS->isThreadLocal() ||
100       RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
101       cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
102     return nullptr;
103 
104   return MCSymbolRefExpr::create(TM.getSymbol(LHS, getMangler()),
105                                  MCSymbolRefExpr::VK_COFF_IMGREL32,
106                                  getContext());
107 }
108 
109 static std::string APIntToHexString(const APInt &AI) {
110   unsigned Width = (AI.getBitWidth() / 8) * 2;
111   std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
112   unsigned Size = HexString.size();
113   assert(Width >= Size && "hex string is too large!");
114   HexString.insert(HexString.begin(), Width - Size, '0');
115 
116   return HexString;
117 }
118 
119 static std::string scalarConstantToHexString(const Constant *C) {
120   Type *Ty = C->getType();
121   if (isa<UndefValue>(C)) {
122     return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
123   } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
124     return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
125   } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
126     return APIntToHexString(CI->getValue());
127   } else {
128     unsigned NumElements;
129     if (isa<VectorType>(Ty))
130       NumElements = Ty->getVectorNumElements();
131     else
132       NumElements = Ty->getArrayNumElements();
133     std::string HexString;
134     for (int I = NumElements - 1, E = -1; I != E; --I)
135       HexString += scalarConstantToHexString(C->getAggregateElement(I));
136     return HexString;
137   }
138 }
139 
140 MCSection *X86WindowsTargetObjectFile::getSectionForConstant(
141     const DataLayout &DL, SectionKind Kind, const Constant *C,
142     unsigned &Align) const {
143   if (Kind.isMergeableConst() && C) {
144     const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
145                                      COFF::IMAGE_SCN_MEM_READ |
146                                      COFF::IMAGE_SCN_LNK_COMDAT;
147     std::string COMDATSymName;
148     if (Kind.isMergeableConst4()) {
149       if (Align <= 4) {
150         COMDATSymName = "__real@" + scalarConstantToHexString(C);
151         Align = 4;
152       }
153     } else if (Kind.isMergeableConst8()) {
154       if (Align <= 8) {
155         COMDATSymName = "__real@" + scalarConstantToHexString(C);
156         Align = 8;
157       }
158     } else if (Kind.isMergeableConst16()) {
159       if (Align <= 16) {
160         COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
161         Align = 16;
162       }
163     } else if (Kind.isMergeableConst32()) {
164       if (Align <= 32) {
165         COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
166         Align = 32;
167       }
168     }
169 
170     if (!COMDATSymName.empty())
171       return getContext().getCOFFSection(".rdata", Characteristics, Kind,
172                                          COMDATSymName,
173                                          COFF::IMAGE_COMDAT_SELECT_ANY);
174   }
175 
176   return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
177 }
178