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