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