Line data Source code
1 : use super::Command;
2 : use lief_ffi as ffi;
3 :
4 : use crate::to_slice;
5 : use crate::macho::binding_info::{Chained, CHAINED_FORMAT};
6 : use crate::{common::FromFFI, declare_iterator};
7 : use std::marker::PhantomData;
8 :
9 : /// Structure that represents the `LC_DYLD_CHAINED_FIXUPS` command
10 : ///
11 : /// This command aims at providing rebase and binding information like
12 : /// the DyldInfo's bytecode. Compared to the DyldInfo bytecode, these chained
13 : /// fixups are taking less space.
14 : pub struct DyldChainedFixups<'a> {
15 : ptr: cxx::UniquePtr<ffi::MachO_DyldChainedFixups>,
16 : _owner: PhantomData<&'a ffi::MachO_Binary>,
17 : }
18 :
19 : impl std::fmt::Debug for DyldChainedFixups<'_> {
20 32 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 32 : let base = self as &dyn Command;
22 32 : f.debug_struct("DyldChainedFixups")
23 32 : .field("base", &base)
24 32 : .field("data_offset", &self.data_offset())
25 32 : .field("data_size", &self.data_size())
26 32 : .field("fixups_version", &self.fixups_version())
27 32 : .field("starts_offset", &self.starts_offset())
28 32 : .field("imports_offset", &self.imports_offset())
29 32 : .field("symbols_offset", &self.symbols_offset())
30 32 : .field("imports_count", &self.imports_count())
31 32 : .field("symbols_format", &self.symbols_format())
32 32 : .field("imports_format", &self.imports_format())
33 32 : .finish()
34 32 : }
35 : }
36 :
37 : impl DyldChainedFixups<'_> {
38 : /// Offset of the `LC_DYLD_CHAINED_FIXUPS` chained payload.
39 : /// This offset should point in the `__LINKEDIT` segment
40 32 : pub fn data_offset(&self) -> u32 {
41 32 : self.ptr.data_offset()
42 32 : }
43 :
44 : /// Size of the `LC_DYLD_CHAINED_FIXUPS` payload.
45 32 : pub fn data_size(&self) -> u32 {
46 32 : self.ptr.data_size()
47 32 : }
48 :
49 : /// Chained fixups version. The loader (dyld v852.2) checks
50 : /// that this value is set to 0
51 32 : pub fn fixups_version(&self) -> u32 {
52 32 : self.ptr.fixups_version()
53 32 : }
54 :
55 : /// offset of `dyld_chained_starts_in_image` in chain_data
56 32 : pub fn starts_offset(&self) -> u32 {
57 32 : self.ptr.starts_offset()
58 32 : }
59 :
60 : /// Offset of imports table in chain data
61 32 : pub fn imports_offset(&self) -> u32 {
62 32 : self.ptr.imports_offset()
63 32 : }
64 :
65 : /// Offset of symbol strings in chain data
66 32 : pub fn symbols_offset(&self) -> u32 {
67 32 : self.ptr.symbols_offset()
68 32 : }
69 :
70 : /// Number of imported symbol names
71 32 : pub fn imports_count(&self) -> u32 {
72 32 : self.ptr.imports_count()
73 32 : }
74 :
75 : /// The compression algorithm (if any) used to store the symbols
76 : /// 0 means uncompressed while 1 means zlib compressed.
77 : ///
78 : /// As far of the version v852.2 of dyld loader, it only supports
79 : /// **uncompressed** format
80 32 : pub fn symbols_format(&self) -> u32 {
81 32 : self.ptr.symbols_format()
82 32 : }
83 :
84 : /// The format of the imports
85 32 : pub fn imports_format(&self) -> CHAINED_FORMAT {
86 32 : CHAINED_FORMAT::from_value(self.ptr.imports_format())
87 32 : }
88 :
89 : /// Iterator over the bindings [`crate::macho::BindingInfo::Chained`]
90 : /// associated with this command
91 16 : pub fn bindings(&self) -> Bindings {
92 16 : Bindings::new(self.ptr.bindings())
93 16 : }
94 :
95 : /// Return the raw content of the command
96 16 : pub fn payload(&self) -> &[u8] {
97 16 : to_slice!(self.ptr.payload());
98 16 : }
99 : }
100 :
101 : impl FromFFI<ffi::MachO_DyldChainedFixups> for DyldChainedFixups<'_> {
102 32 : fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_DyldChainedFixups>) -> Self {
103 32 : Self {
104 32 : ptr: cmd,
105 32 : _owner: PhantomData,
106 32 : }
107 32 : }
108 : }
109 :
110 : impl Command for DyldChainedFixups<'_> {
111 128 : fn get_base(&self) -> &ffi::MachO_Command {
112 128 : self.ptr.as_ref().unwrap().as_ref()
113 128 : }
114 : }
115 :
116 400 : declare_iterator!(
117 400 : Bindings,
118 400 : Chained<'a>,
119 400 : ffi::MachO_ChainedBindingInfo,
120 400 : ffi::MachO_DyldChainedFixups,
121 400 : ffi::MachO_DyldChainedFixups_it_bindings
122 400 : );
|