libbpf-cargo 0.26.2

Cargo plugin to build bpf programs
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
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
mod btf;

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::ffi::c_void;
use std::ffi::CString;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::fmt::Write as fmt_write;
use std::fs::File;
use std::io::stdout;
use std::io::ErrorKind;
use std::io::Write;
use std::mem::size_of;
use std::os::raw::c_ulong;
use std::path::Path;
use std::process::Command;
use std::process::Stdio;
use std::ptr;

use anyhow::bail;
use anyhow::ensure;
use anyhow::Context;
use anyhow::Result;

use libbpf_rs::btf::BtfType;
use libbpf_rs::btf::TypeId;
use libbpf_rs::libbpf_sys;
use libbpf_rs::AsRawLibbpf;
use libbpf_rs::Btf;
use libbpf_rs::Map;
use libbpf_rs::MapCore as _;
use libbpf_rs::MapIter;
use libbpf_rs::MapType;
use libbpf_rs::Object;
use libbpf_rs::Program;

use memmap2::Mmap;
use tracing::debug;
use tracing::info;

use crate::metadata;
use crate::metadata::UnprocessedObj;

pub(crate) use self::btf::GenBtf;
pub(crate) use self::btf::GenStructOps;

/// Escape certain characters in a "raw" name of a section, for example.
fn escape_raw_name(name: &str) -> String {
    name.replace('.', "_")
}

#[derive(Debug, PartialEq)]
pub(crate) enum InternalMapType<'name> {
    Data,
    CustomData(&'name str),
    Rodata,
    CustomRodata(&'name str),
    Bss,
    CustomBss(&'name str),
    Kconfig,
    StructOps,
}

impl Display for InternalMapType<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::Data => write!(f, "data"),
            Self::CustomData(name) => write!(f, "data_{}", escape_raw_name(name)),
            Self::Rodata => write!(f, "rodata"),
            Self::CustomRodata(name) => write!(f, "rodata_{}", escape_raw_name(name)),
            Self::Bss => write!(f, "bss"),
            Self::CustomBss(name) => write!(f, "bss_{}", escape_raw_name(name)),
            Self::Kconfig => write!(f, "kconfig"),
            Self::StructOps => write!(f, "struct_ops"),
        }
    }
}


/// Meta-data about a BPF map.
enum MapMeta {
    NonDatasec,
    Datasec { mmap_idx: usize, read_only: bool },
}

impl MapMeta {
    fn new(idx: usize, map: &Map<'_>) -> Self {
        if map_is_datasec(map) {
            Self::Datasec {
                mmap_idx: idx,
                read_only: map_is_readonly(map),
            }
        } else {
            Self::NonDatasec
        }
    }
}


/// Data about a single BPF map.
struct MapData {
    raw_name: String,
    name: String,
    meta: MapMeta,
}

impl MapData {
    fn new(idx: usize, map: &Map<'_>) -> Result<Option<Self>> {
        let raw_name = map.name();
        let raw_name = raw_name
            .to_str()
            .with_context(|| format!("map has invalid name: {raw_name:?}"))?
            .to_string();

        // TODO: Should work with `raw_name` here instead of retrieving it
        //       again internally.
        let name = if let Some(name) = get_map_name(map)? {
            name.to_string()
        } else {
            return Ok(None)
        };

        let slf = Self {
            raw_name,
            name,
            meta: MapMeta::new(idx, map),
        };
        Ok(Some(slf))
    }
}


/// Data pertaining BPF maps used as part of code generation.
struct MapsData {
    /// Vector of data about individual BPF maps, in the same order as
    /// they appear in the underlying object file.
    maps: Vec<MapData>,
}

impl MapsData {
    fn new(obj: &Object) -> Result<Self> {
        let maps = maps(obj)
            .enumerate()
            .filter_map(|(idx, map)| MapData::new(idx, &map).transpose())
            .collect::<Result<Vec<_>>>()?;
        let slf = Self { maps };
        Ok(slf)
    }

    fn iter(&self) -> impl Iterator<Item = &MapData> {
        self.maps.iter()
    }
}


/// Data pertaining BPF programs used as part of code generation.
struct ProgsData {
    /// Vector of names of individual BPF programs, in the same order as they
    /// appear in the underlying object file.
    progs: Vec<String>,
}

impl ProgsData {
    fn new(obj: &Object) -> Result<Self> {
        let progs = obj
            .progs()
            .map(|prog| get_prog_name(&prog))
            .collect::<Result<Vec<_>>>()?;
        let slf = Self { progs };
        Ok(slf)
    }

    fn iter(&self) -> impl Iterator<Item = &String> {
        self.progs.iter()
    }
}


