blob: 7489883998d84c32db595c3a2d7444fd95a6512e [file] [log] [blame]
Chris Lattnerf0144122009-07-28 03:13:23 +00001//===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements classes used to handle lowerings specific to common
11// object file formats.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Target/TargetLoweringObjectFile.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/GlobalVariable.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000019#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCSection.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000021#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetOptions.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000024#include "llvm/Support/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000025#include "llvm/ADT/StringExtras.h"
26using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29// Generic Code
30//===----------------------------------------------------------------------===//
31
Chris Lattnera87dea42009-07-31 18:48:30 +000032TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000033 TextSection = 0;
34 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000035 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000036 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000037 StaticCtorSection = 0;
38 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000039 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000040 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000041
42 DwarfAbbrevSection = 0;
43 DwarfInfoSection = 0;
44 DwarfLineSection = 0;
45 DwarfFrameSection = 0;
46 DwarfPubNamesSection = 0;
47 DwarfPubTypesSection = 0;
48 DwarfDebugInlineSection = 0;
49 DwarfStrSection = 0;
50 DwarfLocSection = 0;
51 DwarfARangesSection = 0;
52 DwarfRangesSection = 0;
53 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000054}
55
56TargetLoweringObjectFile::~TargetLoweringObjectFile() {
57}
58
59static bool isSuitableForBSS(const GlobalVariable *GV) {
60 Constant *C = GV->getInitializer();
61
62 // Must have zero initializer.
63 if (!C->isNullValue())
64 return false;
65
66 // Leave constant zeros in readonly constant sections, so they can be shared.
67 if (GV->isConstant())
68 return false;
69
70 // If the global has an explicit section specified, don't put it in BSS.
71 if (!GV->getSection().empty())
72 return false;
73
74 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
75 if (NoZerosInBSS)
76 return false;
77
78 // Otherwise, put it in BSS!
79 return true;
80}
81
Chris Lattner1850e5a2009-08-04 16:13:09 +000082/// IsNullTerminatedString - Return true if the specified constant (which is
83/// known to have a type that is an array of 1/2/4 byte elements) ends with a
84/// nul value and contains no other nuls in it.
85static bool IsNullTerminatedString(const Constant *C) {
86 const ArrayType *ATy = cast<ArrayType>(C->getType());
87
Chris Lattnerf0144122009-07-28 03:13:23 +000088 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000089 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
90 if (ATy->getNumElements() == 0) return false;
91
92 ConstantInt *Null =
93 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
94 if (Null == 0 || Null->getZExtValue() != 0)
95 return false; // Not null terminated.
96
97 // Verify that the null doesn't occur anywhere else in the string.
98 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
99 // Reject constantexpr elements etc.
100 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
101 CVA->getOperand(i) == Null)
102 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000103 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000104 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000105
106 // Another possibility: [1 x i8] zeroinitializer
107 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000108 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000109
110 return false;
111}
112
Chris Lattner968ff112009-08-01 21:11:14 +0000113/// SectionKindForGlobal - This is a top-level target-independent classifier for
114/// a global variable. Given an global variable and information from TM, it
115/// classifies the global in a variety of ways that make various target
116/// implementations simpler. The target implementation is free to ignore this
117/// extra info of course.
118static SectionKind SectionKindForGlobal(const GlobalValue *GV,
119 const TargetMachine &TM) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000120 Reloc::Model ReloModel = TM.getRelocationModel();
121
122 // Early exit - functions should be always in text sections.
123 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
124 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000125 return SectionKind::getText();
Chris Lattnerf0144122009-07-28 03:13:23 +0000126
127
128 // Handle thread-local data first.
129 if (GVar->isThreadLocal()) {
130 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000131 return SectionKind::getThreadBSS();
132 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000133 }
134
135 // Variable can be easily put to BSS section.
136 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000137 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000138
139 Constant *C = GVar->getInitializer();
140
141 // If the global is marked constant, we can put it into a mergable section,
142 // a mergable string section, or general .data if it contains relocations.
143 if (GVar->isConstant()) {
144 // If the initializer for the global contains something that requires a
145 // relocation, then we may have to drop this into a wriable data section
146 // even though it is marked const.
147 switch (C->getRelocationInfo()) {
148 default: llvm_unreachable("unknown relocation info kind");
149 case Constant::NoRelocation:
150 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000151 // section of the right width.
152 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
153 if (const IntegerType *ITy =
154 dyn_cast<IntegerType>(ATy->getElementType())) {
155 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
156 ITy->getBitWidth() == 32) &&
157 IsNullTerminatedString(C)) {
158 if (ITy->getBitWidth() == 8)
159 return SectionKind::getMergeable1ByteCString();
160 if (ITy->getBitWidth() == 16)
161 return SectionKind::getMergeable2ByteCString();
162
163 assert(ITy->getBitWidth() == 32 && "Unknown width");
164 return SectionKind::getMergeable4ByteCString();
165 }
166 }
167 }
Chris Lattner3b24c012009-08-04 05:35:56 +0000168
Chris Lattnerf0144122009-07-28 03:13:23 +0000169 // Otherwise, just drop it into a mergable constant section. If we have
170 // a section for this size, use it, otherwise use the arbitrary sized
171 // mergable section.
172 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000173 case 4: return SectionKind::getMergeableConst4();
174 case 8: return SectionKind::getMergeableConst8();
175 case 16: return SectionKind::getMergeableConst16();
176 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000177 }
178
179 case Constant::LocalRelocation:
180 // In static relocation model, the linker will resolve all addresses, so
181 // the relocation entries will actually be constants by the time the app
182 // starts up. However, we can't put this into a mergable section, because
183 // the linker doesn't take relocations into consideration when it tries to
184 // merge entries in the section.
185 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000186 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000187
188 // Otherwise, the dynamic linker needs to fix it up, put it in the
189 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000190 return SectionKind::getReadOnlyWithRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000191
192 case Constant::GlobalRelocations:
193 // In static relocation model, the linker will resolve all addresses, so
194 // the relocation entries will actually be constants by the time the app
195 // starts up. However, we can't put this into a mergable section, because
196 // the linker doesn't take relocations into consideration when it tries to
197 // merge entries in the section.
198 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000199 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000200
201 // Otherwise, the dynamic linker needs to fix it up, put it in the
202 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000203 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000204 }
205 }
206
207 // Okay, this isn't a constant. If the initializer for the global is going
208 // to require a runtime relocation by the dynamic linker, put it into a more
209 // specific section to improve startup time of the app. This coalesces these
210 // globals together onto fewer pages, improving the locality of the dynamic
211 // linker.
212 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000213 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000214
215 switch (C->getRelocationInfo()) {
216 default: llvm_unreachable("unknown relocation info kind");
217 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000218 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000219 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000220 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000221 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000222 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000223 }
224}
225
226/// SectionForGlobal - This method computes the appropriate section to emit
227/// the specified global variable or function definition. This should not
228/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000229const MCSection *TargetLoweringObjectFile::
Chris Lattnere53a6002009-07-29 05:09:30 +0000230SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
231 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000232 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
233 "Can only be used for global definitions");
234
Chris Lattner968ff112009-08-01 21:11:14 +0000235 SectionKind Kind = SectionKindForGlobal(GV, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000236
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 // Select section name.
238 if (GV->hasSection()) {
239 // If the target has special section hacks for specifically named globals,
240 // return them now.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000241 if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind))
Chris Lattnerf0144122009-07-28 03:13:23 +0000242 return TS;
243
244 // If the target has magic semantics for certain section names, make sure to
245 // pick up the flags. This allows the user to write things with attribute
246 // section and still get the appropriate section flags printed.
Chris Lattner968ff112009-08-01 21:11:14 +0000247 Kind = getKindForNamedSection(GV->getSection().c_str(), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000248
Chris Lattner968ff112009-08-01 21:11:14 +0000249 return getOrCreateSection(GV->getSection().c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000250 }
251
252
253 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000254 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000255}
256
257// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000258const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000259TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000260 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000261 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000262 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000263 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000264
Chris Lattnerf9650c02009-08-01 21:46:23 +0000265 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000266 return getTextSection();
267
Chris Lattner82458382009-08-01 21:56:13 +0000268 if (Kind.isBSS() && BSSSection != 0)
269 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000270
Chris Lattnerf9650c02009-08-01 21:46:23 +0000271 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000272 return ReadOnlySection;
273
274 return getDataSection();
275}
276
Chris Lattner83d77fa2009-08-01 23:46:12 +0000277/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000278/// specified size and relocation information, return a section that it
279/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000280const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000281TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000282 if (Kind.isReadOnly() && ReadOnlySection != 0)
283 return ReadOnlySection;
284
285 return DataSection;
286}
287
288
Chris Lattnera87dea42009-07-31 18:48:30 +0000289const MCSection *TargetLoweringObjectFile::
Chris Lattner968ff112009-08-01 21:11:14 +0000290getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnera87dea42009-07-31 18:48:30 +0000291 if (MCSection *S = Ctx->GetSection(Name))
292 return S;
Chris Lattner968ff112009-08-01 21:11:14 +0000293 return MCSection::Create(Name, isDirective, Kind, *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +0000294}
295
296
297
298//===----------------------------------------------------------------------===//
299// ELF
300//===----------------------------------------------------------------------===//
301
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000302void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
303 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000304 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000305 if (!HasCrazyBSS)
Chris Lattner27981192009-08-01 23:57:16 +0000306 BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000307 else
308 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
309 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000310 // FIXME2: this should just be handle by the section printer. We should get
311 // away from syntactic view of the sections and MCSection should just be a
312 // semantic view.
Chris Lattner27981192009-08-01 23:57:16 +0000313 BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000314
315
Chris Lattner27981192009-08-01 23:57:16 +0000316 TextSection = getOrCreateSection("\t.text", true, SectionKind::getText());
317 DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000318 ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000319 getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000320 TLSDataSection =
Chris Lattner27981192009-08-01 23:57:16 +0000321 getOrCreateSection("\t.tdata", false, SectionKind::getThreadData());
Chris Lattner3b24c012009-08-04 05:35:56 +0000322
Chris Lattner968ff112009-08-01 21:11:14 +0000323 TLSBSSSection = getOrCreateSection("\t.tbss", false,
Chris Lattner27981192009-08-01 23:57:16 +0000324 SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000325
326 DataRelSection = getOrCreateSection("\t.data.rel", false,
Chris Lattner27981192009-08-01 23:57:16 +0000327 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000328 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000329 SectionKind::getDataRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000330 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
Chris Lattner27981192009-08-01 23:57:16 +0000331 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000332 DataRelROLocalSection =
333 getOrCreateSection("\t.data.rel.ro.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000334 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000335
336 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
Chris Lattner27981192009-08-01 23:57:16 +0000337 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000338 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
Chris Lattner27981192009-08-01 23:57:16 +0000339 SectionKind::getMergeableConst8());
Chris Lattnerf0144122009-07-28 03:13:23 +0000340 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
Chris Lattner27981192009-08-01 23:57:16 +0000341 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000342
343 StaticCtorSection =
344 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
345 StaticDtorSection =
346 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000347
Chris Lattner18a4c162009-08-02 07:24:22 +0000348 // Exception Handling Sections.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000349
350 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
351 // it contains relocatable pointers. In PIC mode, this is probably a big
352 // runtime hit for C++ apps. Either the contents of the LSDA need to be
353 // adjusted or this should be a data section.
354 LSDASection =
355 getOrCreateSection(".gcc_except_table", false, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000356 EHFrameSection =
357 getOrCreateSection(".eh_frame", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000358
359 // Debug Info Sections.
360 DwarfAbbrevSection =
361 getOrCreateSection(".debug_abbrev", false, SectionKind::getMetadata());
362 DwarfInfoSection =
363 getOrCreateSection(".debug_info", false, SectionKind::getMetadata());
364 DwarfLineSection =
365 getOrCreateSection(".debug_line", false, SectionKind::getMetadata());
366 DwarfFrameSection =
367 getOrCreateSection(".debug_frame", false, SectionKind::getMetadata());
368 DwarfPubNamesSection =
369 getOrCreateSection(".debug_pubnames", false, SectionKind::getMetadata());
370 DwarfPubTypesSection =
371 getOrCreateSection(".debug_pubtypes", false, SectionKind::getMetadata());
372 DwarfStrSection =
373 getOrCreateSection(".debug_str", false, SectionKind::getMetadata());
374 DwarfLocSection =
375 getOrCreateSection(".debug_loc", false, SectionKind::getMetadata());
376 DwarfARangesSection =
377 getOrCreateSection(".debug_aranges", false, SectionKind::getMetadata());
378 DwarfRangesSection =
379 getOrCreateSection(".debug_ranges", false, SectionKind::getMetadata());
380 DwarfMacroInfoSection =
381 getOrCreateSection(".debug_macinfo", false, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000382}
383
384
Chris Lattner968ff112009-08-01 21:11:14 +0000385SectionKind TargetLoweringObjectFileELF::
386getKindForNamedSection(const char *Name, SectionKind K) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000387 if (Name[0] != '.') return K;
388
389 // Some lame default implementation based on some magic section names.
390 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
391 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
392 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
393 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000394 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000395
396 if (strcmp(Name, ".tdata") == 0 ||
397 strncmp(Name, ".tdata.", 7) == 0 ||
398 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
399 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000400 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000401
402 if (strcmp(Name, ".tbss") == 0 ||
403 strncmp(Name, ".tbss.", 6) == 0 ||
404 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
405 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000406 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000407
408 return K;
409}
410
411void TargetLoweringObjectFileELF::
412getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
413 Str.push_back(',');
414 Str.push_back('"');
415
416 if (!Kind.isMetadata())
417 Str.push_back('a');
418 if (Kind.isText())
419 Str.push_back('x');
420 if (Kind.isWriteable())
421 Str.push_back('w');
Chris Lattner3b24c012009-08-04 05:35:56 +0000422 if (Kind.isMergeable1ByteCString() ||
423 Kind.isMergeable2ByteCString() ||
424 Kind.isMergeable4ByteCString() ||
Chris Lattner82987bf2009-07-31 16:17:13 +0000425 Kind.isMergeableConst4() ||
426 Kind.isMergeableConst8() ||
427 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000428 Str.push_back('M');
Chris Lattner3b24c012009-08-04 05:35:56 +0000429 if (Kind.isMergeable1ByteCString() ||
430 Kind.isMergeable2ByteCString() ||
431 Kind.isMergeable4ByteCString())
Chris Lattnerf0144122009-07-28 03:13:23 +0000432 Str.push_back('S');
433 if (Kind.isThreadLocal())
434 Str.push_back('T');
435
436 Str.push_back('"');
437 Str.push_back(',');
438
439 // If comment string is '@', e.g. as on ARM - use '%' instead
440 if (AtIsCommentChar)
441 Str.push_back('%');
442 else
443 Str.push_back('@');
444
445 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000446 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000447 KindStr = "nobits";
448 else
449 KindStr = "progbits";
450
451 Str.append(KindStr, KindStr+strlen(KindStr));
452
Chris Lattner3b24c012009-08-04 05:35:56 +0000453 if (Kind.isMergeable1ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000454 Str.push_back(',');
455 Str.push_back('1');
Chris Lattner3b24c012009-08-04 05:35:56 +0000456 } else if (Kind.isMergeable2ByteCString()) {
457 Str.push_back(',');
458 Str.push_back('2');
459 } else if (Kind.isMergeable4ByteCString()) {
460 Str.push_back(',');
461 Str.push_back('4');
Chris Lattnerf0144122009-07-28 03:13:23 +0000462 } else if (Kind.isMergeableConst4()) {
463 Str.push_back(',');
464 Str.push_back('4');
465 } else if (Kind.isMergeableConst8()) {
466 Str.push_back(',');
467 Str.push_back('8');
468 } else if (Kind.isMergeableConst16()) {
469 Str.push_back(',');
470 Str.push_back('1');
471 Str.push_back('6');
472 }
473}
474
475
476static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
477 if (Kind.isText()) return ".gnu.linkonce.t.";
478 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
479
480 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
481 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
482
483 if (Kind.isBSS()) return ".gnu.linkonce.b.";
484 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
485 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
486 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
487 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
488
489 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
490 return ".gnu.linkonce.d.rel.ro.";
491}
492
Chris Lattnera87dea42009-07-31 18:48:30 +0000493const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000494SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000495 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000496
497 // If this global is linkonce/weak and the target handles this by emitting it
498 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000499 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000500 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000501 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000502 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000503 }
504
Chris Lattnerf9650c02009-08-01 21:46:23 +0000505 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000506
Chris Lattner3b24c012009-08-04 05:35:56 +0000507 if (Kind.isMergeable1ByteCString() ||
508 Kind.isMergeable2ByteCString() ||
509 Kind.isMergeable4ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000510
Chris Lattner067fe1a2009-07-29 04:54:38 +0000511 // We also need alignment here.
512 // FIXME: this is getting the alignment of the character, not the
513 // alignment of the global!
514 unsigned Align =
515 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000516
Chris Lattner7e88a502009-08-04 16:19:50 +0000517 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000518 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000519 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000520 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000521 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000522 else
523 assert(Kind.isMergeable1ByteCString() && "unknown string width");
524
525
Chris Lattner7e88a502009-08-04 16:19:50 +0000526 std::string Name = SizeSpec + utostr(Align);
Chris Lattner3b24c012009-08-04 05:35:56 +0000527 return getOrCreateSection(Name.c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000528 }
529
Chris Lattnerf9650c02009-08-01 21:46:23 +0000530 if (Kind.isMergeableConst()) {
531 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000532 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000533 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000534 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000535 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000536 return MergeableConst16Section;
537 return ReadOnlySection; // .const
538 }
539
Chris Lattnerf9650c02009-08-01 21:46:23 +0000540 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000541
Chris Lattnerf9650c02009-08-01 21:46:23 +0000542 if (Kind.isThreadData()) return TLSDataSection;
543 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000544
Chris Lattner82458382009-08-01 21:56:13 +0000545 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000546
Chris Lattnerf9650c02009-08-01 21:46:23 +0000547 if (Kind.isDataNoRel()) return DataSection;
548 if (Kind.isDataRelLocal()) return DataRelLocalSection;
549 if (Kind.isDataRel()) return DataRelSection;
550 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000551
Chris Lattnerf9650c02009-08-01 21:46:23 +0000552 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000553 return DataRelROSection;
554}
555
Chris Lattner83d77fa2009-08-01 23:46:12 +0000556/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000557/// specified size and relocation information, return a section that it
558/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000559const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000560getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000561 if (Kind.isMergeableConst4())
562 return MergeableConst4Section;
563 if (Kind.isMergeableConst8())
564 return MergeableConst8Section;
565 if (Kind.isMergeableConst16())
566 return MergeableConst16Section;
567 if (Kind.isReadOnly())
568 return ReadOnlySection;
569
570 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
571 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
572 return DataRelROSection;
573}
574
575//===----------------------------------------------------------------------===//
576// MachO
577//===----------------------------------------------------------------------===//
578
Chris Lattner11e96572009-08-03 21:53:27 +0000579const MCSection *TargetLoweringObjectFileMachO::
580getMachOSection(const char *Name, bool isDirective, SectionKind K) {
581 // FOR NOW, Just forward.
582 return getOrCreateSection(Name, isDirective, K);
583}
584
585
586
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000587void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
588 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000589 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000590 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000591 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000592 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000593 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000594
Chris Lattner82458382009-08-01 21:56:13 +0000595 CStringSection = getOrCreateSection("\t.cstring", true,
Chris Lattnerec409752009-08-04 16:27:13 +0000596 SectionKind::getMergeable1ByteCString());
597 UStringSection = getOrCreateSection("__TEXT,__ustring", false,
598 SectionKind::getMergeable2ByteCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000599 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
Chris Lattnerec409752009-08-04 16:27:13 +0000600 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000601 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
Chris Lattnerec409752009-08-04 16:27:13 +0000602 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000603
604 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
605 // to using it in -static mode.
606 if (TM.getRelocationModel() != Reloc::Static &&
607 TM.getTargetData()->getPointerSize() == 32)
608 SixteenByteConstantSection =
Chris Lattner968ff112009-08-01 21:11:14 +0000609 getOrCreateSection("\t.literal16\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000610 SectionKind::getMergeableConst16());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000611 else
612 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000613
Chris Lattner968ff112009-08-01 21:11:14 +0000614 ReadOnlySection = getOrCreateSection("\t.const", true,
Chris Lattner27981192009-08-01 23:57:16 +0000615 SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000616
617 TextCoalSection =
618 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Chris Lattner27981192009-08-01 23:57:16 +0000619 false, SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000620 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000621 false,
Chris Lattner27981192009-08-01 23:57:16 +0000622 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000623 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000624 false,
Chris Lattner27981192009-08-01 23:57:16 +0000625 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000626 ConstDataSection = getOrCreateSection("\t.const_data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000627 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000628 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000629 false,
Chris Lattner27981192009-08-01 23:57:16 +0000630 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000631
632 if (TM.getRelocationModel() == Reloc::Static) {
633 StaticCtorSection =
634 getOrCreateSection(".constructor", true, SectionKind::getDataRel());
635 StaticDtorSection =
636 getOrCreateSection(".destructor", true, SectionKind::getDataRel());
637 } else {
638 StaticCtorSection =
639 getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel());
640 StaticDtorSection =
641 getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel());
642 }
643
Chris Lattner18a4c162009-08-02 07:24:22 +0000644 // Exception Handling.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000645 LSDASection = getOrCreateSection("__DATA,__gcc_except_tab", false,
646 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000647 EHFrameSection =
648 getOrCreateSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
649 "+live_support", false, SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000650
651 // Debug Information.
652 // FIXME: Don't use 'directive' syntax: need flags for debug/regular??
653 // FIXME: Need __DWARF segment.
654 DwarfAbbrevSection =
655 getOrCreateSection(".section __DWARF,__debug_abbrev,regular,debug", true,
656 SectionKind::getMetadata());
657 DwarfInfoSection =
658 getOrCreateSection(".section __DWARF,__debug_info,regular,debug", true,
659 SectionKind::getMetadata());
660 DwarfLineSection =
661 getOrCreateSection(".section __DWARF,__debug_line,regular,debug", true,
662 SectionKind::getMetadata());
663 DwarfFrameSection =
664 getOrCreateSection(".section __DWARF,__debug_frame,regular,debug", true,
665 SectionKind::getMetadata());
666 DwarfPubNamesSection =
667 getOrCreateSection(".section __DWARF,__debug_pubnames,regular,debug", true,
668 SectionKind::getMetadata());
669 DwarfPubTypesSection =
670 getOrCreateSection(".section __DWARF,__debug_pubtypes,regular,debug", true,
671 SectionKind::getMetadata());
672 DwarfStrSection =
673 getOrCreateSection(".section __DWARF,__debug_str,regular,debug", true,
674 SectionKind::getMetadata());
675 DwarfLocSection =
676 getOrCreateSection(".section __DWARF,__debug_loc,regular,debug", true,
677 SectionKind::getMetadata());
678 DwarfARangesSection =
679 getOrCreateSection(".section __DWARF,__debug_aranges,regular,debug", true,
680 SectionKind::getMetadata());
681 DwarfRangesSection =
682 getOrCreateSection(".section __DWARF,__debug_ranges,regular,debug", true,
683 SectionKind::getMetadata());
684 DwarfMacroInfoSection =
685 getOrCreateSection(".section __DWARF,__debug_macinfo,regular,debug", true,
686 SectionKind::getMetadata());
687 DwarfDebugInlineSection =
688 getOrCreateSection(".section __DWARF,__debug_inlined,regular,debug", true,
689 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000690}
691
Chris Lattnera87dea42009-07-31 18:48:30 +0000692const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000693SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000694 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000695 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000696
Chris Lattnerf9650c02009-08-01 21:46:23 +0000697 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000698 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000699
700 // If this is weak/linkonce, put this in a coalescable section, either in text
701 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000702 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000703 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000704 return ConstTextCoalSection;
705 return DataCoalSection;
706 }
707
708 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000709 if (Kind.isMergeable1ByteCString() ||
710 Kind.isMergeable2ByteCString()) {
711 if (TM.getTargetData()->getPreferredAlignment(
712 cast<GlobalVariable>(GV)) < 32) {
713 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000714 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000715 assert(Kind.isMergeable2ByteCString());
716 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000717 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000718 }
719
Chris Lattnerf9650c02009-08-01 21:46:23 +0000720 if (Kind.isMergeableConst()) {
721 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000722 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000723 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000724 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000725 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000726 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000727 }
Chris Lattnerec409752009-08-04 16:27:13 +0000728
729 // Otherwise, if it is readonly, but not something we can specially optimize,
730 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000731 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000732 return ReadOnlySection;
733
734 // If this is marked const, put it into a const section. But if the dynamic
735 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000736 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000737 return ConstDataSection;
738
739 // Otherwise, just drop the variable in the normal data section.
740 return DataSection;
741}
742
Chris Lattnera87dea42009-07-31 18:48:30 +0000743const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000744TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000745 // If this constant requires a relocation, we have to put it in the data
746 // segment, not in the text segment.
747 if (Kind.isDataRel())
748 return ConstDataSection;
749
750 if (Kind.isMergeableConst4())
751 return FourByteConstantSection;
752 if (Kind.isMergeableConst8())
753 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000754 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000755 return SixteenByteConstantSection;
756 return ReadOnlySection; // .const
757}
758
Chris Lattner26630c12009-07-31 20:52:39 +0000759/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
760/// not to emit the UsedDirective for some symbols in llvm.used.
761// FIXME: REMOVE this (rdar://7071300)
762bool TargetLoweringObjectFileMachO::
763shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
764 /// On Darwin, internally linked data beginning with "L" or "l" does not have
765 /// the directive emitted (this occurs in ObjC metadata).
766 if (!GV) return false;
767
768 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
769 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
770 // FIXME: ObjC metadata is currently emitted as internal symbols that have
771 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
772 // this horrible hack can go away.
773 const std::string &Name = Mang->getMangledName(GV);
774 if (Name[0] == 'L' || Name[0] == 'l')
775 return false;
776 }
777
778 return true;
779}
780
781
Chris Lattnerf0144122009-07-28 03:13:23 +0000782//===----------------------------------------------------------------------===//
783// COFF
784//===----------------------------------------------------------------------===//
785
Chris Lattner11e96572009-08-03 21:53:27 +0000786const MCSection *TargetLoweringObjectFileCOFF::
787getCOFFSection(const char *Name, bool isDirective, SectionKind K) {
788 return getOrCreateSection(Name, isDirective, K);
789}
790
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000791void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
792 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000793 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000794 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000795 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000796 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000797 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000798 StaticCtorSection =
799 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
800 StaticDtorSection =
801 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000802
803
804 // Debug info.
805 // FIXME: Don't use 'directive' mode here.
806 DwarfAbbrevSection =
807 getOrCreateSection("\t.section\t.debug_abbrev,\"dr\"",
808 true, SectionKind::getMetadata());
809 DwarfInfoSection =
810 getOrCreateSection("\t.section\t.debug_info,\"dr\"",
811 true, SectionKind::getMetadata());
812 DwarfLineSection =
813 getOrCreateSection("\t.section\t.debug_line,\"dr\"",
814 true, SectionKind::getMetadata());
815 DwarfFrameSection =
816 getOrCreateSection("\t.section\t.debug_frame,\"dr\"",
817 true, SectionKind::getMetadata());
818 DwarfPubNamesSection =
819 getOrCreateSection("\t.section\t.debug_pubnames,\"dr\"",
820 true, SectionKind::getMetadata());
821 DwarfPubTypesSection =
822 getOrCreateSection("\t.section\t.debug_pubtypes,\"dr\"",
823 true, SectionKind::getMetadata());
824 DwarfStrSection =
825 getOrCreateSection("\t.section\t.debug_str,\"dr\"",
826 true, SectionKind::getMetadata());
827 DwarfLocSection =
828 getOrCreateSection("\t.section\t.debug_loc,\"dr\"",
829 true, SectionKind::getMetadata());
830 DwarfARangesSection =
831 getOrCreateSection("\t.section\t.debug_aranges,\"dr\"",
832 true, SectionKind::getMetadata());
833 DwarfRangesSection =
834 getOrCreateSection("\t.section\t.debug_ranges,\"dr\"",
835 true, SectionKind::getMetadata());
836 DwarfMacroInfoSection =
837 getOrCreateSection("\t.section\t.debug_macinfo,\"dr\"",
838 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000839}
840
841void TargetLoweringObjectFileCOFF::
842getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
843 // FIXME: Inefficient.
844 std::string Res = ",\"";
845 if (Kind.isText())
846 Res += 'x';
847 if (Kind.isWriteable())
848 Res += 'w';
849 Res += "\"";
850
851 Str.append(Res.begin(), Res.end());
852}
853
854static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
855 if (Kind.isText())
856 return ".text$linkonce";
857 if (Kind.isWriteable())
858 return ".data$linkonce";
859 return ".rdata$linkonce";
860}
861
862
Chris Lattnera87dea42009-07-31 18:48:30 +0000863const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000864SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000865 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000866 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000867
868 // If this global is linkonce/weak and the target handles this by emitting it
869 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000870 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000871 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000872 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000873 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000874 }
875
Chris Lattnerf9650c02009-08-01 21:46:23 +0000876 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000877 return getTextSection();
878
Chris Lattnerf0144122009-07-28 03:13:23 +0000879 return getDataSection();
880}
881