Merge "Make gets() deprecated."
diff --git a/libc/Android.mk b/libc/Android.mk
index fdaa8ee..b5e4899 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -85,7 +85,7 @@
     bionic/__vsnprintf_chk.cpp \
     bionic/__vsprintf_chk.cpp \
 
-libc_bionic_src_files := \
+libc_bionic_ndk_src_files := \
     bionic/abort.cpp \
     bionic/accept.cpp \
     bionic/accept4.cpp \
@@ -120,12 +120,9 @@
     bionic/fchmodat.cpp \
     bionic/ffs.cpp \
     bionic/flockfile.cpp \
-    bionic/fork.cpp \
     bionic/fpclassify.cpp \
     bionic/futimens.cpp \
-    bionic/getauxval.cpp \
     bionic/getcwd.cpp \
-    bionic/getentropy_linux.c \
     bionic/gethostname.cpp \
     bionic/getpgrp.cpp \
     bionic/getpid.cpp \
@@ -160,27 +157,6 @@
     bionic/posix_fallocate.cpp \
     bionic/posix_madvise.cpp \
     bionic/posix_timers.cpp \
-    bionic/pthread_atfork.cpp \
-    bionic/pthread_attr.cpp \
-    bionic/pthread_cond.cpp \
-    bionic/pthread_create.cpp \
-    bionic/pthread_detach.cpp \
-    bionic/pthread_equal.cpp \
-    bionic/pthread_exit.cpp \
-    bionic/pthread_getcpuclockid.cpp \
-    bionic/pthread_getschedparam.cpp \
-    bionic/pthread_gettid_np.cpp \
-    bionic/pthread_internals.cpp \
-    bionic/pthread_join.cpp \
-    bionic/pthread_key.cpp \
-    bionic/pthread_kill.cpp \
-    bionic/pthread_mutex.cpp \
-    bionic/pthread_once.cpp \
-    bionic/pthread_rwlock.cpp \
-    bionic/pthread_self.cpp \
-    bionic/pthread_setname_np.cpp \
-    bionic/pthread_setschedparam.cpp \
-    bionic/pthread_sigmask.cpp \
     bionic/ptrace.cpp \
     bionic/pty.cpp \
     bionic/raise.cpp \
@@ -223,7 +199,6 @@
     bionic/strtold.cpp \
     bionic/stubs.cpp \
     bionic/symlink.cpp \
-    bionic/sysconf.cpp \
     bionic/sysinfo.cpp \
     bionic/syslog.cpp \
     bionic/sys_siglist.c \
@@ -236,11 +211,27 @@
     bionic/umount.cpp \
     bionic/unlink.cpp \
     bionic/utimes.cpp \
-    bionic/vdso.cpp \
     bionic/wait.cpp \
     bionic/wchar.cpp \
     bionic/wctype.cpp \
 
+libc_bionic_src_files :=
+
+# The fork implementation depends on pthread data, so we can't include it in
+# libc_ndk.a.
+libc_bionic_src_files += bionic/fork.cpp
+
+# The data that backs getauxval is initialized in the libc init functions which
+# are invoked by the linker. If this file is included in libc_ndk.a, only one of
+# the copies of the global data will be initialized, resulting in nullptr
+# dereferences.
+libc_bionic_src_files += bionic/getauxval.cpp
+
+# These three require getauxval, which isn't available on older platforms.
+libc_bionic_src_files += bionic/getentropy_linux.c
+libc_bionic_src_files += bionic/sysconf.cpp
+libc_bionic_src_files += bionic/vdso.cpp
+
 libc_cxa_src_files := \
     bionic/__cxa_guard.cpp \
     bionic/__cxa_pure_virtual.cpp \
@@ -337,10 +328,13 @@
     $(libc_upstream_openbsd_gdtoa_src_files) \
     upstream-openbsd/lib/libc/gdtoa/strtorQ.c \
 
+# These two depend on getentropy_linux.cpp, which isn't in libc_ndk.a.
 libc_upstream_openbsd_src_files := \
-    upstream-openbsd/lib/libc/compat-43/killpg.c \
     upstream-openbsd/lib/libc/crypt/arc4random.c \
     upstream-openbsd/lib/libc/crypt/arc4random_uniform.c \
+
+libc_upstream_openbsd_ndk_src_files := \
+    upstream-openbsd/lib/libc/compat-43/killpg.c \
     upstream-openbsd/lib/libc/gen/alarm.c \
     upstream-openbsd/lib/libc/gen/ctype_.c \
     upstream-openbsd/lib/libc/gen/daemon.c \
