btf 0.5.1

A library for parsing the BPF type format (BTF).
Documentation
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
use crate::{Error, Result};

use byteorder::{BigEndian, ByteOrder, LittleEndian, ReadBytesExt};

use std::collections::HashMap;
use std::io::{BufRead, BufReader, Cursor, Read, Seek, SeekFrom};
use std::marker::PhantomData;
use std::path::Path;

/// Represents a parsed BTF file header (struct btf_header).
#[derive(Clone, Copy, Debug, Default)]
struct Header<B> {
    _version: u8,
    _flags: u8,
    hdr_len: u32,

    /* all offsets are in bytes relative to the end of this header */
    type_off: u32,
    type_len: u32,
    str_off: u32,
    _str_len: u32,

    endianess: PhantomData<B>,
}

impl<B: ByteOrder> Header<B> {
    /// Parses a BTF header.
    ///
    /// # Arguments
    /// * `reader` - The reader from which the header is read.
    fn from_reader<R: Read>(reader: &mut R) -> Result<Self> {
        Ok(Self {
            _version: reader.read_u8()?,
            _flags: reader.read_u8()?,
            hdr_len: reader.read_u32::<B>()?,
            type_off: reader.read_u32::<B>()?,
            type_len: reader.read_u32::<B>()?,
            str_off: reader.read_u32::<B>()?,
            _str_len: reader.read_u32::<B>()?,
            endianess: PhantomData,
        })
    }

    /// Read a string from the reader using the header as a reference.
    ///
    /// # Arguments
    /// * `reader` - The reader from which the string is read.
    /// * `offset` - The offset of the string.
    fn read_string<R: Read + Seek + BufRead>(
        &self,
        reader: &mut R,
        offset: u32,
    ) -> Result<Option<String>> {
        let oldpos = reader.stream_position()?;
        reader.seek(SeekFrom::Start(
            self.hdr_len as u64 + self.str_off as u64 + offset as u64,
        ))?;

        let mut raw_str = vec![];
        reader.read_until(0, &mut raw_str)?;
        raw_str.pop().ok_or(Error::Parsing {
            offset: reader.stream_position()?,
            message: "Reading raw string returned an empty buffer",
        })?;
        reader.seek(SeekFrom::Start(oldpos))?;

        match std::str::from_utf8(&raw_str) {
            Ok(s) => Ok(Some(s.into())),
            Err(_) => Err(Error::Parsing {
                offset: reader.stream_position()?,
                message: "Failed to decode string",
            }),
        }
    }

    /// Reads the type section of the BTF blob contained in reader.
    ///
    /// # Arguments
    /// * `reader` - The reader from which the types are read.
    fn read_types<R: Read + Seek + BufRead>(&self, reader: &mut R) -> Result<Vec<ParsedType>> {
        reader.seek(SeekFrom::Start(self.hdr_len as u64 + self.type_off as u64))?;
        let start_pos = reader.stream_position()?;
        let end_pos = start_pos + self.type_len as u64;

        let mut types = vec![ParsedType::default()];
        loop {
            let type_header = TypeHeader::from_reader::<B, _>(reader, self)?;
            let ty = Type::from_reader::<B, _>(reader, &type_header, self)?;
            types.push(ParsedType {
                header: type_header,
                ty,
            });

            match reader.stream_position()? {
                pos if pos > end_pos => {
                    return Err(Error::Parsing {
                        offset: reader.stream_position()?,
                        message: "Type length didn't match end of file",
                    });
                }
                pos if pos == end_pos => break,
                _ => (),
            }
        }

        Ok(types)
    }
}

/// All types representable by BTF.
#[derive(Clone, Copy, Debug, Default)]
enum TypeKind {
    #[default]
    Void,
    Integer,
    Pointer,
    Array,
    Struct,
    Union,
    Enum32,
    Fwd,
    Typedef,
    Volatile,
    Const,
    Restrict,
    Function,
    FunctionProto,
    Variable,
    DataSection,
    Float,
    DeclTag,
    TypeTag,
    Enum64,
}

/// Represents a parsed BTF type header (struct type_header).
#[derive(Clone, Debug, Default)]
struct TypeHeader {
    /// "name_off" parsed for convenience into a Rust String.
    name: Option<String>,

