blob: 14f510cb2ac137927fce4ebda19a5413e5b758a0 [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
Chris Lattner48130352010-01-13 06:38:18 +0000495static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000496
Chris Lattner48130352010-01-13 06:38:18 +0000497 if (Name == ".init_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000498 return MCSectionELF::SHT_INIT_ARRAY;
499
Chris Lattner48130352010-01-13 06:38:18 +0000500 if (Name == ".fini_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000501 return MCSectionELF::SHT_FINI_ARRAY;
502
Chris Lattner48130352010-01-13 06:38:18 +0000503 if (Name == ".preinit_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000504 return MCSectionELF::SHT_PREINIT_ARRAY;
505
506 if (K.isBSS() || K.isThreadBSS())
507 return MCSectionELF::SHT_NOBITS;
508
509 return MCSectionELF::SHT_PROGBITS;
510}
511
512
513static unsigned
514getELFSectionFlags(SectionKind K) {
515 unsigned Flags = 0;
516
517 if (!K.isMetadata())
518 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000519
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000520 if (K.isText())
521 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000522
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000523 if (K.isWriteable())
524 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000525
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000526 if (K.isThreadLocal())
527 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000528
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000529 // K.isMergeableConst() is left out to honour PR4650
530 if (K.isMergeableCString() || K.isMergeableConst4() ||
531 K.isMergeableConst8() || K.isMergeableConst16())
532 Flags |= MCSectionELF::SHF_MERGE;
533
534 if (K.isMergeableCString())
535 Flags |= MCSectionELF::SHF_STRINGS;
536
537 return Flags;
538}
539
540
Chris Lattner24f654c2009-08-06 16:39:58 +0000541const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000542getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000543 Mangler *Mang, const TargetMachine &TM) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000544 const char *SectionName = GV->getSection().c_str();
545
Chris Lattner24f654c2009-08-06 16:39:58 +0000546 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000547 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000548
549 return getELFSection(SectionName,
550 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000551 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000552}
Chris Lattnerf0144122009-07-28 03:13:23 +0000553
554static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
555 if (Kind.isText()) return ".gnu.linkonce.t.";
556 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000557
Chris Lattnerf0144122009-07-28 03:13:23 +0000558 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
559 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000560
Chris Lattnerf0144122009-07-28 03:13:23 +0000561 if (Kind.isBSS()) return ".gnu.linkonce.b.";
562 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
563 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
564 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
565 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000566
Chris Lattnerf0144122009-07-28 03:13:23 +0000567 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
568 return ".gnu.linkonce.d.rel.ro.";
569}
570
Chris Lattnera87dea42009-07-31 18:48:30 +0000571const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000572SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000573 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000574
Chris Lattnerf0144122009-07-28 03:13:23 +0000575 // If this global is linkonce/weak and the target handles this by emitting it
576 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000577 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000578 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattner48130352010-01-13 06:38:18 +0000579 SmallString<128> Name;
580 Name.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner4d5f06f2010-01-13 08:02:14 +0000581 // FIXME: This will fail for weak globals with no names, this also depends
582 // on the mangling behavior of makeNameProper to mangle the section name
583 // before construction. Instead, this should use getNameWithPrefix on the
584 // global variable and the MCSection printing code should do the mangling.
Chris Lattner48130352010-01-13 06:38:18 +0000585 Mang->makeNameProper(Name, GV->getName());
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000586
Chris Lattner48130352010-01-13 06:38:18 +0000587 return getELFSection(Name.str(),
588 getELFSectionType(Name.str(), Kind),
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000589 getELFSectionFlags(Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000590 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000591 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000592
Chris Lattnerf9650c02009-08-01 21:46:23 +0000593 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000594
Chris Lattner3b24c012009-08-04 05:35:56 +0000595 if (Kind.isMergeable1ByteCString() ||
596 Kind.isMergeable2ByteCString() ||
597 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000598
Chris Lattner067fe1a2009-07-29 04:54:38 +0000599 // We also need alignment here.
600 // FIXME: this is getting the alignment of the character, not the
601 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000602 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000603 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000604
Chris Lattner7e88a502009-08-04 16:19:50 +0000605 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000606 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000607 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000608 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000609 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000610 else
611 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000612
613
Chris Lattner7e88a502009-08-04 16:19:50 +0000614 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000615 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
616 MCSectionELF::SHF_ALLOC |
617 MCSectionELF::SHF_MERGE |
618 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000619 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000620 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000621
Chris Lattnerf9650c02009-08-01 21:46:23 +0000622 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000623 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000624 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000625 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000626 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000627 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000628 return MergeableConst16Section;
629 return ReadOnlySection; // .const
630 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000631
Chris Lattnerf9650c02009-08-01 21:46:23 +0000632 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000633
Chris Lattnerf9650c02009-08-01 21:46:23 +0000634 if (Kind.isThreadData()) return TLSDataSection;
635 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000636
Chris Lattner82458382009-08-01 21:56:13 +0000637 if (Kind.isBSS()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000638
Chris Lattnerf9650c02009-08-01 21:46:23 +0000639 if (Kind.isDataNoRel()) return DataSection;
640 if (Kind.isDataRelLocal()) return DataRelLocalSection;
641 if (Kind.isDataRel()) return DataRelSection;
642 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000643
Chris Lattnerf9650c02009-08-01 21:46:23 +0000644 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000645 return DataRelROSection;
646}
647
Chris Lattner83d77fa2009-08-01 23:46:12 +0000648/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000649/// specified size and relocation information, return a section that it
650/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000651const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000652getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000653 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000654 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000655 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000656 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000657 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000658 return MergeableConst16Section;
659 if (Kind.isReadOnly())
660 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000661
Chris Lattnerf0144122009-07-28 03:13:23 +0000662 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
663 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
664 return DataRelROSection;
665}
666
667//===----------------------------------------------------------------------===//
668// MachO
669//===----------------------------------------------------------------------===//
670
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000671typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000672
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000673TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
674 // If we have the MachO uniquing map, free it.
675 delete (MachOUniqueMapTy*)UniquingMap;
676}
677
678
679const MCSectionMachO *TargetLoweringObjectFileMachO::
Daniel Dunbar2928c832009-11-06 10:58:06 +0000680getMachOSection(StringRef Segment, StringRef Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000681 unsigned TypeAndAttributes,
682 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000683 // We unique sections by their segment/section pair. The returned section
684 // may not have the same flags as the requested section, if so this should be
685 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000686
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000687 // Create the map if it doesn't already exist.
688 if (UniquingMap == 0)
689 UniquingMap = new MachOUniqueMapTy();
690 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000691
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000692 // Form the name to look up.
693 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000694 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000695 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000696 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000697
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000698 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000699 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000700 if (Entry) return Entry;
701
702 // Otherwise, return a new section.
703 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
704 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000705}
706
Chris Lattner11e96572009-08-03 21:53:27 +0000707
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000708void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
709 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000710 if (UniquingMap != 0)
711 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000712 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000713
Chris Lattnerff4bc462009-08-10 01:39:42 +0000714 TextSection // .text
715 = getMachOSection("__TEXT", "__text",
716 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
717 SectionKind::getText());
718 DataSection // .data
719 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000720
Chris Lattnerff4bc462009-08-10 01:39:42 +0000721 CStringSection // .cstring
722 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
723 SectionKind::getMergeable1ByteCString());
724 UStringSection
725 = getMachOSection("__TEXT","__ustring", 0,
726 SectionKind::getMergeable2ByteCString());
727 FourByteConstantSection // .literal4
728 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
729 SectionKind::getMergeableConst4());
730 EightByteConstantSection // .literal8
731 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
732 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000733
Chris Lattner4bb253c2009-07-28 17:50:28 +0000734 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
735 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000736 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000737 if (TM.getRelocationModel() != Reloc::Static &&
738 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000739 SixteenByteConstantSection = // .literal16
740 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000741 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000742
Chris Lattnerff4bc462009-08-10 01:39:42 +0000743 ReadOnlySection // .const
744 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000745
Chris Lattnerff4bc462009-08-10 01:39:42 +0000746 TextCoalSection
747 = getMachOSection("__TEXT", "__textcoal_nt",
748 MCSectionMachO::S_COALESCED |
749 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
750 SectionKind::getText());
751 ConstTextCoalSection
752 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
753 SectionKind::getText());
754 ConstDataCoalSection
755 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
756 SectionKind::getText());
757 ConstDataSection // .const_data
758 = getMachOSection("__DATA", "__const", 0,
759 SectionKind::getReadOnlyWithRel());
760 DataCoalSection
761 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
762 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000763
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000764
Chris Lattnere309cfa2009-08-13 00:05:07 +0000765 LazySymbolPointerSection
766 = getMachOSection("__DATA", "__la_symbol_ptr",
767 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
768 SectionKind::getMetadata());
769 NonLazySymbolPointerSection
770 = getMachOSection("__DATA", "__nl_symbol_ptr",
771 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
772 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000773
Chris Lattner80ec2792009-08-02 00:34:36 +0000774 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000775 StaticCtorSection
776 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
777 StaticDtorSection
778 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000779 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000780 StaticCtorSection
781 = getMachOSection("__DATA", "__mod_init_func",
782 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
783 SectionKind::getDataRel());
784 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000785 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000786 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
787 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000788 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000789
Chris Lattner18a4c162009-08-02 07:24:22 +0000790 // Exception Handling.
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000791 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
792 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000793 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000794 getMachOSection("__TEXT", "__eh_frame",
795 MCSectionMachO::S_COALESCED |
796 MCSectionMachO::S_ATTR_NO_TOC |
797 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
798 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
799 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000800
801 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000802 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000803 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000804 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000805 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000806 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000807 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000808 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000809 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000810 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000811 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000812 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000813 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000814 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000815 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000816 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000817 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000818 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000819 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000820 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000821 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000822 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000823 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000824 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000825 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000826 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000827 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000828 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000829 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000830 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000831 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000832 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000833 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000834 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000835 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000836 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000837 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000838}
839
Chris Lattnera87dea42009-07-31 18:48:30 +0000840const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000841getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000842 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000843 // Parse the section specifier and create it if valid.
844 StringRef Segment, Section;
845 unsigned TAA, StubSize;
846 std::string ErrorCode =
847 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
848 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000849 if (!ErrorCode.empty()) {
850 // If invalid, report the error with llvm_report_error.
851 llvm_report_error("Global variable '" + GV->getNameStr() +
852 "' has an invalid section specifier '" + GV->getSection()+
853 "': " + ErrorCode + ".");
854 // Fall back to dropping it into the data section.
855 return DataSection;
856 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000857
Chris Lattnere309cfa2009-08-13 00:05:07 +0000858 // Get the section.
859 const MCSectionMachO *S =
860 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000861
Chris Lattnere309cfa2009-08-13 00:05:07 +0000862 // Okay, now that we got the section, verify that the TAA & StubSize agree.
863 // If the user declared multiple globals with different section flags, we need
864 // to reject it here.
865 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
866 // If invalid, report the error with llvm_report_error.
867 llvm_report_error("Global variable '" + GV->getNameStr() +
868 "' section type or attributes does not match previous"
869 " section specifier");
870 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000871
Chris Lattnere309cfa2009-08-13 00:05:07 +0000872 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000873}
874
875const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000876SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000877 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000878 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000879
Chris Lattnerf9650c02009-08-01 21:46:23 +0000880 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000881 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000882
Chris Lattnerf0144122009-07-28 03:13:23 +0000883 // If this is weak/linkonce, put this in a coalescable section, either in text
884 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000885 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000886 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000887 return ConstTextCoalSection;
888 return DataCoalSection;
889 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000890
Chris Lattnerf0144122009-07-28 03:13:23 +0000891 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000892 if (Kind.isMergeable1ByteCString() ||
893 Kind.isMergeable2ByteCString()) {
894 if (TM.getTargetData()->getPreferredAlignment(
895 cast<GlobalVariable>(GV)) < 32) {
896 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000897 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000898 assert(Kind.isMergeable2ByteCString());
899 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000900 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000901 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000902
Chris Lattnerf9650c02009-08-01 21:46:23 +0000903 if (Kind.isMergeableConst()) {
904 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000905 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000906 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000907 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000908 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000909 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000910 }
Chris Lattnerec409752009-08-04 16:27:13 +0000911
912 // Otherwise, if it is readonly, but not something we can specially optimize,
913 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000914 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000915 return ReadOnlySection;
916
917 // If this is marked const, put it into a const section. But if the dynamic
918 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000919 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000920 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000921
Chris Lattnerf0144122009-07-28 03:13:23 +0000922 // Otherwise, just drop the variable in the normal data section.
923 return DataSection;
924}
925
Chris Lattnera87dea42009-07-31 18:48:30 +0000926const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000927TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000928 // If this constant requires a relocation, we have to put it in the data
929 // segment, not in the text segment.
Eric Christophera07b7502010-01-07 19:44:05 +0000930 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000931 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000932
Chris Lattnerf0144122009-07-28 03:13:23 +0000933 if (Kind.isMergeableConst4())
934 return FourByteConstantSection;
935 if (Kind.isMergeableConst8())
936 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000937 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000938 return SixteenByteConstantSection;
939 return ReadOnlySection; // .const
940}
941
Chris Lattner26630c12009-07-31 20:52:39 +0000942/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
943/// not to emit the UsedDirective for some symbols in llvm.used.
944// FIXME: REMOVE this (rdar://7071300)
945bool TargetLoweringObjectFileMachO::
946shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
947 /// On Darwin, internally linked data beginning with "L" or "l" does not have
948 /// the directive emitted (this occurs in ObjC metadata).
949 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000950
Chris Lattner26630c12009-07-31 20:52:39 +0000951 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
952 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
953 // FIXME: ObjC metadata is currently emitted as internal symbols that have
954 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
955 // this horrible hack can go away.
956 const std::string &Name = Mang->getMangledName(GV);
957 if (Name[0] == 'L' || Name[0] == 'l')
958 return false;
959 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000960
Chris Lattner26630c12009-07-31 20:52:39 +0000961 return true;
962}
963
Chris Lattner8c6ed052009-09-16 01:46:41 +0000964const MCExpr *TargetLoweringObjectFileMachO::
965getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000966 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000967 bool &IsIndirect, bool &IsPCRel) const {
968 // The mach-o version of this method defaults to returning a stub reference.
969 IsIndirect = true;
970 IsPCRel = false;
971
972 SmallString<128> Name;
973 Mang->getNameWithPrefix(Name, GV, true);
974 Name += "$non_lazy_ptr";
975 return MCSymbolRefExpr::Create(Name.str(), getContext());
976}
977
Chris Lattner26630c12009-07-31 20:52:39 +0000978
Chris Lattnerf0144122009-07-28 03:13:23 +0000979//===----------------------------------------------------------------------===//
980// COFF
981//===----------------------------------------------------------------------===//
982
Chris Lattner38cff382009-08-13 00:37:15 +0000983typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
984
985TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
986 delete (COFFUniqueMapTy*)UniquingMap;
987}
988
Chris Lattner0c0cb712009-08-08 20:22:20 +0000989
Chris Lattner11e96572009-08-03 21:53:27 +0000990const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner48130352010-01-13 06:38:18 +0000991getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000992 // Create the map if it doesn't already exist.
993 if (UniquingMap == 0)
994 UniquingMap = new MachOUniqueMapTy();
995 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000996
Chris Lattner38cff382009-08-13 00:37:15 +0000997 // Do the lookup, if we have a hit, return it.
998 const MCSectionCOFF *&Entry = Map[Name];
999 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001000
Chris Lattner38cff382009-08-13 00:37:15 +00001001 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +00001002}
1003
Chris Lattnerf26e03b2009-07-31 17:42:42 +00001004void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1005 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001006 if (UniquingMap != 0)
1007 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001008 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001009 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1010 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001011 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001012 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001013 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001014 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001015
Chris Lattner35c35312009-08-18 16:56:17 +00001016 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1017 // though it contains relocatable pointers. In PIC mode, this is probably a
1018 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1019 // adjusted or this should be a data section.
1020 LSDASection =
1021 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1022 EHFrameSection =
1023 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001024
Chris Lattner18a4c162009-08-02 07:24:22 +00001025 // Debug info.
1026 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001027 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001028 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1029 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001030 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001031 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1032 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001033 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001034 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1035 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001036 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001037 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1038 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001039 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001040 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1041 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001042 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001043 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1044 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001045 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001046 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1047 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001048 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001049 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1050 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001051 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001052 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1053 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001054 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001055 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1056 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001057 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001058 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1059 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001060}
1061
Chris Lattner24f654c2009-08-06 16:39:58 +00001062const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001063getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001064 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001065 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001066}
1067
Chris Lattnerf0144122009-07-28 03:13:23 +00001068static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1069 if (Kind.isText())
1070 return ".text$linkonce";
1071 if (Kind.isWriteable())
1072 return ".data$linkonce";
1073 return ".rdata$linkonce";
1074}
1075
1076
Chris Lattnera87dea42009-07-31 18:48:30 +00001077const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001078SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001079 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001080 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001081
Chris Lattnerf0144122009-07-28 03:13:23 +00001082 // If this global is linkonce/weak and the target handles this by emitting it
1083 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001084 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001085 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner48130352010-01-13 06:38:18 +00001086 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattnercab16cc2010-01-13 19:19:17 +00001087 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner48130352010-01-13 06:38:18 +00001088 return getCOFFSection(Name.str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001089 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001090
Chris Lattnerf9650c02009-08-01 21:46:23 +00001091 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001092 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001093
Chris Lattnerf0144122009-07-28 03:13:23 +00001094 return getDataSection();
1095}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001096