xref: /llvm-project/llvm/lib/Target/TargetLoweringObjectFile.cpp (revision 506b6170cb513f1cb6e93a3b690c758f9ded18ac)
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     if (F->hasDLLImportStorageClass())
166       return nullptr;
167     return TM->getSymbol(F);
168   };
169 
170   for (const auto &Edge : CFGProfile->operands()) {
171     MDNode *E = cast<MDNode>(Edge);
172     const MCSymbol *From = GetSym(E->getOperand(0));
173     const MCSymbol *To = GetSym(E->getOperand(1));
174     // Skip null functions. This can happen if functions are dead stripped after
175     // the CGProfile pass has been run.
176     if (!From || !To)
177       continue;
178     uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
179                          ->getValue()
180                          ->getUniqueInteger()
181                          .getZExtValue();
182     Streamer.emitCGProfileEntry(
183         MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
184         MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
185   }
186 }
187 
188 /// getKindForGlobal - This is a top-level target-independent classifier for
189 /// a global object.  Given a global variable and information from the TM, this
190 /// function classifies the global in a target independent manner. This function
191 /// may be overridden by the target implementation.
192 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
193                                                        const TargetMachine &TM){
194   assert(!GO->isDeclarationForLinker() &&
195          "Can only be used for global definitions");
196 
197   // Functions are classified as text sections.
198   if (isa<Function>(GO))
199     return SectionKind::getText();
200 
201   // Basic blocks are classified as text sections.
202   if (isa<BasicBlock>(GO))
203     return SectionKind::getText();
204 
205   // Global variables require more detailed analysis.
206   const auto *GVar = cast<GlobalVariable>(GO);
207 
208   // Handle thread-local data first.
209   if (GVar->isThreadLocal()) {
210     if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS)
211       return SectionKind::getThreadBSS();
212     return SectionKind::getThreadData();
213   }
214 
215   // Variables with common linkage always get classified as common.
216   if (GVar->hasCommonLinkage())
217     return SectionKind::getCommon();
218 
219   // Most non-mergeable zero data can be put in the BSS section unless otherwise
220   // specified.
221   if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
222     if (GVar->hasLocalLinkage())
223       return SectionKind::getBSSLocal();
224     else if (GVar->hasExternalLinkage())
225       return SectionKind::getBSSExtern();
226     return SectionKind::getBSS();
227   }
228 
229   // If the global is marked constant, we can put it into a mergable section,
230   // a mergable string section, or general .data if it contains relocations.
231   if (GVar->isConstant()) {
232     // If the initializer for the global contains something that requires a
233     // relocation, then we may have to drop this into a writable data section
234     // even though it is marked const.
235     const Constant *C = GVar->getInitializer();
236     if (!C->needsRelocation()) {
237       // If the global is required to have a unique address, it can't be put
238       // into a mergable section: just drop it into the general read-only
239       // section instead.
240       if (!GVar->hasGlobalUnnamedAddr())
241         return SectionKind::getReadOnly();
242 
243       // If initializer is a null-terminated string, put it in a "cstring"
244       // section of the right width.
245       if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
246         if (IntegerType *ITy =
247               dyn_cast<IntegerType>(ATy->getElementType())) {
248           if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
249                ITy->getBitWidth() == 32) &&
250               IsNullTerminatedString(C)) {
251             if (ITy->getBitWidth() == 8)
252               return SectionKind::getMergeable1ByteCString();
253             if (ITy->getBitWidth() == 16)
254               return SectionKind::getMergeable2ByteCString();
255 
256             assert(ITy->getBitWidth() == 32 && "Unknown width");
257             return SectionKind::getMergeable4ByteCString();
258           }
259         }
260       }
261 
262       // Otherwise, just drop it into a mergable constant section.  If we have
263       // a section for this size, use it, otherwise use the arbitrary sized
264       // mergable section.
265       switch (
266           GVar->getParent()->getDataLayout().getTypeAllocSize(C->getType())) {
267       case 4:  return SectionKind::getMergeableConst4();
268       case 8:  return SectionKind::getMergeableConst8();
269       case 16: return SectionKind::getMergeableConst16();
270       case 32: return SectionKind::getMergeableConst32();
271       default:
272         return SectionKind::getReadOnly();
273       }
274 
275     } else {
276       // In static, ROPI and RWPI relocation models, the linker will resolve
277       // all addresses, so the relocation entries will actually be constants by
278       // the time the app starts up.  However, we can't put this into a
279       // mergable section, because the linker doesn't take relocations into
280       // consideration when it tries to merge entries in the section.
281       Reloc::Model ReloModel = TM.getRelocationModel();
282       if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
283           ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI)
284         return SectionKind::getReadOnly();
285 
286       // Otherwise, the dynamic linker needs to fix it up, put it in the
287       // writable data.rel section.
288       return SectionKind::getReadOnlyWithRel();
289     }
290   }
291 
292   // Okay, this isn't a constant.
293   return SectionKind::getData();
294 }
295 
296 /// This method computes the appropriate section to emit the specified global
297 /// variable or function definition.  This should not be passed external (or
298 /// available externally) globals.
299 MCSection *TargetLoweringObjectFile::SectionForGlobal(
300     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
301   // Select section name.
302   if (GO->hasSection())
303     return getExplicitSectionGlobal(GO, Kind, TM);
304 
305   if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
306     auto Attrs = GVar->getAttributes();
307     if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) ||
308         (Attrs.hasAttribute("data-section") && Kind.isData()) ||
309         (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) ||
310         (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()))  {
311        return getExplicitSectionGlobal(GO, Kind, TM);
312     }
313   }
314 
315   if (auto *F = dyn_cast<Function>(GO)) {
316     if (F->hasFnAttribute("implicit-section-name"))
317       return getExplicitSectionGlobal(GO, Kind, TM);
318   }
319 
320   // Use default section depending on the 'type' of global
321   return SelectSectionForGlobal(GO, Kind, TM);
322 }
323 
324 /// This method computes the appropriate section to emit the specified global
325 /// variable or function definition. This should not be passed external (or
326 /// available externally) globals.
327 MCSection *
328 TargetLoweringObjectFile::SectionForGlobal(const GlobalObject *GO,
329                                            const TargetMachine &TM) const {
330   return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM);
331 }
332 
333 MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
334     const Function &F, const TargetMachine &TM) const {
335   Align Alignment(1);
336   return getSectionForConstant(F.getParent()->getDataLayout(),
337                                SectionKind::getReadOnly(), /*C=*/nullptr,
338                                Alignment);
339 }
340 
341 bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
342     bool UsesLabelDifference, const Function &F) const {
343   // In PIC mode, we need to emit the jump table to the same section as the
344   // function body itself, otherwise the label differences won't make sense.
345   // FIXME: Need a better predicate for this: what about custom entries?
346   if (UsesLabelDifference)
347     return true;
348 
349   // We should also do if the section name is NULL or function is declared
350   // in discardable section
351   // FIXME: this isn't the right predicate, should be based on the MCSection
352   // for the function.
353   return F.isWeakForLinker();
354 }
355 
356 /// Given a mergable constant with the specified size and relocation
357 /// information, return a section that it should be placed in.
358 MCSection *TargetLoweringObjectFile::getSectionForConstant(
359     const DataLayout &DL, SectionKind Kind, const Constant *C,
360     Align &Alignment) const {
361   if (Kind.isReadOnly() && ReadOnlySection != nullptr)
362     return ReadOnlySection;
363 
364   return DataSection;
365 }
366 
367 MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
368     const Function &F, const MachineBasicBlock &MBB,
369     const TargetMachine &TM) const {
370   return nullptr;
371 }
372 
373 /// getTTypeGlobalReference - Return an MCExpr to use for a
374 /// reference to the specified global variable from exception
375 /// handling information.
376 const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference(
377     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
378     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
379   const MCSymbolRefExpr *Ref =
380       MCSymbolRefExpr::create(TM.getSymbol(GV), getContext());
381 
382   return getTTypeReference(Ref, Encoding, Streamer);
383 }
384 
385 const MCExpr *TargetLoweringObjectFile::
386 getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
387                   MCStreamer &Streamer) const {
388   switch (Encoding & 0x70) {
389   default:
390     report_fatal_error("We do not support this DWARF encoding yet!");
391   case dwarf::DW_EH_PE_absptr:
392     // Do nothing special
393     return Sym;
394   case dwarf::DW_EH_PE_pcrel: {
395     // Emit a label to the streamer for the current position.  This gives us
396     // .-foo addressing.
397     MCSymbol *PCSym = getContext().createTempSymbol();
398     Streamer.emitLabel(PCSym);
399     const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
400     return MCBinaryExpr::createSub(Sym, PC, getContext());
401   }
402   }
403 }
404 
405 const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
406   // FIXME: It's not clear what, if any, default this should have - perhaps a
407   // null return could mean 'no location' & we should just do that here.
408   return MCSymbolRefExpr::create(Sym, getContext());
409 }
410 
411 void TargetLoweringObjectFile::getNameWithPrefix(
412     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
413     const TargetMachine &TM) const {
414   Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false);
415 }
416