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/ADT/SmallString.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCSectionCOFF.h"
29 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/MC/MCSectionMachO.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.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/Target/Mangler.h"
38 #include "llvm/Target/TargetMachine.h"
40 using namespace dwarf;
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
47 TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
49 MachineModuleInfo *MMI) const {
50 unsigned Encoding = getPersonalityEncoding();
51 switch (Encoding & 0x70) {
53 report_fatal_error("We do not support this DWARF encoding yet!");
54 case dwarf::DW_EH_PE_absptr:
55 return getSymbol(*Mang, GV);
56 case dwarf::DW_EH_PE_pcrel: {
57 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
58 getSymbol(*Mang, GV)->getName());
63 void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
64 const TargetMachine &TM,
65 const MCSymbol *Sym) const {
66 SmallString<64> NameData("DW.ref.");
67 NameData += Sym->getName();
68 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
69 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
70 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
71 StringRef Prefix = ".data.";
72 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
73 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
74 const MCSection *Sec = getContext().getELFSection(NameData,
77 SectionKind::getDataRel(),
79 unsigned Size = TM.getDataLayout()->getPointerSize();
80 Streamer.SwitchSection(Sec);
81 Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment());
82 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
83 const MCExpr *E = MCConstantExpr::Create(Size, getContext());
84 Streamer.EmitELFSize(Label, E);
85 Streamer.EmitLabel(Label);
87 Streamer.EmitSymbolValue(Sym, Size);
90 const MCExpr *TargetLoweringObjectFileELF::
91 getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang,
92 MachineModuleInfo *MMI, unsigned Encoding,
93 MCStreamer &Streamer) const {
95 if (Encoding & dwarf::DW_EH_PE_indirect) {
96 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
98 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, ".DW.stub");
100 // Add information about the stub reference to ELFMMI so that the stub
101 // gets emitted by the asmprinter.
102 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
103 if (StubSym.getPointer() == 0) {
104 MCSymbol *Sym = getSymbol(*Mang, GV);
105 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
108 return TargetLoweringObjectFile::
109 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
110 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
113 return TargetLoweringObjectFile::
114 getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer);
118 getELFKindForNamedSection(StringRef Name, SectionKind K) {
119 // N.B.: The defaults used in here are no the same ones used in MC.
120 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
121 // both gas and MC will produce a section with no flags. Given
122 // section(".eh_frame") gcc will produce:
124 // .section .eh_frame,"a",@progbits
125 if (Name.empty() || Name[0] != '.') return K;
127 // Some lame default implementation based on some magic section names.
128 if (Name == ".bss" ||
129 Name.startswith(".bss.") ||
130 Name.startswith(".gnu.linkonce.b.") ||
131 Name.startswith(".llvm.linkonce.b.") ||
133 Name.startswith(".sbss.") ||
134 Name.startswith(".gnu.linkonce.sb.") ||
135 Name.startswith(".llvm.linkonce.sb."))
136 return SectionKind::getBSS();
138 if (Name == ".tdata" ||
139 Name.startswith(".tdata.") ||
140 Name.startswith(".gnu.linkonce.td.") ||
141 Name.startswith(".llvm.linkonce.td."))
142 return SectionKind::getThreadData();
144 if (Name == ".tbss" ||
145 Name.startswith(".tbss.") ||
146 Name.startswith(".gnu.linkonce.tb.") ||
147 Name.startswith(".llvm.linkonce.tb."))
148 return SectionKind::getThreadBSS();
154 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
156 if (Name == ".init_array")
157 return ELF::SHT_INIT_ARRAY;
159 if (Name == ".fini_array")
160 return ELF::SHT_FINI_ARRAY;
162 if (Name == ".preinit_array")
163 return ELF::SHT_PREINIT_ARRAY;
165 if (K.isBSS() || K.isThreadBSS())
166 return ELF::SHT_NOBITS;
168 return ELF::SHT_PROGBITS;
173 getELFSectionFlags(SectionKind K) {
177 Flags |= ELF::SHF_ALLOC;
180 Flags |= ELF::SHF_EXECINSTR;
183 Flags |= ELF::SHF_WRITE;
185 if (K.isThreadLocal())
186 Flags |= ELF::SHF_TLS;
188 // K.isMergeableConst() is left out to honour PR4650
189 if (K.isMergeableCString() || K.isMergeableConst4() ||
190 K.isMergeableConst8() || K.isMergeableConst16())
191 Flags |= ELF::SHF_MERGE;
193 if (K.isMergeableCString())
194 Flags |= ELF::SHF_STRINGS;
200 const MCSection *TargetLoweringObjectFileELF::
201 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
202 Mangler *Mang, const TargetMachine &TM) const {
203 StringRef SectionName = GV->getSection();
205 // Infer section flags from the section name if we can.
206 Kind = getELFKindForNamedSection(SectionName, Kind);
208 return getContext().getELFSection(SectionName,
209 getELFSectionType(SectionName, Kind),
210 getELFSectionFlags(Kind), Kind);
213 /// getSectionPrefixForGlobal - Return the section prefix name used by options
214 /// FunctionsSections and DataSections.
215 static const char *getSectionPrefixForGlobal(SectionKind Kind) {
216 if (Kind.isText()) return ".text.";
217 if (Kind.isReadOnly()) return ".rodata.";
218 if (Kind.isBSS()) return ".bss.";
220 if (Kind.isThreadData()) return ".tdata.";
221 if (Kind.isThreadBSS()) return ".tbss.";
223 if (Kind.isDataNoRel()) return ".data.";
224 if (Kind.isDataRelLocal()) return ".data.rel.local.";
225 if (Kind.isDataRel()) return ".data.rel.";
226 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
228 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
229 return ".data.rel.ro.";
233 const MCSection *TargetLoweringObjectFileELF::
234 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
235 Mangler *Mang, const TargetMachine &TM) const {
236 // If we have -ffunction-section or -fdata-section then we should emit the
237 // global value to a uniqued section specifically for it.
238 bool EmitUniquedSection;
240 EmitUniquedSection = TM.getFunctionSections();
242 EmitUniquedSection = TM.getDataSections();
244 // If this global is linkonce/weak and the target handles this by emitting it
245 // into a 'uniqued' section name, create and return the section now.
246 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
249 Prefix = getSectionPrefixForGlobal(Kind);
251 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
252 MCSymbol *Sym = getSymbol(*Mang, GV);
253 Name.append(Sym->getName().begin(), Sym->getName().end());
254 StringRef Group = "";
255 unsigned Flags = getELFSectionFlags(Kind);
256 if (GV->isWeakForLinker()) {
257 Group = Sym->getName();
258 Flags |= ELF::SHF_GROUP;
261 return getContext().getELFSection(Name.str(),
262 getELFSectionType(Name.str(), Kind),
263 Flags, Kind, 0, Group);
266 if (Kind.isText()) return TextSection;
268 if (Kind.isMergeable1ByteCString() ||
269 Kind.isMergeable2ByteCString() ||
270 Kind.isMergeable4ByteCString()) {
272 // We also need alignment here.
273 // FIXME: this is getting the alignment of the character, not the
274 // alignment of the global!
276 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV));
278 const char *SizeSpec = ".rodata.str1.";
279 if (Kind.isMergeable2ByteCString())
280 SizeSpec = ".rodata.str2.";
281 else if (Kind.isMergeable4ByteCString())
282 SizeSpec = ".rodata.str4.";
284 assert(Kind.isMergeable1ByteCString() && "unknown string width");
287 std::string Name = SizeSpec + utostr(Align);
288 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
295 if (Kind.isMergeableConst()) {
296 if (Kind.isMergeableConst4() && MergeableConst4Section)
297 return MergeableConst4Section;
298 if (Kind.isMergeableConst8() && MergeableConst8Section)
299 return MergeableConst8Section;
300 if (Kind.isMergeableConst16() && MergeableConst16Section)
301 return MergeableConst16Section;
302 return ReadOnlySection; // .const
305 if (Kind.isReadOnly()) return ReadOnlySection;
307 if (Kind.isThreadData()) return TLSDataSection;
308 if (Kind.isThreadBSS()) return TLSBSSSection;
310 // Note: we claim that common symbols are put in BSSSection, but they are
311 // really emitted with the magic .comm directive, which creates a symbol table
312 // entry but not a section.
313 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
315 if (Kind.isDataNoRel()) return DataSection;
316 if (Kind.isDataRelLocal()) return DataRelLocalSection;
317 if (Kind.isDataRel()) return DataRelSection;
318 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
320 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
321 return DataRelROSection;
324 /// getSectionForConstant - Given a mergeable constant with the
325 /// specified size and relocation information, return a section that it
326 /// should be placed in.
327 const MCSection *TargetLoweringObjectFileELF::
328 getSectionForConstant(SectionKind Kind) const {
329 if (Kind.isMergeableConst4() && MergeableConst4Section)
330 return MergeableConst4Section;
331 if (Kind.isMergeableConst8() && MergeableConst8Section)
332 return MergeableConst8Section;
333 if (Kind.isMergeableConst16() && MergeableConst16Section)
334 return MergeableConst16Section;
335 if (Kind.isReadOnly())
336 return ReadOnlySection;
338 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
339 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
340 return DataRelROSection;
344 TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const {
345 // The default scheme is .ctor / .dtor, so we have to invert the priority
347 if (Priority == 65535)
348 return StaticCtorSection;
351 std::string Name = std::string(".init_array.") + utostr(Priority);
352 return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY,
353 ELF::SHF_ALLOC | ELF::SHF_WRITE,
354 SectionKind::getDataRel());
356 std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
357 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
358 ELF::SHF_ALLOC |ELF::SHF_WRITE,
359 SectionKind::getDataRel());
364 TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
365 // The default scheme is .ctor / .dtor, so we have to invert the priority
367 if (Priority == 65535)
368 return StaticDtorSection;
371 std::string Name = std::string(".fini_array.") + utostr(Priority);
372 return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY,
373 ELF::SHF_ALLOC | ELF::SHF_WRITE,
374 SectionKind::getDataRel());
376 std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
377 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
378 ELF::SHF_ALLOC |ELF::SHF_WRITE,
379 SectionKind::getDataRel());
384 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
385 UseInitArray = UseInitArray_;
390 getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
393 SectionKind::getDataRel());
395 getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
398 SectionKind::getDataRel());
401 //===----------------------------------------------------------------------===//
403 //===----------------------------------------------------------------------===//
405 /// emitModuleFlags - Perform code emission for module flags.
406 void TargetLoweringObjectFileMachO::
407 emitModuleFlags(MCStreamer &Streamer,
408 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
409 Mangler *Mang, const TargetMachine &TM) const {
410 unsigned VersionVal = 0;
411 unsigned ImageInfoFlags = 0;
412 MDNode *LinkerOptions = 0;
413 StringRef SectionVal;
415 for (ArrayRef<Module::ModuleFlagEntry>::iterator
416 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
417 const Module::ModuleFlagEntry &MFE = *i;
419 // Ignore flags with 'Require' behavior.
420 if (MFE.Behavior == Module::Require)
423 StringRef Key = MFE.Key->getString();
424 Value *Val = MFE.Val;
426 if (Key == "Objective-C Image Info Version") {
427 VersionVal = cast<ConstantInt>(Val)->getZExtValue();
428 } else if (Key == "Objective-C Garbage Collection" ||
429 Key == "Objective-C GC Only" ||
430 Key == "Objective-C Is Simulated") {
431 ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue();
432 } else if (Key == "Objective-C Image Info Section") {
433 SectionVal = cast<MDString>(Val)->getString();
434 } else if (Key == "Linker Options") {
435 LinkerOptions = cast<MDNode>(Val);
439 // Emit the linker options if present.
441 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
442 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
443 SmallVector<std::string, 4> StrOptions;
445 // Convert to strings.
446 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
447 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
448 StrOptions.push_back(MDOption->getString());
451 Streamer.EmitLinkerOptions(StrOptions);
455 // The section is mandatory. If we don't have it, then we don't have GC info.
456 if (SectionVal.empty()) return;
458 StringRef Segment, Section;
459 unsigned TAA = 0, StubSize = 0;
461 std::string ErrorCode =
462 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
463 TAA, TAAParsed, StubSize);
464 if (!ErrorCode.empty())
465 // If invalid, report the error with report_fatal_error.
466 report_fatal_error("Invalid section specifier '" + Section + "': " +
470 const MCSectionMachO *S =
471 getContext().getMachOSection(Segment, Section, TAA, StubSize,
472 SectionKind::getDataNoRel());
473 Streamer.SwitchSection(S);
474 Streamer.EmitLabel(getContext().
475 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
476 Streamer.EmitIntValue(VersionVal, 4);
477 Streamer.EmitIntValue(ImageInfoFlags, 4);
478 Streamer.AddBlankLine();
481 const MCSection *TargetLoweringObjectFileMachO::
482 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
483 Mangler *Mang, const TargetMachine &TM) const {
484 // Parse the section specifier and create it if valid.
485 StringRef Segment, Section;
486 unsigned TAA = 0, StubSize = 0;
488 std::string ErrorCode =
489 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
490 TAA, TAAParsed, StubSize);
491 if (!ErrorCode.empty()) {
492 // If invalid, report the error with report_fatal_error.
493 report_fatal_error("Global variable '" + GV->getName() +
494 "' has an invalid section specifier '" +
495 GV->getSection() + "': " + ErrorCode + ".");
499 const MCSectionMachO *S =
500 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
502 // If TAA wasn't set by ParseSectionSpecifier() above,
503 // use the value returned by getMachOSection() as a default.
505 TAA = S->getTypeAndAttributes();
507 // Okay, now that we got the section, verify that the TAA & StubSize agree.
508 // If the user declared multiple globals with different section flags, we need
509 // to reject it here.
510 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
511 // If invalid, report the error with report_fatal_error.
512 report_fatal_error("Global variable '" + GV->getName() +
513 "' section type or attributes does not match previous"
514 " section specifier");
520 const MCSection *TargetLoweringObjectFileMachO::
521 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
522 Mangler *Mang, const TargetMachine &TM) const {
524 // Handle thread local data.
525 if (Kind.isThreadBSS()) return TLSBSSSection;
526 if (Kind.isThreadData()) return TLSDataSection;
529 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
531 // If this is weak/linkonce, put this in a coalescable section, either in text
532 // or data depending on if it is writable.
533 if (GV->isWeakForLinker()) {
534 if (Kind.isReadOnly())
535 return ConstTextCoalSection;
536 return DataCoalSection;
539 // FIXME: Alignment check should be handled by section classifier.
540 if (Kind.isMergeable1ByteCString() &&
541 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
542 return CStringSection;
544 // Do not put 16-bit arrays in the UString section if they have an
545 // externally visible label, this runs into issues with certain linker
547 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
548 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
549 return UStringSection;
551 if (Kind.isMergeableConst()) {
552 if (Kind.isMergeableConst4())
553 return FourByteConstantSection;
554 if (Kind.isMergeableConst8())
555 return EightByteConstantSection;
556 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
557 return SixteenByteConstantSection;
560 // Otherwise, if it is readonly, but not something we can specially optimize,
561 // just drop it in .const.
562 if (Kind.isReadOnly())
563 return ReadOnlySection;
565 // If this is marked const, put it into a const section. But if the dynamic
566 // linker needs to write to it, put it in the data segment.
567 if (Kind.isReadOnlyWithRel())
568 return ConstDataSection;
570 // Put zero initialized globals with strong external linkage in the
571 // DATA, __common section with the .zerofill directive.
572 if (Kind.isBSSExtern())
573 return DataCommonSection;
575 // Put zero initialized globals with local linkage in __DATA,__bss directive
576 // with the .zerofill directive (aka .lcomm).
577 if (Kind.isBSSLocal())
578 return DataBSSSection;
580 // Otherwise, just drop the variable in the normal data section.
585 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
586 // If this constant requires a relocation, we have to put it in the data
587 // segment, not in the text segment.
588 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
589 return ConstDataSection;
591 if (Kind.isMergeableConst4())
592 return FourByteConstantSection;
593 if (Kind.isMergeableConst8())
594 return EightByteConstantSection;
595 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
596 return SixteenByteConstantSection;
597 return ReadOnlySection; // .const
600 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
601 /// not to emit the UsedDirective for some symbols in llvm.used.
602 // FIXME: REMOVE this (rdar://7071300)
603 bool TargetLoweringObjectFileMachO::
604 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
605 /// On Darwin, internally linked data beginning with "L" or "l" does not have
606 /// the directive emitted (this occurs in ObjC metadata).
607 if (!GV) return false;
609 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
610 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
611 // FIXME: ObjC metadata is currently emitted as internal symbols that have
612 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
613 // this horrible hack can go away.
614 MCSymbol *Sym = getSymbol(*Mang, GV);
615 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
622 const MCExpr *TargetLoweringObjectFileMachO::
623 getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang,
624 MachineModuleInfo *MMI, unsigned Encoding,
625 MCStreamer &Streamer) const {
626 // The mach-o version of this method defaults to returning a stub reference.
628 if (Encoding & DW_EH_PE_indirect) {
629 MachineModuleInfoMachO &MachOMMI =
630 MMI->getObjFileInfo<MachineModuleInfoMachO>();
632 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, "$non_lazy_ptr");
634 // Add information about the stub reference to MachOMMI so that the stub
635 // gets emitted by the asmprinter.
636 MachineModuleInfoImpl::StubValueTy &StubSym =
637 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
638 MachOMMI.getGVStubEntry(SSym);
639 if (StubSym.getPointer() == 0) {
640 MCSymbol *Sym = getSymbol(*Mang, GV);
641 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
644 return TargetLoweringObjectFile::
645 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
646 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
649 return TargetLoweringObjectFile::
650 getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer);
653 MCSymbol *TargetLoweringObjectFileMachO::
654 getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
655 MachineModuleInfo *MMI) const {
656 // The mach-o version of this method defaults to returning a stub reference.
657 MachineModuleInfoMachO &MachOMMI =
658 MMI->getObjFileInfo<MachineModuleInfoMachO>();
660 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, "$non_lazy_ptr");
662 // Add information about the stub reference to MachOMMI so that the stub
663 // gets emitted by the asmprinter.
664 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
665 if (StubSym.getPointer() == 0) {
666 MCSymbol *Sym = getSymbol(*Mang, GV);
667 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
673 //===----------------------------------------------------------------------===//
675 //===----------------------------------------------------------------------===//
678 getCOFFSectionFlags(SectionKind K) {
683 COFF::IMAGE_SCN_MEM_DISCARDABLE;
686 COFF::IMAGE_SCN_MEM_EXECUTE |
687 COFF::IMAGE_SCN_MEM_READ |
688 COFF::IMAGE_SCN_CNT_CODE;
691 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
692 COFF::IMAGE_SCN_MEM_READ |
693 COFF::IMAGE_SCN_MEM_WRITE;
694 else if (K.isThreadLocal())
696 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
697 COFF::IMAGE_SCN_MEM_READ |
698 COFF::IMAGE_SCN_MEM_WRITE;
699 else if (K.isReadOnly())
701 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
702 COFF::IMAGE_SCN_MEM_READ;
703 else if (K.isWriteable())
705 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
706 COFF::IMAGE_SCN_MEM_READ |
707 COFF::IMAGE_SCN_MEM_WRITE;
712 const MCSection *TargetLoweringObjectFileCOFF::
713 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
714 Mangler *Mang, const TargetMachine &TM) const {
716 unsigned Characteristics = getCOFFSectionFlags(Kind);
717 StringRef Name = GV->getSection();
718 StringRef COMDATSymName = "";
719 if (GV->isWeakForLinker()) {
720 Selection = COFF::IMAGE_COMDAT_SELECT_ANY;
721 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
722 MCSymbol *Sym = getSymbol(*Mang, GV);
723 COMDATSymName = Sym->getName();
725 return getContext().getCOFFSection(Name,
732 static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
737 if (Kind.isThreadLocal())
739 if (Kind.isWriteable())
745 const MCSection *TargetLoweringObjectFileCOFF::
746 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
747 Mangler *Mang, const TargetMachine &TM) const {
749 // If this global is linkonce/weak and the target handles this by emitting it
750 // into a 'uniqued' section name, create and return the section now.
751 if (GV->isWeakForLinker()) {
752 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
753 unsigned Characteristics = getCOFFSectionFlags(Kind);
755 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
756 MCSymbol *Sym = getSymbol(*Mang, GV);
757 return getContext().getCOFFSection(Name, Characteristics,
758 Kind, Sym->getName(),
759 COFF::IMAGE_COMDAT_SELECT_ANY);
765 if (Kind.isThreadLocal())
766 return TLSDataSection;
768 if (Kind.isReadOnly())
769 return ReadOnlySection;
777 void TargetLoweringObjectFileCOFF::
778 emitModuleFlags(MCStreamer &Streamer,
779 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
780 Mangler *Mang, const TargetMachine &TM) const {
781 MDNode *LinkerOptions = 0;
783 // Look for the "Linker Options" flag, since it's the only one we support.
784 for (ArrayRef<Module::ModuleFlagEntry>::iterator
785 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
786 const Module::ModuleFlagEntry &MFE = *i;
787 StringRef Key = MFE.Key->getString();
788 Value *Val = MFE.Val;
789 if (Key == "Linker Options") {
790 LinkerOptions = cast<MDNode>(Val);
797 // Emit the linker options to the linker .drectve section. According to the
798 // spec, this section is a space-separated string containing flags for linker.
799 const MCSection *Sec = getDrectveSection();
800 Streamer.SwitchSection(Sec);
801 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
802 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
803 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
804 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
805 StringRef Op = MDOption->getString();
806 // Lead with a space for consistency with our dllexport implementation.
807 std::string Escaped(" ");
808 if (Op.find(" ") != StringRef::npos) {
809 // The PE-COFF spec says args with spaces must be quoted. It doesn't say
810 // how to escape quotes, but it probably uses this algorithm:
811 // http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx
812 // FIXME: Reuse escaping code from Support/Windows/Program.inc
813 Escaped.push_back('\"');
815 Escaped.push_back('\"');
819 Streamer.EmitBytes(Escaped);