blob: 229b1d52c5e7ae9f2c5082b06b2c4406ce1efdd0 [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 Lattnerf0144122009-07-28 03:13:23 +000024#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattner8f9b0f62009-11-07 09:20:54 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000028#include "llvm/Support/Mangler.h"
Chris Lattner5dc47ff2009-08-12 23:55:02 +000029#include "llvm/ADT/SmallString.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/ADT/StringExtras.h"
31using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// Generic Code
35//===----------------------------------------------------------------------===//
36
Chris Lattnera87dea42009-07-31 18:48:30 +000037TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000038 TextSection = 0;
39 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000040 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000041 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000042 StaticCtorSection = 0;
43 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000044 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000045 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000046
47 DwarfAbbrevSection = 0;
48 DwarfInfoSection = 0;
49 DwarfLineSection = 0;
50 DwarfFrameSection = 0;
51 DwarfPubNamesSection = 0;
52 DwarfPubTypesSection = 0;
53 DwarfDebugInlineSection = 0;
54 DwarfStrSection = 0;
55 DwarfLocSection = 0;
56 DwarfARangesSection = 0;
57 DwarfRangesSection = 0;
58 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000059}
60
61TargetLoweringObjectFile::~TargetLoweringObjectFile() {
62}
63
64static bool isSuitableForBSS(const GlobalVariable *GV) {
65 Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000066
Chris Lattnerf0144122009-07-28 03:13:23 +000067 // Must have zero initializer.
68 if (!C->isNullValue())
69 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000070
Chris Lattnerf0144122009-07-28 03:13:23 +000071 // Leave constant zeros in readonly constant sections, so they can be shared.
72 if (GV->isConstant())
73 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000074
Chris Lattnerf0144122009-07-28 03:13:23 +000075 // If the global has an explicit section specified, don't put it in BSS.
76 if (!GV->getSection().empty())
77 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000078
Chris Lattnerf0144122009-07-28 03:13:23 +000079 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
80 if (NoZerosInBSS)
81 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000082
Chris Lattnerf0144122009-07-28 03:13:23 +000083 // Otherwise, put it in BSS!
84 return true;
85}
86
Chris Lattner1850e5a2009-08-04 16:13:09 +000087/// IsNullTerminatedString - Return true if the specified constant (which is
88/// known to have a type that is an array of 1/2/4 byte elements) ends with a
89/// nul value and contains no other nuls in it.
90static bool IsNullTerminatedString(const Constant *C) {
91 const ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000092
Chris Lattnerf0144122009-07-28 03:13:23 +000093 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000094 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
95 if (ATy->getNumElements() == 0) return false;
96
97 ConstantInt *Null =
98 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
99 if (Null == 0 || Null->getZExtValue() != 0)
100 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000101
Chris Lattner1850e5a2009-08-04 16:13:09 +0000102 // Verify that the null doesn't occur anywhere else in the string.
103 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
104 // Reject constantexpr elements etc.
105 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
106 CVA->getOperand(i) == Null)
107 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000108 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000109 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000110
111 // Another possibility: [1 x i8] zeroinitializer
112 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000113 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000114
115 return false;
116}
117
Chris Lattner58bed8f2009-08-05 04:25:40 +0000118/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000119/// a global variable. Given an global variable and information from TM, it
120/// classifies the global in a variety of ways that make various target
121/// implementations simpler. The target implementation is free to ignore this
122/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000123SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
124 const TargetMachine &TM){
125 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
126 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000127
Chris Lattnerf0144122009-07-28 03:13:23 +0000128 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000129
Chris Lattnerf0144122009-07-28 03:13:23 +0000130 // Early exit - functions should be always in text sections.
131 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
132 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000133 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000134
Chris Lattnerf0144122009-07-28 03:13:23 +0000135 // Handle thread-local data first.
136 if (GVar->isThreadLocal()) {
137 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000138 return SectionKind::getThreadBSS();
139 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000140 }
141
142 // Variable can be easily put to BSS section.
143 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000144 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000145
146 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000147
Chris Lattnerf0144122009-07-28 03:13:23 +0000148 // If the global is marked constant, we can put it into a mergable section,
149 // a mergable string section, or general .data if it contains relocations.
150 if (GVar->isConstant()) {
151 // If the initializer for the global contains something that requires a
152 // relocation, then we may have to drop this into a wriable data section
153 // even though it is marked const.
154 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000155 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000156 case Constant::NoRelocation:
157 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000158 // section of the right width.
159 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000160 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000161 dyn_cast<IntegerType>(ATy->getElementType())) {
162 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
163 ITy->getBitWidth() == 32) &&
164 IsNullTerminatedString(C)) {
165 if (ITy->getBitWidth() == 8)
166 return SectionKind::getMergeable1ByteCString();
167 if (ITy->getBitWidth() == 16)
168 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000169
Chris Lattner1850e5a2009-08-04 16:13:09 +0000170 assert(ITy->getBitWidth() == 32 && "Unknown width");
171 return SectionKind::getMergeable4ByteCString();
172 }
173 }
174 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000175
Chris Lattnerf0144122009-07-28 03:13:23 +0000176 // Otherwise, just drop it into a mergable constant section. If we have
177 // a section for this size, use it, otherwise use the arbitrary sized
178 // mergable section.
179 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000180 case 4: return SectionKind::getMergeableConst4();
181 case 8: return SectionKind::getMergeableConst8();
182 case 16: return SectionKind::getMergeableConst16();
183 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000184 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000185
Chris Lattnerf0144122009-07-28 03:13:23 +0000186 case Constant::LocalRelocation:
187 // In static relocation model, the linker will resolve all addresses, so
188 // the relocation entries will actually be constants by the time the app
189 // starts up. However, we can't put this into a mergable section, because
190 // the linker doesn't take relocations into consideration when it tries to
191 // merge entries in the section.
192 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000193 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000194
Chris Lattnerf0144122009-07-28 03:13:23 +0000195 // Otherwise, the dynamic linker needs to fix it up, put it in the
196 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000197 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000198
Chris Lattnerf0144122009-07-28 03:13:23 +0000199 case Constant::GlobalRelocations:
200 // In static relocation model, the linker will resolve all addresses, so
201 // the relocation entries will actually be constants by the time the app
202 // starts up. However, we can't put this into a mergable section, because
203 // the linker doesn't take relocations into consideration when it tries to
204 // merge entries in the section.
205 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000206 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000207
Chris Lattnerf0144122009-07-28 03:13:23 +0000208 // Otherwise, the dynamic linker needs to fix it up, put it in the
209 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000210 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000211 }
212 }
213
214 // Okay, this isn't a constant. If the initializer for the global is going
215 // to require a runtime relocation by the dynamic linker, put it into a more
216 // specific section to improve startup time of the app. This coalesces these
217 // globals together onto fewer pages, improving the locality of the dynamic
218 // linker.
219 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000220 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000221
222 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000223 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000224 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000225 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000226 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000227 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000228 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000229 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000230 }
231}
232
233/// SectionForGlobal - This method computes the appropriate section to emit
234/// the specified global variable or function definition. This should not
235/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000236const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000237SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000238 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000239 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000240 if (GV->hasSection())
241 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000242
243
Chris Lattnerf0144122009-07-28 03:13:23 +0000244 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000245 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000246}
247
Chris Lattner58bed8f2009-08-05 04:25:40 +0000248
Chris Lattnerf0144122009-07-28 03:13:23 +0000249// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000250const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000251TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000252 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000253 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000254 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000255 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000256
Chris Lattnerf9650c02009-08-01 21:46:23 +0000257 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000258 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000259
Chris Lattner82458382009-08-01 21:56:13 +0000260 if (Kind.isBSS() && BSSSection != 0)
261 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000262
Chris Lattnerf9650c02009-08-01 21:46:23 +0000263 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000264 return ReadOnlySection;
265
266 return getDataSection();
267}
268
Chris Lattner83d77fa2009-08-01 23:46:12 +0000269/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000270/// specified size and relocation information, return a section that it
271/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000272const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000273TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000274 if (Kind.isReadOnly() && ReadOnlySection != 0)
275 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000276
Chris Lattnerf0144122009-07-28 03:13:23 +0000277 return DataSection;
278}
279
Chris Lattner8c6ed052009-09-16 01:46:41 +0000280/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
281/// pc-relative reference to the specified global variable from exception
282/// handling information. In addition to the symbol, this returns
283/// by-reference:
284///
285/// IsIndirect - True if the returned symbol is actually a stub that contains
286/// the address of the symbol, false if the symbol is the global itself.
287///
288/// IsPCRel - True if the symbol reference is already pc-relative, false if
289/// the caller needs to subtract off the address of the reference from the
290/// symbol.
291///
292const MCExpr *TargetLoweringObjectFile::
293getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000294 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000295 bool &IsIndirect, bool &IsPCRel) const {
296 // The generic implementation of this just returns a direct reference to the
297 // symbol.
298 IsIndirect = false;
299 IsPCRel = false;
300
301 SmallString<128> Name;
302 Mang->getNameWithPrefix(Name, GV, false);
303 return MCSymbolRefExpr::Create(Name.str(), getContext());
304}
Chris Lattnerf0144122009-07-28 03:13:23 +0000305
Chris Lattnerf0144122009-07-28 03:13:23 +0000306
307//===----------------------------------------------------------------------===//
308// ELF
309//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000310typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
311
312TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
313 // If we have the section uniquing map, free it.
314 delete (ELFUniqueMapTy*)UniquingMap;
315}
Chris Lattnerf0144122009-07-28 03:13:23 +0000316
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000317const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000318getELFSection(StringRef Section, unsigned Type, unsigned Flags,
319 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000320 if (UniquingMap == 0)
321 UniquingMap = new ELFUniqueMapTy();
322 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000323
Chris Lattner38cff382009-08-13 00:37:15 +0000324 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000325 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000326 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000327
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000328 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
329 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000330}
331
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000332void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
333 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000334 if (UniquingMap != 0)
335 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000336 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000337
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000338 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000339 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
340 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
341 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000342
343 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000344 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
345 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
346 SectionKind::getText());
347
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000348 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000349 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
350 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
351 SectionKind::getDataRel());
352
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000353 ReadOnlySection =
354 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
355 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000356 SectionKind::getReadOnly());
357
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000358 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000359 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000360 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000361 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000362
363 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000364 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
365 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
366 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000367
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000368 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000369 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
370 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
371 SectionKind::getDataRel());
372
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000373 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000374 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
375 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
376 SectionKind::getDataRelLocal());
377
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000378 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000379 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
380 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
381 SectionKind::getReadOnlyWithRel());
382
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000383 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000384 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
385 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
386 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000387
388 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000389 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
390 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
391 SectionKind::getMergeableConst4());
392
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000393 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000394 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
395 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
396 SectionKind::getMergeableConst8());
397
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000398 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000399 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
400 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
401 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000402
403 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000404 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000405 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
406 SectionKind::getDataRel());
407
Chris Lattner80ec2792009-08-02 00:34:36 +0000408 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000409 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
410 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
411 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000412
Chris Lattner18a4c162009-08-02 07:24:22 +0000413 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000414
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000415 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
416 // it contains relocatable pointers. In PIC mode, this is probably a big
417 // runtime hit for C++ apps. Either the contents of the LSDA need to be
418 // adjusted or this should be a data section.
419 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000420 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
421 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000422 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000423 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000424 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
425 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000426
Chris Lattner18a4c162009-08-02 07:24:22 +0000427 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000428 DwarfAbbrevSection =
429 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000430 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000431 DwarfInfoSection =
432 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000433 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000434 DwarfLineSection =
435 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000436 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000437 DwarfFrameSection =
438 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000439 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000440 DwarfPubNamesSection =
441 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000442 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000443 DwarfPubTypesSection =
444 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000445 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000446 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000447 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
448 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000449 DwarfLocSection =
450 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000451 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000452 DwarfARangesSection =
453 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000454 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000455 DwarfRangesSection =
456 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000457 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000458 DwarfMacroInfoSection =
459 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000460 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000461}
462
463
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000464static SectionKind
Chris Lattner24f654c2009-08-06 16:39:58 +0000465getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000466 if (Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000467
Chris Lattnerf0144122009-07-28 03:13:23 +0000468 // Some lame default implementation based on some magic section names.
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000469 if (strcmp(Name, ".bss") == 0 ||
470 strncmp(Name, ".bss.", 5) == 0 ||
471 strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000472 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000473 strcmp(Name, ".sbss") == 0 ||
474 strncmp(Name, ".sbss.", 6) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000475 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
476 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000477 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000478
Chris Lattnerf0144122009-07-28 03:13:23 +0000479 if (strcmp(Name, ".tdata") == 0 ||
480 strncmp(Name, ".tdata.", 7) == 0 ||
481 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
482 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000483 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000484
Chris Lattnerf0144122009-07-28 03:13:23 +0000485 if (strcmp(Name, ".tbss") == 0 ||
486 strncmp(Name, ".tbss.", 6) == 0 ||
487 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
488 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000489 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000490
Chris Lattnerf0144122009-07-28 03:13:23 +0000491 return K;
492}
493
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000494
495static unsigned
496getELFSectionType(const char *Name, SectionKind K) {
497
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000498 if (strcmp(Name, ".init_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000499 return MCSectionELF::SHT_INIT_ARRAY;
500
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000501 if (strcmp(Name, ".fini_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000502 return MCSectionELF::SHT_FINI_ARRAY;
503
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000504 if (strcmp(Name, ".preinit_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000505 return MCSectionELF::SHT_PREINIT_ARRAY;
506
507 if (K.isBSS() || K.isThreadBSS())
508 return MCSectionELF::SHT_NOBITS;
509
510 return MCSectionELF::SHT_PROGBITS;
511}
512
513
514static unsigned
515getELFSectionFlags(SectionKind K) {
516 unsigned Flags = 0;
517
518 if (!K.isMetadata())
519 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000520
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000521 if (K.isText())
522 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000523
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000524 if (K.isWriteable())
525 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000526
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000527 if (K.isThreadLocal())
528 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000529
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000530 // K.isMergeableConst() is left out to honour PR4650
531 if (K.isMergeableCString() || K.isMergeableConst4() ||
532 K.isMergeableConst8() || K.isMergeableConst16())
533 Flags |= MCSectionELF::SHF_MERGE;
534
535 if (K.isMergeableCString())
536 Flags |= MCSectionELF::SHF_STRINGS;
537
538 return Flags;
539}
540
541
Chris Lattner24f654c2009-08-06 16:39:58 +0000542const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000543getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000544 Mangler *Mang, const TargetMachine &TM) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000545 const char *SectionName = GV->getSection().c_str();
546
Chris Lattner24f654c2009-08-06 16:39:58 +0000547 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000548 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000549
550 return getELFSection(SectionName,
551 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000552 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000553}
Chris Lattnerf0144122009-07-28 03:13:23 +0000554
555static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
556 if (Kind.isText()) return ".gnu.linkonce.t.";
557 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000558
Chris Lattnerf0144122009-07-28 03:13:23 +0000559 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
560 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000561
Chris Lattnerf0144122009-07-28 03:13:23 +0000562 if (Kind.isBSS()) return ".gnu.linkonce.b.";
563 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
564 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
565 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
566 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000567
Chris Lattnerf0144122009-07-28 03:13:23 +0000568 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
569 return ".gnu.linkonce.d.rel.ro.";
570}
571
Chris Lattnera87dea42009-07-31 18:48:30 +0000572const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000573SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000574 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000575
Chris Lattnerf0144122009-07-28 03:13:23 +0000576 // If this global is linkonce/weak and the target handles this by emitting it
577 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000578 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000579 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000580 std::string Name = Mang->makeNameProper(GV->getNameStr());
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000581
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000582 return getELFSection((Prefix+Name).c_str(),
583 getELFSectionType((Prefix+Name).c_str(), Kind),
584 getELFSectionFlags(Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000585 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000586 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000587
Chris Lattnerf9650c02009-08-01 21:46:23 +0000588 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000589
Chris Lattner3b24c012009-08-04 05:35:56 +0000590 if (Kind.isMergeable1ByteCString() ||
591 Kind.isMergeable2ByteCString() ||
592 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000593
Chris Lattner067fe1a2009-07-29 04:54:38 +0000594 // We also need alignment here.
595 // FIXME: this is getting the alignment of the character, not the
596 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000597 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000598 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000599
Chris Lattner7e88a502009-08-04 16:19:50 +0000600 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000601 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000602 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000603 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000604 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000605 else
606 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000607
608
Chris Lattner7e88a502009-08-04 16:19:50 +0000609 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000610 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
611 MCSectionELF::SHF_ALLOC |
612 MCSectionELF::SHF_MERGE |
613 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000614 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000615 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000616
Chris Lattnerf9650c02009-08-01 21:46:23 +0000617 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000618 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000619 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000620 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000621 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000622 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000623 return MergeableConst16Section;
624 return ReadOnlySection; // .const
625 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000626
Chris Lattnerf9650c02009-08-01 21:46:23 +0000627 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000628
Chris Lattnerf9650c02009-08-01 21:46:23 +0000629 if (Kind.isThreadData()) return TLSDataSection;
630 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000631
Chris Lattner82458382009-08-01 21:56:13 +0000632 if (Kind.isBSS()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000633
Chris Lattnerf9650c02009-08-01 21:46:23 +0000634 if (Kind.isDataNoRel()) return DataSection;
635 if (Kind.isDataRelLocal()) return DataRelLocalSection;
636 if (Kind.isDataRel()) return DataRelSection;
637 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000638
Chris Lattnerf9650c02009-08-01 21:46:23 +0000639 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000640 return DataRelROSection;
641}
642
Chris Lattner83d77fa2009-08-01 23:46:12 +0000643/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000644/// specified size and relocation information, return a section that it
645/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000646const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000647getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000648 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000649 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000650 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000651 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000652 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000653 return MergeableConst16Section;
654 if (Kind.isReadOnly())
655 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000656
Chris Lattnerf0144122009-07-28 03:13:23 +0000657 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
658 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
659 return DataRelROSection;
660}
661
662//===----------------------------------------------------------------------===//
663// MachO
664//===----------------------------------------------------------------------===//
665
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000666typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000667
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000668TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
669 // If we have the MachO uniquing map, free it.
670 delete (MachOUniqueMapTy*)UniquingMap;
671}
672
673
674const MCSectionMachO *TargetLoweringObjectFileMachO::
Daniel Dunbar2928c832009-11-06 10:58:06 +0000675getMachOSection(StringRef Segment, StringRef Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000676 unsigned TypeAndAttributes,
677 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000678 // We unique sections by their segment/section pair. The returned section
679 // may not have the same flags as the requested section, if so this should be
680 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000681
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000682 // Create the map if it doesn't already exist.
683 if (UniquingMap == 0)
684 UniquingMap = new MachOUniqueMapTy();
685 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000686
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000687 // Form the name to look up.
688 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000689 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000690 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000691 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000692
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000693 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000694 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000695 if (Entry) return Entry;
696
697 // Otherwise, return a new section.
698 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
699 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000700}
701
Chris Lattner11e96572009-08-03 21:53:27 +0000702
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000703void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
704 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000705 if (UniquingMap != 0)
706 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000707 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000708
Chris Lattnerff4bc462009-08-10 01:39:42 +0000709 TextSection // .text
710 = getMachOSection("__TEXT", "__text",
711 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
712 SectionKind::getText());
713 DataSection // .data
714 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000715
Chris Lattnerff4bc462009-08-10 01:39:42 +0000716 CStringSection // .cstring
717 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
718 SectionKind::getMergeable1ByteCString());
719 UStringSection
720 = getMachOSection("__TEXT","__ustring", 0,
721 SectionKind::getMergeable2ByteCString());
722 FourByteConstantSection // .literal4
723 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
724 SectionKind::getMergeableConst4());
725 EightByteConstantSection // .literal8
726 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
727 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000728
Chris Lattner4bb253c2009-07-28 17:50:28 +0000729 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
730 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000731 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000732 if (TM.getRelocationModel() != Reloc::Static &&
733 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000734 SixteenByteConstantSection = // .literal16
735 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000736 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000737
Chris Lattnerff4bc462009-08-10 01:39:42 +0000738 ReadOnlySection // .const
739 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000740
Chris Lattnerff4bc462009-08-10 01:39:42 +0000741 TextCoalSection
742 = getMachOSection("__TEXT", "__textcoal_nt",
743 MCSectionMachO::S_COALESCED |
744 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
745 SectionKind::getText());
746 ConstTextCoalSection
747 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
748 SectionKind::getText());
749 ConstDataCoalSection
750 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
751 SectionKind::getText());
752 ConstDataSection // .const_data
753 = getMachOSection("__DATA", "__const", 0,
754 SectionKind::getReadOnlyWithRel());
755 DataCoalSection
756 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
757 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000758
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000759
Chris Lattnere309cfa2009-08-13 00:05:07 +0000760 LazySymbolPointerSection
761 = getMachOSection("__DATA", "__la_symbol_ptr",
762 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
763 SectionKind::getMetadata());
764 NonLazySymbolPointerSection
765 = getMachOSection("__DATA", "__nl_symbol_ptr",
766 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
767 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000768
Chris Lattner80ec2792009-08-02 00:34:36 +0000769 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000770 StaticCtorSection
771 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
772 StaticDtorSection
773 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000774 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000775 StaticCtorSection
776 = getMachOSection("__DATA", "__mod_init_func",
777 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
778 SectionKind::getDataRel());
779 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000780 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000781 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
782 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000783 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000784
Chris Lattner18a4c162009-08-02 07:24:22 +0000785 // Exception Handling.
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000786 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
787 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000788 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000789 getMachOSection("__TEXT", "__eh_frame",
790 MCSectionMachO::S_COALESCED |
791 MCSectionMachO::S_ATTR_NO_TOC |
792 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
793 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
794 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000795
796 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000797 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000798 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000799 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000800 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000801 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000802 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000803 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000804 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000805 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000806 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000807 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000808 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000809 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000810 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000811 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000812 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000813 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000814 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000815 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000816 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000817 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000818 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000819 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000820 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000821 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000822 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000823 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000824 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000825 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000826 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000827 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000828 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000829 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000830 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000831 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000832 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000833}
834
Chris Lattnera87dea42009-07-31 18:48:30 +0000835const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000836getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000837 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000838 // Parse the section specifier and create it if valid.
839 StringRef Segment, Section;
840 unsigned TAA, StubSize;
841 std::string ErrorCode =
842 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
843 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000844 if (!ErrorCode.empty()) {
845 // If invalid, report the error with llvm_report_error.
846 llvm_report_error("Global variable '" + GV->getNameStr() +
847 "' has an invalid section specifier '" + GV->getSection()+
848 "': " + ErrorCode + ".");
849 // Fall back to dropping it into the data section.
850 return DataSection;
851 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000852
Chris Lattnere309cfa2009-08-13 00:05:07 +0000853 // Get the section.
854 const MCSectionMachO *S =
855 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000856
Chris Lattnere309cfa2009-08-13 00:05:07 +0000857 // Okay, now that we got the section, verify that the TAA & StubSize agree.
858 // If the user declared multiple globals with different section flags, we need
859 // to reject it here.
860 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
861 // If invalid, report the error with llvm_report_error.
862 llvm_report_error("Global variable '" + GV->getNameStr() +
863 "' section type or attributes does not match previous"
864 " section specifier");
865 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000866
Chris Lattnere309cfa2009-08-13 00:05:07 +0000867 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000868}
869
870const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000871SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000872 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000873 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000874
Chris Lattnerf9650c02009-08-01 21:46:23 +0000875 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000876 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000877
Chris Lattnerf0144122009-07-28 03:13:23 +0000878 // If this is weak/linkonce, put this in a coalescable section, either in text
879 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000880 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000881 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000882 return ConstTextCoalSection;
883 return DataCoalSection;
884 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000885
Chris Lattnerf0144122009-07-28 03:13:23 +0000886 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000887 if (Kind.isMergeable1ByteCString() ||
888 Kind.isMergeable2ByteCString()) {
889 if (TM.getTargetData()->getPreferredAlignment(
890 cast<GlobalVariable>(GV)) < 32) {
891 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000892 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000893 assert(Kind.isMergeable2ByteCString());
894 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000895 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000896 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000897
Chris Lattnerf9650c02009-08-01 21:46:23 +0000898 if (Kind.isMergeableConst()) {
899 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000900 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000901 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000902 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000903 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000904 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000905 }
Chris Lattnerec409752009-08-04 16:27:13 +0000906
907 // Otherwise, if it is readonly, but not something we can specially optimize,
908 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000909 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000910 return ReadOnlySection;
911
912 // If this is marked const, put it into a const section. But if the dynamic
913 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000914 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000915 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000916
Chris Lattnerf0144122009-07-28 03:13:23 +0000917 // Otherwise, just drop the variable in the normal data section.
918 return DataSection;
919}
920
Chris Lattnera87dea42009-07-31 18:48:30 +0000921const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000922TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000923 // If this constant requires a relocation, we have to put it in the data
924 // segment, not in the text segment.
Eric Christophera07b7502010-01-07 19:44:05 +0000925 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000926 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000927
Chris Lattnerf0144122009-07-28 03:13:23 +0000928 if (Kind.isMergeableConst4())
929 return FourByteConstantSection;
930 if (Kind.isMergeableConst8())
931 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000932 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000933 return SixteenByteConstantSection;
934 return ReadOnlySection; // .const
935}
936
Chris Lattner26630c12009-07-31 20:52:39 +0000937/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
938/// not to emit the UsedDirective for some symbols in llvm.used.
939// FIXME: REMOVE this (rdar://7071300)
940bool TargetLoweringObjectFileMachO::
941shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
942 /// On Darwin, internally linked data beginning with "L" or "l" does not have
943 /// the directive emitted (this occurs in ObjC metadata).
944 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000945
Chris Lattner26630c12009-07-31 20:52:39 +0000946 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
947 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
948 // FIXME: ObjC metadata is currently emitted as internal symbols that have
949 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
950 // this horrible hack can go away.
951 const std::string &Name = Mang->getMangledName(GV);
952 if (Name[0] == 'L' || Name[0] == 'l')
953 return false;
954 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000955
Chris Lattner26630c12009-07-31 20:52:39 +0000956 return true;
957}
958
Chris Lattner8c6ed052009-09-16 01:46:41 +0000959const MCExpr *TargetLoweringObjectFileMachO::
960getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000961 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000962 bool &IsIndirect, bool &IsPCRel) const {
963 // The mach-o version of this method defaults to returning a stub reference.
964 IsIndirect = true;
965 IsPCRel = false;
966
967 SmallString<128> Name;
968 Mang->getNameWithPrefix(Name, GV, true);
969 Name += "$non_lazy_ptr";
970 return MCSymbolRefExpr::Create(Name.str(), getContext());
971}
972
Chris Lattner26630c12009-07-31 20:52:39 +0000973
Chris Lattnerf0144122009-07-28 03:13:23 +0000974//===----------------------------------------------------------------------===//
975// COFF
976//===----------------------------------------------------------------------===//
977
Chris Lattner38cff382009-08-13 00:37:15 +0000978typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
979
980TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
981 delete (COFFUniqueMapTy*)UniquingMap;
982}
983
Chris Lattner0c0cb712009-08-08 20:22:20 +0000984
Chris Lattner11e96572009-08-03 21:53:27 +0000985const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000986getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000987 // Create the map if it doesn't already exist.
988 if (UniquingMap == 0)
989 UniquingMap = new MachOUniqueMapTy();
990 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000991
Chris Lattner38cff382009-08-13 00:37:15 +0000992 // Do the lookup, if we have a hit, return it.
993 const MCSectionCOFF *&Entry = Map[Name];
994 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000995
Chris Lattner38cff382009-08-13 00:37:15 +0000996 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000997}
998
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000999void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1000 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001001 if (UniquingMap != 0)
1002 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001003 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001004 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1005 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001006 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001007 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001008 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001009 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001010
Chris Lattner35c35312009-08-18 16:56:17 +00001011 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1012 // though it contains relocatable pointers. In PIC mode, this is probably a
1013 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1014 // adjusted or this should be a data section.
1015 LSDASection =
1016 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1017 EHFrameSection =
1018 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001019
Chris Lattner18a4c162009-08-02 07:24:22 +00001020 // Debug info.
1021 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001022 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001023 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1024 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001025 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001026 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1027 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001028 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001029 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1030 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001031 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001032 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1033 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001034 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001035 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1036 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001037 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001038 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1039 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001040 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001041 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1042 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001043 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001044 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1045 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001046 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001047 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1048 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001049 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001050 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1051 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001052 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001053 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1054 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001055}
1056
Chris Lattner24f654c2009-08-06 16:39:58 +00001057const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001058getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001059 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001060 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001061}
1062
Chris Lattnerf0144122009-07-28 03:13:23 +00001063static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1064 if (Kind.isText())
1065 return ".text$linkonce";
1066 if (Kind.isWriteable())
1067 return ".data$linkonce";
1068 return ".rdata$linkonce";
1069}
1070
1071
Chris Lattnera87dea42009-07-31 18:48:30 +00001072const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001073SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001074 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001075 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001076
Chris Lattnerf0144122009-07-28 03:13:23 +00001077 // If this global is linkonce/weak and the target handles this by emitting it
1078 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001079 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001080 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +00001081 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +00001082 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001083 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001084
Chris Lattnerf9650c02009-08-01 21:46:23 +00001085 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001086 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001087
Chris Lattnerf0144122009-07-28 03:13:23 +00001088 return getDataSection();
1089}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001090