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 40 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 40 : let base = self as &dyn Command;
22 40 : f.debug_struct("DyldChainedFixups")
23 40 : .field("base", &base)
24 40 : .field("data_offset", &self.data_offset())
25 40 : .field("data_size", &self.data_size())
26 40 : .field("fixups_version", &self.fixups_version())
27 40 : .field("starts_offset", &self.starts_offset())
28 40 : .field("imports_offset", &self.imports_offset())
29 40 : .field("symbols_offset", &self.symbols_offset())
30 40 : .field("imports_count", &self.imports_count())
31 40 : .field("symbols_format", &self.symbols_format())
32 40 : .field("imports_format", &self.imports_format())
33 40 : .finish()
34 40 : }
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 40 : pub fn data_offset(&self) -> u32 {
41 40 : self.ptr.data_offset()
42 40 : }
43 :
44 : /// Size of the `LC_DYLD_CHAINED_FIXUPS` payload.
45 40 : pub fn data_size(&self) -> u32 {
46 40 : self.ptr.data_size()
47 40 : }
48 :
49 : /// Chained fixups version. The loader (dyld v852.2) checks
50 : /// that this value is set to 0
51 40 : pub fn fixups_version(&self) -> u32 {
52 40 : self.ptr.fixups_version()
53 40 : }
54 :
55 : /// offset of `dyld_chained_starts_in_image` in chain_data
56 40 : pub fn starts_offset(&self) -> u32 {
57 40 : self.ptr.starts_offset()
58 40 : }
59 :
60 : /// Offset of imports table in chain data
61 40 : pub fn imports_offset(&self) -> u32 {
62 40 : self.ptr.imports_offset()
63 40 : }
64 :
65 : /// Offset of symbol strings in chain data
66 40 : pub fn symbols_offset(&self) -> u32 {
67 40 : self.ptr.symbols_offset()
68 40 : }
69 :
70 : /// Number of imported symbol names
71 40 : pub fn imports_count(&self) -> u32 {
72 40 : self.ptr.imports_count()
73 40 : }
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 40 : pub fn symbols_format(&self) -> u32 {
81 40 : self.ptr.symbols_format()
82 40 : }
83 :
84 : /// The format of the imports
85 40 : pub fn imports_format(&self) -> CHAINED_FORMAT {
86 40 : CHAINED_FORMAT::from_value(self.ptr.imports_format())
87 40 : }
88 :
89 : /// Iterator over the bindings [`crate::macho::BindingInfo::Chained`]
90 : /// associated with this command
91 20 : pub fn bindings(&self) -> Bindings {
92 20 : Bindings::new(self.ptr.bindings())
93 20 : }
94 :
95 : /// Return the raw content of the command
96 20 : pub fn payload(&self) -> &[u8] {
97 20 : to_slice!(self.ptr.payload());
98 20 : }
99 : }
100 :
101 : impl FromFFI<ffi::MachO_DyldChainedFixups> for DyldChainedFixups<'_> {
102 40 : fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_DyldChainedFixups>) -> Self {
103 40 : Self {
104 40 : ptr: cmd,
105 40 : _owner: PhantomData,
106 40 : }
107 40 : }
108 : }
109 :
110 : impl Command for DyldChainedFixups<'_> {
111 160 : fn get_base(&self) -> &ffi::MachO_Command {
112 160 : self.ptr.as_ref().unwrap().as_ref()
113 160 : }
114 : }
115 :
116 500 : declare_iterator!(
117 500 : Bindings,
118 500 : Chained<'a>,
119 500 : ffi::MachO_ChainedBindingInfo,
120 500 : ffi::MachO_DyldChainedFixups,
121 500 : ffi::MachO_DyldChainedFixups_it_bindings
122 500 : );
|