LLVM 24.0.0git
BTFDebug.cpp
Go to the documentation of this file.
1//===- BTFDebug.cpp - BTF Generator ---------------------------------------===//
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 contains support for writing BTF debug info.
10//
11//===----------------------------------------------------------------------===//
12
13#include "BTFDebug.h"
14#include "BPF.h"
15#include "BPFCORE.h"
17#include "llvm/ADT/STLExtras.h"
27#include "llvm/IR/Module.h"
28#include "llvm/MC/MCContext.h"
31#include "llvm/MC/MCStreamer.h"
32#include "llvm/Support/Debug.h"
38#include <optional>
39
40using namespace llvm;
41
42#define DEBUG_TYPE "btf-debug"
43
44#define GET_CC_REGISTER_LISTS
45#include "BPFGenCallingConv.inc"
46
47static const char *BTFKindStr[] = {
48#define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
49#include "llvm/DebugInfo/BTF/BTF.def"
50};
51
52static const DIType *tryRemoveAtomicType(const DIType *Ty) {
53 if (!Ty)
54 return Ty;
55 auto DerivedTy = dyn_cast<DIDerivedType>(Ty);
56 if (DerivedTy && DerivedTy->getTag() == dwarf::DW_TAG_atomic_type)
57 return DerivedTy->getBaseType();
58 return Ty;
59}
60
61static const DIType *stripDITypeAttributes(const DIType *Ty) {
62 while (const auto *DTy = dyn_cast_or_null<DIDerivedType>(Ty)) {
63 switch (DTy->getTag()) {
64 case dwarf::DW_TAG_atomic_type:
65 case dwarf::DW_TAG_const_type:
66 case dwarf::DW_TAG_restrict_type:
67 case dwarf::DW_TAG_typedef:
68 case dwarf::DW_TAG_volatile_type:
69 Ty = DTy->getBaseType();
70 break;
71 default:
72 return Ty;
73 }
74 }
75 return Ty;
76}
77
78static bool sourceArgMatchesIRType(const DIType *SourceTy, Type *IRTy) {
79 SourceTy = stripDITypeAttributes(SourceTy);
80
81 // All pointers are opaque in LLVM IR, so any source-level pointer matches any
82 // IR pointer regardless of pointee type.
83 if (const auto *DTy = dyn_cast<DIDerivedType>(SourceTy))
84 return DTy->getTag() == dwarf::DW_TAG_pointer_type && IRTy->isPointerTy();
85
86 if (const auto *BTy = dyn_cast<DIBasicType>(SourceTy)) {
87 uint64_t SizeInBits = BTy->getSizeInBits();
88 if (BTy->getEncoding() == dwarf::DW_ATE_float)
89 return IRTy->isFloatingPointTy() &&
90 IRTy->getPrimitiveSizeInBits() == SizeInBits;
91 // _Bool is 8 bits in DWARF/source but lowered to i1 in LLVM IR.
92 if (BTy->getEncoding() == dwarf::DW_ATE_boolean && IRTy->isIntegerTy(1))
93 return true;
94 return IRTy->isIntegerTy(SizeInBits);
95 }
96
97 const auto *CTy = dyn_cast<DICompositeType>(SourceTy);
98 if (!CTy)
99 return false;
100
101 switch (CTy->getTag()) {
102 case dwarf::DW_TAG_enumeration_type:
103 return IRTy->isIntegerTy(CTy->getSizeInBits());
104 default:
105 return false;
106 }
107}
108
109/// Collect the physical register each source argument lives in by scanning
110/// DBG_VALUE instructions in the entry block. A DBG_VALUE is only recorded
111/// when its register either (a) has not been redefined by any preceding
112/// non-debug instruction (i.e. it still holds the caller-passed value), or
113/// (b) was most recently loaded from the stack via $r11 (a stack-passed
114/// argument beyond the first five register args). For each argument only the
115/// first eligible DBG_VALUE is recorded, since that is its entry location.
116///
117/// There is another case where DBG_VALUE is not emitted due to
118/// AssignmentTrackingAnalysis which determines that a variable is
119/// always stack-homed, and describes the variable via MachineFunction's
120/// VariableDbgInfo (setVariableDbgInfo with a frame index). To recover the
121/// register for those arguments, we also track stores of un-redefined physical
122/// registers to stack frame objects during the entry-block walk (using
123/// MachineMemOperands to identify the target frame index), then match them
124/// against VariableDbgInfo entries after the scan.
128 const DISubprogram *SP = MF.getFunction().getSubprogram();
129 SmallDenseSet<Register> DefinedRegs, StackLoadRegs;
130
131 // Build a reverse map from IR alloca to frame index so we can
132 // identify which frame object a store targets via its MachineMemOperand.
133 const MachineFrameInfo &MFI = MF.getFrameInfo();
135 for (int I = 0, N = MFI.getObjectIndexEnd(); I < N; ++I)
136 if (const AllocaInst *AI = MFI.getObjectAllocation(I))
137 AllocaToFI[AI] = I;
138
139 // Maps frame index → first physical register stored there before
140 // that register is redefined.
141 SmallDenseMap<int, Register> FrameIndexToReg;
142
143 for (const MachineInstr &MI : MF.front()) {
144 if (MI.isDebugValue()) {
145 // Skip indirect DBG_VALUEs — the register is a base address for a
146 // memory location, not the argument value itself.
147 if (MI.isIndirectDebugValue())
148 continue;
149
150 const DILocalVariable *DV = MI.getDebugVariable();
151 if (!DV || !DV->getArg() || DV->getScope()->getSubprogram() != SP)
152 continue;
153
154 uint32_t Arg = DV->getArg();
155 const MachineOperand &MO = MI.getDebugOperand(0);
156 if (!MO.isReg() || !MO.getReg().isPhysical())
157 continue;
158
159 if (!DefinedRegs.contains(MO.getReg()) ||
160 StackLoadRegs.contains(MO.getReg()))
161 EntryRegMap.try_emplace(Arg, MO.getReg());
162 continue;
163 }
164
165 // Track stores of unredefined physical registers to stack frame
166 // objects. Use MachineMemOperands to identify the target frame
167 // index rather than assuming a particular addressing mode.
168 if (MI.mayStore() && !MI.isCall() && MI.getOperand(0).isReg()) {
169 Register SrcReg = MI.getOperand(0).getReg();
170 if (SrcReg.isPhysical() && !DefinedRegs.contains(SrcReg)) {
171 for (const MachineMemOperand *MMO : MI.memoperands()) {
172 const Value *V = MMO->getValue();
173 if (!V)
174 continue;
175 auto It = AllocaToFI.find(V);
176 if (It != AllocaToFI.end())
177 FrameIndexToReg.try_emplace(It->second, SrcReg);
178 }
179 }
180 }
181
182 for (const MachineOperand &MO : MI.operands())
183 if (MO.isReg() && MO.isDef() && MO.getReg().isPhysical()) {
184 DefinedRegs.insert(MO.getReg());
185 StackLoadRegs.erase(MO.getReg());
186 }
187
188 // Detect stack argument loads: $rX = LDD $r11, offset.
189 if (MI.getOpcode() == BPF::LDD && MI.getOperand(1).getReg() == BPF::R11)
190 StackLoadRegs.insert(MI.getOperand(0).getReg());
191 }
192
193 // Check VariableDbgInfo for args that AssignmentTrackingAnalysis described
194 // via setVariableDbgInfo (single-loc stack-homed variables) rather than
195 // DBG_VALUE instructions.
196 for (const auto &VI : MF.getVariableDbgInfo()) {
197 if (!VI.Var || !VI.Var->getArg() || !VI.inStackSlot())
198 continue;
199 if (VI.Var->getScope()->getSubprogram() != SP)
200 continue;
201 uint32_t Arg = VI.Var->getArg();
202 if (EntryRegMap.count(Arg))
203 continue;
204 auto It = FrameIndexToReg.find(VI.getStackSlot());
205 if (It != FrameIndexToReg.end())
206 EntryRegMap[Arg] = It->second;
207 }
208
209 SmallVector<std::pair<uint32_t, Register>, 8> AliveArgs(EntryRegMap.begin(),
210 EntryRegMap.end());
211 llvm::sort(AliveArgs, llvm::less_first());
212 return AliveArgs;
213}
214
215/// Check whether the optimized IR signature matches the surviving source
216/// arguments precisely enough to emit a filtered BTF prototype.
217/// Requires exact IR/source arg count match, matching types, and correct
218/// BPF register order (R1..R5) for register args.
220 const MachineFunction &MF, DITypeArray Elements,
221 ArrayRef<std::pair<uint32_t, Register>> AliveArgs,
222 const TargetRegisterInfo &TRI) {
223 if (MF.getFunction().arg_size() != AliveArgs.size()) {
224 LLVM_DEBUG(dbgs() << "BTF skip " << MF.getName() << ": IR arg count ("
225 << MF.getFunction().arg_size() << ") != alive arg count ("
226 << AliveArgs.size() << ")\n");
227 return false;
228 }
229
230 auto ArgIt = MF.getFunction().arg_begin();
231 for (unsigned I = 0, N = AliveArgs.size(); I < N; ++I, ++ArgIt) {
232 auto [ArgNo, Reg] = AliveArgs[I];
233 if (!sourceArgMatchesIRType(Elements[ArgNo], ArgIt->getType())) {
234 LLVM_DEBUG(dbgs() << "BTF skip " << MF.getName()
235 << ": type mismatch for source arg " << ArgNo
236 << " at IR position " << I << "\n");
237 return false;
238 }
239
240 if (I >= std::size(CC_BPF64_ArgRegs))
241 continue;
242
243 int DwarfReg = TRI.getDwarfRegNum(Reg, false);
244 if (DwarfReg != static_cast<int>(I + 1)) {
245 LLVM_DEBUG(dbgs() << "BTF skip " << MF.getName() << ": arg " << ArgNo
246 << " in DWARF reg " << DwarfReg << ", expected "
247 << (I + 1) << "\n");
248 return false;
249 }
250 }
251
252 return true;
253}
254
255/// Emit a BTF common type.
257 OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
258 ")");
259 OS.emitInt32(BTFType.NameOff);
260 OS.AddComment("0x" + Twine::utohexstr(BTFType.Info));
261 OS.emitInt32(BTFType.Info);
262 OS.emitInt32(BTFType.Size);
263}
264
266 bool NeedsFixup)
267 : DTy(DTy), NeedsFixup(NeedsFixup), Name(DTy->getName()) {
268 switch (Tag) {
269 case dwarf::DW_TAG_pointer_type:
270 Kind = BTF::BTF_KIND_PTR;
271 break;
272 case dwarf::DW_TAG_const_type:
273 Kind = BTF::BTF_KIND_CONST;
274 break;
275 case dwarf::DW_TAG_volatile_type:
276 Kind = BTF::BTF_KIND_VOLATILE;
277 break;
278 case dwarf::DW_TAG_typedef:
279 Kind = BTF::BTF_KIND_TYPEDEF;
280 break;
281 case dwarf::DW_TAG_restrict_type:
282 Kind = BTF::BTF_KIND_RESTRICT;
283 break;
284 default:
285 llvm_unreachable("Unknown DIDerivedType Tag");
286 }
287 BTFType.Info = Kind << 24;
288}
289
290/// Used by DW_TAG_pointer_type and DW_TAG_typedef only.
291BTFTypeDerived::BTFTypeDerived(unsigned NextTypeId, unsigned Tag,
292 StringRef Name)
293 : DTy(nullptr), NeedsFixup(false), Name(Name) {
294 switch (Tag) {
295 case dwarf::DW_TAG_pointer_type:
296 Kind = BTF::BTF_KIND_PTR;
297 break;
298 case dwarf::DW_TAG_typedef:
299 Kind = BTF::BTF_KIND_TYPEDEF;
300 break;
301 default:
302 llvm_unreachable("Tag must be pointer or typedef");
303 }
304
305 BTFType.Info = Kind << 24;
306 BTFType.Type = NextTypeId;
307}
308
310 if (IsCompleted)
311 return;
312 IsCompleted = true;
313
314 switch (Kind) {
315 case BTF::BTF_KIND_PTR:
316 case BTF::BTF_KIND_CONST:
317 case BTF::BTF_KIND_VOLATILE:
318 case BTF::BTF_KIND_RESTRICT:
319 // Debug info might contain names for these types, but given that we want
320 // to keep BTF minimal and naming reference types doesn't bring any value
321 // (what matters is the completeness of the base type), we don't emit them.
322 //
323 // Furthermore, the Linux kernel refuses to load BPF programs that contain
324 // BTF with these types named:
325 // https://elixir.bootlin.com/linux/v6.17.1/source/kernel/bpf/btf.c#L2586
326 BTFType.NameOff = 0;
327 break;
328 default:
329 BTFType.NameOff = BDebug.addString(Name);
330 break;
331 }
332
333 if (NeedsFixup || !DTy)
334 return;
335
336 // The base type for PTR/CONST/VOLATILE could be void.
337 const DIType *ResolvedType = tryRemoveAtomicType(DTy->getBaseType());
338 if (!ResolvedType) {
339 assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
340 Kind == BTF::BTF_KIND_VOLATILE) &&
341 "Invalid null basetype");
342 BTFType.Type = 0;
343 } else {
344 BTFType.Type = BDebug.getTypeId(ResolvedType);
345 }
346}
347
349
351 BTFType.Type = PointeeType;
352}
353
354/// Represent a struct/union forward declaration.
355BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) {
356 Kind = BTF::BTF_KIND_FWD;
357 BTFType.Info = IsUnion << 31 | Kind << 24;
358 BTFType.Type = 0;
359}
360
362 if (IsCompleted)
363 return;
364 IsCompleted = true;
365
366 BTFType.NameOff = BDebug.addString(Name);
367}
368
370
372 uint32_t OffsetInBits, StringRef TypeName)
373 : Name(TypeName) {
374 // Translate IR int encoding to BTF int encoding.
375 uint8_t BTFEncoding;
376 switch (Encoding) {
377 case dwarf::DW_ATE_boolean:
378 BTFEncoding = BTF::INT_BOOL;
379 break;
380 case dwarf::DW_ATE_signed:
381 case dwarf::DW_ATE_signed_char:
382 BTFEncoding = BTF::INT_SIGNED;
383 break;
384 case dwarf::DW_ATE_unsigned:
385 case dwarf::DW_ATE_unsigned_char:
386 case dwarf::DW_ATE_UTF:
387 BTFEncoding = 0;
388 break;
389 default:
390 llvm_unreachable("Unknown BTFTypeInt Encoding");
391 }
392
393 Kind = BTF::BTF_KIND_INT;
394 BTFType.Info = Kind << 24;
395 BTFType.Size = roundupToBytes(SizeInBits);
396 IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
397}
398
400 if (IsCompleted)
401 return;
402 IsCompleted = true;
403
404 BTFType.NameOff = BDebug.addString(Name);
405}
406
409 OS.AddComment("0x" + Twine::utohexstr(IntVal));
410 OS.emitInt32(IntVal);
411}
412
414 bool IsSigned) : ETy(ETy) {
415 Kind = BTF::BTF_KIND_ENUM;
416 BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
417 BTFType.Size = roundupToBytes(ETy->getSizeInBits());
418}
419
421 if (IsCompleted)
422 return;
423 IsCompleted = true;
424
425 BTFType.NameOff = BDebug.addString(ETy->getName());
426
427 DINodeArray Elements = ETy->getElements();
428 for (const auto Element : Elements) {
429 const auto *Enum = cast<DIEnumerator>(Element);
430
431 struct BTF::BTFEnum BTFEnum;
432 BTFEnum.NameOff = BDebug.addString(Enum->getName());
433 // BTF enum value is 32bit, enforce it.
435 if (Enum->isUnsigned())
436 Value = static_cast<uint32_t>(Enum->getValue().getZExtValue());
437 else
438 Value = static_cast<uint32_t>(Enum->getValue().getSExtValue());
439 BTFEnum.Val = Value;
440 EnumValues.push_back(BTFEnum);
441 }
442}
443
446 for (const auto &Enum : EnumValues) {
447 OS.emitInt32(Enum.NameOff);
448 OS.emitInt32(Enum.Val);
449 }
450}
451
453 bool IsSigned) : ETy(ETy) {
454 Kind = BTF::BTF_KIND_ENUM64;
455 BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
456 BTFType.Size = roundupToBytes(ETy->getSizeInBits());
457}
458
460 if (IsCompleted)
461 return;
462 IsCompleted = true;
463
464 BTFType.NameOff = BDebug.addString(ETy->getName());
465
466 DINodeArray Elements = ETy->getElements();
467 for (const auto Element : Elements) {
468 const auto *Enum = cast<DIEnumerator>(Element);
469
470 struct BTF::BTFEnum64 BTFEnum;
471 BTFEnum.NameOff = BDebug.addString(Enum->getName());
472 uint64_t Value;
473 if (Enum->isUnsigned())
474 Value = Enum->getValue().getZExtValue();
475 else
476 Value = static_cast<uint64_t>(Enum->getValue().getSExtValue());
477 BTFEnum.Val_Lo32 = Value;
478 BTFEnum.Val_Hi32 = Value >> 32;
479 EnumValues.push_back(BTFEnum);
480 }
481}
482
485 for (const auto &Enum : EnumValues) {
486 OS.emitInt32(Enum.NameOff);
487 OS.AddComment("0x" + Twine::utohexstr(Enum.Val_Lo32));
488 OS.emitInt32(Enum.Val_Lo32);
489 OS.AddComment("0x" + Twine::utohexstr(Enum.Val_Hi32));
490 OS.emitInt32(Enum.Val_Hi32);
491 }
492}
493
495 Kind = BTF::BTF_KIND_ARRAY;
496 BTFType.NameOff = 0;
497 BTFType.Info = Kind << 24;
498 BTFType.Size = 0;
499
500 ArrayInfo.ElemType = ElemTypeId;
501 ArrayInfo.Nelems = NumElems;
502}
503
504/// Represent a BTF array.
506 if (IsCompleted)
507 return;
508 IsCompleted = true;
509
510 // The IR does not really have a type for the index.
511 // A special type for array index should have been
512 // created during initial type traversal. Just
513 // retrieve that type id.
514 ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
515}
516
519 OS.emitInt32(ArrayInfo.ElemType);
520 OS.emitInt32(ArrayInfo.IndexType);
521 OS.emitInt32(ArrayInfo.Nelems);
522}
523
524/// Represent either a struct or a union.
526 ArrayRef<const DINode *> Elements, bool IsStruct,
527 bool HasBitField, uint32_t Vlen)
528 : STy(STy), Elements(Elements.begin(), Elements.end()),
529 HasBitField(HasBitField) {
530 Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
531 BTFType.Size = roundupToBytes(STy->getSizeInBits());
532 BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
533}
534
536 if (IsCompleted)
537 return;
538 IsCompleted = true;
539
540 BTFType.NameOff = BDebug.addString(STy->getName());
541
542 if (STy->getTag() == dwarf::DW_TAG_variant_part) {
543 // Variant parts might have a discriminator, which has its own memory
544 // location, and variants, which share the memory location afterwards. LLVM
545 // DI doesn't consider discriminator as an element and instead keeps
546 // it as a separate reference.
547 // To keep BTF simple, let's represent the structure as an union with
548 // discriminator as the first element.
549 // The offsets inside variant types are already handled correctly in the
550 // DI.
551 const auto *DTy = STy->getDiscriminator();
552 if (DTy) {
553 struct BTF::BTFMember Discriminator;
554
555 Discriminator.NameOff = BDebug.addString(DTy->getName());
556 Discriminator.Offset = DTy->getOffsetInBits();
557 const auto *BaseTy = DTy->getBaseType();
558 Discriminator.Type = BDebug.getTypeId(BaseTy);
559
560 Members.push_back(Discriminator);
561 }
562 }
563
564 // Add struct/union members.
565 for (const auto *Element : Elements) {
566 struct BTF::BTFMember BTFMember;
567
568 switch (Element->getTag()) {
569 case dwarf::DW_TAG_member: {
570 const auto *DDTy = cast<DIDerivedType>(Element);
571
572 BTFMember.NameOff = BDebug.addString(DDTy->getName());
573 if (HasBitField) {
574 uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
575 BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
576 } else {
577 BTFMember.Offset = DDTy->getOffsetInBits();
578 }
579 const auto *BaseTy = tryRemoveAtomicType(DDTy->getBaseType());
580 BTFMember.Type = BDebug.getTypeId(BaseTy);
581 break;
582 }
583 case dwarf::DW_TAG_variant_part: {
584 const auto *DCTy = dyn_cast<DICompositeType>(Element);
585
586 BTFMember.NameOff = BDebug.addString(DCTy->getName());
587 BTFMember.Offset = DCTy->getOffsetInBits();
588 BTFMember.Type = BDebug.getTypeId(DCTy);
589 break;
590 }
591 default:
592 llvm_unreachable("Unexpected DI tag of a struct/union element");
593 }
594 Members.push_back(BTFMember);
595 }
596}
597
600 for (const auto &Member : Members) {
601 OS.emitInt32(Member.NameOff);
602 OS.emitInt32(Member.Type);
603 OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
604 OS.emitInt32(Member.Offset);
605 }
606}
607
608std::string BTFTypeStruct::getName() { return std::string(STy->getName()); }
609
610/// The Func kind represents both subprogram and pointee of function
611/// pointers. If the FuncName is empty, it represents a pointee of function
612/// pointer. Otherwise, it represents a subprogram. The func arg names
613/// are empty for pointee of function pointer case, and are valid names
614/// for subprogram.
616 const DISubroutineType *STy, uint32_t VLen,
617 const SmallDenseMap<uint32_t, StringRef> &FuncArgNames,
618 bool UseFilteredParams, ArrayRef<uint32_t> AliveParamIndices,
619 bool VoidReturn)
620 : STy(STy), FuncArgNames(FuncArgNames),
621 AliveParamIndices(AliveParamIndices),
622 UseFilteredParams(UseFilteredParams), VoidReturn(VoidReturn) {
623 Kind = BTF::BTF_KIND_FUNC_PROTO;
624 BTFType.Info = (Kind << 24) | VLen;
625}
626
628 if (IsCompleted)
629 return;
630 IsCompleted = true;
631
632 DITypeArray Elements = STy->getTypeArray();
633 if (VoidReturn) {
634 BTFType.Type = 0;
635 } else {
636 auto RetType = tryRemoveAtomicType(Elements[0]);
637 BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
638 }
639 BTFType.NameOff = 0;
640
641 auto EmitParam = [&](uint32_t I) {
642 struct BTF::BTFParam Param;
643 auto Element = tryRemoveAtomicType(Elements[I]);
644 if (Element) {
645 auto It = FuncArgNames.find(I);
646 Param.NameOff =
647 It != FuncArgNames.end() ? BDebug.addString(It->second) : 0;
648 Param.Type = BDebug.getTypeId(Element);
649 } else {
650 Param.NameOff = 0;
651 Param.Type = 0;
652 }
653 Parameters.push_back(Param);
654 };
655
656 if (UseFilteredParams) {
657 for (uint32_t I : AliveParamIndices)
658 EmitParam(I);
659 return;
660 }
661
662 for (unsigned I = 1, N = Elements.size(); I < N; ++I)
663 EmitParam(I);
664}
665
668 for (const auto &Param : Parameters) {
669 OS.emitInt32(Param.NameOff);
670 OS.emitInt32(Param.Type);
671 }
672}
673
675 uint32_t Scope)
676 : Name(FuncName) {
677 Kind = BTF::BTF_KIND_FUNC;
678 BTFType.Info = (Kind << 24) | Scope;
679 BTFType.Type = ProtoTypeId;
680}
681
683 if (IsCompleted)
684 return;
685 IsCompleted = true;
686
687 BTFType.NameOff = BDebug.addString(Name);
688}
689
691
693 : Name(VarName) {
694 Kind = BTF::BTF_KIND_VAR;
695 BTFType.Info = Kind << 24;
696 BTFType.Type = TypeId;
697 Info = VarInfo;
698}
699
701 BTFType.NameOff = BDebug.addString(Name);
702}
703
706 OS.emitInt32(Info);
707}
708
709BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
710 : Asm(AsmPrt), Name(SecName) {
711 Kind = BTF::BTF_KIND_DATASEC;
712 BTFType.Info = Kind << 24;
713 BTFType.Size = 0;
714}
715
717 BTFType.NameOff = BDebug.addString(Name);
718 BTFType.Info |= Vars.size();
719}
720
723
724 for (const auto &V : Vars) {
725 OS.emitInt32(std::get<0>(V));
726 Asm->emitLabelReference(std::get<1>(V), 4);
727 OS.emitInt32(std::get<2>(V));
728 }
729}
730
732 : Name(TypeName) {
733 Kind = BTF::BTF_KIND_FLOAT;
734 BTFType.Info = Kind << 24;
735 BTFType.Size = roundupToBytes(SizeInBits);
736}
737
739 if (IsCompleted)
740 return;
741 IsCompleted = true;
742
743 BTFType.NameOff = BDebug.addString(Name);
744}
745
746BTFTypeDeclTag::BTFTypeDeclTag(uint32_t BaseTypeId, int ComponentIdx,
747 StringRef Tag)
748 : Tag(Tag) {
749 Kind = BTF::BTF_KIND_DECL_TAG;
750 BTFType.Info = Kind << 24;
751 BTFType.Type = BaseTypeId;
752 Info = ComponentIdx;
753}
754
756 if (IsCompleted)
757 return;
758 IsCompleted = true;
759
760 BTFType.NameOff = BDebug.addString(Tag);
761}
762
767
769 : DTy(nullptr), Tag(Tag) {
770 Kind = BTF::BTF_KIND_TYPE_TAG;
771 BTFType.Info = Kind << 24;
772 BTFType.Type = NextTypeId;
773}
774
776 : DTy(DTy), Tag(Tag) {
777 Kind = BTF::BTF_KIND_TYPE_TAG;
778 BTFType.Info = Kind << 24;
779}
780
782 if (IsCompleted)
783 return;
784 IsCompleted = true;
785 BTFType.NameOff = BDebug.addString(Tag);
786 if (DTy) {
787 const DIType *ResolvedType = tryRemoveAtomicType(DTy->getBaseType());
788 if (!ResolvedType)
789 BTFType.Type = 0;
790 else
791 BTFType.Type = BDebug.getTypeId(ResolvedType);
792 }
793}
794
796 // Check whether the string already exists.
797 for (auto &OffsetM : OffsetToIdMap) {
798 if (Table[OffsetM.second] == S)
799 return OffsetM.first;
800 }
801 // Not find, add to the string table.
802 uint32_t Offset = Size;
803 OffsetToIdMap[Offset] = Table.size();
804 Table.push_back(std::string(S));
805 Size += S.size() + 1;
806 return Offset;
807}
808
810 : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
811 LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
812 MapDefNotCollected(true) {
813 addString("\0");
814}
815
816uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
817 const DIType *Ty) {
818 TypeEntry->setId(TypeEntries.size() + 1);
819 uint32_t Id = TypeEntry->getId();
820 DIToIdMap[Ty] = Id;
821 TypeEntries.push_back(std::move(TypeEntry));
822 return Id;
823}
824
825uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
826 TypeEntry->setId(TypeEntries.size() + 1);
827 uint32_t Id = TypeEntry->getId();
828 TypeEntries.push_back(std::move(TypeEntry));
829 return Id;
830}
831
832void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
833 // Only int and binary floating point types are supported in BTF.
834 uint32_t Encoding = BTy->getEncoding();
835 std::unique_ptr<BTFTypeBase> TypeEntry;
836 switch (Encoding) {
837 case dwarf::DW_ATE_boolean:
838 case dwarf::DW_ATE_signed:
839 case dwarf::DW_ATE_signed_char:
840 case dwarf::DW_ATE_unsigned:
841 case dwarf::DW_ATE_unsigned_char:
842 case dwarf::DW_ATE_UTF:
843 // Create a BTF type instance for this DIBasicType and put it into
844 // DIToIdMap for cross-type reference check.
845 TypeEntry = std::make_unique<BTFTypeInt>(
846 Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
847 break;
848 case dwarf::DW_ATE_float:
849 TypeEntry =
850 std::make_unique<BTFTypeFloat>(BTy->getSizeInBits(), BTy->getName());
851 break;
852 default:
853 return;
854 }
855
856 TypeId = addType(std::move(TypeEntry), BTy);
857}
858
859/// Handle subprogram or subroutine types.
860void BTFDebug::visitSubroutineType(
861 const DISubroutineType *STy, bool ForSubprog,
862 const SmallDenseMap<uint32_t, StringRef> &FuncArgNames, uint32_t &TypeId,
863 bool VoidReturn) {
864 DITypeArray Elements = STy->getTypeArray();
865 uint32_t VLen = Elements.size() - 1;
866 if (VLen > BTF::MAX_VLEN)
867 return;
868
869 // Subprogram has a valid non-zero-length name, and the pointee of
870 // a function pointer has an empty name. The subprogram type will
871 // not be added to DIToIdMap as it should not be referenced by
872 // any other types.
873 auto TypeEntry = std::make_unique<BTFTypeFuncProto>(
874 STy, VLen, FuncArgNames, false, ArrayRef<uint32_t>(), VoidReturn);
875 if (ForSubprog)
876 TypeId = addType(std::move(TypeEntry)); // For subprogram
877 else
878 TypeId = addType(std::move(TypeEntry), STy); // For func ptr
879
880 // Visit return type and func arg types.
881 if (!VoidReturn) {
882 for (const auto Element : Elements)
883 visitTypeEntry(Element);
884 } else {
885 for (unsigned I = 1, N = Elements.size(); I < N; ++I)
886 visitTypeEntry(Elements[I]);
887 }
888}
889
890void BTFDebug::processDeclAnnotations(DINodeArray Annotations,
891 uint32_t BaseTypeId,
892 int ComponentIdx) {
893 if (!Annotations)
894 return;
895
896 for (const Metadata *Annotation : Annotations->operands()) {
897 const MDNode *MD = cast<MDNode>(Annotation);
898 const MDString *Name = cast<MDString>(MD->getOperand(0));
899 if (Name->getString() != "btf_decl_tag")
900 continue;
901
902 const MDString *Value = cast<MDString>(MD->getOperand(1));
903 auto TypeEntry = std::make_unique<BTFTypeDeclTag>(BaseTypeId, ComponentIdx,
904 Value->getString());
905 addType(std::move(TypeEntry));
906 }
907}
908
909uint32_t BTFDebug::processDISubprogram(
910 const DISubprogram *SP, uint32_t ProtoTypeId, uint8_t Scope,
911 const SmallDenseMap<uint32_t, uint32_t> *ArgIndexMap) {
912 auto FuncTypeEntry =
913 std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
914 uint32_t FuncId = addType(std::move(FuncTypeEntry));
915
916 // Process argument annotations.
917 for (const MDNode *DN : SP->getRetainedNodes()) {
918 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
919 uint32_t Arg = DV->getArg();
920 if (Arg) {
921 if (ArgIndexMap) {
922 auto It = ArgIndexMap->find(Arg);
923 if (It != ArgIndexMap->end())
924 processDeclAnnotations(DV->getAnnotations(), FuncId, It->second);
925 } else {
926 processDeclAnnotations(DV->getAnnotations(), FuncId, Arg - 1);
927 }
928 }
929 }
930 }
931 processDeclAnnotations(SP->getAnnotations(), FuncId, -1);
932
933 return FuncId;
934}
935
936/// Generate btf_type_tag chains.
937int BTFDebug::genBTFTypeTags(const DIDerivedType *DTy, int BaseTypeId) {
939 DINodeArray Annots = DTy->getAnnotations();
940 if (Annots) {
941 // For type with "int __tag1 __tag2 *p", the MDStrs will have
942 // content: [__tag1, __tag2].
943 for (const Metadata *Annotations : Annots->operands()) {
944 const MDNode *MD = cast<MDNode>(Annotations);
945 const MDString *Name = cast<MDString>(MD->getOperand(0));
946 if (Name->getString() != "btf_type_tag")
947 continue;
948 MDStrs.push_back(cast<MDString>(MD->getOperand(1)));
949 }
950 }
951
952 if (MDStrs.size() == 0)
953 return -1;
954
955 // With MDStrs [__tag1, __tag2], the output type chain looks like
956 // PTR -> __tag2 -> __tag1 -> BaseType
957 // In the below, we construct BTF types with the order of __tag1, __tag2
958 // and PTR.
959 unsigned TmpTypeId;
960 std::unique_ptr<BTFTypeTypeTag> TypeEntry;
961 if (BaseTypeId >= 0)
962 TypeEntry =
963 std::make_unique<BTFTypeTypeTag>(BaseTypeId, MDStrs[0]->getString());
964 else
965 TypeEntry = std::make_unique<BTFTypeTypeTag>(DTy, MDStrs[0]->getString());
966 TmpTypeId = addType(std::move(TypeEntry));
967
968 for (unsigned I = 1; I < MDStrs.size(); I++) {
969 const MDString *Value = MDStrs[I];
970 TypeEntry = std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString());
971 TmpTypeId = addType(std::move(TypeEntry));
972 }
973 return TmpTypeId;
974}
975
976/// Handle structure/union types.
977void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
978 uint32_t &TypeId) {
979 DINodeArray DIElements = CTy->getElements();
980 SmallVector<const DINode *, 8> Elements(DIElements.begin(), DIElements.end());
981 // Structure elements must have nondecreasing offsets in BTF. Preserve DI
982 // order for union and variant-part records.
983 if (CTy->getTag() == dwarf::DW_TAG_structure_type)
984 llvm::stable_sort(Elements, [](const DINode *LHS, const DINode *RHS) {
986 });
987 uint32_t VLen = Elements.size();
988 // Variant parts might have a discriminator. LLVM DI doesn't consider it as
989 // an element and instead keeps it as a separate reference. But we represent
990 // it as an element in BTF.
991 if (CTy->getTag() == dwarf::DW_TAG_variant_part) {
992 const auto *DTy = CTy->getDiscriminator();
993 if (DTy) {
994 visitTypeEntry(DTy);
995 VLen++;
996 }
997 }
998 if (VLen > BTF::MAX_VLEN)
999 return;
1000
1001 // Check whether we have any bitfield members or not
1002 bool HasBitField = false;
1003 for (const auto *Element : Elements) {
1004 if (Element->getTag() == dwarf::DW_TAG_member) {
1005 auto E = cast<DIDerivedType>(Element);
1006 if (E->isBitField()) {
1007 HasBitField = true;
1008 break;
1009 }
1010 }
1011 }
1012
1013 auto TypeEntry = std::make_unique<BTFTypeStruct>(CTy, Elements, IsStruct,
1014 HasBitField, VLen);
1015 StructTypes.push_back(TypeEntry.get());
1016 TypeId = addType(std::move(TypeEntry), CTy);
1017
1018 // Check struct/union annotations
1019 processDeclAnnotations(CTy->getAnnotations(), TypeId, -1);
1020
1021 // Visit all struct members.
1022 int FieldNo = 0;
1023 for (const auto *Element : Elements) {
1024 switch (Element->getTag()) {
1025 case dwarf::DW_TAG_member: {
1026 const auto Elem = cast<DIDerivedType>(Element);
1027 visitTypeEntry(Elem);
1028 processDeclAnnotations(Elem->getAnnotations(), TypeId, FieldNo);
1029 break;
1030 }
1031 case dwarf::DW_TAG_variant_part: {
1032 const auto Elem = cast<DICompositeType>(Element);
1033 visitTypeEntry(Elem);
1034 processDeclAnnotations(Elem->getAnnotations(), TypeId, FieldNo);
1035 break;
1036 }
1037 default:
1038 llvm_unreachable("Unexpected DI tag of a struct/union element");
1039 }
1040 FieldNo++;
1041 }
1042}
1043
1044void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
1045 // Visit array element type.
1046 uint32_t ElemTypeId;
1047 const DIType *ElemType = CTy->getBaseType();
1048 visitTypeEntry(ElemType, ElemTypeId, false, false);
1049
1050 // Visit array dimensions.
1051 DINodeArray Elements = CTy->getElements();
1052 if (Elements.size() == 0) {
1053 // Rust and other languages may emit array types with no dimensions.
1054 // Treat as a zero-length array so the type is still registered.
1055 auto TypeEntry = std::make_unique<BTFTypeArray>(ElemTypeId, 0);
1056 ElemTypeId = addType(std::move(TypeEntry), CTy);
1057 }
1058 for (int I = Elements.size() - 1; I >= 0; --I) {
1059 if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
1060 if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
1061 const DISubrange *SR = cast<DISubrange>(Element);
1062 auto *CI = dyn_cast<ConstantInt *>(SR->getCount());
1063 int64_t Count = CI->getSExtValue();
1064
1065 // For struct s { int b; char c[]; }, the c[] will be represented
1066 // as an array with Count = -1.
1067 auto TypeEntry =
1068 std::make_unique<BTFTypeArray>(ElemTypeId,
1069 Count >= 0 ? Count : 0);
1070 if (I == 0)
1071 ElemTypeId = addType(std::move(TypeEntry), CTy);
1072 else
1073 ElemTypeId = addType(std::move(TypeEntry));
1074 }
1075 }
1076
1077 // The array TypeId is the type id of the outermost dimension.
1078 TypeId = ElemTypeId;
1079
1080 // The IR does not have a type for array index while BTF wants one.
1081 // So create an array index type if there is none.
1082 if (!ArrayIndexTypeId) {
1083 auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
1084 0, "__ARRAY_SIZE_TYPE__");
1085 ArrayIndexTypeId = addType(std::move(TypeEntry));
1086 }
1087}
1088
1089void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
1090 DINodeArray Elements = CTy->getElements();
1091 uint32_t VLen = Elements.size();
1092 if (VLen > BTF::MAX_VLEN)
1093 return;
1094
1095 bool IsSigned = false;
1096 unsigned NumBits = 32;
1097 // No BaseType implies forward declaration in which case a
1098 // BTFTypeEnum with Vlen = 0 is emitted.
1099 if (CTy->getBaseType() != nullptr) {
1100 const auto *BTy = cast<DIBasicType>(CTy->getBaseType());
1101 IsSigned = BTy->getEncoding() == dwarf::DW_ATE_signed ||
1102 BTy->getEncoding() == dwarf::DW_ATE_signed_char;
1103 NumBits = BTy->getSizeInBits();
1104 }
1105
1106 if (NumBits <= 32) {
1107 auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen, IsSigned);
1108 TypeId = addType(std::move(TypeEntry), CTy);
1109 } else {
1110 assert(NumBits == 64);
1111 auto TypeEntry = std::make_unique<BTFTypeEnum64>(CTy, VLen, IsSigned);
1112 TypeId = addType(std::move(TypeEntry), CTy);
1113 }
1114 // No need to visit base type as BTF does not encode it.
1115}
1116
1117/// Handle structure/union forward declarations.
1118void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
1119 uint32_t &TypeId) {
1120 auto TypeEntry = std::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
1121 TypeId = addType(std::move(TypeEntry), CTy);
1122}
1123
1124/// Handle structure, union, array and enumeration types.
1125void BTFDebug::visitCompositeType(const DICompositeType *CTy,
1126 uint32_t &TypeId) {
1127 auto Tag = CTy->getTag();
1128 switch (Tag) {
1129 case dwarf::DW_TAG_structure_type:
1130 case dwarf::DW_TAG_union_type:
1131 case dwarf::DW_TAG_variant_part:
1132 // Handle forward declaration differently as it does not have members.
1133 if (CTy->isForwardDecl())
1134 visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
1135 else
1136 visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
1137 break;
1138 case dwarf::DW_TAG_array_type:
1139 visitArrayType(CTy, TypeId);
1140 break;
1141 case dwarf::DW_TAG_enumeration_type:
1142 visitEnumType(CTy, TypeId);
1143 break;
1144 default:
1145 llvm_unreachable("Unexpected DI tag of a composite type");
1146 }
1147}
1148
1149bool BTFDebug::IsForwardDeclCandidate(const DIType *Base) {
1150 if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
1151 auto CTag = CTy->getTag();
1152 if ((CTag == dwarf::DW_TAG_structure_type ||
1153 CTag == dwarf::DW_TAG_union_type) &&
1154 !CTy->getName().empty() && !CTy->isForwardDecl())
1155 return true;
1156 }
1157 return false;
1158}
1159
1160/// Handle pointer, typedef, const, volatile, restrict and member types.
1161void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
1162 bool CheckPointer, bool SeenPointer) {
1163 unsigned Tag = DTy->getTag();
1164
1165 if (Tag == dwarf::DW_TAG_atomic_type)
1166 return visitTypeEntry(DTy->getBaseType(), TypeId, CheckPointer,
1167 SeenPointer);
1168
1169 /// Try to avoid chasing pointees, esp. structure pointees which may
1170 /// unnecessary bring in a lot of types.
1171 if (CheckPointer && !SeenPointer) {
1172 SeenPointer = Tag == dwarf::DW_TAG_pointer_type && !DTy->getAnnotations();
1173 }
1174
1175 if (CheckPointer && SeenPointer) {
1176 const DIType *Base = DTy->getBaseType();
1177 if (Base) {
1178 if (IsForwardDeclCandidate(Base)) {
1179 /// Find a candidate, generate a fixup. Later on the struct/union
1180 /// pointee type will be replaced with either a real type or
1181 /// a forward declaration.
1182 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true);
1183 auto &Fixup = FixupDerivedTypes[cast<DICompositeType>(Base)];
1184 Fixup.push_back(std::make_pair(DTy, TypeEntry.get()));
1185 TypeId = addType(std::move(TypeEntry), DTy);
1186 return;
1187 }
1188 }
1189 }
1190
1191 if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef) {
1192 int TmpTypeId = genBTFTypeTags(DTy, -1);
1193 if (TmpTypeId >= 0) {
1194 auto TypeDEntry =
1195 std::make_unique<BTFTypeDerived>(TmpTypeId, Tag, DTy->getName());
1196 TypeId = addType(std::move(TypeDEntry), DTy);
1197 } else {
1198 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
1199 TypeId = addType(std::move(TypeEntry), DTy);
1200 }
1201 if (Tag == dwarf::DW_TAG_typedef)
1202 processDeclAnnotations(DTy->getAnnotations(), TypeId, -1);
1203 } else if (Tag == dwarf::DW_TAG_const_type ||
1204 Tag == dwarf::DW_TAG_volatile_type ||
1205 Tag == dwarf::DW_TAG_restrict_type) {
1206 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
1207 TypeId = addType(std::move(TypeEntry), DTy);
1208 } else if (Tag != dwarf::DW_TAG_member) {
1209 return;
1210 }
1211
1212 // Visit base type of pointer, typedef, const, volatile, restrict or
1213 // struct/union member.
1214 uint32_t TempTypeId = 0;
1215 if (Tag == dwarf::DW_TAG_member)
1216 visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
1217 else
1218 visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
1219}
1220
1221/// Visit a type entry. CheckPointer is true if the type has
1222/// one of its predecessors as one struct/union member. SeenPointer
1223/// is true if CheckPointer is true and one of its predecessors
1224/// is a pointer. The goal of CheckPointer and SeenPointer is to
1225/// do pruning for struct/union types so some of these types
1226/// will not be emitted in BTF and rather forward declarations
1227/// will be generated.
1228void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
1229 bool CheckPointer, bool SeenPointer) {
1230 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
1231 TypeId = DIToIdMap[Ty];
1232
1233 // To handle the case like the following:
1234 // struct t;
1235 // typedef struct t _t;
1236 // struct s1 { _t *c; };
1237 // int test1(struct s1 *arg) { ... }
1238 //
1239 // struct t { int a; int b; };
1240 // struct s2 { _t c; }
1241 // int test2(struct s2 *arg) { ... }
1242 //
1243 // During traversing test1() argument, "_t" is recorded
1244 // in DIToIdMap and a forward declaration fixup is created
1245 // for "struct t" to avoid pointee type traversal.
1246 //
1247 // During traversing test2() argument, even if we see "_t" is
1248 // already defined, we should keep moving to eventually
1249 // bring in types for "struct t". Otherwise, the "struct s2"
1250 // definition won't be correct.
1251 //
1252 // In the above, we have following debuginfo:
1253 // {ptr, struct_member} -> typedef -> struct
1254 // and BTF type for 'typedef' is generated while 'struct' may
1255 // be in FixUp. But let us generalize the above to handle
1256 // {different types} -> [various derived types]+ -> another type.
1257 // For example,
1258 // {func_param, struct_member} -> const -> ptr -> volatile -> struct
1259 // We will traverse const/ptr/volatile which already have corresponding
1260 // BTF types and generate type for 'struct' which might be in Fixup
1261 // state.
1262 if (Ty && (!CheckPointer || !SeenPointer)) {
1263 if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
1264 while (DTy) {
1265 const DIType *BaseTy = DTy->getBaseType();
1266 if (!BaseTy)
1267 break;
1268
1269 if (DIToIdMap.find(BaseTy) != DIToIdMap.end()) {
1270 DTy = dyn_cast<DIDerivedType>(BaseTy);
1271 } else {
1272 if (CheckPointer && DTy->getTag() == dwarf::DW_TAG_pointer_type &&
1273 !DTy->getAnnotations()) {
1274 SeenPointer = true;
1275 if (IsForwardDeclCandidate(BaseTy))
1276 break;
1277 }
1278 uint32_t TmpTypeId;
1279 visitTypeEntry(BaseTy, TmpTypeId, CheckPointer, SeenPointer);
1280 break;
1281 }
1282 }
1283 }
1284 }
1285
1286 return;
1287 }
1288
1289 if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
1290 visitBasicType(BTy, TypeId);
1291 else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
1292 visitSubroutineType(STy, false, SmallDenseMap<uint32_t, StringRef>(),
1293 TypeId);
1294 else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
1295 visitCompositeType(CTy, TypeId);
1296 else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
1297 visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
1298 else
1299 llvm_unreachable("Unknown DIType");
1300}
1301
1302void BTFDebug::visitTypeEntry(const DIType *Ty) {
1303 uint32_t TypeId;
1304 visitTypeEntry(Ty, TypeId, false, false);
1305}
1306
1307void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
1308 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
1309 TypeId = DIToIdMap[Ty];
1310 return;
1311 }
1312
1313 uint32_t TmpId;
1314 switch (Ty->getTag()) {
1315 case dwarf::DW_TAG_typedef:
1316 case dwarf::DW_TAG_const_type:
1317 case dwarf::DW_TAG_volatile_type:
1318 case dwarf::DW_TAG_restrict_type:
1319 case dwarf::DW_TAG_pointer_type:
1320 visitMapDefType(dyn_cast<DIDerivedType>(Ty)->getBaseType(), TmpId);
1321 break;
1322 case dwarf::DW_TAG_array_type:
1323 // Visit nested map array and jump to the element type
1324 visitMapDefType(dyn_cast<DICompositeType>(Ty)->getBaseType(), TmpId);
1325 break;
1326 case dwarf::DW_TAG_structure_type: {
1327 // Visit all struct members to ensure their types are visited.
1328 const auto *CTy = cast<DICompositeType>(Ty);
1329 const DINodeArray Elements = CTy->getElements();
1330 for (const auto *Element : Elements) {
1331 const auto *MemberType = cast<DIDerivedType>(Element);
1332 const DIType *MemberBaseType = MemberType->getBaseType();
1333 // If the member is a composite type, that may indicate the currently
1334 // visited composite type is a wrapper, and the member represents the
1335 // actual map definition.
1336 // In that case, visit the member with `visitMapDefType` instead of
1337 // `visitTypeEntry`, treating it specifically as a map definition rather
1338 // than as a regular composite type.
1339 const auto *MemberCTy = dyn_cast<DICompositeType>(MemberBaseType);
1340 if (MemberCTy) {
1341 visitMapDefType(MemberBaseType, TmpId);
1342 } else {
1343 visitTypeEntry(MemberBaseType);
1344 }
1345 }
1346 break;
1347 }
1348 default:
1349 break;
1350 }
1351
1352 // Visit this type, struct or a const/typedef/volatile/restrict type
1353 visitTypeEntry(Ty, TypeId, false, false);
1354}
1355
1356/// Read file contents from the actual file or from the source
1357std::string BTFDebug::populateFileContent(const DIFile *File) {
1358 std::string FileName;
1359
1360 if (!File->getFilename().starts_with("/") && File->getDirectory().size())
1361 FileName = File->getDirectory().str() + "/" + File->getFilename().str();
1362 else
1363 FileName = std::string(File->getFilename());
1364
1365 // No need to populate the contends if it has been populated!
1366 if (FileContent.contains(FileName))
1367 return FileName;
1368
1369 std::vector<std::string> Content;
1370 std::string Line;
1371 Content.push_back(Line); // Line 0 for empty string
1372
1373 auto LoadFile = [](StringRef FileName) {
1374 // FIXME(sandboxing): Propagating vfs::FileSystem here is lots of work.
1375 auto BypassSandbox = sys::sandbox::scopedDisable();
1376 return MemoryBuffer::getFile(FileName);
1377 };
1378
1379 std::unique_ptr<MemoryBuffer> Buf;
1380 auto Source = File->getSource();
1381 if (Source)
1382 Buf = MemoryBuffer::getMemBufferCopy(*Source);
1383 else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = LoadFile(FileName))
1384 Buf = std::move(*BufOrErr);
1385 if (Buf)
1386 for (line_iterator I(*Buf, false), E; I != E; ++I)
1387 Content.push_back(std::string(*I));
1388
1389 FileContent[FileName] = std::move(Content);
1390 return FileName;
1391}
1392
1393void BTFDebug::constructLineInfo(MCSymbol *Label, const DIFile *File,
1394 uint32_t Line, uint32_t Column) {
1395 std::string FileName = populateFileContent(File);
1396 BTFLineInfo LineInfo;
1397
1398 LineInfo.Label = Label;
1399 LineInfo.FileNameOff = addString(FileName);
1400 // If file content is not available, let LineOff = 0.
1401 const auto &Content = FileContent[FileName];
1402 if (Line < Content.size())
1403 LineInfo.LineOff = addString(Content[Line]);
1404 else
1405 LineInfo.LineOff = 0;
1406 LineInfo.LineNum = Line;
1407 LineInfo.ColumnNum = Column;
1408 LineInfoTable[SecNameOff].push_back(LineInfo);
1409}
1410
1411void BTFDebug::emitCommonHeader() {
1412 OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
1413 OS.emitIntValue(BTF::MAGIC, 2);
1414 OS.emitInt8(BTF::VERSION);
1415 OS.emitInt8(0);
1416}
1417
1418void BTFDebug::emitBTFSection() {
1419 // Do not emit section if no types and only "" string.
1420 if (!TypeEntries.size() && StringTable.getSize() == 1)
1421 return;
1422
1423 MCContext &Ctx = OS.getContext();
1424 MCSectionELF *Sec = Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0);
1425 Sec->setAlignment(Align(4));
1426 OS.switchSection(Sec);
1427
1428 // Emit header.
1429 emitCommonHeader();
1430 OS.emitInt32(BTF::HeaderSize);
1431
1432 uint32_t TypeLen = 0, StrLen;
1433 for (const auto &TypeEntry : TypeEntries)
1434 TypeLen += TypeEntry->getSize();
1435 StrLen = StringTable.getSize();
1436
1437 OS.emitInt32(0);
1438 OS.emitInt32(TypeLen);
1439 OS.emitInt32(TypeLen);
1440 OS.emitInt32(StrLen);
1441
1442 // Emit type table.
1443 for (const auto &TypeEntry : TypeEntries)
1444 TypeEntry->emitType(OS);
1445
1446 // Emit string table.
1447 uint32_t StringOffset = 0;
1448 for (const auto &S : StringTable.getTable()) {
1449 OS.AddComment("string offset=" + std::to_string(StringOffset));
1450 OS.emitBytes(S);
1451 OS.emitBytes(StringRef("\0", 1));
1452 StringOffset += S.size() + 1;
1453 }
1454}
1455
1456void BTFDebug::emitBTFExtSection() {
1457 // Do not emit section if empty FuncInfoTable and LineInfoTable
1458 // and FieldRelocTable.
1459 if (!FuncInfoTable.size() && !LineInfoTable.size() &&
1460 !FieldRelocTable.size())
1461 return;
1462
1463 MCContext &Ctx = OS.getContext();
1464 MCSectionELF *Sec = Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0);
1465 Sec->setAlignment(Align(4));
1466 OS.switchSection(Sec);
1467
1468 // Emit header.
1469 emitCommonHeader();
1470 OS.emitInt32(BTF::ExtHeaderSize);
1471
1472 // Account for FuncInfo/LineInfo record size as well.
1473 uint32_t FuncLen = 4, LineLen = 4;
1474 // Do not account for optional FieldReloc.
1475 uint32_t FieldRelocLen = 0;
1476 for (const auto &FuncSec : FuncInfoTable) {
1477 FuncLen += BTF::SecFuncInfoSize;
1478 FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
1479 }
1480 for (const auto &LineSec : LineInfoTable) {
1481 LineLen += BTF::SecLineInfoSize;
1482 LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
1483 }
1484 for (const auto &FieldRelocSec : FieldRelocTable) {
1485 FieldRelocLen += BTF::SecFieldRelocSize;
1486 FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize;
1487 }
1488
1489 if (FieldRelocLen)
1490 FieldRelocLen += 4;
1491
1492 OS.emitInt32(0);
1493 OS.emitInt32(FuncLen);
1494 OS.emitInt32(FuncLen);
1495 OS.emitInt32(LineLen);
1496 OS.emitInt32(FuncLen + LineLen);
1497 OS.emitInt32(FieldRelocLen);
1498
1499 // Emit func_info table.
1500 OS.AddComment("FuncInfo");
1501 OS.emitInt32(BTF::BPFFuncInfoSize);
1502 for (const auto &FuncSec : FuncInfoTable) {
1503 OS.AddComment("FuncInfo section string offset=" +
1504 std::to_string(FuncSec.first));
1505 OS.emitInt32(FuncSec.first);
1506 OS.emitInt32(FuncSec.second.size());
1507 for (const auto &FuncInfo : FuncSec.second) {
1508 Asm->emitLabelReference(FuncInfo.Label, 4);
1509 OS.emitInt32(FuncInfo.TypeId);
1510 }
1511 }
1512
1513 // Emit line_info table.
1514 OS.AddComment("LineInfo");
1515 OS.emitInt32(BTF::BPFLineInfoSize);
1516 for (const auto &LineSec : LineInfoTable) {
1517 OS.AddComment("LineInfo section string offset=" +
1518 std::to_string(LineSec.first));
1519 OS.emitInt32(LineSec.first);
1520 OS.emitInt32(LineSec.second.size());
1521 for (const auto &LineInfo : LineSec.second) {
1522 Asm->emitLabelReference(LineInfo.Label, 4);
1523 OS.emitInt32(LineInfo.FileNameOff);
1524 OS.emitInt32(LineInfo.LineOff);
1525 OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
1526 std::to_string(LineInfo.ColumnNum));
1527 OS.emitInt32(LineInfo.LineNum << 10 | LineInfo.ColumnNum);
1528 }
1529 }
1530
1531 // Emit field reloc table.
1532 if (FieldRelocLen) {
1533 OS.AddComment("FieldReloc");
1534 OS.emitInt32(BTF::BPFFieldRelocSize);
1535 for (const auto &FieldRelocSec : FieldRelocTable) {
1536 OS.AddComment("Field reloc section string offset=" +
1537 std::to_string(FieldRelocSec.first));
1538 OS.emitInt32(FieldRelocSec.first);
1539 OS.emitInt32(FieldRelocSec.second.size());
1540 for (const auto &FieldRelocInfo : FieldRelocSec.second) {
1541 Asm->emitLabelReference(FieldRelocInfo.Label, 4);
1542 OS.emitInt32(FieldRelocInfo.TypeID);
1543 OS.emitInt32(FieldRelocInfo.OffsetNameOff);
1544 OS.emitInt32(FieldRelocInfo.RelocKind);
1545 }
1546 }
1547 }
1548}
1549
1551 auto *SP = MF->getFunction().getSubprogram();
1552 auto *Unit = SP->getUnit();
1553
1554 if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
1555 SkipInstruction = true;
1556 return;
1557 }
1558 SkipInstruction = false;
1559
1560 // Collect MapDef types. Map definition needs to collect
1561 // pointee types. Do it first. Otherwise, for the following
1562 // case:
1563 // struct m { ...};
1564 // struct t {
1565 // struct m *key;
1566 // };
1567 // foo(struct t *arg);
1568 //
1569 // struct mapdef {
1570 // ...
1571 // struct m *key;
1572 // ...
1573 // } __attribute__((section(".maps"))) hash_map;
1574 //
1575 // If subroutine foo is traversed first, a type chain
1576 // "ptr->struct m(fwd)" will be created and later on
1577 // when traversing mapdef, since "ptr->struct m" exists,
1578 // the traversal of "struct m" will be omitted.
1579 if (MapDefNotCollected) {
1580 processGlobals(true);
1581 MapDefNotCollected = false;
1582 }
1583
1584 // Collect all types locally referenced in this function.
1585 // Use RetainedNodes so we can collect all argument names
1586 // even if the argument is not used.
1588 for (const MDNode *DN : SP->getRetainedNodes()) {
1589 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
1590 // Collect function arguments for subprogram func type.
1591 uint32_t Arg = DV->getArg();
1592 if (Arg) {
1593 visitTypeEntry(DV->getType());
1594 FuncArgNames[Arg] = DV->getName();
1595 }
1596 }
1597 }
1598
1599 // Construct subprogram func proto type.
1600 uint32_t ProtoTypeId, FuncTypeId;
1601 uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL;
1602 bool IsNocall = SP->getType()->getCC() == dwarf::DW_CC_nocall;
1603 bool UseFilteredParams = false;
1604 bool VoidReturn = MF->getFunction().getReturnType()->isVoidTy();
1605
1606 if (IsNocall) {
1607 // For DW_CC_nocall functions, try to build a FUNC_PROTO reflecting
1608 // the true ABI: only parameters that survived optimization and whose
1609 // first 5 arguments map to the correct BPF registers (R1-R5).
1611 DITypeArray Elements = SP->getType()->getTypeArray();
1612
1615
1616 UseFilteredParams =
1617 canUseNocallOptimizedSignature(*MF, Elements, AliveArgs, *TRI);
1618
1619 if (UseFilteredParams) {
1620 SmallVector<uint32_t, 8> AliveParamIndices;
1622 for (auto [I, ArgReg] : llvm::enumerate(AliveArgs)) {
1623 AliveParamIndices.push_back(ArgReg.first);
1624 ArgIndexMap[ArgReg.first] = I;
1625 }
1626
1627 if (!VoidReturn)
1628 visitTypeEntry(Elements[0]);
1629 for (uint32_t ArgNo : AliveParamIndices)
1630 visitTypeEntry(Elements[ArgNo]);
1631
1632 auto TypeEntry = std::make_unique<BTFTypeFuncProto>(
1633 SP->getType(), AliveParamIndices.size(), FuncArgNames, true,
1634 AliveParamIndices, VoidReturn);
1635 ProtoTypeId = addType(std::move(TypeEntry));
1636 FuncTypeId = processDISubprogram(SP, ProtoTypeId, Scope, &ArgIndexMap);
1637 }
1638 }
1639
1640 if (!UseFilteredParams) {
1641 // Fall back to the full source prototype, still voiding the return
1642 // type if compiler removed it.
1643 visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId,
1644 VoidReturn);
1645 FuncTypeId = processDISubprogram(SP, ProtoTypeId, Scope);
1646 }
1647
1648 for (const auto &TypeEntry : TypeEntries)
1649 TypeEntry->completeType(*this);
1650
1651 // Construct funcinfo and the first lineinfo for the function.
1652 MCSymbol *FuncLabel = Asm->getFunctionBegin();
1653 BTFFuncInfo FuncInfo;
1654 FuncInfo.Label = FuncLabel;
1655 FuncInfo.TypeId = FuncTypeId;
1656 if (FuncLabel->isInSection()) {
1657 auto &Sec = static_cast<const MCSectionELF &>(FuncLabel->getSection());
1658 SecNameOff = addString(Sec.getName());
1659 } else {
1660 SecNameOff = addString(".text");
1661 }
1662 FuncInfoTable[SecNameOff].push_back(FuncInfo);
1663}
1664
1666 SkipInstruction = false;
1667 LineInfoGenerated = false;
1668 SecNameOff = 0;
1669}
1670
1671/// On-demand populate types as requested from abstract member
1672/// accessing or preserve debuginfo type.
1673unsigned BTFDebug::populateType(const DIType *Ty) {
1674 unsigned Id;
1675 visitTypeEntry(Ty, Id, false, false);
1676 for (const auto &TypeEntry : TypeEntries)
1677 TypeEntry->completeType(*this);
1678 return Id;
1679}
1680
1681/// Generate a struct member field relocation.
1682void BTFDebug::generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
1683 const GlobalVariable *GVar, bool IsAma) {
1684 BTFFieldReloc FieldReloc;
1685 FieldReloc.Label = ORSym;
1686 FieldReloc.TypeID = RootId;
1687
1688 StringRef AccessPattern = GVar->getName();
1689 size_t FirstDollar = AccessPattern.find_first_of('$');
1690 if (IsAma) {
1691 size_t FirstColon = AccessPattern.find_first_of(':');
1692 size_t SecondColon = AccessPattern.find_first_of(':', FirstColon + 1);
1693 StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1);
1694 StringRef RelocKindStr = AccessPattern.substr(FirstColon + 1,
1695 SecondColon - FirstColon);
1696 StringRef PatchImmStr = AccessPattern.substr(SecondColon + 1,
1697 FirstDollar - SecondColon);
1698
1699 FieldReloc.OffsetNameOff = addString(IndexPattern);
1700 FieldReloc.RelocKind = std::stoull(std::string(RelocKindStr));
1701 PatchImms[GVar] = std::make_pair(std::stoll(std::string(PatchImmStr)),
1702 FieldReloc.RelocKind);
1703 } else {
1704 StringRef RelocStr = AccessPattern.substr(FirstDollar + 1);
1705 FieldReloc.OffsetNameOff = addString("0");
1706 FieldReloc.RelocKind = std::stoull(std::string(RelocStr));
1707 PatchImms[GVar] = std::make_pair(RootId, FieldReloc.RelocKind);
1708 }
1709 FieldRelocTable[SecNameOff].push_back(FieldReloc);
1710}
1711
1712void BTFDebug::processGlobalValue(const MachineOperand &MO) {
1713 // check whether this is a candidate or not
1714 if (MO.isGlobal()) {
1715 const GlobalValue *GVal = MO.getGlobal();
1716 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1717 if (!GVar) {
1718 // Not a global variable. Maybe an extern function reference.
1719 processFuncPrototypes(dyn_cast<Function>(GVal));
1720 return;
1721 }
1722
1725 return;
1726
1727 MCSymbol *ORSym = OS.getContext().createTempSymbol();
1728 OS.emitLabel(ORSym);
1729
1730 MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1731 uint32_t RootId = populateType(dyn_cast<DIType>(MDN));
1732 generatePatchImmReloc(ORSym, RootId, GVar,
1734 }
1735}
1736
1739
1740 if (SkipInstruction || MI->isMetaInstruction() ||
1741 MI->getFlag(MachineInstr::FrameSetup))
1742 return;
1743
1744 if (MI->isInlineAsm()) {
1745 // Count the number of register definitions to find the asm string.
1746 unsigned NumDefs = 0;
1747 while (true) {
1748 const MachineOperand &MO = MI->getOperand(NumDefs);
1749 if (MO.isReg() && MO.isDef()) {
1750 ++NumDefs;
1751 continue;
1752 }
1753 // Skip this inline asm instruction if the asmstr is empty.
1754 const char *AsmStr = MO.getSymbolName();
1755 if (AsmStr[0] == 0)
1756 return;
1757 break;
1758 }
1759 }
1760
1761 if (MI->getOpcode() == BPF::LD_imm64) {
1762 // If the insn is "r2 = LD_imm64 @<an AmaAttr global>",
1763 // add this insn into the .BTF.ext FieldReloc subsection.
1764 // Relocation looks like:
1765 // . SecName:
1766 // . InstOffset
1767 // . TypeID
1768 // . OffSetNameOff
1769 // . RelocType
1770 // Later, the insn is replaced with "r2 = <offset>"
1771 // where "<offset>" equals to the offset based on current
1772 // type definitions.
1773 //
1774 // If the insn is "r2 = LD_imm64 @<an TypeIdAttr global>",
1775 // The LD_imm64 result will be replaced with a btf type id.
1776 processGlobalValue(MI->getOperand(1));
1777 } else if (MI->getOpcode() == BPF::CORE_LD64 ||
1778 MI->getOpcode() == BPF::CORE_LD32 ||
1779 MI->getOpcode() == BPF::CORE_ST ||
1780 MI->getOpcode() == BPF::CORE_SHIFT) {
1781 // relocation insn is a load, store or shift insn.
1782 processGlobalValue(MI->getOperand(3));
1783 } else if (MI->getOpcode() == BPF::JAL) {
1784 // check extern function references
1785 const MachineOperand &MO = MI->getOperand(0);
1786 if (MO.isGlobal()) {
1787 processFuncPrototypes(dyn_cast<Function>(MO.getGlobal()));
1788 }
1789 }
1790
1791 if (!CurMI) // no debug info
1792 return;
1793
1794 // Skip this instruction if no DebugLoc, the DebugLoc
1795 // is the same as the previous instruction or Line is 0.
1796 const DebugLoc &DL = MI->getDebugLoc();
1797 if (!DL || PrevInstLoc == DL || DL.getLine() == 0) {
1798 // This instruction will be skipped, no LineInfo has
1799 // been generated, construct one based on function signature.
1800 if (LineInfoGenerated == false) {
1801 auto *S = MI->getMF()->getFunction().getSubprogram();
1802 if (!S)
1803 return;
1804 MCSymbol *FuncLabel = Asm->getFunctionBegin();
1805 constructLineInfo(FuncLabel, S->getFile(), S->getLine(), 0);
1806 LineInfoGenerated = true;
1807 }
1808
1809 return;
1810 }
1811
1812 // Create a temporary label to remember the insn for lineinfo.
1813 MCSymbol *LineSym = OS.getContext().createTempSymbol();
1814 OS.emitLabel(LineSym);
1815
1816 // Construct the lineinfo.
1817 constructLineInfo(LineSym, DL->getFile(), DL.getLine(), DL.getCol());
1818
1819 LineInfoGenerated = true;
1820 PrevInstLoc = DL;
1821}
1822
1823void BTFDebug::processGlobals(bool ProcessingMapDef) {
1824 // Collect all types referenced by globals.
1825 const Module *M = MMI->getModule();
1826 for (const GlobalVariable &Global : M->globals()) {
1827 // Decide the section name.
1828 StringRef SecName;
1829 std::optional<SectionKind> GVKind;
1830
1831 if (!Global.isDeclarationForLinker())
1833
1834 if (Global.isDeclarationForLinker())
1835 SecName = Global.hasSection() ? Global.getSection() : "";
1836 else if (GVKind->isCommon())
1837 SecName = ".bss";
1838 else {
1840 MCSection *Sec = TLOF->SectionForGlobal(&Global, Asm->TM);
1841 SecName = Sec->getName();
1842 }
1843
1844 if (ProcessingMapDef != SecName.starts_with(".maps"))
1845 continue;
1846
1847 // Create a .rodata datasec if the global variable is an initialized
1848 // constant with private linkage and if it won't be in .rodata.str<#>
1849 // and .rodata.cst<#> sections.
1850 if (SecName == ".rodata" && Global.hasPrivateLinkage() &&
1851 DataSecEntries.find(SecName) == DataSecEntries.end()) {
1852 // skip .rodata.str<#> and .rodata.cst<#> sections
1853 if (!GVKind->isMergeableCString() && !GVKind->isMergeableConst()) {
1854 DataSecEntries[std::string(SecName)] =
1855 std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1856 }
1857 }
1858
1860 Global.getDebugInfo(GVs);
1861
1862 // No type information, mostly internal, skip it.
1863 if (GVs.size() == 0)
1864 continue;
1865
1866 uint32_t GVTypeId = 0;
1867 DIGlobalVariable *DIGlobal = nullptr;
1868 for (auto *GVE : GVs) {
1869 DIGlobal = GVE->getVariable();
1870 if (SecName.starts_with(".maps"))
1871 visitMapDefType(DIGlobal->getType(), GVTypeId);
1872 else {
1873 const DIType *Ty = tryRemoveAtomicType(DIGlobal->getType());
1874 visitTypeEntry(Ty, GVTypeId, false, false);
1875 }
1876 break;
1877 }
1878
1879 // Only support the following globals:
1880 // . static variables
1881 // . non-static weak or non-weak global variables
1882 // . weak or non-weak extern global variables
1883 // Whether DataSec is readonly or not can be found from corresponding ELF
1884 // section flags. Whether a BTF_KIND_VAR is a weak symbol or not
1885 // can be found from the corresponding ELF symbol table.
1886 auto Linkage = Global.getLinkage();
1892 continue;
1893
1894 uint32_t GVarInfo;
1896 GVarInfo = BTF::VAR_STATIC;
1897 } else if (Global.hasInitializer()) {
1898 GVarInfo = BTF::VAR_GLOBAL_ALLOCATED;
1899 } else {
1900 GVarInfo = BTF::VAR_GLOBAL_EXTERNAL;
1901 }
1902
1903 auto VarEntry =
1904 std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1905 uint32_t VarId = addType(std::move(VarEntry));
1906
1907 processDeclAnnotations(DIGlobal->getAnnotations(), VarId, -1);
1908
1909 // An empty SecName means an extern variable without section attribute.
1910 if (SecName.empty())
1911 continue;
1912
1913 // Find or create a DataSec
1914 auto [It, Inserted] = DataSecEntries.try_emplace(std::string(SecName));
1915 if (Inserted)
1916 It->second = std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1917
1918 // Calculate symbol size
1919 const DataLayout &DL = Global.getDataLayout();
1920 uint32_t Size = Global.getGlobalSize(DL);
1921
1922 It->second->addDataSecEntry(VarId, Asm->getSymbol(&Global), Size);
1923
1924 if (Global.hasInitializer())
1925 processGlobalInitializer(Global.getInitializer());
1926 }
1927}
1928
1929/// Process global variable initializer in pursuit for function
1930/// pointers. Add discovered (extern) functions to BTF. Some (extern)
1931/// functions might have been missed otherwise. Every symbol needs BTF
1932/// info when linking with bpftool. Primary use case: "static"
1933/// initialization of BPF maps.
1934///
1935/// struct {
1936/// __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
1937/// ...
1938/// } prog_map SEC(".maps") = { .values = { extern_func } };
1939///
1940void BTFDebug::processGlobalInitializer(const Constant *C) {
1941 if (auto *Fn = dyn_cast<Function>(C))
1942 processFuncPrototypes(Fn);
1943 if (auto *CA = dyn_cast<ConstantAggregate>(C)) {
1944 for (unsigned I = 0, N = CA->getNumOperands(); I < N; ++I)
1945 processGlobalInitializer(CA->getOperand(I));
1946 }
1947}
1948
1949/// Emit proper patchable instructions.
1951 if (MI->getOpcode() == BPF::LD_imm64) {
1952 const MachineOperand &MO = MI->getOperand(1);
1953 if (MO.isGlobal()) {
1954 const GlobalValue *GVal = MO.getGlobal();
1955 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1956 if (GVar) {
1959 return false;
1960
1961 // Emit "mov ri, <imm>"
1962 auto [Imm, Reloc] = PatchImms[GVar];
1965 OutMI.setOpcode(BPF::LD_imm64);
1966 else
1967 OutMI.setOpcode(BPF::MOV_ri);
1968 OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1970 return true;
1971 }
1972 }
1973 } else if (MI->getOpcode() == BPF::CORE_LD64 ||
1974 MI->getOpcode() == BPF::CORE_LD32 ||
1975 MI->getOpcode() == BPF::CORE_ST ||
1976 MI->getOpcode() == BPF::CORE_SHIFT) {
1977 const MachineOperand &MO = MI->getOperand(3);
1978 if (MO.isGlobal()) {
1979 const GlobalValue *GVal = MO.getGlobal();
1980 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1981 if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1982 uint32_t Imm = PatchImms[GVar].first;
1983 OutMI.setOpcode(MI->getOperand(1).getImm());
1984 if (MI->getOperand(0).isImm())
1985 OutMI.addOperand(MCOperand::createImm(MI->getOperand(0).getImm()));
1986 else
1987 OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1988 OutMI.addOperand(MCOperand::createReg(MI->getOperand(2).getReg()));
1990 return true;
1991 }
1992 }
1993 }
1994 return false;
1995}
1996
1997void BTFDebug::processFuncPrototypes(const Function *F) {
1998 if (!F)
1999 return;
2000
2001 const DISubprogram *SP = F->getSubprogram();
2002 if (!SP || SP->isDefinition())
2003 return;
2004
2005 // Do not emit again if already emitted.
2006 if (!ProtoFunctions.insert(F).second)
2007 return;
2008
2009 uint32_t ProtoTypeId;
2010 const SmallDenseMap<uint32_t, StringRef> FuncArgNames;
2011 visitSubroutineType(SP->getType(), false, FuncArgNames, ProtoTypeId);
2012 uint32_t FuncId = processDISubprogram(SP, ProtoTypeId, BTF::FUNC_EXTERN);
2013
2014 if (F->hasSection()) {
2015 StringRef SecName = F->getSection();
2016
2017 auto [It, Inserted] = DataSecEntries.try_emplace(std::string(SecName));
2018 if (Inserted)
2019 It->second = std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
2020
2021 // We really don't know func size, set it to 0.
2022 It->second->addDataSecEntry(FuncId, Asm->getSymbol(F), 0);
2023 }
2024}
2025
2027 // Collect MapDef globals if not collected yet.
2028 if (MapDefNotCollected) {
2029 processGlobals(true);
2030 MapDefNotCollected = false;
2031 }
2032
2033 // Collect global types/variables except MapDef globals.
2034 processGlobals(false);
2035
2036 // In case that BPF_TRAP usage is removed during machine-level optimization,
2037 // generate btf for BPF_TRAP function here.
2038 for (const Function &F : *MMI->getModule()) {
2039 if (F.getName() == BPF_TRAP)
2040 processFuncPrototypes(&F);
2041 }
2042
2043 for (auto &DataSec : DataSecEntries)
2044 addType(std::move(DataSec.second));
2045
2046 // Fixups
2047 for (auto &Fixup : FixupDerivedTypes) {
2048 const DICompositeType *CTy = Fixup.first;
2049 StringRef TypeName = CTy->getName();
2050 bool IsUnion = CTy->getTag() == dwarf::DW_TAG_union_type;
2051
2052 // Search through struct types
2053 uint32_t StructTypeId = 0;
2054 for (const auto &StructType : StructTypes) {
2055 if (StructType->getName() == TypeName) {
2056 StructTypeId = StructType->getId();
2057 break;
2058 }
2059 }
2060
2061 if (StructTypeId == 0) {
2062 auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
2063 StructTypeId = addType(std::move(FwdTypeEntry));
2064 }
2065
2066 for (auto &TypeInfo : Fixup.second) {
2067 const DIDerivedType *DTy = TypeInfo.first;
2068 BTFTypeDerived *BDType = TypeInfo.second;
2069
2070 int TmpTypeId = genBTFTypeTags(DTy, StructTypeId);
2071 if (TmpTypeId >= 0)
2072 BDType->setPointeeType(TmpTypeId);
2073 else
2074 BDType->setPointeeType(StructTypeId);
2075 }
2076 }
2077
2078 // Complete BTF type cross refereences.
2079 for (const auto &TypeEntry : TypeEntries)
2080 TypeEntry->completeType(*this);
2081
2082 // Emit BTF sections.
2083 emitBTFSection();
2084 emitBTFExtSection();
2085}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Imm
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define BPF_TRAP
Definition BPF.h:29
static SmallVector< std::pair< uint32_t, Register >, 8 > collectNocallEntryArgRegs(const MachineFunction &MF)
Collect the physical register each source argument lives in by scanning DBG_VALUE instructions in the...
Definition BTFDebug.cpp:126
static bool sourceArgMatchesIRType(const DIType *SourceTy, Type *IRTy)
Definition BTFDebug.cpp:78
static const char * BTFKindStr[]
Definition BTFDebug.cpp:47
static const DIType * stripDITypeAttributes(const DIType *Ty)
Definition BTFDebug.cpp:61
static bool canUseNocallOptimizedSignature(const MachineFunction &MF, DITypeArray Elements, ArrayRef< std::pair< uint32_t, Register > > AliveArgs, const TargetRegisterInfo &TRI)
Check whether the optimized IR signature matches the surviving source arguments precisely enough to e...
Definition BTFDebug.cpp:219
static const DIType * tryRemoveAtomicType(const DIType *Ty)
Definition BTFDebug.cpp:52
This file contains support for writing BTF debug info.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
DXIL Finalize Linkage
dxil translate DXIL Translate Metadata
This file contains constants used for implementing Dwarf debug support.
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
PowerPC TLS Dynamic Call Fixup
static StringRef getName(Value *V)
This file contains some templates that are useful if you are working with the STL at all.
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null,...
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Value * RHS
Value * LHS
an instruction to allocate memory on the stack
Annotations lets you mark points and ranges inside source code, for tests:
Definition Annotations.h:67
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:91
MCSymbol * getSymbol(const GlobalValue *GV) const
TargetMachine & TM
Target machine description.
Definition AsmPrinter.h:94
static constexpr StringRef TypeIdAttr
The attribute attached to globals representing a type id.
Definition BPFCORE.h:63
static constexpr StringRef AmaAttr
The attribute attached to globals representing a field access.
Definition BPFCORE.h:61
Collect and emit BTF information.
Definition BTFDebug.h:297
void endFunctionImpl(const MachineFunction *MF) override
Post process after all instructions in this function are processed.
BTFDebug(AsmPrinter *AP)
Definition BTFDebug.cpp:809
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
bool InstLower(const MachineInstr *MI, MCInst &OutMI)
Emit proper patchable instructions.
size_t addString(StringRef S)
Add string to the string table.
Definition BTFDebug.h:427
uint32_t getArrayIndexTypeId()
Get the special array index type id.
Definition BTFDebug.h:421
uint32_t getTypeId(const DIType *Ty)
Get the type id for a particular DIType.
Definition BTFDebug.h:430
void endModule() override
Complete all the types and emit the BTF sections.
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:721
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:716
BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
Definition BTFDebug.cpp:709
BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
Definition BTFDebug.cpp:692
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:704
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:700
uint32_t addString(StringRef S)
Add a string to the string table and returns its offset in the table.
Definition BTFDebug.cpp:795
BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems)
Definition BTFDebug.cpp:494
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:517
void completeType(BTFDebug &BDebug) override
Represent a BTF array.
Definition BTFDebug.cpp:505
struct BTF::CommonType BTFType
Definition BTFDebug.h:45
virtual void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition BTFDebug.cpp:256
uint32_t roundupToBytes(uint32_t NumBits)
Definition BTFDebug.h:52
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:755
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:763
BTFTypeDeclTag(uint32_t BaseTypeId, int ComponentId, StringRef Tag)
Definition BTFDebug.cpp:746
Handle several derived types include pointer, const, volatile, typedef and restrict.
Definition BTFDebug.h:65
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:309
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:348
void setPointeeType(uint32_t PointeeType)
Definition BTFDebug.cpp:350
BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup)
Definition BTFDebug.cpp:265
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:459
BTFTypeEnum64(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned)
Definition BTFDebug.cpp:452
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:483
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:420
BTFTypeEnum(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned)
Definition BTFDebug.cpp:413
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:444
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:738
BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName)
Definition BTFDebug.cpp:731
BTFTypeFuncProto(const DISubroutineType *STy, uint32_t NumParams, const SmallDenseMap< uint32_t, StringRef > &FuncArgNames, bool UseFilteredParams=false, ArrayRef< uint32_t > AliveParamIndices={}, bool VoidReturn=false)
The Func kind represents both subprogram and pointee of function pointers.
Definition BTFDebug.cpp:615
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:627
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:666
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:690
BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId, uint32_t Scope)
Definition BTFDebug.cpp:674
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:682
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:369
BTFTypeFwd(StringRef Name, bool IsUnion)
Represent a struct/union forward declaration.
Definition BTFDebug.cpp:355
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:361
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:407
BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, uint32_t OffsetInBits, StringRef TypeName)
Definition BTFDebug.cpp:371
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:399
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition BTFDebug.cpp:598
BTFTypeStruct(const DICompositeType *STy, ArrayRef< const DINode * > Elements, bool IsStruct, bool HasBitField, uint32_t NumMembers)
Represent either a struct or a union.
Definition BTFDebug.cpp:525
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:535
std::string getName()
Definition BTFDebug.cpp:608
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition BTFDebug.cpp:781
BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag)
Definition BTFDebug.cpp:768
This is an important base class in LLVM.
Definition Constant.h:43
Basic type, like 'int' or 'float'.
unsigned getEncoding() const
DIDerivedType * getDiscriminator() const
DINodeArray getElements() const
DINodeArray getAnnotations() const
DIType * getBaseType() const
DINodeArray getAnnotations() const
Get annotations associated with this derived type.
DINodeArray getAnnotations() const
LLVM_ABI DISubprogram * getSubprogram() const
Get the subprogram for this scope.
DILocalScope * getScope() const
Get the local scope for this variable.
LLVM_ABI dwarf::Tag getTag() const
Subprogram description. Uses SubclassData1.
LLVM_ABI BoundType getCount() const
Type array for a subprogram.
DITypeArray getTypeArray() const
Base class for types.
uint64_t getOffsetInBits() const
StringRef getName() const
bool isForwardDecl() const
uint64_t getSizeInBits() const
DIType * getType() const
const MachineInstr * CurMI
If nonnull, stores the current machine instruction we're processing.
AsmPrinter * Asm
Target of debug info emission.
MachineModuleInfo * MMI
Collected machine module information.
DebugLoc PrevInstLoc
Previous instruction's location information.
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
A debug info location.
Definition DebugLoc.h:126
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
iterator begin()
Definition DenseMap.h:137
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:219
iterator end()
Definition DenseMap.h:141
DISubprogram * getSubprogram() const
Get the attached subprogram.
arg_iterator arg_begin()
Definition Function.h:852
size_t arg_size() const
Definition Function.h:885
Type * getReturnType() const
Returns the type of the ret val.
Definition Function.h:216
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this GlobalObject.
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition GlobalValue.h:57
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition GlobalValue.h:62
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists.
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition MCContext.h:550
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
This represents a section on linux, lots of unix variants and some bare metal systems.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:580
void setAlignment(Align Value)
Definition MCSection.h:665
StringRef getName() const
Definition MCSection.h:650
Streaming machine code generation interface.
Definition MCStreamer.h:222
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition MCStreamer.h:404
void emitInt32(uint64_t Value)
Definition MCStreamer.h:769
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition MCSymbol.h:237
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition MCSymbol.h:251
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
const AllocaInst * getObjectAllocation(int ObjectIdx) const
Return the underlying Alloca of the specified stack object if it exists.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
VariableDbgInfoMapTy & getVariableDbgInfo()
const MachineBasicBlock & front() const
Representation of each machine instruction.
A description of a memory reference used in the backend.
const Module * getModule() const
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:293
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition StringRef.h:396
Class to represent struct types.
LLVM_ABI StringRef getName() const
Return the name for this struct type if it has an identity.
Definition Type.cpp:760
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
MCSection * SectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const
This method computes the appropriate section to emit the specified global variable or function defini...
virtual TargetLoweringObjectFile * getObjFileLowering() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:182
bool erase(const ValueT &V)
Definition DenseSet.h:97
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ INT_SIGNED
Definition BTF.h:146
@ INT_BOOL
Definition BTF.h:148
@ VAR_GLOBAL_ALLOCATED
Linkage: ExternalLinkage.
Definition BTF.h:209
@ VAR_STATIC
Linkage: InternalLinkage.
Definition BTF.h:208
@ VAR_GLOBAL_EXTERNAL
Linkage: ExternalLinkage.
Definition BTF.h:210
@ VERSION
Definition BTF.h:57
@ MAGIC
Definition BTF.h:57
@ BPFFuncInfoSize
Definition BTF.h:73
@ HeaderSize
Definition BTF.h:61
@ ExtHeaderSize
Definition BTF.h:62
@ SecLineInfoSize
Definition BTF.h:71
@ SecFieldRelocSize
Definition BTF.h:72
@ BPFLineInfoSize
Definition BTF.h:74
@ SecFuncInfoSize
Definition BTF.h:70
@ BPFFieldRelocSize
Definition BTF.h:75
@ MAX_VLEN
Max # of struct/union/enum members or func args.
Definition BTF.h:93
@ ENUM_VALUE
Definition BTF.h:293
@ ENUM_VALUE_EXISTENCE
Definition BTF.h:292
@ BTF_TYPE_ID_REMOTE
Definition BTF.h:289
@ BTF_TYPE_ID_LOCAL
Definition BTF.h:288
@ FUNC_STATIC
Definition BTF.h:201
@ FUNC_EXTERN
Definition BTF.h:203
@ FUNC_GLOBAL
Definition BTF.h:202
@ SHT_PROGBITS
Definition ELF.h:1157
StringMapEntry< std::atomic< TypeEntryBody * > > TypeEntry
Definition TypePool.h:28
ScopedSetting scopedDisable()
Definition IOSandbox.h:36
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
void stable_sort(R &&Range)
Definition STLExtras.h:2116
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
uint64_t getBTFRecordElementOffset(const DINode *Element)
Return the bit offset used to order an element of a BTF structure record.
Definition BPFCORE.h:25
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Global
Append to llvm.global_dtors.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
#define N
Represent one field relocation.
Definition BTFDebug.h:289
uint32_t RelocKind
What to patch the instruction.
Definition BTFDebug.h:293
const MCSymbol * Label
MCSymbol identifying insn for the reloc.
Definition BTFDebug.h:290
uint32_t TypeID
Type ID.
Definition BTFDebug.h:291
uint32_t OffsetNameOff
The string to traverse types.
Definition BTFDebug.h:292
Represent one func and its type id.
Definition BTFDebug.h:274
uint32_t TypeId
Type id referring to .BTF type section.
Definition BTFDebug.h:276
const MCSymbol * Label
Func MCSymbol.
Definition BTFDebug.h:275
uint32_t LineOff
line offset in the .BTF string table
Definition BTFDebug.h:283
MCSymbol * Label
MCSymbol identifying insn for the lineinfo.
Definition BTFDebug.h:281
uint32_t ColumnNum
the column number
Definition BTFDebug.h:285
uint32_t FileNameOff
file name offset in the .BTF string table
Definition BTFDebug.h:282
uint32_t LineNum
the line number
Definition BTFDebug.h:284
BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
Definition BTF.h:162
uint32_t NameOff
Enum name offset in the string table.
Definition BTF.h:163
uint32_t Val_Hi32
Enum member hi32 value.
Definition BTF.h:165
uint32_t Val_Lo32
Enum member lo32 value.
Definition BTF.h:164
BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
Definition BTF.h:154
int32_t Val
Enum member value.
Definition BTF.h:156
uint32_t NameOff
Enum name offset in the string table.
Definition BTF.h:155
BTF_KIND_STRUCT and BTF_KIND_UNION are followed by multiple "struct BTFMember".
Definition BTF.h:185
uint32_t NameOff
Member name offset in the string table.
Definition BTF.h:186
uint32_t Offset
BitOffset or BitFieldSize+BitOffset.
Definition BTF.h:188
uint32_t Type
Member type.
Definition BTF.h:187
BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
Definition BTF.h:194
Function object to check whether the first component of a container supported by std::get (like std::...
Definition STLExtras.h:1439