blob: e41e7a958c68b74d893d4b582ced4b10bd474064 [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"
18#include "llvm/GlobalVariable.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000019#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCSection.h"
Chris Lattner5277b222009-08-08 20:43:12 +000021#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000022#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000025#include "llvm/Support/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000026#include "llvm/ADT/StringExtras.h"
27using namespace llvm;
28
29//===----------------------------------------------------------------------===//
30// Generic Code
31//===----------------------------------------------------------------------===//
32
Chris Lattnera87dea42009-07-31 18:48:30 +000033TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000034 TextSection = 0;
35 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000036 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000037 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000038 StaticCtorSection = 0;
39 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000040 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000041 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000042
43 DwarfAbbrevSection = 0;
44 DwarfInfoSection = 0;
45 DwarfLineSection = 0;
46 DwarfFrameSection = 0;
47 DwarfPubNamesSection = 0;
48 DwarfPubTypesSection = 0;
49 DwarfDebugInlineSection = 0;
50 DwarfStrSection = 0;
51 DwarfLocSection = 0;
52 DwarfARangesSection = 0;
53 DwarfRangesSection = 0;
54 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000055}
56
57TargetLoweringObjectFile::~TargetLoweringObjectFile() {
58}
59
60static bool isSuitableForBSS(const GlobalVariable *GV) {
61 Constant *C = GV->getInitializer();
62
63 // Must have zero initializer.
64 if (!C->isNullValue())
65 return false;
66
67 // Leave constant zeros in readonly constant sections, so they can be shared.
68 if (GV->isConstant())
69 return false;
70
71 // If the global has an explicit section specified, don't put it in BSS.
72 if (!GV->getSection().empty())
73 return false;
74
75 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
76 if (NoZerosInBSS)
77 return false;
78
79 // Otherwise, put it in BSS!
80 return true;
81}
82
Chris Lattner1850e5a2009-08-04 16:13:09 +000083/// IsNullTerminatedString - Return true if the specified constant (which is
84/// known to have a type that is an array of 1/2/4 byte elements) ends with a
85/// nul value and contains no other nuls in it.
86static bool IsNullTerminatedString(const Constant *C) {
87 const ArrayType *ATy = cast<ArrayType>(C->getType());
88
Chris Lattnerf0144122009-07-28 03:13:23 +000089 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000090 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
91 if (ATy->getNumElements() == 0) return false;
92
93 ConstantInt *Null =
94 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
95 if (Null == 0 || Null->getZExtValue() != 0)
96 return false; // Not null terminated.
97
98 // Verify that the null doesn't occur anywhere else in the string.
99 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
100 // Reject constantexpr elements etc.
101 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
102 CVA->getOperand(i) == Null)
103 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000104 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000105 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000106
107 // Another possibility: [1 x i8] zeroinitializer
108 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000109 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000110
111 return false;
112}
113
Chris Lattner58bed8f2009-08-05 04:25:40 +0000114/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000115/// a global variable. Given an global variable and information from TM, it
116/// classifies the global in a variety of ways that make various target
117/// implementations simpler. The target implementation is free to ignore this
118/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000119SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
120 const TargetMachine &TM){
121 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
122 "Can only be used for global definitions");
123
Chris Lattnerf0144122009-07-28 03:13:23 +0000124 Reloc::Model ReloModel = TM.getRelocationModel();
125
126 // Early exit - functions should be always in text sections.
127 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
128 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000129 return SectionKind::getText();
Chris Lattnerf0144122009-07-28 03:13:23 +0000130
131
132 // Handle thread-local data first.
133 if (GVar->isThreadLocal()) {
134 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000135 return SectionKind::getThreadBSS();
136 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000137 }
138
139 // Variable can be easily put to BSS section.
140 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000141 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000142
143 Constant *C = GVar->getInitializer();
144
145 // If the global is marked constant, we can put it into a mergable section,
146 // a mergable string section, or general .data if it contains relocations.
147 if (GVar->isConstant()) {
148 // If the initializer for the global contains something that requires a
149 // relocation, then we may have to drop this into a wriable data section
150 // even though it is marked const.
151 switch (C->getRelocationInfo()) {
152 default: llvm_unreachable("unknown relocation info kind");
153 case Constant::NoRelocation:
154 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000155 // section of the right width.
156 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
157 if (const IntegerType *ITy =
158 dyn_cast<IntegerType>(ATy->getElementType())) {
159 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
160 ITy->getBitWidth() == 32) &&
161 IsNullTerminatedString(C)) {
162 if (ITy->getBitWidth() == 8)
163 return SectionKind::getMergeable1ByteCString();
164 if (ITy->getBitWidth() == 16)
165 return SectionKind::getMergeable2ByteCString();
166
167 assert(ITy->getBitWidth() == 32 && "Unknown width");
168 return SectionKind::getMergeable4ByteCString();
169 }
170 }
171 }
Chris Lattner3b24c012009-08-04 05:35:56 +0000172
Chris Lattnerf0144122009-07-28 03:13:23 +0000173 // Otherwise, just drop it into a mergable constant section. If we have
174 // a section for this size, use it, otherwise use the arbitrary sized
175 // mergable section.
176 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000177 case 4: return SectionKind::getMergeableConst4();
178 case 8: return SectionKind::getMergeableConst8();
179 case 16: return SectionKind::getMergeableConst16();
180 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000181 }
182
183 case Constant::LocalRelocation:
184 // In static relocation model, the linker will resolve all addresses, so
185 // the relocation entries will actually be constants by the time the app
186 // starts up. However, we can't put this into a mergable section, because
187 // the linker doesn't take relocations into consideration when it tries to
188 // merge entries in the section.
189 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000190 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000191
192 // Otherwise, the dynamic linker needs to fix it up, put it in the
193 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000194 return SectionKind::getReadOnlyWithRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000195
196 case Constant::GlobalRelocations:
197 // In static relocation model, the linker will resolve all addresses, so
198 // the relocation entries will actually be constants by the time the app
199 // starts up. However, we can't put this into a mergable section, because
200 // the linker doesn't take relocations into consideration when it tries to
201 // merge entries in the section.
202 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000203 return SectionKind::getReadOnly();
Chris Lattnerf0144122009-07-28 03:13:23 +0000204
205 // Otherwise, the dynamic linker needs to fix it up, put it in the
206 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000207 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000208 }
209 }
210
211 // Okay, this isn't a constant. If the initializer for the global is going
212 // to require a runtime relocation by the dynamic linker, put it into a more
213 // specific section to improve startup time of the app. This coalesces these
214 // globals together onto fewer pages, improving the locality of the dynamic
215 // linker.
216 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000217 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000218
219 switch (C->getRelocationInfo()) {
220 default: llvm_unreachable("unknown relocation info kind");
221 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000222 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000223 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000224 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000225 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000226 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000227 }
228}
229
230/// SectionForGlobal - This method computes the appropriate section to emit
231/// the specified global variable or function definition. This should not
232/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000233const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000234SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000235 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000236 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000237 if (GV->hasSection())
238 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
239
Chris Lattnerf0144122009-07-28 03:13:23 +0000240
241 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000242 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000243}
244
Chris Lattner58bed8f2009-08-05 04:25:40 +0000245
Chris Lattnerf0144122009-07-28 03:13:23 +0000246// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000247const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000248TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000249 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000250 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000251 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000252 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000253
Chris Lattnerf9650c02009-08-01 21:46:23 +0000254 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000255 return getTextSection();
256
Chris Lattner82458382009-08-01 21:56:13 +0000257 if (Kind.isBSS() && BSSSection != 0)
258 return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000259
Chris Lattnerf9650c02009-08-01 21:46:23 +0000260 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000261 return ReadOnlySection;
262
263 return getDataSection();
264}
265
Chris Lattner83d77fa2009-08-01 23:46:12 +0000266/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000267/// specified size and relocation information, return a section that it
268/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000269const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000270TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000271 if (Kind.isReadOnly() && ReadOnlySection != 0)
272 return ReadOnlySection;
273
274 return DataSection;
275}
276
277
Chris Lattnerf0144122009-07-28 03:13:23 +0000278
279//===----------------------------------------------------------------------===//
280// ELF
281//===----------------------------------------------------------------------===//
282
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000283const MCSection *TargetLoweringObjectFileELF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000284getELFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000285 if (MCSection *S = getContext().GetSection(Name))
286 return S;
Chris Lattner7c599d02009-08-08 20:52:13 +0000287 return MCSectionELF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000288}
289
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000290void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
291 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000292 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000293 if (!HasCrazyBSS)
Chris Lattner0c0cb712009-08-08 20:22:20 +0000294 BSSSection = getELFSection("\t.bss", true, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000295 else
296 // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
297 // FIXME: Does .section .bss work everywhere??
Chris Lattner968ff112009-08-01 21:11:14 +0000298 // FIXME2: this should just be handle by the section printer. We should get
299 // away from syntactic view of the sections and MCSection should just be a
300 // semantic view.
Chris Lattner0c0cb712009-08-08 20:22:20 +0000301 BSSSection = getELFSection("\t.bss", false, SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000302
303
Chris Lattner0c0cb712009-08-08 20:22:20 +0000304 TextSection = getELFSection("\t.text", true, SectionKind::getText());
305 DataSection = getELFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000306 ReadOnlySection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000307 getELFSection("\t.rodata", false, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000308 TLSDataSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000309 getELFSection("\t.tdata", false, SectionKind::getThreadData());
Chris Lattner3b24c012009-08-04 05:35:56 +0000310
Chris Lattner0c0cb712009-08-08 20:22:20 +0000311 TLSBSSSection = getELFSection("\t.tbss", false,
Chris Lattner27981192009-08-01 23:57:16 +0000312 SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000313
Chris Lattner0c0cb712009-08-08 20:22:20 +0000314 DataRelSection = getELFSection("\t.data.rel", false,
Chris Lattner27981192009-08-01 23:57:16 +0000315 SectionKind::getDataRel());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000316 DataRelLocalSection = getELFSection("\t.data.rel.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000317 SectionKind::getDataRelLocal());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000318 DataRelROSection = getELFSection("\t.data.rel.ro", false,
Chris Lattner27981192009-08-01 23:57:16 +0000319 SectionKind::getReadOnlyWithRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000320 DataRelROLocalSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000321 getELFSection("\t.data.rel.ro.local", false,
Chris Lattner27981192009-08-01 23:57:16 +0000322 SectionKind::getReadOnlyWithRelLocal());
Chris Lattnerf0144122009-07-28 03:13:23 +0000323
Chris Lattner0c0cb712009-08-08 20:22:20 +0000324 MergeableConst4Section = getELFSection(".rodata.cst4", false,
Chris Lattner27981192009-08-01 23:57:16 +0000325 SectionKind::getMergeableConst4());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000326 MergeableConst8Section = getELFSection(".rodata.cst8", false,
Chris Lattner27981192009-08-01 23:57:16 +0000327 SectionKind::getMergeableConst8());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000328 MergeableConst16Section = getELFSection(".rodata.cst16", false,
Chris Lattner27981192009-08-01 23:57:16 +0000329 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000330
331 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000332 getELFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000333 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000334 getELFSection(".dtors", false, SectionKind::getDataRel());
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000335
Chris Lattner18a4c162009-08-02 07:24:22 +0000336 // Exception Handling Sections.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000337
338 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
339 // it contains relocatable pointers. In PIC mode, this is probably a big
340 // runtime hit for C++ apps. Either the contents of the LSDA need to be
341 // adjusted or this should be a data section.
342 LSDASection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000343 getELFSection(".gcc_except_table", false, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000344 EHFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000345 getELFSection(".eh_frame", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000346
347 // Debug Info Sections.
348 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000349 getELFSection(".debug_abbrev", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000350 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000351 getELFSection(".debug_info", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000352 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000353 getELFSection(".debug_line", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000354 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000355 getELFSection(".debug_frame", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000356 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000357 getELFSection(".debug_pubnames", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000358 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000359 getELFSection(".debug_pubtypes", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000360 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000361 getELFSection(".debug_str", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000362 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000363 getELFSection(".debug_loc", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000364 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000365 getELFSection(".debug_aranges", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000366 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000367 getELFSection(".debug_ranges", false, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000368 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000369 getELFSection(".debug_macinfo", false, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000370}
371
372
Chris Lattner24f654c2009-08-06 16:39:58 +0000373static SectionKind
374getELFKindForNamedSection(const char *Name, SectionKind K) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000375 if (Name[0] != '.') return K;
376
377 // Some lame default implementation based on some magic section names.
378 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
379 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
380 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
381 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000382 return SectionKind::getBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000383
384 if (strcmp(Name, ".tdata") == 0 ||
385 strncmp(Name, ".tdata.", 7) == 0 ||
386 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
387 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000388 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000389
390 if (strcmp(Name, ".tbss") == 0 ||
391 strncmp(Name, ".tbss.", 6) == 0 ||
392 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
393 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000394 return SectionKind::getThreadBSS();
Chris Lattnerf0144122009-07-28 03:13:23 +0000395
396 return K;
397}
398
Chris Lattner24f654c2009-08-06 16:39:58 +0000399const MCSection *TargetLoweringObjectFileELF::
400getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
401 Mangler *Mang, const TargetMachine &TM) const {
402 // Infer section flags from the section name if we can.
403 Kind = getELFKindForNamedSection(GV->getSection().c_str(), Kind);
404
Chris Lattner0c0cb712009-08-08 20:22:20 +0000405 return getELFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000406}
Chris Lattnerf0144122009-07-28 03:13:23 +0000407
408static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
409 if (Kind.isText()) return ".gnu.linkonce.t.";
410 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
411
412 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
413 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
414
415 if (Kind.isBSS()) return ".gnu.linkonce.b.";
416 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
417 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
418 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
419 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
420
421 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
422 return ".gnu.linkonce.d.rel.ro.";
423}
424
Chris Lattnera87dea42009-07-31 18:48:30 +0000425const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000426SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000427 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000428
429 // If this global is linkonce/weak and the target handles this by emitting it
430 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000431 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000432 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000433 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000434 return getELFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000435 }
436
Chris Lattnerf9650c02009-08-01 21:46:23 +0000437 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000438
Chris Lattner3b24c012009-08-04 05:35:56 +0000439 if (Kind.isMergeable1ByteCString() ||
440 Kind.isMergeable2ByteCString() ||
441 Kind.isMergeable4ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000442
Chris Lattner067fe1a2009-07-29 04:54:38 +0000443 // We also need alignment here.
444 // FIXME: this is getting the alignment of the character, not the
445 // alignment of the global!
446 unsigned Align =
447 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000448
Chris Lattner7e88a502009-08-04 16:19:50 +0000449 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000450 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000451 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000452 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000453 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000454 else
455 assert(Kind.isMergeable1ByteCString() && "unknown string width");
456
457
Chris Lattner7e88a502009-08-04 16:19:50 +0000458 std::string Name = SizeSpec + utostr(Align);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000459 return getELFSection(Name.c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000460 }
461
Chris Lattnerf9650c02009-08-01 21:46:23 +0000462 if (Kind.isMergeableConst()) {
463 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000464 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000465 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000466 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000467 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000468 return MergeableConst16Section;
469 return ReadOnlySection; // .const
470 }
471
Chris Lattnerf9650c02009-08-01 21:46:23 +0000472 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000473
Chris Lattnerf9650c02009-08-01 21:46:23 +0000474 if (Kind.isThreadData()) return TLSDataSection;
475 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000476
Chris Lattner82458382009-08-01 21:56:13 +0000477 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000478
Chris Lattnerf9650c02009-08-01 21:46:23 +0000479 if (Kind.isDataNoRel()) return DataSection;
480 if (Kind.isDataRelLocal()) return DataRelLocalSection;
481 if (Kind.isDataRel()) return DataRelSection;
482 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000483
Chris Lattnerf9650c02009-08-01 21:46:23 +0000484 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000485 return DataRelROSection;
486}
487
Chris Lattner83d77fa2009-08-01 23:46:12 +0000488/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000489/// specified size and relocation information, return a section that it
490/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000491const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000492getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000493 if (Kind.isMergeableConst4())
494 return MergeableConst4Section;
495 if (Kind.isMergeableConst8())
496 return MergeableConst8Section;
497 if (Kind.isMergeableConst16())
498 return MergeableConst16Section;
499 if (Kind.isReadOnly())
500 return ReadOnlySection;
501
502 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
503 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
504 return DataRelROSection;
505}
506
507//===----------------------------------------------------------------------===//
508// MachO
509//===----------------------------------------------------------------------===//
510
Chris Lattner0c0cb712009-08-08 20:22:20 +0000511
Chris Lattner11e96572009-08-03 21:53:27 +0000512const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000513getMachOSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000514 if (MCSection *S = getContext().GetSection(Name))
515 return S;
Chris Lattner7c599d02009-08-08 20:52:13 +0000516 return MCSectionMachO::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000517}
518
Chris Lattner11e96572009-08-03 21:53:27 +0000519
520
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000521void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
522 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000523 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000524 TextSection = getMachOSection("\t.text", true, SectionKind::getText());
525 DataSection = getMachOSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000526
Chris Lattner0c0cb712009-08-08 20:22:20 +0000527 CStringSection = getMachOSection("\t.cstring", true,
528 SectionKind::getMergeable1ByteCString());
529 UStringSection = getMachOSection("__TEXT,__ustring", false,
530 SectionKind::getMergeable2ByteCString());
531 FourByteConstantSection = getMachOSection("\t.literal4\n", true,
532 SectionKind::getMergeableConst4());
533 EightByteConstantSection = getMachOSection("\t.literal8\n", true,
Chris Lattnerec409752009-08-04 16:27:13 +0000534 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000535
536 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
537 // to using it in -static mode.
538 if (TM.getRelocationModel() != Reloc::Static &&
539 TM.getTargetData()->getPointerSize() == 32)
540 SixteenByteConstantSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000541 getMachOSection("\t.literal16\n", true,
542 SectionKind::getMergeableConst16());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000543 else
544 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000545
Chris Lattner0c0cb712009-08-08 20:22:20 +0000546 ReadOnlySection = getMachOSection("\t.const", true,
547 SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000548
549 TextCoalSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000550 getMachOSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
551 false, SectionKind::getText());
552 ConstTextCoalSection = getMachOSection("\t__TEXT,__const_coal,coalesced",
553 false, SectionKind::getText());
554 ConstDataCoalSection = getMachOSection("\t__DATA,__const_coal,coalesced",
555 false, SectionKind::getText());
556 ConstDataSection = getMachOSection("\t.const_data", true,
557 SectionKind::getReadOnlyWithRel());
558 DataCoalSection = getMachOSection("\t__DATA,__datacoal_nt,coalesced",
559 false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000560
561 if (TM.getRelocationModel() == Reloc::Static) {
562 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000563 getMachOSection(".constructor", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000564 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000565 getMachOSection(".destructor", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000566 } else {
567 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000568 getMachOSection(".mod_init_func", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000569 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000570 getMachOSection(".mod_term_func", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000571 }
572
Chris Lattner18a4c162009-08-02 07:24:22 +0000573 // Exception Handling.
Chris Lattner0c0cb712009-08-08 20:22:20 +0000574 LSDASection = getMachOSection("__DATA,__gcc_except_tab", false,
575 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000576 EHFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000577 getMachOSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
578 "+live_support", false, SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000579
580 // Debug Information.
581 // FIXME: Don't use 'directive' syntax: need flags for debug/regular??
582 // FIXME: Need __DWARF segment.
583 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000584 getMachOSection(".section __DWARF,__debug_abbrev,regular,debug", true,
585 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000586 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000587 getMachOSection(".section __DWARF,__debug_info,regular,debug", true,
588 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000589 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000590 getMachOSection(".section __DWARF,__debug_line,regular,debug", true,
591 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000592 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000593 getMachOSection(".section __DWARF,__debug_frame,regular,debug", true,
594 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000595 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000596 getMachOSection(".section __DWARF,__debug_pubnames,regular,debug", true,
597 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000598 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000599 getMachOSection(".section __DWARF,__debug_pubtypes,regular,debug", true,
600 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000601 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000602 getMachOSection(".section __DWARF,__debug_str,regular,debug", true,
603 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000604 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000605 getMachOSection(".section __DWARF,__debug_loc,regular,debug", true,
606 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000607 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000608 getMachOSection(".section __DWARF,__debug_aranges,regular,debug", true,
609 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000610 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000611 getMachOSection(".section __DWARF,__debug_ranges,regular,debug", true,
612 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000613 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000614 getMachOSection(".section __DWARF,__debug_macinfo,regular,debug", true,
615 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000616 DwarfDebugInlineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000617 getMachOSection(".section __DWARF,__debug_inlined,regular,debug", true,
618 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000619}
620
Chris Lattnera87dea42009-07-31 18:48:30 +0000621const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner24f654c2009-08-06 16:39:58 +0000622getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
623 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +0000624 return getMachOSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000625}
626
627const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000628SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000629 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000630 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000631
Chris Lattnerf9650c02009-08-01 21:46:23 +0000632 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000633 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000634
635 // If this is weak/linkonce, put this in a coalescable section, either in text
636 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000637 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000638 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000639 return ConstTextCoalSection;
640 return DataCoalSection;
641 }
642
643 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000644 if (Kind.isMergeable1ByteCString() ||
645 Kind.isMergeable2ByteCString()) {
646 if (TM.getTargetData()->getPreferredAlignment(
647 cast<GlobalVariable>(GV)) < 32) {
648 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000649 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000650 assert(Kind.isMergeable2ByteCString());
651 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000652 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000653 }
654
Chris Lattnerf9650c02009-08-01 21:46:23 +0000655 if (Kind.isMergeableConst()) {
656 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000657 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000658 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000659 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000660 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000661 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000662 }
Chris Lattnerec409752009-08-04 16:27:13 +0000663
664 // Otherwise, if it is readonly, but not something we can specially optimize,
665 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000666 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000667 return ReadOnlySection;
668
669 // If this is marked const, put it into a const section. But if the dynamic
670 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000671 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000672 return ConstDataSection;
673
674 // Otherwise, just drop the variable in the normal data section.
675 return DataSection;
676}
677
Chris Lattnera87dea42009-07-31 18:48:30 +0000678const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000679TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000680 // If this constant requires a relocation, we have to put it in the data
681 // segment, not in the text segment.
682 if (Kind.isDataRel())
683 return ConstDataSection;
684
685 if (Kind.isMergeableConst4())
686 return FourByteConstantSection;
687 if (Kind.isMergeableConst8())
688 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000689 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000690 return SixteenByteConstantSection;
691 return ReadOnlySection; // .const
692}
693
Chris Lattner26630c12009-07-31 20:52:39 +0000694/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
695/// not to emit the UsedDirective for some symbols in llvm.used.
696// FIXME: REMOVE this (rdar://7071300)
697bool TargetLoweringObjectFileMachO::
698shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
699 /// On Darwin, internally linked data beginning with "L" or "l" does not have
700 /// the directive emitted (this occurs in ObjC metadata).
701 if (!GV) return false;
702
703 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
704 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
705 // FIXME: ObjC metadata is currently emitted as internal symbols that have
706 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
707 // this horrible hack can go away.
708 const std::string &Name = Mang->getMangledName(GV);
709 if (Name[0] == 'L' || Name[0] == 'l')
710 return false;
711 }
712
713 return true;
714}
715
716
Chris Lattnerf0144122009-07-28 03:13:23 +0000717//===----------------------------------------------------------------------===//
718// COFF
719//===----------------------------------------------------------------------===//
720
Chris Lattner0c0cb712009-08-08 20:22:20 +0000721
Chris Lattner11e96572009-08-03 21:53:27 +0000722const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000723getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000724 if (MCSection *S = getContext().GetSection(Name))
725 return S;
Chris Lattner7c599d02009-08-08 20:52:13 +0000726 return MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000727}
728
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000729void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
730 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000731 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000732 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
733 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000734 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000735 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000736 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000737 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000738
739
740 // Debug info.
741 // FIXME: Don't use 'directive' mode here.
742 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000743 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
744 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000745 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000746 getCOFFSection("\t.section\t.debug_info,\"dr\"",
747 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000748 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000749 getCOFFSection("\t.section\t.debug_line,\"dr\"",
750 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000751 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000752 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
753 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000754 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000755 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
756 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000757 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000758 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
759 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000760 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000761 getCOFFSection("\t.section\t.debug_str,\"dr\"",
762 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000763 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000764 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
765 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000766 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000767 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
768 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000769 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000770 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
771 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000772 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000773 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
774 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000775}
776
Chris Lattner24f654c2009-08-06 16:39:58 +0000777const MCSection *TargetLoweringObjectFileCOFF::
778getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
779 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +0000780 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000781}
782
Chris Lattnerf0144122009-07-28 03:13:23 +0000783static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
784 if (Kind.isText())
785 return ".text$linkonce";
786 if (Kind.isWriteable())
787 return ".data$linkonce";
788 return ".rdata$linkonce";
789}
790
791
Chris Lattnera87dea42009-07-31 18:48:30 +0000792const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000793SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000794 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000795 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000796
797 // If this global is linkonce/weak and the target handles this by emitting it
798 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000799 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000800 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000801 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000802 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000803 }
804
Chris Lattnerf9650c02009-08-01 21:46:23 +0000805 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000806 return getTextSection();
807
Chris Lattnerf0144122009-07-28 03:13:23 +0000808 return getDataSection();
809}
810