When building packages that contain shared libraries using libtool,
each compilation unit is compiled twice: to FILE.o as a static
object file, and to .libs/FILE.o for inclusion into a shared library.
On Windows, the latter file is compiled with options -DDLL_EXPORT -DPIC;
libtool.m4 arranges it this way.

Specifically with the MSVC compiler, the .obj files record the symbols
to be exported in a section names '.drectve'. In this case, in FILE.obj,
it is not useful to export most symbols, since the .obj file will not
end up in the particular DLL. The only exceptions are
  - variables, which are declared with __declspec(dllimport) in the
    .h file, do need to be exported, otherwise a crash at runtime is
    is the result,
  - functions that define application-wide locks, such as
    setlocale-lock.c or nl_langinfo-lock.c, are better kept exported,
    so as to minimize the risk that two different lock instances exist
    in the same process.

This patch implements it for the symbol defined by relocatable.c.


2023-09-05  Bruno Haible  <[email protected]>

        relocatable-lib-lgpl: Don't export symbols from static MSVC .obj files.
        Reported by Dmitry Bely <[email protected]> in
        
<https://lists.gnu.org/archive/html/bug-gnu-libiconv/2023-08/msg00002.html>.
        * lib/relocatable.h (RELOCATABLE_DLL_EXPORTED): Don't use
        __declspec(dllexport) when creating static .obj files with MSVC.

diff --git a/lib/relocatable.h b/lib/relocatable.h
index f4634b1238..fcbbb21934 100644
--- a/lib/relocatable.h
+++ b/lib/relocatable.h
@@ -39,7 +39,20 @@ extern "C" {
 #if HAVE_VISIBILITY && BUILDING_DLL
 # define RELOCATABLE_DLL_EXPORTED __attribute__((__visibility__("default")))
 #elif defined _MSC_VER && BUILDING_DLL
-# define RELOCATABLE_DLL_EXPORTED __declspec(dllexport)
+/* When building with MSVC, exporting a symbol means that the object file
+   contains a "linker directive" of the form /EXPORT:symbol.  This can be
+   inspected through the "objdump -s --section=.drectve FILE" or
+   "dumpbin /directives FILE" commands.
+   The symbols from this file should be exported if and only if the object
+   file gets included in a DLL.  Libtool, on Windows platforms, defines
+   the C macro DLL_EXPORT (together with PIC) when compiling for a DLL
+   and does not define it when compiling an object file meant to be linked
+   statically into some executable.  */
+# if defined DLL_EXPORT
+#  define RELOCATABLE_DLL_EXPORTED __declspec(dllexport)
+# else
+#  define RELOCATABLE_DLL_EXPORTED
+# endif
 #else
 # define RELOCATABLE_DLL_EXPORTED
 #endif




Reply via email to