Merge "Set search path to ro.vndk.version property"
diff --git a/benchmarks/math_benchmark.cpp b/benchmarks/math_benchmark.cpp
index 7b9a283..4d9d3cb 100644
--- a/benchmarks/math_benchmark.cpp
+++ b/benchmarks/math_benchmark.cpp
@@ -69,16 +69,11 @@
 }
 BIONIC_BENCHMARK(BM_math_isfinite_macro);
 
-#if defined(__BIONIC__)
-#define test_isfinite __isfinite
-#else
-#define test_isfinite __finite
-#endif
 static void BM_math_isfinite(benchmark::State& state) {
   d = 0.0;
   v = values[state.range(0)];
   while (state.KeepRunning()) {
-    d += test_isfinite(v);
+    d += isfinite(v);
   }
   SetLabel(state);
 }
@@ -134,17 +129,15 @@
 }
 BIONIC_BENCHMARK(BM_math_isnormal_macro);
 
-#if defined(__BIONIC__)
 static void BM_math_isnormal(benchmark::State& state) {
   d = 0.0;
   v = values[state.range(0)];
   while (state.KeepRunning()) {
-    d += (__isnormal)(v);
+    d += isnormal(v);
   }
   SetLabel(state);
 }
 BIONIC_BENCHMARK(BM_math_isnormal);
-#endif
 
 static void BM_math_sin_fast(benchmark::State& state) {
   d = 1.0;
@@ -202,7 +195,7 @@
   d = 0.0;
   v = values[state.range(0)];
   while (state.KeepRunning()) {
-    d += (__signbit)(v);
+    d += signbit(v);
   }
   SetLabel(state);
 }
diff --git a/docs/32-bit-abi.md b/docs/32-bit-abi.md
index eecbe6d..5e3ae45 100644
--- a/docs/32-bit-abi.md
+++ b/docs/32-bit-abi.md
@@ -57,3 +57,14 @@
 Android all use the 32-bit `time_t`.
 
 In the 64-bit ABI, `time_t` is 64-bit.
+
+`pthread_mutex_t` is too small for large pids
+---------------------------------------------
+
+This doesn't generally affect Android devices, because on devices
+`/proc/sys/kernel/pid_max` is usually too small to hit the 16-bit limit,
+but 32-bit bionic's `pthread_mutex` is a total of 32 bits, leaving just
+16 bits for the owner thread id. This means bionic isn't able to support
+mutexes for tids that don't fit in 16 bits. This typically manifests as
+a hang in `pthread_mutex_lock` if the libc startup code doesn't detect
+this condition and abort.
diff --git a/docs/status.md b/docs/status.md
index c119439..cd40e2c 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -9,6 +9,7 @@
 New libc functions in P:
   * `__freading`/`__fwriting` (completing <stdio_ext.h>)
   * `endhostent`/endnetent`/`endprotoent`/`getnetent`/`getprotoent`/`sethostent`/`setnetent`/`setprotoent` (completing <netdb.h>)
+  * `fexecve`
   * `getentropy`/`getrandom` (adding <sys/random.h>)
   * `getlogin_r`
   * `glob`/`globfree` (adding <glob.h>)
@@ -16,6 +17,7 @@
   * `iconv`/`iconv_close`/`iconv_open` (adding <iconv.h>)
   * `pthread_setschedprio`
   * <spawn.h>
+  * `swab`
   * `syncfs`
 
 New libc functions in O:
@@ -71,10 +73,6 @@
 aio_return
 aio_suspend
 aio_write
-fexecve
-fmtmsg
-getdate
-getdate_err
 lio_listio
 pthread_attr_getinheritsched
 pthread_attr_setinheritsched
@@ -91,8 +89,6 @@
 pthread_setcancelstate
 pthread_setcanceltype
 pthread_testcancel
-sockatmark
-swab
 wordexp
 wordfree
 ```
diff --git a/libc/Android.bp b/libc/Android.bp
index 81135ea..2e5ec00 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1534,6 +1534,7 @@
         "bionic/strings_l.cpp",
         "bionic/strsignal.cpp",
         "bionic/strtold.cpp",
+        "bionic/swab.cpp",
         "bionic/symlink.cpp",
         "bionic/sync_file_range.cpp",
         "bionic/sys_msg.cpp",
diff --git a/libc/NOTICE b/libc/NOTICE
index c0e6265..cbcafc8 100644
--- a/libc/NOTICE
+++ b/libc/NOTICE
@@ -3812,32 +3812,6 @@
 -------------------------------------------------------------------
 
 Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-   notice, this list of conditions and the following disclaimer.
-2. 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.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
-
--------------------------------------------------------------------
-
-Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
 Copyright (c) 2002 David Schultz <das@FreeBSD.ORG>
 All rights reserved.
 
diff --git a/libc/bionic/__libc_init_main_thread.cpp b/libc/bionic/__libc_init_main_thread.cpp
index 9cbff11..d7581dd 100644
--- a/libc/bionic/__libc_init_main_thread.cpp
+++ b/libc/bionic/__libc_init_main_thread.cpp
@@ -31,6 +31,7 @@
 #include "private/KernelArgumentBlock.h"
 #include "private/bionic_arc4random.h"
 #include "private/bionic_auxv.h"
+#include "private/bionic_defs.h"
 #include "private/bionic_globals.h"
 #include "private/bionic_ssp.h"
 #include "pthread_internal.h"
@@ -58,6 +59,7 @@
 // -fno-stack-protector because it's responsible for setting up the main
 // thread's TLS (which stack protector relies on).
 
