merge in lmp-mr1-release history after reset to 0cc59dd303205de7110e298e9b90b1c3b98f4711
diff --git a/libc/Android.mk b/libc/Android.mk
index 92ead26..61bdf7e 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -692,6 +692,7 @@
 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_freebsd_src_files))
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -760,6 +761,7 @@
 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_openbsd_src_files))
 include $(BUILD_STATIC_LIBRARY)
 
 
diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk
index 70cc8eb..b5ed7f0 100644
--- a/libc/arch-arm/arm.mk
+++ b/libc/arch-arm/arm.mk
@@ -1,22 +1,35 @@
-# arm specific configs
+# 32-bit arm.
 
-# These are used by the 32-bit targets, but not the 64-bit ones.
-libc_common_src_files_arm := \
+#
+# Various kinds of LP32 cruft.
+#
+
+libc_bionic_src_files_arm += \
+    bionic/mmap.cpp \
+
+libc_common_src_files_arm += \
     bionic/legacy_32_bit_support.cpp \
     bionic/ndk_cruft.cpp \
     bionic/time64.c \
+
+libc_netbsd_src_files_arm += \
+    upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
+
+libc_openbsd_src_files_arm += \
     upstream-openbsd/lib/libc/stdio/putw.c \
 
-# These are shared by all the 32-bit targets, but not the 64-bit ones.
-libc_bionic_src_files_arm := \
-    bionic/mmap.cpp
+#
+# Default implementations of functions that are commonly optimized.
+#
 
-libc_common_src_files_arm += \
+libc_bionic_src_files_arm += \
     bionic/memchr.c \
     bionic/memrchr.c \
     bionic/strchr.cpp \
     bionic/strnlen.c \
     bionic/strrchr.cpp \
+
+libc_freebsd_src_files_arm += \
     upstream-freebsd/lib/libc/string/wcscat.c \
     upstream-freebsd/lib/libc/string/wcschr.c \
     upstream-freebsd/lib/libc/string/wcscmp.c \
@@ -25,6 +38,8 @@
     upstream-freebsd/lib/libc/string/wcsrchr.c \
     upstream-freebsd/lib/libc/string/wmemcmp.c \
     upstream-freebsd/lib/libc/string/wmemmove.c \
+
+libc_openbsd_src_files_arm += \
     upstream-openbsd/lib/libc/string/bcopy.c \
     upstream-openbsd/lib/libc/string/stpcpy.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
@@ -34,20 +49,10 @@
     upstream-openbsd/lib/libc/string/strncmp.c \
     upstream-openbsd/lib/libc/string/strncpy.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 \
+# Inherently architecture-specific code.
+#
 
-libc_common_cflags_arm := -DSOFTFLOAT
-
-##########################################
-### CPU specific source files
 libc_bionic_src_files_arm += \
     arch-arm/bionic/abort_arm.S \
     arch-arm/bionic/atomics_arm.c \
@@ -55,6 +60,7 @@
     arch-arm/bionic/_exit_with_stack_teardown.S \
     arch-arm/bionic/libgcc_compat.c \
     arch-arm/bionic/memcmp.S \
+    arch-arm/bionic/__restore.S \
     arch-arm/bionic/_setjmp.S \
     arch-arm/bionic/setjmp.S \
     arch-arm/bionic/sigsetjmp.S \
@@ -63,9 +69,6 @@
 libc_arch_static_src_files_arm := arch-arm/bionic/exidx_static.c
 libc_arch_dynamic_src_files_arm := arch-arm/bionic/exidx_dynamic.c
 
-libc_netbsd_src_files_arm := \
-    upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
-
 ## 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)
diff --git a/libc/arch-arm/bionic/__restore.S b/libc/arch-arm/bionic/__restore.S
new file mode 100644
index 0000000..9898125
--- /dev/null
+++ b/libc/arch-arm/bionic/__restore.S
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <private/bionic_asm.h>
+
+// gdb is smart enough to unwind through signal frames with just the regular
+// CFI information but libgcc and libunwind both need extra help. We do this
+// by using .fnstart/.fnend and inserting a nop before both __restore and
+// __restore_rt (but covered by the .fnstart/.fnend) so that although they're
+// not inside the functions from objdump's point of view, an unwinder that
+// blindly looks at the previous instruction (but is then smart enough to check
+// the DWARF information to find out where it landed) gets the right answer.
+
+// We need to place .fnstart ourselves (but we may as well keep the free .fnend).
+#undef __bionic_asm_custom_entry
+#define __bionic_asm_custom_entry(f)
+
+  .fnstart
+  .save {r0-r15}
+  .pad #32
+  nop
+ENTRY_PRIVATE(__restore)
+  // This function must have exactly this instruction sequence.
+  mov r7, #__NR_sigreturn
+  swi #0
+END(__restore)
+
+  .fnstart
+  .save {r0-r15}
+  .pad #160
+  nop
+ENTRY_PRIVATE(__restore_rt)
+  // This function must have exactly this instruction sequence.
+  mov r7, #__NR_rt_sigreturn
+  swi #0
+END(__restore_rt)
diff --git a/libc/arch-arm64/arm64.mk b/libc/arch-arm64/arm64.mk
index 6c4f6a6..bb6ca63 100644
--- a/libc/arch-arm64/arm64.mk
+++ b/libc/arch-arm64/arm64.mk
@@ -1,8 +1,17 @@
-# arm64 specific configs
+# 64-bit arm.
 
-libc_common_src_files_arm64 := \
+#
+# Default implementations of functions that are commonly optimized.
+#
+
+libc_bionic_src_files_arm64 += \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
     bionic/memrchr.c \
     bionic/strrchr.cpp \
+
+libc_freebsd_src_files_arm64 += \
     upstream-freebsd/lib/libc/string/wcscat.c \
     upstream-freebsd/lib/libc/string/wcschr.c \
     upstream-freebsd/lib/libc/string/wcscmp.c \
@@ -10,6 +19,8 @@
     upstream-freebsd/lib/libc/string/wcslen.c \
     upstream-freebsd/lib/libc/string/wcsrchr.c \
     upstream-freebsd/lib/libc/string/wmemcmp.c \
+
+libc_openbsd_src_files_arm64 += \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strcat.c \
     upstream-openbsd/lib/libc/string/strlcat.c \
@@ -17,19 +28,13 @@
     upstream-openbsd/lib/libc/string/strncat.c \
     upstream-openbsd/lib/libc/string/strncpy.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 \
+#
+# Inherently architecture-specific code.
+#
 
-##########################################
-### CPU specific source files
-libc_bionic_src_files_arm64 := \
+libc_bionic_src_files_arm64 += \
     arch-arm64/bionic/__bionic_clone.S \
     arch-arm64/bionic/_exit_with_stack_teardown.S \
-    arch-arm64/bionic/__rt_sigreturn.S \
     arch-arm64/bionic/_setjmp.S \
     arch-arm64/bionic/setjmp.S \
     arch-arm64/bionic/__set_tls.c \
@@ -53,7 +58,7 @@
 endif
 cpu_variant_mk := $(LOCAL_PATH)/arch-arm64/$(TARGET_CPU_VARIANT)/$(TARGET_CPU_VARIANT).mk
 ifeq ($(wildcard $(cpu_variant_mk)),)
