blob: f1e10eec724c66e1a28a068c951aadb6ae3e2970 [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"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000025#include "llvm/MC/MCSectionCOFF.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000026#include "llvm/MC/MCSymbol.h"
27#include "llvm/Target/Mangler.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetOptions.h"
31#include "llvm/Support/Dwarf.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/ADT/SmallString.h"
35#include "llvm/ADT/StringExtras.h"
36using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000037using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000038
39//===----------------------------------------------------------------------===//
40// ELF
41//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000042
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000043void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
44 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000045 TargetLoweringObjectFile::Initialize(Ctx, TM);
46
47 BSSSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000048 getContext().getELFSection(".bss", MCSectionELF::SHT_NOBITS,
49 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
50 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000051
52 TextSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000053 getContext().getELFSection(".text", MCSectionELF::SHT_PROGBITS,
54 MCSectionELF::SHF_EXECINSTR |
55 MCSectionELF::SHF_ALLOC,
56 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000057
58 DataSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000059 getContext().getELFSection(".data", MCSectionELF::SHT_PROGBITS,
60 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
61 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000062
63 ReadOnlySection =
Chris Lattner287df1b2010-04-08 21:34:17 +000064 getContext().getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
65 MCSectionELF::SHF_ALLOC,
66 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000067
68 TLSDataSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000069 getContext().getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
70 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
71 MCSectionELF::SHF_WRITE,
72 SectionKind::getThreadData());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000073
74 TLSBSSSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000075 getContext().getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
76 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
77 MCSectionELF::SHF_WRITE,
78 SectionKind::getThreadBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000079
80 DataRelSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000081 getContext().getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
82 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
83 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000084
85 DataRelLocalSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000086 getContext().getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
87 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
88 SectionKind::getDataRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000089
90 DataRelROSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000091 getContext().getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
92 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
93 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000094
95 DataRelROLocalSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000096 getContext().getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
97 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
98 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000099
100 MergeableConst4Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000101 getContext().getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
102 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
103 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000104
105 MergeableConst8Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000106 getContext().getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
107 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
108 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000109
110 MergeableConst16Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000111 getContext().getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
112 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
113 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000114
115 StaticCtorSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000116 getContext().getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
117 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
118 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000119
120 StaticDtorSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000121 getContext().getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
122 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
123 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000124
125 // Exception Handling Sections.
126
127 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
128 // it contains relocatable pointers. In PIC mode, this is probably a big
129 // runtime hit for C++ apps. Either the contents of the LSDA need to be
130 // adjusted or this should be a data section.
131 LSDASection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000132 getContext().getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
133 MCSectionELF::SHF_ALLOC,
134 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000135 EHFrameSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000136 getContext().getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
137 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
138 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000139
140 // Debug Info Sections.
141 DwarfAbbrevSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000142 getContext().getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
143 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000144 DwarfInfoSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000145 getContext().getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
146 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000147 DwarfLineSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000148 getContext().getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
149 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000150 DwarfFrameSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000151 getContext().getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
152 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000153 DwarfPubNamesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000154 getContext().getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
155 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000156 DwarfPubTypesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000157 getContext().getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
158 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000159 DwarfStrSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000160 getContext().getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
161 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000162 DwarfLocSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000163 getContext().getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
164 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000165 DwarfARangesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000166 getContext().getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
167 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000168 DwarfRangesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000169 getContext().getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
170 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000171 DwarfMacroInfoSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000172 getContext().getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
173 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000174}
175
176
177static SectionKind
178getELFKindForNamedSection(StringRef Name, SectionKind K) {
179 if (Name.empty() || Name[0] != '.') return K;
180
181 // Some lame default implementation based on some magic section names.
182 if (Name == ".bss" ||
183 Name.startswith(".bss.") ||
184 Name.startswith(".gnu.linkonce.b.") ||
185 Name.startswith(".llvm.linkonce.b.") ||
186 Name == ".sbss" ||
187 Name.startswith(".sbss.") ||
188 Name.startswith(".gnu.linkonce.sb.") ||
189 Name.startswith(".llvm.linkonce.sb."))
190 return SectionKind::getBSS();
191
192 if (Name == ".tdata" ||
193 Name.startswith(".tdata.") ||
194 Name.startswith(".gnu.linkonce.td.") ||
195 Name.startswith(".llvm.linkonce.td."))
196 return SectionKind::getThreadData();
197
198 if (Name == ".tbss" ||
199 Name.startswith(".tbss.") ||
200 Name.startswith(".gnu.linkonce.tb.") ||
201 Name.startswith(".llvm.linkonce.tb."))
202 return SectionKind::getThreadBSS();
203
204 return K;
205}
206
207
208static unsigned getELFSectionType(StringRef Name, SectionKind K) {
209
210 if (Name == ".init_array")
211 return MCSectionELF::SHT_INIT_ARRAY;
212
213 if (Name == ".fini_array")
214 return MCSectionELF::SHT_FINI_ARRAY;
215
216 if (Name == ".preinit_array")
217 return MCSectionELF::SHT_PREINIT_ARRAY;
218
219 if (K.isBSS() || K.isThreadBSS())
220 return MCSectionELF::SHT_NOBITS;
221
222 return MCSectionELF::SHT_PROGBITS;
223}
224
225
226static unsigned
227getELFSectionFlags(SectionKind K) {
228 unsigned Flags = 0;
229
230 if (!K.isMetadata())
231 Flags |= MCSectionELF::SHF_ALLOC;
232
233 if (K.isText())
234 Flags |= MCSectionELF::SHF_EXECINSTR;
235
236 if (K.isWriteable())
237 Flags |= MCSectionELF::SHF_WRITE;
238
239 if (K.isThreadLocal())
240 Flags |= MCSectionELF::SHF_TLS;
241
242 // K.isMergeableConst() is left out to honour PR4650
243 if (K.isMergeableCString() || K.isMergeableConst4() ||
244 K.isMergeableConst8() || K.isMergeableConst16())
245 Flags |= MCSectionELF::SHF_MERGE;
246
247 if (K.isMergeableCString())
248 Flags |= MCSectionELF::SHF_STRINGS;
249
250 return Flags;
251}
252
253
254const MCSection *TargetLoweringObjectFileELF::
255getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
256 Mangler *Mang, const TargetMachine &TM) const {
257 StringRef SectionName = GV->getSection();
258
259 // Infer section flags from the section name if we can.
260 Kind = getELFKindForNamedSection(SectionName, Kind);
261
Chris Lattner287df1b2010-04-08 21:34:17 +0000262 return getContext().getELFSection(SectionName,
263 getELFSectionType(SectionName, Kind),
264 getELFSectionFlags(Kind), Kind, true);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000265}
266
267static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
268 if (Kind.isText()) return ".gnu.linkonce.t.";
269 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
270
271 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
272 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
273
274 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
275 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
276 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
277 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
278
279 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
280 return ".gnu.linkonce.d.rel.ro.";
281}
282
Chris Lattner43ac7212010-04-13 00:36:43 +0000283/// getSectionPrefixForGlobal - Return the section prefix name used by options
284/// FunctionsSections and DataSections.
285static const char *getSectionPrefixForGlobal(SectionKind Kind) {
286 if (Kind.isText()) return ".text.";
287 if (Kind.isReadOnly()) return ".rodata.";
288
289 if (Kind.isThreadData()) return ".tdata.";
290 if (Kind.isThreadBSS()) return ".tbss.";
291
292 if (Kind.isDataNoRel()) return ".data.";
293 if (Kind.isDataRelLocal()) return ".data.rel.local.";
294 if (Kind.isDataRel()) return ".data.rel.";
295 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
296
297 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
298 return ".data.rel.ro.";
299}
300
301
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000302const MCSection *TargetLoweringObjectFileELF::
303SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
304 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000305 // If we have -ffunction-section or -fdata-section then we should emit the
306 // global value to a uniqued section specifically for it.
307 bool EmitUniquedSection;
308 if (Kind.isText())
309 EmitUniquedSection = TM.getFunctionSections();
310 else
311 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000312
313 // If this global is linkonce/weak and the target handles this by emitting it
314 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000315 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
316 !Kind.isCommon() && !Kind.isBSS()) {
317 const char *Prefix;
318 if (GV->isWeakForLinker())
319 Prefix = getSectionPrefixForUniqueGlobal(Kind);
320 else {
321 assert(EmitUniquedSection);
322 Prefix = getSectionPrefixForGlobal(Kind);
323 }
324
Chris Lattner4c6741f2010-03-15 20:37:38 +0000325 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
326 MCSymbol *Sym = Mang->getSymbol(GV);
327 Name.append(Sym->getName().begin(), Sym->getName().end());
Chris Lattner287df1b2010-04-08 21:34:17 +0000328 return getContext().getELFSection(Name.str(),
329 getELFSectionType(Name.str(), Kind),
330 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000331 }
332
333 if (Kind.isText()) return TextSection;
334
335 if (Kind.isMergeable1ByteCString() ||
336 Kind.isMergeable2ByteCString() ||
337 Kind.isMergeable4ByteCString()) {
338
339 // We also need alignment here.
340 // FIXME: this is getting the alignment of the character, not the
341 // alignment of the global!
342 unsigned Align =
343 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
344
345 const char *SizeSpec = ".rodata.str1.";
346 if (Kind.isMergeable2ByteCString())
347 SizeSpec = ".rodata.str2.";
348 else if (Kind.isMergeable4ByteCString())
349 SizeSpec = ".rodata.str4.";
350 else
351 assert(Kind.isMergeable1ByteCString() && "unknown string width");
352
353
354 std::string Name = SizeSpec + utostr(Align);
Chris Lattner287df1b2010-04-08 21:34:17 +0000355 return getContext().getELFSection(Name, MCSectionELF::SHT_PROGBITS,
356 MCSectionELF::SHF_ALLOC |
357 MCSectionELF::SHF_MERGE |
358 MCSectionELF::SHF_STRINGS,
359 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000360 }
361
362 if (Kind.isMergeableConst()) {
363 if (Kind.isMergeableConst4() && MergeableConst4Section)
364 return MergeableConst4Section;
365 if (Kind.isMergeableConst8() && MergeableConst8Section)
366 return MergeableConst8Section;
367 if (Kind.isMergeableConst16() && MergeableConst16Section)
368 return MergeableConst16Section;
369 return ReadOnlySection; // .const
370 }
371
372 if (Kind.isReadOnly()) return ReadOnlySection;
373
374 if (Kind.isThreadData()) return TLSDataSection;
375 if (Kind.isThreadBSS()) return TLSBSSSection;
376
377 // Note: we claim that common symbols are put in BSSSection, but they are
378 // really emitted with the magic .comm directive, which creates a symbol table
379 // entry but not a section.
380 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
381
382 if (Kind.isDataNoRel()) return DataSection;
383 if (Kind.isDataRelLocal()) return DataRelLocalSection;
384 if (Kind.isDataRel()) return DataRelSection;
385 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
386
387 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
388 return DataRelROSection;
389}
390
391/// getSectionForConstant - Given a mergeable constant with the
392/// specified size and relocation information, return a section that it
393/// should be placed in.
394const MCSection *TargetLoweringObjectFileELF::
395getSectionForConstant(SectionKind Kind) const {
396 if (Kind.isMergeableConst4() && MergeableConst4Section)
397 return MergeableConst4Section;
398 if (Kind.isMergeableConst8() && MergeableConst8Section)
399 return MergeableConst8Section;
400 if (Kind.isMergeableConst16() && MergeableConst16Section)
401 return MergeableConst16Section;
402 if (Kind.isReadOnly())
403 return ReadOnlySection;
404
405 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
406 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
407 return DataRelROSection;
408}
409
410const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000411getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
412 MachineModuleInfo *MMI,
413 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000414
415 if (Encoding & dwarf::DW_EH_PE_indirect) {
416 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
417
418 SmallString<128> Name;
419 Mang->getNameWithPrefix(Name, GV, true);
420 Name += ".DW.stub";
421
422 // Add information about the stub reference to ELFMMI so that the stub
423 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000424 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000425 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000426 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000427 MCSymbol *Sym = Mang->getSymbol(GV);
428 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000429 }
430
431 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000432 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000433 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000434 }
435
436 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000437 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000438}
439
440//===----------------------------------------------------------------------===//
441// MachO
442//===----------------------------------------------------------------------===//
443
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000444void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
445 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000446 // _foo.eh symbols are currently always exported so that the linker knows
447 // about them. This is not necessary on 10.6 and later, but it
448 // doesn't hurt anything.
449 // FIXME: I need to get this from Triple.
450 IsFunctionEHSymbolGlobal = true;
451 IsFunctionEHFrameSymbolPrivate = false;
452 SupportsWeakOmittedEHFrame = false;
453
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000454 TargetLoweringObjectFile::Initialize(Ctx, TM);
455
456 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000457 = getContext().getMachOSection("__TEXT", "__text",
458 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
459 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000460 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000461 = getContext().getMachOSection("__DATA", "__data", 0,
462 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000463
Eric Christopher423c9e32010-05-17 21:02:07 +0000464 TLSDataSection // .tdata
465 = getContext().getMachOSection("__DATA", "__thread_data",
466 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
467 SectionKind::getDataRel());
468 TLSBSSSection // .tbss
469 = getContext().getMachOSection("__DATA", "__thread_bss",
470 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
471 SectionKind::getThreadBSS());
472
473 // TODO: Verify datarel below.
474 TLSTLVSection // .tlv
475 = getContext().getMachOSection("__DATA", "__thread_vars",
476 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
477 SectionKind::getDataRel());
Eric Christopherc6177a42010-05-17 22:53:55 +0000478
479 TLSThreadInitSection
480 = getContext().getMachOSection("__DATA", "__thread_init",
481 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
482 SectionKind::getDataRel());
483
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000484 CStringSection // .cstring
Chris Lattner22772212010-04-08 20:40:11 +0000485 = getContext().getMachOSection("__TEXT", "__cstring",
486 MCSectionMachO::S_CSTRING_LITERALS,
487 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000488 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000489 = getContext().getMachOSection("__TEXT","__ustring", 0,
490 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000491 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000492 = getContext().getMachOSection("__TEXT", "__literal4",
493 MCSectionMachO::S_4BYTE_LITERALS,
494 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000495 EightByteConstantSection // .literal8
Chris Lattner22772212010-04-08 20:40:11 +0000496 = getContext().getMachOSection("__TEXT", "__literal8",
497 MCSectionMachO::S_8BYTE_LITERALS,
498 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000499
500 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
501 // to using it in -static mode.
502 SixteenByteConstantSection = 0;
503 if (TM.getRelocationModel() != Reloc::Static &&
504 TM.getTargetData()->getPointerSize() == 32)
505 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000506 getContext().getMachOSection("__TEXT", "__literal16",
507 MCSectionMachO::S_16BYTE_LITERALS,
508 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000509
510 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000511 = getContext().getMachOSection("__TEXT", "__const", 0,
512 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000513
514 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000515 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
516 MCSectionMachO::S_COALESCED |
517 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
518 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000519 ConstTextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000520 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000521 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000522 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000523 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000524 = getContext().getMachOSection("__DATA", "__const", 0,
525 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000526 DataCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000527 = getContext().getMachOSection("__DATA","__datacoal_nt",
528 MCSectionMachO::S_COALESCED,
529 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000530 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000531 = getContext().getMachOSection("__DATA","__common",
532 MCSectionMachO::S_ZEROFILL,
533 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000534 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000535 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
536 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000537
538
539 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000540 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
541 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
542 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000543 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000544 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
545 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
546 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000547
548 if (TM.getRelocationModel() == Reloc::Static) {
549 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000550 = getContext().getMachOSection("__TEXT", "__constructor", 0,
551 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000552 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000553 = getContext().getMachOSection("__TEXT", "__destructor", 0,
554 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000555 } else {
556 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000557 = getContext().getMachOSection("__DATA", "__mod_init_func",
558 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
559 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000560 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000561 = getContext().getMachOSection("__DATA", "__mod_term_func",
562 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
563 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000564 }
565
566 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000567 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
568 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000569 EHFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000570 getContext().getMachOSection("__TEXT", "__eh_frame",
571 MCSectionMachO::S_COALESCED |
572 MCSectionMachO::S_ATTR_NO_TOC |
573 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
574 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
575 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000576
577 // Debug Information.
578 DwarfAbbrevSection =
Chris Lattner22772212010-04-08 20:40:11 +0000579 getContext().getMachOSection("__DWARF", "__debug_abbrev",
580 MCSectionMachO::S_ATTR_DEBUG,
581 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000582 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000583 getContext().getMachOSection("__DWARF", "__debug_info",
584 MCSectionMachO::S_ATTR_DEBUG,
585 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000586 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000587 getContext().getMachOSection("__DWARF", "__debug_line",
588 MCSectionMachO::S_ATTR_DEBUG,
589 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000590 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000591 getContext().getMachOSection("__DWARF", "__debug_frame",
592 MCSectionMachO::S_ATTR_DEBUG,
593 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000594 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000595 getContext().getMachOSection("__DWARF", "__debug_pubnames",
596 MCSectionMachO::S_ATTR_DEBUG,
597 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000598 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000599 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
600 MCSectionMachO::S_ATTR_DEBUG,
601 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000602 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000603 getContext().getMachOSection("__DWARF", "__debug_str",
604 MCSectionMachO::S_ATTR_DEBUG,
605 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000606 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000607 getContext().getMachOSection("__DWARF", "__debug_loc",
608 MCSectionMachO::S_ATTR_DEBUG,
609 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000610 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000611 getContext().getMachOSection("__DWARF", "__debug_aranges",
612 MCSectionMachO::S_ATTR_DEBUG,
613 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000614 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000615 getContext().getMachOSection("__DWARF", "__debug_ranges",
616 MCSectionMachO::S_ATTR_DEBUG,
617 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000618 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000619 getContext().getMachOSection("__DWARF", "__debug_macinfo",
620 MCSectionMachO::S_ATTR_DEBUG,
621 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000622 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000623 getContext().getMachOSection("__DWARF", "__debug_inlined",
624 MCSectionMachO::S_ATTR_DEBUG,
625 SectionKind::getMetadata());
Eric Christopher8116ca52010-05-22 00:10:22 +0000626
627 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000628}
629
630const MCSection *TargetLoweringObjectFileMachO::
631getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
632 Mangler *Mang, const TargetMachine &TM) const {
633 // Parse the section specifier and create it if valid.
634 StringRef Segment, Section;
635 unsigned TAA, StubSize;
636 std::string ErrorCode =
637 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
638 TAA, StubSize);
639 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000640 // If invalid, report the error with report_fatal_error.
641 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000642 "' has an invalid section specifier '" + GV->getSection()+
643 "': " + ErrorCode + ".");
644 // Fall back to dropping it into the data section.
645 return DataSection;
646 }
647
648 // Get the section.
649 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000650 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000651
652 // Okay, now that we got the section, verify that the TAA & StubSize agree.
653 // If the user declared multiple globals with different section flags, we need
654 // to reject it here.
655 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000656 // If invalid, report the error with report_fatal_error.
657 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000658 "' section type or attributes does not match previous"
659 " section specifier");
660 }
661
662 return S;
663}
664
665const MCSection *TargetLoweringObjectFileMachO::
666SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000667 Mangler *Mang, const TargetMachine &TM) const {
668
Eric Christopher02b46bc2010-05-25 21:28:50 +0000669 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000670 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000671 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000672
673 if (Kind.isText())
674 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
675
676 // If this is weak/linkonce, put this in a coalescable section, either in text
677 // or data depending on if it is writable.
678 if (GV->isWeakForLinker()) {
679 if (Kind.isReadOnly())
680 return ConstTextCoalSection;
681 return DataCoalSection;
682 }
683
684 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000685 if (Kind.isMergeable1ByteCString() &&
686 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
687 return CStringSection;
688
689 // Do not put 16-bit arrays in the UString section if they have an
690 // externally visible label, this runs into issues with certain linker
691 // versions.
692 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
693 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
694 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000695
696 if (Kind.isMergeableConst()) {
697 if (Kind.isMergeableConst4())
698 return FourByteConstantSection;
699 if (Kind.isMergeableConst8())
700 return EightByteConstantSection;
701 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
702 return SixteenByteConstantSection;
703 }
704
705 // Otherwise, if it is readonly, but not something we can specially optimize,
706 // just drop it in .const.
707 if (Kind.isReadOnly())
708 return ReadOnlySection;
709
710 // If this is marked const, put it into a const section. But if the dynamic
711 // linker needs to write to it, put it in the data segment.
712 if (Kind.isReadOnlyWithRel())
713 return ConstDataSection;
714
715 // Put zero initialized globals with strong external linkage in the
716 // DATA, __common section with the .zerofill directive.
717 if (Kind.isBSSExtern())
718 return DataCommonSection;
719
720 // Put zero initialized globals with local linkage in __DATA,__bss directive
721 // with the .zerofill directive (aka .lcomm).
722 if (Kind.isBSSLocal())
723 return DataBSSSection;
724
725 // Otherwise, just drop the variable in the normal data section.
726 return DataSection;
727}
728
729const MCSection *
730TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
731 // If this constant requires a relocation, we have to put it in the data
732 // segment, not in the text segment.
733 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
734 return ConstDataSection;
735
736 if (Kind.isMergeableConst4())
737 return FourByteConstantSection;
738 if (Kind.isMergeableConst8())
739 return EightByteConstantSection;
740 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
741 return SixteenByteConstantSection;
742 return ReadOnlySection; // .const
743}
744
745/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
746/// not to emit the UsedDirective for some symbols in llvm.used.
747// FIXME: REMOVE this (rdar://7071300)
748bool TargetLoweringObjectFileMachO::
749shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
750 /// On Darwin, internally linked data beginning with "L" or "l" does not have
751 /// the directive emitted (this occurs in ObjC metadata).
752 if (!GV) return false;
753
Bill Wendling07d31772010-06-29 22:34:52 +0000754 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000755 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
756 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000757 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
758 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000759 MCSymbol *Sym = Mang->getSymbol(GV);
760 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000761 return false;
762 }
763
764 return true;
765}
766
767const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000768getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
769 MachineModuleInfo *MMI, unsigned Encoding,
770 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000771 // The mach-o version of this method defaults to returning a stub reference.
772
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000773 if (Encoding & DW_EH_PE_indirect) {
774 MachineModuleInfoMachO &MachOMMI =
775 MMI->getObjFileInfo<MachineModuleInfoMachO>();
776
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000777 SmallString<128> Name;
778 Mang->getNameWithPrefix(Name, GV, true);
779 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000780
781 // Add information about the stub reference to MachOMMI so that the stub
782 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000783 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000784 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000785 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000786 MCSymbol *Sym = Mang->getSymbol(GV);
787 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000788 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000789
790 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000791 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000792 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000793 }
794
795 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000796 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000797}
798
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000799unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
800 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
801}
802
803unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
804 return DW_EH_PE_pcrel;
805}
806
807unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
808 return DW_EH_PE_pcrel;
809}
810
811unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000812 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000813}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000814
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000815//===----------------------------------------------------------------------===//
816// COFF
817//===----------------------------------------------------------------------===//
818
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000819void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
820 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000821 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000822 TextSection =
823 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000824 COFF::IMAGE_SCN_CNT_CODE |
825 COFF::IMAGE_SCN_MEM_EXECUTE |
826 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000827 SectionKind::getText());
828 DataSection =
829 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000830 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
831 COFF::IMAGE_SCN_MEM_READ |
832 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000833 SectionKind::getDataRel());
834 ReadOnlySection =
835 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000836 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
837 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000838 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000839 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000840 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000841 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
842 COFF::IMAGE_SCN_MEM_READ |
843 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000844 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000845 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000846 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000847 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
848 COFF::IMAGE_SCN_MEM_READ |
849 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000850 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000851
852 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
853 // though it contains relocatable pointers. In PIC mode, this is probably a
854 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
855 // adjusted or this should be a data section.
856 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000857 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000858 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
859 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000860 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000861 EHFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000862 getContext().getCOFFSection(".eh_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000863 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
864 COFF::IMAGE_SCN_MEM_READ |
865 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000866 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000867
868 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000869 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000870 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000871 COFF::IMAGE_SCN_MEM_DISCARDABLE |
872 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000873 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000874 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000875 getContext().getCOFFSection(".debug_info",
Daniel Dunbar94610582010-07-01 20:07:24 +0000876 COFF::IMAGE_SCN_MEM_DISCARDABLE |
877 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000878 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000879 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000880 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000881 COFF::IMAGE_SCN_MEM_DISCARDABLE |
882 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000883 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000884 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000885 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000886 COFF::IMAGE_SCN_MEM_DISCARDABLE |
887 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000888 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000889 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000890 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +0000891 COFF::IMAGE_SCN_MEM_DISCARDABLE |
892 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000893 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000894 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000895 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +0000896 COFF::IMAGE_SCN_MEM_DISCARDABLE |
897 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000898 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000899 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000900 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +0000901 COFF::IMAGE_SCN_MEM_DISCARDABLE |
902 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000903 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000904 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000905 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000906 COFF::IMAGE_SCN_MEM_DISCARDABLE |
907 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000908 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000909 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000910 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000911 COFF::IMAGE_SCN_MEM_DISCARDABLE |
912 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000913 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000914 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000915 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000916 COFF::IMAGE_SCN_MEM_DISCARDABLE |
917 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000918 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000919 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000920 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000921 COFF::IMAGE_SCN_MEM_DISCARDABLE |
922 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000923 SectionKind::getMetadata());
924
925 DrectveSection =
926 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000927 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000928 SectionKind::getMetadata());
929}
930
931static unsigned
932getCOFFSectionFlags(SectionKind K) {
933 unsigned Flags = 0;
934
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000935 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000936 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000937 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000938 else if (K.isText())
939 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000940 COFF::IMAGE_SCN_MEM_EXECUTE |
941 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000942 else if (K.isBSS ())
943 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000944 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
945 COFF::IMAGE_SCN_MEM_READ |
946 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000947 else if (K.isReadOnly())
948 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000949 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
950 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000951 else if (K.isWriteable())
952 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000953 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
954 COFF::IMAGE_SCN_MEM_READ |
955 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000956
957 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000958}
959
960const MCSection *TargetLoweringObjectFileCOFF::
961getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
962 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000963 return getContext().getCOFFSection(GV->getSection(),
964 getCOFFSectionFlags(Kind),
965 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000966}
967
968static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
969 if (Kind.isText())
970 return ".text$linkonce";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000971 if (Kind.isBSS ())
972 return ".bss$linkonce";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000973 if (Kind.isWriteable())
974 return ".data$linkonce";
975 return ".rdata$linkonce";
976}
977
978
979const MCSection *TargetLoweringObjectFileCOFF::
980SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
981 Mangler *Mang, const TargetMachine &TM) const {
982 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
983
984 // If this global is linkonce/weak and the target handles this by emitting it
985 // into a 'uniqued' section name, create and return the section now.
986 if (GV->isWeakForLinker()) {
987 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
988 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000989 MCSymbol *Sym = Mang->getSymbol(GV);
990 Name.append(Sym->getName().begin(), Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +0000991
992 unsigned Characteristics = getCOFFSectionFlags(Kind);
993
Daniel Dunbar94610582010-07-01 20:07:24 +0000994 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000995
996 return getContext().getCOFFSection(Name.str(), Characteristics,
Daniel Dunbar94610582010-07-01 20:07:24 +0000997 COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000998 }
999
1000 if (Kind.isText())
1001 return getTextSection();
1002
1003 return getDataSection();
1004}
1005