    /// "info" encoded bits.
    ///
    /// bits  0-15: vlen (e.g. # of struct's members)
    /// bits 16-23: unused
    /// bits 24-28: kind (e.g. int, ptr, array...etc)
    /// bits 29-30: unused
    /// bit     31: kind_flag, currently used by
    ///             struct, union, fwd, enum and enum64.
    ///
    info: u32,

    /// "size"/"type" is a union field.
    ///
    /// "size" is used by INT, ENUM, STRUCT, UNION and ENUM64.
    /// "size" tells the size of the type it is describing.
    ///
    /// "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
    /// FUNC, FUNC_PROTO, DECL_TAG and TYPE_TAG.
    /// "type" is a type_id referring to another type.
    ///
    size_type: u32,
}

impl TypeHeader {
    /// Parses a BTF type header. This is the C `struct bpf_type`, type.
    ///
    /// # Arguments
    /// * `reader` - The reader from which the type is read.
    /// * `header` - The header associated with this type.
    fn from_reader<B: ByteOrder, R: Read + Seek + BufRead>(
        reader: &mut R,
        header: &Header<B>,
    ) -> Result<TypeHeader> {
        let name_off = reader.read_u32::<B>()?;
        let name = header.read_string(reader, name_off)?;
        let info = reader.read_u32::<B>()?;
        let size_type = reader.read_u32::<B>()?;

        Ok(Self {
            name,
            info,
            size_type,
        })
    }

    /// Returns the kind field, see the documentation for this structure.
    fn get_kind(&self) -> Result<TypeKind> {
        match (self.info >> 24) & 0x1f {
            x if x == TypeKind::Void as u32 => Ok(TypeKind::Void),
            x if x == TypeKind::Integer as u32 => Ok(TypeKind::Integer),
            x if x == TypeKind::Pointer as u32 => Ok(TypeKind::Pointer),
            x if x == TypeKind::Array as u32 => Ok(TypeKind::Array),
            x if x == TypeKind::Struct as u32 => Ok(TypeKind::Struct),
            x if x == TypeKind::Union as u32 => Ok(TypeKind::Union),
            x if x == TypeKind::Enum32 as u32 => Ok(TypeKind::Enum32),
            x if x == TypeKind::Fwd as u32 => Ok(TypeKind::Fwd),
            x if x == TypeKind::Typedef as u32 => Ok(TypeKind::Typedef),
            x if x == TypeKind::Volatile as u32 => Ok(TypeKind::Volatile),
            x if x == TypeKind::Const as u32 => Ok(TypeKind::Const),
            x if x == TypeKind::Restrict as u32 => Ok(TypeKind::Restrict),
            x if x == TypeKind::Function as u32 => Ok(TypeKind::Function),
            x if x == TypeKind::FunctionProto as u32 => Ok(TypeKind::FunctionProto),
            x if x == TypeKind::Variable as u32 => Ok(TypeKind::Variable),
            x if x == TypeKind::DataSection as u32 => Ok(TypeKind::DataSection),
            x if x == TypeKind::Float as u32 => Ok(TypeKind::Float),
            x if x == TypeKind::DeclTag as u32 => Ok(TypeKind::DeclTag),
            x if x == TypeKind::TypeTag as u32 => Ok(TypeKind::TypeTag),
            x if x == TypeKind::Enum64 as u32 => Ok(TypeKind::Enum64),
            x => Err(Error::UnknownType { type_num: x }),
        }
    }

    /// Returns the vlen field, see the documentation for this structure.
    fn get_vlen(&self) -> u16 {
        self.info as u16
    }

    /// Returns the kind flag, see the documentation for this structure.
    fn get_kind_flag(&self) -> bool {
        ((self.info >> 31) & 0x1) == 0x1
    }

    /// Returns the type, see the documentation for this structure.
    fn get_type(&self) -> u32 {
        self.size_type
    }

    /// Returns the size, see the documentation for this structure.
    fn get_size(&self) -> u32 {
        self.size_type
    }
}

#[derive(Clone, Copy, Debug, Default)]
pub struct Integer {
    pub used_bits: u32,
    pub bits: u32,
    pub is_signed: bool,
    pub is_char: bool,
    pub is_bool: bool,
}

