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
use std::{fs::File, path::PathBuf};

use bincode::Options;
use clap::Parser;
use come::{backend::riscv::simple_instruction, binary_format::clef::Clef};

use shadow_rs::shadow;
shadow!(build);

/// clef format file information viewer.
#[derive(Parser, Debug)]
#[command(version, long_version = build::CLAP_LONG_VERSION, about, long_about = None)]
struct Args {
    /// Input file path.
    #[arg(short, long)]
    input: PathBuf,
}

fn main() {
    let args = Args::parse();
    let clef_file = File::open(args.input).unwrap();
    let loader = bincode::DefaultOptions::new().with_fixint_encoding();
    let clef: Clef = loader.deserialize_from(&clef_file).unwrap();
    println!("architecture: {}", clef.architecture);
    println!("os: {}", clef.os);
    for section in clef.sections {
        println!("section: {}", section.meta.name);
        println!("linkable: {}", section.meta.linkable);
        println!(
            "loadable: {}",
            if let Some(address) = section.meta.loadable {
                format!("should be loaded to 0x{address:0x}")
            } else {
                "no".to_string()
            }
        );
        println!("symbols:");
        for symbol in &section.meta.symbols {
            println!("  {symbol}");
        }
        println!("pending symbols:");
        if section.meta.pending_symbols.is_empty() {
            println!("    <none>")
        } else {
            for pending_symbol in &section.meta.pending_symbols {
                println!("  {pending_symbol}");
            }
        }
        println!("content:",);
        let instructions =
            simple_instruction::parse_whole_binary(&section.content, &section.meta.pending_symbols);
        for instruction in instructions {
            println!("  {instruction}");
        }
    }
}