Skip to content

Commit e432a5f

Browse files
committed
build: add option for reducing exports (v2)
This was committed previously as 4975ae1 and reverted, because the flags were applied even if the checks didn't pass. This is the same commit, fixed up to actually disable the functionality when necessary. Enabled automatically if boost >= 1.49. See: https://svn.boost.org/trac/boost/ticket/2309 Also, check for a default visibility attribute, so that we can mark future api functions correctly.
1 parent a4f151f commit e432a5f

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

configure.ac

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ AC_ARG_ENABLE([hardening],
9090
[use_hardening=$enableval],
9191
[use_hardening=yes])
9292

93+
AC_ARG_ENABLE([reduce-exports],
94+
[AS_HELP_STRING([--enable-reduce-exports],
95+
[attempt to reduce exported symbols in the resulting executables (default is yes)])],
96+
[use_reduce_exports=$enableval],
97+
[use_reduce_exports=auto])
98+
9399
AC_ARG_ENABLE([ccache],
94100
[AS_HELP_STRING([--enable-ccache],
95101
[use ccache for building (default is yes if ccache is found)])],
@@ -396,6 +402,36 @@ AC_TRY_COMPILE([#include <sys/socket.h>],
396402

397403
AC_SEARCH_LIBS([clock_gettime],[rt])
398404

405+
AC_MSG_CHECKING([for visibility attribute])
406+
AC_LINK_IFELSE([AC_LANG_SOURCE([
407+
int foo_def( void ) __attribute__((visibility("default")));
408+
int main(){}
409+
])],
410+
[
411+
AC_DEFINE(HAVE_VISIBILITY_ATTRIBUTE,1,[Define if the visibility attribute is supported.])
412+
AC_MSG_RESULT(yes)
413+
],
414+
[
415+
AC_MSG_RESULT(no)
416+
if test x$use_reduce_exports = xyes; then
417+
AC_MSG_ERROR([Cannot find a working visibility attribute. Use --disable-reduced-exports.])
418+
fi
419+
AC_MSG_WARN([Cannot find a working visibility attribute. Disabling reduced exports.])
420+
use_reduce_exports=no
421+
]
422+
)
423+
424+
if test x$use_reduce_exports != xno; then
425+
AX_CHECK_COMPILE_FLAG([-fvisibility=hidden],[RE_CXXFLAGS="-fvisibility=hidden"],
426+
[
427+
if test x$use_reduce_exports = xyes; then
428+
AC_MSG_ERROR([Cannot set default symbol visibility. Use --disable-reduced-exports.])
429+
fi
430+
AC_MSG_WARN([Cannot set default symbol visibility. Disabling reduced exports.])
431+
use_reduce_exports=no
432+
])
433+
fi
434+
399435
LEVELDB_CPPFLAGS=
400436
LIBLEVELDB=
401437
LIBMEMENV=
@@ -426,6 +462,40 @@ AX_BOOST_PROGRAM_OPTIONS
426462
AX_BOOST_THREAD
427463
AX_BOOST_CHRONO
428464

465+
466+
if test x$use_reduce_exports != xno; then
467+
AC_MSG_CHECKING([for working boost reduced exports])
468+
TEMP_CPPFLAGS="$CPPFLAGS"
469+
CPPFLAGS="$BOOST_CPPFLAGS $CPPFLAGS"
470+
AC_PREPROC_IFELSE([AC_LANG_PROGRAM([[
471+
@%:@include <boost/version.hpp>
472+
]], [[
473+
#if BOOST_VERSION >= 104900
474+
// Everything is okay
475+
#else
476+
# error Boost version is too old
477+
#endif
478+
]])],[
479+
AC_MSG_RESULT(yes)
480+
],[:
481+
if test x$use_reduce_exports = xauto; then
482+
use_reduce_exports=no
483+
else
484+
if test x$use_reduce_exports = xyes; then
485+
AC_MSG_ERROR([boost versions < 1.49 are known to be broken with reduced exports. Use --disable-reduced-exports.])
486+
fi
487+
fi
488+
AC_MSG_RESULT(no)
489+
AC_MSG_WARN([boost versions < 1.49 are known to have symbol visibility issues. Disabling reduced exports.])
490+
])
491+
CPPFLAGS="$TEMP_CPPFLAGS"
492+
fi
493+
494+
if test x$use_reduce_exports != xno; then
495+
CXXFLAGS="$CXXFLAGS $RE_CXXFLAGS"
496+
AX_CHECK_LINK_FLAG([[-Wl,--exclude-libs,ALL]], [RELDFLAGS="-Wl,--exclude-libs,ALL"])
497+
fi
498+
429499
if test x$use_tests = xyes; then
430500

431501
if test x$HEXDUMP = x; then
@@ -672,6 +742,13 @@ else
672742
AC_MSG_RESULT([no])
673743
fi
674744

745+
AC_MSG_CHECKING([whether to reduce exports])
746+
if test x$use_reduce_exports != xno; then
747+
AC_MSG_RESULT([yes])
748+
else
749+
AC_MSG_RESULT([no])
750+
fi
751+
675752
if test x$build_bitcoin_utils$build_bitcoind$bitcoin_enable_qt$use_tests = xnononono; then
676753
AC_MSG_ERROR([No targets! Please specify at least one of: --with-utils --with-daemon --with-gui or --enable-tests])
677754
fi
@@ -704,6 +781,7 @@ AC_SUBST(CLIENT_VERSION_IS_RELEASE, _CLIENT_VERSION_IS_RELEASE)
704781
AC_SUBST(COPYRIGHT_YEAR, _COPYRIGHT_YEAR)
705782

706783

784+
AC_SUBST(RELDFLAGS)
707785
AC_SUBST(LIBTOOL_LDFLAGS)
708786
AC_SUBST(USE_UPNP)
709787
AC_SUBST(USE_QRCODE)

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ endif
261261

262262
bitcoind_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS)
263263
bitcoind_CPPFLAGS = $(BITCOIN_INCLUDES)
264+
bitcoind_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS)
264265

265266
# bitcoin-cli binary #
266267
bitcoin_cli_LDADD = \
@@ -298,10 +299,12 @@ endif
298299
bitcoin_tx_SOURCES = bitcoin-tx.cpp
299300
bitcoin_tx_CPPFLAGS = $(BITCOIN_INCLUDES)
300301
#
302+
bitcoin_tx_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS)
301303

