blob: f7ba62ff2315a6e0f8883cc9a673449af7e8b9e6 [file] [log] [blame]
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001//===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File 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// This file implements classes used to handle lowerings specific to common
11// object file formats.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/ADT/Triple.h"
19#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/GlobalVariable.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000025#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Module.h"
Peter Collingbourne94d77862015-11-03 23:40:03 +000027#include "llvm/MC/MCAsmInfo.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000028#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCExpr.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000030#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MCSectionELF.h"
32#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolaa83b1772011-04-16 03:51:21 +000033#include "llvm/MC/MCStreamer.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000034#include "llvm/MC/MCSymbolELF.h"
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +000035#include "llvm/MC/MCValue.h"
Xinliang David Li5f04f922016-01-14 18:09:45 +000036#include "llvm/ProfileData/InstrProf.h"
Reid Kleckner1f13d472015-09-03 16:41:50 +000037#include "llvm/Support/COFF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000038#include "llvm/Support/Dwarf.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000039#include "llvm/Support/ELF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000040#include "llvm/Support/ErrorHandling.h"
41#include "llvm/Support/raw_ostream.h"
Rafael Espindoladaeafb42014-02-19 17:23:20 +000042#include "llvm/Target/TargetLowering.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000043#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000044#include "llvm/Target/TargetSubtargetInfo.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000045using namespace llvm;
Anton Korobeynikov31a92122010-02-21 20:28:15 +000046using namespace dwarf;
Anton Korobeynikovab663a02010-02-15 22:37:53 +000047
48//===----------------------------------------------------------------------===//
49// ELF
50//===----------------------------------------------------------------------===//
Anton Korobeynikovab663a02010-02-15 22:37:53 +000051
Rafael Espindoladaeafb42014-02-19 17:23:20 +000052MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
53 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
54 MachineModuleInfo *MMI) const {
Rafael Espindolace83fc32011-04-27 23:17:57 +000055 unsigned Encoding = getPersonalityEncoding();
Logan Chienc0029812014-05-30 16:48:56 +000056 if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect)
Jim Grosbach6f482002015-05-18 18:43:14 +000057 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000058 TM.getSymbol(GV, Mang)->getName());
Logan Chienc0029812014-05-30 16:48:56 +000059 if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr)
60 return TM.getSymbol(GV, Mang);
61 report_fatal_error("We do not support this DWARF encoding yet!");
Rafael Espindolaa83b1772011-04-16 03:51:21 +000062}
63
Mehdi Amini5c0fa582015-07-16 06:04:17 +000064void TargetLoweringObjectFileELF::emitPersonalityValue(
65 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000066 SmallString<64> NameData("DW.ref.");
67 NameData += Sym->getName();
Rafael Espindolaa8695762015-06-02 00:25:12 +000068 MCSymbolELF *Label =
69 cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
Rafael Espindola39897762011-04-27 21:29:52 +000070 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
71 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000072 StringRef Prefix = ".data.";
73 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola39897762011-04-27 21:29:52 +000074 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
Rafael Espindola0709a7b2015-05-21 19:20:38 +000075 MCSection *Sec = getContext().getELFSection(NameData, ELF::SHT_PROGBITS,
76 Flags, 0, Label->getName());
Mehdi Amini5c0fa582015-07-16 06:04:17 +000077 unsigned Size = DL.getPointerSize();
Rafael Espindola39897762011-04-27 21:29:52 +000078 Streamer.SwitchSection(Sec);
Mehdi Amini5c0fa582015-07-16 06:04:17 +000079 Streamer.EmitValueToAlignment(DL.getPointerABIAlignment());
Rafael Espindola39897762011-04-27 21:29:52 +000080 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
Jim Grosbach13760bd2015-05-30 01:25:56 +000081 const MCExpr *E = MCConstantExpr::create(Size, getContext());
Rafael Espindolaa8695762015-06-02 00:25:12 +000082 Streamer.emitELFSize(Label, E);
Rafael Espindola39897762011-04-27 21:29:52 +000083 Streamer.EmitLabel(Label);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000084
Rafael Espindola39897762011-04-27 21:29:52 +000085 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000086}
87
Rafael Espindola15b26692014-02-09 14:50:44 +000088const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
89 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +000090 const TargetMachine &TM, MachineModuleInfo *MMI,
91 MCStreamer &Streamer) const {
Anton Korobeynikove42af362012-11-14 01:47:00 +000092
93 if (Encoding & dwarf::DW_EH_PE_indirect) {
94 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
95
Rafael Espindoladaeafb42014-02-19 17:23:20 +000096 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM);
Anton Korobeynikove42af362012-11-14 01:47:00 +000097
98 // Add information about the stub reference to ELFMMI so that the stub
99 // gets emitted by the asmprinter.
Anton Korobeynikove42af362012-11-14 01:47:00 +0000100 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000101 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000102 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000103 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
104 }
105
106 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000107 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Anton Korobeynikove42af362012-11-14 01:47:00 +0000108 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
109 }
110
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000111 return TargetLoweringObjectFile::
112 getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000113}
114
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000115static SectionKind
116getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0d018b12011-05-24 03:10:31 +0000117 // N.B.: The defaults used in here are no the same ones used in MC.
118 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
119 // both gas and MC will produce a section with no flags. Given
Bill Wendlingd1634052012-07-19 00:04:14 +0000120 // section(".eh_frame") gcc will produce:
121 //
122 // .section .eh_frame,"a",@progbits
Xinliang David Li5f04f922016-01-14 18:09:45 +0000123
124 if (Name == getInstrProfCoverageSectionName(false))
125 return SectionKind::getMetadata();
126
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000127 if (Name.empty() || Name[0] != '.') return K;
128
129 // Some lame default implementation based on some magic section names.
130 if (Name == ".bss" ||
131 Name.startswith(".bss.") ||
132 Name.startswith(".gnu.linkonce.b.") ||
133 Name.startswith(".llvm.linkonce.b.") ||
134 Name == ".sbss" ||
135 Name.startswith(".sbss.") ||
136 Name.startswith(".gnu.linkonce.sb.") ||
137 Name.startswith(".llvm.linkonce.sb."))
138 return SectionKind::getBSS();
139
140 if (Name == ".tdata" ||
141 Name.startswith(".tdata.") ||
142 Name.startswith(".gnu.linkonce.td.") ||
143 Name.startswith(".llvm.linkonce.td."))
144 return SectionKind::getThreadData();
145
146 if (Name == ".tbss" ||
147 Name.startswith(".tbss.") ||
148 Name.startswith(".gnu.linkonce.tb.") ||
149 Name.startswith(".llvm.linkonce.tb."))
150 return SectionKind::getThreadBSS();
151
152 return K;
153}
154
155
156static unsigned getELFSectionType(StringRef Name, SectionKind K) {
157
158 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000159 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000160
161 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000162 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000163
164 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000165 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000166
167 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000168 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000169
Rafael Espindolaaea49582011-01-23 04:28:49 +0000170 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000171}
172
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000173static unsigned getELFSectionFlags(SectionKind K) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000174 unsigned Flags = 0;
175
176 if (!K.isMetadata())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000177 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000178
179 if (K.isText())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000180 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000181
Rafael Espindolac85e0d82011-06-07 23:26:45 +0000182 if (K.isWriteable())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000183 Flags |= ELF::SHF_WRITE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000184
185 if (K.isThreadLocal())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000186 Flags |= ELF::SHF_TLS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000187
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000188 if (K.isMergeableCString() || K.isMergeableConst())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000189 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000190
191 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000192 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000193
194 return Flags;
195}
196
David Majnemerdad0a642014-06-27 18:19:56 +0000197static const Comdat *getELFComdat(const GlobalValue *GV) {
198 const Comdat *C = GV->getComdat();
199 if (!C)
200 return nullptr;
201
202 if (C->getSelectionKind() != Comdat::Any)
203 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
204 C->getName() + "' cannot be lowered.");
205
206 return C;
207}
208
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000209MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000210 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
211 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000212 StringRef SectionName = GV->getSection();
213
214 // Infer section flags from the section name if we can.
215 Kind = getELFKindForNamedSection(SectionName, Kind);
216
David Majnemerdad0a642014-06-27 18:19:56 +0000217 StringRef Group = "";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000218 unsigned Flags = getELFSectionFlags(Kind);
David Majnemerdad0a642014-06-27 18:19:56 +0000219 if (const Comdat *C = getELFComdat(GV)) {
220 Group = C->getName();
221 Flags |= ELF::SHF_GROUP;
222 }
Chris Lattner80c34592010-04-08 21:34:17 +0000223 return getContext().getELFSection(SectionName,
David Majnemerdad0a642014-06-27 18:19:56 +0000224 getELFSectionType(SectionName, Kind), Flags,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000225 /*EntrySize=*/0, Group);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000226}
227
Rafael Espindola25d2c202015-02-11 14:44:17 +0000228/// Return the section prefix name used by options FunctionsSections and
229/// DataSections.
David Majnemer102ff692014-06-24 16:01:53 +0000230static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
Rafael Espindola25d2c202015-02-11 14:44:17 +0000231 if (Kind.isText())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000232 return ".text";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000233 if (Kind.isReadOnly())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000234 return ".rodata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000235 if (Kind.isBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000236 return ".bss";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000237 if (Kind.isThreadData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000238 return ".tdata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000239 if (Kind.isThreadBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000240 return ".tbss";
Rafael Espindola449711c2015-11-18 06:02:15 +0000241 if (Kind.isData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000242 return ".data";
Chris Lattner5b212a32010-04-13 00:36:43 +0000243 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Rafael Espindola68fa2492015-02-17 20:48:01 +0000244 return ".data.rel.ro";
Chris Lattner5b212a32010-04-13 00:36:43 +0000245}
246
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000247static MCSectionELF *
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000248selectELFSectionForGlobal(MCContext &Ctx, const GlobalValue *GV,
249 SectionKind Kind, Mangler &Mang,
250 const TargetMachine &TM, bool EmitUniqueSection,
251 unsigned Flags, unsigned *NextUniqueID) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000252 unsigned EntrySize = 0;
253 if (Kind.isMergeableCString()) {
254 if (Kind.isMergeable2ByteCString()) {
255 EntrySize = 2;
256 } else if (Kind.isMergeable4ByteCString()) {
257 EntrySize = 4;
258 } else {
259 EntrySize = 1;
260 assert(Kind.isMergeable1ByteCString() && "unknown string width");
261 }
262 } else if (Kind.isMergeableConst()) {
263 if (Kind.isMergeableConst4()) {
264 EntrySize = 4;
265 } else if (Kind.isMergeableConst8()) {
266 EntrySize = 8;
David Majnemer964b70d2016-02-22 22:23:11 +0000267 } else if (Kind.isMergeableConst16()) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000268 EntrySize = 16;
David Majnemer964b70d2016-02-22 22:23:11 +0000269 } else {
270 assert(Kind.isMergeableConst32() && "unknown data width");
271 EntrySize = 32;
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000272 }
273 }
274
Rafael Espindola9075f772015-02-20 23:28:28 +0000275 StringRef Group = "";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000276 if (const Comdat *C = getELFComdat(GV)) {
Rafael Espindola9075f772015-02-20 23:28:28 +0000277 Flags |= ELF::SHF_GROUP;
278 Group = C->getName();
279 }
280
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000281 bool UniqueSectionNames = TM.getUniqueSectionNames();
282 SmallString<128> Name;
Rafael Espindolaa05b3b72015-01-28 17:54:19 +0000283 if (Kind.isMergeableCString()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000284 // We also need alignment here.
285 // FIXME: this is getting the alignment of the character, not the
286 // alignment of the global!
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000287 unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment(
288 cast<GlobalVariable>(GV));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000289
Rafael Espindolaba31e272015-01-29 17:33:21 +0000290 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000291 Name = SizeSpec + utostr(Align);
292 } else if (Kind.isMergeableConst()) {
293 Name = ".rodata.cst";
294 Name += utostr(EntrySize);
295 } else {
296 Name = getSectionPrefixForGlobal(Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000297 }
Easwaran Raman01d98ba2016-05-16 23:59:04 +0000298 // FIXME: Extend the section prefix to include hotness catagories such as .hot
299 // or .unlikely for functions.
Dehao Chenf84b6302016-02-23 03:39:24 +0000300
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000301 if (EmitUniqueSection && UniqueSectionNames) {
302 Name.push_back('.');
303 TM.getNameWithPrefix(Name, GV, Mang, true);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000304 }
Reid Kleckner97837b72016-05-02 23:22:18 +0000305 unsigned UniqueID = MCContext::GenericSectionID;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000306 if (EmitUniqueSection && !UniqueSectionNames) {
307 UniqueID = *NextUniqueID;
308 (*NextUniqueID)++;
309 }
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000310 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000311 EntrySize, Group, UniqueID);
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000312}
313
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000314MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000315 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
316 const TargetMachine &TM) const {
317 unsigned Flags = getELFSectionFlags(Kind);
318
319 // If we have -ffunction-section or -fdata-section then we should emit the
320 // global value to a uniqued section specifically for it.
321 bool EmitUniqueSection = false;
322 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
323 if (Kind.isText())
324 EmitUniqueSection = TM.getFunctionSections();
325 else
326 EmitUniqueSection = TM.getDataSections();
327 }
328 EmitUniqueSection |= GV->hasComdat();
329
330 return selectELFSectionForGlobal(getContext(), GV, Kind, Mang, TM,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000331 EmitUniqueSection, Flags, &NextUniqueID);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000332}
333
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000334MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
Rafael Espindola29786d42015-02-12 17:16:46 +0000335 const Function &F, Mangler &Mang, const TargetMachine &TM) const {
336 // If the function can be removed, produce a unique section so that
337 // the table doesn't prevent the removal.
338 const Comdat *C = F.getComdat();
339 bool EmitUniqueSection = TM.getFunctionSections() || C;
340 if (!EmitUniqueSection)
341 return ReadOnlySection;
342
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000343 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000344 Mang, TM, EmitUniqueSection, ELF::SHF_ALLOC,
345 &NextUniqueID);
Rafael Espindola29786d42015-02-12 17:16:46 +0000346}
347
Rafael Espindoladf195192015-02-17 23:34:51 +0000348bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
349 bool UsesLabelDifference, const Function &F) const {
350 // We can always create relative relocations, so use another section
351 // that can be marked non-executable.
352 return false;
353}
354
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000355/// Given a mergeable constant with the specified size and relocation
356/// information, return a section that it should be placed in.
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000357MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000358 const DataLayout &DL, SectionKind Kind, const Constant *C,
359 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000360 if (Kind.isMergeableConst4() && MergeableConst4Section)
361 return MergeableConst4Section;
362 if (Kind.isMergeableConst8() && MergeableConst8Section)
363 return MergeableConst8Section;
364 if (Kind.isMergeableConst16() && MergeableConst16Section)
365 return MergeableConst16Section;
David Majnemer964b70d2016-02-22 22:23:11 +0000366 if (Kind.isMergeableConst32() && MergeableConst32Section)
367 return MergeableConst32Section;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000368 if (Kind.isReadOnly())
369 return ReadOnlySection;
370
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000371 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
372 return DataRelROSection;
373}
374
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000375static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
376 bool IsCtor, unsigned Priority,
377 const MCSymbol *KeySym) {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000378 std::string Name;
379 unsigned Type;
380 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
Rafael Espindolac4b42532014-09-04 23:03:58 +0000381 StringRef COMDAT = KeySym ? KeySym->getName() : "";
382
383 if (KeySym)
384 Flags |= ELF::SHF_GROUP;
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000385
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000386 if (UseInitArray) {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000387 if (IsCtor) {
388 Type = ELF::SHT_INIT_ARRAY;
389 Name = ".init_array";
390 } else {
391 Type = ELF::SHT_FINI_ARRAY;
392 Name = ".fini_array";
393 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000394 if (Priority != 65535) {
395 Name += '.';
396 Name += utostr(Priority);
397 }
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000398 } else {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000399 // The default scheme is .ctor / .dtor, so we have to invert the priority
400 // numbering.
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000401 if (IsCtor)
402 Name = ".ctors";
403 else
404 Name = ".dtors";
Rafael Espindolac4b42532014-09-04 23:03:58 +0000405 if (Priority != 65535) {
406 Name += '.';
407 Name += utostr(65535 - Priority);
408 }
409 Type = ELF::SHT_PROGBITS;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000410 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000411
Rafael Espindolaba31e272015-01-29 17:33:21 +0000412 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000413}
414
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000415MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000416 unsigned Priority, const MCSymbol *KeySym) const {
417 return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
418 KeySym);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000419}
420
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000421MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000422 unsigned Priority, const MCSymbol *KeySym) const {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000423 return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
424 KeySym);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000425}
426
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000427const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
428 const GlobalValue *LHS, const GlobalValue *RHS, Mangler &Mang,
429 const TargetMachine &TM) const {
430 // We may only use a PLT-relative relocation to refer to unnamed_addr
431 // functions.
432 if (!LHS->hasUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
433 return nullptr;
434
435 // Basic sanity checks.
436 if (LHS->getType()->getPointerAddressSpace() != 0 ||
437 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
438 RHS->isThreadLocal())
439 return nullptr;
440
441 return MCBinaryExpr::createSub(
442 MCSymbolRefExpr::create(TM.getSymbol(LHS, Mang), PLTRelativeVariantKind,
443 getContext()),
444 MCSymbolRefExpr::create(TM.getSymbol(RHS, Mang), getContext()),
445 getContext());
446}
447
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000448void
449TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
450 UseInitArray = UseInitArray_;
451 if (!UseInitArray)
452 return;
453
Rafael Espindolaba31e272015-01-29 17:33:21 +0000454 StaticCtorSection = getContext().getELFSection(
455 ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
456 StaticDtorSection = getContext().getELFSection(
457 ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000458}
459
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000460//===----------------------------------------------------------------------===//
461// MachO
462//===----------------------------------------------------------------------===//
463
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000464TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
465 : TargetLoweringObjectFile() {
466 SupportIndirectSymViaGOTPCRel = true;
467}
468
Daniel Dunbar95856122013-01-18 19:37:00 +0000469/// emitModuleFlags - Perform code emission for module flags.
Bill Wendling06df7722012-02-14 21:28:13 +0000470void TargetLoweringObjectFileMachO::
Bill Wendling734909a2012-02-15 22:36:15 +0000471emitModuleFlags(MCStreamer &Streamer,
472 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000473 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendling06df7722012-02-14 21:28:13 +0000474 unsigned VersionVal = 0;
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000475 unsigned ImageInfoFlags = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000476 MDNode *LinkerOptions = nullptr;
Bill Wendling734909a2012-02-15 22:36:15 +0000477 StringRef SectionVal;
Bill Wendling06df7722012-02-14 21:28:13 +0000478
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000479 for (const auto &MFE : ModuleFlags) {
Bill Wendling06df7722012-02-14 21:28:13 +0000480 // Ignore flags with 'Require' behavior.
Bill Wendling734909a2012-02-15 22:36:15 +0000481 if (MFE.Behavior == Module::Require)
Bill Wendling06df7722012-02-14 21:28:13 +0000482 continue;
483
Bill Wendling734909a2012-02-15 22:36:15 +0000484 StringRef Key = MFE.Key->getString();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000485 Metadata *Val = MFE.Val;
Bill Wendling06df7722012-02-14 21:28:13 +0000486
Daniel Dunbar95856122013-01-18 19:37:00 +0000487 if (Key == "Objective-C Image Info Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000488 VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000489 } else if (Key == "Objective-C Garbage Collection" ||
490 Key == "Objective-C GC Only" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000491 Key == "Objective-C Is Simulated" ||
Manman Renc77e0ff2016-01-29 23:51:00 +0000492 Key == "Objective-C Class Properties" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000493 Key == "Objective-C Image Swift Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000494 ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000495 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling734909a2012-02-15 22:36:15 +0000496 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar95856122013-01-18 19:37:00 +0000497 } else if (Key == "Linker Options") {
498 LinkerOptions = cast<MDNode>(Val);
499 }
500 }
501
502 // Emit the linker options if present.
503 if (LinkerOptions) {
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000504 for (const auto &Option : LinkerOptions->operands()) {
Daniel Dunbar95856122013-01-18 19:37:00 +0000505 SmallVector<std::string, 4> StrOptions;
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000506 for (const auto &Piece : cast<MDNode>(Option)->operands())
507 StrOptions.push_back(cast<MDString>(Piece)->getString());
Daniel Dunbar95856122013-01-18 19:37:00 +0000508 Streamer.EmitLinkerOptions(StrOptions);
509 }
Bill Wendling06df7722012-02-14 21:28:13 +0000510 }
511
Bill Wendling734909a2012-02-15 22:36:15 +0000512 // The section is mandatory. If we don't have it, then we don't have GC info.
513 if (SectionVal.empty()) return;
Bill Wendling06df7722012-02-14 21:28:13 +0000514
Bill Wendling734909a2012-02-15 22:36:15 +0000515 StringRef Segment, Section;
516 unsigned TAA = 0, StubSize = 0;
517 bool TAAParsed;
518 std::string ErrorCode =
519 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
520 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000521 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000522 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000523 report_fatal_error("Invalid section specifier '" + Section + "': " +
524 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000525
Bill Wendling734909a2012-02-15 22:36:15 +0000526 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000527 MCSectionMachO *S = getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000528 Segment, Section, TAA, StubSize, SectionKind::getData());
Bill Wendling734909a2012-02-15 22:36:15 +0000529 Streamer.SwitchSection(S);
530 Streamer.EmitLabel(getContext().
Jim Grosbach6f482002015-05-18 18:43:14 +0000531 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000532 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000533 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000534 Streamer.AddBlankLine();
535}
536
David Majnemerdad0a642014-06-27 18:19:56 +0000537static void checkMachOComdat(const GlobalValue *GV) {
538 const Comdat *C = GV->getComdat();
539 if (!C)
540 return;
541
542 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
543 "' cannot be lowered.");
544}
545
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000546MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000547 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
548 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000549 // Parse the section specifier and create it if valid.
550 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000551 unsigned TAA = 0, StubSize = 0;
552 bool TAAParsed;
David Majnemerdad0a642014-06-27 18:19:56 +0000553
554 checkMachOComdat(GV);
555
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000556 std::string ErrorCode =
557 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000558 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000559 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000560 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000561 report_fatal_error("Global variable '" + GV->getName() +
562 "' has an invalid section specifier '" +
563 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000564 }
565
566 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000567 MCSectionMachO *S =
568 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000569
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000570 // If TAA wasn't set by ParseSectionSpecifier() above,
571 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000572 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000573 TAA = S->getTypeAndAttributes();
574
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000575 // Okay, now that we got the section, verify that the TAA & StubSize agree.
576 // If the user declared multiple globals with different section flags, we need
577 // to reject it here.
578 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000579 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000580 report_fatal_error("Global variable '" + GV->getName() +
581 "' section type or attributes does not match previous"
582 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000583 }
584
585 return S;
586}
587
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000588MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
589 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
590 const TargetMachine &TM) const {
David Majnemerdad0a642014-06-27 18:19:56 +0000591 checkMachOComdat(GV);
Bill Wendling25b61db2013-11-17 10:53:13 +0000592
593 // Handle thread local data.
594 if (Kind.isThreadBSS()) return TLSBSSSection;
595 if (Kind.isThreadData()) return TLSDataSection;
596
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000597 if (Kind.isText())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000598 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
599
600 // If this is weak/linkonce, put this in a coalescable section, either in text
601 // or data depending on if it is writable.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000602 if (GV->isWeakForLinker()) {
603 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000604 return ConstTextCoalSection;
605 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000606 }
607
608 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000609 if (Kind.isMergeable1ByteCString() &&
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000610 GV->getParent()->getDataLayout().getPreferredAlignment(
611 cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000612 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000613
Chris Lattneref2f8042010-03-07 04:28:09 +0000614 // Do not put 16-bit arrays in the UString section if they have an
615 // externally visible label, this runs into issues with certain linker
616 // versions.
617 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000618 GV->getParent()->getDataLayout().getPreferredAlignment(
619 cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000620 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000621
Rafael Espindolab43d51d2014-08-28 20:13:31 +0000622 // With MachO only variables whose corresponding symbol starts with 'l' or
623 // 'L' can be merged, so we only try merging GVs with private linkage.
624 if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000625 if (Kind.isMergeableConst4())
626 return FourByteConstantSection;
627 if (Kind.isMergeableConst8())
628 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000629 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000630 return SixteenByteConstantSection;
631 }
632
633 // Otherwise, if it is readonly, but not something we can specially optimize,
634 // just drop it in .const.
635 if (Kind.isReadOnly())
636 return ReadOnlySection;
637
638 // If this is marked const, put it into a const section. But if the dynamic
639 // linker needs to write to it, put it in the data segment.
640 if (Kind.isReadOnlyWithRel())
641 return ConstDataSection;
642
643 // Put zero initialized globals with strong external linkage in the
644 // DATA, __common section with the .zerofill directive.
645 if (Kind.isBSSExtern())
646 return DataCommonSection;
647
648 // Put zero initialized globals with local linkage in __DATA,__bss directive
649 // with the .zerofill directive (aka .lcomm).
650 if (Kind.isBSSLocal())
651 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000652
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000653 // Otherwise, just drop the variable in the normal data section.
654 return DataSection;
655}
656
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000657MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000658 const DataLayout &DL, SectionKind Kind, const Constant *C,
659 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000660 // If this constant requires a relocation, we have to put it in the data
661 // segment, not in the text segment.
Rafael Espindola449711c2015-11-18 06:02:15 +0000662 if (Kind.isData() || Kind.isReadOnlyWithRel())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000663 return ConstDataSection;
664
665 if (Kind.isMergeableConst4())
666 return FourByteConstantSection;
667 if (Kind.isMergeableConst8())
668 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000669 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000670 return SixteenByteConstantSection;
671 return ReadOnlySection; // .const
672}
673
Rafael Espindola15b26692014-02-09 14:50:44 +0000674const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
675 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000676 const TargetMachine &TM, MachineModuleInfo *MMI,
677 MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000678 // The mach-o version of this method defaults to returning a stub reference.
679
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000680 if (Encoding & DW_EH_PE_indirect) {
681 MachineModuleInfoMachO &MachOMMI =
682 MMI->getObjFileInfo<MachineModuleInfoMachO>();
683
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000684 MCSymbol *SSym =
685 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000686
687 // Add information about the stub reference to MachOMMI so that the stub
688 // gets emitted by the asmprinter.
Rafael Espindola712f9572016-05-17 16:01:32 +0000689 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000690 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000691 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000692 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000693 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000694
695 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000696 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Anton Korobeynikove42af362012-11-14 01:47:00 +0000697 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000698 }
699
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000700 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang,
701 TM, MMI, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000702}
703
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000704MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
705 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
706 MachineModuleInfo *MMI) const {
Rafael Espindola08704342011-04-27 23:08:15 +0000707 // The mach-o version of this method defaults to returning a stub reference.
708 MachineModuleInfoMachO &MachOMMI =
709 MMI->getObjFileInfo<MachineModuleInfoMachO>();
710
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000711 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Rafael Espindola08704342011-04-27 23:08:15 +0000712
713 // Add information about the stub reference to MachOMMI so that the stub
714 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000715 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000716 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000717 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Rafael Espindola08704342011-04-27 23:08:15 +0000718 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
719 }
720
721 return SSym;
722}
723
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000724const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
725 const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
726 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000727 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000728 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
729 // through a non_lazy_ptr stub instead. One advantage is that it allows the
730 // computation of deltas to final external symbols. Example:
731 //
732 // _extgotequiv:
733 // .long _extfoo
734 //
735 // _delta:
736 // .long _extgotequiv-_delta
737 //
738 // is transformed to:
739 //
740 // _delta:
741 // .long L_extfoo$non_lazy_ptr-(_delta+0)
742 //
743 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
744 // L_extfoo$non_lazy_ptr:
745 // .indirect_symbol _extfoo
746 // .long 0
747 //
748 MachineModuleInfoMachO &MachOMMI =
749 MMI->getObjFileInfo<MachineModuleInfoMachO>();
750 MCContext &Ctx = getContext();
751
752 // The offset must consider the original displacement from the base symbol
753 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
754 Offset = -MV.getConstant();
755 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
756
757 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
758 // non_lazy_ptr stubs.
759 SmallString<128> Name;
760 StringRef Suffix = "$non_lazy_ptr";
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000761 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000762 Name += Sym->getName();
763 Name += Suffix;
Jim Grosbach6f482002015-05-18 18:43:14 +0000764 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000765
766 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
767 if (!StubSym.getPointer())
768 StubSym = MachineModuleInfoImpl::
769 StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
770
771 const MCExpr *BSymExpr =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000772 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000773 const MCExpr *LHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000774 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000775
776 if (!Offset)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000777 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000778
779 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000780 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
781 return MCBinaryExpr::createSub(LHS, RHS, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000782}
783
Peter Collingbourne94d77862015-11-03 23:40:03 +0000784static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
785 const MCSection &Section) {
786 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
787 return true;
788
789 // If it is not dead stripped, it is safe to use private labels.
790 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
791 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
792 return true;
793
794 return false;
795}
796
797void TargetLoweringObjectFileMachO::getNameWithPrefix(
798 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
799 const TargetMachine &TM) const {
800 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
801 const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM);
802 bool CannotUsePrivateLabel =
803 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
804 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
805}
806
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000807//===----------------------------------------------------------------------===//
808// COFF
809//===----------------------------------------------------------------------===//
810
Chris Lattner87cffa92010-05-07 17:17:41 +0000811static unsigned
812getCOFFSectionFlags(SectionKind K) {
813 unsigned Flags = 0;
814
Anton Korobeynikove4152302010-07-06 15:24:56 +0000815 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000816 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000817 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000818 else if (K.isText())
819 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000820 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000821 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar329d2022010-07-01 20:07:24 +0000822 COFF::IMAGE_SCN_CNT_CODE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000823 else if (K.isBSS())
Chris Lattner02844932010-05-07 21:49:09 +0000824 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000825 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
826 COFF::IMAGE_SCN_MEM_READ |
827 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000828 else if (K.isThreadLocal())
829 Flags |=
830 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
831 COFF::IMAGE_SCN_MEM_READ |
832 COFF::IMAGE_SCN_MEM_WRITE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000833 else if (K.isReadOnly() || K.isReadOnlyWithRel())
Chris Lattner87cffa92010-05-07 17:17:41 +0000834 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000835 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
836 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000837 else if (K.isWriteable())
838 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000839 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
840 COFF::IMAGE_SCN_MEM_READ |
841 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000842
843 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000844}
845
Benjamin Kramer6cbe6702014-07-07 14:47:51 +0000846static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
David Majnemerdad0a642014-06-27 18:19:56 +0000847 const Comdat *C = GV->getComdat();
848 assert(C && "expected GV to have a Comdat!");
849
850 StringRef ComdatGVName = C->getName();
851 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
852 if (!ComdatGV)
853 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
854 "' does not exist.");
855
856 if (ComdatGV->getComdat() != C)
857 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
Hans Wennborgc0f0c512014-09-19 01:14:56 +0000858 "' is not a key for its COMDAT.");
David Majnemerdad0a642014-06-27 18:19:56 +0000859
860 return ComdatGV;
861}
862
863static int getSelectionForCOFF(const GlobalValue *GV) {
864 if (const Comdat *C = GV->getComdat()) {
865 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
866 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
867 ComdatKey = GA->getBaseObject();
868 if (ComdatKey == GV) {
869 switch (C->getSelectionKind()) {
870 case Comdat::Any:
871 return COFF::IMAGE_COMDAT_SELECT_ANY;
872 case Comdat::ExactMatch:
873 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
874 case Comdat::Largest:
875 return COFF::IMAGE_COMDAT_SELECT_LARGEST;
876 case Comdat::NoDuplicates:
877 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
878 case Comdat::SameSize:
879 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
880 }
881 } else {
882 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
883 }
David Majnemerdad0a642014-06-27 18:19:56 +0000884 }
885 return 0;
886}
887
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000888MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000889 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
890 const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000891 int Selection = 0;
892 unsigned Characteristics = getCOFFSectionFlags(Kind);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000893 StringRef Name = GV->getSection();
894 StringRef COMDATSymName = "";
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000895 if (GV->hasComdat()) {
David Majnemerdad0a642014-06-27 18:19:56 +0000896 Selection = getSelectionForCOFF(GV);
897 const GlobalValue *ComdatGV;
898 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
899 ComdatGV = getComdatGVForCOFF(GV);
900 else
901 ComdatGV = GV;
902
903 if (!ComdatGV->hasPrivateLinkage()) {
904 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
905 COMDATSymName = Sym->getName();
906 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
907 } else {
908 Selection = 0;
909 }
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000910 }
Reid Kleckner97837b72016-05-02 23:22:18 +0000911
912 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +0000913 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000914}
915
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000916static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000917 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000918 return ".text";
David Majnemera9bdb322014-04-08 22:33:40 +0000919 if (Kind.isBSS())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000920 return ".bss";
921 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +0000922 return ".tls$";
David Majnemer597be2d2014-09-22 20:39:23 +0000923 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
924 return ".rdata";
925 return ".data";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000926}
927
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000928MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
929 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
930 const TargetMachine &TM) const {
David Majnemer93389842014-03-23 17:47:39 +0000931 // If we have -ffunction-sections then we should emit the global value to a
932 // uniqued section specifically for it.
David Majnemer273bff42014-03-25 06:14:26 +0000933 bool EmitUniquedSection;
934 if (Kind.isText())
935 EmitUniquedSection = TM.getFunctionSections();
936 else
937 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000938
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000939 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000940 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Chris Lattner02844932010-05-07 21:49:09 +0000941 unsigned Characteristics = getCOFFSectionFlags(Kind);
942
Daniel Dunbar329d2022010-07-01 20:07:24 +0000943 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
David Majnemerdad0a642014-06-27 18:19:56 +0000944 int Selection = getSelectionForCOFF(GV);
945 if (!Selection)
946 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
947 const GlobalValue *ComdatGV;
948 if (GV->hasComdat())
949 ComdatGV = getComdatGVForCOFF(GV);
950 else
951 ComdatGV = GV;
952
Reid Kleckner97837b72016-05-02 23:22:18 +0000953 unsigned UniqueID = MCContext::GenericSectionID;
954 if (EmitUniquedSection)
955 UniqueID = NextUniqueID++;
956
David Majnemerdad0a642014-06-27 18:19:56 +0000957 if (!ComdatGV->hasPrivateLinkage()) {
958 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
959 StringRef COMDATSymName = Sym->getName();
960 return getContext().getCOFFSection(Name, Characteristics, Kind,
Reid Kleckner97837b72016-05-02 23:22:18 +0000961 COMDATSymName, Selection, UniqueID);
David Majnemer7db449a2015-03-17 23:54:51 +0000962 } else {
963 SmallString<256> TmpData;
Peter Collingbourne94d77862015-11-03 23:40:03 +0000964 Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true);
David Majnemer7db449a2015-03-17 23:54:51 +0000965 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
Reid Kleckner97837b72016-05-02 23:22:18 +0000966 Selection, UniqueID);
David Majnemerdad0a642014-06-27 18:19:56 +0000967 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000968 }
969
970 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +0000971 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000972
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000973 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +0000974 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000975
David Majnemer597be2d2014-09-22 20:39:23 +0000976 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
David Majnemerf76d6b32013-08-08 01:50:52 +0000977 return ReadOnlySection;
978
David Majnemera9bdb322014-04-08 22:33:40 +0000979 // Note: we claim that common symbols are put in BSSSection, but they are
980 // really emitted with the magic .comm directive, which creates a symbol table
981 // entry but not a section.
982 if (Kind.isBSS() || Kind.isCommon())
David Majnemer3d96acb2013-08-13 01:23:53 +0000983 return BSSSection;
984
985 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000986}
987
David Majnemer7db449a2015-03-17 23:54:51 +0000988void TargetLoweringObjectFileCOFF::getNameWithPrefix(
Peter Collingbourne94d77862015-11-03 23:40:03 +0000989 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
990 const TargetMachine &TM) const {
991 bool CannotUsePrivateLabel = false;
David Majnemer7db449a2015-03-17 23:54:51 +0000992 if (GV->hasPrivateLinkage() &&
993 ((isa<Function>(GV) && TM.getFunctionSections()) ||
994 (isa<GlobalVariable>(GV) && TM.getDataSections())))
995 CannotUsePrivateLabel = true;
996
997 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
998}
999
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001000MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
Rafael Espindolaab447e42015-03-11 19:58:37 +00001001 const Function &F, Mangler &Mang, const TargetMachine &TM) const {
1002 // If the function can be removed, produce a unique section so that
1003 // the table doesn't prevent the removal.
1004 const Comdat *C = F.getComdat();
1005 bool EmitUniqueSection = TM.getFunctionSections() || C;
1006 if (!EmitUniqueSection)
1007 return ReadOnlySection;
1008
1009 // FIXME: we should produce a symbol for F instead.
1010 if (F.hasPrivateLinkage())
1011 return ReadOnlySection;
1012
1013 MCSymbol *Sym = TM.getSymbol(&F, Mang);
1014 StringRef COMDATSymName = Sym->getName();
1015
1016 SectionKind Kind = SectionKind::getReadOnly();
1017 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
1018 unsigned Characteristics = getCOFFSectionFlags(Kind);
1019 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Reid Kleckner97837b72016-05-02 23:22:18 +00001020 unsigned UniqueID = NextUniqueID++;
Rafael Espindolaab447e42015-03-11 19:58:37 +00001021
1022 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Reid Kleckner97837b72016-05-02 23:22:18 +00001023 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
Rafael Espindolaab447e42015-03-11 19:58:37 +00001024}
1025
Reid Klecknerd973ca32013-04-25 19:34:41 +00001026void TargetLoweringObjectFileCOFF::
1027emitModuleFlags(MCStreamer &Streamer,
1028 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +00001029 Mangler &Mang, const TargetMachine &TM) const {
Craig Topperc0196b12014-04-14 00:51:57 +00001030 MDNode *LinkerOptions = nullptr;
Reid Klecknerd973ca32013-04-25 19:34:41 +00001031
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001032 for (const auto &MFE : ModuleFlags) {
Reid Klecknerd973ca32013-04-25 19:34:41 +00001033 StringRef Key = MFE.Key->getString();
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001034 if (Key == "Linker Options")
1035 LinkerOptions = cast<MDNode>(MFE.Val);
Reid Klecknerd973ca32013-04-25 19:34:41 +00001036 }
Reid Klecknerd973ca32013-04-25 19:34:41 +00001037
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001038 if (LinkerOptions) {
1039 // Emit the linker options to the linker .drectve section. According to the
1040 // spec, this section is a space-separated string containing flags for
1041 // linker.
1042 MCSection *Sec = getDrectveSection();
1043 Streamer.SwitchSection(Sec);
1044 for (const auto &Option : LinkerOptions->operands()) {
1045 for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1046 // Lead with a space for consistency with our dllexport implementation.
1047 std::string Directive(" ");
1048 Directive.append(cast<MDString>(Piece)->getString());
1049 Streamer.EmitBytes(Directive);
1050 }
Reid Klecknerd973ca32013-04-25 19:34:41 +00001051 }
1052 }
1053}
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001054
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001055MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001056 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +00001057 return getContext().getAssociativeCOFFSection(
Reid Kleckner97837b72016-05-02 23:22:18 +00001058 cast<MCSectionCOFF>(StaticCtorSection), KeySym, 0);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001059}
1060
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001061MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001062 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +00001063 return getContext().getAssociativeCOFFSection(
Reid Kleckner97837b72016-05-02 23:22:18 +00001064 cast<MCSectionCOFF>(StaticDtorSection), KeySym, 0);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001065}
Peter Collingbourneaef36592015-06-29 22:04:09 +00001066
1067void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
1068 raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const {
1069 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
1070 return;
1071
1072 const Triple &TT = getTargetTriple();
1073
1074 if (TT.isKnownWindowsMSVCEnvironment())
1075 OS << " /EXPORT:";
1076 else
1077 OS << " -export:";
1078
1079 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
1080 std::string Flag;
1081 raw_string_ostream FlagOS(Flag);
1082 Mang.getNameWithPrefix(FlagOS, GV, false);
1083 FlagOS.flush();
Mehdi Amini5c0fa582015-07-16 06:04:17 +00001084 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
Peter Collingbourneaef36592015-06-29 22:04:09 +00001085 OS << Flag.substr(1);
1086 else
1087 OS << Flag;
1088 } else {
1089 Mang.getNameWithPrefix(OS, GV, false);
1090 }
1091
1092 if (!GV->getValueType()->isFunctionTy()) {
1093 if (TT.isKnownWindowsMSVCEnvironment())
1094 OS << ",DATA";
1095 else
1096 OS << ",data";
1097 }
1098}