blob: a3c562013b5904ff53d570c468bddd762ff1427a [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
Evan Cheng9bc402c2011-07-13 19:54:59 +000046TargetLoweringObjectFileELF::TargetLoweringObjectFileELF()
47 : TargetLoweringObjectFile(),
48 TLSDataSection(0),
49 TLSBSSSection(0),
50 DataRelSection(0),
51 DataRelLocalSection(0),
52 DataRelROSection(0),
53 DataRelROLocalSection(0),
54 MergeableConst4Section(0),
55 MergeableConst8Section(0),
56 MergeableConst16Section(0) {
57}
58
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000059void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
60 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000061 TargetLoweringObjectFile::Initialize(Ctx, TM);
62
63 BSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000064 getContext().getELFSection(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000065 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000066 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000067
68 TextSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000069 getContext().getELFSection(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000070 ELF::SHF_EXECINSTR |
71 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000072 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000073
74 DataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000075 getContext().getELFSection(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000076 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000077 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000078
79 ReadOnlySection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000080 getContext().getELFSection(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000081 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000082 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000083
84 TLSDataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000085 getContext().getELFSection(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000086 ELF::SHF_ALLOC | ELF::SHF_TLS |
87 ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000088 SectionKind::getThreadData());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000089
90 TLSBSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000091 getContext().getELFSection(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000092 ELF::SHF_ALLOC | ELF::SHF_TLS |
93 ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000094 SectionKind::getThreadBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000095
96 DataRelSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000097 getContext().getELFSection(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000098 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +000099 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000100
101 DataRelLocalSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000102 getContext().getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000103 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000104 SectionKind::getDataRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000105
106 DataRelROSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000107 getContext().getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000108 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000109 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000110
111 DataRelROLocalSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000112 getContext().getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000113 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000114 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000115
116 MergeableConst4Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000117 getContext().getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000118 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000119 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000120
121 MergeableConst8Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000122 getContext().getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000123 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000124 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000125
126 MergeableConst16Section =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000127 getContext().getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000128 ELF::SHF_ALLOC |ELF::SHF_MERGE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000129 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000130
131 StaticCtorSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000132 getContext().getELFSection(".ctors", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000133 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000134 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000135
136 StaticDtorSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000137 getContext().getELFSection(".dtors", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000138 ELF::SHF_ALLOC |ELF::SHF_WRITE,
Chris Lattner287df1b2010-04-08 21:34:17 +0000139 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000140
141 // Exception Handling Sections.
142
143 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
144 // it contains relocatable pointers. In PIC mode, this is probably a big
145 // runtime hit for C++ apps. Either the contents of the LSDA need to be
146 // adjusted or this should be a data section.
147 LSDASection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000148 getContext().getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000149 ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +0000150 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000151 // Debug Info Sections.
152 DwarfAbbrevSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000153 getContext().getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000154 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000155 DwarfInfoSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000156 getContext().getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000157 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000158 DwarfLineSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000159 getContext().getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000160 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000161 DwarfFrameSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000162 getContext().getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000163 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000164 DwarfPubNamesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000165 getContext().getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000166 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000167 DwarfPubTypesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000168 getContext().getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000169 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000170 DwarfStrSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000171 getContext().getELFSection(".debug_str", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000172 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000173 DwarfLocSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000174 getContext().getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000175 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000176 DwarfARangesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000177 getContext().getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000178 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000179 DwarfRangesSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000180 getContext().getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000181 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000182 DwarfMacroInfoSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000183 getContext().getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
Chris Lattner287df1b2010-04-08 21:34:17 +0000184 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000185}
186
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000187const MCSection *TargetLoweringObjectFileELF::getEHFrameSection() const {
188 return getContext().getELFSection(".eh_frame", ELF::SHT_PROGBITS,
189 ELF::SHF_ALLOC,
190 SectionKind::getDataRel());
191}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000192
Rafael Espindola30deafc2011-04-16 03:51:21 +0000193MCSymbol *
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000194TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000195 Mangler *Mang,
196 MachineModuleInfo *MMI) const {
Rafael Espindola60246a92011-04-27 23:17:57 +0000197 unsigned Encoding = getPersonalityEncoding();
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000198 switch (Encoding & 0x70) {
199 default:
200 report_fatal_error("We do not support this DWARF encoding yet!");
201 case dwarf::DW_EH_PE_absptr:
202 return Mang->getSymbol(GV);
203 break;
204 case dwarf::DW_EH_PE_pcrel: {
Rafael Espindolafb66f472011-06-13 03:09:13 +0000205 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
206 Mang->getSymbol(GV)->getName());
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000207 break;
208 }
209 }
Rafael Espindola30deafc2011-04-16 03:51:21 +0000210}
211
212void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
213 const TargetMachine &TM,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000214 const MCSymbol *Sym) const {
Rafael Espindolafb66f472011-06-13 03:09:13 +0000215 SmallString<64> NameData("DW.ref.");
216 NameData += Sym->getName();
217 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
Rafael Espindola018e38c2011-04-27 21:29:52 +0000218 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
219 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindolafb66f472011-06-13 03:09:13 +0000220 StringRef Prefix = ".data.";
221 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola018e38c2011-04-27 21:29:52 +0000222 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
223 const MCSection *Sec = getContext().getELFSection(NameData,
224 ELF::SHT_PROGBITS,
225 Flags,
226 SectionKind::getDataRel(),
227 0, Label->getName());
228 Streamer.SwitchSection(Sec);
229 Streamer.EmitValueToAlignment(8);
230 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
231 const MCExpr *E = MCConstantExpr::Create(8, getContext());
232 Streamer.EmitELFSize(Label, E);
233 Streamer.EmitLabel(Label);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000234
Rafael Espindola018e38c2011-04-27 21:29:52 +0000235 unsigned Size = TM.getTargetData()->getPointerSize();
236 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000237}
238
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000239static SectionKind
240getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindolae6657982011-05-24 03:10:31 +0000241 // N.B.: The defaults used in here are no the same ones used in MC.
242 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
243 // both gas and MC will produce a section with no flags. Given
244 // section(".eh_frame") gcc will produce
245 // .section .eh_frame,"a",@progbits
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000246 if (Name.empty() || Name[0] != '.') return K;
247
248 // Some lame default implementation based on some magic section names.
249 if (Name == ".bss" ||
250 Name.startswith(".bss.") ||
251 Name.startswith(".gnu.linkonce.b.") ||
252 Name.startswith(".llvm.linkonce.b.") ||
253 Name == ".sbss" ||
254 Name.startswith(".sbss.") ||
255 Name.startswith(".gnu.linkonce.sb.") ||
256 Name.startswith(".llvm.linkonce.sb."))
257 return SectionKind::getBSS();
258
259 if (Name == ".tdata" ||
260 Name.startswith(".tdata.") ||
261 Name.startswith(".gnu.linkonce.td.") ||
262 Name.startswith(".llvm.linkonce.td."))
263 return SectionKind::getThreadData();
264
265 if (Name == ".tbss" ||
266 Name.startswith(".tbss.") ||
267 Name.startswith(".gnu.linkonce.tb.") ||
268 Name.startswith(".llvm.linkonce.tb."))
269 return SectionKind::getThreadBSS();
270
271 return K;
272}
273
274
275static unsigned getELFSectionType(StringRef Name, SectionKind K) {
276
277 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000278 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000279
280 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000281 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000282
283 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000284 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000285
286 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000287 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000288
Rafael Espindolac85dca62011-01-23 04:28:49 +0000289 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000290}
291
292
293static unsigned
294getELFSectionFlags(SectionKind K) {
295 unsigned Flags = 0;
296
297 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000298 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000299
300 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000301 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000302
Rafael Espindolad846e3f2011-06-07 23:26:45 +0000303 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000304 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000305
306 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000307 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000308
309 // K.isMergeableConst() is left out to honour PR4650
310 if (K.isMergeableCString() || K.isMergeableConst4() ||
311 K.isMergeableConst8() || K.isMergeableConst16())
Rafael Espindola1c130262011-01-23 04:43:11 +0000312 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000313
314 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000315 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000316
317 return Flags;
318}
319
320
321const MCSection *TargetLoweringObjectFileELF::
322getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
323 Mangler *Mang, const TargetMachine &TM) const {
324 StringRef SectionName = GV->getSection();
325
326 // Infer section flags from the section name if we can.
327 Kind = getELFKindForNamedSection(SectionName, Kind);
328
Chris Lattner287df1b2010-04-08 21:34:17 +0000329 return getContext().getELFSection(SectionName,
330 getELFSectionType(SectionName, Kind),
Rafael Espindola34be3962010-11-09 23:42:07 +0000331 getELFSectionFlags(Kind), Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000332}
333
Chris Lattner43ac7212010-04-13 00:36:43 +0000334/// getSectionPrefixForGlobal - Return the section prefix name used by options
335/// FunctionsSections and DataSections.
336static const char *getSectionPrefixForGlobal(SectionKind Kind) {
337 if (Kind.isText()) return ".text.";
338 if (Kind.isReadOnly()) return ".rodata.";
339
340 if (Kind.isThreadData()) return ".tdata.";
341 if (Kind.isThreadBSS()) return ".tbss.";
342
343 if (Kind.isDataNoRel()) return ".data.";
344 if (Kind.isDataRelLocal()) return ".data.rel.local.";
345 if (Kind.isDataRel()) return ".data.rel.";
346 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
347
348 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
349 return ".data.rel.ro.";
350}
351
352
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000353const MCSection *TargetLoweringObjectFileELF::
354SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
355 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner43ac7212010-04-13 00:36:43 +0000356 // If we have -ffunction-section or -fdata-section then we should emit the
357 // global value to a uniqued section specifically for it.
358 bool EmitUniquedSection;
359 if (Kind.isText())
360 EmitUniquedSection = TM.getFunctionSections();
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000361 else
Chris Lattner43ac7212010-04-13 00:36:43 +0000362 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000363
364 // If this global is linkonce/weak and the target handles this by emitting it
365 // into a 'uniqued' section name, create and return the section now.
Chris Lattner43ac7212010-04-13 00:36:43 +0000366 if ((GV->isWeakForLinker() || EmitUniquedSection) &&
367 !Kind.isCommon() && !Kind.isBSS()) {
368 const char *Prefix;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000369 Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner43ac7212010-04-13 00:36:43 +0000370
Chris Lattner4c6741f2010-03-15 20:37:38 +0000371 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
372 MCSymbol *Sym = Mang->getSymbol(GV);
373 Name.append(Sym->getName().begin(), Sym->getName().end());
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000374 StringRef Group = "";
375 unsigned Flags = getELFSectionFlags(Kind);
376 if (GV->isWeakForLinker()) {
377 Group = Sym->getName();
378 Flags |= ELF::SHF_GROUP;
379 }
380
Chris Lattner287df1b2010-04-08 21:34:17 +0000381 return getContext().getELFSection(Name.str(),
382 getELFSectionType(Name.str(), Kind),
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000383 Flags, Kind, 0, Group);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000384 }
385
386 if (Kind.isText()) return TextSection;
387
388 if (Kind.isMergeable1ByteCString() ||
389 Kind.isMergeable2ByteCString() ||
390 Kind.isMergeable4ByteCString()) {
391
392 // We also need alignment here.
393 // FIXME: this is getting the alignment of the character, not the
394 // alignment of the global!
395 unsigned Align =
396 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
397
398 const char *SizeSpec = ".rodata.str1.";
399 if (Kind.isMergeable2ByteCString())
400 SizeSpec = ".rodata.str2.";
401 else if (Kind.isMergeable4ByteCString())
402 SizeSpec = ".rodata.str4.";
403 else
404 assert(Kind.isMergeable1ByteCString() && "unknown string width");
405
406
407 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolac85dca62011-01-23 04:28:49 +0000408 return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000409 ELF::SHF_ALLOC |
410 ELF::SHF_MERGE |
411 ELF::SHF_STRINGS,
Chris Lattner287df1b2010-04-08 21:34:17 +0000412 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000413 }
414
415 if (Kind.isMergeableConst()) {
416 if (Kind.isMergeableConst4() && MergeableConst4Section)
417 return MergeableConst4Section;
418 if (Kind.isMergeableConst8() && MergeableConst8Section)
419 return MergeableConst8Section;
420 if (Kind.isMergeableConst16() && MergeableConst16Section)
421 return MergeableConst16Section;
422 return ReadOnlySection; // .const
423 }
424
425 if (Kind.isReadOnly()) return ReadOnlySection;
426
427 if (Kind.isThreadData()) return TLSDataSection;
428 if (Kind.isThreadBSS()) return TLSBSSSection;
429
430 // Note: we claim that common symbols are put in BSSSection, but they are
431 // really emitted with the magic .comm directive, which creates a symbol table
432 // entry but not a section.
433 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
434
435 if (Kind.isDataNoRel()) return DataSection;
436 if (Kind.isDataRelLocal()) return DataRelLocalSection;
437 if (Kind.isDataRel()) return DataRelSection;
438 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
439
440 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
441 return DataRelROSection;
442}
443
444/// getSectionForConstant - Given a mergeable constant with the
445/// specified size and relocation information, return a section that it
446/// should be placed in.
447const MCSection *TargetLoweringObjectFileELF::
448getSectionForConstant(SectionKind Kind) const {
449 if (Kind.isMergeableConst4() && MergeableConst4Section)
450 return MergeableConst4Section;
451 if (Kind.isMergeableConst8() && MergeableConst8Section)
452 return MergeableConst8Section;
453 if (Kind.isMergeableConst16() && MergeableConst16Section)
454 return MergeableConst16Section;
455 if (Kind.isReadOnly())
456 return ReadOnlySection;
457
458 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
459 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
460 return DataRelROSection;
461}
462
463const MCExpr *TargetLoweringObjectFileELF::
Chris Lattner3192d142010-03-11 19:41:58 +0000464getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
465 MachineModuleInfo *MMI,
466 unsigned Encoding, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000467
468 if (Encoding & dwarf::DW_EH_PE_indirect) {
469 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
470
471 SmallString<128> Name;
472 Mang->getNameWithPrefix(Name, GV, true);
473 Name += ".DW.stub";
474
475 // Add information about the stub reference to ELFMMI so that the stub
476 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000477 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000478 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000479 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000480 MCSymbol *Sym = Mang->getSymbol(GV);
481 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000482 }
483
484 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000485 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000486 }
487
488 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000489 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000490}
491
492//===----------------------------------------------------------------------===//
493// MachO
494//===----------------------------------------------------------------------===//
495
Evan Cheng9bc402c2011-07-13 19:54:59 +0000496TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
497 : TargetLoweringObjectFile(),
498 TLSDataSection(0),
499 TLSBSSSection(0),
500 TLSTLVSection(0),
501 TLSThreadInitSection(0),
502 CStringSection(0),
503 UStringSection(0),
504 TextCoalSection(0),
505 ConstTextCoalSection(0),
506 ConstDataSection(0),
507 DataCoalSection(0),
508 DataCommonSection(0),
509 DataBSSSection(0),
510 FourByteConstantSection(0),
511 EightByteConstantSection(0),
512 SixteenByteConstantSection(0),
513 LazySymbolPointerSection(0),
514 NonLazySymbolPointerSection(0) {
515}
516
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000517void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
518 const TargetMachine &TM) {
Chris Lattner09d53fe2010-03-10 07:20:42 +0000519 IsFunctionEHFrameSymbolPrivate = false;
520 SupportsWeakOmittedEHFrame = false;
Chris Lattner8048ebe2010-09-27 06:44:54 +0000521
Daniel Dunbarebc50662011-04-19 20:32:39 +0000522 // .comm doesn't support alignment before Leopard.
Chris Lattner8048ebe2010-09-27 06:44:54 +0000523 Triple T(((LLVMTargetMachine&)TM).getTargetTriple());
Bill Wendling4c0c4462011-06-23 05:13:28 +0000524 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
525 CommDirectiveSupportsAlignment = false;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000526
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000527 TargetLoweringObjectFile::Initialize(Ctx, TM);
528
529 TextSection // .text
Chris Lattner22772212010-04-08 20:40:11 +0000530 = getContext().getMachOSection("__TEXT", "__text",
531 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
532 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000533 DataSection // .data
Chris Lattner22772212010-04-08 20:40:11 +0000534 = getContext().getMachOSection("__DATA", "__data", 0,
535 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000536
Eric Christopher423c9e32010-05-17 21:02:07 +0000537 TLSDataSection // .tdata
538 = getContext().getMachOSection("__DATA", "__thread_data",
539 MCSectionMachO::S_THREAD_LOCAL_REGULAR,
540 SectionKind::getDataRel());
541 TLSBSSSection // .tbss
542 = getContext().getMachOSection("__DATA", "__thread_bss",
543 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
544 SectionKind::getThreadBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000545
Eric Christopher423c9e32010-05-17 21:02:07 +0000546 // TODO: Verify datarel below.
547 TLSTLVSection // .tlv
548 = getContext().getMachOSection("__DATA", "__thread_vars",
549 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
550 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000551
Eric Christopherc6177a42010-05-17 22:53:55 +0000552 TLSThreadInitSection
553 = getContext().getMachOSection("__DATA", "__thread_init",
554 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
555 SectionKind::getDataRel());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000556
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000557 CStringSection // .cstring
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000558 = getContext().getMachOSection("__TEXT", "__cstring",
Chris Lattner22772212010-04-08 20:40:11 +0000559 MCSectionMachO::S_CSTRING_LITERALS,
560 SectionKind::getMergeable1ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000561 UStringSection
Chris Lattner22772212010-04-08 20:40:11 +0000562 = getContext().getMachOSection("__TEXT","__ustring", 0,
563 SectionKind::getMergeable2ByteCString());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000564 FourByteConstantSection // .literal4
Chris Lattner22772212010-04-08 20:40:11 +0000565 = getContext().getMachOSection("__TEXT", "__literal4",
566 MCSectionMachO::S_4BYTE_LITERALS,
567 SectionKind::getMergeableConst4());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000568 EightByteConstantSection // .literal8
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000569 = getContext().getMachOSection("__TEXT", "__literal8",
Chris Lattner22772212010-04-08 20:40:11 +0000570 MCSectionMachO::S_8BYTE_LITERALS,
571 SectionKind::getMergeableConst8());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000572
573 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
574 // to using it in -static mode.
575 SixteenByteConstantSection = 0;
576 if (TM.getRelocationModel() != Reloc::Static &&
577 TM.getTargetData()->getPointerSize() == 32)
578 SixteenByteConstantSection = // .literal16
Chris Lattner22772212010-04-08 20:40:11 +0000579 getContext().getMachOSection("__TEXT", "__literal16",
580 MCSectionMachO::S_16BYTE_LITERALS,
581 SectionKind::getMergeableConst16());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000582
583 ReadOnlySection // .const
Chris Lattner22772212010-04-08 20:40:11 +0000584 = getContext().getMachOSection("__TEXT", "__const", 0,
585 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000586
587 TextCoalSection
Chris Lattner22772212010-04-08 20:40:11 +0000588 = getContext().getMachOSection("__TEXT", "__textcoal_nt",
589 MCSectionMachO::S_COALESCED |
590 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
591 SectionKind::getText());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000592 ConstTextCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000593 = getContext().getMachOSection("__TEXT", "__const_coal",
Chris Lattner22772212010-04-08 20:40:11 +0000594 MCSectionMachO::S_COALESCED,
Chris Lattner6a624a62010-07-15 21:22:00 +0000595 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000596 ConstDataSection // .const_data
Chris Lattner22772212010-04-08 20:40:11 +0000597 = getContext().getMachOSection("__DATA", "__const", 0,
598 SectionKind::getReadOnlyWithRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000599 DataCoalSection
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000600 = getContext().getMachOSection("__DATA","__datacoal_nt",
Chris Lattner22772212010-04-08 20:40:11 +0000601 MCSectionMachO::S_COALESCED,
602 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000603 DataCommonSection
Chris Lattner22772212010-04-08 20:40:11 +0000604 = getContext().getMachOSection("__DATA","__common",
605 MCSectionMachO::S_ZEROFILL,
606 SectionKind::getBSS());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000607 DataBSSSection
Chris Lattner22772212010-04-08 20:40:11 +0000608 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
609 SectionKind::getBSS());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000610
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000611
612 LazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000613 = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
614 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
615 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000616 NonLazySymbolPointerSection
Chris Lattner22772212010-04-08 20:40:11 +0000617 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
618 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
619 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000620
621 if (TM.getRelocationModel() == Reloc::Static) {
622 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000623 = getContext().getMachOSection("__TEXT", "__constructor", 0,
624 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000625 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000626 = getContext().getMachOSection("__TEXT", "__destructor", 0,
627 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000628 } else {
629 StaticCtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000630 = getContext().getMachOSection("__DATA", "__mod_init_func",
631 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
632 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000633 StaticDtorSection
Chris Lattner22772212010-04-08 20:40:11 +0000634 = getContext().getMachOSection("__DATA", "__mod_term_func",
635 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
636 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000637 }
638
639 // Exception Handling.
Chris Lattner22772212010-04-08 20:40:11 +0000640 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
641 SectionKind::getReadOnlyWithRel());
Bill Wendlingaa0a8f32011-06-22 22:22:24 +0000642
Bill Wendling4c0c4462011-06-23 05:13:28 +0000643 if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
644 CompactUnwindSection =
645 getContext().getMachOSection("__LD", "__compact_unwind",
646 MCSectionMachO::S_ATTR_DEBUG,
647 SectionKind::getReadOnly());
Bill Wendlingaa0a8f32011-06-22 22:22:24 +0000648
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000649 // Debug Information.
650 DwarfAbbrevSection =
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000651 getContext().getMachOSection("__DWARF", "__debug_abbrev",
Chris Lattner22772212010-04-08 20:40:11 +0000652 MCSectionMachO::S_ATTR_DEBUG,
653 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000654 DwarfInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000655 getContext().getMachOSection("__DWARF", "__debug_info",
656 MCSectionMachO::S_ATTR_DEBUG,
657 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000658 DwarfLineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000659 getContext().getMachOSection("__DWARF", "__debug_line",
660 MCSectionMachO::S_ATTR_DEBUG,
661 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000662 DwarfFrameSection =
Chris Lattner22772212010-04-08 20:40:11 +0000663 getContext().getMachOSection("__DWARF", "__debug_frame",
664 MCSectionMachO::S_ATTR_DEBUG,
665 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000666 DwarfPubNamesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000667 getContext().getMachOSection("__DWARF", "__debug_pubnames",
668 MCSectionMachO::S_ATTR_DEBUG,
669 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000670 DwarfPubTypesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000671 getContext().getMachOSection("__DWARF", "__debug_pubtypes",
672 MCSectionMachO::S_ATTR_DEBUG,
673 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000674 DwarfStrSection =
Chris Lattner22772212010-04-08 20:40:11 +0000675 getContext().getMachOSection("__DWARF", "__debug_str",
676 MCSectionMachO::S_ATTR_DEBUG,
677 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000678 DwarfLocSection =
Chris Lattner22772212010-04-08 20:40:11 +0000679 getContext().getMachOSection("__DWARF", "__debug_loc",
680 MCSectionMachO::S_ATTR_DEBUG,
681 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000682 DwarfARangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000683 getContext().getMachOSection("__DWARF", "__debug_aranges",
684 MCSectionMachO::S_ATTR_DEBUG,
685 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000686 DwarfRangesSection =
Chris Lattner22772212010-04-08 20:40:11 +0000687 getContext().getMachOSection("__DWARF", "__debug_ranges",
688 MCSectionMachO::S_ATTR_DEBUG,
689 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000690 DwarfMacroInfoSection =
Chris Lattner22772212010-04-08 20:40:11 +0000691 getContext().getMachOSection("__DWARF", "__debug_macinfo",
692 MCSectionMachO::S_ATTR_DEBUG,
693 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000694 DwarfDebugInlineSection =
Chris Lattner22772212010-04-08 20:40:11 +0000695 getContext().getMachOSection("__DWARF", "__debug_inlined",
696 MCSectionMachO::S_ATTR_DEBUG,
697 SectionKind::getMetadata());
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000698
Eric Christopher8116ca52010-05-22 00:10:22 +0000699 TLSExtraDataSection = TLSTLVSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000700}
701
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000702const MCSection *TargetLoweringObjectFileMachO::getEHFrameSection() const {
703 return getContext().getMachOSection("__TEXT", "__eh_frame",
704 MCSectionMachO::S_COALESCED |
705 MCSectionMachO::S_ATTR_NO_TOC |
706 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
707 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
708 SectionKind::getReadOnly());
709}
710
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000711const MCSection *TargetLoweringObjectFileMachO::
712getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
713 Mangler *Mang, const TargetMachine &TM) const {
714 // Parse the section specifier and create it if valid.
715 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000716 unsigned TAA = 0, StubSize = 0;
717 bool TAAParsed;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000718 std::string ErrorCode =
719 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000720 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000721 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000722 // If invalid, report the error with report_fatal_error.
723 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000724 "' has an invalid section specifier '" + GV->getSection()+
725 "': " + ErrorCode + ".");
726 // Fall back to dropping it into the data section.
727 return DataSection;
728 }
729
730 // Get the section.
731 const MCSectionMachO *S =
Chris Lattner22772212010-04-08 20:40:11 +0000732 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000733
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000734 // If TAA wasn't set by ParseSectionSpecifier() above,
735 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000736 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000737 TAA = S->getTypeAndAttributes();
738
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000739 // Okay, now that we got the section, verify that the TAA & StubSize agree.
740 // If the user declared multiple globals with different section flags, we need
741 // to reject it here.
742 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000743 // If invalid, report the error with report_fatal_error.
744 report_fatal_error("Global variable '" + GV->getNameStr() +
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000745 "' section type or attributes does not match previous"
746 " section specifier");
747 }
748
749 return S;
750}
751
752const MCSection *TargetLoweringObjectFileMachO::
753SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher8116ca52010-05-22 00:10:22 +0000754 Mangler *Mang, const TargetMachine &TM) const {
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000755
Eric Christopher02b46bc2010-05-25 21:28:50 +0000756 // Handle thread local data.
Eric Christopher8116ca52010-05-22 00:10:22 +0000757 if (Kind.isThreadBSS()) return TLSBSSSection;
Eric Christopher02b46bc2010-05-25 21:28:50 +0000758 if (Kind.isThreadData()) return TLSDataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000759
760 if (Kind.isText())
761 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
762
763 // If this is weak/linkonce, put this in a coalescable section, either in text
764 // or data depending on if it is writable.
765 if (GV->isWeakForLinker()) {
766 if (Kind.isReadOnly())
767 return ConstTextCoalSection;
768 return DataCoalSection;
769 }
770
771 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000772 if (Kind.isMergeable1ByteCString() &&
773 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
774 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000775
Chris Lattner98f15d22010-03-07 04:28:09 +0000776 // Do not put 16-bit arrays in the UString section if they have an
777 // externally visible label, this runs into issues with certain linker
778 // versions.
779 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
780 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
781 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000782
783 if (Kind.isMergeableConst()) {
784 if (Kind.isMergeableConst4())
785 return FourByteConstantSection;
786 if (Kind.isMergeableConst8())
787 return EightByteConstantSection;
788 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
789 return SixteenByteConstantSection;
790 }
791
792 // Otherwise, if it is readonly, but not something we can specially optimize,
793 // just drop it in .const.
794 if (Kind.isReadOnly())
795 return ReadOnlySection;
796
797 // If this is marked const, put it into a const section. But if the dynamic
798 // linker needs to write to it, put it in the data segment.
799 if (Kind.isReadOnlyWithRel())
800 return ConstDataSection;
801
802 // Put zero initialized globals with strong external linkage in the
803 // DATA, __common section with the .zerofill directive.
804 if (Kind.isBSSExtern())
805 return DataCommonSection;
806
807 // Put zero initialized globals with local linkage in __DATA,__bss directive
808 // with the .zerofill directive (aka .lcomm).
809 if (Kind.isBSSLocal())
810 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000811
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000812 // Otherwise, just drop the variable in the normal data section.
813 return DataSection;
814}
815
816const MCSection *
817TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
818 // If this constant requires a relocation, we have to put it in the data
819 // segment, not in the text segment.
820 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
821 return ConstDataSection;
822
823 if (Kind.isMergeableConst4())
824 return FourByteConstantSection;
825 if (Kind.isMergeableConst8())
826 return EightByteConstantSection;
827 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
828 return SixteenByteConstantSection;
829 return ReadOnlySection; // .const
830}
831
832/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
833/// not to emit the UsedDirective for some symbols in llvm.used.
834// FIXME: REMOVE this (rdar://7071300)
835bool TargetLoweringObjectFileMachO::
836shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
837 /// On Darwin, internally linked data beginning with "L" or "l" does not have
838 /// the directive emitted (this occurs in ObjC metadata).
839 if (!GV) return false;
840
Bill Wendling07d31772010-06-29 22:34:52 +0000841 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000842 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
843 // FIXME: ObjC metadata is currently emitted as internal symbols that have
Bill Wendling07d31772010-06-29 22:34:52 +0000844 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
845 // this horrible hack can go away.
Chris Lattner4c6741f2010-03-15 20:37:38 +0000846 MCSymbol *Sym = Mang->getSymbol(GV);
847 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000848 return false;
849 }
850
851 return true;
852}
853
854const MCExpr *TargetLoweringObjectFileMachO::
Chris Lattner3192d142010-03-11 19:41:58 +0000855getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
856 MachineModuleInfo *MMI, unsigned Encoding,
857 MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000858 // The mach-o version of this method defaults to returning a stub reference.
859
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000860 if (Encoding & DW_EH_PE_indirect) {
861 MachineModuleInfoMachO &MachOMMI =
862 MMI->getObjFileInfo<MachineModuleInfoMachO>();
863
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000864 SmallString<128> Name;
865 Mang->getNameWithPrefix(Name, GV, true);
866 Name += "$non_lazy_ptr";
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000867
868 // Add information about the stub reference to MachOMMI so that the stub
869 // gets emitted by the asmprinter.
Chris Lattner9b97a732010-03-30 18:10:53 +0000870 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
Chris Lattner4c6741f2010-03-15 20:37:38 +0000871 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000872 if (StubSym.getPointer() == 0) {
Chris Lattner4c6741f2010-03-15 20:37:38 +0000873 MCSymbol *Sym = Mang->getSymbol(GV);
874 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000875 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000876
877 return TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000878 getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000879 }
880
881 return TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000882 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000883}
884
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000885MCSymbol *TargetLoweringObjectFileMachO::
Rafael Espindola60246a92011-04-27 23:17:57 +0000886getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000887 MachineModuleInfo *MMI) const {
888 // The mach-o version of this method defaults to returning a stub reference.
889 MachineModuleInfoMachO &MachOMMI =
890 MMI->getObjFileInfo<MachineModuleInfoMachO>();
891
892 SmallString<128> Name;
893 Mang->getNameWithPrefix(Name, GV, true);
894 Name += "$non_lazy_ptr";
895
896 // Add information about the stub reference to MachOMMI so that the stub
897 // gets emitted by the asmprinter.
898 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
899 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
900 if (StubSym.getPointer() == 0) {
901 MCSymbol *Sym = Mang->getSymbol(GV);
902 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
903 }
904
905 return SSym;
906}
907
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000908unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
909 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
910}
911
912unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
913 return DW_EH_PE_pcrel;
914}
915
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000916unsigned TargetLoweringObjectFileMachO::getFDEEncoding(bool CFI) const {
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000917 return DW_EH_PE_pcrel;
918}
919
920unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
Bill Wendling505ad8b2010-03-15 21:09:38 +0000921 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000922}
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000923
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000924//===----------------------------------------------------------------------===//
925// COFF
926//===----------------------------------------------------------------------===//
927
Evan Cheng9bc402c2011-07-13 19:54:59 +0000928TargetLoweringObjectFileCOFF::TargetLoweringObjectFileCOFF()
929 : TargetLoweringObjectFile(),
930 DrectveSection(0),
931 PDataSection(0),
932 XDataSection(0) {
933}
934
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000935void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
936 const TargetMachine &TM) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000937 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000938 TextSection =
939 getContext().getCOFFSection(".text",
Daniel Dunbar94610582010-07-01 20:07:24 +0000940 COFF::IMAGE_SCN_CNT_CODE |
941 COFF::IMAGE_SCN_MEM_EXECUTE |
942 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000943 SectionKind::getText());
944 DataSection =
945 getContext().getCOFFSection(".data",
Daniel Dunbar94610582010-07-01 20:07:24 +0000946 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
947 COFF::IMAGE_SCN_MEM_READ |
948 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000949 SectionKind::getDataRel());
950 ReadOnlySection =
951 getContext().getCOFFSection(".rdata",
Daniel Dunbar94610582010-07-01 20:07:24 +0000952 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
953 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000954 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000955 StaticCtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000956 getContext().getCOFFSection(".ctors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000957 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
958 COFF::IMAGE_SCN_MEM_READ |
959 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000960 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000961 StaticDtorSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000962 getContext().getCOFFSection(".dtors",
Daniel Dunbar94610582010-07-01 20:07:24 +0000963 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
964 COFF::IMAGE_SCN_MEM_READ |
965 COFF::IMAGE_SCN_MEM_WRITE,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000966 SectionKind::getDataRel());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000967
968 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
969 // though it contains relocatable pointers. In PIC mode, this is probably a
970 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
971 // adjusted or this should be a data section.
972 LSDASection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000973 getContext().getCOFFSection(".gcc_except_table",
Daniel Dunbar94610582010-07-01 20:07:24 +0000974 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
975 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000976 SectionKind::getReadOnly());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000977 // Debug info.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000978 DwarfAbbrevSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000979 getContext().getCOFFSection(".debug_abbrev",
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 DwarfInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000984 getContext().getCOFFSection(".debug_info",
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());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000988 DwarfLineSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000989 getContext().getCOFFSection(".debug_line",
Daniel Dunbar94610582010-07-01 20:07:24 +0000990 COFF::IMAGE_SCN_MEM_DISCARDABLE |
991 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000992 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000993 DwarfFrameSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000994 getContext().getCOFFSection(".debug_frame",
Daniel Dunbar94610582010-07-01 20:07:24 +0000995 COFF::IMAGE_SCN_MEM_DISCARDABLE |
996 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000997 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000998 DwarfPubNamesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000999 getContext().getCOFFSection(".debug_pubnames",
Daniel Dunbar94610582010-07-01 20:07:24 +00001000 COFF::IMAGE_SCN_MEM_DISCARDABLE |
1001 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001002 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001003 DwarfPubTypesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001004 getContext().getCOFFSection(".debug_pubtypes",
Daniel Dunbar94610582010-07-01 20:07:24 +00001005 COFF::IMAGE_SCN_MEM_DISCARDABLE |
1006 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001007 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001008 DwarfStrSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001009 getContext().getCOFFSection(".debug_str",
Daniel Dunbar94610582010-07-01 20:07:24 +00001010 COFF::IMAGE_SCN_MEM_DISCARDABLE |
1011 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001012 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001013 DwarfLocSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001014 getContext().getCOFFSection(".debug_loc",
Daniel Dunbar94610582010-07-01 20:07:24 +00001015 COFF::IMAGE_SCN_MEM_DISCARDABLE |
1016 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001017 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001018 DwarfARangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001019 getContext().getCOFFSection(".debug_aranges",
Daniel Dunbar94610582010-07-01 20:07:24 +00001020 COFF::IMAGE_SCN_MEM_DISCARDABLE |
1021 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001022 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001023 DwarfRangesSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001024 getContext().getCOFFSection(".debug_ranges",
Daniel Dunbar94610582010-07-01 20:07:24 +00001025 COFF::IMAGE_SCN_MEM_DISCARDABLE |
1026 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001027 SectionKind::getMetadata());
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001028 DwarfMacroInfoSection =
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001029 getContext().getCOFFSection(".debug_macinfo",
Daniel Dunbar94610582010-07-01 20:07:24 +00001030 COFF::IMAGE_SCN_MEM_DISCARDABLE |
1031 COFF::IMAGE_SCN_MEM_READ,
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001032 SectionKind::getMetadata());
1033
1034 DrectveSection =
1035 getContext().getCOFFSection(".drectve",
Daniel Dunbar94610582010-07-01 20:07:24 +00001036 COFF::IMAGE_SCN_LNK_INFO,
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001037 SectionKind::getMetadata());
Charles Davisf3ffc2c2011-05-20 22:13:55 +00001038
1039 PDataSection =
1040 getContext().getCOFFSection(".pdata",
1041 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1042 COFF::IMAGE_SCN_MEM_READ |
1043 COFF::IMAGE_SCN_MEM_WRITE,
1044 SectionKind::getDataRel());
1045
1046 XDataSection =
1047 getContext().getCOFFSection(".xdata",
1048 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1049 COFF::IMAGE_SCN_MEM_READ |
1050 COFF::IMAGE_SCN_MEM_WRITE,
1051 SectionKind::getDataRel());
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001052}
1053
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001054const MCSection *TargetLoweringObjectFileCOFF::getEHFrameSection() const {
1055 return getContext().getCOFFSection(".eh_frame",
1056 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1057 COFF::IMAGE_SCN_MEM_READ |
1058 COFF::IMAGE_SCN_MEM_WRITE,
1059 SectionKind::getDataRel());
1060}
1061
Charles Davis88c81642011-05-27 21:38:47 +00001062const MCSection *TargetLoweringObjectFileCOFF::getWin64EHFuncTableSection(
1063 StringRef suffix) const {
1064 if (suffix == "")
1065 return PDataSection;
1066 return getContext().getCOFFSection((".pdata"+suffix).str(),
1067 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1068 COFF::IMAGE_SCN_MEM_READ |
1069 COFF::IMAGE_SCN_MEM_WRITE,
1070 SectionKind::getDataRel());
1071}
1072
1073const MCSection *TargetLoweringObjectFileCOFF::getWin64EHTableSection(
1074 StringRef suffix) const {
1075 if (suffix == "")
1076 return XDataSection;
1077 return getContext().getCOFFSection((".xdata"+suffix).str(),
1078 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1079 COFF::IMAGE_SCN_MEM_READ |
1080 COFF::IMAGE_SCN_MEM_WRITE,
1081 SectionKind::getDataRel());
1082}
1083
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001084
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001085static unsigned
1086getCOFFSectionFlags(SectionKind K) {
1087 unsigned Flags = 0;
1088
Anton Korobeynikov36335be2010-07-06 15:24:56 +00001089 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +00001090 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001091 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001092 else if (K.isText())
1093 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001094 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +00001095 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar94610582010-07-01 20:07:24 +00001096 COFF::IMAGE_SCN_CNT_CODE;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001097 else if (K.isBSS ())
1098 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001099 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1100 COFF::IMAGE_SCN_MEM_READ |
1101 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001102 else if (K.isReadOnly())
1103 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001104 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1105 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001106 else if (K.isWriteable())
1107 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001108 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1109 COFF::IMAGE_SCN_MEM_READ |
1110 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001111
1112 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001113}
1114
1115const MCSection *TargetLoweringObjectFileCOFF::
1116getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1117 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001118 return getContext().getCOFFSection(GV->getSection(),
1119 getCOFFSectionFlags(Kind),
1120 Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001121}
1122
1123static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1124 if (Kind.isText())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001125 return ".text$";
Chris Lattner6e5ce282010-05-07 21:49:09 +00001126 if (Kind.isBSS ())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001127 return ".bss$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001128 if (Kind.isWriteable())
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001129 return ".data$";
1130 return ".rdata$";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001131}
1132
1133
1134const MCSection *TargetLoweringObjectFileCOFF::
1135SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1136 Mangler *Mang, const TargetMachine &TM) const {
1137 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1138
1139 // If this global is linkonce/weak and the target handles this by emitting it
1140 // into a 'uniqued' section name, create and return the section now.
1141 if (GV->isWeakForLinker()) {
1142 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1143 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattner4c6741f2010-03-15 20:37:38 +00001144 MCSymbol *Sym = Mang->getSymbol(GV);
NAKAMURA Takumi3b3b0eb2010-10-19 03:24:42 +00001145 Name.append(Sym->getName().begin() + 1, Sym->getName().end());
Chris Lattner6e5ce282010-05-07 21:49:09 +00001146
1147 unsigned Characteristics = getCOFFSectionFlags(Kind);
1148
Daniel Dunbar94610582010-07-01 20:07:24 +00001149 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Chris Lattner6e5ce282010-05-07 21:49:09 +00001150
1151 return getContext().getCOFFSection(Name.str(), Characteristics,
Anton Korobeynikov657985e2010-10-08 21:50:04 +00001152 COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001153 }
1154
1155 if (Kind.isText())
1156 return getTextSection();
1157
1158 return getDataSection();
1159}
1160