1f22ef01cSRoman Divacky //===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // This file implements classes used to handle lowerings specific to common 11f22ef01cSRoman Divacky // object file formats. 12f22ef01cSRoman Divacky // 13f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 14f22ef01cSRoman Divacky 15f22ef01cSRoman Divacky #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 16139f7f9bSDimitry Andric #include "llvm/ADT/SmallString.h" 17139f7f9bSDimitry Andric #include "llvm/ADT/StringExtras.h" 18139f7f9bSDimitry Andric #include "llvm/ADT/Triple.h" 19f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineModuleInfoImpls.h" 20139f7f9bSDimitry Andric #include "llvm/IR/Constants.h" 21139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h" 22139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h" 23139f7f9bSDimitry Andric #include "llvm/IR/Function.h" 24139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h" 2591bc56edSDimitry Andric #include "llvm/IR/Mangler.h" 26139f7f9bSDimitry Andric #include "llvm/IR/Module.h" 277d523365SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 28f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h" 29f22ef01cSRoman Divacky #include "llvm/MC/MCExpr.h" 30f22ef01cSRoman Divacky #include "llvm/MC/MCSectionCOFF.h" 31139f7f9bSDimitry Andric #include "llvm/MC/MCSectionELF.h" 32139f7f9bSDimitry Andric #include "llvm/MC/MCSectionMachO.h" 333b0f4066SDimitry Andric #include "llvm/MC/MCStreamer.h" 3497bc6c73SDimitry Andric #include "llvm/MC/MCSymbolELF.h" 35ff0cc061SDimitry Andric #include "llvm/MC/MCValue.h" 367d523365SDimitry Andric #include "llvm/Support/COFF.h" 37f22ef01cSRoman Divacky #include "llvm/Support/Dwarf.h" 382754fe60SDimitry Andric #include "llvm/Support/ELF.h" 39f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h" 40f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h" 4191bc56edSDimitry Andric #include "llvm/Target/TargetLowering.h" 42139f7f9bSDimitry Andric #include "llvm/Target/TargetMachine.h" 4339d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h" 44f22ef01cSRoman Divacky using namespace llvm; 45f22ef01cSRoman Divacky using namespace dwarf; 46f22ef01cSRoman Divacky 47f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 48f22ef01cSRoman Divacky // ELF 49f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 50f22ef01cSRoman Divacky 5191bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( 5291bc56edSDimitry Andric const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 533b0f4066SDimitry Andric MachineModuleInfo *MMI) const { 543b0f4066SDimitry Andric unsigned Encoding = getPersonalityEncoding(); 5591bc56edSDimitry Andric if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect) 56ff0cc061SDimitry Andric return getContext().getOrCreateSymbol(StringRef("DW.ref.") + 5791bc56edSDimitry Andric TM.getSymbol(GV, Mang)->getName()); 5891bc56edSDimitry Andric if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr) 5991bc56edSDimitry Andric return TM.getSymbol(GV, Mang); 6091bc56edSDimitry Andric report_fatal_error("We do not support this DWARF encoding yet!"); 613b0f4066SDimitry Andric } 623b0f4066SDimitry Andric 637d523365SDimitry Andric void TargetLoweringObjectFileELF::emitPersonalityValue( 647d523365SDimitry Andric MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const { 6517a519f9SDimitry Andric SmallString<64> NameData("DW.ref."); 6617a519f9SDimitry Andric NameData += Sym->getName(); 6797bc6c73SDimitry Andric MCSymbolELF *Label = 6897bc6c73SDimitry Andric cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData)); 693b0f4066SDimitry Andric Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); 703b0f4066SDimitry Andric Streamer.EmitSymbolAttribute(Label, MCSA_Weak); 7117a519f9SDimitry Andric StringRef Prefix = ".data."; 7217a519f9SDimitry Andric NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end()); 733b0f4066SDimitry Andric unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; 74ff0cc061SDimitry Andric MCSection *Sec = getContext().getELFSection(NameData, ELF::SHT_PROGBITS, 75ff0cc061SDimitry Andric Flags, 0, Label->getName()); 767d523365SDimitry Andric unsigned Size = DL.getPointerSize(); 773b0f4066SDimitry Andric Streamer.SwitchSection(Sec); 787d523365SDimitry Andric Streamer.EmitValueToAlignment(DL.getPointerABIAlignment()); 793b0f4066SDimitry Andric Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); 8097bc6c73SDimitry Andric const MCExpr *E = MCConstantExpr::create(Size, getContext()); 8197bc6c73SDimitry Andric Streamer.emitELFSize(Label, E); 823b0f4066SDimitry Andric Streamer.EmitLabel(Label); 833b0f4066SDimitry Andric 843b0f4066SDimitry Andric Streamer.EmitSymbolValue(Sym, Size); 853b0f4066SDimitry Andric } 863b0f4066SDimitry Andric 8791bc56edSDimitry Andric const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( 8891bc56edSDimitry Andric const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 8991bc56edSDimitry Andric const TargetMachine &TM, MachineModuleInfo *MMI, 90139f7f9bSDimitry Andric MCStreamer &Streamer) const { 91139f7f9bSDimitry Andric 92139f7f9bSDimitry Andric if (Encoding & dwarf::DW_EH_PE_indirect) { 93139f7f9bSDimitry Andric MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); 94139f7f9bSDimitry Andric 9591bc56edSDimitry Andric MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM); 96139f7f9bSDimitry Andric 97139f7f9bSDimitry Andric // Add information about the stub reference to ELFMMI so that the stub 98139f7f9bSDimitry Andric // gets emitted by the asmprinter. 99139f7f9bSDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); 10091bc56edSDimitry Andric if (!StubSym.getPointer()) { 10191bc56edSDimitry Andric MCSymbol *Sym = TM.getSymbol(GV, Mang); 102139f7f9bSDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 103139f7f9bSDimitry Andric } 104139f7f9bSDimitry Andric 105139f7f9bSDimitry Andric return TargetLoweringObjectFile:: 10697bc6c73SDimitry Andric getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 107139f7f9bSDimitry Andric Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 108139f7f9bSDimitry Andric } 109139f7f9bSDimitry Andric 110139f7f9bSDimitry Andric return TargetLoweringObjectFile:: 11191bc56edSDimitry Andric getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer); 112139f7f9bSDimitry Andric } 113139f7f9bSDimitry Andric 114f22ef01cSRoman Divacky static SectionKind 115f22ef01cSRoman Divacky getELFKindForNamedSection(StringRef Name, SectionKind K) { 116bd5abe19SDimitry Andric // N.B.: The defaults used in here are no the same ones used in MC. 117bd5abe19SDimitry Andric // We follow gcc, MC follows gas. For example, given ".section .eh_frame", 118bd5abe19SDimitry Andric // both gas and MC will produce a section with no flags. Given 1197ae0e2c9SDimitry Andric // section(".eh_frame") gcc will produce: 1207ae0e2c9SDimitry Andric // 121bd5abe19SDimitry Andric // .section .eh_frame,"a",@progbits 122f22ef01cSRoman Divacky if (Name.empty() || Name[0] != '.') return K; 123f22ef01cSRoman Divacky 124f22ef01cSRoman Divacky // Some lame default implementation based on some magic section names. 125f22ef01cSRoman Divacky if (Name == ".bss" || 126f22ef01cSRoman Divacky Name.startswith(".bss.") || 127f22ef01cSRoman Divacky Name.startswith(".gnu.linkonce.b.") || 128f22ef01cSRoman Divacky Name.startswith(".llvm.linkonce.b.") || 129f22ef01cSRoman Divacky Name == ".sbss" || 130f22ef01cSRoman Divacky Name.startswith(".sbss.") || 131f22ef01cSRoman Divacky Name.startswith(".gnu.linkonce.sb.") || 132f22ef01cSRoman Divacky Name.startswith(".llvm.linkonce.sb.")) 133f22ef01cSRoman Divacky return SectionKind::getBSS(); 134f22ef01cSRoman Divacky 135f22ef01cSRoman Divacky if (Name == ".tdata" || 136f22ef01cSRoman Divacky Name.startswith(".tdata.") || 137f22ef01cSRoman Divacky Name.startswith(".gnu.linkonce.td.") || 138f22ef01cSRoman Divacky Name.startswith(".llvm.linkonce.td.")) 139f22ef01cSRoman Divacky return SectionKind::getThreadData(); 140f22ef01cSRoman Divacky 141f22ef01cSRoman Divacky if (Name == ".tbss" || 142f22ef01cSRoman Divacky Name.startswith(".tbss.") || 143f22ef01cSRoman Divacky Name.startswith(".gnu.linkonce.tb.") || 144f22ef01cSRoman Divacky Name.startswith(".llvm.linkonce.tb.")) 145f22ef01cSRoman Divacky return SectionKind::getThreadBSS(); 146f22ef01cSRoman Divacky 147f22ef01cSRoman Divacky return K; 148f22ef01cSRoman Divacky } 149f22ef01cSRoman Divacky 150f22ef01cSRoman Divacky 151f22ef01cSRoman Divacky static unsigned getELFSectionType(StringRef Name, SectionKind K) { 152f22ef01cSRoman Divacky 153f22ef01cSRoman Divacky if (Name == ".init_array") 1542754fe60SDimitry Andric return ELF::SHT_INIT_ARRAY; 155f22ef01cSRoman Divacky 156f22ef01cSRoman Divacky if (Name == ".fini_array") 1572754fe60SDimitry Andric return ELF::SHT_FINI_ARRAY; 158f22ef01cSRoman Divacky 159f22ef01cSRoman Divacky if (Name == ".preinit_array") 1602754fe60SDimitry Andric return ELF::SHT_PREINIT_ARRAY; 161f22ef01cSRoman Divacky 162f22ef01cSRoman Divacky if (K.isBSS() || K.isThreadBSS()) 1632754fe60SDimitry Andric return ELF::SHT_NOBITS; 164f22ef01cSRoman Divacky 1652754fe60SDimitry Andric return ELF::SHT_PROGBITS; 166f22ef01cSRoman Divacky } 167f22ef01cSRoman Divacky 168ff0cc061SDimitry Andric static unsigned getELFSectionFlags(SectionKind K) { 169f22ef01cSRoman Divacky unsigned Flags = 0; 170f22ef01cSRoman Divacky 171f22ef01cSRoman Divacky if (!K.isMetadata()) 1722754fe60SDimitry Andric Flags |= ELF::SHF_ALLOC; 173f22ef01cSRoman Divacky 174f22ef01cSRoman Divacky if (K.isText()) 1752754fe60SDimitry Andric Flags |= ELF::SHF_EXECINSTR; 176f22ef01cSRoman Divacky 177f22ef01cSRoman Divacky if (K.isWriteable()) 1782754fe60SDimitry Andric Flags |= ELF::SHF_WRITE; 179f22ef01cSRoman Divacky 180f22ef01cSRoman Divacky if (K.isThreadLocal()) 1812754fe60SDimitry Andric Flags |= ELF::SHF_TLS; 182f22ef01cSRoman Divacky 183ff0cc061SDimitry Andric if (K.isMergeableCString() || K.isMergeableConst()) 1842754fe60SDimitry Andric Flags |= ELF::SHF_MERGE; 185f22ef01cSRoman Divacky 186f22ef01cSRoman Divacky if (K.isMergeableCString()) 1872754fe60SDimitry Andric Flags |= ELF::SHF_STRINGS; 188f22ef01cSRoman Divacky 189f22ef01cSRoman Divacky return Flags; 190f22ef01cSRoman Divacky } 191f22ef01cSRoman Divacky 19291bc56edSDimitry Andric static const Comdat *getELFComdat(const GlobalValue *GV) { 19391bc56edSDimitry Andric const Comdat *C = GV->getComdat(); 19491bc56edSDimitry Andric if (!C) 19591bc56edSDimitry Andric return nullptr; 196f22ef01cSRoman Divacky 19791bc56edSDimitry Andric if (C->getSelectionKind() != Comdat::Any) 19891bc56edSDimitry Andric report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" + 19991bc56edSDimitry Andric C->getName() + "' cannot be lowered."); 20091bc56edSDimitry Andric 20191bc56edSDimitry Andric return C; 20291bc56edSDimitry Andric } 20391bc56edSDimitry Andric 204ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( 20591bc56edSDimitry Andric const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 20691bc56edSDimitry Andric const TargetMachine &TM) const { 207f22ef01cSRoman Divacky StringRef SectionName = GV->getSection(); 208f22ef01cSRoman Divacky 209f22ef01cSRoman Divacky // Infer section flags from the section name if we can. 210f22ef01cSRoman Divacky Kind = getELFKindForNamedSection(SectionName, Kind); 211f22ef01cSRoman Divacky 21291bc56edSDimitry Andric StringRef Group = ""; 21391bc56edSDimitry Andric unsigned Flags = getELFSectionFlags(Kind); 21491bc56edSDimitry Andric if (const Comdat *C = getELFComdat(GV)) { 21591bc56edSDimitry Andric Group = C->getName(); 21691bc56edSDimitry Andric Flags |= ELF::SHF_GROUP; 21791bc56edSDimitry Andric } 218f22ef01cSRoman Divacky return getContext().getELFSection(SectionName, 21991bc56edSDimitry Andric getELFSectionType(SectionName, Kind), Flags, 220ff0cc061SDimitry Andric /*EntrySize=*/0, Group); 221f22ef01cSRoman Divacky } 222f22ef01cSRoman Divacky 223ff0cc061SDimitry Andric /// Return the section prefix name used by options FunctionsSections and 224ff0cc061SDimitry Andric /// DataSections. 22591bc56edSDimitry Andric static StringRef getSectionPrefixForGlobal(SectionKind Kind) { 226ff0cc061SDimitry Andric if (Kind.isText()) 227ff0cc061SDimitry Andric return ".text"; 228ff0cc061SDimitry Andric if (Kind.isReadOnly()) 229ff0cc061SDimitry Andric return ".rodata"; 230ff0cc061SDimitry Andric if (Kind.isBSS()) 231ff0cc061SDimitry Andric return ".bss"; 232ff0cc061SDimitry Andric if (Kind.isThreadData()) 233ff0cc061SDimitry Andric return ".tdata"; 234ff0cc061SDimitry Andric if (Kind.isThreadBSS()) 235ff0cc061SDimitry Andric return ".tbss"; 2367d523365SDimitry Andric if (Kind.isData()) 237ff0cc061SDimitry Andric return ".data"; 238f22ef01cSRoman Divacky assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 239ff0cc061SDimitry Andric return ".data.rel.ro"; 240f22ef01cSRoman Divacky } 241f22ef01cSRoman Divacky 242ff0cc061SDimitry Andric static MCSectionELF * 243ff0cc061SDimitry Andric selectELFSectionForGlobal(MCContext &Ctx, const GlobalValue *GV, 244ff0cc061SDimitry Andric SectionKind Kind, Mangler &Mang, 245ff0cc061SDimitry Andric const TargetMachine &TM, bool EmitUniqueSection, 246ff0cc061SDimitry Andric unsigned Flags, unsigned *NextUniqueID) { 247ff0cc061SDimitry Andric unsigned EntrySize = 0; 248ff0cc061SDimitry Andric if (Kind.isMergeableCString()) { 249ff0cc061SDimitry Andric if (Kind.isMergeable2ByteCString()) { 250ff0cc061SDimitry Andric EntrySize = 2; 251ff0cc061SDimitry Andric } else if (Kind.isMergeable4ByteCString()) { 252ff0cc061SDimitry Andric EntrySize = 4; 253ff0cc061SDimitry Andric } else { 254ff0cc061SDimitry Andric EntrySize = 1; 255ff0cc061SDimitry Andric assert(Kind.isMergeable1ByteCString() && "unknown string width"); 256ff0cc061SDimitry Andric } 257ff0cc061SDimitry Andric } else if (Kind.isMergeableConst()) { 258ff0cc061SDimitry Andric if (Kind.isMergeableConst4()) { 259ff0cc061SDimitry Andric EntrySize = 4; 260ff0cc061SDimitry Andric } else if (Kind.isMergeableConst8()) { 261ff0cc061SDimitry Andric EntrySize = 8; 262ff0cc061SDimitry Andric } else { 263ff0cc061SDimitry Andric assert(Kind.isMergeableConst16() && "unknown data width"); 264ff0cc061SDimitry Andric EntrySize = 16; 265ff0cc061SDimitry Andric } 266ff0cc061SDimitry Andric } 26791bc56edSDimitry Andric 2682754fe60SDimitry Andric StringRef Group = ""; 269ff0cc061SDimitry Andric if (const Comdat *C = getELFComdat(GV)) { 2702754fe60SDimitry Andric Flags |= ELF::SHF_GROUP; 271ff0cc061SDimitry Andric Group = C->getName(); 2722754fe60SDimitry Andric } 2732754fe60SDimitry Andric 274ff0cc061SDimitry Andric bool UniqueSectionNames = TM.getUniqueSectionNames(); 275ff0cc061SDimitry Andric SmallString<128> Name; 276ff0cc061SDimitry Andric if (Kind.isMergeableCString()) { 277f22ef01cSRoman Divacky // We also need alignment here. 278f22ef01cSRoman Divacky // FIXME: this is getting the alignment of the character, not the 279f22ef01cSRoman Divacky // alignment of the global! 2807d523365SDimitry Andric unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment( 2817d523365SDimitry Andric cast<GlobalVariable>(GV)); 282f22ef01cSRoman Divacky 283ff0cc061SDimitry Andric std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 284ff0cc061SDimitry Andric Name = SizeSpec + utostr(Align); 285ff0cc061SDimitry Andric } else if (Kind.isMergeableConst()) { 286ff0cc061SDimitry Andric Name = ".rodata.cst"; 287ff0cc061SDimitry Andric Name += utostr(EntrySize); 288ff0cc061SDimitry Andric } else { 289ff0cc061SDimitry Andric Name = getSectionPrefixForGlobal(Kind); 290ff0cc061SDimitry Andric } 291ff0cc061SDimitry Andric 292ff0cc061SDimitry Andric if (EmitUniqueSection && UniqueSectionNames) { 293ff0cc061SDimitry Andric Name.push_back('.'); 294ff0cc061SDimitry Andric TM.getNameWithPrefix(Name, GV, Mang, true); 295ff0cc061SDimitry Andric } 296ff0cc061SDimitry Andric unsigned UniqueID = ~0; 297ff0cc061SDimitry Andric if (EmitUniqueSection && !UniqueSectionNames) { 298ff0cc061SDimitry Andric UniqueID = *NextUniqueID; 299ff0cc061SDimitry Andric (*NextUniqueID)++; 300ff0cc061SDimitry Andric } 301ff0cc061SDimitry Andric return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, 302ff0cc061SDimitry Andric EntrySize, Group, UniqueID); 303ff0cc061SDimitry Andric } 304ff0cc061SDimitry Andric 305ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( 306ff0cc061SDimitry Andric const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 307ff0cc061SDimitry Andric const TargetMachine &TM) const { 308ff0cc061SDimitry Andric unsigned Flags = getELFSectionFlags(Kind); 309ff0cc061SDimitry Andric 310ff0cc061SDimitry Andric // If we have -ffunction-section or -fdata-section then we should emit the 311ff0cc061SDimitry Andric // global value to a uniqued section specifically for it. 312ff0cc061SDimitry Andric bool EmitUniqueSection = false; 313ff0cc061SDimitry Andric if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { 314ff0cc061SDimitry Andric if (Kind.isText()) 315ff0cc061SDimitry Andric EmitUniqueSection = TM.getFunctionSections(); 316f22ef01cSRoman Divacky else 317ff0cc061SDimitry Andric EmitUniqueSection = TM.getDataSections(); 318ff0cc061SDimitry Andric } 319ff0cc061SDimitry Andric EmitUniqueSection |= GV->hasComdat(); 320f22ef01cSRoman Divacky 321ff0cc061SDimitry Andric return selectELFSectionForGlobal(getContext(), GV, Kind, Mang, TM, 322ff0cc061SDimitry Andric EmitUniqueSection, Flags, &NextUniqueID); 323f22ef01cSRoman Divacky } 324f22ef01cSRoman Divacky 325ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( 326ff0cc061SDimitry Andric const Function &F, Mangler &Mang, const TargetMachine &TM) const { 327ff0cc061SDimitry Andric // If the function can be removed, produce a unique section so that 328ff0cc061SDimitry Andric // the table doesn't prevent the removal. 329ff0cc061SDimitry Andric const Comdat *C = F.getComdat(); 330ff0cc061SDimitry Andric bool EmitUniqueSection = TM.getFunctionSections() || C; 331ff0cc061SDimitry Andric if (!EmitUniqueSection) 332ff0cc061SDimitry Andric return ReadOnlySection; 333ff0cc061SDimitry Andric 334ff0cc061SDimitry Andric return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), 335ff0cc061SDimitry Andric Mang, TM, EmitUniqueSection, ELF::SHF_ALLOC, 336ff0cc061SDimitry Andric &NextUniqueID); 337f22ef01cSRoman Divacky } 338f22ef01cSRoman Divacky 339ff0cc061SDimitry Andric bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( 340ff0cc061SDimitry Andric bool UsesLabelDifference, const Function &F) const { 341ff0cc061SDimitry Andric // We can always create relative relocations, so use another section 342ff0cc061SDimitry Andric // that can be marked non-executable. 343ff0cc061SDimitry Andric return false; 344f22ef01cSRoman Divacky } 345f22ef01cSRoman Divacky 346ff0cc061SDimitry Andric /// Given a mergeable constant with the specified size and relocation 347ff0cc061SDimitry Andric /// information, return a section that it should be placed in. 3487d523365SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForConstant( 3497d523365SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C) const { 350f22ef01cSRoman Divacky if (Kind.isMergeableConst4() && MergeableConst4Section) 351f22ef01cSRoman Divacky return MergeableConst4Section; 352f22ef01cSRoman Divacky if (Kind.isMergeableConst8() && MergeableConst8Section) 353f22ef01cSRoman Divacky return MergeableConst8Section; 354f22ef01cSRoman Divacky if (Kind.isMergeableConst16() && MergeableConst16Section) 355f22ef01cSRoman Divacky return MergeableConst16Section; 356f22ef01cSRoman Divacky if (Kind.isReadOnly()) 357f22ef01cSRoman Divacky return ReadOnlySection; 358f22ef01cSRoman Divacky 359f22ef01cSRoman Divacky assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 360f22ef01cSRoman Divacky return DataRelROSection; 361f22ef01cSRoman Divacky } 362f22ef01cSRoman Divacky 363ff0cc061SDimitry Andric static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, 364ff0cc061SDimitry Andric bool IsCtor, unsigned Priority, 36539d628a0SDimitry Andric const MCSymbol *KeySym) { 36639d628a0SDimitry Andric std::string Name; 36739d628a0SDimitry Andric unsigned Type; 36839d628a0SDimitry Andric unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 36939d628a0SDimitry Andric StringRef COMDAT = KeySym ? KeySym->getName() : ""; 37039d628a0SDimitry Andric 37139d628a0SDimitry Andric if (KeySym) 37239d628a0SDimitry Andric Flags |= ELF::SHF_GROUP; 373dff0c46cSDimitry Andric 3747ae0e2c9SDimitry Andric if (UseInitArray) { 37539d628a0SDimitry Andric if (IsCtor) { 37639d628a0SDimitry Andric Type = ELF::SHT_INIT_ARRAY; 37739d628a0SDimitry Andric Name = ".init_array"; 3787ae0e2c9SDimitry Andric } else { 37939d628a0SDimitry Andric Type = ELF::SHT_FINI_ARRAY; 38039d628a0SDimitry Andric Name = ".fini_array"; 381dff0c46cSDimitry Andric } 38239d628a0SDimitry Andric if (Priority != 65535) { 38339d628a0SDimitry Andric Name += '.'; 38439d628a0SDimitry Andric Name += utostr(Priority); 38539d628a0SDimitry Andric } 38639d628a0SDimitry Andric } else { 38739d628a0SDimitry Andric // The default scheme is .ctor / .dtor, so we have to invert the priority 38839d628a0SDimitry Andric // numbering. 38939d628a0SDimitry Andric if (IsCtor) 39039d628a0SDimitry Andric Name = ".ctors"; 39139d628a0SDimitry Andric else 39239d628a0SDimitry Andric Name = ".dtors"; 39339d628a0SDimitry Andric if (Priority != 65535) { 39439d628a0SDimitry Andric Name += '.'; 39539d628a0SDimitry Andric Name += utostr(65535 - Priority); 39639d628a0SDimitry Andric } 39739d628a0SDimitry Andric Type = ELF::SHT_PROGBITS; 39839d628a0SDimitry Andric } 39939d628a0SDimitry Andric 400ff0cc061SDimitry Andric return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); 40139d628a0SDimitry Andric } 40239d628a0SDimitry Andric 403ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( 40439d628a0SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 40539d628a0SDimitry Andric return getStaticStructorSection(getContext(), UseInitArray, true, Priority, 40639d628a0SDimitry Andric KeySym); 4077ae0e2c9SDimitry Andric } 408dff0c46cSDimitry Andric 409ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( 41091bc56edSDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 41139d628a0SDimitry Andric return getStaticStructorSection(getContext(), UseInitArray, false, Priority, 41239d628a0SDimitry Andric KeySym); 4137ae0e2c9SDimitry Andric } 4147ae0e2c9SDimitry Andric 4157ae0e2c9SDimitry Andric void 4167ae0e2c9SDimitry Andric TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 4177ae0e2c9SDimitry Andric UseInitArray = UseInitArray_; 4187ae0e2c9SDimitry Andric if (!UseInitArray) 4197ae0e2c9SDimitry Andric return; 4207ae0e2c9SDimitry Andric 421ff0cc061SDimitry Andric StaticCtorSection = getContext().getELFSection( 422ff0cc061SDimitry Andric ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC); 423ff0cc061SDimitry Andric StaticDtorSection = getContext().getELFSection( 424ff0cc061SDimitry Andric ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC); 4257ae0e2c9SDimitry Andric } 426dff0c46cSDimitry Andric 427f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 428f22ef01cSRoman Divacky // MachO 429f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 430f22ef01cSRoman Divacky 431ff0cc061SDimitry Andric TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() 432ff0cc061SDimitry Andric : TargetLoweringObjectFile() { 433ff0cc061SDimitry Andric SupportIndirectSymViaGOTPCRel = true; 434ff0cc061SDimitry Andric } 435ff0cc061SDimitry Andric 436139f7f9bSDimitry Andric /// emitModuleFlags - Perform code emission for module flags. 437dff0c46cSDimitry Andric void TargetLoweringObjectFileMachO:: 438dff0c46cSDimitry Andric emitModuleFlags(MCStreamer &Streamer, 439dff0c46cSDimitry Andric ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 44091bc56edSDimitry Andric Mangler &Mang, const TargetMachine &TM) const { 441dff0c46cSDimitry Andric unsigned VersionVal = 0; 4427ae0e2c9SDimitry Andric unsigned ImageInfoFlags = 0; 44391bc56edSDimitry Andric MDNode *LinkerOptions = nullptr; 444dff0c46cSDimitry Andric StringRef SectionVal; 445dff0c46cSDimitry Andric 446dff0c46cSDimitry Andric for (ArrayRef<Module::ModuleFlagEntry>::iterator 447dff0c46cSDimitry Andric i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { 448dff0c46cSDimitry Andric const Module::ModuleFlagEntry &MFE = *i; 449dff0c46cSDimitry Andric 450dff0c46cSDimitry Andric // Ignore flags with 'Require' behavior. 451dff0c46cSDimitry Andric if (MFE.Behavior == Module::Require) 452dff0c46cSDimitry Andric continue; 453dff0c46cSDimitry Andric 454dff0c46cSDimitry Andric StringRef Key = MFE.Key->getString(); 45539d628a0SDimitry Andric Metadata *Val = MFE.Val; 456dff0c46cSDimitry Andric 457139f7f9bSDimitry Andric if (Key == "Objective-C Image Info Version") { 45839d628a0SDimitry Andric VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue(); 459139f7f9bSDimitry Andric } else if (Key == "Objective-C Garbage Collection" || 4607ae0e2c9SDimitry Andric Key == "Objective-C GC Only" || 46139d628a0SDimitry Andric Key == "Objective-C Is Simulated" || 46239d628a0SDimitry Andric Key == "Objective-C Image Swift Version") { 46339d628a0SDimitry Andric ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue(); 464139f7f9bSDimitry Andric } else if (Key == "Objective-C Image Info Section") { 465dff0c46cSDimitry Andric SectionVal = cast<MDString>(Val)->getString(); 466139f7f9bSDimitry Andric } else if (Key == "Linker Options") { 467139f7f9bSDimitry Andric LinkerOptions = cast<MDNode>(Val); 468139f7f9bSDimitry Andric } 469139f7f9bSDimitry Andric } 470139f7f9bSDimitry Andric 471139f7f9bSDimitry Andric // Emit the linker options if present. 472139f7f9bSDimitry Andric if (LinkerOptions) { 473139f7f9bSDimitry Andric for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { 474139f7f9bSDimitry Andric MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); 475139f7f9bSDimitry Andric SmallVector<std::string, 4> StrOptions; 476139f7f9bSDimitry Andric 477139f7f9bSDimitry Andric // Convert to strings. 478139f7f9bSDimitry Andric for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { 479139f7f9bSDimitry Andric MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); 480139f7f9bSDimitry Andric StrOptions.push_back(MDOption->getString()); 481139f7f9bSDimitry Andric } 482139f7f9bSDimitry Andric 483139f7f9bSDimitry Andric Streamer.EmitLinkerOptions(StrOptions); 484139f7f9bSDimitry Andric } 485dff0c46cSDimitry Andric } 486dff0c46cSDimitry Andric 487dff0c46cSDimitry Andric // The section is mandatory. If we don't have it, then we don't have GC info. 488dff0c46cSDimitry Andric if (SectionVal.empty()) return; 489dff0c46cSDimitry Andric 490dff0c46cSDimitry Andric StringRef Segment, Section; 491dff0c46cSDimitry Andric unsigned TAA = 0, StubSize = 0; 492dff0c46cSDimitry Andric bool TAAParsed; 493dff0c46cSDimitry Andric std::string ErrorCode = 494dff0c46cSDimitry Andric MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, 495dff0c46cSDimitry Andric TAA, TAAParsed, StubSize); 496dff0c46cSDimitry Andric if (!ErrorCode.empty()) 497dff0c46cSDimitry Andric // If invalid, report the error with report_fatal_error. 498dff0c46cSDimitry Andric report_fatal_error("Invalid section specifier '" + Section + "': " + 499dff0c46cSDimitry Andric ErrorCode + "."); 500dff0c46cSDimitry Andric 501dff0c46cSDimitry Andric // Get the section. 502ff0cc061SDimitry Andric MCSectionMachO *S = getContext().getMachOSection( 5037d523365SDimitry Andric Segment, Section, TAA, StubSize, SectionKind::getData()); 504dff0c46cSDimitry Andric Streamer.SwitchSection(S); 505dff0c46cSDimitry Andric Streamer.EmitLabel(getContext(). 506ff0cc061SDimitry Andric getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 507dff0c46cSDimitry Andric Streamer.EmitIntValue(VersionVal, 4); 5087ae0e2c9SDimitry Andric Streamer.EmitIntValue(ImageInfoFlags, 4); 509dff0c46cSDimitry Andric Streamer.AddBlankLine(); 510dff0c46cSDimitry Andric } 511dff0c46cSDimitry Andric 51291bc56edSDimitry Andric static void checkMachOComdat(const GlobalValue *GV) { 51391bc56edSDimitry Andric const Comdat *C = GV->getComdat(); 51491bc56edSDimitry Andric if (!C) 51591bc56edSDimitry Andric return; 51691bc56edSDimitry Andric 51791bc56edSDimitry Andric report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 51891bc56edSDimitry Andric "' cannot be lowered."); 51991bc56edSDimitry Andric } 52091bc56edSDimitry Andric 521ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 52291bc56edSDimitry Andric const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 52391bc56edSDimitry Andric const TargetMachine &TM) const { 524f22ef01cSRoman Divacky // Parse the section specifier and create it if valid. 525f22ef01cSRoman Divacky StringRef Segment, Section; 5263b0f4066SDimitry Andric unsigned TAA = 0, StubSize = 0; 5273b0f4066SDimitry Andric bool TAAParsed; 52891bc56edSDimitry Andric 52991bc56edSDimitry Andric checkMachOComdat(GV); 53091bc56edSDimitry Andric 531f22ef01cSRoman Divacky std::string ErrorCode = 532f22ef01cSRoman Divacky MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, 5333b0f4066SDimitry Andric TAA, TAAParsed, StubSize); 534f22ef01cSRoman Divacky if (!ErrorCode.empty()) { 535f22ef01cSRoman Divacky // If invalid, report the error with report_fatal_error. 536dff0c46cSDimitry Andric report_fatal_error("Global variable '" + GV->getName() + 537dff0c46cSDimitry Andric "' has an invalid section specifier '" + 538dff0c46cSDimitry Andric GV->getSection() + "': " + ErrorCode + "."); 539f22ef01cSRoman Divacky } 540f22ef01cSRoman Divacky 541f22ef01cSRoman Divacky // Get the section. 542ff0cc061SDimitry Andric MCSectionMachO *S = 543f22ef01cSRoman Divacky getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 544f22ef01cSRoman Divacky 545dd6029ffSDimitry Andric // If TAA wasn't set by ParseSectionSpecifier() above, 546dd6029ffSDimitry Andric // use the value returned by getMachOSection() as a default. 5473b0f4066SDimitry Andric if (!TAAParsed) 548dd6029ffSDimitry Andric TAA = S->getTypeAndAttributes(); 549dd6029ffSDimitry Andric 550f22ef01cSRoman Divacky // Okay, now that we got the section, verify that the TAA & StubSize agree. 551f22ef01cSRoman Divacky // If the user declared multiple globals with different section flags, we need 552f22ef01cSRoman Divacky // to reject it here. 553f22ef01cSRoman Divacky if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 554f22ef01cSRoman Divacky // If invalid, report the error with report_fatal_error. 555dff0c46cSDimitry Andric report_fatal_error("Global variable '" + GV->getName() + 556f22ef01cSRoman Divacky "' section type or attributes does not match previous" 557f22ef01cSRoman Divacky " section specifier"); 558f22ef01cSRoman Divacky } 559f22ef01cSRoman Divacky 560f22ef01cSRoman Divacky return S; 561f22ef01cSRoman Divacky } 562f22ef01cSRoman Divacky 563ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 564ff0cc061SDimitry Andric const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 565ff0cc061SDimitry Andric const TargetMachine &TM) const { 56691bc56edSDimitry Andric checkMachOComdat(GV); 567f785676fSDimitry Andric 568f785676fSDimitry Andric // Handle thread local data. 569f785676fSDimitry Andric if (Kind.isThreadBSS()) return TLSBSSSection; 570f785676fSDimitry Andric if (Kind.isThreadData()) return TLSDataSection; 571f785676fSDimitry Andric 572f22ef01cSRoman Divacky if (Kind.isText()) 573f22ef01cSRoman Divacky return GV->isWeakForLinker() ? TextCoalSection : TextSection; 574f22ef01cSRoman Divacky 575f22ef01cSRoman Divacky // If this is weak/linkonce, put this in a coalescable section, either in text 576f22ef01cSRoman Divacky // or data depending on if it is writable. 577f22ef01cSRoman Divacky if (GV->isWeakForLinker()) { 578f22ef01cSRoman Divacky if (Kind.isReadOnly()) 579f22ef01cSRoman Divacky return ConstTextCoalSection; 580f22ef01cSRoman Divacky return DataCoalSection; 581f22ef01cSRoman Divacky } 582f22ef01cSRoman Divacky 583f22ef01cSRoman Divacky // FIXME: Alignment check should be handled by section classifier. 584f22ef01cSRoman Divacky if (Kind.isMergeable1ByteCString() && 5857d523365SDimitry Andric GV->getParent()->getDataLayout().getPreferredAlignment( 5867d523365SDimitry Andric cast<GlobalVariable>(GV)) < 32) 587f22ef01cSRoman Divacky return CStringSection; 588f22ef01cSRoman Divacky 589f22ef01cSRoman Divacky // Do not put 16-bit arrays in the UString section if they have an 590f22ef01cSRoman Divacky // externally visible label, this runs into issues with certain linker 591f22ef01cSRoman Divacky // versions. 592f22ef01cSRoman Divacky if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && 5937d523365SDimitry Andric GV->getParent()->getDataLayout().getPreferredAlignment( 5947d523365SDimitry Andric cast<GlobalVariable>(GV)) < 32) 595f22ef01cSRoman Divacky return UStringSection; 596f22ef01cSRoman Divacky 59739d628a0SDimitry Andric // With MachO only variables whose corresponding symbol starts with 'l' or 59839d628a0SDimitry Andric // 'L' can be merged, so we only try merging GVs with private linkage. 59939d628a0SDimitry Andric if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) { 600f22ef01cSRoman Divacky if (Kind.isMergeableConst4()) 601f22ef01cSRoman Divacky return FourByteConstantSection; 602f22ef01cSRoman Divacky if (Kind.isMergeableConst8()) 603f22ef01cSRoman Divacky return EightByteConstantSection; 60491bc56edSDimitry Andric if (Kind.isMergeableConst16()) 605f22ef01cSRoman Divacky return SixteenByteConstantSection; 606f22ef01cSRoman Divacky } 607f22ef01cSRoman Divacky 608f22ef01cSRoman Divacky // Otherwise, if it is readonly, but not something we can specially optimize, 609f22ef01cSRoman Divacky // just drop it in .const. 610f22ef01cSRoman Divacky if (Kind.isReadOnly()) 611f22ef01cSRoman Divacky return ReadOnlySection; 612f22ef01cSRoman Divacky 613f22ef01cSRoman Divacky // If this is marked const, put it into a const section. But if the dynamic 614f22ef01cSRoman Divacky // linker needs to write to it, put it in the data segment. 615f22ef01cSRoman Divacky if (Kind.isReadOnlyWithRel()) 616f22ef01cSRoman Divacky return ConstDataSection; 617f22ef01cSRoman Divacky 618f22ef01cSRoman Divacky // Put zero initialized globals with strong external linkage in the 619f22ef01cSRoman Divacky // DATA, __common section with the .zerofill directive. 620f22ef01cSRoman Divacky if (Kind.isBSSExtern()) 621f22ef01cSRoman Divacky return DataCommonSection; 622f22ef01cSRoman Divacky 623f22ef01cSRoman Divacky // Put zero initialized globals with local linkage in __DATA,__bss directive 624f22ef01cSRoman Divacky // with the .zerofill directive (aka .lcomm). 625f22ef01cSRoman Divacky if (Kind.isBSSLocal()) 626f22ef01cSRoman Divacky return DataBSSSection; 627f22ef01cSRoman Divacky 628f22ef01cSRoman Divacky // Otherwise, just drop the variable in the normal data section. 629f22ef01cSRoman Divacky return DataSection; 630f22ef01cSRoman Divacky } 631f22ef01cSRoman Divacky 6327d523365SDimitry Andric MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 6337d523365SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C) const { 634f22ef01cSRoman Divacky // If this constant requires a relocation, we have to put it in the data 635f22ef01cSRoman Divacky // segment, not in the text segment. 6367d523365SDimitry Andric if (Kind.isData() || Kind.isReadOnlyWithRel()) 637f22ef01cSRoman Divacky return ConstDataSection; 638f22ef01cSRoman Divacky 639f22ef01cSRoman Divacky if (Kind.isMergeableConst4()) 640f22ef01cSRoman Divacky return FourByteConstantSection; 641f22ef01cSRoman Divacky if (Kind.isMergeableConst8()) 642f22ef01cSRoman Divacky return EightByteConstantSection; 64391bc56edSDimitry Andric if (Kind.isMergeableConst16()) 644f22ef01cSRoman Divacky return SixteenByteConstantSection; 645f22ef01cSRoman Divacky return ReadOnlySection; // .const 646f22ef01cSRoman Divacky } 647f22ef01cSRoman Divacky 64891bc56edSDimitry Andric const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 64991bc56edSDimitry Andric const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 65091bc56edSDimitry Andric const TargetMachine &TM, MachineModuleInfo *MMI, 651f22ef01cSRoman Divacky MCStreamer &Streamer) const { 652f22ef01cSRoman Divacky // The mach-o version of this method defaults to returning a stub reference. 653f22ef01cSRoman Divacky 654f22ef01cSRoman Divacky if (Encoding & DW_EH_PE_indirect) { 655f22ef01cSRoman Divacky MachineModuleInfoMachO &MachOMMI = 656f22ef01cSRoman Divacky MMI->getObjFileInfo<MachineModuleInfoMachO>(); 657f22ef01cSRoman Divacky 65891bc56edSDimitry Andric MCSymbol *SSym = 65991bc56edSDimitry Andric getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); 660f22ef01cSRoman Divacky 661f22ef01cSRoman Divacky // Add information about the stub reference to MachOMMI so that the stub 662f22ef01cSRoman Divacky // gets emitted by the asmprinter. 663f8254f43SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = 664f8254f43SDimitry Andric GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) : 665f8254f43SDimitry Andric MachOMMI.getGVStubEntry(SSym); 66691bc56edSDimitry Andric if (!StubSym.getPointer()) { 66791bc56edSDimitry Andric MCSymbol *Sym = TM.getSymbol(GV, Mang); 668f22ef01cSRoman Divacky StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 669f22ef01cSRoman Divacky } 670f22ef01cSRoman Divacky 671f22ef01cSRoman Divacky return TargetLoweringObjectFile:: 67297bc6c73SDimitry Andric getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 673139f7f9bSDimitry Andric Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 674f22ef01cSRoman Divacky } 675f22ef01cSRoman Divacky 67691bc56edSDimitry Andric return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang, 67791bc56edSDimitry Andric TM, MMI, Streamer); 678f22ef01cSRoman Divacky } 679f22ef01cSRoman Divacky 68091bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 68191bc56edSDimitry Andric const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 6823b0f4066SDimitry Andric MachineModuleInfo *MMI) const { 6833b0f4066SDimitry Andric // The mach-o version of this method defaults to returning a stub reference. 6843b0f4066SDimitry Andric MachineModuleInfoMachO &MachOMMI = 6853b0f4066SDimitry Andric MMI->getObjFileInfo<MachineModuleInfoMachO>(); 6863b0f4066SDimitry Andric 68791bc56edSDimitry Andric MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); 6883b0f4066SDimitry Andric 6893b0f4066SDimitry Andric // Add information about the stub reference to MachOMMI so that the stub 6903b0f4066SDimitry Andric // gets emitted by the asmprinter. 691dff0c46cSDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 69291bc56edSDimitry Andric if (!StubSym.getPointer()) { 69391bc56edSDimitry Andric MCSymbol *Sym = TM.getSymbol(GV, Mang); 6943b0f4066SDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 6953b0f4066SDimitry Andric } 6963b0f4066SDimitry Andric 6973b0f4066SDimitry Andric return SSym; 6983b0f4066SDimitry Andric } 6993b0f4066SDimitry Andric 700ff0cc061SDimitry Andric const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 701ff0cc061SDimitry Andric const MCSymbol *Sym, const MCValue &MV, int64_t Offset, 702ff0cc061SDimitry Andric MachineModuleInfo *MMI, MCStreamer &Streamer) const { 7037d523365SDimitry Andric // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 704ff0cc061SDimitry Andric // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 705ff0cc061SDimitry Andric // through a non_lazy_ptr stub instead. One advantage is that it allows the 706ff0cc061SDimitry Andric // computation of deltas to final external symbols. Example: 707ff0cc061SDimitry Andric // 708ff0cc061SDimitry Andric // _extgotequiv: 709ff0cc061SDimitry Andric // .long _extfoo 710ff0cc061SDimitry Andric // 711ff0cc061SDimitry Andric // _delta: 712ff0cc061SDimitry Andric // .long _extgotequiv-_delta 713ff0cc061SDimitry Andric // 714ff0cc061SDimitry Andric // is transformed to: 715ff0cc061SDimitry Andric // 716ff0cc061SDimitry Andric // _delta: 717ff0cc061SDimitry Andric // .long L_extfoo$non_lazy_ptr-(_delta+0) 718ff0cc061SDimitry Andric // 719ff0cc061SDimitry Andric // .section __IMPORT,__pointers,non_lazy_symbol_pointers 720ff0cc061SDimitry Andric // L_extfoo$non_lazy_ptr: 721ff0cc061SDimitry Andric // .indirect_symbol _extfoo 722ff0cc061SDimitry Andric // .long 0 723ff0cc061SDimitry Andric // 724ff0cc061SDimitry Andric MachineModuleInfoMachO &MachOMMI = 725ff0cc061SDimitry Andric MMI->getObjFileInfo<MachineModuleInfoMachO>(); 726ff0cc061SDimitry Andric MCContext &Ctx = getContext(); 727ff0cc061SDimitry Andric 728ff0cc061SDimitry Andric // The offset must consider the original displacement from the base symbol 729ff0cc061SDimitry Andric // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 730ff0cc061SDimitry Andric Offset = -MV.getConstant(); 731ff0cc061SDimitry Andric const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 732ff0cc061SDimitry Andric 733ff0cc061SDimitry Andric // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 734ff0cc061SDimitry Andric // non_lazy_ptr stubs. 735ff0cc061SDimitry Andric SmallString<128> Name; 736ff0cc061SDimitry Andric StringRef Suffix = "$non_lazy_ptr"; 7377d523365SDimitry Andric Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 738ff0cc061SDimitry Andric Name += Sym->getName(); 739ff0cc061SDimitry Andric Name += Suffix; 740ff0cc061SDimitry Andric MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 741ff0cc061SDimitry Andric 742ff0cc061SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 743ff0cc061SDimitry Andric if (!StubSym.getPointer()) 744ff0cc061SDimitry Andric StubSym = MachineModuleInfoImpl:: 745ff0cc061SDimitry Andric StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */); 746ff0cc061SDimitry Andric 747ff0cc061SDimitry Andric const MCExpr *BSymExpr = 74897bc6c73SDimitry Andric MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 749ff0cc061SDimitry Andric const MCExpr *LHS = 75097bc6c73SDimitry Andric MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 751ff0cc061SDimitry Andric 752ff0cc061SDimitry Andric if (!Offset) 75397bc6c73SDimitry Andric return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 754ff0cc061SDimitry Andric 755ff0cc061SDimitry Andric const MCExpr *RHS = 75697bc6c73SDimitry Andric MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 75797bc6c73SDimitry Andric return MCBinaryExpr::createSub(LHS, RHS, Ctx); 758ff0cc061SDimitry Andric } 759ff0cc061SDimitry Andric 7607d523365SDimitry Andric static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 7617d523365SDimitry Andric const MCSection &Section) { 7627d523365SDimitry Andric if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 7637d523365SDimitry Andric return true; 7647d523365SDimitry Andric 7657d523365SDimitry Andric // If it is not dead stripped, it is safe to use private labels. 7667d523365SDimitry Andric const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 7677d523365SDimitry Andric if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 7687d523365SDimitry Andric return true; 7697d523365SDimitry Andric 7707d523365SDimitry Andric return false; 7717d523365SDimitry Andric } 7727d523365SDimitry Andric 7737d523365SDimitry Andric void TargetLoweringObjectFileMachO::getNameWithPrefix( 7747d523365SDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, 7757d523365SDimitry Andric const TargetMachine &TM) const { 7767d523365SDimitry Andric SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); 7777d523365SDimitry Andric const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM); 7787d523365SDimitry Andric bool CannotUsePrivateLabel = 7797d523365SDimitry Andric !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 7807d523365SDimitry Andric Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 7817d523365SDimitry Andric } 7827d523365SDimitry Andric 783f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 784f22ef01cSRoman Divacky // COFF 785f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 786f22ef01cSRoman Divacky 787f22ef01cSRoman Divacky static unsigned 788f22ef01cSRoman Divacky getCOFFSectionFlags(SectionKind K) { 789f22ef01cSRoman Divacky unsigned Flags = 0; 790f22ef01cSRoman Divacky 791ffd1746dSEd Schouten if (K.isMetadata()) 792f22ef01cSRoman Divacky Flags |= 793ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE; 794f22ef01cSRoman Divacky else if (K.isText()) 795f22ef01cSRoman Divacky Flags |= 796ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_EXECUTE | 7972754fe60SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 798ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_CODE; 799f22ef01cSRoman Divacky else if (K.isBSS()) 800f22ef01cSRoman Divacky Flags |= 801ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 802ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ | 803ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_WRITE; 804dff0c46cSDimitry Andric else if (K.isThreadLocal()) 805dff0c46cSDimitry Andric Flags |= 806dff0c46cSDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 807dff0c46cSDimitry Andric COFF::IMAGE_SCN_MEM_READ | 808dff0c46cSDimitry Andric COFF::IMAGE_SCN_MEM_WRITE; 80939d628a0SDimitry Andric else if (K.isReadOnly() || K.isReadOnlyWithRel()) 810f22ef01cSRoman Divacky Flags |= 811ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 812ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ; 813f22ef01cSRoman Divacky else if (K.isWriteable()) 814f22ef01cSRoman Divacky Flags |= 815ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 816ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ | 817ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_WRITE; 818f22ef01cSRoman Divacky 819f22ef01cSRoman Divacky return Flags; 820f22ef01cSRoman Divacky } 821f22ef01cSRoman Divacky 82291bc56edSDimitry Andric static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 82391bc56edSDimitry Andric const Comdat *C = GV->getComdat(); 82491bc56edSDimitry Andric assert(C && "expected GV to have a Comdat!"); 82591bc56edSDimitry Andric 82691bc56edSDimitry Andric StringRef ComdatGVName = C->getName(); 82791bc56edSDimitry Andric const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 82891bc56edSDimitry Andric if (!ComdatGV) 82991bc56edSDimitry Andric report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 83091bc56edSDimitry Andric "' does not exist."); 83191bc56edSDimitry Andric 83291bc56edSDimitry Andric if (ComdatGV->getComdat() != C) 83391bc56edSDimitry Andric report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 83439d628a0SDimitry Andric "' is not a key for its COMDAT."); 83591bc56edSDimitry Andric 83691bc56edSDimitry Andric return ComdatGV; 83791bc56edSDimitry Andric } 83891bc56edSDimitry Andric 83991bc56edSDimitry Andric static int getSelectionForCOFF(const GlobalValue *GV) { 84091bc56edSDimitry Andric if (const Comdat *C = GV->getComdat()) { 84191bc56edSDimitry Andric const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 84291bc56edSDimitry Andric if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 84391bc56edSDimitry Andric ComdatKey = GA->getBaseObject(); 84491bc56edSDimitry Andric if (ComdatKey == GV) { 84591bc56edSDimitry Andric switch (C->getSelectionKind()) { 84691bc56edSDimitry Andric case Comdat::Any: 84791bc56edSDimitry Andric return COFF::IMAGE_COMDAT_SELECT_ANY; 84891bc56edSDimitry Andric case Comdat::ExactMatch: 84991bc56edSDimitry Andric return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 85091bc56edSDimitry Andric case Comdat::Largest: 85191bc56edSDimitry Andric return COFF::IMAGE_COMDAT_SELECT_LARGEST; 85291bc56edSDimitry Andric case Comdat::NoDuplicates: 85391bc56edSDimitry Andric return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 85491bc56edSDimitry Andric case Comdat::SameSize: 85591bc56edSDimitry Andric return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 85691bc56edSDimitry Andric } 85791bc56edSDimitry Andric } else { 85891bc56edSDimitry Andric return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 85991bc56edSDimitry Andric } 86091bc56edSDimitry Andric } 86191bc56edSDimitry Andric return 0; 86291bc56edSDimitry Andric } 86391bc56edSDimitry Andric 864ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 86591bc56edSDimitry Andric const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 86691bc56edSDimitry Andric const TargetMachine &TM) const { 867139f7f9bSDimitry Andric int Selection = 0; 868139f7f9bSDimitry Andric unsigned Characteristics = getCOFFSectionFlags(Kind); 86991bc56edSDimitry Andric StringRef Name = GV->getSection(); 87091bc56edSDimitry Andric StringRef COMDATSymName = ""; 871ff0cc061SDimitry Andric if (GV->hasComdat()) { 87291bc56edSDimitry Andric Selection = getSelectionForCOFF(GV); 87391bc56edSDimitry Andric const GlobalValue *ComdatGV; 87491bc56edSDimitry Andric if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 87591bc56edSDimitry Andric ComdatGV = getComdatGVForCOFF(GV); 87691bc56edSDimitry Andric else 87791bc56edSDimitry Andric ComdatGV = GV; 87891bc56edSDimitry Andric 87991bc56edSDimitry Andric if (!ComdatGV->hasPrivateLinkage()) { 88091bc56edSDimitry Andric MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); 88191bc56edSDimitry Andric COMDATSymName = Sym->getName(); 882139f7f9bSDimitry Andric Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 88391bc56edSDimitry Andric } else { 88491bc56edSDimitry Andric Selection = 0; 88591bc56edSDimitry Andric } 886139f7f9bSDimitry Andric } 887139f7f9bSDimitry Andric return getContext().getCOFFSection(Name, 888139f7f9bSDimitry Andric Characteristics, 889f785676fSDimitry Andric Kind, 89091bc56edSDimitry Andric COMDATSymName, 891f785676fSDimitry Andric Selection); 892f22ef01cSRoman Divacky } 893f22ef01cSRoman Divacky 89491bc56edSDimitry Andric static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 895f22ef01cSRoman Divacky if (Kind.isText()) 89691bc56edSDimitry Andric return ".text"; 897f22ef01cSRoman Divacky if (Kind.isBSS()) 89891bc56edSDimitry Andric return ".bss"; 89991bc56edSDimitry Andric if (Kind.isThreadLocal()) 90091bc56edSDimitry Andric return ".tls$"; 90139d628a0SDimitry Andric if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 90291bc56edSDimitry Andric return ".rdata"; 90339d628a0SDimitry Andric return ".data"; 904f22ef01cSRoman Divacky } 905f22ef01cSRoman Divacky 906ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 907ff0cc061SDimitry Andric const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 908ff0cc061SDimitry Andric const TargetMachine &TM) const { 90991bc56edSDimitry Andric // If we have -ffunction-sections then we should emit the global value to a 91091bc56edSDimitry Andric // uniqued section specifically for it. 91191bc56edSDimitry Andric bool EmitUniquedSection; 91291bc56edSDimitry Andric if (Kind.isText()) 91391bc56edSDimitry Andric EmitUniquedSection = TM.getFunctionSections(); 91491bc56edSDimitry Andric else 91591bc56edSDimitry Andric EmitUniquedSection = TM.getDataSections(); 916f22ef01cSRoman Divacky 917ff0cc061SDimitry Andric if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) { 91891bc56edSDimitry Andric const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 919f22ef01cSRoman Divacky unsigned Characteristics = getCOFFSectionFlags(Kind); 920f22ef01cSRoman Divacky 921ffd1746dSEd Schouten Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 92291bc56edSDimitry Andric int Selection = getSelectionForCOFF(GV); 92391bc56edSDimitry Andric if (!Selection) 92491bc56edSDimitry Andric Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 92591bc56edSDimitry Andric const GlobalValue *ComdatGV; 92691bc56edSDimitry Andric if (GV->hasComdat()) 92791bc56edSDimitry Andric ComdatGV = getComdatGVForCOFF(GV); 92891bc56edSDimitry Andric else 92991bc56edSDimitry Andric ComdatGV = GV; 930f22ef01cSRoman Divacky 93191bc56edSDimitry Andric if (!ComdatGV->hasPrivateLinkage()) { 93291bc56edSDimitry Andric MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); 93391bc56edSDimitry Andric StringRef COMDATSymName = Sym->getName(); 93491bc56edSDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, 93591bc56edSDimitry Andric COMDATSymName, Selection); 936ff0cc061SDimitry Andric } else { 937ff0cc061SDimitry Andric SmallString<256> TmpData; 9387d523365SDimitry Andric Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true); 939ff0cc061SDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 940ff0cc061SDimitry Andric Selection); 94191bc56edSDimitry Andric } 942f22ef01cSRoman Divacky } 943f22ef01cSRoman Divacky 944f22ef01cSRoman Divacky if (Kind.isText()) 945f785676fSDimitry Andric return TextSection; 946f22ef01cSRoman Divacky 947dff0c46cSDimitry Andric if (Kind.isThreadLocal()) 948f785676fSDimitry Andric return TLSDataSection; 949dff0c46cSDimitry Andric 95039d628a0SDimitry Andric if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 951f785676fSDimitry Andric return ReadOnlySection; 952f785676fSDimitry Andric 95391bc56edSDimitry Andric // Note: we claim that common symbols are put in BSSSection, but they are 95491bc56edSDimitry Andric // really emitted with the magic .comm directive, which creates a symbol table 95591bc56edSDimitry Andric // entry but not a section. 95691bc56edSDimitry Andric if (Kind.isBSS() || Kind.isCommon()) 957f785676fSDimitry Andric return BSSSection; 958f785676fSDimitry Andric 959f785676fSDimitry Andric return DataSection; 960f22ef01cSRoman Divacky } 961f22ef01cSRoman Divacky 962ff0cc061SDimitry Andric void TargetLoweringObjectFileCOFF::getNameWithPrefix( 9637d523365SDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, 9647d523365SDimitry Andric const TargetMachine &TM) const { 9657d523365SDimitry Andric bool CannotUsePrivateLabel = false; 966ff0cc061SDimitry Andric if (GV->hasPrivateLinkage() && 967ff0cc061SDimitry Andric ((isa<Function>(GV) && TM.getFunctionSections()) || 968ff0cc061SDimitry Andric (isa<GlobalVariable>(GV) && TM.getDataSections()))) 969ff0cc061SDimitry Andric CannotUsePrivateLabel = true; 970ff0cc061SDimitry Andric 971ff0cc061SDimitry Andric Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 972ff0cc061SDimitry Andric } 973ff0cc061SDimitry Andric 974ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 975ff0cc061SDimitry Andric const Function &F, Mangler &Mang, const TargetMachine &TM) const { 976ff0cc061SDimitry Andric // If the function can be removed, produce a unique section so that 977ff0cc061SDimitry Andric // the table doesn't prevent the removal. 978ff0cc061SDimitry Andric const Comdat *C = F.getComdat(); 979ff0cc061SDimitry Andric bool EmitUniqueSection = TM.getFunctionSections() || C; 980ff0cc061SDimitry Andric if (!EmitUniqueSection) 981ff0cc061SDimitry Andric return ReadOnlySection; 982ff0cc061SDimitry Andric 983ff0cc061SDimitry Andric // FIXME: we should produce a symbol for F instead. 984ff0cc061SDimitry Andric if (F.hasPrivateLinkage()) 985ff0cc061SDimitry Andric return ReadOnlySection; 986ff0cc061SDimitry Andric 987ff0cc061SDimitry Andric MCSymbol *Sym = TM.getSymbol(&F, Mang); 988ff0cc061SDimitry Andric StringRef COMDATSymName = Sym->getName(); 989ff0cc061SDimitry Andric 990ff0cc061SDimitry Andric SectionKind Kind = SectionKind::getReadOnly(); 991ff0cc061SDimitry Andric const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 992ff0cc061SDimitry Andric unsigned Characteristics = getCOFFSectionFlags(Kind); 993ff0cc061SDimitry Andric Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 994ff0cc061SDimitry Andric 995ff0cc061SDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 996ff0cc061SDimitry Andric COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE); 997ff0cc061SDimitry Andric } 998ff0cc061SDimitry Andric 999284c1978SDimitry Andric void TargetLoweringObjectFileCOFF:: 1000284c1978SDimitry Andric emitModuleFlags(MCStreamer &Streamer, 1001284c1978SDimitry Andric ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 100291bc56edSDimitry Andric Mangler &Mang, const TargetMachine &TM) const { 100391bc56edSDimitry Andric MDNode *LinkerOptions = nullptr; 1004284c1978SDimitry Andric 1005284c1978SDimitry Andric // Look for the "Linker Options" flag, since it's the only one we support. 1006284c1978SDimitry Andric for (ArrayRef<Module::ModuleFlagEntry>::iterator 1007284c1978SDimitry Andric i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { 1008284c1978SDimitry Andric const Module::ModuleFlagEntry &MFE = *i; 1009284c1978SDimitry Andric StringRef Key = MFE.Key->getString(); 101039d628a0SDimitry Andric Metadata *Val = MFE.Val; 1011284c1978SDimitry Andric if (Key == "Linker Options") { 1012284c1978SDimitry Andric LinkerOptions = cast<MDNode>(Val); 1013284c1978SDimitry Andric break; 1014284c1978SDimitry Andric } 1015284c1978SDimitry Andric } 1016284c1978SDimitry Andric if (!LinkerOptions) 1017284c1978SDimitry Andric return; 1018284c1978SDimitry Andric 1019284c1978SDimitry Andric // Emit the linker options to the linker .drectve section. According to the 1020284c1978SDimitry Andric // spec, this section is a space-separated string containing flags for linker. 1021ff0cc061SDimitry Andric MCSection *Sec = getDrectveSection(); 1022284c1978SDimitry Andric Streamer.SwitchSection(Sec); 1023284c1978SDimitry Andric for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { 1024284c1978SDimitry Andric MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); 1025284c1978SDimitry Andric for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { 1026284c1978SDimitry Andric MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); 1027284c1978SDimitry Andric // Lead with a space for consistency with our dllexport implementation. 1028ff0cc061SDimitry Andric std::string Directive(" "); 1029ff0cc061SDimitry Andric Directive.append(MDOption->getString()); 1030ff0cc061SDimitry Andric Streamer.EmitBytes(Directive); 1031284c1978SDimitry Andric } 1032284c1978SDimitry Andric } 1033284c1978SDimitry Andric } 103491bc56edSDimitry Andric 1035ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 103691bc56edSDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 103739d628a0SDimitry Andric return getContext().getAssociativeCOFFSection( 103839d628a0SDimitry Andric cast<MCSectionCOFF>(StaticCtorSection), KeySym); 103991bc56edSDimitry Andric } 104091bc56edSDimitry Andric 1041ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 104291bc56edSDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 104339d628a0SDimitry Andric return getContext().getAssociativeCOFFSection( 104439d628a0SDimitry Andric cast<MCSectionCOFF>(StaticDtorSection), KeySym); 104591bc56edSDimitry Andric } 10463dac3a9bSDimitry Andric 10473dac3a9bSDimitry Andric void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( 10483dac3a9bSDimitry Andric raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const { 10493dac3a9bSDimitry Andric if (!GV->hasDLLExportStorageClass() || GV->isDeclaration()) 10503dac3a9bSDimitry Andric return; 10513dac3a9bSDimitry Andric 10523dac3a9bSDimitry Andric const Triple &TT = getTargetTriple(); 10533dac3a9bSDimitry Andric 10543dac3a9bSDimitry Andric if (TT.isKnownWindowsMSVCEnvironment()) 10553dac3a9bSDimitry Andric OS << " /EXPORT:"; 10563dac3a9bSDimitry Andric else 10573dac3a9bSDimitry Andric OS << " -export:"; 10583dac3a9bSDimitry Andric 10593dac3a9bSDimitry Andric if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) { 10603dac3a9bSDimitry Andric std::string Flag; 10613dac3a9bSDimitry Andric raw_string_ostream FlagOS(Flag); 10623dac3a9bSDimitry Andric Mang.getNameWithPrefix(FlagOS, GV, false); 10633dac3a9bSDimitry Andric FlagOS.flush(); 10647d523365SDimitry Andric if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix()) 10653dac3a9bSDimitry Andric OS << Flag.substr(1); 10663dac3a9bSDimitry Andric else 10673dac3a9bSDimitry Andric OS << Flag; 10683dac3a9bSDimitry Andric } else { 10693dac3a9bSDimitry Andric Mang.getNameWithPrefix(OS, GV, false); 10703dac3a9bSDimitry Andric } 10713dac3a9bSDimitry Andric 10723dac3a9bSDimitry Andric if (!GV->getValueType()->isFunctionTy()) { 10733dac3a9bSDimitry Andric if (TT.isKnownWindowsMSVCEnvironment()) 10743dac3a9bSDimitry Andric OS << ",DATA"; 10753dac3a9bSDimitry Andric else 10763dac3a9bSDimitry Andric OS << ",data"; 10773dac3a9bSDimitry Andric } 10783dac3a9bSDimitry Andric } 1079