@@ -513,6 +507,29 @@
     upstream-openbsd/lib/libc/string/wcsstr.c \
     upstream-openbsd/lib/libc/string/wcswidth.c \
 
+libc_pthread_src_files := \
+    bionic/pthread_atfork.cpp \
+    bionic/pthread_attr.cpp \
+    bionic/pthread_cond.cpp \
+    bionic/pthread_create.cpp \
+    bionic/pthread_detach.cpp \
+    bionic/pthread_equal.cpp \
+    bionic/pthread_exit.cpp \
+    bionic/pthread_getcpuclockid.cpp \
+    bionic/pthread_getschedparam.cpp \
+    bionic/pthread_gettid_np.cpp \
+    bionic/pthread_internals.cpp \
+    bionic/pthread_join.cpp \
+    bionic/pthread_key.cpp \
+    bionic/pthread_kill.cpp \
+    bionic/pthread_mutex.cpp \
+    bionic/pthread_once.cpp \
+    bionic/pthread_rwlock.cpp \
+    bionic/pthread_self.cpp \
+    bionic/pthread_setname_np.cpp \
+    bionic/pthread_setschedparam.cpp \
+    bionic/pthread_sigmask.cpp \
+
 libc_arch_static_src_files := \
     bionic/dl_iterate_phdr_static.cpp \
 
@@ -786,6 +803,51 @@
 
 
 # ========================================================
+# libc_openbsd_ndk.a - upstream OpenBSD C library code
+# that can be safely included in the libc_ndk.a (doesn't
+# contain any troublesome global data or constructors).
+# ========================================================
+#
+# These files are built with the openbsd-compat.h header file
+# automatically included.
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(libc_upstream_openbsd_ndk_src_files)
+ifneq (,$(filter $(TARGET_ARCH),x86 x86_64))
+  # Clang has wrong long double size or LDBL_MANT_DIG, http://b/17163651.
+  LOCAL_CLANG := false
+else
+  LOCAL_CLANG := $(use_clang)
+endif
+
+LOCAL_CFLAGS := \
+    $(libc_common_cflags) \
+    -Wno-sign-compare \
+    -Wno-uninitialized \
+    -Wno-unused-parameter \
+    -include openbsd-compat.h \
+
+LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
+LOCAL_CPPFLAGS := $(libc_common_cppflags)
+LOCAL_C_INCLUDES := $(libc_common_c_includes) \
+    $(LOCAL_PATH)/private \
+    $(LOCAL_PATH)/upstream-openbsd/android/include \
+    $(LOCAL_PATH)/upstream-openbsd/lib/libc/include \
+    $(LOCAL_PATH)/upstream-openbsd/lib/libc/gdtoa/ \
+
+LOCAL_MODULE := libc_openbsd_ndk
+LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
+LOCAL_CXX_STL := none
+LOCAL_SYSTEM_SHARED_LIBRARIES :=
+LOCAL_ADDRESS_SANITIZER := false
+LOCAL_NATIVE_COVERAGE := $(bionic_coverage)
+
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
+include $(BUILD_STATIC_LIBRARY)
+
+
+# ========================================================
 # libc_openbsd.a - upstream OpenBSD C library code
 # ========================================================
 #
