Merge "Make static library for loader tests"
diff --git a/libc/Android.bp b/libc/Android.bp
index ad31192..f61cc46 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1989,4 +1989,125 @@
     defaults: ["crt_defaults"],
 }
 
+// The following module lives in prebuilts/ndk because we need to preprocess the
+// headers to include ifdef guards for __ANDROID_API__. Update with
+// bionic/tools/update_headers.sh.
+// ndk_headers {
+//     name: "common_libc",
+//     from: "include",
+//     to: "",
+//     srcs: ["include/**/*.h"],
+// }
+
+ndk_headers {
+    name: "libc_linux",
+    from: "kernel/uapi/linux",
+    to: "linux",
+    srcs: ["kernel/uapi/linux/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_android",
+    from: "kernel/android/uapi/linux",
+    to: "linux",
+    srcs: ["kernel/android/uapi/linux/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_asm_generic",
+    from: "kernel/uapi/asm-generic",
+    to: "asm-generic",
+    srcs: ["kernel/uapi/asm-generic/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_asm_arm",
+    from: "kernel/uapi/asm-arm",
+    to: "arm-linux-androideabi",
+    srcs: ["kernel/uapi/asm-arm/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_asm_arm64",
+    from: "kernel/uapi/asm-arm64",
+    to: "aarch64-linux-android",
+    srcs: ["kernel/uapi/asm-arm64/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_asm_mips",
+    from: "kernel/uapi/asm-mips",
+    to: "mipsel-linux-android",
+    srcs: ["kernel/uapi/asm-mips/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_asm_mips64",
+    from: "kernel/uapi/asm-mips",
+    to: "mips64el-linux-android",
+    srcs: ["kernel/uapi/asm-mips/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_asm_x86",
+    from: "kernel/uapi/asm-x86",
+    to: "i686-linux-android",
+    srcs: ["kernel/uapi/asm-x86/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_asm_x86_64",
+    from: "kernel/uapi/asm-x86",
+    to: "x86_64-linux-android",
+    srcs: ["kernel/uapi/asm-x86/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_machine_arm",
+    from: "arch-arm/include",
+    to: "arm-linux-androideabi",
+    srcs: ["arch-arm/include/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_machine_arm64",
+    from: "arch-arm64/include",
+    to: "aarch64-linux-android",
+    srcs: ["arch-arm64/include/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_machine_mips",
+    from: "arch-mips/include",
+    to: "mipsel-linux-android",
+    srcs: ["arch-mips/include/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_machine_mips64",
+    from: "arch-mips/include",
+    to: "mips64el-linux-android",
+    srcs: ["arch-mips/include/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_machine_x86",
+    from: "arch-x86/include",
+    to: "i686-linux-android",
+    srcs: ["arch-x86/include/**/*.h"],
+}
+
+ndk_headers {
+    name: "libc_machine_x86_64",
+    from: "arch-x86_64/include",
+    to: "x86_64-linux-android",
+    srcs: ["arch-x86_64/include/**/*.h"],
+}
+
+ndk_library {
+    name: "libc.ndk",
+    symbol_file: "libc.map.txt",
+    first_version: "9",
+}
+
 subdirs = ["malloc_debug"]
diff --git a/libc/bionic/__libc_init_main_thread.cpp b/libc/bionic/__libc_init_main_thread.cpp
index 2643eee..b5a83f4 100644
--- a/libc/bionic/__libc_init_main_thread.cpp
+++ b/libc/bionic/__libc_init_main_thread.cpp
@@ -69,7 +69,9 @@
 
   // The -fstack-protector implementation uses TLS, so make sure that's
   // set up before we call any function that might get a stack check inserted.
+  // TLS also needs to be set up before errno (and therefore syscalls) can be used.
   __set_tls(main_thread.tls);
+  __init_tls(&main_thread);
 
   // Tell the kernel to clear our tid field when we exit, so we're like any other pthread.
   // As a side-effect, this tells us our pid (which is the same as the main thread's tid).
@@ -91,9 +93,9 @@
   // before we initialize the TLS. Dynamic executables will initialize their copy of the global
   // stack protector from the one in the main thread's TLS.
   __libc_init_global_stack_chk_guard(args);
+  __init_thread_stack_guard(&main_thread);
 
   __init_thread(&main_thread);
-  __init_tls(&main_thread);
 
   // Store a pointer to the kernel argument block in a TLS slot to be
   // picked up by the libc constructor.
diff --git a/libc/bionic/brk.cpp b/libc/bionic/brk.cpp
index a8c078b..e1a4b05 100644
--- a/libc/bionic/brk.cpp
+++ b/libc/bionic/brk.cpp
@@ -29,7 +29,7 @@
 #include <errno.h>
 #include <unistd.h>
 
-#if __LP64__
+#if defined(__LP64__)
 static void* __bionic_brk;
 #else
 void* __bionic_brk; // Accidentally exported by the NDK.
diff --git a/libc/bionic/fpclassify.cpp b/libc/bionic/fpclassify.cpp
index f8cea80..42ed3ef 100644
--- a/libc/bionic/fpclassify.cpp
+++ b/libc/bionic/fpclassify.cpp
@@ -113,7 +113,7 @@
 }
 __strong_alias(isnormalf, __isnormalf);
 
-#if __LP64__
+#if defined(__LP64__)
 
 // LP64 uses 128-bit long doubles.
 
diff --git a/libc/bionic/legacy_32_bit_support.cpp b/libc/bionic/legacy_32_bit_support.cpp
index f2bb37d..983fb32 100644
--- a/libc/bionic/legacy_32_bit_support.cpp
+++ b/libc/bionic/legacy_32_bit_support.cpp
@@ -37,7 +37,7 @@
 #include <sys/vfs.h>
 #include <unistd.h>
 
-#if __LP64__
+#if defined(__LP64__)
 #error This code is only needed on 32-bit systems!
 #endif
 
diff --git a/libc/bionic/open.cpp b/libc/bionic/open.cpp
index 41dce43..2daa21f 100644
--- a/libc/bionic/open.cpp
+++ b/libc/bionic/open.cpp
@@ -36,7 +36,7 @@
 extern "C" int __openat(int, const char*, int, int);
 
 static inline int force_O_LARGEFILE(int flags) {
-#if __LP64__
+#if defined(__LP64__)
   return flags; // No need, and aarch64's strace gets confused.
 #else
   return flags | O_LARGEFILE;
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index 1956a8f..bfa4e8c 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -44,7 +44,7 @@
 #include "private/ScopedPthreadMutexLocker.h"
 
 // x86 uses segment descriptors rather than a direct pointer to TLS.
-#if __i386__
+#if defined(__i386__)
 #include <asm/ldt.h>
 void __init_user_desc(struct user_desc*, bool, void*);
 #endif
@@ -56,7 +56,6 @@
   // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0.
   thread->tls[TLS_SLOT_SELF] = thread->tls;
   thread->tls[TLS_SLOT_THREAD_ID] = thread;
-  __init_thread_stack_guard(thread);
 }
 
 void __init_thread_stack_guard(pthread_internal_t* thread) {
@@ -102,7 +101,7 @@
     sched_param param;
     param.sched_priority = thread->attr.sched_priority;
     if (sched_setscheduler(thread->tid, thread->attr.sched_policy, &param) == -1) {
-#if __LP64__
+#if defined(__LP64__)
       // For backwards compatibility reasons, we only report failures on 64-bit devices.
       error = errno;
 #endif
@@ -182,6 +181,7 @@
   thread->mmap_size = mmap_size;
   thread->attr = *attr;
   __init_tls(thread);
+  __init_thread_stack_guard(thread);
 
   *threadp = thread;
   *child_stack = stack_top;
diff --git a/libc/bionic/statvfs.cpp b/libc/bionic/statvfs.cpp
index 39ffb64..cd825eb 100644
--- a/libc/bionic/statvfs.cpp
+++ b/libc/bionic/statvfs.cpp
@@ -20,7 +20,7 @@
 
 // Paper over the fact that 32-bit kernels use fstatfs64/statfs64 with an extra argument,
 // but 64-bit kernels don't have the "64" bit suffix or the extra size_t argument.
-#if __LP64__
+#if defined(__LP64__)
 extern "C" int __fstatfs(int, struct statfs*);
 extern "C" int __statfs(const char*, struct statfs*);
 #  define __fstatfs64(fd,size,buf) __fstatfs(fd,buf)
diff --git a/libc/bionic/strtold.cpp b/libc/bionic/strtold.cpp
index 5616cf7..c55dd61 100644
--- a/libc/bionic/strtold.cpp
+++ b/libc/bionic/strtold.cpp
@@ -32,7 +32,7 @@
 extern "C" int __strtorQ(const char*, char**, int, void*);
 
 long double strtold(const char* s, char** end_ptr) {
-#if __LP64__
+#if defined(__LP64__)
   long double result;
   __strtorQ(s, end_ptr, FLT_ROUNDS, &result);
   return result;
diff --git a/libc/include/link.h b/libc/include/link.h
index f40e7e4..92ecceb 100644
--- a/libc/include/link.h
+++ b/libc/include/link.h
@@ -34,7 +34,7 @@
 
 __BEGIN_DECLS
 
-#if __LP64__
+#if defined(__LP64__)
 #define ElfW(type) Elf64_ ## type
 #else
 #define ElfW(type) Elf32_ ## type
diff --git a/libc/include/netinet/ip6.h b/libc/include/netinet/ip6.h
index 639637e..fcfd5d6 100644
--- a/libc/include/netinet/ip6.h
+++ b/libc/include/netinet/ip6.h
@@ -66,6 +66,7 @@
 
 #include <sys/cdefs.h>
 #include <sys/types.h>
+#include <endian.h>
 
 #include <linux/in6.h>
 
diff --git a/libc/include/stdint.h b/libc/include/stdint.h
index 1cedca1..322a81c 100644
--- a/libc/include/stdint.h
+++ b/libc/include/stdint.h
@@ -39,7 +39,7 @@
 typedef unsigned short __uint16_t;
 typedef int __int32_t;
 typedef unsigned int __uint32_t;
-#if __LP64__
+#if defined(__LP64__)
 typedef long __int64_t;
 typedef unsigned long __uint64_t;
 #else
@@ -47,7 +47,7 @@
 typedef unsigned long long __uint64_t;
 #endif
 
-#if __LP64__
+#if defined(__LP64__)
 typedef long __intptr_t;
 typedef unsigned long __uintptr_t;
 #else
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index 13c3269..3b058e8 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -229,7 +229,7 @@
 #include <android/api-level.h>
 
 /* glibc compatibility. */
-#if __LP64__
+#if defined(__LP64__)
 #define __WORDSIZE 64
 #else
 #define __WORDSIZE 32
@@ -278,7 +278,7 @@
 #include <android/versioning.h>
 
 #if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5
-#if __LP64__
+#if defined(__LP64__)
 #define __size_mul_overflow(a, b, result) __builtin_umull_overflow(a, b, result)
 #else
 #define __size_mul_overflow(a, b, result) __builtin_umul_overflow(a, b, result)
diff --git a/libc/include/sys/user.h b/libc/include/sys/user.h
index 5601b02..f9ad956 100644
--- a/libc/include/sys/user.h
+++ b/libc/include/sys/user.h
@@ -38,7 +38,7 @@
 #define PAGE_SIZE 4096
 #define PAGE_MASK (~(PAGE_SIZE - 1))
 
-#if __i386__
+#if defined(__i386__)
 
 struct user_fpregs_struct {
   long cwd;
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index ab8108a..4e7d8ba 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -209,7 +209,7 @@
 #if __ANDROID_API__ >= 21
 int getpagesize(void) __INTRODUCED_IN(21);
 #else
-__inline__ int getpagesize(void) {
+static __inline__ int getpagesize(void) {
   return sysconf(_SC_PAGESIZE);
 }
 #endif
diff --git a/libc/private/bionic_ieee.h b/libc/private/bionic_ieee.h
index c579969..69095f0 100644
--- a/libc/private/bionic_ieee.h
+++ b/libc/private/bionic_ieee.h
@@ -79,7 +79,7 @@
   unsigned dbl_sign:1;
 };
 
-#if __LP64__
+#if defined(__LP64__)
 
 /* 64-bit Android uses ld128 long doubles. */
 
diff --git a/libc/upstream-openbsd/android/include/arith.h b/libc/upstream-openbsd/android/include/arith.h
index b262e4f..cb116d4 100644
--- a/libc/upstream-openbsd/android/include/arith.h
+++ b/libc/upstream-openbsd/android/include/arith.h
@@ -16,7 +16,7 @@
 
 #define IEEE_8087
 
-#if __LP64__
+#if defined(__LP64__)
 #define Long int
 #endif
 
diff --git a/libc/upstream-openbsd/android/include/gd_qnan.h b/libc/upstream-openbsd/android/include/gd_qnan.h
index e5bf973..bcdff28 100644
--- a/libc/upstream-openbsd/android/include/gd_qnan.h
+++ b/libc/upstream-openbsd/android/include/gd_qnan.h
@@ -23,7 +23,7 @@
 #define d_QNAN0 0x00000000
 #define d_QNAN1 0x7ff80000
 
-#if __LP64__
+#if defined(__LP64__)
 #define ld_QNAN0 0x00000000
 #define ld_QNAN1 0x00000000
 #define ld_QNAN2 0x00000000
diff --git a/libm/Android.bp b/libm/Android.bp
index 23b9d5e..64d281f 100644
--- a/libm/Android.bp
+++ b/libm/Android.bp
@@ -531,3 +531,9 @@
     },
     stl: "none",
 }
+
+ndk_library {
+    name: "libm.ndk",
+    symbol_file: "libm.map.txt",
+    first_version: "9",
+}
diff --git a/tests/libc_logging_test.cpp b/tests/libc_logging_test.cpp
index d4ceded..4150483 100644
--- a/tests/libc_logging_test.cpp
+++ b/tests/libc_logging_test.cpp
@@ -133,7 +133,7 @@
 #if defined(__BIONIC__)
   char buf[BUFSIZ];
   __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
-#if __LP64__
+#if defined(__LP64__)
   EXPECT_STREQ("9223372036854775807", buf);
 #else
   EXPECT_STREQ("2147483647", buf);
@@ -147,7 +147,7 @@
 #if defined(__BIONIC__)
   char buf[BUFSIZ];
   __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
-#if __LP64__
+#if defined(__LP64__)
   EXPECT_STREQ("-9223372036854775808", buf);
 #else
   EXPECT_STREQ("-2147483648", buf);
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
index 9ca1476..8283633 100644
--- a/tests/libs/Android.bp
+++ b/tests/libs/Android.bp
@@ -441,10 +441,4 @@
         "libz",
         "libutils",
     ],
-
-    target: {
-        windows: {
-            enabled: true,
-        },
-    },
 }
diff --git a/tests/libs/bionic_tests_zipalign.cpp b/tests/libs/bionic_tests_zipalign.cpp
index a72820f..8e31474 100644
--- a/tests/libs/bionic_tests_zipalign.cpp
+++ b/tests/libs/bionic_tests_zipalign.cpp
@@ -19,6 +19,7 @@
 #include <stdlib.h>
 
 #include <algorithm>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -36,7 +37,7 @@
   fprintf(stderr, "    The output zip file that will be created from the input file.\n");
 }
 
-typedef std::pair<ZipEntry*, ZipString*> ZipData;
+using ZipData = std::pair<std::unique_ptr<ZipEntry>, std::unique_ptr<ZipString>>;
 
 static bool GetEntries(ZipArchiveHandle handle, std::vector<ZipData>* entries) {
   void* cookie;
@@ -49,14 +50,15 @@
   ZipEntry entry;
   ZipString name;
   while ((return_value = Next(cookie, &entry, &name)) == 0) {
-    entries->push_back(std::make_pair(new ZipEntry(entry), new ZipString(name)));
+    entries->emplace_back(std::make_pair(std::make_unique<ZipEntry>(entry),
+                                         std::make_unique<ZipString>(name)));
   }
   if (return_value != -1) {
     fprintf(stderr, "Error while iterating over zip entries: %s\n", ErrorCodeString(return_value));
   } else {
     // Sort by offset.
     std::sort(entries->begin(), entries->end(),
-              [](ZipData a, ZipData b) { return a.first->offset < b.first->offset; });
+              [](ZipData& a, ZipData& b) { return a.first->offset < b.first->offset; });
   }
 
   EndIteration(cookie);
@@ -75,8 +77,8 @@
 
   int32_t error;
   for (auto& entry : entries) {
-    ZipEntry* zip_entry = entry.first;
-    ZipString* zip_str = entry.second;
+    ZipEntry* zip_entry = entry.first.get();
+    ZipString* zip_str = entry.second.get();
 
     size_t flags = 0;
     if ((zip_entry->method & kCompressDeflated) != 0) {
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 79c5e92..63da9e0 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -548,7 +548,7 @@
 TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
   char buf[BUFSIZ];
   snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
-#if __LP64__
+#if defined(__LP64__)
   EXPECT_STREQ("9223372036854775807", buf);
 #else
   EXPECT_STREQ("2147483647", buf);
@@ -558,7 +558,7 @@
 TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
   char buf[BUFSIZ];
   snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
-#if __LP64__
+#if defined(__LP64__)
   EXPECT_STREQ("-9223372036854775808", buf);
 #else
   EXPECT_STREQ("-2147483648", buf);
diff --git a/tests/sys_ptrace_test.cpp b/tests/sys_ptrace_test.cpp
index 9071acf..bdd6a89 100644
--- a/tests/sys_ptrace_test.cpp
+++ b/tests/sys_ptrace_test.cpp
@@ -183,7 +183,7 @@
   run_watchpoint_test_impl<uint8_t>(cpu);
   run_watchpoint_test_impl<uint16_t>(cpu);
   run_watchpoint_test_impl<uint32_t>(cpu);
-#if __LP64__
+#if defined(__LP64__)
   run_watchpoint_test_impl<uint64_t>(cpu);
 #endif
 }
diff --git a/tools/versioner/src/versioner.cpp b/tools/versioner/src/versioner.cpp
index 432fd66..535eef8 100644
--- a/tools/versioner/src/versioner.cpp
+++ b/tools/versioner/src/versioner.cpp
@@ -77,6 +77,13 @@
     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("-Wall");
+    command.push_back("-Wextra");
+    command.push_back("-Werror");
+    command.push_back("-Wundef");
+    command.push_back("-Wno-unused-macros");
+    command.push_back("-Wno-unused-function");
+    command.push_back("-Wno-unused-variable");
     command.push_back("-Wno-unknown-attributes");
     command.push_back("-Wno-pragma-once-outside-header");
     command.push_back("-target");