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: ElfClassOn-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
impl Elf64File
Sourcepub fn parse(bytes: &[u8]) -> Result<Self>
pub fn parse(bytes: &[u8]) -> Result<Self>
Parse an ELF LE file (either ELFCLASS32 or ELFCLASS64) into a structure that round-trips byte-identically.
Sourcepub fn section_data(&self, idx: usize) -> Option<&[u8]>
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.
Sourcepub 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
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.
Sourcepub fn padding(&self) -> &[(u64, Vec<u8>)]
pub fn padding(&self) -> &[(u64, Vec<u8>)]
All padding regions captured between structured regions.
Returns (file_offset, bytes) pairs in offset order.
Sourcepub fn sections(&self) -> impl Iterator<Item = (usize, &Shdr64, &[u8])>
pub fn sections(&self) -> impl Iterator<Item = (usize, &Shdr64, &[u8])>
Iterator over (index, &Shdr64, &[u8]) for every section.
Sourcepub fn section_name(&self, idx: usize) -> Option<&str>
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).
Sourcepub fn section_by_name(&self, name: &str) -> Option<(usize, &Shdr64, &[u8])>
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.
Sourcepub fn write_to_vec(&self) -> Vec<u8> ⓘ
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.