blob: 3b01bfdd69444bfa3f420e422ff148959dacc0f2 [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;
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000633 unsigned TAA = (unsigned)MCSectionMachO::SECTION_ATTRIBUTES, StubSize = 0;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000634 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
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000646 bool TAAWasSet = (TAA != MCSectionMachO::SECTION_ATTRIBUTES);
647 if (!TAAWasSet)
648 TAA = 0; // Sensible default if this is a new section.
649
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000650 // 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
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000654 // If TAA wasn't set by ParseSectionSpecifier() above,
655 // use the value returned by getMachOSection() as a default.
656 if (!TAAWasSet)
657 TAA = S->getTypeAndAttributes();
658
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000659 // Okay, now that we got the section, verify that the TAA & StubSize agree.
660 // If the user declared multiple globals with different section flags, we need
661 // to reject it here.
662 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000663 // If invalid, report the error with report_fatal_error.
664 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000665 "' section type or attributes does not match previous"
666 " section specifier");
667 }
668
669 return S;
670}
671
672const MCSection *TargetLoweringObjectFileMachO::
673SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000674 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000675
Eric Christopher02b46bc2010-05-25 21:28:50 +0000676 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000677 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000678 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000679
680 if (Kind.isText())
681 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
682
683 // If this is weak/linkonce, put this in a coalescable section, either in text
684 // or data depending on if it is writable.
685 if (GV->isWeakForLinker()) {
686 if (Kind.isReadOnly())
687 return ConstTextCoalSection;
688 return DataCoalSection;
689 }
690
691 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000692 if (Kind.isMergeable1ByteCString() &&
693 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
694 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000695
Chris Lattner98f15d22010-03-07 04:28:09 +0000696 // Do not put 16-bit arrays in the UString section if they have an
697 // externally visible label, this runs into issues with certain linker
698 // versions.
699 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
700 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
701 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000702
703 if (Kind.isMergeableConst()) {
704 if (Kind.isMergeableConst4())
705 return FourByteConstantSection;
706 if (Kind.isMergeableConst8())
707 return EightByteConstantSection;
708 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
709 return SixteenByteConstantSection;
710 }
711
712 // Otherwise, if it is readonly, but not something we can specially optimize,
713 // just drop it in .const.
714 if (Kind.isReadOnly())
715 return ReadOnlySection;
716
717 // If this is marked const, put it into a const section. But if the dynamic
718 // linker needs to write to it, put it in the data segment.
719 if (Kind.isReadOnlyWithRel())
720 return ConstDataSection;
721
722 // Put zero initialized globals with strong external linkage in the
723 // DATA, __common section with the .zerofill directive.
724 if (Kind.isBSSExtern())
725 return DataCommonSection;
726
727 // Put zero initialized globals with local linkage in __DATA,__bss directive
728 // with the .zerofill directive (aka .lcomm).
729 if (Kind.isBSSLocal())
730 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000731
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000732 // Otherwise, just drop the variable in the normal data section.
733 return DataSection;
734}
735
736const MCSection *
737TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
738 // If this constant requires a relocation, we have to put it in the data
739 // segment, not in the text segment.
740 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
741 return ConstDataSection;
742
743 if (Kind.isMergeableConst4())
744 return FourByteConstantSection;
745 if (Kind.isMergeableConst8())
746 return EightByteConstantSection;
747 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
748 return SixteenByteConstantSection;
749 return ReadOnlySection; // .const
750}
751
752/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
753/// not to emit the UsedDirective for some symbols in llvm.used.
754// FIXME: REMOVE this (rdar://7071300)
755bool TargetLoweringObjectFileMachO::
756shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
757 /// On Darwin, internally linked data beginning with "L" or "l" does not have
758 /// the directive emitted (this occurs in ObjC metadata).
759 if (!GV) return false;
760
Bill Wendling07d31772010-06-29 22:34:52 +0000761 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000762 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
763 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000764 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
765 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000766 MCSymbol *Sym = Mang->getSymbol(GV);
767 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000768 return false;
769 }
770
771 return true;
772}
773
774const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000775getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
776 MachineModuleInfo *MMI, unsigned Encoding,
777 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000778 // The mach-o version of this method defaults to returning a stub reference.
779
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000780 if (Encoding & DW_EH_PE_indirect) {
781 MachineModuleInfoMachO &MachOMMI =
782 MMI->getObjFileInfo<MachineModuleInfoMachO>();
783
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000784 SmallString<128> Name;
785 Mang->getNameWithPrefix(Name, GV, true);
786 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000787
788 // Add information about the stub reference to MachOMMI so that the stub
789 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000790 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000791 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000792 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000793 MCSymbol *Sym = Mang->getSymbol(GV);
794 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000795 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000796
797 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000798 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000799 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000800 }
801
802 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000803 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000804}
805
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000806unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
807 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
808}
809
810unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
811 return DW_EH_PE_pcrel;
812}
813
814unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
815 return DW_EH_PE_pcrel;
816}
817
818unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000819 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000820}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000821
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000822//===----------------------------------------------------------------------===//
823// COFF
824//===----------------------------------------------------------------------===//
825
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000826void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
827 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000828 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000829 TextSection =
830 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000831 COFF::IMAGE_SCN_CNT_CODE |
832 COFF::IMAGE_SCN_MEM_EXECUTE |
833 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000834 SectionKind::getText());
835 DataSection =
836 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000837 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
838 COFF::IMAGE_SCN_MEM_READ |
839 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000840 SectionKind::getDataRel());
841 ReadOnlySection =
842 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000843 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
844 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000845 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000846 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000847 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000848 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
849 COFF::IMAGE_SCN_MEM_READ |
850 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000851 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000852 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000853 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000854 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
855 COFF::IMAGE_SCN_MEM_READ |
856 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000857 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000858
859 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
860 // though it contains relocatable pointers. In PIC mode, this is probably a
861 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
862 // adjusted or this should be a data section.
863 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000864 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000865 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
866 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000867 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000868 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000869 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000870 getContext().getCOFFSection(".debug_abbrev",
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 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000875 getContext().getCOFFSection(".debug_info",
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 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000880 getContext().getCOFFSection(".debug_line",
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 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000885 getContext().getCOFFSection(".debug_frame",
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 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000890 getContext().getCOFFSection(".debug_pubnames",
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 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000895 getContext().getCOFFSection(".debug_pubtypes",
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 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000900 getContext().getCOFFSection(".debug_str",
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 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000905 getContext().getCOFFSection(".debug_loc",
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 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000910 getContext().getCOFFSection(".debug_aranges",
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 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000915 getContext().getCOFFSection(".debug_ranges",
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());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000919 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000920 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000921 COFF::IMAGE_SCN_MEM_DISCARDABLE |
922 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000923 SectionKind::getMetadata());
924
925 DrectveSection =
926 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000927 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000928 SectionKind::getMetadata());
929}
930
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000931const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
932 return getContext().getCOFFSection(".eh_frame",
933 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
934 COFF::IMAGE_SCN_MEM_READ |
935 COFF::IMAGE_SCN_MEM_WRITE,
936 SectionKind::getDataRel());
937}
938
939
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000940static unsigned
941getCOFFSectionFlags(SectionKind K) {
942 unsigned Flags = 0;
943
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000944 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000945 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000946 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000947 else if (K.isText())
948 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000949 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000950 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000951 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000952 else if (K.isBSS ())
953 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000954 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
955 COFF::IMAGE_SCN_MEM_READ |
956 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000957 else if (K.isReadOnly())
958 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000959 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
960 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000961 else if (K.isWriteable())
962 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000963 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
964 COFF::IMAGE_SCN_MEM_READ |
965 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000966
967 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000968}
969
970const MCSection *TargetLoweringObjectFileCOFF::
971getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
972 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000973 return getContext().getCOFFSection(GV->getSection(),
974 getCOFFSectionFlags(Kind),
975 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000976}
977
978static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
979 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000980 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000981 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000982 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000983 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000984 return ".data$";
985 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000986}
987
988
989const MCSection *TargetLoweringObjectFileCOFF::
990SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
991 Mangler *Mang, const TargetMachine &TM) const {
992 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
993
994 // If this global is linkonce/weak and the target handles this by emitting it
995 // into a 'uniqued' section name, create and return the section now.
996 if (GV->isWeakForLinker()) {
997 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
998 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +0000999 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001000 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001001
1002 unsigned Characteristics = getCOFFSectionFlags(Kind);
1003
Daniel Dunbar94610582010-07-01 20:07:24 +00001004 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001005
1006 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001007 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001008 }
1009
1010 if (Kind.isText())
1011 return getTextSection();
1012
1013 return getDataSection();
1014}
1015