blob: a231ebc560589f8f702519eeb0060016bb6f8788 [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.
Chris Lattnerce8749e2010-01-19 04:15:51 +0000149 if (isSuitableForBSS(GVar)) {
150 if (GVar->hasLocalLinkage())
151 return SectionKind::getBSSLocal();
152 else if (GVar->hasExternalLinkage())
153 return SectionKind::getBSSExtern();
Chris Lattner27981192009-08-01 23:57:16 +0000154 return SectionKind::getBSS();
Chris Lattnerce8749e2010-01-19 04:15:51 +0000155 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000156
157 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000158
Chris Lattnerf0144122009-07-28 03:13:23 +0000159 // If the global is marked constant, we can put it into a mergable section,
160 // a mergable string section, or general .data if it contains relocations.
161 if (GVar->isConstant()) {
162 // If the initializer for the global contains something that requires a
163 // relocation, then we may have to drop this into a wriable data section
164 // even though it is marked const.
165 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000166 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167 case Constant::NoRelocation:
168 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000169 // section of the right width.
170 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000171 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000172 dyn_cast<IntegerType>(ATy->getElementType())) {
173 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
174 ITy->getBitWidth() == 32) &&
175 IsNullTerminatedString(C)) {
176 if (ITy->getBitWidth() == 8)
177 return SectionKind::getMergeable1ByteCString();
178 if (ITy->getBitWidth() == 16)
179 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000180
Chris Lattner1850e5a2009-08-04 16:13:09 +0000181 assert(ITy->getBitWidth() == 32 && "Unknown width");
182 return SectionKind::getMergeable4ByteCString();
183 }
184 }
185 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000186
Chris Lattnerf0144122009-07-28 03:13:23 +0000187 // Otherwise, just drop it into a mergable constant section. If we have
188 // a section for this size, use it, otherwise use the arbitrary sized
189 // mergable section.
190 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000191 case 4: return SectionKind::getMergeableConst4();
192 case 8: return SectionKind::getMergeableConst8();
193 case 16: return SectionKind::getMergeableConst16();
194 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000195 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000196
Chris Lattnerf0144122009-07-28 03:13:23 +0000197 case Constant::LocalRelocation:
198 // In static relocation model, the linker will resolve all addresses, so
199 // the relocation entries will actually be constants by the time the app
200 // starts up. However, we can't put this into a mergable section, because
201 // the linker doesn't take relocations into consideration when it tries to
202 // merge entries in the section.
203 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000204 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000205
Chris Lattnerf0144122009-07-28 03:13:23 +0000206 // Otherwise, the dynamic linker needs to fix it up, put it in the
207 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000208 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000209
Chris Lattnerf0144122009-07-28 03:13:23 +0000210 case Constant::GlobalRelocations:
211 // In static relocation model, the linker will resolve all addresses, so
212 // the relocation entries will actually be constants by the time the app
213 // starts up. However, we can't put this into a mergable section, because
214 // the linker doesn't take relocations into consideration when it tries to
215 // merge entries in the section.
216 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000217 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000218
Chris Lattnerf0144122009-07-28 03:13:23 +0000219 // Otherwise, the dynamic linker needs to fix it up, put it in the
220 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000221 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000222 }
223 }
224
225 // Okay, this isn't a constant. If the initializer for the global is going
226 // to require a runtime relocation by the dynamic linker, put it into a more
227 // specific section to improve startup time of the app. This coalesces these
228 // globals together onto fewer pages, improving the locality of the dynamic
229 // linker.
230 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000231 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000232
233 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000234 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000235 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000236 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000238 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000239 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000240 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000241 }
242}
243
244/// SectionForGlobal - This method computes the appropriate section to emit
245/// the specified global variable or function definition. This should not
246/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000247const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000248SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000249 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000250 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000251 if (GV->hasSection())
252 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000253
254
Chris Lattnerf0144122009-07-28 03:13:23 +0000255 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000256 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000257}
258
Chris Lattner58bed8f2009-08-05 04:25:40 +0000259
Chris Lattnerf0144122009-07-28 03:13:23 +0000260// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000261const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000262TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000263 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000264 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000265 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000266 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000267
Chris Lattnerf9650c02009-08-01 21:46:23 +0000268 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000269 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000270
Chris Lattner82458382009-08-01 21:56:13 +0000271 if (Kind.isBSS() && BSSSection != 0)
272 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000273
Chris Lattnerf9650c02009-08-01 21:46:23 +0000274 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000275 return ReadOnlySection;
276
277 return getDataSection();
278}
279
Chris Lattner83d77fa2009-08-01 23:46:12 +0000280/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000281/// specified size and relocation information, return a section that it
282/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000283const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000284TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000285 if (Kind.isReadOnly() && ReadOnlySection != 0)
286 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000287
Chris Lattnerf0144122009-07-28 03:13:23 +0000288 return DataSection;
289}
290
Chris Lattner8c6ed052009-09-16 01:46:41 +0000291/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
292/// pc-relative reference to the specified global variable from exception
293/// handling information. In addition to the symbol, this returns
294/// by-reference:
295///
296/// IsIndirect - True if the returned symbol is actually a stub that contains
297/// the address of the symbol, false if the symbol is the global itself.
298///
299/// IsPCRel - True if the symbol reference is already pc-relative, false if
300/// the caller needs to subtract off the address of the reference from the
301/// symbol.
302///
303const MCExpr *TargetLoweringObjectFile::
304getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000305 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000306 bool &IsIndirect, bool &IsPCRel) const {
307 // The generic implementation of this just returns a direct reference to the
308 // symbol.
309 IsIndirect = false;
310 IsPCRel = false;
311
Chris Lattner45111d12010-01-16 21:57:06 +0000312 // FIXME: Use GetGlobalValueSymbol.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000313 SmallString<128> Name;
314 Mang->getNameWithPrefix(Name, GV, false);
315 return MCSymbolRefExpr::Create(Name.str(), getContext());
316}
Chris Lattnerf0144122009-07-28 03:13:23 +0000317
Chris Lattnerf0144122009-07-28 03:13:23 +0000318
319//===----------------------------------------------------------------------===//
320// ELF
321//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000322typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
323
324TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
325 // If we have the section uniquing map, free it.
326 delete (ELFUniqueMapTy*)UniquingMap;
327}
Chris Lattnerf0144122009-07-28 03:13:23 +0000328
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000329const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000330getELFSection(StringRef Section, unsigned Type, unsigned Flags,
331 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000332 if (UniquingMap == 0)
333 UniquingMap = new ELFUniqueMapTy();
334 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000335
Chris Lattner38cff382009-08-13 00:37:15 +0000336 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000337 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000338 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000339
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000340 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
341 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000342}
343
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000344void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
345 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000346 if (UniquingMap != 0)
347 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000348 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000349
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000350 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000351 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
352 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
353 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000354
355 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000356 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
357 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
358 SectionKind::getText());
359
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000360 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000361 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
362 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
363 SectionKind::getDataRel());
364
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000365 ReadOnlySection =
366 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
367 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000368 SectionKind::getReadOnly());
369
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000370 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000371 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000372 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000373 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000374
375 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000376 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
377 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
378 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000379
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000380 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000381 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
382 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
383 SectionKind::getDataRel());
384
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000385 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000386 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
387 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
388 SectionKind::getDataRelLocal());
389
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000390 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000391 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
392 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
393 SectionKind::getReadOnlyWithRel());
394
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000395 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000396 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
397 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
398 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000399
400 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000401 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
402 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
403 SectionKind::getMergeableConst4());
404
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000405 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000406 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
407 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
408 SectionKind::getMergeableConst8());
409
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000410 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000411 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
412 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
413 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000414
415 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000416 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000417 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
418 SectionKind::getDataRel());
419
Chris Lattner80ec2792009-08-02 00:34:36 +0000420 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000421 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
422 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
423 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000424
Chris Lattner18a4c162009-08-02 07:24:22 +0000425 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000426
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000427 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
428 // it contains relocatable pointers. In PIC mode, this is probably a big
429 // runtime hit for C++ apps. Either the contents of the LSDA need to be
430 // adjusted or this should be a data section.
431 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000432 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
433 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000434 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000435 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000436 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
437 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000438
Chris Lattner18a4c162009-08-02 07:24:22 +0000439 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000440 DwarfAbbrevSection =
441 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000442 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000443 DwarfInfoSection =
444 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000445 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000446 DwarfLineSection =
447 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000448 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000449 DwarfFrameSection =
450 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000451 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000452 DwarfPubNamesSection =
453 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000454 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000455 DwarfPubTypesSection =
456 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000457 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000458 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000459 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
460 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000461 DwarfLocSection =
462 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000463 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000464 DwarfARangesSection =
465 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000466 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000467 DwarfRangesSection =
468 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000469 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000470 DwarfMacroInfoSection =
471 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000472 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000473}
474
475
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000476static SectionKind
Benjamin Kramera46918d2010-01-22 18:21:23 +0000477getELFKindForNamedSection(StringRef Name, SectionKind K) {
478 if (Name.empty() || Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000479
Chris Lattnerf0144122009-07-28 03:13:23 +0000480 // Some lame default implementation based on some magic section names.
Benjamin Kramera46918d2010-01-22 18:21:23 +0000481 if (Name == ".bss" ||
482 Name.startswith(".bss.") ||
483 Name.startswith(".gnu.linkonce.b.") ||
484 Name.startswith(".llvm.linkonce.b.") ||
485 Name == ".sbss" ||
486 Name.startswith(".sbss.") ||
487 Name.startswith(".gnu.linkonce.sb.") ||
488 Name.startswith(".llvm.linkonce.sb."))
Chris Lattner27981192009-08-01 23:57:16 +0000489 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000490
Benjamin Kramera46918d2010-01-22 18:21:23 +0000491 if (Name == ".tdata" ||
492 Name.startswith(".tdata.") ||
493 Name.startswith(".gnu.linkonce.td.") ||
494 Name.startswith(".llvm.linkonce.td."))
Chris Lattner27981192009-08-01 23:57:16 +0000495 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000496
Benjamin Kramera46918d2010-01-22 18:21:23 +0000497 if (Name == ".tbss" ||
498 Name.startswith(".tbss.") ||
499 Name.startswith(".gnu.linkonce.tb.") ||
500 Name.startswith(".llvm.linkonce.tb."))
Chris Lattner27981192009-08-01 23:57:16 +0000501 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000502
Chris Lattnerf0144122009-07-28 03:13:23 +0000503 return K;
504}
505
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000506
Chris Lattner48130352010-01-13 06:38:18 +0000507static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000508
Chris Lattner48130352010-01-13 06:38:18 +0000509 if (Name == ".init_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000510 return MCSectionELF::SHT_INIT_ARRAY;
511
Chris Lattner48130352010-01-13 06:38:18 +0000512 if (Name == ".fini_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000513 return MCSectionELF::SHT_FINI_ARRAY;
514
Chris Lattner48130352010-01-13 06:38:18 +0000515 if (Name == ".preinit_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000516 return MCSectionELF::SHT_PREINIT_ARRAY;
517
518 if (K.isBSS() || K.isThreadBSS())
519 return MCSectionELF::SHT_NOBITS;
520
521 return MCSectionELF::SHT_PROGBITS;
522}
523
524
525static unsigned
526getELFSectionFlags(SectionKind K) {
527 unsigned Flags = 0;
528
529 if (!K.isMetadata())
530 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000531
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000532 if (K.isText())
533 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000534
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000535 if (K.isWriteable())
536 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000537
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000538 if (K.isThreadLocal())
539 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000540
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000541 // K.isMergeableConst() is left out to honour PR4650
542 if (K.isMergeableCString() || K.isMergeableConst4() ||
543 K.isMergeableConst8() || K.isMergeableConst16())
544 Flags |= MCSectionELF::SHF_MERGE;
545
546 if (K.isMergeableCString())
547 Flags |= MCSectionELF::SHF_STRINGS;
548
549 return Flags;
550}
551
552
Chris Lattner24f654c2009-08-06 16:39:58 +0000553const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000554getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000555 Mangler *Mang, const TargetMachine &TM) const {
Benjamin Kramera46918d2010-01-22 18:21:23 +0000556 StringRef SectionName = GV->getSection();
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000557
Chris Lattner24f654c2009-08-06 16:39:58 +0000558 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000559 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000560
561 return getELFSection(SectionName,
562 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000563 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000564}
Chris Lattnerf0144122009-07-28 03:13:23 +0000565
566static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
567 if (Kind.isText()) return ".gnu.linkonce.t.";
568 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000569
Chris Lattnerf0144122009-07-28 03:13:23 +0000570 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
571 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000572
Chris Lattnerf0144122009-07-28 03:13:23 +0000573 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
574 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
575 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
576 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000577
Chris Lattnerf0144122009-07-28 03:13:23 +0000578 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
579 return ".gnu.linkonce.d.rel.ro.";
580}
581
Chris Lattnera87dea42009-07-31 18:48:30 +0000582const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000583SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000584 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000585
Chris Lattnerf0144122009-07-28 03:13:23 +0000586 // If this global is linkonce/weak and the target handles this by emitting it
587 // into a 'uniqued' section name, create and return the section now.
Chris Lattnerc7fbe902010-01-19 03:06:01 +0000588 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000589 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000590 SmallString<128> Name;
Chris Lattner48130352010-01-13 06:38:18 +0000591 Name.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner8da8d4b2010-01-13 21:29:21 +0000592 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000593 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
594 getELFSectionFlags(Kind), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000595 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000596
Chris Lattnerf9650c02009-08-01 21:46:23 +0000597 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000598
Chris Lattner3b24c012009-08-04 05:35:56 +0000599 if (Kind.isMergeable1ByteCString() ||
600 Kind.isMergeable2ByteCString() ||
601 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000602
Chris Lattner067fe1a2009-07-29 04:54:38 +0000603 // We also need alignment here.
604 // FIXME: this is getting the alignment of the character, not the
605 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000606 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000607 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000608
Chris Lattner7e88a502009-08-04 16:19:50 +0000609 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000610 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000611 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000612 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000613 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000614 else
615 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000616
617
Chris Lattner7e88a502009-08-04 16:19:50 +0000618 std::string Name = SizeSpec + utostr(Align);
Benjamin Kramera46918d2010-01-22 18:21:23 +0000619 return getELFSection(Name, MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000620 MCSectionELF::SHF_ALLOC |
621 MCSectionELF::SHF_MERGE |
622 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000623 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000624 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000625
Chris Lattnerf9650c02009-08-01 21:46:23 +0000626 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000627 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000628 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000629 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000630 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000631 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000632 return MergeableConst16Section;
633 return ReadOnlySection; // .const
634 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000635
Chris Lattnerf9650c02009-08-01 21:46:23 +0000636 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000637
Chris Lattnerf9650c02009-08-01 21:46:23 +0000638 if (Kind.isThreadData()) return TLSDataSection;
639 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000640
Chris Lattnerc7fbe902010-01-19 03:06:01 +0000641 // Note: we claim that common symbols are put in BSSSection, but they are
642 // really emitted with the magic .comm directive, which creates a symbol table
643 // entry but not a section.
Chris Lattnera3839bc2010-01-19 02:48:26 +0000644 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000645
Chris Lattnerf9650c02009-08-01 21:46:23 +0000646 if (Kind.isDataNoRel()) return DataSection;
647 if (Kind.isDataRelLocal()) return DataRelLocalSection;
648 if (Kind.isDataRel()) return DataRelSection;
649 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000650
Chris Lattnerf9650c02009-08-01 21:46:23 +0000651 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000652 return DataRelROSection;
653}
654
Chris Lattner83d77fa2009-08-01 23:46:12 +0000655/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000656/// specified size and relocation information, return a section that it
657/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000658const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000659getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000660 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000661 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000662 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000663 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000664 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000665 return MergeableConst16Section;
666 if (Kind.isReadOnly())
667 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000668
Chris Lattnerf0144122009-07-28 03:13:23 +0000669 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
670 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
671 return DataRelROSection;
672}
673
674//===----------------------------------------------------------------------===//
675// MachO
676//===----------------------------------------------------------------------===//
677
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000678typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000679
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000680TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
681 // If we have the MachO uniquing map, free it.
682 delete (MachOUniqueMapTy*)UniquingMap;
683}
684
685
686const MCSectionMachO *TargetLoweringObjectFileMachO::
Daniel Dunbar2928c832009-11-06 10:58:06 +0000687getMachOSection(StringRef Segment, StringRef Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000688 unsigned TypeAndAttributes,
689 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000690 // We unique sections by their segment/section pair. The returned section
691 // may not have the same flags as the requested section, if so this should be
692 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000693
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000694 // Create the map if it doesn't already exist.
695 if (UniquingMap == 0)
696 UniquingMap = new MachOUniqueMapTy();
697 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000698
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000699 // Form the name to look up.
700 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000701 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000702 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000703 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000704
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000705 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000706 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000707 if (Entry) return Entry;
708
709 // Otherwise, return a new section.
710 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
711 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000712}
713
Chris Lattner11e96572009-08-03 21:53:27 +0000714
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000715void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
716 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000717 if (UniquingMap != 0)
718 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000719 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000720
Chris Lattnerff4bc462009-08-10 01:39:42 +0000721 TextSection // .text
722 = getMachOSection("__TEXT", "__text",
723 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
724 SectionKind::getText());
725 DataSection // .data
726 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000727
Chris Lattnerff4bc462009-08-10 01:39:42 +0000728 CStringSection // .cstring
729 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
730 SectionKind::getMergeable1ByteCString());
731 UStringSection
732 = getMachOSection("__TEXT","__ustring", 0,
733 SectionKind::getMergeable2ByteCString());
734 FourByteConstantSection // .literal4
735 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
736 SectionKind::getMergeableConst4());
737 EightByteConstantSection // .literal8
738 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
739 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000740
Chris Lattner4bb253c2009-07-28 17:50:28 +0000741 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
742 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000743 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000744 if (TM.getRelocationModel() != Reloc::Static &&
745 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000746 SixteenByteConstantSection = // .literal16
747 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000748 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000749
Chris Lattnerff4bc462009-08-10 01:39:42 +0000750 ReadOnlySection // .const
751 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000752
Chris Lattnerff4bc462009-08-10 01:39:42 +0000753 TextCoalSection
754 = getMachOSection("__TEXT", "__textcoal_nt",
755 MCSectionMachO::S_COALESCED |
756 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
757 SectionKind::getText());
758 ConstTextCoalSection
759 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
760 SectionKind::getText());
761 ConstDataCoalSection
762 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
763 SectionKind::getText());
764 ConstDataSection // .const_data
765 = getMachOSection("__DATA", "__const", 0,
766 SectionKind::getReadOnlyWithRel());
767 DataCoalSection
768 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
769 SectionKind::getDataRel());
Chris Lattner814819f2010-01-19 06:25:51 +0000770 DataCommonSection
771 = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL,
772 SectionKind::getBSS());
773 DataBSSSection
774 = getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
775 SectionKind::getBSS());
776
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000777
Chris Lattnere309cfa2009-08-13 00:05:07 +0000778 LazySymbolPointerSection
779 = getMachOSection("__DATA", "__la_symbol_ptr",
780 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
781 SectionKind::getMetadata());
782 NonLazySymbolPointerSection
783 = getMachOSection("__DATA", "__nl_symbol_ptr",
784 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
785 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000786
Chris Lattner80ec2792009-08-02 00:34:36 +0000787 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000788 StaticCtorSection
789 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
790 StaticDtorSection
791 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000792 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000793 StaticCtorSection
794 = getMachOSection("__DATA", "__mod_init_func",
795 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
796 SectionKind::getDataRel());
797 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000798 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000799 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
800 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000801 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000802
Chris Lattner18a4c162009-08-02 07:24:22 +0000803 // Exception Handling.
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000804 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
805 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000806 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000807 getMachOSection("__TEXT", "__eh_frame",
808 MCSectionMachO::S_COALESCED |
809 MCSectionMachO::S_ATTR_NO_TOC |
810 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
811 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
812 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000813
814 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000815 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000816 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000817 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000818 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000819 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000820 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000821 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000822 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000823 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000824 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000825 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000826 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000827 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000828 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000829 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000830 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000831 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000832 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000833 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000834 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000835 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000836 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000837 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000838 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000839 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000840 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000841 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000842 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000843 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000844 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000845 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000846 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000847 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000848 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000849 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000850 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000851}
852
Chris Lattnera87dea42009-07-31 18:48:30 +0000853const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000854getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000855 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000856 // Parse the section specifier and create it if valid.
857 StringRef Segment, Section;
858 unsigned TAA, StubSize;
859 std::string ErrorCode =
860 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
861 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000862 if (!ErrorCode.empty()) {
863 // If invalid, report the error with llvm_report_error.
864 llvm_report_error("Global variable '" + GV->getNameStr() +
865 "' has an invalid section specifier '" + GV->getSection()+
866 "': " + ErrorCode + ".");
867 // Fall back to dropping it into the data section.
868 return DataSection;
869 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000870
Chris Lattnere309cfa2009-08-13 00:05:07 +0000871 // Get the section.
872 const MCSectionMachO *S =
873 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000874
Chris Lattnere309cfa2009-08-13 00:05:07 +0000875 // Okay, now that we got the section, verify that the TAA & StubSize agree.
876 // If the user declared multiple globals with different section flags, we need
877 // to reject it here.
878 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
879 // If invalid, report the error with llvm_report_error.
880 llvm_report_error("Global variable '" + GV->getNameStr() +
881 "' section type or attributes does not match previous"
882 " section specifier");
883 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000884
Chris Lattnere309cfa2009-08-13 00:05:07 +0000885 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000886}
887
888const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000889SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000890 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000891 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000892
Chris Lattnerf9650c02009-08-01 21:46:23 +0000893 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000894 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000895
Chris Lattnerf0144122009-07-28 03:13:23 +0000896 // If this is weak/linkonce, put this in a coalescable section, either in text
897 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000898 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000899 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000900 return ConstTextCoalSection;
901 return DataCoalSection;
902 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000903
Chris Lattnerf0144122009-07-28 03:13:23 +0000904 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000905 if (Kind.isMergeable1ByteCString() ||
906 Kind.isMergeable2ByteCString()) {
907 if (TM.getTargetData()->getPreferredAlignment(
908 cast<GlobalVariable>(GV)) < 32) {
909 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000910 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000911 assert(Kind.isMergeable2ByteCString());
912 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000913 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000914 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000915
Chris Lattnerf9650c02009-08-01 21:46:23 +0000916 if (Kind.isMergeableConst()) {
917 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000918 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000919 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000920 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000921 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000922 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000923 }
Chris Lattnerec409752009-08-04 16:27:13 +0000924
925 // Otherwise, if it is readonly, but not something we can specially optimize,
926 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000927 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000928 return ReadOnlySection;
929
930 // If this is marked const, put it into a const section. But if the dynamic
931 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000932 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000933 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000934
Chris Lattneraac138e2010-01-19 02:09:44 +0000935 // Put zero initialized globals with strong external linkage in the
936 // DATA, __common section with the .zerofill directive.
Chris Lattnerce8749e2010-01-19 04:15:51 +0000937 if (Kind.isBSSExtern())
Chris Lattneraac138e2010-01-19 02:09:44 +0000938 return DataCommonSection;
Chris Lattner814819f2010-01-19 06:25:51 +0000939
940 // Put zero initialized globals with local linkage in __DATA,__bss directive
941 // with the .zerofill directive (aka .lcomm).
942 if (Kind.isBSSLocal())
943 return DataBSSSection;
Chris Lattneraac138e2010-01-19 02:09:44 +0000944
Chris Lattnerf0144122009-07-28 03:13:23 +0000945 // Otherwise, just drop the variable in the normal data section.
946 return DataSection;
947}
948
Chris Lattnera87dea42009-07-31 18:48:30 +0000949const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000950TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000951 // If this constant requires a relocation, we have to put it in the data
952 // segment, not in the text segment.
Eric Christophera07b7502010-01-07 19:44:05 +0000953 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000954 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000955
Chris Lattnerf0144122009-07-28 03:13:23 +0000956 if (Kind.isMergeableConst4())
957 return FourByteConstantSection;
958 if (Kind.isMergeableConst8())
959 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000960 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000961 return SixteenByteConstantSection;
962 return ReadOnlySection; // .const
963}
964
Chris Lattner26630c12009-07-31 20:52:39 +0000965/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
966/// not to emit the UsedDirective for some symbols in llvm.used.
967// FIXME: REMOVE this (rdar://7071300)
968bool TargetLoweringObjectFileMachO::
969shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
970 /// On Darwin, internally linked data beginning with "L" or "l" does not have
971 /// the directive emitted (this occurs in ObjC metadata).
972 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000973
Chris Lattner26630c12009-07-31 20:52:39 +0000974 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
975 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
976 // FIXME: ObjC metadata is currently emitted as internal symbols that have
977 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
978 // this horrible hack can go away.
Chris Lattner0b3735e2010-01-16 02:16:09 +0000979 SmallString<64> Name;
Chris Lattner036d8f92010-01-16 03:38:27 +0000980 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner26630c12009-07-31 20:52:39 +0000981 if (Name[0] == 'L' || Name[0] == 'l')
982 return false;
983 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000984
Chris Lattner26630c12009-07-31 20:52:39 +0000985 return true;
986}
987
Chris Lattner8c6ed052009-09-16 01:46:41 +0000988const MCExpr *TargetLoweringObjectFileMachO::
989getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000990 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000991 bool &IsIndirect, bool &IsPCRel) const {
992 // The mach-o version of this method defaults to returning a stub reference.
993 IsIndirect = true;
994 IsPCRel = false;
995
996 SmallString<128> Name;
997 Mang->getNameWithPrefix(Name, GV, true);
998 Name += "$non_lazy_ptr";
999 return MCSymbolRefExpr::Create(Name.str(), getContext());
1000}
1001
Chris Lattner26630c12009-07-31 20:52:39 +00001002
Chris Lattnerf0144122009-07-28 03:13:23 +00001003//===----------------------------------------------------------------------===//
1004// COFF
1005//===----------------------------------------------------------------------===//
1006
Chris Lattner38cff382009-08-13 00:37:15 +00001007typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
1008
1009TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
1010 delete (COFFUniqueMapTy*)UniquingMap;
1011}
1012
Chris Lattner0c0cb712009-08-08 20:22:20 +00001013
Chris Lattner11e96572009-08-03 21:53:27 +00001014const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner48130352010-01-13 06:38:18 +00001015getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +00001016 // Create the map if it doesn't already exist.
1017 if (UniquingMap == 0)
1018 UniquingMap = new MachOUniqueMapTy();
1019 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001020
Chris Lattner38cff382009-08-13 00:37:15 +00001021 // Do the lookup, if we have a hit, return it.
1022 const MCSectionCOFF *&Entry = Map[Name];
1023 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001024
Chris Lattner38cff382009-08-13 00:37:15 +00001025 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +00001026}
1027
Chris Lattnerf26e03b2009-07-31 17:42:42 +00001028void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1029 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001030 if (UniquingMap != 0)
1031 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001032 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001033 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1034 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001035 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001036 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001037 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001038 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001039
Chris Lattner35c35312009-08-18 16:56:17 +00001040 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1041 // though it contains relocatable pointers. In PIC mode, this is probably a
1042 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1043 // adjusted or this should be a data section.
1044 LSDASection =
1045 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1046 EHFrameSection =
1047 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001048
Chris Lattner18a4c162009-08-02 07:24:22 +00001049 // Debug info.
1050 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001051 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001052 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1053 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001054 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001055 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1056 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001057 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001058 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1059 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001060 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001061 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1062 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001063 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001064 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1065 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001066 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001067 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1068 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001069 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001070 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1071 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001072 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001073 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1074 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001075 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001076 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1077 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001078 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001079 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1080 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001081 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001082 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1083 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001084}
1085
Chris Lattner24f654c2009-08-06 16:39:58 +00001086const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001087getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001088 Mangler *Mang, const TargetMachine &TM) const {
Benjamin Kramera46918d2010-01-22 18:21:23 +00001089 return getCOFFSection(GV->getSection(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001090}
1091
Chris Lattnerf0144122009-07-28 03:13:23 +00001092static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1093 if (Kind.isText())
1094 return ".text$linkonce";
1095 if (Kind.isWriteable())
1096 return ".data$linkonce";
1097 return ".rdata$linkonce";
1098}
1099
1100
Chris Lattnera87dea42009-07-31 18:48:30 +00001101const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001102SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001103 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001104 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001105
Chris Lattnerf0144122009-07-28 03:13:23 +00001106 // If this global is linkonce/weak and the target handles this by emitting it
1107 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001108 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001109 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner48130352010-01-13 06:38:18 +00001110 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattnercab16cc2010-01-13 19:19:17 +00001111 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner48130352010-01-13 06:38:18 +00001112 return getCOFFSection(Name.str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001113 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001114
Chris Lattnerf9650c02009-08-01 21:46:23 +00001115 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001116 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001117
Chris Lattnerf0144122009-07-28 03:13:23 +00001118 return getDataSection();
1119}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001120