Merge in some LOCALMODs
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b1e2535..3a52df3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -486,7 +486,15 @@
 endif()
 
 # Configure compiler.
-include(config-ix)
+# @LOCALMOD-START Use a custom compiler config because cmake try_compile always
+# tries to link an executable and linking doesnt work during bootstrap without
+# libnacl.
+if (CMAKE_SYSTEM_NAME STREQUAL "nacl")
+  include(config-ix-pnacl)
+else()
+  include(config-ix)
+endif()
+# @LOCALMOD-END
 
 # Configure coverage options.
 if (LIBCXX_GENERATE_COVERAGE)
diff --git a/cmake/config-ix-pnacl.cmake b/cmake/config-ix-pnacl.cmake
new file mode 100644
index 0000000..829cfcc
--- /dev/null
+++ b/cmake/config-ix-pnacl.cmake
@@ -0,0 +1,46 @@
+# @LOCALMOD-START The PNaCl driver automatically adds dependencies to
+#                 libc++ which hasn't been built yet. The tests for
+#                 flags therefore fail because the library can't be
+#                 found. We should fix this in a different CL, and add
+#                 proper detection for newlib's lack of locale_t
+#                 support. See:
+#                  https://code.google.com/p/nativeclient/issues/detail?id=3661
+
+set(LIBCXX_HAS_STDCXX0X_FLAG 1)
+set(LIBCXX_HAS_STDCXX11_FLAG 1)
+set(LIBCXX_HAS_FPIC_FLAG 0) # Not quite true, but untested in PNaCl.
+set(LIBCXX_HAS_NODEFAULTLIBS_FLAG 1)
+set(LIBCXX_HAS_NOSTDINCXX_FLAG 1)
+set(LIBCXX_HAS_WALL_FLAG 1)
+set(LIBCXX_HAS_W_FLAG 1)
+set(LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG 1)
+set(LIBCXX_HAS_WWRITE_STRINGS_FLAG 1)
+set(LIBCXX_HAS_WNO_LONG_LONG_FLAG 1)
+set(LIBCXX_HAS_PEDANTIC_FLAG 1)
+set(LIBCXX_HAS_WERROR_FLAG 1)
+set(LIBCXX_HAS_WNO_ERROR_FLAG 1)
+set(LIBCXX_HAS_FNO_EXCEPTIONS_FLAG 1)
+set(LIBCXX_HAS_FNO_RTTI_FLAG 1)
+# MSVC flags:
+set(LIBCXX_HAS_WX_FLAG 0)
+set(LIBCXX_HAS_NO_WX_FLAG 0)
+set(LIBCXX_HAS_EHSC_FLAG 0)
+set(LIBCXX_HAS_NO_EHS_FLAG 0)
+set(LIBCXX_HAS_NO_EHA_FLAG 0)
+set(LIBCXX_HAS_NO_GR_FLAG 0)
+
+set(LIBCXX_HAS_PTHREAD_LIB 1)
+set(LIBCXX_HAS_C_LIB 1)
+set(LIBCXX_HAS_M_LIB 1)
+set(LIBCXX_HAS_RT_LIB 0)
+set(LIBCXX_HAS_GCC_S_LIB 0)
+# @LOCALMOD-END
+
+# Check C++0x features
+if (LIBCXX_ENABLE_CXX0X)
+  if (LIBCXX_HAS_STDCXX0X_FLAG)
+    set(CMAKE_REQUIRED_DEFINITIONS -std=c++0x)
+  endif()
+else()
+  set(LIBCXX_HAS_STDCXX0X_FLAG FALSE)
+endif()
diff --git a/include/atomic b/include/atomic
index 0f6aee8..768e758 100644
--- a/include/atomic
+++ b/include/atomic
@@ -1601,12 +1601,24 @@
   static _LIBCPP_CONSTEXPR bool is_always_lock_free = __atomic_always_lock_free(sizeof(__a_), 0);
 #endif
 
+  // @LOCALMOD-BEGIN The NaCl builtin delays resolution of the lock-free
+  //                 property until translation time (time at which the
+  //                 actual target is known).
+#if defined (__pnacl__) || (defined(__mips__) && defined (__native_client__))
+    _LIBCPP_INLINE_VISIBILITY
+    bool is_lock_free() const volatile _NOEXCEPT
+        {return __nacl_atomic_is_lock_free(sizeof(_Tp), &__a_);}
+    _LIBCPP_INLINE_VISIBILITY
+    bool is_lock_free() const _NOEXCEPT
+        {return __nacl_atomic_is_lock_free(sizeof(_Tp), &__a_);}
+#else
     _LIBCPP_INLINE_VISIBILITY
     bool is_lock_free() const volatile _NOEXCEPT
         {return __cxx_atomic_is_lock_free(sizeof(_Tp));}
     _LIBCPP_INLINE_VISIBILITY
     bool is_lock_free() const _NOEXCEPT
         {return static_cast<__atomic_base const volatile*>(this)->is_lock_free();}
