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 
26 using namespace llvm;
27 
28 static const char *BTFKindStr[] = {
29 #define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
30 #include "BTF.def"
31 };
32 
33 /// Emit a BTF common type.
34 void BTFTypeBase::emitType(MCStreamer &OS) {
35   OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
36                 ")");
37   OS.EmitIntValue(BTFType.NameOff, 4);
38   OS.AddComment("0x" + Twine::utohexstr(BTFType.Info));
39   OS.EmitIntValue(BTFType.Info, 4);
40   OS.EmitIntValue(BTFType.Size, 4);
41 }
42 
43 BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
44                                bool NeedsFixup)
45     : DTy(DTy), NeedsFixup(NeedsFixup) {
46   switch (Tag) {
47   case dwarf::DW_TAG_pointer_type:
48     Kind = BTF::BTF_KIND_PTR;
49     break;
50   case dwarf::DW_TAG_const_type:
51     Kind = BTF::BTF_KIND_CONST;
52     break;
53   case dwarf::DW_TAG_volatile_type:
54     Kind = BTF::BTF_KIND_VOLATILE;
55     break;
56   case dwarf::DW_TAG_typedef:
57     Kind = BTF::BTF_KIND_TYPEDEF;
58     break;
59   case dwarf::DW_TAG_restrict_type:
60     Kind = BTF::BTF_KIND_RESTRICT;
61     break;
62   default:
63     llvm_unreachable("Unknown DIDerivedType Tag");
64   }
65   BTFType.Info = Kind << 24;
66 }
67 
68 void BTFTypeDerived::completeType(BTFDebug &BDebug) {
69   if (IsCompleted)
70     return;
71   IsCompleted = true;
72 
73   BTFType.NameOff = BDebug.addString(DTy->getName());
74 
75   if (NeedsFixup)
76     return;
77 
78   // The base type for PTR/CONST/VOLATILE could be void.
79   const DIType *ResolvedType = DTy->getBaseType();
80   if (!ResolvedType) {
81     assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
82             Kind == BTF::BTF_KIND_VOLATILE) &&
83            "Invalid null basetype");
84     BTFType.Type = 0;
85   } else {
86     BTFType.Type = BDebug.getTypeId(ResolvedType);
87   }
88 }
89 
90 void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
91 
92 void BTFTypeDerived::setPointeeType(uint32_t PointeeType) {
93   BTFType.Type = PointeeType;
94 }
95 
96 /// Represent a struct/union forward declaration.
97 BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) {
98   Kind = BTF::BTF_KIND_FWD;
99   BTFType.Info = IsUnion << 31 | Kind << 24;
100   BTFType.Type = 0;
101 }
102 
103 void BTFTypeFwd::completeType(BTFDebug &BDebug) {
104   if (IsCompleted)
105     return;
106   IsCompleted = true;
107 
108   BTFType.NameOff = BDebug.addString(Name);
109 }
110 
111 void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
112 
113 BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits,
114                        uint32_t OffsetInBits, StringRef TypeName)
115     : Name(TypeName) {
116   // Translate IR int encoding to BTF int encoding.
117   uint8_t BTFEncoding;
118   switch (Encoding) {
119   case dwarf::DW_ATE_boolean:
120     BTFEncoding = BTF::INT_BOOL;
121     break;
122   case dwarf::DW_ATE_signed:
123   case dwarf::DW_ATE_signed_char:
124     BTFEncoding = BTF::INT_SIGNED;
125     break;
126   case dwarf::DW_ATE_unsigned:
127   case dwarf::DW_ATE_unsigned_char:
128     BTFEncoding = 0;
129     break;
130   default:
131     llvm_unreachable("Unknown BTFTypeInt Encoding");
132   }
133 
134   Kind = BTF::BTF_KIND_INT;
135   BTFType.Info = Kind << 24;
136   BTFType.Size = roundupToBytes(SizeInBits);
137   IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
138 }
139 
140 void BTFTypeInt::completeType(BTFDebug &BDebug) {
141   if (IsCompleted)
142     return;
143   IsCompleted = true;
144 
145   BTFType.NameOff = BDebug.addString(Name);
146 }
147 
148 void BTFTypeInt::emitType(MCStreamer &OS) {
149   BTFTypeBase::emitType(OS);
150   OS.AddComment("0x" + Twine::utohexstr(IntVal));
151   OS.EmitIntValue(IntVal, 4);
152 }
153 
154 BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen) : ETy(ETy) {
155   Kind = BTF::BTF_KIND_ENUM;
156   BTFType.Info = Kind << 24 | VLen;
157   BTFType.Size = roundupToBytes(ETy->getSizeInBits());
158 }
159 
160 void BTFTypeEnum::completeType(BTFDebug &BDebug) {
161   if (IsCompleted)
162     return;
163   IsCompleted = true;
164 
165   BTFType.NameOff = BDebug.addString(ETy->getName());
166 
167   DINodeArray Elements = ETy->getElements();
168   for (const auto Element : Elements) {
169     const auto *Enum = cast<DIEnumerator>(Element);
170 
171     struct BTF::BTFEnum BTFEnum;
172     BTFEnum.NameOff = BDebug.addString(Enum->getName());
173     // BTF enum value is 32bit, enforce it.
174     BTFEnum.Val = static_cast<uint32_t>(Enum->getValue());
175     EnumValues.push_back(BTFEnum);
176   }
177 }
178 
179 void BTFTypeEnum::emitType(MCStreamer &OS) {
180   BTFTypeBase::emitType(OS);
181   for (const auto &Enum : EnumValues) {
182     OS.EmitIntValue(Enum.NameOff, 4);
183     OS.EmitIntValue(Enum.Val, 4);
184   }
185 }
186 
187 BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t ElemSize,
188                            uint32_t NumElems)
189     : ElemSize(ElemSize) {
190   Kind = BTF::BTF_KIND_ARRAY;
191   BTFType.NameOff = 0;
192   BTFType.Info = Kind << 24;
193   BTFType.Size = 0;
194 
195   ArrayInfo.ElemType = ElemTypeId;
196   ArrayInfo.Nelems = NumElems;
197 }
198 
199 /// Represent a BTF array.
200 void BTFTypeArray::completeType(BTFDebug &BDebug) {
201   if (IsCompleted)
202     return;
203   IsCompleted = true;
204 
205   // The IR does not really have a type for the index.
206   // A special type for array index should have been
207   // created during initial type traversal. Just
208   // retrieve that type id.
209   ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
210 }
211 
212 void BTFTypeArray::emitType(MCStreamer &OS) {
213   BTFTypeBase::emitType(OS);
214   OS.EmitIntValue(ArrayInfo.ElemType, 4);
215   OS.EmitIntValue(ArrayInfo.IndexType, 4);
216   OS.EmitIntValue(ArrayInfo.Nelems, 4);
217 }
218 
219 void BTFTypeArray::getLocInfo(uint32_t Loc, uint32_t &LocOffset,
220                               uint32_t &ElementTypeId) {
221   ElementTypeId = ArrayInfo.ElemType;
222   LocOffset = Loc * ElemSize;
223 }
224 
225 /// Represent either a struct or a union.
226 BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy, bool IsStruct,
227                              bool HasBitField, uint32_t Vlen)
228     : STy(STy), HasBitField(HasBitField) {
229   Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
230   BTFType.Size = roundupToBytes(STy->getSizeInBits());
231   BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
232 }
233 
234 void BTFTypeStruct::completeType(BTFDebug &BDebug) {
235   if (IsCompleted)
236     return;
237   IsCompleted = true;
238 
239   BTFType.NameOff = BDebug.addString(STy->getName());
240 
241   // Add struct/union members.
242   const DINodeArray Elements = STy->getElements();
243   for (const auto *Element : Elements) {
244     struct BTF::BTFMember BTFMember;
245     const auto *DDTy = cast<DIDerivedType>(Element);
246 
247     BTFMember.NameOff = BDebug.addString(DDTy->getName());
248     if (HasBitField) {
249       uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
250       BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
251     } else {
252       BTFMember.Offset = DDTy->getOffsetInBits();
253     }
254     BTFMember.Type = BDebug.getTypeId(DDTy->getBaseType());
255     Members.push_back(BTFMember);
256   }
257 }
258 
259 void BTFTypeStruct::emitType(MCStreamer &OS) {
260   BTFTypeBase::emitType(OS);
261   for (const auto &Member : Members) {
262     OS.EmitIntValue(Member.NameOff, 4);
263     OS.EmitIntValue(Member.Type, 4);
264     OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
265     OS.EmitIntValue(Member.Offset, 4);
266   }
267 }
268 
269 std::string BTFTypeStruct::getName() { return STy->getName(); }
270 
271 void BTFTypeStruct::getMemberInfo(uint32_t Loc, uint32_t &MemberOffset,
272                                   uint32_t &MemberType) {
273   MemberType = Members[Loc].Type;
274   MemberOffset =
275       HasBitField ? Members[Loc].Offset & 0xffffff : Members[Loc].Offset;
276 }
277 
278 uint32_t BTFTypeStruct::getStructSize() { return STy->getSizeInBits() >> 3; }
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.EmitIntValue(Param.NameOff, 4);
323     OS.EmitIntValue(Param.Type, 4);
324   }
325 }
326 
327 BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId)
328     : Name(FuncName) {
329   Kind = BTF::BTF_KIND_FUNC;
330   BTFType.Info = Kind << 24;
331   BTFType.Type = ProtoTypeId;
332 }
333 
334 void BTFTypeFunc::completeType(BTFDebug &BDebug) {
335   if (IsCompleted)
336     return;
337   IsCompleted = true;
338 
339   BTFType.NameOff = BDebug.addString(Name);
340 }
341 
342 void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
343 
344 BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
345     : Name(VarName) {
346   Kind = BTF::BTF_KIND_VAR;
347   BTFType.Info = Kind << 24;
348   BTFType.Type = TypeId;
349   Info = VarInfo;
350 }
351 
352 void BTFKindVar::completeType(BTFDebug &BDebug) {
353   BTFType.NameOff = BDebug.addString(Name);
354 }
355 
356 void BTFKindVar::emitType(MCStreamer &OS) {
357   BTFTypeBase::emitType(OS);
358   OS.EmitIntValue(Info, 4);
359 }
360 
361 BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
362     : Asm(AsmPrt), Name(SecName) {
363   Kind = BTF::BTF_KIND_DATASEC;
364   BTFType.Info = Kind << 24;
365   BTFType.Size = 0;
366 }
367 
368 void BTFKindDataSec::completeType(BTFDebug &BDebug) {
369   BTFType.NameOff = BDebug.addString(Name);
370   BTFType.Info |= Vars.size();
371 }
372 
373 void BTFKindDataSec::emitType(MCStreamer &OS) {
374   BTFTypeBase::emitType(OS);
375 
376   for (const auto &V : Vars) {
377     OS.EmitIntValue(std::get<0>(V), 4);
378     Asm->EmitLabelReference(std::get<1>(V), 4);
379     OS.EmitIntValue(std::get<2>(V), 4);
380   }
381 }
382 
383 uint32_t BTFStringTable::addString(StringRef S) {
384   // Check whether the string already exists.
385   for (auto &OffsetM : OffsetToIdMap) {
386     if (Table[OffsetM.second] == S)
387       return OffsetM.first;
388   }
389   // Not find, add to the string table.
390   uint32_t Offset = Size;
391   OffsetToIdMap[Offset] = Table.size();
392   Table.push_back(S);
393   Size += S.size() + 1;
394   return Offset;
395 }
396 
397 BTFDebug::BTFDebug(AsmPrinter *AP)
398     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
399       LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
400       MapDefNotCollected(true) {
401   addString("\0");
402 }
403 
404 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
405                            const DIType *Ty) {
406   TypeEntry->setId(TypeEntries.size() + 1);
407   uint32_t Id = TypeEntry->getId();
408   DIToIdMap[Ty] = Id;
409   TypeEntries.push_back(std::move(TypeEntry));
410   return Id;
411 }
412 
413 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
414   TypeEntry->setId(TypeEntries.size() + 1);
415   uint32_t Id = TypeEntry->getId();
416   TypeEntries.push_back(std::move(TypeEntry));
417   return Id;
418 }
419 
420 void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
421   // Only int types are supported in BTF.
422   uint32_t Encoding = BTy->getEncoding();
423   if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed &&
424       Encoding != dwarf::DW_ATE_signed_char &&
425       Encoding != dwarf::DW_ATE_unsigned &&
426       Encoding != dwarf::DW_ATE_unsigned_char)
427     return;
428 
429   // Create a BTF type instance for this DIBasicType and put it into
430   // DIToIdMap for cross-type reference check.
431   auto TypeEntry = llvm::make_unique<BTFTypeInt>(
432       Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
433   TypeId = addType(std::move(TypeEntry), BTy);
434 }
435 
436 /// Handle subprogram or subroutine types.
437 void BTFDebug::visitSubroutineType(
438     const DISubroutineType *STy, bool ForSubprog,
439     const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
440     uint32_t &TypeId) {
441   DITypeRefArray Elements = STy->getTypeArray();
442   uint32_t VLen = Elements.size() - 1;
443   if (VLen > BTF::MAX_VLEN)
444     return;
445 
446   // Subprogram has a valid non-zero-length name, and the pointee of
447   // a function pointer has an empty name. The subprogram type will
448   // not be added to DIToIdMap as it should not be referenced by
449   // any other types.
450   auto TypeEntry = llvm::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
451   if (ForSubprog)
452     TypeId = addType(std::move(TypeEntry)); // For subprogram
453   else
454     TypeId = addType(std::move(TypeEntry), STy); // For func ptr
455 
456   // Visit return type and func arg types.
457   for (const auto Element : Elements) {
458     visitTypeEntry(Element);
459   }
460 }
461 
462 /// Handle structure/union types.
463 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
464                                uint32_t &TypeId) {
465   const DINodeArray Elements = CTy->getElements();
466   uint32_t VLen = Elements.size();
467   if (VLen > BTF::MAX_VLEN)
468     return;
469 
470   // Check whether we have any bitfield members or not
471   bool HasBitField = false;
472   for (const auto *Element : Elements) {
473     auto E = cast<DIDerivedType>(Element);
474     if (E->isBitField()) {
475       HasBitField = true;
476       break;
477     }
478   }
479 
480   auto TypeEntry =
481       llvm::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
482   StructTypes.push_back(TypeEntry.get());
483   TypeId = addType(std::move(TypeEntry), CTy);
484 
485   // Visit all struct members.
486   for (const auto *Element : Elements)
487     visitTypeEntry(cast<DIDerivedType>(Element));
488 }
489 
490 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
491   // Visit array element type.
492   uint32_t ElemTypeId, ElemSize;
493   const DIType *ElemType = CTy->getBaseType();
494   visitTypeEntry(ElemType, ElemTypeId, false, false);
495   ElemSize = ElemType->getSizeInBits() >> 3;
496 
497   if (!CTy->getSizeInBits()) {
498     auto TypeEntry = llvm::make_unique<BTFTypeArray>(ElemTypeId, 0, 0);
499     ArrayTypes.push_back(TypeEntry.get());
500     ElemTypeId = addType(std::move(TypeEntry), CTy);
501   } else {
502     // Visit array dimensions.
503     DINodeArray Elements = CTy->getElements();
504     for (int I = Elements.size() - 1; I >= 0; --I) {
505       if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
506         if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
507           const DISubrange *SR = cast<DISubrange>(Element);
508           auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
509           int64_t Count = CI->getSExtValue();
510 
511           auto TypeEntry =
512               llvm::make_unique<BTFTypeArray>(ElemTypeId, ElemSize, Count);
513           ArrayTypes.push_back(TypeEntry.get());
514           if (I == 0)
515             ElemTypeId = addType(std::move(TypeEntry), CTy);
516           else
517             ElemTypeId = addType(std::move(TypeEntry));
518           ElemSize = ElemSize * Count;
519         }
520     }
521   }
522 
523   // The array TypeId is the type id of the outermost dimension.
524   TypeId = ElemTypeId;
525 
526   // The IR does not have a type for array index while BTF wants one.
527   // So create an array index type if there is none.
528   if (!ArrayIndexTypeId) {
529     auto TypeEntry = llvm::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
530                                                    0, "__ARRAY_SIZE_TYPE__");
531     ArrayIndexTypeId = addType(std::move(TypeEntry));
532   }
533 }
534 
535 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
536   DINodeArray Elements = CTy->getElements();
537   uint32_t VLen = Elements.size();
538   if (VLen > BTF::MAX_VLEN)
539     return;
540 
541   auto TypeEntry = llvm::make_unique<BTFTypeEnum>(CTy, VLen);
542   TypeId = addType(std::move(TypeEntry), CTy);
543   // No need to visit base type as BTF does not encode it.
544 }
545 
546 /// Handle structure/union forward declarations.
547 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
548                                 uint32_t &TypeId) {
549   auto TypeEntry = llvm::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
550   TypeId = addType(std::move(TypeEntry), CTy);
551 }
552 
553 /// Handle structure, union, array and enumeration types.
554 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
555                                   uint32_t &TypeId) {
556   auto Tag = CTy->getTag();
557   if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
558     // Handle forward declaration differently as it does not have members.
559     if (CTy->isForwardDecl())
560       visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
561     else
562       visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
563   } else if (Tag == dwarf::DW_TAG_array_type)
564     visitArrayType(CTy, TypeId);
565   else if (Tag == dwarf::DW_TAG_enumeration_type)
566     visitEnumType(CTy, TypeId);
567 }
568 
569 /// Handle pointer, typedef, const, volatile, restrict and member types.
570 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
571                                 bool CheckPointer, bool SeenPointer) {
572   unsigned Tag = DTy->getTag();
573 
574   /// Try to avoid chasing pointees, esp. structure pointees which may
575   /// unnecessary bring in a lot of types.
576   if (CheckPointer && !SeenPointer) {
577     SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
578   }
579 
580   if (CheckPointer && SeenPointer) {
581     const DIType *Base = DTy->getBaseType();
582     if (Base) {
583       if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
584         auto CTag = CTy->getTag();
585         if ((CTag == dwarf::DW_TAG_structure_type ||
586              CTag == dwarf::DW_TAG_union_type) &&
587             !CTy->isForwardDecl()) {
588           /// Find a candidate, generate a fixup. Later on the struct/union
589           /// pointee type will be replaced with either a real type or
590           /// a forward declaration.
591           auto TypeEntry = llvm::make_unique<BTFTypeDerived>(DTy, Tag, true);
592           auto &Fixup = FixupDerivedTypes[CTy->getName()];
593           Fixup.first = CTag == dwarf::DW_TAG_union_type;
594           Fixup.second.push_back(TypeEntry.get());
595           TypeId = addType(std::move(TypeEntry), DTy);
596           return;
597         }
598       }
599     }
600   }
601 
602   if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef ||
603       Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
604       Tag == dwarf::DW_TAG_restrict_type) {
605     auto TypeEntry = llvm::make_unique<BTFTypeDerived>(DTy, Tag, false);
606     TypeId = addType(std::move(TypeEntry), DTy);
607   } else if (Tag != dwarf::DW_TAG_member) {
608     return;
609   }
610 
611   // Visit base type of pointer, typedef, const, volatile, restrict or
612   // struct/union member.
613   uint32_t TempTypeId = 0;
614   if (Tag == dwarf::DW_TAG_member)
615     visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
616   else
617     visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
618 }
619 
620 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
621                               bool CheckPointer, bool SeenPointer) {
622   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
623     TypeId = DIToIdMap[Ty];
624     return;
625   }
626 
627   if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
628     visitBasicType(BTy, TypeId);
629   else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
630     visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
631                         TypeId);
632   else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
633     visitCompositeType(CTy, TypeId);
634   else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
635     visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
636   else
637     llvm_unreachable("Unknown DIType");
638 }
639 
640 void BTFDebug::visitTypeEntry(const DIType *Ty) {
641   uint32_t TypeId;
642   visitTypeEntry(Ty, TypeId, false, false);
643 }
644 
645 void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
646   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
647     TypeId = DIToIdMap[Ty];
648     return;
649   }
650 
651   // MapDef type is a struct type
652   const auto *CTy = dyn_cast<DICompositeType>(Ty);
653   if (!CTy)
654     return;
655 
656   auto Tag = CTy->getTag();
657   if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
658     return;
659 
660   // Record this type
661   const DINodeArray Elements = CTy->getElements();
662   bool HasBitField = false;
663   for (const auto *Element : Elements) {
664     auto E = cast<DIDerivedType>(Element);
665     if (E->isBitField()) {
666       HasBitField = true;
667       break;
668     }
669   }
670 
671   auto TypeEntry =
672       llvm::make_unique<BTFTypeStruct>(CTy, true, HasBitField, Elements.size());
673   StructTypes.push_back(TypeEntry.get());
674   TypeId = addType(std::move(TypeEntry), CTy);
675 
676   // Visit all struct members
677   for (const auto *Element : Elements) {
678     const auto *MemberType = cast<DIDerivedType>(Element);
679     visitTypeEntry(MemberType->getBaseType());
680   }
681 }
682 
683 /// Read file contents from the actual file or from the source
684 std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
685   auto File = SP->getFile();
686   std::string FileName;
687 
688   if (!File->getFilename().startswith("/") && File->getDirectory().size())
689     FileName = File->getDirectory().str() + "/" + File->getFilename().str();
690   else
691     FileName = File->getFilename();
692 
693   // No need to populate the contends if it has been populated!
694   if (FileContent.find(FileName) != FileContent.end())
695     return FileName;
696 
697   std::vector<std::string> Content;
698   std::string Line;
699   Content.push_back(Line); // Line 0 for empty string
700 
701   std::unique_ptr<MemoryBuffer> Buf;
702   auto Source = File->getSource();
703   if (Source)
704     Buf = MemoryBuffer::getMemBufferCopy(*Source);
705   else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
706                MemoryBuffer::getFile(FileName))
707     Buf = std::move(*BufOrErr);
708   if (Buf)
709     for (line_iterator I(*Buf, false), E; I != E; ++I)
710       Content.push_back(*I);
711 
712   FileContent[FileName] = Content;
713   return FileName;
714 }
715 
716 void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
717                                  uint32_t Line, uint32_t Column) {
718   std::string FileName = populateFileContent(SP);
719   BTFLineInfo LineInfo;
720 
721   LineInfo.Label = Label;
722   LineInfo.FileNameOff = addString(FileName);
723   // If file content is not available, let LineOff = 0.
724   if (Line < FileContent[FileName].size())
725     LineInfo.LineOff = addString(FileContent[FileName][Line]);
726   else
727     LineInfo.LineOff = 0;
728   LineInfo.LineNum = Line;
729   LineInfo.ColumnNum = Column;
730   LineInfoTable[SecNameOff].push_back(LineInfo);
731 }
732 
733 void BTFDebug::emitCommonHeader() {
734   OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
735   OS.EmitIntValue(BTF::MAGIC, 2);
736   OS.EmitIntValue(BTF::VERSION, 1);
737   OS.EmitIntValue(0, 1);
738 }
739 
740 void BTFDebug::emitBTFSection() {
741   // Do not emit section if no types and only "" string.
742   if (!TypeEntries.size() && StringTable.getSize() == 1)
743     return;
744 
745   MCContext &Ctx = OS.getContext();
746   OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0));
747 
748   // Emit header.
749   emitCommonHeader();
750   OS.EmitIntValue(BTF::HeaderSize, 4);
751 
752   uint32_t TypeLen = 0, StrLen;
753   for (const auto &TypeEntry : TypeEntries)
754     TypeLen += TypeEntry->getSize();
755   StrLen = StringTable.getSize();
756 
757   OS.EmitIntValue(0, 4);
758   OS.EmitIntValue(TypeLen, 4);
759   OS.EmitIntValue(TypeLen, 4);
760   OS.EmitIntValue(StrLen, 4);
761 
762   // Emit type table.
763   for (const auto &TypeEntry : TypeEntries)
764     TypeEntry->emitType(OS);
765 
766   // Emit string table.
767   uint32_t StringOffset = 0;
768   for (const auto &S : StringTable.getTable()) {
769     OS.AddComment("string offset=" + std::to_string(StringOffset));
770     OS.EmitBytes(S);
771     OS.EmitBytes(StringRef("\0", 1));
772     StringOffset += S.size() + 1;
773   }
774 }
775 
776 void BTFDebug::emitBTFExtSection() {
777   // Do not emit section if empty FuncInfoTable and LineInfoTable.
778   if (!FuncInfoTable.size() && !LineInfoTable.size() &&
779       !OffsetRelocTable.size() && !ExternRelocTable.size())
780     return;
781 
782   MCContext &Ctx = OS.getContext();
783   OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0));
784 
785   // Emit header.
786   emitCommonHeader();
787   OS.EmitIntValue(BTF::ExtHeaderSize, 4);
788 
789   // Account for FuncInfo/LineInfo record size as well.
790   uint32_t FuncLen = 4, LineLen = 4;
791   // Do not account for optional OffsetReloc/ExternReloc.
792   uint32_t OffsetRelocLen = 0, ExternRelocLen = 0;
793   for (const auto &FuncSec : FuncInfoTable) {
794     FuncLen += BTF::SecFuncInfoSize;
795     FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
796   }
797   for (const auto &LineSec : LineInfoTable) {
798     LineLen += BTF::SecLineInfoSize;
799     LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
800   }
801   for (const auto &OffsetRelocSec : OffsetRelocTable) {
802     OffsetRelocLen += BTF::SecOffsetRelocSize;
803     OffsetRelocLen += OffsetRelocSec.second.size() * BTF::BPFOffsetRelocSize;
804   }
805   for (const auto &ExternRelocSec : ExternRelocTable) {
806     ExternRelocLen += BTF::SecExternRelocSize;
807     ExternRelocLen += ExternRelocSec.second.size() * BTF::BPFExternRelocSize;
808   }
809 
810   if (OffsetRelocLen)
811     OffsetRelocLen += 4;
812   if (ExternRelocLen)
813     ExternRelocLen += 4;
814 
815   OS.EmitIntValue(0, 4);
816   OS.EmitIntValue(FuncLen, 4);
817   OS.EmitIntValue(FuncLen, 4);
818   OS.EmitIntValue(LineLen, 4);
819   OS.EmitIntValue(FuncLen + LineLen, 4);
820   OS.EmitIntValue(OffsetRelocLen, 4);
821   OS.EmitIntValue(FuncLen + LineLen + OffsetRelocLen, 4);
822   OS.EmitIntValue(ExternRelocLen, 4);
823 
824   // Emit func_info table.
825   OS.AddComment("FuncInfo");
826   OS.EmitIntValue(BTF::BPFFuncInfoSize, 4);
827   for (const auto &FuncSec : FuncInfoTable) {
828     OS.AddComment("FuncInfo section string offset=" +
829                   std::to_string(FuncSec.first));
830     OS.EmitIntValue(FuncSec.first, 4);
831     OS.EmitIntValue(FuncSec.second.size(), 4);
832     for (const auto &FuncInfo : FuncSec.second) {
833       Asm->EmitLabelReference(FuncInfo.Label, 4);
834       OS.EmitIntValue(FuncInfo.TypeId, 4);
835     }
836   }
837 
838   // Emit line_info table.
839   OS.AddComment("LineInfo");
840   OS.EmitIntValue(BTF::BPFLineInfoSize, 4);
841   for (const auto &LineSec : LineInfoTable) {
842     OS.AddComment("LineInfo section string offset=" +
843                   std::to_string(LineSec.first));
844     OS.EmitIntValue(LineSec.first, 4);
845     OS.EmitIntValue(LineSec.second.size(), 4);
846     for (const auto &LineInfo : LineSec.second) {
847       Asm->EmitLabelReference(LineInfo.Label, 4);
848       OS.EmitIntValue(LineInfo.FileNameOff, 4);
849       OS.EmitIntValue(LineInfo.LineOff, 4);
850       OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
851                     std::to_string(LineInfo.ColumnNum));
852       OS.EmitIntValue(LineInfo.LineNum << 10 | LineInfo.ColumnNum, 4);
853     }
854   }
855 
856   // Emit offset reloc table.
857   if (OffsetRelocLen) {
858     OS.AddComment("OffsetReloc");
859     OS.EmitIntValue(BTF::BPFOffsetRelocSize, 4);
860     for (const auto &OffsetRelocSec : OffsetRelocTable) {
861       OS.AddComment("Offset reloc section string offset=" +
862                     std::to_string(OffsetRelocSec.first));
863       OS.EmitIntValue(OffsetRelocSec.first, 4);
864       OS.EmitIntValue(OffsetRelocSec.second.size(), 4);
865       for (const auto &OffsetRelocInfo : OffsetRelocSec.second) {
866         Asm->EmitLabelReference(OffsetRelocInfo.Label, 4);
867         OS.EmitIntValue(OffsetRelocInfo.TypeID, 4);
868         OS.EmitIntValue(OffsetRelocInfo.OffsetNameOff, 4);
869       }
870     }
871   }
872 
873   // Emit extern reloc table.
874   if (ExternRelocLen) {
875     OS.AddComment("ExternReloc");
876     OS.EmitIntValue(BTF::BPFExternRelocSize, 4);
877     for (const auto &ExternRelocSec : ExternRelocTable) {
878       OS.AddComment("Extern reloc section string offset=" +
879                     std::to_string(ExternRelocSec.first));
880       OS.EmitIntValue(ExternRelocSec.first, 4);
881       OS.EmitIntValue(ExternRelocSec.second.size(), 4);
882       for (const auto &ExternRelocInfo : ExternRelocSec.second) {
883         Asm->EmitLabelReference(ExternRelocInfo.Label, 4);
884         OS.EmitIntValue(ExternRelocInfo.ExternNameOff, 4);
885       }
886     }
887   }
888 }
889 
890 void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
891   auto *SP = MF->getFunction().getSubprogram();
892   auto *Unit = SP->getUnit();
893 
894   if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
895     SkipInstruction = true;
896     return;
897   }
898   SkipInstruction = false;
899 
900   // Collect MapDef types. Map definition needs to collect
901   // pointee types. Do it first. Otherwise, for the following
902   // case:
903   //    struct m { ...};
904   //    struct t {
905   //      struct m *key;
906   //    };
907   //    foo(struct t *arg);
908   //
909   //    struct mapdef {
910   //      ...
911   //      struct m *key;
912   //      ...
913   //    } __attribute__((section(".maps"))) hash_map;
914   //
915   // If subroutine foo is traversed first, a type chain
916   // "ptr->struct m(fwd)" will be created and later on
917   // when traversing mapdef, since "ptr->struct m" exists,
918   // the traversal of "struct m" will be omitted.
919   if (MapDefNotCollected) {
920     processGlobals(true);
921     MapDefNotCollected = false;
922   }
923 
924   // Collect all types locally referenced in this function.
925   // Use RetainedNodes so we can collect all argument names
926   // even if the argument is not used.
927   std::unordered_map<uint32_t, StringRef> FuncArgNames;
928   for (const DINode *DN : SP->getRetainedNodes()) {
929     if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
930       // Collect function arguments for subprogram func type.
931       uint32_t Arg = DV->getArg();
932       if (Arg) {
933         visitTypeEntry(DV->getType());
934         FuncArgNames[Arg] = DV->getName();
935       }
936     }
937   }
938 
939   // Construct subprogram func proto type.
940   uint32_t ProtoTypeId;
941   visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
942 
943   // Construct subprogram func type
944   auto FuncTypeEntry =
945       llvm::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId);
946   uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
947 
948   for (const auto &TypeEntry : TypeEntries)
949     TypeEntry->completeType(*this);
950 
951   // Construct funcinfo and the first lineinfo for the function.
952   MCSymbol *FuncLabel = Asm->getFunctionBegin();
953   BTFFuncInfo FuncInfo;
954   FuncInfo.Label = FuncLabel;
955   FuncInfo.TypeId = FuncTypeId;
956   if (FuncLabel->isInSection()) {
957     MCSection &Section = FuncLabel->getSection();
958     const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
959     assert(SectionELF && "Null section for Function Label");
960     SecNameOff = addString(SectionELF->getSectionName());
961   } else {
962     SecNameOff = addString(".text");
963   }
964   FuncInfoTable[SecNameOff].push_back(FuncInfo);
965 }
966 
967 void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
968   SkipInstruction = false;
969   LineInfoGenerated = false;
970   SecNameOff = 0;
971 }
972 
973 /// On-demand populate struct types as requested from abstract member
974 /// accessing.
975 unsigned BTFDebug::populateStructType(const DIType *Ty) {
976   unsigned Id;
977   visitTypeEntry(Ty, Id, false, false);
978   for (const auto &TypeEntry : TypeEntries)
979     TypeEntry->completeType(*this);
980   return Id;
981 }
982 
983 // Find struct/array debuginfo types given a type id.
984 void BTFDebug::setTypeFromId(uint32_t TypeId, BTFTypeStruct **PrevStructType,
985                              BTFTypeArray **PrevArrayType) {
986   for (const auto &StructType : StructTypes) {
987     if (StructType->getId() == TypeId) {
988       *PrevStructType = StructType;
989       return;
990     }
991   }
992   for (const auto &ArrayType : ArrayTypes) {
993     if (ArrayType->getId() == TypeId) {
994       *PrevArrayType = ArrayType;
995       return;
996     }
997   }
998 }
999 
1000 /// Generate a struct member offset relocation.
1001 void BTFDebug::generateOffsetReloc(const MachineInstr *MI,
1002                                    const MCSymbol *ORSym, DIType *RootTy,
1003                                    StringRef AccessPattern) {
1004   BTFTypeStruct *PrevStructType = nullptr;
1005   BTFTypeArray *PrevArrayType = nullptr;
1006   unsigned RootId = populateStructType(RootTy);
1007   setTypeFromId(RootId, &PrevStructType, &PrevArrayType);
1008   unsigned RootTySize = PrevStructType->getStructSize();
1009   StringRef IndexPattern = AccessPattern.substr(AccessPattern.find_first_of(':') + 1);
1010 
1011   BTFOffsetReloc OffsetReloc;
1012   OffsetReloc.Label = ORSym;
1013   OffsetReloc.OffsetNameOff = addString(IndexPattern.drop_back());
1014   OffsetReloc.TypeID = RootId;
1015 
1016   uint32_t Start = 0, End = 0, Offset = 0;
1017   bool FirstAccess = true;
1018   for (auto C : IndexPattern) {
1019     if (C != ':') {
1020       End++;
1021     } else {
1022       std::string SubStr = IndexPattern.substr(Start, End - Start);
1023       int Loc = std::stoi(SubStr);
1024 
1025       if (FirstAccess) {
1026         Offset = Loc * RootTySize;
1027         FirstAccess = false;
1028       } else if (PrevStructType) {
1029         uint32_t MemberOffset, MemberTypeId;
1030         PrevStructType->getMemberInfo(Loc, MemberOffset, MemberTypeId);
1031 
1032         Offset += MemberOffset >> 3;
1033         PrevStructType = nullptr;
1034         setTypeFromId(MemberTypeId, &PrevStructType, &PrevArrayType);
1035       } else if (PrevArrayType) {
1036         uint32_t LocOffset, ElementTypeId;
1037         PrevArrayType->getLocInfo(Loc, LocOffset, ElementTypeId);
1038 
1039         Offset += LocOffset;
1040         PrevArrayType = nullptr;
1041         setTypeFromId(ElementTypeId, &PrevStructType, &PrevArrayType);
1042       }
1043       Start = End + 1;
1044       End = Start;
1045     }
1046   }
1047   AccessOffsets[AccessPattern.str()] = Offset;
1048   OffsetRelocTable[SecNameOff].push_back(OffsetReloc);
1049 }
1050 
1051 void BTFDebug::processLDimm64(const MachineInstr *MI) {
1052   // If the insn is an LD_imm64, the following two cases
1053   // will generate an .BTF.ext record.
1054   //
1055   // If the insn is "r2 = LD_imm64 @__BTF_...",
1056   // add this insn into the .BTF.ext OffsetReloc subsection.
1057   // Relocation looks like:
1058   //  . SecName:
1059   //    . InstOffset
1060   //    . TypeID
1061   //    . OffSetNameOff
1062   // Later, the insn is replaced with "r2 = <offset>"
1063   // where "<offset>" equals to the offset based on current
1064   // type definitions.
1065   //
1066   // If the insn is "r2 = LD_imm64 @VAR" and VAR is
1067   // a patchable external global, add this insn into the .BTF.ext
1068   // ExternReloc subsection.
1069   // Relocation looks like:
1070   //  . SecName:
1071   //    . InstOffset
1072   //    . ExternNameOff
1073   // Later, the insn is replaced with "r2 = <value>" or
1074   // "LD_imm64 r2, <value>" where "<value>" = 0.
1075 
1076   // check whether this is a candidate or not
1077   const MachineOperand &MO = MI->getOperand(1);
1078   if (MO.isGlobal()) {
1079     const GlobalValue *GVal = MO.getGlobal();
1080     auto *GVar = dyn_cast<GlobalVariable>(GVal);
1081     if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1082       MCSymbol *ORSym = OS.getContext().createTempSymbol();
1083       OS.EmitLabel(ORSym);
1084 
1085       MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1086       DIType *Ty = dyn_cast<DIType>(MDN);
1087       generateOffsetReloc(MI, ORSym, Ty, GVar->getName());
1088     } else if (GVar && !GVar->hasInitializer() && GVar->hasExternalLinkage() &&
1089                GVar->getSection() == BPFCoreSharedInfo::PatchableExtSecName) {
1090       MCSymbol *ORSym = OS.getContext().createTempSymbol();
1091       OS.EmitLabel(ORSym);
1092 
1093       BTFExternReloc ExternReloc;
1094       ExternReloc.Label = ORSym;
1095       ExternReloc.ExternNameOff = addString(GVar->getName());
1096       ExternRelocTable[SecNameOff].push_back(ExternReloc);
1097     }
1098   }
1099 }
1100 
1101 void BTFDebug::beginInstruction(const MachineInstr *MI) {
1102   DebugHandlerBase::beginInstruction(MI);
1103 
1104   if (SkipInstruction || MI->isMetaInstruction() ||
1105       MI->getFlag(MachineInstr::FrameSetup))
1106     return;
1107 
1108   if (MI->isInlineAsm()) {
1109     // Count the number of register definitions to find the asm string.
1110     unsigned NumDefs = 0;
1111     for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
1112          ++NumDefs)
1113       ;
1114 
1115     // Skip this inline asm instruction if the asmstr is empty.
1116     const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1117     if (AsmStr[0] == 0)
1118       return;
1119   }
1120 
1121   if (MI->getOpcode() == BPF::LD_imm64)
1122     processLDimm64(MI);
1123 
1124   // Skip this instruction if no DebugLoc or the DebugLoc
1125   // is the same as the previous instruction.
1126   const DebugLoc &DL = MI->getDebugLoc();
1127   if (!DL || PrevInstLoc == DL) {
1128     // This instruction will be skipped, no LineInfo has
1129     // been generated, construct one based on function signature.
1130     if (LineInfoGenerated == false) {
1131       auto *S = MI->getMF()->getFunction().getSubprogram();
1132       MCSymbol *FuncLabel = Asm->getFunctionBegin();
1133       constructLineInfo(S, FuncLabel, S->getLine(), 0);
1134       LineInfoGenerated = true;
1135     }
1136 
1137     return;
1138   }
1139 
1140   // Create a temporary label to remember the insn for lineinfo.
1141   MCSymbol *LineSym = OS.getContext().createTempSymbol();
1142   OS.EmitLabel(LineSym);
1143 
1144   // Construct the lineinfo.
1145   auto SP = DL.get()->getScope()->getSubprogram();
1146   constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
1147 
1148   LineInfoGenerated = true;
1149   PrevInstLoc = DL;
1150 }
1151 
1152 void BTFDebug::processGlobals(bool ProcessingMapDef) {
1153   // Collect all types referenced by globals.
1154   const Module *M = MMI->getModule();
1155   for (const GlobalVariable &Global : M->globals()) {
1156     // Ignore external globals for now.
1157     if (!Global.hasInitializer() && Global.hasExternalLinkage())
1158       continue;
1159 
1160     // Decide the section name.
1161     StringRef SecName;
1162     if (Global.hasSection()) {
1163       SecName = Global.getSection();
1164     } else {
1165       // data, bss, or readonly sections
1166       if (Global.isConstant())
1167         SecName = ".rodata";
1168       else
1169         SecName = Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
1170     }
1171 
1172     if (ProcessingMapDef != SecName.startswith(".maps"))
1173       continue;
1174 
1175     SmallVector<DIGlobalVariableExpression *, 1> GVs;
1176     Global.getDebugInfo(GVs);
1177     uint32_t GVTypeId = 0;
1178     for (auto *GVE : GVs) {
1179       if (SecName.startswith(".maps"))
1180         visitMapDefType(GVE->getVariable()->getType(), GVTypeId);
1181       else
1182         visitTypeEntry(GVE->getVariable()->getType(), GVTypeId, false, false);
1183       break;
1184     }
1185 
1186     // Only support the following globals:
1187     //  . static variables
1188     //  . non-static global variables with section attributes
1189     // Essentially means:
1190     //  . .bcc/.data/.rodata DataSec entities only contain static data
1191     //  . Other DataSec entities contain static or initialized global data.
1192     //    Initialized global data are mostly used for finding map key/value type
1193     //    id's. Whether DataSec is readonly or not can be found from
1194     //    corresponding ELF section flags.
1195     auto Linkage = Global.getLinkage();
1196     if (Linkage != GlobalValue::InternalLinkage &&
1197         (Linkage != GlobalValue::ExternalLinkage || !Global.hasSection()))
1198       continue;
1199 
1200     uint32_t GVarInfo = Linkage == GlobalValue::ExternalLinkage
1201                             ? BTF::VAR_GLOBAL_ALLOCATED
1202                             : BTF::VAR_STATIC;
1203     auto VarEntry =
1204         llvm::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1205     uint32_t VarId = addType(std::move(VarEntry));
1206 
1207     // Find or create a DataSec
1208     if (DataSecEntries.find(SecName) == DataSecEntries.end()) {
1209       DataSecEntries[SecName] = llvm::make_unique<BTFKindDataSec>(Asm, SecName);
1210     }
1211 
1212     // Calculate symbol size
1213     const DataLayout &DL = Global.getParent()->getDataLayout();
1214     uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());
1215 
1216     DataSecEntries[SecName]->addVar(VarId, Asm->getSymbol(&Global), Size);
1217   }
1218 }
1219 
1220 /// Emit proper patchable instructions.
1221 bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1222   if (MI->getOpcode() == BPF::LD_imm64) {
1223     const MachineOperand &MO = MI->getOperand(1);
1224     if (MO.isGlobal()) {
1225       const GlobalValue *GVal = MO.getGlobal();
1226       auto *GVar = dyn_cast<GlobalVariable>(GVal);
1227       if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1228         MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1229         DIType *Ty = dyn_cast<DIType>(MDN);
1230         std::string TypeName = Ty->getName();
1231         int64_t Imm = AccessOffsets[GVar->getName().str()];
1232 
1233         // Emit "mov ri, <imm>" for abstract member accesses.
1234         OutMI.setOpcode(BPF::MOV_ri);
1235         OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1236         OutMI.addOperand(MCOperand::createImm(Imm));
1237         return true;
1238       } else if (GVar && !GVar->hasInitializer() &&
1239                  GVar->hasExternalLinkage() &&
1240                  GVar->getSection() == BPFCoreSharedInfo::PatchableExtSecName) {
1241         const IntegerType *IntTy = dyn_cast<IntegerType>(GVar->getValueType());
1242         assert(IntTy);
1243         // For patchable externals, emit "LD_imm64, ri, 0" if the external
1244         // variable is 64bit width, emit "mov ri, 0" otherwise.
1245         if (IntTy->getBitWidth() == 64)
1246           OutMI.setOpcode(BPF::LD_imm64);
1247         else
1248           OutMI.setOpcode(BPF::MOV_ri);
1249         OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1250         OutMI.addOperand(MCOperand::createImm(0));
1251         return true;
1252       }
1253     }
1254   }
1255   return false;
1256 }
1257 
1258 void BTFDebug::endModule() {
1259   // Collect MapDef globals if not collected yet.
1260   if (MapDefNotCollected) {
1261     processGlobals(true);
1262     MapDefNotCollected = false;
1263   }
1264 
1265   // Collect global types/variables except MapDef globals.
1266   processGlobals(false);
1267   for (auto &DataSec : DataSecEntries)
1268     addType(std::move(DataSec.second));
1269 
1270   // Fixups
1271   for (auto &Fixup : FixupDerivedTypes) {
1272     StringRef TypeName = Fixup.first;
1273     bool IsUnion = Fixup.second.first;
1274 
1275     // Search through struct types
1276     uint32_t StructTypeId = 0;
1277     for (const auto &StructType : StructTypes) {
1278       if (StructType->getName() == TypeName) {
1279         StructTypeId = StructType->getId();
1280         break;
1281       }
1282     }
1283 
1284     if (StructTypeId == 0) {
1285       auto FwdTypeEntry = llvm::make_unique<BTFTypeFwd>(TypeName, IsUnion);
1286       StructTypeId = addType(std::move(FwdTypeEntry));
1287     }
1288 
1289     for (auto &DType : Fixup.second.second) {
1290       DType->setPointeeType(StructTypeId);
1291     }
1292   }
1293 
1294   // Complete BTF type cross refereences.
1295   for (const auto &TypeEntry : TypeEntries)
1296     TypeEntry->completeType(*this);
1297 
1298   // Emit BTF sections.
1299   emitBTFSection();
1300   emitBTFExtSection();
1301 }
1302