LCOV - code coverage report
Current view: top level - src/macho/commands - dynamic_symbol_command.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 100.0 % 112 112
Test Date: 2024-10-27:00:00:00 Functions: 100.0 % 23 23

            Line data    Source code
       1              : use super::Command;
       2              : use crate::common::FromFFI;
       3              : use crate::macho::Symbol;
       4              : use lief_ffi as ffi;
       5              : use std::marker::PhantomData;
       6              : 
       7              : use crate::declare_iterator;
       8              : 
       9              : /// Structure that represents the `LC_DYSYMTAB` command.
      10              : ///
      11              : /// This command completes the `LC_SYMTAB` to provide
      12              : /// a better granularity over the symbols layout.
      13              : pub struct DynamicSymbolCommand<'a> {
      14              :     ptr: cxx::UniquePtr<ffi::MachO_DynamicSymbolCommand>,
      15              :     _owner: PhantomData<&'a ffi::MachO_Binary>,
      16              : }
      17              : 
      18              : impl DynamicSymbolCommand<'_> {
      19              :     /// Index of the first symbol in the group of local symbols.
      20          224 :     pub fn idx_local_symbol(&self) -> u32 {
      21          224 :         self.ptr.idx_local_symbol()
      22          224 :     }
      23              : 
      24              :     /// Number of symbols in the group of local symbols.
      25          224 :     pub fn nb_local_symbols(&self) -> u32 {
      26          224 :         self.ptr.nb_local_symbols()
      27          224 :     }
      28              : 
      29              :     /// Index of the first symbol in the group of defined external symbols.
      30          224 :     pub fn idx_external_define_symbol(&self) -> u32 {
      31          224 :         self.ptr.idx_external_define_symbol()
      32          224 :     }
      33              : 
      34              :     /// Number of symbols in the group of defined external symbols.
      35          224 :     pub fn nb_external_define_symbols(&self) -> u32 {
      36          224 :         self.ptr.nb_external_define_symbols()
      37          224 :     }
      38              : 
      39              :     /// Index of the first symbol in the group of undefined external symbols.
      40          224 :     pub fn idx_undefined_symbol(&self) -> u32 {
      41          224 :         self.ptr.idx_undefined_symbol()
      42          224 :     }
      43              : 
      44              :     /// Number of symbols in the group of undefined external symbols.
      45          224 :     pub fn nb_undefined_symbols(&self) -> u32 {
      46          224 :         self.ptr.nb_undefined_symbols()
      47          224 :     }
      48              : 
      49              :     /// Byte offset from the start of the file to the table of contents data
      50              :     ///
      51              :     /// Table of content is used by legacy Mach-O loader and this field should be
      52              :     /// set to 0
      53          224 :     pub fn toc_offset(&self) -> u32 {
      54          224 :         self.ptr.toc_offset()
      55          224 :     }
      56              : 
      57              :     /// Number of entries in the table of contents.
      58              :     ///
      59              :     /// Should be set to 0 on recent Mach-O
      60          224 :     pub fn nb_toc(&self) -> u32 {
      61          224 :         self.ptr.nb_toc()
      62          224 :     }
      63              : 
      64              :     /// Byte offset from the start of the file to the module table data.
      65              :     ///
      66              :     /// This field seems unused by recent Mach-O loader and should be set to 0
      67          224 :     pub fn module_table_offset(&self) -> u32 {
      68          224 :         self.ptr.module_table_offset()
      69          224 :     }
      70              : 
      71              :     /// Number of entries in the module table.
      72              :     ///
      73              :     /// This field seems unused by recent Mach-O loader and should be set to 0
      74          224 :     pub fn nb_module_table(&self) -> u32 {
      75          224 :         self.ptr.nb_module_table()
      76          224 :     }
      77              : 
      78              :     /// Byte offset from the start of the file to the external reference table data.
      79              :     ///
      80              :     /// This field seems unused by recent Mach-O loader and should be set to 0
      81          224 :     pub fn external_reference_symbol_offset(&self) -> u32 {
      82          224 :         self.ptr.external_reference_symbol_offset()
      83          224 :     }
      84              : 
      85              :     /// Number of entries in the external reference table
      86              :     ///
      87              :     /// This field seems unused by recent Mach-O loader and should be set to 0
      88          224 :     pub fn nb_external_reference_symbols(&self) -> u32 {
      89          224 :         self.ptr.nb_external_reference_symbols()
      90          224 :     }
      91              : 
      92              :     /// Byte offset from the start of the file to the indirect symbol table data.
      93              :     ///
      94              :     /// Indirect symbol table is used by the loader to speed-up symbol resolution during
      95              :     /// the *lazy binding* process
      96              :     ///
      97              :     /// References:
      98              :     ///   * dyld-519.2.1/src/ImageLoaderMachOCompressed.cpp
      99              :     ///   * dyld-519.2.1/src/ImageLoaderMachOClassic.cpp
     100          224 :     pub fn indirect_symbol_offset(&self) -> u32 {
     101          224 :         self.ptr.indirect_symbol_offset()
     102          224 :     }
     103              : 
     104              :     /// Number of entries in the indirect symbol table.
     105          224 :     pub fn nb_indirect_symbols(&self) -> u32 {
     106          224 :         self.ptr.nb_indirect_symbols()
     107          224 :     }
     108              : 
     109              :     /// Byte offset from the start of the file to the external relocation table data.
     110              :     ///
     111              :     /// This field seems unused by recent Mach-O loader and should be set to 0
     112          224 :     pub fn external_relocation_offset(&self) -> u32 {
     113          224 :         self.ptr.external_relocation_offset()
     114          224 :     }
     115              : 
     116              :     /// Number of entries in the external relocation table.
     117              :     ///
     118              :     /// This field seems unused by recent Mach-O loader and should be set to 0
     119          224 :     pub fn nb_external_relocations(&self) -> u32 {
     120          224 :         self.ptr.nb_external_relocations()
     121          224 :     }
     122              : 
     123              :     /// Byte offset from the start of the file to the local relocation table data.
     124              :     ///
     125              :     /// This field seems unused by recent Mach-O loader and should be set to 0
     126          224 :     pub fn local_relocation_offset(&self) -> u32 {
     127          224 :         self.ptr.local_relocation_offset()
     128          224 :     }
     129              : 
     130              :     /// Number of entries in the local relocation table.
     131              :     ///
     132              :     /// This field seems unused by recent Mach-O loader and should be set to 0
     133          224 :     pub fn nb_local_relocations(&self) -> u32 {
     134          224 :         self.ptr.nb_local_relocations()
     135          224 :     }
     136              : 
     137              :     /// Iterator over the indirect symbols indexed by this command
     138          112 :     pub fn indirect_symbols(&self) -> IndirectSymbols {
     139          112 :         IndirectSymbols::new(self.ptr.indirect_symbols())
     140          112 :     }
     141              : }
     142              : 
     143              : impl std::fmt::Debug for DynamicSymbolCommand<'_> {
     144          224 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     145          224 :         let base = self as &dyn Command;
     146          224 :         f.debug_struct("DynamicSymbolCommand")
     147          224 :             .field("base", &base)
     148          224 :             .field("idx_local_symbol", &self.idx_local_symbol())
     149          224 :             .field("nb_local_symbols", &self.nb_local_symbols())
     150          224 :             .field(
     151          224 :                 "idx_external_define_symbol",
     152          224 :                 &self.idx_external_define_symbol(),
     153          224 :             )
     154          224 :             .field(
     155          224 :                 "nb_external_define_symbols",
     156          224 :                 &self.nb_external_define_symbols(),
     157          224 :             )
     158          224 :             .field("idx_undefined_symbol", &self.idx_undefined_symbol())
     159          224 :             .field("nb_undefined_symbols", &self.nb_undefined_symbols())
     160          224 :             .field("toc_offset", &self.toc_offset())
     161          224 :             .field("nb_toc", &self.nb_toc())
     162          224 :             .field("module_table_offset", &self.module_table_offset())
     163          224 :             .field("nb_module_table", &self.nb_module_table())
     164          224 :             .field(
     165          224 :                 "external_reference_symbol_offset",
     166          224 :                 &self.external_reference_symbol_offset(),
     167          224 :             )
     168          224 :             .field(
     169          224 :                 "nb_external_reference_symbols",
     170          224 :                 &self.nb_external_reference_symbols(),
     171          224 :             )
     172          224 :             .field("indirect_symbol_offset", &self.indirect_symbol_offset())
     173          224 :             .field("nb_indirect_symbols", &self.nb_indirect_symbols())
     174          224 :             .field(
     175          224 :                 "external_relocation_offset",
     176          224 :                 &self.external_relocation_offset(),
     177          224 :             )
     178          224 :             .field("nb_external_relocations", &self.nb_external_relocations())
     179          224 :             .field("local_relocation_offset", &self.local_relocation_offset())
     180          224 :             .field("nb_local_relocations", &self.nb_local_relocations())
     181          224 :             .finish()
     182          224 :     }
     183              : }
     184              : 
     185              : impl FromFFI<ffi::MachO_DynamicSymbolCommand> for DynamicSymbolCommand<'_> {
     186          224 :     fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_DynamicSymbolCommand>) -> Self {
     187          224 :         Self {
     188          224 :             ptr: cmd,
     189          224 :             _owner: PhantomData,
     190          224 :         }
     191          224 :     }
     192              : }
     193              : 
     194              : impl Command for DynamicSymbolCommand<'_> {
     195          896 :     fn get_base(&self) -> &ffi::MachO_Command {
     196          896 :         self.ptr.as_ref().unwrap().as_ref()
     197          896 :     }
     198              : }
     199              : 
     200        33664 : declare_iterator!(
     201        33664 :     IndirectSymbols,
     202        33664 :     Symbol<'a>,
     203        33664 :     ffi::MachO_Symbol,
     204        33664 :     ffi::MachO_DynamicSymbolCommand,
     205        33664 :     ffi::MachO_DynamicSymbolCommand_it_indirect_symbols
     206        33664 : );
        

Generated by: LCOV version 2.1-1