LLVM  9.0.0svn
BTFDebug.cpp
Go to the documentation of this file.
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"
17 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCSectionELF.h"
23 #include "llvm/MC/MCStreamer.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.
35  OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
36  ")");
39  OS.EmitIntValue(BTFType.Info, 4);
40  OS.EmitIntValue(BTFType.Size, 4);
41 }
42 
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 
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 
91 
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 
104  if (IsCompleted)
105  return;
106  IsCompleted = true;
107 
108  BTFType.NameOff = BDebug.addString(Name);
109 }
110 
112 
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 
141  if (IsCompleted)
142  return;
143  IsCompleted = true;
144 
145  BTFType.NameOff = BDebug.addString(Name);
146 }
147 
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;
158 }
159 
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 
181  for (const auto &Enum : EnumValues) {
182  OS.EmitIntValue(Enum.NameOff, 4);
183  OS.EmitIntValue(Enum.Val, 4);
184  }
185 }
186 
188  uint32_t NumElems)
189  : ElemSize(ElemSize) {
190  Kind = BTF::BTF_KIND_ARRAY;
191  BTFType.NameOff = 0;
192  BTFType.Info = Kind << 24;
193  BTFType.Size = 0;
194 
195  ArrayInfo.ElemType = ElemTypeId;
196  ArrayInfo.Nelems = NumElems;
197 }
198 
199 /// Represent a BTF array.
201  if (IsCompleted)
202  return;
203  IsCompleted = true;
204 
205  // The IR does not really have a type for the index.
206  // A special type for array index should have been
207  // created during initial type traversal. Just
208  // retrieve that type id.
209  ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
210 }
211 
214  OS.EmitIntValue(ArrayInfo.ElemType, 4);
215  OS.EmitIntValue(ArrayInfo.IndexType, 4);
216  OS.EmitIntValue(ArrayInfo.Nelems, 4);
217 }
218 
220  uint32_t &ElementTypeId) {
221  ElementTypeId = ArrayInfo.ElemType;
222  LocOffset = Loc * ElemSize;
223 }
224 
225 /// Represent either a struct or a union.
227  bool HasBitField, uint32_t Vlen)
228  : STy(STy), HasBitField(HasBitField) {
229  Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
231  BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
232 }
233 
235  if (IsCompleted)
236  return;
237  IsCompleted = true;
238 
239  BTFType.NameOff = BDebug.addString(STy->getName());
240 
241  // Add struct/union members.
242  const DINodeArray Elements = STy->getElements();
243  for (const auto *Element : Elements) {
244  struct BTF::BTFMember BTFMember;
245  const auto *DDTy = cast<DIDerivedType>(Element);
246 
247  BTFMember.NameOff = BDebug.addString(DDTy->getName());
248  if (HasBitField) {
249  uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
250  BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
251  } else {
252  BTFMember.Offset = DDTy->getOffsetInBits();
253  }
254  BTFMember.Type = BDebug.getTypeId(DDTy->getBaseType());
255  Members.push_back(BTFMember);
256  }
257 }
258 
261  for (const auto &Member : Members) {
262  OS.EmitIntValue(Member.NameOff, 4);
263  OS.EmitIntValue(Member.Type, 4);
264  OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
265  OS.EmitIntValue(Member.Offset, 4);
266  }
267 }
268 
269 std::string BTFTypeStruct::getName() { return STy->getName(); }
270 
272  uint32_t &MemberType) {
273  MemberType = Members[Loc].Type;
274  MemberOffset =
275  HasBitField ? Members[Loc].Offset & 0xffffff : Members[Loc].Offset;
276 }
277 
279 
280 /// The Func kind represents both subprogram and pointee of function
281 /// pointers. If the FuncName is empty, it represents a pointee of function
282 /// pointer. Otherwise, it represents a subprogram. The func arg names
283 /// are empty for pointee of function pointer case, and are valid names
284 /// for subprogram.
286  const DISubroutineType *STy, uint32_t VLen,
287  const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
288  : STy(STy), FuncArgNames(FuncArgNames) {
289  Kind = BTF::BTF_KIND_FUNC_PROTO;
290  BTFType.Info = (Kind << 24) | VLen;
291 }
292 
294  if (IsCompleted)
295  return;
296  IsCompleted = true;
297 
298  DITypeRefArray Elements = STy->getTypeArray();
299  auto RetType = Elements[0];
300  BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
301  BTFType.NameOff = 0;
302 
303  // For null parameter which is typically the last one
304  // to represent the vararg, encode the NameOff/Type to be 0.
305  for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
306  struct BTF::BTFParam Param;
307  auto Element = Elements[I];
308  if (Element) {
309  Param.NameOff = BDebug.addString(FuncArgNames[I]);
310  Param.Type = BDebug.getTypeId(Element);
311  } else {
312  Param.NameOff = 0;
313  Param.Type = 0;
314  }
315  Parameters.push_back(Param);
316  }
317 }
318 
321  for (const auto &Param : Parameters) {
322  OS.EmitIntValue(Param.NameOff, 4);
323  OS.EmitIntValue(Param.Type, 4);
324  }
325 }
326 
328  : Name(FuncName) {
329  Kind = BTF::BTF_KIND_FUNC;
330  BTFType.Info = Kind << 24;
331  BTFType.Type = ProtoTypeId;
332 }
333 
335  if (IsCompleted)
336  return;
337  IsCompleted = true;
338 
339  BTFType.NameOff = BDebug.addString(Name);
340 }
341 
343 
345  : Name(VarName) {
346  Kind = BTF::BTF_KIND_VAR;
347  BTFType.Info = Kind << 24;
348  BTFType.Type = TypeId;
349  Info = VarInfo;
350 }
351 
353  BTFType.NameOff = BDebug.addString(Name);
354 }
355 
358  OS.EmitIntValue(Info, 4);
359 }
360 
361 BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
362  : Asm(AsmPrt), Name(SecName) {
363  Kind = BTF::BTF_KIND_DATASEC;
364  BTFType.Info = Kind << 24;
365  BTFType.Size = 0;
366 }
367 
369  BTFType.NameOff = BDebug.addString(Name);
370  BTFType.Info |= Vars.size();
371 }
372 
375 
376  for (const auto &V : Vars) {
377  OS.EmitIntValue(std::get<0>(V), 4);
378  Asm->EmitLabelReference(std::get<1>(V), 4);
379  OS.EmitIntValue(std::get<2>(V), 4);
380  }
381 }
382 
384  // Check whether the string already exists.
385  for (auto &OffsetM : OffsetToIdMap) {
386  if (Table[OffsetM.second] == S)
387  return OffsetM.first;
388  }
389  // Not find, add to the string table.
390  uint32_t Offset = Size;
391  OffsetToIdMap[Offset] = Table.size();
392  Table.push_back(S);
393  Size += S.size() + 1;
394  return Offset;
395 }
396 
398  : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
399  LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
400  MapDefNotCollected(true) {
401  addString("\0");
402 }
403 
404 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
405  const DIType *Ty) {
406  TypeEntry->setId(TypeEntries.size() + 1);
407  uint32_t Id = TypeEntry->getId();
408  DIToIdMap[Ty] = Id;
409  TypeEntries.push_back(std::move(TypeEntry));
410  return Id;
411 }
412 
413 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
414  TypeEntry->setId(TypeEntries.size() + 1);
415  uint32_t Id = TypeEntry->getId();
416  TypeEntries.push_back(std::move(TypeEntry));
417  return Id;
418 }
419 
420 void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
421  // Only int types are supported in BTF.
422  uint32_t Encoding = BTy->getEncoding();
423  if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed &&
424  Encoding != dwarf::DW_ATE_signed_char &&
425  Encoding != dwarf::DW_ATE_unsigned &&
426  Encoding != dwarf::DW_ATE_unsigned_char)
427  return;
428 
429  // Create a BTF type instance for this DIBasicType and put it into
430  // DIToIdMap for cross-type reference check.
431  auto TypeEntry = llvm::make_unique<BTFTypeInt>(
432  Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
433  TypeId = addType(std::move(TypeEntry), BTy);
434 }
435 
436 /// Handle subprogram or subroutine types.
437 void BTFDebug::visitSubroutineType(
438  const DISubroutineType *STy, bool ForSubprog,
439  const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
440  uint32_t &TypeId) {
441  DITypeRefArray Elements = STy->getTypeArray();
442  uint32_t VLen = Elements.size() - 1;
443  if (VLen > BTF::MAX_VLEN)
444  return;
445 
446  // Subprogram has a valid non-zero-length name, and the pointee of
447  // a function pointer has an empty name. The subprogram type will
448  // not be added to DIToIdMap as it should not be referenced by
449  // any other types.
450  auto TypeEntry = llvm::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
451  if (ForSubprog)
452  TypeId = addType(std::move(TypeEntry)); // For subprogram
453  else
454  TypeId = addType(std::move(TypeEntry), STy); // For func ptr
455 
456  // Visit return type and func arg types.
457  for (const auto Element : Elements) {
458  visitTypeEntry(Element);
459  }
460 }
461 
462 /// Handle structure/union types.
463 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
464  uint32_t &TypeId) {
465  const DINodeArray Elements = CTy->getElements();
466  uint32_t VLen = Elements.size();
467  if (VLen > BTF::MAX_VLEN)
468  return;
469 
470  // Check whether we have any bitfield members or not
471  bool HasBitField = false;
472  for (const auto *Element : Elements) {
473  auto E = cast<DIDerivedType>(Element);
474  if (E->isBitField()) {
475  HasBitField = true;
476  break;
477  }
478  }
479 
480  auto TypeEntry =
481  llvm::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
482  StructTypes.push_back(TypeEntry.get());
483  TypeId = addType(std::move(TypeEntry), CTy);
484 
485  // Visit all struct members.
486  for (const auto *Element : Elements)
487  visitTypeEntry(cast<DIDerivedType>(Element));
488 }
489 
490 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
491  // Visit array element type.
492  uint32_t ElemTypeId, ElemSize;
493  const DIType *ElemType = CTy->getBaseType();
494  visitTypeEntry(ElemType, ElemTypeId, false, false);
495  ElemSize = ElemType->getSizeInBits() >> 3;
496 
497  if (!CTy->getSizeInBits()) {
498  auto TypeEntry = llvm::make_unique<BTFTypeArray>(ElemTypeId, 0, 0);
499  ArrayTypes.push_back(TypeEntry.get());
500  ElemTypeId = addType(std::move(TypeEntry), CTy);
501  } else {
502  // Visit array dimensions.
503  DINodeArray Elements = CTy->getElements();
504  for (int I = Elements.size() - 1; I >= 0; --I) {
505  if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
506  if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
507  const DISubrange *SR = cast<DISubrange>(Element);
508  auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
509  int64_t Count = CI->getSExtValue();
510 
511  auto TypeEntry =
512  llvm::make_unique<BTFTypeArray>(ElemTypeId, ElemSize, Count);
513  ArrayTypes.push_back(TypeEntry.get());
514  if (I == 0)
515  ElemTypeId = addType(std::move(TypeEntry), CTy);
516  else
517  ElemTypeId = addType(std::move(TypeEntry));
518  ElemSize = ElemSize * Count;
519  }
520  }
521  }
522 
523  // The array TypeId is the type id of the outermost dimension.
524  TypeId = ElemTypeId;
525 
526  // The IR does not have a type for array index while BTF wants one.
527  // So create an array index type if there is none.
528  if (!ArrayIndexTypeId) {
529  auto TypeEntry = llvm::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
530  0, "__ARRAY_SIZE_TYPE__");
531  ArrayIndexTypeId = addType(std::move(TypeEntry));
532  }
533 }
534 
535 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
536  DINodeArray Elements = CTy->getElements();
537  uint32_t VLen = Elements.size();
538  if (VLen > BTF::MAX_VLEN)
539  return;
540 
541  auto TypeEntry = llvm::make_unique<BTFTypeEnum>(CTy, VLen);
542  TypeId = addType(std::move(TypeEntry), CTy);
543  // No need to visit base type as BTF does not encode it.
544 }
545 
546 /// Handle structure/union forward declarations.
547 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
548  uint32_t &TypeId) {
549  auto TypeEntry = llvm::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
550  TypeId = addType(std::move(TypeEntry), CTy);
551 }
552 
553 /// Handle structure, union, array and enumeration types.
554 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
555  uint32_t &TypeId) {
556  auto Tag = CTy->getTag();
557  if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
558  // Handle forward declaration differently as it does not have members.
559  if (CTy->isForwardDecl())
560  visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
561  else
562  visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
563  } else if (Tag == dwarf::DW_TAG_array_type)
564  visitArrayType(CTy, TypeId);
565  else if (Tag == dwarf::DW_TAG_enumeration_type)
566  visitEnumType(CTy, TypeId);
567 }
568 
569 /// Handle pointer, typedef, const, volatile, restrict and member types.
570 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
571  bool CheckPointer, bool SeenPointer) {
572  unsigned Tag = DTy->getTag();
573 
574  /// Try to avoid chasing pointees, esp. structure pointees which may
575  /// unnecessary bring in a lot of types.
576  if (CheckPointer && !SeenPointer) {
577  SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
578  }
579 
580  if (CheckPointer && SeenPointer) {
581  const DIType *Base = DTy->getBaseType();
582  if (Base) {
583  if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
584  auto CTag = CTy->getTag();
585  if ((CTag == dwarf::DW_TAG_structure_type ||
586  CTag == dwarf::DW_TAG_union_type) &&
587  !CTy->isForwardDecl()) {
588  /// Find a candidate, generate a fixup. Later on the struct/union
589  /// pointee type will be replaced with either a real type or
590  /// a forward declaration.
591  auto TypeEntry = llvm::make_unique<BTFTypeDerived>(DTy, Tag, true);
592  auto &Fixup = FixupDerivedTypes[CTy->getName()];
593  Fixup.first = CTag == dwarf::DW_TAG_union_type;
594  Fixup.second.push_back(TypeEntry.get());
595  TypeId = addType(std::move(TypeEntry), DTy);
596  return;
597  }
598  }
599  }
600  }
601 
602  if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef ||
603  Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
604  Tag == dwarf::DW_TAG_restrict_type) {
605  auto TypeEntry = llvm::make_unique<BTFTypeDerived>(DTy, Tag, false);
606  TypeId = addType(std::move(TypeEntry), DTy);
607  } else if (Tag != dwarf::DW_TAG_member) {
608  return;
609  }
610 
611  // Visit base type of pointer, typedef, const, volatile, restrict or
612  // struct/union member.
613  uint32_t TempTypeId = 0;
614  if (Tag == dwarf::DW_TAG_member)
615  visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
616  else
617  visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
618 }
619 
620 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
621  bool CheckPointer, bool SeenPointer) {
622  if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
623  TypeId = DIToIdMap[Ty];
624  return;
625  }
626 
627  if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
628  visitBasicType(BTy, TypeId);
629  else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
630  visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
631  TypeId);
632  else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
633  visitCompositeType(CTy, TypeId);
634  else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
635  visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
636  else
637  llvm_unreachable("Unknown DIType");
638 }
639 
640 void BTFDebug::visitTypeEntry(const DIType *Ty) {
641  uint32_t TypeId;
642  visitTypeEntry(Ty, TypeId, false, false);
643 }
644 
645 void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
646  if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
647  TypeId = DIToIdMap[Ty];
648  return;
649  }
650 
651  // MapDef type is a struct type
652  const auto *CTy = dyn_cast<DICompositeType>(Ty);
653  if (!CTy)
654  return;
655 
656  auto Tag = CTy->getTag();
657  if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
658  return;
659 
660  // Record this type
661  const DINodeArray Elements = CTy->getElements();
662  bool HasBitField = false;
663  for (const auto *Element : Elements) {
664  auto E = cast<DIDerivedType>(Element);
665  if (E->isBitField()) {
666  HasBitField = true;
667  break;
668  }
669  }
670 
671  auto TypeEntry =
672  llvm::make_unique<BTFTypeStruct>(CTy, true, HasBitField, Elements.size());
673  StructTypes.push_back(TypeEntry.get());
674  TypeId = addType(std::move(TypeEntry), CTy);
675 
676  // Visit all struct members
677  for (const auto *Element : Elements) {
678  const auto *MemberType = cast<DIDerivedType>(Element);
679  visitTypeEntry(MemberType->getBaseType());
680  }
681 }
682 
683 /// Read file contents from the actual file or from the source
684 std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
685  auto File = SP->getFile();
686  std::string FileName;
687 
688  if (!File->getFilename().startswith("/") && File->getDirectory().size())
689  FileName = File->getDirectory().str() + "/" + File->getFilename().str();
690  else
691  FileName = File->getFilename();
692 
693  // No need to populate the contends if it has been populated!
694  if (FileContent.find(FileName) != FileContent.end())
695  return FileName;
696 
697  std::vector<std::string> Content;
698  std::string Line;
699  Content.push_back(Line); // Line 0 for empty string
700 
701  std::unique_ptr<MemoryBuffer> Buf;
702  auto Source = File->getSource();
703  if (Source)
705  else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
706  MemoryBuffer::getFile(FileName))
707  Buf = std::move(*BufOrErr);
708  if (Buf)
709  for (line_iterator I(*Buf, false), E; I != E; ++I)
710  Content.push_back(*I);
711 
712  FileContent[FileName] = Content;
713  return FileName;
714 }
715 
716 void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
717  uint32_t Line, uint32_t Column) {
718  std::string FileName = populateFileContent(SP);
719  BTFLineInfo LineInfo;
720 
721  LineInfo.Label = Label;
722  LineInfo.FileNameOff = addString(FileName);
723  // If file content is not available, let LineOff = 0.
724  if (Line < FileContent[FileName].size())
725  LineInfo.LineOff = addString(FileContent[FileName][Line]);
726  else
727  LineInfo.LineOff = 0;
728  LineInfo.LineNum = Line;
729  LineInfo.ColumnNum = Column;
730  LineInfoTable[SecNameOff].push_back(LineInfo);
731 }
732 
733 void BTFDebug::emitCommonHeader() {
735  OS.EmitIntValue(BTF::MAGIC, 2);
736  OS.EmitIntValue(BTF::VERSION, 1);
737  OS.EmitIntValue(0, 1);
738 }
739 
740 void BTFDebug::emitBTFSection() {
741  // Do not emit section if no types and only "" string.
742  if (!TypeEntries.size() && StringTable.getSize() == 1)
743  return;
744 
745  MCContext &Ctx = OS.getContext();
746  OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0));
747 
748  // Emit header.
749  emitCommonHeader();
751 
752  uint32_t TypeLen = 0, StrLen;
753  for (const auto &TypeEntry : TypeEntries)
754  TypeLen += TypeEntry->getSize();
755  StrLen = StringTable.getSize();
756 
757  OS.EmitIntValue(0, 4);
758  OS.EmitIntValue(TypeLen, 4);
759  OS.EmitIntValue(TypeLen, 4);
760  OS.EmitIntValue(StrLen, 4);
761 
762  // Emit type table.
763  for (const auto &TypeEntry : TypeEntries)
764  TypeEntry->emitType(OS);
765 
766  // Emit string table.
767  uint32_t StringOffset = 0;
768  for (const auto &S : StringTable.getTable()) {
769  OS.AddComment("string offset=" + std::to_string(StringOffset));
770  OS.EmitBytes(S);
771  OS.EmitBytes(StringRef("\0", 1));
772  StringOffset += S.size() + 1;
773  }
774 }
775 
776 void BTFDebug::emitBTFExtSection() {
777  // Do not emit section if empty FuncInfoTable and LineInfoTable.
778  if (!FuncInfoTable.size() && !LineInfoTable.size() &&
779  !OffsetRelocTable.size() && !ExternRelocTable.size())
780  return;
781 
782  MCContext &Ctx = OS.getContext();
783  OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0));
784 
785  // Emit header.
786  emitCommonHeader();
788 
789  // Account for FuncInfo/LineInfo record size as well.
790  uint32_t FuncLen = 4, LineLen = 4;
791  // Do not account for optional OffsetReloc/ExternReloc.
792  uint32_t OffsetRelocLen = 0, ExternRelocLen = 0;
793  for (const auto &FuncSec : FuncInfoTable) {
794  FuncLen += BTF::SecFuncInfoSize;
795  FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
796  }
797  for (const auto &LineSec : LineInfoTable) {
798  LineLen += BTF::SecLineInfoSize;
799  LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
800  }
801  for (const auto &OffsetRelocSec : OffsetRelocTable) {
802  OffsetRelocLen += BTF::SecOffsetRelocSize;
803  OffsetRelocLen += OffsetRelocSec.second.size() * BTF::BPFOffsetRelocSize;
804  }
805  for (const auto &ExternRelocSec : ExternRelocTable) {
806  ExternRelocLen += BTF::SecExternRelocSize;
807  ExternRelocLen += ExternRelocSec.second.size() * BTF::BPFExternRelocSize;
808  }
809 
810  if (OffsetRelocLen)
811  OffsetRelocLen += 4;
812  if (ExternRelocLen)
813  ExternRelocLen += 4;
814 
815  OS.EmitIntValue(0, 4);
816  OS.EmitIntValue(FuncLen, 4);
817  OS.EmitIntValue(FuncLen, 4);
818  OS.EmitIntValue(LineLen, 4);
819  OS.EmitIntValue(FuncLen + LineLen, 4);
820  OS.EmitIntValue(OffsetRelocLen, 4);
821  OS.EmitIntValue(FuncLen + LineLen + OffsetRelocLen, 4);
822  OS.EmitIntValue(ExternRelocLen, 4);
823 
824  // Emit func_info table.
825  OS.AddComment("FuncInfo");
827  for (const auto &FuncSec : FuncInfoTable) {
828  OS.AddComment("FuncInfo section string offset=" +
829  std::to_string(FuncSec.first));
830  OS.EmitIntValue(FuncSec.first, 4);
831  OS.EmitIntValue(FuncSec.second.size(), 4);
832  for (const auto &FuncInfo : FuncSec.second) {
833  Asm->EmitLabelReference(FuncInfo.Label, 4);
834  OS.EmitIntValue(FuncInfo.TypeId, 4);
835  }
836  }
837 
838  // Emit line_info table.
839  OS.AddComment("LineInfo");
841  for (const auto &LineSec : LineInfoTable) {
842  OS.AddComment("LineInfo section string offset=" +
843  std::to_string(LineSec.first));
844  OS.EmitIntValue(LineSec.first, 4);
845  OS.EmitIntValue(LineSec.second.size(), 4);
846  for (const auto &LineInfo : LineSec.second) {
847  Asm->EmitLabelReference(LineInfo.Label, 4);
848  OS.EmitIntValue(LineInfo.FileNameOff, 4);
849  OS.EmitIntValue(LineInfo.LineOff, 4);
850  OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
851  std::to_string(LineInfo.ColumnNum));
852  OS.EmitIntValue(LineInfo.LineNum << 10 | LineInfo.ColumnNum, 4);
853  }
854  }
855 
856  // Emit offset reloc table.
857  if (OffsetRelocLen) {
858  OS.AddComment("OffsetReloc");
860  for (const auto &OffsetRelocSec : OffsetRelocTable) {
861  OS.AddComment("Offset reloc section string offset=" +
862  std::to_string(OffsetRelocSec.first));
863  OS.EmitIntValue(OffsetRelocSec.first, 4);
864  OS.EmitIntValue(OffsetRelocSec.second.size(), 4);
865  for (const auto &OffsetRelocInfo : OffsetRelocSec.second) {
866  Asm->EmitLabelReference(OffsetRelocInfo.Label, 4);
867  OS.EmitIntValue(OffsetRelocInfo.TypeID, 4);
868  OS.EmitIntValue(OffsetRelocInfo.OffsetNameOff, 4);
869  }
870  }
871  }
872 
873  // Emit extern reloc table.
874  if (ExternRelocLen) {
875  OS.AddComment("ExternReloc");
877  for (const auto &ExternRelocSec : ExternRelocTable) {
878  OS.AddComment("Extern reloc section string offset=" +
879  std::to_string(ExternRelocSec.first));
880  OS.EmitIntValue(ExternRelocSec.first, 4);
881  OS.EmitIntValue(ExternRelocSec.second.size(), 4);
882  for (const auto &ExternRelocInfo : ExternRelocSec.second) {
883  Asm->EmitLabelReference(ExternRelocInfo.Label, 4);
884  OS.EmitIntValue(ExternRelocInfo.ExternNameOff, 4);
885  }
886  }
887  }
888 }
889 
891  auto *SP = MF->getFunction().getSubprogram();
892  auto *Unit = SP->getUnit();
893 
894  if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
895  SkipInstruction = true;
896  return;
897  }
898  SkipInstruction = false;
899 
900  // Collect MapDef types. Map definition needs to collect
901  // pointee types. Do it first. Otherwise, for the following
902  // case:
903  // struct m { ...};
904  // struct t {
905  // struct m *key;
906  // };
907  // foo(struct t *arg);
908  //
909  // struct mapdef {
910  // ...
911  // struct m *key;
912  // ...
913  // } __attribute__((section(".maps"))) hash_map;
914  //
915  // If subroutine foo is traversed first, a type chain
916  // "ptr->struct m(fwd)" will be created and later on
917  // when traversing mapdef, since "ptr->struct m" exists,
918  // the traversal of "struct m" will be omitted.
919  if (MapDefNotCollected) {
920  processGlobals(true);
921  MapDefNotCollected = false;
922  }
923 
924  // Collect all types locally referenced in this function.
925  // Use RetainedNodes so we can collect all argument names
926  // even if the argument is not used.
927  std::unordered_map<uint32_t, StringRef> FuncArgNames;
928  for (const DINode *DN : SP->getRetainedNodes()) {
929  if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
930  // Collect function arguments for subprogram func type.
931  uint32_t Arg = DV->getArg();
932  if (Arg) {
933  visitTypeEntry(DV->getType());
934  FuncArgNames[Arg] = DV->getName();
935  }
936  }
937  }
938 
939  // Construct subprogram func proto type.
940  uint32_t ProtoTypeId;
941  visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
942 
943  // Construct subprogram func type
944  auto FuncTypeEntry =
945  llvm::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId);
946  uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
947 
948  for (const auto &TypeEntry : TypeEntries)
949  TypeEntry->completeType(*this);
950 
951  // Construct funcinfo and the first lineinfo for the function.
952  MCSymbol *FuncLabel = Asm->getFunctionBegin();
953  BTFFuncInfo FuncInfo;
954  FuncInfo.Label = FuncLabel;
955  FuncInfo.TypeId = FuncTypeId;
956  if (FuncLabel->isInSection()) {
957  MCSection &Section = FuncLabel->getSection();
958  const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
959  assert(SectionELF && "Null section for Function Label");
960  SecNameOff = addString(SectionELF->getSectionName());
961  } else {
962  SecNameOff = addString(".text");
963  }
964  FuncInfoTable[SecNameOff].push_back(FuncInfo);
965 }
966 
968  SkipInstruction = false;
969  LineInfoGenerated = false;
970  SecNameOff = 0;
971 }
972 
973 /// On-demand populate struct types as requested from abstract member
974 /// accessing.
975 unsigned BTFDebug::populateStructType(const DIType *Ty) {
976  unsigned Id;
977  visitTypeEntry(Ty, Id, false, false);
978  for (const auto &TypeEntry : TypeEntries)
979  TypeEntry->completeType(*this);
980  return Id;
981 }
982 
983 // Find struct/array debuginfo types given a type id.
984 void BTFDebug::setTypeFromId(uint32_t TypeId, BTFTypeStruct **PrevStructType,
985  BTFTypeArray **PrevArrayType) {
986  for (const auto &StructType : StructTypes) {
987  if (StructType->getId() == TypeId) {
988  *PrevStructType = StructType;
989  return;
990  }
991  }
992  for (const auto &ArrayType : ArrayTypes) {
993  if (ArrayType->getId() == TypeId) {
994  *PrevArrayType = ArrayType;
995  return;
996  }
997  }
998 }
999 
1000 /// Generate a struct member offset relocation.
1001 void BTFDebug::generateOffsetReloc(const MachineInstr *MI,
1002  const MCSymbol *ORSym, DIType *RootTy,
1003  StringRef AccessPattern) {
1004  BTFTypeStruct *PrevStructType = nullptr;
1005  BTFTypeArray *PrevArrayType = nullptr;
1006  unsigned RootId = populateStructType(RootTy);
1007  setTypeFromId(RootId, &PrevStructType, &PrevArrayType);
1008  unsigned RootTySize = PrevStructType->getStructSize();
1009 
1010  BTFOffsetReloc OffsetReloc;
1011  OffsetReloc.Label = ORSym;
1012  OffsetReloc.OffsetNameOff = addString(AccessPattern.drop_back());
1013  OffsetReloc.TypeID = RootId;
1014 
1015  uint32_t Start = 0, End = 0, Offset = 0;
1016  bool FirstAccess = true;
1017  for (auto C : AccessPattern) {
1018  if (C != ':') {
1019  End++;
1020  } else {
1021  std::string SubStr = AccessPattern.substr(Start, End - Start);
1022  int Loc = std::stoi(SubStr);
1023 
1024  if (FirstAccess) {
1025  Offset = Loc * RootTySize;
1026  FirstAccess = false;
1027  } else if (PrevStructType) {
1028  uint32_t MemberOffset, MemberTypeId;
1029  PrevStructType->getMemberInfo(Loc, MemberOffset, MemberTypeId);
1030 
1031  Offset += MemberOffset >> 3;
1032  PrevStructType = nullptr;
1033  setTypeFromId(MemberTypeId, &PrevStructType, &PrevArrayType);
1034  } else if (PrevArrayType) {
1035  uint32_t LocOffset, ElementTypeId;
1036  PrevArrayType->getLocInfo(Loc, LocOffset, ElementTypeId);
1037 
1038  Offset += LocOffset;
1039  PrevArrayType = nullptr;
1040  setTypeFromId(ElementTypeId, &PrevStructType, &PrevArrayType);
1041  }
1042  Start = End + 1;
1043  End = Start;
1044  }
1045  }
1046  AccessOffsets[RootTy->getName().str() + ":" + AccessPattern.str()] = Offset;
1047  OffsetRelocTable[SecNameOff].push_back(OffsetReloc);
1048 }
1049 
1050 void BTFDebug::processLDimm64(const MachineInstr *MI) {
1051  // If the insn is an LD_imm64, the following two cases
1052  // will generate an .BTF.ext record.
1053  //
1054  // If the insn is "r2 = LD_imm64 @__BTF_...",
1055  // add this insn into the .BTF.ext OffsetReloc subsection.
1056  // Relocation looks like:
1057  // . SecName:
1058  // . InstOffset
1059  // . TypeID
1060  // . OffSetNameOff
1061  // Later, the insn is replaced with "r2 = <offset>"
1062  // where "<offset>" equals to the offset based on current
1063  // type definitions.
1064  //
1065  // If the insn is "r2 = LD_imm64 @VAR" and VAR is
1066  // a patchable external global, add this insn into the .BTF.ext
1067  // ExternReloc subsection.
1068  // Relocation looks like:
1069  // . SecName:
1070  // . InstOffset
1071  // . ExternNameOff
1072  // Later, the insn is replaced with "r2 = <value>" or
1073  // "LD_imm64 r2, <value>" where "<value>" = 0.
1074 
1075  // check whether this is a candidate or not
1076  const MachineOperand &MO = MI->getOperand(1);
1077  if (MO.isGlobal()) {
1078  const GlobalValue *GVal = MO.getGlobal();
1079  auto *GVar = dyn_cast<GlobalVariable>(GVal);
1080  if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1081  MCSymbol *ORSym = OS.getContext().createTempSymbol();
1082  OS.EmitLabel(ORSym);
1083 
1084  MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1085  DIType *Ty = dyn_cast<DIType>(MDN);
1086  generateOffsetReloc(MI, ORSym, Ty, GVar->getName());
1087  } else if (GVar && !GVar->hasInitializer() && GVar->hasExternalLinkage() &&
1088  GVar->getSection() == BPFCoreSharedInfo::PatchableExtSecName) {
1089  MCSymbol *ORSym = OS.getContext().createTempSymbol();
1090  OS.EmitLabel(ORSym);
1091 
1092  BTFExternReloc ExternReloc;
1093  ExternReloc.Label = ORSym;
1094  ExternReloc.ExternNameOff = addString(GVar->getName());
1095  ExternRelocTable[SecNameOff].push_back(ExternReloc);
1096  }
1097  }
1098 }
1099 
1102 
1103  if (SkipInstruction || MI->isMetaInstruction() ||
1105  return;
1106 
1107  if (MI->isInlineAsm()) {
1108  // Count the number of register definitions to find the asm string.
1109  unsigned NumDefs = 0;
1110  for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
1111  ++NumDefs)
1112  ;
1113 
1114  // Skip this inline asm instruction if the asmstr is empty.
1115  const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1116  if (AsmStr[0] == 0)
1117  return;
1118  }
1119 
1120  if (MI->getOpcode() == BPF::LD_imm64)
1121  processLDimm64(MI);
1122 
1123  // Skip this instruction if no DebugLoc or the DebugLoc
1124  // is the same as the previous instruction.
1125  const DebugLoc &DL = MI->getDebugLoc();
1126  if (!DL || PrevInstLoc == DL) {
1127  // This instruction will be skipped, no LineInfo has
1128  // been generated, construct one based on function signature.
1129  if (LineInfoGenerated == false) {
1130  auto *S = MI->getMF()->getFunction().getSubprogram();
1131  MCSymbol *FuncLabel = Asm->getFunctionBegin();
1132  constructLineInfo(S, FuncLabel, S->getLine(), 0);
1133  LineInfoGenerated = true;
1134  }
1135 
1136  return;
1137  }
1138 
1139  // Create a temporary label to remember the insn for lineinfo.
1140  MCSymbol *LineSym = OS.getContext().createTempSymbol();
1141  OS.EmitLabel(LineSym);
1142 
1143  // Construct the lineinfo.
1144  auto SP = DL.get()->getScope()->getSubprogram();
1145  constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
1146 
1147  LineInfoGenerated = true;
1148  PrevInstLoc = DL;
1149 }
1150 
1151 void BTFDebug::processGlobals(bool ProcessingMapDef) {
1152  // Collect all types referenced by globals.
1153  const Module *M = MMI->getModule();
1154  for (const GlobalVariable &Global : M->globals()) {
1155  // Ignore external globals for now.
1156  if (!Global.hasInitializer() && Global.hasExternalLinkage())
1157  continue;
1158 
1159  // Decide the section name.
1160  StringRef SecName;
1161  if (Global.hasSection()) {
1162  SecName = Global.getSection();
1163  } else {
1164  // data, bss, or readonly sections
1165  if (Global.isConstant())
1166  SecName = ".rodata";
1167  else
1168  SecName = Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
1169  }
1170 
1171  if (ProcessingMapDef != SecName.startswith(".maps"))
1172  continue;
1173 
1175  Global.getDebugInfo(GVs);
1176  uint32_t GVTypeId = 0;
1177  for (auto *GVE : GVs) {
1178  if (SecName.startswith(".maps"))
1179  visitMapDefType(GVE->getVariable()->getType(), GVTypeId);
1180  else
1181  visitTypeEntry(GVE->getVariable()->getType(), GVTypeId, false, false);
1182  break;
1183  }
1184 
1185  // Only support the following globals:
1186  // . static variables
1187  // . non-static global variables with section attributes
1188  // Essentially means:
1189  // . .bcc/.data/.rodata DataSec entities only contain static data
1190  // . Other DataSec entities contain static or initialized global data.
1191  // Initialized global data are mostly used for finding map key/value type
1192  // id's. Whether DataSec is readonly or not can be found from
1193  // corresponding ELF section flags.
1194  auto Linkage = Global.getLinkage();
1195  if (Linkage != GlobalValue::InternalLinkage &&
1196  (Linkage != GlobalValue::ExternalLinkage || !Global.hasSection()))
1197  continue;
1198 
1199  uint32_t GVarInfo = Linkage == GlobalValue::ExternalLinkage
1201  : BTF::VAR_STATIC;
1202  auto VarEntry =
1203  llvm::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1204  uint32_t VarId = addType(std::move(VarEntry));
1205 
1206  // Find or create a DataSec
1207  if (DataSecEntries.find(SecName) == DataSecEntries.end()) {
1208  DataSecEntries[SecName] = llvm::make_unique<BTFKindDataSec>(Asm, SecName);
1209  }
1210 
1211  // Calculate symbol size
1212  const DataLayout &DL = Global.getParent()->getDataLayout();
1213  uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());
1214 
1215  DataSecEntries[SecName]->addVar(VarId, Asm->getSymbol(&Global), Size);
1216  }
1217 }
1218 
1219 /// Emit proper patchable instructions.
1220 bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1221  if (MI->getOpcode() == BPF::LD_imm64) {
1222  const MachineOperand &MO = MI->getOperand(1);
1223  if (MO.isGlobal()) {
1224  const GlobalValue *GVal = MO.getGlobal();
1225  auto *GVar = dyn_cast<GlobalVariable>(GVal);
1226  if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1227  MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1228  DIType *Ty = dyn_cast<DIType>(MDN);
1229  std::string TypeName = Ty->getName();
1230  int64_t Imm = AccessOffsets[TypeName + ":" + GVar->getName().str()];
1231 
1232  // Emit "mov ri, <imm>" for abstract member accesses.
1233  OutMI.setOpcode(BPF::MOV_ri);
1235  OutMI.addOperand(MCOperand::createImm(Imm));
1236  return true;
1237  } else if (GVar && !GVar->hasInitializer() &&
1238  GVar->hasExternalLinkage() &&
1239  GVar->getSection() == BPFCoreSharedInfo::PatchableExtSecName) {
1240  const IntegerType *IntTy = dyn_cast<IntegerType>(GVar->getValueType());
1241  assert(IntTy);
1242  // For patchable externals, emit "LD_imm64, ri, 0" if the external
1243  // variable is 64bit width, emit "mov ri, 0" otherwise.
1244  if (IntTy->getBitWidth() == 64)
1245  OutMI.setOpcode(BPF::LD_imm64);
1246  else
1247  OutMI.setOpcode(BPF::MOV_ri);
1249  OutMI.addOperand(MCOperand::createImm(0));
1250  return true;
1251  }
1252  }
1253  }
1254  return false;
1255 }
1256 
1258  // Collect MapDef globals if not collected yet.
1259  if (MapDefNotCollected) {
1260  processGlobals(true);
1261  MapDefNotCollected = false;
1262  }
1263 
1264  // Collect global types/variables except MapDef globals.
1265  processGlobals(false);
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 = llvm::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 }
uint64_t CallInst * C
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
Handle struct/union type.
Definition: BTFDebug.h:119
Handle array type.
Definition: BTFDebug.h:106
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
Represents either an error or a value T.
Definition: ErrorOr.h:56
uint32_t Type
Definition: BTF.h:185
uint64_t getOffsetInBits() const
void endFunctionImpl(const MachineFunction *MF) override
Post process after all instructions in this function are processed.
Definition: BTFDebug.cpp:967
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:21
Represent one func and its type id.
Definition: BTFDebug.h:216
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
Definition: BTFDebug.cpp:361
BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId)
Definition: BTFDebug.cpp:327
uint32_t OffsetNameOff
The string to traverse types.
Definition: BTFDebug.h:234
uint32_t ColumnNum
the column number
Definition: BTFDebug.h:227
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint32_t NameOff
Definition: BTF.h:184
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
unsigned size() const
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
DIFile * getFile() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:385
uint32_t getArrayIndexTypeId()
Get the special array index type id.
Definition: BTFDebug.h:344
virtual void EmitBytes(StringRef Data)
Emit the bytes in Data into the output.
bool isInlineAsm() const
uint32_t getSize()
Definition: BTFDebug.h:208
BTFTypeEnum(const DICompositeType *ETy, uint32_t NumValues)
Definition: BTFDebug.cpp:154
iterator find(StringRef Key)
Definition: StringMap.h:332
Externally visible function.
Definition: GlobalValue.h:48
BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, uint32_t OffsetInBits, StringRef TypeName)
Definition: BTFDebug.cpp:113
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:31
void completeType(BTFDebug &BDebug)
Represent a BTF array.
Definition: BTFDebug.cpp:200
unsigned getLine() const
Definition: DebugLoc.cpp:25
uint32_t Size
Definition: BTF.h:128
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:863
block Block Frequency true
bool isMetaInstruction() const
Return true if this instruction doesn&#39;t produce any output in the form of executable instructions...
bool isForwardDecl() const
static const char * BTFKindStr[]
Definition: BTFDebug.cpp:28
uint32_t Offset
BitOffset or BitFieldSize+BitOffset.
Definition: BTF.h:177
std::string getName()
Definition: BTFDebug.cpp:269
StringRef getName() const
Tagged DWARF-like metadata node.
uint32_t getStructSize()
Definition: BTFDebug.cpp:278
DINodeArray getElements() const
uint32_t Type
Member type.
Definition: BTF.h:176
MCContext & getContext() const
Definition: MCStreamer.h:250
DebugLoc PrevInstLoc
Previous instruction&#39;s location information.
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:342
BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup)
Definition: BTFDebug.cpp:43
uint32_t ElemType
Element type.
Definition: BTF.h:159
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:319
unsigned getTag() const
Class to represent struct types.
Definition: DerivedTypes.h:233
Array subrange.
virtual void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:34
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:253
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:140
BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
Definition: BTF.h:152
BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
Definition: BTFDebug.cpp:344
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:411
uint32_t roundupToBytes(uint32_t NumBits)
Definition: BTFDebug.h:44
uint64_t getSizeInBits() const
const char * getSymbolName() const
void getLocInfo(uint32_t Loc, uint32_t &LocOffset, uint32_t &ElementTypeId)
Definition: BTFDebug.cpp:219
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
Definition: BTFDebug.cpp:890
Context object for machine code objects.
Definition: MCContext.h:64
Subprogram description.
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:309
DIType * getBaseType() const
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:160
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:208
uint32_t Type
Definition: BTF.h:129
Linkage: InternalLinkage.
Definition: BTF.h:190
Class to represent array types.
Definition: DerivedTypes.h:403
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:373
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:66
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:128
BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
Definition: BTF.h:183
static const std::string PatchableExtSecName
The section name to identify a patchable external global.
Definition: BPFCORE.h:19
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
void endModule() override
Complete all the types and emit the BTF sections.
Definition: BTFDebug.cpp:1257
Represent one extern relocation.
Definition: BTFDebug.h:238
uint8_t Kind
Definition: BTFDebug.h:34
BTFTypeStruct(const DICompositeType *STy, bool IsStruct, bool HasBitField, uint32_t NumMembers)
Represent either a struct or a union.
Definition: BTFDebug.cpp:226
Streaming machine code generation interface.
Definition: MCStreamer.h:188
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:223
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:103
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:368
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1504
Instrumentation for Order File
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
CountType getCount() const
struct BTF::CommonType BTFType
Definition: BTFDebug.h:37
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
Definition: BTFDebug.cpp:1100
const GlobalValue * getGlobal() const
AsmPrinter * Asm
Target of debug info emission.
const MCSymbol * Label
MCSymbol identifying insn for the reloc.
Definition: BTFDebug.h:239
uint32_t ExternNameOff
The extern variable name.
Definition: BTFDebug.h:240
Collect and emit BTF information.
Definition: BTFDebug.h:244
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
BTFTypeArray(uint32_t ElemTypeId, uint32_t ElemSize, uint32_t NumElems)
Definition: BTFDebug.cpp:187
uint32_t NameOff
Member name offset in the string table.
Definition: BTF.h:175
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:148
bool InstLower(const MachineInstr *MI, MCInst &OutMI)
Emit proper patchable instructions.
Definition: BTFDebug.cpp:1220
StringRef getName() const
Return the name for this struct type if it has an identity.
Definition: Type.cpp:499
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:352
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
void EmitLabelReference(const MCSymbol *Label, unsigned Size, bool IsSectionRelative=false) const
Emit something like ".long Label" where the size in bytes of the directive is specified by Size and L...
Definition: AsmPrinter.h:499
Represent one line info.
Definition: BTFDebug.h:222
Class to represent integer types.
Definition: DerivedTypes.h:40
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
Represent one offset relocation.
Definition: BTFDebug.h:231
BTF_KIND_STRUCT and BTF_KIND_UNION are followed by multiple "struct BTFMember".
Definition: BTF.h:174
uint32_t NameOff
Type name offset in the string table.
Definition: BTF.h:110
Max # of struct/union/enum members or func args.
Definition: BTF.h:98
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:179
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
PowerPC TLS Dynamic Call Fixup
std::vector< std::string > & getTable()
Definition: BTFDebug.h:209
void setOpcode(unsigned Op)
Definition: MCInst.h:170
Base class for types.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
uint32_t LineNum
the line number
Definition: BTFDebug.h:226
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1173
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
uint32_t Id
Definition: BTFDebug.h:36
StringRef getName() const
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:443
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
int32_t Val
Enum member value.
Definition: BTF.h:154
uint32_t NameOff
Enum name offset in the string table.
Definition: BTF.h:153
void setPointeeType(uint32_t PointeeType)
Definition: BTFDebug.cpp:92
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:234
MCSymbol * Label
MCSymbol identifying insn for the lineinfo.
Definition: BTFDebug.h:223
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:387
uint32_t getTypeId(const DIType *Ty)
Get the type id for a particular DIType.
Definition: BTFDebug.h:353
uint32_t IndexType
Index type.
Definition: BTF.h:160
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:293
const Function & getFunction() const
Return the LLVM function that this machine code represents.
Linkage: ExternalLinkage.
Definition: BTF.h:191
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:268
This file contains support for writing BTF debug info.
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:212
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:68
uint32_t Nelems
Number of elements for this array.
Definition: BTF.h:161
BTFDebug(AsmPrinter *AP)
Definition: BTFDebug.cpp:397
Type array for a subprogram.
Representation of each machine instruction.
Definition: MachineInstr.h:64
uint32_t addString(StringRef S)
Add a string to the string table and returns its offset in the table.
Definition: BTFDebug.cpp:383
void getMemberInfo(uint32_t Loc, uint32_t &Offset, uint32_t &MemberType)
Definition: BTFDebug.cpp:271
unsigned getEncoding() const
BTFTypeFwd(StringRef Name, bool IsUnion)
Represent a struct/union forward declaration.
Definition: BTFDebug.cpp:97
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:111
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Base class for debug information backends.
BTFTypeFuncProto(const DISubroutineType *STy, uint32_t NumParams, const std::unordered_map< uint32_t, StringRef > &FuncArgNames)
The Func kind represents both subprogram and pointee of function pointers.
Definition: BTFDebug.cpp:285
uint32_t Size
Definition: Profile.cpp:46
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
static const std::string AmaAttr
The attribute attached to globals representing a member offset.
Definition: BPFCORE.h:17
const MCSymbol * Label
Func MCSymbol.
Definition: BTFDebug.h:217
const Module * getModule() const
uint32_t FileNameOff
file name offset in the .BTF string table
Definition: BTFDebug.h:224
size_t addString(StringRef S)
Add string to the string table.
Definition: BTFDebug.h:350
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
unsigned getCol() const
Definition: DebugLoc.cpp:30
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:334
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:406
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:90
LLVM_NODISCARD StringRef drop_back(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the last N elements dropped.
Definition: StringRef.h:628
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:351
DITypeRefArray getTypeArray() const
MachineModuleInfo * MMI
Collected machine module information.
iterator_range< global_iterator > globals()
Definition: Module.h:587
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
uint32_t Info
"Info" bits arrangement: Bits 0-15: vlen (e.g.
Definition: BTF.h:119
Register getReg() const
getReg - Returns the register number.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:156
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:356
const MCSymbol * Label
MCSymbol identifying insn for the reloc.
Definition: BTFDebug.h:232
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:259
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
Definition: MachineInstr.h:297
iterator end()
Definition: StringMap.h:317
uint32_t TypeId
Type id referring to .BTF type section.
Definition: BTFDebug.h:218
StringRef getSectionName() const
Definition: MCSectionELF.h:70
uint32_t LineOff
line offset in the .BTF string table
Definition: BTFDebug.h:225
uint32_t TypeID
Type ID.
Definition: BTFDebug.h:233
Basic type, like &#39;int&#39; or &#39;float&#39;.