impl Integer {
    /// Reads a BTF encoded integer (BTF_KIND_INT).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The BTF type header that was read for this type.
    fn from_reader<B: ByteOrder, R: Read>(
        reader: &mut R,
        type_header: &TypeHeader,
    ) -> Result<Self> {
        let kind_specific = reader.read_u32::<B>()?;
        let bits = kind_specific as u8;
        let is_signed = (kind_specific >> 24) & 0x1 == 0x1;
        let is_char = (kind_specific >> 24) & 0x2 == 0x2;
        let is_bool = (kind_specific >> 24) & 0x4 == 0x4;
        Ok(Self {
            bits: type_header.size_type * 8,
            used_bits: bits.into(),
            is_signed,
            is_char,
            is_bool,
        })
    }
}

/// There are multiple BTF types that simply map to other types like:
/// Pointer, Typedef, etc.. This is used to represent those mappings.
#[derive(Clone, Copy, Debug, Default)]
pub struct TypeMap {
    pub type_id: u32,
}

impl TypeMap {
    /// Creates a type map from a given BTF type header.
    ///
    /// # Arguments
    /// * `type_header` - The BTF type header that was read for this type.
    fn from_type_header(type_header: &TypeHeader) -> Self {
        Self {
            type_id: type_header.get_type(),
        }
    }
}

/// Represents a parsed BTF array (struct btf_array).
#[derive(Clone, Copy, Debug, Default)]
pub struct Array {
    pub elem_type_id: u32,
    pub index_type_id: u32,
    pub num_elements: u32,
}

impl Array {
    /// Reads a BTF encoded array (struct btf_array).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    fn from_reader<B: ByteOrder, R: Read>(reader: &mut R) -> Result<Self> {
        Ok(Self {
            elem_type_id: reader.read_u32::<B>()?,
            index_type_id: reader.read_u32::<B>()?,
            num_elements: reader.read_u32::<B>()?,
        })
    }
}

/// Represents a parsed BTF structure member (struct btf_member).
#[derive(Clone, Debug, Default)]
pub struct StructMember {
    pub name: Option<String>,
    pub type_id: u32,
    pub offset: u32,
    pub bits: Option<u32>,
}

impl StructMember {
    /// Reads a BTF encoded structure member (struct btf_member).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The BTF type header that was read for this type.
    /// * `header` - The header associated with this type.
    fn from_reader<B: ByteOrder, R: Read + Seek + BufRead>(
        reader: &mut R,
        type_header: &TypeHeader,
        header: &Header<B>,
    ) -> Result<StructMember> {
        let name_off = reader.read_u32::<B>()?;
        let name = header.read_string(reader, name_off)?;
        let type_id = reader.read_u32::<B>()?;
        let offset_and_bits = reader.read_u32::<B>()?;

        let (offset, bits) = if type_header.get_kind_flag() {
            (offset_and_bits & 0xffffff, Some(offset_and_bits >> 24))
        } else {
            (offset_and_bits, None)
        };

        Ok(StructMember {
            name,
            type_id,
            offset,
            bits,
        })
    }
}

/// Represents a parsed BTF structure ([struct btf_member]).
#[derive(Clone, Debug, Default)]
pub struct Struct {
    pub members: Vec<StructMember>,
}

impl Struct {
    /// Reads a BTF encoded structure ([struct btf_member]).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The BTF type header that was read for this type.
    /// * `header` - The header associated with this type.
    fn from_reader<B: ByteOrder, R: Read + Seek + BufRead>(
        reader: &mut R,
        type_header: &TypeHeader,
        header: &Header<B>,
    ) -> Result<Self> {
        let num_members = type_header.get_vlen();
        let mut members = Vec::<StructMember>::with_capacity(num_members.into());
        for _ in 0..num_members {
            members.push(StructMember::from_reader::<B, _>(
                reader,
                type_header,
                header,
            )?);
        }

        Ok(Struct { members })
    }
}

/// Represents a parsed BTF enum member (struct btf_enum).
#[derive(Clone, Debug, Default)]
pub struct EnumEntry {
    pub name: Option<String>,
    pub value: i64,
}

