Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use bitflags::bitflags;
4 : use std::{fmt, marker::PhantomData};
5 :
6 : use crate::common::{into_optional, FromFFI};
7 :
8 : use super::{commands::Dylib, Symbol};
9 :
10 : /// This structure represents an export (info) in a Mach-O binary
11 : pub struct ExportInfo<'a> {
12 : ptr: cxx::UniquePtr<ffi::MachO_ExportInfo>,
13 : _owner: PhantomData<&'a ()>,
14 : }
15 :
16 : #[allow(non_camel_case_types)]
17 190560 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
18 : pub enum Kind {
19 : REGULAR,
20 : THREAD_LOCAL,
21 : ABSOLUTE,
22 : UNKNOWN(u64),
23 : }
24 :
25 : impl From<u64> for Kind {
26 190560 : fn from(value: u64) -> Self {
27 190560 : match value {
28 190560 : 0x00000000 => Kind::REGULAR,
29 0 : 0x00000001 => Kind::THREAD_LOCAL,
30 0 : 0x00000002 => Kind::ABSOLUTE,
31 0 : _ => Kind::UNKNOWN(value),
32 : }
33 190560 : }
34 : }
35 :
36 0 : bitflags! {
37 190560 : #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
38 0 : pub struct Flags: u64 {
39 0 : const WEAK_DEFINITION = 0x4;
40 0 : const REEXPORT = 0x8;
41 0 : const STUB_AND_RESOLVER = 0x10;
42 0 : }
43 0 : }
44 :
45 :
46 : impl From<u64> for Flags {
47 190560 : fn from(value: u64) -> Self {
48 190560 : Flags::from_bits_truncate(value)
49 190560 : }
50 : }
51 : impl From<Flags> for u64 {
52 0 : fn from(value: Flags) -> Self {
53 0 : value.bits()
54 0 : }
55 : }
56 : impl std::fmt::Display for Flags {
57 0 : fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
58 0 : bitflags::parser::to_writer(self, f)
59 0 : }
60 : }
61 :
62 : impl fmt::Debug for ExportInfo<'_> {
63 190560 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 190560 : f.debug_struct("ExportInfo")
65 190560 : .field("node_offset", &self.node_offset())
66 190560 : .field("flags", &self.flags())
67 190560 : .field("address", &self.address())
68 190560 : .field("other", &self.other())
69 190560 : .field("kind", &self.kind())
70 190560 : .finish()
71 190560 : }
72 : }
73 :
74 : impl ExportInfo<'_> {
75 : /// Original offset in the export Trie
76 190560 : pub fn node_offset(&self) -> u64 {
77 190560 : self.ptr.node_offset()
78 190560 : }
79 :
80 190560 : pub fn flags(&self) -> Flags {
81 190560 : Flags::from(self.ptr.flags())
82 190560 : }
83 :
84 : /// The address of the export
85 190560 : pub fn address(&self) -> u64 {
86 190560 : self.ptr.address()
87 190560 : }
88 :
89 190560 : pub fn other(&self) -> u64 {
90 190560 : self.ptr.other()
91 190560 : }
92 :
93 : /// The export's kind (regular, thread local, absolute, ...)
94 190560 : pub fn kind(&self) -> Kind {
95 190560 : Kind::from(self.ptr.kind())
96 190560 : }
97 :
98 : /// Symbol associated with this export
99 9200 : pub fn symbol(&self) -> Option<Symbol> {
100 9200 : into_optional(self.ptr.symbol())
101 9200 : }
102 :
103 : /// If the export is a re-export ([`Flags::REEXPORT`]) this function returns
104 : /// the symbol being re-exported
105 9200 : pub fn alias(&self) -> Option<Symbol> {
106 9200 : into_optional(self.ptr.alias())
107 9200 : }
108 :
109 : /// If the export is a re-export ([`Flags::REEXPORT`]) this function returns
110 : /// the library from which the symbol is re-exported
111 9200 : pub fn alias_library(&self) -> Option<Dylib> {
112 9200 : into_optional(self.ptr.alias_library())
113 9200 : }
114 : }
115 :
116 : impl<'a> FromFFI<ffi::MachO_ExportInfo> for ExportInfo<'a> {
117 190560 : fn from_ffi(ptr: cxx::UniquePtr<ffi::MachO_ExportInfo>) -> Self {
118 190560 : Self {
119 190560 : ptr,
120 190560 : _owner: PhantomData,
121 190560 : }
122 190560 : }
123 : }
|