Reconfig libc's Android.mk to build for multilib

1. Moved arch-specific setup to their own files:
    - <arch>/<arch>.mk, arch-specific configs. Variables in those config
      end with the arch name.
    - removed the extra complexity introduced by function libc-add-cpu-variant-src,
      which seems to be not very useful these days.
2. Separated out the crt object files generation rules and set up the
   rules for both TARGET_ARCH and TARGET_2ND_ARCH.
3. Build all the libraries for both TARGET_ARCH and TARGET_2ND_ARCH,
  with the arch-specific LOCAL_ variables.

Bug: 11654773
Change-Id: I9c2d85db0affa49199d182236d2210060a321421
diff --git a/libc/Android.mk b/libc/Android.mk
index ce0e734..2c35ff3 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -1,8 +1,38 @@
 LOCAL_PATH := $(call my-dir)
 
 # Make everything depend on any changes to included makefiles.
-libc_common_additional_dependencies := \
-    $(LOCAL_PATH)/Android.mk \
+libc_common_additional_dependencies := $(LOCAL_PATH)/Android.mk
+
+# Load config for TARGET_ARCH
+my_2nd_arch_prefix :=
+include $(LOCAL_PATH)/arch-$(TARGET_ARCH)/$(TARGET_ARCH).mk
+libc_common_additional_dependencies += \
+    $(LOCAL_PATH)/arch-$(TARGET_ARCH)/$(TARGET_ARCH).mk
+
+
+ifdef TARGET_2ND_ARCH
+# Load config for TARGET_2ND_ARCH
+my_2nd_arch_prefix := $(TARGET_2ND_ARCH_VAR_PREFIX)
+include $(LOCAL_PATH)/arch-$(TARGET_2ND_ARCH)/$(TARGET_2ND_ARCH).mk
+my_2nd_arch_prefix :=
+libc_common_additional_dependencies += \
+    $(LOCAL_PATH)/arch-$(TARGET_2ND_ARCH)/$(TARGET_2ND_ARCH).mk
+endif
+
+# crt obj files
+# ========================================================
+# crtbrand.c needs <stdint.h> and a #define for the platform SDK version.
+libc_crt_target_cflags := \
+    -I$(LOCAL_PATH)/include \
+    -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION) \
+
+my_2nd_arch_prefix :=
+include $(LOCAL_PATH)/crt.mk
+ifdef TARGET_2ND_ARCH
+my_2nd_arch_prefix := $(TARGET_2ND_ARCH_VAR_PREFIX)
+include $(LOCAL_PATH)/crt.mk
+my_2nd_arch_prefix :=
+endif
 
 # Define the common source files for all the libc instances
 # =========================================================
@@ -162,26 +192,14 @@
     netbsd/resolv/res_state.c \
 
 
-# These are used by the 32-bit targets, but not the 64-bit ones.
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),arm mips x86))
-libc_common_src_files += \
-    bionic/legacy_32_bit_support.cpp \
-    bionic/ndk_cruft.cpp \
-
-endif
-
 # Fortify implementations of libc functions.
 libc_common_src_files += \
     bionic/__FD_chk.cpp \
     bionic/__fgets_chk.cpp \
-    bionic/__memcpy_chk.cpp \
     bionic/__memmove_chk.cpp \
-    bionic/__memset_chk.cpp \
     bionic/__read_chk.cpp \
     bionic/__recvfrom_chk.cpp \
-    bionic/__strcat_chk.cpp \
     bionic/__strchr_chk.cpp \
-    bionic/__strcpy_chk.cpp \
     bionic/__strlcat_chk.cpp \
     bionic/__strlcpy_chk.cpp \
     bionic/__strlen_chk.cpp \
@@ -291,19 +309,6 @@
     bionic/wait.cpp \
     bionic/wchar.cpp \
 
-# These are shared by all the 32-bit targets, but not the 64-bit ones.
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),arm mips x86))
-libc_bionic_src_files += \
-    bionic/mmap.cpp \
-
-endif
-
-libc_tzcode_src_files := \
-    tzcode/asctime.c \
-    tzcode/difftime.c \
-    tzcode/localtime.c \
-    tzcode/strftime.c \
-    tzcode/strptime.c \
 
 libc_upstream_freebsd_src_files := \
     upstream-freebsd/lib/libc/gen/sleep.c \
@@ -416,168 +421,8 @@
     upstream-netbsd/libc/thread-stub/__isthreaded.c \
     upstream-netbsd/libc/unistd/killpg.c \
 
-# Architecture specific source files go here
-# =========================================================
-ifeq ($(TARGET_ARCH),arm)
-libc_common_src_files += \
-    bionic/memchr.c \
-    bionic/memmove.c.arm \
-    bionic/memrchr.c \
-    bionic/strchr.cpp \
-    bionic/strnlen.c \
-    string/bcopy.c \
-    string/index.c \
-    string/strlcat.c \
-    string/strlcpy.c \
-    string/strncat.c \
-    string/strncmp.c \
-    string/strncpy.c \
-    string/strrchr.c \
-    upstream-freebsd/lib/libc/string/wcscat.c \
-    upstream-freebsd/lib/libc/string/wcschr.c \
-    upstream-freebsd/lib/libc/string/wcscmp.c \
-    upstream-freebsd/lib/libc/string/wcscpy.c \
-    upstream-freebsd/lib/libc/string/wcslen.c \
-    upstream-freebsd/lib/libc/string/wcsrchr.c \
-    upstream-freebsd/lib/libc/string/wmemcmp.c \
-
-endif # arm
-
-ifeq ($(TARGET_ARCH), arm64)
-#TODO: Replace C stubs with optimised assembly
-libc_common_src_files += \
-    bionic/memchr.c   \
-    bionic/memcmp.c   \
-    bionic/memcpy.c   \
-    bionic/memmove.c  \
-    bionic/memrchr.c  \
-    bionic/memset.c   \
-    bionic/strchr.cpp \
-    bionic/strnlen.c  \
-    string/bcopy.c    \
-    string/index.c    \
-    string/memcmp16.c \
-    string/strcat.c   \
-    string/strcmp.c   \
-    string/strcpy.c   \
-    string/strlcat.c  \
-    string/strlcpy.c  \
-    string/strlen.c   \
-    string/strncat.c  \
-    string/strncmp.c  \
-    string/strncpy.c  \
-    string/strrchr.c  \
-    upstream-freebsd/lib/libc/string/wcscat.c \
-    upstream-freebsd/lib/libc/string/wcschr.c \
-    upstream-freebsd/lib/libc/string/wcscmp.c \
-    upstream-freebsd/lib/libc/string/wcscpy.c \
-    upstream-freebsd/lib/libc/string/wcslen.c \
-    upstream-freebsd/lib/libc/string/wcsrchr.c \
-    upstream-freebsd/lib/libc/string/wmemcmp.c \
-
-endif # arm64
-
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),mips mips64))
-libc_common_src_files += \
-    bionic/memchr.c \
-    bionic/memcmp.c \
-    bionic/memmove.c \
-    bionic/memrchr.c \
-    bionic/strchr.cpp \
-    bionic/strnlen.c \
-    string/bcopy.c \
-    string/index.c \
-    string/strcat.c \
-    string/strcmp.c \
-    string/strcpy.c \
-    string/strlcat.c \
-    string/strlcpy.c \
-    string/strncat.c \
-    string/strncmp.c \
-    string/strncpy.c \
-    string/strrchr.c \
-    upstream-freebsd/lib/libc/string/wcscat.c \
-    upstream-freebsd/lib/libc/string/wcschr.c \
-    upstream-freebsd/lib/libc/string/wcscmp.c \
-    upstream-freebsd/lib/libc/string/wcscpy.c \
-    upstream-freebsd/lib/libc/string/wcslen.c \
-    upstream-freebsd/lib/libc/string/wcsrchr.c \
-    upstream-freebsd/lib/libc/string/wmemcmp.c \
-
-endif # mips || mips64
-
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86_64))
-libc_common_src_files += \
-    bionic/memchr.c \
-    bionic/memcmp.c \
-    bionic/memcpy.c \
-    bionic/memmove.c \
-    bionic/memrchr.c \
-    bionic/memset.c \
-    bionic/strchr.cpp \
-    bionic/strnlen.c \
-    string/bcopy.c \
-    string/index.c \
-    string/strcat.c \
-    string/strcmp.c \
-    string/strcpy.c \
-    string/strlcat.c \
-    string/strlcpy.c \
-    string/strlen.c \
-    string/strncat.c \
-    string/strncmp.c \
-    string/strncpy.c \
-    string/strrchr.c \
-    upstream-freebsd/lib/libc/string/wcscat.c \
-    upstream-freebsd/lib/libc/string/wcschr.c \
-    upstream-freebsd/lib/libc/string/wcscmp.c \
-    upstream-freebsd/lib/libc/string/wcscpy.c \
-    upstream-freebsd/lib/libc/string/wcslen.c \
-    upstream-freebsd/lib/libc/string/wcsrchr.c \
-    upstream-freebsd/lib/libc/string/wmemcmp.c \
-
-endif # x86_64
-
-ifeq ($(TARGET_ARCH),arm)
-  ifeq ($(strip $(TARGET_CPU_VARIANT)),)
-    $(warning TARGET_ARCH is arm, but TARGET_CPU_VARIANT is not defined)
-  endif
-endif
-
-###########################################################
-## Add cpu specific source files.
-##
-## This can be called multiple times, but it will only add
-## the first source file for each unique $(1).
-## $(1): Unique identifier to identify the cpu variant
-##       implementation.
-## $(2): Cpu specific source file.
-###########################################################
-
-define libc-add-cpu-variant-src
-$(if $(filter true,$(_LIBC_ARCH_CPU_VARIANT_HAS_$(1))), \
-	, \
-     $(eval _LIBC_ARCH_CPU_VARIANT_HAS_$(1) := true) \
-     $(eval _LIBC_ARCH_CPU_VARIANT_SRC_FILE.$(1) := $(2)) \
-     $(eval _LIBC_ARCH_CPU_VARIANT_SRC_FILES += $(2)) \
-)
-endef
-
-_LIBC_ARCH_COMMON_SRC_FILES :=
-_LIBC_ARCH_CPU_VARIANT_SRC_FILES :=
-_LIBC_ARCH_STATIC_SRC_FILES :=
-_LIBC_ARCH_DYNAMIC_SRC_FILES :=
-_LIBC_ARCH_ADDITIONAL_DEPENDENCIES :=
-
-libc_common_additional_dependencies += \
-    $(LOCAL_PATH)/arch-$(TARGET_ARCH)/$(TARGET_ARCH).mk
-include $(LOCAL_PATH)/arch-$(TARGET_ARCH)/$(TARGET_ARCH).mk
-
-libc_bionic_src_files += $(_LIBC_ARCH_COMMON_SRC_FILES)
-libc_bionic_src_files += $(_LIBC_ARCH_CPU_VARIANT_SRC_FILES)
-libc_arch_static_src_files := $(_LIBC_ARCH_STATIC_SRC_FILES) bionic/dl_iterate_phdr_static.cpp
-libc_arch_dynamic_src_files := $(_LIBC_ARCH_DYNAMIC_SRC_FILES)
-libc_common_additional_dependencies += $(_LIBC_ARCH_ADDITIONAL_DEPENDENCIES)
+libc_arch_static_src_files := \
+    bionic/dl_iterate_phdr_static.cpp \
 
 # Define some common cflags
 # ========================================================
