--- a/gcc/config/gnu-user.h 2013-08-20 10:31:40.000000000 +0200 +++ b/gcc/config/gnu-user.h 2013-10-23 22:01:42.337238981 +0200 @@ -134,3 +134,17 @@ see the files COPYING3 and COPYING.RUNTI /* Additional libraries needed by -static-libtsan. */ #undef STATIC_LIBTSAN_LIBS #define STATIC_LIBTSAN_LIBS "-ldl -lpthread" + +/* We use this to make the compiler use -fPIE as default and link + with -pie. */ +#ifdef ENABLE_DEFAULT_PIE +#define PIE_DRIVER_SELF_SPECS \ +"%{pie|fpic|fPIC|fpie|fPIE|fno-pic|fno-PIC|fno-pie|fno-PIE| \ + shared|static|nostdlib|nostartfiles:;:-fPIE -pie}" +#else +#define PIE_DRIVER_SELF_SPECS "" +#endif + +#ifndef GNU_DRIVER_SELF_SPECS +#define GNU_DRIVER_SELF_SPECS PIE_DRIVER_SELF_SPECS +#endif --- a/gcc/config/i386/gnu-user-common.h 2013-01-10 21:38:27.000000000 +0100 +++ b/gcc/config/i386/gnu-user-common.h 2013-10-23 17:37:45.432767049 +0200 @@ -70,3 +70,8 @@ along with GCC; see the file COPYING3. /* Static stack checking is supported by means of probes. */ #define STACK_CHECK_STATIC_BUILTIN 1 + +/* Use GNU_DRIVER_SELF_SPECS. */ +#ifndef DRIVER_SELF_SPECS +#define DRIVER_SELF_SPECS GNU_DRIVER_SELF_SPECS +#endif --- a/gcc/configure.ac 2013-09-25 18:10:35.000000000 +0200 +++ b/gcc/configure.ac 2013-10-22 21:26:56.287602139 +0200 @@ -5434,6 +5434,31 @@ if test x"${LINKER_HASH_STYLE}" != x; th [The linker hash style]) fi +# Check whether --enable-default-pie was given and target have the support. +AC_ARG_ENABLE(default-pie, +[AS_HELP_STRING([--enable-default-pie], [Enable Position independent executable as default. + If we have suppot for it when compiling and linking. + Linux targets supported i?86 and x86_64.])], +enable_default_pie=$enableval, +enable_default_pie=no) +if test x$enable_default_pie = xyes; then + AC_MSG_CHECKING(if $target support to default with -fPIE and link with -pie as default) + enable_default_pie=no + case $target in + i?86*-*-linux* | x86_64*-*-linux*) + enable_default_pie=yes + ;; + *) + ;; + esac + AC_MSG_RESULT($enable_default_pie) +fi +if test x$enable_default_pie == xyes ; then + AC_DEFINE(ENABLE_DEFAULT_PIE, 1, + [Define if your target support default-pie and you have enable it.]) +fi +AC_SUBST([enable_default_pie]) + # Configure the subdirectories # AC_CONFIG_SUBDIRS($subdirs) --- a/gcc/Makefile.in 2013-10-02 21:52:27.000000000 +0200 +++ b/gcc/Makefile.in 2013-10-24 17:46:22.055357122 +0200 @@ -957,14 +957,23 @@ CONTEXT_H = context.h # cross compiler which does not use the native headers and libraries. INTERNAL_CFLAGS = -DIN_GCC @CROSS@ +# We don't want to compile the compiler with -fPIE, it make PCH fail. +enable_default_pie = @enable_default_pie@ +ifeq ($(enable_default_pie),yes) +NOPIE_CFLAGS = -fno-PIE +else +NOPIE_CFLAGS= +endif + # This is the variable actually used when we compile. If you change this, # you probably want to update BUILD_CFLAGS in configure.ac -ALL_CFLAGS = $(T_CFLAGS) $(CFLAGS-$@) \ +ALL_CFLAGS = $(NOPIE_CFLAGS) $(T_CFLAGS) $(CFLAGS-$@) \ $(CFLAGS) $(INTERNAL_CFLAGS) $(COVERAGE_FLAGS) $(WARN_CFLAGS) @DEFS@ # The C++ version. -ALL_CXXFLAGS = $(T_CFLAGS) $(CFLAGS-$@) $(CXXFLAGS) $(INTERNAL_CFLAGS) \ - $(COVERAGE_FLAGS) $(NOEXCEPTION_FLAGS) $(WARN_CXXFLAGS) @DEFS@ +ALL_CXXFLAGS = $(NOPIE_CFLAGS) $(T_CFLAGS) $(CFLAGS-$@) $(CXXFLAGS) \ + $(INTERNAL_CFLAGS) $(COVERAGE_FLAGS) $(NOEXCEPTION_FLAGS) \ + $(WARN_CXXFLAGS) @DEFS@ # Likewise. Put INCLUDES at the beginning: this way, if some autoconf macro # puts -I options in CPPFLAGS, our include files in the srcdir will always @@ -1805,6 +1814,7 @@ libgcc.mvars: config.status Makefile spe echo GCC_CFLAGS = '$(GCC_CFLAGS)' >> tmp-libgcc.mvars echo INHIBIT_LIBC_CFLAGS = '$(INHIBIT_LIBC_CFLAGS)' >> tmp-libgcc.mvars echo TARGET_SYSTEM_ROOT = '$(TARGET_SYSTEM_ROOT)' >> tmp-libgcc.mvars + echo enable_default_pie = '$(enable_default_pie)' >> tmp-libgcc.mvars mv tmp-libgcc.mvars libgcc.mvars @@ -3460,6 +3470,9 @@ site.exp: ./config.status Makefile @if test "@enable_lto@" = "yes" ; then \ echo "set ENABLE_LTO 1" >> ./site.tmp; \ fi + @if test "@enable_default_pie@" = "yes" ; then \ + echo "set ENABLE_DEFAULT_PIE 1" >> ./site.tmp; \ + fi # If newlib has been configured, we need to pass -B to gcc so it can find # newlib's crt0.o if it exists. This will cause a "path prefix not used" # message if it doesn't, but the testsuite is supposed to ignore the message - --- a/libgcc/Makefile.in 2011-11-22 04:01:02.000000000 +0100 +++ b/libgcc/Makefile.in 2012-06-29 00:15:04.534016511 +0200 @@ -275,11 +275,17 @@ override CFLAGS := $(filter-out -fprofil INTERNAL_CFLAGS = $(CFLAGS) $(LIBGCC2_CFLAGS) $(HOST_LIBGCC2_CFLAGS) \ $(INCLUDES) @set_have_cc_tls@ @set_use_emutls@ +#Don't use -fPIE when compiling crtbegin/end. +ifeq ($(enable_default_pie),yes) +NOPIE_CFLAGS = -fno-PIE +else +NOPIE_CFLAGS= +endif # Options to use when compiling crtbegin/end. CRTSTUFF_CFLAGS = -O2 $(GCC_CFLAGS) $(INCLUDES) $(MULTILIB_CFLAGS) -g0 \ -finhibit-size-directive -fno-inline -fno-exceptions \ -fno-zero-initialized-in-bss -fno-toplevel-reorder -fno-tree-vectorize \ - -fno-stack-protector \ + -fno-stack-protector $(NOPIE_CFLAGS) \ $(INHIBIT_LIBC_CFLAGS) # Extra flags to use when compiling crt{begin,end}.o.