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