blob: cc278658d0931e8e0a00e7ba28d45f794a902cf3 [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
Chris Lattner43ac7212010-04-13 00:36:43 +0000269/// getSectionPrefixForGlobal - Return the section prefix name used by options
270/// FunctionsSections and DataSections.
271static const char *getSectionPrefixForGlobal(SectionKind Kind) {
272 if (Kind.isText()) return ".text.";
273 if (Kind.isReadOnly()) return ".rodata.";
274
275 if (Kind.isThreadData()) return ".tdata.";
276 if (Kind.isThreadBSS()) return ".tbss.";
277
278 if (Kind.isDataNoRel()) return ".data.";
279 if (Kind.isDataRelLocal()) return ".data.rel.local.";
280 if (Kind.isDataRel()) return ".data.rel.";
281 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
282
283 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
284 return ".data.rel.ro.";
285}
286
287
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000288const MCSection *TargetLoweringObjectFileELF::
289SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
290 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000291 // If we have -ffunction-section or -fdata-section then we should emit the
292 // global value to a uniqued section specifically for it.
293 bool EmitUniquedSection;
294 if (Kind.isText())
295 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000296 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000297 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000298
299 // If this global is linkonce/weak and the target handles this by emitting it
300 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000301 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
302 !Kind.isCommon() && !Kind.isBSS()) {
303 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000304 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000305
Chris Lattner4c6741f2010-03-15 20:37:38 +0000306 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
307 MCSymbol *Sym = Mang->getSymbol(GV);
308 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000309 StringRef Group = "";
310 unsigned Flags = getELFSectionFlags(Kind);
311 if (GV->isWeakForLinker()) {
312 Group = Sym->getName();
313 Flags |= ELF::SHF_GROUP;
314 }
315
Chris Lattner287df1b2010-04-08 21:34:17 +0000316 return getContext().getELFSection(Name.str(),
317 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000318 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000319 }
320
321 if (Kind.isText()) return TextSection;
322
323 if (Kind.isMergeable1ByteCString() ||
324 Kind.isMergeable2ByteCString() ||
325 Kind.isMergeable4ByteCString()) {
326
327 // We also need alignment here.
328 // FIXME: this is getting the alignment of the character, not the
329 // alignment of the global!
330 unsigned Align =
331 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
332
333 const char *SizeSpec = ".rodata.str1.";
334 if (Kind.isMergeable2ByteCString())
335 SizeSpec = ".rodata.str2.";
336 else if (Kind.isMergeable4ByteCString())
337 SizeSpec = ".rodata.str4.";
338 else
339 assert(Kind.isMergeable1ByteCString() && "unknown string width");
340
341
342 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000343 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000344 ELF::SHF_ALLOC |
345 ELF::SHF_MERGE |
346 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000347 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000348 }
349
350 if (Kind.isMergeableConst()) {
351 if (Kind.isMergeableConst4() && MergeableConst4Section)
352 return MergeableConst4Section;
353 if (Kind.isMergeableConst8() && MergeableConst8Section)
354 return MergeableConst8Section;
355 if (Kind.isMergeableConst16() && MergeableConst16Section)
356 return MergeableConst16Section;
357 return ReadOnlySection; // .const
358 }
359
360 if (Kind.isReadOnly()) return ReadOnlySection;
361
362 if (Kind.isThreadData()) return TLSDataSection;
363 if (Kind.isThreadBSS()) return TLSBSSSection;
364
365 // Note: we claim that common symbols are put in BSSSection, but they are
366 // really emitted with the magic .comm directive, which creates a symbol table
367 // entry but not a section.
368 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
369
370 if (Kind.isDataNoRel()) return DataSection;
371 if (Kind.isDataRelLocal()) return DataRelLocalSection;
372 if (Kind.isDataRel()) return DataRelSection;
373 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
374
375 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
376 return DataRelROSection;
377}
378
379/// getSectionForConstant - Given a mergeable constant with the
380/// specified size and relocation information, return a section that it
381/// should be placed in.
382const MCSection *TargetLoweringObjectFileELF::
383getSectionForConstant(SectionKind Kind) const {
384 if (Kind.isMergeableConst4() && MergeableConst4Section)
385 return MergeableConst4Section;
386 if (Kind.isMergeableConst8() && MergeableConst8Section)
387 return MergeableConst8Section;
388 if (Kind.isMergeableConst16() && MergeableConst16Section)
389 return MergeableConst16Section;
390 if (Kind.isReadOnly())
391 return ReadOnlySection;
392
393 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
394 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
395 return DataRelROSection;
396}
397
398const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000399getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
400 MachineModuleInfo *MMI,
401 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000402
403 if (Encoding & dwarf::DW_EH_PE_indirect) {
404 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
405
406 SmallString<128> Name;
407 Mang->getNameWithPrefix(Name, GV, true);
408 Name += ".DW.stub";
409
410 // Add information about the stub reference to ELFMMI so that the stub
411 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000412 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000413 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000414 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000415 MCSymbol *Sym = Mang->getSymbol(GV);
416 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000417 }
418
419 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000420 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000421 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000422 }
423
424 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000425 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000426}
427
428//===----------------------------------------------------------------------===//
429// MachO
430//===----------------------------------------------------------------------===//
431
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000432void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
433 const TargetMachine &TM) {
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000434 // _foo.eh symbols are currently always exported so that the linker knows
435 // about them. This is not necessary on 10.6 and later, but it
436 // doesn't hurt anything.
437 // FIXME: I need to get this from Triple.
Chris Lattner09d53fe2010-03-10 07:20:42 +0000438 IsFunctionEHSymbolGlobal = true;
439 IsFunctionEHFrameSymbolPrivate = false;
440 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000441
442 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
443 if (T.getOS() == Triple::Darwin) {
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000444 switch (T.getDarwinMajorNumber()) {
445 case 7: // 10.3 Panther.
446 case 8: // 10.4 Tiger.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000447 CommDirectiveSupportsAlignment = false;
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000448 break;
449 case 9: // 10.5 Leopard.
450 case 10: // 10.6 SnowLeopard.
451 break;
452 }
Chris Lattner8048ebe2010-09-27 06:44:54 +0000453 }
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000454
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000455 TargetLoweringObjectFile::Initialize(Ctx, TM);
456
457 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000458 = getContext().getMachOSection("__TEXT", "__text",
459 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
460 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000461 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000462 = getContext().getMachOSection("__DATA", "__data", 0,
463 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000464
Eric Christopher423c9e32010-05-17 21:02:07 +0000465 TLSDataSection // .tdata
466 = getContext().getMachOSection("__DATA", "__thread_data",
467 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
468 SectionKind::getDataRel());
469 TLSBSSSection // .tbss
470 = getContext().getMachOSection("__DATA", "__thread_bss",
471 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
472 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000473
Eric Christopher423c9e32010-05-17 21:02:07 +0000474 // TODO: Verify datarel below.
475 TLSTLVSection // .tlv
476 = getContext().getMachOSection("__DATA", "__thread_vars",
477 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
478 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000479
Eric Christopherc6177a42010-05-17 22:53:55 +0000480 TLSThreadInitSection
481 = getContext().getMachOSection("__DATA", "__thread_init",
482 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
483 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000484
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000485 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000486 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000487 MCSectionMachO::S_CSTRING_LITERALS,
488 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000489 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000490 = getContext().getMachOSection("__TEXT","__ustring", 0,
491 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000492 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000493 = getContext().getMachOSection("__TEXT", "__literal4",
494 MCSectionMachO::S_4BYTE_LITERALS,
495 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000496 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000497 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000498 MCSectionMachO::S_8BYTE_LITERALS,
499 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000500
501 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
502 // to using it in -static mode.
503 SixteenByteConstantSection = 0;
504 if (TM.getRelocationModel() != Reloc::Static &&
505 TM.getTargetData()->getPointerSize() == 32)
506 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000507 getContext().getMachOSection("__TEXT", "__literal16",
508 MCSectionMachO::S_16BYTE_LITERALS,
509 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000510
511 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000512 = getContext().getMachOSection("__TEXT", "__const", 0,
513 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000514
515 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000516 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
517 MCSectionMachO::S_COALESCED |
518 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
519 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000520 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000521 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000522 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000523 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000524 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000525 = getContext().getMachOSection("__DATA", "__const", 0,
526 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000527 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000528 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000529 MCSectionMachO::S_COALESCED,
530 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000531 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000532 = getContext().getMachOSection("__DATA","__common",
533 MCSectionMachO::S_ZEROFILL,
534 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000535 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000536 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
537 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000538
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000539
540 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000541 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
542 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
543 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000544 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000545 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
546 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
547 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000548
549 if (TM.getRelocationModel() == Reloc::Static) {
550 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000551 = getContext().getMachOSection("__TEXT", "__constructor", 0,
552 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000553 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000554 = getContext().getMachOSection("__TEXT", "__destructor", 0,
555 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000556 } else {
557 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000558 = getContext().getMachOSection("__DATA", "__mod_init_func",
559 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
560 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000561 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000562 = getContext().getMachOSection("__DATA", "__mod_term_func",
563 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
564 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000565 }
566
567 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000568 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
569 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000570 // Debug Information.
571 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000572 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000573 MCSectionMachO::S_ATTR_DEBUG,
574 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000575 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000576 getContext().getMachOSection("__DWARF", "__debug_info",
577 MCSectionMachO::S_ATTR_DEBUG,
578 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000579 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000580 getContext().getMachOSection("__DWARF", "__debug_line",
581 MCSectionMachO::S_ATTR_DEBUG,
582 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000583 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000584 getContext().getMachOSection("__DWARF", "__debug_frame",
585 MCSectionMachO::S_ATTR_DEBUG,
586 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000587 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000588 getContext().getMachOSection("__DWARF", "__debug_pubnames",
589 MCSectionMachO::S_ATTR_DEBUG,
590 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000591 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000592 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
593 MCSectionMachO::S_ATTR_DEBUG,
594 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000595 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000596 getContext().getMachOSection("__DWARF", "__debug_str",
597 MCSectionMachO::S_ATTR_DEBUG,
598 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000599 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000600 getContext().getMachOSection("__DWARF", "__debug_loc",
601 MCSectionMachO::S_ATTR_DEBUG,
602 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000603 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000604 getContext().getMachOSection("__DWARF", "__debug_aranges",
605 MCSectionMachO::S_ATTR_DEBUG,
606 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000607 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000608 getContext().getMachOSection("__DWARF", "__debug_ranges",
609 MCSectionMachO::S_ATTR_DEBUG,
610 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000611 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000612 getContext().getMachOSection("__DWARF", "__debug_macinfo",
613 MCSectionMachO::S_ATTR_DEBUG,
614 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000615 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000616 getContext().getMachOSection("__DWARF", "__debug_inlined",
617 MCSectionMachO::S_ATTR_DEBUG,
618 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000619
Eric Christopher8116ca52010-05-22 00:10:22 +0000620 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000621}
622
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000623const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
624 return getContext().getMachOSection("__TEXT", "__eh_frame",
625 MCSectionMachO::S_COALESCED |
626 MCSectionMachO::S_ATTR_NO_TOC |
627 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
628 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
629 SectionKind::getReadOnly());
630}
631
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000632const MCSection *TargetLoweringObjectFileMachO::
633getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
634 Mangler *Mang, const TargetMachine &TM) const {
635 // Parse the section specifier and create it if valid.
636 StringRef Segment, Section;
637 unsigned TAA, StubSize;
638 std::string ErrorCode =
639 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
640 TAA, StubSize);
641 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000642 // If invalid, report the error with report_fatal_error.
643 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000644 "' has an invalid section specifier '" + GV->getSection()+
645 "': " + ErrorCode + ".");
646 // Fall back to dropping it into the data section.
647 return DataSection;
648 }
649
650 // Get the section.
651 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000652 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000653
654 // Okay, now that we got the section, verify that the TAA & StubSize agree.
655 // If the user declared multiple globals with different section flags, we need
656 // to reject it here.
657 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000658 // If invalid, report the error with report_fatal_error.
659 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000660 "' section type or attributes does not match previous"
661 " section specifier");
662 }
663
664 return S;
665}
666
667const MCSection *TargetLoweringObjectFileMachO::
668SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000669 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000670
Eric Christopher02b46bc2010-05-25 21:28:50 +0000671 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000672 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000673 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000674
675 if (Kind.isText())
676 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
677
678 // If this is weak/linkonce, put this in a coalescable section, either in text
679 // or data depending on if it is writable.
680 if (GV->isWeakForLinker()) {
681 if (Kind.isReadOnly())
682 return ConstTextCoalSection;
683 return DataCoalSection;
684 }
685
686 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000687 if (Kind.isMergeable1ByteCString() &&
688 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
689 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000690
Chris Lattner98f15d22010-03-07 04:28:09 +0000691 // Do not put 16-bit arrays in the UString section if they have an
692 // externally visible label, this runs into issues with certain linker
693 // versions.
694 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
695 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
696 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000697
698 if (Kind.isMergeableConst()) {
699 if (Kind.isMergeableConst4())
700 return FourByteConstantSection;
701 if (Kind.isMergeableConst8())
702 return EightByteConstantSection;
703 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
704 return SixteenByteConstantSection;
705 }
706
707 // Otherwise, if it is readonly, but not something we can specially optimize,
708 // just drop it in .const.
709 if (Kind.isReadOnly())
710 return ReadOnlySection;
711
712 // If this is marked const, put it into a const section. But if the dynamic
713 // linker needs to write to it, put it in the data segment.
714 if (Kind.isReadOnlyWithRel())
715 return ConstDataSection;
716
717 // Put zero initialized globals with strong external linkage in the
718 // DATA, __common section with the .zerofill directive.
719 if (Kind.isBSSExtern())
720 return DataCommonSection;
721
722 // Put zero initialized globals with local linkage in __DATA,__bss directive
723 // with the .zerofill directive (aka .lcomm).
724 if (Kind.isBSSLocal())
725 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000726
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000727 // Otherwise, just drop the variable in the normal data section.
728 return DataSection;
729}
730
731const MCSection *
732TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
733 // If this constant requires a relocation, we have to put it in the data
734 // segment, not in the text segment.
735 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
736 return ConstDataSection;
737
738 if (Kind.isMergeableConst4())
739 return FourByteConstantSection;
740 if (Kind.isMergeableConst8())
741 return EightByteConstantSection;
742 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
743 return SixteenByteConstantSection;
744 return ReadOnlySection; // .const
745}
746
747/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
748/// not to emit the UsedDirective for some symbols in llvm.used.
749// FIXME: REMOVE this (rdar://7071300)
750bool TargetLoweringObjectFileMachO::
751shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
752 /// On Darwin, internally linked data beginning with "L" or "l" does not have
753 /// the directive emitted (this occurs in ObjC metadata).
754 if (!GV) return false;
755
Bill Wendling07d31772010-06-29 22:34:52 +0000756 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000757 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
758 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000759 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
760 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000761 MCSymbol *Sym = Mang->getSymbol(GV);
762 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000763 return false;
764 }
765
766 return true;
767}
768
769const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000770getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
771 MachineModuleInfo *MMI, unsigned Encoding,
772 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000773 // The mach-o version of this method defaults to returning a stub reference.
774
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000775 if (Encoding & DW_EH_PE_indirect) {
776 MachineModuleInfoMachO &MachOMMI =
777 MMI->getObjFileInfo<MachineModuleInfoMachO>();
778
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000779 SmallString<128> Name;
780 Mang->getNameWithPrefix(Name, GV, true);
781 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000782
783 // Add information about the stub reference to MachOMMI so that the stub
784 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000785 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000786 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000787 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000788 MCSymbol *Sym = Mang->getSymbol(GV);
789 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000790 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000791
792 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000793 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000794 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000795 }
796
797 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000798 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000799}
800
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000801unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
802 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
803}
804
805unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
806 return DW_EH_PE_pcrel;
807}
808
809unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
810 return DW_EH_PE_pcrel;
811}
812
813unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000814 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000815}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000816
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000817//===----------------------------------------------------------------------===//
818// COFF
819//===----------------------------------------------------------------------===//
820
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000821void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
822 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000823 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000824 TextSection =
825 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000826 COFF::IMAGE_SCN_CNT_CODE |
827 COFF::IMAGE_SCN_MEM_EXECUTE |
828 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000829 SectionKind::getText());
830 DataSection =
831 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000832 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
833 COFF::IMAGE_SCN_MEM_READ |
834 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000835 SectionKind::getDataRel());
836 ReadOnlySection =
837 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000838 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
839 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000840 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000841 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000842 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000843 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
844 COFF::IMAGE_SCN_MEM_READ |
845 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000846 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000847 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000848 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000849 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
850 COFF::IMAGE_SCN_MEM_READ |
851 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000852 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000853
854 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
855 // though it contains relocatable pointers. In PIC mode, this is probably a
856 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
857 // adjusted or this should be a data section.
858 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000859 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000860 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
861 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000862 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000863 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000864 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000865 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000866 COFF::IMAGE_SCN_MEM_DISCARDABLE |
867 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000868 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000869 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000870 getContext().getCOFFSection(".debug_info",
Daniel Dunbar94610582010-07-01 20:07:24 +0000871 COFF::IMAGE_SCN_MEM_DISCARDABLE |
872 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000873 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000874 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000875 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000876 COFF::IMAGE_SCN_MEM_DISCARDABLE |
877 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000878 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000879 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000880 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000881 COFF::IMAGE_SCN_MEM_DISCARDABLE |
882 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000883 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000884 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000885 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +0000886 COFF::IMAGE_SCN_MEM_DISCARDABLE |
887 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000888 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000889 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000890 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +0000891 COFF::IMAGE_SCN_MEM_DISCARDABLE |
892 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000893 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000894 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000895 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +0000896 COFF::IMAGE_SCN_MEM_DISCARDABLE |
897 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000898 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000899 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000900 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000901 COFF::IMAGE_SCN_MEM_DISCARDABLE |
902 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000903 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000904 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000905 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000906 COFF::IMAGE_SCN_MEM_DISCARDABLE |
907 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000908 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000909 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000910 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000911 COFF::IMAGE_SCN_MEM_DISCARDABLE |
912 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000913 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000914 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000915 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000916 COFF::IMAGE_SCN_MEM_DISCARDABLE |
917 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000918 SectionKind::getMetadata());
919
920 DrectveSection =
921 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000922 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000923 SectionKind::getMetadata());
924}
925
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000926const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
927 return getContext().getCOFFSection(".eh_frame",
928 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
929 COFF::IMAGE_SCN_MEM_READ |
930 COFF::IMAGE_SCN_MEM_WRITE,
931 SectionKind::getDataRel());
932}
933
934
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000935static unsigned
936getCOFFSectionFlags(SectionKind K) {
937 unsigned Flags = 0;
938
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000939 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000940 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000941 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000942 else if (K.isText())
943 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000944 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000945 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000946 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000947 else if (K.isBSS ())
948 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000949 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
950 COFF::IMAGE_SCN_MEM_READ |
951 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000952 else if (K.isReadOnly())
953 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000954 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
955 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000956 else if (K.isWriteable())
957 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000958 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
959 COFF::IMAGE_SCN_MEM_READ |
960 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000961
962 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000963}
964
965const MCSection *TargetLoweringObjectFileCOFF::
966getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
967 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000968 return getContext().getCOFFSection(GV->getSection(),
969 getCOFFSectionFlags(Kind),
970 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000971}
972
973static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
974 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000975 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000976 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000977 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000978 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000979 return ".data$";
980 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000981}
982
983
984const MCSection *TargetLoweringObjectFileCOFF::
985SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
986 Mangler *Mang, const TargetMachine &TM) const {
987 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
988
989 // If this global is linkonce/weak and the target handles this by emitting it
990 // into a 'uniqued' section name, create and return the section now.
991 if (GV->isWeakForLinker()) {
992 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
993 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000994 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000995 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +0000996
997 unsigned Characteristics = getCOFFSectionFlags(Kind);
998
Daniel Dunbar94610582010-07-01 20:07:24 +0000999 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001000
1001 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001002 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001003 }
1004
1005 if (Kind.isText())
1006 return getTextSection();
1007
1008 return getDataSection();
1009}
1010