@@ -900,11 +962,80 @@
 LOCAL_NATIVE_COVERAGE := $(bionic_coverage)
 
 $(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
+include $(BUILD_STATIC_LIBRARY)
+
+
+# ========================================================
+# libc_bionic_ndk.a - The portions of libc_bionic that can
+# be safely used in libc_ndk.a (no troublesome global data
+# or constructors).
+# ========================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(libc_bionic_ndk_src_files)
+LOCAL_CFLAGS := $(libc_common_cflags) \
+    -Wframe-larger-than=2048 \
+
+# ssse3-strcmp-slm.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_x86_64 += -no-integrated-as
+
+# memcpy.S, memchr.S, etc. do not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
+
+LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
+LOCAL_CPPFLAGS := $(libc_common_cppflags) -Wold-style-cast
+LOCAL_C_INCLUDES := $(libc_common_c_includes) bionic/libstdc++/include
+LOCAL_MODULE := libc_bionic_ndk
+LOCAL_CLANG := $(use_clang)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
+LOCAL_CXX_STL := none
+LOCAL_SYSTEM_SHARED_LIBRARIES :=
+LOCAL_ADDRESS_SANITIZER := false
+LOCAL_NATIVE_COVERAGE := $(bionic_coverage)
+
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
 $(eval $(call patch-up-arch-specific-flags,LOCAL_SRC_FILES,libc_bionic_src_files))
 include $(BUILD_STATIC_LIBRARY)
 
 
 # ========================================================
+# libc_pthread.a - pthreads parts that previously lived in
+# libc_bionic.a. Relocated to their own library because
+# they can't be included in libc_ndk.a (as they layout of
+# pthread_t has changed over the years and has ABI
+# compatibility issues).
+# ========================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(libc_pthread_src_files)
+LOCAL_CFLAGS := $(libc_common_cflags) \
+    -Wframe-larger-than=2048 \
+
+# ssse3-strcmp-slm.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_x86_64 += -no-integrated-as
+
+# memcpy.S, memchr.S, etc. do not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
+
+LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
+LOCAL_CPPFLAGS := $(libc_common_cppflags) -Wold-style-cast
+LOCAL_C_INCLUDES := $(libc_common_c_includes)
+LOCAL_MODULE := libc_pthread
+LOCAL_CLANG := $(use_clang)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
+LOCAL_CXX_STL := none
+LOCAL_SYSTEM_SHARED_LIBRARIES :=
+LOCAL_ADDRESS_SANITIZER := false
+LOCAL_NATIVE_COVERAGE := $(bionic_coverage)
+
+include $(BUILD_STATIC_LIBRARY)
+
+
+# ========================================================
 # libc_cxa.a - Things traditionally in libstdc++
 # ========================================================
 
@@ -992,9 +1123,56 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := libc_ndk
-LOCAL_WHOLE_STATIC_LIBRARIES := libc_syscalls libm
-LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
+LOCAL_CLANG := true
+LOCAL_ASFLAGS := $(LOCAL_CFLAGS)
+LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
+LOCAL_CFLAGS := $(libc_common_cflags) -fvisibility=hidden -O0
+LOCAL_CPPFLAGS := $(libc_common_cppflags)
+LOCAL_C_INCLUDES := $(libc_common_c_includes)
+LOCAL_ADDRESS_SANITIZER := false
+LOCAL_NATIVE_COVERAGE := $(bionic_coverage)
+LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+LOCAL_SRC_FILES := \
+    $(libc_common_src_files) \
+    $(libc_arch_dynamic_src_files) \
+    $(libc_ndk_stub_src_files) \
+    bionic/malloc_debug_common.cpp \
+
+LOCAL_SRC_FILES_arm += \
+    arch-common/bionic/crtbegin_so.c \
+    arch-arm/bionic/atexit_legacy.c \
+    arch-common/bionic/crtend_so.S \
+
+LOCAL_CFLAGS := $(libc_common_cflags) \
+    -DLIBC_STATIC \
+
+LOCAL_WHOLE_STATIC_LIBRARIES := \
+    libc_bionic_ndk \
+    libc_cxa \
+    libc_freebsd \
+    libc_gdtoa \
+    libc_malloc \
+    libc_netbsd \
+    libc_openbsd_ndk \
+    libc_stack_protector \
+    libc_syscalls \
+    libc_tzcode \
+    libm \
+
+LOCAL_WHOLE_STATIC_LIBRARIES_arm := libc_aeabi
+LOCAL_CXX_STL := none
+
+ifneq ($(MALLOC_IMPL),dlmalloc)
+LOCAL_WHOLE_STATIC_LIBRARIES += libjemalloc
+endif
+
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
+$(eval $(call patch-up-arch-specific-flags,LOCAL_SRC_FILES,libc_common_src_files))
+$(eval $(call patch-up-arch-specific-flags,LOCAL_SRC_FILES,libc_arch_dynamic_src_files))
+$(eval $(call patch-up-arch-specific-flags,LOCAL_ASFLAGS,LOCAL_CFLAGS))
+
+LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 include $(BUILD_STATIC_LIBRARY)
 
 # ========================================================
@@ -1013,6 +1191,7 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_WHOLE_STATIC_LIBRARIES := \
     libc_bionic \
+    libc_bionic_ndk \
     libc_cxa \
     libc_dns \
     libc_freebsd \
@@ -1020,6 +1199,8 @@
     libc_malloc \
     libc_netbsd \
     libc_openbsd \
+    libc_openbsd_ndk \
+    libc_pthread \
     libc_stack_protector \
     libc_syscalls \
     libc_tzcode \
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index 4c4cfbd..0f016d7 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -59,22 +59,29 @@
 
 extern int creat(const char*, mode_t);
 extern int creat64(const char*, mode_t);
-extern int fallocate64(int, int, off64_t, off64_t);
-extern int fallocate(int, int, off_t, off_t);
 extern int fcntl(int, int, ...);
 extern int openat(int, const char*, int, ...);
 extern int openat64(int, const char*, int, ...);
 extern int open(const char*, int, ...);
 extern int open64(const char*, int, ...);
-extern int posix_fadvise64(int, off64_t, off64_t, int);
-extern int posix_fadvise(int, off_t, off_t, int);
-extern int posix_fallocate64(int, off64_t, off64_t);
-extern int posix_fallocate(int, off_t, off_t);
 extern ssize_t splice(int, off64_t*, int, off64_t*, size_t, unsigned int);
 extern ssize_t tee(int, int, size_t, unsigned int);
 extern int unlinkat(int, const char*, int);
 extern ssize_t vmsplice(int, const struct iovec*, size_t, unsigned int);
 
+#if defined(__USE_FILE_OFFSET64)
+extern int fallocate(int, int, off_t, off_t) __RENAME(fallocate64);
+extern int posix_fadvise(int, off_t, off_t, int) __RENAME(posix_fadvise64);
+extern int posix_fallocate(int, off_t, off_t) __RENAME(posix_fallocate);
+#else
+extern int fallocate(int, int, off_t, off_t);
+extern int posix_fadvise(int, off_t, off_t, int);
+extern int posix_fallocate(int, off_t, off_t);
+#endif
+extern int fallocate64(int, int, off64_t, off64_t);
+extern int posix_fadvise64(int, off64_t, off64_t, int);
+extern int posix_fallocate64(int, off64_t, off64_t);
+
 extern int __open_2(const char*, int);
 extern int __open_real(const char*, int, ...) __RENAME(open);
 extern int __openat_2(int, const char*, int);
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index eb578f5..17ef096 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -57,8 +57,6 @@
 
 __BEGIN_DECLS
 
-#define	_FSTDIO			/* Define for new stdio with functions. */
-
 typedef off_t fpos_t;		/* stdio file position type */
 
 /*
@@ -282,11 +280,22 @@
 extern int rename(const char*, const char*);
 extern int renameat(int, const char*, int, const char*);
 
+#if defined(__USE_FILE_OFFSET64)
+/* Not possible. */
+int	 fgetpos(FILE * __restrict, fpos_t * __restrict)
+	__attribute__((__error__("not available with _FILE_OFFSET_BITS=64")));
+int	 fsetpos(FILE *, const fpos_t *)
+	__attribute__((__error__("not available with _FILE_OFFSET_BITS=64")));
+int	 fseeko(FILE *, off_t, int)
+	__attribute__((__error__("not available with _FILE_OFFSET_BITS=64")));
+off_t	 ftello(FILE *)
+	__attribute__((__error__("not available with _FILE_OFFSET_BITS=64")));
+#else
 int	 fgetpos(FILE * __restrict, fpos_t * __restrict);
 int	 fsetpos(FILE *, const fpos_t *);
-
 int	 fseeko(FILE *, off_t, int);
 off_t	 ftello(FILE *);
+#endif
 
 #if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
 int	 snprintf(char * __restrict, size_t, const char * __restrict, ...)
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index 1d33895..04613f4 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -380,6 +380,15 @@
 # define __USE_BSD 1
 #endif
 
+/*
+ * _FILE_OFFSET_BITS 64 support.
+ */
+#if !defined(__LP64__) && defined(_FILE_OFFSET_BITS)
+#if _FILE_OFFSET_BITS == 64
+#define __USE_FILE_OFFSET64 1
+#endif
+#endif
+
 /*-
  * POSIX.1 requires that the macros we test be defined before any standard
  * header file is included.
diff --git a/libc/include/sys/mman.h b/libc/include/sys/mman.h
index 1663222..6857f60 100644
--- a/libc/include/sys/mman.h
+++ b/libc/include/sys/mman.h
@@ -49,8 +49,13 @@
 #define POSIX_MADV_WILLNEED   MADV_WILLNEED
 #define POSIX_MADV_DONTNEED   MADV_DONTNEED
 
+#if defined(__USE_FILE_OFFSET64)
+extern void* mmap(void*, size_t, int, int, int, off_t) __RENAME(mmap64);
+#else
 extern void* mmap(void*, size_t, int, int, int, off_t);
+#endif
 extern void* mmap64(void*, size_t, int, int, int, off64_t);
+
 extern int munmap(void*, size_t);
 extern int msync(const void*, size_t, int);
 extern int mprotect(const void*, size_t, int);
diff --git a/libc/include/sys/sendfile.h b/libc/include/sys/sendfile.h
index 81a3c44..c588e68 100644
--- a/libc/include/sys/sendfile.h
+++ b/libc/include/sys/sendfile.h
@@ -34,7 +34,11 @@
 
 __BEGIN_DECLS
 
+#if defined(__USE_FILE_OFFSET64)
+extern ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count) __RENAME(sendfile64);
+#else
 extern ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count);
+#endif
 extern ssize_t sendfile64(int out_fd, int in_fd, off64_t* offset, size_t count);
 
 __END_DECLS
diff --git a/libc/include/sys/types.h b/libc/include/sys/types.h
index a5fa692..a6b0fd8 100644
--- a/libc/include/sys/types.h
+++ b/libc/include/sys/types.h
@@ -90,16 +90,14 @@
 typedef __kernel_time_t __time_t;
 typedef __time_t time_t;
 
-/* This historical accident means that we had a 32-bit off_t on 32-bit architectures. */
-#if !defined(__LP64__)
-typedef __kernel_off_t off_t;
-typedef __kernel_loff_t loff_t;
+#if defined(__USE_FILE_OFFSET64) || defined(__LP64__)
+typedef int64_t off_t;
+typedef off_t loff_t;
 typedef loff_t off64_t;
 #else
-/* We could re-use the LP32 definitions, but that would mean that although off_t and loff_t/off64_t
- * would be the same size, they wouldn't actually be the same type, which can lead to warnings. */
+/* This historical accident means that we had a 32-bit off_t on 32-bit architectures. */
 typedef __kernel_off_t off_t;
-typedef off_t loff_t;
+typedef __kernel_loff_t loff_t;
 typedef loff_t off64_t;
 #endif
 
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 16d413a..0b67004 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -146,22 +146,14 @@
 extern int fchown(int, uid_t, gid_t);
 extern int fchownat(int, const char*, uid_t, gid_t, int);
 extern int lchown(const char *, uid_t, gid_t);
-extern int truncate(const char *, off_t);
-extern int truncate64(const char *, off64_t);
 extern char *getcwd(char *, size_t);
 
 extern int sync(void);
 
 extern int close(int);
-extern off_t lseek(int, off_t, int);
-extern off64_t lseek64(int, off64_t, int);
 
 extern ssize_t read(int, void *, size_t);
 extern ssize_t write(int, const void *, size_t);
-extern ssize_t pread(int, void *, size_t, off_t);
-extern ssize_t pread64(int, void *, size_t, off64_t);
-extern ssize_t pwrite(int, const void *, size_t, off_t);
-extern ssize_t pwrite64(int, const void *, size_t, off64_t);
 
 extern int dup(int);
 extern int dup2(int, int);
@@ -170,7 +162,24 @@
 extern int ioctl(int, int, ...);
 extern int fsync(int);
 extern int fdatasync(int);
+
+#if defined(__USE_FILE_OFFSET64)
+extern int truncate(const char *, off_t) __RENAME(truncate64);
+extern off_t lseek(int, off_t, int) __RENAME(lseek64);
+extern ssize_t pread(int, void *, size_t, off_t) __RENAME(pread64);
+extern ssize_t pwrite(int, const void *, size_t, off_t) __RENAME(pwrite64);
+extern int ftruncate(int, off_t) __RENAME(ftruncate64);
+#else
+extern int truncate(const char *, off_t);
+extern off_t lseek(int, off_t, int);
+extern ssize_t pread(int, void *, size_t, off_t);
+extern ssize_t pwrite(int, const void *, size_t, off_t);
 extern int ftruncate(int, off_t);
+#endif
+extern int truncate64(const char *, off64_t);
+extern off64_t lseek64(int, off64_t, int);
+extern ssize_t pread64(int, void *, size_t, off64_t);
+extern ssize_t pwrite64(int, const void *, size_t, off64_t);
 extern int ftruncate64(int, off64_t);
 
 extern int pause(void);
diff --git a/libc/zoneinfo/tzdata b/libc/zoneinfo/tzdata
index fc6c5ba..b9a6621 100644
--- a/libc/zoneinfo/tzdata
+++ b/libc/zoneinfo/tzdata
Binary files differ