diff -Nru gcc.ORIG/ChangeLog gcc/ChangeLog --- gcc.ORIG/ChangeLog 2006-05-25 01:42:18.000000000 +0200 +++ gcc/ChangeLog 2006-09-30 09:54:29.000000000 +0200 @@ -1,3 +1,12 @@ +2006-09-30 Alexander Gabert + * gcc.c (definitions): added hardened support + for cc_spec, endfile_spec, startfile_spec and + LINK_PIE_SPEC section, minor definition logic + + * pie-ssp-*.h (all functions): added hardened + support headers for creating the positive and + negative logic exclude list for the arguments + 2006-05-24 Release Manager * GCC 4.1.1 released. diff -Nru gcc.ORIG/Makefile.in gcc/Makefile.in --- gcc.ORIG/Makefile.in 2006-09-30 09:51:17.000000000 +0200 +++ gcc/Makefile.in 2006-09-30 10:09:43.000000000 +0200 @@ -791,9 +791,11 @@ # autoconf inserts -DCROSS_COMPILE if we are building a cross compiler. INTERNAL_CFLAGS = -DIN_GCC @CROSS@ +HARDENED_CFLAGS = -DEFAULT_PIE_SSP -DEFAULT_RELRO -DEFAULT_BIND_NOW + # 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 = $(X_CFLAGS) $(T_CFLAGS) \ +ALL_CFLAGS = $(HARDENED_CFLAGS) $(X_CFLAGS) $(T_CFLAGS) \ $(CFLAGS) $(INTERNAL_CFLAGS) $(COVERAGE_FLAGS) $(WARN_CFLAGS) $(XCFLAGS) @DEFS@ # Likewise. Put INCLUDES at the beginning: this way, if some autoconf macro diff -Nru gcc.ORIG/config/linux.h gcc/config/linux.h --- gcc.ORIG/config/linux.h 2005-08-06 15:26:35.000000000 +0200 +++ gcc/config/linux.h 2006-09-30 09:54:29.000000000 +0200 @@ -37,16 +37,28 @@ the GNU/Linux magical crtbegin.o file (see crtstuff.c) which provides part of the support for getting C++ file-scope static object constructed before entering `main'. */ - + #undef STARTFILE_SPEC + #if defined HAVE_LD_PIE + #define STARTFILE_SPEC \ "%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} \ crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}" + +#if defined HARDENED_STARTFILE_SPEC +#undef HARDENED_STARTFILE_SPEC +#endif +#define HARDENED_STARTFILE_SPEC \ + "%{!shared: %{pg|p|profile:gcrt1.o%s;static|nopie:crt1.o%s;:Scrt1.o%s}} \ + crti.o%s %{static:crtbeginT.o%s;nopie:crtbegin.o%s;:crtbeginS.o%s}" + #else + #define STARTFILE_SPEC \ "%{!shared: %{pg|p|profile:gcrt1.o%s;:crt1.o%s}} \ crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}" + #endif /* Provide a ENDFILE_SPEC appropriate for GNU/Linux. Here we tack on @@ -56,9 +68,16 @@ GNU/Linux "finalizer" file, `crtn.o'. */ #undef ENDFILE_SPEC + #define ENDFILE_SPEC \ "%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s" +#if defined HARDENED_ENDFILE_SPEC +#undef HARDENED_ENDFILE_SPEC +#endif +#define HARDENED_ENDFILE_SPEC \ + "%{static|nopie:crtend.o%s;:crtendS.o%s} crtn.o%s" + /* This is for -profile to use -lc_p instead of -lc. */ #ifndef CC1_SPEC #define CC1_SPEC "%{profile:-p}" diff -Nru gcc.ORIG/gcc.c gcc/gcc.c --- gcc.ORIG/gcc.c 2006-09-30 09:51:19.000000000 +0200 +++ gcc/gcc.c 2006-09-30 10:25:26.000000000 +0200 @@ -87,6 +87,30 @@ #include "gcc.h" #include "flags.h" +#include "pie-ssp-setup.h" +#include "pie-ssp-modus.h" +#include "pie-ssp-chain.h" + +#if !( defined(EFAULT_PIE) || defined(EFAULT_PIE_SSP) ) + +#if defined HARDENED_STARTFILE_SPEC +#undef HARDENED_STARTFILE_SPEC +#endif + +#if defined HARDENED_ENDFILE_SPEC +#undef HARDENED_ENDFILE_SPEC +#endif + +#endif + +#if !( defined(EFAULT_PIE) || defined(EFAULT_SSP) || defined(EFAULT_PIE_SSP) ) + +#if defined HARDENED_CC1_SPEC_SUFFIX +#undef HARDENED_CC1_SPEC_SUFFIX +#endif + +#endif + /* By default there is no special suffix for target executables. */ /* FIXME: when autoconf is fixed, remove the host check - dj */ #if defined(TARGET_EXECUTABLE_SUFFIX) && defined(HOST_EXECUTABLE_SUFFIX) @@ -675,20 +699,46 @@ #ifndef LINK_SSP_SPEC #ifdef TARGET_LIBC_PROVIDES_SSP -#define LINK_SSP_SPEC "%{fstack-protector:}" +#define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all:}" #else #define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all:-lssp_nonshared -lssp}" #endif #endif +/* depending on the -D definition we are using default relro or erase the specs */ +#if !( defined(EFAULT_RELRO) ) +#if defined LINK_PIE_SPEC_RELRO +#undef LINK_PIE_SPEC_RELRO +#define LINK_PIE_SPEC_RELRO " " +#endif +#endif + +/* same for bind now ld behaviour */ +#if !( defined(EFAULT_BIND_NOW) ) +#if defined LINK_PIE_SPEC_NOW +#undef LINK_PIE_SPEC_NOW +#define LINK_PIE_SPEC_NOW " " +#endif +#endif + #ifndef LINK_PIE_SPEC + #ifdef HAVE_LD_PIE -#define LINK_PIE_SPEC "%{pie:-pie} " + +#if !( defined(EFAULT_PIE) || defined(EFAULT_PIE_SSP) ) +#define LINK_PIE_SPEC "%{pie:-pie} " LINK_PIE_SPEC_RELRO " " LINK_PIE_SPEC_NOW #else -#define LINK_PIE_SPEC "%{pie:} " -#endif +#define LINK_PIE_SPEC LINK_PIE_SPEC_PIE " " LINK_PIE_SPEC_RELRO " " LINK_PIE_SPEC_NOW #endif +#else /* HAVE_LD_PIE */ + +#define LINK_PIE_SPEC "%{pie:} " + +#endif /* HAVE_LD_PIE */ + +#endif /* LINK_PIE_SPEC */ + /* -u* was put back because both BSD and SysV seem to support it. */ /* %{static:} simply prevents an error message if the target machine doesn't handle -static. */ @@ -729,7 +779,11 @@ static const char *asm_debug; static const char *cpp_spec = CPP_SPEC; +#if defined HARDENED_CC1_SPEC_SUFFIX +static const char *cc1_spec = CC1_SPEC " " HARDENED_CC1_SPEC_SUFFIX; +#else static const char *cc1_spec = CC1_SPEC; +#endif static const char *cc1plus_spec = CC1PLUS_SPEC; static const char *link_gcc_c_sequence_spec = LINK_GCC_C_SEQUENCE_SPEC; static const char *link_ssp_spec = LINK_SSP_SPEC; @@ -740,8 +794,19 @@ static const char *mfwrap_spec = MFWRAP_SPEC; static const char *mflib_spec = MFLIB_SPEC; static const char *libgcc_spec = LIBGCC_SPEC; + +#if defined HARDENED_ENDFILE_SPEC +static const char *endfile_spec = HARDENED_ENDFILE_SPEC; +#else static const char *endfile_spec = ENDFILE_SPEC; +#endif + +#if defined HARDENED_STARTFILE_SPEC +static const char *startfile_spec = HARDENED_STARTFILE_SPEC; +#else static const char *startfile_spec = STARTFILE_SPEC; +#endif + static const char *switches_need_spaces = SWITCHES_NEED_SPACES; static const char *linker_name_spec = LINKER_NAME; static const char *link_command_spec = LINK_COMMAND_SPEC; diff -Nru gcc.ORIG/pie-ssp-chain.h gcc/pie-ssp-chain.h --- gcc.ORIG/pie-ssp-chain.h 1970-01-01 01:00:00.000000000 +0100 +++ gcc/pie-ssp-chain.h 2006-09-30 11:13:47.000000000 +0200 @@ -0,0 +1,95 @@ +#ifndef LINUX_PIE_SSP_CHAIN_H +#define LINUX_PIE_SSP_CHAIN_H + +/* + * cc1: + **************************************************************************** + * C compiler generating code - for nonintel arches fpic/fPIC is not the same + * Thu Feb 5 22:32:02 CET 2004 -fpie is causing TEXT relocations in binaries + * + * -fPIE/-fpie may not be used, it produces unusable objects for shared libs + * if it becomes usable, then -fPIE should be used instead of -fpie (arch dep) + * + * we take care now not using -fPIE on shared libs, by checking for -fPIC/fpic + **************************************************************************** + */ + +#define CC1_PIE "%{!fPIC:%{!fpic:-fPIE}}" + +#define CC1_SSP "-fstack-protector" +#define CC1_SSP_ALL "%{!fno-stack-protector-all:-fstack-protector-all}" + +#define HARDENED_CC1_SSP_DUMMY "%{fstack-protector:} %{fno-stack-protector:} %{fstack-protector-all:} %{fno-stack-protector-all:}" + +#if ! ( defined ( __hppa__ ) || defined ( USE_HTB ) ) +#define HARDENED_CC1_SSP_SPEC SSP_CHAIN_EXCLUDE( SSP_LIBC_EXCLUDE( STD_SSP_TRIGGER( CC1_SSP " " CC1_SSP_ALL) ) ) +#else +#define HARDENED_CC1_SSP_SPEC HARDENED_CC1_SSP_DUMMY +#endif + +#define HARDENED_CC1_SPEC_SUFFIX STD_CHAIN_EXCLUDE( CC1_CHAIN_EXCLUDE( STD_PIE_TRIGGER(CC1_PIE) ) " " HARDENED_CC1_SSP_SPEC ) + +/* + * startfile: + * we have 4 types of archs: + * default: x86[_64]/mips/parisc + * not using profile: alpha/rs6000/sparc[64] + * not using crtbeginT.o for static: arm/ia64 + * having some addon: rs6000(mnewlib) + */ + +/* + * endfile: + * we have 3 types of archs: + * default: x86[_64]/mips/parisc + * fastmath addon: alpha/ia64/sparc[64] + * having some other addon: rs6000(mnewlib) + */ + +/* special cases: rs6000 uses STARTFILE_LINUX_SPEC/ENDFILE_LINUX_SPEC */ + +/* the above variety explains why the startfile/endfile sections + * are moved directly to the respective files inside the gcc dir */ + +/* + * link: + ***************************************************************************** + * building of position independent executables is enabled by binutils:ld -pie + * use the x86 Redhat branch update provided support for LINK_PIE_SPEC section + ***************************************************************************** + */ + +#define LINKSEC_PIE_LNK "-pie" + +#define LINKSEC_RELRO_LNK "-z relro" + +#define LINKSEC_NOW_LNK "-z now" + +#ifdef LINK_PIE_SPEC_RELRO +#undef LINK_PIE_SPEC_RELRO +#endif +/* setup default relro behaviour and filter incoming argument to do nothing */ +#define LINK_PIE_SPEC_RELRO STD_CHAIN_EXCLUDE( SSP_LIBC_EXCLUDE( STD_RELRO_TRIGGER( LINKSEC_RELRO_LNK ) ) ) " " OPP_RELRO_TRIGGER() + +#ifdef LINK_PIE_SPEC_NOW +#undef LINK_PIE_SPEC_NOW +#endif +/* setup default bind now behaviour and filter incoming argument to do nothing (and not interfere with the default logic) */ +#define LINK_PIE_SPEC_NOW STD_CHAIN_EXCLUDE( SSP_LIBC_EXCLUDE( STD_NOW_TRIGGER( LINKSEC_NOW_LNK ) ) ) " " OPP_NOW_TRIGGER() + +#ifdef LINK_PIE_SPEC_PIE +#undef LINK_PIE_SPEC_PIE +#endif +/* set up default pie behaviour */ +#define LINK_PIE_SPEC_PIE_EXCLUDE_CHAIN STD_PIE_TRIGGER( \ + STD_CHAIN_EXCLUDE( \ + SSP_LIBC_EXCLUDE( \ + LNK_CHAIN_EXCLUDE( \ + PIE_CHAIN_EXCLUDE( \ + LINKSEC_PIE_LNK \ + ) ) ) ) ) + +#define LINK_PIE_SPEC_PIE LINK_PIE_SPEC_PIE_EXCLUDE_CHAIN " " OPP_PIE_TRIGGER() + +#endif /* LINUX_PIE_SSP_CHAIN_H */ + diff -Nru gcc.ORIG/pie-ssp-modus.h gcc/pie-ssp-modus.h --- gcc.ORIG/pie-ssp-modus.h 1970-01-01 01:00:00.000000000 +0100 +++ gcc/pie-ssp-modus.h 2006-09-30 09:54:29.000000000 +0200 @@ -0,0 +1,127 @@ +#ifndef LINUX_PIE_SSP_MODUS_H +#define LINUX_PIE_SSP_MODUS_H + +/* + * The enabler and inverse is used for the proper inclusion of the + * startfile and endfile chaining of the crt1, crtbegin and crtend + * to use caution not issuing zero or double occurrences of object + * in the respective sections of the currently utilized definition + */ + +/* + **************************************************************** + * set up the trigger logic for stack smashing protector with CC1 + **************************************************************** + */ + +#if defined ( EFAULT_SSP ) || defined ( EFAULT_PIE_SSP ) + +#define STD_SSP_TRIGGER(flag) \ + NSPEC("fno-stack-protector", \ + flag \ + ) + +#else /* defined ( EFAULT_SSP ) || defined ( EFAULT_PIE_SSP ) */ + +#define STD_SSP_TRIGGER(flag) \ + PSPEC("fstack-protector", \ + flag \ + ) + +#endif /* defined ( EFAULT_SSP ) || defined ( EFAULT_PIE_SSP ) */ + +/* + **************************************************************** + * define the trigger logic for setting up position independence + **************************************************************** + */ + +#if defined ( EFAULT_PIE ) || defined ( EFAULT_PIE_SSP ) + +#define STD_PIE_TRIGGER(flag) \ + NSPEC("nopie", \ + flag \ + ) + +#define INV_PIE_TRIGGER(flag) \ + PSPEC("nopie", \ + flag \ + ) + +#define OPP_PIE_TRIGGER(flag) \ + PSPEC("pie", \ + flag \ + ) + +#else /* defined ( DEFAULT_PIE ) || defined ( EFAULT_PIE_SSP) */ + +#define STD_PIE_TRIGGER(flag) \ + PSPEC("pie", \ + flag \ + ) + +#define INV_PIE_TRIGGER(flag) \ + NSPEC("pie", \ + flag \ + ) + +#define OPP_PIE_TRIGGER(flag) \ + PSPEC("nopie", \ + flag \ + ) + +#endif /* defined ( EFAULT_PIE ) || defined ( EFAULT_PIE_SSP ) */ + +#if defined ( EFAULT_RELRO ) + +#define STD_RELRO_TRIGGER(flag) \ + NSPEC("norelro", \ + flag \ + ) + +#define OPP_RELRO_TRIGGER(flag) \ + PSPEC("relro", \ + flag \ + ) + +#else /* defined ( EFAULT_RELRO ) */ + +#define STD_RELRO_TRIGGER(flag) \ + PSPEC("relro", \ + flag \ + ) + +#define OPP_RELRO_TRIGGER(flag) \ + PSPEC("norelro", \ + flag \ + ) + +#endif /* defined ( EFAULT_RELRO ) */ + +#if defined ( EFAULT_BIND_NOW ) + +#define STD_NOW_TRIGGER(flag) \ + NSPEC("nonow", \ + flag \ + ) + +#define OPP_NOW_TRIGGER(flag) \ + PSPEC("now", \ + flag \ + ) + +#else /* EFAULT_BIND_NOW */ + +#define STD_NOW_TRIGGER(flag) \ + PSPEC("now", \ + flag \ + ) + +#define OPP_NOW_TRIGGER(flag) \ + PSPEC("nonow", \ + flag \ + ) + +#endif /* EFAULT_BIND_NOW */ + +#endif /* LINUX_PIE_SSP_MODUS_H */ diff -Nru gcc.ORIG/pie-ssp-setup.h gcc/pie-ssp-setup.h --- gcc.ORIG/pie-ssp-setup.h 1970-01-01 01:00:00.000000000 +0100 +++ gcc/pie-ssp-setup.h 2006-09-30 11:06:31.000000000 +0200 @@ -0,0 +1,86 @@ +#ifndef LINUX_PIE_SSP_SETUP_H +#define LINUX_PIE_SSP_SETUP_H + +/* ********************************************************************************** */ +/* definition of macros and functions needed for constructing the specs file sections */ +/* ********************************************************************************** */ + +#define PSPEC(a,b) "%{"a": "b"} " + +#define NSPEC(a,b) "%{!"a": "b"} " + +/* + * standard exclusion + * disables -fPIE + * disables -pie + * disables SSP + * applies to cc1 + */ +#define STD_CHAIN_EXCLUDE(flag) \ + NSPEC("DIN_GCC", \ + NSPEC("D__KERNEL__", \ + flag \ + )) + +/* + * link section exclusion + * disables -fPIE + * ibcs not added (yet) + * applies to cc1 and link_command + */ +#define PIE_LNK_CHAIN_EXCLUDE(flag) \ + NSPEC("static", \ + flag \ + ) + +/* pic exclusion, applies to cc1 */ +#define PIC_CHAIN_EXCLUDE(flag) \ + NSPEC("fno-PIC", \ + NSPEC("fno-pic", \ + flag \ + )) + +/* + * pie exclusion + * disables -fPIE and -pie + * applies to cc1 and link_command + */ +#define PIE_CHAIN_EXCLUDE(flag) \ + NSPEC("shared", \ + NSPEC("nostdlib", \ + NSPEC("nostartfiles", \ + NSPEC("fno-PIE", \ + NSPEC("fno-pie", \ + flag \ + ))))) + +/* + * pie exclusion + * disables -pie + * applies to link_command + */ +#define LNK_GEN_CHAIN_EXCLUDE(flag) \ + NSPEC("A", \ + flag \ + ) + +#define CC1_CHAIN_EXCLUDE(flag) PIE_LNK_CHAIN_EXCLUDE( PIC_CHAIN_EXCLUDE( PIE_CHAIN_EXCLUDE(flag) ) ) +#define LNK_CHAIN_EXCLUDE(flag) PIE_LNK_CHAIN_EXCLUDE( LNK_GEN_CHAIN_EXCLUDE(flag) ) + +/* + * ssp exclusion + * disables SSP + * applies to cc1 + */ +#define SSP_CHAIN_EXCLUDE(flag) \ + NSPEC("nostdlib", \ + flag \ + ) + +#define SSP_LIBC_EXCLUDE(flag) \ + NSPEC("D_LIBC", \ + NSPEC("D_LIBC_REENTRANT", \ + flag \ + )) + +#endif /* LINUX_PIE_SSP_SETUP_H */ diff -Nru gcc.ORIG/version.c gcc/version.c --- gcc.ORIG/version.c 2006-09-30 09:51:17.000000000 +0200 +++ gcc/version.c 2006-09-30 09:56:35.000000000 +0200 @@ -8,7 +8,7 @@ in parentheses. You may also wish to include a number indicating the revision of your modified compiler. */ -#define VERSUFFIX " (Gentoo 4.1.1-r1)" +#define VERSUFFIX " (Gentoo Hardened 4.1.1-r1 xXx PIE SSP autoemitter test patch for Adamant xXx)" /* This is the location of the online document giving instructions for reporting bugs. If you distribute a modified version of GCC,