blob: 9f959932e6d5f420505387f5092c933f68137a54 [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
Chris Lattner43ac7212010-04-13 00:36:43 +0000282/// getSectionPrefixForGlobal - Return the section prefix name used by options
283/// FunctionsSections and DataSections.
284static const char *getSectionPrefixForGlobal(SectionKind Kind) {
285 if (Kind.isText()) return ".text.";
286 if (Kind.isReadOnly()) return ".rodata.";
287
288 if (Kind.isThreadData()) return ".tdata.";
289 if (Kind.isThreadBSS()) return ".tbss.";
290
291 if (Kind.isDataNoRel()) return ".data.";
292 if (Kind.isDataRelLocal()) return ".data.rel.local.";
293 if (Kind.isDataRel()) return ".data.rel.";
294 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
295
296 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
297 return ".data.rel.ro.";
298}
299
300
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000301const MCSection *TargetLoweringObjectFileELF::
302SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
303 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000304 // If we have -ffunction-section or -fdata-section then we should emit the
305 // global value to a uniqued section specifically for it.
306 bool EmitUniquedSection;
307 if (Kind.isText())
308 EmitUniquedSection = TM.getFunctionSections();
309 else
310 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000311
312 // If this global is linkonce/weak and the target handles this by emitting it
313 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000314 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
315 !Kind.isCommon() && !Kind.isBSS()) {
316 const char *Prefix;
317 if (GV->isWeakForLinker())
318 Prefix = getSectionPrefixForUniqueGlobal(Kind);
319 else {
320 assert(EmitUniquedSection);
321 Prefix = getSectionPrefixForGlobal(Kind);
322 }
323
Chris Lattner4c6741f2010-03-15 20:37:38 +0000324 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
325 MCSymbol *Sym = Mang->getSymbol(GV);
326 Name.append(Sym->getName().begin(), Sym->getName().end());
Chris Lattner287df1b2010-04-08 21:34:17 +0000327 return getContext().getELFSection(Name.str(),
328 getELFSectionType(Name.str(), Kind),
329 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000330 }
331
332 if (Kind.isText()) return TextSection;
333
334 if (Kind.isMergeable1ByteCString() ||
335 Kind.isMergeable2ByteCString() ||
336 Kind.isMergeable4ByteCString()) {
337
338 // We also need alignment here.
339 // FIXME: this is getting the alignment of the character, not the
340 // alignment of the global!
341 unsigned Align =
342 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
343
344 const char *SizeSpec = ".rodata.str1.";
345 if (Kind.isMergeable2ByteCString())
346 SizeSpec = ".rodata.str2.";
347 else if (Kind.isMergeable4ByteCString())
348 SizeSpec = ".rodata.str4.";
349 else
350 assert(Kind.isMergeable1ByteCString() && "unknown string width");
351
352
353 std::string Name = SizeSpec + utostr(Align);
Chris Lattner287df1b2010-04-08 21:34:17 +0000354 return getContext().getELFSection(Name, MCSectionELF::SHT_PROGBITS,
355 MCSectionELF::SHF_ALLOC |
356 MCSectionELF::SHF_MERGE |
357 MCSectionELF::SHF_STRINGS,
358 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000359 }
360
361 if (Kind.isMergeableConst()) {
362 if (Kind.isMergeableConst4() && MergeableConst4Section)
363 return MergeableConst4Section;
364 if (Kind.isMergeableConst8() && MergeableConst8Section)
365 return MergeableConst8Section;
366 if (Kind.isMergeableConst16() && MergeableConst16Section)
367 return MergeableConst16Section;
368 return ReadOnlySection; // .const
369 }
370
371 if (Kind.isReadOnly()) return ReadOnlySection;
372
373 if (Kind.isThreadData()) return TLSDataSection;
374 if (Kind.isThreadBSS()) return TLSBSSSection;
375
376 // Note: we claim that common symbols are put in BSSSection, but they are
377 // really emitted with the magic .comm directive, which creates a symbol table
378 // entry but not a section.
379 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
380
381 if (Kind.isDataNoRel()) return DataSection;
382 if (Kind.isDataRelLocal()) return DataRelLocalSection;
383 if (Kind.isDataRel()) return DataRelSection;
384 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
385
386 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
387 return DataRelROSection;
388}
389
390/// getSectionForConstant - Given a mergeable constant with the
391/// specified size and relocation information, return a section that it
392/// should be placed in.
393const MCSection *TargetLoweringObjectFileELF::
394getSectionForConstant(SectionKind Kind) const {
395 if (Kind.isMergeableConst4() && MergeableConst4Section)
396 return MergeableConst4Section;
397 if (Kind.isMergeableConst8() && MergeableConst8Section)
398 return MergeableConst8Section;
399 if (Kind.isMergeableConst16() && MergeableConst16Section)
400 return MergeableConst16Section;
401 if (Kind.isReadOnly())
402 return ReadOnlySection;
403
404 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
405 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
406 return DataRelROSection;
407}
408
409const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000410getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
411 MachineModuleInfo *MMI,
412 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000413
414 if (Encoding & dwarf::DW_EH_PE_indirect) {
415 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
416
417 SmallString<128> Name;
418 Mang->getNameWithPrefix(Name, GV, true);
419 Name += ".DW.stub";
420
421 // Add information about the stub reference to ELFMMI so that the stub
422 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000423 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000424 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000425 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000426 MCSymbol *Sym = Mang->getSymbol(GV);
427 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000428 }
429
430 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000431 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000432 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000433 }
434
435 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000436 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000437}
438
439//===----------------------------------------------------------------------===//
440// MachO
441//===----------------------------------------------------------------------===//
442
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000443void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
444 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000445 // _foo.eh symbols are currently always exported so that the linker knows
446 // about them. This is not necessary on 10.6 and later, but it
447 // doesn't hurt anything.
448 // FIXME: I need to get this from Triple.
449 IsFunctionEHSymbolGlobal = true;
450 IsFunctionEHFrameSymbolPrivate = false;
451 SupportsWeakOmittedEHFrame = false;
452
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000453 TargetLoweringObjectFile::Initialize(Ctx, TM);
454
455 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000456 = getContext().getMachOSection("__TEXT", "__text",
457 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
458 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000459 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000460 = getContext().getMachOSection("__DATA", "__data", 0,
461 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000462
463 CStringSection // .cstring
Chris Lattner22772212010-04-08 20:40:11 +0000464 = getContext().getMachOSection("__TEXT", "__cstring",
465 MCSectionMachO::S_CSTRING_LITERALS,
466 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000467 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000468 = getContext().getMachOSection("__TEXT","__ustring", 0,
469 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000470 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000471 = getContext().getMachOSection("__TEXT", "__literal4",
472 MCSectionMachO::S_4BYTE_LITERALS,
473 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000474 EightByteConstantSection // .literal8
Chris Lattner22772212010-04-08 20:40:11 +0000475 = getContext().getMachOSection("__TEXT", "__literal8",
476 MCSectionMachO::S_8BYTE_LITERALS,
477 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000478
479 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
480 // to using it in -static mode.
481 SixteenByteConstantSection = 0;
482 if (TM.getRelocationModel() != Reloc::Static &&
483 TM.getTargetData()->getPointerSize() == 32)
484 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000485 getContext().getMachOSection("__TEXT", "__literal16",
486 MCSectionMachO::S_16BYTE_LITERALS,
487 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000488
489 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000490 = getContext().getMachOSection("__TEXT", "__const", 0,
491 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000492
493 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000494 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
495 MCSectionMachO::S_COALESCED |
496 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
497 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000498 ConstTextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000499 = getContext().getMachOSection("__TEXT", "__const_coal",
500 MCSectionMachO::S_COALESCED,
501 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000502 ConstDataCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000503 = getContext().getMachOSection("__DATA","__const_coal",
504 MCSectionMachO::S_COALESCED,
505 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000506 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000507 = getContext().getMachOSection("__DATA", "__const", 0,
508 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000509 DataCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000510 = getContext().getMachOSection("__DATA","__datacoal_nt",
511 MCSectionMachO::S_COALESCED,
512 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000513 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000514 = getContext().getMachOSection("__DATA","__common",
515 MCSectionMachO::S_ZEROFILL,
516 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000517 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000518 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
519 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000520
521
522 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000523 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
524 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
525 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000526 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000527 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
528 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
529 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000530
531 if (TM.getRelocationModel() == Reloc::Static) {
532 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000533 = getContext().getMachOSection("__TEXT", "__constructor", 0,
534 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000535 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000536 = getContext().getMachOSection("__TEXT", "__destructor", 0,
537 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000538 } else {
539 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000540 = getContext().getMachOSection("__DATA", "__mod_init_func",
541 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
542 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000543 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000544 = getContext().getMachOSection("__DATA", "__mod_term_func",
545 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
546 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000547 }
548
549 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000550 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
551 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000552 EHFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000553 getContext().getMachOSection("__TEXT", "__eh_frame",
554 MCSectionMachO::S_COALESCED |
555 MCSectionMachO::S_ATTR_NO_TOC |
556 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
557 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
558 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000559
560 // Debug Information.
561 DwarfAbbrevSection =
Chris Lattner22772212010-04-08 20:40:11 +0000562 getContext().getMachOSection("__DWARF", "__debug_abbrev",
563 MCSectionMachO::S_ATTR_DEBUG,
564 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000565 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000566 getContext().getMachOSection("__DWARF", "__debug_info",
567 MCSectionMachO::S_ATTR_DEBUG,
568 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000569 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000570 getContext().getMachOSection("__DWARF", "__debug_line",
571 MCSectionMachO::S_ATTR_DEBUG,
572 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000573 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000574 getContext().getMachOSection("__DWARF", "__debug_frame",
575 MCSectionMachO::S_ATTR_DEBUG,
576 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000577 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000578 getContext().getMachOSection("__DWARF", "__debug_pubnames",
579 MCSectionMachO::S_ATTR_DEBUG,
580 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000581 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000582 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
583 MCSectionMachO::S_ATTR_DEBUG,
584 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000585 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000586 getContext().getMachOSection("__DWARF", "__debug_str",
587 MCSectionMachO::S_ATTR_DEBUG,
588 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000589 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000590 getContext().getMachOSection("__DWARF", "__debug_loc",
591 MCSectionMachO::S_ATTR_DEBUG,
592 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000593 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000594 getContext().getMachOSection("__DWARF", "__debug_aranges",
595 MCSectionMachO::S_ATTR_DEBUG,
596 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000597 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000598 getContext().getMachOSection("__DWARF", "__debug_ranges",
599 MCSectionMachO::S_ATTR_DEBUG,
600 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000601 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000602 getContext().getMachOSection("__DWARF", "__debug_macinfo",
603 MCSectionMachO::S_ATTR_DEBUG,
604 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000605 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000606 getContext().getMachOSection("__DWARF", "__debug_inlined",
607 MCSectionMachO::S_ATTR_DEBUG,
608 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000609}
610
611const MCSection *TargetLoweringObjectFileMachO::
612getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
613 Mangler *Mang, const TargetMachine &TM) const {
614 // Parse the section specifier and create it if valid.
615 StringRef Segment, Section;
616 unsigned TAA, StubSize;
617 std::string ErrorCode =
618 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
619 TAA, StubSize);
620 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000621 // If invalid, report the error with report_fatal_error.
622 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000623 "' has an invalid section specifier '" + GV->getSection()+
624 "': " + ErrorCode + ".");
625 // Fall back to dropping it into the data section.
626 return DataSection;
627 }
628
629 // Get the section.
630 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000631 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000632
633 // Okay, now that we got the section, verify that the TAA & StubSize agree.
634 // If the user declared multiple globals with different section flags, we need
635 // to reject it here.
636 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000637 // If invalid, report the error with report_fatal_error.
638 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000639 "' section type or attributes does not match previous"
640 " section specifier");
641 }
642
643 return S;
644}
645
646const MCSection *TargetLoweringObjectFileMachO::
647SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
648 Mangler *Mang, const TargetMachine &TM) const {
649 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
650
651 if (Kind.isText())
652 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
653
654 // If this is weak/linkonce, put this in a coalescable section, either in text
655 // or data depending on if it is writable.
656 if (GV->isWeakForLinker()) {
657 if (Kind.isReadOnly())
658 return ConstTextCoalSection;
659 return DataCoalSection;
660 }
661
662 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000663 if (Kind.isMergeable1ByteCString() &&
664 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
665 return CStringSection;
666
667 // Do not put 16-bit arrays in the UString section if they have an
668 // externally visible label, this runs into issues with certain linker
669 // versions.
670 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
671 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
672 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000673
674 if (Kind.isMergeableConst()) {
675 if (Kind.isMergeableConst4())
676 return FourByteConstantSection;
677 if (Kind.isMergeableConst8())
678 return EightByteConstantSection;
679 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
680 return SixteenByteConstantSection;
681 }
682
683 // Otherwise, if it is readonly, but not something we can specially optimize,
684 // just drop it in .const.
685 if (Kind.isReadOnly())
686 return ReadOnlySection;
687
688 // If this is marked const, put it into a const section. But if the dynamic
689 // linker needs to write to it, put it in the data segment.
690 if (Kind.isReadOnlyWithRel())
691 return ConstDataSection;
692
693 // Put zero initialized globals with strong external linkage in the
694 // DATA, __common section with the .zerofill directive.
695 if (Kind.isBSSExtern())
696 return DataCommonSection;
697
698 // Put zero initialized globals with local linkage in __DATA,__bss directive
699 // with the .zerofill directive (aka .lcomm).
700 if (Kind.isBSSLocal())
701 return DataBSSSection;
702
703 // Otherwise, just drop the variable in the normal data section.
704 return DataSection;
705}
706
707const MCSection *
708TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
709 // If this constant requires a relocation, we have to put it in the data
710 // segment, not in the text segment.
711 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
712 return ConstDataSection;
713
714 if (Kind.isMergeableConst4())
715 return FourByteConstantSection;
716 if (Kind.isMergeableConst8())
717 return EightByteConstantSection;
718 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
719 return SixteenByteConstantSection;
720 return ReadOnlySection; // .const
721}
722
723/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
724/// not to emit the UsedDirective for some symbols in llvm.used.
725// FIXME: REMOVE this (rdar://7071300)
726bool TargetLoweringObjectFileMachO::
727shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
728 /// On Darwin, internally linked data beginning with "L" or "l" does not have
729 /// the directive emitted (this occurs in ObjC metadata).
730 if (!GV) return false;
731
732 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
733 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
734 // FIXME: ObjC metadata is currently emitted as internal symbols that have
735 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
736 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000737 MCSymbol *Sym = Mang->getSymbol(GV);
738 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000739 return false;
740 }
741
742 return true;
743}
744
745const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000746getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
747 MachineModuleInfo *MMI, unsigned Encoding,
748 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000749 // The mach-o version of this method defaults to returning a stub reference.
750
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000751 if (Encoding & DW_EH_PE_indirect) {
752 MachineModuleInfoMachO &MachOMMI =
753 MMI->getObjFileInfo<MachineModuleInfoMachO>();
754
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000755 SmallString<128> Name;
756 Mang->getNameWithPrefix(Name, GV, true);
757 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000758
759 // Add information about the stub reference to MachOMMI so that the stub
760 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000761 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000762 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000763 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000764 MCSymbol *Sym = Mang->getSymbol(GV);
765 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000766 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000767
768 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000769 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000770 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000771 }
772
773 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000774 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000775}
776
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000777unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
778 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
779}
780
781unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
782 return DW_EH_PE_pcrel;
783}
784
785unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
786 return DW_EH_PE_pcrel;
787}
788
789unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000790 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000791}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000792
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000793//===----------------------------------------------------------------------===//
794// COFF
795//===----------------------------------------------------------------------===//
796
797typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
798
799TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
800 delete (COFFUniqueMapTy*)UniquingMap;
801}
802
803
804const MCSection *TargetLoweringObjectFileCOFF::
805getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
806 // Create the map if it doesn't already exist.
807 if (UniquingMap == 0)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000808 UniquingMap = new COFFUniqueMapTy();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000809 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
810
811 // Do the lookup, if we have a hit, return it.
812 const MCSectionCOFF *&Entry = Map[Name];
813 if (Entry) return Entry;
814
815 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
816}
817
818void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
819 const TargetMachine &TM) {
820 if (UniquingMap != 0)
821 ((COFFUniqueMapTy*)UniquingMap)->clear();
822 TargetLoweringObjectFile::Initialize(Ctx, TM);
823 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
824 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
825 StaticCtorSection =
826 getCOFFSection(".ctors", false, SectionKind::getDataRel());
827 StaticDtorSection =
828 getCOFFSection(".dtors", false, SectionKind::getDataRel());
829
830 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
831 // though it contains relocatable pointers. In PIC mode, this is probably a
832 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
833 // adjusted or this should be a data section.
834 LSDASection =
835 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
836 EHFrameSection =
837 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
838
839 // Debug info.
840 // FIXME: Don't use 'directive' mode here.
841 DwarfAbbrevSection =
842 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
843 true, SectionKind::getMetadata());
844 DwarfInfoSection =
845 getCOFFSection("\t.section\t.debug_info,\"dr\"",
846 true, SectionKind::getMetadata());
847 DwarfLineSection =
848 getCOFFSection("\t.section\t.debug_line,\"dr\"",
849 true, SectionKind::getMetadata());
850 DwarfFrameSection =
851 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
852 true, SectionKind::getMetadata());
853 DwarfPubNamesSection =
854 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
855 true, SectionKind::getMetadata());
856 DwarfPubTypesSection =
857 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
858 true, SectionKind::getMetadata());
859 DwarfStrSection =
860 getCOFFSection("\t.section\t.debug_str,\"dr\"",
861 true, SectionKind::getMetadata());
862 DwarfLocSection =
863 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
864 true, SectionKind::getMetadata());
865 DwarfARangesSection =
866 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
867 true, SectionKind::getMetadata());
868 DwarfRangesSection =
869 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
870 true, SectionKind::getMetadata());
871 DwarfMacroInfoSection =
872 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
873 true, SectionKind::getMetadata());
874}
875
876const MCSection *TargetLoweringObjectFileCOFF::
877getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
878 Mangler *Mang, const TargetMachine &TM) const {
879 return getCOFFSection(GV->getSection(), false, Kind);
880}
881
882static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
883 if (Kind.isText())
884 return ".text$linkonce";
885 if (Kind.isWriteable())
886 return ".data$linkonce";
887 return ".rdata$linkonce";
888}
889
890
891const MCSection *TargetLoweringObjectFileCOFF::
892SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
893 Mangler *Mang, const TargetMachine &TM) const {
894 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
895
896 // If this global is linkonce/weak and the target handles this by emitting it
897 // into a 'uniqued' section name, create and return the section now.
898 if (GV->isWeakForLinker()) {
899 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
900 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000901 MCSymbol *Sym = Mang->getSymbol(GV);
902 Name.append(Sym->getName().begin(), Sym->getName().end());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000903 return getCOFFSection(Name.str(), false, Kind);
904 }
905
906 if (Kind.isText())
907 return getTextSection();
908
909 return getDataSection();
910}
911