Use linker script to control visibility of symbols.
authorRichard Jones <rjones@redhat.com>
Mon, 4 Jan 2010 15:39:49 +0000 (15:39 +0000)
committerRichard Jones <rjones@redhat.com>
Mon, 4 Jan 2010 16:23:36 +0000 (16:23 +0000)
.gitignore
configure.ac
src/Makefile.am
src/generator.ml

index 37fb73e..8fc9721 100644 (file)
@@ -219,6 +219,7 @@ src/guestfs_protocol.h
 src/guestfs_protocol.x
 src/guestfs-structs.h
 src/.libs/libguestfs.so
+src/libguestfs.syms
 src/.pod2text.data
 src/stamp-generator
 stamp-h1
index 9a781c7..d7ad7c7 100644 (file)
@@ -116,6 +116,12 @@ test "x$U" != "x" && AC_MSG_ERROR([Compiler not ANSI compliant])
 
 AM_PROG_CC_C_O
 
+dnl Work out how to specify the linker script to the linker.
+VERSION_SCRIPT_FLAGS=-Wl,--version-script=
+`/usr/bin/ld --help 2>&1 | grep -- --version-script >/dev/null` || \
+    VERSION_SCRIPT_FLAGS="-Wl,-M -Wl,"
+AC_SUBST(VERSION_SCRIPT_FLAGS)
+
 dnl Check support for 64 bit file offsets.
 AC_SYS_LARGEFILE
 
index 619f46e..fb37167 100644 (file)
@@ -26,7 +26,8 @@ generator_built = \
        guestfs-bindtests.c \
        guestfs-actions.pod \
        guestfs-availability.pod \
-       guestfs-structs.pod
+       guestfs-structs.pod \
+       libguestfs.syms
 
 $(generator_built): stamp-generator
 
@@ -108,8 +109,10 @@ libprotocol_la_CFLAGS =
 #
 # Note that this scheme means the real library version will always be
 # 'libguestfs.so.0.$(MAX_PROC_NR).0'.
-
 libguestfs_la_LDFLAGS = -version-info $(MAX_PROC_NR):0:$(MAX_PROC_NR)
+
+libguestfs_la_LDFLAGS += $(VERSION_SCRIPT_FLAGS)libguestfs.syms
+
 libguestfs_la_SOURCES = \
        guestfs.c \
        guestfs.h \
@@ -118,7 +121,8 @@ libguestfs_la_SOURCES = \
        guestfs-bindtests.c \
        guestfs-internal.h \
        guestfs_protocol.h \
-       gettext.h
+       gettext.h \
+       libguestfs.syms
 
 libguestfs_la_LIBADD = $(LTLIBTHREAD)
 
index ebfd45d..4bf4b0f 100755 (executable)
@@ -5607,6 +5607,51 @@ and generate_daemon_actions_h () =
         name style;
   ) daemon_functions
 
+(* Generate the linker script which controls the visibility of
+ * symbols in the public ABI and ensures no other symbols get
+ * exported accidentally.
+ *)
+and generate_linker_script () =
+  generate_header HashStyle GPLv2plus;
+
+  let globals = [
+    "guestfs_create";
+    "guestfs_close";
+    "guestfs_get_error_handler";
+    "guestfs_get_out_of_memory_handler";
+    "guestfs_last_error";
+    "guestfs_set_error_handler";
+    "guestfs_set_launch_done_callback";
+    "guestfs_set_log_message_callback";
+    "guestfs_set_out_of_memory_handler";
+    "guestfs_set_subprocess_quit_callback";
+
+    (* Unofficial parts of the API: the bindings code use these
+     * functions, so it is useful to export them.
+     *)
+    "guestfs_safe_calloc";
+    "guestfs_safe_malloc";
+  ] in
+  let functions =
+    List.map (fun (name, _, _, _, _, _, _) -> "guestfs_" ^ name)
+      all_functions in
+  let structs =
+    List.concat (
+      List.map (fun (typ, _) ->
+                  ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
+        structs
+    ) in
+  let globals = List.sort compare (globals @ functions @ structs) in
+
+  pr "{\n";
+  pr "    global:\n";
+  List.iter (pr "        %s;\n") globals;
+  pr "\n";
+
+  pr "    local:\n";
+  pr "        *;\n";
+  pr "};\n"
+
 (* Generate the server-side stubs. *)
 and generate_daemon_actions () =
   generate_header CStyle GPLv2plus;
@@ -11141,6 +11186,7 @@ Run it from the top source directory using the command
   output_to "src/guestfs-actions.pod" generate_actions_pod;
   output_to "src/guestfs-availability.pod" generate_availability_pod;
   output_to "src/MAX_PROC_NR" generate_max_proc_nr;
+  output_to "src/libguestfs.syms" generate_linker_script;
   output_to "daemon/actions.h" generate_daemon_actions_h;
   output_to "daemon/stubs.c" generate_daemon_actions;
   output_to "daemon/names.c" generate_daemon_names;