pub(crate) enum OutputDest<'a> {
    Stdout,
    /// Infer a filename and place file in specified directory
    Directory(&'a Path),
    /// File to place output in
    File(&'a Path),
}

/// Try running `rustfmt` over `s` and return result.
///
/// If no `rustfmt` binary could be found the content is left untouched, as
/// it's only meant as a cosmetic brush up, without change of semantics.
fn try_rustfmt<'code>(s: &'code str, rustfmt_path: Option<&Path>) -> Result<Cow<'code, [u8]>> {
    let result = if let Some(r) = rustfmt_path {
        Command::new(r)
    } else {
        Command::new("rustfmt")
    }
    .stdin(Stdio::piped())
    .stdout(Stdio::piped())
    .spawn();

    match result {
        Ok(mut cmd) => {
            // Send input in via stdin
            cmd.stdin.take().unwrap().write_all(s.as_bytes())?;

            // Extract output
            let output = cmd
                .wait_with_output()
                .context("Failed to execute rustfmt")?;
            ensure!(
                output.status.success(),
                "Failed to rustfmt: {}",
                output.status
            );

            Ok(output.stdout.into())
        }
        Err(err) if err.kind() == ErrorKind::NotFound => {
            // No `rustfmt` is present. Just skip formatting altogether.
            Ok(Cow::Borrowed(s.as_bytes()))
        }
        Err(err) => panic!("failed to spawn rustfmt: {err}"),
    }
}

fn capitalize_first_letter(s: &str) -> String {
    if s.is_empty() {
        return "".to_string();
    }

    s.split('_').fold(String::new(), |mut acc, ts| {
        acc += &ts.chars().next().unwrap().to_uppercase().to_string();
        if ts.len() > 1 {
            acc += &ts[1..];
        }
        acc
    })
}

fn get_raw_map_name(map: &Map<'_>) -> Result<String> {
    let name = map
        .name()
        .to_str()
        .context("map has invalid name")?
        .to_string();
    Ok(name)
}

pub(crate) fn canonicalize_internal_map_name(s: &str) -> Option<InternalMapType<'_>> {
    if s.ends_with(".data") {
        Some(InternalMapType::Data)
    } else if s.ends_with(".rodata") {
        Some(InternalMapType::Rodata)
    } else if s.ends_with(".bss") {
        Some(InternalMapType::Bss)
    } else if s.ends_with(".kconfig") {
        Some(InternalMapType::Kconfig)
    } else if s.ends_with(".struct_ops") {
        Some(InternalMapType::StructOps)
    } else if s.ends_with(".struct_ops.link") {
        // The `*.link` extension really only sets an additional flag in lower
        // layers. For our intents and purposes both can be treated similarly.
        Some(InternalMapType::StructOps)
    // Custom data sections don't prepend bpf_object name, so we can match from
    // start of name.
    // See https://github.com/libbpf/libbpf/blob/20ea95b4505c477af3b6ff6ce9d19cee868ddc5d/src/libbpf.c#L1789-L1794
    } else if s.starts_with(".data.") {
        let name = s.get(".data.".len()..).unwrap();
        Some(InternalMapType::CustomData(name))
    } else if s.starts_with(".rodata.") {
        let name = s.get(".rodata.".len()..).unwrap();
        Some(InternalMapType::CustomRodata(name))
    } else if s.starts_with(".bss.") {
        let name = s.get(".bss.".len()..).unwrap();
        Some(InternalMapType::CustomBss(name))
    } else {
        info!("encountered unrecognized map: {s}");
        None
    }
}

/// Same as `get_raw_map_name` except the name is canonicalized
fn get_map_name(map: &Map<'_>) -> Result<Option<String>> {
    let name = get_raw_map_name(map)?;

    if unsafe { !libbpf_sys::bpf_map__is_internal(map.as_libbpf_object().as_ptr()) } {
        Ok(Some(escape_raw_name(&name)))
    } else {
        Ok(canonicalize_internal_map_name(&name).map(|map| map.to_string()))
    }
}

fn get_prog_name(prog: &Program<'_>) -> Result<String> {
    let name = prog
        .name()
        .to_str()
        .context("program has invalid name")?
        .to_string();
    Ok(name)
}

fn map_is_mmapable(map: &Map<'_>) -> bool {
    let map_ptr = map.as_libbpf_object().as_ptr();
    (unsafe { libbpf_sys::bpf_map__map_flags(map_ptr) } & libbpf_sys::BPF_F_MMAPABLE) > 0
}

fn map_is_datasec(map: &Map<'_>) -> bool {
    let internal = unsafe { libbpf_sys::bpf_map__is_internal(map.as_libbpf_object().as_ptr()) };
    let mmapable = map_is_mmapable(map);

    internal && mmapable
}

fn map_is_readonly(map: &Map<'_>) -> bool {
    assert!(map_is_mmapable(map));

    let map_ptr = map.as_libbpf_object().as_ptr();
    // BPF_F_RDONLY_PROG means readonly from prog side
    (unsafe { libbpf_sys::bpf_map__map_flags(map_ptr) } & libbpf_sys::BPF_F_RDONLY_PROG) > 0
}

fn maps(object: &Object) -> impl Iterator<Item = Map<'_>> {
    // SAFETY: The pointer returned by `as_libbpf_object` is always valid.
    let obj = unsafe { object.as_libbpf_object().as_ref() };
    MapIter::new(obj)
        // SAFETY: Map iteration always yields valid objects.
        .filter(|ptr| unsafe { libbpf_sys::bpf_map__autocreate(ptr.as_ptr()) })
        // SAFETY: We never use the `AsFd` impl of the map.
        .map(|ptr| unsafe { Map::from_map_without_fd(ptr) })
}