impl EnumEntry {
    /// Reads a BTF encoded enum member (struct btf_enum).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `header` - The header associated with this type.
    fn from_reader<B: ByteOrder, R: Read + Seek + BufRead, const WIDE: bool>(
        reader: &mut R,
        header: &Header<B>,
    ) -> Result<Self> {
        let name_off = reader.read_u32::<B>()?;
        let name = header.read_string(reader, name_off)?;
        let value = if WIDE {
            reader.read_i32::<B>()? as i64 | ((reader.read_i32::<B>()? as i64) << 32)
        } else {
            reader.read_i32::<B>()? as i64
        };

        Ok(EnumEntry { name, value })
    }
}

/// Represents a parsed BTF enum ([struct btf_enum]).
#[derive(Clone, Debug, Default)]
pub struct Enum {
    pub is_signed: bool,
    pub entries: Vec<EnumEntry>,
}

impl Enum {
    /// Reads a BTF encoded enum member ([struct btf_enum]).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The BTF type header that was read for this type.
    /// * `header` - The header associated with this type.
    fn from_reader<B: ByteOrder, R: Read + Seek + BufRead>(
        reader: &mut R,
        type_header: &TypeHeader,
        header: &Header<B>,
    ) -> Result<Self> {
        let is_signed = type_header.get_kind_flag();
        let num_entries = type_header.get_vlen();
        let mut entries = Vec::<EnumEntry>::with_capacity(num_entries.into());
        match type_header.get_kind() {
            Ok(TypeKind::Enum32) => {
                for _ in 0..num_entries {
                    entries.push(EnumEntry::from_reader::<B, _, false>(reader, header)?);
                }
            }
            Ok(TypeKind::Enum64) => {
                for _ in 0..num_entries {
                    entries.push(EnumEntry::from_reader::<B, _, true>(reader, header)?);
                }
            }
            _ => return Err(Error::InvalidEnumTypeKind),
        };

        Ok(Self { is_signed, entries })
    }
}

/// Represents a parsed BTF forward-declaration.
#[derive(Clone, Copy, Debug, Default)]
pub enum Fwd {
    #[default]
    Struct,
    Union,
}

impl Fwd {
    /// Creates a forward-declaration type from a BTF type header.
    ///
    /// # Arguments
    /// * `type_header` - The BTF type header that was read for this type.
    fn from_type_header(type_header: &TypeHeader) -> Self {
        if type_header.get_kind_flag() {
            Self::Union
        } else {
            Self::Struct
        }
    }
}

#[derive(Clone, Copy, Debug, Default)]
pub enum LinkageKind {
    #[default]
    Static,
    Global,
}

/// Represents a parsed BTF function.
#[derive(Clone, Copy, Debug, Default)]
pub struct Function {
    pub linkage: LinkageKind,
    pub type_id: u32,
}

impl Function {
    /// Creates a function type from a raw type header.
    ///
    /// # Arguments
    /// * `type_header` - The BTF type header that was read for this type.
    fn from_type_header(type_header: &TypeHeader) -> Self {
        let linkage = if type_header.get_vlen() == 0 {
            LinkageKind::Static
        } else {
            LinkageKind::Global
        };

        Self {
            linkage,
            type_id: type_header.get_type(),
        }
    }
}

/// Represents a parsed BTF function parameter (struct btf_param).
#[derive(Clone, Debug, Default)]
pub struct FunctionParam {
    pub name: Option<String>,
    pub type_id: u32,
}

impl FunctionParam {
    /// Reads a BTF encoded function parameter (struct btf_param).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `header` - The header associated with this type.
    fn from_reader<B: ByteOrder, R: Read + Seek + BufRead>(
        reader: &mut R,
        header: &Header<B>,
    ) -> Result<Self> {
        let name_off = reader.read_u32::<B>()?;
        let name = header.read_string(reader, name_off)?;
        let type_id = reader.read_u32::<B>()?;

        Ok(Self { name, type_id })
    }
}

/// Represents a parsed BTF function prototype ([struct btf_param]).
#[derive(Clone, Debug, Default)]
pub struct FunctionProto {
    pub params: Vec<FunctionParam>,
}

impl FunctionProto {
    /// Reads a BTF encoded function ([struct btf_param]).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The BTF type header that was read for this type.
    /// * `header` - The header associated with this type.
    fn from_reader<B: ByteOrder, R: Read + Seek + BufRead>(
        reader: &mut R,
        type_header: &TypeHeader,
        header: &Header<B>,
    ) -> Result<Self> {
        let num_params = type_header.get_vlen();
        let mut params = Vec::with_capacity(num_params.into());
        for _ in 0..num_params {
            params.push(FunctionParam::from_reader::<B, _>(reader, header)?);
        }

        Ok(Self { params })
    }
}

