Merge changes I1bb1925a,Icd638673

* changes:
  versioner: introduce.
  Update header versions for NDK platform fixes.
diff --git a/libc/include/android/legacy_signal_inlines.h b/libc/include/android/legacy_signal_inlines.h
index bf895da..edc6259 100644
--- a/libc/include/android/legacy_signal_inlines.h
+++ b/libc/include/android/legacy_signal_inlines.h
@@ -34,11 +34,12 @@
 #include <string.h>
 #include <sys/cdefs.h>
 
-#if __ANDROID_API__ < 21
 
 __BEGIN_DECLS
 
-extern sighandler_t bsd_signal(int signum, sighandler_t handler);
+extern sighandler_t bsd_signal(int signum, sighandler_t handler) __REMOVED_IN(21);
+
+#if __ANDROID_API__ < 21
 
 static __inline int sigismember(const sigset_t *set, int signum) {
   /* Signal numbers start at 1, but bit positions start at 0. */
@@ -97,7 +98,8 @@
   return bsd_signal(s, f);
 }
 
+#endif /* __ANDROID_API__ < 21 */
+
 __END_DECLS
 
-#endif
 #endif /* _ANDROID_LEGACY_SIGNAL_INLINES_H_ */
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index c8030f1..ee4d5e2 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -87,13 +87,13 @@
 extern 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(21);
+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(21);
+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(21);
+extern int __openat_2(int, const char*, int) __INTRODUCED_IN(17);
 extern 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 e51f9b6..d4f6994 100644
--- a/libc/include/fenv.h
+++ b/libc/include/fenv.h
@@ -36,23 +36,27 @@
 __BEGIN_DECLS
 #pragma GCC visibility push(default)
 
-int feclearexcept(int) __INTRODUCED_IN(21);
-int fegetexceptflag(fexcept_t*, int) __INTRODUCED_IN(21);
-int feraiseexcept(int) __INTRODUCED_IN(21);
-int fesetexceptflag(const fexcept_t*, int) __INTRODUCED_IN(21);
-int fetestexcept(int) __INTRODUCED_IN(21);
+// fenv was always available on x86.
+int feclearexcept(int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
+int fegetexceptflag(fexcept_t*, int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21)
+    __INTRODUCED_IN_X86(9);
+int feraiseexcept(int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
+int fesetexceptflag(const fexcept_t*, int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21)
+    __INTRODUCED_IN_X86(9);
+int fetestexcept(int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
 
-int fegetround(void) __INTRODUCED_IN(21);
-int fesetround(int) __INTRODUCED_IN(21);
+int fegetround(void) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
+int fesetround(int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
 
-int fegetenv(fenv_t*) __INTRODUCED_IN(21);
-int feholdexcept(fenv_t*) __INTRODUCED_IN(21);
-int fesetenv(const fenv_t*) __INTRODUCED_IN(21);
-int feupdateenv(const fenv_t*) __INTRODUCED_IN(21);
+int fegetenv(fenv_t*) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
+int feholdexcept(fenv_t*) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
+int fesetenv(const fenv_t*) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
+int feupdateenv(const fenv_t*) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21)
+    __INTRODUCED_IN_X86(9);
 
-int feenableexcept(int) __INTRODUCED_IN(21);
-int fedisableexcept(int) __INTRODUCED_IN(21);
-int fegetexcept(void) __INTRODUCED_IN(21);
+int feenableexcept(int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
+int fedisableexcept(int) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
+int fegetexcept(void) __INTRODUCED_IN_ARM(21) __INTRODUCED_IN_MIPS(21) __INTRODUCED_IN_X86(9);
 
 /*
  * The following constant represents the default floating-point environment
diff --git a/libc/include/ftw.h b/libc/include/ftw.h
index ebd59f5..ac901cc 100644
--- a/libc/include/ftw.h
+++ b/libc/include/ftw.h
@@ -54,9 +54,9 @@
 };
 
 __BEGIN_DECLS
-int ftw(const char*, int (*)(const char*, const struct stat*, int), int) __INTRODUCED_IN(21);
+int ftw(const char*, int (*)(const char*, const struct stat*, int), int) __INTRODUCED_IN(17);
 int nftw(const char*, int (*)(const char*, const struct stat*, int, struct FTW*), int, int)
-  __INTRODUCED_IN(21);
+  __INTRODUCED_IN(17);
 int ftw64(const char*, int (*)(const char*, const struct stat64*, int), int) __INTRODUCED_IN(21);
 int nftw64(const char*, int (*)(const char*, const struct stat64*, int, struct FTW*), int, int)
   __INTRODUCED_IN(21);
diff --git a/libc/include/inttypes.h b/libc/include/inttypes.h
index 4752c52..54b5ee2 100644
--- a/libc/include/inttypes.h
+++ b/libc/include/inttypes.h
@@ -254,8 +254,8 @@
 } imaxdiv_t;
 
 __BEGIN_DECLS
-intmax_t imaxabs(intmax_t) __pure2 __INTRODUCED_IN(21);
-imaxdiv_t imaxdiv(intmax_t, intmax_t) __pure2 __INTRODUCED_IN(21);
+intmax_t imaxabs(intmax_t) __pure2 __INTRODUCED_IN(19);
+imaxdiv_t imaxdiv(intmax_t, intmax_t) __pure2 __INTRODUCED_IN(19);
 intmax_t	strtoimax(const char *, char **, int);
 uintmax_t	strtoumax(const char *, char **, int);
 intmax_t wcstoimax(const wchar_t* __restrict, wchar_t** __restrict, int) __INTRODUCED_IN(21);
diff --git a/libc/include/malloc.h b/libc/include/malloc.h
index 02e2da7..8e6cef9 100644
--- a/libc/include/malloc.h
+++ b/libc/include/malloc.h
@@ -29,7 +29,7 @@
 extern 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(21);
+extern size_t malloc_usable_size(const void* p) __INTRODUCED_IN(17);
 
 #ifndef STRUCT_MALLINFO_DECLARED
 #define STRUCT_MALLINFO_DECLARED 1
diff --git a/libc/include/math.h b/libc/include/math.h
index 97539d7..eae1d3b 100644
--- a/libc/include/math.h
+++ b/libc/include/math.h
@@ -127,13 +127,6 @@
 #endif
 #endif /* __BSD_VISIBLE */
 
-/* scalbln, scalbnf, and scalblnl were unavailable on x86 until API level 18 */
-#if defined(__i386__)
-#define __INTRODUCED_IN_X86 __INTRODUCED_IN
-#else
-#define __INTRODUCED_IN_X86(x)
-#endif
-
 /*
  * Most of these functions depend on the rounding mode and have the side
  * effect of raising floating-point exceptions, so they are not declared
@@ -212,7 +205,10 @@
 double	logb(double);
 long	lrint(double);
 long	lround(double);
-double nan(const char*) __pure2 __INTRODUCED_IN(13);
+
+double nan(const char*) __pure2 __INTRODUCED_IN_ARM(13) __INTRODUCED_IN_MIPS(13)
+    __INTRODUCED_IN_X86(9);
+
 double	nextafter(double, double);
 double	remainder(double, double);
 double	remquo(double, double, int *);
@@ -243,8 +239,8 @@
 double	fmin(double, double) __pure2;
 double	nearbyint(double);
 double	round(double);
-double	scalbln(double, long);
-double scalbn(double, int) __INTRODUCED_IN_X86(18);
+double scalbln(double, long) __INTRODUCED_IN_X86(18);
+double scalbn(double, int);
 double	tgamma(double);
 double	trunc(double);
 #endif
@@ -310,7 +306,7 @@
 float	erfcf(float);
 float	hypotf(float, float);
 float	lgammaf(float);
-float tgammaf(float) __INTRODUCED_IN(13);
+float tgammaf(float) __INTRODUCED_IN_ARM(13) __INTRODUCED_IN_MIPS(13) __INTRODUCED_IN_X86(9);
 
 float	acoshf(float);
 float	asinhf(float);
@@ -322,14 +318,15 @@
 long long llroundf(float);
 long	lrintf(float);
 long	lroundf(float);
-float nanf(const char*) __pure2 __INTRODUCED_IN(13);
+float nanf(const char*) __pure2 __INTRODUCED_IN_ARM(13) __INTRODUCED_IN_MIPS(13)
+    __INTRODUCED_IN_X86(9);
 float	nearbyintf(float);
 float	nextafterf(float, float);
 float	remainderf(float, float);
 float	remquof(float, float, int *);
 float	rintf(float);
-float	scalblnf(float, long);
-float scalbnf(float, int) __INTRODUCED_IN_X86(18);
+float	scalblnf(float, long) __INTRODUCED_IN_X86(18);
+float scalbnf(float, int);
 float	truncf(float);
 
 float	fdimf(float, float);
@@ -421,8 +418,8 @@
 long double remquol(long double, long double, int*) __INTRODUCED_IN(21);
 long double rintl(long double) __INTRODUCED_IN(21);
 long double	roundl(long double);
-long double	scalblnl(long double, long);
-long double scalbnl(long double, int) __INTRODUCED_IN_X86(18);
+long double scalblnl(long double, long) __INTRODUCED_IN_X86(18);
+long double scalbnl(long double, int);
 long double sinhl(long double) __INTRODUCED_IN(21);
 long double sinl(long double) __INTRODUCED_IN(21);
 long double sqrtl(long double) __INTRODUCED_IN(21);
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index b4a48f0..3262bd1 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -133,7 +133,7 @@
 
 __BEGIN_DECLS
 
-int pthread_atfork(void (*)(void), void (*)(void), void (*)(void)) __INTRODUCED_IN(21);
+int pthread_atfork(void (*)(void), void (*)(void), void (*)(void)) __INTRODUCED_IN(12);
 
 int pthread_attr_destroy(pthread_attr_t* _Nonnull);
 int pthread_attr_getdetachstate(const pthread_attr_t* _Nonnull, int* _Nonnull);
diff --git a/libc/include/pwd.h b/libc/include/pwd.h
index b45780e..e881129 100644
--- a/libc/include/pwd.h
+++ b/libc/include/pwd.h
@@ -122,8 +122,8 @@
 void setpwent(void) __INTRODUCED_IN_FUTURE;
 void endpwent(void) __INTRODUCED_IN_FUTURE;
 
-int getpwnam_r(const char*, struct passwd*, char*, size_t, struct passwd**) __INTRODUCED_IN(21);
-int getpwuid_r(uid_t, struct passwd*, char*, size_t, struct passwd**) __INTRODUCED_IN(21);
+int getpwnam_r(const char*, struct passwd*, char*, size_t, struct passwd**) __INTRODUCED_IN(12);
+int getpwuid_r(uid_t, struct passwd*, char*, size_t, struct passwd**) __INTRODUCED_IN(12);
 
 __END_DECLS
 
diff --git a/libc/include/sched.h b/libc/include/sched.h
index a1f1dfa..7cb11a7 100644
--- a/libc/include/sched.h
+++ b/libc/include/sched.h
@@ -52,9 +52,10 @@
 
 #if defined(__USE_GNU)
 
-extern int clone(int (*)(void*), void*, int, void*, ...);
-extern int unshare(int) __INTRODUCED_IN(21);
-extern int sched_getcpu(void) __INTRODUCED_IN(21);
+extern 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);
 
 #ifdef __LP64__
@@ -72,9 +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(21);
-
-extern int sched_getaffinity(pid_t pid, size_t setsize, cpu_set_t* set) __INTRODUCED_IN(21);
+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);
 
 #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(21);
-extern void __sched_cpufree(cpu_set_t* set) __INTRODUCED_IN(21);
+extern cpu_set_t* __sched_cpualloc(size_t count) __INTRODUCED_IN(12);
+extern 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(21);
+extern int __sched_cpucount(size_t setsize, cpu_set_t* set) __INTRODUCED_IN(12);
 
 #endif /* __USE_GNU */
 
diff --git a/libc/include/search.h b/libc/include/search.h
index 1ac6d21..dc160e9 100644
--- a/libc/include/search.h
+++ b/libc/include/search.h
@@ -38,10 +38,10 @@
   __INTRODUCED_IN(21);
 
 void* tdelete(const void* __restrict, void** __restrict, int (*)(const void*, const void*))
-  __INTRODUCED_IN(21);
-void tdestroy(void*, void (*)(void*)) __INTRODUCED_IN(21);
-void* tfind(const void*, void* const*, int (*)(const void*, const void*)) __INTRODUCED_IN(21);
-void* tsearch(const void*, void**, int (*)(const void*, const void*)) __INTRODUCED_IN(21);
+  __INTRODUCED_IN(16);
+void tdestroy(void*, void (*)(void*)) __INTRODUCED_IN(16);
+void* tfind(const void*, void* const*, int (*)(const void*, const void*)) __INTRODUCED_IN(16);
+void* tsearch(const void*, void**, int (*)(const void*, const void*)) __INTRODUCED_IN(16);
 void twalk(const void*, void (*)(const void*, VISIT, int)) __INTRODUCED_IN(21);
 
 __END_DECLS
diff --git a/libc/include/setjmp.h b/libc/include/setjmp.h
index 02b06f5..7adeb35 100644
--- a/libc/include/setjmp.h
+++ b/libc/include/setjmp.h
@@ -54,8 +54,10 @@
 int     setjmp(jmp_buf);
 void    longjmp(jmp_buf, int);
 
-int     sigsetjmp(sigjmp_buf, int);
-void    siglongjmp(sigjmp_buf, int);
+int sigsetjmp(sigjmp_buf, int) __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_MIPS(12)
+    __INTRODUCED_IN_X86(12);
+void siglongjmp(sigjmp_buf, int) __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_MIPS(12)
+    __INTRODUCED_IN_X86(12);
 
 __END_DECLS
 
diff --git a/libc/include/signal.h b/libc/include/signal.h
index d9f43de..3905d86 100644
--- a/libc/include/signal.h
+++ b/libc/include/signal.h
@@ -144,8 +144,8 @@
 
 extern int sigaltstack(const stack_t*, stack_t*);
 
-extern void psiginfo(const siginfo_t*, const char*) __INTRODUCED_IN(21);
-extern void psignal(int, const char*) __INTRODUCED_IN(21);
+extern void psiginfo(const siginfo_t*, const char*) __INTRODUCED_IN(17);
+extern 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*);
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index 05f2d98..83ffc20 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -119,8 +119,8 @@
 size_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
 int	 getc(FILE *);
 int	 getchar(void);
-ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(21);
-ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(21);
+ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18);
+ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18);
 
 void	 perror(const char *);
 int	 printf(const char * __restrict, ...)
@@ -280,7 +280,7 @@
 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
 #endif /* __BSD_VISIBLE */
 
-extern char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(21);
+extern char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
 extern 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");
diff --git a/libc/include/string.h b/libc/include/string.h
index f39a86b..b3ea9d6 100644
--- a/libc/include/string.h
+++ b/libc/include/string.h
@@ -54,7 +54,7 @@
 extern void*  memmem(const void *, size_t, const void *, size_t) __purefunc;
 
 extern char*  strchr(const char *, int) __purefunc;
-extern char* __strchr_chk(const char*, int, size_t) __INTRODUCED_IN(21);
+extern char* __strchr_chk(const char*, int, size_t) __INTRODUCED_IN(18);
 #if defined(__USE_GNU)
 #if defined(__cplusplus)
 extern "C++" char* strchrnul(char*, int) __RENAME(strchrnul) __purefunc;
@@ -65,10 +65,10 @@
 #endif
 
 extern char*  strrchr(const char *, int) __purefunc;
-extern char* __strrchr_chk(const char*, int, size_t) __INTRODUCED_IN(21);
+extern char* __strrchr_chk(const char*, int, size_t) __INTRODUCED_IN(18);
 
 extern size_t strlen(const char *) __purefunc;
-extern size_t __strlen_chk(const char*, size_t) __INTRODUCED_IN(21);
+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);
@@ -139,10 +139,10 @@
 extern char* __strncpy_chk2(char* __restrict, const char* __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(21);
+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(21);
+  __INTRODUCED_IN(17);
 
 #if defined(__BIONIC_FORTIFY)
 
diff --git a/libc/include/strings.h b/libc/include/strings.h
index ca9f658..021e2b4 100644
--- a/libc/include/strings.h
+++ b/libc/include/strings.h
@@ -54,11 +54,7 @@
 #define bzero(b, len) (void)(__builtin_memset((b), '\0', (len)))
 #endif
 
-#if defined(__i386__)
-int ffs(int) __INTRODUCED_IN(21);
-#else
-int ffs(int);
-#endif
+int ffs(int) __INTRODUCED_IN_X86(18);
 
 __END_DECLS
 
diff --git a/libc/include/sys/auxv.h b/libc/include/sys/auxv.h
index 4611fcc..2fa637e 100644
--- a/libc/include/sys/auxv.h
+++ b/libc/include/sys/auxv.h
@@ -33,7 +33,7 @@
 
 __BEGIN_DECLS
 
-unsigned long int getauxval(unsigned long int type) __INTRODUCED_IN(21);
+unsigned long int getauxval(unsigned long int type) __INTRODUCED_IN(18);
 
 __END_DECLS
 
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index 7b06967..fa03207 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -501,6 +501,24 @@
 #define __INTRODUCED_IN_64(api_level)
 #endif
 
+#if defined(__arm__)
+#define __INTRODUCED_IN_ARM __INTRODUCED_IN
+#else
+#define __INTRODUCED_IN_ARM(x)
+#endif
+
+#if defined(__i386__)
+#define __INTRODUCED_IN_X86 __INTRODUCED_IN
+#else
+#define __INTRODUCED_IN_X86(x)
+#endif
+
+#if defined(__mips__)
+#define __INTRODUCED_IN_MIPS __INTRODUCED_IN
+#else
+#define __INTRODUCED_IN_MIPS(x)
+#endif
+
 #if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5
 #if __LP64__
 #define __size_mul_overflow(a, b, result) __builtin_umull_overflow(a, b, result)
diff --git a/libc/include/sys/mman.h b/libc/include/sys/mman.h
index af96eff..7a8aa89 100644
--- a/libc/include/sys/mman.h
+++ b/libc/include/sys/mman.h
@@ -61,8 +61,8 @@
 extern int mprotect(const void*, size_t, int);
 extern void* mremap(void*, size_t, size_t, int, ...);
 
-extern int mlockall(int) __INTRODUCED_IN(21);
-extern int munlockall(void) __INTRODUCED_IN(21);
+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);
 
