ocaml-solo5-elftool/bin/main.ml

60 lines
1.6 KiB
OCaml
Raw Permalink Normal View History

2025-01-30 11:39:39 +00:00
let map_binary file =
let fd = Unix.openfile file [Unix.O_RDONLY; Unix.O_CLOEXEC] 0 in
let stat = Unix.fstat fd in
let map () ~pos len =
let len = Int.min (stat.Unix.st_size - pos) len in
let pos = Int64.of_int pos in
let barr =
Unix.map_file fd ~pos Bigarray.char Bigarray.c_layout false [| len |]
in
Bigarray.array1_of_genarray barr
in
2025-01-30 11:43:16 +00:00
at_exit (fun () -> Unix.close fd);
2025-01-30 11:39:39 +00:00
Cachet.make ~map ()
2021-12-10 11:06:52 +00:00
let query_manifest file =
2025-01-30 11:39:39 +00:00
map_binary file
2021-12-10 11:06:52 +00:00
|> Solo5_elftool.query_manifest
|> Result.fold
~ok:(fun mft ->
Fmt.pr "%a\n" Solo5_elftool.pp_mft mft)
~error:(fun (`Msg e) ->
Fmt.epr "%s\n" e)
2021-12-07 21:51:25 +00:00
let query_abi file =
2025-01-30 11:39:39 +00:00
map_binary file
|> Solo5_elftool.query_abi
|> Result.fold
~ok:(fun abi -> Fmt.pr "%a\n" Solo5_elftool.pp_abi abi)
~error:(fun (`Msg e) ->
Fmt.epr "%s\n" e)
2021-12-07 21:51:25 +00:00
let file =
let doc = "Solo5 executable" in
Cmdliner.Arg.(required & pos 0 (some file) None &
info ~doc ~docv:"EXECUTABLE" [])
2021-12-10 11:06:52 +00:00
let query_manifest_cmd =
let doc = "query solo5 manifest" in
2025-01-24 13:06:42 +00:00
Cmdliner.Cmd.v
(Cmdliner.Cmd.info ~doc "query-manifest")
Cmdliner.Term.(const query_manifest $ file)
2021-12-07 21:51:25 +00:00
let query_abi_cmd =
let doc = "query solo5 abi" in
2025-01-24 13:06:42 +00:00
Cmdliner.Cmd.v
(Cmdliner.Cmd.info ~doc "query-abi")
Cmdliner.Term.(const query_abi $ file)
let default_cmd =
2025-01-24 13:06:42 +00:00
let open Cmdliner.Term in
ret (const (fun man_format -> `Help (man_format, None)) $ Cmdliner.Arg.man_format)
2021-12-07 21:51:25 +00:00
let () =
2025-01-24 13:06:42 +00:00
let cmd =
Cmdliner.Cmd.group ~default:default_cmd
(Cmdliner.Cmd.info "osolo5-elftool")
[query_manifest_cmd; query_abi_cmd]
in
exit (Cmdliner.Cmd.eval cmd)