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