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 uint32_t BTFStringTable::addString(StringRef S) {
390   // Check whether the string already exists.
391   for (auto &OffsetM : OffsetToIdMap) {
392     if (Table[OffsetM.second] == S)
393       return OffsetM.first;
394   }
395   // Not find, add to the string table.
396   uint32_t Offset = Size;
397   OffsetToIdMap[Offset] = Table.size();
398   Table.push_back(std::string(S));
399   Size += S.size() + 1;
400   return Offset;
401 }
402 
403 BTFDebug::BTFDebug(AsmPrinter *AP)
404     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
405       LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
406       MapDefNotCollected(true) {
407   addString("\0");
408 }
409 
410 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
411                            const DIType *Ty) {
412   TypeEntry->setId(TypeEntries.size() + 1);
413   uint32_t Id = TypeEntry->getId();
414   DIToIdMap[Ty] = Id;
415   TypeEntries.push_back(std::move(TypeEntry));
416   return Id;
417 }
418 
419 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
420   TypeEntry->setId(TypeEntries.size() + 1);
421   uint32_t Id = TypeEntry->getId();
422   TypeEntries.push_back(std::move(TypeEntry));
423   return Id;
424 }
425 
426 void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
427   // Only int and binary floating point types are supported in BTF.
428   uint32_t Encoding = BTy->getEncoding();
429   std::unique_ptr<BTFTypeBase> TypeEntry;
430   switch (Encoding) {
431   case dwarf::DW_ATE_boolean:
432   case dwarf::DW_ATE_signed:
433   case dwarf::DW_ATE_signed_char:
434   case dwarf::DW_ATE_unsigned:
435   case dwarf::DW_ATE_unsigned_char:
436     // Create a BTF type instance for this DIBasicType and put it into
437     // DIToIdMap for cross-type reference check.
438     TypeEntry = std::make_unique<BTFTypeInt>(
439         Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
440     break;
441   case dwarf::DW_ATE_float:
442     TypeEntry =
443         std::make_unique<BTFTypeFloat>(BTy->getSizeInBits(), BTy->getName());
444     break;
445   default:
446     return;
447   }
448 
449   TypeId = addType(std::move(TypeEntry), BTy);
450 }
451 
452 /// Handle subprogram or subroutine types.
453 void BTFDebug::visitSubroutineType(
454     const DISubroutineType *STy, bool ForSubprog,
455     const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
456     uint32_t &TypeId) {
457   DITypeRefArray Elements = STy->getTypeArray();
458   uint32_t VLen = Elements.size() - 1;
459   if (VLen > BTF::MAX_VLEN)
460     return;
461 
462   // Subprogram has a valid non-zero-length name, and the pointee of
463   // a function pointer has an empty name. The subprogram type will
464   // not be added to DIToIdMap as it should not be referenced by
465   // any other types.
466   auto TypeEntry = std::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
467   if (ForSubprog)
468     TypeId = addType(std::move(TypeEntry)); // For subprogram
469   else
470     TypeId = addType(std::move(TypeEntry), STy); // For func ptr
471 
472   // Visit return type and func arg types.
473   for (const auto Element : Elements) {
474     visitTypeEntry(Element);
475   }
476 }
477 
478 /// Handle structure/union types.
479 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
480                                uint32_t &TypeId) {
481   const DINodeArray Elements = CTy->getElements();
482   uint32_t VLen = Elements.size();
483   if (VLen > BTF::MAX_VLEN)
484     return;
485 
486   // Check whether we have any bitfield members or not
487   bool HasBitField = false;
488   for (const auto *Element : Elements) {
489     auto E = cast<DIDerivedType>(Element);
490     if (E->isBitField()) {
491       HasBitField = true;
492       break;
493     }
494   }
495 
496   auto TypeEntry =
497       std::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
498   StructTypes.push_back(TypeEntry.get());
499   TypeId = addType(std::move(TypeEntry), CTy);
500 
501   // Visit all struct members.
502   for (const auto *Element : Elements)
503     visitTypeEntry(cast<DIDerivedType>(Element));
504 }
505 
506 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
507   // Visit array element type.
508   uint32_t ElemTypeId;
509   const DIType *ElemType = CTy->getBaseType();
510   visitTypeEntry(ElemType, ElemTypeId, false, false);
511 
512   // Visit array dimensions.
513   DINodeArray Elements = CTy->getElements();
514   for (int I = Elements.size() - 1; I >= 0; --I) {
515     if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
516       if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
517         const DISubrange *SR = cast<DISubrange>(Element);
518         auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
519         int64_t Count = CI->getSExtValue();
520 
521         // For struct s { int b; char c[]; }, the c[] will be represented
522         // as an array with Count = -1.
523         auto TypeEntry =
524             std::make_unique<BTFTypeArray>(ElemTypeId,
525                 Count >= 0 ? Count : 0);
526         if (I == 0)
527           ElemTypeId = addType(std::move(TypeEntry), CTy);
528         else
529           ElemTypeId = addType(std::move(TypeEntry));
530       }
531   }
532 
533   // The array TypeId is the type id of the outermost dimension.
534   TypeId = ElemTypeId;
535 
536   // The IR does not have a type for array index while BTF wants one.
537   // So create an array index type if there is none.
538   if (!ArrayIndexTypeId) {
539     auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
540                                                    0, "__ARRAY_SIZE_TYPE__");
541     ArrayIndexTypeId = addType(std::move(TypeEntry));
542   }
543 }
544 
545 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
546   DINodeArray Elements = CTy->getElements();
547   uint32_t VLen = Elements.size();
548   if (VLen > BTF::MAX_VLEN)
549     return;
550 
551   auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen);
552   TypeId = addType(std::move(TypeEntry), CTy);
553   // No need to visit base type as BTF does not encode it.
554 }
555 
556 /// Handle structure/union forward declarations.
557 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
558                                 uint32_t &TypeId) {
559   auto TypeEntry = std::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
560   TypeId = addType(std::move(TypeEntry), CTy);
561 }
562 
563 /// Handle structure, union, array and enumeration types.
564 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
565                                   uint32_t &TypeId) {
566   auto Tag = CTy->getTag();
567   if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
568     // Handle forward declaration differently as it does not have members.
569     if (CTy->isForwardDecl())
570       visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
571     else
572       visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
573   } else if (Tag == dwarf::DW_TAG_array_type)
574     visitArrayType(CTy, TypeId);
575   else if (Tag == dwarf::DW_TAG_enumeration_type)
576     visitEnumType(CTy, TypeId);
577 }
578 
579 /// Handle pointer, typedef, const, volatile, restrict and member types.
580 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
581                                 bool CheckPointer, bool SeenPointer) {
582   unsigned Tag = DTy->getTag();
583 
584   /// Try to avoid chasing pointees, esp. structure pointees which may
585   /// unnecessary bring in a lot of types.
586   if (CheckPointer && !SeenPointer) {
587     SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
588   }
589 
590   if (CheckPointer && SeenPointer) {
591     const DIType *Base = DTy->getBaseType();
592     if (Base) {
593       if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
594         auto CTag = CTy->getTag();
595         if ((CTag == dwarf::DW_TAG_structure_type ||
596              CTag == dwarf::DW_TAG_union_type) &&
597             !CTy->getName().empty() && !CTy->isForwardDecl()) {
598           /// Find a candidate, generate a fixup. Later on the struct/union
599           /// pointee type will be replaced with either a real type or
600           /// a forward declaration.
601           auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true);
602           auto &Fixup = FixupDerivedTypes[CTy->getName()];
603           Fixup.first = CTag == dwarf::DW_TAG_union_type;
604           Fixup.second.push_back(TypeEntry.get());
605           TypeId = addType(std::move(TypeEntry), DTy);
606           return;
607         }
608       }
609     }
610   }
611 
612   if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef ||
613       Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
614       Tag == dwarf::DW_TAG_restrict_type) {
615     auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
616     TypeId = addType(std::move(TypeEntry), DTy);
617   } else if (Tag != dwarf::DW_TAG_member) {
618     return;
619   }
620 
621   // Visit base type of pointer, typedef, const, volatile, restrict or
622   // struct/union member.
623   uint32_t TempTypeId = 0;
624   if (Tag == dwarf::DW_TAG_member)
625     visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
626   else
627     visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
628 }
629 
630 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
631                               bool CheckPointer, bool SeenPointer) {
632   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
633     TypeId = DIToIdMap[Ty];
634 
635     // To handle the case like the following:
636     //    struct t;
637     //    typedef struct t _t;
638     //    struct s1 { _t *c; };
639     //    int test1(struct s1 *arg) { ... }
640     //
641     //    struct t { int a; int b; };
642     //    struct s2 { _t c; }
643     //    int test2(struct s2 *arg) { ... }
644     //
645     // During traversing test1() argument, "_t" is recorded
646     // in DIToIdMap and a forward declaration fixup is created
647     // for "struct t" to avoid pointee type traversal.
648     //
649     // During traversing test2() argument, even if we see "_t" is
650     // already defined, we should keep moving to eventually
651     // bring in types for "struct t". Otherwise, the "struct s2"
652     // definition won't be correct.
653     if (Ty && (!CheckPointer || !SeenPointer)) {
654       if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
655         unsigned Tag = DTy->getTag();
656         if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
657             Tag == dwarf::DW_TAG_volatile_type ||
658             Tag == dwarf::DW_TAG_restrict_type) {
659           uint32_t TmpTypeId;
660           visitTypeEntry(DTy->getBaseType(), TmpTypeId, CheckPointer,
661                          SeenPointer);
662         }
663       }
664     }
665 
666     return;
667   }
668 
669   if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
670     visitBasicType(BTy, TypeId);
671   else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
672     visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
673                         TypeId);
674   else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
675     visitCompositeType(CTy, TypeId);
676   else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
677     visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
678   else
679     llvm_unreachable("Unknown DIType");
680 }
681 
682 void BTFDebug::visitTypeEntry(const DIType *Ty) {
683   uint32_t TypeId;
684   visitTypeEntry(Ty, TypeId, false, false);
685 }
686 
687 void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
688   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
689     TypeId = DIToIdMap[Ty];
690     return;
691   }
692 
693   // MapDef type may be a struct type or a non-pointer derived type
694   const DIType *OrigTy = Ty;
695   while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
696     auto Tag = DTy->getTag();
697     if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
698         Tag != dwarf::DW_TAG_volatile_type &&
699         Tag != dwarf::DW_TAG_restrict_type)
700       break;
701     Ty = DTy->getBaseType();
702   }
703 
704   const auto *CTy = dyn_cast<DICompositeType>(Ty);
705   if (!CTy)
706     return;
707 
708   auto Tag = CTy->getTag();
709   if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
710     return;
711 
712   // Visit all struct members to ensure pointee type is visited
713   const DINodeArray Elements = CTy->getElements();
714   for (const auto *Element : Elements) {
715     const auto *MemberType = cast<DIDerivedType>(Element);
716     visitTypeEntry(MemberType->getBaseType());
717   }
718 
719   // Visit this type, struct or a const/typedef/volatile/restrict type
720   visitTypeEntry(OrigTy, TypeId, false, false);
721 }
722 
723 /// Read file contents from the actual file or from the source
724 std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
725   auto File = SP->getFile();
726   std::string FileName;
727 
728   if (!File->getFilename().startswith("/") && File->getDirectory().size())
729     FileName = File->getDirectory().str() + "/" + File->getFilename().str();
730   else
731     FileName = std::string(File->getFilename());
732 
733   // No need to populate the contends if it has been populated!
734   if (FileContent.find(FileName) != FileContent.end())
735     return FileName;
736 
737   std::vector<std::string> Content;
738   std::string Line;
739   Content.push_back(Line); // Line 0 for empty string
740 
741   std::unique_ptr<MemoryBuffer> Buf;
742   auto Source = File->getSource();
743   if (Source)
744     Buf = MemoryBuffer::getMemBufferCopy(*Source);
745   else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
746                MemoryBuffer::getFile(FileName))
747     Buf = std::move(*BufOrErr);
748   if (Buf)
749     for (line_iterator I(*Buf, false), E; I != E; ++I)
750       Content.push_back(std::string(*I));
751 
752   FileContent[FileName] = Content;
753   return FileName;
754 }
755 
756 void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
757                                  uint32_t Line, uint32_t Column) {
758   std::string FileName = populateFileContent(SP);
759   BTFLineInfo LineInfo;
760 
761   LineInfo.Label = Label;
762   LineInfo.FileNameOff = addString(FileName);
763   // If file content is not available, let LineOff = 0.
764   if (Line < FileContent[FileName].size())
765     LineInfo.LineOff = addString(FileContent[FileName][Line]);
766   else
767     LineInfo.LineOff = 0;
768   LineInfo.LineNum = Line;
769   LineInfo.ColumnNum = Column;
770   LineInfoTable[SecNameOff].push_back(LineInfo);
771 }
772 
773 void BTFDebug::emitCommonHeader() {
774   OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
775   OS.emitIntValue(BTF::MAGIC, 2);
776   OS.emitInt8(BTF::VERSION);
777   OS.emitInt8(0);
778 }
779 
780 void BTFDebug::emitBTFSection() {
781   // Do not emit section if no types and only "" string.
782   if (!TypeEntries.size() && StringTable.getSize() == 1)
783     return;
784 
785   MCContext &Ctx = OS.getContext();
786   OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0));
787 
788   // Emit header.
789   emitCommonHeader();
790   OS.emitInt32(BTF::HeaderSize);
791 
792   uint32_t TypeLen = 0, StrLen;
793   for (const auto &TypeEntry : TypeEntries)
794     TypeLen += TypeEntry->getSize();
795   StrLen = StringTable.getSize();
796 
797   OS.emitInt32(0);
798   OS.emitInt32(TypeLen);
799   OS.emitInt32(TypeLen);
800   OS.emitInt32(StrLen);
801 
802   // Emit type table.
803   for (const auto &TypeEntry : TypeEntries)
804     TypeEntry->emitType(OS);
805 
806   // Emit string table.
807   uint32_t StringOffset = 0;
808   for (const auto &S : StringTable.getTable()) {
809     OS.AddComment("string offset=" + std::to_string(StringOffset));
810     OS.emitBytes(S);
811     OS.emitBytes(StringRef("\0", 1));
812     StringOffset += S.size() + 1;
813   }
814 }
815 
816 void BTFDebug::emitBTFExtSection() {
817   // Do not emit section if empty FuncInfoTable and LineInfoTable
818   // and FieldRelocTable.
819   if (!FuncInfoTable.size() && !LineInfoTable.size() &&
820       !FieldRelocTable.size())
821     return;
822 
823   MCContext &Ctx = OS.getContext();
824   OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0));
825 
826   // Emit header.
827   emitCommonHeader();
828   OS.emitInt32(BTF::ExtHeaderSize);
829 
830   // Account for FuncInfo/LineInfo record size as well.
831   uint32_t FuncLen = 4, LineLen = 4;
832   // Do not account for optional FieldReloc.
833   uint32_t FieldRelocLen = 0;
834   for (const auto &FuncSec : FuncInfoTable) {
835     FuncLen += BTF::SecFuncInfoSize;
836     FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
837   }
838   for (const auto &LineSec : LineInfoTable) {
839     LineLen += BTF::SecLineInfoSize;
840     LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
841   }
842   for (const auto &FieldRelocSec : FieldRelocTable) {
843     FieldRelocLen += BTF::SecFieldRelocSize;
844     FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize;
845   }
846 
847   if (FieldRelocLen)
848     FieldRelocLen += 4;
849 
850   OS.emitInt32(0);
851   OS.emitInt32(FuncLen);
852   OS.emitInt32(FuncLen);
853   OS.emitInt32(LineLen);
854   OS.emitInt32(FuncLen + LineLen);
855   OS.emitInt32(FieldRelocLen);
856 
857   // Emit func_info table.
858   OS.AddComment("FuncInfo");
859   OS.emitInt32(BTF::BPFFuncInfoSize);
860   for (const auto &FuncSec : FuncInfoTable) {
861     OS.AddComment("FuncInfo section string offset=" +
862                   std::to_string(FuncSec.first));
863     OS.emitInt32(FuncSec.first);
864     OS.emitInt32(FuncSec.second.size());
865     for (const auto &FuncInfo : FuncSec.second) {
866       Asm->emitLabelReference(FuncInfo.Label, 4);
867       OS.emitInt32(FuncInfo.TypeId);
868     }
869   }
870 
871   // Emit line_info table.
872   OS.AddComment("LineInfo");
873   OS.emitInt32(BTF::BPFLineInfoSize);
874   for (const auto &LineSec : LineInfoTable) {
875     OS.AddComment("LineInfo section string offset=" +
876                   std::to_string(LineSec.first));
877     OS.emitInt32(LineSec.first);
878     OS.emitInt32(LineSec.second.size());
879     for (const auto &LineInfo : LineSec.second) {
880       Asm->emitLabelReference(LineInfo.Label, 4);
881       OS.emitInt32(LineInfo.FileNameOff);
882       OS.emitInt32(LineInfo.LineOff);
883       OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
884                     std::to_string(LineInfo.ColumnNum));
885       OS.emitInt32(LineInfo.LineNum << 10 | LineInfo.ColumnNum);
886     }
887   }
888 
889   // Emit field reloc table.
890   if (FieldRelocLen) {
891     OS.AddComment("FieldReloc");
892     OS.emitInt32(BTF::BPFFieldRelocSize);
893     for (const auto &FieldRelocSec : FieldRelocTable) {
894       OS.AddComment("Field reloc section string offset=" +
895                     std::to_string(FieldRelocSec.first));
896       OS.emitInt32(FieldRelocSec.first);
897       OS.emitInt32(FieldRelocSec.second.size());
898       for (const auto &FieldRelocInfo : FieldRelocSec.second) {
899         Asm->emitLabelReference(FieldRelocInfo.Label, 4);
900         OS.emitInt32(FieldRelocInfo.TypeID);
901         OS.emitInt32(FieldRelocInfo.OffsetNameOff);
902         OS.emitInt32(FieldRelocInfo.RelocKind);
903       }
904     }
905   }
906 }
907 
908 void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
909   auto *SP = MF->getFunction().getSubprogram();
910   auto *Unit = SP->getUnit();
911 
912   if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
913     SkipInstruction = true;
914     return;
915   }
916   SkipInstruction = false;
917 
918   // Collect MapDef types. Map definition needs to collect
919   // pointee types. Do it first. Otherwise, for the following
920   // case:
921   //    struct m { ...};
922   //    struct t {
923   //      struct m *key;
924   //    };
925   //    foo(struct t *arg);
926   //
927   //    struct mapdef {
928   //      ...
929   //      struct m *key;
930   //      ...
931   //    } __attribute__((section(".maps"))) hash_map;
932   //
933   // If subroutine foo is traversed first, a type chain
934   // "ptr->struct m(fwd)" will be created and later on
935   // when traversing mapdef, since "ptr->struct m" exists,
936   // the traversal of "struct m" will be omitted.
937   if (MapDefNotCollected) {
938     processGlobals(true);
939     MapDefNotCollected = false;
940   }
941 
942   // Collect all types locally referenced in this function.
943   // Use RetainedNodes so we can collect all argument names
944   // even if the argument is not used.
945   std::unordered_map<uint32_t, StringRef> FuncArgNames;
946   for (const DINode *DN : SP->getRetainedNodes()) {
947     if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
948       // Collect function arguments for subprogram func type.
949       uint32_t Arg = DV->getArg();
950       if (Arg) {
951         visitTypeEntry(DV->getType());
952         FuncArgNames[Arg] = DV->getName();
953       }
954     }
955   }
956 
957   // Construct subprogram func proto type.
958   uint32_t ProtoTypeId;
959   visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
960 
961   // Construct subprogram func type
962   uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL;
963   auto FuncTypeEntry =
964       std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
965   uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
966 
967   for (const auto &TypeEntry : TypeEntries)
968     TypeEntry->completeType(*this);
969 
970   // Construct funcinfo and the first lineinfo for the function.
971   MCSymbol *FuncLabel = Asm->getFunctionBegin();
972   BTFFuncInfo FuncInfo;
973   FuncInfo.Label = FuncLabel;
974   FuncInfo.TypeId = FuncTypeId;
975   if (FuncLabel->isInSection()) {
976     MCSection &Section = FuncLabel->getSection();
977     const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
978     assert(SectionELF && "Null section for Function Label");
979     SecNameOff = addString(SectionELF->getName());
980   } else {
981     SecNameOff = addString(".text");
982   }
983   FuncInfoTable[SecNameOff].push_back(FuncInfo);
984 }
985 
986 void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
987   SkipInstruction = false;
988   LineInfoGenerated = false;
989   SecNameOff = 0;
990 }
991 
992 /// On-demand populate types as requested from abstract member
993 /// accessing or preserve debuginfo type.
994 unsigned BTFDebug::populateType(const DIType *Ty) {
995   unsigned Id;
996   visitTypeEntry(Ty, Id, false, false);
997   for (const auto &TypeEntry : TypeEntries)
998     TypeEntry->completeType(*this);
999   return Id;
1000 }
1001 
1002 /// Generate a struct member field relocation.
1003 void BTFDebug::generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
1004                                      const GlobalVariable *GVar, bool IsAma) {
1005   BTFFieldReloc FieldReloc;
1006   FieldReloc.Label = ORSym;
1007   FieldReloc.TypeID = RootId;
1008 
1009   StringRef AccessPattern = GVar->getName();
1010   size_t FirstDollar = AccessPattern.find_first_of('$');
1011   if (IsAma) {
1012     size_t FirstColon = AccessPattern.find_first_of(':');
1013     size_t SecondColon = AccessPattern.find_first_of(':', FirstColon + 1);
1014     StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1);
1015     StringRef RelocKindStr = AccessPattern.substr(FirstColon + 1,
1016         SecondColon - FirstColon);
1017     StringRef PatchImmStr = AccessPattern.substr(SecondColon + 1,
1018         FirstDollar - SecondColon);
1019 
1020     FieldReloc.OffsetNameOff = addString(IndexPattern);
1021     FieldReloc.RelocKind = std::stoull(std::string(RelocKindStr));
1022     PatchImms[GVar] = std::make_pair(std::stoll(std::string(PatchImmStr)),
1023                                      FieldReloc.RelocKind);
1024   } else {
1025     StringRef RelocStr = AccessPattern.substr(FirstDollar + 1);
1026     FieldReloc.OffsetNameOff = addString("0");
1027     FieldReloc.RelocKind = std::stoull(std::string(RelocStr));
1028     PatchImms[GVar] = std::make_pair(RootId, FieldReloc.RelocKind);
1029   }
1030   FieldRelocTable[SecNameOff].push_back(FieldReloc);
1031 }
1032 
1033 void BTFDebug::processReloc(const MachineOperand &MO) {
1034   // check whether this is a candidate or not
1035   if (MO.isGlobal()) {
1036     const GlobalValue *GVal = MO.getGlobal();
1037     auto *GVar = dyn_cast<GlobalVariable>(GVal);
1038     if (!GVar)
1039       return;
1040 
1041     if (!GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr) &&
1042         !GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
1043       return;
1044 
1045     MCSymbol *ORSym = OS.getContext().createTempSymbol();
1046     OS.emitLabel(ORSym);
1047 
1048     MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1049     uint32_t RootId = populateType(dyn_cast<DIType>(MDN));
1050     generatePatchImmReloc(ORSym, RootId, GVar,
1051                           GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr));
1052   }
1053 }
1054 
1055 void BTFDebug::beginInstruction(const MachineInstr *MI) {
1056   DebugHandlerBase::beginInstruction(MI);
1057 
1058   if (SkipInstruction || MI->isMetaInstruction() ||
1059       MI->getFlag(MachineInstr::FrameSetup))
1060     return;
1061 
1062   if (MI->isInlineAsm()) {
1063     // Count the number of register definitions to find the asm string.
1064     unsigned NumDefs = 0;
1065     for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
1066          ++NumDefs)
1067       ;
1068 
1069     // Skip this inline asm instruction if the asmstr is empty.
1070     const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1071     if (AsmStr[0] == 0)
1072       return;
1073   }
1074 
1075   if (MI->getOpcode() == BPF::LD_imm64) {
1076     // If the insn is "r2 = LD_imm64 @<an AmaAttr global>",
1077     // add this insn into the .BTF.ext FieldReloc subsection.
1078     // Relocation looks like:
1079     //  . SecName:
1080     //    . InstOffset
1081     //    . TypeID
1082     //    . OffSetNameOff
1083     //    . RelocType
1084     // Later, the insn is replaced with "r2 = <offset>"
1085     // where "<offset>" equals to the offset based on current
1086     // type definitions.
1087     //
1088     // If the insn is "r2 = LD_imm64 @<an TypeIdAttr global>",
1089     // The LD_imm64 result will be replaced with a btf type id.
1090     processReloc(MI->getOperand(1));
1091   } else if (MI->getOpcode() == BPF::CORE_MEM ||
1092              MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1093              MI->getOpcode() == BPF::CORE_SHIFT) {
1094     // relocation insn is a load, store or shift insn.
1095     processReloc(MI->getOperand(3));
1096   } else if (MI->getOpcode() == BPF::JAL) {
1097     // check extern function references
1098     const MachineOperand &MO = MI->getOperand(0);
1099     if (MO.isGlobal()) {
1100       processFuncPrototypes(dyn_cast<Function>(MO.getGlobal()));
1101     }
1102   }
1103 
1104   if (!CurMI) // no debug info
1105     return;
1106 
1107   // Skip this instruction if no DebugLoc or the DebugLoc
1108   // is the same as the previous instruction.
1109   const DebugLoc &DL = MI->getDebugLoc();
1110   if (!DL || PrevInstLoc == DL) {
1111     // This instruction will be skipped, no LineInfo has
1112     // been generated, construct one based on function signature.
1113     if (LineInfoGenerated == false) {
1114       auto *S = MI->getMF()->getFunction().getSubprogram();
1115       MCSymbol *FuncLabel = Asm->getFunctionBegin();
1116       constructLineInfo(S, FuncLabel, S->getLine(), 0);
1117       LineInfoGenerated = true;
1118     }
1119 
1120     return;
1121   }
1122 
1123   // Create a temporary label to remember the insn for lineinfo.
1124   MCSymbol *LineSym = OS.getContext().createTempSymbol();
1125   OS.emitLabel(LineSym);
1126 
1127   // Construct the lineinfo.
1128   auto SP = DL.get()->getScope()->getSubprogram();
1129   constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
1130 
1131   LineInfoGenerated = true;
1132   PrevInstLoc = DL;
1133 }
1134 
1135 void BTFDebug::processGlobals(bool ProcessingMapDef) {
1136   // Collect all types referenced by globals.
1137   const Module *M = MMI->getModule();
1138   for (const GlobalVariable &Global : M->globals()) {
1139     // Decide the section name.
1140     StringRef SecName;
1141     if (Global.hasSection()) {
1142       SecName = Global.getSection();
1143     } else if (Global.hasInitializer()) {
1144       // data, bss, or readonly sections
1145       if (Global.isConstant())
1146         SecName = ".rodata";
1147       else
1148         SecName = Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
1149     } else {
1150       // extern variables without explicit section,
1151       // put them into ".extern" section.
1152       SecName = ".extern";
1153     }
1154 
1155     if (ProcessingMapDef != SecName.startswith(".maps"))
1156       continue;
1157 
1158     // Create a .rodata datasec if the global variable is an initialized
1159     // constant with private linkage and if it won't be in .rodata.str<#>
1160     // and .rodata.cst<#> sections.
1161     if (SecName == ".rodata" && Global.hasPrivateLinkage() &&
1162         DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1163       SectionKind GVKind =
1164           TargetLoweringObjectFile::getKindForGlobal(&Global, Asm->TM);
1165       // skip .rodata.str<#> and .rodata.cst<#> sections
1166       if (!GVKind.isMergeableCString() && !GVKind.isMergeableConst()) {
1167         DataSecEntries[std::string(SecName)] =
1168             std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1169       }
1170     }
1171 
1172     SmallVector<DIGlobalVariableExpression *, 1> GVs;
1173     Global.getDebugInfo(GVs);
1174 
1175     // No type information, mostly internal, skip it.
1176     if (GVs.size() == 0)
1177       continue;
1178 
1179     uint32_t GVTypeId = 0;
1180     for (auto *GVE : GVs) {
1181       if (SecName.startswith(".maps"))
1182         visitMapDefType(GVE->getVariable()->getType(), GVTypeId);
1183       else
1184         visitTypeEntry(GVE->getVariable()->getType(), GVTypeId, false, false);
1185       break;
1186     }
1187 
1188     // Only support the following globals:
1189     //  . static variables
1190     //  . non-static weak or non-weak global variables
1191     //  . weak or non-weak extern global variables
1192     // Whether DataSec is readonly or not can be found from corresponding ELF
1193     // section flags. Whether a BTF_KIND_VAR is a weak symbol or not
1194     // can be found from the corresponding ELF symbol table.
1195     auto Linkage = Global.getLinkage();
1196     if (Linkage != GlobalValue::InternalLinkage &&
1197         Linkage != GlobalValue::ExternalLinkage &&
1198         Linkage != GlobalValue::WeakAnyLinkage &&
1199         Linkage != GlobalValue::WeakODRLinkage &&
1200         Linkage != GlobalValue::ExternalWeakLinkage)
1201       continue;
1202 
1203     uint32_t GVarInfo;
1204     if (Linkage == GlobalValue::InternalLinkage) {
1205       GVarInfo = BTF::VAR_STATIC;
1206     } else if (Global.hasInitializer()) {
1207       GVarInfo = BTF::VAR_GLOBAL_ALLOCATED;
1208     } else {
1209       GVarInfo = BTF::VAR_GLOBAL_EXTERNAL;
1210     }
1211 
1212     auto VarEntry =
1213         std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1214     uint32_t VarId = addType(std::move(VarEntry));
1215 
1216     assert(!SecName.empty());
1217 
1218     // Find or create a DataSec
1219     if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1220       DataSecEntries[std::string(SecName)] =
1221           std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1222     }
1223 
1224     // Calculate symbol size
1225     const DataLayout &DL = Global.getParent()->getDataLayout();
1226     uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());
1227 
1228     DataSecEntries[std::string(SecName)]->addDataSecEntry(VarId,
1229         Asm->getSymbol(&Global), Size);
1230   }
1231 }
1232 
1233 /// Emit proper patchable instructions.
1234 bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1235   if (MI->getOpcode() == BPF::LD_imm64) {
1236     const MachineOperand &MO = MI->getOperand(1);
1237     if (MO.isGlobal()) {
1238       const GlobalValue *GVal = MO.getGlobal();
1239       auto *GVar = dyn_cast<GlobalVariable>(GVal);
1240       if (GVar) {
1241         // Emit "mov ri, <imm>"
1242         int64_t Imm;
1243         uint32_t Reloc;
1244         if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr) ||
1245             GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr)) {
1246           Imm = PatchImms[GVar].first;
1247           Reloc = PatchImms[GVar].second;
1248         } else {
1249           return false;
1250         }
1251 
1252         if (Reloc == BPFCoreSharedInfo::ENUM_VALUE_EXISTENCE ||
1253             Reloc == BPFCoreSharedInfo::ENUM_VALUE ||
1254             Reloc == BPFCoreSharedInfo::BTF_TYPE_ID_LOCAL ||
1255             Reloc == BPFCoreSharedInfo::BTF_TYPE_ID_REMOTE)
1256           OutMI.setOpcode(BPF::LD_imm64);
1257         else
1258           OutMI.setOpcode(BPF::MOV_ri);
1259         OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1260         OutMI.addOperand(MCOperand::createImm(Imm));
1261         return true;
1262       }
1263     }
1264   } else if (MI->getOpcode() == BPF::CORE_MEM ||
1265              MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1266              MI->getOpcode() == BPF::CORE_SHIFT) {
1267     const MachineOperand &MO = MI->getOperand(3);
1268     if (MO.isGlobal()) {
1269       const GlobalValue *GVal = MO.getGlobal();
1270       auto *GVar = dyn_cast<GlobalVariable>(GVal);
1271       if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1272         uint32_t Imm = PatchImms[GVar].first;
1273         OutMI.setOpcode(MI->getOperand(1).getImm());
1274         if (MI->getOperand(0).isImm())
1275           OutMI.addOperand(MCOperand::createImm(MI->getOperand(0).getImm()));
1276         else
1277           OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1278         OutMI.addOperand(MCOperand::createReg(MI->getOperand(2).getReg()));
1279         OutMI.addOperand(MCOperand::createImm(Imm));
1280         return true;
1281       }
1282     }
1283   }
1284   return false;
1285 }
1286 
1287 void BTFDebug::processFuncPrototypes(const Function *F) {
1288   if (!F)
1289     return;
1290 
1291   const DISubprogram *SP = F->getSubprogram();
1292   if (!SP || SP->isDefinition())
1293     return;
1294 
1295   // Do not emit again if already emitted.
1296   if (ProtoFunctions.find(F) != ProtoFunctions.end())
1297     return;
1298   ProtoFunctions.insert(F);
1299 
1300   uint32_t ProtoTypeId;
1301   const std::unordered_map<uint32_t, StringRef> FuncArgNames;
1302   visitSubroutineType(SP->getType(), false, FuncArgNames, ProtoTypeId);
1303 
1304   uint8_t Scope = BTF::FUNC_EXTERN;
1305   auto FuncTypeEntry =
1306       std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
1307   uint32_t FuncId = addType(std::move(FuncTypeEntry));
1308   if (F->hasSection()) {
1309     StringRef SecName = F->getSection();
1310 
1311     if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1312       DataSecEntries[std::string(SecName)] =
1313           std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1314     }
1315 
1316     // We really don't know func size, set it to 0.
1317     DataSecEntries[std::string(SecName)]->addDataSecEntry(FuncId,
1318         Asm->getSymbol(F), 0);
1319   }
1320 }
1321 
1322 void BTFDebug::endModule() {
1323   // Collect MapDef globals if not collected yet.
1324   if (MapDefNotCollected) {
1325     processGlobals(true);
1326     MapDefNotCollected = false;
1327   }
1328 
1329   // Collect global types/variables except MapDef globals.
1330   processGlobals(false);
1331 
1332   for (auto &DataSec : DataSecEntries)
1333     addType(std::move(DataSec.second));
1334 
1335   // Fixups
1336   for (auto &Fixup : FixupDerivedTypes) {
1337     StringRef TypeName = Fixup.first;
1338     bool IsUnion = Fixup.second.first;
1339 
1340     // Search through struct types
1341     uint32_t StructTypeId = 0;
1342     for (const auto &StructType : StructTypes) {
1343       if (StructType->getName() == TypeName) {
1344         StructTypeId = StructType->getId();
1345         break;
1346       }
1347     }
1348 
1349     if (StructTypeId == 0) {
1350       auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
1351       StructTypeId = addType(std::move(FwdTypeEntry));
1352     }
1353 
1354     for (auto &DType : Fixup.second.second) {
1355       DType->setPointeeType(StructTypeId);
1356     }
1357   }
1358 
1359   // Complete BTF type cross refereences.
1360   for (const auto &TypeEntry : TypeEntries)
1361     TypeEntry->completeType(*this);
1362 
1363   // Emit BTF sections.
1364   emitBTFSection();
1365   emitBTFExtSection();
1366 }
1367