Line data Source code
1 : use super::Command;
2 : use crate::common::FromFFI;
3 : use crate::macho::binding_info::Dyld;
4 : use crate::macho::{BindingInfo, ExportInfo};
5 : use std::marker::PhantomData;
6 :
7 : use crate::{declare_iterator, declare_iterator_conv, to_slice};
8 :
9 : use lief_ffi as ffi;
10 :
11 : /// Structure that represents the `LC_DYLD_INFO` and `LC_DYLD_INFO_ONLY` commands
12 : pub struct DyldInfo<'a> {
13 : ptr: cxx::UniquePtr<ffi::MachO_DyldInfo>,
14 : _owner: PhantomData<&'a ffi::MachO_Binary>,
15 : }
16 :
17 : impl std::fmt::Debug for DyldInfo<'_> {
18 260 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 260 : let base = self as &dyn Command;
20 260 : f.debug_struct("DyldInfo")
21 260 : .field("base", &base)
22 260 : .field("rebase_opcodes", &self.rebase_opcodes().is_empty())
23 260 : .field("bind_opcodes", &self.bind_opcodes().is_empty())
24 260 : .field("weak_bind_opcodes", &self.weak_bind_opcodes().is_empty())
25 260 : .field("lazy_bind_opcodes", &self.lazy_bind_opcodes().is_empty())
26 260 : .field("export_trie", &self.export_trie().is_empty())
27 260 : .finish()
28 260 : }
29 : }
30 :
31 : impl FromFFI<ffi::MachO_DyldInfo> for DyldInfo<'_> {
32 260 : fn from_ffi(cmd: cxx::UniquePtr<ffi::MachO_DyldInfo>) -> Self {
33 260 : Self {
34 260 : ptr: cmd,
35 260 : _owner: PhantomData,
36 260 : }
37 260 : }
38 : }
39 :
40 : impl<'a> DyldInfo<'a> {
41 : /// Return Rebase's opcodes as a slice of bytes
42 390 : pub fn rebase_opcodes(&self) -> &[u8] {
43 390 : to_slice!(self.ptr.rebase_opcodes());
44 390 : }
45 :
46 : /// Return **regular** binding's opcodes as a slice of bytes
47 390 : pub fn bind_opcodes(&self) -> &[u8] {
48 390 : to_slice!(self.ptr.bind_opcodes());
49 390 : }
50 :
51 : /// Return **weak** binding's opcodes as a slice of bytes
52 390 : pub fn weak_bind_opcodes(&self) -> &[u8] {
53 390 : to_slice!(self.ptr.weak_bind_opcodes());
54 390 : }
55 :
56 : /// Return **lazy** binding's opcodes as a slice of bytes
57 390 : pub fn lazy_bind_opcodes(&self) -> &[u8] {
58 390 : to_slice!(self.ptr.lazy_bind_opcodes());
59 390 : }
60 :
61 : /// Return the raw export trie as a slice of bytes
62 390 : pub fn export_trie(&self) -> &[u8] {
63 390 : to_slice!(self.ptr.export_trie());
64 390 : }
65 :
66 : /// Return an iterator over the [`crate::macho::BindingInfo::Dyld`] associated with this
67 : /// command
68 130 : pub fn bindings(&self) -> BindingInfos<'_> {
69 130 : BindingInfos::new(self.ptr.bindings())
70 130 : }
71 :
72 : /// Return an iterator over the [`crate::macho::ExportInfo`] associated with this command
73 130 : pub fn exports(&self) -> ExportInfos<'_> {
74 130 : ExportInfos::new(self.ptr.exports())
75 130 : }
76 : }
77 :
78 : impl Command for DyldInfo<'_> {
79 1040 : fn get_base(&self) -> &ffi::MachO_Command {
80 1040 : self.ptr.as_ref().unwrap().as_ref()
81 1040 : }
82 : }
83 :
84 : declare_iterator_conv!(
85 : BindingInfos,
86 : BindingInfo<'a>,
87 : ffi::MachO_BindingInfo,
88 : ffi::MachO_DyldInfo,
89 : ffi::MachO_DyldInfo_it_bindings,
90 88478 : |n| BindingInfo::Dyld(Dyld::from_ffi(n))
91 : );
92 103142 : declare_iterator!(
93 103142 : ExportInfos,
94 103142 : ExportInfo<'a>,
95 103142 : ffi::MachO_ExportInfo,
96 103142 : ffi::MachO_DyldInfo,
97 103142 : ffi::MachO_DyldInfo_it_exports
98 103142 : );
|