Merge "More kernel header cleanup."
diff --git a/libc/bionic/__libc_init_main_thread.cpp b/libc/bionic/__libc_init_main_thread.cpp
index 01bb9bb..2643eee 100644
--- a/libc/bionic/__libc_init_main_thread.cpp
+++ b/libc/bionic/__libc_init_main_thread.cpp
@@ -28,14 +28,24 @@
 
 #include "libc_init_common.h"
 
+#include "private/KernelArgumentBlock.h"
 #include "private/bionic_auxv.h"
 #include "private/bionic_globals.h"
-#include "private/KernelArgumentBlock.h"
+#include "private/bionic_ssp.h"
 #include "pthread_internal.h"
 
 extern "C" int __set_tls(void* ptr);
 extern "C" int __set_tid_address(int* tid_address);
 
+// Declared in "private/bionic_ssp.h".
+uintptr_t __stack_chk_guard = 0;
+
+void __libc_init_global_stack_chk_guard(KernelArgumentBlock& args) {
+  // AT_RANDOM is a pointer to 16 bytes of randomness on the stack.
+  // Take the first 4/8 for the -fstack-protector implementation.
+  __stack_chk_guard = *reinterpret_cast<uintptr_t*>(args.getauxval(AT_RANDOM));
+}
+
 // Setup for the main thread. For dynamic executables, this is called by the
 // linker _before_ libc is mapped in memory. This means that all writes to
 // globals from this function will apply to linker-private copies and will not
@@ -78,7 +88,8 @@
   // TODO: the main thread's sched_policy and sched_priority need to be queried.
 
   // The TLS stack guard is set from the global, so ensure that we've initialized the global
-  // before we initialize the TLS.
+  // before we initialize the TLS. Dynamic executables will initialize their copy of the global
+  // stack protector from the one in the main thread's TLS.
   __libc_init_global_stack_chk_guard(args);
 
   __init_thread(&main_thread);
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index 2070c9c..919080b 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -45,7 +45,6 @@
 #include "private/WriteProtected.h"
 #include "private/bionic_auxv.h"
 #include "private/bionic_globals.h"
-#include "private/bionic_ssp.h"
 #include "private/bionic_tls.h"
 #include "private/libc_logging.h"
 #include "private/thread_private.h"
@@ -62,15 +61,6 @@
 // Declared in <unistd.h>.
 char** environ;
 
-// Declared in "private/bionic_ssp.h".
-uintptr_t __stack_chk_guard = 0;
-
-void __libc_init_global_stack_chk_guard(KernelArgumentBlock& args) {
-  // AT_RANDOM is a pointer to 16 bytes of randomness on the stack.
-  // Take the first 4/8 for the -fstack-protector implementation.
-  __stack_chk_guard = *reinterpret_cast<uintptr_t*>(args.getauxval(AT_RANDOM));
-}
-
 #if defined(__i386__)
 __LIBC_HIDDEN__ void* __libc_sysinfo = nullptr;
 
@@ -91,7 +81,6 @@
   // Initialize libc globals that are needed in both the linker and in libc.
   // In dynamic binaries, this is run at least twice for different copies of the
   // globals, once for the linker's copy and once for the one in libc.so.