+__BIONIC_WEAK_FOR_NATIVE_BRIDGE
 void __libc_init_main_thread(KernelArgumentBlock& args) {
   __libc_auxv = args.auxv;
 #if defined(__i386__)
diff --git a/libc/bionic/android_set_abort_message.cpp b/libc/bionic/android_set_abort_message.cpp
index 8c80c5a..268c4f3 100644
--- a/libc/bionic/android_set_abort_message.cpp
+++ b/libc/bionic/android_set_abort_message.cpp
@@ -32,6 +32,7 @@
 #include <string.h>
 #include <sys/mman.h>
 
+#include "private/bionic_defs.h"
 #include "private/ScopedPthreadMutexLocker.h"
 
 static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER;
@@ -43,6 +44,7 @@
 
 abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common.
 
+__BIONIC_WEAK_FOR_NATIVE_BRIDGE
 void android_set_abort_message(const char* msg) {
   ScopedPthreadMutexLocker locker(&g_abort_msg_lock);
 
diff --git a/libc/bionic/exec.cpp b/libc/bionic/exec.cpp
index 2001106..1cf3a58 100644
--- a/libc/bionic/exec.cpp
+++ b/libc/bionic/exec.cpp
@@ -39,6 +39,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "private/FdPath.h"
+
 extern "C" char** environ;
 
 enum ExecVariant { kIsExecL, kIsExecLE, kIsExecLP };
@@ -170,3 +172,10 @@
   if (saw_EACCES) errno = EACCES;
   return -1;
 }
+
+int fexecve(int fd, char* const* argv, char* const* envp) {
+  // execveat with AT_EMPTY_PATH (>= 3.19) seems to offer no advantages.
+  execve(FdPath(fd).c_str(), argv, envp);
+  if (errno == ENOENT) errno = EBADF;
+  return -1;
+}
diff --git a/libc/bionic/fchmod.cpp b/libc/bionic/fchmod.cpp
index ace8c6b..a486aae 100644
--- a/libc/bionic/fchmod.cpp
+++ b/libc/bionic/fchmod.cpp
@@ -33,13 +33,14 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#include "private/FdPath.h"
+
 extern "C" int ___fchmod(int, mode_t);
 
 int fchmod(int fd, mode_t mode) {
   int saved_errno = errno;
   int result = ___fchmod(fd, mode);
-
-  if ((result == 0) || (errno != EBADF)) {
+  if (result == 0 || errno != EBADF) {
     return result;
   }
 
@@ -52,16 +53,14 @@
   // on an O_PATH file descriptor, and "man open" documents fchmod
   // on O_PATH file descriptors as returning EBADF.
   int fd_flag = fcntl(fd, F_GETFL);
-  if ((fd_flag == -1) || ((fd_flag & O_PATH) == 0)) {
+  if (fd_flag == -1 || (fd_flag & O_PATH) == 0) {
     errno = EBADF;
     return -1;
   }
 
-  char buf[40];
-  snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
   errno = saved_errno;
-  result = chmod(buf, mode);
-  if ((result == -1) && (errno == ELOOP)) {
+  result = chmod(FdPath(fd).c_str(), mode);
+  if (result == -1 && errno == ELOOP) {
     // Linux does not support changing the mode of a symlink.
     // For fchmodat(AT_SYMLINK_NOFOLLOW), POSIX requires a return
     // value of ENOTSUP. Assume that's true here too.
diff --git a/libc/bionic/fgetxattr.cpp b/libc/bionic/fgetxattr.cpp
index 6d999bf..38b7ac3 100644
--- a/libc/bionic/fgetxattr.cpp
+++ b/libc/bionic/fgetxattr.cpp
@@ -33,13 +33,15 @@
 #include <fcntl.h>
 #include <stdio.h>
 
+#include "private/FdPath.h"
+
 extern "C" ssize_t ___fgetxattr(int, const char*, void*, size_t);
 
 ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) {
   int saved_errno = errno;
   ssize_t result = ___fgetxattr(fd, name, value, size);
 
-  if ((result != -1) || (errno != EBADF)) {
+  if (result != -1 || errno != EBADF) {
     return result;
   }
 
@@ -47,13 +49,11 @@
   // may not directly support fgetxattr() on such a file descriptor.
   // Use /proc/self/fd instead to emulate this support.
   int fd_flag = fcntl(fd, F_GETFL);
-  if ((fd_flag == -1) || ((fd_flag & O_PATH) == 0)) {
+  if (fd_flag == -1 || (fd_flag & O_PATH) == 0) {
     errno = EBADF;
     return -1;
   }
 
-  char buf[40];
-  snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
   errno = saved_errno;
-  return getxattr(buf, name, value, size);
+  return getxattr(FdPath(fd).c_str(), name, value, size);
 }
diff --git a/libc/bionic/flistxattr.cpp b/libc/bionic/flistxattr.cpp
index 05a96d2..8ad9b85 100644
--- a/libc/bionic/flistxattr.cpp
+++ b/libc/bionic/flistxattr.cpp
@@ -33,13 +33,14 @@
 #include <fcntl.h>
 #include <stdio.h>
 
+#include "private/FdPath.h"
+
 extern "C" ssize_t ___flistxattr(int, char*, size_t);
 
 ssize_t flistxattr(int fd, char *list, size_t size) {
   int saved_errno = errno;
   ssize_t result = ___flistxattr(fd, list, size);
-
-  if ((result != -1) || (errno != EBADF)) {
+  if (result != -1 || errno != EBADF) {
     return result;
   }
 
@@ -47,13 +48,11 @@
   // may not directly support fgetxattr() on such a file descriptor.
   // Use /proc/self/fd instead to emulate this support.
   int fd_flag = fcntl(fd, F_GETFL);
-  if ((fd_flag == -1) || ((fd_flag & O_PATH) == 0)) {
+  if (fd_flag == -1 || (fd_flag & O_PATH) == 0) {
     errno = EBADF;
     return -1;
   }
 
-  char buf[40];
-  snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
   errno = saved_errno;
-  return listxattr(buf, list, size);
+  return listxattr(FdPath(fd).c_str(), list, size);
 }
diff --git a/libc/bionic/fpclassify.cpp b/libc/bionic/fpclassify.cpp
index 42ed3ef..7aa53f3 100644
--- a/libc/bionic/fpclassify.cpp
+++ b/libc/bionic/fpclassify.cpp
@@ -26,145 +26,80 @@
  * SUCH DAMAGE.
  */
 
-#include <sys/types.h>
-
 #include <math.h>
 
-#include "private/bionic_ieee.h"
+// Legacy cruft from before we had builtin implementations of the standard macros.
+// No longer declared in our <math.h>.
 
-// These aren't declared in our <math.h>.
-extern "C" int __isinf(double);
-extern "C" int __isnan(double);
-
-union float_u {
-  float f;
-  ieee_single bits;
-};
-
-union double_u {
-  double d;
-  ieee_double bits;
-};
-
-int __fpclassifyd(double d) {
-  double_u u;
-  u.d = d;
-  if (u.bits.dbl_exp == 0) {
-    return ((u.bits.dbl_fracl | u.bits.dbl_frach) == 0) ? FP_ZERO : FP_SUBNORMAL;
-  }
-  if (u.bits.dbl_exp == DBL_EXP_INFNAN) {
-    return ((u.bits.dbl_fracl | u.bits.dbl_frach) == 0) ? FP_INFINITE : FP_NAN;
-  }
-  return FP_NORMAL;
+extern "C" int __fpclassifyd(double d) {
+  return fpclassify(d);
 }
 __strong_alias(__fpclassify, __fpclassifyd); // glibc uses __fpclassify, BSD __fpclassifyd.
 
-int __fpclassifyf(float f) {
-  float_u u;
-  u.f = f;
-  if (u.bits.sng_exp == 0) {
-    return (u.bits.sng_frac == 0) ? FP_ZERO : FP_SUBNORMAL;
-  }
-  if (u.bits.sng_exp == SNG_EXP_INFNAN) {
-    return (u.bits.sng_frac == 0) ? FP_INFINITE : FP_NAN;
-  }
-  return FP_NORMAL;
+extern "C" int __fpclassifyf(float f) {
+  return fpclassify(f);
 }
 
-int __isinf(double d) {
-  return (__fpclassifyd(d) == FP_INFINITE);
+extern "C" int __isinf(double d) {
+  return isinf(d);
 }
 __strong_alias(isinf, __isinf);
 
-int __isinff(float f) {
-  return (__fpclassifyf(f) == FP_INFINITE);
+extern "C" int __isinff(float f) {
+  return isinf(f);
 }
 __strong_alias(isinff, __isinff);
 
-int __isnan(double d) {
-  return (__fpclassifyd(d) == FP_NAN);
+extern "C" int __isnan(double d) {
+  return isnan(d);
 }
 __strong_alias(isnan, __isnan);
 
-int __isnanf(float f) {
-  return (__fpclassifyf(f) == FP_NAN);
+extern "C" int __isnanf(float f) {
+  return isnan(f);
 }
 __strong_alias(isnanf, __isnanf);
 
-int __isfinite(double d) {
-  int type = __fpclassifyd(d);
-  return ((type != FP_NAN) && (type != FP_INFINITE));
+extern "C" int __isfinite(double d) {
+  return isfinite(d);
 }
 __strong_alias(isfinite, __isfinite);
 
-int __isfinitef(float f) {
-  int type = __fpclassifyf(f);
-  return ((type != FP_NAN) && (type != FP_INFINITE));
+extern "C" int __isfinitef(float f) {
+  return isfinite(f);
 }
 __strong_alias(isfinitef, __isfinitef);
 
-int __isnormal(double d) {
-  return (__fpclassifyd(d) == FP_NORMAL);
+extern "C" int __isnormal(double d) {
+  return isnormal(d);
 }
 __strong_alias(isnormal, __isnormal);
 
-int __isnormalf(float f) {
-  return (__fpclassifyf(f) == FP_NORMAL);
+extern "C" int __isnormalf(float f) {
+  return isnormal(f);
 }
 __strong_alias(isnormalf, __isnormalf);
 
-#if defined(__LP64__)
-
-// LP64 uses 128-bit long doubles.
-
-union long_double_u {
-  long double ld;
-  ieee_ext bits;
-};
-
-#define zero_frac(b) ((b.ext_fracl | b.ext_fraclm | b.ext_frachm | b.ext_frach) == 0)
-
-int __fpclassifyl(long double ld) {
-  long_double_u u;
-  u.ld = ld;
-  if (u.bits.ext_exp == 0) {
-    return zero_frac(u.bits) ? FP_ZERO : FP_SUBNORMAL;
-  }
-  if (u.bits.ext_exp == EXT_EXP_INFNAN) {
-    return zero_frac(u.bits) ? FP_INFINITE : FP_NAN;
-  }
-  return FP_NORMAL;
+extern "C" int __fpclassifyl(long double ld) {
+  return fpclassify(ld);
 }
 
-int __isinfl(long double ld) {
-  return (__fpclassifyl(ld) == FP_INFINITE);
+extern "C" int __isinfl(long double ld) {
+  return isinf(ld);
 }
 
-int __isnanl(long double ld) {
-  return (__fpclassifyl(ld) == FP_NAN);
+extern "C" int __isnanl(long double ld) {
+  return isnan(ld);
 }
 
-int __isfinitel(long double ld) {
-  int type = __fpclassifyl(ld);
-  return ((type != FP_NAN) && (type != FP_INFINITE));
+extern "C" int __isfinitel(long double ld) {
+  return isfinite(ld);
 }
 
-int __isnormall(long double ld) {
-  return (__fpclassifyl(ld) == FP_NORMAL);
+extern "C" int __isnormall(long double ld) {
+  return isnormal(ld);
 }
 
-#else
-
-// LP32 uses double as long double.
-
-__strong_alias(__fpclassifyl, __fpclassify);
-__strong_alias(__isinfl, __isinf);
-__strong_alias(__isnanl, __isnan);
-__strong_alias(__isfinitel, __isfinite);
-__strong_alias(__isnormall, __isnormal);
-
-#endif
-
 __strong_alias(isinfl, __isinfl);
 __strong_alias(isnanl, __isnanl);
 __strong_alias(isfinitel, __isfinitel);
diff --git a/libc/bionic/fsetxattr.cpp b/libc/bionic/fsetxattr.cpp
index 6d2e868..9ad0c76 100644
--- a/libc/bionic/fsetxattr.cpp
+++ b/libc/bionic/fsetxattr.cpp
@@ -33,13 +33,14 @@
 #include <fcntl.h>
 #include <stdio.h>
 
+#include "private/FdPath.h"
+
 extern "C" int ___fsetxattr(int, const char*, const void*, size_t, int);
 
 int fsetxattr(int fd, const char* name, const void* value, size_t size, int flags) {
   int saved_errno = errno;
   int result = ___fsetxattr(fd, name, value, size, flags);
-
-  if ((result == 0) || (errno != EBADF)) {
+  if (result == 0 || errno != EBADF) {
     return result;
   }
 
@@ -47,13 +48,11 @@
   // may not directly support fsetxattr() on such a file descriptor.
   // Use /proc/self/fd instead to emulate this support.
   int fd_flag = fcntl(fd, F_GETFL);
-  if ((fd_flag == -1) || ((fd_flag & O_PATH) == 0)) {
+  if (fd_flag == -1 || (fd_flag & O_PATH) == 0) {
     errno = EBADF;
     return -1;
   }
 
-  char buf[40];
-  snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
   errno = saved_errno;
-  return setxattr(buf, name, value, size, flags);
+  return setxattr(FdPath(fd).c_str(), name, value, size, flags);
 }
diff --git a/libc/bionic/libc_init_dynamic.cpp b/libc/bionic/libc_init_dynamic.cpp
index 349d488..0b68280 100644
--- a/libc/bionic/libc_init_dynamic.cpp
+++ b/libc/bionic/libc_init_dynamic.cpp
@@ -77,11 +77,14 @@
   netdClientInit();
 }
 
-// We flag the __libc_preinit function as a constructor to ensure
-// that its address is listed in libc.so's .init_array section.
-// This ensures that the function is called by the dynamic linker
-// as soon as the shared library is loaded.
-__attribute__((constructor)) static void __libc_preinit() {
+// We flag the __libc_preinit function as a constructor to ensure that
+// its address is listed in libc.so's .init_array section.
+// This ensures that the function is called by the dynamic linker as
+// soon as the shared library is loaded.
+// We give this constructor priority 1 because we want libc's constructor
+// to run before any others (such as the jemalloc constructor), and lower
+// is better (http://b/68046352).
+__attribute__((constructor(1))) static void __libc_preinit() {
   // Read the kernel argument block pointer from TLS.
   void** tls = __get_tls();
   KernelArgumentBlock** args_slot = &reinterpret_cast<KernelArgumentBlock**>(tls)[TLS_SLOT_BIONIC_PREINIT];
diff --git a/libc/bionic/pty.cpp b/libc/bionic/pty.cpp
index bdabf36..599cbd2 100644
--- a/libc/bionic/pty.cpp
+++ b/libc/bionic/pty.cpp
@@ -37,6 +37,7 @@
 #include <utmp.h>
 
 #include "bionic/pthread_internal.h"
+#include "private/FdPath.h"
 
 int getpt() {
   return posix_openpt(O_RDWR|O_NOCTTY);
@@ -94,10 +95,7 @@
     return errno;
   }
 
-  char path[64];
-  snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
-
-  ssize_t count = readlink(path, buf, len);
+  ssize_t count = readlink(FdPath(fd).c_str(), buf, len);
   if (count == -1) {
     return errno;
   }
diff --git a/libc/bionic/swab.cpp b/libc/bionic/swab.cpp
new file mode 100644
index 0000000..bc53ba4
--- /dev/null
+++ b/libc/bionic/swab.cpp
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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
+ * COPYRIGHT OWNER OR CONTRIBUTORS 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.
+ */
+
+#include <unistd.h>
+
+void swab(const void* void_src, void* void_dst, ssize_t byte_count) {
+  const uint8_t* src = static_cast<const uint8_t*>(void_src);
+  uint8_t* dst = static_cast<uint8_t*>(void_dst);
+  while (byte_count > 1) {
+    uint8_t x = *src++;
+    uint8_t y = *src++;
+    *dst++ = y;
+    *dst++ = x;
+    byte_count -= 2;
+  }
+}
diff --git a/libc/include/math.h b/libc/include/math.h
index 6f8b863..57ca260 100644
--- a/libc/include/math.h
+++ b/libc/include/math.h
@@ -352,30 +352,6 @@
 
 #define MAXFLOAT	((float)3.40282346638528860e+38)
 
-/* Legacy cruft from before we had builtin implementations of the standard macros. */
-
-int __fpclassifyd(double __x) __attribute_const__;
-int __fpclassifyf(float __x) __attribute_const__;
-int __fpclassifyl(long double __x) __attribute_const__;
-
-int __isfinitef(float __x) __attribute_const__;
-int __isfinite(double __x) __attribute_const__;
-int __isfinitel(long double __x) __attribute_const__;
-
-int __isinff(float __x) __attribute_const__;
-int __isinfl(long double __x) __attribute_const__;
-
-int __isnanf(float __x) __attribute_const__ __INTRODUCED_IN(21);
-int __isnanl(long double __x) __attribute_const__;
-
-int __isnormal(double __x) __attribute_const__;
-int __isnormalf(float __x) __attribute_const__;
-int __isnormall(long double __x) __attribute_const__;
-
-int __signbit(double __x) __attribute_const__;
-int __signbitf(float __x) __attribute_const__;
-int __signbitl(long double __x) __attribute_const__;
-
 /* BSD extensions. */
 
 #if defined(__USE_BSD)
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 9cfb918..c60cf80 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -98,6 +98,7 @@
 int execlp(const char* __file, const char* __arg0, ...) __attribute__((__sentinel__));
 int execle(const char* __path, const char* __arg0, ... /*,  char* const* __envp */)
     __attribute__((__sentinel__(1)));
+int fexecve(int __fd, char* const* __argv, char* const* __envp) __INTRODUCED_IN_FUTURE;
 
 int nice(int __incr);
 
@@ -246,6 +247,8 @@
 int getdomainname(char* __buf, size_t __buf_size) __INTRODUCED_IN(26);
 int setdomainname(const char* __name, size_t __n) __INTRODUCED_IN(26);
 
+void swab(const void* __src, void* __dst, ssize_t __byte_count) __INTRODUCED_IN_FUTURE;
+
 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
 #include <bits/fortify/unistd.h>
 #endif
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index 981dd59..6e1015a 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1325,6 +1325,7 @@
     endhostent;
     endnetent;
     endprotoent;
+    fexecve;
     getentropy;
     getnetent;
     getprotoent;
@@ -1366,6 +1367,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 29c5235..b88ecd0 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1245,6 +1245,7 @@
     endhostent;
     endnetent;
     endprotoent;
+    fexecve;
     getentropy;
     getnetent;
     getprotoent;
@@ -1286,6 +1287,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index eafbbd7..4fc6535 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1350,6 +1350,7 @@
     endhostent;
     endnetent;
     endprotoent;
+    fexecve;
     getentropy;
     getnetent;
     getprotoent;
@@ -1391,6 +1392,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index a32131f..360af09 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1309,6 +1309,7 @@
     endhostent;
     endnetent;
     endprotoent;
+    fexecve;
     getentropy;
     getnetent;
     getprotoent;
@@ -1350,6 +1351,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 29c5235..b88ecd0 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1245,6 +1245,7 @@
     endhostent;
     endnetent;
     endprotoent;
+    fexecve;
     getentropy;
     getnetent;
     getprotoent;
@@ -1286,6 +1287,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index f1308ea..04ff514 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1307,6 +1307,7 @@
     endhostent;
     endnetent;
     endprotoent;
+    fexecve;
     getentropy;
     getnetent;
     getprotoent;
@@ -1348,6 +1349,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 29c5235..b88ecd0 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1245,6 +1245,7 @@
     endhostent;
     endnetent;
     endprotoent;
+    fexecve;
     getentropy;
     getnetent;
     getprotoent;
@@ -1286,6 +1287,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/private/FdPath.h b/libc/private/FdPath.h
new file mode 100644
index 0000000..4a6a2d5
--- /dev/null
+++ b/libc/private/FdPath.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#pragma once
+
+class FdPath {
+ public:
+  explicit FdPath(int fd) {
+    snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
+  }
+
+  const char* c_str() {
+    return buf;
+  }
+
+ private:
+  char buf[40];
+};
diff --git a/libc/private/bionic_defs.h b/libc/private/bionic_defs.h
new file mode 100644
index 0000000..d9e016c
--- /dev/null
+++ b/libc/private/bionic_defs.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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
+ * COPYRIGHT OWNER OR CONTRIBUTORS 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.
+ */
+
+#ifndef __BIONIC_PRIVATE_BIONIC_DEFS_H_
+#define __BIONIC_PRIVATE_BIONIC_DEFS_H_
+
+/*
+ * This label is used to mark libc/libdl symbols that may need to be replaced
+ * by native bridge implementation.
+ */
+#define __BIONIC_WEAK_FOR_NATIVE_BRIDGE __attribute__((__weak__, __noinline__))
+
+#endif /* __BIONIC_PRIVATE_BIONIC_DEFS_H_ */
diff --git a/libc/stdio/fread.c b/libc/stdio/fread.c
index 7b858ae..b6a3077 100644
--- a/libc/stdio/fread.c
+++ b/libc/stdio/fread.c
@@ -43,6 +43,8 @@
 size_t
 fread(void *buf, size_t size, size_t count, FILE *fp) __overloadable
 {
+	CHECK_FP(fp);
+
 	/*
 	 * Extension:  Catch integer overflow.
 	 */
diff --git a/libc/stdio/local.h b/libc/stdio/local.h
index bf6a8f8..02ea8f8 100644
--- a/libc/stdio/local.h
+++ b/libc/stdio/local.h
@@ -38,6 +38,9 @@
 #include <pthread.h>
 #include <stdbool.h>
 #include <wchar.h>
+
+#include <async_safe/log.h>
+
 #include "wcio.h"
 
 /*
@@ -252,4 +255,13 @@
 
 __END_DECLS
 
+// Sanity check a FILE* for nullptr, so we can emit a message while crashing
+// instead of doing a blind null-dereference.
+#define CHECK_FP(fp)                                                       \
+  do {                                                                     \
+    if (__predict_false(fp == 0)) {                                        \
+      async_safe_fatal("invalid FILE* %p passed to %s", fp, __FUNCTION__); \
+    }                                                                      \
+  } while (0)
+
 #endif
diff --git a/libc/stdio/stdio.cpp b/libc/stdio/stdio.cpp
index 4d6438b..cf97a3f 100644
--- a/libc/stdio/stdio.cpp
+++ b/libc/stdio/stdio.cpp
@@ -44,6 +44,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <async_safe/log.h>
+
 #include "local.h"
 #include "glue.h"
 #include "private/bionic_fortify.h"
@@ -261,6 +263,7 @@
 // all possible, no matter what.
 // TODO: rewrite this mess completely.
 FILE* freopen(const char* file, const char* mode, FILE* fp) {
+  CHECK_FP(fp);
   int mode_flags;
   int flags = __sflags(mode, &mode_flags);
   if (flags == 0) {
@@ -361,6 +364,7 @@
 __strong_alias(freopen64, freopen);
 
 int fclose(FILE* fp) {
+  CHECK_FP(fp);
   if (fp->_flags == 0) {
     // Already freed!
     errno = EBADF;
@@ -387,6 +391,7 @@
 }
 
 int fileno_unlocked(FILE* fp) {
+  CHECK_FP(fp);
   int fd = fp->_file;
   if (fd == -1) {
     errno = EBADF;
@@ -396,6 +401,7 @@
 }
 
 int fileno(FILE* fp) {
+  CHECK_FP(fp);
   ScopedFileLock sfl(fp);
   return fileno_unlocked(fp);
 }
@@ -405,24 +411,29 @@
 }
 
 void clearerr(FILE* fp) {
+  CHECK_FP(fp);
   ScopedFileLock sfl(fp);
   clearerr_unlocked(fp);
 }
 
 int feof_unlocked(FILE* fp) {
+  CHECK_FP(fp);
   return ((fp->_flags & __SEOF) != 0);
 }
 
 int feof(FILE* fp) {
+  CHECK_FP(fp);
   ScopedFileLock sfl(fp);
   return feof_unlocked(fp);
 }
 
 int ferror_unlocked(FILE* fp) {
+  CHECK_FP(fp);
   return __sferror(fp);
 }
 
 int ferror(FILE* fp) {
+  CHECK_FP(fp);
   ScopedFileLock sfl(fp);
   return ferror_unlocked(fp);
 }
@@ -533,24 +544,29 @@
 }
 
 int fseeko(FILE* fp, off_t offset, int whence) {
+  CHECK_FP(fp);
   static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)");
   return __fseeko64(fp, offset, whence, 8*sizeof(off_t));
 }
 __strong_alias(fseek, fseeko);
 
 int fseeko64(FILE* fp, off64_t offset, int whence) {
+  CHECK_FP(fp);
   return __fseeko64(fp, offset, whence, 8*sizeof(off_t));
 }
 
 int fsetpos(FILE* fp, const fpos_t* pos) {
+  CHECK_FP(fp);
   return fseeko(fp, *pos, SEEK_SET);
 }
 
 int fsetpos64(FILE* fp, const fpos64_t* pos) {
+  CHECK_FP(fp);
   return fseeko64(fp, *pos, SEEK_SET);
 }
 
 off_t ftello(FILE* fp) {
+  CHECK_FP(fp);
   static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)");
   off64_t result = ftello64(fp);
   if (result > LONG_MAX) {
@@ -562,16 +578,19 @@
 __strong_alias(ftell, ftello);
 
 off64_t ftello64(FILE* fp) {
+  CHECK_FP(fp);
   ScopedFileLock sfl(fp);
   return __ftello64_unlocked(fp);
 }
 
 int fgetpos(FILE* fp, fpos_t* pos) {
+  CHECK_FP(fp);
   *pos = ftello(fp);
   return (*pos == -1) ? -1 : 0;
 }
 
 int fgetpos64(FILE* fp, fpos64_t* pos) {
+  CHECK_FP(fp);
   *pos = ftello64(fp);
   return (*pos == -1) ? -1 : 0;
 }
@@ -642,35 +661,43 @@
 }
 
 int fprintf(FILE* fp, const char* fmt, ...) {
+  CHECK_FP(fp);
   PRINTF_IMPL(vfprintf(fp, fmt, ap));
 }
 
 int fgetc(FILE* fp) {
+  CHECK_FP(fp);
   return getc(fp);
 }
 
 int fputc(int c, FILE* fp) {
+  CHECK_FP(fp);
   return putc(c, fp);
 }
 
 int fscanf(FILE* fp, const char* fmt, ...) {
+  CHECK_FP(fp);
   PRINTF_IMPL(vfscanf(fp, fmt, ap));
 }
 
 int fwprintf(FILE* fp, const wchar_t* fmt, ...) {
+  CHECK_FP(fp);
   PRINTF_IMPL(vfwprintf(fp, fmt, ap));
 }
 
 int fwscanf(FILE* fp, const wchar_t* fmt, ...) {
+  CHECK_FP(fp);
   PRINTF_IMPL(vfwscanf(fp, fmt, ap));
 }
 
 int getc(FILE* fp) {
+  CHECK_FP(fp);
   ScopedFileLock sfl(fp);
   return getc_unlocked(fp);
 }
 
 int getc_unlocked(FILE* fp) {
+  CHECK_FP(fp);
   return __sgetc(fp);
 }
 
@@ -683,10 +710,12 @@
 }
 
 ssize_t getline(char** buf, size_t* len, FILE* fp) {
+  CHECK_FP(fp);
   return getdelim(buf, len, '\n', fp);
 }
 
 wint_t getwc(FILE* fp) {
+  CHECK_FP(fp);
   return fgetwc(fp);
 }
 
@@ -699,11 +728,13 @@
 }
 
 int putc(int c, FILE* fp) {
+  CHECK_FP(fp);
   ScopedFileLock sfl(fp);
   return putc_unlocked(c, fp);
 }
 
 int putc_unlocked(int c, FILE* fp) {
+  CHECK_FP(fp);
   if (cantwrite(fp)) {
     errno = EBADF;
     return EOF;
@@ -724,6 +755,7 @@
 }
 
 wint_t putwc(wchar_t wc, FILE* fp) {
+  CHECK_FP(fp);
   return fputwc(wc, fp);
 }
 
@@ -738,6 +770,7 @@
 }
 
 void rewind(FILE* fp) {
+  CHECK_FP(fp);
   ScopedFileLock sfl(fp);
   fseek(fp, 0, SEEK_SET);
   clearerr_unlocked(fp);
@@ -748,14 +781,17 @@
 }
 
 void setbuf(FILE* fp, char* buf) {
+  CHECK_FP(fp);
   setbuffer(fp, buf, BUFSIZ);
 }
 
 void setbuffer(FILE* fp, char* buf, int size) {
+  CHECK_FP(fp);
   setvbuf(fp, buf, buf ? _IOFBF : _IONBF, size);
 }
 
 int setlinebuf(FILE* fp) {
+  CHECK_FP(fp);
   return setvbuf(fp, nullptr, _IOLBF, 0);
 }
 
diff --git a/libc/tools/check-symbols-glibc.py b/libc/tools/check-symbols-glibc.py
index 7657925..4de0181 100755
--- a/libc/tools/check-symbols-glibc.py
+++ b/libc/tools/check-symbols-glibc.py
@@ -184,20 +184,24 @@
 ])
 # POSIX has some stuff that's too stupid for words (a64l) or not actually
 # implemented in glibc unless you count always failing with ENOSYS as
-# being implemented (fattach).
+# being implemented (fattach). Other stuff (fmtmsg) isn't used in any
+# codebase I have access to, internal or external.
 in_posix_and_glibc_but_dead_or_useless = set([
   'a64l', # obsolete
   'confstr', # obsolete
   'endutxent', # no utmp on Android
-  'fattach', # obsolete
-  'fdetach', # obsolete
+  'fattach', # <stropts.h> marked obsolescent
+  'fdetach', # <stropts.h> marked obsolescent
+  'fmtmsg', # unused
+  'getdate', # unused
+  'getdate_err', # unused
   'gethostid', # obsolete
-  'getmsg', # obsolete
-  'getpmsg', # obsolete
+  'getmsg', # <stropts.h> marked obsolescent
+  'getpmsg', # <stropts.h> marked obsolescent
   'getutxent', # no utmp on Android
   'getutxid', # no utmp on Android
   'getutxline', # no utmp on Android
-  'isastream', # obsolete
+  'isastream', # <stropts.h> marked obsolescent
   'l64a', # obsolete
   'mq_close', # disallowed by SELinux
   'mq_getattr', # disallowed by SELinux
@@ -211,15 +215,16 @@
   'mq_unlink', # disallowed by SELinux
   'pthread_getconcurrency', # marked obsolescent
   'pthread_setconcurrency', # marked obsolescent
-  'putmsg', # obsolete
-  'putpmsg', # obsolete
+  'putmsg', # <stropts.h> marked obsolescent
+  'putpmsg', # <stropts.h> marked obsolescent
   'pututxline', # no utmp on Android
   'shm_open', # disallowed by SELinux
   'shm_unlink', # disallowed by SELinux
   'setutxent', # no utmp on Android
+  'sockatmark', # obsolete (https://tools.ietf.org/html/rfc6093)
   'strfmon', # icu4c
   'strfmon_l', # icu4c
-  'ulimit', # obsolete
+  'ulimit', # <ulimit.h> marked obsolescent
 ])
 
 posix = posix - in_posix_and_glibc_but_dead_or_useless
diff --git a/libdl/libdl.cpp b/libdl/libdl.cpp
index 97cdeb4..c834088 100644
--- a/libdl/libdl.cpp
+++ b/libdl/libdl.cpp
@@ -17,7 +17,6 @@
 #include <dlfcn.h>
 #include <link.h>
 #include <stdlib.h>
-#include <stdbool.h>
 #include <android/dlext.h>
 
 // These functions are exported by the loader
diff --git a/libm/Android.bp b/libm/Android.bp
index bf86264..da9c9a8 100644
--- a/libm/Android.bp
+++ b/libm/Android.bp
@@ -210,11 +210,9 @@
         "sincos.c",
         "fake_long_double.c",
 
-        // Modified versions of BSD code.
-        "signbit.c",
-
         // Home-grown stuff.
         "fabs.cpp",
+        "signbit.cpp",
     ],
 
     multilib: {
diff --git a/libm/signbit.c b/libm/signbit.c
deleted file mode 100644
index b98bf45..0000000
--- a/libm/signbit.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/*-
- * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
- *
- * $FreeBSD$
- */
-
-#include <math.h>
-
-#include "fpmath.h"
-
-int __signbit(double d)
-{
-  union IEEEd2bits u;
-
-  u.d = d;
-  return (u.bits.sign);
-}
-
-int __signbitf(float f)
-{
-  union IEEEf2bits u;
-
-  u.f = f;
-  return (u.bits.sign);
-}
-
-#ifdef __LP64__
-int __signbitl(long double e)
-{
-  union IEEEl2bits u;
-
-  u.e = e;
-  return (u.bits.sign);
-}
-#else // __LP32__
-__weak_reference(__signbit, __signbitl);
-#endif // __LP64__
diff --git a/libm/signbit.cpp b/libm/signbit.cpp
new file mode 100644
index 0000000..b2a5d60
--- /dev/null
+++ b/libm/signbit.cpp
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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
+ * COPYRIGHT OWNER OR CONTRIBUTORS 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.
+ */
+
+#include <math.h>
+
+// Legacy cruft from before we had builtin implementations of the standard macros.
+// No longer declared in our <math.h>.
+
+extern "C" int __signbit(double d) {
+  return signbit(d);
+}
+
+extern "C" int __signbitf(float f) {
+  return signbit(f);
+}
+
+extern "C" int __signbitl(long double ld) {
+  return signbit(ld);
+}
diff --git a/linker/linker.cpp b/linker/linker.cpp
index f6ca430..ec92c92 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -1621,11 +1621,13 @@
   }
 
   // Step 4-3: Add the new global group members to all the linked namespaces
-  for (auto si : new_global_group_members) {
+  if (namespaces != nullptr) {
     for (auto linked_ns : *namespaces) {
-      if (si->get_primary_namespace() != linked_ns) {
-        linked_ns->add_soinfo(si);
-        si->add_secondary_namespace(linked_ns);
+      for (auto si : new_global_group_members) {
+        if (si->get_primary_namespace() != linked_ns) {
+          linked_ns->add_soinfo(si);
+          si->add_secondary_namespace(linked_ns);
+        }
       }
     }
   }
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 4799739..317f0d2 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -129,9 +129,9 @@
   if (path != nullptr) {
     // We have historically supported ':' as well as ' ' in LD_PRELOAD.
     g_ld_preload_names = android::base::Split(path, " :");
-    std::remove_if(g_ld_preload_names.begin(),
-                   g_ld_preload_names.end(),
-                   [] (const std::string& s) { return s.empty(); });
+    g_ld_preload_names.erase(std::remove_if(g_ld_preload_names.begin(), g_ld_preload_names.end(),
+                                            [](const std::string& s) { return s.empty(); }),
+                             g_ld_preload_names.end());
   }
 }
 
diff --git a/tests/dl_test.cpp b/tests/dl_test.cpp
index 2133a2c..d56b017 100644
--- a/tests/dl_test.cpp
+++ b/tests/dl_test.cpp
@@ -181,12 +181,8 @@
 #endif
 
 #if defined(__BIONIC__)
-static bool is_user_build() {
-  std::string build_type = android::base::GetProperty("ro.build.type", "user");
-  if (build_type == "userdebug" || build_type == "eng") {
-    return false;
-  }
-  return true;
+static bool is_debuggable_build() {
+  return android::base::GetBoolProperty("ro.debuggable", false);
 }
 #endif
 
@@ -194,7 +190,7 @@
 // whose search paths include the 'ns2/' subdir.
 TEST(dl, exec_with_ld_config_file) {
 #if defined(__BIONIC__)
-  if (is_user_build()) {
+  if (!is_debuggable_build()) {
     // LD_CONFIG_FILE is not supported on user build
     return;
   }
@@ -216,7 +212,7 @@
 // additional namespaces other than the default namespace.
 TEST(dl, exec_with_ld_config_file_with_ld_preload) {
 #if defined(__BIONIC__)
-  if (is_user_build()) {
+  if (!is_debuggable_build()) {
     // LD_CONFIG_FILE is not supported on user build
     return;
   }
@@ -244,7 +240,7 @@
     // This test is only for CTS.
     return;
   }
-  if (!is_user_build()) {
+  if (is_debuggable_build()) {
     // Skip the test for non production devices
     return;
   }
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index adc5ee4..697b84a 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -1496,4 +1496,9 @@
   ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
 }
 
+TEST(dlfcn, dlopen_df_1_global) {
+  void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+}
+
 #endif
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
index 858f2b1..ba0b1aa 100644
--- a/tests/libs/Android.bp
+++ b/tests/libs/Android.bp
@@ -377,6 +377,26 @@
 }
 
 // -----------------------------------------------------------------------------
+// Library with DF_1_GLOBAL which will be dlopened
+// (note: libdl_test_df_1_global above will be included in DT_NEEDED)
+// -----------------------------------------------------------------------------
+cc_test_library {
+    name: "libtest_dlopen_df_1_global",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dl_df_1_global_dummy.cpp"],
+    ldflags: ["-Wl,-z,global"],
+
+    target: {
+        host: {
+            // TODO (dimitry): host ld.gold does not yet support -z global
+            // remove this line once it is updated.
+            ldflags: ["-fuse-ld=bfd"],
+        },
+    },
+}
+
+
+// -----------------------------------------------------------------------------
 // Library with weak function
 // -----------------------------------------------------------------------------
 cc_test_library {
diff --git a/tests/libs/dl_df_1_global_dummy.cpp b/tests/libs/dl_df_1_global_dummy.cpp
new file mode 100644
index 0000000..423247c
--- /dev/null
+++ b/tests/libs/dl_df_1_global_dummy.cpp
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+extern "C" int foo() {
+  return 1;
+}
diff --git a/tests/limits_test.cpp b/tests/limits_test.cpp
index ed42dbb..e5902ad 100644
--- a/tests/limits_test.cpp
+++ b/tests/limits_test.cpp
@@ -20,7 +20,7 @@
 
 TEST(limits, macros) {
   ASSERT_EQ(8, CHAR_BIT);
-  ASSERT_EQ(static_cast<int>(sizeof(int)), WORD_BIT);
+  ASSERT_EQ(8 * static_cast<int>(sizeof(int)), WORD_BIT);
   ASSERT_EQ(20, NZERO);
 #if !defined(MB_LEN_MAX)
 #error MB_LEN_MAX
diff --git a/tests/linux_swab_test.cpp b/tests/linux_swab_test.cpp
index 6b964dc..ffd4072 100644
--- a/tests/linux_swab_test.cpp
+++ b/tests/linux_swab_test.cpp
@@ -21,7 +21,7 @@
 // This test makes sure that references to all of the kernel swab
 // macros/inline functions that are exported work properly.
 // Verifies that any kernel header updates do not break these macros.
-TEST(swab, fswa) {
+TEST(linux_swab, smoke) {
   EXPECT_EQ(0x3412U, __swab16(0x1234));
   EXPECT_EQ(0x78563412U, __swab32(0x12345678U));
   EXPECT_EQ(0xbaefcdab78563412ULL, __swab64(0x12345678abcdefbaULL));
diff --git a/tests/math_test.cpp b/tests/math_test.cpp
index 0fc4e50..466e697 100644
--- a/tests/math_test.cpp
+++ b/tests/math_test.cpp
@@ -176,6 +176,11 @@
   ASSERT_NE(0, test_capture_signbit(-1.0L));
 }
 
+// Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __fpclassifyd(double);
+extern "C" int __fpclassifyf(float);
+extern "C" int __fpclassifyl(long double);
+
 TEST(MATH_TEST, __fpclassifyd) {
 #if defined(__GLIBC__)
 #define __fpclassifyd __fpclassify
@@ -208,6 +213,11 @@
   ASSERT_FALSE(finitef(HUGE_VALF));
 }
 
+// Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __isfinite(double);
+extern "C" int __isfinitef(float);
+extern "C" int __isfinitel(long double);
+
 TEST(MATH_TEST, __isfinite) {
 #if defined(__GLIBC__)
 #define __isfinite __finite
@@ -243,6 +253,10 @@
   ASSERT_TRUE((isinf)(HUGE_VAL));
 }
 
+// Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __isinff(float);
+extern "C" int __isinfl(long double);
+
 TEST(MATH_TEST, __isinff) {
   ASSERT_FALSE(__isinff(123.0f));
   ASSERT_TRUE(__isinff(HUGE_VALF));
@@ -259,6 +273,10 @@
   ASSERT_TRUE((isnan)(nan("")));
 }
 
+// Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __isnanf(float);
+extern "C" int __isnanl(long double);
+
 TEST(MATH_TEST, __isnanf) {
   ASSERT_FALSE(__isnanf(123.0f));
   ASSERT_TRUE(__isnanf(nanf("")));
@@ -274,6 +292,11 @@
   ASSERT_TRUE(isnanf(nanf("")));
 }
 
+// Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __isnormal(double);
+extern "C" int __isnormalf(float);
+extern "C" int __isnormall(long double);
+
 TEST(MATH_TEST, __isnormal) {
 #if defined(__BIONIC__)
   ASSERT_TRUE(__isnormal(123.0));
@@ -301,6 +324,11 @@
 #endif // __BIONIC__
 }
 
+// Historical BSD cruft that isn't exposed in <math.h> any more.
+extern "C" int __signbit(double);
+extern "C" int __signbitf(float);
+extern "C" int __signbitl(long double);
+
 TEST(MATH_TEST, __signbit) {
   ASSERT_EQ(0, __signbit(0.0));
   ASSERT_EQ(0, __signbit(1.0));
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 81c6d92..456ddde 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -985,16 +985,11 @@
   VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_ROBUST_PRIO_PROTECT);
 
   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_C_DEV);
-  VERIFY_SYSCONF_UNSUPPORTED(_SC_2_CHAR_TERM);
   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_LOCALEDEF);
   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_SW_DEV);
 
   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_CRYPT);
-  VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_ENH_I18N);
   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_LEGACY);
-  VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_REALTIME);
-  VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_REALTIME_THREADS);
-  VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_SHM);
   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_UUCP);
 #endif // defined(__BIONIC__)
 }
@@ -1402,9 +1397,87 @@
               "<unknown>: usage: run-as");
 }
 
+TEST(UNISTD_TEST, fexecve_failure) {
+  ExecTestHelper eth;
+  errno = 0;
+  int fd = open("/", O_RDONLY);
+  ASSERT_NE(-1, fd);
+  ASSERT_EQ(-1, fexecve(fd, eth.GetArgs(), eth.GetEnv()));
+  ASSERT_EQ(EACCES, errno);
+  close(fd);
+}
+
+TEST(UNISTD_TEST, fexecve_bad_fd) {
+  ExecTestHelper eth;
+  errno = 0;
+  ASSERT_EQ(-1, fexecve(-1, eth.GetArgs(), eth.GetEnv()));
+  ASSERT_EQ(EBADF, errno);
+}
+
+TEST(UNISTD_TEST, fexecve_args) {
+  // Test basic argument passing.
+  int echo_fd = open(BIN_DIR "echo", O_RDONLY | O_CLOEXEC);
+  ASSERT_NE(-1, echo_fd);
+  ExecTestHelper eth;
+  eth.SetArgs({"echo", "hello", "world", nullptr});
+  eth.Run([&]() { fexecve(echo_fd, eth.GetArgs(), eth.GetEnv()); }, 0, "hello world\n");
+  close(echo_fd);
+
+  // Test environment variable setting too.
+  int printenv_fd = open(BIN_DIR "printenv", O_RDONLY | O_CLOEXEC);
+  ASSERT_NE(-1, printenv_fd);
+  eth.SetArgs({"printenv", nullptr});
+  eth.SetEnv({"A=B", nullptr});
+  eth.Run([&]() { fexecve(printenv_fd, eth.GetArgs(), eth.GetEnv()); }, 0, "A=B\n");
+  close(printenv_fd);
+}
+
 TEST(UNISTD_TEST, getlogin_r) {
   char buf[LOGIN_NAME_MAX] = {};
   EXPECT_EQ(ERANGE, getlogin_r(buf, 0));
   EXPECT_EQ(0, getlogin_r(buf, sizeof(buf)));
   EXPECT_STREQ(getlogin(), buf);
 }
+
+TEST(UNISTD_TEST, swab) {
+  // POSIX: "The swab() function shall copy nbytes bytes, which are pointed to by src,
+  // to the object pointed to by dest, exchanging adjacent bytes."
+  char buf[BUFSIZ];
+  memset(buf, 'x', sizeof(buf));
+  swab("ehll oowlr\0d", buf, 12);
+  ASSERT_STREQ("hello world", buf);
+}
+
+TEST(UNISTD_TEST, swab_odd_byte_count) {
+  // POSIX: "If nbytes is odd, swab() copies and exchanges nbytes-1 bytes and the disposition
+  // of the last byte is unspecified."
+  // ...but it seems unreasonable to not just leave the last byte alone.
+  char buf[BUFSIZ];
+  memset(buf, 'x', sizeof(buf));
+  swab("012345", buf, 3);
+  ASSERT_EQ('1', buf[0]);
+  ASSERT_EQ('0', buf[1]);
+  ASSERT_EQ('x', buf[2]);
+}
+
+TEST(UNISTD_TEST, swab_overlap) {
+  // POSIX: "If copying takes place between objects that overlap, the behavior is undefined."
+  // ...but it seems unreasonable to not just do the right thing.
+  char buf[] = "012345";
+  swab(buf, buf, 4);
+  ASSERT_EQ('1', buf[0]);
+  ASSERT_EQ('0', buf[1]);
+  ASSERT_EQ('3', buf[2]);
+  ASSERT_EQ('2', buf[3]);
+  ASSERT_EQ('4', buf[4]);
+  ASSERT_EQ('5', buf[5]);
+  ASSERT_EQ(0, buf[6]);
+}
+
+TEST(UNISTD_TEST, swab_negative_byte_count) {
+  // POSIX: "If nbytes is negative, swab() does nothing."
+  char buf[BUFSIZ];
+  memset(buf, 'x', sizeof(buf));
+  swab("hello", buf, -1);
+  ASSERT_EQ('x', buf[0]);
+}