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/Attributes.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/GlobalValue.h"
22#include "llvm/IR/GlobalVariable.h"
23#include "llvm/IR/Mangler.h"
24#include "llvm/IR/Module.h"
25#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/SectionKind.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Target/TargetOptions.h"
33using 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.
42void 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(MCCtx&: ctx, PIC: TM.isPositionIndependent(),
48 LargeCodeModel: 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
57TargetLoweringObjectFile::~TargetLoweringObjectFile() {
58 delete Mang;
59}
60
61unsigned 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
70static bool isNullOrUndef(const Constant *C) {
71 // Check that the constant isn't all zeros or undefs.
72 if (C->isNullValue() || isa<UndefValue>(Val: C))
73 return true;
74 if (!isa<ConstantAggregate>(Val: C))
75 return false;
76 for (const auto *Operand : C->operand_values()) {
77 if (!isNullOrUndef(C: cast<Constant>(Val: Operand)))
78 return false;
79 }
80 return true;
81}
82
83static 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.
106static bool IsNullTerminatedString(const Constant *C) {
107 // First check: is we have constant array terminated with zero
108 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(Val: C)) {
109 uint64_t NumElts = CDS->getNumElements();
110 assert(NumElts != 0 && "Can't have an empty CDS");
111
112 if (CDS->getElementAsInteger(i: 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 (uint64_t 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>(Val: C))
124 return cast<ArrayType>(Val: C->getType())->getNumElements() == 1;
125
126 return false;
127}
128
129MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase(
130 const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const {
131 assert(!Suffix.empty());
132
133 SmallString<60> NameStr;
134 NameStr += GV->getDataLayout().getInternalSymbolPrefix();
135 TM.getNameWithPrefix(Name&: NameStr, GV, Mang&: *Mang);
136 NameStr.append(in_start: Suffix.begin(), in_end: Suffix.end());
137 return getContext().getOrCreateSymbol(Name: NameStr);
138}
139
140MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol(
141 const GlobalValue *GV, const TargetMachine &TM,
142 MachineModuleInfo *MMI) const {
143 return TM.getSymbol(GV);
144}
145
146void TargetLoweringObjectFile::emitPersonalityValue(
147 MCStreamer &Streamer, const DataLayout &, const MCSymbol *Sym,
148 const MachineModuleInfo *MMI) const {}
149
150void TargetLoweringObjectFile::emitCGProfileMetadata(MCStreamer &Streamer,
151 Module &M) const {
152 MCContext &C = getContext();
153 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
154 M.getModuleFlagsMetadata(Flags&: ModuleFlags);
155
156 MDNode *CGProfile = nullptr;
157
158 for (const auto &MFE : ModuleFlags) {
159 StringRef Key = MFE.Key->getString();
160 if (Key == "CG Profile") {
161 CGProfile = cast<MDNode>(Val: MFE.Val);
162 break;
163 }
164 }
165
166 if (!CGProfile)
167 return;
168
169 auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
170 if (!MDO)
171 return nullptr;
172 auto *V = cast<ValueAsMetadata>(Val: MDO);
173 const Function *F = cast<Function>(Val: V->getValue()->stripPointerCasts());
174 if (F->hasDLLImportStorageClass())
175 return nullptr;
176 return TM->getSymbol(GV: F);
177 };
178
179 for (const auto &Edge : CGProfile->operands()) {
180 MDNode *E = cast<MDNode>(Val: Edge);
181 const MCSymbol *From = GetSym(E->getOperand(I: 0));
182 const MCSymbol *To = GetSym(E->getOperand(I: 1));
183 // Skip null functions. This can happen if functions are dead stripped after
184 // the CGProfile pass has been run.
185 if (!From || !To)
186 continue;
187 uint64_t Count = cast<ConstantAsMetadata>(Val: E->getOperand(I: 2))
188 ->getValue()
189 ->getUniqueInteger()
190 .getZExtValue();
191 Streamer.emitCGProfileEntry(From: MCSymbolRefExpr::create(Symbol: From, Ctx&: C),
192 To: MCSymbolRefExpr::create(Symbol: To, Ctx&: C), Count);
193 }
194}
195
196void TargetLoweringObjectFile::emitPseudoProbeDescMetadata(
197 MCStreamer &Streamer, Module &M,
198 std::function<void(MCStreamer &Streamer)> COMDATSymEmitter) const {
199 NamedMDNode *FuncInfo = M.getNamedMetadata(Name: PseudoProbeDescMetadataName);
200 if (!FuncInfo)
201 return;
202
203 // Emit a descriptor for every function including functions that have an
204 // available external linkage. We may not want this for imported functions
205 // that has code in another thinLTO module but we don't have a good way to
206 // tell them apart from inline functions defined in header files. Therefore
207 // we put each descriptor in a separate comdat section and rely on the
208 // linker to deduplicate.
209 auto &C = getContext();
210 for (const auto *Operand : FuncInfo->operands()) {
211 const auto *MD = cast<MDNode>(Val: Operand);
212 auto *GUID = mdconst::extract<ConstantInt>(MD: MD->getOperand(I: 0));
213 auto *Hash = mdconst::extract<ConstantInt>(MD: MD->getOperand(I: 1));
214 auto *Name = cast<MDString>(Val: MD->getOperand(I: 2));
215 auto *S = C.getObjectFileInfo()->getPseudoProbeDescSection(
216 FuncName: TM->getFunctionSections() ? Name->getString() : StringRef(),
217 FuncHash: Hash->getZExtValue());
218
219 Streamer.switchSection(Section: S);
220
221 // emit COFF COMDAT symbol.
222 if (COMDATSymEmitter)
223 COMDATSymEmitter(Streamer);
224
225 Streamer.emitInt64(Value: GUID->getZExtValue());
226 Streamer.emitInt64(Value: Hash->getZExtValue());
227 Streamer.emitULEB128IntValue(Value: Name->getString().size());
228 Streamer.emitBytes(Data: Name->getString());
229 }
230}
231
232static bool containsConstantPtrAuth(const Constant *C) {
233 if (isa<ConstantPtrAuth>(Val: C))
234 return true;
235
236 if (isa<BlockAddress>(Val: C) || isa<GlobalValue>(Val: C))
237 return false;
238
239 for (const Value *Op : C->operands())
240 if (containsConstantPtrAuth(C: cast<Constant>(Val: Op)))
241 return true;
242
243 return false;
244}
245
246/// getKindForGlobal - This is a top-level target-independent classifier for
247/// a global object. Given a global variable and information from the TM, this
248/// function classifies the global in a target independent manner. This function
249/// may be overridden by the target implementation.
250SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
251 const TargetMachine &TM){
252 assert(!GO->isDeclarationForLinker() &&
253 "Can only be used for global definitions");
254
255 // Functions are classified as text sections.
256 if (isa<Function>(Val: GO))
257 return SectionKind::getText();
258
259 // Basic blocks are classified as text sections.
260 if (isa<BasicBlock>(Val: GO))
261 return SectionKind::getText();
262
263 // Global variables require more detailed analysis.
264 const auto *GVar = cast<GlobalVariable>(Val: GO);
265
266 // Handle thread-local data first.
267 if (GVar->isThreadLocal()) {
268 if (isSuitableForBSS(GV: GVar) && !TM.Options.NoZerosInBSS) {
269 // Zero-initialized TLS variables with local linkage always get classified
270 // as ThreadBSSLocal.
271 if (GVar->hasLocalLinkage()) {
272 return SectionKind::getThreadBSSLocal();
273 }
274 return SectionKind::getThreadBSS();
275 }
276 return SectionKind::getThreadData();
277 }
278
279 // Variables with common linkage always get classified as common.
280 if (GVar->hasCommonLinkage())
281 return SectionKind::getCommon();
282
283 // Most non-mergeable zero data can be put in the BSS section unless otherwise
284 // specified.
285 if (isSuitableForBSS(GV: GVar) && !TM.Options.NoZerosInBSS) {
286 if (GVar->hasLocalLinkage())
287 return SectionKind::getBSSLocal();
288 else if (GVar->hasExternalLinkage())
289 return SectionKind::getBSSExtern();
290 return SectionKind::getBSS();
291 }
292
293 // Global variables with '!exclude' should get the exclude section kind if
294 // they have an explicit section and no other metadata. Similarly,
295 // '!metadata_section_kind' forces the section kind to be 'metadata'.
296 if (GVar->hasSection()) {
297 if (MDNode *MD = GVar->getMetadata(KindID: LLVMContext::MD_exclude))
298 if (!MD->getNumOperands())
299 return SectionKind::getExclude();
300 if (MDNode *MD = GVar->getMetadata(KindID: LLVMContext::MD_metadata_section_kind))
301 if (!MD->getNumOperands())
302 return SectionKind::getMetadata();
303 }
304
305 // If the global is marked constant, we can put it into a mergable section,
306 // a mergable string section, or general .data if it contains relocations.
307 if (GVar->isConstant()) {
308 // If the initializer for the global contains something that requires a
309 // relocation, then we may have to drop this into a writable data section
310 // even though it is marked const.
311 const Constant *C = GVar->getInitializer();
312 if (!C->needsRelocation()) {
313 // If the global is required to have a unique address, it can't be put
314 // into a mergable section: just drop it into the general read-only
315 // section instead.
316 if (!GVar->hasGlobalUnnamedAddr())
317 return SectionKind::getReadOnly();
318
319 // If initializer is a null-terminated string, put it in a "cstring"
320 // section of the right width.
321 if (ArrayType *ATy = dyn_cast<ArrayType>(Val: C->getType())) {
322 if (IntegerType *ITy =
323 dyn_cast<IntegerType>(Val: ATy->getElementType())) {
324 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
325 ITy->getBitWidth() == 32) &&
326 IsNullTerminatedString(C)) {
327 if (ITy->getBitWidth() == 8)
328 return SectionKind::getMergeable1ByteCString();
329 if (ITy->getBitWidth() == 16)
330 return SectionKind::getMergeable2ByteCString();
331
332 assert(ITy->getBitWidth() == 32 && "Unknown width");
333 return SectionKind::getMergeable4ByteCString();
334 }
335 }
336 }
337
338 // Otherwise, just drop it into a mergable constant section. If we have
339 // a section for this size, use it, otherwise use the arbitrary sized
340 // mergable section.
341 switch (
342 GVar->getDataLayout().getTypeAllocSize(Ty: C->getType())) {
343 case 4: return SectionKind::getMergeableConst4();
344 case 8: return SectionKind::getMergeableConst8();
345 case 16: return SectionKind::getMergeableConst16();
346 case 32: return SectionKind::getMergeableConst32();
347 default:
348 return SectionKind::getReadOnly();
349 }
350
351 } else {
352 // The dynamic linker always needs to fix PtrAuth relocations up.
353 if (containsConstantPtrAuth(C))
354 return SectionKind::getReadOnlyWithRel();
355
356 // In static, ROPI and RWPI relocation models, the linker will resolve
357 // all addresses, so the relocation entries will actually be constants by
358 // the time the app starts up. However, we can't put this into a
359 // mergable section, because the linker doesn't take relocations into
360 // consideration when it tries to merge entries in the section.
361 Reloc::Model ReloModel = TM.getRelocationModel();
362 if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
363 ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
364 !C->needsDynamicRelocation())
365 return SectionKind::getReadOnly();
366
367 // Otherwise, the dynamic linker needs to fix it up, put it in the
368 // writable data.rel section.
369 return SectionKind::getReadOnlyWithRel();
370 }
371 }
372
373 // Okay, this isn't a constant.
374 return SectionKind::getData();
375}
376
377StringRef
378TargetLoweringObjectFile::getCustomSectionName(const GlobalObject *GO,
379 const TargetMachine &TM) {
380 // Check if '#pragma clang section' name is applicable.
381 // Note that pragma directive overrides -ffunction-section, -fdata-section
382 // and so section name is exactly as user specified and not uniqued.
383 const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: GO);
384 if (GV && GV->hasImplicitSection()) {
385 SectionKind Kind = getKindForGlobal(GO, TM);
386 auto Attrs = GV->getAttributes();
387 if (Attrs.hasAttribute(Kind: "bss-section") && Kind.isBSS())
388 return Attrs.getAttribute(Kind: "bss-section").getValueAsString();
389 else if (Attrs.hasAttribute(Kind: "rodata-section") && Kind.isReadOnly())
390 return Attrs.getAttribute(Kind: "rodata-section").getValueAsString();
391 else if (Attrs.hasAttribute(Kind: "relro-section") && Kind.isReadOnlyWithRel())
392 return Attrs.getAttribute(Kind: "relro-section").getValueAsString();
393 else if (Attrs.hasAttribute(Kind: "data-section") && Kind.isData())
394 return Attrs.getAttribute(Kind: "data-section").getValueAsString();
395 }
396
397 return GO->getSection();
398}
399
400/// This method computes the appropriate section to emit the specified global
401/// variable or function definition. This should not be passed external (or
402/// available externally) globals.
403MCSection *TargetLoweringObjectFile::SectionForGlobal(
404 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
405 // Select section name.
406 if (GO->hasSection())
407 return getExplicitSectionGlobal(GO, Kind, TM);
408
409 if (auto *GVar = dyn_cast<GlobalVariable>(Val: GO)) {
410 auto Attrs = GVar->getAttributes();
411 if ((Attrs.hasAttribute(Kind: "bss-section") && Kind.isBSS()) ||
412 (Attrs.hasAttribute(Kind: "data-section") && Kind.isData()) ||
413 (Attrs.hasAttribute(Kind: "relro-section") && Kind.isReadOnlyWithRel()) ||
414 (Attrs.hasAttribute(Kind: "rodata-section") && Kind.isReadOnly())) {
415 return getExplicitSectionGlobal(GO, Kind, TM);
416 }
417 }
418
419 // Use default section depending on the 'type' of global
420 return SelectSectionForGlobal(GO, Kind, TM);
421}
422
423/// This method computes the appropriate section to emit the specified global
424/// variable or function definition. This should not be passed external (or
425/// available externally) globals.
426MCSection *
427TargetLoweringObjectFile::SectionForGlobal(const GlobalObject *GO,
428 const TargetMachine &TM) const {
429 return SectionForGlobal(GO, Kind: getKindForGlobal(GO, TM), TM);
430}
431
432MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
433 const Function &F, const TargetMachine &TM) const {
434 return getSectionForJumpTable(F, TM, /*JTE=*/nullptr);
435}
436
437MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
438 const Function &F, const TargetMachine &TM,
439 const MachineJumpTableEntry *JTE) const {
440 Align Alignment(1);
441 return getSectionForConstant(DL: F.getDataLayout(), Kind: SectionKind::getReadOnly(),
442 /*C=*/nullptr, Alignment, F: &F);
443}
444
445bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
446 bool UsesLabelDifference, const Function &F) const {
447 // In PIC mode, we need to emit the jump table to the same section as the
448 // function body itself, otherwise the label differences won't make sense.
449 // FIXME: Need a better predicate for this: what about custom entries?
450 if (UsesLabelDifference)
451 return true;
452
453 // We should also do if the section name is NULL or function is declared
454 // in discardable section
455 // FIXME: this isn't the right predicate, should be based on the MCSection
456 // for the function.
457 return F.isWeakForLinker();
458}
459
460/// Given a mergable constant with the specified size and relocation
461/// information, return a section that it should be placed in.
462MCSection *TargetLoweringObjectFile::getSectionForConstant(
463 const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
464 const Function *F) const {
465 if (Kind.isReadOnly() && ReadOnlySection != nullptr)
466 return ReadOnlySection;
467
468 return DataSection;
469}
470
471MCSection *TargetLoweringObjectFile::getSectionForConstant(
472 const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
473 const Function *F, StringRef SectionPrefix) const {
474 // Fallback to `getSectionForConstant` without `SectionPrefix` parameter if it
475 // is empty.
476 if (SectionPrefix.empty())
477 return getSectionForConstant(DL, Kind, C, Alignment, F);
478 report_fatal_error(
479 reason: "TargetLoweringObjectFile::getSectionForConstant that "
480 "accepts SectionPrefix is not implemented for the object file format");
481}
482
483MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
484 const Function &F, const MachineBasicBlock &MBB,
485 const TargetMachine &TM) const {
486 return nullptr;
487}
488
489MCSection *TargetLoweringObjectFile::getUniqueSectionForFunction(
490 const Function &F, const TargetMachine &TM) const {
491 return nullptr;
492}
493
494/// getTTypeGlobalReference - Return an MCExpr to use for a
495/// reference to the specified global variable from exception
496/// handling information.
497const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference(
498 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
499 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
500 const MCSymbolRefExpr *Ref =
501 MCSymbolRefExpr::create(Symbol: TM.getSymbol(GV), Ctx&: getContext());
502
503 return getTTypeReference(Sym: Ref, Encoding, Streamer);
504}
505
506const MCExpr *TargetLoweringObjectFile::
507getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
508 MCStreamer &Streamer) const {
509 switch (Encoding & 0x70) {
510 default:
511 report_fatal_error(reason: "We do not support this DWARF encoding yet!");
512 case dwarf::DW_EH_PE_absptr:
513 // Do nothing special
514 return Sym;
515 case dwarf::DW_EH_PE_pcrel: {
516 // Emit a label to the streamer for the current position. This gives us
517 // .-foo addressing.
518 MCSymbol *PCSym = getContext().createTempSymbol();
519 Streamer.emitLabel(Symbol: PCSym);
520 const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: getContext());
521 return MCBinaryExpr::createSub(LHS: Sym, RHS: PC, Ctx&: getContext());
522 }
523 }
524}
525
526const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
527 // FIXME: It's not clear what, if any, default this should have - perhaps a
528 // null return could mean 'no location' & we should just do that here.
529 return MCSymbolRefExpr::create(Symbol: Sym, Ctx&: getContext());
530}
531
532void TargetLoweringObjectFile::getNameWithPrefix(
533 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
534 const TargetMachine &TM) const {
535 Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false);
536}
537