LCOV - code coverage report
Current view: top level - src/elf - binary.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 78.4 % 259 203
Test Date: 2025-06-24:00:00:00 Functions: 61.2 % 85 52

            Line data    Source code
       1              : use std::mem::size_of;
       2              : use std::path::Path;
       3              : use std::pin::Pin;
       4              : 
       5              : use num_traits::{cast, Num};
       6              : 
       7              : use lief_ffi as ffi;
       8              : 
       9              : use super::parser_config::Config as ParserConfig;
      10              : use super::builder::Config;
      11              : use super::dynamic::{self, DynamicEntries, Library};
      12              : use super::hash::{Gnu, Sysv};
      13              : use super::header::Header;
      14              : use super::note::ItNotes;
      15              : use super::relocation::{
      16              :     DynamicRelocations, ObjectRelocations, PltGotRelocations, Relocation, Relocations,
      17              : };
      18              : use super::section::{Section, Sections};
      19              : use super::segment::{self, Segments};
      20              : use super::symbol::{DynamicSymbols, ExportedSymbols, ImportedSymbols, SymtabSymbols};
      21              : use super::symbol_versioning::{SymbolVersion, SymbolVersionDefinition, SymbolVersionRequirement};
      22              : use super::{Segment, Symbol};
      23              : use crate::Error;
      24              : 
      25              : use crate::common::{into_optional, FromFFI};
      26              : use crate::generic;
      27              : use crate::{declare_iterator, to_conv_result, to_result, to_slice};
      28              : 
      29            0 : #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
      30              : pub enum ElfClass {
      31              :     Elf32,
      32              :     Elf64,
      33              :     Unknown,
      34              : }
      35              : 
      36              : impl ElfClass {
      37              :     const ELF_CLASS32: u32 = 1;
      38              :     const ELF_CLASS64: u32 = 2;
      39              : 
      40            0 :     pub fn from_value(value: u32) -> Self {
      41            0 :         match value {
      42            0 :             Self::ELF_CLASS32 => ElfClass::Elf32,
      43            0 :             Self::ELF_CLASS64 => ElfClass::Elf64,
      44            0 :             _ => ElfClass::Unknown,
      45              :         }
      46            0 :     }
      47              : }
      48              : 
      49              : /// This is the main interface to read and write ELF binary attributes.
      50              : ///
      51              : /// Note that this structure implements the [`generic::Binary`] trait from which other generic
      52              : /// functions are exposed
      53              : pub struct Binary {
      54              :     ptr: cxx::UniquePtr<ffi::ELF_Binary>,
      55              : }
      56              : 
      57              : impl std::fmt::Debug for Binary {
      58           96 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
      59           96 :         f.debug_struct("Binary")
      60           96 :             .field("header", &self.header())
      61           96 :             .finish()
      62           96 :     }
      63              : }
      64              : 
      65              : impl FromFFI<ffi::ELF_Binary> for Binary {
      66          216 :     fn from_ffi(ptr: cxx::UniquePtr<ffi::ELF_Binary>) -> Self {
      67          216 :         Self { ptr }
      68          216 :     }
      69              : }
      70              : 
      71              : impl Binary {
      72              :     /// Create a [`Binary`] from the given file path
      73          120 :     pub fn parse(path: &str) -> Option<Self> {
      74          120 :         let bin = ffi::ELF_Binary::parse(path);
      75          120 :         if bin.is_null() {
      76            0 :             return None;
      77          120 :         }
      78          120 :         Some(Binary::from_ffi(bin))
      79          120 :     }
      80              : 
      81              : 
      82              :     /// Parse from a string file path and with a provided configuration
      83            0 :     pub fn parse_with_config(path: &str, config: &ParserConfig) -> Option<Self> {
      84            0 :         let ffi_config = config.to_ffi();
      85            0 :         let ffi = ffi::ELF_Binary::parse_with_config(path, &ffi_config);
      86            0 :         if ffi.is_null() {
      87            0 :             return None;
      88            0 :         }
      89            0 :         Some(Binary::from_ffi(ffi))
      90            0 :     }
      91              : 
      92              :     /// Return the main ELF header
      93           96 :     pub fn header(&self) -> Header {
      94           96 :         Header::from_ffi(self.ptr.header())
      95           96 :     }
      96              : 
      97              :     /// Return the size taken by the binary when loaded (virtual size)
      98           96 :     pub fn virtual_size(&self) -> u64 {
      99           96 :         self.ptr.virtual_size()
     100           96 :     }
     101              : 
     102              :     /// Return the path to the ELF interpreter that is used to process the ELF information
     103              :     /// once loaded by the kernel
     104           96 :     pub fn interpreter(&self) -> String {
     105           96 :         self.ptr.interpreter().to_string()
     106           96 :     }
     107              : 
     108              :     /// Return sysv-hash information (if present)
     109           96 :     pub fn sysv_hash(&self) -> Option<Sysv> {
     110           96 :         into_optional(self.ptr.sysv_hash())
     111           96 :     }
     112              : 
     113              :     /// Return GNU Hash info (if present)
     114           96 :     pub fn gnu_hash(&self) -> Option<Gnu> {
     115           96 :         into_optional(self.ptr.gnu_hash())
     116           96 :     }
     117              : 
     118              :     /// Return an iterator over the [`crate::elf::Section`] of the binary
     119           96 :     pub fn sections(&self) -> Sections {
     120           96 :         Sections::new(self.ptr.sections())
     121           96 :     }
     122              : 
     123              :     /// Return an iterator over the [`crate::elf::Segment`] of the binary
     124           96 :     pub fn segments(&self) -> Segments {
     125           96 :         Segments::new(self.ptr.segments())
     126           96 :     }
     127              : 
     128              :     /// Return an iterator over the [`crate::elf::DynamicEntries`] of the binary
     129           96 :     pub fn dynamic_entries(&self) -> DynamicEntries {
     130           96 :         DynamicEntries::new(self.ptr.dynamic_entries())
     131           96 :     }
     132              : 
     133              :     /// Remove **all** dynamic entries with the given tag
     134            0 :     pub fn remove_dynamic_entries_by_tag(&mut self, tag: dynamic::Tag) {
     135            0 :         self.ptr.as_mut().unwrap().remove_dynamic_entries_by_tag(tag.into())
     136            0 :     }
     137              : 
     138              :     /// Add the given dynamic entry and return the new entry
     139            0 :     pub fn add_dynamic_entry(&mut self, entry: &dyn dynamic::DynamicEntry) -> dynamic::Entries {
     140            0 :         dynamic::Entries::from_ffi(self.ptr.as_mut().unwrap().add_dynamic_entry(entry.as_base()))
     141            0 :     }
     142              : 
     143              :     /// Return an iterator over the dynamic [`crate::elf::Symbol`] of the binary
     144           96 :     pub fn dynamic_symbols(&self) -> DynamicSymbols {
     145           96 :         DynamicSymbols::new(self.ptr.dynamic_symbols())
     146           96 :     }
     147              : 
     148              :     /// Return an iterator over the **exported** [`crate::elf::Symbol`] of the binary
     149           96 :     pub fn exported_symbols(&self) -> ExportedSymbols {
     150           96 :         ExportedSymbols::new(self.ptr.exported_symbols())
     151           96 :     }
     152              : 
     153              :     /// Return an iterator over the **imported** [`crate::elf::Symbol`] of the binary
     154           96 :     pub fn imported_symbols(&self) -> ImportedSymbols {
     155           96 :         ImportedSymbols::new(self.ptr.imported_symbols())
     156           96 :     }
     157              : 
     158              :     /// Return an iterator over the symtab-debug [`crate::elf::Symbol`] of the binary
     159           96 :     pub fn symtab_symbols(&self) -> SymtabSymbols {
     160           96 :         SymtabSymbols::new(self.ptr.symtab_symbols())
     161           96 :     }
     162              : 
     163              :     /// Return an iterator over the  [`crate::elf::SymbolVersion`] of the binary
     164          192 :     pub fn symbols_version(&self) -> SymbolsVersion {
     165          192 :         SymbolsVersion::new(self.ptr.symbols_version())
     166          192 :     }
     167              : 
     168              :     /// Return an iterator over the  [`crate::elf::SymbolVersionRequirement`] of the binary
     169           96 :     pub fn symbols_version_requirement(&self) -> SymbolsVersionRequirement {
     170           96 :         SymbolsVersionRequirement::new(self.ptr.symbols_version_requirement())
     171           96 :     }
     172              : 
     173              :     /// Return an iterator over the  [`crate::elf::SymbolVersionDefinition`] of the binary
     174           96 :     pub fn symbols_version_definition(&self) -> SymbolsVersionDefinition {
     175           96 :         SymbolsVersionDefinition::new(self.ptr.symbols_version_definition())
     176           96 :     }
     177              : 
     178              :     /// Return an iterator over the  [`crate::elf::Notes`] of the binary
     179           96 :     pub fn notes(&self) -> ItNotes {
     180           96 :         ItNotes::new(self.ptr.notes())
     181           96 :     }
     182              : 
     183              :     /// Return an iterator over the `.plt.got` [`crate::elf::Relocation`] of the binary
     184           96 :     pub fn pltgot_relocations(&self) -> PltGotRelocations {
     185           96 :         PltGotRelocations::new(self.ptr.pltgot_relocations())
     186           96 :     }
     187              : 
     188              :     /// Return an iterator over the regular [`crate::elf::Relocation`] of the binary
     189           96 :     pub fn dynamic_relocations(&self) -> DynamicRelocations {
     190           96 :         DynamicRelocations::new(self.ptr.dynamic_relocations())
     191           96 :     }
     192              : 
     193              :     /// Return an iterator over the object-file (`.o`) [`crate::elf::Relocation`]
     194           96 :     pub fn object_relocations(&self) -> ObjectRelocations {
     195           96 :         ObjectRelocations::new(self.ptr.object_relocations())
     196           96 :     }
     197              : 
     198              :     /// Return an iterator over **all** [`crate::elf::Relocation`] of the binary
     199           96 :     pub fn relocations(&self) -> Relocations {
     200           96 :         Relocations::new(self.ptr.relocations())
     201           96 :     }
     202              : 
     203              :     /// Try to find the ELF section with the given name
     204           96 :     pub fn section_by_name(&self, name: &str) -> Option<Section> {
     205           96 :         into_optional(self.ptr.section_by_name(name))
     206           96 :     }
     207              : 
     208              :     /// Try to find the ELF relocation that takes place at the given address
     209           24 :     pub fn relocation_by_addr(&self, address: u64) -> Option<Relocation> {
     210           24 :         into_optional(self.ptr.relocation_by_addr(address))
     211           24 :     }
     212              : 
     213              :     /// Try to find the `.plt.got` relocation for the given symbol name
     214           24 :     pub fn relocation_for_symbol(&self, sym_name: &str) -> Option<Relocation> {
     215           24 :         into_optional(self.ptr.relocation_for_symbol(sym_name))
     216           24 :     }
     217              : 
     218              :     /// Try to find the symbol with the given name in the dynamic `.dynsym` table
     219           12 :     pub fn dynamic_symbol_by_name(&self, sym_name: &str) -> Option<Symbol> {
     220           12 :         into_optional(self.ptr.get_dynamic_symbol(sym_name))
     221           12 :     }
     222              : 
     223              :     /// Try to find the symbol with the given name in the debug `.symtab` table
     224           12 :     pub fn symtab_symbol_by_name(&self, sym_name: &str) -> Option<Symbol> {
     225           12 :         into_optional(self.ptr.get_symtab_symbol(sym_name))
     226           12 :     }
     227              : 
     228              :     /// Try to find the library (`DT_NEEDED`) with the given name
     229           24 :     pub fn get_library(&self, name: &str) -> Option<dynamic::Library> {
     230           24 :         into_optional(self.ptr.get_library(name))
     231           24 :     }
     232              : 
     233              :     /// Try to find the section that encompasses the given offset. `skip_nobits` can be used
     234              :     /// to include (or not) the `SHT_NOTBIT` sections
     235           24 :     pub fn section_from_offset(&self, offset: u64, skip_nobits: bool) -> Option<Section> {
     236           24 :         into_optional(self.ptr.section_from_offset(offset, skip_nobits))
     237           24 :     }
     238              : 
     239              :     /// Try to find the section that encompasses the given virtual address. `skip_nobits` can be used
     240              :     /// to include (or not) the `SHT_NOTBIT` sections
     241           24 :     pub fn section_from_virtual_address(&self, address: u64, skip_nobits: bool) -> Option<Section> {
     242           24 :         into_optional(self.ptr.section_from_virtual_address(address, skip_nobits))
     243           24 :     }
     244              : 
     245              :     /// Try to find the segment that encompasses the given virtual address
     246           24 :     pub fn segment_from_virtual_address(&self, address: u64) -> Option<Segment> {
     247           24 :         into_optional(self.ptr.segment_from_virtual_address(address))
     248           24 :     }
     249              : 
     250              :     /// Try to find the segment that encompasses the given offset
     251           24 :     pub fn segment_from_offset(&self, offset: u64) -> Option<Segment> {
     252           24 :         into_optional(self.ptr.segment_from_offset(offset))
     253           24 :     }
     254              : 
     255              :     /// Get a slice of the content at the given address.
     256           12 :     pub fn content_from_virtual_address(&self, address: u64, size: u64) -> &[u8] {
     257           12 :         to_slice!(self.ptr.get_content_from_virtual_address(address, size));
     258           12 :     }
     259              : 
     260              :     /// Convert the given virtual address into an offset
     261           24 :     pub fn virtual_address_to_offset(&self, address: u64) -> Result<u64, Error> {
     262           24 :         to_result!(ffi::ELF_Binary::virtual_address_to_offset, &self, address);
     263           24 :     }
     264              : 
     265              :     /// Return the array defined by the given tag (e.g.
     266              :     /// [`dynamic::Tag::INIT_ARRAY`]) with relocations applied (if any)
     267          288 :     pub fn get_relocated_dynamic_array(&self, tag: dynamic::Tag) -> Vec<u64> {
     268          288 :         Vec::from(
     269          288 :             self.ptr
     270          288 :                 .get_relocated_dynamic_array(u64::from(tag))
     271          288 :                 .as_slice(),
     272          288 :         )
     273          288 :     }
     274              : 
     275              :     /// True if the current binary is targeting Android
     276            0 :     pub fn is_targeting_android(&self) -> bool {
     277            0 :         self.ptr.is_targeting_android()
     278            0 :     }
     279              : 
     280              :     /// Get the integer value at the given virtual address
     281            4 :     pub fn get_int_from_virtual_address<T>(&self, addr: u64) -> Result<T, Error>
     282            4 :     where
     283            4 :         T: Num + cast::FromPrimitive + cast::ToPrimitive,
     284            4 :     {
     285            4 :         // Can't be in the generic trait because of:
     286            4 :         //   > for a trait to be "object safe" it needs to allow building a vtable to allow the call
     287            4 :         //   > to be resolvable dynamically; for more information visit
     288            4 :         //   > https://doc.rust-lang.org/reference/items/traits.html#object-safety
     289            4 :         if size_of::<T>() == size_of::<u8>() {
     290            1 :             to_conv_result!(
     291            1 :                 ffi::AbstractBinary::get_u8,
     292            1 :                 self.ptr.as_ref().unwrap().as_ref(),
     293            1 :                 |value| {
     294            1 :                     T::from_u8(value).expect(format!("Can't cast value: {}", value).as_str())
     295            1 :                 },
     296            1 :                 addr
     297              :             );
     298            3 :         }
     299            3 : 
     300            3 :         if size_of::<T>() == size_of::<u16>() {
     301            1 :             to_conv_result!(
     302            1 :                 ffi::AbstractBinary::get_u16,
     303            1 :                 self.ptr.as_ref().unwrap().as_ref(),
     304            1 :                 |value| {
     305            1 :                     T::from_u16(value).expect(format!("Can't cast value: {}", value).as_str())
     306            1 :                 },
     307            1 :                 addr
     308              :             );
     309            2 :         }
     310            2 : 
     311            2 :         if size_of::<T>() == size_of::<u32>() {
     312            1 :             to_conv_result!(
     313            1 :                 ffi::AbstractBinary::get_u32,
     314            1 :                 self.ptr.as_ref().unwrap().as_ref(),
     315            1 :                 |value| {
     316            1 :                     T::from_u32(value).expect(format!("Can't cast value: {}", value).as_str())
     317            1 :                 },
     318            1 :                 addr
     319              :             );
     320            1 :         }
     321            1 : 
     322            1 :         if size_of::<T>() == size_of::<u64>() {
     323            1 :             to_conv_result!(
     324            1 :                 ffi::AbstractBinary::get_u64,
     325            1 :                 self.ptr.as_ref().unwrap().as_ref(),
     326            1 :                 |value| {
     327            1 :                     T::from_u64(value).expect(format!("Can't cast value: {}", value).as_str())
     328            1 :                 },
     329            1 :                 addr
     330              :             );
     331            0 :         }
     332            0 : 
     333            0 :         Err(Error::NotSupported)
     334            4 :     }
     335              : 
     336              :     /// Write back the current ELF binary into the file specified in parameter
     337           12 :     pub fn write(&mut self, output: &Path) {
     338           12 :         self.ptr.as_mut().unwrap().write(output.to_str().unwrap());
     339           12 :     }
     340              : 
     341              :     /// Write back the current ELF binary into the file specified in parameter with the
     342              :     /// configuration provided in the second parameter.
     343           12 :     pub fn write_with_config(&mut self, output: &Path, config: Config) {
     344           12 :         self.ptr
     345           12 :             .as_mut()
     346           12 :             .unwrap()
     347           12 :             .write_with_config(output.to_str().unwrap(), config.to_ffi());
     348           12 :     }
     349              : 
     350              :     /// Add a library as dependency
     351           12 :     pub fn add_library<'a>(&'a mut self, library: &str) -> Library<'a> {
     352           12 :         Library::from_ffi(self.ptr.as_mut().unwrap().add_library(library))
     353           12 :     }
     354              : 
     355              :     /// Iterator over the functions found in this binary
     356           96 :     pub fn functions(&self) -> generic::Functions {
     357           96 :         generic::Functions::new(self.ptr.functions())
     358           96 :     }
     359              : 
     360              :     /// Try to find the dynamic entry associated with the given tag
     361            0 :     pub fn dynamic_entry_by_tag(&self, tag: dynamic::Tag) -> Option<dynamic::Entries> {
     362            0 :         into_optional(self.ptr.dynamic_entry_by_tag(tag.into()))
     363            0 :     }
     364              : 
     365              :     /// Look for the segment with the given type. If there are multiple segment
     366              :     /// with the same type, it returns the first one.
     367            0 :     pub fn segment_by_type(&self, seg_type: segment::Type) -> Option<Segment> {
     368            0 :         into_optional(self.ptr.segment_by_type(seg_type.into()))
     369            0 :     }
     370              : 
     371              :     /// Remove the given dynamic entry
     372            0 :     pub fn remove_dynamic_entry(&mut self, entry: impl dynamic::DynamicEntry) {
     373            0 :         self.ptr.pin_mut().remove_dynamic_entry(entry.as_base());
     374            0 :     }
     375              : 
     376              :     /// Remove the `DT_NEEDED` dependency with the given name
     377            0 :     pub fn remove_library(&mut self, name: &str) {
     378            0 :         self.ptr.pin_mut().remove_library(name.to_string());
     379            0 :     }
     380              : 
     381              :     /// Add the provided segment to the binary. This function returns the
     382              :     /// newly added segment which could define additional attributes like the virtual address.
     383            0 :     pub fn add_segment(&mut self, segment: &Segment) -> Option<Segment> {
     384            0 :         into_optional(self.ptr.pin_mut().add_segment(segment.ptr.as_ref().unwrap()))
     385            0 :     }
     386              : 
     387              :     /// Change the path to the interpreter
     388            0 :     pub fn set_interpreter(&mut self, interpreter: &str) {
     389            0 :         self.ptr.pin_mut().set_interpreter(interpreter.to_string());
     390            0 :     }
     391              : }
     392              : 
     393              : impl generic::Binary for Binary {
     394          480 :     fn as_generic(&self) -> &ffi::AbstractBinary {
     395          480 :         self.ptr.as_ref().unwrap().as_ref()
     396          480 :     }
     397              : 
     398            0 :     fn as_pin_mut_generic(&mut self) -> Pin<&mut ffi::AbstractBinary> {
     399            0 :         unsafe {
     400            0 :             Pin::new_unchecked({
     401            0 :                 (self.ptr.as_ref().unwrap().as_ref() as *const ffi::AbstractBinary
     402            0 :                     as *mut ffi::AbstractBinary)
     403            0 :                     .as_mut()
     404            0 :                     .unwrap()
     405            0 :             })
     406            0 :         }
     407            0 :     }
     408              : }
     409              : 
     410        18648 : declare_iterator!(
     411        18648 :     SymbolsVersion,
     412        18648 :     SymbolVersion<'a>,
     413        18648 :     ffi::ELF_SymbolVersion,
     414        18648 :     ffi::ELF_Binary,
     415        18648 :     ffi::ELF_Binary_it_symbols_version
     416        18648 : );
     417          180 : declare_iterator!(
     418          180 :     SymbolsVersionRequirement,
     419          180 :     SymbolVersionRequirement<'a>,
     420          180 :     ffi::ELF_SymbolVersionRequirement,
     421          180 :     ffi::ELF_Binary,
     422          180 :     ffi::ELF_Binary_it_symbols_version_requirement
     423          180 : );
     424           36 : declare_iterator!(
     425           36 :     SymbolsVersionDefinition,
     426           36 :     SymbolVersionDefinition<'a>,
     427           36 :     ffi::ELF_SymbolVersionDefinition,
     428           36 :     ffi::ELF_Binary,
     429           36 :     ffi::ELF_Binary_it_symbols_version_definition
     430           36 : );
        

Generated by: LCOV version 2.1-1