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,
522f22ef01cSRoman Divacky                                    SectionKind::getText());
523f22ef01cSRoman Divacky   ConstDataCoalSection
524f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__const_coal",
525f22ef01cSRoman Divacky                                    MCSectionMachO::S_COALESCED,
526f22ef01cSRoman Divacky                                    SectionKind::getText());
527f22ef01cSRoman Divacky   ConstDataSection  // .const_data
528f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__const", 0,
529f22ef01cSRoman Divacky                                    SectionKind::getReadOnlyWithRel());
530f22ef01cSRoman Divacky   DataCoalSection
531f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__datacoal_nt",
532f22ef01cSRoman Divacky                                    MCSectionMachO::S_COALESCED,
533f22ef01cSRoman Divacky                                    SectionKind::getDataRel());
534f22ef01cSRoman Divacky   DataCommonSection
535f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__common",
536f22ef01cSRoman Divacky                                    MCSectionMachO::S_ZEROFILL,
537f22ef01cSRoman Divacky                                    SectionKind::getBSS());
538f22ef01cSRoman Divacky   DataBSSSection
539f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
540f22ef01cSRoman Divacky                                    SectionKind::getBSS());
541f22ef01cSRoman Divacky 
542f22ef01cSRoman Divacky 
543f22ef01cSRoman Divacky   LazySymbolPointerSection
544f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
545f22ef01cSRoman Divacky                                    MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
546f22ef01cSRoman Divacky                                    SectionKind::getMetadata());
547f22ef01cSRoman Divacky   NonLazySymbolPointerSection
548f22ef01cSRoman Divacky     = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
549f22ef01cSRoman Divacky                                    MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
550f22ef01cSRoman Divacky                                    SectionKind::getMetadata());
551f22ef01cSRoman Divacky 
552f22ef01cSRoman Divacky   if (TM.getRelocationModel() == Reloc::Static) {
553f22ef01cSRoman Divacky     StaticCtorSection
554f22ef01cSRoman Divacky       = getContext().getMachOSection("__TEXT", "__constructor", 0,
555f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
556f22ef01cSRoman Divacky     StaticDtorSection
557f22ef01cSRoman Divacky       = getContext().getMachOSection("__TEXT", "__destructor", 0,
558f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
559f22ef01cSRoman Divacky   } else {
560f22ef01cSRoman Divacky     StaticCtorSection
561f22ef01cSRoman Divacky       = getContext().getMachOSection("__DATA", "__mod_init_func",
562f22ef01cSRoman Divacky                                      MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
563f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
564f22ef01cSRoman Divacky     StaticDtorSection
565f22ef01cSRoman Divacky       = getContext().getMachOSection("__DATA", "__mod_term_func",
566f22ef01cSRoman Divacky                                      MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
567f22ef01cSRoman Divacky                                      SectionKind::getDataRel());
568f22ef01cSRoman Divacky   }
569f22ef01cSRoman Divacky 
570f22ef01cSRoman Divacky   // Exception Handling.
571f22ef01cSRoman Divacky   LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
572f22ef01cSRoman Divacky                                              SectionKind::getReadOnlyWithRel());
573f22ef01cSRoman Divacky   EHFrameSection =
574f22ef01cSRoman Divacky     getContext().getMachOSection("__TEXT", "__eh_frame",
575f22ef01cSRoman Divacky                                  MCSectionMachO::S_COALESCED |
576f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_NO_TOC |
577f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
578f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_LIVE_SUPPORT,
579f22ef01cSRoman Divacky                                  SectionKind::getReadOnly());
580f22ef01cSRoman Divacky 
581f22ef01cSRoman Divacky   // Debug Information.
582f22ef01cSRoman Divacky   DwarfAbbrevSection =
583f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_abbrev",
584f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
585f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
586f22ef01cSRoman Divacky   DwarfInfoSection =
587f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_info",
588f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
589f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
590f22ef01cSRoman Divacky   DwarfLineSection =
591f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_line",
592f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
593f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
594f22ef01cSRoman Divacky   DwarfFrameSection =
595f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_frame",
596f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
597f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
598f22ef01cSRoman Divacky   DwarfPubNamesSection =
599f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_pubnames",
600f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
601f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
602f22ef01cSRoman Divacky   DwarfPubTypesSection =
603f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_pubtypes",
604f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
605f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
606f22ef01cSRoman Divacky   DwarfStrSection =
607f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_str",
608f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
609f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
610f22ef01cSRoman Divacky   DwarfLocSection =
611f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_loc",
612f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
613f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
614f22ef01cSRoman Divacky   DwarfARangesSection =
615f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_aranges",
616f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
617f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
618f22ef01cSRoman Divacky   DwarfRangesSection =
619f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_ranges",
620f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
621f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
622f22ef01cSRoman Divacky   DwarfMacroInfoSection =
623f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_macinfo",
624f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
625f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
626f22ef01cSRoman Divacky   DwarfDebugInlineSection =
627f22ef01cSRoman Divacky     getContext().getMachOSection("__DWARF", "__debug_inlined",
628f22ef01cSRoman Divacky                                  MCSectionMachO::S_ATTR_DEBUG,
629f22ef01cSRoman Divacky                                  SectionKind::getMetadata());
630f22ef01cSRoman Divacky 
631f22ef01cSRoman Divacky   TLSExtraDataSection = TLSTLVSection;
632f22ef01cSRoman Divacky }
633f22ef01cSRoman Divacky 
634f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileMachO::
635f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
636f22ef01cSRoman Divacky                          Mangler *Mang, const TargetMachine &TM) const {
637f22ef01cSRoman Divacky   // Parse the section specifier and create it if valid.
638f22ef01cSRoman Divacky   StringRef Segment, Section;
639f22ef01cSRoman Divacky   unsigned TAA, StubSize;
640f22ef01cSRoman Divacky   std::string ErrorCode =
641f22ef01cSRoman Divacky     MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
642f22ef01cSRoman Divacky                                           TAA, StubSize);
643f22ef01cSRoman Divacky   if (!ErrorCode.empty()) {
644f22ef01cSRoman Divacky     // If invalid, report the error with report_fatal_error.
645f22ef01cSRoman Divacky     report_fatal_error("Global variable '" + GV->getNameStr() +
646f22ef01cSRoman Divacky                       "' has an invalid section specifier '" + GV->getSection()+
647f22ef01cSRoman Divacky                       "': " + ErrorCode + ".");
648f22ef01cSRoman Divacky     // Fall back to dropping it into the data section.
649f22ef01cSRoman Divacky     return DataSection;
650f22ef01cSRoman Divacky   }
651f22ef01cSRoman Divacky 
652f22ef01cSRoman Divacky   // Get the section.
653f22ef01cSRoman Divacky   const MCSectionMachO *S =
654f22ef01cSRoman Divacky     getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
655f22ef01cSRoman Divacky 
656f22ef01cSRoman Divacky   // Okay, now that we got the section, verify that the TAA & StubSize agree.
657f22ef01cSRoman Divacky   // If the user declared multiple globals with different section flags, we need
658f22ef01cSRoman Divacky   // to reject it here.
659f22ef01cSRoman Divacky   if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
660f22ef01cSRoman Divacky     // If invalid, report the error with report_fatal_error.
661f22ef01cSRoman Divacky     report_fatal_error("Global variable '" + GV->getNameStr() +
662f22ef01cSRoman Divacky                       "' section type or attributes does not match previous"
663f22ef01cSRoman Divacky                       " section specifier");
664f22ef01cSRoman Divacky   }
665f22ef01cSRoman Divacky 
666f22ef01cSRoman Divacky   return S;
667f22ef01cSRoman Divacky }
668f22ef01cSRoman Divacky 
669f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileMachO::
670f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
671f22ef01cSRoman Divacky                        Mangler *Mang, const TargetMachine &TM) const {
672f22ef01cSRoman Divacky 
673f22ef01cSRoman Divacky   // Handle thread local data.
674f22ef01cSRoman Divacky   if (Kind.isThreadBSS()) return TLSBSSSection;
675f22ef01cSRoman Divacky   if (Kind.isThreadData()) return TLSDataSection;
676f22ef01cSRoman Divacky 
677f22ef01cSRoman Divacky   if (Kind.isText())
678f22ef01cSRoman Divacky     return GV->isWeakForLinker() ? TextCoalSection : TextSection;
679f22ef01cSRoman Divacky 
680f22ef01cSRoman Divacky   // If this is weak/linkonce, put this in a coalescable section, either in text
681f22ef01cSRoman Divacky   // or data depending on if it is writable.
682f22ef01cSRoman Divacky   if (GV->isWeakForLinker()) {
683f22ef01cSRoman Divacky     if (Kind.isReadOnly())
684f22ef01cSRoman Divacky       return ConstTextCoalSection;
685f22ef01cSRoman Divacky     return DataCoalSection;
686f22ef01cSRoman Divacky   }
687f22ef01cSRoman Divacky 
688f22ef01cSRoman Divacky   // FIXME: Alignment check should be handled by section classifier.
689f22ef01cSRoman Divacky   if (Kind.isMergeable1ByteCString() &&
690f22ef01cSRoman Divacky       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
691f22ef01cSRoman Divacky     return CStringSection;
692f22ef01cSRoman Divacky 
693f22ef01cSRoman Divacky   // Do not put 16-bit arrays in the UString section if they have an
694f22ef01cSRoman Divacky   // externally visible label, this runs into issues with certain linker
695f22ef01cSRoman Divacky   // versions.
696f22ef01cSRoman Divacky   if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
697f22ef01cSRoman Divacky       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
698f22ef01cSRoman Divacky     return UStringSection;
699f22ef01cSRoman Divacky 
700f22ef01cSRoman Divacky   if (Kind.isMergeableConst()) {
701f22ef01cSRoman Divacky     if (Kind.isMergeableConst4())
702f22ef01cSRoman Divacky       return FourByteConstantSection;
703f22ef01cSRoman Divacky     if (Kind.isMergeableConst8())
704f22ef01cSRoman Divacky       return EightByteConstantSection;
705f22ef01cSRoman Divacky     if (Kind.isMergeableConst16() && SixteenByteConstantSection)
706f22ef01cSRoman Divacky       return SixteenByteConstantSection;
707f22ef01cSRoman Divacky   }
708f22ef01cSRoman Divacky 
709f22ef01cSRoman Divacky   // Otherwise, if it is readonly, but not something we can specially optimize,
710f22ef01cSRoman Divacky   // just drop it in .const.
711f22ef01cSRoman Divacky   if (Kind.isReadOnly())
712f22ef01cSRoman Divacky     return ReadOnlySection;
713f22ef01cSRoman Divacky 
714f22ef01cSRoman Divacky   // If this is marked const, put it into a const section.  But if the dynamic
715f22ef01cSRoman Divacky   // linker needs to write to it, put it in the data segment.
716f22ef01cSRoman Divacky   if (Kind.isReadOnlyWithRel())
717f22ef01cSRoman Divacky     return ConstDataSection;
718f22ef01cSRoman Divacky 
719f22ef01cSRoman Divacky   // Put zero initialized globals with strong external linkage in the
720f22ef01cSRoman Divacky   // DATA, __common section with the .zerofill directive.
721f22ef01cSRoman Divacky   if (Kind.isBSSExtern())
722f22ef01cSRoman Divacky     return DataCommonSection;
723f22ef01cSRoman Divacky 
724f22ef01cSRoman Divacky   // Put zero initialized globals with local linkage in __DATA,__bss directive
725f22ef01cSRoman Divacky   // with the .zerofill directive (aka .lcomm).
726f22ef01cSRoman Divacky   if (Kind.isBSSLocal())
727f22ef01cSRoman Divacky     return DataBSSSection;
728f22ef01cSRoman Divacky 
729f22ef01cSRoman Divacky   // Otherwise, just drop the variable in the normal data section.
730f22ef01cSRoman Divacky   return DataSection;
731f22ef01cSRoman Divacky }
732f22ef01cSRoman Divacky 
733f22ef01cSRoman Divacky const MCSection *
734f22ef01cSRoman Divacky TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
735f22ef01cSRoman Divacky   // If this constant requires a relocation, we have to put it in the data
736f22ef01cSRoman Divacky   // segment, not in the text segment.
737f22ef01cSRoman Divacky   if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
738f22ef01cSRoman Divacky     return ConstDataSection;
739f22ef01cSRoman Divacky 
740f22ef01cSRoman Divacky   if (Kind.isMergeableConst4())
741f22ef01cSRoman Divacky     return FourByteConstantSection;
742f22ef01cSRoman Divacky   if (Kind.isMergeableConst8())
743f22ef01cSRoman Divacky     return EightByteConstantSection;
744f22ef01cSRoman Divacky   if (Kind.isMergeableConst16() && SixteenByteConstantSection)
745f22ef01cSRoman Divacky     return SixteenByteConstantSection;
746f22ef01cSRoman Divacky   return ReadOnlySection;  // .const
747f22ef01cSRoman Divacky }
748f22ef01cSRoman Divacky 
749f22ef01cSRoman Divacky /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
750f22ef01cSRoman Divacky /// not to emit the UsedDirective for some symbols in llvm.used.
751f22ef01cSRoman Divacky // FIXME: REMOVE this (rdar://7071300)
752f22ef01cSRoman Divacky bool TargetLoweringObjectFileMachO::
753f22ef01cSRoman Divacky shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
754f22ef01cSRoman Divacky   /// On Darwin, internally linked data beginning with "L" or "l" does not have
755f22ef01cSRoman Divacky   /// the directive emitted (this occurs in ObjC metadata).
756f22ef01cSRoman Divacky   if (!GV) return false;
757f22ef01cSRoman Divacky 
758f22ef01cSRoman Divacky   // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
759f22ef01cSRoman Divacky   if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
760f22ef01cSRoman Divacky     // FIXME: ObjC metadata is currently emitted as internal symbols that have
761f22ef01cSRoman Divacky     // \1L and \0l prefixes on them.  Fix them to be Private/LinkerPrivate and
762f22ef01cSRoman Divacky     // this horrible hack can go away.
763f22ef01cSRoman Divacky     MCSymbol *Sym = Mang->getSymbol(GV);
764f22ef01cSRoman Divacky     if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
765f22ef01cSRoman Divacky       return false;
766f22ef01cSRoman Divacky   }
767f22ef01cSRoman Divacky 
768f22ef01cSRoman Divacky   return true;
769f22ef01cSRoman Divacky }
770f22ef01cSRoman Divacky 
771f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFileMachO::
772f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
773f22ef01cSRoman Divacky                                MachineModuleInfo *MMI, unsigned Encoding,
774f22ef01cSRoman Divacky                                MCStreamer &Streamer) const {
775f22ef01cSRoman Divacky   // The mach-o version of this method defaults to returning a stub reference.
776f22ef01cSRoman Divacky 
777f22ef01cSRoman Divacky   if (Encoding & DW_EH_PE_indirect) {
778f22ef01cSRoman Divacky     MachineModuleInfoMachO &MachOMMI =
779f22ef01cSRoman Divacky       MMI->getObjFileInfo<MachineModuleInfoMachO>();
780f22ef01cSRoman Divacky 
781f22ef01cSRoman Divacky     SmallString<128> Name;
782f22ef01cSRoman Divacky     Mang->getNameWithPrefix(Name, GV, true);
783f22ef01cSRoman Divacky     Name += "$non_lazy_ptr";
784f22ef01cSRoman Divacky 
785f22ef01cSRoman Divacky     // Add information about the stub reference to MachOMMI so that the stub
786f22ef01cSRoman Divacky     // gets emitted by the asmprinter.
787f22ef01cSRoman Divacky     MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
788f22ef01cSRoman Divacky     MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
789f22ef01cSRoman Divacky     if (StubSym.getPointer() == 0) {
790f22ef01cSRoman Divacky       MCSymbol *Sym = Mang->getSymbol(GV);
791f22ef01cSRoman Divacky       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
792f22ef01cSRoman Divacky     }
793f22ef01cSRoman Divacky 
794f22ef01cSRoman Divacky     return TargetLoweringObjectFile::
795f22ef01cSRoman Divacky       getExprForDwarfReference(SSym, Mang, MMI,
796f22ef01cSRoman Divacky                                Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
797f22ef01cSRoman Divacky   }
798f22ef01cSRoman Divacky 
799f22ef01cSRoman Divacky   return TargetLoweringObjectFile::
800f22ef01cSRoman Divacky     getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
801f22ef01cSRoman Divacky }
802f22ef01cSRoman Divacky 
803f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
804f22ef01cSRoman Divacky   return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
805f22ef01cSRoman Divacky }
806f22ef01cSRoman Divacky 
807f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
808f22ef01cSRoman Divacky   return DW_EH_PE_pcrel;
809f22ef01cSRoman Divacky }
810f22ef01cSRoman Divacky 
811f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
812f22ef01cSRoman Divacky   return DW_EH_PE_pcrel;
813f22ef01cSRoman Divacky }
814f22ef01cSRoman Divacky 
815f22ef01cSRoman Divacky unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
816f22ef01cSRoman Divacky   return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
817f22ef01cSRoman Divacky }
818f22ef01cSRoman Divacky 
819f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
820f22ef01cSRoman Divacky //                                  COFF
821f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
822f22ef01cSRoman Divacky 
823f22ef01cSRoman Divacky void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
824f22ef01cSRoman Divacky                                               const TargetMachine &TM) {
825f22ef01cSRoman Divacky   TargetLoweringObjectFile::Initialize(Ctx, TM);
826f22ef01cSRoman Divacky   TextSection =
827f22ef01cSRoman Divacky     getContext().getCOFFSection(".text",
828ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_CODE |
829ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_EXECUTE |
830ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
831f22ef01cSRoman Divacky                                 SectionKind::getText());
832f22ef01cSRoman Divacky   DataSection =
833f22ef01cSRoman Divacky     getContext().getCOFFSection(".data",
834ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
835ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
836ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
837f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
838f22ef01cSRoman Divacky   ReadOnlySection =
839f22ef01cSRoman Divacky     getContext().getCOFFSection(".rdata",
840ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
841ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
842f22ef01cSRoman Divacky                                 SectionKind::getReadOnly());
843f22ef01cSRoman Divacky   StaticCtorSection =
844f22ef01cSRoman Divacky     getContext().getCOFFSection(".ctors",
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   StaticDtorSection =
850f22ef01cSRoman Divacky     getContext().getCOFFSection(".dtors",
851ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
852ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
853ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
854f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
855f22ef01cSRoman Divacky 
856f22ef01cSRoman Divacky   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
857f22ef01cSRoman Divacky   // though it contains relocatable pointers.  In PIC mode, this is probably a
858f22ef01cSRoman Divacky   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
859f22ef01cSRoman Divacky   // adjusted or this should be a data section.
860f22ef01cSRoman Divacky   LSDASection =
861f22ef01cSRoman Divacky     getContext().getCOFFSection(".gcc_except_table",
862ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
863ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
864f22ef01cSRoman Divacky                                 SectionKind::getReadOnly());
865f22ef01cSRoman Divacky   EHFrameSection =
866f22ef01cSRoman Divacky     getContext().getCOFFSection(".eh_frame",
867ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
868ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ |
869ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_WRITE,
870f22ef01cSRoman Divacky                                 SectionKind::getDataRel());
871f22ef01cSRoman Divacky 
872f22ef01cSRoman Divacky   // Debug info.
873f22ef01cSRoman Divacky   DwarfAbbrevSection =
874f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_abbrev",
875ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
876ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
877f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
878f22ef01cSRoman Divacky   DwarfInfoSection =
879f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_info",
880ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
881ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
882f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
883f22ef01cSRoman Divacky   DwarfLineSection =
884f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_line",
885ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
886ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
887f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
888f22ef01cSRoman Divacky   DwarfFrameSection =
889f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_frame",
890ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
891ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
892f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
893f22ef01cSRoman Divacky   DwarfPubNamesSection =
894f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_pubnames",
895ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
896ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
897f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
898f22ef01cSRoman Divacky   DwarfPubTypesSection =
899f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_pubtypes",
900ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
901ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
902f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
903f22ef01cSRoman Divacky   DwarfStrSection =
904f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_str",
905ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
906ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
907f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
908f22ef01cSRoman Divacky   DwarfLocSection =
909f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_loc",
910ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
911ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
912f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
913f22ef01cSRoman Divacky   DwarfARangesSection =
914f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_aranges",
915ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
916ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
917f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
918f22ef01cSRoman Divacky   DwarfRangesSection =
919f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_ranges",
920ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
921ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
922f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
923f22ef01cSRoman Divacky   DwarfMacroInfoSection =
924f22ef01cSRoman Divacky     getContext().getCOFFSection(".debug_macinfo",
925ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_DISCARDABLE |
926ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_MEM_READ,
927f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
928f22ef01cSRoman Divacky 
929f22ef01cSRoman Divacky   DrectveSection =
930f22ef01cSRoman Divacky     getContext().getCOFFSection(".drectve",
931ffd1746dSEd Schouten                                 COFF::IMAGE_SCN_LNK_INFO,
932f22ef01cSRoman Divacky                                 SectionKind::getMetadata());
933f22ef01cSRoman Divacky }
934f22ef01cSRoman Divacky 
935f22ef01cSRoman Divacky static unsigned
936f22ef01cSRoman Divacky getCOFFSectionFlags(SectionKind K) {
937f22ef01cSRoman Divacky   unsigned Flags = 0;
938f22ef01cSRoman Divacky 
939ffd1746dSEd Schouten   if (K.isMetadata())
940f22ef01cSRoman Divacky     Flags |=
941ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_DISCARDABLE;
942f22ef01cSRoman Divacky   else if (K.isText())
943f22ef01cSRoman Divacky     Flags |=
944ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_EXECUTE |
945ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_CODE;
946f22ef01cSRoman Divacky   else if (K.isBSS ())
947f22ef01cSRoman Divacky     Flags |=
948ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
949ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ |
950ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_WRITE;
951f22ef01cSRoman Divacky   else if (K.isReadOnly())
952f22ef01cSRoman Divacky     Flags |=
953ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
954ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ;
955f22ef01cSRoman Divacky   else if (K.isWriteable())
956f22ef01cSRoman Divacky     Flags |=
957ffd1746dSEd Schouten       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
958ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_READ |
959ffd1746dSEd Schouten       COFF::IMAGE_SCN_MEM_WRITE;
960f22ef01cSRoman Divacky 
961f22ef01cSRoman Divacky   return Flags;
962f22ef01cSRoman Divacky }
963f22ef01cSRoman Divacky 
964f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileCOFF::
965f22ef01cSRoman Divacky getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
966f22ef01cSRoman Divacky                          Mangler *Mang, const TargetMachine &TM) const {
967f22ef01cSRoman Divacky   return getContext().getCOFFSection(GV->getSection(),
968f22ef01cSRoman Divacky                                      getCOFFSectionFlags(Kind),
969f22ef01cSRoman Divacky                                      Kind);
970f22ef01cSRoman Divacky }
971f22ef01cSRoman Divacky 
972f22ef01cSRoman Divacky static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
973f22ef01cSRoman Divacky   if (Kind.isText())
974f22ef01cSRoman Divacky     return ".text$linkonce";
975f22ef01cSRoman Divacky   if (Kind.isBSS ())
976f22ef01cSRoman Divacky     return ".bss$linkonce";
977f22ef01cSRoman Divacky   if (Kind.isWriteable())
978f22ef01cSRoman Divacky     return ".data$linkonce";
979f22ef01cSRoman Divacky   return ".rdata$linkonce";
980f22ef01cSRoman Divacky }
981f22ef01cSRoman Divacky 
982f22ef01cSRoman Divacky 
983f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFileCOFF::
984f22ef01cSRoman Divacky SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
985f22ef01cSRoman Divacky                        Mangler *Mang, const TargetMachine &TM) const {
986f22ef01cSRoman Divacky   assert(!Kind.isThreadLocal() && "Doesn't support TLS");
987f22ef01cSRoman Divacky 
988f22ef01cSRoman Divacky   // If this global is linkonce/weak and the target handles this by emitting it
989f22ef01cSRoman Divacky   // into a 'uniqued' section name, create and return the section now.
990f22ef01cSRoman Divacky   if (GV->isWeakForLinker()) {
991f22ef01cSRoman Divacky     const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
992f22ef01cSRoman Divacky     SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
993f22ef01cSRoman Divacky     MCSymbol *Sym = Mang->getSymbol(GV);
994f22ef01cSRoman Divacky     Name.append(Sym->getName().begin(), Sym->getName().end());
995f22ef01cSRoman Divacky 
996f22ef01cSRoman Divacky     unsigned Characteristics = getCOFFSectionFlags(Kind);
997f22ef01cSRoman Divacky 
998ffd1746dSEd Schouten     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
999f22ef01cSRoman Divacky 
1000f22ef01cSRoman Divacky     return getContext().getCOFFSection(Name.str(), Characteristics,
1001ffd1746dSEd Schouten                           COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH, Kind);
1002f22ef01cSRoman Divacky   }
1003f22ef01cSRoman Divacky 
1004f22ef01cSRoman Divacky   if (Kind.isText())
1005f22ef01cSRoman Divacky     return getTextSection();
1006f22ef01cSRoman Divacky 
1007f22ef01cSRoman Divacky   return getDataSection();
1008f22ef01cSRoman Divacky }
1009f22ef01cSRoman Divacky 
1010