blob: 02abc282e6d6d14ab359545c6d99bc26e62a0d87 [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"
Rafael Espindoladaeafb42014-02-19 17:23:20 +000038#include "llvm/Target/TargetLowering.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000039#include "llvm/Target/TargetMachine.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000040using namespace llvm;
Anton Korobeynikov31a92122010-02-21 20:28:15 +000041using namespace dwarf;
Anton Korobeynikovab663a02010-02-15 22:37:53 +000042
43//===----------------------------------------------------------------------===//
44// ELF
45//===----------------------------------------------------------------------===//
Anton Korobeynikovab663a02010-02-15 22:37:53 +000046
Rafael Espindoladaeafb42014-02-19 17:23:20 +000047MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
48 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
49 MachineModuleInfo *MMI) const {
Rafael Espindolace83fc32011-04-27 23:17:57 +000050 unsigned Encoding = getPersonalityEncoding();
Logan Chienc0029812014-05-30 16:48:56 +000051 if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect)
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000052 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000053 TM.getSymbol(GV, Mang)->getName());
Logan Chienc0029812014-05-30 16:48:56 +000054 if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr)
55 return TM.getSymbol(GV, Mang);
56 report_fatal_error("We do not support this DWARF encoding yet!");
Rafael Espindolaa83b1772011-04-16 03:51:21 +000057}
58
59void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
60 const TargetMachine &TM,
Rafael Espindola08704342011-04-27 23:08:15 +000061 const MCSymbol *Sym) const {
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000062 SmallString<64> NameData("DW.ref.");
63 NameData += Sym->getName();
64 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
Rafael Espindola39897762011-04-27 21:29:52 +000065 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
66 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000067 StringRef Prefix = ".data.";
68 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola39897762011-04-27 21:29:52 +000069 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
70 const MCSection *Sec = getContext().getELFSection(NameData,
71 ELF::SHT_PROGBITS,
72 Flags,
73 SectionKind::getDataRel(),
74 0, Label->getName());
Chandler Carruth5da3f052012-11-01 09:14:31 +000075 unsigned Size = TM.getDataLayout()->getPointerSize();
Rafael Espindola39897762011-04-27 21:29:52 +000076 Streamer.SwitchSection(Sec);
Chandler Carruth5da3f052012-11-01 09:14:31 +000077 Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment());
Rafael Espindola39897762011-04-27 21:29:52 +000078 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
David Chisnall8fa17162012-02-17 16:05:50 +000079 const MCExpr *E = MCConstantExpr::Create(Size, getContext());
Rafael Espindola39897762011-04-27 21:29:52 +000080 Streamer.EmitELFSize(Label, E);
81 Streamer.EmitLabel(Label);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000082
Rafael Espindola39897762011-04-27 21:29:52 +000083 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000084}
85
Rafael Espindola15b26692014-02-09 14:50:44 +000086const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
87 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +000088 const TargetMachine &TM, MachineModuleInfo *MMI,
89 MCStreamer &Streamer) const {
Anton Korobeynikove42af362012-11-14 01:47:00 +000090
91 if (Encoding & dwarf::DW_EH_PE_indirect) {
92 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
93
Rafael Espindoladaeafb42014-02-19 17:23:20 +000094 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM);
Anton Korobeynikove42af362012-11-14 01:47:00 +000095
96 // Add information about the stub reference to ELFMMI so that the stub
97 // gets emitted by the asmprinter.
Anton Korobeynikove42af362012-11-14 01:47:00 +000098 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +000099 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000100 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000101 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
102 }
103
104 return TargetLoweringObjectFile::
105 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
106 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
107 }
108
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000109 return TargetLoweringObjectFile::
110 getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000111}
112
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000113static SectionKind
114getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0d018b12011-05-24 03:10:31 +0000115 // N.B.: The defaults used in here are no the same ones used in MC.
116 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
117 // both gas and MC will produce a section with no flags. Given
Bill Wendlingd1634052012-07-19 00:04:14 +0000118 // section(".eh_frame") gcc will produce:
119 //
120 // .section .eh_frame,"a",@progbits
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000121 if (Name.empty() || Name[0] != '.') return K;
122
123 // Some lame default implementation based on some magic section names.
124 if (Name == ".bss" ||
125 Name.startswith(".bss.") ||
126 Name.startswith(".gnu.linkonce.b.") ||
127 Name.startswith(".llvm.linkonce.b.") ||
128 Name == ".sbss" ||
129 Name.startswith(".sbss.") ||
130 Name.startswith(".gnu.linkonce.sb.") ||
131 Name.startswith(".llvm.linkonce.sb."))
132 return SectionKind::getBSS();
133
134 if (Name == ".tdata" ||
135 Name.startswith(".tdata.") ||
136 Name.startswith(".gnu.linkonce.td.") ||
137 Name.startswith(".llvm.linkonce.td."))
138 return SectionKind::getThreadData();
139
140 if (Name == ".tbss" ||
141 Name.startswith(".tbss.") ||
142 Name.startswith(".gnu.linkonce.tb.") ||
143 Name.startswith(".llvm.linkonce.tb."))
144 return SectionKind::getThreadBSS();
145
146 return K;
147}
148
149
150static unsigned getELFSectionType(StringRef Name, SectionKind K) {
151
152 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000153 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000154
155 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000156 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000157
158 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000159 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000160
161 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000162 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000163
Rafael Espindolaaea49582011-01-23 04:28:49 +0000164 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000165}
166
167
168static unsigned
169getELFSectionFlags(SectionKind K) {
170 unsigned Flags = 0;
171
172 if (!K.isMetadata())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000173 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000174
175 if (K.isText())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000176 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000177
Rafael Espindolac85e0d82011-06-07 23:26:45 +0000178 if (K.isWriteable())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000179 Flags |= ELF::SHF_WRITE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000180
181 if (K.isThreadLocal())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000182 Flags |= ELF::SHF_TLS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000183
184 // K.isMergeableConst() is left out to honour PR4650
185 if (K.isMergeableCString() || K.isMergeableConst4() ||
186 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000187 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000188
189 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000190 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000191
192 return Flags;
193}
194
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000195const MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
196 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
197 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000198 StringRef SectionName = GV->getSection();
199
200 // Infer section flags from the section name if we can.
201 Kind = getELFKindForNamedSection(SectionName, Kind);
202
Chris Lattner80c34592010-04-08 21:34:17 +0000203 return getContext().getELFSection(SectionName,
204 getELFSectionType(SectionName, Kind),
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000205 getELFSectionFlags(Kind), Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000206}
207
Chris Lattner5b212a32010-04-13 00:36:43 +0000208/// getSectionPrefixForGlobal - Return the section prefix name used by options
209/// FunctionsSections and DataSections.
210static const char *getSectionPrefixForGlobal(SectionKind Kind) {
211 if (Kind.isText()) return ".text.";
212 if (Kind.isReadOnly()) return ".rodata.";
Anton Korobeynikova22828e2012-02-23 10:36:04 +0000213 if (Kind.isBSS()) return ".bss.";
Chris Lattner5b212a32010-04-13 00:36:43 +0000214
215 if (Kind.isThreadData()) return ".tdata.";
216 if (Kind.isThreadBSS()) return ".tbss.";
217
218 if (Kind.isDataNoRel()) return ".data.";
219 if (Kind.isDataRelLocal()) return ".data.rel.local.";
220 if (Kind.isDataRel()) return ".data.rel.";
221 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
222
223 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
224 return ".data.rel.ro.";
225}
226
227
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000228const MCSection *TargetLoweringObjectFileELF::
229SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000230 Mangler &Mang, const TargetMachine &TM) const {
Chris Lattner5b212a32010-04-13 00:36:43 +0000231 // If we have -ffunction-section or -fdata-section then we should emit the
232 // global value to a uniqued section specifically for it.
233 bool EmitUniquedSection;
234 if (Kind.isText())
235 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000236 else
Chris Lattner5b212a32010-04-13 00:36:43 +0000237 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000238
239 // If this global is linkonce/weak and the target handles this by emitting it
240 // into a 'uniqued' section name, create and return the section now.
Chris Lattner5b212a32010-04-13 00:36:43 +0000241 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
Anton Korobeynikova22828e2012-02-23 10:36:04 +0000242 !Kind.isCommon()) {
Chris Lattner5b212a32010-04-13 00:36:43 +0000243 const char *Prefix;
Rafael Espindola70d80152011-02-14 22:23:49 +0000244 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner5b212a32010-04-13 00:36:43 +0000245
Chris Lattner2ea586b2010-03-15 20:37:38 +0000246 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000247 TM.getNameWithPrefix(Name, GV, Mang, true);
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000248
Rafael Espindola70d80152011-02-14 22:23:49 +0000249 StringRef Group = "";
250 unsigned Flags = getELFSectionFlags(Kind);
251 if (GV->isWeakForLinker()) {
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000252 Group = Name.substr(strlen(Prefix));
Rafael Espindola70d80152011-02-14 22:23:49 +0000253 Flags |= ELF::SHF_GROUP;
254 }
255
Chris Lattner80c34592010-04-08 21:34:17 +0000256 return getContext().getELFSection(Name.str(),
257 getELFSectionType(Name.str(), Kind),
Rafael Espindola70d80152011-02-14 22:23:49 +0000258 Flags, Kind, 0, Group);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000259 }
260
261 if (Kind.isText()) return TextSection;
262
263 if (Kind.isMergeable1ByteCString() ||
264 Kind.isMergeable2ByteCString() ||
265 Kind.isMergeable4ByteCString()) {
266
267 // We also need alignment here.
268 // FIXME: this is getting the alignment of the character, not the
269 // alignment of the global!
270 unsigned Align =
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000271 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000272
273 const char *SizeSpec = ".rodata.str1.";
274 if (Kind.isMergeable2ByteCString())
275 SizeSpec = ".rodata.str2.";
276 else if (Kind.isMergeable4ByteCString())
277 SizeSpec = ".rodata.str4.";
278 else
279 assert(Kind.isMergeable1ByteCString() && "unknown string width");
280
281
282 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolaaea49582011-01-23 04:28:49 +0000283 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000284 ELF::SHF_ALLOC |
285 ELF::SHF_MERGE |
286 ELF::SHF_STRINGS,
Chris Lattner80c34592010-04-08 21:34:17 +0000287 Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000288 }
289
290 if (Kind.isMergeableConst()) {
291 if (Kind.isMergeableConst4() && MergeableConst4Section)
292 return MergeableConst4Section;
293 if (Kind.isMergeableConst8() && MergeableConst8Section)
294 return MergeableConst8Section;
295 if (Kind.isMergeableConst16() && MergeableConst16Section)
296 return MergeableConst16Section;
297 return ReadOnlySection; // .const
298 }
299
300 if (Kind.isReadOnly()) return ReadOnlySection;
301
302 if (Kind.isThreadData()) return TLSDataSection;
303 if (Kind.isThreadBSS()) return TLSBSSSection;
304
305 // Note: we claim that common symbols are put in BSSSection, but they are
306 // really emitted with the magic .comm directive, which creates a symbol table
307 // entry but not a section.
308 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
309
310 if (Kind.isDataNoRel()) return DataSection;
311 if (Kind.isDataRelLocal()) return DataRelLocalSection;
312 if (Kind.isDataRel()) return DataRelSection;
313 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
314
315 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
316 return DataRelROSection;
317}
318
319/// getSectionForConstant - Given a mergeable constant with the
320/// specified size and relocation information, return a section that it
321/// should be placed in.
322const MCSection *TargetLoweringObjectFileELF::
323getSectionForConstant(SectionKind Kind) const {
324 if (Kind.isMergeableConst4() && MergeableConst4Section)
325 return MergeableConst4Section;
326 if (Kind.isMergeableConst8() && MergeableConst8Section)
327 return MergeableConst8Section;
328 if (Kind.isMergeableConst16() && MergeableConst16Section)
329 return MergeableConst16Section;
330 if (Kind.isReadOnly())
331 return ReadOnlySection;
332
333 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
334 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
335 return DataRelROSection;
336}
337
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000338const MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000339 unsigned Priority, const MCSymbol *KeySym) const {
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000340 // The default scheme is .ctor / .dtor, so we have to invert the priority
341 // numbering.
342 if (Priority == 65535)
343 return StaticCtorSection;
344
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000345 if (UseInitArray) {
346 std::string Name = std::string(".init_array.") + utostr(Priority);
347 return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY,
348 ELF::SHF_ALLOC | ELF::SHF_WRITE,
349 SectionKind::getDataRel());
350 } else {
351 std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
352 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
353 ELF::SHF_ALLOC |ELF::SHF_WRITE,
354 SectionKind::getDataRel());
355 }
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000356}
357
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000358const MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000359 unsigned Priority, const MCSymbol *KeySym) const {
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000360 // The default scheme is .ctor / .dtor, so we have to invert the priority
361 // numbering.
362 if (Priority == 65535)
363 return StaticDtorSection;
364
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000365 if (UseInitArray) {
366 std::string Name = std::string(".fini_array.") + utostr(Priority);
367 return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY,
368 ELF::SHF_ALLOC | ELF::SHF_WRITE,
369 SectionKind::getDataRel());
370 } else {
371 std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
372 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
373 ELF::SHF_ALLOC |ELF::SHF_WRITE,
374 SectionKind::getDataRel());
375 }
376}
377
378void
379TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
380 UseInitArray = UseInitArray_;
381 if (!UseInitArray)
382 return;
383
384 StaticCtorSection =
385 getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
386 ELF::SHF_WRITE |
387 ELF::SHF_ALLOC,
388 SectionKind::getDataRel());
389 StaticDtorSection =
390 getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
391 ELF::SHF_WRITE |
392 ELF::SHF_ALLOC,
393 SectionKind::getDataRel());
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000394}
395
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000396//===----------------------------------------------------------------------===//
397// MachO
398//===----------------------------------------------------------------------===//
399
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000400/// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
401/// option string. Returns StringRef() if the option does not specify a library.
402StringRef TargetLoweringObjectFileMachO::
403getDepLibFromLinkerOpt(StringRef LinkerOption) const {
404 const char *LibCmd = "-l";
405 if (LinkerOption.startswith(LibCmd))
406 return LinkerOption.substr(strlen(LibCmd));
407 return StringRef();
408}
409
Daniel Dunbar95856122013-01-18 19:37:00 +0000410/// emitModuleFlags - Perform code emission for module flags.
Bill Wendling06df7722012-02-14 21:28:13 +0000411void TargetLoweringObjectFileMachO::
Bill Wendling734909a2012-02-15 22:36:15 +0000412emitModuleFlags(MCStreamer &Streamer,
413 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000414 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendling06df7722012-02-14 21:28:13 +0000415 unsigned VersionVal = 0;
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000416 unsigned ImageInfoFlags = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000417 MDNode *LinkerOptions = nullptr;
Bill Wendling734909a2012-02-15 22:36:15 +0000418 StringRef SectionVal;
Bill Wendling06df7722012-02-14 21:28:13 +0000419
Bill Wendling734909a2012-02-15 22:36:15 +0000420 for (ArrayRef<Module::ModuleFlagEntry>::iterator
421 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
422 const Module::ModuleFlagEntry &MFE = *i;
Bill Wendling06df7722012-02-14 21:28:13 +0000423
424 // Ignore flags with 'Require' behavior.
Bill Wendling734909a2012-02-15 22:36:15 +0000425 if (MFE.Behavior == Module::Require)
Bill Wendling06df7722012-02-14 21:28:13 +0000426 continue;
427
Bill Wendling734909a2012-02-15 22:36:15 +0000428 StringRef Key = MFE.Key->getString();
429 Value *Val = MFE.Val;
Bill Wendling06df7722012-02-14 21:28:13 +0000430
Daniel Dunbar95856122013-01-18 19:37:00 +0000431 if (Key == "Objective-C Image Info Version") {
Bill Wendling06df7722012-02-14 21:28:13 +0000432 VersionVal = cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000433 } else if (Key == "Objective-C Garbage Collection" ||
434 Key == "Objective-C GC Only" ||
435 Key == "Objective-C Is Simulated") {
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000436 ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000437 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling734909a2012-02-15 22:36:15 +0000438 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar95856122013-01-18 19:37:00 +0000439 } else if (Key == "Linker Options") {
440 LinkerOptions = cast<MDNode>(Val);
441 }
442 }
443
444 // Emit the linker options if present.
445 if (LinkerOptions) {
446 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
447 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
448 SmallVector<std::string, 4> StrOptions;
449
450 // Convert to strings.
451 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
452 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
453 StrOptions.push_back(MDOption->getString());
454 }
455
456 Streamer.EmitLinkerOptions(StrOptions);
457 }
Bill Wendling06df7722012-02-14 21:28:13 +0000458 }
459
Bill Wendling734909a2012-02-15 22:36:15 +0000460 // The section is mandatory. If we don't have it, then we don't have GC info.
461 if (SectionVal.empty()) return;
Bill Wendling06df7722012-02-14 21:28:13 +0000462
Bill Wendling734909a2012-02-15 22:36:15 +0000463 StringRef Segment, Section;
464 unsigned TAA = 0, StubSize = 0;
465 bool TAAParsed;
466 std::string ErrorCode =
467 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
468 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000469 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000470 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000471 report_fatal_error("Invalid section specifier '" + Section + "': " +
472 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000473
Bill Wendling734909a2012-02-15 22:36:15 +0000474 // Get the section.
475 const MCSectionMachO *S =
476 getContext().getMachOSection(Segment, Section, TAA, StubSize,
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000477 SectionKind::getDataNoRel());
Bill Wendling734909a2012-02-15 22:36:15 +0000478 Streamer.SwitchSection(S);
479 Streamer.EmitLabel(getContext().
480 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000481 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000482 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000483 Streamer.AddBlankLine();
484}
485
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000486const MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
487 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
488 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000489 // Parse the section specifier and create it if valid.
490 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000491 unsigned TAA = 0, StubSize = 0;
492 bool TAAParsed;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000493 std::string ErrorCode =
494 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000495 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000496 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000497 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000498 report_fatal_error("Global variable '" + GV->getName() +
499 "' has an invalid section specifier '" +
500 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000501 }
502
503 // Get the section.
504 const MCSectionMachO *S =
Chris Lattner433d4062010-04-08 20:40:11 +0000505 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000506
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000507 // If TAA wasn't set by ParseSectionSpecifier() above,
508 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000509 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000510 TAA = S->getTypeAndAttributes();
511
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000512 // Okay, now that we got the section, verify that the TAA & StubSize agree.
513 // If the user declared multiple globals with different section flags, we need
514 // to reject it here.
515 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000516 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000517 report_fatal_error("Global variable '" + GV->getName() +
518 "' section type or attributes does not match previous"
519 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000520 }
521
522 return S;
523}
524
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000525bool TargetLoweringObjectFileMachO::isSectionAtomizableBySymbols(
526 const MCSection &Section) const {
527 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
528
529 // Sections holding 1 byte strings are atomized based on the data
530 // they contain.
531 // Sections holding 2 byte strings require symbols in order to be
532 // atomized.
533 // There is no dedicated section for 4 byte strings.
534 if (SMO.getKind().isMergeable1ByteCString())
535 return false;
536
537 if (SMO.getSegmentName() == "__DATA" &&
538 SMO.getSectionName() == "__cfstring")
539 return false;
540
541 switch (SMO.getType()) {
542 default:
543 return true;
544
545 // These sections are atomized at the element boundaries without using
546 // symbols.
David Majnemer7b583052014-03-07 07:36:05 +0000547 case MachO::S_4BYTE_LITERALS:
548 case MachO::S_8BYTE_LITERALS:
549 case MachO::S_16BYTE_LITERALS:
550 case MachO::S_LITERAL_POINTERS:
551 case MachO::S_NON_LAZY_SYMBOL_POINTERS:
552 case MachO::S_LAZY_SYMBOL_POINTERS:
553 case MachO::S_MOD_INIT_FUNC_POINTERS:
554 case MachO::S_MOD_TERM_FUNC_POINTERS:
555 case MachO::S_INTERPOSING:
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000556 return false;
557 }
558}
559
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000560const MCSection *TargetLoweringObjectFileMachO::
561SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000562 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendling25b61db2013-11-17 10:53:13 +0000563
564 // Handle thread local data.
565 if (Kind.isThreadBSS()) return TLSBSSSection;
566 if (Kind.isThreadData()) return TLSDataSection;
567
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000568 if (Kind.isText())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000569 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
570
571 // If this is weak/linkonce, put this in a coalescable section, either in text
572 // or data depending on if it is writable.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000573 if (GV->isWeakForLinker()) {
574 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000575 return ConstTextCoalSection;
576 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000577 }
578
579 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000580 if (Kind.isMergeable1ByteCString() &&
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000581 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000582 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000583
Chris Lattneref2f8042010-03-07 04:28:09 +0000584 // Do not put 16-bit arrays in the UString section if they have an
585 // externally visible label, this runs into issues with certain linker
586 // versions.
587 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000588 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000589 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000590
591 if (Kind.isMergeableConst()) {
592 if (Kind.isMergeableConst4())
593 return FourByteConstantSection;
594 if (Kind.isMergeableConst8())
595 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000596 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000597 return SixteenByteConstantSection;
598 }
599
600 // Otherwise, if it is readonly, but not something we can specially optimize,
601 // just drop it in .const.
602 if (Kind.isReadOnly())
603 return ReadOnlySection;
604
605 // If this is marked const, put it into a const section. But if the dynamic
606 // linker needs to write to it, put it in the data segment.
607 if (Kind.isReadOnlyWithRel())
608 return ConstDataSection;
609
610 // Put zero initialized globals with strong external linkage in the
611 // DATA, __common section with the .zerofill directive.
612 if (Kind.isBSSExtern())
613 return DataCommonSection;
614
615 // Put zero initialized globals with local linkage in __DATA,__bss directive
616 // with the .zerofill directive (aka .lcomm).
617 if (Kind.isBSSLocal())
618 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000619
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000620 // Otherwise, just drop the variable in the normal data section.
621 return DataSection;
622}
623
624const MCSection *
625TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
626 // If this constant requires a relocation, we have to put it in the data
627 // segment, not in the text segment.
628 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
629 return ConstDataSection;
630
631 if (Kind.isMergeableConst4())
632 return FourByteConstantSection;
633 if (Kind.isMergeableConst8())
634 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000635 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000636 return SixteenByteConstantSection;
637 return ReadOnlySection; // .const
638}
639
Rafael Espindola15b26692014-02-09 14:50:44 +0000640const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
641 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000642 const TargetMachine &TM, MachineModuleInfo *MMI,
643 MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000644 // The mach-o version of this method defaults to returning a stub reference.
645
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000646 if (Encoding & DW_EH_PE_indirect) {
647 MachineModuleInfoMachO &MachOMMI =
648 MMI->getObjFileInfo<MachineModuleInfoMachO>();
649
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000650 MCSymbol *SSym =
651 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000652
653 // Add information about the stub reference to MachOMMI so that the stub
654 // gets emitted by the asmprinter.
Bill Wendling57e3aaa2011-10-24 23:05:43 +0000655 MachineModuleInfoImpl::StubValueTy &StubSym =
656 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
657 MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000658 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000659 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000660 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000661 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000662
663 return TargetLoweringObjectFile::
Anton Korobeynikove42af362012-11-14 01:47:00 +0000664 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
665 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000666 }
667
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000668 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang,
669 TM, MMI, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000670}
671
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000672MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
673 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
674 MachineModuleInfo *MMI) const {
Rafael Espindola08704342011-04-27 23:08:15 +0000675 // The mach-o version of this method defaults to returning a stub reference.
676 MachineModuleInfoMachO &MachOMMI =
677 MMI->getObjFileInfo<MachineModuleInfoMachO>();
678
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000679 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Rafael Espindola08704342011-04-27 23:08:15 +0000680
681 // Add information about the stub reference to MachOMMI so that the stub
682 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000683 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000684 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000685 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Rafael Espindola08704342011-04-27 23:08:15 +0000686 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
687 }
688
689 return SSym;
690}
691
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000692//===----------------------------------------------------------------------===//
693// COFF
694//===----------------------------------------------------------------------===//
695
Chris Lattner87cffa92010-05-07 17:17:41 +0000696static unsigned
697getCOFFSectionFlags(SectionKind K) {
698 unsigned Flags = 0;
699
Anton Korobeynikove4152302010-07-06 15:24:56 +0000700 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000701 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000702 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000703 else if (K.isText())
704 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000705 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000706 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar329d2022010-07-01 20:07:24 +0000707 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner02844932010-05-07 21:49:09 +0000708 else if (K.isBSS ())
709 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000710 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
711 COFF::IMAGE_SCN_MEM_READ |
712 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000713 else if (K.isThreadLocal())
714 Flags |=
715 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
716 COFF::IMAGE_SCN_MEM_READ |
717 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000718 else if (K.isReadOnly())
719 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000720 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
721 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000722 else if (K.isWriteable())
723 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000724 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
725 COFF::IMAGE_SCN_MEM_READ |
726 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000727
728 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000729}
730
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000731const MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
732 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
733 const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000734 int Selection = 0;
735 unsigned Characteristics = getCOFFSectionFlags(Kind);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000736 StringRef Name = GV->getSection();
737 StringRef COMDATSymName = "";
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000738 if (GV->isWeakForLinker()) {
739 Selection = COFF::IMAGE_COMDAT_SELECT_ANY;
740 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000741 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000742 COMDATSymName = Sym->getName();
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000743 }
744 return getContext().getCOFFSection(Name,
745 Characteristics,
Nico Riecka37acf72013-07-06 12:13:10 +0000746 Kind,
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000747 COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +0000748 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000749}
750
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000751static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000752 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000753 return ".text";
David Majnemera9bdb322014-04-08 22:33:40 +0000754 if (Kind.isBSS())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000755 return ".bss";
756 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +0000757 return ".tls$";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000758 if (Kind.isWriteable())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000759 return ".data";
760 return ".rdata";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000761}
762
763
764const MCSection *TargetLoweringObjectFileCOFF::
765SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000766 Mangler &Mang, const TargetMachine &TM) const {
David Majnemer93389842014-03-23 17:47:39 +0000767 // If we have -ffunction-sections then we should emit the global value to a
768 // uniqued section specifically for it.
David Majnemer273bff42014-03-25 06:14:26 +0000769 bool EmitUniquedSection;
770 if (Kind.isText())
771 EmitUniquedSection = TM.getFunctionSections();
772 else
773 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000774
775 // If this global is linkonce/weak and the target handles this by emitting it
776 // into a 'uniqued' section name, create and return the section now.
David Majnemer273bff42014-03-25 06:14:26 +0000777 // Section names depend on the name of the symbol which is not feasible if the
778 // symbol has private linkage.
779 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
David Majnemera9bdb322014-04-08 22:33:40 +0000780 !GV->hasPrivateLinkage() && !Kind.isCommon()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000781 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Chris Lattner02844932010-05-07 21:49:09 +0000782 unsigned Characteristics = getCOFFSectionFlags(Kind);
783
Daniel Dunbar329d2022010-07-01 20:07:24 +0000784 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000785 MCSymbol *Sym = TM.getSymbol(GV, Mang);
David Majnemer93389842014-03-23 17:47:39 +0000786 return getContext().getCOFFSection(
787 Name, Characteristics, Kind, Sym->getName(),
788 GV->isWeakForLinker() ? COFF::IMAGE_COMDAT_SELECT_ANY
789 : COFF::IMAGE_COMDAT_SELECT_NODUPLICATES);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000790 }
791
792 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +0000793 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000794
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000795 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +0000796 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000797
David Majnemer3d96acb2013-08-13 01:23:53 +0000798 if (Kind.isReadOnly())
David Majnemerf76d6b32013-08-08 01:50:52 +0000799 return ReadOnlySection;
800
David Majnemera9bdb322014-04-08 22:33:40 +0000801 // Note: we claim that common symbols are put in BSSSection, but they are
802 // really emitted with the magic .comm directive, which creates a symbol table
803 // entry but not a section.
804 if (Kind.isBSS() || Kind.isCommon())
David Majnemer3d96acb2013-08-13 01:23:53 +0000805 return BSSSection;
806
807 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000808}
809
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000810StringRef TargetLoweringObjectFileCOFF::
811getDepLibFromLinkerOpt(StringRef LinkerOption) const {
812 const char *LibCmd = "/DEFAULTLIB:";
813 if (LinkerOption.startswith(LibCmd))
814 return LinkerOption.substr(strlen(LibCmd));
815 return StringRef();
816}
817
Reid Klecknerd973ca32013-04-25 19:34:41 +0000818void TargetLoweringObjectFileCOFF::
819emitModuleFlags(MCStreamer &Streamer,
820 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000821 Mangler &Mang, const TargetMachine &TM) const {
Craig Topperc0196b12014-04-14 00:51:57 +0000822 MDNode *LinkerOptions = nullptr;
Reid Klecknerd973ca32013-04-25 19:34:41 +0000823
824 // Look for the "Linker Options" flag, since it's the only one we support.
825 for (ArrayRef<Module::ModuleFlagEntry>::iterator
826 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
827 const Module::ModuleFlagEntry &MFE = *i;
828 StringRef Key = MFE.Key->getString();
829 Value *Val = MFE.Val;
830 if (Key == "Linker Options") {
831 LinkerOptions = cast<MDNode>(Val);
832 break;
833 }
834 }
835 if (!LinkerOptions)
836 return;
837
838 // Emit the linker options to the linker .drectve section. According to the
839 // spec, this section is a space-separated string containing flags for linker.
840 const MCSection *Sec = getDrectveSection();
841 Streamer.SwitchSection(Sec);
842 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
843 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
844 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
845 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
846 StringRef Op = MDOption->getString();
847 // Lead with a space for consistency with our dllexport implementation.
848 std::string Escaped(" ");
849 if (Op.find(" ") != StringRef::npos) {
850 // The PE-COFF spec says args with spaces must be quoted. It doesn't say
851 // how to escape quotes, but it probably uses this algorithm:
852 // http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx
853 // FIXME: Reuse escaping code from Support/Windows/Program.inc
854 Escaped.push_back('\"');
855 Escaped.append(Op);
856 Escaped.push_back('\"');
857 } else {
858 Escaped.append(Op);
859 }
860 Streamer.EmitBytes(Escaped);
861 }
862 }
863}
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000864
865static const MCSection *getAssociativeCOFFSection(MCContext &Ctx,
866 const MCSection *Sec,
Rafael Espindola0766ae02014-06-06 19:26:12 +0000867 const MCSymbol *KeySym) {
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000868 // Return the normal section if we don't have to be associative.
869 if (!KeySym)
870 return Sec;
871
872 // Make an associative section with the same name and kind as the normal
873 // section.
874 const MCSectionCOFF *SecCOFF = cast<MCSectionCOFF>(Sec);
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000875 unsigned Characteristics =
876 SecCOFF->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
877 return Ctx.getCOFFSection(SecCOFF->getSectionName(), Characteristics,
878 SecCOFF->getKind(), KeySym->getName(),
Rafael Espindola0766ae02014-06-06 19:26:12 +0000879 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000880}
881
882const MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000883 unsigned Priority, const MCSymbol *KeySym) const {
884 return getAssociativeCOFFSection(getContext(), StaticCtorSection, KeySym);
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000885}
886
887const MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000888 unsigned Priority, const MCSymbol *KeySym) const {
889 return getAssociativeCOFFSection(getContext(), StaticDtorSection, KeySym);
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000890}