fn gen_skel_c_skel_constructor(skel: &mut String, object: &Object, name: &str) -> Result<()> {
    write!(
        skel,
        "\
        fn build_skel_config() -> libbpf_rs::Result<libbpf_rs::__internal_skel::ObjectSkeletonConfig<'static>>
        {{
            let mut builder = libbpf_rs::__internal_skel::ObjectSkeletonConfigBuilder::new(&DATA);
            builder
                .name(\"{name}\")
        ",
    )?;

    for map in maps(object) {
        let raw_name = get_raw_map_name(&map)?;
        let mmaped = if map_is_mmapable(&map) {
            "true"
        } else {
            "false"
        };

        writeln!(skel, ".map(\"{raw_name}\", {mmaped})")?;
    }

    for prog in object.progs() {
        let name = get_prog_name(&prog)?;
        writeln!(skel, ".prog(\"{name}\")")?;
    }

    writeln!(skel, ";")?;

    write!(
        skel,
        "\
            builder.build()
        }}
        "
    )?;

    Ok(())
}

fn gen_skel_map_defs(
    skel: &mut String,
    maps: &MapsData,
    raw_obj_name: &str,
    open: bool,
) -> Result<()> {
    let prefix = if open { "Open" } else { "" };

    let obj_name = capitalize_first_letter(raw_obj_name);
    write!(
        skel,
        "\
                pub struct {prefix}{obj_name}Maps<'obj> {{
        ",
    )?;

    for map in maps.iter() {
        write!(
            skel,
            "\
                    pub {name}: libbpf_rs::{prefix}MapMut<'obj>,
            ",
            name = map.name
        )?;

        if let MapMeta::Datasec { read_only, .. } = map.meta {
            // After "open" all maps are writable. That's the point,
            // they can be modified.
            let ref_mut = if open || !read_only { " mut" } else { "" };
            write!(
                skel,
                "\
                    pub {name}_data: Option<&'obj{ref_mut} types::{name}>,
                ",
                name = map.name,
            )?;
        }
    }

    write!(
        skel,
        "\
                    _phantom: std::marker::PhantomData<&'obj ()>,
                }}

                impl<'obj> {prefix}{obj_name}Maps<'obj> {{
                    #[allow(unused_variables)]
                    unsafe fn new(
                        config: &libbpf_rs::__internal_skel::ObjectSkeletonConfig<'_>,
                        object: &mut libbpf_rs::{prefix}Object,
                    ) -> libbpf_rs::Result<Self> {{
        ",
    )?;

    for map in maps.iter() {
        writeln!(skel, "let mut {name} = None;", name = map.name)?;
    }

    write!(
        skel,
        "\
                        let object = unsafe {{
                            std::mem::transmute::<&mut libbpf_rs::{prefix}Object, &'obj mut libbpf_rs::{prefix}Object>(object)
                        }};
                        #[allow(clippy::never_loop)]
                        for map in object.maps_mut() {{
                            let name = map
                                .name()
                                .to_str()
                                .ok_or_else(|| {{
                                    libbpf_rs::Error::from(std::io::Error::new(
                                        std::io::ErrorKind::InvalidData,
                                        \"map has invalid name\",
                                    ))
                                }})?;
                            #[allow(clippy::match_single_binding)]
                            match name {{
        ",
    )?;

    for map in maps.iter() {
        write!(
            skel,
            "\
                                \"{raw_name}\" => {name} = Some(map),
            ",
            raw_name = map.raw_name,
            name = map.name
        )?;
    }

    write!(
        skel,
        "\
                                _ => panic!(\"encountered unexpected map: `{{name}}`\"),
                            }}
                        }}

                        let slf = Self {{
        ",
    )?;

    for map in maps.iter() {
        write!(
            skel,
            "\
                            {name}: {name}.expect(\"map `{name}` not present\"),
            ",
            name = map.name
        )?;

        if let MapMeta::Datasec {
            mmap_idx,
            read_only,
        } = map.meta
        {
            let ref_conv = if open || !read_only { "mut" } else { "ref" };
            write!(
                skel,
                "\
                            {name}_data: unsafe {{
                                config
                                    .map_mmap_ptr({mmap_idx})
                                    .expect(\"BPF map `{name}` does not have mmap pointer\")
                                    .cast::<types::{name}>()
                                    .as_{ref_conv}()
                            }},
                ",
                name = map.name,
            )?;
        }
    }

    write!(
        skel,
        "\
                            _phantom: std::marker::PhantomData,
                        }};
                        Ok(slf)
                    }}
                }}
        ",
    )?;
    Ok(())
}

