LCOV - code coverage report
Current view: top level - src/macho/commands - data_in_code.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 95.6 % 68 65
Test Date: 2026-04-12:00:00:00 Functions: 83.3 % 18 15

            Line data    Source code
       1              : use super::Command;
       2              : use lief_ffi as ffi;
       3              : 
       4              : use crate::to_slice;
       5              : use crate::{common::FromFFI, declare_iterator};
       6              : use std::marker::PhantomData;
       7              : 
       8              : /// Structure that represents the `LC_DATA_IN_CODE` command
       9              : ///
      10              : /// This command is used to list slices of code sections that contain data. The *slices*
      11              : /// information are stored as an array of [`DataCodeEntry`]
      12              : pub struct DataInCode<'a> {
      13              :     ptr: cxx::UniquePtr<ffi::MachO_DataInCode>,
      14              :     _owner: PhantomData<&'a ffi::MachO_Binary>,
      15              : }
      16              : 
      17              : impl DataInCode<'_> {
      18              :     /// Start of the array of the [`DataCodeEntry`] entries
      19          338 :     pub fn data_offset(&self) -> u32 {
      20          338 :         self.ptr.data_offset()
      21          338 :     }
      22              : 
      23              :     /// Size of the (raw) array (`size = sizeof(DataCodeEntry) * nb_elements`)
      24          338 :     pub fn data_size(&self) -> u32 {
      25          338 :         self.ptr.data_size()
      26          338 :     }
      27              : 
      28              :     /// Raw content as a slice of bytes
      29          169 :     pub fn content(&self) -> &[u8] {
      30          169 :         to_slice!(self.ptr.content());
      31          169 :     }
      32              : 
      33              :     /// Iterator over the [`DataCodeEntry`]
      34          169 :     pub fn entries(&self) -> Entries<'_> {
      35          169 :         Entries::new(self.ptr.entries())
      36          169 :     }
      37              : }
      38              : 
      39              : impl std::fmt::Debug for DataInCode<'_> {
      40          338 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
      41          338 :         let base = self as &dyn Command;
      42          338 :         f.debug_struct("DataInCode")
      43          338 :             .field("base", &base)
      44          338 :             .field("data_offset", &self.data_offset())
      45          338 :             .field("data_size", &self.data_size())
      46          338 :             .finish()
      47          338 :     }
      48              : }
      49              : 
      50              : impl FromFFI<ffi::MachO_DataInCode> for DataInCode<'_> {
      51          338 :     fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_DataInCode>) -> Self {
      52          338 :         Self {
      53          338 :             ptr: cmd,
      54          338 :             _owner: PhantomData,
      55          338 :         }
      56          338 :     }
      57              : }
      58              : 
      59              : impl Command for DataInCode<'_> {
      60         1352 :     fn get_base(&self) -> &ffi::MachO_Command {
      61         1352 :         self.ptr.as_ref().unwrap().as_ref()
      62         1352 :     }
      63              : }
      64              : 
      65              : pub struct DataCodeEntry<'a> {
      66              :     ptr: cxx::UniquePtr<ffi::MachO_DataCodeEntry>,
      67              :     _owner: PhantomData<&'a ffi::MachO_DataInCode>,
      68              : }
      69              : 
      70              : #[allow(non_camel_case_types)]
      71        33124 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
      72              : pub enum EntryType {
      73              :     DATA,
      74              :     JUMP_TABLE_8,
      75              :     JUMP_TABLE_16,
      76              :     JUMP_TABLE_32,
      77              :     ABS_JUMP_TABLE_32,
      78              :     UNKNOWN(u32),
      79              : }
      80              : 
      81              : impl From<u32> for EntryType {
      82        33124 :     fn from(value: u32) -> Self {
      83        33124 :         match value {
      84        31044 :             0x00000001 => EntryType::DATA,
      85         1508 :             0x00000002 => EntryType::JUMP_TABLE_8,
      86          572 :             0x00000003 => EntryType::JUMP_TABLE_16,
      87            0 :             0x00000004 => EntryType::JUMP_TABLE_32,
      88            0 :             0x00000005 => EntryType::ABS_JUMP_TABLE_32,
      89            0 :             _ => EntryType::UNKNOWN(value),
      90              :         }
      91        33124 :     }
      92              : }
      93              : 
      94              : impl DataCodeEntry<'_> {
      95              :     /// Offset of the data
      96        33124 :     pub fn offset(&self) -> u32 {
      97        33124 :         self.ptr.offset()
      98        33124 :     }
      99              :     /// Length of the data
     100        33124 :     pub fn length(&self) -> u32 {
     101        33124 :         self.ptr.length()
     102        33124 :     }
     103              : 
     104              :     /// Type of the data
     105        33124 :     pub fn get_type(&self) -> EntryType {
     106        33124 :         EntryType::from(self.ptr.get_type())
     107        33124 :     }
     108              : }
     109              : 
     110              : impl std::fmt::Debug for DataCodeEntry<'_> {
     111        33124 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     112        33124 :         f.debug_struct("DataCodeEntry")
     113        33124 :             .field("offset", &self.offset())
     114        33124 :             .field("length", &self.length())
     115        33124 :             .field("type", &self.get_type())
     116        33124 :             .finish()
     117        33124 :     }
     118              : }
     119              : 
     120              : impl FromFFI<ffi::MachO_DataCodeEntry> for DataCodeEntry<'_> {
     121        33124 :     fn from_ffi(ptr: cxx::UniquePtr<ffi::MachO_DataCodeEntry>) -> Self {
     122        33124 :         Self {
     123        33124 :             ptr,
     124        33124 :             _owner: PhantomData,
     125        33124 :         }
     126        33124 :     }
     127              : }
     128              : 
     129        33124 : declare_iterator!(
     130        33124 :     Entries,
     131        33124 :     DataCodeEntry<'a>,
     132        33124 :     ffi::MachO_DataCodeEntry,
     133        33124 :     ffi::MachO_DataInCode,
     134        33124 :     ffi::MachO_DataInCode_it_entries
     135        33124 : );
        

Generated by: LCOV version 2.1-1