commit:     c98ce0ffdfec2231b6965981df9c832fa0da70a7
Author:     Benda Xu <heroxbd <AT> gentoo <DOT> org>
AuthorDate: Mon Nov 24 08:58:00 2014 +0000
Commit:     Benda XU <heroxbd <AT> gentoo <DOT> org>
CommitDate: Mon Nov 24 15:41:11 2014 +0000
URL:        
http://sources.gentoo.org/gitweb/?p=dev/heroxbd.git;a=commit;h=c98ce0ff

use toolchain eclass from gx86

---
 eclass/toolchain.eclass | 2488 ++++++++++++++++++++++++-----------------------
 1 file changed, 1298 insertions(+), 1190 deletions(-)

diff --git a/eclass/toolchain.eclass b/eclass/toolchain.eclass
index 55d9890..1345b5e 100644
--- a/eclass/toolchain.eclass
+++ b/eclass/toolchain.eclass
@@ -1,16 +1,14 @@
-# Copyright 1999-2013 Gentoo Foundation
+# Copyright 1999-2014 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
-# $Header: /var/cvsroot/gentoo-x86/eclass/toolchain.eclass,v 1.597 2013/07/24 
01:34:38 dirtyepic Exp $
+# $Header: /var/cvsroot/gentoo-x86/eclass/toolchain.eclass,v 1.647 2014/11/15 
08:45:33 vapier Exp $
 
 # Maintainer: Toolchain Ninjas <[email protected]>
 
-#---->> eclass stuff <<----
 DESCRIPTION="The GNU Compiler Collection"
 HOMEPAGE="http://gcc.gnu.org/";
-LICENSE="GPL-2 LGPL-2.1"
 RESTRICT="strip" # cross-compilers need controlled stripping
 
-inherit eutils versionator libtool toolchain-funcs flag-o-matic gnuconfig 
multilib fixheadtails pax-utils prefix
+inherit eutils fixheadtails flag-o-matic gnuconfig libtool multilib pax-utils 
toolchain-funcs versionator
 
 if [[ ${PV} == *_pre9999* ]] ; then
        EGIT_REPO_URI="git://gcc.gnu.org/git/gcc.git"
@@ -26,10 +24,17 @@ fi
 
 FEATURES=${FEATURES/multilib-strict/}
 
-EXPORT_FUNCTIONS pkg_setup src_unpack src_compile src_test src_install 
pkg_postinst pkg_postrm
-#----<< eclass stuff >>----
+EXPORTED_FUNCTIONS="pkg_setup src_unpack src_compile src_test src_install 
pkg_postinst pkg_postrm"
+case ${EAPI:-0} in
+       0|1)    ;;
+       2|3)    EXPORTED_FUNCTIONS+=" src_prepare src_configure" ;;
+       4*|5*)  EXPORTED_FUNCTIONS+=" pkg_pretend src_prepare src_configure" ;;
+       *)      die "I don't speak EAPI ${EAPI}."
+esac
+EXPORT_FUNCTIONS ${EXPORTED_FUNCTIONS}
 
 #---->> globals <<----
+
 export CTARGET=${CTARGET:-${CHOST}}
 if [[ ${CTARGET} = ${CHOST} ]] ; then
        if [[ ${CATEGORY/cross-} != ${CATEGORY} ]] ; then
@@ -45,11 +50,16 @@ is_crosscompile() {
 }
 
 # General purpose version check.  Without a second arg matches up to minor 
version (x.x.x)
-# (ie. 4.6.0_pre9999 matches 4 or 4.6 or 4.6.0 but not 4.6.1)
 tc_version_is_at_least() {
        version_is_at_least "$1" "${2:-${GCC_RELEASE_VER}}"
 }
 
+# General purpose version range check
+# Note that it matches up to but NOT including the second version
+tc_version_is_between() {
+       tc_version_is_at_least "${1}" && ! tc_version_is_at_least "${2}"
+}
+
 GCC_PV=${TOOLCHAIN_GCC_PV:-${PV}}
 GCC_PVR=${GCC_PV}
 [[ ${PR} != "r0" ]] && GCC_PVR=${GCC_PVR}-${PR}
@@ -66,12 +76,9 @@ GCCMICRO=$(get_version_component_range 3 ${GCC_PV})
 GCC_CONFIG_VER=${GCC_CONFIG_VER:-$(replace_version_separator 3 '-' ${GCC_PV})}
 
 # Pre-release support
-if [[ ${GCC_PV} != ${GCC_PV/_pre/-} ]] ; then
+if [[ ${GCC_PV} == *_pre* ]] ; then
        PRERELEASE=${GCC_PV/_pre/-}
