blob: a6999f63040ffe14aab109e2b139393857dad78e [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"
20#include "llvm/CodeGen/MachineModuleInfoImpls.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCSectionMachO.h"
24#include "llvm/MC/MCSectionELF.h"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000025#include "llvm/MC/MCSectionCOFF.h"
Rafael Espindola30deafc2011-04-16 03:51:21 +000026#include "llvm/MC/MCStreamer.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000027#include "llvm/MC/MCSymbol.h"
28#include "llvm/Target/Mangler.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
32#include "llvm/Support/Dwarf.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000033#include "llvm/Support/ELF.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
36#include "llvm/ADT/SmallString.h"
37#include "llvm/ADT/StringExtras.h"
Chris Lattner8048ebe2010-09-27 06:44:54 +000038#include "llvm/ADT/Triple.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000039using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000040using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000041
42//===----------------------------------------------------------------------===//
43// ELF
44//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000045
Rafael Espindola30deafc2011-04-16 03:51:21 +000046MCSymbol *
Rafael Espindola7afec9c2011-04-27 23:08:15 +000047TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
Rafael Espindola7afec9c2011-04-27 23:08:15 +000048 Mangler *Mang,
49 MachineModuleInfo *MMI) const {
Rafael Espindola60246a92011-04-27 23:17:57 +000050 unsigned Encoding = getPersonalityEncoding();
Rafael Espindola7afec9c2011-04-27 23:08:15 +000051 switch (Encoding & 0x70) {
52 default:
53 report_fatal_error("We do not support this DWARF encoding yet!");
54 case dwarf::DW_EH_PE_absptr:
55 return Mang->getSymbol(GV);
Rafael Espindola7afec9c2011-04-27 23:08:15 +000056 case dwarf::DW_EH_PE_pcrel: {
Rafael Espindolafb66f472011-06-13 03:09:13 +000057 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
58 Mang->getSymbol(GV)->getName());
Rafael Espindola7afec9c2011-04-27 23:08:15 +000059 }
60 }
Rafael Espindola30deafc2011-04-16 03:51:21 +000061}
62
63void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
64 const TargetMachine &TM,
Rafael Espindola7afec9c2011-04-27 23:08:15 +000065 const MCSymbol *Sym) const {
Rafael Espindolafb66f472011-06-13 03:09:13 +000066 SmallString<64> NameData("DW.ref.");
67 NameData += Sym->getName();
68 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
Rafael Espindola018e38c2011-04-27 21:29:52 +000069 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
70 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindolafb66f472011-06-13 03:09:13 +000071 StringRef Prefix = ".data.";
72 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola018e38c2011-04-27 21:29:52 +000073 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
74 const MCSection *Sec = getContext().getELFSection(NameData,
75 ELF::SHT_PROGBITS,
76 Flags,
77 SectionKind::getDataRel(),
78 0, Label->getName());
79 Streamer.SwitchSection(Sec);
80 Streamer.EmitValueToAlignment(8);
81 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
82 const MCExpr *E = MCConstantExpr::Create(8, getContext());
83 Streamer.EmitELFSize(Label, E);
84 Streamer.EmitLabel(Label);
Rafael Espindola30deafc2011-04-16 03:51:21 +000085
Rafael Espindola018e38c2011-04-27 21:29:52 +000086 unsigned Size = TM.getTargetData()->getPointerSize();
87 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindola30deafc2011-04-16 03:51:21 +000088}
89
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000090static SectionKind
91getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindolae6657982011-05-24 03:10:31 +000092 // N.B.: The defaults used in here are no the same ones used in MC.
93 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
94 // both gas and MC will produce a section with no flags. Given
95 // section(".eh_frame") gcc will produce
96 // .section .eh_frame,"a",@progbits
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000097 if (Name.empty() || Name[0] != '.') return K;
98
99 // Some lame default implementation based on some magic section names.
100 if (Name == ".bss" ||
101 Name.startswith(".bss.") ||
102 Name.startswith(".gnu.linkonce.b.") ||
103 Name.startswith(".llvm.linkonce.b.") ||
104 Name == ".sbss" ||
105 Name.startswith(".sbss.") ||
106 Name.startswith(".gnu.linkonce.sb.") ||
107 Name.startswith(".llvm.linkonce.sb."))
108 return SectionKind::getBSS();
109
110 if (Name == ".tdata" ||
111 Name.startswith(".tdata.") ||
112 Name.startswith(".gnu.linkonce.td.") ||
113 Name.startswith(".llvm.linkonce.td."))
114 return SectionKind::getThreadData();
115
116 if (Name == ".tbss" ||
117 Name.startswith(".tbss.") ||
118 Name.startswith(".gnu.linkonce.tb.") ||
119 Name.startswith(".llvm.linkonce.tb."))
120 return SectionKind::getThreadBSS();
121
122 return K;
123}
124
125
126static unsigned getELFSectionType(StringRef Name, SectionKind K) {
127
128 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000129 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000130
131 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000132 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000133
134 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000135 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000136
137 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000138 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000139
Rafael Espindolac85dca62011-01-23 04:28:49 +0000140 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000141}
142
143
144static unsigned
145getELFSectionFlags(SectionKind K) {
146 unsigned Flags = 0;
147
148 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000149 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000150
151 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000152 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000153
Rafael Espindolad846e3f2011-06-07 23:26:45 +0000154 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000155 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000156
157 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000158 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000159
160 // K.isMergeableConst() is left out to honour PR4650
161 if (K.isMergeableCString() || K.isMergeableConst4() ||
162 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000163 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000164
165 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000166 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000167
168 return Flags;
169}
170
171
172const MCSection *TargetLoweringObjectFileELF::
173getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
174 Mangler *Mang, const TargetMachine &TM) const {
175 StringRef SectionName = GV->getSection();
176
177 // Infer section flags from the section name if we can.
178 Kind = getELFKindForNamedSection(SectionName, Kind);
179
Chris Lattner287df1b2010-04-08 21:34:17 +0000180 return getContext().getELFSection(SectionName,
181 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000182 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000183}
184
Chris Lattner43ac7212010-04-13 00:36:43 +0000185/// getSectionPrefixForGlobal - Return the section prefix name used by options
186/// FunctionsSections and DataSections.
187static const char *getSectionPrefixForGlobal(SectionKind Kind) {
188 if (Kind.isText()) return ".text.";
189 if (Kind.isReadOnly()) return ".rodata.";
190
191 if (Kind.isThreadData()) return ".tdata.";
192 if (Kind.isThreadBSS()) return ".tbss.";
193
194 if (Kind.isDataNoRel()) return ".data.";
195 if (Kind.isDataRelLocal()) return ".data.rel.local.";
196 if (Kind.isDataRel()) return ".data.rel.";
197 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
198
199 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
200 return ".data.rel.ro.";
201}
202
203
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000204const MCSection *TargetLoweringObjectFileELF::
205SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
206 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000207 // If we have -ffunction-section or -fdata-section then we should emit the
208 // global value to a uniqued section specifically for it.
209 bool EmitUniquedSection;
210 if (Kind.isText())
211 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000212 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000213 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000214
215 // If this global is linkonce/weak and the target handles this by emitting it
216 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000217 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
218 !Kind.isCommon() && !Kind.isBSS()) {
219 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000220 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000221
Chris Lattner4c6741f2010-03-15 20:37:38 +0000222 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
223 MCSymbol *Sym = Mang->getSymbol(GV);
224 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000225 StringRef Group = "";
226 unsigned Flags = getELFSectionFlags(Kind);
227 if (GV->isWeakForLinker()) {
228 Group = Sym->getName();
229 Flags |= ELF::SHF_GROUP;
230 }
231
Chris Lattner287df1b2010-04-08 21:34:17 +0000232 return getContext().getELFSection(Name.str(),
233 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000234 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000235 }
236
237 if (Kind.isText()) return TextSection;
238
239 if (Kind.isMergeable1ByteCString() ||
240 Kind.isMergeable2ByteCString() ||
241 Kind.isMergeable4ByteCString()) {
242
243 // We also need alignment here.
244 // FIXME: this is getting the alignment of the character, not the
245 // alignment of the global!
246 unsigned Align =
247 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
248
249 const char *SizeSpec = ".rodata.str1.";
250 if (Kind.isMergeable2ByteCString())
251 SizeSpec = ".rodata.str2.";
252 else if (Kind.isMergeable4ByteCString())
253 SizeSpec = ".rodata.str4.";
254 else
255 assert(Kind.isMergeable1ByteCString() && "unknown string width");
256
257
258 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000259 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000260 ELF::SHF_ALLOC |
261 ELF::SHF_MERGE |
262 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000263 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000264 }
265
266 if (Kind.isMergeableConst()) {
267 if (Kind.isMergeableConst4() && MergeableConst4Section)
268 return MergeableConst4Section;
269 if (Kind.isMergeableConst8() && MergeableConst8Section)
270 return MergeableConst8Section;
271 if (Kind.isMergeableConst16() && MergeableConst16Section)
272 return MergeableConst16Section;
273 return ReadOnlySection; // .const
274 }
275
276 if (Kind.isReadOnly()) return ReadOnlySection;
277
278 if (Kind.isThreadData()) return TLSDataSection;
279 if (Kind.isThreadBSS()) return TLSBSSSection;
280
281 // Note: we claim that common symbols are put in BSSSection, but they are
282 // really emitted with the magic .comm directive, which creates a symbol table
283 // entry but not a section.
284 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
285
286 if (Kind.isDataNoRel()) return DataSection;
287 if (Kind.isDataRelLocal()) return DataRelLocalSection;
288 if (Kind.isDataRel()) return DataRelSection;
289 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
290
291 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
292 return DataRelROSection;
293}
294
295/// getSectionForConstant - Given a mergeable constant with the
296/// specified size and relocation information, return a section that it
297/// should be placed in.
298const MCSection *TargetLoweringObjectFileELF::
299getSectionForConstant(SectionKind Kind) const {
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 if (Kind.isReadOnly())
307 return ReadOnlySection;
308
309 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
310 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
311 return DataRelROSection;
312}
313
314const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000315getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
316 MachineModuleInfo *MMI,
317 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000318
319 if (Encoding & dwarf::DW_EH_PE_indirect) {
320 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
321
322 SmallString<128> Name;
323 Mang->getNameWithPrefix(Name, GV, true);
324 Name += ".DW.stub";
325
326 // Add information about the stub reference to ELFMMI so that the stub
327 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000328 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000329 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000330 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000331 MCSymbol *Sym = Mang->getSymbol(GV);
332 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000333 }
334
335 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000336 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000337 }
338
339 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000340 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000341}
342
343//===----------------------------------------------------------------------===//
344// MachO
345//===----------------------------------------------------------------------===//
346
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000347const MCSection *TargetLoweringObjectFileMachO::
348getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
349 Mangler *Mang, const TargetMachine &TM) const {
350 // Parse the section specifier and create it if valid.
351 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000352 unsigned TAA = 0, StubSize = 0;
353 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000354 std::string ErrorCode =
355 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000356 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000357 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000358 // If invalid, report the error with report_fatal_error.
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000359 report_fatal_error("Global variable '" + GV->getName() +
360 "' has an invalid section specifier '" +
361 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000362 }
363
364 // Get the section.
365 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000366 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000367
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000368 // If TAA wasn't set by ParseSectionSpecifier() above,
369 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000370 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000371 TAA = S->getTypeAndAttributes();
372
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000373 // Okay, now that we got the section, verify that the TAA & StubSize agree.
374 // If the user declared multiple globals with different section flags, we need
375 // to reject it here.
376 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000377 // If invalid, report the error with report_fatal_error.
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000378 report_fatal_error("Global variable '" + GV->getName() +
379 "' section type or attributes does not match previous"
380 " section specifier");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000381 }
382
383 return S;
384}
385
386const MCSection *TargetLoweringObjectFileMachO::
387SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000388 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000389
Eric Christopher02b46bc2010-05-25 21:28:50 +0000390 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000391 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000392 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000393
394 if (Kind.isText())
395 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
396
397 // If this is weak/linkonce, put this in a coalescable section, either in text
398 // or data depending on if it is writable.
399 if (GV->isWeakForLinker()) {
400 if (Kind.isReadOnly())
401 return ConstTextCoalSection;
402 return DataCoalSection;
403 }
404
405 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000406 if (Kind.isMergeable1ByteCString() &&
407 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
408 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000409
Chris Lattner98f15d22010-03-07 04:28:09 +0000410 // Do not put 16-bit arrays in the UString section if they have an
411 // externally visible label, this runs into issues with certain linker
412 // versions.
413 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
414 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
415 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000416
417 if (Kind.isMergeableConst()) {
418 if (Kind.isMergeableConst4())
419 return FourByteConstantSection;
420 if (Kind.isMergeableConst8())
421 return EightByteConstantSection;
422 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
423 return SixteenByteConstantSection;
424 }
425
426 // Otherwise, if it is readonly, but not something we can specially optimize,
427 // just drop it in .const.
428 if (Kind.isReadOnly())
429 return ReadOnlySection;
430
431 // If this is marked const, put it into a const section. But if the dynamic
432 // linker needs to write to it, put it in the data segment.
433 if (Kind.isReadOnlyWithRel())
434 return ConstDataSection;
435
436 // Put zero initialized globals with strong external linkage in the
437 // DATA, __common section with the .zerofill directive.
438 if (Kind.isBSSExtern())
439 return DataCommonSection;
440
441 // Put zero initialized globals with local linkage in __DATA,__bss directive
442 // with the .zerofill directive (aka .lcomm).
443 if (Kind.isBSSLocal())
444 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000445
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000446 // Otherwise, just drop the variable in the normal data section.
447 return DataSection;
448}
449
450const MCSection *
451TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
452 // If this constant requires a relocation, we have to put it in the data
453 // segment, not in the text segment.
454 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
455 return ConstDataSection;
456
457 if (Kind.isMergeableConst4())
458 return FourByteConstantSection;
459 if (Kind.isMergeableConst8())
460 return EightByteConstantSection;
461 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
462 return SixteenByteConstantSection;
463 return ReadOnlySection; // .const
464}
465
466/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
467/// not to emit the UsedDirective for some symbols in llvm.used.
468// FIXME: REMOVE this (rdar://7071300)
469bool TargetLoweringObjectFileMachO::
470shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
471 /// On Darwin, internally linked data beginning with "L" or "l" does not have
472 /// the directive emitted (this occurs in ObjC metadata).
473 if (!GV) return false;
474
Bill Wendling07d31772010-06-29 22:34:52 +0000475 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000476 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
477 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000478 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
479 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000480 MCSymbol *Sym = Mang->getSymbol(GV);
481 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000482 return false;
483 }
484
485 return true;
486}
487
488const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000489getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
490 MachineModuleInfo *MMI, unsigned Encoding,
491 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000492 // The mach-o version of this method defaults to returning a stub reference.
493
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000494 if (Encoding & DW_EH_PE_indirect) {
495 MachineModuleInfoMachO &MachOMMI =
496 MMI->getObjFileInfo<MachineModuleInfoMachO>();
497
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000498 SmallString<128> Name;
499 Mang->getNameWithPrefix(Name, GV, true);
500 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000501
502 // Add information about the stub reference to MachOMMI so that the stub
503 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000504 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Bill Wendling67121542011-10-24 23:05:43 +0000505 MachineModuleInfoImpl::StubValueTy &StubSym =
506 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
507 MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000508 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000509 MCSymbol *Sym = Mang->getSymbol(GV);
510 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000511 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000512
513 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000514 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000515 }
516
517 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000518 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000519}
520
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000521MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindola60246a92011-04-27 23:17:57 +0000522getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000523 MachineModuleInfo *MMI) const {
524 // The mach-o version of this method defaults to returning a stub reference.
525 MachineModuleInfoMachO &MachOMMI =
526 MMI->getObjFileInfo<MachineModuleInfoMachO>();
527
528 SmallString<128> Name;
529 Mang->getNameWithPrefix(Name, GV, true);
530 Name += "$non_lazy_ptr";
531
532 // Add information about the stub reference to MachOMMI so that the stub
533 // gets emitted by the asmprinter.
534 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Bill Wendlingd7c24942011-11-29 01:43:20 +0000535 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000536 if (StubSym.getPointer() == 0) {
537 MCSymbol *Sym = Mang->getSymbol(GV);
538 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
539 }
540
541 return SSym;
542}
543
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000544//===----------------------------------------------------------------------===//
545// COFF
546//===----------------------------------------------------------------------===//
547
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000548static unsigned
549getCOFFSectionFlags(SectionKind K) {
550 unsigned Flags = 0;
551
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000552 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000553 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000554 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000555 else if (K.isText())
556 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000557 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000558 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000559 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000560 else if (K.isBSS ())
561 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000562 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
563 COFF::IMAGE_SCN_MEM_READ |
564 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000565 else if (K.isReadOnly())
566 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000567 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
568 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000569 else if (K.isWriteable())
570 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000571 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
572 COFF::IMAGE_SCN_MEM_READ |
573 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000574
575 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000576}
577
578const MCSection *TargetLoweringObjectFileCOFF::
579getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
580 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000581 return getContext().getCOFFSection(GV->getSection(),
582 getCOFFSectionFlags(Kind),
583 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000584}
585
586static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
587 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000588 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000589 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000590 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000591 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000592 return ".data$";
593 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000594}
595
596
597const MCSection *TargetLoweringObjectFileCOFF::
598SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
599 Mangler *Mang, const TargetMachine &TM) const {
600 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
601
602 // If this global is linkonce/weak and the target handles this by emitting it
603 // into a 'uniqued' section name, create and return the section now.
604 if (GV->isWeakForLinker()) {
605 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
606 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000607 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000608 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +0000609
610 unsigned Characteristics = getCOFFSectionFlags(Kind);
611
Daniel Dunbar94610582010-07-01 20:07:24 +0000612 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000613
614 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +0000615 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000616 }
617
618 if (Kind.isText())
619 return getTextSection();
620
621 return getDataSection();
622}
623