blob: eabd62637275b0e2126bb834b3a30561eebb7f47 [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;
287 return MCSection::Create(Name, isDirective, Kind, getContext());
288}
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}
407
408
409
Chris Lattnerf0144122009-07-28 03:13:23 +0000410void TargetLoweringObjectFileELF::
Chris Lattner5277b222009-08-08 20:43:12 +0000411getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str,
412 const TargetAsmInfo &TAI) const {
413 // Handle the weird solaris syntax if desired.
414 if (TAI.usesSunStyleELFSectionSwitchSyntax() &&
415 !Kind.isMergeableConst() && !Kind.isMergeableCString()) {
416 // FIXME: Inefficient.
417 std::string Res;
418 if (!Kind.isMetadata())
419 Res += ",#alloc";
420 if (Kind.isText())
421 Res += ",#execinstr";
422 if (Kind.isWriteable())
423 Res += ",#write";
424 if (Kind.isThreadLocal())
425 Res += ",#tls";
426 Str.append(Res.begin(), Res.end());
427 return;
428 }
429
Chris Lattnerf0144122009-07-28 03:13:23 +0000430 Str.push_back(',');
431 Str.push_back('"');
432
433 if (!Kind.isMetadata())
434 Str.push_back('a');
435 if (Kind.isText())
436 Str.push_back('x');
437 if (Kind.isWriteable())
438 Str.push_back('w');
Chris Lattner3b24c012009-08-04 05:35:56 +0000439 if (Kind.isMergeable1ByteCString() ||
440 Kind.isMergeable2ByteCString() ||
441 Kind.isMergeable4ByteCString() ||
Chris Lattner82987bf2009-07-31 16:17:13 +0000442 Kind.isMergeableConst4() ||
443 Kind.isMergeableConst8() ||
444 Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000445 Str.push_back('M');
Chris Lattner3b24c012009-08-04 05:35:56 +0000446 if (Kind.isMergeable1ByteCString() ||
447 Kind.isMergeable2ByteCString() ||
448 Kind.isMergeable4ByteCString())
Chris Lattnerf0144122009-07-28 03:13:23 +0000449 Str.push_back('S');
450 if (Kind.isThreadLocal())
451 Str.push_back('T');
452
453 Str.push_back('"');
454 Str.push_back(',');
455
456 // If comment string is '@', e.g. as on ARM - use '%' instead
457 if (AtIsCommentChar)
458 Str.push_back('%');
459 else
460 Str.push_back('@');
461
462 const char *KindStr;
Chris Lattnerbf15e432009-07-28 17:57:51 +0000463 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattnerf0144122009-07-28 03:13:23 +0000464 KindStr = "nobits";
465 else
466 KindStr = "progbits";
467
468 Str.append(KindStr, KindStr+strlen(KindStr));
469
Chris Lattner3b24c012009-08-04 05:35:56 +0000470 if (Kind.isMergeable1ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000471 Str.push_back(',');
472 Str.push_back('1');
Chris Lattner3b24c012009-08-04 05:35:56 +0000473 } else if (Kind.isMergeable2ByteCString()) {
474 Str.push_back(',');
475 Str.push_back('2');
476 } else if (Kind.isMergeable4ByteCString()) {
477 Str.push_back(',');
478 Str.push_back('4');
Chris Lattnerf0144122009-07-28 03:13:23 +0000479 } else if (Kind.isMergeableConst4()) {
480 Str.push_back(',');
481 Str.push_back('4');
482 } else if (Kind.isMergeableConst8()) {
483 Str.push_back(',');
484 Str.push_back('8');
485 } else if (Kind.isMergeableConst16()) {
486 Str.push_back(',');
487 Str.push_back('1');
488 Str.push_back('6');
489 }
490}
491
492
493static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
494 if (Kind.isText()) return ".gnu.linkonce.t.";
495 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
496
497 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
498 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
499
500 if (Kind.isBSS()) return ".gnu.linkonce.b.";
501 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
502 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
503 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
504 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
505
506 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
507 return ".gnu.linkonce.d.rel.ro.";
508}
509
Chris Lattnera87dea42009-07-31 18:48:30 +0000510const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000511SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000512 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000513
514 // If this global is linkonce/weak and the target handles this by emitting it
515 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000516 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000517 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattnerb8f396b2009-07-29 05:20:33 +0000518 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000519 return getELFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000520 }
521
Chris Lattnerf9650c02009-08-01 21:46:23 +0000522 if (Kind.isText()) return TextSection;
Chris Lattnere53a6002009-07-29 05:09:30 +0000523
Chris Lattner3b24c012009-08-04 05:35:56 +0000524 if (Kind.isMergeable1ByteCString() ||
525 Kind.isMergeable2ByteCString() ||
526 Kind.isMergeable4ByteCString()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000527
Chris Lattner067fe1a2009-07-29 04:54:38 +0000528 // We also need alignment here.
529 // FIXME: this is getting the alignment of the character, not the
530 // alignment of the global!
531 unsigned Align =
532 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Chris Lattnerf0144122009-07-28 03:13:23 +0000533
Chris Lattner7e88a502009-08-04 16:19:50 +0000534 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000535 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000536 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000537 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000538 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000539 else
540 assert(Kind.isMergeable1ByteCString() && "unknown string width");
541
542
Chris Lattner7e88a502009-08-04 16:19:50 +0000543 std::string Name = SizeSpec + utostr(Align);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000544 return getELFSection(Name.c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000545 }
546
Chris Lattnerf9650c02009-08-01 21:46:23 +0000547 if (Kind.isMergeableConst()) {
548 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000549 return MergeableConst4Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000550 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000551 return MergeableConst8Section;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000552 if (Kind.isMergeableConst16())
Chris Lattnerf0144122009-07-28 03:13:23 +0000553 return MergeableConst16Section;
554 return ReadOnlySection; // .const
555 }
556
Chris Lattnerf9650c02009-08-01 21:46:23 +0000557 if (Kind.isReadOnly()) return ReadOnlySection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000558
Chris Lattnerf9650c02009-08-01 21:46:23 +0000559 if (Kind.isThreadData()) return TLSDataSection;
560 if (Kind.isThreadBSS()) return TLSBSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000561
Chris Lattner82458382009-08-01 21:56:13 +0000562 if (Kind.isBSS()) return BSSSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000563
Chris Lattnerf9650c02009-08-01 21:46:23 +0000564 if (Kind.isDataNoRel()) return DataSection;
565 if (Kind.isDataRelLocal()) return DataRelLocalSection;
566 if (Kind.isDataRel()) return DataRelSection;
567 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000568
Chris Lattnerf9650c02009-08-01 21:46:23 +0000569 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000570 return DataRelROSection;
571}
572
Chris Lattner83d77fa2009-08-01 23:46:12 +0000573/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000574/// specified size and relocation information, return a section that it
575/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000576const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000577getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000578 if (Kind.isMergeableConst4())
579 return MergeableConst4Section;
580 if (Kind.isMergeableConst8())
581 return MergeableConst8Section;
582 if (Kind.isMergeableConst16())
583 return MergeableConst16Section;
584 if (Kind.isReadOnly())
585 return ReadOnlySection;
586
587 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
588 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
589 return DataRelROSection;
590}
591
592//===----------------------------------------------------------------------===//
593// MachO
594//===----------------------------------------------------------------------===//
595
Chris Lattner0c0cb712009-08-08 20:22:20 +0000596
Chris Lattner11e96572009-08-03 21:53:27 +0000597const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000598getMachOSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000599 if (MCSection *S = getContext().GetSection(Name))
600 return S;
601 return MCSection::Create(Name, isDirective, Kind, getContext());
602}
603
Chris Lattner11e96572009-08-03 21:53:27 +0000604
605
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000606void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
607 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000608 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000609 TextSection = getMachOSection("\t.text", true, SectionKind::getText());
610 DataSection = getMachOSection("\t.data", true, SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +0000611
Chris Lattner0c0cb712009-08-08 20:22:20 +0000612 CStringSection = getMachOSection("\t.cstring", true,
613 SectionKind::getMergeable1ByteCString());
614 UStringSection = getMachOSection("__TEXT,__ustring", false,
615 SectionKind::getMergeable2ByteCString());
616 FourByteConstantSection = getMachOSection("\t.literal4\n", true,
617 SectionKind::getMergeableConst4());
618 EightByteConstantSection = getMachOSection("\t.literal8\n", true,
Chris Lattnerec409752009-08-04 16:27:13 +0000619 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000620
621 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
622 // to using it in -static mode.
623 if (TM.getRelocationModel() != Reloc::Static &&
624 TM.getTargetData()->getPointerSize() == 32)
625 SixteenByteConstantSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000626 getMachOSection("\t.literal16\n", true,
627 SectionKind::getMergeableConst16());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000628 else
629 SixteenByteConstantSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +0000630
Chris Lattner0c0cb712009-08-08 20:22:20 +0000631 ReadOnlySection = getMachOSection("\t.const", true,
632 SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000633
634 TextCoalSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000635 getMachOSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
636 false, SectionKind::getText());
637 ConstTextCoalSection = getMachOSection("\t__TEXT,__const_coal,coalesced",
638 false, SectionKind::getText());
639 ConstDataCoalSection = getMachOSection("\t__DATA,__const_coal,coalesced",
640 false, SectionKind::getText());
641 ConstDataSection = getMachOSection("\t.const_data", true,
642 SectionKind::getReadOnlyWithRel());
643 DataCoalSection = getMachOSection("\t__DATA,__datacoal_nt,coalesced",
644 false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000645
646 if (TM.getRelocationModel() == Reloc::Static) {
647 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000648 getMachOSection(".constructor", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000649 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000650 getMachOSection(".destructor", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000651 } else {
652 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000653 getMachOSection(".mod_init_func", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000654 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000655 getMachOSection(".mod_term_func", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000656 }
657
Chris Lattner18a4c162009-08-02 07:24:22 +0000658 // Exception Handling.
Chris Lattner0c0cb712009-08-08 20:22:20 +0000659 LSDASection = getMachOSection("__DATA,__gcc_except_tab", false,
660 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000661 EHFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000662 getMachOSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
663 "+live_support", false, SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000664
665 // Debug Information.
666 // FIXME: Don't use 'directive' syntax: need flags for debug/regular??
667 // FIXME: Need __DWARF segment.
668 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000669 getMachOSection(".section __DWARF,__debug_abbrev,regular,debug", true,
670 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000671 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000672 getMachOSection(".section __DWARF,__debug_info,regular,debug", true,
673 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000674 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000675 getMachOSection(".section __DWARF,__debug_line,regular,debug", true,
676 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000677 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000678 getMachOSection(".section __DWARF,__debug_frame,regular,debug", true,
679 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000680 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000681 getMachOSection(".section __DWARF,__debug_pubnames,regular,debug", true,
682 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000683 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000684 getMachOSection(".section __DWARF,__debug_pubtypes,regular,debug", true,
685 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000686 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000687 getMachOSection(".section __DWARF,__debug_str,regular,debug", true,
688 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000689 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000690 getMachOSection(".section __DWARF,__debug_loc,regular,debug", true,
691 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000692 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000693 getMachOSection(".section __DWARF,__debug_aranges,regular,debug", true,
694 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000695 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000696 getMachOSection(".section __DWARF,__debug_ranges,regular,debug", true,
697 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000698 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000699 getMachOSection(".section __DWARF,__debug_macinfo,regular,debug", true,
700 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000701 DwarfDebugInlineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000702 getMachOSection(".section __DWARF,__debug_inlined,regular,debug", true,
703 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000704}
705
Chris Lattnera87dea42009-07-31 18:48:30 +0000706const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner24f654c2009-08-06 16:39:58 +0000707getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
708 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +0000709 return getMachOSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000710}
711
712const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000713SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000714 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000715 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000716
Chris Lattnerf9650c02009-08-01 21:46:23 +0000717 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000718 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000719
720 // If this is weak/linkonce, put this in a coalescable section, either in text
721 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000722 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000723 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000724 return ConstTextCoalSection;
725 return DataCoalSection;
726 }
727
728 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000729 if (Kind.isMergeable1ByteCString() ||
730 Kind.isMergeable2ByteCString()) {
731 if (TM.getTargetData()->getPreferredAlignment(
732 cast<GlobalVariable>(GV)) < 32) {
733 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000734 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000735 assert(Kind.isMergeable2ByteCString());
736 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000737 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000738 }
739
Chris Lattnerf9650c02009-08-01 21:46:23 +0000740 if (Kind.isMergeableConst()) {
741 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000742 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000743 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000744 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000745 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000746 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000747 }
Chris Lattnerec409752009-08-04 16:27:13 +0000748
749 // Otherwise, if it is readonly, but not something we can specially optimize,
750 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000751 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000752 return ReadOnlySection;
753
754 // If this is marked const, put it into a const section. But if the dynamic
755 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000756 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000757 return ConstDataSection;
758
759 // Otherwise, just drop the variable in the normal data section.
760 return DataSection;
761}
762
Chris Lattnera87dea42009-07-31 18:48:30 +0000763const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000764TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000765 // If this constant requires a relocation, we have to put it in the data
766 // segment, not in the text segment.
767 if (Kind.isDataRel())
768 return ConstDataSection;
769
770 if (Kind.isMergeableConst4())
771 return FourByteConstantSection;
772 if (Kind.isMergeableConst8())
773 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000774 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000775 return SixteenByteConstantSection;
776 return ReadOnlySection; // .const
777}
778
Chris Lattner26630c12009-07-31 20:52:39 +0000779/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
780/// not to emit the UsedDirective for some symbols in llvm.used.
781// FIXME: REMOVE this (rdar://7071300)
782bool TargetLoweringObjectFileMachO::
783shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
784 /// On Darwin, internally linked data beginning with "L" or "l" does not have
785 /// the directive emitted (this occurs in ObjC metadata).
786 if (!GV) return false;
787
788 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
789 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
790 // FIXME: ObjC metadata is currently emitted as internal symbols that have
791 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
792 // this horrible hack can go away.
793 const std::string &Name = Mang->getMangledName(GV);
794 if (Name[0] == 'L' || Name[0] == 'l')
795 return false;
796 }
797
798 return true;
799}
800
801
Chris Lattnerf0144122009-07-28 03:13:23 +0000802//===----------------------------------------------------------------------===//
803// COFF
804//===----------------------------------------------------------------------===//
805
Chris Lattner0c0cb712009-08-08 20:22:20 +0000806
Chris Lattner11e96572009-08-03 21:53:27 +0000807const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000808getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000809 if (MCSection *S = getContext().GetSection(Name))
810 return S;
811 return MCSection::Create(Name, isDirective, Kind, getContext());
812}
813
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000814void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
815 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000816 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000817 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
818 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000819 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000820 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000821 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000822 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000823
824
825 // Debug info.
826 // FIXME: Don't use 'directive' mode here.
827 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000828 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
829 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000830 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000831 getCOFFSection("\t.section\t.debug_info,\"dr\"",
832 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000833 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000834 getCOFFSection("\t.section\t.debug_line,\"dr\"",
835 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000836 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000837 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
838 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000839 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000840 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
841 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000842 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000843 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
844 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000845 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000846 getCOFFSection("\t.section\t.debug_str,\"dr\"",
847 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000848 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000849 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
850 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000851 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000852 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
853 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000854 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000855 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
856 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000857 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000858 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
859 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000860}
861
Chris Lattner24f654c2009-08-06 16:39:58 +0000862const MCSection *TargetLoweringObjectFileCOFF::
863getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
864 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +0000865 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000866}
867
868
Chris Lattnerf0144122009-07-28 03:13:23 +0000869void TargetLoweringObjectFileCOFF::
Chris Lattner5277b222009-08-08 20:43:12 +0000870getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str,
871 const TargetAsmInfo &TAI) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000872 // FIXME: Inefficient.
873 std::string Res = ",\"";
874 if (Kind.isText())
875 Res += 'x';
876 if (Kind.isWriteable())
877 Res += 'w';
878 Res += "\"";
879
880 Str.append(Res.begin(), Res.end());
881}
882
883static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
884 if (Kind.isText())
885 return ".text$linkonce";
886 if (Kind.isWriteable())
887 return ".data$linkonce";
888 return ".rdata$linkonce";
889}
890
891
Chris Lattnera87dea42009-07-31 18:48:30 +0000892const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000893SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000894 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000895 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000896
897 // If this global is linkonce/weak and the target handles this by emitting it
898 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000899 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000900 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000901 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000902 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000903 }
904
Chris Lattnerf9650c02009-08-01 21:46:23 +0000905 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000906 return getTextSection();
907
Chris Lattnerf0144122009-07-28 03:13:23 +0000908 return getDataSection();
909}
910