diff --git a/libc/include/sys/personality.h b/libc/include/sys/personality.h
index 68ae77c..15a5e68 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(21);
+extern int personality(unsigned int persona) __INTRODUCED_IN(15);
 
 __END_DECLS
 
diff --git a/libc/include/sys/signalfd.h b/libc/include/sys/signalfd.h
index 084a528..41036b0 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(21);
+extern int signalfd(int fd, const sigset_t* _Nonnull mask, int flags) __INTRODUCED_IN(18);
 
 __END_DECLS
 
diff --git a/libc/include/sys/stat.h b/libc/include/sys/stat.h
index 46eec96..f26a492 100644
--- a/libc/include/sys/stat.h
+++ b/libc/include/sys/stat.h
@@ -157,7 +157,7 @@
 extern int mknod(const char*, mode_t, dev_t);
 extern mode_t umask(mode_t);
 
-extern mode_t __umask_chk(mode_t) __INTRODUCED_IN(21);
+extern mode_t __umask_chk(mode_t) __INTRODUCED_IN(18);
 extern mode_t __umask_real(mode_t) __RENAME(umask);
 __errordecl(__umask_invalid_mode, "umask called with invalid mode");
 
diff --git a/libc/include/sys/statvfs.h b/libc/include/sys/statvfs.h
index 1f00b9d..f6b9803 100644
--- a/libc/include/sys/statvfs.h
+++ b/libc/include/sys/statvfs.h
@@ -60,10 +60,10 @@
 #define ST_RELATIME    0x1000
 
 extern int statvfs(const char* __restrict _Nonnull, struct statvfs* __restrict _Nonnull)
-  __INTRODUCED_IN(21);
+  __INTRODUCED_IN(19);
 extern int statvfs64(const char* __restrict _Nonnull, struct statvfs64* __restrict _Nonnull)
   __INTRODUCED_IN(21);
-extern int fstatvfs(int, struct statvfs* _Nonnull) __INTRODUCED_IN(21);
+extern int fstatvfs(int, struct statvfs* _Nonnull) __INTRODUCED_IN(19);
 extern 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 fe14a30..2d99df8 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(21);
-extern int swapoff(const char* _Nonnull) __INTRODUCED_IN(21);
+extern int swapon(const char* _Nonnull, int) __INTRODUCED_IN(19);
+extern int swapoff(const char* _Nonnull) __INTRODUCED_IN(19);
 
 __END_DECLS
 
diff --git a/libc/include/sys/system_properties.h b/libc/include/sys/system_properties.h
index 99a6e59..d3e0d8d 100644
--- a/libc/include/sys/system_properties.h
+++ b/libc/include/sys/system_properties.h
@@ -48,7 +48,7 @@
 
 /* Set a system property by name.
 **/
-int __system_property_set(const char* key, const char* value) __INTRODUCED_IN(21);
+int __system_property_set(const char* key, const char* value) __INTRODUCED_IN(12);
 
 /* Return a pointer to the system property named name, if it
 ** exists, or NULL if there is no such property.  Use 
@@ -94,7 +94,7 @@
 ** not a bug.
 */
 int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie)
-  __INTRODUCED_IN(21);
+  __INTRODUCED_IN(19);
 
 __END_DECLS
 
diff --git a/libc/include/sys/timerfd.h b/libc/include/sys/timerfd.h
index a189742..8311f08 100644
--- a/libc/include/sys/timerfd.h
+++ b/libc/include/sys/timerfd.h
@@ -41,10 +41,10 @@
 #define TFD_CLOEXEC O_CLOEXEC
 #define TFD_NONBLOCK O_NONBLOCK
 
-extern int timerfd_create(clockid_t, int) __INTRODUCED_IN(21);
+extern int timerfd_create(clockid_t, int) __INTRODUCED_IN(19);
 extern int timerfd_settime(int, int, const struct itimerspec*, struct itimerspec*)
