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
use crate::error;
use crate::mach::load_command;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt::{self, Debug};
use core::ops::Range;
use scroll::{Pread, Uleb128};
type Flag = u64;
pub const EXPORT_SYMBOL_FLAGS_KIND_MASK: Flag = 0x03;
pub const EXPORT_SYMBOL_FLAGS_KIND_REGULAR: Flag = 0x00;
pub const EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE: Flag = 0x02;
pub const EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL: Flag = 0x01;
pub const EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION: Flag = 0x04;
pub const EXPORT_SYMBOL_FLAGS_REEXPORT: Flag = 0x08;
pub const EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER: Flag = 0x10;
#[derive(Debug)]
pub enum SymbolKind {
Regular,
Absolute,
ThreadLocal,
UnknownSymbolKind(Flag),
}
impl SymbolKind {
pub fn new(kind: Flag) -> SymbolKind {
match kind & EXPORT_SYMBOL_FLAGS_KIND_MASK {
0x00 => SymbolKind::Regular,
0x01 => SymbolKind::ThreadLocal,
0x02 => SymbolKind::Absolute,
_ => SymbolKind::UnknownSymbolKind(kind),
}
}
pub fn to_str(&self) -> &'static str {
match self {
SymbolKind::Regular => "Regular",
SymbolKind::Absolute => "Absolute",
SymbolKind::ThreadLocal => "Thread_LOCAL",
SymbolKind::UnknownSymbolKind(_k) => "Unknown",
}
}
}
#[derive(Debug)]
pub enum ExportInfo<'a> {
Regular { address: u64, flags: Flag },
Reexport {
lib: &'a str,
lib_symbol_name: Option<&'a str>,
flags: Flag,
},
Stub {
stub_offset: scroll::Uleb128,
resolver_offset: scroll::Uleb128,
flags: Flag,
},
}
impl<'a> ExportInfo<'a> {
pub fn parse(
bytes: &'a [u8],
libs: &[&'a str],
flags: Flag,
mut offset: usize,
) -> error::Result<ExportInfo<'a>> {
use self::ExportInfo::*;
let regular = |offset| -> error::Result<ExportInfo> {
let address = bytes.pread::<Uleb128>(offset)?;
Ok(Regular {
address: address.into(),
flags,
})
};
let reexport = |mut offset| -> error::Result<ExportInfo<'a>> {
let lib_ordinal: u64 = {
let tmp = bytes.pread::<Uleb128>(offset)?;
offset += tmp.size();
tmp.into()
};
let lib_symbol_name = bytes.pread::<&str>(offset)?;
let lib = libs[lib_ordinal as usize];
let lib_symbol_name = if lib_symbol_name == "" {
None
} else {
Some(lib_symbol_name)
};
Ok(Reexport {
lib,
lib_symbol_name,
flags,
})
};
match SymbolKind::new(flags) {
SymbolKind::Regular => {
if flags & EXPORT_SYMBOL_FLAGS_REEXPORT != 0 {
reexport(offset)
} else if flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER != 0 {
let stub_offset = bytes.pread::<Uleb128>(offset)?;
offset += stub_offset.size();
let resolver_offset = bytes.pread::<Uleb128>(offset)?;
Ok(Stub {
stub_offset,
resolver_offset,
flags,
})
} else {
regular(offset)
}
}
SymbolKind::ThreadLocal | SymbolKind::Absolute => {
if flags & EXPORT_SYMBOL_FLAGS_REEXPORT != 0 {
reexport(offset)
} else {
regular(offset)
}
}
SymbolKind::UnknownSymbolKind(_kind) => {
regular(offset)
}
}
}
}
#[derive(Debug)]
pub struct Export<'a> {
pub name: String,
pub info: ExportInfo<'a>,
pub size: usize,
pub offset: u64,
}
impl<'a> Export<'a> {
pub fn new(name: String, info: ExportInfo<'a>) -> Export<'a> {
let offset = match info {
ExportInfo::Regular { address, .. } => address,
_ => 0x0,
};
Export {
name,
info,
size: 0,
offset,
}
}
}
pub struct ExportTrie<'a> {
data: &'a [u8],
location: Range<usize>,
}
impl<'a> ExportTrie<'a> {
#[inline]
fn walk_nodes(
&self,
libs: &[&'a str],
branches: Vec<(String, usize)>,
acc: &mut Vec<Export<'a>>,
) -> error::Result<()> {
for (symbol, next_node) in branches {
self.walk_trie(libs, symbol, next_node, acc)?;
}
Ok(())
}
fn walk_branches(
&self,
nbranches: usize,
current_symbol: String,
mut offset: usize,
) -> error::Result<Vec<(String, usize)>> {
let mut branches = Vec::with_capacity(nbranches);
for _i in 0..nbranches {
let offset = &mut offset;
let string = self.data.pread::<&str>(*offset)?;
let mut key = current_symbol.clone();
key.push_str(string);
*offset = *offset + string.len() + 1;
let next_node = Uleb128::read(&self.data, offset)? as usize + self.location.start;
branches.push((key, next_node));
}
Ok(branches)
}
fn walk_trie(
&self,
libs: &[&'a str],
current_symbol: String,
start: usize,
exports: &mut Vec<Export<'a>>,
) -> error::Result<()> {
if start < self.location.end {
let mut offset = start;
let terminal_size = Uleb128::read(&self.data, &mut offset)?;
if terminal_size == 0 {
let nbranches = Uleb128::read(&self.data, &mut offset)? as usize;
let branches = self.walk_branches(nbranches, current_symbol, offset)?;
self.walk_nodes(libs, branches, exports)
} else {
let pos = offset;
let children_start = &mut (pos + terminal_size as usize);
let nchildren = Uleb128::read(&self.data, children_start)? as usize;
let flags = Uleb128::read(&self.data, &mut offset)?;
let info = ExportInfo::parse(&self.data, libs, flags, offset)?;
let export = Export::new(current_symbol.clone(), info);
exports.push(export);
if nchildren == 0 {
Ok(())
} else {
let branches =
self.walk_branches(nchildren, current_symbol, *children_start)?;
self.walk_nodes(libs, branches, exports)
}
}
} else {
Ok(())
}
}
pub fn exports(&self, libs: &[&'a str]) -> error::Result<Vec<Export<'a>>> {
let offset = self.location.start;
let current_symbol = String::new();
let mut exports = Vec::new();
self.walk_trie(libs, current_symbol, offset, &mut exports)?;
Ok(exports)
}
pub fn new(bytes: &'a [u8], command: &load_command::DyldInfoCommand) -> Self {
let start = command.export_off as usize;
let end = (command.export_size + command.export_off) as usize;
ExportTrie {
data: bytes,
location: start..end,
}
}
}
impl<'a> Debug for ExportTrie<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("ExportTrie")
.field("data", &"<... redacted ...>")
.field(
"location",
&format_args!("{:#x}..{:#x}", self.location.start, self.location.end),
)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn export_trie() {
const EXPORTS: [u8; 64] = [
0x00, 0x01, 0x5f, 0x00, 0x05, 0x00, 0x02, 0x5f, 0x6d, 0x68, 0x5f, 0x65, 0x78, 0x65,
0x63, 0x75, 0x74, 0x65, 0x5f, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x00, 0x1f, 0x6d,
0x61, 0x00, 0x23, 0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x78, 0x69, 0x6d, 0x75, 0x6d,
0x00, 0x30, 0x69, 0x6e, 0x00, 0x35, 0x03, 0x00, 0xc0, 0x1e, 0x00, 0x03, 0x00, 0xd0,
0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let exports = &EXPORTS[..];
let libs = vec!["/usr/lib/libderp.so", "/usr/lib/libthuglife.so"];
let mut command = load_command::DyldInfoCommand::default();
command.export_size = exports.len() as u32;
let trie = ExportTrie::new(&exports, &command);
println!("trie: {:#?}", &trie);
let exports = trie.exports(&libs).unwrap();
println!("len: {} exports: {:#?}", exports.len(), &exports);
assert_eq!(exports.len() as usize, 3usize)
}
}