summaryrefslogtreecommitdiffstats
path: root/vendor/object-0.26.2/examples/objectmap.rs
blob: de8d8a0023e26544d2aa992920083057d55f2445 (plain)
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
use object::Object;
use std::{env, fs, process};

fn main() {
    let mut args = env::args().skip(1);
    if args.len() == 0 {
        eprintln!(
            "Usage: {} <file> [address] ...",
            env::args().next().unwrap()
        );
        process::exit(1);
    }

    let file_path = args.next().unwrap();
    let file = match fs::File::open(&file_path) {
        Ok(file) => file,
        Err(err) => {
            println!("Failed to open file '{}': {}", file_path, err,);
            process::exit(1);
        }
    };
    let file = match unsafe { memmap2::Mmap::map(&file) } {
        Ok(mmap) => mmap,
        Err(err) => {
            println!("Failed to map file '{}': {}", file_path, err,);
            process::exit(1);
        }
    };
    let file = match object::File::parse(&*file) {
        Ok(file) => file,
        Err(err) => {
            println!("Failed to parse file '{}': {}", file_path, err);
            process::exit(1);
        }
    };

    let map = file.object_map();

    if args.len() == 0 {
        for symbol in map.symbols() {
            print_symbol(symbol, &map);
        }
    } else {
        for arg in args {
            let mut arg = &arg[..];
            if arg.starts_with("0x") {
                arg = &arg[2..];
            }
            let address = u64::from_str_radix(arg, 16).expect("Failed to parse address");
            if let Some(symbol) = map.get(address) {
                print_symbol(symbol, &map);
            } else {
                println!("{:} not found", address);
            }
        }
    }
}

fn print_symbol(symbol: &object::ObjectMapEntry<'_>, map: &object::ObjectMap<'_>) {
    println!(
        "{:x} {:x} {} {}",
        symbol.address(),
        symbol.size(),
        String::from_utf8_lossy(symbol.name()),
        String::from_utf8_lossy(symbol.object(map)),
    );
}