blob: 825a9d3c97359ddd27bfa5a842c2fad189e7d783 [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 Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000022#include "llvm/MC/MCSectionELF.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000026#include "llvm/Support/Mangler.h"
Chris Lattner5dc47ff2009-08-12 23:55:02 +000027#include "llvm/ADT/SmallString.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/ADT/StringExtras.h"
29using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Generic Code
33//===----------------------------------------------------------------------===//
34
Chris Lattnera87dea42009-07-31 18:48:30 +000035TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000036 TextSection = 0;
37 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000038 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000039 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000040 StaticCtorSection = 0;
41 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000042 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000043 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000044
45 DwarfAbbrevSection = 0;
46 DwarfInfoSection = 0;
47 DwarfLineSection = 0;
48 DwarfFrameSection = 0;
49 DwarfPubNamesSection = 0;
50 DwarfPubTypesSection = 0;
51 DwarfDebugInlineSection = 0;
52 DwarfStrSection = 0;
53 DwarfLocSection = 0;
54 DwarfARangesSection = 0;
55 DwarfRangesSection = 0;
56 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000057}
58
59TargetLoweringObjectFile::~TargetLoweringObjectFile() {
60}
61
62static bool isSuitableForBSS(const GlobalVariable *GV) {
63 Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000064
Chris Lattnerf0144122009-07-28 03:13:23 +000065 // Must have zero initializer.
66 if (!C->isNullValue())
67 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000068
Chris Lattnerf0144122009-07-28 03:13:23 +000069 // Leave constant zeros in readonly constant sections, so they can be shared.
70 if (GV->isConstant())
71 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000072
Chris Lattnerf0144122009-07-28 03:13:23 +000073 // If the global has an explicit section specified, don't put it in BSS.
74 if (!GV->getSection().empty())
75 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000076
Chris Lattnerf0144122009-07-28 03:13:23 +000077 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
78 if (NoZerosInBSS)
79 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000080
Chris Lattnerf0144122009-07-28 03:13:23 +000081 // Otherwise, put it in BSS!
82 return true;
83}
84
Chris Lattner1850e5a2009-08-04 16:13:09 +000085/// IsNullTerminatedString - Return true if the specified constant (which is
86/// known to have a type that is an array of 1/2/4 byte elements) ends with a
87/// nul value and contains no other nuls in it.
88static bool IsNullTerminatedString(const Constant *C) {
89 const ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000090
Chris Lattnerf0144122009-07-28 03:13:23 +000091 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000092 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
93 if (ATy->getNumElements() == 0) return false;
94
95 ConstantInt *Null =
96 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
97 if (Null == 0 || Null->getZExtValue() != 0)
98 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000099
Chris Lattner1850e5a2009-08-04 16:13:09 +0000100 // Verify that the null doesn't occur anywhere else in the string.
101 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
102 // Reject constantexpr elements etc.
103 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
104 CVA->getOperand(i) == Null)
105 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000106 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000107 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000108
109 // Another possibility: [1 x i8] zeroinitializer
110 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000111 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000112
113 return false;
114}
115
Chris Lattner58bed8f2009-08-05 04:25:40 +0000116/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000117/// a global variable. Given an global variable and information from TM, it
118/// classifies the global in a variety of ways that make various target
119/// implementations simpler. The target implementation is free to ignore this
120/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000121SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
122 const TargetMachine &TM){
123 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
124 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000125
Chris Lattnerf0144122009-07-28 03:13:23 +0000126 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000127
Chris Lattnerf0144122009-07-28 03:13:23 +0000128 // Early exit - functions should be always in text sections.
129 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
130 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000131 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000132
Chris Lattnerf0144122009-07-28 03:13:23 +0000133 // Handle thread-local data first.
134 if (GVar->isThreadLocal()) {
135 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000136 return SectionKind::getThreadBSS();
137 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000138 }
139
140 // Variable can be easily put to BSS section.
141 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000142 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000143
144 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000145
Chris Lattnerf0144122009-07-28 03:13:23 +0000146 // If the global is marked constant, we can put it into a mergable section,
147 // a mergable string section, or general .data if it contains relocations.
148 if (GVar->isConstant()) {
149 // If the initializer for the global contains something that requires a
150 // relocation, then we may have to drop this into a wriable data section
151 // even though it is marked const.
152 switch (C->getRelocationInfo()) {
153 default: llvm_unreachable("unknown relocation info kind");
154 case Constant::NoRelocation:
155 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000156 // section of the right width.
157 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000158 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000159 dyn_cast<IntegerType>(ATy->getElementType())) {
160 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
161 ITy->getBitWidth() == 32) &&
162 IsNullTerminatedString(C)) {
163 if (ITy->getBitWidth() == 8)
164 return SectionKind::getMergeable1ByteCString();
165 if (ITy->getBitWidth() == 16)
166 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000167
Chris Lattner1850e5a2009-08-04 16:13:09 +0000168 assert(ITy->getBitWidth() == 32 && "Unknown width");
169 return SectionKind::getMergeable4ByteCString();
170 }
171 }
172 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000173
Chris Lattnerf0144122009-07-28 03:13:23 +0000174 // Otherwise, just drop it into a mergable constant section. If we have
175 // a section for this size, use it, otherwise use the arbitrary sized
176 // mergable section.
177 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000178 case 4: return SectionKind::getMergeableConst4();
179 case 8: return SectionKind::getMergeableConst8();
180 case 16: return SectionKind::getMergeableConst16();
181 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000182 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000183
Chris Lattnerf0144122009-07-28 03:13:23 +0000184 case Constant::LocalRelocation:
185 // In static relocation model, the linker will resolve all addresses, so
186 // the relocation entries will actually be constants by the time the app
187 // starts up. However, we can't put this into a mergable section, because
188 // the linker doesn't take relocations into consideration when it tries to
189 // merge entries in the section.
190 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000191 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000192
Chris Lattnerf0144122009-07-28 03:13:23 +0000193 // Otherwise, the dynamic linker needs to fix it up, put it in the
194 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000195 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000196
Chris Lattnerf0144122009-07-28 03:13:23 +0000197 case Constant::GlobalRelocations:
198 // In static relocation model, the linker will resolve all addresses, so
199 // the relocation entries will actually be constants by the time the app
200 // starts up. However, we can't put this into a mergable section, because
201 // the linker doesn't take relocations into consideration when it tries to
202 // merge entries in the section.
203 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000204 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000205
Chris Lattnerf0144122009-07-28 03:13:23 +0000206 // Otherwise, the dynamic linker needs to fix it up, put it in the
207 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000208 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000209 }
210 }
211
212 // Okay, this isn't a constant. If the initializer for the global is going
213 // to require a runtime relocation by the dynamic linker, put it into a more
214 // specific section to improve startup time of the app. This coalesces these
215 // globals together onto fewer pages, improving the locality of the dynamic
216 // linker.
217 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000218 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000219
220 switch (C->getRelocationInfo()) {
221 default: llvm_unreachable("unknown relocation info kind");
222 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000223 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000224 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000225 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000226 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000227 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000228 }
229}
230
231/// SectionForGlobal - This method computes the appropriate section to emit
232/// the specified global variable or function definition. This should not
233/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000234const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000235SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000236 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000238 if (GV->hasSection())
239 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000240
241
Chris Lattnerf0144122009-07-28 03:13:23 +0000242 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000243 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000244}
245
Chris Lattner58bed8f2009-08-05 04:25:40 +0000246
Chris Lattnerf0144122009-07-28 03:13:23 +0000247// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000248const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000249TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000250 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000251 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000252 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000253 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000254
Chris Lattnerf9650c02009-08-01 21:46:23 +0000255 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000256 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000257
Chris Lattner82458382009-08-01 21:56:13 +0000258 if (Kind.isBSS() && BSSSection != 0)
259 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000260
Chris Lattnerf9650c02009-08-01 21:46:23 +0000261 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000262 return ReadOnlySection;
263
264 return getDataSection();
265}
266
Chris Lattner83d77fa2009-08-01 23:46:12 +0000267/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000268/// specified size and relocation information, return a section that it
269/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000270const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000271TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000272 if (Kind.isReadOnly() && ReadOnlySection != 0)
273 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000274
Chris Lattnerf0144122009-07-28 03:13:23 +0000275 return DataSection;
276}
277
278
Chris Lattnerf0144122009-07-28 03:13:23 +0000279
280//===----------------------------------------------------------------------===//
281// ELF
282//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000283typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
284
285TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
286 // If we have the section uniquing map, free it.
287 delete (ELFUniqueMapTy*)UniquingMap;
288}
Chris Lattnerf0144122009-07-28 03:13:23 +0000289
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000290const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000291getELFSection(StringRef Section, unsigned Type, unsigned Flags,
292 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000293 if (UniquingMap == 0)
294 UniquingMap = new ELFUniqueMapTy();
295 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000296
Chris Lattner38cff382009-08-13 00:37:15 +0000297 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000298 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000299 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000300
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000301 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
302 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000303}
304
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000305void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
306 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000307 if (UniquingMap != 0)
308 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000309 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000310
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000311 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000312 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
313 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
314 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000315
316 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000317 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
318 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
319 SectionKind::getText());
320
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000321 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000322 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
323 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
324 SectionKind::getDataRel());
325
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000326 ReadOnlySection =
327 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
328 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000329 SectionKind::getReadOnly());
330
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000331 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000332 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000333 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000334 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000335
336 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000337 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
338 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
339 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000340
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000341 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000342 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
343 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
344 SectionKind::getDataRel());
345
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000346 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000347 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
348 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
349 SectionKind::getDataRelLocal());
350
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000351 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000352 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
353 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
354 SectionKind::getReadOnlyWithRel());
355
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000356 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000357 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
358 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
359 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000360
361 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000362 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
363 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
364 SectionKind::getMergeableConst4());
365
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000366 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000367 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
368 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
369 SectionKind::getMergeableConst8());
370
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000371 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000372 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
373 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
374 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000375
376 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000377 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000378 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
379 SectionKind::getDataRel());
380
Chris Lattner80ec2792009-08-02 00:34:36 +0000381 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000382 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
383 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
384 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000385
Chris Lattner18a4c162009-08-02 07:24:22 +0000386 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000387
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000388 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
389 // it contains relocatable pointers. In PIC mode, this is probably a big
390 // runtime hit for C++ apps. Either the contents of the LSDA need to be
391 // adjusted or this should be a data section.
392 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000393 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
394 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000395 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000396 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000397 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
398 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000399
Chris Lattner18a4c162009-08-02 07:24:22 +0000400 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000401 DwarfAbbrevSection =
402 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000403 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000404 DwarfInfoSection =
405 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000406 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000407 DwarfLineSection =
408 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000409 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000410 DwarfFrameSection =
411 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000412 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000413 DwarfPubNamesSection =
414 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000415 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000416 DwarfPubTypesSection =
417 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000418 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000419 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000420 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
421 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000422 DwarfLocSection =
423 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000424 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000425 DwarfARangesSection =
426 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000427 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000428 DwarfRangesSection =
429 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000430 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000431 DwarfMacroInfoSection =
432 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000433 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000434}
435
436
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000437static SectionKind
Chris Lattner24f654c2009-08-06 16:39:58 +0000438getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000439 if (Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000440
Chris Lattnerf0144122009-07-28 03:13:23 +0000441 // Some lame default implementation based on some magic section names.
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000442 if (strcmp(Name, ".bss") == 0 ||
443 strncmp(Name, ".bss.", 5) == 0 ||
444 strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000445 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
Anton Korobeynikov4c0b3492009-09-09 08:48:53 +0000446 strcmp(Name, ".sbss") == 0 ||
447 strncmp(Name, ".sbss.", 6) == 0 ||
Chris Lattnerf0144122009-07-28 03:13:23 +0000448 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
449 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000450 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000451
Chris Lattnerf0144122009-07-28 03:13:23 +0000452 if (strcmp(Name, ".tdata") == 0 ||
453 strncmp(Name, ".tdata.", 7) == 0 ||
454 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
455 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000456 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000457
Chris Lattnerf0144122009-07-28 03:13:23 +0000458 if (strcmp(Name, ".tbss") == 0 ||
459 strncmp(Name, ".tbss.", 6) == 0 ||
460 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
461 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000462 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000463
Chris Lattnerf0144122009-07-28 03:13:23 +0000464 return K;
465}
466
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000467
468static unsigned
469getELFSectionType(const char *Name, SectionKind K) {
470
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000471 if (strcmp(Name, ".init_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000472 return MCSectionELF::SHT_INIT_ARRAY;
473
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000474 if (strcmp(Name, ".fini_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000475 return MCSectionELF::SHT_FINI_ARRAY;
476
Dan Gohmanfa9ca0f2009-08-14 00:10:19 +0000477 if (strcmp(Name, ".preinit_array") == 0)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000478 return MCSectionELF::SHT_PREINIT_ARRAY;
479
480 if (K.isBSS() || K.isThreadBSS())
481 return MCSectionELF::SHT_NOBITS;
482
483 return MCSectionELF::SHT_PROGBITS;
484}
485
486
487static unsigned
488getELFSectionFlags(SectionKind K) {
489 unsigned Flags = 0;
490
491 if (!K.isMetadata())
492 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000493
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000494 if (K.isText())
495 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000496
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000497 if (K.isWriteable())
498 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000499
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000500 if (K.isThreadLocal())
501 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000502
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000503 // K.isMergeableConst() is left out to honour PR4650
504 if (K.isMergeableCString() || K.isMergeableConst4() ||
505 K.isMergeableConst8() || K.isMergeableConst16())
506 Flags |= MCSectionELF::SHF_MERGE;
507
508 if (K.isMergeableCString())
509 Flags |= MCSectionELF::SHF_STRINGS;
510
511 return Flags;
512}
513
514
Chris Lattner24f654c2009-08-06 16:39:58 +0000515const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000516getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000517 Mangler *Mang, const TargetMachine &TM) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000518 const char *SectionName = GV->getSection().c_str();
519
Chris Lattner24f654c2009-08-06 16:39:58 +0000520 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000521 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000522
523 return getELFSection(SectionName,
524 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000525 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000526}
Chris Lattnerf0144122009-07-28 03:13:23 +0000527
528static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
529 if (Kind.isText()) return ".gnu.linkonce.t.";
530 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000531
Chris Lattnerf0144122009-07-28 03:13:23 +0000532 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
533 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000534
Chris Lattnerf0144122009-07-28 03:13:23 +0000535 if (Kind.isBSS()) return ".gnu.linkonce.b.";
536 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
537 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
538 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
539 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000540
Chris Lattnerf0144122009-07-28 03:13:23 +0000541 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
542 return ".gnu.linkonce.d.rel.ro.";
543}
544
Chris Lattnera87dea42009-07-31 18:48:30 +0000545const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000546SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000547 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000548
Chris Lattnerf0144122009-07-28 03:13:23 +0000549 // If this global is linkonce/weak and the target handles this by emitting it
550 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000551 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000552 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000553 std::string Name = Mang->makeNameProper(GV->getNameStr());
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000554
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000555 return getELFSection((Prefix+Name).c_str(),
556 getELFSectionType((Prefix+Name).c_str(), Kind),
557 getELFSectionFlags(Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000558 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000559 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000560
Chris Lattnerf9650c02009-08-01 21:46:23 +0000561 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000562
Chris Lattner3b24c012009-08-04 05:35:56 +0000563 if (Kind.isMergeable1ByteCString() ||
564 Kind.isMergeable2ByteCString() ||
565 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000566
Chris Lattner067fe1a2009-07-29 04:54:38 +0000567 // We also need alignment here.
568 // FIXME: this is getting the alignment of the character, not the
569 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000570 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000571 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000572
Chris Lattner7e88a502009-08-04 16:19:50 +0000573 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000574 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000575 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000576 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000577 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000578 else
579 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000580
581
Chris Lattner7e88a502009-08-04 16:19:50 +0000582 std::string Name = SizeSpec + utostr(Align);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000583 return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
584 MCSectionELF::SHF_ALLOC |
585 MCSectionELF::SHF_MERGE |
586 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000587 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000588 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000589
Chris Lattnerf9650c02009-08-01 21:46:23 +0000590 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000591 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000592 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000593 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000594 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000595 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000596 return MergeableConst16Section;
597 return ReadOnlySection; // .const
598 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000599
Chris Lattnerf9650c02009-08-01 21:46:23 +0000600 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000601
Chris Lattnerf9650c02009-08-01 21:46:23 +0000602 if (Kind.isThreadData()) return TLSDataSection;
603 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000604
Chris Lattner82458382009-08-01 21:56:13 +0000605 if (Kind.isBSS()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000606
Chris Lattnerf9650c02009-08-01 21:46:23 +0000607 if (Kind.isDataNoRel()) return DataSection;
608 if (Kind.isDataRelLocal()) return DataRelLocalSection;
609 if (Kind.isDataRel()) return DataRelSection;
610 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000611
Chris Lattnerf9650c02009-08-01 21:46:23 +0000612 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000613 return DataRelROSection;
614}
615
Chris Lattner83d77fa2009-08-01 23:46:12 +0000616/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000617/// specified size and relocation information, return a section that it
618/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000619const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000620getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000621 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000622 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000623 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000624 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000625 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000626 return MergeableConst16Section;
627 if (Kind.isReadOnly())
628 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000629
Chris Lattnerf0144122009-07-28 03:13:23 +0000630 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
631 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
632 return DataRelROSection;
633}
634
635//===----------------------------------------------------------------------===//
636// MachO
637//===----------------------------------------------------------------------===//
638
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000639typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000640
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000641TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
642 // If we have the MachO uniquing map, free it.
643 delete (MachOUniqueMapTy*)UniquingMap;
644}
645
646
647const MCSectionMachO *TargetLoweringObjectFileMachO::
Chris Lattnerd3c4486f2009-08-12 23:34:27 +0000648getMachOSection(const StringRef &Segment, const StringRef &Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000649 unsigned TypeAndAttributes,
650 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000651 // We unique sections by their segment/section pair. The returned section
652 // may not have the same flags as the requested section, if so this should be
653 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000654
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000655 // Create the map if it doesn't already exist.
656 if (UniquingMap == 0)
657 UniquingMap = new MachOUniqueMapTy();
658 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000659
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000660 // Form the name to look up.
661 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000662 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000663 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000664 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000665
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000666 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000667 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000668 if (Entry) return Entry;
669
670 // Otherwise, return a new section.
671 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
672 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000673}
674
Chris Lattner11e96572009-08-03 21:53:27 +0000675
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000676void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
677 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000678 if (UniquingMap != 0)
679 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000680 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000681
Chris Lattnerff4bc462009-08-10 01:39:42 +0000682 TextSection // .text
683 = getMachOSection("__TEXT", "__text",
684 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
685 SectionKind::getText());
686 DataSection // .data
687 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000688
Chris Lattnerff4bc462009-08-10 01:39:42 +0000689 CStringSection // .cstring
690 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
691 SectionKind::getMergeable1ByteCString());
692 UStringSection
693 = getMachOSection("__TEXT","__ustring", 0,
694 SectionKind::getMergeable2ByteCString());
695 FourByteConstantSection // .literal4
696 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
697 SectionKind::getMergeableConst4());
698 EightByteConstantSection // .literal8
699 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
700 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000701
Chris Lattner4bb253c2009-07-28 17:50:28 +0000702 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
703 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000704 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000705 if (TM.getRelocationModel() != Reloc::Static &&
706 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000707 SixteenByteConstantSection = // .literal16
708 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000709 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000710
Chris Lattnerff4bc462009-08-10 01:39:42 +0000711 ReadOnlySection // .const
712 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000713
Chris Lattnerff4bc462009-08-10 01:39:42 +0000714 TextCoalSection
715 = getMachOSection("__TEXT", "__textcoal_nt",
716 MCSectionMachO::S_COALESCED |
717 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
718 SectionKind::getText());
719 ConstTextCoalSection
720 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
721 SectionKind::getText());
722 ConstDataCoalSection
723 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
724 SectionKind::getText());
725 ConstDataSection // .const_data
726 = getMachOSection("__DATA", "__const", 0,
727 SectionKind::getReadOnlyWithRel());
728 DataCoalSection
729 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
730 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000731
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000732
Chris Lattnere309cfa2009-08-13 00:05:07 +0000733 LazySymbolPointerSection
734 = getMachOSection("__DATA", "__la_symbol_ptr",
735 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
736 SectionKind::getMetadata());
737 NonLazySymbolPointerSection
738 = getMachOSection("__DATA", "__nl_symbol_ptr",
739 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
740 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000741
Chris Lattner80ec2792009-08-02 00:34:36 +0000742 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000743 StaticCtorSection
744 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
745 StaticDtorSection
746 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000747 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000748 StaticCtorSection
749 = getMachOSection("__DATA", "__mod_init_func",
750 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
751 SectionKind::getDataRel());
752 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000753 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000754 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
755 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000756 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000757
Chris Lattner18a4c162009-08-02 07:24:22 +0000758 // Exception Handling.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000759 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000760 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000761 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000762 getMachOSection("__TEXT", "__eh_frame",
763 MCSectionMachO::S_COALESCED |
764 MCSectionMachO::S_ATTR_NO_TOC |
765 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
766 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
767 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000768
769 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000770 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000771 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000772 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000773 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000774 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000775 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000776 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000777 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000778 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000779 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000780 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000781 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000782 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000783 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000784 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000785 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000786 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000787 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000788 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000789 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000790 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000791 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000792 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000793 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000794 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000795 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000796 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000797 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000798 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000799 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000800 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000801 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000802 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000803 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000804 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000805 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000806}
807
Chris Lattnera87dea42009-07-31 18:48:30 +0000808const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000809getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000810 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000811 // Parse the section specifier and create it if valid.
812 StringRef Segment, Section;
813 unsigned TAA, StubSize;
814 std::string ErrorCode =
815 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
816 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000817 if (!ErrorCode.empty()) {
818 // If invalid, report the error with llvm_report_error.
819 llvm_report_error("Global variable '" + GV->getNameStr() +
820 "' has an invalid section specifier '" + GV->getSection()+
821 "': " + ErrorCode + ".");
822 // Fall back to dropping it into the data section.
823 return DataSection;
824 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000825
Chris Lattnere309cfa2009-08-13 00:05:07 +0000826 // Get the section.
827 const MCSectionMachO *S =
828 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000829
Chris Lattnere309cfa2009-08-13 00:05:07 +0000830 // Okay, now that we got the section, verify that the TAA & StubSize agree.
831 // If the user declared multiple globals with different section flags, we need
832 // to reject it here.
833 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
834 // If invalid, report the error with llvm_report_error.
835 llvm_report_error("Global variable '" + GV->getNameStr() +
836 "' section type or attributes does not match previous"
837 " section specifier");
838 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000839
Chris Lattnere309cfa2009-08-13 00:05:07 +0000840 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000841}
842
843const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000844SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000845 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000846 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000847
Chris Lattnerf9650c02009-08-01 21:46:23 +0000848 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000849 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000850
Chris Lattnerf0144122009-07-28 03:13:23 +0000851 // If this is weak/linkonce, put this in a coalescable section, either in text
852 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000853 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000854 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000855 return ConstTextCoalSection;
856 return DataCoalSection;
857 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000858
Chris Lattnerf0144122009-07-28 03:13:23 +0000859 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000860 if (Kind.isMergeable1ByteCString() ||
861 Kind.isMergeable2ByteCString()) {
862 if (TM.getTargetData()->getPreferredAlignment(
863 cast<GlobalVariable>(GV)) < 32) {
864 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000865 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000866 assert(Kind.isMergeable2ByteCString());
867 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000868 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000869 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000870
Chris Lattnerf9650c02009-08-01 21:46:23 +0000871 if (Kind.isMergeableConst()) {
872 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000873 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000874 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000875 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000876 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000877 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000878 }
Chris Lattnerec409752009-08-04 16:27:13 +0000879
880 // Otherwise, if it is readonly, but not something we can specially optimize,
881 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000882 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000883 return ReadOnlySection;
884
885 // If this is marked const, put it into a const section. But if the dynamic
886 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000887 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000888 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000889
Chris Lattnerf0144122009-07-28 03:13:23 +0000890 // Otherwise, just drop the variable in the normal data section.
891 return DataSection;
892}
893
Chris Lattnera87dea42009-07-31 18:48:30 +0000894const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000895TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000896 // If this constant requires a relocation, we have to put it in the data
897 // segment, not in the text segment.
898 if (Kind.isDataRel())
899 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000900
Chris Lattnerf0144122009-07-28 03:13:23 +0000901 if (Kind.isMergeableConst4())
902 return FourByteConstantSection;
903 if (Kind.isMergeableConst8())
904 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000905 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000906 return SixteenByteConstantSection;
907 return ReadOnlySection; // .const
908}
909
Chris Lattner26630c12009-07-31 20:52:39 +0000910/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
911/// not to emit the UsedDirective for some symbols in llvm.used.
912// FIXME: REMOVE this (rdar://7071300)
913bool TargetLoweringObjectFileMachO::
914shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
915 /// On Darwin, internally linked data beginning with "L" or "l" does not have
916 /// the directive emitted (this occurs in ObjC metadata).
917 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000918
Chris Lattner26630c12009-07-31 20:52:39 +0000919 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
920 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
921 // FIXME: ObjC metadata is currently emitted as internal symbols that have
922 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
923 // this horrible hack can go away.
924 const std::string &Name = Mang->getMangledName(GV);
925 if (Name[0] == 'L' || Name[0] == 'l')
926 return false;
927 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000928
Chris Lattner26630c12009-07-31 20:52:39 +0000929 return true;
930}
931
932
Chris Lattnerf0144122009-07-28 03:13:23 +0000933//===----------------------------------------------------------------------===//
934// COFF
935//===----------------------------------------------------------------------===//
936
Chris Lattner38cff382009-08-13 00:37:15 +0000937typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
938
939TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
940 delete (COFFUniqueMapTy*)UniquingMap;
941}
942
Chris Lattner0c0cb712009-08-08 20:22:20 +0000943
Chris Lattner11e96572009-08-03 21:53:27 +0000944const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000945getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000946 // Create the map if it doesn't already exist.
947 if (UniquingMap == 0)
948 UniquingMap = new MachOUniqueMapTy();
949 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000950
Chris Lattner38cff382009-08-13 00:37:15 +0000951 // Do the lookup, if we have a hit, return it.
952 const MCSectionCOFF *&Entry = Map[Name];
953 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000954
Chris Lattner38cff382009-08-13 00:37:15 +0000955 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000956}
957
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000958void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
959 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000960 if (UniquingMap != 0)
961 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000962 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000963 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
964 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000965 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000966 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000967 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000968 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000969
Chris Lattner35c35312009-08-18 16:56:17 +0000970 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
971 // though it contains relocatable pointers. In PIC mode, this is probably a
972 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
973 // adjusted or this should be a data section.
974 LSDASection =
975 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
976 EHFrameSection =
977 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000978
Chris Lattner18a4c162009-08-02 07:24:22 +0000979 // Debug info.
980 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000981 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000982 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
983 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000984 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000985 getCOFFSection("\t.section\t.debug_info,\"dr\"",
986 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000987 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000988 getCOFFSection("\t.section\t.debug_line,\"dr\"",
989 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000990 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000991 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
992 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000993 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000994 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
995 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000996 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000997 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
998 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000999 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001000 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1001 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001002 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001003 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1004 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001005 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001006 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1007 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001008 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001009 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1010 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001011 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001012 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1013 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001014}
1015
Chris Lattner24f654c2009-08-06 16:39:58 +00001016const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001017getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001018 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +00001019 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001020}
1021
Chris Lattnerf0144122009-07-28 03:13:23 +00001022static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1023 if (Kind.isText())
1024 return ".text$linkonce";
1025 if (Kind.isWriteable())
1026 return ".data$linkonce";
1027 return ".rdata$linkonce";
1028}
1029
1030
Chris Lattnera87dea42009-07-31 18:48:30 +00001031const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001032SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001033 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001034 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001035
Chris Lattnerf0144122009-07-28 03:13:23 +00001036 // If this global is linkonce/weak and the target handles this by emitting it
1037 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001038 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001039 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +00001040 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +00001041 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001042 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001043
Chris Lattnerf9650c02009-08-01 21:46:23 +00001044 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001045 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001046
Chris Lattnerf0144122009-07-28 03:13:23 +00001047 return getDataSection();
1048}