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/ELF.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/ADT/SmallString.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/ADT/Triple.h"
39 using namespace dwarf;
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
45 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
46 const TargetMachine &TM) {
47 TargetLoweringObjectFile::Initialize(Ctx, TM);
50 getContext().getELFSection(".bss", ELF::SHT_NOBITS,
51 ELF::SHF_WRITE |ELF::SHF_ALLOC,
52 SectionKind::getBSS());
55 getContext().getELFSection(".text", ELF::SHT_PROGBITS,
58 SectionKind::getText());
61 getContext().getELFSection(".data", ELF::SHT_PROGBITS,
62 ELF::SHF_WRITE |ELF::SHF_ALLOC,
63 SectionKind::getDataRel());
66 getContext().getELFSection(".rodata", ELF::SHT_PROGBITS,
68 SectionKind::getReadOnly());
71 getContext().getELFSection(".tdata", ELF::SHT_PROGBITS,
72 ELF::SHF_ALLOC | ELF::SHF_TLS |
74 SectionKind::getThreadData());
77 getContext().getELFSection(".tbss", ELF::SHT_NOBITS,
78 ELF::SHF_ALLOC | ELF::SHF_TLS |
80 SectionKind::getThreadBSS());
83 getContext().getELFSection(".data.rel", ELF::SHT_PROGBITS,
84 ELF::SHF_ALLOC |ELF::SHF_WRITE,
85 SectionKind::getDataRel());
88 getContext().getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
89 ELF::SHF_ALLOC |ELF::SHF_WRITE,
90 SectionKind::getDataRelLocal());
93 getContext().getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
94 ELF::SHF_ALLOC |ELF::SHF_WRITE,
95 SectionKind::getReadOnlyWithRel());
97 DataRelROLocalSection =
98 getContext().getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
99 ELF::SHF_ALLOC |ELF::SHF_WRITE,
100 SectionKind::getReadOnlyWithRelLocal());
102 MergeableConst4Section =
103 getContext().getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
104 ELF::SHF_ALLOC |ELF::SHF_MERGE,
105 SectionKind::getMergeableConst4());
107 MergeableConst8Section =
108 getContext().getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
109 ELF::SHF_ALLOC |ELF::SHF_MERGE,
110 SectionKind::getMergeableConst8());
112 MergeableConst16Section =
113 getContext().getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
114 ELF::SHF_ALLOC |ELF::SHF_MERGE,
115 SectionKind::getMergeableConst16());
118 getContext().getELFSection(".ctors", ELF::SHT_PROGBITS,
119 ELF::SHF_ALLOC |ELF::SHF_WRITE,
120 SectionKind::getDataRel());
123 getContext().getELFSection(".dtors", ELF::SHT_PROGBITS,
124 ELF::SHF_ALLOC |ELF::SHF_WRITE,
125 SectionKind::getDataRel());
127 // Exception Handling Sections.
129 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
130 // it contains relocatable pointers. In PIC mode, this is probably a big
131 // runtime hit for C++ apps. Either the contents of the LSDA need to be
132 // adjusted or this should be a data section.
134 getContext().getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
136 SectionKind::getReadOnly());
137 // Debug Info Sections.
139 getContext().getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
140 SectionKind::getMetadata());
142 getContext().getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
143 SectionKind::getMetadata());
145 getContext().getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
146 SectionKind::getMetadata());
148 getContext().getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
149 SectionKind::getMetadata());
150 DwarfPubNamesSection =
151 getContext().getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
152 SectionKind::getMetadata());
153 DwarfPubTypesSection =
154 getContext().getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
155 SectionKind::getMetadata());
157 getContext().getELFSection(".debug_str", ELF::SHT_PROGBITS, 0,
158 SectionKind::getMetadata());
160 getContext().getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
161 SectionKind::getMetadata());
162 DwarfARangesSection =
163 getContext().getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
164 SectionKind::getMetadata());
166 getContext().getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
167 SectionKind::getMetadata());
168 DwarfMacroInfoSection =
169 getContext().getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
170 SectionKind::getMetadata());
173 const MCSection *TargetLoweringObjectFileELF::getEHFrameSection() const {
174 return getContext().getELFSection(".eh_frame", ELF::SHT_PROGBITS,
176 SectionKind::getDataRel());
180 getELFKindForNamedSection(StringRef Name, SectionKind K) {
181 // FIXME: Why is this here? Codegen is should not be in the business
182 // of figuring section flags. If the user wrote section(".eh_frame"),
183 // we should just pass that to MC which will defer to the assembly
184 // or use its default if producing an object file.
185 if (Name.empty() || Name[0] != '.') return K;
187 // Some lame default implementation based on some magic section names.
188 if (Name == ".bss" ||
189 Name.startswith(".bss.") ||
190 Name.startswith(".gnu.linkonce.b.") ||
191 Name.startswith(".llvm.linkonce.b.") ||
193 Name.startswith(".sbss.") ||
194 Name.startswith(".gnu.linkonce.sb.") ||
195 Name.startswith(".llvm.linkonce.sb."))
196 return SectionKind::getBSS();
198 if (Name == ".tdata" ||
199 Name.startswith(".tdata.") ||
200 Name.startswith(".gnu.linkonce.td.") ||
201 Name.startswith(".llvm.linkonce.td."))
202 return SectionKind::getThreadData();
204 if (Name == ".tbss" ||
205 Name.startswith(".tbss.") ||
206 Name.startswith(".gnu.linkonce.tb.") ||
207 Name.startswith(".llvm.linkonce.tb."))
208 return SectionKind::getThreadBSS();
210 if (Name == ".eh_frame")
211 return SectionKind::getDataRel();
217 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
219 if (Name == ".init_array")
220 return ELF::SHT_INIT_ARRAY;
222 if (Name == ".fini_array")
223 return ELF::SHT_FINI_ARRAY;
225 if (Name == ".preinit_array")
226 return ELF::SHT_PREINIT_ARRAY;
228 if (K.isBSS() || K.isThreadBSS())
229 return ELF::SHT_NOBITS;
231 return ELF::SHT_PROGBITS;
236 getELFSectionFlags(SectionKind K) {
240 Flags |= ELF::SHF_ALLOC;
243 Flags |= ELF::SHF_EXECINSTR;
246 Flags |= ELF::SHF_WRITE;
248 if (K.isThreadLocal())
249 Flags |= ELF::SHF_TLS;
251 // K.isMergeableConst() is left out to honour PR4650
252 if (K.isMergeableCString() || K.isMergeableConst4() ||
253 K.isMergeableConst8() || K.isMergeableConst16())
254 Flags |= ELF::SHF_MERGE;
256 if (K.isMergeableCString())
257 Flags |= ELF::SHF_STRINGS;
263 const MCSection *TargetLoweringObjectFileELF::
264 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
265 Mangler *Mang, const TargetMachine &TM) const {
266 StringRef SectionName = GV->getSection();
268 // Infer section flags from the section name if we can.
269 Kind = getELFKindForNamedSection(SectionName, Kind);
271 return getContext().getELFSection(SectionName,
272 getELFSectionType(SectionName, Kind),
273 getELFSectionFlags(Kind), Kind);
276 /// getSectionPrefixForGlobal - Return the section prefix name used by options
277 /// FunctionsSections and DataSections.
278 static const char *getSectionPrefixForGlobal(SectionKind Kind) {
279 if (Kind.isText()) return ".text.";
280 if (Kind.isReadOnly()) return ".rodata.";
282 if (Kind.isThreadData()) return ".tdata.";
283 if (Kind.isThreadBSS()) return ".tbss.";
285 if (Kind.isDataNoRel()) return ".data.";
286 if (Kind.isDataRelLocal()) return ".data.rel.local.";
287 if (Kind.isDataRel()) return ".data.rel.";
288 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
290 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
291 return ".data.rel.ro.";
295 const MCSection *TargetLoweringObjectFileELF::
296 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
297 Mangler *Mang, const TargetMachine &TM) const {
298 // If we have -ffunction-section or -fdata-section then we should emit the
299 // global value to a uniqued section specifically for it.
300 bool EmitUniquedSection;
302 EmitUniquedSection = TM.getFunctionSections();
304 EmitUniquedSection = TM.getDataSections();
306 // If this global is linkonce/weak and the target handles this by emitting it
307 // into a 'uniqued' section name, create and return the section now.
308 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
309 !Kind.isCommon() && !Kind.isBSS()) {
311 Prefix = getSectionPrefixForGlobal(Kind);
313 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
314 MCSymbol *Sym = Mang->getSymbol(GV);
315 Name.append(Sym->getName().begin(), Sym->getName().end());
316 StringRef Group = "";
317 unsigned Flags = getELFSectionFlags(Kind);
318 if (GV->isWeakForLinker()) {
319 Group = Sym->getName();
320 Flags |= ELF::SHF_GROUP;
323 return getContext().getELFSection(Name.str(),
324 getELFSectionType(Name.str(), Kind),
325 Flags, Kind, 0, Group);
328 if (Kind.isText()) return TextSection;
330 if (Kind.isMergeable1ByteCString() ||
331 Kind.isMergeable2ByteCString() ||
332 Kind.isMergeable4ByteCString()) {
334 // We also need alignment here.
335 // FIXME: this is getting the alignment of the character, not the
336 // alignment of the global!
338 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
340 const char *SizeSpec = ".rodata.str1.";
341 if (Kind.isMergeable2ByteCString())
342 SizeSpec = ".rodata.str2.";
343 else if (Kind.isMergeable4ByteCString())
344 SizeSpec = ".rodata.str4.";
346 assert(Kind.isMergeable1ByteCString() && "unknown string width");
349 std::string Name = SizeSpec + utostr(Align);
350 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
357 if (Kind.isMergeableConst()) {
358 if (Kind.isMergeableConst4() && MergeableConst4Section)
359 return MergeableConst4Section;
360 if (Kind.isMergeableConst8() && MergeableConst8Section)
361 return MergeableConst8Section;
362 if (Kind.isMergeableConst16() && MergeableConst16Section)
363 return MergeableConst16Section;
364 return ReadOnlySection; // .const
367 if (Kind.isReadOnly()) return ReadOnlySection;
369 if (Kind.isThreadData()) return TLSDataSection;
370 if (Kind.isThreadBSS()) return TLSBSSSection;
372 // Note: we claim that common symbols are put in BSSSection, but they are
373 // really emitted with the magic .comm directive, which creates a symbol table
374 // entry but not a section.
375 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
377 if (Kind.isDataNoRel()) return DataSection;
378 if (Kind.isDataRelLocal()) return DataRelLocalSection;
379 if (Kind.isDataRel()) return DataRelSection;
380 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
382 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
383 return DataRelROSection;
386 /// getSectionForConstant - Given a mergeable constant with the
387 /// specified size and relocation information, return a section that it
388 /// should be placed in.
389 const MCSection *TargetLoweringObjectFileELF::
390 getSectionForConstant(SectionKind Kind) const {
391 if (Kind.isMergeableConst4() && MergeableConst4Section)
392 return MergeableConst4Section;
393 if (Kind.isMergeableConst8() && MergeableConst8Section)
394 return MergeableConst8Section;
395 if (Kind.isMergeableConst16() && MergeableConst16Section)
396 return MergeableConst16Section;
397 if (Kind.isReadOnly())
398 return ReadOnlySection;
400 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
401 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
402 return DataRelROSection;
405 const MCExpr *TargetLoweringObjectFileELF::
406 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
407 MachineModuleInfo *MMI,
408 unsigned Encoding, MCStreamer &Streamer) const {
410 if (Encoding & dwarf::DW_EH_PE_indirect) {
411 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
413 SmallString<128> Name;
414 Mang->getNameWithPrefix(Name, GV, true);
417 // Add information about the stub reference to ELFMMI so that the stub
418 // gets emitted by the asmprinter.
419 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
420 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
421 if (StubSym.getPointer() == 0) {
422 MCSymbol *Sym = Mang->getSymbol(GV);
423 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
426 return TargetLoweringObjectFile::
427 getExprForDwarfReference(SSym, Mang, MMI,
428 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
431 return TargetLoweringObjectFile::
432 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
435 //===----------------------------------------------------------------------===//
437 //===----------------------------------------------------------------------===//
439 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
440 const TargetMachine &TM) {
441 // _foo.eh symbols are currently always exported so that the linker knows
442 // about them. This is not necessary on 10.6 and later, but it
443 // doesn't hurt anything.
444 // FIXME: I need to get this from Triple.
445 IsFunctionEHSymbolGlobal = true;
446 IsFunctionEHFrameSymbolPrivate = false;
447 SupportsWeakOmittedEHFrame = false;
449 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
450 if (T.getOS() == Triple::Darwin) {
451 switch (T.getDarwinMajorNumber()) {
452 case 7: // 10.3 Panther.
453 case 8: // 10.4 Tiger.
454 CommDirectiveSupportsAlignment = false;
456 case 9: // 10.5 Leopard.
457 case 10: // 10.6 SnowLeopard.
462 TargetLoweringObjectFile::Initialize(Ctx, TM);
465 = getContext().getMachOSection("__TEXT", "__text",
466 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
467 SectionKind::getText());
469 = getContext().getMachOSection("__DATA", "__data", 0,
470 SectionKind::getDataRel());
472 TLSDataSection // .tdata
473 = getContext().getMachOSection("__DATA", "__thread_data",
474 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
475 SectionKind::getDataRel());
476 TLSBSSSection // .tbss
477 = getContext().getMachOSection("__DATA", "__thread_bss",
478 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
479 SectionKind::getThreadBSS());
481 // TODO: Verify datarel below.
482 TLSTLVSection // .tlv
483 = getContext().getMachOSection("__DATA", "__thread_vars",
484 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
485 SectionKind::getDataRel());
488 = getContext().getMachOSection("__DATA", "__thread_init",
489 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
490 SectionKind::getDataRel());
492 CStringSection // .cstring
493 = getContext().getMachOSection("__TEXT", "__cstring",
494 MCSectionMachO::S_CSTRING_LITERALS,
495 SectionKind::getMergeable1ByteCString());
497 = getContext().getMachOSection("__TEXT","__ustring", 0,
498 SectionKind::getMergeable2ByteCString());
499 FourByteConstantSection // .literal4
500 = getContext().getMachOSection("__TEXT", "__literal4",
501 MCSectionMachO::S_4BYTE_LITERALS,
502 SectionKind::getMergeableConst4());
503 EightByteConstantSection // .literal8
504 = getContext().getMachOSection("__TEXT", "__literal8",
505 MCSectionMachO::S_8BYTE_LITERALS,
506 SectionKind::getMergeableConst8());
508 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
509 // to using it in -static mode.
510 SixteenByteConstantSection = 0;
511 if (TM.getRelocationModel() != Reloc::Static &&
512 TM.getTargetData()->getPointerSize() == 32)
513 SixteenByteConstantSection = // .literal16
514 getContext().getMachOSection("__TEXT", "__literal16",
515 MCSectionMachO::S_16BYTE_LITERALS,
516 SectionKind::getMergeableConst16());
518 ReadOnlySection // .const
519 = getContext().getMachOSection("__TEXT", "__const", 0,
520 SectionKind::getReadOnly());
523 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
524 MCSectionMachO::S_COALESCED |
525 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
526 SectionKind::getText());
528 = getContext().getMachOSection("__TEXT", "__const_coal",
529 MCSectionMachO::S_COALESCED,
530 SectionKind::getReadOnly());
531 ConstDataSection // .const_data
532 = getContext().getMachOSection("__DATA", "__const", 0,
533 SectionKind::getReadOnlyWithRel());
535 = getContext().getMachOSection("__DATA","__datacoal_nt",
536 MCSectionMachO::S_COALESCED,
537 SectionKind::getDataRel());
539 = getContext().getMachOSection("__DATA","__common",
540 MCSectionMachO::S_ZEROFILL,
541 SectionKind::getBSS());
543 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
544 SectionKind::getBSS());
547 LazySymbolPointerSection
548 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
549 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
550 SectionKind::getMetadata());
551 NonLazySymbolPointerSection
552 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
553 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
554 SectionKind::getMetadata());
556 if (TM.getRelocationModel() == Reloc::Static) {
558 = getContext().getMachOSection("__TEXT", "__constructor", 0,
559 SectionKind::getDataRel());
561 = getContext().getMachOSection("__TEXT", "__destructor", 0,
562 SectionKind::getDataRel());
565 = getContext().getMachOSection("__DATA", "__mod_init_func",
566 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
567 SectionKind::getDataRel());
569 = getContext().getMachOSection("__DATA", "__mod_term_func",
570 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
571 SectionKind::getDataRel());
574 // Exception Handling.
575 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
576 SectionKind::getReadOnlyWithRel());
577 // Debug Information.
579 getContext().getMachOSection("__DWARF", "__debug_abbrev",
580 MCSectionMachO::S_ATTR_DEBUG,
581 SectionKind::getMetadata());
583 getContext().getMachOSection("__DWARF", "__debug_info",
584 MCSectionMachO::S_ATTR_DEBUG,
585 SectionKind::getMetadata());
587 getContext().getMachOSection("__DWARF", "__debug_line",
588 MCSectionMachO::S_ATTR_DEBUG,
589 SectionKind::getMetadata());
591 getContext().getMachOSection("__DWARF", "__debug_frame",
592 MCSectionMachO::S_ATTR_DEBUG,
593 SectionKind::getMetadata());
594 DwarfPubNamesSection =
595 getContext().getMachOSection("__DWARF", "__debug_pubnames",
596 MCSectionMachO::S_ATTR_DEBUG,
597 SectionKind::getMetadata());
598 DwarfPubTypesSection =
599 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
600 MCSectionMachO::S_ATTR_DEBUG,
601 SectionKind::getMetadata());
603 getContext().getMachOSection("__DWARF", "__debug_str",
604 MCSectionMachO::S_ATTR_DEBUG,
605 SectionKind::getMetadata());
607 getContext().getMachOSection("__DWARF", "__debug_loc",
608 MCSectionMachO::S_ATTR_DEBUG,
609 SectionKind::getMetadata());
610 DwarfARangesSection =
611 getContext().getMachOSection("__DWARF", "__debug_aranges",
612 MCSectionMachO::S_ATTR_DEBUG,
613 SectionKind::getMetadata());
615 getContext().getMachOSection("__DWARF", "__debug_ranges",
616 MCSectionMachO::S_ATTR_DEBUG,
617 SectionKind::getMetadata());
618 DwarfMacroInfoSection =
619 getContext().getMachOSection("__DWARF", "__debug_macinfo",
620 MCSectionMachO::S_ATTR_DEBUG,
621 SectionKind::getMetadata());
622 DwarfDebugInlineSection =
623 getContext().getMachOSection("__DWARF", "__debug_inlined",
624 MCSectionMachO::S_ATTR_DEBUG,
625 SectionKind::getMetadata());
627 TLSExtraDataSection = TLSTLVSection;
630 const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
631 return getContext().getMachOSection("__TEXT", "__eh_frame",
632 MCSectionMachO::S_COALESCED |
633 MCSectionMachO::S_ATTR_NO_TOC |
634 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
635 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
636 SectionKind::getReadOnly());
639 const MCSection *TargetLoweringObjectFileMachO::
640 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
641 Mangler *Mang, const TargetMachine &TM) const {
642 // Parse the section specifier and create it if valid.
643 StringRef Segment, Section;
644 unsigned TAA = (unsigned)MCSectionMachO::SECTION_ATTRIBUTES, StubSize = 0;
645 std::string ErrorCode =
646 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
648 if (!ErrorCode.empty()) {
649 // If invalid, report the error with report_fatal_error.
650 report_fatal_error("Global variable '" + GV->getNameStr() +
651 "' has an invalid section specifier '" + GV->getSection()+
652 "': " + ErrorCode + ".");
653 // Fall back to dropping it into the data section.
657 bool TAAWasSet = (TAA != MCSectionMachO::SECTION_ATTRIBUTES);
659 TAA = 0; // Sensible default if this is a new section.
662 const MCSectionMachO *S =
663 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
665 // If TAA wasn't set by ParseSectionSpecifier() above,
666 // use the value returned by getMachOSection() as a default.
668 TAA = S->getTypeAndAttributes();
670 // Okay, now that we got the section, verify that the TAA & StubSize agree.
671 // If the user declared multiple globals with different section flags, we need
672 // to reject it here.
673 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
674 // If invalid, report the error with report_fatal_error.
675 report_fatal_error("Global variable '" + GV->getNameStr() +
676 "' section type or attributes does not match previous"
677 " section specifier");
683 const MCSection *TargetLoweringObjectFileMachO::
684 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
685 Mangler *Mang, const TargetMachine &TM) const {
687 // Handle thread local data.
688 if (Kind.isThreadBSS()) return TLSBSSSection;
689 if (Kind.isThreadData()) return TLSDataSection;
692 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
694 // If this is weak/linkonce, put this in a coalescable section, either in text
695 // or data depending on if it is writable.
696 if (GV->isWeakForLinker()) {
697 if (Kind.isReadOnly())
698 return ConstTextCoalSection;
699 return DataCoalSection;
702 // FIXME: Alignment check should be handled by section classifier.
703 if (Kind.isMergeable1ByteCString() &&
704 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
705 return CStringSection;
707 // Do not put 16-bit arrays in the UString section if they have an
708 // externally visible label, this runs into issues with certain linker
710 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
711 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
712 return UStringSection;
714 if (Kind.isMergeableConst()) {
715 if (Kind.isMergeableConst4())
716 return FourByteConstantSection;
717 if (Kind.isMergeableConst8())
718 return EightByteConstantSection;
719 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
720 return SixteenByteConstantSection;
723 // Otherwise, if it is readonly, but not something we can specially optimize,
724 // just drop it in .const.
725 if (Kind.isReadOnly())
726 return ReadOnlySection;
728 // If this is marked const, put it into a const section. But if the dynamic
729 // linker needs to write to it, put it in the data segment.
730 if (Kind.isReadOnlyWithRel())
731 return ConstDataSection;
733 // Put zero initialized globals with strong external linkage in the
734 // DATA, __common section with the .zerofill directive.
735 if (Kind.isBSSExtern())
736 return DataCommonSection;
738 // Put zero initialized globals with local linkage in __DATA,__bss directive
739 // with the .zerofill directive (aka .lcomm).
740 if (Kind.isBSSLocal())
741 return DataBSSSection;
743 // Otherwise, just drop the variable in the normal data section.
748 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
749 // If this constant requires a relocation, we have to put it in the data
750 // segment, not in the text segment.
751 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
752 return ConstDataSection;
754 if (Kind.isMergeableConst4())
755 return FourByteConstantSection;
756 if (Kind.isMergeableConst8())
757 return EightByteConstantSection;
758 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
759 return SixteenByteConstantSection;
760 return ReadOnlySection; // .const
763 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
764 /// not to emit the UsedDirective for some symbols in llvm.used.
765 // FIXME: REMOVE this (rdar://7071300)
766 bool TargetLoweringObjectFileMachO::
767 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
768 /// On Darwin, internally linked data beginning with "L" or "l" does not have
769 /// the directive emitted (this occurs in ObjC metadata).
770 if (!GV) return false;
772 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
773 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
774 // FIXME: ObjC metadata is currently emitted as internal symbols that have
775 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
776 // this horrible hack can go away.
777 MCSymbol *Sym = Mang->getSymbol(GV);
778 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
785 const MCExpr *TargetLoweringObjectFileMachO::
786 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
787 MachineModuleInfo *MMI, unsigned Encoding,
788 MCStreamer &Streamer) const {
789 // The mach-o version of this method defaults to returning a stub reference.
791 if (Encoding & DW_EH_PE_indirect) {
792 MachineModuleInfoMachO &MachOMMI =
793 MMI->getObjFileInfo<MachineModuleInfoMachO>();
795 SmallString<128> Name;
796 Mang->getNameWithPrefix(Name, GV, true);
797 Name += "$non_lazy_ptr";
799 // Add information about the stub reference to MachOMMI so that the stub
800 // gets emitted by the asmprinter.
801 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
802 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
803 if (StubSym.getPointer() == 0) {
804 MCSymbol *Sym = Mang->getSymbol(GV);
805 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
808 return TargetLoweringObjectFile::
809 getExprForDwarfReference(SSym, Mang, MMI,
810 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
813 return TargetLoweringObjectFile::
814 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
817 unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
818 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
821 unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
822 return DW_EH_PE_pcrel;
825 unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
826 return DW_EH_PE_pcrel;
829 unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
830 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
833 //===----------------------------------------------------------------------===//
835 //===----------------------------------------------------------------------===//
837 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
838 const TargetMachine &TM) {
839 TargetLoweringObjectFile::Initialize(Ctx, TM);
841 getContext().getCOFFSection(".text",
842 COFF::IMAGE_SCN_CNT_CODE |
843 COFF::IMAGE_SCN_MEM_EXECUTE |
844 COFF::IMAGE_SCN_MEM_READ,
845 SectionKind::getText());
847 getContext().getCOFFSection(".data",
848 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
849 COFF::IMAGE_SCN_MEM_READ |
850 COFF::IMAGE_SCN_MEM_WRITE,
851 SectionKind::getDataRel());
853 getContext().getCOFFSection(".rdata",
854 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
855 COFF::IMAGE_SCN_MEM_READ,
856 SectionKind::getReadOnly());
858 getContext().getCOFFSection(".ctors",
859 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
860 COFF::IMAGE_SCN_MEM_READ |
861 COFF::IMAGE_SCN_MEM_WRITE,
862 SectionKind::getDataRel());
864 getContext().getCOFFSection(".dtors",
865 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
866 COFF::IMAGE_SCN_MEM_READ |
867 COFF::IMAGE_SCN_MEM_WRITE,
868 SectionKind::getDataRel());
870 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
871 // though it contains relocatable pointers. In PIC mode, this is probably a
872 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
873 // adjusted or this should be a data section.
875 getContext().getCOFFSection(".gcc_except_table",
876 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
877 COFF::IMAGE_SCN_MEM_READ,
878 SectionKind::getReadOnly());
881 getContext().getCOFFSection(".debug_abbrev",
882 COFF::IMAGE_SCN_MEM_DISCARDABLE |
883 COFF::IMAGE_SCN_MEM_READ,
884 SectionKind::getMetadata());
886 getContext().getCOFFSection(".debug_info",
887 COFF::IMAGE_SCN_MEM_DISCARDABLE |
888 COFF::IMAGE_SCN_MEM_READ,
889 SectionKind::getMetadata());
891 getContext().getCOFFSection(".debug_line",
892 COFF::IMAGE_SCN_MEM_DISCARDABLE |
893 COFF::IMAGE_SCN_MEM_READ,
894 SectionKind::getMetadata());
896 getContext().getCOFFSection(".debug_frame",
897 COFF::IMAGE_SCN_MEM_DISCARDABLE |
898 COFF::IMAGE_SCN_MEM_READ,
899 SectionKind::getMetadata());
900 DwarfPubNamesSection =
901 getContext().getCOFFSection(".debug_pubnames",
902 COFF::IMAGE_SCN_MEM_DISCARDABLE |
903 COFF::IMAGE_SCN_MEM_READ,
904 SectionKind::getMetadata());
905 DwarfPubTypesSection =
906 getContext().getCOFFSection(".debug_pubtypes",
907 COFF::IMAGE_SCN_MEM_DISCARDABLE |
908 COFF::IMAGE_SCN_MEM_READ,
909 SectionKind::getMetadata());
911 getContext().getCOFFSection(".debug_str",
912 COFF::IMAGE_SCN_MEM_DISCARDABLE |
913 COFF::IMAGE_SCN_MEM_READ,
914 SectionKind::getMetadata());
916 getContext().getCOFFSection(".debug_loc",
917 COFF::IMAGE_SCN_MEM_DISCARDABLE |
918 COFF::IMAGE_SCN_MEM_READ,
919 SectionKind::getMetadata());
920 DwarfARangesSection =
921 getContext().getCOFFSection(".debug_aranges",
922 COFF::IMAGE_SCN_MEM_DISCARDABLE |
923 COFF::IMAGE_SCN_MEM_READ,
924 SectionKind::getMetadata());
926 getContext().getCOFFSection(".debug_ranges",
927 COFF::IMAGE_SCN_MEM_DISCARDABLE |
928 COFF::IMAGE_SCN_MEM_READ,
929 SectionKind::getMetadata());
930 DwarfMacroInfoSection =
931 getContext().getCOFFSection(".debug_macinfo",
932 COFF::IMAGE_SCN_MEM_DISCARDABLE |
933 COFF::IMAGE_SCN_MEM_READ,
934 SectionKind::getMetadata());
937 getContext().getCOFFSection(".drectve",
938 COFF::IMAGE_SCN_LNK_INFO,
939 SectionKind::getMetadata());
942 const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
943 return getContext().getCOFFSection(".eh_frame",
944 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
945 COFF::IMAGE_SCN_MEM_READ |
946 COFF::IMAGE_SCN_MEM_WRITE,
947 SectionKind::getDataRel());
952 getCOFFSectionFlags(SectionKind K) {
957 COFF::IMAGE_SCN_MEM_DISCARDABLE;
960 COFF::IMAGE_SCN_MEM_EXECUTE |
961 COFF::IMAGE_SCN_MEM_READ |
962 COFF::IMAGE_SCN_CNT_CODE;
965 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
966 COFF::IMAGE_SCN_MEM_READ |
967 COFF::IMAGE_SCN_MEM_WRITE;
968 else if (K.isReadOnly())
970 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
971 COFF::IMAGE_SCN_MEM_READ;
972 else if (K.isWriteable())
974 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
975 COFF::IMAGE_SCN_MEM_READ |
976 COFF::IMAGE_SCN_MEM_WRITE;
981 const MCSection *TargetLoweringObjectFileCOFF::
982 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
983 Mangler *Mang, const TargetMachine &TM) const {
984 return getContext().getCOFFSection(GV->getSection(),
985 getCOFFSectionFlags(Kind),
989 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
994 if (Kind.isWriteable())
1000 const MCSection *TargetLoweringObjectFileCOFF::
1001 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1002 Mangler *Mang, const TargetMachine &TM) const {
1003 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1005 // If this global is linkonce/weak and the target handles this by emitting it
1006 // into a 'uniqued' section name, create and return the section now.
1007 if (GV->isWeakForLinker()) {
1008 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1009 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
1010 MCSymbol *Sym = Mang->getSymbol(GV);
1011 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
1013 unsigned Characteristics = getCOFFSectionFlags(Kind);
1015 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1017 return getContext().getCOFFSection(Name.str(), Characteristics,
1018 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
1022 return getTextSection();
1024 return getDataSection();