@@ -2142,30 +2142,78 @@ let override_global =
21422142
21432143(* HACK END *)
21442144
2145- let seek_section toc ic name =
2146- let rec seek_sec curr_ofs = function
2147- | [] -> raise Not_found
2148- | (n , len ) :: rem ->
2149- if String. equal n name
2150- then (
2151- seek_in ic (curr_ofs - len);
2152- len)
2153- else seek_sec (curr_ofs - len) rem
2154- in
2155- seek_sec (in_channel_length ic - 16 - (8 * List. length toc)) toc
2156-
2157- let read_toc ic =
2158- let pos_trailer = in_channel_length ic - 16 in
2159- seek_in ic pos_trailer;
2160- let num_sections = input_binary_int ic in
2161- seek_in ic (pos_trailer - (8 * num_sections));
2162- let section_table = ref [] in
2163- for _i = 1 to num_sections do
2164- let name = really_input_string ic 4 in
2165- let len = input_binary_int ic in
2166- section_table := (name, len) :: ! section_table
2167- done ;
2168- ! section_table
2145+ module Toc : sig
2146+ type t
2147+
2148+ val read : in_channel -> t
2149+
2150+ val seek_section : t -> in_channel -> string -> int
2151+
2152+ val read_code : t -> in_channel -> string
2153+
2154+ val read_data : t -> in_channel -> Obj .t array
2155+
2156+ val read_crcs : t -> in_channel -> (string * Digest .t option ) list
2157+
2158+ val read_prim : t -> in_channel -> string
2159+
2160+ val read_symb : t -> in_channel -> Ocaml_compiler.Symtable.GlobalMap .t
2161+ end = struct
2162+ type t = (string * int ) list
2163+
2164+ let seek_section toc ic name =
2165+ let rec seek_sec curr_ofs = function
2166+ | [] -> raise Not_found
2167+ | (n , len ) :: rem ->
2168+ if String. equal n name
2169+ then (
2170+ seek_in ic (curr_ofs - len);
2171+ len)
2172+ else seek_sec (curr_ofs - len) rem
2173+ in
2174+ seek_sec (in_channel_length ic - 16 - (8 * List. length toc)) toc
2175+
2176+ let read ic =
2177+ let pos_trailer = in_channel_length ic - 16 in
2178+ seek_in ic pos_trailer;
2179+ let num_sections = input_binary_int ic in
2180+ seek_in ic (pos_trailer - (8 * num_sections));
2181+ let section_table = ref [] in
2182+ for _i = 1 to num_sections do
2183+ let name = really_input_string ic 4 in
2184+ let len = input_binary_int ic in
2185+ section_table := (name, len) :: ! section_table
2186+ done ;
2187+ ! section_table
2188+
2189+ let read_code toc ic =
2190+ let code_size = seek_section toc ic " CODE" in
2191+ really_input_string ic code_size
2192+
2193+ let read_data toc ic =
2194+ ignore (seek_section toc ic " DATA" );
2195+ let init_data : Obj.t array = input_value ic in
2196+ init_data
2197+
2198+ let read_symb toc ic =
2199+ ignore (seek_section toc ic " SYMB" );
2200+ let orig_symbols : Ocaml_compiler.Symtable.GlobalMap.t = input_value ic in
2201+ orig_symbols
2202+
2203+ let read_crcs toc ic =
2204+ ignore (seek_section toc ic " CRCS" );
2205+ let orig_crcs : (string * Digest.t option) list = input_value ic in
2206+ orig_crcs
2207+
2208+ let read_prim toc ic =
2209+ let prim_size = seek_section toc ic " PRIM" in
2210+ let prim = really_input_string ic prim_size in
2211+ prim
2212+ end
2213+
2214+ let read_primitives toc ic =
2215+ let prim = Toc. read_prim toc ic in
2216+ String. split_char ~sep: '\000' prim
21692217
21702218let from_exe
21712219 ?(includes = [] )
@@ -2175,19 +2223,14 @@ let from_exe
21752223 ?(debug = false )
21762224 ic =
21772225 let debug_data = Debug. create ~toplevel debug in
2178- let toc = read_toc ic in
2179- let prim_size = seek_section toc ic " PRIM" in
2180- let prim = really_input_string ic prim_size in
2181- let primitive_table = Array. of_list (String. split_char ~sep: '\000' prim) in
2182- let code_size = seek_section toc ic " CODE" in
2183- let code = really_input_string ic code_size in
2184- ignore (seek_section toc ic " DATA" );
2185- let init_data : Obj.t array = input_value ic in
2226+ let toc = Toc. read ic in
2227+ let primitives = read_primitives toc ic in
2228+ let primitive_table = Array. of_list primitives in
2229+ let code = Toc. read_code toc ic in
2230+ let init_data = Toc. read_data toc ic in
21862231 let init_data = Array. map ~f: Constants. parse init_data in
2187- ignore (seek_section toc ic " SYMB" );
2188- let orig_symbols : Ocaml_compiler.Symtable.GlobalMap.t = input_value ic in
2189- ignore (seek_section toc ic " CRCS" );
2190- let orig_crcs : (string * Digest.t option) list = input_value ic in
2232+ let orig_symbols = Toc. read_symb toc ic in
2233+ let orig_crcs = Toc. read_crcs toc ic in
21912234 let keeps =
21922235 let t = Hashtbl. create 17 in
21932236 List. iter ~f: (fun (_ , s ) -> Hashtbl. add t s () ) predefined_exceptions;
@@ -2213,7 +2256,7 @@ let from_exe
22132256 then ()
22142257 else
22152258 try
2216- ignore (seek_section toc ic " DBUG" );
2259+ ignore (Toc. seek_section toc ic " DBUG" );
22172260 Debug. read debug_data ~crcs ~includes ic
22182261 with Not_found ->
22192262 if Debug. enabled debug_data || Debug. toplevel debug_data
@@ -2268,7 +2311,10 @@ let from_exe
22682311 then
22692312 (* Include linking information *)
22702313 let toc =
2271- [ " SYMB" , Obj. repr symbols; " CRCS" , Obj. repr crcs; " PRIM" , Obj. repr prim ]
2314+ [ " SYMB" , Obj. repr symbols
2315+ ; " CRCS" , Obj. repr crcs
2316+ ; " PRIM" , Obj. repr (String. concat ~sep: " \000 " primitives)
2317+ ]
22722318 in
22732319 let gdata = Var. fresh () in
22742320 let infos =
0 commit comments