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 ")");
40 OS.emitInt32(BTFType.NameOff);
41 OS.AddComment("0x" + Twine::utohexstr(BTFType.Info));
42 OS.emitInt32(BTFType.Info);
43 OS.emitInt32(BTFType.Size);
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
785bool BTFDebug::IsForwardDeclCandidate(const DIType *Base) {
786 if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
787 auto CTag = CTy->getTag();
788 if ((CTag == dwarf::DW_TAG_structure_type ||
789 CTag == dwarf::DW_TAG_union_type) &&
790 !CTy->getName().empty() && !CTy->isForwardDecl())
791 return true;
792 }
793 return false;
794}
795
796/// Handle pointer, typedef, const, volatile, restrict and member types.
797void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
798 bool CheckPointer, bool SeenPointer) {
799 unsigned Tag = DTy->getTag();
800
801 /// Try to avoid chasing pointees, esp. structure pointees which may
802 /// unnecessary bring in a lot of types.
803 if (CheckPointer && !SeenPointer) {
804 SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
805 }
806
807 if (CheckPointer && SeenPointer) {
808 const DIType *Base = DTy->getBaseType();
809 if (Base) {
810 if (IsForwardDeclCandidate(Base)) {
811 /// Find a candidate, generate a fixup. Later on the struct/union
812 /// pointee type will be replaced with either a real type or
813 /// a forward declaration.
814 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true);
815 auto &Fixup = FixupDerivedTypes[cast<DICompositeType>(Base)];
816 Fixup.push_back(std::make_pair(DTy, TypeEntry.get()));
817 TypeId = addType(std::move(TypeEntry), DTy);
818 return;
819 }
820 }
821 }
822
823 if (Tag == dwarf::DW_TAG_pointer_type) {
824 int TmpTypeId = genBTFTypeTags(DTy, -1);
825 if (TmpTypeId >= 0) {
826 auto TypeDEntry =
827 std::make_unique<BTFTypeDerived>(TmpTypeId, Tag, DTy->getName());
828 TypeId = addType(std::move(TypeDEntry), DTy);
829 } else {
830 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
831 TypeId = addType(std::move(TypeEntry), DTy);
832 }
833 } else if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
834 Tag == dwarf::DW_TAG_volatile_type ||
835 Tag == dwarf::DW_TAG_restrict_type) {
836 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
837 TypeId = addType(std::move(TypeEntry), DTy);
838 if (Tag == dwarf::DW_TAG_typedef)
839 processDeclAnnotations(DTy->getAnnotations(), TypeId, -1);
840 } else if (Tag != dwarf::DW_TAG_member) {
841 return;
842 }
843
844 // Visit base type of pointer, typedef, const, volatile, restrict or
845 // struct/union member.
846 uint32_t TempTypeId = 0;
847 if (Tag == dwarf::DW_TAG_member)
848 visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
849 else
850 visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
851}
852
853/// Visit a type entry. CheckPointer is true if the type has
854/// one of its predecessors as one struct/union member. SeenPointer
855/// is true if CheckPointer is true and one of its predecessors
856/// is a pointer. The goal of CheckPointer and SeenPointer is to
857/// do pruning for struct/union types so some of these types
858/// will not be emitted in BTF and rather forward declarations
859/// will be generated.
860void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
861 bool CheckPointer, bool SeenPointer) {
862 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
863 TypeId = DIToIdMap[Ty];
864
865 // To handle the case like the following:
866 // struct t;
867 // typedef struct t _t;
868 // struct s1 { _t *c; };
869 // int test1(struct s1 *arg) { ... }
870 //
871 // struct t { int a; int b; };
872 // struct s2 { _t c; }
873 // int test2(struct s2 *arg) { ... }
874 //
875 // During traversing test1() argument, "_t" is recorded
876 // in DIToIdMap and a forward declaration fixup is created
877 // for "struct t" to avoid pointee type traversal.
878 //
879 // During traversing test2() argument, even if we see "_t" is
880 // already defined, we should keep moving to eventually
881 // bring in types for "struct t". Otherwise, the "struct s2"
882 // definition won't be correct.
883 //
884 // In the above, we have following debuginfo:
885 // {ptr, struct_member} -> typedef -> struct
886 // and BTF type for 'typedef' is generated while 'struct' may
887 // be in FixUp. But let us generalize the above to handle
888 // {different types} -> [various derived types]+ -> another type.
889 // For example,
890 // {func_param, struct_member} -> const -> ptr -> volatile -> struct
891 // We will traverse const/ptr/volatile which already have corresponding
892 // BTF types and generate type for 'struct' which might be in Fixup
893 // state.
894 if (Ty && (!CheckPointer || !SeenPointer)) {
895 if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
896 while (DTy) {
897 const DIType *BaseTy = DTy->getBaseType();
898 if (!BaseTy)
899 break;
900
901 if (DIToIdMap.find(BaseTy) != DIToIdMap.end()) {
902 DTy = dyn_cast<DIDerivedType>(BaseTy);
903 } else {
904 if (CheckPointer && DTy->getTag() == dwarf::DW_TAG_pointer_type) {
905 SeenPointer = true;
906 if (IsForwardDeclCandidate(BaseTy))
907 break;
908 }
909 uint32_t TmpTypeId;
910 visitTypeEntry(BaseTy, TmpTypeId, CheckPointer, SeenPointer);
911 break;
912 }
913 }
914 }
915 }
916
917 return;
918 }
919
920 if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
921 visitBasicType(BTy, TypeId);
922 else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
923 visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
924 TypeId);
925 else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
926 visitCompositeType(CTy, TypeId);
927 else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
928 visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
929 else
930 llvm_unreachable("Unknown DIType");
931}
932
933void BTFDebug::visitTypeEntry(const DIType *Ty) {
934 uint32_t TypeId;
935 visitTypeEntry(Ty, TypeId, false, false);
936}
937
938void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
939 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
940 TypeId = DIToIdMap[Ty];
941 return;
942 }
943
944 // MapDef type may be a struct type or a non-pointer derived type
945 const DIType *OrigTy = Ty;
946 while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
947 auto Tag = DTy->getTag();
948 if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
949 Tag != dwarf::DW_TAG_volatile_type &&
950 Tag != dwarf::DW_TAG_restrict_type)
951 break;
952 Ty = DTy->getBaseType();
953 }
954
955 const auto *CTy = dyn_cast<DICompositeType>(Ty);
956 if (!CTy)
957 return;
958
959 auto Tag = CTy->getTag();
960 if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
961 return;
962
963 // Visit all struct members to ensure pointee type is visited
964 const DINodeArray Elements = CTy->getElements();
965 for (const auto *Element : Elements) {
966 const auto *MemberType = cast<DIDerivedType>(Element);
967 visitTypeEntry(MemberType->getBaseType());
968 }
969
970 // Visit this type, struct or a const/typedef/volatile/restrict type
971 visitTypeEntry(OrigTy, TypeId, false, false);
972}
973
974/// Read file contents from the actual file or from the source
975std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
976 auto File = SP->getFile();
977 std::string FileName;
978
979 if (!File->getFilename().startswith("/") && File->getDirectory().size())
980 FileName = File->getDirectory().str() + "/" + File->getFilename().str();
981 else
982 FileName = std::string(File->getFilename());
983
984 // No need to populate the contends if it has been populated!
985 if (FileContent.contains(FileName))
986 return FileName;
987
988 std::vector<std::string> Content;
989 std::string Line;
990 Content.push_back(Line); // Line 0 for empty string
991
992 std::unique_ptr<MemoryBuffer> Buf;
993 auto Source = File->getSource();
994 if (Source)
995 Buf = MemoryBuffer::getMemBufferCopy(*Source);
996 else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
997 MemoryBuffer::getFile(FileName))
998 Buf = std::move(*BufOrErr);
999 if (Buf)
1000 for (line_iterator I(*Buf, false), E; I != E; ++I)
1001 Content.push_back(std::string(*I));
1002
1003 FileContent[FileName] = Content;
1004 return FileName;
1005}
1006
1007void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
1008 uint32_t Line, uint32_t Column) {
1009 std::string FileName = populateFileContent(SP);
1010 BTFLineInfo LineInfo;
1011
1012 LineInfo.Label = Label;
1013 LineInfo.FileNameOff = addString(FileName);
1014 // If file content is not available, let LineOff = 0.
1015 if (Line < FileContent[FileName].size())
1016 LineInfo.LineOff = addString(FileContent[FileName][Line]);
1017 else
1018 LineInfo.LineOff = 0;
1019 LineInfo.LineNum = Line;
1020 LineInfo.ColumnNum = Column;
1021 LineInfoTable[SecNameOff].push_back(LineInfo);
1022}
1023
1024void BTFDebug::emitCommonHeader() {
1025 OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
1026 OS.emitIntValue(BTF::MAGIC, 2);
1027 OS.emitInt8(BTF::VERSION);
1028 OS.emitInt8(0);
1029}
1030
1031void BTFDebug::emitBTFSection() {
1032 // Do not emit section if no types and only "" string.
1033 if (!TypeEntries.size() && StringTable.getSize() == 1)
1034 return;
1035
1036 MCContext &Ctx = OS.getContext();
1037 MCSectionELF *Sec = Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0);
1038 Sec->setAlignment(Align(4));
1039 OS.switchSection(Sec);
1040
1041 // Emit header.
1042 emitCommonHeader();
1043 OS.emitInt32(BTF::HeaderSize);
1044
1045 uint32_t TypeLen = 0, StrLen;
1046 for (const auto &TypeEntry : TypeEntries)
1047 TypeLen += TypeEntry->getSize();
1048 StrLen = StringTable.getSize();
1049
1050 OS.emitInt32(0);
1051 OS.emitInt32(TypeLen);
1052 OS.emitInt32(TypeLen);
1053 OS.emitInt32(StrLen);
1054
1055 // Emit type table.
1056 for (const auto &TypeEntry : TypeEntries)
1057 TypeEntry->emitType(OS);
1058
1059 // Emit string table.
1060 uint32_t StringOffset = 0;
1061 for (const auto &S : StringTable.getTable()) {
1062 OS.AddComment("string offset=" + std::to_string(StringOffset));
1063 OS.emitBytes(S);
1064 OS.emitBytes(StringRef("\0", 1));
1065 StringOffset += S.size() + 1;
1066 }
1067}
1068
1069void BTFDebug::emitBTFExtSection() {
1070 // Do not emit section if empty FuncInfoTable and LineInfoTable
1071 // and FieldRelocTable.
1072 if (!FuncInfoTable.size() && !LineInfoTable.size() &&
1073 !FieldRelocTable.size())
1074 return;
1075
1076 MCContext &Ctx = OS.getContext();
1077 MCSectionELF *Sec = Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0);
1078 Sec->setAlignment(Align(4));
1079 OS.switchSection(Sec);
1080
1081 // Emit header.
1082 emitCommonHeader();
1083 OS.emitInt32(BTF::ExtHeaderSize);
1084
1085 // Account for FuncInfo/LineInfo record size as well.
1086 uint32_t FuncLen = 4, LineLen = 4;
1087 // Do not account for optional FieldReloc.
1088 uint32_t FieldRelocLen = 0;
1089 for (const auto &FuncSec : FuncInfoTable) {
1090 FuncLen += BTF::SecFuncInfoSize;
1091 FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
1092 }
1093 for (const auto &LineSec : LineInfoTable) {
1094 LineLen += BTF::SecLineInfoSize;
1095 LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
1096 }
1097 for (const auto &FieldRelocSec : FieldRelocTable) {
1098 FieldRelocLen += BTF::SecFieldRelocSize;
1099 FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize;
1100 }
1101
1102 if (FieldRelocLen)
1103 FieldRelocLen += 4;
1104
1105 OS.emitInt32(0);
1106 OS.emitInt32(FuncLen);
1107 OS.emitInt32(FuncLen);
1108 OS.emitInt32(LineLen);
1109 OS.emitInt32(FuncLen + LineLen);
1110 OS.emitInt32(FieldRelocLen);
1111
1112 // Emit func_info table.
1113 OS.AddComment("FuncInfo");
1114 OS.emitInt32(BTF::BPFFuncInfoSize);
1115 for (const auto &FuncSec : FuncInfoTable) {
1116 OS.AddComment("FuncInfo section string offset=" +
1117 std::to_string(FuncSec.first));
1118 OS.emitInt32(FuncSec.first);
1119 OS.emitInt32(FuncSec.second.size());
1120 for (const auto &FuncInfo : FuncSec.second) {
1121 Asm->emitLabelReference(FuncInfo.Label, 4);
1122 OS.emitInt32(FuncInfo.TypeId);
1123 }
1124 }
1125
1126 // Emit line_info table.
1127 OS.AddComment("LineInfo");
1128 OS.emitInt32(BTF::BPFLineInfoSize);
1129 for (const auto &LineSec : LineInfoTable) {
1130 OS.AddComment("LineInfo section string offset=" +
1131 std::to_string(LineSec.first));
1132 OS.emitInt32(LineSec.first);
1133 OS.emitInt32(LineSec.second.size());
1134 for (const auto &LineInfo : LineSec.second) {
1135 Asm->emitLabelReference(LineInfo.Label, 4);
1136 OS.emitInt32(LineInfo.FileNameOff);
1137 OS.emitInt32(LineInfo.LineOff);
1138 OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
1139 std::to_string(LineInfo.ColumnNum));
1140 OS.emitInt32(LineInfo.LineNum << 10 | LineInfo.ColumnNum);
1141 }
1142 }
1143
1144 // Emit field reloc table.
1145 if (FieldRelocLen) {
1146 OS.AddComment("FieldReloc");
1147 OS.emitInt32(BTF::BPFFieldRelocSize);
1148 for (const auto &FieldRelocSec : FieldRelocTable) {
1149 OS.AddComment("Field reloc section string offset=" +
1150 std::to_string(FieldRelocSec.first));
1151 OS.emitInt32(FieldRelocSec.first);
1152 OS.emitInt32(FieldRelocSec.second.size());
1153 for (const auto &FieldRelocInfo : FieldRelocSec.second) {
1154 Asm->emitLabelReference(FieldRelocInfo.Label, 4);
1155 OS.emitInt32(FieldRelocInfo.TypeID);
1156 OS.emitInt32(FieldRelocInfo.OffsetNameOff);
1157 OS.emitInt32(FieldRelocInfo.RelocKind);
1158 }
1159 }
1160 }
1161}
1162
1164 auto *SP = MF->getFunction().getSubprogram();
1165 auto *Unit = SP->getUnit();
1166
1167 if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
1168 SkipInstruction = true;
1169 return;
1170 }
1171 SkipInstruction = false;
1172
1173 // Collect MapDef types. Map definition needs to collect
1174 // pointee types. Do it first. Otherwise, for the following
1175 // case:
1176 // struct m { ...};
1177 // struct t {
1178 // struct m *key;
1179 // };
1180 // foo(struct t *arg);
1181 //
1182 // struct mapdef {
1183 // ...
1184 // struct m *key;
1185 // ...
1186 // } __attribute__((section(".maps"))) hash_map;
1187 //
1188 // If subroutine foo is traversed first, a type chain
1189 // "ptr->struct m(fwd)" will be created and later on
1190 // when traversing mapdef, since "ptr->struct m" exists,
1191 // the traversal of "struct m" will be omitted.
1192 if (MapDefNotCollected) {
1193 processGlobals(true);
1194 MapDefNotCollected = false;
1195 }
1196
1197 // Collect all types locally referenced in this function.
1198 // Use RetainedNodes so we can collect all argument names
1199 // even if the argument is not used.
1200 std::unordered_map<uint32_t, StringRef> FuncArgNames;
1201 for (const DINode *DN : SP->getRetainedNodes()) {
1202 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
1203 // Collect function arguments for subprogram func type.
1204 uint32_t Arg = DV->getArg();
1205 if (Arg) {
1206 visitTypeEntry(DV->getType());
1207 FuncArgNames[Arg] = DV->getName();
1208 }
1209 }
1210 }
1211
1212 // Construct subprogram func proto type.
1213 uint32_t ProtoTypeId;
1214 visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
1215
1216 // Construct subprogram func type
1217 uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL;
1218 uint32_t FuncTypeId = processDISubprogram(SP, ProtoTypeId, Scope);
1219
1220 for (const auto &TypeEntry : TypeEntries)
1221 TypeEntry->completeType(*this);
1222
1223 // Construct funcinfo and the first lineinfo for the function.
1224 MCSymbol *FuncLabel = Asm->getFunctionBegin();
1226 FuncInfo.Label = FuncLabel;
1227 FuncInfo.TypeId = FuncTypeId;
1228 if (FuncLabel->isInSection()) {
1229 MCSection &Section = FuncLabel->getSection();
1230 const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
1231 assert(SectionELF && "Null section for Function Label");
1232 SecNameOff = addString(SectionELF->getName());
1233 } else {
1234 SecNameOff = addString(".text");
1235 }
1236 FuncInfoTable[SecNameOff].push_back(FuncInfo);
1237}
1238
1240 SkipInstruction = false;
1241 LineInfoGenerated = false;
1242 SecNameOff = 0;
1243}
1244
1245/// On-demand populate types as requested from abstract member
1246/// accessing or preserve debuginfo type.
1247unsigned BTFDebug::populateType(const DIType *Ty) {
1248 unsigned Id;
1249 visitTypeEntry(Ty, Id, false, false);
1250 for (const auto &TypeEntry : TypeEntries)
1251 TypeEntry->completeType(*this);
1252 return Id;
1253}
1254
1255/// Generate a struct member field relocation.
1256void BTFDebug::generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
1257 const GlobalVariable *GVar, bool IsAma) {
1258 BTFFieldReloc FieldReloc;
1259 FieldReloc.Label = ORSym;
1260 FieldReloc.TypeID = RootId;
1261
1262 StringRef AccessPattern = GVar->getName();
1263 size_t FirstDollar = AccessPattern.find_first_of('$');
1264 if (IsAma) {
1265 size_t FirstColon = AccessPattern.find_first_of(':');
1266 size_t SecondColon = AccessPattern.find_first_of(':', FirstColon + 1);
1267 StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1);
1268 StringRef RelocKindStr = AccessPattern.substr(FirstColon + 1,
1269 SecondColon - FirstColon);
1270 StringRef PatchImmStr = AccessPattern.substr(SecondColon + 1,
1271 FirstDollar - SecondColon);
1272
1273 FieldReloc.OffsetNameOff = addString(IndexPattern);
1274 FieldReloc.RelocKind = std::stoull(std::string(RelocKindStr));
1275 PatchImms[GVar] = std::make_pair(std::stoll(std::string(PatchImmStr)),
1276 FieldReloc.RelocKind);
1277 } else {
1278 StringRef RelocStr = AccessPattern.substr(FirstDollar + 1);
1279 FieldReloc.OffsetNameOff = addString("0");
1280 FieldReloc.RelocKind = std::stoull(std::string(RelocStr));
1281 PatchImms[GVar] = std::make_pair(RootId, FieldReloc.RelocKind);
1282 }
1283 FieldRelocTable[SecNameOff].push_back(FieldReloc);
1284}
1285
1286void BTFDebug::processGlobalValue(const MachineOperand &MO) {
1287 // check whether this is a candidate or not
1288 if (MO.isGlobal()) {
1289 const GlobalValue *GVal = MO.getGlobal();
1290 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1291 if (!GVar) {
1292 // Not a global variable. Maybe an extern function reference.
1293 processFuncPrototypes(dyn_cast<Function>(GVal));
1294 return;
1295 }
1296
1299 return;
1300
1301 MCSymbol *ORSym = OS.getContext().createTempSymbol();
1302 OS.emitLabel(ORSym);
1303
1304 MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1305 uint32_t RootId = populateType(dyn_cast<DIType>(MDN));
1306 generatePatchImmReloc(ORSym, RootId, GVar,
1308 }
1309}
1310
1313
1314 if (SkipInstruction || MI->isMetaInstruction() ||
1315 MI->getFlag(MachineInstr::FrameSetup))
1316 return;
1317
1318 if (MI->isInlineAsm()) {
1319 // Count the number of register definitions to find the asm string.
1320 unsigned NumDefs = 0;
1321 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
1322 ++NumDefs)
1323 ;
1324
1325 // Skip this inline asm instruction if the asmstr is empty.
1326 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1327 if (AsmStr[0] == 0)
1328 return;
1329 }
1330
1331 if (MI->getOpcode() == BPF::LD_imm64) {
1332 // If the insn is "r2 = LD_imm64 @<an AmaAttr global>",
1333 // add this insn into the .BTF.ext FieldReloc subsection.
1334 // Relocation looks like:
1335 // . SecName:
1336 // . InstOffset
1337 // . TypeID
1338 // . OffSetNameOff
1339 // . RelocType
1340 // Later, the insn is replaced with "r2 = <offset>"
1341 // where "<offset>" equals to the offset based on current
1342 // type definitions.
1343 //
1344 // If the insn is "r2 = LD_imm64 @<an TypeIdAttr global>",
1345 // The LD_imm64 result will be replaced with a btf type id.
1346 processGlobalValue(MI->getOperand(1));
1347 } else if (MI->getOpcode() == BPF::CORE_MEM ||
1348 MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1349 MI->getOpcode() == BPF::CORE_SHIFT) {
1350 // relocation insn is a load, store or shift insn.
1351 processGlobalValue(MI->getOperand(3));
1352 } else if (MI->getOpcode() == BPF::JAL) {
1353 // check extern function references
1354 const MachineOperand &MO = MI->getOperand(0);
1355 if (MO.isGlobal()) {
1356 processFuncPrototypes(dyn_cast<Function>(MO.getGlobal()));
1357 }
1358 }
1359
1360 if (!CurMI) // no debug info
1361 return;
1362
1363 // Skip this instruction if no DebugLoc or the DebugLoc
1364 // is the same as the previous instruction.
1365 const DebugLoc &DL = MI->getDebugLoc();
1366 if (!DL || PrevInstLoc == DL) {
1367 // This instruction will be skipped, no LineInfo has
1368 // been generated, construct one based on function signature.
1369 if (LineInfoGenerated == false) {
1370 auto *S = MI->getMF()->getFunction().getSubprogram();
1371 MCSymbol *FuncLabel = Asm->getFunctionBegin();
1372 constructLineInfo(S, FuncLabel, S->getLine(), 0);
1373 LineInfoGenerated = true;
1374 }
1375
1376 return;
1377 }
1378
1379 // Create a temporary label to remember the insn for lineinfo.
1380 MCSymbol *LineSym = OS.getContext().createTempSymbol();
1381 OS.emitLabel(LineSym);
1382
1383 // Construct the lineinfo.
1384 auto SP = DL->getScope()->getSubprogram();
1385 constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
1386
1387 LineInfoGenerated = true;
1388 PrevInstLoc = DL;
1389}
1390
1391void BTFDebug::processGlobals(bool ProcessingMapDef) {
1392 // Collect all types referenced by globals.
1393 const Module *M = MMI->getModule();
1394 for (const GlobalVariable &Global : M->globals()) {
1395 // Decide the section name.
1396 StringRef SecName;
1397 std::optional<SectionKind> GVKind;
1398
1399 if (!Global.isDeclarationForLinker())
1401
1402 if (Global.isDeclarationForLinker())
1403 SecName = Global.hasSection() ? Global.getSection() : "";
1404 else if (GVKind->isCommon())
1405 SecName = ".bss";
1406 else {
1408 MCSection *Sec = TLOF->SectionForGlobal(&Global, Asm->TM);
1409 SecName = Sec->getName();
1410 }
1411
1412 if (ProcessingMapDef != SecName.startswith(".maps"))
1413 continue;
1414
1415 // Create a .rodata datasec if the global variable is an initialized
1416 // constant with private linkage and if it won't be in .rodata.str<#>
1417 // and .rodata.cst<#> sections.
1418 if (SecName == ".rodata" && Global.hasPrivateLinkage() &&
1419 DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1420 // skip .rodata.str<#> and .rodata.cst<#> sections
1421 if (!GVKind->isMergeableCString() && !GVKind->isMergeableConst()) {
1422 DataSecEntries[std::string(SecName)] =
1423 std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1424 }
1425 }
1426
1428 Global.getDebugInfo(GVs);
1429
1430 // No type information, mostly internal, skip it.
1431 if (GVs.size() == 0)
1432 continue;
1433
1434 uint32_t GVTypeId = 0;
1435 DIGlobalVariable *DIGlobal = nullptr;
1436 for (auto *GVE : GVs) {
1437 DIGlobal = GVE->getVariable();
1438 if (SecName.startswith(".maps"))
1439 visitMapDefType(DIGlobal->getType(), GVTypeId);
1440 else
1441 visitTypeEntry(DIGlobal->getType(), GVTypeId, false, false);
1442 break;
1443 }
1444
1445 // Only support the following globals:
1446 // . static variables
1447 // . non-static weak or non-weak global variables
1448 // . weak or non-weak extern global variables
1449 // Whether DataSec is readonly or not can be found from corresponding ELF
1450 // section flags. Whether a BTF_KIND_VAR is a weak symbol or not
1451 // can be found from the corresponding ELF symbol table.
1452 auto Linkage = Global.getLinkage();
1453 if (Linkage != GlobalValue::InternalLinkage &&
1454 Linkage != GlobalValue::ExternalLinkage &&
1455 Linkage != GlobalValue::WeakAnyLinkage &&
1456 Linkage != GlobalValue::WeakODRLinkage &&
1458 continue;
1459
1460 uint32_t GVarInfo;
1461 if (Linkage == GlobalValue::InternalLinkage) {
1462 GVarInfo = BTF::VAR_STATIC;
1463 } else if (Global.hasInitializer()) {
1464 GVarInfo = BTF::VAR_GLOBAL_ALLOCATED;
1465 } else {
1466 GVarInfo = BTF::VAR_GLOBAL_EXTERNAL;
1467 }
1468
1469 auto VarEntry =
1470 std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1471 uint32_t VarId = addType(std::move(VarEntry));
1472
1473 processDeclAnnotations(DIGlobal->getAnnotations(), VarId, -1);
1474
1475 // An empty SecName means an extern variable without section attribute.
1476 if (SecName.empty())
1477 continue;
1478
1479 // Find or create a DataSec
1480 if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1481 DataSecEntries[std::string(SecName)] =
1482 std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1483 }
1484
1485 // Calculate symbol size
1486 const DataLayout &DL = Global.getParent()->getDataLayout();
1487 uint32_t Size = DL.getTypeAllocSize(Global.getValueType());
1488
1489 DataSecEntries[std::string(SecName)]->addDataSecEntry(VarId,
1490 Asm->getSymbol(&Global), Size);
1491 }
1492}
1493
1494/// Emit proper patchable instructions.
1496 if (MI->getOpcode() == BPF::LD_imm64) {
1497 const MachineOperand &MO = MI->getOperand(1);
1498 if (MO.isGlobal()) {
1499 const GlobalValue *GVal = MO.getGlobal();
1500 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1501 if (GVar) {
1502 // Emit "mov ri, <imm>"
1503 int64_t Imm;
1504 uint32_t Reloc;
1507 Imm = PatchImms[GVar].first;
1508 Reloc = PatchImms[GVar].second;
1509 } else {
1510 return false;
1511 }
1512
1517 OutMI.setOpcode(BPF::LD_imm64);
1518 else
1519 OutMI.setOpcode(BPF::MOV_ri);
1520 OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1521 OutMI.addOperand(MCOperand::createImm(Imm));
1522 return true;
1523 }
1524 }
1525 } else if (MI->getOpcode() == BPF::CORE_MEM ||
1526 MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1527 MI->getOpcode() == BPF::CORE_SHIFT) {
1528 const MachineOperand &MO = MI->getOperand(3);
1529 if (MO.isGlobal()) {
1530 const GlobalValue *GVal = MO.getGlobal();
1531 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1532 if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1533 uint32_t Imm = PatchImms[GVar].first;
1534 OutMI.setOpcode(MI->getOperand(1).getImm());
1535 if (MI->getOperand(0).isImm())
1536 OutMI.addOperand(MCOperand::createImm(MI->getOperand(0).getImm()));
1537 else
1538 OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1539 OutMI.addOperand(MCOperand::createReg(MI->getOperand(2).getReg()));
1540 OutMI.addOperand(MCOperand::createImm(Imm));
1541 return true;
1542 }
1543 }
1544 }
1545 return false;
1546}
1547
1548void BTFDebug::processFuncPrototypes(const Function *F) {
1549 if (!F)
1550 return;
1551
1552 const DISubprogram *SP = F->getSubprogram();
1553 if (!SP || SP->isDefinition())
1554 return;
1555
1556 // Do not emit again if already emitted.
1557 if (!ProtoFunctions.insert(F).second)
1558 return;
1559
1560 uint32_t ProtoTypeId;
1561 const std::unordered_map<uint32_t, StringRef> FuncArgNames;
1562 visitSubroutineType(SP->getType(), false, FuncArgNames, ProtoTypeId);
1563 uint32_t FuncId = processDISubprogram(SP, ProtoTypeId, BTF::FUNC_EXTERN);
1564
1565 if (F->hasSection()) {
1566 StringRef SecName = F->getSection();
1567
1568 if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1569 DataSecEntries[std::string(SecName)] =
1570 std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1571 }
1572
1573 // We really don't know func size, set it to 0.
1574 DataSecEntries[std::string(SecName)]->addDataSecEntry(FuncId,
1575 Asm->getSymbol(F), 0);
1576 }
1577}
1578
1580 // Collect MapDef globals if not collected yet.
1581 if (MapDefNotCollected) {
1582 processGlobals(true);
1583 MapDefNotCollected = false;
1584 }
1585
1586 // Collect global types/variables except MapDef globals.
1587 processGlobals(false);
1588
1589 for (auto &DataSec : DataSecEntries)
1590 addType(std::move(DataSec.second));
1591
1592 // Fixups
1593 for (auto &Fixup : FixupDerivedTypes) {
1594 const DICompositeType *CTy = Fixup.first;
1595 StringRef TypeName = CTy->getName();
1596 bool IsUnion = CTy->getTag() == dwarf::DW_TAG_union_type;
1597
1598 // Search through struct types
1599 uint32_t StructTypeId = 0;
1600 for (const auto &StructType : StructTypes) {
1601 if (StructType->getName() == TypeName) {
1602 StructTypeId = StructType->getId();
1603 break;
1604 }
1605 }
1606
1607 if (StructTypeId == 0) {
1608 auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
1609 StructTypeId = addType(std::move(FwdTypeEntry));
1610 }
1611
1612 for (auto &TypeInfo : Fixup.second) {
1613 const DIDerivedType *DTy = TypeInfo.first;
1614 BTFTypeDerived *BDType = TypeInfo.second;
1615
1616 int TmpTypeId = genBTFTypeTags(DTy, StructTypeId);
1617 if (TmpTypeId >= 0)
1618 BDType->setPointeeType(TmpTypeId);
1619 else
1620 BDType->setPointeeType(StructTypeId);
1621 }
1622 }
1623
1624 // Complete BTF type cross refereences.
1625 for (const auto &TypeEntry : TypeEntries)
1626 TypeEntry->completeType(*this);
1627
1628 // Emit BTF sections.
1629 emitBTFSection();
1630 emitBTFExtSection();
1631}
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
typename CallsiteContextGraph< DerivedCCG, FuncTy, CallTy >::FuncInfo FuncInfo
static bool isReg(const MCInst &MI, unsigned OpNo)
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:84
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:663
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:87
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:278
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:675
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:1239
BTFDebug(AsmPrinter *AP)
Definition: BTFDebug.cpp:506
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
Definition: BTFDebug.cpp:1311
bool InstLower(const MachineInstr *MI, MCInst &OutMI)
Emit proper patchable instructions.
Definition: BTFDebug.cpp:1495
size_t addString(StringRef S)
Add string to the string table.
Definition: BTFDebug.h:413
uint32_t getArrayIndexTypeId()
Get the special array index type id.
Definition: BTFDebug.h:407
uint32_t getTypeId(const DIType *Ty)
Get the type id for a particular DIType.
Definition: BTFDebug.h:416
void endModule() override
Complete all the types and emit the BTF sections.
Definition: BTFDebug.cpp:1579
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
Definition: BTFDebug.cpp:1163
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:151
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:110
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:1725
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1354
@ 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
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
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:950
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1303
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
bool contains(StringRef Key) const
contains - Return true if the element is in the map, false otherwise.
Definition: StringMap.h:253
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:569
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:375
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:651
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: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: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
@ MAX_VLEN
Max # of struct/union/enum members or func args.
Definition: BTF.h:92
@ VERSION
Definition: BTF.h:56
@ MAGIC
Definition: BTF.h:56
@ 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
@ FUNC_STATIC
Definition: BTF.h:193
@ FUNC_EXTERN
Definition: BTF.h:195
@ FUNC_GLOBAL
Definition: BTF.h:194
@ INT_SIGNED
Definition: BTF.h:138
@ INT_BOOL
Definition: BTF.h:140
@ SHT_PROGBITS
Definition: ELF.h:1000
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
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:1777
@ 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
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