Fix signal trampolines.

* LP32 should use sa_restorer too. gdb expects this, and future (>= 3.15) x86
  kernels will apparently stop supporting the case where SA_RESTORER isn't
  set.

* gdb and libunwind care about the exact instruction sequences, so we need to
  modify the code slightly in a few cases to match what they're looking for.

* gdb also cares about the exact function names (for some architectures),
  so we need to use __restore and __restore_rt rather than __sigreturn and
  __rt_sigreturn.

* It's possible that we don't have a VDSO; dl_iterate_phdr shouldn't assume
  that getauxval(AT_SYSINFO_EHDR) will return a non-null pointer.

This fixes unwinding through a signal handler in gdb for all architectures.
It doesn't fix libunwind for arm and arm64. I'll keep investigating that...

(cherry picked from commit 36f451a6d93b6807944d99fa23396e039c47e845)

Bug: 17436734
Change-Id: Ic1ea1184db6655c5d96180dc07bcc09628e647cb
diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk
index 70cc8eb..fbde87c 100644
--- a/libc/arch-arm/arm.mk
+++ b/libc/arch-arm/arm.mk
@@ -55,6 +55,8 @@
     arch-arm/bionic/_exit_with_stack_teardown.S \
     arch-arm/bionic/libgcc_compat.c \
     arch-arm/bionic/memcmp.S \
+    arch-arm/bionic/__restore_rt.S \
+    arch-arm/bionic/__restore.S \
     arch-arm/bionic/_setjmp.S \
     arch-arm/bionic/setjmp.S \
     arch-arm/bionic/sigsetjmp.S \
diff --git a/libc/arch-arm64/bionic/__rt_sigreturn.S b/libc/arch-arm/bionic/__restore.S
similarity index 86%
copy from libc/arch-arm64/bionic/__rt_sigreturn.S
copy to libc/arch-arm/bionic/__restore.S
index 8fb6f0c..e76628e 100644
--- a/libc/arch-arm64/bionic/__rt_sigreturn.S
+++ b/libc/arch-arm/bionic/__restore.S
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 The Android Open Source Project
+ * Copyright (C) 2014 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,8 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY_PRIVATE(__rt_sigreturn)
-  mov     x8, __NR_rt_sigreturn
-  svc     #0
-END(__rt_sigreturn)
+// This function must have exactly this instruction sequence for gdb and libunwind.
+ENTRY_PRIVATE(__restore)
+  mov r7, #__NR_sigreturn
+  swi #0
+END(__restore)
diff --git a/libc/arch-arm64/bionic/__rt_sigreturn.S b/libc/arch-arm/bionic/__restore_rt.S
similarity index 85%
copy from libc/arch-arm64/bionic/__rt_sigreturn.S
copy to libc/arch-arm/bionic/__restore_rt.S
index 8fb6f0c..5a1fca1 100644
--- a/libc/arch-arm64/bionic/__rt_sigreturn.S
+++ b/libc/arch-arm/bionic/__restore_rt.S
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 The Android Open Source Project
+ * Copyright (C) 2014 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,8 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY_PRIVATE(__rt_sigreturn)
-  mov     x8, __NR_rt_sigreturn
-  svc     #0
-END(__rt_sigreturn)
+// This function must have exactly this instruction sequence for gdb and libunwind.
+ENTRY_PRIVATE(__restore_rt)
+  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..91cd9fb 100644
--- a/libc/arch-arm64/arm64.mk
+++ b/libc/arch-arm64/arm64.mk
@@ -29,7 +29,7 @@
 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/__restore_rt.S \
     arch-arm64/bionic/_setjmp.S \
     arch-arm64/bionic/setjmp.S \
     arch-arm64/bionic/__set_tls.c \
