blob: 0f36d4296f6435520a335fa40927bc310c3e596e [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"
Chris Lattner8048ebe2010-09-27 06:44:54 +000036#include "llvm/ADT/Triple.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000037using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000038using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000039
40//===----------------------------------------------------------------------===//
41// ELF
42//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000043
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000044void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
45 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000046 TargetLoweringObjectFile::Initialize(Ctx, TM);
47
48 BSSSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000049 getContext().getELFSection(".bss", MCSectionELF::SHT_NOBITS,
50 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
51 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000052
53 TextSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000054 getContext().getELFSection(".text", MCSectionELF::SHT_PROGBITS,
55 MCSectionELF::SHF_EXECINSTR |
56 MCSectionELF::SHF_ALLOC,
57 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000058
59 DataSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000060 getContext().getELFSection(".data", MCSectionELF::SHT_PROGBITS,
61 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
62 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000063
64 ReadOnlySection =
Chris Lattner287df1b2010-04-08 21:34:17 +000065 getContext().getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
66 MCSectionELF::SHF_ALLOC,
67 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000068
69 TLSDataSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000070 getContext().getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
71 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
72 MCSectionELF::SHF_WRITE,
73 SectionKind::getThreadData());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000074
75 TLSBSSSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000076 getContext().getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
77 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
78 MCSectionELF::SHF_WRITE,
79 SectionKind::getThreadBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000080
81 DataRelSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000082 getContext().getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
83 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
84 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000085
86 DataRelLocalSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000087 getContext().getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
88 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
89 SectionKind::getDataRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000090
91 DataRelROSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000092 getContext().getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
93 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
94 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000095
96 DataRelROLocalSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000097 getContext().getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
98 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
99 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000100
101 MergeableConst4Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000102 getContext().getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
103 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
104 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000105
106 MergeableConst8Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000107 getContext().getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
109 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000110
111 MergeableConst16Section =
Chris Lattner287df1b2010-04-08 21:34:17 +0000112 getContext().getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
113 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
114 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000115
116 StaticCtorSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000117 getContext().getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
118 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
119 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000120
121 StaticDtorSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000122 getContext().getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
123 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
124 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000125
126 // Exception Handling Sections.
127
128 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
129 // it contains relocatable pointers. In PIC mode, this is probably a big
130 // runtime hit for C++ apps. Either the contents of the LSDA need to be
131 // adjusted or this should be a data section.
132 LSDASection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000133 getContext().getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
134 MCSectionELF::SHF_ALLOC,
135 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000136 EHFrameSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000137 getContext().getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
138 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
139 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000140
141 // Debug Info Sections.
142 DwarfAbbrevSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000143 getContext().getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
144 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000145 DwarfInfoSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000146 getContext().getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
147 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000148 DwarfLineSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000149 getContext().getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
150 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000151 DwarfFrameSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000152 getContext().getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
153 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000154 DwarfPubNamesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000155 getContext().getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
156 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000157 DwarfPubTypesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000158 getContext().getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
159 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000160 DwarfStrSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000161 getContext().getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
162 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000163 DwarfLocSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000164 getContext().getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
165 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000166 DwarfARangesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000167 getContext().getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
168 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000169 DwarfRangesSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000170 getContext().getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
171 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000172 DwarfMacroInfoSection =
Chris Lattner287df1b2010-04-08 21:34:17 +0000173 getContext().getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
174 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000175}
176
177
178static SectionKind
179getELFKindForNamedSection(StringRef Name, SectionKind K) {
180 if (Name.empty() || Name[0] != '.') return K;
181
182 // Some lame default implementation based on some magic section names.
183 if (Name == ".bss" ||
184 Name.startswith(".bss.") ||
185 Name.startswith(".gnu.linkonce.b.") ||
186 Name.startswith(".llvm.linkonce.b.") ||
187 Name == ".sbss" ||
188 Name.startswith(".sbss.") ||
189 Name.startswith(".gnu.linkonce.sb.") ||
190 Name.startswith(".llvm.linkonce.sb."))
191 return SectionKind::getBSS();
192
193 if (Name == ".tdata" ||
194 Name.startswith(".tdata.") ||
195 Name.startswith(".gnu.linkonce.td.") ||
196 Name.startswith(".llvm.linkonce.td."))
197 return SectionKind::getThreadData();
198
199 if (Name == ".tbss" ||
200 Name.startswith(".tbss.") ||
201 Name.startswith(".gnu.linkonce.tb.") ||
202 Name.startswith(".llvm.linkonce.tb."))
203 return SectionKind::getThreadBSS();
204
205 return K;
206}
207
208
209static unsigned getELFSectionType(StringRef Name, SectionKind K) {
210
211 if (Name == ".init_array")
212 return MCSectionELF::SHT_INIT_ARRAY;
213
214 if (Name == ".fini_array")
215 return MCSectionELF::SHT_FINI_ARRAY;
216
217 if (Name == ".preinit_array")
218 return MCSectionELF::SHT_PREINIT_ARRAY;
219
220 if (K.isBSS() || K.isThreadBSS())
221 return MCSectionELF::SHT_NOBITS;
222
223 return MCSectionELF::SHT_PROGBITS;
224}
225
226
227static unsigned
228getELFSectionFlags(SectionKind K) {
229 unsigned Flags = 0;
230
231 if (!K.isMetadata())
232 Flags |= MCSectionELF::SHF_ALLOC;
233
234 if (K.isText())
235 Flags |= MCSectionELF::SHF_EXECINSTR;
236
237 if (K.isWriteable())
238 Flags |= MCSectionELF::SHF_WRITE;
239
240 if (K.isThreadLocal())
241 Flags |= MCSectionELF::SHF_TLS;
242
243 // K.isMergeableConst() is left out to honour PR4650
244 if (K.isMergeableCString() || K.isMergeableConst4() ||
245 K.isMergeableConst8() || K.isMergeableConst16())
246 Flags |= MCSectionELF::SHF_MERGE;
247
248 if (K.isMergeableCString())
249 Flags |= MCSectionELF::SHF_STRINGS;
250
251 return Flags;
252}
253
254
255const MCSection *TargetLoweringObjectFileELF::
256getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
257 Mangler *Mang, const TargetMachine &TM) const {
258 StringRef SectionName = GV->getSection();
259
260 // Infer section flags from the section name if we can.
261 Kind = getELFKindForNamedSection(SectionName, Kind);
262
Chris Lattner287df1b2010-04-08 21:34:17 +0000263 return getContext().getELFSection(SectionName,
264 getELFSectionType(SectionName, Kind),
265 getELFSectionFlags(Kind), Kind, true);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000266}
267
268static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
269 if (Kind.isText()) return ".gnu.linkonce.t.";
270 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
271
272 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
273 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
274
275 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
276 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
277 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
278 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
279
280 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
281 return ".gnu.linkonce.d.rel.ro.";
282}
283
Chris Lattner43ac7212010-04-13 00:36:43 +0000284/// getSectionPrefixForGlobal - Return the section prefix name used by options
285/// FunctionsSections and DataSections.
286static const char *getSectionPrefixForGlobal(SectionKind Kind) {
287 if (Kind.isText()) return ".text.";
288 if (Kind.isReadOnly()) return ".rodata.";
289
290 if (Kind.isThreadData()) return ".tdata.";
291 if (Kind.isThreadBSS()) return ".tbss.";
292
293 if (Kind.isDataNoRel()) return ".data.";
294 if (Kind.isDataRelLocal()) return ".data.rel.local.";
295 if (Kind.isDataRel()) return ".data.rel.";
296 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
297
298 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
299 return ".data.rel.ro.";
300}
301
302
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000303const MCSection *TargetLoweringObjectFileELF::
304SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
305 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000306 // If we have -ffunction-section or -fdata-section then we should emit the
307 // global value to a uniqued section specifically for it.
308 bool EmitUniquedSection;
309 if (Kind.isText())
310 EmitUniquedSection = TM.getFunctionSections();
311 else
312 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000313
314 // If this global is linkonce/weak and the target handles this by emitting it
315 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000316 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
317 !Kind.isCommon() && !Kind.isBSS()) {
318 const char *Prefix;
319 if (GV->isWeakForLinker())
320 Prefix = getSectionPrefixForUniqueGlobal(Kind);
321 else {
322 assert(EmitUniquedSection);
323 Prefix = getSectionPrefixForGlobal(Kind);
324 }
325
Chris Lattner4c6741f2010-03-15 20:37:38 +0000326 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
327 MCSymbol *Sym = Mang->getSymbol(GV);
328 Name.append(Sym->getName().begin(), Sym->getName().end());
Chris Lattner287df1b2010-04-08 21:34:17 +0000329 return getContext().getELFSection(Name.str(),
330 getELFSectionType(Name.str(), Kind),
331 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000332 }
333
334 if (Kind.isText()) return TextSection;
335
336 if (Kind.isMergeable1ByteCString() ||
337 Kind.isMergeable2ByteCString() ||
338 Kind.isMergeable4ByteCString()) {
339
340 // We also need alignment here.
341 // FIXME: this is getting the alignment of the character, not the
342 // alignment of the global!
343 unsigned Align =
344 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
345
346 const char *SizeSpec = ".rodata.str1.";
347 if (Kind.isMergeable2ByteCString())
348 SizeSpec = ".rodata.str2.";
349 else if (Kind.isMergeable4ByteCString())
350 SizeSpec = ".rodata.str4.";
351 else
352 assert(Kind.isMergeable1ByteCString() && "unknown string width");
353
354
355 std::string Name = SizeSpec + utostr(Align);
Chris Lattner287df1b2010-04-08 21:34:17 +0000356 return getContext().getELFSection(Name, MCSectionELF::SHT_PROGBITS,
357 MCSectionELF::SHF_ALLOC |
358 MCSectionELF::SHF_MERGE |
359 MCSectionELF::SHF_STRINGS,
360 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000361 }
362
363 if (Kind.isMergeableConst()) {
364 if (Kind.isMergeableConst4() && MergeableConst4Section)
365 return MergeableConst4Section;
366 if (Kind.isMergeableConst8() && MergeableConst8Section)
367 return MergeableConst8Section;
368 if (Kind.isMergeableConst16() && MergeableConst16Section)
369 return MergeableConst16Section;
370 return ReadOnlySection; // .const
371 }
372
373 if (Kind.isReadOnly()) return ReadOnlySection;
374
375 if (Kind.isThreadData()) return TLSDataSection;
376 if (Kind.isThreadBSS()) return TLSBSSSection;
377
378 // Note: we claim that common symbols are put in BSSSection, but they are
379 // really emitted with the magic .comm directive, which creates a symbol table
380 // entry but not a section.
381 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
382
383 if (Kind.isDataNoRel()) return DataSection;
384 if (Kind.isDataRelLocal()) return DataRelLocalSection;
385 if (Kind.isDataRel()) return DataRelSection;
386 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
387
388 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
389 return DataRelROSection;
390}
391
392/// getSectionForConstant - Given a mergeable constant with the
393/// specified size and relocation information, return a section that it
394/// should be placed in.
395const MCSection *TargetLoweringObjectFileELF::
396getSectionForConstant(SectionKind Kind) const {
397 if (Kind.isMergeableConst4() && MergeableConst4Section)
398 return MergeableConst4Section;
399 if (Kind.isMergeableConst8() && MergeableConst8Section)
400 return MergeableConst8Section;
401 if (Kind.isMergeableConst16() && MergeableConst16Section)
402 return MergeableConst16Section;
403 if (Kind.isReadOnly())
404 return ReadOnlySection;
405
406 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
407 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
408 return DataRelROSection;
409}
410
411const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000412getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
413 MachineModuleInfo *MMI,
414 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000415
416 if (Encoding & dwarf::DW_EH_PE_indirect) {
417 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
418
419 SmallString<128> Name;
420 Mang->getNameWithPrefix(Name, GV, true);
421 Name += ".DW.stub";
422
423 // Add information about the stub reference to ELFMMI so that the stub
424 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000425 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000426 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000427 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000428 MCSymbol *Sym = Mang->getSymbol(GV);
429 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000430 }
431
432 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000433 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000434 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000435 }
436
437 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000438 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000439}
440
441//===----------------------------------------------------------------------===//
442// MachO
443//===----------------------------------------------------------------------===//
444
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000445void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
446 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000447 IsFunctionEHSymbolGlobal = true;
448 IsFunctionEHFrameSymbolPrivate = false;
449 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000450
451 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
452 if (T.getOS() == Triple::Darwin) {
Bill Wendling4edab1a2010-09-28 22:36:56 +0000453 unsigned MajNum = T.getDarwinMajorNumber();
454 if (MajNum == 7 || MajNum == 8) // 10.3 Panther, 10.4 Tiger
Chris Lattner8048ebe2010-09-27 06:44:54 +0000455 CommDirectiveSupportsAlignment = false;
Bill Wendling4edab1a2010-09-28 22:36:56 +0000456 if (MajNum > 9) // 10.6 SnowLeopard
457 IsFunctionEHSymbolGlobal = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000458 }
Bill Wendling4edab1a2010-09-28 22:36:56 +0000459
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000460 TargetLoweringObjectFile::Initialize(Ctx, TM);
461
462 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000463 = getContext().getMachOSection("__TEXT", "__text",
464 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
465 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000466 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000467 = getContext().getMachOSection("__DATA", "__data", 0,
468 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000469
Eric Christopher423c9e32010-05-17 21:02:07 +0000470 TLSDataSection // .tdata
471 = getContext().getMachOSection("__DATA", "__thread_data",
472 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
473 SectionKind::getDataRel());
474 TLSBSSSection // .tbss
475 = getContext().getMachOSection("__DATA", "__thread_bss",
476 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
477 SectionKind::getThreadBSS());
478
479 // TODO: Verify datarel below.
480 TLSTLVSection // .tlv
481 = getContext().getMachOSection("__DATA", "__thread_vars",
482 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
483 SectionKind::getDataRel());
Eric Christopherc6177a42010-05-17 22:53:55 +0000484
485 TLSThreadInitSection
486 = getContext().getMachOSection("__DATA", "__thread_init",
487 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
488 SectionKind::getDataRel());
489
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000490 CStringSection // .cstring
Chris Lattner22772212010-04-08 20:40:11 +0000491 = getContext().getMachOSection("__TEXT", "__cstring",
492 MCSectionMachO::S_CSTRING_LITERALS,
493 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000494 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000495 = getContext().getMachOSection("__TEXT","__ustring", 0,
496 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000497 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000498 = getContext().getMachOSection("__TEXT", "__literal4",
499 MCSectionMachO::S_4BYTE_LITERALS,
500 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000501 EightByteConstantSection // .literal8
Chris Lattner22772212010-04-08 20:40:11 +0000502 = getContext().getMachOSection("__TEXT", "__literal8",
503 MCSectionMachO::S_8BYTE_LITERALS,
504 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000505
506 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
507 // to using it in -static mode.
508 SixteenByteConstantSection = 0;
509 if (TM.getRelocationModel() != Reloc::Static &&
510 TM.getTargetData()->getPointerSize() == 32)
511 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000512 getContext().getMachOSection("__TEXT", "__literal16",
513 MCSectionMachO::S_16BYTE_LITERALS,
514 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000515
516 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000517 = getContext().getMachOSection("__TEXT", "__const", 0,
518 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000519
520 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000521 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
522 MCSectionMachO::S_COALESCED |
523 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
524 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000525 ConstTextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000526 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000527 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000528 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000529 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000530 = getContext().getMachOSection("__DATA", "__const", 0,
531 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000532 DataCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000533 = getContext().getMachOSection("__DATA","__datacoal_nt",
534 MCSectionMachO::S_COALESCED,
535 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000536 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000537 = getContext().getMachOSection("__DATA","__common",
538 MCSectionMachO::S_ZEROFILL,
539 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000540 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000541 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
542 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000543
544
545 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000546 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
547 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
548 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000549 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000550 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
551 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
552 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000553
554 if (TM.getRelocationModel() == Reloc::Static) {
555 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000556 = getContext().getMachOSection("__TEXT", "__constructor", 0,
557 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000558 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000559 = getContext().getMachOSection("__TEXT", "__destructor", 0,
560 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000561 } else {
562 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000563 = getContext().getMachOSection("__DATA", "__mod_init_func",
564 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
565 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000566 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000567 = getContext().getMachOSection("__DATA", "__mod_term_func",
568 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
569 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000570 }
571
572 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000573 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
574 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000575 EHFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000576 getContext().getMachOSection("__TEXT", "__eh_frame",
577 MCSectionMachO::S_COALESCED |
578 MCSectionMachO::S_ATTR_NO_TOC |
579 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
580 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
581 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000582
583 // Debug Information.
584 DwarfAbbrevSection =
Chris Lattner22772212010-04-08 20:40:11 +0000585 getContext().getMachOSection("__DWARF", "__debug_abbrev",
586 MCSectionMachO::S_ATTR_DEBUG,
587 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000588 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000589 getContext().getMachOSection("__DWARF", "__debug_info",
590 MCSectionMachO::S_ATTR_DEBUG,
591 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000592 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000593 getContext().getMachOSection("__DWARF", "__debug_line",
594 MCSectionMachO::S_ATTR_DEBUG,
595 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000596 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000597 getContext().getMachOSection("__DWARF", "__debug_frame",
598 MCSectionMachO::S_ATTR_DEBUG,
599 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000600 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000601 getContext().getMachOSection("__DWARF", "__debug_pubnames",
602 MCSectionMachO::S_ATTR_DEBUG,
603 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000604 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000605 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
606 MCSectionMachO::S_ATTR_DEBUG,
607 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000608 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000609 getContext().getMachOSection("__DWARF", "__debug_str",
610 MCSectionMachO::S_ATTR_DEBUG,
611 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000612 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000613 getContext().getMachOSection("__DWARF", "__debug_loc",
614 MCSectionMachO::S_ATTR_DEBUG,
615 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000616 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000617 getContext().getMachOSection("__DWARF", "__debug_aranges",
618 MCSectionMachO::S_ATTR_DEBUG,
619 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000620 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000621 getContext().getMachOSection("__DWARF", "__debug_ranges",
622 MCSectionMachO::S_ATTR_DEBUG,
623 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000624 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000625 getContext().getMachOSection("__DWARF", "__debug_macinfo",
626 MCSectionMachO::S_ATTR_DEBUG,
627 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000628 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000629 getContext().getMachOSection("__DWARF", "__debug_inlined",
630 MCSectionMachO::S_ATTR_DEBUG,
631 SectionKind::getMetadata());
Eric Christopher8116ca52010-05-22 00:10:22 +0000632
633 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000634}
635
636const MCSection *TargetLoweringObjectFileMachO::
637getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
638 Mangler *Mang, const TargetMachine &TM) const {
639 // Parse the section specifier and create it if valid.
640 StringRef Segment, Section;
641 unsigned TAA, StubSize;
642 std::string ErrorCode =
643 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
644 TAA, StubSize);
645 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000646 // If invalid, report the error with report_fatal_error.
647 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000648 "' has an invalid section specifier '" + GV->getSection()+
649 "': " + ErrorCode + ".");
650 // Fall back to dropping it into the data section.
651 return DataSection;
652 }
653
654 // Get the section.
655 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000656 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000657
658 // Okay, now that we got the section, verify that the TAA & StubSize agree.
659 // If the user declared multiple globals with different section flags, we need
660 // to reject it here.
661 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000662 // If invalid, report the error with report_fatal_error.
663 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000664 "' section type or attributes does not match previous"
665 " section specifier");
666 }
667
668 return S;
669}
670
671const MCSection *TargetLoweringObjectFileMachO::
672SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000673 Mangler *Mang, const TargetMachine &TM) const {
674
Eric Christopher02b46bc2010-05-25 21:28:50 +0000675 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000676 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000677 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000678
679 if (Kind.isText())
680 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
681
682 // If this is weak/linkonce, put this in a coalescable section, either in text
683 // or data depending on if it is writable.
684 if (GV->isWeakForLinker()) {
685 if (Kind.isReadOnly())
686 return ConstTextCoalSection;
687 return DataCoalSection;
688 }
689
690 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000691 if (Kind.isMergeable1ByteCString() &&
692 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
693 return CStringSection;
694
695 // Do not put 16-bit arrays in the UString section if they have an
696 // externally visible label, this runs into issues with certain linker
697 // versions.
698 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
699 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
700 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000701
702 if (Kind.isMergeableConst()) {
703 if (Kind.isMergeableConst4())
704 return FourByteConstantSection;
705 if (Kind.isMergeableConst8())
706 return EightByteConstantSection;
707 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
708 return SixteenByteConstantSection;
709 }
710
711 // Otherwise, if it is readonly, but not something we can specially optimize,
712 // just drop it in .const.
713 if (Kind.isReadOnly())
714 return ReadOnlySection;
715
716 // If this is marked const, put it into a const section. But if the dynamic
717 // linker needs to write to it, put it in the data segment.
718 if (Kind.isReadOnlyWithRel())
719 return ConstDataSection;
720
721 // Put zero initialized globals with strong external linkage in the
722 // DATA, __common section with the .zerofill directive.
723 if (Kind.isBSSExtern())
724 return DataCommonSection;
725
726 // Put zero initialized globals with local linkage in __DATA,__bss directive
727 // with the .zerofill directive (aka .lcomm).
728 if (Kind.isBSSLocal())
729 return DataBSSSection;
730
731 // Otherwise, just drop the variable in the normal data section.
732 return DataSection;
733}
734
735const MCSection *
736TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
737 // If this constant requires a relocation, we have to put it in the data
738 // segment, not in the text segment.
739 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
740 return ConstDataSection;
741
742 if (Kind.isMergeableConst4())
743 return FourByteConstantSection;
744 if (Kind.isMergeableConst8())
745 return EightByteConstantSection;
746 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
747 return SixteenByteConstantSection;
748 return ReadOnlySection; // .const
749}
750
751/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
752/// not to emit the UsedDirective for some symbols in llvm.used.
753// FIXME: REMOVE this (rdar://7071300)
754bool TargetLoweringObjectFileMachO::
755shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
756 /// On Darwin, internally linked data beginning with "L" or "l" does not have
757 /// the directive emitted (this occurs in ObjC metadata).
758 if (!GV) return false;
759
Bill Wendling07d31772010-06-29 22:34:52 +0000760 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000761 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
762 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000763 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
764 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000765 MCSymbol *Sym = Mang->getSymbol(GV);
766 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000767 return false;
768 }
769
770 return true;
771}
772
773const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000774getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
775 MachineModuleInfo *MMI, unsigned Encoding,
776 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000777 // The mach-o version of this method defaults to returning a stub reference.
778
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000779 if (Encoding & DW_EH_PE_indirect) {
780 MachineModuleInfoMachO &MachOMMI =
781 MMI->getObjFileInfo<MachineModuleInfoMachO>();
782
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000783 SmallString<128> Name;
784 Mang->getNameWithPrefix(Name, GV, true);
785 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000786
787 // Add information about the stub reference to MachOMMI so that the stub
788 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000789 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000790 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000791 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000792 MCSymbol *Sym = Mang->getSymbol(GV);
793 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000794 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000795
796 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000797 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000798 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000799 }
800
801 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000802 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000803}
804
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000805unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
806 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
807}
808
809unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
810 return DW_EH_PE_pcrel;
811}
812
813unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
814 return DW_EH_PE_pcrel;
815}
816
817unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000818 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000819}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000820
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000821//===----------------------------------------------------------------------===//
822// COFF
823//===----------------------------------------------------------------------===//
824
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000825void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
826 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000827 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000828 TextSection =
829 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000830 COFF::IMAGE_SCN_CNT_CODE |
831 COFF::IMAGE_SCN_MEM_EXECUTE |
832 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000833 SectionKind::getText());
834 DataSection =
835 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000836 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
837 COFF::IMAGE_SCN_MEM_READ |
838 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000839 SectionKind::getDataRel());
840 ReadOnlySection =
841 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000842 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
843 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000844 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000845 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000846 getContext().getCOFFSection(".ctors",
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 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000852 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000853 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
854 COFF::IMAGE_SCN_MEM_READ |
855 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000856 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000857
858 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
859 // though it contains relocatable pointers. In PIC mode, this is probably a
860 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
861 // adjusted or this should be a data section.
862 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000863 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000864 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
865 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000866 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000867 EHFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000868 getContext().getCOFFSection(".eh_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000869 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
870 COFF::IMAGE_SCN_MEM_READ |
871 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000872 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000873
874 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000875 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000876 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000877 COFF::IMAGE_SCN_MEM_DISCARDABLE |
878 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000879 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000880 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000881 getContext().getCOFFSection(".debug_info",
Daniel Dunbar94610582010-07-01 20:07:24 +0000882 COFF::IMAGE_SCN_MEM_DISCARDABLE |
883 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000884 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000885 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000886 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000887 COFF::IMAGE_SCN_MEM_DISCARDABLE |
888 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000889 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000890 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000891 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000892 COFF::IMAGE_SCN_MEM_DISCARDABLE |
893 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000894 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000895 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000896 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +0000897 COFF::IMAGE_SCN_MEM_DISCARDABLE |
898 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000899 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000900 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000901 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +0000902 COFF::IMAGE_SCN_MEM_DISCARDABLE |
903 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000904 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000905 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000906 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +0000907 COFF::IMAGE_SCN_MEM_DISCARDABLE |
908 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000909 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000910 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000911 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000912 COFF::IMAGE_SCN_MEM_DISCARDABLE |
913 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000914 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000915 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000916 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000917 COFF::IMAGE_SCN_MEM_DISCARDABLE |
918 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000919 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000920 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000921 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000922 COFF::IMAGE_SCN_MEM_DISCARDABLE |
923 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000924 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000925 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000926 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000927 COFF::IMAGE_SCN_MEM_DISCARDABLE |
928 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000929 SectionKind::getMetadata());
930
931 DrectveSection =
932 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000933 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000934 SectionKind::getMetadata());
935}
936
937static unsigned
938getCOFFSectionFlags(SectionKind K) {
939 unsigned Flags = 0;
940
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000941 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000942 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000943 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000944 else if (K.isText())
945 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000946 COFF::IMAGE_SCN_MEM_EXECUTE |
947 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000948 else if (K.isBSS ())
949 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000950 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
951 COFF::IMAGE_SCN_MEM_READ |
952 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000953 else if (K.isReadOnly())
954 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000955 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
956 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000957 else if (K.isWriteable())
958 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000959 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
960 COFF::IMAGE_SCN_MEM_READ |
961 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000962
963 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000964}
965
966const MCSection *TargetLoweringObjectFileCOFF::
967getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
968 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000969 return getContext().getCOFFSection(GV->getSection(),
970 getCOFFSectionFlags(Kind),
971 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000972}
973
974static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
975 if (Kind.isText())
976 return ".text$linkonce";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000977 if (Kind.isBSS ())
978 return ".bss$linkonce";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000979 if (Kind.isWriteable())
980 return ".data$linkonce";
981 return ".rdata$linkonce";
982}
983
984
985const MCSection *TargetLoweringObjectFileCOFF::
986SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
987 Mangler *Mang, const TargetMachine &TM) const {
988 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
989
990 // If this global is linkonce/weak and the target handles this by emitting it
991 // into a 'uniqued' section name, create and return the section now.
992 if (GV->isWeakForLinker()) {
993 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
994 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000995 MCSymbol *Sym = Mang->getSymbol(GV);
996 Name.append(Sym->getName().begin(), Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +0000997
998 unsigned Characteristics = getCOFFSectionFlags(Kind);
999
Daniel Dunbar94610582010-07-01 20:07:24 +00001000 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001001
1002 return getContext().getCOFFSection(Name.str(), Characteristics,
Daniel Dunbar94610582010-07-01 20:07:24 +00001003 COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001004 }
1005
1006 if (Kind.isText())
1007 return getTextSection();
1008
1009 return getDataSection();
1010}
1011