From 6c6bcc00e401262db3b906259674a3231a119603 Mon Sep 17 00:00:00 2001 From: Peter Rajnoha Date: Wed, 4 Dec 2013 09:58:58 +0100 Subject: [PATCH] configure: check compiler/linker support for RELRO and PIE options Also, add AC_TRY_LDFLAGS m4 macro to help with checking ld flags. --- WHATS_NEW | 1 + acinclude.m4 | 21 ++++++++++++++ configure | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ configure.in | 7 +++++ 4 files changed, 110 insertions(+) diff --git a/WHATS_NEW b/WHATS_NEW index 553edfd2d..9155ddf91 100644 --- a/WHATS_NEW +++ b/WHATS_NEW @@ -1,5 +1,6 @@ Version 2.02.105 - ===================================== + Automatically detect support for compiler/linker options to use RELRO and PIE. Add --splitsnapshot to lvconvert to separate out cow LV. Reinstate origin reload to complete lvconvert -s with active LVs. (2.02.98) Select only active volume groups if vgdisplay -A is used. diff --git a/acinclude.m4 b/acinclude.m4 index bf9fe0979..b9473db17 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -36,3 +36,24 @@ AC_DEFUN([AC_TRY_CCFLAG], ifelse([$4], [], [:], [$4]) fi ]) + +dnl AC_TRY_LDFLAGS([LDFLAGS], [VAR], [ACTION-IF-WORKS], [ACTION-IF-FAILS]) +dnl check if $CC supports given ld flags + +AC_DEFUN([AC_TRY_LDFLAGS], +[ + AC_REQUIRE([AC_PROG_CC]) + ac_save_LDFLAGS=$LDFLAGS + LDFLAGS=$1 + AC_CACHE_CHECK([whether $CC accepts $1 ld flags], [ac_cv_flag_$2], + [AC_LINK_IFELSE([AC_LANG_PROGRAM()], + [AS_VAR_SET([ac_cv_flag_$2], [yes])], + [AS_VAR_SET([ac_cv_flag_$2], [no])])]) + LDLAGS=$ac_save_LDFLAGS + $2=AS_VAR_GET([ac_cv_flag_$2]) + if test "$2" = yes; then + ifelse([$3], [], [:], [$3]) + else + ifelse([$4], [], [:], [$4]) + fi +]) diff --git a/configure b/configure index 564d3224c..2ecc7baed 100755 --- a/configure +++ b/configure @@ -740,6 +740,8 @@ PKG_CONFIG THIN_REPAIR_CMD THIN_DUMP_CMD THIN_CHECK_CMD +HAVE_FULL_RELRO +HAVE_PIE POW_LIB LIBOBJS ALLOCA @@ -6741,6 +6743,85 @@ fi { $as_echo "$as_me:${as_lineno-$LINENO}: result: $STATIC_LINK" >&5 $as_echo "$STATIC_LINK" >&6; } +################################################################################ + + + ac_save_CFLAGS=$CFLAGS + CFLAGS=-pie + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC accepts -pie flag" >&5 +$as_echo_n "checking whether $CC accepts -pie flag... " >&6; } +if test "${ac_cv_flag_HAVE_PIE+set}" = set; then : + $as_echo_n "(cached) " >&6 +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +int +main () +{ + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + ac_cv_flag_HAVE_PIE=yes +else + ac_cv_flag_HAVE_PIE=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_flag_HAVE_PIE" >&5 +$as_echo "$ac_cv_flag_HAVE_PIE" >&6; } + CFLAGS=$ac_save_CFLAGS + HAVE_PIE=$ac_cv_flag_HAVE_PIE + if test "HAVE_PIE" = yes; then + : + else + : + fi + + + + + ac_save_LDFLAGS=$LDFLAGS + LDFLAGS=-Wl,-z,relro,-z,now + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC accepts -Wl,-z,relro,-z,now ld flags" >&5 +$as_echo_n "checking whether $CC accepts -Wl,-z,relro,-z,now ld flags... " >&6; } +if test "${ac_cv_flag_HAVE_FULL_RELRO+set}" = set; then : + $as_echo_n "(cached) " >&6 +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +int +main () +{ + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO"; then : + ac_cv_flag_HAVE_FULL_RELRO=yes +else + ac_cv_flag_HAVE_FULL_RELRO=no +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_flag_HAVE_FULL_RELRO" >&5 +$as_echo "$ac_cv_flag_HAVE_FULL_RELRO" >&6; } + LDLAGS=$ac_save_LDFLAGS + HAVE_FULL_RELRO=$ac_cv_flag_HAVE_FULL_RELRO + if test "HAVE_FULL_RELRO" = yes; then + : + else + : + fi + + + ################################################################################ diff --git a/configure.in b/configure.in index e4d0014bd..851d3f75e 100644 --- a/configure.in +++ b/configure.in @@ -158,6 +158,13 @@ AC_ARG_ENABLE(static_link, STATIC_LINK=$enableval, STATIC_LINK=no) AC_MSG_RESULT($STATIC_LINK) +################################################################################ +dnl -- Check if compiler/linker supports PIE and RELRO +AC_TRY_CCFLAG([-pie], [HAVE_PIE], [], []) +AC_SUBST(HAVE_PIE) +AC_TRY_LDFLAGS([-Wl,-z,relro,-z,now], [HAVE_FULL_RELRO], [], []) +AC_SUBST(HAVE_FULL_RELRO) + ################################################################################ dnl -- Prefix is /usr by default, the exec_prefix default is setup later AC_PREFIX_DEFAULT(/usr)