/// Represents a parsed BTF variable type (struct btf_var).
#[derive(Clone, Copy, Debug, Default)]
pub struct Variable {
    pub type_id: u32,
    pub linkage: LinkageKind,
}

impl Variable {
    /// Reads a BTF encoded variable (struct btf_var).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The type header associated with the variable.
    fn from_reader<B: ByteOrder, R: Read>(
        reader: &mut R,
        type_header: &TypeHeader,
    ) -> Result<Self> {
        let linkage = match reader.read_u32::<B>()? {
            0 => LinkageKind::Static,
            1 => LinkageKind::Global,
            _ => return Err(Error::InvalidLinkageKind),
        };

        Ok(Self {
            type_id: type_header.get_type(),
            linkage,
        })
    }
}

/// Represents a parsed BTF section variable (struct btf_var_secinfo).
#[derive(Clone, Copy, Debug, Default)]
pub struct SectionVariable {
    pub type_id: u32,
    pub offset: u32,
    pub size: u32,
}

impl SectionVariable {
    /// Reads a BTF encoded section variable (struct btf_var_secinfo).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    fn from_reader<B: ByteOrder, R: Read>(reader: &mut R) -> Result<Self> {
        Ok(Self {
            type_id: reader.read_u32::<B>()?,
            offset: reader.read_u32::<B>()?,
            size: reader.read_u32::<B>()?,
        })
    }
}

/// Represents a parsed BTF data section ([struct btf_var_secinfo]).
#[derive(Clone, Debug, Default)]
pub struct DataSection {
    pub vars: Vec<SectionVariable>,
}

impl DataSection {
    /// Reads a BTF encoded data section ([struct btf_var_secinfo]).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The BTF type header that was read for this type.
    fn from_reader<B: ByteOrder, R: Read>(
        reader: &mut R,
        type_header: &TypeHeader,
    ) -> Result<Self> {
        let num_vars = type_header.get_vlen();
        let mut vars = Vec::with_capacity(num_vars.into());
        for _ in 0..num_vars {
            vars.push(SectionVariable::from_reader::<B, _>(reader)?)
        }

        Ok(Self { vars })
    }
}

/// Represents a parsed BTF float.
#[derive(Clone, Copy, Debug, Default)]
pub struct Float {
    pub bits: u32,
}

impl Float {
    /// Creates a float type from a raw type header.
    ///
    /// # Arguments
    /// * `type_header` - The BTF type header that was read for this type.
    fn from_type_header(type_header: &TypeHeader) -> Self {
        Self {
            bits: type_header.get_size() * 8,
        }
    }
}

/// Represents a parsed BTF decl tag.
#[derive(Clone, Copy, Debug, Default)]
pub struct DeclTag {
    pub type_id: u32,
    pub component_index: u32,
}

impl DeclTag {
    /// Reads a BTF encoded decl tag (struct btf_decl_tag).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The type header associated with the variable.
    fn from_reader<B: ByteOrder, R: Read>(
        reader: &mut R,
        type_header: &TypeHeader,
    ) -> Result<Self> {
        Ok(Self {
            type_id: type_header.get_type(),
            component_index: reader.read_u32::<B>()?,
        })
    }
}

/// Represents a parsed BTF type.
#[derive(Clone, Debug, Default)]
pub enum Type {
    #[default]
    Void,
    Integer(Integer),
    Pointer(TypeMap),
    Array(Array),
    Struct(Struct),
    Union(Struct),
    Enum32(Enum),
    Fwd(Fwd),
    Typedef(TypeMap),
    Volatile(TypeMap),
    Const(TypeMap),
    Restrict(TypeMap),
    Function(Function),
    FunctionProto(FunctionProto),
    Variable(Variable),
    DataSection(DataSection),
    Float(Float),
    DeclTag(DeclTag),
    TypeTag(TypeMap),
    Enum64(Enum),
}

