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