From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from magnesium.8pit.net (magnesium.8pit.net [45.76.88.171]) by sourceware.org (Postfix) with ESMTP id 423A13857C41; Fri, 11 Mar 2022 07:34:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 423A13857C41 Received: from localhost (ip5f5ae040.dynamic.kabel-deutschland.de [95.90.224.64]) by magnesium.8pit.net (OpenSMTPD) with ESMTPSA id e28c8df8 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:YES); Fri, 11 Mar 2022 08:34:25 +0100 (CET) From: soeren@soeren-tempel.net To: gcc-patches@gcc.gnu.org Cc: iant@golang.org, gofrontend-dev@googlegroups.com, schwab@linux-m68k.org, dalias@libc.org Subject: [PATCH v4] libgo: Don't use pt_regs member in mcontext_t Date: Fri, 11 Mar 2022 08:34:06 +0100 Message-Id: <20220311073406.26722-1-soeren@soeren-tempel.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220309115236.GU7074@brightrain.aerifal.cx> References: <20220309115236.GU7074@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Mar 2022 07:34:29 -0000 From: Sören Tempel The .regs member is primarily intended to be used in conjunction with ptrace. Since this code is not using ptrace, using .regs is a bad idea. Furthermore, the code currently fails to compile on musl since the pt_regs type (used by .regs) is in an incomplete type which has to be completed by inclusion of the asm/ptrace.h Kernel header. Contrary to glibc, this header is not indirectly included by musl through other header files. This patch fixes compilation of this code with musl libc by accessing the register values via .gp_regs/.gregs (depending on 32-bit or 64-bit PowerPC) instead of using .regs. For more details, see https://gcc.gnu.org/pipermail/gcc-patches/2022-March/591261.html For the offsets in gp_regs refer to the Kernel asm/ptrace.h header. This patch has been tested on Alpine Linux ppc64le (uses musl libc). Signed-off-by: Sören Tempel ChangeLog: * libgo/runtime/go-signal.c (defined): Use .gp_regs/.gregs to access ppc64/ppc32 registers. (dumpregs): Ditto. --- Changes since v3: Add special handling for 32-bit PowerPC with glibc, also avoid use of gregs_t type since glibc does not seem to define it on PowerPC. This version of the patch introduces a new macro (PPC_GPREGS) to access these registers to special case musl/glibc handling in a central place once instead of duplicating it twice. libgo/runtime/go-signal.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/libgo/runtime/go-signal.c b/libgo/runtime/go-signal.c index d30d1603adc..3255046260d 100644 --- a/libgo/runtime/go-signal.c +++ b/libgo/runtime/go-signal.c @@ -16,6 +16,21 @@ #define SA_RESTART 0 #endif +// The PowerPC API for accessing gregs/gp_regs differs greatly across +// different libc implementations (musl and glibc). To workaround that, +// define the canonical way to access these registers once here. +// +// See https://gcc.gnu.org/pipermail/gcc-patches/2022-March/591360.html +#ifdef __PPC__ +#if defined(__PPC64__) /* ppc64 glibc & musl */ +#define PPC_GPREGS(MCTX) (MCTX)->gp_regs +#elif defined(__GLIBC__) /* ppc32 glibc */ +#define PPC_GPREGS(MCTX) (MCTX)->uc_regs->gregs +#else /* ppc32 musl */ +#define PPC_GPREGS(MCTX) (MCTX)->gregs +#endif /* __PPC64__ */ +#endif /* __PPC__ */ + #ifdef USING_SPLIT_STACK extern void __splitstack_getcontext(void *context[10]); @@ -224,7 +239,8 @@ getSiginfo(siginfo_t *info, void *context __attribute__((unused))) #elif defined(__alpha__) && defined(__linux__) ret.sigpc = ((ucontext_t*)(context))->uc_mcontext.sc_pc; #elif defined(__PPC__) && defined(__linux__) - ret.sigpc = ((ucontext_t*)(context))->uc_mcontext.regs->nip; + mcontext_t *m = &((ucontext_t*)(context))->uc_mcontext; + ret.sigpc = PPC_GPREGS(m)[32]; #elif defined(__PPC__) && defined(_AIX) ret.sigpc = ((ucontext_t*)(context))->uc_mcontext.jmp_context.iar; #elif defined(__aarch64__) && defined(__linux__) @@ -341,13 +357,13 @@ dumpregs(siginfo_t *info __attribute__((unused)), void *context __attribute__((u int i; for (i = 0; i < 32; i++) - runtime_printf("r%d %X\n", i, m->regs->gpr[i]); - runtime_printf("pc %X\n", m->regs->nip); - runtime_printf("msr %X\n", m->regs->msr); - runtime_printf("cr %X\n", m->regs->ccr); - runtime_printf("lr %X\n", m->regs->link); - runtime_printf("ctr %X\n", m->regs->ctr); - runtime_printf("xer %X\n", m->regs->xer); + runtime_printf("r%d %X\n", i, PPC_GPREGS(m)[i]); + runtime_printf("pc %X\n", PPC_GPREGS(m)[32]); + runtime_printf("msr %X\n", PPC_GPREGS(m)[33]); + runtime_printf("cr %X\n", PPC_GPREGS(m)[38]); + runtime_printf("lr %X\n", PPC_GPREGS(m)[36]); + runtime_printf("ctr %X\n", PPC_GPREGS(m)[35]); + runtime_printf("xer %X\n", PPC_GPREGS(m)[37]); } #elif defined(__PPC__) && defined(_AIX) {