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" 16f22ef01cSRoman Divacky #include "llvm/Constants.h" 17f22ef01cSRoman Divacky #include "llvm/DerivedTypes.h" 18f22ef01cSRoman Divacky #include "llvm/Function.h" 19f22ef01cSRoman Divacky #include "llvm/GlobalVariable.h" 20f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineModuleInfoImpls.h" 21f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h" 22f22ef01cSRoman Divacky #include "llvm/MC/MCExpr.h" 23f22ef01cSRoman Divacky #include "llvm/MC/MCSectionMachO.h" 24f22ef01cSRoman Divacky #include "llvm/MC/MCSectionELF.h" 25f22ef01cSRoman Divacky #include "llvm/MC/MCSectionCOFF.h" 26f22ef01cSRoman Divacky #include "llvm/MC/MCSymbol.h" 27f22ef01cSRoman Divacky #include "llvm/Target/Mangler.h" 28f22ef01cSRoman Divacky #include "llvm/Target/TargetData.h" 29f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h" 30f22ef01cSRoman Divacky #include "llvm/Target/TargetOptions.h" 31f22ef01cSRoman Divacky #include "llvm/Support/Dwarf.h" 322754fe60SDimitry Andric #include "llvm/Support/ELF.h" 33f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h" 34f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h" 35f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h" 36f22ef01cSRoman Divacky #include "llvm/ADT/StringExtras.h" 372754fe60SDimitry Andric #include "llvm/ADT/Triple.h" 38f22ef01cSRoman Divacky using namespace llvm; 39f22ef01cSRoman Divacky using namespace dwarf; 40f22ef01cSRoman Divacky 41f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 42f22ef01cSRoman Divacky // ELF 43f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 44f22ef01cSRoman Divacky 45f22ef01cSRoman Divacky void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, 46f22ef01cSRoman Divacky const TargetMachine &TM) { 47f22ef01cSRoman Divacky TargetLoweringObjectFile::Initialize(Ctx, TM); 48f22ef01cSRoman Divacky 49f22ef01cSRoman Divacky BSSSection = 502754fe60SDimitry Andric getContext().getELFSection(".bss", ELF::SHT_NOBITS, 512754fe60SDimitry Andric ELF::SHF_WRITE |ELF::SHF_ALLOC, 52f22ef01cSRoman Divacky SectionKind::getBSS()); 53f22ef01cSRoman Divacky 54f22ef01cSRoman Divacky TextSection = 552754fe60SDimitry Andric getContext().getELFSection(".text", ELF::SHT_PROGBITS, 562754fe60SDimitry Andric ELF::SHF_EXECINSTR | 572754fe60SDimitry Andric ELF::SHF_ALLOC, 58f22ef01cSRoman Divacky SectionKind::getText()); 59f22ef01cSRoman Divacky 60f22ef01cSRoman Divacky DataSection = 612754fe60SDimitry Andric getContext().getELFSection(".data", ELF::SHT_PROGBITS, 622754fe60SDimitry Andric ELF::SHF_WRITE |ELF::SHF_ALLOC, 63f22ef01cSRoman Divacky SectionKind::getDataRel()); 64f22ef01cSRoman Divacky 65f22ef01cSRoman Divacky ReadOnlySection = 662754fe60SDimitry Andric getContext().getELFSection(".rodata", ELF::SHT_PROGBITS, 672754fe60SDimitry Andric ELF::SHF_ALLOC, 68f22ef01cSRoman Divacky SectionKind::getReadOnly()); 69f22ef01cSRoman Divacky 70f22ef01cSRoman Divacky TLSDataSection = 712754fe60SDimitry Andric getContext().getELFSection(".tdata", ELF::SHT_PROGBITS, 722754fe60SDimitry Andric ELF::SHF_ALLOC | ELF::SHF_TLS | 732754fe60SDimitry Andric ELF::SHF_WRITE, 74f22ef01cSRoman Divacky SectionKind::getThreadData()); 75f22ef01cSRoman Divacky 76f22ef01cSRoman Divacky TLSBSSSection = 772754fe60SDimitry Andric getContext().getELFSection(".tbss", ELF::SHT_NOBITS, 782754fe60SDimitry Andric ELF::SHF_ALLOC | ELF::SHF_TLS | 792754fe60SDimitry Andric ELF::SHF_WRITE, 80f22ef01cSRoman Divacky SectionKind::getThreadBSS()); 81f22ef01cSRoman Divacky 82f22ef01cSRoman Divacky DataRelSection = 832754fe60SDimitry Andric getContext().getELFSection(".data.rel", ELF::SHT_PROGBITS, 842754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_WRITE, 85f22ef01cSRoman Divacky SectionKind::getDataRel()); 86f22ef01cSRoman Divacky 87f22ef01cSRoman Divacky DataRelLocalSection = 882754fe60SDimitry Andric getContext().getELFSection(".data.rel.local", ELF::SHT_PROGBITS, 892754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_WRITE, 90f22ef01cSRoman Divacky SectionKind::getDataRelLocal()); 91f22ef01cSRoman Divacky 92f22ef01cSRoman Divacky DataRelROSection = 932754fe60SDimitry Andric getContext().getELFSection(".data.rel.ro", ELF::SHT_PROGBITS, 942754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_WRITE, 95f22ef01cSRoman Divacky SectionKind::getReadOnlyWithRel()); 96f22ef01cSRoman Divacky 97f22ef01cSRoman Divacky DataRelROLocalSection = 982754fe60SDimitry Andric getContext().getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS, 992754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_WRITE, 100f22ef01cSRoman Divacky SectionKind::getReadOnlyWithRelLocal()); 101f22ef01cSRoman Divacky 102f22ef01cSRoman Divacky MergeableConst4Section = 1032754fe60SDimitry Andric getContext().getELFSection(".rodata.cst4", ELF::SHT_PROGBITS, 1042754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_MERGE, 105f22ef01cSRoman Divacky SectionKind::getMergeableConst4()); 106f22ef01cSRoman Divacky 107f22ef01cSRoman Divacky MergeableConst8Section = 1082754fe60SDimitry Andric getContext().getELFSection(".rodata.cst8", ELF::SHT_PROGBITS, 1092754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_MERGE, 110f22ef01cSRoman Divacky SectionKind::getMergeableConst8()); 111f22ef01cSRoman Divacky 112f22ef01cSRoman Divacky MergeableConst16Section = 1132754fe60SDimitry Andric getContext().getELFSection(".rodata.cst16", ELF::SHT_PROGBITS, 1142754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_MERGE, 115f22ef01cSRoman Divacky SectionKind::getMergeableConst16()); 116f22ef01cSRoman Divacky 117f22ef01cSRoman Divacky StaticCtorSection = 1182754fe60SDimitry Andric getContext().getELFSection(".ctors", ELF::SHT_PROGBITS, 1192754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_WRITE, 120f22ef01cSRoman Divacky SectionKind::getDataRel()); 121f22ef01cSRoman Divacky 122f22ef01cSRoman Divacky StaticDtorSection = 1232754fe60SDimitry Andric getContext().getELFSection(".dtors", ELF::SHT_PROGBITS, 1242754fe60SDimitry Andric ELF::SHF_ALLOC |ELF::SHF_WRITE, 125f22ef01cSRoman Divacky SectionKind::getDataRel()); 126f22ef01cSRoman Divacky 127f22ef01cSRoman Divacky // Exception Handling Sections. 128f22ef01cSRoman Divacky 129f22ef01cSRoman Divacky // FIXME: We're emitting LSDA info into a readonly section on ELF, even though 130f22ef01cSRoman Divacky // it contains relocatable pointers. In PIC mode, this is probably a big 131f22ef01cSRoman Divacky // runtime hit for C++ apps. Either the contents of the LSDA need to be 132f22ef01cSRoman Divacky // adjusted or this should be a data section. 133f22ef01cSRoman Divacky LSDASection = 1342754fe60SDimitry Andric getContext().getELFSection(".gcc_except_table", ELF::SHT_PROGBITS, 1352754fe60SDimitry Andric ELF::SHF_ALLOC, 136f22ef01cSRoman Divacky SectionKind::getReadOnly()); 137f22ef01cSRoman Divacky // Debug Info Sections. 138f22ef01cSRoman Divacky DwarfAbbrevSection = 1392754fe60SDimitry Andric getContext().getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0, 140f22ef01cSRoman Divacky SectionKind::getMetadata()); 141f22ef01cSRoman Divacky DwarfInfoSection = 1422754fe60SDimitry Andric getContext().getELFSection(".debug_info", ELF::SHT_PROGBITS, 0, 143f22ef01cSRoman Divacky SectionKind::getMetadata()); 144f22ef01cSRoman Divacky DwarfLineSection = 1452754fe60SDimitry Andric getContext().getELFSection(".debug_line", ELF::SHT_PROGBITS, 0, 146f22ef01cSRoman Divacky SectionKind::getMetadata()); 147f22ef01cSRoman Divacky DwarfFrameSection = 1482754fe60SDimitry Andric getContext().getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0, 149f22ef01cSRoman Divacky SectionKind::getMetadata()); 150f22ef01cSRoman Divacky DwarfPubNamesSection = 1512754fe60SDimitry Andric getContext().getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0, 152f22ef01cSRoman Divacky SectionKind::getMetadata()); 153f22ef01cSRoman Divacky DwarfPubTypesSection = 1542754fe60SDimitry Andric getContext().getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0, 155f22ef01cSRoman Divacky SectionKind::getMetadata()); 156f22ef01cSRoman Divacky DwarfStrSection = 1572754fe60SDimitry Andric getContext().getELFSection(".debug_str", ELF::SHT_PROGBITS, 0, 158f22ef01cSRoman Divacky SectionKind::getMetadata()); 159f22ef01cSRoman Divacky DwarfLocSection = 1602754fe60SDimitry Andric getContext().getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0, 161f22ef01cSRoman Divacky SectionKind::getMetadata()); 162f22ef01cSRoman Divacky DwarfARangesSection = 1632754fe60SDimitry Andric getContext().getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0, 164f22ef01cSRoman Divacky SectionKind::getMetadata()); 165f22ef01cSRoman Divacky DwarfRangesSection = 1662754fe60SDimitry Andric getContext().getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0, 167f22ef01cSRoman Divacky SectionKind::getMetadata()); 168f22ef01cSRoman Divacky DwarfMacroInfoSection = 1692754fe60SDimitry Andric getContext().getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0, 170f22ef01cSRoman Divacky SectionKind::getMetadata()); 171f22ef01cSRoman Divacky } 172f22ef01cSRoman Divacky 1732754fe60SDimitry Andric const MCSection *TargetLoweringObjectFileELF::getEHFrameSection() const { 1742754fe60SDimitry Andric return getContext().getELFSection(".eh_frame", ELF::SHT_PROGBITS, 1752754fe60SDimitry Andric ELF::SHF_ALLOC, 1762754fe60SDimitry Andric SectionKind::getDataRel()); 1772754fe60SDimitry Andric } 178f22ef01cSRoman Divacky 179f22ef01cSRoman Divacky static SectionKind 180f22ef01cSRoman Divacky getELFKindForNamedSection(StringRef Name, SectionKind K) { 181dd6029ffSDimitry Andric // FIXME: Why is this here? Codegen is should not be in the business 182dd6029ffSDimitry Andric // of figuring section flags. If the user wrote section(".eh_frame"), 183dd6029ffSDimitry Andric // we should just pass that to MC which will defer to the assembly 184dd6029ffSDimitry Andric // or use its default if producing an object file. 185f22ef01cSRoman Divacky if (Name.empty() || Name[0] != '.') return K; 186f22ef01cSRoman Divacky 187f22ef01cSRoman Divacky // Some lame default implementation based on some magic section names. 188f22ef01cSRoman Divacky if (Name == ".bss" || 189f22ef01cSRoman Divacky Name.startswith(".bss.") || 190f22ef01cSRoman Divacky Name.startswith(".gnu.linkonce.b.") || 191f22ef01cSRoman Divacky Name.startswith(".llvm.linkonce.b.") || 192f22ef01cSRoman Divacky Name == ".sbss" || 193f22ef01cSRoman Divacky Name.startswith(".sbss.") || 194f22ef01cSRoman Divacky Name.startswith(".gnu.linkonce.sb.") || 195f22ef01cSRoman Divacky Name.startswith(".llvm.linkonce.sb.")) 196f22ef01cSRoman Divacky return SectionKind::getBSS(); 197f22ef01cSRoman Divacky 198f22ef01cSRoman Divacky if (Name == ".tdata" || 199f22ef01cSRoman Divacky Name.startswith(".tdata.") || 200f22ef01cSRoman Divacky Name.startswith(".gnu.linkonce.td.") || 201f22ef01cSRoman Divacky Name.startswith(".llvm.linkonce.td.")) 202f22ef01cSRoman Divacky return SectionKind::getThreadData(); 203f22ef01cSRoman Divacky 204f22ef01cSRoman Divacky if (Name == ".tbss" || 205f22ef01cSRoman Divacky Name.startswith(".tbss.") || 206f22ef01cSRoman Divacky Name.startswith(".gnu.linkonce.tb.") || 207f22ef01cSRoman Divacky Name.startswith(".llvm.linkonce.tb.")) 208f22ef01cSRoman Divacky return SectionKind::getThreadBSS(); 209f22ef01cSRoman Divacky 210dd6029ffSDimitry Andric if (Name == ".eh_frame") 211dd6029ffSDimitry Andric return SectionKind::getDataRel(); 212dd6029ffSDimitry Andric 213f22ef01cSRoman Divacky return K; 214f22ef01cSRoman Divacky } 215f22ef01cSRoman Divacky 216f22ef01cSRoman Divacky 217f22ef01cSRoman Divacky static unsigned getELFSectionType(StringRef Name, SectionKind K) { 218f22ef01cSRoman Divacky 219f22ef01cSRoman Divacky if (Name == ".init_array") 2202754fe60SDimitry Andric return ELF::SHT_INIT_ARRAY; 221f22ef01cSRoman Divacky 222f22ef01cSRoman Divacky if (Name == ".fini_array") 2232754fe60SDimitry Andric return ELF::SHT_FINI_ARRAY; 224f22ef01cSRoman Divacky 225f22ef01cSRoman Divacky if (Name == ".preinit_array") 2262754fe60SDimitry Andric return ELF::SHT_PREINIT_ARRAY; 227f22ef01cSRoman Divacky 228f22ef01cSRoman Divacky if (K.isBSS() || K.isThreadBSS()) 2292754fe60SDimitry Andric return ELF::SHT_NOBITS; 230f22ef01cSRoman Divacky 2312754fe60SDimitry Andric return ELF::SHT_PROGBITS; 232f22ef01cSRoman Divacky } 233f22ef01cSRoman Divacky 234f22ef01cSRoman Divacky 235f22ef01cSRoman Divacky static unsigned 236f22ef01cSRoman Divacky getELFSectionFlags(SectionKind K) { 237f22ef01cSRoman Divacky unsigned Flags = 0; 238f22ef01cSRoman Divacky 239f22ef01cSRoman Divacky if (!K.isMetadata()) 2402754fe60SDimitry Andric Flags |= ELF::SHF_ALLOC; 241f22ef01cSRoman Divacky 242f22ef01cSRoman Divacky if (K.isText()) 2432754fe60SDimitry Andric Flags |= ELF::SHF_EXECINSTR; 244f22ef01cSRoman Divacky 245f22ef01cSRoman Divacky if (K.isWriteable()) 2462754fe60SDimitry Andric Flags |= ELF::SHF_WRITE; 247f22ef01cSRoman Divacky 248f22ef01cSRoman Divacky if (K.isThreadLocal()) 2492754fe60SDimitry Andric Flags |= ELF::SHF_TLS; 250f22ef01cSRoman Divacky 251f22ef01cSRoman Divacky // K.isMergeableConst() is left out to honour PR4650 252f22ef01cSRoman Divacky if (K.isMergeableCString() || K.isMergeableConst4() || 253f22ef01cSRoman Divacky K.isMergeableConst8() || K.isMergeableConst16()) 2542754fe60SDimitry Andric Flags |= ELF::SHF_MERGE; 255f22ef01cSRoman Divacky 256f22ef01cSRoman Divacky if (K.isMergeableCString()) 2572754fe60SDimitry Andric Flags |= ELF::SHF_STRINGS; 258f22ef01cSRoman Divacky 259f22ef01cSRoman Divacky return Flags; 260f22ef01cSRoman Divacky } 261f22ef01cSRoman Divacky 262f22ef01cSRoman Divacky 263f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF:: 264f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 265f22ef01cSRoman Divacky Mangler *Mang, const TargetMachine &TM) const { 266f22ef01cSRoman Divacky StringRef SectionName = GV->getSection(); 267f22ef01cSRoman Divacky 268f22ef01cSRoman Divacky // Infer section flags from the section name if we can. 269f22ef01cSRoman Divacky Kind = getELFKindForNamedSection(SectionName, Kind); 270f22ef01cSRoman Divacky 271f22ef01cSRoman Divacky return getContext().getELFSection(SectionName, 272f22ef01cSRoman Divacky getELFSectionType(SectionName, Kind), 2732754fe60SDimitry Andric getELFSectionFlags(Kind), Kind); 274f22ef01cSRoman Divacky } 275f22ef01cSRoman Divacky 276f22ef01cSRoman Divacky /// getSectionPrefixForGlobal - Return the section prefix name used by options 277f22ef01cSRoman Divacky /// FunctionsSections and DataSections. 278f22ef01cSRoman Divacky static const char *getSectionPrefixForGlobal(SectionKind Kind) { 279f22ef01cSRoman Divacky if (Kind.isText()) return ".text."; 280f22ef01cSRoman Divacky if (Kind.isReadOnly()) return ".rodata."; 281f22ef01cSRoman Divacky 282f22ef01cSRoman Divacky if (Kind.isThreadData()) return ".tdata."; 283f22ef01cSRoman Divacky if (Kind.isThreadBSS()) return ".tbss."; 284f22ef01cSRoman Divacky 285f22ef01cSRoman Divacky if (Kind.isDataNoRel()) return ".data."; 286f22ef01cSRoman Divacky if (Kind.isDataRelLocal()) return ".data.rel.local."; 287f22ef01cSRoman Divacky if (Kind.isDataRel()) return ".data.rel."; 288f22ef01cSRoman Divacky if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local."; 289f22ef01cSRoman Divacky 290f22ef01cSRoman Divacky assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 291f22ef01cSRoman Divacky return ".data.rel.ro."; 292f22ef01cSRoman Divacky } 293f22ef01cSRoman Divacky 294f22ef01cSRoman Divacky 295f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF:: 296f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 297f22ef01cSRoman Divacky Mangler *Mang, const TargetMachine &TM) const { 298f22ef01cSRoman Divacky // If we have -ffunction-section or -fdata-section then we should emit the 299f22ef01cSRoman Divacky // global value to a uniqued section specifically for it. 300f22ef01cSRoman Divacky bool EmitUniquedSection; 301f22ef01cSRoman Divacky if (Kind.isText()) 302f22ef01cSRoman Divacky EmitUniquedSection = TM.getFunctionSections(); 303f22ef01cSRoman Divacky else 304f22ef01cSRoman Divacky EmitUniquedSection = TM.getDataSections(); 305f22ef01cSRoman Divacky 306f22ef01cSRoman Divacky // If this global is linkonce/weak and the target handles this by emitting it 307f22ef01cSRoman Divacky // into a 'uniqued' section name, create and return the section now. 308f22ef01cSRoman Divacky if ((GV->isWeakForLinker() || EmitUniquedSection) && 309f22ef01cSRoman Divacky !Kind.isCommon() && !Kind.isBSS()) { 310f22ef01cSRoman Divacky const char *Prefix; 311f22ef01cSRoman Divacky Prefix = getSectionPrefixForGlobal(Kind); 312f22ef01cSRoman Divacky 313f22ef01cSRoman Divacky SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); 314f22ef01cSRoman Divacky MCSymbol *Sym = Mang->getSymbol(GV); 315f22ef01cSRoman Divacky Name.append(Sym->getName().begin(), Sym->getName().end()); 3162754fe60SDimitry Andric StringRef Group = ""; 3172754fe60SDimitry Andric unsigned Flags = getELFSectionFlags(Kind); 3182754fe60SDimitry Andric if (GV->isWeakForLinker()) { 3192754fe60SDimitry Andric Group = Sym->getName(); 3202754fe60SDimitry Andric Flags |= ELF::SHF_GROUP; 3212754fe60SDimitry Andric } 3222754fe60SDimitry Andric 323f22ef01cSRoman Divacky return getContext().getELFSection(Name.str(), 324f22ef01cSRoman Divacky getELFSectionType(Name.str(), Kind), 3252754fe60SDimitry Andric Flags, Kind, 0, Group); 326f22ef01cSRoman Divacky } 327f22ef01cSRoman Divacky 328f22ef01cSRoman Divacky if (Kind.isText()) return TextSection; 329f22ef01cSRoman Divacky 330f22ef01cSRoman Divacky if (Kind.isMergeable1ByteCString() || 331f22ef01cSRoman Divacky Kind.isMergeable2ByteCString() || 332f22ef01cSRoman Divacky Kind.isMergeable4ByteCString()) { 333f22ef01cSRoman Divacky 334f22ef01cSRoman Divacky // We also need alignment here. 335f22ef01cSRoman Divacky // FIXME: this is getting the alignment of the character, not the 336f22ef01cSRoman Divacky // alignment of the global! 337f22ef01cSRoman Divacky unsigned Align = 338f22ef01cSRoman Divacky TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); 339f22ef01cSRoman Divacky 340f22ef01cSRoman Divacky const char *SizeSpec = ".rodata.str1."; 341f22ef01cSRoman Divacky if (Kind.isMergeable2ByteCString()) 342f22ef01cSRoman Divacky SizeSpec = ".rodata.str2."; 343f22ef01cSRoman Divacky else if (Kind.isMergeable4ByteCString()) 344f22ef01cSRoman Divacky SizeSpec = ".rodata.str4."; 345f22ef01cSRoman Divacky else 346f22ef01cSRoman Divacky assert(Kind.isMergeable1ByteCString() && "unknown string width"); 347f22ef01cSRoman Divacky 348f22ef01cSRoman Divacky 349f22ef01cSRoman Divacky std::string Name = SizeSpec + utostr(Align); 3502754fe60SDimitry Andric return getContext().getELFSection(Name, ELF::SHT_PROGBITS, 3512754fe60SDimitry Andric ELF::SHF_ALLOC | 3522754fe60SDimitry Andric ELF::SHF_MERGE | 3532754fe60SDimitry Andric ELF::SHF_STRINGS, 354f22ef01cSRoman Divacky Kind); 355f22ef01cSRoman Divacky } 356f22ef01cSRoman Divacky 357f22ef01cSRoman Divacky if (Kind.isMergeableConst()) { 358f22ef01cSRoman Divacky if (Kind.isMergeableConst4() && MergeableConst4Section) 359f22ef01cSRoman Divacky return MergeableConst4Section; 360f22ef01cSRoman Divacky if (Kind.isMergeableConst8() && MergeableConst8Section) 361f22ef01cSRoman Divacky return MergeableConst8Section; 362f22ef01cSRoman Divacky if (Kind.isMergeableConst16() && MergeableConst16Section) 363f22ef01cSRoman Divacky return MergeableConst16Section; 364f22ef01cSRoman Divacky return ReadOnlySection; // .const 365f22ef01cSRoman Divacky } 366f22ef01cSRoman Divacky 367f22ef01cSRoman Divacky if (Kind.isReadOnly()) return ReadOnlySection; 368f22ef01cSRoman Divacky 369f22ef01cSRoman Divacky if (Kind.isThreadData()) return TLSDataSection; 370f22ef01cSRoman Divacky if (Kind.isThreadBSS()) return TLSBSSSection; 371f22ef01cSRoman Divacky 372f22ef01cSRoman Divacky // Note: we claim that common symbols are put in BSSSection, but they are 373f22ef01cSRoman Divacky // really emitted with the magic .comm directive, which creates a symbol table 374f22ef01cSRoman Divacky // entry but not a section. 375f22ef01cSRoman Divacky if (Kind.isBSS() || Kind.isCommon()) return BSSSection; 376f22ef01cSRoman Divacky 377f22ef01cSRoman Divacky if (Kind.isDataNoRel()) return DataSection; 378f22ef01cSRoman Divacky if (Kind.isDataRelLocal()) return DataRelLocalSection; 379f22ef01cSRoman Divacky if (Kind.isDataRel()) return DataRelSection; 380f22ef01cSRoman Divacky if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 381f22ef01cSRoman Divacky 382f22ef01cSRoman Divacky assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 383f22ef01cSRoman Divacky return DataRelROSection; 384f22ef01cSRoman Divacky } 385f22ef01cSRoman Divacky 386f22ef01cSRoman Divacky /// getSectionForConstant - Given a mergeable constant with the 387f22ef01cSRoman Divacky /// specified size and relocation information, return a section that it 388f22ef01cSRoman Divacky /// should be placed in. 389f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF:: 390f22ef01cSRoman Divacky getSectionForConstant(SectionKind Kind) const { 391f22ef01cSRoman Divacky if (Kind.isMergeableConst4() && MergeableConst4Section) 392f22ef01cSRoman Divacky return MergeableConst4Section; 393f22ef01cSRoman Divacky if (Kind.isMergeableConst8() && MergeableConst8Section) 394f22ef01cSRoman Divacky return MergeableConst8Section; 395f22ef01cSRoman Divacky if (Kind.isMergeableConst16() && MergeableConst16Section) 396f22ef01cSRoman Divacky return MergeableConst16Section; 397f22ef01cSRoman Divacky if (Kind.isReadOnly()) 398f22ef01cSRoman Divacky return ReadOnlySection; 399f22ef01cSRoman Divacky 400f22ef01cSRoman Divacky if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 401f22ef01cSRoman Divacky assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 402f22ef01cSRoman Divacky return DataRelROSection; 403f22ef01cSRoman Divacky } 404f22ef01cSRoman Divacky 405f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFileELF:: 406f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, 407f22ef01cSRoman Divacky MachineModuleInfo *MMI, 408f22ef01cSRoman Divacky unsigned Encoding, MCStreamer &Streamer) const { 409f22ef01cSRoman Divacky 410f22ef01cSRoman Divacky if (Encoding & dwarf::DW_EH_PE_indirect) { 411f22ef01cSRoman Divacky MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); 412f22ef01cSRoman Divacky 413f22ef01cSRoman Divacky SmallString<128> Name; 414f22ef01cSRoman Divacky Mang->getNameWithPrefix(Name, GV, true); 415f22ef01cSRoman Divacky Name += ".DW.stub"; 416f22ef01cSRoman Divacky 417f22ef01cSRoman Divacky // Add information about the stub reference to ELFMMI so that the stub 418f22ef01cSRoman Divacky // gets emitted by the asmprinter. 419f22ef01cSRoman Divacky MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); 420f22ef01cSRoman Divacky MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); 421f22ef01cSRoman Divacky if (StubSym.getPointer() == 0) { 422f22ef01cSRoman Divacky MCSymbol *Sym = Mang->getSymbol(GV); 423f22ef01cSRoman Divacky StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 424f22ef01cSRoman Divacky } 425f22ef01cSRoman Divacky 426f22ef01cSRoman Divacky return TargetLoweringObjectFile:: 427f22ef01cSRoman Divacky getExprForDwarfReference(SSym, Mang, MMI, 428f22ef01cSRoman Divacky Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 429f22ef01cSRoman Divacky } 430f22ef01cSRoman Divacky 431f22ef01cSRoman Divacky return TargetLoweringObjectFile:: 432f22ef01cSRoman Divacky getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); 433f22ef01cSRoman Divacky } 434f22ef01cSRoman Divacky 435f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 436f22ef01cSRoman Divacky // MachO 437f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 438f22ef01cSRoman Divacky 439f22ef01cSRoman Divacky void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 440f22ef01cSRoman Divacky const TargetMachine &TM) { 441f22ef01cSRoman Divacky // _foo.eh symbols are currently always exported so that the linker knows 442f22ef01cSRoman Divacky // about them. This is not necessary on 10.6 and later, but it 443f22ef01cSRoman Divacky // doesn't hurt anything. 444f22ef01cSRoman Divacky // FIXME: I need to get this from Triple. 445f22ef01cSRoman Divacky IsFunctionEHSymbolGlobal = true; 446f22ef01cSRoman Divacky IsFunctionEHFrameSymbolPrivate = false; 447f22ef01cSRoman Divacky SupportsWeakOmittedEHFrame = false; 448f22ef01cSRoman Divacky 4492754fe60SDimitry Andric Triple T(((LLVMTargetMachine&)TM).getTargetTriple()); 4502754fe60SDimitry Andric if (T.getOS() == Triple::Darwin) { 451dd6029ffSDimitry Andric switch (T.getDarwinMajorNumber()) { 452dd6029ffSDimitry Andric case 7: // 10.3 Panther. 453dd6029ffSDimitry Andric case 8: // 10.4 Tiger. 4542754fe60SDimitry Andric CommDirectiveSupportsAlignment = false; 455dd6029ffSDimitry Andric break; 456dd6029ffSDimitry Andric case 9: // 10.5 Leopard. 457dd6029ffSDimitry Andric case 10: // 10.6 SnowLeopard. 458dd6029ffSDimitry Andric break; 459dd6029ffSDimitry Andric } 4602754fe60SDimitry Andric } 4612754fe60SDimitry Andric 462f22ef01cSRoman Divacky TargetLoweringObjectFile::Initialize(Ctx, TM); 463f22ef01cSRoman Divacky 464f22ef01cSRoman Divacky TextSection // .text 465f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__text", 466f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 467f22ef01cSRoman Divacky SectionKind::getText()); 468f22ef01cSRoman Divacky DataSection // .data 469f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__data", 0, 470f22ef01cSRoman Divacky SectionKind::getDataRel()); 471f22ef01cSRoman Divacky 472f22ef01cSRoman Divacky TLSDataSection // .tdata 473f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__thread_data", 474f22ef01cSRoman Divacky MCSectionMachO::S_THREAD_LOCAL_REGULAR, 475f22ef01cSRoman Divacky SectionKind::getDataRel()); 476f22ef01cSRoman Divacky TLSBSSSection // .tbss 477f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__thread_bss", 478f22ef01cSRoman Divacky MCSectionMachO::S_THREAD_LOCAL_ZEROFILL, 479f22ef01cSRoman Divacky SectionKind::getThreadBSS()); 480f22ef01cSRoman Divacky 481f22ef01cSRoman Divacky // TODO: Verify datarel below. 482f22ef01cSRoman Divacky TLSTLVSection // .tlv 483f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__thread_vars", 484f22ef01cSRoman Divacky MCSectionMachO::S_THREAD_LOCAL_VARIABLES, 485f22ef01cSRoman Divacky SectionKind::getDataRel()); 486f22ef01cSRoman Divacky 487f22ef01cSRoman Divacky TLSThreadInitSection 488f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__thread_init", 489f22ef01cSRoman Divacky MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS, 490f22ef01cSRoman Divacky SectionKind::getDataRel()); 491f22ef01cSRoman Divacky 492f22ef01cSRoman Divacky CStringSection // .cstring 493f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__cstring", 494f22ef01cSRoman Divacky MCSectionMachO::S_CSTRING_LITERALS, 495f22ef01cSRoman Divacky SectionKind::getMergeable1ByteCString()); 496f22ef01cSRoman Divacky UStringSection 497f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT","__ustring", 0, 498f22ef01cSRoman Divacky SectionKind::getMergeable2ByteCString()); 499f22ef01cSRoman Divacky FourByteConstantSection // .literal4 500f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__literal4", 501f22ef01cSRoman Divacky MCSectionMachO::S_4BYTE_LITERALS, 502f22ef01cSRoman Divacky SectionKind::getMergeableConst4()); 503f22ef01cSRoman Divacky EightByteConstantSection // .literal8 504f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__literal8", 505f22ef01cSRoman Divacky MCSectionMachO::S_8BYTE_LITERALS, 506f22ef01cSRoman Divacky SectionKind::getMergeableConst8()); 507f22ef01cSRoman Divacky 508f22ef01cSRoman Divacky // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back 509f22ef01cSRoman Divacky // to using it in -static mode. 510f22ef01cSRoman Divacky SixteenByteConstantSection = 0; 511f22ef01cSRoman Divacky if (TM.getRelocationModel() != Reloc::Static && 512f22ef01cSRoman Divacky TM.getTargetData()->getPointerSize() == 32) 513f22ef01cSRoman Divacky SixteenByteConstantSection = // .literal16 514f22ef01cSRoman Divacky getContext().getMachOSection("__TEXT", "__literal16", 515f22ef01cSRoman Divacky MCSectionMachO::S_16BYTE_LITERALS, 516f22ef01cSRoman Divacky SectionKind::getMergeableConst16()); 517f22ef01cSRoman Divacky 518f22ef01cSRoman Divacky ReadOnlySection // .const 519f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__const", 0, 520f22ef01cSRoman Divacky SectionKind::getReadOnly()); 521f22ef01cSRoman Divacky 522f22ef01cSRoman Divacky TextCoalSection 523f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__textcoal_nt", 524f22ef01cSRoman Divacky MCSectionMachO::S_COALESCED | 525f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 526f22ef01cSRoman Divacky SectionKind::getText()); 527f22ef01cSRoman Divacky ConstTextCoalSection 528f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__const_coal", 529f22ef01cSRoman Divacky MCSectionMachO::S_COALESCED, 530e580952dSDimitry Andric SectionKind::getReadOnly()); 531f22ef01cSRoman Divacky ConstDataSection // .const_data 532f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__const", 0, 533f22ef01cSRoman Divacky SectionKind::getReadOnlyWithRel()); 534f22ef01cSRoman Divacky DataCoalSection 535f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA","__datacoal_nt", 536f22ef01cSRoman Divacky MCSectionMachO::S_COALESCED, 537f22ef01cSRoman Divacky SectionKind::getDataRel()); 538f22ef01cSRoman Divacky DataCommonSection 539f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA","__common", 540f22ef01cSRoman Divacky MCSectionMachO::S_ZEROFILL, 541f22ef01cSRoman Divacky SectionKind::getBSS()); 542f22ef01cSRoman Divacky DataBSSSection 543f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL, 544f22ef01cSRoman Divacky SectionKind::getBSS()); 545f22ef01cSRoman Divacky 546f22ef01cSRoman Divacky 547f22ef01cSRoman Divacky LazySymbolPointerSection 548f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__la_symbol_ptr", 549f22ef01cSRoman Divacky MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 550f22ef01cSRoman Divacky SectionKind::getMetadata()); 551f22ef01cSRoman Divacky NonLazySymbolPointerSection 552f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__nl_symbol_ptr", 553f22ef01cSRoman Divacky MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 554f22ef01cSRoman Divacky SectionKind::getMetadata()); 555f22ef01cSRoman Divacky 556f22ef01cSRoman Divacky if (TM.getRelocationModel() == Reloc::Static) { 557f22ef01cSRoman Divacky StaticCtorSection 558f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__constructor", 0, 559f22ef01cSRoman Divacky SectionKind::getDataRel()); 560f22ef01cSRoman Divacky StaticDtorSection 561f22ef01cSRoman Divacky = getContext().getMachOSection("__TEXT", "__destructor", 0, 562f22ef01cSRoman Divacky SectionKind::getDataRel()); 563f22ef01cSRoman Divacky } else { 564f22ef01cSRoman Divacky StaticCtorSection 565f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__mod_init_func", 566f22ef01cSRoman Divacky MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 567f22ef01cSRoman Divacky SectionKind::getDataRel()); 568f22ef01cSRoman Divacky StaticDtorSection 569f22ef01cSRoman Divacky = getContext().getMachOSection("__DATA", "__mod_term_func", 570f22ef01cSRoman Divacky MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 571f22ef01cSRoman Divacky SectionKind::getDataRel()); 572f22ef01cSRoman Divacky } 573f22ef01cSRoman Divacky 574f22ef01cSRoman Divacky // Exception Handling. 575f22ef01cSRoman Divacky LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0, 576f22ef01cSRoman Divacky SectionKind::getReadOnlyWithRel()); 577f22ef01cSRoman Divacky // Debug Information. 578f22ef01cSRoman Divacky DwarfAbbrevSection = 579f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_abbrev", 580f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 581f22ef01cSRoman Divacky SectionKind::getMetadata()); 582f22ef01cSRoman Divacky DwarfInfoSection = 583f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_info", 584f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 585f22ef01cSRoman Divacky SectionKind::getMetadata()); 586f22ef01cSRoman Divacky DwarfLineSection = 587f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_line", 588f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 589f22ef01cSRoman Divacky SectionKind::getMetadata()); 590f22ef01cSRoman Divacky DwarfFrameSection = 591f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_frame", 592f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 593f22ef01cSRoman Divacky SectionKind::getMetadata()); 594f22ef01cSRoman Divacky DwarfPubNamesSection = 595f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_pubnames", 596f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 597f22ef01cSRoman Divacky SectionKind::getMetadata()); 598f22ef01cSRoman Divacky DwarfPubTypesSection = 599f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_pubtypes", 600f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 601f22ef01cSRoman Divacky SectionKind::getMetadata()); 602f22ef01cSRoman Divacky DwarfStrSection = 603f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_str", 604f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 605f22ef01cSRoman Divacky SectionKind::getMetadata()); 606f22ef01cSRoman Divacky DwarfLocSection = 607f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_loc", 608f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 609f22ef01cSRoman Divacky SectionKind::getMetadata()); 610f22ef01cSRoman Divacky DwarfARangesSection = 611f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_aranges", 612f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 613f22ef01cSRoman Divacky SectionKind::getMetadata()); 614f22ef01cSRoman Divacky DwarfRangesSection = 615f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_ranges", 616f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 617f22ef01cSRoman Divacky SectionKind::getMetadata()); 618f22ef01cSRoman Divacky DwarfMacroInfoSection = 619f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_macinfo", 620f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 621f22ef01cSRoman Divacky SectionKind::getMetadata()); 622f22ef01cSRoman Divacky DwarfDebugInlineSection = 623f22ef01cSRoman Divacky getContext().getMachOSection("__DWARF", "__debug_inlined", 624f22ef01cSRoman Divacky MCSectionMachO::S_ATTR_DEBUG, 625f22ef01cSRoman Divacky SectionKind::getMetadata()); 626f22ef01cSRoman Divacky 627f22ef01cSRoman Divacky TLSExtraDataSection = TLSTLVSection; 628f22ef01cSRoman Divacky } 629f22ef01cSRoman Divacky 6302754fe60SDimitry Andric const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const { 6312754fe60SDimitry Andric return getContext().getMachOSection("__TEXT", "__eh_frame", 6322754fe60SDimitry Andric MCSectionMachO::S_COALESCED | 6332754fe60SDimitry Andric MCSectionMachO::S_ATTR_NO_TOC | 6342754fe60SDimitry Andric MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS | 6352754fe60SDimitry Andric MCSectionMachO::S_ATTR_LIVE_SUPPORT, 6362754fe60SDimitry Andric SectionKind::getReadOnly()); 6372754fe60SDimitry Andric } 6382754fe60SDimitry Andric 639f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileMachO:: 640f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 641f22ef01cSRoman Divacky Mangler *Mang, const TargetMachine &TM) const { 642f22ef01cSRoman Divacky // Parse the section specifier and create it if valid. 643f22ef01cSRoman Divacky StringRef Segment, Section; 644dd6029ffSDimitry Andric unsigned TAA = (unsigned)MCSectionMachO::SECTION_ATTRIBUTES, StubSize = 0; 645f22ef01cSRoman Divacky std::string ErrorCode = 646f22ef01cSRoman Divacky MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, 647f22ef01cSRoman Divacky TAA, StubSize); 648f22ef01cSRoman Divacky if (!ErrorCode.empty()) { 649f22ef01cSRoman Divacky // If invalid, report the error with report_fatal_error. 650f22ef01cSRoman Divacky report_fatal_error("Global variable '" + GV->getNameStr() + 651f22ef01cSRoman Divacky "' has an invalid section specifier '" + GV->getSection()+ 652f22ef01cSRoman Divacky "': " + ErrorCode + "."); 653f22ef01cSRoman Divacky // Fall back to dropping it into the data section. 654f22ef01cSRoman Divacky return DataSection; 655f22ef01cSRoman Divacky } 656f22ef01cSRoman Divacky 657dd6029ffSDimitry Andric bool TAAWasSet = (TAA != MCSectionMachO::SECTION_ATTRIBUTES); 658dd6029ffSDimitry Andric if (!TAAWasSet) 659dd6029ffSDimitry Andric TAA = 0; // Sensible default if this is a new section. 660dd6029ffSDimitry Andric 661f22ef01cSRoman Divacky // Get the section. 662f22ef01cSRoman Divacky const MCSectionMachO *S = 663f22ef01cSRoman Divacky getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 664f22ef01cSRoman Divacky 665dd6029ffSDimitry Andric // If TAA wasn't set by ParseSectionSpecifier() above, 666dd6029ffSDimitry Andric // use the value returned by getMachOSection() as a default. 667dd6029ffSDimitry Andric if (!TAAWasSet) 668dd6029ffSDimitry Andric TAA = S->getTypeAndAttributes(); 669dd6029ffSDimitry Andric 670f22ef01cSRoman Divacky // Okay, now that we got the section, verify that the TAA & StubSize agree. 671f22ef01cSRoman Divacky // If the user declared multiple globals with different section flags, we need 672f22ef01cSRoman Divacky // to reject it here. 673f22ef01cSRoman Divacky if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 674f22ef01cSRoman Divacky // If invalid, report the error with report_fatal_error. 675f22ef01cSRoman Divacky report_fatal_error("Global variable '" + GV->getNameStr() + 676f22ef01cSRoman Divacky "' section type or attributes does not match previous" 677f22ef01cSRoman Divacky " section specifier"); 678f22ef01cSRoman Divacky } 679f22ef01cSRoman Divacky 680f22ef01cSRoman Divacky return S; 681f22ef01cSRoman Divacky } 682f22ef01cSRoman Divacky 683f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileMachO:: 684f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 685f22ef01cSRoman Divacky Mangler *Mang, const TargetMachine &TM) const { 686f22ef01cSRoman Divacky 687f22ef01cSRoman Divacky // Handle thread local data. 688f22ef01cSRoman Divacky if (Kind.isThreadBSS()) return TLSBSSSection; 689f22ef01cSRoman Divacky if (Kind.isThreadData()) return TLSDataSection; 690f22ef01cSRoman Divacky 691f22ef01cSRoman Divacky if (Kind.isText()) 692f22ef01cSRoman Divacky return GV->isWeakForLinker() ? TextCoalSection : TextSection; 693f22ef01cSRoman Divacky 694f22ef01cSRoman Divacky // If this is weak/linkonce, put this in a coalescable section, either in text 695f22ef01cSRoman Divacky // or data depending on if it is writable. 696f22ef01cSRoman Divacky if (GV->isWeakForLinker()) { 697f22ef01cSRoman Divacky if (Kind.isReadOnly()) 698f22ef01cSRoman Divacky return ConstTextCoalSection; 699f22ef01cSRoman Divacky return DataCoalSection; 700f22ef01cSRoman Divacky } 701f22ef01cSRoman Divacky 702f22ef01cSRoman Divacky // FIXME: Alignment check should be handled by section classifier. 703f22ef01cSRoman Divacky if (Kind.isMergeable1ByteCString() && 704f22ef01cSRoman Divacky TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) 705f22ef01cSRoman Divacky return CStringSection; 706f22ef01cSRoman Divacky 707f22ef01cSRoman Divacky // Do not put 16-bit arrays in the UString section if they have an 708f22ef01cSRoman Divacky // externally visible label, this runs into issues with certain linker 709f22ef01cSRoman Divacky // versions. 710f22ef01cSRoman Divacky if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && 711f22ef01cSRoman Divacky TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) 712f22ef01cSRoman Divacky return UStringSection; 713f22ef01cSRoman Divacky 714f22ef01cSRoman Divacky if (Kind.isMergeableConst()) { 715f22ef01cSRoman Divacky if (Kind.isMergeableConst4()) 716f22ef01cSRoman Divacky return FourByteConstantSection; 717f22ef01cSRoman Divacky if (Kind.isMergeableConst8()) 718f22ef01cSRoman Divacky return EightByteConstantSection; 719f22ef01cSRoman Divacky if (Kind.isMergeableConst16() && SixteenByteConstantSection) 720f22ef01cSRoman Divacky return SixteenByteConstantSection; 721f22ef01cSRoman Divacky } 722f22ef01cSRoman Divacky 723f22ef01cSRoman Divacky // Otherwise, if it is readonly, but not something we can specially optimize, 724f22ef01cSRoman Divacky // just drop it in .const. 725f22ef01cSRoman Divacky if (Kind.isReadOnly()) 726f22ef01cSRoman Divacky return ReadOnlySection; 727f22ef01cSRoman Divacky 728f22ef01cSRoman Divacky // If this is marked const, put it into a const section. But if the dynamic 729f22ef01cSRoman Divacky // linker needs to write to it, put it in the data segment. 730f22ef01cSRoman Divacky if (Kind.isReadOnlyWithRel()) 731f22ef01cSRoman Divacky return ConstDataSection; 732f22ef01cSRoman Divacky 733f22ef01cSRoman Divacky // Put zero initialized globals with strong external linkage in the 734f22ef01cSRoman Divacky // DATA, __common section with the .zerofill directive. 735f22ef01cSRoman Divacky if (Kind.isBSSExtern()) 736f22ef01cSRoman Divacky return DataCommonSection; 737f22ef01cSRoman Divacky 738f22ef01cSRoman Divacky // Put zero initialized globals with local linkage in __DATA,__bss directive 739f22ef01cSRoman Divacky // with the .zerofill directive (aka .lcomm). 740f22ef01cSRoman Divacky if (Kind.isBSSLocal()) 741f22ef01cSRoman Divacky return DataBSSSection; 742f22ef01cSRoman Divacky 743f22ef01cSRoman Divacky // Otherwise, just drop the variable in the normal data section. 744f22ef01cSRoman Divacky return DataSection; 745f22ef01cSRoman Divacky } 746f22ef01cSRoman Divacky 747f22ef01cSRoman Divacky const MCSection * 748f22ef01cSRoman Divacky TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { 749f22ef01cSRoman Divacky // If this constant requires a relocation, we have to put it in the data 750f22ef01cSRoman Divacky // segment, not in the text segment. 751f22ef01cSRoman Divacky if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) 752f22ef01cSRoman Divacky return ConstDataSection; 753f22ef01cSRoman Divacky 754f22ef01cSRoman Divacky if (Kind.isMergeableConst4()) 755f22ef01cSRoman Divacky return FourByteConstantSection; 756f22ef01cSRoman Divacky if (Kind.isMergeableConst8()) 757f22ef01cSRoman Divacky return EightByteConstantSection; 758f22ef01cSRoman Divacky if (Kind.isMergeableConst16() && SixteenByteConstantSection) 759f22ef01cSRoman Divacky return SixteenByteConstantSection; 760f22ef01cSRoman Divacky return ReadOnlySection; // .const 761f22ef01cSRoman Divacky } 762f22ef01cSRoman Divacky 763f22ef01cSRoman Divacky /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide 764f22ef01cSRoman Divacky /// not to emit the UsedDirective for some symbols in llvm.used. 765f22ef01cSRoman Divacky // FIXME: REMOVE this (rdar://7071300) 766f22ef01cSRoman Divacky bool TargetLoweringObjectFileMachO:: 767f22ef01cSRoman Divacky shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { 768f22ef01cSRoman Divacky /// On Darwin, internally linked data beginning with "L" or "l" does not have 769f22ef01cSRoman Divacky /// the directive emitted (this occurs in ObjC metadata). 770f22ef01cSRoman Divacky if (!GV) return false; 771f22ef01cSRoman Divacky 772f22ef01cSRoman Divacky // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. 773f22ef01cSRoman Divacky if (GV->hasLocalLinkage() && !isa<Function>(GV)) { 774f22ef01cSRoman Divacky // FIXME: ObjC metadata is currently emitted as internal symbols that have 775f22ef01cSRoman Divacky // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and 776f22ef01cSRoman Divacky // this horrible hack can go away. 777f22ef01cSRoman Divacky MCSymbol *Sym = Mang->getSymbol(GV); 778f22ef01cSRoman Divacky if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l') 779f22ef01cSRoman Divacky return false; 780f22ef01cSRoman Divacky } 781f22ef01cSRoman Divacky 782f22ef01cSRoman Divacky return true; 783f22ef01cSRoman Divacky } 784f22ef01cSRoman Divacky 785f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFileMachO:: 786f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, 787f22ef01cSRoman Divacky MachineModuleInfo *MMI, unsigned Encoding, 788f22ef01cSRoman Divacky MCStreamer &Streamer) const { 789f22ef01cSRoman Divacky // The mach-o version of this method defaults to returning a stub reference. 790f22ef01cSRoman Divacky 791f22ef01cSRoman Divacky if (Encoding & DW_EH_PE_indirect) { 792f22ef01cSRoman Divacky MachineModuleInfoMachO &MachOMMI = 793f22ef01cSRoman Divacky MMI->getObjFileInfo<MachineModuleInfoMachO>(); 794f22ef01cSRoman Divacky 795f22ef01cSRoman Divacky SmallString<128> Name; 796f22ef01cSRoman Divacky Mang->getNameWithPrefix(Name, GV, true); 797f22ef01cSRoman Divacky Name += "$non_lazy_ptr"; 798f22ef01cSRoman Divacky 799f22ef01cSRoman Divacky // Add information about the stub reference to MachOMMI so that the stub 800f22ef01cSRoman Divacky // gets emitted by the asmprinter. 801f22ef01cSRoman Divacky MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); 802f22ef01cSRoman Divacky MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 803f22ef01cSRoman Divacky if (StubSym.getPointer() == 0) { 804f22ef01cSRoman Divacky MCSymbol *Sym = Mang->getSymbol(GV); 805f22ef01cSRoman Divacky StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 806f22ef01cSRoman Divacky } 807f22ef01cSRoman Divacky 808f22ef01cSRoman Divacky return TargetLoweringObjectFile:: 809f22ef01cSRoman Divacky getExprForDwarfReference(SSym, Mang, MMI, 810f22ef01cSRoman Divacky Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 811f22ef01cSRoman Divacky } 812f22ef01cSRoman Divacky 813f22ef01cSRoman Divacky return TargetLoweringObjectFile:: 814f22ef01cSRoman Divacky getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); 815f22ef01cSRoman Divacky } 816f22ef01cSRoman Divacky 817f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const { 818f22ef01cSRoman Divacky return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4; 819f22ef01cSRoman Divacky } 820f22ef01cSRoman Divacky 821f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const { 822f22ef01cSRoman Divacky return DW_EH_PE_pcrel; 823f22ef01cSRoman Divacky } 824f22ef01cSRoman Divacky 825f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const { 826f22ef01cSRoman Divacky return DW_EH_PE_pcrel; 827f22ef01cSRoman Divacky } 828f22ef01cSRoman Divacky 829f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const { 830f22ef01cSRoman Divacky return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4; 831f22ef01cSRoman Divacky } 832f22ef01cSRoman Divacky 833f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 834f22ef01cSRoman Divacky // COFF 835f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 836f22ef01cSRoman Divacky 837f22ef01cSRoman Divacky void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 838f22ef01cSRoman Divacky const TargetMachine &TM) { 839f22ef01cSRoman Divacky TargetLoweringObjectFile::Initialize(Ctx, TM); 840f22ef01cSRoman Divacky TextSection = 841f22ef01cSRoman Divacky getContext().getCOFFSection(".text", 842ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_CODE | 843ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_EXECUTE | 844ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 845f22ef01cSRoman Divacky SectionKind::getText()); 846f22ef01cSRoman Divacky DataSection = 847f22ef01cSRoman Divacky getContext().getCOFFSection(".data", 848ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 849ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ | 850ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_WRITE, 851f22ef01cSRoman Divacky SectionKind::getDataRel()); 852f22ef01cSRoman Divacky ReadOnlySection = 853f22ef01cSRoman Divacky getContext().getCOFFSection(".rdata", 854ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 855ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 856f22ef01cSRoman Divacky SectionKind::getReadOnly()); 857f22ef01cSRoman Divacky StaticCtorSection = 858f22ef01cSRoman Divacky getContext().getCOFFSection(".ctors", 859ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 860ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ | 861ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_WRITE, 862f22ef01cSRoman Divacky SectionKind::getDataRel()); 863f22ef01cSRoman Divacky StaticDtorSection = 864f22ef01cSRoman Divacky getContext().getCOFFSection(".dtors", 865ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 866ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ | 867ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_WRITE, 868f22ef01cSRoman Divacky SectionKind::getDataRel()); 869f22ef01cSRoman Divacky 870f22ef01cSRoman Divacky // FIXME: We're emitting LSDA info into a readonly section on COFF, even 871f22ef01cSRoman Divacky // though it contains relocatable pointers. In PIC mode, this is probably a 872f22ef01cSRoman Divacky // big runtime hit for C++ apps. Either the contents of the LSDA need to be 873f22ef01cSRoman Divacky // adjusted or this should be a data section. 874f22ef01cSRoman Divacky LSDASection = 875f22ef01cSRoman Divacky getContext().getCOFFSection(".gcc_except_table", 876ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 877ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 878f22ef01cSRoman Divacky SectionKind::getReadOnly()); 879f22ef01cSRoman Divacky // Debug info. 880f22ef01cSRoman Divacky DwarfAbbrevSection = 881f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_abbrev", 882ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 883ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 884f22ef01cSRoman Divacky SectionKind::getMetadata()); 885f22ef01cSRoman Divacky DwarfInfoSection = 886f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_info", 887ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 888ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 889f22ef01cSRoman Divacky SectionKind::getMetadata()); 890f22ef01cSRoman Divacky DwarfLineSection = 891f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_line", 892ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 893ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 894f22ef01cSRoman Divacky SectionKind::getMetadata()); 895f22ef01cSRoman Divacky DwarfFrameSection = 896f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_frame", 897ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 898ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 899f22ef01cSRoman Divacky SectionKind::getMetadata()); 900f22ef01cSRoman Divacky DwarfPubNamesSection = 901f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_pubnames", 902ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 903ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 904f22ef01cSRoman Divacky SectionKind::getMetadata()); 905f22ef01cSRoman Divacky DwarfPubTypesSection = 906f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_pubtypes", 907ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 908ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 909f22ef01cSRoman Divacky SectionKind::getMetadata()); 910f22ef01cSRoman Divacky DwarfStrSection = 911f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_str", 912ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 913ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 914f22ef01cSRoman Divacky SectionKind::getMetadata()); 915f22ef01cSRoman Divacky DwarfLocSection = 916f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_loc", 917ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 918ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 919f22ef01cSRoman Divacky SectionKind::getMetadata()); 920f22ef01cSRoman Divacky DwarfARangesSection = 921f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_aranges", 922ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 923ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 924f22ef01cSRoman Divacky SectionKind::getMetadata()); 925f22ef01cSRoman Divacky DwarfRangesSection = 926f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_ranges", 927ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 928ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 929f22ef01cSRoman Divacky SectionKind::getMetadata()); 930f22ef01cSRoman Divacky DwarfMacroInfoSection = 931f22ef01cSRoman Divacky getContext().getCOFFSection(".debug_macinfo", 932ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE | 933ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ, 934f22ef01cSRoman Divacky SectionKind::getMetadata()); 935f22ef01cSRoman Divacky 936f22ef01cSRoman Divacky DrectveSection = 937f22ef01cSRoman Divacky getContext().getCOFFSection(".drectve", 938ffd1746dSEd Schouten COFF::IMAGE_SCN_LNK_INFO, 939f22ef01cSRoman Divacky SectionKind::getMetadata()); 940f22ef01cSRoman Divacky } 941f22ef01cSRoman Divacky 9422754fe60SDimitry Andric const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const { 9432754fe60SDimitry Andric return getContext().getCOFFSection(".eh_frame", 9442754fe60SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 9452754fe60SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 9462754fe60SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE, 9472754fe60SDimitry Andric SectionKind::getDataRel()); 9482754fe60SDimitry Andric } 9492754fe60SDimitry Andric 9502754fe60SDimitry Andric 951f22ef01cSRoman Divacky static unsigned 952f22ef01cSRoman Divacky getCOFFSectionFlags(SectionKind K) { 953f22ef01cSRoman Divacky unsigned Flags = 0; 954f22ef01cSRoman Divacky 955ffd1746dSEd Schouten if (K.isMetadata()) 956f22ef01cSRoman Divacky Flags |= 957ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_DISCARDABLE; 958f22ef01cSRoman Divacky else if (K.isText()) 959f22ef01cSRoman Divacky Flags |= 960ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_EXECUTE | 9612754fe60SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 962ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_CODE; 963f22ef01cSRoman Divacky else if (K.isBSS ()) 964f22ef01cSRoman Divacky Flags |= 965ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 966ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ | 967ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_WRITE; 968f22ef01cSRoman Divacky else if (K.isReadOnly()) 969f22ef01cSRoman Divacky Flags |= 970ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 971ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ; 972f22ef01cSRoman Divacky else if (K.isWriteable()) 973f22ef01cSRoman Divacky Flags |= 974ffd1746dSEd Schouten COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 975ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_READ | 976ffd1746dSEd Schouten COFF::IMAGE_SCN_MEM_WRITE; 977f22ef01cSRoman Divacky 978f22ef01cSRoman Divacky return Flags; 979f22ef01cSRoman Divacky } 980f22ef01cSRoman Divacky 981f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileCOFF:: 982f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 983f22ef01cSRoman Divacky Mangler *Mang, const TargetMachine &TM) const { 984f22ef01cSRoman Divacky return getContext().getCOFFSection(GV->getSection(), 985f22ef01cSRoman Divacky getCOFFSectionFlags(Kind), 986f22ef01cSRoman Divacky Kind); 987f22ef01cSRoman Divacky } 988f22ef01cSRoman Divacky 989f22ef01cSRoman Divacky static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { 990f22ef01cSRoman Divacky if (Kind.isText()) 9912754fe60SDimitry Andric return ".text$"; 992f22ef01cSRoman Divacky if (Kind.isBSS ()) 9932754fe60SDimitry Andric return ".bss$"; 994f22ef01cSRoman Divacky if (Kind.isWriteable()) 9952754fe60SDimitry Andric return ".data$"; 9962754fe60SDimitry Andric return ".rdata$"; 997f22ef01cSRoman Divacky } 998f22ef01cSRoman Divacky 999f22ef01cSRoman Divacky 1000f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileCOFF:: 1001f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 1002f22ef01cSRoman Divacky Mangler *Mang, const TargetMachine &TM) const { 1003f22ef01cSRoman Divacky assert(!Kind.isThreadLocal() && "Doesn't support TLS"); 1004f22ef01cSRoman Divacky 1005f22ef01cSRoman Divacky // If this global is linkonce/weak and the target handles this by emitting it 1006f22ef01cSRoman Divacky // into a 'uniqued' section name, create and return the section now. 1007f22ef01cSRoman Divacky if (GV->isWeakForLinker()) { 1008f22ef01cSRoman Divacky const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); 1009f22ef01cSRoman Divacky SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); 1010f22ef01cSRoman Divacky MCSymbol *Sym = Mang->getSymbol(GV); 10112754fe60SDimitry Andric Name.append(Sym->getName().begin() + 1, Sym->getName().end()); 1012f22ef01cSRoman Divacky 1013f22ef01cSRoman Divacky unsigned Characteristics = getCOFFSectionFlags(Kind); 1014f22ef01cSRoman Divacky 1015ffd1746dSEd Schouten Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1016f22ef01cSRoman Divacky 1017f22ef01cSRoman Divacky return getContext().getCOFFSection(Name.str(), Characteristics, 10182754fe60SDimitry Andric COFF::IMAGE_COMDAT_SELECT_ANY, Kind); 1019f22ef01cSRoman Divacky } 1020f22ef01cSRoman Divacky 1021f22ef01cSRoman Divacky if (Kind.isText()) 1022f22ef01cSRoman Divacky return getTextSection(); 1023f22ef01cSRoman Divacky 1024f22ef01cSRoman Divacky return getDataSection(); 1025f22ef01cSRoman Divacky } 1026f22ef01cSRoman Divacky 1027