blob: cd0558025b8ea39004a679f19747d34097b42137 [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
Chris Lattnera3839bc2010-01-19 02:48:26 +0000144 // Variables with common linkage always get classified as common.
145 if (GVar->hasCommonLinkage())
146 return SectionKind::getCommon();
147
Chris Lattnerf0144122009-07-28 03:13:23 +0000148 // Variable can be easily put to BSS section.
149 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000150 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000151
152 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000153
Chris Lattnerf0144122009-07-28 03:13:23 +0000154 // If the global is marked constant, we can put it into a mergable section,
155 // a mergable string section, or general .data if it contains relocations.
156 if (GVar->isConstant()) {
157 // If the initializer for the global contains something that requires a
158 // relocation, then we may have to drop this into a wriable data section
159 // even though it is marked const.
160 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000161 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000162 case Constant::NoRelocation:
163 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000164 // section of the right width.
165 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000166 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000167 dyn_cast<IntegerType>(ATy->getElementType())) {
168 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
169 ITy->getBitWidth() == 32) &&
170 IsNullTerminatedString(C)) {
171 if (ITy->getBitWidth() == 8)
172 return SectionKind::getMergeable1ByteCString();
173 if (ITy->getBitWidth() == 16)
174 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000175
Chris Lattner1850e5a2009-08-04 16:13:09 +0000176 assert(ITy->getBitWidth() == 32 && "Unknown width");
177 return SectionKind::getMergeable4ByteCString();
178 }
179 }
180 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000181
Chris Lattnerf0144122009-07-28 03:13:23 +0000182 // Otherwise, just drop it into a mergable constant section. If we have
183 // a section for this size, use it, otherwise use the arbitrary sized
184 // mergable section.
185 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000186 case 4: return SectionKind::getMergeableConst4();
187 case 8: return SectionKind::getMergeableConst8();
188 case 16: return SectionKind::getMergeableConst16();
189 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000190 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000191
Chris Lattnerf0144122009-07-28 03:13:23 +0000192 case Constant::LocalRelocation:
193 // In static relocation model, the linker will resolve all addresses, so
194 // the relocation entries will actually be constants by the time the app
195 // starts up. However, we can't put this into a mergable section, because
196 // the linker doesn't take relocations into consideration when it tries to
197 // merge entries in the section.
198 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000199 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000200
Chris Lattnerf0144122009-07-28 03:13:23 +0000201 // Otherwise, the dynamic linker needs to fix it up, put it in the
202 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000203 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000204
Chris Lattnerf0144122009-07-28 03:13:23 +0000205 case Constant::GlobalRelocations:
206 // In static relocation model, the linker will resolve all addresses, so
207 // the relocation entries will actually be constants by the time the app
208 // starts up. However, we can't put this into a mergable section, because
209 // the linker doesn't take relocations into consideration when it tries to
210 // merge entries in the section.
211 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000212 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000213
Chris Lattnerf0144122009-07-28 03:13:23 +0000214 // Otherwise, the dynamic linker needs to fix it up, put it in the
215 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000216 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000217 }
218 }
219
220 // Okay, this isn't a constant. If the initializer for the global is going
221 // to require a runtime relocation by the dynamic linker, put it into a more
222 // specific section to improve startup time of the app. This coalesces these
223 // globals together onto fewer pages, improving the locality of the dynamic
224 // linker.
225 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000226 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000227
228 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000229 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000230 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000231 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000232 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000233 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000234 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000235 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000236 }
237}
238
239/// SectionForGlobal - This method computes the appropriate section to emit
240/// the specified global variable or function definition. This should not
241/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000242const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000243SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000244 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000245 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000246 if (GV->hasSection())
247 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000248
249
Chris Lattnerf0144122009-07-28 03:13:23 +0000250 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000251 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000252}
253
Chris Lattner58bed8f2009-08-05 04:25:40 +0000254
Chris Lattnerf0144122009-07-28 03:13:23 +0000255// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000256const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000257TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000258 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000259 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000260 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000261 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000262
Chris Lattnerf9650c02009-08-01 21:46:23 +0000263 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000264 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000265
Chris Lattner82458382009-08-01 21:56:13 +0000266 if (Kind.isBSS() && BSSSection != 0)
267 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000268
Chris Lattnerf9650c02009-08-01 21:46:23 +0000269 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000270 return ReadOnlySection;
271
272 return getDataSection();
273}
274
Chris Lattner83d77fa2009-08-01 23:46:12 +0000275/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000276/// specified size and relocation information, return a section that it
277/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000278const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000279TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000280 if (Kind.isReadOnly() && ReadOnlySection != 0)
281 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000282
Chris Lattnerf0144122009-07-28 03:13:23 +0000283 return DataSection;
284}
285
Chris Lattner8c6ed052009-09-16 01:46:41 +0000286/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
287/// pc-relative reference to the specified global variable from exception
288/// handling information. In addition to the symbol, this returns
289/// by-reference:
290///
291/// IsIndirect - True if the returned symbol is actually a stub that contains
292/// the address of the symbol, false if the symbol is the global itself.
293///
294/// IsPCRel - True if the symbol reference is already pc-relative, false if
295/// the caller needs to subtract off the address of the reference from the
296/// symbol.
297///
298const MCExpr *TargetLoweringObjectFile::
299getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000300 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000301 bool &IsIndirect, bool &IsPCRel) const {
302 // The generic implementation of this just returns a direct reference to the
303 // symbol.
304 IsIndirect = false;
305 IsPCRel = false;
306
Chris Lattner45111d12010-01-16 21:57:06 +0000307 // FIXME: Use GetGlobalValueSymbol.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000308 SmallString<128> Name;
309 Mang->getNameWithPrefix(Name, GV, false);
310 return MCSymbolRefExpr::Create(Name.str(), getContext());
311}
Chris Lattnerf0144122009-07-28 03:13:23 +0000312
Chris Lattnerf0144122009-07-28 03:13:23 +0000313
314//===----------------------------------------------------------------------===//
315// ELF
316//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000317typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
318
319TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
320 // If we have the section uniquing map, free it.
321 delete (ELFUniqueMapTy*)UniquingMap;
322}
Chris Lattnerf0144122009-07-28 03:13:23 +0000323
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000324const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000325getELFSection(StringRef Section, unsigned Type, unsigned Flags,
326 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000327 if (UniquingMap == 0)
328 UniquingMap = new ELFUniqueMapTy();
329 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000330
Chris Lattner38cff382009-08-13 00:37:15 +0000331 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000332 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000333 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000334
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000335 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
336 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000337}
338
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000339void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
340 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000341 if (UniquingMap != 0)
342 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000343 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000344
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000345 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000346 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
347 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
348 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000349
350 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000351 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
352 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
353 SectionKind::getText());
354
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000355 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000356 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
357 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
358 SectionKind::getDataRel());
359
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000360 ReadOnlySection =
361 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
362 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000363 SectionKind::getReadOnly());
364
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000365 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000366 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000367 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000368 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000369
370 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000371 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
372 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
373 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000374
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000375 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000376 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
377 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
378 SectionKind::getDataRel());
379
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000380 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000381 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
382 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
383 SectionKind::getDataRelLocal());
384
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000385 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000386 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
387 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
388 SectionKind::getReadOnlyWithRel());
389
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000390 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000391 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
392 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
393 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000394
395 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000396 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
397 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
398 SectionKind::getMergeableConst4());
399
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000400 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000401 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
402 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
403 SectionKind::getMergeableConst8());
404
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000405 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000406 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
407 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
408 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000409
410 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000411 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000412 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
413 SectionKind::getDataRel());
414
Chris Lattner80ec2792009-08-02 00:34:36 +0000415 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000416 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
417 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
418 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000419
Chris Lattner18a4c162009-08-02 07:24:22 +0000420 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000421
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000422 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
423 // it contains relocatable pointers. In PIC mode, this is probably a big
424 // runtime hit for C++ apps. Either the contents of the LSDA need to be
425 // adjusted or this should be a data section.
426 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000427 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
428 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000429 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000430 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000431 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
432 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000433
Chris Lattner18a4c162009-08-02 07:24:22 +0000434 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000435 DwarfAbbrevSection =
436 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000437 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000438 DwarfInfoSection =
439 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000440 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000441 DwarfLineSection =
442 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000443 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000444 DwarfFrameSection =
445 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000446 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000447 DwarfPubNamesSection =
448 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000449 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000450 DwarfPubTypesSection =
451 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000452 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000453 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000454 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
455 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000456 DwarfLocSection =
457 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000458 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000459 DwarfARangesSection =
460 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000461 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000462 DwarfRangesSection =
463 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000464 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000465 DwarfMacroInfoSection =
466 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000467 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000468}
469
470
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000471static SectionKind
Chris Lattner24f654c2009-08-06 16:39:58 +0000472getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000473 if (Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000474
Chris Lattnerf0144122009-07-28 03:13:23 +0000475 // Some lame default implementation based on some magic section names.
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000476 if (strcmp(Name, ".bss") == 0 ||
477 strncmp(Name, ".bss.", 5) == 0 ||
478 strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000479 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000480 strcmp(Name, ".sbss") == 0 ||
481 strncmp(Name, ".sbss.", 6) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000482 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
483 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000484 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000485
Chris Lattnerf0144122009-07-28 03:13:23 +0000486 if (strcmp(Name, ".tdata") == 0 ||
487 strncmp(Name, ".tdata.", 7) == 0 ||
488 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
489 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000490 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000491
Chris Lattnerf0144122009-07-28 03:13:23 +0000492 if (strcmp(Name, ".tbss") == 0 ||
493 strncmp(Name, ".tbss.", 6) == 0 ||
494 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
495 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000496 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000497
Chris Lattnerf0144122009-07-28 03:13:23 +0000498 return K;
499}
500
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000501
Chris Lattner48130352010-01-13 06:38:18 +0000502static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000503
Chris Lattner48130352010-01-13 06:38:18 +0000504 if (Name == ".init_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000505 return MCSectionELF::SHT_INIT_ARRAY;
506
Chris Lattner48130352010-01-13 06:38:18 +0000507 if (Name == ".fini_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000508 return MCSectionELF::SHT_FINI_ARRAY;
509
Chris Lattner48130352010-01-13 06:38:18 +0000510 if (Name == ".preinit_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000511 return MCSectionELF::SHT_PREINIT_ARRAY;
512
513 if (K.isBSS() || K.isThreadBSS())
514 return MCSectionELF::SHT_NOBITS;
515
516 return MCSectionELF::SHT_PROGBITS;
517}
518
519
520static unsigned
521getELFSectionFlags(SectionKind K) {
522 unsigned Flags = 0;
523
524 if (!K.isMetadata())
525 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000526
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000527 if (K.isText())
528 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000529
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000530 if (K.isWriteable())
531 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000532
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000533 if (K.isThreadLocal())
534 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000535
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000536 // K.isMergeableConst() is left out to honour PR4650
537 if (K.isMergeableCString() || K.isMergeableConst4() ||
538 K.isMergeableConst8() || K.isMergeableConst16())
539 Flags |= MCSectionELF::SHF_MERGE;
540
541 if (K.isMergeableCString())
542 Flags |= MCSectionELF::SHF_STRINGS;
543
544 return Flags;
545}
546
547
Chris Lattner24f654c2009-08-06 16:39:58 +0000548const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000549getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000550 Mangler *Mang, const TargetMachine &TM) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000551 const char *SectionName = GV->getSection().c_str();
552
Chris Lattner24f654c2009-08-06 16:39:58 +0000553 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000554 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000555
556 return getELFSection(SectionName,
557 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000558 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000559}
Chris Lattnerf0144122009-07-28 03:13:23 +0000560
561static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
562 if (Kind.isText()) return ".gnu.linkonce.t.";
563 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000564
Chris Lattnerf0144122009-07-28 03:13:23 +0000565 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
566 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000567
Chris Lattnerf0144122009-07-28 03:13:23 +0000568 if (Kind.isBSS()) return ".gnu.linkonce.b.";
569 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
570 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
571 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
572 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000573
Chris Lattnerf0144122009-07-28 03:13:23 +0000574 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
575 return ".gnu.linkonce.d.rel.ro.";
576}
577
Chris Lattnera87dea42009-07-31 18:48:30 +0000578const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000579SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000580 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000581
Chris Lattnerf0144122009-07-28 03:13:23 +0000582 // If this global is linkonce/weak and the target handles this by emitting it
583 // into a 'uniqued' section name, create and return the section now.
Chris Lattnera3839bc2010-01-19 02:48:26 +0000584 if (GV->isWeakForLinker() && !Kind.isCommon()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000585 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000586 SmallString<128> Name;
Chris Lattner48130352010-01-13 06:38:18 +0000587 Name.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner8da8d4b2010-01-13 21:29:21 +0000588 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000589 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
590 getELFSectionFlags(Kind), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000591 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000592
Chris Lattnerf9650c02009-08-01 21:46:23 +0000593 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000594
Chris Lattner3b24c012009-08-04 05:35:56 +0000595 if (Kind.isMergeable1ByteCString() ||
596 Kind.isMergeable2ByteCString() ||
597 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000598
Chris Lattner067fe1a2009-07-29 04:54:38 +0000599 // We also need alignment here.
600 // FIXME: this is getting the alignment of the character, not the
601 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000602 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000603 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000604
Chris Lattner7e88a502009-08-04 16:19:50 +0000605 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000606 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000607 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000608 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000609 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000610 else
611 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000612
613
Chris Lattner7e88a502009-08-04 16:19:50 +0000614 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000615 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
616 MCSectionELF::SHF_ALLOC |
617 MCSectionELF::SHF_MERGE |
618 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000619 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000620 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000621
Chris Lattnerf9650c02009-08-01 21:46:23 +0000622 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000623 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000624 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000625 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000626 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000627 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000628 return MergeableConst16Section;
629 return ReadOnlySection; // .const
630 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000631
Chris Lattnerf9650c02009-08-01 21:46:23 +0000632 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000633
Chris Lattnerf9650c02009-08-01 21:46:23 +0000634 if (Kind.isThreadData()) return TLSDataSection;
635 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000636
Chris Lattnera3839bc2010-01-19 02:48:26 +0000637 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000638
Chris Lattnerf9650c02009-08-01 21:46:23 +0000639 if (Kind.isDataNoRel()) return DataSection;
640 if (Kind.isDataRelLocal()) return DataRelLocalSection;
641 if (Kind.isDataRel()) return DataRelSection;
642 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000643
Chris Lattnerf9650c02009-08-01 21:46:23 +0000644 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000645 return DataRelROSection;
646}
647
Chris Lattner83d77fa2009-08-01 23:46:12 +0000648/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000649/// specified size and relocation information, return a section that it
650/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000651const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000652getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000653 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000654 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000655 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000656 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000657 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000658 return MergeableConst16Section;
659 if (Kind.isReadOnly())
660 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000661
Chris Lattnerf0144122009-07-28 03:13:23 +0000662 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
663 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
664 return DataRelROSection;
665}
666
667//===----------------------------------------------------------------------===//
668// MachO
669//===----------------------------------------------------------------------===//
670
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000671typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000672
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000673TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
674 // If we have the MachO uniquing map, free it.
675 delete (MachOUniqueMapTy*)UniquingMap;
676}
677
678
679const MCSectionMachO *TargetLoweringObjectFileMachO::
Daniel Dunbar2928c832009-11-06 10:58:06 +0000680getMachOSection(StringRef Segment, StringRef Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000681 unsigned TypeAndAttributes,
682 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000683 // We unique sections by their segment/section pair. The returned section
684 // may not have the same flags as the requested section, if so this should be
685 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000686
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000687 // Create the map if it doesn't already exist.
688 if (UniquingMap == 0)
689 UniquingMap = new MachOUniqueMapTy();
690 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000691
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000692 // Form the name to look up.
693 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000694 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000695 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000696 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000697
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000698 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000699 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000700 if (Entry) return Entry;
701
702 // Otherwise, return a new section.
703 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
704 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000705}
706
Chris Lattner11e96572009-08-03 21:53:27 +0000707
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000708void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
709 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000710 if (UniquingMap != 0)
711 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000712 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000713
Chris Lattnerff4bc462009-08-10 01:39:42 +0000714 TextSection // .text
715 = getMachOSection("__TEXT", "__text",
716 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
717 SectionKind::getText());
718 DataSection // .data
719 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000720
Chris Lattnerff4bc462009-08-10 01:39:42 +0000721 CStringSection // .cstring
722 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
723 SectionKind::getMergeable1ByteCString());
724 UStringSection
725 = getMachOSection("__TEXT","__ustring", 0,
726 SectionKind::getMergeable2ByteCString());
727 FourByteConstantSection // .literal4
728 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
729 SectionKind::getMergeableConst4());
730 EightByteConstantSection // .literal8
731 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
732 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000733
Chris Lattner4bb253c2009-07-28 17:50:28 +0000734 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
735 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000736 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000737 if (TM.getRelocationModel() != Reloc::Static &&
738 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000739 SixteenByteConstantSection = // .literal16
740 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000741 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000742
Chris Lattnerff4bc462009-08-10 01:39:42 +0000743 ReadOnlySection // .const
744 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000745
Chris Lattnerff4bc462009-08-10 01:39:42 +0000746 TextCoalSection
747 = getMachOSection("__TEXT", "__textcoal_nt",
748 MCSectionMachO::S_COALESCED |
749 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
750 SectionKind::getText());
751 ConstTextCoalSection
752 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
753 SectionKind::getText());
754 ConstDataCoalSection
755 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
756 SectionKind::getText());
Chris Lattneraac138e2010-01-19 02:09:44 +0000757 DataCommonSection
758 = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL,
759 SectionKind::getBSS());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000760 ConstDataSection // .const_data
761 = getMachOSection("__DATA", "__const", 0,
762 SectionKind::getReadOnlyWithRel());
763 DataCoalSection
764 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
765 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000766
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000767
Chris Lattnere309cfa2009-08-13 00:05:07 +0000768 LazySymbolPointerSection
769 = getMachOSection("__DATA", "__la_symbol_ptr",
770 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
771 SectionKind::getMetadata());
772 NonLazySymbolPointerSection
773 = getMachOSection("__DATA", "__nl_symbol_ptr",
774 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
775 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000776
Chris Lattner80ec2792009-08-02 00:34:36 +0000777 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000778 StaticCtorSection
779 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
780 StaticDtorSection
781 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000782 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000783 StaticCtorSection
784 = getMachOSection("__DATA", "__mod_init_func",
785 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
786 SectionKind::getDataRel());
787 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000788 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000789 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
790 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000791 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000792
Chris Lattner18a4c162009-08-02 07:24:22 +0000793 // Exception Handling.
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000794 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
795 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000796 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000797 getMachOSection("__TEXT", "__eh_frame",
798 MCSectionMachO::S_COALESCED |
799 MCSectionMachO::S_ATTR_NO_TOC |
800 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
801 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
802 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000803
804 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000805 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000806 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000807 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000808 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000809 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000810 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000811 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000812 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000813 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000814 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000815 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000816 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000817 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000818 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000819 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000820 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000821 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000822 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000823 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000824 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000825 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000826 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000827 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000828 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000829 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000830 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000831 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000832 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000833 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000834 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000835 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000836 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000837 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000838 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000839 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000840 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000841}
842
Chris Lattnera87dea42009-07-31 18:48:30 +0000843const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000844getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000845 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000846 // Parse the section specifier and create it if valid.
847 StringRef Segment, Section;
848 unsigned TAA, StubSize;
849 std::string ErrorCode =
850 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
851 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000852 if (!ErrorCode.empty()) {
853 // If invalid, report the error with llvm_report_error.
854 llvm_report_error("Global variable '" + GV->getNameStr() +
855 "' has an invalid section specifier '" + GV->getSection()+
856 "': " + ErrorCode + ".");
857 // Fall back to dropping it into the data section.
858 return DataSection;
859 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000860
Chris Lattnere309cfa2009-08-13 00:05:07 +0000861 // Get the section.
862 const MCSectionMachO *S =
863 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000864
Chris Lattnere309cfa2009-08-13 00:05:07 +0000865 // Okay, now that we got the section, verify that the TAA & StubSize agree.
866 // If the user declared multiple globals with different section flags, we need
867 // to reject it here.
868 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
869 // If invalid, report the error with llvm_report_error.
870 llvm_report_error("Global variable '" + GV->getNameStr() +
871 "' section type or attributes does not match previous"
872 " section specifier");
873 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000874
Chris Lattnere309cfa2009-08-13 00:05:07 +0000875 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000876}
877
878const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000879SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000880 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000881 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000882
Chris Lattnerf9650c02009-08-01 21:46:23 +0000883 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000884 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000885
Chris Lattnerf0144122009-07-28 03:13:23 +0000886 // If this is weak/linkonce, put this in a coalescable section, either in text
887 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000888 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000889 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000890 return ConstTextCoalSection;
891 return DataCoalSection;
892 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000893
Chris Lattnerf0144122009-07-28 03:13:23 +0000894 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000895 if (Kind.isMergeable1ByteCString() ||
896 Kind.isMergeable2ByteCString()) {
897 if (TM.getTargetData()->getPreferredAlignment(
898 cast<GlobalVariable>(GV)) < 32) {
899 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000900 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000901 assert(Kind.isMergeable2ByteCString());
902 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000903 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000904 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000905
Chris Lattnerf9650c02009-08-01 21:46:23 +0000906 if (Kind.isMergeableConst()) {
907 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000908 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000909 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000910 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000911 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000912 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000913 }
Chris Lattnerec409752009-08-04 16:27:13 +0000914
915 // Otherwise, if it is readonly, but not something we can specially optimize,
916 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000917 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000918 return ReadOnlySection;
919
920 // If this is marked const, put it into a const section. But if the dynamic
921 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000922 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000923 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000924
Chris Lattneraac138e2010-01-19 02:09:44 +0000925 // Put zero initialized globals with strong external linkage in the
926 // DATA, __common section with the .zerofill directive.
927 if (Kind.isBSS() && GV->hasExternalLinkage())
928 return DataCommonSection;
929
Chris Lattnerf0144122009-07-28 03:13:23 +0000930 // Otherwise, just drop the variable in the normal data section.
931 return DataSection;
932}
933
Chris Lattnera87dea42009-07-31 18:48:30 +0000934const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000935TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000936 // If this constant requires a relocation, we have to put it in the data
937 // segment, not in the text segment.
Eric Christophera07b7502010-01-07 19:44:05 +0000938 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000939 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000940
Chris Lattnerf0144122009-07-28 03:13:23 +0000941 if (Kind.isMergeableConst4())
942 return FourByteConstantSection;
943 if (Kind.isMergeableConst8())
944 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000945 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000946 return SixteenByteConstantSection;
947 return ReadOnlySection; // .const
948}
949
Chris Lattner26630c12009-07-31 20:52:39 +0000950/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
951/// not to emit the UsedDirective for some symbols in llvm.used.
952// FIXME: REMOVE this (rdar://7071300)
953bool TargetLoweringObjectFileMachO::
954shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
955 /// On Darwin, internally linked data beginning with "L" or "l" does not have
956 /// the directive emitted (this occurs in ObjC metadata).
957 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000958
Chris Lattner26630c12009-07-31 20:52:39 +0000959 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
960 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
961 // FIXME: ObjC metadata is currently emitted as internal symbols that have
962 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
963 // this horrible hack can go away.
Chris Lattner0b3735e2010-01-16 02:16:09 +0000964 SmallString<64> Name;
Chris Lattner036d8f92010-01-16 03:38:27 +0000965 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner26630c12009-07-31 20:52:39 +0000966 if (Name[0] == 'L' || Name[0] == 'l')
967 return false;
968 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000969
Chris Lattner26630c12009-07-31 20:52:39 +0000970 return true;
971}
972
Chris Lattner8c6ed052009-09-16 01:46:41 +0000973const MCExpr *TargetLoweringObjectFileMachO::
974getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000975 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000976 bool &IsIndirect, bool &IsPCRel) const {
977 // The mach-o version of this method defaults to returning a stub reference.
978 IsIndirect = true;
979 IsPCRel = false;
980
981 SmallString<128> Name;
982 Mang->getNameWithPrefix(Name, GV, true);
983 Name += "$non_lazy_ptr";
984 return MCSymbolRefExpr::Create(Name.str(), getContext());
985}
986
Chris Lattner26630c12009-07-31 20:52:39 +0000987
Chris Lattnerf0144122009-07-28 03:13:23 +0000988//===----------------------------------------------------------------------===//
989// COFF
990//===----------------------------------------------------------------------===//
991
Chris Lattner38cff382009-08-13 00:37:15 +0000992typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
993
994TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
995 delete (COFFUniqueMapTy*)UniquingMap;
996}
997
Chris Lattner0c0cb712009-08-08 20:22:20 +0000998
Chris Lattner11e96572009-08-03 21:53:27 +0000999const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner48130352010-01-13 06:38:18 +00001000getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +00001001 // Create the map if it doesn't already exist.
1002 if (UniquingMap == 0)
1003 UniquingMap = new MachOUniqueMapTy();
1004 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001005
Chris Lattner38cff382009-08-13 00:37:15 +00001006 // Do the lookup, if we have a hit, return it.
1007 const MCSectionCOFF *&Entry = Map[Name];
1008 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001009
Chris Lattner38cff382009-08-13 00:37:15 +00001010 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +00001011}
1012
Chris Lattnerf26e03b2009-07-31 17:42:42 +00001013void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1014 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001015 if (UniquingMap != 0)
1016 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001017 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001018 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1019 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001020 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001021 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001022 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001023 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001024
Chris Lattner35c35312009-08-18 16:56:17 +00001025 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1026 // though it contains relocatable pointers. In PIC mode, this is probably a
1027 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1028 // adjusted or this should be a data section.
1029 LSDASection =
1030 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1031 EHFrameSection =
1032 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001033
Chris Lattner18a4c162009-08-02 07:24:22 +00001034 // Debug info.
1035 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001036 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001037 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1038 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001039 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001040 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1041 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001042 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001043 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1044 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001045 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001046 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1047 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001048 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001049 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1050 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001051 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001052 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1053 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001054 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001055 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1056 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001057 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001058 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1059 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001060 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001061 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1062 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001063 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001064 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1065 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001066 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001067 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1068 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001069}
1070
Chris Lattner24f654c2009-08-06 16:39:58 +00001071const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001072getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001073 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001074 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001075}
1076
Chris Lattnerf0144122009-07-28 03:13:23 +00001077static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1078 if (Kind.isText())
1079 return ".text$linkonce";
1080 if (Kind.isWriteable())
1081 return ".data$linkonce";
1082 return ".rdata$linkonce";
1083}
1084
1085
Chris Lattnera87dea42009-07-31 18:48:30 +00001086const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001087SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001088 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001089 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001090
Chris Lattnerf0144122009-07-28 03:13:23 +00001091 // If this global is linkonce/weak and the target handles this by emitting it
1092 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001093 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001094 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner48130352010-01-13 06:38:18 +00001095 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattnercab16cc2010-01-13 19:19:17 +00001096 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner48130352010-01-13 06:38:18 +00001097 return getCOFFSection(Name.str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001098 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001099
Chris Lattnerf9650c02009-08-01 21:46:23 +00001100 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001101 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001102
Chris Lattnerf0144122009-07-28 03:13:23 +00001103 return getDataSection();
1104}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001105