From 1526be49b8c49719459ef15c93090fdbdc10f3cb Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Sat, 13 Mar 2021 00:42:25 -0800
Subject: [PATCH 1/3] Compatibility fixes for musl.

Upstream-Status: Pending

Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
---
 src/getpc.h                        | 4 ++++
 src/stacktrace_powerpc-linux-inl.h | 8 ++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/getpc.h b/src/getpc.h
index c14c9d0..cf4866b 100644
--- a/src/getpc.h
+++ b/src/getpc.h
@@ -61,6 +61,10 @@
 typedef ucontext ucontext_t;
 #endif
 
+#if defined(__powerpc__) && !defined(PT_NIP)
+#define PT_NIP 32
+#endif
+
 namespace tcmalloc {
 namespace getpc {
 
diff --git a/src/stacktrace_powerpc-linux-inl.h b/src/stacktrace_powerpc-linux-inl.h
index 883e7d2..212bd25 100644
--- a/src/stacktrace_powerpc-linux-inl.h
+++ b/src/stacktrace_powerpc-linux-inl.h
@@ -186,7 +186,7 @@ static int GET_STACK_TRACE_OR_FRAMES {
           ucontext_t uc;
         // We don't care about the rest, since the IP value is at 'uc' field.
         } *sigframe = reinterpret_cast<signal_frame_64*>(current);
-        result[n] = (void*) sigframe->uc.uc_mcontext.gp_regs[PT_NIP];
+        result[n] = (void*) sigframe->uc.uc_mcontext.gp_regs[32];
       }
 #else
       if (sigtramp32_vdso && (sigtramp32_vdso == current->return_addr)) {
@@ -196,7 +196,7 @@ static int GET_STACK_TRACE_OR_FRAMES {
           mcontext_t mctx;
           // We don't care about the rest, since IP value is at 'mctx' field.
         } *sigframe = reinterpret_cast<signal_frame_32*>(current);
-        result[n] = (void*) sigframe->mctx.gregs[PT_NIP];
+        result[n] = (void*) sigframe->mctx.gregs[32];
       } else if (sigtramp32_rt_vdso && (sigtramp32_rt_vdso == current->return_addr)) {
         struct rt_signal_frame_32 {
           char dummy[64 + 16];
@@ -204,7 +204,11 @@ static int GET_STACK_TRACE_OR_FRAMES {
           ucontext_t uc;
           // We don't care about the rest, since IP value is at 'uc' field.A
         } *sigframe = reinterpret_cast<rt_signal_frame_32*>(current);
+#if defined(__GLIBC__)
         result[n] = (void*) sigframe->uc.uc_mcontext.uc_regs->gregs[PT_NIP];
+#else
+        result[n] = (void*) sigframe->uc.uc_mcontext.gregs[32];
+#endif
       }
 #endif
 
-- 
2.25.1

