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