From 001755e77926fdfab90ff324ac2a7b71b3722114 Mon Sep 17 00:00:00 2001 From: Sourabh Niyogi Date: Wed, 25 Sep 2024 14:39:04 -0700 Subject: [PATCH] main_process: read elf file --- tools/polkatool/src/main.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tools/polkatool/src/main.rs b/tools/polkatool/src/main.rs index 4f7fb3c9..f4b1951a 100644 --- a/tools/polkatool/src/main.rs +++ b/tools/polkatool/src/main.rs @@ -62,6 +62,8 @@ enum Args { /// The input files. inputs: Vec, }, + + Process, } macro_rules! bail { @@ -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 { @@ -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(()) +}