impl Type {
    /// Parses a single BTF type from the reader (advances the reader past the type).
    ///
    /// # Arguments
    /// * `reader` - The reader from which the integer is read.
    /// * `type_header` - The BTF type header that was read for this type.
    /// * `header` - The header associated with this type.
    fn from_reader<B: ByteOrder, R: Read + Seek + BufRead>(
        reader: &mut R,
        type_header: &TypeHeader,
        header: &Header<B>,
    ) -> Result<Self> {
        match type_header.get_kind()? {
            TypeKind::Void => Ok(Self::Void),
            TypeKind::Integer => Ok(Self::Integer(Integer::from_reader::<B, _>(
                reader,
                type_header,
            )?)),
            TypeKind::Pointer => Ok(Self::Pointer(TypeMap::from_type_header(type_header))),
            TypeKind::Array => Ok(Self::Array(Array::from_reader::<B, _>(reader)?)),
            TypeKind::Struct => Ok(Self::Struct(Struct::from_reader::<B, _>(
                reader,
                type_header,
                header,
            )?)),
            TypeKind::Union => Ok(Self::Struct(Struct::from_reader::<B, _>(
                reader,
                type_header,
                header,
            )?)),
            TypeKind::Enum32 => Ok(Self::Enum32(Enum::from_reader::<B, _>(
                reader,
                type_header,
                header,
            )?)),
            TypeKind::Fwd => Ok(Self::Fwd(Fwd::from_type_header(type_header))),
            TypeKind::Typedef => Ok(Self::Typedef(TypeMap::from_type_header(type_header))),
            TypeKind::Volatile => Ok(Self::Volatile(TypeMap::from_type_header(type_header))),
            TypeKind::Const => Ok(Self::Const(TypeMap::from_type_header(type_header))),
            TypeKind::Restrict => Ok(Self::Restrict(TypeMap::from_type_header(type_header))),
            TypeKind::Function => Ok(Self::Function(Function::from_type_header(type_header))),
            TypeKind::FunctionProto => Ok(Self::FunctionProto(FunctionProto::from_reader::<B, _>(
                reader,
                type_header,
                header,
            )?)),
            TypeKind::Variable => Ok(Self::Variable(Variable::from_reader::<B, _>(
                reader,
                type_header,
            )?)),
            TypeKind::DataSection => Ok(Self::DataSection(DataSection::from_reader::<B, _>(
                reader,
                type_header,
            )?)),
            TypeKind::Float => Ok(Self::Float(Float::from_type_header(type_header))),
            TypeKind::DeclTag => Ok(Self::DeclTag(DeclTag::from_reader::<B, _>(
                reader,
                type_header,
            )?)),
            TypeKind::TypeTag => Ok(Self::TypeTag(TypeMap::from_type_header(type_header))),
            TypeKind::Enum64 => Ok(Self::Enum64(Enum::from_reader::<B, _>(
                reader,
                type_header,
                header,
            )?)),
        }
    }
}

/// Represents a single parsed BTF type, that is, the common header and
/// type-specific information.
#[derive(Clone, Debug, Default)]
struct ParsedType {
    header: TypeHeader,
    ty: Type,
}

/// A type that's been resolved to its base type with attributes as fields.
#[derive(Clone, Debug, Default)]
pub struct FlattenedType {
    pub type_id: u32,
    pub bits: u32,
    pub base_type: Type,
    pub num_refs: u32,
    pub name: Option<String>,
    pub type_tags: Vec<String>,
    pub decl_tags: Vec<(u32, String)>,
    pub is_volatile: bool,
    pub is_const: bool,
    pub is_restrict: bool,
    pub is_function: bool,
    pub linkage: LinkageKind,
}

impl FlattenedType {
    const MAX_INDIRECTIONS: usize = 100;

