blob: 00f4ffe56020d085ae98f8a79dd9e8d051a2dc37 [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"
Chris Lattner5277b222009-08-08 20:43:12 +000022#include "llvm/Target/TargetAsmInfo.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();
64
65 // Must have zero initializer.
66 if (!C->isNullValue())
67 return false;
68
69 // Leave constant zeros in readonly constant sections, so they can be shared.
70 if (GV->isConstant())
71 return false;
72
73 // If the global has an explicit section specified, don't put it in BSS.
74 if (!GV->getSection().empty())
75 return false;
76
77 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
78 if (NoZerosInBSS)
79 return false;
80
81 // 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());
90
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.
99
100 // 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");
125
Chris Lattnerf0144122009-07-28 03:13:23 +0000126 Reloc::Model ReloModel = TM.getRelocationModel();
127
128 // 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();
Chris Lattnerf0144122009-07-28 03:13:23 +0000132
133 // 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();
145
146 // 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())) {
158 if (const IntegerType *ITy =
159 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();
167
168 assert(ITy->getBitWidth() == 32 && "Unknown width");
169 return SectionKind::getMergeable4ByteCString();
170 }
171 }
172 }
Chris Lattner3b24c012009-08-04 05:35:56 +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 }
183
184 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();
Chris Lattnerf0144122009-07-28 03:13:23 +0000192
193 // 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();
Chris Lattnerf0144122009-07-28 03:13:23 +0000196
197 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();
Chris Lattnerf0144122009-07-28 03:13:23 +0000205
206 // 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);
240
Chris Lattnerf0144122009-07-28 03:13:23 +0000241
242 // 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");
Chris Lattnerf0144122009-07-28 03:13:23 +0000254
Chris Lattnerf9650c02009-08-01 21:46:23 +0000255 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000256 return getTextSection();
257
Chris Lattner82458382009-08-01 21:56:13 +0000258 if (Kind.isBSS() && BSSSection != 0)
259 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +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;
274
275 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::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000291getELFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000292 // Create the map if it doesn't already exist.
293 if (UniquingMap == 0)
294 UniquingMap = new ELFUniqueMapTy();
295 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
296
297 // Do the lookup, if we have a hit, return it.
298 const MCSectionELF *&Entry = Map[Name];
299 if (Entry) return Entry;
300
301 return Entry = MCSectionELF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000302}
303
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000304void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
305 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000306 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000307 if (!HasCrazyBSS)
Chris Lattner0c0cb712009-08-08 20:22:20 +0000308 BSSSection = getELFSection("\t.bss", true, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000309 else
310 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
311 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000312 // FIXME2: this should just be handle by the section printer. We should get
313 // away from syntactic view of the sections and MCSection should just be a
314 // semantic view.
Chris Lattner0c0cb712009-08-08 20:22:20 +0000315 BSSSection = getELFSection("\t.bss", false, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000316
317
Chris Lattner0c0cb712009-08-08 20:22:20 +0000318 TextSection = getELFSection("\t.text", true, SectionKind::getText());
319 DataSection = getELFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000320 ReadOnlySection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000321 getELFSection("\t.rodata", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000322 TLSDataSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000323 getELFSection("\t.tdata", false, SectionKind::getThreadData());
Chris Lattner3b24c012009-08-04 05:35:56 +0000324
Chris Lattner0c0cb712009-08-08 20:22:20 +0000325 TLSBSSSection = getELFSection("\t.tbss", false,
Chris Lattner27981192009-08-01 23:57:16 +0000326 SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000327
Chris Lattner0c0cb712009-08-08 20:22:20 +0000328 DataRelSection = getELFSection("\t.data.rel", false,
Chris Lattner27981192009-08-01 23:57:16 +0000329 SectionKind::getDataRel());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000330 DataRelLocalSection = getELFSection("\t.data.rel.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000331 SectionKind::getDataRelLocal());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000332 DataRelROSection = getELFSection("\t.data.rel.ro", false,
Chris Lattner27981192009-08-01 23:57:16 +0000333 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000334 DataRelROLocalSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000335 getELFSection("\t.data.rel.ro.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000336 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000337
Chris Lattner0c0cb712009-08-08 20:22:20 +0000338 MergeableConst4Section = getELFSection(".rodata.cst4", false,
Chris Lattner27981192009-08-01 23:57:16 +0000339 SectionKind::getMergeableConst4());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000340 MergeableConst8Section = getELFSection(".rodata.cst8", false,
Chris Lattner27981192009-08-01 23:57:16 +0000341 SectionKind::getMergeableConst8());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000342 MergeableConst16Section = getELFSection(".rodata.cst16", false,
Chris Lattner27981192009-08-01 23:57:16 +0000343 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000344
345 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000346 getELFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000347 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000348 getELFSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000349
Chris Lattner18a4c162009-08-02 07:24:22 +0000350 // Exception Handling Sections.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000351
352 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
353 // it contains relocatable pointers. In PIC mode, this is probably a big
354 // runtime hit for C++ apps. Either the contents of the LSDA need to be
355 // adjusted or this should be a data section.
356 LSDASection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000357 getELFSection(".gcc_except_table", false, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000358 EHFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000359 getELFSection(".eh_frame", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000360
361 // Debug Info Sections.
362 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000363 getELFSection(".debug_abbrev", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000364 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000365 getELFSection(".debug_info", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000366 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000367 getELFSection(".debug_line", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000368 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000369 getELFSection(".debug_frame", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000370 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000371 getELFSection(".debug_pubnames", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000372 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000373 getELFSection(".debug_pubtypes", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000374 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000375 getELFSection(".debug_str", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000376 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000377 getELFSection(".debug_loc", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000378 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000379 getELFSection(".debug_aranges", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000380 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000381 getELFSection(".debug_ranges", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000382 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000383 getELFSection(".debug_macinfo", false, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000384}
385
386
Chris Lattner24f654c2009-08-06 16:39:58 +0000387static SectionKind
388getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000389 if (Name[0] != '.') return K;
390
391 // Some lame default implementation based on some magic section names.
392 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
393 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
394 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
395 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000396 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000397
398 if (strcmp(Name, ".tdata") == 0 ||
399 strncmp(Name, ".tdata.", 7) == 0 ||
400 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
401 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000402 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000403
404 if (strcmp(Name, ".tbss") == 0 ||
405 strncmp(Name, ".tbss.", 6) == 0 ||
406 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
407 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000408 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000409
410 return K;
411}
412
Chris Lattner24f654c2009-08-06 16:39:58 +0000413const MCSection *TargetLoweringObjectFileELF::
414getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
415 Mangler *Mang, const TargetMachine &TM) const {
416 // Infer section flags from the section name if we can.
417 Kind = getELFKindForNamedSection(GV->getSection().c_str(), Kind);
418
Chris Lattner0c0cb712009-08-08 20:22:20 +0000419 return getELFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000420}
Chris Lattnerf0144122009-07-28 03:13:23 +0000421
422static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
423 if (Kind.isText()) return ".gnu.linkonce.t.";
424 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
425
426 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
427 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
428
429 if (Kind.isBSS()) return ".gnu.linkonce.b.";
430 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
431 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
432 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
433 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
434
435 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
436 return ".gnu.linkonce.d.rel.ro.";
437}
438
Chris Lattnera87dea42009-07-31 18:48:30 +0000439const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000440SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000441 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000442
443 // If this global is linkonce/weak and the target handles this by emitting it
444 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000445 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000446 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000447 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000448 return getELFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000449 }
450
Chris Lattnerf9650c02009-08-01 21:46:23 +0000451 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000452
Chris Lattner3b24c012009-08-04 05:35:56 +0000453 if (Kind.isMergeable1ByteCString() ||
454 Kind.isMergeable2ByteCString() ||
455 Kind.isMergeable4ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000456
Chris Lattner067fe1a2009-07-29 04:54:38 +0000457 // We also need alignment here.
458 // FIXME: this is getting the alignment of the character, not the
459 // alignment of the global!
460 unsigned Align =
461 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000462
Chris Lattner7e88a502009-08-04 16:19:50 +0000463 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000464 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000465 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000466 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000467 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000468 else
469 assert(Kind.isMergeable1ByteCString() && "unknown string width");
470
471
Chris Lattner7e88a502009-08-04 16:19:50 +0000472 std::string Name = SizeSpec + utostr(Align);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000473 return getELFSection(Name.c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000474 }
475
Chris Lattnerf9650c02009-08-01 21:46:23 +0000476 if (Kind.isMergeableConst()) {
477 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000478 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000479 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000480 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000481 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000482 return MergeableConst16Section;
483 return ReadOnlySection; // .const
484 }
485
Chris Lattnerf9650c02009-08-01 21:46:23 +0000486 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000487
Chris Lattnerf9650c02009-08-01 21:46:23 +0000488 if (Kind.isThreadData()) return TLSDataSection;
489 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000490
Chris Lattner82458382009-08-01 21:56:13 +0000491 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000492
Chris Lattnerf9650c02009-08-01 21:46:23 +0000493 if (Kind.isDataNoRel()) return DataSection;
494 if (Kind.isDataRelLocal()) return DataRelLocalSection;
495 if (Kind.isDataRel()) return DataRelSection;
496 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000497
Chris Lattnerf9650c02009-08-01 21:46:23 +0000498 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000499 return DataRelROSection;
500}
501
Chris Lattner83d77fa2009-08-01 23:46:12 +0000502/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000503/// specified size and relocation information, return a section that it
504/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000505const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000506getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000507 if (Kind.isMergeableConst4())
508 return MergeableConst4Section;
509 if (Kind.isMergeableConst8())
510 return MergeableConst8Section;
511 if (Kind.isMergeableConst16())
512 return MergeableConst16Section;
513 if (Kind.isReadOnly())
514 return ReadOnlySection;
515
516 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
517 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
518 return DataRelROSection;
519}
520
521//===----------------------------------------------------------------------===//
522// MachO
523//===----------------------------------------------------------------------===//
524
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000525typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000526
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000527TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
528 // If we have the MachO uniquing map, free it.
529 delete (MachOUniqueMapTy*)UniquingMap;
530}
531
532
533const MCSectionMachO *TargetLoweringObjectFileMachO::
Chris Lattnerd3c4486f2009-08-12 23:34:27 +0000534getMachOSection(const StringRef &Segment, const StringRef &Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000535 unsigned TypeAndAttributes,
536 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000537 // We unique sections by their segment/section pair. The returned section
538 // may not have the same flags as the requested section, if so this should be
539 // diagnosed by the client as an error.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000540
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000541 // Create the map if it doesn't already exist.
542 if (UniquingMap == 0)
543 UniquingMap = new MachOUniqueMapTy();
544 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
545
546 // Form the name to look up.
547 SmallString<64> Name;
548 Name.append(Segment.begin(), Segment.end());
549 Name.push_back(',');
550 Name.append(Section.begin(), Section.end());
551
552 // Do the lookup, if we have a hit, return it.
553 const MCSectionMachO *&Entry = Map[StringRef(Name.data(), Name.size())];
554 if (Entry) return Entry;
555
556 // Otherwise, return a new section.
557 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
558 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000559}
560
Chris Lattner11e96572009-08-03 21:53:27 +0000561
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000562void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
563 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000564 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000565
Chris Lattnerff4bc462009-08-10 01:39:42 +0000566 TextSection // .text
567 = getMachOSection("__TEXT", "__text",
568 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
569 SectionKind::getText());
570 DataSection // .data
571 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
572
573 CStringSection // .cstring
574 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
575 SectionKind::getMergeable1ByteCString());
576 UStringSection
577 = getMachOSection("__TEXT","__ustring", 0,
578 SectionKind::getMergeable2ByteCString());
579 FourByteConstantSection // .literal4
580 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
581 SectionKind::getMergeableConst4());
582 EightByteConstantSection // .literal8
583 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
584 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000585
586 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
587 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000588 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000589 if (TM.getRelocationModel() != Reloc::Static &&
590 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000591 SixteenByteConstantSection = // .literal16
592 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000593 SectionKind::getMergeableConst16());
Chris Lattnerf0144122009-07-28 03:13:23 +0000594
Chris Lattnerff4bc462009-08-10 01:39:42 +0000595 ReadOnlySection // .const
596 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000597
Chris Lattnerff4bc462009-08-10 01:39:42 +0000598 TextCoalSection
599 = getMachOSection("__TEXT", "__textcoal_nt",
600 MCSectionMachO::S_COALESCED |
601 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
602 SectionKind::getText());
603 ConstTextCoalSection
604 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
605 SectionKind::getText());
606 ConstDataCoalSection
607 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
608 SectionKind::getText());
609 ConstDataSection // .const_data
610 = getMachOSection("__DATA", "__const", 0,
611 SectionKind::getReadOnlyWithRel());
612 DataCoalSection
613 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
614 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000615
Chris Lattnere309cfa2009-08-13 00:05:07 +0000616
617 LazySymbolPointerSection
618 = getMachOSection("__DATA", "__la_symbol_ptr",
619 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
620 SectionKind::getMetadata());
621 NonLazySymbolPointerSection
622 = getMachOSection("__DATA", "__nl_symbol_ptr",
623 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
624 SectionKind::getMetadata());
625
Chris Lattner80ec2792009-08-02 00:34:36 +0000626 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000627 StaticCtorSection
628 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
629 StaticDtorSection
630 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000631 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000632 StaticCtorSection
633 = getMachOSection("__DATA", "__mod_init_func",
634 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
635 SectionKind::getDataRel());
636 StaticDtorSection
637 = getMachOSection("__DATA", "__mod_term_func",
638 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
639 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000640 }
641
Chris Lattner18a4c162009-08-02 07:24:22 +0000642 // Exception Handling.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000643 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000644 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000645 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000646 getMachOSection("__TEXT", "__eh_frame",
647 MCSectionMachO::S_COALESCED |
648 MCSectionMachO::S_ATTR_NO_TOC |
649 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
650 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
651 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000652
653 // Debug Information.
Chris Lattner18a4c162009-08-02 07:24:22 +0000654 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000655 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000656 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000657 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000658 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000659 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000660 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000661 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000662 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000663 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000664 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000665 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000666 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000667 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000668 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000669 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000670 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000671 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000672 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000673 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000674 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000675 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000676 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000677 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000678 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000679 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000680 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000681 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000682 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000683 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000684 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000685 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000686 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000687 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000688 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000689 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000690}
691
Chris Lattnera87dea42009-07-31 18:48:30 +0000692const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner24f654c2009-08-06 16:39:58 +0000693getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
694 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000695 // Parse the section specifier and create it if valid.
696 StringRef Segment, Section;
697 unsigned TAA, StubSize;
698 std::string ErrorCode =
699 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
700 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000701 if (!ErrorCode.empty()) {
702 // If invalid, report the error with llvm_report_error.
703 llvm_report_error("Global variable '" + GV->getNameStr() +
704 "' has an invalid section specifier '" + GV->getSection()+
705 "': " + ErrorCode + ".");
706 // Fall back to dropping it into the data section.
707 return DataSection;
708 }
709
710 // Get the section.
711 const MCSectionMachO *S =
712 getMachOSection(Segment, Section, TAA, StubSize, Kind);
713
714 // Okay, now that we got the section, verify that the TAA & StubSize agree.
715 // If the user declared multiple globals with different section flags, we need
716 // to reject it here.
717 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
718 // If invalid, report the error with llvm_report_error.
719 llvm_report_error("Global variable '" + GV->getNameStr() +
720 "' section type or attributes does not match previous"
721 " section specifier");
722 }
Chris Lattnerff4bc462009-08-10 01:39:42 +0000723
Chris Lattnere309cfa2009-08-13 00:05:07 +0000724 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000725}
726
727const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000728SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000729 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000730 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000731
Chris Lattnerf9650c02009-08-01 21:46:23 +0000732 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000733 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000734
735 // If this is weak/linkonce, put this in a coalescable section, either in text
736 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000737 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000738 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000739 return ConstTextCoalSection;
740 return DataCoalSection;
741 }
742
743 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000744 if (Kind.isMergeable1ByteCString() ||
745 Kind.isMergeable2ByteCString()) {
746 if (TM.getTargetData()->getPreferredAlignment(
747 cast<GlobalVariable>(GV)) < 32) {
748 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000749 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000750 assert(Kind.isMergeable2ByteCString());
751 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000752 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000753 }
754
Chris Lattnerf9650c02009-08-01 21:46:23 +0000755 if (Kind.isMergeableConst()) {
756 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000757 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000758 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000759 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000760 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000761 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000762 }
Chris Lattnerec409752009-08-04 16:27:13 +0000763
764 // Otherwise, if it is readonly, but not something we can specially optimize,
765 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000766 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000767 return ReadOnlySection;
768
769 // If this is marked const, put it into a const section. But if the dynamic
770 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000771 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000772 return ConstDataSection;
773
774 // Otherwise, just drop the variable in the normal data section.
775 return DataSection;
776}
777
Chris Lattnera87dea42009-07-31 18:48:30 +0000778const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000779TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000780 // If this constant requires a relocation, we have to put it in the data
781 // segment, not in the text segment.
782 if (Kind.isDataRel())
783 return ConstDataSection;
784
785 if (Kind.isMergeableConst4())
786 return FourByteConstantSection;
787 if (Kind.isMergeableConst8())
788 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000789 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000790 return SixteenByteConstantSection;
791 return ReadOnlySection; // .const
792}
793
Chris Lattner26630c12009-07-31 20:52:39 +0000794/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
795/// not to emit the UsedDirective for some symbols in llvm.used.
796// FIXME: REMOVE this (rdar://7071300)
797bool TargetLoweringObjectFileMachO::
798shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
799 /// On Darwin, internally linked data beginning with "L" or "l" does not have
800 /// the directive emitted (this occurs in ObjC metadata).
801 if (!GV) return false;
802
803 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
804 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
805 // FIXME: ObjC metadata is currently emitted as internal symbols that have
806 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
807 // this horrible hack can go away.
808 const std::string &Name = Mang->getMangledName(GV);
809 if (Name[0] == 'L' || Name[0] == 'l')
810 return false;
811 }
812
813 return true;
814}
815
816
Chris Lattnerf0144122009-07-28 03:13:23 +0000817//===----------------------------------------------------------------------===//
818// COFF
819//===----------------------------------------------------------------------===//
820
Chris Lattner38cff382009-08-13 00:37:15 +0000821typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
822
823TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
824 delete (COFFUniqueMapTy*)UniquingMap;
825}
826
Chris Lattner0c0cb712009-08-08 20:22:20 +0000827
Chris Lattner11e96572009-08-03 21:53:27 +0000828const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000829getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000830 // Create the map if it doesn't already exist.
831 if (UniquingMap == 0)
832 UniquingMap = new MachOUniqueMapTy();
833 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
834
835 // Do the lookup, if we have a hit, return it.
836 const MCSectionCOFF *&Entry = Map[Name];
837 if (Entry) return Entry;
838
839 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000840}
841
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000842void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
843 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000844 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000845 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
846 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000847 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000848 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000849 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000850 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000851
852
853 // Debug info.
854 // FIXME: Don't use 'directive' mode here.
855 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000856 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
857 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000858 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000859 getCOFFSection("\t.section\t.debug_info,\"dr\"",
860 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000861 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000862 getCOFFSection("\t.section\t.debug_line,\"dr\"",
863 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000864 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000865 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
866 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000867 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000868 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
869 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000870 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000871 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
872 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000873 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000874 getCOFFSection("\t.section\t.debug_str,\"dr\"",
875 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000876 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000877 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
878 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000879 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000880 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
881 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000882 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000883 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
884 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000885 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000886 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
887 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000888}
889
Chris Lattner24f654c2009-08-06 16:39:58 +0000890const MCSection *TargetLoweringObjectFileCOFF::
891getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
892 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +0000893 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000894}
895
Chris Lattnerf0144122009-07-28 03:13:23 +0000896static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
897 if (Kind.isText())
898 return ".text$linkonce";
899 if (Kind.isWriteable())
900 return ".data$linkonce";
901 return ".rdata$linkonce";
902}
903
904
Chris Lattnera87dea42009-07-31 18:48:30 +0000905const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000906SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000907 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000908 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000909
910 // If this global is linkonce/weak and the target handles this by emitting it
911 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000912 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000913 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000914 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000915 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000916 }
917
Chris Lattnerf9650c02009-08-01 21:46:23 +0000918 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000919 return getTextSection();
920
Chris Lattnerf0144122009-07-28 03:13:23 +0000921 return getDataSection();
922}
923