blob: ab4e24fea64b82144f3ed8aa43e8e62861638004 [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
82static bool isConstantString(const Constant *C) {
83 // First check: is we have constant array of i8 terminated with zero
84 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
85 // Check, if initializer is a null-terminated string
86 if (CVA && CVA->isCString())
87 return true;
88
89 // Another possibility: [1 x i8] zeroinitializer
90 if (isa<ConstantAggregateZero>(C))
91 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
92 return (Ty->getElementType() == Type::Int8Ty &&
93 Ty->getNumElements() == 1);
94
95 return false;
96}
97
Chris Lattner968ff112009-08-01 21:11:14 +000098/// SectionKindForGlobal - This is a top-level target-independent classifier for
99/// a global variable. Given an global variable and information from TM, it
100/// classifies the global in a variety of ways that make various target
101/// implementations simpler. The target implementation is free to ignore this
102/// extra info of course.
103static SectionKind SectionKindForGlobal(const GlobalValue *GV,
104 const TargetMachine &TM) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000105 Reloc::Model ReloModel = TM.getRelocationModel();
106
107 // Early exit - functions should be always in text sections.
108 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
109 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000110 return SectionKind::getText();
Chris Lattnerf0144122009-07-28 03:13:23 +0000111
112
113 // Handle thread-local data first.
114 if (GVar->isThreadLocal()) {
115 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000116 return SectionKind::getThreadBSS();
117 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000118 }
119
120 // Variable can be easily put to BSS section.
121 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000122 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000123
124 Constant *C = GVar->getInitializer();
125
126 // If the global is marked constant, we can put it into a mergable section,
127 // a mergable string section, or general .data if it contains relocations.
128 if (GVar->isConstant()) {
129 // If the initializer for the global contains something that requires a
130 // relocation, then we may have to drop this into a wriable data section
131 // even though it is marked const.
132 switch (C->getRelocationInfo()) {
133 default: llvm_unreachable("unknown relocation info kind");
134 case Constant::NoRelocation:
135 // If initializer is a null-terminated string, put it in a "cstring"
136 // section if the target has it.
137 if (isConstantString(C))
Chris Lattner27981192009-08-01 23:57:16 +0000138 return SectionKind::getMergeableCString();
Chris Lattnerf0144122009-07-28 03:13:23 +0000139
140 // Otherwise, just drop it into a mergable constant section. If we have
141 // a section for this size, use it, otherwise use the arbitrary sized
142 // mergable section.
143 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000144 case 4: return SectionKind::getMergeableConst4();
145 case 8: return SectionKind::getMergeableConst8();
146 case 16: return SectionKind::getMergeableConst16();
147 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000148 }
149
150 case Constant::LocalRelocation:
151 // In static relocation model, the linker will resolve all addresses, so
152 // the relocation entries will actually be constants by the time the app
153 // starts up. However, we can't put this into a mergable section, because
154 // the linker doesn't take relocations into consideration when it tries to
155 // merge entries in the section.
156 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000157 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000158
159 // Otherwise, the dynamic linker needs to fix it up, put it in the
160 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000161 return SectionKind::getReadOnlyWithRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000162
163 case Constant::GlobalRelocations:
164 // In static relocation model, the linker will resolve all addresses, so
165 // the relocation entries will actually be constants by the time the app
166 // starts up. However, we can't put this into a mergable section, because
167 // the linker doesn't take relocations into consideration when it tries to
168 // merge entries in the section.
169 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000170 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000171
172 // Otherwise, the dynamic linker needs to fix it up, put it in the
173 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000174 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000175 }
176 }
177
178 // Okay, this isn't a constant. If the initializer for the global is going
179 // to require a runtime relocation by the dynamic linker, put it into a more
180 // specific section to improve startup time of the app. This coalesces these
181 // globals together onto fewer pages, improving the locality of the dynamic
182 // linker.
183 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000184 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000185
186 switch (C->getRelocationInfo()) {
187 default: llvm_unreachable("unknown relocation info kind");
188 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000189 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000190 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000191 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000192 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000193 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000194 }
195}
196
197/// SectionForGlobal - This method computes the appropriate section to emit
198/// the specified global variable or function definition. This should not
199/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000200const MCSection *TargetLoweringObjectFile::
Chris Lattnere53a6002009-07-29 05:09:30 +0000201SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
202 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000203 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
204 "Can only be used for global definitions");
205
Chris Lattner968ff112009-08-01 21:11:14 +0000206 SectionKind Kind = SectionKindForGlobal(GV, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000207
Chris Lattnerf0144122009-07-28 03:13:23 +0000208 // Select section name.
209 if (GV->hasSection()) {
210 // If the target has special section hacks for specifically named globals,
211 // return them now.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000212 if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind))
Chris Lattnerf0144122009-07-28 03:13:23 +0000213 return TS;
214
215 // If the target has magic semantics for certain section names, make sure to
216 // pick up the flags. This allows the user to write things with attribute
217 // section and still get the appropriate section flags printed.
Chris Lattner968ff112009-08-01 21:11:14 +0000218 Kind = getKindForNamedSection(GV->getSection().c_str(), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000219
Chris Lattner968ff112009-08-01 21:11:14 +0000220 return getOrCreateSection(GV->getSection().c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000221 }
222
223
224 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000225 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000226}
227
228// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000229const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000230TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000231 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000232 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000233 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000234 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000235
Chris Lattnerf9650c02009-08-01 21:46:23 +0000236 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 return getTextSection();
238
Chris Lattner82458382009-08-01 21:56:13 +0000239 if (Kind.isBSS() && BSSSection != 0)
240 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000241
Chris Lattnerf9650c02009-08-01 21:46:23 +0000242 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000243 return ReadOnlySection;
244
245 return getDataSection();
246}
247
Chris Lattner83d77fa2009-08-01 23:46:12 +0000248/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000249/// specified size and relocation information, return a section that it
250/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000251const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000252TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000253 if (Kind.isReadOnly() && ReadOnlySection != 0)
254 return ReadOnlySection;
255
256 return DataSection;
257}
258
259
Chris Lattnera87dea42009-07-31 18:48:30 +0000260const MCSection *TargetLoweringObjectFile::
Chris Lattner968ff112009-08-01 21:11:14 +0000261getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnera87dea42009-07-31 18:48:30 +0000262 if (MCSection *S = Ctx->GetSection(Name))
263 return S;
Chris Lattner968ff112009-08-01 21:11:14 +0000264 return MCSection::Create(Name, isDirective, Kind, *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +0000265}
266
267
268
269//===----------------------------------------------------------------------===//
270// ELF
271//===----------------------------------------------------------------------===//
272
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000273void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
274 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000275 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000276 if (!HasCrazyBSS)
Chris Lattner27981192009-08-01 23:57:16 +0000277 BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000278 else
279 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
280 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000281 // FIXME2: this should just be handle by the section printer. We should get
282 // away from syntactic view of the sections and MCSection should just be a
283 // semantic view.
Chris Lattner27981192009-08-01 23:57:16 +0000284 BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000285
286
Chris Lattner27981192009-08-01 23:57:16 +0000287 TextSection = getOrCreateSection("\t.text", true, SectionKind::getText());
288 DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000289 ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000290 getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000291 TLSDataSection =
Chris Lattner27981192009-08-01 23:57:16 +0000292 getOrCreateSection("\t.tdata", false, SectionKind::getThreadData());
Chris Lattner82458382009-08-01 21:56:13 +0000293 CStringSection = getOrCreateSection("\t.rodata.str", true,
Chris Lattner27981192009-08-01 23:57:16 +0000294 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000295
Chris Lattner968ff112009-08-01 21:11:14 +0000296 TLSBSSSection = getOrCreateSection("\t.tbss", false,
Chris Lattner27981192009-08-01 23:57:16 +0000297 SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000298
299 DataRelSection = getOrCreateSection("\t.data.rel", false,
Chris Lattner27981192009-08-01 23:57:16 +0000300 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000301 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000302 SectionKind::getDataRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000303 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
Chris Lattner27981192009-08-01 23:57:16 +0000304 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000305 DataRelROLocalSection =
306 getOrCreateSection("\t.data.rel.ro.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000307 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000308
309 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
Chris Lattner27981192009-08-01 23:57:16 +0000310 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000311 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
Chris Lattner27981192009-08-01 23:57:16 +0000312 SectionKind::getMergeableConst8());
Chris Lattnerf0144122009-07-28 03:13:23 +0000313 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
Chris Lattner27981192009-08-01 23:57:16 +0000314 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000315
316 StaticCtorSection =
317 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
318 StaticDtorSection =
319 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000320
Chris Lattner18a4c162009-08-02 07:24:22 +0000321 // Exception Handling Sections.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000322
323 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
324 // it contains relocatable pointers. In PIC mode, this is probably a big
325 // runtime hit for C++ apps. Either the contents of the LSDA need to be
326 // adjusted or this should be a data section.
327 LSDASection =
328 getOrCreateSection(".gcc_except_table", false, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000329 EHFrameSection =
330 getOrCreateSection(".eh_frame", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000331
332 // Debug Info Sections.
333 DwarfAbbrevSection =
334 getOrCreateSection(".debug_abbrev", false, SectionKind::getMetadata());
335 DwarfInfoSection =
336 getOrCreateSection(".debug_info", false, SectionKind::getMetadata());
337 DwarfLineSection =
338 getOrCreateSection(".debug_line", false, SectionKind::getMetadata());
339 DwarfFrameSection =
340 getOrCreateSection(".debug_frame", false, SectionKind::getMetadata());
341 DwarfPubNamesSection =
342 getOrCreateSection(".debug_pubnames", false, SectionKind::getMetadata());
343 DwarfPubTypesSection =
344 getOrCreateSection(".debug_pubtypes", false, SectionKind::getMetadata());
345 DwarfStrSection =
346 getOrCreateSection(".debug_str", false, SectionKind::getMetadata());
347 DwarfLocSection =
348 getOrCreateSection(".debug_loc", false, SectionKind::getMetadata());
349 DwarfARangesSection =
350 getOrCreateSection(".debug_aranges", false, SectionKind::getMetadata());
351 DwarfRangesSection =
352 getOrCreateSection(".debug_ranges", false, SectionKind::getMetadata());
353 DwarfMacroInfoSection =
354 getOrCreateSection(".debug_macinfo", false, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000355}
356
357
Chris Lattner968ff112009-08-01 21:11:14 +0000358SectionKind TargetLoweringObjectFileELF::
359getKindForNamedSection(const char *Name, SectionKind K) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000360 if (Name[0] != '.') return K;
361
362 // Some lame default implementation based on some magic section names.
363 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
364 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
365 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
366 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000367 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000368
369 if (strcmp(Name, ".tdata") == 0 ||
370 strncmp(Name, ".tdata.", 7) == 0 ||
371 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
372 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000373 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000374
375 if (strcmp(Name, ".tbss") == 0 ||
376 strncmp(Name, ".tbss.", 6) == 0 ||
377 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
378 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000379 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000380
381 return K;
382}
383
384void TargetLoweringObjectFileELF::
385getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
386 Str.push_back(',');
387 Str.push_back('"');
388
389 if (!Kind.isMetadata())
390 Str.push_back('a');
391 if (Kind.isText())
392 Str.push_back('x');
393 if (Kind.isWriteable())
394 Str.push_back('w');
Chris Lattner82987bf2009-07-31 16:17:13 +0000395 if (Kind.isMergeableCString() ||
396 Kind.isMergeableConst4() ||
397 Kind.isMergeableConst8() ||
398 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000399 Str.push_back('M');
400 if (Kind.isMergeableCString())
401 Str.push_back('S');
402 if (Kind.isThreadLocal())
403 Str.push_back('T');
404
405 Str.push_back('"');
406 Str.push_back(',');
407
408 // If comment string is '@', e.g. as on ARM - use '%' instead
409 if (AtIsCommentChar)
410 Str.push_back('%');
411 else
412 Str.push_back('@');
413
414 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000415 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000416 KindStr = "nobits";
417 else
418 KindStr = "progbits";
419
420 Str.append(KindStr, KindStr+strlen(KindStr));
421
422 if (Kind.isMergeableCString()) {
423 // TODO: Eventually handle multiple byte character strings. For now, all
424 // mergable C strings are single byte.
425 Str.push_back(',');
426 Str.push_back('1');
427 } else if (Kind.isMergeableConst4()) {
428 Str.push_back(',');
429 Str.push_back('4');
430 } else if (Kind.isMergeableConst8()) {
431 Str.push_back(',');
432 Str.push_back('8');
433 } else if (Kind.isMergeableConst16()) {
434 Str.push_back(',');
435 Str.push_back('1');
436 Str.push_back('6');
437 }
438}
439
440
441static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
442 if (Kind.isText()) return ".gnu.linkonce.t.";
443 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
444
445 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
446 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
447
448 if (Kind.isBSS()) return ".gnu.linkonce.b.";
449 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
450 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
451 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
452 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
453
454 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
455 return ".gnu.linkonce.d.rel.ro.";
456}
457
Chris Lattnera87dea42009-07-31 18:48:30 +0000458const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000459SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000460 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000461
462 // If this global is linkonce/weak and the target handles this by emitting it
463 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000464 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000465 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000466 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000467 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000468 }
469
Chris Lattnerf9650c02009-08-01 21:46:23 +0000470 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000471
Chris Lattnerf9650c02009-08-01 21:46:23 +0000472 if (Kind.isMergeableCString()) {
Chris Lattner82458382009-08-01 21:56:13 +0000473 assert(CStringSection && "Should have string section prefix");
Chris Lattnerf0144122009-07-28 03:13:23 +0000474
Chris Lattner067fe1a2009-07-29 04:54:38 +0000475 // We also need alignment here.
476 // FIXME: this is getting the alignment of the character, not the
477 // alignment of the global!
478 unsigned Align =
479 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000480
Chris Lattner82458382009-08-01 21:56:13 +0000481 std::string Name = CStringSection->getName() + "1." + utostr(Align);
Chris Lattner067fe1a2009-07-29 04:54:38 +0000482 return getOrCreateSection(Name.c_str(), false,
Chris Lattner27981192009-08-01 23:57:16 +0000483 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000484 }
485
Chris Lattnerf9650c02009-08-01 21:46:23 +0000486 if (Kind.isMergeableConst()) {
487 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000488 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000489 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000490 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000491 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000492 return MergeableConst16Section;
493 return ReadOnlySection; // .const
494 }
495
Chris Lattnerf9650c02009-08-01 21:46:23 +0000496 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000497
Chris Lattnerf9650c02009-08-01 21:46:23 +0000498 if (Kind.isThreadData()) return TLSDataSection;
499 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000500
Chris Lattner82458382009-08-01 21:56:13 +0000501 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000502
Chris Lattnerf9650c02009-08-01 21:46:23 +0000503 if (Kind.isDataNoRel()) return DataSection;
504 if (Kind.isDataRelLocal()) return DataRelLocalSection;
505 if (Kind.isDataRel()) return DataRelSection;
506 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000507
Chris Lattnerf9650c02009-08-01 21:46:23 +0000508 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000509 return DataRelROSection;
510}
511
Chris Lattner83d77fa2009-08-01 23:46:12 +0000512/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000513/// specified size and relocation information, return a section that it
514/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000515const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000516getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000517 if (Kind.isMergeableConst4())
518 return MergeableConst4Section;
519 if (Kind.isMergeableConst8())
520 return MergeableConst8Section;
521 if (Kind.isMergeableConst16())
522 return MergeableConst16Section;
523 if (Kind.isReadOnly())
524 return ReadOnlySection;
525
526 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
527 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
528 return DataRelROSection;
529}
530
531//===----------------------------------------------------------------------===//
532// MachO
533//===----------------------------------------------------------------------===//
534
Chris Lattner11e96572009-08-03 21:53:27 +0000535const MCSection *TargetLoweringObjectFileMachO::
536getMachOSection(const char *Name, bool isDirective, SectionKind K) {
537 // FOR NOW, Just forward.
538 return getOrCreateSection(Name, isDirective, K);
539}
540
541
542
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000543void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
544 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000545 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000546 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000547 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000548 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000549 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000550
Chris Lattner82458382009-08-01 21:56:13 +0000551 CStringSection = getOrCreateSection("\t.cstring", true,
Chris Lattner27981192009-08-01 23:57:16 +0000552 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000553 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000554 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000555 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000556 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000557
558 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
559 // to using it in -static mode.
560 if (TM.getRelocationModel() != Reloc::Static &&
561 TM.getTargetData()->getPointerSize() == 32)
562 SixteenByteConstantSection =
Chris Lattner968ff112009-08-01 21:11:14 +0000563 getOrCreateSection("\t.literal16\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000564 SectionKind::getMergeableConst16());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000565 else
566 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000567
Chris Lattner968ff112009-08-01 21:11:14 +0000568 ReadOnlySection = getOrCreateSection("\t.const", true,
Chris Lattner27981192009-08-01 23:57:16 +0000569 SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000570
571 TextCoalSection =
572 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Chris Lattner27981192009-08-01 23:57:16 +0000573 false, SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000574 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000575 false,
Chris Lattner27981192009-08-01 23:57:16 +0000576 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000577 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000578 false,
Chris Lattner27981192009-08-01 23:57:16 +0000579 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000580 ConstDataSection = getOrCreateSection("\t.const_data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000581 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000582 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000583 false,
Chris Lattner27981192009-08-01 23:57:16 +0000584 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000585
586 if (TM.getRelocationModel() == Reloc::Static) {
587 StaticCtorSection =
588 getOrCreateSection(".constructor", true, SectionKind::getDataRel());
589 StaticDtorSection =
590 getOrCreateSection(".destructor", true, SectionKind::getDataRel());
591 } else {
592 StaticCtorSection =
593 getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel());
594 StaticDtorSection =
595 getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel());
596 }
597
Chris Lattner18a4c162009-08-02 07:24:22 +0000598 // Exception Handling.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000599 LSDASection = getOrCreateSection("__DATA,__gcc_except_tab", false,
600 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000601 EHFrameSection =
602 getOrCreateSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
603 "+live_support", false, SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000604
605 // Debug Information.
606 // FIXME: Don't use 'directive' syntax: need flags for debug/regular??
607 // FIXME: Need __DWARF segment.
608 DwarfAbbrevSection =
609 getOrCreateSection(".section __DWARF,__debug_abbrev,regular,debug", true,
610 SectionKind::getMetadata());
611 DwarfInfoSection =
612 getOrCreateSection(".section __DWARF,__debug_info,regular,debug", true,
613 SectionKind::getMetadata());
614 DwarfLineSection =
615 getOrCreateSection(".section __DWARF,__debug_line,regular,debug", true,
616 SectionKind::getMetadata());
617 DwarfFrameSection =
618 getOrCreateSection(".section __DWARF,__debug_frame,regular,debug", true,
619 SectionKind::getMetadata());
620 DwarfPubNamesSection =
621 getOrCreateSection(".section __DWARF,__debug_pubnames,regular,debug", true,
622 SectionKind::getMetadata());
623 DwarfPubTypesSection =
624 getOrCreateSection(".section __DWARF,__debug_pubtypes,regular,debug", true,
625 SectionKind::getMetadata());
626 DwarfStrSection =
627 getOrCreateSection(".section __DWARF,__debug_str,regular,debug", true,
628 SectionKind::getMetadata());
629 DwarfLocSection =
630 getOrCreateSection(".section __DWARF,__debug_loc,regular,debug", true,
631 SectionKind::getMetadata());
632 DwarfARangesSection =
633 getOrCreateSection(".section __DWARF,__debug_aranges,regular,debug", true,
634 SectionKind::getMetadata());
635 DwarfRangesSection =
636 getOrCreateSection(".section __DWARF,__debug_ranges,regular,debug", true,
637 SectionKind::getMetadata());
638 DwarfMacroInfoSection =
639 getOrCreateSection(".section __DWARF,__debug_macinfo,regular,debug", true,
640 SectionKind::getMetadata());
641 DwarfDebugInlineSection =
642 getOrCreateSection(".section __DWARF,__debug_inlined,regular,debug", true,
643 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000644}
645
Chris Lattnera87dea42009-07-31 18:48:30 +0000646const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000647SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000648 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000649 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000650
Chris Lattnerf9650c02009-08-01 21:46:23 +0000651 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000652 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000653
654 // If this is weak/linkonce, put this in a coalescable section, either in text
655 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000656 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000657 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000658 return ConstTextCoalSection;
659 return DataCoalSection;
660 }
661
662 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000663 if (Kind.isMergeableCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000664 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
665 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
666 const TargetData &TD = *TM.getTargetData();
667 unsigned Size = TD.getTypeAllocSize(Ty);
668 if (Size) {
669 unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV));
670 if (Align <= 32)
Chris Lattner82458382009-08-01 21:56:13 +0000671 return CStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000672 }
673
674 return ReadOnlySection;
675 }
676
Chris Lattnerf9650c02009-08-01 21:46:23 +0000677 if (Kind.isMergeableConst()) {
678 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000679 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000680 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000681 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000682 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000683 return SixteenByteConstantSection;
684 return ReadOnlySection; // .const
685 }
686
687 // FIXME: ROData -> const in -static mode that is relocatable but they happen
688 // by the static linker. Why not mergeable?
Chris Lattnerf9650c02009-08-01 21:46:23 +0000689 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000690 return ReadOnlySection;
691
692 // If this is marked const, put it into a const section. But if the dynamic
693 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000694 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000695 return ConstDataSection;
696
697 // Otherwise, just drop the variable in the normal data section.
698 return DataSection;
699}
700
Chris Lattnera87dea42009-07-31 18:48:30 +0000701const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000702TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000703 // If this constant requires a relocation, we have to put it in the data
704 // segment, not in the text segment.
705 if (Kind.isDataRel())
706 return ConstDataSection;
707
708 if (Kind.isMergeableConst4())
709 return FourByteConstantSection;
710 if (Kind.isMergeableConst8())
711 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000712 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000713 return SixteenByteConstantSection;
714 return ReadOnlySection; // .const
715}
716
Chris Lattner26630c12009-07-31 20:52:39 +0000717/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
718/// not to emit the UsedDirective for some symbols in llvm.used.
719// FIXME: REMOVE this (rdar://7071300)
720bool TargetLoweringObjectFileMachO::
721shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
722 /// On Darwin, internally linked data beginning with "L" or "l" does not have
723 /// the directive emitted (this occurs in ObjC metadata).
724 if (!GV) return false;
725
726 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
727 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
728 // FIXME: ObjC metadata is currently emitted as internal symbols that have
729 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
730 // this horrible hack can go away.
731 const std::string &Name = Mang->getMangledName(GV);
732 if (Name[0] == 'L' || Name[0] == 'l')
733 return false;
734 }
735
736 return true;
737}
738
739
Chris Lattnerf0144122009-07-28 03:13:23 +0000740//===----------------------------------------------------------------------===//
741// COFF
742//===----------------------------------------------------------------------===//
743
Chris Lattner11e96572009-08-03 21:53:27 +0000744const MCSection *TargetLoweringObjectFileCOFF::
745getCOFFSection(const char *Name, bool isDirective, SectionKind K) {
746 return getOrCreateSection(Name, isDirective, K);
747}
748
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000749void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
750 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000751 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000752 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000753 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000754 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000755 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000756 StaticCtorSection =
757 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
758 StaticDtorSection =
759 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000760
761
762 // Debug info.
763 // FIXME: Don't use 'directive' mode here.
764 DwarfAbbrevSection =
765 getOrCreateSection("\t.section\t.debug_abbrev,\"dr\"",
766 true, SectionKind::getMetadata());
767 DwarfInfoSection =
768 getOrCreateSection("\t.section\t.debug_info,\"dr\"",
769 true, SectionKind::getMetadata());
770 DwarfLineSection =
771 getOrCreateSection("\t.section\t.debug_line,\"dr\"",
772 true, SectionKind::getMetadata());
773 DwarfFrameSection =
774 getOrCreateSection("\t.section\t.debug_frame,\"dr\"",
775 true, SectionKind::getMetadata());
776 DwarfPubNamesSection =
777 getOrCreateSection("\t.section\t.debug_pubnames,\"dr\"",
778 true, SectionKind::getMetadata());
779 DwarfPubTypesSection =
780 getOrCreateSection("\t.section\t.debug_pubtypes,\"dr\"",
781 true, SectionKind::getMetadata());
782 DwarfStrSection =
783 getOrCreateSection("\t.section\t.debug_str,\"dr\"",
784 true, SectionKind::getMetadata());
785 DwarfLocSection =
786 getOrCreateSection("\t.section\t.debug_loc,\"dr\"",
787 true, SectionKind::getMetadata());
788 DwarfARangesSection =
789 getOrCreateSection("\t.section\t.debug_aranges,\"dr\"",
790 true, SectionKind::getMetadata());
791 DwarfRangesSection =
792 getOrCreateSection("\t.section\t.debug_ranges,\"dr\"",
793 true, SectionKind::getMetadata());
794 DwarfMacroInfoSection =
795 getOrCreateSection("\t.section\t.debug_macinfo,\"dr\"",
796 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000797}
798
799void TargetLoweringObjectFileCOFF::
800getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
801 // FIXME: Inefficient.
802 std::string Res = ",\"";
803 if (Kind.isText())
804 Res += 'x';
805 if (Kind.isWriteable())
806 Res += 'w';
807 Res += "\"";
808
809 Str.append(Res.begin(), Res.end());
810}
811
812static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
813 if (Kind.isText())
814 return ".text$linkonce";
815 if (Kind.isWriteable())
816 return ".data$linkonce";
817 return ".rdata$linkonce";
818}
819
820
Chris Lattnera87dea42009-07-31 18:48:30 +0000821const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000822SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000823 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000824 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000825
826 // If this global is linkonce/weak and the target handles this by emitting it
827 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000828 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000829 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000830 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000831 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000832 }
833
Chris Lattnerf9650c02009-08-01 21:46:23 +0000834 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000835 return getTextSection();
836
Chris Lattnerf0144122009-07-28 03:13:23 +0000837 return getDataSection();
838}
839