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 if (Name.empty() || Name[0] != '.') return K;
183 // Some lame default implementation based on some magic section names.
184 if (Name == ".bss" ||
185 Name.startswith(".bss.") ||
186 Name.startswith(".gnu.linkonce.b.") ||
187 Name.startswith(".llvm.linkonce.b.") ||
189 Name.startswith(".sbss.") ||
190 Name.startswith(".gnu.linkonce.sb.") ||
191 Name.startswith(".llvm.linkonce.sb."))
192 return SectionKind::getBSS();
194 if (Name == ".tdata" ||
195 Name.startswith(".tdata.") ||
196 Name.startswith(".gnu.linkonce.td.") ||
197 Name.startswith(".llvm.linkonce.td."))
198 return SectionKind::getThreadData();
200 if (Name == ".tbss" ||
201 Name.startswith(".tbss.") ||
202 Name.startswith(".gnu.linkonce.tb.") ||
203 Name.startswith(".llvm.linkonce.tb."))
204 return SectionKind::getThreadBSS();
210 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
212 if (Name == ".init_array")
213 return ELF::SHT_INIT_ARRAY;
215 if (Name == ".fini_array")
216 return ELF::SHT_FINI_ARRAY;
218 if (Name == ".preinit_array")
219 return ELF::SHT_PREINIT_ARRAY;
221 if (K.isBSS() || K.isThreadBSS())
222 return ELF::SHT_NOBITS;
224 return ELF::SHT_PROGBITS;
229 getELFSectionFlags(SectionKind K) {
233 Flags |= ELF::SHF_ALLOC;
236 Flags |= ELF::SHF_EXECINSTR;
239 Flags |= ELF::SHF_WRITE;
241 if (K.isThreadLocal())
242 Flags |= ELF::SHF_TLS;
244 // K.isMergeableConst() is left out to honour PR4650
245 if (K.isMergeableCString() || K.isMergeableConst4() ||
246 K.isMergeableConst8() || K.isMergeableConst16())
247 Flags |= ELF::SHF_MERGE;
249 if (K.isMergeableCString())
250 Flags |= ELF::SHF_STRINGS;
256 const MCSection *TargetLoweringObjectFileELF::
257 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
258 Mangler *Mang, const TargetMachine &TM) const {
259 StringRef SectionName = GV->getSection();
261 // Infer section flags from the section name if we can.
262 Kind = getELFKindForNamedSection(SectionName, Kind);
264 return getContext().getELFSection(SectionName,
265 getELFSectionType(SectionName, Kind),
266 getELFSectionFlags(Kind), Kind);
269 /// getSectionPrefixForGlobal - Return the section prefix name used by options
270 /// FunctionsSections and DataSections.
271 static const char *getSectionPrefixForGlobal(SectionKind Kind) {
272 if (Kind.isText()) return ".text.";
273 if (Kind.isReadOnly()) return ".rodata.";
275 if (Kind.isThreadData()) return ".tdata.";
276 if (Kind.isThreadBSS()) return ".tbss.";
278 if (Kind.isDataNoRel()) return ".data.";
279 if (Kind.isDataRelLocal()) return ".data.rel.local.";
280 if (Kind.isDataRel()) return ".data.rel.";
281 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
283 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
284 return ".data.rel.ro.";
288 const MCSection *TargetLoweringObjectFileELF::
289 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
290 Mangler *Mang, const TargetMachine &TM) const {
291 // If we have -ffunction-section or -fdata-section then we should emit the
292 // global value to a uniqued section specifically for it.
293 bool EmitUniquedSection;
295 EmitUniquedSection = TM.getFunctionSections();
297 EmitUniquedSection = TM.getDataSections();
299 // If this global is linkonce/weak and the target handles this by emitting it
300 // into a 'uniqued' section name, create and return the section now.
301 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
302 !Kind.isCommon() && !Kind.isBSS()) {
304 Prefix = getSectionPrefixForGlobal(Kind);
306 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
307 MCSymbol *Sym = Mang->getSymbol(GV);
308 Name.append(Sym->getName().begin(), Sym->getName().end());
309 StringRef Group = "";
310 unsigned Flags = getELFSectionFlags(Kind);
311 if (GV->isWeakForLinker()) {
312 Group = Sym->getName();
313 Flags |= ELF::SHF_GROUP;
316 return getContext().getELFSection(Name.str(),
317 getELFSectionType(Name.str(), Kind),
318 Flags, Kind, 0, Group);
321 if (Kind.isText()) return TextSection;
323 if (Kind.isMergeable1ByteCString() ||
324 Kind.isMergeable2ByteCString() ||
325 Kind.isMergeable4ByteCString()) {
327 // We also need alignment here.
328 // FIXME: this is getting the alignment of the character, not the
329 // alignment of the global!
331 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
333 const char *SizeSpec = ".rodata.str1.";
334 if (Kind.isMergeable2ByteCString())
335 SizeSpec = ".rodata.str2.";
336 else if (Kind.isMergeable4ByteCString())
337 SizeSpec = ".rodata.str4.";
339 assert(Kind.isMergeable1ByteCString() && "unknown string width");
342 std::string Name = SizeSpec + utostr(Align);
343 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
350 if (Kind.isMergeableConst()) {
351 if (Kind.isMergeableConst4() && MergeableConst4Section)
352 return MergeableConst4Section;
353 if (Kind.isMergeableConst8() && MergeableConst8Section)
354 return MergeableConst8Section;
355 if (Kind.isMergeableConst16() && MergeableConst16Section)
356 return MergeableConst16Section;
357 return ReadOnlySection; // .const
360 if (Kind.isReadOnly()) return ReadOnlySection;
362 if (Kind.isThreadData()) return TLSDataSection;
363 if (Kind.isThreadBSS()) return TLSBSSSection;
365 // Note: we claim that common symbols are put in BSSSection, but they are
366 // really emitted with the magic .comm directive, which creates a symbol table
367 // entry but not a section.
368 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
370 if (Kind.isDataNoRel()) return DataSection;
371 if (Kind.isDataRelLocal()) return DataRelLocalSection;
372 if (Kind.isDataRel()) return DataRelSection;
373 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
375 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
376 return DataRelROSection;
379 /// getSectionForConstant - Given a mergeable constant with the
380 /// specified size and relocation information, return a section that it
381 /// should be placed in.
382 const MCSection *TargetLoweringObjectFileELF::
383 getSectionForConstant(SectionKind Kind) const {
384 if (Kind.isMergeableConst4() && MergeableConst4Section)
385 return MergeableConst4Section;
386 if (Kind.isMergeableConst8() && MergeableConst8Section)
387 return MergeableConst8Section;
388 if (Kind.isMergeableConst16() && MergeableConst16Section)
389 return MergeableConst16Section;
390 if (Kind.isReadOnly())
391 return ReadOnlySection;
393 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
394 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
395 return DataRelROSection;
398 const MCExpr *TargetLoweringObjectFileELF::
399 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
400 MachineModuleInfo *MMI,
401 unsigned Encoding, MCStreamer &Streamer) const {
403 if (Encoding & dwarf::DW_EH_PE_indirect) {
404 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
406 SmallString<128> Name;
407 Mang->getNameWithPrefix(Name, GV, true);
410 // Add information about the stub reference to ELFMMI so that the stub
411 // gets emitted by the asmprinter.
412 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
413 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
414 if (StubSym.getPointer() == 0) {
415 MCSymbol *Sym = Mang->getSymbol(GV);
416 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
419 return TargetLoweringObjectFile::
420 getExprForDwarfReference(SSym, Mang, MMI,
421 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
424 return TargetLoweringObjectFile::
425 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
428 //===----------------------------------------------------------------------===//
430 //===----------------------------------------------------------------------===//
432 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
433 const TargetMachine &TM) {
434 // _foo.eh symbols are currently always exported so that the linker knows
435 // about them. This is not necessary on 10.6 and later, but it
436 // doesn't hurt anything.
437 // FIXME: I need to get this from Triple.
438 IsFunctionEHSymbolGlobal = true;
439 IsFunctionEHFrameSymbolPrivate = false;
440 SupportsWeakOmittedEHFrame = false;
442 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
443 if (T.getOS() == Triple::Darwin) {
444 unsigned MajNum = T.getDarwinMajorNumber();
445 if (MajNum == 7 || MajNum == 8) // 10.3 Panther, 10.4 Tiger
446 CommDirectiveSupportsAlignment = false;
447 if (MajNum > 9) // 10.6 SnowLeopard
448 IsFunctionEHSymbolGlobal = false;
451 TargetLoweringObjectFile::Initialize(Ctx, TM);
454 = getContext().getMachOSection("__TEXT", "__text",
455 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
456 SectionKind::getText());
458 = getContext().getMachOSection("__DATA", "__data", 0,
459 SectionKind::getDataRel());
461 TLSDataSection // .tdata
462 = getContext().getMachOSection("__DATA", "__thread_data",
463 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
464 SectionKind::getDataRel());
465 TLSBSSSection // .tbss
466 = getContext().getMachOSection("__DATA", "__thread_bss",
467 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
468 SectionKind::getThreadBSS());
470 // TODO: Verify datarel below.
471 TLSTLVSection // .tlv
472 = getContext().getMachOSection("__DATA", "__thread_vars",
473 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
474 SectionKind::getDataRel());
477 = getContext().getMachOSection("__DATA", "__thread_init",
478 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
479 SectionKind::getDataRel());
481 CStringSection // .cstring
482 = getContext().getMachOSection("__TEXT", "__cstring",
483 MCSectionMachO::S_CSTRING_LITERALS,
484 SectionKind::getMergeable1ByteCString());
486 = getContext().getMachOSection("__TEXT","__ustring", 0,
487 SectionKind::getMergeable2ByteCString());
488 FourByteConstantSection // .literal4
489 = getContext().getMachOSection("__TEXT", "__literal4",
490 MCSectionMachO::S_4BYTE_LITERALS,
491 SectionKind::getMergeableConst4());
492 EightByteConstantSection // .literal8
493 = getContext().getMachOSection("__TEXT", "__literal8",
494 MCSectionMachO::S_8BYTE_LITERALS,
495 SectionKind::getMergeableConst8());
497 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
498 // to using it in -static mode.
499 SixteenByteConstantSection = 0;
500 if (TM.getRelocationModel() != Reloc::Static &&
501 TM.getTargetData()->getPointerSize() == 32)
502 SixteenByteConstantSection = // .literal16
503 getContext().getMachOSection("__TEXT", "__literal16",
504 MCSectionMachO::S_16BYTE_LITERALS,
505 SectionKind::getMergeableConst16());
507 ReadOnlySection // .const
508 = getContext().getMachOSection("__TEXT", "__const", 0,
509 SectionKind::getReadOnly());
512 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
513 MCSectionMachO::S_COALESCED |
514 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
515 SectionKind::getText());
517 = getContext().getMachOSection("__TEXT", "__const_coal",
518 MCSectionMachO::S_COALESCED,
519 SectionKind::getReadOnly());
520 ConstDataSection // .const_data
521 = getContext().getMachOSection("__DATA", "__const", 0,
522 SectionKind::getReadOnlyWithRel());
524 = getContext().getMachOSection("__DATA","__datacoal_nt",
525 MCSectionMachO::S_COALESCED,
526 SectionKind::getDataRel());
528 = getContext().getMachOSection("__DATA","__common",
529 MCSectionMachO::S_ZEROFILL,
530 SectionKind::getBSS());
532 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
533 SectionKind::getBSS());
536 LazySymbolPointerSection
537 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
538 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
539 SectionKind::getMetadata());
540 NonLazySymbolPointerSection
541 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
542 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
543 SectionKind::getMetadata());
545 if (TM.getRelocationModel() == Reloc::Static) {
547 = getContext().getMachOSection("__TEXT", "__constructor", 0,
548 SectionKind::getDataRel());
550 = getContext().getMachOSection("__TEXT", "__destructor", 0,
551 SectionKind::getDataRel());
554 = getContext().getMachOSection("__DATA", "__mod_init_func",
555 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
556 SectionKind::getDataRel());
558 = getContext().getMachOSection("__DATA", "__mod_term_func",
559 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
560 SectionKind::getDataRel());
563 // Exception Handling.
564 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
565 SectionKind::getReadOnlyWithRel());
566 // Debug Information.
568 getContext().getMachOSection("__DWARF", "__debug_abbrev",
569 MCSectionMachO::S_ATTR_DEBUG,
570 SectionKind::getMetadata());
572 getContext().getMachOSection("__DWARF", "__debug_info",
573 MCSectionMachO::S_ATTR_DEBUG,
574 SectionKind::getMetadata());
576 getContext().getMachOSection("__DWARF", "__debug_line",
577 MCSectionMachO::S_ATTR_DEBUG,
578 SectionKind::getMetadata());
580 getContext().getMachOSection("__DWARF", "__debug_frame",
581 MCSectionMachO::S_ATTR_DEBUG,
582 SectionKind::getMetadata());
583 DwarfPubNamesSection =
584 getContext().getMachOSection("__DWARF", "__debug_pubnames",
585 MCSectionMachO::S_ATTR_DEBUG,
586 SectionKind::getMetadata());
587 DwarfPubTypesSection =
588 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
589 MCSectionMachO::S_ATTR_DEBUG,
590 SectionKind::getMetadata());
592 getContext().getMachOSection("__DWARF", "__debug_str",
593 MCSectionMachO::S_ATTR_DEBUG,
594 SectionKind::getMetadata());
596 getContext().getMachOSection("__DWARF", "__debug_loc",
597 MCSectionMachO::S_ATTR_DEBUG,
598 SectionKind::getMetadata());
599 DwarfARangesSection =
600 getContext().getMachOSection("__DWARF", "__debug_aranges",
601 MCSectionMachO::S_ATTR_DEBUG,
602 SectionKind::getMetadata());
604 getContext().getMachOSection("__DWARF", "__debug_ranges",
605 MCSectionMachO::S_ATTR_DEBUG,
606 SectionKind::getMetadata());
607 DwarfMacroInfoSection =
608 getContext().getMachOSection("__DWARF", "__debug_macinfo",
609 MCSectionMachO::S_ATTR_DEBUG,
610 SectionKind::getMetadata());
611 DwarfDebugInlineSection =
612 getContext().getMachOSection("__DWARF", "__debug_inlined",
613 MCSectionMachO::S_ATTR_DEBUG,
614 SectionKind::getMetadata());
616 TLSExtraDataSection = TLSTLVSection;
619 const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
620 return getContext().getMachOSection("__TEXT", "__eh_frame",
621 MCSectionMachO::S_COALESCED |
622 MCSectionMachO::S_ATTR_NO_TOC |
623 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
624 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
625 SectionKind::getReadOnly());
628 const MCSection *TargetLoweringObjectFileMachO::
629 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
630 Mangler *Mang, const TargetMachine &TM) const {
631 // Parse the section specifier and create it if valid.
632 StringRef Segment, Section;
633 unsigned TAA = (unsigned)MCSectionMachO::SECTION_ATTRIBUTES, StubSize = 0;
634 std::string ErrorCode =
635 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
637 if (!ErrorCode.empty()) {
638 // If invalid, report the error with report_fatal_error.
639 report_fatal_error("Global variable '" + GV->getNameStr() +
640 "' has an invalid section specifier '" + GV->getSection()+
641 "': " + ErrorCode + ".");
642 // Fall back to dropping it into the data section.
646 bool TAAWasSet = (TAA != MCSectionMachO::SECTION_ATTRIBUTES);
648 TAA = 0; // Sensible default if this is a new section.
651 const MCSectionMachO *S =
652 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
654 // If TAA wasn't set by ParseSectionSpecifier() above,
655 // use the value returned by getMachOSection() as a default.
657 TAA = S->getTypeAndAttributes();
659 // Okay, now that we got the section, verify that the TAA & StubSize agree.
660 // If the user declared multiple globals with different section flags, we need
661 // to reject it here.
662 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
663 // If invalid, report the error with report_fatal_error.
664 report_fatal_error("Global variable '" + GV->getNameStr() +
665 "' section type or attributes does not match previous"
666 " section specifier");
672 const MCSection *TargetLoweringObjectFileMachO::
673 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
674 Mangler *Mang, const TargetMachine &TM) const {
676 // Handle thread local data.
677 if (Kind.isThreadBSS()) return TLSBSSSection;
678 if (Kind.isThreadData()) return TLSDataSection;
681 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
683 // If this is weak/linkonce, put this in a coalescable section, either in text
684 // or data depending on if it is writable.
685 if (GV->isWeakForLinker()) {
686 if (Kind.isReadOnly())
687 return ConstTextCoalSection;
688 return DataCoalSection;
691 // FIXME: Alignment check should be handled by section classifier.
692 if (Kind.isMergeable1ByteCString() &&
693 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
694 return CStringSection;
696 // Do not put 16-bit arrays in the UString section if they have an
697 // externally visible label, this runs into issues with certain linker
699 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
700 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
701 return UStringSection;
703 if (Kind.isMergeableConst()) {
704 if (Kind.isMergeableConst4())
705 return FourByteConstantSection;
706 if (Kind.isMergeableConst8())
707 return EightByteConstantSection;
708 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
709 return SixteenByteConstantSection;
712 // Otherwise, if it is readonly, but not something we can specially optimize,
713 // just drop it in .const.
714 if (Kind.isReadOnly())
715 return ReadOnlySection;
717 // If this is marked const, put it into a const section. But if the dynamic
718 // linker needs to write to it, put it in the data segment.
719 if (Kind.isReadOnlyWithRel())
720 return ConstDataSection;
722 // Put zero initialized globals with strong external linkage in the
723 // DATA, __common section with the .zerofill directive.
724 if (Kind.isBSSExtern())
725 return DataCommonSection;
727 // Put zero initialized globals with local linkage in __DATA,__bss directive
728 // with the .zerofill directive (aka .lcomm).
729 if (Kind.isBSSLocal())
730 return DataBSSSection;
732 // Otherwise, just drop the variable in the normal data section.
737 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
738 // If this constant requires a relocation, we have to put it in the data
739 // segment, not in the text segment.
740 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
741 return ConstDataSection;
743 if (Kind.isMergeableConst4())
744 return FourByteConstantSection;
745 if (Kind.isMergeableConst8())
746 return EightByteConstantSection;
747 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
748 return SixteenByteConstantSection;
749 return ReadOnlySection; // .const
752 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
753 /// not to emit the UsedDirective for some symbols in llvm.used.
754 // FIXME: REMOVE this (rdar://7071300)
755 bool TargetLoweringObjectFileMachO::
756 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
757 /// On Darwin, internally linked data beginning with "L" or "l" does not have
758 /// the directive emitted (this occurs in ObjC metadata).
759 if (!GV) return false;
761 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
762 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
763 // FIXME: ObjC metadata is currently emitted as internal symbols that have
764 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
765 // this horrible hack can go away.
766 MCSymbol *Sym = Mang->getSymbol(GV);
767 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
774 const MCExpr *TargetLoweringObjectFileMachO::
775 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
776 MachineModuleInfo *MMI, unsigned Encoding,
777 MCStreamer &Streamer) const {
778 // The mach-o version of this method defaults to returning a stub reference.
780 if (Encoding & DW_EH_PE_indirect) {
781 MachineModuleInfoMachO &MachOMMI =
782 MMI->getObjFileInfo<MachineModuleInfoMachO>();
784 SmallString<128> Name;
785 Mang->getNameWithPrefix(Name, GV, true);
786 Name += "$non_lazy_ptr";
788 // Add information about the stub reference to MachOMMI so that the stub
789 // gets emitted by the asmprinter.
790 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
791 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
792 if (StubSym.getPointer() == 0) {
793 MCSymbol *Sym = Mang->getSymbol(GV);
794 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
797 return TargetLoweringObjectFile::
798 getExprForDwarfReference(SSym, Mang, MMI,
799 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
802 return TargetLoweringObjectFile::
803 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
806 unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
807 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
810 unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
811 return DW_EH_PE_pcrel;
814 unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
815 return DW_EH_PE_pcrel;
818 unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
819 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
822 //===----------------------------------------------------------------------===//
824 //===----------------------------------------------------------------------===//
826 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
827 const TargetMachine &TM) {
828 TargetLoweringObjectFile::Initialize(Ctx, TM);
830 getContext().getCOFFSection(".text",
831 COFF::IMAGE_SCN_CNT_CODE |
832 COFF::IMAGE_SCN_MEM_EXECUTE |
833 COFF::IMAGE_SCN_MEM_READ,
834 SectionKind::getText());
836 getContext().getCOFFSection(".data",
837 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
838 COFF::IMAGE_SCN_MEM_READ |
839 COFF::IMAGE_SCN_MEM_WRITE,
840 SectionKind::getDataRel());
842 getContext().getCOFFSection(".rdata",
843 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
844 COFF::IMAGE_SCN_MEM_READ,
845 SectionKind::getReadOnly());
847 getContext().getCOFFSection(".ctors",
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(".dtors",
854 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
855 COFF::IMAGE_SCN_MEM_READ |
856 COFF::IMAGE_SCN_MEM_WRITE,
857 SectionKind::getDataRel());
859 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
860 // though it contains relocatable pointers. In PIC mode, this is probably a
861 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
862 // adjusted or this should be a data section.
864 getContext().getCOFFSection(".gcc_except_table",
865 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
866 COFF::IMAGE_SCN_MEM_READ,
867 SectionKind::getReadOnly());
870 getContext().getCOFFSection(".debug_abbrev",
871 COFF::IMAGE_SCN_MEM_DISCARDABLE |
872 COFF::IMAGE_SCN_MEM_READ,
873 SectionKind::getMetadata());
875 getContext().getCOFFSection(".debug_info",
876 COFF::IMAGE_SCN_MEM_DISCARDABLE |
877 COFF::IMAGE_SCN_MEM_READ,
878 SectionKind::getMetadata());
880 getContext().getCOFFSection(".debug_line",
881 COFF::IMAGE_SCN_MEM_DISCARDABLE |
882 COFF::IMAGE_SCN_MEM_READ,
883 SectionKind::getMetadata());
885 getContext().getCOFFSection(".debug_frame",
886 COFF::IMAGE_SCN_MEM_DISCARDABLE |
887 COFF::IMAGE_SCN_MEM_READ,
888 SectionKind::getMetadata());
889 DwarfPubNamesSection =
890 getContext().getCOFFSection(".debug_pubnames",
891 COFF::IMAGE_SCN_MEM_DISCARDABLE |
892 COFF::IMAGE_SCN_MEM_READ,
893 SectionKind::getMetadata());
894 DwarfPubTypesSection =
895 getContext().getCOFFSection(".debug_pubtypes",
896 COFF::IMAGE_SCN_MEM_DISCARDABLE |
897 COFF::IMAGE_SCN_MEM_READ,
898 SectionKind::getMetadata());
900 getContext().getCOFFSection(".debug_str",
901 COFF::IMAGE_SCN_MEM_DISCARDABLE |
902 COFF::IMAGE_SCN_MEM_READ,
903 SectionKind::getMetadata());
905 getContext().getCOFFSection(".debug_loc",
906 COFF::IMAGE_SCN_MEM_DISCARDABLE |
907 COFF::IMAGE_SCN_MEM_READ,
908 SectionKind::getMetadata());
909 DwarfARangesSection =
910 getContext().getCOFFSection(".debug_aranges",
911 COFF::IMAGE_SCN_MEM_DISCARDABLE |
912 COFF::IMAGE_SCN_MEM_READ,
913 SectionKind::getMetadata());
915 getContext().getCOFFSection(".debug_ranges",
916 COFF::IMAGE_SCN_MEM_DISCARDABLE |
917 COFF::IMAGE_SCN_MEM_READ,
918 SectionKind::getMetadata());
919 DwarfMacroInfoSection =
920 getContext().getCOFFSection(".debug_macinfo",
921 COFF::IMAGE_SCN_MEM_DISCARDABLE |
922 COFF::IMAGE_SCN_MEM_READ,
923 SectionKind::getMetadata());
926 getContext().getCOFFSection(".drectve",
927 COFF::IMAGE_SCN_LNK_INFO,
928 SectionKind::getMetadata());
931 const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
932 return getContext().getCOFFSection(".eh_frame",
933 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
934 COFF::IMAGE_SCN_MEM_READ |
935 COFF::IMAGE_SCN_MEM_WRITE,
936 SectionKind::getDataRel());
941 getCOFFSectionFlags(SectionKind K) {
946 COFF::IMAGE_SCN_MEM_DISCARDABLE;
949 COFF::IMAGE_SCN_MEM_EXECUTE |
950 COFF::IMAGE_SCN_MEM_READ |
951 COFF::IMAGE_SCN_CNT_CODE;
954 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
955 COFF::IMAGE_SCN_MEM_READ |
956 COFF::IMAGE_SCN_MEM_WRITE;
957 else if (K.isReadOnly())
959 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
960 COFF::IMAGE_SCN_MEM_READ;
961 else if (K.isWriteable())
963 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
964 COFF::IMAGE_SCN_MEM_READ |
965 COFF::IMAGE_SCN_MEM_WRITE;
970 const MCSection *TargetLoweringObjectFileCOFF::
971 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
972 Mangler *Mang, const TargetMachine &TM) const {
973 return getContext().getCOFFSection(GV->getSection(),
974 getCOFFSectionFlags(Kind),
978 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
983 if (Kind.isWriteable())
989 const MCSection *TargetLoweringObjectFileCOFF::
990 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
991 Mangler *Mang, const TargetMachine &TM) const {
992 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
994 // If this global is linkonce/weak and the target handles this by emitting it
995 // into a 'uniqued' section name, create and return the section now.
996 if (GV->isWeakForLinker()) {
997 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
998 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
999 MCSymbol *Sym = Mang->getSymbol(GV);
1000 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
1002 unsigned Characteristics = getCOFFSectionFlags(Kind);
1004 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1006 return getContext().getCOFFSection(Name.str(), Characteristics,
1007 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
1011 return getTextSection();
1013 return getDataSection();