xref: /llvm-project/llvm/lib/Target/BPF/BTFDebug.cpp (revision 10b80ff0cc3e6af8fddb9003571e2cc22f9c58b2)
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/BinaryFormat/ELF.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/CodeGen/MachineOperand.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/MC/MCSectionELF.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/Support/LineIterator.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
29 #include <optional>
30 
31 using namespace llvm;
32 
33 static const char *BTFKindStr[] = {
34 #define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
35 #include "llvm/DebugInfo/BTF/BTF.def"
36 };
37 
38 static const DIType *tryRemoveAtomicType(const DIType *Ty) {
39   if (!Ty)
40     return Ty;
41   auto DerivedTy = dyn_cast<DIDerivedType>(Ty);
42   if (DerivedTy && DerivedTy->getTag() == dwarf::DW_TAG_atomic_type)
43     return DerivedTy->getBaseType();
44   return Ty;
45 }
46 
47 /// Emit a BTF common type.
48 void BTFTypeBase::emitType(MCStreamer &OS) {
49   OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
50                 ")");
51   OS.emitInt32(BTFType.NameOff);
52   OS.AddComment("0x" + Twine::utohexstr(BTFType.Info));
53   OS.emitInt32(BTFType.Info);
54   OS.emitInt32(BTFType.Size);
55 }
56 
57 BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
58                                bool NeedsFixup)
59     : DTy(DTy), NeedsFixup(NeedsFixup), Name(DTy->getName()) {
60   switch (Tag) {
61   case dwarf::DW_TAG_pointer_type:
62     Kind = BTF::BTF_KIND_PTR;
63     break;
64   case dwarf::DW_TAG_const_type:
65     Kind = BTF::BTF_KIND_CONST;
66     break;
67   case dwarf::DW_TAG_volatile_type:
68     Kind = BTF::BTF_KIND_VOLATILE;
69     break;
70   case dwarf::DW_TAG_typedef:
71     Kind = BTF::BTF_KIND_TYPEDEF;
72     break;
73   case dwarf::DW_TAG_restrict_type:
74     Kind = BTF::BTF_KIND_RESTRICT;
75     break;
76   default:
77     llvm_unreachable("Unknown DIDerivedType Tag");
78   }
79   BTFType.Info = Kind << 24;
80 }
81 
82 /// Used by DW_TAG_pointer_type only.
83 BTFTypeDerived::BTFTypeDerived(unsigned NextTypeId, unsigned Tag,
84                                StringRef Name)
85     : DTy(nullptr), NeedsFixup(false), Name(Name) {
86   Kind = BTF::BTF_KIND_PTR;
87   BTFType.Info = Kind << 24;
88   BTFType.Type = NextTypeId;
89 }
90 
91 void BTFTypeDerived::completeType(BTFDebug &BDebug) {
92   if (IsCompleted)
93     return;
94   IsCompleted = true;
95 
96   BTFType.NameOff = BDebug.addString(Name);
97 
98   if (NeedsFixup || !DTy)
99     return;
100 
101   // The base type for PTR/CONST/VOLATILE could be void.
102   const DIType *ResolvedType = tryRemoveAtomicType(DTy->getBaseType());
103   if (!ResolvedType) {
104     assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
105             Kind == BTF::BTF_KIND_VOLATILE) &&
106            "Invalid null basetype");
107     BTFType.Type = 0;
108   } else {
109     BTFType.Type = BDebug.getTypeId(ResolvedType);
110   }
111 }
112 
113 void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
114 
115 void BTFTypeDerived::setPointeeType(uint32_t PointeeType) {
116   BTFType.Type = PointeeType;
117 }
118 
119 /// Represent a struct/union forward declaration.
120 BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) {
121   Kind = BTF::BTF_KIND_FWD;
122   BTFType.Info = IsUnion << 31 | Kind << 24;
123   BTFType.Type = 0;
124 }
125 
126 void BTFTypeFwd::completeType(BTFDebug &BDebug) {
127   if (IsCompleted)
128     return;
129   IsCompleted = true;
130 
131   BTFType.NameOff = BDebug.addString(Name);
132 }
133 
134 void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
135 
136 BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits,
137                        uint32_t OffsetInBits, StringRef TypeName)
138     : Name(TypeName) {
139   // Translate IR int encoding to BTF int encoding.
140   uint8_t BTFEncoding;
141   switch (Encoding) {
142   case dwarf::DW_ATE_boolean:
143     BTFEncoding = BTF::INT_BOOL;
144     break;
145   case dwarf::DW_ATE_signed:
146   case dwarf::DW_ATE_signed_char:
147     BTFEncoding = BTF::INT_SIGNED;
148     break;
149   case dwarf::DW_ATE_unsigned:
150   case dwarf::DW_ATE_unsigned_char:
151     BTFEncoding = 0;
152     break;
153   default:
154     llvm_unreachable("Unknown BTFTypeInt Encoding");
155   }
156 
157   Kind = BTF::BTF_KIND_INT;
158   BTFType.Info = Kind << 24;
159   BTFType.Size = roundupToBytes(SizeInBits);
160   IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
161 }
162 
163 void BTFTypeInt::completeType(BTFDebug &BDebug) {
164   if (IsCompleted)
165     return;
166   IsCompleted = true;
167 
168   BTFType.NameOff = BDebug.addString(Name);
169 }
170 
171 void BTFTypeInt::emitType(MCStreamer &OS) {
172   BTFTypeBase::emitType(OS);
173   OS.AddComment("0x" + Twine::utohexstr(IntVal));
174   OS.emitInt32(IntVal);
175 }
176 
177 BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen,
178     bool IsSigned) : ETy(ETy) {
179   Kind = BTF::BTF_KIND_ENUM;
180   BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
181   BTFType.Size = roundupToBytes(ETy->getSizeInBits());
182 }
183 
184 void BTFTypeEnum::completeType(BTFDebug &BDebug) {
185   if (IsCompleted)
186     return;
187   IsCompleted = true;
188 
189   BTFType.NameOff = BDebug.addString(ETy->getName());
190 
191   DINodeArray Elements = ETy->getElements();
192   for (const auto Element : Elements) {
193     const auto *Enum = cast<DIEnumerator>(Element);
194 
195     struct BTF::BTFEnum BTFEnum;
196     BTFEnum.NameOff = BDebug.addString(Enum->getName());
197     // BTF enum value is 32bit, enforce it.
198     uint32_t Value;
199     if (Enum->isUnsigned())
200       Value = static_cast<uint32_t>(Enum->getValue().getZExtValue());
201     else
202       Value = static_cast<uint32_t>(Enum->getValue().getSExtValue());
203     BTFEnum.Val = Value;
204     EnumValues.push_back(BTFEnum);
205   }
206 }
207 
208 void BTFTypeEnum::emitType(MCStreamer &OS) {
209   BTFTypeBase::emitType(OS);
210   for (const auto &Enum : EnumValues) {
211     OS.emitInt32(Enum.NameOff);
212     OS.emitInt32(Enum.Val);
213   }
214 }
215 
216 BTFTypeEnum64::BTFTypeEnum64(const DICompositeType *ETy, uint32_t VLen,
217     bool IsSigned) : ETy(ETy) {
218   Kind = BTF::BTF_KIND_ENUM64;
219   BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
220   BTFType.Size = roundupToBytes(ETy->getSizeInBits());
221 }
222 
223 void BTFTypeEnum64::completeType(BTFDebug &BDebug) {
224   if (IsCompleted)
225     return;
226   IsCompleted = true;
227 
228   BTFType.NameOff = BDebug.addString(ETy->getName());
229 
230   DINodeArray Elements = ETy->getElements();
231   for (const auto Element : Elements) {
232     const auto *Enum = cast<DIEnumerator>(Element);
233 
234     struct BTF::BTFEnum64 BTFEnum;
235     BTFEnum.NameOff = BDebug.addString(Enum->getName());
236     uint64_t Value;
237     if (Enum->isUnsigned())
238       Value = static_cast<uint64_t>(Enum->getValue().getZExtValue());
239     else
240       Value = static_cast<uint64_t>(Enum->getValue().getSExtValue());
241     BTFEnum.Val_Lo32 = Value;
242     BTFEnum.Val_Hi32 = Value >> 32;
243     EnumValues.push_back(BTFEnum);
244   }
245 }
246 
247 void BTFTypeEnum64::emitType(MCStreamer &OS) {
248   BTFTypeBase::emitType(OS);
249   for (const auto &Enum : EnumValues) {
250     OS.emitInt32(Enum.NameOff);
251     OS.AddComment("0x" + Twine::utohexstr(Enum.Val_Lo32));
252     OS.emitInt32(Enum.Val_Lo32);
253     OS.AddComment("0x" + Twine::utohexstr(Enum.Val_Hi32));
254     OS.emitInt32(Enum.Val_Hi32);
255   }
256 }
257 
258 BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems) {
259   Kind = BTF::BTF_KIND_ARRAY;
260   BTFType.NameOff = 0;
261   BTFType.Info = Kind << 24;
262   BTFType.Size = 0;
263 
264   ArrayInfo.ElemType = ElemTypeId;
265   ArrayInfo.Nelems = NumElems;
266 }
267 
268 /// Represent a BTF array.
269 void BTFTypeArray::completeType(BTFDebug &BDebug) {
270   if (IsCompleted)
271     return;
272   IsCompleted = true;
273 
274   // The IR does not really have a type for the index.
275   // A special type for array index should have been
276   // created during initial type traversal. Just
277   // retrieve that type id.
278   ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
279 }
280 
281 void BTFTypeArray::emitType(MCStreamer &OS) {
282   BTFTypeBase::emitType(OS);
283   OS.emitInt32(ArrayInfo.ElemType);
284   OS.emitInt32(ArrayInfo.IndexType);
285   OS.emitInt32(ArrayInfo.Nelems);
286 }
287 
288 /// Represent either a struct or a union.
289 BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy, bool IsStruct,
290                              bool HasBitField, uint32_t Vlen)
291     : STy(STy), HasBitField(HasBitField) {
292   Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
293   BTFType.Size = roundupToBytes(STy->getSizeInBits());
294   BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
295 }
296 
297 void BTFTypeStruct::completeType(BTFDebug &BDebug) {
298   if (IsCompleted)
299     return;
300   IsCompleted = true;
301 
302   BTFType.NameOff = BDebug.addString(STy->getName());
303 
304   // Add struct/union members.
305   const DINodeArray Elements = STy->getElements();
306   for (const auto *Element : Elements) {
307     struct BTF::BTFMember BTFMember;
308     const auto *DDTy = cast<DIDerivedType>(Element);
309 
310     BTFMember.NameOff = BDebug.addString(DDTy->getName());
311     if (HasBitField) {
312       uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
313       BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
314     } else {
315       BTFMember.Offset = DDTy->getOffsetInBits();
316     }
317     const auto *BaseTy = tryRemoveAtomicType(DDTy->getBaseType());
318     BTFMember.Type = BDebug.getTypeId(BaseTy);
319     Members.push_back(BTFMember);
320   }
321 }
322 
323 void BTFTypeStruct::emitType(MCStreamer &OS) {
324   BTFTypeBase::emitType(OS);
325   for (const auto &Member : Members) {
326     OS.emitInt32(Member.NameOff);
327     OS.emitInt32(Member.Type);
328     OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
329     OS.emitInt32(Member.Offset);
330   }
331 }
332 
333 std::string BTFTypeStruct::getName() { return std::string(STy->getName()); }
334 
335 /// The Func kind represents both subprogram and pointee of function
336 /// pointers. If the FuncName is empty, it represents a pointee of function
337 /// pointer. Otherwise, it represents a subprogram. The func arg names
338 /// are empty for pointee of function pointer case, and are valid names
339 /// for subprogram.
340 BTFTypeFuncProto::BTFTypeFuncProto(
341     const DISubroutineType *STy, uint32_t VLen,
342     const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
343     : STy(STy), FuncArgNames(FuncArgNames) {
344   Kind = BTF::BTF_KIND_FUNC_PROTO;
345   BTFType.Info = (Kind << 24) | VLen;
346 }
347 
348 void BTFTypeFuncProto::completeType(BTFDebug &BDebug) {
349   if (IsCompleted)
350     return;
351   IsCompleted = true;
352 
353   DITypeRefArray Elements = STy->getTypeArray();
354   auto RetType = tryRemoveAtomicType(Elements[0]);
355   BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
356   BTFType.NameOff = 0;
357 
358   // For null parameter which is typically the last one
359   // to represent the vararg, encode the NameOff/Type to be 0.
360   for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
361     struct BTF::BTFParam Param;
362     auto Element = tryRemoveAtomicType(Elements[I]);
363     if (Element) {
364       Param.NameOff = BDebug.addString(FuncArgNames[I]);
365       Param.Type = BDebug.getTypeId(Element);
366     } else {
367       Param.NameOff = 0;
368       Param.Type = 0;
369     }
370     Parameters.push_back(Param);
371   }
372 }
373 
374 void BTFTypeFuncProto::emitType(MCStreamer &OS) {
375   BTFTypeBase::emitType(OS);
376   for (const auto &Param : Parameters) {
377     OS.emitInt32(Param.NameOff);
378     OS.emitInt32(Param.Type);
379   }
380 }
381 
382 BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId,
383     uint32_t Scope)
384     : Name(FuncName) {
385   Kind = BTF::BTF_KIND_FUNC;
386   BTFType.Info = (Kind << 24) | Scope;
387   BTFType.Type = ProtoTypeId;
388 }
389 
390 void BTFTypeFunc::completeType(BTFDebug &BDebug) {
391   if (IsCompleted)
392     return;
393   IsCompleted = true;
394 
395   BTFType.NameOff = BDebug.addString(Name);
396 }
397 
398 void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
399 
400 BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
401     : Name(VarName) {
402   Kind = BTF::BTF_KIND_VAR;
403   BTFType.Info = Kind << 24;
404   BTFType.Type = TypeId;
405   Info = VarInfo;
406 }
407 
408 void BTFKindVar::completeType(BTFDebug &BDebug) {
409   BTFType.NameOff = BDebug.addString(Name);
410 }
411 
412 void BTFKindVar::emitType(MCStreamer &OS) {
413   BTFTypeBase::emitType(OS);
414   OS.emitInt32(Info);
415 }
416 
417 BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
418     : Asm(AsmPrt), Name(SecName) {
419   Kind = BTF::BTF_KIND_DATASEC;
420   BTFType.Info = Kind << 24;
421   BTFType.Size = 0;
422 }
423 
424 void BTFKindDataSec::completeType(BTFDebug &BDebug) {
425   BTFType.NameOff = BDebug.addString(Name);
426   BTFType.Info |= Vars.size();
427 }
428 
429 void BTFKindDataSec::emitType(MCStreamer &OS) {
430   BTFTypeBase::emitType(OS);
431 
432   for (const auto &V : Vars) {
433     OS.emitInt32(std::get<0>(V));
434     Asm->emitLabelReference(std::get<1>(V), 4);
435     OS.emitInt32(std::get<2>(V));
436   }
437 }
438 
439 BTFTypeFloat::BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName)
440     : Name(TypeName) {
441   Kind = BTF::BTF_KIND_FLOAT;
442   BTFType.Info = Kind << 24;
443   BTFType.Size = roundupToBytes(SizeInBits);
444 }
445 
446 void BTFTypeFloat::completeType(BTFDebug &BDebug) {
447   if (IsCompleted)
448     return;
449   IsCompleted = true;
450 
451   BTFType.NameOff = BDebug.addString(Name);
452 }
453 
454 BTFTypeDeclTag::BTFTypeDeclTag(uint32_t BaseTypeId, int ComponentIdx,
455                                StringRef Tag)
456     : Tag(Tag) {
457   Kind = BTF::BTF_KIND_DECL_TAG;
458   BTFType.Info = Kind << 24;
459   BTFType.Type = BaseTypeId;
460   Info = ComponentIdx;
461 }
462 
463 void BTFTypeDeclTag::completeType(BTFDebug &BDebug) {
464   if (IsCompleted)
465     return;
466   IsCompleted = true;
467 
468   BTFType.NameOff = BDebug.addString(Tag);
469 }
470 
471 void BTFTypeDeclTag::emitType(MCStreamer &OS) {
472   BTFTypeBase::emitType(OS);
473   OS.emitInt32(Info);
474 }
475 
476 BTFTypeTypeTag::BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag)
477     : DTy(nullptr), Tag(Tag) {
478   Kind = BTF::BTF_KIND_TYPE_TAG;
479   BTFType.Info = Kind << 24;
480   BTFType.Type = NextTypeId;
481 }
482 
483 BTFTypeTypeTag::BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag)
484     : DTy(DTy), Tag(Tag) {
485   Kind = BTF::BTF_KIND_TYPE_TAG;
486   BTFType.Info = Kind << 24;
487 }
488 
489 void BTFTypeTypeTag::completeType(BTFDebug &BDebug) {
490   if (IsCompleted)
491     return;
492   IsCompleted = true;
493   BTFType.NameOff = BDebug.addString(Tag);
494   if (DTy) {
495     const DIType *ResolvedType = tryRemoveAtomicType(DTy->getBaseType());
496     if (!ResolvedType)
497       BTFType.Type = 0;
498     else
499       BTFType.Type = BDebug.getTypeId(ResolvedType);
500   }
501 }
502 
503 uint32_t BTFStringTable::addString(StringRef S) {
504   // Check whether the string already exists.
505   for (auto &OffsetM : OffsetToIdMap) {
506     if (Table[OffsetM.second] == S)
507       return OffsetM.first;
508   }
509   // Not find, add to the string table.
510   uint32_t Offset = Size;
511   OffsetToIdMap[Offset] = Table.size();
512   Table.push_back(std::string(S));
513   Size += S.size() + 1;
514   return Offset;
515 }
516 
517 BTFDebug::BTFDebug(AsmPrinter *AP)
518     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
519       LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
520       MapDefNotCollected(true) {
521   addString("\0");
522 }
523 
524 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
525                            const DIType *Ty) {
526   TypeEntry->setId(TypeEntries.size() + 1);
527   uint32_t Id = TypeEntry->getId();
528   DIToIdMap[Ty] = Id;
529   TypeEntries.push_back(std::move(TypeEntry));
530   return Id;
531 }
532 
533 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
534   TypeEntry->setId(TypeEntries.size() + 1);
535   uint32_t Id = TypeEntry->getId();
536   TypeEntries.push_back(std::move(TypeEntry));
537   return Id;
538 }
539 
540 void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
541   // Only int and binary floating point types are supported in BTF.
542   uint32_t Encoding = BTy->getEncoding();
543   std::unique_ptr<BTFTypeBase> TypeEntry;
544   switch (Encoding) {
545   case dwarf::DW_ATE_boolean:
546   case dwarf::DW_ATE_signed:
547   case dwarf::DW_ATE_signed_char:
548   case dwarf::DW_ATE_unsigned:
549   case dwarf::DW_ATE_unsigned_char:
550     // Create a BTF type instance for this DIBasicType and put it into
551     // DIToIdMap for cross-type reference check.
552     TypeEntry = std::make_unique<BTFTypeInt>(
553         Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
554     break;
555   case dwarf::DW_ATE_float:
556     TypeEntry =
557         std::make_unique<BTFTypeFloat>(BTy->getSizeInBits(), BTy->getName());
558     break;
559   default:
560     return;
561   }
562 
563   TypeId = addType(std::move(TypeEntry), BTy);
564 }
565 
566 /// Handle subprogram or subroutine types.
567 void BTFDebug::visitSubroutineType(
568     const DISubroutineType *STy, bool ForSubprog,
569     const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
570     uint32_t &TypeId) {
571   DITypeRefArray Elements = STy->getTypeArray();
572   uint32_t VLen = Elements.size() - 1;
573   if (VLen > BTF::MAX_VLEN)
574     return;
575 
576   // Subprogram has a valid non-zero-length name, and the pointee of
577   // a function pointer has an empty name. The subprogram type will
578   // not be added to DIToIdMap as it should not be referenced by
579   // any other types.
580   auto TypeEntry = std::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
581   if (ForSubprog)
582     TypeId = addType(std::move(TypeEntry)); // For subprogram
583   else
584     TypeId = addType(std::move(TypeEntry), STy); // For func ptr
585 
586   // Visit return type and func arg types.
587   for (const auto Element : Elements) {
588     visitTypeEntry(Element);
589   }
590 }
591 
592 void BTFDebug::processDeclAnnotations(DINodeArray Annotations,
593                                       uint32_t BaseTypeId,
594                                       int ComponentIdx) {
595   if (!Annotations)
596      return;
597 
598   for (const Metadata *Annotation : Annotations->operands()) {
599     const MDNode *MD = cast<MDNode>(Annotation);
600     const MDString *Name = cast<MDString>(MD->getOperand(0));
601     if (Name->getString() != "btf_decl_tag")
602       continue;
603 
604     const MDString *Value = cast<MDString>(MD->getOperand(1));
605     auto TypeEntry = std::make_unique<BTFTypeDeclTag>(BaseTypeId, ComponentIdx,
606                                                       Value->getString());
607     addType(std::move(TypeEntry));
608   }
609 }
610 
611 uint32_t BTFDebug::processDISubprogram(const DISubprogram *SP,
612                                        uint32_t ProtoTypeId, uint8_t Scope) {
613   auto FuncTypeEntry =
614       std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
615   uint32_t FuncId = addType(std::move(FuncTypeEntry));
616 
617   // Process argument annotations.
618   for (const DINode *DN : SP->getRetainedNodes()) {
619     if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
620       uint32_t Arg = DV->getArg();
621       if (Arg)
622         processDeclAnnotations(DV->getAnnotations(), FuncId, Arg - 1);
623     }
624   }
625   processDeclAnnotations(SP->getAnnotations(), FuncId, -1);
626 
627   return FuncId;
628 }
629 
630 /// Generate btf_type_tag chains.
631 int BTFDebug::genBTFTypeTags(const DIDerivedType *DTy, int BaseTypeId) {
632   SmallVector<const MDString *, 4> MDStrs;
633   DINodeArray Annots = DTy->getAnnotations();
634   if (Annots) {
635     // For type with "int __tag1 __tag2 *p", the MDStrs will have
636     // content: [__tag1, __tag2].
637     for (const Metadata *Annotations : Annots->operands()) {
638       const MDNode *MD = cast<MDNode>(Annotations);
639       const MDString *Name = cast<MDString>(MD->getOperand(0));
640       if (Name->getString() != "btf_type_tag")
641         continue;
642       MDStrs.push_back(cast<MDString>(MD->getOperand(1)));
643     }
644   }
645 
646   if (MDStrs.size() == 0)
647     return -1;
648 
649   // With MDStrs [__tag1, __tag2], the output type chain looks like
650   //   PTR -> __tag2 -> __tag1 -> BaseType
651   // In the below, we construct BTF types with the order of __tag1, __tag2
652   // and PTR.
653   unsigned TmpTypeId;
654   std::unique_ptr<BTFTypeTypeTag> TypeEntry;
655   if (BaseTypeId >= 0)
656     TypeEntry =
657         std::make_unique<BTFTypeTypeTag>(BaseTypeId, MDStrs[0]->getString());
658   else
659     TypeEntry = std::make_unique<BTFTypeTypeTag>(DTy, MDStrs[0]->getString());
660   TmpTypeId = addType(std::move(TypeEntry));
661 
662   for (unsigned I = 1; I < MDStrs.size(); I++) {
663     const MDString *Value = MDStrs[I];
664     TypeEntry = std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString());
665     TmpTypeId = addType(std::move(TypeEntry));
666   }
667   return TmpTypeId;
668 }
669 
670 /// Handle structure/union types.
671 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
672                                uint32_t &TypeId) {
673   const DINodeArray Elements = CTy->getElements();
674   uint32_t VLen = Elements.size();
675   if (VLen > BTF::MAX_VLEN)
676     return;
677 
678   // Check whether we have any bitfield members or not
679   bool HasBitField = false;
680   for (const auto *Element : Elements) {
681     auto E = cast<DIDerivedType>(Element);
682     if (E->isBitField()) {
683       HasBitField = true;
684       break;
685     }
686   }
687 
688   auto TypeEntry =
689       std::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
690   StructTypes.push_back(TypeEntry.get());
691   TypeId = addType(std::move(TypeEntry), CTy);
692 
693   // Check struct/union annotations
694   processDeclAnnotations(CTy->getAnnotations(), TypeId, -1);
695 
696   // Visit all struct members.
697   int FieldNo = 0;
698   for (const auto *Element : Elements) {
699     const auto Elem = cast<DIDerivedType>(Element);
700     visitTypeEntry(Elem);
701     processDeclAnnotations(Elem->getAnnotations(), TypeId, FieldNo);
702     FieldNo++;
703   }
704 }
705 
706 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
707   // Visit array element type.
708   uint32_t ElemTypeId;
709   const DIType *ElemType = CTy->getBaseType();
710   visitTypeEntry(ElemType, ElemTypeId, false, false);
711 
712   // Visit array dimensions.
713   DINodeArray Elements = CTy->getElements();
714   for (int I = Elements.size() - 1; I >= 0; --I) {
715     if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
716       if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
717         const DISubrange *SR = cast<DISubrange>(Element);
718         auto *CI = dyn_cast<ConstantInt *>(SR->getCount());
719         int64_t Count = CI->getSExtValue();
720 
721         // For struct s { int b; char c[]; }, the c[] will be represented
722         // as an array with Count = -1.
723         auto TypeEntry =
724             std::make_unique<BTFTypeArray>(ElemTypeId,
725                 Count >= 0 ? Count : 0);
726         if (I == 0)
727           ElemTypeId = addType(std::move(TypeEntry), CTy);
728         else
729           ElemTypeId = addType(std::move(TypeEntry));
730       }
731   }
732 
733   // The array TypeId is the type id of the outermost dimension.
734   TypeId = ElemTypeId;
735 
736   // The IR does not have a type for array index while BTF wants one.
737   // So create an array index type if there is none.
738   if (!ArrayIndexTypeId) {
739     auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
740                                                    0, "__ARRAY_SIZE_TYPE__");
741     ArrayIndexTypeId = addType(std::move(TypeEntry));
742   }
743 }
744 
745 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
746   DINodeArray Elements = CTy->getElements();
747   uint32_t VLen = Elements.size();
748   if (VLen > BTF::MAX_VLEN)
749     return;
750 
751   bool IsSigned = false;
752   unsigned NumBits = 32;
753   // No BaseType implies forward declaration in which case a
754   // BTFTypeEnum with Vlen = 0 is emitted.
755   if (CTy->getBaseType() != nullptr) {
756     const auto *BTy = cast<DIBasicType>(CTy->getBaseType());
757     IsSigned = BTy->getEncoding() == dwarf::DW_ATE_signed ||
758                BTy->getEncoding() == dwarf::DW_ATE_signed_char;
759     NumBits = BTy->getSizeInBits();
760   }
761 
762   if (NumBits <= 32) {
763     auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen, IsSigned);
764     TypeId = addType(std::move(TypeEntry), CTy);
765   } else {
766     assert(NumBits == 64);
767     auto TypeEntry = std::make_unique<BTFTypeEnum64>(CTy, VLen, IsSigned);
768     TypeId = addType(std::move(TypeEntry), CTy);
769   }
770   // No need to visit base type as BTF does not encode it.
771 }
772 
773 /// Handle structure/union forward declarations.
774 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
775                                 uint32_t &TypeId) {
776   auto TypeEntry = std::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
777   TypeId = addType(std::move(TypeEntry), CTy);
778 }
779 
780 /// Handle structure, union, array and enumeration types.
781 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
782                                   uint32_t &TypeId) {
783   auto Tag = CTy->getTag();
784   if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
785     // Handle forward declaration differently as it does not have members.
786     if (CTy->isForwardDecl())
787       visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
788     else
789       visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
790   } else if (Tag == dwarf::DW_TAG_array_type)
791     visitArrayType(CTy, TypeId);
792   else if (Tag == dwarf::DW_TAG_enumeration_type)
793     visitEnumType(CTy, TypeId);
794 }
795 
796 bool BTFDebug::IsForwardDeclCandidate(const DIType *Base) {
797   if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
798     auto CTag = CTy->getTag();
799     if ((CTag == dwarf::DW_TAG_structure_type ||
800          CTag == dwarf::DW_TAG_union_type) &&
801         !CTy->getName().empty() && !CTy->isForwardDecl())
802       return true;
803   }
804   return false;
805 }
806 
807 /// Handle pointer, typedef, const, volatile, restrict and member types.
808 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
809                                 bool CheckPointer, bool SeenPointer) {
810   unsigned Tag = DTy->getTag();
811 
812   if (Tag == dwarf::DW_TAG_atomic_type)
813     return visitTypeEntry(DTy->getBaseType(), TypeId, CheckPointer,
814                           SeenPointer);
815 
816   /// Try to avoid chasing pointees, esp. structure pointees which may
817   /// unnecessary bring in a lot of types.
818   if (CheckPointer && !SeenPointer) {
819     SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
820   }
821 
822   if (CheckPointer && SeenPointer) {
823     const DIType *Base = DTy->getBaseType();
824     if (Base) {
825       if (IsForwardDeclCandidate(Base)) {
826         /// Find a candidate, generate a fixup. Later on the struct/union
827         /// pointee type will be replaced with either a real type or
828         /// a forward declaration.
829         auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true);
830         auto &Fixup = FixupDerivedTypes[cast<DICompositeType>(Base)];
831         Fixup.push_back(std::make_pair(DTy, TypeEntry.get()));
832         TypeId = addType(std::move(TypeEntry), DTy);
833         return;
834       }
835     }
836   }
837 
838   if (Tag == dwarf::DW_TAG_pointer_type) {
839     int TmpTypeId = genBTFTypeTags(DTy, -1);
840     if (TmpTypeId >= 0) {
841       auto TypeDEntry =
842           std::make_unique<BTFTypeDerived>(TmpTypeId, Tag, DTy->getName());
843       TypeId = addType(std::move(TypeDEntry), DTy);
844     } else {
845       auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
846       TypeId = addType(std::move(TypeEntry), DTy);
847     }
848   } else if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
849              Tag == dwarf::DW_TAG_volatile_type ||
850              Tag == dwarf::DW_TAG_restrict_type) {
851     auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
852     TypeId = addType(std::move(TypeEntry), DTy);
853     if (Tag == dwarf::DW_TAG_typedef)
854       processDeclAnnotations(DTy->getAnnotations(), TypeId, -1);
855   } else if (Tag != dwarf::DW_TAG_member) {
856     return;
857   }
858 
859   // Visit base type of pointer, typedef, const, volatile, restrict or
860   // struct/union member.
861   uint32_t TempTypeId = 0;
862   if (Tag == dwarf::DW_TAG_member)
863     visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
864   else
865     visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
866 }
867 
868 /// Visit a type entry. CheckPointer is true if the type has
869 /// one of its predecessors as one struct/union member. SeenPointer
870 /// is true if CheckPointer is true and one of its predecessors
871 /// is a pointer. The goal of CheckPointer and SeenPointer is to
872 /// do pruning for struct/union types so some of these types
873 /// will not be emitted in BTF and rather forward declarations
874 /// will be generated.
875 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
876                               bool CheckPointer, bool SeenPointer) {
877   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
878     TypeId = DIToIdMap[Ty];
879 
880     // To handle the case like the following:
881     //    struct t;
882     //    typedef struct t _t;
883     //    struct s1 { _t *c; };
884     //    int test1(struct s1 *arg) { ... }
885     //
886     //    struct t { int a; int b; };
887     //    struct s2 { _t c; }
888     //    int test2(struct s2 *arg) { ... }
889     //
890     // During traversing test1() argument, "_t" is recorded
891     // in DIToIdMap and a forward declaration fixup is created
892     // for "struct t" to avoid pointee type traversal.
893     //
894     // During traversing test2() argument, even if we see "_t" is
895     // already defined, we should keep moving to eventually
896     // bring in types for "struct t". Otherwise, the "struct s2"
897     // definition won't be correct.
898     //
899     // In the above, we have following debuginfo:
900     //  {ptr, struct_member} ->  typedef -> struct
901     // and BTF type for 'typedef' is generated while 'struct' may
902     // be in FixUp. But let us generalize the above to handle
903     //  {different types} -> [various derived types]+ -> another type.
904     // For example,
905     //  {func_param, struct_member} -> const -> ptr -> volatile -> struct
906     // We will traverse const/ptr/volatile which already have corresponding
907     // BTF types and generate type for 'struct' which might be in Fixup
908     // state.
909     if (Ty && (!CheckPointer || !SeenPointer)) {
910       if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
911         while (DTy) {
912           const DIType *BaseTy = DTy->getBaseType();
913           if (!BaseTy)
914             break;
915 
916           if (DIToIdMap.find(BaseTy) != DIToIdMap.end()) {
917             DTy = dyn_cast<DIDerivedType>(BaseTy);
918           } else {
919             if (CheckPointer && DTy->getTag() == dwarf::DW_TAG_pointer_type) {
920               SeenPointer = true;
921               if (IsForwardDeclCandidate(BaseTy))
922                 break;
923             }
924             uint32_t TmpTypeId;
925             visitTypeEntry(BaseTy, TmpTypeId, CheckPointer, SeenPointer);
926             break;
927           }
928         }
929       }
930     }
931 
932     return;
933   }
934 
935   if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
936     visitBasicType(BTy, TypeId);
937   else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
938     visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
939                         TypeId);
940   else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
941     visitCompositeType(CTy, TypeId);
942   else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
943     visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
944   else
945     llvm_unreachable("Unknown DIType");
946 }
947 
948 void BTFDebug::visitTypeEntry(const DIType *Ty) {
949   uint32_t TypeId;
950   visitTypeEntry(Ty, TypeId, false, false);
951 }
952 
953 void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
954   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
955     TypeId = DIToIdMap[Ty];
956     return;
957   }
958 
959   // MapDef type may be a struct type or a non-pointer derived type
960   const DIType *OrigTy = Ty;
961   while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
962     auto Tag = DTy->getTag();
963     if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
964         Tag != dwarf::DW_TAG_volatile_type &&
965         Tag != dwarf::DW_TAG_restrict_type)
966       break;
967     Ty = DTy->getBaseType();
968   }
969 
970   const auto *CTy = dyn_cast<DICompositeType>(Ty);
971   if (!CTy)
972     return;
973 
974   auto Tag = CTy->getTag();
975   if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
976     return;
977 
978   // Visit all struct members to ensure pointee type is visited
979   const DINodeArray Elements = CTy->getElements();
980   for (const auto *Element : Elements) {
981     const auto *MemberType = cast<DIDerivedType>(Element);
982     visitTypeEntry(MemberType->getBaseType());
983   }
984 
985   // Visit this type, struct or a const/typedef/volatile/restrict type
986   visitTypeEntry(OrigTy, TypeId, false, false);
987 }
988 
989 /// Read file contents from the actual file or from the source
990 std::string BTFDebug::populateFileContent(const DIFile *File) {
991   std::string FileName;
992 
993   if (!File->getFilename().starts_with("/") && File->getDirectory().size())
994     FileName = File->getDirectory().str() + "/" + File->getFilename().str();
995   else
996     FileName = std::string(File->getFilename());
997 
998   // No need to populate the contends if it has been populated!
999   if (FileContent.contains(FileName))
1000     return FileName;
1001 
1002   std::vector<std::string> Content;
1003   std::string Line;
1004   Content.push_back(Line); // Line 0 for empty string
1005 
1006   std::unique_ptr<MemoryBuffer> Buf;
1007   auto Source = File->getSource();
1008   if (Source)
1009     Buf = MemoryBuffer::getMemBufferCopy(*Source);
1010   else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
1011                MemoryBuffer::getFile(FileName))
1012     Buf = std::move(*BufOrErr);
1013   if (Buf)
1014     for (line_iterator I(*Buf, false), E; I != E; ++I)
1015       Content.push_back(std::string(*I));
1016 
1017   FileContent[FileName] = Content;
1018   return FileName;
1019 }
1020 
1021 void BTFDebug::constructLineInfo(MCSymbol *Label, const DIFile *File,
1022                                  uint32_t Line, uint32_t Column) {
1023   std::string FileName = populateFileContent(File);
1024   BTFLineInfo LineInfo;
1025 
1026   LineInfo.Label = Label;
1027   LineInfo.FileNameOff = addString(FileName);
1028   // If file content is not available, let LineOff = 0.
1029   if (Line < FileContent[FileName].size())
1030     LineInfo.LineOff = addString(FileContent[FileName][Line]);
1031   else
1032     LineInfo.LineOff = 0;
1033   LineInfo.LineNum = Line;
1034   LineInfo.ColumnNum = Column;
1035   LineInfoTable[SecNameOff].push_back(LineInfo);
1036 }
1037 
1038 void BTFDebug::emitCommonHeader() {
1039   OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
1040   OS.emitIntValue(BTF::MAGIC, 2);
1041   OS.emitInt8(BTF::VERSION);
1042   OS.emitInt8(0);
1043 }
1044 
1045 void BTFDebug::emitBTFSection() {
1046   // Do not emit section if no types and only "" string.
1047   if (!TypeEntries.size() && StringTable.getSize() == 1)
1048     return;
1049 
1050   MCContext &Ctx = OS.getContext();
1051   MCSectionELF *Sec = Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0);
1052   Sec->setAlignment(Align(4));
1053   OS.switchSection(Sec);
1054 
1055   // Emit header.
1056   emitCommonHeader();
1057   OS.emitInt32(BTF::HeaderSize);
1058 
1059   uint32_t TypeLen = 0, StrLen;
1060   for (const auto &TypeEntry : TypeEntries)
1061     TypeLen += TypeEntry->getSize();
1062   StrLen = StringTable.getSize();
1063 
1064   OS.emitInt32(0);
1065   OS.emitInt32(TypeLen);
1066   OS.emitInt32(TypeLen);
1067   OS.emitInt32(StrLen);
1068 
1069   // Emit type table.
1070   for (const auto &TypeEntry : TypeEntries)
1071     TypeEntry->emitType(OS);
1072 
1073   // Emit string table.
1074   uint32_t StringOffset = 0;
1075   for (const auto &S : StringTable.getTable()) {
1076     OS.AddComment("string offset=" + std::to_string(StringOffset));
1077     OS.emitBytes(S);
1078     OS.emitBytes(StringRef("\0", 1));
1079     StringOffset += S.size() + 1;
1080   }
1081 }
1082 
1083 void BTFDebug::emitBTFExtSection() {
1084   // Do not emit section if empty FuncInfoTable and LineInfoTable
1085   // and FieldRelocTable.
1086   if (!FuncInfoTable.size() && !LineInfoTable.size() &&
1087       !FieldRelocTable.size())
1088     return;
1089 
1090   MCContext &Ctx = OS.getContext();
1091   MCSectionELF *Sec = Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0);
1092   Sec->setAlignment(Align(4));
1093   OS.switchSection(Sec);
1094 
1095   // Emit header.
1096   emitCommonHeader();
1097   OS.emitInt32(BTF::ExtHeaderSize);
1098 
1099   // Account for FuncInfo/LineInfo record size as well.
1100   uint32_t FuncLen = 4, LineLen = 4;
1101   // Do not account for optional FieldReloc.
1102   uint32_t FieldRelocLen = 0;
1103   for (const auto &FuncSec : FuncInfoTable) {
1104     FuncLen += BTF::SecFuncInfoSize;
1105     FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
1106   }
1107   for (const auto &LineSec : LineInfoTable) {
1108     LineLen += BTF::SecLineInfoSize;
1109     LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
1110   }
1111   for (const auto &FieldRelocSec : FieldRelocTable) {
1112     FieldRelocLen += BTF::SecFieldRelocSize;
1113     FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize;
1114   }
1115 
1116   if (FieldRelocLen)
1117     FieldRelocLen += 4;
1118 
1119   OS.emitInt32(0);
1120   OS.emitInt32(FuncLen);
1121   OS.emitInt32(FuncLen);
1122   OS.emitInt32(LineLen);
1123   OS.emitInt32(FuncLen + LineLen);
1124   OS.emitInt32(FieldRelocLen);
1125 
1126   // Emit func_info table.
1127   OS.AddComment("FuncInfo");
1128   OS.emitInt32(BTF::BPFFuncInfoSize);
1129   for (const auto &FuncSec : FuncInfoTable) {
1130     OS.AddComment("FuncInfo section string offset=" +
1131                   std::to_string(FuncSec.first));
1132     OS.emitInt32(FuncSec.first);
1133     OS.emitInt32(FuncSec.second.size());
1134     for (const auto &FuncInfo : FuncSec.second) {
1135       Asm->emitLabelReference(FuncInfo.Label, 4);
1136       OS.emitInt32(FuncInfo.TypeId);
1137     }
1138   }
1139 
1140   // Emit line_info table.
1141   OS.AddComment("LineInfo");
1142   OS.emitInt32(BTF::BPFLineInfoSize);
1143   for (const auto &LineSec : LineInfoTable) {
1144     OS.AddComment("LineInfo section string offset=" +
1145                   std::to_string(LineSec.first));
1146     OS.emitInt32(LineSec.first);
1147     OS.emitInt32(LineSec.second.size());
1148     for (const auto &LineInfo : LineSec.second) {
1149       Asm->emitLabelReference(LineInfo.Label, 4);
1150       OS.emitInt32(LineInfo.FileNameOff);
1151       OS.emitInt32(LineInfo.LineOff);
1152       OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
1153                     std::to_string(LineInfo.ColumnNum));
1154       OS.emitInt32(LineInfo.LineNum << 10 | LineInfo.ColumnNum);
1155     }
1156   }
1157 
1158   // Emit field reloc table.
1159   if (FieldRelocLen) {
1160     OS.AddComment("FieldReloc");
1161     OS.emitInt32(BTF::BPFFieldRelocSize);
1162     for (const auto &FieldRelocSec : FieldRelocTable) {
1163       OS.AddComment("Field reloc section string offset=" +
1164                     std::to_string(FieldRelocSec.first));
1165       OS.emitInt32(FieldRelocSec.first);
1166       OS.emitInt32(FieldRelocSec.second.size());
1167       for (const auto &FieldRelocInfo : FieldRelocSec.second) {
1168         Asm->emitLabelReference(FieldRelocInfo.Label, 4);
1169         OS.emitInt32(FieldRelocInfo.TypeID);
1170         OS.emitInt32(FieldRelocInfo.OffsetNameOff);
1171         OS.emitInt32(FieldRelocInfo.RelocKind);
1172       }
1173     }
1174   }
1175 }
1176 
1177 void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
1178   auto *SP = MF->getFunction().getSubprogram();
1179   auto *Unit = SP->getUnit();
1180 
1181   if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
1182     SkipInstruction = true;
1183     return;
1184   }
1185   SkipInstruction = false;
1186 
1187   // Collect MapDef types. Map definition needs to collect
1188   // pointee types. Do it first. Otherwise, for the following
1189   // case:
1190   //    struct m { ...};
1191   //    struct t {
1192   //      struct m *key;
1193   //    };
1194   //    foo(struct t *arg);
1195   //
1196   //    struct mapdef {
1197   //      ...
1198   //      struct m *key;
1199   //      ...
1200   //    } __attribute__((section(".maps"))) hash_map;
1201   //
1202   // If subroutine foo is traversed first, a type chain
1203   // "ptr->struct m(fwd)" will be created and later on
1204   // when traversing mapdef, since "ptr->struct m" exists,
1205   // the traversal of "struct m" will be omitted.
1206   if (MapDefNotCollected) {
1207     processGlobals(true);
1208     MapDefNotCollected = false;
1209   }
1210 
1211   // Collect all types locally referenced in this function.
1212   // Use RetainedNodes so we can collect all argument names
1213   // even if the argument is not used.
1214   std::unordered_map<uint32_t, StringRef> FuncArgNames;
1215   for (const DINode *DN : SP->getRetainedNodes()) {
1216     if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
1217       // Collect function arguments for subprogram func type.
1218       uint32_t Arg = DV->getArg();
1219       if (Arg) {
1220         visitTypeEntry(DV->getType());
1221         FuncArgNames[Arg] = DV->getName();
1222       }
1223     }
1224   }
1225 
1226   // Construct subprogram func proto type.
1227   uint32_t ProtoTypeId;
1228   visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
1229 
1230   // Construct subprogram func type
1231   uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL;
1232   uint32_t FuncTypeId = processDISubprogram(SP, ProtoTypeId, Scope);
1233 
1234   for (const auto &TypeEntry : TypeEntries)
1235     TypeEntry->completeType(*this);
1236 
1237   // Construct funcinfo and the first lineinfo for the function.
1238   MCSymbol *FuncLabel = Asm->getFunctionBegin();
1239   BTFFuncInfo FuncInfo;
1240   FuncInfo.Label = FuncLabel;
1241   FuncInfo.TypeId = FuncTypeId;
1242   if (FuncLabel->isInSection()) {
1243     MCSection &Section = FuncLabel->getSection();
1244     const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
1245     assert(SectionELF && "Null section for Function Label");
1246     SecNameOff = addString(SectionELF->getName());
1247   } else {
1248     SecNameOff = addString(".text");
1249   }
1250   FuncInfoTable[SecNameOff].push_back(FuncInfo);
1251 }
1252 
1253 void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
1254   SkipInstruction = false;
1255   LineInfoGenerated = false;
1256   SecNameOff = 0;
1257 }
1258 
1259 /// On-demand populate types as requested from abstract member
1260 /// accessing or preserve debuginfo type.
1261 unsigned BTFDebug::populateType(const DIType *Ty) {
1262   unsigned Id;
1263   visitTypeEntry(Ty, Id, false, false);
1264   for (const auto &TypeEntry : TypeEntries)
1265     TypeEntry->completeType(*this);
1266   return Id;
1267 }
1268 
1269 /// Generate a struct member field relocation.
1270 void BTFDebug::generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
1271                                      const GlobalVariable *GVar, bool IsAma) {
1272   BTFFieldReloc FieldReloc;
1273   FieldReloc.Label = ORSym;
1274   FieldReloc.TypeID = RootId;
1275 
1276   StringRef AccessPattern = GVar->getName();
1277   size_t FirstDollar = AccessPattern.find_first_of('$');
1278   if (IsAma) {
1279     size_t FirstColon = AccessPattern.find_first_of(':');
1280     size_t SecondColon = AccessPattern.find_first_of(':', FirstColon + 1);
1281     StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1);
1282     StringRef RelocKindStr = AccessPattern.substr(FirstColon + 1,
1283         SecondColon - FirstColon);
1284     StringRef PatchImmStr = AccessPattern.substr(SecondColon + 1,
1285         FirstDollar - SecondColon);
1286 
1287     FieldReloc.OffsetNameOff = addString(IndexPattern);
1288     FieldReloc.RelocKind = std::stoull(std::string(RelocKindStr));
1289     PatchImms[GVar] = std::make_pair(std::stoll(std::string(PatchImmStr)),
1290                                      FieldReloc.RelocKind);
1291   } else {
1292     StringRef RelocStr = AccessPattern.substr(FirstDollar + 1);
1293     FieldReloc.OffsetNameOff = addString("0");
1294     FieldReloc.RelocKind = std::stoull(std::string(RelocStr));
1295     PatchImms[GVar] = std::make_pair(RootId, FieldReloc.RelocKind);
1296   }
1297   FieldRelocTable[SecNameOff].push_back(FieldReloc);
1298 }
1299 
1300 void BTFDebug::processGlobalValue(const MachineOperand &MO) {
1301   // check whether this is a candidate or not
1302   if (MO.isGlobal()) {
1303     const GlobalValue *GVal = MO.getGlobal();
1304     auto *GVar = dyn_cast<GlobalVariable>(GVal);
1305     if (!GVar) {
1306       // Not a global variable. Maybe an extern function reference.
1307       processFuncPrototypes(dyn_cast<Function>(GVal));
1308       return;
1309     }
1310 
1311     if (!GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr) &&
1312         !GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
1313       return;
1314 
1315     MCSymbol *ORSym = OS.getContext().createTempSymbol();
1316     OS.emitLabel(ORSym);
1317 
1318     MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1319     uint32_t RootId = populateType(dyn_cast<DIType>(MDN));
1320     generatePatchImmReloc(ORSym, RootId, GVar,
1321                           GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr));
1322   }
1323 }
1324 
1325 void BTFDebug::beginInstruction(const MachineInstr *MI) {
1326   DebugHandlerBase::beginInstruction(MI);
1327 
1328   if (SkipInstruction || MI->isMetaInstruction() ||
1329       MI->getFlag(MachineInstr::FrameSetup))
1330     return;
1331 
1332   if (MI->isInlineAsm()) {
1333     // Count the number of register definitions to find the asm string.
1334     unsigned NumDefs = 0;
1335     while (true) {
1336       const MachineOperand &MO = MI->getOperand(NumDefs);
1337       if (MO.isReg() && MO.isDef()) {
1338         ++NumDefs;
1339         continue;
1340       }
1341       // Skip this inline asm instruction if the asmstr is empty.
1342       const char *AsmStr = MO.getSymbolName();
1343       if (AsmStr[0] == 0)
1344         return;
1345       break;
1346     }
1347   }
1348 
1349   if (MI->getOpcode() == BPF::LD_imm64) {
1350     // If the insn is "r2 = LD_imm64 @<an AmaAttr global>",
1351     // add this insn into the .BTF.ext FieldReloc subsection.
1352     // Relocation looks like:
1353     //  . SecName:
1354     //    . InstOffset
1355     //    . TypeID
1356     //    . OffSetNameOff
1357     //    . RelocType
1358     // Later, the insn is replaced with "r2 = <offset>"
1359     // where "<offset>" equals to the offset based on current
1360     // type definitions.
1361     //
1362     // If the insn is "r2 = LD_imm64 @<an TypeIdAttr global>",
1363     // The LD_imm64 result will be replaced with a btf type id.
1364     processGlobalValue(MI->getOperand(1));
1365   } else if (MI->getOpcode() == BPF::CORE_LD64 ||
1366              MI->getOpcode() == BPF::CORE_LD32 ||
1367              MI->getOpcode() == BPF::CORE_ST ||
1368              MI->getOpcode() == BPF::CORE_SHIFT) {
1369     // relocation insn is a load, store or shift insn.
1370     processGlobalValue(MI->getOperand(3));
1371   } else if (MI->getOpcode() == BPF::JAL) {
1372     // check extern function references
1373     const MachineOperand &MO = MI->getOperand(0);
1374     if (MO.isGlobal()) {
1375       processFuncPrototypes(dyn_cast<Function>(MO.getGlobal()));
1376     }
1377   }
1378 
1379   if (!CurMI) // no debug info
1380     return;
1381 
1382   // Skip this instruction if no DebugLoc, the DebugLoc
1383   // is the same as the previous instruction or Line is 0.
1384   const DebugLoc &DL = MI->getDebugLoc();
1385   if (!DL || PrevInstLoc == DL || DL.getLine() == 0) {
1386     // This instruction will be skipped, no LineInfo has
1387     // been generated, construct one based on function signature.
1388     if (LineInfoGenerated == false) {
1389       auto *S = MI->getMF()->getFunction().getSubprogram();
1390       if (!S)
1391         return;
1392       MCSymbol *FuncLabel = Asm->getFunctionBegin();
1393       constructLineInfo(FuncLabel, S->getFile(), S->getLine(), 0);
1394       LineInfoGenerated = true;
1395     }
1396 
1397     return;
1398   }
1399 
1400   // Create a temporary label to remember the insn for lineinfo.
1401   MCSymbol *LineSym = OS.getContext().createTempSymbol();
1402   OS.emitLabel(LineSym);
1403 
1404   // Construct the lineinfo.
1405   constructLineInfo(LineSym, DL->getFile(), DL.getLine(), DL.getCol());
1406 
1407   LineInfoGenerated = true;
1408   PrevInstLoc = DL;
1409 }
1410 
1411 void BTFDebug::processGlobals(bool ProcessingMapDef) {
1412   // Collect all types referenced by globals.
1413   const Module *M = MMI->getModule();
1414   for (const GlobalVariable &Global : M->globals()) {
1415     // Decide the section name.
1416     StringRef SecName;
1417     std::optional<SectionKind> GVKind;
1418 
1419     if (!Global.isDeclarationForLinker())
1420       GVKind = TargetLoweringObjectFile::getKindForGlobal(&Global, Asm->TM);
1421 
1422     if (Global.isDeclarationForLinker())
1423       SecName = Global.hasSection() ? Global.getSection() : "";
1424     else if (GVKind->isCommon())
1425       SecName = ".bss";
1426     else {
1427       TargetLoweringObjectFile *TLOF = Asm->TM.getObjFileLowering();
1428       MCSection *Sec = TLOF->SectionForGlobal(&Global, Asm->TM);
1429       SecName = Sec->getName();
1430     }
1431 
1432     if (ProcessingMapDef != SecName.starts_with(".maps"))
1433       continue;
1434 
1435     // Create a .rodata datasec if the global variable is an initialized
1436     // constant with private linkage and if it won't be in .rodata.str<#>
1437     // and .rodata.cst<#> sections.
1438     if (SecName == ".rodata" && Global.hasPrivateLinkage() &&
1439         DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1440       // skip .rodata.str<#> and .rodata.cst<#> sections
1441       if (!GVKind->isMergeableCString() && !GVKind->isMergeableConst()) {
1442         DataSecEntries[std::string(SecName)] =
1443             std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1444       }
1445     }
1446 
1447     SmallVector<DIGlobalVariableExpression *, 1> GVs;
1448     Global.getDebugInfo(GVs);
1449 
1450     // No type information, mostly internal, skip it.
1451     if (GVs.size() == 0)
1452       continue;
1453 
1454     uint32_t GVTypeId = 0;
1455     DIGlobalVariable *DIGlobal = nullptr;
1456     for (auto *GVE : GVs) {
1457       DIGlobal = GVE->getVariable();
1458       if (SecName.starts_with(".maps"))
1459         visitMapDefType(DIGlobal->getType(), GVTypeId);
1460       else {
1461         const DIType *Ty = tryRemoveAtomicType(DIGlobal->getType());
1462         visitTypeEntry(Ty, GVTypeId, false, false);
1463       }
1464       break;
1465     }
1466 
1467     // Only support the following globals:
1468     //  . static variables
1469     //  . non-static weak or non-weak global variables
1470     //  . weak or non-weak extern global variables
1471     // Whether DataSec is readonly or not can be found from corresponding ELF
1472     // section flags. Whether a BTF_KIND_VAR is a weak symbol or not
1473     // can be found from the corresponding ELF symbol table.
1474     auto Linkage = Global.getLinkage();
1475     if (Linkage != GlobalValue::InternalLinkage &&
1476         Linkage != GlobalValue::ExternalLinkage &&
1477         Linkage != GlobalValue::WeakAnyLinkage &&
1478         Linkage != GlobalValue::WeakODRLinkage &&
1479         Linkage != GlobalValue::ExternalWeakLinkage)
1480       continue;
1481 
1482     uint32_t GVarInfo;
1483     if (Linkage == GlobalValue::InternalLinkage) {
1484       GVarInfo = BTF::VAR_STATIC;
1485     } else if (Global.hasInitializer()) {
1486       GVarInfo = BTF::VAR_GLOBAL_ALLOCATED;
1487     } else {
1488       GVarInfo = BTF::VAR_GLOBAL_EXTERNAL;
1489     }
1490 
1491     auto VarEntry =
1492         std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1493     uint32_t VarId = addType(std::move(VarEntry));
1494 
1495     processDeclAnnotations(DIGlobal->getAnnotations(), VarId, -1);
1496 
1497     // An empty SecName means an extern variable without section attribute.
1498     if (SecName.empty())
1499       continue;
1500 
1501     // Find or create a DataSec
1502     auto [It, Inserted] = DataSecEntries.try_emplace(std::string(SecName));
1503     if (Inserted)
1504       It->second = std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1505 
1506     // Calculate symbol size
1507     const DataLayout &DL = Global.getDataLayout();
1508     uint32_t Size = DL.getTypeAllocSize(Global.getValueType());
1509 
1510     It->second->addDataSecEntry(VarId, Asm->getSymbol(&Global), Size);
1511 
1512     if (Global.hasInitializer())
1513       processGlobalInitializer(Global.getInitializer());
1514   }
1515 }
1516 
1517 /// Process global variable initializer in pursuit for function
1518 /// pointers. Add discovered (extern) functions to BTF. Some (extern)
1519 /// functions might have been missed otherwise. Every symbol needs BTF
1520 /// info when linking with bpftool. Primary use case: "static"
1521 /// initialization of BPF maps.
1522 ///
1523 /// struct {
1524 ///   __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
1525 ///   ...
1526 /// } prog_map SEC(".maps") = { .values = { extern_func } };
1527 ///
1528 void BTFDebug::processGlobalInitializer(const Constant *C) {
1529   if (auto *Fn = dyn_cast<Function>(C))
1530     processFuncPrototypes(Fn);
1531   if (auto *CA = dyn_cast<ConstantAggregate>(C)) {
1532     for (unsigned I = 0, N = CA->getNumOperands(); I < N; ++I)
1533       processGlobalInitializer(CA->getOperand(I));
1534   }
1535 }
1536 
1537 /// Emit proper patchable instructions.
1538 bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1539   if (MI->getOpcode() == BPF::LD_imm64) {
1540     const MachineOperand &MO = MI->getOperand(1);
1541     if (MO.isGlobal()) {
1542       const GlobalValue *GVal = MO.getGlobal();
1543       auto *GVar = dyn_cast<GlobalVariable>(GVal);
1544       if (GVar) {
1545         // Emit "mov ri, <imm>"
1546         int64_t Imm;
1547         uint32_t Reloc;
1548         if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr) ||
1549             GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr)) {
1550           Imm = PatchImms[GVar].first;
1551           Reloc = PatchImms[GVar].second;
1552         } else {
1553           return false;
1554         }
1555 
1556         if (Reloc == BTF::ENUM_VALUE_EXISTENCE || Reloc == BTF::ENUM_VALUE ||
1557             Reloc == BTF::BTF_TYPE_ID_LOCAL || Reloc == BTF::BTF_TYPE_ID_REMOTE)
1558           OutMI.setOpcode(BPF::LD_imm64);
1559         else
1560           OutMI.setOpcode(BPF::MOV_ri);
1561         OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1562         OutMI.addOperand(MCOperand::createImm(Imm));
1563         return true;
1564       }
1565     }
1566   } else if (MI->getOpcode() == BPF::CORE_LD64 ||
1567              MI->getOpcode() == BPF::CORE_LD32 ||
1568              MI->getOpcode() == BPF::CORE_ST ||
1569              MI->getOpcode() == BPF::CORE_SHIFT) {
1570     const MachineOperand &MO = MI->getOperand(3);
1571     if (MO.isGlobal()) {
1572       const GlobalValue *GVal = MO.getGlobal();
1573       auto *GVar = dyn_cast<GlobalVariable>(GVal);
1574       if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1575         uint32_t Imm = PatchImms[GVar].first;
1576         OutMI.setOpcode(MI->getOperand(1).getImm());
1577         if (MI->getOperand(0).isImm())
1578           OutMI.addOperand(MCOperand::createImm(MI->getOperand(0).getImm()));
1579         else
1580           OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1581         OutMI.addOperand(MCOperand::createReg(MI->getOperand(2).getReg()));
1582         OutMI.addOperand(MCOperand::createImm(Imm));
1583         return true;
1584       }
1585     }
1586   }
1587   return false;
1588 }
1589 
1590 void BTFDebug::processFuncPrototypes(const Function *F) {
1591   if (!F)
1592     return;
1593 
1594   const DISubprogram *SP = F->getSubprogram();
1595   if (!SP || SP->isDefinition())
1596     return;
1597 
1598   // Do not emit again if already emitted.
1599   if (!ProtoFunctions.insert(F).second)
1600     return;
1601 
1602   uint32_t ProtoTypeId;
1603   const std::unordered_map<uint32_t, StringRef> FuncArgNames;
1604   visitSubroutineType(SP->getType(), false, FuncArgNames, ProtoTypeId);
1605   uint32_t FuncId = processDISubprogram(SP, ProtoTypeId, BTF::FUNC_EXTERN);
1606 
1607   if (F->hasSection()) {
1608     StringRef SecName = F->getSection();
1609 
1610     auto [It, Inserted] = DataSecEntries.try_emplace(std::string(SecName));
1611     if (Inserted)
1612       It->second = std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1613 
1614     // We really don't know func size, set it to 0.
1615     It->second->addDataSecEntry(FuncId, Asm->getSymbol(F), 0);
1616   }
1617 }
1618 
1619 void BTFDebug::endModule() {
1620   // Collect MapDef globals if not collected yet.
1621   if (MapDefNotCollected) {
1622     processGlobals(true);
1623     MapDefNotCollected = false;
1624   }
1625 
1626   // Collect global types/variables except MapDef globals.
1627   processGlobals(false);
1628 
1629   for (auto &DataSec : DataSecEntries)
1630     addType(std::move(DataSec.second));
1631 
1632   // Fixups
1633   for (auto &Fixup : FixupDerivedTypes) {
1634     const DICompositeType *CTy = Fixup.first;
1635     StringRef TypeName = CTy->getName();
1636     bool IsUnion = CTy->getTag() == dwarf::DW_TAG_union_type;
1637 
1638     // Search through struct types
1639     uint32_t StructTypeId = 0;
1640     for (const auto &StructType : StructTypes) {
1641       if (StructType->getName() == TypeName) {
1642         StructTypeId = StructType->getId();
1643         break;
1644       }
1645     }
1646 
1647     if (StructTypeId == 0) {
1648       auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
1649       StructTypeId = addType(std::move(FwdTypeEntry));
1650     }
1651 
1652     for (auto &TypeInfo : Fixup.second) {
1653       const DIDerivedType *DTy = TypeInfo.first;
1654       BTFTypeDerived *BDType = TypeInfo.second;
1655 
1656       int TmpTypeId = genBTFTypeTags(DTy, StructTypeId);
1657       if (TmpTypeId >= 0)
1658         BDType->setPointeeType(TmpTypeId);
1659       else
1660         BDType->setPointeeType(StructTypeId);
1661     }
1662   }
1663 
1664   // Complete BTF type cross refereences.
1665   for (const auto &TypeEntry : TypeEntries)
1666     TypeEntry->completeType(*this);
1667 
1668   // Emit BTF sections.
1669   emitBTFSection();
1670   emitBTFExtSection();
1671 }
1672