aboutsummaryrefslogtreecommitdiff
blob: 86bab05c03035ed715003045a594b1714d42ee12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
--- a/configure.ac	2011-11-29 22:36:43.000000000 +0100
+++ b/configure.ac	2011-12-07 23:29:26.125712475 +0100
@@ -419,6 +419,24 @@ if test "${ENABLE_LIBADA}" != "yes" ; th
   noconfigdirs="$noconfigdirs gnattools"
 fi
 
+# Check whether --enable-espf was given and target have the support.
+AC_ARG_ENABLE([espf],
+[AS_HELP_STRING([--enable-espf],
+		[Enable Stack protector, Position independent executable as 
+		 default if we have suppot for it when compiling.
+		 Linux targets supported i*86, x86_64 and x86_x32.])],
+[
+  case $target in
+    i?86*-*-linux* | x86_??*-*-linux*)
+      enable_espf=yes
+      ;;
+    *)
+      AC_MSG_WARN([*** --enable-espf is not supported on this $target target.])
+      ;;
+  esac
+])
+AC_SUBST([enable_espf])
+
 AC_ARG_ENABLE(libssp,
 [AS_HELP_STRING([--enable-libssp], [build libssp directory])],
 ENABLE_LIBSSP=$enableval,
@@ -3211,6 +3230,11 @@ if test "$GCC" = yes -a "$ENABLE_BUILD_W
   CFLAGS="$saved_CFLAGS"
 fi
 
+# Disable -fstack-protector on stage1
+if test x$enable_espf = xyes; then
+  stage1_cflags="$stage1_cflags -fno-stack-protector"
+fi
+
 AC_SUBST(stage1_cflags)
 
 # Enable --enable-checking in stage1 of the compiler.
--- a/gcc/configure.ac	2011-11-18 11:52:32.000000000 +0100
+++ b/gcc/configure.ac	2012-04-04 16:18:00.269968226 +0200
@@ -5130,6 +5146,148 @@ if test x"${LINKER_HASH_STYLE}" != x; th
                                          [The linker hash style])
 fi
 
