public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Rich Felker <dalias@libc.org>
To: Ian Lance Taylor <iant@golang.org>
Cc: "Sören Tempel" <soeren@soeren-tempel.net>,
	gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com
Subject: Re: [gofrontend-dev] Re: [PATCH] libgo: include asm/ptrace.h for pt_regs definition on PowerPC
Date: Sun, 6 Mar 2022 10:22:56 -0500	[thread overview]
Message-ID: <20220306152256.GG7074@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAOyqgcUD5VqWm1Pqe_DNpD9SwHM3eCJiriD0ppa0Qo3uoyTpgg@mail.gmail.com>

On Mon, Feb 21, 2022 at 09:25:43AM -0800, Ian Lance Taylor via Gcc-patches wrote:
> Note for gofrontend-dev: on gcc-patches only Andreas Schwab suggested
> using uc_regs instead of regs, which does look correct to me.

Yes, this is absolutely the correct fix. Having pt_regs appear at all
in code not using ptrace is a serious code smell.

The root of this problem is twofold: (1) ancient Linux (2.0.x?) had a
bad definition of powerpc32 ucontext_t that lacked any mcontext_t,
instead having a regs member pointing to the storage for the register
state (as pt_regs). This was ostensibly done for extensibility
reasons, but was non-POSIX-conforming and broken, and was later fixed.

And (2) glibc's definition of ucontext_t is also non-conforming,
making the uc_mcontext member have type anon-union rather than type
mcontext_t.

musl does not follow this but puts the uc_mcontext member in the place
later kernel ABI assigned to it after the kernel mistake was fixed.

Ideally you would access uc_mcontext.gregs[32] (32==NIP) and be done
with it, but this won't work on glibc because of (2). However musl
also supports the old uc_regs pointer (it's in the reserved namespace
anyway so not a conformance error), making it so uc_regs->gregs[32]
works on either.

Rich



> On Mon, Feb 21, 2022 at 8:47 AM Sören Tempel <soeren@soeren-tempel.net> wrote:
> >
> > Ping.
> >
> > Summary: Fix build of libgo on PPC with musl libc and libucontext by
> > explicitly including the Linux header defining `struct pt_regs` instead of
> > relying on other libc headers to include it implicitly.
> >
> > See: https://gcc.gnu.org/pipermail/gcc-patches/2022-January/587520.html
> >
> > If the patch needs to be revised further please let me know. This patch has
> > been applied at Alpine Linux downstream (which uses musl libc) for a while, I
> > have not tested it on other systems.
> >
> > Greetings,
> > Sören
> >
> > Sören Tempel <soeren@soeren-tempel.net> wrote:
> > > Both glibc and musl libc declare pt_regs as an incomplete type. This
> > > type has to be completed by inclusion of another header. On Linux, the
> > > asm/ptrace.h header file provides this type definition. Without
> > > including this header file, it is not possible to access the regs member
> > > of the mcontext_t struct as done in libgo/runtime/go-signal.c. On glibc,
> > > other headers (e.g. sys/user.h) include asm/ptrace.h but on musl
> > > asm/ptrace.h is not included by other headers and thus the
> > > aforementioned files do not compile without an explicit include of
> > > asm/ptrace.h:
> > >
> > >       libgo/runtime/go-signal.c: In function 'getSiginfo':
> > >       libgo/runtime/go-signal.c:227:63: error: invalid use of undefined type 'struct pt_regs'
> > >         227 |         ret.sigpc = ((ucontext_t*)(context))->uc_mcontext.regs->nip;
> > >             |
> > >
> > > See also:
> > >
> > > * https://git.musl-libc.org/cgit/musl/commit/?id=c2518a8efb6507f1b41c3b12e03b06f8f2317a1f
> > > * https://github.com/kaniini/libucontext/issues/36
> > >
> > > Signed-off-by: Sören Tempel <soeren@soeren-tempel.net>
> > >
> > > ChangeLog:
> > >
> > >       * libgo/runtime/go-signal.c: Include asm/ptrace.h for the
> > >         definition of pt_regs (used by mcontext_t) on PowerPC.
> > > ---
> > >  libgo/runtime/go-signal.c | 6 ++++++
> > >  1 file changed, 6 insertions(+)
> > >
> > > diff --git a/libgo/runtime/go-signal.c b/libgo/runtime/go-signal.c
> > > index d30d1603adc..fc01e04e4a1 100644
> > > --- a/libgo/runtime/go-signal.c
> > > +++ b/libgo/runtime/go-signal.c
> > > @@ -10,6 +10,12 @@
> > >  #include <sys/time.h>
> > >  #include <ucontext.h>
> > >
> > > +// On PowerPC, ucontext.h uses a pt_regs struct as an incomplete
> > > +// type. This type must be completed by including asm/ptrace.h.
> > > +#ifdef __PPC__
> > > +#include <asm/ptrace.h>
> > > +#endif
> > > +
> > >  #include "runtime.h"
> > >
> > >  #ifndef SA_RESTART
> >
> > --
> > You received this message because you are subscribed to the Google Groups "gofrontend-dev" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to gofrontend-dev+unsubscribe@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/gofrontend-dev/2FICMX0ORZ6O1.3DYXRTDHNGXCN%408pit.net.

  parent reply	other threads:[~2022-03-06 15:22 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-02 16:37 soeren
2022-02-20 10:43 ` Sören Tempel
2022-02-21 17:25   ` [gofrontend-dev] " Ian Lance Taylor
2022-03-06 12:49     ` Sören Tempel
2022-03-06 15:22     ` Rich Felker [this message]
2022-03-06 16:59       ` Rich Felker
2022-02-20 11:01 ` Andreas Schwab
2022-03-06 18:59 ` [PATCH v2] libgo: Don't use pt_regs member in mcontext_t soeren
2022-03-06 21:21   ` Rich Felker
2022-03-07  7:09     ` [PATCH v3] " soeren
2022-03-07 22:59       ` Ian Lance Taylor
2022-03-08 13:23         ` Rich Felker
2022-03-09  7:26         ` Sören Tempel
2022-03-09 11:52           ` Rich Felker
2022-03-11  7:34             ` [PATCH v4] " soeren
2022-03-31 16:41               ` Sören Tempel
2022-03-31 20:26                 ` Ian Lance Taylor
2022-04-02  8:21                   ` Sören Tempel
2022-04-03  2:02                     ` Ian Lance Taylor
2022-04-03  9:28                       ` Sören Tempel
2022-04-11 17:25                         ` Sören Tempel
2022-04-11 17:35                           ` Ian Lance Taylor
2022-04-11 18:28                             ` Sören Tempel
2022-04-14 22:15                               ` Ian Lance Taylor
2022-04-21  0:50                                 ` Ian Lance Taylor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220306152256.GG7074@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gofrontend-dev@googlegroups.com \
    --cc=iant@golang.org \
    --cc=soeren@soeren-tempel.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).