Use ifunc for ARM32 fortify functions

Test: compile and run bionic unit tests
Test: make PRODUCT-sdk_phone_arm64-sdk
Change-Id: I614f495ad6053d507446a6a896b9199825325188
diff --git a/libc/Android.bp b/libc/Android.bp
index 4c5b0b3..a8a2545 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -691,120 +691,32 @@
 
     arch: {
         arm: {
-            cflags: ["-DNO___MEMCPY_CHK"],
+            cflags: [
+                "-DNO___MEMCPY_CHK",
+                "-DRENAME___STRCAT_CHK",
+                "-DRENAME___STRCPY_CHK",
+            ],
             srcs: [
                 "arch-arm/generic/bionic/__memcpy_chk.S",
+
+                "arch-arm/cortex-a15/bionic/__strcat_chk.S",
+                "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
+
+                "arch-arm/cortex-a7/bionic/__strcat_chk.S",
+                "arch-arm/cortex-a7/bionic/__strcpy_chk.S",
+
+                "arch-arm/cortex-a9/bionic/__strcat_chk.S",
+                "arch-arm/cortex-a9/bionic/__strcpy_chk.S",
+
+                "arch-arm/krait/bionic/__strcat_chk.S",
+                "arch-arm/krait/bionic/__strcpy_chk.S",
+
+                "arch-arm/cortex-a53/bionic/__strcat_chk.S",
+                "arch-arm/cortex-a53/bionic/__strcpy_chk.S",
+
+                "arch-arm/denver/bionic/__strcat_chk.S",
+                "arch-arm/denver/bionic/__strcpy_chk.S",
             ],
-            neon: {
-                cflags: [
-                    "-DNO___STRCAT_CHK",
-                    "-DNO___STRCPY_CHK",
-                ],
-                srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            cortex_a7: {
-                srcs: [
-                    "arch-arm/cortex-a7/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a7/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            cortex_a9: {
-                srcs: [
-                    "arch-arm/cortex-a9/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a9/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            krait: {
-                srcs: [
-                    "arch-arm/krait/bionic/__strcat_chk.S",
-                    "arch-arm/krait/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            cortex_a53: {
-                srcs: [
-                    "arch-arm/cortex-a53/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a53/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            cortex_a55: {
-                srcs: [
-                    "arch-arm/denver/bionic/__strcat_chk.S",
-                    "arch-arm/denver/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            cortex_a73: {
-                srcs: [
-                    "arch-arm/denver/bionic/__strcat_chk.S",
-                    "arch-arm/denver/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            cortex_a75: {
-                srcs: [
-                    "arch-arm/denver/bionic/__strcat_chk.S",
-                    "arch-arm/denver/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            cortex_a76: {
-                srcs: [
-                    "arch-arm/denver/bionic/__strcat_chk.S",
-                    "arch-arm/denver/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            denver: {
-                srcs: [
-                    "arch-arm/denver/bionic/__strcat_chk.S",
-                    "arch-arm/denver/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
-            kryo: {
-                srcs: [
-                    "arch-arm/krait/bionic/__strcat_chk.S",
-                    "arch-arm/krait/bionic/__strcpy_chk.S",
-                ],
-                exclude_srcs: [
-                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
-                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
-                ],
-            },
         },
         arm64: {
             cflags: ["-DNO___MEMCPY_CHK"],
diff --git a/libc/arch-arm/cortex-a15/bionic/__strcat_chk.S b/libc/arch-arm/cortex-a15/bionic/__strcat_chk.S
index ca29715..69e405f 100644
--- a/libc/arch-arm/cortex-a15/bionic/__strcat_chk.S
+++ b/libc/arch-arm/cortex-a15/bionic/__strcat_chk.S
@@ -40,7 +40,7 @@
 // Get the length of src string, then get the source of the dst string.
 // Check that the two lengths together don't exceed the threshold, then
 // do a memcpy of the data.
-ENTRY(__strcat_chk)
+ENTRY(__strcat_chk_a15)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -200,4 +200,4 @@
     .cfi_adjust_cfa_offset 8
     .cfi_rel_offset r4, 0
     .cfi_rel_offset r5, 4
-END(__strcat_chk)
+END(__strcat_chk_a15)
diff --git a/libc/arch-arm/cortex-a15/bionic/__strcpy_chk.S b/libc/arch-arm/cortex-a15/bionic/__strcpy_chk.S
index 2679c02..ddde30e 100644
--- a/libc/arch-arm/cortex-a15/bionic/__strcpy_chk.S
+++ b/libc/arch-arm/cortex-a15/bionic/__strcpy_chk.S
@@ -39,7 +39,7 @@
 
 // Get the length of the source string first, then do a memcpy of the data
 // instead of a strcpy.
-ENTRY(__strcpy_chk)
+ENTRY(__strcpy_chk_a15)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -162,4 +162,4 @@
 
 #include "memcpy_base.S"
 
-END(__strcpy_chk)
+END(__strcpy_chk_a15)
diff --git a/libc/arch-arm/cortex-a53/bionic/__strcat_chk.S b/libc/arch-arm/cortex-a53/bionic/__strcat_chk.S
index ca29715..72be243 100644
--- a/libc/arch-arm/cortex-a53/bionic/__strcat_chk.S
+++ b/libc/arch-arm/cortex-a53/bionic/__strcat_chk.S
@@ -40,7 +40,7 @@
 // Get the length of src string, then get the source of the dst string.
 // Check that the two lengths together don't exceed the threshold, then
 // do a memcpy of the data.
-ENTRY(__strcat_chk)
+ENTRY(__strcat_chk_a53)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -200,4 +200,4 @@
     .cfi_adjust_cfa_offset 8
     .cfi_rel_offset r4, 0
     .cfi_rel_offset r5, 4
-END(__strcat_chk)
+END(__strcat_chk_a53)
diff --git a/libc/arch-arm/cortex-a53/bionic/__strcpy_chk.S b/libc/arch-arm/cortex-a53/bionic/__strcpy_chk.S
index 2679c02..4922884 100644
--- a/libc/arch-arm/cortex-a53/bionic/__strcpy_chk.S
+++ b/libc/arch-arm/cortex-a53/bionic/__strcpy_chk.S
@@ -39,7 +39,7 @@
 
 // Get the length of the source string first, then do a memcpy of the data
 // instead of a strcpy.
-ENTRY(__strcpy_chk)
+ENTRY(__strcpy_chk_a53)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -162,4 +162,4 @@
 
 #include "memcpy_base.S"
 
-END(__strcpy_chk)
+END(__strcpy_chk_a53)
diff --git a/libc/arch-arm/cortex-a7/bionic/__strcat_chk.S b/libc/arch-arm/cortex-a7/bionic/__strcat_chk.S
index ca29715..36bda6b 100644
--- a/libc/arch-arm/cortex-a7/bionic/__strcat_chk.S
+++ b/libc/arch-arm/cortex-a7/bionic/__strcat_chk.S
@@ -40,7 +40,7 @@
 // Get the length of src string, then get the source of the dst string.
 // Check that the two lengths together don't exceed the threshold, then
 // do a memcpy of the data.
-ENTRY(__strcat_chk)
+ENTRY(__strcat_chk_a7)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -200,4 +200,4 @@
     .cfi_adjust_cfa_offset 8
     .cfi_rel_offset r4, 0
     .cfi_rel_offset r5, 4
-END(__strcat_chk)
+END(__strcat_chk_a7)
diff --git a/libc/arch-arm/cortex-a7/bionic/__strcpy_chk.S b/libc/arch-arm/cortex-a7/bionic/__strcpy_chk.S
index 2679c02..edbabc7 100644
--- a/libc/arch-arm/cortex-a7/bionic/__strcpy_chk.S
+++ b/libc/arch-arm/cortex-a7/bionic/__strcpy_chk.S
@@ -39,7 +39,7 @@
 
 // Get the length of the source string first, then do a memcpy of the data
 // instead of a strcpy.
-ENTRY(__strcpy_chk)
+ENTRY(__strcpy_chk_a7)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -162,4 +162,4 @@
 
 #include "memcpy_base.S"
 
-END(__strcpy_chk)
+END(__strcpy_chk_a7)
diff --git a/libc/arch-arm/cortex-a9/bionic/__strcat_chk.S b/libc/arch-arm/cortex-a9/bionic/__strcat_chk.S
index 776c782..d20283e 100644
--- a/libc/arch-arm/cortex-a9/bionic/__strcat_chk.S
+++ b/libc/arch-arm/cortex-a9/bionic/__strcat_chk.S
@@ -33,10 +33,14 @@
     .thumb
     .thumb_func
 
+    // To avoid warning about deprecated instructions, add an explicit
+    // arch. The code generated is exactly the same.
+    .arch armv7-a
+
 // Get the length of src string, then get the source of the dst string.
 // Check that the two lengths together don't exceed the threshold, then
 // do a memcpy of the data.
-ENTRY(__strcat_chk)
+ENTRY(__strcat_chk_a9)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -191,8 +195,8 @@
     pop     {r4, r5}
 
     // Fall through into the memcpy_base function.
-END(__strcat_chk)
+END(__strcat_chk_a9)
 
-#define MEMCPY_BASE         __strcat_chk_memcpy_base
-#define MEMCPY_BASE_ALIGNED __strcat_chk_memcpy_base_aligned
+#define MEMCPY_BASE         __strcat_chk_a9_memcpy_base
+#define MEMCPY_BASE_ALIGNED __strcat_chk_a9_memcpy_base_aligned
 #include "memcpy_base.S"
diff --git a/libc/arch-arm/cortex-a9/bionic/__strcpy_chk.S b/libc/arch-arm/cortex-a9/bionic/__strcpy_chk.S
index 1d5e70b..1f0a774 100644
--- a/libc/arch-arm/cortex-a9/bionic/__strcpy_chk.S
+++ b/libc/arch-arm/cortex-a9/bionic/__strcpy_chk.S
@@ -33,9 +33,13 @@
     .thumb
     .thumb_func
 
+    // To avoid warning about deprecated instructions, add an explicit
+    // arch. The code generated is exactly the same.
+    .arch armv7-a
+
 // Get the length of the source string first, then do a memcpy of the data
 // instead of a strcpy.
-ENTRY(__strcpy_chk)
+ENTRY(__strcpy_chk_a9)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -159,8 +163,8 @@
     bhi     __strcpy_chk_fail
 
     // Fall through into the memcpy_base function.
-END(__strcpy_chk)
+END(__strcpy_chk_a9)
 
-#define MEMCPY_BASE         __strcpy_chk_memcpy_base
-#define MEMCPY_BASE_ALIGNED __strcpy_chk_memcpy_base_aligned
+#define MEMCPY_BASE         __strcpy_chk_a9_memcpy_base
+#define MEMCPY_BASE_ALIGNED __strcpy_chk_a9_memcpy_base_aligned
 #include "memcpy_base.S"
diff --git a/libc/arch-arm/denver/bionic/__strcat_chk.S b/libc/arch-arm/denver/bionic/__strcat_chk.S
index d4f651c..1899908 100644
--- a/libc/arch-arm/denver/bionic/__strcat_chk.S
+++ b/libc/arch-arm/denver/bionic/__strcat_chk.S
@@ -40,7 +40,7 @@
 // Get the length of src string, then get the source of the dst string.
 // Check that the two lengths together don't exceed the threshold, then
 // do a memcpy of the data.
-ENTRY(__strcat_chk)
+ENTRY(__strcat_chk_denver)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -190,9 +190,9 @@
     mov     r2, r4
     add     r0, r0, r3
     pop     {r4, r5}
-END(__strcat_chk)
+END(__strcat_chk_denver)
 
-#define MEMCPY_BASE         __strcat_chk_memcpy_base
-#define MEMCPY_BASE_ALIGNED __strcat_chk_memcpy_base_aligned
+#define MEMCPY_BASE         __strcat_chk_denver_memcpy_base
+#define MEMCPY_BASE_ALIGNED __strcat_chk_denver_memcpy_base_aligned
 
 #include "memcpy_base.S"
diff --git a/libc/arch-arm/denver/bionic/__strcpy_chk.S b/libc/arch-arm/denver/bionic/__strcpy_chk.S
index 9295a00..9910c76 100644
--- a/libc/arch-arm/denver/bionic/__strcpy_chk.S
+++ b/libc/arch-arm/denver/bionic/__strcpy_chk.S
@@ -39,7 +39,7 @@
 
 // Get the length of the source string first, then do a memcpy of the data
 // instead of a strcpy.
-ENTRY(__strcpy_chk)
+ENTRY(__strcpy_chk_denver)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_def_cfa_offset 8
@@ -161,8 +161,8 @@
     bhi     __strcpy_chk_fail
 
     // Fall through into the memcpy_base function.
-END(__strcpy_chk)
+END(__strcpy_chk_denver)
 
-#define MEMCPY_BASE         __strcpy_chk_memcpy_base
-#define MEMCPY_BASE_ALIGNED __strcpy_chk_memcpy_base_aligned
+#define MEMCPY_BASE         __strcpy_chk_denver_memcpy_base
+#define MEMCPY_BASE_ALIGNED __strcpy_chk_denver_memcpy_base_aligned
 #include "memcpy_base.S"
diff --git a/libc/arch-arm/dynamic_function_dispatch.cpp b/libc/arch-arm/dynamic_function_dispatch.cpp
index 17f0087..90eff56 100644
--- a/libc/arch-arm/dynamic_function_dispatch.cpp
+++ b/libc/arch-arm/dynamic_function_dispatch.cpp
@@ -220,6 +220,26 @@
     }
 }
 
+typedef char* __strcpy_chk_func(char* dst, const char* src, size_t dst_len);
+DEFINE_IFUNC(__strcpy_chk) {
+    switch(get_cpu_variant()) {
+        case kCortexA7:
+            RETURN_FUNC(__strcpy_chk_func, __strcpy_chk_a7);
+        case kCortexA9:
+            RETURN_FUNC(__strcpy_chk_func, __strcpy_chk_a9);
+        case kKrait:
+        case kKryo:
+            RETURN_FUNC(__strcpy_chk_func, __strcpy_chk_krait);
+        case kCortexA53:
+            RETURN_FUNC(__strcpy_chk_func, __strcpy_chk_a53);
+        case kCortexA55:
+        case kDenver:
+            RETURN_FUNC(__strcpy_chk_func, __strcpy_chk_denver);
+        default:
+            RETURN_FUNC(__strcpy_chk_func, __strcpy_chk_a15);
+    }
+}
+
 typedef char* stpcpy_func(char* __dst, const char* __src);
 DEFINE_IFUNC(stpcpy) {
     switch(get_cpu_variant()) {
@@ -240,6 +260,26 @@
     }
 }
 
+typedef char* __strcat_chk_func(char* dst, const char* src, size_t dst_buf_size);
+DEFINE_IFUNC(__strcat_chk) {
+    switch(get_cpu_variant()) {
+        case kCortexA7:
+            RETURN_FUNC(__strcat_chk_func, __strcat_chk_a7);
+        case kCortexA9:
+            RETURN_FUNC(__strcat_chk_func, __strcat_chk_a9);
+        case kKrait:
+        case kKryo:
+            RETURN_FUNC(__strcat_chk_func, __strcat_chk_krait);
+        case kCortexA53:
+            RETURN_FUNC(__strcat_chk_func, __strcat_chk_a53);
+        case kCortexA55:
+        case kDenver:
+            RETURN_FUNC(__strcat_chk_func, __strcat_chk_denver);
+        default:
+            RETURN_FUNC(__strcat_chk_func, __strcat_chk_a15);
+    }
+}
+
 typedef int strcmp_func(const char* __lhs, const char* __rhs);
 DEFINE_IFUNC(strcmp) {
     switch(get_cpu_variant()) {
diff --git a/libc/arch-arm/krait/bionic/__strcat_chk.S b/libc/arch-arm/krait/bionic/__strcat_chk.S
index 32fa82d..9d4bffb 100644
--- a/libc/arch-arm/krait/bionic/__strcat_chk.S
+++ b/libc/arch-arm/krait/bionic/__strcat_chk.S
@@ -40,7 +40,7 @@
 // Get the length of src string, then get the source of the dst string.
 // Check that the two lengths together don't exceed the threshold, then
 // do a memcpy of the data.
-ENTRY(__strcat_chk)
+ENTRY(__strcat_chk_krait)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_adjust_cfa_offset 8
@@ -200,4 +200,4 @@
     .cfi_adjust_cfa_offset 8
     .cfi_rel_offset r4, 0
     .cfi_rel_offset r5, 4
-END(__strcat_chk)
+END(__strcat_chk_krait)
diff --git a/libc/arch-arm/krait/bionic/__strcpy_chk.S b/libc/arch-arm/krait/bionic/__strcpy_chk.S
index ca4cf7f..969e112 100644
--- a/libc/arch-arm/krait/bionic/__strcpy_chk.S
+++ b/libc/arch-arm/krait/bionic/__strcpy_chk.S
@@ -39,7 +39,7 @@
 
 // Get the length of the source string first, then do a memcpy of the data
 // instead of a strcpy.
-ENTRY(__strcpy_chk)
+ENTRY(__strcpy_chk_krait)
     pld     [r0, #0]
     push    {r0, lr}
     .cfi_adjust_cfa_offset 8
@@ -162,4 +162,4 @@
 
 #include "memcpy_base.S"
 
-END(__strcpy_chk)
+END(__strcpy_chk_krait)
diff --git a/libc/arch-arm/static_function_dispatch.S b/libc/arch-arm/static_function_dispatch.S
index f3dd741..a8235c2 100644
--- a/libc/arch-arm/static_function_dispatch.S
+++ b/libc/arch-arm/static_function_dispatch.S
@@ -38,7 +38,9 @@
 FUNCTION_DELEGATE(memset, memset_generic)
 FUNCTION_DELEGATE(__memset_chk, __memset_chk_generic)
 FUNCTION_DELEGATE(strcpy, strcpy_generic)
+FUNCTION_DELEGATE(__strcpy_chk, __strcpy_chk_generic)
 FUNCTION_DELEGATE(stpcpy, stpcpy_generic)
 FUNCTION_DELEGATE(strcat, strcat_generic)
+FUNCTION_DELEGATE(__strcat_chk, __strcat_chk_generic)
 FUNCTION_DELEGATE(strcmp, strcmp_generic)
 FUNCTION_DELEGATE(strlen, strlen_generic)
diff --git a/libc/bionic/fortify.cpp b/libc/bionic/fortify.cpp
index 4b94573..20e265e 100644
--- a/libc/bionic/fortify.cpp
+++ b/libc/bionic/fortify.cpp
@@ -454,9 +454,14 @@
   return write(fd, buf, count);
 }
 
-#if !defined(NO___STRCAT_CHK)
+#if defined(RENAME___STRCAT_CHK)
+#define __STRCAT_CHK __strcat_chk_generic
+#else
+#define __STRCAT_CHK __strcat_chk
+#endif // RENAME___STRCAT_CHK
+
 // Runtime implementation of __builtin____strcat_chk (used directly by compiler, not in headers).
-extern "C" char* __strcat_chk(char* dst, const char* src, size_t dst_buf_size) {
+extern "C" char* __STRCAT_CHK(char* dst, const char* src, size_t dst_buf_size) {
   char* save = dst;
   size_t dst_len = __strlen_chk(dst, dst_buf_size);
 
@@ -472,17 +477,20 @@
 
   return save;
 }
-#endif // NO___STRCAT_CHK
 
-#if !defined(NO___STRCPY_CHK)
+#if defined(RENAME___STRCPY_CHK)
+#define __STRCPY_CHK __strcpy_chk_generic
+#else
+#define __STRCPY_CHK __strcpy_chk
+#endif // RENAME___STRCPY_CHK
+
 // Runtime implementation of __builtin____strcpy_chk (used directly by compiler, not in headers).
-extern "C" char* __strcpy_chk(char* dst, const char* src, size_t dst_len) {
+extern "C" char* __STRCPY_CHK(char* dst, const char* src, size_t dst_len) {
   // TODO: optimize so we don't scan src twice.
   size_t src_len = strlen(src) + 1;
   __check_buffer_access("strcpy", "write into", src_len, dst_len);
   return strcpy(dst, src);
 }
-#endif // NO___STRCPY_CHK
 
 #if !defined(NO___MEMCPY_CHK)
 // Runtime implementation of __memcpy_chk (used directly by compiler, not in headers).