Embed bytecode in C object when using -custom
authorStephane Glondu <steph@glondu.net>
Sat, 21 Jul 2012 13:40:52 +0000 (15:40 +0200)
committerStéphane Glondu <glondu@debian.org>
Thu, 30 Oct 2014 08:28:06 +0000 (08:28 +0000)
This patch fixes non-strippability of bytecode executables linked with
custom runtime. The new behaviour is enabled when OCAML_CUSTOM_EMBED
is set to "y", or when DEB_HOST_ARCH is non-empty.

Forwarded: not-needed
Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=256900
Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=627761
Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=678577
Signed-off-by: Stephane Glondu <steph@glondu.net>
Gbp-Pq: Name 0008-Embed-bytecode-in-C-object-when-using-custom.patch

bytecomp/bytelink.ml
testsuite/tests/basic-manyargs/Makefile
testsuite/tests/callback/Makefile
testsuite/tests/embedded/Makefile
testsuite/tests/gc-roots/Makefile
testsuite/tests/lib-dynlink-bytecode/Makefile
testsuite/tests/lib-marshal/Makefile

index 87a58ab77c9710cf2f10e736b918b42c1abb7641..72678fa646977cce7c5e69503e9609c2b501b2d5 100644 (file)
@@ -436,7 +436,7 @@ let mlvalues_primitives = [
 
 (* Output a bytecode executable as a C file *)
 
-let link_bytecode_as_c ppf tolink outfile =
+let link_bytecode_as_c ppf tolink outfile with_main =
   let outchan = open_out outfile in
   begin try
     (* The bytecode *)
@@ -478,14 +478,27 @@ let link_bytecode_as_c ppf tolink outfile =
     (* The table of primitives *)
     Symtable.output_primitive_table outchan mlvalues_primitives;
     (* The entry point *)
-    output_string outchan "\
+    if with_main then begin
+      output_string outchan "\
+\nint main(int argc, 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  return 0; /* not reached */\
+\n}\n"
+    end else begin
+      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}\n"
+    end;
+    output_string outchan "\
 \n#ifdef __cplusplus\
 \n}\
 \n#endif\n";
@@ -524,6 +537,17 @@ let fix_exec_name name =
       if String.contains name '.' then name else name ^ ".exe"
   | _ -> name
 
+(* Debian-specific -custom behaviour:
+   - if DEB_HOST_ARCH is non-empty, it is activated by default
+   - can be enabled/disabled by setting OCAML_CUSTOM_EMBED to y/n
+*)
+
+let custom_embed =
+  try Sys.getenv "OCAML_CUSTOM_EMBED" = "y"
+  with Not_found ->
+    try Sys.getenv "DEB_HOST_ARCH" <> ""
+    with Not_found -> false
+
 (* Main entry point (build a custom runtime if needed) *)
 
 let link ppf objfiles output_name =
@@ -538,6 +562,16 @@ let link ppf objfiles output_name =
   Clflags.dllibs := !lib_dllibs @ !Clflags.dllibs; (* put user's DLLs first *)
   if not !Clflags.custom_runtime then
     link_bytecode ppf tolink output_name true
+  else if custom_embed && not !Clflags.output_c_object && not !Clflags.make_runtime then
+    let c_file = Filename.temp_file "camlobj" ".c" in
+    try
+      link_bytecode_as_c ppf tolink c_file true;
+      let exec_name = fix_exec_name output_name in
+      if not (build_custom_runtime c_file exec_name)
+      then raise(Error Custom_runtime);
+    with x ->
+      remove_file c_file;
+      raise x
   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
@@ -580,7 +614,7 @@ let link ppf objfiles output_name =
     if Sys.file_exists c_file then raise(Error(File_exists c_file));
     let temps = ref [] in
     try
-      link_bytecode_as_c ppf tolink c_file;
+      link_bytecode_as_c ppf tolink c_file false;
       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);
index 3cf4a15e8046bedf560c03802bbda7375b26fb1d..d1ad48c8062408796fbdedb5e289f1076be76efe 100644 (file)
@@ -15,5 +15,8 @@ BASEDIR=../..
 MAIN_MODULE=manyargs
 C_FILES=manyargsprim
 
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 include $(BASEDIR)/makefiles/Makefile.one
 include $(BASEDIR)/makefiles/Makefile.common
index 26d02ea8abb08c89ac4b38f6d5bd5b84de9881ef..2069815521efffa2a3d619c06f88f2051e81a50d 100644 (file)
@@ -16,6 +16,9 @@ CC=$(NATIVECC) -I $(CTOPDIR)/byterun
 COMPFLAGS=-I $(OTOPDIR)/otherlibs/unix
 LD_PATH=$(TOPDIR)/otherlibs/unix
 
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 .PHONY: default
 default:
        @case " $(OTHERLIBRARIES) " in \
index 2a01c20848bd57442bc5d8aa4b1a7bb503f71aca..dfd999cc9a03a9efd91d3573555196eaf5a356f2 100644 (file)
 BASEDIR=../..
 
 .PHONY: default
+
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 default: compile run
 
 .PHONY: compile
index a10895381fb21000553bb0246486b3d7446fa0ef..9c1ff434a6b9ee05243928b9589dac389aaaf718 100644 (file)
@@ -16,5 +16,8 @@ MAIN_MODULE=globroots
 C_FILES=globrootsprim
 ADD_COMPFLAGS=-w a
 
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 include $(BASEDIR)/makefiles/Makefile.one
 include $(BASEDIR)/makefiles/Makefile.common
index 74f27b9f522de11665ad773fade88459bc4a321c..14cb81615c6a8a4d656ebb5ffecfbe360a16b158 100644 (file)
@@ -15,6 +15,9 @@ BASEDIR=../..
 COMPFLAGS=-I $(OTOPDIR)/otherlibs/dynlink
 LD_PATH=.:$(TOPDIR)/otherlibs/dynlink
 
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 .PHONY: default
 default:
        @$(SET_LD_PATH) $(MAKE) compile run
index 34b67dc811642c4ae5a6e890c27d8d9e400460e1..e8928e68f983a52371c9b101fdebfbdec6d53a81 100644 (file)
@@ -15,5 +15,8 @@ BASEDIR=../..
 MAIN_MODULE=intext
 C_FILES=intextaux
 
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 include $(BASEDIR)/makefiles/Makefile.one
 include $(BASEDIR)/makefiles/Makefile.common