fix loader.rs

use goblin::Object;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use goblin::pe::PE;
use goblin::elf::Elf;

pub struct BinaryLoader {
    pub entry_point: u64,
    pub sections: Vec<BinarySection>,
    pub bitness: u32,
    pub pe: Option<PE<'static>>,
    pub elf: Option<Elf<'static>>,
}

pub struct BinarySection {
    pub name: String,
    pub addr: u64,
    pub size: u64,
    pub data: Vec<u8>,
    pub is_executable: bool,
}

impl BinaryLoader {
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
        let mut file = File::open(&path)?;
        let mut buffer = Vec::new();

        file.read_to_end(&mut buffer)?;

        match Object::parse(&buffer)? {
            Object::PE(pe) => {
                let bitness = if pe.is_64 { 64 } else { 32 };
                let entry_point = pe.entry as u64;
                let mut sections = Vec::new();
                for section in &pe.sections {
                    let name = section.name().unwrap_or("").to_string();
                    let is_executable = section.characteristics & goblin::pe::section_table::IMAGE_SCN_CNT_CODE != 0;
                    let data = section.data(&buffer)?.expect("Failed to get section data").to_vec();
                    sections.push(BinarySection {
                        name,
                        addr: section.virtual_address as u64,
                        size: section.virtual_size as u64,
                        data,
                        is_executable,
                    });
                }
                Ok(Self {
                    entry_point,
                    sections,
                    bitness,
                    pe: Some(pe),
                    elf: None,
                })
            }
            Object::Elf(elf) => {
                let bitness = if elf.is_64 { 64 } else { 32 };
                let entry_point = elf.entry;
                let mut sections = Vec::new();
                for shdr in &elf.section_headers {
                    let name = elf.shdr_strtab.get_at(shdr.sh_name).unwrap_or("").to_string();
                    let is_executable = shdr.sh_flags & goblin::elf::section_header::SHF_EXECINSTR as u64 != 0;
                    let data = buffer[shdr.sh_offset as usize..(shdr.sh_offset + shdr.sh_size) as usize].to_vec();
                    sections.push(BinarySection {
                        name,
                        addr: shdr.sh_addr,
                        size: shdr.sh_size,
                        data,
                        is_executable,
                    });
                }
                Ok(Self {
                    entry_point,
                    sections,
                    bitness,
                    pe: None,
                    elf: Some(elf),
                })
            }
            _ => Err("Unsupported binary format".into()),
        }
    }
}


error[E0597]: `buffer` does not live long enough
  --> src\loader.rs:32:29
   |
28 |         let mut buffer = Vec::new();
   |             ---------- binding `buffer` declared here
...
32 |         match Object::parse(&buffer)? {
   |               --------------^^^^^^^-
   |               |             |
   |               |             borrowed value does not live long enough
   |               argument requires that `buffer` is borrowed for `'static`
...
83 |     }
   |     - `buffer` dropped here while still borrowed