1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#![allow(dead_code)]

use std::{
    convert::AsRef,
    fs::File,
    io::{BufReader, Cursor, Read},
    path::Path,
    sync::Arc,
};

use anyhow::{bail, Result};

use crate::cbtf;
use crate::obj::BtfObj;

/// Main representation of a parsed BTF object. Provides helpers to resolve
/// types and their associated names.
pub struct Btf {
    obj: Arc<BtfObj>,
    base: Option<Arc<BtfObj>>,
}

impl Btf {
    /// Parse a stand-alone BTF object file and construct a Rust representation for later
    /// use. Trying to open split BTF files using this function will fail. For split BTF
    /// files use `Btf::from_split_file()`.
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Btf> {
        Ok(Btf {
            obj: Arc::new(BtfObj::from_reader(
                &mut BufReader::new(File::open(path)?),
                None,
            )?),
            base: None,
        })
    }

    /// Parse a split BTF object file and construct a Rust representation for later
    /// use. A base Btf object must be provided.
    pub fn from_split_file<P: AsRef<Path>>(path: P, base: &Btf) -> Result<Btf> {
        if !path.as_ref().is_file() {
            bail!("Invalid BTF file {}", path.as_ref().display());
        }

        Ok(Btf {
            obj: Arc::new(BtfObj::from_reader(
                &mut BufReader::new(File::open(path)?),
                Some(base.obj.clone()),
            )?),
            base: Some(base.obj.clone()),
        })
    }

    /// Performs the same actions as from_file(), but fed with a byte slice.
    pub fn from_bytes(bytes: &[u8]) -> Result<Btf> {
        Ok(Btf {
            obj: Arc::new(BtfObj::from_reader(&mut Cursor::new(bytes), None)?),
            base: None,
        })
    }

    /// Performs the same actions as from_split_file(), but fed with a byte slice.
    pub fn from_split_bytes(bytes: &[u8], base: &Btf) -> Result<Btf> {
        let base = base.obj.clone();
        Ok(Btf {
            obj: Arc::new(BtfObj::from_reader(
                &mut Cursor::new(bytes),
                Some(base.clone()),
            )?),
            base: Some(base),
        })
    }

    /// Find a BTF id using its name as a key.
    pub fn resolve_id_by_name(&self, name: &str) -> Result<u32> {
        match &self.base {
            Some(base) => base
                .resolve_id_by_name(name)
                .or_else(|_| self.obj.resolve_id_by_name(name)),
            None => self.obj.resolve_id_by_name(name),
        }
    }

    /// Find a BTF type using its id as a key.
    pub fn resolve_type_by_id(&self, id: u32) -> Result<Type> {
        match &self.base {
            Some(base) => base
                .resolve_type_by_id(id)
                .or_else(|_| self.obj.resolve_type_by_id(id)),
            None => self.obj.resolve_type_by_id(id),
        }
    }

    /// Find a BTF type using its name as a key.
    pub fn resolve_type_by_name(&self, name: &str) -> Result<Type> {
        match &self.base {
            Some(base) => base
                .resolve_type_by_name(name)
                .or_else(|_| self.obj.resolve_type_by_name(name)),
            None => self.obj.resolve_type_by_name(name),
        }
    }

