1 //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements classes used to handle lowerings specific to common
11 // object file formats.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Target/TargetLoweringObjectFile.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Target/Mangler.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Support/Dwarf.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/ADT/SmallString.h"
34 //===----------------------------------------------------------------------===//
36 //===----------------------------------------------------------------------===//
38 TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
43 StaticCtorSection = 0;
44 StaticDtorSection = 0;
47 CommDirectiveSupportsAlignment = true;
48 DwarfAbbrevSection = 0;
51 DwarfFrameSection = 0;
52 DwarfPubNamesSection = 0;
53 DwarfPubTypesSection = 0;
54 DwarfDebugInlineSection = 0;
57 DwarfARangesSection = 0;
58 DwarfRangesSection = 0;
59 DwarfMacroInfoSection = 0;
61 IsFunctionEHSymbolGlobal = false;
62 IsFunctionEHFrameSymbolPrivate = true;
63 SupportsWeakOmittedEHFrame = true;
66 TargetLoweringObjectFile::~TargetLoweringObjectFile() {
69 static bool isSuitableForBSS(const GlobalVariable *GV) {
70 Constant *C = GV->getInitializer();
72 // Must have zero initializer.
73 if (!C->isNullValue())
76 // Leave constant zeros in readonly constant sections, so they can be shared.
80 // If the global has an explicit section specified, don't put it in BSS.
81 if (!GV->getSection().empty())
84 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
88 // Otherwise, put it in BSS!
92 /// IsNullTerminatedString - Return true if the specified constant (which is
93 /// known to have a type that is an array of 1/2/4 byte elements) ends with a
94 /// nul value and contains no other nuls in it.
95 static bool IsNullTerminatedString(const Constant *C) {
96 const ArrayType *ATy = cast<ArrayType>(C->getType());
98 // First check: is we have constant array of i8 terminated with zero
99 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
100 if (ATy->getNumElements() == 0) return false;
103 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
104 if (Null == 0 || !Null->isZero())
105 return false; // Not null terminated.
107 // Verify that the null doesn't occur anywhere else in the string.
108 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
109 // Reject constantexpr elements etc.
110 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
111 CVA->getOperand(i) == Null)
116 // Another possibility: [1 x i8] zeroinitializer
117 if (isa<ConstantAggregateZero>(C))
118 return ATy->getNumElements() == 1;
124 TargetLoweringObjectFile::getPersonalityPICSymbol(StringRef Name) const {
125 assert(0 && "Not Available in this format.");
129 void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
130 const TargetMachine &TM,
131 const MCSymbol *Sym) const {
132 assert(0 && "Not Available in this format.");
136 /// getKindForGlobal - This is a top-level target-independent classifier for
137 /// a global variable. Given an global variable and information from TM, it
138 /// classifies the global in a variety of ways that make various target
139 /// implementations simpler. The target implementation is free to ignore this
140 /// extra info of course.
141 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
142 const TargetMachine &TM){
143 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
144 "Can only be used for global definitions");
146 Reloc::Model ReloModel = TM.getRelocationModel();
148 // Early exit - functions should be always in text sections.
149 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
151 return SectionKind::getText();
153 // Handle thread-local data first.
154 if (GVar->isThreadLocal()) {
155 if (isSuitableForBSS(GVar))
156 return SectionKind::getThreadBSS();
157 return SectionKind::getThreadData();
160 // Variables with common linkage always get classified as common.
161 if (GVar->hasCommonLinkage())
162 return SectionKind::getCommon();
164 // Variable can be easily put to BSS section.
165 if (isSuitableForBSS(GVar)) {
166 if (GVar->hasLocalLinkage())
167 return SectionKind::getBSSLocal();
168 else if (GVar->hasExternalLinkage())
169 return SectionKind::getBSSExtern();
170 return SectionKind::getBSS();
173 Constant *C = GVar->getInitializer();
175 // If the global is marked constant, we can put it into a mergable section,
176 // a mergable string section, or general .data if it contains relocations.
177 if (GVar->isConstant()) {
178 // If the initializer for the global contains something that requires a
179 // relocation, then we may have to drop this into a wriable data section
180 // even though it is marked const.
181 switch (C->getRelocationInfo()) {
182 default: assert(0 && "unknown relocation info kind");
183 case Constant::NoRelocation:
184 // If the global is required to have a unique address, it can't be put
185 // into a mergable section: just drop it into the general read-only
187 if (!GVar->hasUnnamedAddr())
188 return SectionKind::getReadOnly();
190 // If initializer is a null-terminated string, put it in a "cstring"
191 // section of the right width.
192 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
193 if (const IntegerType *ITy =
194 dyn_cast<IntegerType>(ATy->getElementType())) {
195 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
196 ITy->getBitWidth() == 32) &&
197 IsNullTerminatedString(C)) {
198 if (ITy->getBitWidth() == 8)
199 return SectionKind::getMergeable1ByteCString();
200 if (ITy->getBitWidth() == 16)
201 return SectionKind::getMergeable2ByteCString();
203 assert(ITy->getBitWidth() == 32 && "Unknown width");
204 return SectionKind::getMergeable4ByteCString();
209 // Otherwise, just drop it into a mergable constant section. If we have
210 // a section for this size, use it, otherwise use the arbitrary sized
212 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
213 case 4: return SectionKind::getMergeableConst4();
214 case 8: return SectionKind::getMergeableConst8();
215 case 16: return SectionKind::getMergeableConst16();
216 default: return SectionKind::getMergeableConst();
219 case Constant::LocalRelocation:
220 // In static relocation model, the linker will resolve all addresses, so
221 // the relocation entries will actually be constants by the time the app
222 // starts up. However, we can't put this into a mergable section, because
223 // the linker doesn't take relocations into consideration when it tries to
224 // merge entries in the section.
225 if (ReloModel == Reloc::Static)
226 return SectionKind::getReadOnly();
228 // Otherwise, the dynamic linker needs to fix it up, put it in the
229 // writable data.rel.local section.
230 return SectionKind::getReadOnlyWithRelLocal();
232 case Constant::GlobalRelocations:
233 // In static relocation model, the linker will resolve all addresses, so
234 // the relocation entries will actually be constants by the time the app
235 // starts up. However, we can't put this into a mergable section, because
236 // the linker doesn't take relocations into consideration when it tries to
237 // merge entries in the section.
238 if (ReloModel == Reloc::Static)
239 return SectionKind::getReadOnly();
241 // Otherwise, the dynamic linker needs to fix it up, put it in the
242 // writable data.rel section.
243 return SectionKind::getReadOnlyWithRel();
247 // Okay, this isn't a constant. If the initializer for the global is going
248 // to require a runtime relocation by the dynamic linker, put it into a more
249 // specific section to improve startup time of the app. This coalesces these
250 // globals together onto fewer pages, improving the locality of the dynamic
252 if (ReloModel == Reloc::Static)
253 return SectionKind::getDataNoRel();
255 switch (C->getRelocationInfo()) {
256 default: assert(0 && "unknown relocation info kind");
257 case Constant::NoRelocation:
258 return SectionKind::getDataNoRel();
259 case Constant::LocalRelocation:
260 return SectionKind::getDataRelLocal();
261 case Constant::GlobalRelocations:
262 return SectionKind::getDataRel();
266 /// SectionForGlobal - This method computes the appropriate section to emit
267 /// the specified global variable or function definition. This should not
268 /// be passed external (or available externally) globals.
269 const MCSection *TargetLoweringObjectFile::
270 SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
271 const TargetMachine &TM) const {
272 // Select section name.
273 if (GV->hasSection())
274 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
277 // Use default section depending on the 'type' of global
278 return SelectSectionForGlobal(GV, Kind, Mang, TM);
282 // Lame default implementation. Calculate the section name for global.
284 TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
287 const TargetMachine &TM) const{
288 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
291 return getTextSection();
293 if (Kind.isBSS() && BSSSection != 0)
296 if (Kind.isReadOnly() && ReadOnlySection != 0)
297 return ReadOnlySection;
299 return getDataSection();
302 /// getSectionForConstant - Given a mergable constant with the
303 /// specified size and relocation information, return a section that it
304 /// should be placed in.
306 TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
307 if (Kind.isReadOnly() && ReadOnlySection != 0)
308 return ReadOnlySection;
313 /// getExprForDwarfGlobalReference - Return an MCExpr to use for a
314 /// reference to the specified global variable from exception
315 /// handling information.
316 const MCExpr *TargetLoweringObjectFile::
317 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
318 MachineModuleInfo *MMI, unsigned Encoding,
319 MCStreamer &Streamer) const {
320 const MCSymbol *Sym = Mang->getSymbol(GV);
321 return getExprForDwarfReference(Sym, Mang, MMI, Encoding, Streamer);
324 const MCExpr *TargetLoweringObjectFile::
325 getExprForDwarfReference(const MCSymbol *Sym, Mangler *Mang,
326 MachineModuleInfo *MMI, unsigned Encoding,
327 MCStreamer &Streamer) const {
328 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
330 switch (Encoding & 0x70) {
332 report_fatal_error("We do not support this DWARF encoding yet!");
333 case dwarf::DW_EH_PE_absptr:
334 // Do nothing special
336 case dwarf::DW_EH_PE_pcrel: {
337 // Emit a label to the streamer for the current position. This gives us
339 MCSymbol *PCSym = getContext().CreateTempSymbol();
340 Streamer.EmitLabel(PCSym);
341 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
342 return MCBinaryExpr::CreateSub(Res, PC, getContext());
347 unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
348 return dwarf::DW_EH_PE_absptr;
351 unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
352 return dwarf::DW_EH_PE_absptr;
355 unsigned TargetLoweringObjectFile::getFDEEncoding() const {
356 return dwarf::DW_EH_PE_absptr;
359 unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
360 return dwarf::DW_EH_PE_absptr;