blob: 7b00b332f63679a147402a6c6b15f2e068027f45 [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.isDataNoRel()) return ".gnu.linkonce.d.";
569 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
570 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
571 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000572
Chris Lattnerf0144122009-07-28 03:13:23 +0000573 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
574 return ".gnu.linkonce.d.rel.ro.";
575}
576
Chris Lattnera87dea42009-07-31 18:48:30 +0000577const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000578SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000579 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000580
Chris Lattnerf0144122009-07-28 03:13:23 +0000581 // If this global is linkonce/weak and the target handles this by emitting it
582 // into a 'uniqued' section name, create and return the section now.
Chris Lattnerc7fbe902010-01-19 03:06:01 +0000583 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000584 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000585 SmallString<128> Name;
Chris Lattner48130352010-01-13 06:38:18 +0000586 Name.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner8da8d4b2010-01-13 21:29:21 +0000587 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000588 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
589 getELFSectionFlags(Kind), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000590 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000591
Chris Lattnerf9650c02009-08-01 21:46:23 +0000592 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000593
Chris Lattner3b24c012009-08-04 05:35:56 +0000594 if (Kind.isMergeable1ByteCString() ||
595 Kind.isMergeable2ByteCString() ||
596 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000597
Chris Lattner067fe1a2009-07-29 04:54:38 +0000598 // We also need alignment here.
599 // FIXME: this is getting the alignment of the character, not the
600 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000601 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000602 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000603
Chris Lattner7e88a502009-08-04 16:19:50 +0000604 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000605 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000606 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000607 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000608 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000609 else
610 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000611
612
Chris Lattner7e88a502009-08-04 16:19:50 +0000613 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000614 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
615 MCSectionELF::SHF_ALLOC |
616 MCSectionELF::SHF_MERGE |
617 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000618 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000619 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000620
Chris Lattnerf9650c02009-08-01 21:46:23 +0000621 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000622 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000623 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000624 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000625 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000626 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000627 return MergeableConst16Section;
628 return ReadOnlySection; // .const
629 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000630
Chris Lattnerf9650c02009-08-01 21:46:23 +0000631 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000632
Chris Lattnerf9650c02009-08-01 21:46:23 +0000633 if (Kind.isThreadData()) return TLSDataSection;
634 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000635
Chris Lattnerc7fbe902010-01-19 03:06:01 +0000636 // Note: we claim that common symbols are put in BSSSection, but they are
637 // really emitted with the magic .comm directive, which creates a symbol table
638 // entry but not a section.
Chris Lattnera3839bc2010-01-19 02:48:26 +0000639 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000640
Chris Lattnerf9650c02009-08-01 21:46:23 +0000641 if (Kind.isDataNoRel()) return DataSection;
642 if (Kind.isDataRelLocal()) return DataRelLocalSection;
643 if (Kind.isDataRel()) return DataRelSection;
644 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000645
Chris Lattnerf9650c02009-08-01 21:46:23 +0000646 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000647 return DataRelROSection;
648}
649
Chris Lattner83d77fa2009-08-01 23:46:12 +0000650/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000651/// specified size and relocation information, return a section that it
652/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000653const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000654getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000655 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000656 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000657 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000658 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000659 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000660 return MergeableConst16Section;
661 if (Kind.isReadOnly())
662 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000663
Chris Lattnerf0144122009-07-28 03:13:23 +0000664 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
665 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
666 return DataRelROSection;
667}
668
669//===----------------------------------------------------------------------===//
670// MachO
671//===----------------------------------------------------------------------===//
672
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000673typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000674
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000675TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
676 // If we have the MachO uniquing map, free it.
677 delete (MachOUniqueMapTy*)UniquingMap;
678}
679
680
681const MCSectionMachO *TargetLoweringObjectFileMachO::
Daniel Dunbar2928c832009-11-06 10:58:06 +0000682getMachOSection(StringRef Segment, StringRef Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000683 unsigned TypeAndAttributes,
684 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000685 // We unique sections by their segment/section pair. The returned section
686 // may not have the same flags as the requested section, if so this should be
687 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000688
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000689 // Create the map if it doesn't already exist.
690 if (UniquingMap == 0)
691 UniquingMap = new MachOUniqueMapTy();
692 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000693
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000694 // Form the name to look up.
695 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000696 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000697 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000698 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000699
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000700 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000701 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000702 if (Entry) return Entry;
703
704 // Otherwise, return a new section.
705 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
706 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000707}
708
Chris Lattner11e96572009-08-03 21:53:27 +0000709
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000710void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
711 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000712 if (UniquingMap != 0)
713 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000714 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000715
Chris Lattnerff4bc462009-08-10 01:39:42 +0000716 TextSection // .text
717 = getMachOSection("__TEXT", "__text",
718 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
719 SectionKind::getText());
720 DataSection // .data
721 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000722
Chris Lattnerff4bc462009-08-10 01:39:42 +0000723 CStringSection // .cstring
724 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
725 SectionKind::getMergeable1ByteCString());
726 UStringSection
727 = getMachOSection("__TEXT","__ustring", 0,
728 SectionKind::getMergeable2ByteCString());
729 FourByteConstantSection // .literal4
730 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
731 SectionKind::getMergeableConst4());
732 EightByteConstantSection // .literal8
733 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
734 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000735
Chris Lattner4bb253c2009-07-28 17:50:28 +0000736 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
737 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000738 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000739 if (TM.getRelocationModel() != Reloc::Static &&
740 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000741 SixteenByteConstantSection = // .literal16
742 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000743 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000744
Chris Lattnerff4bc462009-08-10 01:39:42 +0000745 ReadOnlySection // .const
746 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000747
Chris Lattnerff4bc462009-08-10 01:39:42 +0000748 TextCoalSection
749 = getMachOSection("__TEXT", "__textcoal_nt",
750 MCSectionMachO::S_COALESCED |
751 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
752 SectionKind::getText());
753 ConstTextCoalSection
754 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
755 SectionKind::getText());
756 ConstDataCoalSection
757 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
758 SectionKind::getText());
Chris Lattneraac138e2010-01-19 02:09:44 +0000759 DataCommonSection
760 = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL,
761 SectionKind::getBSS());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000762 ConstDataSection // .const_data
763 = getMachOSection("__DATA", "__const", 0,
764 SectionKind::getReadOnlyWithRel());
765 DataCoalSection
766 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
767 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000768
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000769
Chris Lattnere309cfa2009-08-13 00:05:07 +0000770 LazySymbolPointerSection
771 = getMachOSection("__DATA", "__la_symbol_ptr",
772 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
773 SectionKind::getMetadata());
774 NonLazySymbolPointerSection
775 = getMachOSection("__DATA", "__nl_symbol_ptr",
776 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
777 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000778
Chris Lattner80ec2792009-08-02 00:34:36 +0000779 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000780 StaticCtorSection
781 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
782 StaticDtorSection
783 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000784 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000785 StaticCtorSection
786 = getMachOSection("__DATA", "__mod_init_func",
787 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
788 SectionKind::getDataRel());
789 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000790 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000791 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
792 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000793 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000794
Chris Lattner18a4c162009-08-02 07:24:22 +0000795 // Exception Handling.
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000796 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
797 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000798 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000799 getMachOSection("__TEXT", "__eh_frame",
800 MCSectionMachO::S_COALESCED |
801 MCSectionMachO::S_ATTR_NO_TOC |
802 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
803 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
804 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000805
806 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000807 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000808 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000809 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000810 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000811 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000812 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000813 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000814 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000815 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000816 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000817 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000818 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000819 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000820 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000821 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000822 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000823 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000824 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000825 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000826 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000827 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000828 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000829 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000830 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000831 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000832 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000833 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000834 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000835 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000836 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000837 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000838 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000839 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000840 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000841 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000842 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000843}
844
Chris Lattnera87dea42009-07-31 18:48:30 +0000845const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000846getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000847 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000848 // Parse the section specifier and create it if valid.
849 StringRef Segment, Section;
850 unsigned TAA, StubSize;
851 std::string ErrorCode =
852 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
853 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000854 if (!ErrorCode.empty()) {
855 // If invalid, report the error with llvm_report_error.
856 llvm_report_error("Global variable '" + GV->getNameStr() +
857 "' has an invalid section specifier '" + GV->getSection()+
858 "': " + ErrorCode + ".");
859 // Fall back to dropping it into the data section.
860 return DataSection;
861 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000862
Chris Lattnere309cfa2009-08-13 00:05:07 +0000863 // Get the section.
864 const MCSectionMachO *S =
865 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000866
Chris Lattnere309cfa2009-08-13 00:05:07 +0000867 // Okay, now that we got the section, verify that the TAA & StubSize agree.
868 // If the user declared multiple globals with different section flags, we need
869 // to reject it here.
870 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
871 // If invalid, report the error with llvm_report_error.
872 llvm_report_error("Global variable '" + GV->getNameStr() +
873 "' section type or attributes does not match previous"
874 " section specifier");
875 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000876
Chris Lattnere309cfa2009-08-13 00:05:07 +0000877 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000878}
879
880const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000881SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000882 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000883 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000884
Chris Lattnerf9650c02009-08-01 21:46:23 +0000885 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000886 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000887
Chris Lattnerf0144122009-07-28 03:13:23 +0000888 // If this is weak/linkonce, put this in a coalescable section, either in text
889 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000890 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000891 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000892 return ConstTextCoalSection;
893 return DataCoalSection;
894 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000895
Chris Lattnerf0144122009-07-28 03:13:23 +0000896 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000897 if (Kind.isMergeable1ByteCString() ||
898 Kind.isMergeable2ByteCString()) {
899 if (TM.getTargetData()->getPreferredAlignment(
900 cast<GlobalVariable>(GV)) < 32) {
901 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000902 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000903 assert(Kind.isMergeable2ByteCString());
904 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000905 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000906 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000907
Chris Lattnerf9650c02009-08-01 21:46:23 +0000908 if (Kind.isMergeableConst()) {
909 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000910 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000911 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000912 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000913 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000914 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000915 }
Chris Lattnerec409752009-08-04 16:27:13 +0000916
917 // Otherwise, if it is readonly, but not something we can specially optimize,
918 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000919 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000920 return ReadOnlySection;
921
922 // If this is marked const, put it into a const section. But if the dynamic
923 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000924 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000925 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000926
Chris Lattneraac138e2010-01-19 02:09:44 +0000927 // Put zero initialized globals with strong external linkage in the
928 // DATA, __common section with the .zerofill directive.
929 if (Kind.isBSS() && GV->hasExternalLinkage())
930 return DataCommonSection;
931
Chris Lattnerf0144122009-07-28 03:13:23 +0000932 // Otherwise, just drop the variable in the normal data section.
933 return DataSection;
934}
935
Chris Lattnera87dea42009-07-31 18:48:30 +0000936const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000937TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000938 // If this constant requires a relocation, we have to put it in the data
939 // segment, not in the text segment.
Eric Christophera07b7502010-01-07 19:44:05 +0000940 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000941 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000942
Chris Lattnerf0144122009-07-28 03:13:23 +0000943 if (Kind.isMergeableConst4())
944 return FourByteConstantSection;
945 if (Kind.isMergeableConst8())
946 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000947 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000948 return SixteenByteConstantSection;
949 return ReadOnlySection; // .const
950}
951
Chris Lattner26630c12009-07-31 20:52:39 +0000952/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
953/// not to emit the UsedDirective for some symbols in llvm.used.
954// FIXME: REMOVE this (rdar://7071300)
955bool TargetLoweringObjectFileMachO::
956shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
957 /// On Darwin, internally linked data beginning with "L" or "l" does not have
958 /// the directive emitted (this occurs in ObjC metadata).
959 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000960
Chris Lattner26630c12009-07-31 20:52:39 +0000961 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
962 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
963 // FIXME: ObjC metadata is currently emitted as internal symbols that have
964 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
965 // this horrible hack can go away.
Chris Lattner0b3735e2010-01-16 02:16:09 +0000966 SmallString<64> Name;
Chris Lattner036d8f92010-01-16 03:38:27 +0000967 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner26630c12009-07-31 20:52:39 +0000968 if (Name[0] == 'L' || Name[0] == 'l')
969 return false;
970 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000971
Chris Lattner26630c12009-07-31 20:52:39 +0000972 return true;
973}
974
Chris Lattner8c6ed052009-09-16 01:46:41 +0000975const MCExpr *TargetLoweringObjectFileMachO::
976getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000977 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000978 bool &IsIndirect, bool &IsPCRel) const {
979 // The mach-o version of this method defaults to returning a stub reference.
980 IsIndirect = true;
981 IsPCRel = false;
982
983 SmallString<128> Name;
984 Mang->getNameWithPrefix(Name, GV, true);
985 Name += "$non_lazy_ptr";
986 return MCSymbolRefExpr::Create(Name.str(), getContext());
987}
988
Chris Lattner26630c12009-07-31 20:52:39 +0000989
Chris Lattnerf0144122009-07-28 03:13:23 +0000990//===----------------------------------------------------------------------===//
991// COFF
992//===----------------------------------------------------------------------===//
993
Chris Lattner38cff382009-08-13 00:37:15 +0000994typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
995
996TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
997 delete (COFFUniqueMapTy*)UniquingMap;
998}
999
Chris Lattner0c0cb712009-08-08 20:22:20 +00001000
Chris Lattner11e96572009-08-03 21:53:27 +00001001const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner48130352010-01-13 06:38:18 +00001002getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +00001003 // Create the map if it doesn't already exist.
1004 if (UniquingMap == 0)
1005 UniquingMap = new MachOUniqueMapTy();
1006 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001007
Chris Lattner38cff382009-08-13 00:37:15 +00001008 // Do the lookup, if we have a hit, return it.
1009 const MCSectionCOFF *&Entry = Map[Name];
1010 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001011
Chris Lattner38cff382009-08-13 00:37:15 +00001012 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +00001013}
1014
Chris Lattnerf26e03b2009-07-31 17:42:42 +00001015void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1016 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001017 if (UniquingMap != 0)
1018 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001019 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001020 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1021 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001022 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001023 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001024 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001025 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001026
Chris Lattner35c35312009-08-18 16:56:17 +00001027 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1028 // though it contains relocatable pointers. In PIC mode, this is probably a
1029 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1030 // adjusted or this should be a data section.
1031 LSDASection =
1032 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1033 EHFrameSection =
1034 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001035
Chris Lattner18a4c162009-08-02 07:24:22 +00001036 // Debug info.
1037 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001038 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001039 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1040 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001041 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001042 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1043 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001044 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001045 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1046 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001047 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001048 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1049 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001050 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001051 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1052 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001053 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001054 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1055 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001056 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001057 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1058 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001059 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001060 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1061 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001062 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001063 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1064 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001065 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001066 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1067 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001068 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001069 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1070 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001071}
1072
Chris Lattner24f654c2009-08-06 16:39:58 +00001073const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001074getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001075 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001076 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001077}
1078
Chris Lattnerf0144122009-07-28 03:13:23 +00001079static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1080 if (Kind.isText())
1081 return ".text$linkonce";
1082 if (Kind.isWriteable())
1083 return ".data$linkonce";
1084 return ".rdata$linkonce";
1085}
1086
1087
Chris Lattnera87dea42009-07-31 18:48:30 +00001088const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001089SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001090 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001091 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001092
Chris Lattnerf0144122009-07-28 03:13:23 +00001093 // If this global is linkonce/weak and the target handles this by emitting it
1094 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001095 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001096 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner48130352010-01-13 06:38:18 +00001097 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattnercab16cc2010-01-13 19:19:17 +00001098 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner48130352010-01-13 06:38:18 +00001099 return getCOFFSection(Name.str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001100 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001101
Chris Lattnerf9650c02009-08-01 21:46:23 +00001102 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001103 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001104
Chris Lattnerf0144122009-07-28 03:13:23 +00001105 return getDataSection();
1106}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001107