fn gen_skel_open_prog_defs(skel: &mut String, progs: &ProgsData, raw_obj_name: &str) -> Result<()> {
    let obj_name = capitalize_first_letter(raw_obj_name);
    write!(
        skel,
        "\
                pub struct Open{obj_name}Progs<'obj> {{
        ",
    )?;

    for name in progs.iter() {
        write!(
            skel,
            "\
                    pub {name}: libbpf_rs::OpenProgramMut<'obj>,
            ",
        )?;
    }

    write!(
        skel,
        "\
                    _phantom: std::marker::PhantomData<&'obj ()>,
                }}

                impl<'obj> Open{obj_name}Progs<'obj> {{
                    unsafe fn new(
                        object: &mut libbpf_rs::OpenObject,
                    ) -> libbpf_rs::Result<Self> {{
        ",
    )?;

    for name in progs.iter() {
        writeln!(skel, "let mut {name} = None;")?;
    }

    write!(
        skel,
        "\
                        let object = unsafe {{
                            std::mem::transmute::<&mut libbpf_rs::OpenObject, &'obj mut libbpf_rs::OpenObject>(object)
                        }};
                        for prog in object.progs_mut() {{
                            let name = prog
                                .name()
                                .to_str()
                                .ok_or_else(|| {{
                                    libbpf_rs::Error::from(std::io::Error::new(
                                        std::io::ErrorKind::InvalidData,
                                        \"prog has invalid name\",
                                    ))
                                }})?;
                            match name {{
        ",
    )?;

    for name in progs.iter() {
        writeln!(skel, "      \"{name}\" => {name} = Some(prog),")?;
    }

    write!(
        skel,
        "\
                                _ => panic!(\"encountered unexpected prog: `{{name}}`\"),
                            }}
                        }}

                        let slf = Self {{
        ",
    )?;

    for name in progs.iter() {
        write!(
            skel,
            "\
                            {name}: {name}.expect(\"prog `{name}` not present\"),
            ",
        )?;
    }

    write!(
        skel,
        "\
                            _phantom: std::marker::PhantomData,
                        }};
                        Ok(slf)
                    }}
                }}
        ",
    )?;
    Ok(())
}

fn gen_skel_prog_defs(skel: &mut String, progs: &ProgsData, raw_obj_name: &str) -> Result<()> {
    let obj_name = capitalize_first_letter(raw_obj_name);
    write!(
        skel,
        "\
                pub struct {obj_name}Progs<'obj> {{
        ",
    )?;

    for name in progs.iter() {
        write!(
            skel,
            "\
                    pub {name}: libbpf_rs::ProgramMut<'obj>,
            ",
        )?;
    }

    write!(
        skel,
        "\
                    _phantom: std::marker::PhantomData<&'obj ()>,
                }}

                impl<'obj> {obj_name}Progs<'obj> {{
                    #[allow(unused_variables)]
                    fn new(open_progs: Open{obj_name}Progs<'obj>) -> Self {{
                        Self {{
        ",
    )?;

    for name in progs.iter() {
        write!(
            skel,
            "\
                            {name}: unsafe {{
                                libbpf_rs::ProgramMut::new_mut(open_progs.{name}.as_libbpf_object().as_mut())
                            }},
            ",
        )?;
    }

    write!(
        skel,
        "\
                            _phantom: std::marker::PhantomData,
                        }}
                    }}
                }}
        ",
    )?;
    Ok(())
}


fn gen_skel_types(
    skel: &mut String,
    btf: Option<&GenBtf<'_>>,
    processed: &mut HashSet<TypeId>,
) -> Result<()> {
    let btf = if let Some(btf) = btf {
        btf
    } else {
        return Ok(());
    };

    for ty_id in 1..btf.len() {
        let ty_id = TypeId::from(ty_id as u32);
        // SANITY: A type with this ID should always exist given that BTF IDs
        //         are fully populated up to `len`. Conversion to `BtfType` is
        //         always infallible.
        let ty = btf.type_by_id::<BtfType<'_>>(ty_id).unwrap();

        let sec_def = btf.type_definition(ty, processed)?;
        write!(skel, "{sec_def}")?;
    }
    Ok(())
}

fn gen_skel_struct_ops_getters(skel: &mut String, object: &Object) -> Result<()> {
    if maps(object).next().is_none() {
        return Ok(());
    }

    write!(
        skel,
        "\
        pub fn struct_ops_raw(&self) -> *const StructOps {{
            &self.struct_ops
        }}

        pub fn struct_ops(&self) -> &StructOps {{
            &self.struct_ops
        }}
        ",
    )?;

    Ok(())
}

