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/MCStreamer.h"
27 #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"
33 #include "llvm/Support/ELF.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/ADT/SmallString.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/ADT/Triple.h"
40 using namespace dwarf;
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
46 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
47 const TargetMachine &TM) {
48 TargetLoweringObjectFile::Initialize(Ctx, TM);
51 getContext().getELFSection(".bss", ELF::SHT_NOBITS,
52 ELF::SHF_WRITE |ELF::SHF_ALLOC,
53 SectionKind::getBSS());
56 getContext().getELFSection(".text", ELF::SHT_PROGBITS,
59 SectionKind::getText());
62 getContext().getELFSection(".data", ELF::SHT_PROGBITS,
63 ELF::SHF_WRITE |ELF::SHF_ALLOC,
64 SectionKind::getDataRel());
67 getContext().getELFSection(".rodata", ELF::SHT_PROGBITS,
69 SectionKind::getReadOnly());
72 getContext().getELFSection(".tdata", ELF::SHT_PROGBITS,
73 ELF::SHF_ALLOC | ELF::SHF_TLS |
75 SectionKind::getThreadData());
78 getContext().getELFSection(".tbss", ELF::SHT_NOBITS,
79 ELF::SHF_ALLOC | ELF::SHF_TLS |
81 SectionKind::getThreadBSS());
84 getContext().getELFSection(".data.rel", ELF::SHT_PROGBITS,
85 ELF::SHF_ALLOC |ELF::SHF_WRITE,
86 SectionKind::getDataRel());
89 getContext().getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
90 ELF::SHF_ALLOC |ELF::SHF_WRITE,
91 SectionKind::getDataRelLocal());
94 getContext().getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
95 ELF::SHF_ALLOC |ELF::SHF_WRITE,
96 SectionKind::getReadOnlyWithRel());
98 DataRelROLocalSection =
99 getContext().getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
100 ELF::SHF_ALLOC |ELF::SHF_WRITE,
101 SectionKind::getReadOnlyWithRelLocal());
103 MergeableConst4Section =
104 getContext().getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
105 ELF::SHF_ALLOC |ELF::SHF_MERGE,
106 SectionKind::getMergeableConst4());
108 MergeableConst8Section =
109 getContext().getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
110 ELF::SHF_ALLOC |ELF::SHF_MERGE,
111 SectionKind::getMergeableConst8());
113 MergeableConst16Section =
114 getContext().getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
115 ELF::SHF_ALLOC |ELF::SHF_MERGE,
116 SectionKind::getMergeableConst16());
119 getContext().getELFSection(".ctors", ELF::SHT_PROGBITS,
120 ELF::SHF_ALLOC |ELF::SHF_WRITE,
121 SectionKind::getDataRel());
124 getContext().getELFSection(".dtors", ELF::SHT_PROGBITS,
125 ELF::SHF_ALLOC |ELF::SHF_WRITE,
126 SectionKind::getDataRel());
128 // Exception Handling Sections.
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.
135 getContext().getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
137 SectionKind::getReadOnly());
138 // Debug Info Sections.
140 getContext().getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
141 SectionKind::getMetadata());
143 getContext().getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
144 SectionKind::getMetadata());
146 getContext().getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
147 SectionKind::getMetadata());
149 getContext().getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
150 SectionKind::getMetadata());
151 DwarfPubNamesSection =
152 getContext().getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
153 SectionKind::getMetadata());
154 DwarfPubTypesSection =
155 getContext().getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
156 SectionKind::getMetadata());
158 getContext().getELFSection(".debug_str", ELF::SHT_PROGBITS, 0,
159 SectionKind::getMetadata());
161 getContext().getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
162 SectionKind::getMetadata());
163 DwarfARangesSection =
164 getContext().getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
165 SectionKind::getMetadata());
167 getContext().getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
168 SectionKind::getMetadata());
169 DwarfMacroInfoSection =
170 getContext().getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
171 SectionKind::getMetadata());
174 const MCSection *TargetLoweringObjectFileELF::getEHFrameSection() const {
175 return getContext().getELFSection(".eh_frame", ELF::SHT_PROGBITS,
177 SectionKind::getDataRel());
181 TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
183 MachineModuleInfo *MMI) const {
184 unsigned Encoding = getPersonalityEncoding();
185 switch (Encoding & 0x70) {
187 report_fatal_error("We do not support this DWARF encoding yet!");
188 case dwarf::DW_EH_PE_absptr:
189 return Mang->getSymbol(GV);
191 case dwarf::DW_EH_PE_pcrel: {
192 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
193 Mang->getSymbol(GV)->getName());
199 void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
200 const TargetMachine &TM,
201 const MCSymbol *Sym) const {
202 SmallString<64> NameData("DW.ref.");
203 NameData += Sym->getName();
204 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
205 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
206 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
207 StringRef Prefix = ".data.";
208 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
209 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
210 const MCSection *Sec = getContext().getELFSection(NameData,
213 SectionKind::getDataRel(),
214 0, Label->getName());
215 Streamer.SwitchSection(Sec);
216 Streamer.EmitValueToAlignment(8);
217 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
218 const MCExpr *E = MCConstantExpr::Create(8, getContext());
219 Streamer.EmitELFSize(Label, E);
220 Streamer.EmitLabel(Label);
222 unsigned Size = TM.getTargetData()->getPointerSize();
223 Streamer.EmitSymbolValue(Sym, Size);
227 getELFKindForNamedSection(StringRef Name, SectionKind K) {
228 // N.B.: The defaults used in here are no the same ones used in MC.
229 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
230 // both gas and MC will produce a section with no flags. Given
231 // section(".eh_frame") gcc will produce
232 // .section .eh_frame,"a",@progbits
233 if (Name.empty() || Name[0] != '.') return K;
235 // Some lame default implementation based on some magic section names.
236 if (Name == ".bss" ||
237 Name.startswith(".bss.") ||
238 Name.startswith(".gnu.linkonce.b.") ||
239 Name.startswith(".llvm.linkonce.b.") ||
241 Name.startswith(".sbss.") ||
242 Name.startswith(".gnu.linkonce.sb.") ||
243 Name.startswith(".llvm.linkonce.sb."))
244 return SectionKind::getBSS();
246 if (Name == ".tdata" ||
247 Name.startswith(".tdata.") ||
248 Name.startswith(".gnu.linkonce.td.") ||
249 Name.startswith(".llvm.linkonce.td."))
250 return SectionKind::getThreadData();
252 if (Name == ".tbss" ||
253 Name.startswith(".tbss.") ||
254 Name.startswith(".gnu.linkonce.tb.") ||
255 Name.startswith(".llvm.linkonce.tb."))
256 return SectionKind::getThreadBSS();
262 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
264 if (Name == ".init_array")
265 return ELF::SHT_INIT_ARRAY;
267 if (Name == ".fini_array")
268 return ELF::SHT_FINI_ARRAY;
270 if (Name == ".preinit_array")
271 return ELF::SHT_PREINIT_ARRAY;
273 if (K.isBSS() || K.isThreadBSS())
274 return ELF::SHT_NOBITS;
276 return ELF::SHT_PROGBITS;
281 getELFSectionFlags(SectionKind K) {
285 Flags |= ELF::SHF_ALLOC;
288 Flags |= ELF::SHF_EXECINSTR;
291 Flags |= ELF::SHF_WRITE;
293 if (K.isThreadLocal())
294 Flags |= ELF::SHF_TLS;
296 // K.isMergeableConst() is left out to honour PR4650
297 if (K.isMergeableCString() || K.isMergeableConst4() ||
298 K.isMergeableConst8() || K.isMergeableConst16())
299 Flags |= ELF::SHF_MERGE;
301 if (K.isMergeableCString())
302 Flags |= ELF::SHF_STRINGS;
308 const MCSection *TargetLoweringObjectFileELF::
309 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
310 Mangler *Mang, const TargetMachine &TM) const {
311 StringRef SectionName = GV->getSection();
313 // Infer section flags from the section name if we can.
314 Kind = getELFKindForNamedSection(SectionName, Kind);
316 return getContext().getELFSection(SectionName,
317 getELFSectionType(SectionName, Kind),
318 getELFSectionFlags(Kind), Kind);
321 /// getSectionPrefixForGlobal - Return the section prefix name used by options
322 /// FunctionsSections and DataSections.
323 static const char *getSectionPrefixForGlobal(SectionKind Kind) {
324 if (Kind.isText()) return ".text.";
325 if (Kind.isReadOnly()) return ".rodata.";
327 if (Kind.isThreadData()) return ".tdata.";
328 if (Kind.isThreadBSS()) return ".tbss.";
330 if (Kind.isDataNoRel()) return ".data.";
331 if (Kind.isDataRelLocal()) return ".data.rel.local.";
332 if (Kind.isDataRel()) return ".data.rel.";
333 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
335 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
336 return ".data.rel.ro.";
340 const MCSection *TargetLoweringObjectFileELF::
341 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
342 Mangler *Mang, const TargetMachine &TM) const {
343 // If we have -ffunction-section or -fdata-section then we should emit the
344 // global value to a uniqued section specifically for it.
345 bool EmitUniquedSection;
347 EmitUniquedSection = TM.getFunctionSections();
349 EmitUniquedSection = TM.getDataSections();
351 // If this global is linkonce/weak and the target handles this by emitting it
352 // into a 'uniqued' section name, create and return the section now.
353 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
354 !Kind.isCommon() && !Kind.isBSS()) {
356 Prefix = getSectionPrefixForGlobal(Kind);
358 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
359 MCSymbol *Sym = Mang->getSymbol(GV);
360 Name.append(Sym->getName().begin(), Sym->getName().end());
361 StringRef Group = "";
362 unsigned Flags = getELFSectionFlags(Kind);
363 if (GV->isWeakForLinker()) {
364 Group = Sym->getName();
365 Flags |= ELF::SHF_GROUP;
368 return getContext().getELFSection(Name.str(),
369 getELFSectionType(Name.str(), Kind),
370 Flags, Kind, 0, Group);
373 if (Kind.isText()) return TextSection;
375 if (Kind.isMergeable1ByteCString() ||
376 Kind.isMergeable2ByteCString() ||
377 Kind.isMergeable4ByteCString()) {
379 // We also need alignment here.
380 // FIXME: this is getting the alignment of the character, not the
381 // alignment of the global!
383 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
385 const char *SizeSpec = ".rodata.str1.";
386 if (Kind.isMergeable2ByteCString())
387 SizeSpec = ".rodata.str2.";
388 else if (Kind.isMergeable4ByteCString())
389 SizeSpec = ".rodata.str4.";
391 assert(Kind.isMergeable1ByteCString() && "unknown string width");
394 std::string Name = SizeSpec + utostr(Align);
395 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
402 if (Kind.isMergeableConst()) {
403 if (Kind.isMergeableConst4() && MergeableConst4Section)
404 return MergeableConst4Section;
405 if (Kind.isMergeableConst8() && MergeableConst8Section)
406 return MergeableConst8Section;
407 if (Kind.isMergeableConst16() && MergeableConst16Section)
408 return MergeableConst16Section;
409 return ReadOnlySection; // .const
412 if (Kind.isReadOnly()) return ReadOnlySection;
414 if (Kind.isThreadData()) return TLSDataSection;
415 if (Kind.isThreadBSS()) return TLSBSSSection;
417 // Note: we claim that common symbols are put in BSSSection, but they are
418 // really emitted with the magic .comm directive, which creates a symbol table
419 // entry but not a section.
420 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
422 if (Kind.isDataNoRel()) return DataSection;
423 if (Kind.isDataRelLocal()) return DataRelLocalSection;
424 if (Kind.isDataRel()) return DataRelSection;
425 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
427 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
428 return DataRelROSection;
431 /// getSectionForConstant - Given a mergeable constant with the
432 /// specified size and relocation information, return a section that it
433 /// should be placed in.
434 const MCSection *TargetLoweringObjectFileELF::
435 getSectionForConstant(SectionKind Kind) const {
436 if (Kind.isMergeableConst4() && MergeableConst4Section)
437 return MergeableConst4Section;
438 if (Kind.isMergeableConst8() && MergeableConst8Section)
439 return MergeableConst8Section;
440 if (Kind.isMergeableConst16() && MergeableConst16Section)
441 return MergeableConst16Section;
442 if (Kind.isReadOnly())
443 return ReadOnlySection;
445 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
446 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
447 return DataRelROSection;
450 const MCExpr *TargetLoweringObjectFileELF::
451 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
452 MachineModuleInfo *MMI,
453 unsigned Encoding, MCStreamer &Streamer) const {
455 if (Encoding & dwarf::DW_EH_PE_indirect) {
456 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
458 SmallString<128> Name;
459 Mang->getNameWithPrefix(Name, GV, true);
462 // Add information about the stub reference to ELFMMI so that the stub
463 // gets emitted by the asmprinter.
464 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
465 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
466 if (StubSym.getPointer() == 0) {
467 MCSymbol *Sym = Mang->getSymbol(GV);
468 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
471 return TargetLoweringObjectFile::
472 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
475 return TargetLoweringObjectFile::
476 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
479 //===----------------------------------------------------------------------===//
481 //===----------------------------------------------------------------------===//
483 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
484 const TargetMachine &TM) {
485 IsFunctionEHFrameSymbolPrivate = false;
486 SupportsWeakOmittedEHFrame = false;
488 // .comm doesn't support alignment before Leopard.
489 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
491 if (T.isMacOSXVersionLT(10, 5))
492 CommDirectiveSupportsAlignment = false;
493 if (!T.isMacOSXVersionLT(10, 6))
494 SupportsCompactUnwindInfo = true;
497 TargetLoweringObjectFile::Initialize(Ctx, TM);
500 = getContext().getMachOSection("__TEXT", "__text",
501 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
502 SectionKind::getText());
504 = getContext().getMachOSection("__DATA", "__data", 0,
505 SectionKind::getDataRel());
507 TLSDataSection // .tdata
508 = getContext().getMachOSection("__DATA", "__thread_data",
509 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
510 SectionKind::getDataRel());
511 TLSBSSSection // .tbss
512 = getContext().getMachOSection("__DATA", "__thread_bss",
513 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
514 SectionKind::getThreadBSS());
516 // TODO: Verify datarel below.
517 TLSTLVSection // .tlv
518 = getContext().getMachOSection("__DATA", "__thread_vars",
519 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
520 SectionKind::getDataRel());
523 = getContext().getMachOSection("__DATA", "__thread_init",
524 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
525 SectionKind::getDataRel());
527 CStringSection // .cstring
528 = getContext().getMachOSection("__TEXT", "__cstring",
529 MCSectionMachO::S_CSTRING_LITERALS,
530 SectionKind::getMergeable1ByteCString());
532 = getContext().getMachOSection("__TEXT","__ustring", 0,
533 SectionKind::getMergeable2ByteCString());
534 FourByteConstantSection // .literal4
535 = getContext().getMachOSection("__TEXT", "__literal4",
536 MCSectionMachO::S_4BYTE_LITERALS,
537 SectionKind::getMergeableConst4());
538 EightByteConstantSection // .literal8
539 = getContext().getMachOSection("__TEXT", "__literal8",
540 MCSectionMachO::S_8BYTE_LITERALS,
541 SectionKind::getMergeableConst8());
543 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
544 // to using it in -static mode.
545 SixteenByteConstantSection = 0;
546 if (TM.getRelocationModel() != Reloc::Static &&
547 TM.getTargetData()->getPointerSize() == 32)
548 SixteenByteConstantSection = // .literal16
549 getContext().getMachOSection("__TEXT", "__literal16",
550 MCSectionMachO::S_16BYTE_LITERALS,
551 SectionKind::getMergeableConst16());
553 ReadOnlySection // .const
554 = getContext().getMachOSection("__TEXT", "__const", 0,
555 SectionKind::getReadOnly());
558 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
559 MCSectionMachO::S_COALESCED |
560 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
561 SectionKind::getText());
563 = getContext().getMachOSection("__TEXT", "__const_coal",
564 MCSectionMachO::S_COALESCED,
565 SectionKind::getReadOnly());
566 ConstDataSection // .const_data
567 = getContext().getMachOSection("__DATA", "__const", 0,
568 SectionKind::getReadOnlyWithRel());
570 = getContext().getMachOSection("__DATA","__datacoal_nt",
571 MCSectionMachO::S_COALESCED,
572 SectionKind::getDataRel());
574 = getContext().getMachOSection("__DATA","__common",
575 MCSectionMachO::S_ZEROFILL,
576 SectionKind::getBSS());
578 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
579 SectionKind::getBSS());
582 LazySymbolPointerSection
583 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
584 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
585 SectionKind::getMetadata());
586 NonLazySymbolPointerSection
587 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
588 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
589 SectionKind::getMetadata());
591 if (TM.getRelocationModel() == Reloc::Static) {
593 = getContext().getMachOSection("__TEXT", "__constructor", 0,
594 SectionKind::getDataRel());
596 = getContext().getMachOSection("__TEXT", "__destructor", 0,
597 SectionKind::getDataRel());
600 = getContext().getMachOSection("__DATA", "__mod_init_func",
601 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
602 SectionKind::getDataRel());
604 = getContext().getMachOSection("__DATA", "__mod_term_func",
605 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
606 SectionKind::getDataRel());
609 // Exception Handling.
610 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
611 SectionKind::getReadOnlyWithRel());
613 CompactUnwindSection =
614 getContext().getMachOSection("__LD", "__compact_unwind",
615 MCSectionMachO::S_ATTR_DEBUG,
616 SectionKind::getReadOnly());
618 // Debug Information.
620 getContext().getMachOSection("__DWARF", "__debug_abbrev",
621 MCSectionMachO::S_ATTR_DEBUG,
622 SectionKind::getMetadata());
624 getContext().getMachOSection("__DWARF", "__debug_info",
625 MCSectionMachO::S_ATTR_DEBUG,
626 SectionKind::getMetadata());
628 getContext().getMachOSection("__DWARF", "__debug_line",
629 MCSectionMachO::S_ATTR_DEBUG,
630 SectionKind::getMetadata());
632 getContext().getMachOSection("__DWARF", "__debug_frame",
633 MCSectionMachO::S_ATTR_DEBUG,
634 SectionKind::getMetadata());
635 DwarfPubNamesSection =
636 getContext().getMachOSection("__DWARF", "__debug_pubnames",
637 MCSectionMachO::S_ATTR_DEBUG,
638 SectionKind::getMetadata());
639 DwarfPubTypesSection =
640 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
641 MCSectionMachO::S_ATTR_DEBUG,
642 SectionKind::getMetadata());
644 getContext().getMachOSection("__DWARF", "__debug_str",
645 MCSectionMachO::S_ATTR_DEBUG,
646 SectionKind::getMetadata());
648 getContext().getMachOSection("__DWARF", "__debug_loc",
649 MCSectionMachO::S_ATTR_DEBUG,
650 SectionKind::getMetadata());
651 DwarfARangesSection =
652 getContext().getMachOSection("__DWARF", "__debug_aranges",
653 MCSectionMachO::S_ATTR_DEBUG,
654 SectionKind::getMetadata());
656 getContext().getMachOSection("__DWARF", "__debug_ranges",
657 MCSectionMachO::S_ATTR_DEBUG,
658 SectionKind::getMetadata());
659 DwarfMacroInfoSection =
660 getContext().getMachOSection("__DWARF", "__debug_macinfo",
661 MCSectionMachO::S_ATTR_DEBUG,
662 SectionKind::getMetadata());
663 DwarfDebugInlineSection =
664 getContext().getMachOSection("__DWARF", "__debug_inlined",
665 MCSectionMachO::S_ATTR_DEBUG,
666 SectionKind::getMetadata());
668 TLSExtraDataSection = TLSTLVSection;
671 const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
672 return getContext().getMachOSection("__TEXT", "__eh_frame",
673 MCSectionMachO::S_COALESCED |
674 MCSectionMachO::S_ATTR_NO_TOC |
675 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
676 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
677 SectionKind::getReadOnly());
680 const MCSection *TargetLoweringObjectFileMachO::
681 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
682 Mangler *Mang, const TargetMachine &TM) const {
683 // Parse the section specifier and create it if valid.
684 StringRef Segment, Section;
685 unsigned TAA = 0, StubSize = 0;
687 std::string ErrorCode =
688 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
689 TAA, TAAParsed, StubSize);
690 if (!ErrorCode.empty()) {
691 // If invalid, report the error with report_fatal_error.
692 report_fatal_error("Global variable '" + GV->getNameStr() +
693 "' has an invalid section specifier '" + GV->getSection()+
694 "': " + ErrorCode + ".");
695 // Fall back to dropping it into the data section.
700 const MCSectionMachO *S =
701 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
703 // If TAA wasn't set by ParseSectionSpecifier() above,
704 // use the value returned by getMachOSection() as a default.
706 TAA = S->getTypeAndAttributes();
708 // Okay, now that we got the section, verify that the TAA & StubSize agree.
709 // If the user declared multiple globals with different section flags, we need
710 // to reject it here.
711 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
712 // If invalid, report the error with report_fatal_error.
713 report_fatal_error("Global variable '" + GV->getNameStr() +
714 "' section type or attributes does not match previous"
715 " section specifier");
721 const MCSection *TargetLoweringObjectFileMachO::
722 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
723 Mangler *Mang, const TargetMachine &TM) const {
725 // Handle thread local data.
726 if (Kind.isThreadBSS()) return TLSBSSSection;
727 if (Kind.isThreadData()) return TLSDataSection;
730 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
732 // If this is weak/linkonce, put this in a coalescable section, either in text
733 // or data depending on if it is writable.
734 if (GV->isWeakForLinker()) {
735 if (Kind.isReadOnly())
736 return ConstTextCoalSection;
737 return DataCoalSection;
740 // FIXME: Alignment check should be handled by section classifier.
741 if (Kind.isMergeable1ByteCString() &&
742 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
743 return CStringSection;
745 // Do not put 16-bit arrays in the UString section if they have an
746 // externally visible label, this runs into issues with certain linker
748 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
749 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
750 return UStringSection;
752 if (Kind.isMergeableConst()) {
753 if (Kind.isMergeableConst4())
754 return FourByteConstantSection;
755 if (Kind.isMergeableConst8())
756 return EightByteConstantSection;
757 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
758 return SixteenByteConstantSection;
761 // Otherwise, if it is readonly, but not something we can specially optimize,
762 // just drop it in .const.
763 if (Kind.isReadOnly())
764 return ReadOnlySection;
766 // If this is marked const, put it into a const section. But if the dynamic
767 // linker needs to write to it, put it in the data segment.
768 if (Kind.isReadOnlyWithRel())
769 return ConstDataSection;
771 // Put zero initialized globals with strong external linkage in the
772 // DATA, __common section with the .zerofill directive.
773 if (Kind.isBSSExtern())
774 return DataCommonSection;
776 // Put zero initialized globals with local linkage in __DATA,__bss directive
777 // with the .zerofill directive (aka .lcomm).
778 if (Kind.isBSSLocal())
779 return DataBSSSection;
781 // Otherwise, just drop the variable in the normal data section.
786 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
787 // If this constant requires a relocation, we have to put it in the data
788 // segment, not in the text segment.
789 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
790 return ConstDataSection;
792 if (Kind.isMergeableConst4())
793 return FourByteConstantSection;
794 if (Kind.isMergeableConst8())
795 return EightByteConstantSection;
796 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
797 return SixteenByteConstantSection;
798 return ReadOnlySection; // .const
801 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
802 /// not to emit the UsedDirective for some symbols in llvm.used.
803 // FIXME: REMOVE this (rdar://7071300)
804 bool TargetLoweringObjectFileMachO::
805 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
806 /// On Darwin, internally linked data beginning with "L" or "l" does not have
807 /// the directive emitted (this occurs in ObjC metadata).
808 if (!GV) return false;
810 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
811 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
812 // FIXME: ObjC metadata is currently emitted as internal symbols that have
813 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
814 // this horrible hack can go away.
815 MCSymbol *Sym = Mang->getSymbol(GV);
816 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
823 const MCExpr *TargetLoweringObjectFileMachO::
824 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
825 MachineModuleInfo *MMI, unsigned Encoding,
826 MCStreamer &Streamer) const {
827 // The mach-o version of this method defaults to returning a stub reference.
829 if (Encoding & DW_EH_PE_indirect) {
830 MachineModuleInfoMachO &MachOMMI =
831 MMI->getObjFileInfo<MachineModuleInfoMachO>();
833 SmallString<128> Name;
834 Mang->getNameWithPrefix(Name, GV, true);
835 Name += "$non_lazy_ptr";
837 // Add information about the stub reference to MachOMMI so that the stub
838 // gets emitted by the asmprinter.
839 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
840 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
841 if (StubSym.getPointer() == 0) {
842 MCSymbol *Sym = Mang->getSymbol(GV);
843 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
846 return TargetLoweringObjectFile::
847 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
850 return TargetLoweringObjectFile::
851 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
854 MCSymbol *TargetLoweringObjectFileMachO::
855 getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
856 MachineModuleInfo *MMI) const {
857 // The mach-o version of this method defaults to returning a stub reference.
858 MachineModuleInfoMachO &MachOMMI =
859 MMI->getObjFileInfo<MachineModuleInfoMachO>();
861 SmallString<128> Name;
862 Mang->getNameWithPrefix(Name, GV, true);
863 Name += "$non_lazy_ptr";
865 // Add information about the stub reference to MachOMMI so that the stub
866 // gets emitted by the asmprinter.
867 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
868 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
869 if (StubSym.getPointer() == 0) {
870 MCSymbol *Sym = Mang->getSymbol(GV);
871 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
877 unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
878 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
881 unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
882 return DW_EH_PE_pcrel;
885 unsigned TargetLoweringObjectFileMachO::getFDEEncoding(bool CFI) const {
886 return DW_EH_PE_pcrel;
889 unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
890 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
893 //===----------------------------------------------------------------------===//
895 //===----------------------------------------------------------------------===//
897 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
898 const TargetMachine &TM) {
899 TargetLoweringObjectFile::Initialize(Ctx, TM);
901 getContext().getCOFFSection(".text",
902 COFF::IMAGE_SCN_CNT_CODE |
903 COFF::IMAGE_SCN_MEM_EXECUTE |
904 COFF::IMAGE_SCN_MEM_READ,
905 SectionKind::getText());
907 getContext().getCOFFSection(".data",
908 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
909 COFF::IMAGE_SCN_MEM_READ |
910 COFF::IMAGE_SCN_MEM_WRITE,
911 SectionKind::getDataRel());
913 getContext().getCOFFSection(".rdata",
914 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
915 COFF::IMAGE_SCN_MEM_READ,
916 SectionKind::getReadOnly());
918 getContext().getCOFFSection(".ctors",
919 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
920 COFF::IMAGE_SCN_MEM_READ |
921 COFF::IMAGE_SCN_MEM_WRITE,
922 SectionKind::getDataRel());
924 getContext().getCOFFSection(".dtors",
925 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
926 COFF::IMAGE_SCN_MEM_READ |
927 COFF::IMAGE_SCN_MEM_WRITE,
928 SectionKind::getDataRel());
930 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
931 // though it contains relocatable pointers. In PIC mode, this is probably a
932 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
933 // adjusted or this should be a data section.
935 getContext().getCOFFSection(".gcc_except_table",
936 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
937 COFF::IMAGE_SCN_MEM_READ,
938 SectionKind::getReadOnly());
941 getContext().getCOFFSection(".debug_abbrev",
942 COFF::IMAGE_SCN_MEM_DISCARDABLE |
943 COFF::IMAGE_SCN_MEM_READ,
944 SectionKind::getMetadata());
946 getContext().getCOFFSection(".debug_info",
947 COFF::IMAGE_SCN_MEM_DISCARDABLE |
948 COFF::IMAGE_SCN_MEM_READ,
949 SectionKind::getMetadata());
951 getContext().getCOFFSection(".debug_line",
952 COFF::IMAGE_SCN_MEM_DISCARDABLE |
953 COFF::IMAGE_SCN_MEM_READ,
954 SectionKind::getMetadata());
956 getContext().getCOFFSection(".debug_frame",
957 COFF::IMAGE_SCN_MEM_DISCARDABLE |
958 COFF::IMAGE_SCN_MEM_READ,
959 SectionKind::getMetadata());
960 DwarfPubNamesSection =
961 getContext().getCOFFSection(".debug_pubnames",
962 COFF::IMAGE_SCN_MEM_DISCARDABLE |
963 COFF::IMAGE_SCN_MEM_READ,
964 SectionKind::getMetadata());
965 DwarfPubTypesSection =
966 getContext().getCOFFSection(".debug_pubtypes",
967 COFF::IMAGE_SCN_MEM_DISCARDABLE |
968 COFF::IMAGE_SCN_MEM_READ,
969 SectionKind::getMetadata());
971 getContext().getCOFFSection(".debug_str",
972 COFF::IMAGE_SCN_MEM_DISCARDABLE |
973 COFF::IMAGE_SCN_MEM_READ,
974 SectionKind::getMetadata());
976 getContext().getCOFFSection(".debug_loc",
977 COFF::IMAGE_SCN_MEM_DISCARDABLE |
978 COFF::IMAGE_SCN_MEM_READ,
979 SectionKind::getMetadata());
980 DwarfARangesSection =
981 getContext().getCOFFSection(".debug_aranges",
982 COFF::IMAGE_SCN_MEM_DISCARDABLE |
983 COFF::IMAGE_SCN_MEM_READ,
984 SectionKind::getMetadata());
986 getContext().getCOFFSection(".debug_ranges",
987 COFF::IMAGE_SCN_MEM_DISCARDABLE |
988 COFF::IMAGE_SCN_MEM_READ,
989 SectionKind::getMetadata());
990 DwarfMacroInfoSection =
991 getContext().getCOFFSection(".debug_macinfo",
992 COFF::IMAGE_SCN_MEM_DISCARDABLE |
993 COFF::IMAGE_SCN_MEM_READ,
994 SectionKind::getMetadata());
997 getContext().getCOFFSection(".drectve",
998 COFF::IMAGE_SCN_LNK_INFO,
999 SectionKind::getMetadata());
1002 getContext().getCOFFSection(".pdata",
1003 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1004 COFF::IMAGE_SCN_MEM_READ |
1005 COFF::IMAGE_SCN_MEM_WRITE,
1006 SectionKind::getDataRel());
1009 getContext().getCOFFSection(".xdata",
1010 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1011 COFF::IMAGE_SCN_MEM_READ |
1012 COFF::IMAGE_SCN_MEM_WRITE,
1013 SectionKind::getDataRel());
1016 const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
1017 return getContext().getCOFFSection(".eh_frame",
1018 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1019 COFF::IMAGE_SCN_MEM_READ |
1020 COFF::IMAGE_SCN_MEM_WRITE,
1021 SectionKind::getDataRel());
1024 const MCSection *TargetLoweringObjectFileCOFF::getWin64EHFuncTableSection(
1025 StringRef suffix) const {
1027 return PDataSection;
1028 return getContext().getCOFFSection((".pdata"+suffix).str(),
1029 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1030 COFF::IMAGE_SCN_MEM_READ |
1031 COFF::IMAGE_SCN_MEM_WRITE,
1032 SectionKind::getDataRel());
1035 const MCSection *TargetLoweringObjectFileCOFF::getWin64EHTableSection(
1036 StringRef suffix) const {
1038 return XDataSection;
1039 return getContext().getCOFFSection((".xdata"+suffix).str(),
1040 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1041 COFF::IMAGE_SCN_MEM_READ |
1042 COFF::IMAGE_SCN_MEM_WRITE,
1043 SectionKind::getDataRel());
1048 getCOFFSectionFlags(SectionKind K) {
1053 COFF::IMAGE_SCN_MEM_DISCARDABLE;
1054 else if (K.isText())
1056 COFF::IMAGE_SCN_MEM_EXECUTE |
1057 COFF::IMAGE_SCN_MEM_READ |
1058 COFF::IMAGE_SCN_CNT_CODE;
1059 else if (K.isBSS ())
1061 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1062 COFF::IMAGE_SCN_MEM_READ |
1063 COFF::IMAGE_SCN_MEM_WRITE;
1064 else if (K.isReadOnly())
1066 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1067 COFF::IMAGE_SCN_MEM_READ;
1068 else if (K.isWriteable())
1070 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1071 COFF::IMAGE_SCN_MEM_READ |
1072 COFF::IMAGE_SCN_MEM_WRITE;
1077 const MCSection *TargetLoweringObjectFileCOFF::
1078 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1079 Mangler *Mang, const TargetMachine &TM) const {
1080 return getContext().getCOFFSection(GV->getSection(),
1081 getCOFFSectionFlags(Kind),
1085 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1090 if (Kind.isWriteable())
1096 const MCSection *TargetLoweringObjectFileCOFF::
1097 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1098 Mangler *Mang, const TargetMachine &TM) const {
1099 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1101 // If this global is linkonce/weak and the target handles this by emitting it
1102 // into a 'uniqued' section name, create and return the section now.
1103 if (GV->isWeakForLinker()) {
1104 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1105 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
1106 MCSymbol *Sym = Mang->getSymbol(GV);
1107 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
1109 unsigned Characteristics = getCOFFSectionFlags(Kind);
1111 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1113 return getContext().getCOFFSection(Name.str(), Characteristics,
1114 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
1118 return getTextSection();
1120 return getDataSection();