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