xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/BTFDebug.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 /// Handle structure/union types.
556 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
557                                uint32_t &TypeId) {
558   const DINodeArray Elements = CTy->getElements();
559   uint32_t VLen = Elements.size();
560   if (VLen > BTF::MAX_VLEN)
561     return;
562 
563   // Check whether we have any bitfield members or not
564   bool HasBitField = false;
565   for (const auto *Element : Elements) {
566     auto E = cast<DIDerivedType>(Element);
567     if (E->isBitField()) {
568       HasBitField = true;
569       break;
570     }
571   }
572 
573   auto TypeEntry =
574       std::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
575   StructTypes.push_back(TypeEntry.get());
576   TypeId = addType(std::move(TypeEntry), CTy);
577 
578   // Check struct/union annotations
579   processDeclAnnotations(CTy->getAnnotations(), TypeId, -1);
580 
581   // Visit all struct members.
582   int FieldNo = 0;
583   for (const auto *Element : Elements) {
584     const auto Elem = cast<DIDerivedType>(Element);
585     visitTypeEntry(Elem);
586     processDeclAnnotations(Elem->getAnnotations(), TypeId, FieldNo);
587     FieldNo++;
588   }
589 }
590 
591 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
592   // Visit array element type.
593   uint32_t ElemTypeId;
594   const DIType *ElemType = CTy->getBaseType();
595   visitTypeEntry(ElemType, ElemTypeId, false, false);
596 
597   // Visit array dimensions.
598   DINodeArray Elements = CTy->getElements();
599   for (int I = Elements.size() - 1; I >= 0; --I) {
600     if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
601       if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
602         const DISubrange *SR = cast<DISubrange>(Element);
603         auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
604         int64_t Count = CI->getSExtValue();
605 
606         // For struct s { int b; char c[]; }, the c[] will be represented
607         // as an array with Count = -1.
608         auto TypeEntry =
609             std::make_unique<BTFTypeArray>(ElemTypeId,
610                 Count >= 0 ? Count : 0);
611         if (I == 0)
612           ElemTypeId = addType(std::move(TypeEntry), CTy);
613         else
614           ElemTypeId = addType(std::move(TypeEntry));
615       }
616   }
617 
618   // The array TypeId is the type id of the outermost dimension.
619   TypeId = ElemTypeId;
620 
621   // The IR does not have a type for array index while BTF wants one.
622   // So create an array index type if there is none.
623   if (!ArrayIndexTypeId) {
624     auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
625                                                    0, "__ARRAY_SIZE_TYPE__");
626     ArrayIndexTypeId = addType(std::move(TypeEntry));
627   }
628 }
629 
630 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
631   DINodeArray Elements = CTy->getElements();
632   uint32_t VLen = Elements.size();
633   if (VLen > BTF::MAX_VLEN)
634     return;
635 
636   auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen);
637   TypeId = addType(std::move(TypeEntry), CTy);
638   // No need to visit base type as BTF does not encode it.
639 }
640 
641 /// Handle structure/union forward declarations.
642 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
643                                 uint32_t &TypeId) {
644   auto TypeEntry = std::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
645   TypeId = addType(std::move(TypeEntry), CTy);
646 }
647 
648 /// Handle structure, union, array and enumeration types.
649 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
650                                   uint32_t &TypeId) {
651   auto Tag = CTy->getTag();
652   if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
653     // Handle forward declaration differently as it does not have members.
654     if (CTy->isForwardDecl())
655       visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
656     else
657       visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
658   } else if (Tag == dwarf::DW_TAG_array_type)
659     visitArrayType(CTy, TypeId);
660   else if (Tag == dwarf::DW_TAG_enumeration_type)
661     visitEnumType(CTy, TypeId);
662 }
663 
664 /// Handle pointer, typedef, const, volatile, restrict and member types.
665 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
666                                 bool CheckPointer, bool SeenPointer) {
667   unsigned Tag = DTy->getTag();
668 
669   /// Try to avoid chasing pointees, esp. structure pointees which may
670   /// unnecessary bring in a lot of types.
671   if (CheckPointer && !SeenPointer) {
672     SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
673   }
674 
675   if (CheckPointer && SeenPointer) {
676     const DIType *Base = DTy->getBaseType();
677     if (Base) {
678       if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
679         auto CTag = CTy->getTag();
680         if ((CTag == dwarf::DW_TAG_structure_type ||
681              CTag == dwarf::DW_TAG_union_type) &&
682             !CTy->getName().empty() && !CTy->isForwardDecl()) {
683           /// Find a candidate, generate a fixup. Later on the struct/union
684           /// pointee type will be replaced with either a real type or
685           /// a forward declaration.
686           auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true);
687           auto &Fixup = FixupDerivedTypes[CTy->getName()];
688           Fixup.first = CTag == dwarf::DW_TAG_union_type;
689           Fixup.second.push_back(TypeEntry.get());
690           TypeId = addType(std::move(TypeEntry), DTy);
691           return;
692         }
693       }
694     }
695   }
696 
697   if (Tag == dwarf::DW_TAG_pointer_type) {
698     SmallVector<const MDString *, 4> MDStrs;
699     DINodeArray Annots = DTy->getAnnotations();
700     if (Annots) {
701       // For type with "int __tag1 __tag2 *p", the MDStrs will have
702       // content: [__tag1, __tag2].
703       for (const Metadata *Annotations : Annots->operands()) {
704         const MDNode *MD = cast<MDNode>(Annotations);
705         const MDString *Name = cast<MDString>(MD->getOperand(0));
706         if (!Name->getString().equals("btf_type_tag"))
707           continue;
708         MDStrs.push_back(cast<MDString>(MD->getOperand(1)));
709       }
710     }
711 
712     if (MDStrs.size() > 0) {
713       // With MDStrs [__tag1, __tag2], the output type chain looks like
714       //   PTR -> __tag2 -> __tag1 -> BaseType
715       // In the below, we construct BTF types with the order of __tag1, __tag2
716       // and PTR.
717       auto TypeEntry =
718           std::make_unique<BTFTypeTypeTag>(DTy, MDStrs[0]->getString());
719       unsigned TmpTypeId = addType(std::move(TypeEntry));
720       for (unsigned I = 1; I < MDStrs.size(); I++) {
721         const MDString *Value = MDStrs[I];
722         TypeEntry =
723             std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString());
724         TmpTypeId = addType(std::move(TypeEntry));
725       }
726       auto TypeDEntry =
727           std::make_unique<BTFTypeDerived>(TmpTypeId, Tag, DTy->getName());
728       TypeId = addType(std::move(TypeDEntry), DTy);
729     } else {
730       auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
731       TypeId = addType(std::move(TypeEntry), DTy);
732     }
733   } else if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
734              Tag == dwarf::DW_TAG_volatile_type ||
735              Tag == dwarf::DW_TAG_restrict_type) {
736     auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
737     TypeId = addType(std::move(TypeEntry), DTy);
738     if (Tag == dwarf::DW_TAG_typedef)
739       processDeclAnnotations(DTy->getAnnotations(), TypeId, -1);
740   } else if (Tag != dwarf::DW_TAG_member) {
741     return;
742   }
743 
744   // Visit base type of pointer, typedef, const, volatile, restrict or
745   // struct/union member.
746   uint32_t TempTypeId = 0;
747   if (Tag == dwarf::DW_TAG_member)
748     visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
749   else
750     visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
751 }
752 
753 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
754                               bool CheckPointer, bool SeenPointer) {
755   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
756     TypeId = DIToIdMap[Ty];
757 
758     // To handle the case like the following:
759     //    struct t;
760     //    typedef struct t _t;
761     //    struct s1 { _t *c; };
762     //    int test1(struct s1 *arg) { ... }
763     //
764     //    struct t { int a; int b; };
765     //    struct s2 { _t c; }
766     //    int test2(struct s2 *arg) { ... }
767     //
768     // During traversing test1() argument, "_t" is recorded
769     // in DIToIdMap and a forward declaration fixup is created
770     // for "struct t" to avoid pointee type traversal.
771     //
772     // During traversing test2() argument, even if we see "_t" is
773     // already defined, we should keep moving to eventually
774     // bring in types for "struct t". Otherwise, the "struct s2"
775     // definition won't be correct.
776     if (Ty && (!CheckPointer || !SeenPointer)) {
777       if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
778         unsigned Tag = DTy->getTag();
779         if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
780             Tag == dwarf::DW_TAG_volatile_type ||
781             Tag == dwarf::DW_TAG_restrict_type) {
782           uint32_t TmpTypeId;
783           visitTypeEntry(DTy->getBaseType(), TmpTypeId, CheckPointer,
784                          SeenPointer);
785         }
786       }
787     }
788 
789     return;
790   }
791 
792   if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
793     visitBasicType(BTy, TypeId);
794   else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
795     visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
796                         TypeId);
797   else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
798     visitCompositeType(CTy, TypeId);
799   else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
800     visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
801   else
802     llvm_unreachable("Unknown DIType");
803 }
804 
805 void BTFDebug::visitTypeEntry(const DIType *Ty) {
806   uint32_t TypeId;
807   visitTypeEntry(Ty, TypeId, false, false);
808 }
809 
810 void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
811   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
812     TypeId = DIToIdMap[Ty];
813     return;
814   }
815 
816   // MapDef type may be a struct type or a non-pointer derived type
817   const DIType *OrigTy = Ty;
818   while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
819     auto Tag = DTy->getTag();
820     if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
821         Tag != dwarf::DW_TAG_volatile_type &&
822         Tag != dwarf::DW_TAG_restrict_type)
823       break;
824     Ty = DTy->getBaseType();
825   }
826 
827   const auto *CTy = dyn_cast<DICompositeType>(Ty);
828   if (!CTy)
829     return;
830 
831   auto Tag = CTy->getTag();
832   if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
833     return;
834 
835   // Visit all struct members to ensure pointee type is visited
836   const DINodeArray Elements = CTy->getElements();
837   for (const auto *Element : Elements) {
838     const auto *MemberType = cast<DIDerivedType>(Element);
839     visitTypeEntry(MemberType->getBaseType());
840   }
841 
842   // Visit this type, struct or a const/typedef/volatile/restrict type
843   visitTypeEntry(OrigTy, TypeId, false, false);
844 }
845 
846 /// Read file contents from the actual file or from the source
847 std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
848   auto File = SP->getFile();
849   std::string FileName;
850 
851   if (!File->getFilename().startswith("/") && File->getDirectory().size())
852     FileName = File->getDirectory().str() + "/" + File->getFilename().str();
853   else
854     FileName = std::string(File->getFilename());
855 
856   // No need to populate the contends if it has been populated!
857   if (FileContent.find(FileName) != FileContent.end())
858     return FileName;
859 
860   std::vector<std::string> Content;
861   std::string Line;
862   Content.push_back(Line); // Line 0 for empty string
863 
864   std::unique_ptr<MemoryBuffer> Buf;
865   auto Source = File->getSource();
866   if (Source)
867     Buf = MemoryBuffer::getMemBufferCopy(*Source);
868   else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
869                MemoryBuffer::getFile(FileName))
870     Buf = std::move(*BufOrErr);
871   if (Buf)
872     for (line_iterator I(*Buf, false), E; I != E; ++I)
873       Content.push_back(std::string(*I));
874 
875   FileContent[FileName] = Content;
876   return FileName;
877 }
878 
879 void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
880                                  uint32_t Line, uint32_t Column) {
881   std::string FileName = populateFileContent(SP);
882   BTFLineInfo LineInfo;
883 
884   LineInfo.Label = Label;
885   LineInfo.FileNameOff = addString(FileName);
886   // If file content is not available, let LineOff = 0.
887   if (Line < FileContent[FileName].size())
888     LineInfo.LineOff = addString(FileContent[FileName][Line]);
889   else
890     LineInfo.LineOff = 0;
891   LineInfo.LineNum = Line;
892   LineInfo.ColumnNum = Column;
893   LineInfoTable[SecNameOff].push_back(LineInfo);
894 }
895 
896 void BTFDebug::emitCommonHeader() {
897   OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
898   OS.emitIntValue(BTF::MAGIC, 2);
899   OS.emitInt8(BTF::VERSION);
900   OS.emitInt8(0);
901 }
902 
903 void BTFDebug::emitBTFSection() {
904   // Do not emit section if no types and only "" string.
905   if (!TypeEntries.size() && StringTable.getSize() == 1)
906     return;
907 
908   MCContext &Ctx = OS.getContext();
909   MCSectionELF *Sec = Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0);
910   Sec->setAlignment(Align(4));
911   OS.SwitchSection(Sec);
912 
913   // Emit header.
914   emitCommonHeader();
915   OS.emitInt32(BTF::HeaderSize);
916 
917   uint32_t TypeLen = 0, StrLen;
918   for (const auto &TypeEntry : TypeEntries)
919     TypeLen += TypeEntry->getSize();
920   StrLen = StringTable.getSize();
921 
922   OS.emitInt32(0);
923   OS.emitInt32(TypeLen);
924   OS.emitInt32(TypeLen);
925   OS.emitInt32(StrLen);
926 
927   // Emit type table.
928   for (const auto &TypeEntry : TypeEntries)
929     TypeEntry->emitType(OS);
930 
931   // Emit string table.
932   uint32_t StringOffset = 0;
933   for (const auto &S : StringTable.getTable()) {
934     OS.AddComment("string offset=" + std::to_string(StringOffset));
935     OS.emitBytes(S);
936     OS.emitBytes(StringRef("\0", 1));
937     StringOffset += S.size() + 1;
938   }
939 }
940 
941 void BTFDebug::emitBTFExtSection() {
942   // Do not emit section if empty FuncInfoTable and LineInfoTable
943   // and FieldRelocTable.
944   if (!FuncInfoTable.size() && !LineInfoTable.size() &&
945       !FieldRelocTable.size())
946     return;
947 
948   MCContext &Ctx = OS.getContext();
949   MCSectionELF *Sec = Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0);
950   Sec->setAlignment(Align(4));
951   OS.SwitchSection(Sec);
952 
953   // Emit header.
954   emitCommonHeader();
955   OS.emitInt32(BTF::ExtHeaderSize);
956 
957   // Account for FuncInfo/LineInfo record size as well.
958   uint32_t FuncLen = 4, LineLen = 4;
959   // Do not account for optional FieldReloc.
960   uint32_t FieldRelocLen = 0;
961   for (const auto &FuncSec : FuncInfoTable) {
962     FuncLen += BTF::SecFuncInfoSize;
963     FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
964   }
965   for (const auto &LineSec : LineInfoTable) {
966     LineLen += BTF::SecLineInfoSize;
967     LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
968   }
969   for (const auto &FieldRelocSec : FieldRelocTable) {
970     FieldRelocLen += BTF::SecFieldRelocSize;
971     FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize;
972   }
973 
974   if (FieldRelocLen)
975     FieldRelocLen += 4;
976 
977   OS.emitInt32(0);
978   OS.emitInt32(FuncLen);
979   OS.emitInt32(FuncLen);
980   OS.emitInt32(LineLen);
981   OS.emitInt32(FuncLen + LineLen);
982   OS.emitInt32(FieldRelocLen);
983 
984   // Emit func_info table.
985   OS.AddComment("FuncInfo");
986   OS.emitInt32(BTF::BPFFuncInfoSize);
987   for (const auto &FuncSec : FuncInfoTable) {
988     OS.AddComment("FuncInfo section string offset=" +
989                   std::to_string(FuncSec.first));
990     OS.emitInt32(FuncSec.first);
991     OS.emitInt32(FuncSec.second.size());
992     for (const auto &FuncInfo : FuncSec.second) {
993       Asm->emitLabelReference(FuncInfo.Label, 4);
994       OS.emitInt32(FuncInfo.TypeId);
995     }
996   }
997 
998   // Emit line_info table.
999   OS.AddComment("LineInfo");
1000   OS.emitInt32(BTF::BPFLineInfoSize);
1001   for (const auto &LineSec : LineInfoTable) {
1002     OS.AddComment("LineInfo section string offset=" +
1003                   std::to_string(LineSec.first));
1004     OS.emitInt32(LineSec.first);
1005     OS.emitInt32(LineSec.second.size());
1006     for (const auto &LineInfo : LineSec.second) {
1007       Asm->emitLabelReference(LineInfo.Label, 4);
1008       OS.emitInt32(LineInfo.FileNameOff);
1009       OS.emitInt32(LineInfo.LineOff);
1010       OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
1011                     std::to_string(LineInfo.ColumnNum));
1012       OS.emitInt32(LineInfo.LineNum << 10 | LineInfo.ColumnNum);
1013     }
1014   }
1015 
1016   // Emit field reloc table.
1017   if (FieldRelocLen) {
1018     OS.AddComment("FieldReloc");
1019     OS.emitInt32(BTF::BPFFieldRelocSize);
1020     for (const auto &FieldRelocSec : FieldRelocTable) {
1021       OS.AddComment("Field reloc section string offset=" +
1022                     std::to_string(FieldRelocSec.first));
1023       OS.emitInt32(FieldRelocSec.first);
1024       OS.emitInt32(FieldRelocSec.second.size());
1025       for (const auto &FieldRelocInfo : FieldRelocSec.second) {
1026         Asm->emitLabelReference(FieldRelocInfo.Label, 4);
1027         OS.emitInt32(FieldRelocInfo.TypeID);
1028         OS.emitInt32(FieldRelocInfo.OffsetNameOff);
1029         OS.emitInt32(FieldRelocInfo.RelocKind);
1030       }
1031     }
1032   }
1033 }
1034 
1035 void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
1036   auto *SP = MF->getFunction().getSubprogram();
1037   auto *Unit = SP->getUnit();
1038 
1039   if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
1040     SkipInstruction = true;
1041     return;
1042   }
1043   SkipInstruction = false;
1044 
1045   // Collect MapDef types. Map definition needs to collect
1046   // pointee types. Do it first. Otherwise, for the following
1047   // case:
1048   //    struct m { ...};
1049   //    struct t {
1050   //      struct m *key;
1051   //    };
1052   //    foo(struct t *arg);
1053   //
1054   //    struct mapdef {
1055   //      ...
1056   //      struct m *key;
1057   //      ...
1058   //    } __attribute__((section(".maps"))) hash_map;
1059   //
1060   // If subroutine foo is traversed first, a type chain
1061   // "ptr->struct m(fwd)" will be created and later on
1062   // when traversing mapdef, since "ptr->struct m" exists,
1063   // the traversal of "struct m" will be omitted.
1064   if (MapDefNotCollected) {
1065     processGlobals(true);
1066     MapDefNotCollected = false;
1067   }
1068 
1069   // Collect all types locally referenced in this function.
1070   // Use RetainedNodes so we can collect all argument names
1071   // even if the argument is not used.
1072   std::unordered_map<uint32_t, StringRef> FuncArgNames;
1073   for (const DINode *DN : SP->getRetainedNodes()) {
1074     if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
1075       // Collect function arguments for subprogram func type.
1076       uint32_t Arg = DV->getArg();
1077       if (Arg) {
1078         visitTypeEntry(DV->getType());
1079         FuncArgNames[Arg] = DV->getName();
1080       }
1081     }
1082   }
1083 
1084   // Construct subprogram func proto type.
1085   uint32_t ProtoTypeId;
1086   visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
1087 
1088   // Construct subprogram func type
1089   uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL;
1090   auto FuncTypeEntry =
1091       std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
1092   uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
1093 
1094   // Process argument annotations.
1095   for (const DINode *DN : SP->getRetainedNodes()) {
1096     if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
1097       uint32_t Arg = DV->getArg();
1098       if (Arg)
1099         processDeclAnnotations(DV->getAnnotations(), FuncTypeId, Arg - 1);
1100     }
1101   }
1102 
1103   processDeclAnnotations(SP->getAnnotations(), FuncTypeId, -1);
1104 
1105   for (const auto &TypeEntry : TypeEntries)
1106     TypeEntry->completeType(*this);
1107 
1108   // Construct funcinfo and the first lineinfo for the function.
1109   MCSymbol *FuncLabel = Asm->getFunctionBegin();
1110   BTFFuncInfo FuncInfo;
1111   FuncInfo.Label = FuncLabel;
1112   FuncInfo.TypeId = FuncTypeId;
1113   if (FuncLabel->isInSection()) {
1114     MCSection &Section = FuncLabel->getSection();
1115     const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
1116     assert(SectionELF && "Null section for Function Label");
1117     SecNameOff = addString(SectionELF->getName());
1118   } else {
1119     SecNameOff = addString(".text");
1120   }
1121   FuncInfoTable[SecNameOff].push_back(FuncInfo);
1122 }
1123 
1124 void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
1125   SkipInstruction = false;
1126   LineInfoGenerated = false;
1127   SecNameOff = 0;
1128 }
1129 
1130 /// On-demand populate types as requested from abstract member
1131 /// accessing or preserve debuginfo type.
1132 unsigned BTFDebug::populateType(const DIType *Ty) {
1133   unsigned Id;
1134   visitTypeEntry(Ty, Id, false, false);
1135   for (const auto &TypeEntry : TypeEntries)
1136     TypeEntry->completeType(*this);
1137   return Id;
1138 }
1139 
1140 /// Generate a struct member field relocation.
1141 void BTFDebug::generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
1142                                      const GlobalVariable *GVar, bool IsAma) {
1143   BTFFieldReloc FieldReloc;
1144   FieldReloc.Label = ORSym;
1145   FieldReloc.TypeID = RootId;
1146 
1147   StringRef AccessPattern = GVar->getName();
1148   size_t FirstDollar = AccessPattern.find_first_of('$');
1149   if (IsAma) {
1150     size_t FirstColon = AccessPattern.find_first_of(':');
1151     size_t SecondColon = AccessPattern.find_first_of(':', FirstColon + 1);
1152     StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1);
1153     StringRef RelocKindStr = AccessPattern.substr(FirstColon + 1,
1154         SecondColon - FirstColon);
1155     StringRef PatchImmStr = AccessPattern.substr(SecondColon + 1,
1156         FirstDollar - SecondColon);
1157 
1158     FieldReloc.OffsetNameOff = addString(IndexPattern);
1159     FieldReloc.RelocKind = std::stoull(std::string(RelocKindStr));
1160     PatchImms[GVar] = std::make_pair(std::stoll(std::string(PatchImmStr)),
1161                                      FieldReloc.RelocKind);
1162   } else {
1163     StringRef RelocStr = AccessPattern.substr(FirstDollar + 1);
1164     FieldReloc.OffsetNameOff = addString("0");
1165     FieldReloc.RelocKind = std::stoull(std::string(RelocStr));
1166     PatchImms[GVar] = std::make_pair(RootId, FieldReloc.RelocKind);
1167   }
1168   FieldRelocTable[SecNameOff].push_back(FieldReloc);
1169 }
1170 
1171 void BTFDebug::processGlobalValue(const MachineOperand &MO) {
1172   // check whether this is a candidate or not
1173   if (MO.isGlobal()) {
1174     const GlobalValue *GVal = MO.getGlobal();
1175     auto *GVar = dyn_cast<GlobalVariable>(GVal);
1176     if (!GVar) {
1177       // Not a global variable. Maybe an extern function reference.
1178       processFuncPrototypes(dyn_cast<Function>(GVal));
1179       return;
1180     }
1181 
1182     if (!GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr) &&
1183         !GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
1184       return;
1185 
1186     MCSymbol *ORSym = OS.getContext().createTempSymbol();
1187     OS.emitLabel(ORSym);
1188 
1189     MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1190     uint32_t RootId = populateType(dyn_cast<DIType>(MDN));
1191     generatePatchImmReloc(ORSym, RootId, GVar,
1192                           GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr));
1193   }
1194 }
1195 
1196 void BTFDebug::beginInstruction(const MachineInstr *MI) {
1197   DebugHandlerBase::beginInstruction(MI);
1198 
1199   if (SkipInstruction || MI->isMetaInstruction() ||
1200       MI->getFlag(MachineInstr::FrameSetup))
1201     return;
1202 
1203   if (MI->isInlineAsm()) {
1204     // Count the number of register definitions to find the asm string.
1205     unsigned NumDefs = 0;
1206     for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
1207          ++NumDefs)
1208       ;
1209 
1210     // Skip this inline asm instruction if the asmstr is empty.
1211     const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1212     if (AsmStr[0] == 0)
1213       return;
1214   }
1215 
1216   if (MI->getOpcode() == BPF::LD_imm64) {
1217     // If the insn is "r2 = LD_imm64 @<an AmaAttr global>",
1218     // add this insn into the .BTF.ext FieldReloc subsection.
1219     // Relocation looks like:
1220     //  . SecName:
1221     //    . InstOffset
1222     //    . TypeID
1223     //    . OffSetNameOff
1224     //    . RelocType
1225     // Later, the insn is replaced with "r2 = <offset>"
1226     // where "<offset>" equals to the offset based on current
1227     // type definitions.
1228     //
1229     // If the insn is "r2 = LD_imm64 @<an TypeIdAttr global>",
1230     // The LD_imm64 result will be replaced with a btf type id.
1231     processGlobalValue(MI->getOperand(1));
1232   } else if (MI->getOpcode() == BPF::CORE_MEM ||
1233              MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1234              MI->getOpcode() == BPF::CORE_SHIFT) {
1235     // relocation insn is a load, store or shift insn.
1236     processGlobalValue(MI->getOperand(3));
1237   } else if (MI->getOpcode() == BPF::JAL) {
1238     // check extern function references
1239     const MachineOperand &MO = MI->getOperand(0);
1240     if (MO.isGlobal()) {
1241       processFuncPrototypes(dyn_cast<Function>(MO.getGlobal()));
1242     }
1243   }
1244 
1245   if (!CurMI) // no debug info
1246     return;
1247 
1248   // Skip this instruction if no DebugLoc or the DebugLoc
1249   // is the same as the previous instruction.
1250   const DebugLoc &DL = MI->getDebugLoc();
1251   if (!DL || PrevInstLoc == DL) {
1252     // This instruction will be skipped, no LineInfo has
1253     // been generated, construct one based on function signature.
1254     if (LineInfoGenerated == false) {
1255       auto *S = MI->getMF()->getFunction().getSubprogram();
1256       MCSymbol *FuncLabel = Asm->getFunctionBegin();
1257       constructLineInfo(S, FuncLabel, S->getLine(), 0);
1258       LineInfoGenerated = true;
1259     }
1260 
1261     return;
1262   }
1263 
1264   // Create a temporary label to remember the insn for lineinfo.
1265   MCSymbol *LineSym = OS.getContext().createTempSymbol();
1266   OS.emitLabel(LineSym);
1267 
1268   // Construct the lineinfo.
1269   auto SP = DL.get()->getScope()->getSubprogram();
1270   constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
1271 
1272   LineInfoGenerated = true;
1273   PrevInstLoc = DL;
1274 }
1275 
1276 void BTFDebug::processGlobals(bool ProcessingMapDef) {
1277   // Collect all types referenced by globals.
1278   const Module *M = MMI->getModule();
1279   for (const GlobalVariable &Global : M->globals()) {
1280     // Decide the section name.
1281     StringRef SecName;
1282     if (Global.hasSection()) {
1283       SecName = Global.getSection();
1284     } else if (Global.hasInitializer()) {
1285       // data, bss, or readonly sections
1286       if (Global.isConstant())
1287         SecName = ".rodata";
1288       else
1289         SecName = Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
1290     }
1291 
1292     if (ProcessingMapDef != SecName.startswith(".maps"))
1293       continue;
1294 
1295     // Create a .rodata datasec if the global variable is an initialized
1296     // constant with private linkage and if it won't be in .rodata.str<#>
1297     // and .rodata.cst<#> sections.
1298     if (SecName == ".rodata" && Global.hasPrivateLinkage() &&
1299         DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1300       SectionKind GVKind =
1301           TargetLoweringObjectFile::getKindForGlobal(&Global, Asm->TM);
1302       // skip .rodata.str<#> and .rodata.cst<#> sections
1303       if (!GVKind.isMergeableCString() && !GVKind.isMergeableConst()) {
1304         DataSecEntries[std::string(SecName)] =
1305             std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1306       }
1307     }
1308 
1309     SmallVector<DIGlobalVariableExpression *, 1> GVs;
1310     Global.getDebugInfo(GVs);
1311 
1312     // No type information, mostly internal, skip it.
1313     if (GVs.size() == 0)
1314       continue;
1315 
1316     uint32_t GVTypeId = 0;
1317     DIGlobalVariable *DIGlobal = nullptr;
1318     for (auto *GVE : GVs) {
1319       DIGlobal = GVE->getVariable();
1320       if (SecName.startswith(".maps"))
1321         visitMapDefType(DIGlobal->getType(), GVTypeId);
1322       else
1323         visitTypeEntry(DIGlobal->getType(), GVTypeId, false, false);
1324       break;
1325     }
1326 
1327     // Only support the following globals:
1328     //  . static variables
1329     //  . non-static weak or non-weak global variables
1330     //  . weak or non-weak extern global variables
1331     // Whether DataSec is readonly or not can be found from corresponding ELF
1332     // section flags. Whether a BTF_KIND_VAR is a weak symbol or not
1333     // can be found from the corresponding ELF symbol table.
1334     auto Linkage = Global.getLinkage();
1335     if (Linkage != GlobalValue::InternalLinkage &&
1336         Linkage != GlobalValue::ExternalLinkage &&
1337         Linkage != GlobalValue::WeakAnyLinkage &&
1338         Linkage != GlobalValue::WeakODRLinkage &&
1339         Linkage != GlobalValue::ExternalWeakLinkage)
1340       continue;
1341 
1342     uint32_t GVarInfo;
1343     if (Linkage == GlobalValue::InternalLinkage) {
1344       GVarInfo = BTF::VAR_STATIC;
1345     } else if (Global.hasInitializer()) {
1346       GVarInfo = BTF::VAR_GLOBAL_ALLOCATED;
1347     } else {
1348       GVarInfo = BTF::VAR_GLOBAL_EXTERNAL;
1349     }
1350 
1351     auto VarEntry =
1352         std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1353     uint32_t VarId = addType(std::move(VarEntry));
1354 
1355     processDeclAnnotations(DIGlobal->getAnnotations(), VarId, -1);
1356 
1357     // An empty SecName means an extern variable without section attribute.
1358     if (SecName.empty())
1359       continue;
1360 
1361     // Find or create a DataSec
1362     if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1363       DataSecEntries[std::string(SecName)] =
1364           std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1365     }
1366 
1367     // Calculate symbol size
1368     const DataLayout &DL = Global.getParent()->getDataLayout();
1369     uint32_t Size = DL.getTypeAllocSize(Global.getValueType());
1370 
1371     DataSecEntries[std::string(SecName)]->addDataSecEntry(VarId,
1372         Asm->getSymbol(&Global), Size);
1373   }
1374 }
1375 
1376 /// Emit proper patchable instructions.
1377 bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1378   if (MI->getOpcode() == BPF::LD_imm64) {
1379     const MachineOperand &MO = MI->getOperand(1);
1380     if (MO.isGlobal()) {
1381       const GlobalValue *GVal = MO.getGlobal();
1382       auto *GVar = dyn_cast<GlobalVariable>(GVal);
1383       if (GVar) {
1384         // Emit "mov ri, <imm>"
1385         int64_t Imm;
1386         uint32_t Reloc;
1387         if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr) ||
1388             GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr)) {
1389           Imm = PatchImms[GVar].first;
1390           Reloc = PatchImms[GVar].second;
1391         } else {
1392           return false;
1393         }
1394 
1395         if (Reloc == BPFCoreSharedInfo::ENUM_VALUE_EXISTENCE ||
1396             Reloc == BPFCoreSharedInfo::ENUM_VALUE ||
1397             Reloc == BPFCoreSharedInfo::BTF_TYPE_ID_LOCAL ||
1398             Reloc == BPFCoreSharedInfo::BTF_TYPE_ID_REMOTE)
1399           OutMI.setOpcode(BPF::LD_imm64);
1400         else
1401           OutMI.setOpcode(BPF::MOV_ri);
1402         OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1403         OutMI.addOperand(MCOperand::createImm(Imm));
1404         return true;
1405       }
1406     }
1407   } else if (MI->getOpcode() == BPF::CORE_MEM ||
1408              MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1409              MI->getOpcode() == BPF::CORE_SHIFT) {
1410     const MachineOperand &MO = MI->getOperand(3);
1411     if (MO.isGlobal()) {
1412       const GlobalValue *GVal = MO.getGlobal();
1413       auto *GVar = dyn_cast<GlobalVariable>(GVal);
1414       if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1415         uint32_t Imm = PatchImms[GVar].first;
1416         OutMI.setOpcode(MI->getOperand(1).getImm());
1417         if (MI->getOperand(0).isImm())
1418           OutMI.addOperand(MCOperand::createImm(MI->getOperand(0).getImm()));
1419         else
1420           OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1421         OutMI.addOperand(MCOperand::createReg(MI->getOperand(2).getReg()));
1422         OutMI.addOperand(MCOperand::createImm(Imm));
1423         return true;
1424       }
1425     }
1426   }
1427   return false;
1428 }
1429 
1430 void BTFDebug::processFuncPrototypes(const Function *F) {
1431   if (!F)
1432     return;
1433 
1434   const DISubprogram *SP = F->getSubprogram();
1435   if (!SP || SP->isDefinition())
1436     return;
1437 
1438   // Do not emit again if already emitted.
1439   if (ProtoFunctions.find(F) != ProtoFunctions.end())
1440     return;
1441   ProtoFunctions.insert(F);
1442 
1443   uint32_t ProtoTypeId;
1444   const std::unordered_map<uint32_t, StringRef> FuncArgNames;
1445   visitSubroutineType(SP->getType(), false, FuncArgNames, ProtoTypeId);
1446 
1447   uint8_t Scope = BTF::FUNC_EXTERN;
1448   auto FuncTypeEntry =
1449       std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
1450   uint32_t FuncId = addType(std::move(FuncTypeEntry));
1451 
1452   processDeclAnnotations(SP->getAnnotations(), FuncId, -1);
1453 
1454   if (F->hasSection()) {
1455     StringRef SecName = F->getSection();
1456 
1457     if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1458       DataSecEntries[std::string(SecName)] =
1459           std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1460     }
1461 
1462     // We really don't know func size, set it to 0.
1463     DataSecEntries[std::string(SecName)]->addDataSecEntry(FuncId,
1464         Asm->getSymbol(F), 0);
1465   }
1466 }
1467 
1468 void BTFDebug::endModule() {
1469   // Collect MapDef globals if not collected yet.
1470   if (MapDefNotCollected) {
1471     processGlobals(true);
1472     MapDefNotCollected = false;
1473   }
1474 
1475   // Collect global types/variables except MapDef globals.
1476   processGlobals(false);
1477 
1478   for (auto &DataSec : DataSecEntries)
1479     addType(std::move(DataSec.second));
1480 
1481   // Fixups
1482   for (auto &Fixup : FixupDerivedTypes) {
1483     StringRef TypeName = Fixup.first;
1484     bool IsUnion = Fixup.second.first;
1485 
1486     // Search through struct types
1487     uint32_t StructTypeId = 0;
1488     for (const auto &StructType : StructTypes) {
1489       if (StructType->getName() == TypeName) {
1490         StructTypeId = StructType->getId();
1491         break;
1492       }
1493     }
1494 
1495     if (StructTypeId == 0) {
1496       auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
1497       StructTypeId = addType(std::move(FwdTypeEntry));
1498     }
1499 
1500     for (auto &DType : Fixup.second.second) {
1501       DType->setPointeeType(StructTypeId);
1502     }
1503   }
1504 
1505   // Complete BTF type cross refereences.
1506   for (const auto &TypeEntry : TypeEntries)
1507     TypeEntry->completeType(*this);
1508 
1509   // Emit BTF sections.
1510   emitBTFSection();
1511   emitBTFExtSection();
1512 }
1513