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: 2025-08-10: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          360 :     pub fn idx_local_symbol(&self) -> u32 {
      21          360 :         self.ptr.idx_local_symbol()
      22          360 :     }
      23              : 
      24              :     /// Number of symbols in the group of local symbols.
      25          360 :     pub fn nb_local_symbols(&self) -> u32 {
      26          360 :         self.ptr.nb_local_symbols()
      27          360 :     }
      28              : 
      29              :     /// Index of the first symbol in the group of defined external symbols.
      30          360 :     pub fn idx_external_define_symbol(&self) -> u32 {
      31          360 :         self.ptr.idx_external_define_symbol()
      32          360 :     }
      33              : 
      34              :     /// Number of symbols in the group of defined external symbols.
      35          360 :     pub fn nb_external_define_symbols(&self) -> u32 {
      36          360 :         self.ptr.nb_external_define_symbols()
      37          360 :     }
      38              : 
      39              :     /// Index of the first symbol in the group of undefined external symbols.
      40          360 :     pub fn idx_undefined_symbol(&self) -> u32 {
      41          360 :         self.ptr.idx_undefined_symbol()
      42          360 :     }
      43              : 
      44              :     /// Number of symbols in the group of undefined external symbols.
      45          360 :     pub fn nb_undefined_symbols(&self) -> u32 {
      46          360 :         self.ptr.nb_undefined_symbols()
      47          360 :     }
      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          360 :     pub fn toc_offset(&self) -> u32 {
      54          360 :         self.ptr.toc_offset()
      55          360 :     }
      56              : 
      57              :     /// Number of entries in the table of contents.
      58              :     ///
      59              :     /// Should be set to 0 on recent Mach-O
      60          360 :     pub fn nb_toc(&self) -> u32 {
      61          360 :         self.ptr.nb_toc()
      62          360 :     }
      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          360 :     pub fn module_table_offset(&self) -> u32 {
      68          360 :         self.ptr.module_table_offset()
      69          360 :     }
      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          360 :     pub fn nb_module_table(&self) -> u32 {
      75          360 :         self.ptr.nb_module_table()
      76          360 :     }
      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          360 :     pub fn external_reference_symbol_offset(&self) -> u32 {
      82          360 :         self.ptr.external_reference_symbol_offset()
      83          360 :     }
      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          360 :     pub fn nb_external_reference_symbols(&self) -> u32 {
      89          360 :         self.ptr.nb_external_reference_symbols()
      90          360 :     }
      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          360 :     pub fn indirect_symbol_offset(&self) -> u32 {
     101          360 :         self.ptr.indirect_symbol_offset()
     102          360 :     }
     103              : 
     104              :     /// Number of entries in the indirect symbol table.
     105          360 :     pub fn nb_indirect_symbols(&self) -> u32 {
     106          360 :         self.ptr.nb_indirect_symbols()
     107          360 :     }
     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          360 :     pub fn external_relocation_offset(&self) -> u32 {
     113          360 :         self.ptr.external_relocation_offset()
     114          360 :     }
     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          360 :     pub fn nb_external_relocations(&self) -> u32 {
     120          360 :         self.ptr.nb_external_relocations()
     121          360 :     }
     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          360 :     pub fn local_relocation_offset(&self) -> u32 {
     127          360 :         self.ptr.local_relocation_offset()
     128          360 :     }
     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          360 :     pub fn nb_local_relocations(&self) -> u32 {
     134          360 :         self.ptr.nb_local_relocations()
     135          360 :     }
     136              : 
     137              :     /// Iterator over the indirect symbols indexed by this command
     138          180 :     pub fn indirect_symbols(&self) -> IndirectSymbols {
     139          180 :         IndirectSymbols::new(self.ptr.indirect_symbols())
     140          180 :     }
     141              : }
     142              : 
     143              : impl std::fmt::Debug for DynamicSymbolCommand<'_> {
     144          360 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     145          360 :         let base = self as &dyn Command;
     146          360 :         f.debug_struct("DynamicSymbolCommand")
     147          360 :             .field("base", &base)
     148          360 :             .field("idx_local_symbol", &self.idx_local_symbol())
     149          360 :             .field("nb_local_symbols", &self.nb_local_symbols())
     150          360 :             .field(
     151          360 :                 "idx_external_define_symbol",
     152          360 :                 &self.idx_external_define_symbol(),
     153          360 :             )
     154          360 :             .field(
     155          360 :                 "nb_external_define_symbols",
     156          360 :                 &self.nb_external_define_symbols(),
     157          360 :             )
     158          360 :             .field("idx_undefined_symbol", &self.idx_undefined_symbol())
     159          360 :             .field("nb_undefined_symbols", &self.nb_undefined_symbols())
     160          360 :             .field("toc_offset", &self.toc_offset())
     161          360 :             .field("nb_toc", &self.nb_toc())
     162          360 :             .field("module_table_offset", &self.module_table_offset())
     163          360 :             .field("nb_module_table", &self.nb_module_table())
     164          360 :             .field(
     165          360 :                 "external_reference_symbol_offset",
     166          360 :                 &self.external_reference_symbol_offset(),
     167          360 :             )
     168          360 :             .field(
     169          360 :                 "nb_external_reference_symbols",
     170          360 :                 &self.nb_external_reference_symbols(),
     171          360 :             )
     172          360 :             .field("indirect_symbol_offset", &self.indirect_symbol_offset())
     173          360 :             .field("nb_indirect_symbols", &self.nb_indirect_symbols())
     174          360 :             .field(
     175          360 :                 "external_relocation_offset",
     176          360 :                 &self.external_relocation_offset(),
     177          360 :             )
     178          360 :             .field("nb_external_relocations", &self.nb_external_relocations())
     179          360 :             .field("local_relocation_offset", &self.local_relocation_offset())
     180          360 :             .field("nb_local_relocations", &self.nb_local_relocations())
     181          360 :             .finish()
     182          360 :     }
     183              : }
     184              : 
     185              : impl FromFFI<ffi::MachO_DynamicSymbolCommand> for DynamicSymbolCommand<'_> {
     186          360 :     fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_DynamicSymbolCommand>) -> Self {
     187          360 :         Self {
     188          360 :             ptr: cmd,
     189          360 :             _owner: PhantomData,
     190          360 :         }
     191          360 :     }
     192              : }
     193              : 
     194              : impl Command for DynamicSymbolCommand<'_> {
     195         1440 :     fn get_base(&self) -> &ffi::MachO_Command {
     196         1440 :         self.ptr.as_ref().unwrap().as_ref()
     197         1440 :     }
     198              : }
     199              : 
     200        50520 : declare_iterator!(
     201        50520 :     IndirectSymbols,
     202        50520 :     Symbol<'a>,
     203        50520 :     ffi::MachO_Symbol,
     204        50520 :     ffi::MachO_DynamicSymbolCommand,
     205        50520 :     ffi::MachO_DynamicSymbolCommand_it_indirect_symbols
     206        50520 : );
        

Generated by: LCOV version 2.1-1