pub struct ElfImage<'a> { /* private fields */ }Expand description
A loaded ELF image (e.g. a .so). Pointers are resolved through the
relocation table, since ELF stores relative relocations rather than absolute
addresses.
Implementations§
Source§impl<'a> ElfImage<'a>
impl<'a> ElfImage<'a>
pub fn parse(data: &'a [u8]) -> Result<ElfImage<'a>, PewterError>
Sourcepub fn write_at_vaddr(
&mut self,
vaddr: u64,
data: &[u8],
) -> Result<(), PewterError>
pub fn write_at_vaddr( &mut self, vaddr: u64, data: &[u8], ) -> Result<(), PewterError>
Overwrite the bytes at virtual address vaddr (a patch applied by
write_file). Fails if vaddr is not backed by a
PT_LOAD segment, or the write would run past the end of the file — this
patches in place; to grow the image use add_section.
The patch is recorded, not applied to this view: reads here still see the original bytes until the next load.
Sourcepub fn add_section(
&mut self,
name: &str,
data: &[u8],
perms: SectionPermissions,
) -> Result<u64, PewterError>
pub fn add_section( &mut self, name: &str, data: &[u8], perms: SectionPermissions, ) -> Result<u64, PewterError>
Append a readable, loaded section, returning its virtual address — the
format-neutral form, matching PeImage::add_section
and [AnyImage::add_section]. For an ELF-specific section (a non-ALLOC
note, a custom SectionType, explicit link/info) use
add_raw_section.
Sourcepub fn add_raw_section(
&mut self,
name: &str,
section_type: SectionType,
flags: SectionFlags,
data: Vec<u8>,
) -> Result<u64, PewterError>
pub fn add_raw_section( &mut self, name: &str, section_type: SectionType, flags: SectionFlags, data: Vec<u8>, ) -> Result<u64, PewterError>
Append a section with full control over its SectionType and raw
SectionFlags, returning its virtual address (0 for a non-ALLOC
section). write_file appends the section after the
original content, so the original loaded image is preserved.
A non-ALLOC section (metadata, notes, embedded data) is just appended.
A loadable (ALLOC) section is also given a fresh page-aligned virtual
address past everything mapped and a new PT_LOAD at write_file, so it
is mapped when the file loads — the ELF counterpart of PE’s new_section.
Sourcepub fn add_needed(&mut self, soname: &str) -> Result<(), PewterError>
pub fn add_needed(&mut self, soname: &str) -> Result<(), PewterError>
Add a DT_NEEDED dependency on soname, so the loader auto-loads it when
this library is loaded (like patchelf --add-needed). Built from the
other edits: .dynstr is grown (via add_raw_section,
since it has no room for a new name) and .dynamic is patched in place
(via write_at_vaddr) to repoint DT_STRTAB/
DT_STRSZ and turn a spare DT_NULL slot into the new DT_NEEDED.
Fails if there is no spare DT_NULL slot in .dynamic (rewriting an
exactly-sized .dynamic would need PT_DYNAMIC relocation, not done yet).
Sourcepub fn write_file(&self) -> Result<Vec<u8>, PewterError>
pub fn write_file(&self) -> Result<Vec<u8>, PewterError>
Serialize back to bytes, applying any write_at_vaddr
patches and appending any add_section sections.
Sourcepub fn forwarding_proxy(
&self,
config: &ProxyConfig<'_>,
) -> Result<Vec<u8>, PewterError>
pub fn forwarding_proxy( &self, config: &ProxyConfig<'_>, ) -> Result<Vec<u8>, PewterError>
Build a forwarding proxy .so for this image (see ProxyConfig).
ELF symbol scope is flat, so a DT_NEEDED on the renamed real library
(forward_to) brings its symbols into scope for anything linked against
this proxy — the proxy both auto-loads the payload and keeps a linked
program working, with no per-symbol thunks. (config.reexport, i.e.
.dynsym thunks that also satisfy dlopen+dlsym on the proxy itself,
is a follow-up — see ElfImageDef::dynamic_proxy.)
Trait Implementations§
Source§impl LoadedImage for ElfImage<'_>
impl LoadedImage for ElfImage<'_>
Source§fn bytes(&self, vaddr: u64, len: usize) -> Option<&[u8]>
fn bytes(&self, vaddr: u64, len: usize) -> Option<&[u8]>
len bytes at virtual address vaddr, or None if unmapped.Source§fn c_str(&self, vaddr: u64) -> Option<String>
fn c_str(&self, vaddr: u64) -> Option<String>
vaddr, decoded as lossy UTF-8, or None
if vaddr is unmapped. Reads to a NUL byte or the end of the mapping.Source§fn pointer(&self, vaddr: u64) -> Option<u64>
fn pointer(&self, vaddr: u64) -> Option<u64>
vaddr, after relocation.Source§fn data_segments(&self) -> Vec<(u64, u64)>
fn data_segments(&self) -> Vec<(u64, u64)>
Source§fn exports(&self) -> Result<Vec<Export>, PewterError>
fn exports(&self) -> Result<Vec<Export>, PewterError>
Source§fn machine(&self) -> Option<ImageFileMachine>
fn machine(&self) -> Option<ImageFileMachine>
None if PE has no equivalent.fn pointer_size(&self) -> u64
fn u32(&self, vaddr: u64) -> Option<u32>
fn u8(&self, vaddr: u64) -> Option<u8>
Source§fn pointer_at(&self, base: u64, index: u64) -> Option<u64>
fn pointer_at(&self, base: u64, index: u64) -> Option<u64>
indexth pointer of an array at base, i.e. the pointer at
base + index * pointer_size(). Saves spelling out the stride when
walking a table of pointers.Source§fn pointers(&self, vaddr: u64, count: u64) -> Option<Vec<u64>>
fn pointers(&self, vaddr: u64, count: u64) -> Option<Vec<u64>>
count consecutive pointers starting at vaddr, or None if any is
unmapped.Source§fn pointer_slots(&self, segment: (u64, u64)) -> impl Iterator<Item = u64>
fn pointer_slots(&self, segment: (u64, u64)) -> impl Iterator<Item = u64>
segment (a data_segments
range), as the virtual addresses start, start + pointer_size(), … that
each still hold a full pointer. Useful for scanning a segment for a structure.