blob: c209a7b320af97148360890495cfcb1223647589 [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"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.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 Lattnerff4bc462009-08-10 01:39:42 +0000513getMachOSection(StringRef Segment, StringRef Section,
514 unsigned TypeAndAttributes,
515 unsigned Reserved2, SectionKind Kind) const {
516 // FIXME: UNIQUE HERE.
517 //if (MCSection *S = getContext().GetSection(Name))
518 // return S;
519
520 return MCSectionMachO::Create(Segment, Section, TypeAndAttributes, Reserved2,
521 Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000522}
523
Chris Lattner11e96572009-08-03 21:53:27 +0000524
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000525void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
526 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000527 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000528
Chris Lattnerff4bc462009-08-10 01:39:42 +0000529 TextSection // .text
530 = getMachOSection("__TEXT", "__text",
531 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
532 SectionKind::getText());
533 DataSection // .data
534 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
535
536 CStringSection // .cstring
537 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
538 SectionKind::getMergeable1ByteCString());
539 UStringSection
540 = getMachOSection("__TEXT","__ustring", 0,
541 SectionKind::getMergeable2ByteCString());
542 FourByteConstantSection // .literal4
543 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
544 SectionKind::getMergeableConst4());
545 EightByteConstantSection // .literal8
546 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
547 SectionKind::getMergeableConst8());
Chris Lattner4bb253c2009-07-28 17:50:28 +0000548
549 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
550 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000551 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000552 if (TM.getRelocationModel() != Reloc::Static &&
553 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000554 SixteenByteConstantSection = // .literal16
555 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000556 SectionKind::getMergeableConst16());
Chris Lattnerf0144122009-07-28 03:13:23 +0000557
Chris Lattnerff4bc462009-08-10 01:39:42 +0000558 ReadOnlySection // .const
559 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000560
Chris Lattnerff4bc462009-08-10 01:39:42 +0000561 TextCoalSection
562 = getMachOSection("__TEXT", "__textcoal_nt",
563 MCSectionMachO::S_COALESCED |
564 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
565 SectionKind::getText());
566 ConstTextCoalSection
567 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
568 SectionKind::getText());
569 ConstDataCoalSection
570 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
571 SectionKind::getText());
572 ConstDataSection // .const_data
573 = getMachOSection("__DATA", "__const", 0,
574 SectionKind::getReadOnlyWithRel());
575 DataCoalSection
576 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
577 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000578
579 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000580 StaticCtorSection
581 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
582 StaticDtorSection
583 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000584 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000585 StaticCtorSection
586 = getMachOSection("__DATA", "__mod_init_func",
587 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
588 SectionKind::getDataRel());
589 StaticDtorSection
590 = getMachOSection("__DATA", "__mod_term_func",
591 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
592 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000593 }
594
Chris Lattner18a4c162009-08-02 07:24:22 +0000595 // Exception Handling.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000596 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000597 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000598 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000599 getMachOSection("__TEXT", "__eh_frame",
600 MCSectionMachO::S_COALESCED |
601 MCSectionMachO::S_ATTR_NO_TOC |
602 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
603 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
604 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000605
606 // Debug Information.
Chris Lattner18a4c162009-08-02 07:24:22 +0000607 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000608 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000609 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000610 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000611 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000612 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000613 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000614 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000615 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000616 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000617 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000618 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000619 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000620 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000621 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000622 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000623 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000624 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000625 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000626 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000627 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000628 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000629 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000630 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000631 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000632 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000633 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000634 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000635 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000636 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000637 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000638 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000639 SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000640 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000641 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000642 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000643}
644
Chris Lattnerff4bc462009-08-10 01:39:42 +0000645/// getLazySymbolPointerSection - Return the section corresponding to
646/// the .lazy_symbol_pointer directive.
647const MCSection *TargetLoweringObjectFileMachO::
648getLazySymbolPointerSection() const {
649 return getMachOSection("__DATA", "__la_symbol_ptr",
650 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
651 SectionKind::getMetadata());
652}
653
654/// getNonLazySymbolPointerSection - Return the section corresponding to
655/// the .non_lazy_symbol_pointer directive.
656const MCSection *TargetLoweringObjectFileMachO::
657getNonLazySymbolPointerSection() const {
658 return getMachOSection("__DATA", "__nl_symbol_ptr",
659 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
660 SectionKind::getMetadata());
661}
662
663
Chris Lattnera87dea42009-07-31 18:48:30 +0000664const MCSection *TargetLoweringObjectFileMachO::
Chris Lattner24f654c2009-08-06 16:39:58 +0000665getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
666 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000667 // Parse the section specifier and create it if valid.
668 StringRef Segment, Section;
669 unsigned TAA, StubSize;
670 std::string ErrorCode =
671 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
672 TAA, StubSize);
673 if (ErrorCode.empty())
674 return getMachOSection(Segment, Section, TAA, StubSize, Kind);
675
676
677 // If invalid, report the error with llvm_report_error.
678 llvm_report_error("Global variable '" + GV->getNameStr() +
679 "' has an invalid section specifier '" + GV->getSection() +
680 "': " + ErrorCode + ".");
681 // Fall back to dropping it into the data section.
682 return DataSection;
Chris Lattner24f654c2009-08-06 16:39:58 +0000683}
684
685const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000686SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000687 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000688 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000689
Chris Lattnerf9650c02009-08-01 21:46:23 +0000690 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000691 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000692
693 // If this is weak/linkonce, put this in a coalescable section, either in text
694 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000695 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000696 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000697 return ConstTextCoalSection;
698 return DataCoalSection;
699 }
700
701 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000702 if (Kind.isMergeable1ByteCString() ||
703 Kind.isMergeable2ByteCString()) {
704 if (TM.getTargetData()->getPreferredAlignment(
705 cast<GlobalVariable>(GV)) < 32) {
706 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000707 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000708 assert(Kind.isMergeable2ByteCString());
709 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000710 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000711 }
712
Chris Lattnerf9650c02009-08-01 21:46:23 +0000713 if (Kind.isMergeableConst()) {
714 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000715 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000716 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000717 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000718 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000719 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000720 }
Chris Lattnerec409752009-08-04 16:27:13 +0000721
722 // Otherwise, if it is readonly, but not something we can specially optimize,
723 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000724 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000725 return ReadOnlySection;
726
727 // If this is marked const, put it into a const section. But if the dynamic
728 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000729 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000730 return ConstDataSection;
731
732 // Otherwise, just drop the variable in the normal data section.
733 return DataSection;
734}
735
Chris Lattnera87dea42009-07-31 18:48:30 +0000736const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000737TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000738 // If this constant requires a relocation, we have to put it in the data
739 // segment, not in the text segment.
740 if (Kind.isDataRel())
741 return ConstDataSection;
742
743 if (Kind.isMergeableConst4())
744 return FourByteConstantSection;
745 if (Kind.isMergeableConst8())
746 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000747 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000748 return SixteenByteConstantSection;
749 return ReadOnlySection; // .const
750}
751
Chris Lattner26630c12009-07-31 20:52:39 +0000752/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
753/// not to emit the UsedDirective for some symbols in llvm.used.
754// FIXME: REMOVE this (rdar://7071300)
755bool TargetLoweringObjectFileMachO::
756shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
757 /// On Darwin, internally linked data beginning with "L" or "l" does not have
758 /// the directive emitted (this occurs in ObjC metadata).
759 if (!GV) return false;
760
761 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
762 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
763 // FIXME: ObjC metadata is currently emitted as internal symbols that have
764 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
765 // this horrible hack can go away.
766 const std::string &Name = Mang->getMangledName(GV);
767 if (Name[0] == 'L' || Name[0] == 'l')
768 return false;
769 }
770
771 return true;
772}
773
774
Chris Lattnerf0144122009-07-28 03:13:23 +0000775//===----------------------------------------------------------------------===//
776// COFF
777//===----------------------------------------------------------------------===//
778
Chris Lattner0c0cb712009-08-08 20:22:20 +0000779
Chris Lattner11e96572009-08-03 21:53:27 +0000780const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner0c0cb712009-08-08 20:22:20 +0000781getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000782 if (MCSection *S = getContext().GetSection(Name))
783 return S;
Chris Lattner7c599d02009-08-08 20:52:13 +0000784 return MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000785}
786
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000787void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
788 const TargetMachine &TM) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000789 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +0000790 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
791 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000792 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000793 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000794 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000795 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Chris Lattner18a4c162009-08-02 07:24:22 +0000796
797
798 // Debug info.
799 // FIXME: Don't use 'directive' mode here.
800 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000801 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
802 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000803 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000804 getCOFFSection("\t.section\t.debug_info,\"dr\"",
805 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000806 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000807 getCOFFSection("\t.section\t.debug_line,\"dr\"",
808 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000809 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000810 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
811 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000812 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000813 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
814 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000815 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000816 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
817 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000818 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000819 getCOFFSection("\t.section\t.debug_str,\"dr\"",
820 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000821 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000822 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
823 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000824 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000825 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
826 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000827 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000828 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
829 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +0000830 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +0000831 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
832 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000833}
834
Chris Lattner24f654c2009-08-06 16:39:58 +0000835const MCSection *TargetLoweringObjectFileCOFF::
836getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
837 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner0c0cb712009-08-08 20:22:20 +0000838 return getCOFFSection(GV->getSection().c_str(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +0000839}
840
Chris Lattnerf0144122009-07-28 03:13:23 +0000841static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
842 if (Kind.isText())
843 return ".text$linkonce";
844 if (Kind.isWriteable())
845 return ".data$linkonce";
846 return ".rdata$linkonce";
847}
848
849
Chris Lattnera87dea42009-07-31 18:48:30 +0000850const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000851SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000852 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000853 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Chris Lattnerf0144122009-07-28 03:13:23 +0000854
855 // If this global is linkonce/weak and the target handles this by emitting it
856 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +0000857 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000858 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner968ff112009-08-01 21:11:14 +0000859 std::string Name = Mang->makeNameProper(GV->getNameStr());
Chris Lattner0c0cb712009-08-08 20:22:20 +0000860 return getCOFFSection((Prefix+Name).c_str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000861 }
862
Chris Lattnerf9650c02009-08-01 21:46:23 +0000863 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000864 return getTextSection();
865
Chris Lattnerf0144122009-07-28 03:13:23 +0000866 return getDataSection();
867}
868