Git Product home page Git Product logo

Comments (1)

krh avatar krh commented on August 30, 2024
// xmas_elf wants a slice, not a pointer, so we have to try to figure out how
// big the ELF data is from just the pointer.  We do this by first creating a
// slice for just the Elf64 header size.  From that we can see where the
// section headers end and extend the slice to cover those.  Since ELF doesn't
// guaranetee any ordering of section header vs sections, we scan the section
// headers to see where the last section ends, and if that's past the section
// headers, we further extend the slice and recreate the ELF one last time.
unsafe fn elf_from_ptr<'a>(image: *const c_void) -> Result<xmas_elf::ElfFile<'a>, &'static str> {
    let image = image.cast::<u8>();
    const ELF64_HEADER_SIZE: usize = 64;
    let slice = unsafe { std::slice::from_raw_parts(image, ELF64_HEADER_SIZE) };
    let elf = xmas_elf::ElfFile::new(slice)?;

    let section_header_end = elf.header.pt2.sh_offset() as usize
        + elf.header.pt2.sh_count() as usize * elf.header.pt2.sh_entry_size() as usize;

    let slice = unsafe { std::slice::from_raw_parts(image, section_header_end) };
    let elf = xmas_elf::ElfFile::new(slice)?;

    let last_section_end = elf
        .section_iter()
        .map(|s| s.offset() + s.size())
        .max()
        .ok_or(Err("no sections in ELF"))? as usize;
    if sh_end > end {
        let slice = unsafe { std::slice::from_raw_parts(image, std::cmp::max(end, last_section_end)) };
        xmas_elf::ElfFile::new(slice)
    } else {
        Ok(elf)
    }
}

from xmas-elf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.