blob: df42b2ca0b5699c411ec40d3dde5ae4c81ed47f9 [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: {
Rafael Espindolafb66f472011-06-13 03:09:13 +0000192 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
193 Mang->getSymbol(GV)->getName());
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000194 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 {
Rafael Espindolafb66f472011-06-13 03:09:13 +0000202 SmallString<64> NameData("DW.ref.");
203 NameData += Sym->getName();
204 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
Rafael Espindola018e38c2011-04-27 21:29:52 +0000205 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
206 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindolafb66f472011-06-13 03:09:13 +0000207 StringRef Prefix = ".data.";
208 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola018e38c2011-04-27 21:29:52 +0000209 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
258 return K;
259}
260
261
262static unsigned getELFSectionType(StringRef Name, SectionKind K) {
263
264 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000265 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000266
267 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000268 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000269
270 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000271 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000272
273 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000274 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000275
Rafael Espindolac85dca62011-01-23 04:28:49 +0000276 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000277}
278
279
280static unsigned
281getELFSectionFlags(SectionKind K) {
282 unsigned Flags = 0;
283
284 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000285 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000286
287 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000288 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000289
Rafael Espindolad846e3f2011-06-07 23:26:45 +0000290 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000291 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000292
293 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000294 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000295
296 // K.isMergeableConst() is left out to honour PR4650
297 if (K.isMergeableCString() || K.isMergeableConst4() ||
298 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000299 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000300
301 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000302 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000303
304 return Flags;
305}
306
307
308const MCSection *TargetLoweringObjectFileELF::
309getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
310 Mangler *Mang, const TargetMachine &TM) const {
311 StringRef SectionName = GV->getSection();
312
313 // Infer section flags from the section name if we can.
314 Kind = getELFKindForNamedSection(SectionName, Kind);
315
Chris Lattner287df1b2010-04-08 21:34:17 +0000316 return getContext().getELFSection(SectionName,
317 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000318 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000319}
320
Chris Lattner43ac7212010-04-13 00:36:43 +0000321/// getSectionPrefixForGlobal - Return the section prefix name used by options
322/// FunctionsSections and DataSections.
323static const char *getSectionPrefixForGlobal(SectionKind Kind) {
324 if (Kind.isText()) return ".text.";
325 if (Kind.isReadOnly()) return ".rodata.";
326
327 if (Kind.isThreadData()) return ".tdata.";
328 if (Kind.isThreadBSS()) return ".tbss.";
329
330 if (Kind.isDataNoRel()) return ".data.";
331 if (Kind.isDataRelLocal()) return ".data.rel.local.";
332 if (Kind.isDataRel()) return ".data.rel.";
333 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
334
335 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
336 return ".data.rel.ro.";
337}
338
339
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000340const MCSection *TargetLoweringObjectFileELF::
341SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
342 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000343 // If we have -ffunction-section or -fdata-section then we should emit the
344 // global value to a uniqued section specifically for it.
345 bool EmitUniquedSection;
346 if (Kind.isText())
347 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000348 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000349 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000350
351 // If this global is linkonce/weak and the target handles this by emitting it
352 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000353 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
354 !Kind.isCommon() && !Kind.isBSS()) {
355 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000356 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000357
Chris Lattner4c6741f2010-03-15 20:37:38 +0000358 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
359 MCSymbol *Sym = Mang->getSymbol(GV);
360 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000361 StringRef Group = "";
362 unsigned Flags = getELFSectionFlags(Kind);
363 if (GV->isWeakForLinker()) {
364 Group = Sym->getName();
365 Flags |= ELF::SHF_GROUP;
366 }
367
Chris Lattner287df1b2010-04-08 21:34:17 +0000368 return getContext().getELFSection(Name.str(),
369 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000370 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000371 }
372
373 if (Kind.isText()) return TextSection;
374
375 if (Kind.isMergeable1ByteCString() ||
376 Kind.isMergeable2ByteCString() ||
377 Kind.isMergeable4ByteCString()) {
378
379 // We also need alignment here.
380 // FIXME: this is getting the alignment of the character, not the
381 // alignment of the global!
382 unsigned Align =
383 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
384
385 const char *SizeSpec = ".rodata.str1.";
386 if (Kind.isMergeable2ByteCString())
387 SizeSpec = ".rodata.str2.";
388 else if (Kind.isMergeable4ByteCString())
389 SizeSpec = ".rodata.str4.";
390 else
391 assert(Kind.isMergeable1ByteCString() && "unknown string width");
392
393
394 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000395 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000396 ELF::SHF_ALLOC |
397 ELF::SHF_MERGE |
398 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000399 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000400 }
401
402 if (Kind.isMergeableConst()) {
403 if (Kind.isMergeableConst4() && MergeableConst4Section)
404 return MergeableConst4Section;
405 if (Kind.isMergeableConst8() && MergeableConst8Section)
406 return MergeableConst8Section;
407 if (Kind.isMergeableConst16() && MergeableConst16Section)
408 return MergeableConst16Section;
409 return ReadOnlySection; // .const
410 }
411
412 if (Kind.isReadOnly()) return ReadOnlySection;
413
414 if (Kind.isThreadData()) return TLSDataSection;
415 if (Kind.isThreadBSS()) return TLSBSSSection;
416
417 // Note: we claim that common symbols are put in BSSSection, but they are
418 // really emitted with the magic .comm directive, which creates a symbol table
419 // entry but not a section.
420 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
421
422 if (Kind.isDataNoRel()) return DataSection;
423 if (Kind.isDataRelLocal()) return DataRelLocalSection;
424 if (Kind.isDataRel()) return DataRelSection;
425 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
426
427 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
428 return DataRelROSection;
429}
430
431/// getSectionForConstant - Given a mergeable constant with the
432/// specified size and relocation information, return a section that it
433/// should be placed in.
434const MCSection *TargetLoweringObjectFileELF::
435getSectionForConstant(SectionKind Kind) const {
436 if (Kind.isMergeableConst4() && MergeableConst4Section)
437 return MergeableConst4Section;
438 if (Kind.isMergeableConst8() && MergeableConst8Section)
439 return MergeableConst8Section;
440 if (Kind.isMergeableConst16() && MergeableConst16Section)
441 return MergeableConst16Section;
442 if (Kind.isReadOnly())
443 return ReadOnlySection;
444
445 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
446 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
447 return DataRelROSection;
448}
449
450const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000451getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
452 MachineModuleInfo *MMI,
453 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000454
455 if (Encoding & dwarf::DW_EH_PE_indirect) {
456 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
457
458 SmallString<128> Name;
459 Mang->getNameWithPrefix(Name, GV, true);
460 Name += ".DW.stub";
461
462 // Add information about the stub reference to ELFMMI so that the stub
463 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000464 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000465 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000466 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000467 MCSymbol *Sym = Mang->getSymbol(GV);
468 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000469 }
470
471 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000472 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000473 }
474
475 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000476 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000477}
478
479//===----------------------------------------------------------------------===//
480// MachO
481//===----------------------------------------------------------------------===//
482
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000483void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
484 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000485 IsFunctionEHFrameSymbolPrivate = false;
486 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000487
Daniel Dunbarebc50662011-04-19 20:32:39 +0000488 // .comm doesn't support alignment before Leopard.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000489 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
Bill Wendling1a3ab632011-06-22 23:16:51 +0000490 if (T.isMacOSX()) {
491 if (T.isMacOSXVersionLT(10, 5))
492 CommDirectiveSupportsAlignment = false;
493 if (!T.isMacOSXVersionLT(10, 6))
494 SupportsCompactUnwindInfo = true;
495 }
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000496
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000497 TargetLoweringObjectFile::Initialize(Ctx, TM);
498
499 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000500 = getContext().getMachOSection("__TEXT", "__text",
501 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
502 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000503 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000504 = getContext().getMachOSection("__DATA", "__data", 0,
505 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000506
Eric Christopher423c9e32010-05-17 21:02:07 +0000507 TLSDataSection // .tdata
508 = getContext().getMachOSection("__DATA", "__thread_data",
509 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
510 SectionKind::getDataRel());
511 TLSBSSSection // .tbss
512 = getContext().getMachOSection("__DATA", "__thread_bss",
513 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
514 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000515
Eric Christopher423c9e32010-05-17 21:02:07 +0000516 // TODO: Verify datarel below.
517 TLSTLVSection // .tlv
518 = getContext().getMachOSection("__DATA", "__thread_vars",
519 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
520 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000521
Eric Christopherc6177a42010-05-17 22:53:55 +0000522 TLSThreadInitSection
523 = getContext().getMachOSection("__DATA", "__thread_init",
524 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
525 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000526
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000527 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000528 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000529 MCSectionMachO::S_CSTRING_LITERALS,
530 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000531 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000532 = getContext().getMachOSection("__TEXT","__ustring", 0,
533 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000534 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000535 = getContext().getMachOSection("__TEXT", "__literal4",
536 MCSectionMachO::S_4BYTE_LITERALS,
537 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000538 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000539 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000540 MCSectionMachO::S_8BYTE_LITERALS,
541 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000542
543 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
544 // to using it in -static mode.
545 SixteenByteConstantSection = 0;
546 if (TM.getRelocationModel() != Reloc::Static &&
547 TM.getTargetData()->getPointerSize() == 32)
548 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000549 getContext().getMachOSection("__TEXT", "__literal16",
550 MCSectionMachO::S_16BYTE_LITERALS,
551 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000552
553 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000554 = getContext().getMachOSection("__TEXT", "__const", 0,
555 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000556
557 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000558 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
559 MCSectionMachO::S_COALESCED |
560 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
561 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000562 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000563 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000564 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000565 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000566 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000567 = getContext().getMachOSection("__DATA", "__const", 0,
568 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000569 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000570 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000571 MCSectionMachO::S_COALESCED,
572 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000573 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000574 = getContext().getMachOSection("__DATA","__common",
575 MCSectionMachO::S_ZEROFILL,
576 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000577 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000578 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
579 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000580
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000581
582 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000583 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
584 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
585 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000586 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000587 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
588 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
589 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000590
591 if (TM.getRelocationModel() == Reloc::Static) {
592 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000593 = getContext().getMachOSection("__TEXT", "__constructor", 0,
594 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000595 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000596 = getContext().getMachOSection("__TEXT", "__destructor", 0,
597 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000598 } else {
599 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000600 = getContext().getMachOSection("__DATA", "__mod_init_func",
601 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
602 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000603 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000604 = getContext().getMachOSection("__DATA", "__mod_term_func",
605 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
606 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000607 }
608
609 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000610 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
611 SectionKind::getReadOnlyWithRel());
Bill Wendlingaa0a8f32011-06-22 22:22:24 +0000612
613 CompactUnwindSection =
614 getContext().getMachOSection("__LD", "__compact_unwind",
615 MCSectionMachO::S_ATTR_DEBUG,
616 SectionKind::getReadOnly());
617
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000618 // Debug Information.
619 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000620 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000621 MCSectionMachO::S_ATTR_DEBUG,
622 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000623 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000624 getContext().getMachOSection("__DWARF", "__debug_info",
625 MCSectionMachO::S_ATTR_DEBUG,
626 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000627 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000628 getContext().getMachOSection("__DWARF", "__debug_line",
629 MCSectionMachO::S_ATTR_DEBUG,
630 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000631 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000632 getContext().getMachOSection("__DWARF", "__debug_frame",
633 MCSectionMachO::S_ATTR_DEBUG,
634 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000635 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000636 getContext().getMachOSection("__DWARF", "__debug_pubnames",
637 MCSectionMachO::S_ATTR_DEBUG,
638 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000639 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000640 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
641 MCSectionMachO::S_ATTR_DEBUG,
642 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000643 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000644 getContext().getMachOSection("__DWARF", "__debug_str",
645 MCSectionMachO::S_ATTR_DEBUG,
646 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000647 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000648 getContext().getMachOSection("__DWARF", "__debug_loc",
649 MCSectionMachO::S_ATTR_DEBUG,
650 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000651 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000652 getContext().getMachOSection("__DWARF", "__debug_aranges",
653 MCSectionMachO::S_ATTR_DEBUG,
654 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000655 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000656 getContext().getMachOSection("__DWARF", "__debug_ranges",
657 MCSectionMachO::S_ATTR_DEBUG,
658 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000659 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000660 getContext().getMachOSection("__DWARF", "__debug_macinfo",
661 MCSectionMachO::S_ATTR_DEBUG,
662 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000663 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000664 getContext().getMachOSection("__DWARF", "__debug_inlined",
665 MCSectionMachO::S_ATTR_DEBUG,
666 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000667
Eric Christopher8116ca52010-05-22 00:10:22 +0000668 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000669}
670
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000671const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
672 return getContext().getMachOSection("__TEXT", "__eh_frame",
673 MCSectionMachO::S_COALESCED |
674 MCSectionMachO::S_ATTR_NO_TOC |
675 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
676 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
677 SectionKind::getReadOnly());
678}
679
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000680const MCSection *TargetLoweringObjectFileMachO::
681getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
682 Mangler *Mang, const TargetMachine &TM) const {
683 // Parse the section specifier and create it if valid.
684 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000685 unsigned TAA = 0, StubSize = 0;
686 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000687 std::string ErrorCode =
688 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000689 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000690 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000691 // If invalid, report the error with report_fatal_error.
692 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000693 "' has an invalid section specifier '" + GV->getSection()+
694 "': " + ErrorCode + ".");
695 // Fall back to dropping it into the data section.
696 return DataSection;
697 }
698
699 // Get the section.
700 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000701 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000702
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000703 // If TAA wasn't set by ParseSectionSpecifier() above,
704 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000705 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000706 TAA = S->getTypeAndAttributes();
707
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000708 // Okay, now that we got the section, verify that the TAA & StubSize agree.
709 // If the user declared multiple globals with different section flags, we need
710 // to reject it here.
711 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000712 // If invalid, report the error with report_fatal_error.
713 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000714 "' section type or attributes does not match previous"
715 " section specifier");
716 }
717
718 return S;
719}
720
721const MCSection *TargetLoweringObjectFileMachO::
722SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000723 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000724
Eric Christopher02b46bc2010-05-25 21:28:50 +0000725 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000726 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000727 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000728
729 if (Kind.isText())
730 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
731
732 // If this is weak/linkonce, put this in a coalescable section, either in text
733 // or data depending on if it is writable.
734 if (GV->isWeakForLinker()) {
735 if (Kind.isReadOnly())
736 return ConstTextCoalSection;
737 return DataCoalSection;
738 }
739
740 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000741 if (Kind.isMergeable1ByteCString() &&
742 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
743 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000744
Chris Lattner98f15d22010-03-07 04:28:09 +0000745 // Do not put 16-bit arrays in the UString section if they have an
746 // externally visible label, this runs into issues with certain linker
747 // versions.
748 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
749 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
750 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000751
752 if (Kind.isMergeableConst()) {
753 if (Kind.isMergeableConst4())
754 return FourByteConstantSection;
755 if (Kind.isMergeableConst8())
756 return EightByteConstantSection;
757 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
758 return SixteenByteConstantSection;
759 }
760
761 // Otherwise, if it is readonly, but not something we can specially optimize,
762 // just drop it in .const.
763 if (Kind.isReadOnly())
764 return ReadOnlySection;
765
766 // If this is marked const, put it into a const section. But if the dynamic
767 // linker needs to write to it, put it in the data segment.
768 if (Kind.isReadOnlyWithRel())
769 return ConstDataSection;
770
771 // Put zero initialized globals with strong external linkage in the
772 // DATA, __common section with the .zerofill directive.
773 if (Kind.isBSSExtern())
774 return DataCommonSection;
775
776 // Put zero initialized globals with local linkage in __DATA,__bss directive
777 // with the .zerofill directive (aka .lcomm).
778 if (Kind.isBSSLocal())
779 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000780
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000781 // Otherwise, just drop the variable in the normal data section.
782 return DataSection;
783}
784
785const MCSection *
786TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
787 // If this constant requires a relocation, we have to put it in the data
788 // segment, not in the text segment.
789 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
790 return ConstDataSection;
791
792 if (Kind.isMergeableConst4())
793 return FourByteConstantSection;
794 if (Kind.isMergeableConst8())
795 return EightByteConstantSection;
796 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
797 return SixteenByteConstantSection;
798 return ReadOnlySection; // .const
799}
800
801/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
802/// not to emit the UsedDirective for some symbols in llvm.used.
803// FIXME: REMOVE this (rdar://7071300)
804bool TargetLoweringObjectFileMachO::
805shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
806 /// On Darwin, internally linked data beginning with "L" or "l" does not have
807 /// the directive emitted (this occurs in ObjC metadata).
808 if (!GV) return false;
809
Bill Wendling07d31772010-06-29 22:34:52 +0000810 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000811 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
812 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000813 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
814 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000815 MCSymbol *Sym = Mang->getSymbol(GV);
816 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000817 return false;
818 }
819
820 return true;
821}
822
823const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000824getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
825 MachineModuleInfo *MMI, unsigned Encoding,
826 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000827 // The mach-o version of this method defaults to returning a stub reference.
828
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000829 if (Encoding & DW_EH_PE_indirect) {
830 MachineModuleInfoMachO &MachOMMI =
831 MMI->getObjFileInfo<MachineModuleInfoMachO>();
832
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000833 SmallString<128> Name;
834 Mang->getNameWithPrefix(Name, GV, true);
835 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000836
837 // Add information about the stub reference to MachOMMI so that the stub
838 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000839 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000840 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000841 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000842 MCSymbol *Sym = Mang->getSymbol(GV);
843 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000844 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000845
846 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000847 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000848 }
849
850 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000851 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000852}
853
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000854MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindola60246a92011-04-27 23:17:57 +0000855getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000856 MachineModuleInfo *MMI) const {
857 // The mach-o version of this method defaults to returning a stub reference.
858 MachineModuleInfoMachO &MachOMMI =
859 MMI->getObjFileInfo<MachineModuleInfoMachO>();
860
861 SmallString<128> Name;
862 Mang->getNameWithPrefix(Name, GV, true);
863 Name += "$non_lazy_ptr";
864
865 // Add information about the stub reference to MachOMMI so that the stub
866 // gets emitted by the asmprinter.
867 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
868 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
869 if (StubSym.getPointer() == 0) {
870 MCSymbol *Sym = Mang->getSymbol(GV);
871 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
872 }
873
874 return SSym;
875}
876
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000877unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
878 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
879}
880
881unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
882 return DW_EH_PE_pcrel;
883}
884
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000885unsigned TargetLoweringObjectFileMachO::getFDEEncoding(bool CFI) const {
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000886 return DW_EH_PE_pcrel;
887}
888
889unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000890 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000891}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000892
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000893//===----------------------------------------------------------------------===//
894// COFF
895//===----------------------------------------------------------------------===//
896
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000897void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
898 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000899 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000900 TextSection =
901 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000902 COFF::IMAGE_SCN_CNT_CODE |
903 COFF::IMAGE_SCN_MEM_EXECUTE |
904 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000905 SectionKind::getText());
906 DataSection =
907 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000908 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
909 COFF::IMAGE_SCN_MEM_READ |
910 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000911 SectionKind::getDataRel());
912 ReadOnlySection =
913 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000914 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
915 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000916 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000917 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000918 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000919 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
920 COFF::IMAGE_SCN_MEM_READ |
921 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000922 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000923 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000924 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000925 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
926 COFF::IMAGE_SCN_MEM_READ |
927 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000928 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000929
930 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
931 // though it contains relocatable pointers. In PIC mode, this is probably a
932 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
933 // adjusted or this should be a data section.
934 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000935 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000936 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
937 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000938 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000939 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000940 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000941 getContext().getCOFFSection(".debug_abbrev",
Daniel Dunbar94610582010-07-01 20:07:24 +0000942 COFF::IMAGE_SCN_MEM_DISCARDABLE |
943 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000944 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000945 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000946 getContext().getCOFFSection(".debug_info",
Daniel Dunbar94610582010-07-01 20:07:24 +0000947 COFF::IMAGE_SCN_MEM_DISCARDABLE |
948 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000949 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000950 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000951 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000952 COFF::IMAGE_SCN_MEM_DISCARDABLE |
953 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000954 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000955 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000956 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000957 COFF::IMAGE_SCN_MEM_DISCARDABLE |
958 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000959 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000960 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000961 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +0000962 COFF::IMAGE_SCN_MEM_DISCARDABLE |
963 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000964 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000965 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000966 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +0000967 COFF::IMAGE_SCN_MEM_DISCARDABLE |
968 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000969 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000970 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000971 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +0000972 COFF::IMAGE_SCN_MEM_DISCARDABLE |
973 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000974 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000975 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000976 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +0000977 COFF::IMAGE_SCN_MEM_DISCARDABLE |
978 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000979 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000980 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000981 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000982 COFF::IMAGE_SCN_MEM_DISCARDABLE |
983 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000984 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000985 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000986 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +0000987 COFF::IMAGE_SCN_MEM_DISCARDABLE |
988 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000989 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000990 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000991 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +0000992 COFF::IMAGE_SCN_MEM_DISCARDABLE |
993 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000994 SectionKind::getMetadata());
995
996 DrectveSection =
997 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +0000998 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000999 SectionKind::getMetadata());
Charles Davisf3ffc2c2011-05-20 22:13:55 +00001000
1001 PDataSection =
1002 getContext().getCOFFSection(".pdata",
1003 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1004 COFF::IMAGE_SCN_MEM_READ |
1005 COFF::IMAGE_SCN_MEM_WRITE,
1006 SectionKind::getDataRel());
1007
1008 XDataSection =
1009 getContext().getCOFFSection(".xdata",
1010 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1011 COFF::IMAGE_SCN_MEM_READ |
1012 COFF::IMAGE_SCN_MEM_WRITE,
1013 SectionKind::getDataRel());
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001014}
1015
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001016const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
1017 return getContext().getCOFFSection(".eh_frame",
1018 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1019 COFF::IMAGE_SCN_MEM_READ |
1020 COFF::IMAGE_SCN_MEM_WRITE,
1021 SectionKind::getDataRel());
1022}
1023
Charles Davis88c81642011-05-27 21:38:47 +00001024const MCSection *TargetLoweringObjectFileCOFF::getWin64EHFuncTableSection(
1025 StringRef suffix) const {
1026 if (suffix == "")
1027 return PDataSection;
1028 return getContext().getCOFFSection((".pdata"+suffix).str(),
1029 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1030 COFF::IMAGE_SCN_MEM_READ |
1031 COFF::IMAGE_SCN_MEM_WRITE,
1032 SectionKind::getDataRel());
1033}
1034
1035const MCSection *TargetLoweringObjectFileCOFF::getWin64EHTableSection(
1036 StringRef suffix) const {
1037 if (suffix == "")
1038 return XDataSection;
1039 return getContext().getCOFFSection((".xdata"+suffix).str(),
1040 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1041 COFF::IMAGE_SCN_MEM_READ |
1042 COFF::IMAGE_SCN_MEM_WRITE,
1043 SectionKind::getDataRel());
1044}
1045
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001046
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001047static unsigned
1048getCOFFSectionFlags(SectionKind K) {
1049 unsigned Flags = 0;
1050
Anton Korobeynikov36335be2010-07-06 15:24:56 +00001051 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +00001052 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001053 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001054 else if (K.isText())
1055 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001056 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +00001057 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +00001058 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001059 else if (K.isBSS ())
1060 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001061 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1062 COFF::IMAGE_SCN_MEM_READ |
1063 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001064 else if (K.isReadOnly())
1065 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001066 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1067 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001068 else if (K.isWriteable())
1069 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001070 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1071 COFF::IMAGE_SCN_MEM_READ |
1072 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001073
1074 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001075}
1076
1077const MCSection *TargetLoweringObjectFileCOFF::
1078getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1079 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001080 return getContext().getCOFFSection(GV->getSection(),
1081 getCOFFSectionFlags(Kind),
1082 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001083}
1084
1085static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1086 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001087 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +00001088 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001089 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001090 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001091 return ".data$";
1092 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001093}
1094
1095
1096const MCSection *TargetLoweringObjectFileCOFF::
1097SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1098 Mangler *Mang, const TargetMachine &TM) const {
1099 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1100
1101 // If this global is linkonce/weak and the target handles this by emitting it
1102 // into a 'uniqued' section name, create and return the section now.
1103 if (GV->isWeakForLinker()) {
1104 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1105 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +00001106 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001107 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001108
1109 unsigned Characteristics = getCOFFSectionFlags(Kind);
1110
Daniel Dunbar94610582010-07-01 20:07:24 +00001111 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001112
1113 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001114 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001115 }
1116
1117 if (Kind.isText())
1118 return getTextSection();
1119
1120 return getDataSection();
1121}
1122