1f22ef01cSRoman Divacky //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file implements classes used to handle lowerings specific to common
11f22ef01cSRoman Divacky // object file formats.
12f22ef01cSRoman Divacky //
13f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
14f22ef01cSRoman Divacky 
15f22ef01cSRoman Divacky #include "llvm/Target/TargetLoweringObjectFile.h"
16f22ef01cSRoman Divacky #include "llvm/Constants.h"
17f22ef01cSRoman Divacky #include "llvm/DerivedTypes.h"
18f22ef01cSRoman Divacky #include "llvm/Function.h"
19f22ef01cSRoman Divacky #include "llvm/GlobalVariable.h"
20f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h"
21f22ef01cSRoman Divacky #include "llvm/MC/MCExpr.h"
22f22ef01cSRoman Divacky #include "llvm/MC/MCStreamer.h"
23f22ef01cSRoman Divacky #include "llvm/MC/MCSymbol.h"
24f22ef01cSRoman Divacky #include "llvm/Target/Mangler.h"
253861d79fSDimitry Andric #include "llvm/DataLayout.h"
26f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h"
27f22ef01cSRoman Divacky #include "llvm/Target/TargetOptions.h"
28f22ef01cSRoman Divacky #include "llvm/Support/Dwarf.h"
29f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h"
30f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
31f22ef01cSRoman Divacky using namespace llvm;
32f22ef01cSRoman Divacky 
33f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
34f22ef01cSRoman Divacky //                              Generic Code
35f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
36f22ef01cSRoman Divacky 
376122f3e6SDimitry Andric /// Initialize - this method must be called before any actual lowering is
386122f3e6SDimitry Andric /// done.  This specifies the current context for codegen, and gives the
396122f3e6SDimitry Andric /// lowering implementations a chance to set up their default sections.
406122f3e6SDimitry Andric void TargetLoweringObjectFile::Initialize(MCContext &ctx,
416122f3e6SDimitry Andric                                           const TargetMachine &TM) {
426122f3e6SDimitry Andric   Ctx = &ctx;
436122f3e6SDimitry Andric   InitMCObjectFileInfo(TM.getTargetTriple(),
446122f3e6SDimitry Andric                        TM.getRelocationModel(), TM.getCodeModel(), *Ctx);
45f22ef01cSRoman Divacky }
46f22ef01cSRoman Divacky 
47f22ef01cSRoman Divacky TargetLoweringObjectFile::~TargetLoweringObjectFile() {
48f22ef01cSRoman Divacky }
49f22ef01cSRoman Divacky 
50dff0c46cSDimitry Andric static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) {
5117a519f9SDimitry Andric   const Constant *C = GV->getInitializer();
52f22ef01cSRoman Divacky 
53f22ef01cSRoman Divacky   // Must have zero initializer.
54f22ef01cSRoman Divacky   if (!C->isNullValue())
55f22ef01cSRoman Divacky     return false;
56f22ef01cSRoman Divacky 
57f22ef01cSRoman Divacky   // Leave constant zeros in readonly constant sections, so they can be shared.
58f22ef01cSRoman Divacky   if (GV->isConstant())
59f22ef01cSRoman Divacky     return false;
60f22ef01cSRoman Divacky 
61f22ef01cSRoman Divacky   // If the global has an explicit section specified, don't put it in BSS.
62f22ef01cSRoman Divacky   if (!GV->getSection().empty())
63f22ef01cSRoman Divacky     return false;
64f22ef01cSRoman Divacky 
65f22ef01cSRoman Divacky   // If -nozero-initialized-in-bss is specified, don't ever use BSS.
66f22ef01cSRoman Divacky   if (NoZerosInBSS)
67f22ef01cSRoman Divacky     return false;
68f22ef01cSRoman Divacky 
69f22ef01cSRoman Divacky   // Otherwise, put it in BSS!
70f22ef01cSRoman Divacky   return true;
71f22ef01cSRoman Divacky }
72f22ef01cSRoman Divacky 
73f22ef01cSRoman Divacky /// IsNullTerminatedString - Return true if the specified constant (which is
74f22ef01cSRoman Divacky /// known to have a type that is an array of 1/2/4 byte elements) ends with a
75dff0c46cSDimitry Andric /// nul value and contains no other nuls in it.  Note that this is more general
76dff0c46cSDimitry Andric /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
77f22ef01cSRoman Divacky static bool IsNullTerminatedString(const Constant *C) {
78dff0c46cSDimitry Andric   // First check: is we have constant array terminated with zero
79dff0c46cSDimitry Andric   if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
80dff0c46cSDimitry Andric     unsigned NumElts = CDS->getNumElements();
81dff0c46cSDimitry Andric     assert(NumElts != 0 && "Can't have an empty CDS");
82f22ef01cSRoman Divacky 
83dff0c46cSDimitry Andric     if (CDS->getElementAsInteger(NumElts-1) != 0)
84f22ef01cSRoman Divacky       return false; // Not null terminated.
85f22ef01cSRoman Divacky 
86f22ef01cSRoman Divacky     // Verify that the null doesn't occur anywhere else in the string.
87dff0c46cSDimitry Andric     for (unsigned i = 0; i != NumElts-1; ++i)
88dff0c46cSDimitry Andric       if (CDS->getElementAsInteger(i) == 0)
89f22ef01cSRoman Divacky         return false;
90f22ef01cSRoman Divacky     return true;
91f22ef01cSRoman Divacky   }
92f22ef01cSRoman Divacky 
93f22ef01cSRoman Divacky   // Another possibility: [1 x i8] zeroinitializer
94f22ef01cSRoman Divacky   if (isa<ConstantAggregateZero>(C))
95dff0c46cSDimitry Andric     return cast<ArrayType>(C->getType())->getNumElements() == 1;
96f22ef01cSRoman Divacky 
97f22ef01cSRoman Divacky   return false;
98f22ef01cSRoman Divacky }
99f22ef01cSRoman Divacky 
1003b0f4066SDimitry Andric MCSymbol *TargetLoweringObjectFile::
1013b0f4066SDimitry Andric getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
1023b0f4066SDimitry Andric                         MachineModuleInfo *MMI) const {
1033b0f4066SDimitry Andric   return Mang->getSymbol(GV);
1043b0f4066SDimitry Andric }
1053b0f4066SDimitry Andric 
1063b0f4066SDimitry Andric void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
1073b0f4066SDimitry Andric                                                     const TargetMachine &TM,
1083b0f4066SDimitry Andric                                                     const MCSymbol *Sym) const {
1093b0f4066SDimitry Andric }
1103b0f4066SDimitry Andric 
1113b0f4066SDimitry Andric 
112f22ef01cSRoman Divacky /// getKindForGlobal - This is a top-level target-independent classifier for
113f22ef01cSRoman Divacky /// a global variable.  Given an global variable and information from TM, it
114f22ef01cSRoman Divacky /// classifies the global in a variety of ways that make various target
115f22ef01cSRoman Divacky /// implementations simpler.  The target implementation is free to ignore this
116f22ef01cSRoman Divacky /// extra info of course.
117f22ef01cSRoman Divacky SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
118f22ef01cSRoman Divacky                                                        const TargetMachine &TM){
119f22ef01cSRoman Divacky   assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
120f22ef01cSRoman Divacky          "Can only be used for global definitions");
121f22ef01cSRoman Divacky 
122f22ef01cSRoman Divacky   Reloc::Model ReloModel = TM.getRelocationModel();
123f22ef01cSRoman Divacky 
124f22ef01cSRoman Divacky   // Early exit - functions should be always in text sections.
125f22ef01cSRoman Divacky   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
126f22ef01cSRoman Divacky   if (GVar == 0)
127f22ef01cSRoman Divacky     return SectionKind::getText();
128f22ef01cSRoman Divacky 
129f22ef01cSRoman Divacky   // Handle thread-local data first.
130f22ef01cSRoman Divacky   if (GVar->isThreadLocal()) {
131dff0c46cSDimitry Andric     if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS))
132f22ef01cSRoman Divacky       return SectionKind::getThreadBSS();
133f22ef01cSRoman Divacky     return SectionKind::getThreadData();
134f22ef01cSRoman Divacky   }
135f22ef01cSRoman Divacky 
136f22ef01cSRoman Divacky   // Variables with common linkage always get classified as common.
137f22ef01cSRoman Divacky   if (GVar->hasCommonLinkage())
138f22ef01cSRoman Divacky     return SectionKind::getCommon();
139f22ef01cSRoman Divacky 
140f22ef01cSRoman Divacky   // Variable can be easily put to BSS section.
141dff0c46cSDimitry Andric   if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) {
142f22ef01cSRoman Divacky     if (GVar->hasLocalLinkage())
143f22ef01cSRoman Divacky       return SectionKind::getBSSLocal();
144f22ef01cSRoman Divacky     else if (GVar->hasExternalLinkage())
145f22ef01cSRoman Divacky       return SectionKind::getBSSExtern();
146f22ef01cSRoman Divacky     return SectionKind::getBSS();
147f22ef01cSRoman Divacky   }
148f22ef01cSRoman Divacky 
14917a519f9SDimitry Andric   const Constant *C = GVar->getInitializer();
150f22ef01cSRoman Divacky 
151f22ef01cSRoman Divacky   // If the global is marked constant, we can put it into a mergable section,
152f22ef01cSRoman Divacky   // a mergable string section, or general .data if it contains relocations.
153f22ef01cSRoman Divacky   if (GVar->isConstant()) {
154f22ef01cSRoman Divacky     // If the initializer for the global contains something that requires a
1557ae0e2c9SDimitry Andric     // relocation, then we may have to drop this into a writable data section
156f22ef01cSRoman Divacky     // even though it is marked const.
157f22ef01cSRoman Divacky     switch (C->getRelocationInfo()) {
158f22ef01cSRoman Divacky     case Constant::NoRelocation:
1592754fe60SDimitry Andric       // If the global is required to have a unique address, it can't be put
1602754fe60SDimitry Andric       // into a mergable section: just drop it into the general read-only
1612754fe60SDimitry Andric       // section instead.
1622754fe60SDimitry Andric       if (!GVar->hasUnnamedAddr())
1632754fe60SDimitry Andric         return SectionKind::getReadOnly();
1642754fe60SDimitry Andric 
165f22ef01cSRoman Divacky       // If initializer is a null-terminated string, put it in a "cstring"
166f22ef01cSRoman Divacky       // section of the right width.
1676122f3e6SDimitry Andric       if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
1686122f3e6SDimitry Andric         if (IntegerType *ITy =
169f22ef01cSRoman Divacky               dyn_cast<IntegerType>(ATy->getElementType())) {
170f22ef01cSRoman Divacky           if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
171f22ef01cSRoman Divacky                ITy->getBitWidth() == 32) &&
172f22ef01cSRoman Divacky               IsNullTerminatedString(C)) {
173f22ef01cSRoman Divacky             if (ITy->getBitWidth() == 8)
174f22ef01cSRoman Divacky               return SectionKind::getMergeable1ByteCString();
175f22ef01cSRoman Divacky             if (ITy->getBitWidth() == 16)
176f22ef01cSRoman Divacky               return SectionKind::getMergeable2ByteCString();
177f22ef01cSRoman Divacky 
178f22ef01cSRoman Divacky             assert(ITy->getBitWidth() == 32 && "Unknown width");
179f22ef01cSRoman Divacky             return SectionKind::getMergeable4ByteCString();
180f22ef01cSRoman Divacky           }
181f22ef01cSRoman Divacky         }
182f22ef01cSRoman Divacky       }
183f22ef01cSRoman Divacky 
184f22ef01cSRoman Divacky       // Otherwise, just drop it into a mergable constant section.  If we have
185f22ef01cSRoman Divacky       // a section for this size, use it, otherwise use the arbitrary sized
186f22ef01cSRoman Divacky       // mergable section.
1873861d79fSDimitry Andric       switch (TM.getDataLayout()->getTypeAllocSize(C->getType())) {
188f22ef01cSRoman Divacky       case 4:  return SectionKind::getMergeableConst4();
189f22ef01cSRoman Divacky       case 8:  return SectionKind::getMergeableConst8();
190f22ef01cSRoman Divacky       case 16: return SectionKind::getMergeableConst16();
191f22ef01cSRoman Divacky       default: return SectionKind::getMergeableConst();
192f22ef01cSRoman Divacky       }
193f22ef01cSRoman Divacky 
194f22ef01cSRoman Divacky     case Constant::LocalRelocation:
195f22ef01cSRoman Divacky       // In static relocation model, the linker will resolve all addresses, so
196f22ef01cSRoman Divacky       // the relocation entries will actually be constants by the time the app
197f22ef01cSRoman Divacky       // starts up.  However, we can't put this into a mergable section, because
198f22ef01cSRoman Divacky       // the linker doesn't take relocations into consideration when it tries to
199f22ef01cSRoman Divacky       // merge entries in the section.
200f22ef01cSRoman Divacky       if (ReloModel == Reloc::Static)
201f22ef01cSRoman Divacky         return SectionKind::getReadOnly();
202f22ef01cSRoman Divacky 
203f22ef01cSRoman Divacky       // Otherwise, the dynamic linker needs to fix it up, put it in the
204f22ef01cSRoman Divacky       // writable data.rel.local section.
205f22ef01cSRoman Divacky       return SectionKind::getReadOnlyWithRelLocal();
206f22ef01cSRoman Divacky 
207f22ef01cSRoman Divacky     case Constant::GlobalRelocations:
208f22ef01cSRoman Divacky       // In static relocation model, the linker will resolve all addresses, so
209f22ef01cSRoman Divacky       // the relocation entries will actually be constants by the time the app
210f22ef01cSRoman Divacky       // starts up.  However, we can't put this into a mergable section, because
211f22ef01cSRoman Divacky       // the linker doesn't take relocations into consideration when it tries to
212f22ef01cSRoman Divacky       // merge entries in the section.
213f22ef01cSRoman Divacky       if (ReloModel == Reloc::Static)
214f22ef01cSRoman Divacky         return SectionKind::getReadOnly();
215f22ef01cSRoman Divacky 
216f22ef01cSRoman Divacky       // Otherwise, the dynamic linker needs to fix it up, put it in the
217f22ef01cSRoman Divacky       // writable data.rel section.
218f22ef01cSRoman Divacky       return SectionKind::getReadOnlyWithRel();
219f22ef01cSRoman Divacky     }
220f22ef01cSRoman Divacky   }
221f22ef01cSRoman Divacky 
222f22ef01cSRoman Divacky   // Okay, this isn't a constant.  If the initializer for the global is going
223f22ef01cSRoman Divacky   // to require a runtime relocation by the dynamic linker, put it into a more
224f22ef01cSRoman Divacky   // specific section to improve startup time of the app.  This coalesces these
225f22ef01cSRoman Divacky   // globals together onto fewer pages, improving the locality of the dynamic
226f22ef01cSRoman Divacky   // linker.
227f22ef01cSRoman Divacky   if (ReloModel == Reloc::Static)
228f22ef01cSRoman Divacky     return SectionKind::getDataNoRel();
229f22ef01cSRoman Divacky 
230f22ef01cSRoman Divacky   switch (C->getRelocationInfo()) {
231f22ef01cSRoman Divacky   case Constant::NoRelocation:
232f22ef01cSRoman Divacky     return SectionKind::getDataNoRel();
233f22ef01cSRoman Divacky   case Constant::LocalRelocation:
234f22ef01cSRoman Divacky     return SectionKind::getDataRelLocal();
235f22ef01cSRoman Divacky   case Constant::GlobalRelocations:
236f22ef01cSRoman Divacky     return SectionKind::getDataRel();
237f22ef01cSRoman Divacky   }
238dff0c46cSDimitry Andric   llvm_unreachable("Invalid relocation");
239f22ef01cSRoman Divacky }
240f22ef01cSRoman Divacky 
241f22ef01cSRoman Divacky /// SectionForGlobal - This method computes the appropriate section to emit
242f22ef01cSRoman Divacky /// the specified global variable or function definition.  This should not
243f22ef01cSRoman Divacky /// be passed external (or available externally) globals.
244f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFile::
245f22ef01cSRoman Divacky SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
246f22ef01cSRoman Divacky                  const TargetMachine &TM) const {
247f22ef01cSRoman Divacky   // Select section name.
248f22ef01cSRoman Divacky   if (GV->hasSection())
249f22ef01cSRoman Divacky     return getExplicitSectionGlobal(GV, Kind, Mang, TM);
250f22ef01cSRoman Divacky 
251f22ef01cSRoman Divacky 
252f22ef01cSRoman Divacky   // Use default section depending on the 'type' of global
253f22ef01cSRoman Divacky   return SelectSectionForGlobal(GV, Kind, Mang, TM);
254f22ef01cSRoman Divacky }
255f22ef01cSRoman Divacky 
256f22ef01cSRoman Divacky 
257f22ef01cSRoman Divacky // Lame default implementation. Calculate the section name for global.
258f22ef01cSRoman Divacky const MCSection *
259f22ef01cSRoman Divacky TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
260f22ef01cSRoman Divacky                                                  SectionKind Kind,
261f22ef01cSRoman Divacky                                                  Mangler *Mang,
262f22ef01cSRoman Divacky                                                  const TargetMachine &TM) const{
263f22ef01cSRoman Divacky   assert(!Kind.isThreadLocal() && "Doesn't support TLS");
264f22ef01cSRoman Divacky 
265f22ef01cSRoman Divacky   if (Kind.isText())
266f22ef01cSRoman Divacky     return getTextSection();
267f22ef01cSRoman Divacky 
268f22ef01cSRoman Divacky   if (Kind.isBSS() && BSSSection != 0)
269f22ef01cSRoman Divacky     return BSSSection;
270f22ef01cSRoman Divacky 
271f22ef01cSRoman Divacky   if (Kind.isReadOnly() && ReadOnlySection != 0)
272f22ef01cSRoman Divacky     return ReadOnlySection;
273f22ef01cSRoman Divacky 
274f22ef01cSRoman Divacky   return getDataSection();
275f22ef01cSRoman Divacky }
276f22ef01cSRoman Divacky 
277f22ef01cSRoman Divacky /// getSectionForConstant - Given a mergable constant with the
278f22ef01cSRoman Divacky /// specified size and relocation information, return a section that it
279f22ef01cSRoman Divacky /// should be placed in.
280f22ef01cSRoman Divacky const MCSection *
281f22ef01cSRoman Divacky TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
282f22ef01cSRoman Divacky   if (Kind.isReadOnly() && ReadOnlySection != 0)
283f22ef01cSRoman Divacky     return ReadOnlySection;
284f22ef01cSRoman Divacky 
285f22ef01cSRoman Divacky   return DataSection;
286f22ef01cSRoman Divacky }
287f22ef01cSRoman Divacky 
288f22ef01cSRoman Divacky /// getExprForDwarfGlobalReference - Return an MCExpr to use for a
289f22ef01cSRoman Divacky /// reference to the specified global variable from exception
290f22ef01cSRoman Divacky /// handling information.
291f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFile::
292f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
293f22ef01cSRoman Divacky                                MachineModuleInfo *MMI, unsigned Encoding,
294f22ef01cSRoman Divacky                                MCStreamer &Streamer) const {
295f22ef01cSRoman Divacky   const MCSymbol *Sym = Mang->getSymbol(GV);
2963b0f4066SDimitry Andric   return getExprForDwarfReference(Sym, Encoding, Streamer);
297f22ef01cSRoman Divacky }
298f22ef01cSRoman Divacky 
299f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFile::
3003b0f4066SDimitry Andric getExprForDwarfReference(const MCSymbol *Sym, unsigned Encoding,
301f22ef01cSRoman Divacky                          MCStreamer &Streamer) const {
302f22ef01cSRoman Divacky   const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
303f22ef01cSRoman Divacky 
3043b0f4066SDimitry Andric   switch (Encoding & 0x70) {
305f22ef01cSRoman Divacky   default:
306f22ef01cSRoman Divacky     report_fatal_error("We do not support this DWARF encoding yet!");
307f22ef01cSRoman Divacky   case dwarf::DW_EH_PE_absptr:
308f22ef01cSRoman Divacky     // Do nothing special
309f22ef01cSRoman Divacky     return Res;
310f22ef01cSRoman Divacky   case dwarf::DW_EH_PE_pcrel: {
311f22ef01cSRoman Divacky     // Emit a label to the streamer for the current position.  This gives us
312f22ef01cSRoman Divacky     // .-foo addressing.
313f22ef01cSRoman Divacky     MCSymbol *PCSym = getContext().CreateTempSymbol();
314f22ef01cSRoman Divacky     Streamer.EmitLabel(PCSym);
315f22ef01cSRoman Divacky     const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
316f22ef01cSRoman Divacky     return MCBinaryExpr::CreateSub(Res, PC, getContext());
317f22ef01cSRoman Divacky   }
318f22ef01cSRoman Divacky   }
319f22ef01cSRoman Divacky }
320