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 Twine FullName = StringRef("DW.ref.") + Mang->getSymbol(GV)->getName();
193 return getContext().GetOrCreateSymbol(FullName);
199 void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
200 const TargetMachine &TM,
201 const MCSymbol *Sym) const {
202 Twine FullName = StringRef("DW.ref.") + Sym->getName();
203 MCSymbol *Label = getContext().GetOrCreateSymbol(FullName);
204 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
205 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
206 Twine SectionName = StringRef(".data.") + Label->getName();
207 SmallString<64> NameData;
208 SectionName.toVector(NameData);
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 // FIXME: Why is this here? Codegen is should not be in the business
229 // of figuring section flags. If the user wrote section(".eh_frame"),
230 // we should just pass that to MC which will defer to the assembly
231 // or use its default if producing an object file.
232 if (Name.empty() || Name[0] != '.') return K;
234 // Some lame default implementation based on some magic section names.
235 if (Name == ".bss" ||
236 Name.startswith(".bss.") ||
237 Name.startswith(".gnu.linkonce.b.") ||
238 Name.startswith(".llvm.linkonce.b.") ||
240 Name.startswith(".sbss.") ||
241 Name.startswith(".gnu.linkonce.sb.") ||
242 Name.startswith(".llvm.linkonce.sb."))
243 return SectionKind::getBSS();
245 if (Name == ".tdata" ||
246 Name.startswith(".tdata.") ||
247 Name.startswith(".gnu.linkonce.td.") ||
248 Name.startswith(".llvm.linkonce.td."))
249 return SectionKind::getThreadData();
251 if (Name == ".tbss" ||
252 Name.startswith(".tbss.") ||
253 Name.startswith(".gnu.linkonce.tb.") ||
254 Name.startswith(".llvm.linkonce.tb."))
255 return SectionKind::getThreadBSS();
257 if (Name == ".eh_frame")
258 return SectionKind::getDataRel();
264 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
266 if (Name == ".init_array")
267 return ELF::SHT_INIT_ARRAY;
269 if (Name == ".fini_array")
270 return ELF::SHT_FINI_ARRAY;
272 if (Name == ".preinit_array")
273 return ELF::SHT_PREINIT_ARRAY;
275 if (K.isBSS() || K.isThreadBSS())
276 return ELF::SHT_NOBITS;
278 return ELF::SHT_PROGBITS;
283 getELFSectionFlags(SectionKind K) {
287 Flags |= ELF::SHF_ALLOC;
290 Flags |= ELF::SHF_EXECINSTR;
293 Flags |= ELF::SHF_WRITE;
295 if (K.isThreadLocal())
296 Flags |= ELF::SHF_TLS;
298 // K.isMergeableConst() is left out to honour PR4650
299 if (K.isMergeableCString() || K.isMergeableConst4() ||
300 K.isMergeableConst8() || K.isMergeableConst16())
301 Flags |= ELF::SHF_MERGE;
303 if (K.isMergeableCString())
304 Flags |= ELF::SHF_STRINGS;
310 const MCSection *TargetLoweringObjectFileELF::
311 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
312 Mangler *Mang, const TargetMachine &TM) const {
313 StringRef SectionName = GV->getSection();
315 // Infer section flags from the section name if we can.
316 Kind = getELFKindForNamedSection(SectionName, Kind);
318 return getContext().getELFSection(SectionName,
319 getELFSectionType(SectionName, Kind),
320 getELFSectionFlags(Kind), Kind);
323 /// getSectionPrefixForGlobal - Return the section prefix name used by options
324 /// FunctionsSections and DataSections.
325 static const char *getSectionPrefixForGlobal(SectionKind Kind) {
326 if (Kind.isText()) return ".text.";
327 if (Kind.isReadOnly()) return ".rodata.";
329 if (Kind.isThreadData()) return ".tdata.";
330 if (Kind.isThreadBSS()) return ".tbss.";
332 if (Kind.isDataNoRel()) return ".data.";
333 if (Kind.isDataRelLocal()) return ".data.rel.local.";
334 if (Kind.isDataRel()) return ".data.rel.";
335 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
337 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
338 return ".data.rel.ro.";
342 const MCSection *TargetLoweringObjectFileELF::
343 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
344 Mangler *Mang, const TargetMachine &TM) const {
345 // If we have -ffunction-section or -fdata-section then we should emit the
346 // global value to a uniqued section specifically for it.
347 bool EmitUniquedSection;
349 EmitUniquedSection = TM.getFunctionSections();
351 EmitUniquedSection = TM.getDataSections();
353 // If this global is linkonce/weak and the target handles this by emitting it
354 // into a 'uniqued' section name, create and return the section now.
355 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
356 !Kind.isCommon() && !Kind.isBSS()) {
358 Prefix = getSectionPrefixForGlobal(Kind);
360 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
361 MCSymbol *Sym = Mang->getSymbol(GV);
362 Name.append(Sym->getName().begin(), Sym->getName().end());
363 StringRef Group = "";
364 unsigned Flags = getELFSectionFlags(Kind);
365 if (GV->isWeakForLinker()) {
366 Group = Sym->getName();
367 Flags |= ELF::SHF_GROUP;
370 return getContext().getELFSection(Name.str(),
371 getELFSectionType(Name.str(), Kind),
372 Flags, Kind, 0, Group);
375 if (Kind.isText()) return TextSection;
377 if (Kind.isMergeable1ByteCString() ||
378 Kind.isMergeable2ByteCString() ||
379 Kind.isMergeable4ByteCString()) {
381 // We also need alignment here.
382 // FIXME: this is getting the alignment of the character, not the
383 // alignment of the global!
385 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
387 const char *SizeSpec = ".rodata.str1.";
388 if (Kind.isMergeable2ByteCString())
389 SizeSpec = ".rodata.str2.";
390 else if (Kind.isMergeable4ByteCString())
391 SizeSpec = ".rodata.str4.";
393 assert(Kind.isMergeable1ByteCString() && "unknown string width");
396 std::string Name = SizeSpec + utostr(Align);
397 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
404 if (Kind.isMergeableConst()) {
405 if (Kind.isMergeableConst4() && MergeableConst4Section)
406 return MergeableConst4Section;
407 if (Kind.isMergeableConst8() && MergeableConst8Section)
408 return MergeableConst8Section;
409 if (Kind.isMergeableConst16() && MergeableConst16Section)
410 return MergeableConst16Section;
411 return ReadOnlySection; // .const
414 if (Kind.isReadOnly()) return ReadOnlySection;
416 if (Kind.isThreadData()) return TLSDataSection;
417 if (Kind.isThreadBSS()) return TLSBSSSection;
419 // Note: we claim that common symbols are put in BSSSection, but they are
420 // really emitted with the magic .comm directive, which creates a symbol table
421 // entry but not a section.
422 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
424 if (Kind.isDataNoRel()) return DataSection;
425 if (Kind.isDataRelLocal()) return DataRelLocalSection;
426 if (Kind.isDataRel()) return DataRelSection;
427 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
429 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
430 return DataRelROSection;
433 /// getSectionForConstant - Given a mergeable constant with the
434 /// specified size and relocation information, return a section that it
435 /// should be placed in.
436 const MCSection *TargetLoweringObjectFileELF::
437 getSectionForConstant(SectionKind Kind) const {
438 if (Kind.isMergeableConst4() && MergeableConst4Section)
439 return MergeableConst4Section;
440 if (Kind.isMergeableConst8() && MergeableConst8Section)
441 return MergeableConst8Section;
442 if (Kind.isMergeableConst16() && MergeableConst16Section)
443 return MergeableConst16Section;
444 if (Kind.isReadOnly())
445 return ReadOnlySection;
447 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
448 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
449 return DataRelROSection;
452 const MCExpr *TargetLoweringObjectFileELF::
453 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
454 MachineModuleInfo *MMI,
455 unsigned Encoding, MCStreamer &Streamer) const {
457 if (Encoding & dwarf::DW_EH_PE_indirect) {
458 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
460 SmallString<128> Name;
461 Mang->getNameWithPrefix(Name, GV, true);
464 // Add information about the stub reference to ELFMMI so that the stub
465 // gets emitted by the asmprinter.
466 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
467 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
468 if (StubSym.getPointer() == 0) {
469 MCSymbol *Sym = Mang->getSymbol(GV);
470 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
473 return TargetLoweringObjectFile::
474 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
477 return TargetLoweringObjectFile::
478 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
481 //===----------------------------------------------------------------------===//
483 //===----------------------------------------------------------------------===//
485 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
486 const TargetMachine &TM) {
487 IsFunctionEHFrameSymbolPrivate = false;
488 SupportsWeakOmittedEHFrame = false;
490 // .comm doesn't support alignment before Leopard.
491 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
492 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
493 CommDirectiveSupportsAlignment = false;
495 TargetLoweringObjectFile::Initialize(Ctx, TM);
498 = getContext().getMachOSection("__TEXT", "__text",
499 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
500 SectionKind::getText());
502 = getContext().getMachOSection("__DATA", "__data", 0,
503 SectionKind::getDataRel());
505 TLSDataSection // .tdata
506 = getContext().getMachOSection("__DATA", "__thread_data",
507 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
508 SectionKind::getDataRel());
509 TLSBSSSection // .tbss
510 = getContext().getMachOSection("__DATA", "__thread_bss",
511 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
512 SectionKind::getThreadBSS());
514 // TODO: Verify datarel below.
515 TLSTLVSection // .tlv
516 = getContext().getMachOSection("__DATA", "__thread_vars",
517 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
518 SectionKind::getDataRel());
521 = getContext().getMachOSection("__DATA", "__thread_init",
522 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
523 SectionKind::getDataRel());
525 CStringSection // .cstring
526 = getContext().getMachOSection("__TEXT", "__cstring",
527 MCSectionMachO::S_CSTRING_LITERALS,
528 SectionKind::getMergeable1ByteCString());
530 = getContext().getMachOSection("__TEXT","__ustring", 0,
531 SectionKind::getMergeable2ByteCString());
532 FourByteConstantSection // .literal4
533 = getContext().getMachOSection("__TEXT", "__literal4",
534 MCSectionMachO::S_4BYTE_LITERALS,
535 SectionKind::getMergeableConst4());
536 EightByteConstantSection // .literal8
537 = getContext().getMachOSection("__TEXT", "__literal8",
538 MCSectionMachO::S_8BYTE_LITERALS,
539 SectionKind::getMergeableConst8());
541 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
542 // to using it in -static mode.
543 SixteenByteConstantSection = 0;
544 if (TM.getRelocationModel() != Reloc::Static &&
545 TM.getTargetData()->getPointerSize() == 32)
546 SixteenByteConstantSection = // .literal16
547 getContext().getMachOSection("__TEXT", "__literal16",
548 MCSectionMachO::S_16BYTE_LITERALS,
549 SectionKind::getMergeableConst16());
551 ReadOnlySection // .const
552 = getContext().getMachOSection("__TEXT", "__const", 0,
553 SectionKind::getReadOnly());
556 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
557 MCSectionMachO::S_COALESCED |
558 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
559 SectionKind::getText());
561 = getContext().getMachOSection("__TEXT", "__const_coal",
562 MCSectionMachO::S_COALESCED,
563 SectionKind::getReadOnly());
564 ConstDataSection // .const_data
565 = getContext().getMachOSection("__DATA", "__const", 0,
566 SectionKind::getReadOnlyWithRel());
568 = getContext().getMachOSection("__DATA","__datacoal_nt",
569 MCSectionMachO::S_COALESCED,
570 SectionKind::getDataRel());
572 = getContext().getMachOSection("__DATA","__common",
573 MCSectionMachO::S_ZEROFILL,
574 SectionKind::getBSS());
576 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
577 SectionKind::getBSS());
580 LazySymbolPointerSection
581 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
582 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
583 SectionKind::getMetadata());
584 NonLazySymbolPointerSection
585 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
586 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
587 SectionKind::getMetadata());
589 if (TM.getRelocationModel() == Reloc::Static) {
591 = getContext().getMachOSection("__TEXT", "__constructor", 0,
592 SectionKind::getDataRel());
594 = getContext().getMachOSection("__TEXT", "__destructor", 0,
595 SectionKind::getDataRel());
598 = getContext().getMachOSection("__DATA", "__mod_init_func",
599 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
600 SectionKind::getDataRel());
602 = getContext().getMachOSection("__DATA", "__mod_term_func",
603 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
604 SectionKind::getDataRel());
607 // Exception Handling.
608 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
609 SectionKind::getReadOnlyWithRel());
610 // Debug Information.
612 getContext().getMachOSection("__DWARF", "__debug_abbrev",
613 MCSectionMachO::S_ATTR_DEBUG,
614 SectionKind::getMetadata());
616 getContext().getMachOSection("__DWARF", "__debug_info",
617 MCSectionMachO::S_ATTR_DEBUG,
618 SectionKind::getMetadata());
620 getContext().getMachOSection("__DWARF", "__debug_line",
621 MCSectionMachO::S_ATTR_DEBUG,
622 SectionKind::getMetadata());
624 getContext().getMachOSection("__DWARF", "__debug_frame",
625 MCSectionMachO::S_ATTR_DEBUG,
626 SectionKind::getMetadata());
627 DwarfPubNamesSection =
628 getContext().getMachOSection("__DWARF", "__debug_pubnames",
629 MCSectionMachO::S_ATTR_DEBUG,
630 SectionKind::getMetadata());
631 DwarfPubTypesSection =
632 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
633 MCSectionMachO::S_ATTR_DEBUG,
634 SectionKind::getMetadata());
636 getContext().getMachOSection("__DWARF", "__debug_str",
637 MCSectionMachO::S_ATTR_DEBUG,
638 SectionKind::getMetadata());
640 getContext().getMachOSection("__DWARF", "__debug_loc",
641 MCSectionMachO::S_ATTR_DEBUG,
642 SectionKind::getMetadata());
643 DwarfARangesSection =
644 getContext().getMachOSection("__DWARF", "__debug_aranges",
645 MCSectionMachO::S_ATTR_DEBUG,
646 SectionKind::getMetadata());
648 getContext().getMachOSection("__DWARF", "__debug_ranges",
649 MCSectionMachO::S_ATTR_DEBUG,
650 SectionKind::getMetadata());
651 DwarfMacroInfoSection =
652 getContext().getMachOSection("__DWARF", "__debug_macinfo",
653 MCSectionMachO::S_ATTR_DEBUG,
654 SectionKind::getMetadata());
655 DwarfDebugInlineSection =
656 getContext().getMachOSection("__DWARF", "__debug_inlined",
657 MCSectionMachO::S_ATTR_DEBUG,
658 SectionKind::getMetadata());
660 TLSExtraDataSection = TLSTLVSection;
663 const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
664 return getContext().getMachOSection("__TEXT", "__eh_frame",
665 MCSectionMachO::S_COALESCED |
666 MCSectionMachO::S_ATTR_NO_TOC |
667 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
668 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
669 SectionKind::getReadOnly());
672 const MCSection *TargetLoweringObjectFileMachO::
673 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
674 Mangler *Mang, const TargetMachine &TM) const {
675 // Parse the section specifier and create it if valid.
676 StringRef Segment, Section;
677 unsigned TAA = 0, StubSize = 0;
679 std::string ErrorCode =
680 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
681 TAA, TAAParsed, StubSize);
682 if (!ErrorCode.empty()) {
683 // If invalid, report the error with report_fatal_error.
684 report_fatal_error("Global variable '" + GV->getNameStr() +
685 "' has an invalid section specifier '" + GV->getSection()+
686 "': " + ErrorCode + ".");
687 // Fall back to dropping it into the data section.
692 const MCSectionMachO *S =
693 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
695 // If TAA wasn't set by ParseSectionSpecifier() above,
696 // use the value returned by getMachOSection() as a default.
698 TAA = S->getTypeAndAttributes();
700 // Okay, now that we got the section, verify that the TAA & StubSize agree.
701 // If the user declared multiple globals with different section flags, we need
702 // to reject it here.
703 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
704 // If invalid, report the error with report_fatal_error.
705 report_fatal_error("Global variable '" + GV->getNameStr() +
706 "' section type or attributes does not match previous"
707 " section specifier");
713 const MCSection *TargetLoweringObjectFileMachO::
714 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
715 Mangler *Mang, const TargetMachine &TM) const {
717 // Handle thread local data.
718 if (Kind.isThreadBSS()) return TLSBSSSection;
719 if (Kind.isThreadData()) return TLSDataSection;
722 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
724 // If this is weak/linkonce, put this in a coalescable section, either in text
725 // or data depending on if it is writable.
726 if (GV->isWeakForLinker()) {
727 if (Kind.isReadOnly())
728 return ConstTextCoalSection;
729 return DataCoalSection;
732 // FIXME: Alignment check should be handled by section classifier.
733 if (Kind.isMergeable1ByteCString() &&
734 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
735 return CStringSection;
737 // Do not put 16-bit arrays in the UString section if they have an
738 // externally visible label, this runs into issues with certain linker
740 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
741 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
742 return UStringSection;
744 if (Kind.isMergeableConst()) {
745 if (Kind.isMergeableConst4())
746 return FourByteConstantSection;
747 if (Kind.isMergeableConst8())
748 return EightByteConstantSection;
749 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
750 return SixteenByteConstantSection;
753 // Otherwise, if it is readonly, but not something we can specially optimize,
754 // just drop it in .const.
755 if (Kind.isReadOnly())
756 return ReadOnlySection;
758 // If this is marked const, put it into a const section. But if the dynamic
759 // linker needs to write to it, put it in the data segment.
760 if (Kind.isReadOnlyWithRel())
761 return ConstDataSection;
763 // Put zero initialized globals with strong external linkage in the
764 // DATA, __common section with the .zerofill directive.
765 if (Kind.isBSSExtern())
766 return DataCommonSection;
768 // Put zero initialized globals with local linkage in __DATA,__bss directive
769 // with the .zerofill directive (aka .lcomm).
770 if (Kind.isBSSLocal())
771 return DataBSSSection;
773 // Otherwise, just drop the variable in the normal data section.
778 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
779 // If this constant requires a relocation, we have to put it in the data
780 // segment, not in the text segment.
781 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
782 return ConstDataSection;
784 if (Kind.isMergeableConst4())
785 return FourByteConstantSection;
786 if (Kind.isMergeableConst8())
787 return EightByteConstantSection;
788 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
789 return SixteenByteConstantSection;
790 return ReadOnlySection; // .const
793 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
794 /// not to emit the UsedDirective for some symbols in llvm.used.
795 // FIXME: REMOVE this (rdar://7071300)
796 bool TargetLoweringObjectFileMachO::
797 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
798 /// On Darwin, internally linked data beginning with "L" or "l" does not have
799 /// the directive emitted (this occurs in ObjC metadata).
800 if (!GV) return false;
802 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
803 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
804 // FIXME: ObjC metadata is currently emitted as internal symbols that have
805 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
806 // this horrible hack can go away.
807 MCSymbol *Sym = Mang->getSymbol(GV);
808 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
815 const MCExpr *TargetLoweringObjectFileMachO::
816 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
817 MachineModuleInfo *MMI, unsigned Encoding,
818 MCStreamer &Streamer) const {
819 // The mach-o version of this method defaults to returning a stub reference.
821 if (Encoding & DW_EH_PE_indirect) {
822 MachineModuleInfoMachO &MachOMMI =
823 MMI->getObjFileInfo<MachineModuleInfoMachO>();
825 SmallString<128> Name;
826 Mang->getNameWithPrefix(Name, GV, true);
827 Name += "$non_lazy_ptr";
829 // Add information about the stub reference to MachOMMI so that the stub
830 // gets emitted by the asmprinter.
831 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
832 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
833 if (StubSym.getPointer() == 0) {
834 MCSymbol *Sym = Mang->getSymbol(GV);
835 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
838 return TargetLoweringObjectFile::
839 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
842 return TargetLoweringObjectFile::
843 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
846 MCSymbol *TargetLoweringObjectFileMachO::
847 getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
848 MachineModuleInfo *MMI) const {
849 // The mach-o version of this method defaults to returning a stub reference.
850 MachineModuleInfoMachO &MachOMMI =
851 MMI->getObjFileInfo<MachineModuleInfoMachO>();
853 SmallString<128> Name;
854 Mang->getNameWithPrefix(Name, GV, true);
855 Name += "$non_lazy_ptr";
857 // Add information about the stub reference to MachOMMI so that the stub
858 // gets emitted by the asmprinter.
859 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
860 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
861 if (StubSym.getPointer() == 0) {
862 MCSymbol *Sym = Mang->getSymbol(GV);
863 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
869 unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
870 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
873 unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
874 return DW_EH_PE_pcrel;
877 unsigned TargetLoweringObjectFileMachO::getFDEEncoding(bool CFI) const {
878 return DW_EH_PE_pcrel;
881 unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
882 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
885 //===----------------------------------------------------------------------===//
887 //===----------------------------------------------------------------------===//
889 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
890 const TargetMachine &TM) {
891 TargetLoweringObjectFile::Initialize(Ctx, TM);
893 getContext().getCOFFSection(".text",
894 COFF::IMAGE_SCN_CNT_CODE |
895 COFF::IMAGE_SCN_MEM_EXECUTE |
896 COFF::IMAGE_SCN_MEM_READ,
897 SectionKind::getText());
899 getContext().getCOFFSection(".data",
900 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
901 COFF::IMAGE_SCN_MEM_READ |
902 COFF::IMAGE_SCN_MEM_WRITE,
903 SectionKind::getDataRel());
905 getContext().getCOFFSection(".rdata",
906 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
907 COFF::IMAGE_SCN_MEM_READ,
908 SectionKind::getReadOnly());
910 getContext().getCOFFSection(".ctors",
911 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
912 COFF::IMAGE_SCN_MEM_READ |
913 COFF::IMAGE_SCN_MEM_WRITE,
914 SectionKind::getDataRel());
916 getContext().getCOFFSection(".dtors",
917 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
918 COFF::IMAGE_SCN_MEM_READ |
919 COFF::IMAGE_SCN_MEM_WRITE,
920 SectionKind::getDataRel());
922 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
923 // though it contains relocatable pointers. In PIC mode, this is probably a
924 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
925 // adjusted or this should be a data section.
927 getContext().getCOFFSection(".gcc_except_table",
928 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
929 COFF::IMAGE_SCN_MEM_READ,
930 SectionKind::getReadOnly());
933 getContext().getCOFFSection(".debug_abbrev",
934 COFF::IMAGE_SCN_MEM_DISCARDABLE |
935 COFF::IMAGE_SCN_MEM_READ,
936 SectionKind::getMetadata());
938 getContext().getCOFFSection(".debug_info",
939 COFF::IMAGE_SCN_MEM_DISCARDABLE |
940 COFF::IMAGE_SCN_MEM_READ,
941 SectionKind::getMetadata());
943 getContext().getCOFFSection(".debug_line",
944 COFF::IMAGE_SCN_MEM_DISCARDABLE |
945 COFF::IMAGE_SCN_MEM_READ,
946 SectionKind::getMetadata());
948 getContext().getCOFFSection(".debug_frame",
949 COFF::IMAGE_SCN_MEM_DISCARDABLE |
950 COFF::IMAGE_SCN_MEM_READ,
951 SectionKind::getMetadata());
952 DwarfPubNamesSection =
953 getContext().getCOFFSection(".debug_pubnames",
954 COFF::IMAGE_SCN_MEM_DISCARDABLE |
955 COFF::IMAGE_SCN_MEM_READ,
956 SectionKind::getMetadata());
957 DwarfPubTypesSection =
958 getContext().getCOFFSection(".debug_pubtypes",
959 COFF::IMAGE_SCN_MEM_DISCARDABLE |
960 COFF::IMAGE_SCN_MEM_READ,
961 SectionKind::getMetadata());
963 getContext().getCOFFSection(".debug_str",
964 COFF::IMAGE_SCN_MEM_DISCARDABLE |
965 COFF::IMAGE_SCN_MEM_READ,
966 SectionKind::getMetadata());
968 getContext().getCOFFSection(".debug_loc",
969 COFF::IMAGE_SCN_MEM_DISCARDABLE |
970 COFF::IMAGE_SCN_MEM_READ,
971 SectionKind::getMetadata());
972 DwarfARangesSection =
973 getContext().getCOFFSection(".debug_aranges",
974 COFF::IMAGE_SCN_MEM_DISCARDABLE |
975 COFF::IMAGE_SCN_MEM_READ,
976 SectionKind::getMetadata());
978 getContext().getCOFFSection(".debug_ranges",
979 COFF::IMAGE_SCN_MEM_DISCARDABLE |
980 COFF::IMAGE_SCN_MEM_READ,
981 SectionKind::getMetadata());
982 DwarfMacroInfoSection =
983 getContext().getCOFFSection(".debug_macinfo",
984 COFF::IMAGE_SCN_MEM_DISCARDABLE |
985 COFF::IMAGE_SCN_MEM_READ,
986 SectionKind::getMetadata());
989 getContext().getCOFFSection(".drectve",
990 COFF::IMAGE_SCN_LNK_INFO,
991 SectionKind::getMetadata());
994 const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
995 return getContext().getCOFFSection(".eh_frame",
996 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
997 COFF::IMAGE_SCN_MEM_READ |
998 COFF::IMAGE_SCN_MEM_WRITE,
999 SectionKind::getDataRel());
1004 getCOFFSectionFlags(SectionKind K) {
1009 COFF::IMAGE_SCN_MEM_DISCARDABLE;
1010 else if (K.isText())
1012 COFF::IMAGE_SCN_MEM_EXECUTE |
1013 COFF::IMAGE_SCN_MEM_READ |
1014 COFF::IMAGE_SCN_CNT_CODE;
1015 else if (K.isBSS ())
1017 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1018 COFF::IMAGE_SCN_MEM_READ |
1019 COFF::IMAGE_SCN_MEM_WRITE;
1020 else if (K.isReadOnly())
1022 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1023 COFF::IMAGE_SCN_MEM_READ;
1024 else if (K.isWriteable())
1026 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1027 COFF::IMAGE_SCN_MEM_READ |
1028 COFF::IMAGE_SCN_MEM_WRITE;
1033 const MCSection *TargetLoweringObjectFileCOFF::
1034 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1035 Mangler *Mang, const TargetMachine &TM) const {
1036 return getContext().getCOFFSection(GV->getSection(),
1037 getCOFFSectionFlags(Kind),
1041 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1046 if (Kind.isWriteable())
1052 const MCSection *TargetLoweringObjectFileCOFF::
1053 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1054 Mangler *Mang, const TargetMachine &TM) const {
1055 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1057 // If this global is linkonce/weak and the target handles this by emitting it
1058 // into a 'uniqued' section name, create and return the section now.
1059 if (GV->isWeakForLinker()) {
1060 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1061 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
1062 MCSymbol *Sym = Mang->getSymbol(GV);
1063 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
1065 unsigned Characteristics = getCOFFSectionFlags(Kind);
1067 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1069 return getContext().getCOFFSection(Name.str(), Characteristics,
1070 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
1074 return getTextSection();
1076 return getDataSection();