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

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