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 <soe...@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 <soe...@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 <soe...@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+unsubscr...@googlegroups.com.
> > To view this discussion on the web visit 
> > https://groups.google.com/d/msgid/gofrontend-dev/2FICMX0ORZ6O1.3DYXRTDHNGXCN%408pit.net.

Reply via email to