blob: 127b2aa324d30dac116129ae87cca798a9f28d21 [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"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCExpr.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000029#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/MC/MCSectionELF.h"
31#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolaa83b1772011-04-16 03:51:21 +000032#include "llvm/MC/MCStreamer.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000033#include "llvm/MC/MCSymbol.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000034#include "llvm/Support/Dwarf.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000035#include "llvm/Support/ELF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Target/TargetMachine.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000039using namespace llvm;
Anton Korobeynikov31a92122010-02-21 20:28:15 +000040using namespace dwarf;
Anton Korobeynikovab663a02010-02-15 22:37:53 +000041
42//===----------------------------------------------------------------------===//
43// ELF
44//===----------------------------------------------------------------------===//
Anton Korobeynikovab663a02010-02-15 22:37:53 +000045
Rafael Espindolaa83b1772011-04-16 03:51:21 +000046MCSymbol *
Rafael Espindola08704342011-04-27 23:08:15 +000047TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
Rafael Espindolafa0f7282014-02-08 14:53:28 +000048 Mangler &Mang,
Rafael Espindola08704342011-04-27 23:08:15 +000049 MachineModuleInfo *MMI) const {
Rafael Espindolace83fc32011-04-27 23:17:57 +000050 unsigned Encoding = getPersonalityEncoding();
Rafael Espindola08704342011-04-27 23:08:15 +000051 switch (Encoding & 0x70) {
52 default:
53 report_fatal_error("We do not support this DWARF encoding yet!");
54 case dwarf::DW_EH_PE_absptr:
Rafael Espindola15b26692014-02-09 14:50:44 +000055 return getSymbol(GV, Mang);
Rafael Espindola08704342011-04-27 23:08:15 +000056 case dwarf::DW_EH_PE_pcrel: {
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000057 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
Rafael Espindola15b26692014-02-09 14:50:44 +000058 getSymbol(GV, Mang)->getName());
Rafael Espindola08704342011-04-27 23:08:15 +000059 }
60 }
Rafael Espindolaa83b1772011-04-16 03:51:21 +000061}
62
63void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
64 const TargetMachine &TM,
Rafael Espindola08704342011-04-27 23:08:15 +000065 const MCSymbol *Sym) const {
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000066 SmallString<64> NameData("DW.ref.");
67 NameData += Sym->getName();
68 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
Rafael Espindola39897762011-04-27 21:29:52 +000069 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
70 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000071 StringRef Prefix = ".data.";
72 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola39897762011-04-27 21:29:52 +000073 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
74 const MCSection *Sec = getContext().getELFSection(NameData,
75 ELF::SHT_PROGBITS,
76 Flags,
77 SectionKind::getDataRel(),
78 0, Label->getName());
Chandler Carruth5da3f052012-11-01 09:14:31 +000079 unsigned Size = TM.getDataLayout()->getPointerSize();
Rafael Espindola39897762011-04-27 21:29:52 +000080 Streamer.SwitchSection(Sec);
Chandler Carruth5da3f052012-11-01 09:14:31 +000081 Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment());
Rafael Espindola39897762011-04-27 21:29:52 +000082 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
David Chisnall8fa17162012-02-17 16:05:50 +000083 const MCExpr *E = MCConstantExpr::Create(Size, getContext());
Rafael Espindola39897762011-04-27 21:29:52 +000084 Streamer.EmitELFSize(Label, E);
85 Streamer.EmitLabel(Label);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000086
Rafael Espindola39897762011-04-27 21:29:52 +000087 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000088}
89
Rafael Espindola15b26692014-02-09 14:50:44 +000090const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
91 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
92 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Anton Korobeynikove42af362012-11-14 01:47:00 +000093
94 if (Encoding & dwarf::DW_EH_PE_indirect) {
95 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
96
Rafael Espindola15b26692014-02-09 14:50:44 +000097 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang);
Anton Korobeynikove42af362012-11-14 01:47:00 +000098
99 // Add information about the stub reference to ELFMMI so that the stub
100 // gets emitted by the asmprinter.
Anton Korobeynikove42af362012-11-14 01:47:00 +0000101 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
102 if (StubSym.getPointer() == 0) {
Rafael Espindola15b26692014-02-09 14:50:44 +0000103 MCSymbol *Sym = getSymbol(GV, Mang);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000104 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
105 }
106
107 return TargetLoweringObjectFile::
108 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
109 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
110 }
111
Rafael Espindola15b26692014-02-09 14:50:44 +0000112 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang,
113 MMI, Streamer);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000114}
115
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000116static SectionKind
117getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0d018b12011-05-24 03:10:31 +0000118 // N.B.: The defaults used in here are no the same ones used in MC.
119 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
120 // both gas and MC will produce a section with no flags. Given
Bill Wendlingd1634052012-07-19 00:04:14 +0000121 // section(".eh_frame") gcc will produce:
122 //
123 // .section .eh_frame,"a",@progbits
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000124 if (Name.empty() || Name[0] != '.') return K;
125
126 // Some lame default implementation based on some magic section names.
127 if (Name == ".bss" ||
128 Name.startswith(".bss.") ||
129 Name.startswith(".gnu.linkonce.b.") ||
130 Name.startswith(".llvm.linkonce.b.") ||
131 Name == ".sbss" ||
132 Name.startswith(".sbss.") ||
133 Name.startswith(".gnu.linkonce.sb.") ||
134 Name.startswith(".llvm.linkonce.sb."))
135 return SectionKind::getBSS();
136
137 if (Name == ".tdata" ||
138 Name.startswith(".tdata.") ||
139 Name.startswith(".gnu.linkonce.td.") ||
140 Name.startswith(".llvm.linkonce.td."))
141 return SectionKind::getThreadData();
142
143 if (Name == ".tbss" ||
144 Name.startswith(".tbss.") ||
145 Name.startswith(".gnu.linkonce.tb.") ||
146 Name.startswith(".llvm.linkonce.tb."))
147 return SectionKind::getThreadBSS();
148
149 return K;
150}
151
152
153static unsigned getELFSectionType(StringRef Name, SectionKind K) {
154
155 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000156 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000157
158 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000159 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000160
161 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000162 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000163
164 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000165 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000166
Rafael Espindolaaea49582011-01-23 04:28:49 +0000167 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000168}
169
170
171static unsigned
172getELFSectionFlags(SectionKind K) {
173 unsigned Flags = 0;
174
175 if (!K.isMetadata())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000176 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000177
178 if (K.isText())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000179 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000180
Rafael Espindolac85e0d82011-06-07 23:26:45 +0000181 if (K.isWriteable())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000182 Flags |= ELF::SHF_WRITE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000183
184 if (K.isThreadLocal())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000185 Flags |= ELF::SHF_TLS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000186
187 // K.isMergeableConst() is left out to honour PR4650
188 if (K.isMergeableCString() || K.isMergeableConst4() ||
189 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000190 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000191
192 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000193 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000194
195 return Flags;
196}
197
198
199const MCSection *TargetLoweringObjectFileELF::
200getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000201 Mangler &Mang, const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000202 StringRef SectionName = GV->getSection();
203
204 // Infer section flags from the section name if we can.
205 Kind = getELFKindForNamedSection(SectionName, Kind);
206
Chris Lattner80c34592010-04-08 21:34:17 +0000207 return getContext().getELFSection(SectionName,
208 getELFSectionType(SectionName, Kind),
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000209 getELFSectionFlags(Kind), Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000210}
211
Chris Lattner5b212a32010-04-13 00:36:43 +0000212/// getSectionPrefixForGlobal - Return the section prefix name used by options
213/// FunctionsSections and DataSections.
214static const char *getSectionPrefixForGlobal(SectionKind Kind) {
215 if (Kind.isText()) return ".text.";
216 if (Kind.isReadOnly()) return ".rodata.";
Anton Korobeynikova22828e2012-02-23 10:36:04 +0000217 if (Kind.isBSS()) return ".bss.";
Chris Lattner5b212a32010-04-13 00:36:43 +0000218
219 if (Kind.isThreadData()) return ".tdata.";
220 if (Kind.isThreadBSS()) return ".tbss.";
221
222 if (Kind.isDataNoRel()) return ".data.";
223 if (Kind.isDataRelLocal()) return ".data.rel.local.";
224 if (Kind.isDataRel()) return ".data.rel.";
225 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
226
227 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
228 return ".data.rel.ro.";
229}
230
231
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000232const MCSection *TargetLoweringObjectFileELF::
233SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000234 Mangler &Mang, const TargetMachine &TM) const {
Chris Lattner5b212a32010-04-13 00:36:43 +0000235 // If we have -ffunction-section or -fdata-section then we should emit the
236 // global value to a uniqued section specifically for it.
237 bool EmitUniquedSection;
238 if (Kind.isText())
239 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000240 else
Chris Lattner5b212a32010-04-13 00:36:43 +0000241 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000242
243 // If this global is linkonce/weak and the target handles this by emitting it
244 // into a 'uniqued' section name, create and return the section now.
Chris Lattner5b212a32010-04-13 00:36:43 +0000245 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
Anton Korobeynikova22828e2012-02-23 10:36:04 +0000246 !Kind.isCommon()) {
Chris Lattner5b212a32010-04-13 00:36:43 +0000247 const char *Prefix;
Rafael Espindola70d80152011-02-14 22:23:49 +0000248 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner5b212a32010-04-13 00:36:43 +0000249
Chris Lattner2ea586b2010-03-15 20:37:38 +0000250 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Rafael Espindola15b26692014-02-09 14:50:44 +0000251 MCSymbol *Sym = getSymbol(GV, Mang);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000252 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola70d80152011-02-14 22:23:49 +0000253 StringRef Group = "";
254 unsigned Flags = getELFSectionFlags(Kind);
255 if (GV->isWeakForLinker()) {
256 Group = Sym->getName();
257 Flags |= ELF::SHF_GROUP;
258 }
259
Chris Lattner80c34592010-04-08 21:34:17 +0000260 return getContext().getELFSection(Name.str(),
261 getELFSectionType(Name.str(), Kind),
Rafael Espindola70d80152011-02-14 22:23:49 +0000262 Flags, Kind, 0, Group);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000263 }
264
265 if (Kind.isText()) return TextSection;
266
267 if (Kind.isMergeable1ByteCString() ||
268 Kind.isMergeable2ByteCString() ||
269 Kind.isMergeable4ByteCString()) {
270
271 // We also need alignment here.
272 // FIXME: this is getting the alignment of the character, not the
273 // alignment of the global!
274 unsigned Align =
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000275 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000276
277 const char *SizeSpec = ".rodata.str1.";
278 if (Kind.isMergeable2ByteCString())
279 SizeSpec = ".rodata.str2.";
280 else if (Kind.isMergeable4ByteCString())
281 SizeSpec = ".rodata.str4.";
282 else
283 assert(Kind.isMergeable1ByteCString() && "unknown string width");
284
285
286 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolaaea49582011-01-23 04:28:49 +0000287 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000288 ELF::SHF_ALLOC |
289 ELF::SHF_MERGE |
290 ELF::SHF_STRINGS,
Chris Lattner80c34592010-04-08 21:34:17 +0000291 Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000292 }
293
294 if (Kind.isMergeableConst()) {
295 if (Kind.isMergeableConst4() && MergeableConst4Section)
296 return MergeableConst4Section;
297 if (Kind.isMergeableConst8() && MergeableConst8Section)
298 return MergeableConst8Section;
299 if (Kind.isMergeableConst16() && MergeableConst16Section)
300 return MergeableConst16Section;
301 return ReadOnlySection; // .const
302 }
303
304 if (Kind.isReadOnly()) return ReadOnlySection;
305
306 if (Kind.isThreadData()) return TLSDataSection;
307 if (Kind.isThreadBSS()) return TLSBSSSection;
308
309 // Note: we claim that common symbols are put in BSSSection, but they are
310 // really emitted with the magic .comm directive, which creates a symbol table
311 // entry but not a section.
312 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
313
314 if (Kind.isDataNoRel()) return DataSection;
315 if (Kind.isDataRelLocal()) return DataRelLocalSection;
316 if (Kind.isDataRel()) return DataRelSection;
317 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
318
319 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
320 return DataRelROSection;
321}
322
323/// getSectionForConstant - Given a mergeable constant with the
324/// specified size and relocation information, return a section that it
325/// should be placed in.
326const MCSection *TargetLoweringObjectFileELF::
327getSectionForConstant(SectionKind Kind) const {
328 if (Kind.isMergeableConst4() && MergeableConst4Section)
329 return MergeableConst4Section;
330 if (Kind.isMergeableConst8() && MergeableConst8Section)
331 return MergeableConst8Section;
332 if (Kind.isMergeableConst16() && MergeableConst16Section)
333 return MergeableConst16Section;
334 if (Kind.isReadOnly())
335 return ReadOnlySection;
336
337 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
338 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
339 return DataRelROSection;
340}
341
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000342const MCSection *
343TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const {
344 // The default scheme is .ctor / .dtor, so we have to invert the priority
345 // numbering.
346 if (Priority == 65535)
347 return StaticCtorSection;
348
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000349 if (UseInitArray) {
350 std::string Name = std::string(".init_array.") + utostr(Priority);
351 return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY,
352 ELF::SHF_ALLOC | ELF::SHF_WRITE,
353 SectionKind::getDataRel());
354 } else {
355 std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
356 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
357 ELF::SHF_ALLOC |ELF::SHF_WRITE,
358 SectionKind::getDataRel());
359 }
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000360}
361
362const MCSection *
363TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
364 // The default scheme is .ctor / .dtor, so we have to invert the priority
365 // numbering.
366 if (Priority == 65535)
367 return StaticDtorSection;
368
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000369 if (UseInitArray) {
370 std::string Name = std::string(".fini_array.") + utostr(Priority);
371 return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY,
372 ELF::SHF_ALLOC | ELF::SHF_WRITE,
373 SectionKind::getDataRel());
374 } else {
375 std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
376 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
377 ELF::SHF_ALLOC |ELF::SHF_WRITE,
378 SectionKind::getDataRel());
379 }
380}
381
382void
383TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
384 UseInitArray = UseInitArray_;
385 if (!UseInitArray)
386 return;
387
388 StaticCtorSection =
389 getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
390 ELF::SHF_WRITE |
391 ELF::SHF_ALLOC,
392 SectionKind::getDataRel());
393 StaticDtorSection =
394 getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
395 ELF::SHF_WRITE |
396 ELF::SHF_ALLOC,
397 SectionKind::getDataRel());
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000398}
399
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000400//===----------------------------------------------------------------------===//
401// MachO
402//===----------------------------------------------------------------------===//
403
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000404/// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
405/// option string. Returns StringRef() if the option does not specify a library.
406StringRef TargetLoweringObjectFileMachO::
407getDepLibFromLinkerOpt(StringRef LinkerOption) const {
408 const char *LibCmd = "-l";
409 if (LinkerOption.startswith(LibCmd))
410 return LinkerOption.substr(strlen(LibCmd));
411 return StringRef();
412}
413
Daniel Dunbar95856122013-01-18 19:37:00 +0000414/// emitModuleFlags - Perform code emission for module flags.
Bill Wendling06df7722012-02-14 21:28:13 +0000415void TargetLoweringObjectFileMachO::
Bill Wendling734909a2012-02-15 22:36:15 +0000416emitModuleFlags(MCStreamer &Streamer,
417 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000418 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendling06df7722012-02-14 21:28:13 +0000419 unsigned VersionVal = 0;
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000420 unsigned ImageInfoFlags = 0;
Daniel Dunbar95856122013-01-18 19:37:00 +0000421 MDNode *LinkerOptions = 0;
Bill Wendling734909a2012-02-15 22:36:15 +0000422 StringRef SectionVal;
Bill Wendling06df7722012-02-14 21:28:13 +0000423
Bill Wendling734909a2012-02-15 22:36:15 +0000424 for (ArrayRef<Module::ModuleFlagEntry>::iterator
425 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
426 const Module::ModuleFlagEntry &MFE = *i;
Bill Wendling06df7722012-02-14 21:28:13 +0000427
428 // Ignore flags with 'Require' behavior.
Bill Wendling734909a2012-02-15 22:36:15 +0000429 if (MFE.Behavior == Module::Require)
Bill Wendling06df7722012-02-14 21:28:13 +0000430 continue;
431
Bill Wendling734909a2012-02-15 22:36:15 +0000432 StringRef Key = MFE.Key->getString();
433 Value *Val = MFE.Val;
Bill Wendling06df7722012-02-14 21:28:13 +0000434
Daniel Dunbar95856122013-01-18 19:37:00 +0000435 if (Key == "Objective-C Image Info Version") {
Bill Wendling06df7722012-02-14 21:28:13 +0000436 VersionVal = cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000437 } else if (Key == "Objective-C Garbage Collection" ||
438 Key == "Objective-C GC Only" ||
439 Key == "Objective-C Is Simulated") {
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000440 ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000441 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling734909a2012-02-15 22:36:15 +0000442 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar95856122013-01-18 19:37:00 +0000443 } else if (Key == "Linker Options") {
444 LinkerOptions = cast<MDNode>(Val);
445 }
446 }
447
448 // Emit the linker options if present.
449 if (LinkerOptions) {
450 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
451 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
452 SmallVector<std::string, 4> StrOptions;
453
454 // Convert to strings.
455 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
456 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
457 StrOptions.push_back(MDOption->getString());
458 }
459
460 Streamer.EmitLinkerOptions(StrOptions);
461 }
Bill Wendling06df7722012-02-14 21:28:13 +0000462 }
463
Bill Wendling734909a2012-02-15 22:36:15 +0000464 // The section is mandatory. If we don't have it, then we don't have GC info.
465 if (SectionVal.empty()) return;
Bill Wendling06df7722012-02-14 21:28:13 +0000466
Bill Wendling734909a2012-02-15 22:36:15 +0000467 StringRef Segment, Section;
468 unsigned TAA = 0, StubSize = 0;
469 bool TAAParsed;
470 std::string ErrorCode =
471 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
472 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000473 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000474 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000475 report_fatal_error("Invalid section specifier '" + Section + "': " +
476 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000477
Bill Wendling734909a2012-02-15 22:36:15 +0000478 // Get the section.
479 const MCSectionMachO *S =
480 getContext().getMachOSection(Segment, Section, TAA, StubSize,
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000481 SectionKind::getDataNoRel());
Bill Wendling734909a2012-02-15 22:36:15 +0000482 Streamer.SwitchSection(S);
483 Streamer.EmitLabel(getContext().
484 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000485 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000486 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000487 Streamer.AddBlankLine();
488}
489
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000490const MCSection *TargetLoweringObjectFileMachO::
491getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000492 Mangler &Mang, const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000493 // Parse the section specifier and create it if valid.
494 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000495 unsigned TAA = 0, StubSize = 0;
496 bool TAAParsed;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000497 std::string ErrorCode =
498 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000499 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000500 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000501 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000502 report_fatal_error("Global variable '" + GV->getName() +
503 "' has an invalid section specifier '" +
504 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000505 }
506
507 // Get the section.
508 const MCSectionMachO *S =
Chris Lattner433d4062010-04-08 20:40:11 +0000509 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000510
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000511 // If TAA wasn't set by ParseSectionSpecifier() above,
512 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000513 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000514 TAA = S->getTypeAndAttributes();
515
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000516 // Okay, now that we got the section, verify that the TAA & StubSize agree.
517 // If the user declared multiple globals with different section flags, we need
518 // to reject it here.
519 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000520 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000521 report_fatal_error("Global variable '" + GV->getName() +
522 "' section type or attributes does not match previous"
523 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000524 }
525
526 return S;
527}
528
529const MCSection *TargetLoweringObjectFileMachO::
530SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000531 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendling25b61db2013-11-17 10:53:13 +0000532
533 // Handle thread local data.
534 if (Kind.isThreadBSS()) return TLSBSSSection;
535 if (Kind.isThreadData()) return TLSDataSection;
536
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000537 if (Kind.isText())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000538 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
539
540 // If this is weak/linkonce, put this in a coalescable section, either in text
541 // or data depending on if it is writable.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000542 if (GV->isWeakForLinker()) {
543 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000544 return ConstTextCoalSection;
545 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000546 }
547
548 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000549 if (Kind.isMergeable1ByteCString() &&
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000550 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000551 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000552
Chris Lattneref2f8042010-03-07 04:28:09 +0000553 // Do not put 16-bit arrays in the UString section if they have an
554 // externally visible label, this runs into issues with certain linker
555 // versions.
556 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000557 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000558 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000559
560 if (Kind.isMergeableConst()) {
561 if (Kind.isMergeableConst4())
562 return FourByteConstantSection;
563 if (Kind.isMergeableConst8())
564 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000565 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000566 return SixteenByteConstantSection;
567 }
568
569 // Otherwise, if it is readonly, but not something we can specially optimize,
570 // just drop it in .const.
571 if (Kind.isReadOnly())
572 return ReadOnlySection;
573
574 // If this is marked const, put it into a const section. But if the dynamic
575 // linker needs to write to it, put it in the data segment.
576 if (Kind.isReadOnlyWithRel())
577 return ConstDataSection;
578
579 // Put zero initialized globals with strong external linkage in the
580 // DATA, __common section with the .zerofill directive.
581 if (Kind.isBSSExtern())
582 return DataCommonSection;
583
584 // Put zero initialized globals with local linkage in __DATA,__bss directive
585 // with the .zerofill directive (aka .lcomm).
586 if (Kind.isBSSLocal())
587 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000588
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000589 // Otherwise, just drop the variable in the normal data section.
590 return DataSection;
591}
592
593const MCSection *
594TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
595 // If this constant requires a relocation, we have to put it in the data
596 // segment, not in the text segment.
597 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
598 return ConstDataSection;
599
600 if (Kind.isMergeableConst4())
601 return FourByteConstantSection;
602 if (Kind.isMergeableConst8())
603 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000604 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000605 return SixteenByteConstantSection;
606 return ReadOnlySection; // .const
607}
608
609/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
610/// not to emit the UsedDirective for some symbols in llvm.used.
611// FIXME: REMOVE this (rdar://7071300)
612bool TargetLoweringObjectFileMachO::
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000613shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler &Mang) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000614 /// On Darwin, internally linked data beginning with "L" or "l" does not have
615 /// the directive emitted (this occurs in ObjC metadata).
616 if (!GV) return false;
617
Bill Wendling36321712010-06-29 22:34:52 +0000618 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000619 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
620 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling36321712010-06-29 22:34:52 +0000621 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
622 // this horrible hack can go away.
Rafael Espindola15b26692014-02-09 14:50:44 +0000623 MCSymbol *Sym = getSymbol(GV, Mang);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000624 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000625 return false;
626 }
627
628 return true;
629}
630
Rafael Espindola15b26692014-02-09 14:50:44 +0000631const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
632 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
633 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000634 // The mach-o version of this method defaults to returning a stub reference.
635
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000636 if (Encoding & DW_EH_PE_indirect) {
637 MachineModuleInfoMachO &MachOMMI =
638 MMI->getObjFileInfo<MachineModuleInfoMachO>();
639
Rafael Espindola15b26692014-02-09 14:50:44 +0000640 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang);
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000641
642 // Add information about the stub reference to MachOMMI so that the stub
643 // gets emitted by the asmprinter.
Bill Wendling57e3aaa2011-10-24 23:05:43 +0000644 MachineModuleInfoImpl::StubValueTy &StubSym =
645 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
646 MachOMMI.getGVStubEntry(SSym);
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000647 if (StubSym.getPointer() == 0) {
Rafael Espindola15b26692014-02-09 14:50:44 +0000648 MCSymbol *Sym = getSymbol(GV, Mang);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000649 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000650 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000651
652 return TargetLoweringObjectFile::
Anton Korobeynikove42af362012-11-14 01:47:00 +0000653 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
654 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000655 }
656
657 return TargetLoweringObjectFile::
Rafael Espindola15b26692014-02-09 14:50:44 +0000658 getTTypeGlobalReference(GV, Encoding, Mang, MMI, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000659}
660
Rafael Espindola08704342011-04-27 23:08:15 +0000661MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000662getCFIPersonalitySymbol(const GlobalValue *GV, Mangler &Mang,
Rafael Espindola08704342011-04-27 23:08:15 +0000663 MachineModuleInfo *MMI) const {
664 // The mach-o version of this method defaults to returning a stub reference.
665 MachineModuleInfoMachO &MachOMMI =
666 MMI->getObjFileInfo<MachineModuleInfoMachO>();
667
Rafael Espindola15b26692014-02-09 14:50:44 +0000668 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang);
Rafael Espindola08704342011-04-27 23:08:15 +0000669
670 // Add information about the stub reference to MachOMMI so that the stub
671 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000672 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Rafael Espindola08704342011-04-27 23:08:15 +0000673 if (StubSym.getPointer() == 0) {
Rafael Espindola15b26692014-02-09 14:50:44 +0000674 MCSymbol *Sym = getSymbol(GV, Mang);
Rafael Espindola08704342011-04-27 23:08:15 +0000675 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
676 }
677
678 return SSym;
679}
680
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000681//===----------------------------------------------------------------------===//
682// COFF
683//===----------------------------------------------------------------------===//
684
Chris Lattner87cffa92010-05-07 17:17:41 +0000685static unsigned
686getCOFFSectionFlags(SectionKind K) {
687 unsigned Flags = 0;
688
Anton Korobeynikove4152302010-07-06 15:24:56 +0000689 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000690 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000691 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000692 else if (K.isText())
693 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000694 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000695 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar329d2022010-07-01 20:07:24 +0000696 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner02844932010-05-07 21:49:09 +0000697 else if (K.isBSS ())
698 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000699 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
700 COFF::IMAGE_SCN_MEM_READ |
701 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000702 else if (K.isThreadLocal())
703 Flags |=
704 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
705 COFF::IMAGE_SCN_MEM_READ |
706 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000707 else if (K.isReadOnly())
708 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000709 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
710 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000711 else if (K.isWriteable())
712 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000713 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
714 COFF::IMAGE_SCN_MEM_READ |
715 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000716
717 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000718}
719
720const MCSection *TargetLoweringObjectFileCOFF::
721getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000722 Mangler &Mang, const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000723 int Selection = 0;
724 unsigned Characteristics = getCOFFSectionFlags(Kind);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000725 StringRef Name = GV->getSection();
726 StringRef COMDATSymName = "";
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000727 if (GV->isWeakForLinker()) {
728 Selection = COFF::IMAGE_COMDAT_SELECT_ANY;
729 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Rafael Espindola15b26692014-02-09 14:50:44 +0000730 MCSymbol *Sym = getSymbol(GV, Mang);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000731 COMDATSymName = Sym->getName();
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000732 }
733 return getContext().getCOFFSection(Name,
734 Characteristics,
Nico Riecka37acf72013-07-06 12:13:10 +0000735 Kind,
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000736 COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +0000737 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000738}
739
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000740static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000741 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000742 return ".text";
Chris Lattner02844932010-05-07 21:49:09 +0000743 if (Kind.isBSS ())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000744 return ".bss";
745 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +0000746 return ".tls$";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000747 if (Kind.isWriteable())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000748 return ".data";
749 return ".rdata";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000750}
751
752
753const MCSection *TargetLoweringObjectFileCOFF::
754SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000755 Mangler &Mang, const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000756
757 // If this global is linkonce/weak and the target handles this by emitting it
758 // into a 'uniqued' section name, create and return the section now.
759 if (GV->isWeakForLinker()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000760 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Chris Lattner02844932010-05-07 21:49:09 +0000761 unsigned Characteristics = getCOFFSectionFlags(Kind);
762
Daniel Dunbar329d2022010-07-01 20:07:24 +0000763 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Rafael Espindola15b26692014-02-09 14:50:44 +0000764 MCSymbol *Sym = getSymbol(GV, Mang);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000765 return getContext().getCOFFSection(Name, Characteristics,
766 Kind, Sym->getName(),
767 COFF::IMAGE_COMDAT_SELECT_ANY);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000768 }
769
770 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +0000771 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000772
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000773 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +0000774 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000775
David Majnemer3d96acb2013-08-13 01:23:53 +0000776 if (Kind.isReadOnly())
David Majnemerf76d6b32013-08-08 01:50:52 +0000777 return ReadOnlySection;
778
David Majnemer3d96acb2013-08-13 01:23:53 +0000779 if (Kind.isBSS())
780 return BSSSection;
781
782 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000783}
784
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000785StringRef TargetLoweringObjectFileCOFF::
786getDepLibFromLinkerOpt(StringRef LinkerOption) const {
787 const char *LibCmd = "/DEFAULTLIB:";
788 if (LinkerOption.startswith(LibCmd))
789 return LinkerOption.substr(strlen(LibCmd));
790 return StringRef();
791}
792
Reid Klecknerd973ca32013-04-25 19:34:41 +0000793void TargetLoweringObjectFileCOFF::
794emitModuleFlags(MCStreamer &Streamer,
795 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000796 Mangler &Mang, const TargetMachine &TM) const {
Reid Klecknerd973ca32013-04-25 19:34:41 +0000797 MDNode *LinkerOptions = 0;
798
799 // Look for the "Linker Options" flag, since it's the only one we support.
800 for (ArrayRef<Module::ModuleFlagEntry>::iterator
801 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
802 const Module::ModuleFlagEntry &MFE = *i;
803 StringRef Key = MFE.Key->getString();
804 Value *Val = MFE.Val;
805 if (Key == "Linker Options") {
806 LinkerOptions = cast<MDNode>(Val);
807 break;
808 }
809 }
810 if (!LinkerOptions)
811 return;
812
813 // Emit the linker options to the linker .drectve section. According to the
814 // spec, this section is a space-separated string containing flags for linker.
815 const MCSection *Sec = getDrectveSection();
816 Streamer.SwitchSection(Sec);
817 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
818 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
819 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
820 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
821 StringRef Op = MDOption->getString();
822 // Lead with a space for consistency with our dllexport implementation.
823 std::string Escaped(" ");
824 if (Op.find(" ") != StringRef::npos) {
825 // The PE-COFF spec says args with spaces must be quoted. It doesn't say
826 // how to escape quotes, but it probably uses this algorithm:
827 // http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx
828 // FIXME: Reuse escaping code from Support/Windows/Program.inc
829 Escaped.push_back('\"');
830 Escaped.append(Op);
831 Escaped.push_back('\"');
832 } else {
833 Escaped.append(Op);
834 }
835 Streamer.EmitBytes(Escaped);
836 }
837 }
838}