302304
if TARGET_WINDOWS
303305
bitcoin_cli_SOURCES += bitcoin-cli-res.rc
304306
endif
307+
bitcoin_cli_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS)
305308

306309
CLEANFILES = leveldb/libleveldb.a leveldb/libmemenv.a *.gcda *.gcno
307310

src/Makefile.qt.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ qt_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL)
362362
if USE_LIBSECP256K1
363363
qt_bitcoin_qt_LDADD += secp256k1/libsecp256k1.la
364364
endif
365-
qt_bitcoin_qt_LDFLAGS = $(AM_LDFLAGS) $(QT_LDFLAGS)
365+
qt_bitcoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS)
366366

367367
#locale/foo.ts -> locale/foo.qm
368368
QT_QM=$(QT_TS:.ts=.qm)

src/Makefile.qttest.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBIT
3636
if USE_LIBSECP256K1
3737
qt_test_test_bitcoin_qt_LDADD += secp256k1/libsecp256k1.la
3838
endif
39-
qt_test_test_bitcoin_qt_LDFLAGS = $(AM_LDFLAGS) $(QT_LDFLAGS)
39+
qt_test_test_bitcoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS)
4040

4141
CLEAN_BITCOIN_QT_TEST = $(TEST_QT_MOC_CPP) qt/test/*.gcda qt/test/*.gcno
4242

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ if USE_LIBSECP256K1
7575
endif
7676

7777
test_test_bitcoin_LDADD += $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS)
78+
test_test_bitcoin_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS)
7879

7980
nodist_test_test_bitcoin_SOURCES = $(GENERATED_TEST_FILES)
8081

0 commit comments

Comments
 (0)