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
use std::collections::HashMap;
use memory::Memory;
pub fn load_instructions(file:&elflib::File, mem:&mut dyn Memory) -> bool {
file.get_section(".text").map_or(false, | text | -> bool {
let mut i = 0;
while i < text.data.len() {
let x = if file.ehdr.data == elflib::types::ELFDATA2LSB {
u32::from_le(text.data.get_32(i))
} else {
u32::from_be(text.data.get_32(i))
};
let addr = (text.shdr.addr as usize) + i;
mem.set_32(addr, x);
i += 4
}
true
})
}
pub fn load_rodata(file:&elflib::File, mem:&mut dyn Memory) -> bool {
file.get_section(".rodata").map_or(false, | section | -> bool {
let mut rodata_i = section.shdr.addr as usize;
for byte in §ion.data {
mem.set_8(rodata_i, *byte);
rodata_i += 1
}
true
})
}
pub fn get_plt_symbols(file:&elflib::File) -> Option<HashMap<i32, String>> {
let symtab = file.get_section(".symtab")?;
let symbols = file.get_symbols(symtab).ok()?;
let plt = file.get_section(".plt")?;
let mut ret = HashMap::new();
for sym in symbols {
if sym.value >= plt.shdr.addr && sym.value < plt.shdr.addr + plt.shdr.size {
ret.insert(sym.value as i32, sym.name);
}
}
Some(ret)
}
pub fn get_main_pc(file:&elflib::File) -> Option<i32> {
let symtab = file.get_section(".symtab")?;
let symbols = file.get_symbols(symtab).ok()?;
for sym in symbols {
if sym.name == "main" {
return Some(sym.value as i32)
}
}
None
}