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