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"
16139f7f9bSDimitry Andric #include "llvm/IR/Constants.h"
17139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
18139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h"
19139f7f9bSDimitry Andric #include "llvm/IR/Function.h"
20139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h"
2191bc56edSDimitry Andric #include "llvm/IR/Mangler.h"
2291bc56edSDimitry Andric #include "llvm/MC/MCAsmInfo.h"
23f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h"
24f22ef01cSRoman Divacky #include "llvm/MC/MCExpr.h"
25f22ef01cSRoman Divacky #include "llvm/MC/MCStreamer.h"
26f22ef01cSRoman Divacky #include "llvm/MC/MCSymbol.h"
27f22ef01cSRoman Divacky #include "llvm/Support/Dwarf.h"
28f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h"
29f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
3091bc56edSDimitry Andric #include "llvm/Target/TargetLowering.h"
31139f7f9bSDimitry Andric #include "llvm/Target/TargetMachine.h"
32139f7f9bSDimitry Andric #include "llvm/Target/TargetOptions.h"
3339d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h"
34f22ef01cSRoman Divacky using namespace llvm;
35f22ef01cSRoman Divacky 
36f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
37f22ef01cSRoman Divacky //                              Generic Code
38f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
39f22ef01cSRoman Divacky 
406122f3e6SDimitry Andric /// Initialize - this method must be called before any actual lowering is
416122f3e6SDimitry Andric /// done.  This specifies the current context for codegen, and gives the
426122f3e6SDimitry Andric /// lowering implementations a chance to set up their default sections.
436122f3e6SDimitry Andric void TargetLoweringObjectFile::Initialize(MCContext &ctx,
446122f3e6SDimitry Andric                                           const TargetMachine &TM) {
456122f3e6SDimitry Andric   Ctx = &ctx;
463ca95b02SDimitry Andric   InitMCObjectFileInfo(TM.getTargetTriple(), TM.isPositionIndependent(),
478f0fd8f6SDimitry Andric                        TM.getCodeModel(), *Ctx);
48f22ef01cSRoman Divacky }
49f22ef01cSRoman Divacky 
50f22ef01cSRoman Divacky TargetLoweringObjectFile::~TargetLoweringObjectFile() {
51f22ef01cSRoman Divacky }
52f22ef01cSRoman Divacky 
53dff0c46cSDimitry Andric static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) {
5417a519f9SDimitry Andric   const Constant *C = GV->getInitializer();
55f22ef01cSRoman Divacky 
56f22ef01cSRoman Divacky   // Must have zero initializer.
57f22ef01cSRoman Divacky   if (!C->isNullValue())
58f22ef01cSRoman Divacky     return false;
59f22ef01cSRoman Divacky 
60f22ef01cSRoman Divacky   // Leave constant zeros in readonly constant sections, so they can be shared.
61f22ef01cSRoman Divacky   if (GV->isConstant())
62f22ef01cSRoman Divacky     return false;
63f22ef01cSRoman Divacky 
64f22ef01cSRoman Divacky   // If the global has an explicit section specified, don't put it in BSS.
6591bc56edSDimitry Andric   if (GV->hasSection())
66f22ef01cSRoman Divacky     return false;
67f22ef01cSRoman Divacky 
68f22ef01cSRoman Divacky   // If -nozero-initialized-in-bss is specified, don't ever use BSS.
69f22ef01cSRoman Divacky   if (NoZerosInBSS)
70f22ef01cSRoman Divacky     return false;
71f22ef01cSRoman Divacky 
72f22ef01cSRoman Divacky   // Otherwise, put it in BSS!
73f22ef01cSRoman Divacky   return true;
74f22ef01cSRoman Divacky }
75f22ef01cSRoman Divacky 
76f22ef01cSRoman Divacky /// IsNullTerminatedString - Return true if the specified constant (which is
77f22ef01cSRoman Divacky /// known to have a type that is an array of 1/2/4 byte elements) ends with a
78dff0c46cSDimitry Andric /// nul value and contains no other nuls in it.  Note that this is more general
79dff0c46cSDimitry Andric /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
80f22ef01cSRoman Divacky static bool IsNullTerminatedString(const Constant *C) {
81dff0c46cSDimitry Andric   // First check: is we have constant array terminated with zero
82dff0c46cSDimitry Andric   if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
83dff0c46cSDimitry Andric     unsigned NumElts = CDS->getNumElements();
84dff0c46cSDimitry Andric     assert(NumElts != 0 && "Can't have an empty CDS");
85f22ef01cSRoman Divacky 
86dff0c46cSDimitry Andric     if (CDS->getElementAsInteger(NumElts-1) != 0)
87f22ef01cSRoman Divacky       return false; // Not null terminated.
88f22ef01cSRoman Divacky 
89f22ef01cSRoman Divacky     // Verify that the null doesn't occur anywhere else in the string.
90dff0c46cSDimitry Andric     for (unsigned i = 0; i != NumElts-1; ++i)
91dff0c46cSDimitry Andric       if (CDS->getElementAsInteger(i) == 0)
92f22ef01cSRoman Divacky         return false;
93f22ef01cSRoman Divacky     return true;
94f22ef01cSRoman Divacky   }
95f22ef01cSRoman Divacky 
96f22ef01cSRoman Divacky   // Another possibility: [1 x i8] zeroinitializer
97f22ef01cSRoman Divacky   if (isa<ConstantAggregateZero>(C))
98dff0c46cSDimitry Andric     return cast<ArrayType>(C->getType())->getNumElements() == 1;
99f22ef01cSRoman Divacky 
100f22ef01cSRoman Divacky   return false;
101f22ef01cSRoman Divacky }
102f22ef01cSRoman Divacky 
10391bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase(
10491bc56edSDimitry Andric     const GlobalValue *GV, StringRef Suffix, Mangler &Mang,
10591bc56edSDimitry Andric     const TargetMachine &TM) const {
10691bc56edSDimitry Andric   assert(!Suffix.empty());
10791bc56edSDimitry Andric 
108f785676fSDimitry Andric   SmallString<60> NameStr;
1097d523365SDimitry Andric   NameStr += GV->getParent()->getDataLayout().getPrivateGlobalPrefix();
11091bc56edSDimitry Andric   TM.getNameWithPrefix(NameStr, GV, Mang);
11191bc56edSDimitry Andric   NameStr.append(Suffix.begin(), Suffix.end());
112ff0cc061SDimitry Andric   return Ctx->getOrCreateSymbol(NameStr);
113f785676fSDimitry Andric }
114f785676fSDimitry Andric 
11591bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol(
11691bc56edSDimitry Andric     const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
1173b0f4066SDimitry Andric     MachineModuleInfo *MMI) const {
11891bc56edSDimitry Andric   return TM.getSymbol(GV, Mang);
1193b0f4066SDimitry Andric }
1203b0f4066SDimitry Andric 
1213b0f4066SDimitry Andric void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
1227d523365SDimitry Andric                                                     const DataLayout &,
1233b0f4066SDimitry Andric                                                     const MCSymbol *Sym) const {
1243b0f4066SDimitry Andric }
1253b0f4066SDimitry Andric 
1263b0f4066SDimitry Andric 
127f22ef01cSRoman Divacky /// getKindForGlobal - This is a top-level target-independent classifier for
128f22ef01cSRoman Divacky /// a global variable.  Given an global variable and information from TM, it
129f22ef01cSRoman Divacky /// classifies the global in a variety of ways that make various target
130f22ef01cSRoman Divacky /// implementations simpler.  The target implementation is free to ignore this
131f22ef01cSRoman Divacky /// extra info of course.
132f22ef01cSRoman Divacky SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
133f22ef01cSRoman Divacky                                                        const TargetMachine &TM){
134f22ef01cSRoman Divacky   assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
135f22ef01cSRoman Divacky          "Can only be used for global definitions");
136f22ef01cSRoman Divacky 
137f22ef01cSRoman Divacky   Reloc::Model ReloModel = TM.getRelocationModel();
138f22ef01cSRoman Divacky 
139f22ef01cSRoman Divacky   // Early exit - functions should be always in text sections.
140f22ef01cSRoman Divacky   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
14191bc56edSDimitry Andric   if (!GVar)
142f22ef01cSRoman Divacky     return SectionKind::getText();
143f22ef01cSRoman Divacky 
144f22ef01cSRoman Divacky   // Handle thread-local data first.
145f22ef01cSRoman Divacky   if (GVar->isThreadLocal()) {
146dff0c46cSDimitry Andric     if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS))
147f22ef01cSRoman Divacky       return SectionKind::getThreadBSS();
148f22ef01cSRoman Divacky     return SectionKind::getThreadData();
149f22ef01cSRoman Divacky   }
150f22ef01cSRoman Divacky 
151f22ef01cSRoman Divacky   // Variables with common linkage always get classified as common.
152f22ef01cSRoman Divacky   if (GVar->hasCommonLinkage())
153f22ef01cSRoman Divacky     return SectionKind::getCommon();
154f22ef01cSRoman Divacky 
155f22ef01cSRoman Divacky   // Variable can be easily put to BSS section.
156dff0c46cSDimitry Andric   if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) {
157f22ef01cSRoman Divacky     if (GVar->hasLocalLinkage())
158f22ef01cSRoman Divacky       return SectionKind::getBSSLocal();
159f22ef01cSRoman Divacky     else if (GVar->hasExternalLinkage())
160f22ef01cSRoman Divacky       return SectionKind::getBSSExtern();
161f22ef01cSRoman Divacky     return SectionKind::getBSS();
162f22ef01cSRoman Divacky   }
163f22ef01cSRoman Divacky 
16417a519f9SDimitry Andric   const Constant *C = GVar->getInitializer();
165f22ef01cSRoman Divacky 
166f22ef01cSRoman Divacky   // If the global is marked constant, we can put it into a mergable section,
167f22ef01cSRoman Divacky   // a mergable string section, or general .data if it contains relocations.
168f22ef01cSRoman Divacky   if (GVar->isConstant()) {
169f22ef01cSRoman Divacky     // If the initializer for the global contains something that requires a
1707ae0e2c9SDimitry Andric     // relocation, then we may have to drop this into a writable data section
171f22ef01cSRoman Divacky     // even though it is marked const.
1727d523365SDimitry Andric     if (!C->needsRelocation()) {
1732754fe60SDimitry Andric       // If the global is required to have a unique address, it can't be put
1742754fe60SDimitry Andric       // into a mergable section: just drop it into the general read-only
1752754fe60SDimitry Andric       // section instead.
1763ca95b02SDimitry Andric       if (!GVar->hasGlobalUnnamedAddr())
1772754fe60SDimitry Andric         return SectionKind::getReadOnly();
1782754fe60SDimitry Andric 
179f22ef01cSRoman Divacky       // If initializer is a null-terminated string, put it in a "cstring"
180f22ef01cSRoman Divacky       // section of the right width.
1816122f3e6SDimitry Andric       if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
1826122f3e6SDimitry Andric         if (IntegerType *ITy =
183f22ef01cSRoman Divacky               dyn_cast<IntegerType>(ATy->getElementType())) {
184f22ef01cSRoman Divacky           if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
185f22ef01cSRoman Divacky                ITy->getBitWidth() == 32) &&
186f22ef01cSRoman Divacky               IsNullTerminatedString(C)) {
187f22ef01cSRoman Divacky             if (ITy->getBitWidth() == 8)
188f22ef01cSRoman Divacky               return SectionKind::getMergeable1ByteCString();
189f22ef01cSRoman Divacky             if (ITy->getBitWidth() == 16)
190f22ef01cSRoman Divacky               return SectionKind::getMergeable2ByteCString();
191f22ef01cSRoman Divacky 
192f22ef01cSRoman Divacky             assert(ITy->getBitWidth() == 32 && "Unknown width");
193f22ef01cSRoman Divacky             return SectionKind::getMergeable4ByteCString();
194f22ef01cSRoman Divacky           }
195f22ef01cSRoman Divacky         }
196f22ef01cSRoman Divacky       }
197f22ef01cSRoman Divacky 
198f22ef01cSRoman Divacky       // Otherwise, just drop it into a mergable constant section.  If we have
199f22ef01cSRoman Divacky       // a section for this size, use it, otherwise use the arbitrary sized
200f22ef01cSRoman Divacky       // mergable section.
2017d523365SDimitry Andric       switch (GV->getParent()->getDataLayout().getTypeAllocSize(C->getType())) {
202f22ef01cSRoman Divacky       case 4:  return SectionKind::getMergeableConst4();
203f22ef01cSRoman Divacky       case 8:  return SectionKind::getMergeableConst8();
204f22ef01cSRoman Divacky       case 16: return SectionKind::getMergeableConst16();
2053ca95b02SDimitry Andric       case 32: return SectionKind::getMergeableConst32();
206ff0cc061SDimitry Andric       default:
207ff0cc061SDimitry Andric         return SectionKind::getReadOnly();
208f22ef01cSRoman Divacky       }
209f22ef01cSRoman Divacky 
2107d523365SDimitry Andric     } else {
211f22ef01cSRoman Divacky       // In static relocation model, the linker will resolve all addresses, so
212f22ef01cSRoman Divacky       // the relocation entries will actually be constants by the time the app
213f22ef01cSRoman Divacky       // starts up.  However, we can't put this into a mergable section, because
214f22ef01cSRoman Divacky       // the linker doesn't take relocations into consideration when it tries to
215f22ef01cSRoman Divacky       // merge entries in the section.
216f22ef01cSRoman Divacky       if (ReloModel == Reloc::Static)
217f22ef01cSRoman Divacky         return SectionKind::getReadOnly();
218f22ef01cSRoman Divacky 
219f22ef01cSRoman Divacky       // Otherwise, the dynamic linker needs to fix it up, put it in the
220f22ef01cSRoman Divacky       // writable data.rel section.
221f22ef01cSRoman Divacky       return SectionKind::getReadOnlyWithRel();
222f22ef01cSRoman Divacky     }
223f22ef01cSRoman Divacky   }
224f22ef01cSRoman Divacky 
2253ca95b02SDimitry Andric   // Okay, this isn't a constant.
2267d523365SDimitry Andric   return SectionKind::getData();
227f22ef01cSRoman Divacky }
228f22ef01cSRoman Divacky 
229ff0cc061SDimitry Andric /// This method computes the appropriate section to emit the specified global
230ff0cc061SDimitry Andric /// variable or function definition.  This should not be passed external (or
231ff0cc061SDimitry Andric /// available externally) globals.
232ff0cc061SDimitry Andric MCSection *
233ff0cc061SDimitry Andric TargetLoweringObjectFile::SectionForGlobal(const GlobalValue *GV,
234ff0cc061SDimitry Andric                                            SectionKind Kind, Mangler &Mang,
235f22ef01cSRoman Divacky                                            const TargetMachine &TM) const {
236f22ef01cSRoman Divacky   // Select section name.
237f22ef01cSRoman Divacky   if (GV->hasSection())
238f22ef01cSRoman Divacky     return getExplicitSectionGlobal(GV, Kind, Mang, TM);
239f22ef01cSRoman Divacky 
240f22ef01cSRoman Divacky 
241f22ef01cSRoman Divacky   // Use default section depending on the 'type' of global
242f22ef01cSRoman Divacky   return SelectSectionForGlobal(GV, Kind, Mang, TM);
243f22ef01cSRoman Divacky }
244f22ef01cSRoman Divacky 
245ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
246ff0cc061SDimitry Andric     const Function &F, Mangler &Mang, const TargetMachine &TM) const {
2473ca95b02SDimitry Andric   unsigned Align = 0;
2487d523365SDimitry Andric   return getSectionForConstant(F.getParent()->getDataLayout(),
2493ca95b02SDimitry Andric                                SectionKind::getReadOnly(), /*C=*/nullptr,
2503ca95b02SDimitry Andric                                Align);
251ff0cc061SDimitry Andric }
252ff0cc061SDimitry Andric 
253ff0cc061SDimitry Andric bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
254ff0cc061SDimitry Andric     bool UsesLabelDifference, const Function &F) const {
255ff0cc061SDimitry Andric   // In PIC mode, we need to emit the jump table to the same section as the
256ff0cc061SDimitry Andric   // function body itself, otherwise the label differences won't make sense.
257ff0cc061SDimitry Andric   // FIXME: Need a better predicate for this: what about custom entries?
258ff0cc061SDimitry Andric   if (UsesLabelDifference)
259ff0cc061SDimitry Andric     return true;
260ff0cc061SDimitry Andric 
261ff0cc061SDimitry Andric   // We should also do if the section name is NULL or function is declared
262ff0cc061SDimitry Andric   // in discardable section
263ff0cc061SDimitry Andric   // FIXME: this isn't the right predicate, should be based on the MCSection
264ff0cc061SDimitry Andric   // for the function.
265ff0cc061SDimitry Andric   if (F.isWeakForLinker())
266ff0cc061SDimitry Andric     return true;
267ff0cc061SDimitry Andric 
268ff0cc061SDimitry Andric   return false;
269ff0cc061SDimitry Andric }
270ff0cc061SDimitry Andric 
271ff0cc061SDimitry Andric /// Given a mergable constant with the specified size and relocation
272ff0cc061SDimitry Andric /// information, return a section that it should be placed in.
2737d523365SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForConstant(
2743ca95b02SDimitry Andric     const DataLayout &DL, SectionKind Kind, const Constant *C,
2753ca95b02SDimitry Andric     unsigned &Align) const {
27691bc56edSDimitry Andric   if (Kind.isReadOnly() && ReadOnlySection != nullptr)
277f22ef01cSRoman Divacky     return ReadOnlySection;
278f22ef01cSRoman Divacky 
279f22ef01cSRoman Divacky   return DataSection;
280f22ef01cSRoman Divacky }
281f22ef01cSRoman Divacky 
282139f7f9bSDimitry Andric /// getTTypeGlobalReference - Return an MCExpr to use for a
283f22ef01cSRoman Divacky /// reference to the specified global variable from exception
284f22ef01cSRoman Divacky /// handling information.
28591bc56edSDimitry Andric const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference(
28691bc56edSDimitry Andric     const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
28791bc56edSDimitry Andric     const TargetMachine &TM, MachineModuleInfo *MMI,
288f22ef01cSRoman Divacky     MCStreamer &Streamer) const {
289139f7f9bSDimitry Andric   const MCSymbolRefExpr *Ref =
29097bc6c73SDimitry Andric       MCSymbolRefExpr::create(TM.getSymbol(GV, Mang), getContext());
291139f7f9bSDimitry Andric 
292139f7f9bSDimitry Andric   return getTTypeReference(Ref, Encoding, Streamer);
293f22ef01cSRoman Divacky }
294f22ef01cSRoman Divacky 
295f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFile::
296139f7f9bSDimitry Andric getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
297f22ef01cSRoman Divacky                   MCStreamer &Streamer) const {
2983b0f4066SDimitry Andric   switch (Encoding & 0x70) {
299f22ef01cSRoman Divacky   default:
300f22ef01cSRoman Divacky     report_fatal_error("We do not support this DWARF encoding yet!");
301f22ef01cSRoman Divacky   case dwarf::DW_EH_PE_absptr:
302f22ef01cSRoman Divacky     // Do nothing special
303139f7f9bSDimitry Andric     return Sym;
304f22ef01cSRoman Divacky   case dwarf::DW_EH_PE_pcrel: {
305f22ef01cSRoman Divacky     // Emit a label to the streamer for the current position.  This gives us
306f22ef01cSRoman Divacky     // .-foo addressing.
307ff0cc061SDimitry Andric     MCSymbol *PCSym = getContext().createTempSymbol();
308f22ef01cSRoman Divacky     Streamer.EmitLabel(PCSym);
30997bc6c73SDimitry Andric     const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
31097bc6c73SDimitry Andric     return MCBinaryExpr::createSub(Sym, PC, getContext());
311f22ef01cSRoman Divacky   }
312f22ef01cSRoman Divacky   }
313f22ef01cSRoman Divacky }
314f785676fSDimitry Andric 
315f785676fSDimitry Andric const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
316f785676fSDimitry Andric   // FIXME: It's not clear what, if any, default this should have - perhaps a
317f785676fSDimitry Andric   // null return could mean 'no location' & we should just do that here.
31897bc6c73SDimitry Andric   return MCSymbolRefExpr::create(Sym, *Ctx);
319f785676fSDimitry Andric }
320ff0cc061SDimitry Andric 
321ff0cc061SDimitry Andric void TargetLoweringObjectFile::getNameWithPrefix(
3227d523365SDimitry Andric     SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
3237d523365SDimitry Andric     const TargetMachine &TM) const {
3247d523365SDimitry Andric   Mang.getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false);
325ff0cc061SDimitry Andric }
326