@@ -603,40 +448,6 @@
   libc_common_cflags += -DMALLOC_ALIGNMENT=$(BOARD_MALLOC_ALIGNMENT)
 endif
 
-# crtbrand.c needs <stdint.h> and a #define for the platform SDK version.
-libc_crt_target_cflags := \
-    -I$(LOCAL_PATH)/include \
-    -I$(LOCAL_PATH)/arch-$(TARGET_ARCH)/include \
-    -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION) \
-
-ifeq ($(TARGET_ARCH),arm)
-  libc_common_cflags += -DSOFTFLOAT
-  libc_common_cflags += -fstrict-aliasing
-  libc_crt_target_cflags += -mthumb-interwork
-endif # arm
-
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),mips mips64))
-  ifneq ($(ARCH_MIPS_HAS_FPU),true)
-    libc_common_cflags += -DSOFTFLOAT
-  endif
-  libc_common_cflags += -fstrict-aliasing
-  libc_crt_target_cflags += $(TARGET_GLOBAL_CFLAGS)
-endif # mips
-ifeq ($(TARGET_ARCH),mips64)
-  libc_crt_target_ldflags := -melf64ltsmip
-endif
-
-
-ifeq ($(TARGET_ARCH),x86)
-  libc_crt_target_cflags += -m32
-  libc_crt_target_ldflags := -melf_i386
-endif # x86
-
-ifeq ($(TARGET_ARCH),x86_64)
-  libc_crt_target_cflags += -m64
-  libc_crt_target_ldflags := -melf_x86_64
-endif # x86_64
-
 # Define ANDROID_SMP appropriately.
 ifeq ($(TARGET_CPU_SMP),true)
     libc_common_cflags += -DANDROID_SMP=1
@@ -660,132 +471,18 @@
 		$(LOCAL_PATH)/stdio   \
 		external/safe-iop/include
 
-# Define the libc run-time (crt) support object files that must be built,
-# which are needed to build all other objects (shared/static libs and
-# executables)
-# ==========================================================================
-# ARM, Arm64, MIPS, and x86 all need crtbegin_so/crtend_so.
-#
-# For x86, the .init section must point to a function that calls all
-# entries in the .ctors section. (on ARM this is done through the
-# .init_array section instead).
-#
-# For all the platforms, the .fini_array section must point to a function
-# that will call __cxa_finalize(&__dso_handle) in order to ensure that
-# static C++ destructors are properly called on dlclose().
-#
-libc_crt_target_crtbegin_file := $(LOCAL_PATH)/arch-common/bionic/crtbegin.c
-libc_crt_target_crtbegin_so_file := $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
-
-ifeq ($(TARGET_ARCH),arm)
-    libc_crt_target_so_cflags :=
+# ========================================================
+# Add in the arch-specific flags.
+# Must be called with $(eval).
+# $(1): the LOCAL_ variable name
+# $(2): the bionic variable name to pull in
+define patch-up-arch-specific-flags
+$(1)_$(TARGET_ARCH) += $($(2)_$(TARGET_ARCH))
+ifdef TARGET_2ND_ARCH
+$(1)_$(TARGET_2ND_ARCH) += $($(2)_$(TARGET_2ND_ARCH))
 endif
-ifeq ($(TARGET_ARCH),arm64)
-    libc_crt_target_so_cflags :=
-    libc_crt_target_crtbegin_file := $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin.c
-endif
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),mips mips64))
-    libc_crt_target_so_cflags := -fPIC
-    libc_crt_target_crtbegin_file := $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin.c
-endif
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86 x86_64))
-    libc_crt_target_so_cflags := -fPIC
-endif
-libc_crt_target_so_cflags += $(libc_crt_target_cflags)
+endef
 
