1*0b57cec5SDimitry Andric //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements classes used to handle lowerings specific to common
10*0b57cec5SDimitry Andric // object file formats.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
15*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
17*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
18*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
19*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
20*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
21*0b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
22*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
23*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
24*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
25*0b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
26*0b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
27*0b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h"
28*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
29*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
30*0b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
31*0b57cec5SDimitry Andric using namespace llvm;
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
34*0b57cec5SDimitry Andric //                              Generic Code
35*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric /// Initialize - this method must be called before any actual lowering is
38*0b57cec5SDimitry Andric /// done.  This specifies the current context for codegen, and gives the
39*0b57cec5SDimitry Andric /// lowering implementations a chance to set up their default sections.
Initialize(MCContext & ctx,const TargetMachine & TM)40*0b57cec5SDimitry Andric void TargetLoweringObjectFile::Initialize(MCContext &ctx,
41*0b57cec5SDimitry Andric                                           const TargetMachine &TM) {
42*0b57cec5SDimitry Andric   // `Initialize` can be called more than once.
43*0b57cec5SDimitry Andric   delete Mang;
44*0b57cec5SDimitry Andric   Mang = new Mangler();
45*0b57cec5SDimitry Andric   initMCObjectFileInfo(ctx, TM.isPositionIndependent(),
46*0b57cec5SDimitry Andric                        TM.getCodeModel() == CodeModel::Large);
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric   // Reset various EH DWARF encodings.
49*0b57cec5SDimitry Andric   PersonalityEncoding = LSDAEncoding = TTypeEncoding = dwarf::DW_EH_PE_absptr;
50*0b57cec5SDimitry Andric   CallSiteEncoding = dwarf::DW_EH_PE_uleb128;
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   this->TM = &TM;
53*0b57cec5SDimitry Andric }
54*0b57cec5SDimitry Andric 
~TargetLoweringObjectFile()55*0b57cec5SDimitry Andric TargetLoweringObjectFile::~TargetLoweringObjectFile() {
56*0b57cec5SDimitry Andric   delete Mang;
57*0b57cec5SDimitry Andric }
58*0b57cec5SDimitry Andric 
getCallSiteEncoding() const59*0b57cec5SDimitry Andric unsigned TargetLoweringObjectFile::getCallSiteEncoding() const {
60*0b57cec5SDimitry Andric   // If target does not have LEB128 directives, we would need the
61*0b57cec5SDimitry Andric   // call site encoding to be udata4 so that the alternative path
62*0b57cec5SDimitry Andric   // for not having LEB128 directives could work.
63*0b57cec5SDimitry Andric   if (!getContext().getAsmInfo()->hasLEB128Directives())
64*0b57cec5SDimitry Andric     return dwarf::DW_EH_PE_udata4;
65*0b57cec5SDimitry Andric   return CallSiteEncoding;
66*0b57cec5SDimitry Andric }
67*0b57cec5SDimitry Andric 
isNullOrUndef(const Constant * C)68*0b57cec5SDimitry Andric static bool isNullOrUndef(const Constant *C) {
69*0b57cec5SDimitry Andric   // Check that the constant isn't all zeros or undefs.
70*0b57cec5SDimitry Andric   if (C->isNullValue() || isa<UndefValue>(C))
71*0b57cec5SDimitry Andric     return true;
72*0b57cec5SDimitry Andric   if (!isa<ConstantAggregate>(C))
73*0b57cec5SDimitry Andric     return false;
74*0b57cec5SDimitry Andric   for (const auto *Operand : C->operand_values()) {
75*0b57cec5SDimitry Andric     if (!isNullOrUndef(cast<Constant>(Operand)))
76*0b57cec5SDimitry Andric       return false;
77*0b57cec5SDimitry Andric   }
78*0b57cec5SDimitry Andric   return true;
79*0b57cec5SDimitry Andric }
80*0b57cec5SDimitry Andric 
isSuitableForBSS(const GlobalVariable * GV)81*0b57cec5SDimitry Andric static bool isSuitableForBSS(const GlobalVariable *GV) {
82*0b57cec5SDimitry Andric   const Constant *C = GV->getInitializer();
83*0b57cec5SDimitry Andric 
84*0b57cec5SDimitry Andric   // Must have zero initializer.
85*0b57cec5SDimitry Andric   if (!isNullOrUndef(C))
86*0b57cec5SDimitry Andric     return false;
87*0b57cec5SDimitry Andric 
88*0b57cec5SDimitry Andric   // Leave constant zeros in readonly constant sections, so they can be shared.
89*0b57cec5SDimitry Andric   if (GV->isConstant())
90*0b57cec5SDimitry Andric     return false;
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric   // If the global has an explicit section specified, don't put it in BSS.
93*0b57cec5SDimitry Andric   if (GV->hasSection())
94*0b57cec5SDimitry Andric     return false;
95*0b57cec5SDimitry Andric 
96*0b57cec5SDimitry Andric   // Otherwise, put it in BSS!
97*0b57cec5SDimitry Andric   return true;
98*0b57cec5SDimitry Andric }
99*0b57cec5SDimitry Andric 
100*0b57cec5SDimitry Andric /// IsNullTerminatedString - Return true if the specified constant (which is
101*0b57cec5SDimitry Andric /// known to have a type that is an array of 1/2/4 byte elements) ends with a
102*0b57cec5SDimitry Andric /// nul value and contains no other nuls in it.  Note that this is more general
103*0b57cec5SDimitry Andric /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
IsNullTerminatedString(const Constant * C)104*0b57cec5SDimitry Andric static bool IsNullTerminatedString(const Constant *C) {
105*0b57cec5SDimitry Andric   // First check: is we have constant array terminated with zero
106*0b57cec5SDimitry Andric   if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
107*0b57cec5SDimitry Andric     unsigned NumElts = CDS->getNumElements();
108*0b57cec5SDimitry Andric     assert(NumElts != 0 && "Can't have an empty CDS");
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric     if (CDS->getElementAsInteger(NumElts-1) != 0)
111*0b57cec5SDimitry Andric       return false; // Not null terminated.
112*0b57cec5SDimitry Andric 
113*0b57cec5SDimitry Andric     // Verify that the null doesn't occur anywhere else in the string.
114*0b57cec5SDimitry Andric     for (unsigned i = 0; i != NumElts-1; ++i)
115*0b57cec5SDimitry Andric       if (CDS->getElementAsInteger(i) == 0)
116*0b57cec5SDimitry Andric         return false;
117*0b57cec5SDimitry Andric     return true;
118*0b57cec5SDimitry Andric   }
119*0b57cec5SDimitry Andric 
120*0b57cec5SDimitry Andric   // Another possibility: [1 x i8] zeroinitializer
121*0b57cec5SDimitry Andric   if (isa<ConstantAggregateZero>(C))
122*0b57cec5SDimitry Andric     return cast<ArrayType>(C->getType())->getNumElements() == 1;
123*0b57cec5SDimitry Andric 
124*0b57cec5SDimitry Andric   return false;
125*0b57cec5SDimitry Andric }
126*0b57cec5SDimitry Andric 
getSymbolWithGlobalValueBase(const GlobalValue * GV,StringRef Suffix,const TargetMachine & TM) const127*0b57cec5SDimitry Andric MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase(
128*0b57cec5SDimitry Andric     const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const {
129*0b57cec5SDimitry Andric   assert(!Suffix.empty());
130*0b57cec5SDimitry Andric 
131*0b57cec5SDimitry Andric   SmallString<60> NameStr;
132*0b57cec5SDimitry Andric   NameStr += GV->getParent()->getDataLayout().getPrivateGlobalPrefix();
133*0b57cec5SDimitry Andric   TM.getNameWithPrefix(NameStr, GV, *Mang);
134*0b57cec5SDimitry Andric   NameStr.append(Suffix.begin(), Suffix.end());
135*0b57cec5SDimitry Andric   return getContext().getOrCreateSymbol(NameStr);
136*0b57cec5SDimitry Andric }
137*0b57cec5SDimitry Andric 
getCFIPersonalitySymbol(const GlobalValue * GV,const TargetMachine & TM,MachineModuleInfo * MMI) const138*0b57cec5SDimitry Andric MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol(
139*0b57cec5SDimitry Andric     const GlobalValue *GV, const TargetMachine &TM,
140*0b57cec5SDimitry Andric     MachineModuleInfo *MMI) const {
141*0b57cec5SDimitry Andric   return TM.getSymbol(GV);
142*0b57cec5SDimitry Andric }
143*0b57cec5SDimitry Andric 
emitPersonalityValue(MCStreamer & Streamer,const DataLayout &,const MCSymbol * Sym) const144*0b57cec5SDimitry Andric void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
145*0b57cec5SDimitry Andric                                                     const DataLayout &,
146*0b57cec5SDimitry Andric                                                     const MCSymbol *Sym) const {
147*0b57cec5SDimitry Andric }
148*0b57cec5SDimitry Andric 
emitCGProfileMetadata(MCStreamer & Streamer,Module & M) const149*0b57cec5SDimitry Andric void TargetLoweringObjectFile::emitCGProfileMetadata(MCStreamer &Streamer,
150*0b57cec5SDimitry Andric                                                      Module &M) const {
151*0b57cec5SDimitry Andric   MCContext &C = getContext();
152*0b57cec5SDimitry Andric   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
153*0b57cec5SDimitry Andric   M.getModuleFlagsMetadata(ModuleFlags);
154*0b57cec5SDimitry Andric 
155*0b57cec5SDimitry Andric   MDNode *CFGProfile = nullptr;
156*0b57cec5SDimitry Andric 
157*0b57cec5SDimitry Andric   for (const auto &MFE : ModuleFlags) {
158*0b57cec5SDimitry Andric     StringRef Key = MFE.Key->getString();
159*0b57cec5SDimitry Andric     if (Key == "CG Profile") {
160*0b57cec5SDimitry Andric       CFGProfile = cast<MDNode>(MFE.Val);
161*0b57cec5SDimitry Andric       break;
162*0b57cec5SDimitry Andric     }
163*0b57cec5SDimitry Andric   }
164*0b57cec5SDimitry Andric 
165*0b57cec5SDimitry Andric   if (!CFGProfile)
166*0b57cec5SDimitry Andric     return;
167*0b57cec5SDimitry Andric 
168*0b57cec5SDimitry Andric   auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
169*0b57cec5SDimitry Andric     if (!MDO)
170*0b57cec5SDimitry Andric       return nullptr;
171*0b57cec5SDimitry Andric     auto *V = cast<ValueAsMetadata>(MDO);
172*0b57cec5SDimitry Andric     const Function *F = cast<Function>(V->getValue()->stripPointerCasts());
173*0b57cec5SDimitry Andric     if (F->hasDLLImportStorageClass())
174*0b57cec5SDimitry Andric       return nullptr;
175*0b57cec5SDimitry Andric     return TM->getSymbol(F);
176*0b57cec5SDimitry Andric   };
177*0b57cec5SDimitry Andric 
178*0b57cec5SDimitry Andric   for (const auto &Edge : CFGProfile->operands()) {
179*0b57cec5SDimitry Andric     MDNode *E = cast<MDNode>(Edge);
180*0b57cec5SDimitry Andric     const MCSymbol *From = GetSym(E->getOperand(0));
181*0b57cec5SDimitry Andric     const MCSymbol *To = GetSym(E->getOperand(1));
182*0b57cec5SDimitry Andric     // Skip null functions. This can happen if functions are dead stripped after
183*0b57cec5SDimitry Andric     // the CGProfile pass has been run.
184*0b57cec5SDimitry Andric     if (!From || !To)
185*0b57cec5SDimitry Andric       continue;
186*0b57cec5SDimitry Andric     uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
187*0b57cec5SDimitry Andric                          ->getValue()
188*0b57cec5SDimitry Andric                          ->getUniqueInteger()
189*0b57cec5SDimitry Andric                          .getZExtValue();
190*0b57cec5SDimitry Andric     Streamer.emitCGProfileEntry(
191*0b57cec5SDimitry Andric         MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
192*0b57cec5SDimitry Andric         MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
193*0b57cec5SDimitry Andric   }
194*0b57cec5SDimitry Andric }
195*0b57cec5SDimitry Andric 
196*0b57cec5SDimitry Andric /// getKindForGlobal - This is a top-level target-independent classifier for
197*0b57cec5SDimitry Andric /// a global object.  Given a global variable and information from the TM, this
198*0b57cec5SDimitry Andric /// function classifies the global in a target independent manner. This function
199*0b57cec5SDimitry Andric /// may be overridden by the target implementation.
getKindForGlobal(const GlobalObject * GO,const TargetMachine & TM)200*0b57cec5SDimitry Andric SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
201*0b57cec5SDimitry Andric                                                        const TargetMachine &TM){
202*0b57cec5SDimitry Andric   assert(!GO->isDeclarationForLinker() &&
203*0b57cec5SDimitry Andric          "Can only be used for global definitions");
204*0b57cec5SDimitry Andric 
205*0b57cec5SDimitry Andric   // Functions are classified as text sections.
206*0b57cec5SDimitry Andric   if (isa<Function>(GO))
207*0b57cec5SDimitry Andric     return SectionKind::getText();
208*0b57cec5SDimitry Andric 
209*0b57cec5SDimitry Andric   // Basic blocks are classified as text sections.
210*0b57cec5SDimitry Andric   if (isa<BasicBlock>(GO))
211*0b57cec5SDimitry Andric     return SectionKind::getText();
212*0b57cec5SDimitry Andric 
213*0b57cec5SDimitry Andric   // Global variables require more detailed analysis.
214*0b57cec5SDimitry Andric   const auto *GVar = cast<GlobalVariable>(GO);
215*0b57cec5SDimitry Andric 
216*0b57cec5SDimitry Andric   // Handle thread-local data first.
217*0b57cec5SDimitry Andric   if (GVar->isThreadLocal()) {
218*0b57cec5SDimitry Andric     if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
219*0b57cec5SDimitry Andric       // Zero-initialized TLS variables with local linkage always get classified
220*0b57cec5SDimitry Andric       // as ThreadBSSLocal.
221*0b57cec5SDimitry Andric       if (GVar->hasLocalLinkage()) {
222*0b57cec5SDimitry Andric         return SectionKind::getThreadBSSLocal();
223*0b57cec5SDimitry Andric       }
224*0b57cec5SDimitry Andric       return SectionKind::getThreadBSS();
225*0b57cec5SDimitry Andric     }
226*0b57cec5SDimitry Andric     return SectionKind::getThreadData();
227*0b57cec5SDimitry Andric   }
228*0b57cec5SDimitry Andric 
229*0b57cec5SDimitry Andric   // Variables with common linkage always get classified as common.
230*0b57cec5SDimitry Andric   if (GVar->hasCommonLinkage())
231*0b57cec5SDimitry Andric     return SectionKind::getCommon();
232*0b57cec5SDimitry Andric 
233*0b57cec5SDimitry Andric   // Most non-mergeable zero data can be put in the BSS section unless otherwise
234*0b57cec5SDimitry Andric   // specified.
235*0b57cec5SDimitry Andric   if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
236*0b57cec5SDimitry Andric     if (GVar->hasLocalLinkage())
237*0b57cec5SDimitry Andric       return SectionKind::getBSSLocal();
238*0b57cec5SDimitry Andric     else if (GVar->hasExternalLinkage())
239*0b57cec5SDimitry Andric       return SectionKind::getBSSExtern();
240*0b57cec5SDimitry Andric     return SectionKind::getBSS();
241*0b57cec5SDimitry Andric   }
242*0b57cec5SDimitry Andric 
243*0b57cec5SDimitry Andric   // Global variables with '!exclude' should get the exclude section kind if
244*0b57cec5SDimitry Andric   // they have an explicit section and no other metadata.
245*0b57cec5SDimitry Andric   if (GVar->hasSection())
246*0b57cec5SDimitry Andric     if (MDNode *MD = GVar->getMetadata(LLVMContext::MD_exclude))
247*0b57cec5SDimitry Andric       if (!MD->getNumOperands())
248*0b57cec5SDimitry Andric         return SectionKind::getExclude();
249*0b57cec5SDimitry Andric 
250*0b57cec5SDimitry Andric   // If the global is marked constant, we can put it into a mergable section,
251*0b57cec5SDimitry Andric   // a mergable string section, or general .data if it contains relocations.
252*0b57cec5SDimitry Andric   if (GVar->isConstant()) {
253*0b57cec5SDimitry Andric     // If the initializer for the global contains something that requires a
254*0b57cec5SDimitry Andric     // relocation, then we may have to drop this into a writable data section
255*0b57cec5SDimitry Andric     // even though it is marked const.
256*0b57cec5SDimitry Andric     const Constant *C = GVar->getInitializer();
257*0b57cec5SDimitry Andric     if (!C->needsRelocation()) {
258*0b57cec5SDimitry Andric       // If the global is required to have a unique address, it can't be put
259*0b57cec5SDimitry Andric       // into a mergable section: just drop it into the general read-only
260*0b57cec5SDimitry Andric       // section instead.
261*0b57cec5SDimitry Andric       if (!GVar->hasGlobalUnnamedAddr())
262*0b57cec5SDimitry Andric         return SectionKind::getReadOnly();
263*0b57cec5SDimitry Andric 
264*0b57cec5SDimitry Andric       // If initializer is a null-terminated string, put it in a "cstring"
265*0b57cec5SDimitry Andric       // section of the right width.
266*0b57cec5SDimitry Andric       if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
267*0b57cec5SDimitry Andric         if (IntegerType *ITy =
268*0b57cec5SDimitry Andric               dyn_cast<IntegerType>(ATy->getElementType())) {
269*0b57cec5SDimitry Andric           if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
270*0b57cec5SDimitry Andric                ITy->getBitWidth() == 32) &&
271*0b57cec5SDimitry Andric               IsNullTerminatedString(C)) {
272*0b57cec5SDimitry Andric             if (ITy->getBitWidth() == 8)
273*0b57cec5SDimitry Andric               return SectionKind::getMergeable1ByteCString();
274*0b57cec5SDimitry Andric             if (ITy->getBitWidth() == 16)
275*0b57cec5SDimitry Andric               return SectionKind::getMergeable2ByteCString();
276*0b57cec5SDimitry Andric 
277*0b57cec5SDimitry Andric             assert(ITy->getBitWidth() == 32 && "Unknown width");
278*0b57cec5SDimitry Andric             return SectionKind::getMergeable4ByteCString();
279*0b57cec5SDimitry Andric           }
280*0b57cec5SDimitry Andric         }
281*0b57cec5SDimitry Andric       }
282*0b57cec5SDimitry Andric 
283*0b57cec5SDimitry Andric       // Otherwise, just drop it into a mergable constant section.  If we have
284*0b57cec5SDimitry Andric       // a section for this size, use it, otherwise use the arbitrary sized
285*0b57cec5SDimitry Andric       // mergable section.
286*0b57cec5SDimitry Andric       switch (
287*0b57cec5SDimitry Andric           GVar->getParent()->getDataLayout().getTypeAllocSize(C->getType())) {
288*0b57cec5SDimitry Andric       case 4:  return SectionKind::getMergeableConst4();
289*0b57cec5SDimitry Andric       case 8:  return SectionKind::getMergeableConst8();
290*0b57cec5SDimitry Andric       case 16: return SectionKind::getMergeableConst16();
291*0b57cec5SDimitry Andric       case 32: return SectionKind::getMergeableConst32();
292*0b57cec5SDimitry Andric       default:
293*0b57cec5SDimitry Andric         return SectionKind::getReadOnly();
294*0b57cec5SDimitry Andric       }
295*0b57cec5SDimitry Andric 
296*0b57cec5SDimitry Andric     } else {
297*0b57cec5SDimitry Andric       // In static, ROPI and RWPI relocation models, the linker will resolve
298*0b57cec5SDimitry Andric       // all addresses, so the relocation entries will actually be constants by
299*0b57cec5SDimitry Andric       // the time the app starts up.  However, we can't put this into a
300*0b57cec5SDimitry Andric       // mergable section, because the linker doesn't take relocations into
301*0b57cec5SDimitry Andric       // consideration when it tries to merge entries in the section.
302*0b57cec5SDimitry Andric       Reloc::Model ReloModel = TM.getRelocationModel();
303*0b57cec5SDimitry Andric       if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
304*0b57cec5SDimitry Andric           ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
305*0b57cec5SDimitry Andric           !C->needsDynamicRelocation())
306*0b57cec5SDimitry Andric         return SectionKind::getReadOnly();
307*0b57cec5SDimitry Andric 
308*0b57cec5SDimitry Andric       // Otherwise, the dynamic linker needs to fix it up, put it in the
309*0b57cec5SDimitry Andric       // writable data.rel section.
310*0b57cec5SDimitry Andric       return SectionKind::getReadOnlyWithRel();
311*0b57cec5SDimitry Andric     }
312*0b57cec5SDimitry Andric   }
313*0b57cec5SDimitry Andric 
314*0b57cec5SDimitry Andric   // Okay, this isn't a constant.
315*0b57cec5SDimitry Andric   return SectionKind::getData();
316*0b57cec5SDimitry Andric }
317*0b57cec5SDimitry Andric 
318*0b57cec5SDimitry Andric /// This method computes the appropriate section to emit the specified global
319*0b57cec5SDimitry Andric /// variable or function definition.  This should not be passed external (or
320*0b57cec5SDimitry Andric /// available externally) globals.
SectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const321*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFile::SectionForGlobal(
322*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
323*0b57cec5SDimitry Andric   // Select section name.
324*0b57cec5SDimitry Andric   if (GO->hasSection())
325*0b57cec5SDimitry Andric     return getExplicitSectionGlobal(GO, Kind, TM);
326*0b57cec5SDimitry Andric 
327*0b57cec5SDimitry Andric   if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
328*0b57cec5SDimitry Andric     auto Attrs = GVar->getAttributes();
329*0b57cec5SDimitry Andric     if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) ||
330*0b57cec5SDimitry Andric         (Attrs.hasAttribute("data-section") && Kind.isData()) ||
331*0b57cec5SDimitry Andric         (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) ||
332*0b57cec5SDimitry Andric         (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()))  {
333*0b57cec5SDimitry Andric        return getExplicitSectionGlobal(GO, Kind, TM);
334*0b57cec5SDimitry Andric     }
335*0b57cec5SDimitry Andric   }
336*0b57cec5SDimitry Andric 
337*0b57cec5SDimitry Andric   if (auto *F = dyn_cast<Function>(GO)) {
338*0b57cec5SDimitry Andric     if (F->hasFnAttribute("implicit-section-name"))
339*0b57cec5SDimitry Andric       return getExplicitSectionGlobal(GO, Kind, TM);
340*0b57cec5SDimitry Andric   }
341*0b57cec5SDimitry Andric 
342*0b57cec5SDimitry Andric   // Use default section depending on the 'type' of global
343*0b57cec5SDimitry Andric   return SelectSectionForGlobal(GO, Kind, TM);
344*0b57cec5SDimitry Andric }
345*0b57cec5SDimitry Andric 
346*0b57cec5SDimitry Andric /// This method computes the appropriate section to emit the specified global
347 /// variable or function definition. This should not be passed external (or
348 /// available externally) globals.
349 MCSection *
SectionForGlobal(const GlobalObject * GO,const TargetMachine & TM) const350 TargetLoweringObjectFile::SectionForGlobal(const GlobalObject *GO,
351                                            const TargetMachine &TM) const {
352   return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM);
353 }
354 
getSectionForJumpTable(const Function & F,const TargetMachine & TM) const355 MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
356     const Function &F, const TargetMachine &TM) const {
357   Align Alignment(1);
358   return getSectionForConstant(F.getParent()->getDataLayout(),
359                                SectionKind::getReadOnly(), /*C=*/nullptr,
360                                Alignment);
361 }
362 
shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,const Function & F) const363 bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
364     bool UsesLabelDifference, const Function &F) const {
365   // In PIC mode, we need to emit the jump table to the same section as the
366   // function body itself, otherwise the label differences won't make sense.
367   // FIXME: Need a better predicate for this: what about custom entries?
368   if (UsesLabelDifference)
369     return true;
370 
371   // We should also do if the section name is NULL or function is declared
372   // in discardable section
373   // FIXME: this isn't the right predicate, should be based on the MCSection
374   // for the function.
375   return F.isWeakForLinker();
376 }
377 
378 /// Given a mergable constant with the specified size and relocation
379 /// information, return a section that it should be placed in.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const380 MCSection *TargetLoweringObjectFile::getSectionForConstant(
381     const DataLayout &DL, SectionKind Kind, const Constant *C,
382     Align &Alignment) const {
383   if (Kind.isReadOnly() && ReadOnlySection != nullptr)
384     return ReadOnlySection;
385 
386   return DataSection;
387 }
388 
getSectionForMachineBasicBlock(const Function & F,const MachineBasicBlock & MBB,const TargetMachine & TM) const389 MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
390     const Function &F, const MachineBasicBlock &MBB,
391     const TargetMachine &TM) const {
392   return nullptr;
393 }
394 
getUniqueSectionForFunction(const Function & F,const TargetMachine & TM) const395 MCSection *TargetLoweringObjectFile::getUniqueSectionForFunction(
396     const Function &F, const TargetMachine &TM) const {
397   return nullptr;
398 }
399 
400 /// getTTypeGlobalReference - Return an MCExpr to use for a
401 /// reference to the specified global variable from exception
402 /// handling information.
getTTypeGlobalReference(const GlobalValue * GV,unsigned Encoding,const TargetMachine & TM,MachineModuleInfo * MMI,MCStreamer & Streamer) const403 const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference(
404     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
405     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
406   const MCSymbolRefExpr *Ref =
407       MCSymbolRefExpr::create(TM.getSymbol(GV), getContext());
408 
409   return getTTypeReference(Ref, Encoding, Streamer);
410 }
411 
412 const MCExpr *TargetLoweringObjectFile::
getTTypeReference(const MCSymbolRefExpr * Sym,unsigned Encoding,MCStreamer & Streamer) const413 getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
414                   MCStreamer &Streamer) const {
415   switch (Encoding & 0x70) {
416   default:
417     report_fatal_error("We do not support this DWARF encoding yet!");
418   case dwarf::DW_EH_PE_absptr:
419     // Do nothing special
420     return Sym;
421   case dwarf::DW_EH_PE_pcrel: {
422     // Emit a label to the streamer for the current position.  This gives us
423     // .-foo addressing.
424     MCSymbol *PCSym = getContext().createTempSymbol();
425     Streamer.emitLabel(PCSym);
426     const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
427     return MCBinaryExpr::createSub(Sym, PC, getContext());
428   }
429   }
430 }
431 
getDebugThreadLocalSymbol(const MCSymbol * Sym) const432 const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
433   // FIXME: It's not clear what, if any, default this should have - perhaps a
434   // null return could mean 'no location' & we should just do that here.
435   return MCSymbolRefExpr::create(Sym, getContext());
436 }
437 
getNameWithPrefix(SmallVectorImpl<char> & OutName,const GlobalValue * GV,const TargetMachine & TM) const438 void TargetLoweringObjectFile::getNameWithPrefix(
439     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
440     const TargetMachine &TM) const {
441   Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false);
442 }
443