blob: 0b7bd98cc692356b85aa823d31029689f6a896ae [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 Wendling50282492011-02-18 21:12:58 +0000444 unsigned MajNum = T.getDarwinMajorNumber();
445 if (MajNum == 7 || MajNum == 8) // 10.3 Panther, 10.4 Tiger
Chris Lattner8048ebe2010-09-27 06:44:54 +0000446 CommDirectiveSupportsAlignment = false;
Bill Wendling50282492011-02-18 21:12:58 +0000447 if (MajNum > 9) // 10.6 SnowLeopard
448 IsFunctionEHSymbolGlobal = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000449 }
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000450
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000451 TargetLoweringObjectFile::Initialize(Ctx, TM);
452
453 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000454 = getContext().getMachOSection("__TEXT", "__text",
455 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
456 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000457 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000458 = getContext().getMachOSection("__DATA", "__data", 0,
459 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000460
Eric Christopher423c9e32010-05-17 21:02:07 +0000461 TLSDataSection // .tdata
462 = getContext().getMachOSection("__DATA", "__thread_data",
463 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
464 SectionKind::getDataRel());
465 TLSBSSSection // .tbss
466 = getContext().getMachOSection("__DATA", "__thread_bss",
467 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
468 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000469
Eric Christopher423c9e32010-05-17 21:02:07 +0000470 // TODO: Verify datarel below.
471 TLSTLVSection // .tlv
472 = getContext().getMachOSection("__DATA", "__thread_vars",
473 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
474 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000475
Eric Christopherc6177a42010-05-17 22:53:55 +0000476 TLSThreadInitSection
477 = getContext().getMachOSection("__DATA", "__thread_init",
478 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
479 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000480
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000481 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000482 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000483 MCSectionMachO::S_CSTRING_LITERALS,
484 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000485 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000486 = getContext().getMachOSection("__TEXT","__ustring", 0,
487 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000488 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000489 = getContext().getMachOSection("__TEXT", "__literal4",
490 MCSectionMachO::S_4BYTE_LITERALS,
491 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000492 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000493 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000494 MCSectionMachO::S_8BYTE_LITERALS,
495 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000496
497 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
498 // to using it in -static mode.
499 SixteenByteConstantSection = 0;
500 if (TM.getRelocationModel() != Reloc::Static &&
501 TM.getTargetData()->getPointerSize() == 32)
502 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000503 getContext().getMachOSection("__TEXT", "__literal16",
504 MCSectionMachO::S_16BYTE_LITERALS,
505 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000506
507 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000508 = getContext().getMachOSection("__TEXT", "__const", 0,
509 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000510
511 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000512 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
513 MCSectionMachO::S_COALESCED |
514 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
515 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000516 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000517 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000518 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000519 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000520 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000521 = getContext().getMachOSection("__DATA", "__const", 0,
522 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000523 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000524 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000525 MCSectionMachO::S_COALESCED,
526 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000527 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000528 = getContext().getMachOSection("__DATA","__common",
529 MCSectionMachO::S_ZEROFILL,
530 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000531 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000532 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
533 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000534
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000535
536 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000537 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
538 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
539 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000540 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000541 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
542 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
543 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000544
545 if (TM.getRelocationModel() == Reloc::Static) {
546 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000547 = getContext().getMachOSection("__TEXT", "__constructor", 0,
548 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000549 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000550 = getContext().getMachOSection("__TEXT", "__destructor", 0,
551 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000552 } else {
553 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000554 = getContext().getMachOSection("__DATA", "__mod_init_func",
555 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
556 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000557 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000558 = getContext().getMachOSection("__DATA", "__mod_term_func",
559 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
560 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000561 }
562
563 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000564 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
565 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000566 // Debug Information.
567 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000568 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000569 MCSectionMachO::S_ATTR_DEBUG,
570 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000571 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000572 getContext().getMachOSection("__DWARF", "__debug_info",
573 MCSectionMachO::S_ATTR_DEBUG,
574 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000575 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000576 getContext().getMachOSection("__DWARF", "__debug_line",
577 MCSectionMachO::S_ATTR_DEBUG,
578 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000579 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000580 getContext().getMachOSection("__DWARF", "__debug_frame",
581 MCSectionMachO::S_ATTR_DEBUG,
582 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000583 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000584 getContext().getMachOSection("__DWARF", "__debug_pubnames",
585 MCSectionMachO::S_ATTR_DEBUG,
586 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000587 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000588 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
589 MCSectionMachO::S_ATTR_DEBUG,
590 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000591 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000592 getContext().getMachOSection("__DWARF", "__debug_str",
593 MCSectionMachO::S_ATTR_DEBUG,
594 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000595 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000596 getContext().getMachOSection("__DWARF", "__debug_loc",
597 MCSectionMachO::S_ATTR_DEBUG,
598 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000599 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000600 getContext().getMachOSection("__DWARF", "__debug_aranges",
601 MCSectionMachO::S_ATTR_DEBUG,
602 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000603 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000604 getContext().getMachOSection("__DWARF", "__debug_ranges",
605 MCSectionMachO::S_ATTR_DEBUG,
606 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000607 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000608 getContext().getMachOSection("__DWARF", "__debug_macinfo",
609 MCSectionMachO::S_ATTR_DEBUG,
610 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000611 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000612 getContext().getMachOSection("__DWARF", "__debug_inlined",
613 MCSectionMachO::S_ATTR_DEBUG,
614 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000615
Eric Christopher8116ca52010-05-22 00:10:22 +0000616 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000617}
618
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000619const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
620 return getContext().getMachOSection("__TEXT", "__eh_frame",
621 MCSectionMachO::S_COALESCED |
622 MCSectionMachO::S_ATTR_NO_TOC |
623 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
624 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
625 SectionKind::getReadOnly());
626}
627
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000628const MCSection *TargetLoweringObjectFileMachO::
629getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
630 Mangler *Mang, const TargetMachine &TM) const {
631 // Parse the section specifier and create it if valid.
632 StringRef Segment, Section;
633 unsigned TAA, StubSize;
634 std::string ErrorCode =
635 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
636 TAA, StubSize);
637 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000638 // If invalid, report the error with report_fatal_error.
639 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000640 "' has an invalid section specifier '" + GV->getSection()+
641 "': " + ErrorCode + ".");
642 // Fall back to dropping it into the data section.
643 return DataSection;
644 }
645
646 // Get the section.
647 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000648 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000649
650 // Okay, now that we got the section, verify that the TAA & StubSize agree.
651 // If the user declared multiple globals with different section flags, we need
652 // to reject it here.
653 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000654 // If invalid, report the error with report_fatal_error.
655 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000656 "' section type or attributes does not match previous"
657 " section specifier");
658 }
659
660 return S;
661}
662
663const MCSection *TargetLoweringObjectFileMachO::
664SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000665 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000666
Eric Christopher02b46bc2010-05-25 21:28:50 +0000667 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000668 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000669 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000670
671 if (Kind.isText())
672 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
673
674 // If this is weak/linkonce, put this in a coalescable section, either in text
675 // or data depending on if it is writable.
676 if (GV->isWeakForLinker()) {
677 if (Kind.isReadOnly())
678 return ConstTextCoalSection;
679 return DataCoalSection;
680 }
681
682 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000683 if (Kind.isMergeable1ByteCString() &&
684 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
685 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000686
Chris Lattner98f15d22010-03-07 04:28:09 +0000687 // Do not put 16-bit arrays in the UString section if they have an
688 // externally visible label, this runs into issues with certain linker
689 // versions.
690 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
691 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
692 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000693
694 if (Kind.isMergeableConst()) {
695 if (Kind.isMergeableConst4())
696 return FourByteConstantSection;
697 if (Kind.isMergeableConst8())
698 return EightByteConstantSection;
699 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
700 return SixteenByteConstantSection;
701 }
702
703 // Otherwise, if it is readonly, but not something we can specially optimize,
704 // just drop it in .const.
705 if (Kind.isReadOnly())
706 return ReadOnlySection;
707
708 // If this is marked const, put it into a const section. But if the dynamic
709 // linker needs to write to it, put it in the data segment.
710 if (Kind.isReadOnlyWithRel())
711 return ConstDataSection;
712
713 // Put zero initialized globals with strong external linkage in the
714 // DATA, __common section with the .zerofill directive.
715 if (Kind.isBSSExtern())
716 return DataCommonSection;
717
718 // Put zero initialized globals with local linkage in __DATA,__bss directive
719 // with the .zerofill directive (aka .lcomm).
720 if (Kind.isBSSLocal())
721 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000722
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000723 // Otherwise, just drop the variable in the normal data section.
724 return DataSection;
725}
726
727const MCSection *
728TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
729 // If this constant requires a relocation, we have to put it in the data
730 // segment, not in the text segment.
731 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
732 return ConstDataSection;
733
734 if (Kind.isMergeableConst4())
735 return FourByteConstantSection;
736 if (Kind.isMergeableConst8())
737 return EightByteConstantSection;
738 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
739 return SixteenByteConstantSection;
740 return ReadOnlySection; // .const
741}
742
743/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
744/// not to emit the UsedDirective for some symbols in llvm.used.
745// FIXME: REMOVE this (rdar://7071300)
746bool TargetLoweringObjectFileMachO::
747shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
748 /// On Darwin, internally linked data beginning with "L" or "l" does not have
749 /// the directive emitted (this occurs in ObjC metadata).
750 if (!GV) return false;
751
Bill Wendling07d31772010-06-29 22:34:52 +0000752 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000753 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
754 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000755 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
756 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000757 MCSymbol *Sym = Mang->getSymbol(GV);
758 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000759 return false;
760 }
761
762 return true;
763}
764
765const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000766getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
767 MachineModuleInfo *MMI, unsigned Encoding,
768 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000769 // The mach-o version of this method defaults to returning a stub reference.
770
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000771 if (Encoding & DW_EH_PE_indirect) {
772 MachineModuleInfoMachO &MachOMMI =
773 MMI->getObjFileInfo<MachineModuleInfoMachO>();
774
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000775 SmallString<128> Name;
776 Mang->getNameWithPrefix(Name, GV, true);
777 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000778
779 // Add information about the stub reference to MachOMMI so that the stub
780 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000781 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000782 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000783 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000784 MCSymbol *Sym = Mang->getSymbol(GV);
785 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000786 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000787
788 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000789 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000790 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000791 }
792
793 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000794 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000795}
796
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000797unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
798 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
799}
800
801unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
802 return DW_EH_PE_pcrel;
803}
804
805unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
806 return DW_EH_PE_pcrel;
807}
808
809unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000810 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000811}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000812
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000813//===----------------------------------------------------------------------===//
814// COFF
815//===----------------------------------------------------------------------===//
816
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000817void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
818 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000819 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000820 TextSection =
821 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000822 COFF::IMAGE_SCN_CNT_CODE |
823 COFF::IMAGE_SCN_MEM_EXECUTE |
824 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000825 SectionKind::getText());
826 DataSection =
827 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000828 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
829 COFF::IMAGE_SCN_MEM_READ |
830 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000831 SectionKind::getDataRel());
832 ReadOnlySection =
833 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000834 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
835 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000836 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000837 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000838 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000839 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
840 COFF::IMAGE_SCN_MEM_READ |
841 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000842 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000843 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000844 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000845 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
846 COFF::IMAGE_SCN_MEM_READ |
847 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000848 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000849
850 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
851 // though it contains relocatable pointers. In PIC mode, this is probably a
852 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
853 // adjusted or this should be a data section.
854 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000855 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000856 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
857 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000858 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000859 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000860 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000861 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000862 COFF::IMAGE_SCN_MEM_DISCARDABLE |
863 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000864 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000865 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000866 getContext().getCOFFSection(".debug_info",
Daniel Dunbar94610582010-07-01 20:07:24 +0000867 COFF::IMAGE_SCN_MEM_DISCARDABLE |
868 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000869 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000870 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000871 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000872 COFF::IMAGE_SCN_MEM_DISCARDABLE |
873 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000874 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000875 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000876 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000877 COFF::IMAGE_SCN_MEM_DISCARDABLE |
878 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000879 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000880 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000881 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +0000882 COFF::IMAGE_SCN_MEM_DISCARDABLE |
883 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000884 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000885 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000886 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +0000887 COFF::IMAGE_SCN_MEM_DISCARDABLE |
888 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000889 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000890 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000891 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +0000892 COFF::IMAGE_SCN_MEM_DISCARDABLE |
893 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000894 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000895 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000896 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000897 COFF::IMAGE_SCN_MEM_DISCARDABLE |
898 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000899 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000900 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000901 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000902 COFF::IMAGE_SCN_MEM_DISCARDABLE |
903 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000904 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000905 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000906 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000907 COFF::IMAGE_SCN_MEM_DISCARDABLE |
908 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000909 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000910 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000911 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000912 COFF::IMAGE_SCN_MEM_DISCARDABLE |
913 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000914 SectionKind::getMetadata());
915
916 DrectveSection =
917 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000918 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000919 SectionKind::getMetadata());
920}
921
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000922const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
923 return getContext().getCOFFSection(".eh_frame",
924 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
925 COFF::IMAGE_SCN_MEM_READ |
926 COFF::IMAGE_SCN_MEM_WRITE,
927 SectionKind::getDataRel());
928}
929
930
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000931static unsigned
932getCOFFSectionFlags(SectionKind K) {
933 unsigned Flags = 0;
934
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000935 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000936 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000937 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000938 else if (K.isText())
939 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000940 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000941 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000942 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000943 else if (K.isBSS ())
944 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000945 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
946 COFF::IMAGE_SCN_MEM_READ |
947 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000948 else if (K.isReadOnly())
949 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000950 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
951 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000952 else if (K.isWriteable())
953 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000954 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
955 COFF::IMAGE_SCN_MEM_READ |
956 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000957
958 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000959}
960
961const MCSection *TargetLoweringObjectFileCOFF::
962getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
963 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000964 return getContext().getCOFFSection(GV->getSection(),
965 getCOFFSectionFlags(Kind),
966 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000967}
968
969static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
970 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000971 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000972 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000973 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000974 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000975 return ".data$";
976 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000977}
978
979
980const MCSection *TargetLoweringObjectFileCOFF::
981SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
982 Mangler *Mang, const TargetMachine &TM) const {
983 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
984
985 // If this global is linkonce/weak and the target handles this by emitting it
986 // into a 'uniqued' section name, create and return the section now.
987 if (GV->isWeakForLinker()) {
988 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
989 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000990 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000991 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +0000992
993 unsigned Characteristics = getCOFFSectionFlags(Kind);
994
Daniel Dunbar94610582010-07-01 20:07:24 +0000995 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000996
997 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +0000998 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000999 }
1000
1001 if (Kind.isText())
1002 return getTextSection();
1003
1004 return getDataSection();
1005}
1006