Embed bytecode in C object when using -custom
authorStephane Glondu <steph@glondu.net>
Tue, 11 Jul 2017 11:27:34 +0000 (13:27 +0200)
committerStéphane Glondu <glondu@debian.org>
Tue, 6 Aug 2019 07:27:23 +0000 (09:27 +0200)
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 0006-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
testsuite/tests/regression/pr3612/Makefile
testsuite/tests/runtime-C-exceptions/Makefile

index 40a33517d10c9847ad4036089031f9c1ecc72ded..ae887c3cb89590e35bc05653385425dece699fc1 100644 (file)
@@ -465,7 +465,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 *)
@@ -507,7 +507,18 @@ 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),\
@@ -521,7 +532,9 @@ let link_bytecode_as_c ppf tolink outfile =
 \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";
@@ -560,6 +573,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 =
@@ -582,6 +606,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
@@ -631,7 +665,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
index b387d6ec24d2f13babe2b78f6049344ec4da143b..e75ba7d14792d1d0ca22343b06a2bb9358938e4a 100644 (file)
@@ -18,5 +18,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 d6615a1c49cc9233c9bcb7a519679274fdae1d68..8e1b4cb1040f838c9478c9a3cc2cd6464012e0f1 100644 (file)
@@ -19,6 +19,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 679c5b9dfbefa36c1877fa3225541266fb6d7be8..4d38cd8ac3e014cc5bfcd8e49d35ebe008ffeaa0 100644 (file)
 BASEDIR=../..
 
 .PHONY: default
+
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 default:
        @$(MAKE) compile
        @$(MAKE) run
index c8e24ccee0761e8c7854c15a8d718ffc8cce6acd..7057e12ce1aaa9bf6da8f41cf0b78f70b328800f 100644 (file)
@@ -19,5 +19,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 1e56b168296361b7bc2be0942d37cbf672414f5f..62a0034bca8525093ffc7e79ef7352ea00f5b8d7 100644 (file)
@@ -18,6 +18,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:
        @if ! $(SUPPORTS_SHARED_LIBRARIES); then \
index a79f6bdda934727ea4b45eebac01e2a0079817d4..d30d409a6810e1a4d98f613765832b66d77dfff7 100644 (file)
@@ -18,5 +18,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
index 866927b39aefd93c47d56b4a9d21eedfecf4569e..6d0b361f14889045fc0e62ca1ea1d210c9ce6c3d 100644 (file)
@@ -16,6 +16,9 @@
 MAIN_MODULE=pr3612
 C_FILES=custom_finalize
 
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 BASEDIR=../../..
 include $(BASEDIR)/makefiles/Makefile.one
 include $(BASEDIR)/makefiles/Makefile.common
index da534b756bc7393d27e7c152819e58c5a9d70ec7..e9598df57d9bca00a3ee94392b160a91465dce8e 100644 (file)
@@ -3,5 +3,8 @@ BASEDIR=../..
 MAIN_MODULE=test
 C_FILES=stub_test
 
+# This test relies on the upstream behaviour of -custom
+export OCAML_CUSTOM_EMBED=n
+
 include $(BASEDIR)/makefiles/Makefile.one
 include $(BASEDIR)/makefiles/Makefile.common