1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use core::marker::PhantomData;
use crate::endian::Endian;
use crate::macho;
use crate::pod::{Bytes, Pod};
use crate::read::macho::{MachHeader, SymbolTable};
use crate::read::{ReadError, ReadRef, Result, StringTable};
#[derive(Debug, Default, Clone, Copy)]
pub struct LoadCommandIterator<'data, E: Endian> {
endian: E,
data: Bytes<'data>,
ncmds: u32,
}
impl<'data, E: Endian> LoadCommandIterator<'data, E> {
pub(super) fn new(endian: E, data: &'data [u8], ncmds: u32) -> Self {
LoadCommandIterator {
endian,
data: Bytes(data),
ncmds,
}
}
pub fn next(&mut self) -> Result<Option<LoadCommandData<'data, E>>> {
if self.ncmds == 0 {
return Ok(None);
}
let header = self
.data
.read_at::<macho::LoadCommand<E>>(0)
.read_error("Invalid Mach-O load command header")?;
let cmd = header.cmd.get(self.endian);
let cmdsize = header.cmdsize.get(self.endian) as usize;
let data = self
.data
.read_bytes(cmdsize)
.read_error("Invalid Mach-O load command size")?;
self.ncmds -= 1;
Ok(Some(LoadCommandData {
cmd,
data,
marker: Default::default(),
}))
}
}
#[derive(Debug, Clone, Copy)]
pub struct LoadCommandData<'data, E: Endian> {
cmd: u32,
data: Bytes<'data>,
marker: PhantomData<E>,
}
impl<'data, E: Endian> LoadCommandData<'data, E> {
pub fn cmd(&self) -> u32 {
self.cmd
}
pub fn cmdsize(&self) -> u32 {
self.data.len() as u32
}
#[inline]
pub fn data<T: Pod>(&self) -> Result<&'data T> {
self.data
.read_at(0)
.read_error("Invalid Mach-O command size")
}
pub fn string(&self, endian: E, s: macho::LcStr<E>) -> Result<&'data [u8]> {
self.data
.read_string_at(s.offset.get(endian) as usize)
.read_error("Invalid load command string offset")
}
pub fn variant(&self) -> Result<LoadCommandVariant<'data, E>> {
Ok(match self.cmd {
macho::LC_SEGMENT => {
let mut data = self.data;
let segment = data.read().read_error("Invalid Mach-O command size")?;
LoadCommandVariant::Segment32(segment, data.0)
}
macho::LC_SYMTAB => LoadCommandVariant::Symtab(self.data()?),
macho::LC_THREAD | macho::LC_UNIXTHREAD => {
let mut data = self.data;
let thread = data.read().read_error("Invalid Mach-O command size")?;
LoadCommandVariant::Thread(thread, data.0)
}
macho::LC_DYSYMTAB => LoadCommandVariant::Dysymtab(self.data()?),
macho::LC_LOAD_DYLIB
| macho::LC_LOAD_WEAK_DYLIB
| macho::LC_REEXPORT_DYLIB
| macho::LC_LAZY_LOAD_DYLIB
| macho::LC_LOAD_UPWARD_DYLIB => LoadCommandVariant::Dylib(self.data()?),
macho::LC_ID_DYLIB => LoadCommandVariant::IdDylib(self.data()?),
macho::LC_LOAD_DYLINKER => LoadCommandVariant::LoadDylinker(self.data()?),
macho::LC_ID_DYLINKER => LoadCommandVariant::IdDylinker(self.data()?),
macho::LC_PREBOUND_DYLIB => LoadCommandVariant::PreboundDylib(self.data()?),
macho::LC_ROUTINES => LoadCommandVariant::Routines32(self.data()?),
macho::LC_SUB_FRAMEWORK => LoadCommandVariant::SubFramework(self.data()?),
macho::LC_SUB_UMBRELLA => LoadCommandVariant::SubUmbrella(self.data()?),
macho::LC_SUB_CLIENT => LoadCommandVariant::SubClient(self.data()?),
macho::LC_SUB_LIBRARY => LoadCommandVariant::SubLibrary(self.data()?),
macho::LC_TWOLEVEL_HINTS => LoadCommandVariant::TwolevelHints(self.data()?),
macho::LC_PREBIND_CKSUM => LoadCommandVariant::PrebindCksum(self.data()?),
macho::LC_SEGMENT_64 => {
let mut data = self.data;
let segment = data.read().read_error("Invalid Mach-O command size")?;
LoadCommandVariant::Segment64(segment, data.0)
}
macho::LC_ROUTINES_64 => LoadCommandVariant::Routines64(self.data()?),
macho::LC_UUID => LoadCommandVariant::Uuid(self.data()?),
macho::LC_RPATH => LoadCommandVariant::Rpath(self.data()?),
macho::LC_CODE_SIGNATURE
| macho::LC_SEGMENT_SPLIT_INFO
| macho::LC_FUNCTION_STARTS
| macho::LC_DATA_IN_CODE
| macho::LC_DYLIB_CODE_SIGN_DRS
| macho::LC_LINKER_OPTIMIZATION_HINT
| macho::LC_DYLD_EXPORTS_TRIE
| macho::LC_DYLD_CHAINED_FIXUPS => LoadCommandVariant::LinkeditData(self.data()?),
macho::LC_ENCRYPTION_INFO => LoadCommandVariant::EncryptionInfo32(self.data()?),
macho::LC_DYLD_INFO | macho::LC_DYLD_INFO_ONLY => {
LoadCommandVariant::DyldInfo(self.data()?)
}
macho::LC_VERSION_MIN_MACOSX
| macho::LC_VERSION_MIN_IPHONEOS
| macho::LC_VERSION_MIN_TVOS
| macho::LC_VERSION_MIN_WATCHOS => LoadCommandVariant::VersionMin(self.data()?),
macho::LC_DYLD_ENVIRONMENT => LoadCommandVariant::DyldEnvironment(self.data()?),
macho::LC_MAIN => LoadCommandVariant::EntryPoint(self.data()?),
macho::LC_SOURCE_VERSION => LoadCommandVariant::SourceVersion(self.data()?),
macho::LC_ENCRYPTION_INFO_64 => LoadCommandVariant::EncryptionInfo64(self.data()?),
macho::LC_LINKER_OPTION => LoadCommandVariant::LinkerOption(self.data()?),
macho::LC_NOTE => LoadCommandVariant::Note(self.data()?),
macho::LC_BUILD_VERSION => LoadCommandVariant::BuildVersion(self.data()?),
macho::LC_FILESET_ENTRY => LoadCommandVariant::FilesetEntry(self.data()?),
_ => LoadCommandVariant::Other,
})
}
pub fn segment_32(self) -> Result<Option<(&'data macho::SegmentCommand32<E>, &'data [u8])>> {
if self.cmd == macho::LC_SEGMENT {
let mut data = self.data;
let segment = data.read().read_error("Invalid Mach-O command size")?;
Ok(Some((segment, data.0)))
} else {
Ok(None)
}
}
pub fn symtab(self) -> Result<Option<&'data macho::SymtabCommand<E>>> {
if self.cmd == macho::LC_SYMTAB {
Some(self.data()).transpose()
} else {
Ok(None)
}
}
pub fn dysymtab(self) -> Result<Option<&'data macho::DysymtabCommand<E>>> {
if self.cmd == macho::LC_DYSYMTAB {
Some(self.data()).transpose()
} else {
Ok(None)
}
}
pub fn dylib(self) -> Result<Option<&'data macho::DylibCommand<E>>> {
if self.cmd == macho::LC_LOAD_DYLIB
|| self.cmd == macho::LC_LOAD_WEAK_DYLIB
|| self.cmd == macho::LC_REEXPORT_DYLIB
|| self.cmd == macho::LC_LAZY_LOAD_DYLIB
|| self.cmd == macho::LC_LOAD_UPWARD_DYLIB
{
Some(self.data()).transpose()
} else {
Ok(None)
}
}
pub fn uuid(self) -> Result<Option<&'data macho::UuidCommand<E>>> {
if self.cmd == macho::LC_UUID {
Some(self.data()).transpose()
} else {
Ok(None)
}
}
pub fn segment_64(self) -> Result<Option<(&'data macho::SegmentCommand64<E>, &'data [u8])>> {
if self.cmd == macho::LC_SEGMENT_64 {
let mut data = self.data;
let command = data.read().read_error("Invalid Mach-O command size")?;
Ok(Some((command, data.0)))
} else {
Ok(None)
}
}
pub fn dyld_info(self) -> Result<Option<&'data macho::DyldInfoCommand<E>>> {
if self.cmd == macho::LC_DYLD_INFO || self.cmd == macho::LC_DYLD_INFO_ONLY {
Some(self.data()).transpose()
} else {
Ok(None)
}
}
pub fn entry_point(self) -> Result<Option<&'data macho::EntryPointCommand<E>>> {
if self.cmd == macho::LC_MAIN {
Some(self.data()).transpose()
} else {
Ok(None)
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum LoadCommandVariant<'data, E: Endian> {
Segment32(&'data macho::SegmentCommand32<E>, &'data [u8]),
Symtab(&'data macho::SymtabCommand<E>),
Thread(&'data macho::ThreadCommand<E>, &'data [u8]),
Dysymtab(&'data macho::DysymtabCommand<E>),
Dylib(&'data macho::DylibCommand<E>),
IdDylib(&'data macho::DylibCommand<E>),
LoadDylinker(&'data macho::DylinkerCommand<E>),
IdDylinker(&'data macho::DylinkerCommand<E>),
PreboundDylib(&'data macho::PreboundDylibCommand<E>),
Routines32(&'data macho::RoutinesCommand32<E>),
SubFramework(&'data macho::SubFrameworkCommand<E>),
SubUmbrella(&'data macho::SubUmbrellaCommand<E>),
SubClient(&'data macho::SubClientCommand<E>),
SubLibrary(&'data macho::SubLibraryCommand<E>),
TwolevelHints(&'data macho::TwolevelHintsCommand<E>),
PrebindCksum(&'data macho::PrebindCksumCommand<E>),
Segment64(&'data macho::SegmentCommand64<E>, &'data [u8]),
Routines64(&'data macho::RoutinesCommand64<E>),
Uuid(&'data macho::UuidCommand<E>),
Rpath(&'data macho::RpathCommand<E>),
LinkeditData(&'data macho::LinkeditDataCommand<E>),
EncryptionInfo32(&'data macho::EncryptionInfoCommand32<E>),
DyldInfo(&'data macho::DyldInfoCommand<E>),
VersionMin(&'data macho::VersionMinCommand<E>),
DyldEnvironment(&'data macho::DylinkerCommand<E>),
EntryPoint(&'data macho::EntryPointCommand<E>),
SourceVersion(&'data macho::SourceVersionCommand<E>),
EncryptionInfo64(&'data macho::EncryptionInfoCommand64<E>),
LinkerOption(&'data macho::LinkerOptionCommand<E>),
Note(&'data macho::NoteCommand<E>),
BuildVersion(&'data macho::BuildVersionCommand<E>),
FilesetEntry(&'data macho::FilesetEntryCommand<E>),
Other,
}
impl<E: Endian> macho::SymtabCommand<E> {
pub fn symbols<'data, Mach: MachHeader<Endian = E>, R: ReadRef<'data>>(
&self,
endian: E,
data: R,
) -> Result<SymbolTable<'data, Mach>> {
let symbols = data
.read_slice_at(
self.symoff.get(endian).into(),
self.nsyms.get(endian) as usize,
)
.read_error("Invalid Mach-O symbol table offset or size")?;
let strings = data
.read_bytes_at(
self.stroff.get(endian).into(),
self.strsize.get(endian).into(),
)
.read_error("Invalid Mach-O string table offset or size")?;
let strings = StringTable::new(strings);
Ok(SymbolTable::new(symbols, strings))
}
}