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