    /// Helper for finding the total size of a parsed type.
    fn get_parsed_type_bits(types: &[ParsedType], id: u32, mut indirections: usize) -> Result<u32> {
        indirections += 1;
        if indirections == Self::MAX_INDIRECTIONS {
            return Err(Error::TypeLoop);
        }

        let flattened_type = Self::from_parsed_types(types, id)?;
        if flattened_type.num_refs > 0 {
            return Ok(64); // Treat all pointers as 64 bits for now.
        }

        let bits = match flattened_type.base_type {
            Type::Integer(t) => t.bits,
            Type::Array(t) => {
                let element_bits = Self::get_parsed_type_bits(types, t.elem_type_id, indirections)?;
                t.num_elements * element_bits
            }
            Type::Struct(t) | Type::Union(t) => {
                let mut bits = 0;
                for member in &t.members {
                    let member_bits =
                        Self::get_parsed_type_bits(types, member.type_id, indirections)?;
                    if member.offset + member_bits > bits {
                        bits = member.offset + member_bits;
                    }
                }
                bits
            }
            Type::Enum32(_) => 32,
            Type::Enum64(_) => 64,
            Type::DataSection(t) => {
                let mut bits = 0;
                for vars in &t.vars {
                    if vars.offset + vars.size * 8 > bits {
                        bits = vars.offset + vars.size * 8;
                    }
                }
                bits
            }
            Type::Void | Type::Fwd(_) | Type::FunctionProto(_) => 0,
            Type::Float(t) => t.bits,
            _ => {
                return Err(Error::InternalError {
                    message: "FlattenedType has non base type.",
                })
            }
        };

        Ok(bits)
    }

    /// Flattens a type by its type id. Since BTF types are chained together,
    /// the type info needs to be traversed to find the base type. This function
    /// traverses the function, starting at `id` and returns the base type.
    ///
    /// # Arguments
    ///
    /// * `types` - The array of parsed types where index is the type id.
    /// * `id` - The id of the type to flatten.
    fn from_parsed_types(types: &[ParsedType], id: u32) -> Result<Self> {
        let mut index: usize = id.try_into()?;
        let mut num_refs = 0;
        let mut name = None;
        let mut type_tags = vec![];
        let mut decl_tags = vec![];
        let mut is_volatile = false;
        let mut is_const = false;
        let mut is_restrict = false;
        let mut is_function = false;
        let mut linkage = LinkageKind::Static;
        let mut base_type: &Type;

        let mut i = 0;
        loop {
            // Prevent type loops.
            i += 1;
            if i == Self::MAX_INDIRECTIONS {
                return Err(Error::TypeLoop);
            }

            let ty = types.get(index).ok_or(Error::InvalidTypeIndex)?;
            base_type = &ty.ty;
            match base_type {
                Type::Void
                | Type::Integer(_)
                | Type::Array(_)
                | Type::Struct(_)
                | Type::Union(_)
                | Type::Enum32(_)
                | Type::Enum64(_)
                | Type::Fwd(_)
                | Type::FunctionProto(_)
                | Type::DataSection(_)
                | Type::Float(_) => {
                    // only use the base's name, if it's not part of a chained type
                    if name.is_none() && i == 1 {
                        name = ty.header.name.clone();
                    }
                    break;
                }
                Type::Pointer(t) => {
                    num_refs += 1;
                    index = t.type_id.try_into()?;
                }
                Type::Typedef(t) => {
                    name = ty.header.name.clone();
                    index = t.type_id.try_into()?;
                }
                Type::Volatile(t) => {
                    is_volatile = true;
                    index = t.type_id.try_into()?;
                }
                Type::Const(t) => {
                    is_const = true;
                    index = t.type_id.try_into()?;
                }
                Type::Restrict(t) => {
                    is_restrict = true;
                    index = t.type_id.try_into()?;
                }
                Type::Function(t) => {
                    name = ty.header.name.clone();
                    is_function = true;
                    index = t.type_id.try_into()?;
                }
                Type::Variable(t) => {
                    name = ty.header.name.clone();
                    linkage = t.linkage;
                    index = t.type_id.try_into()?;
                }
                Type::TypeTag(t) => {
                    if let Some(name) = &ty.header.name {
                        type_tags.push(name.clone());
                    }
                    index = t.type_id.try_into()?;
                }
                Type::DeclTag(t) => {
                    if let Some(name) = &ty.header.name {
                        decl_tags.push((t.component_index, name.clone()));
                    }
                    index = t.type_id.try_into()?;
                }
            }
        }

        Ok(Self {
            type_id: id,
            bits: 0,
            base_type: base_type.clone(),
            num_refs,
            name,
            type_tags,
            decl_tags,
            is_volatile,
            is_const,
            is_restrict,
            is_function,
            linkage,
        })
    }
}

/// Represents a deserialized BTF file.
#[derive(Clone, Debug, Default)]
pub struct Btf {
    types: Vec<FlattenedType>,
    name_map: HashMap<String, u32>,
}

