blob: dc08cadaaa92ce222bd177b2f32ccaa8db04b672 [file] [log] [blame]
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001//===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements classes used to handle lowerings specific to common
11// object file formats.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/GlobalVariable.h"
20#include "llvm/CodeGen/MachineModuleInfoImpls.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCSectionMachO.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/Target/Mangler.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetOptions.h"
30#include "llvm/Support/Dwarf.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/ADT/SmallString.h"
34#include "llvm/ADT/StringExtras.h"
35using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000036using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000037
38//===----------------------------------------------------------------------===//
39// ELF
40//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000041
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000042void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
43 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000044 TargetLoweringObjectFile::Initialize(Ctx, TM);
45
46 BSSSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000047 getContext().getELFSection(".bss", MCSectionELF::SHT_NOBITS,
48 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
49 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000050
51 TextSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000052 getContext().getELFSection(".text", MCSectionELF::SHT_PROGBITS,
53 MCSectionELF::SHF_EXECINSTR |
54 MCSectionELF::SHF_ALLOC,
55 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000056
57 DataSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000058 getContext().getELFSection(".data", MCSectionELF::SHT_PROGBITS,
59 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
60 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000061
62 ReadOnlySection =
Chris Lattner287df1b2010-04-08 21:34:17 +000063 getContext().getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
64 MCSectionELF::SHF_ALLOC,
65 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000066
67 TLSDataSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000068 getContext().getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
69 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
70 MCSectionELF::SHF_WRITE,
71 SectionKind::getThreadData());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000072
73 TLSBSSSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000074 getContext().getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
75 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
76 MCSectionELF::SHF_WRITE,
77 SectionKind::getThreadBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000078
79 DataRelSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000080 getContext().getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
81 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
82 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000083
84 DataRelLocalSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000085 getContext().getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
86 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
87 SectionKind::getDataRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000088
89 DataRelROSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000090 getContext().getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
91 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
92 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000093
94 DataRelROLocalSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000095 getContext().getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
96 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
97 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000098
99 MergeableConst4Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000100 getContext().getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
101 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
102 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000103
104 MergeableConst8Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000105 getContext().getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
106 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
107 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000108
109 MergeableConst16Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000110 getContext().getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
111 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
112 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000113
114 StaticCtorSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000115 getContext().getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
116 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
117 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000118
119 StaticDtorSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000120 getContext().getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
121 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
122 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000123
124 // Exception Handling Sections.
125
126 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
127 // it contains relocatable pointers. In PIC mode, this is probably a big
128 // runtime hit for C++ apps. Either the contents of the LSDA need to be
129 // adjusted or this should be a data section.
130 LSDASection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000131 getContext().getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
132 MCSectionELF::SHF_ALLOC,
133 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000134 EHFrameSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000135 getContext().getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
136 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
137 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000138
139 // Debug Info Sections.
140 DwarfAbbrevSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000141 getContext().getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
142 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000143 DwarfInfoSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000144 getContext().getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
145 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000146 DwarfLineSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000147 getContext().getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
148 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000149 DwarfFrameSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000150 getContext().getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
151 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000152 DwarfPubNamesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000153 getContext().getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
154 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000155 DwarfPubTypesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000156 getContext().getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
157 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000158 DwarfStrSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000159 getContext().getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
160 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000161 DwarfLocSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000162 getContext().getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
163 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000164 DwarfARangesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000165 getContext().getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
166 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000167 DwarfRangesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000168 getContext().getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
169 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000170 DwarfMacroInfoSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000171 getContext().getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
172 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000173}
174
175
176static SectionKind
177getELFKindForNamedSection(StringRef Name, SectionKind K) {
178 if (Name.empty() || Name[0] != '.') return K;
179
180 // Some lame default implementation based on some magic section names.
181 if (Name == ".bss" ||
182 Name.startswith(".bss.") ||
183 Name.startswith(".gnu.linkonce.b.") ||
184 Name.startswith(".llvm.linkonce.b.") ||
185 Name == ".sbss" ||
186 Name.startswith(".sbss.") ||
187 Name.startswith(".gnu.linkonce.sb.") ||
188 Name.startswith(".llvm.linkonce.sb."))
189 return SectionKind::getBSS();
190
191 if (Name == ".tdata" ||
192 Name.startswith(".tdata.") ||
193 Name.startswith(".gnu.linkonce.td.") ||
194 Name.startswith(".llvm.linkonce.td."))
195 return SectionKind::getThreadData();
196
197 if (Name == ".tbss" ||
198 Name.startswith(".tbss.") ||
199 Name.startswith(".gnu.linkonce.tb.") ||
200 Name.startswith(".llvm.linkonce.tb."))
201 return SectionKind::getThreadBSS();
202
203 return K;
204}
205
206
207static unsigned getELFSectionType(StringRef Name, SectionKind K) {
208
209 if (Name == ".init_array")
210 return MCSectionELF::SHT_INIT_ARRAY;
211
212 if (Name == ".fini_array")
213 return MCSectionELF::SHT_FINI_ARRAY;
214
215 if (Name == ".preinit_array")
216 return MCSectionELF::SHT_PREINIT_ARRAY;
217
218 if (K.isBSS() || K.isThreadBSS())
219 return MCSectionELF::SHT_NOBITS;
220
221 return MCSectionELF::SHT_PROGBITS;
222}
223
224
225static unsigned
226getELFSectionFlags(SectionKind K) {
227 unsigned Flags = 0;
228
229 if (!K.isMetadata())
230 Flags |= MCSectionELF::SHF_ALLOC;
231
232 if (K.isText())
233 Flags |= MCSectionELF::SHF_EXECINSTR;
234
235 if (K.isWriteable())
236 Flags |= MCSectionELF::SHF_WRITE;
237
238 if (K.isThreadLocal())
239 Flags |= MCSectionELF::SHF_TLS;
240
241 // K.isMergeableConst() is left out to honour PR4650
242 if (K.isMergeableCString() || K.isMergeableConst4() ||
243 K.isMergeableConst8() || K.isMergeableConst16())
244 Flags |= MCSectionELF::SHF_MERGE;
245
246 if (K.isMergeableCString())
247 Flags |= MCSectionELF::SHF_STRINGS;
248
249 return Flags;
250}
251
252
253const MCSection *TargetLoweringObjectFileELF::
254getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
255 Mangler *Mang, const TargetMachine &TM) const {
256 StringRef SectionName = GV->getSection();
257
258 // Infer section flags from the section name if we can.
259 Kind = getELFKindForNamedSection(SectionName, Kind);
260
Chris Lattner287df1b2010-04-08 21:34:17 +0000261 return getContext().getELFSection(SectionName,
262 getELFSectionType(SectionName, Kind),
263 getELFSectionFlags(Kind), Kind, true);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000264}
265
266static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
267 if (Kind.isText()) return ".gnu.linkonce.t.";
268 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
269
270 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
271 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
272
273 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
274 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
275 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
276 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
277
278 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
279 return ".gnu.linkonce.d.rel.ro.";
280}
281
282const MCSection *TargetLoweringObjectFileELF::
283SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
284 Mangler *Mang, const TargetMachine &TM) const {
285
286 // If this global is linkonce/weak and the target handles this by emitting it
287 // into a 'uniqued' section name, create and return the section now.
288 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
289 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattner4c6741f2010-03-15 20:37:38 +0000290 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
291 MCSymbol *Sym = Mang->getSymbol(GV);
292 Name.append(Sym->getName().begin(), Sym->getName().end());
Chris Lattner287df1b2010-04-08 21:34:17 +0000293 return getContext().getELFSection(Name.str(),
294 getELFSectionType(Name.str(), Kind),
295 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000296 }
297
298 if (Kind.isText()) return TextSection;
299
300 if (Kind.isMergeable1ByteCString() ||
301 Kind.isMergeable2ByteCString() ||
302 Kind.isMergeable4ByteCString()) {
303
304 // We also need alignment here.
305 // FIXME: this is getting the alignment of the character, not the
306 // alignment of the global!
307 unsigned Align =
308 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
309
310 const char *SizeSpec = ".rodata.str1.";
311 if (Kind.isMergeable2ByteCString())
312 SizeSpec = ".rodata.str2.";
313 else if (Kind.isMergeable4ByteCString())
314 SizeSpec = ".rodata.str4.";
315 else
316 assert(Kind.isMergeable1ByteCString() && "unknown string width");
317
318
319 std::string Name = SizeSpec + utostr(Align);
Chris Lattner287df1b2010-04-08 21:34:17 +0000320 return getContext().getELFSection(Name, MCSectionELF::SHT_PROGBITS,
321 MCSectionELF::SHF_ALLOC |
322 MCSectionELF::SHF_MERGE |
323 MCSectionELF::SHF_STRINGS,
324 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000325 }
326
327 if (Kind.isMergeableConst()) {
328 if (Kind.isMergeableConst4() && MergeableConst4Section)
329 return MergeableConst4Section;
330 if (Kind.isMergeableConst8() && MergeableConst8Section)
331 return MergeableConst8Section;
332 if (Kind.isMergeableConst16() && MergeableConst16Section)
333 return MergeableConst16Section;
334 return ReadOnlySection; // .const
335 }
336
337 if (Kind.isReadOnly()) return ReadOnlySection;
338
339 if (Kind.isThreadData()) return TLSDataSection;
340 if (Kind.isThreadBSS()) return TLSBSSSection;
341
342 // Note: we claim that common symbols are put in BSSSection, but they are
343 // really emitted with the magic .comm directive, which creates a symbol table
344 // entry but not a section.
345 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
346
347 if (Kind.isDataNoRel()) return DataSection;
348 if (Kind.isDataRelLocal()) return DataRelLocalSection;
349 if (Kind.isDataRel()) return DataRelSection;
350 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
351
352 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
353 return DataRelROSection;
354}
355
356/// getSectionForConstant - Given a mergeable constant with the
357/// specified size and relocation information, return a section that it
358/// should be placed in.
359const MCSection *TargetLoweringObjectFileELF::
360getSectionForConstant(SectionKind Kind) const {
361 if (Kind.isMergeableConst4() && MergeableConst4Section)
362 return MergeableConst4Section;
363 if (Kind.isMergeableConst8() && MergeableConst8Section)
364 return MergeableConst8Section;
365 if (Kind.isMergeableConst16() && MergeableConst16Section)
366 return MergeableConst16Section;
367 if (Kind.isReadOnly())
368 return ReadOnlySection;
369
370 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
371 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
372 return DataRelROSection;
373}
374
375const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000376getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
377 MachineModuleInfo *MMI,
378 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000379
380 if (Encoding & dwarf::DW_EH_PE_indirect) {
381 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
382
383 SmallString<128> Name;
384 Mang->getNameWithPrefix(Name, GV, true);
385 Name += ".DW.stub";
386
387 // Add information about the stub reference to ELFMMI so that the stub
388 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000389 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000390 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000391 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000392 MCSymbol *Sym = Mang->getSymbol(GV);
393 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000394 }
395
396 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000397 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000398 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000399 }
400
401 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000402 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000403}
404
405//===----------------------------------------------------------------------===//
406// MachO
407//===----------------------------------------------------------------------===//
408
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000409void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
410 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000411 // _foo.eh symbols are currently always exported so that the linker knows
412 // about them. This is not necessary on 10.6 and later, but it
413 // doesn't hurt anything.
414 // FIXME: I need to get this from Triple.
415 IsFunctionEHSymbolGlobal = true;
416 IsFunctionEHFrameSymbolPrivate = false;
417 SupportsWeakOmittedEHFrame = false;
418
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000419 TargetLoweringObjectFile::Initialize(Ctx, TM);
420
421 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000422 = getContext().getMachOSection("__TEXT", "__text",
423 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
424 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000425 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000426 = getContext().getMachOSection("__DATA", "__data", 0,
427 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000428
429 CStringSection // .cstring
Chris Lattner22772212010-04-08 20:40:11 +0000430 = getContext().getMachOSection("__TEXT", "__cstring",
431 MCSectionMachO::S_CSTRING_LITERALS,
432 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000433 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000434 = getContext().getMachOSection("__TEXT","__ustring", 0,
435 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000436 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000437 = getContext().getMachOSection("__TEXT", "__literal4",
438 MCSectionMachO::S_4BYTE_LITERALS,
439 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000440 EightByteConstantSection // .literal8
Chris Lattner22772212010-04-08 20:40:11 +0000441 = getContext().getMachOSection("__TEXT", "__literal8",
442 MCSectionMachO::S_8BYTE_LITERALS,
443 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000444
445 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
446 // to using it in -static mode.
447 SixteenByteConstantSection = 0;
448 if (TM.getRelocationModel() != Reloc::Static &&
449 TM.getTargetData()->getPointerSize() == 32)
450 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000451 getContext().getMachOSection("__TEXT", "__literal16",
452 MCSectionMachO::S_16BYTE_LITERALS,
453 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000454
455 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000456 = getContext().getMachOSection("__TEXT", "__const", 0,
457 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000458
459 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000460 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
461 MCSectionMachO::S_COALESCED |
462 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
463 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000464 ConstTextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000465 = getContext().getMachOSection("__TEXT", "__const_coal",
466 MCSectionMachO::S_COALESCED,
467 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000468 ConstDataCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000469 = getContext().getMachOSection("__DATA","__const_coal",
470 MCSectionMachO::S_COALESCED,
471 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000472 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000473 = getContext().getMachOSection("__DATA", "__const", 0,
474 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000475 DataCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000476 = getContext().getMachOSection("__DATA","__datacoal_nt",
477 MCSectionMachO::S_COALESCED,
478 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000479 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000480 = getContext().getMachOSection("__DATA","__common",
481 MCSectionMachO::S_ZEROFILL,
482 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000483 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000484 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
485 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000486
487
488 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000489 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
490 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
491 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000492 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000493 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
494 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
495 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000496
497 if (TM.getRelocationModel() == Reloc::Static) {
498 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000499 = getContext().getMachOSection("__TEXT", "__constructor", 0,
500 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000501 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000502 = getContext().getMachOSection("__TEXT", "__destructor", 0,
503 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000504 } else {
505 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000506 = getContext().getMachOSection("__DATA", "__mod_init_func",
507 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
508 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000509 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000510 = getContext().getMachOSection("__DATA", "__mod_term_func",
511 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
512 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000513 }
514
515 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000516 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
517 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000518 EHFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000519 getContext().getMachOSection("__TEXT", "__eh_frame",
520 MCSectionMachO::S_COALESCED |
521 MCSectionMachO::S_ATTR_NO_TOC |
522 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
523 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
524 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000525
526 // Debug Information.
527 DwarfAbbrevSection =
Chris Lattner22772212010-04-08 20:40:11 +0000528 getContext().getMachOSection("__DWARF", "__debug_abbrev",
529 MCSectionMachO::S_ATTR_DEBUG,
530 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000531 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000532 getContext().getMachOSection("__DWARF", "__debug_info",
533 MCSectionMachO::S_ATTR_DEBUG,
534 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000535 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000536 getContext().getMachOSection("__DWARF", "__debug_line",
537 MCSectionMachO::S_ATTR_DEBUG,
538 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000539 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000540 getContext().getMachOSection("__DWARF", "__debug_frame",
541 MCSectionMachO::S_ATTR_DEBUG,
542 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000543 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000544 getContext().getMachOSection("__DWARF", "__debug_pubnames",
545 MCSectionMachO::S_ATTR_DEBUG,
546 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000547 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000548 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
549 MCSectionMachO::S_ATTR_DEBUG,
550 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000551 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000552 getContext().getMachOSection("__DWARF", "__debug_str",
553 MCSectionMachO::S_ATTR_DEBUG,
554 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000555 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000556 getContext().getMachOSection("__DWARF", "__debug_loc",
557 MCSectionMachO::S_ATTR_DEBUG,
558 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000559 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000560 getContext().getMachOSection("__DWARF", "__debug_aranges",
561 MCSectionMachO::S_ATTR_DEBUG,
562 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000563 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000564 getContext().getMachOSection("__DWARF", "__debug_ranges",
565 MCSectionMachO::S_ATTR_DEBUG,
566 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000567 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000568 getContext().getMachOSection("__DWARF", "__debug_macinfo",
569 MCSectionMachO::S_ATTR_DEBUG,
570 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000571 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000572 getContext().getMachOSection("__DWARF", "__debug_inlined",
573 MCSectionMachO::S_ATTR_DEBUG,
574 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000575}
576
577const MCSection *TargetLoweringObjectFileMachO::
578getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
579 Mangler *Mang, const TargetMachine &TM) const {
580 // Parse the section specifier and create it if valid.
581 StringRef Segment, Section;
582 unsigned TAA, StubSize;
583 std::string ErrorCode =
584 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
585 TAA, StubSize);
586 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000587 // If invalid, report the error with report_fatal_error.
588 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000589 "' has an invalid section specifier '" + GV->getSection()+
590 "': " + ErrorCode + ".");
591 // Fall back to dropping it into the data section.
592 return DataSection;
593 }
594
595 // Get the section.
596 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000597 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000598
599 // Okay, now that we got the section, verify that the TAA & StubSize agree.
600 // If the user declared multiple globals with different section flags, we need
601 // to reject it here.
602 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000603 // If invalid, report the error with report_fatal_error.
604 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000605 "' section type or attributes does not match previous"
606 " section specifier");
607 }
608
609 return S;
610}
611
612const MCSection *TargetLoweringObjectFileMachO::
613SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
614 Mangler *Mang, const TargetMachine &TM) const {
615 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
616
617 if (Kind.isText())
618 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
619
620 // If this is weak/linkonce, put this in a coalescable section, either in text
621 // or data depending on if it is writable.
622 if (GV->isWeakForLinker()) {
623 if (Kind.isReadOnly())
624 return ConstTextCoalSection;
625 return DataCoalSection;
626 }
627
628 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000629 if (Kind.isMergeable1ByteCString() &&
630 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
631 return CStringSection;
632
633 // Do not put 16-bit arrays in the UString section if they have an
634 // externally visible label, this runs into issues with certain linker
635 // versions.
636 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
637 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
638 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000639
640 if (Kind.isMergeableConst()) {
641 if (Kind.isMergeableConst4())
642 return FourByteConstantSection;
643 if (Kind.isMergeableConst8())
644 return EightByteConstantSection;
645 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
646 return SixteenByteConstantSection;
647 }
648
649 // Otherwise, if it is readonly, but not something we can specially optimize,
650 // just drop it in .const.
651 if (Kind.isReadOnly())
652 return ReadOnlySection;
653
654 // If this is marked const, put it into a const section. But if the dynamic
655 // linker needs to write to it, put it in the data segment.
656 if (Kind.isReadOnlyWithRel())
657 return ConstDataSection;
658
659 // Put zero initialized globals with strong external linkage in the
660 // DATA, __common section with the .zerofill directive.
661 if (Kind.isBSSExtern())
662 return DataCommonSection;
663
664 // Put zero initialized globals with local linkage in __DATA,__bss directive
665 // with the .zerofill directive (aka .lcomm).
666 if (Kind.isBSSLocal())
667 return DataBSSSection;
668
669 // Otherwise, just drop the variable in the normal data section.
670 return DataSection;
671}
672
673const MCSection *
674TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
675 // If this constant requires a relocation, we have to put it in the data
676 // segment, not in the text segment.
677 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
678 return ConstDataSection;
679
680 if (Kind.isMergeableConst4())
681 return FourByteConstantSection;
682 if (Kind.isMergeableConst8())
683 return EightByteConstantSection;
684 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
685 return SixteenByteConstantSection;
686 return ReadOnlySection; // .const
687}
688
689/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
690/// not to emit the UsedDirective for some symbols in llvm.used.
691// FIXME: REMOVE this (rdar://7071300)
692bool TargetLoweringObjectFileMachO::
693shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
694 /// On Darwin, internally linked data beginning with "L" or "l" does not have
695 /// the directive emitted (this occurs in ObjC metadata).
696 if (!GV) return false;
697
698 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
699 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
700 // FIXME: ObjC metadata is currently emitted as internal symbols that have
701 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
702 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000703 MCSymbol *Sym = Mang->getSymbol(GV);
704 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000705 return false;
706 }
707
708 return true;
709}
710
711const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000712getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
713 MachineModuleInfo *MMI, unsigned Encoding,
714 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000715 // The mach-o version of this method defaults to returning a stub reference.
716
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000717 if (Encoding & DW_EH_PE_indirect) {
718 MachineModuleInfoMachO &MachOMMI =
719 MMI->getObjFileInfo<MachineModuleInfoMachO>();
720
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000721 SmallString<128> Name;
722 Mang->getNameWithPrefix(Name, GV, true);
723 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000724
725 // Add information about the stub reference to MachOMMI so that the stub
726 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000727 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000728 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000729 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000730 MCSymbol *Sym = Mang->getSymbol(GV);
731 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000732 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000733
734 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000735 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000736 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000737 }
738
739 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000740 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000741}
742
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000743unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
744 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
745}
746
747unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
748 return DW_EH_PE_pcrel;
749}
750
751unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
752 return DW_EH_PE_pcrel;
753}
754
755unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000756 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000757}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000758
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000759//===----------------------------------------------------------------------===//
760// COFF
761//===----------------------------------------------------------------------===//
762
763typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
764
765TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
766 delete (COFFUniqueMapTy*)UniquingMap;
767}
768
769
770const MCSection *TargetLoweringObjectFileCOFF::
771getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
772 // Create the map if it doesn't already exist.
773 if (UniquingMap == 0)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000774 UniquingMap = new COFFUniqueMapTy();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000775 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
776
777 // Do the lookup, if we have a hit, return it.
778 const MCSectionCOFF *&Entry = Map[Name];
779 if (Entry) return Entry;
780
781 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
782}
783
784void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
785 const TargetMachine &TM) {
786 if (UniquingMap != 0)
787 ((COFFUniqueMapTy*)UniquingMap)->clear();
788 TargetLoweringObjectFile::Initialize(Ctx, TM);
789 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
790 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
791 StaticCtorSection =
792 getCOFFSection(".ctors", false, SectionKind::getDataRel());
793 StaticDtorSection =
794 getCOFFSection(".dtors", false, SectionKind::getDataRel());
795
796 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
797 // though it contains relocatable pointers. In PIC mode, this is probably a
798 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
799 // adjusted or this should be a data section.
800 LSDASection =
801 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
802 EHFrameSection =
803 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
804
805 // Debug info.
806 // FIXME: Don't use 'directive' mode here.
807 DwarfAbbrevSection =
808 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
809 true, SectionKind::getMetadata());
810 DwarfInfoSection =
811 getCOFFSection("\t.section\t.debug_info,\"dr\"",
812 true, SectionKind::getMetadata());
813 DwarfLineSection =
814 getCOFFSection("\t.section\t.debug_line,\"dr\"",
815 true, SectionKind::getMetadata());
816 DwarfFrameSection =
817 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
818 true, SectionKind::getMetadata());
819 DwarfPubNamesSection =
820 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
821 true, SectionKind::getMetadata());
822 DwarfPubTypesSection =
823 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
824 true, SectionKind::getMetadata());
825 DwarfStrSection =
826 getCOFFSection("\t.section\t.debug_str,\"dr\"",
827 true, SectionKind::getMetadata());
828 DwarfLocSection =
829 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
830 true, SectionKind::getMetadata());
831 DwarfARangesSection =
832 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
833 true, SectionKind::getMetadata());
834 DwarfRangesSection =
835 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
836 true, SectionKind::getMetadata());
837 DwarfMacroInfoSection =
838 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
839 true, SectionKind::getMetadata());
840}
841
842const MCSection *TargetLoweringObjectFileCOFF::
843getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
844 Mangler *Mang, const TargetMachine &TM) const {
845 return getCOFFSection(GV->getSection(), false, Kind);
846}
847
848static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
849 if (Kind.isText())
850 return ".text$linkonce";
851 if (Kind.isWriteable())
852 return ".data$linkonce";
853 return ".rdata$linkonce";
854}
855
856
857const MCSection *TargetLoweringObjectFileCOFF::
858SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
859 Mangler *Mang, const TargetMachine &TM) const {
860 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
861
862 // If this global is linkonce/weak and the target handles this by emitting it
863 // into a 'uniqued' section name, create and return the section now.
864 if (GV->isWeakForLinker()) {
865 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
866 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000867 MCSymbol *Sym = Mang->getSymbol(GV);
868 Name.append(Sym->getName().begin(), Sym->getName().end());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000869 return getCOFFSection(Name.str(), false, Kind);
870 }
871
872 if (Kind.isText())
873 return getTextSection();
874
875 return getDataSection();
876}
877