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
//!  SPDX-License-Identifier: MIT
//!
//! Copyright (c) 2023, eunomia-bpf
//! All rights reserved.
//!

use anyhow::{anyhow, Result};
use object::ElfFile;
use ouroboros::self_referencing;
/// A helper struct to solve the reference problem of ElfFile
/// This struct contains the binary of the original elf file and the ElfFile struct
/// With this we don't need to take care of the reference problem anymore
#[self_referencing]
pub struct ElfContainer {
    bin: Vec<u8>,
    #[borrows(bin)]
    #[covariant]
    pub(crate) elf: ElfFile<'this>,
}

impl ElfContainer {
    /// Create a container from a ELF binary
    pub fn new_from_binary(bin: &[u8]) -> Result<Self> {
        let bin = bin.to_vec();
        let val = ElfContainerTryBuilder {
            bin,
            elf_builder: |vec: &Vec<u8>| {
                ElfFile::parse(&vec[..]).map_err(|e| anyhow!("Failed to parse elf: {}", e))
            },
        }
        .try_build()?;
        Ok(val)
    }
}