blob: 6e94a13a89b8a64af57ab83369aef4b19d4d9641 [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();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000311 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000312 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) {
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000447 // _foo.eh symbols are currently always exported so that the linker knows
448 // about them. This is not necessary on 10.6 and later, but it
449 // doesn't hurt anything.
450 // FIXME: I need to get this from Triple.
Chris Lattner09d53fe2010-03-10 07:20:42 +0000451 IsFunctionEHSymbolGlobal = true;
452 IsFunctionEHFrameSymbolPrivate = false;
453 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000454
455 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
456 if (T.getOS() == Triple::Darwin) {
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000457 switch (T.getDarwinMajorNumber()) {
458 case 7: // 10.3 Panther.
459 case 8: // 10.4 Tiger.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000460 CommDirectiveSupportsAlignment = false;
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000461 break;
462 case 9: // 10.5 Leopard.
463 case 10: // 10.6 SnowLeopard.
464 break;
465 }
Chris Lattner8048ebe2010-09-27 06:44:54 +0000466 }
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000467
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000468 TargetLoweringObjectFile::Initialize(Ctx, TM);
469
470 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000471 = getContext().getMachOSection("__TEXT", "__text",
472 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
473 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000474 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000475 = getContext().getMachOSection("__DATA", "__data", 0,
476 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000477
Eric Christopher423c9e32010-05-17 21:02:07 +0000478 TLSDataSection // .tdata
479 = getContext().getMachOSection("__DATA", "__thread_data",
480 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
481 SectionKind::getDataRel());
482 TLSBSSSection // .tbss
483 = getContext().getMachOSection("__DATA", "__thread_bss",
484 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
485 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000486
Eric Christopher423c9e32010-05-17 21:02:07 +0000487 // TODO: Verify datarel below.
488 TLSTLVSection // .tlv
489 = getContext().getMachOSection("__DATA", "__thread_vars",
490 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
491 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000492
Eric Christopherc6177a42010-05-17 22:53:55 +0000493 TLSThreadInitSection
494 = getContext().getMachOSection("__DATA", "__thread_init",
495 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
496 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000497
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000498 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000499 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000500 MCSectionMachO::S_CSTRING_LITERALS,
501 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000502 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000503 = getContext().getMachOSection("__TEXT","__ustring", 0,
504 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000505 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000506 = getContext().getMachOSection("__TEXT", "__literal4",
507 MCSectionMachO::S_4BYTE_LITERALS,
508 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000509 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000510 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000511 MCSectionMachO::S_8BYTE_LITERALS,
512 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000513
514 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
515 // to using it in -static mode.
516 SixteenByteConstantSection = 0;
517 if (TM.getRelocationModel() != Reloc::Static &&
518 TM.getTargetData()->getPointerSize() == 32)
519 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000520 getContext().getMachOSection("__TEXT", "__literal16",
521 MCSectionMachO::S_16BYTE_LITERALS,
522 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000523
524 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000525 = getContext().getMachOSection("__TEXT", "__const", 0,
526 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000527
528 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000529 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
530 MCSectionMachO::S_COALESCED |
531 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
532 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000533 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000534 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000535 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000536 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000537 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000538 = getContext().getMachOSection("__DATA", "__const", 0,
539 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000540 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000541 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000542 MCSectionMachO::S_COALESCED,
543 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000544 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000545 = getContext().getMachOSection("__DATA","__common",
546 MCSectionMachO::S_ZEROFILL,
547 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000548 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000549 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
550 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000551
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000552
553 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000554 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
555 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
556 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000557 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000558 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
559 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
560 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000561
562 if (TM.getRelocationModel() == Reloc::Static) {
563 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000564 = getContext().getMachOSection("__TEXT", "__constructor", 0,
565 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000566 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000567 = getContext().getMachOSection("__TEXT", "__destructor", 0,
568 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000569 } else {
570 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000571 = getContext().getMachOSection("__DATA", "__mod_init_func",
572 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
573 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000574 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000575 = getContext().getMachOSection("__DATA", "__mod_term_func",
576 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
577 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000578 }
579
580 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000581 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
582 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000583 EHFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000584 getContext().getMachOSection("__TEXT", "__eh_frame",
585 MCSectionMachO::S_COALESCED |
586 MCSectionMachO::S_ATTR_NO_TOC |
587 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
588 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
589 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000590
591 // Debug Information.
592 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000593 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000594 MCSectionMachO::S_ATTR_DEBUG,
595 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000596 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000597 getContext().getMachOSection("__DWARF", "__debug_info",
598 MCSectionMachO::S_ATTR_DEBUG,
599 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000600 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000601 getContext().getMachOSection("__DWARF", "__debug_line",
602 MCSectionMachO::S_ATTR_DEBUG,
603 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000604 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000605 getContext().getMachOSection("__DWARF", "__debug_frame",
606 MCSectionMachO::S_ATTR_DEBUG,
607 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000608 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000609 getContext().getMachOSection("__DWARF", "__debug_pubnames",
610 MCSectionMachO::S_ATTR_DEBUG,
611 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000612 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000613 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
614 MCSectionMachO::S_ATTR_DEBUG,
615 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000616 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000617 getContext().getMachOSection("__DWARF", "__debug_str",
618 MCSectionMachO::S_ATTR_DEBUG,
619 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000620 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000621 getContext().getMachOSection("__DWARF", "__debug_loc",
622 MCSectionMachO::S_ATTR_DEBUG,
623 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000624 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000625 getContext().getMachOSection("__DWARF", "__debug_aranges",
626 MCSectionMachO::S_ATTR_DEBUG,
627 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000628 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000629 getContext().getMachOSection("__DWARF", "__debug_ranges",
630 MCSectionMachO::S_ATTR_DEBUG,
631 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000632 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000633 getContext().getMachOSection("__DWARF", "__debug_macinfo",
634 MCSectionMachO::S_ATTR_DEBUG,
635 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000636 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000637 getContext().getMachOSection("__DWARF", "__debug_inlined",
638 MCSectionMachO::S_ATTR_DEBUG,
639 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000640
Eric Christopher8116ca52010-05-22 00:10:22 +0000641 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000642}
643
644const MCSection *TargetLoweringObjectFileMachO::
645getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
646 Mangler *Mang, const TargetMachine &TM) const {
647 // Parse the section specifier and create it if valid.
648 StringRef Segment, Section;
649 unsigned TAA, StubSize;
650 std::string ErrorCode =
651 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
652 TAA, StubSize);
653 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000654 // If invalid, report the error with report_fatal_error.
655 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000656 "' has an invalid section specifier '" + GV->getSection()+
657 "': " + ErrorCode + ".");
658 // Fall back to dropping it into the data section.
659 return DataSection;
660 }
661
662 // Get the section.
663 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000664 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000665
666 // Okay, now that we got the section, verify that the TAA & StubSize agree.
667 // If the user declared multiple globals with different section flags, we need
668 // to reject it here.
669 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000670 // If invalid, report the error with report_fatal_error.
671 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000672 "' section type or attributes does not match previous"
673 " section specifier");
674 }
675
676 return S;
677}
678
679const MCSection *TargetLoweringObjectFileMachO::
680SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000681 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000682
Eric Christopher02b46bc2010-05-25 21:28:50 +0000683 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000684 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000685 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000686
687 if (Kind.isText())
688 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
689
690 // If this is weak/linkonce, put this in a coalescable section, either in text
691 // or data depending on if it is writable.
692 if (GV->isWeakForLinker()) {
693 if (Kind.isReadOnly())
694 return ConstTextCoalSection;
695 return DataCoalSection;
696 }
697
698 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000699 if (Kind.isMergeable1ByteCString() &&
700 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
701 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000702
Chris Lattner98f15d22010-03-07 04:28:09 +0000703 // Do not put 16-bit arrays in the UString section if they have an
704 // externally visible label, this runs into issues with certain linker
705 // versions.
706 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
707 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
708 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000709
710 if (Kind.isMergeableConst()) {
711 if (Kind.isMergeableConst4())
712 return FourByteConstantSection;
713 if (Kind.isMergeableConst8())
714 return EightByteConstantSection;
715 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
716 return SixteenByteConstantSection;
717 }
718
719 // Otherwise, if it is readonly, but not something we can specially optimize,
720 // just drop it in .const.
721 if (Kind.isReadOnly())
722 return ReadOnlySection;
723
724 // If this is marked const, put it into a const section. But if the dynamic
725 // linker needs to write to it, put it in the data segment.
726 if (Kind.isReadOnlyWithRel())
727 return ConstDataSection;
728
729 // Put zero initialized globals with strong external linkage in the
730 // DATA, __common section with the .zerofill directive.
731 if (Kind.isBSSExtern())
732 return DataCommonSection;
733
734 // Put zero initialized globals with local linkage in __DATA,__bss directive
735 // with the .zerofill directive (aka .lcomm).
736 if (Kind.isBSSLocal())
737 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000738
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000739 // Otherwise, just drop the variable in the normal data section.
740 return DataSection;
741}
742
743const MCSection *
744TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
745 // If this constant requires a relocation, we have to put it in the data
746 // segment, not in the text segment.
747 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
748 return ConstDataSection;
749
750 if (Kind.isMergeableConst4())
751 return FourByteConstantSection;
752 if (Kind.isMergeableConst8())
753 return EightByteConstantSection;
754 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
755 return SixteenByteConstantSection;
756 return ReadOnlySection; // .const
757}
758
759/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
760/// not to emit the UsedDirective for some symbols in llvm.used.
761// FIXME: REMOVE this (rdar://7071300)
762bool TargetLoweringObjectFileMachO::
763shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
764 /// On Darwin, internally linked data beginning with "L" or "l" does not have
765 /// the directive emitted (this occurs in ObjC metadata).
766 if (!GV) return false;
767
Bill Wendling07d31772010-06-29 22:34:52 +0000768 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000769 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
770 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000771 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
772 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000773 MCSymbol *Sym = Mang->getSymbol(GV);
774 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000775 return false;
776 }
777
778 return true;
779}
780
781const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000782getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
783 MachineModuleInfo *MMI, unsigned Encoding,
784 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000785 // The mach-o version of this method defaults to returning a stub reference.
786
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000787 if (Encoding & DW_EH_PE_indirect) {
788 MachineModuleInfoMachO &MachOMMI =
789 MMI->getObjFileInfo<MachineModuleInfoMachO>();
790
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000791 SmallString<128> Name;
792 Mang->getNameWithPrefix(Name, GV, true);
793 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000794
795 // Add information about the stub reference to MachOMMI so that the stub
796 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000797 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000798 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000799 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000800 MCSymbol *Sym = Mang->getSymbol(GV);
801 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000802 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000803
804 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000805 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000806 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000807 }
808
809 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000810 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000811}
812
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000813unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
814 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
815}
816
817unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
818 return DW_EH_PE_pcrel;
819}
820
821unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
822 return DW_EH_PE_pcrel;
823}
824
825unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000826 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000827}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000828
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000829//===----------------------------------------------------------------------===//
830// COFF
831//===----------------------------------------------------------------------===//
832
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000833void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
834 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000835 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000836 TextSection =
837 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000838 COFF::IMAGE_SCN_CNT_CODE |
839 COFF::IMAGE_SCN_MEM_EXECUTE |
840 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000841 SectionKind::getText());
842 DataSection =
843 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000844 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
845 COFF::IMAGE_SCN_MEM_READ |
846 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000847 SectionKind::getDataRel());
848 ReadOnlySection =
849 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000850 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
851 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000852 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000853 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000854 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000855 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
856 COFF::IMAGE_SCN_MEM_READ |
857 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000858 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000859 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000860 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000861 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
862 COFF::IMAGE_SCN_MEM_READ |
863 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000864 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000865
866 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
867 // though it contains relocatable pointers. In PIC mode, this is probably a
868 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
869 // adjusted or this should be a data section.
870 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000871 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000872 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
873 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000874 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000875 EHFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000876 getContext().getCOFFSection(".eh_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000877 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
878 COFF::IMAGE_SCN_MEM_READ |
879 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000880 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000881
882 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000883 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000884 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000885 COFF::IMAGE_SCN_MEM_DISCARDABLE |
886 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000887 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000888 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000889 getContext().getCOFFSection(".debug_info",
Daniel Dunbar94610582010-07-01 20:07:24 +0000890 COFF::IMAGE_SCN_MEM_DISCARDABLE |
891 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000892 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000893 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000894 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000895 COFF::IMAGE_SCN_MEM_DISCARDABLE |
896 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000897 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000898 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000899 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000900 COFF::IMAGE_SCN_MEM_DISCARDABLE |
901 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000902 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000903 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000904 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +0000905 COFF::IMAGE_SCN_MEM_DISCARDABLE |
906 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000907 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000908 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000909 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +0000910 COFF::IMAGE_SCN_MEM_DISCARDABLE |
911 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000912 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000913 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000914 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +0000915 COFF::IMAGE_SCN_MEM_DISCARDABLE |
916 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000917 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000918 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000919 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000920 COFF::IMAGE_SCN_MEM_DISCARDABLE |
921 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000922 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000923 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000924 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000925 COFF::IMAGE_SCN_MEM_DISCARDABLE |
926 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000927 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000928 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000929 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000930 COFF::IMAGE_SCN_MEM_DISCARDABLE |
931 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000932 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000933 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000934 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000935 COFF::IMAGE_SCN_MEM_DISCARDABLE |
936 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000937 SectionKind::getMetadata());
938
939 DrectveSection =
940 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000941 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000942 SectionKind::getMetadata());
943}
944
945static unsigned
946getCOFFSectionFlags(SectionKind K) {
947 unsigned Flags = 0;
948
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000949 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000950 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000951 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000952 else if (K.isText())
953 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000954 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000955 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000956 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000957 else if (K.isBSS ())
958 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000959 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
960 COFF::IMAGE_SCN_MEM_READ |
961 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000962 else if (K.isReadOnly())
963 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000964 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
965 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000966 else if (K.isWriteable())
967 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000968 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
969 COFF::IMAGE_SCN_MEM_READ |
970 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000971
972 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000973}
974
975const MCSection *TargetLoweringObjectFileCOFF::
976getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
977 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000978 return getContext().getCOFFSection(GV->getSection(),
979 getCOFFSectionFlags(Kind),
980 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000981}
982
983static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
984 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000985 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000986 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000987 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000988 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000989 return ".data$";
990 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000991}
992
993
994const MCSection *TargetLoweringObjectFileCOFF::
995SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
996 Mangler *Mang, const TargetMachine &TM) const {
997 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
998
999 // If this global is linkonce/weak and the target handles this by emitting it
1000 // into a 'uniqued' section name, create and return the section now.
1001 if (GV->isWeakForLinker()) {
1002 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1003 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +00001004 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001005 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001006
1007 unsigned Characteristics = getCOFFSectionFlags(Kind);
1008
Daniel Dunbar94610582010-07-01 20:07:24 +00001009 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001010
1011 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001012 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001013 }
1014
1015 if (Kind.isText())
1016 return getTextSection();
1017
1018 return getDataSection();
1019}
1020