-$(error "TARGET_CPU_VARIANT not set or set to an unknown value. Possible values are generic, generic-neon, denver64. Use generic for devices that do not have a CPU similar to any of the supported cpu variants.")
+$(error "TARGET_CPU_VARIANT not set or set to an unknown value. Possible values are generic, denver64. 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_variank_mk)
diff --git a/libc/arch-arm64/bionic/__rt_sigreturn.S b/libc/arch-arm64/bionic/__rt_sigreturn.S
deleted file mode 100644
index 8fb6f0c..0000000
--- a/libc/arch-arm64/bionic/__rt_sigreturn.S
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <private/bionic_asm.h>
-
-ENTRY_PRIVATE(__rt_sigreturn)
-  mov     x8, __NR_rt_sigreturn
-  svc     #0
-END(__rt_sigreturn)
diff --git a/libc/arch-arm64/denver64/bionic/memcpy.S b/libc/arch-arm64/denver64/bionic/memcpy.S
index 700f0d0..85129fe 100644
--- a/libc/arch-arm64/denver64/bionic/memcpy.S
+++ b/libc/arch-arm64/denver64/bionic/memcpy.S
@@ -1,205 +1,63 @@
-/* Copyright (c) 2012, Linaro Limited
-   All rights reserved.
-   Copyright (c) 2014, NVIDIA Corporation.  All rights reserved.
-
-   Redistribution and use in source and binary forms, with or without
-   modification, are permitted provided that the following conditions are met:
-       * Redistributions of source code must retain the above copyright
-         notice, this list of conditions and the following disclaimer.
-       * Redistributions in binary form must reproduce the above copyright
-         notice, this list of conditions and the following disclaimer in the
-         documentation and/or other materials provided with the distribution.
-       * Neither the name of the Linaro nor the
-         names of its contributors may be used to endorse or promote products
-         derived from this software without specific prior written permission.
-
-   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
-/* Assumptions:
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
  *
- * denver, ARMv8-a, AArch64
- * Unaligned accesses
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
  *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
+// Prototype: void *memcpy (void *dst, const void *src, size_t count).
+
 #include <private/bionic_asm.h>
+#include <private/libc_events.h>
 
-#define dstin	x0
-#define src	x1
-#define count	x2
-#define tmp1	x3
-#define tmp1w	w3
-#define tmp2	x4
-#define tmp2w	w4
-#define tmp3	x5
-#define tmp3w	w5
-#define dst	x6
+ENTRY(__memcpy_chk)
+  cmp   x2, x3
+  b.hi  __memcpy_chk_fail
 
-#define A_l	x7
-#define A_h	x8
-#define B_l	x9
-#define B_h	x10
-#define C_l	x11
-#define C_h	x12
-#define D_l	x13
-#define D_h	x14
-
-#define QA_l	q0
-#define QA_h	q1
-#define QB_l	q2
-#define QB_h	q3
+  // Fall through to memcpy...
+END(__memcpy_chk)
 
 ENTRY(memcpy)
-
-	mov	dst, dstin
-	cmp	count, #64
-	b.ge	.Lcpy_not_short
-	cmp	count, #15
-	b.le	.Ltail15tiny
-
-	/* Deal with small copies quickly by dropping straight into the
-	 * exit block.  */
-.Ltail63:
-	/* Copy up to 48 bytes of data.  At this point we only need the
-	 * bottom 6 bits of count to be accurate.  */
-	ands	tmp1, count, #0x30
-	b.eq	.Ltail15
-	add	dst, dst, tmp1
-	add	src, src, tmp1
-	cmp	tmp1w, #0x20
-	b.eq	1f
-	b.lt	2f
-	ldp	A_l, A_h, [src, #-48]
-	stp	A_l, A_h, [dst, #-48]
-1:
-	ldp	A_l, A_h, [src, #-32]
-	stp	A_l, A_h, [dst, #-32]
-2:
-	ldp	A_l, A_h, [src, #-16]
-	stp	A_l, A_h, [dst, #-16]
-
-.Ltail15:
-	ands	count, count, #15
-	beq	1f
-	add	src, src, count
-	ldp	A_l, A_h, [src, #-16]
-	add	dst, dst, count
-	stp	A_l, A_h, [dst, #-16]
-1:
-	ret
-
-.Ltail15tiny:
-	/* Copy up to 15 bytes of data.  Does not assume additional data
-	   being copied.  */
-	tbz	count, #3, 1f
-	ldr	tmp1, [src], #8
-	str	tmp1, [dst], #8
-1:
-	tbz	count, #2, 1f
-	ldr	tmp1w, [src], #4
-	str	tmp1w, [dst], #4
-1:
-	tbz	count, #1, 1f
-	ldrh	tmp1w, [src], #2
-	strh	tmp1w, [dst], #2
-1:
-	tbz	count, #0, 1f
-	ldrb	tmp1w, [src]
-	strb	tmp1w, [dst]
-1:
-	ret
-
-.Lcpy_not_short:
-	/* We don't much care about the alignment of DST, but we want SRC
-	 * to be 128-bit (16 byte) aligned so that we don't cross cache line
-	 * boundaries on both loads and stores.  */
-	neg	tmp2, src
-	ands	tmp2, tmp2, #15		/* Bytes to reach alignment.  */
-	b.eq	2f
-	sub	count, count, tmp2
-	/* Copy more data than needed; it's faster than jumping
-	 * around copying sub-Quadword quantities.  We know that
-	 * it can't overrun.  */
-	ldp	A_l, A_h, [src]
-	add	src, src, tmp2
-	stp	A_l, A_h, [dst]
-	add	dst, dst, tmp2
-	/* There may be less than 63 bytes to go now.  */
-	cmp	count, #63
-	b.le	.Ltail63
-2:
-	subs	count, count, #128
-	b.ge	.Lcpy_body_large
-	/* Less than 128 bytes to copy, so handle 64 here and then jump
-	 * to the tail.  */
-	ldp	QA_l, QA_h, [src]
-	ldp	QB_l, QB_h, [src, #32]
-	stp	QA_l, QA_h, [dst]
-	stp	QB_l, QB_h, [dst, #32]
-	tst	count, #0x3f
-	add	src, src, #64
-	add	dst, dst, #64
-	b.ne	.Ltail63
-	ret
-
-	/* Critical loop.  Start at a new cache line boundary.  Assuming
-	 * 64 bytes per line this ensures the entire loop is in one line.  */
-	.p2align 6
-.Lcpy_body_large:
-	cmp	count, 65536
-	bhi	.Lcpy_body_huge
-	/* There are at least 128 bytes to copy.  */
-	ldp	QA_l, QA_h, [src, #0]
-	sub	dst, dst, #32		/* Pre-bias.  */
-	ldp	QB_l, QB_h, [src, #32]!	/* src += 64 - Pre-bias.  */
-1:
-	stp	QA_l, QA_h, [dst, #32]
-	ldp	QA_l, QA_h, [src, #32]
-	stp	QB_l, QB_h, [dst, #64]!
-	ldp	QB_l, QB_h, [src, #64]!
-
-	subs	count, count, #64
-	b.ge	1b
-
-	stp	QA_l, QA_h, [dst, #32]
-	stp	QB_l, QB_h, [dst, #64]
-	add	src, src, #32
-	add	dst, dst, #64 + 32
-	tst	count, #0x3f
-	b.ne	.Ltail63
-	ret
-.Lcpy_body_huge:
-	/* There are at least 128 bytes to copy.  */
-	ldp	QA_l, QA_h, [src, #0]
-	sub	dst, dst, #32		/* Pre-bias.  */
-	ldp	QB_l, QB_h, [src, #32]!
-1:
-	stnp	QA_l, QA_h, [dst, #32]
-	stnp	QB_l, QB_h, [dst, #64]
-	ldp	QA_l, QA_h, [src, #32]
-	ldp	QB_l, QB_h, [src, #64]!
-	add	dst, dst, #64
-
-	subs	count, count, #64
-	b.ge	1b
-
-	stnp	QA_l, QA_h, [dst, #32]
-	stnp	QB_l, QB_h, [dst, #64]
-	add	src, src, #32
-	add	dst, dst, #64 + 32
-	tst	count, #0x3f
-	b.ne	.Ltail63
-	ret
-
+  #include "memcpy_base.S"
 END(memcpy)
+
+ENTRY_PRIVATE(__memcpy_chk_fail)
+  // Preserve for accurate backtrace.
+  stp  x29, x30, [sp, -16]!
+  .cfi_def_cfa_offset 16
+  .cfi_rel_offset x29, 0
+  .cfi_rel_offset x30, 8
+
+  adrp  x0, error_string
+  add   x0, x0, :lo12:error_string
+  ldr   x1, error_code
+  bl    __fortify_chk_fail
+error_code:
+  .word   BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW
+END(__memcpy_chk_fail)
+
+  .data
+  .align 2
+error_string:
+  .string "memcpy: prevented write past end of buffer"
diff --git a/libc/arch-arm64/generic-neon/bionic/memcpy.S b/libc/arch-arm64/denver64/bionic/memcpy_base.S
similarity index 79%
copy from libc/arch-arm64/generic-neon/bionic/memcpy.S
copy to libc/arch-arm64/denver64/bionic/memcpy_base.S
index 320f748..3d7e9dd 100644
--- a/libc/arch-arm64/generic-neon/bionic/memcpy.S
+++ b/libc/arch-arm64/denver64/bionic/memcpy_base.S
@@ -1,5 +1,6 @@
 /* Copyright (c) 2012, Linaro Limited
    All rights reserved.
+   Copyright (c) 2014, NVIDIA Corporation.  All rights reserved.
 
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
@@ -27,13 +28,11 @@
 
 /* Assumptions:
  *
- * ARMv8-a, AArch64
+ * denver, ARMv8-a, AArch64
  * Unaligned accesses
  *
  */
 
-#include <private/bionic_asm.h>
-
 #define dstin	x0
 #define src	x1
 #define count	x2
@@ -54,12 +53,10 @@
 #define D_l	x13
 #define D_h	x14
 
-#define QA_l q0
-#define QA_h q1
-#define QB_l q2
-#define QB_h q3
-
-ENTRY(memcpy)
+#define QA_l	q0
+#define QA_h	q1
+#define QB_l	q2
+#define QB_h	q3
 
 	mov	dst, dstin
 	cmp	count, #64
@@ -142,10 +139,10 @@
 	b.ge	.Lcpy_body_large
 	/* Less than 128 bytes to copy, so handle 64 here and then jump
 	 * to the tail.  */
-	ldp QA_l, QA_h, [src]
-	ldp QB_l, QB_h, [src, #32]
-	stp QA_l, QA_h, [dst]
-	stp QB_l, QB_h, [dst, #32]
+	ldp	QA_l, QA_h, [src]
+	ldp	QB_l, QB_h, [src, #32]
+	stp	QA_l, QA_h, [dst]
+	stp	QB_l, QB_h, [dst, #32]
 	tst	count, #0x3f
 	add	src, src, #64
 	add	dst, dst, #64
@@ -156,24 +153,47 @@
 	 * 64 bytes per line this ensures the entire loop is in one line.  */
 	.p2align 6
 .Lcpy_body_large:
+	cmp	count, 65536
+	bhi	.Lcpy_body_huge
 	/* There are at least 128 bytes to copy.  */
-	ldp QA_l, QA_h, [src, #0]
+	ldp	QA_l, QA_h, [src, #0]
 	sub	dst, dst, #32		/* Pre-bias.  */
-	ldp QB_l, QB_h, [src, #32]!	/* src += 64 - Pre-bias.  */
+	ldp	QB_l, QB_h, [src, #32]!	/* src += 64 - Pre-bias.  */
 1:
-	stp QA_l, QA_h, [dst, #32]
-	ldp QA_l, QA_h, [src, #32]
-	stp QB_l, QB_h, [dst, #64]!
-	ldp QB_l, QB_h, [src, #64]!
+	stp	QA_l, QA_h, [dst, #32]
+	ldp	QA_l, QA_h, [src, #32]
+	stp	QB_l, QB_h, [dst, #64]!
+	ldp	QB_l, QB_h, [src, #64]!
 
 	subs	count, count, #64
 	b.ge	1b
 
-	stp QA_l, QA_h, [dst, #32]
-	stp QB_l, QB_h, [dst, #64]
+	stp	QA_l, QA_h, [dst, #32]
+	stp	QB_l, QB_h, [dst, #64]
 	add	src, src, #32
 	add	dst, dst, #64 + 32
 	tst	count, #0x3f
 	b.ne	.Ltail63
 	ret
-END(memcpy)
+.Lcpy_body_huge:
+	/* There are at least 128 bytes to copy.  */
+	ldp	QA_l, QA_h, [src, #0]
+	sub	dst, dst, #32		/* Pre-bias.  */
+	ldp	QB_l, QB_h, [src, #32]!
+1:
+	stnp	QA_l, QA_h, [dst, #32]
+	stnp	QB_l, QB_h, [dst, #64]
+	ldp	QA_l, QA_h, [src, #32]
+	ldp	QB_l, QB_h, [src, #64]!
+	add	dst, dst, #64
+
+	subs	count, count, #64
+	b.ge	1b
+
+	stnp	QA_l, QA_h, [dst, #32]
+	stnp	QB_l, QB_h, [dst, #64]
+	add	src, src, #32
+	add	dst, dst, #64 + 32
+	tst	count, #0x3f
+	b.ne	.Ltail63
+	ret
diff --git a/libc/arch-arm64/generic-neon/generic-neon.mk b/libc/arch-arm64/generic-neon/generic-neon.mk
deleted file mode 100644
index 77e3861..0000000
--- a/libc/arch-arm64/generic-neon/generic-neon.mk
+++ /dev/null
@@ -1,13 +0,0 @@
-libc_bionic_src_files_arm64 += \
-    arch-arm64/generic/bionic/memchr.S \
-    arch-arm64/generic/bionic/memcmp.S \
-    arch-arm64/generic/bionic/memmove.S \
-    arch-arm64/generic/bionic/memset.S \
-    arch-arm64/generic/bionic/stpcpy.S \
-    arch-arm64/generic/bionic/strchr.S \
-    arch-arm64/generic/bionic/strcmp.S \
-    arch-arm64/generic/bionic/strcpy.S \
-    arch-arm64/generic/bionic/strlen.S \
-    arch-arm64/generic/bionic/strncmp.S \
-    arch-arm64/generic/bionic/strnlen.S \
-    arch-arm64/generic-neon/bionic/memcpy.S \
diff --git a/libc/arch-arm64/generic/bionic/memcpy.S b/libc/arch-arm64/generic/bionic/memcpy.S
index e1b1a72..85129fe 100644
--- a/libc/arch-arm64/generic/bionic/memcpy.S
+++ b/libc/arch-arm64/generic/bionic/memcpy.S
@@ -1,184 +1,63 @@
-/* Copyright (c) 2012, Linaro Limited
-   All rights reserved.
-
-   Redistribution and use in source and binary forms, with or without
-   modification, are permitted provided that the following conditions are met:
-       * Redistributions of source code must retain the above copyright
-         notice, this list of conditions and the following disclaimer.
-       * Redistributions in binary form must reproduce the above copyright
-         notice, this list of conditions and the following disclaimer in the
-         documentation and/or other materials provided with the distribution.
-       * Neither the name of the Linaro nor the
-         names of its contributors may be used to endorse or promote products
-         derived from this software without specific prior written permission.
-
-   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
-/* Assumptions:
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
  *
- * ARMv8-a, AArch64
- * Unaligned accesses
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
  *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
+// Prototype: void *memcpy (void *dst, const void *src, size_t count).
+
 #include <private/bionic_asm.h>
+#include <private/libc_events.h>
 
-#define dstin	x0
-#define src	x1
-#define count	x2
-#define tmp1	x3
-#define tmp1w	w3
-#define tmp2	x4
-#define tmp2w	w4
-#define tmp3	x5
-#define tmp3w	w5
-#define dst	x6
+ENTRY(__memcpy_chk)
+  cmp   x2, x3
+  b.hi  __memcpy_chk_fail
 
-#define A_l	x7
-#define A_h	x8
-#define B_l	x9
-#define B_h	x10
-#define C_l	x11
-#define C_h	x12
-#define D_l	x13
-#define D_h	x14
+  // Fall through to memcpy...
+END(__memcpy_chk)
 
 ENTRY(memcpy)
-
-	mov	dst, dstin
-	cmp	count, #64
-	b.ge	.Lcpy_not_short
-	cmp	count, #15
-	b.le	.Ltail15tiny
-
-	/* Deal with small copies quickly by dropping straight into the
-	 * exit block.  */
-.Ltail63:
-	/* Copy up to 48 bytes of data.  At this point we only need the
-	 * bottom 6 bits of count to be accurate.  */
-	ands	tmp1, count, #0x30
-	b.eq	.Ltail15
-	add	dst, dst, tmp1
-	add	src, src, tmp1
-	cmp	tmp1w, #0x20
-	b.eq	1f
-	b.lt	2f
-	ldp	A_l, A_h, [src, #-48]
-	stp	A_l, A_h, [dst, #-48]
-1:
-	ldp	A_l, A_h, [src, #-32]
-	stp	A_l, A_h, [dst, #-32]
-2:
-	ldp	A_l, A_h, [src, #-16]
-	stp	A_l, A_h, [dst, #-16]
-
-.Ltail15:
-	ands	count, count, #15
-	beq	1f
-	add	src, src, count
-	ldp	A_l, A_h, [src, #-16]
-	add	dst, dst, count
-	stp	A_l, A_h, [dst, #-16]
-1:
-	ret
-
-.Ltail15tiny:
-	/* Copy up to 15 bytes of data.  Does not assume additional data
-	   being copied.  */
-	tbz	count, #3, 1f
-	ldr	tmp1, [src], #8
-	str	tmp1, [dst], #8
-1:
-	tbz	count, #2, 1f
-	ldr	tmp1w, [src], #4
-	str	tmp1w, [dst], #4
-1:
-	tbz	count, #1, 1f
-	ldrh	tmp1w, [src], #2
-	strh	tmp1w, [dst], #2
-1:
-	tbz	count, #0, 1f
-	ldrb	tmp1w, [src]
-	strb	tmp1w, [dst]
-1:
-	ret
-
-.Lcpy_not_short:
-	/* We don't much care about the alignment of DST, but we want SRC
-	 * to be 128-bit (16 byte) aligned so that we don't cross cache line
-	 * boundaries on both loads and stores.  */
-	neg	tmp2, src
-	ands	tmp2, tmp2, #15		/* Bytes to reach alignment.  */
-	b.eq	2f
-	sub	count, count, tmp2
-	/* Copy more data than needed; it's faster than jumping
-	 * around copying sub-Quadword quantities.  We know that
-	 * it can't overrun.  */
-	ldp	A_l, A_h, [src]
-	add	src, src, tmp2
-	stp	A_l, A_h, [dst]
-	add	dst, dst, tmp2
-	/* There may be less than 63 bytes to go now.  */
-	cmp	count, #63
-	b.le	.Ltail63
-2:
-	subs	count, count, #128
-	b.ge	.Lcpy_body_large
-	/* Less than 128 bytes to copy, so handle 64 here and then jump
-	 * to the tail.  */
-	ldp	A_l, A_h, [src]
-	ldp	B_l, B_h, [src, #16]
-	ldp	C_l, C_h, [src, #32]
-	ldp	D_l, D_h, [src, #48]
-	stp	A_l, A_h, [dst]
-	stp	B_l, B_h, [dst, #16]
-	stp	C_l, C_h, [dst, #32]
-	stp	D_l, D_h, [dst, #48]
-	tst	count, #0x3f
-	add	src, src, #64
-	add	dst, dst, #64
-	b.ne	.Ltail63
-	ret
-
-	/* Critical loop.  Start at a new cache line boundary.  Assuming
-	 * 64 bytes per line this ensures the entire loop is in one line.  */
-	.p2align 6
-.Lcpy_body_large:
-	/* There are at least 128 bytes to copy.  */
-	ldp	A_l, A_h, [src, #0]
-	sub	dst, dst, #16		/* Pre-bias.  */
-	ldp	B_l, B_h, [src, #16]
-	ldp	C_l, C_h, [src, #32]
-	ldp	D_l, D_h, [src, #48]!	/* src += 64 - Pre-bias.  */
-1:
-	stp	A_l, A_h, [dst, #16]
-	ldp	A_l, A_h, [src, #16]
-	stp	B_l, B_h, [dst, #32]
-	ldp	B_l, B_h, [src, #32]
-	stp	C_l, C_h, [dst, #48]
-	ldp	C_l, C_h, [src, #48]
-	stp	D_l, D_h, [dst, #64]!
-	ldp	D_l, D_h, [src, #64]!
-	subs	count, count, #64
-	b.ge	1b
-	stp	A_l, A_h, [dst, #16]
-	stp	B_l, B_h, [dst, #32]
-	stp	C_l, C_h, [dst, #48]
-	stp	D_l, D_h, [dst, #64]
-	add	src, src, #16
-	add	dst, dst, #64 + 16
-	tst	count, #0x3f
-	b.ne	.Ltail63
-	ret
+  #include "memcpy_base.S"
 END(memcpy)
+
+ENTRY_PRIVATE(__memcpy_chk_fail)
+  // Preserve for accurate backtrace.
+  stp  x29, x30, [sp, -16]!
+  .cfi_def_cfa_offset 16
+  .cfi_rel_offset x29, 0
+  .cfi_rel_offset x30, 8
+
+  adrp  x0, error_string
+  add   x0, x0, :lo12:error_string
+  ldr   x1, error_code
+  bl    __fortify_chk_fail
+error_code:
+  .word   BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW
+END(__memcpy_chk_fail)
+
+  .data
+  .align 2
+error_string:
+  .string "memcpy: prevented write past end of buffer"
diff --git a/libc/arch-arm64/generic-neon/bionic/memcpy.S b/libc/arch-arm64/generic/bionic/memcpy_base.S
similarity index 84%
rename from libc/arch-arm64/generic-neon/bionic/memcpy.S
rename to libc/arch-arm64/generic/bionic/memcpy_base.S
index 320f748..c5d42ce 100644
--- a/libc/arch-arm64/generic-neon/bionic/memcpy.S
+++ b/libc/arch-arm64/generic/bionic/memcpy_base.S
@@ -32,8 +32,6 @@
  *
  */
 
-#include <private/bionic_asm.h>
-
 #define dstin	x0
 #define src	x1
 #define count	x2
@@ -54,13 +52,6 @@
 #define D_l	x13
 #define D_h	x14
 
-#define QA_l q0
-#define QA_h q1
-#define QB_l q2
-#define QB_h q3
-
-ENTRY(memcpy)
-
 	mov	dst, dstin
 	cmp	count, #64
 	b.ge	.Lcpy_not_short
@@ -142,10 +133,14 @@
 	b.ge	.Lcpy_body_large
 	/* Less than 128 bytes to copy, so handle 64 here and then jump
 	 * to the tail.  */
-	ldp QA_l, QA_h, [src]
-	ldp QB_l, QB_h, [src, #32]
-	stp QA_l, QA_h, [dst]
-	stp QB_l, QB_h, [dst, #32]
+	ldp	A_l, A_h, [src]
+	ldp	B_l, B_h, [src, #16]
+	ldp	C_l, C_h, [src, #32]
+	ldp	D_l, D_h, [src, #48]
+	stp	A_l, A_h, [dst]
+	stp	B_l, B_h, [dst, #16]
+	stp	C_l, C_h, [dst, #32]
+	stp	D_l, D_h, [dst, #48]
 	tst	count, #0x3f
 	add	src, src, #64
 	add	dst, dst, #64
@@ -157,23 +152,28 @@
 	.p2align 6
 .Lcpy_body_large:
 	/* There are at least 128 bytes to copy.  */
-	ldp QA_l, QA_h, [src, #0]
-	sub	dst, dst, #32		/* Pre-bias.  */
-	ldp QB_l, QB_h, [src, #32]!	/* src += 64 - Pre-bias.  */
+	ldp	A_l, A_h, [src, #0]
+	sub	dst, dst, #16		/* Pre-bias.  */
+	ldp	B_l, B_h, [src, #16]
+	ldp	C_l, C_h, [src, #32]
+	ldp	D_l, D_h, [src, #48]!	/* src += 64 - Pre-bias.  */
 1:
-	stp QA_l, QA_h, [dst, #32]
-	ldp QA_l, QA_h, [src, #32]
-	stp QB_l, QB_h, [dst, #64]!
-	ldp QB_l, QB_h, [src, #64]!
-
+	stp	A_l, A_h, [dst, #16]
+	ldp	A_l, A_h, [src, #16]
+	stp	B_l, B_h, [dst, #32]
+	ldp	B_l, B_h, [src, #32]
+	stp	C_l, C_h, [dst, #48]
+	ldp	C_l, C_h, [src, #48]
+	stp	D_l, D_h, [dst, #64]!
+	ldp	D_l, D_h, [src, #64]!
 	subs	count, count, #64
 	b.ge	1b
-
-	stp QA_l, QA_h, [dst, #32]
-	stp QB_l, QB_h, [dst, #64]
-	add	src, src, #32
-	add	dst, dst, #64 + 32
+	stp	A_l, A_h, [dst, #16]
+	stp	B_l, B_h, [dst, #32]
+	stp	C_l, C_h, [dst, #48]
+	stp	D_l, D_h, [dst, #64]
+	add	src, src, #16
+	add	dst, dst, #64 + 16
 	tst	count, #0x3f
 	b.ne	.Ltail63
 	ret
-END(memcpy)
diff --git a/libc/arch-mips/mips.mk b/libc/arch-mips/mips.mk
index 31a1f32..ac75a4b 100644
--- a/libc/arch-mips/mips.mk
+++ b/libc/arch-mips/mips.mk
@@ -1,17 +1,32 @@
-# mips specific configs
+# 32-bit mips.
 
-# These are shared by all the 32-bit targets, but not the 64-bit ones.
-libc_common_src_files_mips := \
+#
+# Various kinds of LP32 cruft.
+#
+
+libc_bionic_src_files_mips += \
+    bionic/mmap.cpp \
+
+libc_common_src_files_mips += \
     bionic/legacy_32_bit_support.cpp \
     bionic/ndk_cruft.cpp \
     bionic/time64.c \
+
+libc_netbsd_src_files_mips += \
+    upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
+
+libc_openbsd_src_files_mips += \
     upstream-openbsd/lib/libc/stdio/putw.c \
 
-# These are shared by all the 32-bit targets, but not the 64-bit ones.
-libc_bionic_src_files_mips += \
-     bionic/mmap.cpp
+#
+# Default implementations of functions that are commonly optimized.
+#
 
-libc_common_src_files_mips += \
+libc_bionic_src_files_mips += \
+    bionic/__memcpy_chk.cpp \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
     bionic/memchr.c \
     bionic/memcmp.c \
     bionic/memmove.c \
@@ -19,6 +34,8 @@
     bionic/strchr.cpp \
     bionic/strnlen.c \
     bionic/strrchr.cpp \
+
+libc_freebsd_src_files_mips += \
     upstream-freebsd/lib/libc/string/wcscat.c \
     upstream-freebsd/lib/libc/string/wcschr.c \
     upstream-freebsd/lib/libc/string/wcscmp.c \
@@ -27,6 +44,8 @@
     upstream-freebsd/lib/libc/string/wcsrchr.c \
     upstream-freebsd/lib/libc/string/wmemcmp.c \
     upstream-freebsd/lib/libc/string/wmemmove.c \
+
+libc_openbsd_src_files_mips += \
     upstream-openbsd/lib/libc/string/bcopy.c \
     upstream-openbsd/lib/libc/string/stpcpy.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
@@ -39,16 +58,10 @@
     upstream-openbsd/lib/libc/string/strncmp.c \
     upstream-openbsd/lib/libc/string/strncpy.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 \
+#
+# Inherently architecture-specific code.
+#
 
-
-##########################################
-### CPU specific source files
 libc_bionic_src_files_mips += \
     arch-mips/bionic/__bionic_clone.S \
     arch-mips/bionic/bzero.S \
@@ -69,13 +82,12 @@
 else
 libc_bionic_src_files_mips += \
     bionic/memcpy.cpp \
-    bionic/memset.c
-libc_common_src_files_mips += \
-    upstream-openbsd/lib/libc/string/strlen.c
-endif
+    bionic/memset.c \
 
-libc_netbsd_src_files_mips := \
-    upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
+libc_openbsd_src_files_mips += \
+    upstream-openbsd/lib/libc/string/strlen.c \
+
+endif
 
 libc_crt_target_cflags_mips := \
     $($(my_2nd_arch_prefix)TARGET_GLOBAL_CFLAGS) \
diff --git a/libc/arch-mips64/mips64.mk b/libc/arch-mips64/mips64.mk
index 230cb26..0d4b727 100644
--- a/libc/arch-mips64/mips64.mk
+++ b/libc/arch-mips64/mips64.mk
@@ -1,13 +1,25 @@
-# mips64 specific configs
+# 64-bit mips.
 
-libc_common_src_files_mips64 := \
+#
+# Default implementations of functions that are commonly optimized.
+#
+
+libc_bionic_src_files_mips64 += \
+    bionic/__memcpy_chk.cpp \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
     bionic/memchr.c \
     bionic/memcmp.c \
+    bionic/memcpy.cpp \
     bionic/memmove.c \
     bionic/memrchr.c \
+    bionic/memset.c \
     bionic/strchr.cpp \
     bionic/strnlen.c \
     bionic/strrchr.cpp \
+
+libc_freebsd_src_files_mips64 += \
     upstream-freebsd/lib/libc/string/wcscat.c \
     upstream-freebsd/lib/libc/string/wcschr.c \
     upstream-freebsd/lib/libc/string/wcscmp.c \
@@ -16,6 +28,8 @@
     upstream-freebsd/lib/libc/string/wcsrchr.c \
     upstream-freebsd/lib/libc/string/wmemcmp.c \
     upstream-freebsd/lib/libc/string/wmemmove.c \
+
+libc_openbsd_src_files_mips64 += \
     upstream-openbsd/lib/libc/string/stpcpy.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strcat.c \
@@ -28,17 +42,11 @@
     upstream-openbsd/lib/libc/string/strncmp.c \
     upstream-openbsd/lib/libc/string/strncpy.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 \
+#
+# Inherently architecture-specific code.
+#
 
-
-##########################################
-### CPU specific source files
-libc_bionic_src_files_mips64 := \
+libc_bionic_src_files_mips64 += \
     arch-mips64/bionic/__bionic_clone.S \
     arch-mips64/bionic/_exit_with_stack_teardown.S \
     arch-mips64/bionic/__get_sp.S \
@@ -48,25 +56,18 @@
     arch-mips64/bionic/syscall.S \
     arch-mips64/bionic/vfork.S \
 
-# FIXME TODO
-## 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 += bionic/memcpy.cpp
-libc_bionic_src_files_mips64 += bionic/memset.c
-
-
 libc_crt_target_cflags_mips64 := \
     $($(my_2nd_arch_prefix)TARGET_GLOBAL_CFLAGS) \
-    -I$(LOCAL_PATH)/arch-mips64/include
+    -I$(LOCAL_PATH)/arch-mips64/include \
 
 libc_crt_target_crtbegin_file_mips64 := \
-    $(LOCAL_PATH)/arch-mips64/bionic/crtbegin.c
+    $(LOCAL_PATH)/arch-mips64/bionic/crtbegin.c \
 
 libc_crt_target_crtbegin_so_file_mips64 := \
-    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c \
 
 libc_crt_target_so_cflags_mips64 := \
-    -fPIC
+    -fPIC \
 
 libc_crt_target_ldflags_mips64 := \
-    -melf64ltsmip
+    -melf64ltsmip \
diff --git a/libc/arch-x86/bionic/__restore.S b/libc/arch-x86/bionic/__restore.S
new file mode 100644
index 0000000..cb18fd0
--- /dev/null
+++ b/libc/arch-x86/bionic/__restore.S
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <private/bionic_asm.h>
+
+// DWARF constants.
+#define DW_CFA_def_cfa_expression 0x0f
+#define DW_CFA_expression 0x10
+#define DW_EH_PE_pcrel 0x10
+#define DW_EH_PE_sdata4 0x0b
+#define DW_OP_breg4 0x74
+#define DW_OP_deref 0x06
+
+// Offsets into struct sigcontext.
+#define OFFSET_EDI 16
+#define OFFSET_ESI 20
+#define OFFSET_EBP 24
+#define OFFSET_ESP 28
+#define OFFSET_EBX 32
+#define OFFSET_EDX 36
+#define OFFSET_ECX 40
+#define OFFSET_EAX 44
+#define OFFSET_EIP 56
+
+// Non-standard DWARF constants for the x86 registers.
+#define DW_x86_REG_EAX 0
+#define DW_x86_REG_ECX 1
+#define DW_x86_REG_EDX 2
+#define DW_x86_REG_EBX 3
+#define DW_x86_REG_EBP 5
+#define DW_x86_REG_ESI 6
+#define DW_x86_REG_EDI 7
+#define DW_x86_REG_EIP 8
+
+#define cfi_signal_frame_start(f) \
+.section .eh_frame,"a",@progbits; \
+.L ## f ## _START_EH_FRAME: \
+  .long 2f - 1f; /* CIE length. */ \
+1:.long 0;       /* CIE ID. */ \
+  .byte 1;       /* Version. */ \
+  .string "zRS"; /* Augmentation string. */ \
+  .uleb128 1;    /* Code alignment factor. */ \
+  .sleb128 -4;   /* Data alignment factor. */ \
+  .uleb128 DW_x86_REG_EIP;    /* Return address register. */ \
+  .uleb128 1;    /* 1 byte of augmentation data. */ \
+  .byte (DW_EH_PE_pcrel|DW_EH_PE_sdata4); /* FDE encoding. */ \
+  .align 8; \
+2: \
+  .long .L ## f ## _END_FDE - .L ## f ## _START_FDE;   /* FDE length. */ \
+.L ## f ## _START_FDE: \
+  .long .L ## f ## _START_FDE - .L ## f ## _START_EH_FRAME; /* CIE location. */ \
+  .long (.L ## f ## _START - 1) - .;                   /* pcrel start address (see FDE encoding above). */ \
+  .long .L ## f ## _END - (.L ## f ## _START - 1);     /* Function this FDE applies to. */ \
+  .uleb128 0;                                          /* FDE augmentation length. */ \
+
+#define cfi_signal_frame_end(f) \
+.L ## f ## _END_FDE: \
+
+#define cfi_def_cfa(offset) \
+  .byte DW_CFA_def_cfa_expression; \
+  .uleb128 2f-1f; \
+1:.byte DW_OP_breg4; \
+  .sleb128 offset; \
+  .byte DW_OP_deref; \
+2: \
+
+#define cfi_offset(reg_number,offset) \
+  .byte DW_CFA_expression; \
+  .uleb128 reg_number; \
+  .uleb128 2f-1f; \
+1:.byte DW_OP_breg4; \
+  .sleb128 offset; \
+2: \
+
+ENTRY_PRIVATE(__restore)
+.L__restore_START:
+  popl %eax
+  movl $__NR_sigreturn, %eax
+  int $0x80
+.L__restore_END:
+END(__restore)
+cfi_signal_frame_start(__restore)
+  cfi_def_cfa(OFFSET_ESP + 4)
+  cfi_offset(DW_x86_REG_EDI, OFFSET_EDI + 4)
+  cfi_offset(DW_x86_REG_ESI, OFFSET_ESI + 4)
+  cfi_offset(DW_x86_REG_EBP, OFFSET_EBP + 4)
+  cfi_offset(DW_x86_REG_EBX, OFFSET_EBX + 4)
+  cfi_offset(DW_x86_REG_EDX, OFFSET_EDX + 4)
+  cfi_offset(DW_x86_REG_ECX, OFFSET_ECX + 4)
+  cfi_offset(DW_x86_REG_EAX, OFFSET_EAX + 4)
+  cfi_offset(DW_x86_REG_EIP, OFFSET_EIP + 4)
+cfi_signal_frame_end(__restore)
+
+ENTRY_PRIVATE(__restore_rt)
+.L__restore_rt_START:
+  movl $__NR_rt_sigreturn, %eax
+  int $0x80
+.L__restore_rt_END:
+END(__restore_rt)
+cfi_signal_frame_start(__restore_rt)
+  cfi_def_cfa(OFFSET_ESP + 160)
+  cfi_offset(DW_x86_REG_EDI, OFFSET_EDI + 160)
+  cfi_offset(DW_x86_REG_ESI, OFFSET_ESI + 160)
+  cfi_offset(DW_x86_REG_EBP, OFFSET_EBP + 160)
+  cfi_offset(DW_x86_REG_EBX, OFFSET_EBX + 160)
+  cfi_offset(DW_x86_REG_EDX, OFFSET_EDX + 160)
+  cfi_offset(DW_x86_REG_ECX, OFFSET_ECX + 160)
+  cfi_offset(DW_x86_REG_EAX, OFFSET_EAX + 160)
+  cfi_offset(DW_x86_REG_EIP, OFFSET_EIP + 160)
+cfi_signal_frame_end(__restore_rt)
diff --git a/libc/arch-x86/x86.mk b/libc/arch-x86/x86.mk
index 2a0609d..2c90317 100644
--- a/libc/arch-x86/x86.mk
+++ b/libc/arch-x86/x86.mk
@@ -1,31 +1,45 @@
-# x86 specific configs
+# 32-bit x86.
 
-# These are shared by all the 32-bit targets, but not the 64-bit ones.
-libc_common_src_files_x86 := \
+#
+# Various kinds of LP32 cruft.
+#
+
+libc_bionic_src_files_x86 += \
+    bionic/mmap.cpp \
+
+libc_common_src_files_x86 += \
     bionic/legacy_32_bit_support.cpp \
     bionic/ndk_cruft.cpp \
     bionic/time64.c \
+
+libc_netbsd_src_files_x86 += \
+    upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
+
+libc_openbsd_src_files_x86 += \
     upstream-openbsd/lib/libc/stdio/putw.c \
 
-# Fortify implementations of libc functions.
+#
+# Default implementations of functions that are commonly optimized.
+#
+
 libc_common_src_files_x86 += \
     bionic/__memcpy_chk.cpp \
     bionic/__memset_chk.cpp \
     bionic/__strcpy_chk.cpp \
     bionic/__strcat_chk.cpp \
+
+libc_freebsd_src_files_x86 += \
     upstream-freebsd/lib/libc/string/wmemmove.c \
 
+#
+# Inherently architecture-specific functions.
+#
 
-# 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/libgcc_compat.c \
+    arch-x86/bionic/__restore.S \
     arch-x86/bionic/_setjmp.S \
     arch-x86/bionic/setjmp.S \
     arch-x86/bionic/__set_tls.c \
@@ -41,9 +55,6 @@
 include $(arch_variant_mk)
 libc_common_additional_dependencies += $(arch_variant_mk)
 
-libc_netbsd_src_files_x86 := \
-    upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
-
 arch_variant_mk :=
 
 libc_crt_target_cflags_x86 := \
diff --git a/libc/arch-x86_64/bionic/__restore_rt.S b/libc/arch-x86_64/bionic/__restore_rt.S
new file mode 100644
index 0000000..785b3b3
--- /dev/null
+++ b/libc/arch-x86_64/bionic/__restore_rt.S
@@ -0,0 +1,143 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <private/bionic_asm.h>
+
+// DWARF constants.
+#define DW_CFA_def_cfa_expression 0x0f
+#define DW_CFA_expression 0x10
+#define DW_EH_PE_pcrel 0x10
+#define DW_EH_PE_sdata4 0x0b
+#define DW_OP_breg4 0x74
+#define DW_OP_breg7 0x77
+#define DW_OP_deref 0x06
+
+// Offsets into struct ucontext_t of uc_mcontext.gregs[x].
+#define OFFSET_R8 40
+#define OFFSET_R9 48
+#define OFFSET_R10 56
+#define OFFSET_R11 64
+#define OFFSET_R12 72
+#define OFFSET_R13 80
+#define OFFSET_R14 88
+#define OFFSET_R15 96
+#define OFFSET_RDI 104
+#define OFFSET_RSI 112
+#define OFFSET_RBP 120
+#define OFFSET_RSP 160
+#define OFFSET_RBX 128
+#define OFFSET_RDX 136
+#define OFFSET_RAX 144
+#define OFFSET_RCX 152
+#define OFFSET_RIP 168
+
+// Non-standard DWARF constants for the x86-64 registers.
+#define DW_x86_64_RAX 0
+#define DW_x86_64_RDX 1
+#define DW_x86_64_RCX 2
+#define DW_x86_64_RBX 3
+#define DW_x86_64_RSI 4
+#define DW_x86_64_RDI 5
+#define DW_x86_64_RBP 6
+#define DW_x86_64_RSP 7
+#define DW_x86_64_R8 8
+#define DW_x86_64_R9 9
+#define DW_x86_64_R10 10
+#define DW_x86_64_R11 11
+#define DW_x86_64_R12 12
+#define DW_x86_64_R13 13
+#define DW_x86_64_R14 14
+#define DW_x86_64_R15 15
+#define DW_x86_64_RIP 16
+
+#define cfi_signal_frame_start(f) \
+.section .eh_frame,"a",@progbits; \
+.L ## f ## _START_EH_FRAME: \
+  .long 2f - 1f; /* CIE length. */ \
+1:.long 0;       /* CIE ID. */ \
+  .byte 1;       /* Version. */ \
+  .string "zRS"; /* Augmentation string. */ \
+  .uleb128 1;    /* Code alignment factor. */ \
+  .sleb128 -8;   /* Data alignment factor. */ \
+  .uleb128 DW_x86_64_RIP;   /* Return address register. */ \
+  .uleb128 1;    /* 1 byte of augmentation data. */ \
+  .byte (DW_EH_PE_pcrel | DW_EH_PE_sdata4); /* FDE encoding. */ \
+  .align 8; \
+2: \
+  .long .L ## f ## _END_FDE - .L ## f ## _START_FDE;   /* FDE length. */ \
+.L ## f ## _START_FDE: \
+  .long .L ## f ## _START_FDE - .L ## f ## _START_EH_FRAME; /* CIE location. */ \
+  .long (.L ## f ## _START - 1) - .;                   /* pcrel start address (see FDE encoding above). */ \
+  .long .L ## f ## _END - (.L ## f ## _START - 1);     /* Function this FDE applies to. */ \
+  .uleb128 0;                                          /* FDE augmentation length. */ \
+
+#define cfi_signal_frame_end(f) \
+.L ## f ## _END_FDE: \
+
+#define cfi_def_cfa(offset) \
+  .byte DW_CFA_def_cfa_expression; \
+  .uleb128 2f-1f; \
+1:.byte DW_OP_breg7; \
+  .sleb128 offset; \
+  .byte DW_OP_deref; \
+2: \
+
+#define cfi_offset(reg_number,offset) \
+  .byte DW_CFA_expression; \
+  .uleb128 reg_number; \
+  .uleb128 2f-1f; \
+1:.byte DW_OP_breg7; \
+  .sleb128 offset; \
+2: \
+
+ENTRY_PRIVATE(__restore_rt)
+.L__restore_rt_START:
+  mov $__NR_rt_sigreturn, %rax
+  syscall
+.L__restore_rt_END:
+END(__restore_rt)
+cfi_signal_frame_start(__restore_rt)
+  cfi_def_cfa(OFFSET_RSP)
+  cfi_offset(DW_x86_64_R8, OFFSET_R8)
+  cfi_offset(DW_x86_64_R9, OFFSET_R9)
+  cfi_offset(DW_x86_64_R10, OFFSET_R10)
+  cfi_offset(DW_x86_64_R11, OFFSET_R11)
+  cfi_offset(DW_x86_64_R12, OFFSET_R12)
+  cfi_offset(DW_x86_64_R13, OFFSET_R13)
+  cfi_offset(DW_x86_64_R14, OFFSET_R14)
+  cfi_offset(DW_x86_64_R15, OFFSET_R15)
+  cfi_offset(DW_x86_64_RDI, OFFSET_RDI)
+  cfi_offset(DW_x86_64_RSI, OFFSET_RSI)
+  cfi_offset(DW_x86_64_RBP, OFFSET_RBP)
+  cfi_offset(DW_x86_64_RSP, OFFSET_RSP)
+  cfi_offset(DW_x86_64_RBX, OFFSET_RBX)
+  cfi_offset(DW_x86_64_RDX, OFFSET_RDX)
+  cfi_offset(DW_x86_64_RAX, OFFSET_RAX)
+  cfi_offset(DW_x86_64_RCX, OFFSET_RCX)
+  cfi_offset(DW_x86_64_RIP, OFFSET_RIP)
+cfi_signal_frame_end(__restore_rt)
diff --git a/libc/arch-x86_64/bionic/__rt_sigreturn.S b/libc/arch-x86_64/bionic/__rt_sigreturn.S
deleted file mode 100644
index eddceb1..0000000
--- a/libc/arch-x86_64/bionic/__rt_sigreturn.S
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <private/bionic_asm.h>
-
-ENTRY_PRIVATE(__rt_sigreturn)
-  movl $__NR_rt_sigreturn, %eax
-  syscall
-END(__rt_sigreturn)
diff --git a/libc/arch-x86_64/x86_64.mk b/libc/arch-x86_64/x86_64.mk
index b001b5e..8675ef4 100644
--- a/libc/arch-x86_64/x86_64.mk
+++ b/libc/arch-x86_64/x86_64.mk
@@ -1,11 +1,21 @@
-# x86_64 specific configs
+# 64-bit x86.
 
-libc_common_src_files_x86_64 := \
+#
+# Default implementations of functions that are commonly optimized.
+#
+
+libc_bionic_src_files_x86_64 += \
+    bionic/__memcpy_chk.cpp \
+    bionic/__memset_chk.cpp \
+    bionic/__strcpy_chk.cpp \
+    bionic/__strcat_chk.cpp \
     bionic/memchr.c \
     bionic/memrchr.c \
     bionic/strchr.cpp \
     bionic/strnlen.c \
     bionic/strrchr.cpp \
+
+libc_freebsd_src_files_x86_64 += \
     upstream-freebsd/lib/libc/string/wcscat.c \
     upstream-freebsd/lib/libc/string/wcschr.c \
     upstream-freebsd/lib/libc/string/wcscmp.c \
@@ -14,23 +24,19 @@
     upstream-freebsd/lib/libc/string/wcsrchr.c \
     upstream-freebsd/lib/libc/string/wmemcmp.c \
     upstream-freebsd/lib/libc/string/wmemmove.c \
+
+libc_openbsd_src_files_x86_64 += \
     upstream-openbsd/lib/libc/string/strlcat.c \
     upstream-openbsd/lib/libc/string/strlcpy.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 \
+#
+# Inherently architecture-specific code.
+#
 
-
-##########################################
-### CPU specific source files
-libc_bionic_src_files_x86_64 := \
+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/__rt_sigreturn.S \
+    arch-x86_64/bionic/__restore_rt.S \
     arch-x86_64/bionic/_setjmp.S \
     arch-x86_64/bionic/setjmp.S \
     arch-x86_64/bionic/__set_tls.c \
@@ -38,6 +44,10 @@
     arch-x86_64/bionic/syscall.S \
     arch-x86_64/bionic/vfork.S \
 
+#
+# Optimized memory/string functions.
+#
+
 libc_bionic_src_files_x86_64 += \
     arch-x86_64/string/sse2-memcpy-slm.S \
     arch-x86_64/string/sse2-memmove-slm.S \
@@ -55,15 +65,15 @@
 
 libc_crt_target_cflags_x86_64 += \
     -m64 \
-    -I$(LOCAL_PATH)/arch-x86_64/include
+    -I$(LOCAL_PATH)/arch-x86_64/include \
 
-libc_crt_target_ldflags_x86_64 := -melf_x86_64
+libc_crt_target_ldflags_x86_64 := -melf_x86_64 \
 
 libc_crt_target_crtbegin_file_x86_64 := \
-    $(LOCAL_PATH)/arch-common/bionic/crtbegin.c
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin.c \
 
 libc_crt_target_crtbegin_so_file_x86_64 := \
-    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
+    $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c \
 
 libc_crt_target_so_cflags_x86_64 := \
-    -fPIC
+    -fPIC \
diff --git a/libc/bionic/dl_iterate_phdr_static.cpp b/libc/bionic/dl_iterate_phdr_static.cpp
index 155a7a0..2196ac8 100644
--- a/libc/bionic/dl_iterate_phdr_static.cpp
+++ b/libc/bionic/dl_iterate_phdr_static.cpp
@@ -62,6 +62,11 @@
 
   // Try the VDSO if that didn't work.
   ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(getauxval(AT_SYSINFO_EHDR));
+  if (ehdr_vdso == nullptr) {
+    // There is no VDSO, so there's nowhere left to look.
+    return rc;
+  }
+
   struct dl_phdr_info vdso_info;
   vdso_info.dlpi_addr = 0;
   vdso_info.dlpi_name = NULL;
diff --git a/libc/bionic/sigaction.cpp b/libc/bionic/sigaction.cpp
index 225a823..0633748 100644
--- a/libc/bionic/sigaction.cpp
+++ b/libc/bionic/sigaction.cpp
@@ -28,28 +28,33 @@
 
 #include <signal.h>
 
-#if __LP64__
-extern "C" void __rt_sigreturn(void);
+extern "C" void __restore_rt(void);
+extern "C" void __restore(void);
+
+#if defined(__LP64__)
+
 extern "C" int __rt_sigaction(int, const struct __kernel_sigaction*, struct __kernel_sigaction*, size_t);
-#else
-extern "C" int __sigaction(int, const struct sigaction*, struct sigaction*);
-#endif
 
 int sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) {
-#if __LP64__
   __kernel_sigaction kernel_new_action;
   if (bionic_new_action != NULL) {
     kernel_new_action.sa_flags = bionic_new_action->sa_flags;
     kernel_new_action.sa_handler = bionic_new_action->sa_handler;
     kernel_new_action.sa_mask = bionic_new_action->sa_mask;
-#ifdef SA_RESTORER
+#if defined(SA_RESTORER)
     kernel_new_action.sa_restorer = bionic_new_action->sa_restorer;
-
+#if defined(__aarch64__)
+    // arm64 has sa_restorer, but unwinding works best if you just let the
+    // kernel supply the default restorer from [vdso]. gdb doesn't care, but
+    // libgcc needs the nop that the kernel includes before the actual code.
+    // (We could add that ourselves, but why bother?)
+#else
     if (!(kernel_new_action.sa_flags & SA_RESTORER)) {
       kernel_new_action.sa_flags |= SA_RESTORER;
-      kernel_new_action.sa_restorer = &__rt_sigreturn;
+      kernel_new_action.sa_restorer = &__restore_rt;
     }
 #endif
+#endif
   }
 
   __kernel_sigaction kernel_old_action;
@@ -62,19 +67,36 @@
     bionic_old_action->sa_flags = kernel_old_action.sa_flags;
     bionic_old_action->sa_handler = kernel_old_action.sa_handler;
     bionic_old_action->sa_mask = kernel_old_action.sa_mask;
-#ifdef SA_RESTORER
+#if defined(SA_RESTORER)
     bionic_old_action->sa_restorer = kernel_old_action.sa_restorer;
-
-    if (bionic_old_action->sa_restorer == &__rt_sigreturn) {
-      bionic_old_action->sa_flags &= ~SA_RESTORER;
-    }
 #endif
   }
 
   return result;
-#else
-  // The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t.
-  // TODO: if we also had correct struct sigaction definitions available, we could copy in and out.
-  return __sigaction(signal, bionic_new_action, bionic_old_action);
-#endif
 }
+
+#else
+
+extern "C" int __sigaction(int, const struct sigaction*, struct sigaction*);
+
+int sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) {
+  // The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t,
+  // so we have to use sigaction(2) rather than rt_sigaction(2).
+  struct sigaction kernel_new_action;
+  if (bionic_new_action != NULL) {
+    kernel_new_action.sa_flags = bionic_new_action->sa_flags;
+    kernel_new_action.sa_handler = bionic_new_action->sa_handler;
+    kernel_new_action.sa_mask = bionic_new_action->sa_mask;
+#if defined(SA_RESTORER)
+    kernel_new_action.sa_restorer = bionic_new_action->sa_restorer;
+
+    if (!(kernel_new_action.sa_flags & SA_RESTORER)) {
+      kernel_new_action.sa_flags |= SA_RESTORER;
+      kernel_new_action.sa_restorer = (kernel_new_action.sa_flags & SA_SIGINFO) ? &__restore_rt : &__restore;
+    }
+#endif
+  }
+  return __sigaction(signal, (bionic_new_action != NULL) ? &kernel_new_action : NULL, bionic_old_action);
+}
+
+#endif
diff --git a/libc/tzcode/localtime.c b/libc/tzcode/localtime.c
index 3bbed90..28d13f4 100644
--- a/libc/tzcode/localtime.c
+++ b/libc/tzcode/localtime.c
@@ -2252,14 +2252,11 @@
 }
 
 static int __bionic_open_tzdata(const char* olson_id, int* data_size) {
-  int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/tzdata", olson_id, data_size);
-  if (fd < 0) {
-    fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id, data_size);
-    if (fd == -2) {
-      // The first thing that 'recovery' does is try to format the current time. It doesn't have
-      // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
-      fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
-    }
+  int fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id, data_size);
+  if (fd == -2) {
+    // The first thing that 'recovery' does is try to format the current time. It doesn't have
+    // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
+    fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
   }
   return fd;
 }
diff --git a/tests/Android.mk b/tests/Android.mk
index dd43049..407f21b 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -134,9 +134,6 @@
 libBionicStandardTests_ldlibs_host := \
     -lrt \
 
-libBionicStandardTests_whole_static_libraries := \
-    libBionicUnwindTest \
-
 module := libBionicStandardTests
 module_tag := optional
 build_type := target
@@ -146,25 +143,6 @@
 include $(LOCAL_PATH)/Android.build.mk
 
 # -----------------------------------------------------------------------------
-# Special stack unwinding test library compiled with special flags.
-# -----------------------------------------------------------------------------
-libBionicUnwindTest_cflags := \
-    $(test_cflags) \
-    -fexceptions \
-    -fnon-call-exceptions \
-
-libBionicUnwindTest_src_files := \
-    stack_unwinding_test_impl.c \
-
-module := libBionicUnwindTest
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-include $(LOCAL_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
 # Fortify tests.
 # -----------------------------------------------------------------------------
 $(foreach compiler,gcc clang, \
@@ -249,6 +227,11 @@
     dlfcn_test.cpp \
 
 bionic-unit-tests_cflags := $(test_cflags)
+
+bionic-unit-tests_conlyflags := \
+    -fexceptions \
+    -fnon-call-exceptions \
+
 bionic-unit-tests_cppflags := $(test_cppflags)
 
 bionic-unit-tests_ldflags := \
diff --git a/tests/ScopedSignalHandler.h b/tests/ScopedSignalHandler.h
index 89a14a6..3ec23b0 100644
--- a/tests/ScopedSignalHandler.h
+++ b/tests/ScopedSignalHandler.h
@@ -21,9 +21,10 @@
 
 class ScopedSignalHandler {
  public:
-  ScopedSignalHandler(int signal_number, void (*handler)(int)) : signal_number_(signal_number) {
+  ScopedSignalHandler(int signal_number, void (*handler)(int), int sa_flags = 0)
+      : signal_number_(signal_number) {
     sigemptyset(&action_.sa_mask);
-    action_.sa_flags = 0;
+    action_.sa_flags = sa_flags;
     action_.sa_handler = handler;
     sigaction(signal_number_, &action_, &old_action_);
   }
diff --git a/tests/signal_test.cpp b/tests/signal_test.cpp
index 89b8088..8fd8b72 100644
--- a/tests/signal_test.cpp
+++ b/tests/signal_test.cpp
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include <gtest/gtest.h>
+#include <signal.h>
 
 #include <errno.h>
-#include <signal.h>
+#include <gtest/gtest.h>
 
 #include "ScopedSignalHandler.h"
 
@@ -198,13 +198,19 @@
 static void EmptySignalAction(int, siginfo_t*, void*) {}
 
 TEST(signal, sigaction) {
+  // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm,
+  // arm64, x86, and x86-64. The version of glibc we're using also doesn't
+  // define SA_RESTORER, but luckily it's the same value everywhere, and mips
+  // doesn't use the bit for anything.
+  static const unsigned sa_restorer = 0x4000000;
+
   // See what's currently set for SIGALRM.
   struct sigaction original_sa;
   memset(&original_sa, 0, sizeof(original_sa));
   ASSERT_EQ(0, sigaction(SIGALRM, NULL, &original_sa));
   ASSERT_TRUE(original_sa.sa_handler == NULL);
   ASSERT_TRUE(original_sa.sa_sigaction == NULL);
-  ASSERT_TRUE(original_sa.sa_flags == 0);
+  ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer);
 
   // Set a traditional sa_handler signal handler.
   struct sigaction sa;
@@ -219,7 +225,7 @@
   ASSERT_EQ(0, sigaction(SIGALRM, NULL, &sa));
   ASSERT_TRUE(sa.sa_handler == EmptySignalHandler);
   ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
-  ASSERT_TRUE(sa.sa_flags == SA_ONSTACK);
+  ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer);
 
   // Set a new-style sa_sigaction signal handler.
   memset(&sa, 0, sizeof(sa));
@@ -233,7 +239,7 @@
   ASSERT_EQ(0, sigaction(SIGALRM, NULL, &sa));
   ASSERT_TRUE(sa.sa_sigaction == EmptySignalAction);
   ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
-  ASSERT_TRUE(sa.sa_flags == (SA_ONSTACK | SA_SIGINFO));
+  ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer);
 
   // Put everything back how it was.
   ASSERT_EQ(0, sigaction(SIGALRM, &original_sa, NULL));
diff --git a/tests/stack_unwinding_test.cpp b/tests/stack_unwinding_test.cpp
index 1024f28..3fc45c5 100644
--- a/tests/stack_unwinding_test.cpp
+++ b/tests/stack_unwinding_test.cpp
@@ -20,18 +20,86 @@
 
 #include <gtest/gtest.h>
 
-extern "C" {
-  void do_test();
+#include <dlfcn.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <unwind.h>
+
+#include "ScopedSignalHandler.h"
+
+#define noinline __attribute__((__noinline__))
+#define __unused __attribute__((__unused__))
+
+static _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx __unused, void* arg) {
+  int* count_ptr = reinterpret_cast<int*>(arg);
+
+#if SHOW_FRAME_LOCATIONS
+  void* ip = reinterpret_cast<void*>(_Unwind_GetIP(ctx));
+
+  const char* symbol = "<unknown>";
+  int offset = 0;
+
+  Dl_info info;
+  memset(&info, 0, sizeof(info));
+  if (dladdr(ip, &info) != 0) {
+    symbol = info.dli_sname;
+    if (info.dli_saddr != nullptr) {
+      offset = static_cast<int>(reinterpret_cast<char*>(ip) - reinterpret_cast<char*>(info.dli_saddr));
+    }
+  }
+
+  fprintf(stderr, " #%02d %p %s%+d (%s)\n", *count_ptr, ip, symbol, offset, info.dli_fname ? info.dli_fname : "??");
+  fflush(stderr);
+#endif
+
+  ++*count_ptr;
+  return _URC_NO_REASON;
 }
 
-// We have to say "DeathTest" here so gtest knows to run this test (which exits)
-// in its own process.
-TEST(stack_unwinding_DeathTest, unwinding_through_signal_frame) {
-// Only our x86 unwinding is good enough. Switch to libunwind?
-#if defined(__BIONIC__) && defined(__i386__)
-  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
-  ASSERT_EXIT(do_test(), ::testing::ExitedWithCode(42), "");
-#else // __i386__
-  GTEST_LOG_(INFO) << "This test does nothing.\n";
-#endif // __i386__
+static int noinline unwind_one_frame_deeper() {
+  int count = 0;
+  _Unwind_Backtrace(FrameCounter, &count);
+  return count;
+}
+
+TEST(stack_unwinding, easy) {
+  int count = 0;
+  _Unwind_Backtrace(FrameCounter, &count);
+  int deeper_count = unwind_one_frame_deeper();
+  ASSERT_EQ(count + 1, deeper_count);
+}
+
+static int killer_count = 0;
+static int handler_count = 0;
+static int handler_one_deeper_count = 0;
+
+static void noinline UnwindSignalHandler(int) {
+  _Unwind_Backtrace(FrameCounter, &handler_count);
+  ASSERT_GT(handler_count, killer_count);
+
+  handler_one_deeper_count = unwind_one_frame_deeper();
+  ASSERT_EQ(handler_count + 1, handler_one_deeper_count);
+}
+
+TEST(stack_unwinding, unwind_through_signal_frame) {
+  killer_count = handler_count = handler_one_deeper_count = 0;
+  ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler);
+
+  _Unwind_Backtrace(FrameCounter, &killer_count);
+
+  ASSERT_EQ(0, kill(getpid(), SIGUSR1));
+}
+
+// On LP32, the SA_SIGINFO flag gets you __restore_rt instead of __restore.
+TEST(stack_unwinding, unwind_through_signal_frame_SA_SIGINFO) {
+  killer_count = handler_count = handler_one_deeper_count = 0;
+  ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler, SA_SIGINFO);
+
+  _Unwind_Backtrace(FrameCounter, &killer_count);
+
+  ASSERT_EQ(0, kill(getpid(), SIGUSR1));
 }
diff --git a/tests/stack_unwinding_test_impl.c b/tests/stack_unwinding_test_impl.c
deleted file mode 100644
index 7518a2c..0000000
--- a/tests/stack_unwinding_test_impl.c
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/*
- * Contributed by: Intel Corporation
- */
-
-#include <stdio.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unwind.h>
-
-#define noinline __attribute__((__noinline__))
-#define unused __attribute__((__unused__))
-
-static noinline _Unwind_Reason_Code stop_fn(int a unused,
-    _Unwind_Action action,
-    _Unwind_Exception_Class b unused, struct _Unwind_Exception* c unused,
-    struct _Unwind_Context* d unused, void* e unused) {
-  if ((action & _UA_END_OF_STACK) != 0) {
-    // We reached the end of the stack without executing foo_cleanup. Test failed.
-    abort();
-  }
-  return _URC_NO_REASON;
-}
-
-static void noinline foo_cleanup(char* param unused) {
-  exit(42);
-}
-
-static void noinline do_crash() {
-  char* ptr = NULL;
-  *ptr = 0; // Deliberately cause a SIGSEGV.
-}
-
-static void noinline foo() {
-  char c1 __attribute__((cleanup(foo_cleanup))) unused;
-  do_crash();
-}
-
-// It's SEGSEGV handler. We start forced stack unwinding here.
-// If libgcc don't find dso for signal frame stack unwinding will be finished.
-// libgcc pass to stop_fn _UA_END_OF_STACK flag.
-// Test pass condition: stack unwinding through signal frame and foo1_handler execution.
-static void noinline sigsegv_handler(int param unused) {
-  struct _Unwind_Exception* exception = (struct _Unwind_Exception*) malloc(sizeof(*exception));
-  memset(&exception->exception_class, 0, sizeof(exception->exception_class));
-  exception->exception_cleanup = 0;
-  _Unwind_ForcedUnwind(exception, stop_fn, 0);
-}
-
-void do_test() {
-  signal(SIGSEGV, &sigsegv_handler);
-  foo();
-}