-  __libc_init_global_stack_chk_guard(args);
   __libc_auxv = args.auxv;
   __libc_globals.initialize();
   __libc_globals.mutate([&args](libc_globals* globals) {
diff --git a/libc/bionic/libc_init_dynamic.cpp b/libc/bionic/libc_init_dynamic.cpp
index ab48fb8..43bca30 100644
--- a/libc/bionic/libc_init_dynamic.cpp
+++ b/libc/bionic/libc_init_dynamic.cpp
@@ -52,6 +52,7 @@
 #include "libc_init_common.h"
 
 #include "private/bionic_globals.h"
+#include "private/bionic_ssp.h"
 #include "private/bionic_tls.h"
 #include "private/KernelArgumentBlock.h"
 
@@ -74,6 +75,10 @@
   // __libc_init_common() will change the TLS area so the old one won't be accessible anyway.
   *args_slot = NULL;
 
+  // The linker has initialized its copy of the global stack_chk_guard, and filled in the main
+  // thread's TLS slot with that value. Initialize the local global stack guard with its value.
+  __stack_chk_guard = reinterpret_cast<uintptr_t>(tls[TLS_SLOT_STACK_GUARD]);
+
   __libc_init_globals(*args);
   __libc_init_common(*args);
 
diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp
index d1494d7..5e06d39 100644
--- a/libc/bionic/libc_init_static.cpp
+++ b/libc/bionic/libc_init_static.cpp
@@ -39,6 +39,7 @@
 #include "libc_init_common.h"
 #include "pthread_internal.h"
 
+#include "private/bionic_globals.h"
 #include "private/bionic_page.h"
 #include "private/bionic_tls.h"
 #include "private/KernelArgumentBlock.h"
@@ -85,6 +86,7 @@
   __libc_init_main_thread(args);
 
   // Initializing the globals requires TLS to be available for errno.
+  __init_thread_stack_guard(__get_thread());
   __libc_init_globals(args);
 
   __libc_init_AT_SECURE(args);
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index 08d9bed..1956a8f 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -56,6 +56,10 @@
   // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0.
   thread->tls[TLS_SLOT_SELF] = thread->tls;
   thread->tls[TLS_SLOT_THREAD_ID] = thread;
+  __init_thread_stack_guard(thread);
+}
+
+void __init_thread_stack_guard(pthread_internal_t* thread) {
   // GCC looks in the TLS for the stack guard on x86, so copy it there from our global.
   thread->tls[TLS_SLOT_STACK_GUARD] = reinterpret_cast<void*>(__stack_chk_guard);
 }
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index 1d9e03e..d2abea0 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -114,6 +114,7 @@
 
 __LIBC_HIDDEN__ int __init_thread(pthread_internal_t* thread);
 __LIBC_HIDDEN__ void __init_tls(pthread_internal_t* thread);
+__LIBC_HIDDEN__ void __init_thread_stack_guard(pthread_internal_t* thread);
 __LIBC_HIDDEN__ void __init_alternate_signal_stack(pthread_internal_t*);
 
 __LIBC_HIDDEN__ pthread_t           __pthread_internal_add(pthread_internal_t* thread);
@@ -123,7 +124,13 @@
 
 // Make __get_thread() inlined for performance reason. See http://b/19825434.
 static inline __always_inline pthread_internal_t* __get_thread() {
-  return reinterpret_cast<pthread_internal_t*>(__get_tls()[TLS_SLOT_THREAD_ID]);
+  void** tls = __get_tls();
+  if (__predict_true(tls)) {
+    return reinterpret_cast<pthread_internal_t*>(tls[TLS_SLOT_THREAD_ID]);
+  }
+
+  // This happens when called during libc initialization before TLS has been initialized.
+  return nullptr;
 }
 
 __LIBC_HIDDEN__ void pthread_key_clean_all(void);
diff --git a/libc/dns/include/resolv_private.h b/libc/dns/include/resolv_private.h
index 8dad926..54b9626 100644
--- a/libc/dns/include/resolv_private.h
+++ b/libc/dns/include/resolv_private.h
@@ -63,9 +63,6 @@
 #include <net/if.h>
 #include <time.h>
 
-/* Despite this file's name, it's part of libresolv. On Android, that means it's part of libc :-( */
-#pragma GCC visibility push(default)
-
 // Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
 // RFC 1034 and RFC 1035 is 255 octets.
 #ifdef MAXHOSTNAMELEN
@@ -547,6 +544,4 @@
 
 __END_DECLS
 
-#pragma GCC visibility pop
-
 #endif /* !_RESOLV_PRIVATE_H_ */
diff --git a/libc/include/alloca.h b/libc/include/alloca.h
index 0c50fc3..0508d31 100644
--- a/libc/include/alloca.h
+++ b/libc/include/alloca.h
@@ -25,9 +25,12 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _ALLOCA_H
 #define _ALLOCA_H
 
+#include <sys/cdefs.h>
+
 #define alloca(size)   __builtin_alloca(size)
 
 #endif /* _ALLOCA_H */
diff --git a/libc/include/android/api-level.h b/libc/include/android/api-level.h
index b230cef..001877b 100644
--- a/libc/include/android/api-level.h
+++ b/libc/include/android/api-level.h
@@ -29,6 +29,8 @@
 #ifndef ANDROID_API_LEVEL_H
 #define ANDROID_API_LEVEL_H
 
+#include <sys/cdefs.h>
+
 /*
  * Magic version number for a current development build, which has
  * not yet turned into an official release.
diff --git a/libc/include/android/dlext.h b/libc/include/android/dlext.h
index 37e4cde..ca3bd25 100644
--- a/libc/include/android/dlext.h
+++ b/libc/include/android/dlext.h
@@ -129,7 +129,7 @@
   struct android_namespace_t* library_namespace;
 } android_dlextinfo;
 
-extern void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo)
+void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo)
   __INTRODUCED_IN(21);
 
 __END_DECLS
diff --git a/libc/include/android/legacy_signal_inlines.h b/libc/include/android/legacy_signal_inlines.h
index edc6259..8ba7894 100644
--- a/libc/include/android/legacy_signal_inlines.h
+++ b/libc/include/android/legacy_signal_inlines.h
@@ -37,7 +37,7 @@
 
 __BEGIN_DECLS
 
-extern sighandler_t bsd_signal(int signum, sighandler_t handler) __REMOVED_IN(21);
+sighandler_t bsd_signal(int signum, sighandler_t handler) __REMOVED_IN(21);
 
 #if __ANDROID_API__ < 21
 
diff --git a/libc/include/ar.h b/libc/include/ar.h
index 835290b..413f767 100644
--- a/libc/include/ar.h
+++ b/libc/include/ar.h
@@ -43,6 +43,8 @@
 #ifndef _AR_H_
 #define	_AR_H_
 
+#include <sys/cdefs.h>
+
 /* Pre-4BSD archives had these magic numbers in them. */
 #define	OARMAG1	0177555
 #define	OARMAG2	0177545
diff --git a/libc/include/arpa/inet.h b/libc/include/arpa/inet.h
index e4f94ef..d137130 100644
--- a/libc/include/arpa/inet.h
+++ b/libc/include/arpa/inet.h
@@ -29,9 +29,10 @@
 #ifndef _ARPA_INET_H_
 #define _ARPA_INET_H_
 
-#include <stdint.h>
-#include <sys/types.h>
 #include <netinet/in.h>
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
 
 __BEGIN_DECLS
 
diff --git a/libc/include/arpa/nameser_compat.h b/libc/include/arpa/nameser_compat.h
index 539864e..6868876 100644
--- a/libc/include/arpa/nameser_compat.h
+++ b/libc/include/arpa/nameser_compat.h
@@ -40,9 +40,10 @@
 #ifndef _ARPA_NAMESER_COMPAT_
 #define	_ARPA_NAMESER_COMPAT_
 
-#define	__BIND		19950621	/* (DEAD) interface version stamp. */
-
 #include <endian.h>
+#include <sys/cdefs.h>
+
+#define	__BIND		19950621	/* (DEAD) interface version stamp. */
 
 /*
  * Structure for query header.  The order of the fields is machine- and
diff --git a/libc/include/arpa/telnet.h b/libc/include/arpa/telnet.h
index b8212f8..594dc7a 100644
--- a/libc/include/arpa/telnet.h
+++ b/libc/include/arpa/telnet.h
@@ -32,6 +32,8 @@
 #ifndef _ARPA_TELNET_H
 #define	_ARPA_TELNET_H 1
 
+#include <sys/cdefs.h>
+
 /*
  * Definitions for the TELNET protocol.
  */
diff --git a/libc/include/assert.h b/libc/include/assert.h
index dea8382..8c456ef 100644
--- a/libc/include/assert.h
+++ b/libc/include/assert.h
@@ -1,6 +1,3 @@
-/*	$OpenBSD: assert.h,v 1.12 2006/01/31 10:53:51 hshoexer Exp $	*/
-/*	$NetBSD: assert.h,v 1.6 1994/10/26 00:55:44 cgd Exp $	*/
-
 /*-
  * Copyright (c) 1992, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -33,34 +30,33 @@
  * 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.
- *
- *	@(#)assert.h	8.2 (Berkeley) 1/21/94
  */
 
 /*
- * Unlike other ANSI header files, <assert.h> may usefully be included
- * multiple times, with and without NDEBUG defined.
+ * There's no include guard in this file because <assert.h> may usefully be
+ * included multiple times, with and without NDEBUG defined.
  */
 
 #include <sys/cdefs.h>
 
 #undef assert
-#undef _assert
+#undef __assert_no_op
+
+#define __assert_no_op __BIONIC_CAST(static_cast, void, 0)
 
 #ifdef NDEBUG
-# define	assert(e)	((void)0)
-# define	_assert(e)	((void)0)
+# define assert(e) __assert_no_op
 #else
-# define	_assert(e)	assert(e)
-# if __STDC_VERSION__ >= 199901L
-#  define	assert(e)	((e) ? (void)0 : __assert2(__FILE__, __LINE__, __func__, #e))
+# if defined(__cplusplus) || __STDC_VERSION__ >= 199901L
+#  define assert(e) ((e) ? __assert_no_op : __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, #e))
 # else
-#  define	assert(e)	((e) ? (void)0 : __assert(__FILE__, __LINE__, #e))
+#  define assert(e) ((e) ? __assert_no_op : __assert(__FILE__, __LINE__, #e))
 # endif
 #endif
 
 #if !defined(__cplusplus) && __STDC_VERSION__ >= 201112L
-#define static_assert _Static_assert
+# undef static_assert
+# define static_assert _Static_assert
 #endif
 
 __BEGIN_DECLS
diff --git a/libc/include/bits/fcntl.h b/libc/include/bits/fcntl.h
index 90f933c..604d35a 100644
--- a/libc/include/bits/fcntl.h
+++ b/libc/include/bits/fcntl.h
@@ -33,7 +33,7 @@
 
 __BEGIN_DECLS
 
-extern int fcntl(int, int, ...);
+int fcntl(int, int, ...);
 
 __END_DECLS
 
diff --git a/libc/include/bits/ioctl.h b/libc/include/bits/ioctl.h
index 788a4f7..53116ca 100644
--- a/libc/include/bits/ioctl.h
+++ b/libc/include/bits/ioctl.h
@@ -33,7 +33,7 @@
 
 __BEGIN_DECLS
 
-extern int ioctl(int, int, ...);
+int ioctl(int, int, ...);
 
 __END_DECLS
 
diff --git a/libc/include/bits/posix_limits.h b/libc/include/bits/posix_limits.h
index 31016a8..0f17381 100644
--- a/libc/include/bits/posix_limits.h
+++ b/libc/include/bits/posix_limits.h
@@ -29,6 +29,7 @@
 #ifndef _BITS_POSIX_LIMITS_H_
 #define _BITS_POSIX_LIMITS_H_
 
+#include <sys/cdefs.h>
 
 /* Any constant values here other than -1 or 200809L are explicitly specified by POSIX.1-2008. */
 /* Keep it sorted. */
diff --git a/libc/include/bits/pthread_types.h b/libc/include/bits/pthread_types.h
index 6ac1c68..194a49b 100644
--- a/libc/include/bits/pthread_types.h
+++ b/libc/include/bits/pthread_types.h
@@ -29,6 +29,7 @@
 #ifndef _BITS_PTHREAD_TYPES_H_
 #define _BITS_PTHREAD_TYPES_H_
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 typedef long pthread_t;
diff --git a/libc/include/bits/sa_family_t.h b/libc/include/bits/sa_family_t.h
index 51ea51f..98ca27f 100644
--- a/libc/include/bits/sa_family_t.h
+++ b/libc/include/bits/sa_family_t.h
@@ -29,6 +29,8 @@
 #ifndef _BITS_SA_FAMILY_T_H_
 #define _BITS_SA_FAMILY_T_H_
 
+#include <sys/cdefs.h>
+
 typedef unsigned short sa_family_t;
 
 #endif
diff --git a/libc/include/bits/timespec.h b/libc/include/bits/timespec.h
index 046d898..df7a7ce 100644
--- a/libc/include/bits/timespec.h
+++ b/libc/include/bits/timespec.h
@@ -29,6 +29,7 @@
 #ifndef _BITS_TIMESPEC_H_
 #define _BITS_TIMESPEC_H_
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 /*
diff --git a/libc/include/bits/wchar_limits.h b/libc/include/bits/wchar_limits.h
index f779c1a..ffad604 100644
--- a/libc/include/bits/wchar_limits.h
+++ b/libc/include/bits/wchar_limits.h
@@ -29,6 +29,8 @@
 #ifndef _BITS_WCHAR_LIMITS_H_
 #define _BITS_WCHAR_LIMITS_H_
 
+#include <sys/cdefs.h>
+
 /* Both GCC and clang define __WCHAR_MAX__. */
 #define WCHAR_MAX __WCHAR_MAX__
 
diff --git a/libc/include/byteswap.h b/libc/include/byteswap.h
index 628fb7f..0838e6c 100644
--- a/libc/include/byteswap.h
+++ b/libc/include/byteswap.h
@@ -25,9 +25,11 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _BYTESWAP_H_
 #define _BYTESWAP_H_
 
+#include <sys/cdefs.h>
 #include <sys/endian.h>
 
 #define bswap_16(x) __swap16(x)
diff --git a/libc/include/complex.h b/libc/include/complex.h
index e874e1c..f4dc124 100644
--- a/libc/include/complex.h
+++ b/libc/include/complex.h
@@ -56,7 +56,6 @@
 #endif
 
 __BEGIN_DECLS
-#pragma GCC visibility push(default)
 
 /* 7.3.5 Trigonometric functions */
 /* 7.3.5.1 The cacos functions */
@@ -156,7 +155,6 @@
 float crealf(float complex) __INTRODUCED_IN(23);
 long double creall(long double complex) __INTRODUCED_IN(23);
 
-#pragma GCC visibility pop
 __END_DECLS
 
 #endif /* _COMPLEX_H */
diff --git a/libc/include/dirent.h b/libc/include/dirent.h
index db8a520..3f9ad18 100644
--- a/libc/include/dirent.h
+++ b/libc/include/dirent.h
@@ -68,22 +68,22 @@
 
 typedef struct DIR DIR;
 
-extern DIR* opendir(const char*);
-extern DIR* fdopendir(int);
-extern struct dirent* readdir(DIR*);
-extern struct dirent64* readdir64(DIR*) __INTRODUCED_IN(21);
-extern int readdir_r(DIR*, struct dirent*, struct dirent**);
-extern int readdir64_r(DIR*, struct dirent64*, struct dirent64**) __INTRODUCED_IN(21);
-extern int closedir(DIR*);
-extern void rewinddir(DIR*);
-extern void seekdir(DIR*, long) __INTRODUCED_IN(23);
-extern long telldir(DIR*) __INTRODUCED_IN(23);
-extern int dirfd(DIR*);
-extern int alphasort(const struct dirent**, const struct dirent**);
-extern int alphasort64(const struct dirent64**, const struct dirent64**) __INTRODUCED_IN(21);
-extern int scandir64(const char*, struct dirent64***, int (*)(const struct dirent64*),
-                     int (*)(const struct dirent64**, const struct dirent64**)) __INTRODUCED_IN(21);
-extern int scandir(const char*, struct dirent***, int (*)(const struct dirent*), int (*)(const struct dirent**, const struct dirent**));
+DIR* opendir(const char*);
+DIR* fdopendir(int);
+struct dirent* readdir(DIR*);
+struct dirent64* readdir64(DIR*) __INTRODUCED_IN(21);
+int readdir_r(DIR*, struct dirent*, struct dirent**);
+int readdir64_r(DIR*, struct dirent64*, struct dirent64**) __INTRODUCED_IN(21);
+int closedir(DIR*);
+void rewinddir(DIR*);
+void seekdir(DIR*, long) __INTRODUCED_IN(23);
+long telldir(DIR*) __INTRODUCED_IN(23);
+int dirfd(DIR*);
+int alphasort(const struct dirent**, const struct dirent**);
+int alphasort64(const struct dirent64**, const struct dirent64**) __INTRODUCED_IN(21);
+int scandir64(const char*, struct dirent64***, int (*)(const struct dirent64*),
+              int (*)(const struct dirent64**, const struct dirent64**)) __INTRODUCED_IN(21);
+int scandir(const char*, struct dirent***, int (*)(const struct dirent*), int (*)(const struct dirent**, const struct dirent**));
 
 #if defined(__USE_GNU)
 int scandirat64(int, const char*, struct dirent64***, int (*)(const struct dirent64*),
diff --git a/libc/include/dlfcn.h b/libc/include/dlfcn.h
index 5dc8350..a53f664 100644
--- a/libc/include/dlfcn.h
+++ b/libc/include/dlfcn.h
@@ -25,6 +25,7 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef __DLFCN_H__
 #define __DLFCN_H__
 
@@ -32,24 +33,28 @@
 
 __BEGIN_DECLS
 
+#if defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnullability-completeness"
+#endif
+
 typedef struct {
-    const char *dli_fname;  /* Pathname of shared object that
-                               contains address */
-    void       *dli_fbase;  /* Address at which shared object
-                               is loaded */
-    const char *dli_sname;  /* Name of nearest symbol with address
-                               lower than addr */
-    void       *dli_saddr;  /* Exact address of symbol named
-                               in dli_sname */
+  /* Pathname of shared object that contains address. */
+  const char* dli_fname;
+  /* Address at which shared object is loaded. */
+  void* dli_fbase;
+  /* Name of nearest symbol with address lower than addr. */
+  const char* dli_sname;
+  /* Exact address of symbol named in dli_sname. */
+  void* dli_saddr;
 } Dl_info;
 
-extern void* dlopen(const char*  filename, int flag);
-extern int dlclose(void*  handle);
-extern const char* dlerror(void);
-extern void* dlsym(void* handle, const char* _Nonnull symbol);
-extern void* dlvsym(void* handle, const char* _Nonnull symbol, const char* _Nonnull version)
-  __INTRODUCED_IN(24);
-extern int dladdr(const void* addr, Dl_info *info);
+void* dlopen(const char* filename, int flag);
+int dlclose(void* _Nonnull handle);
+const char* dlerror(void);
+void* dlsym(void* handle, const char* _Nonnull symbol);
+void* dlvsym(void* handle, const char* _Nonnull symbol, const char* _Nonnull version) __INTRODUCED_IN(24);
+int dladdr(const void* addr, Dl_info* _Nonnull info);
 
 enum {
 #if defined(__LP64__)
@@ -70,11 +75,15 @@
 };
 
 #if defined (__LP64__)
-#define RTLD_DEFAULT  ((void*) 0)
-#define RTLD_NEXT     ((void*) -1L)
+#define RTLD_DEFAULT  __BIONIC_CAST(reinterpret_cast, void*, 0)
+#define RTLD_NEXT     __BIONIC_CAST(reinterpret_cast, void*, -1L)
 #else
-#define RTLD_DEFAULT  ((void*) 0xffffffff)
-#define RTLD_NEXT     ((void*) 0xfffffffe)
+#define RTLD_DEFAULT  __BIONIC_CAST(reinterpret_cast, void*, 0xffffffff)
+#define RTLD_NEXT     __BIONIC_CAST(reinterpret_cast, void*, 0xfffffffe)
+#endif
+
+#if defined(__clang__)
+#pragma clang diagnostic pop
 #endif
 
 __END_DECLS
diff --git a/libc/include/elf.h b/libc/include/elf.h
index eaad1d3..1a99337 100644
--- a/libc/include/elf.h
+++ b/libc/include/elf.h
@@ -25,14 +25,15 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _ELF_H
 #define _ELF_H
 
 #include <linux/auxvec.h>
 #include <linux/elf.h>
 #include <linux/elf-em.h>
-
 #include <machine/elf_machdep.h>
+#include <sys/cdefs.h>
 
 #define ELF32_R_INFO(sym, type) ((((Elf32_Word)sym) << 8) | ((type) & 0xff))
 #define ELF64_R_INFO(sym, type) ((((Elf64_Xword)sym) << 32) | ((type) & 0xffffffff))
diff --git a/libc/include/endian.h b/libc/include/endian.h
index 475b48c..65e2930 100644
--- a/libc/include/endian.h
+++ b/libc/include/endian.h
@@ -1,33 +1 @@
-/*
- * Copyright (C) 2008 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 _ENDIAN_H_
-#define _ENDIAN_H_
-
 #include <sys/endian.h>
-
-#endif /* _ENDIAN_H_ */
diff --git a/libc/include/errno.h b/libc/include/errno.h
index 2ff1369..8b2e8ed 100644
--- a/libc/include/errno.h
+++ b/libc/include/errno.h
@@ -41,7 +41,7 @@
 #endif
 
 /* internal function returning the address of the thread-specific errno */
-extern volatile int* __errno(void) __pure2;
+volatile int* __errno(void) __pure2;
 
 /* a macro expanding to the errno l-value */
 #define  errno   (*__errno())
diff --git a/libc/include/error.h b/libc/include/error.h
index 18ce379..05ce35d 100644
--- a/libc/include/error.h
+++ b/libc/include/error.h
@@ -33,14 +33,14 @@
 
 __BEGIN_DECLS
 
-void error(int, int, const char* _Nonnull, ...) __printflike(3, 4) __INTRODUCED_IN(23);
-void error_at_line(int, int, const char*, unsigned int, const char* _Nonnull, ...) __printflike(5, 6)
-  __INTRODUCED_IN(23);
-
-extern void (*error_print_progname)(void) __INTRODUCED_IN(23);
+extern void (* _Nullable error_print_progname)(void) __INTRODUCED_IN(23);
 extern unsigned int error_message_count __INTRODUCED_IN(23);
 extern int error_one_per_line __INTRODUCED_IN(23);
 
+void error(int, int, const char* _Nonnull, ...) __printflike(3, 4) __INTRODUCED_IN(23);
+void error_at_line(int, int, const char* _Nullable, unsigned int, const char* _Nonnull, ...)
+    __printflike(5, 6) __INTRODUCED_IN(23);
+
 __END_DECLS
 
 #endif
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index 4aa2ea0..872a727 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -63,38 +63,38 @@
 #define SYNC_FILE_RANGE_WRITE 2
 #define SYNC_FILE_RANGE_WAIT_AFTER 4
 
-extern int creat(const char*, mode_t);
-extern int creat64(const char*, mode_t) __INTRODUCED_IN(21);
-extern int openat(int, const char*, int, ...);
-extern int openat64(int, const char*, int, ...) __INTRODUCED_IN(21);
-extern int open(const char*, int, ...);
-extern int open64(const char*, int, ...) __INTRODUCED_IN(21);
-extern ssize_t splice(int, off64_t*, int, off64_t*, size_t, unsigned int) __INTRODUCED_IN(21);
-extern ssize_t tee(int, int, size_t, unsigned int) __INTRODUCED_IN(21);
-extern ssize_t vmsplice(int, const struct iovec*, size_t, unsigned int) __INTRODUCED_IN(21);
+int creat(const char*, mode_t);
+int creat64(const char*, mode_t) __INTRODUCED_IN(21);
+int openat(int, const char*, int, ...);
+int openat64(int, const char*, int, ...) __INTRODUCED_IN(21);
+int open(const char*, int, ...);
+int open64(const char*, int, ...) __INTRODUCED_IN(21);
+ssize_t splice(int, off64_t*, int, off64_t*, size_t, unsigned int) __INTRODUCED_IN(21);
+ssize_t tee(int, int, size_t, unsigned int) __INTRODUCED_IN(21);
+ssize_t vmsplice(int, const struct iovec*, size_t, unsigned int) __INTRODUCED_IN(21);
 
 #if defined(__USE_FILE_OFFSET64)
-extern int fallocate(int, int, off_t, off_t) __RENAME(fallocate64) __INTRODUCED_IN(21);
-extern int posix_fadvise(int, off_t, off_t, int) __RENAME(posix_fadvise64) __INTRODUCED_IN(21);
-extern int posix_fallocate(int, off_t, off_t) __RENAME(posix_fallocate) __INTRODUCED_IN(21);
+int fallocate(int, int, off_t, off_t) __RENAME(fallocate64) __INTRODUCED_IN(21);
+int posix_fadvise(int, off_t, off_t, int) __RENAME(posix_fadvise64) __INTRODUCED_IN(21);
+int posix_fallocate(int, off_t, off_t) __RENAME(posix_fallocate) __INTRODUCED_IN(21);
 #else
-extern int fallocate(int, int, off_t, off_t) __INTRODUCED_IN(21);
-extern int posix_fadvise(int, off_t, off_t, int) __INTRODUCED_IN(21);
-extern int posix_fallocate(int, off_t, off_t) __INTRODUCED_IN(21);
+int fallocate(int, int, off_t, off_t) __INTRODUCED_IN(21);
+int posix_fadvise(int, off_t, off_t, int) __INTRODUCED_IN(21);
+int posix_fallocate(int, off_t, off_t) __INTRODUCED_IN(21);
 #endif
-extern int fallocate64(int, int, off64_t, off64_t) __INTRODUCED_IN(21);
-extern int posix_fadvise64(int, off64_t, off64_t, int) __INTRODUCED_IN(21);
-extern int posix_fallocate64(int, off64_t, off64_t) __INTRODUCED_IN(21);
+int fallocate64(int, int, off64_t, off64_t) __INTRODUCED_IN(21);
+int posix_fadvise64(int, off64_t, off64_t, int) __INTRODUCED_IN(21);
+int posix_fallocate64(int, off64_t, off64_t) __INTRODUCED_IN(21);
 
 #if defined(__USE_GNU)
 ssize_t readahead(int, off64_t, size_t) __INTRODUCED_IN(16);
 int sync_file_range(int, off64_t, off64_t, unsigned int) __INTRODUCED_IN_FUTURE;
 #endif
 
-extern int __open_2(const char*, int) __INTRODUCED_IN(17);
-extern int __open_real(const char*, int, ...) __RENAME(open);
-extern int __openat_2(int, const char*, int) __INTRODUCED_IN(17);
-extern int __openat_real(int, const char*, int, ...) __RENAME(openat);
+int __open_2(const char*, int) __INTRODUCED_IN(17);
+int __open_real(const char*, int, ...) __RENAME(open);
+int __openat_2(int, const char*, int) __INTRODUCED_IN(17);
+int __openat_real(int, const char*, int, ...) __RENAME(openat);
 __errordecl(__creat_missing_mode, "called with O_CREAT, but missing mode");
 __errordecl(__creat_too_many_args, "too many arguments");
 
diff --git a/libc/include/fenv.h b/libc/include/fenv.h
index d4f6994..f1b2b25 100644
--- a/libc/include/fenv.h
+++ b/libc/include/fenv.h
@@ -34,7 +34,6 @@
 #include <machine/fenv.h>
 
 __BEGIN_DECLS
-#pragma GCC visibility push(default)
 
 // fenv was always available on x86.
 int feclearexcept(int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
@@ -69,7 +68,6 @@
 extern const fenv_t __fe_dfl_env;
 #define FE_DFL_ENV  (&__fe_dfl_env)
 
-#pragma GCC visibility pop
 __END_DECLS
 
 #endif  /* ! _FENV_H_ */
diff --git a/libc/include/fnmatch.h b/libc/include/fnmatch.h
index 772b4ef..1a5348b 100644
--- a/libc/include/fnmatch.h
+++ b/libc/include/fnmatch.h
@@ -44,9 +44,8 @@
 #define FNM_IGNORECASE   FNM_CASEFOLD
 #define FNM_FILE_NAME    FNM_PATHNAME
 
-extern int  fnmatch(const char *pattern, const char *string, int flags);
+int fnmatch(const char* pattern, const char* string, int flags);
 
 __END_DECLS
 
 #endif /* _FNMATCH_H */
-
diff --git a/libc/include/fts.h b/libc/include/fts.h
index cde0349..b46c19c 100644
--- a/libc/include/fts.h
+++ b/libc/include/fts.h
@@ -35,6 +35,7 @@
 #ifndef	_FTS_H_
 #define	_FTS_H_
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 typedef struct {
diff --git a/libc/include/ftw.h b/libc/include/ftw.h
index ac901cc..cb8ae7d 100644
--- a/libc/include/ftw.h
+++ b/libc/include/ftw.h
@@ -25,8 +25,9 @@
 #ifndef	_FTW_H
 #define	_FTW_H
 
-#include <sys/types.h>
+#include <sys/cdefs.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 
 /*
  * Valid flags for the 3rd argument to the function that is passed as the
diff --git a/libc/include/grp.h b/libc/include/grp.h
index 498d1b8..1d38265 100644
--- a/libc/include/grp.h
+++ b/libc/include/grp.h
@@ -39,19 +39,19 @@
 #include <sys/types.h>
 
 struct group {
-    char* gr_name; /* group name */
-    char* gr_passwd; /* group password */
-    gid_t gr_gid; /* group id */
-    char** gr_mem; /* group members */
+  char* gr_name; /* group name */
+  char* gr_passwd; /* group password */
+  gid_t gr_gid; /* group id */
+  char** gr_mem; /* group members */
 };
 
 __BEGIN_DECLS
+
 struct group* getgrgid(gid_t);
 struct group* getgrnam(const char*);
 
-/* Android has thousands and thousands of ids to iterate through. */
-struct group* getgrent(void)
-  __attribute__((warning("getgrent is inefficient on Android"))) __INTRODUCED_IN_FUTURE;
+/* Note: Android has thousands and thousands of ids to iterate through. */
+struct group* getgrent(void) __INTRODUCED_IN_FUTURE;
 
 void setgrent(void) __INTRODUCED_IN_FUTURE;
 void endgrent(void) __INTRODUCED_IN_FUTURE;
diff --git a/libc/include/libgen.h b/libc/include/libgen.h
index 4d22d15..f864ee1 100644
--- a/libc/include/libgen.h
+++ b/libc/include/libgen.h
@@ -42,17 +42,17 @@
  * Note that this has the wrong argument cv-qualifiers, but doesn't modify its
  * input and uses thread-local storage for the result if necessary.
  */
-extern char* __posix_basename(const char*) __RENAME(basename);
+char* __posix_basename(const char*) __RENAME(basename);
 
 #define basename __posix_basename
 
 /* This has the wrong argument cv-qualifiers, but doesn't modify its input and uses thread-local storage for the result if necessary. */
-extern char* dirname(const char*);
+char* dirname(const char*);
 
 #if !defined(__LP64__)
 /* These non-standard functions are not needed on Android; basename and dirname use thread-local storage. */
-extern int dirname_r(const char*, char*, size_t);
-extern int basename_r(const char*, char*, size_t);
+int dirname_r(const char*, char*, size_t);
+int basename_r(const char*, char*, size_t);
 #endif
 
 __END_DECLS
diff --git a/libc/include/link.h b/libc/include/link.h
index 2d189f6..f40e7e4 100644
--- a/libc/include/link.h
+++ b/libc/include/link.h
@@ -28,8 +28,9 @@
 #ifndef _LINK_H_
 #define _LINK_H_
 
-#include <sys/types.h>
 #include <elf.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
 
 __BEGIN_DECLS
 
diff --git a/libc/include/locale.h b/libc/include/locale.h
index daef18f..a8f03bc 100644
--- a/libc/include/locale.h
+++ b/libc/include/locale.h
@@ -25,6 +25,7 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _LOCALE_H_
 #define _LOCALE_H_
 
@@ -66,30 +67,30 @@
                      LC_IDENTIFICATION_MASK)
 
 struct lconv {
-    char* decimal_point;
-    char* thousands_sep;
-    char* grouping;
-    char* int_curr_symbol;
-    char* currency_symbol;
-    char* mon_decimal_point;
-    char* mon_thousands_sep;
-    char* mon_grouping;
-    char* positive_sign;
-    char* negative_sign;
-    char  int_frac_digits;
-    char  frac_digits;
-    char  p_cs_precedes;
-    char  p_sep_by_space;
-    char  n_cs_precedes;
-    char  n_sep_by_space;
-    char  p_sign_posn;
-    char  n_sign_posn;
-    char  int_p_cs_precedes;
-    char  int_p_sep_by_space;
-    char  int_n_cs_precedes;
-    char  int_n_sep_by_space;
-    char  int_p_sign_posn;
-    char  int_n_sign_posn;
+  char* decimal_point;
+  char* thousands_sep;
+  char* grouping;
+  char* int_curr_symbol;
+  char* currency_symbol;
+  char* mon_decimal_point;
+  char* mon_thousands_sep;
+  char* mon_grouping;
+  char* positive_sign;
+  char* negative_sign;
+  char int_frac_digits;
+  char frac_digits;
+  char p_cs_precedes;
+  char p_sep_by_space;
+  char n_cs_precedes;
+  char n_sep_by_space;
+  char p_sign_posn;
+  char n_sign_posn;
+  char int_p_cs_precedes;
+  char int_p_sep_by_space;
+  char int_n_cs_precedes;
+  char int_n_sep_by_space;
+  char int_p_sign_posn;
+  char int_n_sign_posn;
 };
 
 struct lconv* localeconv(void) __INTRODUCED_IN(21);
@@ -100,7 +101,7 @@
 char* setlocale(int, const char*);
 locale_t uselocale(locale_t) __INTRODUCED_IN(21);
 
-#define LC_GLOBAL_LOCALE ((locale_t) -1L)
+#define LC_GLOBAL_LOCALE __BIONIC_CAST(reinterpret_cast, locale_t, -1L)
 
 __END_DECLS
 
diff --git a/libc/include/malloc.h b/libc/include/malloc.h
index 8e6cef9..07d7896 100644
--- a/libc/include/malloc.h
+++ b/libc/include/malloc.h
@@ -23,13 +23,13 @@
 
 __BEGIN_DECLS
 
-extern void* malloc(size_t byte_count) __mallocfunc __wur __attribute__((alloc_size(1)));
-extern void* calloc(size_t item_count, size_t item_size) __mallocfunc __wur __attribute__((alloc_size(1,2)));
-extern void* realloc(void* p, size_t byte_count) __wur __attribute__((alloc_size(2)));
-extern void free(void* p);
+void* malloc(size_t byte_count) __mallocfunc __wur;
+void* calloc(size_t item_count, size_t item_size) __mallocfunc __wur;
+void* realloc(void* p, size_t byte_count) __wur;
+void free(void* p);
 
-extern void* memalign(size_t alignment, size_t byte_count) __mallocfunc __wur __attribute__((alloc_size(2)));
-extern size_t malloc_usable_size(const void* p) __INTRODUCED_IN(17);
+void* memalign(size_t alignment, size_t byte_count) __mallocfunc __wur;
+size_t malloc_usable_size(const void* p) __INTRODUCED_IN(17);
 
 #ifndef STRUCT_MALLINFO_DECLARED
 #define STRUCT_MALLINFO_DECLARED 1
@@ -47,7 +47,7 @@
 };
 #endif  /* STRUCT_MALLINFO_DECLARED */
 
-extern struct mallinfo mallinfo(void);
+struct mallinfo mallinfo(void);
 
 /*
  * XML structure for malloc_info(3) is in the following format:
@@ -68,7 +68,7 @@
  *   <!-- more heaps -->
  * </malloc>
  */
-extern int malloc_info(int, FILE*) __INTRODUCED_IN(23);
+int malloc_info(int, FILE*) __INTRODUCED_IN(23);
 
 __END_DECLS
 
diff --git a/libc/include/net/route.h b/libc/include/net/route.h
index 326fbe7..9c53875 100644
--- a/libc/include/net/route.h
+++ b/libc/include/net/route.h
@@ -29,6 +29,7 @@
 #ifndef _NET_ROUTE_H_
 #define _NET_ROUTE_H_
 
+#include <sys/cdefs.h>
 #include <sys/socket.h>
 #include <linux/route.h>
 #include <linux/in6.h>
diff --git a/libc/include/netdb.h b/libc/include/netdb.h
index 6bcfdcd..7fcb11f 100644
--- a/libc/include/netdb.h
+++ b/libc/include/netdb.h
@@ -193,7 +193,6 @@
 #define	SCOPE_DELIMITER	'%'
 
 __BEGIN_DECLS
-#pragma GCC visibility push(default)
 
 /* BIONIC-BEGIN */
 #define  h_errno   (*__get_h_errno())
@@ -225,7 +224,6 @@
 const char	*gai_strerror(int);
 void setservent(int);
 
-#pragma GCC visibility pop
 __END_DECLS
 
 #endif /* !_NETDB_H_ */
diff --git a/libc/include/netinet/icmp6.h b/libc/include/netinet/icmp6.h
index e3e6e9a..eca7b45 100644
--- a/libc/include/netinet/icmp6.h
+++ b/libc/include/netinet/icmp6.h
@@ -66,6 +66,7 @@
 #define _NETINET_ICMP6_H_
 
 #include <netinet/in.h> /* android-added: glibc source compatibility. */
+#include <sys/cdefs.h>
 
 #define ICMPV6_PLD_MAXLEN	1232	/* IPV6_MMTU - sizeof(struct ip6_hdr)
 					   - sizeof(struct icmp6_hdr) */
diff --git a/libc/include/netinet/in6.h b/libc/include/netinet/in6.h
index 879ac75..ae20f83 100644
--- a/libc/include/netinet/in6.h
+++ b/libc/include/netinet/in6.h
@@ -29,33 +29,34 @@
 #ifndef _NETINET_IN6_H
 #define _NETINET_IN6_H
 
+#include <sys/cdefs.h>
+
 #include <linux/in6.h>
 
 #define IN6_IS_ADDR_UNSPECIFIED(a) \
-  ((*(const uint32_t*)(&(a)->s6_addr[0]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[4]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[8]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[12]) == 0))
+  ((((a)->s6_addr32[0]) == 0) && \
+   (((a)->s6_addr32[1]) == 0) && \
+   (((a)->s6_addr32[2]) == 0) && \
+   (((a)->s6_addr32[3]) == 0))
 
 #define IN6_IS_ADDR_LOOPBACK(a) \
-  ((*(const uint32_t*)(&(a)->s6_addr[0]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[4]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[8]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[12]) == ntohl(1)))
+  ((((a)->s6_addr32[0]) == 0) && \
+   (((a)->s6_addr32[1]) == 0) && \
+   (((a)->s6_addr32[2]) == 0) && \
+   (((a)->s6_addr32[3]) == ntohl(1)))
 
 #define IN6_IS_ADDR_V4COMPAT(a) \
-  ((*(const uint32_t*)(&(a)->s6_addr[0]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[4]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[8]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[12]) != 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[12]) != ntohl(1)))
+  ((((a)->s6_addr32[0]) == 0) && \
+   (((a)->s6_addr32[1]) == 0) && \
+   (((a)->s6_addr32[2]) == 0) && \
+   (((a)->s6_addr32[3]) != 0) && (((a)->s6_addr32[3]) != ntohl(1)))
 
 #define IN6_IS_ADDR_V4MAPPED(a) \
-  ((*(const uint32_t*)(&(a)->s6_addr[0]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[4]) == 0) && \
-   (*(const uint32_t*)(&(a)->s6_addr[8]) == ntohl(0x0000ffff)))
+  ((((a)->s6_addr32[0]) == 0) && \
+   (((a)->s6_addr32[1]) == 0) && \
+   (((a)->s6_addr32[2]) == ntohl(0x0000ffff)))
 
-#define __bionic_s6_addr(a) ((const uint8_t*)(a))
+#define __bionic_s6_addr(a) __BIONIC_CAST(reinterpret_cast, const uint8_t*, a)
 
 #define IN6_IS_ADDR_LINKLOCAL(a) \
   ((__bionic_s6_addr(a)[0] == 0xfe) && ((__bionic_s6_addr(a)[1] & 0xc0) == 0x80))
