```text
# run_applix none
Bad system call
return code=140, signal=12
```

# From `Bad system call` to `libc3-shim-01.c`

This investigation begins with the old libraries staged and the launcher able
to load, but with no preload shim.  It does not assume which function caused
the signal.  Commands using `jexec applix151` run from the host; commands
without it run inside the jail unless stated otherwise.

## 1. Decode the process result

Shells normally report a signal death as `128 + signal number`:

```text
140 - 128 = 12
```

Confirm signal 12 from the jail's headers:

```sh
grep '^[[:space:]]*#define[[:space:]]*SIGSYS[[:space:]]' \
  /usr/include/sys/signal.h
```

```text
#define SIGSYS 12 /* non-existent system call invoked */
```

The result establishes only that the kernel delivered `SIGSYS`.  It does not
identify the obsolete system call, so the next step is a system-call trace.

## 2. Find the system call that raises SIGSYS

Run the launcher under `truss` with the same 32-bit library path and no
`LD_32_PRELOAD`:

```sh
env \
  LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
  AXHOME=/applixware1999cdrom \
  AXGFSDIR=/applixware1999cdrom/axdata/fontmetrics/gallium \
  AXLANG=english_us AX_TEMP_DIR=/tmp DISPLAY=:1 \
  truss -f -o /tmp/01-unshimmed.truss \
  /applixware1999cdrom/applix -wp

tail -20 /tmp/01-unshimmed.truss
```

The final records are:

```text
compat.freebsd32_sigaction() ERR#78 'Function not implemented'
SIGNAL 12 (SIGSYS) code=SI_KERNEL
process killed, signal = 12
```

The stable sequence is therefore:

```text
old libc.so.3 sigaction()
    -> obsolete 32-bit compatibility syscall
    -> ENOSYS
    -> SIGSYS
    -> process death
```

grep only for `sigaction`.  That name comes directly from the
last successful diagnostic: `truss` printed
`compat.freebsd32_sigaction()`.  Remove the kernel's `compat.freebsd32_` prefix
and check whether the launcher expects another object to supply the userland
symbol:

```sh
nm -D /applixware1999cdrom/applix | grep ' sigaction$'
         w sigaction
```

The lowercase `w` is a weak undefined reference: the launcher does not define
`sigaction`, so a loaded library normally supplies it and a preload can
interpose it.  `nm` does not independently prove the failure—the preceding
trace does—but it confirms the symbol boundary available to the shim.

## 3. Explain ENOSYS from the kernel source

On the FreeBSD host, the generated 32-bit syscall table identifies the failing
entry:

```text
46 = compat.freebsd32_sigaction
```

The entry is guarded by the old `COMPAT_43` kernel option.  The generated
table defines its `compat()` selector as:

```c
#ifdef COMPAT_43
#define compat(n, name) \
    .sy_narg = n, .sy_call = (sy_call_t *)__CONCAT(o, name)
#else
#define compat(n, name) \
    .sy_narg = 0, .sy_call = (sy_call_t *)nosys
#endif
```

Check the running kernel configuration:

```sh
sysctl -n kern.conftxt | grep COMPAT_43
```

It produces no output: this kernel does not include `COMPAT_43`.  Consequently,
old syscall 46 points to `nosys`, which returns `ENOSYS`.  This explains the
trace without assuming that libc3 itself was defective on FreeBSD 3.x; its
kernel interface is simply no longer enabled.

## 4. Determine the ABI translation

Calling modern libc with the old structure pointer unchanged would be unsafe.
The host's compatibility source shows the old 32-bit action layout used by
syscall 46:

```c
struct osigaction32 {
    uint32_t  sa_u;       /* 32-bit handler pointer */
    osigset_t sa_mask;    /* one 32-bit mask word */
    int       sa_flags;
};
```

It is 12 bytes in the order `handler, mask, flags`.  The current 32-bit layout
is:

```c
struct sigaction32 {
    uint32_t sa_u;        /* 32-bit handler pointer */
    int      sa_flags;
    sigset_t sa_mask;
};
```

Modern `sigset_t` contains four 32-bit words:

```c
#define _SIG_WORDS 4
typedef struct __sigset {
    __uint32_t __bits[_SIG_WORDS];
} __sigset_t;
```

The current structure is therefore 24 bytes in the order
`handler, flags, mask[4]`.  The shim must preserve the old layout at its public
entry point, construct the modern layout before calling libc7, and translate an
output action back to the old layout.

Represent the two layouts explicitly:

```c
struct old_sigaction {
    void *handler;
    unsigned int mask;
    int flags;
};

struct new_sigaction {
    void *handler;
    int flags;
    unsigned int mask[4];
};
```

For an input action, zero the modern structure, copy the handler and flags, and
place the old mask in `mask[0]`.  For an output action, copy those fields in the
opposite direction.

## 5. Test the first hypothesis, then discover `sigprocmask`

Build a temporary preload containing only the libc7 resolver and the
`sigaction()` translation.  PRELOAD the .so and run applix and trace with truss:

```text
freebsd32_sigaction(SIGINT, ...)  = 0
freebsd32_sigaction(SIGUSR1, ...) = 0
compat.sigprocmask()              ERR#78 'Function not implemented'
SIGNAL 12 (SIGSYS) code=SI_KERNEL
process killed, signal = 12
```

This result proves two things:

1. The `sigaction()` translation reaches the working current syscall.
2. Startup then reaches old syscall 48, so stage 01 also needs a
   `sigprocmask()` translation.

Only now is `sigprocmask` a justified search term.  Confirm that the launcher
imports it:

```sh
nm -D /applixware1999cdrom/applix | grep ' sigprocmask$'
```

```text
         U sigprocmask
```

The kernel syscall table independently labels old syscall 48 as
`compat.sigprocmask`.  As with syscall 46, the absent `COMPAT_43` implementation
maps it to `nosys`, matching the measured `ENOSYS` result.

The old mask is one 32-bit word and the modern mask is four.  Preserve the low
word, zero the other three, call modern libc7, and copy the low result word back
when the caller requested it:

```c
int
sigprocmask(int how, const unsigned int *set, unsigned int *oset)
{
    unsigned int m_set[4];
    unsigned int m_oset[4];
    void *sp = NULL;
    void *op = NULL;
    int ret;

    if (!libc7_h)
        libc7_init();

    memset(m_set, 0, sizeof(m_set));
    memset(m_oset, 0, sizeof(m_oset));

    if (set) {
        m_set[0] = *set;
        sp = m_set;
    }
    if (oset)
        op = m_oset;

    ret = rl_sigprocmask ? rl_sigprocmask(how, sp, op) : -1;
    if (ret == 0 && oset && op)
        *oset = m_oset[0];
    return ret;
}
```

## 6. Assemble `libc3-shim-01.c`

The preload must call the modern implementations without resolving recursively
back to its own exported names.  Open `libc.so.7` explicitly and retain its
function pointers.

`signal` was not diagnosed as a failing function and is not a third ABI
wrapper.  It is resolved here only to install `SIG_IGN` for `SIGSYS` as a
bring-up safeguard.  The evidence-driven repairs remain the `sigaction` and
`sigprocmask` wrappers.

The resolver state and initialization are:

```c
static void *libc7_h;
static int (*rl_sigaction)(int, const void *, void *);
static int (*rl_sigprocmask)(int, const void *, void *);
static void (*(*rl_signal)(int, void (*)(int)))(int);

static void
libc7_init(void)
{
    if (libc7_h)
        return;

    libc7_h = dlopen("libc.so.7", RTLD_LAZY | RTLD_LOCAL);
    if (!libc7_h)
        return;

    rl_sigaction = dlsym(libc7_h, "sigaction");
    rl_sigprocmask = dlsym(libc7_h, "sigprocmask");
    rl_signal = dlsym(libc7_h, "signal");

    if (rl_signal)
        rl_signal(12, (void (*)(int))1);
}
```

Ignoring `SIGSYS` during initialization is a bring-up safeguard for any
remaining legacy edge.  It is not the core repair: the two wrappers are what
replace the obsolete calls with working current calls.

The `sigaction()` wrapper performs the structure conversion established above:

```c
int
sigaction(int sig, const struct old_sigaction *act, struct old_sigaction *oact)
{
    struct new_sigaction m_act;
    struct new_sigaction m_oact;
    struct new_sigaction *ap = NULL;
    struct new_sigaction *op = NULL;
    int ret;

    if (!libc7_h)
        libc7_init();

    if (act) {
        memset(&m_act, 0, sizeof(m_act));
        m_act.handler = act->handler;
        m_act.flags = act->flags;
        m_act.mask[0] = act->mask;
        ap = &m_act;
    }
    if (oact)
        op = &m_oact;

    ret = rl_sigaction ? rl_sigaction(sig, ap, op) : -1;
    if (ret == 0 && oact && op) {
        oact->handler = m_oact.handler;
        oact->flags = m_oact.flags;
        oact->mask = m_oact.mask[0];
    }
    return ret;
}
```

The complete source, including headers and constants, is
[`libc3-shim-01.c`](libc3-shim-01.c).

## 7. Build and verify stage 01

```sh
cc -m32 -shared -fPIC -Wall -Wextra \
  -o /usr/local/lib/libc3-shim-01.so \
  /root/libc3-shim-01.c

nm -D /usr/local/lib/libc3-shim-01.so |
  egrep ' (sigaction|sigprocmask)$'
```

```text
00001500 T sigaction
00001700 T sigprocmask
```

Run the launcher with `LD_32_PRELOAD` set to the new library.  The completed
trace now uses current interfaces successfully:

```text
freebsd32_sigaction(SIGSYS, ...) = 0
freebsd32_sigaction(SIGINT, ...) = 0
freebsd32_sigaction(SIGUSR1, ...) = 0
sigprocmask(SIG_BLOCK, ...)       = 0
```

There is no `compat.*` ENOSYS result and no fatal SIGSYS.  The next visible
failure is now an ordinary application error:

```text
get user name: No such file or directory
```

That forward movement validates stage 01: signal ABI translation removes the
kernel-fatal boundary and exposes the next independent startup problem.