    /// Resolve a name referenced by a Type which is defined in the current BTF
    /// object.
    pub fn resolve_name<T: BtfType>(&self, r#type: &T) -> Result<String> {
        match &self.base {
            Some(base) => base
                .resolve_name(r#type)
                .or_else(|_| self.obj.resolve_name(r#type)),
            None => self.obj.resolve_name(r#type),
        }
    }

    /// Types can have a reference to another one, e.g. `Ptr -> Int`. This
    /// helper resolve a Type referenced in an other one. It is the main helper
    /// to traverse the Type tree.
    pub fn resolve_chained_type<T: BtfType>(&self, r#type: &T) -> Result<Type> {
        self.resolve_type_by_id(r#type.get_type_id()?)
    }
}

/// Rust representation of BTF types. Each type then contains its own specific
/// data and provides helpers to access it.
#[derive(Clone, Debug)]
pub enum Type {
    Void,
    Int(Int),
    Ptr(Ptr),
    Array(Array),
    Struct(Struct),
    Union(Union),
    Enum(Enum),
    Fwd(Fwd),
    Typedef(Typedef),
    Volatile(Volatile),
    Const(Const),
    Restrict(Restrict),
    Func(Func),
    FuncProto(FuncProto),
    Var(Var),
    Datasec(Datasec),
    Float(Float),
    DeclTag(DeclTag),
    TypeTag(TypeTag),
    Enum64(Enum64),
}

impl Type {
    pub fn name(&self) -> &'static str {
        match &self {
            Type::Void => "void",
            Type::Int(_) => "int",
            Type::Ptr(_) => "ptr",
            Type::Array(_) => "array",
            Type::Struct(_) => "struct",
            Type::Union(_) => "union",
            Type::Enum(_) => "enum",
            Type::Fwd(_) => "fwd",
            Type::Typedef(_) => "typedef",
            Type::Volatile(_) => "volatile",
            Type::Const(_) => "const",
            Type::Restrict(_) => "restrict",
            Type::Func(_) => "func",
            Type::FuncProto(_) => "func-proto",
            Type::Var(_) => "var",
            Type::Datasec(_) => "datasec",
            Type::Float(_) => "float",
            Type::DeclTag(_) => "decl-tag",
            Type::TypeTag(_) => "type-tag",
            Type::Enum64(_) => "enum64",
        }
    }
}

pub trait BtfType {
    fn get_name_offset(&self) -> Result<u32> {
        bail!("No name offset in type");
    }

    fn get_type_id(&self) -> Result<u32> {
        bail!("No type offset in type");
    }
}

/// Rust representation for BTF type `BTF_KIND_INT`.
#[derive(Clone, Debug)]
pub struct Int {
    btf_type: cbtf::btf_type,
    btf_int: cbtf::btf_int,
}

impl Int {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<Int> {
        Ok(Int {
            btf_type,
            btf_int: cbtf::btf_int::from_reader(reader, endianness)?,
        })
    }

    pub fn is_signed(&self) -> bool {
        self.btf_int.encoding() & cbtf::BTF_INT_SIGNED == cbtf::BTF_INT_SIGNED
    }

    pub fn is_char(&self) -> bool {
        self.btf_int.encoding() & cbtf::BTF_INT_CHAR == cbtf::BTF_INT_CHAR
    }

    pub fn is_bool(&self) -> bool {
        self.btf_int.encoding() & cbtf::BTF_INT_BOOL == cbtf::BTF_INT_BOOL
    }

    pub fn size(&self) -> usize {
        self.btf_type.size()
    }
}

impl BtfType for Int {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }
}

/// Rust representation for BTF type `BTF_KIND_PTR`.
#[derive(Clone, Debug)]
pub struct Ptr {
    btf_type: cbtf::btf_type,
}

impl Ptr {
    pub(super) fn new(btf_type: cbtf::btf_type) -> Ptr {
        Ptr { btf_type }
    }
}

impl BtfType for Ptr {
    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_type.r#type())
    }
}

/// Rust representation for BTF type `BTF_KIND_ARRAY`.
#[derive(Clone, Debug)]
pub struct Array {
    btf_type: cbtf::btf_type,
    btf_array: cbtf::btf_array,
}

impl Array {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<Array> {
        Ok(Array {
            btf_type,
            btf_array: cbtf::btf_array::from_reader(reader, endianness)?,
        })
    }
}

impl BtfType for Array {
    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_type.r#type())
    }
}

/// Rust representation for BTF type `BTF_KIND_STRUCT`.
#[derive(Clone, Debug)]
pub struct Struct {
    btf_type: cbtf::btf_type,
    pub members: Vec<Member>,
}

impl Struct {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<Struct> {
        let mut members = Vec::new();

        for _ in 0..btf_type.vlen() {
            members.push(Member::from_reader(
                reader,
                endianness,
                btf_type.kind_flag(),
            )?);
        }

        Ok(Struct { btf_type, members })
    }

    pub fn size(&self) -> usize {
        self.btf_type.size()
    }
}

impl BtfType for Struct {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }
}

/// Rust representation for BTF type `BTF_KIND_UNION`.
pub type Union = Struct;

/// Represents a [`Struct`] member.
#[derive(Clone, Debug)]
pub struct Member {
    kind_flag: u32,
    btf_member: cbtf::btf_member,
}

impl Member {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        kind_flag: u32,
    ) -> Result<Member> {
        Ok(Member {
            kind_flag,
            btf_member: cbtf::btf_member::from_reader(reader, endianness)?,
        })
    }

    pub fn bit_offset(&self) -> u32 {
        match self.kind_flag {
            1 => self.btf_member.offset & 0xffffff,
            _ => self.btf_member.offset,
        }
    }

    pub fn bitfield_size(&self) -> Option<u32> {
        match self.kind_flag {
            1 => Some(self.btf_member.offset >> 24),
            _ => None,
        }
    }
}

impl BtfType for Member {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_member.name_off)
    }

    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_member.r#type)
    }
}

/// Rust representation for BTF type `BTF_KIND_ENUM`.
#[derive(Clone, Debug)]
pub struct Enum {
    btf_type: cbtf::btf_type,
    pub members: Vec<EnumMember>,
}

