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/Module.h"
21 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCSectionMachO.h"
25 #include "llvm/MC/MCSectionELF.h"
26 #include "llvm/MC/MCSectionCOFF.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Target/Mangler.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Target/TargetOptions.h"
33 #include "llvm/Support/Dwarf.h"
34 #include "llvm/Support/ELF.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/ADT/SmallString.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/ADT/Triple.h"
41 using namespace dwarf;
43 //===----------------------------------------------------------------------===//
45 //===----------------------------------------------------------------------===//
48 TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
50 MachineModuleInfo *MMI) const {
51 unsigned Encoding = getPersonalityEncoding();
52 switch (Encoding & 0x70) {
54 report_fatal_error("We do not support this DWARF encoding yet!");
55 case dwarf::DW_EH_PE_absptr:
56 return Mang->getSymbol(GV);
57 case dwarf::DW_EH_PE_pcrel: {
58 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
59 Mang->getSymbol(GV)->getName());
64 void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
65 const TargetMachine &TM,
66 const MCSymbol *Sym) const {
67 SmallString<64> NameData("DW.ref.");
68 NameData += Sym->getName();
69 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
70 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
71 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
72 StringRef Prefix = ".data.";
73 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
74 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
75 const MCSection *Sec = getContext().getELFSection(NameData,
78 SectionKind::getDataRel(),
80 Streamer.SwitchSection(Sec);
81 Streamer.EmitValueToAlignment(8);
82 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
83 const MCExpr *E = MCConstantExpr::Create(8, getContext());
84 Streamer.EmitELFSize(Label, E);
85 Streamer.EmitLabel(Label);
87 unsigned Size = TM.getTargetData()->getPointerSize();
88 Streamer.EmitSymbolValue(Sym, Size);
92 getELFKindForNamedSection(StringRef Name, SectionKind K) {
93 // N.B.: The defaults used in here are no the same ones used in MC.
94 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
95 // both gas and MC will produce a section with no flags. Given
96 // section(".eh_frame") gcc will produce
97 // .section .eh_frame,"a",@progbits
98 if (Name.empty() || Name[0] != '.') return K;
100 // Some lame default implementation based on some magic section names.
101 if (Name == ".bss" ||
102 Name.startswith(".bss.") ||
103 Name.startswith(".gnu.linkonce.b.") ||
104 Name.startswith(".llvm.linkonce.b.") ||
106 Name.startswith(".sbss.") ||
107 Name.startswith(".gnu.linkonce.sb.") ||
108 Name.startswith(".llvm.linkonce.sb."))
109 return SectionKind::getBSS();
111 if (Name == ".tdata" ||
112 Name.startswith(".tdata.") ||
113 Name.startswith(".gnu.linkonce.td.") ||
114 Name.startswith(".llvm.linkonce.td."))
115 return SectionKind::getThreadData();
117 if (Name == ".tbss" ||
118 Name.startswith(".tbss.") ||
119 Name.startswith(".gnu.linkonce.tb.") ||
120 Name.startswith(".llvm.linkonce.tb."))
121 return SectionKind::getThreadBSS();
127 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
129 if (Name == ".init_array")
130 return ELF::SHT_INIT_ARRAY;
132 if (Name == ".fini_array")
133 return ELF::SHT_FINI_ARRAY;
135 if (Name == ".preinit_array")
136 return ELF::SHT_PREINIT_ARRAY;
138 if (K.isBSS() || K.isThreadBSS())
139 return ELF::SHT_NOBITS;
141 return ELF::SHT_PROGBITS;
146 getELFSectionFlags(SectionKind K) {
150 Flags |= ELF::SHF_ALLOC;
153 Flags |= ELF::SHF_EXECINSTR;
156 Flags |= ELF::SHF_WRITE;
158 if (K.isThreadLocal())
159 Flags |= ELF::SHF_TLS;
161 // K.isMergeableConst() is left out to honour PR4650
162 if (K.isMergeableCString() || K.isMergeableConst4() ||
163 K.isMergeableConst8() || K.isMergeableConst16())
164 Flags |= ELF::SHF_MERGE;
166 if (K.isMergeableCString())
167 Flags |= ELF::SHF_STRINGS;
173 const MCSection *TargetLoweringObjectFileELF::
174 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
175 Mangler *Mang, const TargetMachine &TM) const {
176 StringRef SectionName = GV->getSection();
178 // Infer section flags from the section name if we can.
179 Kind = getELFKindForNamedSection(SectionName, Kind);
181 return getContext().getELFSection(SectionName,
182 getELFSectionType(SectionName, Kind),
183 getELFSectionFlags(Kind), Kind);
186 /// getSectionPrefixForGlobal - Return the section prefix name used by options
187 /// FunctionsSections and DataSections.
188 static const char *getSectionPrefixForGlobal(SectionKind Kind) {
189 if (Kind.isText()) return ".text.";
190 if (Kind.isReadOnly()) return ".rodata.";
192 if (Kind.isThreadData()) return ".tdata.";
193 if (Kind.isThreadBSS()) return ".tbss.";
195 if (Kind.isDataNoRel()) return ".data.";
196 if (Kind.isDataRelLocal()) return ".data.rel.local.";
197 if (Kind.isDataRel()) return ".data.rel.";
198 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
200 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
201 return ".data.rel.ro.";
205 const MCSection *TargetLoweringObjectFileELF::
206 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
207 Mangler *Mang, const TargetMachine &TM) const {
208 // If we have -ffunction-section or -fdata-section then we should emit the
209 // global value to a uniqued section specifically for it.
210 bool EmitUniquedSection;
212 EmitUniquedSection = TM.getFunctionSections();
214 EmitUniquedSection = TM.getDataSections();
216 // If this global is linkonce/weak and the target handles this by emitting it
217 // into a 'uniqued' section name, create and return the section now.
218 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
219 !Kind.isCommon() && !Kind.isBSS()) {
221 Prefix = getSectionPrefixForGlobal(Kind);
223 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
224 MCSymbol *Sym = Mang->getSymbol(GV);
225 Name.append(Sym->getName().begin(), Sym->getName().end());
226 StringRef Group = "";
227 unsigned Flags = getELFSectionFlags(Kind);
228 if (GV->isWeakForLinker()) {
229 Group = Sym->getName();
230 Flags |= ELF::SHF_GROUP;
233 return getContext().getELFSection(Name.str(),
234 getELFSectionType(Name.str(), Kind),
235 Flags, Kind, 0, Group);
238 if (Kind.isText()) return TextSection;
240 if (Kind.isMergeable1ByteCString() ||
241 Kind.isMergeable2ByteCString() ||
242 Kind.isMergeable4ByteCString()) {
244 // We also need alignment here.
245 // FIXME: this is getting the alignment of the character, not the
246 // alignment of the global!
248 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
250 const char *SizeSpec = ".rodata.str1.";
251 if (Kind.isMergeable2ByteCString())
252 SizeSpec = ".rodata.str2.";
253 else if (Kind.isMergeable4ByteCString())
254 SizeSpec = ".rodata.str4.";
256 assert(Kind.isMergeable1ByteCString() && "unknown string width");
259 std::string Name = SizeSpec + utostr(Align);
260 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
267 if (Kind.isMergeableConst()) {
268 if (Kind.isMergeableConst4() && MergeableConst4Section)
269 return MergeableConst4Section;
270 if (Kind.isMergeableConst8() && MergeableConst8Section)
271 return MergeableConst8Section;
272 if (Kind.isMergeableConst16() && MergeableConst16Section)
273 return MergeableConst16Section;
274 return ReadOnlySection; // .const
277 if (Kind.isReadOnly()) return ReadOnlySection;
279 if (Kind.isThreadData()) return TLSDataSection;
280 if (Kind.isThreadBSS()) return TLSBSSSection;
282 // Note: we claim that common symbols are put in BSSSection, but they are
283 // really emitted with the magic .comm directive, which creates a symbol table
284 // entry but not a section.
285 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
287 if (Kind.isDataNoRel()) return DataSection;
288 if (Kind.isDataRelLocal()) return DataRelLocalSection;
289 if (Kind.isDataRel()) return DataRelSection;
290 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
292 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
293 return DataRelROSection;
296 /// getSectionForConstant - Given a mergeable constant with the
297 /// specified size and relocation information, return a section that it
298 /// should be placed in.
299 const MCSection *TargetLoweringObjectFileELF::
300 getSectionForConstant(SectionKind Kind) const {
301 if (Kind.isMergeableConst4() && MergeableConst4Section)
302 return MergeableConst4Section;
303 if (Kind.isMergeableConst8() && MergeableConst8Section)
304 return MergeableConst8Section;
305 if (Kind.isMergeableConst16() && MergeableConst16Section)
306 return MergeableConst16Section;
307 if (Kind.isReadOnly())
308 return ReadOnlySection;
310 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
311 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
312 return DataRelROSection;
315 const MCExpr *TargetLoweringObjectFileELF::
316 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
317 MachineModuleInfo *MMI,
318 unsigned Encoding, MCStreamer &Streamer) const {
320 if (Encoding & dwarf::DW_EH_PE_indirect) {
321 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
323 SmallString<128> Name;
324 Mang->getNameWithPrefix(Name, GV, true);
327 // Add information about the stub reference to ELFMMI so that the stub
328 // gets emitted by the asmprinter.
329 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
330 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
331 if (StubSym.getPointer() == 0) {
332 MCSymbol *Sym = Mang->getSymbol(GV);
333 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
336 return TargetLoweringObjectFile::
337 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
340 return TargetLoweringObjectFile::
341 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
345 TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const {
346 // The default scheme is .ctor / .dtor, so we have to invert the priority
348 if (Priority == 65535)
349 return StaticCtorSection;
351 std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
352 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
353 ELF::SHF_ALLOC |ELF::SHF_WRITE,
354 SectionKind::getDataRel());
358 TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
359 // The default scheme is .ctor / .dtor, so we have to invert the priority
361 if (Priority == 65535)
362 return StaticDtorSection;
364 std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
365 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
366 ELF::SHF_ALLOC |ELF::SHF_WRITE,
367 SectionKind::getDataRel());
370 //===----------------------------------------------------------------------===//
372 //===----------------------------------------------------------------------===//
374 /// emitModuleFlags - Emit the module flags that specify the garbage collection
376 void TargetLoweringObjectFileMachO::
377 emitModuleFlags(MCStreamer &Streamer,
378 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
379 Mangler *Mang, const TargetMachine &TM) const {
380 unsigned VersionVal = 0;
381 unsigned GCFlags = 0;
382 StringRef SectionVal;
384 for (ArrayRef<Module::ModuleFlagEntry>::iterator
385 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
386 const Module::ModuleFlagEntry &MFE = *i;
388 // Ignore flags with 'Require' behavior.
389 if (MFE.Behavior == Module::Require)
392 StringRef Key = MFE.Key->getString();
393 Value *Val = MFE.Val;
395 if (Key == "Objective-C Image Info Version")
396 VersionVal = cast<ConstantInt>(Val)->getZExtValue();
397 else if (Key == "Objective-C Garbage Collection" ||
398 Key == "Objective-C GC Only")
399 GCFlags |= cast<ConstantInt>(Val)->getZExtValue();
400 else if (Key == "Objective-C Image Info Section")
401 SectionVal = cast<MDString>(Val)->getString();
404 // The section is mandatory. If we don't have it, then we don't have GC info.
405 if (SectionVal.empty()) return;
407 StringRef Segment, Section;
408 unsigned TAA = 0, StubSize = 0;
410 std::string ErrorCode =
411 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
412 TAA, TAAParsed, StubSize);
413 if (!ErrorCode.empty())
414 // If invalid, report the error with report_fatal_error.
415 report_fatal_error("Invalid section specifier '" + Section + "': " +
419 const MCSectionMachO *S =
420 getContext().getMachOSection(Segment, Section, TAA, StubSize,
421 SectionKind::getDataNoRel());
422 Streamer.SwitchSection(S);
423 Streamer.EmitLabel(getContext().
424 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
425 Streamer.EmitIntValue(VersionVal, 4);
426 Streamer.EmitIntValue(GCFlags, 4);
427 Streamer.AddBlankLine();
430 const MCSection *TargetLoweringObjectFileMachO::
431 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
432 Mangler *Mang, const TargetMachine &TM) const {
433 // Parse the section specifier and create it if valid.
434 StringRef Segment, Section;
435 unsigned TAA = 0, StubSize = 0;
437 std::string ErrorCode =
438 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
439 TAA, TAAParsed, StubSize);
440 if (!ErrorCode.empty()) {
441 // If invalid, report the error with report_fatal_error.
442 report_fatal_error("Global variable '" + GV->getName() +
443 "' has an invalid section specifier '" +
444 GV->getSection() + "': " + ErrorCode + ".");
448 const MCSectionMachO *S =
449 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
451 // If TAA wasn't set by ParseSectionSpecifier() above,
452 // use the value returned by getMachOSection() as a default.
454 TAA = S->getTypeAndAttributes();
456 // Okay, now that we got the section, verify that the TAA & StubSize agree.
457 // If the user declared multiple globals with different section flags, we need
458 // to reject it here.
459 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
460 // If invalid, report the error with report_fatal_error.
461 report_fatal_error("Global variable '" + GV->getName() +
462 "' section type or attributes does not match previous"
463 " section specifier");
469 const MCSection *TargetLoweringObjectFileMachO::
470 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
471 Mangler *Mang, const TargetMachine &TM) const {
473 // Handle thread local data.
474 if (Kind.isThreadBSS()) return TLSBSSSection;
475 if (Kind.isThreadData()) return TLSDataSection;
478 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
480 // If this is weak/linkonce, put this in a coalescable section, either in text
481 // or data depending on if it is writable.
482 if (GV->isWeakForLinker()) {
483 if (Kind.isReadOnly())
484 return ConstTextCoalSection;
485 return DataCoalSection;
488 // FIXME: Alignment check should be handled by section classifier.
489 if (Kind.isMergeable1ByteCString() &&
490 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
491 return CStringSection;
493 // Do not put 16-bit arrays in the UString section if they have an
494 // externally visible label, this runs into issues with certain linker
496 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
497 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
498 return UStringSection;
500 if (Kind.isMergeableConst()) {
501 if (Kind.isMergeableConst4())
502 return FourByteConstantSection;
503 if (Kind.isMergeableConst8())
504 return EightByteConstantSection;
505 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
506 return SixteenByteConstantSection;
509 // Otherwise, if it is readonly, but not something we can specially optimize,
510 // just drop it in .const.
511 if (Kind.isReadOnly())
512 return ReadOnlySection;
514 // If this is marked const, put it into a const section. But if the dynamic
515 // linker needs to write to it, put it in the data segment.
516 if (Kind.isReadOnlyWithRel())
517 return ConstDataSection;
519 // Put zero initialized globals with strong external linkage in the
520 // DATA, __common section with the .zerofill directive.
521 if (Kind.isBSSExtern())
522 return DataCommonSection;
524 // Put zero initialized globals with local linkage in __DATA,__bss directive
525 // with the .zerofill directive (aka .lcomm).
526 if (Kind.isBSSLocal())
527 return DataBSSSection;
529 // Otherwise, just drop the variable in the normal data section.
534 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
535 // If this constant requires a relocation, we have to put it in the data
536 // segment, not in the text segment.
537 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
538 return ConstDataSection;
540 if (Kind.isMergeableConst4())
541 return FourByteConstantSection;
542 if (Kind.isMergeableConst8())
543 return EightByteConstantSection;
544 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
545 return SixteenByteConstantSection;
546 return ReadOnlySection; // .const
549 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
550 /// not to emit the UsedDirective for some symbols in llvm.used.
551 // FIXME: REMOVE this (rdar://7071300)
552 bool TargetLoweringObjectFileMachO::
553 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
554 /// On Darwin, internally linked data beginning with "L" or "l" does not have
555 /// the directive emitted (this occurs in ObjC metadata).
556 if (!GV) return false;
558 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
559 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
560 // FIXME: ObjC metadata is currently emitted as internal symbols that have
561 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
562 // this horrible hack can go away.
563 MCSymbol *Sym = Mang->getSymbol(GV);
564 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
571 const MCExpr *TargetLoweringObjectFileMachO::
572 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
573 MachineModuleInfo *MMI, unsigned Encoding,
574 MCStreamer &Streamer) const {
575 // The mach-o version of this method defaults to returning a stub reference.
577 if (Encoding & DW_EH_PE_indirect) {
578 MachineModuleInfoMachO &MachOMMI =
579 MMI->getObjFileInfo<MachineModuleInfoMachO>();
581 SmallString<128> Name;
582 Mang->getNameWithPrefix(Name, GV, true);
583 Name += "$non_lazy_ptr";
585 // Add information about the stub reference to MachOMMI so that the stub
586 // gets emitted by the asmprinter.
587 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
588 MachineModuleInfoImpl::StubValueTy &StubSym =
589 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
590 MachOMMI.getGVStubEntry(SSym);
591 if (StubSym.getPointer() == 0) {
592 MCSymbol *Sym = Mang->getSymbol(GV);
593 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
596 return TargetLoweringObjectFile::
597 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
600 return TargetLoweringObjectFile::
601 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
604 MCSymbol *TargetLoweringObjectFileMachO::
605 getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
606 MachineModuleInfo *MMI) const {
607 // The mach-o version of this method defaults to returning a stub reference.
608 MachineModuleInfoMachO &MachOMMI =
609 MMI->getObjFileInfo<MachineModuleInfoMachO>();
611 SmallString<128> Name;
612 Mang->getNameWithPrefix(Name, GV, true);
613 Name += "$non_lazy_ptr";
615 // Add information about the stub reference to MachOMMI so that the stub
616 // gets emitted by the asmprinter.
617 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
618 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
619 if (StubSym.getPointer() == 0) {
620 MCSymbol *Sym = Mang->getSymbol(GV);
621 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
627 //===----------------------------------------------------------------------===//
629 //===----------------------------------------------------------------------===//
632 getCOFFSectionFlags(SectionKind K) {
637 COFF::IMAGE_SCN_MEM_DISCARDABLE;
640 COFF::IMAGE_SCN_MEM_EXECUTE |
641 COFF::IMAGE_SCN_MEM_READ |
642 COFF::IMAGE_SCN_CNT_CODE;
645 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
646 COFF::IMAGE_SCN_MEM_READ |
647 COFF::IMAGE_SCN_MEM_WRITE;
648 else if (K.isThreadLocal())
650 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
651 COFF::IMAGE_SCN_MEM_READ |
652 COFF::IMAGE_SCN_MEM_WRITE;
653 else if (K.isReadOnly())
655 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
656 COFF::IMAGE_SCN_MEM_READ;
657 else if (K.isWriteable())
659 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
660 COFF::IMAGE_SCN_MEM_READ |
661 COFF::IMAGE_SCN_MEM_WRITE;
666 const MCSection *TargetLoweringObjectFileCOFF::
667 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
668 Mangler *Mang, const TargetMachine &TM) const {
669 return getContext().getCOFFSection(GV->getSection(),
670 getCOFFSectionFlags(Kind),
674 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
679 if (Kind.isThreadLocal())
681 if (Kind.isWriteable())
687 const MCSection *TargetLoweringObjectFileCOFF::
688 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
689 Mangler *Mang, const TargetMachine &TM) const {
691 // If this global is linkonce/weak and the target handles this by emitting it
692 // into a 'uniqued' section name, create and return the section now.
693 if (GV->isWeakForLinker()) {
694 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
695 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
696 MCSymbol *Sym = Mang->getSymbol(GV);
697 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
699 unsigned Characteristics = getCOFFSectionFlags(Kind);
701 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
703 return getContext().getCOFFSection(Name.str(), Characteristics,
704 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
708 return getTextSection();
710 if (Kind.isThreadLocal())
711 return getTLSDataSection();
713 return getDataSection();