components/ocaml/files/ocamlplugininfo.ml
author Petr Sumbera <petr.sumbera@oracle.com>
Fri, 11 Apr 2014 08:09:58 -0700
changeset 1823 aa5916252876
parent 1450 02791a89ea4f
permissions -rw-r--r--
PSARC/2014/099 APR and APR-util 1.5; EOF APR and APR-util 1.3 18503507 APR-util 1.5.3
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: ocamlplugininfo.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 .cmxs 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 Natdynlink
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    19
open Format
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 file =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    22
  try
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    23
    Sys.argv.(1)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    24
  with _ -> begin
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    25
    Printf.eprintf "Usage: %s file.cmxs\n" Sys.argv.(0);
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    26
    exit(1)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    27
  end
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    28
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    29
exception Abnormal_exit
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    30
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    31
let error s e =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    32
  let eprint = Printf.eprintf in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    33
  let print_exc s = function
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    34
    | End_of_file ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    35
       eprint "%s: %s\n" s file
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    36
    | Abnormal_exit ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    37
        eprint "%s\n" s
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    38
    | e -> eprint "%s\n" (Printexc.to_string e)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    39
  in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    40
    print_exc s e;
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    41
    exit(1)
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
let read_in command =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    44
  let cmd = Printf.sprintf command file in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    45
  let ic = Unix.open_process_in cmd in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    46
  try
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    47
    let line = input_line ic in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    48
    begin match (Unix.close_process_in ic) with
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    49
      | Unix.WEXITED 0 -> Str.split (Str.regexp "[ ]+") line
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    50
      | Unix.WEXITED _  | Unix.WSIGNALED _ | Unix.WSTOPPED _ ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    51
          error
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    52
            (Printf.sprintf
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    53
               "Command \"%s\" exited abnormally"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    54
               cmd
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    55
            )
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    56
            Abnormal_exit
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    57
    end
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    58
  with e -> error "File is empty" e
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    59
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    60
let get_offset adr_off adr_sec =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    61
  try
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    62
    let adr = List.nth adr_off 4 in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    63
    let off = List.nth adr_off 5 in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    64
    let sec = List.hd adr_sec in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    65
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    66
    let (!) x = Int64.of_string ("0x" ^ x) in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    67
    let (+) = Int64.add in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    68
    let (-) = Int64.sub in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    69
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    70
      Int64.to_int (!off + !sec - !adr)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    71
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    72
  with Failure _ | Invalid_argument _ ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    73
    error
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    74
      "Command output doesn't have the expected format"
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    75
      Abnormal_exit
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    76
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    77
let print_infos name crc defines cmi cmx =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    78
  let print_name_crc (name, crc) =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    79
    printf "@ %s (%s)" name (Digest.to_hex crc)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    80
  in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    81
  let pr_imports ppf imps = List.iter print_name_crc imps in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    82
  printf "Name: %s@." name;
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    83
  printf "CRC of implementation: %s@." (Digest.to_hex crc);
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    84
  printf "@[<hov 2>Globals defined:";
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    85
  List.iter (fun s -> printf "@ %s" s) defines;
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    86
  printf "@]@.";
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    87
  printf "@[<v 2>Interfaces imported:%a@]@." pr_imports cmi;
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    88
  printf "@[<v 2>Implementations imported:%a@]@." pr_imports cmx
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    89
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    90
let _ =
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    91
  let adr_off = read_in "objdump -h %s | grep ' .data '" in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    92
  let adr_sec = read_in "objdump -T %s | grep ' caml_plugin_header$'" in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    93
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    94
  let ic = open_in file in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    95
  let _ = seek_in ic (get_offset adr_off adr_sec) in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    96
  let header  = (input_value ic : Natdynlink.dynheader) in
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    97
    if header.magic <> Natdynlink.dyn_magic_number then
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    98
      raise(Error(Natdynlink.Not_a_bytecode_file file))
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
    99
    else begin
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   100
      List.iter
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   101
        (fun ui ->
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   102
           print_infos
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   103
             ui.name
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   104
             ui.crc
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   105
             ui.defines
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   106
             ui.imports_cmi
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   107
             ui.imports_cmx)
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   108
        header.units
02791a89ea4f 17262870 Migrate ocaml to Userland
Brian Cameron <brian.cameron@oracle.com>
parents:
diff changeset
   109
    end