blob: 427338b7579bfbbc017f32b39ed1aeb4807f9771 [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"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/Target/Mangler.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetOptions.h"
30#include "llvm/Support/Dwarf.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/ADT/SmallString.h"
34#include "llvm/ADT/StringExtras.h"
35using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000036using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000037
38//===----------------------------------------------------------------------===//
39// ELF
40//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000041
42const MCSection *TargetLoweringObjectFileELF::
43getELFSection(StringRef Section, unsigned Type, unsigned Flags,
44 SectionKind Kind, bool IsExplicit) const {
Chris Lattner74aae472010-04-08 21:26:26 +000045 return getContext().getELFSection(Section, Type, Flags, Kind, IsExplicit);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000046
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000047}
48
49void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
50 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000051 TargetLoweringObjectFile::Initialize(Ctx, TM);
52
53 BSSSection =
54 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
55 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
56 SectionKind::getBSS());
57
58 TextSection =
59 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
60 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
61 SectionKind::getText());
62
63 DataSection =
64 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
65 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
66 SectionKind::getDataRel());
67
68 ReadOnlySection =
69 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
70 MCSectionELF::SHF_ALLOC,
71 SectionKind::getReadOnly());
72
73 TLSDataSection =
74 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
75 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
76 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
77
78 TLSBSSSection =
79 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
80 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
81 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
82
83 DataRelSection =
84 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
85 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
86 SectionKind::getDataRel());
87
88 DataRelLocalSection =
89 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
90 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
91 SectionKind::getDataRelLocal());
92
93 DataRelROSection =
94 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
95 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
96 SectionKind::getReadOnlyWithRel());
97
98 DataRelROLocalSection =
99 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
100 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
101 SectionKind::getReadOnlyWithRelLocal());
102
103 MergeableConst4Section =
104 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
105 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
106 SectionKind::getMergeableConst4());
107
108 MergeableConst8Section =
109 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
110 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
111 SectionKind::getMergeableConst8());
112
113 MergeableConst16Section =
114 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
115 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
116 SectionKind::getMergeableConst16());
117
118 StaticCtorSection =
119 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
120 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
121 SectionKind::getDataRel());
122
123 StaticDtorSection =
124 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
125 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
126 SectionKind::getDataRel());
127
128 // Exception Handling Sections.
129
130 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
131 // it contains relocatable pointers. In PIC mode, this is probably a big
132 // runtime hit for C++ apps. Either the contents of the LSDA need to be
133 // adjusted or this should be a data section.
134 LSDASection =
135 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
136 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
137 EHFrameSection =
138 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
139 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
140 SectionKind::getDataRel());
141
142 // Debug Info Sections.
143 DwarfAbbrevSection =
144 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
145 SectionKind::getMetadata());
146 DwarfInfoSection =
147 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
148 SectionKind::getMetadata());
149 DwarfLineSection =
150 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
151 SectionKind::getMetadata());
152 DwarfFrameSection =
153 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
154 SectionKind::getMetadata());
155 DwarfPubNamesSection =
156 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
157 SectionKind::getMetadata());
158 DwarfPubTypesSection =
159 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
160 SectionKind::getMetadata());
161 DwarfStrSection =
162 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
163 SectionKind::getMetadata());
164 DwarfLocSection =
165 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
166 SectionKind::getMetadata());
167 DwarfARangesSection =
168 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
169 SectionKind::getMetadata());
170 DwarfRangesSection =
171 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
172 SectionKind::getMetadata());
173 DwarfMacroInfoSection =
174 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
175 SectionKind::getMetadata());
176}
177
178
179static SectionKind
180getELFKindForNamedSection(StringRef Name, SectionKind K) {
181 if (Name.empty() || Name[0] != '.') return K;
182
183 // Some lame default implementation based on some magic section names.
184 if (Name == ".bss" ||
185 Name.startswith(".bss.") ||
186 Name.startswith(".gnu.linkonce.b.") ||
187 Name.startswith(".llvm.linkonce.b.") ||
188 Name == ".sbss" ||
189 Name.startswith(".sbss.") ||
190 Name.startswith(".gnu.linkonce.sb.") ||
191 Name.startswith(".llvm.linkonce.sb."))
192 return SectionKind::getBSS();
193
194 if (Name == ".tdata" ||
195 Name.startswith(".tdata.") ||
196 Name.startswith(".gnu.linkonce.td.") ||
197 Name.startswith(".llvm.linkonce.td."))
198 return SectionKind::getThreadData();
199
200 if (Name == ".tbss" ||
201 Name.startswith(".tbss.") ||
202 Name.startswith(".gnu.linkonce.tb.") ||
203 Name.startswith(".llvm.linkonce.tb."))
204 return SectionKind::getThreadBSS();
205
206 return K;
207}
208
209
210static unsigned getELFSectionType(StringRef Name, SectionKind K) {
211
212 if (Name == ".init_array")
213 return MCSectionELF::SHT_INIT_ARRAY;
214
215 if (Name == ".fini_array")
216 return MCSectionELF::SHT_FINI_ARRAY;
217
218 if (Name == ".preinit_array")
219 return MCSectionELF::SHT_PREINIT_ARRAY;
220
221 if (K.isBSS() || K.isThreadBSS())
222 return MCSectionELF::SHT_NOBITS;
223
224 return MCSectionELF::SHT_PROGBITS;
225}
226
227
228static unsigned
229getELFSectionFlags(SectionKind K) {
230 unsigned Flags = 0;
231
232 if (!K.isMetadata())
233 Flags |= MCSectionELF::SHF_ALLOC;
234
235 if (K.isText())
236 Flags |= MCSectionELF::SHF_EXECINSTR;
237
238 if (K.isWriteable())
239 Flags |= MCSectionELF::SHF_WRITE;
240
241 if (K.isThreadLocal())
242 Flags |= MCSectionELF::SHF_TLS;
243
244 // K.isMergeableConst() is left out to honour PR4650
245 if (K.isMergeableCString() || K.isMergeableConst4() ||
246 K.isMergeableConst8() || K.isMergeableConst16())
247 Flags |= MCSectionELF::SHF_MERGE;
248
249 if (K.isMergeableCString())
250 Flags |= MCSectionELF::SHF_STRINGS;
251
252 return Flags;
253}
254
255
256const MCSection *TargetLoweringObjectFileELF::
257getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
258 Mangler *Mang, const TargetMachine &TM) const {
259 StringRef SectionName = GV->getSection();
260
261 // Infer section flags from the section name if we can.
262 Kind = getELFKindForNamedSection(SectionName, Kind);
263
264 return getELFSection(SectionName,
265 getELFSectionType(SectionName, Kind),
266 getELFSectionFlags(Kind), Kind, true);
267}
268
269static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
270 if (Kind.isText()) return ".gnu.linkonce.t.";
271 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
272
273 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
274 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
275
276 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
277 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
278 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
279 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
280
281 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
282 return ".gnu.linkonce.d.rel.ro.";
283}
284
285const MCSection *TargetLoweringObjectFileELF::
286SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
287 Mangler *Mang, const TargetMachine &TM) const {
288
289 // If this global is linkonce/weak and the target handles this by emitting it
290 // into a 'uniqued' section name, create and return the section now.
291 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
292 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattner4c6741f2010-03-15 20:37:38 +0000293 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
294 MCSymbol *Sym = Mang->getSymbol(GV);
295 Name.append(Sym->getName().begin(), Sym->getName().end());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000296 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
297 getELFSectionFlags(Kind), Kind);
298 }
299
300 if (Kind.isText()) return TextSection;
301
302 if (Kind.isMergeable1ByteCString() ||
303 Kind.isMergeable2ByteCString() ||
304 Kind.isMergeable4ByteCString()) {
305
306 // We also need alignment here.
307 // FIXME: this is getting the alignment of the character, not the
308 // alignment of the global!
309 unsigned Align =
310 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
311
312 const char *SizeSpec = ".rodata.str1.";
313 if (Kind.isMergeable2ByteCString())
314 SizeSpec = ".rodata.str2.";
315 else if (Kind.isMergeable4ByteCString())
316 SizeSpec = ".rodata.str4.";
317 else
318 assert(Kind.isMergeable1ByteCString() && "unknown string width");
319
320
321 std::string Name = SizeSpec + utostr(Align);
322 return getELFSection(Name, MCSectionELF::SHT_PROGBITS,
323 MCSectionELF::SHF_ALLOC |
324 MCSectionELF::SHF_MERGE |
325 MCSectionELF::SHF_STRINGS,
326 Kind);
327 }
328
329 if (Kind.isMergeableConst()) {
330 if (Kind.isMergeableConst4() && MergeableConst4Section)
331 return MergeableConst4Section;
332 if (Kind.isMergeableConst8() && MergeableConst8Section)
333 return MergeableConst8Section;
334 if (Kind.isMergeableConst16() && MergeableConst16Section)
335 return MergeableConst16Section;
336 return ReadOnlySection; // .const
337 }
338
339 if (Kind.isReadOnly()) return ReadOnlySection;
340
341 if (Kind.isThreadData()) return TLSDataSection;
342 if (Kind.isThreadBSS()) return TLSBSSSection;
343
344 // Note: we claim that common symbols are put in BSSSection, but they are
345 // really emitted with the magic .comm directive, which creates a symbol table
346 // entry but not a section.
347 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
348
349 if (Kind.isDataNoRel()) return DataSection;
350 if (Kind.isDataRelLocal()) return DataRelLocalSection;
351 if (Kind.isDataRel()) return DataRelSection;
352 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
353
354 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
355 return DataRelROSection;
356}
357
358/// getSectionForConstant - Given a mergeable constant with the
359/// specified size and relocation information, return a section that it
360/// should be placed in.
361const MCSection *TargetLoweringObjectFileELF::
362getSectionForConstant(SectionKind Kind) const {
363 if (Kind.isMergeableConst4() && MergeableConst4Section)
364 return MergeableConst4Section;
365 if (Kind.isMergeableConst8() && MergeableConst8Section)
366 return MergeableConst8Section;
367 if (Kind.isMergeableConst16() && MergeableConst16Section)
368 return MergeableConst16Section;
369 if (Kind.isReadOnly())
370 return ReadOnlySection;
371
372 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
373 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
374 return DataRelROSection;
375}
376
377const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000378getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
379 MachineModuleInfo *MMI,
380 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000381
382 if (Encoding & dwarf::DW_EH_PE_indirect) {
383 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
384
385 SmallString<128> Name;
386 Mang->getNameWithPrefix(Name, GV, true);
387 Name += ".DW.stub";
388
389 // Add information about the stub reference to ELFMMI so that the stub
390 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000391 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000392 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000393 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000394 MCSymbol *Sym = Mang->getSymbol(GV);
395 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000396 }
397
398 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000399 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000400 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000401 }
402
403 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000404 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000405}
406
407//===----------------------------------------------------------------------===//
408// MachO
409//===----------------------------------------------------------------------===//
410
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000411void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
412 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000413 // _foo.eh symbols are currently always exported so that the linker knows
414 // about them. This is not necessary on 10.6 and later, but it
415 // doesn't hurt anything.
416 // FIXME: I need to get this from Triple.
417 IsFunctionEHSymbolGlobal = true;
418 IsFunctionEHFrameSymbolPrivate = false;
419 SupportsWeakOmittedEHFrame = false;
420
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000421 TargetLoweringObjectFile::Initialize(Ctx, TM);
422
423 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000424 = getContext().getMachOSection("__TEXT", "__text",
425 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
426 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000427 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000428 = getContext().getMachOSection("__DATA", "__data", 0,
429 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000430
431 CStringSection // .cstring
Chris Lattner22772212010-04-08 20:40:11 +0000432 = getContext().getMachOSection("__TEXT", "__cstring",
433 MCSectionMachO::S_CSTRING_LITERALS,
434 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000435 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000436 = getContext().getMachOSection("__TEXT","__ustring", 0,
437 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000438 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000439 = getContext().getMachOSection("__TEXT", "__literal4",
440 MCSectionMachO::S_4BYTE_LITERALS,
441 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000442 EightByteConstantSection // .literal8
Chris Lattner22772212010-04-08 20:40:11 +0000443 = getContext().getMachOSection("__TEXT", "__literal8",
444 MCSectionMachO::S_8BYTE_LITERALS,
445 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000446
447 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
448 // to using it in -static mode.
449 SixteenByteConstantSection = 0;
450 if (TM.getRelocationModel() != Reloc::Static &&
451 TM.getTargetData()->getPointerSize() == 32)
452 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000453 getContext().getMachOSection("__TEXT", "__literal16",
454 MCSectionMachO::S_16BYTE_LITERALS,
455 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000456
457 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000458 = getContext().getMachOSection("__TEXT", "__const", 0,
459 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000460
461 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000462 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
463 MCSectionMachO::S_COALESCED |
464 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
465 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000466 ConstTextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000467 = getContext().getMachOSection("__TEXT", "__const_coal",
468 MCSectionMachO::S_COALESCED,
469 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000470 ConstDataCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000471 = getContext().getMachOSection("__DATA","__const_coal",
472 MCSectionMachO::S_COALESCED,
473 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000474 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000475 = getContext().getMachOSection("__DATA", "__const", 0,
476 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000477 DataCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000478 = getContext().getMachOSection("__DATA","__datacoal_nt",
479 MCSectionMachO::S_COALESCED,
480 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000481 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000482 = getContext().getMachOSection("__DATA","__common",
483 MCSectionMachO::S_ZEROFILL,
484 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000485 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000486 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
487 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000488
489
490 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000491 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
492 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
493 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000494 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000495 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
496 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
497 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000498
499 if (TM.getRelocationModel() == Reloc::Static) {
500 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000501 = getContext().getMachOSection("__TEXT", "__constructor", 0,
502 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000503 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000504 = getContext().getMachOSection("__TEXT", "__destructor", 0,
505 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000506 } else {
507 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000508 = getContext().getMachOSection("__DATA", "__mod_init_func",
509 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
510 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000511 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000512 = getContext().getMachOSection("__DATA", "__mod_term_func",
513 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
514 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000515 }
516
517 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000518 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
519 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000520 EHFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000521 getContext().getMachOSection("__TEXT", "__eh_frame",
522 MCSectionMachO::S_COALESCED |
523 MCSectionMachO::S_ATTR_NO_TOC |
524 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
525 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
526 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000527
528 // Debug Information.
529 DwarfAbbrevSection =
Chris Lattner22772212010-04-08 20:40:11 +0000530 getContext().getMachOSection("__DWARF", "__debug_abbrev",
531 MCSectionMachO::S_ATTR_DEBUG,
532 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000533 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000534 getContext().getMachOSection("__DWARF", "__debug_info",
535 MCSectionMachO::S_ATTR_DEBUG,
536 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000537 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000538 getContext().getMachOSection("__DWARF", "__debug_line",
539 MCSectionMachO::S_ATTR_DEBUG,
540 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000541 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000542 getContext().getMachOSection("__DWARF", "__debug_frame",
543 MCSectionMachO::S_ATTR_DEBUG,
544 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000545 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000546 getContext().getMachOSection("__DWARF", "__debug_pubnames",
547 MCSectionMachO::S_ATTR_DEBUG,
548 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000549 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000550 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
551 MCSectionMachO::S_ATTR_DEBUG,
552 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000553 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000554 getContext().getMachOSection("__DWARF", "__debug_str",
555 MCSectionMachO::S_ATTR_DEBUG,
556 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000557 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000558 getContext().getMachOSection("__DWARF", "__debug_loc",
559 MCSectionMachO::S_ATTR_DEBUG,
560 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000561 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000562 getContext().getMachOSection("__DWARF", "__debug_aranges",
563 MCSectionMachO::S_ATTR_DEBUG,
564 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000565 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000566 getContext().getMachOSection("__DWARF", "__debug_ranges",
567 MCSectionMachO::S_ATTR_DEBUG,
568 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000569 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000570 getContext().getMachOSection("__DWARF", "__debug_macinfo",
571 MCSectionMachO::S_ATTR_DEBUG,
572 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000573 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000574 getContext().getMachOSection("__DWARF", "__debug_inlined",
575 MCSectionMachO::S_ATTR_DEBUG,
576 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000577}
578
579const MCSection *TargetLoweringObjectFileMachO::
580getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
581 Mangler *Mang, const TargetMachine &TM) const {
582 // Parse the section specifier and create it if valid.
583 StringRef Segment, Section;
584 unsigned TAA, StubSize;
585 std::string ErrorCode =
586 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
587 TAA, StubSize);
588 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000589 // If invalid, report the error with report_fatal_error.
590 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000591 "' has an invalid section specifier '" + GV->getSection()+
592 "': " + ErrorCode + ".");
593 // Fall back to dropping it into the data section.
594 return DataSection;
595 }
596
597 // Get the section.
598 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000599 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000600
601 // Okay, now that we got the section, verify that the TAA & StubSize agree.
602 // If the user declared multiple globals with different section flags, we need
603 // to reject it here.
604 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000605 // If invalid, report the error with report_fatal_error.
606 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000607 "' section type or attributes does not match previous"
608 " section specifier");
609 }
610
611 return S;
612}
613
614const MCSection *TargetLoweringObjectFileMachO::
615SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
616 Mangler *Mang, const TargetMachine &TM) const {
617 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
618
619 if (Kind.isText())
620 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
621
622 // If this is weak/linkonce, put this in a coalescable section, either in text
623 // or data depending on if it is writable.
624 if (GV->isWeakForLinker()) {
625 if (Kind.isReadOnly())
626 return ConstTextCoalSection;
627 return DataCoalSection;
628 }
629
630 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000631 if (Kind.isMergeable1ByteCString() &&
632 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
633 return CStringSection;
634
635 // Do not put 16-bit arrays in the UString section if they have an
636 // externally visible label, this runs into issues with certain linker
637 // versions.
638 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
639 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
640 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000641
642 if (Kind.isMergeableConst()) {
643 if (Kind.isMergeableConst4())
644 return FourByteConstantSection;
645 if (Kind.isMergeableConst8())
646 return EightByteConstantSection;
647 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
648 return SixteenByteConstantSection;
649 }
650
651 // Otherwise, if it is readonly, but not something we can specially optimize,
652 // just drop it in .const.
653 if (Kind.isReadOnly())
654 return ReadOnlySection;
655
656 // If this is marked const, put it into a const section. But if the dynamic
657 // linker needs to write to it, put it in the data segment.
658 if (Kind.isReadOnlyWithRel())
659 return ConstDataSection;
660
661 // Put zero initialized globals with strong external linkage in the
662 // DATA, __common section with the .zerofill directive.
663 if (Kind.isBSSExtern())
664 return DataCommonSection;
665
666 // Put zero initialized globals with local linkage in __DATA,__bss directive
667 // with the .zerofill directive (aka .lcomm).
668 if (Kind.isBSSLocal())
669 return DataBSSSection;
670
671 // Otherwise, just drop the variable in the normal data section.
672 return DataSection;
673}
674
675const MCSection *
676TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
677 // If this constant requires a relocation, we have to put it in the data
678 // segment, not in the text segment.
679 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
680 return ConstDataSection;
681
682 if (Kind.isMergeableConst4())
683 return FourByteConstantSection;
684 if (Kind.isMergeableConst8())
685 return EightByteConstantSection;
686 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
687 return SixteenByteConstantSection;
688 return ReadOnlySection; // .const
689}
690
691/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
692/// not to emit the UsedDirective for some symbols in llvm.used.
693// FIXME: REMOVE this (rdar://7071300)
694bool TargetLoweringObjectFileMachO::
695shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
696 /// On Darwin, internally linked data beginning with "L" or "l" does not have
697 /// the directive emitted (this occurs in ObjC metadata).
698 if (!GV) return false;
699
700 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
701 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
702 // FIXME: ObjC metadata is currently emitted as internal symbols that have
703 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
704 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000705 MCSymbol *Sym = Mang->getSymbol(GV);
706 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000707 return false;
708 }
709
710 return true;
711}
712
713const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000714getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
715 MachineModuleInfo *MMI, unsigned Encoding,
716 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000717 // The mach-o version of this method defaults to returning a stub reference.
718
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000719 if (Encoding & DW_EH_PE_indirect) {
720 MachineModuleInfoMachO &MachOMMI =
721 MMI->getObjFileInfo<MachineModuleInfoMachO>();
722
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000723 SmallString<128> Name;
724 Mang->getNameWithPrefix(Name, GV, true);
725 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000726
727 // Add information about the stub reference to MachOMMI so that the stub
728 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000729 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000730 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000731 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000732 MCSymbol *Sym = Mang->getSymbol(GV);
733 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000734 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000735
736 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000737 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000738 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000739 }
740
741 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000742 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000743}
744
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000745unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
746 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
747}
748
749unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
750 return DW_EH_PE_pcrel;
751}
752
753unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
754 return DW_EH_PE_pcrel;
755}
756
757unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000758 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000759}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000760
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000761//===----------------------------------------------------------------------===//
762// COFF
763//===----------------------------------------------------------------------===//
764
765typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
766
767TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
768 delete (COFFUniqueMapTy*)UniquingMap;
769}
770
771
772const MCSection *TargetLoweringObjectFileCOFF::
773getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
774 // Create the map if it doesn't already exist.
775 if (UniquingMap == 0)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000776 UniquingMap = new COFFUniqueMapTy();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000777 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
778
779 // Do the lookup, if we have a hit, return it.
780 const MCSectionCOFF *&Entry = Map[Name];
781 if (Entry) return Entry;
782
783 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
784}
785
786void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
787 const TargetMachine &TM) {
788 if (UniquingMap != 0)
789 ((COFFUniqueMapTy*)UniquingMap)->clear();
790 TargetLoweringObjectFile::Initialize(Ctx, TM);
791 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
792 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
793 StaticCtorSection =
794 getCOFFSection(".ctors", false, SectionKind::getDataRel());
795 StaticDtorSection =
796 getCOFFSection(".dtors", false, SectionKind::getDataRel());
797
798 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
799 // though it contains relocatable pointers. In PIC mode, this is probably a
800 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
801 // adjusted or this should be a data section.
802 LSDASection =
803 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
804 EHFrameSection =
805 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
806
807 // Debug info.
808 // FIXME: Don't use 'directive' mode here.
809 DwarfAbbrevSection =
810 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
811 true, SectionKind::getMetadata());
812 DwarfInfoSection =
813 getCOFFSection("\t.section\t.debug_info,\"dr\"",
814 true, SectionKind::getMetadata());
815 DwarfLineSection =
816 getCOFFSection("\t.section\t.debug_line,\"dr\"",
817 true, SectionKind::getMetadata());
818 DwarfFrameSection =
819 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
820 true, SectionKind::getMetadata());
821 DwarfPubNamesSection =
822 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
823 true, SectionKind::getMetadata());
824 DwarfPubTypesSection =
825 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
826 true, SectionKind::getMetadata());
827 DwarfStrSection =
828 getCOFFSection("\t.section\t.debug_str,\"dr\"",
829 true, SectionKind::getMetadata());
830 DwarfLocSection =
831 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
832 true, SectionKind::getMetadata());
833 DwarfARangesSection =
834 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
835 true, SectionKind::getMetadata());
836 DwarfRangesSection =
837 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
838 true, SectionKind::getMetadata());
839 DwarfMacroInfoSection =
840 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
841 true, SectionKind::getMetadata());
842}
843
844const MCSection *TargetLoweringObjectFileCOFF::
845getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
846 Mangler *Mang, const TargetMachine &TM) const {
847 return getCOFFSection(GV->getSection(), false, Kind);
848}
849
850static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
851 if (Kind.isText())
852 return ".text$linkonce";
853 if (Kind.isWriteable())
854 return ".data$linkonce";
855 return ".rdata$linkonce";
856}
857
858
859const MCSection *TargetLoweringObjectFileCOFF::
860SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
861 Mangler *Mang, const TargetMachine &TM) const {
862 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
863
864 // If this global is linkonce/weak and the target handles this by emitting it
865 // into a 'uniqued' section name, create and return the section now.
866 if (GV->isWeakForLinker()) {
867 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
868 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000869 MCSymbol *Sym = Mang->getSymbol(GV);
870 Name.append(Sym->getName().begin(), Sym->getName().end());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000871 return getCOFFSection(Name.str(), false, Kind);
872 }
873
874 if (Kind.isText())
875 return getTextSection();
876
877 return getDataSection();
878}
879