+# --------------
+# Espf checks
+# --------------
+
+# Check whether --enable-espf was given and target have the support.
+AC_ARG_ENABLE([espf],
+[AS_HELP_STRING([--enable-espf],
+		[Enable Stack protector, Position independent executable and
+		 Fortify_sources as default if we have suppot for it when compiling.
+		 Linux targets supported i*86, x86_64, and x86_x32.])],
+	set_enable_espf=$enableval,
+	set_enable_espf=no)
+if test $set_enable_espf = yes ; then
+  AC_MSG_CHECKING(if $target support espf)
+if test $set_enable_espf = yes ; then
+  case "$target" in
+    i?86*-*-linux* | x86_??*-*-linux*)
+      enable_espf=yes
+      AC_DEFINE(ENABLE_ESPF, 1,
+        [Define if your target support espf and you have enable it.])
+      ;;
+    *)
+      enable_espf=no
+      ;;
+  esac
+else
+ enable_espf=no
+fi
+AC_MSG_RESULT($enable_espf)
+fi
+AC_SUBST([enable_espf])
+if test $enable_espf = yes ; then
+
+# Check for FORTIFY_SOURCES support in target C library.
+  AC_CACHE_CHECK(for _FORTIFY_SOURCES support  in target C library,
+    gcc_cv_libc_provides_fortify, 
+    [gcc_cv_libc_provides_fortify=no
+    case "$target" in
+      *-*-linux*)
+        [# glibc 2.8 and later provides _FORTIFY_SOURCES.
+	 # uClibc 0.9.32 and later provides _FORTIFY_SOURCES.
+        if test -f $target_header_dir/features.h; then
+          if $EGREP '^[ 	]*#[ 	]*define[ 	]+__GLIBC__[ 	]+2' \
+	    $target_header_dir/features.h > /dev/null \
+	    && $EGREP '^[ 	]*#[ 	]*define[ 	]+__GLIBC_MINOR__[ 	]+([1-9][0-9]|[8-9])' \
+	    $target_header_dir/features.h > /dev/null; then
+	      gcc_cv_libc_provides_fortify=yes
+          elif $EGREP '^[ 	]*#[ 	]*define[ 	]+__UCLIBC__[ 	]+1' \
+	     $target_header_dir/features.h > /dev/null; then
+	    if test -f $target_header_dir/bits/uClibc_config.h && \
+	       $EGREP '^[ 	]*#[ 	]*define[ 	]+__UCLIBC_SUBLEVEL__[ 	]+([3-9][2-9]|[4-9][0-9])' \
+	       $target_header_dir/bits/uClibc_config.h > /dev/null; then
+	      gcc_cv_libc_provides_fortify=yes
+	    else
+	      gcc_cv_libc_provides_fortify=no
+	    fi
+	  fi
+        fi]
+        ;;
+      *) gcc_cv_libc_provides_fortify=no ;;
+    esac])
+
+  AC_MSG_CHECKING(if we can default to use -fPIE and link with -pie)
+  if test x$gcc_cv_ld_pie = xyes; then
+    saved_LDFLAGS="$LDFLAGS"
+    saved_CFLAGS="$CFLAGS"
+    CFLAGS="$CFLAGS -fPIE -Werror"
+    LDFLAGS="$LDFLAGS -fPIE -pie"
+    AC_TRY_LINK(,,
+      [AC_MSG_RESULT([yes]); enable_espf_pie=yes],
+      [AC_MSG_RESULT([no]); enable_espf_pie=no])
+    LDFLAGS="$saved_LDFLAGS"
+    CFLAGS="$saved_CFLAGS"
+  else
+    AC_MSG_RESULT([no])
+    enable_espf_pie=no
+  fi
+
+  if test $enable_espf_pie = yes ; then
+    AC_DEFINE(ENABLE_ESPF_PIE, 1,
+      [Define if your compiler will default to use -fPIE and link with -pie.])
+  fi
+
+  AC_MSG_CHECKING(if we can default to use -fstack-protector)
+  ssp_link_test=no
+  if test x$gcc_cv_libc_provides_ssp = xyes && test x$set_have_as_tls = yes; then
+    if $EGREP '^[ 	]*#[ 	]*define[ 	]+__UCLIBC__[ 	]+1' \
+       $target_header_dir/features.h > /dev/null; then
+      if test -f $target_header_dir/bits/uClibc_config.h && \
+	 $EGREP '^[ 	]*#[ 	]*define[ 	]+__UCLIBC_SUBLEVEL__[ 	]+([3-9][2-9]|[4-9][0-9])' \
+	 $target_header_dir/bits/uClibc_config.h > /dev/null && \
+	 $EGREP '^[ 	]*#[ 	]*define[ 	]+__UCLIBC_HAS_TLS__[ 	]+1' \
+	 $target_header_dir/bits/uClibc_config.h > /dev/null; then
+	ssp_link_test=yes
+      fi
+    else
+      ssp_link_test=yes
+    fi
+  fi
+  if test x$ssp_link_test=xyes ; then
+    saved_CFLAGS="$CFLAGS"
+    CFLAGS="$CFLAGS -O2 -fstack-protector -Werror"
+    AC_TRY_LINK(,,
+      [AC_MSG_RESULT([yes]); enable_espf_ssp=yes],
+      [AC_MSG_RESULT([no]); enable_espf_ssp=no])
+    CFLAGS="$saved_CFLAGS"
+  else
+    AC_MSG_RESULT([no])
+    enable_espf_ssp=no
+  fi
+  if test $enable_espf_ssp = yes ; then
+    AC_DEFINE(ENABLE_ESPF_SSP, 1,
+      [Define if your compiler will default to use -fstack-protector.])
+  fi
+
+  AC_MSG_CHECKING(if we can default to use -D_FORTIFY_SOURCES=2)
+  if test x$gcc_cv_libc_provides_fortify = xyes; then
+    saved_CFLAGS="$CFLAGS"
+    saved_CPPFLAGS="$CPPFLAGS"
+    CFLAGS="$CFLAGS -O2 -Werror"
+    CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCES=2"
+    AC_TRY_LINK([
+      #include <sys/types.h>
+      #include <sys/stat.h>
+      #include <fcntl.h>
+      ],[
+      open ("/tmp/foo", O_WRONLY | O_CREAT);
+      ],
+      [AC_MSG_RESULT([no]); enable_espf_fortify=no],
+      [AC_MSG_RESULT([yes]); enable_espf_fortify=yes])
+    CFLAGS="$saved_CFLAGS"
+    CPPFLAGS="$saved_CPPFLAGS"
+  else
+    [AC_MSG_RESULT([no]); enable_espf_fortify=no]
+  fi
+  if test x$enable_espf_fortify = xyes ; then
+    AC_DEFINE(ENABLE_ESPF_FORTIFY, 1,
+      [Define if your compiler will default to use -D_FORTIFY_SOURCES=2.])
+  fi
+
+fi
+
 # Configure the subdirectories
 # AC_CONFIG_SUBDIRS($subdirs)