1 //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/Support/Dwarf.h"
19 #include "llvm/Target/TargetLowering.h"
22 using namespace dwarf;
24 X86_64MachoTargetObjectFile::X86_64MachoTargetObjectFile()
25 : TargetLoweringObjectFileMachO() {
26 SupportIndirectSymViaGOTPCRel = true;
29 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
30 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
31 const TargetMachine &TM, MachineModuleInfo *MMI,
32 MCStreamer &Streamer) const {
34 // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
35 // is an indirect pc-relative reference.
36 if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
37 const MCSymbol *Sym = TM.getSymbol(GV, Mang);
39 MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
40 const MCExpr *Four = MCConstantExpr::Create(4, getContext());
41 return MCBinaryExpr::CreateAdd(Res, Four, getContext());
44 return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
45 GV, Encoding, Mang, TM, MMI, Streamer);
48 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
49 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
50 MachineModuleInfo *MMI) const {
51 return TM.getSymbol(GV, Mang);
54 const MCExpr *X86_64MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
55 const MCSymbol *Sym, int64_t Offset) const {
56 // On Darwin/X86-64, we need to use foo@GOTPCREL+4 to access the got entry
57 // from a data section. In case there's an additional offset, then use
58 // foo@GOTPCREL+4+<offset>.
60 MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
61 const MCExpr *Off = MCConstantExpr::Create(Offset+4, getContext());
62 return MCBinaryExpr::CreateAdd(Res, Off, getContext());
66 X86LinuxTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) {
67 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
68 InitializeELF(TM.Options.UseInitArray);
72 X86LinuxTargetObjectFile::getDebugThreadLocalSymbol(
73 const MCSymbol *Sym) const {
74 return MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
77 const MCExpr *PS4TargetObjectFile::getDebugThreadLocalSymbol(
78 const MCSymbol *Sym) const {
79 return MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
82 const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol(
83 const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const {
84 // We are looking for the difference of two symbols, need a subtraction
86 const SubOperator *Sub = dyn_cast<SubOperator>(CE);
90 // Symbols must first be numbers before we can subtract them, we need to see a
91 // ptrtoint on both subtraction operands.
92 const PtrToIntOperator *SubLHS =
93 dyn_cast<PtrToIntOperator>(Sub->getOperand(0));
94 const PtrToIntOperator *SubRHS =
95 dyn_cast<PtrToIntOperator>(Sub->getOperand(1));
96 if (!SubLHS || !SubRHS)
99 // Our symbols should exist in address space zero, cowardly no-op if
101 if (SubLHS->getPointerAddressSpace() != 0 ||
102 SubRHS->getPointerAddressSpace() != 0)
105 // Both ptrtoint instructions must wrap global variables:
106 // - Only global variables are eligible for image relative relocations.
107 // - The subtrahend refers to the special symbol __ImageBase, a global.
108 const GlobalVariable *GVLHS =
109 dyn_cast<GlobalVariable>(SubLHS->getPointerOperand());
110 const GlobalVariable *GVRHS =
111 dyn_cast<GlobalVariable>(SubRHS->getPointerOperand());
112 if (!GVLHS || !GVRHS)
115 // We expect __ImageBase to be a global variable without a section, externally
118 // It should look something like this: @__ImageBase = external constant i8
119 if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" ||
120 !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() ||
124 // An image-relative, thread-local, symbol makes no sense.
125 if (GVLHS->isThreadLocal())
128 return MCSymbolRefExpr::Create(TM.getSymbol(GVLHS, Mang),
129 MCSymbolRefExpr::VK_COFF_IMGREL32,
133 static std::string APIntToHexString(const APInt &AI) {
134 unsigned Width = (AI.getBitWidth() / 8) * 2;
135 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
136 unsigned Size = HexString.size();
137 assert(Width >= Size && "hex string is too large!");
138 HexString.insert(HexString.begin(), Width - Size, '0');
144 static std::string scalarConstantToHexString(const Constant *C) {
145 Type *Ty = C->getType();
147 if (isa<UndefValue>(C)) {
148 AI = APInt(Ty->getPrimitiveSizeInBits(), /*val=*/0);
149 } else if (Ty->isFloatTy() || Ty->isDoubleTy()) {
150 const auto *CFP = cast<ConstantFP>(C);
151 AI = CFP->getValueAPF().bitcastToAPInt();
152 } else if (Ty->isIntegerTy()) {
153 const auto *CI = cast<ConstantInt>(C);
156 llvm_unreachable("unexpected constant pool element type!");
158 return APIntToHexString(AI);
162 X86WindowsTargetObjectFile::getSectionForConstant(SectionKind Kind,
163 const Constant *C) const {
164 if (Kind.isReadOnly()) {
166 Type *Ty = C->getType();
167 SmallString<32> COMDATSymName;
168 if (Ty->isFloatTy() || Ty->isDoubleTy()) {
169 COMDATSymName = "__real@";
170 COMDATSymName += scalarConstantToHexString(C);
171 } else if (const auto *VTy = dyn_cast<VectorType>(Ty)) {
172 uint64_t NumBits = VTy->getBitWidth();
173 if (NumBits == 128 || NumBits == 256) {
174 COMDATSymName = NumBits == 128 ? "__xmm@" : "__ymm@";
175 for (int I = VTy->getNumElements() - 1, E = -1; I != E; --I)
177 scalarConstantToHexString(C->getAggregateElement(I));
180 if (!COMDATSymName.empty()) {
181 unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
182 COFF::IMAGE_SCN_MEM_READ |
183 COFF::IMAGE_SCN_LNK_COMDAT;
184 return getContext().getCOFFSection(".rdata", Characteristics, Kind,
186 COFF::IMAGE_COMDAT_SELECT_ANY);
191 return TargetLoweringObjectFile::getSectionForConstant(Kind, C);