Enable building libc with clang.

Bug: 16218205
Change-Id: I024de2044fe016412c7bead22c264e96be4cb39c
diff --git a/libc/Android.mk b/libc/Android.mk
index b0a9fb1..3ce9f2d 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -495,6 +495,8 @@
 libc_common_cflags += -DTARGET_USES_LOGD
 endif
 
+use_clang := false
+
 # Try to catch typical 32-bit assumptions that break with 64-bit pointers.
 libc_common_cflags += \
     -Werror=pointer-to-int-cast \
@@ -575,6 +577,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_stack_protector
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -612,6 +615,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes) $(LOCAL_PATH)/tzcode/
 LOCAL_MODULE := libc_tzcode
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -646,6 +650,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_dns
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -674,6 +679,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_freebsd
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -703,6 +709,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_netbsd
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -733,6 +740,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_openbsd
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -763,6 +771,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_gdtoa
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -784,6 +793,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_bionic
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -803,6 +813,7 @@
 LOCAL_SRC_FILES_$(TARGET_2ND_ARCH) := $(call all-S-files-under,arch-$(TARGET_2ND_ARCH)/syscalls)
 endif
 LOCAL_MODULE := libc_syscalls
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
@@ -810,6 +821,24 @@
 
 
 # ========================================================
+# libc_aeabi.a
+# This is an LP32 ARM-only library that needs to be built with -fno-builtin
+# to avoid infinite recursion. For the other architectures we just build an
+# empty library to keep this makefile simple.
+# ========================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES_arm := arch-arm/bionic/__aeabi.c
+LOCAL_MODULE := libc_aeabi
+LOCAL_CLANG := $(use_clang)
+LOCAL_CFLAGS := $(libc_common_cflags) -fno-builtin
+LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
+LOCAL_SYSTEM_SHARED_LIBRARIES :=
+
+include $(BUILD_STATIC_LIBRARY)
+
+# ========================================================
 # libc_common.a
 # ========================================================
 
@@ -821,6 +850,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_common
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_WHOLE_STATIC_LIBRARIES := \
     libc_bionic \
@@ -834,6 +864,8 @@
     libc_syscalls \
     libc_tzcode \
 
+LOCAL_WHOLE_STATIC_LIBRARIES_arm := libc_aeabi
+
 ifeq ($(MALLOC_IMPL),jemalloc)
 LOCAL_WHOLE_STATIC_LIBRARIES += libjemalloc
 endif
@@ -874,6 +906,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 
 LOCAL_MODULE := libc_nomalloc
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_WHOLE_STATIC_LIBRARIES := libc_common
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
@@ -896,6 +929,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_malloc
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 include $(BUILD_STATIC_LIBRARY)
 
@@ -918,6 +952,7 @@
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_WHOLE_STATIC_LIBRARIES := libc_common
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
@@ -946,6 +981,7 @@
     bionic/NetdClient.cpp \
 
 LOCAL_MODULE := libc
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_REQUIRED_MODULES := tzdata
 
@@ -1016,6 +1052,7 @@
     bionic/malloc_debug_check.cpp \
 
 LOCAL_MODULE := libc_malloc_debug_leak
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 
 LOCAL_SHARED_LIBRARIES := libc libdl
@@ -1048,6 +1085,7 @@
     bionic/malloc_debug_qemu.cpp \
 
 LOCAL_MODULE := libc_malloc_debug_qemu
+LOCAL_CLANG := $(use_clang)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 
 LOCAL_SHARED_LIBRARIES := libc libdl
diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk
index 47e436c..00be4ae 100644
--- a/libc/arch-arm/arm.mk
+++ b/libc/arch-arm/arm.mk
@@ -52,7 +52,6 @@
     arch-arm/bionic/abort_arm.S \
     arch-arm/bionic/atomics_arm.c \
     arch-arm/bionic/__bionic_clone.S \
-    arch-arm/bionic/eabi.c \
     arch-arm/bionic/_exit_with_stack_teardown.S \
     arch-arm/bionic/__get_sp.S \
     arch-arm/bionic/libgcc_compat.c \
diff --git a/libc/arch-arm/bionic/eabi.c b/libc/arch-arm/bionic/__aeabi.c
similarity index 84%
rename from libc/arch-arm/bionic/eabi.c
rename to libc/arch-arm/bionic/__aeabi.c
index 5511ddd..3254f64 100644
--- a/libc/arch-arm/bionic/eabi.c
+++ b/libc/arch-arm/bionic/__aeabi.c
@@ -25,10 +25,19 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
+#if defined(__clang__)
+// clang interprets -fno-builtin more loosely than you might expect,
+// and thinks it's okay to still substitute builtins as long as they're
+// named __aeabi_* rather than __builtin_*, which causes infinite
+// recursion if we have the fortified memcpy visible in this file.
+#undef _FORTIFY_SOURCE
+#endif
+
 #include <stddef.h>
 #include <string.h>
 
-extern int  __cxa_atexit(void (*)(void*), void*, void* );
+extern int __cxa_atexit(void (*)(void*), void*, void*);
 
 /* The "C++ ABI for ARM" document states that static C++ constructors,
  * which are called from the .init_array, should manually call
@@ -38,11 +47,9 @@
  * variable from the shared object that contains the constructor/destructor
  */
 
-/* Make this a weak symbol to avoid a multiple definition error when linking
- * with libstdc++-v3.  */
+// Make this a weak symbol to avoid a multiple definition error when linking with libstdc++-v3.
 int __attribute__((weak))
-__aeabi_atexit (void *object, void (*destructor) (void *), void *dso_handle)
-{
+__aeabi_atexit(void *object, void (*destructor) (void *), void *dso_handle) {
     return __cxa_atexit(destructor, object, dso_handle);
 }
 
@@ -73,10 +80,10 @@
 }
 
 /*
- * __aeabi_memset has the order of its second and third arguments reversed. 
+ * __aeabi_memset has the order of its second and third arguments reversed.
  *  This allows __aeabi_memclr to tail-call __aeabi_memset
  */
- 
+
 void __aeabi_memset8(void *dest, size_t n, int c) {
     memset(dest, c, n);
 }