[asan] move more code into OS-specific files

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@147671 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_internal.h b/lib/asan/asan_internal.h
index 1f078fa..169ed53 100644
--- a/lib/asan/asan_internal.h
+++ b/lib/asan/asan_internal.h
@@ -100,6 +100,7 @@
 void AsanUnmapOrDie(void *ptr, size_t size);
 
 void AsanDisableCoreDumper();
+void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
 
 ssize_t AsanRead(int fd, void *buf, size_t count);
 ssize_t AsanWrite(int fd, const void *buf, size_t count);
diff --git a/lib/asan/asan_linux.cc b/lib/asan/asan_linux.cc
index bd92617..2167efa 100644
--- a/lib/asan/asan_linux.cc
+++ b/lib/asan/asan_linux.cc
@@ -29,6 +29,11 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#ifndef ANDROID
+// FIXME: where to get ucontext on Android?
+#include <sys/ucontext.h>
+#endif
+
 namespace __asan {
 
 void *AsanDoesNotSupportStaticLinkage() {
@@ -36,6 +41,29 @@
   return &_DYNAMIC;  // defined in link.h
 }
 
+void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) {
+#ifdef ANDROID
+  *pc = *sp = *bp = 0;
+#elif defined(__arm__)
+  ucontext_t *ucontext = (ucontext_t*)context;
+  *pc = ucontext->uc_mcontext.arm_pc;
+  *bp = ucontext->uc_mcontext.arm_fp;
+  *sp = ucontext->uc_mcontext.arm_sp;
+# elif defined(__x86_64__)
+  ucontext_t *ucontext = (ucontext_t*)context;
+  *pc = ucontext->uc_mcontext.gregs[REG_RIP];
+  *bp = ucontext->uc_mcontext.gregs[REG_RBP];
+  *sp = ucontext->uc_mcontext.gregs[REG_RSP];
+# elif defined(__i386__)
+  ucontext_t *ucontext = (ucontext_t*)context;
+  *pc = ucontext->uc_mcontext.gregs[REG_EIP];
+  *bp = ucontext->uc_mcontext.gregs[REG_EBP];
+  *sp = ucontext->uc_mcontext.gregs[REG_ESP];
+#else
+# error "Unsupported arch"
+#endif
+}
+
 static void *asan_mmap(void *addr, size_t length, int prot, int flags,
                 int fd, uint64_t offset) {
 # if __WORDSIZE == 64
diff --git a/lib/asan/asan_mac.cc b/lib/asan/asan_mac.cc
index 6d73309..af2d0e1 100644
--- a/lib/asan/asan_mac.cc
+++ b/lib/asan/asan_mac.cc
@@ -23,6 +23,7 @@
 
 #include <sys/mman.h>
 #include <sys/resource.h>
+#include <sys/ucontext.h>
 #include <pthread.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -38,6 +39,20 @@
 extern dispatch_group_async_f_f real_dispatch_group_async_f;
 extern pthread_workqueue_additem_np_f real_pthread_workqueue_additem_np;
 
+void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) {
+  ucontext_t *ucontext = (ucontext_t*)context;
+# if __WORDSIZE == 64
+  *pc = ucontext->uc_mcontext->__ss.__rip;
+  *bp = ucontext->uc_mcontext->__ss.__rbp;
+  *sp = ucontext->uc_mcontext->__ss.__rsp;
+# else
+  *pc = ucontext->uc_mcontext->__ss.__eip;
+  *bp = ucontext->uc_mcontext->__ss.__ebp;
+  *sp = ucontext->uc_mcontext->__ss.__esp;
+# endif  // __WORDSIZE
+}
+
+
 // No-op. Mac does not support static linkage anyway.
 void *AsanDoesNotSupportStaticLinkage() {
   return NULL;
diff --git a/lib/asan/asan_rtl.cc b/lib/asan/asan_rtl.cc
index 67b62da..93701db 100644
--- a/lib/asan/asan_rtl.cc
+++ b/lib/asan/asan_rtl.cc
@@ -36,9 +36,6 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#ifndef ANDROID
-#include <sys/ucontext.h>
-#endif
 // must not include <setjmp.h> on Linux
 
 namespace __asan {
@@ -271,45 +268,6 @@
 }
 
 // -------------------------- Run-time entry ------------------- {{{1
-void GetPcSpBpAx(void *context,
-                 uintptr_t *pc, uintptr_t *sp, uintptr_t *bp, uintptr_t *ax) {
-#ifndef ANDROID
-  ucontext_t *ucontext = (ucontext_t*)context;
-#endif
-#ifdef __APPLE__
-# if __WORDSIZE == 64
-  *pc = ucontext->uc_mcontext->__ss.__rip;
-  *bp = ucontext->uc_mcontext->__ss.__rbp;
-  *sp = ucontext->uc_mcontext->__ss.__rsp;
-  *ax = ucontext->uc_mcontext->__ss.__rax;
-# else
-  *pc = ucontext->uc_mcontext->__ss.__eip;
-  *bp = ucontext->uc_mcontext->__ss.__ebp;
-  *sp = ucontext->uc_mcontext->__ss.__esp;
-  *ax = ucontext->uc_mcontext->__ss.__eax;
-# endif  // __WORDSIZE
-#else  // assume linux
-# if defined (ANDROID)
-  *pc = *sp = *bp = *ax = 0;
-# elif defined(__arm__)
-  *pc = ucontext->uc_mcontext.arm_pc;
-  *bp = ucontext->uc_mcontext.arm_fp;
-  *sp = ucontext->uc_mcontext.arm_sp;
-  *ax = ucontext->uc_mcontext.arm_r0;
-# elif __WORDSIZE == 64
-  *pc = ucontext->uc_mcontext.gregs[REG_RIP];
-  *bp = ucontext->uc_mcontext.gregs[REG_RBP];
-  *sp = ucontext->uc_mcontext.gregs[REG_RSP];
-  *ax = ucontext->uc_mcontext.gregs[REG_RAX];
-# else
-  *pc = ucontext->uc_mcontext.gregs[REG_EIP];
-  *bp = ucontext->uc_mcontext.gregs[REG_EBP];
-  *sp = ucontext->uc_mcontext.gregs[REG_ESP];
-  *ax = ucontext->uc_mcontext.gregs[REG_EAX];
-# endif  // __WORDSIZE
-#endif
-}
-
 static void     ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) {
   uintptr_t addr = (uintptr_t)siginfo->si_addr;
   if (AddrIsInShadow(addr) && FLAG_lazy_shadow) {
@@ -322,11 +280,11 @@
   }
   // Write the first message using the bullet-proof write.
   if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) ASAN_DIE;
-  uintptr_t pc, sp, bp, ax;
-  GetPcSpBpAx(context, &pc, &sp, &bp, &ax);
+  uintptr_t pc, sp, bp;
+  GetPcSpBp(context, &pc, &sp, &bp);
   Report("ERROR: AddressSanitizer crashed on unknown address %p"
-         " (pc %p sp %p bp %p ax %p T%d)\n",
-         addr, pc, sp, bp, ax,
+         " (pc %p sp %p bp %p T%d)\n",
+         addr, pc, sp, bp,
          asanThreadRegistry().GetCurrentTidOrMinusOne());
   Printf("AddressSanitizer can not provide additional info. ABORTING\n");
   GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, false, pc, bp);