Skip to main content

Elf64File

Struct Elf64File 

Source
pub struct Elf64File {
    pub class: ElfClass,
    pub ehdr: Ehdr64,
    pub phdrs: Vec<Phdr64>,
    pub shdrs: Vec<Shdr64>,
    /* private fields */
}
Expand description

A parsed ELF64 file in a form that round-trips byte-identically.

The structured fields (ehdr, phdrs, shdrs) are interpreted; the raw bytes inside sections and any interstitial padding are stored verbatim. On write_to_vec, the structured fields are reassembled and the verbatim bytes are dropped back in place at their original offsets.

Fields§

§class: ElfClass

On-disk header layout. Determines whether the headers re-emit as 32-bit or 64-bit on serialisation.

§ehdr: Ehdr64§phdrs: Vec<Phdr64>§shdrs: Vec<Shdr64>

Implementations§

Source§

impl Elf64File

Source

pub fn parse(bytes: &[u8]) -> Result<Self>

Parse an ELF LE file (either ELFCLASS32 or ELFCLASS64) into a structure that round-trips byte-identically.

Source

pub fn section_data(&self, idx: usize) -> Option<&[u8]>

Raw on-disk bytes of the section at index idx, parallel to Self::shdrs. Returns an empty slice for NOBITS or zero-size sections. Returns None only for an out-of-range index.

Source

pub fn from_parts( class: ElfClass, ehdr: Ehdr64, phdrs: Vec<Phdr64>, shdrs: Vec<Shdr64>, section_data: Vec<Vec<u8>>, padding: Vec<(u64, Vec<u8>)>, file_size: u64, ) -> Self

Construct an Elf64File from already-parsed parts.

Used by reconstructive code paths (such as ud-compile’s lower path) that build the file’s structure from a .ud AST rather than from on-disk bytes. The caller is responsible for keeping the parts consistent: section_data must be parallel to shdrs, padding must cover every gap between structured regions, and file_size must equal the total covered. write_to_vec does no validation; it assumes consistency.

Source

pub fn file_size(&self) -> u64

Total size of the underlying file in bytes.

Source

pub fn padding(&self) -> &[(u64, Vec<u8>)]

All padding regions captured between structured regions. Returns (file_offset, bytes) pairs in offset order.

Source

pub fn sections(&self) -> impl Iterator<Item = (usize, &Shdr64, &[u8])>

Iterator over (index, &Shdr64, &[u8]) for every section.

Source

pub fn section_name(&self, idx: usize) -> Option<&str>

Resolve the section’s name through the section-header string table (.shstrtab, indexed by e_shstrndx).

Returns None if the section index is out of range, the e_shstrndx points outside the section table, the name offset is past the end of .shstrtab, or the bytes aren’t valid UTF-8 (which would indicate a malformed or non-standard ELF; real toolchains write ASCII section names).

Source

pub fn section_by_name(&self, name: &str) -> Option<(usize, &Shdr64, &[u8])>

Find the first section with the given name.

Iterates section headers in order, so for ELFs with multiple sections sharing a name (rare but legal) the lowest-indexed one wins.

Source

pub fn write_to_vec(&self) -> Vec<u8>

Serialize the parsed file back to bytes. For any input parsed from real bytes, the output is byte-identical to the input.

Trait Implementations§

Source§

impl Clone for Elf64File

Source§

fn clone(&self) -> Elf64File

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Elf64File

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.