Skip to main content

macho_objc/
types.rs

1use crate::model::addr::Va;
2use crate::objc::encoding::{ObjCMethodSignature, ObjCPropertyAttributes, ObjCQualifiedType};
3use std::fmt;
4
5#[derive(Debug, Clone)]
6/// The ObjCClass type.
7pub struct ObjCClass {
8    /// The name field.
9    pub name: String,
10    /// The superclass_name field.
11    pub superclass_name: Option<String>,
12    /// The instance_methods field.
13    pub instance_methods: Vec<ObjCMethod>,
14    /// The class_methods field.
15    pub class_methods: Vec<ObjCMethod>,
16    /// The ivars field.
17    pub ivars: Vec<ObjCIvar>,
18    /// The properties field.
19    pub properties: Vec<ObjCProperty>,
20    /// The protocols field.
21    pub protocols: Vec<String>,
22    /// The instance_size field.
23    pub instance_size: u32,
24    /// The is_meta field.
25    pub is_meta: bool,
26    /// The is_swift field.
27    pub is_swift: bool,
28}
29
30#[derive(Debug, Clone)]
31/// The ObjCCategory type.
32pub struct ObjCCategory {
33    /// The name field.
34    pub name: String,
35    /// The class_name field.
36    pub class_name: String,
37    /// The instance_methods field.
38    pub instance_methods: Vec<ObjCMethod>,
39    /// The class_methods field.
40    pub class_methods: Vec<ObjCMethod>,
41    /// The properties field.
42    pub properties: Vec<ObjCProperty>,
43    /// The protocols field.
44    pub protocols: Vec<String>,
45}
46
47#[derive(Debug, Clone)]
48/// The ObjCProtocol type.
49pub struct ObjCProtocol {
50    /// The name field.
51    pub name: String,
52    /// The instance_methods field.
53    pub instance_methods: Vec<ObjCMethod>,
54    /// The class_methods field.
55    pub class_methods: Vec<ObjCMethod>,
56    /// The optional_instance_methods field.
57    pub optional_instance_methods: Vec<ObjCMethod>,
58    /// The optional_class_methods field.
59    pub optional_class_methods: Vec<ObjCMethod>,
60    /// The properties field.
61    pub properties: Vec<ObjCProperty>,
62    /// The adopted_protocols field.
63    pub adopted_protocols: Vec<String>,
64}
65
66#[derive(Debug, Clone)]
67/// The ObjCMethod type.
68pub struct ObjCMethod {
69    /// The name field.
70    pub name: String,
71    /// The type_encoding field.
72    pub type_encoding: String,
73    /// The imp field.
74    pub imp: Va,
75}
76
77impl fmt::Display for ObjCMethod {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        write!(f, "{} ({})", self.name, self.type_encoding)
80    }
81}
82
83impl ObjCMethod {
84    /// Performs parsed_signature.
85    pub fn parsed_signature(&self) -> Option<ObjCMethodSignature> {
86        ObjCMethodSignature::parse(&self.type_encoding).ok()
87    }
88}
89
90#[derive(Debug, Clone)]
91/// The ObjCIvar type.
92pub struct ObjCIvar {
93    /// The name field.
94    pub name: String,
95    /// The decoded type encoding, or `None` when its pointer cannot be
96    /// resolved. An empty encoding means the ivar carries none, which is how
97    /// Swift stored properties appear.
98    pub type_encoding: Option<String>,
99    /// The decoded runtime offset, or `None` when its pointer cannot be resolved.
100    pub offset: Option<u32>,
101    /// The size field.
102    pub size: u32,
103    /// The alignment field.
104    pub alignment: u32,
105}
106
107impl ObjCIvar {
108    /// Performs parsed_type.
109    pub fn parsed_type(&self) -> Option<ObjCQualifiedType> {
110        ObjCQualifiedType::parse(self.type_encoding.as_deref()?).ok()
111    }
112}
113
114#[derive(Debug, Clone)]
115/// The ObjCProperty type.
116pub struct ObjCProperty {
117    /// The name field.
118    pub name: String,
119    /// The attributes field.
120    pub attributes: String,
121    /// The is_class field.
122    pub is_class: bool,
123}
124
125impl ObjCProperty {
126    /// Performs parsed_attributes.
127    pub fn parsed_attributes(&self) -> ObjCPropertyAttributes {
128        ObjCPropertyAttributes::parse(&self.attributes)
129    }
130}
131
132// ObjC class_ro_t flags
133pub(crate) const CLASS_DATA_POINTER_MASK: u64 = !7;
134/// The RO_META constant.
135pub const RO_META: u32 = 1 << 0;
136/// The RO_ROOT constant.
137pub const RO_ROOT: u32 = 1 << 1;
138/// The RO_HAS_CXX_STRUCTORS constant.
139pub const RO_HAS_CXX_STRUCTORS: u32 = 1 << 2;
140/// The RO_HIDDEN constant.
141pub const RO_HIDDEN: u32 = 1 << 4;
142/// The RO_IS_ARC constant.
143pub const RO_IS_ARC: u32 = 1 << 7;
144/// The RO_HAS_CXX_DTOR_ONLY constant.
145pub const RO_HAS_CXX_DTOR_ONLY: u32 = 1 << 8;
146
147// method_list_t flags
148/// The METHOD_LIST_USES_RELATIVE_OFFSETS constant.
149pub const METHOD_LIST_USES_RELATIVE_OFFSETS: u32 = 0x8000_0000;
150/// Relative method selector offsets point directly to selector strings.
151pub const METHOD_LIST_USES_DIRECT_SELECTOR_OFFSETS: u32 = 0x4000_0000;
152/// The METHOD_LIST_ENTSIZE_MASK constant.
153pub const METHOD_LIST_ENTSIZE_MASK: u32 = 0x0000_FFFF;