Embed bytecode in C object when using -custom
authorStephane Glondu <steph@glondu.net>
Sat, 21 Jul 2012 13:40:52 +0000 (15:40 +0200)
committerMehdi Dogguy <mehdi@debian.org>
Wed, 21 Dec 2016 23:48:59 +0000 (23:48 +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 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

index 9c972a7d807914d500567a4b0742a620a49eef14..fcb96cf9733651f0ad1b466180a228380f66208a 100644 (file)
@@ -455,7 +455,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 *)
@@ -497,14 +497,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";
@@ -543,6 +556,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 =
@@ -557,6 +581,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
@@ -606,7 +640,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 d89c53263583981afeb0856b2e25b413798016f5..5e640ac9a7f0e6302ae9b44d437e1a2273d93068 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 088b021656356567d5045597e2be3e428014571b..f0c3d288b7ef6e754cd53200b1b2890d0e8f0387 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 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 f9b1c6f9c3ab3d6daf48b505b5ba6a2ab8010ed2..1e8377d7e13d9e7218fe11a36767ed331e199a79 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:
        @if ! $(SUPPORTS_SHARED_LIBRARIES); then \
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