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