+#endif // @LOCALMOD-END
     _LIBCPP_INLINE_VISIBILITY
     void store(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
       _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
diff --git a/include/cstdio b/include/cstdio
index b480f80..a207c3f 100644
--- a/include/cstdio
+++ b/include/cstdio
@@ -140,9 +140,14 @@
 #endif
 using ::ftell _LIBCPP_USING_IF_EXISTS;
 using ::rewind _LIBCPP_USING_IF_EXISTS;
-using ::clearerr _LIBCPP_USING_IF_EXISTS;
-using ::feof _LIBCPP_USING_IF_EXISTS;
-using ::ferror _LIBCPP_USING_IF_EXISTS;
+// @LOCALMOD-START
+#undef clearerr
+using ::clearerr;
+#undef feof
+using ::feof;
+#undef ferror
+using ::ferror;
+// @LOCALMOD-END
 using ::perror _LIBCPP_USING_IF_EXISTS;
 
 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
diff --git a/include/math.h b/include/math.h
index 77762d5..484df3a 100644
--- a/include/math.h
+++ b/include/math.h
@@ -1455,7 +1455,11 @@
 // log2
 
 inline _LIBCPP_INLINE_VISIBILITY float       log2(float __lcpp_x) _NOEXCEPT       {return ::log2f(__lcpp_x);}
+#if defined(_NEWLIB_VERSION) // @LOCALMOD-START Missing from newlib. Note that PNaCl represents ``long double`` as ``double``, hence the intrinsic's type.
+inline _LIBCPP_INLINE_VISIBILITY long double log2(long double __lcpp_x) _NOEXCEPT {return __builtin_log2(__lcpp_x);} // TODO(fabiansommer): does this need the :: ?
+#else
 inline _LIBCPP_INLINE_VISIBILITY long double log2(long double __lcpp_x) _NOEXCEPT {return ::log2l(__lcpp_x);}
+#endif // @LOCALMOD-END
 
 template <class _A1>
 inline _LIBCPP_INLINE_VISIBILITY
@@ -1465,7 +1469,11 @@
 // logb
 
 inline _LIBCPP_INLINE_VISIBILITY float       logb(float __lcpp_x) _NOEXCEPT       {return ::logbf(__lcpp_x);}
+#if defined(_NEWLIB_VERSION) // @LOCALMOD-START Missing from newlib. Note that PNaCl represents ``long double`` as ``double``, hence the intrinsic's type.
+inline _LIBCPP_INLINE_VISIBILITY long double logb(long double __lcpp_x) _NOEXCEPT {return __builtin_logb(__lcpp_x);}
+#else
 inline _LIBCPP_INLINE_VISIBILITY long double logb(long double __lcpp_x) _NOEXCEPT {return ::logbl(__lcpp_x);}
+#endif // @LOCALMOD-END
 
 template <class _A1>
 inline _LIBCPP_INLINE_VISIBILITY
@@ -1569,6 +1577,15 @@
 
 // nexttoward
 
+#if defined(_NEWLIB_VERSION) // @LOCALMOD-START Missing from newlib. Note that PNaCl represents ``long double`` as ``double``, hence the intrinsic's type.
+inline _LIBCPP_INLINE_VISIBILITY float       nexttoward(float __lcpp_x, long double __lcpp_y) _NOEXCEPT       {return __builtin_nexttoward(__lcpp_x, __lcpp_y);}
+inline _LIBCPP_INLINE_VISIBILITY long double nexttoward(long double __lcpp_x, long double __lcpp_y) _NOEXCEPT {return __builtin_nexttoward(__lcpp_x, __lcpp_y);}
+
+template <class _A1>
+inline _LIBCPP_INLINE_VISIBILITY
+typename std::enable_if<std::is_integral<_A1>::value, double>::type
+nexttoward(_A1 __lcpp_x, long double __lcpp_y) _NOEXCEPT {return __builtin_nexttoward((double)__lcpp_x, __lcpp_y);}
+#else
 inline _LIBCPP_INLINE_VISIBILITY float       nexttoward(float __lcpp_x, long double __lcpp_y) _NOEXCEPT       {return ::nexttowardf(__lcpp_x, __lcpp_y);}
 inline _LIBCPP_INLINE_VISIBILITY long double nexttoward(long double __lcpp_x, long double __lcpp_y) _NOEXCEPT {return ::nexttowardl(__lcpp_x, __lcpp_y);}
 
@@ -1576,6 +1593,7 @@
 inline _LIBCPP_INLINE_VISIBILITY
 typename std::enable_if<std::is_integral<_A1>::value, double>::type
 nexttoward(_A1 __lcpp_x, long double __lcpp_y) _NOEXCEPT {return ::nexttoward((double)__lcpp_x, __lcpp_y);}
+#endif // @LOCALMOD-END
 
 // remainder
 
diff --git a/src/include/config_elast.h b/src/include/config_elast.h
index 7880c73..109ef68 100644
--- a/src/include/config_elast.h
+++ b/src/include/config_elast.h
@@ -31,6 +31,10 @@
 // No _LIBCPP_ELAST needed on WASI
 #elif defined(__linux__) || defined(_LIBCPP_HAS_MUSL_LIBC)
 #define _LIBCPP_ELAST 4095
+// @LOCALMOD-START
+#elif defined(__native_client__)
+// NaCl doesn't have ELAST or __ELASTERROR
+// @LOCALMOD-END
 #elif defined(__APPLE__)
 // No _LIBCPP_ELAST needed on Apple
 #elif defined(__sun__)