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"
16#include "MCTargetDesc/BPFMCTargetDesc.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/BinaryFormat/Dwarf.h"
20#include "llvm/BinaryFormat/ELF.h"
21#include "llvm/CodeGen/AsmPrinter.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineOperand.h"
25#include "llvm/CodeGen/TargetRegisterInfo.h"
26#include "llvm/CodeGen/TargetSubtargetInfo.h"
27#include "llvm/IR/Module.h"
28#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCObjectFileInfo.h"
30#include "llvm/MC/MCSectionELF.h"
31#include "llvm/MC/MCStreamer.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/IOSandbox.h"
35#include "llvm/Support/LineIterator.h"
36#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Target/TargetLoweringObjectFile.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>(Val: 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>(Val: 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(Ty: 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>(Val: SourceTy))
84 return DTy->getTag() == dwarf::DW_TAG_pointer_type && IRTy->isPointerTy();
85
86 if (const auto *BTy = dyn_cast<DIBasicType>(Val: 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(BitWidth: 1))
93 return true;
94 return IRTy->isIntegerTy(BitWidth: SizeInBits);
95 }
96
97 const auto *CTy = dyn_cast<DICompositeType>(Val: SourceTy);
98 if (!CTy)
99 return false;
100
101 switch (CTy->getTag()) {
102 case dwarf::DW_TAG_enumeration_type:
103 return IRTy->isIntegerTy(BitWidth: 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.
124static SmallVector<std::pair<uint32_t, Register>, 8>
125collectNocallEntryArgRegs(const MachineFunction &MF) {
126 SmallDenseMap<uint32_t, Register> EntryRegMap;
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();
133 SmallDenseMap<const Value *, int> AllocaToFI;
134 for (int I = 0, N = MFI.getObjectIndexEnd(); I < N; ++I)
135 if (const AllocaInst *AI = MFI.getObjectAllocation(ObjectIdx: 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(Index: 0);
155 if (!MO.isReg() || !MO.getReg().isPhysical())
156 continue;
157
158 if (!DefinedRegs.contains(V: MO.getReg()) ||
159 StackLoadRegs.contains(V: 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(i: 0).isReg()) {
168 Register SrcReg = MI.getOperand(i: 0).getReg();
169 if (SrcReg.isPhysical() && !DefinedRegs.contains(V: SrcReg)) {
170 for (const MachineMemOperand *MMO : MI.memoperands()) {
171 const Value *V = MMO->getValue();
172 if (!V)
173 continue;
174 auto It = AllocaToFI.find(Val: V);
175 if (It != AllocaToFI.end())
176 FrameIndexToReg.try_emplace(Key: It->second, Args&: SrcReg);
177 }
178 }
179 }
180
181 for (const MachineOperand &MO : MI.operands())
182 if (MO.isReg() && MO.isDef() && MO.getReg().isPhysical()) {
183 DefinedRegs.insert(V: MO.getReg());
184 StackLoadRegs.erase(V: MO.getReg());
185 }
186
187 // Detect stack argument loads: $rX = LDD $r11, offset.
188 if (MI.getOpcode() == BPF::LDD && MI.getOperand(i: 1).getReg() == BPF::R11)
189 StackLoadRegs.insert(V: MI.getOperand(i: 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(Val: Arg))
202 continue;
203 auto It = FrameIndexToReg.find(Val: 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(C&: AliveArgs, Comp: 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.
218static bool canUseNocallOptimizedSignature(
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(SourceTy: Elements[ArgNo], IRTy: 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, isEH: 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.
255void BTFTypeBase::emitType(MCStreamer &OS) {
256 OS.AddComment(T: std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(val: Id) +
257 ")");
258 OS.emitInt32(Value: BTFType.NameOff);
259 OS.AddComment(T: "0x" + Twine::utohexstr(Val: BTFType.Info));
260 OS.emitInt32(Value: BTFType.Info);
261 OS.emitInt32(Value: BTFType.Size);
262}
263
264BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
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
308void BTFTypeDerived::completeType(BTFDebug &BDebug) {
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(S: 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(Ty: 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(Ty: ResolvedType);
344 }
345}
346
347void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
348
349void BTFTypeDerived::setPointeeType(uint32_t PointeeType) {
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
360void BTFTypeFwd::completeType(BTFDebug &BDebug) {
361 if (IsCompleted)
362 return;
363 IsCompleted = true;
364
365 BTFType.NameOff = BDebug.addString(S: Name);
366}
367
368void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
369
370BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits,
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(NumBits: SizeInBits);
395 IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
396}
397
398void BTFTypeInt::completeType(BTFDebug &BDebug) {
399 if (IsCompleted)
400 return;
401 IsCompleted = true;
402
403 BTFType.NameOff = BDebug.addString(S: Name);
404}
405
406void BTFTypeInt::emitType(MCStreamer &OS) {
407 BTFTypeBase::emitType(OS);
408 OS.AddComment(T: "0x" + Twine::utohexstr(Val: IntVal));
409 OS.emitInt32(Value: IntVal);
410}
411
412BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen,
413 bool IsSigned) : ETy(ETy) {
414 Kind = BTF::BTF_KIND_ENUM;
415 BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
416 BTFType.Size = roundupToBytes(NumBits: ETy->getSizeInBits());
417}
418
419void BTFTypeEnum::completeType(BTFDebug &BDebug) {
420 if (IsCompleted)
421 return;
422 IsCompleted = true;
423
424 BTFType.NameOff = BDebug.addString(S: ETy->getName());
425
426 DINodeArray Elements = ETy->getElements();
427 for (const auto Element : Elements) {
428 const auto *Enum = cast<DIEnumerator>(Val: Element);
429
430 struct BTF::BTFEnum BTFEnum;
431 BTFEnum.NameOff = BDebug.addString(S: Enum->getName());
432 // BTF enum value is 32bit, enforce it.
433 uint32_t Value;
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(x: BTFEnum);
440 }
441}
442
443void BTFTypeEnum::emitType(MCStreamer &OS) {
444 BTFTypeBase::emitType(OS);
445 for (const auto &Enum : EnumValues) {
446 OS.emitInt32(Value: Enum.NameOff);
447 OS.emitInt32(Value: Enum.Val);
448 }
449}
450
451BTFTypeEnum64::BTFTypeEnum64(const DICompositeType *ETy, uint32_t VLen,
452 bool IsSigned) : ETy(ETy) {
453 Kind = BTF::BTF_KIND_ENUM64;
454 BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
455 BTFType.Size = roundupToBytes(NumBits: ETy->getSizeInBits());
456}
457
458void BTFTypeEnum64::completeType(BTFDebug &BDebug) {
459 if (IsCompleted)
460 return;
461 IsCompleted = true;
462
463 BTFType.NameOff = BDebug.addString(S: ETy->getName());
464
465 DINodeArray Elements = ETy->getElements();
466 for (const auto Element : Elements) {
467 const auto *Enum = cast<DIEnumerator>(Val: Element);
468
469 struct BTF::BTFEnum64 BTFEnum;
470 BTFEnum.NameOff = BDebug.addString(S: Enum->getName());
471 uint64_t Value;
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(x: BTFEnum);
479 }
480}
481
482void BTFTypeEnum64::emitType(MCStreamer &OS) {
483 BTFTypeBase::emitType(OS);
484 for (const auto &Enum : EnumValues) {
485 OS.emitInt32(Value: Enum.NameOff);
486 OS.AddComment(T: "0x" + Twine::utohexstr(Val: Enum.Val_Lo32));
487 OS.emitInt32(Value: Enum.Val_Lo32);
488 OS.AddComment(T: "0x" + Twine::utohexstr(Val: Enum.Val_Hi32));
489 OS.emitInt32(Value: Enum.Val_Hi32);
490 }
491}
492
493BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems) {
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.
504void BTFTypeArray::completeType(BTFDebug &BDebug) {
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
516void BTFTypeArray::emitType(MCStreamer &OS) {
517 BTFTypeBase::emitType(OS);
518 OS.emitInt32(Value: ArrayInfo.ElemType);
519 OS.emitInt32(Value: ArrayInfo.IndexType);
520 OS.emitInt32(Value: ArrayInfo.Nelems);
521}
522
523/// Represent either a struct or a union.
524BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy,
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(NumBits: STy->getSizeInBits());
531 BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
532}
533
534void BTFTypeStruct::completeType(BTFDebug &BDebug) {
535 if (IsCompleted)
536 return;
537 IsCompleted = true;
538
539 BTFType.NameOff = BDebug.addString(S: 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(S: DTy->getName());
555 Discriminator.Offset = DTy->getOffsetInBits();
556 const auto *BaseTy = DTy->getBaseType();
557 Discriminator.Type = BDebug.getTypeId(Ty: BaseTy);
558
559 Members.push_back(x: 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>(Val: Element);
570
571 BTFMember.NameOff = BDebug.addString(S: 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(Ty: DDTy->getBaseType());
579 BTFMember.Type = BDebug.getTypeId(Ty: BaseTy);
580 break;
581 }
582 case dwarf::DW_TAG_variant_part: {
583 const auto *DCTy = dyn_cast<DICompositeType>(Val: Element);
584
585 BTFMember.NameOff = BDebug.addString(S: DCTy->getName());
586 BTFMember.Offset = DCTy->getOffsetInBits();
587 BTFMember.Type = BDebug.getTypeId(Ty: DCTy);
588 break;
589 }
590 default:
591 llvm_unreachable("Unexpected DI tag of a struct/union element");
592 }
593 Members.push_back(x: BTFMember);
594 }
595}
596
597void BTFTypeStruct::emitType(MCStreamer &OS) {
598 BTFTypeBase::emitType(OS);
599 for (const auto &Member : Members) {
600 OS.emitInt32(Value: Member.NameOff);
601 OS.emitInt32(Value: Member.Type);
602 OS.AddComment(T: "0x" + Twine::utohexstr(Val: Member.Offset));
603 OS.emitInt32(Value: 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.
614BTFTypeFuncProto::BTFTypeFuncProto(
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
626void BTFTypeFuncProto::completeType(BTFDebug &BDebug) {
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(Ty: Elements[0]);
636 BTFType.Type = RetType ? BDebug.getTypeId(Ty: RetType) : 0;
637 }
638 BTFType.NameOff = 0;
639
640 auto EmitParam = [&](uint32_t I) {
641 struct BTF::BTFParam Param;
642 auto Element = tryRemoveAtomicType(Ty: Elements[I]);
643 if (Element) {
644 auto It = FuncArgNames.find(Val: I);
645 Param.NameOff =
646 It != FuncArgNames.end() ? BDebug.addString(S: It->second) : 0;
647 Param.Type = BDebug.getTypeId(Ty: Element);
648 } else {
649 Param.NameOff = 0;
650 Param.Type = 0;
651 }
652 Parameters.push_back(x: 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
665void BTFTypeFuncProto::emitType(MCStreamer &OS) {
666 BTFTypeBase::emitType(OS);
667 for (const auto &Param : Parameters) {
668 OS.emitInt32(Value: Param.NameOff);
669 OS.emitInt32(Value: Param.Type);
670 }
671}
672
673BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId,
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
681void BTFTypeFunc::completeType(BTFDebug &BDebug) {
682 if (IsCompleted)
683 return;
684 IsCompleted = true;
685
686 BTFType.NameOff = BDebug.addString(S: Name);
687}
688
689void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
690
691BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
692 : Name(VarName) {
693 Kind = BTF::BTF_KIND_VAR;
694 BTFType.Info = Kind << 24;
695 BTFType.Type = TypeId;
696 Info = VarInfo;
697}
698
699void BTFKindVar::completeType(BTFDebug &BDebug) {
700 BTFType.NameOff = BDebug.addString(S: Name);
701}
702
703void BTFKindVar::emitType(MCStreamer &OS) {
704 BTFTypeBase::emitType(OS);
705 OS.emitInt32(Value: 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
715void BTFKindDataSec::completeType(BTFDebug &BDebug) {
716 BTFType.NameOff = BDebug.addString(S: Name);
717 BTFType.Info |= Vars.size();
718}
719
720void BTFKindDataSec::emitType(MCStreamer &OS) {
721 BTFTypeBase::emitType(OS);
722
723 for (const auto &V : Vars) {
724 OS.emitInt32(Value: std::get<0>(t: V));
725 Asm->emitLabelReference(Label: std::get<1>(t: V), Size: 4);
726 OS.emitInt32(Value: std::get<2>(t: V));
727 }
728}
729
730BTFTypeFloat::BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName)
731 : Name(TypeName) {
732 Kind = BTF::BTF_KIND_FLOAT;
733 BTFType.Info = Kind << 24;
734 BTFType.Size = roundupToBytes(NumBits: SizeInBits);
735}
736
737void BTFTypeFloat::completeType(BTFDebug &BDebug) {
738 if (IsCompleted)
739 return;
740 IsCompleted = true;
741
742 BTFType.NameOff = BDebug.addString(S: 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
754void BTFTypeDeclTag::completeType(BTFDebug &BDebug) {
755 if (IsCompleted)
756 return;
757 IsCompleted = true;
758
759 BTFType.NameOff = BDebug.addString(S: Tag);
760}
761
762void BTFTypeDeclTag::emitType(MCStreamer &OS) {
763 BTFTypeBase::emitType(OS);
764 OS.emitInt32(Value: Info);
765}
766
767BTFTypeTypeTag::BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag)
768 : DTy(nullptr), Tag(Tag) {
769 Kind = BTF::BTF_KIND_TYPE_TAG;
770 BTFType.Info = Kind << 24;
771 BTFType.Type = NextTypeId;
772}
773
774BTFTypeTypeTag::BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag)
775 : DTy(DTy), Tag(Tag) {
776 Kind = BTF::BTF_KIND_TYPE_TAG;
777 BTFType.Info = Kind << 24;
778}
779
780void BTFTypeTypeTag::completeType(BTFDebug &BDebug) {
781 if (IsCompleted)
782 return;
783 IsCompleted = true;
784 BTFType.NameOff = BDebug.addString(S: Tag);
785 if (DTy) {
786 const DIType *ResolvedType = tryRemoveAtomicType(Ty: DTy->getBaseType());
787 if (!ResolvedType)
788 BTFType.Type = 0;
789 else
790 BTFType.Type = BDebug.getTypeId(Ty: ResolvedType);
791 }
792}
793
794uint32_t BTFStringTable::addString(StringRef S) {
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(x: std::string(S));
804 Size += S.size() + 1;
805 return Offset;
806}
807
808BTFDebug::BTFDebug(AsmPrinter *AP)
809 : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
810 LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
811 MapDefNotCollected(true) {
812 addString(S: "\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(x: 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(x: 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 args&: Encoding, args: BTy->getSizeInBits(), args: BTy->getOffsetInBits(), args: BTy->getName());
846 break;
847 case dwarf::DW_ATE_float:
848 TypeEntry =
849 std::make_unique<BTFTypeFloat>(args: BTy->getSizeInBits(), args: BTy->getName());
850 break;
851 default:
852 return;
853 }
854
855 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: 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 args&: STy, args&: VLen, args: FuncArgNames, args: false, args: ArrayRef<uint32_t>(), args&: VoidReturn);
874 if (ForSubprog)
875 TypeId = addType(TypeEntry: std::move(TypeEntry)); // For subprogram
876 else
877 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: STy); // For func ptr
878
879 // Visit return type and func arg types.
880 if (!VoidReturn) {
881 for (const auto Element : Elements)
882 visitTypeEntry(Ty: Element);
883 } else {
884 for (unsigned I = 1, N = Elements.size(); I < N; ++I)
885 visitTypeEntry(Ty: 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>(Val: Annotation);
897 const MDString *Name = cast<MDString>(Val: MD->getOperand(I: 0));
898 if (Name->getString() != "btf_decl_tag")
899 continue;
900
901 const MDString *Value = cast<MDString>(Val: MD->getOperand(I: 1));
902 auto TypeEntry = std::make_unique<BTFTypeDeclTag>(args&: BaseTypeId, args&: ComponentIdx,
903 args: Value->getString());
904 addType(TypeEntry: 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>(args: SP->getName(), args&: ProtoTypeId, args&: Scope);
913 uint32_t FuncId = addType(TypeEntry: std::move(FuncTypeEntry));
914
915 // Process argument annotations.
916 for (const DINode *DN : SP->getRetainedNodes()) {
917 if (const auto *DV = dyn_cast<DILocalVariable>(Val: DN)) {
918 uint32_t Arg = DV->getArg();
919 if (Arg) {
920 if (ArgIndexMap) {
921 auto It = ArgIndexMap->find(Val: Arg);
922 if (It != ArgIndexMap->end())
923 processDeclAnnotations(Annotations: DV->getAnnotations(), BaseTypeId: FuncId, ComponentIdx: It->second);
924 } else {
925 processDeclAnnotations(Annotations: DV->getAnnotations(), BaseTypeId: FuncId, ComponentIdx: Arg - 1);
926 }
927 }
928 }
929 }
930 processDeclAnnotations(Annotations: SP->getAnnotations(), BaseTypeId: FuncId, ComponentIdx: -1);
931
932 return FuncId;
933}
934
935/// Generate btf_type_tag chains.
936int BTFDebug::genBTFTypeTags(const DIDerivedType *DTy, int BaseTypeId) {
937 SmallVector<const MDString *, 4> MDStrs;
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>(Val: Annotations);
944 const MDString *Name = cast<MDString>(Val: MD->getOperand(I: 0));
945 if (Name->getString() != "btf_type_tag")
946 continue;
947 MDStrs.push_back(Elt: cast<MDString>(Val: MD->getOperand(I: 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>(args&: BaseTypeId, args: MDStrs[0]->getString());
963 else
964 TypeEntry = std::make_unique<BTFTypeTypeTag>(args&: DTy, args: MDStrs[0]->getString());
965 TmpTypeId = addType(TypeEntry: 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>(args&: TmpTypeId, args: Value->getString());
970 TmpTypeId = addType(TypeEntry: 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(Range&: Elements, C: [](const DINode *LHS, const DINode *RHS) {
984 return getBTFRecordElementOffset(Element: LHS) < getBTFRecordElementOffset(Element: 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(Ty: 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>(Val: Element);
1005 if (E->isBitField()) {
1006 HasBitField = true;
1007 break;
1008 }
1009 }
1010 }
1011
1012 auto TypeEntry = std::make_unique<BTFTypeStruct>(args&: CTy, args&: Elements, args&: IsStruct,
1013 args&: HasBitField, args&: VLen);
1014 StructTypes.push_back(x: TypeEntry.get());
1015 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
1016
1017 // Check struct/union annotations
1018 processDeclAnnotations(Annotations: CTy->getAnnotations(), BaseTypeId: TypeId, ComponentIdx: -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>(Val: Element);
1026 visitTypeEntry(Ty: Elem);
1027 processDeclAnnotations(Annotations: Elem->getAnnotations(), BaseTypeId: TypeId, ComponentIdx: FieldNo);
1028 break;
1029 }
1030 case dwarf::DW_TAG_variant_part: {
1031 const auto Elem = cast<DICompositeType>(Val: Element);
1032 visitTypeEntry(Ty: Elem);
1033 processDeclAnnotations(Annotations: Elem->getAnnotations(), BaseTypeId: TypeId, ComponentIdx: 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(Ty: ElemType, TypeId&: ElemTypeId, CheckPointer: false, SeenPointer: 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>(args&: ElemTypeId, args: 0);
1055 ElemTypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
1056 }
1057 for (int I = Elements.size() - 1; I >= 0; --I) {
1058 if (auto *Element = dyn_cast_or_null<DINode>(Val: Elements[I]))
1059 if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
1060 const DISubrange *SR = cast<DISubrange>(Val: Element);
1061 auto *CI = dyn_cast<ConstantInt *>(Val: 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>(args&: ElemTypeId,
1068 args: Count >= 0 ? Count : 0);
1069 if (I == 0)
1070 ElemTypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
1071 else
1072 ElemTypeId = addType(TypeEntry: 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>(args: dwarf::DW_ATE_unsigned, args: 32,
1083 args: 0, args: "__ARRAY_SIZE_TYPE__");
1084 ArrayIndexTypeId = addType(TypeEntry: 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>(Val: 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>(args&: CTy, args&: VLen, args&: IsSigned);
1107 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
1108 } else {
1109 assert(NumBits == 64);
1110 auto TypeEntry = std::make_unique<BTFTypeEnum64>(args&: CTy, args&: VLen, args&: IsSigned);
1111 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: 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>(args: CTy->getName(), args&: IsUnion);
1120 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: 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, IsUnion: Tag == dwarf::DW_TAG_union_type, TypeId);
1134 else
1135 visitStructType(CTy, IsStruct: 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>(Val: 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(Ty: 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>(args&: DTy, args&: Tag, args: true);
1182 auto &Fixup = FixupDerivedTypes[cast<DICompositeType>(Val: Base)];
1183 Fixup.push_back(x: std::make_pair(x&: DTy, y: TypeEntry.get()));
1184 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: 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, BaseTypeId: -1);
1192 if (TmpTypeId >= 0) {
1193 auto TypeDEntry =
1194 std::make_unique<BTFTypeDerived>(args&: TmpTypeId, args&: Tag, args: DTy->getName());
1195 TypeId = addType(TypeEntry: std::move(TypeDEntry), Ty: DTy);
1196 } else {
1197 auto TypeEntry = std::make_unique<BTFTypeDerived>(args&: DTy, args&: Tag, args: false);
1198 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: DTy);
1199 }
1200 if (Tag == dwarf::DW_TAG_typedef)
1201 processDeclAnnotations(Annotations: DTy->getAnnotations(), BaseTypeId: TypeId, ComponentIdx: -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>(args&: DTy, args&: Tag, args: false);
1206 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: 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(Ty: DTy->getBaseType(), TypeId&: TempTypeId, CheckPointer: true, SeenPointer: false);
1216 else
1217 visitTypeEntry(Ty: DTy->getBaseType(), TypeId&: 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(Val: 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>(Val: Ty)) {
1263 while (DTy) {
1264 const DIType *BaseTy = DTy->getBaseType();
1265 if (!BaseTy)
1266 break;
1267
1268 if (DIToIdMap.find(Val: BaseTy) != DIToIdMap.end()) {
1269 DTy = dyn_cast<DIDerivedType>(Val: BaseTy);
1270 } else {
1271 if (CheckPointer && DTy->getTag() == dwarf::DW_TAG_pointer_type &&
1272 !DTy->getAnnotations()) {
1273 SeenPointer = true;
1274 if (IsForwardDeclCandidate(Base: BaseTy))
1275 break;
1276 }
1277 uint32_t TmpTypeId;
1278 visitTypeEntry(Ty: BaseTy, TypeId&: TmpTypeId, CheckPointer, SeenPointer);
1279 break;
1280 }
1281 }
1282 }
1283 }
1284
1285 return;
1286 }
1287
1288 if (const auto *BTy = dyn_cast<DIBasicType>(Val: Ty))
1289 visitBasicType(BTy, TypeId);
1290 else if (const auto *STy = dyn_cast<DISubroutineType>(Val: Ty))
1291 visitSubroutineType(STy, ForSubprog: false, FuncArgNames: SmallDenseMap<uint32_t, StringRef>(),
1292 TypeId);
1293 else if (const auto *CTy = dyn_cast<DICompositeType>(Val: Ty))
1294 visitCompositeType(CTy, TypeId);
1295 else if (const auto *DTy = dyn_cast<DIDerivedType>(Val: 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, CheckPointer: false, SeenPointer: false);
1304}
1305
1306void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
1307 if (!Ty || DIToIdMap.find(Val: 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(Ty: dyn_cast<DIDerivedType>(Val: Ty)->getBaseType(), TypeId&: TmpId);
1320 break;
1321 case dwarf::DW_TAG_array_type:
1322 // Visit nested map array and jump to the element type
1323 visitMapDefType(Ty: dyn_cast<DICompositeType>(Val: Ty)->getBaseType(), TypeId&: 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>(Val: Ty);
1328 const DINodeArray Elements = CTy->getElements();
1329 for (const auto *Element : Elements) {
1330 const auto *MemberType = cast<DIDerivedType>(Val: 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>(Val: MemberBaseType);
1339 if (MemberCTy) {
1340 visitMapDefType(Ty: MemberBaseType, TypeId&: TmpId);
1341 } else {
1342 visitTypeEntry(Ty: 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, CheckPointer: false, SeenPointer: 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(Prefix: "/") && 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(Key: FileName))
1366 return FileName;
1367
1368 std::vector<std::string> Content;
1369 std::string Line;
1370 Content.push_back(x: 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: FileName);
1376 };
1377
1378 std::unique_ptr<MemoryBuffer> Buf;
1379 auto Source = File->getSource();
1380 if (Source)
1381 Buf = MemoryBuffer::getMemBufferCopy(InputData: *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(x: 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(S: 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(S: Content[Line]);
1403 else
1404 LineInfo.LineOff = 0;
1405 LineInfo.LineNum = Line;
1406 LineInfo.ColumnNum = Column;
1407 LineInfoTable[SecNameOff].push_back(x: LineInfo);
1408}
1409
1410void BTFDebug::emitCommonHeader() {
1411 OS.AddComment(T: "0x" + Twine::utohexstr(Val: BTF::MAGIC));
1412 OS.emitIntValue(Value: BTF::MAGIC, Size: 2);
1413 OS.emitInt8(Value: BTF::VERSION);
1414 OS.emitInt8(Value: 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(Section: ".BTF", Type: ELF::SHT_PROGBITS, Flags: 0);
1424 Sec->setAlignment(Align(4));
1425 OS.switchSection(Section: Sec);
1426
1427 // Emit header.
1428 emitCommonHeader();
1429 OS.emitInt32(Value: 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(Value: 0);
1437 OS.emitInt32(Value: TypeLen);
1438 OS.emitInt32(Value: TypeLen);
1439 OS.emitInt32(Value: 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(T: "string offset=" + std::to_string(val: StringOffset));
1449 OS.emitBytes(Data: S);
1450 OS.emitBytes(Data: 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(Section: ".BTF.ext", Type: ELF::SHT_PROGBITS, Flags: 0);
1464 Sec->setAlignment(Align(4));
1465 OS.switchSection(Section: Sec);
1466
1467 // Emit header.
1468 emitCommonHeader();
1469 OS.emitInt32(Value: 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(Value: 0);
1492 OS.emitInt32(Value: FuncLen);
1493 OS.emitInt32(Value: FuncLen);
1494 OS.emitInt32(Value: LineLen);
1495 OS.emitInt32(Value: FuncLen + LineLen);
1496 OS.emitInt32(Value: FieldRelocLen);
1497
1498 // Emit func_info table.
1499 OS.AddComment(T: "FuncInfo");
1500 OS.emitInt32(Value: BTF::BPFFuncInfoSize);
1501 for (const auto &FuncSec : FuncInfoTable) {
1502 OS.AddComment(T: "FuncInfo section string offset=" +
1503 std::to_string(val: FuncSec.first));
1504 OS.emitInt32(Value: FuncSec.first);
1505 OS.emitInt32(Value: FuncSec.second.size());
1506 for (const auto &FuncInfo : FuncSec.second) {
1507 Asm->emitLabelReference(Label: FuncInfo.Label, Size: 4);
1508 OS.emitInt32(Value: FuncInfo.TypeId);
1509 }
1510 }
1511
1512 // Emit line_info table.
1513 OS.AddComment(T: "LineInfo");
1514 OS.emitInt32(Value: BTF::BPFLineInfoSize);
1515 for (const auto &LineSec : LineInfoTable) {
1516 OS.AddComment(T: "LineInfo section string offset=" +
1517 std::to_string(val: LineSec.first));
1518 OS.emitInt32(Value: LineSec.first);
1519 OS.emitInt32(Value: LineSec.second.size());
1520 for (const auto &LineInfo : LineSec.second) {
1521 Asm->emitLabelReference(Label: LineInfo.Label, Size: 4);
1522 OS.emitInt32(Value: LineInfo.FileNameOff);
1523 OS.emitInt32(Value: LineInfo.LineOff);
1524 OS.AddComment(T: "Line " + std::to_string(val: LineInfo.LineNum) + " Col " +
1525 std::to_string(val: LineInfo.ColumnNum));
1526 OS.emitInt32(Value: LineInfo.LineNum << 10 | LineInfo.ColumnNum);
1527 }
1528 }
1529
1530 // Emit field reloc table.
1531 if (FieldRelocLen) {
1532 OS.AddComment(T: "FieldReloc");
1533 OS.emitInt32(Value: BTF::BPFFieldRelocSize);
1534 for (const auto &FieldRelocSec : FieldRelocTable) {
1535 OS.AddComment(T: "Field reloc section string offset=" +
1536 std::to_string(val: FieldRelocSec.first));
1537 OS.emitInt32(Value: FieldRelocSec.first);
1538 OS.emitInt32(Value: FieldRelocSec.second.size());
1539 for (const auto &FieldRelocInfo : FieldRelocSec.second) {
1540 Asm->emitLabelReference(Label: FieldRelocInfo.Label, Size: 4);
1541 OS.emitInt32(Value: FieldRelocInfo.TypeID);
1542 OS.emitInt32(Value: FieldRelocInfo.OffsetNameOff);
1543 OS.emitInt32(Value: FieldRelocInfo.RelocKind);
1544 }
1545 }
1546 }
1547}
1548
1549void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
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(ProcessingMapDef: 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.
1586 SmallDenseMap<uint32_t, StringRef> FuncArgNames;
1587 for (const DINode *DN : SP->getRetainedNodes()) {
1588 if (const auto *DV = dyn_cast<DILocalVariable>(Val: DN)) {
1589 // Collect function arguments for subprogram func type.
1590 uint32_t Arg = DV->getArg();
1591 if (Arg) {
1592 visitTypeEntry(Ty: 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).
1609 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
1610 DITypeArray Elements = SP->getType()->getTypeArray();
1611
1612 SmallVector<std::pair<uint32_t, Register>, 8> AliveArgs =
1613 collectNocallEntryArgRegs(MF: *MF);
1614
1615 UseFilteredParams =
1616 canUseNocallOptimizedSignature(MF: *MF, Elements, AliveArgs, TRI: *TRI);
1617
1618 if (UseFilteredParams) {
1619 SmallVector<uint32_t, 8> AliveParamIndices;
1620 SmallDenseMap<uint32_t, uint32_t> ArgIndexMap;
1621 for (auto [I, ArgReg] : llvm::enumerate(First&: AliveArgs)) {
1622 AliveParamIndices.push_back(Elt: ArgReg.first);
1623 ArgIndexMap[ArgReg.first] = I;
1624 }
1625
1626 if (!VoidReturn)
1627 visitTypeEntry(Ty: Elements[0]);
1628 for (uint32_t ArgNo : AliveParamIndices)
1629 visitTypeEntry(Ty: Elements[ArgNo]);
1630
1631 auto TypeEntry = std::make_unique<BTFTypeFuncProto>(
1632 args: SP->getType(), args: AliveParamIndices.size(), args&: FuncArgNames, args: true,
1633 args&: AliveParamIndices, args&: VoidReturn);
1634 ProtoTypeId = addType(TypeEntry: std::move(TypeEntry));
1635 FuncTypeId = processDISubprogram(SP, ProtoTypeId, Scope, ArgIndexMap: &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(STy: SP->getType(), ForSubprog: true, FuncArgNames, TypeId&: ProtoTypeId,
1643 VoidReturn);
1644 FuncTypeId = processDISubprogram(SP, ProtoTypeId, Scope);
1645 }
1646
1647 for (const auto &TypeEntry : TypeEntries)
1648 TypeEntry->completeType(BDebug&: *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(S: Sec.getName());
1658 } else {
1659 SecNameOff = addString(S: ".text");
1660 }
1661 FuncInfoTable[SecNameOff].push_back(x: FuncInfo);
1662}
1663
1664void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
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, TypeId&: Id, CheckPointer: false, SeenPointer: false);
1675 for (const auto &TypeEntry : TypeEntries)
1676 TypeEntry->completeType(BDebug&: *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(C: '$');
1689 if (IsAma) {
1690 size_t FirstColon = AccessPattern.find_first_of(C: ':');
1691 size_t SecondColon = AccessPattern.find_first_of(C: ':', From: FirstColon + 1);
1692 StringRef IndexPattern = AccessPattern.substr(Start: FirstDollar + 1);
1693 StringRef RelocKindStr = AccessPattern.substr(Start: FirstColon + 1,
1694 N: SecondColon - FirstColon);
1695 StringRef PatchImmStr = AccessPattern.substr(Start: SecondColon + 1,
1696 N: FirstDollar - SecondColon);
1697
1698 FieldReloc.OffsetNameOff = addString(S: IndexPattern);
1699 FieldReloc.RelocKind = std::stoull(str: std::string(RelocKindStr));
1700 PatchImms[GVar] = std::make_pair(x: std::stoll(str: std::string(PatchImmStr)),
1701 y&: FieldReloc.RelocKind);
1702 } else {
1703 StringRef RelocStr = AccessPattern.substr(Start: FirstDollar + 1);
1704 FieldReloc.OffsetNameOff = addString(S: "0");
1705 FieldReloc.RelocKind = std::stoull(str: std::string(RelocStr));
1706 PatchImms[GVar] = std::make_pair(x&: RootId, y&: FieldReloc.RelocKind);
1707 }
1708 FieldRelocTable[SecNameOff].push_back(x: 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>(Val: GVal);
1716 if (!GVar) {
1717 // Not a global variable. Maybe an extern function reference.
1718 processFuncPrototypes(dyn_cast<Function>(Val: GVal));
1719 return;
1720 }
1721
1722 if (!GVar->hasAttribute(Kind: BPFCoreSharedInfo::AmaAttr) &&
1723 !GVar->hasAttribute(Kind: BPFCoreSharedInfo::TypeIdAttr))
1724 return;
1725
1726 MCSymbol *ORSym = OS.getContext().createTempSymbol();
1727 OS.emitLabel(Symbol: ORSym);
1728
1729 MDNode *MDN = GVar->getMetadata(KindID: LLVMContext::MD_preserve_access_index);
1730 uint32_t RootId = populateType(Ty: dyn_cast<DIType>(Val: MDN));
1731 generatePatchImmReloc(ORSym, RootId, GVar,
1732 IsAma: GVar->hasAttribute(Kind: BPFCoreSharedInfo::AmaAttr));
1733 }
1734}
1735
1736void BTFDebug::beginInstruction(const MachineInstr *MI) {
1737 DebugHandlerBase::beginInstruction(MI);
1738
1739 if (SkipInstruction || MI->isMetaInstruction() ||
1740 MI->getFlag(Flag: 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(i: 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(MO: MI->getOperand(i: 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(MO: MI->getOperand(i: 3));
1782 } else if (MI->getOpcode() == BPF::JAL) {
1783 // check extern function references
1784 const MachineOperand &MO = MI->getOperand(i: 0);
1785 if (MO.isGlobal()) {
1786 processFuncPrototypes(dyn_cast<Function>(Val: 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(Label: FuncLabel, File: S->getFile(), Line: S->getLine(), Column: 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(Symbol: LineSym);
1814
1815 // Construct the lineinfo.
1816 constructLineInfo(Label: LineSym, File: DL->getFile(), Line: DL.getLine(), Column: 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())
1831 GVKind = TargetLoweringObjectFile::getKindForGlobal(GO: &Global, TM: Asm->TM);
1832
1833 if (Global.isDeclarationForLinker())
1834 SecName = Global.hasSection() ? Global.getSection() : "";
1835 else if (GVKind->isCommon())
1836 SecName = ".bss";
1837 else {
1838 TargetLoweringObjectFile *TLOF = Asm->TM.getObjFileLowering();
1839 MCSection *Sec = TLOF->SectionForGlobal(GO: &Global, TM: Asm->TM);
1840 SecName = Sec->getName();
1841 }
1842
1843 if (ProcessingMapDef != SecName.starts_with(Prefix: ".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(x: 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>(args&: Asm, args: std::string(SecName));
1855 }
1856 }
1857
1858 SmallVector<DIGlobalVariableExpression *, 1> GVs;
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(Prefix: ".maps"))
1870 visitMapDefType(Ty: DIGlobal->getType(), TypeId&: GVTypeId);
1871 else {
1872 const DIType *Ty = tryRemoveAtomicType(Ty: DIGlobal->getType());
1873 visitTypeEntry(Ty, TypeId&: GVTypeId, CheckPointer: false, SeenPointer: 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();
1886 if (Linkage != GlobalValue::InternalLinkage &&
1887 Linkage != GlobalValue::ExternalLinkage &&
1888 Linkage != GlobalValue::WeakAnyLinkage &&
1889 Linkage != GlobalValue::WeakODRLinkage &&
1890 Linkage != GlobalValue::ExternalWeakLinkage)
1891 continue;
1892
1893 uint32_t GVarInfo;
1894 if (Linkage == GlobalValue::InternalLinkage) {
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>(args: Global.getName(), args&: GVTypeId, args&: GVarInfo);
1904 uint32_t VarId = addType(TypeEntry: std::move(VarEntry));
1905
1906 processDeclAnnotations(Annotations: DIGlobal->getAnnotations(), BaseTypeId: VarId, ComponentIdx: -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(k: std::string(SecName));
1914 if (Inserted)
1915 It->second = std::make_unique<BTFKindDataSec>(args&: Asm, args: 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(Id: VarId, Sym: Asm->getSymbol(GV: &Global), Size);
1922
1923 if (Global.hasInitializer())
1924 processGlobalInitializer(C: 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>(Val: C))
1941 processFuncPrototypes(Fn);
1942 if (auto *CA = dyn_cast<ConstantAggregate>(Val: C)) {
1943 for (unsigned I = 0, N = CA->getNumOperands(); I < N; ++I)
1944 processGlobalInitializer(C: CA->getOperand(i_nocapture: I));
1945 }
1946}
1947
1948/// Emit proper patchable instructions.
1949bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1950 if (MI->getOpcode() == BPF::LD_imm64) {
1951 const MachineOperand &MO = MI->getOperand(i: 1);
1952 if (MO.isGlobal()) {
1953 const GlobalValue *GVal = MO.getGlobal();
1954 auto *GVar = dyn_cast<GlobalVariable>(Val: GVal);
1955 if (GVar) {
1956 if (!GVar->hasAttribute(Kind: BPFCoreSharedInfo::AmaAttr) &&
1957 !GVar->hasAttribute(Kind: BPFCoreSharedInfo::TypeIdAttr))
1958 return false;
1959
1960 // Emit "mov ri, <imm>"
1961 auto [Imm, Reloc] = PatchImms[GVar];
1962 if (Reloc == BTF::ENUM_VALUE_EXISTENCE || Reloc == BTF::ENUM_VALUE ||
1963 Reloc == BTF::BTF_TYPE_ID_LOCAL || Reloc == BTF::BTF_TYPE_ID_REMOTE)
1964 OutMI.setOpcode(BPF::LD_imm64);
1965 else
1966 OutMI.setOpcode(BPF::MOV_ri);
1967 OutMI.addOperand(Op: MCOperand::createReg(Reg: MI->getOperand(i: 0).getReg()));
1968 OutMI.addOperand(Op: MCOperand::createImm(Val: 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(i: 3);
1977 if (MO.isGlobal()) {
1978 const GlobalValue *GVal = MO.getGlobal();
1979 auto *GVar = dyn_cast<GlobalVariable>(Val: GVal);
1980 if (GVar && GVar->hasAttribute(Kind: BPFCoreSharedInfo::AmaAttr)) {
1981 uint32_t Imm = PatchImms[GVar].first;
1982 OutMI.setOpcode(MI->getOperand(i: 1).getImm());
1983 if (MI->getOperand(i: 0).isImm())
1984 OutMI.addOperand(Op: MCOperand::createImm(Val: MI->getOperand(i: 0).getImm()));
1985 else
1986 OutMI.addOperand(Op: MCOperand::createReg(Reg: MI->getOperand(i: 0).getReg()));
1987 OutMI.addOperand(Op: MCOperand::createReg(Reg: MI->getOperand(i: 2).getReg()));
1988 OutMI.addOperand(Op: MCOperand::createImm(Val: 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(x: F).second)
2006 return;
2007
2008 uint32_t ProtoTypeId;
2009 const SmallDenseMap<uint32_t, StringRef> FuncArgNames;
2010 visitSubroutineType(STy: SP->getType(), ForSubprog: false, FuncArgNames, TypeId&: ProtoTypeId);
2011 uint32_t FuncId = processDISubprogram(SP, ProtoTypeId, Scope: BTF::FUNC_EXTERN);
2012
2013 if (F->hasSection()) {
2014 StringRef SecName = F->getSection();
2015
2016 auto [It, Inserted] = DataSecEntries.try_emplace(k: std::string(SecName));
2017 if (Inserted)
2018 It->second = std::make_unique<BTFKindDataSec>(args&: Asm, args: std::string(SecName));
2019
2020 // We really don't know func size, set it to 0.
2021 It->second->addDataSecEntry(Id: FuncId, Sym: Asm->getSymbol(GV: F), Size: 0);
2022 }
2023}
2024
2025void BTFDebug::endModule() {
2026 // Collect MapDef globals if not collected yet.
2027 if (MapDefNotCollected) {
2028 processGlobals(ProcessingMapDef: true);
2029 MapDefNotCollected = false;
2030 }
2031
2032 // Collect global types/variables except MapDef globals.
2033 processGlobals(ProcessingMapDef: 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: &F);
2040 }
2041
2042 for (auto &DataSec : DataSecEntries)
2043 addType(TypeEntry: 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>(args&: TypeName, args&: IsUnion);
2062 StructTypeId = addType(TypeEntry: 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, BaseTypeId: 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(BDebug&: *this);
2080
2081 // Emit BTF sections.
2082 emitBTFSection();
2083 emitBTFExtSection();
2084}
2085