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