-  __INTRODUCED_IN(21);
-extern int timerfd_gettime(int, struct itimerspec*) __INTRODUCED_IN(21);
+  __INTRODUCED_IN(19);
+extern int timerfd_gettime(int, struct itimerspec*) __INTRODUCED_IN(19);
 
 __END_DECLS
 
diff --git a/libc/include/sys/wait.h b/libc/include/sys/wait.h
index aba20e5..e0afabc 100644
--- a/libc/include/sys/wait.h
+++ b/libc/include/sys/wait.h
@@ -51,7 +51,7 @@
 
 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(19);
+extern 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
diff --git a/libc/include/sys/xattr.h b/libc/include/sys/xattr.h
index 070913f..63605c5 100644
--- a/libc/include/sys/xattr.h
+++ b/libc/include/sys/xattr.h
@@ -36,25 +36,25 @@
 #define XATTR_REPLACE 2
 
 extern int fsetxattr(int fd, const char* name, const void* value, size_t size, int flags)
-  __INTRODUCED_IN(21);
+  __INTRODUCED_IN(16);
 extern int setxattr(const char* path, const char* name, const void* value, size_t size, int flags)
-  __INTRODUCED_IN(21);
+  __INTRODUCED_IN(16);
 extern int lsetxattr(const char* path, const char* name, const void* value, size_t size, int flags)
-  __INTRODUCED_IN(21);
+  __INTRODUCED_IN(16);
 
-extern ssize_t fgetxattr(int fd, const char* name, void* value, size_t size) __INTRODUCED_IN(21);
+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(21);
+  __INTRODUCED_IN(16);
 extern ssize_t lgetxattr(const char* path, const char* name, void* value, size_t size)
-  __INTRODUCED_IN(21);
+  __INTRODUCED_IN(16);
 
-extern ssize_t listxattr(const char* path, char* list, size_t size) __INTRODUCED_IN(21);
-extern ssize_t llistxattr(const char* path, char* list, size_t size) __INTRODUCED_IN(21);
-extern ssize_t flistxattr(int fd, char* list, size_t size) __INTRODUCED_IN(21);
+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);
 
-extern int removexattr(const char* path, const char* name) __INTRODUCED_IN(21);
-extern int lremovexattr(const char* path, const char* name) __INTRODUCED_IN(21);
-extern int fremovexattr(int fd, const char* name) __INTRODUCED_IN(21);
+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);
 
 __END_DECLS
 
diff --git a/libc/include/time.h b/libc/include/time.h
index 039608b..ef4ac2f 100644
--- a/libc/include/time.h
+++ b/libc/include/time.h
@@ -100,8 +100,8 @@
 extern int timer_getoverrun(timer_t) __LIBC_ABI_PUBLIC__;
 
 /* Non-standard extensions that are in the BSDs and glibc. */
-extern time_t timelocal(struct tm*) __LIBC_ABI_PUBLIC__ __INTRODUCED_IN(21);
-extern time_t timegm(struct tm*) __LIBC_ABI_PUBLIC__ __INTRODUCED_IN(21);
+extern time_t timelocal(struct tm*) __LIBC_ABI_PUBLIC__ __INTRODUCED_IN(12);
+extern time_t timegm(struct tm*) __LIBC_ABI_PUBLIC__ __INTRODUCED_IN(12);
 
 __END_DECLS
 
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 71764ae..7deca95 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -90,7 +90,7 @@
 extern pid_t  getppid(void);
 extern pid_t  getpgrp(void);
 extern int    setpgrp(void);
-extern pid_t  getsid(pid_t __pid) __INTRODUCED_IN(21);
+extern pid_t  getsid(pid_t __pid) __INTRODUCED_IN(17);
 extern pid_t  setsid(void);
 
 extern int execv(const char* __path, char* const* __argv);
@@ -127,7 +127,7 @@
 
 extern int access(const char* __path, int __mode);
 extern int faccessat(int __dirfd, const char* __path, int __mode, int __flags)
-  __INTRODUCED_IN(21);
+  __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);
@@ -285,7 +285,7 @@
 extern 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);
