blob: 7c5eaf9dbffbd498e142fadaed83d2b0ad75b6b7 [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 Lattnerf0144122009-07-28 03:13:23 +000037}
38
39TargetLoweringObjectFile::~TargetLoweringObjectFile() {
40}
41
42static bool isSuitableForBSS(const GlobalVariable *GV) {
43 Constant *C = GV->getInitializer();
44
45 // Must have zero initializer.
46 if (!C->isNullValue())
47 return false;
48
49 // Leave constant zeros in readonly constant sections, so they can be shared.
50 if (GV->isConstant())
51 return false;
52
53 // If the global has an explicit section specified, don't put it in BSS.
54 if (!GV->getSection().empty())
55 return false;
56
57 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
58 if (NoZerosInBSS)
59 return false;
60
61 // Otherwise, put it in BSS!
62 return true;
63}
64
65static bool isConstantString(const Constant *C) {
66 // First check: is we have constant array of i8 terminated with zero
67 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
68 // Check, if initializer is a null-terminated string
69 if (CVA && CVA->isCString())
70 return true;
71
72 // Another possibility: [1 x i8] zeroinitializer
73 if (isa<ConstantAggregateZero>(C))
74 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
75 return (Ty->getElementType() == Type::Int8Ty &&
76 Ty->getNumElements() == 1);
77
78 return false;
79}
80
Chris Lattner968ff112009-08-01 21:11:14 +000081/// SectionKindForGlobal - This is a top-level target-independent classifier for
82/// a global variable. Given an global variable and information from TM, it
83/// classifies the global in a variety of ways that make various target
84/// implementations simpler. The target implementation is free to ignore this
85/// extra info of course.
86static SectionKind SectionKindForGlobal(const GlobalValue *GV,
87 const TargetMachine &TM) {
Chris Lattnerf0144122009-07-28 03:13:23 +000088 Reloc::Model ReloModel = TM.getRelocationModel();
89
90 // Early exit - functions should be always in text sections.
91 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
92 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +000093 return SectionKind::getText();
Chris Lattnerf0144122009-07-28 03:13:23 +000094
95
96 // Handle thread-local data first.
97 if (GVar->isThreadLocal()) {
98 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +000099 return SectionKind::getThreadBSS();
100 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000101 }
102
103 // Variable can be easily put to BSS section.
104 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000105 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000106
107 Constant *C = GVar->getInitializer();
108
109 // If the global is marked constant, we can put it into a mergable section,
110 // a mergable string section, or general .data if it contains relocations.
111 if (GVar->isConstant()) {
112 // If the initializer for the global contains something that requires a
113 // relocation, then we may have to drop this into a wriable data section
114 // even though it is marked const.
115 switch (C->getRelocationInfo()) {
116 default: llvm_unreachable("unknown relocation info kind");
117 case Constant::NoRelocation:
118 // If initializer is a null-terminated string, put it in a "cstring"
119 // section if the target has it.
120 if (isConstantString(C))
Chris Lattner27981192009-08-01 23:57:16 +0000121 return SectionKind::getMergeableCString();
Chris Lattnerf0144122009-07-28 03:13:23 +0000122
123 // Otherwise, just drop it into a mergable constant section. If we have
124 // a section for this size, use it, otherwise use the arbitrary sized
125 // mergable section.
126 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000127 case 4: return SectionKind::getMergeableConst4();
128 case 8: return SectionKind::getMergeableConst8();
129 case 16: return SectionKind::getMergeableConst16();
130 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000131 }
132
133 case Constant::LocalRelocation:
134 // In static relocation model, the linker will resolve all addresses, so
135 // the relocation entries will actually be constants by the time the app
136 // starts up. However, we can't put this into a mergable section, because
137 // the linker doesn't take relocations into consideration when it tries to
138 // merge entries in the section.
139 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000140 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000141
142 // Otherwise, the dynamic linker needs to fix it up, put it in the
143 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000144 return SectionKind::getReadOnlyWithRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000145
146 case Constant::GlobalRelocations:
147 // In static relocation model, the linker will resolve all addresses, so
148 // the relocation entries will actually be constants by the time the app
149 // starts up. However, we can't put this into a mergable section, because
150 // the linker doesn't take relocations into consideration when it tries to
151 // merge entries in the section.
152 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000153 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000154
155 // Otherwise, the dynamic linker needs to fix it up, put it in the
156 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000157 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000158 }
159 }
160
161 // Okay, this isn't a constant. If the initializer for the global is going
162 // to require a runtime relocation by the dynamic linker, put it into a more
163 // specific section to improve startup time of the app. This coalesces these
164 // globals together onto fewer pages, improving the locality of the dynamic
165 // linker.
166 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000167 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000168
169 switch (C->getRelocationInfo()) {
170 default: llvm_unreachable("unknown relocation info kind");
171 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000172 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000174 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000175 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000176 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000177 }
178}
179
180/// SectionForGlobal - This method computes the appropriate section to emit
181/// the specified global variable or function definition. This should not
182/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000183const MCSection *TargetLoweringObjectFile::
Chris Lattnere53a6002009-07-29 05:09:30 +0000184SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
185 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000186 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
187 "Can only be used for global definitions");
188
Chris Lattner968ff112009-08-01 21:11:14 +0000189 SectionKind Kind = SectionKindForGlobal(GV, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000190
Chris Lattnerf0144122009-07-28 03:13:23 +0000191 // Select section name.
192 if (GV->hasSection()) {
193 // If the target has special section hacks for specifically named globals,
194 // return them now.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000195 if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind))
Chris Lattnerf0144122009-07-28 03:13:23 +0000196 return TS;
197
198 // If the target has magic semantics for certain section names, make sure to
199 // pick up the flags. This allows the user to write things with attribute
200 // section and still get the appropriate section flags printed.
Chris Lattner968ff112009-08-01 21:11:14 +0000201 Kind = getKindForNamedSection(GV->getSection().c_str(), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000202
Chris Lattner968ff112009-08-01 21:11:14 +0000203 return getOrCreateSection(GV->getSection().c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000204 }
205
206
207 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000208 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000209}
210
211// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000212const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000213TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000214 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000215 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000216 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000217 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000218
Chris Lattnerf9650c02009-08-01 21:46:23 +0000219 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000220 return getTextSection();
221
Chris Lattner82458382009-08-01 21:56:13 +0000222 if (Kind.isBSS() && BSSSection != 0)
223 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000224
Chris Lattnerf9650c02009-08-01 21:46:23 +0000225 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000226 return ReadOnlySection;
227
228 return getDataSection();
229}
230
Chris Lattner83d77fa2009-08-01 23:46:12 +0000231/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000232/// specified size and relocation information, return a section that it
233/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000234const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000235TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000236 if (Kind.isReadOnly() && ReadOnlySection != 0)
237 return ReadOnlySection;
238
239 return DataSection;
240}
241
242
Chris Lattnera87dea42009-07-31 18:48:30 +0000243const MCSection *TargetLoweringObjectFile::
Chris Lattner968ff112009-08-01 21:11:14 +0000244getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnera87dea42009-07-31 18:48:30 +0000245 if (MCSection *S = Ctx->GetSection(Name))
246 return S;
Chris Lattner968ff112009-08-01 21:11:14 +0000247 return MCSection::Create(Name, isDirective, Kind, *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +0000248}
249
250
251
252//===----------------------------------------------------------------------===//
253// ELF
254//===----------------------------------------------------------------------===//
255
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000256void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
257 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000258 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000259 if (!HasCrazyBSS)
Chris Lattner27981192009-08-01 23:57:16 +0000260 BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000261 else
262 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
263 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000264 // FIXME2: this should just be handle by the section printer. We should get
265 // away from syntactic view of the sections and MCSection should just be a
266 // semantic view.
Chris Lattner27981192009-08-01 23:57:16 +0000267 BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000268
269
Chris Lattner27981192009-08-01 23:57:16 +0000270 TextSection = getOrCreateSection("\t.text", true, SectionKind::getText());
271 DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000272 ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000273 getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000274 TLSDataSection =
Chris Lattner27981192009-08-01 23:57:16 +0000275 getOrCreateSection("\t.tdata", false, SectionKind::getThreadData());
Chris Lattner82458382009-08-01 21:56:13 +0000276 CStringSection = getOrCreateSection("\t.rodata.str", true,
Chris Lattner27981192009-08-01 23:57:16 +0000277 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000278
Chris Lattner968ff112009-08-01 21:11:14 +0000279 TLSBSSSection = getOrCreateSection("\t.tbss", false,
Chris Lattner27981192009-08-01 23:57:16 +0000280 SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000281
282 DataRelSection = getOrCreateSection("\t.data.rel", false,
Chris Lattner27981192009-08-01 23:57:16 +0000283 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000284 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000285 SectionKind::getDataRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000286 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
Chris Lattner27981192009-08-01 23:57:16 +0000287 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000288 DataRelROLocalSection =
289 getOrCreateSection("\t.data.rel.ro.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000290 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000291
292 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
Chris Lattner27981192009-08-01 23:57:16 +0000293 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000294 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
Chris Lattner27981192009-08-01 23:57:16 +0000295 SectionKind::getMergeableConst8());
Chris Lattnerf0144122009-07-28 03:13:23 +0000296 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
Chris Lattner27981192009-08-01 23:57:16 +0000297 SectionKind::getMergeableConst16());
Chris Lattnerf0144122009-07-28 03:13:23 +0000298}
299
300
Chris Lattner968ff112009-08-01 21:11:14 +0000301SectionKind TargetLoweringObjectFileELF::
302getKindForNamedSection(const char *Name, SectionKind K) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000303 if (Name[0] != '.') return K;
304
305 // Some lame default implementation based on some magic section names.
306 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
307 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
308 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
309 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000310 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000311
312 if (strcmp(Name, ".tdata") == 0 ||
313 strncmp(Name, ".tdata.", 7) == 0 ||
314 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
315 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000316 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000317
318 if (strcmp(Name, ".tbss") == 0 ||
319 strncmp(Name, ".tbss.", 6) == 0 ||
320 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
321 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000322 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000323
324 return K;
325}
326
327void TargetLoweringObjectFileELF::
328getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
329 Str.push_back(',');
330 Str.push_back('"');
331
332 if (!Kind.isMetadata())
333 Str.push_back('a');
334 if (Kind.isText())
335 Str.push_back('x');
336 if (Kind.isWriteable())
337 Str.push_back('w');
Chris Lattner82987bf2009-07-31 16:17:13 +0000338 if (Kind.isMergeableCString() ||
339 Kind.isMergeableConst4() ||
340 Kind.isMergeableConst8() ||
341 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000342 Str.push_back('M');
343 if (Kind.isMergeableCString())
344 Str.push_back('S');
345 if (Kind.isThreadLocal())
346 Str.push_back('T');
347
348 Str.push_back('"');
349 Str.push_back(',');
350
351 // If comment string is '@', e.g. as on ARM - use '%' instead
352 if (AtIsCommentChar)
353 Str.push_back('%');
354 else
355 Str.push_back('@');
356
357 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000358 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000359 KindStr = "nobits";
360 else
361 KindStr = "progbits";
362
363 Str.append(KindStr, KindStr+strlen(KindStr));
364
365 if (Kind.isMergeableCString()) {
366 // TODO: Eventually handle multiple byte character strings. For now, all
367 // mergable C strings are single byte.
368 Str.push_back(',');
369 Str.push_back('1');
370 } else if (Kind.isMergeableConst4()) {
371 Str.push_back(',');
372 Str.push_back('4');
373 } else if (Kind.isMergeableConst8()) {
374 Str.push_back(',');
375 Str.push_back('8');
376 } else if (Kind.isMergeableConst16()) {
377 Str.push_back(',');
378 Str.push_back('1');
379 Str.push_back('6');
380 }
381}
382
383
384static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
385 if (Kind.isText()) return ".gnu.linkonce.t.";
386 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
387
388 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
389 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
390
391 if (Kind.isBSS()) return ".gnu.linkonce.b.";
392 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
393 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
394 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
395 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
396
397 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
398 return ".gnu.linkonce.d.rel.ro.";
399}
400
Chris Lattnera87dea42009-07-31 18:48:30 +0000401const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000402SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000403 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000404
405 // If this global is linkonce/weak and the target handles this by emitting it
406 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000407 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000408 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000409 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000410 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000411 }
412
Chris Lattnerf9650c02009-08-01 21:46:23 +0000413 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000414
Chris Lattnerf9650c02009-08-01 21:46:23 +0000415 if (Kind.isMergeableCString()) {
Chris Lattner82458382009-08-01 21:56:13 +0000416 assert(CStringSection && "Should have string section prefix");
Chris Lattnerf0144122009-07-28 03:13:23 +0000417
Chris Lattner067fe1a2009-07-29 04:54:38 +0000418 // We also need alignment here.
419 // FIXME: this is getting the alignment of the character, not the
420 // alignment of the global!
421 unsigned Align =
422 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000423
Chris Lattner82458382009-08-01 21:56:13 +0000424 std::string Name = CStringSection->getName() + "1." + utostr(Align);
Chris Lattner067fe1a2009-07-29 04:54:38 +0000425 return getOrCreateSection(Name.c_str(), false,
Chris Lattner27981192009-08-01 23:57:16 +0000426 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000427 }
428
Chris Lattnerf9650c02009-08-01 21:46:23 +0000429 if (Kind.isMergeableConst()) {
430 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000431 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000432 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000433 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000434 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000435 return MergeableConst16Section;
436 return ReadOnlySection; // .const
437 }
438
Chris Lattnerf9650c02009-08-01 21:46:23 +0000439 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000440
Chris Lattnerf9650c02009-08-01 21:46:23 +0000441 if (Kind.isThreadData()) return TLSDataSection;
442 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000443
Chris Lattner82458382009-08-01 21:56:13 +0000444 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000445
Chris Lattnerf9650c02009-08-01 21:46:23 +0000446 if (Kind.isDataNoRel()) return DataSection;
447 if (Kind.isDataRelLocal()) return DataRelLocalSection;
448 if (Kind.isDataRel()) return DataRelSection;
449 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000450
Chris Lattnerf9650c02009-08-01 21:46:23 +0000451 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000452 return DataRelROSection;
453}
454
Chris Lattner83d77fa2009-08-01 23:46:12 +0000455/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000456/// specified size and relocation information, return a section that it
457/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000458const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000459getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000460 if (Kind.isMergeableConst4())
461 return MergeableConst4Section;
462 if (Kind.isMergeableConst8())
463 return MergeableConst8Section;
464 if (Kind.isMergeableConst16())
465 return MergeableConst16Section;
466 if (Kind.isReadOnly())
467 return ReadOnlySection;
468
469 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
470 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
471 return DataRelROSection;
472}
473
474//===----------------------------------------------------------------------===//
475// MachO
476//===----------------------------------------------------------------------===//
477
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000478void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
479 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000480 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000481 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000482 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000483 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000484 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000485
Chris Lattner82458382009-08-01 21:56:13 +0000486 CStringSection = getOrCreateSection("\t.cstring", true,
Chris Lattner27981192009-08-01 23:57:16 +0000487 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000488 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000489 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000490 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000491 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000492
493 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
494 // to using it in -static mode.
495 if (TM.getRelocationModel() != Reloc::Static &&
496 TM.getTargetData()->getPointerSize() == 32)
497 SixteenByteConstantSection =
Chris Lattner968ff112009-08-01 21:11:14 +0000498 getOrCreateSection("\t.literal16\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000499 SectionKind::getMergeableConst16());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000500 else
501 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000502
Chris Lattner968ff112009-08-01 21:11:14 +0000503 ReadOnlySection = getOrCreateSection("\t.const", true,
Chris Lattner27981192009-08-01 23:57:16 +0000504 SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000505
506 TextCoalSection =
507 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Chris Lattner27981192009-08-01 23:57:16 +0000508 false, SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000509 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000510 false,
Chris Lattner27981192009-08-01 23:57:16 +0000511 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000512 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000513 false,
Chris Lattner27981192009-08-01 23:57:16 +0000514 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000515 ConstDataSection = getOrCreateSection("\t.const_data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000516 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000517 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000518 false,
Chris Lattner27981192009-08-01 23:57:16 +0000519 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000520}
521
Chris Lattnera87dea42009-07-31 18:48:30 +0000522const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000523SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000524 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000525 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000526
Chris Lattnerf9650c02009-08-01 21:46:23 +0000527 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000528 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000529
530 // If this is weak/linkonce, put this in a coalescable section, either in text
531 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000532 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000533 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000534 return ConstTextCoalSection;
535 return DataCoalSection;
536 }
537
538 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000539 if (Kind.isMergeableCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000540 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
541 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
542 const TargetData &TD = *TM.getTargetData();
543 unsigned Size = TD.getTypeAllocSize(Ty);
544 if (Size) {
545 unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV));
546 if (Align <= 32)
Chris Lattner82458382009-08-01 21:56:13 +0000547 return CStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000548 }
549
550 return ReadOnlySection;
551 }
552
Chris Lattnerf9650c02009-08-01 21:46:23 +0000553 if (Kind.isMergeableConst()) {
554 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000555 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000556 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000557 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000558 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000559 return SixteenByteConstantSection;
560 return ReadOnlySection; // .const
561 }
562
563 // FIXME: ROData -> const in -static mode that is relocatable but they happen
564 // by the static linker. Why not mergeable?
Chris Lattnerf9650c02009-08-01 21:46:23 +0000565 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000566 return ReadOnlySection;
567
568 // If this is marked const, put it into a const section. But if the dynamic
569 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000570 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000571 return ConstDataSection;
572
573 // Otherwise, just drop the variable in the normal data section.
574 return DataSection;
575}
576
Chris Lattnera87dea42009-07-31 18:48:30 +0000577const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000578TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000579 // If this constant requires a relocation, we have to put it in the data
580 // segment, not in the text segment.
581 if (Kind.isDataRel())
582 return ConstDataSection;
583
584 if (Kind.isMergeableConst4())
585 return FourByteConstantSection;
586 if (Kind.isMergeableConst8())
587 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000588 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000589 return SixteenByteConstantSection;
590 return ReadOnlySection; // .const
591}
592
Chris Lattner26630c12009-07-31 20:52:39 +0000593/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
594/// not to emit the UsedDirective for some symbols in llvm.used.
595// FIXME: REMOVE this (rdar://7071300)
596bool TargetLoweringObjectFileMachO::
597shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
598 /// On Darwin, internally linked data beginning with "L" or "l" does not have
599 /// the directive emitted (this occurs in ObjC metadata).
600 if (!GV) return false;
601
602 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
603 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
604 // FIXME: ObjC metadata is currently emitted as internal symbols that have
605 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
606 // this horrible hack can go away.
607 const std::string &Name = Mang->getMangledName(GV);
608 if (Name[0] == 'L' || Name[0] == 'l')
609 return false;
610 }
611
612 return true;
613}
614
615
Chris Lattnerf0144122009-07-28 03:13:23 +0000616//===----------------------------------------------------------------------===//
617// COFF
618//===----------------------------------------------------------------------===//
619
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000620void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
621 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000622 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000623 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000624 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000625 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000626 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000627}
628
629void TargetLoweringObjectFileCOFF::
630getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
631 // FIXME: Inefficient.
632 std::string Res = ",\"";
633 if (Kind.isText())
634 Res += 'x';
635 if (Kind.isWriteable())
636 Res += 'w';
637 Res += "\"";
638
639 Str.append(Res.begin(), Res.end());
640}
641
642static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
643 if (Kind.isText())
644 return ".text$linkonce";
645 if (Kind.isWriteable())
646 return ".data$linkonce";
647 return ".rdata$linkonce";
648}
649
650
Chris Lattnera87dea42009-07-31 18:48:30 +0000651const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000652SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000653 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000654 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000655
656 // If this global is linkonce/weak and the target handles this by emitting it
657 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000658 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000659 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000660 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000661 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000662 }
663
Chris Lattnerf9650c02009-08-01 21:46:23 +0000664 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000665 return getTextSection();
666
Chris Lattnerf0144122009-07-28 03:13:23 +0000667 return getDataSection();
668}
669