-
Notifications
You must be signed in to change notification settings - Fork 7
/
macho.rs
409 lines (386 loc) · 14.4 KB
/
macho.rs
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use crate::dwarf::{collect_dwarf_address_debug_data, AddressPair};
use crate::error::{GetSymbolsError, Result};
use crate::shared::{
object_to_map, FileAndPathHelper, OwnedFileData, SymbolicationQuery, SymbolicationResult,
SymbolicationResultKind,
};
use addr2line::object;
use goblin::mach;
use object::read::{File, Object};
use std::collections::{BTreeSet, HashMap, VecDeque};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use uuid::Uuid;
pub async fn get_symbolication_result_multiarch<'a, R>(
owned_data: Rc<impl OwnedFileData>,
query: SymbolicationQuery<'a>,
helper: &impl FileAndPathHelper,
) -> Result<R>
where
R: SymbolicationResult,
{
let buffer = owned_data.get_data();
let mut errors = vec![];
let arches: Vec<_> = {
let multi_arch = mach::MultiArch::new(buffer)?;
multi_arch
.iter_arches()
.filter_map(std::result::Result::ok)
.collect()
};
for fat_arch in arches {
let slice = (fat_arch.offset as usize)..(fat_arch.offset as usize + fat_arch.size as usize);
match get_symbolication_result(owned_data.clone(), Some(slice), query.clone(), helper).await
{
Ok(table) => return Ok(table),
Err(err) => errors.push(err),
}
}
Err(GetSymbolsError::NoMatchMultiArch(errors))
}
pub async fn get_symbolication_result<'a, 'b, R>(
owned_data: Rc<impl OwnedFileData>,
slice: Option<std::ops::Range<usize>>,
query: SymbolicationQuery<'a>,
helper: &impl FileAndPathHelper,
) -> Result<R>
where
R: SymbolicationResult,
{
let mut buffer = owned_data.get_data();
if let Some(slice) = slice {
buffer = &buffer[slice];
}
let macho_file =
File::parse(buffer).or_else(|x| Err(GetSymbolsError::MachOHeaderParseError(x)))?;
let macho_id = match macho_file.mach_uuid() {
Ok(Some(uuid)) => format!("{:X}0", Uuid::from_bytes(uuid).to_simple()),
_ => {
return Err(GetSymbolsError::InvalidInputError(
"Could not get mach uuid",
))
}
};
let SymbolicationQuery {
breakpad_id,
addresses,
..
} = query;
if macho_id != breakpad_id {
return Err(GetSymbolsError::UnmatchedBreakpadId(
macho_id,
breakpad_id.to_string(),
));
}
let map = object_to_map(&macho_file);
let mut symbolication_result = R::from_full_map(map, addresses);
if let SymbolicationResultKind::SymbolsForAddresses {
with_debug_info: true,
} = R::result_kind()
{
let mut remainder = VecDeque::new();
// Look up addresses that don't have external debug info, and collect information
// about the ones that do have external debug info.
let goblin_macho = mach::MachO::parse(buffer, 0)?;
let addresses_in_this_object: Vec<_> =
addresses.iter().map(|a| AddressPair::same(*a)).collect();
remainder.extend(collect_debug_info_and_remainder(
&macho_file,
&goblin_macho,
&addresses_in_this_object,
&mut symbolication_result,
)?);
// Now we're done with the original file. Release it.
drop(owned_data);
// Do a breadth-first-traversal of the external debug info reference tree.
while let Some(obj_ref) = remainder.pop_front() {
let path = obj_ref.path();
let owned_data = match helper.read_file(path).await {
Ok(data) => data,
Err(_) => {
// We probably couldn't find the file, but that's fine.
// It would be good to collect this error somewhere.
continue;
}
};
let buffer = owned_data.get_data();
match obj_ref {
ObjectReference::Regular {
path, functions, ..
} => {
let macho_file = File::parse(buffer)
.or_else(|x| Err(GetSymbolsError::MachOHeaderParseError(x)))?;
let addresses_in_this_object =
translate_addresses_to_object(&path, &macho_file, functions);
let goblin_macho = mach::MachO::parse(buffer, 0)?;
remainder.extend(collect_debug_info_and_remainder(
&macho_file,
&goblin_macho,
&addresses_in_this_object,
&mut symbolication_result,
)?);
}
ObjectReference::Archive {
path, archive_info, ..
} => {
let archive = goblin::archive::Archive::parse(buffer)?;
for (name_in_archive, functions) in archive_info {
let buffer = match archive.extract(&name_in_archive, buffer) {
Ok(buffer) => buffer,
Err(_) => continue,
};
let macho_file = File::parse(buffer)
.or_else(|x| Err(GetSymbolsError::MachOHeaderParseError(x)))?;
let addresses_in_this_object =
translate_addresses_to_object(&path, &macho_file, functions);
let goblin_macho = mach::MachO::parse(buffer, 0)?;
remainder.extend(collect_debug_info_and_remainder(
&macho_file,
&goblin_macho,
&addresses_in_this_object,
&mut symbolication_result,
)?);
}
}
};
}
}
Ok(symbolication_result)
}
fn translate_addresses_to_object<'data, 'file, O>(
_path: &Path,
macho_file: &'file O,
mut functions: HashMap<String, Vec<AddressWithOffset>>,
) -> Vec<AddressPair>
where
O: object::Object<'data, 'file>,
{
let mut addresses_in_this_object = Vec::new();
for (_, symbol) in macho_file.symbols() {
if let Some(symbol_name) = symbol.name() {
if let Some(addresses) = functions.remove(symbol_name) {
for AddressWithOffset {
original_address,
offset_from_function_start,
} in addresses
{
let address_in_this_object =
symbol.address() as u32 + offset_from_function_start;
addresses_in_this_object.push(AddressPair {
original_address,
address_in_this_object,
});
}
}
}
}
addresses_in_this_object.sort_by_key(|ap| ap.address_in_this_object);
addresses_in_this_object
}
enum ObjectReference {
Regular {
path: PathBuf,
functions: HashMap<String, Vec<AddressWithOffset>>,
},
Archive {
path: PathBuf,
archive_info: HashMap<String, HashMap<String, Vec<AddressWithOffset>>>,
},
}
impl ObjectReference {
fn path(&self) -> &Path {
match self {
ObjectReference::Regular { path, .. } => path,
ObjectReference::Archive { path, .. } => path,
}
}
}
fn collect_debug_info_and_remainder<'data, 'file, 'a, O, R>(
macho_file: &'file O,
goblin_macho: &'a mach::MachO<'data>,
addresses: &[AddressPair],
symbolication_result: &mut R,
) -> Result<Vec<ObjectReference>>
where
O: object::Object<'data, 'file>,
R: SymbolicationResult,
{
let ObjectsAndFunctions { objects, functions } = ObjectsAndFunctions::from_macho(&goblin_macho);
let functions_with_addresses = match_funs_to_addresses(&functions, addresses);
let mut external_funs_by_object: HashMap<usize, HashMap<String, Vec<AddressWithOffset>>> =
HashMap::new();
let mut original_addresses_found_in_external_objects = BTreeSet::new();
for MatchedFunctionWithAddresses {
object_index,
fun_name,
addresses,
} in functions_with_addresses.into_iter()
{
for AddressWithOffset {
original_address, ..
} in &addresses
{
original_addresses_found_in_external_objects.insert(*original_address);
}
external_funs_by_object
.entry(object_index)
.or_insert_with(HashMap::new)
.insert(fun_name.to_owned(), addresses);
}
let internal_addresses: Vec<_> = addresses
.iter()
.cloned()
.filter(|ap| !original_addresses_found_in_external_objects.contains(&ap.original_address))
.collect();
collect_dwarf_address_debug_data(macho_file, &internal_addresses, symbolication_result);
let mut archives = HashMap::new();
let mut regular_objects = HashMap::new();
for (object_index, functions) in external_funs_by_object.into_iter() {
let object_name = objects[object_index].name;
match object_name.find('(') {
Some(index) => {
// This is an "archive" reference of the form
// "/Users/mstange/code/obj-m-opt/toolkit/library/build/../../../js/src/build/libjs_static.a(Unified_cpp_js_src13.o)"
let (path, paren_rest) = object_name.split_at(index);
let path: PathBuf = path.into();
let name_in_archive = paren_rest
.trim_start_matches('(')
.trim_end_matches(')')
.to_string();
let archive_info = archives.entry(path).or_insert_with(HashMap::new);
archive_info.insert(name_in_archive, functions);
}
None => {
// This is a reference to a regular object file. Example:
// "/Users/mstange/code/obj-m-opt/toolkit/library/build/../../components/sessionstore/Unified_cpp_sessionstore0.o"
let path: PathBuf = object_name.into();
regular_objects.insert(path, functions);
}
}
}
let mut combined: Vec<_> = Vec::new();
for (path, archive_info) in archives.into_iter() {
combined.push(ObjectReference::Archive { path, archive_info });
}
for (path, functions) in regular_objects.into_iter() {
combined.push(ObjectReference::Regular { path, functions });
}
Ok(combined)
}
#[derive(Debug)]
struct AddressWithOffset {
original_address: u32,
offset_from_function_start: u32,
}
struct MatchedFunctionWithAddresses<'a> {
object_index: usize,
fun_name: &'a str,
addresses: Vec<AddressWithOffset>,
}
// functions must be sorted by function.address_range.start
// addresses must be sorted
fn match_funs_to_addresses<'a, 'b, 'c>(
functions: &'a [Function<'c>],
addresses: &'b [AddressPair],
) -> Vec<MatchedFunctionWithAddresses<'c>> {
let mut yo: Vec<MatchedFunctionWithAddresses> = Vec::new();
let mut addr_iter = addresses.iter();
let mut cur_addr = addr_iter.next();
let mut fun_iter = functions.iter();
let mut cur_fun = fun_iter.next();
let mut cur_fun_is_last_vec_element = false;
while let (Some(address_pair), Some(fun)) = (cur_addr, cur_fun) {
let original_address = address_pair.original_address;
let address_in_this_object = address_pair.address_in_this_object;
if !(fun.address_range.start <= address_in_this_object) {
// Advance address_pair.
cur_addr = addr_iter.next();
continue;
}
if !(address_in_this_object < fun.address_range.end || !fun.object_index.is_some()) {
// Advance fun.
cur_fun = fun_iter.next();
cur_fun_is_last_vec_element = false;
continue;
}
// Now the following is true:
// fun.object_index.is_some() &&
// fun.address_range.start <= address_in_this_object && address_in_this_object < fun.addr_range.end
let offset_from_function_start = address_in_this_object - fun.address_range.start;
let address_with_offset = AddressWithOffset {
original_address,
offset_from_function_start,
};
if cur_fun_is_last_vec_element {
yo.last_mut().unwrap().addresses.push(address_with_offset);
} else {
yo.push(MatchedFunctionWithAddresses {
object_index: fun.object_index.unwrap(),
fun_name: fun.name,
addresses: vec![address_with_offset],
});
cur_fun_is_last_vec_element = true;
}
// Advance addr.
cur_addr = addr_iter.next();
}
yo
}
#[derive(Debug)]
struct OriginObject<'a> {
name: &'a str,
}
#[derive(Debug)]
struct Function<'a> {
name: &'a str,
address_range: std::ops::Range<u32>,
object_index: Option<usize>,
}
struct ObjectsAndFunctions<'a> {
objects: Vec<OriginObject<'a>>,
functions: Vec<Function<'a>>,
}
impl<'a> ObjectsAndFunctions<'a> {
pub fn from_macho(macho: &mach::MachO<'a>) -> Self {
use mach::symbols;
let mut objects = Vec::new();
let mut functions = Vec::new();
let mut current_function = None;
for symbol in macho.symbols() {
let (name, nlist) = match symbol {
Ok(sym) => sym,
Err(_) => continue,
};
if !nlist.is_stab() {
continue;
}
match nlist.n_type {
symbols::N_OSO => {
objects.push(OriginObject { name });
}
symbols::N_FUN => {
if !name.is_empty() {
current_function = Some((name, nlist.n_value));
} else if let Some((name, start_address)) = current_function.take() {
let start_address = start_address as u32;
let size = nlist.n_value as u32;
let object_index = if objects.is_empty() {
None
} else {
Some(objects.len() - 1)
};
let address_range = start_address..(start_address + size);
functions.push(Function {
name,
address_range,
object_index,
});
}
}
_ => {}
}
}
functions.sort_by_key(|f| f.address_range.start);
Self { objects, functions }
}
}