f482b15
From b57344b6f2527f13dfcf6452353f09c85faef940 Mon Sep 17 00:00:00 2001
a071122
From: "Richard W.M. Jones" <rjones@redhat.com>
a071122
Date: Tue, 29 May 2012 20:40:36 +0100
88f9772
Subject: [PATCH 04/11] ocamlbyteinfo, ocamlplugininfo: Useful utilities from
a071122
 Debian, sent upstream.
a071122
a071122
See:
a071122
http://git.debian.org/?p=pkg-ocaml-maint/packages/ocaml.git;a=tree;f=debian/ocamlbyteinfo;hb=HEAD
a071122
---
ed0c3aa
 ocamlbyteinfo.ml   | 101 +++++++++++++++++++++++++++++++++++++++++++++++++
ed0c3aa
 ocamlplugininfo.ml | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++
963a64f
 2 files changed, 210 insertions(+)
a071122
 create mode 100644 ocamlbyteinfo.ml
a071122
 create mode 100644 ocamlplugininfo.ml
a071122
a071122
diff --git a/ocamlbyteinfo.ml b/ocamlbyteinfo.ml
a071122
new file mode 100644
a071122
index 0000000..eb9a293
a071122
--- /dev/null
a071122
+++ b/ocamlbyteinfo.ml
a071122
@@ -0,0 +1,101 @@
a071122
+(***********************************************************************)
a071122
+(*                                                                     *)
a071122
+(*                           Objective Caml                            *)
a071122
+(*                                                                     *)
a071122
+(*            Xavier Leroy, projet Gallium, INRIA Rocquencourt         *)
a071122
+(*                                                                     *)
a071122
+(*  Copyright 2009 Institut National de Recherche en Informatique et   *)
a071122
+(*  en Automatique.  All rights reserved.  This file is distributed    *)
a071122
+(*  under the terms of the GNU Library General Public License, with    *)
a071122
+(*  the special exception on linking described in file ../../LICENSE.  *)
a071122
+(*                                                                     *)
a071122
+(***********************************************************************)
a071122
+
a071122
+(* $Id$ *)
a071122
+
a071122
+(* Dumps a bytecode binary file *)
a071122
+
a071122
+open Sys
a071122
+open Dynlinkaux
a071122
+
a071122
+let input_stringlist ic len =
a071122
+  let get_string_list sect len =
a071122
+    let rec fold s e acc =
a071122
+      if e != len then
a071122
+        if sect.[e] = '\000' then
a071122
+          fold (e+1) (e+1) (String.sub sect s (e-s) :: acc)
a071122
+        else fold s (e+1) acc
a071122
+      else acc
a071122
+    in fold 0 0 []
a071122
+  in
a071122
+  let sect = String.create len in
a071122
+  let _ = really_input ic sect 0 len in
a071122
+  get_string_list sect len
a071122
+
a071122
+let print = Printf.printf
a071122
+let perr s =
a071122
+  Printf.eprintf "%s\n" s;
a071122
+  exit(1)
a071122
+let p_title title = print "%s:\n" title
a071122
+
a071122
+let p_section title format pdata = function
a071122
+  | [] -> ()
a071122
+  | l ->
a071122
+      p_title title;
a071122
+      List.iter
a071122
+        (fun (name, data) -> print format (pdata data) name)
a071122
+        l
a071122
+
a071122
+let p_list title format = function
a071122
+  | [] -> ()
a071122
+  | l ->
a071122
+      p_title title;
a071122
+      List.iter
a071122
+        (fun name -> print format name)
a071122
+        l
a071122
+
a071122
+let _ =
a071122
+  try
a071122
+    let input_name = Sys.argv.(1) in
a071122
+    let ic = open_in_bin input_name in
a071122
+    Bytesections.read_toc ic;
a071122
+    List.iter
a071122
+      (fun section ->
a071122
+         try
a071122
+           let len = Bytesections.seek_section ic section in
a071122
+           if len > 0 then match section with
a071122
+             | "CRCS" ->
a071122
+                 p_section
a071122
+                   "Imported Units"
a071122
+                   "\t%s\t%s\n"
a071122
+                   Digest.to_hex
a071122
+                   (input_value ic : (string * Digest.t) list)
a071122
+             | "DLLS" ->
a071122
+                 p_list
a071122
+                   "Used Dlls" "\t%s\n"
a071122
+                   (input_stringlist ic len)
a071122
+             | "DLPT" ->
a071122
+                 p_list
a071122
+                   "Additional Dll paths"
a071122
+                   "\t%s\n"
a071122
+                   (input_stringlist ic len)
a071122
+             | "PRIM" ->
a071122
+                 let prims = (input_stringlist ic len) in
a071122
+                 print "Uses unsafe features: ";
a071122
+                 begin match prims with
a071122
+                     [] -> print "no\n"
a071122
+                   | l  -> print "YES\n";
a071122
+                       p_list "Primitives declared in this module"
a071122
+                         "\t%s\n"
a071122
+                         l
a071122
+                 end
a071122
+             | _ -> ()
a071122
+         with Not_found | Failure _ | Invalid_argument _ -> ()
a071122
+      )
a071122
+      ["CRCS"; "DLLS"; "DLPT"; "PRIM"];
a071122
+    close_in ic
a071122
+  with
a071122
+    | Sys_error msg ->
a071122
+        perr msg
a071122
+    | Invalid_argument("index out of bounds") ->
a071122
+        perr (Printf.sprintf "Usage: %s filename" Sys.argv.(0))
a071122
diff --git a/ocamlplugininfo.ml b/ocamlplugininfo.ml
a071122
new file mode 100644
a071122
index 0000000..e28800f
a071122
--- /dev/null
a071122
+++ b/ocamlplugininfo.ml
a071122
@@ -0,0 +1,109 @@
a071122
+(***********************************************************************)
a071122
+(*                                                                     *)
a071122
+(*                           Objective Caml                            *)
a071122
+(*                                                                     *)
a071122
+(*            Xavier Leroy, projet Gallium, INRIA Rocquencourt         *)
a071122
+(*                                                                     *)
a071122
+(*  Copyright 2009 Institut National de Recherche en Informatique et   *)
a071122
+(*  en Automatique.  All rights reserved.  This file is distributed    *)
a071122
+(*  under the terms of the GNU Library General Public License, with    *)
a071122
+(*  the special exception on linking described in file ../../LICENSE.  *)
a071122
+(*                                                                     *)
a071122
+(***********************************************************************)
a071122
+
a071122
+(* $Id$ *)
a071122
+
a071122
+(* Dumps a .cmxs file *)
a071122
+
a071122
+open Natdynlink
a071122
+open Format
a071122
+
a071122
+let file =
a071122
+  try
a071122
+    Sys.argv.(1)
a071122
+  with _ -> begin
a071122
+    Printf.eprintf "Usage: %s file.cmxs\n" Sys.argv.(0);
a071122
+    exit(1)
a071122
+  end
a071122
+
a071122
+exception Abnormal_exit
a071122
+
a071122
+let error s e =
a071122
+  let eprint = Printf.eprintf in
a071122
+  let print_exc s = function
a071122
+    | End_of_file ->
a071122
+       eprint "%s: %s\n" s file
a071122
+    | Abnormal_exit ->
a071122
+        eprint "%s\n" s
a071122
+    | e -> eprint "%s\n" (Printexc.to_string e)
a071122
+  in
a071122
+    print_exc s e;
a071122
+    exit(1)
a071122
+
a071122
+let read_in command =
a071122
+  let cmd = Printf.sprintf command file in
a071122
+  let ic = Unix.open_process_in cmd in
a071122
+  try
a071122
+    let line = input_line ic in
a071122
+    begin match (Unix.close_process_in ic) with
a071122
+      | Unix.WEXITED 0 -> Str.split (Str.regexp "[ ]+") line
a071122
+      | Unix.WEXITED _  | Unix.WSIGNALED _ | Unix.WSTOPPED _ ->
a071122
+          error
a071122
+            (Printf.sprintf
a071122
+               "Command \"%s\" exited abnormally"
a071122
+               cmd
a071122
+            )
a071122
+            Abnormal_exit
a071122
+    end
a071122
+  with e -> error "File is empty" e
a071122
+
a071122
+let get_offset adr_off adr_sec =
a071122
+  try
a071122
+    let adr = List.nth adr_off 4 in
a071122
+    let off = List.nth adr_off 5 in
a071122
+    let sec = List.hd adr_sec in
a071122
+
a071122
+    let (!) x = Int64.of_string ("0x" ^ x) in
a071122
+    let (+) = Int64.add in
a071122
+    let (-) = Int64.sub in
a071122
+
a071122
+      Int64.to_int (!off + !sec - !adr)
a071122
+
a071122
+  with Failure _ | Invalid_argument _ ->
a071122
+    error
a071122
+      "Command output doesn't have the expected format"
a071122
+      Abnormal_exit
a071122
+
a071122
+let print_infos name crc defines cmi cmx =
a071122
+  let print_name_crc (name, crc) =
a071122
+    printf "@ %s (%s)" name (Digest.to_hex crc)
a071122
+  in
a071122
+  let pr_imports ppf imps = List.iter print_name_crc imps in
a071122
+  printf "Name: %s@." name;
a071122
+  printf "CRC of implementation: %s@." (Digest.to_hex crc);
a071122
+  printf "@[<hov 2>Globals defined:";
a071122
+  List.iter (fun s -> printf "@ %s" s) defines;
a071122
+  printf "@]@.";
a071122
+  printf "@[<v 2>Interfaces imported:%a@]@." pr_imports cmi;
a071122
+  printf "@[<v 2>Implementations imported:%a@]@." pr_imports cmx
a071122
+
a071122
+let _ =
a071122
+  let adr_off = read_in "objdump -h %s | grep ' .data '" in
a071122
+  let adr_sec = read_in "objdump -T %s | grep ' caml_plugin_header$'" in
a071122
+
a071122
+  let ic = open_in file in
a071122
+  let _ = seek_in ic (get_offset adr_off adr_sec) in
a071122
+  let header  = (input_value ic : Natdynlink.dynheader) in
a071122
+    if header.magic <> Natdynlink.dyn_magic_number then
a071122
+      raise(Error(Natdynlink.Not_a_bytecode_file file))
a071122
+    else begin
a071122
+      List.iter
a071122
+        (fun ui ->
a071122
+           print_infos
a071122
+             ui.name
a071122
+             ui.crc
a071122
+             ui.defines
a071122
+             ui.imports_cmi
a071122
+             ui.imports_cmx)
a071122
+        header.units
a071122
+    end
a071122
-- 
f482b15
1.9.3
a071122