blob: 4be0b9e4e0904320939546315213565d1e5b0017 [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"
Dan Gohmanffef8ac2009-08-11 16:02:12 +000018#include "llvm/Function.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000019#include "llvm/GlobalVariable.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000020#include "llvm/MC/MCContext.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000022#include "llvm/MC/MCSectionELF.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000026#include "llvm/Support/Mangler.h"
Chris Lattner5dc47ff2009-08-12 23:55:02 +000027#include "llvm/ADT/SmallString.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/ADT/StringExtras.h"
29using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Generic Code
33//===----------------------------------------------------------------------===//
34
Chris Lattnera87dea42009-07-31 18:48:30 +000035TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000036 TextSection = 0;
37 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000038 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000039 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000040 StaticCtorSection = 0;
41 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000042 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000043 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000044
45 DwarfAbbrevSection = 0;
46 DwarfInfoSection = 0;
47 DwarfLineSection = 0;
48 DwarfFrameSection = 0;
49 DwarfPubNamesSection = 0;
50 DwarfPubTypesSection = 0;
51 DwarfDebugInlineSection = 0;
52 DwarfStrSection = 0;
53 DwarfLocSection = 0;
54 DwarfARangesSection = 0;
55 DwarfRangesSection = 0;
56 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000057}
58
59TargetLoweringObjectFile::~TargetLoweringObjectFile() {
60}
61
62static bool isSuitableForBSS(const GlobalVariable *GV) {
63 Constant *C = GV->getInitializer();
64
65 // Must have zero initializer.
66 if (!C->isNullValue())
67 return false;
68
69 // Leave constant zeros in readonly constant sections, so they can be shared.
70 if (GV->isConstant())
71 return false;
72
73 // If the global has an explicit section specified, don't put it in BSS.
74 if (!GV->getSection().empty())
75 return false;
76
77 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
78 if (NoZerosInBSS)
79 return false;
80
81 // Otherwise, put it in BSS!
82 return true;
83}
84
Chris Lattner1850e5a2009-08-04 16:13:09 +000085/// IsNullTerminatedString - Return true if the specified constant (which is
86/// known to have a type that is an array of 1/2/4 byte elements) ends with a
87/// nul value and contains no other nuls in it.
88static bool IsNullTerminatedString(const Constant *C) {
89 const ArrayType *ATy = cast<ArrayType>(C->getType());
90
Chris Lattnerf0144122009-07-28 03:13:23 +000091 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000092 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
93 if (ATy->getNumElements() == 0) return false;
94
95 ConstantInt *Null =
96 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
97 if (Null == 0 || Null->getZExtValue() != 0)
98 return false; // Not null terminated.
99
100 // Verify that the null doesn't occur anywhere else in the string.
101 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
102 // Reject constantexpr elements etc.
103 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
104 CVA->getOperand(i) == Null)
105 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000106 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000107 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000108
109 // Another possibility: [1 x i8] zeroinitializer
110 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000111 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000112
113 return false;
114}
115
Chris Lattner58bed8f2009-08-05 04:25:40 +0000116/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000117/// a global variable. Given an global variable and information from TM, it
118/// classifies the global in a variety of ways that make various target
119/// implementations simpler. The target implementation is free to ignore this
120/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000121SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
122 const TargetMachine &TM){
123 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
124 "Can only be used for global definitions");
125
Chris Lattnerf0144122009-07-28 03:13:23 +0000126 Reloc::Model ReloModel = TM.getRelocationModel();
127
128 // Early exit - functions should be always in text sections.
129 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
130 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000131 return SectionKind::getText();
Chris Lattnerf0144122009-07-28 03:13:23 +0000132
133 // Handle thread-local data first.
134 if (GVar->isThreadLocal()) {
135 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000136 return SectionKind::getThreadBSS();
137 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000138 }
139
140 // Variable can be easily put to BSS section.
141 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000142 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000143
144 Constant *C = GVar->getInitializer();
145
146 // If the global is marked constant, we can put it into a mergable section,
147 // a mergable string section, or general .data if it contains relocations.
148 if (GVar->isConstant()) {
149 // If the initializer for the global contains something that requires a
150 // relocation, then we may have to drop this into a wriable data section
151 // even though it is marked const.
152 switch (C->getRelocationInfo()) {
153 default: llvm_unreachable("unknown relocation info kind");
154 case Constant::NoRelocation:
155 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000156 // section of the right width.
157 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
158 if (const IntegerType *ITy =
159 dyn_cast<IntegerType>(ATy->getElementType())) {
160 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
161 ITy->getBitWidth() == 32) &&
162 IsNullTerminatedString(C)) {
163 if (ITy->getBitWidth() == 8)
164 return SectionKind::getMergeable1ByteCString();
165 if (ITy->getBitWidth() == 16)
166 return SectionKind::getMergeable2ByteCString();
167
168 assert(ITy->getBitWidth() == 32 && "Unknown width");
169 return SectionKind::getMergeable4ByteCString();
170 }
171 }
172 }
Chris Lattner3b24c012009-08-04 05:35:56 +0000173
Chris Lattnerf0144122009-07-28 03:13:23 +0000174 // Otherwise, just drop it into a mergable constant section. If we have
175 // a section for this size, use it, otherwise use the arbitrary sized
176 // mergable section.
177 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000178 case 4: return SectionKind::getMergeableConst4();
179 case 8: return SectionKind::getMergeableConst8();
180 case 16: return SectionKind::getMergeableConst16();
181 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000182 }
183
184 case Constant::LocalRelocation:
185 // In static relocation model, the linker will resolve all addresses, so
186 // the relocation entries will actually be constants by the time the app
187 // starts up. However, we can't put this into a mergable section, because
188 // the linker doesn't take relocations into consideration when it tries to
189 // merge entries in the section.
190 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000191 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000192
193 // Otherwise, the dynamic linker needs to fix it up, put it in the
194 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000195 return SectionKind::getReadOnlyWithRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000196
197 case Constant::GlobalRelocations:
198 // In static relocation model, the linker will resolve all addresses, so
199 // the relocation entries will actually be constants by the time the app
200 // starts up. However, we can't put this into a mergable section, because
201 // the linker doesn't take relocations into consideration when it tries to
202 // merge entries in the section.
203 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000204 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000205
206 // Otherwise, the dynamic linker needs to fix it up, put it in the
207 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000208 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000209 }
210 }
211
212 // Okay, this isn't a constant. If the initializer for the global is going
213 // to require a runtime relocation by the dynamic linker, put it into a more
214 // specific section to improve startup time of the app. This coalesces these
215 // globals together onto fewer pages, improving the locality of the dynamic
216 // linker.
217 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000218 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000219
220 switch (C->getRelocationInfo()) {
221 default: llvm_unreachable("unknown relocation info kind");
222 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000223 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000224 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000225 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000226 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000227 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000228 }
229}
230
231/// SectionForGlobal - This method computes the appropriate section to emit
232/// the specified global variable or function definition. This should not
233/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000234const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000235SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000236 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000238 if (GV->hasSection())
239 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
240
Chris Lattnerf0144122009-07-28 03:13:23 +0000241
242 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000243 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000244}
245
Chris Lattner58bed8f2009-08-05 04:25:40 +0000246
Chris Lattnerf0144122009-07-28 03:13:23 +0000247// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000248const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000249TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000250 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000251 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000252 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000253 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000254
Chris Lattnerf9650c02009-08-01 21:46:23 +0000255 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000256 return getTextSection();
257
Chris Lattner82458382009-08-01 21:56:13 +0000258 if (Kind.isBSS() && BSSSection != 0)
259 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000260
Chris Lattnerf9650c02009-08-01 21:46:23 +0000261 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000262 return ReadOnlySection;
263
264 return getDataSection();
265}
266
Chris Lattner83d77fa2009-08-01 23:46:12 +0000267/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000268/// specified size and relocation information, return a section that it
269/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000270const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000271TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000272 if (Kind.isReadOnly() && ReadOnlySection != 0)
273 return ReadOnlySection;
274
275 return DataSection;
276}
277
278
Chris Lattnerf0144122009-07-28 03:13:23 +0000279
280//===----------------------------------------------------------------------===//
281// ELF
282//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000283typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
284
285TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
286 // If we have the section uniquing map, free it.
287 delete (ELFUniqueMapTy*)UniquingMap;
288}
Chris Lattnerf0144122009-07-28 03:13:23 +0000289
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000290const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000291getELFSection(StringRef Section, unsigned Type, unsigned Flags,
292 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000293 if (UniquingMap == 0)
294 UniquingMap = new ELFUniqueMapTy();
295 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
296
297 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000298 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000299 if (Entry) return Entry;
300
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000301 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
302 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000303}
304
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000305void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
306 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000307 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000308
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000309 BSSSection =
310 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
311 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
312 SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000313
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000314 TextSection =
315 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
316 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
317 SectionKind::getText());
318
319 DataSection =
320 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
321 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
322 SectionKind::getDataRel());
323
324 ReadOnlySection =
325 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
326 MCSectionELF::SHF_ALLOC,
327 SectionKind::getReadOnly());
328
329 TLSDataSection =
330 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
331 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
332 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Chris Lattner3b24c012009-08-04 05:35:56 +0000333
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000334 TLSBSSSection =
335 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
336 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
337 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000338
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000339 DataRelSection =
340 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
341 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
342 SectionKind::getDataRel());
343
344 DataRelLocalSection =
345 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
346 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
347 SectionKind::getDataRelLocal());
348
349 DataRelROSection =
350 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
351 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
352 SectionKind::getReadOnlyWithRel());
353
354 DataRelROLocalSection =
355 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
356 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
357 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000358
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000359 MergeableConst4Section =
360 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
361 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
362 SectionKind::getMergeableConst4());
363
364 MergeableConst8Section =
365 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
366 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
367 SectionKind::getMergeableConst8());
368
369 MergeableConst16Section =
370 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
371 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
372 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000373
374 StaticCtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000375 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
376 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
377 SectionKind::getDataRel());
378
Chris Lattner80ec2792009-08-02 00:34:36 +0000379 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000380 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
381 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
382 SectionKind::getDataRel());
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000383
Chris Lattner18a4c162009-08-02 07:24:22 +0000384 // Exception Handling Sections.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000385
386 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
387 // it contains relocatable pointers. In PIC mode, this is probably a big
388 // runtime hit for C++ apps. Either the contents of the LSDA need to be
389 // adjusted or this should be a data section.
390 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000391 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
392 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000393 EHFrameSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000394 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
395 MCSectionELF::SHF_ALLOC, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000396
397 // Debug Info Sections.
398 DwarfAbbrevSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000399 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
400 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000401 DwarfInfoSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000402 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
403 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000404 DwarfLineSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000405 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
406 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000407 DwarfFrameSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000408 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
409 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000410 DwarfPubNamesSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000411 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
412 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000413 DwarfPubTypesSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000414 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
415 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000416 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000417 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
418 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000419 DwarfLocSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000420 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
421 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000422 DwarfARangesSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000423 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
424 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000425 DwarfRangesSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000426 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
427 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000428 DwarfMacroInfoSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000429 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
430 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000431}
432
433
Chris Lattner24f654c2009-08-06 16:39:58 +0000434static SectionKind
435getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000436 if (Name[0] != '.') return K;
437
438 // Some lame default implementation based on some magic section names.
439 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
440 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
441 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
442 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000443 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000444
445 if (strcmp(Name, ".tdata") == 0 ||
446 strncmp(Name, ".tdata.", 7) == 0 ||
447 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
448 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000449 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000450
451 if (strcmp(Name, ".tbss") == 0 ||
452 strncmp(Name, ".tbss.", 6) == 0 ||
453 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
454 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000455 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000456
457 return K;
458}
459
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000460
461static unsigned
462getELFSectionType(const char *Name, SectionKind K) {
463
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000464 if (strcmp(Name, ".init_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000465 return MCSectionELF::SHT_INIT_ARRAY;
466
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000467 if (strcmp(Name, ".fini_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000468 return MCSectionELF::SHT_FINI_ARRAY;
469
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000470 if (strcmp(Name, ".preinit_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000471 return MCSectionELF::SHT_PREINIT_ARRAY;
472
473 if (K.isBSS() || K.isThreadBSS())
474 return MCSectionELF::SHT_NOBITS;
475
476 return MCSectionELF::SHT_PROGBITS;
477}
478
479
480static unsigned
481getELFSectionFlags(SectionKind K) {
482 unsigned Flags = 0;
483
484 if (!K.isMetadata())
485 Flags |= MCSectionELF::SHF_ALLOC;
486
487 if (K.isWriteable())
488 Flags |= MCSectionELF::SHF_WRITE;
489
490 if (K.isThreadLocal())
491 Flags |= MCSectionELF::SHF_TLS;
492
493 // K.isMergeableConst() is left out to honour PR4650
494 if (K.isMergeableCString() || K.isMergeableConst4() ||
495 K.isMergeableConst8() || K.isMergeableConst16())
496 Flags |= MCSectionELF::SHF_MERGE;
497
498 if (K.isMergeableCString())
499 Flags |= MCSectionELF::SHF_STRINGS;
500
501 return Flags;
502}
503
504
Chris Lattner24f654c2009-08-06 16:39:58 +0000505const MCSection *TargetLoweringObjectFileELF::
506getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
507 Mangler *Mang, const TargetMachine &TM) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000508 const char *SectionName = GV->getSection().c_str();
509
Chris Lattner24f654c2009-08-06 16:39:58 +0000510 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000511 Kind = getELFKindForNamedSection(SectionName, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000512
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000513 return getELFSection(SectionName,
514 getELFSectionType(SectionName, Kind),
515 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000516}
Chris Lattnerf0144122009-07-28 03:13:23 +0000517
518static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
519 if (Kind.isText()) return ".gnu.linkonce.t.";
520 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
521
522 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
523 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
524
525 if (Kind.isBSS()) return ".gnu.linkonce.b.";
526 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
527 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
528 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
529 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
530
531 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
532 return ".gnu.linkonce.d.rel.ro.";
533}
534
Chris Lattnera87dea42009-07-31 18:48:30 +0000535const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000536SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000537 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000538
539 // If this global is linkonce/weak and the target handles this by emitting it
540 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000541 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000542 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000543 std::string Name = Mang->makeNameProper(GV->getNameStr());
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000544
545 return getELFSection((Prefix+Name).c_str(),
546 getELFSectionType((Prefix+Name).c_str(), Kind),
547 getELFSectionFlags(Kind),
548 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000549 }
550
Chris Lattnerf9650c02009-08-01 21:46:23 +0000551 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000552
Chris Lattner3b24c012009-08-04 05:35:56 +0000553 if (Kind.isMergeable1ByteCString() ||
554 Kind.isMergeable2ByteCString() ||
555 Kind.isMergeable4ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000556
Chris Lattner067fe1a2009-07-29 04:54:38 +0000557 // We also need alignment here.
558 // FIXME: this is getting the alignment of the character, not the
559 // alignment of the global!
560 unsigned Align =
561 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000562
Chris Lattner7e88a502009-08-04 16:19:50 +0000563 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000564 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000565 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000566 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000567 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000568 else
569 assert(Kind.isMergeable1ByteCString() && "unknown string width");
570
571
Chris Lattner7e88a502009-08-04 16:19:50 +0000572 std::string Name = SizeSpec + utostr(Align);
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000573 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
574 MCSectionELF::SHF_ALLOC |
575 MCSectionELF::SHF_MERGE |
576 MCSectionELF::SHF_STRINGS,
577 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000578 }
579
Chris Lattnerf9650c02009-08-01 21:46:23 +0000580 if (Kind.isMergeableConst()) {
581 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000582 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000583 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000584 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000585 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000586 return MergeableConst16Section;
587 return ReadOnlySection; // .const
588 }
589
Chris Lattnerf9650c02009-08-01 21:46:23 +0000590 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000591
Chris Lattnerf9650c02009-08-01 21:46:23 +0000592 if (Kind.isThreadData()) return TLSDataSection;
593 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000594
Chris Lattner82458382009-08-01 21:56:13 +0000595 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000596
Chris Lattnerf9650c02009-08-01 21:46:23 +0000597 if (Kind.isDataNoRel()) return DataSection;
598 if (Kind.isDataRelLocal()) return DataRelLocalSection;
599 if (Kind.isDataRel()) return DataRelSection;
600 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000601
Chris Lattnerf9650c02009-08-01 21:46:23 +0000602 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000603 return DataRelROSection;
604}
605
Chris Lattner83d77fa2009-08-01 23:46:12 +0000606/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000607/// specified size and relocation information, return a section that it
608/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000609const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000610getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000611 if (Kind.isMergeableConst4())
612 return MergeableConst4Section;
613 if (Kind.isMergeableConst8())
614 return MergeableConst8Section;
615 if (Kind.isMergeableConst16())
616 return MergeableConst16Section;
617 if (Kind.isReadOnly())
618 return ReadOnlySection;
619
620 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
621 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
622 return DataRelROSection;
623}
624
625//===----------------------------------------------------------------------===//
626// MachO
627//===----------------------------------------------------------------------===//
628
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000629typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000630
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000631TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
632 // If we have the MachO uniquing map, free it.
633 delete (MachOUniqueMapTy*)UniquingMap;
634}
635
636
637const MCSectionMachO *TargetLoweringObjectFileMachO::
Chris Lattnerd3c4486f2009-08-12 23:34:27 +0000638getMachOSection(const StringRef &Segment, const StringRef &Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000639 unsigned TypeAndAttributes,
640 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000641 // We unique sections by their segment/section pair. The returned section
642 // may not have the same flags as the requested section, if so this should be
643 // diagnosed by the client as an error.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000644
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000645 // Create the map if it doesn't already exist.
646 if (UniquingMap == 0)
647 UniquingMap = new MachOUniqueMapTy();
648 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
649
650 // Form the name to look up.
651 SmallString<64> Name;
652 Name.append(Segment.begin(), Segment.end());
653 Name.push_back(',');
654 Name.append(Section.begin(), Section.end());
655
656 // Do the lookup, if we have a hit, return it.
657 const MCSectionMachO *&Entry = Map[StringRef(Name.data(), Name.size())];
658 if (Entry) return Entry;
659
660 // Otherwise, return a new section.
661 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
662 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000663}
664
Chris Lattner11e96572009-08-03 21:53:27 +0000665
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000666void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
667 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000668 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000669
Chris Lattnerff4bc462009-08-10 01:39:42 +0000670 TextSection // .text
671 = getMachOSection("__TEXT", "__text",
672 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
673 SectionKind::getText());
674 DataSection // .data
675 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
676
677 CStringSection // .cstring
678 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
679 SectionKind::getMergeable1ByteCString());
680 UStringSection
681 = getMachOSection("__TEXT","__ustring", 0,
682 SectionKind::getMergeable2ByteCString());
683 FourByteConstantSection // .literal4
684 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
685 SectionKind::getMergeableConst4());
686 EightByteConstantSection // .literal8
687 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
688 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000689
690 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
691 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000692 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000693 if (TM.getRelocationModel() != Reloc::Static &&
694 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000695 SixteenByteConstantSection = // .literal16
696 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000697 SectionKind::getMergeableConst16());
Chris Lattnerf0144122009-07-28 03:13:23 +0000698
Chris Lattnerff4bc462009-08-10 01:39:42 +0000699 ReadOnlySection // .const
700 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000701
Chris Lattnerff4bc462009-08-10 01:39:42 +0000702 TextCoalSection
703 = getMachOSection("__TEXT", "__textcoal_nt",
704 MCSectionMachO::S_COALESCED |
705 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
706 SectionKind::getText());
707 ConstTextCoalSection
708 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
709 SectionKind::getText());
710 ConstDataCoalSection
711 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
712 SectionKind::getText());
713 ConstDataSection // .const_data
714 = getMachOSection("__DATA", "__const", 0,
715 SectionKind::getReadOnlyWithRel());
716 DataCoalSection
717 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
718 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000719
Chris Lattnere309cfa2009-08-13 00:05:07 +0000720
721 LazySymbolPointerSection
722 = getMachOSection("__DATA", "__la_symbol_ptr",
723 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
724 SectionKind::getMetadata());
725 NonLazySymbolPointerSection
726 = getMachOSection("__DATA", "__nl_symbol_ptr",
727 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
728 SectionKind::getMetadata());
729
Chris Lattner80ec2792009-08-02 00:34:36 +0000730 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000731 StaticCtorSection
732 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
733 StaticDtorSection
734 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000735 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000736 StaticCtorSection
737 = getMachOSection("__DATA", "__mod_init_func",
738 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
739 SectionKind::getDataRel());
740 StaticDtorSection
741 = getMachOSection("__DATA", "__mod_term_func",
742 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
743 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000744 }
745
Chris Lattner18a4c162009-08-02 07:24:22 +0000746 // Exception Handling.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000747 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000748 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000749 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000750 getMachOSection("__TEXT", "__eh_frame",
751 MCSectionMachO::S_COALESCED |
752 MCSectionMachO::S_ATTR_NO_TOC |
753 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
754 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
755 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000756
757 // Debug Information.
Chris Lattner18a4c162009-08-02 07:24:22 +0000758 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000759 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000760 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000761 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000762 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000763 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000764 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000765 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000766 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000767 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000768 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000769 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000770 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000771 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000772 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000773 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000774 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000775 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000776 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000777 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000778 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000779 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000780 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000781 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000782 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000783 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000784 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000785 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000786 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000787 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000788 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000789 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000790 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000791 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000792 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000793 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000794}
795
Chris Lattnera87dea42009-07-31 18:48:30 +0000796const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner24f654c2009-08-06 16:39:58 +0000797getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
798 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000799 // Parse the section specifier and create it if valid.
800 StringRef Segment, Section;
801 unsigned TAA, StubSize;
802 std::string ErrorCode =
803 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
804 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000805 if (!ErrorCode.empty()) {
806 // If invalid, report the error with llvm_report_error.
807 llvm_report_error("Global variable '" + GV->getNameStr() +
808 "' has an invalid section specifier '" + GV->getSection()+
809 "': " + ErrorCode + ".");
810 // Fall back to dropping it into the data section.
811 return DataSection;
812 }
813
814 // Get the section.
815 const MCSectionMachO *S =
816 getMachOSection(Segment, Section, TAA, StubSize, Kind);
817
818 // Okay, now that we got the section, verify that the TAA & StubSize agree.
819 // If the user declared multiple globals with different section flags, we need
820 // to reject it here.
821 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
822 // If invalid, report the error with llvm_report_error.
823 llvm_report_error("Global variable '" + GV->getNameStr() +
824 "' section type or attributes does not match previous"
825 " section specifier");
826 }
Chris Lattnerff4bc462009-08-10 01:39:42 +0000827
Chris Lattnere309cfa2009-08-13 00:05:07 +0000828 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000829}
830
831const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000832SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000833 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000834 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000835
Chris Lattnerf9650c02009-08-01 21:46:23 +0000836 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000837 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000838
839 // If this is weak/linkonce, put this in a coalescable section, either in text
840 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000841 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000842 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000843 return ConstTextCoalSection;
844 return DataCoalSection;
845 }
846
847 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000848 if (Kind.isMergeable1ByteCString() ||
849 Kind.isMergeable2ByteCString()) {
850 if (TM.getTargetData()->getPreferredAlignment(
851 cast<GlobalVariable>(GV)) < 32) {
852 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000853 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000854 assert(Kind.isMergeable2ByteCString());
855 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000856 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000857 }
858
Chris Lattnerf9650c02009-08-01 21:46:23 +0000859 if (Kind.isMergeableConst()) {
860 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000861 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000862 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000863 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000864 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000865 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000866 }
Chris Lattnerec409752009-08-04 16:27:13 +0000867
868 // Otherwise, if it is readonly, but not something we can specially optimize,
869 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000870 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000871 return ReadOnlySection;
872
873 // If this is marked const, put it into a const section. But if the dynamic
874 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000875 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000876 return ConstDataSection;
877
878 // Otherwise, just drop the variable in the normal data section.
879 return DataSection;
880}
881
Chris Lattnera87dea42009-07-31 18:48:30 +0000882const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000883TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000884 // If this constant requires a relocation, we have to put it in the data
885 // segment, not in the text segment.
886 if (Kind.isDataRel())
887 return ConstDataSection;
888
889 if (Kind.isMergeableConst4())
890 return FourByteConstantSection;
891 if (Kind.isMergeableConst8())
892 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000893 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000894 return SixteenByteConstantSection;
895 return ReadOnlySection; // .const
896}
897
Chris Lattner26630c12009-07-31 20:52:39 +0000898/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
899/// not to emit the UsedDirective for some symbols in llvm.used.
900// FIXME: REMOVE this (rdar://7071300)
901bool TargetLoweringObjectFileMachO::
902shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
903 /// On Darwin, internally linked data beginning with "L" or "l" does not have
904 /// the directive emitted (this occurs in ObjC metadata).
905 if (!GV) return false;
906
907 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
908 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
909 // FIXME: ObjC metadata is currently emitted as internal symbols that have
910 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
911 // this horrible hack can go away.
912 const std::string &Name = Mang->getMangledName(GV);
913 if (Name[0] == 'L' || Name[0] == 'l')
914 return false;
915 }
916
917 return true;
918}
919
920
Chris Lattnerf0144122009-07-28 03:13:23 +0000921//===----------------------------------------------------------------------===//
922// COFF
923//===----------------------------------------------------------------------===//
924
Chris Lattner38cff382009-08-13 00:37:15 +0000925typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
926
927TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
928 delete (COFFUniqueMapTy*)UniquingMap;
929}
930
Chris Lattner0c0cb712009-08-08 20:22:20 +0000931
Chris Lattner11e96572009-08-03 21:53:27 +0000932const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000933getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000934 // Create the map if it doesn't already exist.
935 if (UniquingMap == 0)
936 UniquingMap = new MachOUniqueMapTy();
937 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
938
939 // Do the lookup, if we have a hit, return it.
940 const MCSectionCOFF *&Entry = Map[Name];
941 if (Entry) return Entry;
942
943 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000944}
945
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000946void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
947 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000948 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000949 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
950 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000951 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000952 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000953 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000954 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000955
956
957 // Debug info.
958 // FIXME: Don't use 'directive' mode here.
959 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000960 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
961 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000962 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000963 getCOFFSection("\t.section\t.debug_info,\"dr\"",
964 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000965 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000966 getCOFFSection("\t.section\t.debug_line,\"dr\"",
967 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000968 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000969 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
970 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000971 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000972 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
973 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000974 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000975 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
976 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000977 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000978 getCOFFSection("\t.section\t.debug_str,\"dr\"",
979 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000980 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000981 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
982 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000983 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000984 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
985 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000986 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000987 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
988 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000989 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000990 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
991 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000992}
993
Chris Lattner24f654c2009-08-06 16:39:58 +0000994const MCSection *TargetLoweringObjectFileCOFF::
995getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
996 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +0000997 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000998}
999
Chris Lattnerf0144122009-07-28 03:13:23 +00001000static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1001 if (Kind.isText())
1002 return ".text$linkonce";
1003 if (Kind.isWriteable())
1004 return ".data$linkonce";
1005 return ".rdata$linkonce";
1006}
1007
1008
Chris Lattnera87dea42009-07-31 18:48:30 +00001009const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001010SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001011 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001012 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +00001013
1014 // If this global is linkonce/weak and the target handles this by emitting it
1015 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001016 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001017 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +00001018 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +00001019 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001020 }
1021
Chris Lattnerf9650c02009-08-01 21:46:23 +00001022 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001023 return getTextSection();
1024
Chris Lattnerf0144122009-07-28 03:13:23 +00001025 return getDataSection();
1026}
1027