diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a33eae8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,12 @@
+build/prebuilt
+
+samples/*/bin/
+samples/*/build.xml
+samples/*/gen/
+samples/*/libs/
+samples/*/local.properties
+samples/*/obj
+
+build/platforms/android-*/*/usr/lib/libmissing.a
+
+.gvide
diff --git a/build/core/build-binary.mk b/build/core/build-binary.mk
index 4152db5..0386c04 100644
--- a/build/core/build-binary.mk
+++ b/build/core/build-binary.mk
@@ -207,7 +207,7 @@ ifneq ($(filter -l%,$(LOCAL_LDLIBS)),)
     LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib $(LOCAL_LDLIBS)
 endif
 
-$(LOCAL_BUILT_MODULE): PRIVATE_STATIC_LIBRARIES := $(static_libraries)
+$(LOCAL_BUILT_MODULE): PRIVATE_STATIC_LIBRARIES := $(static_libraries) $(TARGET_PREBUILT_STATIC_LIBRARIES)
 $(LOCAL_BUILT_MODULE): PRIVATE_SHARED_LIBRARIES := $(shared_libraries)
 $(LOCAL_BUILT_MODULE): PRIVATE_OBJECTS          := $(LOCAL_OBJECTS)
 
diff --git a/build/core/setup-toolchain.mk b/build/core/setup-toolchain.mk
index b7211a5..ac9a65f 100644
--- a/build/core/setup-toolchain.mk
+++ b/build/core/setup-toolchain.mk
@@ -60,7 +60,10 @@ TARGET_CRTBEGIN_STATIC_O  := $(SYSROOT)/usr/lib/crtbegin_static.o
 TARGET_CRTBEGIN_DYNAMIC_O := $(SYSROOT)/usr/lib/crtbegin_dynamic.o
 TARGET_CRTEND_O           := $(SYSROOT)/usr/lib/crtend_android.o
 
-TARGET_PREBUILT_SHARED_LIBRARIES := libc libstdc++ libm
+TARGET_PREBUILT_STATIC_LIBRARIES := libmissing
+TARGET_PREBUILT_STATIC_LIBRARIES := $(TARGET_PREBUILT_STATIC_LIBRARIES:%=$(SYSROOT)/usr/lib/%.a)
+
+TARGET_PREBUILT_SHARED_LIBRARIES := libc libm
 TARGET_PREBUILT_SHARED_LIBRARIES := $(TARGET_PREBUILT_SHARED_LIBRARIES:%=$(SYSROOT)/usr/lib/%.so)
 
 # now call the toolchain-specific setup script
@@ -96,6 +99,7 @@ installed_modules: $(NDK_APP_GDBSETUP)
 
 $(NDK_APP_GDBSETUP)::
 	@ echo "Gdbsetup       : $(PRIVATE_DST)"
+	$(hide) mkdir -p $(shell dirname $(PRIVATE_DST))
 	$(hide) echo "set solib-search-path $(PRIVATE_SOLIB_PATH)" > $(PRIVATE_DST)
 	$(hide) echo "directory $(SYSROOT)/usr/include" >> $(PRIVATE_DST)
 
diff --git a/build/platforms/android-3/arch-arm/usr/include/asm/byteorder.h b/build/platforms/android-3/arch-arm/usr/include/asm/byteorder.h
index 4da37bf..b869695 100644
--- a/build/platforms/android-3/arch-arm/usr/include/asm/byteorder.h
+++ b/build/platforms/android-3/arch-arm/usr/include/asm/byteorder.h
@@ -22,7 +22,7 @@ static inline __attribute_const__ __u32 ___arch__swab32(__u32 x)
 #ifndef __thumb__
  if (!__builtin_constant_p(x)) {
 
- asm ("eor\t%0, %1, %1, ror #16" : "=r" (t) : "r" (x));
+ __asm__ ("eor\t%0, %1, %1, ror #16" : "=r" (t) : "r" (x));
  } else
 #endif
  t = x ^ ((x << 16) | (x >> 16));
diff --git a/build/platforms/android-4/arch-arm/usr/include/asm/byteorder.h b/build/platforms/android-4/arch-arm/usr/include/asm/byteorder.h
index 4da37bf..b869695 100644
--- a/build/platforms/android-4/arch-arm/usr/include/asm/byteorder.h
+++ b/build/platforms/android-4/arch-arm/usr/include/asm/byteorder.h
@@ -22,7 +22,7 @@ static inline __attribute_const__ __u32 ___arch__swab32(__u32 x)
 #ifndef __thumb__
  if (!__builtin_constant_p(x)) {
 
- asm ("eor\t%0, %1, %1, ror #16" : "=r" (t) : "r" (x));
+ __asm__ ("eor\t%0, %1, %1, ror #16" : "=r" (t) : "r" (x));
  } else
 #endif
  t = x ^ ((x << 16) | (x >> 16));
diff --git a/build/platforms/android-5/arch-arm/usr/include/asm/byteorder.h b/build/platforms/android-5/arch-arm/usr/include/asm/byteorder.h
index 4da37bf..b869695 100644
--- a/build/platforms/android-5/arch-arm/usr/include/asm/byteorder.h
+++ b/build/platforms/android-5/arch-arm/usr/include/asm/byteorder.h
@@ -22,7 +22,7 @@ static inline __attribute_const__ __u32 ___arch__swab32(__u32 x)
 #ifndef __thumb__
  if (!__builtin_constant_p(x)) {
 
- asm ("eor\t%0, %1, %1, ror #16" : "=r" (t) : "r" (x));
+ __asm__ ("eor\t%0, %1, %1, ror #16" : "=r" (t) : "r" (x));
  } else
 #endif
  t = x ^ ((x << 16) | (x >> 16));
diff --git a/build/platforms/android-8/arch-arm/usr/include/asm/byteorder.h b/build/platforms/android-8/arch-arm/usr/include/asm/byteorder.h
index 4da37bf..b869695 100644
--- a/build/platforms/android-8/arch-arm/usr/include/asm/byteorder.h
+++ b/build/platforms/android-8/arch-arm/usr/include/asm/byteorder.h
@@ -22,7 +22,7 @@ static inline __attribute_const__ __u32 ___arch__swab32(__u32 x)
 #ifndef __thumb__
  if (!__builtin_constant_p(x)) {
 
- asm ("eor\t%0, %1, %1, ror #16" : "=r" (t) : "r" (x));
+ __asm__ ("eor\t%0, %1, %1, ror #16" : "=r" (t) : "r" (x));
  } else
 #endif
  t = x ^ ((x << 16) | (x >> 16));
diff --git a/build/toolchains/arm-eabi-4.2.1/setup.mk b/build/toolchains/arm-eabi-4.2.1/setup.mk
index 82be6f7..4c0d7c6 100644
--- a/build/toolchains/arm-eabi-4.2.1/setup.mk
+++ b/build/toolchains/arm-eabi-4.2.1/setup.mk
@@ -93,7 +93,7 @@ TARGET_CFLAGS   := $(TARGET_CFLAGS.common)
 
 
 TARGET_CXX      := $(TOOLCHAIN_PREFIX)g++
-TARGET_CXXFLAGS := $(TARGET_CFLAGS.common) -fno-exceptions -fno-rtti
+TARGET_CXXFLAGS := $(TARGET_CFLAGS.common)
 
 TARGET_LD      := $(TOOLCHAIN_PREFIX)ld
 TARGET_LDFLAGS :=
@@ -104,6 +104,9 @@ TARGET_ARFLAGS := crs
 TARGET_LIBGCC := $(shell $(TARGET_CC) -mthumb-interwork -print-libgcc-file-name)
 TARGET_LDLIBS := -Wl,-rpath-link=$(SYSROOT)/usr/lib
 
+TARGET_CXXLIBS := $(shell $(TARGET_CXX) -mthumb-interwork -print-file-name=libstdc++.a)
+TARGET_CXXLIBS += $(shell $(TARGET_CXX) -mthumb-interwork -print-file-name=libsupc++.a)
+
 # These flags are used to ensure that a binary doesn't reference undefined
 # flags.
 TARGET_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
@@ -137,10 +140,13 @@ $(TARGET_CC) \
     $(PRIVATE_WHOLE_STATIC_LIBRARIES) \
     -Wl,--no-whole-archive \
     $(PRIVATE_STATIC_LIBRARIES) \
+    $(TARGET_CXXLIBS) \
     $(TARGET_LIBGCC) \
     $(PRIVATE_SHARED_LIBRARIES) \
     $(PRIVATE_LDFLAGS) \
     $(PRIVATE_LDLIBS) \
+    $(TARGET_CXXLIBS) \
+    $(TARGET_LIBGCC) \
     -o $@
 endef
 
@@ -153,10 +159,13 @@ $(TARGET_CC) \
     $(TARGET_CRTBEGIN_DYNAMIC_O) \
     $(PRIVATE_OBJECTS) \
     $(PRIVATE_STATIC_LIBRARIES) \
+    $(TARGET_CXXLIBS) \
     $(TARGET_LIBGCC) \
     $(PRIVATE_SHARED_LIBRARIES) \
     $(PRIVATE_LDFLAGS) \
     $(PRIVATE_LDLIBS) \
+    $(TARGET_CXXLIBS) \
+    $(TARGET_LIBGCC) \
     $(TARGET_CRTEND_O) \
     -o $@
 endef
diff --git a/build/toolchains/arm-eabi-4.4.0/setup.mk b/build/toolchains/arm-eabi-4.4.0/setup.mk
index 585f73d..e8e21cf 100644
--- a/build/toolchains/arm-eabi-4.4.0/setup.mk
+++ b/build/toolchains/arm-eabi-4.4.0/setup.mk
@@ -117,7 +117,7 @@ TARGET_CFLAGS   := $(TARGET_CFLAGS.common) $(TARGET_ARCH_CFLAGS)
 
 
 TARGET_CXX      := $(TOOLCHAIN_PREFIX)g++
-TARGET_CXXFLAGS := $(TARGET_CFLAGS.common) $(TARGET_ARCH_CFLAGS) -fno-exceptions -fno-rtti
+TARGET_CXXFLAGS := $(TARGET_CFLAGS.common) $(TARGET_ARCH_CFLAGS)
 
 TARGET_LD      := $(TOOLCHAIN_PREFIX)ld
 TARGET_LDFLAGS := $(TARGET_ARCH_LDFLAGS)
@@ -128,6 +128,9 @@ TARGET_ARFLAGS := crs
 TARGET_LIBGCC := $(shell $(TARGET_CC) -mthumb-interwork -print-libgcc-file-name)
 TARGET_LDLIBS := -Wl,-rpath-link=$(SYSROOT)/usr/lib
 
+TARGET_CXXLIBS := $(shell $(TARGET_CXX) -mthumb-interwork -print-file-name=libstdc++.a)
+TARGET_CXXLIBS += $(shell $(TARGET_CXX) -mthumb-interwork -print-file-name=libsupc++.a)
+
 # These flags are used to ensure that a binary doesn't reference undefined
 # flags.
 TARGET_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
@@ -157,10 +160,13 @@ $(TARGET_CC) \
     $(PRIVATE_WHOLE_STATIC_LIBRARIES) \
     -Wl,--no-whole-archive \
     $(PRIVATE_STATIC_LIBRARIES) \
+    $(TARGET_CXXLIBS) \
     $(TARGET_LIBGCC) \
     $(PRIVATE_SHARED_LIBRARIES) \
     $(PRIVATE_LDFLAGS) \
     $(PRIVATE_LDLIBS) \
+    $(TARGET_CXXLIBS) \
+    $(TARGET_LIBGCC) \
     -o $@
 endef
 
@@ -173,10 +179,13 @@ $(TARGET_CC) \
     $(TARGET_CRTBEGIN_DYNAMIC_O) \
     $(PRIVATE_OBJECTS) \
     $(PRIVATE_STATIC_LIBRARIES) \
+    $(TARGET_CXXLIBS) \
     $(TARGET_LIBGCC) \
     $(PRIVATE_SHARED_LIBRARIES) \
     $(PRIVATE_LDFLAGS) \
     $(PRIVATE_LDLIBS) \
+    $(TARGET_CXXLIBS) \
+    $(TARGET_LIBGCC) \
     $(TARGET_CRTEND_O) \
     -o $@
 endef
diff --git a/build/toolchains/x86-4.2.1/setup.mk b/build/toolchains/x86-4.2.1/setup.mk
index a7e7d1d..e23fe47 100644
--- a/build/toolchains/x86-4.2.1/setup.mk
+++ b/build/toolchains/x86-4.2.1/setup.mk
@@ -60,7 +60,7 @@ TARGET_CFLAGS   := $(TARGET_CFLAGS.common)
 
 
 TARGET_CXX      := $(TOOLCHAIN_PREFIX)g++
-TARGET_CXXFLAGS := $(TARGET_CFLAGS.common) -fno-exceptions -fno-rtti
+TARGET_CXXFLAGS := $(TARGET_CFLAGS.common)
 
 TARGET_LD      := $(TOOLCHAIN_PREFIX)ld
 TARGET_LDFLAGS :=
@@ -71,6 +71,9 @@ TARGET_ARFLAGS := crs
 TARGET_LIBGCC := $(shell $(TARGET_CC) -print-libgcc-file-name)
 TARGET_LDLIBS := -Wl,-rpath-link=$(SYSROOT)/usr/lib
 
+TARGET_CXXLIBS := $(shell $(TARGET_CXX) -print-file-name=libstdc++.a)
+TARGET_CXXLIBS := $(shell $(TARGET_CXX) -print-file-name=libsupc++.a)
+
 # These flags are used to ensure that a binary doesn't reference undefined
 # flags.
 TARGET_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
@@ -104,10 +107,13 @@ $(TARGET_CC) \
     $(PRIVATE_WHOLE_STATIC_LIBRARIES) \
     -Wl,--no-whole-archive \
     $(PRIVATE_STATIC_LIBRARIES) \
+    $(TARGET_CXXLIBS) \
     $(TARGET_LIBGCC) \
     $(PRIVATE_SHARED_LIBRARIES) \
     $(PRIVATE_LDFLAGS) \
     $(PRIVATE_LDLIBS) \
+    $(TARGET_CXXLIBS) \
+    $(TARGET_LIBGCC) \
     -o $@
 endef
 
@@ -120,10 +126,13 @@ $(TARGET_CC) \
     $(TARGET_CRTBEGIN_DYNAMIC_O) \
     $(PRIVATE_OBJECTS) \
     $(PRIVATE_STATIC_LIBRARIES) \
+    $(TARGET_CXXLIBS) \
     $(TARGET_LIBGCC) \
     $(PRIVATE_SHARED_LIBRARIES) \
     $(PRIVATE_LDFLAGS) \
     $(PRIVATE_LDLIBS) \
+    $(TARGET_CXXLIBS) \
+    $(TARGET_LIBGCC) \
     $(TARGET_CRTEND_O) \
     -o $@
 endef
diff --git a/build/tools/build-gcc.sh b/build/tools/build-gcc.sh
index 689d876..1af574b 100755
--- a/build/tools/build-gcc.sh
+++ b/build/tools/build-gcc.sh
@@ -41,6 +41,7 @@ OPTION_BUILD_OUT=
 OPTION_PLATFORM=
 OPTION_SYSROOT=
 OPTION_BINUTILS_VERSION=
+OPTION_REMOVE_BUILD_DIR=yes
 
 register_option "--build-out=<path>" do_build_out "Set temporary build directory" "/tmp/<random>"
 register_option "--sysroot=<path>"   do_sysroot   "Specify sysroot directory directly"
@@ -48,6 +49,7 @@ register_option "--platform=<name>"  do_platform  "Specify platform name" "$PLAT
 register_option "--gdb-version=<version>"  do_gdb_version  "Specify gdb version" "$GDB_VERSION"
 register_option "--binutils-version=<version>" do_binutils_version "Specify binutils version" "$BINUTILS_VERSION"
 register_option "-j<number>" do_jobs "Use <number> parallel build jobs" "$JOBS"
+register_option "--remove-build-dir=<yes|no>" do_remove_build_dir "Remove temporary build dir" "$OPTION_REMOVE_BUILD_DIR"
 
 do_build_out ()
 {
@@ -79,6 +81,11 @@ do_jobs ()
     JOBS=$1
 }
 
+do_remove_build_dir()
+{
+    OPTION_REMOVE_BUILD_DIR=$1
+}
+
 extract_parameters $@
 
 set_parameters ()
@@ -178,6 +185,8 @@ $BUILD_SRCDIR/configure --target=$ABI_TOOLCHAIN_PREFIX \
                         --disable-nls \
                         --prefix=$TOOLCHAIN_PATH \
                         --with-sysroot=$SYSROOT \
+                        --enable-libstdc__-v3 \
+                        --enable-threads=posix \
                         --with-binutils-version=$BINUTILS_VERSION \
                         --with-gcc-version=$GCC_VERSION \
                         --with-gdb-version=$GDB_VERSION
@@ -227,6 +236,11 @@ run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus
 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2
 
 dump "Done."
-if [ -n "$OPTION_BUILD_OUT" ] ; then
-    rm -rf $BUILD_OUT
+if [ -n "$OPTION_BUILD_OUT" -a "x$OPTION_REMOVE_BUILD_DIR" = "xyes" ] ; then
+    for i in 1 2 3 4 5 ; do
+        rm -rf $BUILD_OUT && break
+        sleep 2
+    done
 fi
+
+exit 0
diff --git a/build/tools/build-gdbserver.sh b/build/tools/build-gdbserver.sh
index 85eca23..520ea29 100755
--- a/build/tools/build-gdbserver.sh
+++ b/build/tools/build-gdbserver.sh
@@ -41,16 +41,19 @@ JOBS=$HOST_NUM_CPUS
 OPTION_HELP=no
 OPTION_BUILD_OUT=
 OPTION_SYSROOT=
+OPTION_REMOVE_BUILD_DIR=yes
 
 register_option "--build-out=<path>" do_build_out "Set temporary build directory" "/tmp/random"
 register_option "--platform=<name>"  do_platform  "Target specific platform" "$PLATFORM"
 register_option "--sysroot=<path>"   do_sysroot   "Specify sysroot directory directly."
 register_option "-j<number>"         do_jobs      "Use <number> build jobs in parallel" "$JOBS"
+register_option "--remove-build-dir=<yes|no>" do_remove_build_dir "Remove temporary build dir" "$OPTION_REMOVE_BUILD_DIR"
 
 do_build_out () { OPTION_BUILD_OUT=$1; }
 do_platform () { OPTION_PLATFORM=$1; }
 do_sysroot () { OPTION_SYSROOT=$1; }
 do_jobs () { JOBS=$1; }
+do_remove_build_dir () { OPTION_REMOVE_BUILD_DIR=$1; }
 
 extract_parameters $@
 
@@ -121,11 +124,11 @@ INCLUDE_DIRS=\
 -I$SYSROOT/usr/include"
 CRTBEGIN="$SYSROOT/usr/lib/crtbegin_static.o"
 CRTEND="$SYSROOT/usr/lib/crtend_android.o"
-LIBRARY_LDFLAGS="$CRTBEGIN -L$SYSROOT/usr/lib -lc -lm -lgcc $CRTEND "
+LIBRARY_LDFLAGS="$CRTBEGIN -L$SYSROOT/usr/lib -lc -lm -lgcc -lc $CRTEND "
 
 cd $BUILD_OUT &&
 export CC="$TOOLCHAIN_PREFIX-gcc" &&
-export CFLAGS="-O2 -nostdinc -nostdlib -D__ANDROID__ -DANDROID -DSTDC_HEADERS $INCLUDE_DIRS"  &&
+export CFLAGS="-O2 -nostdinc -nostdlib -fno-short-enums -D__ANDROID__ -DANDROID -DSTDC_HEADERS $INCLUDE_DIRS"  &&
 export LDFLAGS="-static -Wl,-z,nocopyreloc -Wl,--no-undefined $LIBRARY_LDFLAGS" &&
 run $SRC_DIR/configure \
 --host=${ABI_CONFIGURE_HOST} \
@@ -162,6 +165,11 @@ if [ $? != 0 ] ; then
 fi
 
 dump "Done."
-if [ -n "$OPTION_BUILD_OUT" ] ; then
-    rm -rf $BUILD_OUT
+if [ -n "$OPTION_BUILD_OUT" -a "x$OPTION_REMOVE_BUILD_DIR" = "xyes" ] ; then
+    for i in 1 2 3 4 5 ; do
+        rm -rf $BUILD_OUT && break
+        sleep 2
+    done
 fi
+
+exit 0
diff --git a/build/tools/build-missing.sh b/build/tools/build-missing.sh
new file mode 100755
index 0000000..cbe07f1
--- /dev/null
+++ b/build/tools/build-missing.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+# include common function and variable definitions
+. `dirname $0`/prebuilt-common.sh
+
+if [ "x$1" = "x" ]; then
+    dump "ERROR: missing NDK_DIR parameter"
+    exit 1
+fi
+
+NDK_DIR=$1
+cd $NDK_DIR
+NDK_DIR=`pwd`
+
+ARCH_armeabi=arm
+ARCH_x86=x86
+
+if [ "x$NDK_DIR" != "x$ANDROID_NDK_ROOT" ]; then
+    mkdir -p $NDK_DIR/build
+
+    for i in `ls -1 $ANDROID_NDK_ROOT/build | grep -v prebuilt`; do
+        rm -Rf $NDK_DIR/build/$i
+        cp -R $ANDROID_NDK_ROOT/build/$i $NDK_DIR/build
+    done
+
+    rm -Rf $NDK_DIR/samples
+    mkdir -p $NDK_DIR/samples
+    cp -R $ANDROID_NDK_ROOT/samples/missing $NDK_DIR/samples
+
+    cp -f $ANDROID_NDK_ROOT/ndk-build $NDK_DIR
+fi
+
+cd $NDK_DIR/samples/missing
+
+#ABI_LIST="armeabi x86"
+ABI_LIST=armeabi
+
+PLATFORMS=`ls -1 $ANDROID_NDK_ROOT/build/platforms`
+for abi in $ABI_LIST; do
+    for p in $PLATFORMS; do
+        eval dir=\$ARCH_$abi
+        if [ -d $ANDROID_NDK_ROOT/build/platforms/$p/arch-$dir ]; then
+            $NDK_DIR/ndk-build -B V=1 TARGET_PLATFORM=$p TARGET_ARCH_ABI=$abi
+            dest=$ANDROID_NDK_ROOT/build/platforms/$p/arch-$dir/usr/lib/libmissing.a
+            rm -Rf $dest
+            cp obj/local/$abi/libmissing.a $dest
+        fi
+    done
+done
+rm -Rf obj
+
+cd $NDK_DIR
+if [ "x$NDK_DIR" != "x$ANDROID_NDK_ROOT" ]; then
+    for i in `ls -1 $NDK_DIR/build | grep -v prebuilt`; do
+        rm -Rf $NDK_DIR/build/$i
+    done
+    rm -Rf $NDK_DIR/samples $NDK_DIR/ndk-build
+fi
diff --git a/build/tools/download-toolchain-sources.sh b/build/tools/download-toolchain-sources.sh
index bfc0e74..cf56180 100755
--- a/build/tools/download-toolchain-sources.sh
+++ b/build/tools/download-toolchain-sources.sh
@@ -27,6 +27,8 @@ OPTION_RELEASE=
 OPTION_GIT=
 OPTION_BRANCH=
 OPTION_PACKAGE=no
+OPTION_OVER_HTTP=no
+OPTION_FOR_DATE=
 
 # the default release name (use today's date)
 RELEASE=`date +%Y%m%d`
@@ -39,11 +41,15 @@ register_option "--branch=<name>" do_branch "Specify release branch" $BRANCH
 register_option "--release=<name>" do_release "Specify release name" $RELEASE
 register_option "--git=<executable>" do_git "Use this version of the git tool" $GITCMD
 register_option "--package" do_package "Create source package in /tmp"
+register_option "--over-http" do_over_http "Download sources over http:// (git:// by default)"
+register_option "--for-date=<date>" do_for_date "Download sources for specified date (latest by default)"
 
 do_branch () { OPTION_BRANCH=$1; }
 do_release () { OPTION_RELEASE=$1; }
 do_git () { OPTION_GIT=$1; }
 do_package () { OPTION_PACKAGE=yes; }
+do_over_http () { OPTION_OVER_HTTP=yes; }
+do_for_date () { OPTION_FOR_DATE=$1; }
 
 PROGRAM_PARAMETERS="<src-dir>"
 PROGRAM_DESCRIPTION=\
@@ -89,7 +95,9 @@ if [ $? != 0 ] ; then
 fi
 
 # prefix used for all clone operations
-GITPREFIX=git://android.git.kernel.org/toolchain
+PROTO_PREFIX=git
+[ "x$OPTION_OVER_HTTP" = "xyes" ] && PROTO_PREFIX=http
+GITPREFIX=$PROTO_PREFIX://android.git.kernel.org/toolchain
 
 toolchain_clone ()
 {
@@ -109,6 +117,9 @@ toolchain_clone ()
             exit 1
         fi
     fi
+    if [ "x$OPTION_FOR_DATE" != "x" ] ; then
+        run git checkout `git rev-list -n 1 --before="$OPTION_FOR_DATE" HEAD`
+    fi
     # get rid of .git directory, we won't need it.
     cd ..
     log "getting rid of .git directory for $1."
diff --git a/build/tools/fix-sysroot.sh b/build/tools/fix-sysroot.sh
new file mode 100755
index 0000000..6153850
--- /dev/null
+++ b/build/tools/fix-sysroot.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+if [ "x$ANDROID_NDK_ROOT" = "x" ]; then
+    cd `dirname $0`/../..
+    ANDROID_NDK_ROOT=`pwd`
+fi
+
+CPPHEADERS="cstddef cassert cctype cerrno cfloat climits cmath csetjmp csignal"
+CPPHEADERS="$CPPHEADERS cstdint cstdio cstdlib cstring ctime cwchar"
+CPPHEADERS="$CPPHEADERS cwctype_is_not_supported new stl_pair.h typeinfo utility"
+for i in $CPPHEADERS libstdc++.a libstdc++.so
+do
+    find $ANDROID_NDK_ROOT/build/platforms -name $i -exec rm -f '{}' \;
+done
diff --git a/build/tools/patch-sources.sh b/build/tools/patch-sources.sh
index 7376880..2004b30 100755
--- a/build/tools/patch-sources.sh
+++ b/build/tools/patch-sources.sh
@@ -57,7 +57,7 @@ parse_parameters ()
 extract_parameters $@
 parse_parameters $PARAMETERS
 
-PATCHES=`(cd $PATCHES_DIR && find . -name "*.patch") 2> /dev/null`
+PATCHES=`(cd $PATCHES_DIR && find . -name "*.patch" | sort) 2> /dev/null`
 if [ -z "$PATCHES" ] ; then
     log "No patches files in $PATCHES_DIR"
     exit 0
diff --git a/build/tools/patches/001-crystax.patch b/build/tools/patches/001-crystax.patch
new file mode 100644
index 0000000..cf93344
--- /dev/null
+++ b/build/tools/patches/001-crystax.patch
@@ -0,0 +1,98 @@
+diff --git a/gcc/gcc-4.2.1/gcc/config/arm/eabi.h b/gcc/gcc-4.2.1/gcc/config/arm/eabi.h
+index afe8155..388ad48 100644
+--- a/gcc/gcc-4.2.1/gcc/config/arm/eabi.h
++++ b/gcc/gcc-4.2.1/gcc/config/arm/eabi.h
+@@ -68,7 +68,6 @@
+    default. */
+ #undef ANDROID_CC1_SPEC
+ #define ANDROID_CC1_SPEC \
+-"%{!fexceptions:-fno-exceptions} " \
+ "%{!fno-pic:%{!fno-PIC:-fpic}} " \
+ "%{!fshort-enums:-fno-short-enums} " \
+ "%{!mno-thumb-interwork:-mthumb-interwork}"
+@@ -84,7 +83,7 @@
+ 
+ /* Android uses -fno-rtti by default. */
+ #undef ANDROID_CC1PLUS_SPEC
+-#define ANDROID_CC1PLUS_SPEC "%{!frtti:-fno-rtti}"
++#define ANDROID_CC1PLUS_SPEC ""
+ 
+ /* Default CC1PLUS_SPEC as in gcc.c. */
+ #undef CC1PLUS_DEFAULT_SPEC
+diff --git a/gcc/gcc-4.2.1/libiberty/getpagesize.c b/gcc/gcc-4.2.1/libiberty/getpagesize.c
+index f29f40c..c310b73 100644
+--- a/gcc/gcc-4.2.1/libiberty/getpagesize.c
++++ b/gcc/gcc-4.2.1/libiberty/getpagesize.c
+@@ -60,17 +60,6 @@ BUGS
+ # endif /* PAGESIZE */
+ #endif /* GNU_OUR_PAGESIZE */
+ 
+-/* ANDROID LOCAL BEGIN */
+-/* ANDROID FIXME: The declaration of getpagesize in Bionic's unistd.h
+-   is a static inline is different from one below.  */
+-#ifndef __ANDROID__ 
+-/* ANDROID LOCAL END */
+-int
+-getpagesize (void)
+-{
+-  return (GNU_OUR_PAGESIZE);
+-}
+-#endif /* ANDROID LOCAL */
+ 
+ #else /* VMS */
+ 
+diff --git a/gcc/gcc-4.4.0/gcc/config/arm/eabi.h b/gcc/gcc-4.4.0/gcc/config/arm/eabi.h
+index f398cdd..e6cbfcd 100644
+--- a/gcc/gcc-4.4.0/gcc/config/arm/eabi.h
++++ b/gcc/gcc-4.4.0/gcc/config/arm/eabi.h
+@@ -68,7 +68,6 @@
+    default. */
+ #undef ANDROID_CC1_SPEC
+ #define ANDROID_CC1_SPEC \
+-"%{!fexceptions:-fno-exceptions} " \
+ "%{!fno-pic:%{!fno-PIC:-fpic}} " \
+ "%{!fshort-enums:-fno-short-enums} " \
+ "%{!mno-thumb-interwork:-mthumb-interwork}"
+@@ -84,7 +83,7 @@
+ 
+ /* Android uses -fno-rtti by default. */
+ #undef ANDROID_CC1PLUS_SPEC
+-#define ANDROID_CC1PLUS_SPEC "%{!frtti:-fno-rtti}"
++#define ANDROID_CC1PLUS_SPEC ""
+ 
+ /* Default CC1PLUS_SPEC as in gcc.c. */
+ #undef CC1PLUS_DEFAULT_SPEC
+diff --git a/gcc/gcc-4.4.0/libiberty/getpagesize.c b/gcc/gcc-4.4.0/libiberty/getpagesize.c
+index 1c3a263..6bc4691 100644
+--- a/gcc/gcc-4.4.0/libiberty/getpagesize.c
++++ b/gcc/gcc-4.4.0/libiberty/getpagesize.c
+@@ -60,12 +60,6 @@ BUGS
+ # endif /* PAGESIZE */
+ #endif /* GNU_OUR_PAGESIZE */
+ 
+-int
+-getpagesize (void)
+-{
+-  return (GNU_OUR_PAGESIZE);
+-}
+-
+ #else /* VMS */
+ 
+ #if 0	/* older distributions of gcc-vms are missing <syidef.h> */
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/configure b/gcc/gcc-4.4.0/libstdc++-v3/configure
+index a0f84bc..da0e7d5 100755
+--- a/gcc/gcc-4.4.0/libstdc++-v3/configure
++++ b/gcc/gcc-4.4.0/libstdc++-v3/configure
+@@ -41940,6 +41940,12 @@ case "${host}" in
+     # This is a freestanding configuration; there is nothing to do here.
+     ;;
+ 
++  # ANDROID LOCAL BEGIN
++  arm*-*-eabi*)
++    # This is a freestanding configuration; there is nothing to do here.
++    ;;
++  # ANDROID LOCAL END
++
+   mips*-sde-elf*)
+     # These definitions are for the SDE C library rather than newlib.
+     SECTION_FLAGS='-ffunction-sections -fdata-sections'
diff --git a/build/tools/patches/002-unwind-find-exidx-weak-symbol.patch b/build/tools/patches/002-unwind-find-exidx-weak-symbol.patch
new file mode 100644
index 0000000..ffab111
--- /dev/null
+++ b/build/tools/patches/002-unwind-find-exidx-weak-symbol.patch
@@ -0,0 +1,26 @@
+diff --git a/gcc/gcc-4.2.1/gcc/config/arm/unwind-arm.c b/gcc/gcc-4.2.1/gcc/config/arm/unwind-arm.c
+index 41d89d1..976901e 100644
+--- a/gcc/gcc-4.2.1/gcc/config/arm/unwind-arm.c
++++ b/gcc/gcc-4.2.1/gcc/config/arm/unwind-arm.c
+@@ -43,7 +43,7 @@ bool __attribute__((weak)) __cxa_type_match(_Unwind_Control_Block *ucbp,
+ 					    const type_info *rttip,
+ 					    void **matched_object);
+ 
+-_Unwind_Ptr __attribute__((weak))
++_Unwind_Ptr
+ __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
+ 
+ /* Misc constants.  */
+diff --git a/gcc/gcc-4.4.0/gcc/config/arm/unwind-arm.c b/gcc/gcc-4.4.0/gcc/config/arm/unwind-arm.c
+index 4eb1821..4e92149 100644
+--- a/gcc/gcc-4.4.0/gcc/config/arm/unwind-arm.c
++++ b/gcc/gcc-4.4.0/gcc/config/arm/unwind-arm.c
+@@ -40,7 +40,7 @@ bool __attribute__((weak)) __cxa_type_match(_Unwind_Control_Block *ucbp,
+ 					    bool is_reference,
+ 					    void **matched_object);
+ 
+-_Unwind_Ptr __attribute__((weak))
++_Unwind_Ptr
+ __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
+ 
+ /* Misc constants.  */
diff --git a/build/tools/patches/003-setlocale.patch b/build/tools/patches/003-setlocale.patch
new file mode 100644
index 0000000..9800427
--- /dev/null
+++ b/build/tools/patches/003-setlocale.patch
@@ -0,0 +1,259 @@
+diff --git a/gcc/gcc-4.2.1/intl/localename.c b/gcc/gcc-4.2.1/intl/localename.c
+index 28c4ca7..bf0662c 100644
+--- a/gcc/gcc-4.2.1/intl/localename.c
++++ b/gcc/gcc-4.2.1/intl/localename.c
+@@ -393,6 +393,8 @@ _nl_locale_name (category, categoryname)
+      On some systems this can be done by the 'setlocale' function itself.  */
+ # if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
+   retval = setlocale (category, NULL);
++  if (retval == NULL || retval[0] == '\0')
++    retval = "C";
+ # else
+   /* Setting of LC_ALL overwrites all other.  */
+   retval = getenv ("LC_ALL");
+diff --git a/gcc/gcc-4.2.1/libstdc++-v3/config/locale/generic/c_locale.h b/gcc/gcc-4.2.1/libstdc++-v3/config/locale/generic/c_locale.h
+index d180c20..e5484c7 100644
+--- a/gcc/gcc-4.2.1/libstdc++-v3/config/locale/generic/c_locale.h
++++ b/gcc/gcc-4.2.1/libstdc++-v3/config/locale/generic/c_locale.h
+@@ -65,6 +65,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+ 		   const char* __fmt, ...)
+   {
+     char* __old = std::setlocale(LC_NUMERIC, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     char* __sav = NULL;
+     if (std::strcmp(__old, "C"))
+       {
+diff --git a/gcc/gcc-4.2.1/libstdc++-v3/config/locale/gnu/c_locale.h b/gcc/gcc-4.2.1/libstdc++-v3/config/locale/gnu/c_locale.h
+index f0b2b7d..98be1c7 100644
+--- a/gcc/gcc-4.2.1/libstdc++-v3/config/locale/gnu/c_locale.h
++++ b/gcc/gcc-4.2.1/libstdc++-v3/config/locale/gnu/c_locale.h
+@@ -82,6 +82,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+     __c_locale __old = __gnu_cxx::__uselocale(__cloc);
+ #else
+     char* __old = std::setlocale(LC_ALL, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     char* __sav = new char[std::strlen(__old) + 1];
+     std::strcpy(__sav, __old);
+     std::setlocale(LC_ALL, "C");
+diff --git a/gcc/gcc-4.4.0/intl/localename.c b/gcc/gcc-4.4.0/intl/localename.c
+index 28c4ca7..bf0662c 100644
+--- a/gcc/gcc-4.4.0/intl/localename.c
++++ b/gcc/gcc-4.4.0/intl/localename.c
+@@ -393,6 +393,8 @@ _nl_locale_name (category, categoryname)
+      On some systems this can be done by the 'setlocale' function itself.  */
+ # if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
+   retval = setlocale (category, NULL);
++  if (retval == NULL || retval[0] == '\0')
++    retval = "C";
+ # else
+   /* Setting of LC_ALL overwrites all other.  */
+   retval = getenv ("LC_ALL");
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/c_locale.cc b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/c_locale.cc
+index 3b9f1bd..0ae5a04 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/c_locale.cc
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/c_locale.cc
+@@ -51,6 +51,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+     {
+       // Assumes __s formatted for "C" locale.
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old[0] == '\0')
++        __old = "C";
+       const size_t __len = strlen(__old) + 1;
+       char* __sav = new char[__len];
+       memcpy(__sav, __old, __len);
+@@ -116,6 +118,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+     {
+       // Assumes __s formatted for "C" locale.
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old[0] == '\0')
++        __old = "C";
+       const size_t __len = strlen(__old) + 1;
+       char* __sav = new char[__len];
+       memcpy(__sav, __old, __len);
+@@ -161,6 +165,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+     {
+       // Assumes __s formatted for "C" locale.
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old[0] == '\0')
++        __old = "C";
+       const size_t __len = strlen(__old) + 1;
+       char* __sav = new char[__len];
+       memcpy(__sav, __old, __len);
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/c_locale.h b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/c_locale.h
+index 472ef55..c28cb97 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/c_locale.h
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/c_locale.h
+@@ -58,6 +58,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+ 		   const char* __fmt, ...)
+   {
+     char* __old = std::setlocale(LC_NUMERIC, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     char* __sav = NULL;
+     if (__builtin_strcmp(__old, "C"))
+       {
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/time_members.cc b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/time_members.cc
+index 86c9249..5b51595 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/time_members.cc
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/generic/time_members.cc
+@@ -43,6 +43,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+ 	   const tm* __tm) const
+     {
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old[0] == '\0')
++        __old = "C";
+       const size_t __llen = strlen(__old) + 1;
+       char* __sav = new char[__llen];
+       memcpy(__sav, __old, __llen);
+@@ -128,6 +130,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+ 	   const tm* __tm) const
+     {
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old[0] == '\0')
++        __old = "C";
+       const size_t __llen = strlen(__old) + 1;
+       char* __sav = new char[__llen];
+       memcpy(__sav, __old, __llen);
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/c_locale.h b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/c_locale.h
+index 749a97a..8157312 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/c_locale.h
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/c_locale.h
+@@ -72,6 +72,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+     __c_locale __old = __gnu_cxx::__uselocale(__cloc);
+ #else
+     char* __old = std::setlocale(LC_NUMERIC, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     char* __sav = NULL;
+     if (__builtin_strcmp(__old, "C"))
+       {
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/messages_members.cc b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/messages_members.cc
+index 1a42b38..e5f4d72 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/messages_members.cc
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/messages_members.cc
+@@ -45,6 +45,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+       return string(__msg);
+ #else
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old[0] == '\0')
++        __old = "C";
+       const size_t __len = strlen(__old) + 1;
+       char* __sav = new char[__len];
+       memcpy(__sav, __old, __len);
+@@ -68,6 +70,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+       return _M_convert_from_char(__msg);
+ # else
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old[0] == '\0')
++        __old = "C";
+       const size_t __len = strlen(__old) + 1;
+       char* __sav = new char[__len];
+       memcpy(__sav, __old, __len);
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/monetary_members.cc b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/monetary_members.cc
+index c5ec857..7b994bb 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/monetary_members.cc
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/monetary_members.cc
+@@ -428,6 +428,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+ #else
+ 	  // Switch to named locale so that mbsrtowcs will work.
+ 	  char* __old = setlocale(LC_ALL, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+           const size_t __llen = strlen(__old) + 1;
+           char* __sav = new char[__llen];
+           memcpy(__sav, __old, __llen);
+@@ -596,6 +598,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+ #else
+ 	  // Switch to named locale so that mbsrtowcs will work.
+ 	  char* __old = setlocale(LC_ALL, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+           const size_t __llen = strlen(__old) + 1;
+           char* __sav = new char[__llen];
+           memcpy(__sav, __old, __llen);
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/time_members.cc b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/time_members.cc
+index f80932c..4f79822 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/time_members.cc
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/locale/gnu/time_members.cc
+@@ -46,6 +46,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+ 					_M_c_locale_timepunct);
+ #else
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old == '\0')
++        __old = "C";
+       const size_t __llen = strlen(__old) + 1;  
+       char* __sav = new char[__llen];
+       memcpy(__sav, __old, __llen);
+@@ -202,6 +204,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
+ 					_M_c_locale_timepunct);
+ #else
+       char* __old = setlocale(LC_ALL, NULL);
++      if (__old == NULL || __old[0] == '\0')
++        __old = "C";
+       const size_t __llen = strlen(__old) + 1;
+       char* __sav = new char[__llen];
+       memcpy(__sav, __old, __llen);
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/os/gnu-linux/ctype_noninline.h b/gcc/gcc-4.4.0/libstdc++-v3/config/os/gnu-linux/ctype_noninline.h
+index 4fc47a9..68e4130 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/os/gnu-linux/ctype_noninline.h
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/os/gnu-linux/ctype_noninline.h
+@@ -45,6 +45,8 @@
+   {
+     const ctype_base::mask* __ret;
+     char* __old = setlocale(LC_CTYPE, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     char* __sav = NULL;
+     if (__builtin_strcmp(__old, "C"))
+       {
+@@ -87,6 +89,8 @@
+   _M_del(__table != 0 && __del), _M_widen_ok(0), _M_narrow_ok(0)
+   {
+     char* __old = setlocale(LC_CTYPE, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     char* __sav = NULL;
+     if (__builtin_strcmp(__old, "C"))
+       {    
+@@ -132,6 +136,8 @@
+   _M_del(__table != 0 && __del), _M_widen_ok(0), _M_narrow_ok(0)
+   {
+     char* __old = setlocale(LC_CTYPE, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     char* __sav = NULL;
+     if (__builtin_strcmp(__old, "C"))
+       {
+diff --git a/gcc/gcc-4.4.0/libstdc++-v3/config/os/tpf/ctype_noninline.h b/gcc/gcc-4.4.0/libstdc++-v3/config/os/tpf/ctype_noninline.h
+index 64917c4..6be8670 100644
+--- a/gcc/gcc-4.4.0/libstdc++-v3/config/os/tpf/ctype_noninline.h
++++ b/gcc/gcc-4.4.0/libstdc++-v3/config/os/tpf/ctype_noninline.h
+@@ -38,6 +38,8 @@
+   {
+     const ctype_base::mask* __ret;
+     char* __old = setlocale(LC_CTYPE, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     const size_t __len = __builtin_strlen(__old) + 1;
+     char* __sav = new char[__len];
+     __builtin_memcpy(__sav, __old, __len);
+@@ -53,6 +55,8 @@
+   : facet(__refs), _M_del(__table != 0 && __del)
+   {
+     char* __old = setlocale(LC_CTYPE, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     const size_t __len = __builtin_strlen(__old) + 1;
+     char* __sav = new char[__len];
+     __builtin_memcpy(__sav, __old, __len);
+@@ -69,6 +73,8 @@
+   : facet(__refs), _M_del(__table != 0 && __del)
+   {
+     char* __old = setlocale(LC_CTYPE, NULL);
++    if (__old == NULL || __old[0] == '\0')
++      __old = "C";
+     const size_t __len = __builtin_strlen(__old) + 1;
+     char* __sav = new char[__len];
+     __builtin_memcpy(__sav, __old, __len);
diff --git a/build/tools/patches/004-threading.patch b/build/tools/patches/004-threading.patch
new file mode 100644
index 0000000..dfb216b
--- /dev/null
+++ b/build/tools/patches/004-threading.patch
@@ -0,0 +1,105 @@
+diff --git a/build/Makefile.in b/build/Makefile.in
+index f66edc7..670b620 100644
+--- a/build/Makefile.in
++++ b/build/Makefile.in
+@@ -105,6 +105,8 @@ CFLAGS_FOR_TARGET= -O2 -Os -g
+ CXXFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET)
+ LDFLAGS_FOR_TARGET=-mandroid
+ 
++CFLAGS_FOR_TARGET+= -DANDROID -D__ANDROID__
++
+ # Helper
+ prefix-list = $(foreach e, $(2), $(join $1, $e))
+ 
+diff --git a/gcc/gcc-4.2.1/gcc/gthr-posix.h b/gcc/gcc-4.2.1/gcc/gthr-posix.h
+index 47d38a3..a8c4f46 100644
+--- a/gcc/gcc-4.2.1/gcc/gthr-posix.h
++++ b/gcc/gcc-4.2.1/gcc/gthr-posix.h
+@@ -58,6 +58,12 @@ typedef pthread_mutex_t __gthread_recursive_mutex_t;
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
+ #endif
+ 
++#if defined(ANDROID) || defined(__ANDROID__)
++# if defined(GTHREAD_USE_WEAK)
++#  undef GTHREAD_USE_WEAK
++# endif
++#endif
++
+ #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
+ # ifndef __gthrw_pragma
+ #  define __gthrw_pragma(pragma)
+@@ -83,7 +89,9 @@ __gthrw3(pthread_once)
+ __gthrw3(pthread_getspecific)
+ __gthrw3(pthread_setspecific)
+ __gthrw3(pthread_create)
++#if !defined(ANDROID) && !defined(__ANDROID__)
+ __gthrw3(pthread_cancel)
++#endif
+ __gthrw3(pthread_mutex_lock)
+ __gthrw3(pthread_mutex_trylock)
+ __gthrw3(pthread_mutex_unlock)
+@@ -93,7 +101,9 @@ __gthrw(pthread_once)
+ __gthrw(pthread_getspecific)
+ __gthrw(pthread_setspecific)
+ __gthrw(pthread_create)
++#if !defined(ANDROID) && !defined(__ANDROID__)
+ __gthrw(pthread_cancel)
++#endif
+ __gthrw(pthread_mutex_lock)
+ __gthrw(pthread_mutex_trylock)
+ __gthrw(pthread_mutex_unlock)
+@@ -203,7 +213,7 @@ static inline int
+ __gthread_active_p (void)
+ {
+   static void *const __gthread_active_ptr 
+-    = __extension__ (void *) &__gthrw_(pthread_cancel);
++    = __extension__ (void *) &__gthrw_(pthread_create);
+   return __gthread_active_ptr != 0;
+ }
+ 
+diff --git a/gcc/gcc-4.4.0/gcc/gthr-posix.h b/gcc/gcc-4.4.0/gcc/gthr-posix.h
+index 82a3c58..db2575d 100644
+--- a/gcc/gcc-4.4.0/gcc/gthr-posix.h
++++ b/gcc/gcc-4.4.0/gcc/gthr-posix.h
+@@ -65,6 +65,12 @@ typedef struct timespec __gthread_time_t;
+ #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
+ #define __GTHREAD_TIME_INIT {0,0}
+ 
++#if defined(ANDROID) || defined(__ANDROID__)
++# if defined(GTHREAD_USE_WEAK)
++#  undef GTHREAD_USE_WEAK
++# endif
++#endif
++
+ #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
+ # ifndef __gthrw_pragma
+ #  define __gthrw_pragma(pragma)
+@@ -95,7 +101,9 @@ __gthrw3(pthread_join)
+ __gthrw3(pthread_detach)
+ __gthrw3(pthread_equal)
+ __gthrw3(pthread_self)
++#if !defined(ANDROID) && !defined(__ANDROID__)
+ __gthrw3(pthread_cancel)
++#endif
+ __gthrw3(sched_yield)
+ 
+ __gthrw3(pthread_mutex_lock)
+@@ -124,7 +132,9 @@ __gthrw(pthread_join)
+ __gthrw(pthread_equal)
+ __gthrw(pthread_self)
+ __gthrw(pthread_detach)
++#if !defined(ANDROID) && !defined(__ANDROID__)
+ __gthrw(pthread_cancel)
++#endif
+ __gthrw(sched_yield)
+ 
+ __gthrw(pthread_mutex_lock)
+@@ -238,7 +248,7 @@ static inline int
+ __gthread_active_p (void)
+ {
+   static void *const __gthread_active_ptr 
+-    = __extension__ (void *) &__gthrw_(pthread_cancel);
++    = __extension__ (void *) &__gthrw_(pthread_create);
+   return __gthread_active_ptr != 0;
+ }
+ 
diff --git a/build/tools/patches/005-flags.patch b/build/tools/patches/005-flags.patch
new file mode 100644
index 0000000..b55f153
--- /dev/null
+++ b/build/tools/patches/005-flags.patch
@@ -0,0 +1,13 @@
+diff --git a/build/Makefile.in b/build/Makefile.in
+index 670b620..06c95e9 100644
+--- a/build/Makefile.in
++++ b/build/Makefile.in
+@@ -106,6 +106,8 @@ CXXFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET)
+ LDFLAGS_FOR_TARGET=-mandroid
+ 
+ CFLAGS_FOR_TARGET+= -DANDROID -D__ANDROID__
++CFLAGS_FOR_TARGET+= -fexceptions
++CXXFLAGS_FOR_TARGET+= -frtti
+ 
+ # Helper
+ prefix-list = $(foreach e, $(2), $(join $1, $e))
diff --git a/build/tools/rebuild-all-prebuilt.sh b/build/tools/rebuild-all-prebuilt.sh
index 46d780d..47023fa 100755
--- a/build/tools/rebuild-all-prebuilt.sh
+++ b/build/tools/rebuild-all-prebuilt.sh
@@ -61,6 +61,10 @@ OPTION_TRY_X86=no
 register_option "--try-x86" do_try_x86 "Build experimental x86 toolchain too."
 do_try_x86 () { OPTION_TRY_X86=yes; }
 
+OPTION_REMOVE_BUILD_DIR=yes
+register_option "--remove-build-dir=<yes|no>" do_remove_build_dir "Remove temporary build directory" "$OPTION_REMOVE_BUILD_DIR"
+do_remove_build_dir () { OPTION_REMOVE_BUILD_DIR=$1; }
+
 extract_parameters $@
 
 if [ "$OPTION_PACKAGE" = yes -a -z "$OPTION_NDK_DIR" ] ; then
@@ -151,7 +155,7 @@ fi # ! $TOOLCHAIN_SRC_DIR
 build_toolchain ()
 {
     dump "Building $1 toolchain... (this can be long)"
-    $PROGDIR/build-gcc.sh $FLAGS --build-out=$BUILD_DIR/toolchain-$1 $SRC_DIR $NDK_DIR $1
+    $PROGDIR/build-gcc.sh $FLAGS --build-out=$BUILD_DIR/toolchain-$1 --remove-build-dir=$OPTION_REMOVE_BUILD_DIR $SRC_DIR $NDK_DIR $1
     if [ $? != 0 ] ; then
         dump "ERROR: Could not build $1 toolchain!"
         exit 1
@@ -161,7 +165,7 @@ build_toolchain ()
 build_gdbserver ()
 {
     dump "Build $1 gdbserver..."
-    $PROGDIR/build-gdbserver.sh $FLAGS --build-out=$BUILD_DIR/gdbserver-$1 $SRC_DIR/gdb/gdb-$GDB_VERSION/gdb/gdbserver $NDK_DIR $1
+    $PROGDIR/build-gdbserver.sh $FLAGS --build-out=$BUILD_DIR/gdbserver-$1 --remove-build-dir=$OPTION_REMOVE_BUILD_DIR $SRC_DIR/gdb/gdb-$GDB_VERSION/gdb/gdbserver $NDK_DIR $1
     if [ $? != 0 ] ; then
         dump "ERROR: Could not build $1 toolchain!"
         exit 1
@@ -188,11 +192,35 @@ fi
 #    exit 1
 #fi
 
+# CrystaX BEGIN
+
+# Replace android libstdc++ simulation to GCC's (full) version
+dump "Replacing android libstdc++ simulation to GCC's (full) version"
+/bin/sh $PROGDIR/fix-sysroot.sh
+if [ $? != 0 ] ; then
+    dump "ERROR: Could not fix sysroot!"
+    exit 1
+fi
+
+# Build library containing missing functions
+dump "Build library containing some missing functionality (sysinfo etc)"
+/bin/sh $PROGDIR/build-missing.sh $NDK_DIR
+if [ $? != 0 ] ; then
+    dump "ERROR: Could not build 'missing' library!"
+    exit 1
+fi
+
+# CrystaX END
+
 if [ "$OPTION_PACKAGE" = yes ] ; then
     RELEASE=`date +%Y%m%d`
     dump "Packaging prebuilt binaries..."
     PREBUILT_PACKAGE=/tmp/android-ndk-prebuilt-$RELEASE-$HOST_TAG.tar.bz2
-    cd $NDK_DIR && tar cjf $PREBUILT_PACKAGE *
+    for i in 1 2 3 4 5 ; do
+        rm -f $PREBUILT_PACKAGE
+        cd $NDK_DIR && tar cjf $PREBUILT_PACKAGE * && break
+        sleep 2
+    done
     if [ $? != 0 ] ; then
         dump "ERROR: Could not package prebuilt binaries!"
         exit 1
diff --git a/ndk-build b/ndk-build
index 625368e..b85d0dd 100755
--- a/ndk-build
+++ b/ndk-build
@@ -86,4 +86,4 @@ if [ "$OSTYPE" = "cygwin" ] ; then
     fi
 fi
 
-$GNUMAKE -f $PROGDIR/build/core/build-local.mk $@
+$GNUMAKE -f $PROGDIR/build/core/build-local.mk "$@"
diff --git a/samples/hello-exe/jni/Android.mk b/samples/hello-exe/jni/Android.mk
new file mode 100644
index 0000000..70c8322
--- /dev/null
+++ b/samples/hello-exe/jni/Android.mk
@@ -0,0 +1,24 @@
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_CPP_EXTENSION := cc
+LOCAL_MODULE    := hello-exe
+LOCAL_SRC_FILES := main.cc
+LOCAL_LDLIBS    := -llog
+
+include $(BUILD_EXECUTABLE)
diff --git a/samples/hello-exe/jni/main.cc b/samples/hello-exe/jni/main.cc
new file mode 100644
index 0000000..b770b34
--- /dev/null
+++ b/samples/hello-exe/jni/main.cc
@@ -0,0 +1,24 @@
+#include <cstdio>
+#include <cstdlib>
+#include <stdexcept>
+#include <android/log.h>
+
+static const char TAG[] = "hello-exe";
+
+int main(int argc, char* argv[])
+{
+    __android_log_print(ANDROID_LOG_INFO, TAG, "Start hello-exe");
+    try
+    {
+        if (std::getenv("NON_EXISTENT_ENVIRONMENT_VARIABLE") == NULL)
+            throw std::runtime_error("Hello, world (with full C++ support) !");
+        std::printf("Hello, world!\n");
+    }
+    catch (std::exception &ex)
+    {
+        std::printf("%s\n", ex.what());
+    }
+    __android_log_print(ANDROID_LOG_INFO, TAG, "Finish hello-exe");
+
+    return 0;
+}
diff --git a/samples/hello-jni/jni/Android.mk b/samples/hello-jni/jni/Android.mk
index 6ccf381..00035db 100644
--- a/samples/hello-jni/jni/Android.mk
+++ b/samples/hello-jni/jni/Android.mk
@@ -16,7 +16,8 @@ LOCAL_PATH := $(call my-dir)
 
 include $(CLEAR_VARS)
 
+LOCAL_CPP_EXTENSION := cc
 LOCAL_MODULE    := hello-jni
-LOCAL_SRC_FILES := hello-jni.c
+LOCAL_SRC_FILES := hello-jni.cc
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/samples/hello-jni/jni/hello-jni.c b/samples/hello-jni/jni/hello-jni.c
deleted file mode 100644
index 632ac17..0000000
--- a/samples/hello-jni/jni/hello-jni.c
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-#include <string.h>
-#include <jni.h>
-
-/* This is a trivial JNI example where we use a native method
- * to return a new VM String. See the corresponding Java source
- * file located at:
- *
- *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
- */
-jstring
-Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
-                                                  jobject thiz )
-{
-    return (*env)->NewStringUTF(env, "Hello from JNI !");
-}
diff --git a/samples/hello-jni/jni/hello-jni.cc b/samples/hello-jni/jni/hello-jni.cc
new file mode 100644
index 0000000..9d0fa62
--- /dev/null
+++ b/samples/hello-jni/jni/hello-jni.cc
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#include <jni.h>
+#include <cstdlib>
+#include <stdexcept>
+#include <string>
+
+/* This is a trivial JNI example where we use a native method
+ * to return a new VM String. See the corresponding Java source
+ * file located at:
+ *
+ *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
+ */
+extern "C"
+jstring
+Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
+                                                  jobject thiz )
+{
+    std::string s = "Hello from JNI !";
+    try
+    {
+        if (std::getenv("NON_EXISTENT_ENVIRONMENT_VARIABLE") == NULL)
+            throw std::runtime_error("Hello from JNI (with full C++ support) !");
+    }
+    catch (std::exception &ex)
+    {
+        s = ex.what();
+    }
+    return env->NewStringUTF(s.c_str());
+}
diff --git a/samples/missing/AndroidManifest.xml b/samples/missing/AndroidManifest.xml
new file mode 100644
index 0000000..52e0916
--- /dev/null
+++ b/samples/missing/AndroidManifest.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+      package="net.crystax.ndk.missing"
+      android:versionCode="1"
+      android:versionName="1.0">
+    <uses-sdk android:minSdkVersion="3" />
+</manifest> 
diff --git a/samples/missing/jni/Android.mk b/samples/missing/jni/Android.mk
new file mode 100644
index 0000000..8125461
--- /dev/null
+++ b/samples/missing/jni/Android.mk
@@ -0,0 +1,24 @@
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE    := missing
+LOCAL_SRC_FILES := \
+	wchar.c \
+	sysinfo.S
+
+include $(BUILD_STATIC_LIBRARY)
diff --git a/samples/missing/jni/Application.mk b/samples/missing/jni/Application.mk
new file mode 100644
index 0000000..df473ba
--- /dev/null
+++ b/samples/missing/jni/Application.mk
@@ -0,0 +1,3 @@
+APP_PROJECT_PATH := $(call my-dir)
+APP_BUILD_SCRIPT := $(call my-dir)/Android.mk
+APP_MODULES      := missing
diff --git a/samples/missing/jni/sysinfo.S b/samples/missing/jni/sysinfo.S
new file mode 100644
index 0000000..9d0c793
--- /dev/null
+++ b/samples/missing/jni/sysinfo.S
@@ -0,0 +1,20 @@
+#include <sys/linux-syscalls.h> 
+
+#define __NR_sysinfo 116
+
+    .text 
+    .type sysinfo, #function 
+    .globl sysinfo 
+    .align 4 
+    .fnstart 
+
+sysinfo: 
+    .save   {r4, r7} 
+    stmfd   sp!, {r4, r7} 
+    ldr     r7, =__NR_sysinfo 
+    swi     #0 
+    ldmfd   sp!, {r4, r7} 
+    movs    r0, r0 
+    bxpl    lr 
+    b       __set_syscall_errno 
+    .fnend
diff --git a/samples/missing/jni/wchar.c b/samples/missing/jni/wchar.c
new file mode 100644
index 0000000..1f4461b
--- /dev/null
+++ b/samples/missing/jni/wchar.c
@@ -0,0 +1,6 @@
+#include <wchar.h>
+#include <stdlib.h>
+
+__BEGIN_DECLS
+
+__END_DECLS
