blob: 9dbba7c1f76817c0b674d8fff381f7ec3fe8acdd [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"
25#include "llvm/IR/Module.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000026#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000028#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/MC/MCSectionELF.h"
30#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolaa83b1772011-04-16 03:51:21 +000031#include "llvm/MC/MCStreamer.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000032#include "llvm/MC/MCSymbol.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000033#include "llvm/Support/Dwarf.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000034#include "llvm/Support/ELF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/Target/Mangler.h"
38#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 Espindola08704342011-04-27 23:08:15 +000048 Mangler *Mang,
49 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 Espindolae133ed82013-10-29 17:28:26 +000055 return getSymbol(*Mang, GV);
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 Espindolae133ed82013-10-29 17:28:26 +000058 getSymbol(*Mang, GV)->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
Anton Korobeynikove42af362012-11-14 01:47:00 +000090const MCExpr *TargetLoweringObjectFileELF::
91getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang,
92 MachineModuleInfo *MMI, unsigned Encoding,
93 MCStreamer &Streamer) const {
94
95 if (Encoding & dwarf::DW_EH_PE_indirect) {
96 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
97
Rafael Espindolaf4e6b292013-12-02 16:25:47 +000098 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, ".DW.stub");
Anton Korobeynikove42af362012-11-14 01:47:00 +000099
100 // Add information about the stub reference to ELFMMI so that the stub
101 // gets emitted by the asmprinter.
Anton Korobeynikove42af362012-11-14 01:47:00 +0000102 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
103 if (StubSym.getPointer() == 0) {
Rafael Espindolae133ed82013-10-29 17:28:26 +0000104 MCSymbol *Sym = getSymbol(*Mang, GV);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000105 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
106 }
107
108 return TargetLoweringObjectFile::
109 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
110 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
111 }
112
113 return TargetLoweringObjectFile::
114 getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer);
115}
116
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000117static SectionKind
118getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0d018b12011-05-24 03:10:31 +0000119 // N.B.: The defaults used in here are no the same ones used in MC.
120 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
121 // both gas and MC will produce a section with no flags. Given
Bill Wendlingd1634052012-07-19 00:04:14 +0000122 // section(".eh_frame") gcc will produce:
123 //
124 // .section .eh_frame,"a",@progbits
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000125 if (Name.empty() || Name[0] != '.') return K;
126
127 // Some lame default implementation based on some magic section names.
128 if (Name == ".bss" ||
129 Name.startswith(".bss.") ||
130 Name.startswith(".gnu.linkonce.b.") ||
131 Name.startswith(".llvm.linkonce.b.") ||
132 Name == ".sbss" ||
133 Name.startswith(".sbss.") ||
134 Name.startswith(".gnu.linkonce.sb.") ||
135 Name.startswith(".llvm.linkonce.sb."))
136 return SectionKind::getBSS();
137
138 if (Name == ".tdata" ||
139 Name.startswith(".tdata.") ||
140 Name.startswith(".gnu.linkonce.td.") ||
141 Name.startswith(".llvm.linkonce.td."))
142 return SectionKind::getThreadData();
143
144 if (Name == ".tbss" ||
145 Name.startswith(".tbss.") ||
146 Name.startswith(".gnu.linkonce.tb.") ||
147 Name.startswith(".llvm.linkonce.tb."))
148 return SectionKind::getThreadBSS();
149
150 return K;
151}
152
153
154static unsigned getELFSectionType(StringRef Name, SectionKind K) {
155
156 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000157 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000158
159 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000160 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000161
162 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000163 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000164
165 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000166 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000167
Rafael Espindolaaea49582011-01-23 04:28:49 +0000168 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000169}
170
171
172static unsigned
173getELFSectionFlags(SectionKind K) {
174 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
188 // K.isMergeableConst() is left out to honour PR4650
189 if (K.isMergeableCString() || K.isMergeableConst4() ||
190 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000191 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000192
193 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000194 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000195
196 return Flags;
197}
198
199
200const MCSection *TargetLoweringObjectFileELF::
201getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
202 Mangler *Mang, const TargetMachine &TM) const {
203 StringRef SectionName = GV->getSection();
204
205 // Infer section flags from the section name if we can.
206 Kind = getELFKindForNamedSection(SectionName, Kind);
207
Chris Lattner80c34592010-04-08 21:34:17 +0000208 return getContext().getELFSection(SectionName,
209 getELFSectionType(SectionName, Kind),
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000210 getELFSectionFlags(Kind), Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000211}
212
Chris Lattner5b212a32010-04-13 00:36:43 +0000213/// getSectionPrefixForGlobal - Return the section prefix name used by options
214/// FunctionsSections and DataSections.
215static const char *getSectionPrefixForGlobal(SectionKind Kind) {
216 if (Kind.isText()) return ".text.";
217 if (Kind.isReadOnly()) return ".rodata.";
Anton Korobeynikova22828e2012-02-23 10:36:04 +0000218 if (Kind.isBSS()) return ".bss.";
Chris Lattner5b212a32010-04-13 00:36:43 +0000219
220 if (Kind.isThreadData()) return ".tdata.";
221 if (Kind.isThreadBSS()) return ".tbss.";
222
223 if (Kind.isDataNoRel()) return ".data.";
224 if (Kind.isDataRelLocal()) return ".data.rel.local.";
225 if (Kind.isDataRel()) return ".data.rel.";
226 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
227
228 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
229 return ".data.rel.ro.";
230}
231
232
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000233const MCSection *TargetLoweringObjectFileELF::
234SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
235 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner5b212a32010-04-13 00:36:43 +0000236 // If we have -ffunction-section or -fdata-section then we should emit the
237 // global value to a uniqued section specifically for it.
238 bool EmitUniquedSection;
239 if (Kind.isText())
240 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000241 else
Chris Lattner5b212a32010-04-13 00:36:43 +0000242 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000243
244 // If this global is linkonce/weak and the target handles this by emitting it
245 // into a 'uniqued' section name, create and return the section now.
Chris Lattner5b212a32010-04-13 00:36:43 +0000246 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
Anton Korobeynikova22828e2012-02-23 10:36:04 +0000247 !Kind.isCommon()) {
Chris Lattner5b212a32010-04-13 00:36:43 +0000248 const char *Prefix;
Rafael Espindola70d80152011-02-14 22:23:49 +0000249 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner5b212a32010-04-13 00:36:43 +0000250
Chris Lattner2ea586b2010-03-15 20:37:38 +0000251 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Rafael Espindolae133ed82013-10-29 17:28:26 +0000252 MCSymbol *Sym = getSymbol(*Mang, GV);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000253 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola70d80152011-02-14 22:23:49 +0000254 StringRef Group = "";
255 unsigned Flags = getELFSectionFlags(Kind);
256 if (GV->isWeakForLinker()) {
257 Group = Sym->getName();
258 Flags |= ELF::SHF_GROUP;
259 }
260
Chris Lattner80c34592010-04-08 21:34:17 +0000261 return getContext().getELFSection(Name.str(),
262 getELFSectionType(Name.str(), Kind),
Rafael Espindola70d80152011-02-14 22:23:49 +0000263 Flags, Kind, 0, Group);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000264 }
265
266 if (Kind.isText()) return TextSection;
267
268 if (Kind.isMergeable1ByteCString() ||
269 Kind.isMergeable2ByteCString() ||
270 Kind.isMergeable4ByteCString()) {
271
272 // We also need alignment here.
273 // FIXME: this is getting the alignment of the character, not the
274 // alignment of the global!
275 unsigned Align =
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000276 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000277
278 const char *SizeSpec = ".rodata.str1.";
279 if (Kind.isMergeable2ByteCString())
280 SizeSpec = ".rodata.str2.";
281 else if (Kind.isMergeable4ByteCString())
282 SizeSpec = ".rodata.str4.";
283 else
284 assert(Kind.isMergeable1ByteCString() && "unknown string width");
285
286
287 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolaaea49582011-01-23 04:28:49 +0000288 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000289 ELF::SHF_ALLOC |
290 ELF::SHF_MERGE |
291 ELF::SHF_STRINGS,
Chris Lattner80c34592010-04-08 21:34:17 +0000292 Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000293 }
294
295 if (Kind.isMergeableConst()) {
296 if (Kind.isMergeableConst4() && MergeableConst4Section)
297 return MergeableConst4Section;
298 if (Kind.isMergeableConst8() && MergeableConst8Section)
299 return MergeableConst8Section;
300 if (Kind.isMergeableConst16() && MergeableConst16Section)
301 return MergeableConst16Section;
302 return ReadOnlySection; // .const
303 }
304
305 if (Kind.isReadOnly()) return ReadOnlySection;
306
307 if (Kind.isThreadData()) return TLSDataSection;
308 if (Kind.isThreadBSS()) return TLSBSSSection;
309
310 // Note: we claim that common symbols are put in BSSSection, but they are
311 // really emitted with the magic .comm directive, which creates a symbol table
312 // entry but not a section.
313 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
314
315 if (Kind.isDataNoRel()) return DataSection;
316 if (Kind.isDataRelLocal()) return DataRelLocalSection;
317 if (Kind.isDataRel()) return DataRelSection;
318 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
319
320 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
321 return DataRelROSection;
322}
323
324/// getSectionForConstant - Given a mergeable constant with the
325/// specified size and relocation information, return a section that it
326/// should be placed in.
327const MCSection *TargetLoweringObjectFileELF::
328getSectionForConstant(SectionKind Kind) const {
329 if (Kind.isMergeableConst4() && MergeableConst4Section)
330 return MergeableConst4Section;
331 if (Kind.isMergeableConst8() && MergeableConst8Section)
332 return MergeableConst8Section;
333 if (Kind.isMergeableConst16() && MergeableConst16Section)
334 return MergeableConst16Section;
335 if (Kind.isReadOnly())
336 return ReadOnlySection;
337
338 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
339 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
340 return DataRelROSection;
341}
342
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000343const MCSection *
344TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const {
345 // The default scheme is .ctor / .dtor, so we have to invert the priority
346 // numbering.
347 if (Priority == 65535)
348 return StaticCtorSection;
349
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000350 if (UseInitArray) {
351 std::string Name = std::string(".init_array.") + utostr(Priority);
352 return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY,
353 ELF::SHF_ALLOC | ELF::SHF_WRITE,
354 SectionKind::getDataRel());
355 } else {
356 std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
357 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
358 ELF::SHF_ALLOC |ELF::SHF_WRITE,
359 SectionKind::getDataRel());
360 }
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000361}
362
363const MCSection *
364TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
365 // The default scheme is .ctor / .dtor, so we have to invert the priority
366 // numbering.
367 if (Priority == 65535)
368 return StaticDtorSection;
369
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000370 if (UseInitArray) {
371 std::string Name = std::string(".fini_array.") + utostr(Priority);
372 return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY,
373 ELF::SHF_ALLOC | ELF::SHF_WRITE,
374 SectionKind::getDataRel());
375 } else {
376 std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
377 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
378 ELF::SHF_ALLOC |ELF::SHF_WRITE,
379 SectionKind::getDataRel());
380 }
381}
382
383void
384TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
385 UseInitArray = UseInitArray_;
386 if (!UseInitArray)
387 return;
388
389 StaticCtorSection =
390 getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
391 ELF::SHF_WRITE |
392 ELF::SHF_ALLOC,
393 SectionKind::getDataRel());
394 StaticDtorSection =
395 getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
396 ELF::SHF_WRITE |
397 ELF::SHF_ALLOC,
398 SectionKind::getDataRel());
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000399}
400
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000401//===----------------------------------------------------------------------===//
402// MachO
403//===----------------------------------------------------------------------===//
404
Daniel Dunbar95856122013-01-18 19:37:00 +0000405/// emitModuleFlags - Perform code emission for module flags.
Bill Wendling06df7722012-02-14 21:28:13 +0000406void TargetLoweringObjectFileMachO::
Bill Wendling734909a2012-02-15 22:36:15 +0000407emitModuleFlags(MCStreamer &Streamer,
408 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Bill Wendling06df7722012-02-14 21:28:13 +0000409 Mangler *Mang, const TargetMachine &TM) const {
Bill Wendling06df7722012-02-14 21:28:13 +0000410 unsigned VersionVal = 0;
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000411 unsigned ImageInfoFlags = 0;
Daniel Dunbar95856122013-01-18 19:37:00 +0000412 MDNode *LinkerOptions = 0;
Bill Wendling734909a2012-02-15 22:36:15 +0000413 StringRef SectionVal;
Bill Wendling06df7722012-02-14 21:28:13 +0000414
Bill Wendling734909a2012-02-15 22:36:15 +0000415 for (ArrayRef<Module::ModuleFlagEntry>::iterator
416 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
417 const Module::ModuleFlagEntry &MFE = *i;
Bill Wendling06df7722012-02-14 21:28:13 +0000418
419 // Ignore flags with 'Require' behavior.
Bill Wendling734909a2012-02-15 22:36:15 +0000420 if (MFE.Behavior == Module::Require)
Bill Wendling06df7722012-02-14 21:28:13 +0000421 continue;
422
Bill Wendling734909a2012-02-15 22:36:15 +0000423 StringRef Key = MFE.Key->getString();
424 Value *Val = MFE.Val;
Bill Wendling06df7722012-02-14 21:28:13 +0000425
Daniel Dunbar95856122013-01-18 19:37:00 +0000426 if (Key == "Objective-C Image Info Version") {
Bill Wendling06df7722012-02-14 21:28:13 +0000427 VersionVal = cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000428 } else if (Key == "Objective-C Garbage Collection" ||
429 Key == "Objective-C GC Only" ||
430 Key == "Objective-C Is Simulated") {
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000431 ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000432 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling734909a2012-02-15 22:36:15 +0000433 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar95856122013-01-18 19:37:00 +0000434 } else if (Key == "Linker Options") {
435 LinkerOptions = cast<MDNode>(Val);
436 }
437 }
438
439 // Emit the linker options if present.
440 if (LinkerOptions) {
441 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
442 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
443 SmallVector<std::string, 4> StrOptions;
444
445 // Convert to strings.
446 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
447 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
448 StrOptions.push_back(MDOption->getString());
449 }
450
451 Streamer.EmitLinkerOptions(StrOptions);
452 }
Bill Wendling06df7722012-02-14 21:28:13 +0000453 }
454
Bill Wendling734909a2012-02-15 22:36:15 +0000455 // The section is mandatory. If we don't have it, then we don't have GC info.
456 if (SectionVal.empty()) return;
Bill Wendling06df7722012-02-14 21:28:13 +0000457
Bill Wendling734909a2012-02-15 22:36:15 +0000458 StringRef Segment, Section;
459 unsigned TAA = 0, StubSize = 0;
460 bool TAAParsed;
461 std::string ErrorCode =
462 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
463 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000464 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000465 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000466 report_fatal_error("Invalid section specifier '" + Section + "': " +
467 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000468
Bill Wendling734909a2012-02-15 22:36:15 +0000469 // Get the section.
470 const MCSectionMachO *S =
471 getContext().getMachOSection(Segment, Section, TAA, StubSize,
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000472 SectionKind::getDataNoRel());
Bill Wendling734909a2012-02-15 22:36:15 +0000473 Streamer.SwitchSection(S);
474 Streamer.EmitLabel(getContext().
475 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000476 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000477 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000478 Streamer.AddBlankLine();
479}
480
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000481const MCSection *TargetLoweringObjectFileMachO::
482getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
483 Mangler *Mang, const TargetMachine &TM) const {
484 // Parse the section specifier and create it if valid.
485 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000486 unsigned TAA = 0, StubSize = 0;
487 bool TAAParsed;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000488 std::string ErrorCode =
489 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000490 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000491 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000492 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000493 report_fatal_error("Global variable '" + GV->getName() +
494 "' has an invalid section specifier '" +
495 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000496 }
497
498 // Get the section.
499 const MCSectionMachO *S =
Chris Lattner433d4062010-04-08 20:40:11 +0000500 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000501
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000502 // If TAA wasn't set by ParseSectionSpecifier() above,
503 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000504 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000505 TAA = S->getTypeAndAttributes();
506
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000507 // Okay, now that we got the section, verify that the TAA & StubSize agree.
508 // If the user declared multiple globals with different section flags, we need
509 // to reject it here.
510 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000511 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000512 report_fatal_error("Global variable '" + GV->getName() +
513 "' section type or attributes does not match previous"
514 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000515 }
516
517 return S;
518}
519
520const MCSection *TargetLoweringObjectFileMachO::
521SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher6fdea1b2010-05-22 00:10:22 +0000522 Mangler *Mang, const TargetMachine &TM) const {
Bill Wendling25b61db2013-11-17 10:53:13 +0000523
524 // Handle thread local data.
525 if (Kind.isThreadBSS()) return TLSBSSSection;
526 if (Kind.isThreadData()) return TLSDataSection;
527
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000528 if (Kind.isText())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000529 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
530
531 // If this is weak/linkonce, put this in a coalescable section, either in text
532 // or data depending on if it is writable.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000533 if (GV->isWeakForLinker()) {
534 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000535 return ConstTextCoalSection;
536 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000537 }
538
539 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000540 if (Kind.isMergeable1ByteCString() &&
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000541 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000542 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000543
Chris Lattneref2f8042010-03-07 04:28:09 +0000544 // Do not put 16-bit arrays in the UString section if they have an
545 // externally visible label, this runs into issues with certain linker
546 // versions.
547 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000548 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000549 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000550
551 if (Kind.isMergeableConst()) {
552 if (Kind.isMergeableConst4())
553 return FourByteConstantSection;
554 if (Kind.isMergeableConst8())
555 return EightByteConstantSection;
556 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
557 return SixteenByteConstantSection;
558 }
559
560 // Otherwise, if it is readonly, but not something we can specially optimize,
561 // just drop it in .const.
562 if (Kind.isReadOnly())
563 return ReadOnlySection;
564
565 // If this is marked const, put it into a const section. But if the dynamic
566 // linker needs to write to it, put it in the data segment.
567 if (Kind.isReadOnlyWithRel())
568 return ConstDataSection;
569
570 // Put zero initialized globals with strong external linkage in the
571 // DATA, __common section with the .zerofill directive.
572 if (Kind.isBSSExtern())
573 return DataCommonSection;
574
575 // Put zero initialized globals with local linkage in __DATA,__bss directive
576 // with the .zerofill directive (aka .lcomm).
577 if (Kind.isBSSLocal())
578 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000579
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000580 // Otherwise, just drop the variable in the normal data section.
581 return DataSection;
582}
583
584const MCSection *
585TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
586 // If this constant requires a relocation, we have to put it in the data
587 // segment, not in the text segment.
588 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
589 return ConstDataSection;
590
591 if (Kind.isMergeableConst4())
592 return FourByteConstantSection;
593 if (Kind.isMergeableConst8())
594 return EightByteConstantSection;
595 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
596 return SixteenByteConstantSection;
597 return ReadOnlySection; // .const
598}
599
600/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
601/// not to emit the UsedDirective for some symbols in llvm.used.
602// FIXME: REMOVE this (rdar://7071300)
603bool TargetLoweringObjectFileMachO::
604shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
605 /// On Darwin, internally linked data beginning with "L" or "l" does not have
606 /// the directive emitted (this occurs in ObjC metadata).
607 if (!GV) return false;
608
Bill Wendling36321712010-06-29 22:34:52 +0000609 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000610 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
611 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling36321712010-06-29 22:34:52 +0000612 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
613 // this horrible hack can go away.
Rafael Espindolae133ed82013-10-29 17:28:26 +0000614 MCSymbol *Sym = getSymbol(*Mang, GV);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000615 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000616 return false;
617 }
618
619 return true;
620}
621
622const MCExpr *TargetLoweringObjectFileMachO::
Anton Korobeynikove42af362012-11-14 01:47:00 +0000623getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang,
624 MachineModuleInfo *MMI, unsigned Encoding,
625 MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000626 // The mach-o version of this method defaults to returning a stub reference.
627
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000628 if (Encoding & DW_EH_PE_indirect) {
629 MachineModuleInfoMachO &MachOMMI =
630 MMI->getObjFileInfo<MachineModuleInfoMachO>();
631
Rafael Espindolaf4e6b292013-12-02 16:25:47 +0000632 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, "$non_lazy_ptr");
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000633
634 // Add information about the stub reference to MachOMMI so that the stub
635 // gets emitted by the asmprinter.
Bill Wendling57e3aaa2011-10-24 23:05:43 +0000636 MachineModuleInfoImpl::StubValueTy &StubSym =
637 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
638 MachOMMI.getGVStubEntry(SSym);
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000639 if (StubSym.getPointer() == 0) {
Rafael Espindolae133ed82013-10-29 17:28:26 +0000640 MCSymbol *Sym = getSymbol(*Mang, GV);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000641 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000642 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000643
644 return TargetLoweringObjectFile::
Anton Korobeynikove42af362012-11-14 01:47:00 +0000645 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
646 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000647 }
648
649 return TargetLoweringObjectFile::
Anton Korobeynikove42af362012-11-14 01:47:00 +0000650 getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000651}
652
Rafael Espindola08704342011-04-27 23:08:15 +0000653MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindolace83fc32011-04-27 23:17:57 +0000654getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola08704342011-04-27 23:08:15 +0000655 MachineModuleInfo *MMI) const {
656 // The mach-o version of this method defaults to returning a stub reference.
657 MachineModuleInfoMachO &MachOMMI =
658 MMI->getObjFileInfo<MachineModuleInfoMachO>();
659
Rafael Espindolaf4e6b292013-12-02 16:25:47 +0000660 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, "$non_lazy_ptr");
Rafael Espindola08704342011-04-27 23:08:15 +0000661
662 // Add information about the stub reference to MachOMMI so that the stub
663 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000664 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Rafael Espindola08704342011-04-27 23:08:15 +0000665 if (StubSym.getPointer() == 0) {
Rafael Espindolae133ed82013-10-29 17:28:26 +0000666 MCSymbol *Sym = getSymbol(*Mang, GV);
Rafael Espindola08704342011-04-27 23:08:15 +0000667 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
668 }
669
670 return SSym;
671}
672
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000673//===----------------------------------------------------------------------===//
674// COFF
675//===----------------------------------------------------------------------===//
676
Chris Lattner87cffa92010-05-07 17:17:41 +0000677static unsigned
678getCOFFSectionFlags(SectionKind K) {
679 unsigned Flags = 0;
680
Anton Korobeynikove4152302010-07-06 15:24:56 +0000681 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000682 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000683 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000684 else if (K.isText())
685 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000686 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000687 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar329d2022010-07-01 20:07:24 +0000688 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner02844932010-05-07 21:49:09 +0000689 else if (K.isBSS ())
690 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000691 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
692 COFF::IMAGE_SCN_MEM_READ |
693 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000694 else if (K.isThreadLocal())
695 Flags |=
696 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
697 COFF::IMAGE_SCN_MEM_READ |
698 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000699 else if (K.isReadOnly())
700 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000701 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
702 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000703 else if (K.isWriteable())
704 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000705 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
706 COFF::IMAGE_SCN_MEM_READ |
707 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000708
709 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000710}
711
712const MCSection *TargetLoweringObjectFileCOFF::
713getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
714 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000715 int Selection = 0;
716 unsigned Characteristics = getCOFFSectionFlags(Kind);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000717 StringRef Name = GV->getSection();
718 StringRef COMDATSymName = "";
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000719 if (GV->isWeakForLinker()) {
720 Selection = COFF::IMAGE_COMDAT_SELECT_ANY;
721 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000722 MCSymbol *Sym = getSymbol(*Mang, GV);
723 COMDATSymName = Sym->getName();
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000724 }
725 return getContext().getCOFFSection(Name,
726 Characteristics,
Nico Riecka37acf72013-07-06 12:13:10 +0000727 Kind,
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000728 COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +0000729 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000730}
731
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000732static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000733 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000734 return ".text";
Chris Lattner02844932010-05-07 21:49:09 +0000735 if (Kind.isBSS ())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000736 return ".bss";
737 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +0000738 return ".tls$";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000739 if (Kind.isWriteable())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000740 return ".data";
741 return ".rdata";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000742}
743
744
745const MCSection *TargetLoweringObjectFileCOFF::
746SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
747 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000748
749 // If this global is linkonce/weak and the target handles this by emitting it
750 // into a 'uniqued' section name, create and return the section now.
751 if (GV->isWeakForLinker()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000752 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Chris Lattner02844932010-05-07 21:49:09 +0000753 unsigned Characteristics = getCOFFSectionFlags(Kind);
754
Daniel Dunbar329d2022010-07-01 20:07:24 +0000755 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000756 MCSymbol *Sym = getSymbol(*Mang, GV);
757 return getContext().getCOFFSection(Name, Characteristics,
758 Kind, Sym->getName(),
759 COFF::IMAGE_COMDAT_SELECT_ANY);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000760 }
761
762 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +0000763 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000764
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000765 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +0000766 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000767
David Majnemer3d96acb2013-08-13 01:23:53 +0000768 if (Kind.isReadOnly())
David Majnemerf76d6b32013-08-08 01:50:52 +0000769 return ReadOnlySection;
770
David Majnemer3d96acb2013-08-13 01:23:53 +0000771 if (Kind.isBSS())
772 return BSSSection;
773
774 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000775}
776
Reid Klecknerd973ca32013-04-25 19:34:41 +0000777void TargetLoweringObjectFileCOFF::
778emitModuleFlags(MCStreamer &Streamer,
779 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
780 Mangler *Mang, const TargetMachine &TM) const {
781 MDNode *LinkerOptions = 0;
782
783 // Look for the "Linker Options" flag, since it's the only one we support.
784 for (ArrayRef<Module::ModuleFlagEntry>::iterator
785 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
786 const Module::ModuleFlagEntry &MFE = *i;
787 StringRef Key = MFE.Key->getString();
788 Value *Val = MFE.Val;
789 if (Key == "Linker Options") {
790 LinkerOptions = cast<MDNode>(Val);
791 break;
792 }
793 }
794 if (!LinkerOptions)
795 return;
796
797 // Emit the linker options to the linker .drectve section. According to the
798 // spec, this section is a space-separated string containing flags for linker.
799 const MCSection *Sec = getDrectveSection();
800 Streamer.SwitchSection(Sec);
801 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
802 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
803 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
804 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
805 StringRef Op = MDOption->getString();
806 // Lead with a space for consistency with our dllexport implementation.
807 std::string Escaped(" ");
808 if (Op.find(" ") != StringRef::npos) {
809 // The PE-COFF spec says args with spaces must be quoted. It doesn't say
810 // how to escape quotes, but it probably uses this algorithm:
811 // http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx
812 // FIXME: Reuse escaping code from Support/Windows/Program.inc
813 Escaped.push_back('\"');
814 Escaped.append(Op);
815 Escaped.push_back('\"');
816 } else {
817 Escaped.append(Op);
818 }
819 Streamer.EmitBytes(Escaped);
820 }
821 }
822}