[PATCH] Add -D_FORTIFY_SOURCE=2 to CFLAGS if possible.

Mark Wielaard mark@klomp.org
Wed Feb 15 08:48:00 GMT 2017


On Tue, 2017-02-14 at 21:58 -0500, Mike Frysinger wrote:
> On 09 Feb 2017 21:13, Mark Wielaard wrote:
> > +# 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])
> 
> -D flags should be in CPPFLAGS ...

Normally yes, but _FORTIFY_SOURCE is "special". It depends on CFLAGS
optimization settings. If the user has given CFLAGS="-O0" for example,
or set _FORTIFY_SOURCE to 1 in CFLAGS already adding -D_FORTIFY_SOURCE=2
to CPPFLAGS won't work as intended.

> > +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"])
> > +  ;;
> 
> why not AC_PREPROC_IFELSE and check if _FORTIFY_SOURCE is defined ?

Same reason. We need to test whether the combination of
-D_FORTIFY_SOURCE=2 and any other CFLAG/CPPFLAGS flags work or not. Just
scanning preprocessor flags won't show issues exposed by actually
compiling with a C header (string.h) to see if that errors out.

Cheers,

Mark



More information about the Elfutils-devel mailing list