blob: 9925185be120f741abd66523dc8d0637d34929fb [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"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/GlobalVariable.h"
Bill Wendlingb464d3f2012-02-14 21:28:13 +000020#include "llvm/Module.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000021#include "llvm/CodeGen/MachineModuleInfoImpls.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCSectionMachO.h"
25#include "llvm/MC/MCSectionELF.h"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000026#include "llvm/MC/MCSectionCOFF.h"
Rafael Espindola30deafc2011-04-16 03:51:21 +000027#include "llvm/MC/MCStreamer.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000028#include "llvm/MC/MCSymbol.h"
29#include "llvm/Target/Mangler.h"
30#include "llvm/Target/TargetData.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Target/TargetOptions.h"
33#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"
37#include "llvm/ADT/SmallString.h"
38#include "llvm/ADT/StringExtras.h"
Chris Lattner8048ebe2010-09-27 06:44:54 +000039#include "llvm/ADT/Triple.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());
David Chisnallca5b7522012-02-17 16:05:50 +000080 unsigned Size = TM.getTargetData()->getPointerSize();
Rafael Espindola018e38c2011-04-27 21:29:52 +000081 Streamer.SwitchSection(Sec);
David Chisnall50f603f2012-02-17 16:30:39 +000082 Streamer.EmitValueToAlignment(TM.getTargetData()->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 Korobeynikov362dd0b2010-02-15 22:37:53 +000091static SectionKind
92getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindolae6657982011-05-24 03:10:31 +000093 // N.B.: The defaults used in here are no the same ones used in MC.
94 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
95 // both gas and MC will produce a section with no flags. Given
96 // section(".eh_frame") gcc will produce
97 // .section .eh_frame,"a",@progbits
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000098 if (Name.empty() || Name[0] != '.') return K;
99
100 // Some lame default implementation based on some magic section names.
101 if (Name == ".bss" ||
102 Name.startswith(".bss.") ||
103 Name.startswith(".gnu.linkonce.b.") ||
104 Name.startswith(".llvm.linkonce.b.") ||
105 Name == ".sbss" ||
106 Name.startswith(".sbss.") ||
107 Name.startswith(".gnu.linkonce.sb.") ||
108 Name.startswith(".llvm.linkonce.sb."))
109 return SectionKind::getBSS();
110
111 if (Name == ".tdata" ||
112 Name.startswith(".tdata.") ||
113 Name.startswith(".gnu.linkonce.td.") ||
114 Name.startswith(".llvm.linkonce.td."))
115 return SectionKind::getThreadData();
116
117 if (Name == ".tbss" ||
118 Name.startswith(".tbss.") ||
119 Name.startswith(".gnu.linkonce.tb.") ||
120 Name.startswith(".llvm.linkonce.tb."))
121 return SectionKind::getThreadBSS();
122
123 return K;
124}
125
126
127static unsigned getELFSectionType(StringRef Name, SectionKind K) {
128
129 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000130 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000131
132 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000133 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000134
135 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000136 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000137
138 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000139 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000140
Rafael Espindolac85dca62011-01-23 04:28:49 +0000141 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000142}
143
144
145static unsigned
146getELFSectionFlags(SectionKind K) {
147 unsigned Flags = 0;
148
149 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000150 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000151
152 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000153 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000154
Rafael Espindolad846e3f2011-06-07 23:26:45 +0000155 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000156 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000157
158 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000159 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000160
161 // K.isMergeableConst() is left out to honour PR4650
162 if (K.isMergeableCString() || K.isMergeableConst4() ||
163 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000164 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000165
166 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000167 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000168
169 return Flags;
170}
171
172
173const MCSection *TargetLoweringObjectFileELF::
174getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
175 Mangler *Mang, const TargetMachine &TM) const {
176 StringRef SectionName = GV->getSection();
177
178 // Infer section flags from the section name if we can.
179 Kind = getELFKindForNamedSection(SectionName, Kind);
180
Chris Lattner287df1b2010-04-08 21:34:17 +0000181 return getContext().getELFSection(SectionName,
182 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000183 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000184}
185
Chris Lattner43ac7212010-04-13 00:36:43 +0000186/// getSectionPrefixForGlobal - Return the section prefix name used by options
187/// FunctionsSections and DataSections.
188static const char *getSectionPrefixForGlobal(SectionKind Kind) {
189 if (Kind.isText()) return ".text.";
190 if (Kind.isReadOnly()) return ".rodata.";
Anton Korobeynikov1d2d5a02012-02-23 10:36:04 +0000191 if (Kind.isBSS()) return ".bss.";
Chris Lattner43ac7212010-04-13 00:36:43 +0000192
193 if (Kind.isThreadData()) return ".tdata.";
194 if (Kind.isThreadBSS()) return ".tbss.";
195
196 if (Kind.isDataNoRel()) return ".data.";
197 if (Kind.isDataRelLocal()) return ".data.rel.local.";
198 if (Kind.isDataRel()) return ".data.rel.";
199 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
200
201 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
202 return ".data.rel.ro.";
203}
204
205
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000206const MCSection *TargetLoweringObjectFileELF::
207SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
208 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000209 // If we have -ffunction-section or -fdata-section then we should emit the
210 // global value to a uniqued section specifically for it.
211 bool EmitUniquedSection;
212 if (Kind.isText())
213 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000214 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000215 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000216
217 // If this global is linkonce/weak and the target handles this by emitting it
218 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000219 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
Anton Korobeynikov1d2d5a02012-02-23 10:36:04 +0000220 !Kind.isCommon()) {
Chris Lattner43ac7212010-04-13 00:36:43 +0000221 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000222 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000223
Chris Lattner4c6741f2010-03-15 20:37:38 +0000224 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
225 MCSymbol *Sym = Mang->getSymbol(GV);
226 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000227 StringRef Group = "";
228 unsigned Flags = getELFSectionFlags(Kind);
229 if (GV->isWeakForLinker()) {
230 Group = Sym->getName();
231 Flags |= ELF::SHF_GROUP;
232 }
233
Chris Lattner287df1b2010-04-08 21:34:17 +0000234 return getContext().getELFSection(Name.str(),
235 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000236 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000237 }
238
239 if (Kind.isText()) return TextSection;
240
241 if (Kind.isMergeable1ByteCString() ||
242 Kind.isMergeable2ByteCString() ||
243 Kind.isMergeable4ByteCString()) {
244
245 // We also need alignment here.
246 // FIXME: this is getting the alignment of the character, not the
247 // alignment of the global!
248 unsigned Align =
249 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
250
251 const char *SizeSpec = ".rodata.str1.";
252 if (Kind.isMergeable2ByteCString())
253 SizeSpec = ".rodata.str2.";
254 else if (Kind.isMergeable4ByteCString())
255 SizeSpec = ".rodata.str4.";
256 else
257 assert(Kind.isMergeable1ByteCString() && "unknown string width");
258
259
260 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000261 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000262 ELF::SHF_ALLOC |
263 ELF::SHF_MERGE |
264 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000265 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000266 }
267
268 if (Kind.isMergeableConst()) {
269 if (Kind.isMergeableConst4() && MergeableConst4Section)
270 return MergeableConst4Section;
271 if (Kind.isMergeableConst8() && MergeableConst8Section)
272 return MergeableConst8Section;
273 if (Kind.isMergeableConst16() && MergeableConst16Section)
274 return MergeableConst16Section;
275 return ReadOnlySection; // .const
276 }
277
278 if (Kind.isReadOnly()) return ReadOnlySection;
279
280 if (Kind.isThreadData()) return TLSDataSection;
281 if (Kind.isThreadBSS()) return TLSBSSSection;
282
283 // Note: we claim that common symbols are put in BSSSection, but they are
284 // really emitted with the magic .comm directive, which creates a symbol table
285 // entry but not a section.
286 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
287
288 if (Kind.isDataNoRel()) return DataSection;
289 if (Kind.isDataRelLocal()) return DataRelLocalSection;
290 if (Kind.isDataRel()) return DataRelSection;
291 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
292
293 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
294 return DataRelROSection;
295}
296
297/// getSectionForConstant - Given a mergeable constant with the
298/// specified size and relocation information, return a section that it
299/// should be placed in.
300const MCSection *TargetLoweringObjectFileELF::
301getSectionForConstant(SectionKind Kind) const {
302 if (Kind.isMergeableConst4() && MergeableConst4Section)
303 return MergeableConst4Section;
304 if (Kind.isMergeableConst8() && MergeableConst8Section)
305 return MergeableConst8Section;
306 if (Kind.isMergeableConst16() && MergeableConst16Section)
307 return MergeableConst16Section;
308 if (Kind.isReadOnly())
309 return ReadOnlySection;
310
311 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
312 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
313 return DataRelROSection;
314}
315
316const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000317getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
318 MachineModuleInfo *MMI,
319 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000320
321 if (Encoding & dwarf::DW_EH_PE_indirect) {
322 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
323
324 SmallString<128> Name;
325 Mang->getNameWithPrefix(Name, GV, true);
326 Name += ".DW.stub";
327
328 // Add information about the stub reference to ELFMMI so that the stub
329 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000330 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000331 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000332 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000333 MCSymbol *Sym = Mang->getSymbol(GV);
334 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000335 }
336
337 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000338 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000339 }
340
341 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000342 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000343}
344
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000345const MCSection *
346TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const {
347 // The default scheme is .ctor / .dtor, so we have to invert the priority
348 // numbering.
349 if (Priority == 65535)
350 return StaticCtorSection;
351
352 std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
353 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
354 ELF::SHF_ALLOC |ELF::SHF_WRITE,
355 SectionKind::getDataRel());
356}
357
358const MCSection *
359TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
360 // The default scheme is .ctor / .dtor, so we have to invert the priority
361 // numbering.
362 if (Priority == 65535)
363 return StaticDtorSection;
364
365 std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
366 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
367 ELF::SHF_ALLOC |ELF::SHF_WRITE,
368 SectionKind::getDataRel());
369}
370
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000371//===----------------------------------------------------------------------===//
372// MachO
373//===----------------------------------------------------------------------===//
374
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000375/// emitModuleFlags - Emit the module flags that specify the garbage collection
376/// information.
377void TargetLoweringObjectFileMachO::
Bill Wendling057d5212012-02-15 22:36:15 +0000378emitModuleFlags(MCStreamer &Streamer,
379 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000380 Mangler *Mang, const TargetMachine &TM) const {
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000381 unsigned VersionVal = 0;
382 unsigned GCFlags = 0;
Bill Wendling057d5212012-02-15 22:36:15 +0000383 StringRef SectionVal;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000384
Bill Wendling057d5212012-02-15 22:36:15 +0000385 for (ArrayRef<Module::ModuleFlagEntry>::iterator
386 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
387 const Module::ModuleFlagEntry &MFE = *i;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000388
389 // Ignore flags with 'Require' behavior.
Bill Wendling057d5212012-02-15 22:36:15 +0000390 if (MFE.Behavior == Module::Require)
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000391 continue;
392
Bill Wendling057d5212012-02-15 22:36:15 +0000393 StringRef Key = MFE.Key->getString();
394 Value *Val = MFE.Val;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000395
Bill Wendling057d5212012-02-15 22:36:15 +0000396 if (Key == "Objective-C Image Info Version")
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000397 VersionVal = cast<ConstantInt>(Val)->getZExtValue();
Bill Wendling057d5212012-02-15 22:36:15 +0000398 else if (Key == "Objective-C Garbage Collection" ||
399 Key == "Objective-C GC Only")
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000400 GCFlags |= cast<ConstantInt>(Val)->getZExtValue();
Bill Wendling057d5212012-02-15 22:36:15 +0000401 else if (Key == "Objective-C Image Info Section")
402 SectionVal = cast<MDString>(Val)->getString();
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000403 }
404
Bill Wendling057d5212012-02-15 22:36:15 +0000405 // The section is mandatory. If we don't have it, then we don't have GC info.
406 if (SectionVal.empty()) return;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000407
Bill Wendling057d5212012-02-15 22:36:15 +0000408 StringRef Segment, Section;
409 unsigned TAA = 0, StubSize = 0;
410 bool TAAParsed;
411 std::string ErrorCode =
412 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
413 TAA, TAAParsed, StubSize);
Bill Wendling2de3ff52012-02-15 22:47:53 +0000414 if (!ErrorCode.empty())
Bill Wendling057d5212012-02-15 22:36:15 +0000415 // If invalid, report the error with report_fatal_error.
Bill Wendling2de3ff52012-02-15 22:47:53 +0000416 report_fatal_error("Invalid section specifier '" + Section + "': " +
417 ErrorCode + ".");
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000418
Bill Wendling057d5212012-02-15 22:36:15 +0000419 // Get the section.
420 const MCSectionMachO *S =
421 getContext().getMachOSection(Segment, Section, TAA, StubSize,
Bill Wendling2de3ff52012-02-15 22:47:53 +0000422 SectionKind::getDataNoRel());
Bill Wendling057d5212012-02-15 22:36:15 +0000423 Streamer.SwitchSection(S);
424 Streamer.EmitLabel(getContext().
425 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000426 Streamer.EmitIntValue(VersionVal, 4);
427 Streamer.EmitIntValue(GCFlags, 4);
428 Streamer.AddBlankLine();
429}
430
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000431const MCSection *TargetLoweringObjectFileMachO::
432getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
433 Mangler *Mang, const TargetMachine &TM) const {
434 // Parse the section specifier and create it if valid.
435 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000436 unsigned TAA = 0, StubSize = 0;
437 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000438 std::string ErrorCode =
439 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000440 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000441 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000442 // If invalid, report the error with report_fatal_error.
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000443 report_fatal_error("Global variable '" + GV->getName() +
444 "' has an invalid section specifier '" +
445 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000446 }
447
448 // Get the section.
449 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000450 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000451
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000452 // If TAA wasn't set by ParseSectionSpecifier() above,
453 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000454 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000455 TAA = S->getTypeAndAttributes();
456
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000457 // Okay, now that we got the section, verify that the TAA & StubSize agree.
458 // If the user declared multiple globals with different section flags, we need
459 // to reject it here.
460 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000461 // If invalid, report the error with report_fatal_error.
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000462 report_fatal_error("Global variable '" + GV->getName() +
463 "' section type or attributes does not match previous"
464 " section specifier");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000465 }
466
467 return S;
468}
469
470const MCSection *TargetLoweringObjectFileMachO::
471SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000472 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000473
Eric Christopher02b46bc2010-05-25 21:28:50 +0000474 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000475 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000476 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000477
478 if (Kind.isText())
479 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
480
481 // If this is weak/linkonce, put this in a coalescable section, either in text
482 // or data depending on if it is writable.
483 if (GV->isWeakForLinker()) {
484 if (Kind.isReadOnly())
485 return ConstTextCoalSection;
486 return DataCoalSection;
487 }
488
489 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000490 if (Kind.isMergeable1ByteCString() &&
491 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
492 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000493
Chris Lattner98f15d22010-03-07 04:28:09 +0000494 // Do not put 16-bit arrays in the UString section if they have an
495 // externally visible label, this runs into issues with certain linker
496 // versions.
497 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
498 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
499 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000500
501 if (Kind.isMergeableConst()) {
502 if (Kind.isMergeableConst4())
503 return FourByteConstantSection;
504 if (Kind.isMergeableConst8())
505 return EightByteConstantSection;
506 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
507 return SixteenByteConstantSection;
508 }
509
510 // Otherwise, if it is readonly, but not something we can specially optimize,
511 // just drop it in .const.
512 if (Kind.isReadOnly())
513 return ReadOnlySection;
514
515 // If this is marked const, put it into a const section. But if the dynamic
516 // linker needs to write to it, put it in the data segment.
517 if (Kind.isReadOnlyWithRel())
518 return ConstDataSection;
519
520 // Put zero initialized globals with strong external linkage in the
521 // DATA, __common section with the .zerofill directive.
522 if (Kind.isBSSExtern())
523 return DataCommonSection;
524
525 // Put zero initialized globals with local linkage in __DATA,__bss directive
526 // with the .zerofill directive (aka .lcomm).
527 if (Kind.isBSSLocal())
528 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000529
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000530 // Otherwise, just drop the variable in the normal data section.
531 return DataSection;
532}
533
534const MCSection *
535TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
536 // If this constant requires a relocation, we have to put it in the data
537 // segment, not in the text segment.
538 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
539 return ConstDataSection;
540
541 if (Kind.isMergeableConst4())
542 return FourByteConstantSection;
543 if (Kind.isMergeableConst8())
544 return EightByteConstantSection;
545 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
546 return SixteenByteConstantSection;
547 return ReadOnlySection; // .const
548}
549
550/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
551/// not to emit the UsedDirective for some symbols in llvm.used.
552// FIXME: REMOVE this (rdar://7071300)
553bool TargetLoweringObjectFileMachO::
554shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
555 /// On Darwin, internally linked data beginning with "L" or "l" does not have
556 /// the directive emitted (this occurs in ObjC metadata).
557 if (!GV) return false;
558
Bill Wendling07d31772010-06-29 22:34:52 +0000559 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000560 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
561 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000562 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
563 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000564 MCSymbol *Sym = Mang->getSymbol(GV);
565 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000566 return false;
567 }
568
569 return true;
570}
571
572const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000573getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
574 MachineModuleInfo *MMI, unsigned Encoding,
575 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000576 // The mach-o version of this method defaults to returning a stub reference.
577
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000578 if (Encoding & DW_EH_PE_indirect) {
579 MachineModuleInfoMachO &MachOMMI =
580 MMI->getObjFileInfo<MachineModuleInfoMachO>();
581
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000582 SmallString<128> Name;
583 Mang->getNameWithPrefix(Name, GV, true);
584 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000585
586 // Add information about the stub reference to MachOMMI so that the stub
587 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000588 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Bill Wendling67121542011-10-24 23:05:43 +0000589 MachineModuleInfoImpl::StubValueTy &StubSym =
590 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
591 MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000592 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000593 MCSymbol *Sym = Mang->getSymbol(GV);
594 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000595 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000596
597 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000598 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000599 }
600
601 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000602 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000603}
604
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000605MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindola60246a92011-04-27 23:17:57 +0000606getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000607 MachineModuleInfo *MMI) const {
608 // The mach-o version of this method defaults to returning a stub reference.
609 MachineModuleInfoMachO &MachOMMI =
610 MMI->getObjFileInfo<MachineModuleInfoMachO>();
611
612 SmallString<128> Name;
613 Mang->getNameWithPrefix(Name, GV, true);
614 Name += "$non_lazy_ptr";
615
616 // Add information about the stub reference to MachOMMI so that the stub
617 // gets emitted by the asmprinter.
618 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Bill Wendlingd7c24942011-11-29 01:43:20 +0000619 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000620 if (StubSym.getPointer() == 0) {
621 MCSymbol *Sym = Mang->getSymbol(GV);
622 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
623 }
624
625 return SSym;
626}
627
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000628//===----------------------------------------------------------------------===//
629// COFF
630//===----------------------------------------------------------------------===//
631
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000632static unsigned
633getCOFFSectionFlags(SectionKind K) {
634 unsigned Flags = 0;
635
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000636 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000637 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000638 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000639 else if (K.isText())
640 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000641 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000642 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000643 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000644 else if (K.isBSS ())
645 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000646 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
647 COFF::IMAGE_SCN_MEM_READ |
648 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000649 else if (K.isThreadLocal())
650 Flags |=
651 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
652 COFF::IMAGE_SCN_MEM_READ |
653 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000654 else if (K.isReadOnly())
655 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000656 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
657 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000658 else if (K.isWriteable())
659 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000660 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
661 COFF::IMAGE_SCN_MEM_READ |
662 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000663
664 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000665}
666
667const MCSection *TargetLoweringObjectFileCOFF::
668getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
669 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000670 return getContext().getCOFFSection(GV->getSection(),
671 getCOFFSectionFlags(Kind),
672 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000673}
674
675static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
676 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000677 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000678 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000679 return ".bss$";
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000680 if (Kind.isThreadLocal())
681 return ".tls$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000682 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000683 return ".data$";
684 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000685}
686
687
688const MCSection *TargetLoweringObjectFileCOFF::
689SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
690 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000691
692 // If this global is linkonce/weak and the target handles this by emitting it
693 // into a 'uniqued' section name, create and return the section now.
694 if (GV->isWeakForLinker()) {
695 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
696 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000697 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000698 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +0000699
700 unsigned Characteristics = getCOFFSectionFlags(Kind);
701
Daniel Dunbar94610582010-07-01 20:07:24 +0000702 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000703
704 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +0000705 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000706 }
707
708 if (Kind.isText())
709 return getTextSection();
710
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +0000711 if (Kind.isThreadLocal())
712 return getTLSDataSection();
713
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000714 return getDataSection();
715}
716