blob: 38ae71e67d6549226e95ab0aefd4a33abb82dca0 [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 Lattnerf0144122009-07-28 03:13:23 +000041}
42
43TargetLoweringObjectFile::~TargetLoweringObjectFile() {
44}
45
46static bool isSuitableForBSS(const GlobalVariable *GV) {
47 Constant *C = GV->getInitializer();
48
49 // Must have zero initializer.
50 if (!C->isNullValue())
51 return false;
52
53 // Leave constant zeros in readonly constant sections, so they can be shared.
54 if (GV->isConstant())
55 return false;
56
57 // If the global has an explicit section specified, don't put it in BSS.
58 if (!GV->getSection().empty())
59 return false;
60
61 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
62 if (NoZerosInBSS)
63 return false;
64
65 // Otherwise, put it in BSS!
66 return true;
67}
68
69static bool isConstantString(const Constant *C) {
70 // First check: is we have constant array of i8 terminated with zero
71 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
72 // Check, if initializer is a null-terminated string
73 if (CVA && CVA->isCString())
74 return true;
75
76 // Another possibility: [1 x i8] zeroinitializer
77 if (isa<ConstantAggregateZero>(C))
78 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
79 return (Ty->getElementType() == Type::Int8Ty &&
80 Ty->getNumElements() == 1);
81
82 return false;
83}
84
Chris Lattner968ff112009-08-01 21:11:14 +000085/// SectionKindForGlobal - This is a top-level target-independent classifier for
86/// a global variable. Given an global variable and information from TM, it
87/// classifies the global in a variety of ways that make various target
88/// implementations simpler. The target implementation is free to ignore this
89/// extra info of course.
90static SectionKind SectionKindForGlobal(const GlobalValue *GV,
91 const TargetMachine &TM) {
Chris Lattnerf0144122009-07-28 03:13:23 +000092 Reloc::Model ReloModel = TM.getRelocationModel();
93
94 // Early exit - functions should be always in text sections.
95 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
96 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +000097 return SectionKind::getText();
Chris Lattnerf0144122009-07-28 03:13:23 +000098
99
100 // Handle thread-local data first.
101 if (GVar->isThreadLocal()) {
102 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000103 return SectionKind::getThreadBSS();
104 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000105 }
106
107 // Variable can be easily put to BSS section.
108 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000109 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000110
111 Constant *C = GVar->getInitializer();
112
113 // If the global is marked constant, we can put it into a mergable section,
114 // a mergable string section, or general .data if it contains relocations.
115 if (GVar->isConstant()) {
116 // If the initializer for the global contains something that requires a
117 // relocation, then we may have to drop this into a wriable data section
118 // even though it is marked const.
119 switch (C->getRelocationInfo()) {
120 default: llvm_unreachable("unknown relocation info kind");
121 case Constant::NoRelocation:
122 // If initializer is a null-terminated string, put it in a "cstring"
123 // section if the target has it.
124 if (isConstantString(C))
Chris Lattner27981192009-08-01 23:57:16 +0000125 return SectionKind::getMergeableCString();
Chris Lattnerf0144122009-07-28 03:13:23 +0000126
127 // Otherwise, just drop it into a mergable constant section. If we have
128 // a section for this size, use it, otherwise use the arbitrary sized
129 // mergable section.
130 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000131 case 4: return SectionKind::getMergeableConst4();
132 case 8: return SectionKind::getMergeableConst8();
133 case 16: return SectionKind::getMergeableConst16();
134 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000135 }
136
137 case Constant::LocalRelocation:
138 // In static relocation model, the linker will resolve all addresses, so
139 // the relocation entries will actually be constants by the time the app
140 // starts up. However, we can't put this into a mergable section, because
141 // the linker doesn't take relocations into consideration when it tries to
142 // merge entries in the section.
143 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000144 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000145
146 // Otherwise, the dynamic linker needs to fix it up, put it in the
147 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000148 return SectionKind::getReadOnlyWithRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000149
150 case Constant::GlobalRelocations:
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 section.
Chris Lattner27981192009-08-01 23:57:16 +0000161 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000162 }
163 }
164
165 // Okay, this isn't a constant. If the initializer for the global is going
166 // to require a runtime relocation by the dynamic linker, put it into a more
167 // specific section to improve startup time of the app. This coalesces these
168 // globals together onto fewer pages, improving the locality of the dynamic
169 // linker.
170 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000171 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000172
173 switch (C->getRelocationInfo()) {
174 default: llvm_unreachable("unknown relocation info kind");
175 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000176 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000177 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000178 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000179 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000180 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000181 }
182}
183
184/// SectionForGlobal - This method computes the appropriate section to emit
185/// the specified global variable or function definition. This should not
186/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000187const MCSection *TargetLoweringObjectFile::
Chris Lattnere53a6002009-07-29 05:09:30 +0000188SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
189 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000190 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
191 "Can only be used for global definitions");
192
Chris Lattner968ff112009-08-01 21:11:14 +0000193 SectionKind Kind = SectionKindForGlobal(GV, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000194
Chris Lattnerf0144122009-07-28 03:13:23 +0000195 // Select section name.
196 if (GV->hasSection()) {
197 // If the target has special section hacks for specifically named globals,
198 // return them now.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000199 if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind))
Chris Lattnerf0144122009-07-28 03:13:23 +0000200 return TS;
201
202 // If the target has magic semantics for certain section names, make sure to
203 // pick up the flags. This allows the user to write things with attribute
204 // section and still get the appropriate section flags printed.
Chris Lattner968ff112009-08-01 21:11:14 +0000205 Kind = getKindForNamedSection(GV->getSection().c_str(), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000206
Chris Lattner968ff112009-08-01 21:11:14 +0000207 return getOrCreateSection(GV->getSection().c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000208 }
209
210
211 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000212 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000213}
214
215// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000216const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000217TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000218 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000219 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000220 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000221 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000222
Chris Lattnerf9650c02009-08-01 21:46:23 +0000223 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000224 return getTextSection();
225
Chris Lattner82458382009-08-01 21:56:13 +0000226 if (Kind.isBSS() && BSSSection != 0)
227 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000228
Chris Lattnerf9650c02009-08-01 21:46:23 +0000229 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000230 return ReadOnlySection;
231
232 return getDataSection();
233}
234
Chris Lattner83d77fa2009-08-01 23:46:12 +0000235/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000236/// specified size and relocation information, return a section that it
237/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000238const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000239TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000240 if (Kind.isReadOnly() && ReadOnlySection != 0)
241 return ReadOnlySection;
242
243 return DataSection;
244}
245
246
Chris Lattnera87dea42009-07-31 18:48:30 +0000247const MCSection *TargetLoweringObjectFile::
Chris Lattner968ff112009-08-01 21:11:14 +0000248getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnera87dea42009-07-31 18:48:30 +0000249 if (MCSection *S = Ctx->GetSection(Name))
250 return S;
Chris Lattner968ff112009-08-01 21:11:14 +0000251 return MCSection::Create(Name, isDirective, Kind, *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +0000252}
253
254
255
256//===----------------------------------------------------------------------===//
257// ELF
258//===----------------------------------------------------------------------===//
259
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000260void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
261 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000262 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000263 if (!HasCrazyBSS)
Chris Lattner27981192009-08-01 23:57:16 +0000264 BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000265 else
266 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
267 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000268 // FIXME2: this should just be handle by the section printer. We should get
269 // away from syntactic view of the sections and MCSection should just be a
270 // semantic view.
Chris Lattner27981192009-08-01 23:57:16 +0000271 BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000272
273
Chris Lattner27981192009-08-01 23:57:16 +0000274 TextSection = getOrCreateSection("\t.text", true, SectionKind::getText());
275 DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000276 ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000277 getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000278 TLSDataSection =
Chris Lattner27981192009-08-01 23:57:16 +0000279 getOrCreateSection("\t.tdata", false, SectionKind::getThreadData());
Chris Lattner82458382009-08-01 21:56:13 +0000280 CStringSection = getOrCreateSection("\t.rodata.str", true,
Chris Lattner27981192009-08-01 23:57:16 +0000281 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000282
Chris Lattner968ff112009-08-01 21:11:14 +0000283 TLSBSSSection = getOrCreateSection("\t.tbss", false,
Chris Lattner27981192009-08-01 23:57:16 +0000284 SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000285
286 DataRelSection = getOrCreateSection("\t.data.rel", false,
Chris Lattner27981192009-08-01 23:57:16 +0000287 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000288 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000289 SectionKind::getDataRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000290 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
Chris Lattner27981192009-08-01 23:57:16 +0000291 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000292 DataRelROLocalSection =
293 getOrCreateSection("\t.data.rel.ro.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000294 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000295
296 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
Chris Lattner27981192009-08-01 23:57:16 +0000297 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000298 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
Chris Lattner27981192009-08-01 23:57:16 +0000299 SectionKind::getMergeableConst8());
Chris Lattnerf0144122009-07-28 03:13:23 +0000300 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
Chris Lattner27981192009-08-01 23:57:16 +0000301 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000302
303 StaticCtorSection =
304 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
305 StaticDtorSection =
306 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000307
308
309 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
310 // it contains relocatable pointers. In PIC mode, this is probably a big
311 // runtime hit for C++ apps. Either the contents of the LSDA need to be
312 // adjusted or this should be a data section.
313 LSDASection =
314 getOrCreateSection(".gcc_except_table", false, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000315 EHFrameSection =
316 getOrCreateSection(".eh_frame", false, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000317}
318
319
Chris Lattner968ff112009-08-01 21:11:14 +0000320SectionKind TargetLoweringObjectFileELF::
321getKindForNamedSection(const char *Name, SectionKind K) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000322 if (Name[0] != '.') return K;
323
324 // Some lame default implementation based on some magic section names.
325 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
326 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
327 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
328 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000329 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000330
331 if (strcmp(Name, ".tdata") == 0 ||
332 strncmp(Name, ".tdata.", 7) == 0 ||
333 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
334 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000335 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000336
337 if (strcmp(Name, ".tbss") == 0 ||
338 strncmp(Name, ".tbss.", 6) == 0 ||
339 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
340 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000341 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000342
343 return K;
344}
345
346void TargetLoweringObjectFileELF::
347getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
348 Str.push_back(',');
349 Str.push_back('"');
350
351 if (!Kind.isMetadata())
352 Str.push_back('a');
353 if (Kind.isText())
354 Str.push_back('x');
355 if (Kind.isWriteable())
356 Str.push_back('w');
Chris Lattner82987bf2009-07-31 16:17:13 +0000357 if (Kind.isMergeableCString() ||
358 Kind.isMergeableConst4() ||
359 Kind.isMergeableConst8() ||
360 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000361 Str.push_back('M');
362 if (Kind.isMergeableCString())
363 Str.push_back('S');
364 if (Kind.isThreadLocal())
365 Str.push_back('T');
366
367 Str.push_back('"');
368 Str.push_back(',');
369
370 // If comment string is '@', e.g. as on ARM - use '%' instead
371 if (AtIsCommentChar)
372 Str.push_back('%');
373 else
374 Str.push_back('@');
375
376 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000377 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000378 KindStr = "nobits";
379 else
380 KindStr = "progbits";
381
382 Str.append(KindStr, KindStr+strlen(KindStr));
383
384 if (Kind.isMergeableCString()) {
385 // TODO: Eventually handle multiple byte character strings. For now, all
386 // mergable C strings are single byte.
387 Str.push_back(',');
388 Str.push_back('1');
389 } else if (Kind.isMergeableConst4()) {
390 Str.push_back(',');
391 Str.push_back('4');
392 } else if (Kind.isMergeableConst8()) {
393 Str.push_back(',');
394 Str.push_back('8');
395 } else if (Kind.isMergeableConst16()) {
396 Str.push_back(',');
397 Str.push_back('1');
398 Str.push_back('6');
399 }
400}
401
402
403static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
404 if (Kind.isText()) return ".gnu.linkonce.t.";
405 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
406
407 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
408 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
409
410 if (Kind.isBSS()) return ".gnu.linkonce.b.";
411 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
412 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
413 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
414 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
415
416 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
417 return ".gnu.linkonce.d.rel.ro.";
418}
419
Chris Lattnera87dea42009-07-31 18:48:30 +0000420const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000421SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000422 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000423
424 // If this global is linkonce/weak and the target handles this by emitting it
425 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000426 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000427 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000428 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000429 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000430 }
431
Chris Lattnerf9650c02009-08-01 21:46:23 +0000432 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000433
Chris Lattnerf9650c02009-08-01 21:46:23 +0000434 if (Kind.isMergeableCString()) {
Chris Lattner82458382009-08-01 21:56:13 +0000435 assert(CStringSection && "Should have string section prefix");
Chris Lattnerf0144122009-07-28 03:13:23 +0000436
Chris Lattner067fe1a2009-07-29 04:54:38 +0000437 // We also need alignment here.
438 // FIXME: this is getting the alignment of the character, not the
439 // alignment of the global!
440 unsigned Align =
441 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000442
Chris Lattner82458382009-08-01 21:56:13 +0000443 std::string Name = CStringSection->getName() + "1." + utostr(Align);
Chris Lattner067fe1a2009-07-29 04:54:38 +0000444 return getOrCreateSection(Name.c_str(), false,
Chris Lattner27981192009-08-01 23:57:16 +0000445 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000446 }
447
Chris Lattnerf9650c02009-08-01 21:46:23 +0000448 if (Kind.isMergeableConst()) {
449 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000450 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000451 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000452 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000453 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000454 return MergeableConst16Section;
455 return ReadOnlySection; // .const
456 }
457
Chris Lattnerf9650c02009-08-01 21:46:23 +0000458 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000459
Chris Lattnerf9650c02009-08-01 21:46:23 +0000460 if (Kind.isThreadData()) return TLSDataSection;
461 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000462
Chris Lattner82458382009-08-01 21:56:13 +0000463 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000464
Chris Lattnerf9650c02009-08-01 21:46:23 +0000465 if (Kind.isDataNoRel()) return DataSection;
466 if (Kind.isDataRelLocal()) return DataRelLocalSection;
467 if (Kind.isDataRel()) return DataRelSection;
468 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000469
Chris Lattnerf9650c02009-08-01 21:46:23 +0000470 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000471 return DataRelROSection;
472}
473
Chris Lattner83d77fa2009-08-01 23:46:12 +0000474/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000475/// specified size and relocation information, return a section that it
476/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000477const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000478getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000479 if (Kind.isMergeableConst4())
480 return MergeableConst4Section;
481 if (Kind.isMergeableConst8())
482 return MergeableConst8Section;
483 if (Kind.isMergeableConst16())
484 return MergeableConst16Section;
485 if (Kind.isReadOnly())
486 return ReadOnlySection;
487
488 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
489 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
490 return DataRelROSection;
491}
492
493//===----------------------------------------------------------------------===//
494// MachO
495//===----------------------------------------------------------------------===//
496
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000497void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
498 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000499 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000500 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000501 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000502 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000503 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000504
Chris Lattner82458382009-08-01 21:56:13 +0000505 CStringSection = getOrCreateSection("\t.cstring", true,
Chris Lattner27981192009-08-01 23:57:16 +0000506 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000507 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000508 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000509 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000510 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000511
512 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
513 // to using it in -static mode.
514 if (TM.getRelocationModel() != Reloc::Static &&
515 TM.getTargetData()->getPointerSize() == 32)
516 SixteenByteConstantSection =
Chris Lattner968ff112009-08-01 21:11:14 +0000517 getOrCreateSection("\t.literal16\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000518 SectionKind::getMergeableConst16());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000519 else
520 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000521
Chris Lattner968ff112009-08-01 21:11:14 +0000522 ReadOnlySection = getOrCreateSection("\t.const", true,
Chris Lattner27981192009-08-01 23:57:16 +0000523 SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000524
525 TextCoalSection =
526 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Chris Lattner27981192009-08-01 23:57:16 +0000527 false, SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000528 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000529 false,
Chris Lattner27981192009-08-01 23:57:16 +0000530 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000531 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000532 false,
Chris Lattner27981192009-08-01 23:57:16 +0000533 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000534 ConstDataSection = getOrCreateSection("\t.const_data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000535 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000536 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000537 false,
Chris Lattner27981192009-08-01 23:57:16 +0000538 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000539
540 if (TM.getRelocationModel() == Reloc::Static) {
541 StaticCtorSection =
542 getOrCreateSection(".constructor", true, SectionKind::getDataRel());
543 StaticDtorSection =
544 getOrCreateSection(".destructor", true, SectionKind::getDataRel());
545 } else {
546 StaticCtorSection =
547 getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel());
548 StaticDtorSection =
549 getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel());
550 }
551
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000552 LSDASection = getOrCreateSection("__DATA,__gcc_except_tab", false,
553 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000554 EHFrameSection =
555 getOrCreateSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
556 "+live_support", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000557}
558
Chris Lattnera87dea42009-07-31 18:48:30 +0000559const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000560SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000561 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000562 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000563
Chris Lattnerf9650c02009-08-01 21:46:23 +0000564 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000565 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000566
567 // If this is weak/linkonce, put this in a coalescable section, either in text
568 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000569 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000570 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000571 return ConstTextCoalSection;
572 return DataCoalSection;
573 }
574
575 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000576 if (Kind.isMergeableCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000577 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
578 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
579 const TargetData &TD = *TM.getTargetData();
580 unsigned Size = TD.getTypeAllocSize(Ty);
581 if (Size) {
582 unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV));
583 if (Align <= 32)
Chris Lattner82458382009-08-01 21:56:13 +0000584 return CStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000585 }
586
587 return ReadOnlySection;
588 }
589
Chris Lattnerf9650c02009-08-01 21:46:23 +0000590 if (Kind.isMergeableConst()) {
591 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000592 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000593 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000594 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000595 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000596 return SixteenByteConstantSection;
597 return ReadOnlySection; // .const
598 }
599
600 // FIXME: ROData -> const in -static mode that is relocatable but they happen
601 // by the static linker. Why not mergeable?
Chris Lattnerf9650c02009-08-01 21:46:23 +0000602 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000603 return ReadOnlySection;
604
605 // If this is marked const, put it into a const section. But if the dynamic
606 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000607 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000608 return ConstDataSection;
609
610 // Otherwise, just drop the variable in the normal data section.
611 return DataSection;
612}
613
Chris Lattnera87dea42009-07-31 18:48:30 +0000614const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000615TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000616 // If this constant requires a relocation, we have to put it in the data
617 // segment, not in the text segment.
618 if (Kind.isDataRel())
619 return ConstDataSection;
620
621 if (Kind.isMergeableConst4())
622 return FourByteConstantSection;
623 if (Kind.isMergeableConst8())
624 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000625 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000626 return SixteenByteConstantSection;
627 return ReadOnlySection; // .const
628}
629
Chris Lattner26630c12009-07-31 20:52:39 +0000630/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
631/// not to emit the UsedDirective for some symbols in llvm.used.
632// FIXME: REMOVE this (rdar://7071300)
633bool TargetLoweringObjectFileMachO::
634shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
635 /// On Darwin, internally linked data beginning with "L" or "l" does not have
636 /// the directive emitted (this occurs in ObjC metadata).
637 if (!GV) return false;
638
639 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
640 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
641 // FIXME: ObjC metadata is currently emitted as internal symbols that have
642 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
643 // this horrible hack can go away.
644 const std::string &Name = Mang->getMangledName(GV);
645 if (Name[0] == 'L' || Name[0] == 'l')
646 return false;
647 }
648
649 return true;
650}
651
652
Chris Lattnerf0144122009-07-28 03:13:23 +0000653//===----------------------------------------------------------------------===//
654// COFF
655//===----------------------------------------------------------------------===//
656
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000657void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
658 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000659 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000660 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000661 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000662 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000663 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000664 StaticCtorSection =
665 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
666 StaticDtorSection =
667 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000668}
669
670void TargetLoweringObjectFileCOFF::
671getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
672 // FIXME: Inefficient.
673 std::string Res = ",\"";
674 if (Kind.isText())
675 Res += 'x';
676 if (Kind.isWriteable())
677 Res += 'w';
678 Res += "\"";
679
680 Str.append(Res.begin(), Res.end());
681}
682
683static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
684 if (Kind.isText())
685 return ".text$linkonce";
686 if (Kind.isWriteable())
687 return ".data$linkonce";
688 return ".rdata$linkonce";
689}
690
691
Chris Lattnera87dea42009-07-31 18:48:30 +0000692const MCSection *TargetLoweringObjectFileCOFF::
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() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000696
697 // If this global is linkonce/weak and the target handles this by emitting it
698 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000699 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000700 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000701 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000702 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000703 }
704
Chris Lattnerf9650c02009-08-01 21:46:23 +0000705 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000706 return getTextSection();
707
Chris Lattnerf0144122009-07-28 03:13:23 +0000708 return getDataSection();
709}
710