]> git.meshlink.io Git - catta/blob - common/gcc_stack_protect.m4
616101ea329d36524910b62d3a99de4b4e113e4e
[catta] / common / gcc_stack_protect.m4
1 dnl
2 dnl Useful macros for autoconf to check for ssp-patched gcc
3 dnl 1.0 - September 2003 - Tiago Sousa <mirage@kaotik.org>
4 dnl 1.1 - August 2006 - Ted Percival <ted@midg3t.net>
5 dnl     * Stricter language checking (C or C++)
6 dnl     * Adds GCC_STACK_PROTECT_LIB to add -lssp to LDFLAGS as necessary
7 dnl     * Caches all results
8 dnl     * Uses macros to ensure correct ouput in quiet/silent mode
9 dnl
10 dnl About ssp:
11 dnl GCC extension for protecting applications from stack-smashing attacks
12 dnl http://www.research.ibm.com/trl/projects/security/ssp/
13 dnl
14 dnl Usage:
15 dnl Call GCC_STACK_PROTECT_LIB to determine if the library implementing SSP is
16 dnl available, then the appropriate C or C++ language's test. If you are using
17 dnl both C and C++ you will need to use AC_LANG_PUSH and AC_LANG_POP to ensure
18 dnl the right language is being used for each test.
19 dnl
20 dnl GCC_STACK_PROTECT_LIB
21 dnl adds libssp to the LDFLAGS if it is available
22 dnl
23 dnl GCC_STACK_PROTECT_CC
24 dnl checks -fstack-protector with the C compiler, if it exists then updates
25 dnl CFLAGS and defines ENABLE_SSP_CC
26 dnl
27 dnl GCC_STACK_PROTECT_CXX
28 dnl checks -fstack-protector with the C++ compiler, if it exists then updates
29 dnl CXXFLAGS and defines ENABLE_SSP_CXX
30 dnl
31
32 AC_DEFUN([GCC_STACK_PROTECT_LIB],[
33   AC_CACHE_CHECK([whether libssp exists], ssp_cv_lib,
34     [ssp_old_ldflags="$LDFLAGS"
35      LDFLAGS="$LDFLAGS -lssp"
36      AC_TRY_LINK(,, ssp_cv_lib=yes, ssp_cv_lib=no)
37      LDFLAGS="$ssp_old_ldflags"
38     ])
39   if test $ssp_cv_lib = yes; then
40     LDFLAGS="$LDFLAGS -lssp"
41   fi
42 ])
43
44 AC_DEFUN([GCC_STACK_PROTECT_CC],[
45   AC_LANG_ASSERT(C)
46   if test "X$CC" != "X"; then
47     AC_CACHE_CHECK([whether ${CC} accepts -fstack-protector],
48       ssp_cv_cc,
49       [ssp_old_cflags="$CFLAGS"
50        CFLAGS="$CFLAGS -fstack-protector"
51        AC_TRY_COMPILE(,, ssp_cv_cc=yes, ssp_cv_cc=no)
52        CFLAGS="$ssp_old_cflags"
53       ])
54     if test $ssp_cv_cc = yes; then
55       CFLAGS="$CFLAGS -fstack-protector"
56       AC_DEFINE([ENABLE_SSP_CC], 1, [Define if SSP C support is enabled.])
57     fi
58   fi
59 ])
60
61 AC_DEFUN([GCC_STACK_PROTECT_CXX],[
62   AC_LANG_ASSERT(C++)
63   if test "X$CXX" != "X"; then
64     AC_CACHE_CHECK([whether ${CXX} accepts -fstack-protector],
65       ssp_cv_cxx,
66       [ssp_old_cxxflags="$CXXFLAGS"
67        CXXFLAGS="$CXXFLAGS -fstack-protector"
68        AC_TRY_COMPILE(,, ssp_cv_cxx=yes, ssp_cv_cxx=no)
69        CXXFLAGS="$ssp_old_cxxflags"
70       ])
71     if test $ssp_cv_cxx = yes; then
72       CXXFLAGS="$CXXFLAGS -fstack-protector"
73       AC_DEFINE([ENABLE_SSP_CXX], 1, [Define if SSP C++ support is enabled.])
74     fi
75   fi
76 ])
77