blob: c1aab9921fb22a5b468cb33bfe4715da46c6a0a8 [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 Lattnera87dea42009-07-31 18:48:30 +000027#include "llvm/Support/Mangler.h"
Chris Lattner5dc47ff2009-08-12 23:55:02 +000028#include "llvm/ADT/SmallString.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000029#include "llvm/ADT/StringExtras.h"
30using namespace llvm;
31
32//===----------------------------------------------------------------------===//
33// Generic Code
34//===----------------------------------------------------------------------===//
35
Chris Lattnera87dea42009-07-31 18:48:30 +000036TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000037 TextSection = 0;
38 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000039 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000040 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000041 StaticCtorSection = 0;
42 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000043 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000044 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000045
46 DwarfAbbrevSection = 0;
47 DwarfInfoSection = 0;
48 DwarfLineSection = 0;
49 DwarfFrameSection = 0;
50 DwarfPubNamesSection = 0;
51 DwarfPubTypesSection = 0;
52 DwarfDebugInlineSection = 0;
53 DwarfStrSection = 0;
54 DwarfLocSection = 0;
55 DwarfARangesSection = 0;
56 DwarfRangesSection = 0;
57 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000058}
59
60TargetLoweringObjectFile::~TargetLoweringObjectFile() {
61}
62
63static bool isSuitableForBSS(const GlobalVariable *GV) {
64 Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000065
Chris Lattnerf0144122009-07-28 03:13:23 +000066 // Must have zero initializer.
67 if (!C->isNullValue())
68 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000069
Chris Lattnerf0144122009-07-28 03:13:23 +000070 // Leave constant zeros in readonly constant sections, so they can be shared.
71 if (GV->isConstant())
72 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000073
Chris Lattnerf0144122009-07-28 03:13:23 +000074 // If the global has an explicit section specified, don't put it in BSS.
75 if (!GV->getSection().empty())
76 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000077
Chris Lattnerf0144122009-07-28 03:13:23 +000078 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
79 if (NoZerosInBSS)
80 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000081
Chris Lattnerf0144122009-07-28 03:13:23 +000082 // Otherwise, put it in BSS!
83 return true;
84}
85
Chris Lattner1850e5a2009-08-04 16:13:09 +000086/// IsNullTerminatedString - Return true if the specified constant (which is
87/// known to have a type that is an array of 1/2/4 byte elements) ends with a
88/// nul value and contains no other nuls in it.
89static bool IsNullTerminatedString(const Constant *C) {
90 const ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000091
Chris Lattnerf0144122009-07-28 03:13:23 +000092 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000093 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
94 if (ATy->getNumElements() == 0) return false;
95
96 ConstantInt *Null =
97 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
98 if (Null == 0 || Null->getZExtValue() != 0)
99 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000100
Chris Lattner1850e5a2009-08-04 16:13:09 +0000101 // Verify that the null doesn't occur anywhere else in the string.
102 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
103 // Reject constantexpr elements etc.
104 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
105 CVA->getOperand(i) == Null)
106 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000107 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000108 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000109
110 // Another possibility: [1 x i8] zeroinitializer
111 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000112 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000113
114 return false;
115}
116
Chris Lattner58bed8f2009-08-05 04:25:40 +0000117/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000118/// a global variable. Given an global variable and information from TM, it
119/// classifies the global in a variety of ways that make various target
120/// implementations simpler. The target implementation is free to ignore this
121/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000122SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
123 const TargetMachine &TM){
124 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
125 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000126
Chris Lattnerf0144122009-07-28 03:13:23 +0000127 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000128
Chris Lattnerf0144122009-07-28 03:13:23 +0000129 // Early exit - functions should be always in text sections.
130 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
131 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000132 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000133
Chris Lattnerf0144122009-07-28 03:13:23 +0000134 // Handle thread-local data first.
135 if (GVar->isThreadLocal()) {
136 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000137 return SectionKind::getThreadBSS();
138 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000139 }
140
141 // Variable can be easily put to BSS section.
142 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000143 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000144
145 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000146
Chris Lattnerf0144122009-07-28 03:13:23 +0000147 // If the global is marked constant, we can put it into a mergable section,
148 // a mergable string section, or general .data if it contains relocations.
149 if (GVar->isConstant()) {
150 // If the initializer for the global contains something that requires a
151 // relocation, then we may have to drop this into a wriable data section
152 // even though it is marked const.
153 switch (C->getRelocationInfo()) {
154 default: llvm_unreachable("unknown relocation info kind");
155 case Constant::NoRelocation:
156 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000157 // section of the right width.
158 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000159 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000160 dyn_cast<IntegerType>(ATy->getElementType())) {
161 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
162 ITy->getBitWidth() == 32) &&
163 IsNullTerminatedString(C)) {
164 if (ITy->getBitWidth() == 8)
165 return SectionKind::getMergeable1ByteCString();
166 if (ITy->getBitWidth() == 16)
167 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000168
Chris Lattner1850e5a2009-08-04 16:13:09 +0000169 assert(ITy->getBitWidth() == 32 && "Unknown width");
170 return SectionKind::getMergeable4ByteCString();
171 }
172 }
173 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000174
Chris Lattnerf0144122009-07-28 03:13:23 +0000175 // Otherwise, just drop it into a mergable constant section. If we have
176 // a section for this size, use it, otherwise use the arbitrary sized
177 // mergable section.
178 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000179 case 4: return SectionKind::getMergeableConst4();
180 case 8: return SectionKind::getMergeableConst8();
181 case 16: return SectionKind::getMergeableConst16();
182 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000183 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000184
Chris Lattnerf0144122009-07-28 03:13:23 +0000185 case Constant::LocalRelocation:
186 // In static relocation model, the linker will resolve all addresses, so
187 // the relocation entries will actually be constants by the time the app
188 // starts up. However, we can't put this into a mergable section, because
189 // the linker doesn't take relocations into consideration when it tries to
190 // merge entries in the section.
191 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000192 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000193
Chris Lattnerf0144122009-07-28 03:13:23 +0000194 // Otherwise, the dynamic linker needs to fix it up, put it in the
195 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000196 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000197
Chris Lattnerf0144122009-07-28 03:13:23 +0000198 case Constant::GlobalRelocations:
199 // In static relocation model, the linker will resolve all addresses, so
200 // the relocation entries will actually be constants by the time the app
201 // starts up. However, we can't put this into a mergable section, because
202 // the linker doesn't take relocations into consideration when it tries to
203 // merge entries in the section.
204 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000205 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000206
Chris Lattnerf0144122009-07-28 03:13:23 +0000207 // Otherwise, the dynamic linker needs to fix it up, put it in the
208 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000209 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000210 }
211 }
212
213 // Okay, this isn't a constant. If the initializer for the global is going
214 // to require a runtime relocation by the dynamic linker, put it into a more
215 // specific section to improve startup time of the app. This coalesces these
216 // globals together onto fewer pages, improving the locality of the dynamic
217 // linker.
218 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000219 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000220
221 switch (C->getRelocationInfo()) {
222 default: llvm_unreachable("unknown relocation info kind");
223 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000224 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000225 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000226 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000227 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000228 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000229 }
230}
231
232/// SectionForGlobal - This method computes the appropriate section to emit
233/// the specified global variable or function definition. This should not
234/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000235const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000236SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000237 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000238 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000239 if (GV->hasSection())
240 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000241
242
Chris Lattnerf0144122009-07-28 03:13:23 +0000243 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000244 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000245}
246
Chris Lattner58bed8f2009-08-05 04:25:40 +0000247
Chris Lattnerf0144122009-07-28 03:13:23 +0000248// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000249const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000250TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000251 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000252 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000253 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000254 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000255
Chris Lattnerf9650c02009-08-01 21:46:23 +0000256 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000257 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000258
Chris Lattner82458382009-08-01 21:56:13 +0000259 if (Kind.isBSS() && BSSSection != 0)
260 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000261
Chris Lattnerf9650c02009-08-01 21:46:23 +0000262 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000263 return ReadOnlySection;
264
265 return getDataSection();
266}
267
Chris Lattner83d77fa2009-08-01 23:46:12 +0000268/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000269/// specified size and relocation information, return a section that it
270/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000271const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000272TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000273 if (Kind.isReadOnly() && ReadOnlySection != 0)
274 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000275
Chris Lattnerf0144122009-07-28 03:13:23 +0000276 return DataSection;
277}
278
Chris Lattner8c6ed052009-09-16 01:46:41 +0000279/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
280/// pc-relative reference to the specified global variable from exception
281/// handling information. In addition to the symbol, this returns
282/// by-reference:
283///
284/// IsIndirect - True if the returned symbol is actually a stub that contains
285/// the address of the symbol, false if the symbol is the global itself.
286///
287/// IsPCRel - True if the symbol reference is already pc-relative, false if
288/// the caller needs to subtract off the address of the reference from the
289/// symbol.
290///
291const MCExpr *TargetLoweringObjectFile::
292getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000293 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000294 bool &IsIndirect, bool &IsPCRel) const {
295 // The generic implementation of this just returns a direct reference to the
296 // symbol.
297 IsIndirect = false;
298 IsPCRel = false;
299
300 SmallString<128> Name;
301 Mang->getNameWithPrefix(Name, GV, false);
302 return MCSymbolRefExpr::Create(Name.str(), getContext());
303}
Chris Lattnerf0144122009-07-28 03:13:23 +0000304
Chris Lattnerf0144122009-07-28 03:13:23 +0000305
306//===----------------------------------------------------------------------===//
307// ELF
308//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000309typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
310
311TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
312 // If we have the section uniquing map, free it.
313 delete (ELFUniqueMapTy*)UniquingMap;
314}
Chris Lattnerf0144122009-07-28 03:13:23 +0000315
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000316const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000317getELFSection(StringRef Section, unsigned Type, unsigned Flags,
318 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000319 if (UniquingMap == 0)
320 UniquingMap = new ELFUniqueMapTy();
321 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000322
Chris Lattner38cff382009-08-13 00:37:15 +0000323 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000324 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000325 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000326
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000327 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
328 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000329}
330
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000331void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
332 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000333 if (UniquingMap != 0)
334 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000335 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000336
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000337 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000338 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
339 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
340 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000341
342 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000343 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
344 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
345 SectionKind::getText());
346
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000347 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000348 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
349 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
350 SectionKind::getDataRel());
351
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000352 ReadOnlySection =
353 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
354 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000355 SectionKind::getReadOnly());
356
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000357 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000358 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000359 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000360 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000361
362 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000363 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
364 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
365 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000366
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000367 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000368 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
369 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
370 SectionKind::getDataRel());
371
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000372 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000373 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
374 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
375 SectionKind::getDataRelLocal());
376
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000377 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000378 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
379 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
380 SectionKind::getReadOnlyWithRel());
381
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000382 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000383 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
384 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
385 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000386
387 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000388 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
389 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
390 SectionKind::getMergeableConst4());
391
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000392 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000393 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
394 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
395 SectionKind::getMergeableConst8());
396
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000397 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000398 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
399 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
400 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000401
402 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000403 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000404 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
405 SectionKind::getDataRel());
406
Chris Lattner80ec2792009-08-02 00:34:36 +0000407 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000408 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
409 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
410 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000411
Chris Lattner18a4c162009-08-02 07:24:22 +0000412 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000413
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000414 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
415 // it contains relocatable pointers. In PIC mode, this is probably a big
416 // runtime hit for C++ apps. Either the contents of the LSDA need to be
417 // adjusted or this should be a data section.
418 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000419 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
420 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000421 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000422 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000423 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
424 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000425
Chris Lattner18a4c162009-08-02 07:24:22 +0000426 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000427 DwarfAbbrevSection =
428 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000429 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000430 DwarfInfoSection =
431 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000432 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000433 DwarfLineSection =
434 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000435 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000436 DwarfFrameSection =
437 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000438 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000439 DwarfPubNamesSection =
440 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000441 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000442 DwarfPubTypesSection =
443 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000444 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000445 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000446 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
447 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000448 DwarfLocSection =
449 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000450 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000451 DwarfARangesSection =
452 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000453 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000454 DwarfRangesSection =
455 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000456 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000457 DwarfMacroInfoSection =
458 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000459 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000460}
461
462
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000463static SectionKind
Chris Lattner24f654c2009-08-06 16:39:58 +0000464getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000465 if (Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000466
Chris Lattnerf0144122009-07-28 03:13:23 +0000467 // Some lame default implementation based on some magic section names.
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000468 if (strcmp(Name, ".bss") == 0 ||
469 strncmp(Name, ".bss.", 5) == 0 ||
470 strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000471 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000472 strcmp(Name, ".sbss") == 0 ||
473 strncmp(Name, ".sbss.", 6) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000474 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
475 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000476 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000477
Chris Lattnerf0144122009-07-28 03:13:23 +0000478 if (strcmp(Name, ".tdata") == 0 ||
479 strncmp(Name, ".tdata.", 7) == 0 ||
480 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
481 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000482 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000483
Chris Lattnerf0144122009-07-28 03:13:23 +0000484 if (strcmp(Name, ".tbss") == 0 ||
485 strncmp(Name, ".tbss.", 6) == 0 ||
486 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
487 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000488 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000489
Chris Lattnerf0144122009-07-28 03:13:23 +0000490 return K;
491}
492
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000493
494static unsigned
495getELFSectionType(const char *Name, SectionKind K) {
496
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000497 if (strcmp(Name, ".init_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000498 return MCSectionELF::SHT_INIT_ARRAY;
499
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000500 if (strcmp(Name, ".fini_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000501 return MCSectionELF::SHT_FINI_ARRAY;
502
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000503 if (strcmp(Name, ".preinit_array") == 0)
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 Lattnerb8f396b2009-07-29 05:20:33 +0000579 std::string Name = Mang->makeNameProper(GV->getNameStr());
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000580
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000581 return getELFSection((Prefix+Name).c_str(),
582 getELFSectionType((Prefix+Name).c_str(), Kind),
583 getELFSectionFlags(Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000584 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000585 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000586
Chris Lattnerf9650c02009-08-01 21:46:23 +0000587 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000588
Chris Lattner3b24c012009-08-04 05:35:56 +0000589 if (Kind.isMergeable1ByteCString() ||
590 Kind.isMergeable2ByteCString() ||
591 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000592
Chris Lattner067fe1a2009-07-29 04:54:38 +0000593 // We also need alignment here.
594 // FIXME: this is getting the alignment of the character, not the
595 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000596 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000597 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000598
Chris Lattner7e88a502009-08-04 16:19:50 +0000599 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000600 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000601 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000602 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000603 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000604 else
605 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000606
607
Chris Lattner7e88a502009-08-04 16:19:50 +0000608 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000609 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
610 MCSectionELF::SHF_ALLOC |
611 MCSectionELF::SHF_MERGE |
612 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000613 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000614 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000615
Chris Lattnerf9650c02009-08-01 21:46:23 +0000616 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000617 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000618 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000619 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000620 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000621 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000622 return MergeableConst16Section;
623 return ReadOnlySection; // .const
624 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000625
Chris Lattnerf9650c02009-08-01 21:46:23 +0000626 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000627
Chris Lattnerf9650c02009-08-01 21:46:23 +0000628 if (Kind.isThreadData()) return TLSDataSection;
629 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000630
Chris Lattner82458382009-08-01 21:56:13 +0000631 if (Kind.isBSS()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000632
Chris Lattnerf9650c02009-08-01 21:46:23 +0000633 if (Kind.isDataNoRel()) return DataSection;
634 if (Kind.isDataRelLocal()) return DataRelLocalSection;
635 if (Kind.isDataRel()) return DataRelSection;
636 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000637
Chris Lattnerf9650c02009-08-01 21:46:23 +0000638 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000639 return DataRelROSection;
640}
641
Chris Lattner83d77fa2009-08-01 23:46:12 +0000642/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000643/// specified size and relocation information, return a section that it
644/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000645const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000646getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000647 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000648 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000649 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000650 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000651 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000652 return MergeableConst16Section;
653 if (Kind.isReadOnly())
654 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000655
Chris Lattnerf0144122009-07-28 03:13:23 +0000656 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
657 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
658 return DataRelROSection;
659}
660
661//===----------------------------------------------------------------------===//
662// MachO
663//===----------------------------------------------------------------------===//
664
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000665typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000666
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000667TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
668 // If we have the MachO uniquing map, free it.
669 delete (MachOUniqueMapTy*)UniquingMap;
670}
671
672
673const MCSectionMachO *TargetLoweringObjectFileMachO::
Chris Lattnerd3c4486f2009-08-12 23:34:27 +0000674getMachOSection(const StringRef &Segment, const StringRef &Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000675 unsigned TypeAndAttributes,
676 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000677 // We unique sections by their segment/section pair. The returned section
678 // may not have the same flags as the requested section, if so this should be
679 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000680
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000681 // Create the map if it doesn't already exist.
682 if (UniquingMap == 0)
683 UniquingMap = new MachOUniqueMapTy();
684 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000685
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000686 // Form the name to look up.
687 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000688 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000689 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000690 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000691
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000692 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000693 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000694 if (Entry) return Entry;
695
696 // Otherwise, return a new section.
697 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
698 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000699}
700
Chris Lattner11e96572009-08-03 21:53:27 +0000701
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000702void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
703 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000704 if (UniquingMap != 0)
705 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000706 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000707
Chris Lattnerff4bc462009-08-10 01:39:42 +0000708 TextSection // .text
709 = getMachOSection("__TEXT", "__text",
710 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
711 SectionKind::getText());
712 DataSection // .data
713 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000714
Chris Lattnerff4bc462009-08-10 01:39:42 +0000715 CStringSection // .cstring
716 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
717 SectionKind::getMergeable1ByteCString());
718 UStringSection
719 = getMachOSection("__TEXT","__ustring", 0,
720 SectionKind::getMergeable2ByteCString());
721 FourByteConstantSection // .literal4
722 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
723 SectionKind::getMergeableConst4());
724 EightByteConstantSection // .literal8
725 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
726 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000727
Chris Lattner4bb253c2009-07-28 17:50:28 +0000728 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
729 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000730 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000731 if (TM.getRelocationModel() != Reloc::Static &&
732 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000733 SixteenByteConstantSection = // .literal16
734 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000735 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000736
Chris Lattnerff4bc462009-08-10 01:39:42 +0000737 ReadOnlySection // .const
738 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000739
Chris Lattnerff4bc462009-08-10 01:39:42 +0000740 TextCoalSection
741 = getMachOSection("__TEXT", "__textcoal_nt",
742 MCSectionMachO::S_COALESCED |
743 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
744 SectionKind::getText());
745 ConstTextCoalSection
746 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
747 SectionKind::getText());
748 ConstDataCoalSection
749 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
750 SectionKind::getText());
751 ConstDataSection // .const_data
752 = getMachOSection("__DATA", "__const", 0,
753 SectionKind::getReadOnlyWithRel());
754 DataCoalSection
755 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
756 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000757
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000758
Chris Lattnere309cfa2009-08-13 00:05:07 +0000759 LazySymbolPointerSection
760 = getMachOSection("__DATA", "__la_symbol_ptr",
761 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
762 SectionKind::getMetadata());
763 NonLazySymbolPointerSection
764 = getMachOSection("__DATA", "__nl_symbol_ptr",
765 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
766 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000767
Chris Lattner80ec2792009-08-02 00:34:36 +0000768 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000769 StaticCtorSection
770 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
771 StaticDtorSection
772 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000773 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000774 StaticCtorSection
775 = getMachOSection("__DATA", "__mod_init_func",
776 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
777 SectionKind::getDataRel());
778 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000779 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000780 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
781 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000782 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000783
Chris Lattner18a4c162009-08-02 07:24:22 +0000784 // Exception Handling.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000785 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000786 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000787 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000788 getMachOSection("__TEXT", "__eh_frame",
789 MCSectionMachO::S_COALESCED |
790 MCSectionMachO::S_ATTR_NO_TOC |
791 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
792 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
793 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000794
795 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000796 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000797 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000798 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000799 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000800 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000801 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000802 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000803 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000804 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000805 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000806 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000807 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000808 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000809 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000810 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000811 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000812 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000813 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000814 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000815 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000816 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000817 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000818 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000819 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000820 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000821 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000822 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000823 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000824 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000825 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000826 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000827 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000828 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000829 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000830 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000831 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000832}
833
Chris Lattnera87dea42009-07-31 18:48:30 +0000834const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000835getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000836 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000837 // Parse the section specifier and create it if valid.
838 StringRef Segment, Section;
839 unsigned TAA, StubSize;
840 std::string ErrorCode =
841 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
842 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000843 if (!ErrorCode.empty()) {
844 // If invalid, report the error with llvm_report_error.
845 llvm_report_error("Global variable '" + GV->getNameStr() +
846 "' has an invalid section specifier '" + GV->getSection()+
847 "': " + ErrorCode + ".");
848 // Fall back to dropping it into the data section.
849 return DataSection;
850 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000851
Chris Lattnere309cfa2009-08-13 00:05:07 +0000852 // Get the section.
853 const MCSectionMachO *S =
854 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000855
Chris Lattnere309cfa2009-08-13 00:05:07 +0000856 // Okay, now that we got the section, verify that the TAA & StubSize agree.
857 // If the user declared multiple globals with different section flags, we need
858 // to reject it here.
859 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
860 // If invalid, report the error with llvm_report_error.
861 llvm_report_error("Global variable '" + GV->getNameStr() +
862 "' section type or attributes does not match previous"
863 " section specifier");
864 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000865
Chris Lattnere309cfa2009-08-13 00:05:07 +0000866 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000867}
868
869const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000870SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000871 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000872 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000873
Chris Lattnerf9650c02009-08-01 21:46:23 +0000874 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000875 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000876
Chris Lattnerf0144122009-07-28 03:13:23 +0000877 // If this is weak/linkonce, put this in a coalescable section, either in text
878 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000879 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000880 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000881 return ConstTextCoalSection;
882 return DataCoalSection;
883 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000884
Chris Lattnerf0144122009-07-28 03:13:23 +0000885 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000886 if (Kind.isMergeable1ByteCString() ||
887 Kind.isMergeable2ByteCString()) {
888 if (TM.getTargetData()->getPreferredAlignment(
889 cast<GlobalVariable>(GV)) < 32) {
890 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000891 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000892 assert(Kind.isMergeable2ByteCString());
893 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000894 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000895 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000896
Chris Lattnerf9650c02009-08-01 21:46:23 +0000897 if (Kind.isMergeableConst()) {
898 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000899 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000900 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000901 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000902 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000903 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000904 }
Chris Lattnerec409752009-08-04 16:27:13 +0000905
906 // Otherwise, if it is readonly, but not something we can specially optimize,
907 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000908 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000909 return ReadOnlySection;
910
911 // If this is marked const, put it into a const section. But if the dynamic
912 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000913 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000914 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000915
Chris Lattnerf0144122009-07-28 03:13:23 +0000916 // Otherwise, just drop the variable in the normal data section.
917 return DataSection;
918}
919
Chris Lattnera87dea42009-07-31 18:48:30 +0000920const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000921TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000922 // If this constant requires a relocation, we have to put it in the data
923 // segment, not in the text segment.
924 if (Kind.isDataRel())
925 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000926
Chris Lattnerf0144122009-07-28 03:13:23 +0000927 if (Kind.isMergeableConst4())
928 return FourByteConstantSection;
929 if (Kind.isMergeableConst8())
930 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000931 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000932 return SixteenByteConstantSection;
933 return ReadOnlySection; // .const
934}
935
Chris Lattner26630c12009-07-31 20:52:39 +0000936/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
937/// not to emit the UsedDirective for some symbols in llvm.used.
938// FIXME: REMOVE this (rdar://7071300)
939bool TargetLoweringObjectFileMachO::
940shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
941 /// On Darwin, internally linked data beginning with "L" or "l" does not have
942 /// the directive emitted (this occurs in ObjC metadata).
943 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000944
Chris Lattner26630c12009-07-31 20:52:39 +0000945 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
946 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
947 // FIXME: ObjC metadata is currently emitted as internal symbols that have
948 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
949 // this horrible hack can go away.
950 const std::string &Name = Mang->getMangledName(GV);
951 if (Name[0] == 'L' || Name[0] == 'l')
952 return false;
953 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000954
Chris Lattner26630c12009-07-31 20:52:39 +0000955 return true;
956}
957
Chris Lattner8c6ed052009-09-16 01:46:41 +0000958const MCExpr *TargetLoweringObjectFileMachO::
959getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Chris Lattner8609c7c2009-09-17 18:49:52 +0000960 MachineModuleInfo *MMI,
Chris Lattner8c6ed052009-09-16 01:46:41 +0000961 bool &IsIndirect, bool &IsPCRel) const {
962 // The mach-o version of this method defaults to returning a stub reference.
963 IsIndirect = true;
964 IsPCRel = false;
965
966 SmallString<128> Name;
967 Mang->getNameWithPrefix(Name, GV, true);
968 Name += "$non_lazy_ptr";
969 return MCSymbolRefExpr::Create(Name.str(), getContext());
970}
971
Chris Lattner26630c12009-07-31 20:52:39 +0000972
Chris Lattnerf0144122009-07-28 03:13:23 +0000973//===----------------------------------------------------------------------===//
974// COFF
975//===----------------------------------------------------------------------===//
976
Chris Lattner38cff382009-08-13 00:37:15 +0000977typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
978
979TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
980 delete (COFFUniqueMapTy*)UniquingMap;
981}
982
Chris Lattner0c0cb712009-08-08 20:22:20 +0000983
Chris Lattner11e96572009-08-03 21:53:27 +0000984const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000985getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000986 // Create the map if it doesn't already exist.
987 if (UniquingMap == 0)
988 UniquingMap = new MachOUniqueMapTy();
989 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000990
Chris Lattner38cff382009-08-13 00:37:15 +0000991 // Do the lookup, if we have a hit, return it.
992 const MCSectionCOFF *&Entry = Map[Name];
993 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000994
Chris Lattner38cff382009-08-13 00:37:15 +0000995 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000996}
997
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000998void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
999 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001000 if (UniquingMap != 0)
1001 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001002 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001003 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1004 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001005 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001006 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001007 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001008 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001009
Chris Lattner35c35312009-08-18 16:56:17 +00001010 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1011 // though it contains relocatable pointers. In PIC mode, this is probably a
1012 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1013 // adjusted or this should be a data section.
1014 LSDASection =
1015 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1016 EHFrameSection =
1017 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001018
Chris Lattner18a4c162009-08-02 07:24:22 +00001019 // Debug info.
1020 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001021 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001022 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1023 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001024 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001025 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1026 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001027 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001028 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1029 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001030 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001031 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1032 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001033 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001034 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1035 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001036 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001037 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1038 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001039 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001040 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1041 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001042 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001043 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1044 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001045 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001046 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1047 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001048 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001049 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1050 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001051 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001052 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1053 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001054}
1055
Chris Lattner24f654c2009-08-06 16:39:58 +00001056const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001057getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001058 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001059 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001060}
1061
Chris Lattnerf0144122009-07-28 03:13:23 +00001062static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1063 if (Kind.isText())
1064 return ".text$linkonce";
1065 if (Kind.isWriteable())
1066 return ".data$linkonce";
1067 return ".rdata$linkonce";
1068}
1069
1070
Chris Lattnera87dea42009-07-31 18:48:30 +00001071const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001072SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001073 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001074 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001075
Chris Lattnerf0144122009-07-28 03:13:23 +00001076 // If this global is linkonce/weak and the target handles this by emitting it
1077 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001078 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001079 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +00001080 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +00001081 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001082 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001083
Chris Lattnerf9650c02009-08-01 21:46:23 +00001084 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001085 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001086
Chris Lattnerf0144122009-07-28 03:13:23 +00001087 return getDataSection();
1088}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001089