diff --git a/libc/include/netinet/in_systm.h b/libc/include/netinet/in_systm.h
index 188268e..8da19a5 100644
--- a/libc/include/netinet/in_systm.h
+++ b/libc/include/netinet/in_systm.h
@@ -34,6 +34,7 @@
 #ifndef _NETINET_IN_SYSTM_H_
 #define _NETINET_IN_SYSTM_H_
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 /*
diff --git a/libc/include/netinet/ip6.h b/libc/include/netinet/ip6.h
index 52fcfae..639637e 100644
--- a/libc/include/netinet/ip6.h
+++ b/libc/include/netinet/ip6.h
@@ -64,6 +64,7 @@
 #ifndef _NETINET_IP6_H_
 #define _NETINET_IP6_H_
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 #include <linux/in6.h>
diff --git a/libc/include/netinet/udp.h b/libc/include/netinet/udp.h
index d4eb368..ef517d6 100644
--- a/libc/include/netinet/udp.h
+++ b/libc/include/netinet/udp.h
@@ -29,6 +29,7 @@
 #ifndef _NETINET_UDP_H
 #define _NETINET_UDP_H
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 #include <linux/udp.h>
diff --git a/libc/include/paths.h b/libc/include/paths.h
index ffb1737..b76d045 100644
--- a/libc/include/paths.h
+++ b/libc/include/paths.h
@@ -32,6 +32,8 @@
 #ifndef _PATHS_H_
 #define	_PATHS_H_
 
+#include <sys/cdefs.h>
+
 #define	_PATH_BSHELL	"/system/bin/sh"
 #define	_PATH_CONSOLE	"/dev/console"
 #define	_PATH_DEFPATH	"/sbin:/system/sbin:/system/bin:/system/xbin:/vendor/bin:/vendor/xbin"
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 3262bd1..1153695 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -36,6 +36,13 @@
 #include <sys/types.h>
 #include <time.h>
 
+__BEGIN_DECLS
+
+#if defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnullability-completeness"
+#endif
+
 typedef struct {
 #if defined(__LP64__)
   int32_t __private[10];
@@ -131,8 +138,6 @@
 #define PTHREAD_SCOPE_SYSTEM     0
 #define PTHREAD_SCOPE_PROCESS    1
 
-__BEGIN_DECLS
-
 int pthread_atfork(void (*)(void), void (*)(void), void (*)(void)) __INTRODUCED_IN(12);
 
 int pthread_attr_destroy(pthread_attr_t* _Nonnull);
@@ -263,8 +268,8 @@
   void*                         __cleanup_arg;
 } __pthread_cleanup_t;
 
-extern void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
-extern void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
+void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
+void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
 
 /* Believe or not, the definitions of pthread_cleanup_push and
  * pthread_cleanup_pop below are correct. Posix states that these
@@ -281,6 +286,10 @@
         __pthread_cleanup_pop( &__cleanup, (execute)); \
     } while (0);                                       \
 
+#if defined(__clang__)
+#pragma clang diagnostic pop
+#endif
+
 __END_DECLS
 
 #endif /* _PTHREAD_H_ */
diff --git a/libc/include/pwd.h b/libc/include/pwd.h
index 0b39a78..a686d05 100644
--- a/libc/include/pwd.h
+++ b/libc/include/pwd.h
@@ -63,6 +63,8 @@
 #include <sys/cdefs.h>
 #include <sys/types.h>
 
+__BEGIN_DECLS
+
 #define _PATH_PASSWD        "/etc/passwd"
 #define _PATH_MASTERPASSWD  "/etc/master.passwd"
 #define _PATH_MASTERPASSWD_LOCK "/etc/ptmp"
@@ -96,8 +98,7 @@
 #define _PASSWORD_WARNDAYS  14  /* days to warn about expiry */
 #define _PASSWORD_CHGNOW    -1  /* special day to force password change at next login */
 
-struct passwd
-{
+struct passwd {
   char* pw_name;
   char* pw_passwd;
   uid_t pw_uid;
@@ -112,13 +113,12 @@
   char* pw_shell;
 };
 
-__BEGIN_DECLS
-
 struct passwd* getpwnam(const char*);
 struct passwd* getpwuid(uid_t);
-/* Android has thousands and thousands of ids to iterate through */
-struct passwd* getpwent(void)
-  __attribute__((warning("getpwent is inefficient on Android"))) __INTRODUCED_IN_FUTURE;
+
+/* Note: Android has thousands and thousands of ids to iterate through */
+struct passwd* getpwent(void) __INTRODUCED_IN_FUTURE;
+
 void setpwent(void) __INTRODUCED_IN_FUTURE;
 void endpwent(void) __INTRODUCED_IN_FUTURE;
 
diff --git a/libc/include/resolv.h b/libc/include/resolv.h
index aca3552..2c12819 100644
--- a/libc/include/resolv.h
+++ b/libc/include/resolv.h
@@ -38,7 +38,6 @@
 #include <netinet/in.h>
 
 __BEGIN_DECLS
-#pragma GCC visibility push(default)
 
 #define b64_ntop __b64_ntop
 int b64_ntop(u_char const*, size_t, char*, size_t);
@@ -60,7 +59,6 @@
 int res_query(const char*, int, int, u_char*, int);
 int res_search(const char*, int, int, u_char*, int);
 
-#pragma GCC visibility pop
 __END_DECLS
 
 #endif /* _RESOLV_H_ */
diff --git a/libc/include/sched.h b/libc/include/sched.h
index 7cb11a7..d407202 100644
--- a/libc/include/sched.h
+++ b/libc/include/sched.h
@@ -41,22 +41,22 @@
   int sched_priority;
 };
 
-extern int sched_setscheduler(pid_t, int, const struct sched_param*);
-extern int sched_getscheduler(pid_t);
-extern int sched_yield(void);
-extern int sched_get_priority_max(int);
-extern int sched_get_priority_min(int);
-extern int sched_setparam(pid_t, const struct sched_param*);
-extern int sched_getparam(pid_t, struct sched_param*);
-extern int sched_rr_get_interval(pid_t, struct timespec*);
+int sched_setscheduler(pid_t, int, const struct sched_param*);
+int sched_getscheduler(pid_t);
+int sched_yield(void);
+int sched_get_priority_max(int);
+int sched_get_priority_min(int);
+int sched_setparam(pid_t, const struct sched_param*);
+int sched_getparam(pid_t, struct sched_param*);
+int sched_rr_get_interval(pid_t, struct timespec*);
 
 #if defined(__USE_GNU)
 
-extern int clone(int (*)(void*), void*, int, void*, ...) __INTRODUCED_IN_ARM(9)
+int clone(int (*)(void*), void*, int, void*, ...) __INTRODUCED_IN_ARM(9)
     __INTRODUCED_IN_MIPS(12) __INTRODUCED_IN_X86(17);
-extern int unshare(int) __INTRODUCED_IN(17);
-extern int sched_getcpu(void) __INTRODUCED_IN(12);
-extern int setns(int, int) __INTRODUCED_IN(21);
+int unshare(int) __INTRODUCED_IN(17);
+int sched_getcpu(void) __INTRODUCED_IN(12);
+int setns(int, int) __INTRODUCED_IN(21);
 
 #ifdef __LP64__
 #define CPU_SETSIZE 1024
@@ -73,8 +73,8 @@
   __CPU_BITTYPE  __bits[ CPU_SETSIZE / __CPU_BITS ];
 } cpu_set_t;
 
-extern int sched_setaffinity(pid_t pid, size_t setsize, const cpu_set_t* set) __INTRODUCED_IN(12);
-extern int sched_getaffinity(pid_t pid, size_t setsize, cpu_set_t* set) __INTRODUCED_IN(12);
+int sched_setaffinity(pid_t pid, size_t setsize, const cpu_set_t* set) __INTRODUCED_IN(12);
+int sched_getaffinity(pid_t pid, size_t setsize, cpu_set_t* set) __INTRODUCED_IN(12);
 
 #define CPU_ZERO(set)          CPU_ZERO_S(sizeof(cpu_set_t), set)
 #define CPU_SET(cpu, set)      CPU_SET_S(cpu, sizeof(cpu_set_t), set)
@@ -97,8 +97,8 @@
 #define CPU_ALLOC(count)  __sched_cpualloc((count))
 #define CPU_FREE(set)     __sched_cpufree((set))
 
-extern cpu_set_t* __sched_cpualloc(size_t count) __INTRODUCED_IN(12);
-extern void __sched_cpufree(cpu_set_t* set) __INTRODUCED_IN(12);
+cpu_set_t* __sched_cpualloc(size_t count) __INTRODUCED_IN(12);
+void __sched_cpufree(cpu_set_t* set) __INTRODUCED_IN(12);
 
 #define CPU_ZERO_S(setsize, set)  __builtin_memset(set, 0, setsize)
 
@@ -142,7 +142,7 @@
 
 #define CPU_COUNT_S(setsize, set)  __sched_cpucount((setsize), (set))
 
-extern int __sched_cpucount(size_t setsize, cpu_set_t* set) __INTRODUCED_IN(12);
+int __sched_cpucount(size_t setsize, cpu_set_t* set) __INTRODUCED_IN(12);
 
 #endif /* __USE_GNU */
 
diff --git a/libc/include/signal.h b/libc/include/signal.h
index 3905d86..085f92f 100644
--- a/libc/include/signal.h
+++ b/libc/include/signal.h
@@ -53,6 +53,11 @@
 
 __BEGIN_DECLS
 
+#if defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnullability-completeness"
+#endif
+
 typedef int sig_atomic_t;
 
 /* The arm and x86 kernel header files don't define _NSIG. */
@@ -67,16 +72,16 @@
 /* The kernel headers define SIG_DFL (0) and SIG_IGN (1) but not SIG_HOLD, since
  * SIG_HOLD is only used by the deprecated SysV signal API.
  */
-#define SIG_HOLD ((sighandler_t)(uintptr_t)2)
+#define SIG_HOLD __BIONIC_CAST(reinterpret_cast, sighandler_t, 2)
 
 /* We take a few real-time signals for ourselves. May as well use the same names as glibc. */
 #define SIGRTMIN (__libc_current_sigrtmin())
 #define SIGRTMAX (__libc_current_sigrtmax())
-extern int __libc_current_sigrtmin(void) __INTRODUCED_IN(21);
-extern int __libc_current_sigrtmax(void) __INTRODUCED_IN(21);
+int __libc_current_sigrtmin(void) __INTRODUCED_IN(21);
+int __libc_current_sigrtmax(void) __INTRODUCED_IN(21);
 
-extern const char* const sys_siglist[];
-extern const char* const sys_signame[]; /* BSD compatibility. */
+extern const char* const sys_siglist[_NSIG];
+extern const char* const sys_signame[_NSIG]; /* BSD compatibility. */
 
 typedef __sighandler_t sig_t; /* BSD compatibility. */
 typedef __sighandler_t sighandler_t; /* glibc compatibility. */
@@ -108,51 +113,58 @@
 
 #endif
 
-extern int sigaction(int, const struct sigaction*, struct sigaction*);
+int sigaction(int, const struct sigaction*, struct sigaction*);
 
-__BIONIC_LEGACY_INLINE sighandler_t signal(int, sighandler_t);
+int siginterrupt(int, int);
 
-extern int siginterrupt(int, int);
+#if __ANDROID_API__ >= 21
+sighandler_t signal(int, sighandler_t);
+int sigaddset(sigset_t*, int);
+int sigdelset(sigset_t*, int);
+int sigemptyset(sigset_t*);
+int sigfillset(sigset_t*);
+int sigismember(const sigset_t*, int);
+#else
+// Implemented as static inlines before 21.
+#endif
 
-__BIONIC_LEGACY_INLINE int sigaddset(sigset_t*, int);
-__BIONIC_LEGACY_INLINE int sigdelset(sigset_t*, int);
-__BIONIC_LEGACY_INLINE int sigemptyset(sigset_t*);
-__BIONIC_LEGACY_INLINE int sigfillset(sigset_t*);
-__BIONIC_LEGACY_INLINE int sigismember(const sigset_t*, int);
+int sigpending(sigset_t* _Nonnull);
+int sigprocmask(int, const sigset_t*, sigset_t*);
+int sigsuspend(const sigset_t* _Nonnull);
+int sigwait(const sigset_t* _Nonnull, int* _Nonnull);
 
-extern int sigpending(sigset_t* _Nonnull);
-extern int sigprocmask(int, const sigset_t*, sigset_t*);
-extern int sigsuspend(const sigset_t* _Nonnull);
-extern int sigwait(const sigset_t* _Nonnull, int* _Nonnull);
-
-extern int sighold(int)
+int sighold(int)
   __attribute__((deprecated("use sigprocmask() or pthread_sigmask() instead")))
   __INTRODUCED_IN_FUTURE;
-extern int sigignore(int)
+int sigignore(int)
   __attribute__((deprecated("use sigaction() instead"))) __INTRODUCED_IN_FUTURE;
-extern int sigpause(int)
+int sigpause(int)
   __attribute__((deprecated("use sigsuspend() instead"))) __INTRODUCED_IN_FUTURE;
-extern int sigrelse(int)
+int sigrelse(int)
   __attribute__((deprecated("use sigprocmask() or pthread_sigmask() instead")))
   __INTRODUCED_IN_FUTURE;
-extern sighandler_t sigset(int, sighandler_t)
+sighandler_t sigset(int, sighandler_t)
   __attribute__((deprecated("use sigaction() instead"))) __INTRODUCED_IN_FUTURE;
 
-extern int raise(int);
-extern int kill(pid_t, int);
-extern int killpg(int, int);
+int raise(int);
+int kill(pid_t, int);
+int killpg(int, int);
 
-extern int sigaltstack(const stack_t*, stack_t*);
+int sigaltstack(const stack_t*, stack_t*);
 
-extern void psiginfo(const siginfo_t*, const char*) __INTRODUCED_IN(17);
-extern void psignal(int, const char*) __INTRODUCED_IN(17);
+void psiginfo(const siginfo_t*, const char*) __INTRODUCED_IN(17);
+void psignal(int, const char*) __INTRODUCED_IN(17);
 
-extern int pthread_kill(pthread_t, int);
-extern int pthread_sigmask(int, const sigset_t*, sigset_t*);
+int pthread_kill(pthread_t, int);
+int pthread_sigmask(int, const sigset_t*, sigset_t*);
 
-extern int sigqueue(pid_t, int, const union sigval) __INTRODUCED_IN(23);
-extern int sigtimedwait(const sigset_t*, siginfo_t*, const struct timespec*) __INTRODUCED_IN(23);
-extern int sigwaitinfo(const sigset_t*, siginfo_t*) __INTRODUCED_IN(23);
+int sigqueue(pid_t, int, const union sigval) __INTRODUCED_IN(23);
+int sigtimedwait(const sigset_t* _Nonnull, siginfo_t*, const struct timespec*) __INTRODUCED_IN(23);
+int sigwaitinfo(const sigset_t* _Nonnull, siginfo_t*) __INTRODUCED_IN(23);
+
+#if defined(__clang__)
+#pragma clang diagnostic pop
+#endif
 
 __END_DECLS
 
diff --git a/libc/include/stdint.h b/libc/include/stdint.h
index a66e21e..1cedca1 100644
--- a/libc/include/stdint.h
+++ b/libc/include/stdint.h
@@ -31,8 +31,9 @@
 
 #include <bits/wchar_limits.h>
 #include <stddef.h>
+#include <sys/cdefs.h>
 
-typedef __signed char __int8_t;
+typedef signed char __int8_t;
 typedef unsigned char __uint8_t;
 typedef short __int16_t;
 typedef unsigned short __uint16_t;
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index 621caa8..03effcd 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -49,6 +49,11 @@
 
 __BEGIN_DECLS
 
+#if defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnullability-completeness"
+#endif
+
 typedef off_t fpos_t;
 typedef off64_t fpos64_t;
 
@@ -144,17 +149,16 @@
 int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __INTRODUCED_IN(21);
 int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __INTRODUCED_IN(21);
 
-#if __STDC_VERSION__ < 201112L
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \
+    (defined(__cplusplus) && __cplusplus <= 201103L)
 char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
 #endif
-int sprintf(char* __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf");
-int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf");
+int sprintf(char* __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
+int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
 char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
-#if defined(__USE_BSD) || defined(__USE_GNU)
 #define P_tmpdir "/tmp/" /* deprecated */
 char* tempnam(const char*, const char*)
     __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
-#endif
 
 int rename(const char*, const char*);
 int renameat(int, const char*, int, const char*);
@@ -243,20 +247,20 @@
 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
 #endif /* __USE_BSD */
 
-extern char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
-extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
+char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
+char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
 __errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
 __errordecl(__fgets_too_small_error, "fgets called with size less than zero");
 
-extern size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
+size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
   __INTRODUCED_IN(24);
-extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
+size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
 __errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
 __errordecl(__fread_overflow, "fread called with overflowing size * count");
 
-extern size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
+size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
   __INTRODUCED_IN(24);
-extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
+size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
 __errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
 __errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
 
@@ -384,6 +388,10 @@
 
 #endif /* defined(__BIONIC_FORTIFY) */
 
+#if defined(__clang__)
+#pragma clang diagnostic pop
+#endif
+
 __END_DECLS
 
 #endif /* _STDIO_H_ */
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h
index 65b92a6..f6c3baf 100644
--- a/libc/include/stdlib.h
+++ b/libc/include/stdlib.h
@@ -41,65 +41,57 @@
 #define EXIT_FAILURE 1
 #define EXIT_SUCCESS 0
 
-extern __noreturn void abort(void);
-extern __noreturn void exit(int);
-extern __noreturn void _Exit(int) __INTRODUCED_IN(21);
-extern int atexit(void (*)(void));
+__noreturn void abort(void);
+__noreturn void exit(int);
+__noreturn void _Exit(int) __INTRODUCED_IN(21);
+int atexit(void (*)(void));
 
 int at_quick_exit(void (*)(void)) __INTRODUCED_IN(21);
 void quick_exit(int) __noreturn __INTRODUCED_IN(21);
 
-extern char* getenv(const char*);
-extern int putenv(char*);
-extern int setenv(const char*, const char*, int);
-extern int unsetenv(const char*);
-extern int clearenv(void);
+char* getenv(const char*);
+int putenv(char*);
+int setenv(const char*, const char*, int);
+int unsetenv(const char*);
+int clearenv(void);
 
-extern char* mkdtemp(char*);
-extern char* mktemp(char*) __attribute__((deprecated("mktemp is unsafe, use mkstemp or tmpfile instead")));
+char* mkdtemp(char*);
+char* mktemp(char*) __attribute__((deprecated("mktemp is unsafe, use mkstemp or tmpfile instead")));
 
-extern int mkostemp64(char*, int) __INTRODUCED_IN(23);
-extern int mkostemp(char*, int) __INTRODUCED_IN(23);
-extern int mkostemps64(char*, int, int) __INTRODUCED_IN(23);
-extern int mkostemps(char*, int, int) __INTRODUCED_IN(23);
-extern int mkstemp64(char*) __INTRODUCED_IN(21);
-extern int mkstemp(char*);
-extern int mkstemps64(char*, int) __INTRODUCED_IN(23);
-extern int mkstemps(char*, int);
+int mkostemp64(char*, int) __INTRODUCED_IN(23);
+int mkostemp(char*, int) __INTRODUCED_IN(23);
+int mkostemps64(char*, int, int) __INTRODUCED_IN(23);
+int mkostemps(char*, int, int) __INTRODUCED_IN(23);
+int mkstemp64(char*) __INTRODUCED_IN(21);
+int mkstemp(char*);
+int mkstemps64(char*, int) __INTRODUCED_IN(23);
+int mkstemps(char*, int);
 
-extern long strtol(const char *, char **, int);
-extern long long strtoll(const char *, char **, int);
-extern unsigned long strtoul(const char *, char **, int);
-extern unsigned long long strtoull(const char *, char **, int);
+long strtol(const char *, char **, int);
+long long strtoll(const char *, char **, int);
+unsigned long strtoul(const char *, char **, int);
+unsigned long long strtoull(const char *, char **, int);
 
-extern int posix_memalign(void** memptr, size_t alignment, size_t size) __INTRODUCED_IN(16);
+int posix_memalign(void** memptr, size_t alignment, size_t size) __INTRODUCED_IN(16);
 
-__BIONIC_LEGACY_INLINE double atof(const char*);
+double strtod(const char*, char**);
+long double strtold(const char*, char**) __INTRODUCED_IN(21);
 
-extern double strtod(const char*, char**);
-__BIONIC_LEGACY_INLINE float strtof(const char*, char**);
-extern long double strtold(const char*, char**) __INTRODUCED_IN(21);
+long double strtold_l(const char*, char**, locale_t) __INTRODUCED_IN(21);
+long long strtoll_l(const char*, char**, int, locale_t) __INTRODUCED_IN(21);
+unsigned long long strtoull_l(const char*, char**, int, locale_t) __INTRODUCED_IN(21);
 
-extern long double strtold_l(const char*, char**, locale_t) __INTRODUCED_IN(21);
-extern long long strtoll_l(const char*, char**, int, locale_t) __INTRODUCED_IN(21);
-extern unsigned long long strtoull_l(const char*, char**, int, locale_t) __INTRODUCED_IN(21);
+int atoi(const char*) __purefunc;
+long atol(const char*) __purefunc;
+long long atoll(const char*) __purefunc;
 
-extern int atoi(const char*) __purefunc;
-extern long atol(const char*) __purefunc;
-extern long long atoll(const char*) __purefunc;
+char* realpath(const char* path, char* resolved);
+int system(const char* string);
 
-__BIONIC_LEGACY_INLINE int abs(int) __pure2;
-__BIONIC_LEGACY_INLINE long labs(long) __pure2;
-__BIONIC_LEGACY_INLINE long long llabs(long long) __pure2;
+void* bsearch(const void* key, const void* base0, size_t nmemb, size_t size,
+              int (*compar)(const void*, const void*));
 
-extern char * realpath(const char *path, char *resolved);
-extern int system(const char *string);
-
-extern void * bsearch(const void *key, const void *base0,
-	size_t nmemb, size_t size,
-	int (*compar)(const void *, const void *));
-
-extern void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
+void qsort(void*, size_t, size_t, int (*)(const void*, const void*));
 
 uint32_t arc4random(void);
 uint32_t arc4random_uniform(uint32_t);
@@ -107,9 +99,7 @@
 
 #define RAND_MAX 0x7fffffff
 
-__BIONIC_LEGACY_INLINE int rand(void);
 int rand_r(unsigned int*) __INTRODUCED_IN(21);
-__BIONIC_LEGACY_INLINE void srand(unsigned int);
 
 double drand48(void);
 double erand48(unsigned short[3]);
@@ -122,12 +112,9 @@
 void srand48(long);
 
 char* initstate(unsigned int, char*, size_t) __INTRODUCED_IN(21);
-__BIONIC_LEGACY_INLINE long random(void);
 char* setstate(char*) __INTRODUCED_IN(21);
-__BIONIC_LEGACY_INLINE void srandom(unsigned int);
 
 int getpt(void);
-__BIONIC_LEGACY_INLINE int grantpt(int);
 int posix_openpt(int) __INTRODUCED_IN(21);
 char* ptsname(int);
 int ptsname_r(int, char*, size_t);
@@ -140,25 +127,25 @@
     int  rem;
 } div_t;
 
-extern div_t   div(int, int) __pure2;
+div_t div(int, int) __pure2;
 
 typedef struct {
     long int  quot;
     long int  rem;
 } ldiv_t;
 
-extern ldiv_t   ldiv(long, long) __pure2;
+ldiv_t ldiv(long, long) __pure2;
 
 typedef struct {
     long long int  quot;
     long long int  rem;
 } lldiv_t;
 
-extern lldiv_t   lldiv(long long, long long) __pure2;
+lldiv_t lldiv(long long, long long) __pure2;
 
 /* BSD compatibility. */
-extern const char* getprogname(void) __INTRODUCED_IN(21);
-extern void setprogname(const char*) __INTRODUCED_IN(21);
+const char* getprogname(void) __INTRODUCED_IN(21);
+void setprogname(const char*) __INTRODUCED_IN(21);
 
 int mblen(const char*, size_t) __INTRODUCED_IN_FUTURE;
 size_t mbstowcs(wchar_t*, const char*, size_t);
@@ -166,12 +153,12 @@
 int wctomb(char*, wchar_t) __INTRODUCED_IN(21);
 size_t wcstombs(char*, const wchar_t*, size_t);
 
-extern size_t __ctype_get_mb_cur_max(void) __INTRODUCED_IN(21);
+size_t __ctype_get_mb_cur_max(void) __INTRODUCED_IN(21);
 #define MB_CUR_MAX __ctype_get_mb_cur_max()
 
 #if defined(__BIONIC_FORTIFY)
 
-extern char* __realpath_real(const char*, char*) __RENAME(realpath);
+char* __realpath_real(const char*, char*) __RENAME(realpath);
 __errordecl(__realpath_size_error, "realpath output parameter must be NULL or a >= PATH_MAX bytes buffer");
 
 #if !defined(__clang__)
@@ -190,6 +177,21 @@
 
 #endif /* defined(__BIONIC_FORTIFY) */
 
+#if __ANDROID_API__ >= 21
+float strtof(const char*, char**);
+double atof(const char*);
+int abs(int) __pure2;
+long labs(long) __pure2;
+long long llabs(long long) __pure2;
+int rand(void);
+void srand(unsigned int);
+long random(void);
+void srandom(unsigned int);
+int grantpt(int);
+#else
+// Implemented as static inlines before 21.
+#endif
+
 __END_DECLS
 
 #include <android/legacy_stdlib_inlines.h>
diff --git a/libc/include/string.h b/libc/include/string.h
index b3ea9d6..c5bcd22 100644
--- a/libc/include/string.h
+++ b/libc/include/string.h
@@ -37,117 +37,119 @@
 
 __BEGIN_DECLS
 
+#if defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnullability-completeness"
+#endif
+
 #if defined(__USE_BSD)
 #include <strings.h>
 #endif
 
-extern void*  memccpy(void* __restrict, const void* __restrict, int, size_t);
-extern void*  memchr(const void *, int, size_t) __purefunc;
-extern void*  memrchr(const void *, int, size_t) __purefunc;
-extern int    memcmp(const void *, const void *, size_t) __purefunc;
-extern void*  memcpy(void* __restrict, const void* __restrict, size_t);
+void* memccpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, int, size_t);
+void* memchr(const void* _Nonnull, int, size_t) __purefunc;
+void* memrchr(const void* _Nonnull, int, size_t) __purefunc;
+int memcmp(const void* _Nonnull, const void* _Nonnull, size_t) __purefunc;
+void* memcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t);
 #if defined(__USE_GNU)
