LCOV - code coverage report
Current view: top level - src/elf - binary.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 87.7 % 187 164
Test Date: 2025-01-11:00:00:00 Functions: 68.0 % 75 51

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

Generated by: LCOV version 2.1-1