impl Btf {
    /// Reads the header using a specified endianess. This is called in `from_reader`
    /// after endianess has been determined.
    ///
    /// # Arguments
    ///
    /// * `reader` - A reader interface into the BTF data.
    fn inner_from_reader<B: ByteOrder, R: BufRead + Seek>(mut reader: R) -> Result<Self> {
        let header = Header::<B>::from_reader(&mut reader)?;
        let types = header.read_types(&mut reader)?;

        let mut name_map = HashMap::default();
        let mut flattened_types = vec![];
        for id in 0..types.len() {
            let index = id.try_into()?;
            let mut flattened_type = FlattenedType::from_parsed_types(&types, index)?;
            flattened_type.bits = FlattenedType::get_parsed_type_bits(&types, index, 0)?;
            if let Some(name) = &flattened_type.name {
                name_map.insert(name.clone(), index);
            }
            flattened_types.push(flattened_type);
        }

        Ok(Btf {
            types: flattened_types,
            name_map,
        })
    }

    /// Parses a BTF file into a vector of types.
    ///
    /// # Arguments
    ///
    /// * `path` - The path to the BTF file.
    ///
    /// # Example
    /// ```
    /// use btf::btf::Btf;
    ///
    /// let btf = Btf::from_file("/sys/kernel/btf/vmlinux").expect("failed to parse btf");
    /// ```
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        /*
         * Arbitrary threshold of 50 MB to limit memory usage when parsing. If the
         * file is over 50MB the file is used to seek/parse, otherwise all data is
         * read into memory and then parsed. The latter is much quicker.
         */
        let meta = std::fs::metadata(&path)?;
        if meta.len() > 50 << 20 {
            let mut reader = BufReader::new(std::fs::File::open(&path)?);
            let magic = reader.read_u16::<LittleEndian>()?;
            match magic {
                0xeb9f => Self::inner_from_reader::<LittleEndian, _>(reader),
                0x9feb => Self::inner_from_reader::<BigEndian, _>(reader),
                _ => Err(Error::Parsing {
                    offset: reader.stream_position()?,
                    message: "Invalid magic value",
                }),
            }
        } else {
            let data = std::fs::read(path)?;
            let mut reader = Cursor::new(data);
            let magic = reader.read_u16::<LittleEndian>()?;
            match magic {
                0xeb9f => Self::inner_from_reader::<LittleEndian, _>(reader),
                0x9feb => Self::inner_from_reader::<BigEndian, _>(reader),
                _ => Err(Error::Parsing {
                    offset: reader.stream_position()?,
                    message: "Invalid magic value",
                }),
            }
        }
    }

    /// Returns a slice of the internal types.
    ///
    /// # Example
    /// ```
    /// use btf::btf::{Btf, FlattenedType};
    ///
    /// let btf = Btf::from_file("/sys/kernel/btf/vmlinux").expect("failed to parse btf");
    /// let types: Vec<&FlattenedType> = btf.get_types().iter().collect();
    /// assert!(types.len() > 0);
    /// ```
    pub fn get_types(&self) -> &[FlattenedType] {
        &self.types
    }

    /// Retrieves a type by its identifier.
    ///
    /// # Arguments
    ///
    /// * `id` - The id of the type to flatten.
    ///
    /// # Example
    /// ```
    /// use btf::btf::Btf;
    ///
    /// let btf = Btf::from_file("/sys/kernel/btf/vmlinux").expect("failed to parse btf");
    /// btf.get_type_by_id(0).expect("Type 0 not found");
    /// ```
    pub fn get_type_by_id(&self, id: u32) -> Result<&FlattenedType> {
        let index: usize = id.try_into()?;
        self.types.get(index).ok_or(Error::InvalidTypeIndex)
    }

    /// Retrieves a type by its name.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the type.
    ///
    /// # Example
    /// ```
    /// use btf::btf::Btf;
    ///
    /// let btf = Btf::from_file("/sys/kernel/btf/vmlinux").expect("failed to parse btf");
    /// btf.get_type_by_name("task_struct").expect("task_struct not found");
    /// ```
    pub fn get_type_by_name(&self, name: &str) -> Result<&FlattenedType> {
        let index = *self.name_map.get(name).ok_or(Error::TypeNotFound)?;
        let index: usize = index.try_into()?;
        self.types.get(index).ok_or(Error::InvalidTypeIndex)
    }
}