blob: 1170bf265636c8a696f0486b70f3b254ae49b5a3 [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"
25#include "llvm/IR/Module.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000026#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000028#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/MC/MCSectionELF.h"
30#include "llvm/MC/MCSectionMachO.h"
Rafael Espindola30deafc2011-04-16 03:51:21 +000031#include "llvm/MC/MCStreamer.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000032#include "llvm/MC/MCSymbol.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000033#include "llvm/Support/Dwarf.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000034#include "llvm/Support/ELF.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000037#include "llvm/Target/Mangler.h"
38#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetOptions.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
Rafael Espindola30deafc2011-04-16 03:51:21 +000047MCSymbol *
Rafael Espindola7afec9c2011-04-27 23:08:15 +000048TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
Rafael Espindola7afec9c2011-04-27 23:08:15 +000049 Mangler *Mang,
50 MachineModuleInfo *MMI) const {
Rafael Espindola60246a92011-04-27 23:17:57 +000051 unsigned Encoding = getPersonalityEncoding();
Rafael Espindola7afec9c2011-04-27 23:08:15 +000052 switch (Encoding & 0x70) {
53 default:
54 report_fatal_error("We do not support this DWARF encoding yet!");
55 case dwarf::DW_EH_PE_absptr:
56 return Mang->getSymbol(GV);
Rafael Espindola7afec9c2011-04-27 23:08:15 +000057 case dwarf::DW_EH_PE_pcrel: {
Rafael Espindolafb66f472011-06-13 03:09:13 +000058 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
59 Mang->getSymbol(GV)->getName());
Rafael Espindola7afec9c2011-04-27 23:08:15 +000060 }
61 }
Rafael Espindola30deafc2011-04-16 03:51:21 +000062}
63
64void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
65 const TargetMachine &TM,
Rafael Espindola7afec9c2011-04-27 23:08:15 +000066 const MCSymbol *Sym) const {
Rafael Espindolafb66f472011-06-13 03:09:13 +000067 SmallString<64> NameData("DW.ref.");
68 NameData += Sym->getName();
69 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
Rafael Espindola018e38c2011-04-27 21:29:52 +000070 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
71 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindolafb66f472011-06-13 03:09:13 +000072 StringRef Prefix = ".data.";
73 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola018e38c2011-04-27 21:29:52 +000074 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
75 const MCSection *Sec = getContext().getELFSection(NameData,
76 ELF::SHT_PROGBITS,
77 Flags,
78 SectionKind::getDataRel(),
79 0, Label->getName());
Chandler Carruth426c2bf2012-11-01 09:14:31 +000080 unsigned Size = TM.getDataLayout()->getPointerSize();
Rafael Espindola018e38c2011-04-27 21:29:52 +000081 Streamer.SwitchSection(Sec);
Chandler Carruth426c2bf2012-11-01 09:14:31 +000082 Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment());
Rafael Espindola018e38c2011-04-27 21:29:52 +000083 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
David Chisnallca5b7522012-02-17 16:05:50 +000084 const MCExpr *E = MCConstantExpr::Create(Size, getContext());
Rafael Espindola018e38c2011-04-27 21:29:52 +000085 Streamer.EmitELFSize(Label, E);
86 Streamer.EmitLabel(Label);
Rafael Espindola30deafc2011-04-16 03:51:21 +000087
Rafael Espindola018e38c2011-04-27 21:29:52 +000088 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindola30deafc2011-04-16 03:51:21 +000089}
90
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +000091const MCExpr *TargetLoweringObjectFileELF::
92getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang,
93 MachineModuleInfo *MMI, unsigned Encoding,
94 MCStreamer &Streamer) const {
95
96 if (Encoding & dwarf::DW_EH_PE_indirect) {
97 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
98
99 SmallString<128> Name;
100 Mang->getNameWithPrefix(Name, GV, true);
101 Name += ".DW.stub";
102
103 // Add information about the stub reference to ELFMMI so that the stub
104 // gets emitted by the asmprinter.
105 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
106 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
107 if (StubSym.getPointer() == 0) {
108 MCSymbol *Sym = Mang->getSymbol(GV);
109 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
110 }
111
112 return TargetLoweringObjectFile::
113 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
114 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
115 }
116
117 return TargetLoweringObjectFile::
118 getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer);
119}
120
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000121static SectionKind
122getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindolae6657982011-05-24 03:10:31 +0000123 // N.B.: The defaults used in here are no the same ones used in MC.
124 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
125 // both gas and MC will produce a section with no flags. Given
Bill Wendling96cb1122012-07-19 00:04:14 +0000126 // section(".eh_frame") gcc will produce:
127 //
128 // .section .eh_frame,"a",@progbits
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000129 if (Name.empty() || Name[0] != '.') return K;
130
131 // Some lame default implementation based on some magic section names.
132 if (Name == ".bss" ||
133 Name.startswith(".bss.") ||
134 Name.startswith(".gnu.linkonce.b.") ||
135 Name.startswith(".llvm.linkonce.b.") ||
136 Name == ".sbss" ||
137 Name.startswith(".sbss.") ||
138 Name.startswith(".gnu.linkonce.sb.") ||
139 Name.startswith(".llvm.linkonce.sb."))
140 return SectionKind::getBSS();
141
142 if (Name == ".tdata" ||
143 Name.startswith(".tdata.") ||
144 Name.startswith(".gnu.linkonce.td.") ||
145 Name.startswith(".llvm.linkonce.td."))
146 return SectionKind::getThreadData();
147
148 if (Name == ".tbss" ||
149 Name.startswith(".tbss.") ||
150 Name.startswith(".gnu.linkonce.tb.") ||
151 Name.startswith(".llvm.linkonce.tb."))
152 return SectionKind::getThreadBSS();
153
154 return K;
155}
156
157
158static unsigned getELFSectionType(StringRef Name, SectionKind K) {
159
160 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000161 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000162
163 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000164 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000165
166 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000167 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000168
169 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000170 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000171
Rafael Espindolac85dca62011-01-23 04:28:49 +0000172 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000173}
174
175
176static unsigned
177getELFSectionFlags(SectionKind K) {
178 unsigned Flags = 0;
179
180 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000181 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000182
183 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000184 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000185
Rafael Espindolad846e3f2011-06-07 23:26:45 +0000186 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000187 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000188
189 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000190 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000191
192 // K.isMergeableConst() is left out to honour PR4650
193 if (K.isMergeableCString() || K.isMergeableConst4() ||
194 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000195 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000196
197 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000198 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000199
200 return Flags;
201}
202
203
204const MCSection *TargetLoweringObjectFileELF::
205getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
206 Mangler *Mang, const TargetMachine &TM) const {
207 StringRef SectionName = GV->getSection();
208
209 // Infer section flags from the section name if we can.
210 Kind = getELFKindForNamedSection(SectionName, Kind);
211
Chris Lattner287df1b2010-04-08 21:34:17 +0000212 return getContext().getELFSection(SectionName,
213 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000214 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000215}
216
Chris Lattner43ac7212010-04-13 00:36:43 +0000217/// getSectionPrefixForGlobal - Return the section prefix name used by options
218/// FunctionsSections and DataSections.
219static const char *getSectionPrefixForGlobal(SectionKind Kind) {
220 if (Kind.isText()) return ".text.";
221 if (Kind.isReadOnly()) return ".rodata.";
Anton Korobeynikov1d2d5a02012-02-23 10:36:04 +0000222 if (Kind.isBSS()) return ".bss.";
Chris Lattner43ac7212010-04-13 00:36:43 +0000223
224 if (Kind.isThreadData()) return ".tdata.";
225 if (Kind.isThreadBSS()) return ".tbss.";
226
227 if (Kind.isDataNoRel()) return ".data.";
228 if (Kind.isDataRelLocal()) return ".data.rel.local.";
229 if (Kind.isDataRel()) return ".data.rel.";
230 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
231
232 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
233 return ".data.rel.ro.";
234}
235
236
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000237const MCSection *TargetLoweringObjectFileELF::
238SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
239 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000240 // If we have -ffunction-section or -fdata-section then we should emit the
241 // global value to a uniqued section specifically for it.
242 bool EmitUniquedSection;
243 if (Kind.isText())
244 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000245 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000246 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000247
248 // If this global is linkonce/weak and the target handles this by emitting it
249 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000250 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
Anton Korobeynikov1d2d5a02012-02-23 10:36:04 +0000251 !Kind.isCommon()) {
Chris Lattner43ac7212010-04-13 00:36:43 +0000252 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000253 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000254
Chris Lattner4c6741f2010-03-15 20:37:38 +0000255 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
256 MCSymbol *Sym = Mang->getSymbol(GV);
257 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000258 StringRef Group = "";
259 unsigned Flags = getELFSectionFlags(Kind);
260 if (GV->isWeakForLinker()) {
261 Group = Sym->getName();
262 Flags |= ELF::SHF_GROUP;
263 }
264
Chris Lattner287df1b2010-04-08 21:34:17 +0000265 return getContext().getELFSection(Name.str(),
266 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000267 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000268 }
269
270 if (Kind.isText()) return TextSection;
271
272 if (Kind.isMergeable1ByteCString() ||
273 Kind.isMergeable2ByteCString() ||
274 Kind.isMergeable4ByteCString()) {
275
276 // We also need alignment here.
277 // FIXME: this is getting the alignment of the character, not the
278 // alignment of the global!
279 unsigned Align =
Micah Villmow3574eca2012-10-08 16:38:25 +0000280 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000281
282 const char *SizeSpec = ".rodata.str1.";
283 if (Kind.isMergeable2ByteCString())
284 SizeSpec = ".rodata.str2.";
285 else if (Kind.isMergeable4ByteCString())
286 SizeSpec = ".rodata.str4.";
287 else
288 assert(Kind.isMergeable1ByteCString() && "unknown string width");
289
290
291 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000292 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000293 ELF::SHF_ALLOC |
294 ELF::SHF_MERGE |
295 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000296 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000297 }
298
299 if (Kind.isMergeableConst()) {
300 if (Kind.isMergeableConst4() && MergeableConst4Section)
301 return MergeableConst4Section;
302 if (Kind.isMergeableConst8() && MergeableConst8Section)
303 return MergeableConst8Section;
304 if (Kind.isMergeableConst16() && MergeableConst16Section)
305 return MergeableConst16Section;
306 return ReadOnlySection; // .const
307 }
308
309 if (Kind.isReadOnly()) return ReadOnlySection;
310
311 if (Kind.isThreadData()) return TLSDataSection;
312 if (Kind.isThreadBSS()) return TLSBSSSection;
313
314 // Note: we claim that common symbols are put in BSSSection, but they are
315 // really emitted with the magic .comm directive, which creates a symbol table
316 // entry but not a section.
317 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
318
319 if (Kind.isDataNoRel()) return DataSection;
320 if (Kind.isDataRelLocal()) return DataRelLocalSection;
321 if (Kind.isDataRel()) return DataRelSection;
322 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
323
324 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
325 return DataRelROSection;
326}
327
328/// getSectionForConstant - Given a mergeable constant with the
329/// specified size and relocation information, return a section that it
330/// should be placed in.
331const MCSection *TargetLoweringObjectFileELF::
332getSectionForConstant(SectionKind Kind) const {
333 if (Kind.isMergeableConst4() && MergeableConst4Section)
334 return MergeableConst4Section;
335 if (Kind.isMergeableConst8() && MergeableConst8Section)
336 return MergeableConst8Section;
337 if (Kind.isMergeableConst16() && MergeableConst16Section)
338 return MergeableConst16Section;
339 if (Kind.isReadOnly())
340 return ReadOnlySection;
341
342 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
343 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
344 return DataRelROSection;
345}
346
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000347const MCSection *
348TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const {
349 // The default scheme is .ctor / .dtor, so we have to invert the priority
350 // numbering.
351 if (Priority == 65535)
352 return StaticCtorSection;
353
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000354 if (UseInitArray) {
355 std::string Name = std::string(".init_array.") + utostr(Priority);
356 return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY,
357 ELF::SHF_ALLOC | ELF::SHF_WRITE,
358 SectionKind::getDataRel());
359 } else {
360 std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
361 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
362 ELF::SHF_ALLOC |ELF::SHF_WRITE,
363 SectionKind::getDataRel());
364 }
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000365}
366
367const MCSection *
368TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
369 // The default scheme is .ctor / .dtor, so we have to invert the priority
370 // numbering.
371 if (Priority == 65535)
372 return StaticDtorSection;
373
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000374 if (UseInitArray) {
375 std::string Name = std::string(".fini_array.") + utostr(Priority);
376 return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY,
377 ELF::SHF_ALLOC | ELF::SHF_WRITE,
378 SectionKind::getDataRel());
379 } else {
380 std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
381 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
382 ELF::SHF_ALLOC |ELF::SHF_WRITE,
383 SectionKind::getDataRel());
384 }
385}
386
387void
388TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
389 UseInitArray = UseInitArray_;
390 if (!UseInitArray)
391 return;
392
393 StaticCtorSection =
394 getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
395 ELF::SHF_WRITE |
396 ELF::SHF_ALLOC,
397 SectionKind::getDataRel());
398 StaticDtorSection =
399 getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
400 ELF::SHF_WRITE |
401 ELF::SHF_ALLOC,
402 SectionKind::getDataRel());
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000403}
404
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000405//===----------------------------------------------------------------------===//
406// MachO
407//===----------------------------------------------------------------------===//
408
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000409/// emitModuleFlags - Perform code emission for module flags.
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000410void TargetLoweringObjectFileMachO::
Bill Wendling057d5212012-02-15 22:36:15 +0000411emitModuleFlags(MCStreamer &Streamer,
412 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000413 Mangler *Mang, const TargetMachine &TM) const {
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000414 unsigned VersionVal = 0;
Bill Wendlingadb082c2012-04-24 11:03:50 +0000415 unsigned ImageInfoFlags = 0;
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000416 MDNode *LinkerOptions = 0;
Bill Wendling057d5212012-02-15 22:36:15 +0000417 StringRef SectionVal;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000418
Bill Wendling057d5212012-02-15 22:36:15 +0000419 for (ArrayRef<Module::ModuleFlagEntry>::iterator
420 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
421 const Module::ModuleFlagEntry &MFE = *i;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000422
423 // Ignore flags with 'Require' behavior.
Bill Wendling057d5212012-02-15 22:36:15 +0000424 if (MFE.Behavior == Module::Require)
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000425 continue;
426
Bill Wendling057d5212012-02-15 22:36:15 +0000427 StringRef Key = MFE.Key->getString();
428 Value *Val = MFE.Val;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000429
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000430 if (Key == "Objective-C Image Info Version") {
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000431 VersionVal = cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000432 } else if (Key == "Objective-C Garbage Collection" ||
433 Key == "Objective-C GC Only" ||
434 Key == "Objective-C Is Simulated") {
Bill Wendlingadb082c2012-04-24 11:03:50 +0000435 ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000436 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling057d5212012-02-15 22:36:15 +0000437 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000438 } else if (Key == "Linker Options") {
439 LinkerOptions = cast<MDNode>(Val);
440 }
441 }
442
443 // Emit the linker options if present.
444 if (LinkerOptions) {
445 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
446 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
447 SmallVector<std::string, 4> StrOptions;
448
449 // Convert to strings.
450 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
451 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
452 StrOptions.push_back(MDOption->getString());
453 }
454
455 Streamer.EmitLinkerOptions(StrOptions);
456 }
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000457 }
458
Bill Wendling057d5212012-02-15 22:36:15 +0000459 // The section is mandatory. If we don't have it, then we don't have GC info.
460 if (SectionVal.empty()) return;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000461
Bill Wendling057d5212012-02-15 22:36:15 +0000462 StringRef Segment, Section;
463 unsigned TAA = 0, StubSize = 0;
464 bool TAAParsed;
465 std::string ErrorCode =
466 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
467 TAA, TAAParsed, StubSize);
Bill Wendling2de3ff52012-02-15 22:47:53 +0000468 if (!ErrorCode.empty())
Bill Wendling057d5212012-02-15 22:36:15 +0000469 // If invalid, report the error with report_fatal_error.
Bill Wendling2de3ff52012-02-15 22:47:53 +0000470 report_fatal_error("Invalid section specifier '" + Section + "': " +
471 ErrorCode + ".");
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000472
Bill Wendling057d5212012-02-15 22:36:15 +0000473 // Get the section.
474 const MCSectionMachO *S =
475 getContext().getMachOSection(Segment, Section, TAA, StubSize,
Bill Wendling2de3ff52012-02-15 22:47:53 +0000476 SectionKind::getDataNoRel());
Bill Wendling057d5212012-02-15 22:36:15 +0000477 Streamer.SwitchSection(S);
478 Streamer.EmitLabel(getContext().
479 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000480 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingadb082c2012-04-24 11:03:50 +0000481 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000482 Streamer.AddBlankLine();
483}
484
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000485const MCSection *TargetLoweringObjectFileMachO::
486getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
487 Mangler *Mang, const TargetMachine &TM) const {
488 // Parse the section specifier and create it if valid.
489 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000490 unsigned TAA = 0, StubSize = 0;
491 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000492 std::string ErrorCode =
493 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000494 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000495 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000496 // If invalid, report the error with report_fatal_error.
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000497 report_fatal_error("Global variable '" + GV->getName() +
498 "' has an invalid section specifier '" +
499 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000500 }
501
502 // Get the section.
503 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000504 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000505
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000506 // If TAA wasn't set by ParseSectionSpecifier() above,
507 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000508 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000509 TAA = S->getTypeAndAttributes();
510
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000511 // Okay, now that we got the section, verify that the TAA & StubSize agree.
512 // If the user declared multiple globals with different section flags, we need
513 // to reject it here.
514 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000515 // If invalid, report the error with report_fatal_error.
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000516 report_fatal_error("Global variable '" + GV->getName() +
517 "' section type or attributes does not match previous"
518 " section specifier");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000519 }
520
521 return S;
522}
523
524const MCSection *TargetLoweringObjectFileMachO::
525SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000526 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000527
Eric Christopher02b46bc2010-05-25 21:28:50 +0000528 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000529 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000530 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000531
532 if (Kind.isText())
533 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
534
535 // If this is weak/linkonce, put this in a coalescable section, either in text
536 // or data depending on if it is writable.
537 if (GV->isWeakForLinker()) {
538 if (Kind.isReadOnly())
539 return ConstTextCoalSection;
540 return DataCoalSection;
541 }
542
543 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000544 if (Kind.isMergeable1ByteCString() &&
Micah Villmow3574eca2012-10-08 16:38:25 +0000545 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattner98f15d22010-03-07 04:28:09 +0000546 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000547
Chris Lattner98f15d22010-03-07 04:28:09 +0000548 // Do not put 16-bit arrays in the UString section if they have an
549 // externally visible label, this runs into issues with certain linker
550 // versions.
551 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Micah Villmow3574eca2012-10-08 16:38:25 +0000552 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattner98f15d22010-03-07 04:28:09 +0000553 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000554
555 if (Kind.isMergeableConst()) {
556 if (Kind.isMergeableConst4())
557 return FourByteConstantSection;
558 if (Kind.isMergeableConst8())
559 return EightByteConstantSection;
560 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
561 return SixteenByteConstantSection;
562 }
563
564 // Otherwise, if it is readonly, but not something we can specially optimize,
565 // just drop it in .const.
566 if (Kind.isReadOnly())
567 return ReadOnlySection;
568
569 // If this is marked const, put it into a const section. But if the dynamic
570 // linker needs to write to it, put it in the data segment.
571 if (Kind.isReadOnlyWithRel())
572 return ConstDataSection;
573
574 // Put zero initialized globals with strong external linkage in the
575 // DATA, __common section with the .zerofill directive.
576 if (Kind.isBSSExtern())
577 return DataCommonSection;
578
579 // Put zero initialized globals with local linkage in __DATA,__bss directive
580 // with the .zerofill directive (aka .lcomm).
581 if (Kind.isBSSLocal())
582 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000583
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000584 // Otherwise, just drop the variable in the normal data section.
585 return DataSection;
586}
587
588const MCSection *
589TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
590 // If this constant requires a relocation, we have to put it in the data
591 // segment, not in the text segment.
592 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
593 return ConstDataSection;
594
595 if (Kind.isMergeableConst4())
596 return FourByteConstantSection;
597 if (Kind.isMergeableConst8())
598 return EightByteConstantSection;
599 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
600 return SixteenByteConstantSection;
601 return ReadOnlySection; // .const
602}
603
604/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
605/// not to emit the UsedDirective for some symbols in llvm.used.
606// FIXME: REMOVE this (rdar://7071300)
607bool TargetLoweringObjectFileMachO::
608shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
609 /// On Darwin, internally linked data beginning with "L" or "l" does not have
610 /// the directive emitted (this occurs in ObjC metadata).
611 if (!GV) return false;
612
Bill Wendling07d31772010-06-29 22:34:52 +0000613 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000614 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
615 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000616 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
617 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000618 MCSymbol *Sym = Mang->getSymbol(GV);
619 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000620 return false;
621 }
622
623 return true;
624}
625
626const MCExpr *TargetLoweringObjectFileMachO::
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000627getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang,
628 MachineModuleInfo *MMI, unsigned Encoding,
629 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000630 // The mach-o version of this method defaults to returning a stub reference.
631
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000632 if (Encoding & DW_EH_PE_indirect) {
633 MachineModuleInfoMachO &MachOMMI =
634 MMI->getObjFileInfo<MachineModuleInfoMachO>();
635
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000636 SmallString<128> Name;
637 Mang->getNameWithPrefix(Name, GV, true);
638 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000639
640 // Add information about the stub reference to MachOMMI so that the stub
641 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000642 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Bill Wendling67121542011-10-24 23:05:43 +0000643 MachineModuleInfoImpl::StubValueTy &StubSym =
644 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
645 MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000646 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000647 MCSymbol *Sym = Mang->getSymbol(GV);
648 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000649 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000650
651 return TargetLoweringObjectFile::
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000652 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
653 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000654 }
655
656 return TargetLoweringObjectFile::
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000657 getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000658}
659
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000660MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindola60246a92011-04-27 23:17:57 +0000661getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000662 MachineModuleInfo *MMI) const {
663 // The mach-o version of this method defaults to returning a stub reference.
664 MachineModuleInfoMachO &MachOMMI =
665 MMI->getObjFileInfo<MachineModuleInfoMachO>();
666
667 SmallString<128> Name;
668 Mang->getNameWithPrefix(Name, GV, true);
669 Name += "$non_lazy_ptr";
670
671 // Add information about the stub reference to MachOMMI so that the stub
672 // gets emitted by the asmprinter.
673 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Bill Wendlingd7c24942011-11-29 01:43:20 +0000674 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000675 if (StubSym.getPointer() == 0) {
676 MCSymbol *Sym = Mang->getSymbol(GV);
677 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
678 }
679
680 return SSym;
681}
682
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000683//===----------------------------------------------------------------------===//
684// COFF
685//===----------------------------------------------------------------------===//
686
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000687static unsigned
688getCOFFSectionFlags(SectionKind K) {
689 unsigned Flags = 0;
690
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000691 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000692 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000693 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000694 else if (K.isText())
695 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000696 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000697 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000698 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000699 else if (K.isBSS ())
700 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000701 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
702 COFF::IMAGE_SCN_MEM_READ |
703 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000704 else if (K.isThreadLocal())
705 Flags |=
706 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
707 COFF::IMAGE_SCN_MEM_READ |
708 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000709 else if (K.isReadOnly())
710 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000711 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
712 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000713 else if (K.isWriteable())
714 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000715 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
716 COFF::IMAGE_SCN_MEM_READ |
717 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000718
719 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000720}
721
722const MCSection *TargetLoweringObjectFileCOFF::
723getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
724 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer4de58722012-11-13 22:04:09 +0000725 int Selection = 0;
726 unsigned Characteristics = getCOFFSectionFlags(Kind);
727 SmallString<128> Name(GV->getSection().c_str());
728 if (GV->isWeakForLinker()) {
729 Selection = COFF::IMAGE_COMDAT_SELECT_ANY;
730 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
731 MCSymbol *Sym = Mang->getSymbol(GV);
732 Name.append("$");
733 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
734 }
735 return getContext().getCOFFSection(Name,
736 Characteristics,
737 Selection,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000738 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000739}
740
741static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
742 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000743 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000744 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000745 return ".bss$";
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000746 if (Kind.isThreadLocal())
747 return ".tls$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000748 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000749 return ".data$";
750 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000751}
752
753
754const MCSection *TargetLoweringObjectFileCOFF::
755SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
756 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000757
758 // If this global is linkonce/weak and the target handles this by emitting it
759 // into a 'uniqued' section name, create and return the section now.
760 if (GV->isWeakForLinker()) {
761 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
762 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000763 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000764 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +0000765
766 unsigned Characteristics = getCOFFSectionFlags(Kind);
767
Daniel Dunbar94610582010-07-01 20:07:24 +0000768 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000769
770 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +0000771 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000772 }
773
774 if (Kind.isText())
775 return getTextSection();
776
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000777 if (Kind.isThreadLocal())
778 return getTLSDataSection();
779
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000780 return getDataSection();
781}
782