fn gen_skel_link_defs(skel: &mut String, object: &Object, obj_name: &str) -> Result<()> {
    if object.progs().next().is_none() {
        return Ok(());
    }

    write!(
        skel,
        "\
        #[derive(Default)]
        pub struct {obj_name}Links {{
        ",
    )?;

    for prog in object.progs() {
        writeln!(
            skel,
            "pub {}: Option<libbpf_rs::Link>,",
            get_prog_name(&prog)?
        )?;
    }

    writeln!(skel, "}}")?;

    Ok(())
}

fn gen_skel_link_getter(skel: &mut String, object: &Object, obj_name: &str) -> Result<()> {
    if object.progs().next().is_none() {
        return Ok(());
    }

    writeln!(skel, "pub links: {obj_name}Links,")?;
    Ok(())
}

fn open_bpf_object(name: &str, data: &[u8]) -> Result<Object> {
    let cname = CString::new(name)?;
    let obj_opts = libbpf_sys::bpf_object_open_opts {
        sz: size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
        object_name: cname.as_ptr(),
        ..Default::default()
    };
    let object = unsafe {
        libbpf_sys::bpf_object__open_mem(
            data.as_ptr() as *const c_void,
            data.len() as c_ulong,
            &obj_opts,
        )
    };
    ensure!(!object.is_null(), "Failed to bpf_object__open_mem()");

    let obj = unsafe { Object::from_ptr(ptr::NonNull::new(object).unwrap()) };
    Ok(obj)
}

fn gen_skel_attach(skel: &mut String, object: &Object, obj_name: &str) -> Result<()> {
    if object.progs().next().is_none() {
        return Ok(());
    }

    write!(
        skel,
        "\
        fn attach(&mut self) -> libbpf_rs::Result<()> {{
            let skel_ptr = self.skel_config.as_libbpf_object().as_ptr();
            let ret = unsafe {{ libbpf_sys::bpf_object__attach_skeleton(skel_ptr) }};
            if ret != 0 {{
                return Err(libbpf_rs::Error::from_raw_os_error(-ret));
            }}

            self.links = {obj_name}Links {{
        ",
    )?;

    for (idx, prog) in object.progs().enumerate() {
        let prog_name = get_prog_name(&prog)?;

        write!(
            skel,
            "{prog_name}: core::ptr::NonNull::new(self.skel_config.prog_link_ptr({idx})?)
                        .map(|ptr| unsafe {{ libbpf_rs::Link::from_ptr(ptr) }}),
            "
        )?;
    }

    write!(
        skel,
        "
            }};

            Ok(())
        }}
        ",
    )?;

    Ok(())
}

fn gen_skel_struct_ops_init(object: &Object) -> Result<String> {
    let mut def = String::new();

    for map in maps(object) {
        let type_ = map.map_type();
        if type_ != MapType::StructOps {
            continue;
        }

        let raw_name = get_raw_map_name(&map)?;

        write!(
            def,
            "\
            skel.struct_ops.{raw_name} = skel.maps.{raw_name}.initial_value_mut().unwrap().as_mut_ptr().cast();
            ",
        )?;
    }
    Ok(def)
}