-extern void* mempcpy(void* __restrict, const void* __restrict, size_t) __INTRODUCED_IN(23);
+void* mempcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t) __INTRODUCED_IN(23);
 #endif
-extern void*  memmove(void *, const void *, size_t);
-extern void*  memset(void *, int, size_t);
-extern void*  memmem(const void *, size_t, const void *, size_t) __purefunc;
+void* memmove(void* _Nonnull, const void* _Nonnull, size_t);
+void* memset(void* _Nonnull, int, size_t);
+void* memmem(const void* _Nonnull, size_t, const void* _Nonnull, size_t) __purefunc;
 
-extern char*  strchr(const char *, int) __purefunc;
-extern char* __strchr_chk(const char*, int, size_t) __INTRODUCED_IN(18);
+char* strchr(const char* _Nonnull, int) __purefunc;
+char* __strchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18);
 #if defined(__USE_GNU)
 #if defined(__cplusplus)
-extern "C++" char* strchrnul(char*, int) __RENAME(strchrnul) __purefunc;
-extern "C++" const char* strchrnul(const char*, int) __RENAME(strchrnul) __purefunc;
+extern "C++" char* strchrnul(char* _Nonnull, int) __RENAME(strchrnul) __purefunc;
+extern "C++" const char* strchrnul(const char* _Nonnull, int) __RENAME(strchrnul) __purefunc;
 #else
-char* strchrnul(const char*, int) __purefunc __INTRODUCED_IN(24);
+char* strchrnul(const char* _Nonnull, int) __purefunc __INTRODUCED_IN(24);
 #endif
 #endif
 
-extern char*  strrchr(const char *, int) __purefunc;
-extern char* __strrchr_chk(const char*, int, size_t) __INTRODUCED_IN(18);
+char* strrchr(const char* _Nonnull, int) __purefunc;
+char* __strrchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18);
 
-extern size_t strlen(const char *) __purefunc;
-extern size_t __strlen_chk(const char*, size_t) __INTRODUCED_IN(17);
-extern int    strcmp(const char *, const char *) __purefunc;
-extern char* stpcpy(char* __restrict, const char* __restrict) __INTRODUCED_IN(21);
-extern char*  strcpy(char* __restrict, const char* __restrict);
-extern char*  strcat(char* __restrict, const char* __restrict);
+size_t strlen(const char* _Nonnull) __purefunc;
+size_t __strlen_chk(const char* _Nonnull, size_t) __INTRODUCED_IN(17);
+int strcmp(const char* _Nonnull, const char* _Nonnull) __purefunc;
+char* stpcpy(char* _Nonnull __restrict, const char* _Nonnull__restrict) __INTRODUCED_IN(21);
+char* strcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict);
+char* strcat(char* _Nonnull __restrict, const char* _Nonnull __restrict);
 
-extern char*  strdup(const char *);
+char* strdup(const char* _Nonnull);
 
-extern char*  strstr(const char *, const char *) __purefunc;
-extern char*  strcasestr(const char *haystack, const char *needle) __purefunc;
-extern char*  strtok(char* __restrict, const char* __restrict);
-extern char*  strtok_r(char* __restrict, const char* __restrict, char** __restrict);
+char* strstr(const char* _Nonnull, const char* _Nonnull) __purefunc;
+char* strcasestr(const char* _Nonnull, const char* _Nonnull) __purefunc;
+char* strtok(char* __restrict, const char* _Nonnull __restrict);
+char* strtok_r(char* __restrict, const char* _Nonnull __restrict, char** _Nonnull __restrict);
 
-extern char* strerror(int);
-extern char* strerror_l(int, locale_t) __INTRODUCED_IN(23);
+char* strerror(int);
+char* strerror_l(int, locale_t) __INTRODUCED_IN(23);
 #if defined(__USE_GNU)
-extern char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
+char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
 #else /* POSIX */
-extern int strerror_r(int, char*, size_t);
+int strerror_r(int, char*, size_t);
 #endif
 
-extern size_t strnlen(const char *, size_t) __purefunc;
-extern char*  strncat(char* __restrict, const char* __restrict, size_t);
-extern char*  strndup(const char *, size_t);
-extern int    strncmp(const char *, const char *, size_t) __purefunc;
-extern char* stpncpy(char* __restrict, const char* __restrict, size_t) __INTRODUCED_IN(21);
-extern char*  strncpy(char* __restrict, const char* __restrict, size_t);
+size_t strnlen(const char* _Nonnull, size_t) __purefunc;
+char* strncat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t);
+char* strndup(const char* _Nonnull, size_t);
+int strncmp(const char* _Nonnull, const char* _Nonnull, size_t) __purefunc;
+char* stpncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __INTRODUCED_IN(21);
+char* strncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t);
 
-extern size_t strlcat(char* __restrict, const char* __restrict, size_t);
-extern size_t strlcpy(char* __restrict, const char* __restrict, size_t);
+size_t strlcat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t);
+size_t strlcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t);
 
-extern size_t strcspn(const char *, const char *) __purefunc;
-extern char*  strpbrk(const char *, const char *) __purefunc;
-extern char*  strsep(char** __restrict, const char* __restrict);
-extern size_t strspn(const char *, const char *);
+size_t strcspn(const char* _Nonnull, const char* _Nonnull) __purefunc;
+char* strpbrk(const char* _Nonnull, const char* _Nonnull) __purefunc;
+char* strsep(char** _Nonnull __restrict, const char* _Nonnull __restrict);
+size_t strspn(const char* _Nonnull, const char* _Nonnull);
 
-extern char*  strsignal(int  sig);
+char* strsignal(int);
 
-extern int    strcoll(const char *, const char *) __purefunc;
-extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
+int strcoll(const char* _Nonnull, const char* _Nonnull) __purefunc;
+size_t strxfrm(char* __restrict, const char* _Nonnull __restrict, size_t);
 
-extern int strcoll_l(const char*, const char*, locale_t) __purefunc __INTRODUCED_IN(21);
-extern size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t)
-  __INTRODUCED_IN(21);
+int strcoll_l(const char* _Nonnull, const char* _Nonnull, locale_t) __purefunc __INTRODUCED_IN(21);
+size_t strxfrm_l(char* __restrict, const char* _Nonnull __restrict, size_t, locale_t) __INTRODUCED_IN(21);
 
 #if defined(__USE_GNU) && !defined(basename)
 /*
  * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
  * It doesn't modify its argument, and in C++ it's const-correct.
  */
-
 #if defined(__cplusplus)
 extern "C++" char* basename(char* _Nonnull) __RENAME(__gnu_basename);
 extern "C++" const char* basename(const char* _Nonnull) __RENAME(__gnu_basename);
 #else
-extern char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
+char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
 #endif
 #endif
 
-extern void* __memchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23);
+void* __memchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23);
 __errordecl(__memchr_buf_size_error, "memchr called with size bigger than buffer");
 
-extern void* __memrchr_chk(const void*, int, size_t, size_t) __INTRODUCED_IN(23);
+void* __memrchr_chk(const void* _Nonnull, int, size_t, size_t) __INTRODUCED_IN(23);
 __errordecl(__memrchr_buf_size_error, "memrchr called with size bigger than buffer");
-extern void* __memrchr_real(const void*, int, size_t) __RENAME(memrchr);
+void* __memrchr_real(const void* _Nonnull, int, size_t) __RENAME(memrchr);
 
-extern char* __stpncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t)
+char* __stpncpy_chk2(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t, size_t)
   __INTRODUCED_IN(21);
-extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t)
+char* __strncpy_chk2(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t, size_t)
   __INTRODUCED_IN(21);
-extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcpy);
-extern size_t __strlcpy_chk(char*, const char*, size_t, size_t) __INTRODUCED_IN(17);
-extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcat);
-extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t)
-  __INTRODUCED_IN(17);
+size_t __strlcpy_real(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __RENAME(strlcpy);
+size_t __strlcpy_chk(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t) __INTRODUCED_IN(17);
+size_t __strlcat_real(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) __RENAME(strlcat);
+size_t __strlcat_chk(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t, size_t) __INTRODUCED_IN(17);
 
 #if defined(__BIONIC_FORTIFY)
 
 __BIONIC_FORTIFY_INLINE
-void* memchr(const void *s, int c, size_t n) {
+void* memchr(const void* s, int c, size_t n) {
     size_t bos = __bos(s);
 
 #if !defined(__clang__)
@@ -168,7 +170,7 @@
 }
 
 __BIONIC_FORTIFY_INLINE
-void* memrchr(const void *s, int c, size_t n) {
+void* memrchr(const void* s, int c, size_t n) {
     size_t bos = __bos(s);
 
 #if !defined(__clang__)
@@ -189,125 +191,125 @@
 }
 
 __BIONIC_FORTIFY_INLINE
-void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) {
-    return __builtin___memcpy_chk(dest, src, copy_amount, __bos0(dest));
+void* memcpy(void* _Nonnull __restrict dst, const void* _Nonnull __restrict src, size_t copy_amount) {
+    return __builtin___memcpy_chk(dst, src, copy_amount, __bos0(dst));
 }
 
 __BIONIC_FORTIFY_INLINE
-void* memmove(void *dest, const void *src, size_t len) {
-    return __builtin___memmove_chk(dest, src, len, __bos0(dest));
+void* memmove(void* _Nonnull dst, const void* _Nonnull src, size_t len) {
+    return __builtin___memmove_chk(dst, src, len, __bos0(dst));
 }
 
 __BIONIC_FORTIFY_INLINE
-char* stpcpy(char* __restrict dest, const char* __restrict src) {
-    return __builtin___stpcpy_chk(dest, src, __bos(dest));
+char* stpcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) {
+    return __builtin___stpcpy_chk(dst, src, __bos(dst));
 }
 
 __BIONIC_FORTIFY_INLINE
-char* strcpy(char* __restrict dest, const char* __restrict src) {
-    return __builtin___strcpy_chk(dest, src, __bos(dest));
+char* strcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) {
+    return __builtin___strcpy_chk(dst, src, __bos(dst));
 }
 
 __BIONIC_FORTIFY_INLINE
-char* stpncpy(char* __restrict dest, const char* __restrict src, size_t n) {
-    size_t bos_dest = __bos(dest);
+char* stpncpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) {
+    size_t bos_dst = __bos(dst);
     size_t bos_src = __bos(src);
 
     if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
-        return __builtin___stpncpy_chk(dest, src, n, bos_dest);
+        return __builtin___stpncpy_chk(dst, src, n, bos_dst);
     }
 
     if (__builtin_constant_p(n) && (n <= bos_src)) {
-        return __builtin___stpncpy_chk(dest, src, n, bos_dest);
+        return __builtin___stpncpy_chk(dst, src, n, bos_dst);
     }
 
     size_t slen = __builtin_strlen(src);
     if (__builtin_constant_p(slen)) {
-        return __builtin___stpncpy_chk(dest, src, n, bos_dest);
+        return __builtin___stpncpy_chk(dst, src, n, bos_dst);
     }
 
-    return __stpncpy_chk2(dest, src, n, bos_dest, bos_src);
+    return __stpncpy_chk2(dst, src, n, bos_dst, bos_src);
 }
 
 __BIONIC_FORTIFY_INLINE
-char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
-    size_t bos_dest = __bos(dest);
+char* strncpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) {
+    size_t bos_dst = __bos(dst);
     size_t bos_src = __bos(src);
 
     if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
-        return __builtin___strncpy_chk(dest, src, n, bos_dest);
+        return __builtin___strncpy_chk(dst, src, n, bos_dst);
     }
 
     if (__builtin_constant_p(n) && (n <= bos_src)) {
-        return __builtin___strncpy_chk(dest, src, n, bos_dest);
+        return __builtin___strncpy_chk(dst, src, n, bos_dst);
     }
 
     size_t slen = __builtin_strlen(src);
     if (__builtin_constant_p(slen)) {
-        return __builtin___strncpy_chk(dest, src, n, bos_dest);
+        return __builtin___strncpy_chk(dst, src, n, bos_dst);
     }
 
-    return __strncpy_chk2(dest, src, n, bos_dest, bos_src);
+    return __strncpy_chk2(dst, src, n, bos_dst, bos_src);
 }
 
 __BIONIC_FORTIFY_INLINE
-char* strcat(char* __restrict dest, const char* __restrict src) {
-    return __builtin___strcat_chk(dest, src, __bos(dest));
+char* strcat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src) {
+    return __builtin___strcat_chk(dst, src, __bos(dst));
 }
 
 __BIONIC_FORTIFY_INLINE
-char *strncat(char* __restrict dest, const char* __restrict src, size_t n) {
-    return __builtin___strncat_chk(dest, src, n, __bos(dest));
+char *strncat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t n) {
+    return __builtin___strncat_chk(dst, src, n, __bos(dst));
 }
 
 __BIONIC_FORTIFY_INLINE
-void* memset(void *s, int c, size_t n) {
+void* memset(void* _Nonnull s, int c, size_t n) {
     return __builtin___memset_chk(s, c, n, __bos0(s));
 }
 
 __BIONIC_FORTIFY_INLINE
-size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) {
-    size_t bos = __bos(dest);
+size_t strlcpy(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t size) {
+    size_t bos = __bos(dst);
 
 #if !defined(__clang__)
     // Compiler doesn't know destination size. Don't call __strlcpy_chk
     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
-        return __strlcpy_real(dest, src, size);
+        return __strlcpy_real(dst, src, size);
     }
 
     // Compiler can prove, at compile time, that the passed in size
     // is always <= the actual object size. Don't call __strlcpy_chk
     if (__builtin_constant_p(size) && (size <= bos)) {
-        return __strlcpy_real(dest, src, size);
+        return __strlcpy_real(dst, src, size);
     }
 #endif /* !defined(__clang__) */
 
-    return __strlcpy_chk(dest, src, size, bos);
+    return __strlcpy_chk(dst, src, size, bos);
 }
 
 
 __BIONIC_FORTIFY_INLINE
-size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) {
-    size_t bos = __bos(dest);
+size_t strlcat(char* _Nonnull __restrict dst, const char* _Nonnull __restrict src, size_t size) {
+    size_t bos = __bos(dst);
 
 #if !defined(__clang__)
     // Compiler doesn't know destination size. Don't call __strlcat_chk
     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
-        return __strlcat_real(dest, src, size);
+        return __strlcat_real(dst, src, size);
     }
 
     // Compiler can prove, at compile time, that the passed in size
     // is always <= the actual object size. Don't call __strlcat_chk
     if (__builtin_constant_p(size) && (size <= bos)) {
-        return __strlcat_real(dest, src, size);
+        return __strlcat_real(dst, src, size);
     }
 #endif /* !defined(__clang__) */
 
-    return __strlcat_chk(dest, src, size, bos);
+    return __strlcat_chk(dst, src, size, bos);
 }
 
 __BIONIC_FORTIFY_INLINE
