components/ocaml/files/ocamlbyteinfo.ml
author Brian Cameron <brian.cameron@oracle.com>
Wed, 21 Aug 2013 14:33:32 -0700
changeset 1450 02791a89ea4f
permissions -rw-r--r--
17262870 Migrate ocaml to Userland
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1450
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     1
(***********************************************************************)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     2
(*                                                                     *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     3
(*                           Objective Caml                            *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     4
(*                                                                     *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     5
(*            Xavier Leroy, projet Gallium, INRIA Rocquencourt         *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     6
(*                                                                     *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     7
(*  Copyright 2009 Institut National de Recherche en Informatique et   *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     8
(*  en Automatique.  All rights reserved.  This file is distributed    *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
     9
(*  under the terms of the GNU Library General Public License, with    *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    10
(*  the special exception on linking described in file ../../LICENSE.  *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    11
(*                                                                     *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    12
(***********************************************************************)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    13
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    14
(* $Id: ocamlbyteinfo.ml,v 1.1 2010/01/11 18:45:03 rjones Exp $ *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    15
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    16
(* Dumps a bytecode binary file *)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    17
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    18
open Sys
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    19
open Dynlinkaux
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    20
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    21
let input_stringlist ic len =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    22
  let get_string_list sect len =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    23
    let rec fold s e acc =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    24
      if e != len then
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    25
        if sect.[e] = '\000' then
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    26
          fold (e+1) (e+1) (String.sub sect s (e-s) :: acc)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    27
        else fold s (e+1) acc
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    28
      else acc
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    29
    in fold 0 0 []
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    30
  in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    31
  let sect = String.create len in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    32
  let _ = really_input ic sect 0 len in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    33
  get_string_list sect len
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    34
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    35
let print = Printf.printf
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    36
let perr s =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    37
  Printf.eprintf "%s\n" s;
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    38
  exit(1)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    39
let p_title title = print "%s:\n" title
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    40
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    41
let p_section title format pdata = function
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    42
  | [] -> ()
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    43
  | l ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    44
      p_title title;
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    45
      List.iter
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    46
        (fun (name, data) -> print format (pdata data) name)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    47
        l
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    48
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    49
let p_list title format = function
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    50
  | [] -> ()
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    51
  | l ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    52
      p_title title;
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    53
      List.iter
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    54
        (fun name -> print format name)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    55
        l
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    56
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    57
let _ =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    58
  try
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    59
    let input_name = Sys.argv.(1) in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    60
    let ic = open_in_bin input_name in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    61
    Bytesections.read_toc ic;
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    62
    List.iter
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    63
      (fun section ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    64
         try
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    65
           let len = Bytesections.seek_section ic section in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    66
           if len > 0 then match section with
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    67
             | "CRCS" ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    68
                 p_section
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    69
                   "Imported Units"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    70
                   "\t%s\t%s\n"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    71
                   Digest.to_hex
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    72
                   (input_value ic : (string * Digest.t) list)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    73
             | "DLLS" ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    74
                 p_list
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    75
                   "Used Dlls" "\t%s\n"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    76
                   (input_stringlist ic len)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    77
             | "DLPT" ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    78
                 p_list
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    79
                   "Additional Dll paths"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    80
                   "\t%s\n"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    81
                   (input_stringlist ic len)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    82
             | "PRIM" ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    83
                 let prims = (input_stringlist ic len) in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    84
                 print "Uses unsafe features: ";
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    85
                 begin match prims with
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    86
                     [] -> print "no\n"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    87
                   | l  -> print "YES\n";
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    88
                       p_list "Primitives declared in this module"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    89
                         "\t%s\n"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    90
                         l
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    91
                 end
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    92
             | _ -> ()
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    93
         with Not_found | Failure _ | Invalid_argument _ -> ()
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    94
      )
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    95
      ["CRCS"; "DLLS"; "DLPT"; "PRIM"];
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    96
    close_in ic
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    97
  with
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    98
    | Sys_error msg ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    99
        perr msg
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   100
    | Invalid_argument("index out of bounds") ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   101
        perr (Printf.sprintf "Usage: %s filename" Sys.argv.(0))