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