-size_t strlen(const char *s) {
+size_t strlen(const char* _Nonnull s) {
     size_t bos = __bos(s);
 
 #if !defined(__clang__)
@@ -326,7 +328,7 @@
 }
 
 __BIONIC_FORTIFY_INLINE
-char* strchr(const char *s, int c) {
+char* strchr(const char* _Nonnull s, int c) {
     size_t bos = __bos(s);
 
 #if !defined(__clang__)
@@ -345,7 +347,7 @@
 }
 
 __BIONIC_FORTIFY_INLINE
-char* strrchr(const char *s, int c) {
+char* strrchr(const char* _Nonnull s, int c) {
     size_t bos = __bos(s);
 
 #if !defined(__clang__)
@@ -366,6 +368,10 @@
 
 #endif /* defined(__BIONIC_FORTIFY) */
 
+#if defined(__clang__)
+#pragma clang diagnostic pop
+#endif
+
 __END_DECLS
 
 #endif /* _STRING_H */
diff --git a/libc/include/sys/_system_properties.h b/libc/include/sys/_system_properties.h
index 3b1f7d0..60789d4 100644
--- a/libc/include/sys/_system_properties.h
+++ b/libc/include/sys/_system_properties.h
@@ -29,6 +29,8 @@
 #ifndef _INCLUDE_SYS__SYSTEM_PROPERTIES_H
 #define _INCLUDE_SYS__SYSTEM_PROPERTIES_H
 
+#include <sys/cdefs.h>
+
 #ifndef _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
 #error you should #include <sys/system_properties.h> instead
 #else
diff --git a/libc/include/sys/cachectl.h b/libc/include/sys/cachectl.h
index 0830293..5b20c19 100644
--- a/libc/include/sys/cachectl.h
+++ b/libc/include/sys/cachectl.h
@@ -25,10 +25,13 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _SYS_CACHECTL_H
 #define _SYS_CACHECTL_H 1
 
+#include <sys/cdefs.h>
 #ifdef __mips__
 #include <asm/cachectl.h>
 #endif
+
 #endif /* sys/cachectl.h */
diff --git a/libc/include/sys/capability.h b/libc/include/sys/capability.h
index 7718a6c..b9a40b3 100644
--- a/libc/include/sys/capability.h
+++ b/libc/include/sys/capability.h
@@ -34,8 +34,8 @@
 
 __BEGIN_DECLS
 
-extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
-extern int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
+int capget(cap_user_header_t hdrp, cap_user_data_t datap);
+int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
 
 __END_DECLS
 
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index 89061b6..4d1e34c 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -72,6 +72,12 @@
 #define	__static_cast(x,y)	(x)y
 #endif
 
+#if defined(__cplusplus)
+#define __BIONIC_CAST(_k,_t,_v) (_k<_t>(_v))
+#else
+#define __BIONIC_CAST(_k,_t,_v) ((_t) (_v))
+#endif
+
 /*
  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
@@ -88,9 +94,6 @@
 #define	__CONCAT(x,y)	x ## y
 #define	__STRING(x)	#x
 
-#define	__const		const		/* define reserved names to standard */
-#define	__signed	signed
-#define	__volatile	volatile
 #if defined(__cplusplus)
 #define	__inline	inline		/* convert to C++ keyword */
 #endif /* !__cplusplus */
@@ -102,17 +105,6 @@
 
 #endif	/* !(__STDC__ || __cplusplus) */
 
-/*
- * The following macro is used to remove const cast-away warnings
- * from gcc -Wcast-qual; it should be used with caution because it
- * can hide valid errors; in particular most valid uses are in
- * situations where the API requires it, not to cast away string
- * constants. We don't use *intptr_t on purpose here and we are
- * explicit about unsigned long so that we don't have additional
- * dependencies.
- */
-#define __UNCONST(a)	((void *)(unsigned long)(const void *)(a))
-
 #define __always_inline __attribute__((__always_inline__))
 #define __dead __attribute__((__noreturn__))
 #define __noreturn __attribute__((__noreturn__))
@@ -154,6 +146,7 @@
  */
 #if !(defined(__clang__) && __has_feature(nullability))
 #define _Nonnull
+#define _Nullable
 #endif
 
 #define __printflike(x, y) __attribute__((__format__(printf, x, y)))
@@ -198,8 +191,6 @@
 #define __errorattr(msg) __attribute__((__error__(msg)))
 #endif
 
-#define __warnattr(msg) __attribute__((__warning__(msg)))
-
 #define __errordecl(name, msg) extern void name(void) __errorattr(msg)
 
 /*
@@ -238,16 +229,6 @@
 #endif
 #endif
 
-/* C99 added the `restrict` type qualifier keyword. Before then, `__restrict` is a GNU extension. */
-#if __STDC_VERSION__ >= 199901L
-#define __restrict restrict
-#endif
-
-/* C99 added the `__func__` predefined identifier. */
-#if __STDC_VERSION__ < 199901L
-#define __func__ __PRETTY_FUNCTION__
-#endif
-
 #define  __BIONIC__   1
 #include <android/api-level.h>
 
@@ -274,9 +255,13 @@
 #    define __bos(s) __builtin_object_size((s), 0)
 #  endif
 #  define __bos0(s) __builtin_object_size((s), 0)
-#  define __BIONIC_FORTIFY_INLINE extern __inline__ __always_inline __attribute__((gnu_inline)) __attribute__((__artificial__))
+#  if defined(__clang__)
+#    define __BIONIC_FORTIFY_INLINE extern __inline__ __always_inline __attribute__((gnu_inline))
+#  else
+#    define __BIONIC_FORTIFY_INLINE extern __inline__ __always_inline __attribute__((gnu_inline)) __attribute__((__artificial__))
+#  endif
 #endif
-#define __BIONIC_FORTIFY_UNKNOWN_SIZE ((size_t) -1)
+#define __BIONIC_FORTIFY_UNKNOWN_SIZE __BIONIC_CAST(static_cast, size_t, -1)
 
 /* Used to tag non-static symbols that are private and never exposed by the shared library. */
 #define __LIBC_HIDDEN__ __attribute__((visibility("hidden")))
@@ -294,12 +279,6 @@
 /* Used to rename functions so that the compiler emits a call to 'x' rather than the function this was applied to. */
 #define __RENAME(x) __asm__(#x)
 
-#if __ANDROID_API__ < 21
-#define __BIONIC_LEGACY_INLINE static __inline
-#else
-#define __BIONIC_LEGACY_INLINE extern
-#endif
-
 #ifdef __clang__
 #define __AVAILABILITY(...) __attribute__((availability(android,__VA_ARGS__)))
 #else
@@ -353,4 +332,17 @@
 }
 #endif
 
+/*
+ * TODO(danalbert): Remove this once we've moved entirely off prebuilts/ndk.
+ *
+ * The NDK used to have a __NDK_FPABI__ that was defined to empty for most cases
+ * but `__attribute__((pcs("aapcs")))` for the now defunct armeabi-v7a-hard ABI.
+ *
+ * During the transition from prebuilts/ndk to ndk_headers, we'll have some
+ * headers that still use __NDK_FPABI__ while the libc headers have stopped
+ * defining it. In the interim, just provide an empty definition to keep the
+ * build working.
+ */
+#define __NDK_FPABI__
+
 #endif /* !_SYS_CDEFS_H_ */
diff --git a/libc/include/sys/eventfd.h b/libc/include/sys/eventfd.h
index 4c8da0c..6b9749a 100644
--- a/libc/include/sys/eventfd.h
+++ b/libc/include/sys/eventfd.h
@@ -40,10 +40,10 @@
 /* type of event counter */
 typedef uint64_t eventfd_t;
 
-extern int eventfd(unsigned int initial_value, int flags);
+int eventfd(unsigned int initial_value, int flags);
 
-extern int eventfd_read(int fd, eventfd_t* value);
-extern int eventfd_write(int fd, eventfd_t value);
+int eventfd_read(int fd, eventfd_t* value);
+int eventfd_write(int fd, eventfd_t value);
 
 __END_DECLS
 
diff --git a/libc/include/sys/fsuid.h b/libc/include/sys/fsuid.h
index 223db90..e4d9ebc 100644
--- a/libc/include/sys/fsuid.h
+++ b/libc/include/sys/fsuid.h
@@ -34,8 +34,8 @@
 
 __BEGIN_DECLS
 
-extern int setfsuid(uid_t) __INTRODUCED_IN(21);
-extern int setfsgid(gid_t) __INTRODUCED_IN(21);
+int setfsuid(uid_t) __INTRODUCED_IN(21);
+int setfsgid(gid_t) __INTRODUCED_IN(21);
 
 __END_DECLS
 
diff --git a/libc/include/sys/inotify.h b/libc/include/sys/inotify.h
index dcf0e1d..2e99144 100644
--- a/libc/include/sys/inotify.h
+++ b/libc/include/sys/inotify.h
@@ -40,10 +40,10 @@
 #define IN_CLOEXEC O_CLOEXEC
 #define IN_NONBLOCK O_NONBLOCK
 
-extern int inotify_init(void);
-extern int inotify_init1(int) __INTRODUCED_IN(21);
-extern int inotify_add_watch(int, const char*, uint32_t);
-extern int inotify_rm_watch(int, uint32_t);
+int inotify_init(void);
+int inotify_init1(int) __INTRODUCED_IN(21);
+int inotify_add_watch(int, const char*, uint32_t);
+int inotify_rm_watch(int, uint32_t);
 
 __END_DECLS
 
diff --git a/libc/include/sys/ipc.h b/libc/include/sys/ipc.h
index c0ae0ba..99bc5db 100644
--- a/libc/include/sys/ipc.h
+++ b/libc/include/sys/ipc.h
@@ -25,6 +25,7 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _SYS_IPC_H
 #define _SYS_IPC_H
 
@@ -34,7 +35,7 @@
 
 __BEGIN_DECLS
 
-extern key_t  ftok(const char*  path, int  id);
+key_t ftok(const char* path, int id);
 
 __END_DECLS
 
diff --git a/libc/include/sys/klog.h b/libc/include/sys/klog.h
index acfaa20..47eb3a4 100644
--- a/libc/include/sys/klog.h
+++ b/libc/include/sys/klog.h
@@ -46,7 +46,7 @@
 #define KLOG_SIZE_UNREAD   9
 #define KLOG_SIZE_BUFFER   10
 
-extern int klogctl(int, char *, int);
+int klogctl(int, char *, int);
 
 __END_DECLS
 
diff --git a/libc/include/sys/mman.h b/libc/include/sys/mman.h
index 7a8aa89..934963f 100644
--- a/libc/include/sys/mman.h
+++ b/libc/include/sys/mman.h
@@ -38,7 +38,7 @@
 #define MAP_ANON  MAP_ANONYMOUS
 #endif
 
-#define MAP_FAILED ((void *)-1)
+#define MAP_FAILED __BIONIC_CAST(reinterpret_cast, void*, -1)
 
 #define MREMAP_MAYMOVE  1
 #define MREMAP_FIXED    2
@@ -50,26 +50,26 @@
 #define POSIX_MADV_DONTNEED   MADV_DONTNEED
 
 #if defined(__USE_FILE_OFFSET64)
-extern void* mmap(void*, size_t, int, int, int, off_t) __RENAME(mmap64) __INTRODUCED_IN(21);
+void* mmap(void*, size_t, int, int, int, off_t) __RENAME(mmap64) __INTRODUCED_IN(21);
 #else
-extern void* mmap(void*, size_t, int, int, int, off_t);
+void* mmap(void*, size_t, int, int, int, off_t);
 #endif
-extern void* mmap64(void*, size_t, int, int, int, off64_t) __INTRODUCED_IN(21);
+void* mmap64(void*, size_t, int, int, int, off64_t) __INTRODUCED_IN(21);
 
-extern int munmap(void*, size_t);
-extern int msync(const void*, size_t, int);
-extern int mprotect(const void*, size_t, int);
-extern void* mremap(void*, size_t, size_t, int, ...);
+int munmap(void*, size_t);
+int msync(const void*, size_t, int);
+int mprotect(const void*, size_t, int);
+void* mremap(void*, size_t, size_t, int, ...);
 
-extern int mlockall(int) __INTRODUCED_IN(17);
-extern int munlockall(void) __INTRODUCED_IN(17);
-extern int mlock(const void*, size_t);
-extern int munlock(const void*, size_t);
+int mlockall(int) __INTRODUCED_IN(17);
+int munlockall(void) __INTRODUCED_IN(17);
+int mlock(const void*, size_t);
+int munlock(const void*, size_t);
 
-extern int mincore(void*, size_t, unsigned char*);
+int mincore(void*, size_t, unsigned char*);
 
-extern int madvise(void*, size_t, int);
-extern int posix_madvise(void*, size_t, int) __INTRODUCED_IN(23);
+int madvise(void*, size_t, int);
+int posix_madvise(void*, size_t, int) __INTRODUCED_IN(23);
 
 __END_DECLS
 
diff --git a/libc/include/sys/mount.h b/libc/include/sys/mount.h
index fd7cf17..26c0e0f 100644
--- a/libc/include/sys/mount.h
+++ b/libc/include/sys/mount.h
@@ -41,9 +41,9 @@
 #define MNT_EXPIRE 4
 #define UMOUNT_NOFOLLOW 8
 
-extern int mount(const char*, const char*, const char*, unsigned long, const void*);
-extern int umount(const char*);
-extern int umount2(const char*, int);
+int mount(const char*, const char*, const char*, unsigned long, const void*);
+int umount(const char*);
+int umount2(const char*, int);
 
 __END_DECLS
 
diff --git a/libc/include/sys/msg.h b/libc/include/sys/msg.h
index 1a6d30d..73965f2 100644
--- a/libc/include/sys/msg.h
+++ b/libc/include/sys/msg.h
@@ -29,6 +29,7 @@
 #ifndef _SYS_MSG_H_
 #define _SYS_MSG_H_
 
+#include <sys/cdefs.h>
 #include <linux/msg.h>
 
 #endif /* _SYS_MSG_H_ */
diff --git a/libc/include/sys/param.h b/libc/include/sys/param.h
index 5d2248f..d3686e0 100644
--- a/libc/include/sys/param.h
+++ b/libc/include/sys/param.h
@@ -25,11 +25,13 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _SYS_PARAM_H_
 #define _SYS_PARAM_H_
 
 #include <limits.h>
 #include <linux/param.h>
+#include <sys/cdefs.h>
 
 #define DEV_BSIZE 512
 
diff --git a/libc/include/sys/personality.h b/libc/include/sys/personality.h
index 15a5e68..6f3a3c9 100644
--- a/libc/include/sys/personality.h
+++ b/libc/include/sys/personality.h
@@ -34,7 +34,7 @@
 
 __BEGIN_DECLS
 
-extern int personality(unsigned int persona) __INTRODUCED_IN(15);
+int personality(unsigned int persona) __INTRODUCED_IN(15);
 
 __END_DECLS
 
diff --git a/libc/include/sys/prctl.h b/libc/include/sys/prctl.h
index d96b8b6..742ed57 100644
--- a/libc/include/sys/prctl.h
+++ b/libc/include/sys/prctl.h
@@ -35,7 +35,7 @@
 
 __BEGIN_DECLS
 
-extern int prctl(int option, ...);
+int prctl(int option, ...);
 
 __END_DECLS
 
diff --git a/libc/include/sys/queue.h b/libc/include/sys/queue.h
index b0e6b38..0890838 100644
--- a/libc/include/sys/queue.h
+++ b/libc/include/sys/queue.h
@@ -32,6 +32,8 @@
 #ifndef	_SYS_QUEUE_H_
 #define	_SYS_QUEUE_H_
 
+#include <sys/cdefs.h>
+
 /*
  * This file defines five types of data structures: singly-linked lists,
  * lists, simple queues, tail queues, and circular queues.
diff --git a/libc/include/sys/reboot.h b/libc/include/sys/reboot.h
index 7d202f7..df4739e 100644
--- a/libc/include/sys/reboot.h
+++ b/libc/include/sys/reboot.h
@@ -25,6 +25,7 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _SYS_REBOOT_H_
 #define _SYS_REBOOT_H_
 
@@ -41,7 +42,7 @@
 #define RB_DISABLE_CAD  LINUX_REBOOT_CMD_CAD_OFF
 #define RB_POWER_OFF    LINUX_REBOOT_CMD_POWER_OFF
 
-extern int reboot(int  reboot_type);
+int reboot(int reboot_type);
 
 __END_DECLS
 
diff --git a/libc/include/sys/reg.h b/libc/include/sys/reg.h
index b3d2aac..1066b6d 100644
--- a/libc/include/sys/reg.h
+++ b/libc/include/sys/reg.h
@@ -29,6 +29,8 @@
 #ifndef _SYS_REG_H_
 #define _SYS_REG_H_
 
+#include <sys/cdefs.h>
+
 #if defined(__i386__)
 
 #define EBX 0
diff --git a/libc/include/sys/resource.h b/libc/include/sys/resource.h
index 3187420..248310e 100644
--- a/libc/include/sys/resource.h
+++ b/libc/include/sys/resource.h
@@ -42,20 +42,20 @@
 
 typedef unsigned long rlim_t;
 
-extern int getrlimit(int, struct rlimit*);
-extern int setrlimit(int, const struct rlimit*);
+int getrlimit(int, struct rlimit*);
+int setrlimit(int, const struct rlimit*);
 
-extern int getrlimit64(int, struct rlimit64*) __INTRODUCED_IN(21);
-extern int setrlimit64(int, const struct rlimit64*) __INTRODUCED_IN(21);
+int getrlimit64(int, struct rlimit64*) __INTRODUCED_IN(21);
+int setrlimit64(int, const struct rlimit64*) __INTRODUCED_IN(21);
 
-extern int getpriority(int, int);
-extern int setpriority(int, int, int);
+int getpriority(int, int);
+int setpriority(int, int, int);
 
-extern int getrusage(int, struct rusage*);
+int getrusage(int, struct rusage*);
 
-extern int prlimit(pid_t, int, const struct rlimit*, struct rlimit*) __INTRODUCED_IN_32(24)
+int prlimit(pid_t, int, const struct rlimit*, struct rlimit*) __INTRODUCED_IN_32(24)
   __INTRODUCED_IN_64(21);
-extern int prlimit64(pid_t, int, const struct rlimit64*, struct rlimit64*) __INTRODUCED_IN(21);
+int prlimit64(pid_t, int, const struct rlimit64*, struct rlimit64*) __INTRODUCED_IN(21);
 
 __END_DECLS
 
diff --git a/libc/include/sys/select.h b/libc/include/sys/select.h
index 06c1c72..0f454b0 100644
--- a/libc/include/sys/select.h
+++ b/libc/include/sys/select.h
@@ -46,7 +46,7 @@
 
 #define __FDELT(fd) ((fd) / NFDBITS)
 #define __FDMASK(fd) (1UL << ((fd) % NFDBITS))
-#define __FDS_BITS(set) (((fd_set*)(set))->fds_bits)
+#define __FDS_BITS(set) (__BIONIC_CAST(static_cast, fd_set*, set)->fds_bits)
 
 /* Inline loop so we don't have to declare memset. */
 #define FD_ZERO(set) \
@@ -57,9 +57,9 @@
     } \
   } while (0)
 
-extern void __FD_CLR_chk(int, fd_set*, size_t) __INTRODUCED_IN(21);
-extern void __FD_SET_chk(int, fd_set*, size_t) __INTRODUCED_IN(21);
-extern int __FD_ISSET_chk(int, fd_set*, size_t) __INTRODUCED_IN(21);
+void __FD_CLR_chk(int, fd_set*, size_t) __INTRODUCED_IN(21);
+void __FD_SET_chk(int, fd_set*, size_t) __INTRODUCED_IN(21);
+int __FD_ISSET_chk(int, fd_set*, size_t) __INTRODUCED_IN(21);
 
 #if defined(__BIONIC_FORTIFY)
 #define FD_CLR(fd, set) __FD_CLR_chk(fd, set, __bos(set))
@@ -71,8 +71,8 @@
 #define FD_ISSET(fd, set) ((__FDS_BITS(set)[__FDELT(fd)] & __FDMASK(fd)) != 0)
 #endif /* defined(__BIONIC_FORTIFY) */
 
-extern int select(int, fd_set*, fd_set*, fd_set*, struct timeval*);
-extern int pselect(int, fd_set*, fd_set*, fd_set*, const struct timespec*, const sigset_t*);
+int select(int, fd_set*, fd_set*, fd_set*, struct timeval*);
+int pselect(int, fd_set*, fd_set*, fd_set*, const struct timespec*, const sigset_t*);
 
 __END_DECLS
 
diff --git a/libc/include/sys/sem.h b/libc/include/sys/sem.h
index a1ecd1f..753fc37 100644
--- a/libc/include/sys/sem.h
+++ b/libc/include/sys/sem.h
@@ -29,6 +29,7 @@
 #ifndef _SYS_SEM_H_
 #define _SYS_SEM_H_
 
+#include <sys/cdefs.h>
 #include <linux/sem.h>
 
 #endif /* _SYS_SEM_H_ */
diff --git a/libc/include/sys/sendfile.h b/libc/include/sys/sendfile.h
index f90498a..3ac8fdf 100644
--- a/libc/include/sys/sendfile.h
+++ b/libc/include/sys/sendfile.h
@@ -35,12 +35,12 @@
 __BEGIN_DECLS
 
 #if defined(__USE_FILE_OFFSET64)
-extern ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count) __RENAME(sendfile64)
+ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count) __RENAME(sendfile64)
   __INTRODUCED_IN(21);
 #else
-extern ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count);
+ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count);
 #endif
-extern ssize_t sendfile64(int out_fd, int in_fd, off64_t* offset, size_t count) __INTRODUCED_IN(21);
+ssize_t sendfile64(int out_fd, int in_fd, off64_t* offset, size_t count) __INTRODUCED_IN(21);
 
 __END_DECLS
 
diff --git a/libc/include/sys/signalfd.h b/libc/include/sys/signalfd.h
index 41036b0..a21c57b 100644
--- a/libc/include/sys/signalfd.h
+++ b/libc/include/sys/signalfd.h
@@ -35,7 +35,7 @@
 
 __BEGIN_DECLS
 
-extern int signalfd(int fd, const sigset_t* _Nonnull mask, int flags) __INTRODUCED_IN(18);
+int signalfd(int fd, const sigset_t* _Nonnull mask, int flags) __INTRODUCED_IN(18);
 
 __END_DECLS
 
diff --git a/libc/include/sys/socket.h b/libc/include/sys/socket.h
index 9736ba8..40b9d37 100644
--- a/libc/include/sys/socket.h
+++ b/libc/include/sys/socket.h
@@ -287,16 +287,16 @@
 __socketcall int socket(int, int, int);
 __socketcall int socketpair(int, int, int, int*);
 
-extern ssize_t send(int, const void*, size_t, int);
-extern ssize_t recv(int, void*, size_t, int);
+ssize_t send(int, const void*, size_t, int);
+ssize_t recv(int, void*, size_t, int);
 
 __socketcall ssize_t sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t);
 __socketcall ssize_t recvfrom(int, void*, size_t, int, const struct sockaddr*, socklen_t*);
 
 __errordecl(__recvfrom_error, "recvfrom called with size bigger than buffer");
-extern ssize_t __recvfrom_chk(int, void*, size_t, size_t, int, const struct sockaddr*, socklen_t*)
+ssize_t __recvfrom_chk(int, void*, size_t, size_t, int, const struct sockaddr*, socklen_t*)
   __INTRODUCED_IN(21);
-extern ssize_t __recvfrom_real(int, void*, size_t, int, const struct sockaddr*, socklen_t*) __RENAME(recvfrom);
+ssize_t __recvfrom_real(int, void*, size_t, int, const struct sockaddr*, socklen_t*) __RENAME(recvfrom);
 
 #if defined(__BIONIC_FORTIFY)
 
diff --git a/libc/include/sys/stat.h b/libc/include/sys/stat.h
index 0a999e4..4397a0d 100644
--- a/libc/include/sys/stat.h
+++ b/libc/include/sys/stat.h
@@ -141,24 +141,24 @@
 #define S_IEXEC S_IXUSR
 #endif
 
-extern int chmod(const char*, mode_t);
-extern int fchmod(int, mode_t);
-extern int mkdir(const char*, mode_t);
+int chmod(const char*, mode_t);
+int fchmod(int, mode_t);
+int mkdir(const char*, mode_t);
 