/// Generate contents of a single skeleton
fn gen_skel_contents(raw_obj_name: &str, obj_file_path: &Path, obj_ref: bool) -> Result<String> {
    let mut skel = String::new();

    write!(
        skel,
        "\
        // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
        //
        // THIS FILE IS AUTOGENERATED BY CARGO-LIBBPF-GEN!

        pub use self::imp::*;

        #[allow(renamed_and_removed_lints)]
        #[allow(dead_code)]
        #[allow(missing_docs)]
        #[allow(non_snake_case)]
        #[allow(non_camel_case_types)]
        #[allow(clippy::absolute_paths)]
        #[allow(clippy::upper_case_acronyms)]
        #[allow(clippy::use_self)]
        #[allow(clippy::zero_repeat_side_effects)]
        #[warn(single_use_lifetimes)]
        mod imp {{
        #[allow(unused_imports, clippy::wildcard_imports)]
        use super::*;
        use libbpf_rs::libbpf_sys;
        use libbpf_rs::skel::OpenSkel;
        use libbpf_rs::skel::Skel;
        use libbpf_rs::skel::SkelBuilder;
        use libbpf_rs::AsRawLibbpf as _;
        use libbpf_rs::MapCore as _;
        use libbpf_rs::ObjectBuilder;
        "
    )?;

    // The name we'll always hand to libbpf
    //
    // Note it's important this remains consistent b/c libbpf infers map/prog names from this name
    let libbpf_obj_name = format!("{raw_obj_name}_bpf");
    // We'll use `obj_name` as the rust-ified object name
    let obj_name = capitalize_first_letter(raw_obj_name);

    // Open bpf_object so we can iterate over maps and progs
    let file = File::open(obj_file_path)
        .with_context(|| format!("failed to open BPF object `{}`", obj_file_path.display()))?;
    let mmap = unsafe { Mmap::map(&file)? };
    let object = open_bpf_object(&libbpf_obj_name, &mmap)?;
    let btf =
        Btf::from_bpf_object(unsafe { object.as_libbpf_object().as_ref() })?.map(GenBtf::from);
    let maps = MapsData::new(&object)?;
    let progs = ProgsData::new(&object)?;

    gen_skel_c_skel_constructor(&mut skel, &object, &libbpf_obj_name)?;
    gen_skel_map_defs(&mut skel, &maps, raw_obj_name, true)?;
    gen_skel_map_defs(&mut skel, &maps, raw_obj_name, false)?;
    gen_skel_open_prog_defs(&mut skel, &progs, raw_obj_name)?;
    gen_skel_prog_defs(&mut skel, &progs, raw_obj_name)?;

    #[allow(clippy::uninlined_format_args)]
    write!(
        skel,
        "\
        struct OwnedRef<'obj, O> {{
            object: Option<&'obj mut std::mem::MaybeUninit<O>>,
        }}

        impl<'obj, O> OwnedRef<'obj, O> {{
            /// # Safety
            /// The object has to be initialized.
            unsafe fn new(object: &'obj mut std::mem::MaybeUninit<O>) -> Self {{
                Self {{
                    object: Some(object),
                }}
            }}

            fn as_ref(&self) -> &O {{
                // SAFETY: As per the contract during construction, the
                //         object has to be initialized.
                unsafe {{ self.object.as_ref().unwrap().assume_init_ref() }}
            }}

            fn as_mut(&mut self) -> &mut O {{
                // SAFETY: As per the contract during construction, the
                //         object has to be initialized.
                unsafe {{ self.object.as_mut().unwrap().assume_init_mut() }}
            }}

            fn take(mut self) -> &'obj mut std::mem::MaybeUninit<O> {{
                self.object.take().unwrap()
            }}
        }}

        impl<O> Drop for OwnedRef<'_, O> {{
            fn drop(&mut self) {{
                if let Some(object) = &mut self.object {{
                    unsafe {{ object.assume_init_drop() }}
                }}
            }}
        }}

        #[derive(Default)]
        pub struct {name}SkelBuilder {{
            pub obj_builder: libbpf_rs::ObjectBuilder,
        }}

        impl<'obj> {name}SkelBuilder {{
            fn open_opts_impl(
                self,
                open_opts: *const libbpf_sys::bpf_object_open_opts,
                object: &'obj mut std::mem::MaybeUninit<libbpf_rs::OpenObject>,
            ) -> libbpf_rs::Result<Open{name}Skel<'obj>> {{
                let skel_config = build_skel_config()?;
                let skel_ptr = skel_config.as_libbpf_object();

                let ret = unsafe {{ libbpf_sys::bpf_object__open_skeleton(skel_ptr.as_ptr(), open_opts) }};
                if ret != 0 {{
                    return Err(libbpf_rs::Error::from_raw_os_error(-ret));
                }}

                // SAFETY: `skel_ptr` points to a valid object after the
                //         open call.
                let obj_ptr = unsafe {{ *skel_ptr.as_ref().obj }};
                // SANITY: `bpf_object__open_skeleton` should have
                //         allocated the object.
                let obj_ptr = std::ptr::NonNull::new(obj_ptr).unwrap();
                // SAFETY: `obj_ptr` points to an opened object after
                //         skeleton open.
                let obj = unsafe {{ libbpf_rs::OpenObject::from_ptr(obj_ptr) }};
                let _obj = object.write(obj);
                // SAFETY: We just wrote initialized data to `object`.
                let mut obj_ref = unsafe {{ OwnedRef::new(object) }};

                #[allow(unused_mut)]
                let mut skel = Open{name}Skel {{
                    maps: unsafe {{ Open{name}Maps::new(&skel_config, obj_ref.as_mut())? }},
                    progs: unsafe {{ Open{name}Progs::new(obj_ref.as_mut())? }},
                    obj: obj_ref,
                    // SAFETY: Our `struct_ops` type contains only pointers,
                    //         which are allowed to be NULL.
                    // TODO: Generate and use a `Default` representation
                    //       instead, to cut down on unsafe code.
                    struct_ops: unsafe {{ std::mem::zeroed() }},
                    skel_config
                }};
                {struct_ops_init}
                Ok(skel)
            }}
        }}

        impl<'obj> SkelBuilder<'obj> for {name}SkelBuilder {{
            type Output = Open{name}Skel<'obj>;
            fn open(
                self,
                object: &'obj mut std::mem::MaybeUninit<libbpf_rs::OpenObject>,
            ) -> libbpf_rs::Result<Open{name}Skel<'obj>> {{
                // Only produce a pointer to our custom open opts object
                // if customizations have been made. This works around a
                // bug in older versions of libbpf.
                let opts = if self.obj_builder.ne(&ObjectBuilder::default()) {{
                    self.obj_builder.as_libbpf_object().as_ptr().cast_const()
                }} else {{
                    std::ptr::null()
                }};
                self.open_opts_impl(opts, object)
            }}

            fn open_opts(
                self,
                open_opts: libbpf_sys::bpf_object_open_opts,
                object: &'obj mut std::mem::MaybeUninit<libbpf_rs::OpenObject>,
            ) -> libbpf_rs::Result<Open{name}Skel<'obj>> {{
                self.open_opts_impl(&open_opts, object)
            }}

            fn object_builder(&self) -> &libbpf_rs::ObjectBuilder {{
                &self.obj_builder
            }}
            fn object_builder_mut(&mut self) -> &mut libbpf_rs::ObjectBuilder {{
                &mut self.obj_builder
            }}
        }}
        ",
        name = obj_name,
        struct_ops_init = gen_skel_struct_ops_init(&object)?,
    )?;

    let mut processed = HashSet::new();
    // Generate struct_ops types before anything else, as they are slightly
    // modified compared to the dumb structure contained in BTF.
    if let Some(btf) = &btf {
        let ops = GenStructOps::new(btf)?;
        let () = ops.gen_struct_ops_def(&mut skel)?;
        write!(
            skel,
            "\
                pub mod types {{
                    #[allow(unused_imports)]
                    use super::*;
            "
        )?;

        let () = ops.gen_dependent_types(&mut processed, &mut skel)?;
    } else {
        write!(
            skel,
            "
#[derive(Debug, Clone)]
#[repr(C)]
pub struct StructOps {{}}
"
        )?;
        write!(
            skel,
            "\
                pub mod types {{
                    #[allow(unused_imports)]
                    use super::*;
            "
        )?;
    }

    gen_skel_types(&mut skel, btf.as_ref(), &mut processed)?;
    writeln!(skel, "}}")?;

    write!(
        skel,
        "\
        pub struct Open{name}Skel<'obj> {{
            obj: OwnedRef<'obj, libbpf_rs::OpenObject>,
            pub maps: Open{name}Maps<'obj>,
            pub progs: Open{name}Progs<'obj>,
            pub struct_ops: StructOps,
            skel_config: libbpf_rs::__internal_skel::ObjectSkeletonConfig<'obj>,
        }}

        impl<'obj> OpenSkel<'obj> for Open{name}Skel<'obj> {{
            type Output = {name}Skel<'obj>;
            fn load(self) -> libbpf_rs::Result<{name}Skel<'obj>> {{
                let skel_ptr = self.skel_config.as_libbpf_object().as_ptr();

                let ret = unsafe {{ libbpf_sys::bpf_object__load_skeleton(skel_ptr) }};
                if ret != 0 {{
                    return Err(libbpf_rs::Error::from_raw_os_error(-ret));
                }}

                let obj_ref = self.obj.take();
                let open_obj = std::mem::replace(obj_ref, std::mem::MaybeUninit::uninit());
                // SAFETY: `open_obj` is guaranteed to be properly
                //         initialized as it came from an `OwnedRef`.
                let obj_ptr = unsafe {{ open_obj.assume_init().take_ptr() }};
                // SAFETY: `obj_ptr` points to a loaded object after
                //         skeleton load.
                let obj = unsafe {{ libbpf_rs::Object::from_ptr(obj_ptr) }};
                // SAFETY: `OpenObject` and `Object` are guaranteed to
                //         have the same memory layout.
                let obj_ref = unsafe {{
                    std::mem::transmute::<
                        &'obj mut std::mem::MaybeUninit<libbpf_rs::OpenObject>,
                        &'obj mut std::mem::MaybeUninit<libbpf_rs::Object>,
                    >(obj_ref)
                }};
                let _obj = obj_ref.write(obj);
                // SAFETY: We just wrote initialized data to `obj_ref`.
                let mut obj_ref = unsafe {{ OwnedRef::new(obj_ref) }};

                Ok({name}Skel {{
                    maps: unsafe {{ {name}Maps::new(&self.skel_config, obj_ref.as_mut())? }},
                    progs: {name}Progs::new(self.progs),
                    obj: obj_ref,
                    struct_ops: self.struct_ops,
                    skel_config: self.skel_config,
                    {links}
                }})
            }}

            fn open_object(&self) -> &libbpf_rs::OpenObject {{
                self.obj.as_ref()
            }}

            fn open_object_mut(&mut self) -> &mut libbpf_rs::OpenObject {{
                self.obj.as_mut()
            }}
        ",
        name = &obj_name,
        links = if object.progs().next().is_some() {
            format!("links: {obj_name}Links::default()")
        } else {
            "".to_string()
        }
    )?;
    writeln!(skel, "}}")?;

    gen_skel_link_defs(&mut skel, &object, &obj_name)?;

    write!(
        skel,
        "\
        pub struct {name}Skel<'obj> {{
            obj: OwnedRef<'obj, libbpf_rs::Object>,
            pub maps: {name}Maps<'obj>,
            pub progs: {name}Progs<'obj>,
            struct_ops: StructOps,
            skel_config: libbpf_rs::__internal_skel::ObjectSkeletonConfig<'obj>,
        ",
        name = &obj_name,
    )?;
    gen_skel_link_getter(&mut skel, &object, &obj_name)?;
    write!(
        skel,
        "\
        }}

        unsafe impl Send for {name}Skel<'_> {{}}
        unsafe impl Sync for {name}Skel<'_> {{}}

        impl<'obj> Skel<'obj> for {name}Skel<'obj> {{
            fn object(&self) -> &libbpf_rs::Object {{
                self.obj.as_ref()
            }}

            fn object_mut(&mut self) -> &mut libbpf_rs::Object {{
                self.obj.as_mut()
            }}
        ",
        name = &obj_name,
    )?;
    gen_skel_attach(&mut skel, &object, &obj_name)?;
    writeln!(skel, "}}")?;

    write!(skel, "impl {name}Skel<'_> {{", name = &obj_name)?;
    gen_skel_struct_ops_getters(&mut skel, &object)?;
    writeln!(skel, "}}")?;

    writeln!(skel, "#[unsafe(link_section = \".bpf.objs\")]")?;
    if obj_ref {
        let obj_file_path = obj_file_path
            .canonicalize()
            .unwrap_or_else(|_| obj_file_path.to_path_buf());
        writeln!(
            skel,
            "static DATA: [u8; {}] = *include_bytes!(\"{path}\");",
            mmap.len(),
            path = obj_file_path.display(),
        )?;
    } else {
        // Coerce to &[u8] just to be safe, as we'll be using debug formatting
        let bytes: &[u8] = &mmap;
        writeln!(skel, "static DATA: [u8; {}] = {bytes:?};", bytes.len())?;
    }
    writeln!(skel, "}}")?;

    Ok(skel)
}