-# See the comment in crtbrand.c for the reason why we need to generate
-# crtbrand.s before generating crtbrand.o.
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.s
-$(GEN): $(LOCAL_PATH)/bionic/crtbrand.c
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_CC) $(libc_crt_target_so_cflags) -S \
-		-MD -MF $(@:%.s=%.d) -o $@ $<
-	$(hide) sed -i -e '/\.note\.ABI-tag/s/progbits/note/' $@
-	$(call transform-d-to-p-args,$(@:%.s=%.d),$(@:%.s=%.P))
--include $(GEN:%.s=%.P)
-ALL_GENERATED_SOURCES += $(GEN)
-
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
-$(GEN): $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.s
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_CC) $(libc_crt_target_so_cflags) -o $@ -c $<
-ALL_GENERATED_SOURCES += $(GEN)
-
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
-$(GEN): $(libc_crt_target_crtbegin_so_file)
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_CC) $(libc_crt_target_so_cflags) \
-		-MD -MF $(@:%.o=%.d) -o $@ -c $<
-	$(transform-d-to-p)
--include $(GEN:%.o=%.P)
-ALL_GENERATED_SOURCES += $(GEN)
-
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_so.o
-$(GEN): $(LOCAL_PATH)/arch-common/bionic/crtend_so.S
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_CC) $(libc_crt_target_so_cflags) \
-		-MD -MF $(@:%.o=%.d) -o $@ -c $<
-	$(transform-d-to-p)
--include $(GEN:%.o=%.P)
-ALL_GENERATED_SOURCES += $(GEN)
-
-# The following two are installed to device
-GEN := $(TARGET_OUT_SHARED_LIBRARIES)/crtbegin_so.o
-$(GEN): $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
-	$(hide) mkdir -p $(dir $@) && cp -f $< $@
-ALL_GENERATED_SOURCES += $(GEN)
-
-GEN := $(TARGET_OUT_SHARED_LIBRARIES)/crtend_so.o
-$(GEN): $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_so.o
-	$(hide) mkdir -p $(dir $@) && cp -f $< $@
-ALL_GENERATED_SOURCES += $(GEN)
-
-
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static1.o
-$(GEN): $(libc_crt_target_crtbegin_file)
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_CC) $(libc_crt_target_cflags) \
-		-MD -MF $(@:%.o=%.d) -o $@ -c $<
-	$(transform-d-to-p)
--include $(GEN:%.o=%.P)
-ALL_GENERATED_SOURCES += $(GEN)
-
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static.o
-$(GEN): $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static1.o $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_LD) $(libc_crt_target_ldflags) -r -o $@ $^
-ALL_GENERATED_SOURCES += $(GEN)
-
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic1.o
-$(GEN): $(libc_crt_target_crtbegin_file)
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_CC) $(libc_crt_target_cflags) \
-		-MD -MF $(@:%.o=%.d) -o $@ -c $<
-	$(transform-d-to-p)
--include $(GEN:%.o=%.P)
-ALL_GENERATED_SOURCES += $(GEN)
-
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic.o
-$(GEN): $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic1.o $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_LD) $(libc_crt_target_ldflags) -r -o $@ $^
-ALL_GENERATED_SOURCES += $(GEN)
-
-# We rename crtend.o to crtend_android.o to avoid a
-# name clash between gcc and bionic.
-GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_android.o
-$(GEN): $(LOCAL_PATH)/arch-common/bionic/crtend.S
-	@mkdir -p $(dir $@)
-	$(hide) $(TARGET_CC) $(libc_crt_target_cflags) \
-		-MD -MF $(@:%.o=%.d) -o $@ -c $<
-	$(transform-d-to-p)
--include $(GEN:%.o=%.P)
-ALL_GENERATED_SOURCES += $(GEN)
-
-
-# To enable malloc leak check for statically linked programs, add
-# "WITH_MALLOC_CHECK_LIBC_A := true" to buildspec.mk
-WITH_MALLOC_CHECK_LIBC_A := $(strip $(WITH_MALLOC_CHECK_LIBC_A))
 
 # ========================================================
 # libbionic_ssp.a - stack protector code
@@ -806,6 +503,7 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -815,7 +513,13 @@
 
 include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES := $(libc_tzcode_src_files)
+LOCAL_SRC_FILES := \
+    tzcode/asctime.c \
+    tzcode/difftime.c \
+    tzcode/localtime.c \
+    tzcode/strftime.c \
+    tzcode/strptime.c \
+
 LOCAL_CFLAGS := \
     $(libc_common_cflags) \
     -DSTD_INSPIRED=1 \
@@ -829,6 +533,7 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -852,6 +557,7 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -877,6 +583,7 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -903,6 +610,7 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -923,8 +631,10 @@
 
 # Set -DPTHREAD_DEBUG_ENABLED=true to enable support for pthread deadlock prediction.
 # Since this code is experimental it is disabled by default.
-LOCAL_CFLAGS += $(libc_common_cflags) -DPTHREAD_DEBUG_ENABLED=false
+LOCAL_CFLAGS += -DPTHREAD_DEBUG_ENABLED=false
 
+$(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)
 
 
@@ -934,7 +644,10 @@
 
 include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES := $(call all-S-files-under,arch-$(TARGET_ARCH)/syscalls)
+LOCAL_SRC_FILES_$(TARGET_ARCH) := $(call all-S-files-under,arch-$(TARGET_ARCH)/syscalls)
+ifdef TARGET_2ND_ARCH
+LOCAL_SRC_FILES_$(TARGET_2ND_ARCH) := $(call all-S-files-under,arch-$(TARGET_2ND_ARCH)/syscalls)
+endif
 LOCAL_MODULE := libc_syscalls
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
@@ -969,6 +682,9 @@
 # TODO: split out the asflags.
 LOCAL_ASFLAGS := $(LOCAL_CFLAGS)
 
+$(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_ASFLAGS,LOCAL_CFLAGS))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -985,9 +701,9 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
-	$(libc_arch_static_src_files) \
-	$(libc_static_common_src_files) \
-	bionic/libc_init_static.cpp
+    $(libc_arch_static_src_files) \
+    $(libc_static_common_src_files) \
+    bionic/libc_init_static.cpp
 
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_CFLAGS := $(libc_common_cflags) \
@@ -1000,6 +716,8 @@
 LOCAL_WHOLE_STATIC_LIBRARIES := libc_common
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
+$(eval $(call patch-up-arch-specific-flags,LOCAL_SRC_FILES,libc_arch_static_src_files))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -1009,11 +727,11 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
-	$(libc_arch_static_src_files) \
-	$(libc_static_common_src_files) \
-	bionic/dlmalloc.c \
-	bionic/malloc_debug_common.cpp \
-	bionic/libc_init_static.cpp
+    $(libc_arch_static_src_files) \
+    $(libc_static_common_src_files) \
+    bionic/dlmalloc.c \
+    bionic/malloc_debug_common.cpp \
+    bionic/libc_init_static.cpp \
 
 LOCAL_CFLAGS := $(libc_common_cflags) \
                 -DLIBC_STATIC
@@ -1025,6 +743,8 @@
 LOCAL_WHOLE_STATIC_LIBRARIES := libc_common
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
+$(eval $(call patch-up-arch-specific-flags,LOCAL_SRC_FILES,libc_arch_static_src_files))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -1048,17 +768,6 @@
     bionic/pthread_debug.cpp \
     bionic/libc_init_dynamic.cpp \
 
-ifeq ($(TARGET_ARCH),arm)
-	LOCAL_NO_CRT := true
-	LOCAL_CFLAGS += -DCRT_LEGACY_WORKAROUND
-
-	LOCAL_SRC_FILES := \
-		arch-common/bionic/crtbegin_so.c \
-		arch-arm/bionic/atexit_legacy.c \
-		$(LOCAL_SRC_FILES) \
-		arch-common/bionic/crtend_so.S
-endif
-
 LOCAL_MODULE:= libc
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_REQUIRED_MODULES := tzdata
@@ -1075,6 +784,17 @@
 LOCAL_WHOLE_STATIC_LIBRARIES := libc_common
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
+$(eval $(call patch-up-arch-specific-flags,libc_arch_dynamic_src_files))
+$(eval $(call patch-up-arch-specific-flags,libc_static_common_src_files))
+# special for arm
+LOCAL_NO_CRT_arm := true
+LOCAL_CFLAGS_arm += -DCRT_LEGACY_WORKAROUND
+LOCAL_SRC_FILES_arm += \
+    arch-common/bionic/crtbegin_so.c \
+    arch-arm/bionic/atexit_legacy.c \
+    arch-common/bionic/crtend_so.S
+
 include $(BUILD_SHARED_LIBRARY)
 
 
@@ -1092,18 +812,18 @@
 include $(CLEAR_VARS)
 
 LOCAL_CFLAGS := \
-	$(libc_common_cflags) \
-	-DMALLOC_LEAK_CHECK
+    $(libc_common_cflags) \
+    -DMALLOC_LEAK_CHECK
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 
 LOCAL_SRC_FILES := \