-extern int fstat(int, struct stat*);
-extern int fstat64(int, struct stat64*) __INTRODUCED_IN(21);
-extern int fstatat(int, const char*, struct stat*, int);
-extern int fstatat64(int, const char*, struct stat64*, int) __INTRODUCED_IN(21);
-extern int lstat(const char*, struct stat*);
-extern int lstat64(const char*, struct stat64*) __INTRODUCED_IN(21);
-extern int stat(const char*, struct stat*);
-extern int stat64(const char*, struct stat64*) __INTRODUCED_IN(21);
+int fstat(int, struct stat*);
+int fstat64(int, struct stat64*) __INTRODUCED_IN(21);
+int fstatat(int, const char*, struct stat*, int);
+int fstatat64(int, const char*, struct stat64*, int) __INTRODUCED_IN(21);
+int lstat(const char*, struct stat*);
+int lstat64(const char*, struct stat64*) __INTRODUCED_IN(21);
+int stat(const char*, struct stat*);
+int stat64(const char*, struct stat64*) __INTRODUCED_IN(21);
 
-extern int mknod(const char*, mode_t, dev_t);
-extern mode_t umask(mode_t);
+int mknod(const char*, mode_t, dev_t);
+mode_t umask(mode_t);
 
-extern mode_t __umask_chk(mode_t) __INTRODUCED_IN(18);
-extern mode_t __umask_real(mode_t) __RENAME(umask);
+mode_t __umask_chk(mode_t) __INTRODUCED_IN(18);
+mode_t __umask_real(mode_t) __RENAME(umask);
 __errordecl(__umask_invalid_mode, "umask called with invalid mode");
 
 #if defined(__BIONIC_FORTIFY)
@@ -177,18 +177,23 @@
 }
 #endif /* defined(__BIONIC_FORTIFY) */
 
-__BIONIC_LEGACY_INLINE int mkfifo(const char*, mode_t);
-extern int mkfifoat(int, const char*, mode_t) __INTRODUCED_IN(23);
+#if __ANDROID_API__ >= 21
+int mkfifo(const char*, mode_t);
+#else
+// Implemented as a static inline before 21.
+#endif
 
-extern int fchmodat(int, const char*, mode_t, int);
-extern int mkdirat(int, const char*, mode_t);
-extern int mknodat(int, const char*, mode_t, dev_t) __INTRODUCED_IN(21);
+int mkfifoat(int, const char*, mode_t) __INTRODUCED_IN(23);
+
+int fchmodat(int, const char*, mode_t, int);
+int mkdirat(int, const char*, mode_t);
+int mknodat(int, const char*, mode_t, dev_t) __INTRODUCED_IN(21);
 
 #define UTIME_NOW  ((1L << 30) - 1L)
 #define UTIME_OMIT ((1L << 30) - 2L)
-extern int utimensat(int fd, const char* path, const struct timespec times[2], int flags)
+int utimensat(int fd, const char* path, const struct timespec times[2], int flags)
   __INTRODUCED_IN(12);
-extern int futimens(int fd, const struct timespec times[2]) __INTRODUCED_IN(19);
+int futimens(int fd, const struct timespec times[2]) __INTRODUCED_IN(19);
 
 __END_DECLS
 
diff --git a/libc/include/sys/statvfs.h b/libc/include/sys/statvfs.h
index f6b9803..af1b9c0 100644
--- a/libc/include/sys/statvfs.h
+++ b/libc/include/sys/statvfs.h
@@ -59,12 +59,12 @@
 #define ST_NODIRATIME  0x0800
 #define ST_RELATIME    0x1000
 
-extern int statvfs(const char* __restrict _Nonnull, struct statvfs* __restrict _Nonnull)
+int statvfs(const char* __restrict _Nonnull, struct statvfs* __restrict _Nonnull)
   __INTRODUCED_IN(19);
-extern int statvfs64(const char* __restrict _Nonnull, struct statvfs64* __restrict _Nonnull)
+int statvfs64(const char* __restrict _Nonnull, struct statvfs64* __restrict _Nonnull)
   __INTRODUCED_IN(21);
-extern int fstatvfs(int, struct statvfs* _Nonnull) __INTRODUCED_IN(19);
-extern int fstatvfs64(int, struct statvfs64* _Nonnull) __INTRODUCED_IN(21);
+int fstatvfs(int, struct statvfs* _Nonnull) __INTRODUCED_IN(19);
+int fstatvfs64(int, struct statvfs64* _Nonnull) __INTRODUCED_IN(21);
 
 __END_DECLS
 
diff --git a/libc/include/sys/swap.h b/libc/include/sys/swap.h
index 2d99df8..3444736 100644
--- a/libc/include/sys/swap.h
+++ b/libc/include/sys/swap.h
@@ -38,8 +38,8 @@
 #define SWAP_FLAG_PRIO_MASK 0x7fff
 #define SWAP_FLAG_PRIO_SHIFT 0
 
-extern int swapon(const char* _Nonnull, int) __INTRODUCED_IN(19);
-extern int swapoff(const char* _Nonnull) __INTRODUCED_IN(19);
+int swapon(const char* _Nonnull, int) __INTRODUCED_IN(19);
+int swapoff(const char* _Nonnull) __INTRODUCED_IN(19);
 
 __END_DECLS
 
diff --git a/libc/include/sys/syscall.h b/libc/include/sys/syscall.h
index 87cddc9..a49323d 100644
--- a/libc/include/sys/syscall.h
+++ b/libc/include/sys/syscall.h
@@ -31,6 +31,7 @@
 
 #include <asm/unistd.h> /* Linux kernel __NR_* names. */
 #include <bits/glibc-syscalls.h> /* glibc-compatible SYS_* aliases. */
+#include <sys/cdefs.h>
 
 /* The syscall function itself is declared in <unistd.h>, not here. */
 
diff --git a/libc/include/sys/sysconf.h b/libc/include/sys/sysconf.h
index c9895f2..eb30faf 100644
--- a/libc/include/sys/sysconf.h
+++ b/libc/include/sys/sysconf.h
@@ -1,33 +1,6 @@
 /*
- * Copyright (C) 2008 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.
+ * This file used to contain the declarations of sysconf and its associated constants.
+ * No standard mentions a <sys/sysconf.h>, but there are enough users in vendor (and potential ones
+ * in the NDK) to warrant not breaking source compatibility.
  */
-
-// This file used to contain the declarations of sysconf and its associated constants.
-// No standard mentions a <sys/sysconf.h>, but there are enough users in vendor (and potential ones
-// in the NDK) to warrant not breaking source compatibility.
-
 #include <bits/sysconf.h>
diff --git a/libc/include/sys/sysmacros.h b/libc/include/sys/sysmacros.h
index 54e43dd..592cc5e 100644
--- a/libc/include/sys/sysmacros.h
+++ b/libc/include/sys/sysmacros.h
@@ -29,6 +29,8 @@
 #ifndef _SYS_SYSMACROS_H_
 #define _SYS_SYSMACROS_H_
 
+#include <sys/cdefs.h>
+
 #define makedev(__major, __minor) \
   ( \
     (((__major) & 0xfffff000ULL) << 32) | (((__major) & 0xfffULL) << 8) | \
diff --git a/libc/include/sys/timerfd.h b/libc/include/sys/timerfd.h
index 8311f08..a500060 100644
--- a/libc/include/sys/timerfd.h
+++ b/libc/include/sys/timerfd.h
@@ -31,6 +31,7 @@
 
 #include <fcntl.h> /* For O_CLOEXEC and O_NONBLOCK. */
 #include <time.h>
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 __BEGIN_DECLS
@@ -41,10 +42,9 @@
 #define TFD_CLOEXEC O_CLOEXEC
 #define TFD_NONBLOCK O_NONBLOCK
 
-extern int timerfd_create(clockid_t, int) __INTRODUCED_IN(19);
-extern int timerfd_settime(int, int, const struct itimerspec*, struct itimerspec*)
-  __INTRODUCED_IN(19);
-extern int timerfd_gettime(int, struct itimerspec*) __INTRODUCED_IN(19);
+int timerfd_create(clockid_t, int) __INTRODUCED_IN(19);
+int timerfd_settime(int, int, const struct itimerspec*, struct itimerspec*) __INTRODUCED_IN(19);
+int timerfd_gettime(int, struct itimerspec*) __INTRODUCED_IN(19);
 
 __END_DECLS
 
diff --git a/libc/include/sys/times.h b/libc/include/sys/times.h
index 6ce5b55..f52db1f 100644
--- a/libc/include/sys/times.h
+++ b/libc/include/sys/times.h
@@ -35,7 +35,7 @@
 
 __BEGIN_DECLS
 
-extern clock_t times(struct tms*);
+clock_t times(struct tms*);
 
 __END_DECLS
 
diff --git a/libc/include/sys/ttydefaults.h b/libc/include/sys/ttydefaults.h
index b62aab1..62af84b 100644
--- a/libc/include/sys/ttydefaults.h
+++ b/libc/include/sys/ttydefaults.h
@@ -42,6 +42,8 @@
 #ifndef _SYS_TTYDEFAULTS_H_
 #define	_SYS_TTYDEFAULTS_H_
 
+#include <sys/cdefs.h>
+
 /*
  * Defaults on "first" open.
  */
diff --git a/libc/include/sys/ucontext.h b/libc/include/sys/ucontext.h
index f252a37..fdb3015 100644
--- a/libc/include/sys/ucontext.h
+++ b/libc/include/sys/ucontext.h
@@ -30,6 +30,7 @@
 #define _SYS_UCONTEXT_H_
 
 #include <signal.h>
+#include <sys/cdefs.h>
 #include <sys/user.h>
 
 __BEGIN_DECLS
diff --git a/libc/include/sys/un.h b/libc/include/sys/un.h
index 421b900..3e1c429 100644
--- a/libc/include/sys/un.h
+++ b/libc/include/sys/un.h
@@ -31,5 +31,6 @@
 
 #include <bits/sa_family_t.h>
 #include <linux/un.h>
+#include <sys/cdefs.h>
 
 #endif
diff --git a/libc/include/sys/utsname.h b/libc/include/sys/utsname.h
index d54a994..b3856a7 100644
--- a/libc/include/sys/utsname.h
+++ b/libc/include/sys/utsname.h
@@ -25,6 +25,7 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _SYS_UTSNAME_H_
 #define _SYS_UTSNAME_H_
 
@@ -43,7 +44,7 @@
     char  domainname[SYS_NMLN];
 };
 
-extern int uname(struct utsname *);
+int uname(struct utsname*);
 
 __END_DECLS
 
diff --git a/libc/include/sys/vfs.h b/libc/include/sys/vfs.h
index bf094dc..1231eb1 100644
--- a/libc/include/sys/vfs.h
+++ b/libc/include/sys/vfs.h
@@ -137,10 +137,10 @@
 #define XENIX_SUPER_MAGIC     0x012FF7B4
 #define XFS_SUPER_MAGIC       0x58465342
 
-extern int statfs(const char* _Nonnull, struct statfs* _Nonnull);
-extern int statfs64(const char* _Nonnull, struct statfs64* _Nonnull) __INTRODUCED_IN(21);
-extern int fstatfs(int, struct statfs* _Nonnull);
-extern int fstatfs64(int, struct statfs64* _Nonnull) __INTRODUCED_IN(21);
+int statfs(const char* _Nonnull, struct statfs* _Nonnull);
+int statfs64(const char* _Nonnull, struct statfs64* _Nonnull) __INTRODUCED_IN(21);
+int fstatfs(int, struct statfs* _Nonnull);
+int fstatfs64(int, struct statfs64* _Nonnull) __INTRODUCED_IN(21);
 
 __END_DECLS
 
diff --git a/libc/include/sys/wait.h b/libc/include/sys/wait.h
index e0afabc..90eb35b 100644
--- a/libc/include/sys/wait.h
+++ b/libc/include/sys/wait.h
@@ -49,9 +49,9 @@
 #define W_EXITCODE(ret, sig)    ((ret) << 8 | (sig))
 #define W_STOPCODE(sig)         ((sig) << 8 | 0x7f)
 
-extern pid_t  wait(int *);
-extern pid_t  waitpid(pid_t, int *, int);
-extern pid_t wait4(pid_t, int*, int, struct rusage*) __INTRODUCED_IN(18);
+pid_t wait(int*);
+pid_t waitpid(pid_t, int*, int);
+pid_t wait4(pid_t, int*, int, struct rusage*) __INTRODUCED_IN(18);
 
 /* Posix states that idtype_t should be an enumeration type, but
  * the kernel headers define P_ALL, P_PID and P_PGID as constant macros
@@ -59,7 +59,7 @@
  */
 typedef int idtype_t;
 
-extern int  waitid(idtype_t which, id_t id, siginfo_t *info, int options);
+int waitid(idtype_t which, id_t id, siginfo_t* info, int options);
 
 __END_DECLS
 
diff --git a/libc/include/sys/xattr.h b/libc/include/sys/xattr.h
index 63605c5..37e352d 100644
--- a/libc/include/sys/xattr.h
+++ b/libc/include/sys/xattr.h
@@ -25,36 +25,34 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _SYS_XATTR_H_
 #define _SYS_XATTR_H_
 
+#include <linux/xattr.h>
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 __BEGIN_DECLS
 
-#define XATTR_CREATE 1
-#define XATTR_REPLACE 2
-
-extern int fsetxattr(int fd, const char* name, const void* value, size_t size, int flags)
+int fsetxattr(int fd, const char* name, const void* value, size_t size, int flags)
   __INTRODUCED_IN(16);
-extern int setxattr(const char* path, const char* name, const void* value, size_t size, int flags)
+int setxattr(const char* path, const char* name, const void* value, size_t size, int flags)
   __INTRODUCED_IN(16);
-extern int lsetxattr(const char* path, const char* name, const void* value, size_t size, int flags)
+int lsetxattr(const char* path, const char* name, const void* value, size_t size, int flags)
   __INTRODUCED_IN(16);
 
-extern ssize_t fgetxattr(int fd, const char* name, void* value, size_t size) __INTRODUCED_IN(16);
-extern ssize_t getxattr(const char* path, const char* name, void* value, size_t size)
-  __INTRODUCED_IN(16);
-extern ssize_t lgetxattr(const char* path, const char* name, void* value, size_t size)
-  __INTRODUCED_IN(16);
+ssize_t fgetxattr(int fd, const char* name, void* value, size_t size) __INTRODUCED_IN(16);
+ssize_t getxattr(const char* path, const char* name, void* value, size_t size) __INTRODUCED_IN(16);
+ssize_t lgetxattr(const char* path, const char* name, void* value, size_t size) __INTRODUCED_IN(16);
 
-extern ssize_t listxattr(const char* path, char* list, size_t size) __INTRODUCED_IN(16);
-extern ssize_t llistxattr(const char* path, char* list, size_t size) __INTRODUCED_IN(16);
-extern ssize_t flistxattr(int fd, char* list, size_t size) __INTRODUCED_IN(16);
+ssize_t listxattr(const char* path, char* list, size_t size) __INTRODUCED_IN(16);
+ssize_t llistxattr(const char* path, char* list, size_t size) __INTRODUCED_IN(16);
+ssize_t flistxattr(int fd, char* list, size_t size) __INTRODUCED_IN(16);
 
-extern int removexattr(const char* path, const char* name) __INTRODUCED_IN(16);
-extern int lremovexattr(const char* path, const char* name) __INTRODUCED_IN(16);
-extern int fremovexattr(int fd, const char* name) __INTRODUCED_IN(16);
+int removexattr(const char* path, const char* name) __INTRODUCED_IN(16);
+int lremovexattr(const char* path, const char* name) __INTRODUCED_IN(16);
+int fremovexattr(int fd, const char* name) __INTRODUCED_IN(16);
 
 __END_DECLS
 
diff --git a/libc/include/sysexits.h b/libc/include/sysexits.h
index e244836..97f5366 100644
--- a/libc/include/sysexits.h
+++ b/libc/include/sysexits.h
@@ -35,6 +35,8 @@
 #ifndef	_SYSEXITS_H_
 #define	_SYSEXITS_H_
 
+#include <sys/cdefs.h>
+
 /*
  *  SYSEXITS.H -- Exit status codes for system programs.
  *
diff --git a/libc/include/syslog.h b/libc/include/syslog.h
index d51c9f2..ee288e5 100644
--- a/libc/include/syslog.h
+++ b/libc/include/syslog.h
@@ -86,7 +86,7 @@
 #define LOG_PERROR 0x20
 
 void closelog(void);
-void openlog(const char*, int, int);
+void openlog(const char* _Nullable, int, int);
 int setlogmask(int);
 void syslog(int, const char* _Nonnull, ...) __printflike(2, 3);
 void vsyslog(int, const char* _Nonnull, va_list) __printflike(2, 0);
diff --git a/libc/include/termio.h b/libc/include/termio.h
index 99d3630..9e26956 100644
--- a/libc/include/termio.h
+++ b/libc/include/termio.h
@@ -1,32 +1 @@
-/*
- * Copyright (C) 2010 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.
- */
-
-/* All definitions related to termio are in Linux kernel headers
- * that are already included by <termios.h>
- */
 #include <termios.h>
diff --git a/libc/include/termios.h b/libc/include/termios.h
index 15a7b32..ad33bbe 100644
--- a/libc/include/termios.h
+++ b/libc/include/termios.h
@@ -35,19 +35,23 @@
 
 __BEGIN_DECLS
 
-__BIONIC_LEGACY_INLINE speed_t cfgetispeed(const struct termios*);
-__BIONIC_LEGACY_INLINE speed_t cfgetospeed(const struct termios*);
-__BIONIC_LEGACY_INLINE void cfmakeraw(struct termios*);
-__BIONIC_LEGACY_INLINE int cfsetispeed(struct termios*, speed_t);
-__BIONIC_LEGACY_INLINE int cfsetospeed(struct termios*, speed_t);
-__BIONIC_LEGACY_INLINE int cfsetspeed(struct termios*, speed_t) __INTRODUCED_IN(21);
-__BIONIC_LEGACY_INLINE int tcdrain(int) __INTRODUCED_IN(21);
-__BIONIC_LEGACY_INLINE int tcflow(int, int);
-__BIONIC_LEGACY_INLINE int tcflush(int, int);
-__BIONIC_LEGACY_INLINE int tcgetattr(int, struct termios*);
-__BIONIC_LEGACY_INLINE pid_t tcgetsid(int);
-__BIONIC_LEGACY_INLINE int tcsendbreak(int, int);
-__BIONIC_LEGACY_INLINE int tcsetattr(int, int, const struct termios*);
+#if __ANDROID_API__ >= 21
+// Implemented as static inlines before 21.
+speed_t cfgetispeed(const struct termios*);
+speed_t cfgetospeed(const struct termios*);
+void cfmakeraw(struct termios*);
+int cfsetispeed(struct termios*, speed_t);
+int cfsetospeed(struct termios*, speed_t);
+int tcflow(int, int);
+int tcflush(int, int);
+int tcgetattr(int, struct termios*);
+pid_t tcgetsid(int);
+int tcsendbreak(int, int);
+int tcsetattr(int, int, const struct termios*);
+#endif
+
+int cfsetspeed(struct termios*, speed_t) __INTRODUCED_IN(21);
+int tcdrain(int) __INTRODUCED_IN(21);
 
 __END_DECLS
 
diff --git a/libc/include/time.h b/libc/include/time.h
index ca4c708..888ce12 100644
--- a/libc/include/time.h
+++ b/libc/include/time.h
@@ -59,48 +59,48 @@
 
 #define TM_ZONE tm_zone
 
-extern time_t time(time_t*);
-extern int nanosleep(const struct timespec*, struct timespec*);
+time_t time(time_t*);
+int nanosleep(const struct timespec*, struct timespec*);
 
-extern char* asctime(const struct tm*);
-extern char* asctime_r(const struct tm*, char*);
+char* asctime(const struct tm*);
+char* asctime_r(const struct tm*, char*);
 
-extern double difftime(time_t, time_t);
-extern time_t mktime(struct tm*);
+double difftime(time_t, time_t);
+time_t mktime(struct tm*);
 
-extern struct tm* localtime(const time_t*);
-extern struct tm* localtime_r(const time_t*, struct tm*);
+struct tm* localtime(const time_t*);
+struct tm* localtime_r(const time_t*, struct tm*);
 
-extern struct tm* gmtime(const time_t*);
-extern struct tm* gmtime_r(const time_t*, struct tm*);
+struct tm* gmtime(const time_t*);
+struct tm* gmtime_r(const time_t*, struct tm*);
 
-extern char* strptime(const char*, const char*, struct tm*);
-extern size_t strftime(char*, size_t, const char*, const struct tm*);
-extern size_t strftime_l(char*, size_t, const char*, const struct tm*, locale_t) __INTRODUCED_IN(21);
+char* strptime(const char*, const char*, struct tm*);
+size_t strftime(char*, size_t, const char*, const struct tm*);
+size_t strftime_l(char*, size_t, const char*, const struct tm*, locale_t) __INTRODUCED_IN(21);
 
-extern char* ctime(const time_t*);
-extern char* ctime_r(const time_t*, char*);
+char* ctime(const time_t*);
+char* ctime_r(const time_t*, char*);
 
-extern void tzset(void);
+void tzset(void);
 
-extern clock_t clock(void);
+clock_t clock(void);
 
-extern int clock_getcpuclockid(pid_t, clockid_t*) __INTRODUCED_IN(23);
+int clock_getcpuclockid(pid_t, clockid_t*) __INTRODUCED_IN(23);
 
-extern int clock_getres(clockid_t, struct timespec*);
-extern int clock_gettime(clockid_t, struct timespec*);
-extern int clock_nanosleep(clockid_t, int, const struct timespec*, struct timespec*);
-extern int clock_settime(clockid_t, const struct timespec*);
+int clock_getres(clockid_t, struct timespec*);
+int clock_gettime(clockid_t, struct timespec*);
+int clock_nanosleep(clockid_t, int, const struct timespec*, struct timespec*);
+int clock_settime(clockid_t, const struct timespec*);
 
