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