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) {
228dd6029ffSDimitry Andric   // FIXME: Why is this here? Codegen is should not be in the business
229dd6029ffSDimitry Andric   // of figuring section flags. If the user wrote section(".eh_frame"),
230dd6029ffSDimitry Andric   // we should just pass that to MC which will defer to the assembly
231dd6029ffSDimitry Andric   // or use its default if producing an object file.
232f22ef01cSRoman Divacky   if (Name.empty() || Name[0] != '.') return K;
233f22ef01cSRoman Divacky 
234f22ef01cSRoman Divacky   // Some lame default implementation based on some magic section names.
235f22ef01cSRoman Divacky   if (Name == ".bss" ||
236f22ef01cSRoman Divacky       Name.startswith(".bss.") ||
237f22ef01cSRoman Divacky       Name.startswith(".gnu.linkonce.b.") ||
238f22ef01cSRoman Divacky       Name.startswith(".llvm.linkonce.b.") ||
239f22ef01cSRoman Divacky       Name == ".sbss" ||
240f22ef01cSRoman Divacky       Name.startswith(".sbss.") ||
241f22ef01cSRoman Divacky       Name.startswith(".gnu.linkonce.sb.") ||
242f22ef01cSRoman Divacky       Name.startswith(".llvm.linkonce.sb."))
243f22ef01cSRoman Divacky     return SectionKind::getBSS();
244f22ef01cSRoman Divacky 
245f22ef01cSRoman Divacky   if (Name == ".tdata" ||
246f22ef01cSRoman Divacky       Name.startswith(".tdata.") ||
247f22ef01cSRoman Divacky       Name.startswith(".gnu.linkonce.td.") ||
248f22ef01cSRoman Divacky       Name.startswith(".llvm.linkonce.td."))
249f22ef01cSRoman Divacky     return SectionKind::getThreadData();
250f22ef01cSRoman Divacky 
251f22ef01cSRoman Divacky   if (Name == ".tbss" ||
252f22ef01cSRoman Divacky       Name.startswith(".tbss.") ||
253f22ef01cSRoman Divacky       Name.startswith(".gnu.linkonce.tb.") ||
254f22ef01cSRoman Divacky       Name.startswith(".llvm.linkonce.tb."))
255f22ef01cSRoman Divacky     return SectionKind::getThreadBSS();
256f22ef01cSRoman Divacky 
257dd6029ffSDimitry Andric   if (Name == ".eh_frame")
258dd6029ffSDimitry Andric     return SectionKind::getDataRel();
259dd6029ffSDimitry Andric 
260f22ef01cSRoman Divacky   return K;
261f22ef01cSRoman Divacky }
262f22ef01cSRoman Divacky 
263f22ef01cSRoman Divacky 
264f22ef01cSRoman Divacky static unsigned getELFSectionType(StringRef Name, SectionKind K) {
265f22ef01cSRoman Divacky 
266f22ef01cSRoman Divacky   if (Name == ".init_array")
2672754fe60SDimitry Andric     return ELF::SHT_INIT_ARRAY;
268f22ef01cSRoman Divacky 
269f22ef01cSRoman Divacky   if (Name == ".fini_array")
2702754fe60SDimitry Andric     return ELF::SHT_FINI_ARRAY;
271f22ef01cSRoman Divacky 
272f22ef01cSRoman Divacky   if (Name == ".preinit_array")
2732754fe60SDimitry Andric     return ELF::SHT_PREINIT_ARRAY;
274f22ef01cSRoman Divacky 
275f22ef01cSRoman Divacky   if (K.isBSS() || K.isThreadBSS())
2762754fe60SDimitry Andric     return ELF::SHT_NOBITS;
277f22ef01cSRoman Divacky 
2782754fe60SDimitry Andric   return ELF::SHT_PROGBITS;
279f22ef01cSRoman Divacky }
280f22ef01cSRoman Divacky 
281f22ef01cSRoman Divacky 
282f22ef01cSRoman Divacky static unsigned
283f22ef01cSRoman Divacky getELFSectionFlags(SectionKind K) {
284f22ef01cSRoman Divacky   unsigned Flags = 0;
285f22ef01cSRoman Divacky 
286f22ef01cSRoman Divacky   if (!K.isMetadata())
2872754fe60SDimitry Andric     Flags |= ELF::SHF_ALLOC;
288f22ef01cSRoman Divacky 
289f22ef01cSRoman Divacky   if (K.isText())
2902754fe60SDimitry Andric     Flags |= ELF::SHF_EXECINSTR;
291f22ef01cSRoman Divacky 
292f22ef01cSRoman Divacky   if (K.isWriteable())
2932754fe60SDimitry Andric     Flags |= ELF::SHF_WRITE;
294f22ef01cSRoman Divacky 
295f22ef01cSRoman Divacky   if (K.isThreadLocal())
2962754fe60SDimitry Andric     Flags |= ELF::SHF_TLS;
297f22ef01cSRoman Divacky 
298f22ef01cSRoman Divacky   // K.isMergeableConst() is left out to honour PR4650
299f22ef01cSRoman Divacky   if (K.isMergeableCString() || K.isMergeableConst4() ||
300f22ef01cSRoman Divacky       K.isMergeableConst8() || K.isMergeableConst16())
3012754fe60SDimitry Andric     Flags |= ELF::SHF_MERGE;
302f22ef01cSRoman Divacky 
303f22ef01cSRoman Divacky   if (K.isMergeableCString())
3042754fe60SDimitry Andric     Flags |= ELF::SHF_STRINGS;
305f22ef01cSRoman Divacky 
306f22ef01cSRoman Divacky   return Flags;
307f22ef01cSRoman Divacky }
308f22ef01cSRoman Divacky 
309f22ef01cSRoman Divacky 
310f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF::
311f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
312f22ef01cSRoman Divacky                          Mangler *Mang, const TargetMachine &TM) const {
313f22ef01cSRoman Divacky   StringRef SectionName = GV->getSection();
314f22ef01cSRoman Divacky 
315f22ef01cSRoman Divacky   // Infer section flags from the section name if we can.
316f22ef01cSRoman Divacky   Kind = getELFKindForNamedSection(SectionName, Kind);
317f22ef01cSRoman Divacky 
318f22ef01cSRoman Divacky   return getContext().getELFSection(SectionName,
319f22ef01cSRoman Divacky                                     getELFSectionType(SectionName, Kind),
3202754fe60SDimitry Andric                                     getELFSectionFlags(Kind), Kind);
321f22ef01cSRoman Divacky }
322f22ef01cSRoman Divacky 
323f22ef01cSRoman Divacky /// getSectionPrefixForGlobal - Return the section prefix name used by options
324f22ef01cSRoman Divacky /// FunctionsSections and DataSections.
325f22ef01cSRoman Divacky static const char *getSectionPrefixForGlobal(SectionKind Kind) {
326f22ef01cSRoman Divacky   if (Kind.isText())                 return ".text.";
327f22ef01cSRoman Divacky   if (Kind.isReadOnly())             return ".rodata.";
328f22ef01cSRoman Divacky 
329f22ef01cSRoman Divacky   if (Kind.isThreadData())           return ".tdata.";
330f22ef01cSRoman Divacky   if (Kind.isThreadBSS())            return ".tbss.";
331f22ef01cSRoman Divacky 
332f22ef01cSRoman Divacky   if (Kind.isDataNoRel())            return ".data.";
333f22ef01cSRoman Divacky   if (Kind.isDataRelLocal())         return ".data.rel.local.";
334f22ef01cSRoman Divacky   if (Kind.isDataRel())              return ".data.rel.";
335f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
336f22ef01cSRoman Divacky 
337f22ef01cSRoman Divacky   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
338f22ef01cSRoman Divacky   return ".data.rel.ro.";
339f22ef01cSRoman Divacky }
340f22ef01cSRoman Divacky 
341f22ef01cSRoman Divacky 
342f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF::
343f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
344f22ef01cSRoman Divacky                        Mangler *Mang, const TargetMachine &TM) const {
345f22ef01cSRoman Divacky   // If we have -ffunction-section or -fdata-section then we should emit the
346f22ef01cSRoman Divacky   // global value to a uniqued section specifically for it.
347f22ef01cSRoman Divacky   bool EmitUniquedSection;
348f22ef01cSRoman Divacky   if (Kind.isText())
349f22ef01cSRoman Divacky     EmitUniquedSection = TM.getFunctionSections();
350f22ef01cSRoman Divacky   else
351f22ef01cSRoman Divacky     EmitUniquedSection = TM.getDataSections();
352f22ef01cSRoman Divacky 
353f22ef01cSRoman Divacky   // If this global is linkonce/weak and the target handles this by emitting it
354f22ef01cSRoman Divacky   // into a 'uniqued' section name, create and return the section now.
355f22ef01cSRoman Divacky   if ((GV->isWeakForLinker() || EmitUniquedSection) &&
356f22ef01cSRoman Divacky       !Kind.isCommon() && !Kind.isBSS()) {
357f22ef01cSRoman Divacky     const char *Prefix;
358f22ef01cSRoman Divacky     Prefix = getSectionPrefixForGlobal(Kind);
359f22ef01cSRoman Divacky 
360f22ef01cSRoman Divacky     SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
361f22ef01cSRoman Divacky     MCSymbol *Sym = Mang->getSymbol(GV);
362f22ef01cSRoman Divacky     Name.append(Sym->getName().begin(), Sym->getName().end());
3632754fe60SDimitry Andric     StringRef Group = "";
3642754fe60SDimitry Andric     unsigned Flags = getELFSectionFlags(Kind);
3652754fe60SDimitry Andric     if (GV->isWeakForLinker()) {
3662754fe60SDimitry Andric       Group = Sym->getName();
3672754fe60SDimitry Andric       Flags |= ELF::SHF_GROUP;
3682754fe60SDimitry Andric     }
3692754fe60SDimitry Andric 
370f22ef01cSRoman Divacky     return getContext().getELFSection(Name.str(),
371f22ef01cSRoman Divacky                                       getELFSectionType(Name.str(), Kind),
3722754fe60SDimitry Andric                                       Flags, Kind, 0, Group);
373f22ef01cSRoman Divacky   }
374f22ef01cSRoman Divacky 
375f22ef01cSRoman Divacky   if (Kind.isText()) return TextSection;
376f22ef01cSRoman Divacky 
377f22ef01cSRoman Divacky   if (Kind.isMergeable1ByteCString() ||
378f22ef01cSRoman Divacky       Kind.isMergeable2ByteCString() ||
379f22ef01cSRoman Divacky       Kind.isMergeable4ByteCString()) {
380f22ef01cSRoman Divacky 
381f22ef01cSRoman Divacky     // We also need alignment here.
382f22ef01cSRoman Divacky     // FIXME: this is getting the alignment of the character, not the
383f22ef01cSRoman Divacky     // alignment of the global!
384f22ef01cSRoman Divacky     unsigned Align =
385f22ef01cSRoman Divacky       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
386f22ef01cSRoman Divacky 
387f22ef01cSRoman Divacky     const char *SizeSpec = ".rodata.str1.";
388f22ef01cSRoman Divacky     if (Kind.isMergeable2ByteCString())
389f22ef01cSRoman Divacky       SizeSpec = ".rodata.str2.";
390f22ef01cSRoman Divacky     else if (Kind.isMergeable4ByteCString())
391f22ef01cSRoman Divacky       SizeSpec = ".rodata.str4.";
392f22ef01cSRoman Divacky     else
393f22ef01cSRoman Divacky       assert(Kind.isMergeable1ByteCString() && "unknown string width");
394f22ef01cSRoman Divacky 
395f22ef01cSRoman Divacky 
396f22ef01cSRoman Divacky     std::string Name = SizeSpec + utostr(Align);
3972754fe60SDimitry Andric     return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
3982754fe60SDimitry Andric                                       ELF::SHF_ALLOC |
3992754fe60SDimitry Andric                                       ELF::SHF_MERGE |
4002754fe60SDimitry Andric                                       ELF::SHF_STRINGS,
401f22ef01cSRoman Divacky                                       Kind);
402f22ef01cSRoman Divacky   }
403f22ef01cSRoman Divacky 
404f22ef01cSRoman Divacky   if (Kind.isMergeableConst()) {
405f22ef01cSRoman Divacky     if (Kind.isMergeableConst4() && MergeableConst4Section)
406f22ef01cSRoman Divacky       return MergeableConst4Section;
407f22ef01cSRoman Divacky     if (Kind.isMergeableConst8() && MergeableConst8Section)
408f22ef01cSRoman Divacky       return MergeableConst8Section;
409f22ef01cSRoman Divacky     if (Kind.isMergeableConst16() && MergeableConst16Section)
410f22ef01cSRoman Divacky       return MergeableConst16Section;
411f22ef01cSRoman Divacky     return ReadOnlySection;  // .const
412f22ef01cSRoman Divacky   }
413f22ef01cSRoman Divacky 
414f22ef01cSRoman Divacky   if (Kind.isReadOnly())             return ReadOnlySection;
415f22ef01cSRoman Divacky 
416f22ef01cSRoman Divacky   if (Kind.isThreadData())           return TLSDataSection;
417f22ef01cSRoman Divacky   if (Kind.isThreadBSS())            return TLSBSSSection;
418f22ef01cSRoman Divacky 
419f22ef01cSRoman Divacky   // Note: we claim that common symbols are put in BSSSection, but they are
420f22ef01cSRoman Divacky   // really emitted with the magic .comm directive, which creates a symbol table
421f22ef01cSRoman Divacky   // entry but not a section.
422f22ef01cSRoman Divacky   if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
423f22ef01cSRoman Divacky 
424f22ef01cSRoman Divacky   if (Kind.isDataNoRel())            return DataSection;
425f22ef01cSRoman Divacky   if (Kind.isDataRelLocal())         return DataRelLocalSection;
426f22ef01cSRoman Divacky   if (Kind.isDataRel())              return DataRelSection;
427f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
428f22ef01cSRoman Divacky 
429f22ef01cSRoman Divacky   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
430f22ef01cSRoman Divacky   return DataRelROSection;
431f22ef01cSRoman Divacky }
432f22ef01cSRoman Divacky 
433f22ef01cSRoman Divacky /// getSectionForConstant - Given a mergeable constant with the
434f22ef01cSRoman Divacky /// specified size and relocation information, return a section that it
435f22ef01cSRoman Divacky /// should be placed in.
436f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileELF::
437f22ef01cSRoman Divacky getSectionForConstant(SectionKind Kind) const {
438f22ef01cSRoman Divacky   if (Kind.isMergeableConst4() && MergeableConst4Section)
439f22ef01cSRoman Divacky     return MergeableConst4Section;
440f22ef01cSRoman Divacky   if (Kind.isMergeableConst8() && MergeableConst8Section)
441f22ef01cSRoman Divacky     return MergeableConst8Section;
442f22ef01cSRoman Divacky   if (Kind.isMergeableConst16() && MergeableConst16Section)
443f22ef01cSRoman Divacky     return MergeableConst16Section;
444f22ef01cSRoman Divacky   if (Kind.isReadOnly())
445f22ef01cSRoman Divacky     return ReadOnlySection;
446f22ef01cSRoman Divacky 
447f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
448f22ef01cSRoman Divacky   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
449f22ef01cSRoman Divacky   return DataRelROSection;
450f22ef01cSRoman Divacky }
451f22ef01cSRoman Divacky 
452f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFileELF::
453f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
454f22ef01cSRoman Divacky                                MachineModuleInfo *MMI,
455f22ef01cSRoman Divacky                                unsigned Encoding, MCStreamer &Streamer) const {
456f22ef01cSRoman Divacky 
457f22ef01cSRoman Divacky   if (Encoding & dwarf::DW_EH_PE_indirect) {
458f22ef01cSRoman Divacky     MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
459f22ef01cSRoman Divacky 
460f22ef01cSRoman Divacky     SmallString<128> Name;
461f22ef01cSRoman Divacky     Mang->getNameWithPrefix(Name, GV, true);
462f22ef01cSRoman Divacky     Name += ".DW.stub";
463f22ef01cSRoman Divacky 
464f22ef01cSRoman Divacky     // Add information about the stub reference to ELFMMI so that the stub
465f22ef01cSRoman Divacky     // gets emitted by the asmprinter.
466f22ef01cSRoman Divacky     MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
467f22ef01cSRoman Divacky     MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
468f22ef01cSRoman Divacky     if (StubSym.getPointer() == 0) {
469f22ef01cSRoman Divacky       MCSymbol *Sym = Mang->getSymbol(GV);
470f22ef01cSRoman Divacky       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
471f22ef01cSRoman Divacky     }
472f22ef01cSRoman Divacky 
473f22ef01cSRoman Divacky     return TargetLoweringObjectFile::
4743b0f4066SDimitry Andric       getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
475f22ef01cSRoman Divacky   }
476f22ef01cSRoman Divacky 
477f22ef01cSRoman Divacky   return TargetLoweringObjectFile::
478f22ef01cSRoman Divacky     getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
479f22ef01cSRoman Divacky }
480f22ef01cSRoman Divacky 
481f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
482f22ef01cSRoman Divacky //                                 MachO
483f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
484f22ef01cSRoman Divacky 
485f22ef01cSRoman Divacky void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
486f22ef01cSRoman Divacky                                                const TargetMachine &TM) {
487f22ef01cSRoman Divacky   // _foo.eh symbols are currently always exported so that the linker knows
488f22ef01cSRoman Divacky   // about them.  This is not necessary on 10.6 and later, but it
489f22ef01cSRoman Divacky   // doesn't hurt anything.
490f22ef01cSRoman Divacky   // FIXME: I need to get this from Triple.
491f22ef01cSRoman Divacky   IsFunctionEHSymbolGlobal = true;
492f22ef01cSRoman Divacky   IsFunctionEHFrameSymbolPrivate = false;
493f22ef01cSRoman Divacky   SupportsWeakOmittedEHFrame = false;
494f22ef01cSRoman Divacky 
4953b0f4066SDimitry Andric   // .comm doesn't support alignment before Leopard.
4962754fe60SDimitry Andric   Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
4973b0f4066SDimitry Andric   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
4982754fe60SDimitry Andric     CommDirectiveSupportsAlignment = false;
4992754fe60SDimitry Andric 
500f22ef01cSRoman Divacky   TargetLoweringObjectFile::Initialize(Ctx, TM);
501f22ef01cSRoman Divacky 
502f22ef01cSRoman Divacky   TextSection // .text
503f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__text",
504f22ef01cSRoman Divacky                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
505f22ef01cSRoman Divacky                                    SectionKind::getText());
506f22ef01cSRoman Divacky   DataSection // .data
507f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__data", 0,
508f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
509f22ef01cSRoman Divacky 
510f22ef01cSRoman Divacky   TLSDataSection // .tdata
511f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__thread_data",
512f22ef01cSRoman Divacky                                    MCSectionMachO::S_THREAD_LOCAL_REGULAR,
513f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
514f22ef01cSRoman Divacky   TLSBSSSection // .tbss
515f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__thread_bss",
516f22ef01cSRoman Divacky                                    MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
517f22ef01cSRoman Divacky                                    SectionKind::getThreadBSS());
518f22ef01cSRoman Divacky 
519f22ef01cSRoman Divacky   // TODO: Verify datarel below.
520f22ef01cSRoman Divacky   TLSTLVSection // .tlv
521f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__thread_vars",
522f22ef01cSRoman Divacky                                    MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
523f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
524f22ef01cSRoman Divacky 
525f22ef01cSRoman Divacky   TLSThreadInitSection
526f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__thread_init",
527f22ef01cSRoman Divacky                           MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
528f22ef01cSRoman Divacky                           SectionKind::getDataRel());
529f22ef01cSRoman Divacky 
530f22ef01cSRoman Divacky   CStringSection // .cstring
531f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__cstring",
532f22ef01cSRoman Divacky                                    MCSectionMachO::S_CSTRING_LITERALS,
533f22ef01cSRoman Divacky                                    SectionKind::getMergeable1ByteCString());
534f22ef01cSRoman Divacky   UStringSection
535f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT","__ustring", 0,
536f22ef01cSRoman Divacky                                    SectionKind::getMergeable2ByteCString());
537f22ef01cSRoman Divacky   FourByteConstantSection // .literal4
538f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__literal4",
539f22ef01cSRoman Divacky                                    MCSectionMachO::S_4BYTE_LITERALS,
540f22ef01cSRoman Divacky                                    SectionKind::getMergeableConst4());
541f22ef01cSRoman Divacky   EightByteConstantSection // .literal8
542f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__literal8",
543f22ef01cSRoman Divacky                                    MCSectionMachO::S_8BYTE_LITERALS,
544f22ef01cSRoman Divacky                                    SectionKind::getMergeableConst8());
545f22ef01cSRoman Divacky 
546f22ef01cSRoman Divacky   // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
547f22ef01cSRoman Divacky   // to using it in -static mode.
548f22ef01cSRoman Divacky   SixteenByteConstantSection = 0;
549f22ef01cSRoman Divacky   if (TM.getRelocationModel() != Reloc::Static &&
550f22ef01cSRoman Divacky       TM.getTargetData()->getPointerSize() == 32)
551f22ef01cSRoman Divacky     SixteenByteConstantSection =   // .literal16
552f22ef01cSRoman Divacky       getContext().getMachOSection("__TEXT", "__literal16",
553f22ef01cSRoman Divacky                                    MCSectionMachO::S_16BYTE_LITERALS,
554f22ef01cSRoman Divacky                                    SectionKind::getMergeableConst16());
555f22ef01cSRoman Divacky 
556f22ef01cSRoman Divacky   ReadOnlySection  // .const
557f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__const", 0,
558f22ef01cSRoman Divacky                                    SectionKind::getReadOnly());
559f22ef01cSRoman Divacky 
560f22ef01cSRoman Divacky   TextCoalSection
561f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__textcoal_nt",
562f22ef01cSRoman Divacky                                    MCSectionMachO::S_COALESCED |
563f22ef01cSRoman Divacky                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
564f22ef01cSRoman Divacky                                    SectionKind::getText());
565f22ef01cSRoman Divacky   ConstTextCoalSection
566f22ef01cSRoman Divacky     = getContext().getMachOSection("__TEXT", "__const_coal",
567f22ef01cSRoman Divacky                                    MCSectionMachO::S_COALESCED,
568e580952dSDimitry Andric                                    SectionKind::getReadOnly());
569f22ef01cSRoman Divacky   ConstDataSection  // .const_data
570f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__const", 0,
571f22ef01cSRoman Divacky                                    SectionKind::getReadOnlyWithRel());
572f22ef01cSRoman Divacky   DataCoalSection
573f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__datacoal_nt",
574f22ef01cSRoman Divacky                                    MCSectionMachO::S_COALESCED,
575f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
576f22ef01cSRoman Divacky   DataCommonSection
577f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__common",
578f22ef01cSRoman Divacky                                    MCSectionMachO::S_ZEROFILL,
579f22ef01cSRoman Divacky                                    SectionKind::getBSS());
580f22ef01cSRoman Divacky   DataBSSSection
581f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
582f22ef01cSRoman Divacky                                    SectionKind::getBSS());
583f22ef01cSRoman Divacky 
584f22ef01cSRoman Divacky 
585f22ef01cSRoman Divacky   LazySymbolPointerSection
586f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
587f22ef01cSRoman Divacky                                    MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
588f22ef01cSRoman Divacky                                    SectionKind::getMetadata());
589f22ef01cSRoman Divacky   NonLazySymbolPointerSection
590f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
591f22ef01cSRoman Divacky                                    MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
592f22ef01cSRoman Divacky                                    SectionKind::getMetadata());
593f22ef01cSRoman Divacky 
594f22ef01cSRoman Divacky   if (TM.getRelocationModel() == Reloc::Static) {
595f22ef01cSRoman Divacky     StaticCtorSection
596f22ef01cSRoman Divacky       = getContext().getMachOSection("__TEXT", "__constructor", 0,
597f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
598f22ef01cSRoman Divacky     StaticDtorSection
599f22ef01cSRoman Divacky       = getContext().getMachOSection("__TEXT", "__destructor", 0,
600f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
601f22ef01cSRoman Divacky   } else {
602f22ef01cSRoman Divacky     StaticCtorSection
603f22ef01cSRoman Divacky       = getContext().getMachOSection("__DATA", "__mod_init_func",
604f22ef01cSRoman Divacky                                      MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
605f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
606f22ef01cSRoman Divacky     StaticDtorSection
607f22ef01cSRoman Divacky       = getContext().getMachOSection("__DATA", "__mod_term_func",
608f22ef01cSRoman Divacky                                      MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
609f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
610f22ef01cSRoman Divacky   }
611f22ef01cSRoman Divacky 
612f22ef01cSRoman Divacky   // Exception Handling.
613f22ef01cSRoman Divacky   LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
614f22ef01cSRoman Divacky                                              SectionKind::getReadOnlyWithRel());
615f22ef01cSRoman Divacky   // Debug Information.
616f22ef01cSRoman Divacky   DwarfAbbrevSection =
617f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_abbrev",
618f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
619f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
620f22ef01cSRoman Divacky   DwarfInfoSection =
621f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_info",
622f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
623f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
624f22ef01cSRoman Divacky   DwarfLineSection =
625f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_line",
626f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
627f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
628f22ef01cSRoman Divacky   DwarfFrameSection =
629f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_frame",
630f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
631f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
632f22ef01cSRoman Divacky   DwarfPubNamesSection =
633f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_pubnames",
634f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
635f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
636f22ef01cSRoman Divacky   DwarfPubTypesSection =
637f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_pubtypes",
638f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
639f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
640f22ef01cSRoman Divacky   DwarfStrSection =
641f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_str",
642f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
643f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
644f22ef01cSRoman Divacky   DwarfLocSection =
645f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_loc",
646f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
647f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
648f22ef01cSRoman Divacky   DwarfARangesSection =
649f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_aranges",
650f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
651f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
652f22ef01cSRoman Divacky   DwarfRangesSection =
653f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_ranges",
654f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
655f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
656f22ef01cSRoman Divacky   DwarfMacroInfoSection =
657f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_macinfo",
658f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
659f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
660f22ef01cSRoman Divacky   DwarfDebugInlineSection =
661f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_inlined",
662f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
663f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
664f22ef01cSRoman Divacky 
665f22ef01cSRoman Divacky   TLSExtraDataSection = TLSTLVSection;
666f22ef01cSRoman Divacky }
667f22ef01cSRoman Divacky 
6682754fe60SDimitry Andric const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
6692754fe60SDimitry Andric   return getContext().getMachOSection("__TEXT", "__eh_frame",
6702754fe60SDimitry Andric                                       MCSectionMachO::S_COALESCED |
6712754fe60SDimitry Andric                                       MCSectionMachO::S_ATTR_NO_TOC |
6722754fe60SDimitry Andric                                       MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
6732754fe60SDimitry Andric                                       MCSectionMachO::S_ATTR_LIVE_SUPPORT,
6742754fe60SDimitry Andric                                       SectionKind::getReadOnly());
6752754fe60SDimitry Andric }
6762754fe60SDimitry Andric 
677f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileMachO::
678f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
679f22ef01cSRoman Divacky                          Mangler *Mang, const TargetMachine &TM) const {
680f22ef01cSRoman Divacky   // Parse the section specifier and create it if valid.
681f22ef01cSRoman Divacky   StringRef Segment, Section;
6823b0f4066SDimitry Andric   unsigned TAA = 0, StubSize = 0;
6833b0f4066SDimitry Andric   bool TAAParsed;
684f22ef01cSRoman Divacky   std::string ErrorCode =
685f22ef01cSRoman Divacky     MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
6863b0f4066SDimitry Andric                                           TAA, TAAParsed, StubSize);
687f22ef01cSRoman Divacky   if (!ErrorCode.empty()) {
688f22ef01cSRoman Divacky     // If invalid, report the error with report_fatal_error.
689f22ef01cSRoman Divacky     report_fatal_error("Global variable '" + GV->getNameStr() +
690f22ef01cSRoman Divacky                       "' has an invalid section specifier '" + GV->getSection()+
691f22ef01cSRoman Divacky                       "': " + ErrorCode + ".");
692f22ef01cSRoman Divacky     // Fall back to dropping it into the data section.
693f22ef01cSRoman Divacky     return DataSection;
694f22ef01cSRoman Divacky   }
695f22ef01cSRoman Divacky 
696f22ef01cSRoman Divacky   // Get the section.
697f22ef01cSRoman Divacky   const MCSectionMachO *S =
698f22ef01cSRoman Divacky     getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
699f22ef01cSRoman Divacky 
700dd6029ffSDimitry Andric   // If TAA wasn't set by ParseSectionSpecifier() above,
701dd6029ffSDimitry Andric   // use the value returned by getMachOSection() as a default.
7023b0f4066SDimitry Andric   if (!TAAParsed)
703dd6029ffSDimitry Andric     TAA = S->getTypeAndAttributes();
704dd6029ffSDimitry Andric 
705f22ef01cSRoman Divacky   // Okay, now that we got the section, verify that the TAA & StubSize agree.
706f22ef01cSRoman Divacky   // If the user declared multiple globals with different section flags, we need
707f22ef01cSRoman Divacky   // to reject it here.
708f22ef01cSRoman Divacky   if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
709f22ef01cSRoman Divacky     // If invalid, report the error with report_fatal_error.
710f22ef01cSRoman Divacky     report_fatal_error("Global variable '" + GV->getNameStr() +
711f22ef01cSRoman Divacky                       "' section type or attributes does not match previous"
712f22ef01cSRoman Divacky                       " section specifier");
713f22ef01cSRoman Divacky   }
714f22ef01cSRoman Divacky 
715f22ef01cSRoman Divacky   return S;
716f22ef01cSRoman Divacky }
717f22ef01cSRoman Divacky 
718f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileMachO::
719f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
720f22ef01cSRoman Divacky                        Mangler *Mang, const TargetMachine &TM) const {
721f22ef01cSRoman Divacky 
722f22ef01cSRoman Divacky   // Handle thread local data.
723f22ef01cSRoman Divacky   if (Kind.isThreadBSS()) return TLSBSSSection;
724f22ef01cSRoman Divacky   if (Kind.isThreadData()) return TLSDataSection;
725f22ef01cSRoman Divacky 
726f22ef01cSRoman Divacky   if (Kind.isText())
727f22ef01cSRoman Divacky     return GV->isWeakForLinker() ? TextCoalSection : TextSection;
728f22ef01cSRoman Divacky 
729f22ef01cSRoman Divacky   // If this is weak/linkonce, put this in a coalescable section, either in text
730f22ef01cSRoman Divacky   // or data depending on if it is writable.
731f22ef01cSRoman Divacky   if (GV->isWeakForLinker()) {
732f22ef01cSRoman Divacky     if (Kind.isReadOnly())
733f22ef01cSRoman Divacky       return ConstTextCoalSection;
734f22ef01cSRoman Divacky     return DataCoalSection;
735f22ef01cSRoman Divacky   }
736f22ef01cSRoman Divacky 
737f22ef01cSRoman Divacky   // FIXME: Alignment check should be handled by section classifier.
738f22ef01cSRoman Divacky   if (Kind.isMergeable1ByteCString() &&
739f22ef01cSRoman Divacky       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
740f22ef01cSRoman Divacky     return CStringSection;
741f22ef01cSRoman Divacky 
742f22ef01cSRoman Divacky   // Do not put 16-bit arrays in the UString section if they have an
743f22ef01cSRoman Divacky   // externally visible label, this runs into issues with certain linker
744f22ef01cSRoman Divacky   // versions.
745f22ef01cSRoman Divacky   if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
746f22ef01cSRoman Divacky       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
747f22ef01cSRoman Divacky     return UStringSection;
748f22ef01cSRoman Divacky 
749f22ef01cSRoman Divacky   if (Kind.isMergeableConst()) {
750f22ef01cSRoman Divacky     if (Kind.isMergeableConst4())
751f22ef01cSRoman Divacky       return FourByteConstantSection;
752f22ef01cSRoman Divacky     if (Kind.isMergeableConst8())
753f22ef01cSRoman Divacky       return EightByteConstantSection;
754f22ef01cSRoman Divacky     if (Kind.isMergeableConst16() && SixteenByteConstantSection)
755f22ef01cSRoman Divacky       return SixteenByteConstantSection;
756f22ef01cSRoman Divacky   }
757f22ef01cSRoman Divacky 
758f22ef01cSRoman Divacky   // Otherwise, if it is readonly, but not something we can specially optimize,
759f22ef01cSRoman Divacky   // just drop it in .const.
760f22ef01cSRoman Divacky   if (Kind.isReadOnly())
761f22ef01cSRoman Divacky     return ReadOnlySection;
762f22ef01cSRoman Divacky 
763f22ef01cSRoman Divacky   // If this is marked const, put it into a const section.  But if the dynamic
764f22ef01cSRoman Divacky   // linker needs to write to it, put it in the data segment.
765f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRel())
766f22ef01cSRoman Divacky     return ConstDataSection;
767f22ef01cSRoman Divacky 
768f22ef01cSRoman Divacky   // Put zero initialized globals with strong external linkage in the
769f22ef01cSRoman Divacky   // DATA, __common section with the .zerofill directive.
770f22ef01cSRoman Divacky   if (Kind.isBSSExtern())
771f22ef01cSRoman Divacky     return DataCommonSection;
772f22ef01cSRoman Divacky 
773f22ef01cSRoman Divacky   // Put zero initialized globals with local linkage in __DATA,__bss directive
774f22ef01cSRoman Divacky   // with the .zerofill directive (aka .lcomm).
775f22ef01cSRoman Divacky   if (Kind.isBSSLocal())
776f22ef01cSRoman Divacky     return DataBSSSection;
777f22ef01cSRoman Divacky 
778f22ef01cSRoman Divacky   // Otherwise, just drop the variable in the normal data section.
779f22ef01cSRoman Divacky   return DataSection;
780f22ef01cSRoman Divacky }
781f22ef01cSRoman Divacky 
782f22ef01cSRoman Divacky const MCSection *
783f22ef01cSRoman Divacky TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
784f22ef01cSRoman Divacky   // If this constant requires a relocation, we have to put it in the data
785f22ef01cSRoman Divacky   // segment, not in the text segment.
786f22ef01cSRoman Divacky   if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
787f22ef01cSRoman Divacky     return ConstDataSection;
788f22ef01cSRoman Divacky 
789f22ef01cSRoman Divacky   if (Kind.isMergeableConst4())
790f22ef01cSRoman Divacky     return FourByteConstantSection;
791f22ef01cSRoman Divacky   if (Kind.isMergeableConst8())
792f22ef01cSRoman Divacky     return EightByteConstantSection;
793f22ef01cSRoman Divacky   if (Kind.isMergeableConst16() && SixteenByteConstantSection)
794f22ef01cSRoman Divacky     return SixteenByteConstantSection;
795f22ef01cSRoman Divacky   return ReadOnlySection;  // .const
796f22ef01cSRoman Divacky }
797f22ef01cSRoman Divacky 
798f22ef01cSRoman Divacky /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
799f22ef01cSRoman Divacky /// not to emit the UsedDirective for some symbols in llvm.used.
800f22ef01cSRoman Divacky // FIXME: REMOVE this (rdar://7071300)
801f22ef01cSRoman Divacky bool TargetLoweringObjectFileMachO::
802f22ef01cSRoman Divacky shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
803f22ef01cSRoman Divacky   /// On Darwin, internally linked data beginning with "L" or "l" does not have
804f22ef01cSRoman Divacky   /// the directive emitted (this occurs in ObjC metadata).
805f22ef01cSRoman Divacky   if (!GV) return false;
806f22ef01cSRoman Divacky 
807f22ef01cSRoman Divacky   // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
808f22ef01cSRoman Divacky   if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
809f22ef01cSRoman Divacky     // FIXME: ObjC metadata is currently emitted as internal symbols that have
810f22ef01cSRoman Divacky     // \1L and \0l prefixes on them.  Fix them to be Private/LinkerPrivate and
811f22ef01cSRoman Divacky     // this horrible hack can go away.
812f22ef01cSRoman Divacky     MCSymbol *Sym = Mang->getSymbol(GV);
813f22ef01cSRoman Divacky     if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
814f22ef01cSRoman Divacky       return false;
815f22ef01cSRoman Divacky   }
816f22ef01cSRoman Divacky 
817f22ef01cSRoman Divacky   return true;
818f22ef01cSRoman Divacky }
819f22ef01cSRoman Divacky 
820f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFileMachO::
821f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
822f22ef01cSRoman Divacky                                MachineModuleInfo *MMI, unsigned Encoding,
823f22ef01cSRoman Divacky                                MCStreamer &Streamer) const {
824f22ef01cSRoman Divacky   // The mach-o version of this method defaults to returning a stub reference.
825f22ef01cSRoman Divacky 
826f22ef01cSRoman Divacky   if (Encoding & DW_EH_PE_indirect) {
827f22ef01cSRoman Divacky     MachineModuleInfoMachO &MachOMMI =
828f22ef01cSRoman Divacky       MMI->getObjFileInfo<MachineModuleInfoMachO>();
829f22ef01cSRoman Divacky 
830f22ef01cSRoman Divacky     SmallString<128> Name;
831f22ef01cSRoman Divacky     Mang->getNameWithPrefix(Name, GV, true);
832f22ef01cSRoman Divacky     Name += "$non_lazy_ptr";
833f22ef01cSRoman Divacky 
834f22ef01cSRoman Divacky     // Add information about the stub reference to MachOMMI so that the stub
835f22ef01cSRoman Divacky     // gets emitted by the asmprinter.
836f22ef01cSRoman Divacky     MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
837f22ef01cSRoman Divacky     MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
838f22ef01cSRoman Divacky     if (StubSym.getPointer() == 0) {
839f22ef01cSRoman Divacky       MCSymbol *Sym = Mang->getSymbol(GV);
840f22ef01cSRoman Divacky       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
841f22ef01cSRoman Divacky     }
842f22ef01cSRoman Divacky 
843f22ef01cSRoman Divacky     return TargetLoweringObjectFile::
8443b0f4066SDimitry Andric       getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
845f22ef01cSRoman Divacky   }
846f22ef01cSRoman Divacky 
847f22ef01cSRoman Divacky   return TargetLoweringObjectFile::
848f22ef01cSRoman Divacky     getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
849f22ef01cSRoman Divacky }
850f22ef01cSRoman Divacky 
8513b0f4066SDimitry Andric MCSymbol *TargetLoweringObjectFileMachO::
8523b0f4066SDimitry Andric getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
8533b0f4066SDimitry Andric                         MachineModuleInfo *MMI) const {
8543b0f4066SDimitry Andric   // The mach-o version of this method defaults to returning a stub reference.
8553b0f4066SDimitry Andric   MachineModuleInfoMachO &MachOMMI =
8563b0f4066SDimitry Andric     MMI->getObjFileInfo<MachineModuleInfoMachO>();
8573b0f4066SDimitry Andric 
8583b0f4066SDimitry Andric   SmallString<128> Name;
8593b0f4066SDimitry Andric   Mang->getNameWithPrefix(Name, GV, true);
8603b0f4066SDimitry Andric   Name += "$non_lazy_ptr";
8613b0f4066SDimitry Andric 
8623b0f4066SDimitry Andric   // Add information about the stub reference to MachOMMI so that the stub
8633b0f4066SDimitry Andric   // gets emitted by the asmprinter.
8643b0f4066SDimitry Andric   MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
8653b0f4066SDimitry Andric   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
8663b0f4066SDimitry Andric   if (StubSym.getPointer() == 0) {
8673b0f4066SDimitry Andric     MCSymbol *Sym = Mang->getSymbol(GV);
8683b0f4066SDimitry Andric     StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
8693b0f4066SDimitry Andric   }
8703b0f4066SDimitry Andric 
8713b0f4066SDimitry Andric   return SSym;
8723b0f4066SDimitry Andric }
8733b0f4066SDimitry Andric 
874f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
875f22ef01cSRoman Divacky   return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
876f22ef01cSRoman Divacky }
877f22ef01cSRoman Divacky 
878f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
879f22ef01cSRoman Divacky   return DW_EH_PE_pcrel;
880f22ef01cSRoman Divacky }
881f22ef01cSRoman Divacky 
8823b0f4066SDimitry Andric unsigned TargetLoweringObjectFileMachO::getFDEEncoding(bool CFI) const {
883f22ef01cSRoman Divacky   return DW_EH_PE_pcrel;
884f22ef01cSRoman Divacky }
885f22ef01cSRoman Divacky 
886f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
887f22ef01cSRoman Divacky   return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
888f22ef01cSRoman Divacky }
889f22ef01cSRoman Divacky 
890f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
891f22ef01cSRoman Divacky //                                  COFF
892f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
893f22ef01cSRoman Divacky 
894f22ef01cSRoman Divacky void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
895f22ef01cSRoman Divacky                                               const TargetMachine &TM) {
896f22ef01cSRoman Divacky   TargetLoweringObjectFile::Initialize(Ctx, TM);
897f22ef01cSRoman Divacky   TextSection =
898f22ef01cSRoman Divacky     getContext().getCOFFSection(".text",
899ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_CODE |
900ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_EXECUTE |
901ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
902f22ef01cSRoman Divacky                                 SectionKind::getText());
903f22ef01cSRoman Divacky   DataSection =
904f22ef01cSRoman Divacky     getContext().getCOFFSection(".data",
905ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
906ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
907ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
908f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
909f22ef01cSRoman Divacky   ReadOnlySection =
910f22ef01cSRoman Divacky     getContext().getCOFFSection(".rdata",
911ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
912ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
913f22ef01cSRoman Divacky                                 SectionKind::getReadOnly());
914f22ef01cSRoman Divacky   StaticCtorSection =
915f22ef01cSRoman Divacky     getContext().getCOFFSection(".ctors",
916ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
917ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
918ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
919f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
920f22ef01cSRoman Divacky   StaticDtorSection =
921f22ef01cSRoman Divacky     getContext().getCOFFSection(".dtors",
922ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
923ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
924ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
925f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
926f22ef01cSRoman Divacky 
927f22ef01cSRoman Divacky   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
928f22ef01cSRoman Divacky   // though it contains relocatable pointers.  In PIC mode, this is probably a
929f22ef01cSRoman Divacky   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
930f22ef01cSRoman Divacky   // adjusted or this should be a data section.
931f22ef01cSRoman Divacky   LSDASection =
932f22ef01cSRoman Divacky     getContext().getCOFFSection(".gcc_except_table",
933ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
934ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
935f22ef01cSRoman Divacky                                 SectionKind::getReadOnly());
936f22ef01cSRoman Divacky   // Debug info.
937f22ef01cSRoman Divacky   DwarfAbbrevSection =
938f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_abbrev",
939ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
940ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
941f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
942f22ef01cSRoman Divacky   DwarfInfoSection =
943f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_info",
944ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
945ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
946f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
947f22ef01cSRoman Divacky   DwarfLineSection =
948f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_line",
949ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
950ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
951f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
952f22ef01cSRoman Divacky   DwarfFrameSection =
953f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_frame",
954ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
955ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
956f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
957f22ef01cSRoman Divacky   DwarfPubNamesSection =
958f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_pubnames",
959ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
960ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
961f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
962f22ef01cSRoman Divacky   DwarfPubTypesSection =
963f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_pubtypes",
964ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
965ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
966f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
967f22ef01cSRoman Divacky   DwarfStrSection =
968f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_str",
969ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
970ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
971f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
972f22ef01cSRoman Divacky   DwarfLocSection =
973f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_loc",
974ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
975ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
976f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
977f22ef01cSRoman Divacky   DwarfARangesSection =
978f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_aranges",
979ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
980ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
981f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
982f22ef01cSRoman Divacky   DwarfRangesSection =
983f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_ranges",
984ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
985ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
986f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
987f22ef01cSRoman Divacky   DwarfMacroInfoSection =
988f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_macinfo",
989ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
990ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
991f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
992f22ef01cSRoman Divacky 
993f22ef01cSRoman Divacky   DrectveSection =
994f22ef01cSRoman Divacky     getContext().getCOFFSection(".drectve",
995ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_LNK_INFO,
996f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
997f22ef01cSRoman Divacky }
998f22ef01cSRoman Divacky 
9992754fe60SDimitry Andric const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
10002754fe60SDimitry Andric   return getContext().getCOFFSection(".eh_frame",
10012754fe60SDimitry Andric                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
10022754fe60SDimitry Andric                                      COFF::IMAGE_SCN_MEM_READ |
10032754fe60SDimitry Andric                                      COFF::IMAGE_SCN_MEM_WRITE,
10042754fe60SDimitry Andric                                      SectionKind::getDataRel());
10052754fe60SDimitry Andric }
10062754fe60SDimitry Andric 
10072754fe60SDimitry Andric 
1008f22ef01cSRoman Divacky static unsigned
1009f22ef01cSRoman Divacky getCOFFSectionFlags(SectionKind K) {
1010f22ef01cSRoman Divacky   unsigned Flags = 0;
1011f22ef01cSRoman Divacky 
1012ffd1746dSEd Schouten   if (K.isMetadata())
1013f22ef01cSRoman Divacky     Flags |=
1014ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_DISCARDABLE;
1015f22ef01cSRoman Divacky   else if (K.isText())
1016f22ef01cSRoman Divacky     Flags |=
1017ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_EXECUTE |
10182754fe60SDimitry Andric       COFF::IMAGE_SCN_MEM_READ |
1019ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_CODE;
1020f22ef01cSRoman Divacky   else if (K.isBSS ())
1021f22ef01cSRoman Divacky     Flags |=
1022ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1023ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ |
1024ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_WRITE;
1025f22ef01cSRoman Divacky   else if (K.isReadOnly())
1026f22ef01cSRoman Divacky     Flags |=
1027ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1028ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ;
1029f22ef01cSRoman Divacky   else if (K.isWriteable())
1030f22ef01cSRoman Divacky     Flags |=
1031ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1032ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ |
1033ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_WRITE;
1034f22ef01cSRoman Divacky 
1035f22ef01cSRoman Divacky   return Flags;
1036f22ef01cSRoman Divacky }
1037f22ef01cSRoman Divacky 
1038f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileCOFF::
1039f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1040f22ef01cSRoman Divacky                          Mangler *Mang, const TargetMachine &TM) const {
1041f22ef01cSRoman Divacky   return getContext().getCOFFSection(GV->getSection(),
1042f22ef01cSRoman Divacky                                      getCOFFSectionFlags(Kind),
1043f22ef01cSRoman Divacky                                      Kind);
1044f22ef01cSRoman Divacky }
1045f22ef01cSRoman Divacky 
1046f22ef01cSRoman Divacky static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1047f22ef01cSRoman Divacky   if (Kind.isText())
10482754fe60SDimitry Andric     return ".text$";
1049f22ef01cSRoman Divacky   if (Kind.isBSS ())
10502754fe60SDimitry Andric     return ".bss$";
1051f22ef01cSRoman Divacky   if (Kind.isWriteable())
10522754fe60SDimitry Andric     return ".data$";
10532754fe60SDimitry Andric   return ".rdata$";
1054f22ef01cSRoman Divacky }
1055f22ef01cSRoman Divacky 
1056f22ef01cSRoman Divacky 
1057f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileCOFF::
1058f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1059f22ef01cSRoman Divacky                        Mangler *Mang, const TargetMachine &TM) const {
1060f22ef01cSRoman Divacky   assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1061f22ef01cSRoman Divacky 
1062f22ef01cSRoman Divacky   // If this global is linkonce/weak and the target handles this by emitting it
1063f22ef01cSRoman Divacky   // into a 'uniqued' section name, create and return the section now.
1064f22ef01cSRoman Divacky   if (GV->isWeakForLinker()) {
1065f22ef01cSRoman Divacky     const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1066f22ef01cSRoman Divacky     SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
1067f22ef01cSRoman Divacky     MCSymbol *Sym = Mang->getSymbol(GV);
10682754fe60SDimitry Andric     Name.append(Sym->getName().begin() + 1, Sym->getName().end());
1069f22ef01cSRoman Divacky 
1070f22ef01cSRoman Divacky     unsigned Characteristics = getCOFFSectionFlags(Kind);
1071f22ef01cSRoman Divacky 
1072ffd1746dSEd Schouten     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1073f22ef01cSRoman Divacky 
1074f22ef01cSRoman Divacky     return getContext().getCOFFSection(Name.str(), Characteristics,
10752754fe60SDimitry Andric                           COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
1076f22ef01cSRoman Divacky   }
1077f22ef01cSRoman Divacky 
1078f22ef01cSRoman Divacky   if (Kind.isText())
1079f22ef01cSRoman Divacky     return getTextSection();
1080f22ef01cSRoman Divacky 
1081f22ef01cSRoman Divacky   return getDataSection();
1082f22ef01cSRoman Divacky }
1083f22ef01cSRoman Divacky 
1084