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