1(**************************************************************************) 2(* *) 3(* OCaml *) 4(* *) 5(* Xavier Leroy, projet Gallium, INRIA Paris *) 6(* *) 7(* Copyright 2015 Institut National de Recherche en Informatique et *) 8(* en Automatique. *) 9(* *) 10(* All rights reserved. This file is distributed under the terms of *) 11(* the GNU Lesser General Public License version 2.1, with the *) 12(* special exception on linking described in the file LICENSE. *) 13(* *) 14(**************************************************************************) 15 16(* Copy a bytecode executable, removing debugging information 17 and #! header from the copy. 18 Usage: stripdebug <source file> <dest file> 19*) 20 21open Printf 22open Misc 23 24let stripdebug infile outfile = 25 let ic = open_in_bin infile in 26 Bytesections.read_toc ic; 27 let toc = Bytesections.toc() in 28 let pos_first_section = Bytesections.pos_first_section ic in 29 let oc = 30 open_out_gen [Open_wronly; Open_creat; Open_trunc; Open_binary] 0o777 31 outfile in 32 (* Skip the #! header, going straight to the first section. *) 33 seek_in ic pos_first_section; 34 (* Copy each section except DBUG *) 35 Bytesections.init_record oc; 36 List.iter 37 (fun (name, len) -> 38 if name = "DBUG" then begin 39 seek_in ic (in_channel_length ic + len) 40 end else begin 41 copy_file_chunk ic oc len; 42 Bytesections.record oc name 43 end) 44 toc; 45 (* Rewrite the toc and trailer *) 46 Bytesections.write_toc_and_trailer oc; 47 (* Done *) 48 close_in ic; 49 close_out oc 50 51let _ = 52 if Array.length Sys.argv = 3 53 then stripdebug Sys.argv.(1) Sys.argv.(2) 54 else begin 55 eprintf "Usage: stripdebug <source file> <destination file>\n"; 56 exit 2 57 end 58