blob: 6bf45c37e38b216eed34b20afedc1f1b26781851 [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
Derek Schuff072f93f2015-03-11 16:16:09 +000069X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
70 const TargetMachine &TM) {
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +000071 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
72 InitializeELF(TM.Options.UseInitArray);
73}
David Blaikie1b01ae82013-07-01 21:45:25 +000074
Rafael Espindoladaeafb42014-02-19 17:23:20 +000075const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol(
76 const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const {
David Majnemerdee10572014-01-15 09:16:42 +000077 // 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)
Craig Topper062a2ba2014-04-25 05:30:21 +000081 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +000082
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)
Craig Topper062a2ba2014-04-25 05:30:21 +000090 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +000091
92 // Our symbols should exist in address space zero, cowardly no-op if
93 // otherwise.
94 if (SubLHS->getPointerAddressSpace() != 0 ||
95 SubRHS->getPointerAddressSpace() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +000096 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +000097
David Majnemerb61f4e42015-03-06 08:11:32 +000098 // Both ptrtoint instructions must wrap global objects:
David Majnemerdee10572014-01-15 09:16:42 +000099 // - Only global variables are eligible for image relative relocations.
David Majnemerb61f4e42015-03-06 08:11:32 +0000100 // - 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)
Craig Topper062a2ba2014-04-25 05:30:21 +0000104 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +0000105
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())
Craig Topper062a2ba2014-04-25 05:30:21 +0000113 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +0000114
115 // An image-relative, thread-local, symbol makes no sense.
David Majnemerb61f4e42015-03-06 08:11:32 +0000116 if (GOLHS->isThreadLocal())
Craig Topper062a2ba2014-04-25 05:30:21 +0000117 return nullptr;
David Majnemerdee10572014-01-15 09:16:42 +0000118
David Majnemerb61f4e42015-03-06 08:11:32 +0000119 return MCSymbolRefExpr::Create(TM.getSymbol(GOLHS, Mang),
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000120 MCSymbolRefExpr::VK_COFF_IMGREL32,
121 getContext());
David Majnemerdee10572014-01-15 09:16:42 +0000122}
David Majnemer8bce66b2014-07-14 22:57:27 +0000123
David Majnemer4e3ccc02014-07-15 02:34:12 +0000124static std::string APIntToHexString(const APInt &AI) {
125 unsigned Width = (AI.getBitWidth() / 8) * 2;
David Majnemer8bce66b2014-07-14 22:57:27 +0000126 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
135static std::string scalarConstantToHexString(const Constant *C) {
136 Type *Ty = C->getType();
David Majnemer4e3ccc02014-07-15 02:34:12 +0000137 APInt AI;
138 if (isa<UndefValue>(C)) {
139 AI = APInt(Ty->getPrimitiveSizeInBits(), /*val=*/0);
140 } else if (Ty->isFloatTy() || Ty->isDoubleTy()) {
David Majnemer8bce66b2014-07-14 22:57:27 +0000141 const auto *CFP = cast<ConstantFP>(C);
David Majnemer4e3ccc02014-07-15 02:34:12 +0000142 AI = CFP->getValueAPF().bitcastToAPInt();
143 } else if (Ty->isIntegerTy()) {
David Majnemer8bce66b2014-07-14 22:57:27 +0000144 const auto *CI = cast<ConstantInt>(C);
David Majnemer4e3ccc02014-07-15 02:34:12 +0000145 AI = CI->getValue();
146 } else {
147 llvm_unreachable("unexpected constant pool element type!");
David Majnemer8bce66b2014-07-14 22:57:27 +0000148 }
David Majnemer4e3ccc02014-07-15 02:34:12 +0000149 return APIntToHexString(AI);
David Majnemer8bce66b2014-07-14 22:57:27 +0000150}
151
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000152MCSection *
David Majnemer8bce66b2014-07-14 22:57:27 +0000153X86WindowsTargetObjectFile::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) {
David Majnemer8bce66b2014-07-14 22:57:27 +0000165 COMDATSymName = NumBits == 128 ? "__xmm@" : "__ymm@";
David Majnemer3821ff02014-07-15 23:01:10 +0000166 for (int I = VTy->getNumElements() - 1, E = -1; I != E; --I)
167 COMDATSymName +=
168 scalarConstantToHexString(C->getAggregateElement(I));
David Majnemer8bce66b2014-07-14 22:57:27 +0000169 }
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}