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