blob: fa311dc5d66c5b4fb33011d4a4458b853366fe24 [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) {
Rafael Espindola0412d5b2011-02-24 20:18:01 +0000181 // FIXME: Why is this here? Codegen is should not be in the business
182 // of figuring section flags. If the user wrote section(".eh_frame"),
183 // we should just pass that to MC which will defer to the assembly
184 // or use its default if producing an object file.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000185 if (Name.empty() || Name[0] != '.') return K;
186
187 // Some lame default implementation based on some magic section names.
188 if (Name == ".bss" ||
189 Name.startswith(".bss.") ||
190 Name.startswith(".gnu.linkonce.b.") ||
191 Name.startswith(".llvm.linkonce.b.") ||
192 Name == ".sbss" ||
193 Name.startswith(".sbss.") ||
194 Name.startswith(".gnu.linkonce.sb.") ||
195 Name.startswith(".llvm.linkonce.sb."))
196 return SectionKind::getBSS();
197
198 if (Name == ".tdata" ||
199 Name.startswith(".tdata.") ||
200 Name.startswith(".gnu.linkonce.td.") ||
201 Name.startswith(".llvm.linkonce.td."))
202 return SectionKind::getThreadData();
203
204 if (Name == ".tbss" ||
205 Name.startswith(".tbss.") ||
206 Name.startswith(".gnu.linkonce.tb.") ||
207 Name.startswith(".llvm.linkonce.tb."))
208 return SectionKind::getThreadBSS();
209
Rafael Espindola0412d5b2011-02-24 20:18:01 +0000210 if (Name == ".eh_frame")
211 return SectionKind::getDataRel();
212
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000213 return K;
214}
215
216
217static unsigned getELFSectionType(StringRef Name, SectionKind K) {
218
219 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000220 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000221
222 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000223 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000224
225 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000226 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000227
228 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000229 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000230
Rafael Espindolac85dca62011-01-23 04:28:49 +0000231 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000232}
233
234
235static unsigned
236getELFSectionFlags(SectionKind K) {
237 unsigned Flags = 0;
238
239 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000240 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000241
242 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000243 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000244
245 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000246 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000247
248 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000249 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000250
251 // K.isMergeableConst() is left out to honour PR4650
252 if (K.isMergeableCString() || K.isMergeableConst4() ||
253 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000254 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000255
256 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000257 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000258
259 return Flags;
260}
261
262
263const MCSection *TargetLoweringObjectFileELF::
264getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
265 Mangler *Mang, const TargetMachine &TM) const {
266 StringRef SectionName = GV->getSection();
267
268 // Infer section flags from the section name if we can.
269 Kind = getELFKindForNamedSection(SectionName, Kind);
270
Chris Lattner287df1b2010-04-08 21:34:17 +0000271 return getContext().getELFSection(SectionName,
272 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000273 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000274}
275
Chris Lattner43ac7212010-04-13 00:36:43 +0000276/// getSectionPrefixForGlobal - Return the section prefix name used by options
277/// FunctionsSections and DataSections.
278static const char *getSectionPrefixForGlobal(SectionKind Kind) {
279 if (Kind.isText()) return ".text.";
280 if (Kind.isReadOnly()) return ".rodata.";
281
282 if (Kind.isThreadData()) return ".tdata.";
283 if (Kind.isThreadBSS()) return ".tbss.";
284
285 if (Kind.isDataNoRel()) return ".data.";
286 if (Kind.isDataRelLocal()) return ".data.rel.local.";
287 if (Kind.isDataRel()) return ".data.rel.";
288 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
289
290 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
291 return ".data.rel.ro.";
292}
293
294
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000295const MCSection *TargetLoweringObjectFileELF::
296SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
297 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000298 // If we have -ffunction-section or -fdata-section then we should emit the
299 // global value to a uniqued section specifically for it.
300 bool EmitUniquedSection;
301 if (Kind.isText())
302 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000303 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000304 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000305
306 // If this global is linkonce/weak and the target handles this by emitting it
307 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000308 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
309 !Kind.isCommon() && !Kind.isBSS()) {
310 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000311 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000312
Chris Lattner4c6741f2010-03-15 20:37:38 +0000313 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
314 MCSymbol *Sym = Mang->getSymbol(GV);
315 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000316 StringRef Group = "";
317 unsigned Flags = getELFSectionFlags(Kind);
318 if (GV->isWeakForLinker()) {
319 Group = Sym->getName();
320 Flags |= ELF::SHF_GROUP;
321 }
322
Chris Lattner287df1b2010-04-08 21:34:17 +0000323 return getContext().getELFSection(Name.str(),
324 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000325 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000326 }
327
328 if (Kind.isText()) return TextSection;
329
330 if (Kind.isMergeable1ByteCString() ||
331 Kind.isMergeable2ByteCString() ||
332 Kind.isMergeable4ByteCString()) {
333
334 // We also need alignment here.
335 // FIXME: this is getting the alignment of the character, not the
336 // alignment of the global!
337 unsigned Align =
338 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
339
340 const char *SizeSpec = ".rodata.str1.";
341 if (Kind.isMergeable2ByteCString())
342 SizeSpec = ".rodata.str2.";
343 else if (Kind.isMergeable4ByteCString())
344 SizeSpec = ".rodata.str4.";
345 else
346 assert(Kind.isMergeable1ByteCString() && "unknown string width");
347
348
349 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000350 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000351 ELF::SHF_ALLOC |
352 ELF::SHF_MERGE |
353 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000354 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000355 }
356
357 if (Kind.isMergeableConst()) {
358 if (Kind.isMergeableConst4() && MergeableConst4Section)
359 return MergeableConst4Section;
360 if (Kind.isMergeableConst8() && MergeableConst8Section)
361 return MergeableConst8Section;
362 if (Kind.isMergeableConst16() && MergeableConst16Section)
363 return MergeableConst16Section;
364 return ReadOnlySection; // .const
365 }
366
367 if (Kind.isReadOnly()) return ReadOnlySection;
368
369 if (Kind.isThreadData()) return TLSDataSection;
370 if (Kind.isThreadBSS()) return TLSBSSSection;
371
372 // Note: we claim that common symbols are put in BSSSection, but they are
373 // really emitted with the magic .comm directive, which creates a symbol table
374 // entry but not a section.
375 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
376
377 if (Kind.isDataNoRel()) return DataSection;
378 if (Kind.isDataRelLocal()) return DataRelLocalSection;
379 if (Kind.isDataRel()) return DataRelSection;
380 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
381
382 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
383 return DataRelROSection;
384}
385
386/// getSectionForConstant - Given a mergeable constant with the
387/// specified size and relocation information, return a section that it
388/// should be placed in.
389const MCSection *TargetLoweringObjectFileELF::
390getSectionForConstant(SectionKind Kind) const {
391 if (Kind.isMergeableConst4() && MergeableConst4Section)
392 return MergeableConst4Section;
393 if (Kind.isMergeableConst8() && MergeableConst8Section)
394 return MergeableConst8Section;
395 if (Kind.isMergeableConst16() && MergeableConst16Section)
396 return MergeableConst16Section;
397 if (Kind.isReadOnly())
398 return ReadOnlySection;
399
400 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
401 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
402 return DataRelROSection;
403}
404
405const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000406getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
407 MachineModuleInfo *MMI,
408 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000409
410 if (Encoding & dwarf::DW_EH_PE_indirect) {
411 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
412
413 SmallString<128> Name;
414 Mang->getNameWithPrefix(Name, GV, true);
415 Name += ".DW.stub";
416
417 // Add information about the stub reference to ELFMMI so that the stub
418 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000419 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000420 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000421 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000422 MCSymbol *Sym = Mang->getSymbol(GV);
423 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000424 }
425
426 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000427 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner3192d142010-03-11 19:41:58 +0000428 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000429 }
430
431 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000432 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000433}
434
435//===----------------------------------------------------------------------===//
436// MachO
437//===----------------------------------------------------------------------===//
438
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000439void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
440 const TargetMachine &TM) {
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000441 // _foo.eh symbols are currently always exported so that the linker knows
442 // about them. This is not necessary on 10.6 and later, but it
443 // doesn't hurt anything.
444 // FIXME: I need to get this from Triple.
Chris Lattner09d53fe2010-03-10 07:20:42 +0000445 IsFunctionEHSymbolGlobal = true;
446 IsFunctionEHFrameSymbolPrivate = false;
447 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000448
449 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
450 if (T.getOS() == Triple::Darwin) {
Eric Christopherf8597eb2011-02-21 23:52:19 +0000451 switch (T.getDarwinMajorNumber()) {
452 case 7: // 10.3 Panther.
453 case 8: // 10.4 Tiger.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000454 CommDirectiveSupportsAlignment = false;
Eric Christopherf8597eb2011-02-21 23:52:19 +0000455 break;
456 case 9: // 10.5 Leopard.
457 case 10: // 10.6 SnowLeopard.
458 break;
459 }
Chris Lattner8048ebe2010-09-27 06:44:54 +0000460 }
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000461
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000462 TargetLoweringObjectFile::Initialize(Ctx, TM);
463
464 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000465 = getContext().getMachOSection("__TEXT", "__text",
466 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
467 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000468 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000469 = getContext().getMachOSection("__DATA", "__data", 0,
470 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000471
Eric Christopher423c9e32010-05-17 21:02:07 +0000472 TLSDataSection // .tdata
473 = getContext().getMachOSection("__DATA", "__thread_data",
474 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
475 SectionKind::getDataRel());
476 TLSBSSSection // .tbss
477 = getContext().getMachOSection("__DATA", "__thread_bss",
478 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
479 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000480
Eric Christopher423c9e32010-05-17 21:02:07 +0000481 // TODO: Verify datarel below.
482 TLSTLVSection // .tlv
483 = getContext().getMachOSection("__DATA", "__thread_vars",
484 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
485 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000486
Eric Christopherc6177a42010-05-17 22:53:55 +0000487 TLSThreadInitSection
488 = getContext().getMachOSection("__DATA", "__thread_init",
489 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
490 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000491
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000492 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000493 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000494 MCSectionMachO::S_CSTRING_LITERALS,
495 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000496 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000497 = getContext().getMachOSection("__TEXT","__ustring", 0,
498 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000499 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000500 = getContext().getMachOSection("__TEXT", "__literal4",
501 MCSectionMachO::S_4BYTE_LITERALS,
502 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000503 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000504 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000505 MCSectionMachO::S_8BYTE_LITERALS,
506 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000507
508 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
509 // to using it in -static mode.
510 SixteenByteConstantSection = 0;
511 if (TM.getRelocationModel() != Reloc::Static &&
512 TM.getTargetData()->getPointerSize() == 32)
513 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000514 getContext().getMachOSection("__TEXT", "__literal16",
515 MCSectionMachO::S_16BYTE_LITERALS,
516 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000517
518 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000519 = getContext().getMachOSection("__TEXT", "__const", 0,
520 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000521
522 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000523 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
524 MCSectionMachO::S_COALESCED |
525 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
526 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000527 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000528 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000529 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000530 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000531 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000532 = getContext().getMachOSection("__DATA", "__const", 0,
533 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000534 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000535 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000536 MCSectionMachO::S_COALESCED,
537 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000538 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000539 = getContext().getMachOSection("__DATA","__common",
540 MCSectionMachO::S_ZEROFILL,
541 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000542 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000543 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
544 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000545
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000546
547 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000548 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
549 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
550 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000551 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000552 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
553 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
554 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000555
556 if (TM.getRelocationModel() == Reloc::Static) {
557 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000558 = getContext().getMachOSection("__TEXT", "__constructor", 0,
559 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000560 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000561 = getContext().getMachOSection("__TEXT", "__destructor", 0,
562 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000563 } else {
564 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000565 = getContext().getMachOSection("__DATA", "__mod_init_func",
566 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
567 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000568 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000569 = getContext().getMachOSection("__DATA", "__mod_term_func",
570 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
571 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000572 }
573
574 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000575 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
576 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000577 // Debug Information.
578 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000579 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000580 MCSectionMachO::S_ATTR_DEBUG,
581 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000582 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000583 getContext().getMachOSection("__DWARF", "__debug_info",
584 MCSectionMachO::S_ATTR_DEBUG,
585 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000586 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000587 getContext().getMachOSection("__DWARF", "__debug_line",
588 MCSectionMachO::S_ATTR_DEBUG,
589 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000590 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000591 getContext().getMachOSection("__DWARF", "__debug_frame",
592 MCSectionMachO::S_ATTR_DEBUG,
593 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000594 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000595 getContext().getMachOSection("__DWARF", "__debug_pubnames",
596 MCSectionMachO::S_ATTR_DEBUG,
597 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000598 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000599 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
600 MCSectionMachO::S_ATTR_DEBUG,
601 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000602 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000603 getContext().getMachOSection("__DWARF", "__debug_str",
604 MCSectionMachO::S_ATTR_DEBUG,
605 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000606 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000607 getContext().getMachOSection("__DWARF", "__debug_loc",
608 MCSectionMachO::S_ATTR_DEBUG,
609 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000610 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000611 getContext().getMachOSection("__DWARF", "__debug_aranges",
612 MCSectionMachO::S_ATTR_DEBUG,
613 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000614 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000615 getContext().getMachOSection("__DWARF", "__debug_ranges",
616 MCSectionMachO::S_ATTR_DEBUG,
617 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000618 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000619 getContext().getMachOSection("__DWARF", "__debug_macinfo",
620 MCSectionMachO::S_ATTR_DEBUG,
621 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000622 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000623 getContext().getMachOSection("__DWARF", "__debug_inlined",
624 MCSectionMachO::S_ATTR_DEBUG,
625 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000626
Eric Christopher8116ca52010-05-22 00:10:22 +0000627 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000628}
629
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000630const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
631 return getContext().getMachOSection("__TEXT", "__eh_frame",
632 MCSectionMachO::S_COALESCED |
633 MCSectionMachO::S_ATTR_NO_TOC |
634 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
635 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
636 SectionKind::getReadOnly());
637}
638
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000639const MCSection *TargetLoweringObjectFileMachO::
640getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
641 Mangler *Mang, const TargetMachine &TM) const {
642 // Parse the section specifier and create it if valid.
643 StringRef Segment, Section;
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000644 unsigned TAA = (unsigned)MCSectionMachO::SECTION_ATTRIBUTES, StubSize = 0;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000645 std::string ErrorCode =
646 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
647 TAA, StubSize);
648 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000649 // If invalid, report the error with report_fatal_error.
650 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000651 "' has an invalid section specifier '" + GV->getSection()+
652 "': " + ErrorCode + ".");
653 // Fall back to dropping it into the data section.
654 return DataSection;
655 }
656
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000657 bool TAAWasSet = (TAA != MCSectionMachO::SECTION_ATTRIBUTES);
658 if (!TAAWasSet)
659 TAA = 0; // Sensible default if this is a new section.
660
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000661 // Get the section.
662 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000663 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000664
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000665 // If TAA wasn't set by ParseSectionSpecifier() above,
666 // use the value returned by getMachOSection() as a default.
667 if (!TAAWasSet)
668 TAA = S->getTypeAndAttributes();
669
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000670 // Okay, now that we got the section, verify that the TAA & StubSize agree.
671 // If the user declared multiple globals with different section flags, we need
672 // to reject it here.
673 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000674 // If invalid, report the error with report_fatal_error.
675 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000676 "' section type or attributes does not match previous"
677 " section specifier");
678 }
679
680 return S;
681}
682
683const MCSection *TargetLoweringObjectFileMachO::
684SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000685 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000686
Eric Christopher02b46bc2010-05-25 21:28:50 +0000687 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000688 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000689 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000690
691 if (Kind.isText())
692 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
693
694 // If this is weak/linkonce, put this in a coalescable section, either in text
695 // or data depending on if it is writable.
696 if (GV->isWeakForLinker()) {
697 if (Kind.isReadOnly())
698 return ConstTextCoalSection;
699 return DataCoalSection;
700 }
701
702 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000703 if (Kind.isMergeable1ByteCString() &&
704 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
705 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000706
Chris Lattner98f15d22010-03-07 04:28:09 +0000707 // Do not put 16-bit arrays in the UString section if they have an
708 // externally visible label, this runs into issues with certain linker
709 // versions.
710 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
711 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
712 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000713
714 if (Kind.isMergeableConst()) {
715 if (Kind.isMergeableConst4())
716 return FourByteConstantSection;
717 if (Kind.isMergeableConst8())
718 return EightByteConstantSection;
719 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
720 return SixteenByteConstantSection;
721 }
722
723 // Otherwise, if it is readonly, but not something we can specially optimize,
724 // just drop it in .const.
725 if (Kind.isReadOnly())
726 return ReadOnlySection;
727
728 // If this is marked const, put it into a const section. But if the dynamic
729 // linker needs to write to it, put it in the data segment.
730 if (Kind.isReadOnlyWithRel())
731 return ConstDataSection;
732
733 // Put zero initialized globals with strong external linkage in the
734 // DATA, __common section with the .zerofill directive.
735 if (Kind.isBSSExtern())
736 return DataCommonSection;
737
738 // Put zero initialized globals with local linkage in __DATA,__bss directive
739 // with the .zerofill directive (aka .lcomm).
740 if (Kind.isBSSLocal())
741 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000742
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000743 // Otherwise, just drop the variable in the normal data section.
744 return DataSection;
745}
746
747const MCSection *
748TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
749 // If this constant requires a relocation, we have to put it in the data
750 // segment, not in the text segment.
751 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
752 return ConstDataSection;
753
754 if (Kind.isMergeableConst4())
755 return FourByteConstantSection;
756 if (Kind.isMergeableConst8())
757 return EightByteConstantSection;
758 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
759 return SixteenByteConstantSection;
760 return ReadOnlySection; // .const
761}
762
763/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
764/// not to emit the UsedDirective for some symbols in llvm.used.
765// FIXME: REMOVE this (rdar://7071300)
766bool TargetLoweringObjectFileMachO::
767shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
768 /// On Darwin, internally linked data beginning with "L" or "l" does not have
769 /// the directive emitted (this occurs in ObjC metadata).
770 if (!GV) return false;
771
Bill Wendling07d31772010-06-29 22:34:52 +0000772 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000773 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
774 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000775 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
776 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000777 MCSymbol *Sym = Mang->getSymbol(GV);
778 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000779 return false;
780 }
781
782 return true;
783}
784
785const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000786getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
787 MachineModuleInfo *MMI, unsigned Encoding,
788 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000789 // The mach-o version of this method defaults to returning a stub reference.
790
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000791 if (Encoding & DW_EH_PE_indirect) {
792 MachineModuleInfoMachO &MachOMMI =
793 MMI->getObjFileInfo<MachineModuleInfoMachO>();
794
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000795 SmallString<128> Name;
796 Mang->getNameWithPrefix(Name, GV, true);
797 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000798
799 // Add information about the stub reference to MachOMMI so that the stub
800 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000801 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000802 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000803 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000804 MCSymbol *Sym = Mang->getSymbol(GV);
805 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000806 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000807
808 return TargetLoweringObjectFile::
Chris Lattner4c6741f2010-03-15 20:37:38 +0000809 getExprForDwarfReference(SSym, Mang, MMI,
Chris Lattner42263e22010-03-11 21:55:20 +0000810 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000811 }
812
813 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000814 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000815}
816
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000817unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
818 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
819}
820
821unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
822 return DW_EH_PE_pcrel;
823}
824
825unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
826 return DW_EH_PE_pcrel;
827}
828
829unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000830 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000831}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000832
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000833//===----------------------------------------------------------------------===//
834// COFF
835//===----------------------------------------------------------------------===//
836
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000837void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
838 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000839 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000840 TextSection =
841 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000842 COFF::IMAGE_SCN_CNT_CODE |
843 COFF::IMAGE_SCN_MEM_EXECUTE |
844 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000845 SectionKind::getText());
846 DataSection =
847 getContext().getCOFFSection(".data",
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());
852 ReadOnlySection =
853 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000854 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
855 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000856 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000857 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000858 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000859 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
860 COFF::IMAGE_SCN_MEM_READ |
861 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000862 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000863 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000864 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000865 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
866 COFF::IMAGE_SCN_MEM_READ |
867 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000868 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000869
870 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
871 // though it contains relocatable pointers. In PIC mode, this is probably a
872 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
873 // adjusted or this should be a data section.
874 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000875 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000876 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
877 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000878 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000879 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000880 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000881 getContext().getCOFFSection(".debug_abbrev",
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 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000886 getContext().getCOFFSection(".debug_info",
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 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000891 getContext().getCOFFSection(".debug_line",
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 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000896 getContext().getCOFFSection(".debug_frame",
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 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000901 getContext().getCOFFSection(".debug_pubnames",
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 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000906 getContext().getCOFFSection(".debug_pubtypes",
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 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000911 getContext().getCOFFSection(".debug_str",
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());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000915 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000916 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000917 COFF::IMAGE_SCN_MEM_DISCARDABLE |
918 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000919 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000920 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000921 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000922 COFF::IMAGE_SCN_MEM_DISCARDABLE |
923 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000924 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000925 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000926 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000927 COFF::IMAGE_SCN_MEM_DISCARDABLE |
928 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000929 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000930 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000931 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000932 COFF::IMAGE_SCN_MEM_DISCARDABLE |
933 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000934 SectionKind::getMetadata());
935
936 DrectveSection =
937 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000938 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000939 SectionKind::getMetadata());
940}
941
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000942const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
943 return getContext().getCOFFSection(".eh_frame",
944 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
945 COFF::IMAGE_SCN_MEM_READ |
946 COFF::IMAGE_SCN_MEM_WRITE,
947 SectionKind::getDataRel());
948}
949
950
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000951static unsigned
952getCOFFSectionFlags(SectionKind K) {
953 unsigned Flags = 0;
954
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000955 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000956 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000957 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000958 else if (K.isText())
959 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000960 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000961 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000962 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000963 else if (K.isBSS ())
964 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000965 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
966 COFF::IMAGE_SCN_MEM_READ |
967 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000968 else if (K.isReadOnly())
969 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000970 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
971 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000972 else if (K.isWriteable())
973 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000974 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
975 COFF::IMAGE_SCN_MEM_READ |
976 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000977
978 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000979}
980
981const MCSection *TargetLoweringObjectFileCOFF::
982getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
983 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000984 return getContext().getCOFFSection(GV->getSection(),
985 getCOFFSectionFlags(Kind),
986 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000987}
988
989static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
990 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000991 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +0000992 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000993 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000994 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +0000995 return ".data$";
996 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000997}
998
999
1000const MCSection *TargetLoweringObjectFileCOFF::
1001SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1002 Mangler *Mang, const TargetMachine &TM) const {
1003 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1004
1005 // If this global is linkonce/weak and the target handles this by emitting it
1006 // into a 'uniqued' section name, create and return the section now.
1007 if (GV->isWeakForLinker()) {
1008 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1009 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +00001010 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001011 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001012
1013 unsigned Characteristics = getCOFFSectionFlags(Kind);
1014
Daniel Dunbar94610582010-07-01 20:07:24 +00001015 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001016
1017 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001018 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001019 }
1020
1021 if (Kind.isText())
1022 return getTextSection();
1023
1024 return getDataSection();
1025}
1026