-extern int timer_create(int, struct sigevent*, timer_t*);
-extern int timer_delete(timer_t);
-extern int timer_settime(timer_t, int, const struct itimerspec*, struct itimerspec*);
-extern int timer_gettime(timer_t, struct itimerspec*);
-extern int timer_getoverrun(timer_t);
+int timer_create(int, struct sigevent*, timer_t*);
+int timer_delete(timer_t);
+int timer_settime(timer_t, int, const struct itimerspec*, struct itimerspec*);
+int timer_gettime(timer_t, struct itimerspec*);
+int timer_getoverrun(timer_t);
 
 /* Non-standard extensions that are in the BSDs and glibc. */
-extern time_t timelocal(struct tm*) __INTRODUCED_IN(12);
-extern time_t timegm(struct tm*) __INTRODUCED_IN(12);
+time_t timelocal(struct tm*) __INTRODUCED_IN(12);
+time_t timegm(struct tm*) __INTRODUCED_IN(12);
 
 __END_DECLS
 
diff --git a/libc/include/ucontext.h b/libc/include/ucontext.h
index 5ea2982..73c2166 100644
--- a/libc/include/ucontext.h
+++ b/libc/include/ucontext.h
@@ -1,34 +1 @@
-/*
- * Copyright (C) 2014 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 _UCONTEXT_H_
-#define _UCONTEXT_H_
-
 #include <sys/ucontext.h>
-
-#endif /* _UCONTEXT_H_ */
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 7deca95..25f0304 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -79,139 +79,134 @@
 
 extern char** environ;
 
-extern __noreturn void _exit(int __status);
+__noreturn void _exit(int __status);
 
-extern pid_t  fork(void);
-extern pid_t  vfork(void);
-extern pid_t  getpid(void);
-extern pid_t  gettid(void) __pure2;
-extern pid_t  getpgid(pid_t __pid);
-extern int    setpgid(pid_t __pid, pid_t __pgid);
-extern pid_t  getppid(void);
-extern pid_t  getpgrp(void);
-extern int    setpgrp(void);
-extern pid_t  getsid(pid_t __pid) __INTRODUCED_IN(17);
-extern pid_t  setsid(void);
+pid_t  fork(void);
+pid_t  vfork(void);
+pid_t  getpid(void);
+pid_t  gettid(void) __pure2;
+pid_t  getpgid(pid_t __pid);
+int    setpgid(pid_t __pid, pid_t __pgid);
+pid_t  getppid(void);
+pid_t  getpgrp(void);
+int    setpgrp(void);
+pid_t  getsid(pid_t __pid) __INTRODUCED_IN(17);
+pid_t  setsid(void);
 
-extern int execv(const char* __path, char* const* __argv);
-extern int execvp(const char* __file, char* const* __argv);
-extern int execvpe(const char* __file, char* const* __argv, char* const* __envp)
-  __INTRODUCED_IN(21);
-extern int execve(const char* __file, char* const* __argv, char* const* __envp);
-extern int execl(const char* __path, const char* __arg0, ...);
-extern int execlp(const char* __file, const char* __arg0, ...);
-extern int execle(const char* __path, const char* __arg0, ...);
+int execv(const char* __path, char* const* __argv);
+int execvp(const char* __file, char* const* __argv);
+int execvpe(const char* __file, char* const* __argv, char* const* __envp) __INTRODUCED_IN(21);
+int execve(const char* __file, char* const* __argv, char* const* __envp);
+int execl(const char* __path, const char* __arg0, ...);
+int execlp(const char* __file, const char* __arg0, ...);
+int execle(const char* __path, const char* __arg0, ...);
 
-extern int nice(int __incr);
+int nice(int __incr);
 
-extern int setuid(uid_t __uid);
-extern uid_t getuid(void);
-extern int seteuid(uid_t __uid);
-extern uid_t geteuid(void);
-extern int setgid(gid_t __gid);
-extern gid_t getgid(void);
-extern int setegid(gid_t __gid);
-extern gid_t getegid(void);
-extern int getgroups(int __size, gid_t* __list);
-extern int setgroups(size_t __size, const gid_t* __list);
-extern int setreuid(uid_t __ruid, uid_t __euid);
-extern int setregid(gid_t __rgid, gid_t __egid);
-extern int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
-extern int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
-extern int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
-extern int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
-extern char* getlogin(void);
+int setuid(uid_t __uid);
+uid_t getuid(void);
+int seteuid(uid_t __uid);
+uid_t geteuid(void);
+int setgid(gid_t __gid);
+gid_t getgid(void);
+int setegid(gid_t __gid);
+gid_t getegid(void);
+int getgroups(int __size, gid_t* __list);
+int setgroups(size_t __size, const gid_t* __list);
+int setreuid(uid_t __ruid, uid_t __euid);
+int setregid(gid_t __rgid, gid_t __egid);
+int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
+int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
+int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
+int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
+char* getlogin(void);
 
-extern long fpathconf(int __fd, int __name);
-extern long pathconf(const char* __path, int __name);
+long fpathconf(int __fd, int __name);
+long pathconf(const char* __path, int __name);
 
-extern int access(const char* __path, int __mode);
-extern int faccessat(int __dirfd, const char* __path, int __mode, int __flags)
-  __INTRODUCED_IN(16);
-extern int link(const char* __oldpath, const char* __newpath);
-extern int linkat(int __olddirfd, const char* __oldpath, int __newdirfd,
-                  const char* __newpath, int __flags) __INTRODUCED_IN(21);
-extern int unlink(const char* __path);
-extern int unlinkat(int __dirfd, const char* __path, int __flags);
-extern int chdir(const char* __path);
-extern int fchdir(int __fd);
-extern int rmdir(const char* __path);
-extern int pipe(int* __pipefd);
+int access(const char* __path, int __mode);
+int faccessat(int __dirfd, const char* __path, int __mode, int __flags) __INTRODUCED_IN(16);
+int link(const char* __oldpath, const char* __newpath);
+int linkat(int __olddirfd, const char* __oldpath, int __newdirfd,
+           const char* __newpath, int __flags) __INTRODUCED_IN(21);
+int unlink(const char* __path);
+int unlinkat(int __dirfd, const char* __path, int __flags);
+int chdir(const char* __path);
+int fchdir(int __fd);
+int rmdir(const char* __path);
+int pipe(int* __pipefd);
 #if defined(__USE_GNU)
-extern int pipe2(int* __pipefd, int __flags) __INTRODUCED_IN(9);
+int pipe2(int* __pipefd, int __flags) __INTRODUCED_IN(9);
 #endif
-extern int chroot(const char* __path);
-extern int symlink(const char* __oldpath, const char* __newpath);
-extern int symlinkat(const char* __oldpath, int __newdirfd,
-                     const char* __newpath) __INTRODUCED_IN(21);
-extern ssize_t readlink(const char* __path, char* __buf, size_t __bufsiz);
-extern ssize_t readlinkat(int __dirfd, const char* __path, char* __buf,
-                          size_t __bufsiz) __INTRODUCED_IN(21);
-extern int chown(const char* __path, uid_t __owner, gid_t __group);
-extern int fchown(int __fd, uid_t __owner, gid_t __group);
-extern int fchownat(int __dirfd, const char* __path, uid_t __owner,
-                    gid_t __group, int __flags);
-extern int lchown(const char* __path, uid_t __owner, gid_t __group);
-extern char* getcwd(char* __buf, size_t __size);
+int chroot(const char* __path);
+int symlink(const char* __oldpath, const char* __newpath);
+int symlinkat(const char* __oldpath, int __newdirfd, const char* __newpath) __INTRODUCED_IN(21);
+ssize_t readlink(const char* __path, char* __buf, size_t __bufsiz);
+ssize_t readlinkat(int __dirfd, const char* __path, char* __buf,
+                   size_t __bufsiz) __INTRODUCED_IN(21);
+int chown(const char* __path, uid_t __owner, gid_t __group);
+int fchown(int __fd, uid_t __owner, gid_t __group);
+int fchownat(int __dirfd, const char* __path, uid_t __owner, gid_t __group, int __flags);
+int lchown(const char* __path, uid_t __owner, gid_t __group);
+char* getcwd(char* __buf, size_t __size);
 
-extern int sync(void);
+int sync(void);
 
-extern int close(int __fd);
+int close(int __fd);
 
-extern ssize_t read(int __fd, void* __buf, size_t __count);
-extern ssize_t write(int __fd, const void* __buf, size_t __count);
+ssize_t read(int __fd, void* __buf, size_t __count);
+ssize_t write(int __fd, const void* __buf, size_t __count);
 
-extern int dup(int __oldfd);
-extern int dup2(int __oldfd, int __newfd);
-extern int dup3(int __oldfd, int __newfd, int __flags) __INTRODUCED_IN(21);
-extern int fsync(int __fd);
-extern int fdatasync(int __fd) __INTRODUCED_IN(9);
+int dup(int __oldfd);
+int dup2(int __oldfd, int __newfd);
+int dup3(int __oldfd, int __newfd, int __flags) __INTRODUCED_IN(21);
+int fsync(int __fd);
+int fdatasync(int __fd) __INTRODUCED_IN(9);
 
 #if defined(__USE_FILE_OFFSET64)
-extern off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
+off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
 #else
-extern off_t lseek(int __fd, off_t __offset, int __whence);
+off_t lseek(int __fd, off_t __offset, int __whence);
 #endif
 
-extern off64_t lseek64(int __fd, off64_t __offset, int __whence);
+off64_t lseek64(int __fd, off64_t __offset, int __whence);
 
 #if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ >= 21
-extern int truncate(const char* __path, off_t __length) __RENAME(truncate64) __INTRODUCED_IN(21);
-extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset) __RENAME(pread64)
+int truncate(const char* __path, off_t __length) __RENAME(truncate64) __INTRODUCED_IN(21);
+ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset) __RENAME(pread64)
   __INTRODUCED_IN(12);
-extern ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset)
+ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset)
   __RENAME(pwrite64) __INTRODUCED_IN(12);
-extern int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64) __INTRODUCED_IN(12);
+int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64) __INTRODUCED_IN(12);
 #else
-extern int truncate(const char* __path, off_t __length);
-extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
-extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
-                      off_t __offset);
-extern int ftruncate(int __fd, off_t __length);
+int truncate(const char* __path, off_t __length);
+ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
+ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset);
+int ftruncate(int __fd, off_t __length);
 #endif
 
-extern int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
-extern ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(12);
-extern ssize_t pwrite64(int __fd, const void* __buf, size_t __count, off64_t __offset)
+int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
+ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(12);
+ssize_t pwrite64(int __fd, const void* __buf, size_t __count, off64_t __offset)
   __INTRODUCED_IN(12);
-extern int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(12);
+int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(12);
 
-extern int pause(void);
-extern unsigned int alarm(unsigned int __seconds);
-extern unsigned int sleep(unsigned int __seconds);
-extern int usleep(useconds_t __usec);
+int pause(void);
+unsigned int alarm(unsigned int __seconds);
+unsigned int sleep(unsigned int __seconds);
+int usleep(useconds_t __usec);
 
 int gethostname(char* __name, size_t __len);
 int sethostname(const char* __name, size_t __len) __INTRODUCED_IN(23);
 
-extern int brk(void* __addr);
-extern void* sbrk(ptrdiff_t __increment);
+int brk(void* __addr);
+void* sbrk(ptrdiff_t __increment);
 
-extern int isatty(int __fd);
-extern char* ttyname(int __fd);
-extern int ttyname_r(int __fd, char* __buf, size_t __buflen) __INTRODUCED_IN(8);
+int isatty(int __fd);
+char* ttyname(int __fd);
+int ttyname_r(int __fd, char* __buf, size_t __buflen) __INTRODUCED_IN(8);
 
-extern int acct(const char* __filepath);
+int acct(const char* __filepath);
 
 #if __ANDROID_API__ >= 21
 int getpagesize(void);
@@ -223,15 +218,15 @@
 
 long syscall(long __number, ...);
 
-extern int daemon(int __nochdir, int __noclose);
+int daemon(int __nochdir, int __noclose);
 
 #if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
-extern int cacheflush(long __addr, long __nbytes, long __cache);
+int cacheflush(long __addr, long __nbytes, long __cache);
     /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
 #endif
 
-extern pid_t tcgetpgrp(int __fd);
-extern int tcsetpgrp(int __fd, pid_t __pid);
+pid_t tcgetpgrp(int __fd);
+int tcsetpgrp(int __fd, pid_t __pid);
 
 /* Used to retry syscalls that can return EINTR. */
 #define TEMP_FAILURE_RETRY(exp) ({         \
@@ -242,53 +237,52 @@
     _rc; })
 
 /* TODO(unified-headers): Factor out all the FORTIFY features. */
-extern char* __getcwd_chk(char*, size_t, size_t) __INTRODUCED_IN(24);
+char* __getcwd_chk(char*, size_t, size_t) __INTRODUCED_IN(24);
 __errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
-extern char* __getcwd_real(char*, size_t) __RENAME(getcwd);
+char* __getcwd_real(char*, size_t) __RENAME(getcwd);
 
-extern ssize_t __pread_chk(int, void*, size_t, off_t, size_t) __INTRODUCED_IN(23);
+ssize_t __pread_chk(int, void*, size_t, off_t, size_t) __INTRODUCED_IN(23);
 __errordecl(__pread_dest_size_error, "pread called with size bigger than destination");
 __errordecl(__pread_count_toobig_error, "pread called with count > SSIZE_MAX");
-extern ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
+ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
 
-extern ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t) __INTRODUCED_IN(23);
+ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t) __INTRODUCED_IN(23);
 __errordecl(__pread64_dest_size_error, "pread64 called with size bigger than destination");
 __errordecl(__pread64_count_toobig_error, "pread64 called with count > SSIZE_MAX");
-extern ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64) __INTRODUCED_IN(12);
+ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64) __INTRODUCED_IN(12);
 
-extern ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t) __INTRODUCED_IN(24);
+ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t) __INTRODUCED_IN(24);
 __errordecl(__pwrite_dest_size_error, "pwrite called with size bigger than destination");
 __errordecl(__pwrite_count_toobig_error, "pwrite called with count > SSIZE_MAX");
-extern ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
+ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
 
-extern ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t) __INTRODUCED_IN(24);
+ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t) __INTRODUCED_IN(24);
 __errordecl(__pwrite64_dest_size_error, "pwrite64 called with size bigger than destination");
 __errordecl(__pwrite64_count_toobig_error, "pwrite64 called with count > SSIZE_MAX");
-extern ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64)
-  __INTRODUCED_IN(12);
+ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64) __INTRODUCED_IN(12);
 
-extern ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21);
+ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21);
 __errordecl(__read_dest_size_error, "read called with size bigger than destination");
 __errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
-extern ssize_t __read_real(int, void*, size_t) __RENAME(read);
+ssize_t __read_real(int, void*, size_t) __RENAME(read);
 
-extern ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24);
+ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24);
 __errordecl(__write_dest_size_error, "write called with size bigger than destination");
 __errordecl(__write_count_toobig_error, "write called with count > SSIZE_MAX");
-extern ssize_t __write_real(int, const void*, size_t) __RENAME(write);
+ssize_t __write_real(int, const void*, size_t) __RENAME(write);
 
-extern ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
+ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
 __errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
 __errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
-extern ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
+ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
 
-extern ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
+ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
 __errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
 __errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
-extern ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat) __INTRODUCED_IN(21);
+ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat) __INTRODUCED_IN(21);
 
-extern int getdomainname(char*, size_t) __INTRODUCED_IN_FUTURE;
-extern int setdomainname(const char*, size_t) __INTRODUCED_IN_FUTURE;
+int getdomainname(char*, size_t) __INTRODUCED_IN_FUTURE;
+int setdomainname(const char*, size_t) __INTRODUCED_IN_FUTURE;
 
 #if defined(__BIONIC_FORTIFY)
 
diff --git a/libc/include/utime.h b/libc/include/utime.h
index 3d72da4..9783859 100644
--- a/libc/include/utime.h
+++ b/libc/include/utime.h
@@ -35,7 +35,7 @@
 
 __BEGIN_DECLS
 
-extern int utime(const char*, const struct utimbuf*);
+int utime(const char*, const struct utimbuf*);
 
 __END_DECLS
 
diff --git a/libc/include/wchar.h b/libc/include/wchar.h
index fbea851..1f0c1c5 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -67,88 +67,88 @@
 
 #define  WEOF        ((wint_t)(-1))
 
-extern wint_t            btowc(int);
-extern int               fwprintf(FILE *, const wchar_t *, ...);
-extern int               fwscanf(FILE *, const wchar_t *, ...);
-extern wint_t            fgetwc(FILE *);
-extern wchar_t          *fgetws(wchar_t *, int, FILE *);
-extern wint_t            fputwc(wchar_t, FILE *);
-extern int               fputws(const wchar_t *, FILE *);
-extern int               fwide(FILE *, int);
-extern wint_t            getwc(FILE *);
-extern wint_t            getwchar(void);
-extern int               mbsinit(const mbstate_t *);
-extern size_t            mbrlen(const char *, size_t, mbstate_t *);
-extern size_t            mbrtowc(wchar_t *, const char *, size_t, mbstate_t *);
-extern size_t mbsrtowcs(wchar_t*, const char**, size_t, mbstate_t*);
-extern size_t mbsnrtowcs(wchar_t*, const char**, size_t, size_t, mbstate_t*) __INTRODUCED_IN(21);
-extern wint_t            putwc(wchar_t, FILE *);
-extern wint_t            putwchar(wchar_t);
-extern int               swprintf(wchar_t *, size_t, const wchar_t *, ...);
-extern int               swscanf(const wchar_t *, const wchar_t *, ...);
-extern wint_t            ungetwc(wint_t, FILE *);
-extern int vfwprintf(FILE*, const wchar_t*, va_list);
-extern int vfwscanf(FILE*, const wchar_t*, va_list) __INTRODUCED_IN(21);
-extern int vswprintf(wchar_t*, size_t, const wchar_t*, va_list);
-extern int vswscanf(const wchar_t*, const wchar_t*, va_list) __INTRODUCED_IN(21);
-extern int vwprintf(const wchar_t*, va_list);
-extern int vwscanf(const wchar_t*, va_list) __INTRODUCED_IN(21);
-extern wchar_t* wcpcpy (wchar_t*, const wchar_t *);
-extern wchar_t* wcpncpy (wchar_t*, const wchar_t *, size_t);
-extern size_t            wcrtomb(char *, wchar_t, mbstate_t *);
-extern int               wcscasecmp(const wchar_t *, const wchar_t *);
-extern int wcscasecmp_l(const wchar_t*, const wchar_t*, locale_t) __INTRODUCED_IN(23);
-extern wchar_t          *wcscat(wchar_t *, const wchar_t *);
-extern wchar_t          *wcschr(const wchar_t *, wchar_t);
-extern int               wcscmp(const wchar_t *, const wchar_t *);
-extern int               wcscoll(const wchar_t *, const wchar_t *);
-extern wchar_t          *wcscpy(wchar_t *, const wchar_t *);
-extern size_t            wcscspn(const wchar_t *, const wchar_t *);
-extern size_t            wcsftime(wchar_t *, size_t, const wchar_t *, const struct tm *);
-extern size_t            wcslen(const wchar_t *);
-extern int               wcsncasecmp(const wchar_t *, const wchar_t *, size_t);
-extern int wcsncasecmp_l(const wchar_t*, const wchar_t*, size_t, locale_t) __INTRODUCED_IN(23);
-extern wchar_t          *wcsncat(wchar_t *, const wchar_t *, size_t);
-extern int               wcsncmp(const wchar_t *, const wchar_t *, size_t);
-extern wchar_t          *wcsncpy(wchar_t *, const wchar_t *, size_t);
-extern size_t wcsnrtombs(char*, const wchar_t**, size_t, size_t, mbstate_t*) __INTRODUCED_IN(21);
-extern wchar_t          *wcspbrk(const wchar_t *, const wchar_t *);
-extern wchar_t          *wcsrchr(const wchar_t *, wchar_t);
-extern size_t wcsrtombs(char*, const wchar_t**, size_t, mbstate_t*);
-extern size_t            wcsspn(const wchar_t *, const wchar_t *);
-extern wchar_t          *wcsstr(const wchar_t *, const wchar_t *);
-extern double wcstod(const wchar_t*, wchar_t**);
-extern float wcstof(const wchar_t*, wchar_t**) __INTRODUCED_IN(21);
-extern wchar_t* wcstok(wchar_t*, const wchar_t*, wchar_t**);
-extern long wcstol(const wchar_t*, wchar_t**, int);
-extern long long wcstoll(const wchar_t*, wchar_t**, int) __INTRODUCED_IN(21);
-extern long double wcstold(const wchar_t*, wchar_t**) __INTRODUCED_IN(21);
-extern unsigned long wcstoul(const wchar_t*, wchar_t**, int);
-extern unsigned long long wcstoull(const wchar_t*, wchar_t**, int) __INTRODUCED_IN(21);
-extern int               wcswidth(const wchar_t *, size_t);
-extern size_t            wcsxfrm(wchar_t *, const wchar_t *, size_t);
-extern int               wctob(wint_t);
-extern int               wcwidth(wchar_t);
-extern wchar_t          *wmemchr(const wchar_t *, wchar_t, size_t);
-extern int               wmemcmp(const wchar_t *, const wchar_t *, size_t);
-extern wchar_t          *wmemcpy(wchar_t *, const wchar_t *, size_t);
+wint_t            btowc(int);
+int               fwprintf(FILE *, const wchar_t *, ...);
+int               fwscanf(FILE *, const wchar_t *, ...);
+wint_t            fgetwc(FILE *);
+wchar_t          *fgetws(wchar_t *, int, FILE *);
+wint_t            fputwc(wchar_t, FILE *);
+int               fputws(const wchar_t *, FILE *);
+int               fwide(FILE *, int);
+wint_t            getwc(FILE *);
+wint_t            getwchar(void);
+int               mbsinit(const mbstate_t *);
+size_t            mbrlen(const char *, size_t, mbstate_t *);
+size_t            mbrtowc(wchar_t *, const char *, size_t, mbstate_t *);
+size_t mbsrtowcs(wchar_t*, const char**, size_t, mbstate_t*);
+size_t mbsnrtowcs(wchar_t*, const char**, size_t, size_t, mbstate_t*) __INTRODUCED_IN(21);
+wint_t            putwc(wchar_t, FILE *);
+wint_t            putwchar(wchar_t);
+int               swprintf(wchar_t *, size_t, const wchar_t *, ...);
+int               swscanf(const wchar_t *, const wchar_t *, ...);
+wint_t            ungetwc(wint_t, FILE *);
+int vfwprintf(FILE*, const wchar_t*, va_list);
+int vfwscanf(FILE*, const wchar_t*, va_list) __INTRODUCED_IN(21);
+int vswprintf(wchar_t*, size_t, const wchar_t*, va_list);
+int vswscanf(const wchar_t*, const wchar_t*, va_list) __INTRODUCED_IN(21);
+int vwprintf(const wchar_t*, va_list);
+int vwscanf(const wchar_t*, va_list) __INTRODUCED_IN(21);
+wchar_t* wcpcpy (wchar_t*, const wchar_t *);
+wchar_t* wcpncpy (wchar_t*, const wchar_t *, size_t);
+size_t            wcrtomb(char *, wchar_t, mbstate_t *);
+int               wcscasecmp(const wchar_t *, const wchar_t *);
+int wcscasecmp_l(const wchar_t*, const wchar_t*, locale_t) __INTRODUCED_IN(23);
+wchar_t          *wcscat(wchar_t *, const wchar_t *);
+wchar_t          *wcschr(const wchar_t *, wchar_t);
+int               wcscmp(const wchar_t *, const wchar_t *);
+int               wcscoll(const wchar_t *, const wchar_t *);
+wchar_t          *wcscpy(wchar_t *, const wchar_t *);
+size_t            wcscspn(const wchar_t *, const wchar_t *);
+size_t            wcsftime(wchar_t *, size_t, const wchar_t *, const struct tm *);
+size_t            wcslen(const wchar_t *);
+int               wcsncasecmp(const wchar_t *, const wchar_t *, size_t);
+int wcsncasecmp_l(const wchar_t*, const wchar_t*, size_t, locale_t) __INTRODUCED_IN(23);
+wchar_t          *wcsncat(wchar_t *, const wchar_t *, size_t);
+int               wcsncmp(const wchar_t *, const wchar_t *, size_t);
+wchar_t          *wcsncpy(wchar_t *, const wchar_t *, size_t);
+size_t wcsnrtombs(char*, const wchar_t**, size_t, size_t, mbstate_t*) __INTRODUCED_IN(21);
+wchar_t          *wcspbrk(const wchar_t *, const wchar_t *);
+wchar_t          *wcsrchr(const wchar_t *, wchar_t);
+size_t wcsrtombs(char*, const wchar_t**, size_t, mbstate_t*);
+size_t            wcsspn(const wchar_t *, const wchar_t *);
+wchar_t          *wcsstr(const wchar_t *, const wchar_t *);
+double wcstod(const wchar_t*, wchar_t**);
+float wcstof(const wchar_t*, wchar_t**) __INTRODUCED_IN(21);
+wchar_t* wcstok(wchar_t*, const wchar_t*, wchar_t**);
+long wcstol(const wchar_t*, wchar_t**, int);
+long long wcstoll(const wchar_t*, wchar_t**, int) __INTRODUCED_IN(21);
+long double wcstold(const wchar_t*, wchar_t**) __INTRODUCED_IN(21);
+unsigned long wcstoul(const wchar_t*, wchar_t**, int);
+unsigned long long wcstoull(const wchar_t*, wchar_t**, int) __INTRODUCED_IN(21);
+int               wcswidth(const wchar_t *, size_t);
+size_t            wcsxfrm(wchar_t *, const wchar_t *, size_t);
+int               wctob(wint_t);
+int               wcwidth(wchar_t);
+wchar_t          *wmemchr(const wchar_t *, wchar_t, size_t);
+int               wmemcmp(const wchar_t *, const wchar_t *, size_t);
+wchar_t          *wmemcpy(wchar_t *, const wchar_t *, size_t);
 #if defined(__USE_GNU)
