Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use crate::Error;
4 : use crate::common::{FromFFI, into_optional};
5 : use crate::{declare_iterator, declare_fwd_iterator, to_result};
6 : use super::{SubCache, Dylib, MappingInfo};
7 :
8 : use crate::assembly;
9 :
10 : #[allow(non_camel_case_types)]
11 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
12 : /// This enum wraps the dyld's git tags for which the structure of
13 : /// dyld shared cache evolved
14 : pub enum Version {
15 : /// dyld-95.3 (2007-10-30)
16 : DYLD_95_3,
17 : /// dyld-195.5 (2011-07-13)
18 : DYLD_195_5,
19 : /// dyld-239.3 (2013-10-29)
20 : DYLD_239_3,
21 : /// dyld-360.14 (2015-09-04)
22 : DYLD_360_14,
23 : /// dyld-421.1 (2016-09-22)
24 : DYLD_421_1,
25 : /// dyld-832.7.1 (2020-11-19)
26 : DYLD_832_7_1,
27 : /// dyld-940 (2021-02-09)
28 : DYLD_940,
29 : /// dyld-1042.1 (2022-10-19)
30 : DYLD_1042_1,
31 : /// dyld-1231.3 (2024-09-24)
32 : DYLD_1231_3,
33 : /// This value is used for versions of dyld not publicly released or not yet
34 : /// supported by LIEF
35 : UNRELEASED,
36 : UNKNOWN(u32),
37 : }
38 :
39 : impl From<u32> for Version {
40 0 : fn from(value: u32) -> Self {
41 0 : match value {
42 0 : 0x00000001 => Version::DYLD_95_3,
43 0 : 0x00000002 => Version::DYLD_195_5,
44 0 : 0x00000003 => Version::DYLD_239_3,
45 0 : 0x00000004 => Version::DYLD_360_14,
46 0 : 0x00000005 => Version::DYLD_421_1,
47 0 : 0x00000006 => Version::DYLD_832_7_1,
48 0 : 0x00000007 => Version::DYLD_940,
49 0 : 0x00000008 => Version::DYLD_1042_1,
50 0 : 0x00000009 => Version::DYLD_1231_3,
51 0 : 0x0000000A => Version::UNRELEASED,
52 0 : _ => Version::UNKNOWN(value),
53 :
54 : }
55 0 : }
56 : }
57 : impl From<Version> for u32 {
58 0 : fn from(value: Version) -> u32 {
59 0 : match value {
60 0 : Version::DYLD_95_3 => 0x00000001,
61 0 : Version::DYLD_195_5 => 0x00000002,
62 0 : Version::DYLD_239_3 => 0x00000003,
63 0 : Version::DYLD_360_14 => 0x00000004,
64 0 : Version::DYLD_421_1 => 0x00000005,
65 0 : Version::DYLD_832_7_1 => 0x00000006,
66 0 : Version::DYLD_940 => 0x00000007,
67 0 : Version::DYLD_1042_1 => 0x00000008,
68 0 : Version::DYLD_1231_3 => 0x00000009,
69 0 : Version::UNRELEASED => 0x0000000A,
70 0 : Version::UNKNOWN(_) => 0,
71 :
72 : }
73 0 : }
74 : }
75 :
76 :
77 : #[allow(non_camel_case_types)]
78 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
79 : /// Platforms supported by the dyld shared cache
80 : pub enum Platform {
81 : MACOS,
82 : IOS,
83 : TVOS,
84 : WATCHOS,
85 : BRIDGEOS,
86 : IOSMAC,
87 : IOS_SIMULATOR,
88 : TVOS_SIMULATOR,
89 : WATCHOS_SIMULATOR,
90 : DRIVERKIT,
91 : VISIONOS,
92 : VISIONOS_SIMULATOR,
93 : FIRMWARE,
94 : SEPOS,
95 : ANY,
96 : UNKNOWN(u32),
97 : }
98 :
99 : impl From<u32> for Platform {
100 0 : fn from(value: u32) -> Self {
101 0 : match value {
102 0 : 0x00000001 => Platform::MACOS,
103 0 : 0x00000002 => Platform::IOS,
104 0 : 0x00000003 => Platform::TVOS,
105 0 : 0x00000004 => Platform::WATCHOS,
106 0 : 0x00000005 => Platform::BRIDGEOS,
107 0 : 0x00000006 => Platform::IOSMAC,
108 0 : 0x00000007 => Platform::IOS_SIMULATOR,
109 0 : 0x00000008 => Platform::TVOS_SIMULATOR,
110 0 : 0x00000009 => Platform::WATCHOS_SIMULATOR,
111 0 : 0x0000000a => Platform::DRIVERKIT,
112 0 : 0x0000000b => Platform::VISIONOS,
113 0 : 0x0000000c => Platform::VISIONOS_SIMULATOR,
114 0 : 0x0000000d => Platform::FIRMWARE,
115 0 : 0x0000000e => Platform::SEPOS,
116 0 : 0xffffffff => Platform::ANY,
117 0 : _ => Platform::UNKNOWN(value),
118 :
119 : }
120 0 : }
121 : }
122 : impl From<Platform> for u32 {
123 0 : fn from(value: Platform) -> u32 {
124 0 : match value {
125 0 : Platform::MACOS => 0x00000001,
126 0 : Platform::IOS => 0x00000002,
127 0 : Platform::TVOS => 0x00000003,
128 0 : Platform::WATCHOS => 0x00000004,
129 0 : Platform::BRIDGEOS => 0x00000005,
130 0 : Platform::IOSMAC => 0x00000006,
131 0 : Platform::IOS_SIMULATOR => 0x00000007,
132 0 : Platform::TVOS_SIMULATOR => 0x00000008,
133 0 : Platform::WATCHOS_SIMULATOR => 0x00000009,
134 0 : Platform::DRIVERKIT => 0x0000000a,
135 0 : Platform::VISIONOS => 0x0000000b,
136 0 : Platform::VISIONOS_SIMULATOR => 0x0000000c,
137 0 : Platform::FIRMWARE => 0x0000000d,
138 0 : Platform::SEPOS => 0x0000000e,
139 0 : Platform::ANY => 0xffffffff,
140 0 : Platform::UNKNOWN(_) => 0,
141 :
142 : }
143 0 : }
144 : }
145 :
146 : #[allow(non_camel_case_types)]
147 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
148 : /// Architecture supported by the dyld shared cache
149 : pub enum Arch {
150 : I386,
151 : X86_64,
152 : X86_64H,
153 : ARMV5,
154 : ARMV6,
155 : ARMV7,
156 : ARM64,
157 : ARM64E,
158 : UNKNOWN(u32),
159 : }
160 :
161 : impl From<u32> for Arch {
162 0 : fn from(value: u32) -> Self {
163 0 : match value {
164 0 : 0x00000001 => Arch::I386,
165 0 : 0x00000002 => Arch::X86_64,
166 0 : 0x00000003 => Arch::X86_64H,
167 0 : 0x00000004 => Arch::ARMV5,
168 0 : 0x00000005 => Arch::ARMV6,
169 0 : 0x00000006 => Arch::ARMV7,
170 0 : 0x00000007 => Arch::ARM64,
171 0 : 0x00000008 => Arch::ARM64E,
172 0 : _ => Arch::UNKNOWN(value),
173 :
174 : }
175 0 : }
176 : }
177 : impl From<Arch> for u32 {
178 0 : fn from(value: Arch) -> u32 {
179 0 : match value {
180 0 : Arch::I386 => 0x00000001,
181 0 : Arch::X86_64 => 0x00000002,
182 0 : Arch::X86_64H => 0x00000003,
183 0 : Arch::ARMV5 => 0x00000004,
184 0 : Arch::ARMV6 => 0x00000005,
185 0 : Arch::ARMV7 => 0x00000006,
186 0 : Arch::ARM64 => 0x00000007,
187 0 : Arch::ARM64E => 0x00000008,
188 0 : Arch::UNKNOWN(_) => 0,
189 :
190 : }
191 0 : }
192 : }
193 :
194 : /// This struct interfaces a dyld shared cache file.
195 : pub struct DyldSharedCache {
196 : ptr: cxx::UniquePtr<ffi::dsc_DyldSharedCache>,
197 : }
198 :
199 : impl FromFFI<ffi::dsc_DyldSharedCache> for DyldSharedCache {
200 0 : fn from_ffi(info: cxx::UniquePtr<ffi::dsc_DyldSharedCache>) -> Self {
201 0 : Self {
202 0 : ptr: info,
203 0 : }
204 0 : }
205 : }
206 :
207 : impl DyldSharedCache {
208 : /// Filename of the dyld shared file associated with this object.
209 : ///
210 : /// For instance: `dyld_shared_cache_arm64e, dyld_shared_cache_arm64e.62.dyldlinkedit`
211 0 : pub fn filename(&self) -> String {
212 0 : self.ptr.filename().to_string()
213 0 : }
214 :
215 : /// Full path to the original dyld shared cache file associated with object
216 : /// (e.g. `/home/lief/downloads/visionos/dyld_shared_cache_arm64e.42`)
217 0 : pub fn filepath(&self) -> String {
218 0 : self.ptr.filepath().to_string()
219 0 : }
220 :
221 : /// Based address of this cache
222 0 : pub fn load_address(&self) -> u64 {
223 0 : self.ptr.load_address()
224 0 : }
225 :
226 : /// Version of dyld used by this cache
227 0 : pub fn version(&self) -> Version {
228 0 : Version::from(self.ptr.version())
229 0 : }
230 :
231 : /// Name of the architecture targeted by this cache (`x86_64h`)
232 0 : pub fn arch_name(&self) -> String {
233 0 : self.ptr.arch_name().to_string()
234 0 : }
235 :
236 : /// Platform targeted by this cache (e.g. vision-os)
237 0 : pub fn platform(&self) -> Platform {
238 0 : Platform::from(self.ptr.platform())
239 0 : }
240 :
241 : /// Architecture targeted by this cache
242 0 : pub fn arch(&self) -> Arch {
243 0 : Arch::from(self.ptr.arch())
244 0 : }
245 :
246 : /// Find the [`Dylib`] that encompasses the given virtual address.
247 0 : pub fn find_lib_from_va(&self, va: u64) -> Option<Dylib> {
248 0 : into_optional(self.ptr.find_lib_from_va(va))
249 0 : }
250 :
251 : /// Find the [`Dylib`] whose [`Dylib::path`] matches the provided path.
252 0 : pub fn find_lib_from_path(&self, path: &str) -> Option<Dylib> {
253 0 : into_optional(self.ptr.find_lib_from_path(path))
254 0 : }
255 :
256 : /// Find the [`Dylib`] whose filename of [`Dylib::path`] matches the provided name.
257 : ///
258 : /// If multiple libraries have the same name (but with a different path),
259 : /// the **first one** matching the provided name is returned.
260 0 : pub fn find_lib_from_name(&self, name: &str) -> Option<Dylib> {
261 0 : into_optional(self.ptr.find_lib_from_name(name))
262 0 : }
263 :
264 : /// True if the subcaches are associated with this cache
265 0 : pub fn has_subcaches(&self) -> bool {
266 0 : self.ptr.has_subcaches()
267 0 : }
268 :
269 : /// Return an iterator over the different [`Dylib`] libraries embedded
270 : /// in this dyld shared cache
271 0 : pub fn libraries(&self) -> Dylibs {
272 0 : Dylibs::new(self.ptr.libraries())
273 0 : }
274 :
275 : /// Return an iterator over the different [`MappingInfo`] associated
276 : /// with this dyld shared cache
277 0 : pub fn mapping_info(&self) -> MappingInfoIt {
278 0 : MappingInfoIt::new(self.ptr.mapping_info())
279 0 : }
280 :
281 : /// Return an interator over the subcaches associated with this (main) dyld shared
282 : /// cache.
283 0 : pub fn subcaches(&self) -> SubCacheIt {
284 0 : SubCacheIt::new(self.ptr.subcaches())
285 0 : }
286 :
287 : /// Disassemble instructions at the provided virtual address.
288 : ///
289 : /// This function returns an iterator over [`assembly::Instructions`].
290 0 : pub fn disassemble(&self, address: u64) -> Instructions {
291 0 : Instructions::new(self.ptr.disassemble(address))
292 0 : }
293 :
294 : /// Return the content at the specified virtual address
295 0 : pub fn get_content_from_va(&self, address: u64, size: u64) -> Vec<u8> {
296 0 : Vec::from(self.ptr.get_content_from_va(address, size).as_slice())
297 0 : }
298 :
299 : /// Find the sub-DyldSharedCache that wraps the given virtual address
300 0 : pub fn cache_for_address(&self, address: u64) -> Option<DyldSharedCache> {
301 0 : into_optional(self.ptr.cache_for_address(address))
302 0 : }
303 :
304 : /// Return the principal dyld shared cache in the case of multiple subcaches
305 0 : pub fn main_cache(&self) -> Option<DyldSharedCache> {
306 0 : into_optional(self.ptr.main_cache())
307 0 : }
308 :
309 : /// Try to find the [`DyldSharedCache`] associated with the filename given
310 : /// in the first parameter.
311 0 : pub fn find_subcache(&self, filename: &str) -> Option<DyldSharedCache> {
312 0 : into_optional(self.ptr.find_subcache(filename))
313 0 : }
314 :
315 : /// Convert the given virtual address into an offset.
316 : /// <div class="warning">
317 : /// If the shared cache contains multiple subcaches,
318 : /// this function needs to be called on the targeted subcache.
319 : /// </div>
320 : ///
321 : /// See: [`DyldSharedCache::cache_for_address`]
322 0 : pub fn va_to_offset(&self, address: u64) -> Result<u64, Error> {
323 0 : to_result!(ffi::dsc_DyldSharedCache::va_to_offset, &self, address);
324 0 : }
325 :
326 : /// When enabled, this function allows to record and to keep in *cache*,
327 : /// dyld shared cache information that are costly to access.
328 : ///
329 : /// For instance, GOT symbols, rebases information, stub symbols, ...
330 : ///
331 : /// It is **highly** recommended to enable this function when processing
332 : /// a dyld shared cache several times or when extracting a large number of
333 : /// [`Dylib`] with enhanced extraction options (e.g. [`crate::dsc::dylib::ExtractOpt::fix_memory`])
334 : ///
335 : /// One can enable caching by calling this function:
336 : ///
337 : /// ```rust
338 : /// let dyld_cache = lief::dsc::load_from_path("macos-15.0.1/", "");
339 : /// dyld_cache.enable_caching("/home/user/.cache/lief-dsc");
340 : /// ```
341 : ///
342 : /// One can also enable this cache optimization **globally** using the
343 : /// function: [`crate::dsc::enable_cache`] or by setting the environment variable
344 : /// `DYLDSC_ENABLE_CACHE` to 1.
345 0 : pub fn enable_caching(&self, target_cache_dir: &str) {
346 0 : self.ptr.enable_caching(target_cache_dir)
347 0 : }
348 :
349 : /// Flush internal information into the on-disk cache (see: enable_caching)
350 0 : pub fn flush_cache(&self) {
351 0 : self.ptr.flush_cache()
352 0 : }
353 : }
354 :
355 0 : declare_iterator!(
356 0 : Dylibs,
357 0 : Dylib<'a>,
358 0 : ffi::dsc_Dylib,
359 0 : ffi::dsc_DyldSharedCache,
360 0 : ffi::dsc_DyldSharedCache_it_libraries
361 0 : );
362 :
363 0 : declare_iterator!(
364 0 : MappingInfoIt,
365 0 : MappingInfo<'a>,
366 0 : ffi::dsc_MappingInfo,
367 0 : ffi::dsc_DyldSharedCache,
368 0 : ffi::dsc_DyldSharedCache_it_mapping_info
369 0 : );
370 :
371 0 : declare_iterator!(
372 0 : SubCacheIt,
373 0 : SubCache<'a>,
374 0 : ffi::dsc_SubCache,
375 0 : ffi::dsc_DyldSharedCache,
376 0 : ffi::dsc_DyldSharedCache_it_subcaches
377 0 : );
378 :
379 0 : declare_fwd_iterator!(
380 0 : Instructions,
381 0 : assembly::Instructions,
382 0 : ffi::asm_Instruction,
383 0 : ffi::dsc_DyldSharedCache,
384 0 : ffi::dsc_DyldSharedCache_it_instructions
385 0 : );
|