Fix system and GABI++ headers.

The C++ headers provided by the 'sytem' and 'gabi++' runtimes do
not compile for API level < 9 (cstdlib), or at all (stl_pair.h).

This patch fixes these issues, and also adds a regression test.

Change-Id: Ia78b3871039ed40dd2089da96bd775e9c2d359cd
diff --git a/sources/cxx-stl/gabi++/include/cstdlib b/sources/cxx-stl/gabi++/include/cstdlib
index bb6f5a5..ef29fa7 100644
--- a/sources/cxx-stl/gabi++/include/cstdlib
+++ b/sources/cxx-stl/gabi++/include/cstdlib
@@ -50,7 +50,6 @@
 using ::putenv;
 using ::setenv;
 using ::unsetenv;
-using ::clearenv;
 
 using ::mktemp;
 using ::mkstemp;
@@ -112,6 +111,11 @@
 using ::mbtowc;
 using ::wctomb;
 using ::wcstombs;
+
+#if __ANDROID_API__ >= 9
+using ::clearenv;
+#endif
+
 }  // namespace std
 
 }  // extern C++
diff --git a/sources/cxx-stl/gabi++/include/stl_pair.h b/sources/cxx-stl/gabi++/include/stl_pair.h
index 37f757b..648f213 100644
--- a/sources/cxx-stl/gabi++/include/stl_pair.h
+++ b/sources/cxx-stl/gabi++/include/stl_pair.h
@@ -58,6 +58,14 @@
 #ifndef __SGI_STL_INTERNAL_PAIR_H
 #define __SGI_STL_INTERNAL_PAIR_H
 
+#ifndef __STL_BEGIN_NAMESPACE
+#define __STL_BEGIN_NAMESPACE namespace std {
+#endif
+
+#ifndef __STL_END_NAMESPACE
+#define __STL_END_NAMESPACE   }
+#endif
+
 __STL_BEGIN_NAMESPACE
 
 template <class _T1, class _T2>
diff --git a/sources/cxx-stl/system/include/cstdlib b/sources/cxx-stl/system/include/cstdlib
index bb6f5a5..ef29fa7 100644
--- a/sources/cxx-stl/system/include/cstdlib
+++ b/sources/cxx-stl/system/include/cstdlib
@@ -50,7 +50,6 @@
 using ::putenv;
 using ::setenv;
 using ::unsetenv;
-using ::clearenv;
 
 using ::mktemp;
 using ::mkstemp;
@@ -112,6 +111,11 @@
 using ::mbtowc;
 using ::wctomb;
 using ::wcstombs;
+
+#if __ANDROID_API__ >= 9
+using ::clearenv;
+#endif
+
 }  // namespace std
 
 }  // extern C++
diff --git a/sources/cxx-stl/system/include/stl_pair.h b/sources/cxx-stl/system/include/stl_pair.h
index 37f757b..648f213 100644
--- a/sources/cxx-stl/system/include/stl_pair.h
+++ b/sources/cxx-stl/system/include/stl_pair.h
@@ -58,6 +58,14 @@
 #ifndef __SGI_STL_INTERNAL_PAIR_H
 #define __SGI_STL_INTERNAL_PAIR_H
 
+#ifndef __STL_BEGIN_NAMESPACE
+#define __STL_BEGIN_NAMESPACE namespace std {
+#endif
+
+#ifndef __STL_END_NAMESPACE
+#define __STL_END_NAMESPACE   }
+#endif
+
 __STL_BEGIN_NAMESPACE
 
 template <class _T1, class _T2>
diff --git a/tests/build/system-cpp-headers/jni/Android.mk b/tests/build/system-cpp-headers/jni/Android.mk
new file mode 100644
index 0000000..1f1f659
--- /dev/null
+++ b/tests/build/system-cpp-headers/jni/Android.mk
@@ -0,0 +1,15 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := test_system_cpp_headers
+LOCAL_SRC_FILES := main.cpp
+LOCAL_C_INCLUDES := $(NDK_ROOT)/sources/cxx-stl/system/include
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := test_gabixx_cpp_headers
+LOCAL_SRC_FILES := main.cpp
+LOCAL_STATIC_LIBRARIES := libgabi++_static
+include $(BUILD_EXECUTABLE)
+
+include $(NDK_ROOT)/sources/cxx-stl/gabi++/Android.mk
diff --git a/tests/build/system-cpp-headers/jni/Application.mk b/tests/build/system-cpp-headers/jni/Application.mk
new file mode 100644
index 0000000..7a8e91a
--- /dev/null
+++ b/tests/build/system-cpp-headers/jni/Application.mk
@@ -0,0 +1,2 @@
+APP_STL := none
+APP_PLATFORM := android-3
diff --git a/tests/build/system-cpp-headers/jni/main.cpp b/tests/build/system-cpp-headers/jni/main.cpp
new file mode 100644
index 0000000..81b71cd
--- /dev/null
+++ b/tests/build/system-cpp-headers/jni/main.cpp
@@ -0,0 +1,25 @@
+// Check that including <cstdlib> doesn't result in a build error
+// with API level 3.
+#include <cassert>
+#include <cctype>
+#include <cerrno>
+#include <climits>
+#include <cmath>
+#include <csetjmp>
+#include <csignal>
+#include <cstddef>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <ctime>
+#include <cwchar>
+#include <new>
+#include <new>
+#include <stl_pair.h>
+#include <typeinfo>
+#include <utility>
+
+int main(void) {
+  return 0;
+}