From 73697a01478a61e7fa5385f881731c9111dcd9a4 Mon Sep 17 00:00:00 2001 From: Jon French Date: Mon, 15 Oct 2018 14:35:18 +0100 Subject: Interpreter: add new command :bin to load raw binary into memory --- src/elf_loader.ml | 18 ++++++++++++++++++ src/isail.ml | 14 +++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/elf_loader.ml b/src/elf_loader.ml index 88fcfddb..c6fb0589 100644 --- a/src/elf_loader.ml +++ b/src/elf_loader.ml @@ -149,6 +149,24 @@ let load_elf ?writer:(writer=write_sail_lib) name = opt_elf_tohost := tohost_addr); List.iter (load_segment ~writer:writer) segments +let load_binary ?writer:(writer=write_sail_lib) addr name = + let f = open_in_bin name in + let buf = Buffer.create 1024 in + try + while true do + let char = input_char f in + Buffer.add_char buf char; + done; + assert false + with + | End_of_file -> begin + Bytes.iteri (fun i ch -> writer addr i (int_of_char ch)) (Buffer.to_bytes buf); + close_in f + end + | exc -> + close_in f; + raise exc + (* The sail model can access this by externing a unit -> int function as Elf_loader.elf_entry. *) let elf_entry () = !opt_elf_entry diff --git a/src/isail.ml b/src/isail.ml index c3f869a3..4e237943 100644 --- a/src/isail.ml +++ b/src/isail.ml @@ -179,6 +179,8 @@ let help = function ":help - Get a description of . Commands are prefixed with a colon, e.g. :help :type." | ":elf" -> ":elf - Load an ELF file." + | ":bin" -> + ":bin
- Load a binary file at the given address." | ":r" | ":run" -> "(:r | :run) - Completely evaluate the currently evaluating expression." | ":s" | ":step" -> @@ -256,7 +258,7 @@ let handle_input' input = | ":commands" -> let commands = [ "Universal commands - :(t)ype :(i)nfer :(q)uit :(v)erbose :clear :commands :help :output :option"; - "Normal mode commands - :elf :(l)oad :(u)nload"; + "Normal mode commands - :elf :bin :(l)oad :(u)nload"; "Evaluation mode commands - :(r)un :(s)tep :(n)ormal"; ""; ":(c)ommand can be called as either :c or :command." ] @@ -320,6 +322,16 @@ let handle_input' input = begin match cmd with | ":elf" -> Elf_loader.load_elf arg + | ":bin" -> + begin + let args = Util.split_on_char ' ' arg in + match args with + | [addr_s; filename] -> + let addr = Big_int.of_string addr_s in + Elf_loader.load_binary addr filename + | _ -> + print_endline "Invalid argument for :bin, expected " + end | ":l" | ":load" -> let files = Util.split_on_char ' ' arg in let (_, ast, env) = load_files !interactive_env files in -- cgit v1.2.3