blob: 36fda0b9583709e4b1a61e5be84820c029650910 [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;
35 BSSSection_ = 0;
36 ReadOnlySection = 0;
37 TLSDataSection = 0;
38 TLSBSSSection = 0;
39 CStringSection_ = 0;
40}
41
42TargetLoweringObjectFile::~TargetLoweringObjectFile() {
43}
44
45static bool isSuitableForBSS(const GlobalVariable *GV) {
46 Constant *C = GV->getInitializer();
47
48 // Must have zero initializer.
49 if (!C->isNullValue())
50 return false;
51
52 // Leave constant zeros in readonly constant sections, so they can be shared.
53 if (GV->isConstant())
54 return false;
55
56 // If the global has an explicit section specified, don't put it in BSS.
57 if (!GV->getSection().empty())
58 return false;
59
60 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
61 if (NoZerosInBSS)
62 return false;
63
64 // Otherwise, put it in BSS!
65 return true;
66}
67
68static bool isConstantString(const Constant *C) {
69 // First check: is we have constant array of i8 terminated with zero
70 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
71 // Check, if initializer is a null-terminated string
72 if (CVA && CVA->isCString())
73 return true;
74
75 // Another possibility: [1 x i8] zeroinitializer
76 if (isa<ConstantAggregateZero>(C))
77 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
78 return (Ty->getElementType() == Type::Int8Ty &&
79 Ty->getNumElements() == 1);
80
81 return false;
82}
83
84static SectionKind::Kind SectionKindForGlobal(const GlobalValue *GV,
85 const TargetMachine &TM) {
86 Reloc::Model ReloModel = TM.getRelocationModel();
87
88 // Early exit - functions should be always in text sections.
89 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
90 if (GVar == 0)
91 return SectionKind::Text;
92
93
94 // Handle thread-local data first.
95 if (GVar->isThreadLocal()) {
96 if (isSuitableForBSS(GVar))
97 return SectionKind::ThreadBSS;
98 return SectionKind::ThreadData;
99 }
100
101 // Variable can be easily put to BSS section.
102 if (isSuitableForBSS(GVar))
103 return SectionKind::BSS;
104
105 Constant *C = GVar->getInitializer();
106
107 // If the global is marked constant, we can put it into a mergable section,
108 // a mergable string section, or general .data if it contains relocations.
109 if (GVar->isConstant()) {
110 // If the initializer for the global contains something that requires a
111 // relocation, then we may have to drop this into a wriable data section
112 // even though it is marked const.
113 switch (C->getRelocationInfo()) {
114 default: llvm_unreachable("unknown relocation info kind");
115 case Constant::NoRelocation:
116 // If initializer is a null-terminated string, put it in a "cstring"
117 // section if the target has it.
118 if (isConstantString(C))
119 return SectionKind::MergeableCString;
120
121 // Otherwise, just drop it into a mergable constant section. If we have
122 // a section for this size, use it, otherwise use the arbitrary sized
123 // mergable section.
124 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
125 case 4: return SectionKind::MergeableConst4;
126 case 8: return SectionKind::MergeableConst8;
127 case 16: return SectionKind::MergeableConst16;
128 default: return SectionKind::MergeableConst;
129 }
130
131 case Constant::LocalRelocation:
132 // In static relocation model, the linker will resolve all addresses, so
133 // the relocation entries will actually be constants by the time the app
134 // starts up. However, we can't put this into a mergable section, because
135 // the linker doesn't take relocations into consideration when it tries to
136 // merge entries in the section.
137 if (ReloModel == Reloc::Static)
138 return SectionKind::ReadOnly;
139
140 // Otherwise, the dynamic linker needs to fix it up, put it in the
141 // writable data.rel.local section.
142 return SectionKind::ReadOnlyWithRelLocal;
143
144 case Constant::GlobalRelocations:
145 // In static relocation model, the linker will resolve all addresses, so
146 // the relocation entries will actually be constants by the time the app
147 // starts up. However, we can't put this into a mergable section, because
148 // the linker doesn't take relocations into consideration when it tries to
149 // merge entries in the section.
150 if (ReloModel == Reloc::Static)
151 return SectionKind::ReadOnly;
152
153 // Otherwise, the dynamic linker needs to fix it up, put it in the
154 // writable data.rel section.
155 return SectionKind::ReadOnlyWithRel;
156 }
157 }
158
159 // Okay, this isn't a constant. If the initializer for the global is going
160 // to require a runtime relocation by the dynamic linker, put it into a more
161 // specific section to improve startup time of the app. This coalesces these
162 // globals together onto fewer pages, improving the locality of the dynamic
163 // linker.
164 if (ReloModel == Reloc::Static)
165 return SectionKind::DataNoRel;
166
167 switch (C->getRelocationInfo()) {
168 default: llvm_unreachable("unknown relocation info kind");
169 case Constant::NoRelocation:
170 return SectionKind::DataNoRel;
171 case Constant::LocalRelocation:
172 return SectionKind::DataRelLocal;
173 case Constant::GlobalRelocations:
174 return SectionKind::DataRel;
175 }
176}
177
178/// SectionForGlobal - This method computes the appropriate section to emit
179/// the specified global variable or function definition. This should not
180/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000181const MCSection *TargetLoweringObjectFile::
Chris Lattnere53a6002009-07-29 05:09:30 +0000182SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
183 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000184 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
185 "Can only be used for global definitions");
186
187 SectionKind::Kind GVKind = SectionKindForGlobal(GV, TM);
188
189 SectionKind Kind = SectionKind::get(GVKind, GV->isWeakForLinker(),
190 GV->hasSection());
191
192
193 // Select section name.
194 if (GV->hasSection()) {
195 // If the target has special section hacks for specifically named globals,
196 // return them now.
Chris Lattnera87dea42009-07-31 18:48:30 +0000197 if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind))
Chris Lattnerf0144122009-07-28 03:13:23 +0000198 return TS;
199
200 // If the target has magic semantics for certain section names, make sure to
201 // pick up the flags. This allows the user to write things with attribute
202 // section and still get the appropriate section flags printed.
203 GVKind = getKindForNamedSection(GV->getSection().c_str(), GVKind);
204
205 return getOrCreateSection(GV->getSection().c_str(), false, GVKind);
206 }
207
208
209 // Use default section depending on the 'type' of global
Chris Lattnere53a6002009-07-29 05:09:30 +0000210 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000211}
212
213// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000214const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000215TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
216 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000217 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000218 const TargetMachine &TM) const{
219 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
220
221 if (Kind.isText())
222 return getTextSection();
223
224 if (Kind.isBSS() && BSSSection_ != 0)
225 return BSSSection_;
226
227 if (Kind.isReadOnly() && ReadOnlySection != 0)
228 return ReadOnlySection;
229
230 return getDataSection();
231}
232
233/// getSectionForMergableConstant - Given a mergable constant with the
234/// specified size and relocation information, return a section that it
235/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000236const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000237TargetLoweringObjectFile::
238getSectionForMergeableConstant(SectionKind Kind) const {
239 if (Kind.isReadOnly() && ReadOnlySection != 0)
240 return ReadOnlySection;
241
242 return DataSection;
243}
244
245
Chris Lattnera87dea42009-07-31 18:48:30 +0000246const MCSection *TargetLoweringObjectFile::
Chris Lattnerf0144122009-07-28 03:13:23 +0000247getOrCreateSection(const char *Name, bool isDirective,
248 SectionKind::Kind Kind) const {
Chris Lattnera87dea42009-07-31 18:48:30 +0000249 if (MCSection *S = Ctx->GetSection(Name))
250 return S;
251 SectionKind K = SectionKind::get(Kind, false /*weak*/, !isDirective);
252 return MCSectionWithKind::Create(Name, K, *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +0000253}
254
255
256
257//===----------------------------------------------------------------------===//
258// ELF
259//===----------------------------------------------------------------------===//
260
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000261void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
262 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000263 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000264 if (!HasCrazyBSS)
265 BSSSection_ = getOrCreateSection("\t.bss", true, SectionKind::BSS);
266 else
267 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
268 // FIXME: Does .section .bss work everywhere??
269 BSSSection_ = getOrCreateSection("\t.bss", false, SectionKind::BSS);
270
271
272 TextSection = getOrCreateSection("\t.text", true, SectionKind::Text);
273 DataSection = getOrCreateSection("\t.data", true, SectionKind::DataRel);
274 ReadOnlySection =
275 getOrCreateSection("\t.rodata", false, SectionKind::ReadOnly);
276 TLSDataSection =
277 getOrCreateSection("\t.tdata", false, SectionKind::ThreadData);
278 CStringSection_ = getOrCreateSection("\t.rodata.str", true,
279 SectionKind::MergeableCString);
280
281 TLSBSSSection = getOrCreateSection("\t.tbss", false, SectionKind::ThreadBSS);
282
283 DataRelSection = getOrCreateSection("\t.data.rel", false,
284 SectionKind::DataRel);
285 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
286 SectionKind::DataRelLocal);
287 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
288 SectionKind::ReadOnlyWithRel);
289 DataRelROLocalSection =
290 getOrCreateSection("\t.data.rel.ro.local", false,
291 SectionKind::ReadOnlyWithRelLocal);
292
293 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
294 SectionKind::MergeableConst4);
295 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
296 SectionKind::MergeableConst8);
297 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
298 SectionKind::MergeableConst16);
299}
300
301
302SectionKind::Kind TargetLoweringObjectFileELF::
303getKindForNamedSection(const char *Name, SectionKind::Kind K) const {
304 if (Name[0] != '.') return K;
305
306 // Some lame default implementation based on some magic section names.
307 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
308 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
309 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
310 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
311 return SectionKind::BSS;
312
313 if (strcmp(Name, ".tdata") == 0 ||
314 strncmp(Name, ".tdata.", 7) == 0 ||
315 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
316 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
317 return SectionKind::ThreadData;
318
319 if (strcmp(Name, ".tbss") == 0 ||
320 strncmp(Name, ".tbss.", 6) == 0 ||
321 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
322 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
323 return SectionKind::ThreadBSS;
324
325 return K;
326}
327
328void TargetLoweringObjectFileELF::
329getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
330 Str.push_back(',');
331 Str.push_back('"');
332
333 if (!Kind.isMetadata())
334 Str.push_back('a');
335 if (Kind.isText())
336 Str.push_back('x');
337 if (Kind.isWriteable())
338 Str.push_back('w');
Chris Lattner82987bf2009-07-31 16:17:13 +0000339 if (Kind.isMergeableCString() ||
340 Kind.isMergeableConst4() ||
341 Kind.isMergeableConst8() ||
342 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000343 Str.push_back('M');
344 if (Kind.isMergeableCString())
345 Str.push_back('S');
346 if (Kind.isThreadLocal())
347 Str.push_back('T');
348
349 Str.push_back('"');
350 Str.push_back(',');
351
352 // If comment string is '@', e.g. as on ARM - use '%' instead
353 if (AtIsCommentChar)
354 Str.push_back('%');
355 else
356 Str.push_back('@');
357
358 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000359 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000360 KindStr = "nobits";
361 else
362 KindStr = "progbits";
363
364 Str.append(KindStr, KindStr+strlen(KindStr));
365
366 if (Kind.isMergeableCString()) {
367 // TODO: Eventually handle multiple byte character strings. For now, all
368 // mergable C strings are single byte.
369 Str.push_back(',');
370 Str.push_back('1');
371 } else if (Kind.isMergeableConst4()) {
372 Str.push_back(',');
373 Str.push_back('4');
374 } else if (Kind.isMergeableConst8()) {
375 Str.push_back(',');
376 Str.push_back('8');
377 } else if (Kind.isMergeableConst16()) {
378 Str.push_back(',');
379 Str.push_back('1');
380 Str.push_back('6');
381 }
382}
383
384
385static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
386 if (Kind.isText()) return ".gnu.linkonce.t.";
387 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
388
389 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
390 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
391
392 if (Kind.isBSS()) return ".gnu.linkonce.b.";
393 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
394 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
395 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
396 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
397
398 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
399 return ".gnu.linkonce.d.rel.ro.";
400}
401
Chris Lattnera87dea42009-07-31 18:48:30 +0000402const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf0144122009-07-28 03:13:23 +0000403SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000404 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000405
406 // If this global is linkonce/weak and the target handles this by emitting it
407 // into a 'uniqued' section name, create and return the section now.
408 if (Kind.isWeak()) {
409 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000410 std::string Name = Mang->makeNameProper(GV->getNameStr());
411 return getOrCreateSection((Prefix+Name).c_str(), false, Kind.getKind());
Chris Lattnerf0144122009-07-28 03:13:23 +0000412 }
413
414 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000415
Chris Lattnerf0144122009-07-28 03:13:23 +0000416 if (Kind.isMergeableCString()) {
Chris Lattner067fe1a2009-07-29 04:54:38 +0000417 assert(CStringSection_ && "Should have string section prefix");
Chris Lattnerf0144122009-07-28 03:13:23 +0000418
Chris Lattner067fe1a2009-07-29 04:54:38 +0000419 // We also need alignment here.
420 // FIXME: this is getting the alignment of the character, not the
421 // alignment of the global!
422 unsigned Align =
423 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000424
Chris Lattnera6792072009-07-29 04:55:08 +0000425 std::string Name = CStringSection_->getName() + "1." + utostr(Align);
Chris Lattner067fe1a2009-07-29 04:54:38 +0000426 return getOrCreateSection(Name.c_str(), false,
427 SectionKind::MergeableCString);
Chris Lattnerf0144122009-07-28 03:13:23 +0000428 }
429
430 if (Kind.isMergeableConst()) {
431 if (Kind.isMergeableConst4())
432 return MergeableConst4Section;
433 if (Kind.isMergeableConst8())
434 return MergeableConst8Section;
435 if (Kind.isMergeableConst16())
436 return MergeableConst16Section;
437 return ReadOnlySection; // .const
438 }
439
440 if (Kind.isReadOnly()) return ReadOnlySection;
441
442 if (Kind.isThreadData()) return TLSDataSection;
443 if (Kind.isThreadBSS()) return TLSBSSSection;
444
445 if (Kind.isBSS()) return BSSSection_;
446
447 if (Kind.isDataNoRel()) return DataSection;
448 if (Kind.isDataRelLocal()) return DataRelLocalSection;
449 if (Kind.isDataRel()) return DataRelSection;
450 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
451
452 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
453 return DataRelROSection;
454}
455
456/// getSectionForMergeableConstant - Given a mergeable constant with the
457/// specified size and relocation information, return a section that it
458/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000459const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf0144122009-07-28 03:13:23 +0000460getSectionForMergeableConstant(SectionKind Kind) const {
461 if (Kind.isMergeableConst4())
462 return MergeableConst4Section;
463 if (Kind.isMergeableConst8())
464 return MergeableConst8Section;
465 if (Kind.isMergeableConst16())
466 return MergeableConst16Section;
467 if (Kind.isReadOnly())
468 return ReadOnlySection;
469
470 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
471 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
472 return DataRelROSection;
473}
474
475//===----------------------------------------------------------------------===//
476// MachO
477//===----------------------------------------------------------------------===//
478
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000479void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
480 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000481 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000482 TextSection = getOrCreateSection("\t.text", true, SectionKind::Text);
483 DataSection = getOrCreateSection("\t.data", true, SectionKind::DataRel);
484
485 CStringSection_ = getOrCreateSection("\t.cstring", true,
486 SectionKind::MergeableCString);
487 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
488 SectionKind::MergeableConst4);
489 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
490 SectionKind::MergeableConst8);
Chris Lattner4bb253c2009-07-28 17:50:28 +0000491
492 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
493 // to using it in -static mode.
494 if (TM.getRelocationModel() != Reloc::Static &&
495 TM.getTargetData()->getPointerSize() == 32)
496 SixteenByteConstantSection =
497 getOrCreateSection("\t.literal16\n", true, SectionKind::MergeableConst16);
498 else
499 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000500
501 ReadOnlySection = getOrCreateSection("\t.const", true, SectionKind::ReadOnly);
502
503 TextCoalSection =
504 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
505 false, SectionKind::Text);
506 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
507 false, SectionKind::Text);
508 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
509 false, SectionKind::Text);
510 ConstDataSection = getOrCreateSection("\t.const_data", true,
511 SectionKind::ReadOnlyWithRel);
512 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
513 false, SectionKind::DataRel);
514}
515
Chris Lattnera87dea42009-07-31 18:48:30 +0000516const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnere53a6002009-07-29 05:09:30 +0000517SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
518 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000519 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
520
521 if (Kind.isText())
522 return Kind.isWeak() ? TextCoalSection : TextSection;
523
524 // If this is weak/linkonce, put this in a coalescable section, either in text
525 // or data depending on if it is writable.
526 if (Kind.isWeak()) {
527 if (Kind.isReadOnly())
528 return ConstTextCoalSection;
529 return DataCoalSection;
530 }
531
532 // FIXME: Alignment check should be handled by section classifier.
533 if (Kind.isMergeableCString()) {
534 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
535 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
536 const TargetData &TD = *TM.getTargetData();
537 unsigned Size = TD.getTypeAllocSize(Ty);
538 if (Size) {
539 unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV));
540 if (Align <= 32)
541 return CStringSection_;
542 }
543
544 return ReadOnlySection;
545 }
546
547 if (Kind.isMergeableConst()) {
548 if (Kind.isMergeableConst4())
549 return FourByteConstantSection;
550 if (Kind.isMergeableConst8())
551 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000552 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000553 return SixteenByteConstantSection;
554 return ReadOnlySection; // .const
555 }
556
557 // FIXME: ROData -> const in -static mode that is relocatable but they happen
558 // by the static linker. Why not mergeable?
559 if (Kind.isReadOnly())
560 return ReadOnlySection;
561
562 // If this is marked const, put it into a const section. But if the dynamic
563 // linker needs to write to it, put it in the data segment.
564 if (Kind.isReadOnlyWithRel())
565 return ConstDataSection;
566
567 // Otherwise, just drop the variable in the normal data section.
568 return DataSection;
569}
570
Chris Lattnera87dea42009-07-31 18:48:30 +0000571const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000572TargetLoweringObjectFileMachO::
573getSectionForMergeableConstant(SectionKind Kind) const {
574 // If this constant requires a relocation, we have to put it in the data
575 // segment, not in the text segment.
576 if (Kind.isDataRel())
577 return ConstDataSection;
578
579 if (Kind.isMergeableConst4())
580 return FourByteConstantSection;
581 if (Kind.isMergeableConst8())
582 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000583 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000584 return SixteenByteConstantSection;
585 return ReadOnlySection; // .const
586}
587
588//===----------------------------------------------------------------------===//
589// COFF
590//===----------------------------------------------------------------------===//
591
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000592void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
593 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000594 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner83d1c3d2009-07-29 05:25:42 +0000595 TextSection = getOrCreateSection("\t.text", true, SectionKind::Text);
596 DataSection = getOrCreateSection("\t.data", true, SectionKind::DataRel);
Chris Lattnerf0144122009-07-28 03:13:23 +0000597}
598
599void TargetLoweringObjectFileCOFF::
600getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
601 // FIXME: Inefficient.
602 std::string Res = ",\"";
603 if (Kind.isText())
604 Res += 'x';
605 if (Kind.isWriteable())
606 Res += 'w';
607 Res += "\"";
608
609 Str.append(Res.begin(), Res.end());
610}
611
612static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
613 if (Kind.isText())
614 return ".text$linkonce";
615 if (Kind.isWriteable())
616 return ".data$linkonce";
617 return ".rdata$linkonce";
618}
619
620
Chris Lattnera87dea42009-07-31 18:48:30 +0000621const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf0144122009-07-28 03:13:23 +0000622SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000623 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000624 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
625
626 // If this global is linkonce/weak and the target handles this by emitting it
627 // into a 'uniqued' section name, create and return the section now.
628 if (Kind.isWeak()) {
629 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
630 // FIXME: Use mangler interface (PR4584).
631 std::string Name = Prefix+GV->getNameStr();
632 return getOrCreateSection(Name.c_str(), false, Kind.getKind());
633 }
634
635 if (Kind.isText())
636 return getTextSection();
637
Chris Lattnera87dea42009-07-31 18:48:30 +0000638 if (Kind.isBSS() && BSSSection_ != 0)
639 return BSSSection_;
Chris Lattnerf0144122009-07-28 03:13:23 +0000640
641 if (Kind.isReadOnly() && ReadOnlySection != 0)
642 return ReadOnlySection;
643
644 return getDataSection();
645}
646