[RFC][PATCH] Building gmp with -fvisibility=hidden

Yuri Gribov tetra2005 at gmail.com
Sat May 12 19:05:17 UTC 2018


Hi all,

I've sent this mail to gmp-discuss but didn't get any reply so I'll
dup here just in case.

Is there any interest in building GMP with -fvisility=hidden on Linux?
I gave it a try recently and got few symbols removed:
  __gmp_asprintf_funs
  __gmp_fac2cnt_table
  __gmp_fprintf_funs
  __gmp_fscanf_funs
  __gmp_limbroots_table
  __gmp_mt_recalc_buffer
  __gmpn_divexact_by3
  __gmpn_divmod_1
  __gmpn_gcdext_hook
  __gmp_obstack_printf_funs
  __gmp_odd2fac_table
  __gmp_oddfac_table
  __gmp_randclear_mt
  __gmp_randget_mt
  __gmp_randiset_mt
  __gmp_snprintf_funs
  __gmp_sprintf_funs
  __gmp_sscanf_funs
  __gmpz_legendre

.text size didn't change which means that hiding those didn't allow
for any new optimizations. Dynamic linker sections (.plt, .got,
.rela.dyn, etc.) decreased so maybe applications startup will be
slightly faster.

I attached draft patch (passes `make check').

Best regards,
Yury Gribov
-------------- next part --------------
diff -r 765c2c27523b configure.ac
--- a/configure.ac	Wed Apr 25 07:38:14 2018 +0200
+++ b/configure.ac	Thu Apr 26 22:33:31 2018 +0100
@@ -2596,10 +2596,21 @@
   [*-*-aix[34]*])
     if test -z "$enable_shared"; then enable_shared=no; fi ;;
 esac
 
 
+# Visibility.
+OLDCFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS -fvisibility=hidden"
+AC_MSG_CHECKING(whether compiler supports visibility annotations)
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([[void __attribute__((visibility("default"))) foo() {}]])],
+                  [CFLAGS="$CFLAGS -fvisibility=hidden -D__GMP_ENABLE_VISIBILITY=1";
+                   AC_MSG_RESULT(yes)],
+                  [CFLAGS="$OLDCFLAGS -D__GMP_ENABLE_VISIBILITY=0"
+                   AC_MSG_RESULT(no)])
+
+
 # Configs for Windows DLLs.
 
 AC_LIBTOOL_WIN32_DLL
 
 AC_SUBST(LIBGMP_DLL,0)
diff -r 765c2c27523b gmp-h.in
--- a/gmp-h.in	Wed Apr 25 07:38:14 2018 +0200
+++ b/gmp-h.in	Thu Apr 26 22:33:31 2018 +0100
@@ -98,13 +98,16 @@
    In Borland C _export still exists, but needs to go after the type, like
    "void _export foo();".  Would have to change the __GMP_DECLSPEC syntax to
    make use of that.  Probably more trouble than it's worth.  */
 
 #if defined (__GNUC__)
-#define __GMP_DECLSPEC_EXPORT  __declspec(__dllexport__)
+#ifdef __GMP_ENABLE_VISIBILITY
+#define __GMP_DECLSPEC_EXPORT  __attribute__((visibility("default")))
+#else
 #define __GMP_DECLSPEC_IMPORT  __declspec(__dllimport__)
 #endif
+#endif
 #if defined (_MSC_VER) || defined (__BORLANDC__)
 #define __GMP_DECLSPEC_EXPORT  __declspec(dllexport)
 #define __GMP_DECLSPEC_IMPORT  __declspec(dllimport)
 #endif
 #ifdef __WATCOMC__
@@ -115,20 +118,29 @@
 #define __GMP_DECLSPEC_EXPORT  _Export
 #define __GMP_DECLSPEC_IMPORT  _Import
 #endif
 
 #if __GMP_LIBGMP_DLL
-#ifdef __GMP_WITHIN_GMP
-/* compiling to go into a DLL libgmp */
-#define __GMP_DECLSPEC  __GMP_DECLSPEC_EXPORT
+#  ifdef __GMP_WITHIN_GMP
+  /* compiling to go into a DLL libgmp */
+#    define __GMP_DECLSPEC  __GMP_DECLSPEC_EXPORT
+#  else
+  /* compiling to go into an application which will link to a DLL libgmp */
+#  define __GMP_DECLSPEC  __GMP_DECLSPEC_IMPORT
+#  endif
+#elif __GMP_ENABLE_VISIBILITY
+  /* compilers that support GNU visibility */
+#  ifdef __GMP_WITHIN_GMP
+    /* compiling to go into a DLL libgmp */
+#    define __GMP_DECLSPEC  __GMP_DECLSPEC_EXPORT
+#  else
+    /* compiling to go into an application which will link to a DLL libgmp */
+#    define __GMP_DECLSPEC
+#  endif
 #else
-/* compiling to go into an application which will link to a DLL libgmp */
-#define __GMP_DECLSPEC  __GMP_DECLSPEC_IMPORT
-#endif
-#else
-/* all other cases */
-#define __GMP_DECLSPEC
+  /* all other cases */
+#  define __GMP_DECLSPEC
 #endif
 
 
 #ifdef __GMP_SHORT_LIMB
 typedef unsigned int		mp_limb_t;
@@ -228,20 +240,29 @@
 typedef const __mpq_struct *mpq_srcptr;
 typedef __mpq_struct *mpq_ptr;
 
 
 #if __GMP_LIBGMP_DLL
-#ifdef __GMP_WITHIN_GMPXX
-/* compiling to go into a DLL libgmpxx */
-#define __GMP_DECLSPEC_XX  __GMP_DECLSPEC_EXPORT
+#  ifdef __GMP_WITHIN_GMPXX
+    /* compiling to go into a DLL libgmpxx */
+#    define __GMP_DECLSPEC_XX  __GMP_DECLSPEC_EXPORT
+#  else
+    /* compiling to go into a application which will link to a DLL libgmpxx */
+#    define __GMP_DECLSPEC_XX  __GMP_DECLSPEC_IMPORT
+#  endif
+#elif __GMP_ENABLE_VISIBILITY
+  /* compilers that support GNU visibility */
+#  ifdef __GMP_WITHIN_GMPXX
+    /* compiling to go into a DLL libgmpxx */
+#    define __GMP_DECLSPEC_XX  __GMP_DECLSPEC_EXPORT
+#  else
+    /* compiling to go into a application which will link to a DLL libgmpxx */
+#    define __GMP_DECLSPEC_XX
+#  endif
 #else
-/* compiling to go into a application which will link to a DLL libgmpxx */
-#define __GMP_DECLSPEC_XX  __GMP_DECLSPEC_IMPORT
-#endif
-#else
-/* all other cases */
-#define __GMP_DECLSPEC_XX
+  /* all other cases */
+#  define __GMP_DECLSPEC_XX
 #endif
 
 
 #ifndef __MPN
 #define __MPN(x) __gmpn_##x


More information about the gmp-devel mailing list