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