1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
(* Link a set of .cmo files and produce a bytecode executable. *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Config
|
2006-05-11 08:50:53 -07:00
|
|
|
open Cmo_format
|
1995-07-02 09:45:21 -07:00
|
|
|
|
|
|
|
type error =
|
|
|
|
File_not_found of string
|
|
|
|
| Not_an_object_file of string
|
2012-09-01 06:10:10 -07:00
|
|
|
| Wrong_object_name of string
|
1995-07-02 09:45:21 -07:00
|
|
|
| Symbol_error of string * Symtable.error
|
|
|
|
| Inconsistent_import of string * string * string
|
|
|
|
| Custom_runtime
|
1996-11-07 02:56:52 -08:00
|
|
|
| File_exists of string
|
2001-08-28 07:47:48 -07:00
|
|
|
| Cannot_open_dll of string
|
2013-04-18 04:58:59 -07:00
|
|
|
| Not_compatible_32
|
2012-01-17 13:57:54 -08:00
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
exception Error of error
|
|
|
|
|
|
|
|
type link_action =
|
|
|
|
Link_object of string * compilation_unit
|
|
|
|
(* Name of .cmo file and descriptor of the unit *)
|
|
|
|
| Link_archive of string * compilation_unit list
|
|
|
|
(* Name of .cma file and descriptors of the units to be linked. *)
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
(* Add C objects and options from a library descriptor *)
|
2001-10-30 02:11:30 -08:00
|
|
|
(* Ignore them if -noautolink or -use-runtime or -use-prim was given *)
|
2000-03-09 01:12:28 -08:00
|
|
|
|
|
|
|
let lib_ccobjs = ref []
|
|
|
|
let lib_ccopts = ref []
|
2001-10-30 01:32:32 -08:00
|
|
|
let lib_dllibs = ref []
|
2000-03-09 01:12:28 -08:00
|
|
|
|
|
|
|
let add_ccobjs l =
|
2007-11-06 07:16:56 -08:00
|
|
|
if not !Clflags.no_auto_link then begin
|
|
|
|
if
|
|
|
|
String.length !Clflags.use_runtime = 0
|
2001-10-30 02:11:30 -08:00
|
|
|
&& String.length !Clflags.use_prims = 0
|
2007-11-06 07:16:56 -08:00
|
|
|
then begin
|
|
|
|
if l.lib_custom then Clflags.custom_runtime := true;
|
|
|
|
lib_ccobjs := l.lib_ccobjs @ !lib_ccobjs;
|
|
|
|
lib_ccopts := l.lib_ccopts @ !lib_ccopts;
|
|
|
|
end;
|
2001-10-30 01:32:32 -08:00
|
|
|
lib_dllibs := l.lib_dllibs @ !lib_dllibs
|
2000-03-09 01:12:28 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
(* A note on ccobj ordering:
|
2010-01-07 07:15:07 -08:00
|
|
|
- Clflags.ccobjs is in reverse order w.r.t. what was given on the
|
2000-03-09 01:12:28 -08:00
|
|
|
ocamlc command line;
|
|
|
|
- l.lib_ccobjs is also in reverse order w.r.t. what was given on the
|
|
|
|
ocamlc -a command line when the library was created;
|
|
|
|
- Clflags.ccobjs is reversed just before calling the C compiler for the
|
|
|
|
custom link;
|
|
|
|
- .cma files on the command line of ocamlc are scanned right to left;
|
|
|
|
- Before linking, we add lib_ccobjs after Clflags.ccobjs.
|
|
|
|
Thus, for ocamlc a.cma b.cma obj1 obj2
|
|
|
|
where a.cma was built with ocamlc -i ... obja1 obja2
|
|
|
|
and b.cma was built with ocamlc -i ... objb1 objb2
|
|
|
|
lib_ccobjs starts as [],
|
|
|
|
becomes objb2 objb1 when b.cma is scanned,
|
2001-08-28 07:47:48 -07:00
|
|
|
then obja2 obja1 objb2 objb1 when a.cma is scanned.
|
|
|
|
Clflags.ccobjs was initially obj2 obj1.
|
2000-03-09 01:12:28 -08:00
|
|
|
and is set to obj2 obj1 obja2 obja1 objb2 objb1.
|
|
|
|
Finally, the C compiler is given objb1 objb2 obja1 obja2 obj1 obj2,
|
|
|
|
which is what we need. (If b depends on a, a.cma must appear before
|
|
|
|
b.cma, but b's C libraries must appear before a's C libraries.)
|
|
|
|
*)
|
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
(* First pass: determine which units are needed *)
|
|
|
|
|
|
|
|
module IdentSet =
|
|
|
|
Set.Make(struct
|
|
|
|
type t = Ident.t
|
|
|
|
let compare = compare
|
|
|
|
end)
|
|
|
|
|
|
|
|
let missing_globals = ref IdentSet.empty
|
|
|
|
|
|
|
|
let is_required (rel, pos) =
|
|
|
|
match rel with
|
|
|
|
Reloc_setglobal id ->
|
|
|
|
IdentSet.mem id !missing_globals
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let add_required (rel, pos) =
|
|
|
|
match rel with
|
|
|
|
Reloc_getglobal id ->
|
|
|
|
missing_globals := IdentSet.add id !missing_globals
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
let remove_required (rel, pos) =
|
|
|
|
match rel with
|
|
|
|
Reloc_setglobal id ->
|
|
|
|
missing_globals := IdentSet.remove id !missing_globals
|
|
|
|
| _ -> ()
|
|
|
|
|
1995-07-11 11:05:47 -07:00
|
|
|
let scan_file obj_name tolink =
|
1995-07-02 09:45:21 -07:00
|
|
|
let file_name =
|
|
|
|
try
|
|
|
|
find_in_path !load_path obj_name
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(File_not_found obj_name)) in
|
|
|
|
let ic = open_in_bin file_name in
|
|
|
|
try
|
2014-04-29 04:56:17 -07:00
|
|
|
let buffer = really_input_string ic (String.length cmo_magic_number) in
|
1995-07-02 09:45:21 -07:00
|
|
|
if buffer = cmo_magic_number then begin
|
|
|
|
(* This is a .cmo file. It must be linked in any case.
|
|
|
|
Read the relocation information to see which modules it
|
|
|
|
requires. *)
|
|
|
|
let compunit_pos = input_binary_int ic in (* Go to descriptor *)
|
|
|
|
seek_in ic compunit_pos;
|
|
|
|
let compunit = (input_value ic : compilation_unit) in
|
|
|
|
close_in ic;
|
|
|
|
List.iter add_required compunit.cu_reloc;
|
|
|
|
Link_object(file_name, compunit) :: tolink
|
|
|
|
end
|
|
|
|
else if buffer = cma_magic_number then begin
|
|
|
|
(* This is an archive file. Each unit contained in it will be linked
|
|
|
|
in only if needed. *)
|
|
|
|
let pos_toc = input_binary_int ic in (* Go to table of contents *)
|
|
|
|
seek_in ic pos_toc;
|
2000-03-09 01:12:28 -08:00
|
|
|
let toc = (input_value ic : library) in
|
1995-07-02 09:45:21 -07:00
|
|
|
close_in ic;
|
2005-10-13 06:32:06 -07:00
|
|
|
add_ccobjs toc;
|
1995-07-02 09:45:21 -07:00
|
|
|
let required =
|
1995-07-11 11:05:47 -07:00
|
|
|
List.fold_right
|
|
|
|
(fun compunit reqd ->
|
1996-04-18 09:28:28 -07:00
|
|
|
if compunit.cu_force_link
|
2000-03-09 01:12:28 -08:00
|
|
|
|| !Clflags.link_everything
|
|
|
|
|| List.exists is_required compunit.cu_reloc
|
1995-07-02 09:45:21 -07:00
|
|
|
then begin
|
|
|
|
List.iter remove_required compunit.cu_reloc;
|
|
|
|
List.iter add_required compunit.cu_reloc;
|
|
|
|
compunit :: reqd
|
|
|
|
end else
|
|
|
|
reqd)
|
2000-03-09 01:12:28 -08:00
|
|
|
toc.lib_units [] in
|
1995-07-02 09:45:21 -07:00
|
|
|
Link_archive(file_name, required) :: tolink
|
|
|
|
end
|
|
|
|
else raise(Error(Not_an_object_file file_name))
|
1998-10-01 05:38:09 -07:00
|
|
|
with
|
|
|
|
End_of_file -> close_in ic; raise(Error(Not_an_object_file file_name))
|
|
|
|
| x -> close_in ic; raise x
|
1995-07-02 09:45:21 -07:00
|
|
|
|
|
|
|
(* Second pass: link in the required units *)
|
|
|
|
|
|
|
|
(* Consistency check between interfaces *)
|
|
|
|
|
2002-11-17 08:42:12 -08:00
|
|
|
let crc_interfaces = Consistbl.create ()
|
2014-05-06 17:34:20 -07:00
|
|
|
let interfaces = ref ([] : string list)
|
2012-01-17 13:57:54 -08:00
|
|
|
let implementations_defined = ref ([] : (string * string) list)
|
1995-07-02 09:45:21 -07:00
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let check_consistency ppf file_name cu =
|
2012-01-17 13:57:54 -08:00
|
|
|
begin try
|
2002-11-17 08:42:12 -08:00
|
|
|
List.iter
|
2014-05-06 17:34:20 -07:00
|
|
|
(fun (name, crco) ->
|
|
|
|
interfaces := name :: !interfaces;
|
|
|
|
match crco with
|
|
|
|
None -> ()
|
|
|
|
| Some crc ->
|
|
|
|
if name = cu.cu_name
|
|
|
|
then Consistbl.set crc_interfaces name crc file_name
|
|
|
|
else Consistbl.check crc_interfaces name crc file_name)
|
2002-11-17 08:42:12 -08:00
|
|
|
cu.cu_imports
|
|
|
|
with Consistbl.Inconsistency(name, user, auth) ->
|
|
|
|
raise(Error(Inconsistent_import(name, user, auth)))
|
2012-01-17 13:57:54 -08:00
|
|
|
end;
|
|
|
|
begin try
|
|
|
|
let source = List.assoc cu.cu_name !implementations_defined in
|
2012-01-20 06:23:34 -08:00
|
|
|
Location.print_warning (Location.in_file file_name) ppf
|
2013-03-09 14:38:52 -08:00
|
|
|
(Warnings.Multiple_definition(cu.cu_name,
|
|
|
|
Location.show_filename file_name,
|
|
|
|
Location.show_filename source))
|
2012-01-17 13:57:54 -08:00
|
|
|
with Not_found -> ()
|
|
|
|
end;
|
|
|
|
implementations_defined :=
|
|
|
|
(cu.cu_name, file_name) :: !implementations_defined
|
1995-07-02 09:45:21 -07:00
|
|
|
|
2002-02-08 08:55:44 -08:00
|
|
|
let extract_crc_interfaces () =
|
2014-05-06 17:34:20 -07:00
|
|
|
Consistbl.extract !interfaces crc_interfaces
|
|
|
|
|
|
|
|
let clear_crc_interfaces () =
|
|
|
|
Consistbl.clear crc_interfaces;
|
|
|
|
interfaces := []
|
2002-02-08 08:55:44 -08:00
|
|
|
|
1998-10-20 05:45:45 -07:00
|
|
|
(* Record compilation events *)
|
1996-11-29 10:36:42 -08:00
|
|
|
|
2013-04-17 02:07:00 -07:00
|
|
|
let debug_info = ref ([] : (int * LongString.t) list)
|
1996-11-29 10:36:42 -08:00
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
(* Link in a compilation unit *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let link_compunit ppf output_fun currpos_fun inchan file_name compunit =
|
|
|
|
check_consistency ppf file_name compunit;
|
1995-07-02 09:45:21 -07:00
|
|
|
seek_in inchan compunit.cu_pos;
|
2013-04-17 02:07:00 -07:00
|
|
|
let code_block = LongString.input_bytes inchan compunit.cu_codesize in
|
|
|
|
Symtable.ls_patch_object code_block compunit.cu_reloc;
|
1997-02-19 08:08:05 -08:00
|
|
|
if !Clflags.debug && compunit.cu_debug > 0 then begin
|
|
|
|
seek_in inchan compunit.cu_debug;
|
2013-04-17 02:07:00 -07:00
|
|
|
let buffer = LongString.input_bytes inchan compunit.cu_debugsize in
|
1998-10-20 05:45:45 -07:00
|
|
|
debug_info := (currpos_fun(), buffer) :: !debug_info
|
1997-02-19 08:08:05 -08:00
|
|
|
end;
|
2013-04-17 02:07:00 -07:00
|
|
|
Array.iter output_fun code_block;
|
1995-11-05 09:32:12 -08:00
|
|
|
if !Clflags.link_everything then
|
|
|
|
List.iter Symtable.require_primitive compunit.cu_primitives
|
1995-07-02 09:45:21 -07:00
|
|
|
|
|
|
|
(* Link in a .cmo file *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let link_object ppf output_fun currpos_fun file_name compunit =
|
1995-07-02 09:45:21 -07:00
|
|
|
let inchan = open_in_bin file_name in
|
|
|
|
try
|
2012-01-20 06:23:34 -08:00
|
|
|
link_compunit ppf output_fun currpos_fun inchan file_name compunit;
|
1995-07-02 09:45:21 -07:00
|
|
|
close_in inchan
|
|
|
|
with
|
|
|
|
Symtable.Error msg ->
|
|
|
|
close_in inchan; raise(Error(Symbol_error(file_name, msg)))
|
|
|
|
| x ->
|
|
|
|
close_in inchan; raise x
|
|
|
|
|
|
|
|
(* Link in a .cma file *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let link_archive ppf output_fun currpos_fun file_name units_required =
|
1995-07-02 09:45:21 -07:00
|
|
|
let inchan = open_in_bin file_name in
|
|
|
|
try
|
1996-11-29 10:36:42 -08:00
|
|
|
List.iter
|
1997-02-19 08:08:05 -08:00
|
|
|
(fun cu ->
|
|
|
|
let name = file_name ^ "(" ^ cu.cu_name ^ ")" in
|
|
|
|
try
|
2012-01-20 06:23:34 -08:00
|
|
|
link_compunit ppf output_fun currpos_fun inchan name cu
|
1997-02-19 08:08:05 -08:00
|
|
|
with Symtable.Error msg ->
|
|
|
|
raise(Error(Symbol_error(name, msg))))
|
1996-11-29 10:36:42 -08:00
|
|
|
units_required;
|
1995-07-02 09:45:21 -07:00
|
|
|
close_in inchan
|
1997-02-19 08:08:05 -08:00
|
|
|
with x -> close_in inchan; raise x
|
1995-07-02 09:45:21 -07:00
|
|
|
|
|
|
|
(* Link in a .cmo or .cma file *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let link_file ppf output_fun currpos_fun = function
|
1996-11-29 10:36:42 -08:00
|
|
|
Link_object(file_name, unit) ->
|
2012-01-20 06:23:34 -08:00
|
|
|
link_object ppf output_fun currpos_fun file_name unit
|
1996-11-29 10:36:42 -08:00
|
|
|
| Link_archive(file_name, units) ->
|
2012-01-20 06:23:34 -08:00
|
|
|
link_archive ppf output_fun currpos_fun file_name units
|
1995-07-02 09:45:21 -07:00
|
|
|
|
1998-10-20 05:45:45 -07:00
|
|
|
(* Output the debugging information *)
|
|
|
|
(* Format is:
|
|
|
|
<int32> number of event lists
|
|
|
|
<int32> offset of first event list
|
|
|
|
<output_value> first event list
|
|
|
|
...
|
|
|
|
<int32> offset of last event list
|
|
|
|
<output_value> last event list *)
|
|
|
|
|
|
|
|
let output_debug_info oc =
|
|
|
|
output_binary_int oc (List.length !debug_info);
|
|
|
|
List.iter
|
2013-09-04 08:12:37 -07:00
|
|
|
(fun (ofs, evl) ->
|
|
|
|
output_binary_int oc ofs;
|
2014-04-29 04:56:17 -07:00
|
|
|
Array.iter (output_bytes oc) evl)
|
1998-10-20 05:45:45 -07:00
|
|
|
!debug_info;
|
|
|
|
debug_info := []
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
(* Output a list of strings with 0-termination *)
|
1998-04-14 07:48:34 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
let output_stringlist oc l =
|
|
|
|
List.iter (fun s -> output_string oc s; output_byte oc 0) l
|
1998-04-14 07:48:34 -07:00
|
|
|
|
2001-10-30 02:11:30 -08:00
|
|
|
(* Transform a file name into an absolute file name *)
|
|
|
|
|
|
|
|
let make_absolute file =
|
|
|
|
if Filename.is_relative file
|
|
|
|
then Filename.concat (Sys.getcwd()) file
|
|
|
|
else file
|
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
(* Create a bytecode executable file *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let link_bytecode ppf tolink exec_name standalone =
|
2012-09-01 06:10:10 -07:00
|
|
|
(* Avoid the case where the specified exec output file is the same as
|
|
|
|
one of the objects to be linked *)
|
|
|
|
List.iter (function
|
|
|
|
| Link_object(file_name, _) when file_name = exec_name ->
|
|
|
|
raise (Error (Wrong_object_name exec_name));
|
|
|
|
| _ -> ()) tolink;
|
2004-07-13 05:19:15 -07:00
|
|
|
Misc.remove_file exec_name; (* avoid permission problems, cf PR#1911 *)
|
|
|
|
let outchan =
|
|
|
|
open_out_gen [Open_wronly; Open_trunc; Open_creat; Open_binary]
|
|
|
|
0o777 exec_name in
|
1995-07-02 09:45:21 -07:00
|
|
|
try
|
2001-08-28 07:47:48 -07:00
|
|
|
if standalone then begin
|
|
|
|
(* Copy the header *)
|
1995-07-02 09:45:21 -07:00
|
|
|
try
|
2001-10-30 02:11:30 -08:00
|
|
|
let header =
|
|
|
|
if String.length !Clflags.use_runtime > 0
|
2011-03-17 09:18:05 -07:00
|
|
|
then "camlheader_ur" else "camlheader" ^ !Clflags.runtime_variant in
|
2001-10-30 02:11:30 -08:00
|
|
|
let inchan = open_in_bin (find_in_path !load_path header) in
|
1995-07-02 09:45:21 -07:00
|
|
|
copy_file inchan outchan;
|
|
|
|
close_in inchan
|
|
|
|
with Not_found | Sys_error _ -> ()
|
|
|
|
end;
|
2000-03-05 11:18:50 -08:00
|
|
|
Bytesections.init_record outchan;
|
2001-10-30 02:11:30 -08:00
|
|
|
(* The path to the bytecode interpreter (in use_runtime mode) *)
|
|
|
|
if String.length !Clflags.use_runtime > 0 then begin
|
|
|
|
output_string outchan (make_absolute !Clflags.use_runtime);
|
|
|
|
output_char outchan '\n';
|
|
|
|
Bytesections.record outchan "RNTM"
|
|
|
|
end;
|
1995-07-02 09:45:21 -07:00
|
|
|
(* The bytecode *)
|
2000-03-05 11:18:50 -08:00
|
|
|
let start_code = pos_out outchan in
|
1995-07-02 09:45:21 -07:00
|
|
|
Symtable.init();
|
2014-05-06 17:34:20 -07:00
|
|
|
clear_crc_interfaces ();
|
2001-10-30 01:32:32 -08:00
|
|
|
let sharedobjs = List.map Dll.extract_dll_name !Clflags.dllibs in
|
2013-07-01 14:33:21 -07:00
|
|
|
let check_dlls = standalone && Config.target = Config.host in
|
|
|
|
if check_dlls then begin
|
2001-08-28 07:47:48 -07:00
|
|
|
(* Initialize the DLL machinery *)
|
2002-07-02 09:13:12 -07:00
|
|
|
Dll.init_compile !Clflags.no_std_include;
|
2001-08-28 07:47:48 -07:00
|
|
|
Dll.add_path !load_path;
|
2006-09-28 14:36:38 -07:00
|
|
|
try Dll.open_dlls Dll.For_checking sharedobjs
|
2001-08-28 07:47:48 -07:00
|
|
|
with Failure reason -> raise(Error(Cannot_open_dll reason))
|
|
|
|
end;
|
2014-04-29 04:56:17 -07:00
|
|
|
let output_fun = output_bytes outchan
|
2000-03-05 11:18:50 -08:00
|
|
|
and currpos_fun () = pos_out outchan - start_code in
|
2012-01-20 06:23:34 -08:00
|
|
|
List.iter (link_file ppf output_fun currpos_fun) tolink;
|
2013-07-01 14:33:21 -07:00
|
|
|
if check_dlls then Dll.close_all_dlls();
|
1995-07-02 09:45:21 -07:00
|
|
|
(* The final STOP instruction *)
|
|
|
|
output_byte outchan Opcodes.opSTOP;
|
|
|
|
output_byte outchan 0; output_byte outchan 0; output_byte outchan 0;
|
2000-03-05 11:18:50 -08:00
|
|
|
Bytesections.record outchan "CODE";
|
2001-08-28 07:47:48 -07:00
|
|
|
(* DLL stuff *)
|
|
|
|
if standalone then begin
|
|
|
|
(* The extra search path for DLLs *)
|
|
|
|
output_stringlist outchan !Clflags.dllpaths;
|
|
|
|
Bytesections.record outchan "DLPT";
|
|
|
|
(* The names of the DLLs *)
|
|
|
|
output_stringlist outchan sharedobjs;
|
|
|
|
Bytesections.record outchan "DLLS"
|
|
|
|
end;
|
1997-06-13 08:48:53 -07:00
|
|
|
(* The names of all primitives *)
|
|
|
|
Symtable.output_primitive_names outchan;
|
2000-03-05 11:18:50 -08:00
|
|
|
Bytesections.record outchan "PRIM";
|
1997-06-13 08:48:53 -07:00
|
|
|
(* The table of global data *)
|
2013-04-18 04:58:59 -07:00
|
|
|
begin try
|
|
|
|
Marshal.to_channel outchan (Symtable.initial_global_table())
|
|
|
|
(if !Clflags.bytecode_compatible_32
|
|
|
|
then [Marshal.Compat_32] else [])
|
|
|
|
with Failure _ ->
|
|
|
|
raise (Error Not_compatible_32)
|
|
|
|
end;
|
2000-03-05 11:18:50 -08:00
|
|
|
Bytesections.record outchan "DATA";
|
1996-02-15 08:19:09 -08:00
|
|
|
(* The map of global identifiers *)
|
1995-07-02 09:45:21 -07:00
|
|
|
Symtable.output_global_map outchan;
|
2000-03-05 11:18:50 -08:00
|
|
|
Bytesections.record outchan "SYMB";
|
2002-11-17 08:42:12 -08:00
|
|
|
(* CRCs for modules *)
|
|
|
|
output_value outchan (extract_crc_interfaces());
|
|
|
|
Bytesections.record outchan "CRCS";
|
1996-11-29 10:36:42 -08:00
|
|
|
(* Debug info *)
|
2000-03-05 11:18:50 -08:00
|
|
|
if !Clflags.debug then begin
|
|
|
|
output_debug_info outchan;
|
|
|
|
Bytesections.record outchan "DBUG"
|
|
|
|
end;
|
|
|
|
(* The table of contents and the trailer *)
|
|
|
|
Bytesections.write_toc_and_trailer outchan;
|
1995-07-02 09:45:21 -07:00
|
|
|
close_out outchan
|
|
|
|
with x ->
|
|
|
|
close_out outchan;
|
|
|
|
remove_file exec_name;
|
|
|
|
raise x
|
|
|
|
|
1996-11-07 02:56:52 -08:00
|
|
|
(* Output a string as a C array of unsigned ints *)
|
|
|
|
|
|
|
|
let output_code_string_counter = ref 0
|
|
|
|
|
|
|
|
let output_code_string outchan code =
|
|
|
|
let pos = ref 0 in
|
2014-04-29 04:56:17 -07:00
|
|
|
let len = Bytes.length code in
|
1996-11-07 02:56:52 -08:00
|
|
|
while !pos < len do
|
2014-04-29 04:56:17 -07:00
|
|
|
let c1 = Char.code(Bytes.get code !pos) in
|
|
|
|
let c2 = Char.code(Bytes.get code (!pos + 1)) in
|
|
|
|
let c3 = Char.code(Bytes.get code (!pos + 2)) in
|
|
|
|
let c4 = Char.code(Bytes.get code (!pos + 3)) in
|
1996-11-07 02:56:52 -08:00
|
|
|
pos := !pos + 4;
|
|
|
|
Printf.fprintf outchan "0x%02x%02x%02x%02x, " c4 c3 c2 c1;
|
|
|
|
incr output_code_string_counter;
|
|
|
|
if !output_code_string_counter >= 6 then begin
|
|
|
|
output_char outchan '\n';
|
|
|
|
output_code_string_counter := 0
|
|
|
|
end
|
|
|
|
done
|
|
|
|
|
|
|
|
(* Output a string as a C string *)
|
|
|
|
|
|
|
|
let output_data_string outchan data =
|
|
|
|
let counter = ref 0 in
|
|
|
|
for i = 0 to String.length data - 1 do
|
1998-06-01 07:53:28 -07:00
|
|
|
Printf.fprintf outchan "%d, " (Char.code(data.[i]));
|
1996-11-07 02:56:52 -08:00
|
|
|
incr counter;
|
1998-06-01 07:53:28 -07:00
|
|
|
if !counter >= 12 then begin
|
|
|
|
output_string outchan "\n";
|
1996-11-07 02:56:52 -08:00
|
|
|
counter := 0
|
|
|
|
end
|
1998-06-01 07:53:28 -07:00
|
|
|
done
|
1996-11-07 02:56:52 -08:00
|
|
|
|
2010-01-20 08:26:46 -08:00
|
|
|
(* Output a debug stub *)
|
|
|
|
|
|
|
|
let output_cds_file outfile =
|
|
|
|
Misc.remove_file outfile;
|
|
|
|
let outchan =
|
|
|
|
open_out_gen [Open_wronly; Open_trunc; Open_creat; Open_binary]
|
|
|
|
0o777 outfile in
|
|
|
|
try
|
|
|
|
Bytesections.init_record outchan;
|
|
|
|
(* The map of global identifiers *)
|
|
|
|
Symtable.output_global_map outchan;
|
|
|
|
Bytesections.record outchan "SYMB";
|
|
|
|
(* Debug info *)
|
|
|
|
output_debug_info outchan;
|
|
|
|
Bytesections.record outchan "DBUG";
|
|
|
|
(* The table of contents and the trailer *)
|
|
|
|
Bytesections.write_toc_and_trailer outchan;
|
|
|
|
close_out outchan
|
|
|
|
with x ->
|
|
|
|
close_out outchan;
|
|
|
|
remove_file outfile;
|
|
|
|
raise x
|
|
|
|
|
1996-11-07 02:56:52 -08:00
|
|
|
(* Output a bytecode executable as a C file *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let link_bytecode_as_c ppf tolink outfile =
|
1996-11-07 02:56:52 -08:00
|
|
|
let outchan = open_out outfile in
|
2010-01-20 08:26:46 -08:00
|
|
|
begin try
|
1996-11-07 02:56:52 -08:00
|
|
|
(* The bytecode *)
|
2004-02-22 07:07:51 -08:00
|
|
|
output_string outchan "\
|
2011-07-20 02:17:07 -07:00
|
|
|
#ifdef __cplusplus\
|
|
|
|
\nextern \"C\" {\
|
|
|
|
\n#endif\
|
|
|
|
\n#include <caml/mlvalues.h>\
|
|
|
|
\nCAMLextern void caml_startup_code(\
|
|
|
|
\n code_t code, asize_t code_size,\
|
|
|
|
\n char *data, asize_t data_size,\
|
|
|
|
\n char *section_table, asize_t section_table_size,\
|
|
|
|
\n char **argv);\n";
|
1996-11-07 02:56:52 -08:00
|
|
|
output_string outchan "static int caml_code[] = {\n";
|
|
|
|
Symtable.init();
|
2014-05-06 17:34:20 -07:00
|
|
|
clear_crc_interfaces ();
|
2010-01-20 08:26:46 -08:00
|
|
|
let currpos = ref 0 in
|
|
|
|
let output_fun code =
|
|
|
|
output_code_string outchan code;
|
2014-04-29 04:56:17 -07:00
|
|
|
currpos := !currpos + Bytes.length code
|
2010-01-20 08:26:46 -08:00
|
|
|
and currpos_fun () = !currpos in
|
2012-01-20 06:23:34 -08:00
|
|
|
List.iter (link_file ppf output_fun currpos_fun) tolink;
|
1996-11-07 02:56:52 -08:00
|
|
|
(* The final STOP instruction *)
|
|
|
|
Printf.fprintf outchan "\n0x%x};\n\n" Opcodes.opSTOP;
|
|
|
|
(* The table of global data *)
|
1998-06-01 07:53:28 -07:00
|
|
|
output_string outchan "static char caml_data[] = {\n";
|
1996-11-07 02:56:52 -08:00
|
|
|
output_data_string outchan
|
1997-07-02 11:16:15 -07:00
|
|
|
(Marshal.to_string (Symtable.initial_global_table()) []);
|
2004-02-22 07:07:51 -08:00
|
|
|
output_string outchan "\n};\n\n";
|
|
|
|
(* The sections *)
|
|
|
|
let sections =
|
|
|
|
[ "SYMB", Symtable.data_global_map();
|
|
|
|
"PRIM", Obj.repr(Symtable.data_primitive_names());
|
|
|
|
"CRCS", Obj.repr(extract_crc_interfaces()) ] in
|
|
|
|
output_string outchan "static char caml_sections[] = {\n";
|
|
|
|
output_data_string outchan
|
|
|
|
(Marshal.to_string sections []);
|
|
|
|
output_string outchan "\n};\n\n";
|
1996-11-07 02:56:52 -08:00
|
|
|
(* The table of primitives *)
|
1997-06-13 08:48:53 -07:00
|
|
|
Symtable.output_primitive_table outchan;
|
1996-11-07 02:56:52 -08:00
|
|
|
(* The entry point *)
|
2011-07-20 02:17:07 -07:00
|
|
|
output_string outchan "\
|
|
|
|
\nvoid caml_startup(char ** argv)\
|
|
|
|
\n{\
|
|
|
|
\n caml_startup_code(caml_code, sizeof(caml_code),\
|
|
|
|
\n caml_data, sizeof(caml_data),\
|
|
|
|
\n caml_sections, sizeof(caml_sections),\
|
|
|
|
\n argv);\
|
|
|
|
\n}\
|
|
|
|
\n#ifdef __cplusplus\
|
|
|
|
\n}\
|
|
|
|
\n#endif\n";
|
1996-11-07 02:56:52 -08:00
|
|
|
close_out outchan
|
|
|
|
with x ->
|
|
|
|
close_out outchan;
|
2012-04-16 08:26:25 -07:00
|
|
|
remove_file outfile;
|
1996-11-07 02:56:52 -08:00
|
|
|
raise x
|
2010-01-20 08:26:46 -08:00
|
|
|
end;
|
|
|
|
if !Clflags.debug then
|
|
|
|
output_cds_file ((Filename.chop_extension outfile) ^ ".cds")
|
1996-11-02 09:55:06 -08:00
|
|
|
|
1996-02-15 08:19:09 -08:00
|
|
|
(* Build a custom runtime *)
|
|
|
|
|
|
|
|
let build_custom_runtime prim_name exec_name =
|
2011-03-17 09:18:05 -07:00
|
|
|
let runtime_lib = "-lcamlrun" ^ !Clflags.runtime_variant in
|
2010-01-07 07:15:07 -08:00
|
|
|
Ccomp.call_linker Ccomp.Exe exec_name
|
2011-03-17 09:18:05 -07:00
|
|
|
([prim_name] @ List.rev !Clflags.ccobjs @ [runtime_lib])
|
2010-01-20 08:26:46 -08:00
|
|
|
(Clflags.std_include_flag "-I" ^ " " ^ Config.bytecomp_c_libraries)
|
1996-11-07 02:56:52 -08:00
|
|
|
|
|
|
|
let append_bytecode_and_cleanup bytecode_name exec_name prim_name =
|
1999-11-29 11:04:49 -08:00
|
|
|
let oc = open_out_gen [Open_wronly; Open_append; Open_binary] 0 exec_name in
|
|
|
|
let ic = open_in_bin bytecode_name in
|
|
|
|
copy_file ic oc;
|
|
|
|
close_in ic;
|
|
|
|
close_out oc;
|
|
|
|
remove_file bytecode_name;
|
|
|
|
remove_file prim_name
|
1996-02-15 08:19:09 -08:00
|
|
|
|
1997-10-24 08:51:36 -07:00
|
|
|
(* Fix the name of the output file, if the C compiler changes it behind
|
|
|
|
our back. *)
|
|
|
|
|
|
|
|
let fix_exec_name name =
|
|
|
|
match Sys.os_type with
|
2000-08-10 02:58:08 -07:00
|
|
|
"Win32" | "Cygwin" ->
|
1998-11-12 06:51:27 -08:00
|
|
|
if String.contains name '.' then name else name ^ ".exe"
|
1997-10-24 08:51:36 -07:00
|
|
|
| _ -> name
|
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
(* Main entry point (build a custom runtime if needed) *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let link ppf objfiles output_name =
|
2001-11-27 05:00:09 -08:00
|
|
|
let objfiles =
|
|
|
|
if !Clflags.nopervasives then objfiles
|
|
|
|
else if !Clflags.output_c_object then "stdlib.cma" :: objfiles
|
|
|
|
else "stdlib.cma" :: (objfiles @ ["std_exit.cmo"]) in
|
2000-03-09 01:12:28 -08:00
|
|
|
let tolink = List.fold_right scan_file objfiles [] in
|
2001-10-30 01:32:32 -08:00
|
|
|
Clflags.ccobjs := !Clflags.ccobjs @ !lib_ccobjs; (* put user's libs last *)
|
2013-09-04 08:12:37 -07:00
|
|
|
Clflags.all_ccopts := !lib_ccopts @ !Clflags.all_ccopts;
|
|
|
|
(* put user's opts first *)
|
2001-10-30 01:32:32 -08:00
|
|
|
Clflags.dllibs := !lib_dllibs @ !Clflags.dllibs; (* put user's DLLs first *)
|
1995-07-02 09:45:21 -07:00
|
|
|
if not !Clflags.custom_runtime then
|
2012-01-20 06:23:34 -08:00
|
|
|
link_bytecode ppf tolink output_name true
|
1996-11-07 02:56:52 -08:00
|
|
|
else if not !Clflags.output_c_object then begin
|
|
|
|
let bytecode_name = Filename.temp_file "camlcode" "" in
|
|
|
|
let prim_name = Filename.temp_file "camlprim" ".c" in
|
|
|
|
try
|
2012-01-20 06:23:34 -08:00
|
|
|
link_bytecode ppf tolink bytecode_name false;
|
1996-11-07 02:56:52 -08:00
|
|
|
let poc = open_out prim_name in
|
2010-01-20 08:26:46 -08:00
|
|
|
output_string poc "\
|
|
|
|
#ifdef __cplusplus\n\
|
|
|
|
extern \"C\" {\n\
|
|
|
|
#endif\n\
|
|
|
|
#ifdef _WIN64\n\
|
2011-12-21 08:31:01 -08:00
|
|
|
#ifdef __MINGW32__\n\
|
|
|
|
typedef long long value;\n\
|
|
|
|
#else\n\
|
2010-01-20 08:26:46 -08:00
|
|
|
typedef __int64 value;\n\
|
2011-12-21 08:31:01 -08:00
|
|
|
#endif\n\
|
2010-01-20 08:26:46 -08:00
|
|
|
#else\n\
|
|
|
|
typedef long value;\n\
|
|
|
|
#endif\n";
|
1997-06-13 08:48:53 -07:00
|
|
|
Symtable.output_primitive_table poc;
|
2010-01-20 08:26:46 -08:00
|
|
|
output_string poc "\
|
|
|
|
#ifdef __cplusplus\n\
|
|
|
|
}\n\
|
|
|
|
#endif\n";
|
1996-11-07 02:56:52 -08:00
|
|
|
close_out poc;
|
2002-06-11 07:15:12 -07:00
|
|
|
let exec_name = fix_exec_name output_name in
|
2007-11-15 05:21:15 -08:00
|
|
|
if not (build_custom_runtime prim_name exec_name)
|
1995-07-02 09:45:21 -07:00
|
|
|
then raise(Error Custom_runtime);
|
1998-04-14 07:48:34 -07:00
|
|
|
if !Clflags.make_runtime
|
|
|
|
then (remove_file bytecode_name; remove_file prim_name)
|
|
|
|
else append_bytecode_and_cleanup bytecode_name exec_name prim_name
|
1995-07-02 09:45:21 -07:00
|
|
|
with x ->
|
|
|
|
remove_file bytecode_name;
|
|
|
|
remove_file prim_name;
|
|
|
|
raise x
|
1996-11-07 02:56:52 -08:00
|
|
|
end else begin
|
2007-11-15 07:18:28 -08:00
|
|
|
let basename = Filename.chop_extension output_name in
|
|
|
|
let c_file = basename ^ ".c"
|
|
|
|
and obj_file = basename ^ Config.ext_obj in
|
1996-11-07 02:56:52 -08:00
|
|
|
if Sys.file_exists c_file then raise(Error(File_exists c_file));
|
2007-11-15 07:18:28 -08:00
|
|
|
let temps = ref [] in
|
1996-11-07 02:56:52 -08:00
|
|
|
try
|
2012-01-20 06:23:34 -08:00
|
|
|
link_bytecode_as_c ppf tolink c_file;
|
2007-11-15 07:18:28 -08:00
|
|
|
if not (Filename.check_suffix output_name ".c") then begin
|
|
|
|
temps := c_file :: !temps;
|
|
|
|
if Ccomp.compile_file c_file <> 0 then raise(Error Custom_runtime);
|
|
|
|
if not (Filename.check_suffix output_name Config.ext_obj) then begin
|
|
|
|
temps := obj_file :: !temps;
|
|
|
|
if not (
|
2011-03-17 09:18:05 -07:00
|
|
|
let runtime_lib = "-lcamlrun" ^ !Clflags.runtime_variant in
|
2007-11-15 07:18:28 -08:00
|
|
|
Ccomp.call_linker Ccomp.MainDll output_name
|
2011-03-17 09:18:05 -07:00
|
|
|
([obj_file] @ List.rev !Clflags.ccobjs @ [runtime_lib])
|
2007-11-15 07:18:28 -08:00
|
|
|
Config.bytecomp_c_libraries
|
|
|
|
) then raise (Error Custom_runtime);
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
List.iter remove_file !temps
|
1996-11-07 02:56:52 -08:00
|
|
|
with x ->
|
2007-11-15 07:18:28 -08:00
|
|
|
List.iter remove_file !temps;
|
1996-11-07 02:56:52 -08:00
|
|
|
raise x
|
1996-11-02 09:55:06 -08:00
|
|
|
end
|
1995-07-02 09:45:21 -07:00
|
|
|
|
|
|
|
(* Error report *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1995-07-02 09:45:21 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let report_error ppf = function
|
|
|
|
| File_not_found name ->
|
2012-03-07 09:32:37 -08:00
|
|
|
fprintf ppf "Cannot find file %a" Location.print_filename name
|
1995-07-02 09:45:21 -07:00
|
|
|
| Not_an_object_file name ->
|
2012-03-07 09:32:37 -08:00
|
|
|
fprintf ppf "The file %a is not a bytecode object file"
|
|
|
|
Location.print_filename name
|
2012-09-01 06:10:10 -07:00
|
|
|
| Wrong_object_name name ->
|
2013-03-09 14:38:52 -08:00
|
|
|
fprintf ppf "The output file %s has the wrong name. The extension implies\
|
|
|
|
\ an object file but the link step was requested" name
|
1995-07-02 09:45:21 -07:00
|
|
|
| Symbol_error(name, err) ->
|
2012-03-07 09:32:37 -08:00
|
|
|
fprintf ppf "Error while linking %a:@ %a" Location.print_filename name
|
1995-07-02 09:45:21 -07:00
|
|
|
Symtable.report_error err
|
|
|
|
| Inconsistent_import(intf, file1, file2) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
2012-03-07 09:32:37 -08:00
|
|
|
"@[<hov>Files %a@ and %a@ \
|
2000-02-08 12:00:06 -08:00
|
|
|
make inconsistent assumptions over interface %s@]"
|
2012-03-07 09:32:37 -08:00
|
|
|
Location.print_filename file1
|
|
|
|
Location.print_filename file2
|
|
|
|
intf
|
1995-07-02 09:45:21 -07:00
|
|
|
| Custom_runtime ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Error while building custom runtime system"
|
1996-11-07 02:56:52 -08:00
|
|
|
| File_exists file ->
|
2012-03-07 09:32:37 -08:00
|
|
|
fprintf ppf "Cannot overwrite existing file %a"
|
|
|
|
Location.print_filename file
|
2001-08-28 07:47:48 -07:00
|
|
|
| Cannot_open_dll file ->
|
2012-03-07 09:32:37 -08:00
|
|
|
fprintf ppf "Error on dynamically loaded library: %a"
|
|
|
|
Location.print_filename file
|
2013-04-18 04:58:59 -07:00
|
|
|
| Not_compatible_32 ->
|
|
|
|
fprintf ppf "Generated bytecode executable cannot be run\
|
|
|
|
\ on a 32-bit platform"
|
2013-09-12 07:34:13 -07:00
|
|
|
|
|
|
|
let () =
|
|
|
|
Location.register_error_of_exn
|
|
|
|
(function
|
|
|
|
| Error err -> Some (Location.error_of_printer_file report_error err)
|
|
|
|
| _ -> None
|
|
|
|
)
|
2014-05-09 05:01:21 -07:00
|
|
|
|
|
|
|
let reset () =
|
|
|
|
lib_ccobjs := [];
|
|
|
|
lib_ccopts := [];
|
|
|
|
lib_dllibs := [];
|
|
|
|
missing_globals := IdentSet.empty;
|
|
|
|
Consistbl.clear crc_interfaces;
|
|
|
|
implementations_defined := [];
|
|
|
|
debug_info := [];
|
|
|
|
output_code_string_counter := 0
|