blob: 5a0e2cfa30bd885b76bdc09b764c70c29a4adff9 [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"
Rafael Espindolac85dca62011-01-23 04:28:49 +000032#include "llvm/Support/ELF.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
35#include "llvm/ADT/SmallString.h"
36#include "llvm/ADT/StringExtras.h"
Chris Lattner8048ebe2010-09-27 06:44:54 +000037#include "llvm/ADT/Triple.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000038using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000039using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000040
41//===----------------------------------------------------------------------===//
42// ELF
43//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000044
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000045void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
46 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000047 TargetLoweringObjectFile::Initialize(Ctx, TM);
48
49 BSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000050 getContext().getELFSection(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000051 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000052 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000053
54 TextSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000055 getContext().getELFSection(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000056 ELF::SHF_EXECINSTR |
57 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000058 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000059
60 DataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000061 getContext().getELFSection(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000062 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000063 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000064
65 ReadOnlySection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000066 getContext().getELFSection(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000067 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000068 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000069
70 TLSDataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000071 getContext().getELFSection(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000072 ELF::SHF_ALLOC | ELF::SHF_TLS |
73 ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000074 SectionKind::getThreadData());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000075
76 TLSBSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000077 getContext().getELFSection(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000078 ELF::SHF_ALLOC | ELF::SHF_TLS |
79 ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000080 SectionKind::getThreadBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000081
82 DataRelSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000083 getContext().getELFSection(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000084 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000085 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000086
87 DataRelLocalSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000088 getContext().getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000089 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000090 SectionKind::getDataRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000091
92 DataRelROSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000093 getContext().getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000094 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000095 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000096
97 DataRelROLocalSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000098 getContext().getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000099 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000100 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000101
102 MergeableConst4Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000103 getContext().getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000104 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000105 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000106
107 MergeableConst8Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000108 getContext().getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000109 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000110 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000111
112 MergeableConst16Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000113 getContext().getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000114 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000115 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000116
117 StaticCtorSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000118 getContext().getELFSection(".ctors", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000119 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000120 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000121
122 StaticDtorSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000123 getContext().getELFSection(".dtors", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000124 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000125 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000126
127 // Exception Handling Sections.
128
129 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
130 // it contains relocatable pointers. In PIC mode, this is probably a big
131 // runtime hit for C++ apps. Either the contents of the LSDA need to be
132 // adjusted or this should be a data section.
133 LSDASection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000134 getContext().getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000135 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +0000136 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000137 // Debug Info Sections.
138 DwarfAbbrevSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000139 getContext().getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000140 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000141 DwarfInfoSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000142 getContext().getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000143 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000144 DwarfLineSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000145 getContext().getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000146 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000147 DwarfFrameSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000148 getContext().getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000149 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000150 DwarfPubNamesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000151 getContext().getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000152 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000153 DwarfPubTypesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000154 getContext().getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000155 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000156 DwarfStrSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000157 getContext().getELFSection(".debug_str", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000158 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000159 DwarfLocSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000160 getContext().getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000161 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000162 DwarfARangesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000163 getContext().getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000164 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000165 DwarfRangesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000166 getContext().getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000167 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000168 DwarfMacroInfoSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000169 getContext().getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000170 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000171}
172
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000173const MCSection *TargetLoweringObjectFileELF::getEHFrameSection() const {
174 return getContext().getELFSection(".eh_frame", ELF::SHT_PROGBITS,
175 ELF::SHF_ALLOC,
176 SectionKind::getDataRel());
177}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000178
179static SectionKind
180getELFKindForNamedSection(StringRef Name, SectionKind K) {
181 if (Name.empty() || Name[0] != '.') return K;
182
183 // Some lame default implementation based on some magic section names.
184 if (Name == ".bss" ||
185 Name.startswith(".bss.") ||
186 Name.startswith(".gnu.linkonce.b.") ||
187 Name.startswith(".llvm.linkonce.b.") ||
188 Name == ".sbss" ||
189 Name.startswith(".sbss.") ||
190 Name.startswith(".gnu.linkonce.sb.") ||
191 Name.startswith(".llvm.linkonce.sb."))
192 return SectionKind::getBSS();
193
194 if (Name == ".tdata" ||
195 Name.startswith(".tdata.") ||
196 Name.startswith(".gnu.linkonce.td.") ||
197 Name.startswith(".llvm.linkonce.td."))
198 return SectionKind::getThreadData();
199
200 if (Name == ".tbss" ||
201 Name.startswith(".tbss.") ||
202 Name.startswith(".gnu.linkonce.tb.") ||
203 Name.startswith(".llvm.linkonce.tb."))
204 return SectionKind::getThreadBSS();
205
206 return K;
207}
208
209
210static unsigned getELFSectionType(StringRef Name, SectionKind K) {
211
212 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000213 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000214
215 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000216 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000217
218 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000219 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000220
221 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000222 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000223
Rafael Espindolac85dca62011-01-23 04:28:49 +0000224 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000225}
226
227
228static unsigned
229getELFSectionFlags(SectionKind K) {
230 unsigned Flags = 0;
231
232 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000233 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000234
235 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000236 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000237
238 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000239 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000240
241 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000242 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000243
244 // K.isMergeableConst() is left out to honour PR4650
245 if (K.isMergeableCString() || K.isMergeableConst4() ||
246 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000247 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000248
249 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000250 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000251
252 return Flags;
253}
254
255
256const MCSection *TargetLoweringObjectFileELF::
257getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
258 Mangler *Mang, const TargetMachine &TM) const {
259 StringRef SectionName = GV->getSection();
260
261 // Infer section flags from the section name if we can.
262 Kind = getELFKindForNamedSection(SectionName, Kind);
263
Chris Lattner287df1b2010-04-08 21:34:17 +0000264 return getContext().getELFSection(SectionName,
265 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000266 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000267}
268
269static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
270 if (Kind.isText()) return ".gnu.linkonce.t.";
271 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
272
273 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
274 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
275
276 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
277 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
278 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
279 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
280
281 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
282 return ".gnu.linkonce.d.rel.ro.";
283}
284
Chris Lattner43ac7212010-04-13 00:36:43 +0000285/// getSectionPrefixForGlobal - Return the section prefix name used by options
286/// FunctionsSections and DataSections.
287static const char *getSectionPrefixForGlobal(SectionKind Kind) {
288 if (Kind.isText()) return ".text.";
289 if (Kind.isReadOnly()) return ".rodata.";
290
291 if (Kind.isThreadData()) return ".tdata.";
292 if (Kind.isThreadBSS()) return ".tbss.";
293
294 if (Kind.isDataNoRel()) return ".data.";
295 if (Kind.isDataRelLocal()) return ".data.rel.local.";
296 if (Kind.isDataRel()) return ".data.rel.";
297 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
298
299 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
300 return ".data.rel.ro.";
301}
302
303
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000304const MCSection *TargetLoweringObjectFileELF::
305SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
306 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000307 // If we have -ffunction-section or -fdata-section then we should emit the
308 // global value to a uniqued section specifically for it.
309 bool EmitUniquedSection;
310 if (Kind.isText())
311 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000312 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000313 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000314
315 // If this global is linkonce/weak and the target handles this by emitting it
316 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000317 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
318 !Kind.isCommon() && !Kind.isBSS()) {
319 const char *Prefix;
320 if (GV->isWeakForLinker())
321 Prefix = getSectionPrefixForUniqueGlobal(Kind);
322 else {
323 assert(EmitUniquedSection);
324 Prefix = getSectionPrefixForGlobal(Kind);
325 }
326
Chris Lattner4c6741f2010-03-15 20:37:38 +0000327 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
328 MCSymbol *Sym = Mang->getSymbol(GV);
329 Name.append(Sym->getName().begin(), Sym->getName().end());
Chris Lattner287df1b2010-04-08 21:34:17 +0000330 return getContext().getELFSection(Name.str(),
331 getELFSectionType(Name.str(), Kind),
332 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000333 }
334
335 if (Kind.isText()) return TextSection;
336
337 if (Kind.isMergeable1ByteCString() ||
338 Kind.isMergeable2ByteCString() ||
339 Kind.isMergeable4ByteCString()) {
340
341 // We also need alignment here.
342 // FIXME: this is getting the alignment of the character, not the
343 // alignment of the global!
344 unsigned Align =
345 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
346
347 const char *SizeSpec = ".rodata.str1.";
348 if (Kind.isMergeable2ByteCString())
349 SizeSpec = ".rodata.str2.";
350 else if (Kind.isMergeable4ByteCString())
351 SizeSpec = ".rodata.str4.";
352 else
353 assert(Kind.isMergeable1ByteCString() && "unknown string width");
354
355
356 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000357 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000358 ELF::SHF_ALLOC |
359 ELF::SHF_MERGE |
360 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000361 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000362 }
363
364 if (Kind.isMergeableConst()) {
365 if (Kind.isMergeableConst4() && MergeableConst4Section)
366 return MergeableConst4Section;
367 if (Kind.isMergeableConst8() && MergeableConst8Section)
368 return MergeableConst8Section;
369 if (Kind.isMergeableConst16() && MergeableConst16Section)
370 return MergeableConst16Section;
371 return ReadOnlySection; // .const
372 }
373
374 if (Kind.isReadOnly()) return ReadOnlySection;
375
376 if (Kind.isThreadData()) return TLSDataSection;
377 if (Kind.isThreadBSS()) return TLSBSSSection;
378
379 // Note: we claim that common symbols are put in BSSSection, but they are
380 // really emitted with the magic .comm directive, which creates a symbol table
381 // entry but not a section.
382 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
383
384 if (Kind.isDataNoRel()) return DataSection;
385 if (Kind.isDataRelLocal()) return DataRelLocalSection;
386 if (Kind.isDataRel()) return DataRelSection;
387 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
388
389 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
390 return DataRelROSection;
391}
392
393/// getSectionForConstant - Given a mergeable constant with the
394/// specified size and relocation information, return a section that it
395/// should be placed in.
396const MCSection *TargetLoweringObjectFileELF::
397getSectionForConstant(SectionKind Kind) const {
398 if (Kind.isMergeableConst4() && MergeableConst4Section)
399 return MergeableConst4Section;
400 if (Kind.isMergeableConst8() && MergeableConst8Section)
401 return MergeableConst8Section;
402 if (Kind.isMergeableConst16() && MergeableConst16Section)
403 return MergeableConst16Section;
404 if (Kind.isReadOnly())
405 return ReadOnlySection;
406
407 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
408 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
409 return DataRelROSection;
410}
411
412const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000413getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
414 MachineModuleInfo *MMI,
415 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000416
417 if (Encoding & dwarf::DW_EH_PE_indirect) {
418 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
419
420 SmallString<128> Name;
421 Mang->getNameWithPrefix(Name, GV, true);
422 Name += ".DW.stub";
423
424 // Add information about the stub reference to ELFMMI so that the stub
425 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000426 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000427 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000428 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000429 MCSymbol *Sym = Mang->getSymbol(GV);
430 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000431 }
432
433 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000434 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000435 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000436 }
437
438 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000439 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000440}
441
442//===----------------------------------------------------------------------===//
443// MachO
444//===----------------------------------------------------------------------===//
445
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000446void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
447 const TargetMachine &TM) {
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000448 // _foo.eh symbols are currently always exported so that the linker knows
449 // about them. This is not necessary on 10.6 and later, but it
450 // doesn't hurt anything.
451 // FIXME: I need to get this from Triple.
Chris Lattner09d53fe2010-03-10 07:20:42 +0000452 IsFunctionEHSymbolGlobal = true;
453 IsFunctionEHFrameSymbolPrivate = false;
454 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000455
456 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
457 if (T.getOS() == Triple::Darwin) {
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000458 switch (T.getDarwinMajorNumber()) {
459 case 7: // 10.3 Panther.
460 case 8: // 10.4 Tiger.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000461 CommDirectiveSupportsAlignment = false;
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000462 break;
463 case 9: // 10.5 Leopard.
464 case 10: // 10.6 SnowLeopard.
465 break;
466 }
Chris Lattner8048ebe2010-09-27 06:44:54 +0000467 }
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000468
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000469 TargetLoweringObjectFile::Initialize(Ctx, TM);
470
471 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000472 = getContext().getMachOSection("__TEXT", "__text",
473 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
474 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000475 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000476 = getContext().getMachOSection("__DATA", "__data", 0,
477 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000478
Eric Christopher423c9e32010-05-17 21:02:07 +0000479 TLSDataSection // .tdata
480 = getContext().getMachOSection("__DATA", "__thread_data",
481 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
482 SectionKind::getDataRel());
483 TLSBSSSection // .tbss
484 = getContext().getMachOSection("__DATA", "__thread_bss",
485 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
486 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000487
Eric Christopher423c9e32010-05-17 21:02:07 +0000488 // TODO: Verify datarel below.
489 TLSTLVSection // .tlv
490 = getContext().getMachOSection("__DATA", "__thread_vars",
491 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
492 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000493
Eric Christopherc6177a42010-05-17 22:53:55 +0000494 TLSThreadInitSection
495 = getContext().getMachOSection("__DATA", "__thread_init",
496 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
497 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000498
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000499 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000500 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000501 MCSectionMachO::S_CSTRING_LITERALS,
502 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000503 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000504 = getContext().getMachOSection("__TEXT","__ustring", 0,
505 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000506 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000507 = getContext().getMachOSection("__TEXT", "__literal4",
508 MCSectionMachO::S_4BYTE_LITERALS,
509 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000510 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000511 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000512 MCSectionMachO::S_8BYTE_LITERALS,
513 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000514
515 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
516 // to using it in -static mode.
517 SixteenByteConstantSection = 0;
518 if (TM.getRelocationModel() != Reloc::Static &&
519 TM.getTargetData()->getPointerSize() == 32)
520 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000521 getContext().getMachOSection("__TEXT", "__literal16",
522 MCSectionMachO::S_16BYTE_LITERALS,
523 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000524
525 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000526 = getContext().getMachOSection("__TEXT", "__const", 0,
527 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000528
529 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000530 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
531 MCSectionMachO::S_COALESCED |
532 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
533 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000534 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000535 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000536 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000537 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000538 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000539 = getContext().getMachOSection("__DATA", "__const", 0,
540 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000541 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000542 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000543 MCSectionMachO::S_COALESCED,
544 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000545 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000546 = getContext().getMachOSection("__DATA","__common",
547 MCSectionMachO::S_ZEROFILL,
548 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000549 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000550 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
551 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000552
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000553
554 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000555 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
556 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
557 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000558 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000559 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
560 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
561 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000562
563 if (TM.getRelocationModel() == Reloc::Static) {
564 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000565 = getContext().getMachOSection("__TEXT", "__constructor", 0,
566 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000567 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000568 = getContext().getMachOSection("__TEXT", "__destructor", 0,
569 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000570 } else {
571 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000572 = getContext().getMachOSection("__DATA", "__mod_init_func",
573 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
574 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000575 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000576 = getContext().getMachOSection("__DATA", "__mod_term_func",
577 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
578 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000579 }
580
581 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000582 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
583 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000584 // Debug Information.
585 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000586 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000587 MCSectionMachO::S_ATTR_DEBUG,
588 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000589 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000590 getContext().getMachOSection("__DWARF", "__debug_info",
591 MCSectionMachO::S_ATTR_DEBUG,
592 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000593 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000594 getContext().getMachOSection("__DWARF", "__debug_line",
595 MCSectionMachO::S_ATTR_DEBUG,
596 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000597 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000598 getContext().getMachOSection("__DWARF", "__debug_frame",
599 MCSectionMachO::S_ATTR_DEBUG,
600 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000601 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000602 getContext().getMachOSection("__DWARF", "__debug_pubnames",
603 MCSectionMachO::S_ATTR_DEBUG,
604 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000605 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000606 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
607 MCSectionMachO::S_ATTR_DEBUG,
608 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000609 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000610 getContext().getMachOSection("__DWARF", "__debug_str",
611 MCSectionMachO::S_ATTR_DEBUG,
612 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000613 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000614 getContext().getMachOSection("__DWARF", "__debug_loc",
615 MCSectionMachO::S_ATTR_DEBUG,
616 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000617 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000618 getContext().getMachOSection("__DWARF", "__debug_aranges",
619 MCSectionMachO::S_ATTR_DEBUG,
620 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000621 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000622 getContext().getMachOSection("__DWARF", "__debug_ranges",
623 MCSectionMachO::S_ATTR_DEBUG,
624 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000625 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000626 getContext().getMachOSection("__DWARF", "__debug_macinfo",
627 MCSectionMachO::S_ATTR_DEBUG,
628 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000629 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000630 getContext().getMachOSection("__DWARF", "__debug_inlined",
631 MCSectionMachO::S_ATTR_DEBUG,
632 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000633
Eric Christopher8116ca52010-05-22 00:10:22 +0000634 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000635}
636
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000637const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
638 return getContext().getMachOSection("__TEXT", "__eh_frame",
639 MCSectionMachO::S_COALESCED |
640 MCSectionMachO::S_ATTR_NO_TOC |
641 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
642 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
643 SectionKind::getReadOnly());
644}
645
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000646const MCSection *TargetLoweringObjectFileMachO::
647getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
648 Mangler *Mang, const TargetMachine &TM) const {
649 // Parse the section specifier and create it if valid.
650 StringRef Segment, Section;
651 unsigned TAA, StubSize;
652 std::string ErrorCode =
653 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
654 TAA, StubSize);
655 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000656 // If invalid, report the error with report_fatal_error.
657 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000658 "' has an invalid section specifier '" + GV->getSection()+
659 "': " + ErrorCode + ".");
660 // Fall back to dropping it into the data section.
661 return DataSection;
662 }
663
664 // Get the section.
665 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000666 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000667
668 // Okay, now that we got the section, verify that the TAA & StubSize agree.
669 // If the user declared multiple globals with different section flags, we need
670 // to reject it here.
671 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000672 // If invalid, report the error with report_fatal_error.
673 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000674 "' section type or attributes does not match previous"
675 " section specifier");
676 }
677
678 return S;
679}
680
681const MCSection *TargetLoweringObjectFileMachO::
682SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000683 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000684
Eric Christopher02b46bc2010-05-25 21:28:50 +0000685 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000686 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000687 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000688
689 if (Kind.isText())
690 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
691
692 // If this is weak/linkonce, put this in a coalescable section, either in text
693 // or data depending on if it is writable.
694 if (GV->isWeakForLinker()) {
695 if (Kind.isReadOnly())
696 return ConstTextCoalSection;
697 return DataCoalSection;
698 }
699
700 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000701 if (Kind.isMergeable1ByteCString() &&
702 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
703 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000704
Chris Lattner98f15d22010-03-07 04:28:09 +0000705 // Do not put 16-bit arrays in the UString section if they have an
706 // externally visible label, this runs into issues with certain linker
707 // versions.
708 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
709 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
710 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000711
712 if (Kind.isMergeableConst()) {
713 if (Kind.isMergeableConst4())
714 return FourByteConstantSection;
715 if (Kind.isMergeableConst8())
716 return EightByteConstantSection;
717 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
718 return SixteenByteConstantSection;
719 }
720
721 // Otherwise, if it is readonly, but not something we can specially optimize,
722 // just drop it in .const.
723 if (Kind.isReadOnly())
724 return ReadOnlySection;
725
726 // If this is marked const, put it into a const section. But if the dynamic
727 // linker needs to write to it, put it in the data segment.
728 if (Kind.isReadOnlyWithRel())
729 return ConstDataSection;
730
731 // Put zero initialized globals with strong external linkage in the
732 // DATA, __common section with the .zerofill directive.
733 if (Kind.isBSSExtern())
734 return DataCommonSection;
735
736 // Put zero initialized globals with local linkage in __DATA,__bss directive
737 // with the .zerofill directive (aka .lcomm).
738 if (Kind.isBSSLocal())
739 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000740
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000741 // Otherwise, just drop the variable in the normal data section.
742 return DataSection;
743}
744
745const MCSection *
746TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
747 // If this constant requires a relocation, we have to put it in the data
748 // segment, not in the text segment.
749 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
750 return ConstDataSection;
751
752 if (Kind.isMergeableConst4())
753 return FourByteConstantSection;
754 if (Kind.isMergeableConst8())
755 return EightByteConstantSection;
756 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
757 return SixteenByteConstantSection;
758 return ReadOnlySection; // .const
759}
760
761/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
762/// not to emit the UsedDirective for some symbols in llvm.used.
763// FIXME: REMOVE this (rdar://7071300)
764bool TargetLoweringObjectFileMachO::
765shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
766 /// On Darwin, internally linked data beginning with "L" or "l" does not have
767 /// the directive emitted (this occurs in ObjC metadata).
768 if (!GV) return false;
769
Bill Wendling07d31772010-06-29 22:34:52 +0000770 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000771 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
772 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000773 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
774 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000775 MCSymbol *Sym = Mang->getSymbol(GV);
776 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000777 return false;
778 }
779
780 return true;
781}
782
783const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000784getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
785 MachineModuleInfo *MMI, unsigned Encoding,
786 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000787 // The mach-o version of this method defaults to returning a stub reference.
788
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000789 if (Encoding & DW_EH_PE_indirect) {
790 MachineModuleInfoMachO &MachOMMI =
791 MMI->getObjFileInfo<MachineModuleInfoMachO>();
792
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000793 SmallString<128> Name;
794 Mang->getNameWithPrefix(Name, GV, true);
795 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000796
797 // Add information about the stub reference to MachOMMI so that the stub
798 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000799 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000800 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000801 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000802 MCSymbol *Sym = Mang->getSymbol(GV);
803 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000804 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000805
806 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000807 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000808 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000809 }
810
811 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000812 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000813}
814
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000815unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
816 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
817}
818
819unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
820 return DW_EH_PE_pcrel;
821}
822
823unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
824 return DW_EH_PE_pcrel;
825}
826
827unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000828 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000829}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000830
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000831//===----------------------------------------------------------------------===//
832// COFF
833//===----------------------------------------------------------------------===//
834
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000835void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
836 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000837 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000838 TextSection =
839 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000840 COFF::IMAGE_SCN_CNT_CODE |
841 COFF::IMAGE_SCN_MEM_EXECUTE |
842 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000843 SectionKind::getText());
844 DataSection =
845 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000846 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
847 COFF::IMAGE_SCN_MEM_READ |
848 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000849 SectionKind::getDataRel());
850 ReadOnlySection =
851 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000852 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
853 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000854 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000855 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000856 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000857 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
858 COFF::IMAGE_SCN_MEM_READ |
859 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000860 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000861 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000862 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000863 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
864 COFF::IMAGE_SCN_MEM_READ |
865 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000866 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000867
868 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
869 // though it contains relocatable pointers. In PIC mode, this is probably a
870 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
871 // adjusted or this should be a data section.
872 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000873 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000874 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
875 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000876 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000877 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000878 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000879 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000880 COFF::IMAGE_SCN_MEM_DISCARDABLE |
881 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000882 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000883 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000884 getContext().getCOFFSection(".debug_info",
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 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000889 getContext().getCOFFSection(".debug_line",
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 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000894 getContext().getCOFFSection(".debug_frame",
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 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000899 getContext().getCOFFSection(".debug_pubnames",
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 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000904 getContext().getCOFFSection(".debug_pubtypes",
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 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000909 getContext().getCOFFSection(".debug_str",
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 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000914 getContext().getCOFFSection(".debug_loc",
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 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000919 getContext().getCOFFSection(".debug_aranges",
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 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000924 getContext().getCOFFSection(".debug_ranges",
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 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000929 getContext().getCOFFSection(".debug_macinfo",
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());
933
934 DrectveSection =
935 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000936 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000937 SectionKind::getMetadata());
938}
939
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000940const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
941 return getContext().getCOFFSection(".eh_frame",
942 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
943 COFF::IMAGE_SCN_MEM_READ |
944 COFF::IMAGE_SCN_MEM_WRITE,
945 SectionKind::getDataRel());
946}
947
948
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000949static unsigned
950getCOFFSectionFlags(SectionKind K) {
951 unsigned Flags = 0;
952
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000953 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000954 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000955 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000956 else if (K.isText())
957 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000958 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000959 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000960 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000961 else if (K.isBSS ())
962 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000963 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
964 COFF::IMAGE_SCN_MEM_READ |
965 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000966 else if (K.isReadOnly())
967 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000968 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
969 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000970 else if (K.isWriteable())
971 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000972 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
973 COFF::IMAGE_SCN_MEM_READ |
974 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000975
976 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000977}
978
979const MCSection *TargetLoweringObjectFileCOFF::
980getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
981 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000982 return getContext().getCOFFSection(GV->getSection(),
983 getCOFFSectionFlags(Kind),
984 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000985}
986
987static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
988 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000989 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000990 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000991 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000992 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000993 return ".data$";
994 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000995}
996
997
998const MCSection *TargetLoweringObjectFileCOFF::
999SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1000 Mangler *Mang, const TargetMachine &TM) const {
1001 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1002
1003 // If this global is linkonce/weak and the target handles this by emitting it
1004 // into a 'uniqued' section name, create and return the section now.
1005 if (GV->isWeakForLinker()) {
1006 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1007 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +00001008 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001009 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001010
1011 unsigned Characteristics = getCOFFSectionFlags(Kind);
1012
Daniel Dunbar94610582010-07-01 20:07:24 +00001013 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001014
1015 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001016 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001017 }
1018
1019 if (Kind.isText())
1020 return getTextSection();
1021
1022 return getDataSection();
1023}
1024