From: Vincent Bernat Date: Tue, 6 Oct 2015 05:55:46 +0000 (+0200) Subject: build: check runtime support of constructor/destructor X-Git-Tag: 0.8.0~46 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fheads%2Ffeature%2Ftest-runtime-ctors;p=thirdparty%2Flldpd.git build: check runtime support of constructor/destructor With some libc, we may have a compile-time support (through GCC), but not a runtime support. For example, with uclibc, support for constructors/destructors is optional. They can be enabled with `CFLAGS=--uclibc-ctors` on some versions. On some others, they need to be builtin by compiling uclibc with UCLIBC_CTOR_DTOR. Unfortunately, cannot be tested when cross-compilation is in effect. --- diff --git a/configure.ac b/configure.ac index b61cf631..eaa576d0 100644 --- a/configure.ac +++ b/configure.ac @@ -88,17 +88,7 @@ AX_CFLAGS_GCC_OPTION([-Wno-missing-field-initializers], [LLDP_CFLAGS]) AX_CFLAGS_GCC_OPTION([-Wno-sign-compare], [LLDP_CFLAGS]) dnl Should be fixed later AX_LDFLAGS_OPTION([-Wl,-z,relro], [LLDP_LDFLAGS]) AX_LDFLAGS_OPTION([-Wl,-z,now], [LLDP_LDFLAGS]) - -# Also, we require support of constructor/destructor -AX_GCC_FUNC_ATTRIBUTE(constructor) -if test x"$ac_cv_have_func_attribute_constructor" = x"no"; then - AC_MSG_FAILURE([*** GCC constructor function attribute mandatory]) -fi -AX_GCC_FUNC_ATTRIBUTE(constructor_priority) -AX_GCC_FUNC_ATTRIBUTE(destructor) -if test x"$ac_cv_have_func_attribute_destructor" = x"no"; then - AC_MSG_FAILURE([*** GCC destructor function attribute mandatory]) -fi +lldp_CHECK_CTORS # Hardening AC_ARG_ENABLE([hardening], diff --git a/m4/ctors.m4 b/m4/ctors.m4 new file mode 100644 index 00000000..f8231693 --- /dev/null +++ b/m4/ctors.m4 @@ -0,0 +1,48 @@ +# +# lldp_CHECK_CTORS +# +AC_DEFUN([lldp_CHECK_CTORS], [ + + # Compiler support + AX_GCC_FUNC_ATTRIBUTE(constructor) + if test x"$ac_cv_have_func_attribute_constructor" = x"no"; then + AC_MSG_FAILURE([*** GCC constructor function attribute mandatory]) + fi + AX_GCC_FUNC_ATTRIBUTE(constructor_priority) + AX_GCC_FUNC_ATTRIBUTE(destructor) + if test x"$ac_cv_have_func_attribute_destructor" = x"no"; then + AC_MSG_FAILURE([*** GCC destructor function attribute mandatory]) + fi + + # Runtime support (libc) + AC_CACHE_CHECK([constructor/destructor runtime support], lldp_cv_ctor_runtime, [ + dnl We need to observe some sideeffect. When + dnl constructor/destructors are running, we may not have a working + dnl standard output. + true > conftest.1 + true > conftest.2 + AC_RUN_IFELSE([AC_LANG_PROGRAM([ + @%:@include + void ctor1(void) __attribute__((constructor)); + void ctor1() { unlink("conftest.1"); } + void ctor2(void) __attribute__((destructor)); + void ctor2() { unlink("conftest.2"); }], []) + ], [ + if test -r conftest.1 -o -r conftest.2; then + lldp_cv_ctor_runtime=no + else + lldp_cv_ctor_runtime=yes + fi + ], [ + dnl Unable to build the check + lldp_cv_ctor_runtime=no + ], [ + dnl Cross compilation + lldp_cv_ctor_runtime=yes + ]) + ]) + + if test x"$lldp_cv_ctor_runtime" = x"no"; then + AC_MSG_FAILURE([*** Constructor/destructor runtime support is missing]) + fi +])