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/MCSymbol.h"
23 #include "llvm/Target/Mangler.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/ADT/SmallString.h"
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
37 TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
42 StaticCtorSection = 0;
43 StaticDtorSection = 0;
47 DwarfAbbrevSection = 0;
50 DwarfFrameSection = 0;
51 DwarfPubNamesSection = 0;
52 DwarfPubTypesSection = 0;
53 DwarfDebugInlineSection = 0;
56 DwarfARangesSection = 0;
57 DwarfRangesSection = 0;
58 DwarfMacroInfoSection = 0;
60 IsFunctionEHSymbolGlobal = false;
61 IsFunctionEHFrameSymbolPrivate = true;
62 SupportsWeakOmittedEHFrame = true;
65 TargetLoweringObjectFile::~TargetLoweringObjectFile() {
68 static bool isSuitableForBSS(const GlobalVariable *GV) {
69 Constant *C = GV->getInitializer();
71 // Must have zero initializer.
72 if (!C->isNullValue())
75 // Leave constant zeros in readonly constant sections, so they can be shared.
79 // If the global has an explicit section specified, don't put it in BSS.
80 if (!GV->getSection().empty())
83 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
87 // Otherwise, put it in BSS!
91 /// IsNullTerminatedString - Return true if the specified constant (which is
92 /// known to have a type that is an array of 1/2/4 byte elements) ends with a
93 /// nul value and contains no other nuls in it.
94 static bool IsNullTerminatedString(const Constant *C) {
95 const ArrayType *ATy = cast<ArrayType>(C->getType());
97 // First check: is we have constant array of i8 terminated with zero
98 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
99 if (ATy->getNumElements() == 0) return false;
102 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
103 if (Null == 0 || Null->getZExtValue() != 0)
104 return false; // Not null terminated.
106 // Verify that the null doesn't occur anywhere else in the string.
107 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
108 // Reject constantexpr elements etc.
109 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
110 CVA->getOperand(i) == Null)
115 // Another possibility: [1 x i8] zeroinitializer
116 if (isa<ConstantAggregateZero>(C))
117 return ATy->getNumElements() == 1;
122 /// getKindForGlobal - This is a top-level target-independent classifier for
123 /// a global variable. Given an global variable and information from TM, it
124 /// classifies the global in a variety of ways that make various target
125 /// implementations simpler. The target implementation is free to ignore this
126 /// extra info of course.
127 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
128 const TargetMachine &TM){
129 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
130 "Can only be used for global definitions");
132 Reloc::Model ReloModel = TM.getRelocationModel();
134 // Early exit - functions should be always in text sections.
135 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
137 return SectionKind::getText();
139 // Handle thread-local data first.
140 if (GVar->isThreadLocal()) {
141 if (isSuitableForBSS(GVar))
142 return SectionKind::getThreadBSS();
143 return SectionKind::getThreadData();
146 // Variables with common linkage always get classified as common.
147 if (GVar->hasCommonLinkage())
148 return SectionKind::getCommon();
150 // Variable can be easily put to BSS section.
151 if (isSuitableForBSS(GVar)) {
152 if (GVar->hasLocalLinkage())
153 return SectionKind::getBSSLocal();
154 else if (GVar->hasExternalLinkage())
155 return SectionKind::getBSSExtern();
156 return SectionKind::getBSS();
159 Constant *C = GVar->getInitializer();
161 // If the global is marked constant, we can put it into a mergable section,
162 // a mergable string section, or general .data if it contains relocations.
163 if (GVar->isConstant()) {
164 // If the initializer for the global contains something that requires a
165 // relocation, then we may have to drop this into a wriable data section
166 // even though it is marked const.
167 switch (C->getRelocationInfo()) {
168 default: assert(0 && "unknown relocation info kind");
169 case Constant::NoRelocation:
170 // If initializer is a null-terminated string, put it in a "cstring"
171 // section of the right width.
172 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
173 if (const IntegerType *ITy =
174 dyn_cast<IntegerType>(ATy->getElementType())) {
175 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
176 ITy->getBitWidth() == 32) &&
177 IsNullTerminatedString(C)) {
178 if (ITy->getBitWidth() == 8)
179 return SectionKind::getMergeable1ByteCString();
180 if (ITy->getBitWidth() == 16)
181 return SectionKind::getMergeable2ByteCString();
183 assert(ITy->getBitWidth() == 32 && "Unknown width");
184 return SectionKind::getMergeable4ByteCString();
189 // Otherwise, just drop it into a mergable constant section. If we have
190 // a section for this size, use it, otherwise use the arbitrary sized
192 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
193 case 4: return SectionKind::getMergeableConst4();
194 case 8: return SectionKind::getMergeableConst8();
195 case 16: return SectionKind::getMergeableConst16();
196 default: return SectionKind::getMergeableConst();
199 case Constant::LocalRelocation:
200 // In static relocation model, the linker will resolve all addresses, so
201 // the relocation entries will actually be constants by the time the app
202 // starts up. However, we can't put this into a mergable section, because
203 // the linker doesn't take relocations into consideration when it tries to
204 // merge entries in the section.
205 if (ReloModel == Reloc::Static)
206 return SectionKind::getReadOnly();
208 // Otherwise, the dynamic linker needs to fix it up, put it in the
209 // writable data.rel.local section.
210 return SectionKind::getReadOnlyWithRelLocal();
212 case Constant::GlobalRelocations:
213 // In static relocation model, the linker will resolve all addresses, so
214 // the relocation entries will actually be constants by the time the app
215 // starts up. However, we can't put this into a mergable section, because
216 // the linker doesn't take relocations into consideration when it tries to
217 // merge entries in the section.
218 if (ReloModel == Reloc::Static)
219 return SectionKind::getReadOnly();
221 // Otherwise, the dynamic linker needs to fix it up, put it in the
222 // writable data.rel section.
223 return SectionKind::getReadOnlyWithRel();
227 // Okay, this isn't a constant. If the initializer for the global is going
228 // to require a runtime relocation by the dynamic linker, put it into a more
229 // specific section to improve startup time of the app. This coalesces these
230 // globals together onto fewer pages, improving the locality of the dynamic
232 if (ReloModel == Reloc::Static)
233 return SectionKind::getDataNoRel();
235 switch (C->getRelocationInfo()) {
236 default: assert(0 && "unknown relocation info kind");
237 case Constant::NoRelocation:
238 return SectionKind::getDataNoRel();
239 case Constant::LocalRelocation:
240 return SectionKind::getDataRelLocal();
241 case Constant::GlobalRelocations:
242 return SectionKind::getDataRel();
246 /// SectionForGlobal - This method computes the appropriate section to emit
247 /// the specified global variable or function definition. This should not
248 /// be passed external (or available externally) globals.
249 const MCSection *TargetLoweringObjectFile::
250 SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
251 const TargetMachine &TM) const {
252 // Select section name.
253 if (GV->hasSection())
254 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
257 // Use default section depending on the 'type' of global
258 return SelectSectionForGlobal(GV, Kind, Mang, TM);
262 // Lame default implementation. Calculate the section name for global.
264 TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
267 const TargetMachine &TM) const{
268 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
271 return getTextSection();
273 if (Kind.isBSS() && BSSSection != 0)
276 if (Kind.isReadOnly() && ReadOnlySection != 0)
277 return ReadOnlySection;
279 return getDataSection();
282 /// getSectionForConstant - Given a mergable constant with the
283 /// specified size and relocation information, return a section that it
284 /// should be placed in.
286 TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
287 if (Kind.isReadOnly() && ReadOnlySection != 0)
288 return ReadOnlySection;
293 /// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
294 /// reference to the specified global variable from exception
295 /// handling information.
296 const MCExpr *TargetLoweringObjectFile::
297 getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
298 MachineModuleInfo *MMI, unsigned Encoding) const {
299 // FIXME: Use GetGlobalValueSymbol.
300 SmallString<128> Name;
301 Mang->getNameWithPrefix(Name, GV, false);
304 if (GV->hasPrivateLinkage())
305 Sym = getContext().GetOrCreateTemporarySymbol(Name.str());
307 Sym = getContext().GetOrCreateSymbol(Name.str());
309 return getSymbolForDwarfReference(Sym, MMI, Encoding);
312 const MCExpr *TargetLoweringObjectFile::
313 getSymbolForDwarfReference(const MCSymbol *Sym, MachineModuleInfo *MMI,
314 unsigned Encoding) const {
315 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
317 switch (Encoding & 0xF0) {
319 llvm_report_error("We do not support this DWARF encoding yet!");
321 case dwarf::DW_EH_PE_absptr:
322 // Do nothing special
324 case dwarf::DW_EH_PE_pcrel:
326 const MCExpr *PC = MCSymbolRefExpr::Create(".", getContext());
327 Res = MCBinaryExpr::CreateSub(Res, PC, getContext());
334 unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
335 return dwarf::DW_EH_PE_absptr;
338 unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
339 return dwarf::DW_EH_PE_absptr;
342 unsigned TargetLoweringObjectFile::getFDEEncoding() const {
343 return dwarf::DW_EH_PE_absptr;
346 unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
347 return dwarf::DW_EH_PE_absptr;