-	bionic/debug_mapinfo.cpp \
-	bionic/debug_stacktrace.cpp \
-	bionic/malloc_debug_leak.cpp \
-	bionic/malloc_debug_check.cpp \
+    bionic/debug_mapinfo.cpp \
+    bionic/debug_stacktrace.cpp \
+    bionic/malloc_debug_leak.cpp \
+    bionic/malloc_debug_check.cpp \
 
 LOCAL_MODULE:= libc_malloc_debug_leak
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
@@ -1116,6 +836,7 @@
 # Don't install on release build
 LOCAL_MODULE_TAGS := eng debug
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
 include $(BUILD_SHARED_LIBRARY)
 
 
@@ -1125,15 +846,15 @@
 include $(CLEAR_VARS)
 
 LOCAL_CFLAGS := \
-	$(libc_common_cflags) \
-	-DMALLOC_QEMU_INSTRUMENT
+    $(libc_common_cflags) \
+    -DMALLOC_QEMU_INSTRUMENT
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
 
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 
 LOCAL_SRC_FILES := \
-	bionic/malloc_debug_qemu.cpp
+    bionic/malloc_debug_qemu.cpp
 
 LOCAL_MODULE:= libc_malloc_debug_qemu
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
@@ -1145,9 +866,10 @@
 # Don't install on release build
 LOCAL_MODULE_TAGS := eng debug
 
+$(eval $(call patch-up-arch-specific-flags,LOCAL_CFLAGS,libc_common_cflags))
 include $(BUILD_SHARED_LIBRARY)
 
-endif	#!user
+endif  #!user
 
 
 # ========================================================
diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk
index 56b0d27..ccdf5a1 100644
--- a/libc/arch-arm/arm.mk
+++ b/libc/arch-arm/arm.mk
@@ -1,4 +1,54 @@
-_LIBC_ARCH_COMMON_SRC_FILES := \
+# arm specific configs
+
+# These are used by the 32-bit targets, but not the 64-bit ones.
+libc_common_src_files_arm := \
+    bionic/legacy_32_bit_support.cpp \
+    bionic/ndk_cruft.cpp \
+
+# These are shared by all the 32-bit targets, but not the 64-bit ones.
+libc_bionic_src_files_arm := \
+    bionic/mmap.cpp
+
+libc_common_src_files_arm += \
+    bionic/memchr.c \
+    bionic/memmove.c.arm \
+    bionic/memrchr.c \
+    bionic/strchr.cpp \
+    bionic/strnlen.c \
+    string/bcopy.c \
+    string/index.c \
+    string/strlcat.c \
+    string/strlcpy.c \
+    string/strncat.c \
+    string/strncmp.c \
+    string/strncpy.c \
+    string/strrchr.c \
+    upstream-freebsd/lib/libc/string/wcscat.c \
+    upstream-freebsd/lib/libc/string/wcschr.c \
+    upstream-freebsd/lib/libc/string/wcscmp.c \
+    upstream-freebsd/lib/libc/string/wcscpy.c \
+    upstream-freebsd/lib/libc/string/wcslen.c \
+    upstream-freebsd/lib/libc/string/wcsrchr.c \
+    upstream-freebsd/lib/libc/string/wmemcmp.c \
+
+# The C++ fortify function implementations for which there is an
+# arm assembler version.
+#
+# Fortify implementations of libc functions.
+# libc_common_src_files_arm +=
+#    bionic/__memcpy_chk.cpp \
+#    bionic/__memset_chk.cpp \
+#    bionic/__strcpy_chk.cpp \
+#    bionic/__strcat_chk.cpp \
+
+# cflags
+libc_common_cflags_arm := \
+    -DSOFTFLOAT \
+    -fstrict-aliasing
+
+##########################################
+### CPU specific source files
+libc_bionic_src_files_arm += \
     arch-arm/bionic/abort_arm.S \
     arch-arm/bionic/atomics_arm.c \
     arch-arm/bionic/__bionic_clone.S \
@@ -14,27 +64,37 @@
     arch-arm/bionic/sigsetjmp.S \
     arch-arm/bionic/syscall.S \
 
-_LIBC_ARCH_STATIC_SRC_FILES := \
+# These are used by the static and dynamic versions of the libc
+# respectively.
+libc_arch_static_src_files_arm := \
     arch-arm/bionic/exidx_static.c \
 
-_LIBC_ARCH_DYNAMIC_SRC_FILES := \
+libc_arch_dynamic_src_files_arm := \
     arch-arm/bionic/exidx_dynamic.c \
 
-# Remove the C++ fortify function implementations for which there is an
-# arm assembler version.
-_LIBC_FORTIFY_FILES_TO_REMOVE := \
-    bionic/__memcpy_chk.cpp \
-    bionic/__memset_chk.cpp \
-    bionic/__strcpy_chk.cpp \
-    bionic/__strcat_chk.cpp \
-
-libc_common_src_files := \
-    $(filter-out $(_LIBC_FORTIFY_FILES_TO_REMOVE),$(libc_common_src_files))
-
-ifeq ($(strip $(wildcard bionic/libc/arch-arm/$(TARGET_CPU_VARIANT)/$(TARGET_CPU_VARIANT).mk)),)
-$(error "TARGET_CPU_VARIANT not set or set to an unknown value. Possible values are cortex-a7, cortex-a8, cortex-a9, cortex-a15, krait. Use generic for devices that do not have a CPU similar to any of the supported cpu variants.")
+## CPU variant specific source files
+ifeq ($(strip $(TARGET_$(my_2nd_arch_prefix)CPU_VARIANT)),)
+  $(warning TARGET_$(my_2nd_arch_prefix)ARCH is arm, but TARGET_$(my_2nd_arch_prefix)CPU_VARIANT is not defined)
 endif
+cpu_variant_mk := $(LOCAL_PATH)/arch-arm/$(TARGET_$(my_2nd_arch_prefix)CPU_VARIANT)/$(TARGET_$(my_2nd_arch_prefix)CPU_VARIANT).mk
+ifeq ($(wildcard $(cpu_variant_mk)),)
+$(error "TARGET_$(my_2nd_arch_prefix)CPU_VARIANT not set or set to an unknown value. Possible values are cortex-a7, cortex-a8, cortex-a9, cortex-a15, krait. Use generic for devices that do not have a CPU similar to any of the supported cpu variants.")
+endif
+include $(cpu_variant_mk)
+libc_common_additional_dependencies += $(cpu_variant_mk)
 
