blob: 4f07f9036a8ed5fdfd2889386c13540d649fa0bc [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"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetData.h"
21#include "llvm/Target/TargetOptions.h"
22#include "llvm/ADT/StringExtras.h"
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26// Generic Code
27//===----------------------------------------------------------------------===//
28
29TargetLoweringObjectFile::TargetLoweringObjectFile() {
30 TextSection = 0;
31 DataSection = 0;
32 BSSSection_ = 0;
33 ReadOnlySection = 0;
34 TLSDataSection = 0;
35 TLSBSSSection = 0;
36 CStringSection_ = 0;
37}
38
39TargetLoweringObjectFile::~TargetLoweringObjectFile() {
40}
41
42static bool isSuitableForBSS(const GlobalVariable *GV) {
43 Constant *C = GV->getInitializer();
44
45 // Must have zero initializer.
46 if (!C->isNullValue())
47 return false;
48
49 // Leave constant zeros in readonly constant sections, so they can be shared.
50 if (GV->isConstant())
51 return false;
52
53 // If the global has an explicit section specified, don't put it in BSS.
54 if (!GV->getSection().empty())
55 return false;
56
57 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
58 if (NoZerosInBSS)
59 return false;
60
61 // Otherwise, put it in BSS!
62 return true;
63}
64
65static bool isConstantString(const Constant *C) {
66 // First check: is we have constant array of i8 terminated with zero
67 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
68 // Check, if initializer is a null-terminated string
69 if (CVA && CVA->isCString())
70 return true;
71
72 // Another possibility: [1 x i8] zeroinitializer
73 if (isa<ConstantAggregateZero>(C))
74 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
75 return (Ty->getElementType() == Type::Int8Ty &&
76 Ty->getNumElements() == 1);
77
78 return false;
79}
80
81static SectionKind::Kind SectionKindForGlobal(const GlobalValue *GV,
82 const TargetMachine &TM) {
83 Reloc::Model ReloModel = TM.getRelocationModel();
84
85 // Early exit - functions should be always in text sections.
86 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
87 if (GVar == 0)
88 return SectionKind::Text;
89
90
91 // Handle thread-local data first.
92 if (GVar->isThreadLocal()) {
93 if (isSuitableForBSS(GVar))
94 return SectionKind::ThreadBSS;
95 return SectionKind::ThreadData;
96 }
97
98 // Variable can be easily put to BSS section.
99 if (isSuitableForBSS(GVar))
100 return SectionKind::BSS;
101
102 Constant *C = GVar->getInitializer();
103
104 // If the global is marked constant, we can put it into a mergable section,
105 // a mergable string section, or general .data if it contains relocations.
106 if (GVar->isConstant()) {
107 // If the initializer for the global contains something that requires a
108 // relocation, then we may have to drop this into a wriable data section
109 // even though it is marked const.
110 switch (C->getRelocationInfo()) {
111 default: llvm_unreachable("unknown relocation info kind");
112 case Constant::NoRelocation:
113 // If initializer is a null-terminated string, put it in a "cstring"
114 // section if the target has it.
115 if (isConstantString(C))
116 return SectionKind::MergeableCString;
117
118 // Otherwise, just drop it into a mergable constant section. If we have
119 // a section for this size, use it, otherwise use the arbitrary sized
120 // mergable section.
121 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
122 case 4: return SectionKind::MergeableConst4;
123 case 8: return SectionKind::MergeableConst8;
124 case 16: return SectionKind::MergeableConst16;
125 default: return SectionKind::MergeableConst;
126 }
127
128 case Constant::LocalRelocation:
129 // In static relocation model, the linker will resolve all addresses, so
130 // the relocation entries will actually be constants by the time the app
131 // starts up. However, we can't put this into a mergable section, because
132 // the linker doesn't take relocations into consideration when it tries to
133 // merge entries in the section.
134 if (ReloModel == Reloc::Static)
135 return SectionKind::ReadOnly;
136
137 // Otherwise, the dynamic linker needs to fix it up, put it in the
138 // writable data.rel.local section.
139 return SectionKind::ReadOnlyWithRelLocal;
140
141 case Constant::GlobalRelocations:
142 // In static relocation model, the linker will resolve all addresses, so
143 // the relocation entries will actually be constants by the time the app
144 // starts up. However, we can't put this into a mergable section, because
145 // the linker doesn't take relocations into consideration when it tries to
146 // merge entries in the section.
147 if (ReloModel == Reloc::Static)
148 return SectionKind::ReadOnly;
149
150 // Otherwise, the dynamic linker needs to fix it up, put it in the
151 // writable data.rel section.
152 return SectionKind::ReadOnlyWithRel;
153 }
154 }
155
156 // Okay, this isn't a constant. If the initializer for the global is going
157 // to require a runtime relocation by the dynamic linker, put it into a more
158 // specific section to improve startup time of the app. This coalesces these
159 // globals together onto fewer pages, improving the locality of the dynamic
160 // linker.
161 if (ReloModel == Reloc::Static)
162 return SectionKind::DataNoRel;
163
164 switch (C->getRelocationInfo()) {
165 default: llvm_unreachable("unknown relocation info kind");
166 case Constant::NoRelocation:
167 return SectionKind::DataNoRel;
168 case Constant::LocalRelocation:
169 return SectionKind::DataRelLocal;
170 case Constant::GlobalRelocations:
171 return SectionKind::DataRel;
172 }
173}
174
175/// SectionForGlobal - This method computes the appropriate section to emit
176/// the specified global variable or function definition. This should not
177/// be passed external (or available externally) globals.
178const Section *TargetLoweringObjectFile::
Chris Lattnere53a6002009-07-29 05:09:30 +0000179SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
180 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000181 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
182 "Can only be used for global definitions");
183
184 SectionKind::Kind GVKind = SectionKindForGlobal(GV, TM);
185
186 SectionKind Kind = SectionKind::get(GVKind, GV->isWeakForLinker(),
187 GV->hasSection());
188
189
190 // Select section name.
191 if (GV->hasSection()) {
192 // If the target has special section hacks for specifically named globals,
193 // return them now.
Chris Lattnere53a6002009-07-29 05:09:30 +0000194 if (const Section *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind))
Chris Lattnerf0144122009-07-28 03:13:23 +0000195 return TS;
196
197 // If the target has magic semantics for certain section names, make sure to
198 // pick up the flags. This allows the user to write things with attribute
199 // section and still get the appropriate section flags printed.
200 GVKind = getKindForNamedSection(GV->getSection().c_str(), GVKind);
201
202 return getOrCreateSection(GV->getSection().c_str(), false, GVKind);
203 }
204
205
206 // Use default section depending on the 'type' of global
Chris Lattnere53a6002009-07-29 05:09:30 +0000207 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000208}
209
210// Lame default implementation. Calculate the section name for global.
211const Section*
212TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
213 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000214 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000215 const TargetMachine &TM) const{
216 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
217
218 if (Kind.isText())
219 return getTextSection();
220
221 if (Kind.isBSS() && BSSSection_ != 0)
222 return BSSSection_;
223
224 if (Kind.isReadOnly() && ReadOnlySection != 0)
225 return ReadOnlySection;
226
227 return getDataSection();
228}
229
230/// getSectionForMergableConstant - Given a mergable constant with the
231/// specified size and relocation information, return a section that it
232/// should be placed in.
233const Section *
234TargetLoweringObjectFile::
235getSectionForMergeableConstant(SectionKind Kind) const {
236 if (Kind.isReadOnly() && ReadOnlySection != 0)
237 return ReadOnlySection;
238
239 return DataSection;
240}
241
242
243const Section *TargetLoweringObjectFile::
244getOrCreateSection(const char *Name, bool isDirective,
245 SectionKind::Kind Kind) const {
246 Section &S = Sections[Name];
247
248 // This is newly-created section, set it up properly.
249 if (S.Name.empty()) {
250 S.Kind = SectionKind::get(Kind, false /*weak*/, !isDirective);
251 S.Name = Name;
252 }
253
254 return &S;
255}
256
257
258
259//===----------------------------------------------------------------------===//
260// ELF
261//===----------------------------------------------------------------------===//
262
263TargetLoweringObjectFileELF::TargetLoweringObjectFileELF(bool atIsCommentChar,
264 bool HasCrazyBSS)
265 : AtIsCommentChar(atIsCommentChar) {
266
267 if (!HasCrazyBSS)
268 BSSSection_ = getOrCreateSection("\t.bss", true, SectionKind::BSS);
269 else
270 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
271 // FIXME: Does .section .bss work everywhere??
272 BSSSection_ = getOrCreateSection("\t.bss", false, SectionKind::BSS);
273
274
275 TextSection = getOrCreateSection("\t.text", true, SectionKind::Text);
276 DataSection = getOrCreateSection("\t.data", true, SectionKind::DataRel);
277 ReadOnlySection =
278 getOrCreateSection("\t.rodata", false, SectionKind::ReadOnly);
279 TLSDataSection =
280 getOrCreateSection("\t.tdata", false, SectionKind::ThreadData);
281 CStringSection_ = getOrCreateSection("\t.rodata.str", true,
282 SectionKind::MergeableCString);
283
284 TLSBSSSection = getOrCreateSection("\t.tbss", false, SectionKind::ThreadBSS);
285
286 DataRelSection = getOrCreateSection("\t.data.rel", false,
287 SectionKind::DataRel);
288 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
289 SectionKind::DataRelLocal);
290 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
291 SectionKind::ReadOnlyWithRel);
292 DataRelROLocalSection =
293 getOrCreateSection("\t.data.rel.ro.local", false,
294 SectionKind::ReadOnlyWithRelLocal);
295
296 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
297 SectionKind::MergeableConst4);
298 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
299 SectionKind::MergeableConst8);
300 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
301 SectionKind::MergeableConst16);
302}
303
304
305SectionKind::Kind TargetLoweringObjectFileELF::
306getKindForNamedSection(const char *Name, SectionKind::Kind K) const {
307 if (Name[0] != '.') return K;
308
309 // Some lame default implementation based on some magic section names.
310 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
311 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
312 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
313 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
314 return SectionKind::BSS;
315
316 if (strcmp(Name, ".tdata") == 0 ||
317 strncmp(Name, ".tdata.", 7) == 0 ||
318 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
319 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
320 return SectionKind::ThreadData;
321
322 if (strcmp(Name, ".tbss") == 0 ||
323 strncmp(Name, ".tbss.", 6) == 0 ||
324 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
325 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
326 return SectionKind::ThreadBSS;
327
328 return K;
329}
330
331void TargetLoweringObjectFileELF::
332getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
333 Str.push_back(',');
334 Str.push_back('"');
335
336 if (!Kind.isMetadata())
337 Str.push_back('a');
338 if (Kind.isText())
339 Str.push_back('x');
340 if (Kind.isWriteable())
341 Str.push_back('w');
342 if (Kind.isMergeableConst() || Kind.isMergeableCString())
343 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
402const Section *TargetLoweringObjectFileELF::
403SelectSectionForGlobal(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);
410 // FIXME: Use mangler interface (PR4584).
411 std::string Name = Prefix+GV->getNameStr();
412 return getOrCreateSection(Name.c_str(), false, Kind.getKind());
413 }
414
415 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000416
Chris Lattnerf0144122009-07-28 03:13:23 +0000417 if (Kind.isMergeableCString()) {
Chris Lattner067fe1a2009-07-29 04:54:38 +0000418 assert(CStringSection_ && "Should have string section prefix");
Chris Lattnerf0144122009-07-28 03:13:23 +0000419
Chris Lattner067fe1a2009-07-29 04:54:38 +0000420 // We also need alignment here.
421 // FIXME: this is getting the alignment of the character, not the
422 // alignment of the global!
423 unsigned Align =
424 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000425
Chris Lattnera6792072009-07-29 04:55:08 +0000426 std::string Name = CStringSection_->getName() + "1." + utostr(Align);
Chris Lattner067fe1a2009-07-29 04:54:38 +0000427 return getOrCreateSection(Name.c_str(), false,
428 SectionKind::MergeableCString);
Chris Lattnerf0144122009-07-28 03:13:23 +0000429 }
430
431 if (Kind.isMergeableConst()) {
432 if (Kind.isMergeableConst4())
433 return MergeableConst4Section;
434 if (Kind.isMergeableConst8())
435 return MergeableConst8Section;
436 if (Kind.isMergeableConst16())
437 return MergeableConst16Section;
438 return ReadOnlySection; // .const
439 }
440
441 if (Kind.isReadOnly()) return ReadOnlySection;
442
443 if (Kind.isThreadData()) return TLSDataSection;
444 if (Kind.isThreadBSS()) return TLSBSSSection;
445
446 if (Kind.isBSS()) return BSSSection_;
447
448 if (Kind.isDataNoRel()) return DataSection;
449 if (Kind.isDataRelLocal()) return DataRelLocalSection;
450 if (Kind.isDataRel()) return DataRelSection;
451 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
452
453 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
454 return DataRelROSection;
455}
456
457/// getSectionForMergeableConstant - Given a mergeable constant with the
458/// specified size and relocation information, return a section that it
459/// should be placed in.
460const Section *TargetLoweringObjectFileELF::
461getSectionForMergeableConstant(SectionKind Kind) const {
462 if (Kind.isMergeableConst4())
463 return MergeableConst4Section;
464 if (Kind.isMergeableConst8())
465 return MergeableConst8Section;
466 if (Kind.isMergeableConst16())
467 return MergeableConst16Section;
468 if (Kind.isReadOnly())
469 return ReadOnlySection;
470
471 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
472 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
473 return DataRelROSection;
474}
475
476//===----------------------------------------------------------------------===//
477// MachO
478//===----------------------------------------------------------------------===//
479
480TargetLoweringObjectFileMachO::
Chris Lattner4bb253c2009-07-28 17:50:28 +0000481TargetLoweringObjectFileMachO(const TargetMachine &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 Lattnere53a6002009-07-29 05:09:30 +0000516const Section *TargetLoweringObjectFileMachO::
517SelectSectionForGlobal(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
571const Section *
572TargetLoweringObjectFileMachO::
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
592TargetLoweringObjectFileCOFF::TargetLoweringObjectFileCOFF() {
593 TextSection = getOrCreateSection("_text", true, SectionKind::Text);
594 DataSection = getOrCreateSection("_data", true, SectionKind::DataRel);
595}
596
597void TargetLoweringObjectFileCOFF::
598getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
599 // FIXME: Inefficient.
600 std::string Res = ",\"";
601 if (Kind.isText())
602 Res += 'x';
603 if (Kind.isWriteable())
604 Res += 'w';
605 Res += "\"";
606
607 Str.append(Res.begin(), Res.end());
608}
609
610static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
611 if (Kind.isText())
612 return ".text$linkonce";
613 if (Kind.isWriteable())
614 return ".data$linkonce";
615 return ".rdata$linkonce";
616}
617
618
619const Section *TargetLoweringObjectFileCOFF::
620SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000621 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000622 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
623
624 // If this global is linkonce/weak and the target handles this by emitting it
625 // into a 'uniqued' section name, create and return the section now.
626 if (Kind.isWeak()) {
627 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
628 // FIXME: Use mangler interface (PR4584).
629 std::string Name = Prefix+GV->getNameStr();
630 return getOrCreateSection(Name.c_str(), false, Kind.getKind());
631 }
632
633 if (Kind.isText())
634 return getTextSection();
635
636 if (Kind.isBSS())
637 if (const Section *S = BSSSection_)
638 return S;
639
640 if (Kind.isReadOnly() && ReadOnlySection != 0)
641 return ReadOnlySection;
642
643 return getDataSection();
644}
645