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
use std::cmp::Ordering;
use itertools::Itertools;
use pelite::pe::{exception::UnwindInfo, Pe, Rva};
use crate::Program;
pub struct Function<'a> {
pub entry: Rva,
pub size: u32,
pub unwind_info: Option<UnwindInfo<'a, Program<'a>>>,
}
pub fn find_functions(program: Program) -> impl Iterator<Item = Function> + '_ {
program
.exception()
.ok()
.into_iter()
.flat_map(|exception_table| exception_table.functions())
.map(|function| Function {
entry: function.image().BeginAddress,
size: function.image().EndAddress - function.image().BeginAddress,
unwind_info: function.unwind_info().ok(),
})
.sorted_by_key(|func| func.entry)
}
pub fn find_function_containing(program: Program, address: Rva) -> Option<Function> {
let mut functions: Vec<Function> = find_functions(program).collect();
functions
.binary_search_by(|f| {
if address >= f.entry + f.size {
Ordering::Greater
} else if address < f.entry {
Ordering::Less
} else {
Ordering::Equal
}
})
.ok()
.map(move |index| functions.swap_remove(index))
}