blob: cb337d84dbadc1ef3c16294fff6d1ce873ba4115 [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 *
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000181TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000182 Mangler *Mang,
183 MachineModuleInfo *MMI) const {
Rafael Espindola60246a92011-04-27 23:17:57 +0000184 unsigned Encoding = getPersonalityEncoding();
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000185 switch (Encoding & 0x70) {
186 default:
187 report_fatal_error("We do not support this DWARF encoding yet!");
188 case dwarf::DW_EH_PE_absptr:
189 return Mang->getSymbol(GV);
190 break;
191 case dwarf::DW_EH_PE_pcrel: {
192 Twine FullName = StringRef("DW.ref.") + Mang->getSymbol(GV)->getName();
193 return getContext().GetOrCreateSymbol(FullName);
194 break;
195 }
196 }
Rafael Espindola30deafc2011-04-16 03:51:21 +0000197}
198
199void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
200 const TargetMachine &TM,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000201 const MCSymbol *Sym) const {
202 Twine FullName = StringRef("DW.ref.") + Sym->getName();
203 MCSymbol *Label = getContext().GetOrCreateSymbol(FullName);
Rafael Espindola018e38c2011-04-27 21:29:52 +0000204 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
205 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
206 Twine SectionName = StringRef(".data.") + Label->getName();
207 SmallString<64> NameData;
208 SectionName.toVector(NameData);
209 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
210 const MCSection *Sec = getContext().getELFSection(NameData,
211 ELF::SHT_PROGBITS,
212 Flags,
213 SectionKind::getDataRel(),
214 0, Label->getName());
215 Streamer.SwitchSection(Sec);
216 Streamer.EmitValueToAlignment(8);
217 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
218 const MCExpr *E = MCConstantExpr::Create(8, getContext());
219 Streamer.EmitELFSize(Label, E);
220 Streamer.EmitLabel(Label);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000221
Rafael Espindola018e38c2011-04-27 21:29:52 +0000222 unsigned Size = TM.getTargetData()->getPointerSize();
223 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000224}
225
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000226static SectionKind
227getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0412d5b2011-02-24 20:18:01 +0000228 // FIXME: Why is this here? Codegen is should not be in the business
229 // of figuring section flags. If the user wrote section(".eh_frame"),
230 // we should just pass that to MC which will defer to the assembly
231 // or use its default if producing an object file.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000232 if (Name.empty() || Name[0] != '.') return K;
233
234 // Some lame default implementation based on some magic section names.
235 if (Name == ".bss" ||
236 Name.startswith(".bss.") ||
237 Name.startswith(".gnu.linkonce.b.") ||
238 Name.startswith(".llvm.linkonce.b.") ||
239 Name == ".sbss" ||
240 Name.startswith(".sbss.") ||
241 Name.startswith(".gnu.linkonce.sb.") ||
242 Name.startswith(".llvm.linkonce.sb."))
243 return SectionKind::getBSS();
244
245 if (Name == ".tdata" ||
246 Name.startswith(".tdata.") ||
247 Name.startswith(".gnu.linkonce.td.") ||
248 Name.startswith(".llvm.linkonce.td."))
249 return SectionKind::getThreadData();
250
251 if (Name == ".tbss" ||
252 Name.startswith(".tbss.") ||
253 Name.startswith(".gnu.linkonce.tb.") ||
254 Name.startswith(".llvm.linkonce.tb."))
255 return SectionKind::getThreadBSS();
256
Rafael Espindola0412d5b2011-02-24 20:18:01 +0000257 if (Name == ".eh_frame")
258 return SectionKind::getDataRel();
259
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000260 return K;
261}
262
263
264static unsigned getELFSectionType(StringRef Name, SectionKind K) {
265
266 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000267 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000268
269 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000270 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000271
272 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000273 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000274
275 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000276 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000277
Rafael Espindolac85dca62011-01-23 04:28:49 +0000278 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000279}
280
281
282static unsigned
283getELFSectionFlags(SectionKind K) {
284 unsigned Flags = 0;
285
286 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000287 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000288
289 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000290 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000291
292 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000293 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000294
295 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000296 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000297
298 // K.isMergeableConst() is left out to honour PR4650
299 if (K.isMergeableCString() || K.isMergeableConst4() ||
300 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000301 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000302
303 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000304 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000305
306 return Flags;
307}
308
309
310const MCSection *TargetLoweringObjectFileELF::
311getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
312 Mangler *Mang, const TargetMachine &TM) const {
313 StringRef SectionName = GV->getSection();
314
315 // Infer section flags from the section name if we can.
316 Kind = getELFKindForNamedSection(SectionName, Kind);
317
Chris Lattner287df1b2010-04-08 21:34:17 +0000318 return getContext().getELFSection(SectionName,
319 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000320 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000321}
322
Chris Lattner43ac7212010-04-13 00:36:43 +0000323/// getSectionPrefixForGlobal - Return the section prefix name used by options
324/// FunctionsSections and DataSections.
325static const char *getSectionPrefixForGlobal(SectionKind Kind) {
326 if (Kind.isText()) return ".text.";
327 if (Kind.isReadOnly()) return ".rodata.";
328
329 if (Kind.isThreadData()) return ".tdata.";
330 if (Kind.isThreadBSS()) return ".tbss.";
331
332 if (Kind.isDataNoRel()) return ".data.";
333 if (Kind.isDataRelLocal()) return ".data.rel.local.";
334 if (Kind.isDataRel()) return ".data.rel.";
335 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
336
337 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
338 return ".data.rel.ro.";
339}
340
341
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000342const MCSection *TargetLoweringObjectFileELF::
343SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
344 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000345 // If we have -ffunction-section or -fdata-section then we should emit the
346 // global value to a uniqued section specifically for it.
347 bool EmitUniquedSection;
348 if (Kind.isText())
349 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000350 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000351 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000352
353 // If this global is linkonce/weak and the target handles this by emitting it
354 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000355 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
356 !Kind.isCommon() && !Kind.isBSS()) {
357 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000358 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000359
Chris Lattner4c6741f2010-03-15 20:37:38 +0000360 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
361 MCSymbol *Sym = Mang->getSymbol(GV);
362 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000363 StringRef Group = "";
364 unsigned Flags = getELFSectionFlags(Kind);
365 if (GV->isWeakForLinker()) {
366 Group = Sym->getName();
367 Flags |= ELF::SHF_GROUP;
368 }
369
Chris Lattner287df1b2010-04-08 21:34:17 +0000370 return getContext().getELFSection(Name.str(),
371 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000372 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000373 }
374
375 if (Kind.isText()) return TextSection;
376
377 if (Kind.isMergeable1ByteCString() ||
378 Kind.isMergeable2ByteCString() ||
379 Kind.isMergeable4ByteCString()) {
380
381 // We also need alignment here.
382 // FIXME: this is getting the alignment of the character, not the
383 // alignment of the global!
384 unsigned Align =
385 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
386
387 const char *SizeSpec = ".rodata.str1.";
388 if (Kind.isMergeable2ByteCString())
389 SizeSpec = ".rodata.str2.";
390 else if (Kind.isMergeable4ByteCString())
391 SizeSpec = ".rodata.str4.";
392 else
393 assert(Kind.isMergeable1ByteCString() && "unknown string width");
394
395
396 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000397 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000398 ELF::SHF_ALLOC |
399 ELF::SHF_MERGE |
400 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000401 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000402 }
403
404 if (Kind.isMergeableConst()) {
405 if (Kind.isMergeableConst4() && MergeableConst4Section)
406 return MergeableConst4Section;
407 if (Kind.isMergeableConst8() && MergeableConst8Section)
408 return MergeableConst8Section;
409 if (Kind.isMergeableConst16() && MergeableConst16Section)
410 return MergeableConst16Section;
411 return ReadOnlySection; // .const
412 }
413
414 if (Kind.isReadOnly()) return ReadOnlySection;
415
416 if (Kind.isThreadData()) return TLSDataSection;
417 if (Kind.isThreadBSS()) return TLSBSSSection;
418
419 // Note: we claim that common symbols are put in BSSSection, but they are
420 // really emitted with the magic .comm directive, which creates a symbol table
421 // entry but not a section.
422 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
423
424 if (Kind.isDataNoRel()) return DataSection;
425 if (Kind.isDataRelLocal()) return DataRelLocalSection;
426 if (Kind.isDataRel()) return DataRelSection;
427 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
428
429 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
430 return DataRelROSection;
431}
432
433/// getSectionForConstant - Given a mergeable constant with the
434/// specified size and relocation information, return a section that it
435/// should be placed in.
436const MCSection *TargetLoweringObjectFileELF::
437getSectionForConstant(SectionKind Kind) const {
438 if (Kind.isMergeableConst4() && MergeableConst4Section)
439 return MergeableConst4Section;
440 if (Kind.isMergeableConst8() && MergeableConst8Section)
441 return MergeableConst8Section;
442 if (Kind.isMergeableConst16() && MergeableConst16Section)
443 return MergeableConst16Section;
444 if (Kind.isReadOnly())
445 return ReadOnlySection;
446
447 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
448 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
449 return DataRelROSection;
450}
451
452const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000453getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
454 MachineModuleInfo *MMI,
455 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000456
457 if (Encoding & dwarf::DW_EH_PE_indirect) {
458 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
459
460 SmallString<128> Name;
461 Mang->getNameWithPrefix(Name, GV, true);
462 Name += ".DW.stub";
463
464 // Add information about the stub reference to ELFMMI so that the stub
465 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000466 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000467 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000468 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000469 MCSymbol *Sym = Mang->getSymbol(GV);
470 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000471 }
472
473 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000474 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000475 }
476
477 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000478 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000479}
480
481//===----------------------------------------------------------------------===//
482// MachO
483//===----------------------------------------------------------------------===//
484
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000485void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
486 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000487 IsFunctionEHFrameSymbolPrivate = false;
488 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000489
Daniel Dunbarebc50662011-04-19 20:32:39 +0000490 // .comm doesn't support alignment before Leopard.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000491 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
Daniel Dunbar558692f2011-04-20 00:14:25 +0000492 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
Daniel Dunbarebc50662011-04-19 20:32:39 +0000493 CommDirectiveSupportsAlignment = false;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000494
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000495 TargetLoweringObjectFile::Initialize(Ctx, TM);
496
497 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000498 = getContext().getMachOSection("__TEXT", "__text",
499 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
500 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000501 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000502 = getContext().getMachOSection("__DATA", "__data", 0,
503 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000504
Eric Christopher423c9e32010-05-17 21:02:07 +0000505 TLSDataSection // .tdata
506 = getContext().getMachOSection("__DATA", "__thread_data",
507 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
508 SectionKind::getDataRel());
509 TLSBSSSection // .tbss
510 = getContext().getMachOSection("__DATA", "__thread_bss",
511 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
512 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000513
Eric Christopher423c9e32010-05-17 21:02:07 +0000514 // TODO: Verify datarel below.
515 TLSTLVSection // .tlv
516 = getContext().getMachOSection("__DATA", "__thread_vars",
517 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
518 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000519
Eric Christopherc6177a42010-05-17 22:53:55 +0000520 TLSThreadInitSection
521 = getContext().getMachOSection("__DATA", "__thread_init",
522 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
523 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000524
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000525 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000526 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000527 MCSectionMachO::S_CSTRING_LITERALS,
528 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000529 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000530 = getContext().getMachOSection("__TEXT","__ustring", 0,
531 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000532 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000533 = getContext().getMachOSection("__TEXT", "__literal4",
534 MCSectionMachO::S_4BYTE_LITERALS,
535 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000536 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000537 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000538 MCSectionMachO::S_8BYTE_LITERALS,
539 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000540
541 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
542 // to using it in -static mode.
543 SixteenByteConstantSection = 0;
544 if (TM.getRelocationModel() != Reloc::Static &&
545 TM.getTargetData()->getPointerSize() == 32)
546 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000547 getContext().getMachOSection("__TEXT", "__literal16",
548 MCSectionMachO::S_16BYTE_LITERALS,
549 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000550
551 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000552 = getContext().getMachOSection("__TEXT", "__const", 0,
553 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000554
555 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000556 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
557 MCSectionMachO::S_COALESCED |
558 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
559 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000560 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000561 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000562 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000563 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000564 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000565 = getContext().getMachOSection("__DATA", "__const", 0,
566 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000567 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000568 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000569 MCSectionMachO::S_COALESCED,
570 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000571 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000572 = getContext().getMachOSection("__DATA","__common",
573 MCSectionMachO::S_ZEROFILL,
574 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000575 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000576 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
577 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000578
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000579
580 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000581 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
582 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
583 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000584 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000585 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
586 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
587 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000588
589 if (TM.getRelocationModel() == Reloc::Static) {
590 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000591 = getContext().getMachOSection("__TEXT", "__constructor", 0,
592 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000593 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000594 = getContext().getMachOSection("__TEXT", "__destructor", 0,
595 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000596 } else {
597 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000598 = getContext().getMachOSection("__DATA", "__mod_init_func",
599 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
600 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000601 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000602 = getContext().getMachOSection("__DATA", "__mod_term_func",
603 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
604 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000605 }
606
607 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000608 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
609 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000610 // Debug Information.
611 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000612 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000613 MCSectionMachO::S_ATTR_DEBUG,
614 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000615 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000616 getContext().getMachOSection("__DWARF", "__debug_info",
617 MCSectionMachO::S_ATTR_DEBUG,
618 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000619 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000620 getContext().getMachOSection("__DWARF", "__debug_line",
621 MCSectionMachO::S_ATTR_DEBUG,
622 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000623 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000624 getContext().getMachOSection("__DWARF", "__debug_frame",
625 MCSectionMachO::S_ATTR_DEBUG,
626 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000627 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000628 getContext().getMachOSection("__DWARF", "__debug_pubnames",
629 MCSectionMachO::S_ATTR_DEBUG,
630 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000631 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000632 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
633 MCSectionMachO::S_ATTR_DEBUG,
634 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000635 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000636 getContext().getMachOSection("__DWARF", "__debug_str",
637 MCSectionMachO::S_ATTR_DEBUG,
638 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000639 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000640 getContext().getMachOSection("__DWARF", "__debug_loc",
641 MCSectionMachO::S_ATTR_DEBUG,
642 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000643 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000644 getContext().getMachOSection("__DWARF", "__debug_aranges",
645 MCSectionMachO::S_ATTR_DEBUG,
646 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000647 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000648 getContext().getMachOSection("__DWARF", "__debug_ranges",
649 MCSectionMachO::S_ATTR_DEBUG,
650 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000651 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000652 getContext().getMachOSection("__DWARF", "__debug_macinfo",
653 MCSectionMachO::S_ATTR_DEBUG,
654 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000655 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000656 getContext().getMachOSection("__DWARF", "__debug_inlined",
657 MCSectionMachO::S_ATTR_DEBUG,
658 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000659
Eric Christopher8116ca52010-05-22 00:10:22 +0000660 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000661}
662
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000663const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
664 return getContext().getMachOSection("__TEXT", "__eh_frame",
665 MCSectionMachO::S_COALESCED |
666 MCSectionMachO::S_ATTR_NO_TOC |
667 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
668 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
669 SectionKind::getReadOnly());
670}
671
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000672const MCSection *TargetLoweringObjectFileMachO::
673getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
674 Mangler *Mang, const TargetMachine &TM) const {
675 // Parse the section specifier and create it if valid.
676 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000677 unsigned TAA = 0, StubSize = 0;
678 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000679 std::string ErrorCode =
680 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000681 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000682 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000683 // If invalid, report the error with report_fatal_error.
684 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000685 "' has an invalid section specifier '" + GV->getSection()+
686 "': " + ErrorCode + ".");
687 // Fall back to dropping it into the data section.
688 return DataSection;
689 }
690
691 // Get the section.
692 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000693 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000694
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000695 // If TAA wasn't set by ParseSectionSpecifier() above,
696 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000697 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000698 TAA = S->getTypeAndAttributes();
699
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000700 // Okay, now that we got the section, verify that the TAA & StubSize agree.
701 // If the user declared multiple globals with different section flags, we need
702 // to reject it here.
703 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000704 // If invalid, report the error with report_fatal_error.
705 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000706 "' section type or attributes does not match previous"
707 " section specifier");
708 }
709
710 return S;
711}
712
713const MCSection *TargetLoweringObjectFileMachO::
714SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000715 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000716
Eric Christopher02b46bc2010-05-25 21:28:50 +0000717 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000718 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000719 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000720
721 if (Kind.isText())
722 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
723
724 // If this is weak/linkonce, put this in a coalescable section, either in text
725 // or data depending on if it is writable.
726 if (GV->isWeakForLinker()) {
727 if (Kind.isReadOnly())
728 return ConstTextCoalSection;
729 return DataCoalSection;
730 }
731
732 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000733 if (Kind.isMergeable1ByteCString() &&
734 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
735 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000736
Chris Lattner98f15d22010-03-07 04:28:09 +0000737 // Do not put 16-bit arrays in the UString section if they have an
738 // externally visible label, this runs into issues with certain linker
739 // versions.
740 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
741 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
742 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000743
744 if (Kind.isMergeableConst()) {
745 if (Kind.isMergeableConst4())
746 return FourByteConstantSection;
747 if (Kind.isMergeableConst8())
748 return EightByteConstantSection;
749 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
750 return SixteenByteConstantSection;
751 }
752
753 // Otherwise, if it is readonly, but not something we can specially optimize,
754 // just drop it in .const.
755 if (Kind.isReadOnly())
756 return ReadOnlySection;
757
758 // If this is marked const, put it into a const section. But if the dynamic
759 // linker needs to write to it, put it in the data segment.
760 if (Kind.isReadOnlyWithRel())
761 return ConstDataSection;
762
763 // Put zero initialized globals with strong external linkage in the
764 // DATA, __common section with the .zerofill directive.
765 if (Kind.isBSSExtern())
766 return DataCommonSection;
767
768 // Put zero initialized globals with local linkage in __DATA,__bss directive
769 // with the .zerofill directive (aka .lcomm).
770 if (Kind.isBSSLocal())
771 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000772
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000773 // Otherwise, just drop the variable in the normal data section.
774 return DataSection;
775}
776
777const MCSection *
778TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
779 // If this constant requires a relocation, we have to put it in the data
780 // segment, not in the text segment.
781 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
782 return ConstDataSection;
783
784 if (Kind.isMergeableConst4())
785 return FourByteConstantSection;
786 if (Kind.isMergeableConst8())
787 return EightByteConstantSection;
788 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
789 return SixteenByteConstantSection;
790 return ReadOnlySection; // .const
791}
792
793/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
794/// not to emit the UsedDirective for some symbols in llvm.used.
795// FIXME: REMOVE this (rdar://7071300)
796bool TargetLoweringObjectFileMachO::
797shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
798 /// On Darwin, internally linked data beginning with "L" or "l" does not have
799 /// the directive emitted (this occurs in ObjC metadata).
800 if (!GV) return false;
801
Bill Wendling07d31772010-06-29 22:34:52 +0000802 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000803 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
804 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000805 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
806 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000807 MCSymbol *Sym = Mang->getSymbol(GV);
808 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000809 return false;
810 }
811
812 return true;
813}
814
815const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000816getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
817 MachineModuleInfo *MMI, unsigned Encoding,
818 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000819 // The mach-o version of this method defaults to returning a stub reference.
820
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000821 if (Encoding & DW_EH_PE_indirect) {
822 MachineModuleInfoMachO &MachOMMI =
823 MMI->getObjFileInfo<MachineModuleInfoMachO>();
824
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000825 SmallString<128> Name;
826 Mang->getNameWithPrefix(Name, GV, true);
827 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000828
829 // Add information about the stub reference to MachOMMI so that the stub
830 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000831 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000832 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000833 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000834 MCSymbol *Sym = Mang->getSymbol(GV);
835 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000836 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000837
838 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000839 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000840 }
841
842 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000843 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000844}
845
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000846MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindola60246a92011-04-27 23:17:57 +0000847getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000848 MachineModuleInfo *MMI) const {
849 // The mach-o version of this method defaults to returning a stub reference.
850 MachineModuleInfoMachO &MachOMMI =
851 MMI->getObjFileInfo<MachineModuleInfoMachO>();
852
853 SmallString<128> Name;
854 Mang->getNameWithPrefix(Name, GV, true);
855 Name += "$non_lazy_ptr";
856
857 // Add information about the stub reference to MachOMMI so that the stub
858 // gets emitted by the asmprinter.
859 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
860 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
861 if (StubSym.getPointer() == 0) {
862 MCSymbol *Sym = Mang->getSymbol(GV);
863 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
864 }
865
866 return SSym;
867}
868
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000869unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
870 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
871}
872
873unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
874 return DW_EH_PE_pcrel;
875}
876
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000877unsigned TargetLoweringObjectFileMachO::getFDEEncoding(bool CFI) const {
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000878 return DW_EH_PE_pcrel;
879}
880
881unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000882 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000883}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000884
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000885//===----------------------------------------------------------------------===//
886// COFF
887//===----------------------------------------------------------------------===//
888
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000889void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
890 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000891 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000892 TextSection =
893 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000894 COFF::IMAGE_SCN_CNT_CODE |
895 COFF::IMAGE_SCN_MEM_EXECUTE |
896 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000897 SectionKind::getText());
898 DataSection =
899 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000900 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
901 COFF::IMAGE_SCN_MEM_READ |
902 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000903 SectionKind::getDataRel());
904 ReadOnlySection =
905 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000906 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
907 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000908 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000909 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000910 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000911 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
912 COFF::IMAGE_SCN_MEM_READ |
913 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000914 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000915 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000916 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000917 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
918 COFF::IMAGE_SCN_MEM_READ |
919 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000920 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000921
922 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
923 // though it contains relocatable pointers. In PIC mode, this is probably a
924 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
925 // adjusted or this should be a data section.
926 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000927 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000928 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
929 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000930 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000931 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000932 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000933 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000934 COFF::IMAGE_SCN_MEM_DISCARDABLE |
935 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000936 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000937 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000938 getContext().getCOFFSection(".debug_info",
Daniel Dunbar94610582010-07-01 20:07:24 +0000939 COFF::IMAGE_SCN_MEM_DISCARDABLE |
940 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000941 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000942 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000943 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000944 COFF::IMAGE_SCN_MEM_DISCARDABLE |
945 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000946 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000947 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000948 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000949 COFF::IMAGE_SCN_MEM_DISCARDABLE |
950 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000951 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000952 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000953 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +0000954 COFF::IMAGE_SCN_MEM_DISCARDABLE |
955 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000956 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000957 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000958 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +0000959 COFF::IMAGE_SCN_MEM_DISCARDABLE |
960 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000961 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000962 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000963 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +0000964 COFF::IMAGE_SCN_MEM_DISCARDABLE |
965 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000966 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000967 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000968 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000969 COFF::IMAGE_SCN_MEM_DISCARDABLE |
970 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000971 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000972 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000973 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000974 COFF::IMAGE_SCN_MEM_DISCARDABLE |
975 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000976 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000977 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000978 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000979 COFF::IMAGE_SCN_MEM_DISCARDABLE |
980 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000981 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000982 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000983 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000984 COFF::IMAGE_SCN_MEM_DISCARDABLE |
985 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000986 SectionKind::getMetadata());
987
988 DrectveSection =
989 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000990 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000991 SectionKind::getMetadata());
Charles Davisf3ffc2c2011-05-20 22:13:55 +0000992
993 PDataSection =
994 getContext().getCOFFSection(".pdata",
995 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
996 COFF::IMAGE_SCN_MEM_READ |
997 COFF::IMAGE_SCN_MEM_WRITE,
998 SectionKind::getDataRel());
999
1000 XDataSection =
1001 getContext().getCOFFSection(".xdata",
1002 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1003 COFF::IMAGE_SCN_MEM_READ |
1004 COFF::IMAGE_SCN_MEM_WRITE,
1005 SectionKind::getDataRel());
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001006}
1007
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001008const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
1009 return getContext().getCOFFSection(".eh_frame",
1010 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1011 COFF::IMAGE_SCN_MEM_READ |
1012 COFF::IMAGE_SCN_MEM_WRITE,
1013 SectionKind::getDataRel());
1014}
1015
1016
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001017static unsigned
1018getCOFFSectionFlags(SectionKind K) {
1019 unsigned Flags = 0;
1020
Anton Korobeynikov36335be2010-07-06 15:24:56 +00001021 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +00001022 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001023 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001024 else if (K.isText())
1025 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001026 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +00001027 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +00001028 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001029 else if (K.isBSS ())
1030 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001031 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1032 COFF::IMAGE_SCN_MEM_READ |
1033 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001034 else if (K.isReadOnly())
1035 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001036 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1037 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001038 else if (K.isWriteable())
1039 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001040 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1041 COFF::IMAGE_SCN_MEM_READ |
1042 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001043
1044 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001045}
1046
1047const MCSection *TargetLoweringObjectFileCOFF::
1048getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1049 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001050 return getContext().getCOFFSection(GV->getSection(),
1051 getCOFFSectionFlags(Kind),
1052 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001053}
1054
1055static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1056 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001057 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +00001058 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001059 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001060 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001061 return ".data$";
1062 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001063}
1064
1065
1066const MCSection *TargetLoweringObjectFileCOFF::
1067SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1068 Mangler *Mang, const TargetMachine &TM) const {
1069 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1070
1071 // If this global is linkonce/weak and the target handles this by emitting it
1072 // into a 'uniqued' section name, create and return the section now.
1073 if (GV->isWeakForLinker()) {
1074 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1075 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +00001076 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001077 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001078
1079 unsigned Characteristics = getCOFFSectionFlags(Kind);
1080
Daniel Dunbar94610582010-07-01 20:07:24 +00001081 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001082
1083 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001084 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001085 }
1086
1087 if (Kind.isText())
1088 return getTextSection();
1089
1090 return getDataSection();
1091}
1092