blob: 6a4438cde2b6c15d985816ddd0a90b250ca912a4 [file] [log] [blame]
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001//===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements classes used to handle lowerings specific to common
11// object file formats.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/GlobalVariable.h"
20#include "llvm/CodeGen/MachineModuleInfoImpls.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCSectionMachO.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/Target/Mangler.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetOptions.h"
30#include "llvm/Support/Dwarf.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/ADT/SmallString.h"
34#include "llvm/ADT/StringExtras.h"
35using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000036using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000037
38//===----------------------------------------------------------------------===//
39// ELF
40//===----------------------------------------------------------------------===//
41typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
42
43TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
44 // If we have the section uniquing map, free it.
45 delete (ELFUniqueMapTy*)UniquingMap;
46}
47
48const MCSection *TargetLoweringObjectFileELF::
49getELFSection(StringRef Section, unsigned Type, unsigned Flags,
50 SectionKind Kind, bool IsExplicit) const {
51 if (UniquingMap == 0)
52 UniquingMap = new ELFUniqueMapTy();
53 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
54
55 // Do the lookup, if we have a hit, return it.
Chris Lattner1f8008c2010-03-15 06:23:52 +000056 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
57 if (Entry.getValue()) return Entry.getValue();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000058
Chris Lattner1f8008c2010-03-15 06:23:52 +000059 MCSectionELF *Result = MCSectionELF::Create(Entry.getKey(), Type, Flags, Kind,
60 IsExplicit, getContext());
61 Entry.setValue(Result);
62 return Result;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000063}
64
65void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
66 const TargetMachine &TM) {
67 if (UniquingMap != 0)
68 ((ELFUniqueMapTy*)UniquingMap)->clear();
69 TargetLoweringObjectFile::Initialize(Ctx, TM);
70
71 BSSSection =
72 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
73 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
74 SectionKind::getBSS());
75
76 TextSection =
77 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
78 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
79 SectionKind::getText());
80
81 DataSection =
82 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
83 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
84 SectionKind::getDataRel());
85
86 ReadOnlySection =
87 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
88 MCSectionELF::SHF_ALLOC,
89 SectionKind::getReadOnly());
90
91 TLSDataSection =
92 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
93 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
94 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
95
96 TLSBSSSection =
97 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
98 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
99 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
100
101 DataRelSection =
102 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
103 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
104 SectionKind::getDataRel());
105
106 DataRelLocalSection =
107 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
109 SectionKind::getDataRelLocal());
110
111 DataRelROSection =
112 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
113 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
114 SectionKind::getReadOnlyWithRel());
115
116 DataRelROLocalSection =
117 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
118 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
119 SectionKind::getReadOnlyWithRelLocal());
120
121 MergeableConst4Section =
122 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
123 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
124 SectionKind::getMergeableConst4());
125
126 MergeableConst8Section =
127 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
128 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
129 SectionKind::getMergeableConst8());
130
131 MergeableConst16Section =
132 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
133 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
134 SectionKind::getMergeableConst16());
135
136 StaticCtorSection =
137 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
138 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
139 SectionKind::getDataRel());
140
141 StaticDtorSection =
142 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
143 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
144 SectionKind::getDataRel());
145
146 // Exception Handling Sections.
147
148 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
149 // it contains relocatable pointers. In PIC mode, this is probably a big
150 // runtime hit for C++ apps. Either the contents of the LSDA need to be
151 // adjusted or this should be a data section.
152 LSDASection =
153 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
154 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
155 EHFrameSection =
156 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
157 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
158 SectionKind::getDataRel());
159
160 // Debug Info Sections.
161 DwarfAbbrevSection =
162 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
163 SectionKind::getMetadata());
164 DwarfInfoSection =
165 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
166 SectionKind::getMetadata());
167 DwarfLineSection =
168 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
169 SectionKind::getMetadata());
170 DwarfFrameSection =
171 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
172 SectionKind::getMetadata());
173 DwarfPubNamesSection =
174 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
175 SectionKind::getMetadata());
176 DwarfPubTypesSection =
177 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
178 SectionKind::getMetadata());
179 DwarfStrSection =
180 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
181 SectionKind::getMetadata());
182 DwarfLocSection =
183 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
184 SectionKind::getMetadata());
185 DwarfARangesSection =
186 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
187 SectionKind::getMetadata());
188 DwarfRangesSection =
189 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
190 SectionKind::getMetadata());
191 DwarfMacroInfoSection =
192 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
193 SectionKind::getMetadata());
194}
195
196
197static SectionKind
198getELFKindForNamedSection(StringRef Name, SectionKind K) {
199 if (Name.empty() || Name[0] != '.') return K;
200
201 // Some lame default implementation based on some magic section names.
202 if (Name == ".bss" ||
203 Name.startswith(".bss.") ||
204 Name.startswith(".gnu.linkonce.b.") ||
205 Name.startswith(".llvm.linkonce.b.") ||
206 Name == ".sbss" ||
207 Name.startswith(".sbss.") ||
208 Name.startswith(".gnu.linkonce.sb.") ||
209 Name.startswith(".llvm.linkonce.sb."))
210 return SectionKind::getBSS();
211
212 if (Name == ".tdata" ||
213 Name.startswith(".tdata.") ||
214 Name.startswith(".gnu.linkonce.td.") ||
215 Name.startswith(".llvm.linkonce.td."))
216 return SectionKind::getThreadData();
217
218 if (Name == ".tbss" ||
219 Name.startswith(".tbss.") ||
220 Name.startswith(".gnu.linkonce.tb.") ||
221 Name.startswith(".llvm.linkonce.tb."))
222 return SectionKind::getThreadBSS();
223
224 return K;
225}
226
227
228static unsigned getELFSectionType(StringRef Name, SectionKind K) {
229
230 if (Name == ".init_array")
231 return MCSectionELF::SHT_INIT_ARRAY;
232
233 if (Name == ".fini_array")
234 return MCSectionELF::SHT_FINI_ARRAY;
235
236 if (Name == ".preinit_array")
237 return MCSectionELF::SHT_PREINIT_ARRAY;
238
239 if (K.isBSS() || K.isThreadBSS())
240 return MCSectionELF::SHT_NOBITS;
241
242 return MCSectionELF::SHT_PROGBITS;
243}
244
245
246static unsigned
247getELFSectionFlags(SectionKind K) {
248 unsigned Flags = 0;
249
250 if (!K.isMetadata())
251 Flags |= MCSectionELF::SHF_ALLOC;
252
253 if (K.isText())
254 Flags |= MCSectionELF::SHF_EXECINSTR;
255
256 if (K.isWriteable())
257 Flags |= MCSectionELF::SHF_WRITE;
258
259 if (K.isThreadLocal())
260 Flags |= MCSectionELF::SHF_TLS;
261
262 // K.isMergeableConst() is left out to honour PR4650
263 if (K.isMergeableCString() || K.isMergeableConst4() ||
264 K.isMergeableConst8() || K.isMergeableConst16())
265 Flags |= MCSectionELF::SHF_MERGE;
266
267 if (K.isMergeableCString())
268 Flags |= MCSectionELF::SHF_STRINGS;
269
270 return Flags;
271}
272
273
274const MCSection *TargetLoweringObjectFileELF::
275getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
276 Mangler *Mang, const TargetMachine &TM) const {
277 StringRef SectionName = GV->getSection();
278
279 // Infer section flags from the section name if we can.
280 Kind = getELFKindForNamedSection(SectionName, Kind);
281
282 return getELFSection(SectionName,
283 getELFSectionType(SectionName, Kind),
284 getELFSectionFlags(Kind), Kind, true);
285}
286
287static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
288 if (Kind.isText()) return ".gnu.linkonce.t.";
289 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
290
291 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
292 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
293
294 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
295 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
296 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
297 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
298
299 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
300 return ".gnu.linkonce.d.rel.ro.";
301}
302
303const MCSection *TargetLoweringObjectFileELF::
304SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
305 Mangler *Mang, const TargetMachine &TM) const {
306
307 // If this global is linkonce/weak and the target handles this by emitting it
308 // into a 'uniqued' section name, create and return the section now.
309 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
310 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattner4c6741f2010-03-15 20:37:38 +0000311 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
312 MCSymbol *Sym = Mang->getSymbol(GV);
313 Name.append(Sym->getName().begin(), Sym->getName().end());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000314 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
315 getELFSectionFlags(Kind), Kind);
316 }
317
318 if (Kind.isText()) return TextSection;
319
320 if (Kind.isMergeable1ByteCString() ||
321 Kind.isMergeable2ByteCString() ||
322 Kind.isMergeable4ByteCString()) {
323
324 // We also need alignment here.
325 // FIXME: this is getting the alignment of the character, not the
326 // alignment of the global!
327 unsigned Align =
328 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
329
330 const char *SizeSpec = ".rodata.str1.";
331 if (Kind.isMergeable2ByteCString())
332 SizeSpec = ".rodata.str2.";
333 else if (Kind.isMergeable4ByteCString())
334 SizeSpec = ".rodata.str4.";
335 else
336 assert(Kind.isMergeable1ByteCString() && "unknown string width");
337
338
339 std::string Name = SizeSpec + utostr(Align);
340 return getELFSection(Name, MCSectionELF::SHT_PROGBITS,
341 MCSectionELF::SHF_ALLOC |
342 MCSectionELF::SHF_MERGE |
343 MCSectionELF::SHF_STRINGS,
344 Kind);
345 }
346
347 if (Kind.isMergeableConst()) {
348 if (Kind.isMergeableConst4() && MergeableConst4Section)
349 return MergeableConst4Section;
350 if (Kind.isMergeableConst8() && MergeableConst8Section)
351 return MergeableConst8Section;
352 if (Kind.isMergeableConst16() && MergeableConst16Section)
353 return MergeableConst16Section;
354 return ReadOnlySection; // .const
355 }
356
357 if (Kind.isReadOnly()) return ReadOnlySection;
358
359 if (Kind.isThreadData()) return TLSDataSection;
360 if (Kind.isThreadBSS()) return TLSBSSSection;
361
362 // Note: we claim that common symbols are put in BSSSection, but they are
363 // really emitted with the magic .comm directive, which creates a symbol table
364 // entry but not a section.
365 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
366
367 if (Kind.isDataNoRel()) return DataSection;
368 if (Kind.isDataRelLocal()) return DataRelLocalSection;
369 if (Kind.isDataRel()) return DataRelSection;
370 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
371
372 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
373 return DataRelROSection;
374}
375
376/// getSectionForConstant - Given a mergeable constant with the
377/// specified size and relocation information, return a section that it
378/// should be placed in.
379const MCSection *TargetLoweringObjectFileELF::
380getSectionForConstant(SectionKind Kind) const {
381 if (Kind.isMergeableConst4() && MergeableConst4Section)
382 return MergeableConst4Section;
383 if (Kind.isMergeableConst8() && MergeableConst8Section)
384 return MergeableConst8Section;
385 if (Kind.isMergeableConst16() && MergeableConst16Section)
386 return MergeableConst16Section;
387 if (Kind.isReadOnly())
388 return ReadOnlySection;
389
390 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
391 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
392 return DataRelROSection;
393}
394
395const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000396getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
397 MachineModuleInfo *MMI,
398 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000399
400 if (Encoding & dwarf::DW_EH_PE_indirect) {
401 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
402
403 SmallString<128> Name;
404 Mang->getNameWithPrefix(Name, GV, true);
405 Name += ".DW.stub";
406
407 // Add information about the stub reference to ELFMMI so that the stub
408 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000409 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000410 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000411 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000412 MCSymbol *Sym = Mang->getSymbol(GV);
413 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000414 }
415
416 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000417 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000418 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000419 }
420
421 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000422 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000423}
424
425//===----------------------------------------------------------------------===//
426// MachO
427//===----------------------------------------------------------------------===//
428
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000429
430const MCSectionMachO *TargetLoweringObjectFileMachO::
431getMachOSection(StringRef Segment, StringRef Section,
432 unsigned TypeAndAttributes,
433 unsigned Reserved2, SectionKind Kind) const {
Chris Lattnerf0559e42010-04-08 20:30:37 +0000434
435 return getContext().getMachOSection(Segment, Section, TypeAndAttributes,
436 Reserved2, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000437}
438
439
440void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
441 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000442 // _foo.eh symbols are currently always exported so that the linker knows
443 // about them. This is not necessary on 10.6 and later, but it
444 // doesn't hurt anything.
445 // FIXME: I need to get this from Triple.
446 IsFunctionEHSymbolGlobal = true;
447 IsFunctionEHFrameSymbolPrivate = false;
448 SupportsWeakOmittedEHFrame = false;
449
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000450 TargetLoweringObjectFile::Initialize(Ctx, TM);
451
452 TextSection // .text
453 = getMachOSection("__TEXT", "__text",
454 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
455 SectionKind::getText());
456 DataSection // .data
457 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
458
459 CStringSection // .cstring
460 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
461 SectionKind::getMergeable1ByteCString());
462 UStringSection
463 = getMachOSection("__TEXT","__ustring", 0,
464 SectionKind::getMergeable2ByteCString());
465 FourByteConstantSection // .literal4
466 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
467 SectionKind::getMergeableConst4());
468 EightByteConstantSection // .literal8
469 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
470 SectionKind::getMergeableConst8());
471
472 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
473 // to using it in -static mode.
474 SixteenByteConstantSection = 0;
475 if (TM.getRelocationModel() != Reloc::Static &&
476 TM.getTargetData()->getPointerSize() == 32)
477 SixteenByteConstantSection = // .literal16
478 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
479 SectionKind::getMergeableConst16());
480
481 ReadOnlySection // .const
482 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
483
484 TextCoalSection
485 = getMachOSection("__TEXT", "__textcoal_nt",
486 MCSectionMachO::S_COALESCED |
487 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
488 SectionKind::getText());
489 ConstTextCoalSection
490 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
491 SectionKind::getText());
492 ConstDataCoalSection
493 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
494 SectionKind::getText());
495 ConstDataSection // .const_data
496 = getMachOSection("__DATA", "__const", 0,
497 SectionKind::getReadOnlyWithRel());
498 DataCoalSection
499 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
500 SectionKind::getDataRel());
501 DataCommonSection
502 = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL,
503 SectionKind::getBSS());
504 DataBSSSection
505 = getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
506 SectionKind::getBSS());
507
508
509 LazySymbolPointerSection
510 = getMachOSection("__DATA", "__la_symbol_ptr",
511 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
512 SectionKind::getMetadata());
513 NonLazySymbolPointerSection
514 = getMachOSection("__DATA", "__nl_symbol_ptr",
515 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
516 SectionKind::getMetadata());
517
518 if (TM.getRelocationModel() == Reloc::Static) {
519 StaticCtorSection
520 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
521 StaticDtorSection
522 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
523 } else {
524 StaticCtorSection
525 = getMachOSection("__DATA", "__mod_init_func",
526 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
527 SectionKind::getDataRel());
528 StaticDtorSection
529 = getMachOSection("__DATA", "__mod_term_func",
530 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
531 SectionKind::getDataRel());
532 }
533
534 // Exception Handling.
Bill Wendling505ad8b2010-03-15 21:09:38 +0000535 LSDASection = getMachOSection("__TEXT", "__gcc_except_tab", 0,
536 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000537 EHFrameSection =
538 getMachOSection("__TEXT", "__eh_frame",
539 MCSectionMachO::S_COALESCED |
540 MCSectionMachO::S_ATTR_NO_TOC |
541 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
542 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
543 SectionKind::getReadOnly());
544
545 // Debug Information.
546 DwarfAbbrevSection =
547 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
548 SectionKind::getMetadata());
549 DwarfInfoSection =
550 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
551 SectionKind::getMetadata());
552 DwarfLineSection =
553 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
554 SectionKind::getMetadata());
555 DwarfFrameSection =
556 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
557 SectionKind::getMetadata());
558 DwarfPubNamesSection =
559 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
560 SectionKind::getMetadata());
561 DwarfPubTypesSection =
562 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
563 SectionKind::getMetadata());
564 DwarfStrSection =
565 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
566 SectionKind::getMetadata());
567 DwarfLocSection =
568 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
569 SectionKind::getMetadata());
570 DwarfARangesSection =
571 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
572 SectionKind::getMetadata());
573 DwarfRangesSection =
574 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
575 SectionKind::getMetadata());
576 DwarfMacroInfoSection =
577 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
578 SectionKind::getMetadata());
579 DwarfDebugInlineSection =
580 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
581 SectionKind::getMetadata());
582}
583
584const MCSection *TargetLoweringObjectFileMachO::
585getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
586 Mangler *Mang, const TargetMachine &TM) const {
587 // Parse the section specifier and create it if valid.
588 StringRef Segment, Section;
589 unsigned TAA, StubSize;
590 std::string ErrorCode =
591 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
592 TAA, StubSize);
593 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000594 // If invalid, report the error with report_fatal_error.
595 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000596 "' has an invalid section specifier '" + GV->getSection()+
597 "': " + ErrorCode + ".");
598 // Fall back to dropping it into the data section.
599 return DataSection;
600 }
601
602 // Get the section.
603 const MCSectionMachO *S =
604 getMachOSection(Segment, Section, TAA, StubSize, Kind);
605
606 // Okay, now that we got the section, verify that the TAA & StubSize agree.
607 // If the user declared multiple globals with different section flags, we need
608 // to reject it here.
609 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000610 // If invalid, report the error with report_fatal_error.
611 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000612 "' section type or attributes does not match previous"
613 " section specifier");
614 }
615
616 return S;
617}
618
619const MCSection *TargetLoweringObjectFileMachO::
620SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
621 Mangler *Mang, const TargetMachine &TM) const {
622 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
623
624 if (Kind.isText())
625 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
626
627 // If this is weak/linkonce, put this in a coalescable section, either in text
628 // or data depending on if it is writable.
629 if (GV->isWeakForLinker()) {
630 if (Kind.isReadOnly())
631 return ConstTextCoalSection;
632 return DataCoalSection;
633 }
634
635 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000636 if (Kind.isMergeable1ByteCString() &&
637 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
638 return CStringSection;
639
640 // Do not put 16-bit arrays in the UString section if they have an
641 // externally visible label, this runs into issues with certain linker
642 // versions.
643 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
644 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
645 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000646
647 if (Kind.isMergeableConst()) {
648 if (Kind.isMergeableConst4())
649 return FourByteConstantSection;
650 if (Kind.isMergeableConst8())
651 return EightByteConstantSection;
652 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
653 return SixteenByteConstantSection;
654 }
655
656 // Otherwise, if it is readonly, but not something we can specially optimize,
657 // just drop it in .const.
658 if (Kind.isReadOnly())
659 return ReadOnlySection;
660
661 // If this is marked const, put it into a const section. But if the dynamic
662 // linker needs to write to it, put it in the data segment.
663 if (Kind.isReadOnlyWithRel())
664 return ConstDataSection;
665
666 // Put zero initialized globals with strong external linkage in the
667 // DATA, __common section with the .zerofill directive.
668 if (Kind.isBSSExtern())
669 return DataCommonSection;
670
671 // Put zero initialized globals with local linkage in __DATA,__bss directive
672 // with the .zerofill directive (aka .lcomm).
673 if (Kind.isBSSLocal())
674 return DataBSSSection;
675
676 // Otherwise, just drop the variable in the normal data section.
677 return DataSection;
678}
679
680const MCSection *
681TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
682 // If this constant requires a relocation, we have to put it in the data
683 // segment, not in the text segment.
684 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
685 return ConstDataSection;
686
687 if (Kind.isMergeableConst4())
688 return FourByteConstantSection;
689 if (Kind.isMergeableConst8())
690 return EightByteConstantSection;
691 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
692 return SixteenByteConstantSection;
693 return ReadOnlySection; // .const
694}
695
696/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
697/// not to emit the UsedDirective for some symbols in llvm.used.
698// FIXME: REMOVE this (rdar://7071300)
699bool TargetLoweringObjectFileMachO::
700shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
701 /// On Darwin, internally linked data beginning with "L" or "l" does not have
702 /// the directive emitted (this occurs in ObjC metadata).
703 if (!GV) return false;
704
705 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
706 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
707 // FIXME: ObjC metadata is currently emitted as internal symbols that have
708 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
709 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000710 MCSymbol *Sym = Mang->getSymbol(GV);
711 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000712 return false;
713 }
714
715 return true;
716}
717
718const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000719getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
720 MachineModuleInfo *MMI, unsigned Encoding,
721 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000722 // The mach-o version of this method defaults to returning a stub reference.
723
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000724 if (Encoding & DW_EH_PE_indirect) {
725 MachineModuleInfoMachO &MachOMMI =
726 MMI->getObjFileInfo<MachineModuleInfoMachO>();
727
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000728 SmallString<128> Name;
729 Mang->getNameWithPrefix(Name, GV, true);
730 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000731
732 // Add information about the stub reference to MachOMMI so that the stub
733 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000734 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000735 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000736 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000737 MCSymbol *Sym = Mang->getSymbol(GV);
738 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000739 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000740
741 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000742 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000743 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000744 }
745
746 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000747 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000748}
749
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000750unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
751 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
752}
753
754unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
755 return DW_EH_PE_pcrel;
756}
757
758unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
759 return DW_EH_PE_pcrel;
760}
761
762unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000763 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000764}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000765
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000766//===----------------------------------------------------------------------===//
767// COFF
768//===----------------------------------------------------------------------===//
769
770typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
771
772TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
773 delete (COFFUniqueMapTy*)UniquingMap;
774}
775
776
777const MCSection *TargetLoweringObjectFileCOFF::
778getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
779 // Create the map if it doesn't already exist.
780 if (UniquingMap == 0)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000781 UniquingMap = new COFFUniqueMapTy();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000782 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
783
784 // Do the lookup, if we have a hit, return it.
785 const MCSectionCOFF *&Entry = Map[Name];
786 if (Entry) return Entry;
787
788 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
789}
790
791void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
792 const TargetMachine &TM) {
793 if (UniquingMap != 0)
794 ((COFFUniqueMapTy*)UniquingMap)->clear();
795 TargetLoweringObjectFile::Initialize(Ctx, TM);
796 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
797 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
798 StaticCtorSection =
799 getCOFFSection(".ctors", false, SectionKind::getDataRel());
800 StaticDtorSection =
801 getCOFFSection(".dtors", false, SectionKind::getDataRel());
802
803 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
804 // though it contains relocatable pointers. In PIC mode, this is probably a
805 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
806 // adjusted or this should be a data section.
807 LSDASection =
808 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
809 EHFrameSection =
810 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
811
812 // Debug info.
813 // FIXME: Don't use 'directive' mode here.
814 DwarfAbbrevSection =
815 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
816 true, SectionKind::getMetadata());
817 DwarfInfoSection =
818 getCOFFSection("\t.section\t.debug_info,\"dr\"",
819 true, SectionKind::getMetadata());
820 DwarfLineSection =
821 getCOFFSection("\t.section\t.debug_line,\"dr\"",
822 true, SectionKind::getMetadata());
823 DwarfFrameSection =
824 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
825 true, SectionKind::getMetadata());
826 DwarfPubNamesSection =
827 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
828 true, SectionKind::getMetadata());
829 DwarfPubTypesSection =
830 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
831 true, SectionKind::getMetadata());
832 DwarfStrSection =
833 getCOFFSection("\t.section\t.debug_str,\"dr\"",
834 true, SectionKind::getMetadata());
835 DwarfLocSection =
836 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
837 true, SectionKind::getMetadata());
838 DwarfARangesSection =
839 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
840 true, SectionKind::getMetadata());
841 DwarfRangesSection =
842 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
843 true, SectionKind::getMetadata());
844 DwarfMacroInfoSection =
845 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
846 true, SectionKind::getMetadata());
847}
848
849const MCSection *TargetLoweringObjectFileCOFF::
850getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
851 Mangler *Mang, const TargetMachine &TM) const {
852 return getCOFFSection(GV->getSection(), false, Kind);
853}
854
855static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
856 if (Kind.isText())
857 return ".text$linkonce";
858 if (Kind.isWriteable())
859 return ".data$linkonce";
860 return ".rdata$linkonce";
861}
862
863
864const MCSection *TargetLoweringObjectFileCOFF::
865SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
866 Mangler *Mang, const TargetMachine &TM) const {
867 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
868
869 // If this global is linkonce/weak and the target handles this by emitting it
870 // into a 'uniqued' section name, create and return the section now.
871 if (GV->isWeakForLinker()) {
872 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
873 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000874 MCSymbol *Sym = Mang->getSymbol(GV);
875 Name.append(Sym->getName().begin(), Sym->getName().end());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000876 return getCOFFSection(Name.str(), false, Kind);
877 }
878
879 if (Kind.isText())
880 return getTextSection();
881
882 return getDataSection();
883}
884