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

Generated by: LCOV version 2.1-1