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/MCSymbol.h"
26 #include "llvm/Target/Mangler.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetOptions.h"
30 #include "llvm/Support/Dwarf.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/ADT/StringExtras.h"
36 using namespace dwarf;
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
41 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
43 TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
44 // If we have the section uniquing map, free it.
45 delete (ELFUniqueMapTy*)UniquingMap;
48 const MCSection *TargetLoweringObjectFileELF::
49 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
50 SectionKind Kind, bool IsExplicit) const {
52 UniquingMap = new ELFUniqueMapTy();
53 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
55 // Do the lookup, if we have a hit, return it.
56 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
57 if (Entry.getValue()) return Entry.getValue();
59 MCSectionELF *Result = MCSectionELF::Create(Entry.getKey(), Type, Flags, Kind,
60 IsExplicit, getContext());
61 Entry.setValue(Result);
65 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
66 const TargetMachine &TM) {
68 ((ELFUniqueMapTy*)UniquingMap)->clear();
69 TargetLoweringObjectFile::Initialize(Ctx, TM);
72 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
73 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
74 SectionKind::getBSS());
77 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
78 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
79 SectionKind::getText());
82 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
83 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
84 SectionKind::getDataRel());
87 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
88 MCSectionELF::SHF_ALLOC,
89 SectionKind::getReadOnly());
92 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
93 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
94 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
97 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
98 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
99 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
102 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
103 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
104 SectionKind::getDataRel());
106 DataRelLocalSection =
107 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
109 SectionKind::getDataRelLocal());
112 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
113 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
114 SectionKind::getReadOnlyWithRel());
116 DataRelROLocalSection =
117 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
118 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
119 SectionKind::getReadOnlyWithRelLocal());
121 MergeableConst4Section =
122 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
123 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
124 SectionKind::getMergeableConst4());
126 MergeableConst8Section =
127 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
128 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
129 SectionKind::getMergeableConst8());
131 MergeableConst16Section =
132 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
133 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
134 SectionKind::getMergeableConst16());
137 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
138 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
139 SectionKind::getDataRel());
142 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
143 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
144 SectionKind::getDataRel());
146 // Exception Handling Sections.
148 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
149 // it contains relocatable pointers. In PIC mode, this is probably a big
150 // runtime hit for C++ apps. Either the contents of the LSDA need to be
151 // adjusted or this should be a data section.
153 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
154 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
156 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
157 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
158 SectionKind::getDataRel());
160 // Debug Info Sections.
162 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
163 SectionKind::getMetadata());
165 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
166 SectionKind::getMetadata());
168 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
169 SectionKind::getMetadata());
171 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
172 SectionKind::getMetadata());
173 DwarfPubNamesSection =
174 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
175 SectionKind::getMetadata());
176 DwarfPubTypesSection =
177 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
178 SectionKind::getMetadata());
180 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
181 SectionKind::getMetadata());
183 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
184 SectionKind::getMetadata());
185 DwarfARangesSection =
186 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
187 SectionKind::getMetadata());
189 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
190 SectionKind::getMetadata());
191 DwarfMacroInfoSection =
192 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
193 SectionKind::getMetadata());
198 getELFKindForNamedSection(StringRef Name, SectionKind K) {
199 if (Name.empty() || Name[0] != '.') return K;
201 // Some lame default implementation based on some magic section names.
202 if (Name == ".bss" ||
203 Name.startswith(".bss.") ||
204 Name.startswith(".gnu.linkonce.b.") ||
205 Name.startswith(".llvm.linkonce.b.") ||
207 Name.startswith(".sbss.") ||
208 Name.startswith(".gnu.linkonce.sb.") ||
209 Name.startswith(".llvm.linkonce.sb."))
210 return SectionKind::getBSS();
212 if (Name == ".tdata" ||
213 Name.startswith(".tdata.") ||
214 Name.startswith(".gnu.linkonce.td.") ||
215 Name.startswith(".llvm.linkonce.td."))
216 return SectionKind::getThreadData();
218 if (Name == ".tbss" ||
219 Name.startswith(".tbss.") ||
220 Name.startswith(".gnu.linkonce.tb.") ||
221 Name.startswith(".llvm.linkonce.tb."))
222 return SectionKind::getThreadBSS();
228 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
230 if (Name == ".init_array")
231 return MCSectionELF::SHT_INIT_ARRAY;
233 if (Name == ".fini_array")
234 return MCSectionELF::SHT_FINI_ARRAY;
236 if (Name == ".preinit_array")
237 return MCSectionELF::SHT_PREINIT_ARRAY;
239 if (K.isBSS() || K.isThreadBSS())
240 return MCSectionELF::SHT_NOBITS;
242 return MCSectionELF::SHT_PROGBITS;
247 getELFSectionFlags(SectionKind K) {
251 Flags |= MCSectionELF::SHF_ALLOC;
254 Flags |= MCSectionELF::SHF_EXECINSTR;
257 Flags |= MCSectionELF::SHF_WRITE;
259 if (K.isThreadLocal())
260 Flags |= MCSectionELF::SHF_TLS;
262 // K.isMergeableConst() is left out to honour PR4650
263 if (K.isMergeableCString() || K.isMergeableConst4() ||
264 K.isMergeableConst8() || K.isMergeableConst16())
265 Flags |= MCSectionELF::SHF_MERGE;
267 if (K.isMergeableCString())
268 Flags |= MCSectionELF::SHF_STRINGS;
274 const MCSection *TargetLoweringObjectFileELF::
275 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
276 Mangler *Mang, const TargetMachine &TM) const {
277 StringRef SectionName = GV->getSection();
279 // Infer section flags from the section name if we can.
280 Kind = getELFKindForNamedSection(SectionName, Kind);
282 return getELFSection(SectionName,
283 getELFSectionType(SectionName, Kind),
284 getELFSectionFlags(Kind), Kind, true);
287 static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
288 if (Kind.isText()) return ".gnu.linkonce.t.";
289 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
291 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
292 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
294 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
295 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
296 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
297 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
299 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
300 return ".gnu.linkonce.d.rel.ro.";
303 const MCSection *TargetLoweringObjectFileELF::
304 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
305 Mangler *Mang, const TargetMachine &TM) const {
307 // If this global is linkonce/weak and the target handles this by emitting it
308 // into a 'uniqued' section name, create and return the section now.
309 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
310 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
311 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
312 MCSymbol *Sym = Mang->getSymbol(GV);
313 Name.append(Sym->getName().begin(), Sym->getName().end());
314 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
315 getELFSectionFlags(Kind), Kind);
318 if (Kind.isText()) return TextSection;
320 if (Kind.isMergeable1ByteCString() ||
321 Kind.isMergeable2ByteCString() ||
322 Kind.isMergeable4ByteCString()) {
324 // We also need alignment here.
325 // FIXME: this is getting the alignment of the character, not the
326 // alignment of the global!
328 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
330 const char *SizeSpec = ".rodata.str1.";
331 if (Kind.isMergeable2ByteCString())
332 SizeSpec = ".rodata.str2.";
333 else if (Kind.isMergeable4ByteCString())
334 SizeSpec = ".rodata.str4.";
336 assert(Kind.isMergeable1ByteCString() && "unknown string width");
339 std::string Name = SizeSpec + utostr(Align);
340 return getELFSection(Name, MCSectionELF::SHT_PROGBITS,
341 MCSectionELF::SHF_ALLOC |
342 MCSectionELF::SHF_MERGE |
343 MCSectionELF::SHF_STRINGS,
347 if (Kind.isMergeableConst()) {
348 if (Kind.isMergeableConst4() && MergeableConst4Section)
349 return MergeableConst4Section;
350 if (Kind.isMergeableConst8() && MergeableConst8Section)
351 return MergeableConst8Section;
352 if (Kind.isMergeableConst16() && MergeableConst16Section)
353 return MergeableConst16Section;
354 return ReadOnlySection; // .const
357 if (Kind.isReadOnly()) return ReadOnlySection;
359 if (Kind.isThreadData()) return TLSDataSection;
360 if (Kind.isThreadBSS()) return TLSBSSSection;
362 // Note: we claim that common symbols are put in BSSSection, but they are
363 // really emitted with the magic .comm directive, which creates a symbol table
364 // entry but not a section.
365 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
367 if (Kind.isDataNoRel()) return DataSection;
368 if (Kind.isDataRelLocal()) return DataRelLocalSection;
369 if (Kind.isDataRel()) return DataRelSection;
370 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
372 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
373 return DataRelROSection;
376 /// getSectionForConstant - Given a mergeable constant with the
377 /// specified size and relocation information, return a section that it
378 /// should be placed in.
379 const MCSection *TargetLoweringObjectFileELF::
380 getSectionForConstant(SectionKind Kind) const {
381 if (Kind.isMergeableConst4() && MergeableConst4Section)
382 return MergeableConst4Section;
383 if (Kind.isMergeableConst8() && MergeableConst8Section)
384 return MergeableConst8Section;
385 if (Kind.isMergeableConst16() && MergeableConst16Section)
386 return MergeableConst16Section;
387 if (Kind.isReadOnly())
388 return ReadOnlySection;
390 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
391 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
392 return DataRelROSection;
395 const MCExpr *TargetLoweringObjectFileELF::
396 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
397 MachineModuleInfo *MMI,
398 unsigned Encoding, MCStreamer &Streamer) const {
400 if (Encoding & dwarf::DW_EH_PE_indirect) {
401 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
403 SmallString<128> Name;
404 Mang->getNameWithPrefix(Name, GV, true);
407 // Add information about the stub reference to ELFMMI so that the stub
408 // gets emitted by the asmprinter.
409 MCSymbol *SSym = getContext().GetOrCreateTemporarySymbol(Name.str());
410 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
411 if (StubSym.getPointer() == 0) {
412 MCSymbol *Sym = Mang->getSymbol(GV);
413 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
416 return TargetLoweringObjectFile::
417 getExprForDwarfReference(SSym, Mang, MMI,
418 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
421 return TargetLoweringObjectFile::
422 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
425 //===----------------------------------------------------------------------===//
427 //===----------------------------------------------------------------------===//
429 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
431 TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
432 // If we have the MachO uniquing map, free it.
433 delete (MachOUniqueMapTy*)UniquingMap;
437 const MCSectionMachO *TargetLoweringObjectFileMachO::
438 getMachOSection(StringRef Segment, StringRef Section,
439 unsigned TypeAndAttributes,
440 unsigned Reserved2, SectionKind Kind) const {
441 // We unique sections by their segment/section pair. The returned section
442 // may not have the same flags as the requested section, if so this should be
443 // diagnosed by the client as an error.
445 // Create the map if it doesn't already exist.
446 if (UniquingMap == 0)
447 UniquingMap = new MachOUniqueMapTy();
448 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
450 // Form the name to look up.
451 SmallString<64> Name;
456 // Do the lookup, if we have a hit, return it.
457 const MCSectionMachO *&Entry = Map[Name.str()];
458 if (Entry) return Entry;
460 // Otherwise, return a new section.
461 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
462 Reserved2, Kind, getContext());
466 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
467 const TargetMachine &TM) {
468 // _foo.eh symbols are currently always exported so that the linker knows
469 // about them. This is not necessary on 10.6 and later, but it
470 // doesn't hurt anything.
471 // FIXME: I need to get this from Triple.
472 IsFunctionEHSymbolGlobal = true;
473 IsFunctionEHFrameSymbolPrivate = false;
474 SupportsWeakOmittedEHFrame = false;
476 if (UniquingMap != 0)
477 ((MachOUniqueMapTy*)UniquingMap)->clear();
478 TargetLoweringObjectFile::Initialize(Ctx, TM);
481 = getMachOSection("__TEXT", "__text",
482 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
483 SectionKind::getText());
485 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
487 CStringSection // .cstring
488 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
489 SectionKind::getMergeable1ByteCString());
491 = getMachOSection("__TEXT","__ustring", 0,
492 SectionKind::getMergeable2ByteCString());
493 FourByteConstantSection // .literal4
494 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
495 SectionKind::getMergeableConst4());
496 EightByteConstantSection // .literal8
497 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
498 SectionKind::getMergeableConst8());
500 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
501 // to using it in -static mode.
502 SixteenByteConstantSection = 0;
503 if (TM.getRelocationModel() != Reloc::Static &&
504 TM.getTargetData()->getPointerSize() == 32)
505 SixteenByteConstantSection = // .literal16
506 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
507 SectionKind::getMergeableConst16());
509 ReadOnlySection // .const
510 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
513 = getMachOSection("__TEXT", "__textcoal_nt",
514 MCSectionMachO::S_COALESCED |
515 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
516 SectionKind::getText());
518 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
519 SectionKind::getText());
521 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
522 SectionKind::getText());
523 ConstDataSection // .const_data
524 = getMachOSection("__DATA", "__const", 0,
525 SectionKind::getReadOnlyWithRel());
527 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
528 SectionKind::getDataRel());
530 = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL,
531 SectionKind::getBSS());
533 = getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
534 SectionKind::getBSS());
537 LazySymbolPointerSection
538 = getMachOSection("__DATA", "__la_symbol_ptr",
539 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
540 SectionKind::getMetadata());
541 NonLazySymbolPointerSection
542 = getMachOSection("__DATA", "__nl_symbol_ptr",
543 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
544 SectionKind::getMetadata());
546 if (TM.getRelocationModel() == Reloc::Static) {
548 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
550 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
553 = getMachOSection("__DATA", "__mod_init_func",
554 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
555 SectionKind::getDataRel());
557 = getMachOSection("__DATA", "__mod_term_func",
558 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
559 SectionKind::getDataRel());
562 // Exception Handling.
563 LSDASection = getMachOSection("__TEXT", "__gcc_except_tab", 0,
564 SectionKind::getReadOnlyWithRel());
566 getMachOSection("__TEXT", "__eh_frame",
567 MCSectionMachO::S_COALESCED |
568 MCSectionMachO::S_ATTR_NO_TOC |
569 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
570 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
571 SectionKind::getReadOnly());
573 // Debug Information.
575 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
576 SectionKind::getMetadata());
578 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
579 SectionKind::getMetadata());
581 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
582 SectionKind::getMetadata());
584 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
585 SectionKind::getMetadata());
586 DwarfPubNamesSection =
587 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
588 SectionKind::getMetadata());
589 DwarfPubTypesSection =
590 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
591 SectionKind::getMetadata());
593 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
594 SectionKind::getMetadata());
596 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
597 SectionKind::getMetadata());
598 DwarfARangesSection =
599 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
600 SectionKind::getMetadata());
602 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
603 SectionKind::getMetadata());
604 DwarfMacroInfoSection =
605 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
606 SectionKind::getMetadata());
607 DwarfDebugInlineSection =
608 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
609 SectionKind::getMetadata());
612 const MCSection *TargetLoweringObjectFileMachO::
613 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
614 Mangler *Mang, const TargetMachine &TM) const {
615 // Parse the section specifier and create it if valid.
616 StringRef Segment, Section;
617 unsigned TAA, StubSize;
618 std::string ErrorCode =
619 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
621 if (!ErrorCode.empty()) {
622 // If invalid, report the error with llvm_report_error.
623 llvm_report_error("Global variable '" + GV->getNameStr() +
624 "' has an invalid section specifier '" + GV->getSection()+
625 "': " + ErrorCode + ".");
626 // Fall back to dropping it into the data section.
631 const MCSectionMachO *S =
632 getMachOSection(Segment, Section, TAA, StubSize, Kind);
634 // Okay, now that we got the section, verify that the TAA & StubSize agree.
635 // If the user declared multiple globals with different section flags, we need
636 // to reject it here.
637 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
638 // If invalid, report the error with llvm_report_error.
639 llvm_report_error("Global variable '" + GV->getNameStr() +
640 "' section type or attributes does not match previous"
641 " section specifier");
647 const MCSection *TargetLoweringObjectFileMachO::
648 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
649 Mangler *Mang, const TargetMachine &TM) const {
650 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
653 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
655 // If this is weak/linkonce, put this in a coalescable section, either in text
656 // or data depending on if it is writable.
657 if (GV->isWeakForLinker()) {
658 if (Kind.isReadOnly())
659 return ConstTextCoalSection;
660 return DataCoalSection;
663 // FIXME: Alignment check should be handled by section classifier.
664 if (Kind.isMergeable1ByteCString() &&
665 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
666 return CStringSection;
668 // Do not put 16-bit arrays in the UString section if they have an
669 // externally visible label, this runs into issues with certain linker
671 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
672 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
673 return UStringSection;
675 if (Kind.isMergeableConst()) {
676 if (Kind.isMergeableConst4())
677 return FourByteConstantSection;
678 if (Kind.isMergeableConst8())
679 return EightByteConstantSection;
680 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
681 return SixteenByteConstantSection;
684 // Otherwise, if it is readonly, but not something we can specially optimize,
685 // just drop it in .const.
686 if (Kind.isReadOnly())
687 return ReadOnlySection;
689 // If this is marked const, put it into a const section. But if the dynamic
690 // linker needs to write to it, put it in the data segment.
691 if (Kind.isReadOnlyWithRel())
692 return ConstDataSection;
694 // Put zero initialized globals with strong external linkage in the
695 // DATA, __common section with the .zerofill directive.
696 if (Kind.isBSSExtern())
697 return DataCommonSection;
699 // Put zero initialized globals with local linkage in __DATA,__bss directive
700 // with the .zerofill directive (aka .lcomm).
701 if (Kind.isBSSLocal())
702 return DataBSSSection;
704 // Otherwise, just drop the variable in the normal data section.
709 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
710 // If this constant requires a relocation, we have to put it in the data
711 // segment, not in the text segment.
712 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
713 return ConstDataSection;
715 if (Kind.isMergeableConst4())
716 return FourByteConstantSection;
717 if (Kind.isMergeableConst8())
718 return EightByteConstantSection;
719 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
720 return SixteenByteConstantSection;
721 return ReadOnlySection; // .const
724 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
725 /// not to emit the UsedDirective for some symbols in llvm.used.
726 // FIXME: REMOVE this (rdar://7071300)
727 bool TargetLoweringObjectFileMachO::
728 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
729 /// On Darwin, internally linked data beginning with "L" or "l" does not have
730 /// the directive emitted (this occurs in ObjC metadata).
731 if (!GV) return false;
733 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
734 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
735 // FIXME: ObjC metadata is currently emitted as internal symbols that have
736 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
737 // this horrible hack can go away.
738 MCSymbol *Sym = Mang->getSymbol(GV);
739 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
746 const MCExpr *TargetLoweringObjectFileMachO::
747 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
748 MachineModuleInfo *MMI, unsigned Encoding,
749 MCStreamer &Streamer) const {
750 // The mach-o version of this method defaults to returning a stub reference.
752 if (Encoding & DW_EH_PE_indirect) {
753 MachineModuleInfoMachO &MachOMMI =
754 MMI->getObjFileInfo<MachineModuleInfoMachO>();
756 SmallString<128> Name;
757 Mang->getNameWithPrefix(Name, GV, true);
758 Name += "$non_lazy_ptr";
760 // Add information about the stub reference to MachOMMI so that the stub
761 // gets emitted by the asmprinter.
762 MCSymbol *SSym = getContext().GetOrCreateTemporarySymbol(Name.str());
763 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
764 if (StubSym.getPointer() == 0) {
765 MCSymbol *Sym = Mang->getSymbol(GV);
766 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
769 return TargetLoweringObjectFile::
770 getExprForDwarfReference(SSym, Mang, MMI,
771 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
774 return TargetLoweringObjectFile::
775 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
778 unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
779 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
782 unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
783 return DW_EH_PE_pcrel;
786 unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
787 return DW_EH_PE_pcrel;
790 unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
791 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
794 //===----------------------------------------------------------------------===//
796 //===----------------------------------------------------------------------===//
798 typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
800 TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
801 delete (COFFUniqueMapTy*)UniquingMap;
805 const MCSection *TargetLoweringObjectFileCOFF::
806 getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
807 // Create the map if it doesn't already exist.
808 if (UniquingMap == 0)
809 UniquingMap = new MachOUniqueMapTy();
810 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
812 // Do the lookup, if we have a hit, return it.
813 const MCSectionCOFF *&Entry = Map[Name];
814 if (Entry) return Entry;
816 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
819 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
820 const TargetMachine &TM) {
821 if (UniquingMap != 0)
822 ((COFFUniqueMapTy*)UniquingMap)->clear();
823 TargetLoweringObjectFile::Initialize(Ctx, TM);
824 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
825 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
827 getCOFFSection(".ctors", false, SectionKind::getDataRel());
829 getCOFFSection(".dtors", false, SectionKind::getDataRel());
831 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
832 // though it contains relocatable pointers. In PIC mode, this is probably a
833 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
834 // adjusted or this should be a data section.
836 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
838 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
841 // FIXME: Don't use 'directive' mode here.
843 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
844 true, SectionKind::getMetadata());
846 getCOFFSection("\t.section\t.debug_info,\"dr\"",
847 true, SectionKind::getMetadata());
849 getCOFFSection("\t.section\t.debug_line,\"dr\"",
850 true, SectionKind::getMetadata());
852 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
853 true, SectionKind::getMetadata());
854 DwarfPubNamesSection =
855 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
856 true, SectionKind::getMetadata());
857 DwarfPubTypesSection =
858 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
859 true, SectionKind::getMetadata());
861 getCOFFSection("\t.section\t.debug_str,\"dr\"",
862 true, SectionKind::getMetadata());
864 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
865 true, SectionKind::getMetadata());
866 DwarfARangesSection =
867 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
868 true, SectionKind::getMetadata());
870 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
871 true, SectionKind::getMetadata());
872 DwarfMacroInfoSection =
873 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
874 true, SectionKind::getMetadata());
877 const MCSection *TargetLoweringObjectFileCOFF::
878 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
879 Mangler *Mang, const TargetMachine &TM) const {
880 return getCOFFSection(GV->getSection(), false, Kind);
883 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
885 return ".text$linkonce";
886 if (Kind.isWriteable())
887 return ".data$linkonce";
888 return ".rdata$linkonce";
892 const MCSection *TargetLoweringObjectFileCOFF::
893 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
894 Mangler *Mang, const TargetMachine &TM) const {
895 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
897 // If this global is linkonce/weak and the target handles this by emitting it
898 // into a 'uniqued' section name, create and return the section now.
899 if (GV->isWeakForLinker()) {
900 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
901 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
902 MCSymbol *Sym = Mang->getSymbol(GV);
903 Name.append(Sym->getName().begin(), Sym->getName().end());
904 return getCOFFSection(Name.str(), false, Kind);
908 return getTextSection();
910 return getDataSection();