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
impl Binary
pub fn parse_with_config<P: AsRef<Path>>(
path: P,
config: &ParserConfig
) -> Option<Self>
pub fn parse_with_config<P: AsRef<Path>>( path: P, config: &ParserConfig ) -> Option<Self>
Parse from a string file path and with a provided configuration
pub fn virtual_size(&self) -> u64
pub fn virtual_size(&self) -> u64
Return the size taken by the binary when loaded (virtual size)
pub fn interpreter(&self) -> String
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 sections(&self) -> Sections<'_> ⓘ
pub fn sections(&self) -> Sections<'_> ⓘ
Return an iterator over the crate::elf::Section of the binary
pub fn segments(&self) -> Segments<'_> ⓘ
pub fn segments(&self) -> Segments<'_> ⓘ
Return an iterator over the crate::elf::Segment of the binary
pub fn dynamic_entries(&self) -> DynamicEntries<'_> ⓘ
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)
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<'_>
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<'_> ⓘ
pub fn dynamic_symbols(&self) -> DynamicSymbols<'_> ⓘ
Return an iterator over the dynamic crate::elf::Symbol of the binary
pub fn exported_symbols(&self) -> ExportedSymbols<'_> ⓘ
pub fn exported_symbols(&self) -> ExportedSymbols<'_> ⓘ
Return an iterator over the exported crate::elf::Symbol of the binary
pub fn imported_symbols(&self) -> ImportedSymbols<'_> ⓘ
pub fn imported_symbols(&self) -> ImportedSymbols<'_> ⓘ
Return an iterator over the imported crate::elf::Symbol of the binary
pub fn symtab_symbols(&self) -> SymtabSymbols<'_> ⓘ
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<'_> ⓘ
pub fn symbols_version(&self) -> SymbolsVersion<'_> ⓘ
Return an iterator over the crate::elf::SymbolVersion of the binary
pub fn symbols_version_requirement(&self) -> SymbolsVersionRequirement<'_> ⓘ
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<'_> ⓘ
pub fn symbols_version_definition(&self) -> SymbolsVersionDefinition<'_> ⓘ
Return an iterator over the crate::elf::SymbolVersionDefinition of the binary
pub fn notes(&self) -> ItNotes<'_> ⓘ
pub fn notes(&self) -> ItNotes<'_> ⓘ
Return an iterator over the crate::elf::Notes of the binary
pub fn pltgot_relocations(&self) -> PltGotRelocations<'_> ⓘ
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<'_> ⓘ
pub fn dynamic_relocations(&self) -> DynamicRelocations<'_> ⓘ
Return an iterator over the regular crate::elf::Relocation of the binary
pub fn object_relocations(&self) -> ObjectRelocations<'_> ⓘ
pub fn object_relocations(&self) -> ObjectRelocations<'_> ⓘ
Return an iterator over the object-file (.o) crate::elf::Relocation
pub fn relocations(&self) -> Relocations<'_> ⓘ
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<'_>>
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<'_>>
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<'_>>
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<'_>>
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<'_>>
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<'_>>
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<'_>>
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<'_>>
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<'_>>
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<'_>>
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] ⓘ
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>
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>
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
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,
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<P: AsRef<Path>>(&mut self, output: P)
pub fn write<P: AsRef<Path>>(&mut self, output: P)
Write back the current ELF binary into the file specified in parameter
pub fn write_with_config<P: AsRef<Path>>(&mut self, output: P, config: Config)
pub fn write_with_config<P: AsRef<Path>>(&mut self, output: P, 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>
pub fn add_library<'a>(&'a mut self, library: &str) -> Library<'a>
Add a library as dependency
pub fn dynamic_entry_by_tag(&self, tag: Tag) -> Option<Entries<'_>>
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<'_>>
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)
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,
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)
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<'_>>
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<P: AsRef<Path>>(&mut self, interpreter: P)
pub fn set_interpreter<P: AsRef<Path>>(&mut self, interpreter: P)
Change the path to the interpreter
pub fn find_version_requirement(
&self,
libname: &str
) -> Option<SymbolVersionRequirement<'_>>
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
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.
pub fn remove_segment(&mut self, segment: Segment<'_>, clear: bool)
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)
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.
pub fn symbols(&self) -> AllSymbols<'_> ⓘ
pub fn symbols(&self) -> AllSymbols<'_> ⓘ
Return an iterator over all symbols (combined dynamic + symtab)
pub fn strings(&self, min_size: u64) -> Vec<String>
pub fn strings(&self, min_size: u64) -> Vec<String>
Return all printable strings from the binary with a minimum length
pub fn last_offset_section(&self) -> u64
pub fn last_offset_section(&self) -> u64
Return the last offset used in binary according to sections table
pub fn last_offset_segment(&self) -> u64
pub fn last_offset_segment(&self) -> u64
Return the last offset used in binary according to segments table
pub fn next_virtual_address(&self) -> u64
pub fn next_virtual_address(&self) -> u64
Return the next virtual address available
pub fn eof_offset(&self) -> u64
pub fn eof_offset(&self) -> u64
Return the offset of the end of the binary on disk
pub fn dtor_functions(&self) -> Functions<'_> ⓘ
pub fn dtor_functions(&self) -> Functions<'_> ⓘ
Return the destructor functions (from .fini_array, .dtors, etc.)
pub fn set_overlay(&mut self, data: &[u8])
pub fn set_overlay(&mut self, data: &[u8])
Set the overlay data
pub fn has_dynamic_entry_tag(&self, tag: Tag) -> bool
pub fn has_dynamic_entry_tag(&self, tag: Tag) -> bool
Check if the binary has a dynamic entry with the given tag
pub fn has_segment_type(&self, ty: Type) -> bool
pub fn has_segment_type(&self, ty: Type) -> bool
Check if the binary has a segment with the given type
pub fn has_note_type(&self, ty: Type) -> bool
pub fn has_note_type(&self, ty: Type) -> bool
Check if the binary has a note with the given type
pub fn has_section_type(&self, ty: Type) -> bool
pub fn has_section_type(&self, ty: Type) -> bool
Check if the binary has a section with the given type
pub fn note_by_type(&self, ty: Type) -> Option<Notes<'_>>
pub fn note_by_type(&self, ty: Type) -> Option<Notes<'_>>
Try to find a note by its type
pub fn section_by_type(&self, ty: Type) -> Option<Section<'_>>
pub fn section_by_type(&self, ty: Type) -> Option<Section<'_>>
Try to find a section by its type
pub fn has_section(&self, name: &str) -> bool
pub fn has_section(&self, name: &str) -> bool
Check if the binary has a section with the given name
pub fn has_section_with_offset(&self, offset: u64) -> bool
pub fn has_section_with_offset(&self, offset: u64) -> bool
Check if the binary has a section that spans the given offset
pub fn has_section_with_va(&self, va: u64) -> bool
pub fn has_section_with_va(&self, va: u64) -> bool
Check if the binary has a section that spans the given virtual address
pub fn has_library(&self, name: &str) -> bool
pub fn has_library(&self, name: &str) -> bool
Check if the binary has a library dependency with the given name
pub fn has_dynamic_symbol(&self, name: &str) -> bool
pub fn has_dynamic_symbol(&self, name: &str) -> bool
Check if the binary has a dynamic symbol with the given name
pub fn has_symtab_symbol(&self, name: &str) -> bool
pub fn has_symtab_symbol(&self, name: &str) -> bool
Check if the binary has a symtab symbol with the given name
pub fn dynsym_idx(&self, name: &str) -> Option<u64>
pub fn dynsym_idx(&self, name: &str) -> Option<u64>
Return the index of the given name in the dynamic symbol table
pub fn symtab_idx(&self, name: &str) -> Option<u64>
pub fn symtab_idx(&self, name: &str) -> Option<u64>
Return the index of the given name in the symtab symbol table, or -1
pub fn patch_pltgot(&mut self, symbol_name: &str, address: u64)
pub fn patch_pltgot(&mut self, symbol_name: &str, address: u64)
Patch the GOT/PLT entry for the given symbol name
pub fn add_section(
&mut self,
section: &Section<'_>,
loaded: bool,
pos: SecInsertPos
) -> Option<Section<'_>>
pub fn add_section( &mut self, section: &Section<'_>, loaded: bool, pos: SecInsertPos ) -> Option<Section<'_>>
Add a section to the binary. If loaded is true, the section will be
added in a way that it is loaded in memory. pos controls where the
section is inserted.
pub fn add_dynamic_relocation(
&mut self,
reloc: &Relocation<'_>
) -> Relocation<'_>
pub fn add_dynamic_relocation( &mut self, reloc: &Relocation<'_> ) -> Relocation<'_>
Add a dynamic relocation
pub fn add_pltgot_relocation(
&mut self,
reloc: &Relocation<'_>
) -> Relocation<'_>
pub fn add_pltgot_relocation( &mut self, reloc: &Relocation<'_> ) -> Relocation<'_>
Add a .plt.got relocation
pub fn add_symtab_symbol(&mut self, symbol: &Symbol<'_>) -> Symbol<'_>
pub fn add_symtab_symbol(&mut self, symbol: &Symbol<'_>) -> Symbol<'_>
Add a symbol to the .symtab table
pub fn add_dynamic_symbol(&mut self, symbol: &Symbol<'_>) -> Symbol<'_>
pub fn add_dynamic_symbol(&mut self, symbol: &Symbol<'_>) -> Symbol<'_>
Add a symbol to the dynamic symbol table (.dynsym)
pub fn add_exported_function(&mut self, address: u64, name: &str) -> Symbol<'_>
pub fn add_exported_function(&mut self, address: u64, name: &str) -> Symbol<'_>
Add an exported function with the given address and name
pub fn export_symbol_by_name(
&mut self,
symbol_name: &str,
value: u64
) -> Symbol<'_>
pub fn export_symbol_by_name( &mut self, symbol_name: &str, value: u64 ) -> Symbol<'_>
Export the symbol with the given name, optionally setting its value (can be 0)
pub fn export_symbol(&mut self, symbol: &Symbol<'_>) -> Symbol<'_>
pub fn export_symbol(&mut self, symbol: &Symbol<'_>) -> Symbol<'_>
Export an existing symbol
pub fn remove_symtab_symbol(&mut self, name: &str)
pub fn remove_symtab_symbol(&mut self, name: &str)
Remove the symtab symbol with the given name
pub fn remove_dynamic_symbol(&mut self, name: &str)
pub fn remove_dynamic_symbol(&mut self, name: &str)
Remove the dynamic symbol with the given name
pub fn remove_section(&mut self, section: &Section<'_>, clear: bool)
pub fn remove_section(&mut self, section: &Section<'_>, clear: bool)
Remove the given section. If clear is set, the section content will be
filled with zeros before removal.
pub fn remove_note(&mut self, note: &Notes<'_>)
pub fn remove_note(&mut self, note: &Notes<'_>)
Remove the given note
pub fn extend_segment(
&mut self,
segment: &Segment<'_>,
size: u64
) -> Option<Segment<'_>>
pub fn extend_segment( &mut self, segment: &Segment<'_>, size: u64 ) -> Option<Segment<'_>>
Extend the given segment by size bytes
pub fn extend_section(
&mut self,
section: &Section<'_>,
size: u64
) -> Option<Section<'_>>
pub fn extend_section( &mut self, section: &Section<'_>, size: u64 ) -> Option<Section<'_>>
Extend the given section by size bytes
pub fn strip(&mut self)
pub fn strip(&mut self)
Strip all debug symbols from the binary
pub fn section_idx_by_name(&self, name: &str) -> Option<usize>
pub fn section_idx_by_name(&self, name: &str) -> Option<usize>
Get the index of a section by its name. Returns None if not found.
pub fn section_idx(&self, section: &Section<'_>) -> Option<usize>
pub fn section_idx(&self, section: &Section<'_>) -> Option<usize>
Get the index of the given section. Returns None if not found.
pub fn relocate_phdr_table(&mut self, reloc_type: PhdrReloc) -> u64
pub fn relocate_phdr_table(&mut self, reloc_type: PhdrReloc) -> u64
Relocate the PHDR table using the given strategy. Returns the new offset of the PHDR table.
Trait Implementations§
§impl Binary for Binary
impl Binary for Binary
§fn entrypoint(&self) -> u64
fn entrypoint(&self) -> u64
§fn virtual_size(&self) -> u64
fn virtual_size(&self) -> u64
§fn original_size(&self) -> u64
fn original_size(&self) -> u64
§fn debug_info(&self) -> Option<DebugInfo<'_>>
fn debug_info(&self) -> Option<DebugInfo<'_>>
crate::pdb::DebugInfo or crate::dwarf::DebugInfo. Read more§fn disassemble(&self, address: u64, size: u64) -> InstructionsIt<'_> ⓘ
fn disassemble(&self, address: u64, size: u64) -> InstructionsIt<'_> ⓘ
§fn disassemble_symbol(&self, name: &str) -> InstructionsIt<'_> ⓘ
fn disassemble_symbol(&self, name: &str) -> InstructionsIt<'_> ⓘ
§fn disassemble_address(&self, address: u64) -> InstructionsIt<'_> ⓘ
fn disassemble_address(&self, address: u64) -> InstructionsIt<'_> ⓘ
§fn disassemble_slice(&self, slice: &[u8], address: u64) -> InstructionsIt<'_> ⓘ
fn disassemble_slice(&self, slice: &[u8], address: u64) -> InstructionsIt<'_> ⓘ
address parameter. Read more§fn assemble(&mut self, address: u64, asm: &str) -> Vec<u8>
fn assemble(&mut self, address: u64, asm: &str) -> Vec<u8>
§fn assemble_with_config(
&mut self,
address: u64,
asm: &str,
config: &AssemblerConfig
) -> Vec<u8>
fn assemble_with_config( &mut self, address: u64, asm: &str, config: &AssemblerConfig ) -> Vec<u8>
Binary::assemble but this function takes an extra AssemblerConfig that
is used to configure the assembly engine: dialect, symbols definitions.