+extern 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;
diff --git a/tools/versioner/Android.mk b/tools/versioner/Android.mk
new file mode 100644
index 0000000..3036300
--- /dev/null
+++ b/tools/versioner/Android.mk
@@ -0,0 +1,25 @@
+ifeq (true,$(FORCE_BUILD_LLVM_COMPONENTS))
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := versioner
+LOCAL_MODULE_HOST_OS := linux
+
+LOCAL_CLANG := true
+LOCAL_CFLAGS := -Wall -Wextra -Werror -Wno-unused-parameter
+LOCAL_CFLAGS += -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS
+LOCAL_CPPFLAGS := $(LOCAL_CFLAGS) -std=c++14 -fno-rtti
+
+LOCAL_SRC_FILES := \
+  src/versioner.cpp \
+  src/DeclarationDatabase.cpp \
+  src/SymbolDatabase.cpp \
+  src/Utils.cpp
+
+LOCAL_SHARED_LIBRARIES := libclang libLLVM
+
+include $(BUILD_HOST_EXECUTABLE)
+
+endif
diff --git a/tools/versioner/README.md b/tools/versioner/README.md
new file mode 100644
index 0000000..39b5128
--- /dev/null
+++ b/tools/versioner/README.md
@@ -0,0 +1,8 @@
+## versioner
+Use clang to verify the correctness of bionic's availability attributes against the NDK platform definitions.
+
+#### Build
+Build with `FORCE_BUILD_LLVM_COMPONENTS=true mma -j48`
+
+#### Use
+`versioner -p platforms current dependencies`
diff --git a/tools/versioner/current b/tools/versioner/current
new file mode 120000
index 0000000..234dfe7
--- /dev/null
+++ b/tools/versioner/current
@@ -0,0 +1 @@
+../../libc/include
\ No newline at end of file
diff --git a/tools/versioner/dependencies/arm/arch-arm b/tools/versioner/dependencies/arm/arch-arm
new file mode 120000
index 0000000..ed69f41
--- /dev/null
+++ b/tools/versioner/dependencies/arm/arch-arm
@@ -0,0 +1 @@
+../../../../libc/arch-arm/include
\ No newline at end of file
diff --git a/tools/versioner/dependencies/arm/kernel_uapi_asm-arm b/tools/versioner/dependencies/arm/kernel_uapi_asm-arm
new file mode 120000
index 0000000..fabed85
--- /dev/null
+++ b/tools/versioner/dependencies/arm/kernel_uapi_asm-arm
@@ -0,0 +1 @@
+../../../../libc/kernel/uapi/asm-arm
\ No newline at end of file
diff --git a/tools/versioner/dependencies/arm64/arch-arm64 b/tools/versioner/dependencies/arm64/arch-arm64
new file mode 120000
index 0000000..21a21d4
--- /dev/null
+++ b/tools/versioner/dependencies/arm64/arch-arm64
@@ -0,0 +1 @@
+../../../../libc/arch-arm64/include
\ No newline at end of file
diff --git a/tools/versioner/dependencies/arm64/kernel_uapi_asm-arm64 b/tools/versioner/dependencies/arm64/kernel_uapi_asm-arm64
new file mode 120000
index 0000000..16e74a3
--- /dev/null
+++ b/tools/versioner/dependencies/arm64/kernel_uapi_asm-arm64
@@ -0,0 +1 @@
+../../../../libc/kernel/uapi/asm-arm64
\ No newline at end of file
diff --git a/tools/versioner/dependencies/common/clang-builtins b/tools/versioner/dependencies/common/clang-builtins
new file mode 120000
index 0000000..fc27e65
--- /dev/null
+++ b/tools/versioner/dependencies/common/clang-builtins
@@ -0,0 +1 @@
+../../../../../external/clang/lib/Headers
\ No newline at end of file
diff --git a/tools/versioner/dependencies/common/kernel_common b/tools/versioner/dependencies/common/kernel_common
new file mode 120000
index 0000000..1ea6a74
--- /dev/null
+++ b/tools/versioner/dependencies/common/kernel_common
@@ -0,0 +1 @@
+../../../../libc/kernel/common
\ No newline at end of file
diff --git a/tools/versioner/dependencies/common/kernel_uapi b/tools/versioner/dependencies/common/kernel_uapi
new file mode 120000
index 0000000..2a915ef
--- /dev/null
+++ b/tools/versioner/dependencies/common/kernel_uapi
@@ -0,0 +1 @@
+../../../../libc/kernel/uapi
\ No newline at end of file
diff --git a/tools/versioner/dependencies/mips/arch-mips b/tools/versioner/dependencies/mips/arch-mips
new file mode 120000
index 0000000..7f2f104
--- /dev/null
+++ b/tools/versioner/dependencies/mips/arch-mips
@@ -0,0 +1 @@
+../../../../libc/arch-mips/include
\ No newline at end of file
diff --git a/tools/versioner/dependencies/mips/kernel_uapi_asm-mips b/tools/versioner/dependencies/mips/kernel_uapi_asm-mips
new file mode 120000
index 0000000..dcd1955
--- /dev/null
+++ b/tools/versioner/dependencies/mips/kernel_uapi_asm-mips
@@ -0,0 +1 @@
+../../../../libc/kernel/uapi/asm-mips
\ No newline at end of file
diff --git a/tools/versioner/dependencies/mips64/arch-mips64 b/tools/versioner/dependencies/mips64/arch-mips64
new file mode 120000
index 0000000..48cea72
--- /dev/null
+++ b/tools/versioner/dependencies/mips64/arch-mips64
@@ -0,0 +1 @@
+../../../../libc/arch-mips64/include
\ No newline at end of file
diff --git a/tools/versioner/dependencies/mips64/kernel_uapi_asm-mips b/tools/versioner/dependencies/mips64/kernel_uapi_asm-mips
new file mode 120000
index 0000000..dcd1955
--- /dev/null
+++ b/tools/versioner/dependencies/mips64/kernel_uapi_asm-mips
@@ -0,0 +1 @@
+../../../../libc/kernel/uapi/asm-mips
\ No newline at end of file
diff --git a/tools/versioner/dependencies/x86/arch-x86 b/tools/versioner/dependencies/x86/arch-x86
new file mode 120000
index 0000000..d0f016a
--- /dev/null
+++ b/tools/versioner/dependencies/x86/arch-x86
@@ -0,0 +1 @@
+../../../../libc/arch-x86/include
\ No newline at end of file
diff --git a/tools/versioner/dependencies/x86/kernel_uapi_asm-x86 b/tools/versioner/dependencies/x86/kernel_uapi_asm-x86
new file mode 120000
index 0000000..0efae62
--- /dev/null
+++ b/tools/versioner/dependencies/x86/kernel_uapi_asm-x86
@@ -0,0 +1 @@
+../../../../libc/kernel/uapi/asm-x86
\ No newline at end of file
diff --git a/tools/versioner/dependencies/x86_64/arch-x86_64 b/tools/versioner/dependencies/x86_64/arch-x86_64
new file mode 120000
index 0000000..deb647d
--- /dev/null
+++ b/tools/versioner/dependencies/x86_64/arch-x86_64
@@ -0,0 +1 @@
+../../../../libc/arch-x86_64/include
\ No newline at end of file
diff --git a/tools/versioner/dependencies/x86_64/kernel_uapi_asm-x86 b/tools/versioner/dependencies/x86_64/kernel_uapi_asm-x86
new file mode 120000
index 0000000..0efae62
--- /dev/null
+++ b/tools/versioner/dependencies/x86_64/kernel_uapi_asm-x86
@@ -0,0 +1 @@
+../../../../libc/kernel/uapi/asm-x86
\ No newline at end of file
diff --git a/tools/versioner/platforms b/tools/versioner/platforms
new file mode 120000
index 0000000..bcb7da8
--- /dev/null
+++ b/tools/versioner/platforms
@@ -0,0 +1 @@
+../../../development/ndk/platforms
\ No newline at end of file
diff --git a/tools/versioner/run_tests.py b/tools/versioner/run_tests.py
new file mode 100644
index 0000000..f5a31f2
--- /dev/null
+++ b/tools/versioner/run_tests.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python3
+
+import os
+import subprocess
+import sys
+
+red = '\033[91m'
+green = '\033[92m'
+bold = '\033[1m'
+reset = '\033[0m'
+prefix_pass = bold + "[" + green + "PASS" + reset + bold + "]" + reset
+prefix_fail = bold + "[" + red + "FAIL" + reset + bold + "]" + reset
+
+
+def indent(text, spaces=4):
+    prefix = "    "
+    return "\n".join([prefix + line for line in text.split("\n")])
+
+
+def run_test(test_name, path):
+    os.chdir(path)
+    process = subprocess.Popen(
+        ["/bin/sh", "run.sh"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    (output, error) = process.communicate()
+
+    if os.path.exists("expected_fail"):
+        with open("expected_fail") as f:
+            expected_output = f.read()
+            if output != expected_output:
+                print("{} {}: expected output mismatch".format(
+                    prefix_fail, test_name))
+                print("")
+                print("  Expected:")
+                print(indent(expected_output))
+                print("  Actual:")
+                print(indent(output))
+                return False
+    elif process.returncode != 0:
+        print("{} {}: unexpected failure:".format(prefix_fail, test_name))
+        print("")
+        print(indent(output))
+        return False
+
+    print("{} {}".format(prefix_pass, test_name))
+    return True
+
+root_dir = os.path.dirname(os.path.realpath(__file__))
+test_dir = os.path.join(root_dir, "tests")
+tests = os.listdir(test_dir)
+
+success = True
+for test in sorted(tests):
+    if not run_test(test, os.path.join(test_dir, test)):
+        success = False
+
+sys.exit(0 if success else 1)
diff --git a/tools/versioner/src/DeclarationDatabase.cpp b/tools/versioner/src/DeclarationDatabase.cpp
new file mode 100644
index 0000000..9f02588
--- /dev/null
+++ b/tools/versioner/src/DeclarationDatabase.cpp
@@ -0,0 +1,179 @@
+/*
+ * Copyright 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 "DeclarationDatabase.h"
+
+#include <iostream>
+#include <map>
+#include <set>
+#include <string>
+
+#include <clang/AST/AST.h>
+#include <clang/AST/Attr.h>
+#include <clang/AST/Mangle.h>
+#include <clang/AST/RecursiveASTVisitor.h>
+#include <clang/Frontend/ASTUnit.h>
+#include <llvm/Support/raw_ostream.h>
+
+using namespace clang;
+
+class Visitor : public RecursiveASTVisitor<Visitor> {
+  HeaderDatabase& database;
+  SourceManager& src_manager;
+  std::unique_ptr<MangleContext> mangler;
+
+ public:
+  Visitor(HeaderDatabase& database, ASTContext& ctx)
+      : database(database), src_manager(ctx.getSourceManager()) {
+    mangler.reset(ItaniumMangleContext::create(ctx, ctx.getDiagnostics()));
+  }
+
+  std::string getDeclName(NamedDecl* decl) {
+    if (auto var_decl = dyn_cast<VarDecl>(decl)) {
+      if (!var_decl->isFileVarDecl()) {
+        return "<local var>";
+      }
+    }
+
+    if (mangler->shouldMangleDeclName(decl)) {
+      std::string mangled;
+      llvm::raw_string_ostream ss(mangled);
+      mangler->mangleName(decl, ss);
+      return mangled;
+    }
+
+    auto identifier = decl->getIdentifier();
+    if (!identifier) {
+      return "<error>";
+    }
+    return identifier->getName();
+  }
+
+  bool VisitDecl(Decl* decl) {
+    // Skip declarations inside of functions (function arguments, variable declarations inside of
+    // inline functions, etc).
+    if (decl->getParentFunctionOrMethod()) {
+      return true;
+    }
+
+    auto named_decl = dyn_cast<NamedDecl>(decl);
+    if (!named_decl) {
+      return true;
+    }
+
+    DeclarationType declaration_type;
+    std::string declaration_name = getDeclName(named_decl);
+    bool is_extern = named_decl->getFormalLinkage() == ExternalLinkage;
+    bool is_definition = false;
+
+    if (auto function_decl = dyn_cast<FunctionDecl>(decl)) {
+      declaration_type = DeclarationType::function;
+      is_definition = function_decl->isThisDeclarationADefinition();
+    } else if (auto var_decl = dyn_cast<VarDecl>(decl)) {
+      if (!var_decl->isFileVarDecl()) {
+        return true;
+      }
+
+      declaration_type = DeclarationType::variable;
+      switch (var_decl->isThisDeclarationADefinition()) {
+        case VarDecl::DeclarationOnly:
+          is_definition = false;
+          break;
+
+        case VarDecl::Definition:
+          is_definition = true;
+          break;
+
+        case VarDecl::TentativeDefinition:
+          // Forbid tentative definitions in headers.
+          fprintf(stderr, "ERROR: declaration '%s' is a tentative definition\n",
+                  declaration_name.c_str());
+          decl->dump();
+          abort();
+      }
+    } else {
+      // We only care about function and variable declarations.
+      return true;
+    }
+
+    if (decl->hasAttr<UnavailableAttr>()) {
+      // Skip declarations that exist only for compile-time diagnostics.
+      return true;
+    }
+
+    // Look for availability annotations.
+    DeclarationAvailability availability;
+    for (const AvailabilityAttr* attr : decl->specific_attrs<AvailabilityAttr>()) {
+      if (attr->getPlatform()->getName() != "android") {
+        fprintf(stderr, "skipping non-android platform %s\n",
+                attr->getPlatform()->getName().str().c_str());
+        continue;
+      }
+      if (attr->getIntroduced().getMajor() != 0) {
+        availability.introduced = attr->getIntroduced().getMajor();
+      }
+      if (attr->getDeprecated().getMajor() != 0) {
+        availability.deprecated = attr->getDeprecated().getMajor();
+      }
+      if (attr->getObsoleted().getMajor() != 0) {
+        availability.obsoleted = attr->getObsoleted().getMajor();
+      }
+    }
+
+    // Find or insert an entry for the declaration.
+    auto declaration_it = database.declarations.find(declaration_name);
+    if (declaration_it == database.declarations.end()) {
+      Declaration declaration = {.name = declaration_name };
+      bool inserted;
+      std::tie(declaration_it, inserted) =
+          database.declarations.insert({ declaration_name, declaration });
+    }
+
+    auto& declaration_locations = declaration_it->second.locations;
+    auto presumed_loc = src_manager.getPresumedLoc(decl->getLocation());
+    DeclarationLocation location = {
+      .filename = presumed_loc.getFilename(),
+      .line_number = presumed_loc.getLine(),
+      .column = presumed_loc.getColumn(),
+      .type = declaration_type,
+      .is_extern = is_extern,
+      .is_definition = is_definition,
+      .availability = availability,
+    };
+
+    // It's fine if the location is already there, we'll get an iterator to the existing element.
+    auto location_it = declaration_locations.begin();
+    bool inserted = false;
+    std::tie(location_it, inserted) = declaration_locations.insert(location);
+
+    // If we didn't insert, check to see if the availability attributes are identical.
+    if (!inserted) {
+      if (location_it->availability != availability) {
+        fprintf(stderr, "ERROR: availability attribute mismatch\n");
+        decl->dump();
+        abort();
+      }
+    }
+
+    return true;
+  }
+};
+
+void HeaderDatabase::parseAST(ASTUnit* ast) {
+  ASTContext& ctx = ast->getASTContext();
+  Visitor visitor(*this, ctx);
+  visitor.TraverseDecl(ctx.getTranslationUnitDecl());
+}
diff --git a/tools/versioner/src/DeclarationDatabase.h b/tools/versioner/src/DeclarationDatabase.h
new file mode 100644
index 0000000..057e416
--- /dev/null
+++ b/tools/versioner/src/DeclarationDatabase.h
@@ -0,0 +1,211 @@
+/*
+ * Copyright 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.
+ */
+
+#pragma once
+
+#include <iostream>
+#include <map>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <llvm/ADT/StringRef.h>
+
+#include "Utils.h"
+
+enum class DeclarationType {
+  function,
+  variable,
+  inconsistent,
+};
+
+static const char* declarationTypeName(DeclarationType type) {
+  switch (type) {
+    case DeclarationType::function:
+      return "function";
+    case DeclarationType::variable:
+      return "variable";
+    case DeclarationType::inconsistent:
+      return "inconsistent";
+  }
+}
+
+struct CompilationType {
+  std::string arch;
+  int api_level;
+
+ private:
+  auto tie() const {
+    return std::tie(arch, api_level);
+  }
+
+ public:
+  bool operator<(const CompilationType& other) const {
+    return tie() < other.tie();
+  }
+
+  bool operator==(const CompilationType& other) const {
+    return tie() == other.tie();
+  }
+
+  std::string describe() const {
+    return arch + "-" + std::to_string(api_level);
+  }
+};
+
+struct DeclarationAvailability {
+  int introduced = 0;
+  int deprecated = 0;
+  int obsoleted = 0;
+
+  void dump(std::ostream& out = std::cout) const {
+    bool need_comma = false;
+    auto comma = [&out, &need_comma]() {
+      if (!need_comma) {
+        need_comma = true;
+        return;
+      }
+      out << ", ";
+    };
+
+    if (introduced != 0) {
+      comma();
+      out << "introduced = " << introduced;
+    }
+    if (deprecated != 0) {
+      comma();
+      out << "deprecated = " << deprecated;
+    }
+    if (obsoleted != 0) {
+      comma();
+      out << "obsoleted = " << obsoleted;
+    }
+  }
+
+  bool empty() const {
+    return !(introduced || deprecated || obsoleted);
+  }
+
+  auto tie() const {
+    return std::tie(introduced, deprecated, obsoleted);
+  }
+
+  bool operator==(const DeclarationAvailability& rhs) const {
+    return this->tie() == rhs.tie();
+  }
+
+  bool operator!=(const DeclarationAvailability& rhs) const {
+    return !(*this == rhs);
+  }
+
+  std::string describe() const {
+    return std::string("[") + std::to_string(introduced) + "," + std::to_string(deprecated) + "," +
+           std::to_string(obsoleted) + "]";
+  }
+};
+
+struct DeclarationLocation {
+  std::string filename;
+  unsigned line_number;
+  unsigned column;
+  DeclarationType type;
+  bool is_extern;
+  bool is_definition;
+  DeclarationAvailability availability;
+
+  auto tie() const {
+    return std::tie(filename, line_number, column, type, is_extern, is_definition);
+  }
+
+  bool operator<(const DeclarationLocation& other) const {
+    return tie() < other.tie();
+  }
+
+  bool operator==(const DeclarationLocation& other) const {
+    return tie() == other.tie();
+  }
+};
+
+struct Declaration {
+  std::string name;
+  std::set<DeclarationLocation> locations;
+
+  bool hasDefinition() const {
+    for (const auto& location : locations) {
+      if (location.is_definition) {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  DeclarationType type() const {
+    DeclarationType result = locations.begin()->type;
+    for (const DeclarationLocation& location : locations) {
+      if (location.type != result) {
+        result = DeclarationType::inconsistent;
+      }
+    }
+    return result;
+  }
+
+  void dump(const std::string& base_path = "", std::ostream& out = std::cout) const {
+    out << "    " << name << " declared in " << locations.size() << " locations:\n";
+    for (const DeclarationLocation& location : locations) {
+      const char* var_type = declarationTypeName(location.type);
+      const char* declaration_type = location.is_definition ? "definition" : "declaration";
+      const char* linkage = location.is_extern ? "extern" : "static";
+
+      std::string filename;
+      if (llvm::StringRef(location.filename).startswith(base_path)) {
+        filename = location.filename.substr(base_path.size());
+      } else {
+        filename = location.filename;
+      }
+
+      out << "        " << linkage << " " << var_type << " " << declaration_type << " @ "
+          << filename << ":" << location.line_number << ":" << location.column;
+
+      if (!location.availability.empty()) {
+        out << "\t[";
+        location.availability.dump(out);
+        out << "]";
+      } else {
+        out << "\t[no availability]";
+      }
+
+      out << "\n";
+    }
+  }
+};
+
+namespace clang {
+class ASTUnit;
+}
+
+class HeaderDatabase {
+ public:
+  std::map<std::string, Declaration> declarations;
+
+  void parseAST(clang::ASTUnit* ast);
+
+  void dump(const std::string& base_path = "", std::ostream& out = std::cout) const {
+    out << "HeaderDatabase contains " << declarations.size() << " declarations:\n";
+    for (const auto& pair : declarations) {
+      pair.second.dump(base_path, out);
+    }
+  }
+};
diff --git a/tools/versioner/src/SymbolDatabase.cpp b/tools/versioner/src/SymbolDatabase.cpp
new file mode 100644
index 0000000..a35f045
--- /dev/null
+++ b/tools/versioner/src/SymbolDatabase.cpp
@@ -0,0 +1,155 @@
+/*
+ * Copyright 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 "SymbolDatabase.h"
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <fstream>
+#include <streambuf>
+#include <string>
+#include <unordered_set>
+
+#include <llvm/ADT/SmallVector.h>
+#include <llvm/ADT/StringRef.h>
+#include <llvm/Object/Binary.h>
+#include <llvm/Object/ELFObjectFile.h>
+
+#include "versioner.h"
+
+using namespace llvm;
+using namespace llvm::object;
+
+std::unordered_set<std::string> getSymbols(const std::string& filename) {
+  std::unordered_set<std::string> result;
+  auto binary = createBinary(filename);
+  if (std::error_code ec = binary.getError()) {
+    errx(1, "failed to open library at %s: %s\n", filename.c_str(), ec.message().c_str());
+  }
+
+  ELFObjectFileBase* elf = dyn_cast_or_null<ELFObjectFileBase>(binary.get().getBinary());
+  if (!elf) {
+    errx(1, "failed to parse %s as ELF", filename.c_str());
+  }
+
+  for (const ELFSymbolRef symbol : elf->getDynamicSymbolIterators()) {
+    ErrorOr<StringRef> symbol_name = symbol.getName();
+
+    if (std::error_code ec = binary.getError()) {
+      errx(1, "failed to get symbol name for symbol in %s: %s", filename.c_str(),
+           ec.message().c_str());
+    }
+
+    result.insert(symbol_name.get().str());
+  }
+
+  return result;
+}
+
+// The NDK platforms are built by copying the platform directories on top of
+// each other to build each successive API version. Thus, we need to walk
+// backwards to find each desired file.
+static std::string readPlatformFile(const CompilationType& type, llvm::StringRef platform_dir,
+                                    const std::string& filename, bool required) {
+  int api_level = type.api_level;
+  std::ifstream stream;
+  while (api_level >= arch_min_api[type.arch]) {
+    if (supported_levels.count(api_level) == 0) {
+      --api_level;
+      continue;
+    }
+
+    std::string path = std::string(platform_dir) + "/android-" + std::to_string(api_level) +
+                       "/arch-" + type.arch + "/symbols/" + filename;
+
+    stream = std::ifstream(path);
+    if (stream) {
+      return std::string(std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>());
+    }
+
+    --api_level;
+  }
+
+  if (required) {
+    errx(1, "failed to find platform file '%s' for %s", filename.c_str(), type.describe().c_str());
+  }
+
+  return std::string();
+}
+
+static std::map<std::string, NdkSymbolType> parsePlatform(const CompilationType& type,
+                                                          const std::string& platform_dir) {
+  std::map<std::string, NdkSymbolType> result;
+  std::map<std::string, bool /*required*/> wanted_files = {
+    { "libc.so.functions.txt", true },
+    { "libc.so.variables.txt", false },
+    { "libdl.so.functions.txt", false },
+    { "libm.so.functions.txt", false },
+    { "libm.so.variables.txt", false },
+  };
+
+  for (const auto& pair : wanted_files) {
+    llvm::StringRef file = pair.first;
+    bool required = pair.second;
+    NdkSymbolType symbol_type;
+    if (file.endswith(".functions.txt")) {
+      symbol_type = NdkSymbolType::function;
+    } else if (file.endswith(".variables.txt")) {
+      symbol_type = NdkSymbolType::variable;
+    } else {
+      errx(1, "internal error: unexpected platform filename '%s'\n", file.str().c_str());
+    }
+
+    std::string platform_file = readPlatformFile(type, platform_dir, file, required);
+    if (platform_file.empty()) {
+      continue;
+    }
+
+    llvm::SmallVector<llvm::StringRef, 0> symbols;
+    llvm::StringRef(platform_file).split(symbols, "\n");
+
+    for (llvm::StringRef symbol_name : symbols) {
+      if (symbol_name.empty()) {
+        continue;
+      }
+
+      if (result.count(symbol_name) != 0) {
+        if (verbose) {
+          printf("duplicated symbol '%s' in '%s'\n", symbol_name.str().c_str(), file.str().c_str());
+        }
+      }
+
+      result[symbol_name] = symbol_type;
+    }
+  }
+
+  return result;
+}
+
+NdkSymbolDatabase parsePlatforms(const std::set<CompilationType>& types,
+                                 const std::string& platform_dir) {
+  std::map<std::string, std::map<CompilationType, NdkSymbolType>> result;
+  for (const CompilationType& type : types) {
+    std::map<std::string, NdkSymbolType> symbols = parsePlatform(type, platform_dir);
+    for (const auto& it : symbols) {
+      result[it.first][type] = it.second;
+    }
+  }
+
+  return result;
+}
diff --git a/tools/versioner/src/SymbolDatabase.h b/tools/versioner/src/SymbolDatabase.h
new file mode 100644
index 0000000..3f41b98
--- /dev/null
+++ b/tools/versioner/src/SymbolDatabase.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright 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.
+ */
+
+#pragma once
+
+#include <map>
+#include <set>
+#include <string>
+#include <unordered_set>
+
+#include "DeclarationDatabase.h"
+
+using LibrarySymbolDatabase = std::unordered_set<std::string>;
+std::unordered_set<std::string> getSymbols(const std::string& filename);
+
+enum class NdkSymbolType {
+  function,
+  variable,
+};
+
+using NdkSymbolDatabase = std::map<std::string, std::map<CompilationType, NdkSymbolType>>;
+NdkSymbolDatabase parsePlatforms(const std::set<CompilationType>& types,
+                                 const std::string& platform_dir);
diff --git a/tools/versioner/src/Utils.cpp b/tools/versioner/src/Utils.cpp
new file mode 100644
index 0000000..92cc9de
--- /dev/null
+++ b/tools/versioner/src/Utils.cpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright 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 "Utils.h"
+
+#include <err.h>
+#include <fts.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <string>
+#include <vector>
+
+std::string getWorkingDir() {
+  char buf[PATH_MAX];
+  if (!getcwd(buf, sizeof(buf))) {
+    err(1, "getcwd failed");
+  }
+  return buf;
+}
+
+std::vector<std::string> collectFiles(const std::string& directory) {
+  std::vector<std::string> files;
+
+  char* dir_argv[2] = { const_cast<char*>(directory.c_str()), nullptr };
+  FTS* fts = fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr);
+
+  if (!fts) {
+    err(1, "failed to open directory '%s'", directory.c_str());
+  }
+
+  FTSENT* ent;
+  while ((ent = fts_read(fts))) {
+    if (ent->fts_info & (FTS_D | FTS_DP)) {
+      continue;
+    }
+
+    files.push_back(ent->fts_path);
+  }
+
+  fts_close(fts);
+  return files;
+}
diff --git a/tools/versioner/src/Utils.h b/tools/versioner/src/Utils.h
new file mode 100644
index 0000000..f4845b8
--- /dev/null
+++ b/tools/versioner/src/Utils.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+std::string getWorkingDir();
+std::vector<std::string> collectFiles(const std::string& directory);
+
+namespace std {
+static __attribute__((unused)) std::string to_string(const char* c) {
+  return c;
+}
+
+static __attribute__((unused)) const std::string& to_string(const std::string& str) {
+  return str;
+}
+}
+
+template <typename Collection>
+static std::string Join(Collection c, const std::string& delimiter = ", ") {
+  std::string result;
+  for (const auto& item : c) {
+    result.append(std::to_string(item));
+    result.append(delimiter);
+  }
+  if (!result.empty()) {
+    result.resize(result.length() - delimiter.length());
+  }
+  return result;
+}
diff --git a/tools/versioner/src/versioner.cpp b/tools/versioner/src/versioner.cpp
new file mode 100644
index 0000000..1612ed0
--- /dev/null
+++ b/tools/versioner/src/versioner.cpp
@@ -0,0 +1,601 @@
+/*
+ * Copyright 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 <dirent.h>
+#include <err.h>
+#include <limits.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <atomic>
+#include <iostream>
+#include <map>
+#include <memory>
+#include <set>
+#include <sstream>
+#include <string>
+#include <thread>
+#include <unordered_map>
+#include <vector>
+
+#include <clang/Frontend/TextDiagnosticPrinter.h>
+#include <clang/Tooling/Tooling.h>
+#include <llvm/ADT/StringRef.h>
+
+#include "DeclarationDatabase.h"
+#include "SymbolDatabase.h"
+#include "Utils.h"
+#include "versioner.h"
+
+using namespace std::string_literals;
+using namespace clang;
+using namespace clang::tooling;
+
+bool verbose;
+
+class HeaderCompilationDatabase : public CompilationDatabase {
+  CompilationType type;
+  std::string cwd;
+  std::vector<std::string> headers;
+  std::vector<std::string> include_dirs;
+
+ public:
+  HeaderCompilationDatabase(CompilationType type, std::string cwd, std::vector<std::string> headers,
+                            std::vector<std::string> include_dirs)
+      : type(type),
+        cwd(std::move(cwd)),
+        headers(std::move(headers)),
+        include_dirs(std::move(include_dirs)) {
+  }
+
+  CompileCommand generateCompileCommand(const std::string& filename) const {
+    std::vector<std::string> command = { "clang-tool", filename, "-nostdlibinc" };
+    for (const auto& dir : include_dirs) {
+      command.push_back("-isystem");
+      command.push_back(dir);
+    }
+    command.push_back("-std=c11");
+    command.push_back("-DANDROID");
+    command.push_back("-D__ANDROID_API__="s + std::to_string(type.api_level));
+    command.push_back("-D_FORTIFY_SOURCE=2");
+    command.push_back("-D_GNU_SOURCE");
+    command.push_back("-Wno-unknown-attributes");
+    command.push_back("-target");
+    command.push_back(arch_targets[type.arch]);
+
+    return CompileCommand(cwd, filename, command);
+  }
+
+  std::vector<CompileCommand> getAllCompileCommands() const override {
+    std::vector<CompileCommand> commands;
+    for (const std::string& file : headers) {
+      commands.push_back(generateCompileCommand(file));
+    }
+    return commands;
+  }
+
+  std::vector<CompileCommand> getCompileCommands(StringRef file) const override {
+    std::vector<CompileCommand> commands;
+    commands.push_back(generateCompileCommand(file));
+    return commands;
+  }
+
+  std::vector<std::string> getAllFiles() const override {
+    return headers;
+  }
+};
+
+struct CompilationRequirements {
+  std::vector<std::string> headers;
+  std::vector<std::string> dependencies;
+};
+
+static CompilationRequirements collectRequirements(const std::string& arch,
+                                                   const std::string& header_dir,
+                                                   const std::string& dependency_dir) {
+  std::vector<std::string> headers = collectFiles(header_dir);
+
+  std::vector<std::string> dependencies = { header_dir };
+  if (!dependency_dir.empty()) {
+    auto collect_children = [&dependencies](const std::string& dir_path) {
+      DIR* dir = opendir(dir_path.c_str());
+      if (!dir) {
+        err(1, "failed to open dependency directory '%s'", dir_path.c_str());
+      }
+
+      struct dirent* dent;
+      while ((dent = readdir(dir))) {
+        if (dent->d_name[0] == '.') {
+          continue;
+        }
+
+        // TODO: Resolve symlinks.
+        std::string dependency = dir_path + "/" + dent->d_name;
+
+        struct stat st;
+        if (stat(dependency.c_str(), &st) != 0) {
+          err(1, "failed to stat dependency '%s'", dependency.c_str());
+        }
+
+        if (!S_ISDIR(st.st_mode)) {
+          errx(1, "'%s' is not a directory", dependency.c_str());
+        }
+
+        dependencies.push_back(dependency);
+      }
+
+      closedir(dir);
+    };
+
+    collect_children(dependency_dir + "/common");
+    collect_children(dependency_dir + "/" + arch);
+  }
+
+  auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
+    for (const auto& it : header_blacklist) {
+      if (it.second.find(arch) == it.second.end()) {
+        continue;
+      }
+
+      if (header.endswith("/" + it.first)) {
+        return true;
+      }
+    }
+    return false;
+  });
+
+  headers.erase(new_end, headers.end());
+
+  CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
+  return result;
+}
+
+static std::set<CompilationType> generateCompilationTypes(
+    const std::set<std::string> selected_architectures, const std::set<int>& selected_levels) {
+  std::set<CompilationType> result;
+  for (const std::string& arch : selected_architectures) {
+    int min_api = arch_min_api[arch];
+    for (int api_level : selected_levels) {
+      if (api_level < min_api) {
+        continue;
+      }
+      CompilationType type = { .arch = arch, .api_level = api_level };
+      result.insert(type);
+    }
+  }
+  return result;
+}
+
+using DeclarationDatabase = std::map<std::string, std::map<CompilationType, Declaration>>;
+
+static DeclarationDatabase transposeHeaderDatabases(
+    const std::map<CompilationType, HeaderDatabase>& original) {
+  DeclarationDatabase result;
+  for (const auto& outer : original) {
+    const CompilationType& type = outer.first;
+    for (const auto& inner : outer.second.declarations) {
+      const std::string& symbol_name = inner.first;
+      result[symbol_name][type] = inner.second;
+    }
+  }
+  return result;
+}
+
+static DeclarationDatabase compileHeaders(const std::set<CompilationType>& types,
+                                          const std::string& header_dir,
+                                          const std::string& dependency_dir, bool* failed) {
+  constexpr size_t thread_count = 8;
+  size_t threads_created = 0;
+  std::mutex mutex;
+  std::vector<std::thread> threads(thread_count);
+
+  std::map<CompilationType, HeaderDatabase> header_databases;
+  std::unordered_map<std::string, CompilationRequirements> requirements;
+
+  std::string cwd = getWorkingDir();
+  bool errors = false;
+
+  for (const auto& type : types) {
+    if (requirements.count(type.arch) == 0) {
+      requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
+    }
+  }
+
+  for (const auto& type : types) {
+    size_t thread_id = threads_created++;
+    if (thread_id >= thread_count) {
+      thread_id = thread_id % thread_count;
+      threads[thread_id].join();
+    }
+
+    threads[thread_id] = std::thread(
+        [&](CompilationType type) {
+          const auto& req = requirements[type.arch];
+
+          HeaderDatabase database;
+          HeaderCompilationDatabase compilation_database(type, cwd, req.headers, req.dependencies);
+
+          ClangTool tool(compilation_database, req.headers);
+
+          clang::DiagnosticOptions diagnostic_options;
+          std::vector<std::unique_ptr<ASTUnit>> asts;
+          tool.buildASTs(asts);
+          for (const auto& ast : asts) {
+            clang::DiagnosticsEngine& diagnostics_engine = ast->getDiagnostics();
+            if (diagnostics_engine.getNumWarnings() || diagnostics_engine.hasErrorOccurred()) {
+              std::unique_lock<std::mutex> l(mutex);
+              errors = true;
+              printf("versioner: compilation failure for %s in %s\n", type.describe().c_str(),
+                     ast->getOriginalSourceFileName().str().c_str());
+            }
+
+            database.parseAST(ast.get());
+          }
+
+          std::unique_lock<std::mutex> l(mutex);
+          header_databases[type] = database;
+        },
+        type);
+  }
+
+  if (threads_created < thread_count) {
+    threads.resize(threads_created);
+  }
+
+  for (auto& thread : threads) {
+    thread.join();
+  }
+
+  if (errors) {
+    printf("versioner: compilation generated warnings or errors\n");
+    *failed = errors;
+  }
+
+  return transposeHeaderDatabases(header_databases);
+}
+
+static bool sanityCheck(const std::set<CompilationType>& types,
+                        const DeclarationDatabase& database) {
+  bool error = false;
+  for (auto outer : database) {
+    const std::string& symbol_name = outer.first;
+    CompilationType last_type;
+    DeclarationAvailability last_availability;
+
+    // Rely on std::set being sorted to loop through the types by architecture.
+    for (const CompilationType& type : types) {
+      auto inner = outer.second.find(type);
+      if (inner == outer.second.end()) {
+        // TODO: Check for holes.
+        continue;
+      }
+
+      const Declaration& declaration = inner->second;
+      bool found_availability = false;
+      bool availability_mismatch = false;
+      DeclarationAvailability current_availability;
+
+      // Make sure that all of the availability declarations for this symbol match.
+      for (const DeclarationLocation& location : declaration.locations) {
+        if (!found_availability) {
+          found_availability = true;
+          current_availability = location.availability;
+          continue;
+        }
+
+        if (current_availability != location.availability) {
+          availability_mismatch = true;
+          error = true;
+        }
+      }
+
+      if (availability_mismatch) {
+        printf("%s: availability mismatch for %s\n", symbol_name.c_str(), type.describe().c_str());
+        declaration.dump(getWorkingDir() + "/");
+      }
+
+      if (type.arch != last_type.arch) {
+        last_type = type;
+        last_availability = current_availability;
+        continue;
+      }
+
+      // Make sure that availability declarations are consistent across API levels for a given arch.
+      if (last_availability != current_availability) {
+        error = true;
+        printf("%s: availability mismatch between %s and %s: %s before, %s after\n",
+               symbol_name.c_str(), last_type.describe().c_str(), type.describe().c_str(),
+               last_availability.describe().c_str(), current_availability.describe().c_str());
+      }
+
+      last_type = type;
+    }
+  }
+  return !error;
+}
+
+// Check that our symbol availability declarations match the actual NDK
+// platform symbol availability.
+static bool checkVersions(const std::set<CompilationType>& types,
+                          const DeclarationDatabase& declaration_database,
+                          const NdkSymbolDatabase& symbol_database) {
+  bool failed = false;
+
+  std::map<std::string, std::set<CompilationType>> arch_types;
+  for (const CompilationType& type : types) {
+    arch_types[type.arch].insert(type);
+  }
+
+  for (const auto& outer : declaration_database) {
+    const std::string& symbol_name = outer.first;
+    const auto& compilations = outer.second;
+
+    auto platform_availability_it = symbol_database.find(symbol_name);
+    if (platform_availability_it == symbol_database.end()) {
+      // This currently has lots of false positives (__INTRODUCED_IN_FUTURE, __errordecl, functions
+      // that come from crtbegin, etc.). Only print them with verbose, because of this.
+      if (verbose) {
+        printf("%s: not available in any platform\n", symbol_name.c_str());
+      }
+      continue;
+    }
+
+    const auto& platform_availability = platform_availability_it->second;
+    std::set<CompilationType> missing_symbol;
+    std::set<CompilationType> missing_decl;
+
+    for (const CompilationType& type : types) {
+      auto it = compilations.find(type);
+      if (it == compilations.end()) {
+        missing_decl.insert(type);
+        continue;
+      }
+
+      const Declaration& declaration = it->second;
+
+      // sanityCheck ensured that the availability declarations for a given arch match.
+      DeclarationAvailability availability = declaration.locations.begin()->availability;
+      int api_level = type.api_level;
+
+      int introduced = std::max(0, availability.introduced);
+      int obsoleted = availability.obsoleted == 0 ? INT_MAX : availability.obsoleted;
+      bool decl_available = api_level >= introduced && api_level < obsoleted;
+
+      auto symbol_availability_it = platform_availability.find(type);
+      bool symbol_available = symbol_availability_it != platform_availability.end();
+      if (decl_available) {
+        if (!symbol_available) {
+          // Make sure that either it exists in the platform, or an inline definition is visible.
+          if (!declaration.hasDefinition()) {
+            missing_symbol.insert(type);
+            continue;
+          }
+        } else {
+          // Make sure that symbols declared as functions/variables actually are.
+          switch (declaration.type()) {
+            case DeclarationType::inconsistent:
+              printf("%s: inconsistent declaration type\n", symbol_name.c_str());
+              declaration.dump();
+              exit(1);
+
+            case DeclarationType::variable:
+              if (symbol_availability_it->second != NdkSymbolType::variable) {
+                printf("%s: declared as variable, exists in platform as function\n",
+                       symbol_name.c_str());
+                failed = true;
+              }
+              break;
+
+            case DeclarationType::function:
+              if (symbol_availability_it->second != NdkSymbolType::function) {
+                printf("%s: declared as function, exists in platform as variable\n",
+                       symbol_name.c_str());
+                failed = true;
+              }
+              break;
+          }
+        }
+      } else {
+        // Make sure it's not available in the platform.
+        if (symbol_availability_it != platform_availability.end()) {
+          printf("%s: symbol should be unavailable in %s (declared with availability %s)\n",
+                 symbol_name.c_str(), type.describe().c_str(), availability.describe().c_str());
+          failed = true;
+        }
+      }
+    }
+
+    // Allow declarations to be missing from an entire architecture.
+    for (const auto& arch_type : arch_types) {
+      const std::string& arch = arch_type.first;
+      bool found_all = true;
+      for (const auto& type : arch_type.second) {
+        if (missing_decl.find(type) == missing_decl.end()) {
+          found_all = false;
+          break;
+        }
+      }
+
+      if (!found_all) {
+        continue;
+      }
+
+      for (auto it = missing_decl.begin(); it != missing_decl.end();) {
+        if (it->arch == arch) {
+          it = missing_decl.erase(it);
+        } else {
+          ++it;
+        }
+      }
+    }
+
+    auto types_to_string = [](const std::set<CompilationType>& types) {
+      std::string result;
+      for (const CompilationType& type : types) {
+        result += type.describe();
+        result += ", ";
+      }
+      result.resize(result.length() - 2);
+      return result;
+    };
+
+    if (!missing_decl.empty()) {
+      printf("%s: declaration missing in %s\n", symbol_name.c_str(),
+             types_to_string(missing_decl).c_str());
+      failed = true;
+    }
+
+    if (!missing_symbol.empty()) {
+      printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
+             types_to_string(missing_symbol).c_str());
+      failed = true;
+    }
+  }
+
+  return !failed;
+}
+
+static void usage() {
+  fprintf(stderr, "Usage: versioner [OPTION]... HEADER_PATH [DEPS_PATH]\n");
+  fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
+  fprintf(stderr, "\n");
+  fprintf(stderr, "Target specification (defaults to all):\n");
+  fprintf(stderr, "  -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
+  fprintf(stderr, "    \t\tvalid levels are %s\n", Join(supported_levels).c_str());
+  fprintf(stderr, "  -r ARCH\tbuild with specified architecture (can be repeated)\n");
+  fprintf(stderr, "    \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
+  fprintf(stderr, "\n");
+  fprintf(stderr, "Validation:\n");
+  fprintf(stderr, "  -p PATH\tcompare against NDK platform at PATH\n");
+  fprintf(stderr, "  -d\t\tdump symbol availability in libraries\n");
+  fprintf(stderr, "  -v\t\tenable verbose warnings\n");
+  exit(1);
+}
+
+int main(int argc, char** argv) {
+  std::string cwd = getWorkingDir() + "/";
+  bool default_args = true;
+  std::string platform_dir;
+  std::set<std::string> selected_architectures;
+  std::set<int> selected_levels;
+
+  int c;
+  while ((c = getopt(argc, argv, "a:r:p:n:duv")) != -1) {
+    default_args = false;
+    switch (c) {
+      case 'a': {
+        char* end;
+        int api_level = strtol(optarg, &end, 10);
+        if (end == optarg || strlen(end) > 0) {
+          usage();
+        }
+
+        if (supported_levels.count(api_level) == 0) {
+          errx(1, "unsupported API level %d", api_level);
+        }
+
+        selected_levels.insert(api_level);
+        break;
+      }
+
+      case 'r': {
+        if (supported_archs.count(optarg) == 0) {
+          errx(1, "unsupported architecture: %s", optarg);
+        }
+        selected_architectures.insert(optarg);
+        break;
+      }
+
+      case 'p': {
+        if (!platform_dir.empty()) {
+          usage();
+        }
+
+        platform_dir = optarg;
+
+        struct stat st;
+        if (stat(platform_dir.c_str(), &st) != 0) {
+          err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
+        }
+        if (!S_ISDIR(st.st_mode)) {
+          errx(1, "'%s' is not a directory", optarg);
+        }
+        break;
+      }
+
+      case 'v':
+        verbose = true;
+        break;
+
+      default:
+        usage();
+        break;
+    }
+  }
+
+  if (argc - optind > 2 || optind >= argc) {
+    usage();
+  }
+
+  if (selected_levels.empty()) {
+    selected_levels = supported_levels;
+  }
+
+  if (selected_architectures.empty()) {
+    selected_architectures = supported_archs;
+  }
+
+  std::string dependencies = (argc - optind == 2) ? argv[optind + 1] : "";
+  const char* header_dir = argv[optind];
+
+  struct stat st;
+  if (stat(header_dir, &st) != 0) {
+    err(1, "failed to stat '%s'", header_dir);
+  } else if (!S_ISDIR(st.st_mode)) {
+    errx(1, "'%s' is not a directory", header_dir);
+  }
+
+  std::set<CompilationType> compilation_types;
+  DeclarationDatabase declaration_database;
+  NdkSymbolDatabase symbol_database;
+
+  compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
+
+  // Do this before compiling so that we can early exit if the platforms don't match what we
+  // expect.
+  if (!platform_dir.empty()) {
+    symbol_database = parsePlatforms(compilation_types, platform_dir);
+  }
+
+  bool failed = false;
+  declaration_database = compileHeaders(compilation_types, header_dir, dependencies, &failed);
+
+  if (!sanityCheck(compilation_types, declaration_database)) {
+    printf("versioner: sanity check failed\n");
+    failed = true;
+  }
+
+  if (!platform_dir.empty()) {
+    if (!checkVersions(compilation_types, declaration_database, symbol_database)) {
+      printf("versioner: version check failed\n");
+      failed = true;
+    }
+  }
+
+  return failed;
+}
diff --git a/tools/versioner/src/versioner.h b/tools/versioner/src/versioner.h
new file mode 100644
index 0000000..95635bb
--- /dev/null
+++ b/tools/versioner/src/versioner.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright 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.
+ */
+
+#pragma once
+
+#include <map>
+#include <set>
+#include <string>
+#include <unordered_map>
+
+extern bool verbose;
+
+static const std::set<std::string> supported_archs = {
+  "arm", "arm64", "mips", "mips64", "x86", "x86_64",
+};
+
+static std::unordered_map<std::string, std::string> arch_targets = {
+  { "arm", "arm-linux-androideabi" },
+  { "arm64", "aarch64-linux-android" },
+  { "mips", "mipsel-linux-android" },
+  { "mips64", "mips64el-linux-android" },
+  { "x86", "i686-linux-android" },
+  { "x86_64", "x86_64-linux-android" },
+};
+
+static const std::set<int> supported_levels = { 9, 12, 13, 14, 15, 16, 17, 18, 19, 21, 23, 24 };
+
+// Non-const for the convenience of being able to index with operator[].
+static std::map<std::string, int> arch_min_api = {
+  { "arm", 9 },
+  { "arm64", 21 },
+  { "mips", 9 },
+  { "mips64", 21 },
+  { "x86", 9 },
+  { "x86_64", 21 },
+};
+
+static const std::unordered_map<std::string, std::set<std::string>> header_blacklist = {
+  // Internal header.
+  { "sys/_system_properties.h", supported_archs },
+
+  // time64.h #errors when included on LP64 archs.
+  { "time64.h", { "arm64", "mips64", "x86_64" } },
+};
diff --git a/tools/versioner/tests/arch_specific/dependencies/common/foo/foodep.h b/tools/versioner/tests/arch_specific/dependencies/common/foo/foodep.h
new file mode 100644
index 0000000..9feeb6c
--- /dev/null
+++ b/tools/versioner/tests/arch_specific/dependencies/common/foo/foodep.h
@@ -0,0 +1 @@
+typedef int foo_t;
diff --git a/tools/versioner/tests/arch_specific/headers/foo.h b/tools/versioner/tests/arch_specific/headers/foo.h
new file mode 100644
index 0000000..34035b4
--- /dev/null
+++ b/tools/versioner/tests/arch_specific/headers/foo.h
@@ -0,0 +1,5 @@
+int foo();
+
+#if defined(__i386__)
+int bar();
+#endif
diff --git a/tools/versioner/tests/arch_specific/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/arch_specific/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/arch_specific/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/arch_specific/platforms/android-9/arch-x86/symbols/libc.so.functions.txt b/tools/versioner/tests/arch_specific/platforms/android-9/arch-x86/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..3bd1f0e
--- /dev/null
+++ b/tools/versioner/tests/arch_specific/platforms/android-9/arch-x86/symbols/libc.so.functions.txt
@@ -0,0 +1,2 @@
+foo
+bar
diff --git a/tools/versioner/tests/arch_specific/run.sh b/tools/versioner/tests/arch_specific/run.sh
new file mode 100644
index 0000000..3a3dda8
--- /dev/null
+++ b/tools/versioner/tests/arch_specific/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -r x86 -a 9
diff --git a/tools/versioner/tests/compilation_error/expected_fail b/tools/versioner/tests/compilation_error/expected_fail
new file mode 100644
index 0000000..62a643f
--- /dev/null
+++ b/tools/versioner/tests/compilation_error/expected_fail
@@ -0,0 +1,2 @@
+versioner: compilation failure for arm-9 in /android/aosp/bionic/tools/versioner/tests/compilation_error/headers/foo.h
+versioner: compilation generated warnings or errors
diff --git a/tools/versioner/tests/compilation_error/headers/foo.h b/tools/versioner/tests/compilation_error/headers/foo.h
new file mode 100644
index 0000000..c8c1473
--- /dev/null
+++ b/tools/versioner/tests/compilation_error/headers/foo.h
@@ -0,0 +1 @@
+#error foo
diff --git a/tools/versioner/tests/compilation_error/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/compilation_error/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/compilation_error/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/compilation_error/run.sh b/tools/versioner/tests/compilation_error/run.sh
new file mode 100644
index 0000000..d26ab70
--- /dev/null
+++ b/tools/versioner/tests/compilation_error/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -a 9
diff --git a/tools/versioner/tests/dependencies/dependencies/arm/archdep/archdep.h b/tools/versioner/tests/dependencies/dependencies/arm/archdep/archdep.h
new file mode 100644
index 0000000..2005972
--- /dev/null
+++ b/tools/versioner/tests/dependencies/dependencies/arm/archdep/archdep.h
@@ -0,0 +1 @@
+typedef int arm_t;
diff --git a/tools/versioner/tests/dependencies/dependencies/common/foo/foodep.h b/tools/versioner/tests/dependencies/dependencies/common/foo/foodep.h
new file mode 100644
index 0000000..9feeb6c
--- /dev/null
+++ b/tools/versioner/tests/dependencies/dependencies/common/foo/foodep.h
@@ -0,0 +1 @@
+typedef int foo_t;
diff --git a/tools/versioner/tests/dependencies/dependencies/x86/archdep/archdep.h b/tools/versioner/tests/dependencies/dependencies/x86/archdep/archdep.h
new file mode 100644
index 0000000..5cc7de2
--- /dev/null
+++ b/tools/versioner/tests/dependencies/dependencies/x86/archdep/archdep.h
@@ -0,0 +1 @@
+typedef int x86_t;
diff --git a/tools/versioner/tests/dependencies/headers/foo.h b/tools/versioner/tests/dependencies/headers/foo.h
new file mode 100644
index 0000000..4491f1c
--- /dev/null
+++ b/tools/versioner/tests/dependencies/headers/foo.h
@@ -0,0 +1,8 @@
+#include <archdep.h>
+#include <foodep.h>
+
+#if defined(__i386__)
+x86_t foo(foo_t);
+#elif defined(__arm__)
+arm_t foo(foo_t);
+#endif
diff --git a/tools/versioner/tests/dependencies/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/dependencies/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/dependencies/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/dependencies/platforms/android-9/arch-x86/symbols/libc.so.functions.txt b/tools/versioner/tests/dependencies/platforms/android-9/arch-x86/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/dependencies/platforms/android-9/arch-x86/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/dependencies/run.sh b/tools/versioner/tests/dependencies/run.sh
new file mode 100644
index 0000000..3a3dda8
--- /dev/null
+++ b/tools/versioner/tests/dependencies/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -r x86 -a 9
diff --git a/tools/versioner/tests/inline/headers/foo.h b/tools/versioner/tests/inline/headers/foo.h
new file mode 100644
index 0000000..7a48a72
--- /dev/null
+++ b/tools/versioner/tests/inline/headers/foo.h
@@ -0,0 +1,7 @@
+#if __ANDROID_API__ <= 9
+static int foo() __attribute__((availability(android, introduced = 9))) {
+  return 0;
+}
+#else
+int foo() __attribute__((availability(android, introduced = 9)));
+#endif
diff --git a/tools/versioner/tests/inline/platforms/android-12/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/inline/platforms/android-12/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/inline/platforms/android-12/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/inline/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/inline/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/versioner/tests/inline/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
diff --git a/tools/versioner/tests/inline/run.sh b/tools/versioner/tests/inline/run.sh
new file mode 100644
index 0000000..0277123
--- /dev/null
+++ b/tools/versioner/tests/inline/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -a 9 -a 12
diff --git a/tools/versioner/tests/inline_version_mismatch/expected_fail b/tools/versioner/tests/inline_version_mismatch/expected_fail
new file mode 100644
index 0000000..2894499
--- /dev/null
+++ b/tools/versioner/tests/inline_version_mismatch/expected_fail
@@ -0,0 +1,2 @@
+foo: availability mismatch between arm-9 and arm-12: [9,0,0] before, [10,0,0] after
+versioner: sanity check failed
diff --git a/tools/versioner/tests/inline_version_mismatch/headers/foo.h b/tools/versioner/tests/inline_version_mismatch/headers/foo.h
new file mode 100644
index 0000000..be7cc2c
--- /dev/null
+++ b/tools/versioner/tests/inline_version_mismatch/headers/foo.h
@@ -0,0 +1,7 @@
+#if __ANDROID_API__ <= 9
+static int foo() __attribute__((availability(android, introduced = 9))) {
+  return 0;
+}
+#else
+int foo() __attribute__((availability(android, introduced = 10)));
+#endif
diff --git a/tools/versioner/tests/inline_version_mismatch/platforms/android-12/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/inline_version_mismatch/platforms/android-12/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/inline_version_mismatch/platforms/android-12/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/inline_version_mismatch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/inline_version_mismatch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/versioner/tests/inline_version_mismatch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
diff --git a/tools/versioner/tests/inline_version_mismatch/run.sh b/tools/versioner/tests/inline_version_mismatch/run.sh
new file mode 100644
index 0000000..0277123
--- /dev/null
+++ b/tools/versioner/tests/inline_version_mismatch/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -a 9 -a 12
diff --git a/tools/versioner/tests/missing_api/expected_fail b/tools/versioner/tests/missing_api/expected_fail
new file mode 100644
index 0000000..85c07f5
--- /dev/null
+++ b/tools/versioner/tests/missing_api/expected_fail
@@ -0,0 +1,2 @@
+foo: declaration marked available but symbol missing in [arm-12]
+versioner: version check failed
diff --git a/tools/versioner/tests/missing_api/headers/foo.h b/tools/versioner/tests/missing_api/headers/foo.h
new file mode 100644
index 0000000..2998c8e
--- /dev/null
+++ b/tools/versioner/tests/missing_api/headers/foo.h
@@ -0,0 +1 @@
+int foo() __attribute__((availability(android, introduced = 9)));
diff --git a/tools/versioner/tests/missing_api/platforms/android-12/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/missing_api/platforms/android-12/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/versioner/tests/missing_api/platforms/android-12/arch-arm/symbols/libc.so.functions.txt
diff --git a/tools/versioner/tests/missing_api/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/missing_api/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/missing_api/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/missing_api/run.sh b/tools/versioner/tests/missing_api/run.sh
new file mode 100644
index 0000000..0277123
--- /dev/null
+++ b/tools/versioner/tests/missing_api/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -a 9 -a 12
diff --git a/tools/versioner/tests/missing_arch/dependencies/common/foo/foodep.h b/tools/versioner/tests/missing_arch/dependencies/common/foo/foodep.h
new file mode 100644
index 0000000..9feeb6c
--- /dev/null
+++ b/tools/versioner/tests/missing_arch/dependencies/common/foo/foodep.h
@@ -0,0 +1 @@
+typedef int foo_t;
diff --git a/tools/versioner/tests/missing_arch/expected_fail b/tools/versioner/tests/missing_arch/expected_fail
new file mode 100644
index 0000000..56de9d9
--- /dev/null
+++ b/tools/versioner/tests/missing_arch/expected_fail
@@ -0,0 +1,2 @@
+foo: declaration marked available but symbol missing in [x86-9]
+versioner: version check failed
diff --git a/tools/versioner/tests/missing_arch/headers/foo.h b/tools/versioner/tests/missing_arch/headers/foo.h
new file mode 100644
index 0000000..5d5f8f0
--- /dev/null
+++ b/tools/versioner/tests/missing_arch/headers/foo.h
@@ -0,0 +1 @@
+int foo();
diff --git a/tools/versioner/tests/missing_arch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/missing_arch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/missing_arch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/missing_arch/platforms/android-9/arch-x86/symbols/libc.so.functions.txt b/tools/versioner/tests/missing_arch/platforms/android-9/arch-x86/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/versioner/tests/missing_arch/platforms/android-9/arch-x86/symbols/libc.so.functions.txt
diff --git a/tools/versioner/tests/missing_arch/run.sh b/tools/versioner/tests/missing_arch/run.sh
new file mode 100644
index 0000000..3a3dda8
--- /dev/null
+++ b/tools/versioner/tests/missing_arch/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -r x86 -a 9
diff --git a/tools/versioner/tests/multiple_decl/headers/bar.h b/tools/versioner/tests/multiple_decl/headers/bar.h
new file mode 100644
index 0000000..95f0174
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl/headers/bar.h
@@ -0,0 +1 @@
+int foo() __attribute__((availability(android, obsoleted = 12)));
diff --git a/tools/versioner/tests/multiple_decl/headers/foo.h b/tools/versioner/tests/multiple_decl/headers/foo.h
new file mode 100644
index 0000000..95f0174
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl/headers/foo.h
@@ -0,0 +1 @@
+int foo() __attribute__((availability(android, obsoleted = 12)));
diff --git a/tools/versioner/tests/multiple_decl/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/multiple_decl/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/multiple_decl/run.sh b/tools/versioner/tests/multiple_decl/run.sh
new file mode 100644
index 0000000..d26ab70
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -a 9
diff --git a/tools/versioner/tests/multiple_decl_mismatch/expected_fail b/tools/versioner/tests/multiple_decl_mismatch/expected_fail
new file mode 100644
index 0000000..6d0d209
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl_mismatch/expected_fail
@@ -0,0 +1,5 @@
+foo: availability mismatch for arm-9
+    foo declared in 2 locations:
+        extern function declaration @ headers/bar.h:1:5	[obsoleted = 12]
+        extern function declaration @ headers/foo.h:1:5	[obsoleted = 9]
+versioner: sanity check failed
diff --git a/tools/versioner/tests/multiple_decl_mismatch/headers/bar.h b/tools/versioner/tests/multiple_decl_mismatch/headers/bar.h
new file mode 100644
index 0000000..95f0174
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl_mismatch/headers/bar.h
@@ -0,0 +1 @@
+int foo() __attribute__((availability(android, obsoleted = 12)));
diff --git a/tools/versioner/tests/multiple_decl_mismatch/headers/foo.h b/tools/versioner/tests/multiple_decl_mismatch/headers/foo.h
new file mode 100644
index 0000000..9c81a89
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl_mismatch/headers/foo.h
@@ -0,0 +1 @@
+int foo() __attribute__((availability(android, obsoleted = 9)));
diff --git a/tools/versioner/tests/multiple_decl_mismatch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/multiple_decl_mismatch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl_mismatch/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/multiple_decl_mismatch/run.sh b/tools/versioner/tests/multiple_decl_mismatch/run.sh
new file mode 100644
index 0000000..d26ab70
--- /dev/null
+++ b/tools/versioner/tests/multiple_decl_mismatch/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -a 9
diff --git a/tools/versioner/tests/obsoleted/headers/foo.h b/tools/versioner/tests/obsoleted/headers/foo.h
new file mode 100644
index 0000000..c190629
--- /dev/null
+++ b/tools/versioner/tests/obsoleted/headers/foo.h
@@ -0,0 +1 @@
+int foo() __attribute__((availability(android, introduced = 9, obsoleted = 11)));
diff --git a/tools/versioner/tests/obsoleted/platforms/android-12/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/obsoleted/platforms/android-12/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/versioner/tests/obsoleted/platforms/android-12/arch-arm/symbols/libc.so.functions.txt
diff --git a/tools/versioner/tests/obsoleted/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/obsoleted/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/obsoleted/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/obsoleted/run.sh b/tools/versioner/tests/obsoleted/run.sh
new file mode 100644
index 0000000..0277123
--- /dev/null
+++ b/tools/versioner/tests/obsoleted/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -a 9 -a 12
diff --git a/tools/versioner/tests/smoke/headers/foo.h b/tools/versioner/tests/smoke/headers/foo.h
new file mode 100644
index 0000000..2998c8e
--- /dev/null
+++ b/tools/versioner/tests/smoke/headers/foo.h
@@ -0,0 +1 @@
+int foo() __attribute__((availability(android, introduced = 9)));
diff --git a/tools/versioner/tests/smoke/platforms/android-9/arch-arm/symbols/libc.so.functions.txt b/tools/versioner/tests/smoke/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/tools/versioner/tests/smoke/platforms/android-9/arch-arm/symbols/libc.so.functions.txt
@@ -0,0 +1 @@
+foo
diff --git a/tools/versioner/tests/smoke/run.sh b/tools/versioner/tests/smoke/run.sh
new file mode 100644
index 0000000..d26ab70
--- /dev/null
+++ b/tools/versioner/tests/smoke/run.sh
@@ -0,0 +1 @@
+versioner headers dependencies -p platforms -r arm -a 9