blob: fd8fd9006e3d420d60b92a94888b0d318ba13815 [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,
293 bool &IsIndirect, bool &IsPCRel) const {
294 // The generic implementation of this just returns a direct reference to the
295 // symbol.
296 IsIndirect = false;
297 IsPCRel = false;
298
299 SmallString<128> Name;
300 Mang->getNameWithPrefix(Name, GV, false);
301 return MCSymbolRefExpr::Create(Name.str(), getContext());
302}
Chris Lattnerf0144122009-07-28 03:13:23 +0000303
Chris Lattnerf0144122009-07-28 03:13:23 +0000304
305//===----------------------------------------------------------------------===//
306// ELF
307//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000308typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
309
310TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
311 // If we have the section uniquing map, free it.
312 delete (ELFUniqueMapTy*)UniquingMap;
313}
Chris Lattnerf0144122009-07-28 03:13:23 +0000314
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000315const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000316getELFSection(StringRef Section, unsigned Type, unsigned Flags,
317 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000318 if (UniquingMap == 0)
319 UniquingMap = new ELFUniqueMapTy();
320 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000321
Chris Lattner38cff382009-08-13 00:37:15 +0000322 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000323 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000324 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000325
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000326 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
327 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000328}
329
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000330void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
331 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000332 if (UniquingMap != 0)
333 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000334 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000335
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000336 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000337 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
338 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
339 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000340
341 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000342 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
343 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
344 SectionKind::getText());
345
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000346 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000347 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
348 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
349 SectionKind::getDataRel());
350
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000351 ReadOnlySection =
352 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
353 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000354 SectionKind::getReadOnly());
355
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000356 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000357 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000358 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000359 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000360
361 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000362 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
363 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
364 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000365
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000366 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000367 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
368 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
369 SectionKind::getDataRel());
370
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000371 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000372 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
373 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
374 SectionKind::getDataRelLocal());
375
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000376 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000377 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
378 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
379 SectionKind::getReadOnlyWithRel());
380
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000381 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000382 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
383 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
384 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000385
386 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000387 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
388 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
389 SectionKind::getMergeableConst4());
390
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000391 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000392 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
393 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
394 SectionKind::getMergeableConst8());
395
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000396 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000397 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
398 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
399 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000400
401 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000402 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000403 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
404 SectionKind::getDataRel());
405
Chris Lattner80ec2792009-08-02 00:34:36 +0000406 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000407 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
408 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
409 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000410
Chris Lattner18a4c162009-08-02 07:24:22 +0000411 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000412
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000413 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
414 // it contains relocatable pointers. In PIC mode, this is probably a big
415 // runtime hit for C++ apps. Either the contents of the LSDA need to be
416 // adjusted or this should be a data section.
417 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000418 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
419 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000420 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000421 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000422 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
423 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000424
Chris Lattner18a4c162009-08-02 07:24:22 +0000425 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000426 DwarfAbbrevSection =
427 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000428 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000429 DwarfInfoSection =
430 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000431 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000432 DwarfLineSection =
433 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000434 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000435 DwarfFrameSection =
436 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000437 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000438 DwarfPubNamesSection =
439 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000440 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000441 DwarfPubTypesSection =
442 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000443 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000444 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000445 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
446 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000447 DwarfLocSection =
448 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000449 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000450 DwarfARangesSection =
451 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000452 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000453 DwarfRangesSection =
454 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000455 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000456 DwarfMacroInfoSection =
457 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000458 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000459}
460
461
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000462static SectionKind
Chris Lattner24f654c2009-08-06 16:39:58 +0000463getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000464 if (Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000465
Chris Lattnerf0144122009-07-28 03:13:23 +0000466 // Some lame default implementation based on some magic section names.
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000467 if (strcmp(Name, ".bss") == 0 ||
468 strncmp(Name, ".bss.", 5) == 0 ||
469 strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000470 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000471 strcmp(Name, ".sbss") == 0 ||
472 strncmp(Name, ".sbss.", 6) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000473 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
474 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000475 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000476
Chris Lattnerf0144122009-07-28 03:13:23 +0000477 if (strcmp(Name, ".tdata") == 0 ||
478 strncmp(Name, ".tdata.", 7) == 0 ||
479 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
480 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000481 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000482
Chris Lattnerf0144122009-07-28 03:13:23 +0000483 if (strcmp(Name, ".tbss") == 0 ||
484 strncmp(Name, ".tbss.", 6) == 0 ||
485 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
486 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000487 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000488
Chris Lattnerf0144122009-07-28 03:13:23 +0000489 return K;
490}
491
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000492
493static unsigned
494getELFSectionType(const char *Name, SectionKind K) {
495
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000496 if (strcmp(Name, ".init_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000497 return MCSectionELF::SHT_INIT_ARRAY;
498
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000499 if (strcmp(Name, ".fini_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000500 return MCSectionELF::SHT_FINI_ARRAY;
501
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000502 if (strcmp(Name, ".preinit_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000503 return MCSectionELF::SHT_PREINIT_ARRAY;
504
505 if (K.isBSS() || K.isThreadBSS())
506 return MCSectionELF::SHT_NOBITS;
507
508 return MCSectionELF::SHT_PROGBITS;
509}
510
511
512static unsigned
513getELFSectionFlags(SectionKind K) {
514 unsigned Flags = 0;
515
516 if (!K.isMetadata())
517 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000518
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000519 if (K.isText())
520 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000521
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000522 if (K.isWriteable())
523 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000524
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000525 if (K.isThreadLocal())
526 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000527
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000528 // K.isMergeableConst() is left out to honour PR4650
529 if (K.isMergeableCString() || K.isMergeableConst4() ||
530 K.isMergeableConst8() || K.isMergeableConst16())
531 Flags |= MCSectionELF::SHF_MERGE;
532
533 if (K.isMergeableCString())
534 Flags |= MCSectionELF::SHF_STRINGS;
535
536 return Flags;
537}
538
539
Chris Lattner24f654c2009-08-06 16:39:58 +0000540const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000541getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000542 Mangler *Mang, const TargetMachine &TM) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000543 const char *SectionName = GV->getSection().c_str();
544
Chris Lattner24f654c2009-08-06 16:39:58 +0000545 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000546 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000547
548 return getELFSection(SectionName,
549 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000550 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000551}
Chris Lattnerf0144122009-07-28 03:13:23 +0000552
553static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
554 if (Kind.isText()) return ".gnu.linkonce.t.";
555 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000556
Chris Lattnerf0144122009-07-28 03:13:23 +0000557 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
558 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000559
Chris Lattnerf0144122009-07-28 03:13:23 +0000560 if (Kind.isBSS()) return ".gnu.linkonce.b.";
561 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
562 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
563 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
564 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000565
Chris Lattnerf0144122009-07-28 03:13:23 +0000566 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
567 return ".gnu.linkonce.d.rel.ro.";
568}
569
Chris Lattnera87dea42009-07-31 18:48:30 +0000570const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000571SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000572 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000573
Chris Lattnerf0144122009-07-28 03:13:23 +0000574 // If this global is linkonce/weak and the target handles this by emitting it
575 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000576 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000577 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000578 std::string Name = Mang->makeNameProper(GV->getNameStr());
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000579
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000580 return getELFSection((Prefix+Name).c_str(),
581 getELFSectionType((Prefix+Name).c_str(), Kind),
582 getELFSectionFlags(Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000583 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000584 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000585
Chris Lattnerf9650c02009-08-01 21:46:23 +0000586 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000587
Chris Lattner3b24c012009-08-04 05:35:56 +0000588 if (Kind.isMergeable1ByteCString() ||
589 Kind.isMergeable2ByteCString() ||
590 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000591
Chris Lattner067fe1a2009-07-29 04:54:38 +0000592 // We also need alignment here.
593 // FIXME: this is getting the alignment of the character, not the
594 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000595 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000596 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000597
Chris Lattner7e88a502009-08-04 16:19:50 +0000598 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000599 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000600 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000601 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000602 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000603 else
604 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000605
606
Chris Lattner7e88a502009-08-04 16:19:50 +0000607 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000608 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
609 MCSectionELF::SHF_ALLOC |
610 MCSectionELF::SHF_MERGE |
611 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000612 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000613 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000614
Chris Lattnerf9650c02009-08-01 21:46:23 +0000615 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000616 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000617 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000618 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000619 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000620 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000621 return MergeableConst16Section;
622 return ReadOnlySection; // .const
623 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000624
Chris Lattnerf9650c02009-08-01 21:46:23 +0000625 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000626
Chris Lattnerf9650c02009-08-01 21:46:23 +0000627 if (Kind.isThreadData()) return TLSDataSection;
628 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000629
Chris Lattner82458382009-08-01 21:56:13 +0000630 if (Kind.isBSS()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000631
Chris Lattnerf9650c02009-08-01 21:46:23 +0000632 if (Kind.isDataNoRel()) return DataSection;
633 if (Kind.isDataRelLocal()) return DataRelLocalSection;
634 if (Kind.isDataRel()) return DataRelSection;
635 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000636
Chris Lattnerf9650c02009-08-01 21:46:23 +0000637 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000638 return DataRelROSection;
639}
640
Chris Lattner83d77fa2009-08-01 23:46:12 +0000641/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000642/// specified size and relocation information, return a section that it
643/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000644const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000645getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000646 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000647 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000648 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000649 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000650 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000651 return MergeableConst16Section;
652 if (Kind.isReadOnly())
653 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000654
Chris Lattnerf0144122009-07-28 03:13:23 +0000655 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
656 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
657 return DataRelROSection;
658}
659
660//===----------------------------------------------------------------------===//
661// MachO
662//===----------------------------------------------------------------------===//
663
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000664typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000665
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000666TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
667 // If we have the MachO uniquing map, free it.
668 delete (MachOUniqueMapTy*)UniquingMap;
669}
670
671
672const MCSectionMachO *TargetLoweringObjectFileMachO::
Chris Lattnerd3c4486f2009-08-12 23:34:27 +0000673getMachOSection(const StringRef &Segment, const StringRef &Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000674 unsigned TypeAndAttributes,
675 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000676 // We unique sections by their segment/section pair. The returned section
677 // may not have the same flags as the requested section, if so this should be
678 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000679
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000680 // Create the map if it doesn't already exist.
681 if (UniquingMap == 0)
682 UniquingMap = new MachOUniqueMapTy();
683 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000684
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000685 // Form the name to look up.
686 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000687 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000688 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000689 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000690
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000691 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000692 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000693 if (Entry) return Entry;
694
695 // Otherwise, return a new section.
696 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
697 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000698}
699
Chris Lattner11e96572009-08-03 21:53:27 +0000700
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000701void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
702 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000703 if (UniquingMap != 0)
704 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000705 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000706
Chris Lattnerff4bc462009-08-10 01:39:42 +0000707 TextSection // .text
708 = getMachOSection("__TEXT", "__text",
709 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
710 SectionKind::getText());
711 DataSection // .data
712 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000713
Chris Lattnerff4bc462009-08-10 01:39:42 +0000714 CStringSection // .cstring
715 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
716 SectionKind::getMergeable1ByteCString());
717 UStringSection
718 = getMachOSection("__TEXT","__ustring", 0,
719 SectionKind::getMergeable2ByteCString());
720 FourByteConstantSection // .literal4
721 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
722 SectionKind::getMergeableConst4());
723 EightByteConstantSection // .literal8
724 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
725 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000726
Chris Lattner4bb253c2009-07-28 17:50:28 +0000727 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
728 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000729 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000730 if (TM.getRelocationModel() != Reloc::Static &&
731 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000732 SixteenByteConstantSection = // .literal16
733 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000734 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000735
Chris Lattnerff4bc462009-08-10 01:39:42 +0000736 ReadOnlySection // .const
737 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000738
Chris Lattnerff4bc462009-08-10 01:39:42 +0000739 TextCoalSection
740 = getMachOSection("__TEXT", "__textcoal_nt",
741 MCSectionMachO::S_COALESCED |
742 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
743 SectionKind::getText());
744 ConstTextCoalSection
745 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
746 SectionKind::getText());
747 ConstDataCoalSection
748 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
749 SectionKind::getText());
750 ConstDataSection // .const_data
751 = getMachOSection("__DATA", "__const", 0,
752 SectionKind::getReadOnlyWithRel());
753 DataCoalSection
754 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
755 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000756
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000757
Chris Lattnere309cfa2009-08-13 00:05:07 +0000758 LazySymbolPointerSection
759 = getMachOSection("__DATA", "__la_symbol_ptr",
760 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
761 SectionKind::getMetadata());
762 NonLazySymbolPointerSection
763 = getMachOSection("__DATA", "__nl_symbol_ptr",
764 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
765 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000766
Chris Lattner80ec2792009-08-02 00:34:36 +0000767 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000768 StaticCtorSection
769 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
770 StaticDtorSection
771 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000772 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000773 StaticCtorSection
774 = getMachOSection("__DATA", "__mod_init_func",
775 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
776 SectionKind::getDataRel());
777 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000778 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000779 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
780 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000781 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000782
Chris Lattner18a4c162009-08-02 07:24:22 +0000783 // Exception Handling.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000784 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000785 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000786 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000787 getMachOSection("__TEXT", "__eh_frame",
788 MCSectionMachO::S_COALESCED |
789 MCSectionMachO::S_ATTR_NO_TOC |
790 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
791 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
792 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000793
794 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000795 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000796 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000797 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000798 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000799 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000800 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000801 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000802 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000803 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000804 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000805 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000806 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000807 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000808 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000809 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000810 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000811 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000812 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000813 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000814 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000815 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000816 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000817 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000818 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000819 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000820 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000821 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000822 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000823 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000824 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000825 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000826 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000827 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000828 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000829 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000830 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000831}
832
Chris Lattnera87dea42009-07-31 18:48:30 +0000833const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000834getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000835 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000836 // Parse the section specifier and create it if valid.
837 StringRef Segment, Section;
838 unsigned TAA, StubSize;
839 std::string ErrorCode =
840 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
841 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000842 if (!ErrorCode.empty()) {
843 // If invalid, report the error with llvm_report_error.
844 llvm_report_error("Global variable '" + GV->getNameStr() +
845 "' has an invalid section specifier '" + GV->getSection()+
846 "': " + ErrorCode + ".");
847 // Fall back to dropping it into the data section.
848 return DataSection;
849 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000850
Chris Lattnere309cfa2009-08-13 00:05:07 +0000851 // Get the section.
852 const MCSectionMachO *S =
853 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000854
Chris Lattnere309cfa2009-08-13 00:05:07 +0000855 // Okay, now that we got the section, verify that the TAA & StubSize agree.
856 // If the user declared multiple globals with different section flags, we need
857 // to reject it here.
858 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
859 // If invalid, report the error with llvm_report_error.
860 llvm_report_error("Global variable '" + GV->getNameStr() +
861 "' section type or attributes does not match previous"
862 " section specifier");
863 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000864
Chris Lattnere309cfa2009-08-13 00:05:07 +0000865 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000866}
867
868const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000869SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000870 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000871 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000872
Chris Lattnerf9650c02009-08-01 21:46:23 +0000873 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000874 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000875
Chris Lattnerf0144122009-07-28 03:13:23 +0000876 // If this is weak/linkonce, put this in a coalescable section, either in text
877 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000878 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000879 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000880 return ConstTextCoalSection;
881 return DataCoalSection;
882 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000883
Chris Lattnerf0144122009-07-28 03:13:23 +0000884 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000885 if (Kind.isMergeable1ByteCString() ||
886 Kind.isMergeable2ByteCString()) {
887 if (TM.getTargetData()->getPreferredAlignment(
888 cast<GlobalVariable>(GV)) < 32) {
889 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000890 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000891 assert(Kind.isMergeable2ByteCString());
892 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000893 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000894 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000895
Chris Lattnerf9650c02009-08-01 21:46:23 +0000896 if (Kind.isMergeableConst()) {
897 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000898 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000899 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000900 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000901 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000902 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000903 }
Chris Lattnerec409752009-08-04 16:27:13 +0000904
905 // Otherwise, if it is readonly, but not something we can specially optimize,
906 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000907 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000908 return ReadOnlySection;
909
910 // If this is marked const, put it into a const section. But if the dynamic
911 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000912 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000913 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000914
Chris Lattnerf0144122009-07-28 03:13:23 +0000915 // Otherwise, just drop the variable in the normal data section.
916 return DataSection;
917}
918
Chris Lattnera87dea42009-07-31 18:48:30 +0000919const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000920TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000921 // If this constant requires a relocation, we have to put it in the data
922 // segment, not in the text segment.
923 if (Kind.isDataRel())
924 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000925
Chris Lattnerf0144122009-07-28 03:13:23 +0000926 if (Kind.isMergeableConst4())
927 return FourByteConstantSection;
928 if (Kind.isMergeableConst8())
929 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000930 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000931 return SixteenByteConstantSection;
932 return ReadOnlySection; // .const
933}
934
Chris Lattner26630c12009-07-31 20:52:39 +0000935/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
936/// not to emit the UsedDirective for some symbols in llvm.used.
937// FIXME: REMOVE this (rdar://7071300)
938bool TargetLoweringObjectFileMachO::
939shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
940 /// On Darwin, internally linked data beginning with "L" or "l" does not have
941 /// the directive emitted (this occurs in ObjC metadata).
942 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000943
Chris Lattner26630c12009-07-31 20:52:39 +0000944 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
945 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
946 // FIXME: ObjC metadata is currently emitted as internal symbols that have
947 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
948 // this horrible hack can go away.
949 const std::string &Name = Mang->getMangledName(GV);
950 if (Name[0] == 'L' || Name[0] == 'l')
951 return false;
952 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000953
Chris Lattner26630c12009-07-31 20:52:39 +0000954 return true;
955}
956
Chris Lattner8c6ed052009-09-16 01:46:41 +0000957const MCExpr *TargetLoweringObjectFileMachO::
958getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
959 bool &IsIndirect, bool &IsPCRel) const {
960 // The mach-o version of this method defaults to returning a stub reference.
961 IsIndirect = true;
962 IsPCRel = false;
963
964 SmallString<128> Name;
965 Mang->getNameWithPrefix(Name, GV, true);
966 Name += "$non_lazy_ptr";
967 return MCSymbolRefExpr::Create(Name.str(), getContext());
968}
969
Chris Lattner26630c12009-07-31 20:52:39 +0000970
Chris Lattnerf0144122009-07-28 03:13:23 +0000971//===----------------------------------------------------------------------===//
972// COFF
973//===----------------------------------------------------------------------===//
974
Chris Lattner38cff382009-08-13 00:37:15 +0000975typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
976
977TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
978 delete (COFFUniqueMapTy*)UniquingMap;
979}
980
Chris Lattner0c0cb712009-08-08 20:22:20 +0000981
Chris Lattner11e96572009-08-03 21:53:27 +0000982const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000983getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000984 // Create the map if it doesn't already exist.
985 if (UniquingMap == 0)
986 UniquingMap = new MachOUniqueMapTy();
987 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000988
Chris Lattner38cff382009-08-13 00:37:15 +0000989 // Do the lookup, if we have a hit, return it.
990 const MCSectionCOFF *&Entry = Map[Name];
991 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000992
Chris Lattner38cff382009-08-13 00:37:15 +0000993 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000994}
995
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000996void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
997 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000998 if (UniquingMap != 0)
999 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001000 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001001 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1002 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001003 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001004 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001005 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001006 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001007
Chris Lattner35c35312009-08-18 16:56:17 +00001008 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1009 // though it contains relocatable pointers. In PIC mode, this is probably a
1010 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1011 // adjusted or this should be a data section.
1012 LSDASection =
1013 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1014 EHFrameSection =
1015 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001016
Chris Lattner18a4c162009-08-02 07:24:22 +00001017 // Debug info.
1018 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001019 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001020 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1021 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001022 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001023 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1024 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001025 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001026 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1027 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001028 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001029 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1030 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001031 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001032 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1033 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001034 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001035 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1036 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001037 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001038 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1039 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001040 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001041 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1042 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001043 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001044 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1045 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001046 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001047 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1048 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001049 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001050 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1051 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001052}
1053
Chris Lattner24f654c2009-08-06 16:39:58 +00001054const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001055getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001056 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001057 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001058}
1059
Chris Lattnerf0144122009-07-28 03:13:23 +00001060static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1061 if (Kind.isText())
1062 return ".text$linkonce";
1063 if (Kind.isWriteable())
1064 return ".data$linkonce";
1065 return ".rdata$linkonce";
1066}
1067
1068
Chris Lattnera87dea42009-07-31 18:48:30 +00001069const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001070SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001071 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001072 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001073
Chris Lattnerf0144122009-07-28 03:13:23 +00001074 // If this global is linkonce/weak and the target handles this by emitting it
1075 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001076 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001077 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +00001078 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +00001079 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001080 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001081
Chris Lattnerf9650c02009-08-01 21:46:23 +00001082 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001083 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001084
Chris Lattnerf0144122009-07-28 03:13:23 +00001085 return getDataSection();
1086}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001087