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