1 //===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements classes used to handle lowerings specific to common
11 // object file formats.
13 //===----------------------------------------------------------------------===//
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/MCSectionCOFF.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/Mangler.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/Support/Dwarf.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/ADT/SmallString.h"
35 #include "llvm/ADT/StringExtras.h"
37 using namespace dwarf;
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
43 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
44 const TargetMachine &TM) {
45 TargetLoweringObjectFile::Initialize(Ctx, TM);
48 getContext().getELFSection(".bss", MCSectionELF::SHT_NOBITS,
49 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
50 SectionKind::getBSS());
53 getContext().getELFSection(".text", MCSectionELF::SHT_PROGBITS,
54 MCSectionELF::SHF_EXECINSTR |
55 MCSectionELF::SHF_ALLOC,
56 SectionKind::getText());
59 getContext().getELFSection(".data", MCSectionELF::SHT_PROGBITS,
60 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
61 SectionKind::getDataRel());
64 getContext().getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
65 MCSectionELF::SHF_ALLOC,
66 SectionKind::getReadOnly());
69 getContext().getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
70 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
71 MCSectionELF::SHF_WRITE,
72 SectionKind::getThreadData());
75 getContext().getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
76 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
77 MCSectionELF::SHF_WRITE,
78 SectionKind::getThreadBSS());
81 getContext().getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
82 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
83 SectionKind::getDataRel());
86 getContext().getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
87 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
88 SectionKind::getDataRelLocal());
91 getContext().getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
92 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
93 SectionKind::getReadOnlyWithRel());
95 DataRelROLocalSection =
96 getContext().getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
97 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
98 SectionKind::getReadOnlyWithRelLocal());
100 MergeableConst4Section =
101 getContext().getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
102 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
103 SectionKind::getMergeableConst4());
105 MergeableConst8Section =
106 getContext().getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
107 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
108 SectionKind::getMergeableConst8());
110 MergeableConst16Section =
111 getContext().getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
112 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
113 SectionKind::getMergeableConst16());
116 getContext().getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
117 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
118 SectionKind::getDataRel());
121 getContext().getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
122 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
123 SectionKind::getDataRel());
125 // Exception Handling Sections.
127 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
128 // it contains relocatable pointers. In PIC mode, this is probably a big
129 // runtime hit for C++ apps. Either the contents of the LSDA need to be
130 // adjusted or this should be a data section.
132 getContext().getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
133 MCSectionELF::SHF_ALLOC,
134 SectionKind::getReadOnly());
136 getContext().getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
137 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
138 SectionKind::getDataRel());
140 // Debug Info Sections.
142 getContext().getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
143 SectionKind::getMetadata());
145 getContext().getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
146 SectionKind::getMetadata());
148 getContext().getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
149 SectionKind::getMetadata());
151 getContext().getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
152 SectionKind::getMetadata());
153 DwarfPubNamesSection =
154 getContext().getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
155 SectionKind::getMetadata());
156 DwarfPubTypesSection =
157 getContext().getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
158 SectionKind::getMetadata());
160 getContext().getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
161 SectionKind::getMetadata());
163 getContext().getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
164 SectionKind::getMetadata());
165 DwarfARangesSection =
166 getContext().getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
167 SectionKind::getMetadata());
169 getContext().getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
170 SectionKind::getMetadata());
171 DwarfMacroInfoSection =
172 getContext().getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
173 SectionKind::getMetadata());
178 getELFKindForNamedSection(StringRef Name, SectionKind K) {
179 if (Name.empty() || Name[0] != '.') return K;
181 // Some lame default implementation based on some magic section names.
182 if (Name == ".bss" ||
183 Name.startswith(".bss.") ||
184 Name.startswith(".gnu.linkonce.b.") ||
185 Name.startswith(".llvm.linkonce.b.") ||
187 Name.startswith(".sbss.") ||
188 Name.startswith(".gnu.linkonce.sb.") ||
189 Name.startswith(".llvm.linkonce.sb."))
190 return SectionKind::getBSS();
192 if (Name == ".tdata" ||
193 Name.startswith(".tdata.") ||
194 Name.startswith(".gnu.linkonce.td.") ||
195 Name.startswith(".llvm.linkonce.td."))
196 return SectionKind::getThreadData();
198 if (Name == ".tbss" ||
199 Name.startswith(".tbss.") ||
200 Name.startswith(".gnu.linkonce.tb.") ||
201 Name.startswith(".llvm.linkonce.tb."))
202 return SectionKind::getThreadBSS();
208 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
210 if (Name == ".init_array")
211 return MCSectionELF::SHT_INIT_ARRAY;
213 if (Name == ".fini_array")
214 return MCSectionELF::SHT_FINI_ARRAY;
216 if (Name == ".preinit_array")
217 return MCSectionELF::SHT_PREINIT_ARRAY;
219 if (K.isBSS() || K.isThreadBSS())
220 return MCSectionELF::SHT_NOBITS;
222 return MCSectionELF::SHT_PROGBITS;
227 getELFSectionFlags(SectionKind K) {
231 Flags |= MCSectionELF::SHF_ALLOC;
234 Flags |= MCSectionELF::SHF_EXECINSTR;
237 Flags |= MCSectionELF::SHF_WRITE;
239 if (K.isThreadLocal())
240 Flags |= MCSectionELF::SHF_TLS;
242 // K.isMergeableConst() is left out to honour PR4650
243 if (K.isMergeableCString() || K.isMergeableConst4() ||
244 K.isMergeableConst8() || K.isMergeableConst16())
245 Flags |= MCSectionELF::SHF_MERGE;
247 if (K.isMergeableCString())
248 Flags |= MCSectionELF::SHF_STRINGS;
254 const MCSection *TargetLoweringObjectFileELF::
255 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
256 Mangler *Mang, const TargetMachine &TM) const {
257 StringRef SectionName = GV->getSection();
259 // Infer section flags from the section name if we can.
260 Kind = getELFKindForNamedSection(SectionName, Kind);
262 return getContext().getELFSection(SectionName,
263 getELFSectionType(SectionName, Kind),
264 getELFSectionFlags(Kind), Kind, true);
267 static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
268 if (Kind.isText()) return ".gnu.linkonce.t.";
269 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
271 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
272 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
274 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
275 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
276 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
277 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
279 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
280 return ".gnu.linkonce.d.rel.ro.";
283 /// getSectionPrefixForGlobal - Return the section prefix name used by options
284 /// FunctionsSections and DataSections.
285 static const char *getSectionPrefixForGlobal(SectionKind Kind) {
286 if (Kind.isText()) return ".text.";
287 if (Kind.isReadOnly()) return ".rodata.";
289 if (Kind.isThreadData()) return ".tdata.";
290 if (Kind.isThreadBSS()) return ".tbss.";
292 if (Kind.isDataNoRel()) return ".data.";
293 if (Kind.isDataRelLocal()) return ".data.rel.local.";
294 if (Kind.isDataRel()) return ".data.rel.";
295 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
297 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
298 return ".data.rel.ro.";
302 const MCSection *TargetLoweringObjectFileELF::
303 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
304 Mangler *Mang, const TargetMachine &TM) const {
305 // If we have -ffunction-section or -fdata-section then we should emit the
306 // global value to a uniqued section specifically for it.
307 bool EmitUniquedSection;
309 EmitUniquedSection = TM.getFunctionSections();
311 EmitUniquedSection = TM.getDataSections();
313 // If this global is linkonce/weak and the target handles this by emitting it
314 // into a 'uniqued' section name, create and return the section now.
315 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
316 !Kind.isCommon() && !Kind.isBSS()) {
318 if (GV->isWeakForLinker())
319 Prefix = getSectionPrefixForUniqueGlobal(Kind);
321 assert(EmitUniquedSection);
322 Prefix = getSectionPrefixForGlobal(Kind);
325 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
326 MCSymbol *Sym = Mang->getSymbol(GV);
327 Name.append(Sym->getName().begin(), Sym->getName().end());
328 return getContext().getELFSection(Name.str(),
329 getELFSectionType(Name.str(), Kind),
330 getELFSectionFlags(Kind), Kind);
333 if (Kind.isText()) return TextSection;
335 if (Kind.isMergeable1ByteCString() ||
336 Kind.isMergeable2ByteCString() ||
337 Kind.isMergeable4ByteCString()) {
339 // We also need alignment here.
340 // FIXME: this is getting the alignment of the character, not the
341 // alignment of the global!
343 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
345 const char *SizeSpec = ".rodata.str1.";
346 if (Kind.isMergeable2ByteCString())
347 SizeSpec = ".rodata.str2.";
348 else if (Kind.isMergeable4ByteCString())
349 SizeSpec = ".rodata.str4.";
351 assert(Kind.isMergeable1ByteCString() && "unknown string width");
354 std::string Name = SizeSpec + utostr(Align);
355 return getContext().getELFSection(Name, MCSectionELF::SHT_PROGBITS,
356 MCSectionELF::SHF_ALLOC |
357 MCSectionELF::SHF_MERGE |
358 MCSectionELF::SHF_STRINGS,
362 if (Kind.isMergeableConst()) {
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 return ReadOnlySection; // .const
372 if (Kind.isReadOnly()) return ReadOnlySection;
374 if (Kind.isThreadData()) return TLSDataSection;
375 if (Kind.isThreadBSS()) return TLSBSSSection;
377 // Note: we claim that common symbols are put in BSSSection, but they are
378 // really emitted with the magic .comm directive, which creates a symbol table
379 // entry but not a section.
380 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
382 if (Kind.isDataNoRel()) return DataSection;
383 if (Kind.isDataRelLocal()) return DataRelLocalSection;
384 if (Kind.isDataRel()) return DataRelSection;
385 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
387 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
388 return DataRelROSection;
391 /// getSectionForConstant - Given a mergeable constant with the
392 /// specified size and relocation information, return a section that it
393 /// should be placed in.
394 const MCSection *TargetLoweringObjectFileELF::
395 getSectionForConstant(SectionKind Kind) const {
396 if (Kind.isMergeableConst4() && MergeableConst4Section)
397 return MergeableConst4Section;
398 if (Kind.isMergeableConst8() && MergeableConst8Section)
399 return MergeableConst8Section;
400 if (Kind.isMergeableConst16() && MergeableConst16Section)
401 return MergeableConst16Section;
402 if (Kind.isReadOnly())
403 return ReadOnlySection;
405 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
406 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
407 return DataRelROSection;
410 const MCExpr *TargetLoweringObjectFileELF::
411 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
412 MachineModuleInfo *MMI,
413 unsigned Encoding, MCStreamer &Streamer) const {
415 if (Encoding & dwarf::DW_EH_PE_indirect) {
416 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
418 SmallString<128> Name;
419 Mang->getNameWithPrefix(Name, GV, true);
422 // Add information about the stub reference to ELFMMI so that the stub
423 // gets emitted by the asmprinter.
424 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
425 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
426 if (StubSym.getPointer() == 0) {
427 MCSymbol *Sym = Mang->getSymbol(GV);
428 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
431 return TargetLoweringObjectFile::
432 getExprForDwarfReference(SSym, Mang, MMI,
433 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
436 return TargetLoweringObjectFile::
437 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
440 //===----------------------------------------------------------------------===//
442 //===----------------------------------------------------------------------===//
444 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
445 const TargetMachine &TM) {
446 // _foo.eh symbols are currently always exported so that the linker knows
447 // about them. This is not necessary on 10.6 and later, but it
448 // doesn't hurt anything.
449 // FIXME: I need to get this from Triple.
450 IsFunctionEHSymbolGlobal = true;
451 IsFunctionEHFrameSymbolPrivate = false;
452 SupportsWeakOmittedEHFrame = false;
454 TargetLoweringObjectFile::Initialize(Ctx, TM);
457 = getContext().getMachOSection("__TEXT", "__text",
458 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
459 SectionKind::getText());
461 = getContext().getMachOSection("__DATA", "__data", 0,
462 SectionKind::getDataRel());
464 CStringSection // .cstring
465 = getContext().getMachOSection("__TEXT", "__cstring",
466 MCSectionMachO::S_CSTRING_LITERALS,
467 SectionKind::getMergeable1ByteCString());
469 = getContext().getMachOSection("__TEXT","__ustring", 0,
470 SectionKind::getMergeable2ByteCString());
471 FourByteConstantSection // .literal4
472 = getContext().getMachOSection("__TEXT", "__literal4",
473 MCSectionMachO::S_4BYTE_LITERALS,
474 SectionKind::getMergeableConst4());
475 EightByteConstantSection // .literal8
476 = getContext().getMachOSection("__TEXT", "__literal8",
477 MCSectionMachO::S_8BYTE_LITERALS,
478 SectionKind::getMergeableConst8());
480 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
481 // to using it in -static mode.
482 SixteenByteConstantSection = 0;
483 if (TM.getRelocationModel() != Reloc::Static &&
484 TM.getTargetData()->getPointerSize() == 32)
485 SixteenByteConstantSection = // .literal16
486 getContext().getMachOSection("__TEXT", "__literal16",
487 MCSectionMachO::S_16BYTE_LITERALS,
488 SectionKind::getMergeableConst16());
490 ReadOnlySection // .const
491 = getContext().getMachOSection("__TEXT", "__const", 0,
492 SectionKind::getReadOnly());
495 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
496 MCSectionMachO::S_COALESCED |
497 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
498 SectionKind::getText());
500 = getContext().getMachOSection("__TEXT", "__const_coal",
501 MCSectionMachO::S_COALESCED,
502 SectionKind::getText());
504 = getContext().getMachOSection("__DATA","__const_coal",
505 MCSectionMachO::S_COALESCED,
506 SectionKind::getText());
507 ConstDataSection // .const_data
508 = getContext().getMachOSection("__DATA", "__const", 0,
509 SectionKind::getReadOnlyWithRel());
511 = getContext().getMachOSection("__DATA","__datacoal_nt",
512 MCSectionMachO::S_COALESCED,
513 SectionKind::getDataRel());
515 = getContext().getMachOSection("__DATA","__common",
516 MCSectionMachO::S_ZEROFILL,
517 SectionKind::getBSS());
519 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
520 SectionKind::getBSS());
523 LazySymbolPointerSection
524 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
525 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
526 SectionKind::getMetadata());
527 NonLazySymbolPointerSection
528 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
529 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
530 SectionKind::getMetadata());
532 if (TM.getRelocationModel() == Reloc::Static) {
534 = getContext().getMachOSection("__TEXT", "__constructor", 0,
535 SectionKind::getDataRel());
537 = getContext().getMachOSection("__TEXT", "__destructor", 0,
538 SectionKind::getDataRel());
541 = getContext().getMachOSection("__DATA", "__mod_init_func",
542 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
543 SectionKind::getDataRel());
545 = getContext().getMachOSection("__DATA", "__mod_term_func",
546 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
547 SectionKind::getDataRel());
550 // Exception Handling.
551 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
552 SectionKind::getReadOnlyWithRel());
554 getContext().getMachOSection("__TEXT", "__eh_frame",
555 MCSectionMachO::S_COALESCED |
556 MCSectionMachO::S_ATTR_NO_TOC |
557 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
558 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
559 SectionKind::getReadOnly());
561 // Debug Information.
563 getContext().getMachOSection("__DWARF", "__debug_abbrev",
564 MCSectionMachO::S_ATTR_DEBUG,
565 SectionKind::getMetadata());
567 getContext().getMachOSection("__DWARF", "__debug_info",
568 MCSectionMachO::S_ATTR_DEBUG,
569 SectionKind::getMetadata());
571 getContext().getMachOSection("__DWARF", "__debug_line",
572 MCSectionMachO::S_ATTR_DEBUG,
573 SectionKind::getMetadata());
575 getContext().getMachOSection("__DWARF", "__debug_frame",
576 MCSectionMachO::S_ATTR_DEBUG,
577 SectionKind::getMetadata());
578 DwarfPubNamesSection =
579 getContext().getMachOSection("__DWARF", "__debug_pubnames",
580 MCSectionMachO::S_ATTR_DEBUG,
581 SectionKind::getMetadata());
582 DwarfPubTypesSection =
583 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
584 MCSectionMachO::S_ATTR_DEBUG,
585 SectionKind::getMetadata());
587 getContext().getMachOSection("__DWARF", "__debug_str",
588 MCSectionMachO::S_ATTR_DEBUG,
589 SectionKind::getMetadata());
591 getContext().getMachOSection("__DWARF", "__debug_loc",
592 MCSectionMachO::S_ATTR_DEBUG,
593 SectionKind::getMetadata());
594 DwarfARangesSection =
595 getContext().getMachOSection("__DWARF", "__debug_aranges",
596 MCSectionMachO::S_ATTR_DEBUG,
597 SectionKind::getMetadata());
599 getContext().getMachOSection("__DWARF", "__debug_ranges",
600 MCSectionMachO::S_ATTR_DEBUG,
601 SectionKind::getMetadata());
602 DwarfMacroInfoSection =
603 getContext().getMachOSection("__DWARF", "__debug_macinfo",
604 MCSectionMachO::S_ATTR_DEBUG,
605 SectionKind::getMetadata());
606 DwarfDebugInlineSection =
607 getContext().getMachOSection("__DWARF", "__debug_inlined",
608 MCSectionMachO::S_ATTR_DEBUG,
609 SectionKind::getMetadata());
612 const MCSection *TargetLoweringObjectFileMachO::
613 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
614 Mangler *Mang, const TargetMachine &TM) const {
615 // Parse the section specifier and create it if valid.
616 StringRef Segment, Section;
617 unsigned TAA, StubSize;
618 std::string ErrorCode =
619 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
621 if (!ErrorCode.empty()) {
622 // If invalid, report the error with report_fatal_error.
623 report_fatal_error("Global variable '" + GV->getNameStr() +
624 "' has an invalid section specifier '" + GV->getSection()+
625 "': " + ErrorCode + ".");
626 // Fall back to dropping it into the data section.
631 const MCSectionMachO *S =
632 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
634 // Okay, now that we got the section, verify that the TAA & StubSize agree.
635 // If the user declared multiple globals with different section flags, we need
636 // to reject it here.
637 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
638 // If invalid, report the error with report_fatal_error.
639 report_fatal_error("Global variable '" + GV->getNameStr() +
640 "' section type or attributes does not match previous"
641 " section specifier");
647 const MCSection *TargetLoweringObjectFileMachO::
648 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
649 Mangler *Mang, const TargetMachine &TM) const {
650 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
653 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
655 // If this is weak/linkonce, put this in a coalescable section, either in text
656 // or data depending on if it is writable.
657 if (GV->isWeakForLinker()) {
658 if (Kind.isReadOnly())
659 return ConstTextCoalSection;
660 return DataCoalSection;
663 // FIXME: Alignment check should be handled by section classifier.
664 if (Kind.isMergeable1ByteCString() &&
665 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
666 return CStringSection;
668 // Do not put 16-bit arrays in the UString section if they have an
669 // externally visible label, this runs into issues with certain linker
671 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
672 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
673 return UStringSection;
675 if (Kind.isMergeableConst()) {
676 if (Kind.isMergeableConst4())
677 return FourByteConstantSection;
678 if (Kind.isMergeableConst8())
679 return EightByteConstantSection;
680 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
681 return SixteenByteConstantSection;
684 // Otherwise, if it is readonly, but not something we can specially optimize,
685 // just drop it in .const.
686 if (Kind.isReadOnly())
687 return ReadOnlySection;
689 // If this is marked const, put it into a const section. But if the dynamic
690 // linker needs to write to it, put it in the data segment.
691 if (Kind.isReadOnlyWithRel())
692 return ConstDataSection;
694 // Put zero initialized globals with strong external linkage in the
695 // DATA, __common section with the .zerofill directive.
696 if (Kind.isBSSExtern())
697 return DataCommonSection;
699 // Put zero initialized globals with local linkage in __DATA,__bss directive
700 // with the .zerofill directive (aka .lcomm).
701 if (Kind.isBSSLocal())
702 return DataBSSSection;
704 // Otherwise, just drop the variable in the normal data section.
709 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
710 // If this constant requires a relocation, we have to put it in the data
711 // segment, not in the text segment.
712 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
713 return ConstDataSection;
715 if (Kind.isMergeableConst4())
716 return FourByteConstantSection;
717 if (Kind.isMergeableConst8())
718 return EightByteConstantSection;
719 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
720 return SixteenByteConstantSection;
721 return ReadOnlySection; // .const
724 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
725 /// not to emit the UsedDirective for some symbols in llvm.used.
726 // FIXME: REMOVE this (rdar://7071300)
727 bool TargetLoweringObjectFileMachO::
728 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
729 /// On Darwin, internally linked data beginning with "L" or "l" does not have
730 /// the directive emitted (this occurs in ObjC metadata).
731 if (!GV) return false;
733 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
734 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
735 // FIXME: ObjC metadata is currently emitted as internal symbols that have
736 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
737 // this horrible hack can go away.
738 MCSymbol *Sym = Mang->getSymbol(GV);
739 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
746 const MCExpr *TargetLoweringObjectFileMachO::
747 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
748 MachineModuleInfo *MMI, unsigned Encoding,
749 MCStreamer &Streamer) const {
750 // The mach-o version of this method defaults to returning a stub reference.
752 if (Encoding & DW_EH_PE_indirect) {
753 MachineModuleInfoMachO &MachOMMI =
754 MMI->getObjFileInfo<MachineModuleInfoMachO>();
756 SmallString<128> Name;
757 Mang->getNameWithPrefix(Name, GV, true);
758 Name += "$non_lazy_ptr";
760 // Add information about the stub reference to MachOMMI so that the stub
761 // gets emitted by the asmprinter.
762 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
763 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
764 if (StubSym.getPointer() == 0) {
765 MCSymbol *Sym = Mang->getSymbol(GV);
766 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
769 return TargetLoweringObjectFile::
770 getExprForDwarfReference(SSym, Mang, MMI,
771 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
774 return TargetLoweringObjectFile::
775 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
778 unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
779 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
782 unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
783 return DW_EH_PE_pcrel;
786 unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
787 return DW_EH_PE_pcrel;
790 unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
791 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
794 //===----------------------------------------------------------------------===//
796 //===----------------------------------------------------------------------===//
798 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
799 const TargetMachine &TM) {
800 TargetLoweringObjectFile::Initialize(Ctx, TM);
802 getContext().getCOFFSection(".text",
803 MCSectionCOFF::IMAGE_SCN_CNT_CODE |
804 MCSectionCOFF::IMAGE_SCN_MEM_EXECUTE |
805 MCSectionCOFF::IMAGE_SCN_MEM_READ,
806 SectionKind::getText());
808 getContext().getCOFFSection(".data",
809 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
810 MCSectionCOFF::IMAGE_SCN_MEM_READ |
811 MCSectionCOFF::IMAGE_SCN_MEM_WRITE,
812 SectionKind::getDataRel());
814 getContext().getCOFFSection(".rdata",
815 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
816 MCSectionCOFF::IMAGE_SCN_MEM_READ,
817 SectionKind::getReadOnly());
819 getContext().getCOFFSection(".ctors",
820 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
821 MCSectionCOFF::IMAGE_SCN_MEM_READ |
822 MCSectionCOFF::IMAGE_SCN_MEM_WRITE,
823 SectionKind::getDataRel());
825 getContext().getCOFFSection(".dtors",
826 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
827 MCSectionCOFF::IMAGE_SCN_MEM_READ |
828 MCSectionCOFF::IMAGE_SCN_MEM_WRITE,
829 SectionKind::getDataRel());
831 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
832 // though it contains relocatable pointers. In PIC mode, this is probably a
833 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
834 // adjusted or this should be a data section.
836 getContext().getCOFFSection(".gcc_except_table",
837 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
838 MCSectionCOFF::IMAGE_SCN_MEM_READ,
839 SectionKind::getReadOnly());
841 getContext().getCOFFSection(".eh_frame",
842 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
843 MCSectionCOFF::IMAGE_SCN_MEM_READ |
844 MCSectionCOFF::IMAGE_SCN_MEM_WRITE,
845 SectionKind::getDataRel());
849 getContext().getCOFFSection(".debug_abbrev",
850 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
851 MCSectionCOFF::IMAGE_SCN_MEM_READ,
852 SectionKind::getMetadata());
854 getContext().getCOFFSection(".debug_info",
855 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
856 MCSectionCOFF::IMAGE_SCN_MEM_READ,
857 SectionKind::getMetadata());
859 getContext().getCOFFSection(".debug_line",
860 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
861 MCSectionCOFF::IMAGE_SCN_MEM_READ,
862 SectionKind::getMetadata());
864 getContext().getCOFFSection(".debug_frame",
865 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
866 MCSectionCOFF::IMAGE_SCN_MEM_READ,
867 SectionKind::getMetadata());
868 DwarfPubNamesSection =
869 getContext().getCOFFSection(".debug_pubnames",
870 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
871 MCSectionCOFF::IMAGE_SCN_MEM_READ,
872 SectionKind::getMetadata());
873 DwarfPubTypesSection =
874 getContext().getCOFFSection(".debug_pubtypes",
875 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
876 MCSectionCOFF::IMAGE_SCN_MEM_READ,
877 SectionKind::getMetadata());
879 getContext().getCOFFSection(".debug_str",
880 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
881 MCSectionCOFF::IMAGE_SCN_MEM_READ,
882 SectionKind::getMetadata());
884 getContext().getCOFFSection(".debug_loc",
885 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
886 MCSectionCOFF::IMAGE_SCN_MEM_READ,
887 SectionKind::getMetadata());
888 DwarfARangesSection =
889 getContext().getCOFFSection(".debug_aranges",
890 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
891 MCSectionCOFF::IMAGE_SCN_MEM_READ,
892 SectionKind::getMetadata());
894 getContext().getCOFFSection(".debug_ranges",
895 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
896 MCSectionCOFF::IMAGE_SCN_MEM_READ,
897 SectionKind::getMetadata());
898 DwarfMacroInfoSection =
899 getContext().getCOFFSection(".debug_macinfo",
900 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE |
901 MCSectionCOFF::IMAGE_SCN_MEM_READ,
902 SectionKind::getMetadata());
905 getContext().getCOFFSection(".drectve",
906 MCSectionCOFF::IMAGE_SCN_LNK_INFO,
907 SectionKind::getMetadata());
911 getCOFFSectionFlags(SectionKind K) {
916 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE;
919 MCSectionCOFF::IMAGE_SCN_MEM_EXECUTE |
920 MCSectionCOFF::IMAGE_SCN_CNT_CODE;
923 MCSectionCOFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
924 MCSectionCOFF::IMAGE_SCN_MEM_READ |
925 MCSectionCOFF::IMAGE_SCN_MEM_WRITE;
926 else if (K.isReadOnly())
928 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
929 MCSectionCOFF::IMAGE_SCN_MEM_READ;
930 else if (K.isWriteable())
932 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
933 MCSectionCOFF::IMAGE_SCN_MEM_READ |
934 MCSectionCOFF::IMAGE_SCN_MEM_WRITE;
939 const MCSection *TargetLoweringObjectFileCOFF::
940 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
941 Mangler *Mang, const TargetMachine &TM) const {
942 return getContext().getCOFFSection(GV->getSection(),
943 getCOFFSectionFlags(Kind),
947 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
949 return ".text$linkonce";
951 return ".bss$linkonce";
952 if (Kind.isWriteable())
953 return ".data$linkonce";
954 return ".rdata$linkonce";
958 const MCSection *TargetLoweringObjectFileCOFF::
959 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
960 Mangler *Mang, const TargetMachine &TM) const {
961 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
963 // If this global is linkonce/weak and the target handles this by emitting it
964 // into a 'uniqued' section name, create and return the section now.
965 if (GV->isWeakForLinker()) {
966 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
967 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
968 MCSymbol *Sym = Mang->getSymbol(GV);
969 Name.append(Sym->getName().begin(), Sym->getName().end());
971 unsigned Characteristics = getCOFFSectionFlags(Kind);
973 Characteristics |= MCSectionCOFF::IMAGE_SCN_LNK_COMDAT;
975 return getContext().getCOFFSection(Name.str(), Characteristics,
976 MCSectionCOFF::IMAGE_COMDAT_SELECT_EXACT_MATCH, Kind);
980 return getTextSection();
982 return getDataSection();