-fi
-
-# make _alpha and _beta ebuilds automatically use a snapshot
-if [[ ${GCC_PV} == *_alpha* ]] ; then
+elif [[ ${GCC_PV} == *_alpha* ]] ; then
        SNAPSHOT=${GCC_BRANCH_VER}-${GCC_PV##*_alpha}
 elif [[ ${GCC_PV} == *_beta* ]] ; then
        SNAPSHOT=${GCC_BRANCH_VER}-${GCC_PV##*_beta}
@@ -79,7 +86,13 @@ elif [[ ${GCC_PV} == *_rc* ]] ; then
        SNAPSHOT=${GCC_PV%_rc*}-RC-${GCC_PV##*_rc}
 fi
 
+if [[ ${SNAPSHOT} == 5.0-* ]] ; then
+       # The gcc-5 release has dropped the .0 for some reason.
+       SNAPSHOT=${SNAPSHOT/5.0/5}
+fi
+
 export GCC_FILESDIR=${GCC_FILESDIR:-${FILESDIR}}
+
 PREFIX=${TOOLCHAIN_PREFIX:-/usr}
 
 if tc_version_is_at_least 3.4.0 ; then
@@ -100,38 +113,58 @@ 
DATAPATH=${TOOLCHAIN_DATAPATH:-${PREFIX}/share/gcc-data/${CTARGET}/${GCC_CONFIG_
 # Dont install in /usr/include/g++-v3/, but in gcc internal directory.
 # We will handle /usr/include/g++-v3/ with gcc-config ...
 
STDCXX_INCDIR=${TOOLCHAIN_STDCXX_INCDIR:-${LIBPATH}/include/g++-v${GCC_BRANCH_VER/\.*/}}
-#----<< globals >>----
 
-#---->> SLOT+IUSE logic <<----
-IUSE="multislot nls nptl regression-test vanilla"
+#---->> LICENSE+SLOT+IUSE logic <<----
+
+if tc_version_is_at_least 4.6 ; then
+       LICENSE="GPL-3+ LGPL-3+ || ( GPL-3+ libgcc libstdc++ 
gcc-runtime-library-exception-3.1 ) FDL-1.3+"
+elif tc_version_is_at_least 4.4 ; then
+       LICENSE="GPL-3+ LGPL-3+ || ( GPL-3+ libgcc libstdc++ 
gcc-runtime-library-exception-3.1 ) FDL-1.2+"
+elif tc_version_is_at_least 4.3 ; then
+       LICENSE="GPL-3+ LGPL-3+ || ( GPL-3+ libgcc libstdc++ ) FDL-1.2+"
+elif tc_version_is_at_least 4.2 ; then
+       LICENSE="GPL-3+ LGPL-2.1+ || ( GPL-3+ libgcc libstdc++ ) FDL-1.2+"
+elif tc_version_is_at_least 3.3 ; then
+       LICENSE="GPL-2+ LGPL-2.1+ FDL-1.2+"
+else
+       LICENSE="GPL-2+ LGPL-2.1+ FDL-1.1+"
+fi
+
+IUSE="multislot regression-test vanilla"
+IUSE_DEF=( nls nptl )
 
 if [[ ${PN} != "kgcc64" && ${PN} != gcc-* ]] ; then
-       IUSE+=" altivec cxx fortran"
+       IUSE+=" altivec"
+       IUSE_DEF+=( cxx fortran )
        [[ -n ${PIE_VER} ]] && IUSE+=" nopie"
        [[ -n ${HTB_VER} ]] && IUSE+=" boundschecking"
        [[ -n ${D_VER}   ]] && IUSE+=" d"
        [[ -n ${SPECS_VER} ]] && IUSE+=" nossp"
-       tc_version_is_at_least 3 && IUSE+=" doc gcj gtk hardened multilib objc"
-       tc_version_is_at_least 4.0 && IUSE+=" objc-gc mudflap"
+       tc_version_is_at_least 3 && IUSE+=" doc gcj awt hardened multilib objc"
+       tc_version_is_at_least 4.0 && IUSE+=" objc-gc"
+       tc_version_is_between 4.0 4.9 && IUSE+=" mudflap"
        tc_version_is_at_least 4.1 && IUSE+=" libssp objc++"
-       tc_version_is_at_least 4.2 && IUSE+=" openmp"
+       tc_version_is_at_least 4.2 && IUSE_DEF+=( openmp )
        tc_version_is_at_least 4.3 && IUSE+=" fixed-point"
        tc_version_is_at_least 4.6 && IUSE+=" graphite"
-       tc_version_is_at_least 4.6 && IUSE+=" lto"
-       tc_version_is_at_least 4.7 && IUSE+=" go rap"
+       tc_version_is_at_least 4.7 && IUSE+=" go"
+       tc_version_is_at_least 4.8 && IUSE_DEF+=( sanitize )
 fi
 
+[[ ${EAPI:-0} != 0 ]] && IUSE_DEF=( "${IUSE_DEF[@]/#/+}" )
+IUSE+=" ${IUSE_DEF[*]}"
+
 # Support upgrade paths here or people get pissed
 if use multislot ; then
        SLOT="${GCC_CONFIG_VER}"
 else
        SLOT="${GCC_BRANCH_VER}"
 fi
-#----<< SLOT+IUSE logic >>----
 
 #---->> DEPEND <<----
+
 RDEPEND="sys-libs/zlib
-       nls? ( sys-devel/gettext )"
+       nls? ( virtual/libintl )"
 
 tc_version_is_at_least 3 && RDEPEND+=" virtual/libiconv"
 
@@ -165,6 +198,7 @@ fi
 DEPEND="${RDEPEND}
        >=sys-devel/bison-1.875
        >=sys-devel/flex-2.5.4
+       nls? ( sys-devel/gettext )
        regression-test? (
                >=dev-util/dejagnu-1.4.4
                >=sys-devel/autogen-5.5.4
@@ -187,11 +221,10 @@ if in_iuse gcj ; then
        "
        tc_version_is_at_least 3.4 && GCJ_GTK_DEPS+=" x11-libs/pango"
        tc_version_is_at_least 4.2 && GCJ_DEPS+=" app-arch/zip app-arch/unzip"
-       DEPEND+=" gcj? ( gtk? ( ${GCJ_GTK_DEPS} ) ${GCJ_DEPS} )"
+       DEPEND+=" gcj? ( awt? ( ${GCJ_GTK_DEPS} ) ${GCJ_DEPS} )"
 fi
 
 PDEPEND=">=sys-devel/gcc-config-1.7"
-#----<< DEPEND >>----
 
 #---->> S + SRC_URI essentials <<----
 
@@ -207,6 +240,13 @@ S=$(
        fi
 )
 
+gentoo_urls() {
+       local devspace="HTTP~vapier/dist/URI HTTP~rhill/dist/URI
+       HTTP~halcy0n/patches/URI HTTP~zorry/patches/gcc/URI"
+       devspace=${devspace//HTTP/http:\/\/dev.gentoo.org\/}
+       echo mirror://gentoo/$1 ${devspace//URI/$1}
+}
+
 # This function handles the basics of setting the SRC_URI for a gcc ebuild.
 # To use, set SRC_URI with:
 #
@@ -265,14 +305,6 @@ S=$(
 #                      ten Brugge's bounds-checking patches. If you want to 
use a patch
 #                      for an older gcc version with a new gcc, make sure you 
set
 #                      HTB_GCC_VER to that version of gcc.
-#
-gentoo_urls() {
-       local devspace="HTTP~vapier/dist/URI HTTP~dirtyepic/dist/URI
-       HTTP~halcy0n/patches/URI HTTP~zorry/patches/gcc/URI"
-       devspace=${devspace//HTTP/http:\/\/dev.gentoo.org\/}
-       echo mirror://gentoo/$1 ${devspace//URI/$1}
-}
-
 get_gcc_src_uri() {
        export PATCH_GCC_VER=${PATCH_GCC_VER:-${GCC_RELEASE_VER}}
        export UCLIBC_GCC_VER=${UCLIBC_GCC_VER:-${PATCH_GCC_VER}}
@@ -282,11 +314,14 @@ get_gcc_src_uri() {
 
        # Set where to download gcc itself depending on whether we're using a
        # prerelease, snapshot, or release tarball.
-       if [[ -n ${PRERELEASE} ]] ; then
+       if [[ ${PV} == *9999* ]] ; then
+               # Nothing to do w/git snapshots.
+               :
+       elif [[ -n ${PRERELEASE} ]] ; then
                
GCC_SRC_URI="ftp://gcc.gnu.org/pub/gcc/prerelease-${PRERELEASE}/gcc-${PRERELEASE}.tar.bz2";
        elif [[ -n ${SNAPSHOT} ]] ; then
                
GCC_SRC_URI="ftp://gcc.gnu.org/pub/gcc/snapshots/${SNAPSHOT}/gcc-${SNAPSHOT}.tar.bz2";
-       elif [[ ${PV} != *9999* ]] ; then
+       else
                
GCC_SRC_URI="mirror://gnu/gcc/gcc-${GCC_PV}/gcc-${GCC_RELEASE_VER}.tar.bz2"
                # we want all branch updates to be against the main release
                [[ -n ${BRANCH_UPDATE} ]] && \
@@ -330,440 +365,162 @@ get_gcc_src_uri() {
 
        echo "${GCC_SRC_URI}"
 }
+
 SRC_URI=$(get_gcc_src_uri)
-#---->> S + SRC_URI essentials >>----
 
-#---->> support checks <<----
+#---->> pkg_pretend <<----
 
-# Grab a variable from the build system (taken from linux-info.eclass)
-get_make_var() {
-       local var=$1 makefile=${2:-${WORKDIR}/build/Makefile}
-       echo -e "e:\\n\\t@echo \$(${var})\\ninclude ${makefile}" | \
-               r=${makefile%/*} emake --no-print-directory -s -f - 2>/dev/null
-}
-XGCC() { get_make_var GCC_FOR_TARGET ; }
+toolchain_pkg_pretend() {
+       if [[ -n ${PRERELEASE}${SNAPSHOT} || ${PV} == *9999* ]] &&
+          [[ -z ${I_PROMISE_TO_SUPPLY_PATCHES_WITH_BUGS} ]] ; then
+               die "Please \`export I_PROMISE_TO_SUPPLY_PATCHES_WITH_BUGS=1\` 
or define it" \
+                       "in your make.conf if you want to use this version."
+       fi
 
-# The gentoo piessp patches allow for 3 configurations:
-# 1) PIE+SSP by default
-# 2) PIE by default
-# 3) SSP by default
-hardened_gcc_works() {
-       if [[ $1 == "pie" ]] ; then
-               # $gcc_cv_ld_pie is unreliable as it simply take the output of
-               # `ld --help | grep -- -pie`, that reports the option in all 
cases, also if
-               # the loader doesn't actually load the resulting executables.
-               # To avoid breakage, blacklist FreeBSD here at least
-               [[ ${CTARGET} == *-freebsd* ]] && return 1
+       [[ -z ${UCLIBC_VER} ]] && [[ ${CTARGET} == *-uclibc* ]] && \
+               die "Sorry, this version does not support uClibc"
 
-               want_pie || return 1
-               use_if_iuse nopie && return 1
-               hardened_gcc_is_stable pie
-               return $?
-       elif [[ $1 == "ssp" ]] ; then
-               [[ -n ${SPECS_VER} ]] || return 1
-               use_if_iuse nossp && return 1
-               hardened_gcc_is_stable ssp
-               return $?
-       else
-               # laziness ;)
-               hardened_gcc_works pie || return 1
-               hardened_gcc_works ssp || return 1
-               return 0
+       if ! use_if_iuse cxx ; then
+               use_if_iuse go && ewarn 'Go requires a C++ compiler, disabled 
due to USE="-cxx"'
+               use_if_iuse objc++ && ewarn 'Obj-C++ requires a C++ compiler, 
disabled due to USE="-cxx"'
+               use_if_iuse gcj && ewarn 'GCJ requires a C++ compiler, disabled 
due to USE="-cxx"'
        fi
+
+       want_minispecs
 }
 
-hardened_gcc_is_stable() {
-       local tocheck
-       if [[ $1 == "pie" ]] ; then
-               if [[ ${CTARGET} == *-uclibc* ]] ; then
-                       tocheck=${PIE_UCLIBC_STABLE}
-               else
-                       tocheck=${PIE_GLIBC_STABLE}
-               fi
-       elif [[ $1 == "ssp" ]] ; then
-               if [[ ${CTARGET} == *-uclibc* ]] ; then
-                       tocheck=${SSP_UCLIBC_STABLE}
-               else
-                       tocheck=${SSP_STABLE}
-               fi
+#---->> pkg_setup <<----
+
+toolchain_pkg_setup() {
+       case "${EAPI:-0}" in
+               0|1|2|3)    toolchain_pkg_pretend ;;
+       esac
+
+       # we dont want to use the installed compiler's specs to build gcc
+       unset GCC_SPECS
+       unset LANGUAGES #265283
+}
+
+#---->> src_unpack <<----
+
+toolchain_src_unpack() {
+       if [[ ${PV} == *9999* ]]; then
+               git-2_src_unpack
        else
-               die "hardened_gcc_stable needs to be called with pie or ssp"
+               gcc_quick_unpack
        fi
 
-       has $(tc-arch) ${tocheck} && return 0
-       return 1
+       case ${EAPI:-0} in
+               0|1)   toolchain_src_prepare ;;
+       esac
 }
 
-want_pie() {
-       ! use hardened && [[ -n ${PIE_VER} ]] && use nopie && return 1
-       [[ -n ${PIE_VER} ]] && [[ -n ${SPECS_VER} ]] && return 0
-       tc_version_is_at_least 4.3.2 && return 1
-       [[ -z ${PIE_VER} ]] && return 1
-       use !nopie && return 0
-       return 1
-}
+gcc_quick_unpack() {
+       pushd "${WORKDIR}" > /dev/null
+       export PATCH_GCC_VER=${PATCH_GCC_VER:-${GCC_RELEASE_VER}}
+       export UCLIBC_GCC_VER=${UCLIBC_GCC_VER:-${PATCH_GCC_VER}}
+       export PIE_GCC_VER=${PIE_GCC_VER:-${GCC_RELEASE_VER}}
+       export HTB_GCC_VER=${HTB_GCC_VER:-${GCC_RELEASE_VER}}
+       export SPECS_GCC_VER=${SPECS_GCC_VER:-${GCC_RELEASE_VER}}
 
-want_minispecs() {
-       if tc_version_is_at_least 4.3.2 && use hardened ; then
-               if ! want_pie ; then
-                       ewarn "PIE_VER or SPECS_VER is not defined in the GCC 
ebuild."
-               elif use vanilla ; then
-                       ewarn "You will not get hardened features if you have 
the vanilla USE-flag."
-               elif use nopie && use nossp ; then
-                       ewarn "You will not get hardened features if you have 
the nopie and nossp USE-flag."
-               elif ! hardened_gcc_works ; then
-                       ewarn "Your $(tc-arch) arch is not supported."
-               else
-                       return 0
+       if [[ -n ${GCC_A_FAKEIT} ]] ; then
+               unpack ${GCC_A_FAKEIT}
+       elif [[ -n ${PRERELEASE} ]] ; then
+               unpack gcc-${PRERELEASE}.tar.bz2
+       elif [[ -n ${SNAPSHOT} ]] ; then
+               unpack gcc-${SNAPSHOT}.tar.bz2
+       elif [[ ${PV} != *9999* ]] ; then
+               unpack gcc-${GCC_RELEASE_VER}.tar.bz2
+               # We want branch updates to be against a release tarball
+               if [[ -n ${BRANCH_UPDATE} ]] ; then
+                       pushd "${S}" > /dev/null
+                       epatch 
"${DISTDIR}"/gcc-${GCC_RELEASE_VER}-branch-update-${BRANCH_UPDATE}.patch.bz2
+                       popd > /dev/null
                fi
-               ewarn "Hope you know what you are doing. Hardened will not 
work."
-               return 0
        fi
-       return 1
-}
 
-# This is to make sure we don't accidentally try to enable support for a
-# language that doesnt exist. GCC 3.4 supports f77, while 4.0 supports f95, 
etc.
-#
-# Also add a hook so special ebuilds (kgcc64) can control which languages
-# exactly get enabled
-gcc-lang-supported() {
-       grep ^language=\"${1}\" "${S}"/gcc/*/config-lang.in > /dev/null || 
return 1
-       [[ -z ${TOOLCHAIN_ALLOWED_LANGS} ]] && return 0
-       has $1 ${TOOLCHAIN_ALLOWED_LANGS}
-}
-#----<< support checks >>----
+       if [[ -n ${D_VER} ]] && use d ; then
+               pushd "${S}"/gcc > /dev/null
+               unpack gdc-${D_VER}-src.tar.bz2
+               cd ..
+               ebegin "Adding support for the D language"
+               ./gcc/d/setup-gcc.sh >& "${T}"/dgcc.log
+               if ! eend $? ; then
+                       eerror "The D GCC package failed to apply"
+                       eerror "Please include this log file when posting a bug 
report:"
+                       eerror "  ${T}/dgcc.log"
+                       die "failed to include the D language"
+               fi
+               popd > /dev/null
+       fi
 
-#---->> specs + env.d logic <<----
+       [[ -n ${PATCH_VER} ]] && \
+               unpack gcc-${PATCH_GCC_VER}-patches-${PATCH_VER}.tar.bz2
 
-# configure to build with the hardened GCC specs as the default
-make_gcc_hard() {
-       # defaults to enable for all hardened toolchains
-       local gcc_hard_flags="-DEFAULT_RELRO -DEFAULT_BIND_NOW"
+       [[ -n ${UCLIBC_VER} ]] && \
+               unpack 
gcc-${UCLIBC_GCC_VER}-uclibc-patches-${UCLIBC_VER}.tar.bz2
 
-       if hardened_gcc_works ; then
-               einfo "Updating gcc to use automatic PIE + SSP building ..."
-               gcc_hard_flags+=" -DEFAULT_PIE_SSP"
-       elif hardened_gcc_works pie ; then
-               einfo "Updating gcc to use automatic PIE building ..."
-               ewarn "SSP has not been enabled by default"
-               gcc_hard_flags+=" -DEFAULT_PIE"
-       elif hardened_gcc_works ssp ; then
-               einfo "Updating gcc to use automatic SSP building ..."
-               ewarn "PIE has not been enabled by default"
-               gcc_hard_flags+=" -DEFAULT_SSP"
-       else
-               # do nothing if hardened isnt supported, but dont die either
-               ewarn "hardened is not supported for this arch in this gcc 
version"
-               ebeep
-               return 0
+       if want_pie ; then
+               if [[ -n ${PIE_CORE} ]] ; then
+                       unpack ${PIE_CORE}
+               else
+                       unpack gcc-${PIE_GCC_VER}-piepatches-v${PIE_VER}.tar.bz2
+               fi
+               [[ -n ${SPECS_VER} ]] && \
+                       unpack gcc-${SPECS_GCC_VER}-specs-${SPECS_VER}.tar.bz2
        fi
 
-       sed -i \
-               -e "/^HARD_CFLAGS = /s|=|= ${gcc_hard_flags} |" \
-               "${S}"/gcc/Makefile.in || die
+       use_if_iuse boundschecking && unpack 
"bounds-checking-gcc-${HTB_GCC_VER}-${HTB_VER}.patch.bz2"
 
-       # rebrand to make bug reports easier
-       BRANDING_GCC_PKGVERSION=${BRANDING_GCC_PKGVERSION/Gentoo/Gentoo 
Hardened}
+       popd > /dev/null
 }
 
-prefix_gcc_dynamic_loader() {
-       local dlf
+#---->> src_prepare <<----
 
-       case $(tc-arch) in
-       amd64) dlf=i386/linux64.h ;;
-       arm) dlf=arm/linux-eabi.h ;;
-       x86) dlf=i386/linux.h ;;
-        esac
+toolchain_src_prepare() {
+       export BRANDING_GCC_PKGVERSION="Gentoo ${GCC_PVR}"
+       cd "${S}"
 
-       eprefixify gcc/config/${dlf}
-}
+       if ! use vanilla ; then
+               if [[ -n ${PATCH_VER} ]] ; then
+                       guess_patch_type_in_dir "${WORKDIR}"/patch
+                       EPATCH_MULTI_MSG="Applying Gentoo patches ..." \
+                       epatch "${WORKDIR}"/patch
+                       BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION} 
p${PATCH_VER}"
+               fi
+               if [[ -n ${UCLIBC_VER} ]] ; then
+                       guess_patch_type_in_dir "${WORKDIR}"/uclibc
+                       EPATCH_MULTI_MSG="Applying uClibc patches ..." \
+                       epatch "${WORKDIR}"/uclibc
+               fi
+       fi
+       do_gcc_HTB_patches
+       do_gcc_PIE_patches
+       epatch_user
 
-create_gcc_env_entry() {
-       dodir /etc/env.d/gcc
-       local gcc_envd_base="/etc/env.d/gcc/${CTARGET}-${GCC_CONFIG_VER}"
+       if ( tc_version_is_at_least 4.8.2 || use hardened ) && ! use vanilla ; 
then
+               make_gcc_hard
+       fi
 
-       local gcc_specs_file
-       local gcc_envd_file="${ED}${gcc_envd_base}"
-       if [[ -z $1 ]] ; then
-               # I'm leaving the following commented out to remind me that it
-               # was an insanely -bad- idea. Stuff broke. GCC_SPECS isnt unset
-               # on chroot or in non-toolchain.eclass gcc ebuilds!
-               #gcc_specs_file="${LIBPATH}/specs"
-               gcc_specs_file=""
-       else
-               gcc_envd_file+="-$1"
-               gcc_specs_file="${EPREFIX}${LIBPATH}/$1.specs"
+       # install the libstdc++ python into the right location
+       # http://gcc.gnu.org/PR51368
+       if tc_version_is_between 4.5 4.7 ; then
+               sed -i \
+                       '/^pythondir =/s:=.*:= $(datadir)/python:' \
+                       "${S}"/libstdc++-v3/python/Makefile.in || die
        fi
 
-       # We want to list the default ABI's LIBPATH first so libtool
-       # searches that directory first.  This is a temporary
-       # workaround for libtool being stupid and using .la's from
-       # conflicting ABIs by using the first one in the search path
-       local ldpaths mosdirs
-       if tc_version_is_at_least 3.2 ; then
-               local mdir mosdir abi ldpath
-               for abi in $(get_all_abis TARGET) ; do
-                       mdir=$($(XGCC) $(get_abi_CFLAGS ${abi}) 
--print-multi-directory)
-                       ldpath=${EPREFIX}${LIBPATH}
-                       [[ ${mdir} != "." ]] && ldpath+="/${mdir}"
-                       ldpaths="${ldpath}${ldpaths:+:${ldpaths}}"
+       # make sure the pkg config files install into multilib dirs.
+       # since we configure with just one --libdir, we can't use that
+       # (as gcc itself takes care of building multilibs).  #435728
+       find "${S}" -name Makefile.in \
+               -exec sed -i 
'/^pkgconfigdir/s:=.*:=$(toolexeclibdir)/pkgconfig:' {} +
 
-                       mosdir=$($(XGCC) $(get_abi_CFLAGS ${abi}) 
-print-multi-os-directory)
-                       mosdirs="${mosdir}${mosdirs:+:${mosdirs}}"
-               done
-       else
-               # Older gcc's didn't do multilib, so logic is simple.
-               ldpaths=${EPREFIX}${LIBPATH}
-       fi
-
-       cat <<-EOF > ${gcc_envd_file}
-       PATH="${EPREFIX}${BINPATH}"
-       ROOTPATH="${EPREFIX}${BINPATH}"
-       GCC_PATH="${EPREFIX}${BINPATH}"
-       LDPATH="${ldpaths}"
-       MANPATH="${EPREFIX}${DATAPATH}/man"
-       INFOPATH="${EPREFIX}${DATAPATH}/info"
-       STDCXX_INCDIR="${STDCXX_INCDIR##*/}"
-       CTARGET="${CTARGET}"
-       GCC_SPECS="${gcc_specs_file}"
-       MULTIOSDIRS="${mosdirs}"
-       EOF
-}
-
-setup_minispecs_gcc_build_specs() {
-       # Setup the "build.specs" file for gcc 4.3 to use when building.
-       if hardened_gcc_works pie ; then
-               cat "${WORKDIR}"/specs/pie.specs >> "${WORKDIR}"/build.specs
-       fi
-       if hardened_gcc_works ssp ; then
-               for s in ssp sspall ; do
-                       cat "${WORKDIR}"/specs/${s}.specs >> 
"${WORKDIR}"/build.specs
-               done
-       fi
-       for s in nostrict znow ; do
-               cat "${WORKDIR}"/specs/${s}.specs >> "${WORKDIR}"/build.specs
-       done
-       export GCC_SPECS="${WORKDIR}"/build.specs
-}
-
-copy_minispecs_gcc_specs() {
-       # setup the hardenedno* specs files and the vanilla specs file.
-       if hardened_gcc_works ; then
-               create_gcc_env_entry hardenednopiessp
-       fi
-       if hardened_gcc_works pie ; then
-               create_gcc_env_entry hardenednopie
-       fi
-       if hardened_gcc_works ssp ; then
-               create_gcc_env_entry hardenednossp
-       fi
-       create_gcc_env_entry vanilla
-       insinto ${LIBPATH}
-       doins "${WORKDIR}"/specs/*.specs || die "failed to install specs"
-       # Build system specs file which, if it exists, must be a complete set of
-       # specs as it completely and unconditionally overrides the builtin 
specs.
-       if ! tc_version_is_at_least 4.4 ; then
-               $(XGCC) -dumpspecs > "${WORKDIR}"/specs/specs
-               cat "${WORKDIR}"/build.specs >> "${WORKDIR}"/specs/specs
-               doins "${WORKDIR}"/specs/specs || die "failed to install the 
specs file"
-       fi
-}
-
-#----<< specs + env.d logic >>----
-
-#---->> pkg_* <<----
-toolchain_pkg_setup() {
-       if [[ -n ${PRERELEASE}${SNAPSHOT} || ${PV} == *9999* ]] &&
-          [[ -z ${I_PROMISE_TO_SUPPLY_PATCHES_WITH_BUGS} ]] ;  then
-               die "Please \`export I_PROMISE_TO_SUPPLY_PATCHES_WITH_BUGS=1\` 
or define it" \
-                       "in your make.conf if you want to use this version."
-       fi
-
-       local realEPREFIX=$(python -c 'import os; 
print(os.path.realpath("'"${EPREFIX}"'"))')
-       if [[ ${EPREFIX} != ${realEPREFIX} ]] ; then
-               ewarn "Your \${EPREFIX} contains one or more symlinks.  GCC has 
a"
-               ewarn "bug which prevents it from working properly when there 
are"
-               ewarn "symlinks in your \${EPREFIX}."
-               ewarn "See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29831";
-       fi
-
-       # we dont want to use the installed compiler's specs to build gcc!
-       unset GCC_SPECS
-
-       if ! use_if_iuse cxx ; then
-               use_if_iuse go && ewarn 'Go requires a C++ compiler, disabled 
due to USE="-cxx"'
-               use_if_iuse objc++ && ewarn 'Obj-C++ requires a C++ compiler, 
disabled due to USE="-cxx"'
-               use_if_iuse gcj && ewarn 'GCJ requires a C++ compiler, disabled 
due to USE="-cxx"'
-       fi
-
-       want_minispecs
-
-       unset LANGUAGES #265283
-}
-
-toolchain_pkg_postinst() {
-       do_gcc_config
-
-       if ! is_crosscompile ; then
-               echo
-               ewarn "If you have issues with packages unable to locate 
libstdc++.la,"
-               ewarn "then try running 'fix_libtool_files.sh' on the old gcc 
versions."
-               echo
-               ewarn "You might want to review the GCC upgrade guide when 
moving between"
-               ewarn "major versions (like 4.2 to 4.3):"
-               ewarn "http://www.gentoo.org/doc/en/gcc-upgrading.xml";
-               echo
-
-               # Clean up old paths
-               rm -f "${EROOT}"/*/rcscripts/awk/fixlafiles.awk 
"${EROOT}"/sbin/fix_libtool_files.sh
-               rmdir "${EROOT}"/*/rcscripts{/awk,} 2>/dev/null
-
-               mkdir -p "${EROOT}"/usr/{share/gcc-data,sbin,bin}
-               cp "${EROOT}/${DATAPATH}"/fixlafiles.awk 
"${EROOT}"/usr/share/gcc-data/ || die
-               cp "${EROOT}/${DATAPATH}"/fix_libtool_files.sh 
"${EROOT}"/usr/sbin/ || die
-
-               # Since these aren't critical files and portage sucks with
-               # handling of binpkgs, don't require these to be found
-               cp "${EROOT}/${DATAPATH}"/c{89,99} "${EROOT}"/usr/bin/ 
2>/dev/null
-       fi
-
-       if use regression-test ; then
-               elog "Testsuite results have been installed into 
/usr/share/doc/${PF}/testsuite"
-               echo
-       fi
-}
-
-toolchain_pkg_postrm() {
-       # to make our lives easier (and saner), we do the fix_libtool stuff 
here.
-       # rather than checking SLOT's and trying in upgrade paths, we just see 
if
-       # the common libstdc++.la exists in the ${LIBPATH} of the gcc that we 
are
-       # unmerging.  if it does, that means this was a simple re-emerge.
-
-       # clean up the cruft left behind by cross-compilers
-       if is_crosscompile ; then
-               if [[ -z $(ls "${EROOT}"/etc/env.d/gcc/${CTARGET}* 2>/dev/null) 
]] ; then
-                       rm -f "${EROOT}"/etc/env.d/gcc/config-${CTARGET}
-                       rm -f "${EROOT}"/etc/env.d/??gcc-${CTARGET}
-                       rm -f 
"${EROOT}"/usr/bin/${CTARGET}-{gcc,{g,c}++}{,32,64}
-               fi
-               return 0
-       fi
-
-       # ROOT isnt handled by the script
-       [[ ${ROOT} != "/" ]] && return 0
-
-       if [[ ! -e ${EPREFIX}${LIBPATH}/libstdc++.so ]] ; then
-               # make sure the profile is sane during same-slot upgrade #289403
-               do_gcc_config
-
-               einfo "Running 'fix_libtool_files.sh ${GCC_RELEASE_VER}'"
-               "${EPREFIX}"/usr/sbin/fix_libtool_files.sh ${GCC_RELEASE_VER}
-               if [[ -n ${BRANCH_UPDATE} ]] ; then
-                       einfo "Running 'fix_libtool_files.sh 
${GCC_RELEASE_VER}-${BRANCH_UPDATE}'"
-                       "${EPREFIX}"/usr/sbin/fix_libtool_files.sh 
${GCC_RELEASE_VER}-${BRANCH_UPDATE}
-               fi
-       fi
-
-       return 0
-}
-
-#---->> pkg_* <<----
-
-#---->> src_* <<----
-
-guess_patch_type_in_dir() {
-       [[ -n $(ls "$1"/*.bz2 2>/dev/null) ]] \
-               && EPATCH_SUFFIX="patch.bz2" \
-               || EPATCH_SUFFIX="patch"
-}
-
-do_gcc_rename_java_bins() {
-       # bug #139918 - conflict between gcc and java-config-2 for ownership of
-       # /usr/bin/rmi{c,registry}.      Done with mv & sed rather than a patch
-       # because patches would be large (thanks to the rename of man files),
-       # and it's clear from the sed invocations that all that changes is the
-       # rmi{c,registry} names to grmi{c,registry} names.
-       # Kevin F. Quinn 2006-07-12
-       einfo "Renaming jdk executables rmic and rmiregistry to grmic and 
grmiregistry."
-       # 1) Move the man files if present (missing prior to gcc-3.4)
-       for manfile in rmic rmiregistry ; do
-               [[ -f ${S}/gcc/doc/${manfile}.1 ]] || continue
-               mv "${S}"/gcc/doc/${manfile}.1 "${S}"/gcc/doc/g${manfile}.1
-       done
-       # 2) Fixup references in the docs if present (mission prior to gcc-3.4)
-       for jfile in gcc/doc/gcj.info gcc/doc/grmic.1 gcc/doc/grmiregistry.1 
gcc/java/gcj.texi ; do
-               [[ -f ${S}/${jfile} ]] || continue
-               sed -i -e 's:rmiregistry:grmiregistry:g' "${S}"/${jfile} ||
-                       die "Failed to fixup file ${jfile} for rename to 
grmiregistry"
-               sed -i -e 's:rmic:grmic:g' "${S}"/${jfile} ||
-                       die "Failed to fixup file ${jfile} for rename to grmic"
-       done
-       # 3) Fixup Makefiles to build the changed executable names
-       #        These are present in all 3.x versions, and are the important 
bit
-       #        to get gcc to build with the new names.
-       for jfile in libjava/Makefile.am libjava/Makefile.in 
gcc/java/Make-lang.in ; do
-               sed -i -e 's:rmiregistry:grmiregistry:g' "${S}"/${jfile} ||
-                       die "Failed to fixup file ${jfile} for rename to 
grmiregistry"
-               # Careful with rmic on these files; it's also the name of a 
directory
-               # which should be left unchanged.  Replace occurrences of 
'rmic$',
-               # 'rmic_' and 'rmic '.
-               sed -i -e 's:rmic\([$_ ]\):grmic\1:g' "${S}"/${jfile} ||
-                       die "Failed to fixup file ${jfile} for rename to grmic"
-       done
-}
-
-toolchain_src_unpack() {
-       [[ -z ${UCLIBC_VER} ]] && [[ ${CTARGET} == *-uclibc* ]] && \
-               die "Sorry, this version does not support uClibc"
-
-       if [[ ${PV} == *9999* ]]; then
-               git-2_src_unpack
-       else
-               gcc_quick_unpack
-       fi
-
-       export BRANDING_GCC_PKGVERSION="Gentoo ${GCC_PVR}"
-       cd "${S}"
-
-       if ! use vanilla ; then
-               if [[ -n ${PATCH_VER} ]] ; then
-                       guess_patch_type_in_dir "${WORKDIR}"/patch
-                       EPATCH_MULTI_MSG="Applying Gentoo patches ..." \
-                       epatch "${WORKDIR}"/patch
-                       BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION} 
p${PATCH_VER}"
-               fi
-               if [[ -n ${UCLIBC_VER} ]] ; then
-                       guess_patch_type_in_dir "${WORKDIR}"/uclibc
-                       EPATCH_MULTI_MSG="Applying uClibc patches ..." \
-                       epatch "${WORKDIR}"/uclibc
-               fi
-       fi
-       do_gcc_HTB_patches
-       do_gcc_PIE_patches
-       epatch_user
-
-       use hardened && make_gcc_hard
-
-       # install the libstdc++ python into the right location
-       # http://gcc.gnu.org/PR51368
-       if tc_version_is_at_least 4.5 && ! tc_version_is_at_least 4.7 ; then
-               sed -i \
-                       '/^pythondir =/s:=.*:= $(datadir)/python:' \
-                       "${S}"/libstdc++-v3/python/Makefile.in || die
-       fi
-
-       # make sure the pkg config files install into multilib dirs.
-       # since we configure with just one --libdir, we can't use that
-       # (as gcc itself takes care of building multilibs).  #435728
-       find "${S}" -name Makefile.in \
-               -exec sed -i 
'/^pkgconfigdir/s:=.*:=$(toolexeclibdir)/pkgconfig:' {} +
-
-       # No idea when this first started being fixed, but let's go with 4.3.x 
for now
-       if ! tc_version_is_at_least 4.3 ; then
-               fix_files=""
-               for x in contrib/test_summary 
libstdc++-v3/scripts/check_survey.in ; do
-                       [[ -e ${x} ]] && fix_files="${fix_files} ${x}"
+       # No idea when this first started being fixed, but let's go with 4.3.x 
for now
+       if ! tc_version_is_at_least 4.3 ; then
+               fix_files=""
+               for x in contrib/test_summary 
libstdc++-v3/scripts/check_survey.in ; do
+                       [[ -e ${x} ]] && fix_files="${fix_files} ${x}"
                done
                ht_fix_file ${fix_files} */configure *.sh */Makefile.in
        fi
@@ -795,19 +552,19 @@ toolchain_src_unpack() {
 
        # disable --as-needed from being compiled into gcc specs
        # natively when using a gcc version < 3.4.4
-       # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14992
+       # http://gcc.gnu.org/PR14992
        if ! tc_version_is_at_least 3.4.4 ; then
                sed -i -e s/HAVE_LD_AS_NEEDED/USE_LD_AS_NEEDED/g 
"${S}"/gcc/config.in
        fi
 
        # In gcc 3.3.x and 3.4.x, rename the java bins to gcc-specific names
        # in line with gcc-4.
-       if tc_version_is_at_least 3.3 && ! tc_version_is_at_least 4.0 ; then
+       if tc_version_is_between 3.3 4.0 ; then
                do_gcc_rename_java_bins
        fi
 
        # Prevent libffi from being installed
-       if tc_version_is_at_least 3.0 && ! tc_version_is_at_least 4.8 ; then
+       if tc_version_is_between 3.0 4.8 ; then
                sed -i -e 's/\(install.*:\) install-.*recursive/\1/' 
"${S}"/libffi/Makefile.in || die
                sed -i -e 's/\(install-data-am:\).*/\1/' 
"${S}"/libffi/include/Makefile.in || die
        fi
@@ -820,8 +577,6 @@ toolchain_src_unpack() {
        # update configure files
        local f
        einfo "Fixing misc issues in configure files"
-       # TODO - check if we can drop this now that we don't gen info files
-       tc_version_is_at_least 4.1 && epatch 
"${GCC_FILESDIR}"/gcc-configure-texinfo.patch
        for f in $(grep -l 'autoconf version 2.13' $(find "${S}" -name 
configure)) ; do
                ebegin "  Updating ${f/${S}\/} [LANG]"
                patch "${f}" "${GCC_FILESDIR}"/gcc-configure-LANG.patch >& 
"${T}"/configure-patch.log \
@@ -830,6 +585,9 @@ toolchain_src_unpack() {
        done
        sed -i 's|A-Za-z0-9|[:alnum:]|g' "${S}"/gcc/*.awk #215828
 
+       # Prevent new texinfo from breaking old versions (see #198182, #464008)
+       tc_version_is_at_least 4.1 && epatch 
"${GCC_FILESDIR}"/gcc-configure-texinfo.patch
+
        if [[ -x contrib/gcc_update ]] ; then
                einfo "Touching generated files"
                ./contrib/gcc_update --touch | \
@@ -839,206 +597,250 @@ toolchain_src_unpack() {
        fi
 }
 
-gcc-abi-map() {
-       # Convert the ABI name we use in Gentoo to what gcc uses
-       local map=()
-       case ${CTARGET} in
-       mips*)   map=("o32 32" "n32 n32" "n64 64") ;;
-       x86_64*) map=("amd64 m64" "x86 m32" "x32 mx32") ;;
-       esac
+guess_patch_type_in_dir() {
+       [[ -n $(ls "$1"/*.bz2 2>/dev/null) ]] \
+               && EPATCH_SUFFIX="patch.bz2" \
+               || EPATCH_SUFFIX="patch"
+}
 
-       local m
-       for m in "${map[@]}" ; do
-               l=( ${m} )
-               [[ $1 == ${l[0]} ]] && echo ${l[1]} && break
-       done
+do_gcc_HTB_patches() {
+       use_if_iuse boundschecking || return 0
+
+       # modify the bounds checking patch with a regression patch
+       epatch "${WORKDIR}/bounds-checking-gcc-${HTB_GCC_VER}-${HTB_VER}.patch"
+       BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION}, 
HTB-${HTB_GCC_VER}-${HTB_VER}"
 }
 
-gcc-multilib-configure() {
-       if ! is_multilib ; then
-               confgcc+=( --disable-multilib )
-               # Fun times: if we are building for a target that has multiple
-               # possible ABI formats, and the user has told us to pick one
-               # that isn't the default, then not specifying it via the list
-               # below will break that on us.
+do_gcc_PIE_patches() {
+       want_pie || return 0
+       use vanilla && return 0
+
+       if tc_version_is_at_least 4.3.2 ; then
+               guess_patch_type_in_dir "${WORKDIR}"/piepatch/
+               EPATCH_MULTI_MSG="Applying pie patches ..." \
+               epatch "${WORKDIR}"/piepatch/
        else
-               confgcc+=( --enable-multilib )
-       fi
+               guess_patch_type_in_dir "${WORKDIR}"/piepatch/upstream
 
-       # translate our notion of multilibs into gcc's
-       local abi list
-       for abi in $(get_all_abis TARGET) ; do
-               local l=$(gcc-abi-map ${abi})
-               [[ -n ${l} ]] && list+=",${l}"
-       done
-       if [[ -n ${list} ]] ; then
-               case ${CTARGET} in
-               x86_64*)
-                       tc_version_is_at_least 4.7 && confgcc+=( 
--with-multilib-list=${list:1} )
-                       ;;
-               esac
+               # corrects startfile/endfile selection and shared/static/pie 
flag usage
+               EPATCH_MULTI_MSG="Applying upstream pie patches ..." \
+               epatch "${WORKDIR}"/piepatch/upstream
+               # adds non-default pie support (rs6000)
+               EPATCH_MULTI_MSG="Applying non-default pie patches ..." \
+               epatch "${WORKDIR}"/piepatch/nondef
+               # adds default pie support (rs6000 too) if DEFAULT_PIE[_SSP] is 
defined
+               EPATCH_MULTI_MSG="Applying default pie patches ..." \
+               epatch "${WORKDIR}"/piepatch/def
        fi
+
+       BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION}, pie-${PIE_VER}"
 }
 
-gcc-compiler-configure() {
-       gcc-multilib-configure
+# configure to build with the hardened GCC specs as the default
+make_gcc_hard() {
+       # we want to be able to control the pie patch logic via something other
+       # than ALL_CFLAGS...
+       sed -e '/^ALL_CFLAGS/iHARD_CFLAGS = ' \
+               -e 's|^ALL_CFLAGS = |ALL_CFLAGS = $(HARD_CFLAGS) |' \
+               -i "${S}"/gcc/Makefile.in
+       # Need to add HARD_CFLAGS to ALL_CXXFLAGS on >= 4.7
+       if tc_version_is_at_least 4.7 ; then
+               sed -e '/^ALL_CXXFLAGS/iHARD_CFLAGS = ' \
+                        -e 's|^ALL_CXXFLAGS = |ALL_CXXFLAGS = $(HARD_CFLAGS) 
|' \
+                        -i "${S}"/gcc/Makefile.in
+       fi
 
-       if tc_version_is_at_least 4.0 ; then
-               if in_iuse mudflap ; then
-                       confgcc+=( $(use_enable mudflap libmudflap) )
+       # defaults to enable for all toolchains
+       local gcc_hard_flags=""
+       if use hardened ; then
+               if hardened_gcc_works ; then
+                       einfo "Updating gcc to use automatic PIE + SSP building 
..."
+                       gcc_hard_flags+=" -DEFAULT_PIE_SSP"
+               elif hardened_gcc_works pie ; then
+                       einfo "Updating gcc to use automatic PIE building ..."
+                       ewarn "SSP has not been enabled by default"
+                       gcc_hard_flags+=" -DEFAULT_PIE"
+               elif hardened_gcc_works ssp ; then
+                       einfo "Updating gcc to use automatic SSP building ..."
+                       ewarn "PIE has not been enabled by default"
+                       gcc_hard_flags+=" -DEFAULT_SSP"
                else
-                       confgcc+=( --disable-libmudflap )
+                       # do nothing if hardened is't supported, but don't die 
either
+                       ewarn "hardened is not supported for this arch in this 
gcc version"
+                       return 0
                fi
-
-               if use_if_iuse libssp ; then
-                       confgcc+=( --enable-libssp )
-               else
-                       export gcc_cv_libc_provides_ssp=yes
-                       confgcc+=( --disable-libssp )
+               # rebrand to make bug reports easier
+               BRANDING_GCC_PKGVERSION=${BRANDING_GCC_PKGVERSION/Gentoo/Gentoo 
Hardened}
+       else
+               if hardened_gcc_works ssp ; then
+                       einfo "Updating gcc to use automatic SSP building ..."
+                       gcc_hard_flags+=" -DEFAULT_SSP"
                fi
+       fi
 
-               # If we want hardened support with the newer piepatchset for 
>=gcc 4.4
-               if tc_version_is_at_least 4.4 && want_minispecs ; then
-                       confgcc+=( $(use_enable hardened esp) )
-               fi
+       sed -i \
+               -e "/^HARD_CFLAGS = /s|=|= ${gcc_hard_flags} |" \
+               "${S}"/gcc/Makefile.in || die
 
-               if tc_version_is_at_least 4.2 ; then
-                       if in_iuse openmp ; then
-                               # Make sure target has pthreads support. 
#326757 #335883
-                               # There shouldn't be a chicken&egg problem here 
as openmp won't
-                               # build without a C library, and you can't 
build that w/out
-                               # already having a compiler ...
-                               if ! is_crosscompile || \
-                                  $(tc-getCPP ${CTARGET}) -E - <<<"#include 
<pthread.h>" >& /dev/null
-                               then
-                                       confgcc+=( $(use_enable openmp libgomp) 
)
-                               else
-                                       # Force disable as the configure script 
can be dumb #359855
-                                       confgcc+=( --disable-libgomp )
-                               fi
-                       else
-                               # For gcc variants where we don't want openmp 
(e.g. kgcc)
-                               confgcc+=( --disable-libgomp )
-                       fi
-               fi
+}
 
-               # Stick the python scripts in their own slotted directory
-               # bug #279252
-               #
-               #  --with-python-dir=DIR
-               #  Specifies where to install the Python modules used for 
aot-compile. DIR
-               #  should not include the prefix used in installation. For 
example, if the
-               #  Python modules are to be installed in 
/usr/lib/python2.5/site-packages,
-               #  then --with-python-dir=/lib/python2.5/site-packages should 
be passed.
-               #
-               # This should translate into 
"/share/gcc-data/${CTARGET}/${GCC_CONFIG_VER}/python"
-               if tc_version_is_at_least 4.4 ; then
-                       confgcc+=( 
--with-python-dir=${DATAPATH/$PREFIX/}/python )
+# This is a historical wart.  The original Gentoo/amd64 port used:
+#    lib32 - 32bit binaries (x86)
+#    lib64 - 64bit binaries (x86_64)
+#    lib   - "native" binaries (a symlink to lib64)
+# Most other distros use the logic (including mainline gcc):
+#    lib   - 32bit binaries (x86)
+#    lib64 - 64bit binaries (x86_64)
+# Over time, Gentoo is migrating to the latter form.
+#
+# Unfortunately, due to distros picking the lib32 behavior, newer gcc
+# versions will dynamically detect whether to use lib or lib32 for its
+# 32bit multilib.  So, to keep the automagic from getting things wrong
+# while people are transitioning from the old style to the new style,
+# we always set the MULTILIB_OSDIRNAMES var for relevant targets.
+setup_multilib_osdirnames() {
+       is_multilib || return 0
+
+       local config
+       local libdirs="../lib64 ../lib32"
+
+       # this only makes sense for some Linux targets
+       case ${CTARGET} in
+       x86_64*-linux*)    config="i386" ;;
+       powerpc64*-linux*) config="rs6000" ;;
+       sparc64*-linux*)   config="sparc" ;;
+       s390x*-linux*)     config="s390" ;;
+       *)                     return 0 ;;
+       esac
+       config+="/t-linux64"
+
+       local sed_args=()
+       if tc_version_is_at_least 4.6 ; then
+               sed_args+=( -e 's:$[(]call if_multiarch[^)]*[)]::g' )
+       fi
+       if [[ ${SYMLINK_LIB} == "yes" ]] ; then
+               einfo "updating multilib directories to be: ${libdirs}"
+               if tc_version_is_at_least 4.6.4 || tc_version_is_at_least 4.7 ; 
then
+                       sed_args+=( -e 
'/^MULTILIB_OSDIRNAMES.*lib32/s:[$][(]if.*):../lib32:' )
+               else
+                       sed_args+=( -e "/^MULTILIB_OSDIRNAMES/s:=.*:= 
${libdirs}:" )
                fi
+       else
+               einfo "using upstream multilib; disabling lib32 autodetection"
+               sed_args+=( -r -e 's:[$][(]if.*,(.*)[)]:\1:' )
        fi
+       sed -i "${sed_args[@]}" "${S}"/gcc/config/${config} || die
+}
 
-       # Enable build warnings by default with cross-compilers when system
-       # paths are included (e.g. via -I flags).
-       is_crosscompile && confgcc+=( --enable-poison-system-directories )
+gcc_version_patch() {
+       # gcc-4.3+ has configure flags (whoo!)
+       tc_version_is_at_least 4.3 && return 0
 
-       # For newer versions of gcc, use the default ("release"), because no
-       # one (even upstream apparently) tests with it disabled. #317217
-       if tc_version_is_at_least 4 || [[ -n ${GCC_CHECKS_LIST} ]] ; then
-               confgcc+=( --enable-checking=${GCC_CHECKS_LIST:-release} )
+       local version_string=${GCC_CONFIG_VER}
+       [[ -n ${BRANCH_UPDATE} ]] && version_string+=" ${BRANCH_UPDATE}"
+
+       einfo "patching gcc version: ${version_string} 
(${BRANDING_GCC_PKGVERSION})"
+
+       local gcc_sed=( -e 's:gcc\.gnu\.org/bugs\.html:bugs\.gentoo\.org/:' )
+       if grep -qs VERSUFFIX "${S}"/gcc/version.c ; then
+               gcc_sed+=( -e "/VERSUFFIX \"\"/s:\"\":\" 
(${BRANDING_GCC_PKGVERSION})\":" )
        else
-               confgcc+=( --disable-checking )
+               version_string="${version_string} (${BRANDING_GCC_PKGVERSION})"
+               gcc_sed+=( -e "/const char version_string\[\] = /s:= \".*\":= 
\"${version_string}\":" )
        fi
+       sed -i "${gcc_sed[@]}" "${S}"/gcc/version.c || die
+}
 
-       # GTK+ is preferred over xlib in 3.4.x (xlib is unmaintained
-       # right now). Much thanks to <[email protected]> for the heads up.
-       # Travis Tilley <[email protected]>        (11 Jul 2004)
-       if ! is_gcj ; then
-               confgcc+=( --disable-libgcj )
-       elif use gtk ; then
-               confgcc+=( --enable-java-awt=gtk )
-       fi
+do_gcc_rename_java_bins() {
+       # bug #139918 - conflict between gcc and java-config-2 for ownership of
+       # /usr/bin/rmi{c,registry}.      Done with mv & sed rather than a patch
+       # because patches would be large (thanks to the rename of man files),
+       # and it's clear from the sed invocations that all that changes is the
+       # rmi{c,registry} names to grmi{c,registry} names.
+       # Kevin F. Quinn 2006-07-12
+       einfo "Renaming jdk executables rmic and rmiregistry to grmic and 
grmiregistry."
+       # 1) Move the man files if present (missing prior to gcc-3.4)
+       for manfile in rmic rmiregistry ; do
+               [[ -f ${S}/gcc/doc/${manfile}.1 ]] || continue
+               mv "${S}"/gcc/doc/${manfile}.1 "${S}"/gcc/doc/g${manfile}.1
+       done
+       # 2) Fixup references in the docs if present (mission prior to gcc-3.4)
+       for jfile in gcc/doc/gcj.info gcc/doc/grmic.1 gcc/doc/grmiregistry.1 
gcc/java/gcj.texi ; do
+               [[ -f ${S}/${jfile} ]] || continue
+               sed -i -e 's:rmiregistry:grmiregistry:g' "${S}"/${jfile} ||
+                       die "Failed to fixup file ${jfile} for rename to 
grmiregistry"
+               sed -i -e 's:rmic:grmic:g' "${S}"/${jfile} ||
+                       die "Failed to fixup file ${jfile} for rename to grmic"
+       done
+       # 3) Fixup Makefiles to build the changed executable names
+       #        These are present in all 3.x versions, and are the important 
bit
+       #        to get gcc to build with the new names.
+       for jfile in libjava/Makefile.am libjava/Makefile.in 
gcc/java/Make-lang.in ; do
+               sed -i -e 's:rmiregistry:grmiregistry:g' "${S}"/${jfile} ||
+                       die "Failed to fixup file ${jfile} for rename to 
grmiregistry"
+               # Careful with rmic on these files; it's also the name of a 
directory
+               # which should be left unchanged.  Replace occurrences of 
'rmic$',
+               # 'rmic_' and 'rmic '.
+               sed -i -e 's:rmic\([$_ ]\):grmic\1:g' "${S}"/${jfile} ||
+                       die "Failed to fixup file ${jfile} for rename to grmic"
+       done
+}
 
-       # allow gcc to search for clock funcs in the main C lib.
-       # if it can't find them, then tough cookies -- we aren't
-       # going to link in -lrt to all C++ apps.  #411681
-       if tc_version_is_at_least 4.4 && is_cxx ; then
-               confgcc+=( --enable-libstdcxx-time )
-       fi
+#---->> src_configure <<----
 
-       # newer gcc versions like to bootstrap themselves with C++,
-       # so we need to manually disable it ourselves
-       if tc_version_is_at_least 4.7 && ! is_cxx ; then
-               confgcc+=( --disable-build-with-cxx 
--disable-build-poststage1-with-cxx )
-       fi
+toolchain_src_configure() {
+       downgrade_arch_flags
+       gcc_do_filter_flags
 
-       # newer gcc's come with libquadmath, but only fortran uses
-       # it, so auto punt it when we don't care
-       if tc_version_is_at_least 4.6 && ! is_fortran ; then
-               confgcc+=( --disable-libquadmath )
+       einfo "CFLAGS=\"${CFLAGS}\""
+       einfo "CXXFLAGS=\"${CXXFLAGS}\""
+       einfo "LDFLAGS=\"${LDFLAGS}\""
+
+       # Force internal zip based jar script to avoid random
+       # issues with 3rd party jar implementations.  #384291
+       export JAR=no
+
+       # For hardened gcc 4.3 piepatchset to build the hardened specs
+       # file (build.specs) to use when building gcc.
+       if ! tc_version_is_at_least 4.4 && want_minispecs ; then
+               setup_minispecs_gcc_build_specs
        fi
 
-       local with_abi_map=()
-       case $(tc-arch) in
-       arm)    #264534 #414395
-               local a arm_arch=${CTARGET%%-*}
-               # Remove trailing endian variations first: eb el be bl b l
-               for a in e{b,l} {b,l}e b l ; do
-                       if [[ ${arm_arch} == *${a} ]] ; then
-                               arm_arch=${arm_arch%${a}}
-                               break
-                       fi
-               done
-               # Convert armv7{a,r,m} to armv7-{a,r,m}
-               [[ ${arm_arch} == armv7? ]] && arm_arch=${arm_arch/7/7-}
-               # See if this is a valid --with-arch flag
-               if (srcdir=${S}/gcc target=${CTARGET} with_arch=${arm_arch};
-                   . "${srcdir}"/config.gcc) &>/dev/null
-               then
-                       confgcc+=( --with-arch=${arm_arch} )
-               fi
+       local confgcc=( --host=${CHOST} )
+
+       if is_crosscompile || tc-is-cross-compiler ; then
+               # Straight from the GCC install doc:
+               # "GCC has code to correctly determine the correct value for 
target
+               # for nearly all native systems. Therefore, we highly recommend 
you
+               # not provide a configure target when configuring a native 
compiler."
+               confgcc+=( --target=${CTARGET} )
+       fi
+       [[ -n ${CBUILD} ]] && confgcc+=( --build=${CBUILD} )
 
-               # Make default mode thumb for microcontroller classes #418209
-               [[ ${arm_arch} == *-m ]] && confgcc+=( --with-mode=thumb )
+       confgcc+=(
+               --prefix="${PREFIX}"
+               --bindir="${BINPATH}"
+               --includedir="${INCLUDEPATH}"
+               --datadir="${DATAPATH}"
+               --mandir="${DATAPATH}/man"
+               --infodir="${DATAPATH}/info"
+               --with-gxx-include-dir="${STDCXX_INCDIR}"
+       )
 
-               # Enable hardvfp
-               if [[ $(tc-is-softfloat) == "no" ]] && \
-                  [[ ${CTARGET} == armv[67]* ]] && \
-                  tc_version_is_at_least 4.5
-               then
-                       # Follow the new arm hardfp distro standard by default
-                       confgcc+=( --with-float=hard )
-                       case ${CTARGET} in
-                       armv6*) confgcc+=( --with-fpu=vfp ) ;;
-                       armv7*) confgcc+=( --with-fpu=vfpv3-d16 ) ;;
-                       esac
-               fi
-               ;;
-       mips)
-               # Add --with-abi flags to set default ABI
-               confgcc+=( --with-abi=$(gcc-abi-map ${TARGET_DEFAULT_ABI}) )
-               ;;
-       amd64)
-               # drop the older/ABI checks once this get's merged into some
-               # version of gcc upstream
-               if tc_version_is_at_least 4.7 && has x32 $(get_all_abis TARGET) 
; then
-                       confgcc+=( --with-abi=$(gcc-abi-map 
${TARGET_DEFAULT_ABI}) )
-               fi
-               ;;
-       x86)
-               # Default arch for x86 is normally i386, lets give it a bump
-               # since glibc will do so based on CTARGET anyways
-               confgcc+=( --with-arch=${CTARGET%%-*} )
-               ;;
-       hppa)
-               # Enable sjlj exceptions for backward compatibility on hppa
-               [[ ${GCCMAJOR} == "3" ]] && confgcc+=( --enable-sjlj-exceptions 
)
-               ;;
-       ppc)
-               # Set up defaults based on current CFLAGS
-               is-flagq -mfloat-gprs=double && confgcc+=( --enable-e500-double 
)
-               [[ ${CTARGET//_/-} == *-e500v2-* ]] && confgcc+=( 
--enable-e500-double )
-               ;;
-       esac
+       # Stick the python scripts in their own slotted directory (bug #279252)
+       #
+       #  --with-python-dir=DIR
+       #  Specifies where to install the Python modules used for aot-compile. 
DIR
+       #  should not include the prefix used in installation. For example, if 
the
+       #  Python modules are to be installed in 
/usr/lib/python2.5/site-packages,
+       #  then --with-python-dir=/lib/python2.5/site-packages should be passed.
+       #
+       # This should translate into 
"/share/gcc-data/${CTARGET}/${GCC_CONFIG_VER}/python"
+       if tc_version_is_at_least 4.4 ; then
+               confgcc+=( --with-python-dir=${DATAPATH/$PREFIX/}/python )
+       fi
+
+       ### language options
 
        local GCC_LANG="c"
        is_cxx && GCC_LANG+=",c++"
@@ -1052,7 +854,6 @@ gcc-compiler-configure() {
                fi
                is_objcxx && GCC_LANG+=",obj-c++"
        fi
-       is_treelang && GCC_LANG+=",treelang"
 
        # fortran support just got sillier! the lang value can be f77 for
        # fortran77, f95 for fortran95, or just plain old fortran for the
@@ -1064,98 +865,73 @@ gcc-compiler-configure() {
        # We do NOT want 'ADA support' in here!
        # is_ada && GCC_LANG+=",ada"
 
-       einfo "configuring for GCC_LANG: ${GCC_LANG}"
        confgcc+=( --enable-languages=${GCC_LANG} )
-}
 
-gcc_do_configure() {
-       local confgcc=(
-               --prefix="${EPREFIX}${PREFIX}"
-               --bindir="${EPREFIX}${BINPATH}"
-               --includedir="${EPREFIX}${INCLUDEPATH}"
-               --datadir="${EPREFIX}${DATAPATH}"
-               --mandir="${EPREFIX}${DATAPATH}/man"
-               --infodir="${EPREFIX}${DATAPATH}/info"
-               --with-gxx-include-dir="${EPREFIX}${STDCXX_INCDIR}"
+       ### general options
+
+       confgcc+=(
+               --enable-obsolete
+               --enable-secureplt
+               --disable-werror
+               --with-system-zlib
        )
-       # On Darwin we need libdir to be set in order to get correct install 
names
-       # for things like libobjc-gnu, libgcj and libfortran.  If we enable it 
on
-       # non-Darwin we screw up the behaviour this eclass relies on.  We in
-       # particular need this over --libdir for bug #255315.
-       [[ ${CTARGET} == *-darwin* ]] && \
-               confgcc+=( --enable-version-specific-runtime-libs )
 
-       # All our cross-compile logic goes here !  woo !
-       confgcc+=( --host=${CHOST} )
-       if is_crosscompile || tc-is-cross-compiler ; then
-               # Straight from the GCC install doc:
-               # "GCC has code to correctly determine the correct value for 
target
-               # for nearly all native systems. Therefore, we highly recommend 
you
-               # not provide a configure target when configuring a native 
compiler."
-               confgcc+=( --target=${CTARGET} )
+       if use nls ; then
+               confgcc+=( --enable-nls --without-included-gettext )
+       else
+               confgcc+=( --disable-nls )
        fi
-       [[ -n ${CBUILD} ]] && confgcc+=( --build=${CBUILD} )
-
-       # ppc altivec support
-       confgcc+=( $(use_enable altivec) )
 
-       # gcc has fixed-point arithmetic support in 4.3 for mips targets that 
can
-       # significantly increase compile time by several hours.  This will allow
-       # users to control this feature in the event they need the support.
-       tc_version_is_at_least 4.3 && confgcc+=( $(use_enable fixed-point) )
+       tc_version_is_at_least 3.4 || confgcc+=( --disable-libunwind-exceptions 
)
 
-       # graphite was added in 4.4 but we only support it in 4.6+ due to 
external
-       # library issues.  4.6/4.7 uses cloog-ppl which is a fork of CLooG with 
a
-       # PPL backend.  4.8+ uses upstream CLooG with the ISL backend.  We 
install
-       # cloog-ppl into a non-standard location to prevent collisions.
-       if tc_version_is_at_least 4.8 ; then
-               confgcc+=( $(use_with graphite cloog) )
-               use graphite && confgcc+=( --disable-isl-version-check )
-       elif tc_version_is_at_least 4.6 ; then
-               confgcc+=( $(use_with graphite cloog) )
-               confgcc+=( $(use_with graphite ppl) )
-               use graphite && confgcc+=( 
--with-cloog-include="${EPREFIX}"/usr/include/cloog-ppl )
-               use graphite && confgcc+=( --disable-ppl-version-check )
-       elif tc_version_is_at_least 4.4 ; then
-               confgcc+=( --without-cloog )
-               confgcc+=( --without-ppl )
+       # Use the default ("release") checking because upstream usually neglects
+       # to test "disabled" so it has a history of breaking. #317217
+       if tc_version_is_at_least 4 || [[ -n ${GCC_CHECKS_LIST} ]] ; then
+               confgcc+=( --enable-checking=${GCC_CHECKS_LIST:-release} )
+       else
+               confgcc+=( --disable-checking )
        fi
 
-       if tc_version_is_at_least 4.6 ; then
-               confgcc+=( $(use_enable lto) )
-       elif tc_version_is_at_least 4.5 ; then
-               confgcc+=( --disable-lto )
-       fi
+       # Branding
+       tc_version_is_at_least 4.3 && confgcc+=(
+               --with-bugurl=https://bugs.gentoo.org/
+               --with-pkgversion="${BRANDING_GCC_PKGVERSION}"
+       )
 
-       case $(tc-is-softfloat) in
-       yes)    confgcc+=( --with-float=soft ) ;;
-       softfp) confgcc+=( --with-float=softfp ) ;;
-       *)
-               # If they've explicitly opt-ed in, do hardfloat,
-               # otherwise let the gcc default kick in.
-               [[ ${CTARGET//_/-} == *-hardfloat-* ]] \
-                       && confgcc+=( --with-float=hard )
-               ;;
-       esac
+       # If we want hardened support with the newer piepatchset for >=gcc 4.4
+       if tc_version_is_at_least 4.4 && want_minispecs ; then
+               confgcc+=( $(use_enable hardened esp) )
+       fi
 
-       # Native Language Support
-       if use nls ; then
-               confgcc+=( --enable-nls --without-included-gettext )
-       else
-               confgcc+=( --disable-nls )
+       # allow gcc to search for clock funcs in the main C lib.
+       # if it can't find them, then tough cookies -- we aren't
+       # going to link in -lrt to all C++ apps.  #411681
+       if tc_version_is_at_least 4.4 && is_cxx ; then
+               confgcc+=( --enable-libstdcxx-time )
        fi
 
-       # reasonably sane globals (hopefully)
-       confgcc+=(
-               --with-system-zlib
-               --enable-obsolete
-               --disable-werror
-               --enable-secureplt
-       )
+       # # Turn on the -Wl,--build-id flag by default for ELF targets. #525942
+       # # This helps with locating debug files.
+       # case ${CTARGET} in
+       # *-linux-*|*-elf|*-eabi)
+       #       tc_version_is_at_least 4.5 && confgcc+=(
+       #               --enable-linker-build-id
+       #       )
+       #       ;;
+       # esac
 
-       gcc-compiler-configure || die
+       # newer gcc versions like to bootstrap themselves with C++,
+       # so we need to manually disable it ourselves
+       if tc_version_is_between 4.7 4.8 && ! is_cxx ; then
+               confgcc+=( --disable-build-with-cxx 
--disable-build-poststage1-with-cxx )
+       fi
 
+       ### Cross-compiler options
        if is_crosscompile ; then
+               # Enable build warnings by default with cross-compilers when 
system
+               # paths are included (e.g. via -I flags).
+               confgcc+=( --enable-poison-system-directories )
+
                # When building a stage1 cross-compiler (just C compiler), we 
have to
                # disable a bunch of features or gcc goes boom
                local needed_libc=""
@@ -1181,9 +957,6 @@ gcc_do_configure() {
                *-w64-mingw*)    needed_libc=mingw64-runtime;;
                mingw*|*-mingw*) needed_libc=mingw-runtime;;
                avr)                     confgcc+=( --enable-shared 
--disable-threads );;
-               *-apple-darwin*) confgcc+=( 
--with-sysroot="${EPREFIX}"${PREFIX}/${CTARGET} );;
-               *-solaris*)      confgcc+=( 
--with-sysroot="${EPREFIX}"${PREFIX}/${CTARGET} );;
-               *-freebsd*)      confgcc+=( 
--with-sysroot="${EPREFIX}"${PREFIX}/${CTARGET} );;
                esac
                if [[ -n ${needed_libc} ]] ; then
                        local confgcc_no_libc=( --disable-shared )
@@ -1197,10 +970,10 @@ gcc_do_configure() {
                        elif built_with_use --hidden --missing false 
${CATEGORY}/${needed_libc} crosscompile_opts_headers-only ; then
                                confgcc+=(
                                        "${confgcc_no_libc[@]}"
-                                       
--with-sysroot="${EPREFIX}"${PREFIX}/${CTARGET}
+                                       --with-sysroot=${PREFIX}/${CTARGET}
                                )
                        else
-                               confgcc+=( 
--with-sysroot="${EPREFIX}"${PREFIX}/${CTARGET} )
+                               confgcc+=( --with-sysroot=${PREFIX}/${CTARGET} )
                        fi
                fi
 
@@ -1217,23 +990,8 @@ gcc_do_configure() {
                *)
                        confgcc+=( --enable-threads=posix ) ;;
                esac
-
-               if use prefix ; then
-                       # should be /usr, because it's the path to search 
includes for,
-                       # which is unrelated to TOOLCHAIN_PREFIX, a.k.a. PREFIX
-                       confgcc+=( --with-local-prefix="${TPREFIX}"/usr )
-               fi
-
-               if use rap ; then
-                       # use sysroot of toolchain to get currect include and 
library at
-                       # compile time
-                       confgcc+=( --with-sysroot="${EPREFIX}" )
-
-                       # when doing cross compiling native for target, the 
build sysroot
-                       # should be pointed to SYSROOT of the cross compiler.
-                       tc-is-cross-compiler && confgcc+=( 
--with-build-sysroot="${ROOT}" )
-               fi
        fi
+
        # __cxa_atexit is "essential for fully standards-compliant handling of
        # destructors", but apparently requires glibc.
        case ${CTARGET} in
@@ -1242,8 +1000,8 @@ gcc_do_configure() {
                        --disable-__cxa_atexit
                        $(use_enable nptl tls)
                )
-               [[ ${GCCMAJOR}.${GCCMINOR} == 3.3 ]] && confgcc+=( 
--enable-sjlj-exceptions )
-               if tc_version_is_at_least 3.4 && ! tc_version_is_at_least 4.3 ; 
then
+               tc_version_is_between 3.3 3.4 && confgcc+=( 
--enable-sjlj-exceptions )
+               if tc_version_is_between 3.4 4.3 ; then
                        confgcc+=( --enable-clocale=uclibc )
                fi
                ;;
@@ -1264,28 +1022,186 @@ gcc_do_configure() {
                ;;
        esac
 
-       tc_version_is_at_least 3.4 || confgcc+=( --disable-libunwind-exceptions 
)
+       ### arch options
+
+       gcc-multilib-configure
+
+       # ppc altivec support
+       confgcc+=( $(use_enable altivec) )
+
+       # gcc has fixed-point arithmetic support in 4.3 for mips targets that 
can
+       # significantly increase compile time by several hours.  This will allow
+       # users to control this feature in the event they need the support.
+       tc_version_is_at_least 4.3 && confgcc+=( $(use_enable fixed-point) )
+
+       case $(tc-is-softfloat) in
+       yes)    confgcc+=( --with-float=soft ) ;;
+       softfp) confgcc+=( --with-float=softfp ) ;;
+       *)
+               # If they've explicitly opt-ed in, do hardfloat,
+               # otherwise let the gcc default kick in.
+               case ${CTARGET//_/-} in
+               *-hardfloat-*|*eabihf) confgcc+=( --with-float=hard ) ;;
+               esac
+       esac
+
+       local with_abi_map=()
+       case $(tc-arch) in
+       arm)    #264534 #414395
+               local a arm_arch=${CTARGET%%-*}
+               # Remove trailing endian variations first: eb el be bl b l
+               for a in e{b,l} {b,l}e b l ; do
+                       if [[ ${arm_arch} == *${a} ]] ; then
+                               arm_arch=${arm_arch%${a}}
+                               break
+                       fi
+               done
+               # Convert armv7{a,r,m} to armv7-{a,r,m}
+               [[ ${arm_arch} == armv7? ]] && arm_arch=${arm_arch/7/7-}
+               # See if this is a valid --with-arch flag
+               if (srcdir=${S}/gcc target=${CTARGET} with_arch=${arm_arch};
+                   . "${srcdir}"/config.gcc) &>/dev/null
+               then
+                       confgcc+=( --with-arch=${arm_arch} )
+               fi
+
+               # Make default mode thumb for microcontroller classes #418209
+               [[ ${arm_arch} == *-m ]] && confgcc+=( --with-mode=thumb )
+
+               # Enable hardvfp
+               if [[ $(tc-is-softfloat) == "no" ]] && \
+                  [[ ${CTARGET} == armv[67]* ]] && \
+                  tc_version_is_at_least 4.5
+               then
+                       # Follow the new arm hardfp distro standard by default
+                       confgcc+=( --with-float=hard )
+                       case ${CTARGET} in
+                       armv6*) confgcc+=( --with-fpu=vfp ) ;;
+                       armv7*) confgcc+=( --with-fpu=vfpv3-d16 ) ;;
+                       esac
+               fi
+               ;;
+       mips)
+               # Add --with-abi flags to set default ABI
+               confgcc+=( --with-abi=$(gcc-abi-map ${TARGET_DEFAULT_ABI}) )
+               ;;
+       amd64)
+               # drop the older/ABI checks once this get's merged into some
+               # version of gcc upstream
+               if tc_version_is_at_least 4.7 && has x32 $(get_all_abis TARGET) 
; then
+                       confgcc+=( --with-abi=$(gcc-abi-map 
${TARGET_DEFAULT_ABI}) )
+               fi
+               ;;
+       x86)
+               # Default arch for x86 is normally i386, lets give it a bump
+               # since glibc will do so based on CTARGET anyways
+               confgcc+=( --with-arch=${CTARGET%%-*} )
+               ;;
+       hppa)
+               # Enable sjlj exceptions for backward compatibility on hppa
+               [[ ${GCCMAJOR} == "3" ]] && confgcc+=( --enable-sjlj-exceptions 
)
+               ;;
+       ppc)
+               # Set up defaults based on current CFLAGS
+               is-flagq -mfloat-gprs=double && confgcc+=( --enable-e500-double 
)
+               [[ ${CTARGET//_/-} == *-e500v2-* ]] && confgcc+=( 
--enable-e500-double )
+               ;;
+       esac
+
+       # if the target can do biarch (-m32/-m64), enable it.  overhead should
+       # be small, and should simplify building of 64bit kernels in a 32bit
+       # userland by not needing sys-devel/kgcc64.  #349405
+       case $(tc-arch) in
+       ppc|ppc64) tc_version_is_at_least 3.4 && confgcc+=( 
--enable-targets=all ) ;;
+       sparc)     tc_version_is_at_least 4.4 && confgcc+=( 
--enable-targets=all ) ;;
+       amd64|x86) tc_version_is_at_least 4.3 && confgcc+=( 
--enable-targets=all ) ;;
+       esac
+
+       # On Darwin we need libdir to be set in order to get correct install 
names
+       # for things like libobjc-gnu, libgcj and libfortran.  If we enable it 
on
+       # non-Darwin we screw up the behaviour this eclass relies on.  We in
+       # particular need this over --libdir for bug #255315.
+       [[ ${CTARGET} == *-darwin* ]] && \
+               confgcc+=( --enable-version-specific-runtime-libs )
+
+       ### library options
+
+       if ! is_gcj ; then
+               confgcc+=( --disable-libgcj )
+       elif use awt ; then
+               confgcc+=( --enable-java-awt=gtk )
+       fi
+
+       if tc_version_is_at_least 4.2 ; then
+               if in_iuse openmp ; then
+                       # Make sure target has pthreads support. #326757 #335883
+                       # There shouldn't be a chicken & egg problem here as 
openmp won't
+                       # build without a C library, and you can't build that 
w/out
+                       # already having a compiler ...
+                       if ! is_crosscompile || \
+                          $(tc-getCPP ${CTARGET}) -E - <<<"#include 
<pthread.h>" >& /dev/null
+                       then
+                               confgcc+=( $(use_enable openmp libgomp) )
+                       else
+                               # Force disable as the configure script can be 
dumb #359855
+                               confgcc+=( --disable-libgomp )
+                       fi
+               else
+                       # For gcc variants where we don't want openmp (e.g. 
kgcc)
+                       confgcc+=( --disable-libgomp )
+               fi
+       fi
+
+       if tc_version_is_at_least 4.0 ; then
+               if in_iuse mudflap ; then
+                       confgcc+=( $(use_enable mudflap libmudflap) )
+               else
+                       confgcc+=( --disable-libmudflap )
+               fi
+
+               if use_if_iuse libssp ; then
+                       confgcc+=( --enable-libssp )
+               else
+                       if hardened_gcc_is_stable ssp; then
+                               export gcc_cv_libc_provides_ssp=yes
+                       fi
+                       confgcc+=( --disable-libssp )
+               fi
 
-       # if the target can do biarch (-m32/-m64), enable it.  overhead should
-       # be small, and should simplify building of 64bit kernels in a 32bit
-       # userland by not needing sys-devel/kgcc64.  #349405
-       if ! use prefix ; then
-       case $(tc-arch) in
-       ppc|ppc64) tc_version_is_at_least 3.4 && confgcc+=( 
--enable-targets=all ) ;;
-       sparc)     tc_version_is_at_least 4.4 && confgcc+=( 
--enable-targets=all ) ;;
-       amd64|x86) tc_version_is_at_least 4.3 && confgcc+=( 
--enable-targets=all ) ;;
-       esac
        fi
 
-       tc_version_is_at_least 4.3 && confgcc+=(
-               --with-bugurl=http://bugs.gentoo.org/
-               --with-pkgversion="${BRANDING_GCC_PKGVERSION}"
-       )
+       # newer gcc's come with libquadmath, but only fortran uses
+       # it, so auto punt it when we don't care
+       if tc_version_is_at_least 4.6 && ! is_fortran ; then
+               confgcc+=( --disable-libquadmath )
+       fi
 
-       confgcc+=(
-               "$@"
-               ${EXTRA_ECONF}
-       )
+       if tc_version_is_at_least 4.6 ; then
+               confgcc+=( --enable-lto )
+       elif tc_version_is_at_least 4.5 ; then
+               confgcc+=( --disable-lto )
+       fi
+
+       # graphite was added in 4.4 but we only support it in 4.6+ due to 
external
+       # library issues.  4.6/4.7 uses cloog-ppl which is a fork of CLooG with 
a
+       # PPL backend.  4.8+ uses upstream CLooG with the ISL backend.  We 
install
+       # cloog-ppl into a non-standard location to prevent collisions.
+       if tc_version_is_at_least 4.8 ; then
+               confgcc+=( $(use_with graphite cloog) )
+               use graphite && confgcc+=( --disable-isl-version-check )
+       elif tc_version_is_at_least 4.6 ; then
+               confgcc+=( $(use_with graphite cloog) )
+               confgcc+=( $(use_with graphite ppl) )
+               use graphite && confgcc+=( 
--with-cloog-include=/usr/include/cloog-ppl )
+               use graphite && confgcc+=( --disable-ppl-version-check )
+       elif tc_version_is_at_least 4.4 ; then
+               confgcc+=( --without-cloog )
+               confgcc+=( --without-ppl )
+       fi
+
+       if tc_version_is_at_least 4.8 ; then
+               confgcc+=( $(use_enable sanitize libsanitizer) )
+       fi
 
        # Disable gcc info regeneration -- it ships with generated info pages
        # already.  Our custom version/urls/etc... trigger it.  #464008
@@ -1297,14 +1213,17 @@ gcc_do_configure() {
        # killing the 32bit builds which want /usr/lib.
        export ac_cv_have_x='have_x=yes ac_x_includes= ac_x_libraries='
 
+       confgcc+=( "$@" ${EXTRA_ECONF} )
+
        # Nothing wrong with a good dose of verbosity
        echo
-       einfo "EPREFIX:                 ${EPREFIX}"
-       einfo "PREFIX:                  ${PREFIX}"
-       einfo "BINPATH:                 ${BINPATH}"
-       einfo "LIBPATH:                 ${LIBPATH}"
-       einfo "DATAPATH:                ${DATAPATH}"
-       einfo "STDCXX_INCDIR:   ${STDCXX_INCDIR}"
+       einfo "PREFIX:          ${PREFIX}"
+       einfo "BINPATH:         ${BINPATH}"
+       einfo "LIBPATH:         ${LIBPATH}"
+       einfo "DATAPATH:        ${DATAPATH}"
+       einfo "STDCXX_INCDIR:   ${STDCXX_INCDIR}"
+       echo
+       einfo "Languages:       ${GCC_LANG}"
        echo
        einfo "Configuring GCC with: ${confgcc[@]//--/\n\t--}"
        echo
@@ -1322,135 +1241,146 @@ gcc_do_configure() {
        popd > /dev/null
 }
 
-has toolchain_death_notice ${EBUILD_DEATH_HOOKS} || EBUILD_DEATH_HOOKS+=" 
toolchain_death_notice"
-toolchain_death_notice() {
-       pushd "${WORKDIR}"/build >/dev/null
-       tar jcf gcc-build-logs.tar.bz2 $(find -name config.log)
-       eerror
-       eerror "Please include ${PWD}/gcc-build-logs.tar.bz2 in your bug report"
-       eerror
-       popd >/dev/null
-}
+# Replace -m flags unsupported by the version being built with the best
+# available equivalent
+downgrade_arch_flags() {
+       local arch bver i isa myarch mytune rep ver
 
-# This function accepts one optional argument, the make target to be used.
-# If ommitted, gcc_do_make will try to guess whether it should use all,
-# profiledbootstrap, or bootstrap-lean depending on CTARGET and arch. An
-# example of how to use this function:
-#
-#      gcc_do_make all-target-libstdc++-v3
-#
-# In addition to the target to be used, the following variables alter the
-# behavior of this function:
-#
-#      LDFLAGS
-#                      Flags to pass to ld
-#
-#      STAGE1_CFLAGS
-#                      CFLAGS to use during stage1 of a gcc bootstrap
-#
-#      BOOT_CFLAGS
-#                      CFLAGS to use during stages 2+3 of a gcc bootstrap.
-#
-# Travis Tilley <[email protected]> (04 Sep 2004)
-#
-gcc_do_make() {
-       # Set make target to $1 if passed
-       [[ -n $1 ]] && GCC_MAKE_TARGET=$1
-       # default target
-       if is_crosscompile || tc-is-cross-compiler ; then
-               # 3 stage bootstrapping doesnt quite work when you cant run the
-               # resulting binaries natively ^^;
-               GCC_MAKE_TARGET=${GCC_MAKE_TARGET-all}
-       else
-               GCC_MAKE_TARGET=${GCC_MAKE_TARGET-bootstrap-lean}
-       fi
+       bver=${1:-${GCC_BRANCH_VER}}
+       [[ $(gcc-version) < ${bver} ]] && return 0
+       [[ $(tc-arch) != amd64 && $(tc-arch) != x86 ]] && return 0
 
-       # the gcc docs state that parallel make isnt supported for the
-       # profiledbootstrap target, as collisions in profile collecting may 
occur.
-       # boundschecking also seems to introduce parallel build issues.
-       if [[ ${GCC_MAKE_TARGET} == "profiledbootstrap" ]] ||
-          use_if_iuse boundschecking
-       then
-               export MAKEOPTS="${MAKEOPTS} -j1"
-       fi
+       myarch=$(get-flag march)
+       mytune=$(get-flag mtune)
 
-       if [[ ${GCC_MAKE_TARGET} == "all" ]] ; then
-               STAGE1_CFLAGS=${STAGE1_CFLAGS-"${CFLAGS}"}
-       elif [[ $(gcc-version) == "3.4" && ${GCC_BRANCH_VER} == "3.4" ]] && 
gcc-specs-ssp ; then
-               # See bug #79852
-               STAGE1_CFLAGS=${STAGE1_CFLAGS-"-O2"}
+       # If -march=native isn't supported we have to tease out the actual arch
+       if [[ ${myarch} == native || ${mytune} == native ]] ; then
+               if [[ ${bver} < 4.2 ]] ; then
+                       arch=$($(tc-getCC) -march=native -v -E -P - </dev/null 
2>&1 \
+                               | sed -rn "/cc1.*-march/s:.*-march=([^ 
']*).*:\1:p")
+                       replace-cpu-flags native ${arch}
+               fi
        fi
 
-       if is_crosscompile; then
-               # In 3.4, BOOT_CFLAGS is never used on a crosscompile...
-               # but I'll leave this in anyways as someone might have had
-               # some reason for putting it in here... --eradicator
-               BOOT_CFLAGS=${BOOT_CFLAGS-"-O2"}
-       else
-               # we only want to use the system's CFLAGS if not building a
-               # cross-compiler.
-               BOOT_CFLAGS=${BOOT_CFLAGS-"$(get_abi_CFLAGS 
${TARGET_DEFAULT_ABI}) ${CFLAGS}"}
-       fi
+       # Handle special -mtune flags
+       [[ ${mytune} == intel && ${bver} < 4.9 ]] && replace-cpu-flags intel 
generic
+       [[ ${mytune} == generic && ${bver} < 4.2 ]] && filter-flags '-mtune=*'
+       [[ ${mytune} == x86-64 ]] && filter-flags '-mtune=*'
+       [[ ${bver} < 3.4 ]] && filter-flags '-mtune=*'
+
+       # "added" "arch" "replacement"
+       local archlist=(
+               4.9 bdver4 bdver3
+               4.9 bonnell atom
+               4.9 broadwell core-avx2
+               4.9 haswell core-avx2
+               4.9 ivybridge core-avx-i
+               4.9 nehalem corei7
+               4.9 sandybridge corei7-avx
+               4.9 silvermont corei7
+               4.9 westmere corei7
+               4.8 bdver3 bdver2
+               4.8 btver2 btver1
+               4.7 bdver2 bdver1
+               4.7 core-avx2 core-avx-i
+               4.6 bdver1 amdfam10
+               4.6 btver1 amdfam10
+               4.6 core-avx-i core2
+               4.6 corei7 core2
+               4.6 corei7-avx core2
+               4.5 atom core2
+               4.3 amdfam10 k8
+               4.3 athlon64-sse3 k8
+               4.3 barcelona k8
+               4.3 core2 nocona
+               4.3 geode k6-2 # gcc.gnu.org/PR41989#c22
+               4.3 k8-sse3 k8
+               4.3 opteron-sse3 k8
+               3.4 athlon-fx x86-64
+               3.4 athlon64 x86-64
+               3.4 c3-2 c3
+               3.4 k8 x86-64
+               3.4 opteron x86-64
+               3.4 pentium-m pentium3
+               3.4 pentium3m pentium3
+               3.4 pentium4m pentium4
+       )
 
-       pushd "${WORKDIR}"/build >/dev/null
+       for ((i = 0; i < ${#archlist[@]}; i += 3)) ; do
+               myarch=$(get-flag march)
+               mytune=$(get-flag mtune)
 
-       # we "undef" T because the GCC makefiles use this variable, and if it's 
set
-       # in the environment (like Portage does) the build fails, bug #286494
-       emake \
-               T= \
-               LDFLAGS="${LDFLAGS}" \
-               STAGE1_CFLAGS="${STAGE1_CFLAGS}" \
-               LIBPATH="${EPREFIX}${LIBPATH}" \
-               BOOT_CFLAGS="${BOOT_CFLAGS}" \
-               ${GCC_MAKE_TARGET} \
-               || die "emake failed with ${GCC_MAKE_TARGET}"
+               ver=${archlist[i]}
+               arch=${archlist[i + 1]}
+               rep=${archlist[i + 2]}
 
-       if ! is_crosscompile && use cxx && use_if_iuse doc ; then
-               if type -p doxygen > /dev/null ; then
-                       if tc_version_is_at_least 4.3 ; then
-                               cd "${CTARGET}"/libstdc++-v3/doc
-                               emake doc-man-doxygen || ewarn "failed to make 
docs"
-                       elif tc_version_is_at_least 3.0 ; then
-                               cd "${CTARGET}"/libstdc++-v3
-                               emake doxygen-man || ewarn "failed to make docs"
-                       fi
+               [[ ${myarch} != ${arch} && ${mytune} != ${arch} ]] && continue
+
+               if [[ ${ver} > ${bver} ]] ; then
+                       einfo "Replacing ${myarch} (added in gcc ${ver}) with 
${rep}..."
+                       [[ ${myarch} == ${arch} ]] && replace-cpu-flags 
${myarch} ${rep}
+                       [[ ${mytune} == ${arch} ]] && replace-cpu-flags 
${mytune} ${rep}
+                       continue
                else
-                       ewarn "Skipping libstdc++ manpage generation since you 
don't have doxygen installed"
+                       break
                fi
-       fi
+       done
 
-       popd >/dev/null
+       # we only check -mno* here since -m* get removed by strip-flags later on
+       local isalist=(
+               4.9 -mno-sha
+               4.9 -mno-avx512pf
+               4.9 -mno-avx512f
+               4.9 -mno-avx512er
+               4.9 -mno-avx512cd
+               4.8 -mno-xsaveopt
+               4.8 -mno-xsave
+               4.8 -mno-rtm
+               4.8 -mno-fxsr
+               4.7 -mno-lzcnt
+               4.7 -mno-bmi2
+               4.7 -mno-avx2
+               4.6 -mno-tbm
+               4.6 -mno-rdrnd
+               4.6 -mno-fsgsbase
+               4.6 -mno-f16c
+               4.6 -mno-bmi
+               4.5 -mno-xop
+               4.5 -mno-movbe
+               4.5 -mno-lwp
+               4.5 -mno-fma4
+               4.4 -mno-pclmul
+               4.4 -mno-fma
+               4.4 -mno-avx
+               4.4 -mno-aes
+               4.3 -mno-ssse3
+               4.3 -mno-sse4a
+               4.3 -mno-sse4
+               4.3 -mno-sse4.2
+               4.3 -mno-sse4.1
+               4.3 -mno-popcnt
+               4.3 -mno-abm
+       )
+
+       for ((i = 0; i < ${#isalist[@]}; i += 2)) ; do
+               ver=${isalist[i]}
+               isa=${isalist[i + 1]}
+               [[ ${ver} > ${bver} ]] && filter-flags ${isa} ${isa/-m/-mno-}
+       done
 }
 
 gcc_do_filter_flags() {
-
        strip-flags
-
-       # In general gcc does not like optimization, and add -O2 where
-       # it is safe.  This is especially true for gcc 3.3 + 3.4
        replace-flags -O? -O2
 
-       # ... sure, why not?
-       strip-unsupported-flags
-
        # dont want to funk ourselves
        filter-flags '-mabi*' -m31 -m32 -m64
 
-       case ${GCC_BRANCH_VER} in
-       3.2|3.3)
-               replace-cpu-flags k8 athlon64 opteron i686 x86-64
-               replace-cpu-flags pentium-m pentium3m pentium3
-               case $(tc-arch) in
-                       amd64|x86) filter-flags '-mtune=*' ;;
-                       # in gcc 3.3 there is a bug on ppc64 where if -mcpu is 
used,
-                       # the compiler wrongly assumes a 32bit target
-                       ppc64) filter-flags "-mcpu=*";;
-               esac
-               case $(tc-arch) in
-                       amd64) replace-cpu-flags core2 nocona;;
-                       x86)   replace-cpu-flags core2 prescott;;
-               esac
+       filter-flags -frecord-gcc-switches # 490738
+       filter-flags -mno-rtm -mno-htm # 506202
 
+       if tc_version_is_between 3.2 3.4 ; then
+               # XXX: this is so outdated it's barely useful, but it don't 
hurt...
                replace-cpu-flags G3 750
                replace-cpu-flags G4 7400
                replace-cpu-flags G5 7400
@@ -1460,47 +1390,41 @@ gcc_do_filter_flags() {
                filter-flags -f{no-,}unit-at-a-time -f{no-,}web 
-mno-tls-direct-seg-refs
                filter-flags -f{no-,}stack-protector{,-all}
                filter-flags -fvisibility-inlines-hidden -fvisibility=hidden
-               ;;
-       3.4|4.*)
+       fi
+
+       if tc_version_is_at_least 3.4 ; then
                case $(tc-arch) in
-                       x86|amd64) filter-flags '-mcpu=*';;
+                       amd64|x86)
+                               filter-flags '-mcpu=*'
+
+                               tc_version_is_between 4.4 4.5 && append-flags 
-mno-avx # 357287
+
+                               if tc_version_is_between 4.6 4.7 ; then
+                                       # https://bugs.gentoo.org/411333
+                                       # https://bugs.gentoo.org/466454
+                                       replace-cpu-flags c3-2 pentium2 
pentium3 pentium3m pentium-m i686
+                               fi
+                               ;;
+                       alpha)
+                               # https://bugs.gentoo.org/454426
+                               append-ldflags -Wl,--no-relax
+                               ;;
+                       sparc)
+                               # temporary workaround for random ICEs 
reproduced by multiple users
+                               # https://bugs.gentoo.org/457062
+                               tc_version_is_between 4.6 4.8 && MAKEOPTS+=" 
-j1"
+                               ;;
                        *-macos)
-                               # 
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25127
-                               [[ ${GCC_BRANCH_VER} == 4.0 || 
${GCC_BRANCH_VER}  == 4.1 ]] && \
+                               # http://gcc.gnu.org/PR25127
+                               tc_version_is_between 4.0 4.2 && \
                                        filter-flags '-mcpu=*' '-march=*' 
'-mtune=*'
-                       ;;
+                               ;;
                esac
-               ;;
-       esac
-
-       case ${GCC_BRANCH_VER} in
-       4.6)
-               # https://bugs.gentoo.org/411333
-               # https://bugs.gentoo.org/466454
-               replace-cpu-flags c3-2 pentium2 pentium3 pentium3m pentium-m 
i686
-               ;;
-       esac
-
-       # TODO: Move to gcc_do_make()
+       fi
 
-       # CFLAGS logic (verified with 3.4.3):
-       # CFLAGS:
-       #       This conflicts when creating a crosscompiler, so set to a sane
-       #         default in this case:
-       #       used in ./configure and elsewhere for the native compiler
-       #       used by gcc when creating libiberty.a
-       #       used by xgcc when creating libstdc++ (and probably others)!
-       #         this behavior should be removed...
-       #
-       # CXXFLAGS:
-       #       used by xgcc when creating libstdc++
-       #
-       # STAGE1_CFLAGS (not used in creating a crosscompile gcc):
-       #       used by ${CHOST}-gcc for building stage1 compiler
-       #
-       # BOOT_CFLAGS (not used in creating a crosscompile gcc):
-       #       used by xgcc for building stage2/3 compiler
+       strip-unsupported-flags
 
+       # these are set here so we have something sane at configure time
        if is_crosscompile ; then
                # Set this to something sane for both native and target
                CFLAGS="-O2 -pipe"
@@ -1514,39 +1438,154 @@ gcc_do_filter_flags() {
        export GCJFLAGS=${GCJFLAGS:-${CFLAGS}}
 }
 
-toolchain_src_compile() {
-       gcc_do_filter_flags
-       einfo "CFLAGS=\"${CFLAGS}\""
-       einfo "CXXFLAGS=\"${CXXFLAGS}\""
+setup_minispecs_gcc_build_specs() {
+       # Setup the "build.specs" file for gcc 4.3 to use when building.
+       if hardened_gcc_works pie ; then
+               cat "${WORKDIR}"/specs/pie.specs >> "${WORKDIR}"/build.specs
+       fi
+       if hardened_gcc_works ssp ; then
+               for s in ssp sspall ; do
+                       cat "${WORKDIR}"/specs/${s}.specs >> 
"${WORKDIR}"/build.specs
+               done
+       fi
+       for s in nostrict znow ; do
+               cat "${WORKDIR}"/specs/${s}.specs >> "${WORKDIR}"/build.specs
+       done
+       export GCC_SPECS="${WORKDIR}"/build.specs
+}
 
-       # Force internal zip based jar script to avoid random
-       # issues with 3rd party jar implementations.  #384291
-       export JAR=no
+gcc-multilib-configure() {
+       if ! is_multilib ; then
+               confgcc+=( --disable-multilib )
+               # Fun times: if we are building for a target that has multiple
+               # possible ABI formats, and the user has told us to pick one
+               # that isn't the default, then not specifying it via the list
+               # below will break that on us.
+       else
+               confgcc+=( --enable-multilib )
+       fi
 
-       # For hardened gcc 4.3 piepatchset to build the hardened specs
-       # file (build.specs) to use when building gcc.
-       if ! tc_version_is_at_least 4.4 && want_minispecs ; then
-               setup_minispecs_gcc_build_specs
+       # translate our notion of multilibs into gcc's
+       local abi list
+       for abi in $(get_all_abis TARGET) ; do
+               local l=$(gcc-abi-map ${abi})
+               [[ -n ${l} ]] && list+=",${l}"
+       done
+       if [[ -n ${list} ]] ; then
+               case ${CTARGET} in
+               x86_64*)
+                       tc_version_is_at_least 4.7 && confgcc+=( 
--with-multilib-list=${list:1} )
+                       ;;
+               esac
        fi
-       # Build in a separate build tree
-       mkdir -p "${WORKDIR}"/build
-       pushd "${WORKDIR}"/build > /dev/null
+}
+
+gcc-abi-map() {
+       # Convert the ABI name we use in Gentoo to what gcc uses
+       local map=()
+       case ${CTARGET} in
+       mips*)   map=("o32 32" "n32 n32" "n64 64") ;;
+       x86_64*) map=("amd64 m64" "x86 m32" "x32 mx32") ;;
+       esac
+
+       local m
+       for m in "${map[@]}" ; do
+               l=( ${m} )
+               [[ $1 == ${l[0]} ]] && echo ${l[1]} && break
+       done
+}
+
+#----> src_compile <----
 
-       einfo "Configuring ${PN} ..."
-       gcc_do_configure
+toolchain_src_compile() {
+       case ${EAPI:-0} in
+               0|1)   toolchain_src_configure ;;
+       esac
 
        touch "${S}"/gcc/c-gperf.h
 
        # Do not make manpages if we do not have perl ...
-       [[ ! -x ${EPREFIX}/usr/bin/perl ]] \
+       [[ ! -x /usr/bin/perl ]] \
                && find "${WORKDIR}"/build -name '*.[17]' | xargs touch
 
-       einfo "Compiling ${PN} ..."
-       gcc_do_make ${GCC_MAKE_TARGET}
+       gcc_do_make ${GCC_MAKE_TARGET}
+}
+
+gcc_do_make() {
+       # This function accepts one optional argument, the make target to be 
used.
+       # If omitted, gcc_do_make will try to guess whether it should use all,
+       # or bootstrap-lean depending on CTARGET and arch.
+       # An example of how to use this function:
+       #
+       #       gcc_do_make all-target-libstdc++-v3
+
+       [[ -n ${1} ]] && GCC_MAKE_TARGET=${1}
+
+       # default target
+       if is_crosscompile || tc-is-cross-compiler ; then
+               # 3 stage bootstrapping doesnt quite work when you cant run the
+               # resulting binaries natively ^^;
+               GCC_MAKE_TARGET=${GCC_MAKE_TARGET-all}
+       else
+               GCC_MAKE_TARGET=${GCC_MAKE_TARGET-bootstrap-lean}
+       fi
+
+       # Older versions of GCC could not do profiledbootstrap in parallel due 
to
+       # collisions with profiling info.
+       # boundschecking also seems to introduce parallel build issues.
+       if [[ ${GCC_MAKE_TARGET} == "profiledbootstrap" ]] || use_if_iuse 
boundschecking ; then
+               ! tc_version_is_at_least 4.6 && export MAKEOPTS="${MAKEOPTS} 
-j1"
+       fi
+
+       if [[ ${GCC_MAKE_TARGET} == "all" ]] ; then
+               STAGE1_CFLAGS=${STAGE1_CFLAGS-"${CFLAGS}"}
+       elif [[ $(gcc-version) == "3.4" && ${GCC_BRANCH_VER} == "3.4" ]] && 
gcc-specs-ssp ; then
+               # See bug #79852
+               STAGE1_CFLAGS=${STAGE1_CFLAGS-"-O2"}
+       fi
+
+       if is_crosscompile; then
+               # In 3.4, BOOT_CFLAGS is never used on a crosscompile...
+               # but I'll leave this in anyways as someone might have had
+               # some reason for putting it in here... --eradicator
+               BOOT_CFLAGS=${BOOT_CFLAGS-"-O2"}
+       else
+               # we only want to use the system's CFLAGS if not building a
+               # cross-compiler.
+               BOOT_CFLAGS=${BOOT_CFLAGS-"$(get_abi_CFLAGS 
${TARGET_DEFAULT_ABI}) ${CFLAGS}"}
+       fi
+
+       einfo "Compiling ${PN} (${GCC_MAKE_TARGET})..."
+
+       pushd "${WORKDIR}"/build >/dev/null
+
+       emake \
+               LDFLAGS="${LDFLAGS}" \
+               STAGE1_CFLAGS="${STAGE1_CFLAGS}" \
+               LIBPATH="${LIBPATH}" \
+               BOOT_CFLAGS="${BOOT_CFLAGS}" \
+               ${GCC_MAKE_TARGET} \
+               || die "emake failed with ${GCC_MAKE_TARGET}"
 
-       popd > /dev/null
+       if ! is_crosscompile && use cxx && use_if_iuse doc ; then
+               if type -p doxygen > /dev/null ; then
+                       if tc_version_is_at_least 4.3 ; then
+                               cd "${CTARGET}"/libstdc++-v3/doc
+                               emake doc-man-doxygen || ewarn "failed to make 
docs"
+                       elif tc_version_is_at_least 3.0 ; then
+                               cd "${CTARGET}"/libstdc++-v3
+                               emake doxygen-man || ewarn "failed to make docs"
+                       fi
+               else
+                       ewarn "Skipping libstdc++ manpage generation since you 
don't have doxygen installed"
+               fi
+       fi
+
+       popd >/dev/null
 }
 
+#---->> src_test <<----
+
 toolchain_src_test() {
        if use regression-test ; then
                cd "${WORKDIR}"/build
@@ -1554,6 +1593,8 @@ toolchain_src_test() {
        fi
 }
 
+#---->> src_install <<----
+
 toolchain_src_install() {
        cd "${WORKDIR}"/build
 
@@ -1581,9 +1622,9 @@ toolchain_src_install() {
        S="${WORKDIR}"/build emake -j1 DESTDIR="${D}" install || die
 
        # Punt some tools which are really only useful while building gcc
-       find "${ED}" -name install-tools -prune -type d -exec rm -rf "{}" \;
+       find "${D}" -name install-tools -prune -type d -exec rm -rf "{}" \;
        # This one comes with binutils
-       find "${ED}" -name libiberty.a -delete
+       find "${D}" -name libiberty.a -delete
 
        # Move the libraries to the proper location
        gcc_movelibs
@@ -1592,7 +1633,7 @@ toolchain_src_install() {
        if ! is_crosscompile ; then
                local EXEEXT
                eval $(grep ^EXEEXT= "${WORKDIR}"/build/gcc/config.log)
-               [[ -r ${ED}${BINPATH}/gcc${EXEEXT} ]] || die "gcc not found in 
${ED}"
+               [[ -r ${D}${BINPATH}/gcc${EXEEXT} ]] || die "gcc not found in 
${D}"
        fi
 
        dodir /etc/env.d/gcc
@@ -1606,7 +1647,7 @@ toolchain_src_install() {
        gcc_slot_java
 
        dodir /usr/bin
-       cd "${ED}"${BINPATH}
+       cd "${D}"${BINPATH}
        # Ugh: we really need to auto-detect this list.
        #      It's constantly out of date.
        for x in cpp gcc g++ c++ gcov g77 gcj gcjh gfortran gccgo ; do
@@ -1632,34 +1673,34 @@ toolchain_src_install() {
        done
 
        # Now do the fun stripping stuff
-       env RESTRICT="" CHOST=${CHOST} prepstrip "${ED}${BINPATH}"
-       env RESTRICT="" CHOST=${CTARGET} prepstrip "${ED}${LIBPATH}"
+       env RESTRICT="" CHOST=${CHOST} prepstrip "${D}${BINPATH}"
+       env RESTRICT="" CHOST=${CTARGET} prepstrip "${D}${LIBPATH}"
        # gcc used to install helper binaries in lib/ but then moved to libexec/
-       [[ -d ${ED}${PREFIX}/libexec/gcc ]] && \
-               env RESTRICT="" CHOST=${CHOST} prepstrip 
"${ED}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}"
+       [[ -d ${D}${PREFIX}/libexec/gcc ]] && \
+               env RESTRICT="" CHOST=${CHOST} prepstrip 
"${D}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}"
 
        cd "${S}"
        if is_crosscompile; then
-               rm -rf "${ED}"/usr/share/{man,info}
-               rm -rf "${ED}"${DATAPATH}/{man,info}
+               rm -rf "${D}"/usr/share/{man,info}
+               rm -rf "${D}"${DATAPATH}/{man,info}
        else
                if tc_version_is_at_least 3.0 ; then
                        local cxx_mandir=$(find 
"${WORKDIR}/build/${CTARGET}/libstdc++-v3" -name man)
                        if [[ -d ${cxx_mandir} ]] ; then
                                # clean bogus manpages #113902
                                find "${cxx_mandir}" -name '*_build_*' -exec rm 
{} \;
-                               cp -r "${cxx_mandir}"/man? 
"${ED}/${DATAPATH}"/man/
+                               cp -r "${cxx_mandir}"/man? 
"${D}/${DATAPATH}"/man/
                        fi
                fi
                has noinfo ${FEATURES} \
-                       && rm -r "${ED}/${DATAPATH}"/info \
+                       && rm -r "${D}/${DATAPATH}"/info \
                        || prepinfo "${DATAPATH}"
                has noman ${FEATURES} \
-                       && rm -r "${ED}/${DATAPATH}"/man \
+                       && rm -r "${D}/${DATAPATH}"/man \
                        || prepman "${DATAPATH}"
        fi
        # prune empty dirs left behind
-       find "${ED}" -depth -type d -delete 2>/dev/null
+       find "${D}" -depth -type d -delete 2>/dev/null
 
        # install testsuite results
        if use regression-test; then
@@ -1672,34 +1713,27 @@ toolchain_src_install() {
        # Rather install the script, else portage with changing $FILESDIR
        # between binary and source package borks things ....
        if ! is_crosscompile ; then
-               cp "${GCC_FILESDIR}"/fix_libtool_files.sh "${T}"
-               cp "${GCC_FILESDIR}"/awk/fixlafiles.awk-no_gcc_la "${T}"
-               cp "${GCC_FILESDIR}"/awk/fixlafiles.awk "${T}"
-               eprefixify \
-                       "${T}"/fix_libtool_files.sh \
-                       "${T}"/fixlafiles.awk-no_gcc_la \
-                       "${T}"/fixlafiles.awk
-
                insinto "${DATAPATH}"
                if tc_version_is_at_least 4.0 ; then
-                       newins "${T}"/fixlafiles.awk-no_gcc_la fixlafiles.awk 
|| die
-                       find "${ED}/${LIBPATH}" -name libstdc++.la -type f 
-exec rm "{}" \;
+                       newins "${GCC_FILESDIR}"/awk/fixlafiles.awk-no_gcc_la 
fixlafiles.awk || die
+                       find "${D}/${LIBPATH}" -name libstdc++.la -type f -exec 
rm "{}" \;
+                       find "${D}/${LIBPATH}" -name "lib?san.la" -type f -exec 
rm "{}" \; # 487550
                else
-                       doins "${T}"/fixlafiles.awk || die
+                       doins "${GCC_FILESDIR}"/awk/fixlafiles.awk || die
                fi
                exeinto "${DATAPATH}"
-               doexe "${T}"/fix_libtool_files.sh || die
+               doexe "${GCC_FILESDIR}"/fix_libtool_files.sh || die
                doexe "${GCC_FILESDIR}"/c{89,99} || die
        fi
 
        # Use gid of 0 because some stupid ports don't have
        # the group 'root' set to gid 0.  Send to /dev/null
        # for people who are testing as non-root.
-       chown -R ${PORTAGE_INST_UID:-0}:${PORTAGE_INST_GID:-0} 
"${ED}"${LIBPATH} 2>/dev/null
+       chown -R root:0 "${D}"${LIBPATH} 2>/dev/null
 
        # Move pretty-printers to gdb datadir to shut ldconfig up
        local py 
gdbdir=/usr/share/gdb/auto-load${LIBPATH/\/lib\//\/$(get_libdir)\/}
-       pushd "${ED}"${LIBPATH} >/dev/null
+       pushd "${D}"${LIBPATH} >/dev/null
        for py in $(find . -name '*-gdb.py') ; do
                local multidir=${py%/*}
                insinto "${gdbdir}/${multidir}"
@@ -1715,42 +1749,9 @@ toolchain_src_install() {
 
        # Disable RANDMMAP so PCH works. #301299
        if tc_version_is_at_least 4.3 ; then
-               pax-mark -r 
"${ED}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}/cc1"
-               pax-mark -r 
"${ED}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}/cc1plus"
-       fi
-}
-
-gcc_slot_java() {
-       local x
-
-       # Move Java headers to compiler-specific dir
-       for x in "${ED}"${PREFIX}/include/gc*.h "${ED}"${PREFIX}/include/j*.h ; 
do
-               [[ -f ${x} ]] && mv -f "${x}" "${ED}"${LIBPATH}/include/
-       done
-       for x in gcj gnu java javax org ; do
-               if [[ -d ${ED}${PREFIX}/include/${x} ]] ; then
-                       dodir /${LIBPATH}/include/${x}
-                       mv -f "${ED}"${PREFIX}/include/${x}/* 
"${ED}"${LIBPATH}/include/${x}/
-                       rm -rf "${ED}"${PREFIX}/include/${x}
-               fi
-       done
-
-       if [[ -d ${ED}${PREFIX}/lib/security ]] || [[ -d 
${ED}${PREFIX}/$(get_libdir)/security ]] ; then
-               dodir /${LIBPATH}/security
-               mv -f "${ED}"${PREFIX}/lib*/security/* 
"${ED}"${LIBPATH}/security
-               rm -rf "${ED}"${PREFIX}/lib*/security
+               pax-mark -r 
"${D}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}/cc1"
+               pax-mark -r 
"${D}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}/cc1plus"
        fi
-
-       # Move random gcj files to compiler-specific directories
-       for x in libgcj.spec logging.properties ; do
-               x="${ED}${PREFIX}/lib/${x}"
-               [[ -f ${x} ]] && mv -f "${x}" "${ED}"${LIBPATH}/
-       done
-
-       # Rename jar because it could clash with Kaffe's jar if this gcc is
-       # primary compiler (aka don't have the -<version> extension)
-       cd "${ED}"${BINPATH}
-       [[ -f jar ]] && mv -f jar gcj-jar
 }
 
 # Move around the libs to the right location.  For some reason,
@@ -1767,7 +1768,7 @@ gcc_movelibs() {
 
                local OS_MULTIDIR=$($(XGCC) ${multiarg} 
--print-multi-os-directory)
                local MULTIDIR=$($(XGCC) ${multiarg} --print-multi-directory)
-               local TODIR=${ED}${LIBPATH}/${MULTIDIR}
+               local TODIR=${D}${LIBPATH}/${MULTIDIR}
                local FROMDIR=
 
                [[ -d ${TODIR} ]] || mkdir -p ${TODIR}
@@ -1779,7 +1780,7 @@ gcc_movelibs() {
                        ${PREFIX}/${CTARGET}/lib/${OS_MULTIDIR}
                do
                        removedirs="${removedirs} ${FROMDIR}"
-                       FROMDIR=${ED}${FROMDIR}
+                       FROMDIR=${D}${FROMDIR}
                        if [[ ${FROMDIR} != "${TODIR}" && -d ${FROMDIR} ]] ; 
then
                                local files=$(find "${FROMDIR}" -maxdepth 1 ! 
-type d 2>/dev/null)
                                if [[ -n ${files} ]] ; then
@@ -1791,10 +1792,10 @@ gcc_movelibs() {
 
                # SLOT up libgcj.pc if it's available (and let gcc-config worry 
about links)
                FROMDIR="${PREFIX}/lib/${OS_MULTIDIR}"
-               for x in "${ED}${FROMDIR}"/pkgconfig/libgcj*.pc ; do
+               for x in "${D}${FROMDIR}"/pkgconfig/libgcj*.pc ; do
                        [[ -f ${x} ]] || continue
                        sed -i "/^libdir=/s:=.*:=${LIBPATH}/${MULTIDIR}:" "${x}"
-                       mv "${x}" 
"${ED}${FROMDIR}"/pkgconfig/libgcj-${GCC_PV}.pc || die
+                       mv "${x}" 
"${D}${FROMDIR}"/pkgconfig/libgcj-${GCC_PV}.pc || die
                done
        done
 
@@ -1803,137 +1804,254 @@ gcc_movelibs() {
        #       rmdir SRC/lib/../lib/
        #       mv SRC/lib/../lib32/*.o DEST  # Bork
        for FROMDIR in ${removedirs} ; do
-               rmdir "${ED}"${FROMDIR} >& /dev/null
+               rmdir "${D}"${FROMDIR} >& /dev/null
        done
-       find "${ED}" -type d | xargs rmdir >& /dev/null
+       find "${D}" -type d | xargs rmdir >& /dev/null
 }
-#----<< src_* >>----
 
-#---->> unorganized crap in need of refactoring follows
+# make sure the libtool archives have libdir set to where they actually
+# -are-, and not where they -used- to be.  also, any dependencies we have
+# on our own .la files need to be updated.
+fix_libtool_libdir_paths() {
+       pushd "${D}" >/dev/null
 
-# gcc_quick_unpack will unpack the gcc tarball and patches in a way that is
-# consistant with the behavior of get_gcc_src_uri. The only patch it applies
-# itself is the branch update if present.
-#
-# Travis Tilley <[email protected]> (03 Sep 2004)
-#
-gcc_quick_unpack() {
-       pushd "${WORKDIR}" > /dev/null
-       export PATCH_GCC_VER=${PATCH_GCC_VER:-${GCC_RELEASE_VER}}
-       export UCLIBC_GCC_VER=${UCLIBC_GCC_VER:-${PATCH_GCC_VER}}
-       export PIE_GCC_VER=${PIE_GCC_VER:-${GCC_RELEASE_VER}}
-       export HTB_GCC_VER=${HTB_GCC_VER:-${GCC_RELEASE_VER}}
-       export SPECS_GCC_VER=${SPECS_GCC_VER:-${GCC_RELEASE_VER}}
+       pushd "./${1}" >/dev/null
+       local dir="${PWD#${D%/}}"
+       local allarchives=$(echo *.la)
+       allarchives="\(${allarchives// /\\|}\)"
+       popd >/dev/null
 
-       if [[ -n ${GCC_A_FAKEIT} ]] ; then
-               unpack ${GCC_A_FAKEIT}
-       elif [[ -n ${PRERELEASE} ]] ; then
-               unpack gcc-${PRERELEASE}.tar.bz2
-       elif [[ -n ${SNAPSHOT} ]] ; then
-               unpack gcc-${SNAPSHOT}.tar.bz2
-       elif [[ ${PV} != *9999* ]] ; then
-               unpack gcc-${GCC_RELEASE_VER}.tar.bz2
-               # We want branch updates to be against a release tarball
-               if [[ -n ${BRANCH_UPDATE} ]] ; then
-                       pushd "${S}" > /dev/null
-                       epatch 
"${DISTDIR}"/gcc-${GCC_RELEASE_VER}-branch-update-${BRANCH_UPDATE}.patch.bz2
-                       popd > /dev/null
-               fi
+       sed -i \
+               -e "/^libdir=/s:=.*:='${dir}':" \
+               ./${dir}/*.la
+       sed -i \
+               -e "/^dependency_libs=/s:/[^ ]*/${allarchives}:${LIBPATH}/\1:g" 
\
+               $(find ./${PREFIX}/lib* -maxdepth 3 -name '*.la') \
+               ./${dir}/*.la
+
+       popd >/dev/null
+}
+
+create_gcc_env_entry() {
+       dodir /etc/env.d/gcc
+       local gcc_envd_base="/etc/env.d/gcc/${CTARGET}-${GCC_CONFIG_VER}"
+
+       local gcc_specs_file
+       local gcc_envd_file="${D}${gcc_envd_base}"
+       if [[ -z $1 ]] ; then
+               # I'm leaving the following commented out to remind me that it
+               # was an insanely -bad- idea. Stuff broke. GCC_SPECS isnt unset
+               # on chroot or in non-toolchain.eclass gcc ebuilds!
+               #gcc_specs_file="${LIBPATH}/specs"
+               gcc_specs_file=""
+       else
+               gcc_envd_file+="-$1"
+               gcc_specs_file="${LIBPATH}/$1.specs"
        fi
 
-       if [[ -n ${D_VER} ]] && use d ; then
-               pushd "${S}"/gcc > /dev/null
-               unpack gdc-${D_VER}-src.tar.bz2
-               cd ..
-               ebegin "Adding support for the D language"
-               ./gcc/d/setup-gcc.sh >& "${T}"/dgcc.log
-               if ! eend $? ; then
-                       eerror "The D gcc package failed to apply"
-                       eerror "Please include this log file when posting a bug 
report:"
-                       eerror "  ${T}/dgcc.log"
-                       die "failed to include the D language"
-               fi
-               popd > /dev/null
+       # We want to list the default ABI's LIBPATH first so libtool
+       # searches that directory first.  This is a temporary
+       # workaround for libtool being stupid and using .la's from
+       # conflicting ABIs by using the first one in the search path
+       local ldpaths mosdirs
+       if tc_version_is_at_least 3.2 ; then
+               local mdir mosdir abi ldpath
+               for abi in $(get_all_abis TARGET) ; do
+                       mdir=$($(XGCC) $(get_abi_CFLAGS ${abi}) 
--print-multi-directory)
+                       ldpath=${LIBPATH}
+                       [[ ${mdir} != "." ]] && ldpath+="/${mdir}"
+                       ldpaths="${ldpath}${ldpaths:+:${ldpaths}}"
+
+                       mosdir=$($(XGCC) $(get_abi_CFLAGS ${abi}) 
-print-multi-os-directory)
+                       mosdirs="${mosdir}${mosdirs:+:${mosdirs}}"
+               done
+       else
+               # Older gcc's didn't do multilib, so logic is simple.
+               ldpaths=${LIBPATH}
        fi
 
-       [[ -n ${PATCH_VER} ]] && \
-               unpack gcc-${PATCH_GCC_VER}-patches-${PATCH_VER}.tar.bz2
+       cat <<-EOF > ${gcc_envd_file}
+       PATH="${BINPATH}"
+       ROOTPATH="${BINPATH}"
+       GCC_PATH="${BINPATH}"
+       LDPATH="${ldpaths}"
+       MANPATH="${DATAPATH}/man"
+       INFOPATH="${DATAPATH}/info"
+       STDCXX_INCDIR="${STDCXX_INCDIR##*/}"
+       CTARGET="${CTARGET}"
+       GCC_SPECS="${gcc_specs_file}"
+       MULTIOSDIRS="${mosdirs}"
+       EOF
+}
 
-       [[ -n ${UCLIBC_VER} ]] && \
-               unpack 
gcc-${UCLIBC_GCC_VER}-uclibc-patches-${UCLIBC_VER}.tar.bz2
+copy_minispecs_gcc_specs() {
+       # setup the hardenedno* specs files and the vanilla specs file.
+       if hardened_gcc_works ; then
+               create_gcc_env_entry hardenednopiessp
+       fi
+       if hardened_gcc_works pie ; then
+               create_gcc_env_entry hardenednopie
+       fi
+       if hardened_gcc_works ssp ; then
+               create_gcc_env_entry hardenednossp
+       fi
+       create_gcc_env_entry vanilla
+       insinto ${LIBPATH}
+       doins "${WORKDIR}"/specs/*.specs || die "failed to install specs"
+       # Build system specs file which, if it exists, must be a complete set of
+       # specs as it completely and unconditionally overrides the builtin 
specs.
+       if ! tc_version_is_at_least 4.4 ; then
+               $(XGCC) -dumpspecs > "${WORKDIR}"/specs/specs
+               cat "${WORKDIR}"/build.specs >> "${WORKDIR}"/specs/specs
+               doins "${WORKDIR}"/specs/specs || die "failed to install the 
specs file"
+       fi
+}
 
-       if want_pie ; then
-               if [[ -n ${PIE_CORE} ]] ; then
-                       unpack ${PIE_CORE}
-               else
-                       unpack gcc-${PIE_GCC_VER}-piepatches-v${PIE_VER}.tar.bz2
+gcc_slot_java() {
+       local x
+
+       # Move Java headers to compiler-specific dir
+       for x in "${D}"${PREFIX}/include/gc*.h "${D}"${PREFIX}/include/j*.h ; do
+               [[ -f ${x} ]] && mv -f "${x}" "${D}"${LIBPATH}/include/
+       done
+       for x in gcj gnu java javax org ; do
+               if [[ -d ${D}${PREFIX}/include/${x} ]] ; then
+                       dodir /${LIBPATH}/include/${x}
+                       mv -f "${D}"${PREFIX}/include/${x}/* 
"${D}"${LIBPATH}/include/${x}/
+                       rm -rf "${D}"${PREFIX}/include/${x}
                fi
-               [[ -n ${SPECS_VER} ]] && \
-                       unpack gcc-${SPECS_GCC_VER}-specs-${SPECS_VER}.tar.bz2
+       done
+
+       if [[ -d ${D}${PREFIX}/lib/security ]] || [[ -d 
${D}${PREFIX}/$(get_libdir)/security ]] ; then
+               dodir /${LIBPATH}/security
+               mv -f "${D}"${PREFIX}/lib*/security/* "${D}"${LIBPATH}/security
+               rm -rf "${D}"${PREFIX}/lib*/security
        fi
 
-       use_if_iuse boundschecking && unpack 
"bounds-checking-gcc-${HTB_GCC_VER}-${HTB_VER}.patch.bz2"
+       # Move random gcj files to compiler-specific directories
+       for x in libgcj.spec logging.properties ; do
+               x="${D}${PREFIX}/lib/${x}"
+               [[ -f ${x} ]] && mv -f "${x}" "${D}"${LIBPATH}/
+       done
 
-       popd > /dev/null
+       # Rename jar because it could clash with Kaffe's jar if this gcc is
+       # primary compiler (aka don't have the -<version> extension)
+       cd "${D}"${BINPATH}
+       [[ -f jar ]] && mv -f jar gcj-jar
 }
 
-do_gcc_HTB_patches() {
-       use_if_iuse boundschecking || return 0
+#---->> pkg_post* <<----
 
-       # modify the bounds checking patch with a regression patch
-       epatch "${WORKDIR}/bounds-checking-gcc-${HTB_GCC_VER}-${HTB_VER}.patch"
-       BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION}, 
HTB-${HTB_GCC_VER}-${HTB_VER}"
+toolchain_pkg_postinst() {
+       do_gcc_config
+
+       if ! is_crosscompile ; then
+               echo
+               ewarn "If you have issues with packages unable to locate 
libstdc++.la,"
+               ewarn "then try running 'fix_libtool_files.sh' on the old gcc 
versions."
+               echo
+               ewarn "You might want to review the GCC upgrade guide when 
moving between"
+               ewarn "major versions (like 4.2 to 4.3):"
+               ewarn "http://www.gentoo.org/doc/en/gcc-upgrading.xml";
+               echo
+
+               # Clean up old paths
+               rm -f "${ROOT}"/*/rcscripts/awk/fixlafiles.awk 
"${ROOT}"/sbin/fix_libtool_files.sh
+               rmdir "${ROOT}"/*/rcscripts{/awk,} 2>/dev/null
+
+               mkdir -p "${ROOT}"/usr/{share/gcc-data,sbin,bin}
+               cp "${ROOT}/${DATAPATH}"/fixlafiles.awk 
"${ROOT}"/usr/share/gcc-data/ || die
+               cp "${ROOT}/${DATAPATH}"/fix_libtool_files.sh 
"${ROOT}"/usr/sbin/ || die
+
+               # Since these aren't critical files and portage sucks with
+               # handling of binpkgs, don't require these to be found
+               cp "${ROOT}/${DATAPATH}"/c{89,99} "${ROOT}"/usr/bin/ 2>/dev/null
+       fi
+
+       if use regression-test ; then
+               elog "Testsuite results have been installed into 
/usr/share/doc/${PF}/testsuite"
+               echo
+       fi
+
+       if [[ -n ${PRERELEASE}${SNAPSHOT} ]] ; then
+               einfo "This GCC ebuild is provided for your convenience, and 
the use"
+               einfo "of this compiler is not supported by the Gentoo 
Developers."
+               einfo "Please report bugs to upstream at 
http://gcc.gnu.org/bugzilla/";
+       fi
 }
 
-# do various updates to PIE logic
-do_gcc_PIE_patches() {
-       want_pie || return 0
+toolchain_pkg_postrm() {
+       # to make our lives easier (and saner), we do the fix_libtool stuff 
here.
+       # rather than checking SLOT's and trying in upgrade paths, we just see 
if
+       # the common libstdc++.la exists in the ${LIBPATH} of the gcc that we 
are
+       # unmerging.  if it does, that means this was a simple re-emerge.
 
-       use vanilla && return 0
+       # clean up the cruft left behind by cross-compilers
+       if is_crosscompile ; then
+               if [[ -z $(ls "${ROOT}"/etc/env.d/gcc/${CTARGET}* 2>/dev/null) 
]] ; then
+                       rm -f "${ROOT}"/etc/env.d/gcc/config-${CTARGET}
+                       rm -f "${ROOT}"/etc/env.d/??gcc-${CTARGET}
+                       rm -f "${ROOT}"/usr/bin/${CTARGET}-{gcc,{g,c}++}{,32,64}
+               fi
+               return 0
+       fi
 
-       if tc_version_is_at_least 4.3.2 ; then
-               guess_patch_type_in_dir "${WORKDIR}"/piepatch/
-               EPATCH_MULTI_MSG="Applying pie patches ..." \
-               epatch "${WORKDIR}"/piepatch/
-       else
-               guess_patch_type_in_dir "${WORKDIR}"/piepatch/upstream
+       # ROOT isnt handled by the script
+       [[ ${ROOT} != "/" ]] && return 0
 
-               # corrects startfile/endfile selection and shared/static/pie 
flag usage
-               EPATCH_MULTI_MSG="Applying upstream pie patches ..." \
-               epatch "${WORKDIR}"/piepatch/upstream
-               # adds non-default pie support (rs6000)
-               EPATCH_MULTI_MSG="Applying non-default pie patches ..." \
-               epatch "${WORKDIR}"/piepatch/nondef
-               # adds default pie support (rs6000 too) if DEFAULT_PIE[_SSP] is 
defined
-               EPATCH_MULTI_MSG="Applying default pie patches ..." \
-               epatch "${WORKDIR}"/piepatch/def
+       if [[ ! -e ${LIBPATH}/libstdc++.so ]] ; then
+               # make sure the profile is sane during same-slot upgrade #289403
+               do_gcc_config
+
+               einfo "Running 'fix_libtool_files.sh ${GCC_RELEASE_VER}'"
+               /usr/sbin/fix_libtool_files.sh ${GCC_RELEASE_VER}
+               if [[ -n ${BRANCH_UPDATE} ]] ; then
+                       einfo "Running 'fix_libtool_files.sh 
${GCC_RELEASE_VER}-${BRANCH_UPDATE}'"
+                       /usr/sbin/fix_libtool_files.sh 
${GCC_RELEASE_VER}-${BRANCH_UPDATE}
+               fi
        fi
 
-       # we want to be able to control the pie patch logic via something other
-       # than ALL_CFLAGS...
-       sed -e '/^ALL_CFLAGS/iHARD_CFLAGS = ' \
-               -e 's|^ALL_CFLAGS = |ALL_CFLAGS = $(HARD_CFLAGS) |' \
-               -i "${S}"/gcc/Makefile.in
-       # Need to add HARD_CFLAGS to ALL_CXXFLAGS on >= 4.7
-       if tc_version_is_at_least 4.7 ; then
-               sed -e '/^ALL_CXXFLAGS/iHARD_CFLAGS = ' \
-                        -e 's|^ALL_CXXFLAGS = |ALL_CXXFLAGS = $(HARD_CFLAGS) 
|' \
-                        -i "${S}"/gcc/Makefile.in
+       return 0
+}
+
+do_gcc_config() {
+       if ! should_we_gcc_config ; then
+               env -i ROOT="${ROOT}" gcc-config --use-old --force
+               return 0
        fi
 
-       BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION}, pie-${PIE_VER}"
+       local current_gcc_config="" current_specs="" use_specs=""
+
+       current_gcc_config=$(env -i ROOT="${ROOT}" gcc-config -c ${CTARGET} 
2>/dev/null)
+       if [[ -n ${current_gcc_config} ]] ; then
+               # figure out which specs-specific config is active
+               current_specs=$(gcc-config -S ${current_gcc_config} | awk 
'{print $3}')
+               [[ -n ${current_specs} ]] && use_specs=-${current_specs}
+       fi
+       if [[ -n ${use_specs} ]] && \
+          [[ ! -e 
${ROOT}/etc/env.d/gcc/${CTARGET}-${GCC_CONFIG_VER}${use_specs} ]]
+       then
+               ewarn "The currently selected specs-specific gcc config,"
+               ewarn "${current_specs}, doesn't exist anymore. This is usually"
+               ewarn "due to enabling/disabling hardened or switching to a 
version"
+               ewarn "of gcc that doesnt create multiple specs files. The 
default"
+               ewarn "config will be used, and the previous preference 
forgotten."
+               use_specs=""
+       fi
+
+       gcc-config ${CTARGET}-${GCC_CONFIG_VER}${use_specs}
 }
 
 should_we_gcc_config() {
        # if the current config is invalid, we definitely want a new one
        # Note: due to bash quirkiness, the following must not be 1 line
        local curr_config
-       curr_config=$(env -i ROOT="${ROOT}" "${EPREFIX}"/usr/bin/gcc-config -c 
${CTARGET} 2>&1) || return 0
+       curr_config=$(env -i ROOT="${ROOT}" gcc-config -c ${CTARGET} 2>&1) || 
return 0
 
        # if the previously selected config has the same major.minor (branch) as
        # the version we are installing, then it will probably be uninstalled
        # for being in the same SLOT, make sure we run gcc-config.
-       local curr_config_ver=$(env -i ROOT="${ROOT}" 
"${EPREFIX}"/usr/bin/gcc-config -S ${curr_config} | awk '{print $2}')
+       local curr_config_ver=$(env -i ROOT="${ROOT}" gcc-config -S 
${curr_config} | awk '{print $2}')
 
        local curr_branch_ver=$(get_version_component_range 1-2 
${curr_config_ver})
 
@@ -1966,133 +2084,27 @@ should_we_gcc_config() {
        fi
 }
 
-do_gcc_config() {
-       if ! should_we_gcc_config ; then
-               env -i ROOT="${ROOT}" "${EPREFIX}"/usr/bin/gcc-config --use-old 
--force
-               return 0
-       fi
-
-       local current_gcc_config="" current_specs="" use_specs=""
-
-       current_gcc_config=$(env -i ROOT="${ROOT}" 
"${EPREFIX}"/usr/bin/gcc-config -c ${CTARGET} 2>/dev/null)
-       if [[ -n ${current_gcc_config} ]] ; then
-               # figure out which specs-specific config is active
-               current_specs=$(gcc-config -S ${current_gcc_config} | awk 
'{print $3}')
-               [[ -n ${current_specs} ]] && use_specs=-${current_specs}
-       fi
-       if [[ -n ${use_specs} ]] && \
-          [[ ! -e 
${EROOT}/etc/env.d/gcc/${CTARGET}-${GCC_CONFIG_VER}${use_specs} ]]
-       then
-               ewarn "The currently selected specs-specific gcc config,"
-               ewarn "${current_specs}, doesn't exist anymore. This is usually"
-               ewarn "due to enabling/disabling hardened or switching to a 
version"
-               ewarn "of gcc that doesnt create multiple specs files. The 
default"
-               ewarn "config will be used, and the previous preference 
forgotten."
-               use_specs=""
-       fi
-
-       gcc-config ${CTARGET}-${GCC_CONFIG_VER}${use_specs}
-}
-
-# This function allows us to gentoo-ize GCCs version number and bugzilla
-# URL without needing to use patches.
-gcc_version_patch() {
-       # gcc-4.3+ has configure flags (whoo!)
-       tc_version_is_at_least 4.3 && return 0
-
-       local version_string=${GCC_CONFIG_VER}
-       [[ -n ${BRANCH_UPDATE} ]] && version_string+=" ${BRANCH_UPDATE}"
-
-       einfo "patching gcc version: ${version_string} 
(${BRANDING_GCC_PKGVERSION})"
-
-       local gcc_sed=( -e 's:gcc\.gnu\.org/bugs\.html:bugs\.gentoo\.org/:' )
-       if grep -qs VERSUFFIX "${S}"/gcc/version.c ; then
-               gcc_sed+=( -e "/VERSUFFIX \"\"/s:\"\":\" 
(${BRANDING_GCC_PKGVERSION})\":" )
-       else
-               version_string="${version_string} (${BRANDING_GCC_PKGVERSION})"
-               gcc_sed+=( -e "/const char version_string\[\] = /s:= \".*\":= 
\"${version_string}\":" )
-       fi
-       sed -i "${gcc_sed[@]}" "${S}"/gcc/version.c || die
-}
+#---->> support and misc functions <<----
 
-# This is a historical wart.  The original Gentoo/amd64 port used:
-#    lib32 - 32bit binaries (x86)
-#    lib64 - 64bit binaries (x86_64)
-#    lib   - "native" binaries (a symlink to lib64)
-# Most other distros use the logic (including mainline gcc):
-#    lib   - 32bit binaries (x86)
-#    lib64 - 64bit binaries (x86_64)
-# Over time, Gentoo is migrating to the latter form.
+# This is to make sure we don't accidentally try to enable support for a
+# language that doesnt exist. GCC 3.4 supports f77, while 4.0 supports f95, 
etc.
 #
-# Unfortunately, due to distros picking the lib32 behavior, newer gcc
-# versions will dynamically detect whether to use lib or lib32 for its
-# 32bit multilib.  So, to keep the automagic from getting things wrong
-# while people are transitioning from the old style to the new style,
-# we always set the MULTILIB_OSDIRNAMES var for relevant targets.
-setup_multilib_osdirnames() {
-       is_multilib || return 0
-
-       local config
-       local libdirs="../lib64 ../lib32"
-
-       # this only makes sense for some Linux targets
-       case ${CTARGET} in
-       x86_64*-linux*)    config="i386" ;;
-       powerpc64*-linux*) config="rs6000" ;;
-       sparc64*-linux*)   config="sparc" ;;
-       s390x*-linux*)     config="s390" ;;
-       *)                     return 0 ;;
-       esac
-       config+="/t-linux64"
-
-       local sed_args=()
-       if tc_version_is_at_least 4.6 ; then
-               sed_args+=( -e 's:$[(]call if_multiarch[^)]*[)]::g' )
-       fi
-       if [[ ${SYMLINK_LIB} == "yes" ]] ; then
-               einfo "updating multilib directories to be: ${libdirs}"
-               if tc_version_is_at_least 4.6.4 || tc_version_is_at_least 4.7 ; 
then
-                       sed_args+=( -e 
'/^MULTILIB_OSDIRNAMES.*lib32/s:[$][(]if.*):../lib32:' )
-               else
-                       sed_args+=( -e "/^MULTILIB_OSDIRNAMES/s:=.*:= 
${libdirs}:" )
-               fi
-       else
-               einfo "using upstream multilib; disabling lib32 autodetection"
-               sed_args+=( -r -e 's:[$][(]if.*,(.*)[)]:\1:' )
-       fi
-       sed -i "${sed_args[@]}" "${S}"/gcc/config/${config} || die
-}
-
-# make sure the libtool archives have libdir set to where they actually
-# -are-, and not where they -used- to be.  also, any dependencies we have
-# on our own .la files need to be updated.
-fix_libtool_libdir_paths() {
-       pushd "${ED}" >/dev/null
-
-       pushd "./${1}" >/dev/null
-       local dir="${PWD#${D%/}}"
-       local allarchives=$(echo *.la)
-       allarchives="\(${allarchives// /\\|}\)"
-       popd >/dev/null
-
-       sed -i \
-               -e "/^libdir=/s:=.*:='${EPREFIX}/${dir##/}':" \
-               ./${dir}/*.la
-       sed -i \
-               -e "/^dependency_libs=/s:/[^ 
]*/${allarchives}:${EPREFIX}/${LIBPATH##/}/\1:g" \
-               $(find ./${PREFIX}/lib* -maxdepth 3 -name '*.la') \
-               ./${dir}/*.la
-
-       popd >/dev/null
+# Also add a hook so special ebuilds (kgcc64) can control which languages
+# exactly get enabled
+gcc-lang-supported() {
+       grep ^language=\"${1}\" "${S}"/gcc/*/config-lang.in > /dev/null || 
return 1
+       [[ -z ${TOOLCHAIN_ALLOWED_LANGS} ]] && return 0
+       has $1 ${TOOLCHAIN_ALLOWED_LANGS}
 }
 
-is_multilib() {
-       tc_version_is_at_least 3 || return 1
-       use multilib
+is_ada() {
+       gcc-lang-supported ada || return 1
+       use ada
 }
 
 is_cxx() {
        gcc-lang-supported 'c++' || return 1
+       ! is_crosscompile && tc_version_is_at_least 4.8 && return 0
        use cxx
 }
 
@@ -2126,6 +2138,11 @@ is_go() {
        use cxx && use_if_iuse go
 }
 
+is_multilib() {
+       tc_version_is_at_least 3 || return 1
+       use multilib
+}
+
 is_objc() {
        gcc-lang-supported objc || return 1
        use_if_iuse objc
@@ -2136,15 +2153,106 @@ is_objcxx() {
        use cxx && use_if_iuse objc++
 }
 
-is_ada() {
-       gcc-lang-supported ada || return 1
-       use ada
+# Grab a variable from the build system (taken from linux-info.eclass)
+get_make_var() {
+       local var=$1 makefile=${2:-${WORKDIR}/build/Makefile}
+       echo -e "e:\\n\\t@echo \$(${var})\\ninclude ${makefile}" | \
+               r=${makefile%/*} emake --no-print-directory -s -f - 2>/dev/null
 }
 
-is_treelang() {
-       use_if_iuse boundschecking && return 1 #260532
-       is_crosscompile && return 1 #199924
-       gcc-lang-supported treelang || return 1
-       #use treelang
-       return 0
+XGCC() { get_make_var GCC_FOR_TARGET ; }
+
+# The gentoo piessp patches allow for 3 configurations:
+# 1) PIE+SSP by default
+# 2) PIE by default
+# 3) SSP by default
+hardened_gcc_works() {
+       if [[ $1 == "pie" ]] ; then
+               # $gcc_cv_ld_pie is unreliable as it simply take the output of
+               # `ld --help | grep -- -pie`, that reports the option in all 
cases, also if
+               # the loader doesn't actually load the resulting executables.
+               # To avoid breakage, blacklist FreeBSD here at least
+               [[ ${CTARGET} == *-freebsd* ]] && return 1
+
+               want_pie || return 1
+               use_if_iuse nopie && return 1
+               hardened_gcc_is_stable pie
+               return $?
+       elif [[ $1 == "ssp" ]] ; then
+               [[ -n ${SPECS_VER} ]] || return 1
+               use_if_iuse nossp && return 1
+               hardened_gcc_is_stable ssp
+               return $?
+       else
+               # laziness ;)
+               hardened_gcc_works pie || return 1
+               hardened_gcc_works ssp || return 1
+               return 0
+       fi
+}
+
+hardened_gcc_is_stable() {
+       local tocheck
+       if [[ $1 == "pie" ]] ; then
+               if [[ ${CTARGET} == *-uclibc* ]] ; then
+                       tocheck=${PIE_UCLIBC_STABLE}
+               else
+                       tocheck=${PIE_GLIBC_STABLE}
+               fi
+       elif [[ $1 == "ssp" ]] ; then
+               if [[ ${CTARGET} == *-uclibc* ]] ; then
+                       tocheck=${SSP_UCLIBC_STABLE}
+               elif  [[ ${CTARGET} == *-gnu* ]] ; then
+                       tocheck=${SSP_STABLE}
+               fi
+       else
+               die "hardened_gcc_stable needs to be called with pie or ssp"
+       fi
+
+       has $(tc-arch) ${tocheck} && return 0
+       return 1
+}
+
+want_minispecs() {
+       if tc_version_is_at_least 4.3.2 && use hardened ; then
+               if ! want_pie ; then
+                       ewarn "PIE_VER or SPECS_VER is not defined in the GCC 
ebuild."
+               elif use vanilla ; then
+                       ewarn "You will not get hardened features if you have 
the vanilla USE-flag."
+               elif use nopie && use nossp ; then
+                       ewarn "You will not get hardened features if you have 
the nopie and nossp USE-flag."
+               elif ! hardened_gcc_works ; then
+                       ewarn "Your $(tc-arch) arch is not supported."
+               else
+                       return 0
+               fi
+               ewarn "Hope you know what you are doing. Hardened will not 
work."
+               return 0
+       fi
+       return 1
+}
+
+want_pie() {
+       ! use hardened && [[ -n ${PIE_VER} ]] && use nopie && return 1
+       [[ -n ${PIE_VER} ]] && [[ -n ${SPECS_VER} ]] && return 0
+       tc_version_is_at_least 4.3.2 && return 1
+       [[ -z ${PIE_VER} ]] && return 1
+       use !nopie && return 0
+       return 1
+}
+
+has toolchain_death_notice ${EBUILD_DEATH_HOOKS} || EBUILD_DEATH_HOOKS+=" 
toolchain_death_notice"
+toolchain_death_notice() {
+       if [[ -e "${WORKDIR}"/build ]] ; then
+               pushd "${WORKDIR}"/build >/dev/null
+               (echo '' | $(tc-getCC ${CTARGET}) ${CFLAGS} -v -E - 2>&1) > 
gccinfo.log
+               [[ -e "${T}"/build.log ]] && cp "${T}"/build.log .
+               tar jcf "${WORKDIR}"/gcc-build-logs.tar.bz2 \
+                       gccinfo.log build.log $(find -name config.log)
+               rm gccinfo.log build.log
+               eerror
+               eerror "Please include ${WORKDIR}/gcc-build-logs.tar.bz2 in 
your bug report."
+               eerror
+               popd >/dev/null
+       fi
 }

Reply via email to