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