LCOV - code coverage report
Current view: top level - src/macho - header.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 43.9 % 171 75
Test Date: 2026-04-12:00:00:00 Functions: 57.6 % 33 19

            Line data    Source code
       1              : use lief_ffi as ffi;
       2              : 
       3              : use bitflags::bitflags;
       4              : 
       5              : use crate::common::FromFFI;
       6              : use std::{fmt, marker::PhantomData};
       7              : 
       8              : #[allow(non_camel_case_types)]
       9          416 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
      10              : pub enum FileType {
      11              :     OBJECT,
      12              :     EXECUTE,
      13              :     FVMLIB,
      14              :     CORE,
      15              :     PRELOAD,
      16              :     DYLIB,
      17              :     DYLINKER,
      18              :     BUNDLE,
      19              :     DYLIB_STUB,
      20              :     DSYM,
      21              :     KEXT_BUNDLE,
      22              :     FILESET,
      23              :     GPU_EXECUTE,
      24              :     GPU_DYLIB,
      25              :     UNKNOWN(u32),
      26              : }
      27              : 
      28              : impl From<u32> for FileType {
      29          416 :     fn from(value: u32) -> Self {
      30          416 :         match value {
      31           13 :             0x00000001 => FileType::OBJECT,
      32           52 :             0x00000002 => FileType::EXECUTE,
      33            0 :             0x00000003 => FileType::FVMLIB,
      34            0 :             0x00000004 => FileType::CORE,
      35            0 :             0x00000005 => FileType::PRELOAD,
      36          143 :             0x00000006 => FileType::DYLIB,
      37            0 :             0x00000007 => FileType::DYLINKER,
      38            0 :             0x00000008 => FileType::BUNDLE,
      39            0 :             0x00000009 => FileType::DYLIB_STUB,
      40            0 :             0x0000000a => FileType::DSYM,
      41            0 :             0x0000000b => FileType::KEXT_BUNDLE,
      42           13 :             0x0000000c => FileType::FILESET,
      43          195 :             0x0000000d => FileType::GPU_EXECUTE,
      44            0 :             0x0000000e => FileType::GPU_DYLIB,
      45            0 :             _ => FileType::UNKNOWN(value),
      46              :         }
      47          416 :     }
      48              : }
      49              : impl From<FileType> for u32 {
      50            0 :     fn from(value: FileType) -> u32 {
      51            0 :         match value {
      52            0 :             FileType::OBJECT => 0x00000001,
      53            0 :             FileType::EXECUTE => 0x00000002,
      54            0 :             FileType::FVMLIB => 0x00000003,
      55            0 :             FileType::CORE => 0x00000004,
      56            0 :             FileType::PRELOAD => 0x00000005,
      57            0 :             FileType::DYLIB => 0x00000006,
      58            0 :             FileType::DYLINKER => 0x00000007,
      59            0 :             FileType::BUNDLE => 0x00000008,
      60            0 :             FileType::DYLIB_STUB => 0x00000009,
      61            0 :             FileType::DSYM => 0x0000000a,
      62            0 :             FileType::KEXT_BUNDLE => 0x0000000b,
      63            0 :             FileType::FILESET => 0x0000000c,
      64            0 :             FileType::GPU_EXECUTE => 0x0000000d,
      65            0 :             FileType::GPU_DYLIB => 0x0000000e,
      66            0 :             FileType::UNKNOWN(value) => value,
      67              :         }
      68            0 :     }
      69              : }
      70              : 
      71              : #[allow(non_camel_case_types)]
      72          416 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
      73              : pub enum CpuType {
      74              :     ANY,
      75              :     X86,
      76              :     X86_64,
      77              :     MIPS,
      78              :     MC98000,
      79              :     HPPA,
      80              :     ARM,
      81              :     ARM64,
      82              :     MC88000,
      83              :     SPARC,
      84              :     I860,
      85              :     ALPHA,
      86              :     POWERPC,
      87              :     POWERPC64,
      88              :     APPLE_GPU,
      89              :     AMD_GPU,
      90              :     INTEL_GPU,
      91              :     AIR64,
      92              :     UNKNOWN(i32),
      93              : }
      94              : 
      95              : impl From<i32> for CpuType {
      96          416 :     fn from(value: i32) -> Self {
      97          416 :         match value {
      98            0 :             -1 => CpuType::ANY,
      99           26 :             0x00000007 => CpuType::X86,
     100           52 :             0x01000007 => CpuType::X86_64,
     101            0 :             0x00000008 => CpuType::MIPS,
     102            0 :             0x0000000a => CpuType::MC98000,
     103            0 :             0x0000000b => CpuType::HPPA,
     104           26 :             0x0000000c => CpuType::ARM,
     105          117 :             0x0100000c => CpuType::ARM64,
     106            0 :             0x0000000d => CpuType::MC88000,
     107            0 :             0x0000000e => CpuType::SPARC,
     108            0 :             0x0000000f => CpuType::I860,
     109            0 :             0x00000010 => CpuType::ALPHA,
     110            0 :             0x00000012 => CpuType::POWERPC,
     111            0 :             0x01000012 => CpuType::POWERPC64,
     112          195 :             0x01000013 => CpuType::APPLE_GPU,
     113            0 :             0x01000014 => CpuType::AMD_GPU,
     114            0 :             0x01000015 => CpuType::INTEL_GPU,
     115            0 :             0x01000017 => CpuType::AIR64,
     116            0 :             _ => CpuType::UNKNOWN(value),
     117              :         }
     118          416 :     }
     119              : }
     120              : impl From<CpuType> for i32 {
     121          338 :     fn from(value: CpuType) -> i32 {
     122          338 :         match value {
     123            0 :             CpuType::ANY => -1,
     124            0 :             CpuType::X86 => 0x00000007,
     125            0 :             CpuType::X86_64 => 0x01000007,
     126            0 :             CpuType::MIPS => 0x00000008,
     127            0 :             CpuType::MC98000 => 0x0000000a,
     128          169 :             CpuType::HPPA => 0x0000000b,
     129            0 :             CpuType::ARM => 0x0000000c,
     130          169 :             CpuType::ARM64 => 0x0100000c,
     131            0 :             CpuType::MC88000 => 0x0000000d,
     132            0 :             CpuType::SPARC => 0x0000000e,
     133            0 :             CpuType::I860 => 0x0000000f,
     134            0 :             CpuType::ALPHA => 0x00000010,
     135            0 :             CpuType::POWERPC => 0x00000012,
     136            0 :             CpuType::POWERPC64 => 0x01000012,
     137            0 :             CpuType::APPLE_GPU => 0x01000013,
     138            0 :             CpuType::AMD_GPU => 0x01000014,
     139            0 :             CpuType::INTEL_GPU => 0x01000015,
     140            0 :             CpuType::AIR64 => 0x01000017,
     141            0 :             CpuType::UNKNOWN(_) => -1,
     142              :         }
     143          338 :     }
     144              : }
     145              : 
     146            0 : bitflags! {
     147          416 :     #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
     148            0 :     pub struct Flags: u32 {
     149            0 :         const NOUNDEFS = 0x1;
     150            0 :         const INCRLINK = 0x2;
     151            0 :         const DYLDLINK = 0x4;
     152            0 :         const BINDATLOAD = 0x8;
     153            0 :         const PREBOUND = 0x10;
     154            0 :         const SPLIT_SEGS = 0x20;
     155            0 :         const LAZY_INIT = 0x40;
     156            0 :         const TWOLEVEL = 0x80;
     157            0 :         const FORCE_FLAT = 0x100;
     158            0 :         const NOMULTIDEFS = 0x200;
     159            0 :         const NOFIXPREBINDING = 0x400;
     160            0 :         const PREBINDABLE = 0x800;
     161            0 :         const ALLMODSBOUND = 0x1000;
     162            0 :         const SUBSECTIONS_VIA_SYMBOLS = 0x2000;
     163            0 :         const CANONICAL = 0x4000;
     164            0 :         const WEAK_DEFINES = 0x8000;
     165            0 :         const BINDS_TO_WEAK = 0x10000;
     166            0 :         const ALLOW_STACK_EXECUTION = 0x20000;
     167            0 :         const ROOT_SAFE = 0x40000;
     168            0 :         const SETUID_SAFE = 0x80000;
     169            0 :         const NO_REEXPORTED_DYLIBS = 0x100000;
     170            0 :         const PIE = 0x200000;
     171            0 :         const DEAD_STRIPPABLE_DYLIB = 0x400000;
     172            0 :         const HAS_TLV_DESCRIPTORS = 0x800000;
     173            0 :         const NO_HEAP_EXECUTION = 0x1000000;
     174            0 :         const APP_EXTENSION_SAFE = 0x2000000;
     175            0 :         const NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000;
     176            0 :         const SIM_SUPPORT = 0x08000000;
     177            0 :         const IMPLICIT_PAGEZERO = 0x10000000;
     178            0 :         const DYLIB_IN_CACHE = 0x80000000;
     179            0 :     }
     180            0 : }
     181              : 
     182              : impl From<u32> for Flags {
     183          416 :     fn from(value: u32) -> Self {
     184          416 :         Flags::from_bits_truncate(value)
     185          416 :     }
     186              : }
     187              : impl From<Flags> for u32 {
     188            0 :     fn from(value: Flags) -> Self {
     189            0 :         value.bits()
     190            0 :     }
     191              : }
     192              : 
     193              : /// Structure that represents the main Mach-O header (at the beginning of the file)
     194              : pub struct Header<'a> {
     195              :     ptr: cxx::UniquePtr<ffi::MachO_Header>,
     196              :     _owner: PhantomData<&'a ffi::MachO_Binary>,
     197              : }
     198              : 
     199              : impl FromFFI<ffi::MachO_Header> for Header<'_> {
     200         1248 :     fn from_ffi(hdr: cxx::UniquePtr<ffi::MachO_Header>) -> Self {
     201         1248 :         Self {
     202         1248 :             ptr: hdr,
     203         1248 :             _owner: PhantomData,
     204         1248 :         }
     205         1248 :     }
     206              : }
     207              : 
     208              : impl Header<'_> {
     209              :     /// The Mach-O magic bytes. These bytes determine whether it is
     210              :     /// a 32 bits Mach-O, a 64 bits Mach-O files etc.
     211          416 :     pub fn magic(&self) -> u32 {
     212          416 :         self.ptr.magic()
     213          416 :     }
     214              : 
     215              :     /// The CPU architecture targeted by this binary
     216          416 :     pub fn cpu_type(&self) -> CpuType {
     217          416 :         CpuType::from(self.ptr.cpu_type())
     218          416 :     }
     219              : 
     220              :     /// Return the CPU subtype supported by the Mach-O binary.
     221              :     /// For ARM architectures, this value could represent the minimum version
     222              :     /// for which the Mach-O binary has been compiled.
     223          416 :     pub fn cpu_subtype(&self) -> u32 {
     224          416 :         self.ptr.cpu_subtype()
     225          416 :     }
     226              : 
     227              :     /// Return the type of the Mach-O file (executable, object, shared library, ...)
     228          416 :     pub fn file_type(&self) -> FileType {
     229          416 :         FileType::from(self.ptr.file_type())
     230          416 :     }
     231              : 
     232              :     /// Number of [`crate::macho::Commands`] present in the Mach-O binary
     233          416 :     pub fn nb_cmds(&self) -> u32 {
     234          416 :         self.ptr.nb_cmds()
     235          416 :     }
     236              : 
     237              :     /// The raw size of **all** the load commands (`LC_xxx`)
     238          416 :     pub fn sizeof_cmds(&self) -> u32 {
     239          416 :         self.ptr.sizeof_cmds()
     240          416 :     }
     241              : 
     242              :     /// Header flags
     243          416 :     pub fn flags(&self) -> Flags {
     244          416 :         Flags::from(self.ptr.flags())
     245          416 :     }
     246              : 
     247              :     /// According to the official specs, a reserved value
     248          416 :     pub fn reserved(&self) -> u32 {
     249          416 :         self.ptr.reserved()
     250          416 :     }
     251              : 
     252              :     /// True if the binary is 32-bit
     253          416 :     pub fn is_32bit(&self) -> bool {
     254          416 :         self.ptr.is_32bit()
     255          416 :     }
     256              : 
     257              :     /// True if the binary is 64-bit
     258          416 :     pub fn is_64bit(&self) -> bool {
     259          416 :         self.ptr.is_64bit()
     260          416 :     }
     261              : }
     262              : 
     263              : impl fmt::Debug for Header<'_> {
     264          416 :     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     265          416 :         f.debug_struct("Header")
     266          416 :             .field("magic", &self.magic())
     267          416 :             .field("cpu_type", &self.cpu_type())
     268          416 :             .field("cpu_subtype", &self.cpu_subtype())
     269          416 :             .field("file_type", &self.file_type())
     270          416 :             .field("nb_cmds", &self.nb_cmds())
     271          416 :             .field("sizeof_cmds", &self.sizeof_cmds())
     272          416 :             .field("flags", &self.flags())
     273          416 :             .field("reserved", &self.reserved())
     274          416 :             .finish()
     275          416 :     }
     276              : }
        

Generated by: LCOV version 2.1-1