blob: 93cb420d6285e06251ce990859ff4f0ad384bd13 [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 Lattner8c6ed052009-09-16 01:46:41 +000021#include "llvm/MC/MCExpr.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000022#include "llvm/MC/MCSectionMachO.h"
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000023#include "llvm/MC/MCSectionELF.h"
Chris Lattner8da8d4b2010-01-13 21:29:21 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000026#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000027#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattner8f9b0f62009-11-07 09:20:54 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattner8da8d4b2010-01-13 21:29:21 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattner5dc47ff2009-08-12 23:55:02 +000031#include "llvm/ADT/SmallString.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/ADT/StringExtras.h"
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// Generic Code
37//===----------------------------------------------------------------------===//
38
Chris Lattnera87dea42009-07-31 18:48:30 +000039TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000040 TextSection = 0;
41 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000042 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000043 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000044 StaticCtorSection = 0;
45 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000046 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000047 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000048
49 DwarfAbbrevSection = 0;
50 DwarfInfoSection = 0;
51 DwarfLineSection = 0;
52 DwarfFrameSection = 0;
53 DwarfPubNamesSection = 0;
54 DwarfPubTypesSection = 0;
55 DwarfDebugInlineSection = 0;
56 DwarfStrSection = 0;
57 DwarfLocSection = 0;
58 DwarfARangesSection = 0;
59 DwarfRangesSection = 0;
60 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000061}
62
63TargetLoweringObjectFile::~TargetLoweringObjectFile() {
64}
65
66static bool isSuitableForBSS(const GlobalVariable *GV) {
67 Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000068
Chris Lattnerf0144122009-07-28 03:13:23 +000069 // Must have zero initializer.
70 if (!C->isNullValue())
71 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000072
Chris Lattnerf0144122009-07-28 03:13:23 +000073 // Leave constant zeros in readonly constant sections, so they can be shared.
74 if (GV->isConstant())
75 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000076
Chris Lattnerf0144122009-07-28 03:13:23 +000077 // If the global has an explicit section specified, don't put it in BSS.
78 if (!GV->getSection().empty())
79 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000080
Chris Lattnerf0144122009-07-28 03:13:23 +000081 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
82 if (NoZerosInBSS)
83 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000084
Chris Lattnerf0144122009-07-28 03:13:23 +000085 // Otherwise, put it in BSS!
86 return true;
87}
88
Chris Lattner1850e5a2009-08-04 16:13:09 +000089/// IsNullTerminatedString - Return true if the specified constant (which is
90/// known to have a type that is an array of 1/2/4 byte elements) ends with a
91/// nul value and contains no other nuls in it.
92static bool IsNullTerminatedString(const Constant *C) {
93 const ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000094
Chris Lattnerf0144122009-07-28 03:13:23 +000095 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000096 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
97 if (ATy->getNumElements() == 0) return false;
98
99 ConstantInt *Null =
100 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
101 if (Null == 0 || Null->getZExtValue() != 0)
102 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000103
Chris Lattner1850e5a2009-08-04 16:13:09 +0000104 // Verify that the null doesn't occur anywhere else in the string.
105 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
106 // Reject constantexpr elements etc.
107 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
108 CVA->getOperand(i) == Null)
109 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000110 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000111 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000112
113 // Another possibility: [1 x i8] zeroinitializer
114 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000115 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000116
117 return false;
118}
119
Chris Lattner58bed8f2009-08-05 04:25:40 +0000120/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000121/// a global variable. Given an global variable and information from TM, it
122/// classifies the global in a variety of ways that make various target
123/// implementations simpler. The target implementation is free to ignore this
124/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000125SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
126 const TargetMachine &TM){
127 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
128 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000129
Chris Lattnerf0144122009-07-28 03:13:23 +0000130 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000131
Chris Lattnerf0144122009-07-28 03:13:23 +0000132 // Early exit - functions should be always in text sections.
133 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
134 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000135 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137 // Handle thread-local data first.
138 if (GVar->isThreadLocal()) {
139 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000140 return SectionKind::getThreadBSS();
141 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000142 }
143
144 // Variable can be easily put to BSS section.
145 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000146 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000147
148 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000149
Chris Lattnerf0144122009-07-28 03:13:23 +0000150 // If the global is marked constant, we can put it into a mergable section,
151 // a mergable string section, or general .data if it contains relocations.
152 if (GVar->isConstant()) {
153 // If the initializer for the global contains something that requires a
154 // relocation, then we may have to drop this into a wriable data section
155 // even though it is marked const.
156 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000157 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000158 case Constant::NoRelocation:
159 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000160 // section of the right width.
161 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000162 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000163 dyn_cast<IntegerType>(ATy->getElementType())) {
164 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
165 ITy->getBitWidth() == 32) &&
166 IsNullTerminatedString(C)) {
167 if (ITy->getBitWidth() == 8)
168 return SectionKind::getMergeable1ByteCString();
169 if (ITy->getBitWidth() == 16)
170 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000171
Chris Lattner1850e5a2009-08-04 16:13:09 +0000172 assert(ITy->getBitWidth() == 32 && "Unknown width");
173 return SectionKind::getMergeable4ByteCString();
174 }
175 }
176 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000177
Chris Lattnerf0144122009-07-28 03:13:23 +0000178 // Otherwise, just drop it into a mergable constant section. If we have
179 // a section for this size, use it, otherwise use the arbitrary sized
180 // mergable section.
181 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000182 case 4: return SectionKind::getMergeableConst4();
183 case 8: return SectionKind::getMergeableConst8();
184 case 16: return SectionKind::getMergeableConst16();
185 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000186 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000187
Chris Lattnerf0144122009-07-28 03:13:23 +0000188 case Constant::LocalRelocation:
189 // In static relocation model, the linker will resolve all addresses, so
190 // the relocation entries will actually be constants by the time the app
191 // starts up. However, we can't put this into a mergable section, because
192 // the linker doesn't take relocations into consideration when it tries to
193 // merge entries in the section.
194 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000195 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000196
Chris Lattnerf0144122009-07-28 03:13:23 +0000197 // Otherwise, the dynamic linker needs to fix it up, put it in the
198 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000199 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000200
Chris Lattnerf0144122009-07-28 03:13:23 +0000201 case Constant::GlobalRelocations:
202 // In static relocation model, the linker will resolve all addresses, so
203 // the relocation entries will actually be constants by the time the app
204 // starts up. However, we can't put this into a mergable section, because
205 // the linker doesn't take relocations into consideration when it tries to
206 // merge entries in the section.
207 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000208 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000209
Chris Lattnerf0144122009-07-28 03:13:23 +0000210 // Otherwise, the dynamic linker needs to fix it up, put it in the
211 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000212 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000213 }
214 }
215
216 // Okay, this isn't a constant. If the initializer for the global is going
217 // to require a runtime relocation by the dynamic linker, put it into a more
218 // specific section to improve startup time of the app. This coalesces these
219 // globals together onto fewer pages, improving the locality of the dynamic
220 // linker.
221 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000222 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000223
224 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000225 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000226 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000227 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000228 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000229 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000230 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000231 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000232 }
233}
234
235/// SectionForGlobal - This method computes the appropriate section to emit
236/// the specified global variable or function definition. This should not
237/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000238const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000239SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000240 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000241 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000242 if (GV->hasSection())
243 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000244
245
Chris Lattnerf0144122009-07-28 03:13:23 +0000246 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000247 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000248}
249
Chris Lattner58bed8f2009-08-05 04:25:40 +0000250
Chris Lattnerf0144122009-07-28 03:13:23 +0000251// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000252const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000253TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000254 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000255 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000256 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000257 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000258
Chris Lattnerf9650c02009-08-01 21:46:23 +0000259 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000260 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000261
Chris Lattner82458382009-08-01 21:56:13 +0000262 if (Kind.isBSS() && BSSSection != 0)
263 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000264
Chris Lattnerf9650c02009-08-01 21:46:23 +0000265 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000266 return ReadOnlySection;
267
268 return getDataSection();
269}
270
Chris Lattner83d77fa2009-08-01 23:46:12 +0000271/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000272/// specified size and relocation information, return a section that it
273/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000274const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000275TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000276 if (Kind.isReadOnly() && ReadOnlySection != 0)
277 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000278
Chris Lattnerf0144122009-07-28 03:13:23 +0000279 return DataSection;
280}
281
Chris Lattner8c6ed052009-09-16 01:46:41 +0000282/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
283/// pc-relative reference to the specified global variable from exception
284/// handling information. In addition to the symbol, this returns
285/// by-reference:
286///
287/// IsIndirect - True if the returned symbol is actually a stub that contains
288/// the address of the symbol, false if the symbol is the global itself.
289///
290/// IsPCRel - True if the symbol reference is already pc-relative, false if
291/// the caller needs to subtract off the address of the reference from the
292/// symbol.
293///
294const MCExpr *TargetLoweringObjectFile::
295getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000296 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000297 bool &IsIndirect, bool &IsPCRel) const {
298 // The generic implementation of this just returns a direct reference to the
299 // symbol.
300 IsIndirect = false;
301 IsPCRel = false;
302
Chris Lattner45111d12010-01-16 21:57:06 +0000303 // FIXME: Use GetGlobalValueSymbol.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000304 SmallString<128> Name;
305 Mang->getNameWithPrefix(Name, GV, false);
306 return MCSymbolRefExpr::Create(Name.str(), getContext());
307}
Chris Lattnerf0144122009-07-28 03:13:23 +0000308
Chris Lattnerf0144122009-07-28 03:13:23 +0000309
310//===----------------------------------------------------------------------===//
311// ELF
312//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000313typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
314
315TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
316 // If we have the section uniquing map, free it.
317 delete (ELFUniqueMapTy*)UniquingMap;
318}
Chris Lattnerf0144122009-07-28 03:13:23 +0000319
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000320const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000321getELFSection(StringRef Section, unsigned Type, unsigned Flags,
322 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000323 if (UniquingMap == 0)
324 UniquingMap = new ELFUniqueMapTy();
325 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000326
Chris Lattner38cff382009-08-13 00:37:15 +0000327 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000328 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000329 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000330
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000331 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
332 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000333}
334
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000335void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
336 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000337 if (UniquingMap != 0)
338 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000339 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000340
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000341 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000342 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
343 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
344 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000345
346 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000347 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
348 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
349 SectionKind::getText());
350
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000351 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000352 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
353 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
354 SectionKind::getDataRel());
355
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000356 ReadOnlySection =
357 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
358 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000359 SectionKind::getReadOnly());
360
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000361 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000362 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000363 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000364 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000365
366 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000367 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
368 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
369 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000370
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000371 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000372 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
373 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
374 SectionKind::getDataRel());
375
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000376 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000377 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
378 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
379 SectionKind::getDataRelLocal());
380
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000381 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000382 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
383 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
384 SectionKind::getReadOnlyWithRel());
385
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000386 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000387 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
388 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
389 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000390
391 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000392 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
393 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
394 SectionKind::getMergeableConst4());
395
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000396 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000397 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
398 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
399 SectionKind::getMergeableConst8());
400
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000401 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000402 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
403 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
404 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000405
406 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000407 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000408 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
409 SectionKind::getDataRel());
410
Chris Lattner80ec2792009-08-02 00:34:36 +0000411 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000412 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
413 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
414 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000415
Chris Lattner18a4c162009-08-02 07:24:22 +0000416 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000417
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000418 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
419 // it contains relocatable pointers. In PIC mode, this is probably a big
420 // runtime hit for C++ apps. Either the contents of the LSDA need to be
421 // adjusted or this should be a data section.
422 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000423 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
424 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000425 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000426 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000427 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
428 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000429
Chris Lattner18a4c162009-08-02 07:24:22 +0000430 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000431 DwarfAbbrevSection =
432 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000433 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000434 DwarfInfoSection =
435 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000436 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000437 DwarfLineSection =
438 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000439 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000440 DwarfFrameSection =
441 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000442 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000443 DwarfPubNamesSection =
444 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000445 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000446 DwarfPubTypesSection =
447 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000448 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000449 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000450 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
451 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000452 DwarfLocSection =
453 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000454 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000455 DwarfARangesSection =
456 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000457 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000458 DwarfRangesSection =
459 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000460 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000461 DwarfMacroInfoSection =
462 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000463 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000464}
465
466
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000467static SectionKind
Chris Lattner24f654c2009-08-06 16:39:58 +0000468getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000469 if (Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000470
Chris Lattnerf0144122009-07-28 03:13:23 +0000471 // Some lame default implementation based on some magic section names.
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000472 if (strcmp(Name, ".bss") == 0 ||
473 strncmp(Name, ".bss.", 5) == 0 ||
474 strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000475 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000476 strcmp(Name, ".sbss") == 0 ||
477 strncmp(Name, ".sbss.", 6) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000478 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
479 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000480 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000481
Chris Lattnerf0144122009-07-28 03:13:23 +0000482 if (strcmp(Name, ".tdata") == 0 ||
483 strncmp(Name, ".tdata.", 7) == 0 ||
484 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
485 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000486 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000487
Chris Lattnerf0144122009-07-28 03:13:23 +0000488 if (strcmp(Name, ".tbss") == 0 ||
489 strncmp(Name, ".tbss.", 6) == 0 ||
490 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
491 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000492 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000493
Chris Lattnerf0144122009-07-28 03:13:23 +0000494 return K;
495}
496
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000497
Chris Lattner48130352010-01-13 06:38:18 +0000498static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000499
Chris Lattner48130352010-01-13 06:38:18 +0000500 if (Name == ".init_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000501 return MCSectionELF::SHT_INIT_ARRAY;
502
Chris Lattner48130352010-01-13 06:38:18 +0000503 if (Name == ".fini_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000504 return MCSectionELF::SHT_FINI_ARRAY;
505
Chris Lattner48130352010-01-13 06:38:18 +0000506 if (Name == ".preinit_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000507 return MCSectionELF::SHT_PREINIT_ARRAY;
508
509 if (K.isBSS() || K.isThreadBSS())
510 return MCSectionELF::SHT_NOBITS;
511
512 return MCSectionELF::SHT_PROGBITS;
513}
514
515
516static unsigned
517getELFSectionFlags(SectionKind K) {
518 unsigned Flags = 0;
519
520 if (!K.isMetadata())
521 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000522
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000523 if (K.isText())
524 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000525
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000526 if (K.isWriteable())
527 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000528
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000529 if (K.isThreadLocal())
530 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000531
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000532 // K.isMergeableConst() is left out to honour PR4650
533 if (K.isMergeableCString() || K.isMergeableConst4() ||
534 K.isMergeableConst8() || K.isMergeableConst16())
535 Flags |= MCSectionELF::SHF_MERGE;
536
537 if (K.isMergeableCString())
538 Flags |= MCSectionELF::SHF_STRINGS;
539
540 return Flags;
541}
542
543
Chris Lattner24f654c2009-08-06 16:39:58 +0000544const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000545getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000546 Mangler *Mang, const TargetMachine &TM) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000547 const char *SectionName = GV->getSection().c_str();
548
Chris Lattner24f654c2009-08-06 16:39:58 +0000549 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000550 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000551
552 return getELFSection(SectionName,
553 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000554 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000555}
Chris Lattnerf0144122009-07-28 03:13:23 +0000556
557static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
558 if (Kind.isText()) return ".gnu.linkonce.t.";
559 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000560
Chris Lattnerf0144122009-07-28 03:13:23 +0000561 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
562 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000563
Chris Lattnerf0144122009-07-28 03:13:23 +0000564 if (Kind.isBSS()) return ".gnu.linkonce.b.";
565 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
566 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
567 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
568 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000569
Chris Lattnerf0144122009-07-28 03:13:23 +0000570 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
571 return ".gnu.linkonce.d.rel.ro.";
572}
573
Chris Lattnera87dea42009-07-31 18:48:30 +0000574const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000575SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000576 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000577
Chris Lattnerf0144122009-07-28 03:13:23 +0000578 // If this global is linkonce/weak and the target handles this by emitting it
579 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000580 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000581 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000582 SmallString<128> Name;
Chris Lattner48130352010-01-13 06:38:18 +0000583 Name.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner8da8d4b2010-01-13 21:29:21 +0000584 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000585 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
586 getELFSectionFlags(Kind), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000587 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000588
Chris Lattnerf9650c02009-08-01 21:46:23 +0000589 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000590
Chris Lattner3b24c012009-08-04 05:35:56 +0000591 if (Kind.isMergeable1ByteCString() ||
592 Kind.isMergeable2ByteCString() ||
593 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000594
Chris Lattner067fe1a2009-07-29 04:54:38 +0000595 // We also need alignment here.
596 // FIXME: this is getting the alignment of the character, not the
597 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000598 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000599 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000600
Chris Lattner7e88a502009-08-04 16:19:50 +0000601 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000602 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000603 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000604 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000605 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000606 else
607 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000608
609
Chris Lattner7e88a502009-08-04 16:19:50 +0000610 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000611 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
612 MCSectionELF::SHF_ALLOC |
613 MCSectionELF::SHF_MERGE |
614 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000615 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000616 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000617
Chris Lattnerf9650c02009-08-01 21:46:23 +0000618 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000619 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000620 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000621 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000622 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000623 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000624 return MergeableConst16Section;
625 return ReadOnlySection; // .const
626 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000627
Chris Lattnerf9650c02009-08-01 21:46:23 +0000628 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000629
Chris Lattnerf9650c02009-08-01 21:46:23 +0000630 if (Kind.isThreadData()) return TLSDataSection;
631 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000632
Chris Lattner82458382009-08-01 21:56:13 +0000633 if (Kind.isBSS()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000634
Chris Lattnerf9650c02009-08-01 21:46:23 +0000635 if (Kind.isDataNoRel()) return DataSection;
636 if (Kind.isDataRelLocal()) return DataRelLocalSection;
637 if (Kind.isDataRel()) return DataRelSection;
638 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000639
Chris Lattnerf9650c02009-08-01 21:46:23 +0000640 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000641 return DataRelROSection;
642}
643
Chris Lattner83d77fa2009-08-01 23:46:12 +0000644/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000645/// specified size and relocation information, return a section that it
646/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000647const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000648getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000649 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000650 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000651 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000652 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000653 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000654 return MergeableConst16Section;
655 if (Kind.isReadOnly())
656 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000657
Chris Lattnerf0144122009-07-28 03:13:23 +0000658 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
659 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
660 return DataRelROSection;
661}
662
663//===----------------------------------------------------------------------===//
664// MachO
665//===----------------------------------------------------------------------===//
666
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000667typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000668
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000669TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
670 // If we have the MachO uniquing map, free it.
671 delete (MachOUniqueMapTy*)UniquingMap;
672}
673
674
675const MCSectionMachO *TargetLoweringObjectFileMachO::
Daniel Dunbar2928c832009-11-06 10:58:06 +0000676getMachOSection(StringRef Segment, StringRef Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000677 unsigned TypeAndAttributes,
678 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000679 // We unique sections by their segment/section pair. The returned section
680 // may not have the same flags as the requested section, if so this should be
681 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000682
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000683 // Create the map if it doesn't already exist.
684 if (UniquingMap == 0)
685 UniquingMap = new MachOUniqueMapTy();
686 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000687
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000688 // Form the name to look up.
689 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000690 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000691 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000692 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000693
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000694 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000695 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000696 if (Entry) return Entry;
697
698 // Otherwise, return a new section.
699 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
700 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000701}
702
Chris Lattner11e96572009-08-03 21:53:27 +0000703
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000704void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
705 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000706 if (UniquingMap != 0)
707 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000708 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000709
Chris Lattnerff4bc462009-08-10 01:39:42 +0000710 TextSection // .text
711 = getMachOSection("__TEXT", "__text",
712 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
713 SectionKind::getText());
714 DataSection // .data
715 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000716
Chris Lattnerff4bc462009-08-10 01:39:42 +0000717 CStringSection // .cstring
718 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
719 SectionKind::getMergeable1ByteCString());
720 UStringSection
721 = getMachOSection("__TEXT","__ustring", 0,
722 SectionKind::getMergeable2ByteCString());
723 FourByteConstantSection // .literal4
724 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
725 SectionKind::getMergeableConst4());
726 EightByteConstantSection // .literal8
727 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
728 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000729
Chris Lattner4bb253c2009-07-28 17:50:28 +0000730 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
731 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000732 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000733 if (TM.getRelocationModel() != Reloc::Static &&
734 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000735 SixteenByteConstantSection = // .literal16
736 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000737 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000738
Chris Lattnerff4bc462009-08-10 01:39:42 +0000739 ReadOnlySection // .const
740 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000741
Chris Lattnerff4bc462009-08-10 01:39:42 +0000742 TextCoalSection
743 = getMachOSection("__TEXT", "__textcoal_nt",
744 MCSectionMachO::S_COALESCED |
745 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
746 SectionKind::getText());
747 ConstTextCoalSection
748 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
749 SectionKind::getText());
750 ConstDataCoalSection
751 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
752 SectionKind::getText());
753 ConstDataSection // .const_data
754 = getMachOSection("__DATA", "__const", 0,
755 SectionKind::getReadOnlyWithRel());
756 DataCoalSection
757 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
758 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000759
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000760
Chris Lattnere309cfa2009-08-13 00:05:07 +0000761 LazySymbolPointerSection
762 = getMachOSection("__DATA", "__la_symbol_ptr",
763 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
764 SectionKind::getMetadata());
765 NonLazySymbolPointerSection
766 = getMachOSection("__DATA", "__nl_symbol_ptr",
767 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
768 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000769
Chris Lattner80ec2792009-08-02 00:34:36 +0000770 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000771 StaticCtorSection
772 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
773 StaticDtorSection
774 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000775 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000776 StaticCtorSection
777 = getMachOSection("__DATA", "__mod_init_func",
778 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
779 SectionKind::getDataRel());
780 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000781 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000782 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
783 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000784 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000785
Chris Lattner18a4c162009-08-02 07:24:22 +0000786 // Exception Handling.
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000787 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
788 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000789 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000790 getMachOSection("__TEXT", "__eh_frame",
791 MCSectionMachO::S_COALESCED |
792 MCSectionMachO::S_ATTR_NO_TOC |
793 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
794 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
795 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000796
797 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000798 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000799 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000800 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000801 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000802 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000803 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000804 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000805 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000806 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000807 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000808 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000809 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000810 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000811 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000812 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000813 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000814 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000815 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000816 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000817 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000818 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000819 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000820 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000821 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000822 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000823 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000824 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000825 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000826 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000827 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000828 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000829 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000830 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000831 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000832 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000833 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000834}
835
Chris Lattnera87dea42009-07-31 18:48:30 +0000836const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000837getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000838 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000839 // Parse the section specifier and create it if valid.
840 StringRef Segment, Section;
841 unsigned TAA, StubSize;
842 std::string ErrorCode =
843 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
844 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000845 if (!ErrorCode.empty()) {
846 // If invalid, report the error with llvm_report_error.
847 llvm_report_error("Global variable '" + GV->getNameStr() +
848 "' has an invalid section specifier '" + GV->getSection()+
849 "': " + ErrorCode + ".");
850 // Fall back to dropping it into the data section.
851 return DataSection;
852 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000853
Chris Lattnere309cfa2009-08-13 00:05:07 +0000854 // Get the section.
855 const MCSectionMachO *S =
856 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000857
Chris Lattnere309cfa2009-08-13 00:05:07 +0000858 // Okay, now that we got the section, verify that the TAA & StubSize agree.
859 // If the user declared multiple globals with different section flags, we need
860 // to reject it here.
861 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
862 // If invalid, report the error with llvm_report_error.
863 llvm_report_error("Global variable '" + GV->getNameStr() +
864 "' section type or attributes does not match previous"
865 " section specifier");
866 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000867
Chris Lattnere309cfa2009-08-13 00:05:07 +0000868 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000869}
870
871const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000872SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000873 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000874 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000875
Chris Lattnerf9650c02009-08-01 21:46:23 +0000876 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000877 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000878
Chris Lattnerf0144122009-07-28 03:13:23 +0000879 // If this is weak/linkonce, put this in a coalescable section, either in text
880 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000881 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000882 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000883 return ConstTextCoalSection;
884 return DataCoalSection;
885 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000886
Chris Lattnerf0144122009-07-28 03:13:23 +0000887 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000888 if (Kind.isMergeable1ByteCString() ||
889 Kind.isMergeable2ByteCString()) {
890 if (TM.getTargetData()->getPreferredAlignment(
891 cast<GlobalVariable>(GV)) < 32) {
892 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000893 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000894 assert(Kind.isMergeable2ByteCString());
895 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000896 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000897 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000898
Chris Lattnerf9650c02009-08-01 21:46:23 +0000899 if (Kind.isMergeableConst()) {
900 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000901 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000902 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000903 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000904 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000905 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000906 }
Chris Lattnerec409752009-08-04 16:27:13 +0000907
908 // Otherwise, if it is readonly, but not something we can specially optimize,
909 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000910 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000911 return ReadOnlySection;
912
913 // If this is marked const, put it into a const section. But if the dynamic
914 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000915 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000916 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000917
Chris Lattnerf0144122009-07-28 03:13:23 +0000918 // Otherwise, just drop the variable in the normal data section.
919 return DataSection;
920}
921
Chris Lattnera87dea42009-07-31 18:48:30 +0000922const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000923TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000924 // If this constant requires a relocation, we have to put it in the data
925 // segment, not in the text segment.
Eric Christophera07b7502010-01-07 19:44:05 +0000926 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000927 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000928
Chris Lattnerf0144122009-07-28 03:13:23 +0000929 if (Kind.isMergeableConst4())
930 return FourByteConstantSection;
931 if (Kind.isMergeableConst8())
932 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000933 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000934 return SixteenByteConstantSection;
935 return ReadOnlySection; // .const
936}
937
Chris Lattner26630c12009-07-31 20:52:39 +0000938/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
939/// not to emit the UsedDirective for some symbols in llvm.used.
940// FIXME: REMOVE this (rdar://7071300)
941bool TargetLoweringObjectFileMachO::
942shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
943 /// On Darwin, internally linked data beginning with "L" or "l" does not have
944 /// the directive emitted (this occurs in ObjC metadata).
945 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000946
Chris Lattner26630c12009-07-31 20:52:39 +0000947 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
948 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
949 // FIXME: ObjC metadata is currently emitted as internal symbols that have
950 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
951 // this horrible hack can go away.
Chris Lattner0b3735e2010-01-16 02:16:09 +0000952 SmallString<64> Name;
Chris Lattner036d8f92010-01-16 03:38:27 +0000953 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner26630c12009-07-31 20:52:39 +0000954 if (Name[0] == 'L' || Name[0] == 'l')
955 return false;
956 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000957
Chris Lattner26630c12009-07-31 20:52:39 +0000958 return true;
959}
960
Chris Lattner8c6ed052009-09-16 01:46:41 +0000961const MCExpr *TargetLoweringObjectFileMachO::
962getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000963 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000964 bool &IsIndirect, bool &IsPCRel) const {
965 // The mach-o version of this method defaults to returning a stub reference.
966 IsIndirect = true;
967 IsPCRel = false;
968
969 SmallString<128> Name;
970 Mang->getNameWithPrefix(Name, GV, true);
971 Name += "$non_lazy_ptr";
972 return MCSymbolRefExpr::Create(Name.str(), getContext());
973}
974
Chris Lattner26630c12009-07-31 20:52:39 +0000975
Chris Lattnerf0144122009-07-28 03:13:23 +0000976//===----------------------------------------------------------------------===//
977// COFF
978//===----------------------------------------------------------------------===//
979
Chris Lattner38cff382009-08-13 00:37:15 +0000980typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
981
982TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
983 delete (COFFUniqueMapTy*)UniquingMap;
984}
985
Chris Lattner0c0cb712009-08-08 20:22:20 +0000986
Chris Lattner11e96572009-08-03 21:53:27 +0000987const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner48130352010-01-13 06:38:18 +0000988getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000989 // Create the map if it doesn't already exist.
990 if (UniquingMap == 0)
991 UniquingMap = new MachOUniqueMapTy();
992 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000993
Chris Lattner38cff382009-08-13 00:37:15 +0000994 // Do the lookup, if we have a hit, return it.
995 const MCSectionCOFF *&Entry = Map[Name];
996 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000997
Chris Lattner38cff382009-08-13 00:37:15 +0000998 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000999}
1000
Chris Lattnerf26e03b2009-07-31 17:42:42 +00001001void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1002 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001003 if (UniquingMap != 0)
1004 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001005 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001006 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1007 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001008 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001009 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001010 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001011 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001012
Chris Lattner35c35312009-08-18 16:56:17 +00001013 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1014 // though it contains relocatable pointers. In PIC mode, this is probably a
1015 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1016 // adjusted or this should be a data section.
1017 LSDASection =
1018 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1019 EHFrameSection =
1020 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001021
Chris Lattner18a4c162009-08-02 07:24:22 +00001022 // Debug info.
1023 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001024 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001025 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1026 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001027 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001028 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1029 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001030 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001031 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1032 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001033 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001034 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1035 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001036 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001037 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1038 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001039 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001040 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1041 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001042 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001043 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1044 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001045 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001046 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1047 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001048 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001049 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1050 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001051 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001052 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1053 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001054 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001055 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1056 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001057}
1058
Chris Lattner24f654c2009-08-06 16:39:58 +00001059const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001060getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001061 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001062 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001063}
1064
Chris Lattnerf0144122009-07-28 03:13:23 +00001065static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1066 if (Kind.isText())
1067 return ".text$linkonce";
1068 if (Kind.isWriteable())
1069 return ".data$linkonce";
1070 return ".rdata$linkonce";
1071}
1072
1073
Chris Lattnera87dea42009-07-31 18:48:30 +00001074const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001075SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001076 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001077 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001078
Chris Lattnerf0144122009-07-28 03:13:23 +00001079 // If this global is linkonce/weak and the target handles this by emitting it
1080 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001081 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001082 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner48130352010-01-13 06:38:18 +00001083 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattnercab16cc2010-01-13 19:19:17 +00001084 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner48130352010-01-13 06:38:18 +00001085 return getCOFFSection(Name.str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001086 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001087
Chris Lattnerf9650c02009-08-01 21:46:23 +00001088 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001089 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001090
Chris Lattnerf0144122009-07-28 03:13:23 +00001091 return getDataSection();
1092}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001093