blob: 6aca6da8afef6dad72cdccd49157b4d2e6429c40 [file] [log] [blame]
Chris Lattnerf0144122009-07-28 03:13:23 +00001//===-- llvm/Target/TargetLoweringObjectFile.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/Target/TargetLoweringObjectFile.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/GlobalVariable.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000019#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCSection.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000021#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetOptions.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000024#include "llvm/Support/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000025#include "llvm/ADT/StringExtras.h"
26using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29// Generic Code
30//===----------------------------------------------------------------------===//
31
Chris Lattnera87dea42009-07-31 18:48:30 +000032TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000033 TextSection = 0;
34 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000035 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000036 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000037 StaticCtorSection = 0;
38 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000039 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000040 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000041
42 DwarfAbbrevSection = 0;
43 DwarfInfoSection = 0;
44 DwarfLineSection = 0;
45 DwarfFrameSection = 0;
46 DwarfPubNamesSection = 0;
47 DwarfPubTypesSection = 0;
48 DwarfDebugInlineSection = 0;
49 DwarfStrSection = 0;
50 DwarfLocSection = 0;
51 DwarfARangesSection = 0;
52 DwarfRangesSection = 0;
53 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000054}
55
56TargetLoweringObjectFile::~TargetLoweringObjectFile() {
57}
58
59static bool isSuitableForBSS(const GlobalVariable *GV) {
60 Constant *C = GV->getInitializer();
61
62 // Must have zero initializer.
63 if (!C->isNullValue())
64 return false;
65
66 // Leave constant zeros in readonly constant sections, so they can be shared.
67 if (GV->isConstant())
68 return false;
69
70 // If the global has an explicit section specified, don't put it in BSS.
71 if (!GV->getSection().empty())
72 return false;
73
74 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
75 if (NoZerosInBSS)
76 return false;
77
78 // Otherwise, put it in BSS!
79 return true;
80}
81
Chris Lattner1850e5a2009-08-04 16:13:09 +000082/// IsNullTerminatedString - Return true if the specified constant (which is
83/// known to have a type that is an array of 1/2/4 byte elements) ends with a
84/// nul value and contains no other nuls in it.
85static bool IsNullTerminatedString(const Constant *C) {
86 const ArrayType *ATy = cast<ArrayType>(C->getType());
87
Chris Lattnerf0144122009-07-28 03:13:23 +000088 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000089 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
90 if (ATy->getNumElements() == 0) return false;
91
92 ConstantInt *Null =
93 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
94 if (Null == 0 || Null->getZExtValue() != 0)
95 return false; // Not null terminated.
96
97 // Verify that the null doesn't occur anywhere else in the string.
98 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
99 // Reject constantexpr elements etc.
100 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
101 CVA->getOperand(i) == Null)
102 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000103 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000104 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000105
106 // Another possibility: [1 x i8] zeroinitializer
107 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000108 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000109
110 return false;
111}
112
Chris Lattner58bed8f2009-08-05 04:25:40 +0000113/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000114/// a global variable. Given an global variable and information from TM, it
115/// classifies the global in a variety of ways that make various target
116/// implementations simpler. The target implementation is free to ignore this
117/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000118SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
119 const TargetMachine &TM){
120 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
121 "Can only be used for global definitions");
122
Chris Lattnerf0144122009-07-28 03:13:23 +0000123 Reloc::Model ReloModel = TM.getRelocationModel();
124
125 // Early exit - functions should be always in text sections.
126 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
127 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000128 return SectionKind::getText();
Chris Lattnerf0144122009-07-28 03:13:23 +0000129
130
131 // Handle thread-local data first.
132 if (GVar->isThreadLocal()) {
133 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000134 return SectionKind::getThreadBSS();
135 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000136 }
137
138 // Variable can be easily put to BSS section.
139 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000140 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000141
142 Constant *C = GVar->getInitializer();
143
144 // If the global is marked constant, we can put it into a mergable section,
145 // a mergable string section, or general .data if it contains relocations.
146 if (GVar->isConstant()) {
147 // If the initializer for the global contains something that requires a
148 // relocation, then we may have to drop this into a wriable data section
149 // even though it is marked const.
150 switch (C->getRelocationInfo()) {
151 default: llvm_unreachable("unknown relocation info kind");
152 case Constant::NoRelocation:
153 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000154 // section of the right width.
155 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
156 if (const IntegerType *ITy =
157 dyn_cast<IntegerType>(ATy->getElementType())) {
158 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
159 ITy->getBitWidth() == 32) &&
160 IsNullTerminatedString(C)) {
161 if (ITy->getBitWidth() == 8)
162 return SectionKind::getMergeable1ByteCString();
163 if (ITy->getBitWidth() == 16)
164 return SectionKind::getMergeable2ByteCString();
165
166 assert(ITy->getBitWidth() == 32 && "Unknown width");
167 return SectionKind::getMergeable4ByteCString();
168 }
169 }
170 }
Chris Lattner3b24c012009-08-04 05:35:56 +0000171
Chris Lattnerf0144122009-07-28 03:13:23 +0000172 // Otherwise, just drop it into a mergable constant section. If we have
173 // a section for this size, use it, otherwise use the arbitrary sized
174 // mergable section.
175 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000176 case 4: return SectionKind::getMergeableConst4();
177 case 8: return SectionKind::getMergeableConst8();
178 case 16: return SectionKind::getMergeableConst16();
179 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000180 }
181
182 case Constant::LocalRelocation:
183 // In static relocation model, the linker will resolve all addresses, so
184 // the relocation entries will actually be constants by the time the app
185 // starts up. However, we can't put this into a mergable section, because
186 // the linker doesn't take relocations into consideration when it tries to
187 // merge entries in the section.
188 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000189 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000190
191 // Otherwise, the dynamic linker needs to fix it up, put it in the
192 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000193 return SectionKind::getReadOnlyWithRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000194
195 case Constant::GlobalRelocations:
196 // In static relocation model, the linker will resolve all addresses, so
197 // the relocation entries will actually be constants by the time the app
198 // starts up. However, we can't put this into a mergable section, because
199 // the linker doesn't take relocations into consideration when it tries to
200 // merge entries in the section.
201 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000202 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000203
204 // Otherwise, the dynamic linker needs to fix it up, put it in the
205 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000206 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000207 }
208 }
209
210 // Okay, this isn't a constant. If the initializer for the global is going
211 // to require a runtime relocation by the dynamic linker, put it into a more
212 // specific section to improve startup time of the app. This coalesces these
213 // globals together onto fewer pages, improving the locality of the dynamic
214 // linker.
215 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000216 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000217
218 switch (C->getRelocationInfo()) {
219 default: llvm_unreachable("unknown relocation info kind");
220 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000221 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000222 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000223 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000224 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000225 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000226 }
227}
228
229/// SectionForGlobal - This method computes the appropriate section to emit
230/// the specified global variable or function definition. This should not
231/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000232const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000233SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000234 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000235 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000236 if (GV->hasSection())
237 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
238
Chris Lattnerf0144122009-07-28 03:13:23 +0000239
240 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000241 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000242}
243
Chris Lattner58bed8f2009-08-05 04:25:40 +0000244
Chris Lattnerf0144122009-07-28 03:13:23 +0000245// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000246const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000247TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000248 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000249 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000250 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000251 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000252
Chris Lattnerf9650c02009-08-01 21:46:23 +0000253 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000254 return getTextSection();
255
Chris Lattner82458382009-08-01 21:56:13 +0000256 if (Kind.isBSS() && BSSSection != 0)
257 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000258
Chris Lattnerf9650c02009-08-01 21:46:23 +0000259 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000260 return ReadOnlySection;
261
262 return getDataSection();
263}
264
Chris Lattner83d77fa2009-08-01 23:46:12 +0000265/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000266/// specified size and relocation information, return a section that it
267/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000268const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000269TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000270 if (Kind.isReadOnly() && ReadOnlySection != 0)
271 return ReadOnlySection;
272
273 return DataSection;
274}
275
276
Chris Lattnera87dea42009-07-31 18:48:30 +0000277const MCSection *TargetLoweringObjectFile::
Chris Lattner968ff112009-08-01 21:11:14 +0000278getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnera87dea42009-07-31 18:48:30 +0000279 if (MCSection *S = Ctx->GetSection(Name))
280 return S;
Chris Lattner968ff112009-08-01 21:11:14 +0000281 return MCSection::Create(Name, isDirective, Kind, *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +0000282}
283
284
285
286//===----------------------------------------------------------------------===//
287// ELF
288//===----------------------------------------------------------------------===//
289
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000290void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
291 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000292 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000293 if (!HasCrazyBSS)
Chris Lattner27981192009-08-01 23:57:16 +0000294 BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000295 else
296 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
297 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000298 // FIXME2: this should just be handle by the section printer. We should get
299 // away from syntactic view of the sections and MCSection should just be a
300 // semantic view.
Chris Lattner27981192009-08-01 23:57:16 +0000301 BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000302
303
Chris Lattner27981192009-08-01 23:57:16 +0000304 TextSection = getOrCreateSection("\t.text", true, SectionKind::getText());
305 DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000306 ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000307 getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000308 TLSDataSection =
Chris Lattner27981192009-08-01 23:57:16 +0000309 getOrCreateSection("\t.tdata", false, SectionKind::getThreadData());
Chris Lattner3b24c012009-08-04 05:35:56 +0000310
Chris Lattner968ff112009-08-01 21:11:14 +0000311 TLSBSSSection = getOrCreateSection("\t.tbss", false,
Chris Lattner27981192009-08-01 23:57:16 +0000312 SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000313
314 DataRelSection = getOrCreateSection("\t.data.rel", false,
Chris Lattner27981192009-08-01 23:57:16 +0000315 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000316 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000317 SectionKind::getDataRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000318 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
Chris Lattner27981192009-08-01 23:57:16 +0000319 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000320 DataRelROLocalSection =
321 getOrCreateSection("\t.data.rel.ro.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000322 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000323
324 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
Chris Lattner27981192009-08-01 23:57:16 +0000325 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000326 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
Chris Lattner27981192009-08-01 23:57:16 +0000327 SectionKind::getMergeableConst8());
Chris Lattnerf0144122009-07-28 03:13:23 +0000328 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
Chris Lattner27981192009-08-01 23:57:16 +0000329 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000330
331 StaticCtorSection =
332 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
333 StaticDtorSection =
334 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000335
Chris Lattner18a4c162009-08-02 07:24:22 +0000336 // Exception Handling Sections.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000337
338 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
339 // it contains relocatable pointers. In PIC mode, this is probably a big
340 // runtime hit for C++ apps. Either the contents of the LSDA need to be
341 // adjusted or this should be a data section.
342 LSDASection =
343 getOrCreateSection(".gcc_except_table", false, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000344 EHFrameSection =
345 getOrCreateSection(".eh_frame", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000346
347 // Debug Info Sections.
348 DwarfAbbrevSection =
349 getOrCreateSection(".debug_abbrev", false, SectionKind::getMetadata());
350 DwarfInfoSection =
351 getOrCreateSection(".debug_info", false, SectionKind::getMetadata());
352 DwarfLineSection =
353 getOrCreateSection(".debug_line", false, SectionKind::getMetadata());
354 DwarfFrameSection =
355 getOrCreateSection(".debug_frame", false, SectionKind::getMetadata());
356 DwarfPubNamesSection =
357 getOrCreateSection(".debug_pubnames", false, SectionKind::getMetadata());
358 DwarfPubTypesSection =
359 getOrCreateSection(".debug_pubtypes", false, SectionKind::getMetadata());
360 DwarfStrSection =
361 getOrCreateSection(".debug_str", false, SectionKind::getMetadata());
362 DwarfLocSection =
363 getOrCreateSection(".debug_loc", false, SectionKind::getMetadata());
364 DwarfARangesSection =
365 getOrCreateSection(".debug_aranges", false, SectionKind::getMetadata());
366 DwarfRangesSection =
367 getOrCreateSection(".debug_ranges", false, SectionKind::getMetadata());
368 DwarfMacroInfoSection =
369 getOrCreateSection(".debug_macinfo", false, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000370}
371
372
Chris Lattner24f654c2009-08-06 16:39:58 +0000373static SectionKind
374getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000375 if (Name[0] != '.') return K;
376
377 // Some lame default implementation based on some magic section names.
378 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
379 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
380 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
381 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000382 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000383
384 if (strcmp(Name, ".tdata") == 0 ||
385 strncmp(Name, ".tdata.", 7) == 0 ||
386 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
387 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000388 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000389
390 if (strcmp(Name, ".tbss") == 0 ||
391 strncmp(Name, ".tbss.", 6) == 0 ||
392 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
393 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000394 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000395
396 return K;
397}
398
Chris Lattner24f654c2009-08-06 16:39:58 +0000399const MCSection *TargetLoweringObjectFileELF::
400getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
401 Mangler *Mang, const TargetMachine &TM) const {
402 // Infer section flags from the section name if we can.
403 Kind = getELFKindForNamedSection(GV->getSection().c_str(), Kind);
404
405 return getOrCreateSection(GV->getSection().c_str(), false, Kind);
406}
407
408
409
Chris Lattnerf0144122009-07-28 03:13:23 +0000410void TargetLoweringObjectFileELF::
411getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
412 Str.push_back(',');
413 Str.push_back('"');
414
415 if (!Kind.isMetadata())
416 Str.push_back('a');
417 if (Kind.isText())
418 Str.push_back('x');
419 if (Kind.isWriteable())
420 Str.push_back('w');
Chris Lattner3b24c012009-08-04 05:35:56 +0000421 if (Kind.isMergeable1ByteCString() ||
422 Kind.isMergeable2ByteCString() ||
423 Kind.isMergeable4ByteCString() ||
Chris Lattner82987bf2009-07-31 16:17:13 +0000424 Kind.isMergeableConst4() ||
425 Kind.isMergeableConst8() ||
426 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000427 Str.push_back('M');
Chris Lattner3b24c012009-08-04 05:35:56 +0000428 if (Kind.isMergeable1ByteCString() ||
429 Kind.isMergeable2ByteCString() ||
430 Kind.isMergeable4ByteCString())
Chris Lattnerf0144122009-07-28 03:13:23 +0000431 Str.push_back('S');
432 if (Kind.isThreadLocal())
433 Str.push_back('T');
434
435 Str.push_back('"');
436 Str.push_back(',');
437
438 // If comment string is '@', e.g. as on ARM - use '%' instead
439 if (AtIsCommentChar)
440 Str.push_back('%');
441 else
442 Str.push_back('@');
443
444 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000445 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000446 KindStr = "nobits";
447 else
448 KindStr = "progbits";
449
450 Str.append(KindStr, KindStr+strlen(KindStr));
451
Chris Lattner3b24c012009-08-04 05:35:56 +0000452 if (Kind.isMergeable1ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000453 Str.push_back(',');
454 Str.push_back('1');
Chris Lattner3b24c012009-08-04 05:35:56 +0000455 } else if (Kind.isMergeable2ByteCString()) {
456 Str.push_back(',');
457 Str.push_back('2');
458 } else if (Kind.isMergeable4ByteCString()) {
459 Str.push_back(',');
460 Str.push_back('4');
Chris Lattnerf0144122009-07-28 03:13:23 +0000461 } else if (Kind.isMergeableConst4()) {
462 Str.push_back(',');
463 Str.push_back('4');
464 } else if (Kind.isMergeableConst8()) {
465 Str.push_back(',');
466 Str.push_back('8');
467 } else if (Kind.isMergeableConst16()) {
468 Str.push_back(',');
469 Str.push_back('1');
470 Str.push_back('6');
471 }
472}
473
474
475static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
476 if (Kind.isText()) return ".gnu.linkonce.t.";
477 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
478
479 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
480 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
481
482 if (Kind.isBSS()) return ".gnu.linkonce.b.";
483 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
484 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
485 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
486 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
487
488 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
489 return ".gnu.linkonce.d.rel.ro.";
490}
491
Chris Lattnera87dea42009-07-31 18:48:30 +0000492const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000493SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000494 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000495
496 // If this global is linkonce/weak and the target handles this by emitting it
497 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000498 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000499 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000500 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000501 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000502 }
503
Chris Lattnerf9650c02009-08-01 21:46:23 +0000504 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000505
Chris Lattner3b24c012009-08-04 05:35:56 +0000506 if (Kind.isMergeable1ByteCString() ||
507 Kind.isMergeable2ByteCString() ||
508 Kind.isMergeable4ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000509
Chris Lattner067fe1a2009-07-29 04:54:38 +0000510 // We also need alignment here.
511 // FIXME: this is getting the alignment of the character, not the
512 // alignment of the global!
513 unsigned Align =
514 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000515
Chris Lattner7e88a502009-08-04 16:19:50 +0000516 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000517 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000518 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000519 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000520 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000521 else
522 assert(Kind.isMergeable1ByteCString() && "unknown string width");
523
524
Chris Lattner7e88a502009-08-04 16:19:50 +0000525 std::string Name = SizeSpec + utostr(Align);
Chris Lattner3b24c012009-08-04 05:35:56 +0000526 return getOrCreateSection(Name.c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000527 }
528
Chris Lattnerf9650c02009-08-01 21:46:23 +0000529 if (Kind.isMergeableConst()) {
530 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000531 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000532 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000533 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000534 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000535 return MergeableConst16Section;
536 return ReadOnlySection; // .const
537 }
538
Chris Lattnerf9650c02009-08-01 21:46:23 +0000539 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000540
Chris Lattnerf9650c02009-08-01 21:46:23 +0000541 if (Kind.isThreadData()) return TLSDataSection;
542 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000543
Chris Lattner82458382009-08-01 21:56:13 +0000544 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000545
Chris Lattnerf9650c02009-08-01 21:46:23 +0000546 if (Kind.isDataNoRel()) return DataSection;
547 if (Kind.isDataRelLocal()) return DataRelLocalSection;
548 if (Kind.isDataRel()) return DataRelSection;
549 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000550
Chris Lattnerf9650c02009-08-01 21:46:23 +0000551 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000552 return DataRelROSection;
553}
554
Chris Lattner83d77fa2009-08-01 23:46:12 +0000555/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000556/// specified size and relocation information, return a section that it
557/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000558const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000559getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000560 if (Kind.isMergeableConst4())
561 return MergeableConst4Section;
562 if (Kind.isMergeableConst8())
563 return MergeableConst8Section;
564 if (Kind.isMergeableConst16())
565 return MergeableConst16Section;
566 if (Kind.isReadOnly())
567 return ReadOnlySection;
568
569 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
570 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
571 return DataRelROSection;
572}
573
574//===----------------------------------------------------------------------===//
575// MachO
576//===----------------------------------------------------------------------===//
577
Chris Lattner11e96572009-08-03 21:53:27 +0000578const MCSection *TargetLoweringObjectFileMachO::
579getMachOSection(const char *Name, bool isDirective, SectionKind K) {
580 // FOR NOW, Just forward.
581 return getOrCreateSection(Name, isDirective, K);
582}
583
584
585
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000586void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
587 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000588 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000589 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000590 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000591 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000592 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000593
Chris Lattner82458382009-08-01 21:56:13 +0000594 CStringSection = getOrCreateSection("\t.cstring", true,
Chris Lattnerec409752009-08-04 16:27:13 +0000595 SectionKind::getMergeable1ByteCString());
596 UStringSection = getOrCreateSection("__TEXT,__ustring", false,
597 SectionKind::getMergeable2ByteCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000598 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
Chris Lattnerec409752009-08-04 16:27:13 +0000599 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000600 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
Chris Lattnerec409752009-08-04 16:27:13 +0000601 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000602
603 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
604 // to using it in -static mode.
605 if (TM.getRelocationModel() != Reloc::Static &&
606 TM.getTargetData()->getPointerSize() == 32)
607 SixteenByteConstantSection =
Chris Lattner968ff112009-08-01 21:11:14 +0000608 getOrCreateSection("\t.literal16\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000609 SectionKind::getMergeableConst16());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000610 else
611 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000612
Chris Lattner968ff112009-08-01 21:11:14 +0000613 ReadOnlySection = getOrCreateSection("\t.const", true,
Chris Lattner27981192009-08-01 23:57:16 +0000614 SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000615
616 TextCoalSection =
617 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Chris Lattner27981192009-08-01 23:57:16 +0000618 false, SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000619 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000620 false,
Chris Lattner27981192009-08-01 23:57:16 +0000621 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000622 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000623 false,
Chris Lattner27981192009-08-01 23:57:16 +0000624 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000625 ConstDataSection = getOrCreateSection("\t.const_data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000626 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000627 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000628 false,
Chris Lattner27981192009-08-01 23:57:16 +0000629 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000630
631 if (TM.getRelocationModel() == Reloc::Static) {
632 StaticCtorSection =
633 getOrCreateSection(".constructor", true, SectionKind::getDataRel());
634 StaticDtorSection =
635 getOrCreateSection(".destructor", true, SectionKind::getDataRel());
636 } else {
637 StaticCtorSection =
638 getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel());
639 StaticDtorSection =
640 getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel());
641 }
642
Chris Lattner18a4c162009-08-02 07:24:22 +0000643 // Exception Handling.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000644 LSDASection = getOrCreateSection("__DATA,__gcc_except_tab", false,
645 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000646 EHFrameSection =
647 getOrCreateSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
648 "+live_support", false, SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000649
650 // Debug Information.
651 // FIXME: Don't use 'directive' syntax: need flags for debug/regular??
652 // FIXME: Need __DWARF segment.
653 DwarfAbbrevSection =
654 getOrCreateSection(".section __DWARF,__debug_abbrev,regular,debug", true,
655 SectionKind::getMetadata());
656 DwarfInfoSection =
657 getOrCreateSection(".section __DWARF,__debug_info,regular,debug", true,
658 SectionKind::getMetadata());
659 DwarfLineSection =
660 getOrCreateSection(".section __DWARF,__debug_line,regular,debug", true,
661 SectionKind::getMetadata());
662 DwarfFrameSection =
663 getOrCreateSection(".section __DWARF,__debug_frame,regular,debug", true,
664 SectionKind::getMetadata());
665 DwarfPubNamesSection =
666 getOrCreateSection(".section __DWARF,__debug_pubnames,regular,debug", true,
667 SectionKind::getMetadata());
668 DwarfPubTypesSection =
669 getOrCreateSection(".section __DWARF,__debug_pubtypes,regular,debug", true,
670 SectionKind::getMetadata());
671 DwarfStrSection =
672 getOrCreateSection(".section __DWARF,__debug_str,regular,debug", true,
673 SectionKind::getMetadata());
674 DwarfLocSection =
675 getOrCreateSection(".section __DWARF,__debug_loc,regular,debug", true,
676 SectionKind::getMetadata());
677 DwarfARangesSection =
678 getOrCreateSection(".section __DWARF,__debug_aranges,regular,debug", true,
679 SectionKind::getMetadata());
680 DwarfRangesSection =
681 getOrCreateSection(".section __DWARF,__debug_ranges,regular,debug", true,
682 SectionKind::getMetadata());
683 DwarfMacroInfoSection =
684 getOrCreateSection(".section __DWARF,__debug_macinfo,regular,debug", true,
685 SectionKind::getMetadata());
686 DwarfDebugInlineSection =
687 getOrCreateSection(".section __DWARF,__debug_inlined,regular,debug", true,
688 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000689}
690
Chris Lattnera87dea42009-07-31 18:48:30 +0000691const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner24f654c2009-08-06 16:39:58 +0000692getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
693 Mangler *Mang, const TargetMachine &TM) const {
694 return getOrCreateSection(GV->getSection().c_str(), false, Kind);
695}
696
697const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000698SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000699 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000700 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000701
Chris Lattnerf9650c02009-08-01 21:46:23 +0000702 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000703 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000704
705 // If this is weak/linkonce, put this in a coalescable section, either in text
706 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000707 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000708 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000709 return ConstTextCoalSection;
710 return DataCoalSection;
711 }
712
713 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000714 if (Kind.isMergeable1ByteCString() ||
715 Kind.isMergeable2ByteCString()) {
716 if (TM.getTargetData()->getPreferredAlignment(
717 cast<GlobalVariable>(GV)) < 32) {
718 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000719 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000720 assert(Kind.isMergeable2ByteCString());
721 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000722 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000723 }
724
Chris Lattnerf9650c02009-08-01 21:46:23 +0000725 if (Kind.isMergeableConst()) {
726 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000727 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000728 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000729 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000730 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000731 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000732 }
Chris Lattnerec409752009-08-04 16:27:13 +0000733
734 // Otherwise, if it is readonly, but not something we can specially optimize,
735 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000736 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000737 return ReadOnlySection;
738
739 // If this is marked const, put it into a const section. But if the dynamic
740 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000741 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000742 return ConstDataSection;
743
744 // Otherwise, just drop the variable in the normal data section.
745 return DataSection;
746}
747
Chris Lattnera87dea42009-07-31 18:48:30 +0000748const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000749TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000750 // If this constant requires a relocation, we have to put it in the data
751 // segment, not in the text segment.
752 if (Kind.isDataRel())
753 return ConstDataSection;
754
755 if (Kind.isMergeableConst4())
756 return FourByteConstantSection;
757 if (Kind.isMergeableConst8())
758 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000759 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000760 return SixteenByteConstantSection;
761 return ReadOnlySection; // .const
762}
763
Chris Lattner26630c12009-07-31 20:52:39 +0000764/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
765/// not to emit the UsedDirective for some symbols in llvm.used.
766// FIXME: REMOVE this (rdar://7071300)
767bool TargetLoweringObjectFileMachO::
768shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
769 /// On Darwin, internally linked data beginning with "L" or "l" does not have
770 /// the directive emitted (this occurs in ObjC metadata).
771 if (!GV) return false;
772
773 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
774 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
775 // FIXME: ObjC metadata is currently emitted as internal symbols that have
776 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
777 // this horrible hack can go away.
778 const std::string &Name = Mang->getMangledName(GV);
779 if (Name[0] == 'L' || Name[0] == 'l')
780 return false;
781 }
782
783 return true;
784}
785
786
Chris Lattnerf0144122009-07-28 03:13:23 +0000787//===----------------------------------------------------------------------===//
788// COFF
789//===----------------------------------------------------------------------===//
790
Chris Lattner11e96572009-08-03 21:53:27 +0000791const MCSection *TargetLoweringObjectFileCOFF::
792getCOFFSection(const char *Name, bool isDirective, SectionKind K) {
793 return getOrCreateSection(Name, isDirective, K);
794}
795
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000796void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
797 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000798 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000799 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000800 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000801 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000802 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000803 StaticCtorSection =
804 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
805 StaticDtorSection =
806 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000807
808
809 // Debug info.
810 // FIXME: Don't use 'directive' mode here.
811 DwarfAbbrevSection =
812 getOrCreateSection("\t.section\t.debug_abbrev,\"dr\"",
813 true, SectionKind::getMetadata());
814 DwarfInfoSection =
815 getOrCreateSection("\t.section\t.debug_info,\"dr\"",
816 true, SectionKind::getMetadata());
817 DwarfLineSection =
818 getOrCreateSection("\t.section\t.debug_line,\"dr\"",
819 true, SectionKind::getMetadata());
820 DwarfFrameSection =
821 getOrCreateSection("\t.section\t.debug_frame,\"dr\"",
822 true, SectionKind::getMetadata());
823 DwarfPubNamesSection =
824 getOrCreateSection("\t.section\t.debug_pubnames,\"dr\"",
825 true, SectionKind::getMetadata());
826 DwarfPubTypesSection =
827 getOrCreateSection("\t.section\t.debug_pubtypes,\"dr\"",
828 true, SectionKind::getMetadata());
829 DwarfStrSection =
830 getOrCreateSection("\t.section\t.debug_str,\"dr\"",
831 true, SectionKind::getMetadata());
832 DwarfLocSection =
833 getOrCreateSection("\t.section\t.debug_loc,\"dr\"",
834 true, SectionKind::getMetadata());
835 DwarfARangesSection =
836 getOrCreateSection("\t.section\t.debug_aranges,\"dr\"",
837 true, SectionKind::getMetadata());
838 DwarfRangesSection =
839 getOrCreateSection("\t.section\t.debug_ranges,\"dr\"",
840 true, SectionKind::getMetadata());
841 DwarfMacroInfoSection =
842 getOrCreateSection("\t.section\t.debug_macinfo,\"dr\"",
843 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000844}
845
Chris Lattner24f654c2009-08-06 16:39:58 +0000846const MCSection *TargetLoweringObjectFileCOFF::
847getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
848 Mangler *Mang, const TargetMachine &TM) const {
849 return getOrCreateSection(GV->getSection().c_str(), false, Kind);
850}
851
852
Chris Lattnerf0144122009-07-28 03:13:23 +0000853void TargetLoweringObjectFileCOFF::
854getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
855 // FIXME: Inefficient.
856 std::string Res = ",\"";
857 if (Kind.isText())
858 Res += 'x';
859 if (Kind.isWriteable())
860 Res += 'w';
861 Res += "\"";
862
863 Str.append(Res.begin(), Res.end());
864}
865
866static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
867 if (Kind.isText())
868 return ".text$linkonce";
869 if (Kind.isWriteable())
870 return ".data$linkonce";
871 return ".rdata$linkonce";
872}
873
874
Chris Lattnera87dea42009-07-31 18:48:30 +0000875const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000876SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000877 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000878 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000879
880 // If this global is linkonce/weak and the target handles this by emitting it
881 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000882 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000883 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000884 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000885 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000886 }
887
Chris Lattnerf9650c02009-08-01 21:46:23 +0000888 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000889 return getTextSection();
890
Chris Lattnerf0144122009-07-28 03:13:23 +0000891 return getDataSection();
892}
893