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