-extern wchar_t* wmempcpy(wchar_t*, const wchar_t*, size_t) __INTRODUCED_IN(23);
+wchar_t* wmempcpy(wchar_t*, const wchar_t*, size_t) __INTRODUCED_IN(23);
 #endif
-extern wchar_t          *wmemmove(wchar_t *, const wchar_t *, size_t);
-extern wchar_t          *wmemset(wchar_t *, wchar_t, size_t);
-extern int               wprintf(const wchar_t *, ...);
-extern int               wscanf(const wchar_t *, ...);
+wchar_t          *wmemmove(wchar_t *, const wchar_t *, size_t);
+wchar_t          *wmemset(wchar_t *, wchar_t, size_t);
+int               wprintf(const wchar_t *, ...);
+int               wscanf(const wchar_t *, ...);
 
-extern long long wcstoll_l(const wchar_t*, wchar_t**, int, locale_t) __INTRODUCED_IN(21);
-extern unsigned long long wcstoull_l(const wchar_t*, wchar_t**, int, locale_t) __INTRODUCED_IN(21);
-extern long double wcstold_l(const wchar_t*, wchar_t**, locale_t) __INTRODUCED_IN(21);
+long long wcstoll_l(const wchar_t*, wchar_t**, int, locale_t) __INTRODUCED_IN(21);
+unsigned long long wcstoull_l(const wchar_t*, wchar_t**, int, locale_t) __INTRODUCED_IN(21);
+long double wcstold_l(const wchar_t*, wchar_t**, locale_t) __INTRODUCED_IN(21);
 
-extern int wcscoll_l(const wchar_t*, const wchar_t*, locale_t) __INTRODUCED_IN(21);
-extern size_t wcsxfrm_l(wchar_t*, const wchar_t*, size_t, locale_t) __INTRODUCED_IN(21);
+int wcscoll_l(const wchar_t*, const wchar_t*, locale_t) __INTRODUCED_IN(21);
+size_t wcsxfrm_l(wchar_t*, const wchar_t*, size_t, locale_t) __INTRODUCED_IN(21);
 
-extern size_t wcslcat(wchar_t*, const wchar_t*, size_t);
-extern size_t wcslcpy(wchar_t*, const wchar_t*, size_t);
+size_t wcslcat(wchar_t*, const wchar_t*, size_t);
+size_t wcslcpy(wchar_t*, const wchar_t*, size_t);
 
 FILE* open_wmemstream(wchar_t**, size_t*) __INTRODUCED_IN(23);
 wchar_t* wcsdup(const wchar_t*);
diff --git a/libc/include/wctype.h b/libc/include/wctype.h
index 41e0ec3..96464b1 100644
--- a/libc/include/wctype.h
+++ b/libc/include/wctype.h
@@ -29,9 +29,9 @@
 #ifndef _WCTYPE_H_
 #define _WCTYPE_H_
 
-#include <wchar.h>
-
 #include <bits/wctype.h>
+#include <wchar.h>
+#include <sys/cdefs.h>
 
 __BEGIN_DECLS
 
diff --git a/libc/include/xlocale.h b/libc/include/xlocale.h
index f7eb8f4..559d24d 100644
--- a/libc/include/xlocale.h
+++ b/libc/include/xlocale.h
@@ -29,6 +29,8 @@
 #ifndef _XLOCALE_H_
 #define _XLOCALE_H_
 
+#include <sys/cdefs.h>
+
 /* If we just use void* here, GCC exposes that in error messages. */
 struct __locale_t;
 typedef struct __locale_t* locale_t;
diff --git a/libc/private/UniquePtr.h b/libc/private/UniquePtr.h
deleted file mode 100644
index 5ac7599..0000000
--- a/libc/private/UniquePtr.h
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-
-#ifndef UNIQUE_PTR_H_included
-#define UNIQUE_PTR_H_included
-
-// Default deleter for pointer types.
-template <typename T>
-struct DefaultDelete {
-    enum { type_must_be_complete = sizeof(T) };
-    DefaultDelete() {}
-    void operator()(T* p) const {
-        delete p;
-    }
-};
-
-// Default deleter for array types.
-template <typename T>
-struct DefaultDelete<T[]> {
-    enum { type_must_be_complete = sizeof(T) };
-    void operator()(T* p) const {
-        delete[] p;
-    }
-};
-
-// A smart pointer that deletes the given pointer on destruction.
-// Equivalent to C++0x's std::unique_ptr (a combination of boost::scoped_ptr
-// and boost::scoped_array).
-// Named to be in keeping with Android style but also to avoid
-// collision with any other implementation, until we can switch over
-// to unique_ptr.
-// Use thus:
-//   UniquePtr<C> c(new C);
-template <typename T, typename D = DefaultDelete<T> >
-class UniquePtr {
-public:
-    // Construct a new UniquePtr, taking ownership of the given raw pointer.
-    explicit UniquePtr(T* ptr = nullptr) : mPtr(ptr) { }
-
-    UniquePtr(UniquePtr<T, D>&& that) {
-      mPtr = that.mPtr;
-      that.mPtr = nullptr;
-    }
-
-    ~UniquePtr() {
-        reset();
-    }
-
-    // Accessors.
-    T& operator*() const { return *mPtr; }
-    T* operator->() const { return mPtr; }
-    T* get() const { return mPtr; }
-
-    // Returns the raw pointer and hands over ownership to the caller.
-    // The pointer will not be deleted by UniquePtr.
-    T* release() __attribute__((warn_unused_result)) {
-        T* result = mPtr;
-        mPtr = nullptr;
-        return result;
-    }
-
-    // Takes ownership of the given raw pointer.
-    // If this smart pointer previously owned a different raw pointer, that
-    // raw pointer will be freed.
-    void reset(T* ptr = nullptr) {
-        if (ptr != mPtr) {
-            D()(mPtr);
-            mPtr = ptr;
-        }
-    }
-
-private:
-    // The raw pointer.
-    T* mPtr;
-
-    // Comparing unique pointers is probably a mistake, since they're unique.
-    template <typename T2> bool operator==(const UniquePtr<T2>& p) const = delete;
-    template <typename T2> bool operator!=(const UniquePtr<T2>& p) const = delete;
-
-    // Disallow copy and assignment.
-    UniquePtr(const UniquePtr&) = delete;
-    void operator=(const UniquePtr&) = delete;
-};
-
-// Partial specialization for array types. Like std::unique_ptr, this removes
-// operator* and operator-> but adds operator[].
-template <typename T, typename D>
-class UniquePtr<T[], D> {
-public:
-    explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) {
-    }
-    UniquePtr(UniquePtr<T, D>&& that) {
-      mPtr = that.mPtr;
-      that.mPtr = nullptr;
-    }
-
-    ~UniquePtr() {
-        reset();
-    }
-
-    T& operator[](size_t i) const {
-        return mPtr[i];
-    }
-    T* get() const { return mPtr; }
-
-    T* release() __attribute__((warn_unused_result)) {
-        T* result = mPtr;
-        mPtr = NULL;
-        return result;
-    }
-
-    void reset(T* ptr = NULL) {
-        if (ptr != mPtr) {
-            D()(mPtr);
-            mPtr = ptr;
-        }
-    }
-
-private:
-    T* mPtr;
-
-    // Disallow copy and assignment.
-    UniquePtr(const UniquePtr&) = delete;
-    void operator=(const UniquePtr&) = delete;
-};
-
-#endif  // UNIQUE_PTR_H_included
diff --git a/libc/private/bionic_globals.h b/libc/private/bionic_globals.h
index b45c0c3..94dd7e8 100644
--- a/libc/private/bionic_globals.h
+++ b/libc/private/bionic_globals.h
@@ -44,7 +44,6 @@
 __LIBC_HIDDEN__ extern WriteProtected<libc_globals> __libc_globals;
 
 class KernelArgumentBlock;
-__LIBC_HIDDEN__ void __libc_init_global_stack_chk_guard(KernelArgumentBlock& args);
 __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals);
 __LIBC_HIDDEN__ void __libc_init_setjmp_cookie(libc_globals* globals, KernelArgumentBlock& args);
 __LIBC_HIDDEN__ void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args);
diff --git a/libc/stdio/local.h b/libc/stdio/local.h
index 7fe339a..f25e902 100644
--- a/libc/stdio/local.h
+++ b/libc/stdio/local.h
@@ -200,8 +200,6 @@
 __LIBC32_LEGACY_PUBLIC__ int __sclose(void *);
 __LIBC32_LEGACY_PUBLIC__ int _fwalk(int (*)(FILE *));
 
-#pragma GCC visibility push(hidden)
-
 off64_t __sseek64(void*, off64_t, int);
 int	__sflush_locked(FILE *);
 int	__swhatbuf(FILE *, size_t *, int *);
@@ -271,8 +269,6 @@
 extern void __sinit(void); // Not actually implemented.
 #define __sdidinit 1
 
-#pragma GCC visibility pop
-
 __END_DECLS
 
 #endif
diff --git a/libc/upstream-netbsd/android/include/netbsd-compat.h b/libc/upstream-netbsd/android/include/netbsd-compat.h
index 665d65e..cc0f71f 100644
--- a/libc/upstream-netbsd/android/include/netbsd-compat.h
+++ b/libc/upstream-netbsd/android/include/netbsd-compat.h
@@ -25,6 +25,17 @@
 // that they don't actually test or ship with this.
 #define _DIAGASSERT(e) /* nothing */
 
+/*
+ * The following macro is used to remove const cast-away warnings
+ * from gcc -Wcast-qual; it should be used with caution because it
+ * can hide valid errors; in particular most valid uses are in
+ * situations where the API requires it, not to cast away string
+ * constants. We don't use *intptr_t on purpose here and we are
+ * explicit about unsigned long so that we don't have additional
+ * dependencies.
+ */
+#define __UNCONST(a)    ((void *)(unsigned long)(const void *)(a))
+
 // TODO: we don't yet have thread-safe environment variables.
 #define __readlockenv() 0
 #define __unlockenv() 0
diff --git a/libm/i387/fenv.c b/libm/i387/fenv.c
index 64c2b7a..09b4386 100644
--- a/libm/i387/fenv.c
+++ b/libm/i387/fenv.c
@@ -70,18 +70,18 @@
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff } /*__other*/
 };
 
-#define __fldcw(__cw)           __asm __volatile("fldcw %0" : : "m" (__cw))
-#define __fldenv(__env)         __asm __volatile("fldenv %0" : : "m" (__env))
-#define __fldenvx(__env)        __asm __volatile("fldenv %0" : : "m" (__env)  \
+#define __fldcw(__cw)           __asm volatile("fldcw %0" : : "m" (__cw))
+#define __fldenv(__env)         __asm volatile("fldenv %0" : : "m" (__env))
+#define __fldenvx(__env)        __asm volatile("fldenv %0" : : "m" (__env)  \
                                 : "st", "st(1)", "st(2)", "st(3)", "st(4)",   \
                                 "st(5)", "st(6)", "st(7)")
-#define __fnclex()              __asm __volatile("fnclex")
-#define __fnstenv(__env)        __asm __volatile("fnstenv %0" : "=m" (*(__env)))
-#define __fnstcw(__cw)          __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
-#define __fnstsw(__sw)          __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
-#define __fwait()               __asm __volatile("fwait")
-#define __ldmxcsr(__csr)        __asm __volatile("ldmxcsr %0" : : "m" (__csr))
-#define __stmxcsr(__csr)        __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
+#define __fnclex()              __asm volatile("fnclex")
+#define __fnstenv(__env)        __asm volatile("fnstenv %0" : "=m" (*(__env)))
+#define __fnstcw(__cw)          __asm volatile("fnstcw %0" : "=m" (*(__cw)))
+#define __fnstsw(__sw)          __asm volatile("fnstsw %0" : "=am" (*(__sw)))
+#define __fwait()               __asm volatile("fwait")
+#define __ldmxcsr(__csr)        __asm volatile("ldmxcsr %0" : : "m" (__csr))
+#define __stmxcsr(__csr)        __asm volatile("stmxcsr %0" : "=m" (*(__csr)))
 
 /* After testing for SSE support once, we cache the result in __has_sse. */
 enum __sse_support { __SSE_YES, __SSE_NO, __SSE_UNK };
@@ -100,9 +100,9 @@
 #endif
 
 #ifndef __SSE__
-#define getfl(x)    __asm __volatile("pushfl\n\tpopl %0" : "=mr" (*(x)))
-#define setfl(x)    __asm __volatile("pushl %0\n\tpopfl" : : "g" (x))
-#define cpuid_dx(x) __asm __volatile("pushl %%ebx\n\tmovl $1, %%eax\n\t"  \
+#define getfl(x)    __asm volatile("pushfl\n\tpopl %0" : "=mr" (*(x)))
+#define setfl(x)    __asm volatile("pushl %0\n\tpopfl" : : "g" (x))
+#define cpuid_dx(x) __asm volatile("pushl %%ebx\n\tmovl $1, %%eax\n\t"  \
                     "cpuid\n\tpopl %%ebx"          \
                     : "=d" (*(x)) : : "eax", "ecx")
 
diff --git a/linker/linker_phdr.cpp b/linker/linker_phdr.cpp
index a7af82f..ebc3166 100644
--- a/linker/linker_phdr.cpp
+++ b/linker/linker_phdr.cpp
@@ -245,6 +245,17 @@
     return false;
   }
 
+  if (header_.e_shentsize != sizeof(ElfW(Shdr))) {
+    DL_ERR("\"%s\" has unsupported e_shentsize: 0x%x (expected 0x%zx)",
+           name_.c_str(), header_.e_shentsize, sizeof(ElfW(Shdr)));
+    return false;
+  }
+
+  if (header_.e_shstrndx == 0) {
+    DL_ERR("\"%s\" has invalid e_shstrndx", name_.c_str());
+    return false;
+  }
+
   return true;
 }
 
diff --git a/tests/Android.bp b/tests/Android.bp
index 4ea1122..8f937a3 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -52,6 +52,7 @@
     defaults: ["bionic_tests_defaults"],
     srcs: [
         "arpa_inet_test.cpp",
+        "assert_test.cpp",
         "buffer_tests.cpp",
         "bug_26110743_test.cpp",
         "complex_test.cpp",
@@ -129,7 +130,6 @@
         "system_properties_test.cpp",
         "time_test.cpp",
         "uchar_test.cpp",
-        "uniqueptr_test.cpp",
         "unistd_nofortify_test.cpp",
         "unistd_test.cpp",
         "utmp_test.cpp",
diff --git a/tests/assert_test.cpp b/tests/assert_test.cpp
new file mode 100644
index 0000000..9436151
--- /dev/null
+++ b/tests/assert_test.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2016 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 <gtest/gtest.h>
+
+#undef NDEBUG
+#include <assert.h>
+
+TEST(assert, assert_true) {
+  assert(true);
+}
+
+TEST(assert, assert_false) {
+  EXPECT_DEATH(assert(false),
+               "bionic/tests/assert_test.cpp:.*: "
+               "virtual void assert_assert_false_Test::TestBody\\(\\): "
+               "assertion \"false\" failed");
+}
+
+// Re-include <assert.h> with assertions disabled.
+#define NDEBUG
+#include <assert.h>
+
+TEST(assert, assert_true_NDEBUG) {
+  assert(true);
+}
+
+TEST(assert, assert_false_NDEBUG) {
+  assert(false);
+}
diff --git a/tests/uniqueptr_test.cpp b/tests/uniqueptr_test.cpp
deleted file mode 100644
index 4b6608a..0000000
--- a/tests/uniqueptr_test.cpp
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * Copyright (C) 2014 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 <gtest/gtest.h>
-
-#include <private/UniquePtr.h>
-
-static int cCount = 0;
-struct C {
-  C() { ++cCount; }
-  ~C() { --cCount; }
-};
-
-static bool freed = false;
-struct Freer {
-  void operator() (int* p) {
-    ASSERT_EQ(123, *p);
-    free(p);
-    freed = true;
-  }
-};
-
-TEST(UniquePtr, smoke) {
-  //
-  // UniquePtr<T> tests...
-  //
-
-  // Can we free a single object?
-  {
-    UniquePtr<C> c(new C);
-    ASSERT_TRUE(cCount == 1);
-  }
-  ASSERT_TRUE(cCount == 0);
-  // Does release work?
-  C* rawC;
-  {
-      UniquePtr<C> c(new C);
-      ASSERT_TRUE(cCount == 1);
-      rawC = c.release();
-  }
-  ASSERT_TRUE(cCount == 1);
-  delete rawC;
-  // Does reset work?
-  {
-      UniquePtr<C> c(new C);
-      ASSERT_TRUE(cCount == 1);
-      c.reset(new C);
-      ASSERT_TRUE(cCount == 1);
-  }
-  ASSERT_TRUE(cCount == 0);
-
-  //
-  // UniquePtr<T[]> tests...
-  //
-
-  // Can we free an array?
-  {
-      UniquePtr<C[]> cs(new C[4]);
-      ASSERT_TRUE(cCount == 4);
-  }
-  ASSERT_TRUE(cCount == 0);
-  // Does release work?
-  {
-      UniquePtr<C[]> c(new C[4]);
-      ASSERT_TRUE(cCount == 4);
-      rawC = c.release();
-  }
-  ASSERT_TRUE(cCount == 4);
-  delete[] rawC;
-  // Does reset work?
-  {
-      UniquePtr<C[]> c(new C[4]);
-      ASSERT_TRUE(cCount == 4);
-      c.reset(new C[2]);
-      ASSERT_TRUE(cCount == 2);
-  }
-  ASSERT_TRUE(cCount == 0);
-
-  //
-  // Custom deleter tests...
-  //
-  ASSERT_TRUE(!freed);
-  {
-      UniquePtr<int, Freer> i(reinterpret_cast<int*>(malloc(sizeof(int))));
-      *i = 123;
-  }
-  ASSERT_TRUE(freed);
-}