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