blob: 7888a838ad9cefbfe28d2fb70e48ad1cc0f43cf2 [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"
Rafael Espindola30deafc2011-04-16 03:51:21 +000026#include "llvm/MC/MCStreamer.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000027#include "llvm/MC/MCSymbol.h"
28#include "llvm/Target/Mangler.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
32#include "llvm/Support/Dwarf.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000033#include "llvm/Support/ELF.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
36#include "llvm/ADT/SmallString.h"
37#include "llvm/ADT/StringExtras.h"
Chris Lattner8048ebe2010-09-27 06:44:54 +000038#include "llvm/ADT/Triple.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000039using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000040using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000041
42//===----------------------------------------------------------------------===//
43// ELF
44//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000045
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000046void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
47 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000048 TargetLoweringObjectFile::Initialize(Ctx, TM);
49
50 BSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000051 getContext().getELFSection(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000052 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000053 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000054
55 TextSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000056 getContext().getELFSection(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000057 ELF::SHF_EXECINSTR |
58 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000059 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000060
61 DataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000062 getContext().getELFSection(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000063 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000064 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000065
66 ReadOnlySection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000067 getContext().getELFSection(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000068 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000069 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000070
71 TLSDataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000072 getContext().getELFSection(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000073 ELF::SHF_ALLOC | ELF::SHF_TLS |
74 ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000075 SectionKind::getThreadData());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000076
77 TLSBSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000078 getContext().getELFSection(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000079 ELF::SHF_ALLOC | ELF::SHF_TLS |
80 ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000081 SectionKind::getThreadBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000082
83 DataRelSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000084 getContext().getELFSection(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000085 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000086 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000087
88 DataRelLocalSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000089 getContext().getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000090 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000091 SectionKind::getDataRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000092
93 DataRelROSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000094 getContext().getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000095 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000096 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000097
98 DataRelROLocalSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000099 getContext().getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000100 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000101 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000102
103 MergeableConst4Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000104 getContext().getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000105 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000106 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000107
108 MergeableConst8Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000109 getContext().getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000110 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000111 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000112
113 MergeableConst16Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000114 getContext().getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000115 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000116 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000117
118 StaticCtorSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000119 getContext().getELFSection(".ctors", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000120 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000121 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000122
123 StaticDtorSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000124 getContext().getELFSection(".dtors", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000125 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000126 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000127
128 // Exception Handling Sections.
129
130 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
131 // it contains relocatable pointers. In PIC mode, this is probably a big
132 // runtime hit for C++ apps. Either the contents of the LSDA need to be
133 // adjusted or this should be a data section.
134 LSDASection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000135 getContext().getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000136 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +0000137 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000138 // Debug Info Sections.
139 DwarfAbbrevSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000140 getContext().getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000141 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000142 DwarfInfoSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000143 getContext().getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000144 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000145 DwarfLineSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000146 getContext().getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000147 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000148 DwarfFrameSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000149 getContext().getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000150 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000151 DwarfPubNamesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000152 getContext().getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000153 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000154 DwarfPubTypesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000155 getContext().getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000156 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000157 DwarfStrSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000158 getContext().getELFSection(".debug_str", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000159 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000160 DwarfLocSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000161 getContext().getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000162 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000163 DwarfARangesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000164 getContext().getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000165 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000166 DwarfRangesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000167 getContext().getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000168 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000169 DwarfMacroInfoSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000170 getContext().getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000171 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000172}
173
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000174const MCSection *TargetLoweringObjectFileELF::getEHFrameSection() const {
175 return getContext().getELFSection(".eh_frame", ELF::SHT_PROGBITS,
176 ELF::SHF_ALLOC,
177 SectionKind::getDataRel());
178}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000179
Rafael Espindola30deafc2011-04-16 03:51:21 +0000180MCSymbol *
181TargetLoweringObjectFileELF::getPersonalityPICSymbol(StringRef Name) const {
182 Twine FullName = StringRef("DW.ref.") + Name;
183 return getContext().GetOrCreateSymbol(FullName);
184}
185
186void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
187 const TargetMachine &TM,
188 const MCSymbol *Sym) const {
Rafael Espindola018e38c2011-04-27 21:29:52 +0000189 MCSymbol *Label = getPersonalityPICSymbol(Sym->getName());
190 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
191 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
192 Twine SectionName = StringRef(".data.") + Label->getName();
193 SmallString<64> NameData;
194 SectionName.toVector(NameData);
195 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
196 const MCSection *Sec = getContext().getELFSection(NameData,
197 ELF::SHT_PROGBITS,
198 Flags,
199 SectionKind::getDataRel(),
200 0, Label->getName());
201 Streamer.SwitchSection(Sec);
202 Streamer.EmitValueToAlignment(8);
203 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
204 const MCExpr *E = MCConstantExpr::Create(8, getContext());
205 Streamer.EmitELFSize(Label, E);
206 Streamer.EmitLabel(Label);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000207
Rafael Espindola018e38c2011-04-27 21:29:52 +0000208 unsigned Size = TM.getTargetData()->getPointerSize();
209 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000210}
211
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000212static SectionKind
213getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0412d5b2011-02-24 20:18:01 +0000214 // FIXME: Why is this here? Codegen is should not be in the business
215 // of figuring section flags. If the user wrote section(".eh_frame"),
216 // we should just pass that to MC which will defer to the assembly
217 // or use its default if producing an object file.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000218 if (Name.empty() || Name[0] != '.') return K;
219
220 // Some lame default implementation based on some magic section names.
221 if (Name == ".bss" ||
222 Name.startswith(".bss.") ||
223 Name.startswith(".gnu.linkonce.b.") ||
224 Name.startswith(".llvm.linkonce.b.") ||
225 Name == ".sbss" ||
226 Name.startswith(".sbss.") ||
227 Name.startswith(".gnu.linkonce.sb.") ||
228 Name.startswith(".llvm.linkonce.sb."))
229 return SectionKind::getBSS();
230
231 if (Name == ".tdata" ||
232 Name.startswith(".tdata.") ||
233 Name.startswith(".gnu.linkonce.td.") ||
234 Name.startswith(".llvm.linkonce.td."))
235 return SectionKind::getThreadData();
236
237 if (Name == ".tbss" ||
238 Name.startswith(".tbss.") ||
239 Name.startswith(".gnu.linkonce.tb.") ||
240 Name.startswith(".llvm.linkonce.tb."))
241 return SectionKind::getThreadBSS();
242
Rafael Espindola0412d5b2011-02-24 20:18:01 +0000243 if (Name == ".eh_frame")
244 return SectionKind::getDataRel();
245
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000246 return K;
247}
248
249
250static unsigned getELFSectionType(StringRef Name, SectionKind K) {
251
252 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000253 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000254
255 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000256 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000257
258 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000259 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000260
261 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000262 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000263
Rafael Espindolac85dca62011-01-23 04:28:49 +0000264 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000265}
266
267
268static unsigned
269getELFSectionFlags(SectionKind K) {
270 unsigned Flags = 0;
271
272 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000273 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000274
275 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000276 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000277
278 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000279 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000280
281 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000282 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000283
284 // K.isMergeableConst() is left out to honour PR4650
285 if (K.isMergeableCString() || K.isMergeableConst4() ||
286 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000287 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000288
289 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000290 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000291
292 return Flags;
293}
294
295
296const MCSection *TargetLoweringObjectFileELF::
297getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
298 Mangler *Mang, const TargetMachine &TM) const {
299 StringRef SectionName = GV->getSection();
300
301 // Infer section flags from the section name if we can.
302 Kind = getELFKindForNamedSection(SectionName, Kind);
303
Chris Lattner287df1b2010-04-08 21:34:17 +0000304 return getContext().getELFSection(SectionName,
305 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000306 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000307}
308
Chris Lattner43ac7212010-04-13 00:36:43 +0000309/// getSectionPrefixForGlobal - Return the section prefix name used by options
310/// FunctionsSections and DataSections.
311static const char *getSectionPrefixForGlobal(SectionKind Kind) {
312 if (Kind.isText()) return ".text.";
313 if (Kind.isReadOnly()) return ".rodata.";
314
315 if (Kind.isThreadData()) return ".tdata.";
316 if (Kind.isThreadBSS()) return ".tbss.";
317
318 if (Kind.isDataNoRel()) return ".data.";
319 if (Kind.isDataRelLocal()) return ".data.rel.local.";
320 if (Kind.isDataRel()) return ".data.rel.";
321 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
322
323 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
324 return ".data.rel.ro.";
325}
326
327
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000328const MCSection *TargetLoweringObjectFileELF::
329SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
330 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000331 // If we have -ffunction-section or -fdata-section then we should emit the
332 // global value to a uniqued section specifically for it.
333 bool EmitUniquedSection;
334 if (Kind.isText())
335 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000336 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000337 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000338
339 // If this global is linkonce/weak and the target handles this by emitting it
340 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000341 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
342 !Kind.isCommon() && !Kind.isBSS()) {
343 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000344 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000345
Chris Lattner4c6741f2010-03-15 20:37:38 +0000346 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
347 MCSymbol *Sym = Mang->getSymbol(GV);
348 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000349 StringRef Group = "";
350 unsigned Flags = getELFSectionFlags(Kind);
351 if (GV->isWeakForLinker()) {
352 Group = Sym->getName();
353 Flags |= ELF::SHF_GROUP;
354 }
355
Chris Lattner287df1b2010-04-08 21:34:17 +0000356 return getContext().getELFSection(Name.str(),
357 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000358 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000359 }
360
361 if (Kind.isText()) return TextSection;
362
363 if (Kind.isMergeable1ByteCString() ||
364 Kind.isMergeable2ByteCString() ||
365 Kind.isMergeable4ByteCString()) {
366
367 // We also need alignment here.
368 // FIXME: this is getting the alignment of the character, not the
369 // alignment of the global!
370 unsigned Align =
371 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
372
373 const char *SizeSpec = ".rodata.str1.";
374 if (Kind.isMergeable2ByteCString())
375 SizeSpec = ".rodata.str2.";
376 else if (Kind.isMergeable4ByteCString())
377 SizeSpec = ".rodata.str4.";
378 else
379 assert(Kind.isMergeable1ByteCString() && "unknown string width");
380
381
382 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000383 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000384 ELF::SHF_ALLOC |
385 ELF::SHF_MERGE |
386 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000387 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000388 }
389
390 if (Kind.isMergeableConst()) {
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 return ReadOnlySection; // .const
398 }
399
400 if (Kind.isReadOnly()) return ReadOnlySection;
401
402 if (Kind.isThreadData()) return TLSDataSection;
403 if (Kind.isThreadBSS()) return TLSBSSSection;
404
405 // Note: we claim that common symbols are put in BSSSection, but they are
406 // really emitted with the magic .comm directive, which creates a symbol table
407 // entry but not a section.
408 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
409
410 if (Kind.isDataNoRel()) return DataSection;
411 if (Kind.isDataRelLocal()) return DataRelLocalSection;
412 if (Kind.isDataRel()) return DataRelSection;
413 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
414
415 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
416 return DataRelROSection;
417}
418
419/// getSectionForConstant - Given a mergeable constant with the
420/// specified size and relocation information, return a section that it
421/// should be placed in.
422const MCSection *TargetLoweringObjectFileELF::
423getSectionForConstant(SectionKind Kind) const {
424 if (Kind.isMergeableConst4() && MergeableConst4Section)
425 return MergeableConst4Section;
426 if (Kind.isMergeableConst8() && MergeableConst8Section)
427 return MergeableConst8Section;
428 if (Kind.isMergeableConst16() && MergeableConst16Section)
429 return MergeableConst16Section;
430 if (Kind.isReadOnly())
431 return ReadOnlySection;
432
433 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
434 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
435 return DataRelROSection;
436}
437
438const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000439getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
440 MachineModuleInfo *MMI,
441 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000442
443 if (Encoding & dwarf::DW_EH_PE_indirect) {
444 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
445
446 SmallString<128> Name;
447 Mang->getNameWithPrefix(Name, GV, true);
448 Name += ".DW.stub";
449
450 // Add information about the stub reference to ELFMMI so that the stub
451 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000452 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000453 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000454 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000455 MCSymbol *Sym = Mang->getSymbol(GV);
456 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000457 }
458
459 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000460 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000461 }
462
463 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000464 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000465}
466
467//===----------------------------------------------------------------------===//
468// MachO
469//===----------------------------------------------------------------------===//
470
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000471void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
472 const TargetMachine &TM) {
Bill Wendling2ff6e1e2010-09-28 23:11:55 +0000473 // _foo.eh symbols are currently always exported so that the linker knows
474 // about them. This is not necessary on 10.6 and later, but it
475 // doesn't hurt anything.
476 // FIXME: I need to get this from Triple.
Chris Lattner09d53fe2010-03-10 07:20:42 +0000477 IsFunctionEHSymbolGlobal = true;
478 IsFunctionEHFrameSymbolPrivate = false;
479 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000480
Daniel Dunbarebc50662011-04-19 20:32:39 +0000481 // .comm doesn't support alignment before Leopard.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000482 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
Daniel Dunbar558692f2011-04-20 00:14:25 +0000483 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
Daniel Dunbarebc50662011-04-19 20:32:39 +0000484 CommDirectiveSupportsAlignment = false;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000485
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000486 TargetLoweringObjectFile::Initialize(Ctx, TM);
487
488 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000489 = getContext().getMachOSection("__TEXT", "__text",
490 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
491 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000492 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000493 = getContext().getMachOSection("__DATA", "__data", 0,
494 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000495
Eric Christopher423c9e32010-05-17 21:02:07 +0000496 TLSDataSection // .tdata
497 = getContext().getMachOSection("__DATA", "__thread_data",
498 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
499 SectionKind::getDataRel());
500 TLSBSSSection // .tbss
501 = getContext().getMachOSection("__DATA", "__thread_bss",
502 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
503 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000504
Eric Christopher423c9e32010-05-17 21:02:07 +0000505 // TODO: Verify datarel below.
506 TLSTLVSection // .tlv
507 = getContext().getMachOSection("__DATA", "__thread_vars",
508 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
509 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000510
Eric Christopherc6177a42010-05-17 22:53:55 +0000511 TLSThreadInitSection
512 = getContext().getMachOSection("__DATA", "__thread_init",
513 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
514 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000515
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000516 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000517 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000518 MCSectionMachO::S_CSTRING_LITERALS,
519 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000520 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000521 = getContext().getMachOSection("__TEXT","__ustring", 0,
522 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000523 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000524 = getContext().getMachOSection("__TEXT", "__literal4",
525 MCSectionMachO::S_4BYTE_LITERALS,
526 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000527 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000528 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000529 MCSectionMachO::S_8BYTE_LITERALS,
530 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000531
532 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
533 // to using it in -static mode.
534 SixteenByteConstantSection = 0;
535 if (TM.getRelocationModel() != Reloc::Static &&
536 TM.getTargetData()->getPointerSize() == 32)
537 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000538 getContext().getMachOSection("__TEXT", "__literal16",
539 MCSectionMachO::S_16BYTE_LITERALS,
540 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000541
542 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000543 = getContext().getMachOSection("__TEXT", "__const", 0,
544 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000545
546 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000547 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
548 MCSectionMachO::S_COALESCED |
549 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
550 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000551 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000552 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000553 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000554 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000555 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000556 = getContext().getMachOSection("__DATA", "__const", 0,
557 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000558 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000559 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000560 MCSectionMachO::S_COALESCED,
561 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000562 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000563 = getContext().getMachOSection("__DATA","__common",
564 MCSectionMachO::S_ZEROFILL,
565 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000566 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000567 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
568 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000569
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000570
571 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000572 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
573 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
574 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000575 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000576 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
577 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
578 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000579
580 if (TM.getRelocationModel() == Reloc::Static) {
581 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000582 = getContext().getMachOSection("__TEXT", "__constructor", 0,
583 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000584 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000585 = getContext().getMachOSection("__TEXT", "__destructor", 0,
586 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000587 } else {
588 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000589 = getContext().getMachOSection("__DATA", "__mod_init_func",
590 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
591 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000592 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000593 = getContext().getMachOSection("__DATA", "__mod_term_func",
594 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
595 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000596 }
597
598 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000599 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
600 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000601 // Debug Information.
602 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000603 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000604 MCSectionMachO::S_ATTR_DEBUG,
605 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000606 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000607 getContext().getMachOSection("__DWARF", "__debug_info",
608 MCSectionMachO::S_ATTR_DEBUG,
609 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000610 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000611 getContext().getMachOSection("__DWARF", "__debug_line",
612 MCSectionMachO::S_ATTR_DEBUG,
613 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000614 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000615 getContext().getMachOSection("__DWARF", "__debug_frame",
616 MCSectionMachO::S_ATTR_DEBUG,
617 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000618 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000619 getContext().getMachOSection("__DWARF", "__debug_pubnames",
620 MCSectionMachO::S_ATTR_DEBUG,
621 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000622 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000623 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
624 MCSectionMachO::S_ATTR_DEBUG,
625 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000626 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000627 getContext().getMachOSection("__DWARF", "__debug_str",
628 MCSectionMachO::S_ATTR_DEBUG,
629 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000630 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000631 getContext().getMachOSection("__DWARF", "__debug_loc",
632 MCSectionMachO::S_ATTR_DEBUG,
633 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000634 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000635 getContext().getMachOSection("__DWARF", "__debug_aranges",
636 MCSectionMachO::S_ATTR_DEBUG,
637 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000638 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000639 getContext().getMachOSection("__DWARF", "__debug_ranges",
640 MCSectionMachO::S_ATTR_DEBUG,
641 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000642 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000643 getContext().getMachOSection("__DWARF", "__debug_macinfo",
644 MCSectionMachO::S_ATTR_DEBUG,
645 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000646 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000647 getContext().getMachOSection("__DWARF", "__debug_inlined",
648 MCSectionMachO::S_ATTR_DEBUG,
649 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000650
Eric Christopher8116ca52010-05-22 00:10:22 +0000651 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000652}
653
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000654const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
655 return getContext().getMachOSection("__TEXT", "__eh_frame",
656 MCSectionMachO::S_COALESCED |
657 MCSectionMachO::S_ATTR_NO_TOC |
658 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
659 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
660 SectionKind::getReadOnly());
661}
662
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000663const MCSection *TargetLoweringObjectFileMachO::
664getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
665 Mangler *Mang, const TargetMachine &TM) const {
666 // Parse the section specifier and create it if valid.
667 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000668 unsigned TAA = 0, StubSize = 0;
669 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000670 std::string ErrorCode =
671 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000672 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000673 if (!ErrorCode.empty()) {
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 "' has an invalid section specifier '" + GV->getSection()+
677 "': " + ErrorCode + ".");
678 // Fall back to dropping it into the data section.
679 return DataSection;
680 }
681
682 // Get the section.
683 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000684 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000685
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000686 // If TAA wasn't set by ParseSectionSpecifier() above,
687 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000688 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000689 TAA = S->getTypeAndAttributes();
690
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000691 // Okay, now that we got the section, verify that the TAA & StubSize agree.
692 // If the user declared multiple globals with different section flags, we need
693 // to reject it here.
694 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000695 // If invalid, report the error with report_fatal_error.
696 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000697 "' section type or attributes does not match previous"
698 " section specifier");
699 }
700
701 return S;
702}
703
704const MCSection *TargetLoweringObjectFileMachO::
705SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000706 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000707
Eric Christopher02b46bc2010-05-25 21:28:50 +0000708 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000709 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000710 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000711
712 if (Kind.isText())
713 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
714
715 // If this is weak/linkonce, put this in a coalescable section, either in text
716 // or data depending on if it is writable.
717 if (GV->isWeakForLinker()) {
718 if (Kind.isReadOnly())
719 return ConstTextCoalSection;
720 return DataCoalSection;
721 }
722
723 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000724 if (Kind.isMergeable1ByteCString() &&
725 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
726 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000727
Chris Lattner98f15d22010-03-07 04:28:09 +0000728 // Do not put 16-bit arrays in the UString section if they have an
729 // externally visible label, this runs into issues with certain linker
730 // versions.
731 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
732 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
733 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000734
735 if (Kind.isMergeableConst()) {
736 if (Kind.isMergeableConst4())
737 return FourByteConstantSection;
738 if (Kind.isMergeableConst8())
739 return EightByteConstantSection;
740 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
741 return SixteenByteConstantSection;
742 }
743
744 // Otherwise, if it is readonly, but not something we can specially optimize,
745 // just drop it in .const.
746 if (Kind.isReadOnly())
747 return ReadOnlySection;
748
749 // If this is marked const, put it into a const section. But if the dynamic
750 // linker needs to write to it, put it in the data segment.
751 if (Kind.isReadOnlyWithRel())
752 return ConstDataSection;
753
754 // Put zero initialized globals with strong external linkage in the
755 // DATA, __common section with the .zerofill directive.
756 if (Kind.isBSSExtern())
757 return DataCommonSection;
758
759 // Put zero initialized globals with local linkage in __DATA,__bss directive
760 // with the .zerofill directive (aka .lcomm).
761 if (Kind.isBSSLocal())
762 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000763
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000764 // Otherwise, just drop the variable in the normal data section.
765 return DataSection;
766}
767
768const MCSection *
769TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
770 // If this constant requires a relocation, we have to put it in the data
771 // segment, not in the text segment.
772 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
773 return ConstDataSection;
774
775 if (Kind.isMergeableConst4())
776 return FourByteConstantSection;
777 if (Kind.isMergeableConst8())
778 return EightByteConstantSection;
779 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
780 return SixteenByteConstantSection;
781 return ReadOnlySection; // .const
782}
783
784/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
785/// not to emit the UsedDirective for some symbols in llvm.used.
786// FIXME: REMOVE this (rdar://7071300)
787bool TargetLoweringObjectFileMachO::
788shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
789 /// On Darwin, internally linked data beginning with "L" or "l" does not have
790 /// the directive emitted (this occurs in ObjC metadata).
791 if (!GV) return false;
792
Bill Wendling07d31772010-06-29 22:34:52 +0000793 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000794 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
795 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000796 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
797 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000798 MCSymbol *Sym = Mang->getSymbol(GV);
799 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000800 return false;
801 }
802
803 return true;
804}
805
806const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000807getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
808 MachineModuleInfo *MMI, unsigned Encoding,
809 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000810 // The mach-o version of this method defaults to returning a stub reference.
811
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000812 if (Encoding & DW_EH_PE_indirect) {
813 MachineModuleInfoMachO &MachOMMI =
814 MMI->getObjFileInfo<MachineModuleInfoMachO>();
815
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000816 SmallString<128> Name;
817 Mang->getNameWithPrefix(Name, GV, true);
818 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000819
820 // Add information about the stub reference to MachOMMI so that the stub
821 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000822 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000823 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000824 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000825 MCSymbol *Sym = Mang->getSymbol(GV);
826 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000827 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000828
829 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000830 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000831 }
832
833 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000834 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000835}
836
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000837unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
838 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
839}
840
841unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
842 return DW_EH_PE_pcrel;
843}
844
845unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
846 return DW_EH_PE_pcrel;
847}
848
849unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000850 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000851}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000852
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000853//===----------------------------------------------------------------------===//
854// COFF
855//===----------------------------------------------------------------------===//
856
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000857void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
858 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000859 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000860 TextSection =
861 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000862 COFF::IMAGE_SCN_CNT_CODE |
863 COFF::IMAGE_SCN_MEM_EXECUTE |
864 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000865 SectionKind::getText());
866 DataSection =
867 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000868 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
869 COFF::IMAGE_SCN_MEM_READ |
870 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000871 SectionKind::getDataRel());
872 ReadOnlySection =
873 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000874 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
875 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000876 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000877 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000878 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000879 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
880 COFF::IMAGE_SCN_MEM_READ |
881 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000882 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000883 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000884 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000885 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
886 COFF::IMAGE_SCN_MEM_READ |
887 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000888 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000889
890 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
891 // though it contains relocatable pointers. In PIC mode, this is probably a
892 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
893 // adjusted or this should be a data section.
894 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000895 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000896 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
897 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000898 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000899 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000900 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000901 getContext().getCOFFSection(".debug_abbrev",
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 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000906 getContext().getCOFFSection(".debug_info",
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 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000911 getContext().getCOFFSection(".debug_line",
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 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000916 getContext().getCOFFSection(".debug_frame",
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 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000921 getContext().getCOFFSection(".debug_pubnames",
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 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000926 getContext().getCOFFSection(".debug_pubtypes",
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 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000931 getContext().getCOFFSection(".debug_str",
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());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000935 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000936 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000937 COFF::IMAGE_SCN_MEM_DISCARDABLE |
938 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000939 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000940 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000941 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000942 COFF::IMAGE_SCN_MEM_DISCARDABLE |
943 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000944 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000945 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000946 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000947 COFF::IMAGE_SCN_MEM_DISCARDABLE |
948 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000949 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000950 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000951 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000952 COFF::IMAGE_SCN_MEM_DISCARDABLE |
953 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000954 SectionKind::getMetadata());
955
956 DrectveSection =
957 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000958 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000959 SectionKind::getMetadata());
960}
961
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000962const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
963 return getContext().getCOFFSection(".eh_frame",
964 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
965 COFF::IMAGE_SCN_MEM_READ |
966 COFF::IMAGE_SCN_MEM_WRITE,
967 SectionKind::getDataRel());
968}
969
970
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000971static unsigned
972getCOFFSectionFlags(SectionKind K) {
973 unsigned Flags = 0;
974
Anton Korobeynikov36335be2010-07-06 15:24:56 +0000975 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +0000976 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000977 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000978 else if (K.isText())
979 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000980 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +0000981 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +0000982 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +0000983 else if (K.isBSS ())
984 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000985 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
986 COFF::IMAGE_SCN_MEM_READ |
987 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000988 else if (K.isReadOnly())
989 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000990 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
991 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000992 else if (K.isWriteable())
993 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +0000994 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
995 COFF::IMAGE_SCN_MEM_READ |
996 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000997
998 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000999}
1000
1001const MCSection *TargetLoweringObjectFileCOFF::
1002getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1003 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001004 return getContext().getCOFFSection(GV->getSection(),
1005 getCOFFSectionFlags(Kind),
1006 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001007}
1008
1009static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1010 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001011 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +00001012 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001013 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001014 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001015 return ".data$";
1016 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001017}
1018
1019
1020const MCSection *TargetLoweringObjectFileCOFF::
1021SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1022 Mangler *Mang, const TargetMachine &TM) const {
1023 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1024
1025 // If this global is linkonce/weak and the target handles this by emitting it
1026 // into a 'uniqued' section name, create and return the section now.
1027 if (GV->isWeakForLinker()) {
1028 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1029 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +00001030 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001031 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001032
1033 unsigned Characteristics = getCOFFSectionFlags(Kind);
1034
Daniel Dunbar94610582010-07-01 20:07:24 +00001035 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001036
1037 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001038 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001039 }
1040
1041 if (Kind.isText())
1042 return getTextSection();
1043
1044 return getDataSection();
1045}
1046