@@ -43,7 +43,7 @@ void BTFTypeBase::emitType(MCStreamer &OS) {
43
43
44
44
BTFTypeDerived::BTFTypeDerived (const DIDerivedType *DTy, unsigned Tag,
45
45
bool NeedsFixup)
46
- : DTy(DTy), NeedsFixup(NeedsFixup) {
46
+ : DTy(DTy), NeedsFixup(NeedsFixup), Name(DTy-> getName ()) {
47
47
switch (Tag) {
48
48
case dwarf::DW_TAG_pointer_type:
49
49
Kind = BTF::BTF_KIND_PTR;
@@ -66,14 +66,23 @@ BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
66
66
BTFType.Info = Kind << 24 ;
67
67
}
68
68
69
+ // / Used by DW_TAG_pointer_type only.
70
+ BTFTypeDerived::BTFTypeDerived (unsigned NextTypeId, unsigned Tag,
71
+ StringRef Name)
72
+ : DTy(nullptr ), NeedsFixup(false ), Name(Name) {
73
+ Kind = BTF::BTF_KIND_PTR;
74
+ BTFType.Info = Kind << 24 ;
75
+ BTFType.Type = NextTypeId;
76
+ }
77
+
69
78
void BTFTypeDerived::completeType (BTFDebug &BDebug) {
70
79
if (IsCompleted)
71
80
return ;
72
81
IsCompleted = true ;
73
82
74
- BTFType.NameOff = BDebug.addString (DTy-> getName () );
83
+ BTFType.NameOff = BDebug.addString (Name );
75
84
76
- if (NeedsFixup)
85
+ if (NeedsFixup || !DTy )
77
86
return ;
78
87
79
88
// The base type for PTR/CONST/VOLATILE could be void.
@@ -408,17 +417,31 @@ void BTFTypeDeclTag::emitType(MCStreamer &OS) {
408
417
OS.emitInt32 (Info);
409
418
}
410
419
411
- BTFTypeTypeTag::BTFTypeTypeTag (uint32_t BaseTypeId, StringRef Tag) : Tag(Tag) {
420
+ BTFTypeTypeTag::BTFTypeTypeTag (uint32_t NextTypeId, StringRef Tag)
421
+ : DTy(nullptr ), Tag(Tag) {
422
+ Kind = BTF::BTF_KIND_TYPE_TAG;
423
+ BTFType.Info = Kind << 24 ;
424
+ BTFType.Type = NextTypeId;
425
+ }
426
+
427
+ BTFTypeTypeTag::BTFTypeTypeTag (const DIDerivedType *DTy, StringRef Tag)
428
+ : DTy(DTy), Tag(Tag) {
412
429
Kind = BTF::BTF_KIND_TYPE_TAG;
413
430
BTFType.Info = Kind << 24 ;
414
- BTFType.Type = BaseTypeId;
415
431
}
416
432
417
433
void BTFTypeTypeTag::completeType (BTFDebug &BDebug) {
418
434
if (IsCompleted)
419
435
return ;
420
436
IsCompleted = true ;
421
437
BTFType.NameOff = BDebug.addString (Tag);
438
+ if (DTy) {
439
+ const DIType *ResolvedType = DTy->getBaseType ();
440
+ if (!ResolvedType)
441
+ BTFType.Type = 0 ;
442
+ else
443
+ BTFType.Type = BDebug.getTypeId (ResolvedType);
444
+ }
422
445
}
423
446
424
447
uint32_t BTFStringTable::addString (StringRef S) {
@@ -675,6 +698,8 @@ void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
675
698
SmallVector<const MDString *, 4 > MDStrs;
676
699
DINodeArray Annots = DTy->getAnnotations ();
677
700
if (Annots) {
701
+ // For type with "int __tag1 __tag2 *p", the MDStrs will have
702
+ // content: [__tag1, __tag2].
678
703
for (const Metadata *Annotations : Annots->operands ()) {
679
704
const MDNode *MD = cast<MDNode>(Annotations);
680
705
const MDString *Name = cast<MDString>(MD->getOperand (0 ));
@@ -685,20 +710,22 @@ void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
685
710
}
686
711
687
712
if (MDStrs.size () > 0 ) {
688
- auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false );
713
+ // With MDStrs [__tag1, __tag2], the output type chain looks like
714
+ // PTR -> __tag2 -> __tag1 -> BaseType
715
+ // In the below, we construct BTF types with the order of __tag1, __tag2
716
+ // and PTR.
717
+ auto TypeEntry =
718
+ std::make_unique<BTFTypeTypeTag>(DTy, MDStrs[0 ]->getString ());
689
719
unsigned TmpTypeId = addType (std::move (TypeEntry));
690
- for (unsigned I = MDStrs.size (); I > 0 ; I--) {
691
- const MDString *Value = MDStrs[I - 1 ];
692
- if (I != 1 ) {
693
- auto TypeEntry =
694
- std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString ());
695
- TmpTypeId = addType (std::move (TypeEntry));
696
- } else {
697
- auto TypeEntry =
698
- std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString ());
699
- TypeId = addType (std::move (TypeEntry), DTy);
700
- }
720
+ for (unsigned I = 1 ; I < MDStrs.size (); I++) {
721
+ const MDString *Value = MDStrs[I];
722
+ TypeEntry =
723
+ std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString ());
724
+ TmpTypeId = addType (std::move (TypeEntry));
701
725
}
726
+ auto TypeDEntry =
727
+ std::make_unique<BTFTypeDerived>(TmpTypeId, Tag, DTy->getName ());
728
+ TypeId = addType (std::move (TypeDEntry), DTy);
702
729
} else {
703
730
auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false );
704
731
TypeId = addType (std::move (TypeEntry), DTy);
0 commit comments