blob: c86a3ee0f49e8c2da32fa1e468682d9f0a1e007f [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
Chris Lattnerb8666022009-09-16 01:46:41 +00002//
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
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +000010#include "X86TargetObjectFile.h"
David Majnemer8bce66b2014-07-14 22:57:27 +000011#include "llvm/ADT/StringExtras.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000012#include "llvm/IR/Mangler.h"
David Majnemerdee10572014-01-15 09:16:42 +000013#include "llvm/IR/Operator.h"
Chris Lattner3a78ce32009-09-18 20:22:52 +000014#include "llvm/MC/MCContext.h"
Daniel Dunbar55992562010-03-15 23:51:06 +000015#include "llvm/MC/MCExpr.h"
David Majnemer8bce66b2014-07-14 22:57:27 +000016#include "llvm/MC/MCSectionCOFF.h"
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +000017#include "llvm/MC/MCSectionELF.h"
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +000018#include "llvm/MC/MCValue.h"
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +000019#include "llvm/Support/Dwarf.h"
Rafael Espindoladaeafb42014-02-19 17:23:20 +000020#include "llvm/Target/TargetLowering.h"
Jakub Staszakab3d8782013-01-10 23:43:56 +000021
Chris Lattnerb8666022009-09-16 01:46:41 +000022using namespace llvm;
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +000023using namespace dwarf;
Chris Lattnerb8666022009-09-16 01:46:41 +000024
Rafael Espindola15b26692014-02-09 14:50:44 +000025const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
26 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +000027 const TargetMachine &TM, MachineModuleInfo *MMI,
28 MCStreamer &Streamer) const {
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +000029
Chris Lattnerb8666022009-09-16 01:46:41 +000030 // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
31 // is an indirect pc-relative reference.
Joerg Sonnenbergerfe543642014-04-30 22:40:27 +000032 if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000033 const MCSymbol *Sym = TM.getSymbol(GV, Mang);
Anton Korobeynikovc9adb6a2010-02-15 22:38:10 +000034 const MCExpr *Res =
Daniel Dunbar55992562010-03-15 23:51:06 +000035 MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
Anton Korobeynikovc9adb6a2010-02-15 22:38:10 +000036 const MCExpr *Four = MCConstantExpr::Create(4, getContext());
37 return MCBinaryExpr::CreateAdd(Res, Four, getContext());
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +000038 }
39
Rafael Espindola15b26692014-02-09 14:50:44 +000040 return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
Rafael Espindoladaeafb42014-02-19 17:23:20 +000041 GV, Encoding, Mang, TM, MMI, Streamer);
Chris Lattnerb8666022009-09-16 01:46:41 +000042}
43
Rafael Espindoladaeafb42014-02-19 17:23:20 +000044MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
45 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
46 MachineModuleInfo *MMI) const {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000047 return TM.getSymbol(GV, Mang);
Rafael Espindola08704342011-04-27 23:08:15 +000048}
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +000049
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +000050const MCExpr *X86_64MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +000051 const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
52 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +000053 // 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>.
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +000056 unsigned FinalOff = Offset+MV.getConstant()+4;
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +000057 const MCExpr *Res =
58 MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +000059 const MCExpr *Off = MCConstantExpr::Create(FinalOff, getContext());
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +000060 return MCBinaryExpr::CreateAdd(Res, Off, getContext());
61}
62
Paul Robinson06a8eb82015-03-03 21:01:27 +000063const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
64 const MCSymbol *Sym) const {
65 return MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
66}
67
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +000068void
69X86LinuxTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) {
70 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
71 InitializeELF(TM.Options.UseInitArray);
72}
David Blaikie1b01ae82013-07-01 21:45:25 +000073
Rafael Espindoladaeafb42014-02-19 17:23:20 +000074const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol(
75 const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const {
David Majnemerdee10572014-01-15 09:16:42 +000076 // We are looking for the difference of two symbols, need a subtraction
77 // operation.
78 const SubOperator *Sub = dyn_cast<SubOperator>(CE);
79 if (!Sub)
Craig Topper062a2ba2014-04-25 05:30:21 +000080 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +000081
82 // Symbols must first be numbers before we can subtract them, we need to see a
83 // ptrtoint on both subtraction operands.
84 const PtrToIntOperator *SubLHS =
85 dyn_cast<PtrToIntOperator>(Sub->getOperand(0));
86 const PtrToIntOperator *SubRHS =
87 dyn_cast<PtrToIntOperator>(Sub->getOperand(1));
88 if (!SubLHS || !SubRHS)
Craig Topper062a2ba2014-04-25 05:30:21 +000089 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +000090
91 // Our symbols should exist in address space zero, cowardly no-op if
92 // otherwise.
93 if (SubLHS->getPointerAddressSpace() != 0 ||
94 SubRHS->getPointerAddressSpace() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +000095 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +000096
David Majnemerb61f4e42015-03-06 08:11:32 +000097 // Both ptrtoint instructions must wrap global objects:
David Majnemerdee10572014-01-15 09:16:42 +000098 // - Only global variables are eligible for image relative relocations.
David Majnemerb61f4e42015-03-06 08:11:32 +000099 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
100 const auto *GOLHS = dyn_cast<GlobalObject>(SubLHS->getPointerOperand());
101 const auto *GVRHS = dyn_cast<GlobalVariable>(SubRHS->getPointerOperand());
102 if (!GOLHS || !GVRHS)
Craig Topper062a2ba2014-04-25 05:30:21 +0000103 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +0000104
105 // We expect __ImageBase to be a global variable without a section, externally
106 // defined.
107 //
108 // It should look something like this: @__ImageBase = external constant i8
109 if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" ||
110 !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() ||
111 GVRHS->hasSection())
Craig Topper062a2ba2014-04-25 05:30:21 +0000112 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +0000113
114 // An image-relative, thread-local, symbol makes no sense.
David Majnemerb61f4e42015-03-06 08:11:32 +0000115 if (GOLHS->isThreadLocal())
Craig Topper062a2ba2014-04-25 05:30:21 +0000116 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +0000117
David Majnemerb61f4e42015-03-06 08:11:32 +0000118 return MCSymbolRefExpr::Create(TM.getSymbol(GOLHS, Mang),
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000119 MCSymbolRefExpr::VK_COFF_IMGREL32,
120 getContext());
David Majnemerdee10572014-01-15 09:16:42 +0000121}
David Majnemer8bce66b2014-07-14 22:57:27 +0000122
David Majnemer4e3ccc02014-07-15 02:34:12 +0000123static std::string APIntToHexString(const APInt &AI) {
124 unsigned Width = (AI.getBitWidth() / 8) * 2;
David Majnemer8bce66b2014-07-14 22:57:27 +0000125 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
126 unsigned Size = HexString.size();
127 assert(Width >= Size && "hex string is too large!");
128 HexString.insert(HexString.begin(), Width - Size, '0');
129
130 return HexString;
131}
132
133
134static std::string scalarConstantToHexString(const Constant *C) {
135 Type *Ty = C->getType();
David Majnemer4e3ccc02014-07-15 02:34:12 +0000136 APInt AI;
137 if (isa<UndefValue>(C)) {
138 AI = APInt(Ty->getPrimitiveSizeInBits(), /*val=*/0);
139 } else if (Ty->isFloatTy() || Ty->isDoubleTy()) {
David Majnemer8bce66b2014-07-14 22:57:27 +0000140 const auto *CFP = cast<ConstantFP>(C);
David Majnemer4e3ccc02014-07-15 02:34:12 +0000141 AI = CFP->getValueAPF().bitcastToAPInt();
142 } else if (Ty->isIntegerTy()) {
David Majnemer8bce66b2014-07-14 22:57:27 +0000143 const auto *CI = cast<ConstantInt>(C);
David Majnemer4e3ccc02014-07-15 02:34:12 +0000144 AI = CI->getValue();
145 } else {
146 llvm_unreachable("unexpected constant pool element type!");
David Majnemer8bce66b2014-07-14 22:57:27 +0000147 }
David Majnemer4e3ccc02014-07-15 02:34:12 +0000148 return APIntToHexString(AI);
David Majnemer8bce66b2014-07-14 22:57:27 +0000149}
150
151const MCSection *
152X86WindowsTargetObjectFile::getSectionForConstant(SectionKind Kind,
153 const Constant *C) const {
154 if (Kind.isReadOnly()) {
155 if (C) {
156 Type *Ty = C->getType();
157 SmallString<32> COMDATSymName;
158 if (Ty->isFloatTy() || Ty->isDoubleTy()) {
159 COMDATSymName = "__real@";
160 COMDATSymName += scalarConstantToHexString(C);
161 } else if (const auto *VTy = dyn_cast<VectorType>(Ty)) {
162 uint64_t NumBits = VTy->getBitWidth();
163 if (NumBits == 128 || NumBits == 256) {
David Majnemer8bce66b2014-07-14 22:57:27 +0000164 COMDATSymName = NumBits == 128 ? "__xmm@" : "__ymm@";
David Majnemer3821ff02014-07-15 23:01:10 +0000165 for (int I = VTy->getNumElements() - 1, E = -1; I != E; --I)
166 COMDATSymName +=
167 scalarConstantToHexString(C->getAggregateElement(I));
David Majnemer8bce66b2014-07-14 22:57:27 +0000168 }
169 }
170 if (!COMDATSymName.empty()) {
171 unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
172 COFF::IMAGE_SCN_MEM_READ |
173 COFF::IMAGE_SCN_LNK_COMDAT;
174 return getContext().getCOFFSection(".rdata", Characteristics, Kind,
175 COMDATSymName,
176 COFF::IMAGE_COMDAT_SELECT_ANY);
177 }
178 }
179 }
180
181 return TargetLoweringObjectFile::getSectionForConstant(Kind, C);
182}