]> git.proxmox.com Git - rustc.git/blame - src/llvm/lib/Target/TargetLoweringObjectFile.cpp
Imported Upstream version 1.0.0~0alpha
[rustc.git] / src / llvm / lib / Target / TargetLoweringObjectFile.cpp
CommitLineData
223e47cc
LB
1//===-- 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"
970d7e83
LB
16#include "llvm/IR/Constants.h"
17#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalVariable.h"
1a4d82fc
JJ
21#include "llvm/IR/Mangler.h"
22#include "llvm/MC/MCAsmInfo.h"
223e47cc
LB
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
25#include "llvm/MC/MCStreamer.h"
26#include "llvm/MC/MCSymbol.h"
223e47cc
LB
27#include "llvm/Support/Dwarf.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
1a4d82fc 30#include "llvm/Target/TargetLowering.h"
970d7e83
LB
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Target/TargetOptions.h"
1a4d82fc 33#include "llvm/Target/TargetSubtargetInfo.h"
223e47cc
LB
34using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// Generic Code
38//===----------------------------------------------------------------------===//
39
40/// Initialize - this method must be called before any actual lowering is
41/// done. This specifies the current context for codegen, and gives the
42/// lowering implementations a chance to set up their default sections.
43void TargetLoweringObjectFile::Initialize(MCContext &ctx,
44 const TargetMachine &TM) {
45 Ctx = &ctx;
1a4d82fc 46 DL = TM.getSubtargetImpl()->getDataLayout();
223e47cc
LB
47 InitMCObjectFileInfo(TM.getTargetTriple(),
48 TM.getRelocationModel(), TM.getCodeModel(), *Ctx);
49}
1a4d82fc 50
223e47cc
LB
51TargetLoweringObjectFile::~TargetLoweringObjectFile() {
52}
53
54static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) {
55 const Constant *C = GV->getInitializer();
56
57 // Must have zero initializer.
58 if (!C->isNullValue())
59 return false;
60
61 // Leave constant zeros in readonly constant sections, so they can be shared.
62 if (GV->isConstant())
63 return false;
64
65 // If the global has an explicit section specified, don't put it in BSS.
1a4d82fc 66 if (GV->hasSection())
223e47cc
LB
67 return false;
68
69 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
70 if (NoZerosInBSS)
71 return false;
72
73 // Otherwise, put it in BSS!
74 return true;
75}
76
77/// IsNullTerminatedString - Return true if the specified constant (which is
78/// known to have a type that is an array of 1/2/4 byte elements) ends with a
79/// nul value and contains no other nuls in it. Note that this is more general
80/// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
81static bool IsNullTerminatedString(const Constant *C) {
82 // First check: is we have constant array terminated with zero
83 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
84 unsigned NumElts = CDS->getNumElements();
85 assert(NumElts != 0 && "Can't have an empty CDS");
86
87 if (CDS->getElementAsInteger(NumElts-1) != 0)
88 return false; // Not null terminated.
89
90 // Verify that the null doesn't occur anywhere else in the string.
91 for (unsigned i = 0; i != NumElts-1; ++i)
92 if (CDS->getElementAsInteger(i) == 0)
93 return false;
94 return true;
95 }
96
97 // Another possibility: [1 x i8] zeroinitializer
98 if (isa<ConstantAggregateZero>(C))
99 return cast<ArrayType>(C->getType())->getNumElements() == 1;
100
101 return false;
102}
103
1a4d82fc
JJ
104MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase(
105 const GlobalValue *GV, StringRef Suffix, Mangler &Mang,
106 const TargetMachine &TM) const {
107 assert(!Suffix.empty());
108
109 SmallString<60> NameStr;
110 NameStr += DL->getPrivateGlobalPrefix();
111 TM.getNameWithPrefix(NameStr, GV, Mang);
112 NameStr.append(Suffix.begin(), Suffix.end());
113 return Ctx->GetOrCreateSymbol(NameStr.str());
114}
115
116MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol(
117 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
118 MachineModuleInfo *MMI) const {
119 return TM.getSymbol(GV, Mang);
223e47cc
LB
120}
121
122void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
123 const TargetMachine &TM,
124 const MCSymbol *Sym) const {
125}
126
127
128/// getKindForGlobal - This is a top-level target-independent classifier for
129/// a global variable. Given an global variable and information from TM, it
130/// classifies the global in a variety of ways that make various target
131/// implementations simpler. The target implementation is free to ignore this
132/// extra info of course.
133SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
134 const TargetMachine &TM){
135 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
136 "Can only be used for global definitions");
137
138 Reloc::Model ReloModel = TM.getRelocationModel();
139
140 // Early exit - functions should be always in text sections.
141 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1a4d82fc 142 if (!GVar)
223e47cc
LB
143 return SectionKind::getText();
144
145 // Handle thread-local data first.
146 if (GVar->isThreadLocal()) {
147 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS))
148 return SectionKind::getThreadBSS();
149 return SectionKind::getThreadData();
150 }
151
152 // Variables with common linkage always get classified as common.
153 if (GVar->hasCommonLinkage())
154 return SectionKind::getCommon();
155
156 // Variable can be easily put to BSS section.
157 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) {
158 if (GVar->hasLocalLinkage())
159 return SectionKind::getBSSLocal();
160 else if (GVar->hasExternalLinkage())
161 return SectionKind::getBSSExtern();
162 return SectionKind::getBSS();
163 }
164
165 const Constant *C = GVar->getInitializer();
166
167 // If the global is marked constant, we can put it into a mergable section,
168 // a mergable string section, or general .data if it contains relocations.
169 if (GVar->isConstant()) {
170 // If the initializer for the global contains something that requires a
171 // relocation, then we may have to drop this into a writable data section
172 // even though it is marked const.
173 switch (C->getRelocationInfo()) {
174 case Constant::NoRelocation:
175 // If the global is required to have a unique address, it can't be put
176 // into a mergable section: just drop it into the general read-only
177 // section instead.
178 if (!GVar->hasUnnamedAddr())
179 return SectionKind::getReadOnly();
180
181 // If initializer is a null-terminated string, put it in a "cstring"
182 // section of the right width.
183 if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
184 if (IntegerType *ITy =
185 dyn_cast<IntegerType>(ATy->getElementType())) {
186 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
187 ITy->getBitWidth() == 32) &&
188 IsNullTerminatedString(C)) {
189 if (ITy->getBitWidth() == 8)
190 return SectionKind::getMergeable1ByteCString();
191 if (ITy->getBitWidth() == 16)
192 return SectionKind::getMergeable2ByteCString();
193
194 assert(ITy->getBitWidth() == 32 && "Unknown width");
195 return SectionKind::getMergeable4ByteCString();
196 }
197 }
198 }
199
200 // Otherwise, just drop it into a mergable constant section. If we have
201 // a section for this size, use it, otherwise use the arbitrary sized
202 // mergable section.
1a4d82fc
JJ
203 switch (TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(
204 C->getType())) {
223e47cc
LB
205 case 4: return SectionKind::getMergeableConst4();
206 case 8: return SectionKind::getMergeableConst8();
207 case 16: return SectionKind::getMergeableConst16();
208 default: return SectionKind::getMergeableConst();
209 }
210
211 case Constant::LocalRelocation:
212 // In static relocation model, the linker will resolve all addresses, so
213 // the relocation entries will actually be constants by the time the app
214 // starts up. However, we can't put this into a mergable section, because
215 // the linker doesn't take relocations into consideration when it tries to
216 // merge entries in the section.
217 if (ReloModel == Reloc::Static)
218 return SectionKind::getReadOnly();
219
220 // Otherwise, the dynamic linker needs to fix it up, put it in the
221 // writable data.rel.local section.
222 return SectionKind::getReadOnlyWithRelLocal();
223
224 case Constant::GlobalRelocations:
225 // In static relocation model, the linker will resolve all addresses, so
226 // the relocation entries will actually be constants by the time the app
227 // starts up. However, we can't put this into a mergable section, because
228 // the linker doesn't take relocations into consideration when it tries to
229 // merge entries in the section.
230 if (ReloModel == Reloc::Static)
231 return SectionKind::getReadOnly();
232
233 // Otherwise, the dynamic linker needs to fix it up, put it in the
234 // writable data.rel section.
235 return SectionKind::getReadOnlyWithRel();
236 }
237 }
238
239 // Okay, this isn't a constant. If the initializer for the global is going
240 // to require a runtime relocation by the dynamic linker, put it into a more
241 // specific section to improve startup time of the app. This coalesces these
242 // globals together onto fewer pages, improving the locality of the dynamic
243 // linker.
244 if (ReloModel == Reloc::Static)
245 return SectionKind::getDataNoRel();
246
247 switch (C->getRelocationInfo()) {
248 case Constant::NoRelocation:
249 return SectionKind::getDataNoRel();
250 case Constant::LocalRelocation:
251 return SectionKind::getDataRelLocal();
252 case Constant::GlobalRelocations:
253 return SectionKind::getDataRel();
254 }
255 llvm_unreachable("Invalid relocation");
256}
257
258/// SectionForGlobal - This method computes the appropriate section to emit
259/// the specified global variable or function definition. This should not
260/// be passed external (or available externally) globals.
261const MCSection *TargetLoweringObjectFile::
1a4d82fc 262SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
223e47cc
LB
263 const TargetMachine &TM) const {
264 // Select section name.
265 if (GV->hasSection())
266 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
267
268
269 // Use default section depending on the 'type' of global
270 return SelectSectionForGlobal(GV, Kind, Mang, TM);
271}
272
1a4d82fc
JJ
273bool TargetLoweringObjectFile::isSectionAtomizableBySymbols(
274 const MCSection &Section) const {
275 return false;
276}
223e47cc
LB
277
278// Lame default implementation. Calculate the section name for global.
279const MCSection *
280TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
281 SectionKind Kind,
1a4d82fc 282 Mangler &Mang,
223e47cc
LB
283 const TargetMachine &TM) const{
284 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
285
286 if (Kind.isText())
287 return getTextSection();
288
1a4d82fc 289 if (Kind.isBSS() && BSSSection != nullptr)
223e47cc
LB
290 return BSSSection;
291
1a4d82fc 292 if (Kind.isReadOnly() && ReadOnlySection != nullptr)
223e47cc
LB
293 return ReadOnlySection;
294
295 return getDataSection();
296}
297
298/// getSectionForConstant - Given a mergable constant with the
299/// specified size and relocation information, return a section that it
300/// should be placed in.
301const MCSection *
1a4d82fc
JJ
302TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind,
303 const Constant *C) const {
304 if (Kind.isReadOnly() && ReadOnlySection != nullptr)
223e47cc
LB
305 return ReadOnlySection;
306
307 return DataSection;
308}
309
970d7e83 310/// getTTypeGlobalReference - Return an MCExpr to use for a
223e47cc
LB
311/// reference to the specified global variable from exception
312/// handling information.
1a4d82fc
JJ
313const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference(
314 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
315 const TargetMachine &TM, MachineModuleInfo *MMI,
316 MCStreamer &Streamer) const {
970d7e83 317 const MCSymbolRefExpr *Ref =
1a4d82fc 318 MCSymbolRefExpr::Create(TM.getSymbol(GV, Mang), getContext());
970d7e83
LB
319
320 return getTTypeReference(Ref, Encoding, Streamer);
223e47cc
LB
321}
322
323const MCExpr *TargetLoweringObjectFile::
970d7e83
LB
324getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
325 MCStreamer &Streamer) const {
223e47cc
LB
326 switch (Encoding & 0x70) {
327 default:
328 report_fatal_error("We do not support this DWARF encoding yet!");
329 case dwarf::DW_EH_PE_absptr:
330 // Do nothing special
970d7e83 331 return Sym;
223e47cc
LB
332 case dwarf::DW_EH_PE_pcrel: {
333 // Emit a label to the streamer for the current position. This gives us
334 // .-foo addressing.
335 MCSymbol *PCSym = getContext().CreateTempSymbol();
336 Streamer.EmitLabel(PCSym);
337 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
970d7e83 338 return MCBinaryExpr::CreateSub(Sym, PC, getContext());
223e47cc
LB
339 }
340 }
341}
1a4d82fc
JJ
342
343const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
344 // FIXME: It's not clear what, if any, default this should have - perhaps a
345 // null return could mean 'no location' & we should just do that here.
346 return MCSymbolRefExpr::Create(Sym, *Ctx);
347}