diff --git a/libc/arch-arm64/bionic/__rt_sigreturn.S b/libc/arch-arm64/bionic/__restore_rt.S
similarity index 89%
rename from libc/arch-arm64/bionic/__rt_sigreturn.S
rename to libc/arch-arm64/bionic/__restore_rt.S
index 8fb6f0c..9506490 100644
--- a/libc/arch-arm64/bionic/__rt_sigreturn.S
+++ b/libc/arch-arm64/bionic/__restore_rt.S
@@ -28,7 +28,8 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY_PRIVATE(__rt_sigreturn)
-  mov     x8, __NR_rt_sigreturn
-  svc     #0
-END(__rt_sigreturn)
+// This function must have exactly this instruction sequence for gdb and libunwind.
+ENTRY_PRIVATE(__restore_rt)
+  mov x8, __NR_rt_sigreturn
+  svc #0
+END(__restore_rt)
diff --git a/libc/arch-arm64/bionic/__rt_sigreturn.S b/libc/arch-x86/bionic/__restore.S
similarity index 82%
copy from libc/arch-arm64/bionic/__rt_sigreturn.S
copy to libc/arch-x86/bionic/__restore.S
index 8fb6f0c..755c3f8 100644
--- a/libc/arch-arm64/bionic/__rt_sigreturn.S
+++ b/libc/arch-x86/bionic/__restore.S
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 The Android Open Source Project
+ * Copyright (C) 2014 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,10 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY_PRIVATE(__rt_sigreturn)
-  mov     x8, __NR_rt_sigreturn
-  svc     #0
-END(__rt_sigreturn)
+// This function must have exactly this instruction sequence for gdb and libunwind.
+// This function must have exactly this name for gdb.
+ENTRY(__restore)
+  popl %eax
+  movl $__NR_sigreturn, %eax
+  int $0x80
+END(__restore)
diff --git a/libc/arch-x86_64/bionic/__rt_sigreturn.S b/libc/arch-x86/bionic/__restore_rt.S
similarity index 84%
copy from libc/arch-x86_64/bionic/__rt_sigreturn.S
copy to libc/arch-x86/bionic/__restore_rt.S
index eddceb1..0cd8081 100644
--- a/libc/arch-x86_64/bionic/__rt_sigreturn.S
+++ b/libc/arch-x86/bionic/__restore_rt.S
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 The Android Open Source Project
+ * Copyright (C) 2014 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,9 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY_PRIVATE(__rt_sigreturn)
+// This function must have exactly this instruction sequence for gdb and libunwind.
+// This function must have exactly this name for gdb.
+ENTRY(__restore_rt)
   movl $__NR_rt_sigreturn, %eax
-  syscall
-END(__rt_sigreturn)
+  int $0x80
+END(__restore_rt)
diff --git a/libc/arch-x86/x86.mk b/libc/arch-x86/x86.mk
index 2a0609d..9055197 100644
--- a/libc/arch-x86/x86.mk
+++ b/libc/arch-x86/x86.mk
@@ -26,6 +26,8 @@
     arch-x86/bionic/__bionic_clone.S \
     arch-x86/bionic/_exit_with_stack_teardown.S \
     arch-x86/bionic/libgcc_compat.c \
+    arch-x86/bionic/__restore_rt.S \
+    arch-x86/bionic/__restore.S \
     arch-x86/bionic/_setjmp.S \
     arch-x86/bionic/setjmp.S \
     arch-x86/bionic/__set_tls.c \
diff --git a/libc/arch-x86_64/bionic/__rt_sigreturn.S b/libc/arch-x86_64/bionic/__restore_rt.S
similarity index 87%
rename from libc/arch-x86_64/bionic/__rt_sigreturn.S
rename to libc/arch-x86_64/bionic/__restore_rt.S
index eddceb1..d84be21 100644
--- a/libc/arch-x86_64/bionic/__rt_sigreturn.S
+++ b/libc/arch-x86_64/bionic/__restore_rt.S
@@ -28,7 +28,9 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY_PRIVATE(__rt_sigreturn)
-  movl $__NR_rt_sigreturn, %eax
+// This function must have exactly this instruction sequence for gdb and libunwind.
+// This function must have exactly this name for gdb.
+ENTRY(__restore_rt)
+  mov $__NR_rt_sigreturn, %rax
   syscall
-END(__rt_sigreturn)
+END(__restore_rt)
diff --git a/libc/arch-x86_64/x86_64.mk b/libc/arch-x86_64/x86_64.mk
index b001b5e..5f12a49 100644
--- a/libc/arch-x86_64/x86_64.mk
+++ b/libc/arch-x86_64/x86_64.mk
@@ -30,7 +30,7 @@
 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 \
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..8ba4e2a 100644
--- a/libc/bionic/sigaction.cpp
+++ b/libc/bionic/sigaction.cpp
@@ -28,8 +28,10 @@
 
 #include <signal.h>
 
+extern "C" void __restore_rt(void);
+extern "C" void __restore(void);
+
 #if __LP64__
-extern "C" void __rt_sigreturn(void);
 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*);
@@ -47,7 +49,7 @@
 
     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
   }
@@ -75,6 +77,20 @@
 #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);
+  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;
+#ifdef 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
 }