blob: dda22599d252ecb33b27106a62211dd0432e0c14 [file] [log] [blame]
Anton Korobeynikov362dd0b2010-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 Carruthd04a8d42012-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 Carruth0b8c9a82013-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"
Stephen Hines36b56882014-04-23 16:57:46 -070025#include "llvm/IR/Mangler.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/Module.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000027#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCExpr.h"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000029#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/MC/MCSectionELF.h"
31#include "llvm/MC/MCSectionMachO.h"
Rafael Espindola30deafc2011-04-16 03:51:21 +000032#include "llvm/MC/MCStreamer.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000033#include "llvm/MC/MCSymbol.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000034#include "llvm/Support/Dwarf.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000035#include "llvm/Support/ELF.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Stephen Hines36b56882014-04-23 16:57:46 -070038#include "llvm/Target/TargetLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000039#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000040using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000041using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000042
43//===----------------------------------------------------------------------===//
44// ELF
45//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000046
Stephen Hines36b56882014-04-23 16:57:46 -070047MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
48 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
49 MachineModuleInfo *MMI) const {
Rafael Espindola60246a92011-04-27 23:17:57 +000050 unsigned Encoding = getPersonalityEncoding();
Rafael Espindola7afec9c2011-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:
Stephen Hines36b56882014-04-23 16:57:46 -070055 return TM.getSymbol(GV, Mang);
Rafael Espindola7afec9c2011-04-27 23:08:15 +000056 case dwarf::DW_EH_PE_pcrel: {
Rafael Espindolafb66f472011-06-13 03:09:13 +000057 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
Stephen Hines36b56882014-04-23 16:57:46 -070058 TM.getSymbol(GV, Mang)->getName());
Rafael Espindola7afec9c2011-04-27 23:08:15 +000059 }
60 }
Rafael Espindola30deafc2011-04-16 03:51:21 +000061}
62
63void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
64 const TargetMachine &TM,
Rafael Espindola7afec9c2011-04-27 23:08:15 +000065 const MCSymbol *Sym) const {
Rafael Espindolafb66f472011-06-13 03:09:13 +000066 SmallString<64> NameData("DW.ref.");
67 NameData += Sym->getName();
68 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
Rafael Espindola018e38c2011-04-27 21:29:52 +000069 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
70 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindolafb66f472011-06-13 03:09:13 +000071 StringRef Prefix = ".data.";
72 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola018e38c2011-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 Carruth426c2bf2012-11-01 09:14:31 +000079 unsigned Size = TM.getDataLayout()->getPointerSize();
Rafael Espindola018e38c2011-04-27 21:29:52 +000080 Streamer.SwitchSection(Sec);
Chandler Carruth426c2bf2012-11-01 09:14:31 +000081 Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment());
Rafael Espindola018e38c2011-04-27 21:29:52 +000082 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
David Chisnallca5b7522012-02-17 16:05:50 +000083 const MCExpr *E = MCConstantExpr::Create(Size, getContext());
Rafael Espindola018e38c2011-04-27 21:29:52 +000084 Streamer.EmitELFSize(Label, E);
85 Streamer.EmitLabel(Label);
Rafael Espindola30deafc2011-04-16 03:51:21 +000086
Rafael Espindola018e38c2011-04-27 21:29:52 +000087 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindola30deafc2011-04-16 03:51:21 +000088}
89
Stephen Hines36b56882014-04-23 16:57:46 -070090const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
91 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
92 const TargetMachine &TM, MachineModuleInfo *MMI,
93 MCStreamer &Streamer) const {
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +000094
95 if (Encoding & dwarf::DW_EH_PE_indirect) {
96 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
97
Stephen Hines36b56882014-04-23 16:57:46 -070098 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM);
Anton Korobeynikov25efd6d2012-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 Korobeynikov25efd6d2012-11-14 01:47:00 +0000102 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700103 if (!StubSym.getPointer()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700104 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Anton Korobeynikov25efd6d2012-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::
Stephen Hines36b56882014-04-23 16:57:46 -0700114 getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer);
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000115}
116
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000117static SectionKind
118getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindolae6657982011-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 Wendling96cb1122012-07-19 00:04:14 +0000122 // section(".eh_frame") gcc will produce:
123 //
124 // .section .eh_frame,"a",@progbits
Anton Korobeynikov362dd0b2010-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 Espindolac85dca62011-01-23 04:28:49 +0000157 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000158
159 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000160 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000161
162 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000163 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000164
165 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000166 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000167
Rafael Espindolac85dca62011-01-23 04:28:49 +0000168 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000169}
170
171
172static unsigned
173getELFSectionFlags(SectionKind K) {
174 unsigned Flags = 0;
175
176 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000177 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000178
179 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000180 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000181
Rafael Espindolad846e3f2011-06-07 23:26:45 +0000182 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000183 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000184
185 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000186 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-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 Espindola1c130262011-01-23 04:43:11 +0000191 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000192
193 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000194 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000195
196 return Flags;
197}
198
Stephen Hines36b56882014-04-23 16:57:46 -0700199const MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
200 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
201 const TargetMachine &TM) const {
Anton Korobeynikov362dd0b2010-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 Lattner287df1b2010-04-08 21:34:17 +0000207 return getContext().getELFSection(SectionName,
208 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000209 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000210}
211
Chris Lattner43ac7212010-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 Korobeynikov1d2d5a02012-02-23 10:36:04 +0000217 if (Kind.isBSS()) return ".bss.";
Chris Lattner43ac7212010-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 Korobeynikov362dd0b2010-02-15 22:37:53 +0000232const MCSection *TargetLoweringObjectFileELF::
233SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Stephen Hines36b56882014-04-23 16:57:46 -0700234 Mangler &Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-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. Spencer579d7a32010-10-27 18:52:20 +0000240 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000241 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-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 Lattner43ac7212010-04-13 00:36:43 +0000245 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
Anton Korobeynikov1d2d5a02012-02-23 10:36:04 +0000246 !Kind.isCommon()) {
Chris Lattner43ac7212010-04-13 00:36:43 +0000247 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000248 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000249
Chris Lattner4c6741f2010-03-15 20:37:38 +0000250 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Stephen Hines36b56882014-04-23 16:57:46 -0700251 TM.getNameWithPrefix(Name, GV, Mang, true);
252
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000253 StringRef Group = "";
254 unsigned Flags = getELFSectionFlags(Kind);
255 if (GV->isWeakForLinker()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700256 Group = Name.substr(strlen(Prefix));
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000257 Flags |= ELF::SHF_GROUP;
258 }
259
Chris Lattner287df1b2010-04-08 21:34:17 +0000260 return getContext().getELFSection(Name.str(),
261 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000262 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-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 Villmow3574eca2012-10-08 16:38:25 +0000275 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov362dd0b2010-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 Espindolac85dca62011-01-23 04:28:49 +0000287 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000288 ELF::SHF_ALLOC |
289 ELF::SHF_MERGE |
290 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000291 Kind);
Anton Korobeynikov362dd0b2010-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
Stephen Hinesdce4a402014-05-29 02:49:00 -0700342const MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
343 unsigned Priority, const MCSymbol *KeySym, const MCSection *KeySec) const {
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000344 // The default scheme is .ctor / .dtor, so we have to invert the priority
345 // numbering.
346 if (Priority == 65535)
347 return StaticCtorSection;
348
Rafael Espindolad6b43a32012-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 Korobeynikov4a99f592012-01-25 22:24:19 +0000360}
361
Stephen Hinesdce4a402014-05-29 02:49:00 -0700362const MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
363 unsigned Priority, const MCSymbol *KeySym, const MCSection *KeySec) const {
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000364 // The default scheme is .ctor / .dtor, so we have to invert the priority
365 // numbering.
366 if (Priority == 65535)
367 return StaticDtorSection;
368
Rafael Espindolad6b43a32012-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 Korobeynikov4a99f592012-01-25 22:24:19 +0000398}
399
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000400//===----------------------------------------------------------------------===//
401// MachO
402//===----------------------------------------------------------------------===//
403
Stephen Hines36b56882014-04-23 16:57:46 -0700404/// 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 Dunbar6d49b682013-01-18 19:37:00 +0000414/// emitModuleFlags - Perform code emission for module flags.
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000415void TargetLoweringObjectFileMachO::
Bill Wendling057d5212012-02-15 22:36:15 +0000416emitModuleFlags(MCStreamer &Streamer,
417 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Stephen Hines36b56882014-04-23 16:57:46 -0700418 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000419 unsigned VersionVal = 0;
Bill Wendlingadb082c2012-04-24 11:03:50 +0000420 unsigned ImageInfoFlags = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700421 MDNode *LinkerOptions = nullptr;
Bill Wendling057d5212012-02-15 22:36:15 +0000422 StringRef SectionVal;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000423
Bill Wendling057d5212012-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 Wendlingb464d3f2012-02-14 21:28:13 +0000427
428 // Ignore flags with 'Require' behavior.
Bill Wendling057d5212012-02-15 22:36:15 +0000429 if (MFE.Behavior == Module::Require)
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000430 continue;
431
Bill Wendling057d5212012-02-15 22:36:15 +0000432 StringRef Key = MFE.Key->getString();
433 Value *Val = MFE.Val;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000434
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000435 if (Key == "Objective-C Image Info Version") {
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000436 VersionVal = cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar6d49b682013-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 Wendlingadb082c2012-04-24 11:03:50 +0000440 ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000441 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling057d5212012-02-15 22:36:15 +0000442 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar6d49b682013-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 Wendlingb464d3f2012-02-14 21:28:13 +0000462 }
463
Bill Wendling057d5212012-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 Wendlingb464d3f2012-02-14 21:28:13 +0000466
Bill Wendling057d5212012-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 Wendling2de3ff52012-02-15 22:47:53 +0000473 if (!ErrorCode.empty())
Bill Wendling057d5212012-02-15 22:36:15 +0000474 // If invalid, report the error with report_fatal_error.
Bill Wendling2de3ff52012-02-15 22:47:53 +0000475 report_fatal_error("Invalid section specifier '" + Section + "': " +
476 ErrorCode + ".");
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000477
Bill Wendling057d5212012-02-15 22:36:15 +0000478 // Get the section.
479 const MCSectionMachO *S =
480 getContext().getMachOSection(Segment, Section, TAA, StubSize,
Bill Wendling2de3ff52012-02-15 22:47:53 +0000481 SectionKind::getDataNoRel());
Bill Wendling057d5212012-02-15 22:36:15 +0000482 Streamer.SwitchSection(S);
483 Streamer.EmitLabel(getContext().
484 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000485 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingadb082c2012-04-24 11:03:50 +0000486 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000487 Streamer.AddBlankLine();
488}
489
Stephen Hines36b56882014-04-23 16:57:46 -0700490const MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
491 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
492 const TargetMachine &TM) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000493 // Parse the section specifier and create it if valid.
494 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000495 unsigned TAA = 0, StubSize = 0;
496 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000497 std::string ErrorCode =
498 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000499 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000500 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000501 // If invalid, report the error with report_fatal_error.
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000502 report_fatal_error("Global variable '" + GV->getName() +
503 "' has an invalid section specifier '" +
504 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000505 }
506
507 // Get the section.
508 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000509 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000510
Stuart Hastings6ad82d82011-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 Hastings65c8bca2011-03-19 02:42:31 +0000513 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000514 TAA = S->getTypeAndAttributes();
515
Anton Korobeynikov362dd0b2010-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 Lattner75361b62010-04-07 22:58:41 +0000520 // If invalid, report the error with report_fatal_error.
Benjamin Kramera7b0cb72011-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 Korobeynikov362dd0b2010-02-15 22:37:53 +0000524 }
525
526 return S;
527}
528
Stephen Hines36b56882014-04-23 16:57:46 -0700529bool TargetLoweringObjectFileMachO::isSectionAtomizableBySymbols(
530 const MCSection &Section) const {
531 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
532
533 // Sections holding 1 byte strings are atomized based on the data
534 // they contain.
535 // Sections holding 2 byte strings require symbols in order to be
536 // atomized.
537 // There is no dedicated section for 4 byte strings.
538 if (SMO.getKind().isMergeable1ByteCString())
539 return false;
540
541 if (SMO.getSegmentName() == "__DATA" &&
542 SMO.getSectionName() == "__cfstring")
543 return false;
544
545 switch (SMO.getType()) {
546 default:
547 return true;
548
549 // These sections are atomized at the element boundaries without using
550 // symbols.
551 case MachO::S_4BYTE_LITERALS:
552 case MachO::S_8BYTE_LITERALS:
553 case MachO::S_16BYTE_LITERALS:
554 case MachO::S_LITERAL_POINTERS:
555 case MachO::S_NON_LAZY_SYMBOL_POINTERS:
556 case MachO::S_LAZY_SYMBOL_POINTERS:
557 case MachO::S_MOD_INIT_FUNC_POINTERS:
558 case MachO::S_MOD_TERM_FUNC_POINTERS:
559 case MachO::S_INTERPOSING:
560 return false;
561 }
562}
563
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000564const MCSection *TargetLoweringObjectFileMachO::
565SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Stephen Hines36b56882014-04-23 16:57:46 -0700566 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendlingfe128c62013-11-17 10:53:13 +0000567
568 // Handle thread local data.
569 if (Kind.isThreadBSS()) return TLSBSSSection;
570 if (Kind.isThreadData()) return TLSDataSection;
571
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000572 if (Kind.isText())
Arnold Schwaighofer9b19dfc2013-08-08 21:04:16 +0000573 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
574
575 // If this is weak/linkonce, put this in a coalescable section, either in text
576 // or data depending on if it is writable.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000577 if (GV->isWeakForLinker()) {
578 if (Kind.isReadOnly())
Arnold Schwaighofer9b19dfc2013-08-08 21:04:16 +0000579 return ConstTextCoalSection;
580 return DataCoalSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000581 }
582
583 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000584 if (Kind.isMergeable1ByteCString() &&
Micah Villmow3574eca2012-10-08 16:38:25 +0000585 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattner98f15d22010-03-07 04:28:09 +0000586 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000587
Chris Lattner98f15d22010-03-07 04:28:09 +0000588 // Do not put 16-bit arrays in the UString section if they have an
589 // externally visible label, this runs into issues with certain linker
590 // versions.
591 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Micah Villmow3574eca2012-10-08 16:38:25 +0000592 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattner98f15d22010-03-07 04:28:09 +0000593 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000594
595 if (Kind.isMergeableConst()) {
596 if (Kind.isMergeableConst4())
597 return FourByteConstantSection;
598 if (Kind.isMergeableConst8())
599 return EightByteConstantSection;
Stephen Hines36b56882014-04-23 16:57:46 -0700600 if (Kind.isMergeableConst16())
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000601 return SixteenByteConstantSection;
602 }
603
604 // Otherwise, if it is readonly, but not something we can specially optimize,
605 // just drop it in .const.
606 if (Kind.isReadOnly())
607 return ReadOnlySection;
608
609 // If this is marked const, put it into a const section. But if the dynamic
610 // linker needs to write to it, put it in the data segment.
611 if (Kind.isReadOnlyWithRel())
612 return ConstDataSection;
613
614 // Put zero initialized globals with strong external linkage in the
615 // DATA, __common section with the .zerofill directive.
616 if (Kind.isBSSExtern())
617 return DataCommonSection;
618
619 // Put zero initialized globals with local linkage in __DATA,__bss directive
620 // with the .zerofill directive (aka .lcomm).
621 if (Kind.isBSSLocal())
622 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000623
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000624 // Otherwise, just drop the variable in the normal data section.
625 return DataSection;
626}
627
628const MCSection *
629TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
630 // If this constant requires a relocation, we have to put it in the data
631 // segment, not in the text segment.
632 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
633 return ConstDataSection;
634
635 if (Kind.isMergeableConst4())
636 return FourByteConstantSection;
637 if (Kind.isMergeableConst8())
638 return EightByteConstantSection;
Stephen Hines36b56882014-04-23 16:57:46 -0700639 if (Kind.isMergeableConst16())
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000640 return SixteenByteConstantSection;
641 return ReadOnlySection; // .const
642}
643
Stephen Hines36b56882014-04-23 16:57:46 -0700644const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
645 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
646 const TargetMachine &TM, MachineModuleInfo *MMI,
647 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000648 // The mach-o version of this method defaults to returning a stub reference.
649
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000650 if (Encoding & DW_EH_PE_indirect) {
651 MachineModuleInfoMachO &MachOMMI =
652 MMI->getObjFileInfo<MachineModuleInfoMachO>();
653
Stephen Hines36b56882014-04-23 16:57:46 -0700654 MCSymbol *SSym =
655 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000656
657 // Add information about the stub reference to MachOMMI so that the stub
658 // gets emitted by the asmprinter.
Bill Wendling67121542011-10-24 23:05:43 +0000659 MachineModuleInfoImpl::StubValueTy &StubSym =
660 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
661 MachOMMI.getGVStubEntry(SSym);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700662 if (!StubSym.getPointer()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700663 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Chris Lattner4c6741f2010-03-15 20:37:38 +0000664 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000665 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000666
667 return TargetLoweringObjectFile::
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000668 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
669 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000670 }
671
Stephen Hines36b56882014-04-23 16:57:46 -0700672 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang,
673 TM, MMI, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000674}
675
Stephen Hines36b56882014-04-23 16:57:46 -0700676MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
677 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
678 MachineModuleInfo *MMI) const {
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000679 // The mach-o version of this method defaults to returning a stub reference.
680 MachineModuleInfoMachO &MachOMMI =
681 MMI->getObjFileInfo<MachineModuleInfoMachO>();
682
Stephen Hines36b56882014-04-23 16:57:46 -0700683 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000684
685 // Add information about the stub reference to MachOMMI so that the stub
686 // gets emitted by the asmprinter.
Bill Wendlingd7c24942011-11-29 01:43:20 +0000687 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700688 if (!StubSym.getPointer()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700689 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000690 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
691 }
692
693 return SSym;
694}
695
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000696//===----------------------------------------------------------------------===//
697// COFF
698//===----------------------------------------------------------------------===//
699
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000700static unsigned
701getCOFFSectionFlags(SectionKind K) {
702 unsigned Flags = 0;
703
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000704 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000705 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000706 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000707 else if (K.isText())
708 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000709 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000710 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000711 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000712 else if (K.isBSS ())
713 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000714 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
715 COFF::IMAGE_SCN_MEM_READ |
716 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000717 else if (K.isThreadLocal())
718 Flags |=
719 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
720 COFF::IMAGE_SCN_MEM_READ |
721 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000722 else if (K.isReadOnly())
723 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000724 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
725 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000726 else if (K.isWriteable())
727 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000728 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
729 COFF::IMAGE_SCN_MEM_READ |
730 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000731
732 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000733}
734
Stephen Hines36b56882014-04-23 16:57:46 -0700735const MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
736 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
737 const TargetMachine &TM) const {
Michael J. Spencer4de58722012-11-13 22:04:09 +0000738 int Selection = 0;
739 unsigned Characteristics = getCOFFSectionFlags(Kind);
Stephen Hines36b56882014-04-23 16:57:46 -0700740 StringRef Name = GV->getSection();
741 StringRef COMDATSymName = "";
Michael J. Spencer4de58722012-11-13 22:04:09 +0000742 if (GV->isWeakForLinker()) {
743 Selection = COFF::IMAGE_COMDAT_SELECT_ANY;
744 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Stephen Hines36b56882014-04-23 16:57:46 -0700745 MCSymbol *Sym = TM.getSymbol(GV, Mang);
746 COMDATSymName = Sym->getName();
Michael J. Spencer4de58722012-11-13 22:04:09 +0000747 }
748 return getContext().getCOFFSection(Name,
749 Characteristics,
Nico Rieck80646282013-07-06 12:13:10 +0000750 Kind,
Stephen Hines36b56882014-04-23 16:57:46 -0700751 COMDATSymName,
Nico Rieck80646282013-07-06 12:13:10 +0000752 Selection);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000753}
754
Stephen Hines36b56882014-04-23 16:57:46 -0700755static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000756 if (Kind.isText())
Stephen Hines36b56882014-04-23 16:57:46 -0700757 return ".text";
Stephen Hinesdce4a402014-05-29 02:49:00 -0700758 if (Kind.isBSS())
Stephen Hines36b56882014-04-23 16:57:46 -0700759 return ".bss";
760 if (Kind.isThreadLocal())
761 return ".tls$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000762 if (Kind.isWriteable())
Stephen Hines36b56882014-04-23 16:57:46 -0700763 return ".data";
764 return ".rdata";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000765}
766
767
768const MCSection *TargetLoweringObjectFileCOFF::
769SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Stephen Hines36b56882014-04-23 16:57:46 -0700770 Mangler &Mang, const TargetMachine &TM) const {
771 // If we have -ffunction-sections then we should emit the global value to a
772 // uniqued section specifically for it.
773 bool EmitUniquedSection;
774 if (Kind.isText())
775 EmitUniquedSection = TM.getFunctionSections();
776 else
777 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000778
779 // If this global is linkonce/weak and the target handles this by emitting it
780 // into a 'uniqued' section name, create and return the section now.
Stephen Hines36b56882014-04-23 16:57:46 -0700781 // Section names depend on the name of the symbol which is not feasible if the
782 // symbol has private linkage.
783 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
Stephen Hinesdce4a402014-05-29 02:49:00 -0700784 !GV->hasPrivateLinkage() && !Kind.isCommon()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700785 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Chris Lattner6e5ce282010-05-07 21:49:09 +0000786 unsigned Characteristics = getCOFFSectionFlags(Kind);
787
Daniel Dunbar94610582010-07-01 20:07:24 +0000788 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Stephen Hines36b56882014-04-23 16:57:46 -0700789 MCSymbol *Sym = TM.getSymbol(GV, Mang);
790 return getContext().getCOFFSection(
791 Name, Characteristics, Kind, Sym->getName(),
792 GV->isWeakForLinker() ? COFF::IMAGE_COMDAT_SELECT_ANY
793 : COFF::IMAGE_COMDAT_SELECT_NODUPLICATES);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000794 }
795
796 if (Kind.isText())
David Majnemer6aa93152013-08-13 01:23:53 +0000797 return TextSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000798
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000799 if (Kind.isThreadLocal())
David Majnemer6aa93152013-08-13 01:23:53 +0000800 return TLSDataSection;
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000801
David Majnemer6aa93152013-08-13 01:23:53 +0000802 if (Kind.isReadOnly())
David Majnemer9706d432013-08-08 01:50:52 +0000803 return ReadOnlySection;
804
Stephen Hinesdce4a402014-05-29 02:49:00 -0700805 // Note: we claim that common symbols are put in BSSSection, but they are
806 // really emitted with the magic .comm directive, which creates a symbol table
807 // entry but not a section.
808 if (Kind.isBSS() || Kind.isCommon())
David Majnemer6aa93152013-08-13 01:23:53 +0000809 return BSSSection;
810
811 return DataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000812}
813
Stephen Hines36b56882014-04-23 16:57:46 -0700814StringRef TargetLoweringObjectFileCOFF::
815getDepLibFromLinkerOpt(StringRef LinkerOption) const {
816 const char *LibCmd = "/DEFAULTLIB:";
817 if (LinkerOption.startswith(LibCmd))
818 return LinkerOption.substr(strlen(LibCmd));
819 return StringRef();
820}
821
Reid Kleckner4b558152013-04-25 19:34:41 +0000822void TargetLoweringObjectFileCOFF::
823emitModuleFlags(MCStreamer &Streamer,
824 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Stephen Hines36b56882014-04-23 16:57:46 -0700825 Mangler &Mang, const TargetMachine &TM) const {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700826 MDNode *LinkerOptions = nullptr;
Reid Kleckner4b558152013-04-25 19:34:41 +0000827
828 // Look for the "Linker Options" flag, since it's the only one we support.
829 for (ArrayRef<Module::ModuleFlagEntry>::iterator
830 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
831 const Module::ModuleFlagEntry &MFE = *i;
832 StringRef Key = MFE.Key->getString();
833 Value *Val = MFE.Val;
834 if (Key == "Linker Options") {
835 LinkerOptions = cast<MDNode>(Val);
836 break;
837 }
838 }
839 if (!LinkerOptions)
840 return;
841
842 // Emit the linker options to the linker .drectve section. According to the
843 // spec, this section is a space-separated string containing flags for linker.
844 const MCSection *Sec = getDrectveSection();
845 Streamer.SwitchSection(Sec);
846 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
847 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
848 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
849 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
850 StringRef Op = MDOption->getString();
851 // Lead with a space for consistency with our dllexport implementation.
852 std::string Escaped(" ");
853 if (Op.find(" ") != StringRef::npos) {
854 // The PE-COFF spec says args with spaces must be quoted. It doesn't say
855 // how to escape quotes, but it probably uses this algorithm:
856 // http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx
857 // FIXME: Reuse escaping code from Support/Windows/Program.inc
858 Escaped.push_back('\"');
859 Escaped.append(Op);
860 Escaped.push_back('\"');
861 } else {
862 Escaped.append(Op);
863 }
864 Streamer.EmitBytes(Escaped);
865 }
866 }
867}
Stephen Hinesdce4a402014-05-29 02:49:00 -0700868
869static const MCSection *getAssociativeCOFFSection(MCContext &Ctx,
870 const MCSection *Sec,
871 const MCSymbol *KeySym,
872 const MCSection *KeySec) {
873 // Return the normal section if we don't have to be associative.
874 if (!KeySym)
875 return Sec;
876
877 // Make an associative section with the same name and kind as the normal
878 // section.
879 const MCSectionCOFF *SecCOFF = cast<MCSectionCOFF>(Sec);
880 const MCSectionCOFF *KeySecCOFF = cast<MCSectionCOFF>(KeySec);
881 unsigned Characteristics =
882 SecCOFF->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
883 return Ctx.getCOFFSection(SecCOFF->getSectionName(), Characteristics,
884 SecCOFF->getKind(), KeySym->getName(),
885 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, KeySecCOFF);
886}
887
888const MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
889 unsigned Priority, const MCSymbol *KeySym, const MCSection *KeySec) const {
890 return getAssociativeCOFFSection(getContext(), StaticCtorSection, KeySym, KeySec);
891}
892
893const MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
894 unsigned Priority, const MCSymbol *KeySym, const MCSection *KeySec) const {
895 return getAssociativeCOFFSection(getContext(), StaticDtorSection, KeySym, KeySec);
896}