]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
Add -D_FORTIFY_SOURCE=2 to CFLAGS if possible.
authorMark Wielaard <mark@klomp.org>
Thu, 9 Feb 2017 20:09:54 +0000 (21:09 +0100)
committerMark Wielaard <mark@klomp.org>
Tue, 14 Feb 2017 09:39:51 +0000 (10:39 +0100)
Some distros now add -D_FORTIFY_SOURCE=2 by default and we have missed
some issues in the past caught by it. Add it to CFLAGS if possible.
The configure check will make sure that it doesn't conflict with any
other CFLAGS already defined.

Signed-off-by: Mark Wielaard <mark@klomp.org>
ChangeLog
configure.ac

index a587b430c6847188918a46a149ccafbbef86ca52..b50f79ee7002ef513a973d2b68fc9d405baab8e7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-02-09  Mark Wielaard  <mark@klomp.org>
+
+       * configure.ac: Add check for adding -D_FORTIFY_SOURCE=2 to CFLAGS.
+
 2017-01-12  Mark Wielaard  <mark@klomp.org>
 
        * configure.ac: Define PACKAGE_URL for older autoconf.
index 07ad592f4edb21497703873b1b505b4ca062259c..46055f26291e1c115473ca92cf88f52848965a57 100644 (file)
@@ -159,6 +159,27 @@ AC_CACHE_CHECK([whether fts.h is bad when included (with LFS)], ac_cv_bad_fts,
                     ac_cv_bad_fts=no, ac_cv_bad_fts=yes)])
 AS_IF([test "x$ac_cv_bad_fts" = "xyes"], [CFLAGS="$CFLAGS -DBAD_FTS=1"])
 
+# See if we can add -D_FORTIFY_SOURCE=2. Don't do it if it is already
+# (differently) defined or if it generates warnings/errors because we
+# don't use the right optimisation level (string.h will warn about that).
+AC_MSG_CHECKING([whether to add -D_FORTIFY_SOURCE=2 to CFLAGS])
+case "$CFLAGS" in
+  *-D_FORTIFY_SOURCE=2*)
+    AC_MSG_RESULT([no, already there])
+    ;;
+  *)
+    save_CFLAGS="$CFLAGS"
+    CFLAGS="-D_FORTIFY_SOURCE=2 -Werror $CFLAGS"
+    AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+      #include <string.h>
+      int main() { return 0; }
+    ]])], [ AC_MSG_RESULT([yes])
+            CFLAGS="-D_FORTIFY_SOURCE=2 $save_CFLAGS" ],
+          [ AC_MSG_RESULT([no])
+            CFLAGS="$save_CFLAGS"])
+  ;;
+esac
+
 dnl enable debugging of branch prediction.
 AC_ARG_ENABLE([debugpred],
 AS_HELP_STRING([--enable-debugpred],[build binaries with support to debug branch prediction]),