#[allow(clippy::len_without_is_empty)]
impl Enum {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<Enum> {
        let mut members = Vec::new();

        for _ in 0..btf_type.vlen() {
            members.push(EnumMember::from_reader(reader, endianness)?);
        }

        Ok(Enum { btf_type, members })
    }

    pub fn is_signed(&self) -> bool {
        self.btf_type.kind_flag() == 1
    }

    pub fn len(&self) -> usize {
        self.btf_type.vlen() as usize
    }

    pub fn size(&self) -> usize {
        self.btf_type.size()
    }
}

impl BtfType for Enum {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }
}

/// Represents an [`Enum`] member.
#[derive(Clone, Debug)]
pub struct EnumMember {
    btf_enum: cbtf::btf_enum,
}

impl EnumMember {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
    ) -> Result<EnumMember> {
        Ok(EnumMember {
            btf_enum: cbtf::btf_enum::from_reader(reader, endianness)?,
        })
    }

    pub fn val(&self) -> i32 {
        self.btf_enum.val
    }
}

impl BtfType for EnumMember {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_enum.name_off)
    }
}

/// Rust representation for BTF type `BTF_KIND_FWD`.
#[derive(Clone, Debug)]
pub struct Fwd {
    btf_type: cbtf::btf_type,
}

impl Fwd {
    pub(super) fn new(btf_type: cbtf::btf_type) -> Fwd {
        Fwd { btf_type }
    }
}

impl BtfType for Fwd {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }
}

/// Rust representation for BTF type `BTF_KIND_TYPEDEF`.
#[derive(Clone, Debug)]
pub struct Typedef {
    btf_type: cbtf::btf_type,
}

impl Typedef {
    pub(super) fn new(btf_type: cbtf::btf_type) -> Typedef {
        Typedef { btf_type }
    }
}

impl BtfType for Typedef {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }

    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_type.r#type())
    }
}

/// Rust representation for BTF type `BTF_KIND_TYPE_TAG`.
pub type TypeTag = Typedef;

/// Rust representation for BTF type `BTF_KIND_VOLATILE`.
#[derive(Clone, Debug)]
pub struct Volatile {
    btf_type: cbtf::btf_type,
}

impl Volatile {
    pub(super) fn new(btf_type: cbtf::btf_type) -> Volatile {
        Volatile { btf_type }
    }
}

impl BtfType for Volatile {
    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_type.r#type())
    }
}

/// Rust representation for BTF type `BTF_KIND_CONST`.
pub type Const = Volatile;

/// Rust representation for BTF type `BTF_KIND_RESTRICT`.
pub type Restrict = Volatile;

/// Rust representation for BTF type `BTF_KIND_FUNC`.
#[derive(Clone, Debug)]
pub struct Func {
    btf_type: cbtf::btf_type,
}

impl Func {
    pub(super) fn new(btf_type: cbtf::btf_type) -> Func {
        Func { btf_type }
    }

    pub fn is_static(&self) -> bool {
        self.btf_type.vlen() == cbtf::BTF_FUNC_STATIC
    }

    pub fn is_global(&self) -> bool {
        self.btf_type.vlen() == cbtf::BTF_FUNC_GLOBAL
    }

    pub fn is_extern(&self) -> bool {
        self.btf_type.vlen() == cbtf::BTF_FUNC_EXTERN
    }
}

impl BtfType for Func {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }

    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_type.r#type())
    }
}

/// Rust representation for BTF type `BTF_KIND_FUNC_PROTO`.
#[derive(Clone, Debug)]
pub struct FuncProto {
    btf_type: cbtf::btf_type,
    pub parameters: Vec<Parameter>,
}

impl FuncProto {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<FuncProto> {
        let mut parameters = Vec::new();

        for _ in 0..btf_type.vlen() {
            parameters.push(Parameter::from_reader(reader, endianness)?);
        }

        Ok(FuncProto {
            btf_type,
            parameters,
        })
    }

    pub fn return_type_id(&self) -> u32 {
        self.btf_type.r#type()
    }
}

/// Represents a [`FuncProto`] parameter.
#[derive(Clone, Debug)]
pub struct Parameter {
    btf_param: cbtf::btf_param,
}

impl Parameter {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
    ) -> Result<Parameter> {
        Ok(Parameter {
            btf_param: cbtf::btf_param::from_reader(reader, endianness)?,
        })
    }

    pub fn is_variadic(&self) -> bool {
        self.btf_param.name_off == 0 && self.btf_param.r#type == 0
    }
}

impl BtfType for Parameter {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_param.name_off)
    }

    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_param.r#type)
    }
}

/// Rust representation for BTF type `BTF_KIND_VAR`.
#[derive(Clone, Debug)]
pub struct Var {
    btf_type: cbtf::btf_type,
    btf_var: cbtf::btf_var,
}