/// Generate a single skeleton
fn gen_skel(
    name: &str,
    obj: &Path,
    out: OutputDest<'_>,
    rustfmt_path: Option<&Path>,
    obj_ref: bool,
) -> Result<()> {
    ensure!(!name.is_empty(), "Object file has no name");

    let skel = gen_skel_contents(name, obj, obj_ref)?;
    let skel = try_rustfmt(&skel, rustfmt_path)?;

    match out {
        OutputDest::Stdout => stdout().write_all(&skel)?,
        OutputDest::Directory(dir) => {
            let path = dir.join(format!("{name}.skel.rs"));
            let mut file = File::create(path)?;
            file.write_all(&skel)?;
        }
        OutputDest::File(file) => {
            let mut file = File::create(file)?;
            file.write_all(&skel)?;
        }
    };

    Ok(())
}

/// Generate mod.rs in src/bpf directory of each project.
///
/// Each `UnprocessedObj` in `objs` must belong to same project.
pub(crate) fn gen_mods(objs: &[UnprocessedObj], rustfmt_path: Option<&Path>) -> Result<()> {
    if objs.is_empty() {
        return Ok(());
    }

    let mut path = objs[0].path.clone();
    path.pop();
    path.push("mod.rs");

    let mut contents = String::new();
    write!(
        contents,
        "\
        // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
        //
        // THIS FILE IS AUTOGENERATED BY CARGO-LIBBPF-GEN!

        "
    )?;

    for obj in objs {
        write!(
            contents,
            "
            #[path = \"{name}.skel.rs\"]
            mod {name}_skel;
            ",
            name = obj.name
        )?;
    }

    for obj in objs {
        writeln!(contents, "pub use {}_skel::*;", obj.name)?;
    }

    let mut file = File::create(path)?;
    file.write_all(&try_rustfmt(&contents, rustfmt_path)?)?;

    Ok(())
}

