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;

/// Use the function to load code (`.text` section) into a memory buffer.
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
    })
}

/// Use this function to load `.rodata` section into a memory buffer.
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 &section.data {
            mem.set_8(rodata_i, *byte);
            rodata_i += 1
        }

        true
    })
}

/// Use this function to retrieve the mapping of linked functions addresses to
/// their name. Useful when emulating `libc` calls.
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)
}


/// Use this function to retrieve the address of a binary symbol in the elf.
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
}