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