pub(crate) fn gen_single(
    obj_file: &Path,
    output: OutputDest<'_>,
    rustfmt_path: Option<&Path>,
    obj_ref: bool,
    original_name: Option<&str>,
) -> Result<()> {
    let filename = match obj_file.file_name() {
        Some(n) => n,
        None => bail!(
            "Could not determine file name for object file: {}",
            obj_file.to_string_lossy()
        ),
    };

    let name = if let Some(name) = original_name {
        name
    } else {
        match filename.to_str() {
            Some(n) => {
                ensure!(
                    n.ends_with(".o"),
                    "Object file does not have `.o` suffix: {n}"
                );

                n.split('.').next().unwrap()
            }
            None => bail!(
                "Object file name is not valid unicode: {}",
                filename.to_string_lossy()
            ),
        }
    };

    let () = gen_skel(name, obj_file, output, rustfmt_path, obj_ref).with_context(|| {
        format!(
            "Failed to generate skeleton for {}",
            obj_file.to_string_lossy(),
        )
    })?;

    Ok(())
}

fn gen_project(manifest_path: Option<&Path>, rustfmt_path: Option<&Path>) -> Result<()> {
    let (_target_dir, to_gen) = metadata::get(manifest_path)?;
    if !to_gen.is_empty() {
        debug!("Found bpf objs to gen skel:");
        for obj in &to_gen {
            debug!("\t{obj:?}");
        }
    } else if to_gen.is_empty() {
        bail!("Did not find any bpf objects to generate skeleton");
    }

    // Map to store package_name -> [UnprocessedObj]
    let mut package_objs: BTreeMap<String, Vec<UnprocessedObj>> = BTreeMap::new();

    for obj in to_gen {
        let mut obj_file_path = obj.out.clone();
        obj_file_path.push(format!("{}.bpf.o", obj.name));

        let mut skel_path = obj.path.clone();
        skel_path.pop();

        let () = gen_skel(
            &obj.name,
            obj_file_path.as_path(),
            OutputDest::Directory(skel_path.as_path()),
            rustfmt_path,
            false,
        )
        .with_context(|| {
            format!(
                "Failed to generate project skeleton for {}",
                obj.path.as_path().display()
            )
        })?;

        match package_objs.get_mut(&obj.package) {
            Some(v) => v.push(obj.clone()),
            None => {
                package_objs.insert(obj.package.clone(), vec![obj.clone()]);
            }
        };
    }

    for (package, objs) in package_objs {
        let () = gen_mods(&objs, rustfmt_path)
            .with_context(|| format!("Failed to generate mod.rs for package={package}"))?;
    }

    Ok(())
}

/// Generate skeleton files for a project.
pub fn generate(
    manifest_path: Option<&Path>,
    rustfmt_path: Option<&Path>,
    object: Option<&Path>,
) -> Result<()> {
    if manifest_path.is_some() && object.is_some() {
        bail!("--manifest-path and --object cannot be used together");
    }

    if let Some(obj_file) = object {
        gen_single(obj_file, OutputDest::Stdout, rustfmt_path, false, None)
    } else {
        gen_project(manifest_path, rustfmt_path)
    }
}