LLVM  15.0.0git
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"
27 
28 using namespace llvm;
29 
30 static const char *BTFKindStr[] = {
31 #define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
32 #include "BTF.def"
33 };
34 
35 /// Emit a BTF common type.
37  OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
38  ")");
43 }
44 
46  bool NeedsFixup)
47  : DTy(DTy), NeedsFixup(NeedsFixup), Name(DTy->getName()) {
48  switch (Tag) {
49  case dwarf::DW_TAG_pointer_type:
50  Kind = BTF::BTF_KIND_PTR;
51  break;
52  case dwarf::DW_TAG_const_type:
53  Kind = BTF::BTF_KIND_CONST;
54  break;
55  case dwarf::DW_TAG_volatile_type:
56  Kind = BTF::BTF_KIND_VOLATILE;
57  break;
58  case dwarf::DW_TAG_typedef:
59  Kind = BTF::BTF_KIND_TYPEDEF;
60  break;
61  case dwarf::DW_TAG_restrict_type:
62  Kind = BTF::BTF_KIND_RESTRICT;
63  break;
64  default:
65  llvm_unreachable("Unknown DIDerivedType Tag");
66  }
67  BTFType.Info = Kind << 24;
68 }
69 
70 /// Used by DW_TAG_pointer_type only.
71 BTFTypeDerived::BTFTypeDerived(unsigned NextTypeId, unsigned Tag,
73  : DTy(nullptr), NeedsFixup(false), Name(Name) {
74  Kind = BTF::BTF_KIND_PTR;
75  BTFType.Info = Kind << 24;
76  BTFType.Type = NextTypeId;
77 }
78 
80  if (IsCompleted)
81  return;
82  IsCompleted = true;
83 
84  BTFType.NameOff = BDebug.addString(Name);
85 
86  if (NeedsFixup || !DTy)
87  return;
88 
89  // The base type for PTR/CONST/VOLATILE could be void.
90  const DIType *ResolvedType = DTy->getBaseType();
91  if (!ResolvedType) {
92  assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
93  Kind == BTF::BTF_KIND_VOLATILE) &&
94  "Invalid null basetype");
95  BTFType.Type = 0;
96  } else {
97  BTFType.Type = BDebug.getTypeId(ResolvedType);
98  }
99 }
100 
102 
104  BTFType.Type = PointeeType;
105 }
106 
107 /// Represent a struct/union forward declaration.
109  Kind = BTF::BTF_KIND_FWD;
110  BTFType.Info = IsUnion << 31 | Kind << 24;
111  BTFType.Type = 0;
112 }
113 
115  if (IsCompleted)
116  return;
117  IsCompleted = true;
118 
119  BTFType.NameOff = BDebug.addString(Name);
120 }
121 
123 
125  uint32_t OffsetInBits, StringRef TypeName)
126  : Name(TypeName) {
127  // Translate IR int encoding to BTF int encoding.
128  uint8_t BTFEncoding;
129  switch (Encoding) {
130  case dwarf::DW_ATE_boolean:
131  BTFEncoding = BTF::INT_BOOL;
132  break;
133  case dwarf::DW_ATE_signed:
134  case dwarf::DW_ATE_signed_char:
135  BTFEncoding = BTF::INT_SIGNED;
136  break;
137  case dwarf::DW_ATE_unsigned:
138  case dwarf::DW_ATE_unsigned_char:
139  BTFEncoding = 0;
140  break;
141  default:
142  llvm_unreachable("Unknown BTFTypeInt Encoding");
143  }
144 
145  Kind = BTF::BTF_KIND_INT;
146  BTFType.Info = Kind << 24;
147  BTFType.Size = roundupToBytes(SizeInBits);
148  IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
149 }
150 
152  if (IsCompleted)
153  return;
154  IsCompleted = true;
155 
156  BTFType.NameOff = BDebug.addString(Name);
157 }
158 
161  OS.AddComment("0x" + Twine::utohexstr(IntVal));
162  OS.emitInt32(IntVal);
163 }
164 
166  bool IsSigned) : ETy(ETy) {
167  Kind = BTF::BTF_KIND_ENUM;
168  BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
170 }
171 
173  if (IsCompleted)
174  return;
175  IsCompleted = true;
176 
177  BTFType.NameOff = BDebug.addString(ETy->getName());
178 
179  DINodeArray Elements = ETy->getElements();
180  for (const auto Element : Elements) {
181  const auto *Enum = cast<DIEnumerator>(Element);
182 
183  struct BTF::BTFEnum BTFEnum;
184  BTFEnum.NameOff = BDebug.addString(Enum->getName());
185  // BTF enum value is 32bit, enforce it.
186  uint32_t Value;
187  if (Enum->isUnsigned())
188  Value = static_cast<uint32_t>(Enum->getValue().getZExtValue());
189  else
190  Value = static_cast<uint32_t>(Enum->getValue().getSExtValue());
191  BTFEnum.Val = Value;
192  EnumValues.push_back(BTFEnum);
193  }
194 }
195 
198  for (const auto &Enum : EnumValues) {
199  OS.emitInt32(Enum.NameOff);
200  OS.emitInt32(Enum.Val);
201  }
202 }
203 
205  bool IsSigned) : ETy(ETy) {
206  Kind = BTF::BTF_KIND_ENUM64;
207  BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
209 }
210 
212  if (IsCompleted)
213  return;
214  IsCompleted = true;
215 
216  BTFType.NameOff = BDebug.addString(ETy->getName());
217 
218  DINodeArray Elements = ETy->getElements();
219  for (const auto Element : Elements) {
220  const auto *Enum = cast<DIEnumerator>(Element);
221 
222  struct BTF::BTFEnum64 BTFEnum;
223  BTFEnum.NameOff = BDebug.addString(Enum->getName());
224  uint64_t Value;
225  if (Enum->isUnsigned())
226  Value = static_cast<uint64_t>(Enum->getValue().getZExtValue());
227  else
228  Value = static_cast<uint64_t>(Enum->getValue().getSExtValue());
229  BTFEnum.Val_Lo32 = Value;
230  BTFEnum.Val_Hi32 = Value >> 32;
231  EnumValues.push_back(BTFEnum);
232  }
233 }
234 
237  for (const auto &Enum : EnumValues) {
238  OS.emitInt32(Enum.NameOff);
239  OS.AddComment("0x" + Twine::utohexstr(Enum.Val_Lo32));
240  OS.emitInt32(Enum.Val_Lo32);
241  OS.AddComment("0x" + Twine::utohexstr(Enum.Val_Hi32));
242  OS.emitInt32(Enum.Val_Hi32);
243  }
244 }
245 
247  Kind = BTF::BTF_KIND_ARRAY;
248  BTFType.NameOff = 0;
249  BTFType.Info = Kind << 24;
250  BTFType.Size = 0;
251 
252  ArrayInfo.ElemType = ElemTypeId;
253  ArrayInfo.Nelems = NumElems;
254 }
255 
256 /// Represent a BTF array.
258  if (IsCompleted)
259  return;
260  IsCompleted = true;
261 
262  // The IR does not really have a type for the index.
263  // A special type for array index should have been
264  // created during initial type traversal. Just
265  // retrieve that type id.
266  ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
267 }
268 
271  OS.emitInt32(ArrayInfo.ElemType);
272  OS.emitInt32(ArrayInfo.IndexType);
273  OS.emitInt32(ArrayInfo.Nelems);
274 }
275 
276 /// Represent either a struct or a union.
278  bool HasBitField, uint32_t Vlen)
279  : STy(STy), HasBitField(HasBitField) {
280  Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
282  BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
283 }
284 
286  if (IsCompleted)
287  return;
288  IsCompleted = true;
289 
290  BTFType.NameOff = BDebug.addString(STy->getName());
291 
292  // Add struct/union members.
293  const DINodeArray Elements = STy->getElements();
294  for (const auto *Element : Elements) {
295  struct BTF::BTFMember BTFMember;
296  const auto *DDTy = cast<DIDerivedType>(Element);
297 
298  BTFMember.NameOff = BDebug.addString(DDTy->getName());
299  if (HasBitField) {
300  uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
301  BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
302  } else {
303  BTFMember.Offset = DDTy->getOffsetInBits();
304  }
305  const auto *BaseTy = DDTy->getBaseType();
306  BTFMember.Type = BDebug.getTypeId(BaseTy);
307  Members.push_back(BTFMember);
308  }
309 }
310 
313  for (const auto &Member : Members) {
314  OS.emitInt32(Member.NameOff);
315  OS.emitInt32(Member.Type);
316  OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
317  OS.emitInt32(Member.Offset);
318  }
319 }
320 
321 std::string BTFTypeStruct::getName() { return std::string(STy->getName()); }
322 
323 /// The Func kind represents both subprogram and pointee of function
324 /// pointers. If the FuncName is empty, it represents a pointee of function
325 /// pointer. Otherwise, it represents a subprogram. The func arg names
326 /// are empty for pointee of function pointer case, and are valid names
327 /// for subprogram.
329  const DISubroutineType *STy, uint32_t VLen,
330  const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
331  : STy(STy), FuncArgNames(FuncArgNames) {
332  Kind = BTF::BTF_KIND_FUNC_PROTO;
333  BTFType.Info = (Kind << 24) | VLen;
334 }
335 
337  if (IsCompleted)
338  return;
339  IsCompleted = true;
340 
341  DITypeRefArray Elements = STy->getTypeArray();
342  auto RetType = Elements[0];
343  BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
344  BTFType.NameOff = 0;
345 
346  // For null parameter which is typically the last one
347  // to represent the vararg, encode the NameOff/Type to be 0.
348  for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
349  struct BTF::BTFParam Param;
350  auto Element = Elements[I];
351  if (Element) {
352  Param.NameOff = BDebug.addString(FuncArgNames[I]);
353  Param.Type = BDebug.getTypeId(Element);
354  } else {
355  Param.NameOff = 0;
356  Param.Type = 0;
357  }
358  Parameters.push_back(Param);
359  }
360 }
361 
364  for (const auto &Param : Parameters) {
365  OS.emitInt32(Param.NameOff);
366  OS.emitInt32(Param.Type);
367  }
368 }
369 
371  uint32_t Scope)
372  : Name(FuncName) {
373  Kind = BTF::BTF_KIND_FUNC;
374  BTFType.Info = (Kind << 24) | Scope;
375  BTFType.Type = ProtoTypeId;
376 }
377 
379  if (IsCompleted)
380  return;
381  IsCompleted = true;
382 
383  BTFType.NameOff = BDebug.addString(Name);
384 }
385 
387 
389  : Name(VarName) {
390  Kind = BTF::BTF_KIND_VAR;
391  BTFType.Info = Kind << 24;
392  BTFType.Type = TypeId;
393  Info = VarInfo;
394 }
395 
397  BTFType.NameOff = BDebug.addString(Name);
398 }
399 
402  OS.emitInt32(Info);
403 }
404 
405 BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
406  : Asm(AsmPrt), Name(SecName) {
407  Kind = BTF::BTF_KIND_DATASEC;
408  BTFType.Info = Kind << 24;
409  BTFType.Size = 0;
410 }
411 
413  BTFType.NameOff = BDebug.addString(Name);
414  BTFType.Info |= Vars.size();
415 }
416 
419 
420  for (const auto &V : Vars) {
421  OS.emitInt32(std::get<0>(V));
422  Asm->emitLabelReference(std::get<1>(V), 4);
423  OS.emitInt32(std::get<2>(V));
424  }
425 }
426 
428  : Name(TypeName) {
429  Kind = BTF::BTF_KIND_FLOAT;
430  BTFType.Info = Kind << 24;
431  BTFType.Size = roundupToBytes(SizeInBits);
432 }
433 
435  if (IsCompleted)
436  return;
437  IsCompleted = true;
438 
439  BTFType.NameOff = BDebug.addString(Name);
440 }
441 
442 BTFTypeDeclTag::BTFTypeDeclTag(uint32_t BaseTypeId, int ComponentIdx,
443  StringRef Tag)
444  : Tag(Tag) {
445  Kind = BTF::BTF_KIND_DECL_TAG;
446  BTFType.Info = Kind << 24;
447  BTFType.Type = BaseTypeId;
448  Info = ComponentIdx;
449 }
450 
452  if (IsCompleted)
453  return;
454  IsCompleted = true;
455 
456  BTFType.NameOff = BDebug.addString(Tag);
457 }
458 
461  OS.emitInt32(Info);
462 }
463 
465  : DTy(nullptr), Tag(Tag) {
466  Kind = BTF::BTF_KIND_TYPE_TAG;
467  BTFType.Info = Kind << 24;
468  BTFType.Type = NextTypeId;
469 }
470 
472  : DTy(DTy), Tag(Tag) {
473  Kind = BTF::BTF_KIND_TYPE_TAG;
474  BTFType.Info = Kind << 24;
475 }
476 
478  if (IsCompleted)
479  return;
480  IsCompleted = true;
481  BTFType.NameOff = BDebug.addString(Tag);
482  if (DTy) {
483  const DIType *ResolvedType = DTy->getBaseType();
484  if (!ResolvedType)
485  BTFType.Type = 0;
486  else
487  BTFType.Type = BDebug.getTypeId(ResolvedType);
488  }
489 }
490 
492  // Check whether the string already exists.
493  for (auto &OffsetM : OffsetToIdMap) {
494  if (Table[OffsetM.second] == S)
495  return OffsetM.first;
496  }
497  // Not find, add to the string table.
498  uint32_t Offset = Size;
499  OffsetToIdMap[Offset] = Table.size();
500  Table.push_back(std::string(S));
501  Size += S.size() + 1;
502  return Offset;
503 }
504 
506  : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
507  LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
508  MapDefNotCollected(true) {
509  addString("\0");
510 }
511 
512 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
513  const DIType *Ty) {
514  TypeEntry->setId(TypeEntries.size() + 1);
515  uint32_t Id = TypeEntry->getId();
516  DIToIdMap[Ty] = Id;
517  TypeEntries.push_back(std::move(TypeEntry));
518  return Id;
519 }
520 
521 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
522  TypeEntry->setId(TypeEntries.size() + 1);
523  uint32_t Id = TypeEntry->getId();
524  TypeEntries.push_back(std::move(TypeEntry));
525  return Id;
526 }
527 
528 void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
529  // Only int and binary floating point types are supported in BTF.
530  uint32_t Encoding = BTy->getEncoding();
531  std::unique_ptr<BTFTypeBase> TypeEntry;
532  switch (Encoding) {
533  case dwarf::DW_ATE_boolean:
534  case dwarf::DW_ATE_signed:
535  case dwarf::DW_ATE_signed_char:
536  case dwarf::DW_ATE_unsigned:
537  case dwarf::DW_ATE_unsigned_char:
538  // Create a BTF type instance for this DIBasicType and put it into
539  // DIToIdMap for cross-type reference check.
540  TypeEntry = std::make_unique<BTFTypeInt>(
541  Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
542  break;
543  case dwarf::DW_ATE_float:
544  TypeEntry =
545  std::make_unique<BTFTypeFloat>(BTy->getSizeInBits(), BTy->getName());
546  break;
547  default:
548  return;
549  }
550 
551  TypeId = addType(std::move(TypeEntry), BTy);
552 }
553 
554 /// Handle subprogram or subroutine types.
555 void BTFDebug::visitSubroutineType(
556  const DISubroutineType *STy, bool ForSubprog,
557  const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
558  uint32_t &TypeId) {
559  DITypeRefArray Elements = STy->getTypeArray();
560  uint32_t VLen = Elements.size() - 1;
561  if (VLen > BTF::MAX_VLEN)
562  return;
563 
564  // Subprogram has a valid non-zero-length name, and the pointee of
565  // a function pointer has an empty name. The subprogram type will
566  // not be added to DIToIdMap as it should not be referenced by
567  // any other types.
568  auto TypeEntry = std::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
569  if (ForSubprog)
570  TypeId = addType(std::move(TypeEntry)); // For subprogram
571  else
572  TypeId = addType(std::move(TypeEntry), STy); // For func ptr
573 
574  // Visit return type and func arg types.
575  for (const auto Element : Elements) {
576  visitTypeEntry(Element);
577  }
578 }
579 
580 void BTFDebug::processDeclAnnotations(DINodeArray Annotations,
581  uint32_t BaseTypeId,
582  int ComponentIdx) {
583  if (!Annotations)
584  return;
585 
586  for (const Metadata *Annotation : Annotations->operands()) {
587  const MDNode *MD = cast<MDNode>(Annotation);
588  const MDString *Name = cast<MDString>(MD->getOperand(0));
589  if (!Name->getString().equals("btf_decl_tag"))
590  continue;
591 
592  const MDString *Value = cast<MDString>(MD->getOperand(1));
593  auto TypeEntry = std::make_unique<BTFTypeDeclTag>(BaseTypeId, ComponentIdx,
594  Value->getString());
595  addType(std::move(TypeEntry));
596  }
597 }
598 
599 /// Generate btf_type_tag chains.
600 int BTFDebug::genBTFTypeTags(const DIDerivedType *DTy, int BaseTypeId) {
602  DINodeArray Annots = DTy->getAnnotations();
603  if (Annots) {
604  // For type with "int __tag1 __tag2 *p", the MDStrs will have
605  // content: [__tag1, __tag2].
606  for (const Metadata *Annotations : Annots->operands()) {
607  const MDNode *MD = cast<MDNode>(Annotations);
608  const MDString *Name = cast<MDString>(MD->getOperand(0));
609  if (!Name->getString().equals("btf_type_tag"))
610  continue;
611  MDStrs.push_back(cast<MDString>(MD->getOperand(1)));
612  }
613  }
614 
615  if (MDStrs.size() == 0)
616  return -1;
617 
618  // With MDStrs [__tag1, __tag2], the output type chain looks like
619  // PTR -> __tag2 -> __tag1 -> BaseType
620  // In the below, we construct BTF types with the order of __tag1, __tag2
621  // and PTR.
622  unsigned TmpTypeId;
623  std::unique_ptr<BTFTypeTypeTag> TypeEntry;
624  if (BaseTypeId >= 0)
625  TypeEntry =
626  std::make_unique<BTFTypeTypeTag>(BaseTypeId, MDStrs[0]->getString());
627  else
628  TypeEntry = std::make_unique<BTFTypeTypeTag>(DTy, MDStrs[0]->getString());
629  TmpTypeId = addType(std::move(TypeEntry));
630 
631  for (unsigned I = 1; I < MDStrs.size(); I++) {
632  const MDString *Value = MDStrs[I];
633  TypeEntry = std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString());
634  TmpTypeId = addType(std::move(TypeEntry));
635  }
636  return TmpTypeId;
637 }
638 
639 /// Handle structure/union types.
640 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
641  uint32_t &TypeId) {
642  const DINodeArray Elements = CTy->getElements();
643  uint32_t VLen = Elements.size();
644  if (VLen > BTF::MAX_VLEN)
645  return;
646 
647  // Check whether we have any bitfield members or not
648  bool HasBitField = false;
649  for (const auto *Element : Elements) {
650  auto E = cast<DIDerivedType>(Element);
651  if (E->isBitField()) {
652  HasBitField = true;
653  break;
654  }
655  }
656 
657  auto TypeEntry =
658  std::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
659  StructTypes.push_back(TypeEntry.get());
660  TypeId = addType(std::move(TypeEntry), CTy);
661 
662  // Check struct/union annotations
663  processDeclAnnotations(CTy->getAnnotations(), TypeId, -1);
664 
665  // Visit all struct members.
666  int FieldNo = 0;
667  for (const auto *Element : Elements) {
668  const auto Elem = cast<DIDerivedType>(Element);
669  visitTypeEntry(Elem);
670  processDeclAnnotations(Elem->getAnnotations(), TypeId, FieldNo);
671  FieldNo++;
672  }
673 }
674 
675 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
676  // Visit array element type.
677  uint32_t ElemTypeId;
678  const DIType *ElemType = CTy->getBaseType();
679  visitTypeEntry(ElemType, ElemTypeId, false, false);
680 
681  // Visit array dimensions.
682  DINodeArray Elements = CTy->getElements();
683  for (int I = Elements.size() - 1; I >= 0; --I) {
684  if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
685  if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
686  const DISubrange *SR = cast<DISubrange>(Element);
687  auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
688  int64_t Count = CI->getSExtValue();
689 
690  // For struct s { int b; char c[]; }, the c[] will be represented
691  // as an array with Count = -1.
692  auto TypeEntry =
693  std::make_unique<BTFTypeArray>(ElemTypeId,
694  Count >= 0 ? Count : 0);
695  if (I == 0)
696  ElemTypeId = addType(std::move(TypeEntry), CTy);
697  else
698  ElemTypeId = addType(std::move(TypeEntry));
699  }
700  }
701 
702  // The array TypeId is the type id of the outermost dimension.
703  TypeId = ElemTypeId;
704 
705  // The IR does not have a type for array index while BTF wants one.
706  // So create an array index type if there is none.
707  if (!ArrayIndexTypeId) {
708  auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
709  0, "__ARRAY_SIZE_TYPE__");
710  ArrayIndexTypeId = addType(std::move(TypeEntry));
711  }
712 }
713 
714 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
715  DINodeArray Elements = CTy->getElements();
716  uint32_t VLen = Elements.size();
717  if (VLen > BTF::MAX_VLEN)
718  return;
719 
720  bool IsSigned = false;
721  unsigned NumBits = 32;
722  // No BaseType implies forward declaration in which case a
723  // BTFTypeEnum with Vlen = 0 is emitted.
724  if (CTy->getBaseType() != nullptr) {
725  const auto *BTy = cast<DIBasicType>(CTy->getBaseType());
726  IsSigned = BTy->getEncoding() == dwarf::DW_ATE_signed ||
727  BTy->getEncoding() == dwarf::DW_ATE_signed_char;
728  NumBits = BTy->getSizeInBits();
729  }
730 
731  if (NumBits <= 32) {
732  auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen, IsSigned);
733  TypeId = addType(std::move(TypeEntry), CTy);
734  } else {
735  assert(NumBits == 64);
736  auto TypeEntry = std::make_unique<BTFTypeEnum64>(CTy, VLen, IsSigned);
737  TypeId = addType(std::move(TypeEntry), CTy);
738  }
739  // No need to visit base type as BTF does not encode it.
740 }
741 
742 /// Handle structure/union forward declarations.
743 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
744  uint32_t &TypeId) {
745  auto TypeEntry = std::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
746  TypeId = addType(std::move(TypeEntry), CTy);
747 }
748 
749 /// Handle structure, union, array and enumeration types.
750 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
751  uint32_t &TypeId) {
752  auto Tag = CTy->getTag();
753  if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
754  // Handle forward declaration differently as it does not have members.
755  if (CTy->isForwardDecl())
756  visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
757  else
758  visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
759  } else if (Tag == dwarf::DW_TAG_array_type)
760  visitArrayType(CTy, TypeId);
761  else if (Tag == dwarf::DW_TAG_enumeration_type)
762  visitEnumType(CTy, TypeId);
763 }
764 
765 /// Handle pointer, typedef, const, volatile, restrict and member types.
766 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
767  bool CheckPointer, bool SeenPointer) {
768  unsigned Tag = DTy->getTag();
769 
770  /// Try to avoid chasing pointees, esp. structure pointees which may
771  /// unnecessary bring in a lot of types.
772  if (CheckPointer && !SeenPointer) {
773  SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
774  }
775 
776  if (CheckPointer && SeenPointer) {
777  const DIType *Base = DTy->getBaseType();
778  if (Base) {
779  if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
780  auto CTag = CTy->getTag();
781  if ((CTag == dwarf::DW_TAG_structure_type ||
782  CTag == dwarf::DW_TAG_union_type) &&
783  !CTy->getName().empty() && !CTy->isForwardDecl()) {
784  /// Find a candidate, generate a fixup. Later on the struct/union
785  /// pointee type will be replaced with either a real type or
786  /// a forward declaration.
787  auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true);
788  auto &Fixup = FixupDerivedTypes[CTy];
789  Fixup.push_back(std::make_pair(DTy, TypeEntry.get()));
790  TypeId = addType(std::move(TypeEntry), DTy);
791  return;
792  }
793  }
794  }
795  }
796 
797  if (Tag == dwarf::DW_TAG_pointer_type) {
798  int TmpTypeId = genBTFTypeTags(DTy, -1);
799  if (TmpTypeId >= 0) {
800  auto TypeDEntry =
801  std::make_unique<BTFTypeDerived>(TmpTypeId, Tag, DTy->getName());
802  TypeId = addType(std::move(TypeDEntry), DTy);
803  } else {
804  auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
805  TypeId = addType(std::move(TypeEntry), DTy);
806  }
807  } else if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
808  Tag == dwarf::DW_TAG_volatile_type ||
809  Tag == dwarf::DW_TAG_restrict_type) {
810  auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
811  TypeId = addType(std::move(TypeEntry), DTy);
812  if (Tag == dwarf::DW_TAG_typedef)
813  processDeclAnnotations(DTy->getAnnotations(), TypeId, -1);
814  } else if (Tag != dwarf::DW_TAG_member) {
815  return;
816  }
817 
818  // Visit base type of pointer, typedef, const, volatile, restrict or
819  // struct/union member.
820  uint32_t TempTypeId = 0;
821  if (Tag == dwarf::DW_TAG_member)
822  visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
823  else
824  visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
825 }
826 
827 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
828  bool CheckPointer, bool SeenPointer) {
829  if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
830  TypeId = DIToIdMap[Ty];
831 
832  // To handle the case like the following:
833  // struct t;
834  // typedef struct t _t;
835  // struct s1 { _t *c; };
836  // int test1(struct s1 *arg) { ... }
837  //
838  // struct t { int a; int b; };
839  // struct s2 { _t c; }
840  // int test2(struct s2 *arg) { ... }
841  //
842  // During traversing test1() argument, "_t" is recorded
843  // in DIToIdMap and a forward declaration fixup is created
844  // for "struct t" to avoid pointee type traversal.
845  //
846  // During traversing test2() argument, even if we see "_t" is
847  // already defined, we should keep moving to eventually
848  // bring in types for "struct t". Otherwise, the "struct s2"
849  // definition won't be correct.
850  //
851  // In the above, we have following debuginfo:
852  // {ptr, struct_member} -> typedef -> struct
853  // and BTF type for 'typedef' is generated while 'struct' may
854  // be in FixUp. But let us generalize the above to handle
855  // {different types} -> [various derived types]+ -> another type.
856  // For example,
857  // {func_param, struct_member} -> const -> ptr -> volatile -> struct
858  // We will traverse const/ptr/volatile which already have corresponding
859  // BTF types and generate type for 'struct' which might be in Fixup
860  // state.
861  if (Ty && (!CheckPointer || !SeenPointer)) {
862  if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
863  while (DTy) {
864  const DIType *BaseTy = DTy->getBaseType();
865  if (!BaseTy)
866  break;
867 
868  if (DIToIdMap.find(BaseTy) != DIToIdMap.end()) {
869  DTy = dyn_cast<DIDerivedType>(BaseTy);
870  } else {
871  uint32_t TmpTypeId;
872  visitTypeEntry(BaseTy, TmpTypeId, CheckPointer, SeenPointer);
873  break;
874  }
875  }
876  }
877  }
878 
879  return;
880  }
881 
882  if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
883  visitBasicType(BTy, TypeId);
884  else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
885  visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
886  TypeId);
887  else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
888  visitCompositeType(CTy, TypeId);
889  else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
890  visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
891  else
892  llvm_unreachable("Unknown DIType");
893 }
894 
895 void BTFDebug::visitTypeEntry(const DIType *Ty) {
896  uint32_t TypeId;
897  visitTypeEntry(Ty, TypeId, false, false);
898 }
899 
900 void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
901  if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
902  TypeId = DIToIdMap[Ty];
903  return;
904  }
905 
906  // MapDef type may be a struct type or a non-pointer derived type
907  const DIType *OrigTy = Ty;
908  while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
909  auto Tag = DTy->getTag();
910  if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
911  Tag != dwarf::DW_TAG_volatile_type &&
912  Tag != dwarf::DW_TAG_restrict_type)
913  break;
914  Ty = DTy->getBaseType();
915  }
916 
917  const auto *CTy = dyn_cast<DICompositeType>(Ty);
918  if (!CTy)
919  return;
920 
921  auto Tag = CTy->getTag();
922  if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
923  return;
924 
925  // Visit all struct members to ensure pointee type is visited
926  const DINodeArray Elements = CTy->getElements();
927  for (const auto *Element : Elements) {
928  const auto *MemberType = cast<DIDerivedType>(Element);
929  visitTypeEntry(MemberType->getBaseType());
930  }
931 
932  // Visit this type, struct or a const/typedef/volatile/restrict type
933  visitTypeEntry(OrigTy, TypeId, false, false);
934 }
935 
936 /// Read file contents from the actual file or from the source
937 std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
938  auto File = SP->getFile();
939  std::string FileName;
940 
941  if (!File->getFilename().startswith("/") && File->getDirectory().size())
942  FileName = File->getDirectory().str() + "/" + File->getFilename().str();
943  else
944  FileName = std::string(File->getFilename());
945 
946  // No need to populate the contends if it has been populated!
947  if (FileContent.find(FileName) != FileContent.end())
948  return FileName;
949 
950  std::vector<std::string> Content;
951  std::string Line;
952  Content.push_back(Line); // Line 0 for empty string
953 
954  std::unique_ptr<MemoryBuffer> Buf;
955  auto Source = File->getSource();
956  if (Source)
958  else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
959  MemoryBuffer::getFile(FileName))
960  Buf = std::move(*BufOrErr);
961  if (Buf)
962  for (line_iterator I(*Buf, false), E; I != E; ++I)
963  Content.push_back(std::string(*I));
964 
965  FileContent[FileName] = Content;
966  return FileName;
967 }
968 
969 void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
970  uint32_t Line, uint32_t Column) {
971  std::string FileName = populateFileContent(SP);
972  BTFLineInfo LineInfo;
973 
974  LineInfo.Label = Label;
975  LineInfo.FileNameOff = addString(FileName);
976  // If file content is not available, let LineOff = 0.
977  if (Line < FileContent[FileName].size())
978  LineInfo.LineOff = addString(FileContent[FileName][Line]);
979  else
980  LineInfo.LineOff = 0;
981  LineInfo.LineNum = Line;
982  LineInfo.ColumnNum = Column;
983  LineInfoTable[SecNameOff].push_back(LineInfo);
984 }
985 
986 void BTFDebug::emitCommonHeader() {
988  OS.emitIntValue(BTF::MAGIC, 2);
990  OS.emitInt8(0);
991 }
992 
993 void BTFDebug::emitBTFSection() {
994  // Do not emit section if no types and only "" string.
995  if (!TypeEntries.size() && StringTable.getSize() == 1)
996  return;
997 
998  MCContext &Ctx = OS.getContext();
999  MCSectionELF *Sec = Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0);
1000  Sec->setAlignment(Align(4));
1001  OS.switchSection(Sec);
1002 
1003  // Emit header.
1004  emitCommonHeader();
1006 
1007  uint32_t TypeLen = 0, StrLen;
1008  for (const auto &TypeEntry : TypeEntries)
1009  TypeLen += TypeEntry->getSize();
1010  StrLen = StringTable.getSize();
1011 
1012  OS.emitInt32(0);
1013  OS.emitInt32(TypeLen);
1014  OS.emitInt32(TypeLen);
1015  OS.emitInt32(StrLen);
1016 
1017  // Emit type table.
1018  for (const auto &TypeEntry : TypeEntries)
1019  TypeEntry->emitType(OS);
1020 
1021  // Emit string table.
1022  uint32_t StringOffset = 0;
1023  for (const auto &S : StringTable.getTable()) {
1024  OS.AddComment("string offset=" + std::to_string(StringOffset));
1025  OS.emitBytes(S);
1026  OS.emitBytes(StringRef("\0", 1));
1027  StringOffset += S.size() + 1;
1028  }
1029 }
1030 
1031 void BTFDebug::emitBTFExtSection() {
1032  // Do not emit section if empty FuncInfoTable and LineInfoTable
1033  // and FieldRelocTable.
1034  if (!FuncInfoTable.size() && !LineInfoTable.size() &&
1035  !FieldRelocTable.size())
1036  return;
1037 
1038  MCContext &Ctx = OS.getContext();
1039  MCSectionELF *Sec = Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0);
1040  Sec->setAlignment(Align(4));
1041  OS.switchSection(Sec);
1042 
1043  // Emit header.
1044  emitCommonHeader();
1046 
1047  // Account for FuncInfo/LineInfo record size as well.
1048  uint32_t FuncLen = 4, LineLen = 4;
1049  // Do not account for optional FieldReloc.
1050  uint32_t FieldRelocLen = 0;
1051  for (const auto &FuncSec : FuncInfoTable) {
1052  FuncLen += BTF::SecFuncInfoSize;
1053  FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
1054  }
1055  for (const auto &LineSec : LineInfoTable) {
1056  LineLen += BTF::SecLineInfoSize;
1057  LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
1058  }
1059  for (const auto &FieldRelocSec : FieldRelocTable) {
1060  FieldRelocLen += BTF::SecFieldRelocSize;
1061  FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize;
1062  }
1063 
1064  if (FieldRelocLen)
1065  FieldRelocLen += 4;
1066 
1067  OS.emitInt32(0);
1068  OS.emitInt32(FuncLen);
1069  OS.emitInt32(FuncLen);
1070  OS.emitInt32(LineLen);
1071  OS.emitInt32(FuncLen + LineLen);
1072  OS.emitInt32(FieldRelocLen);
1073 
1074  // Emit func_info table.
1075  OS.AddComment("FuncInfo");
1077  for (const auto &FuncSec : FuncInfoTable) {
1078  OS.AddComment("FuncInfo section string offset=" +
1079  std::to_string(FuncSec.first));
1080  OS.emitInt32(FuncSec.first);
1081  OS.emitInt32(FuncSec.second.size());
1082  for (const auto &FuncInfo : FuncSec.second) {
1083  Asm->emitLabelReference(FuncInfo.Label, 4);
1084  OS.emitInt32(FuncInfo.TypeId);
1085  }
1086  }
1087 
1088  // Emit line_info table.
1089  OS.AddComment("LineInfo");
1091  for (const auto &LineSec : LineInfoTable) {
1092  OS.AddComment("LineInfo section string offset=" +
1093  std::to_string(LineSec.first));
1094  OS.emitInt32(LineSec.first);
1095  OS.emitInt32(LineSec.second.size());
1096  for (const auto &LineInfo : LineSec.second) {
1097  Asm->emitLabelReference(LineInfo.Label, 4);
1098  OS.emitInt32(LineInfo.FileNameOff);
1099  OS.emitInt32(LineInfo.LineOff);
1100  OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
1101  std::to_string(LineInfo.ColumnNum));
1102  OS.emitInt32(LineInfo.LineNum << 10 | LineInfo.ColumnNum);
1103  }
1104  }
1105 
1106  // Emit field reloc table.
1107  if (FieldRelocLen) {
1108  OS.AddComment("FieldReloc");
1110  for (const auto &FieldRelocSec : FieldRelocTable) {
1111  OS.AddComment("Field reloc section string offset=" +
1112  std::to_string(FieldRelocSec.first));
1113  OS.emitInt32(FieldRelocSec.first);
1114  OS.emitInt32(FieldRelocSec.second.size());
1115  for (const auto &FieldRelocInfo : FieldRelocSec.second) {
1116  Asm->emitLabelReference(FieldRelocInfo.Label, 4);
1117  OS.emitInt32(FieldRelocInfo.TypeID);
1118  OS.emitInt32(FieldRelocInfo.OffsetNameOff);
1119  OS.emitInt32(FieldRelocInfo.RelocKind);
1120  }
1121  }
1122  }
1123 }
1124 
1126  auto *SP = MF->getFunction().getSubprogram();
1127  auto *Unit = SP->getUnit();
1128 
1129  if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
1130  SkipInstruction = true;
1131  return;
1132  }
1133  SkipInstruction = false;
1134 
1135  // Collect MapDef types. Map definition needs to collect
1136  // pointee types. Do it first. Otherwise, for the following
1137  // case:
1138  // struct m { ...};
1139  // struct t {
1140  // struct m *key;
1141  // };
1142  // foo(struct t *arg);
1143  //
1144  // struct mapdef {
1145  // ...
1146  // struct m *key;
1147  // ...
1148  // } __attribute__((section(".maps"))) hash_map;
1149  //
1150  // If subroutine foo is traversed first, a type chain
1151  // "ptr->struct m(fwd)" will be created and later on
1152  // when traversing mapdef, since "ptr->struct m" exists,
1153  // the traversal of "struct m" will be omitted.
1154  if (MapDefNotCollected) {
1155  processGlobals(true);
1156  MapDefNotCollected = false;
1157  }
1158 
1159  // Collect all types locally referenced in this function.
1160  // Use RetainedNodes so we can collect all argument names
1161  // even if the argument is not used.
1162  std::unordered_map<uint32_t, StringRef> FuncArgNames;
1163  for (const DINode *DN : SP->getRetainedNodes()) {
1164  if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
1165  // Collect function arguments for subprogram func type.
1166  uint32_t Arg = DV->getArg();
1167  if (Arg) {
1168  visitTypeEntry(DV->getType());
1169  FuncArgNames[Arg] = DV->getName();
1170  }
1171  }
1172  }
1173 
1174  // Construct subprogram func proto type.
1175  uint32_t ProtoTypeId;
1176  visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
1177 
1178  // Construct subprogram func type
1179  uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL;
1180  auto FuncTypeEntry =
1181  std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
1182  uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
1183 
1184  // Process argument annotations.
1185  for (const DINode *DN : SP->getRetainedNodes()) {
1186  if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
1187  uint32_t Arg = DV->getArg();
1188  if (Arg)
1189  processDeclAnnotations(DV->getAnnotations(), FuncTypeId, Arg - 1);
1190  }
1191  }
1192 
1193  processDeclAnnotations(SP->getAnnotations(), FuncTypeId, -1);
1194 
1195  for (const auto &TypeEntry : TypeEntries)
1196  TypeEntry->completeType(*this);
1197 
1198  // Construct funcinfo and the first lineinfo for the function.
1199  MCSymbol *FuncLabel = Asm->getFunctionBegin();
1200  BTFFuncInfo FuncInfo;
1201  FuncInfo.Label = FuncLabel;
1202  FuncInfo.TypeId = FuncTypeId;
1203  if (FuncLabel->isInSection()) {
1204  MCSection &Section = FuncLabel->getSection();
1205  const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
1206  assert(SectionELF && "Null section for Function Label");
1207  SecNameOff = addString(SectionELF->getName());
1208  } else {
1209  SecNameOff = addString(".text");
1210  }
1211  FuncInfoTable[SecNameOff].push_back(FuncInfo);
1212 }
1213 
1215  SkipInstruction = false;
1216  LineInfoGenerated = false;
1217  SecNameOff = 0;
1218 }
1219 
1220 /// On-demand populate types as requested from abstract member
1221 /// accessing or preserve debuginfo type.
1222 unsigned BTFDebug::populateType(const DIType *Ty) {
1223  unsigned Id;
1224  visitTypeEntry(Ty, Id, false, false);
1225  for (const auto &TypeEntry : TypeEntries)
1226  TypeEntry->completeType(*this);
1227  return Id;
1228 }
1229 
1230 /// Generate a struct member field relocation.
1231 void BTFDebug::generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
1232  const GlobalVariable *GVar, bool IsAma) {
1233  BTFFieldReloc FieldReloc;
1234  FieldReloc.Label = ORSym;
1235  FieldReloc.TypeID = RootId;
1236 
1237  StringRef AccessPattern = GVar->getName();
1238  size_t FirstDollar = AccessPattern.find_first_of('$');
1239  if (IsAma) {
1240  size_t FirstColon = AccessPattern.find_first_of(':');
1241  size_t SecondColon = AccessPattern.find_first_of(':', FirstColon + 1);
1242  StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1);
1243  StringRef RelocKindStr = AccessPattern.substr(FirstColon + 1,
1244  SecondColon - FirstColon);
1245  StringRef PatchImmStr = AccessPattern.substr(SecondColon + 1,
1246  FirstDollar - SecondColon);
1247 
1248  FieldReloc.OffsetNameOff = addString(IndexPattern);
1249  FieldReloc.RelocKind = std::stoull(std::string(RelocKindStr));
1250  PatchImms[GVar] = std::make_pair(std::stoll(std::string(PatchImmStr)),
1251  FieldReloc.RelocKind);
1252  } else {
1253  StringRef RelocStr = AccessPattern.substr(FirstDollar + 1);
1254  FieldReloc.OffsetNameOff = addString("0");
1255  FieldReloc.RelocKind = std::stoull(std::string(RelocStr));
1256  PatchImms[GVar] = std::make_pair(RootId, FieldReloc.RelocKind);
1257  }
1258  FieldRelocTable[SecNameOff].push_back(FieldReloc);
1259 }
1260 
1261 void BTFDebug::processGlobalValue(const MachineOperand &MO) {
1262  // check whether this is a candidate or not
1263  if (MO.isGlobal()) {
1264  const GlobalValue *GVal = MO.getGlobal();
1265  auto *GVar = dyn_cast<GlobalVariable>(GVal);
1266  if (!GVar) {
1267  // Not a global variable. Maybe an extern function reference.
1268  processFuncPrototypes(dyn_cast<Function>(GVal));
1269  return;
1270  }
1271 
1274  return;
1275 
1276  MCSymbol *ORSym = OS.getContext().createTempSymbol();
1277  OS.emitLabel(ORSym);
1278 
1279  MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1280  uint32_t RootId = populateType(dyn_cast<DIType>(MDN));
1281  generatePatchImmReloc(ORSym, RootId, GVar,
1283  }
1284 }
1285 
1288 
1289  if (SkipInstruction || MI->isMetaInstruction() ||
1290  MI->getFlag(MachineInstr::FrameSetup))
1291  return;
1292 
1293  if (MI->isInlineAsm()) {
1294  // Count the number of register definitions to find the asm string.
1295  unsigned NumDefs = 0;
1296  for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
1297  ++NumDefs)
1298  ;
1299 
1300  // Skip this inline asm instruction if the asmstr is empty.
1301  const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1302  if (AsmStr[0] == 0)
1303  return;
1304  }
1305 
1306  if (MI->getOpcode() == BPF::LD_imm64) {
1307  // If the insn is "r2 = LD_imm64 @<an AmaAttr global>",
1308  // add this insn into the .BTF.ext FieldReloc subsection.
1309  // Relocation looks like:
1310  // . SecName:
1311  // . InstOffset
1312  // . TypeID
1313  // . OffSetNameOff
1314  // . RelocType
1315  // Later, the insn is replaced with "r2 = <offset>"
1316  // where "<offset>" equals to the offset based on current
1317  // type definitions.
1318  //
1319  // If the insn is "r2 = LD_imm64 @<an TypeIdAttr global>",
1320  // The LD_imm64 result will be replaced with a btf type id.
1321  processGlobalValue(MI->getOperand(1));
1322  } else if (MI->getOpcode() == BPF::CORE_MEM ||
1323  MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1324  MI->getOpcode() == BPF::CORE_SHIFT) {
1325  // relocation insn is a load, store or shift insn.
1326  processGlobalValue(MI->getOperand(3));
1327  } else if (MI->getOpcode() == BPF::JAL) {
1328  // check extern function references
1329  const MachineOperand &MO = MI->getOperand(0);
1330  if (MO.isGlobal()) {
1331  processFuncPrototypes(dyn_cast<Function>(MO.getGlobal()));
1332  }
1333  }
1334 
1335  if (!CurMI) // no debug info
1336  return;
1337 
1338  // Skip this instruction if no DebugLoc or the DebugLoc
1339  // is the same as the previous instruction.
1340  const DebugLoc &DL = MI->getDebugLoc();
1341  if (!DL || PrevInstLoc == DL) {
1342  // This instruction will be skipped, no LineInfo has
1343  // been generated, construct one based on function signature.
1344  if (LineInfoGenerated == false) {
1345  auto *S = MI->getMF()->getFunction().getSubprogram();
1346  MCSymbol *FuncLabel = Asm->getFunctionBegin();
1347  constructLineInfo(S, FuncLabel, S->getLine(), 0);
1348  LineInfoGenerated = true;
1349  }
1350 
1351  return;
1352  }
1353 
1354  // Create a temporary label to remember the insn for lineinfo.
1355  MCSymbol *LineSym = OS.getContext().createTempSymbol();
1356  OS.emitLabel(LineSym);
1357 
1358  // Construct the lineinfo.
1359  auto SP = DL.get()->getScope()->getSubprogram();
1360  constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
1361 
1362  LineInfoGenerated = true;
1363  PrevInstLoc = DL;
1364 }
1365 
1366 void BTFDebug::processGlobals(bool ProcessingMapDef) {
1367  // Collect all types referenced by globals.
1368  const Module *M = MMI->getModule();
1369  for (const GlobalVariable &Global : M->globals()) {
1370  // Decide the section name.
1371  StringRef SecName;
1372  if (Global.hasSection()) {
1373  SecName = Global.getSection();
1374  } else if (Global.hasInitializer()) {
1375  // data, bss, or readonly sections
1376  if (Global.isConstant())
1377  SecName = ".rodata";
1378  else
1379  SecName = Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
1380  }
1381 
1382  if (ProcessingMapDef != SecName.startswith(".maps"))
1383  continue;
1384 
1385  // Create a .rodata datasec if the global variable is an initialized
1386  // constant with private linkage and if it won't be in .rodata.str<#>
1387  // and .rodata.cst<#> sections.
1388  if (SecName == ".rodata" && Global.hasPrivateLinkage() &&
1389  DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1390  SectionKind GVKind =
1392  // skip .rodata.str<#> and .rodata.cst<#> sections
1393  if (!GVKind.isMergeableCString() && !GVKind.isMergeableConst()) {
1394  DataSecEntries[std::string(SecName)] =
1395  std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1396  }
1397  }
1398 
1400  Global.getDebugInfo(GVs);
1401 
1402  // No type information, mostly internal, skip it.
1403  if (GVs.size() == 0)
1404  continue;
1405 
1406  uint32_t GVTypeId = 0;
1407  DIGlobalVariable *DIGlobal = nullptr;
1408  for (auto *GVE : GVs) {
1409  DIGlobal = GVE->getVariable();
1410  if (SecName.startswith(".maps"))
1411  visitMapDefType(DIGlobal->getType(), GVTypeId);
1412  else
1413  visitTypeEntry(DIGlobal->getType(), GVTypeId, false, false);
1414  break;
1415  }
1416 
1417  // Only support the following globals:
1418  // . static variables
1419  // . non-static weak or non-weak global variables
1420  // . weak or non-weak extern global variables
1421  // Whether DataSec is readonly or not can be found from corresponding ELF
1422  // section flags. Whether a BTF_KIND_VAR is a weak symbol or not
1423  // can be found from the corresponding ELF symbol table.
1424  auto Linkage = Global.getLinkage();
1430  continue;
1431 
1432  uint32_t GVarInfo;
1434  GVarInfo = BTF::VAR_STATIC;
1435  } else if (Global.hasInitializer()) {
1436  GVarInfo = BTF::VAR_GLOBAL_ALLOCATED;
1437  } else {
1438  GVarInfo = BTF::VAR_GLOBAL_EXTERNAL;
1439  }
1440 
1441  auto VarEntry =
1442  std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1443  uint32_t VarId = addType(std::move(VarEntry));
1444 
1445  processDeclAnnotations(DIGlobal->getAnnotations(), VarId, -1);
1446 
1447  // An empty SecName means an extern variable without section attribute.
1448  if (SecName.empty())
1449  continue;
1450 
1451  // Find or create a DataSec
1452  if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1453  DataSecEntries[std::string(SecName)] =
1454  std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1455  }
1456 
1457  // Calculate symbol size
1458  const DataLayout &DL = Global.getParent()->getDataLayout();
1459  uint32_t Size = DL.getTypeAllocSize(Global.getValueType());
1460 
1461  DataSecEntries[std::string(SecName)]->addDataSecEntry(VarId,
1462  Asm->getSymbol(&Global), Size);
1463  }
1464 }
1465 
1466 /// Emit proper patchable instructions.
1468  if (MI->getOpcode() == BPF::LD_imm64) {
1469  const MachineOperand &MO = MI->getOperand(1);
1470  if (MO.isGlobal()) {
1471  const GlobalValue *GVal = MO.getGlobal();
1472  auto *GVar = dyn_cast<GlobalVariable>(GVal);
1473  if (GVar) {
1474  // Emit "mov ri, <imm>"
1475  int64_t Imm;
1476  uint32_t Reloc;
1479  Imm = PatchImms[GVar].first;
1480  Reloc = PatchImms[GVar].second;
1481  } else {
1482  return false;
1483  }
1484 
1486  Reloc == BPFCoreSharedInfo::ENUM_VALUE ||
1489  OutMI.setOpcode(BPF::LD_imm64);
1490  else
1491  OutMI.setOpcode(BPF::MOV_ri);
1492  OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1494  return true;
1495  }
1496  }
1497  } else if (MI->getOpcode() == BPF::CORE_MEM ||
1498  MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1499  MI->getOpcode() == BPF::CORE_SHIFT) {
1500  const MachineOperand &MO = MI->getOperand(3);
1501  if (MO.isGlobal()) {
1502  const GlobalValue *GVal = MO.getGlobal();
1503  auto *GVar = dyn_cast<GlobalVariable>(GVal);
1504  if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1505  uint32_t Imm = PatchImms[GVar].first;
1506  OutMI.setOpcode(MI->getOperand(1).getImm());
1507  if (MI->getOperand(0).isImm())
1508  OutMI.addOperand(MCOperand::createImm(MI->getOperand(0).getImm()));
1509  else
1510  OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1511  OutMI.addOperand(MCOperand::createReg(MI->getOperand(2).getReg()));
1513  return true;
1514  }
1515  }
1516  }
1517  return false;
1518 }
1519 
1520 void BTFDebug::processFuncPrototypes(const Function *F) {
1521  if (!F)
1522  return;
1523 
1524  const DISubprogram *SP = F->getSubprogram();
1525  if (!SP || SP->isDefinition())
1526  return;
1527 
1528  // Do not emit again if already emitted.
1529  if (!ProtoFunctions.insert(F).second)
1530  return;
1531 
1532  uint32_t ProtoTypeId;
1533  const std::unordered_map<uint32_t, StringRef> FuncArgNames;
1534  visitSubroutineType(SP->getType(), false, FuncArgNames, ProtoTypeId);
1535 
1536  uint8_t Scope = BTF::FUNC_EXTERN;
1537  auto FuncTypeEntry =
1538  std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
1539  uint32_t FuncId = addType(std::move(FuncTypeEntry));
1540 
1541  processDeclAnnotations(SP->getAnnotations(), FuncId, -1);
1542 
1543  if (F->hasSection()) {
1544  StringRef SecName = F->getSection();
1545 
1546  if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1547  DataSecEntries[std::string(SecName)] =
1548  std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1549  }
1550 
1551  // We really don't know func size, set it to 0.
1552  DataSecEntries[std::string(SecName)]->addDataSecEntry(FuncId,
1553  Asm->getSymbol(F), 0);
1554  }
1555 }
1556 
1558  // Collect MapDef globals if not collected yet.
1559  if (MapDefNotCollected) {
1560  processGlobals(true);
1561  MapDefNotCollected = false;
1562  }
1563 
1564  // Collect global types/variables except MapDef globals.
1565  processGlobals(false);
1566 
1567  for (auto &DataSec : DataSecEntries)
1568  addType(std::move(DataSec.second));
1569 
1570  // Fixups
1571  for (auto &Fixup : FixupDerivedTypes) {
1572  const DICompositeType *CTy = Fixup.first;
1573  StringRef TypeName = CTy->getName();
1574  bool IsUnion = CTy->getTag() == dwarf::DW_TAG_union_type;
1575 
1576  // Search through struct types
1577  uint32_t StructTypeId = 0;
1578  for (const auto &StructType : StructTypes) {
1579  if (StructType->getName() == TypeName) {
1580  StructTypeId = StructType->getId();
1581  break;
1582  }
1583  }
1584 
1585  if (StructTypeId == 0) {
1586  auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
1587  StructTypeId = addType(std::move(FwdTypeEntry));
1588  }
1589 
1590  for (auto &TypeInfo : Fixup.second) {
1591  const DIDerivedType *DTy = TypeInfo.first;
1592  BTFTypeDerived *BDType = TypeInfo.second;
1593 
1594  int TmpTypeId = genBTFTypeTags(DTy, StructTypeId);
1595  if (TmpTypeId >= 0)
1596  BDType->setPointeeType(TmpTypeId);
1597  else
1598  BDType->setPointeeType(StructTypeId);
1599  }
1600  }
1601 
1602  // Complete BTF type cross refereences.
1603  for (const auto &TypeEntry : TypeEntries)
1604  TypeEntry->completeType(*this);
1605 
1606  // Emit BTF sections.
1607  emitBTFSection();
1608  emitBTFExtSection();
1609 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:77
BPFMCTargetDesc.h
llvm::BTFTypeFloat::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:434
MemoryBuffer.h
AsmPrinter.h
llvm::BTFFieldReloc::OffsetNameOff
uint32_t OffsetNameOff
The string to traverse types.
Definition: BTFDebug.h:284
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:290
llvm::BTF::BTFMember
BTF_KIND_STRUCT and BTF_KIND_UNION are followed by multiple "struct BTFMember".
Definition: BTF.h:175
getName
static StringRef getName(Value *V)
Definition: ProvenanceAnalysisEvaluator.cpp:42
llvm::DICompositeType::getBaseType
DIType * getBaseType() const
Definition: DebugInfoMetadata.h:1175
llvm::DebugHandlerBase::beginInstruction
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
Definition: DebugHandlerBase.cpp:348
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm::BTFTypeDerived::setPointeeType
void setPointeeType(uint32_t PointeeType)
Definition: BTFDebug.cpp:103
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DIType
Base class for types.
Definition: DebugInfoMetadata.h:658
llvm::BTFTypeDeclTag::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:451
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::BTF::BPFFieldRelocSize
@ BPFFieldRelocSize
Definition: BTF.h:72
llvm::BTF::BTFEnum64::Val_Hi32
uint32_t Val_Hi32
Enum member hi32 value.
Definition: BTF.h:155
llvm::StructType::getName
StringRef getName() const
Return the name for this struct type if it has an identity.
Definition: Type.cpp:581
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::BTFFuncInfo::Label
const MCSymbol * Label
Func MCSymbol.
Definition: BTFDebug.h:267
llvm::BTF::BTFEnum
BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
Definition: BTF.h:144
llvm::DIType::getSizeInBits
uint64_t getSizeInBits() const
Definition: DebugInfoMetadata.h:697
llvm::line_iterator
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
llvm::BTF::HeaderSize
@ HeaderSize
Definition: BTF.h:58
llvm::MachineOperand::getGlobal
const GlobalValue * getGlobal() const
Definition: MachineOperand.h:572
llvm::BTFLineInfo
Represent one line info.
Definition: BTFDebug.h:272
llvm::BTFDebug::InstLower
bool InstLower(const MachineInstr *MI, MCInst &OutMI)
Emit proper patchable instructions.
Definition: BTFDebug.cpp:1467
llvm::MCOperand::createImm
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
llvm::Function
Definition: Function.h:60
llvm::BTFKindDataSec::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:417
llvm::DICompositeType::getAnnotations
DINodeArray getAnnotations() const
Definition: DebugInfoMetadata.h:1229
llvm::BTFLineInfo::FileNameOff
uint32_t FileNameOff
file name offset in the .BTF string table
Definition: BTFDebug.h:274
llvm::BTF::FUNC_EXTERN
@ FUNC_EXTERN
Definition: BTF.h:193
llvm::BTF::VAR_STATIC
@ VAR_STATIC
Linkage: InternalLinkage.
Definition: BTF.h:198
llvm::BTFTypeBase::Kind
uint8_t Kind
Definition: BTFDebug.h:41
llvm::BTFTypeEnum::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:172
MCSectionELF.h
llvm::MCStreamer::emitInt8
void emitInt8(uint64_t Value)
Definition: MCStreamer.h:736
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::BTFFieldReloc::Label
const MCSymbol * Label
MCSymbol identifying insn for the reloc.
Definition: BTFDebug.h:282
llvm::BTFFieldReloc::TypeID
uint32_t TypeID
Type ID.
Definition: BTFDebug.h:283
llvm::Function::getSubprogram
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1571
llvm::BTF::FUNC_GLOBAL
@ FUNC_GLOBAL
Definition: BTF.h:192
llvm::BPFCoreSharedInfo::BTF_TYPE_ID_LOCAL
@ BTF_TYPE_ID_LOCAL
Definition: BPFCORE.h:29
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::GlobalObject::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1227
llvm::BTFKindVar::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:396
Content
T Content
Definition: ELFObjHandler.cpp:88
llvm::BTFTypeEnum64::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:235
llvm::DIScope::getName
StringRef getName() const
Definition: DebugInfoMetadata.cpp:264
llvm::tgtok::VarName
@ VarName
Definition: TGLexer.h:72
llvm::DebugHandlerBase::PrevInstLoc
DebugLoc PrevInstLoc
Previous instruction's location information.
Definition: DebugHandlerBase.h:67
MCObjectFileInfo.h
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1886
llvm::StringMap::end
iterator end()
Definition: StringMap.h:205
llvm::BTFKindDataSec::BTFKindDataSec
BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
Definition: BTFDebug.cpp:405
llvm::MCStreamer::emitInt32
void emitInt32(uint64_t Value)
Definition: MCStreamer.h:738
llvm::SectionKind::isMergeableConst
bool isMergeableConst() const
Definition: SectionKind.h:144
llvm::BTF::BTFMember::Type
uint32_t Type
Member type.
Definition: BTF.h:177
llvm::GlobalVariable::hasAttribute
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists.
Definition: GlobalVariable.h:197
llvm::BTFStringTable::getTable
std::vector< std::string > & getTable()
Definition: BTFDebug.h:259
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:79
llvm::BTF::VAR_GLOBAL_EXTERNAL
@ VAR_GLOBAL_EXTERNAL
Linkage: ExternalLinkage.
Definition: BTF.h:200
llvm::AsmPrinter::emitLabelReference
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:632
llvm::BTF::SecFieldRelocSize
@ SecFieldRelocSize
Definition: BTF.h:69
llvm::BTFDebug::BTFDebug
BTFDebug(AsmPrinter *AP)
Definition: BTFDebug.cpp:505
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MCSectionELF
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:26
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:218
BPF.h
llvm::BTFTypeDerived::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:79
llvm::BTFTypeArray::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:269
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:105
llvm::BTFTypeFuncProto::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:336
llvm::BTFDebug::getTypeId
uint32_t getTypeId(const DIType *Ty)
Get the type id for a particular DIType.
Definition: BTFDebug.h:409
BTFKindStr
static const char * BTFKindStr[]
Definition: BTFDebug.cpp:30
llvm::BTF::BPFLineInfoSize
@ BPFLineInfoSize
Definition: BTF.h:71
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::Annotations
Annotations lets you mark points and ranges inside source code, for tests:
Definition: Annotations.h:48
llvm::MCInst::setOpcode
void setOpcode(unsigned Op)
Definition: MCInst.h:197
llvm::BTF::SecFuncInfoSize
@ SecFuncInfoSize
Definition: BTF.h:67
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:82
llvm::StringRef::substr
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:615
llvm::BTFTypeFloat::BTFTypeFloat
BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName)
Definition: BTFDebug.cpp:427
llvm::DITypeRefArray
Definition: DebugInfoMetadata.h:69
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
llvm::BTFTypeDerived::BTFTypeDerived
BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup)
Definition: BTFDebug.cpp:45
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:212
llvm::BTF::BTFArray::Nelems
uint32_t Nelems
Number of elements for this array.
Definition: BTF.h:162
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:239
ELF.h
llvm::MCSymbol::getSection
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:262
llvm::DISubrange::getCount
BoundType getCount() const
Definition: DebugInfoMetadata.cpp:378
llvm::BTFLineInfo::ColumnNum
uint32_t ColumnNum
the column number
Definition: BTFDebug.h:277
llvm::DebugHandlerBase::Asm
AsmPrinter * Asm
Target of debug info emission.
Definition: DebugHandlerBase.h:58
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::BTFTypeTypeTag::BTFTypeTypeTag
BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag)
Definition: BTFDebug.cpp:464
llvm::BTFStringTable::addString
uint32_t addString(StringRef S)
Add a string to the string table and returns its offset in the table.
Definition: BTFDebug.cpp:491
llvm::BTFTypeStruct::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:285
MCContext.h
llvm::MCStreamer::emitLabel
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:423
llvm::MCStreamer::switchSection
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:1225
llvm::DINode::getTag
dwarf::Tag getTag() const
Definition: DebugInfoMetadata.cpp:184
Param
Value * Param
Definition: NVPTXLowerArgs.cpp:164
llvm::DIType::getName
StringRef getName() const
Definition: DebugInfoMetadata.h:704
llvm::MCContext::getELFSection
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:560
llvm::DISubrange
Array subrange.
Definition: DebugInfoMetadata.h:300
false
Definition: StackSlotColoring.cpp:141
llvm::BTFTypeBase::BTFType
struct BTF::CommonType BTFType
Definition: BTFDebug.h:44
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:48
llvm::DIBasicType::getEncoding
unsigned getEncoding() const
Definition: DebugInfoMetadata.h:818
llvm::BTFTypeDerived
Handle several derived types include pointer, const, volatile, typedef and restrict.
Definition: BTFDebug.h:64
llvm::MachineInstr::FrameSetup
@ FrameSetup
Definition: MachineInstr.h:84
LineIterator.h
llvm::BTF::BTFMember::Offset
uint32_t Offset
BitOffset or BitFieldSize+BitOffset.
Definition: BTF.h:178
llvm::BTFTypeInt::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:159
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
Align
uint64_t Align
Definition: ELFObjHandler.cpp:81
llvm::GlobalValue::InternalLinkage
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::BTFDebug::addString
size_t addString(StringRef S)
Add string to the string table.
Definition: BTFDebug.h:406
llvm::TargetLoweringObjectFile::getKindForGlobal
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
Definition: TargetLoweringObjectFile.cpp:200
llvm::AsmPrinter::getFunctionBegin
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:263
llvm::BTFTypeFwd::BTFTypeFwd
BTFTypeFwd(StringRef Name, bool IsUnion)
Represent a struct/union forward declaration.
Definition: BTFDebug.cpp:108
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
llvm::BTF::BPFFuncInfoSize
@ BPFFuncInfoSize
Definition: BTF.h:70
llvm::BPFCoreSharedInfo::BTF_TYPE_ID_REMOTE
@ BTF_TYPE_ID_REMOTE
Definition: BPFCORE.h:30
llvm::BTFDebug
Collect and emit BTF information.
Definition: BTFDebug.h:289
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1204
llvm::BTF::BTFMember::NameOff
uint32_t NameOff
Member name offset in the string table.
Definition: BTF.h:176
llvm::BTFTypeEnum::BTFTypeEnum
BTFTypeEnum(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned)
Definition: BTFDebug.cpp:165
llvm::BTFTypeFunc::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:378
llvm::BTFFuncInfo
Represent one func and its type id.
Definition: BTFDebug.h:266
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::Twine::utohexstr
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:408
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::DICompositeType::getElements
DINodeArray getElements() const
Definition: DebugInfoMetadata.h:1176
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::BPFCoreSharedInfo::ENUM_VALUE
@ ENUM_VALUE
Definition: BPFCORE.h:34
uint64_t
llvm::BTFLineInfo::LineNum
uint32_t LineNum
the line number
Definition: BTFDebug.h:276
llvm::GlobalValue::WeakAnyLinkage
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
llvm::DIGlobalVariable
Global variables.
Definition: DebugInfoMetadata.h:2914
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::BTF::VAR_GLOBAL_ALLOCATED
@ VAR_GLOBAL_ALLOCATED
Linkage: ExternalLinkage.
Definition: BTF.h:199
llvm::BTF::MAX_VLEN
@ MAX_VLEN
Max # of struct/union/enum members or func args.
Definition: BTF.h:90
I
#define I(x, y, z)
Definition: MD5.cpp:58
FuncId
Profile::FuncID FuncId
Definition: Profile.cpp:321
llvm::BTFTypeTypeTag::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:477
llvm::SectionKind::isMergeableCString
bool isMergeableCString() const
Definition: SectionKind.h:136
BTFDebug.h
llvm::BTF::BTFParam
BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
Definition: BTF.h:184
llvm::BTF::BTFArray::IndexType
uint32_t IndexType
Index type.
Definition: BTF.h:161
llvm::BTFStringTable::getSize
uint32_t getSize()
Definition: BTFDebug.h:258
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:654
llvm::BTF::INT_SIGNED
@ INT_SIGNED
Definition: BTF.h:136
llvm::MCStreamer::emitIntValue
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:134
llvm::BTFLineInfo::Label
MCSymbol * Label
MCSymbol identifying insn for the lineinfo.
Definition: BTFDebug.h:273
llvm::BPFCoreSharedInfo::TypeIdAttr
static constexpr StringRef TypeIdAttr
The attribute attached to globals representing a type id.
Definition: BPFCORE.h:63
MachineModuleInfo.h
llvm::BTFTypeEnum::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:196
llvm::BTFTypeStruct::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:311
llvm::BTFTypeFuncProto::BTFTypeFuncProto
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:328
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MDNode
Metadata node.
Definition: Metadata.h:937
llvm::BTFTypeStruct::BTFTypeStruct
BTFTypeStruct(const DICompositeType *STy, bool IsStruct, bool HasBitField, uint32_t NumMembers)
Represent either a struct or a union.
Definition: BTFDebug.cpp:277
llvm::BTFDebug::endFunctionImpl
void endFunctionImpl(const MachineFunction *MF) override
Post process after all instructions in this function are processed.
Definition: BTFDebug.cpp:1214
llvm::DebugHandlerBase::CurMI
const MachineInstr * CurMI
If nonnull, stores the current machine instruction we're processing.
Definition: DebugHandlerBase.h:76
llvm::BTF::MAGIC
@ MAGIC
Definition: BTF.h:54
isReg
static bool isReg(const MCInst &MI, unsigned OpNo)
Definition: MipsInstPrinter.cpp:31
llvm::DIBasicType
Basic type, like 'int' or 'float'.
Definition: DebugInfoMetadata.h:764
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1598
llvm::BTFTypeInt::BTFTypeInt
BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, uint32_t OffsetInBits, StringRef TypeName)
Definition: BTFDebug.cpp:124
llvm::DISubroutineType
Type array for a subprogram.
Definition: DebugInfoMetadata.h:1265
llvm::DICompositeType
Composite types.
Definition: DebugInfoMetadata.h:1042
llvm::DICompileUnit::NoDebug
@ NoDebug
Definition: DebugInfoMetadata.h:1328
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::BTFFieldReloc
Represent one field relocation.
Definition: BTFDebug.h:281
llvm::BTF::BTFEnum64::Val_Lo32
uint32_t Val_Lo32
Enum member lo32 value.
Definition: BTF.h:154
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:213
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:233
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::DINode
Tagged DWARF-like metadata node.
Definition: DebugInfoMetadata.h:129
llvm::BTFTypeFuncProto::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:362
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::MCOperand::createReg
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
TargetLoweringObjectFile.h
llvm::GlobalValue::WeakODRLinkage
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
uint32_t
llvm::BTFDebug::getArrayIndexTypeId
uint32_t getArrayIndexTypeId()
Get the special array index type id.
Definition: BTFDebug.h:400
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::BTF::BTFEnum64
BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
Definition: BTF.h:152
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::ConstantInt::getSExtValue
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:148
llvm::BPFCoreSharedInfo::ENUM_VALUE_EXISTENCE
@ ENUM_VALUE_EXISTENCE
Definition: BPFCORE.h:33
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:305
llvm::MCContext::createTempSymbol
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:324
llvm::BTF::BTFEnum::Val
int32_t Val
Enum member value.
Definition: BTF.h:146
llvm::BTF::BTFArray::ElemType
uint32_t ElemType
Element type.
Definition: BTF.h:160
llvm::BTF::BTFEnum::NameOff
uint32_t NameOff
Enum name offset in the string table.
Definition: BTF.h:145
llvm::MCSection::getName
StringRef getName() const
Definition: MCSection.h:124
llvm::MCSection::setAlignment
void setAlignment(Align Value)
Definition: MCSection.h:141
llvm::DIType::isForwardDecl
bool isForwardDecl() const
Definition: DebugInfoMetadata.h:725
llvm::BTFTypeStruct::getName
std::string getName()
Definition: BTFDebug.cpp:321
llvm::DITypeRefArray::size
unsigned size() const
Definition: DebugInfoMetadata.h:84
llvm::DIType::getOffsetInBits
uint64_t getOffsetInBits() const
Definition: DebugInfoMetadata.h:700
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
llvm::pdb::PDB_SymType::Label
@ Label
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::BTF::CommonType::Info
uint32_t Info
"Info" bits arrangement: Bits 0-15: vlen (e.g.
Definition: BTF.h:111
llvm::DIGlobal
Container for description of a global variable.
Definition: DIContext.h:113
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:622
llvm::BTF::BTFEnum64::NameOff
uint32_t NameOff
Enum name offset in the string table.
Definition: BTF.h:153
llvm::BTFFuncInfo::TypeId
uint32_t TypeId
Type id referring to .BTF type section.
Definition: BTFDebug.h:268
llvm::BTFDebug::beginFunctionImpl
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
Definition: BTFDebug.cpp:1125
llvm::BTFTypeBase::Id
uint32_t Id
Definition: BTFDebug.h:43
llvm::AMDGPU::HSAMD::Kernel::Arg::Key::TypeName
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
Definition: AMDGPUMetadata.h:175
llvm::BTFTypeArray::completeType
void completeType(BTFDebug &BDebug) override
Represent a BTF array.
Definition: BTFDebug.cpp:257
llvm::BTFTypeBase::emitType
virtual void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:36
llvm::BTF::VERSION
@ VERSION
Definition: BTF.h:54
llvm::DISubroutineType::getTypeArray
DITypeRefArray getTypeArray() const
Definition: DebugInfoMetadata.h:1310
llvm::BTFKindVar::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:400
llvm::BTFTypeFunc::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:386
llvm::BTF::ExtHeaderSize
@ ExtHeaderSize
Definition: BTF.h:59
BPFCORE.h
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:81
llvm::BTFTypeDeclTag::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:459
llvm::BTFTypeEnum64::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:211
llvm::BTF::SecLineInfoSize
@ SecLineInfoSize
Definition: BTF.h:68
llvm::DIDerivedType
Derived types.
Definition: DebugInfoMetadata.h:924
llvm::BTF::FUNC_STATIC
@ FUNC_STATIC
Definition: BTF.h:191
llvm::BTFTypeEnum64::BTFTypeEnum64
BTFTypeEnum64(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned)
Definition: BTFDebug.cpp:204
llvm::RISCVMatInt::Imm
@ Imm
Definition: RISCVMatInt.h:23
llvm::BTF::INT_BOOL
@ INT_BOOL
Definition: BTF.h:138
llvm::GlobalValue::ExternalLinkage
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
llvm::BPFCoreSharedInfo::AmaAttr
static constexpr StringRef AmaAttr
The attribute attached to globals representing a field access.
Definition: BPFCORE.h:61
llvm::BTF::CommonType::NameOff
uint32_t NameOff
Type name offset in the string table.
Definition: BTF.h:102
llvm::BTFFieldReloc::RelocKind
uint32_t RelocKind
What to patch the instruction.
Definition: BTFDebug.h:285
llvm::MCStreamer::getContext
MCContext & getContext() const
Definition: MCStreamer.h:293
llvm::AsmPrinter::TM
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:84
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition: ELF.h:955
N
#define N
MCStreamer.h
llvm::MCSymbol::isInSection
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition: MCSymbol.h:247
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::to_string
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:85
llvm::BTFTypeFunc::BTFTypeFunc
BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId, uint32_t Scope)
Definition: BTFDebug.cpp:370
llvm::pdb::PDB_DataKind::Global
@ Global
llvm::DebugHandlerBase::MMI
MachineModuleInfo * MMI
Collected machine module information.
Definition: DebugHandlerBase.h:61
llvm::DebugHandlerBase
Base class for debug information backends.
Definition: DebugHandlerBase.h:53
llvm::MCStreamer::emitBytes
virtual void emitBytes(StringRef Data)
Emit the bytes in Data into the output.
Definition: MCStreamer.cpp:1202
llvm::DISubprogram
Subprogram description.
Definition: DebugInfoMetadata.h:1803
llvm::BTF::CommonType::Type
uint32_t Type
Definition: BTF.h:121
llvm::BTFTypeDeclTag::BTFTypeDeclTag
BTFTypeDeclTag(uint32_t BaseTypeId, int ComponentId, StringRef Tag)
Definition: BTFDebug.cpp:442
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::BTFLineInfo::LineOff
uint32_t LineOff
line offset in the .BTF string table
Definition: BTFDebug.h:275
llvm::StringRef::find_first_of
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:414
llvm::BTFTypeInt::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:151
llvm::BTFTypeBase::IsCompleted
bool IsCompleted
Definition: BTFDebug.h:42
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::BTFTypeArray::BTFTypeArray
BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems)
Definition: BTFDebug.cpp:246
llvm::GlobalValue::ExternalWeakLinkage
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:57
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:238
llvm::MemoryBuffer::getMemBufferCopy
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.
Definition: MemoryBuffer.cpp:138
llvm::MDString
A single uniqued string.
Definition: Metadata.h:612
llvm::MachineModuleInfo::getModule
const Module * getModule() const
Definition: MachineModuleInfo.h:151
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::BTF::CommonType::Size
uint32_t Size
Definition: BTF.h:120
llvm::BTFKindDataSec::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:412
llvm::BTFTypeFwd::completeType
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:114
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:198
llvm::BTFDebug::endModule
void endModule() override
Complete all the types and emit the BTF sections.
Definition: BTFDebug.cpp:1557
llvm::MCStreamer::AddComment
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:355
llvm::BTFKindVar::BTFKindVar
BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
Definition: BTFDebug.cpp:388
llvm::BTFTypeDerived::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:101
llvm::BTFDebug::beginInstruction
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
Definition: BTFDebug.cpp:1286
llvm::BTFTypeFwd::emitType
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:122
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::DIScope::getFile
DIFile * getFile() const
Definition: DebugInfoMetadata.h:479
llvm::MachineOperand::isGlobal
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Definition: MachineOperand.h:338
llvm::BTFTypeBase::roundupToBytes
uint32_t roundupToBytes(uint32_t NumBits)
Definition: BTFDebug.h:51