blob: a86946da9f107513becec13f144fc595970931ef [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 Lattnerf0144122009-07-28 03:13:23 +000039}
40
41TargetLoweringObjectFile::~TargetLoweringObjectFile() {
42}
43
44static bool isSuitableForBSS(const GlobalVariable *GV) {
45 Constant *C = GV->getInitializer();
46
47 // Must have zero initializer.
48 if (!C->isNullValue())
49 return false;
50
51 // Leave constant zeros in readonly constant sections, so they can be shared.
52 if (GV->isConstant())
53 return false;
54
55 // If the global has an explicit section specified, don't put it in BSS.
56 if (!GV->getSection().empty())
57 return false;
58
59 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
60 if (NoZerosInBSS)
61 return false;
62
63 // Otherwise, put it in BSS!
64 return true;
65}
66
67static bool isConstantString(const Constant *C) {
68 // First check: is we have constant array of i8 terminated with zero
69 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
70 // Check, if initializer is a null-terminated string
71 if (CVA && CVA->isCString())
72 return true;
73
74 // Another possibility: [1 x i8] zeroinitializer
75 if (isa<ConstantAggregateZero>(C))
76 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
77 return (Ty->getElementType() == Type::Int8Ty &&
78 Ty->getNumElements() == 1);
79
80 return false;
81}
82
Chris Lattner968ff112009-08-01 21:11:14 +000083/// SectionKindForGlobal - This is a top-level target-independent classifier for
84/// a global variable. Given an global variable and information from TM, it
85/// classifies the global in a variety of ways that make various target
86/// implementations simpler. The target implementation is free to ignore this
87/// extra info of course.
88static SectionKind SectionKindForGlobal(const GlobalValue *GV,
89 const TargetMachine &TM) {
Chris Lattnerf0144122009-07-28 03:13:23 +000090 Reloc::Model ReloModel = TM.getRelocationModel();
91
92 // Early exit - functions should be always in text sections.
93 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
94 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +000095 return SectionKind::getText();
Chris Lattnerf0144122009-07-28 03:13:23 +000096
97
98 // Handle thread-local data first.
99 if (GVar->isThreadLocal()) {
100 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000101 return SectionKind::getThreadBSS();
102 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000103 }
104
105 // Variable can be easily put to BSS section.
106 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000107 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000108
109 Constant *C = GVar->getInitializer();
110
111 // If the global is marked constant, we can put it into a mergable section,
112 // a mergable string section, or general .data if it contains relocations.
113 if (GVar->isConstant()) {
114 // If the initializer for the global contains something that requires a
115 // relocation, then we may have to drop this into a wriable data section
116 // even though it is marked const.
117 switch (C->getRelocationInfo()) {
118 default: llvm_unreachable("unknown relocation info kind");
119 case Constant::NoRelocation:
120 // If initializer is a null-terminated string, put it in a "cstring"
121 // section if the target has it.
122 if (isConstantString(C))
Chris Lattner27981192009-08-01 23:57:16 +0000123 return SectionKind::getMergeableCString();
Chris Lattnerf0144122009-07-28 03:13:23 +0000124
125 // Otherwise, just drop it into a mergable constant section. If we have
126 // a section for this size, use it, otherwise use the arbitrary sized
127 // mergable section.
128 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000129 case 4: return SectionKind::getMergeableConst4();
130 case 8: return SectionKind::getMergeableConst8();
131 case 16: return SectionKind::getMergeableConst16();
132 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000133 }
134
135 case Constant::LocalRelocation:
136 // In static relocation model, the linker will resolve all addresses, so
137 // the relocation entries will actually be constants by the time the app
138 // starts up. However, we can't put this into a mergable section, because
139 // the linker doesn't take relocations into consideration when it tries to
140 // merge entries in the section.
141 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000142 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000143
144 // Otherwise, the dynamic linker needs to fix it up, put it in the
145 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000146 return SectionKind::getReadOnlyWithRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000147
148 case Constant::GlobalRelocations:
149 // In static relocation model, the linker will resolve all addresses, so
150 // the relocation entries will actually be constants by the time the app
151 // starts up. However, we can't put this into a mergable section, because
152 // the linker doesn't take relocations into consideration when it tries to
153 // merge entries in the section.
154 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000155 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000156
157 // Otherwise, the dynamic linker needs to fix it up, put it in the
158 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000159 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000160 }
161 }
162
163 // Okay, this isn't a constant. If the initializer for the global is going
164 // to require a runtime relocation by the dynamic linker, put it into a more
165 // specific section to improve startup time of the app. This coalesces these
166 // globals together onto fewer pages, improving the locality of the dynamic
167 // linker.
168 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000169 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000170
171 switch (C->getRelocationInfo()) {
172 default: llvm_unreachable("unknown relocation info kind");
173 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000174 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000175 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000176 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000177 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000178 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000179 }
180}
181
182/// SectionForGlobal - This method computes the appropriate section to emit
183/// the specified global variable or function definition. This should not
184/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000185const MCSection *TargetLoweringObjectFile::
Chris Lattnere53a6002009-07-29 05:09:30 +0000186SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
187 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000188 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
189 "Can only be used for global definitions");
190
Chris Lattner968ff112009-08-01 21:11:14 +0000191 SectionKind Kind = SectionKindForGlobal(GV, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000192
Chris Lattnerf0144122009-07-28 03:13:23 +0000193 // Select section name.
194 if (GV->hasSection()) {
195 // If the target has special section hacks for specifically named globals,
196 // return them now.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000197 if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind))
Chris Lattnerf0144122009-07-28 03:13:23 +0000198 return TS;
199
200 // If the target has magic semantics for certain section names, make sure to
201 // pick up the flags. This allows the user to write things with attribute
202 // section and still get the appropriate section flags printed.
Chris Lattner968ff112009-08-01 21:11:14 +0000203 Kind = getKindForNamedSection(GV->getSection().c_str(), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000204
Chris Lattner968ff112009-08-01 21:11:14 +0000205 return getOrCreateSection(GV->getSection().c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000206 }
207
208
209 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000210 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000211}
212
213// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000214const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000215TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000216 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000217 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000218 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000219 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000220
Chris Lattnerf9650c02009-08-01 21:46:23 +0000221 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000222 return getTextSection();
223
Chris Lattner82458382009-08-01 21:56:13 +0000224 if (Kind.isBSS() && BSSSection != 0)
225 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000226
Chris Lattnerf9650c02009-08-01 21:46:23 +0000227 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000228 return ReadOnlySection;
229
230 return getDataSection();
231}
232
Chris Lattner83d77fa2009-08-01 23:46:12 +0000233/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000234/// specified size and relocation information, return a section that it
235/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000236const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000237TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000238 if (Kind.isReadOnly() && ReadOnlySection != 0)
239 return ReadOnlySection;
240
241 return DataSection;
242}
243
244
Chris Lattnera87dea42009-07-31 18:48:30 +0000245const MCSection *TargetLoweringObjectFile::
Chris Lattner968ff112009-08-01 21:11:14 +0000246getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnera87dea42009-07-31 18:48:30 +0000247 if (MCSection *S = Ctx->GetSection(Name))
248 return S;
Chris Lattner968ff112009-08-01 21:11:14 +0000249 return MCSection::Create(Name, isDirective, Kind, *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +0000250}
251
252
253
254//===----------------------------------------------------------------------===//
255// ELF
256//===----------------------------------------------------------------------===//
257
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000258void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
259 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000260 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000261 if (!HasCrazyBSS)
Chris Lattner27981192009-08-01 23:57:16 +0000262 BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000263 else
264 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
265 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000266 // FIXME2: this should just be handle by the section printer. We should get
267 // away from syntactic view of the sections and MCSection should just be a
268 // semantic view.
Chris Lattner27981192009-08-01 23:57:16 +0000269 BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000270
271
Chris Lattner27981192009-08-01 23:57:16 +0000272 TextSection = getOrCreateSection("\t.text", true, SectionKind::getText());
273 DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000274 ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000275 getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000276 TLSDataSection =
Chris Lattner27981192009-08-01 23:57:16 +0000277 getOrCreateSection("\t.tdata", false, SectionKind::getThreadData());
Chris Lattner82458382009-08-01 21:56:13 +0000278 CStringSection = getOrCreateSection("\t.rodata.str", true,
Chris Lattner27981192009-08-01 23:57:16 +0000279 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000280
Chris Lattner968ff112009-08-01 21:11:14 +0000281 TLSBSSSection = getOrCreateSection("\t.tbss", false,
Chris Lattner27981192009-08-01 23:57:16 +0000282 SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000283
284 DataRelSection = getOrCreateSection("\t.data.rel", false,
Chris Lattner27981192009-08-01 23:57:16 +0000285 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000286 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000287 SectionKind::getDataRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000288 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
Chris Lattner27981192009-08-01 23:57:16 +0000289 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000290 DataRelROLocalSection =
291 getOrCreateSection("\t.data.rel.ro.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000292 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000293
294 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
Chris Lattner27981192009-08-01 23:57:16 +0000295 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000296 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
Chris Lattner27981192009-08-01 23:57:16 +0000297 SectionKind::getMergeableConst8());
Chris Lattnerf0144122009-07-28 03:13:23 +0000298 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
Chris Lattner27981192009-08-01 23:57:16 +0000299 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000300
301 StaticCtorSection =
302 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
303 StaticDtorSection =
304 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000305}
306
307
Chris Lattner968ff112009-08-01 21:11:14 +0000308SectionKind TargetLoweringObjectFileELF::
309getKindForNamedSection(const char *Name, SectionKind K) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000310 if (Name[0] != '.') return K;
311
312 // Some lame default implementation based on some magic section names.
313 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
314 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
315 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
316 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000317 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000318
319 if (strcmp(Name, ".tdata") == 0 ||
320 strncmp(Name, ".tdata.", 7) == 0 ||
321 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
322 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000323 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000324
325 if (strcmp(Name, ".tbss") == 0 ||
326 strncmp(Name, ".tbss.", 6) == 0 ||
327 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
328 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000329 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000330
331 return K;
332}
333
334void TargetLoweringObjectFileELF::
335getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
336 Str.push_back(',');
337 Str.push_back('"');
338
339 if (!Kind.isMetadata())
340 Str.push_back('a');
341 if (Kind.isText())
342 Str.push_back('x');
343 if (Kind.isWriteable())
344 Str.push_back('w');
Chris Lattner82987bf2009-07-31 16:17:13 +0000345 if (Kind.isMergeableCString() ||
346 Kind.isMergeableConst4() ||
347 Kind.isMergeableConst8() ||
348 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000349 Str.push_back('M');
350 if (Kind.isMergeableCString())
351 Str.push_back('S');
352 if (Kind.isThreadLocal())
353 Str.push_back('T');
354
355 Str.push_back('"');
356 Str.push_back(',');
357
358 // If comment string is '@', e.g. as on ARM - use '%' instead
359 if (AtIsCommentChar)
360 Str.push_back('%');
361 else
362 Str.push_back('@');
363
364 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000365 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000366 KindStr = "nobits";
367 else
368 KindStr = "progbits";
369
370 Str.append(KindStr, KindStr+strlen(KindStr));
371
372 if (Kind.isMergeableCString()) {
373 // TODO: Eventually handle multiple byte character strings. For now, all
374 // mergable C strings are single byte.
375 Str.push_back(',');
376 Str.push_back('1');
377 } else if (Kind.isMergeableConst4()) {
378 Str.push_back(',');
379 Str.push_back('4');
380 } else if (Kind.isMergeableConst8()) {
381 Str.push_back(',');
382 Str.push_back('8');
383 } else if (Kind.isMergeableConst16()) {
384 Str.push_back(',');
385 Str.push_back('1');
386 Str.push_back('6');
387 }
388}
389
390
391static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
392 if (Kind.isText()) return ".gnu.linkonce.t.";
393 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
394
395 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
396 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
397
398 if (Kind.isBSS()) return ".gnu.linkonce.b.";
399 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
400 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
401 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
402 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
403
404 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
405 return ".gnu.linkonce.d.rel.ro.";
406}
407
Chris Lattnera87dea42009-07-31 18:48:30 +0000408const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000409SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000410 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000411
412 // If this global is linkonce/weak and the target handles this by emitting it
413 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000414 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000415 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000416 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000417 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000418 }
419
Chris Lattnerf9650c02009-08-01 21:46:23 +0000420 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000421
Chris Lattnerf9650c02009-08-01 21:46:23 +0000422 if (Kind.isMergeableCString()) {
Chris Lattner82458382009-08-01 21:56:13 +0000423 assert(CStringSection && "Should have string section prefix");
Chris Lattnerf0144122009-07-28 03:13:23 +0000424
Chris Lattner067fe1a2009-07-29 04:54:38 +0000425 // We also need alignment here.
426 // FIXME: this is getting the alignment of the character, not the
427 // alignment of the global!
428 unsigned Align =
429 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000430
Chris Lattner82458382009-08-01 21:56:13 +0000431 std::string Name = CStringSection->getName() + "1." + utostr(Align);
Chris Lattner067fe1a2009-07-29 04:54:38 +0000432 return getOrCreateSection(Name.c_str(), false,
Chris Lattner27981192009-08-01 23:57:16 +0000433 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000434 }
435
Chris Lattnerf9650c02009-08-01 21:46:23 +0000436 if (Kind.isMergeableConst()) {
437 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000438 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000439 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000440 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000441 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000442 return MergeableConst16Section;
443 return ReadOnlySection; // .const
444 }
445
Chris Lattnerf9650c02009-08-01 21:46:23 +0000446 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000447
Chris Lattnerf9650c02009-08-01 21:46:23 +0000448 if (Kind.isThreadData()) return TLSDataSection;
449 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000450
Chris Lattner82458382009-08-01 21:56:13 +0000451 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000452
Chris Lattnerf9650c02009-08-01 21:46:23 +0000453 if (Kind.isDataNoRel()) return DataSection;
454 if (Kind.isDataRelLocal()) return DataRelLocalSection;
455 if (Kind.isDataRel()) return DataRelSection;
456 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000457
Chris Lattnerf9650c02009-08-01 21:46:23 +0000458 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000459 return DataRelROSection;
460}
461
Chris Lattner83d77fa2009-08-01 23:46:12 +0000462/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000463/// specified size and relocation information, return a section that it
464/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000465const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000466getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000467 if (Kind.isMergeableConst4())
468 return MergeableConst4Section;
469 if (Kind.isMergeableConst8())
470 return MergeableConst8Section;
471 if (Kind.isMergeableConst16())
472 return MergeableConst16Section;
473 if (Kind.isReadOnly())
474 return ReadOnlySection;
475
476 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
477 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
478 return DataRelROSection;
479}
480
481//===----------------------------------------------------------------------===//
482// MachO
483//===----------------------------------------------------------------------===//
484
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000485void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
486 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000487 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000488 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000489 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000490 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000491 SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000492
Chris Lattner82458382009-08-01 21:56:13 +0000493 CStringSection = getOrCreateSection("\t.cstring", true,
Chris Lattner27981192009-08-01 23:57:16 +0000494 SectionKind::getMergeableCString());
Chris Lattnerf0144122009-07-28 03:13:23 +0000495 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000496 SectionKind::getMergeableConst4());
Chris Lattnerf0144122009-07-28 03:13:23 +0000497 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000498 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000499
500 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
501 // to using it in -static mode.
502 if (TM.getRelocationModel() != Reloc::Static &&
503 TM.getTargetData()->getPointerSize() == 32)
504 SixteenByteConstantSection =
Chris Lattner968ff112009-08-01 21:11:14 +0000505 getOrCreateSection("\t.literal16\n", true,
Chris Lattner27981192009-08-01 23:57:16 +0000506 SectionKind::getMergeableConst16());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000507 else
508 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000509
Chris Lattner968ff112009-08-01 21:11:14 +0000510 ReadOnlySection = getOrCreateSection("\t.const", true,
Chris Lattner27981192009-08-01 23:57:16 +0000511 SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000512
513 TextCoalSection =
514 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Chris Lattner27981192009-08-01 23:57:16 +0000515 false, SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000516 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000517 false,
Chris Lattner27981192009-08-01 23:57:16 +0000518 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000519 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000520 false,
Chris Lattner27981192009-08-01 23:57:16 +0000521 SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +0000522 ConstDataSection = getOrCreateSection("\t.const_data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000523 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000524 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
Chris Lattner968ff112009-08-01 21:11:14 +0000525 false,
Chris Lattner27981192009-08-01 23:57:16 +0000526 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000527
528 if (TM.getRelocationModel() == Reloc::Static) {
529 StaticCtorSection =
530 getOrCreateSection(".constructor", true, SectionKind::getDataRel());
531 StaticDtorSection =
532 getOrCreateSection(".destructor", true, SectionKind::getDataRel());
533 } else {
534 StaticCtorSection =
535 getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel());
536 StaticDtorSection =
537 getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel());
538 }
539
Chris Lattnerf0144122009-07-28 03:13:23 +0000540}
541
Chris Lattnera87dea42009-07-31 18:48:30 +0000542const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000543SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000544 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000545 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000546
Chris Lattnerf9650c02009-08-01 21:46:23 +0000547 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000548 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000549
550 // If this is weak/linkonce, put this in a coalescable section, either in text
551 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000552 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000553 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000554 return ConstTextCoalSection;
555 return DataCoalSection;
556 }
557
558 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000559 if (Kind.isMergeableCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000560 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
561 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
562 const TargetData &TD = *TM.getTargetData();
563 unsigned Size = TD.getTypeAllocSize(Ty);
564 if (Size) {
565 unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV));
566 if (Align <= 32)
Chris Lattner82458382009-08-01 21:56:13 +0000567 return CStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000568 }
569
570 return ReadOnlySection;
571 }
572
Chris Lattnerf9650c02009-08-01 21:46:23 +0000573 if (Kind.isMergeableConst()) {
574 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000575 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000576 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000577 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000578 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000579 return SixteenByteConstantSection;
580 return ReadOnlySection; // .const
581 }
582
583 // FIXME: ROData -> const in -static mode that is relocatable but they happen
584 // by the static linker. Why not mergeable?
Chris Lattnerf9650c02009-08-01 21:46:23 +0000585 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000586 return ReadOnlySection;
587
588 // If this is marked const, put it into a const section. But if the dynamic
589 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000590 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000591 return ConstDataSection;
592
593 // Otherwise, just drop the variable in the normal data section.
594 return DataSection;
595}
596
Chris Lattnera87dea42009-07-31 18:48:30 +0000597const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000598TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000599 // If this constant requires a relocation, we have to put it in the data
600 // segment, not in the text segment.
601 if (Kind.isDataRel())
602 return ConstDataSection;
603
604 if (Kind.isMergeableConst4())
605 return FourByteConstantSection;
606 if (Kind.isMergeableConst8())
607 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000608 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000609 return SixteenByteConstantSection;
610 return ReadOnlySection; // .const
611}
612
Chris Lattner26630c12009-07-31 20:52:39 +0000613/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
614/// not to emit the UsedDirective for some symbols in llvm.used.
615// FIXME: REMOVE this (rdar://7071300)
616bool TargetLoweringObjectFileMachO::
617shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
618 /// On Darwin, internally linked data beginning with "L" or "l" does not have
619 /// the directive emitted (this occurs in ObjC metadata).
620 if (!GV) return false;
621
622 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
623 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
624 // FIXME: ObjC metadata is currently emitted as internal symbols that have
625 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
626 // this horrible hack can go away.
627 const std::string &Name = Mang->getMangledName(GV);
628 if (Name[0] == 'L' || Name[0] == 'l')
629 return false;
630 }
631
632 return true;
633}
634
635
Chris Lattnerf0144122009-07-28 03:13:23 +0000636//===----------------------------------------------------------------------===//
637// COFF
638//===----------------------------------------------------------------------===//
639
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000640void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
641 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000642 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner968ff112009-08-01 21:11:14 +0000643 TextSection = getOrCreateSection("\t.text", true,
Chris Lattner27981192009-08-01 23:57:16 +0000644 SectionKind::getText());
Chris Lattner968ff112009-08-01 21:11:14 +0000645 DataSection = getOrCreateSection("\t.data", true,
Chris Lattner27981192009-08-01 23:57:16 +0000646 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000647 StaticCtorSection =
648 getOrCreateSection(".ctors", false, SectionKind::getDataRel());
649 StaticDtorSection =
650 getOrCreateSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000651}
652
653void TargetLoweringObjectFileCOFF::
654getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
655 // FIXME: Inefficient.
656 std::string Res = ",\"";
657 if (Kind.isText())
658 Res += 'x';
659 if (Kind.isWriteable())
660 Res += 'w';
661 Res += "\"";
662
663 Str.append(Res.begin(), Res.end());
664}
665
666static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
667 if (Kind.isText())
668 return ".text$linkonce";
669 if (Kind.isWriteable())
670 return ".data$linkonce";
671 return ".rdata$linkonce";
672}
673
674
Chris Lattnera87dea42009-07-31 18:48:30 +0000675const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000676SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000677 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000678 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000679
680 // If this global is linkonce/weak and the target handles this by emitting it
681 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000682 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000683 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000684 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattnerf9650c02009-08-01 21:46:23 +0000685 return getOrCreateSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000686 }
687
Chris Lattnerf9650c02009-08-01 21:46:23 +0000688 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000689 return getTextSection();
690
Chris Lattnerf0144122009-07-28 03:13:23 +0000691 return getDataSection();
692}
693