-_LIBC_ARCH_ADDITIONAL_DEPENDENCIES := \
-    $(LOCAL_PATH)/arch-arm/$(TARGET_CPU_VARIANT)/$(TARGET_CPU_VARIANT).mk
-include $(LOCAL_PATH)/arch-arm/$(TARGET_CPU_VARIANT)/$(TARGET_CPU_VARIANT).mk
+cpu_variant_mk :=
+
+##########################################
+# crt-related
+libc_crt_target_cflags_arm := \
+    -I$(LOCAL_PATH)/arch-arm/include \
+    -mthumb-interwork
+
+libc_crt_target_so_cflags_arm :=
+
+libc_crt_target_crtbegin_file_arm := \
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin.c
+
+libc_crt_target_crtbegin_so_file_arm := \
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
diff --git a/libc/arch-arm/cortex-a15/cortex-a15.mk b/libc/arch-arm/cortex-a15/cortex-a15.mk
index c62e7e7..d0896af 100644
--- a/libc/arch-arm/cortex-a15/cortex-a15.mk
+++ b/libc/arch-arm/cortex-a15/cortex-a15.mk
@@ -1,10 +1,9 @@
-$(call libc-add-cpu-variant-src,MEMCPY,arch-arm/cortex-a15/bionic/memcpy.S)
-$(call libc-add-cpu-variant-src,MEMSET,arch-arm/cortex-a15/bionic/memset.S)
-$(call libc-add-cpu-variant-src,STRCAT,arch-arm/cortex-a15/bionic/strcat.S)
-$(call libc-add-cpu-variant-src,STRCMP,arch-arm/cortex-a15/bionic/strcmp.S)
-$(call libc-add-cpu-variant-src,STRCPY,arch-arm/cortex-a15/bionic/strcpy.S)
-$(call libc-add-cpu-variant-src,STRLEN,arch-arm/cortex-a15/bionic/strlen.S)
-$(call libc-add-cpu-variant-src,__STRCAT_CHK,arch-arm/cortex-a15/bionic/__strcat_chk.S)
-$(call libc-add-cpu-variant-src,__STRCPY_CHK,arch-arm/cortex-a15/bionic/__strcpy_chk.S)
-
-include bionic/libc/arch-arm/generic/generic.mk
+libc_bionic_src_files_arm += \
+    arch-arm/cortex-a15/bionic/memcpy.S \
+    arch-arm/cortex-a15/bionic/memset.S \
+    arch-arm/cortex-a15/bionic/strcat.S \
+    arch-arm/cortex-a15/bionic/strcmp.S \
+    arch-arm/cortex-a15/bionic/strcpy.S \
+    arch-arm/cortex-a15/bionic/strlen.S \
+    arch-arm/cortex-a15/bionic/__strcat_chk.S \
+    arch-arm/cortex-a15/bionic/__strcpy_chk.S \
diff --git a/libc/arch-arm/cortex-a9/cortex-a9.mk b/libc/arch-arm/cortex-a9/cortex-a9.mk
index eee1b36..e15602b 100644
--- a/libc/arch-arm/cortex-a9/cortex-a9.mk
+++ b/libc/arch-arm/cortex-a9/cortex-a9.mk
@@ -1,10 +1,9 @@
-$(call libc-add-cpu-variant-src,MEMCPY,arch-arm/cortex-a9/bionic/memcpy.S)
-$(call libc-add-cpu-variant-src,MEMSET,arch-arm/cortex-a9/bionic/memset.S)
-$(call libc-add-cpu-variant-src,STRCAT,arch-arm/cortex-a9/bionic/strcat.S)
-$(call libc-add-cpu-variant-src,STRCMP,arch-arm/cortex-a9/bionic/strcmp.S)
-$(call libc-add-cpu-variant-src,STRCPY,arch-arm/cortex-a9/bionic/strcpy.S)
-$(call libc-add-cpu-variant-src,STRLEN,arch-arm/cortex-a9/bionic/strlen.S)
-$(call libc-add-cpu-variant-src,__STRCAT_CHK,arch-arm/cortex-a9/bionic/__strcat_chk.S)
-$(call libc-add-cpu-variant-src,__STRCPY_CHK,arch-arm/cortex-a9/bionic/__strcpy_chk.S)
-
-include bionic/libc/arch-arm/generic/generic.mk
+libc_bionic_src_files_arm += \
+    arch-arm/cortex-a9/bionic/memcpy.S \
+    arch-arm/cortex-a9/bionic/memset.S \
+    arch-arm/cortex-a9/bionic/strcat.S \
+    arch-arm/cortex-a9/bionic/strcmp.S \
+    arch-arm/cortex-a9/bionic/strcpy.S \
+    arch-arm/cortex-a9/bionic/strlen.S \
+    arch-arm/cortex-a9/bionic/__strcat_chk.S \
+    arch-arm/cortex-a9/bionic/__strcpy_chk.S \
diff --git a/libc/arch-arm/generic/generic.mk b/libc/arch-arm/generic/generic.mk
index e230003..063d893 100644
--- a/libc/arch-arm/generic/generic.mk
+++ b/libc/arch-arm/generic/generic.mk
@@ -1,8 +1,9 @@
-$(call libc-add-cpu-variant-src,MEMCPY,arch-arm/generic/bionic/memcpy.S)
-$(call libc-add-cpu-variant-src,MEMSET,arch-arm/generic/bionic/memset.S)
-$(call libc-add-cpu-variant-src,STRCAT,string/strcat.c)
-$(call libc-add-cpu-variant-src,STRCMP,arch-arm/generic/bionic/strcmp.S)
-$(call libc-add-cpu-variant-src,STRCPY,arch-arm/generic/bionic/strcpy.S)
-$(call libc-add-cpu-variant-src,STRLEN,arch-arm/generic/bionic/strlen.c)
-$(call libc-add-cpu-variant-src,__STRCAT_CHK,bionic/__strcat_chk.cpp)
-$(call libc-add-cpu-variant-src,__STRCPY_CHK,bionic/__strcpy_chk.cpp)
+libc_bionic_src_files_arm += \
+    arch-arm/generic/bionic/memcpy.S \
+    arch-arm/generic/bionic/memset.S \
+    string/strcat.c \
+    arch-arm/generic/bionic/strcmp.S \
+    arch-arm/generic/bionic/strcpy.S \
+    arch-arm/generic/bionic/strlen.c \
+    bionic/__strcat_chk.cpp \
+    bionic/__strcpy_chk.cpp \
diff --git a/libc/arch-arm/krait/krait.mk b/libc/arch-arm/krait/krait.mk
index 29ab743..08342d6 100644
--- a/libc/arch-arm/krait/krait.mk
+++ b/libc/arch-arm/krait/krait.mk
@@ -1,11 +1,12 @@
-$(call libc-add-cpu-variant-src,MEMCPY,arch-arm/krait/bionic/memcpy.S)
-$(call libc-add-cpu-variant-src,MEMSET,arch-arm/krait/bionic/memset.S)
-$(call libc-add-cpu-variant-src,STRCMP,arch-arm/krait/bionic/strcmp.S)
-$(call libc-add-cpu-variant-src,__STRCAT_CHK,arch-arm/krait/bionic/__strcat_chk.S)
-$(call libc-add-cpu-variant-src,__STRCPY_CHK,arch-arm/krait/bionic/__strcpy_chk.S)
-# Use cortex-a15 versions of strcat/strcpy/strlen.
-$(call libc-add-cpu-variant-src,STRCAT,arch-arm/cortex-a15/bionic/strcat.S)
-$(call libc-add-cpu-variant-src,STRCPY,arch-arm/cortex-a15/bionic/strcpy.S)
-$(call libc-add-cpu-variant-src,STRLEN,arch-arm/cortex-a15/bionic/strlen.S)
+libc_bionic_src_files_arm += \
+    arch-arm/krait/bionic/memcpy.S \
+    arch-arm/krait/bionic/memset.S \
+    arch-arm/krait/bionic/strcmp.S \
+    arch-arm/krait/bionic/__strcat_chk.S \
+    arch-arm/krait/bionic/__strcpy_chk.S \
 
-include bionic/libc/arch-arm/generic/generic.mk
+# Use cortex-a15 versions of strcat/strcpy/strlen.
+libc_bionic_src_files_arm += \
+    arch-arm/cortex-a15/bionic/strcat.S \
+    arch-arm/cortex-a15/bionic/strcpy.S \
+    arch-arm/cortex-a15/bionic/strlen.S \
diff --git a/libc/arch-arm64/arm64.mk b/libc/arch-arm64/arm64.mk
index 11894f1..4b47c6d 100644
--- a/libc/arch-arm64/arm64.mk
+++ b/libc/arch-arm64/arm64.mk
@@ -1,4 +1,45 @@
-_LIBC_ARCH_COMMON_SRC_FILES := \
+# arm64 specific configs
+
+libc_common_src_files_arm64 := \
+    bionic/memchr.c   \
+    bionic/memcmp.c   \
+    bionic/memcpy.c   \
+    bionic/memmove.c  \
+    bionic/memrchr.c  \
+    bionic/memset.c   \
+    bionic/strchr.cpp \
+    bionic/strnlen.c  \
+    string/bcopy.c    \
+    string/index.c    \
+    string/memcmp16.c \
+    string/strcat.c   \
+    string/strcmp.c   \
+    string/strcpy.c   \
+    string/strlcat.c  \
+    string/strlcpy.c  \
+    string/strlen.c   \
+    string/strncat.c  \
+    string/strncmp.c  \
+    string/strncpy.c  \
+    string/strrchr.c  \
+    upstream-freebsd/lib/libc/string/wcscat.c \
+    upstream-freebsd/lib/libc/string/wcschr.c \
+    upstream-freebsd/lib/libc/string/wcscmp.c \
+    upstream-freebsd/lib/libc/string/wcscpy.c \
+    upstream-freebsd/lib/libc/string/wcslen.c \
+    upstream-freebsd/lib/libc/string/wcsrchr.c \
+    upstream-freebsd/lib/libc/string/wmemcmp.c \
+
+# Fortify implementations of libc functions.
+libc_common_src_files_arm64 += \
+    bionic/__memcpy_chk.cpp \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
+
+##########################################
+### CPU specific source files
+libc_bionic_src_files_arm64 := \
     arch-arm64/bionic/__bionic_clone.S \
     arch-arm64/bionic/bzero_arm64.c \
     arch-arm64/bionic/_exit_with_stack_teardown.S \
