Struct lief::elf::Binary

pub struct Binary { /* private fields */ }
Expand description

This is the main interface to read and write ELF binary attributes.

Note that this structure implements the generic::Binary trait from which other generic functions are exposed

Implementations§

§

impl Binary

pub fn parse(path: &str) -> Option<Self>

Create a Binary from the given file path

pub fn parse_with_config(path: &str, config: &ParserConfig) -> Option<Self>

Parse from a string file path and with a provided configuration

pub fn header(&self) -> Header<'_>

Return the main ELF header

pub fn virtual_size(&self) -> u64

Return the size taken by the binary when loaded (virtual size)

pub fn interpreter(&self) -> String

Return the path to the ELF interpreter that is used to process the ELF information once loaded by the kernel

pub fn sysv_hash(&self) -> Option<Sysv<'_>>

Return sysv-hash information (if present)

pub fn gnu_hash(&self) -> Option<Gnu<'_>>

Return GNU Hash info (if present)

pub fn sections(&self) -> Sections<'_>

Return an iterator over the crate::elf::Section of the binary

pub fn segments(&self) -> Segments<'_>

Return an iterator over the crate::elf::Segment of the binary

pub fn dynamic_entries(&self) -> DynamicEntries<'_>

Return an iterator over the crate::elf::DynamicEntries of the binary

pub fn remove_dynamic_entries_by_tag(&mut self, tag: Tag)

Remove all dynamic entries with the given tag

pub fn add_dynamic_entry(&mut self, entry: &dyn DynamicEntry) -> Entries<'_>

Add the given dynamic entry and return the new entry

pub fn dynamic_symbols(&self) -> DynamicSymbols<'_>

Return an iterator over the dynamic crate::elf::Symbol of the binary

pub fn exported_symbols(&self) -> ExportedSymbols<'_>

Return an iterator over the exported crate::elf::Symbol of the binary

pub fn imported_symbols(&self) -> ImportedSymbols<'_>

Return an iterator over the imported crate::elf::Symbol of the binary

pub fn symtab_symbols(&self) -> SymtabSymbols<'_>

Return an iterator over the symtab-debug crate::elf::Symbol of the binary

pub fn symbols_version(&self) -> SymbolsVersion<'_>

Return an iterator over the crate::elf::SymbolVersion of the binary

pub fn symbols_version_requirement(&self) -> SymbolsVersionRequirement<'_>

Return an iterator over the crate::elf::SymbolVersionRequirement of the binary

pub fn symbols_version_definition(&self) -> SymbolsVersionDefinition<'_>

Return an iterator over the crate::elf::SymbolVersionDefinition of the binary

pub fn notes(&self) -> ItNotes<'_>

Return an iterator over the crate::elf::Notes of the binary

pub fn pltgot_relocations(&self) -> PltGotRelocations<'_>

Return an iterator over the .plt.got crate::elf::Relocation of the binary

pub fn dynamic_relocations(&self) -> DynamicRelocations<'_>

Return an iterator over the regular crate::elf::Relocation of the binary

pub fn object_relocations(&self) -> ObjectRelocations<'_>

Return an iterator over the object-file (.o) crate::elf::Relocation

pub fn relocations(&self) -> Relocations<'_>

Return an iterator over all crate::elf::Relocation of the binary

pub fn section_by_name(&self, name: &str) -> Option<Section<'_>>

Try to find the ELF section with the given name

pub fn relocation_by_addr(&self, address: u64) -> Option<Relocation<'_>>

Try to find the ELF relocation that takes place at the given address

pub fn relocation_for_symbol(&self, sym_name: &str) -> Option<Relocation<'_>>

Try to find the .plt.got relocation for the given symbol name

pub fn dynamic_symbol_by_name(&self, sym_name: &str) -> Option<Symbol<'_>>

Try to find the symbol with the given name in the dynamic .dynsym table

pub fn symtab_symbol_by_name(&self, sym_name: &str) -> Option<Symbol<'_>>

Try to find the symbol with the given name in the debug .symtab table

pub fn get_library(&self, name: &str) -> Option<Library<'_>>

Try to find the library (DT_NEEDED) with the given name

pub fn section_from_offset( &self, offset: u64, skip_nobits: bool ) -> Option<Section<'_>>

Try to find the section that encompasses the given offset. skip_nobits can be used to include (or not) the SHT_NOTBIT sections

pub fn section_from_virtual_address( &self, address: u64, skip_nobits: bool ) -> Option<Section<'_>>

Try to find the section that encompasses the given virtual address. skip_nobits can be used to include (or not) the SHT_NOTBIT sections

pub fn segment_from_virtual_address(&self, address: u64) -> Option<Segment<'_>>

Try to find the segment that encompasses the given virtual address

pub fn segment_from_offset(&self, offset: u64) -> Option<Segment<'_>>

Try to find the segment that encompasses the given offset

