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"
263b0f4066SDimitry Andric #include "llvm/MC/MCStreamer.h"
27f22ef01cSRoman Divacky #include "llvm/MC/MCSymbol.h"
28f22ef01cSRoman Divacky #include "llvm/Target/Mangler.h"
29f22ef01cSRoman Divacky #include "llvm/Target/TargetData.h"
30f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h"
31f22ef01cSRoman Divacky #include "llvm/Target/TargetOptions.h"
32f22ef01cSRoman Divacky #include "llvm/Support/Dwarf.h"
332754fe60SDimitry Andric #include "llvm/Support/ELF.h"
34f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h"
35f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
36f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h"
37f22ef01cSRoman Divacky #include "llvm/ADT/StringExtras.h"
382754fe60SDimitry Andric #include "llvm/ADT/Triple.h"
39f22ef01cSRoman Divacky using namespace llvm;
40f22ef01cSRoman Divacky using namespace dwarf;
41f22ef01cSRoman Divacky 
42f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
43f22ef01cSRoman Divacky //                                  ELF
44f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
45f22ef01cSRoman Divacky 
46f22ef01cSRoman Divacky void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
47f22ef01cSRoman Divacky                                              const TargetMachine &TM) {
48f22ef01cSRoman Divacky   TargetLoweringObjectFile::Initialize(Ctx, TM);
49f22ef01cSRoman Divacky 
50f22ef01cSRoman Divacky   BSSSection =
512754fe60SDimitry Andric     getContext().getELFSection(".bss", ELF::SHT_NOBITS,
522754fe60SDimitry Andric                                ELF::SHF_WRITE |ELF::SHF_ALLOC,
53f22ef01cSRoman Divacky                                SectionKind::getBSS());
54f22ef01cSRoman Divacky 
55f22ef01cSRoman Divacky   TextSection =
562754fe60SDimitry Andric     getContext().getELFSection(".text", ELF::SHT_PROGBITS,
572754fe60SDimitry Andric                                ELF::SHF_EXECINSTR |
582754fe60SDimitry Andric                                ELF::SHF_ALLOC,
59f22ef01cSRoman Divacky                                SectionKind::getText());
60f22ef01cSRoman Divacky 
61f22ef01cSRoman Divacky   DataSection =
622754fe60SDimitry Andric     getContext().getELFSection(".data", ELF::SHT_PROGBITS,
632754fe60SDimitry Andric                                ELF::SHF_WRITE |ELF::SHF_ALLOC,
64f22ef01cSRoman Divacky                                SectionKind::getDataRel());
65f22ef01cSRoman Divacky 
66f22ef01cSRoman Divacky   ReadOnlySection =
672754fe60SDimitry Andric     getContext().getELFSection(".rodata", ELF::SHT_PROGBITS,
682754fe60SDimitry Andric                                ELF::SHF_ALLOC,
69f22ef01cSRoman Divacky                                SectionKind::getReadOnly());
70f22ef01cSRoman Divacky 
71f22ef01cSRoman Divacky   TLSDataSection =
722754fe60SDimitry Andric     getContext().getELFSection(".tdata", ELF::SHT_PROGBITS,
732754fe60SDimitry Andric                                ELF::SHF_ALLOC | ELF::SHF_TLS |
742754fe60SDimitry Andric                                ELF::SHF_WRITE,
75f22ef01cSRoman Divacky                                SectionKind::getThreadData());
76f22ef01cSRoman Divacky 
77f22ef01cSRoman Divacky   TLSBSSSection =
782754fe60SDimitry Andric     getContext().getELFSection(".tbss", ELF::SHT_NOBITS,
792754fe60SDimitry Andric                                ELF::SHF_ALLOC | ELF::SHF_TLS |
802754fe60SDimitry Andric                                ELF::SHF_WRITE,
81f22ef01cSRoman Divacky                                SectionKind::getThreadBSS());
82f22ef01cSRoman Divacky 
83f22ef01cSRoman Divacky   DataRelSection =
842754fe60SDimitry Andric     getContext().getELFSection(".data.rel", ELF::SHT_PROGBITS,
852754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_WRITE,
86f22ef01cSRoman Divacky                                SectionKind::getDataRel());
87f22ef01cSRoman Divacky 
88f22ef01cSRoman Divacky   DataRelLocalSection =
892754fe60SDimitry Andric     getContext().getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
902754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_WRITE,
91f22ef01cSRoman Divacky                                SectionKind::getDataRelLocal());
92f22ef01cSRoman Divacky 
93f22ef01cSRoman Divacky   DataRelROSection =
942754fe60SDimitry Andric     getContext().getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
952754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_WRITE,
96f22ef01cSRoman Divacky                                SectionKind::getReadOnlyWithRel());
97f22ef01cSRoman Divacky 
98f22ef01cSRoman Divacky   DataRelROLocalSection =
992754fe60SDimitry Andric     getContext().getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
1002754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_WRITE,
101f22ef01cSRoman Divacky                                SectionKind::getReadOnlyWithRelLocal());
102f22ef01cSRoman Divacky 
103f22ef01cSRoman Divacky   MergeableConst4Section =
1042754fe60SDimitry Andric     getContext().getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
1052754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_MERGE,
106f22ef01cSRoman Divacky                                SectionKind::getMergeableConst4());
107f22ef01cSRoman Divacky 
108f22ef01cSRoman Divacky   MergeableConst8Section =
1092754fe60SDimitry Andric     getContext().getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
1102754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_MERGE,
111f22ef01cSRoman Divacky                                SectionKind::getMergeableConst8());
112f22ef01cSRoman Divacky 
113f22ef01cSRoman Divacky   MergeableConst16Section =
1142754fe60SDimitry Andric     getContext().getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
1152754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_MERGE,
116f22ef01cSRoman Divacky                                SectionKind::getMergeableConst16());
117f22ef01cSRoman Divacky 
118f22ef01cSRoman Divacky   StaticCtorSection =
1192754fe60SDimitry Andric     getContext().getELFSection(".ctors", ELF::SHT_PROGBITS,
1202754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_WRITE,
121f22ef01cSRoman Divacky                                SectionKind::getDataRel());
122f22ef01cSRoman Divacky 
123f22ef01cSRoman Divacky   StaticDtorSection =
1242754fe60SDimitry Andric     getContext().getELFSection(".dtors", ELF::SHT_PROGBITS,
1252754fe60SDimitry Andric                                ELF::SHF_ALLOC |ELF::SHF_WRITE,
126f22ef01cSRoman Divacky                                SectionKind::getDataRel());
127f22ef01cSRoman Divacky 
128f22ef01cSRoman Divacky   // Exception Handling Sections.
129f22ef01cSRoman Divacky 
130f22ef01cSRoman Divacky   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
131f22ef01cSRoman Divacky   // it contains relocatable pointers.  In PIC mode, this is probably a big
132f22ef01cSRoman Divacky   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
133f22ef01cSRoman Divacky   // adjusted or this should be a data section.
134f22ef01cSRoman Divacky   LSDASection =
1352754fe60SDimitry Andric     getContext().getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
1362754fe60SDimitry Andric                                ELF::SHF_ALLOC,
137f22ef01cSRoman Divacky                                SectionKind::getReadOnly());
138f22ef01cSRoman Divacky   // Debug Info Sections.
139f22ef01cSRoman Divacky   DwarfAbbrevSection =
1402754fe60SDimitry Andric     getContext().getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
141f22ef01cSRoman Divacky                                SectionKind::getMetadata());
142f22ef01cSRoman Divacky   DwarfInfoSection =
1432754fe60SDimitry Andric     getContext().getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
144f22ef01cSRoman Divacky                                SectionKind::getMetadata());
145f22ef01cSRoman Divacky   DwarfLineSection =
1462754fe60SDimitry Andric     getContext().getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
147f22ef01cSRoman Divacky                                SectionKind::getMetadata());
148f22ef01cSRoman Divacky   DwarfFrameSection =
1492754fe60SDimitry Andric     getContext().getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
150f22ef01cSRoman Divacky                                SectionKind::getMetadata());
151f22ef01cSRoman Divacky   DwarfPubNamesSection =
1522754fe60SDimitry Andric     getContext().getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
153f22ef01cSRoman Divacky                                SectionKind::getMetadata());
154f22ef01cSRoman Divacky   DwarfPubTypesSection =
1552754fe60SDimitry Andric     getContext().getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
156f22ef01cSRoman Divacky                                SectionKind::getMetadata());
157f22ef01cSRoman Divacky   DwarfStrSection =
1582754fe60SDimitry Andric     getContext().getELFSection(".debug_str", ELF::SHT_PROGBITS, 0,
159f22ef01cSRoman Divacky                                SectionKind::getMetadata());
160f22ef01cSRoman Divacky   DwarfLocSection =
1612754fe60SDimitry Andric     getContext().getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
162f22ef01cSRoman Divacky                                SectionKind::getMetadata());
163f22ef01cSRoman Divacky   DwarfARangesSection =
1642754fe60SDimitry Andric     getContext().getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
165f22ef01cSRoman Divacky                                SectionKind::getMetadata());
166f22ef01cSRoman Divacky   DwarfRangesSection =
1672754fe60SDimitry Andric     getContext().getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
168f22ef01cSRoman Divacky                                SectionKind::getMetadata());
169f22ef01cSRoman Divacky   DwarfMacroInfoSection =
1702754fe60SDimitry Andric     getContext().getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
171f22ef01cSRoman Divacky                                SectionKind::getMetadata());
172f22ef01cSRoman Divacky }
173f22ef01cSRoman Divacky 
1742754fe60SDimitry Andric const MCSection *TargetLoweringObjectFileELF::getEHFrameSection() const {
1752754fe60SDimitry Andric   return getContext().getELFSection(".eh_frame", ELF::SHT_PROGBITS,
1762754fe60SDimitry Andric                                     ELF::SHF_ALLOC,
1772754fe60SDimitry Andric                                     SectionKind::getDataRel());
1782754fe60SDimitry Andric }
179f22ef01cSRoman Divacky 
1803b0f4066SDimitry Andric MCSymbol *
1813b0f4066SDimitry Andric TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
1823b0f4066SDimitry Andric                                                      Mangler *Mang,
1833b0f4066SDimitry Andric                                                 MachineModuleInfo *MMI) const {
1843b0f4066SDimitry Andric   unsigned Encoding = getPersonalityEncoding();
1853b0f4066SDimitry Andric   switch (Encoding & 0x70) {
1863b0f4066SDimitry Andric   default:
1873b0f4066SDimitry Andric     report_fatal_error("We do not support this DWARF encoding yet!");
1883b0f4066SDimitry Andric   case dwarf::DW_EH_PE_absptr:
1893b0f4066SDimitry Andric     return  Mang->getSymbol(GV);
1903b0f4066SDimitry Andric     break;
1913b0f4066SDimitry Andric   case dwarf::DW_EH_PE_pcrel: {
1923b0f4066SDimitry Andric     Twine FullName = StringRef("DW.ref.") + Mang->getSymbol(GV)->getName();
1933b0f4066SDimitry Andric     return getContext().GetOrCreateSymbol(FullName);
1943b0f4066SDimitry Andric     break;
1953b0f4066SDimitry Andric   }
1963b0f4066SDimitry Andric   }
1973b0f4066SDimitry Andric }
1983b0f4066SDimitry Andric 
1993b0f4066SDimitry Andric void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
2003b0f4066SDimitry Andric                                                        const TargetMachine &TM,
2013b0f4066SDimitry Andric                                                        const MCSymbol *Sym) const {
2023b0f4066SDimitry Andric   Twine FullName = StringRef("DW.ref.") + Sym->getName();
2033b0f4066SDimitry Andric   MCSymbol *Label = getContext().GetOrCreateSymbol(FullName);
2043b0f4066SDimitry Andric   Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
2053b0f4066SDimitry Andric   Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
2063b0f4066SDimitry Andric   Twine SectionName = StringRef(".data.") + Label->getName();
2073b0f4066SDimitry Andric   SmallString<64> NameData;
2083b0f4066SDimitry Andric   SectionName.toVector(NameData);
2093b0f4066SDimitry Andric   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
2103b0f4066SDimitry Andric   const MCSection *Sec = getContext().getELFSection(NameData,
2113b0f4066SDimitry Andric                                                     ELF::SHT_PROGBITS,
2123b0f4066SDimitry Andric                                                     Flags,
2133b0f4066SDimitry Andric                                                     SectionKind::getDataRel(),
2143b0f4066SDimitry Andric                                                     0, Label->getName());
2153b0f4066SDimitry Andric   Streamer.SwitchSection(Sec);
2163b0f4066SDimitry Andric   Streamer.EmitValueToAlignment(8);
2173b0f4066SDimitry Andric   Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
2183b0f4066SDimitry Andric   const MCExpr *E = MCConstantExpr::Create(8, getContext());
2193b0f4066SDimitry Andric   Streamer.EmitELFSize(Label, E);
2203b0f4066SDimitry Andric   Streamer.EmitLabel(Label);
2213b0f4066SDimitry Andric 
2223b0f4066SDimitry Andric   unsigned Size = TM.getTargetData()->getPointerSize();
2233b0f4066SDimitry Andric   Streamer.EmitSymbolValue(Sym, Size);
2243b0f4066SDimitry Andric }
2253b0f4066SDimitry Andric 
226f22ef01cSRoman Divacky static SectionKind
227f22ef01cSRoman Divacky getELFKindForNamedSection(StringRef Name, SectionKind K) {
228bd5abe19SDimitry Andric   // N.B.: The defaults used in here are no the same ones used in MC.
229bd5abe19SDimitry Andric   // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
230bd5abe19SDimitry Andric   // both gas and MC will produce a section with no flags. Given
231bd5abe19SDimitry Andric   // section(".eh_frame") gcc will produce
232bd5abe19SDimitry Andric   // .section	.eh_frame,"a",@progbits
233f22ef01cSRoman Divacky   if (Name.empty() || Name[0] != '.') return K;
234f22ef01cSRoman Divacky 
235f22ef01cSRoman Divacky   // Some lame default implementation based on some magic section names.
236f22ef01cSRoman Divacky   if (Name == ".bss" ||
237f22ef01cSRoman Divacky       Name.startswith(".bss.") ||
238f22ef01cSRoman Divacky       Name.startswith(".gnu.linkonce.b.") ||
239f22ef01cSRoman Divacky       Name.startswith(".llvm.linkonce.b.") ||
240f22ef01cSRoman Divacky       Name == ".sbss" ||
241f22ef01cSRoman Divacky       Name.startswith(".sbss.") ||
242f22ef01cSRoman Divacky       Name.startswith(".gnu.linkonce.sb.") ||
243f22ef01cSRoman Divacky       Name.startswith(".llvm.linkonce.sb."))
244f22ef01cSRoman Divacky     return SectionKind::getBSS();
245f22ef01cSRoman Divacky 
246f22ef01cSRoman Divacky   if (Name == ".tdata" ||
247f22ef01cSRoman Divacky       Name.startswith(".tdata.") ||
248f22ef01cSRoman Divacky       Name.startswith(".gnu.linkonce.td.") ||
249f22ef01cSRoman Divacky       Name.startswith(".llvm.linkonce.td."))
250f22ef01cSRoman Divacky     return SectionKind::getThreadData();
251f22ef01cSRoman Divacky 
252f22ef01cSRoman Divacky   if (Name == ".tbss" ||
253f22ef01cSRoman Divacky       Name.startswith(".tbss.") ||
254f22ef01cSRoman Divacky       Name.startswith(".gnu.linkonce.tb.") ||
255f22ef01cSRoman Divacky       Name.startswith(".llvm.linkonce.tb."))
256f22ef01cSRoman Divacky     return SectionKind::getThreadBSS();
257f22ef01cSRoman Divacky 
258f22ef01cSRoman Divacky   return K;
259f22ef01cSRoman Divacky }
260f22ef01cSRoman Divacky 
261f22ef01cSRoman Divacky 
262f22ef01cSRoman Divacky static unsigned getELFSectionType(StringRef Name, SectionKind K) {
263f22ef01cSRoman Divacky 
264f22ef01cSRoman Divacky   if (Name == ".init_array")
2652754fe60SDimitry Andric     return ELF::SHT_INIT_ARRAY;
266f22ef01cSRoman Divacky 
267f22ef01cSRoman Divacky   if (Name == ".fini_array")
2682754fe60SDimitry Andric     return ELF::SHT_FINI_ARRAY;
269f22ef01cSRoman Divacky 
270f22ef01cSRoman Divacky   if (Name == ".preinit_array")
2712754fe60SDimitry Andric     return ELF::SHT_PREINIT_ARRAY;
272f22ef01cSRoman Divacky 
273f22ef01cSRoman Divacky   if (K.isBSS() || K.isThreadBSS())
2742754fe60SDimitry Andric     return ELF::SHT_NOBITS;
275f22ef01cSRoman Divacky 
2762754fe60SDimitry Andric   return ELF::SHT_PROGBITS;
277f22ef01cSRoman Divacky }
278f22ef01cSRoman Divacky 
279f22ef01cSRoman Divacky 
280f22ef01cSRoman Divacky static unsigned
281f22ef01cSRoman Divacky getELFSectionFlags(SectionKind K) {
282f22ef01cSRoman Divacky   unsigned Flags = 0;
283f22ef01cSRoman Divacky 
284f22ef01cSRoman Divacky   if (!K.isMetadata())
2852754fe60SDimitry Andric     Flags |= ELF::SHF_ALLOC;
286f22ef01cSRoman Divacky 
287f22ef01cSRoman Divacky   if (K.isText())
2882754fe60SDimitry Andric     Flags |= ELF::SHF_EXECINSTR;
289f22ef01cSRoman Divacky 
290f22ef01cSRoman Divacky   if (K.isWriteable())
2912754fe60SDimitry Andric     Flags |= ELF::SHF_WRITE;
292f22ef01cSRoman Divacky 
293f22ef01cSRoman Divacky   if (K.isThreadLocal())
2942754fe60SDimitry Andric     Flags |= ELF::SHF_TLS;
295f22ef01cSRoman Divacky 
296f22ef01cSRoman Divacky   // K.isMergeableConst() is left out to honour PR4650
297f22ef01cSRoman Divacky   if (K.isMergeableCString() || K.isMergeableConst4() ||
298f22ef01cSRoman Divacky       K.isMergeableConst8() || K.isMergeableConst16())
2992754fe60SDimitry Andric     Flags |= ELF::SHF_MERGE;
300f22ef01cSRoman Divacky 
301f22ef01cSRoman Divacky   if (K.isMergeableCString())
3022754fe60SDimitry Andric     Flags |= ELF::SHF_STRINGS;
303f22ef01cSRoman Divacky 
304f22ef01cSRoman Divacky   return Flags;
305f22ef01cSRoman Divacky }
306f22ef01cSRoman Divacky 
307f22ef01cSRoman Divacky 
308f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF::
309f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
310f22ef01cSRoman Divacky                          Mangler *Mang, const TargetMachine &TM) const {
311f22ef01cSRoman Divacky   StringRef SectionName = GV->getSection();
312f22ef01cSRoman Divacky 
313f22ef01cSRoman Divacky   // Infer section flags from the section name if we can.
314f22ef01cSRoman Divacky   Kind = getELFKindForNamedSection(SectionName, Kind);
315f22ef01cSRoman Divacky 
316f22ef01cSRoman Divacky   return getContext().getELFSection(SectionName,
317f22ef01cSRoman Divacky                                     getELFSectionType(SectionName, Kind),
3182754fe60SDimitry Andric                                     getELFSectionFlags(Kind), Kind);
319f22ef01cSRoman Divacky }
320f22ef01cSRoman Divacky 
321f22ef01cSRoman Divacky /// getSectionPrefixForGlobal - Return the section prefix name used by options
322f22ef01cSRoman Divacky /// FunctionsSections and DataSections.
323f22ef01cSRoman Divacky static const char *getSectionPrefixForGlobal(SectionKind Kind) {
324f22ef01cSRoman Divacky   if (Kind.isText())                 return ".text.";
325f22ef01cSRoman Divacky   if (Kind.isReadOnly())             return ".rodata.";
326f22ef01cSRoman Divacky 
327f22ef01cSRoman Divacky   if (Kind.isThreadData())           return ".tdata.";
328f22ef01cSRoman Divacky   if (Kind.isThreadBSS())            return ".tbss.";
329f22ef01cSRoman Divacky 
330f22ef01cSRoman Divacky   if (Kind.isDataNoRel())            return ".data.";
331f22ef01cSRoman Divacky   if (Kind.isDataRelLocal())         return ".data.rel.local.";
332f22ef01cSRoman Divacky   if (Kind.isDataRel())              return ".data.rel.";
333f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
334f22ef01cSRoman Divacky 
335f22ef01cSRoman Divacky   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
336f22ef01cSRoman Divacky   return ".data.rel.ro.";
337f22ef01cSRoman Divacky }
338f22ef01cSRoman Divacky 
339f22ef01cSRoman Divacky 
340f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF::
341f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
342f22ef01cSRoman Divacky                        Mangler *Mang, const TargetMachine &TM) const {
343f22ef01cSRoman Divacky   // If we have -ffunction-section or -fdata-section then we should emit the
344f22ef01cSRoman Divacky   // global value to a uniqued section specifically for it.
345f22ef01cSRoman Divacky   bool EmitUniquedSection;
346f22ef01cSRoman Divacky   if (Kind.isText())
347f22ef01cSRoman Divacky     EmitUniquedSection = TM.getFunctionSections();
348f22ef01cSRoman Divacky   else
349f22ef01cSRoman Divacky     EmitUniquedSection = TM.getDataSections();
350f22ef01cSRoman Divacky 
351f22ef01cSRoman Divacky   // If this global is linkonce/weak and the target handles this by emitting it
352f22ef01cSRoman Divacky   // into a 'uniqued' section name, create and return the section now.
353f22ef01cSRoman Divacky   if ((GV->isWeakForLinker() || EmitUniquedSection) &&
354f22ef01cSRoman Divacky       !Kind.isCommon() && !Kind.isBSS()) {
355f22ef01cSRoman Divacky     const char *Prefix;
356f22ef01cSRoman Divacky     Prefix = getSectionPrefixForGlobal(Kind);
357f22ef01cSRoman Divacky 
358f22ef01cSRoman Divacky     SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
359f22ef01cSRoman Divacky     MCSymbol *Sym = Mang->getSymbol(GV);
360f22ef01cSRoman Divacky     Name.append(Sym->getName().begin(), Sym->getName().end());
3612754fe60SDimitry Andric     StringRef Group = "";
3622754fe60SDimitry Andric     unsigned Flags = getELFSectionFlags(Kind);
3632754fe60SDimitry Andric     if (GV->isWeakForLinker()) {
3642754fe60SDimitry Andric       Group = Sym->getName();
3652754fe60SDimitry Andric       Flags |= ELF::SHF_GROUP;
3662754fe60SDimitry Andric     }
3672754fe60SDimitry Andric 
368f22ef01cSRoman Divacky     return getContext().getELFSection(Name.str(),
369f22ef01cSRoman Divacky                                       getELFSectionType(Name.str(), Kind),
3702754fe60SDimitry Andric                                       Flags, Kind, 0, Group);
371f22ef01cSRoman Divacky   }
372f22ef01cSRoman Divacky 
373f22ef01cSRoman Divacky   if (Kind.isText()) return TextSection;
374f22ef01cSRoman Divacky 
375f22ef01cSRoman Divacky   if (Kind.isMergeable1ByteCString() ||
376f22ef01cSRoman Divacky       Kind.isMergeable2ByteCString() ||
377f22ef01cSRoman Divacky       Kind.isMergeable4ByteCString()) {
378f22ef01cSRoman Divacky 
379f22ef01cSRoman Divacky     // We also need alignment here.
380f22ef01cSRoman Divacky     // FIXME: this is getting the alignment of the character, not the
381f22ef01cSRoman Divacky     // alignment of the global!
382f22ef01cSRoman Divacky     unsigned Align =
383f22ef01cSRoman Divacky       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
384f22ef01cSRoman Divacky 
385f22ef01cSRoman Divacky     const char *SizeSpec = ".rodata.str1.";
386f22ef01cSRoman Divacky     if (Kind.isMergeable2ByteCString())
387f22ef01cSRoman Divacky       SizeSpec = ".rodata.str2.";
388f22ef01cSRoman Divacky     else if (Kind.isMergeable4ByteCString())
389f22ef01cSRoman Divacky       SizeSpec = ".rodata.str4.";
390f22ef01cSRoman Divacky     else
391f22ef01cSRoman Divacky       assert(Kind.isMergeable1ByteCString() && "unknown string width");
392f22ef01cSRoman Divacky 
393f22ef01cSRoman Divacky 
394f22ef01cSRoman Divacky     std::string Name = SizeSpec + utostr(Align);
3952754fe60SDimitry Andric     return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
3962754fe60SDimitry Andric                                       ELF::SHF_ALLOC |
3972754fe60SDimitry Andric                                       ELF::SHF_MERGE |
3982754fe60SDimitry Andric                                       ELF::SHF_STRINGS,
399f22ef01cSRoman Divacky                                       Kind);
400f22ef01cSRoman Divacky   }
401f22ef01cSRoman Divacky 
402f22ef01cSRoman Divacky   if (Kind.isMergeableConst()) {
403f22ef01cSRoman Divacky     if (Kind.isMergeableConst4() && MergeableConst4Section)
404f22ef01cSRoman Divacky       return MergeableConst4Section;
405f22ef01cSRoman Divacky     if (Kind.isMergeableConst8() && MergeableConst8Section)
406f22ef01cSRoman Divacky       return MergeableConst8Section;
407f22ef01cSRoman Divacky     if (Kind.isMergeableConst16() && MergeableConst16Section)
408f22ef01cSRoman Divacky       return MergeableConst16Section;
409f22ef01cSRoman Divacky     return ReadOnlySection;  // .const
410f22ef01cSRoman Divacky   }
411f22ef01cSRoman Divacky 
412f22ef01cSRoman Divacky   if (Kind.isReadOnly())             return ReadOnlySection;
413f22ef01cSRoman Divacky 
414f22ef01cSRoman Divacky   if (Kind.isThreadData())           return TLSDataSection;
415f22ef01cSRoman Divacky   if (Kind.isThreadBSS())            return TLSBSSSection;
416f22ef01cSRoman Divacky 
417f22ef01cSRoman Divacky   // Note: we claim that common symbols are put in BSSSection, but they are
418f22ef01cSRoman Divacky   // really emitted with the magic .comm directive, which creates a symbol table
419f22ef01cSRoman Divacky   // entry but not a section.
420f22ef01cSRoman Divacky   if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
421f22ef01cSRoman Divacky 
422f22ef01cSRoman Divacky   if (Kind.isDataNoRel())            return DataSection;
423f22ef01cSRoman Divacky   if (Kind.isDataRelLocal())         return DataRelLocalSection;
424f22ef01cSRoman Divacky   if (Kind.isDataRel())              return DataRelSection;
425f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
426f22ef01cSRoman Divacky 
427f22ef01cSRoman Divacky   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
428f22ef01cSRoman Divacky   return DataRelROSection;
429f22ef01cSRoman Divacky }
430f22ef01cSRoman Divacky 
431f22ef01cSRoman Divacky /// getSectionForConstant - Given a mergeable constant with the
432f22ef01cSRoman Divacky /// specified size and relocation information, return a section that it
433f22ef01cSRoman Divacky /// should be placed in.
434f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF::
435f22ef01cSRoman Divacky getSectionForConstant(SectionKind Kind) const {
436f22ef01cSRoman Divacky   if (Kind.isMergeableConst4() && MergeableConst4Section)
437f22ef01cSRoman Divacky     return MergeableConst4Section;
438f22ef01cSRoman Divacky   if (Kind.isMergeableConst8() && MergeableConst8Section)
439f22ef01cSRoman Divacky     return MergeableConst8Section;
440f22ef01cSRoman Divacky   if (Kind.isMergeableConst16() && MergeableConst16Section)
441f22ef01cSRoman Divacky     return MergeableConst16Section;
442f22ef01cSRoman Divacky   if (Kind.isReadOnly())
443f22ef01cSRoman Divacky     return ReadOnlySection;
444f22ef01cSRoman Divacky 
445f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
446f22ef01cSRoman Divacky   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
447f22ef01cSRoman Divacky   return DataRelROSection;
448f22ef01cSRoman Divacky }
449f22ef01cSRoman Divacky 
450f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFileELF::
451f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
452f22ef01cSRoman Divacky                                MachineModuleInfo *MMI,
453f22ef01cSRoman Divacky                                unsigned Encoding, MCStreamer &Streamer) const {
454f22ef01cSRoman Divacky 
455f22ef01cSRoman Divacky   if (Encoding & dwarf::DW_EH_PE_indirect) {
456f22ef01cSRoman Divacky     MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
457f22ef01cSRoman Divacky 
458f22ef01cSRoman Divacky     SmallString<128> Name;
459f22ef01cSRoman Divacky     Mang->getNameWithPrefix(Name, GV, true);
460f22ef01cSRoman Divacky     Name += ".DW.stub";
461f22ef01cSRoman Divacky 
462f22ef01cSRoman Divacky     // Add information about the stub reference to ELFMMI so that the stub
463f22ef01cSRoman Divacky     // gets emitted by the asmprinter.
464f22ef01cSRoman Divacky     MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
465f22ef01cSRoman Divacky     MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
466f22ef01cSRoman Divacky     if (StubSym.getPointer() == 0) {
467f22ef01cSRoman Divacky       MCSymbol *Sym = Mang->getSymbol(GV);
468f22ef01cSRoman Divacky       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
469f22ef01cSRoman Divacky     }
470f22ef01cSRoman Divacky 
471f22ef01cSRoman Divacky     return TargetLoweringObjectFile::
4723b0f4066SDimitry Andric       getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
473f22ef01cSRoman Divacky   }
474f22ef01cSRoman Divacky 
475f22ef01cSRoman Divacky   return TargetLoweringObjectFile::
476f22ef01cSRoman Divacky     getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
477f22ef01cSRoman Divacky }
478f22ef01cSRoman Divacky 
479f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
480f22ef01cSRoman Divacky //                                 MachO
481f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
482f22ef01cSRoman Divacky 
483f22ef01cSRoman Divacky void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
484f22ef01cSRoman Divacky                                                const TargetMachine &TM) {
485f22ef01cSRoman Divacky   IsFunctionEHFrameSymbolPrivate = false;
486f22ef01cSRoman Divacky   SupportsWeakOmittedEHFrame = false;
487f22ef01cSRoman Divacky 
4883b0f4066SDimitry Andric   // .comm doesn't support alignment before Leopard.
4892754fe60SDimitry Andric   Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
4903b0f4066SDimitry Andric   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
4912754fe60SDimitry Andric     CommDirectiveSupportsAlignment = false;
4922754fe60SDimitry Andric 
493f22ef01cSRoman Divacky   TargetLoweringObjectFile::Initialize(Ctx, TM);
494f22ef01cSRoman Divacky 
495f22ef01cSRoman Divacky   TextSection // .text
496f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__text",
497f22ef01cSRoman Divacky                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
498f22ef01cSRoman Divacky                                    SectionKind::getText());
499f22ef01cSRoman Divacky   DataSection // .data
500f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__data", 0,
501f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
502f22ef01cSRoman Divacky 
503f22ef01cSRoman Divacky   TLSDataSection // .tdata
504f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__thread_data",
505f22ef01cSRoman Divacky                                    MCSectionMachO::S_THREAD_LOCAL_REGULAR,
506f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
507f22ef01cSRoman Divacky   TLSBSSSection // .tbss
508f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__thread_bss",
509f22ef01cSRoman Divacky                                    MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
510f22ef01cSRoman Divacky                                    SectionKind::getThreadBSS());
511f22ef01cSRoman Divacky 
512f22ef01cSRoman Divacky   // TODO: Verify datarel below.
513f22ef01cSRoman Divacky   TLSTLVSection // .tlv
514f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__thread_vars",
515f22ef01cSRoman Divacky                                    MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
516f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
517f22ef01cSRoman Divacky 
518f22ef01cSRoman Divacky   TLSThreadInitSection
519f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__thread_init",
520f22ef01cSRoman Divacky                           MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
521f22ef01cSRoman Divacky                           SectionKind::getDataRel());
522f22ef01cSRoman Divacky 
523f22ef01cSRoman Divacky   CStringSection // .cstring
524f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__cstring",
525f22ef01cSRoman Divacky                                    MCSectionMachO::S_CSTRING_LITERALS,
526f22ef01cSRoman Divacky                                    SectionKind::getMergeable1ByteCString());
527f22ef01cSRoman Divacky   UStringSection
528f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT","__ustring", 0,
529f22ef01cSRoman Divacky                                    SectionKind::getMergeable2ByteCString());
530f22ef01cSRoman Divacky   FourByteConstantSection // .literal4
531f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__literal4",
532f22ef01cSRoman Divacky                                    MCSectionMachO::S_4BYTE_LITERALS,
533f22ef01cSRoman Divacky                                    SectionKind::getMergeableConst4());
534f22ef01cSRoman Divacky   EightByteConstantSection // .literal8
535f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__literal8",
536f22ef01cSRoman Divacky                                    MCSectionMachO::S_8BYTE_LITERALS,
537f22ef01cSRoman Divacky                                    SectionKind::getMergeableConst8());
538f22ef01cSRoman Divacky 
539f22ef01cSRoman Divacky   // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
540f22ef01cSRoman Divacky   // to using it in -static mode.
541f22ef01cSRoman Divacky   SixteenByteConstantSection = 0;
542f22ef01cSRoman Divacky   if (TM.getRelocationModel() != Reloc::Static &&
543f22ef01cSRoman Divacky       TM.getTargetData()->getPointerSize() == 32)
544f22ef01cSRoman Divacky     SixteenByteConstantSection =   // .literal16
545f22ef01cSRoman Divacky       getContext().getMachOSection("__TEXT", "__literal16",
546f22ef01cSRoman Divacky                                    MCSectionMachO::S_16BYTE_LITERALS,
547f22ef01cSRoman Divacky                                    SectionKind::getMergeableConst16());
548f22ef01cSRoman Divacky 
549f22ef01cSRoman Divacky   ReadOnlySection  // .const
550f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__const", 0,
551f22ef01cSRoman Divacky                                    SectionKind::getReadOnly());
552f22ef01cSRoman Divacky 
553f22ef01cSRoman Divacky   TextCoalSection
554f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__textcoal_nt",
555f22ef01cSRoman Divacky                                    MCSectionMachO::S_COALESCED |
556f22ef01cSRoman Divacky                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
557f22ef01cSRoman Divacky                                    SectionKind::getText());
558f22ef01cSRoman Divacky   ConstTextCoalSection
559f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__const_coal",
560f22ef01cSRoman Divacky                                    MCSectionMachO::S_COALESCED,
561e580952dSDimitry Andric                                    SectionKind::getReadOnly());
562f22ef01cSRoman Divacky   ConstDataSection  // .const_data
563f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__const", 0,
564f22ef01cSRoman Divacky                                    SectionKind::getReadOnlyWithRel());
565f22ef01cSRoman Divacky   DataCoalSection
566f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__datacoal_nt",
567f22ef01cSRoman Divacky                                    MCSectionMachO::S_COALESCED,
568f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
569f22ef01cSRoman Divacky   DataCommonSection
570f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__common",
571f22ef01cSRoman Divacky                                    MCSectionMachO::S_ZEROFILL,
572f22ef01cSRoman Divacky                                    SectionKind::getBSS());
573f22ef01cSRoman Divacky   DataBSSSection
574f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
575f22ef01cSRoman Divacky                                    SectionKind::getBSS());
576f22ef01cSRoman Divacky 
577f22ef01cSRoman Divacky 
578f22ef01cSRoman Divacky   LazySymbolPointerSection
579f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
580f22ef01cSRoman Divacky                                    MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
581f22ef01cSRoman Divacky                                    SectionKind::getMetadata());
582f22ef01cSRoman Divacky   NonLazySymbolPointerSection
583f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
584f22ef01cSRoman Divacky                                    MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
585f22ef01cSRoman Divacky                                    SectionKind::getMetadata());
586f22ef01cSRoman Divacky 
587f22ef01cSRoman Divacky   if (TM.getRelocationModel() == Reloc::Static) {
588f22ef01cSRoman Divacky     StaticCtorSection
589f22ef01cSRoman Divacky       = getContext().getMachOSection("__TEXT", "__constructor", 0,
590f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
591f22ef01cSRoman Divacky     StaticDtorSection
592f22ef01cSRoman Divacky       = getContext().getMachOSection("__TEXT", "__destructor", 0,
593f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
594f22ef01cSRoman Divacky   } else {
595f22ef01cSRoman Divacky     StaticCtorSection
596f22ef01cSRoman Divacky       = getContext().getMachOSection("__DATA", "__mod_init_func",
597f22ef01cSRoman Divacky                                      MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
598f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
599f22ef01cSRoman Divacky     StaticDtorSection
600f22ef01cSRoman Divacky       = getContext().getMachOSection("__DATA", "__mod_term_func",
601f22ef01cSRoman Divacky                                      MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
602f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
603f22ef01cSRoman Divacky   }
604f22ef01cSRoman Divacky 
605f22ef01cSRoman Divacky   // Exception Handling.
606f22ef01cSRoman Divacky   LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
607f22ef01cSRoman Divacky                                              SectionKind::getReadOnlyWithRel());
608f22ef01cSRoman Divacky   // Debug Information.
609f22ef01cSRoman Divacky   DwarfAbbrevSection =
610f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_abbrev",
611f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
612f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
613f22ef01cSRoman Divacky   DwarfInfoSection =
614f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_info",
615f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
616f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
617f22ef01cSRoman Divacky   DwarfLineSection =
618f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_line",
619f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
620f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
621f22ef01cSRoman Divacky   DwarfFrameSection =
622f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_frame",
623f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
624f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
625f22ef01cSRoman Divacky   DwarfPubNamesSection =
626f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_pubnames",
627f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
628f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
629f22ef01cSRoman Divacky   DwarfPubTypesSection =
630f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_pubtypes",
631f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
632f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
633f22ef01cSRoman Divacky   DwarfStrSection =
634f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_str",
635f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
636f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
637f22ef01cSRoman Divacky   DwarfLocSection =
638f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_loc",
639f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
640f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
641f22ef01cSRoman Divacky   DwarfARangesSection =
642f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_aranges",
643f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
644f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
645f22ef01cSRoman Divacky   DwarfRangesSection =
646f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_ranges",
647f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
648f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
649f22ef01cSRoman Divacky   DwarfMacroInfoSection =
650f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_macinfo",
651f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
652f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
653f22ef01cSRoman Divacky   DwarfDebugInlineSection =
654f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_inlined",
655f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
656f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
657f22ef01cSRoman Divacky 
658f22ef01cSRoman Divacky   TLSExtraDataSection = TLSTLVSection;
659f22ef01cSRoman Divacky }
660f22ef01cSRoman Divacky 
6612754fe60SDimitry Andric const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
6622754fe60SDimitry Andric   return getContext().getMachOSection("__TEXT", "__eh_frame",
6632754fe60SDimitry Andric                                       MCSectionMachO::S_COALESCED |
6642754fe60SDimitry Andric                                       MCSectionMachO::S_ATTR_NO_TOC |
6652754fe60SDimitry Andric                                       MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
6662754fe60SDimitry Andric                                       MCSectionMachO::S_ATTR_LIVE_SUPPORT,
6672754fe60SDimitry Andric                                       SectionKind::getReadOnly());
6682754fe60SDimitry Andric }
6692754fe60SDimitry Andric 
670f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileMachO::
671f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
672f22ef01cSRoman Divacky                          Mangler *Mang, const TargetMachine &TM) const {
673f22ef01cSRoman Divacky   // Parse the section specifier and create it if valid.
674f22ef01cSRoman Divacky   StringRef Segment, Section;
6753b0f4066SDimitry Andric   unsigned TAA = 0, StubSize = 0;
6763b0f4066SDimitry Andric   bool TAAParsed;
677f22ef01cSRoman Divacky   std::string ErrorCode =
678f22ef01cSRoman Divacky     MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
6793b0f4066SDimitry Andric                                           TAA, TAAParsed, StubSize);
680f22ef01cSRoman Divacky   if (!ErrorCode.empty()) {
681f22ef01cSRoman Divacky     // If invalid, report the error with report_fatal_error.
682f22ef01cSRoman Divacky     report_fatal_error("Global variable '" + GV->getNameStr() +
683f22ef01cSRoman Divacky                       "' has an invalid section specifier '" + GV->getSection()+
684f22ef01cSRoman Divacky                       "': " + ErrorCode + ".");
685f22ef01cSRoman Divacky     // Fall back to dropping it into the data section.
686f22ef01cSRoman Divacky     return DataSection;
687f22ef01cSRoman Divacky   }
688f22ef01cSRoman Divacky 
689f22ef01cSRoman Divacky   // Get the section.
690f22ef01cSRoman Divacky   const MCSectionMachO *S =
691f22ef01cSRoman Divacky     getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
692f22ef01cSRoman Divacky 
693dd6029ffSDimitry Andric   // If TAA wasn't set by ParseSectionSpecifier() above,
694dd6029ffSDimitry Andric   // use the value returned by getMachOSection() as a default.
6953b0f4066SDimitry Andric   if (!TAAParsed)
696dd6029ffSDimitry Andric     TAA = S->getTypeAndAttributes();
697dd6029ffSDimitry Andric 
698f22ef01cSRoman Divacky   // Okay, now that we got the section, verify that the TAA & StubSize agree.
699f22ef01cSRoman Divacky   // If the user declared multiple globals with different section flags, we need
700f22ef01cSRoman Divacky   // to reject it here.
701f22ef01cSRoman Divacky   if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
702f22ef01cSRoman Divacky     // If invalid, report the error with report_fatal_error.
703f22ef01cSRoman Divacky     report_fatal_error("Global variable '" + GV->getNameStr() +
704f22ef01cSRoman Divacky                       "' section type or attributes does not match previous"
705f22ef01cSRoman Divacky                       " section specifier");
706f22ef01cSRoman Divacky   }
707f22ef01cSRoman Divacky 
708f22ef01cSRoman Divacky   return S;
709f22ef01cSRoman Divacky }
710f22ef01cSRoman Divacky 
711f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileMachO::
712f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
713f22ef01cSRoman Divacky                        Mangler *Mang, const TargetMachine &TM) const {
714f22ef01cSRoman Divacky 
715f22ef01cSRoman Divacky   // Handle thread local data.
716f22ef01cSRoman Divacky   if (Kind.isThreadBSS()) return TLSBSSSection;
717f22ef01cSRoman Divacky   if (Kind.isThreadData()) return TLSDataSection;
718f22ef01cSRoman Divacky 
719f22ef01cSRoman Divacky   if (Kind.isText())
720f22ef01cSRoman Divacky     return GV->isWeakForLinker() ? TextCoalSection : TextSection;
721f22ef01cSRoman Divacky 
722f22ef01cSRoman Divacky   // If this is weak/linkonce, put this in a coalescable section, either in text
723f22ef01cSRoman Divacky   // or data depending on if it is writable.
724f22ef01cSRoman Divacky   if (GV->isWeakForLinker()) {
725f22ef01cSRoman Divacky     if (Kind.isReadOnly())
726f22ef01cSRoman Divacky       return ConstTextCoalSection;
727f22ef01cSRoman Divacky     return DataCoalSection;
728f22ef01cSRoman Divacky   }
729f22ef01cSRoman Divacky 
730f22ef01cSRoman Divacky   // FIXME: Alignment check should be handled by section classifier.
731f22ef01cSRoman Divacky   if (Kind.isMergeable1ByteCString() &&
732f22ef01cSRoman Divacky       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
733f22ef01cSRoman Divacky     return CStringSection;
734f22ef01cSRoman Divacky 
735f22ef01cSRoman Divacky   // Do not put 16-bit arrays in the UString section if they have an
736f22ef01cSRoman Divacky   // externally visible label, this runs into issues with certain linker
737f22ef01cSRoman Divacky   // versions.
738f22ef01cSRoman Divacky   if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
739f22ef01cSRoman Divacky       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
740f22ef01cSRoman Divacky     return UStringSection;
741f22ef01cSRoman Divacky 
742f22ef01cSRoman Divacky   if (Kind.isMergeableConst()) {
743f22ef01cSRoman Divacky     if (Kind.isMergeableConst4())
744f22ef01cSRoman Divacky       return FourByteConstantSection;
745f22ef01cSRoman Divacky     if (Kind.isMergeableConst8())
746f22ef01cSRoman Divacky       return EightByteConstantSection;
747f22ef01cSRoman Divacky     if (Kind.isMergeableConst16() && SixteenByteConstantSection)
748f22ef01cSRoman Divacky       return SixteenByteConstantSection;
749f22ef01cSRoman Divacky   }
750f22ef01cSRoman Divacky 
751f22ef01cSRoman Divacky   // Otherwise, if it is readonly, but not something we can specially optimize,
752f22ef01cSRoman Divacky   // just drop it in .const.
753f22ef01cSRoman Divacky   if (Kind.isReadOnly())
754f22ef01cSRoman Divacky     return ReadOnlySection;
755f22ef01cSRoman Divacky 
756f22ef01cSRoman Divacky   // If this is marked const, put it into a const section.  But if the dynamic
757f22ef01cSRoman Divacky   // linker needs to write to it, put it in the data segment.
758f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRel())
759f22ef01cSRoman Divacky     return ConstDataSection;
760f22ef01cSRoman Divacky 
761f22ef01cSRoman Divacky   // Put zero initialized globals with strong external linkage in the
762f22ef01cSRoman Divacky   // DATA, __common section with the .zerofill directive.
763f22ef01cSRoman Divacky   if (Kind.isBSSExtern())
764f22ef01cSRoman Divacky     return DataCommonSection;
765f22ef01cSRoman Divacky 
766f22ef01cSRoman Divacky   // Put zero initialized globals with local linkage in __DATA,__bss directive
767f22ef01cSRoman Divacky   // with the .zerofill directive (aka .lcomm).
768f22ef01cSRoman Divacky   if (Kind.isBSSLocal())
769f22ef01cSRoman Divacky     return DataBSSSection;
770f22ef01cSRoman Divacky 
771f22ef01cSRoman Divacky   // Otherwise, just drop the variable in the normal data section.
772f22ef01cSRoman Divacky   return DataSection;
773f22ef01cSRoman Divacky }
774f22ef01cSRoman Divacky 
775f22ef01cSRoman Divacky const MCSection *
776f22ef01cSRoman Divacky TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
777f22ef01cSRoman Divacky   // If this constant requires a relocation, we have to put it in the data
778f22ef01cSRoman Divacky   // segment, not in the text segment.
779f22ef01cSRoman Divacky   if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
780f22ef01cSRoman Divacky     return ConstDataSection;
781f22ef01cSRoman Divacky 
782f22ef01cSRoman Divacky   if (Kind.isMergeableConst4())
783f22ef01cSRoman Divacky     return FourByteConstantSection;
784f22ef01cSRoman Divacky   if (Kind.isMergeableConst8())
785f22ef01cSRoman Divacky     return EightByteConstantSection;
786f22ef01cSRoman Divacky   if (Kind.isMergeableConst16() && SixteenByteConstantSection)
787f22ef01cSRoman Divacky     return SixteenByteConstantSection;
788f22ef01cSRoman Divacky   return ReadOnlySection;  // .const
789f22ef01cSRoman Divacky }
790f22ef01cSRoman Divacky 
791f22ef01cSRoman Divacky /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
792f22ef01cSRoman Divacky /// not to emit the UsedDirective for some symbols in llvm.used.
793f22ef01cSRoman Divacky // FIXME: REMOVE this (rdar://7071300)
794f22ef01cSRoman Divacky bool TargetLoweringObjectFileMachO::
795f22ef01cSRoman Divacky shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
796f22ef01cSRoman Divacky   /// On Darwin, internally linked data beginning with "L" or "l" does not have
797f22ef01cSRoman Divacky   /// the directive emitted (this occurs in ObjC metadata).
798f22ef01cSRoman Divacky   if (!GV) return false;
799f22ef01cSRoman Divacky 
800f22ef01cSRoman Divacky   // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
801f22ef01cSRoman Divacky   if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
802f22ef01cSRoman Divacky     // FIXME: ObjC metadata is currently emitted as internal symbols that have
803f22ef01cSRoman Divacky     // \1L and \0l prefixes on them.  Fix them to be Private/LinkerPrivate and
804f22ef01cSRoman Divacky     // this horrible hack can go away.
805f22ef01cSRoman Divacky     MCSymbol *Sym = Mang->getSymbol(GV);
806f22ef01cSRoman Divacky     if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
807f22ef01cSRoman Divacky       return false;
808f22ef01cSRoman Divacky   }
809f22ef01cSRoman Divacky 
810f22ef01cSRoman Divacky   return true;
811f22ef01cSRoman Divacky }
812f22ef01cSRoman Divacky 
813f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFileMachO::
814f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
815f22ef01cSRoman Divacky                                MachineModuleInfo *MMI, unsigned Encoding,
816f22ef01cSRoman Divacky                                MCStreamer &Streamer) const {
817f22ef01cSRoman Divacky   // The mach-o version of this method defaults to returning a stub reference.
818f22ef01cSRoman Divacky 
819f22ef01cSRoman Divacky   if (Encoding & DW_EH_PE_indirect) {
820f22ef01cSRoman Divacky     MachineModuleInfoMachO &MachOMMI =
821f22ef01cSRoman Divacky       MMI->getObjFileInfo<MachineModuleInfoMachO>();
822f22ef01cSRoman Divacky 
823f22ef01cSRoman Divacky     SmallString<128> Name;
824f22ef01cSRoman Divacky     Mang->getNameWithPrefix(Name, GV, true);
825f22ef01cSRoman Divacky     Name += "$non_lazy_ptr";
826f22ef01cSRoman Divacky 
827f22ef01cSRoman Divacky     // Add information about the stub reference to MachOMMI so that the stub
828f22ef01cSRoman Divacky     // gets emitted by the asmprinter.
829f22ef01cSRoman Divacky     MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
830f22ef01cSRoman Divacky     MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
831f22ef01cSRoman Divacky     if (StubSym.getPointer() == 0) {
832f22ef01cSRoman Divacky       MCSymbol *Sym = Mang->getSymbol(GV);
833f22ef01cSRoman Divacky       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
834f22ef01cSRoman Divacky     }
835f22ef01cSRoman Divacky 
836f22ef01cSRoman Divacky     return TargetLoweringObjectFile::
8373b0f4066SDimitry Andric       getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
838f22ef01cSRoman Divacky   }
839f22ef01cSRoman Divacky 
840f22ef01cSRoman Divacky   return TargetLoweringObjectFile::
841f22ef01cSRoman Divacky     getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
842f22ef01cSRoman Divacky }
843f22ef01cSRoman Divacky 
8443b0f4066SDimitry Andric MCSymbol *TargetLoweringObjectFileMachO::
8453b0f4066SDimitry Andric getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
8463b0f4066SDimitry Andric                         MachineModuleInfo *MMI) const {
8473b0f4066SDimitry Andric   // The mach-o version of this method defaults to returning a stub reference.
8483b0f4066SDimitry Andric   MachineModuleInfoMachO &MachOMMI =
8493b0f4066SDimitry Andric     MMI->getObjFileInfo<MachineModuleInfoMachO>();
8503b0f4066SDimitry Andric 
8513b0f4066SDimitry Andric   SmallString<128> Name;
8523b0f4066SDimitry Andric   Mang->getNameWithPrefix(Name, GV, true);
8533b0f4066SDimitry Andric   Name += "$non_lazy_ptr";
8543b0f4066SDimitry Andric 
8553b0f4066SDimitry Andric   // Add information about the stub reference to MachOMMI so that the stub
8563b0f4066SDimitry Andric   // gets emitted by the asmprinter.
8573b0f4066SDimitry Andric   MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
8583b0f4066SDimitry Andric   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
8593b0f4066SDimitry Andric   if (StubSym.getPointer() == 0) {
8603b0f4066SDimitry Andric     MCSymbol *Sym = Mang->getSymbol(GV);
8613b0f4066SDimitry Andric     StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
8623b0f4066SDimitry Andric   }
8633b0f4066SDimitry Andric 
8643b0f4066SDimitry Andric   return SSym;
8653b0f4066SDimitry Andric }
8663b0f4066SDimitry Andric 
867f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
868f22ef01cSRoman Divacky   return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
869f22ef01cSRoman Divacky }
870f22ef01cSRoman Divacky 
871f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
872f22ef01cSRoman Divacky   return DW_EH_PE_pcrel;
873f22ef01cSRoman Divacky }
874f22ef01cSRoman Divacky 
8753b0f4066SDimitry Andric unsigned TargetLoweringObjectFileMachO::getFDEEncoding(bool CFI) const {
876f22ef01cSRoman Divacky   return DW_EH_PE_pcrel;
877f22ef01cSRoman Divacky }
878f22ef01cSRoman Divacky 
879f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
880f22ef01cSRoman Divacky   return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
881f22ef01cSRoman Divacky }
882f22ef01cSRoman Divacky 
883f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
884f22ef01cSRoman Divacky //                                  COFF
885f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
886f22ef01cSRoman Divacky 
887f22ef01cSRoman Divacky void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
888f22ef01cSRoman Divacky                                               const TargetMachine &TM) {
889f22ef01cSRoman Divacky   TargetLoweringObjectFile::Initialize(Ctx, TM);
890f22ef01cSRoman Divacky   TextSection =
891f22ef01cSRoman Divacky     getContext().getCOFFSection(".text",
892ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_CODE |
893ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_EXECUTE |
894ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
895f22ef01cSRoman Divacky                                 SectionKind::getText());
896f22ef01cSRoman Divacky   DataSection =
897f22ef01cSRoman Divacky     getContext().getCOFFSection(".data",
898ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
899ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
900ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
901f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
902f22ef01cSRoman Divacky   ReadOnlySection =
903f22ef01cSRoman Divacky     getContext().getCOFFSection(".rdata",
904ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
905ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
906f22ef01cSRoman Divacky                                 SectionKind::getReadOnly());
907f22ef01cSRoman Divacky   StaticCtorSection =
908f22ef01cSRoman Divacky     getContext().getCOFFSection(".ctors",
909ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
910ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
911ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
912f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
913f22ef01cSRoman Divacky   StaticDtorSection =
914f22ef01cSRoman Divacky     getContext().getCOFFSection(".dtors",
915ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
916ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
917ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
918f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
919f22ef01cSRoman Divacky 
920f22ef01cSRoman Divacky   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
921f22ef01cSRoman Divacky   // though it contains relocatable pointers.  In PIC mode, this is probably a
922f22ef01cSRoman Divacky   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
923f22ef01cSRoman Divacky   // adjusted or this should be a data section.
924f22ef01cSRoman Divacky   LSDASection =
925f22ef01cSRoman Divacky     getContext().getCOFFSection(".gcc_except_table",
926ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
927ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
928f22ef01cSRoman Divacky                                 SectionKind::getReadOnly());
929f22ef01cSRoman Divacky   // Debug info.
930f22ef01cSRoman Divacky   DwarfAbbrevSection =
931f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_abbrev",
932ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
933ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
934f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
935f22ef01cSRoman Divacky   DwarfInfoSection =
936f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_info",
937ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
938ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
939f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
940f22ef01cSRoman Divacky   DwarfLineSection =
941f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_line",
942ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
943ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
944f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
945f22ef01cSRoman Divacky   DwarfFrameSection =
946f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_frame",
947ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
948ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
949f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
950f22ef01cSRoman Divacky   DwarfPubNamesSection =
951f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_pubnames",
952ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
953ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
954f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
955f22ef01cSRoman Divacky   DwarfPubTypesSection =
956f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_pubtypes",
957ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
958ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
959f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
960f22ef01cSRoman Divacky   DwarfStrSection =
961f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_str",
962ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
963ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
964f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
965f22ef01cSRoman Divacky   DwarfLocSection =
966f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_loc",
967ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
968ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
969f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
970f22ef01cSRoman Divacky   DwarfARangesSection =
971f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_aranges",
972ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
973ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
974f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
975f22ef01cSRoman Divacky   DwarfRangesSection =
976f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_ranges",
977ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
978ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
979f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
980f22ef01cSRoman Divacky   DwarfMacroInfoSection =
981f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_macinfo",
982ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
983ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
984f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
985f22ef01cSRoman Divacky 
986f22ef01cSRoman Divacky   DrectveSection =
987f22ef01cSRoman Divacky     getContext().getCOFFSection(".drectve",
988ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_LNK_INFO,
989f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
990bd5abe19SDimitry Andric 
991bd5abe19SDimitry Andric   PDataSection =
992bd5abe19SDimitry Andric     getContext().getCOFFSection(".pdata",
993bd5abe19SDimitry Andric                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
994bd5abe19SDimitry Andric                                 COFF::IMAGE_SCN_MEM_READ |
995bd5abe19SDimitry Andric                                 COFF::IMAGE_SCN_MEM_WRITE,
996bd5abe19SDimitry Andric                                 SectionKind::getDataRel());
997bd5abe19SDimitry Andric 
998bd5abe19SDimitry Andric   XDataSection =
999bd5abe19SDimitry Andric     getContext().getCOFFSection(".xdata",
1000bd5abe19SDimitry Andric                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1001bd5abe19SDimitry Andric                                 COFF::IMAGE_SCN_MEM_READ |
1002bd5abe19SDimitry Andric                                 COFF::IMAGE_SCN_MEM_WRITE,
1003bd5abe19SDimitry Andric                                 SectionKind::getDataRel());
1004f22ef01cSRoman Divacky }
1005f22ef01cSRoman Divacky 
10062754fe60SDimitry Andric const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
10072754fe60SDimitry Andric   return getContext().getCOFFSection(".eh_frame",
10082754fe60SDimitry Andric                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
10092754fe60SDimitry Andric                                      COFF::IMAGE_SCN_MEM_READ |
10102754fe60SDimitry Andric                                      COFF::IMAGE_SCN_MEM_WRITE,
10112754fe60SDimitry Andric                                      SectionKind::getDataRel());
10122754fe60SDimitry Andric }
10132754fe60SDimitry Andric 
1014bd5abe19SDimitry Andric const MCSection *TargetLoweringObjectFileCOFF::getWin64EHFuncTableSection(
1015bd5abe19SDimitry Andric                                                        StringRef suffix) const {
1016bd5abe19SDimitry Andric   if (suffix == "")
1017bd5abe19SDimitry Andric     return PDataSection;
1018bd5abe19SDimitry Andric   return getContext().getCOFFSection((".pdata"+suffix).str(),
1019bd5abe19SDimitry Andric                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1020bd5abe19SDimitry Andric                                      COFF::IMAGE_SCN_MEM_READ |
1021bd5abe19SDimitry Andric                                      COFF::IMAGE_SCN_MEM_WRITE,
1022bd5abe19SDimitry Andric                                      SectionKind::getDataRel());
1023bd5abe19SDimitry Andric }
1024bd5abe19SDimitry Andric 
1025bd5abe19SDimitry Andric const MCSection *TargetLoweringObjectFileCOFF::getWin64EHTableSection(
1026bd5abe19SDimitry Andric                                                        StringRef suffix) const {
1027bd5abe19SDimitry Andric   if (suffix == "")
1028bd5abe19SDimitry Andric     return XDataSection;
1029bd5abe19SDimitry Andric   return getContext().getCOFFSection((".xdata"+suffix).str(),
1030bd5abe19SDimitry Andric                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1031bd5abe19SDimitry Andric                                      COFF::IMAGE_SCN_MEM_READ |
1032bd5abe19SDimitry Andric                                      COFF::IMAGE_SCN_MEM_WRITE,
1033bd5abe19SDimitry Andric                                      SectionKind::getDataRel());
1034bd5abe19SDimitry Andric }
1035bd5abe19SDimitry Andric 
10362754fe60SDimitry Andric 
1037f22ef01cSRoman Divacky static unsigned
1038f22ef01cSRoman Divacky getCOFFSectionFlags(SectionKind K) {
1039f22ef01cSRoman Divacky   unsigned Flags = 0;
1040f22ef01cSRoman Divacky 
1041ffd1746dSEd Schouten   if (K.isMetadata())
1042f22ef01cSRoman Divacky     Flags |=
1043ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_DISCARDABLE;
1044f22ef01cSRoman Divacky   else if (K.isText())
1045f22ef01cSRoman Divacky     Flags |=
1046ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_EXECUTE |
10472754fe60SDimitry Andric       COFF::IMAGE_SCN_MEM_READ |
1048ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_CODE;
1049f22ef01cSRoman Divacky   else if (K.isBSS ())
1050f22ef01cSRoman Divacky     Flags |=
1051ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1052ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ |
1053ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_WRITE;
1054f22ef01cSRoman Divacky   else if (K.isReadOnly())
1055f22ef01cSRoman Divacky     Flags |=
1056ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1057ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ;
1058f22ef01cSRoman Divacky   else if (K.isWriteable())
1059f22ef01cSRoman Divacky     Flags |=
1060ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1061ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ |
1062ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_WRITE;
1063f22ef01cSRoman Divacky 
1064f22ef01cSRoman Divacky   return Flags;
1065f22ef01cSRoman Divacky }
1066f22ef01cSRoman Divacky 
1067f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileCOFF::
1068f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1069f22ef01cSRoman Divacky                          Mangler *Mang, const TargetMachine &TM) const {
1070f22ef01cSRoman Divacky   return getContext().getCOFFSection(GV->getSection(),
1071f22ef01cSRoman Divacky                                      getCOFFSectionFlags(Kind),
1072f22ef01cSRoman Divacky                                      Kind);
1073f22ef01cSRoman Divacky }
1074f22ef01cSRoman Divacky 
1075f22ef01cSRoman Divacky static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1076f22ef01cSRoman Divacky   if (Kind.isText())
10772754fe60SDimitry Andric     return ".text$";
1078f22ef01cSRoman Divacky   if (Kind.isBSS ())
10792754fe60SDimitry Andric     return ".bss$";
1080f22ef01cSRoman Divacky   if (Kind.isWriteable())
10812754fe60SDimitry Andric     return ".data$";
10822754fe60SDimitry Andric   return ".rdata$";
1083f22ef01cSRoman Divacky }
1084f22ef01cSRoman Divacky 
1085f22ef01cSRoman Divacky 
1086f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileCOFF::
1087f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1088f22ef01cSRoman Divacky                        Mangler *Mang, const TargetMachine &TM) const {
1089f22ef01cSRoman Divacky   assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1090f22ef01cSRoman Divacky 
1091f22ef01cSRoman Divacky   // If this global is linkonce/weak and the target handles this by emitting it
1092f22ef01cSRoman Divacky   // into a 'uniqued' section name, create and return the section now.
1093f22ef01cSRoman Divacky   if (GV->isWeakForLinker()) {
1094f22ef01cSRoman Divacky     const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1095f22ef01cSRoman Divacky     SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
1096f22ef01cSRoman Divacky     MCSymbol *Sym = Mang->getSymbol(GV);
10972754fe60SDimitry Andric     Name.append(Sym->getName().begin() + 1, Sym->getName().end());
1098f22ef01cSRoman Divacky 
1099f22ef01cSRoman Divacky     unsigned Characteristics = getCOFFSectionFlags(Kind);
1100f22ef01cSRoman Divacky 
1101ffd1746dSEd Schouten     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1102f22ef01cSRoman Divacky 
1103f22ef01cSRoman Divacky     return getContext().getCOFFSection(Name.str(), Characteristics,
11042754fe60SDimitry Andric                           COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
1105f22ef01cSRoman Divacky   }
1106f22ef01cSRoman Divacky 
1107f22ef01cSRoman Divacky   if (Kind.isText())
1108f22ef01cSRoman Divacky     return getTextSection();
1109f22ef01cSRoman Divacky 
1110f22ef01cSRoman Divacky   return getDataSection();
1111f22ef01cSRoman Divacky }
1112f22ef01cSRoman Divacky 
1113