blob: 4e2888ddfc452c6f5c55fea6942e2fe25409b069 [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 Espindolae6657982011-05-24 03:10:31 +0000228 // N.B.: The defaults used in here are no the same ones used in MC.
229 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
230 // both gas and MC will produce a section with no flags. Given
231 // section(".eh_frame") gcc will produce
232 // .section .eh_frame,"a",@progbits
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000233 if (Name.empty() || Name[0] != '.') return K;
234
235 // Some lame default implementation based on some magic section names.
236 if (Name == ".bss" ||
237 Name.startswith(".bss.") ||
238 Name.startswith(".gnu.linkonce.b.") ||
239 Name.startswith(".llvm.linkonce.b.") ||
240 Name == ".sbss" ||
241 Name.startswith(".sbss.") ||
242 Name.startswith(".gnu.linkonce.sb.") ||
243 Name.startswith(".llvm.linkonce.sb."))
244 return SectionKind::getBSS();
245
246 if (Name == ".tdata" ||
247 Name.startswith(".tdata.") ||
248 Name.startswith(".gnu.linkonce.td.") ||
249 Name.startswith(".llvm.linkonce.td."))
250 return SectionKind::getThreadData();
251
252 if (Name == ".tbss" ||
253 Name.startswith(".tbss.") ||
254 Name.startswith(".gnu.linkonce.tb.") ||
255 Name.startswith(".llvm.linkonce.tb."))
256 return SectionKind::getThreadBSS();
257
Rafael Espindola0412d5b2011-02-24 20:18:01 +0000258 if (Name == ".eh_frame")
Rafael Espindola10c3e122011-05-24 02:50:20 +0000259 return SectionKind::getReadOnlyWithRel();
Rafael Espindola0412d5b2011-02-24 20:18:01 +0000260
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000261 return K;
262}
263
264
265static unsigned getELFSectionType(StringRef Name, SectionKind K) {
266
267 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000268 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000269
270 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000271 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000272
273 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000274 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000275
276 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000277 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000278
Rafael Espindolac85dca62011-01-23 04:28:49 +0000279 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000280}
281
282
283static unsigned
284getELFSectionFlags(SectionKind K) {
285 unsigned Flags = 0;
286
287 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000288 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000289
290 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000291 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000292
Rafael Espindola10c3e122011-05-24 02:50:20 +0000293 if (K.isWriteable() && !K.isReadOnlyWithRel())
Rafael Espindola1c130262011-01-23 04:43:11 +0000294 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000295
296 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000297 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000298
299 // K.isMergeableConst() is left out to honour PR4650
300 if (K.isMergeableCString() || K.isMergeableConst4() ||
301 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000302 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000303
304 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000305 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000306
307 return Flags;
308}
309
310
311const MCSection *TargetLoweringObjectFileELF::
312getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
313 Mangler *Mang, const TargetMachine &TM) const {
314 StringRef SectionName = GV->getSection();
315
316 // Infer section flags from the section name if we can.
317 Kind = getELFKindForNamedSection(SectionName, Kind);
318
Chris Lattner287df1b2010-04-08 21:34:17 +0000319 return getContext().getELFSection(SectionName,
320 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000321 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000322}
323
Chris Lattner43ac7212010-04-13 00:36:43 +0000324/// getSectionPrefixForGlobal - Return the section prefix name used by options
325/// FunctionsSections and DataSections.
326static const char *getSectionPrefixForGlobal(SectionKind Kind) {
327 if (Kind.isText()) return ".text.";
328 if (Kind.isReadOnly()) return ".rodata.";
329
330 if (Kind.isThreadData()) return ".tdata.";
331 if (Kind.isThreadBSS()) return ".tbss.";
332
333 if (Kind.isDataNoRel()) return ".data.";
334 if (Kind.isDataRelLocal()) return ".data.rel.local.";
335 if (Kind.isDataRel()) return ".data.rel.";
336 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
337
338 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
339 return ".data.rel.ro.";
340}
341
342
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000343const MCSection *TargetLoweringObjectFileELF::
344SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
345 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000346 // If we have -ffunction-section or -fdata-section then we should emit the
347 // global value to a uniqued section specifically for it.
348 bool EmitUniquedSection;
349 if (Kind.isText())
350 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000351 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000352 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000353
354 // If this global is linkonce/weak and the target handles this by emitting it
355 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000356 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
357 !Kind.isCommon() && !Kind.isBSS()) {
358 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000359 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000360
Chris Lattner4c6741f2010-03-15 20:37:38 +0000361 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
362 MCSymbol *Sym = Mang->getSymbol(GV);
363 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000364 StringRef Group = "";
365 unsigned Flags = getELFSectionFlags(Kind);
366 if (GV->isWeakForLinker()) {
367 Group = Sym->getName();
368 Flags |= ELF::SHF_GROUP;
369 }
370
Chris Lattner287df1b2010-04-08 21:34:17 +0000371 return getContext().getELFSection(Name.str(),
372 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000373 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000374 }
375
376 if (Kind.isText()) return TextSection;
377
378 if (Kind.isMergeable1ByteCString() ||
379 Kind.isMergeable2ByteCString() ||
380 Kind.isMergeable4ByteCString()) {
381
382 // We also need alignment here.
383 // FIXME: this is getting the alignment of the character, not the
384 // alignment of the global!
385 unsigned Align =
386 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
387
388 const char *SizeSpec = ".rodata.str1.";
389 if (Kind.isMergeable2ByteCString())
390 SizeSpec = ".rodata.str2.";
391 else if (Kind.isMergeable4ByteCString())
392 SizeSpec = ".rodata.str4.";
393 else
394 assert(Kind.isMergeable1ByteCString() && "unknown string width");
395
396
397 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000398 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000399 ELF::SHF_ALLOC |
400 ELF::SHF_MERGE |
401 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000402 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000403 }
404
405 if (Kind.isMergeableConst()) {
406 if (Kind.isMergeableConst4() && MergeableConst4Section)
407 return MergeableConst4Section;
408 if (Kind.isMergeableConst8() && MergeableConst8Section)
409 return MergeableConst8Section;
410 if (Kind.isMergeableConst16() && MergeableConst16Section)
411 return MergeableConst16Section;
412 return ReadOnlySection; // .const
413 }
414
415 if (Kind.isReadOnly()) return ReadOnlySection;
416
417 if (Kind.isThreadData()) return TLSDataSection;
418 if (Kind.isThreadBSS()) return TLSBSSSection;
419
420 // Note: we claim that common symbols are put in BSSSection, but they are
421 // really emitted with the magic .comm directive, which creates a symbol table
422 // entry but not a section.
423 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
424
425 if (Kind.isDataNoRel()) return DataSection;
426 if (Kind.isDataRelLocal()) return DataRelLocalSection;
427 if (Kind.isDataRel()) return DataRelSection;
428 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
429
430 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
431 return DataRelROSection;
432}
433
434/// getSectionForConstant - Given a mergeable constant with the
435/// specified size and relocation information, return a section that it
436/// should be placed in.
437const MCSection *TargetLoweringObjectFileELF::
438getSectionForConstant(SectionKind Kind) const {
439 if (Kind.isMergeableConst4() && MergeableConst4Section)
440 return MergeableConst4Section;
441 if (Kind.isMergeableConst8() && MergeableConst8Section)
442 return MergeableConst8Section;
443 if (Kind.isMergeableConst16() && MergeableConst16Section)
444 return MergeableConst16Section;
445 if (Kind.isReadOnly())
446 return ReadOnlySection;
447
448 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
449 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
450 return DataRelROSection;
451}
452
453const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000454getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
455 MachineModuleInfo *MMI,
456 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000457
458 if (Encoding & dwarf::DW_EH_PE_indirect) {
459 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
460
461 SmallString<128> Name;
462 Mang->getNameWithPrefix(Name, GV, true);
463 Name += ".DW.stub";
464
465 // Add information about the stub reference to ELFMMI so that the stub
466 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000467 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000468 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000469 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000470 MCSymbol *Sym = Mang->getSymbol(GV);
471 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000472 }
473
474 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000475 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000476 }
477
478 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000479 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000480}
481
482//===----------------------------------------------------------------------===//
483// MachO
484//===----------------------------------------------------------------------===//
485
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000486void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
487 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000488 IsFunctionEHFrameSymbolPrivate = false;
489 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000490
Daniel Dunbarebc50662011-04-19 20:32:39 +0000491 // .comm doesn't support alignment before Leopard.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000492 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
Daniel Dunbar558692f2011-04-20 00:14:25 +0000493 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
Daniel Dunbarebc50662011-04-19 20:32:39 +0000494 CommDirectiveSupportsAlignment = false;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000495
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000496 TargetLoweringObjectFile::Initialize(Ctx, TM);
497
498 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000499 = getContext().getMachOSection("__TEXT", "__text",
500 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
501 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000502 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000503 = getContext().getMachOSection("__DATA", "__data", 0,
504 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000505
Eric Christopher423c9e32010-05-17 21:02:07 +0000506 TLSDataSection // .tdata
507 = getContext().getMachOSection("__DATA", "__thread_data",
508 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
509 SectionKind::getDataRel());
510 TLSBSSSection // .tbss
511 = getContext().getMachOSection("__DATA", "__thread_bss",
512 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
513 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000514
Eric Christopher423c9e32010-05-17 21:02:07 +0000515 // TODO: Verify datarel below.
516 TLSTLVSection // .tlv
517 = getContext().getMachOSection("__DATA", "__thread_vars",
518 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
519 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000520
Eric Christopherc6177a42010-05-17 22:53:55 +0000521 TLSThreadInitSection
522 = getContext().getMachOSection("__DATA", "__thread_init",
523 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
524 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000525
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000526 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000527 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000528 MCSectionMachO::S_CSTRING_LITERALS,
529 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000530 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000531 = getContext().getMachOSection("__TEXT","__ustring", 0,
532 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000533 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000534 = getContext().getMachOSection("__TEXT", "__literal4",
535 MCSectionMachO::S_4BYTE_LITERALS,
536 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000537 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000538 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000539 MCSectionMachO::S_8BYTE_LITERALS,
540 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000541
542 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
543 // to using it in -static mode.
544 SixteenByteConstantSection = 0;
545 if (TM.getRelocationModel() != Reloc::Static &&
546 TM.getTargetData()->getPointerSize() == 32)
547 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000548 getContext().getMachOSection("__TEXT", "__literal16",
549 MCSectionMachO::S_16BYTE_LITERALS,
550 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000551
552 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000553 = getContext().getMachOSection("__TEXT", "__const", 0,
554 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000555
556 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000557 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
558 MCSectionMachO::S_COALESCED |
559 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
560 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000561 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000562 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000563 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000564 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000565 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000566 = getContext().getMachOSection("__DATA", "__const", 0,
567 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000568 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000569 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000570 MCSectionMachO::S_COALESCED,
571 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000572 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000573 = getContext().getMachOSection("__DATA","__common",
574 MCSectionMachO::S_ZEROFILL,
575 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000576 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000577 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
578 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000579
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000580
581 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000582 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
583 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
584 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000585 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000586 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
587 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
588 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000589
590 if (TM.getRelocationModel() == Reloc::Static) {
591 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000592 = getContext().getMachOSection("__TEXT", "__constructor", 0,
593 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000594 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000595 = getContext().getMachOSection("__TEXT", "__destructor", 0,
596 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000597 } else {
598 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000599 = getContext().getMachOSection("__DATA", "__mod_init_func",
600 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
601 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000602 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000603 = getContext().getMachOSection("__DATA", "__mod_term_func",
604 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
605 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000606 }
607
608 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000609 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
610 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000611 // Debug Information.
612 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000613 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000614 MCSectionMachO::S_ATTR_DEBUG,
615 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000616 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000617 getContext().getMachOSection("__DWARF", "__debug_info",
618 MCSectionMachO::S_ATTR_DEBUG,
619 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000620 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000621 getContext().getMachOSection("__DWARF", "__debug_line",
622 MCSectionMachO::S_ATTR_DEBUG,
623 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000624 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000625 getContext().getMachOSection("__DWARF", "__debug_frame",
626 MCSectionMachO::S_ATTR_DEBUG,
627 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000628 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000629 getContext().getMachOSection("__DWARF", "__debug_pubnames",
630 MCSectionMachO::S_ATTR_DEBUG,
631 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000632 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000633 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
634 MCSectionMachO::S_ATTR_DEBUG,
635 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000636 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000637 getContext().getMachOSection("__DWARF", "__debug_str",
638 MCSectionMachO::S_ATTR_DEBUG,
639 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000640 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000641 getContext().getMachOSection("__DWARF", "__debug_loc",
642 MCSectionMachO::S_ATTR_DEBUG,
643 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000644 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000645 getContext().getMachOSection("__DWARF", "__debug_aranges",
646 MCSectionMachO::S_ATTR_DEBUG,
647 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000648 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000649 getContext().getMachOSection("__DWARF", "__debug_ranges",
650 MCSectionMachO::S_ATTR_DEBUG,
651 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000652 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000653 getContext().getMachOSection("__DWARF", "__debug_macinfo",
654 MCSectionMachO::S_ATTR_DEBUG,
655 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000656 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000657 getContext().getMachOSection("__DWARF", "__debug_inlined",
658 MCSectionMachO::S_ATTR_DEBUG,
659 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000660
Eric Christopher8116ca52010-05-22 00:10:22 +0000661 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000662}
663
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000664const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
665 return getContext().getMachOSection("__TEXT", "__eh_frame",
666 MCSectionMachO::S_COALESCED |
667 MCSectionMachO::S_ATTR_NO_TOC |
668 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
669 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
670 SectionKind::getReadOnly());
671}
672
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000673const MCSection *TargetLoweringObjectFileMachO::
674getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
675 Mangler *Mang, const TargetMachine &TM) const {
676 // Parse the section specifier and create it if valid.
677 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000678 unsigned TAA = 0, StubSize = 0;
679 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000680 std::string ErrorCode =
681 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000682 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000683 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000684 // If invalid, report the error with report_fatal_error.
685 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000686 "' has an invalid section specifier '" + GV->getSection()+
687 "': " + ErrorCode + ".");
688 // Fall back to dropping it into the data section.
689 return DataSection;
690 }
691
692 // Get the section.
693 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000694 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000695
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000696 // If TAA wasn't set by ParseSectionSpecifier() above,
697 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000698 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000699 TAA = S->getTypeAndAttributes();
700
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000701 // Okay, now that we got the section, verify that the TAA & StubSize agree.
702 // If the user declared multiple globals with different section flags, we need
703 // to reject it here.
704 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000705 // If invalid, report the error with report_fatal_error.
706 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000707 "' section type or attributes does not match previous"
708 " section specifier");
709 }
710
711 return S;
712}
713
714const MCSection *TargetLoweringObjectFileMachO::
715SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000716 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000717
Eric Christopher02b46bc2010-05-25 21:28:50 +0000718 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000719 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000720 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000721
722 if (Kind.isText())
723 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
724
725 // If this is weak/linkonce, put this in a coalescable section, either in text
726 // or data depending on if it is writable.
727 if (GV->isWeakForLinker()) {
728 if (Kind.isReadOnly())
729 return ConstTextCoalSection;
730 return DataCoalSection;
731 }
732
733 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000734 if (Kind.isMergeable1ByteCString() &&
735 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
736 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000737
Chris Lattner98f15d22010-03-07 04:28:09 +0000738 // Do not put 16-bit arrays in the UString section if they have an
739 // externally visible label, this runs into issues with certain linker
740 // versions.
741 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
742 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
743 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000744
745 if (Kind.isMergeableConst()) {
746 if (Kind.isMergeableConst4())
747 return FourByteConstantSection;
748 if (Kind.isMergeableConst8())
749 return EightByteConstantSection;
750 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
751 return SixteenByteConstantSection;
752 }
753
754 // Otherwise, if it is readonly, but not something we can specially optimize,
755 // just drop it in .const.
756 if (Kind.isReadOnly())
757 return ReadOnlySection;
758
759 // If this is marked const, put it into a const section. But if the dynamic
760 // linker needs to write to it, put it in the data segment.
761 if (Kind.isReadOnlyWithRel())
762 return ConstDataSection;
763
764 // Put zero initialized globals with strong external linkage in the
765 // DATA, __common section with the .zerofill directive.
766 if (Kind.isBSSExtern())
767 return DataCommonSection;
768
769 // Put zero initialized globals with local linkage in __DATA,__bss directive
770 // with the .zerofill directive (aka .lcomm).
771 if (Kind.isBSSLocal())
772 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000773
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000774 // Otherwise, just drop the variable in the normal data section.
775 return DataSection;
776}
777
778const MCSection *
779TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
780 // If this constant requires a relocation, we have to put it in the data
781 // segment, not in the text segment.
782 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
783 return ConstDataSection;
784
785 if (Kind.isMergeableConst4())
786 return FourByteConstantSection;
787 if (Kind.isMergeableConst8())
788 return EightByteConstantSection;
789 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
790 return SixteenByteConstantSection;
791 return ReadOnlySection; // .const
792}
793
794/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
795/// not to emit the UsedDirective for some symbols in llvm.used.
796// FIXME: REMOVE this (rdar://7071300)
797bool TargetLoweringObjectFileMachO::
798shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
799 /// On Darwin, internally linked data beginning with "L" or "l" does not have
800 /// the directive emitted (this occurs in ObjC metadata).
801 if (!GV) return false;
802
Bill Wendling07d31772010-06-29 22:34:52 +0000803 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000804 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
805 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000806 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
807 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000808 MCSymbol *Sym = Mang->getSymbol(GV);
809 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000810 return false;
811 }
812
813 return true;
814}
815
816const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000817getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
818 MachineModuleInfo *MMI, unsigned Encoding,
819 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000820 // The mach-o version of this method defaults to returning a stub reference.
821
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000822 if (Encoding & DW_EH_PE_indirect) {
823 MachineModuleInfoMachO &MachOMMI =
824 MMI->getObjFileInfo<MachineModuleInfoMachO>();
825
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000826 SmallString<128> Name;
827 Mang->getNameWithPrefix(Name, GV, true);
828 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000829
830 // Add information about the stub reference to MachOMMI so that the stub
831 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000832 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000833 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000834 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000835 MCSymbol *Sym = Mang->getSymbol(GV);
836 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000837 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000838
839 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000840 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000841 }
842
843 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000844 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000845}
846
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000847MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindola60246a92011-04-27 23:17:57 +0000848getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000849 MachineModuleInfo *MMI) const {
850 // The mach-o version of this method defaults to returning a stub reference.
851 MachineModuleInfoMachO &MachOMMI =
852 MMI->getObjFileInfo<MachineModuleInfoMachO>();
853
854 SmallString<128> Name;
855 Mang->getNameWithPrefix(Name, GV, true);
856 Name += "$non_lazy_ptr";
857
858 // Add information about the stub reference to MachOMMI so that the stub
859 // gets emitted by the asmprinter.
860 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
861 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
862 if (StubSym.getPointer() == 0) {
863 MCSymbol *Sym = Mang->getSymbol(GV);
864 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
865 }
866
867 return SSym;
868}
869
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000870unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
871 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
872}
873
874unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
875 return DW_EH_PE_pcrel;
876}
877
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000878unsigned TargetLoweringObjectFileMachO::getFDEEncoding(bool CFI) const {
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000879 return DW_EH_PE_pcrel;
880}
881
882unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000883 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000884}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000885
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000886//===----------------------------------------------------------------------===//
887// COFF
888//===----------------------------------------------------------------------===//
889
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000890void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
891 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000892 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000893 TextSection =
894 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000895 COFF::IMAGE_SCN_CNT_CODE |
896 COFF::IMAGE_SCN_MEM_EXECUTE |
897 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000898 SectionKind::getText());
899 DataSection =
900 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000901 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
902 COFF::IMAGE_SCN_MEM_READ |
903 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000904 SectionKind::getDataRel());
905 ReadOnlySection =
906 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000907 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
908 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000909 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000910 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000911 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000912 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
913 COFF::IMAGE_SCN_MEM_READ |
914 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000915 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000916 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000917 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000918 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
919 COFF::IMAGE_SCN_MEM_READ |
920 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000921 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000922
923 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
924 // though it contains relocatable pointers. In PIC mode, this is probably a
925 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
926 // adjusted or this should be a data section.
927 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000928 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000929 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
930 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000931 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000932 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000933 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000934 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000935 COFF::IMAGE_SCN_MEM_DISCARDABLE |
936 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000937 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000938 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000939 getContext().getCOFFSection(".debug_info",
Daniel Dunbar94610582010-07-01 20:07:24 +0000940 COFF::IMAGE_SCN_MEM_DISCARDABLE |
941 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000942 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000943 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000944 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000945 COFF::IMAGE_SCN_MEM_DISCARDABLE |
946 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000947 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000948 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000949 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000950 COFF::IMAGE_SCN_MEM_DISCARDABLE |
951 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000952 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000953 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000954 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +0000955 COFF::IMAGE_SCN_MEM_DISCARDABLE |
956 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000957 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000958 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000959 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +0000960 COFF::IMAGE_SCN_MEM_DISCARDABLE |
961 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000962 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000963 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000964 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +0000965 COFF::IMAGE_SCN_MEM_DISCARDABLE |
966 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000967 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000968 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000969 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000970 COFF::IMAGE_SCN_MEM_DISCARDABLE |
971 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000972 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000973 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000974 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000975 COFF::IMAGE_SCN_MEM_DISCARDABLE |
976 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000977 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000978 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000979 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000980 COFF::IMAGE_SCN_MEM_DISCARDABLE |
981 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000982 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000983 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000984 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000985 COFF::IMAGE_SCN_MEM_DISCARDABLE |
986 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000987 SectionKind::getMetadata());
988
989 DrectveSection =
990 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000991 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000992 SectionKind::getMetadata());
Charles Davisf3ffc2c2011-05-20 22:13:55 +0000993
994 PDataSection =
995 getContext().getCOFFSection(".pdata",
996 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
997 COFF::IMAGE_SCN_MEM_READ |
998 COFF::IMAGE_SCN_MEM_WRITE,
999 SectionKind::getDataRel());
1000
1001 XDataSection =
1002 getContext().getCOFFSection(".xdata",
1003 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1004 COFF::IMAGE_SCN_MEM_READ |
1005 COFF::IMAGE_SCN_MEM_WRITE,
1006 SectionKind::getDataRel());
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001007}
1008
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001009const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
1010 return getContext().getCOFFSection(".eh_frame",
1011 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1012 COFF::IMAGE_SCN_MEM_READ |
1013 COFF::IMAGE_SCN_MEM_WRITE,
1014 SectionKind::getDataRel());
1015}
1016
Charles Davis88c81642011-05-27 21:38:47 +00001017const MCSection *TargetLoweringObjectFileCOFF::getWin64EHFuncTableSection(
1018 StringRef suffix) const {
1019 if (suffix == "")
1020 return PDataSection;
1021 return getContext().getCOFFSection((".pdata"+suffix).str(),
1022 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1023 COFF::IMAGE_SCN_MEM_READ |
1024 COFF::IMAGE_SCN_MEM_WRITE,
1025 SectionKind::getDataRel());
1026}
1027
1028const MCSection *TargetLoweringObjectFileCOFF::getWin64EHTableSection(
1029 StringRef suffix) const {
1030 if (suffix == "")
1031 return XDataSection;
1032 return getContext().getCOFFSection((".xdata"+suffix).str(),
1033 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1034 COFF::IMAGE_SCN_MEM_READ |
1035 COFF::IMAGE_SCN_MEM_WRITE,
1036 SectionKind::getDataRel());
1037}
1038
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001039
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001040static unsigned
1041getCOFFSectionFlags(SectionKind K) {
1042 unsigned Flags = 0;
1043
Anton Korobeynikov36335be2010-07-06 15:24:56 +00001044 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +00001045 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001046 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001047 else if (K.isText())
1048 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001049 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +00001050 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +00001051 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001052 else if (K.isBSS ())
1053 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001054 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1055 COFF::IMAGE_SCN_MEM_READ |
1056 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001057 else if (K.isReadOnly())
1058 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001059 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1060 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001061 else if (K.isWriteable())
1062 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001063 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1064 COFF::IMAGE_SCN_MEM_READ |
1065 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001066
1067 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001068}
1069
1070const MCSection *TargetLoweringObjectFileCOFF::
1071getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1072 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001073 return getContext().getCOFFSection(GV->getSection(),
1074 getCOFFSectionFlags(Kind),
1075 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001076}
1077
1078static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1079 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001080 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +00001081 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001082 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001083 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001084 return ".data$";
1085 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001086}
1087
1088
1089const MCSection *TargetLoweringObjectFileCOFF::
1090SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1091 Mangler *Mang, const TargetMachine &TM) const {
1092 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1093
1094 // If this global is linkonce/weak and the target handles this by emitting it
1095 // into a 'uniqued' section name, create and return the section now.
1096 if (GV->isWeakForLinker()) {
1097 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1098 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +00001099 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001100 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001101
1102 unsigned Characteristics = getCOFFSectionFlags(Kind);
1103
Daniel Dunbar94610582010-07-01 20:07:24 +00001104 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001105
1106 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001107 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001108 }
1109
1110 if (Kind.isText())
1111 return getTextSection();
1112
1113 return getDataSection();
1114}
1115