blob: 70e8008eb44a12b6318bad5ce9b8eec6a6a1a564 [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 Lattnerf0144122009-07-28 03:13:23 +000025#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000026#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000027#include "llvm/Target/TargetOptions.h"
Chris Lattner8f9b0f62009-11-07 09:20:54 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/Support/Mangler.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
303 SmallString<128> Name;
304 Mang->getNameWithPrefix(Name, GV, false);
305 return MCSymbolRefExpr::Create(Name.str(), getContext());
306}
Chris Lattnerf0144122009-07-28 03:13:23 +0000307
Chris Lattnerf0144122009-07-28 03:13:23 +0000308
309//===----------------------------------------------------------------------===//
310// ELF
311//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000312typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
313
314TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
315 // If we have the section uniquing map, free it.
316 delete (ELFUniqueMapTy*)UniquingMap;
317}
Chris Lattnerf0144122009-07-28 03:13:23 +0000318
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000319const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000320getELFSection(StringRef Section, unsigned Type, unsigned Flags,
321 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000322 if (UniquingMap == 0)
323 UniquingMap = new ELFUniqueMapTy();
324 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000325
Chris Lattner38cff382009-08-13 00:37:15 +0000326 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000327 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000328 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000329
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000330 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
331 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000332}
333
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000334void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
335 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000336 if (UniquingMap != 0)
337 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000338 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000339
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000340 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000341 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
342 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
343 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000344
345 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000346 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
347 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
348 SectionKind::getText());
349
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000350 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000351 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
352 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
353 SectionKind::getDataRel());
354
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000355 ReadOnlySection =
356 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
357 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000358 SectionKind::getReadOnly());
359
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000360 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000361 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000362 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000363 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000364
365 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000366 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
367 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
368 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000369
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000370 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000371 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
372 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
373 SectionKind::getDataRel());
374
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000375 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000376 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
377 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
378 SectionKind::getDataRelLocal());
379
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000380 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000381 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
382 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
383 SectionKind::getReadOnlyWithRel());
384
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000385 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000386 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
387 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
388 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000389
390 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000391 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
392 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
393 SectionKind::getMergeableConst4());
394
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000395 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000396 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
397 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
398 SectionKind::getMergeableConst8());
399
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000400 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000401 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
402 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
403 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000404
405 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000406 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000407 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
408 SectionKind::getDataRel());
409
Chris Lattner80ec2792009-08-02 00:34:36 +0000410 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000411 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
412 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
413 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000414
Chris Lattner18a4c162009-08-02 07:24:22 +0000415 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000416
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000417 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
418 // it contains relocatable pointers. In PIC mode, this is probably a big
419 // runtime hit for C++ apps. Either the contents of the LSDA need to be
420 // adjusted or this should be a data section.
421 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000422 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
423 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000424 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000425 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000426 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
427 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000428
Chris Lattner18a4c162009-08-02 07:24:22 +0000429 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000430 DwarfAbbrevSection =
431 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000432 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000433 DwarfInfoSection =
434 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000435 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000436 DwarfLineSection =
437 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000438 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000439 DwarfFrameSection =
440 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000441 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000442 DwarfPubNamesSection =
443 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000444 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000445 DwarfPubTypesSection =
446 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000447 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000448 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000449 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
450 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000451 DwarfLocSection =
452 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000453 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000454 DwarfARangesSection =
455 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000456 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000457 DwarfRangesSection =
458 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000459 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000460 DwarfMacroInfoSection =
461 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000462 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000463}
464
465
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000466static SectionKind
Chris Lattner24f654c2009-08-06 16:39:58 +0000467getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000468 if (Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000469
Chris Lattnerf0144122009-07-28 03:13:23 +0000470 // Some lame default implementation based on some magic section names.
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000471 if (strcmp(Name, ".bss") == 0 ||
472 strncmp(Name, ".bss.", 5) == 0 ||
473 strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000474 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000475 strcmp(Name, ".sbss") == 0 ||
476 strncmp(Name, ".sbss.", 6) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000477 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
478 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000479 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000480
Chris Lattnerf0144122009-07-28 03:13:23 +0000481 if (strcmp(Name, ".tdata") == 0 ||
482 strncmp(Name, ".tdata.", 7) == 0 ||
483 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
484 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000485 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000486
Chris Lattnerf0144122009-07-28 03:13:23 +0000487 if (strcmp(Name, ".tbss") == 0 ||
488 strncmp(Name, ".tbss.", 6) == 0 ||
489 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
490 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000491 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000492
Chris Lattnerf0144122009-07-28 03:13:23 +0000493 return K;
494}
495
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000496
Chris Lattner48130352010-01-13 06:38:18 +0000497static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000498
Chris Lattner48130352010-01-13 06:38:18 +0000499 if (Name == ".init_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000500 return MCSectionELF::SHT_INIT_ARRAY;
501
Chris Lattner48130352010-01-13 06:38:18 +0000502 if (Name == ".fini_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000503 return MCSectionELF::SHT_FINI_ARRAY;
504
Chris Lattner48130352010-01-13 06:38:18 +0000505 if (Name == ".preinit_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000506 return MCSectionELF::SHT_PREINIT_ARRAY;
507
508 if (K.isBSS() || K.isThreadBSS())
509 return MCSectionELF::SHT_NOBITS;
510
511 return MCSectionELF::SHT_PROGBITS;
512}
513
514
515static unsigned
516getELFSectionFlags(SectionKind K) {
517 unsigned Flags = 0;
518
519 if (!K.isMetadata())
520 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000521
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000522 if (K.isText())
523 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000524
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000525 if (K.isWriteable())
526 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000527
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000528 if (K.isThreadLocal())
529 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000530
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000531 // K.isMergeableConst() is left out to honour PR4650
532 if (K.isMergeableCString() || K.isMergeableConst4() ||
533 K.isMergeableConst8() || K.isMergeableConst16())
534 Flags |= MCSectionELF::SHF_MERGE;
535
536 if (K.isMergeableCString())
537 Flags |= MCSectionELF::SHF_STRINGS;
538
539 return Flags;
540}
541
542
Chris Lattner24f654c2009-08-06 16:39:58 +0000543const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000544getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000545 Mangler *Mang, const TargetMachine &TM) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000546 const char *SectionName = GV->getSection().c_str();
547
Chris Lattner24f654c2009-08-06 16:39:58 +0000548 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000549 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000550
551 return getELFSection(SectionName,
552 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000553 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000554}
Chris Lattnerf0144122009-07-28 03:13:23 +0000555
556static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
557 if (Kind.isText()) return ".gnu.linkonce.t.";
558 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000559
Chris Lattnerf0144122009-07-28 03:13:23 +0000560 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
561 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000562
Chris Lattnerf0144122009-07-28 03:13:23 +0000563 if (Kind.isBSS()) return ".gnu.linkonce.b.";
564 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
565 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
566 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
567 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000568
Chris Lattnerf0144122009-07-28 03:13:23 +0000569 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
570 return ".gnu.linkonce.d.rel.ro.";
571}
572
Chris Lattnera87dea42009-07-31 18:48:30 +0000573const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000574SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000575 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000576
Chris Lattnerf0144122009-07-28 03:13:23 +0000577 // If this global is linkonce/weak and the target handles this by emitting it
578 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000579 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000580 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattner8da8d4b2010-01-13 21:29:21 +0000581 SmallString<128> Name, MangledName;
Chris Lattner48130352010-01-13 06:38:18 +0000582 Name.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner8da8d4b2010-01-13 21:29:21 +0000583 Mang->getNameWithPrefix(Name, GV, false);
584
585 raw_svector_ostream OS(MangledName);
586 MCSymbol::printMangledName(Name, OS, 0);
587 OS.flush();
588
589 return getELFSection(MangledName.str(),
590 getELFSectionType(MangledName.str(), Kind),
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000591 getELFSectionFlags(Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000592 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000593 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000594
Chris Lattnerf9650c02009-08-01 21:46:23 +0000595 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000596
Chris Lattner3b24c012009-08-04 05:35:56 +0000597 if (Kind.isMergeable1ByteCString() ||
598 Kind.isMergeable2ByteCString() ||
599 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000600
Chris Lattner067fe1a2009-07-29 04:54:38 +0000601 // We also need alignment here.
602 // FIXME: this is getting the alignment of the character, not the
603 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000604 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000605 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000606
Chris Lattner7e88a502009-08-04 16:19:50 +0000607 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000608 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000609 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000610 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000611 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000612 else
613 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000614
615
Chris Lattner7e88a502009-08-04 16:19:50 +0000616 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000617 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
618 MCSectionELF::SHF_ALLOC |
619 MCSectionELF::SHF_MERGE |
620 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000621 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000622 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000623
Chris Lattnerf9650c02009-08-01 21:46:23 +0000624 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000625 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000626 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000627 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000628 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000629 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000630 return MergeableConst16Section;
631 return ReadOnlySection; // .const
632 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000633
Chris Lattnerf9650c02009-08-01 21:46:23 +0000634 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000635
Chris Lattnerf9650c02009-08-01 21:46:23 +0000636 if (Kind.isThreadData()) return TLSDataSection;
637 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000638
Chris Lattner82458382009-08-01 21:56:13 +0000639 if (Kind.isBSS()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000640
Chris Lattnerf9650c02009-08-01 21:46:23 +0000641 if (Kind.isDataNoRel()) return DataSection;
642 if (Kind.isDataRelLocal()) return DataRelLocalSection;
643 if (Kind.isDataRel()) return DataRelSection;
644 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000645
Chris Lattnerf9650c02009-08-01 21:46:23 +0000646 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000647 return DataRelROSection;
648}
649
Chris Lattner83d77fa2009-08-01 23:46:12 +0000650/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000651/// specified size and relocation information, return a section that it
652/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000653const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000654getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000655 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000656 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000657 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000658 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000659 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000660 return MergeableConst16Section;
661 if (Kind.isReadOnly())
662 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000663
Chris Lattnerf0144122009-07-28 03:13:23 +0000664 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
665 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
666 return DataRelROSection;
667}
668
669//===----------------------------------------------------------------------===//
670// MachO
671//===----------------------------------------------------------------------===//
672
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000673typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000674
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000675TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
676 // If we have the MachO uniquing map, free it.
677 delete (MachOUniqueMapTy*)UniquingMap;
678}
679
680
681const MCSectionMachO *TargetLoweringObjectFileMachO::
Daniel Dunbar2928c832009-11-06 10:58:06 +0000682getMachOSection(StringRef Segment, StringRef Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000683 unsigned TypeAndAttributes,
684 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000685 // We unique sections by their segment/section pair. The returned section
686 // may not have the same flags as the requested section, if so this should be
687 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000688
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000689 // Create the map if it doesn't already exist.
690 if (UniquingMap == 0)
691 UniquingMap = new MachOUniqueMapTy();
692 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000693
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000694 // Form the name to look up.
695 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000696 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000697 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000698 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000699
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000700 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000701 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000702 if (Entry) return Entry;
703
704 // Otherwise, return a new section.
705 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
706 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000707}
708
Chris Lattner11e96572009-08-03 21:53:27 +0000709
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000710void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
711 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000712 if (UniquingMap != 0)
713 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000714 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000715
Chris Lattnerff4bc462009-08-10 01:39:42 +0000716 TextSection // .text
717 = getMachOSection("__TEXT", "__text",
718 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
719 SectionKind::getText());
720 DataSection // .data
721 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000722
Chris Lattnerff4bc462009-08-10 01:39:42 +0000723 CStringSection // .cstring
724 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
725 SectionKind::getMergeable1ByteCString());
726 UStringSection
727 = getMachOSection("__TEXT","__ustring", 0,
728 SectionKind::getMergeable2ByteCString());
729 FourByteConstantSection // .literal4
730 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
731 SectionKind::getMergeableConst4());
732 EightByteConstantSection // .literal8
733 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
734 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000735
Chris Lattner4bb253c2009-07-28 17:50:28 +0000736 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
737 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000738 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000739 if (TM.getRelocationModel() != Reloc::Static &&
740 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000741 SixteenByteConstantSection = // .literal16
742 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000743 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000744
Chris Lattnerff4bc462009-08-10 01:39:42 +0000745 ReadOnlySection // .const
746 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000747
Chris Lattnerff4bc462009-08-10 01:39:42 +0000748 TextCoalSection
749 = getMachOSection("__TEXT", "__textcoal_nt",
750 MCSectionMachO::S_COALESCED |
751 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
752 SectionKind::getText());
753 ConstTextCoalSection
754 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
755 SectionKind::getText());
756 ConstDataCoalSection
757 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
758 SectionKind::getText());
759 ConstDataSection // .const_data
760 = getMachOSection("__DATA", "__const", 0,
761 SectionKind::getReadOnlyWithRel());
762 DataCoalSection
763 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
764 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000765
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000766
Chris Lattnere309cfa2009-08-13 00:05:07 +0000767 LazySymbolPointerSection
768 = getMachOSection("__DATA", "__la_symbol_ptr",
769 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
770 SectionKind::getMetadata());
771 NonLazySymbolPointerSection
772 = getMachOSection("__DATA", "__nl_symbol_ptr",
773 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
774 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000775
Chris Lattner80ec2792009-08-02 00:34:36 +0000776 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000777 StaticCtorSection
778 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
779 StaticDtorSection
780 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000781 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000782 StaticCtorSection
783 = getMachOSection("__DATA", "__mod_init_func",
784 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
785 SectionKind::getDataRel());
786 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000787 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000788 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
789 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000790 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000791
Chris Lattner18a4c162009-08-02 07:24:22 +0000792 // Exception Handling.
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000793 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
794 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000795 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000796 getMachOSection("__TEXT", "__eh_frame",
797 MCSectionMachO::S_COALESCED |
798 MCSectionMachO::S_ATTR_NO_TOC |
799 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
800 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
801 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000802
803 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000804 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000805 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000806 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000807 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000808 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000809 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000810 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000811 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000812 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000813 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000814 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000815 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000816 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000817 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000818 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000819 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000820 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000821 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000822 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000823 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000824 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000825 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000826 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000827 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000828 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000829 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000830 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000831 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000832 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000833 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000834 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000835 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000836 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000837 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000838 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000839 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000840}
841
Chris Lattnera87dea42009-07-31 18:48:30 +0000842const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000843getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000844 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000845 // Parse the section specifier and create it if valid.
846 StringRef Segment, Section;
847 unsigned TAA, StubSize;
848 std::string ErrorCode =
849 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
850 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000851 if (!ErrorCode.empty()) {
852 // If invalid, report the error with llvm_report_error.
853 llvm_report_error("Global variable '" + GV->getNameStr() +
854 "' has an invalid section specifier '" + GV->getSection()+
855 "': " + ErrorCode + ".");
856 // Fall back to dropping it into the data section.
857 return DataSection;
858 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000859
Chris Lattnere309cfa2009-08-13 00:05:07 +0000860 // Get the section.
861 const MCSectionMachO *S =
862 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000863
Chris Lattnere309cfa2009-08-13 00:05:07 +0000864 // Okay, now that we got the section, verify that the TAA & StubSize agree.
865 // If the user declared multiple globals with different section flags, we need
866 // to reject it here.
867 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
868 // If invalid, report the error with llvm_report_error.
869 llvm_report_error("Global variable '" + GV->getNameStr() +
870 "' section type or attributes does not match previous"
871 " section specifier");
872 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000873
Chris Lattnere309cfa2009-08-13 00:05:07 +0000874 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000875}
876
877const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000878SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000879 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000880 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000881
Chris Lattnerf9650c02009-08-01 21:46:23 +0000882 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000883 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000884
Chris Lattnerf0144122009-07-28 03:13:23 +0000885 // If this is weak/linkonce, put this in a coalescable section, either in text
886 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000887 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000888 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000889 return ConstTextCoalSection;
890 return DataCoalSection;
891 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000892
Chris Lattnerf0144122009-07-28 03:13:23 +0000893 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000894 if (Kind.isMergeable1ByteCString() ||
895 Kind.isMergeable2ByteCString()) {
896 if (TM.getTargetData()->getPreferredAlignment(
897 cast<GlobalVariable>(GV)) < 32) {
898 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000899 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000900 assert(Kind.isMergeable2ByteCString());
901 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000902 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000903 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000904
Chris Lattnerf9650c02009-08-01 21:46:23 +0000905 if (Kind.isMergeableConst()) {
906 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000907 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000908 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000909 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000910 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000911 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000912 }
Chris Lattnerec409752009-08-04 16:27:13 +0000913
914 // Otherwise, if it is readonly, but not something we can specially optimize,
915 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000916 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000917 return ReadOnlySection;
918
919 // If this is marked const, put it into a const section. But if the dynamic
920 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000921 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000922 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000923
Chris Lattnerf0144122009-07-28 03:13:23 +0000924 // Otherwise, just drop the variable in the normal data section.
925 return DataSection;
926}
927
Chris Lattnera87dea42009-07-31 18:48:30 +0000928const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000929TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000930 // If this constant requires a relocation, we have to put it in the data
931 // segment, not in the text segment.
Eric Christophera07b7502010-01-07 19:44:05 +0000932 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000933 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000934
Chris Lattnerf0144122009-07-28 03:13:23 +0000935 if (Kind.isMergeableConst4())
936 return FourByteConstantSection;
937 if (Kind.isMergeableConst8())
938 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000939 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000940 return SixteenByteConstantSection;
941 return ReadOnlySection; // .const
942}
943
Chris Lattner26630c12009-07-31 20:52:39 +0000944/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
945/// not to emit the UsedDirective for some symbols in llvm.used.
946// FIXME: REMOVE this (rdar://7071300)
947bool TargetLoweringObjectFileMachO::
948shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
949 /// On Darwin, internally linked data beginning with "L" or "l" does not have
950 /// the directive emitted (this occurs in ObjC metadata).
951 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000952
Chris Lattner26630c12009-07-31 20:52:39 +0000953 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
954 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
955 // FIXME: ObjC metadata is currently emitted as internal symbols that have
956 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
957 // this horrible hack can go away.
958 const std::string &Name = Mang->getMangledName(GV);
959 if (Name[0] == 'L' || Name[0] == 'l')
960 return false;
961 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000962
Chris Lattner26630c12009-07-31 20:52:39 +0000963 return true;
964}
965
Chris Lattner8c6ed052009-09-16 01:46:41 +0000966const MCExpr *TargetLoweringObjectFileMachO::
967getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000968 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000969 bool &IsIndirect, bool &IsPCRel) const {
970 // The mach-o version of this method defaults to returning a stub reference.
971 IsIndirect = true;
972 IsPCRel = false;
973
974 SmallString<128> Name;
975 Mang->getNameWithPrefix(Name, GV, true);
976 Name += "$non_lazy_ptr";
977 return MCSymbolRefExpr::Create(Name.str(), getContext());
978}
979
Chris Lattner26630c12009-07-31 20:52:39 +0000980
Chris Lattnerf0144122009-07-28 03:13:23 +0000981//===----------------------------------------------------------------------===//
982// COFF
983//===----------------------------------------------------------------------===//
984
Chris Lattner38cff382009-08-13 00:37:15 +0000985typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
986
987TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
988 delete (COFFUniqueMapTy*)UniquingMap;
989}
990
Chris Lattner0c0cb712009-08-08 20:22:20 +0000991
Chris Lattner11e96572009-08-03 21:53:27 +0000992const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner48130352010-01-13 06:38:18 +0000993getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000994 // Create the map if it doesn't already exist.
995 if (UniquingMap == 0)
996 UniquingMap = new MachOUniqueMapTy();
997 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000998
Chris Lattner38cff382009-08-13 00:37:15 +0000999 // Do the lookup, if we have a hit, return it.
1000 const MCSectionCOFF *&Entry = Map[Name];
1001 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001002
Chris Lattner38cff382009-08-13 00:37:15 +00001003 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +00001004}
1005
Chris Lattnerf26e03b2009-07-31 17:42:42 +00001006void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1007 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001008 if (UniquingMap != 0)
1009 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001010 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001011 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1012 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001013 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001014 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001015 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001016 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001017
Chris Lattner35c35312009-08-18 16:56:17 +00001018 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1019 // though it contains relocatable pointers. In PIC mode, this is probably a
1020 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1021 // adjusted or this should be a data section.
1022 LSDASection =
1023 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1024 EHFrameSection =
1025 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001026
Chris Lattner18a4c162009-08-02 07:24:22 +00001027 // Debug info.
1028 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001029 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001030 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1031 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001032 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001033 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1034 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001035 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001036 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1037 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001038 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001039 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1040 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001041 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001042 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1043 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001044 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001045 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1046 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001047 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001048 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1049 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001050 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001051 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1052 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001053 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001054 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1055 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001056 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001057 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1058 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001059 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001060 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1061 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001062}
1063
Chris Lattner24f654c2009-08-06 16:39:58 +00001064const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001065getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001066 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001067 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001068}
1069
Chris Lattnerf0144122009-07-28 03:13:23 +00001070static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1071 if (Kind.isText())
1072 return ".text$linkonce";
1073 if (Kind.isWriteable())
1074 return ".data$linkonce";
1075 return ".rdata$linkonce";
1076}
1077
1078
Chris Lattnera87dea42009-07-31 18:48:30 +00001079const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001080SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001081 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001082 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001083
Chris Lattnerf0144122009-07-28 03:13:23 +00001084 // If this global is linkonce/weak and the target handles this by emitting it
1085 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001086 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001087 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner48130352010-01-13 06:38:18 +00001088 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattnercab16cc2010-01-13 19:19:17 +00001089 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner48130352010-01-13 06:38:18 +00001090 return getCOFFSection(Name.str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001091 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001092
Chris Lattnerf9650c02009-08-01 21:46:23 +00001093 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001094 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001095
Chris Lattnerf0144122009-07-28 03:13:23 +00001096 return getDataSection();
1097}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001098