@@ -11,3 +52,20 @@
     arch-arm64/bionic/sigsetjmp.S \
     arch-arm64/bionic/syscall.S \
     arch-arm64/bionic/vfork.S \
+
+# These are used by the static and dynamic versions of the libc
+# respectively.
+libc_arch_static_src_files_arm64 :=
+
+libc_arch_dynamic_src_files_arm64 :=
+
+##########################################
+# crt-related
+libc_crt_target_cflags_arm64 := \
+    -I$(LOCAL_PATH)/arch-arm64/include
+
+libc_crt_target_crtbegin_file_arm64 := \
+    $(LOCAL_PATH)/arch-arm64/bionic/crtbegin.c
+
+libc_crt_target_crtbegin_so_file_arm64 := \
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
diff --git a/libc/arch-mips/mips.mk b/libc/arch-mips/mips.mk
index aa8e1ff..4e007e5 100644
--- a/libc/arch-mips/mips.mk
+++ b/libc/arch-mips/mips.mk
@@ -1,4 +1,59 @@
-_LIBC_ARCH_COMMON_SRC_FILES := \
+# mips specific configs
+
+# These are shared by all the 32-bit targets, but not the 64-bit ones.
+libc_common_src_files_mips := \
+    bionic/legacy_32_bit_support.cpp \
+    bionic/ndk_cruft.cpp \
+
+# These are shared by all the 32-bit targets, but not the 64-bit ones.
+libc_bionic_src_files_mips += \
+     bionic/mmap.cpp
+
+libc_common_src_files_mips += \
+    bionic/memchr.c \
+    bionic/memcmp.c \
+    bionic/memmove.c \
+    bionic/memrchr.c \
+    bionic/strchr.cpp \
+    bionic/strnlen.c \
+    string/bcopy.c \
+    string/index.c \
+    string/strcat.c \
+    string/strcmp.c \
+    string/strcpy.c \
+    string/strlcat.c \
+    string/strlcpy.c \
+    string/strncat.c \
+    string/strncmp.c \
+    string/strncpy.c \
+    string/strrchr.c \
+    upstream-freebsd/lib/libc/string/wcscat.c \
+    upstream-freebsd/lib/libc/string/wcschr.c \
+    upstream-freebsd/lib/libc/string/wcscmp.c \
+    upstream-freebsd/lib/libc/string/wcscpy.c \
+    upstream-freebsd/lib/libc/string/wcslen.c \
+    upstream-freebsd/lib/libc/string/wcsrchr.c \
+    upstream-freebsd/lib/libc/string/wmemcmp.c \
+
+# Fortify implementations of libc functions.
+libc_common_src_files_mips += \
+    bionic/__memcpy_chk.cpp \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
+
+
+# cflags
+ifneq ($(ARCH_MIPS_HAS_FPU),true)
+libc_common_cflags_mips := \
+    -DSOFTFLOAT
+endif
+libc_common_cflags_mips += \
+    -fstrict-aliasing
+
+##########################################
+### CPU specific source files
+libc_bionic_src_files_mips += \
     arch-mips/bionic/__bionic_clone.S \
     arch-mips/bionic/bzero.S \
     arch-mips/bionic/cacheflush.cpp \
@@ -15,3 +70,25 @@
     arch-mips/string/memcpy.S \
     arch-mips/string/memset.S \
     arch-mips/string/mips_strlen.c \
+
+# These are used by the static and dynamic versions of the libc
+# respectively.
+libc_arch_static_src_files_mips :=
+
+libc_arch_dynamic_src_files_mips :=
+
+
+##########################################
+# crt-related
+libc_crt_target_cflags_mips := \
+    $($(my_2nd_arch_prefix)TARGET_GLOBAL_CFLAGS) \
+    -I$(LOCAL_PATH)/arch-mips/include
+
+libc_crt_target_crtbegin_file_mips := \
+    $(LOCAL_PATH)/arch-mips/bionic/crtbegin.c
+
+libc_crt_target_crtbegin_so_file_mips := \
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
+
+libc_crt_target_so_cflags_mips := \
+    -fPIC
diff --git a/libc/arch-mips64/mips64.mk b/libc/arch-mips64/mips64.mk
index 19be126..6977d6a 100644
--- a/libc/arch-mips64/mips64.mk
+++ b/libc/arch-mips64/mips64.mk
@@ -1,4 +1,42 @@
-_LIBC_ARCH_COMMON_SRC_FILES := \
+# mips64 specific configs
+
+libc_common_src_files_mips64 := \
+  bionic/memchr.c \
+  bionic/memcmp.c \
+  bionic/memmove.c \
+  bionic/memrchr.c \
+  bionic/strchr.cpp \
+  bionic/strnlen.c \
+  string/bcopy.c \
+  string/index.c \
+  string/strcat.c \
+  string/strcmp.c \
+  string/strcpy.c \
+  string/strlcat.c \
+  string/strlcpy.c \
+  string/strncat.c \
+  string/strncmp.c \
+  string/strncpy.c \
+  string/strrchr.c \
+  upstream-freebsd/lib/libc/string/wcscat.c \
+  upstream-freebsd/lib/libc/string/wcschr.c \
+  upstream-freebsd/lib/libc/string/wcscmp.c \
+  upstream-freebsd/lib/libc/string/wcscpy.c \
+  upstream-freebsd/lib/libc/string/wcslen.c \
+  upstream-freebsd/lib/libc/string/wcsrchr.c \
+  upstream-freebsd/lib/libc/string/wmemcmp.c \
+
+# Fortify implementations of libc functions.
+libc_common_src_files_mips64 += \
+    bionic/__memcpy_chk.cpp \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
+
+
+##########################################
+### CPU specific source files
+libc_bionic_src_files_mips64 := \
     arch-mips64/bionic/__bionic_clone.S \
     arch-mips64/bionic/bzero.S \
     arch-mips64/bionic/_exit_with_stack_teardown.S \
@@ -14,9 +52,31 @@
     arch-mips64/bionic/vfork.S \
 
 # FIXME TODO
-## _LIBC_ARCH_COMMON_SRC_FILES += arch-mips64/string/memcpy.S
-## _LIBC_ARCH_COMMON_SRC_FILES += arch-mips64/string/memset.S
-## _LIBC_ARCH_COMMON_SRC_FILES += arch-mips64/string/mips_strlen.c
-_LIBC_ARCH_COMMON_SRC_FILES += bionic/memcpy.c
-_LIBC_ARCH_COMMON_SRC_FILES += bionic/memset.c
-_LIBC_ARCH_COMMON_SRC_FILES += string/strlen.c
+## libc_bionic_src_files_mips64 += arch-mips64/string/memcpy.S
+## libc_bionic_src_files_mips64 += arch-mips64/string/memset.S
+## libc_bionic_src_files_mips64 += arch-mips64/string/mips_strlen.c
+libc_bionic_src_files_mips64 += bionic/memcpy.c
+libc_bionic_src_files_mips64 += bionic/memset.c
+libc_bionic_src_files_mips64 += string/strlen.c
+
+libc_arch_static_src_files_mips64 :=
+
+libc_arch_dynamic_src_files_mips64 :=
+
+##########################################
+# crt-related
+libc_crt_target_cflags_mips64 := \
+    $($(my_2nd_arch_prefix)TARGET_GLOBAL_CFLAGS) \
+    -I$(LOCAL_PATH)/arch-mips/include
+
+libc_crt_target_crtbegin_file_mips64 := \
+    $(LOCAL_PATH)/arch-mips/bionic/crtbegin.c
+
+libc_crt_target_crtbegin_so_file_mips64 := \
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
+
+libc_crt_target_so_cflags_mips64 := \
+    -fPIC
+
+libc_crt_target_ldflags_mips64 := \
+    -melf64ltsmip
diff --git a/libc/arch-x86/x86.mk b/libc/arch-x86/x86.mk
index 912ce5a..a39fcf2 100644
--- a/libc/arch-x86/x86.mk
+++ b/libc/arch-x86/x86.mk
@@ -1,4 +1,25 @@
-_LIBC_ARCH_COMMON_SRC_FILES := \
+# x86 specific configs
+
+# These are shared by all the 32-bit targets, but not the 64-bit ones.
+libc_common_src_files_x86 := \
+    bionic/legacy_32_bit_support.cpp \
+    bionic/ndk_cruft.cpp \
+
+# Fortify implementations of libc functions.
+libc_common_src_files_x86 += \
+    bionic/__memcpy_chk.cpp \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
+
+
+# These are shared by all the 32-bit targets, but not the 64-bit ones.
+libc_bionic_src_files_x86 := \
+    bionic/mmap.cpp
+
+##########################################
+### CPU specific source files
+libc_bionic_src_files_x86 += \
     arch-x86/bionic/__bionic_clone.S \
     arch-x86/bionic/_exit_with_stack_teardown.S \
     arch-x86/bionic/futex_x86.S \
