1 //===-- llvm/Target/TargetLoweringObjectFile.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/Target/TargetLoweringObjectFile.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/Target/TargetAsmInfo.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/ADT/StringExtras.h"
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
38 StaticCtorSection = 0;
39 StaticDtorSection = 0;
43 DwarfAbbrevSection = 0;
46 DwarfFrameSection = 0;
47 DwarfPubNamesSection = 0;
48 DwarfPubTypesSection = 0;
49 DwarfDebugInlineSection = 0;
52 DwarfARangesSection = 0;
53 DwarfRangesSection = 0;
54 DwarfMacroInfoSection = 0;
57 TargetLoweringObjectFile::~TargetLoweringObjectFile() {
60 static bool isSuitableForBSS(const GlobalVariable *GV) {
61 Constant *C = GV->getInitializer();
63 // Must have zero initializer.
64 if (!C->isNullValue())
67 // Leave constant zeros in readonly constant sections, so they can be shared.
71 // If the global has an explicit section specified, don't put it in BSS.
72 if (!GV->getSection().empty())
75 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
79 // Otherwise, put it in BSS!
83 /// IsNullTerminatedString - Return true if the specified constant (which is
84 /// known to have a type that is an array of 1/2/4 byte elements) ends with a
85 /// nul value and contains no other nuls in it.
86 static bool IsNullTerminatedString(const Constant *C) {
87 const ArrayType *ATy = cast<ArrayType>(C->getType());
89 // First check: is we have constant array of i8 terminated with zero
90 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
91 if (ATy->getNumElements() == 0) return false;
94 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
95 if (Null == 0 || Null->getZExtValue() != 0)
96 return false; // Not null terminated.
98 // Verify that the null doesn't occur anywhere else in the string.
99 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
100 // Reject constantexpr elements etc.
101 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
102 CVA->getOperand(i) == Null)
107 // Another possibility: [1 x i8] zeroinitializer
108 if (isa<ConstantAggregateZero>(C))
109 return ATy->getNumElements() == 1;
114 /// getKindForGlobal - This is a top-level target-independent classifier for
115 /// a global variable. Given an global variable and information from TM, it
116 /// classifies the global in a variety of ways that make various target
117 /// implementations simpler. The target implementation is free to ignore this
118 /// extra info of course.
119 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
120 const TargetMachine &TM){
121 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
122 "Can only be used for global definitions");
124 Reloc::Model ReloModel = TM.getRelocationModel();
126 // Early exit - functions should be always in text sections.
127 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
129 return SectionKind::getText();
132 // Handle thread-local data first.
133 if (GVar->isThreadLocal()) {
134 if (isSuitableForBSS(GVar))
135 return SectionKind::getThreadBSS();
136 return SectionKind::getThreadData();
139 // Variable can be easily put to BSS section.
140 if (isSuitableForBSS(GVar))
141 return SectionKind::getBSS();
143 Constant *C = GVar->getInitializer();
145 // If the global is marked constant, we can put it into a mergable section,
146 // a mergable string section, or general .data if it contains relocations.
147 if (GVar->isConstant()) {
148 // If the initializer for the global contains something that requires a
149 // relocation, then we may have to drop this into a wriable data section
150 // even though it is marked const.
151 switch (C->getRelocationInfo()) {
152 default: llvm_unreachable("unknown relocation info kind");
153 case Constant::NoRelocation:
154 // If initializer is a null-terminated string, put it in a "cstring"
155 // section of the right width.
156 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
157 if (const IntegerType *ITy =
158 dyn_cast<IntegerType>(ATy->getElementType())) {
159 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
160 ITy->getBitWidth() == 32) &&
161 IsNullTerminatedString(C)) {
162 if (ITy->getBitWidth() == 8)
163 return SectionKind::getMergeable1ByteCString();
164 if (ITy->getBitWidth() == 16)
165 return SectionKind::getMergeable2ByteCString();
167 assert(ITy->getBitWidth() == 32 && "Unknown width");
168 return SectionKind::getMergeable4ByteCString();
173 // Otherwise, just drop it into a mergable constant section. If we have
174 // a section for this size, use it, otherwise use the arbitrary sized
176 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
177 case 4: return SectionKind::getMergeableConst4();
178 case 8: return SectionKind::getMergeableConst8();
179 case 16: return SectionKind::getMergeableConst16();
180 default: return SectionKind::getMergeableConst();
183 case Constant::LocalRelocation:
184 // In static relocation model, the linker will resolve all addresses, so
185 // the relocation entries will actually be constants by the time the app
186 // starts up. However, we can't put this into a mergable section, because
187 // the linker doesn't take relocations into consideration when it tries to
188 // merge entries in the section.
189 if (ReloModel == Reloc::Static)
190 return SectionKind::getReadOnly();
192 // Otherwise, the dynamic linker needs to fix it up, put it in the
193 // writable data.rel.local section.
194 return SectionKind::getReadOnlyWithRelLocal();
196 case Constant::GlobalRelocations:
197 // In static relocation model, the linker will resolve all addresses, so
198 // the relocation entries will actually be constants by the time the app
199 // starts up. However, we can't put this into a mergable section, because
200 // the linker doesn't take relocations into consideration when it tries to
201 // merge entries in the section.
202 if (ReloModel == Reloc::Static)
203 return SectionKind::getReadOnly();
205 // Otherwise, the dynamic linker needs to fix it up, put it in the
206 // writable data.rel section.
207 return SectionKind::getReadOnlyWithRel();
211 // Okay, this isn't a constant. If the initializer for the global is going
212 // to require a runtime relocation by the dynamic linker, put it into a more
213 // specific section to improve startup time of the app. This coalesces these
214 // globals together onto fewer pages, improving the locality of the dynamic
216 if (ReloModel == Reloc::Static)
217 return SectionKind::getDataNoRel();
219 switch (C->getRelocationInfo()) {
220 default: llvm_unreachable("unknown relocation info kind");
221 case Constant::NoRelocation:
222 return SectionKind::getDataNoRel();
223 case Constant::LocalRelocation:
224 return SectionKind::getDataRelLocal();
225 case Constant::GlobalRelocations:
226 return SectionKind::getDataRel();
230 /// SectionForGlobal - This method computes the appropriate section to emit
231 /// the specified global variable or function definition. This should not
232 /// be passed external (or available externally) globals.
233 const MCSection *TargetLoweringObjectFile::
234 SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
235 const TargetMachine &TM) const {
236 // Select section name.
237 if (GV->hasSection())
238 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
241 // Use default section depending on the 'type' of global
242 return SelectSectionForGlobal(GV, Kind, Mang, TM);
246 // Lame default implementation. Calculate the section name for global.
248 TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
251 const TargetMachine &TM) const{
252 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
255 return getTextSection();
257 if (Kind.isBSS() && BSSSection != 0)
260 if (Kind.isReadOnly() && ReadOnlySection != 0)
261 return ReadOnlySection;
263 return getDataSection();
266 /// getSectionForConstant - Given a mergable constant with the
267 /// specified size and relocation information, return a section that it
268 /// should be placed in.
270 TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
271 if (Kind.isReadOnly() && ReadOnlySection != 0)
272 return ReadOnlySection;
279 //===----------------------------------------------------------------------===//
281 //===----------------------------------------------------------------------===//
283 const MCSection *TargetLoweringObjectFileELF::
284 getELFSection(const char *Name, bool isDirective, SectionKind Kind) const {
285 if (MCSection *S = getContext().GetSection(Name))
287 return MCSectionELF::Create(Name, isDirective, Kind, getContext());
290 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
291 const TargetMachine &TM) {
292 TargetLoweringObjectFile::Initialize(Ctx, TM);
294 BSSSection = getELFSection("\t.bss", true, SectionKind::getBSS());
296 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
297 // FIXME: Does .section .bss work everywhere??
298 // FIXME2: this should just be handle by the section printer. We should get
299 // away from syntactic view of the sections and MCSection should just be a
301 BSSSection = getELFSection("\t.bss", false, SectionKind::getBSS());
304 TextSection = getELFSection("\t.text", true, SectionKind::getText());
305 DataSection = getELFSection("\t.data", true, SectionKind::getDataRel());
307 getELFSection("\t.rodata", false, SectionKind::getReadOnly());
309 getELFSection("\t.tdata", false, SectionKind::getThreadData());
311 TLSBSSSection = getELFSection("\t.tbss", false,
312 SectionKind::getThreadBSS());
314 DataRelSection = getELFSection("\t.data.rel", false,
315 SectionKind::getDataRel());
316 DataRelLocalSection = getELFSection("\t.data.rel.local", false,
317 SectionKind::getDataRelLocal());
318 DataRelROSection = getELFSection("\t.data.rel.ro", false,
319 SectionKind::getReadOnlyWithRel());
320 DataRelROLocalSection =
321 getELFSection("\t.data.rel.ro.local", false,
322 SectionKind::getReadOnlyWithRelLocal());
324 MergeableConst4Section = getELFSection(".rodata.cst4", false,
325 SectionKind::getMergeableConst4());
326 MergeableConst8Section = getELFSection(".rodata.cst8", false,
327 SectionKind::getMergeableConst8());
328 MergeableConst16Section = getELFSection(".rodata.cst16", false,
329 SectionKind::getMergeableConst16());
332 getELFSection(".ctors", false, SectionKind::getDataRel());
334 getELFSection(".dtors", false, SectionKind::getDataRel());
336 // Exception Handling Sections.
338 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
339 // it contains relocatable pointers. In PIC mode, this is probably a big
340 // runtime hit for C++ apps. Either the contents of the LSDA need to be
341 // adjusted or this should be a data section.
343 getELFSection(".gcc_except_table", false, SectionKind::getReadOnly());
345 getELFSection(".eh_frame", false, SectionKind::getDataRel());
347 // Debug Info Sections.
349 getELFSection(".debug_abbrev", false, SectionKind::getMetadata());
351 getELFSection(".debug_info", false, SectionKind::getMetadata());
353 getELFSection(".debug_line", false, SectionKind::getMetadata());
355 getELFSection(".debug_frame", false, SectionKind::getMetadata());
356 DwarfPubNamesSection =
357 getELFSection(".debug_pubnames", false, SectionKind::getMetadata());
358 DwarfPubTypesSection =
359 getELFSection(".debug_pubtypes", false, SectionKind::getMetadata());
361 getELFSection(".debug_str", false, SectionKind::getMetadata());
363 getELFSection(".debug_loc", false, SectionKind::getMetadata());
364 DwarfARangesSection =
365 getELFSection(".debug_aranges", false, SectionKind::getMetadata());
367 getELFSection(".debug_ranges", false, SectionKind::getMetadata());
368 DwarfMacroInfoSection =
369 getELFSection(".debug_macinfo", false, SectionKind::getMetadata());
374 getELFKindForNamedSection(const char *Name, SectionKind K) {
375 if (Name[0] != '.') return K;
377 // Some lame default implementation based on some magic section names.
378 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
379 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
380 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
381 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
382 return SectionKind::getBSS();
384 if (strcmp(Name, ".tdata") == 0 ||
385 strncmp(Name, ".tdata.", 7) == 0 ||
386 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
387 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
388 return SectionKind::getThreadData();
390 if (strcmp(Name, ".tbss") == 0 ||
391 strncmp(Name, ".tbss.", 6) == 0 ||
392 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
393 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
394 return SectionKind::getThreadBSS();
399 const MCSection *TargetLoweringObjectFileELF::
400 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
401 Mangler *Mang, const TargetMachine &TM) const {
402 // Infer section flags from the section name if we can.
403 Kind = getELFKindForNamedSection(GV->getSection().c_str(), Kind);
405 return getELFSection(GV->getSection().c_str(), false, Kind);
408 static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
409 if (Kind.isText()) return ".gnu.linkonce.t.";
410 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
412 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
413 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
415 if (Kind.isBSS()) return ".gnu.linkonce.b.";
416 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
417 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
418 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
419 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
421 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
422 return ".gnu.linkonce.d.rel.ro.";
425 const MCSection *TargetLoweringObjectFileELF::
426 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
427 Mangler *Mang, const TargetMachine &TM) const {
429 // If this global is linkonce/weak and the target handles this by emitting it
430 // into a 'uniqued' section name, create and return the section now.
431 if (GV->isWeakForLinker()) {
432 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
433 std::string Name = Mang->makeNameProper(GV->getNameStr());
434 return getELFSection((Prefix+Name).c_str(), false, Kind);
437 if (Kind.isText()) return TextSection;
439 if (Kind.isMergeable1ByteCString() ||
440 Kind.isMergeable2ByteCString() ||
441 Kind.isMergeable4ByteCString()) {
443 // We also need alignment here.
444 // FIXME: this is getting the alignment of the character, not the
445 // alignment of the global!
447 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
449 const char *SizeSpec = ".rodata.str1.";
450 if (Kind.isMergeable2ByteCString())
451 SizeSpec = ".rodata.str2.";
452 else if (Kind.isMergeable4ByteCString())
453 SizeSpec = ".rodata.str4.";
455 assert(Kind.isMergeable1ByteCString() && "unknown string width");
458 std::string Name = SizeSpec + utostr(Align);
459 return getELFSection(Name.c_str(), false, Kind);
462 if (Kind.isMergeableConst()) {
463 if (Kind.isMergeableConst4())
464 return MergeableConst4Section;
465 if (Kind.isMergeableConst8())
466 return MergeableConst8Section;
467 if (Kind.isMergeableConst16())
468 return MergeableConst16Section;
469 return ReadOnlySection; // .const
472 if (Kind.isReadOnly()) return ReadOnlySection;
474 if (Kind.isThreadData()) return TLSDataSection;
475 if (Kind.isThreadBSS()) return TLSBSSSection;
477 if (Kind.isBSS()) return BSSSection;
479 if (Kind.isDataNoRel()) return DataSection;
480 if (Kind.isDataRelLocal()) return DataRelLocalSection;
481 if (Kind.isDataRel()) return DataRelSection;
482 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
484 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
485 return DataRelROSection;
488 /// getSectionForConstant - Given a mergeable constant with the
489 /// specified size and relocation information, return a section that it
490 /// should be placed in.
491 const MCSection *TargetLoweringObjectFileELF::
492 getSectionForConstant(SectionKind Kind) const {
493 if (Kind.isMergeableConst4())
494 return MergeableConst4Section;
495 if (Kind.isMergeableConst8())
496 return MergeableConst8Section;
497 if (Kind.isMergeableConst16())
498 return MergeableConst16Section;
499 if (Kind.isReadOnly())
500 return ReadOnlySection;
502 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
503 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
504 return DataRelROSection;
507 //===----------------------------------------------------------------------===//
509 //===----------------------------------------------------------------------===//
512 const MCSection *TargetLoweringObjectFileMachO::
513 getMachOSection(const char *Name, bool isDirective, SectionKind Kind) const {
514 if (MCSection *S = getContext().GetSection(Name))
516 return MCSectionMachO::Create(Name, isDirective, Kind, getContext());
521 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
522 const TargetMachine &TM) {
523 TargetLoweringObjectFile::Initialize(Ctx, TM);
524 TextSection = getMachOSection("\t.text", true, SectionKind::getText());
525 DataSection = getMachOSection("\t.data", true, SectionKind::getDataRel());
527 CStringSection = getMachOSection("\t.cstring", true,
528 SectionKind::getMergeable1ByteCString());
529 UStringSection = getMachOSection("__TEXT,__ustring", false,
530 SectionKind::getMergeable2ByteCString());
531 FourByteConstantSection = getMachOSection("\t.literal4\n", true,
532 SectionKind::getMergeableConst4());
533 EightByteConstantSection = getMachOSection("\t.literal8\n", true,
534 SectionKind::getMergeableConst8());
536 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
537 // to using it in -static mode.
538 if (TM.getRelocationModel() != Reloc::Static &&
539 TM.getTargetData()->getPointerSize() == 32)
540 SixteenByteConstantSection =
541 getMachOSection("\t.literal16\n", true,
542 SectionKind::getMergeableConst16());
544 SixteenByteConstantSection = 0;
546 ReadOnlySection = getMachOSection("\t.const", true,
547 SectionKind::getReadOnly());
550 getMachOSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
551 false, SectionKind::getText());
552 ConstTextCoalSection = getMachOSection("\t__TEXT,__const_coal,coalesced",
553 false, SectionKind::getText());
554 ConstDataCoalSection = getMachOSection("\t__DATA,__const_coal,coalesced",
555 false, SectionKind::getText());
556 ConstDataSection = getMachOSection("\t.const_data", true,
557 SectionKind::getReadOnlyWithRel());
558 DataCoalSection = getMachOSection("\t__DATA,__datacoal_nt,coalesced",
559 false, SectionKind::getDataRel());
561 if (TM.getRelocationModel() == Reloc::Static) {
563 getMachOSection(".constructor", true, SectionKind::getDataRel());
565 getMachOSection(".destructor", true, SectionKind::getDataRel());
568 getMachOSection(".mod_init_func", true, SectionKind::getDataRel());
570 getMachOSection(".mod_term_func", true, SectionKind::getDataRel());
573 // Exception Handling.
574 LSDASection = getMachOSection("__DATA,__gcc_except_tab", false,
575 SectionKind::getDataRel());
577 getMachOSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
578 "+live_support", false, SectionKind::getReadOnly());
580 // Debug Information.
581 // FIXME: Don't use 'directive' syntax: need flags for debug/regular??
582 // FIXME: Need __DWARF segment.
584 getMachOSection(".section __DWARF,__debug_abbrev,regular,debug", true,
585 SectionKind::getMetadata());
587 getMachOSection(".section __DWARF,__debug_info,regular,debug", true,
588 SectionKind::getMetadata());
590 getMachOSection(".section __DWARF,__debug_line,regular,debug", true,
591 SectionKind::getMetadata());
593 getMachOSection(".section __DWARF,__debug_frame,regular,debug", true,
594 SectionKind::getMetadata());
595 DwarfPubNamesSection =
596 getMachOSection(".section __DWARF,__debug_pubnames,regular,debug", true,
597 SectionKind::getMetadata());
598 DwarfPubTypesSection =
599 getMachOSection(".section __DWARF,__debug_pubtypes,regular,debug", true,
600 SectionKind::getMetadata());
602 getMachOSection(".section __DWARF,__debug_str,regular,debug", true,
603 SectionKind::getMetadata());
605 getMachOSection(".section __DWARF,__debug_loc,regular,debug", true,
606 SectionKind::getMetadata());
607 DwarfARangesSection =
608 getMachOSection(".section __DWARF,__debug_aranges,regular,debug", true,
609 SectionKind::getMetadata());
611 getMachOSection(".section __DWARF,__debug_ranges,regular,debug", true,
612 SectionKind::getMetadata());
613 DwarfMacroInfoSection =
614 getMachOSection(".section __DWARF,__debug_macinfo,regular,debug", true,
615 SectionKind::getMetadata());
616 DwarfDebugInlineSection =
617 getMachOSection(".section __DWARF,__debug_inlined,regular,debug", true,
618 SectionKind::getMetadata());
621 const MCSection *TargetLoweringObjectFileMachO::
622 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
623 Mangler *Mang, const TargetMachine &TM) const {
624 return getMachOSection(GV->getSection().c_str(), false, Kind);
627 const MCSection *TargetLoweringObjectFileMachO::
628 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
629 Mangler *Mang, const TargetMachine &TM) const {
630 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
633 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
635 // If this is weak/linkonce, put this in a coalescable section, either in text
636 // or data depending on if it is writable.
637 if (GV->isWeakForLinker()) {
638 if (Kind.isReadOnly())
639 return ConstTextCoalSection;
640 return DataCoalSection;
643 // FIXME: Alignment check should be handled by section classifier.
644 if (Kind.isMergeable1ByteCString() ||
645 Kind.isMergeable2ByteCString()) {
646 if (TM.getTargetData()->getPreferredAlignment(
647 cast<GlobalVariable>(GV)) < 32) {
648 if (Kind.isMergeable1ByteCString())
649 return CStringSection;
650 assert(Kind.isMergeable2ByteCString());
651 return UStringSection;
655 if (Kind.isMergeableConst()) {
656 if (Kind.isMergeableConst4())
657 return FourByteConstantSection;
658 if (Kind.isMergeableConst8())
659 return EightByteConstantSection;
660 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
661 return SixteenByteConstantSection;
664 // Otherwise, if it is readonly, but not something we can specially optimize,
665 // just drop it in .const.
666 if (Kind.isReadOnly())
667 return ReadOnlySection;
669 // If this is marked const, put it into a const section. But if the dynamic
670 // linker needs to write to it, put it in the data segment.
671 if (Kind.isReadOnlyWithRel())
672 return ConstDataSection;
674 // Otherwise, just drop the variable in the normal data section.
679 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
680 // If this constant requires a relocation, we have to put it in the data
681 // segment, not in the text segment.
682 if (Kind.isDataRel())
683 return ConstDataSection;
685 if (Kind.isMergeableConst4())
686 return FourByteConstantSection;
687 if (Kind.isMergeableConst8())
688 return EightByteConstantSection;
689 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
690 return SixteenByteConstantSection;
691 return ReadOnlySection; // .const
694 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
695 /// not to emit the UsedDirective for some symbols in llvm.used.
696 // FIXME: REMOVE this (rdar://7071300)
697 bool TargetLoweringObjectFileMachO::
698 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
699 /// On Darwin, internally linked data beginning with "L" or "l" does not have
700 /// the directive emitted (this occurs in ObjC metadata).
701 if (!GV) return false;
703 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
704 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
705 // FIXME: ObjC metadata is currently emitted as internal symbols that have
706 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
707 // this horrible hack can go away.
708 const std::string &Name = Mang->getMangledName(GV);
709 if (Name[0] == 'L' || Name[0] == 'l')
717 //===----------------------------------------------------------------------===//
719 //===----------------------------------------------------------------------===//
722 const MCSection *TargetLoweringObjectFileCOFF::
723 getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
724 if (MCSection *S = getContext().GetSection(Name))
726 return MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
729 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
730 const TargetMachine &TM) {
731 TargetLoweringObjectFile::Initialize(Ctx, TM);
732 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
733 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
735 getCOFFSection(".ctors", false, SectionKind::getDataRel());
737 getCOFFSection(".dtors", false, SectionKind::getDataRel());
741 // FIXME: Don't use 'directive' mode here.
743 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
744 true, SectionKind::getMetadata());
746 getCOFFSection("\t.section\t.debug_info,\"dr\"",
747 true, SectionKind::getMetadata());
749 getCOFFSection("\t.section\t.debug_line,\"dr\"",
750 true, SectionKind::getMetadata());
752 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
753 true, SectionKind::getMetadata());
754 DwarfPubNamesSection =
755 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
756 true, SectionKind::getMetadata());
757 DwarfPubTypesSection =
758 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
759 true, SectionKind::getMetadata());
761 getCOFFSection("\t.section\t.debug_str,\"dr\"",
762 true, SectionKind::getMetadata());
764 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
765 true, SectionKind::getMetadata());
766 DwarfARangesSection =
767 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
768 true, SectionKind::getMetadata());
770 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
771 true, SectionKind::getMetadata());
772 DwarfMacroInfoSection =
773 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
774 true, SectionKind::getMetadata());
777 const MCSection *TargetLoweringObjectFileCOFF::
778 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
779 Mangler *Mang, const TargetMachine &TM) const {
780 return getCOFFSection(GV->getSection().c_str(), false, Kind);
783 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
785 return ".text$linkonce";
786 if (Kind.isWriteable())
787 return ".data$linkonce";
788 return ".rdata$linkonce";
792 const MCSection *TargetLoweringObjectFileCOFF::
793 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
794 Mangler *Mang, const TargetMachine &TM) const {
795 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
797 // If this global is linkonce/weak and the target handles this by emitting it
798 // into a 'uniqued' section name, create and return the section now.
799 if (GV->isWeakForLinker()) {
800 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
801 std::string Name = Mang->makeNameProper(GV->getNameStr());
802 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
806 return getTextSection();
808 return getDataSection();