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, StubSize;
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.
647 const MCSectionMachO *S =
648 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
650 // Okay, now that we got the section, verify that the TAA & StubSize agree.
651 // If the user declared multiple globals with different section flags, we need
652 // to reject it here.
653 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
654 // If invalid, report the error with report_fatal_error.
655 report_fatal_error("Global variable '" + GV->getNameStr() +
656 "' section type or attributes does not match previous"
657 " section specifier");
663 const MCSection *TargetLoweringObjectFileMachO::
664 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
665 Mangler *Mang, const TargetMachine &TM) const {
667 // Handle thread local data.
668 if (Kind.isThreadBSS()) return TLSBSSSection;
669 if (Kind.isThreadData()) return TLSDataSection;
672 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
674 // If this is weak/linkonce, put this in a coalescable section, either in text
675 // or data depending on if it is writable.
676 if (GV->isWeakForLinker()) {
677 if (Kind.isReadOnly())
678 return ConstTextCoalSection;
679 return DataCoalSection;
682 // FIXME: Alignment check should be handled by section classifier.
683 if (Kind.isMergeable1ByteCString() &&
684 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
685 return CStringSection;
687 // Do not put 16-bit arrays in the UString section if they have an
688 // externally visible label, this runs into issues with certain linker
690 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
691 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
692 return UStringSection;
694 if (Kind.isMergeableConst()) {
695 if (Kind.isMergeableConst4())
696 return FourByteConstantSection;
697 if (Kind.isMergeableConst8())
698 return EightByteConstantSection;
699 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
700 return SixteenByteConstantSection;
703 // Otherwise, if it is readonly, but not something we can specially optimize,
704 // just drop it in .const.
705 if (Kind.isReadOnly())
706 return ReadOnlySection;
708 // If this is marked const, put it into a const section. But if the dynamic
709 // linker needs to write to it, put it in the data segment.
710 if (Kind.isReadOnlyWithRel())
711 return ConstDataSection;
713 // Put zero initialized globals with strong external linkage in the
714 // DATA, __common section with the .zerofill directive.
715 if (Kind.isBSSExtern())
716 return DataCommonSection;
718 // Put zero initialized globals with local linkage in __DATA,__bss directive
719 // with the .zerofill directive (aka .lcomm).
720 if (Kind.isBSSLocal())
721 return DataBSSSection;
723 // Otherwise, just drop the variable in the normal data section.
728 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
729 // If this constant requires a relocation, we have to put it in the data
730 // segment, not in the text segment.
731 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
732 return ConstDataSection;
734 if (Kind.isMergeableConst4())
735 return FourByteConstantSection;
736 if (Kind.isMergeableConst8())
737 return EightByteConstantSection;
738 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
739 return SixteenByteConstantSection;
740 return ReadOnlySection; // .const
743 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
744 /// not to emit the UsedDirective for some symbols in llvm.used.
745 // FIXME: REMOVE this (rdar://7071300)
746 bool TargetLoweringObjectFileMachO::
747 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
748 /// On Darwin, internally linked data beginning with "L" or "l" does not have
749 /// the directive emitted (this occurs in ObjC metadata).
750 if (!GV) return false;
752 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
753 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
754 // FIXME: ObjC metadata is currently emitted as internal symbols that have
755 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
756 // this horrible hack can go away.
757 MCSymbol *Sym = Mang->getSymbol(GV);
758 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
765 const MCExpr *TargetLoweringObjectFileMachO::
766 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
767 MachineModuleInfo *MMI, unsigned Encoding,
768 MCStreamer &Streamer) const {
769 // The mach-o version of this method defaults to returning a stub reference.
771 if (Encoding & DW_EH_PE_indirect) {
772 MachineModuleInfoMachO &MachOMMI =
773 MMI->getObjFileInfo<MachineModuleInfoMachO>();
775 SmallString<128> Name;
776 Mang->getNameWithPrefix(Name, GV, true);
777 Name += "$non_lazy_ptr";
779 // Add information about the stub reference to MachOMMI so that the stub
780 // gets emitted by the asmprinter.
781 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
782 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
783 if (StubSym.getPointer() == 0) {
784 MCSymbol *Sym = Mang->getSymbol(GV);
785 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
788 return TargetLoweringObjectFile::
789 getExprForDwarfReference(SSym, Mang, MMI,
790 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
793 return TargetLoweringObjectFile::
794 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
797 unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
798 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
801 unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
802 return DW_EH_PE_pcrel;
805 unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
806 return DW_EH_PE_pcrel;
809 unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
810 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
813 //===----------------------------------------------------------------------===//
815 //===----------------------------------------------------------------------===//
817 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
818 const TargetMachine &TM) {
819 TargetLoweringObjectFile::Initialize(Ctx, TM);
821 getContext().getCOFFSection(".text",
822 COFF::IMAGE_SCN_CNT_CODE |
823 COFF::IMAGE_SCN_MEM_EXECUTE |
824 COFF::IMAGE_SCN_MEM_READ,
825 SectionKind::getText());
827 getContext().getCOFFSection(".data",
828 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
829 COFF::IMAGE_SCN_MEM_READ |
830 COFF::IMAGE_SCN_MEM_WRITE,
831 SectionKind::getDataRel());
833 getContext().getCOFFSection(".rdata",
834 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
835 COFF::IMAGE_SCN_MEM_READ,
836 SectionKind::getReadOnly());
838 getContext().getCOFFSection(".ctors",
839 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
840 COFF::IMAGE_SCN_MEM_READ |
841 COFF::IMAGE_SCN_MEM_WRITE,
842 SectionKind::getDataRel());
844 getContext().getCOFFSection(".dtors",
845 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
846 COFF::IMAGE_SCN_MEM_READ |
847 COFF::IMAGE_SCN_MEM_WRITE,
848 SectionKind::getDataRel());
850 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
851 // though it contains relocatable pointers. In PIC mode, this is probably a
852 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
853 // adjusted or this should be a data section.
855 getContext().getCOFFSection(".gcc_except_table",
856 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
857 COFF::IMAGE_SCN_MEM_READ,
858 SectionKind::getReadOnly());
861 getContext().getCOFFSection(".debug_abbrev",
862 COFF::IMAGE_SCN_MEM_DISCARDABLE |
863 COFF::IMAGE_SCN_MEM_READ,
864 SectionKind::getMetadata());
866 getContext().getCOFFSection(".debug_info",
867 COFF::IMAGE_SCN_MEM_DISCARDABLE |
868 COFF::IMAGE_SCN_MEM_READ,
869 SectionKind::getMetadata());
871 getContext().getCOFFSection(".debug_line",
872 COFF::IMAGE_SCN_MEM_DISCARDABLE |
873 COFF::IMAGE_SCN_MEM_READ,
874 SectionKind::getMetadata());
876 getContext().getCOFFSection(".debug_frame",
877 COFF::IMAGE_SCN_MEM_DISCARDABLE |
878 COFF::IMAGE_SCN_MEM_READ,
879 SectionKind::getMetadata());
880 DwarfPubNamesSection =
881 getContext().getCOFFSection(".debug_pubnames",
882 COFF::IMAGE_SCN_MEM_DISCARDABLE |
883 COFF::IMAGE_SCN_MEM_READ,
884 SectionKind::getMetadata());
885 DwarfPubTypesSection =
886 getContext().getCOFFSection(".debug_pubtypes",
887 COFF::IMAGE_SCN_MEM_DISCARDABLE |
888 COFF::IMAGE_SCN_MEM_READ,
889 SectionKind::getMetadata());
891 getContext().getCOFFSection(".debug_str",
892 COFF::IMAGE_SCN_MEM_DISCARDABLE |
893 COFF::IMAGE_SCN_MEM_READ,
894 SectionKind::getMetadata());
896 getContext().getCOFFSection(".debug_loc",
897 COFF::IMAGE_SCN_MEM_DISCARDABLE |
898 COFF::IMAGE_SCN_MEM_READ,
899 SectionKind::getMetadata());
900 DwarfARangesSection =
901 getContext().getCOFFSection(".debug_aranges",
902 COFF::IMAGE_SCN_MEM_DISCARDABLE |
903 COFF::IMAGE_SCN_MEM_READ,
904 SectionKind::getMetadata());
906 getContext().getCOFFSection(".debug_ranges",
907 COFF::IMAGE_SCN_MEM_DISCARDABLE |
908 COFF::IMAGE_SCN_MEM_READ,
909 SectionKind::getMetadata());
910 DwarfMacroInfoSection =
911 getContext().getCOFFSection(".debug_macinfo",
912 COFF::IMAGE_SCN_MEM_DISCARDABLE |
913 COFF::IMAGE_SCN_MEM_READ,
914 SectionKind::getMetadata());
917 getContext().getCOFFSection(".drectve",
918 COFF::IMAGE_SCN_LNK_INFO,
919 SectionKind::getMetadata());
922 const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
923 return getContext().getCOFFSection(".eh_frame",
924 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
925 COFF::IMAGE_SCN_MEM_READ |
926 COFF::IMAGE_SCN_MEM_WRITE,
927 SectionKind::getDataRel());
932 getCOFFSectionFlags(SectionKind K) {
937 COFF::IMAGE_SCN_MEM_DISCARDABLE;
940 COFF::IMAGE_SCN_MEM_EXECUTE |
941 COFF::IMAGE_SCN_MEM_READ |
942 COFF::IMAGE_SCN_CNT_CODE;
945 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
946 COFF::IMAGE_SCN_MEM_READ |
947 COFF::IMAGE_SCN_MEM_WRITE;
948 else if (K.isReadOnly())
950 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
951 COFF::IMAGE_SCN_MEM_READ;
952 else if (K.isWriteable())
954 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
955 COFF::IMAGE_SCN_MEM_READ |
956 COFF::IMAGE_SCN_MEM_WRITE;
961 const MCSection *TargetLoweringObjectFileCOFF::
962 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
963 Mangler *Mang, const TargetMachine &TM) const {
964 return getContext().getCOFFSection(GV->getSection(),
965 getCOFFSectionFlags(Kind),
969 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
974 if (Kind.isWriteable())
980 const MCSection *TargetLoweringObjectFileCOFF::
981 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
982 Mangler *Mang, const TargetMachine &TM) const {
983 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
985 // If this global is linkonce/weak and the target handles this by emitting it
986 // into a 'uniqued' section name, create and return the section now.
987 if (GV->isWeakForLinker()) {
988 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
989 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
990 MCSymbol *Sym = Mang->getSymbol(GV);
991 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
993 unsigned Characteristics = getCOFFSectionFlags(Kind);
995 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
997 return getContext().getCOFFSection(Name.str(), Characteristics,
998 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
1002 return getTextSection();
1004 return getDataSection();