Index: tools/toolchain.sh
===================================================================
--- tools/toolchain.sh	(revision 6c9f1a6e27209452eec4f7374bf4fe488649945b)
+++ tools/toolchain.sh	(revision 5a65d2911d4d1c7b70d454ff0997668ebf286e00)
@@ -55,6 +55,9 @@
 BINUTILS_VERSION="2.23.1"
 BINUTILS_RELEASE=""
+BINUTILS_PATCHES="toolchain-binutils-2.23.1.patch"
 GCC_VERSION="4.8.1"
+GCC_PATCHES="toolchain-gcc-4.8.1-targets.patch toolchain-gcc-4.8.1-headers.patch"
 GDB_VERSION="7.6"
+GDB_PATCHES="toolchain-gdb-7.6.patch"
 
 BASEDIR="`pwd`"
@@ -64,4 +67,5 @@
 
 REAL_INSTALL=true
+USE_HELENOS_TARGET=false
 INSTALL_DIR="${BASEDIR}/PKG"
 
@@ -138,5 +142,5 @@
 	echo
 	echo "Syntax:"
-	echo " $0 [--no-install] <platform>"
+	echo " $0 [--no-install] [--helenos-target] <platform>"
 	echo
 	echo "Possible target platforms are:"
@@ -158,4 +162,5 @@
 	echo "CROSS_PREFIX environment variable. If the variable is not"
 	echo "defined, /usr/local/cross/ is used as default."
+	echo
 	echo "If --no-install is present, the toolchain still uses the"
 	echo "CROSS_PREFIX as the target directory but the installation"
@@ -163,4 +168,10 @@
 	echo "the actual root file system. That is only useful if you do"
 	echo "not want to run the script under the super user."
+	echo
+	echo "The --helenos-target will build HelenOS-specific toolchain"
+	echo "(i.e. it will use *-helenos-* triplet instead of *-linux-*)."
+	echo "This toolchain is installed into /usr/local/cross-helenos by"
+	echo "default. The settings can be changed by setting environment"
+	echo "variable CROSS_HELENOS_PREFIX."
 	echo
 	
@@ -273,4 +284,16 @@
 }
 
+patch_sources() {
+	PATCH_FILE="$1"
+	PATCH_STRIP="$2"
+	DESC="$3"
+	
+	change_title "Patching ${DESC}"
+	echo " >>> Patching ${DESC} with ${PATCH_FILE}"
+	
+	patch -t "-p${PATCH_STRIP}" <"$PATCH_FILE"
+	check_error $? "Error patching ${DESC}."
+}
+
 prepare() {
 	show_dependencies
@@ -290,32 +313,42 @@
 	case "$1" in
 		"amd64")
-			TARGET="amd64-linux-gnu"
+			LINUX_TARGET="amd64-linux-gnu"
+			HELENOS_TARGET="amd64-helenos"
 			;;
 		"arm32")
-			TARGET="arm-linux-gnueabi"
+			LINUX_TARGET="arm-linux-gnueabi"
+			HELENOS_TARGET="arm-helenos-gnueabi"
 			;;
 		"ia32")
-			TARGET="i686-pc-linux-gnu"
+			LINUX_TARGET="i686-pc-linux-gnu"
+			HELENOS_TARGET="i686-pc-helenos"
 			;;
 		"ia64")
-			TARGET="ia64-pc-linux-gnu"
+			LINUX_TARGET="ia64-pc-linux-gnu"
+			HELENOS_TARGET="ia64-pc-helenos"
 			;;
 		"mips32")
-			TARGET="mipsel-linux-gnu"
+			LINUX_TARGET="mipsel-linux-gnu"
+			HELENOS_TARGET="mipsel-helenos"
 			;;
 		"mips32eb")
-			TARGET="mips-linux-gnu"
+			LINUX_TARGET="mips-linux-gnu"
+			HELENOS_TARGET="mips-helenos"
 			;;
 		"mips64")
-			TARGET="mips64el-linux-gnu"
+			LINUX_TARGET="mips64el-linux-gnu"
+			HELENOS_TARGET="mips64el-helenos"
 			;;
 		"ppc32")
-			TARGET="ppc-linux-gnu"
+			LINUX_TARGET="ppc-linux-gnu"
+			HELENOS_TARGET="ppc-helenos"
 			;;
 		"ppc64")
-			TARGET="ppc64-linux-gnu"
+			LINUX_TARGET="ppc64-linux-gnu"
+			HELENOS_TARGET="ppc64-helenos"
 			;;
 		"sparc64")
-			TARGET="sparc64-linux-gnu"
+			LINUX_TARGET="sparc64-linux-gnu"
+			HELENOS_TARGET="sparc64-helenos"
 			;;
 		*)
@@ -327,6 +360,11 @@
 build_target() {
 	PLATFORM="$1"
-	# This sets the TARGET variable
+	# This sets the *_TARGET variables
 	set_target_from_platform "$PLATFORM"
+	if $USE_HELENOS_TARGET; then
+		TARGET="$HELENOS_TARGET"
+	else
+		TARGET="$LINUX_TARGET"
+	fi
 	
 	WORKDIR="${BASEDIR}/${PLATFORM}"
@@ -339,6 +377,13 @@
 		CROSS_PREFIX="/usr/local/cross"
 	fi
-	
-	PREFIX="${CROSS_PREFIX}/${PLATFORM}"
+	if [ -z "${CROSS_HELENOS_PREFIX}" ] ; then
+		CROSS_HELENOS_PREFIX="/usr/local/cross-helenos"
+	fi
+	
+	if $USE_HELENOS_TARGET; then
+		PREFIX="${CROSS_HELENOS_PREFIX}/${PLATFORM}"
+	else
+		PREFIX="${CROSS_PREFIX}/${PLATFORM}"
+	fi
 	
 	echo ">>> Downloading tarballs"
@@ -362,4 +407,14 @@
 	unpack_tarball "${BASEDIR}/${GDB}" "GDB"
 	
+	echo ">>> Applying patches"
+	for p in $BINUTILS_PATCHES; do
+		patch_sources "${BASEDIR}/${p}" 0 "binutils"
+	done
+	for p in $GCC_PATCHES; do
+		patch_sources "${BASEDIR}/${p}" 0 "GCC"
+	done
+	for p in $GDB_PATCHES; do
+		patch_sources "${BASEDIR}/${p}" 0 "GDB"
+	done
 	
 	echo ">>> Processing binutils (${PLATFORM})"
@@ -447,8 +502,19 @@
 }
 
-if [ "$1" = "--no-install" ]; then
-	REAL_INSTALL=false
-	shift
-fi
+while [ "$#" -gt 1 ]; do
+	case "$1" in
+		--no-install)
+			REAL_INSTALL=false
+			shift
+			;;
+		--helenos-target)
+			USE_HELENOS_TARGET=true
+			shift
+			;;
+		*)
+			show_usage
+			;;
+	esac
+done
 
 if [ "$#" -lt "1" ]; then
