Blame SOURCES/0005-ocamlbyteinfo-ocamlplugininfo-Useful-utilities-from-.patch

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