blob: b9e585d6dd0970d8c34cc55b09a0abdadf01afb2 [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;
35 BSSSection_ = 0;
36 ReadOnlySection = 0;
37 TLSDataSection = 0;
38 TLSBSSSection = 0;
39 CStringSection_ = 0;
40}
41
42TargetLoweringObjectFile::~TargetLoweringObjectFile() {
43}
44
45static bool isSuitableForBSS(const GlobalVariable *GV) {
46 Constant *C = GV->getInitializer();
47
48 // Must have zero initializer.
49 if (!C->isNullValue())
50 return false;
51
52 // Leave constant zeros in readonly constant sections, so they can be shared.
53 if (GV->isConstant())
54 return false;
55
56 // If the global has an explicit section specified, don't put it in BSS.
57 if (!GV->getSection().empty())
58 return false;
59
60 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
61 if (NoZerosInBSS)
62 return false;
63
64 // Otherwise, put it in BSS!
65 return true;
66}
67
68static bool isConstantString(const Constant *C) {
69 // First check: is we have constant array of i8 terminated with zero
70 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
71 // Check, if initializer is a null-terminated string
72 if (CVA && CVA->isCString())
73 return true;
74
75 // Another possibility: [1 x i8] zeroinitializer
76 if (isa<ConstantAggregateZero>(C))
77 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
78 return (Ty->getElementType() == Type::Int8Ty &&
79 Ty->getNumElements() == 1);
80
81 return false;
82}
83
Chris Lattner968ff112009-08-01 21:11:14 +000084/// SectionKindForGlobal - This is a top-level target-independent classifier for
85/// a global variable. Given an global variable and information from TM, it
86/// classifies the global in a variety of ways that make various target
87/// implementations simpler. The target implementation is free to ignore this
88/// extra info of course.
89static SectionKind SectionKindForGlobal(const GlobalValue *GV,
90 const TargetMachine &TM) {
Chris Lattnerf0144122009-07-28 03:13:23 +000091 Reloc::Model ReloModel = TM.getRelocationModel();
92
93 // Early exit - functions should be always in text sections.
94 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
95 if (GVar == 0)
Chris Lattner968ff112009-08-01 21:11:14 +000096 return SectionKind::get(SectionKind::Text);
Chris Lattnerf0144122009-07-28 03:13:23 +000097
98
99 // Handle thread-local data first.
100 if (GVar->isThreadLocal()) {
101 if (isSuitableForBSS(GVar))
Chris Lattner968ff112009-08-01 21:11:14 +0000102 return SectionKind::get(SectionKind::ThreadBSS);
103 return SectionKind::get(SectionKind::ThreadData);
Chris Lattnerf0144122009-07-28 03:13:23 +0000104 }
105
106 // Variable can be easily put to BSS section.
107 if (isSuitableForBSS(GVar))
Chris Lattner968ff112009-08-01 21:11:14 +0000108 return SectionKind::get(SectionKind::BSS);
Chris Lattnerf0144122009-07-28 03:13:23 +0000109
110 Constant *C = GVar->getInitializer();
111
112 // If the global is marked constant, we can put it into a mergable section,
113 // a mergable string section, or general .data if it contains relocations.
114 if (GVar->isConstant()) {
115 // If the initializer for the global contains something that requires a
116 // relocation, then we may have to drop this into a wriable data section
117 // even though it is marked const.
118 switch (C->getRelocationInfo()) {
119 default: llvm_unreachable("unknown relocation info kind");
120 case Constant::NoRelocation:
121 // If initializer is a null-terminated string, put it in a "cstring"
122 // section if the target has it.
123 if (isConstantString(C))
Chris Lattner968ff112009-08-01 21:11:14 +0000124 return SectionKind::get(SectionKind::MergeableCString);
Chris Lattnerf0144122009-07-28 03:13:23 +0000125
126 // Otherwise, just drop it into a mergable constant section. If we have
127 // a section for this size, use it, otherwise use the arbitrary sized
128 // mergable section.
129 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner968ff112009-08-01 21:11:14 +0000130 case 4: return SectionKind::get(SectionKind::MergeableConst4);
131 case 8: return SectionKind::get(SectionKind::MergeableConst8);
132 case 16: return SectionKind::get(SectionKind::MergeableConst16);
133 default: return SectionKind::get(SectionKind::MergeableConst);
Chris Lattnerf0144122009-07-28 03:13:23 +0000134 }
135
136 case Constant::LocalRelocation:
137 // In static relocation model, the linker will resolve all addresses, so
138 // the relocation entries will actually be constants by the time the app
139 // starts up. However, we can't put this into a mergable section, because
140 // the linker doesn't take relocations into consideration when it tries to
141 // merge entries in the section.
142 if (ReloModel == Reloc::Static)
Chris Lattner968ff112009-08-01 21:11:14 +0000143 return SectionKind::get(SectionKind::ReadOnly);
Chris Lattnerf0144122009-07-28 03:13:23 +0000144
145 // Otherwise, the dynamic linker needs to fix it up, put it in the
146 // writable data.rel.local section.
Chris Lattner968ff112009-08-01 21:11:14 +0000147 return SectionKind::get(SectionKind::ReadOnlyWithRelLocal);
Chris Lattnerf0144122009-07-28 03:13:23 +0000148
149 case Constant::GlobalRelocations:
150 // In static relocation model, the linker will resolve all addresses, so
151 // the relocation entries will actually be constants by the time the app
152 // starts up. However, we can't put this into a mergable section, because
153 // the linker doesn't take relocations into consideration when it tries to
154 // merge entries in the section.
155 if (ReloModel == Reloc::Static)
Chris Lattner968ff112009-08-01 21:11:14 +0000156 return SectionKind::get(SectionKind::ReadOnly);
Chris Lattnerf0144122009-07-28 03:13:23 +0000157
158 // Otherwise, the dynamic linker needs to fix it up, put it in the
159 // writable data.rel section.
Chris Lattner968ff112009-08-01 21:11:14 +0000160 return SectionKind::get(SectionKind::ReadOnlyWithRel);
Chris Lattnerf0144122009-07-28 03:13:23 +0000161 }
162 }
163
164 // Okay, this isn't a constant. If the initializer for the global is going
165 // to require a runtime relocation by the dynamic linker, put it into a more
166 // specific section to improve startup time of the app. This coalesces these
167 // globals together onto fewer pages, improving the locality of the dynamic
168 // linker.
169 if (ReloModel == Reloc::Static)
Chris Lattner968ff112009-08-01 21:11:14 +0000170 return SectionKind::get(SectionKind::DataNoRel);
Chris Lattnerf0144122009-07-28 03:13:23 +0000171
172 switch (C->getRelocationInfo()) {
173 default: llvm_unreachable("unknown relocation info kind");
174 case Constant::NoRelocation:
Chris Lattner968ff112009-08-01 21:11:14 +0000175 return SectionKind::get(SectionKind::DataNoRel);
Chris Lattnerf0144122009-07-28 03:13:23 +0000176 case Constant::LocalRelocation:
Chris Lattner968ff112009-08-01 21:11:14 +0000177 return SectionKind::get(SectionKind::DataRelLocal);
Chris Lattnerf0144122009-07-28 03:13:23 +0000178 case Constant::GlobalRelocations:
Chris Lattner968ff112009-08-01 21:11:14 +0000179 return SectionKind::get(SectionKind::DataRel);
Chris Lattnerf0144122009-07-28 03:13:23 +0000180 }
181}
182
183/// SectionForGlobal - This method computes the appropriate section to emit
184/// the specified global variable or function definition. This should not
185/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000186const MCSection *TargetLoweringObjectFile::
Chris Lattnere53a6002009-07-29 05:09:30 +0000187SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
188 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000189 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
190 "Can only be used for global definitions");
191
Chris Lattner968ff112009-08-01 21:11:14 +0000192 SectionKind Kind = SectionKindForGlobal(GV, TM);
193 SectionInfo Info = SectionInfo::get(Kind, GV->isWeakForLinker());
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 Lattner968ff112009-08-01 21:11:14 +0000199 if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Info))
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 Lattner968ff112009-08-01 21:11:14 +0000212 return SelectSectionForGlobal(GV, Info, 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 Lattner968ff112009-08-01 21:11:14 +0000218 SectionInfo Info,
Chris Lattnere53a6002009-07-29 05:09:30 +0000219 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000220 const TargetMachine &TM) const{
Chris Lattner968ff112009-08-01 21:11:14 +0000221 assert(!Info.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000222
Chris Lattner968ff112009-08-01 21:11:14 +0000223 if (Info.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000224 return getTextSection();
225
Chris Lattner968ff112009-08-01 21:11:14 +0000226 if (Info.isBSS() && BSSSection_ != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000227 return BSSSection_;
228
Chris Lattner968ff112009-08-01 21:11:14 +0000229 if (Info.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000230 return ReadOnlySection;
231
232 return getDataSection();
233}
234
235/// getSectionForMergableConstant - Given a mergable constant with the
236/// 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 Lattnerf0144122009-07-28 03:13:23 +0000239TargetLoweringObjectFile::
240getSectionForMergeableConstant(SectionKind Kind) const {
241 if (Kind.isReadOnly() && ReadOnlySection != 0)
242 return ReadOnlySection;
243
244 return DataSection;
245}
246
247
Chris Lattnera87dea42009-07-31 18:48:30 +0000248const MCSection *TargetLoweringObjectFile::
Chris Lattner968ff112009-08-01 21:11:14 +0000249getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnera87dea42009-07-31 18:48:30 +0000250 if (MCSection *S = Ctx->GetSection(Name))
251 return S;
Chris Lattner968ff112009-08-01 21:11:14 +0000252 return MCSection::Create(Name, isDirective, Kind, *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +0000253}
254
255
256
257//===----------------------------------------------------------------------===//
258// ELF
259//===----------------------------------------------------------------------===//
260
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000261void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
262 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000263 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000264 if (!HasCrazyBSS)
Chris Lattner968ff112009-08-01 21:11:14 +0000265 BSSSection_ = getOrCreateSection("\t.bss", true,
266 SectionKind::get(SectionKind::BSS));
Chris Lattnerf0144122009-07-28 03:13:23 +0000267 else
268 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
269 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000270 // FIXME2: this should just be handle by the section printer. We should get
271 // away from syntactic view of the sections and MCSection should just be a
272 // semantic view.
273 BSSSection_ = getOrCreateSection("\t.bss", false,
274 SectionKind::get(SectionKind::BSS));
Chris Lattnerf0144122009-07-28 03:13:23 +0000275
276
Chris Lattner968ff112009-08-01 21:11:14 +0000277 TextSection = getOrCreateSection("\t.text", true,
278 SectionKind::get(SectionKind::Text));
279 DataSection = getOrCreateSection("\t.data", true,
280 SectionKind::get(SectionKind::DataRel));
Chris Lattnerf0144122009-07-28 03:13:23 +0000281 ReadOnlySection =
Chris Lattner968ff112009-08-01 21:11:14 +0000282 getOrCreateSection("\t.rodata", false,
283 SectionKind::get(SectionKind::ReadOnly));
Chris Lattnerf0144122009-07-28 03:13:23 +0000284 TLSDataSection =
Chris Lattner968ff112009-08-01 21:11:14 +0000285 getOrCreateSection("\t.tdata", false,
286 SectionKind::get(SectionKind::ThreadData));
Chris Lattnerf0144122009-07-28 03:13:23 +0000287 CStringSection_ = getOrCreateSection("\t.rodata.str", true,
Chris Lattner968ff112009-08-01 21:11:14 +0000288 SectionKind::get(SectionKind::MergeableCString));
Chris Lattnerf0144122009-07-28 03:13:23 +0000289
Chris Lattner968ff112009-08-01 21:11:14 +0000290 TLSBSSSection = getOrCreateSection("\t.tbss", false,
291 SectionKind::get(SectionKind::ThreadBSS));
Chris Lattnerf0144122009-07-28 03:13:23 +0000292
293 DataRelSection = getOrCreateSection("\t.data.rel", false,
Chris Lattner968ff112009-08-01 21:11:14 +0000294 SectionKind::get(SectionKind::DataRel));
Chris Lattnerf0144122009-07-28 03:13:23 +0000295 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
Chris Lattner968ff112009-08-01 21:11:14 +0000296 SectionKind::get(SectionKind::DataRelLocal));
Chris Lattnerf0144122009-07-28 03:13:23 +0000297 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
Chris Lattner968ff112009-08-01 21:11:14 +0000298 SectionKind::get(SectionKind::ReadOnlyWithRel));
Chris Lattnerf0144122009-07-28 03:13:23 +0000299 DataRelROLocalSection =
300 getOrCreateSection("\t.data.rel.ro.local", false,
Chris Lattner968ff112009-08-01 21:11:14 +0000301 SectionKind::get(SectionKind::ReadOnlyWithRelLocal));
Chris Lattnerf0144122009-07-28 03:13:23 +0000302
303 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
Chris Lattner968ff112009-08-01 21:11:14 +0000304 SectionKind::get(SectionKind::MergeableConst4));
Chris Lattnerf0144122009-07-28 03:13:23 +0000305 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
Chris Lattner968ff112009-08-01 21:11:14 +0000306 SectionKind::get(SectionKind::MergeableConst8));
Chris Lattnerf0144122009-07-28 03:13:23 +0000307 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
Chris Lattner968ff112009-08-01 21:11:14 +0000308 SectionKind::get(SectionKind::MergeableConst16));
Chris Lattnerf0144122009-07-28 03:13:23 +0000309}
310
311
Chris Lattner968ff112009-08-01 21:11:14 +0000312SectionKind TargetLoweringObjectFileELF::
313getKindForNamedSection(const char *Name, SectionKind K) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000314 if (Name[0] != '.') return K;
315
316 // Some lame default implementation based on some magic section names.
317 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
318 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
319 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
320 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner968ff112009-08-01 21:11:14 +0000321 return SectionKind::get(SectionKind::BSS);
Chris Lattnerf0144122009-07-28 03:13:23 +0000322
323 if (strcmp(Name, ".tdata") == 0 ||
324 strncmp(Name, ".tdata.", 7) == 0 ||
325 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
326 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner968ff112009-08-01 21:11:14 +0000327 return SectionKind::get(SectionKind::ThreadData);
Chris Lattnerf0144122009-07-28 03:13:23 +0000328
329 if (strcmp(Name, ".tbss") == 0 ||
330 strncmp(Name, ".tbss.", 6) == 0 ||
331 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
332 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner968ff112009-08-01 21:11:14 +0000333 return SectionKind::get(SectionKind::ThreadBSS);
Chris Lattnerf0144122009-07-28 03:13:23 +0000334
335 return K;
336}
337
338void TargetLoweringObjectFileELF::
339getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
340 Str.push_back(',');
341 Str.push_back('"');
342
343 if (!Kind.isMetadata())
344 Str.push_back('a');
345 if (Kind.isText())
346 Str.push_back('x');
347 if (Kind.isWriteable())
348 Str.push_back('w');
Chris Lattner82987bf2009-07-31 16:17:13 +0000349 if (Kind.isMergeableCString() ||
350 Kind.isMergeableConst4() ||
351 Kind.isMergeableConst8() ||
352 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000353 Str.push_back('M');
354 if (Kind.isMergeableCString())
355 Str.push_back('S');
356 if (Kind.isThreadLocal())
357 Str.push_back('T');
358
359 Str.push_back('"');
360 Str.push_back(',');
361
362 // If comment string is '@', e.g. as on ARM - use '%' instead
363 if (AtIsCommentChar)
364 Str.push_back('%');
365 else
366 Str.push_back('@');
367
368 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000369 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000370 KindStr = "nobits";
371 else
372 KindStr = "progbits";
373
374 Str.append(KindStr, KindStr+strlen(KindStr));
375
376 if (Kind.isMergeableCString()) {
377 // TODO: Eventually handle multiple byte character strings. For now, all
378 // mergable C strings are single byte.
379 Str.push_back(',');
380 Str.push_back('1');
381 } else if (Kind.isMergeableConst4()) {
382 Str.push_back(',');
383 Str.push_back('4');
384 } else if (Kind.isMergeableConst8()) {
385 Str.push_back(',');
386 Str.push_back('8');
387 } else if (Kind.isMergeableConst16()) {
388 Str.push_back(',');
389 Str.push_back('1');
390 Str.push_back('6');
391 }
392}
393
394
395static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
396 if (Kind.isText()) return ".gnu.linkonce.t.";
397 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
398
399 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
400 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
401
402 if (Kind.isBSS()) return ".gnu.linkonce.b.";
403 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
404 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
405 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
406 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
407
408 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
409 return ".gnu.linkonce.d.rel.ro.";
410}
411
Chris Lattnera87dea42009-07-31 18:48:30 +0000412const MCSection *TargetLoweringObjectFileELF::
Chris Lattner968ff112009-08-01 21:11:14 +0000413SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Info,
Chris Lattnere53a6002009-07-29 05:09:30 +0000414 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000415
416 // If this global is linkonce/weak and the target handles this by emitting it
417 // into a 'uniqued' section name, create and return the section now.
Chris Lattner968ff112009-08-01 21:11:14 +0000418 if (Info.isWeak()) {
419 const char *Prefix = getSectionPrefixForUniqueGlobal(Info);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000420 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner968ff112009-08-01 21:11:14 +0000421 return getOrCreateSection((Prefix+Name).c_str(), false, Info);
Chris Lattnerf0144122009-07-28 03:13:23 +0000422 }
423
Chris Lattner968ff112009-08-01 21:11:14 +0000424 if (Info.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000425
Chris Lattner968ff112009-08-01 21:11:14 +0000426 if (Info.isMergeableCString()) {
Chris Lattner067fe1a2009-07-29 04:54:38 +0000427 assert(CStringSection_ && "Should have string section prefix");
Chris Lattnerf0144122009-07-28 03:13:23 +0000428
Chris Lattner067fe1a2009-07-29 04:54:38 +0000429 // We also need alignment here.
430 // FIXME: this is getting the alignment of the character, not the
431 // alignment of the global!
432 unsigned Align =
433 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000434
Chris Lattnera6792072009-07-29 04:55:08 +0000435 std::string Name = CStringSection_->getName() + "1." + utostr(Align);
Chris Lattner067fe1a2009-07-29 04:54:38 +0000436 return getOrCreateSection(Name.c_str(), false,
Chris Lattner968ff112009-08-01 21:11:14 +0000437 SectionKind::get(SectionKind::MergeableCString));
Chris Lattnerf0144122009-07-28 03:13:23 +0000438 }
439
Chris Lattner968ff112009-08-01 21:11:14 +0000440 if (Info.isMergeableConst()) {
441 if (Info.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000442 return MergeableConst4Section;
Chris Lattner968ff112009-08-01 21:11:14 +0000443 if (Info.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000444 return MergeableConst8Section;
Chris Lattner968ff112009-08-01 21:11:14 +0000445 if (Info.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000446 return MergeableConst16Section;
447 return ReadOnlySection; // .const
448 }
449
Chris Lattner968ff112009-08-01 21:11:14 +0000450 if (Info.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000451
Chris Lattner968ff112009-08-01 21:11:14 +0000452 if (Info.isThreadData()) return TLSDataSection;
453 if (Info.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000454
Chris Lattner968ff112009-08-01 21:11:14 +0000455 if (Info.isBSS()) return BSSSection_;
Chris Lattnerf0144122009-07-28 03:13:23 +0000456
Chris Lattner968ff112009-08-01 21:11:14 +0000457 if (Info.isDataNoRel()) return DataSection;
458 if (Info.isDataRelLocal()) return DataRelLocalSection;
459 if (Info.isDataRel()) return DataRelSection;
460 if (Info.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000461
Chris Lattner968ff112009-08-01 21:11:14 +0000462 assert(Info.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000463 return DataRelROSection;
464}
465
466/// getSectionForMergeableConstant - Given a mergeable constant with the
467/// specified size and relocation information, return a section that it
468/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000469const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf0144122009-07-28 03:13:23 +0000470getSectionForMergeableConstant(SectionKind Kind) const {
471 if (Kind.isMergeableConst4())
472 return MergeableConst4Section;
473 if (Kind.isMergeableConst8())
474 return MergeableConst8Section;
475 if (Kind.isMergeableConst16())
476 return MergeableConst16Section;
477 if (Kind.isReadOnly())
478 return ReadOnlySection;
479
480 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
481 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
482 return DataRelROSection;
483}
484
485//===----------------------------------------------------------------------===//
486// MachO
487//===----------------------------------------------------------------------===//
488
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000489void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
490 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000491 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000492 TextSection = getOrCreateSection("\t.text", true,
493 SectionKind::get(SectionKind::Text));
494 DataSection = getOrCreateSection("\t.data", true,
495 SectionKind::get(SectionKind::DataRel));
Chris Lattnerf0144122009-07-28 03:13:23 +0000496
497 CStringSection_ = getOrCreateSection("\t.cstring", true,
Chris Lattner968ff112009-08-01 21:11:14 +0000498 SectionKind::get(SectionKind::MergeableCString));
Chris Lattnerf0144122009-07-28 03:13:23 +0000499 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
Chris Lattner968ff112009-08-01 21:11:14 +0000500 SectionKind::get(SectionKind::MergeableConst4));
Chris Lattnerf0144122009-07-28 03:13:23 +0000501 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
Chris Lattner968ff112009-08-01 21:11:14 +0000502 SectionKind::get(SectionKind::MergeableConst8));
Chris Lattner4bb253c2009-07-28 17:50:28 +0000503
504 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
505 // to using it in -static mode.
506 if (TM.getRelocationModel() != Reloc::Static &&
507 TM.getTargetData()->getPointerSize() == 32)
508 SixteenByteConstantSection =
Chris Lattner968ff112009-08-01 21:11:14 +0000509 getOrCreateSection("\t.literal16\n", true,
510 SectionKind::get(SectionKind::MergeableConst16));
Chris Lattner4bb253c2009-07-28 17:50:28 +0000511 else
512 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000513
Chris Lattner968ff112009-08-01 21:11:14 +0000514 ReadOnlySection = getOrCreateSection("\t.const", true,
515 SectionKind::get(SectionKind::ReadOnly));
Chris Lattnerf0144122009-07-28 03:13:23 +0000516
517 TextCoalSection =
518 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Chris Lattner968ff112009-08-01 21:11:14 +0000519 false, SectionKind::get(SectionKind::Text));
Chris Lattnerf0144122009-07-28 03:13:23 +0000520 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000521 false,
522 SectionKind::get(SectionKind::Text));
Chris Lattnerf0144122009-07-28 03:13:23 +0000523 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000524 false,
525 SectionKind::get(SectionKind::Text));
Chris Lattnerf0144122009-07-28 03:13:23 +0000526 ConstDataSection = getOrCreateSection("\t.const_data", true,
Chris Lattner968ff112009-08-01 21:11:14 +0000527 SectionKind::get(SectionKind::ReadOnlyWithRel));
Chris Lattnerf0144122009-07-28 03:13:23 +0000528 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000529 false,
530 SectionKind::get(SectionKind::DataRel));
Chris Lattnerf0144122009-07-28 03:13:23 +0000531}
532
Chris Lattnera87dea42009-07-31 18:48:30 +0000533const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner968ff112009-08-01 21:11:14 +0000534SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Info,
Chris Lattnere53a6002009-07-29 05:09:30 +0000535 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner968ff112009-08-01 21:11:14 +0000536 assert(!Info.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000537
Chris Lattner968ff112009-08-01 21:11:14 +0000538 if (Info.isText())
539 return Info.isWeak() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000540
541 // If this is weak/linkonce, put this in a coalescable section, either in text
542 // or data depending on if it is writable.
Chris Lattner968ff112009-08-01 21:11:14 +0000543 if (Info.isWeak()) {
544 if (Info.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000545 return ConstTextCoalSection;
546 return DataCoalSection;
547 }
548
549 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner968ff112009-08-01 21:11:14 +0000550 if (Info.isMergeableCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000551 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
552 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
553 const TargetData &TD = *TM.getTargetData();
554 unsigned Size = TD.getTypeAllocSize(Ty);
555 if (Size) {
556 unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV));
557 if (Align <= 32)
558 return CStringSection_;
559 }
560
561 return ReadOnlySection;
562 }
563
Chris Lattner968ff112009-08-01 21:11:14 +0000564 if (Info.isMergeableConst()) {
565 if (Info.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000566 return FourByteConstantSection;
Chris Lattner968ff112009-08-01 21:11:14 +0000567 if (Info.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000568 return EightByteConstantSection;
Chris Lattner968ff112009-08-01 21:11:14 +0000569 if (Info.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000570 return SixteenByteConstantSection;
571 return ReadOnlySection; // .const
572 }
573
574 // FIXME: ROData -> const in -static mode that is relocatable but they happen
575 // by the static linker. Why not mergeable?
Chris Lattner968ff112009-08-01 21:11:14 +0000576 if (Info.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000577 return ReadOnlySection;
578
579 // If this is marked const, put it into a const section. But if the dynamic
580 // linker needs to write to it, put it in the data segment.
Chris Lattner968ff112009-08-01 21:11:14 +0000581 if (Info.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000582 return ConstDataSection;
583
584 // Otherwise, just drop the variable in the normal data section.
585 return DataSection;
586}
587
Chris Lattnera87dea42009-07-31 18:48:30 +0000588const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000589TargetLoweringObjectFileMachO::
590getSectionForMergeableConstant(SectionKind Kind) const {
591 // If this constant requires a relocation, we have to put it in the data
592 // segment, not in the text segment.
593 if (Kind.isDataRel())
594 return ConstDataSection;
595
596 if (Kind.isMergeableConst4())
597 return FourByteConstantSection;
598 if (Kind.isMergeableConst8())
599 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000600 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000601 return SixteenByteConstantSection;
602 return ReadOnlySection; // .const
603}
604
Chris Lattner26630c12009-07-31 20:52:39 +0000605/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
606/// not to emit the UsedDirective for some symbols in llvm.used.
607// FIXME: REMOVE this (rdar://7071300)
608bool TargetLoweringObjectFileMachO::
609shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
610 /// On Darwin, internally linked data beginning with "L" or "l" does not have
611 /// the directive emitted (this occurs in ObjC metadata).
612 if (!GV) return false;
613
614 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
615 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
616 // FIXME: ObjC metadata is currently emitted as internal symbols that have
617 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
618 // this horrible hack can go away.
619 const std::string &Name = Mang->getMangledName(GV);
620 if (Name[0] == 'L' || Name[0] == 'l')
621 return false;
622 }
623
624 return true;
625}
626
627
Chris Lattnerf0144122009-07-28 03:13:23 +0000628//===----------------------------------------------------------------------===//
629// COFF
630//===----------------------------------------------------------------------===//
631
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000632void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
633 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000634 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000635 TextSection = getOrCreateSection("\t.text", true,
636 SectionKind::get(SectionKind::Text));
637 DataSection = getOrCreateSection("\t.data", true,
638 SectionKind::get(SectionKind::DataRel));
Chris Lattnerf0144122009-07-28 03:13:23 +0000639}
640
641void TargetLoweringObjectFileCOFF::
642getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
643 // FIXME: Inefficient.
644 std::string Res = ",\"";
645 if (Kind.isText())
646 Res += 'x';
647 if (Kind.isWriteable())
648 Res += 'w';
649 Res += "\"";
650
651 Str.append(Res.begin(), Res.end());
652}
653
654static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
655 if (Kind.isText())
656 return ".text$linkonce";
657 if (Kind.isWriteable())
658 return ".data$linkonce";
659 return ".rdata$linkonce";
660}
661
662
Chris Lattnera87dea42009-07-31 18:48:30 +0000663const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner968ff112009-08-01 21:11:14 +0000664SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Info,
Chris Lattnere53a6002009-07-29 05:09:30 +0000665 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner968ff112009-08-01 21:11:14 +0000666 assert(!Info.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000667
668 // If this global is linkonce/weak and the target handles this by emitting it
669 // into a 'uniqued' section name, create and return the section now.
Chris Lattner968ff112009-08-01 21:11:14 +0000670 if (Info.isWeak()) {
671 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Info);
672 std::string Name = Mang->makeNameProper(GV->getNameStr());
673 return getOrCreateSection((Prefix+Name).c_str(), false, Info);
Chris Lattnerf0144122009-07-28 03:13:23 +0000674 }
675
Chris Lattner968ff112009-08-01 21:11:14 +0000676 if (Info.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000677 return getTextSection();
678
Chris Lattner968ff112009-08-01 21:11:14 +0000679 if (Info.isBSS() && BSSSection_ != 0)
Chris Lattnera87dea42009-07-31 18:48:30 +0000680 return BSSSection_;
Chris Lattnerf0144122009-07-28 03:13:23 +0000681
Chris Lattner968ff112009-08-01 21:11:14 +0000682 if (Info.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000683 return ReadOnlySection;
684
685 return getDataSection();
686}
687