Index: uspace/app/binutils/Makefile
===================================================================
--- uspace/app/binutils/Makefile	(revision 53d9ee96329510963ab21f729dcc2e29c0617773)
+++ uspace/app/binutils/Makefile	(revision 53d9ee96329510963ab21f729dcc2e29c0617773)
@@ -0,0 +1,151 @@
+#
+# Copyright (c) 2011 Petr Koupy
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+nullstring =
+space = $(nullstring) # space
+
+# Symlink to the sources of the ported application.
+APP_DIR = ./src
+
+# Default directory for the binutils source tree.
+REDIST_DIR = ./redist
+
+# Absolute path to the binutils source tree.
+# Can be set externally, defaults to $REDIST_DIR.
+BINUTILS_ABS_PATH ?= $(subst $(space),\ ,$(PWD))/$(REDIST_DIR)
+
+# $USPACE_PREFIX have to be based on the absolute path,
+# because targets derived from it will be referenced from
+# other than the current directory.
+USPACE_PREFIX = $(subst $(space),\ ,$(PWD))/../..
+
+# Ensure static configuration of Makefile.common.
+STATIC_ONLY = y
+
+# Makefile.common for native applications.
+COMMON_MAKEFILE_NATIVE = $(USPACE_PREFIX)/Makefile.common
+
+# Generated from native Makefile.common.
+COMMON_MAKEFILE_PATCHED = ./Makefile.common
+
+# AWK script which generates patched Makefile.common.
+PATCH_SCRIPT = ./patch.awk
+
+# Compilers that can be used to build binutils.
+SUPPORTED_COMPILERS = gcc_cross gcc_native
+
+# Patched Makefile.common for ported user space applications.
+include $(COMMON_MAKEFILE_PATCHED)
+
+# Map the HelenOS target to binutils target.
+ifeq ($(PLATFORM),amd64)
+TARGET=amd64-linux-gnu
+else ($(PLATFORM),arm32)
+TARGET=arm-linux-gnu
+else ($(PLATFORM),ia32)
+TARGET=i686-pc-linux-gnu
+else ($(PLATFORM),ia64)
+TARGET=ia64-pc-linux-gnu
+else ($(PLATFORM),mips32)
+TARGET=mipsel-linux-gnu
+else ($(PLATFORM),mips32eb)
+TARGET=mips-linux-gnu
+else ($(PLATFORM),mips64)
+TARGET=mips64el-linux-gnu
+else ($(PLATFORM),ppc32)
+TARGET=ppc-linux-gnu
+else ($(PLATFORM),ppc64)
+TARGET=ppc64-linux-gnu
+else ($(PLATFORM),sparc64)
+TARGET=sparc64-linux-gnu
+endif
+
+# Binutils configure flags.
+CONF_FLAGS = --disable-nls --disable-shared --enable-static \
+	--with-zlib=no
+
+# Binutils make targets.
+MAKE_TARGETS = all-gas all-ld
+
+# Patch native Makefile.common.
+# Check presence of gcc compiler.
+# Patch $PATH to intercept toolchain calls.
+# Generate false toolchain.
+# Create symlink to the binutils directory.
+# Patch binutils.
+# Configure binutils for target architecture.
+# Make binutils.
+# Copy binaries.
+all: $(COMMON_MAKEFILE_PATCHED) all_
+ifeq ($(COMPILER),$(findstring $(COMPILER),$(SUPPORTED_COMPILERS)))
+	OLDPATH = $(PATH)
+	PATH = $(PWD):$(PATH)
+	export PATH
+	./toolchain.sh gcc $(CC) '$(CFLAGS)'
+	./toolchain.sh as $(AS)
+	./toolchain.sh ar $(AR)
+	./toolchain.sh ranlib
+	./toolchain.sh ld $(LD) '$(LFLAGS)' '$(LINKER_SCRIPT)' '$(LIBS) $(BASE_LIBS)'
+	./toolchain.sh objdump $(OBJDUMP)
+	./toolchain.sh objcopy $(OBJCOPY)
+	./toolchain.sh strip $(STRIP)
+	ln -s -T $(BINUTILS_ABS_PATH) $(APP_DIR)
+	./intrusive.sh do $(APP_DIR)
+	ORIG_DIR = $(subst $(space),\ ,$(PWD))
+	cd $(APP_DIR)
+	./configure --target=$(TARGET) $(CONF_FLAGS)
+	make $(MAKE_TARGETS)
+	cd $(ORIG_DIR)
+	cp $(APP_DIR)/gas/as-new ./as
+	cp $(APP_DIR)/ld/ld-new ./ld
+	PATH = $(OLDPATH)
+	export PATH
+else
+	echo 'Skipped: Cannot build binutils with unsupported compiler.'
+endif
+
+# Create patched Makefile.common from native Makefile.common.
+$(COMMON_MAKEFILE_PATCHED): $(PATCH_SCRIPT) $(COMMON_MAKEFILE_NATIVE)
+	awk -f $^ > $@
+
+# Delete binaries.
+# Clean binutils.
+# Unpatch binutils.
+# Delete symlink
+# Delete generated scripts.
+clean:
+	rm -f as ld
+	ORIG_DIR = $(subst $(space),\ ,$(PWD))
+	cd $(APP_DIR)
+	make distclean
+	cd $(ORIG_DIR)
+	./intrusive.sh undo $(APP_DIR)
+	rm -f $(APP_DIR)
+	rm -f gcc as ar ranlib ld objdump objcopy strip
+	rm -f $(COMMON_MAKEFILE_PATCHED)
+
Index: uspace/app/binutils/intrusive.sh
===================================================================
--- uspace/app/binutils/intrusive.sh	(revision 53d9ee96329510963ab21f729dcc2e29c0617773)
+++ uspace/app/binutils/intrusive.sh	(revision 53d9ee96329510963ab21f729dcc2e29c0617773)
@@ -0,0 +1,54 @@
+#! /bin/bash
+
+#
+# Copyright (c) 2011 Petr Koupy
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+#
+# This shell script is reserved for intrusive patches (hacks) to binutils
+# that are not possible to be done in a clean and isolated way or for
+# which doing so would require too much complexity.
+#
+
+case "$1" in
+	"do")
+		# Binutils plugin support is dependent on libdl.so library.
+		# By default, the plugin support is switched of for all
+		# configure scripts of binutils. The only exception is configure
+		# script of ld 2.21 (and possibly above), where plugin support
+		# became mandatory (although not really needed). 
+		mv -f "$2/ld/configure" "$2/ld/configure.backup"
+		sed 's/enable_plugins=yes/enable_plugins=no/g' \
+			< "$2/ld/configure.backup" > "$2/ld/configure"
+		;;
+	"undo")
+		mv -f "$2/ld/configure.backup" "$2/ld/configure"
+		;;
+	*)
+		;;
+esac
+
Index: uspace/app/binutils/patch.awk
===================================================================
--- uspace/app/binutils/patch.awk	(revision 53d9ee96329510963ab21f729dcc2e29c0617773)
+++ uspace/app/binutils/patch.awk	(revision 53d9ee96329510963ab21f729dcc2e29c0617773)
@@ -0,0 +1,51 @@
+#! /bin/awk -f
+
+#
+# Copyright (c) 2011 Petr Koupy
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+#
+# This script is reserved for transformation of native
+# uspace/Makefile.common so it would be compatible with ported
+# application and its operation could be intercepted.
+#
+
+# Rename 'all' target so it can be hijacked.
+/^all/ {
+	$1 = "all_:"
+}
+
+# Rename 'clean' target so it can be hijacked.
+/^clean/ {
+	$1 = "clean_:"
+}
+
+# Print rest of the lines unaltered.
+{
+	print
+}
+
Index: uspace/app/binutils/toolchain.sh
===================================================================
--- uspace/app/binutils/toolchain.sh	(revision 53d9ee96329510963ab21f729dcc2e29c0617773)
+++ uspace/app/binutils/toolchain.sh	(revision 53d9ee96329510963ab21f729dcc2e29c0617773)
@@ -0,0 +1,120 @@
+#! /bin/bash
+
+#
+# Copyright (c) 2011 Petr Koupy
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+# 
+# This script is intended to be called indirectly from the Makefile.
+# Given information from Makefile.common (e.g. CFLAGS), this shell
+# script generates false toolchain consisting of a short shell script
+# for each tool. Therefore, generated false tool already contains some
+# filtered information from the HelenOS side. When the actual call is
+# intercepted, information passed from the binutils side is filtered
+# and stored. Information from both sides is then joined together
+# and passed to the real tool.
+#
+# Basic idea behind filtering the information is that binutils is
+# dominant provider for the compilation, whereas HelenOS is dominant
+# for the linkage. In that way, the compilation command from binutils
+# is left untouched and only extended with a couple of flags that are
+# specific for HelenOS (i.e. include directories, preprocessor 
+# directives, preprocessor flags, standard libc exclusion). Similarly,
+# the linkage command is taken from HelenOS and extended with the
+# filtered information from binutils (i.e. output binary, objects,
+# binutils libraries).
+#
+# Most of the false tools are straightforward. However, gcc needs a 
+# special approach because binutils use it for both compilation and
+# linkage. In case the linking usage is detected, the call is redirected
+# to the linker.
+#
+
+case "$1" in
+	"gcc")
+		echo '#! /bin/bash' > 'gcc'
+		echo 'AS_LINK="`echo $* | grep '\'as-new\''`"' >> 'gcc'
+		echo 'LD_LINK="`echo $* | grep '\'ld-new\''`"' >> 'gcc'
+		echo 'LINK="`echo -n "$AS_LINK""$LD_LINK"`"' >> 'gcc'
+		echo 'if [ -n "$LINK" ]; then' >> 'gcc'
+		echo '	LD_ARGS="`echo $* | \' >> 'gcc'
+		echo '		sed '\'s/-O[^ ]*//g\'' | \' >> 'gcc'
+		echo '		sed '\'s/-W[^ ]*//g\'' | \' >> 'gcc'
+		echo '		sed '\'s/-g//g\'' | \' >> 'gcc'
+		echo '		sed '\'s/-l[^ ]*//g\'' | \' >> 'gcc'
+		echo '		sed '\'s/ [ ]*/ /g\''`"' >> 'gcc'
+		echo '	ld "$LD_ARGS"' >> 'gcc'
+		echo 'else' >> 'gcc'
+		CFLAGS="`echo "$3" | \
+			sed 's/-O[^ ]*//g' | \
+			sed 's/-W[^ ]*//g' | \
+			sed 's/-pipe//g' | \
+			sed 's/-g//g' | \
+			sed 's/ [ ]*/ /g'`"
+		echo "	$2" "$CFLAGS" '$@' >> 'gcc'
+		echo 'fi' >> 'gcc'
+		chmod a+x 'gcc'
+		;;
+	"as")
+		echo '#! /bin/bash' > 'as'
+		echo "$2" '$@' >> 'as'
+		chmod a+x 'as'
+		;;
+	"ar")
+		echo '#! /bin/bash' > 'ar'
+		echo "$2" '$@' >> 'ar'
+		chmod a+x 'ar'
+		;;
+	"ranlib")
+		echo '#! /bin/bash' > 'ranlib'
+		echo "ar -s" '$@' >> 'ranlib'
+		chmod a+x 'ranlib'
+		;;
+	"ld")
+		echo '#! /bin/bash' > 'ld'
+		echo "$2 -n $3 -T $4" '$@' "$5" >> 'ld'
+		chmod a+x 'ld'
+		;;
+	"objdump")
+		echo '#! /bin/bash' > 'objdump'
+		echo "$2" '$@' >> 'objdump'
+		chmod a+x 'objdump'
+		;;
+	"objcopy")
+		echo '#! /bin/bash' > 'objcopy'
+		echo "$2" '$@' >> 'objcopy'
+		chmod a+x 'objcopy'
+		;;
+	"strip")
+		echo '#! /bin/bash' > 'strip'
+		echo "$2" '$@' >> 'strip'
+		chmod a+x 'strip'
+		;;
+	*)
+		;;
+esac
+