@@ -11,54 +32,78 @@
     arch-x86/bionic/vfork.S \
 
 ifeq ($(ARCH_X86_HAVE_SSSE3),true)
-_LIBC_ARCH_COMMON_SRC_FILES += \
-	arch-x86/string/ssse3-memcpy-atom.S \
-	arch-x86/string/ssse3-memmove-atom.S \
-	arch-x86/string/ssse3-bcopy-atom.S \
-	arch-x86/string/ssse3-strncat-atom.S \
-	arch-x86/string/ssse3-strncpy-atom.S \
-	arch-x86/string/ssse3-strlcat-atom.S \
-	arch-x86/string/ssse3-strlcpy-atom.S \
-	arch-x86/string/ssse3-strcmp-atom.S \
-	arch-x86/string/ssse3-strncmp-atom.S \
-	arch-x86/string/ssse3-strcat-atom.S \
-	arch-x86/string/ssse3-strcpy-atom.S \
-	arch-x86/string/ssse3-memcmp-atom.S \
-	arch-x86/string/ssse3-wmemcmp-atom.S \
-	arch-x86/string/ssse3-memcmp16-atom.S \
-	arch-x86/string/ssse3-wcscat-atom.S \
-	arch-x86/string/ssse3-wcscpy-atom.S
+libc_bionic_src_files_x86 += \
+    arch-x86/string/ssse3-memcpy-atom.S \
+    arch-x86/string/ssse3-memmove-atom.S \
+    arch-x86/string/ssse3-bcopy-atom.S \
+    arch-x86/string/ssse3-strncat-atom.S \
+    arch-x86/string/ssse3-strncpy-atom.S \
+    arch-x86/string/ssse3-strlcat-atom.S \
+    arch-x86/string/ssse3-strlcpy-atom.S \
+    arch-x86/string/ssse3-strcmp-atom.S \
+    arch-x86/string/ssse3-strncmp-atom.S \
+    arch-x86/string/ssse3-strcat-atom.S \
+    arch-x86/string/ssse3-strcpy-atom.S \
+    arch-x86/string/ssse3-memcmp-atom.S \
+    arch-x86/string/ssse3-wmemcmp-atom.S \
+    arch-x86/string/ssse3-memcmp16-atom.S \
+    arch-x86/string/ssse3-wcscat-atom.S \
+    arch-x86/string/ssse3-wcscpy-atom.S
 else
-_LIBC_ARCH_COMMON_SRC_FILES += \
-	arch-x86/string/memcpy.S \
-	arch-x86/string/memmove.S \
-	arch-x86/string/bcopy.S \
-	arch-x86/string/strcmp.S \
-	arch-x86/string/strncmp.S \
-	arch-x86/string/strcat.S \
-	arch-x86/string/memcmp.S \
-	string/memcmp16.c \
-	string/strcpy.c \
-	string/strncat.c \
-	string/strncpy.c \
-	string/strlcat.c \
-	string/strlcpy.c \
-	upstream-freebsd/lib/libc/string/wcscpy.c \
-	upstream-freebsd/lib/libc/string/wcscat.c \
-	upstream-freebsd/lib/libc/string/wmemcmp.c
+libc_bionic_src_files_x86 += \
+    arch-x86/string/memcpy.S \
+    arch-x86/string/memmove.S \
+    arch-x86/string/bcopy.S \
+    arch-x86/string/strcmp.S \
+    arch-x86/string/strncmp.S \
+    arch-x86/string/strcat.S \
+    arch-x86/string/memcmp.S \
+    string/memcmp16.c \
+    string/strcpy.c \
+    string/strncat.c \
+    string/strncpy.c \
+    string/strlcat.c \
+    string/strlcpy.c \
+    upstream-freebsd/lib/libc/string/wcscpy.c \
+    upstream-freebsd/lib/libc/string/wcscat.c \
+    upstream-freebsd/lib/libc/string/wmemcmp.c
 endif
 
-_LIBC_ARCH_COMMON_SRC_FILES += \
-	arch-x86/string/sse2-memset-atom.S \
-	arch-x86/string/sse2-bzero-atom.S \
-	arch-x86/string/sse2-memchr-atom.S \
-	arch-x86/string/sse2-memrchr-atom.S \
-	arch-x86/string/sse2-strchr-atom.S \
-	arch-x86/string/sse2-strrchr-atom.S \
-	arch-x86/string/sse2-index-atom.S \
-	arch-x86/string/sse2-strlen-atom.S \
-	arch-x86/string/sse2-strnlen-atom.S \
-	arch-x86/string/sse2-wcschr-atom.S \
-	arch-x86/string/sse2-wcsrchr-atom.S \
-	arch-x86/string/sse2-wcslen-atom.S \
-	arch-x86/string/sse2-wcscmp-atom.S \
+libc_bionic_src_files_x86 += \
+    arch-x86/string/sse2-memset-atom.S \
+    arch-x86/string/sse2-bzero-atom.S \
+    arch-x86/string/sse2-memchr-atom.S \
+    arch-x86/string/sse2-memrchr-atom.S \
+    arch-x86/string/sse2-strchr-atom.S \
+    arch-x86/string/sse2-strrchr-atom.S \
+    arch-x86/string/sse2-index-atom.S \
+    arch-x86/string/sse2-strlen-atom.S \
+    arch-x86/string/sse2-strnlen-atom.S \
+    arch-x86/string/sse2-wcschr-atom.S \
+    arch-x86/string/sse2-wcsrchr-atom.S \
+    arch-x86/string/sse2-wcslen-atom.S \
+    arch-x86/string/sse2-wcscmp-atom.S \
+
+# These are used by the static and dynamic versions of the libc
+# respectively.
+libc_arch_static_src_files_x86 :=
+
+libc_arch_dynamic_src_files_x86 :=
+
+
+##########################################
+# crt-related
+libc_crt_target_cflags_x86 := \
+    -m32 \
+    -I$(LOCAL_PATH)/arch-x86/include
+
+libc_crt_target_ldflags_x86 := -melf_i386
+
+libc_crt_target_crtbegin_file_x86 := \
+     $(LOCAL_PATH)/arch-common/bionic/crtbegin.c
+
+libc_crt_target_crtbegin_so_file_x86 := \
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
+
+libc_crt_target_so_cflags_x86 := \
+    -fPIC
diff --git a/libc/arch-x86_64/x86_64.mk b/libc/arch-x86_64/x86_64.mk
index f4d3ef1..5d2b05b 100644
--- a/libc/arch-x86_64/x86_64.mk
+++ b/libc/arch-x86_64/x86_64.mk
@@ -1,4 +1,45 @@
-_LIBC_ARCH_COMMON_SRC_FILES := \
+# x86_64 specific configs
+
+libc_common_src_files_x86_64 := \
+    bionic/memchr.c \
+    bionic/memcmp.c \
+    bionic/memcpy.c \
+    bionic/memmove.c \
+    bionic/memrchr.c \
+    bionic/memset.c \
+    bionic/strchr.cpp \
+    bionic/strnlen.c \
+    string/bcopy.c \
+    string/index.c \
+    string/strcat.c \
+    string/strcmp.c \
+    string/strcpy.c \
+    string/strlcat.c \
+    string/strlcpy.c \
+    string/strlen.c \
+    string/strncat.c \
+    string/strncmp.c \
+    string/strncpy.c \
+    string/strrchr.c \
+    upstream-freebsd/lib/libc/string/wcscat.c \
+    upstream-freebsd/lib/libc/string/wcschr.c \
+    upstream-freebsd/lib/libc/string/wcscmp.c \
+    upstream-freebsd/lib/libc/string/wcscpy.c \
+    upstream-freebsd/lib/libc/string/wcslen.c \
+    upstream-freebsd/lib/libc/string/wcsrchr.c \
+    upstream-freebsd/lib/libc/string/wmemcmp.c \
+
+# Fortify implementations of libc functions.
+libc_common_src_files_x86_64 += \
+    bionic/__memcpy_chk.cpp \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
+
+
+##########################################
+### CPU specific source files
+libc_bionic_src_files_x86_64 := \
     arch-x86_64/bionic/__bionic_clone.S \
     arch-x86_64/bionic/_exit_with_stack_teardown.S \
     arch-x86_64/bionic/futex_x86_64.S \