impl Var {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<Var> {
        Ok(Var {
            btf_type,
            btf_var: cbtf::btf_var::from_reader(reader, endianness)?,
        })
    }

    pub fn is_static(&self) -> bool {
        self.btf_var.linkage == 0
    }

    pub fn is_global(&self) -> bool {
        self.btf_var.linkage == 1
    }
}

impl BtfType for Var {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }

    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_type.r#type())
    }
}

/// Rust representation for BTF type `BTF_KIND_DATASEC`.
#[derive(Clone, Debug)]
pub struct Datasec {
    btf_type: cbtf::btf_type,
    pub variables: Vec<VarSecinfo>,
}

impl Datasec {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<Datasec> {
        let mut variables = Vec::new();

        for _ in 0..btf_type.vlen() {
            variables.push(VarSecinfo::from_reader(reader, endianness)?);
        }

        Ok(Datasec {
            btf_type,
            variables,
        })
    }
}

impl BtfType for Datasec {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }
}

/// Represents a [`Datasec`] variable.
#[derive(Clone, Debug)]
pub struct VarSecinfo {
    btf_var_secinfo: cbtf::btf_var_secinfo,
}

impl VarSecinfo {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
    ) -> Result<VarSecinfo> {
        Ok(VarSecinfo {
            btf_var_secinfo: cbtf::btf_var_secinfo::from_reader(reader, endianness)?,
        })
    }

    pub fn offset(&self) -> u32 {
        self.btf_var_secinfo.offset
    }

    pub fn size(&self) -> usize {
        self.btf_var_secinfo.size as usize
    }
}

impl BtfType for VarSecinfo {
    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_var_secinfo.r#type)
    }
}

/// Rust representation for BTF type `BTF_KIND_FLOAT`.
#[derive(Clone, Debug)]
pub struct Float {
    btf_type: cbtf::btf_type,
}

impl Float {
    pub(super) fn new(btf_type: cbtf::btf_type) -> Float {
        Float { btf_type }
    }

    pub fn size(&self) -> usize {
        self.btf_type.size()
    }
}

impl BtfType for Float {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }
}

/// Rust representation for BTF type `BTF_KIND_DECL_TAG`.
#[derive(Clone, Debug)]
pub struct DeclTag {
    btf_type: cbtf::btf_type,
    btf_decl_tag: cbtf::btf_decl_tag,
}

impl DeclTag {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<DeclTag> {
        Ok(DeclTag {
            btf_type,
            btf_decl_tag: cbtf::btf_decl_tag::from_reader(reader, endianness)?,
        })
    }

    pub fn component_index(&self) -> Option<u32> {
        let component_idx = self.btf_decl_tag.component_idx;
        match component_idx {
            x if x < 0 => None,
            x => Some(x as u32),
        }
    }
}

impl BtfType for DeclTag {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }

    fn get_type_id(&self) -> Result<u32> {
        Ok(self.btf_type.r#type())
    }
}

/// Rust representation for BTF type `BTF_KIND_ENUM64`.
#[derive(Clone, Debug)]
pub struct Enum64 {
    btf_type: cbtf::btf_type,
    pub members: Vec<Enum64Member>,
}

#[allow(clippy::len_without_is_empty)]
impl Enum64 {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
        btf_type: cbtf::btf_type,
    ) -> Result<Enum64> {
        let mut members = Vec::new();

        for _ in 0..btf_type.vlen() {
            members.push(Enum64Member::from_reader(reader, endianness)?);
        }

        Ok(Enum64 { btf_type, members })
    }

    pub fn is_signed(&self) -> bool {
        self.btf_type.kind_flag() == 1
    }

    pub fn len(&self) -> usize {
        self.btf_type.vlen() as usize
    }

    pub fn size(&self) -> usize {
        self.btf_type.size()
    }
}

impl BtfType for Enum64 {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_type.name_off)
    }
}

/// Represents an [`Enum64`] member.
#[derive(Clone, Debug)]
pub struct Enum64Member {
    btf_enum64: cbtf::btf_enum64,
}

impl Enum64Member {
    pub(super) fn from_reader<R: Read>(
        reader: &mut R,
        endianness: &cbtf::Endianness,
    ) -> Result<Enum64Member> {
        Ok(Enum64Member {
            btf_enum64: cbtf::btf_enum64::from_reader(reader, endianness)?,
        })
    }

    pub fn val(&self) -> u64 {
        (self.btf_enum64.val_hi32 as u64) << 32 | self.btf_enum64.val_lo32 as u64
    }
}

impl BtfType for Enum64Member {
    fn get_name_offset(&self) -> Result<u32> {
        Ok(self.btf_enum64.name_off)
    }
}