Skip to content

Commit

Permalink
main_process: read elf file
Browse files Browse the repository at this point in the history
  • Loading branch information
sourabhniyogi committed Sep 25, 2024
1 parent 87e0187 commit 001755e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tools/polkatool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ enum Args {
/// The input files.
inputs: Vec<PathBuf>,
},

Process,
}

macro_rules! bail {
Expand Down Expand Up @@ -90,6 +92,7 @@ fn main() {
} => main_disassemble(input, format, display_gas, show_raw_bytes, output),
Args::Assemble { input, output } => main_assemble(input, output),
Args::Stats { inputs } => main_stats(inputs),
Args::Process => main_process(), // New match arm
};

if let Err(error) = result {
Expand Down Expand Up @@ -233,3 +236,28 @@ fn main_assemble(input_path: PathBuf, output_path: PathBuf) -> Result<(), String

Ok(())
}

fn main_process() -> Result<(), String> {
use std::fs;

let mut config = polkavm_linker::Config::default();
config.set_strip(true);
config.set_dispatch_table(vec![
b"is_authorized_ext".into(),
b"refine_ext".into(),
b"accumulate_ext".into(),
b"on_transfer_ext".into(),
]);

let elf = fs::read("../../../guest-programs/jam-service-fib/custom-target/riscv32ema-unknown-none-elf/release/jam-service-fib")
.map_err(|err| format!("Failed to read ELF file: {}", err))?;
let raw_blob = polkavm_linker::program_from_elf(config, elf.as_ref())
.map_err(|err| format!("Failed to create program from ELF: {}", err))?;
let parts = polkavm_linker::ProgramParts::from_bytes(raw_blob.into())
.map_err(|err| format!("Failed to parse program parts: {}", err))?;

fs::write("blob.pvm", &parts.code_and_jump_table)
.map_err(|err| format!("Failed to write blob.pvm: {}", err))?;

Ok(())
}

0 comments on commit 001755e

Please sign in to comment.