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