@@ -11,3 +52,26 @@
     arch-x86_64/bionic/syscall.S \
     arch-x86_64/bionic/vfork.S \
     string/memcmp16.c \
+
+# These are used by the static and dynamic versions of the libc
+# respectively.
+libc_arch_static_src_files_x86_64 :=
+
+libc_arch_dynamic_src_files_x86_64 :=
+
+##########################################
+# crt-related
+libc_crt_target_cflags_x86_64 += \
+    -m64 \
+    -I$(LOCAL_PATH)/arch-x86_64/include
+
+libc_crt_target_ldflags_x86_64 := -melf_x86_64
+
+libc_crt_target_crtbegin_file_x86_64 := \
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin.c
+
+libc_crt_target_crtbegin_so_file_x86_64 := \
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
+
+libc_crt_target_so_cflags_x86_64 := \
+    -fPIC
diff --git a/libc/crt.mk b/libc/crt.mk
new file mode 100644
index 0000000..c3ba54b
--- /dev/null
+++ b/libc/crt.mk
@@ -0,0 +1,149 @@
+# Define the libc run-time (crt) support object files that must be built,
+# which are needed to build all other objects (shared/static libs and
+# executables)
+# ==========================================================================
+# AArch64, ARM, MIPS, and x86 all need crtbegin_so/crtend_so.
+#
+# For x86, the .init section must point to a function that calls all
+# entries in the .ctors section. (on ARM this is done through the
+# .init_array section instead).
+#
+# For all the platforms, the .fini_array section must point to a function
+# that will call __cxa_finalize(&__dso_handle) in order to ensure that
+# static C++ destructors are properly called on dlclose().
+#
+# Args:
+#     my_2nd_arch_prefix: set to $(TARGET_2ND_ARCH_VAR_PREFIX) if it's
+#                         for the 2nd arch; otherwise empty.
+
+my_arch := $(TARGET_$(my_2nd_arch_prefix)ARCH)
+
+my_libc_crt_target_crtbegin_file := $(libc_crt_target_crtbegin_file_$(my_arch))
+my_libc_crt_target_crtbegin_so_file := $(libc_crt_target_crtbegin_so_file_$(my_arch))
+
+my_libc_crt_target_cflags := \
+    $(libc_crt_target_cflags) \
+    $(libc_crt_target_cflags_$(my_arch))
+
+my_libc_crt_target_so_cflags := \
+    $(libc_crt_target_so_cflags_$(my_arch)) \
+    $(my_libc_crt_target_cflags)
+
+my_libc_crt_target_ldflags := $(libc_crt_target_ldflags_$(my_arch))
+
+
+# See the comment in crtbrand.c for the reason why we need to generate
+# crtbrand.s before generating crtbrand.o.
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.s
+$(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
+$(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_so_cflags)
+$(GEN): $(LOCAL_PATH)/bionic/crtbrand.c
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_CC) $(PRIVATE_CFLAGS) -S \
+		-MD -MF $(@:%.s=%.d) -o $@ $<
+	$(hide) sed -i -e '/\.note\.ABI-tag/s/progbits/note/' $@
+	$(call transform-d-to-p-args,$(@:%.s=%.d),$(@:%.s=%.P))
+-include $(GEN:%.s=%.P)
+
+
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
+$(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
+$(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_so_cflags)
+$(GEN): $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.s
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_CC) $(PRIVATE_CFLAGS) -o $@ -c $<
+
+
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
+$(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
+$(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_so_cflags)
+$(GEN): $(my_libc_crt_target_crtbegin_so_file)
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_CC) $(PRIVATE_CFLAGS) \
+		-MD -MF $(@:%.o=%.d) -o $@ -c $<
+	$(transform-d-to-p)
+-include $(GEN:%.o=%.P)
+
+
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_so.o
+$(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
+$(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_so_cflags)
+$(GEN): $(LOCAL_PATH)/arch-common/bionic/crtend_so.S
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_CC) $(PRIVATE_CFLAGS) \
+		-MD -MF $(@:%.o=%.d) -o $@ -c $<
+	$(transform-d-to-p)
+-include $(GEN:%.o=%.P)
+
+
+# The following two are installed to device
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_SHARED_LIBRARIES)/crtbegin_so.o
+$(GEN): $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
+	$(hide) mkdir -p $(dir $@) && cp -f $< $@
+ALL_GENERATED_SOURCES += $(GEN)
+
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_SHARED_LIBRARIES)/crtend_so.o
+$(GEN): $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_so.o
+	$(hide) mkdir -p $(dir $@) && cp -f $< $@
+ALL_GENERATED_SOURCES += $(GEN)
+
+
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static1.o
+$(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
+$(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_cflags)
+$(GEN): $(my_libc_crt_target_crtbegin_file)
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_CC) $(PRIVATE_CFLAGS) \
+		-MD -MF $(@:%.o=%.d) -o $@ -c $<
+	$(transform-d-to-p)
+-include $(GEN:%.o=%.P)
+
+
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static.o
+$(GEN): PRIVATE_LD := $($(my_2nd_arch_prefix)TARGET_LD)
+$(GEN): PRIVATE_LDFLAGS := $(my_libc_crt_target_ldflags)
+$(GEN): $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static1.o \
+    $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_LD) $(PRIVATE_LDFLAGS) -r -o $@ $^
+
+
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic1.o
+$(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
+$(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_cflags)
+$(GEN): $(my_libc_crt_target_crtbegin_file)
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_CC) $(PRIVATE_CFLAGS) \
+		-MD -MF $(@:%.o=%.d) -o $@ -c $<
+	$(transform-d-to-p)
+-include $(GEN:%.o=%.P)
+
+
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic.o
+$(GEN): PRIVATE_LD := $($(my_2nd_arch_prefix)TARGET_LD)
+$(GEN): PRIVATE_LDFLAGS := $(my_libc_crt_target_ldflags)
+$(GEN): $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic1.o \
+    $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_LD) $(PRIVATE_LDFLAGS) -r -o $@ $^
+
+
+# We rename crtend.o to crtend_android.o to avoid a
+# name clash between gcc and bionic.
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_android.o
+$(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
+$(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_cflags)
+$(GEN): $(LOCAL_PATH)/arch-common/bionic/crtend.S
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_CC) $(PRIVATE_CFLAGS) \
+		-MD -MF $(@:%.o=%.d) -o $@ -c $<
+	$(transform-d-to-p)
+-include $(GEN:%.o=%.P)
+
+# Clear temp vars
+my_libc_crt_target_ldflags :=
+my_libc_crt_target_so_cflags :=
+my_libc_crt_target_cflags :=
+my_libc_crt_target_crtbegin_so_file :=
+my_libc_crt_target_crtbegin_file :=
+my_arch :=