pub fn content_from_virtual_address(&self, address: u64, size: u64) -> &[u8]

Get a slice of the content at the given address.

pub fn virtual_address_to_offset(&self, address: u64) -> Result<u64, Error>

Convert the given virtual address into an offset

pub fn get_relocated_dynamic_array(&self, tag: Tag) -> Vec<u64>

Return the array defined by the given tag (e.g. dynamic::Tag::INIT_ARRAY) with relocations applied (if any)

pub fn is_targeting_android(&self) -> bool

True if the current binary is targeting Android

pub fn get_int_from_virtual_address<T>(&self, addr: u64) -> Result<T, Error>where T: Num + FromPrimitive + ToPrimitive,

Get the integer value at the given virtual address

pub fn write(&mut self, output: &Path)

Write back the current ELF binary into the file specified in parameter

pub fn write_with_config(&mut self, output: &Path, config: Config)

Write back the current ELF binary into the file specified in parameter with the configuration provided in the second parameter.

pub fn add_library<'a>(&'a mut self, library: &str) -> Library<'a>

Add a library as dependency

pub fn functions(&self) -> Functions<'_>

Iterator over the functions found in this binary

pub fn dynamic_entry_by_tag(&self, tag: Tag) -> Option<Entries<'_>>

Try to find the dynamic entry associated with the given tag

pub fn segment_by_type(&self, seg_type: Type) -> Option<Segment<'_>>

Look for the segment with the given type. If there are multiple segment with the same type, it returns the first one.

pub fn remove_dynamic_entry(&mut self, entry: impl DynamicEntry)

Remove the given dynamic entry

pub fn remove_dynamic_entry_if<P>(&mut self, predicate: P) -> usizewhere P: Fn(&Entries<'_>) -> bool,

Remove the dynamic entries matching the given predicate.

The function returns the number of entries that have been deleted.

pub fn remove_library(&mut self, name: &str)

Remove the DT_NEEDED dependency with the given name

pub fn add_segment(&mut self, segment: &Segment<'_>) -> Option<Segment<'_>>

Add the provided segment to the binary. This function returns the newly added segment which could define additional attributes like the virtual address.

pub fn set_interpreter(&mut self, interpreter: &str)

Change the path to the interpreter

pub fn find_version_requirement( &self, libname: &str ) -> Option<SymbolVersionRequirement<'_>>

Try to find the SymbolVersionRequirement associated with the given library name (e.g. libc.so.6)

pub fn remove_version_requirement(&mut self, libname: &str) -> bool

Deletes all required symbol versions linked to the specified library name. The function returns true if the operation succeed, false otherwise.

To maintain consistency, this function also removes versions associated with dynamic symbols that are linked to the specified library name.

pub fn remove_segment(&mut self, segment: Segment<'_>, clear: bool)

Remove the given segment. If clear is set, the original content of the segment will be filled with zeros before removal.

pub fn remove_segments_by_type(&mut self, ty: Type, clear: bool)

Remove all segments associated with the given type.

If clear is set, the original content of the segments will be filled with zeros before removal.

Trait Implementations§

§

impl Binary for Binary

§

fn entrypoint(&self) -> u64

Binary’s entrypoint
§

fn imagebase(&self) -> u64

Default base address where the binary should be mapped
§

fn is_pie(&self) -> bool

Whether the current binary is an executable and position independent
§

fn has_nx(&self) -> bool

Whether the binary defines a non-executable stack
§

fn original_size(&self) -> u64

Original file size of the binary
§

fn debug_info(&self) -> Option<DebugInfo<'_>>

Return the debug info if present. It can be either a crate::pdb::DebugInfo or crate::dwarf::DebugInfo. Read more
§

fn disassemble(&self, address: u64, size: u64) -> InstructionsIt<'_>

Disassemble code starting a the given virtual address and with the given size. Read more
§

fn disassemble_symbol(&self, name: &str) -> InstructionsIt<'_>

Disassemble code for the given symbol name Read more
§

fn disassemble_address(&self, address: u64) -> InstructionsIt<'_>

Disassemble code at the given virtual address Read more
§

fn disassemble_slice(&self, slice: &[u8], address: u64) -> InstructionsIt<'_>

Disassemble code provided by the given slice at the specified address parameter. Read more
§

fn assemble(&mut self, address: u64, asm: &str) -> Vec<u8>

Assemble and patch the provided assembly code at the specified address. Read more
§

fn assemble_with_config( &mut self, address: u64, asm: &str, config: &AssemblerConfig ) -> Vec<u8>

Same as Binary::assemble but this function takes an extra AssemblerConfig that is used to configure the assembly engine: dialect, symbols definitions.
§

fn page_size(&self) -> u64

Get the default memory page size according to the architecture and the format of the current binary
§

impl Debug for Binary

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Binary

§

impl !Send for Binary

§

impl !Sync for Binary

§

impl Unpin for Binary

§

impl UnwindSafe for Binary

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.