```text
get user name: No such file or directory
return code=1, signal=0
```

# From `get user name` to the Stage-02 Shim

This investigation starts with only the launcher, the stage-01 signal shim,
and the error above.  It does not assume that `getpwuid()` is the cause.
Commands run inside the `applix151` jail unless stated otherwise.

## 1. Reproduce, but do not over-read, the message

```sh
export LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib
export LD_32_PRELOAD=/usr/local/lib/libc3-shim-01.so
export AXHOME=/applixware1999cdrom
export AXGFSDIR=/applixware1999cdrom/axdata/fontmetrics/gallium
export AXLANG=english_us AX_TEMP_DIR=/tmp DISPLAY=:1

timeout 10 /applixware1999cdrom/applix -wp
rc=$?
printf 'return code=%s, signal=%s\n' \
  "$rc" "$((rc >= 128 ? rc - 128 : 0))"
```

A fresh run produced:

```text
get user name: Invalid argument
return code=1, signal=0
```

The changed suffix is the first clue that neither message names a missing
file.  Status 1 with no signal says that Applix printed an error and exited
normally.  The stable part is `get user name`; the suffix depends on `errno`:

```sh
egrep '^#define[[:space:]]+(ENOENT|EINVAL)' /usr/include/sys/errno.h
```

```text
#define ENOENT          2       /* No such file or directory */
#define EINVAL          22      /* Invalid argument */
```

## 2. Find where `get user name` comes from

Search the launcher rather than guessing from the English message:

```sh
strings -a -t x /applixware1999cdrom/applix | grep 'get user name'
```

```text
  10693 get user name
```

At this point there is no reason to search for either `getpwuid` or `perror`.
The only new fact is that the string starts at file offset `0x10693`.  Map that
offset into the executable's virtual address space.  The `.data` section is:

```sh
readelf -S /applixware1999cdrom/applix | grep ' \.data '
```

```text
[12] .data PROGBITS 08058c24 00fc24 001438 00 WA 0 0 4
```

The relevant `readelf` columns are:

- `.data`: the section name.  This section contains writable initialized data,
  including this string.
- `PROGBITS`: the section occupies actual bytes in the executable file.  This
  matters because a `NOBITS` section such as `.bss` has memory space but no
  corresponding file bytes to translate.
- `08058c24` (`Address`): the virtual address assigned to the first byte of
  `.data` when the executable is loaded.
- `00fc24` (`Off`): the file offset of that same first byte, measured from the
  beginning of the executable.
- `001438` (`Size`): `.data` occupies `0x1438` bytes in both coordinate systems.

First prove that the string belongs to this section.  `.data` covers file
offsets from `0xfc24` through, but not including:

```text
0xfc24 + 0x1438 = 0x1105c
```

The string offset reported by `strings -t x`, `0x10693`, satisfies:

```text
0xfc24 <= 0x10693 < 0x1105c
```

The file offset and virtual address are different coordinate systems.  Convert
between them in two steps:

1. Subtract `.data`'s starting file offset to find how far the string lies from
   the first byte of the section:

   ```text
   0x10693 - 0xfc24 = 0xa6f
   ```

   `0xfc24` is subtracted from `0x10693` specifically because `strings -t x`
   reports an absolute file offset, while the required intermediate value is a
   section-relative displacement.

2. Add that same displacement to `.data`'s starting virtual address:

   ```text
   0x8058c24 + 0xa6f = 0x8059693
   ```

   The relative position is preserved when a `PROGBITS` section is mapped from
   the file into memory: a byte `0xa6f` bytes from the section's file start is
   also `0xa6f` bytes from its memory start.

Combined into one expression, the translation is:

```text
runtime address = section virtual address
                + (string file offset - section file offset)

0x8058c24 + (0x10693 - 0xfc24) = 0x8059693
```

The subtraction removes the file-coordinate origin (`0xfc24`); the addition
applies the memory-coordinate origin (`0x8058c24`).  The arithmetic can be
checked directly in the jail shell:

```sh
printf 'offset within .data = 0x%x\n' "$((0x10693 - 0xfc24))"
printf 'runtime address     = 0x%x\n' \
  "$((0x8058c24 + (0x10693 - 0xfc24)))"
```

```text
offset within .data = 0xa6f
runtime address     = 0x8059693
```

Now search the disassembly for that address, without supplying any function
name:

```sh
# objdump -dr /applixware1999cdrom/applix |
  grep -B 12 -A 4 '8059693' | egrep -v 'addl|movl'
 804c7d2: e8 81 ec ff ff                calll   0x804b458 <strcpy@plt>
 804c7da: eb 50                         jmp     0x804c82c <FindAxMain+0x98>
 804c7dc: e8 27 ed ff ff                calll   0x804b508 <getuid@plt>
 804c7e3: 50                            pushl   %eax
 804c7e4: e8 bf ef ff ff                calll   0x804b7a8 <getpwuid@plt>
 804c7f4: 83 bd 70 cc ff ff 00          cmpl    $0x0, -0x3390(%ebp)
 804c7fb: 75 17                         jne     0x804c814 <FindAxMain+0x80>
 804c7fd: 68 93 96 05 08                pushl   $0x8059693              # imm = 0x8059693
 804c802: e8 d1 ec ff ff                calll   0x804b4d8 <perror@plt>
 804c80a: 6a 01                         pushl   $0x1
 804c80c: e8 03 18 00 00                calll   0x804e014 <ThimSafeExit>
```

The relevant lines are:

```text
804c7dc: e8 27 ed ff ff    calll 0x804b508 <getuid@plt>
804c7e4: e8 bf ef ff ff    calll 0x804b7a8 <getpwuid@plt>
804c7f4: 83 bd 70 cc ff ff 00 cmpl  $0x0, -0x3390(%ebp)
804c7fb: 75 17             jne   0x804c814 <FindAxMain+0x80>
804c7fd: 68 93 96 05 08    pushl $0x8059693
804c802: e8 d1 ec ff ff    calll 0x804b4d8 <perror@plt>
804c80a: 6a 01             pushl $0x1
804c80c: e8 03 18 00 00    calll 0x804e014 <ThimSafeExit>
```

This is where the names come from.  `objdump -dr` uses the executable's dynamic
relocation and symbol information to label the nearby PLT calls.  The code
loads the current UID, calls the newly discovered `getpwuid@plt`, tests its
return value, pushes the address of `"get user name"`, and calls the newly
discovered `perror@plt`.

Only after discovering those names in the string's code reference should
`nm -D` be used to confirm that they are unresolved imports to be supplied by
a loaded library:

```sh
nm -D /applixware1999cdrom/applix |
  egrep ' U (getpwuid|perror)$'
```

```text
         U getpwuid
         U perror
```

`U` means that the launcher uses the symbol but does not define it.  It does
not, by itself, prove the call order; the disassembly above does that.

This gives the static control flow:

```text
uid = getuid()
pw = getpwuid(uid)
if (pw == NULL) {
    perror("get user name")
    exit(1)
}
```

The `perror(3)` contract explains the displayed line: it prepends its argument,
adds `": "`, and appends the message for the current `errno`.  Thus
`get user name` is an error label compiled into Applix.  `No such file or
directory` is `strerror(2)` for `ENOENT`; another run can append
`Invalid argument` for `EINVAL` without changing the failing branch.

## 3. Derive the DTrace probe

A first attempt can trace every symbolized userspace function without naming a
suspect.  The two pid-provider forms are:

```d
pid$target:::entry   /* every function entry */
pid$target:::return  /* every function return */
```

At entry, `arg0` through `arg5` are raw candidate argument slots.  At return,
`arg1` is the raw return value.  `probemod` and `probefunc` report the library
and function selected at runtime.  Those facts lead to a generic script such
as:

```d
#pragma D option quiet

pid$target:::entry
{
    printf("E %s`%s a0=%#lx a1=%#lx\n",
        probemod, probefunc, (uintptr_t)arg0, (uintptr_t)arg1);
}

pid$target:::return
{
    printf("R %s`%s result=%#lx\n",
        probemod, probefunc, (uintptr_t)arg1);
}
```

On this launcher, tracing everything is too noisy: the service-file scan
generated tens of thousands of libc calls.  The measured attempt reported a
16 MiB buffer limit, 2,641 dropped records, and 29,346 captured events before
the useful exit sequence.  The static cross-reference above gives a reliable
way to narrow the probe without pretending that the broad trace succeeded.

The focused script follows directly from the discovered C interfaces:

- `getpwuid(uid_t)` needs its integer `arg0` and pointer return value.
- `dbopen(const char *, ...)` reveals the database path through `arg0`.
- `perror(const char *)` reveals the prefix through `arg0`.
- `strerror(int)` reveals the numeric error through `arg0`.
- `copyinstr()` converts a target-process string pointer into DTrace text.

Create `/root/02-getpwuid.d`:

```sh
cat >/root/02-getpwuid.d <<'DTRACE'
#pragma D option quiet

pid$target::getpwuid:entry
{
    printf("%s`%s(%u)\n", probemod, probefunc, (uint32_t)arg0);
    ustack(6);
}

pid$target::getpwuid:return
{
    printf("%s`%s returned 0x%lx\n",
        probemod, probefunc, (uintptr_t)arg1);
}

pid$target::dbopen:entry
{
    printf("%s`%s(\"%s\")\n",
        probemod, probefunc, copyinstr((uintptr_t)arg0));
}

pid$target::perror:entry
{
    printf("%s`%s(\"%s\")\n",
        probemod, probefunc, copyinstr((uintptr_t)arg0));
}

pid$target::strerror:entry
{
    printf("%s`%s(%d)\n", probemod, probefunc, (int)arg0);
}
DTRACE
```

Run it with the stage-01 environment already exported:

```sh
dtrace -q -x evaltime=postinit \
  -s /root/02-getpwuid.d \
  -c '/applixware1999cdrom/applix -wp'
```

Actual output from the fresh run:

```text
get user name: Invalid argument
libc.so.3`getpwuid(0)
    libc.so.3`getpwuid+0x1
    applix`0x804c7e9
libc.so.3`dbopen("/etc/spwd.db")
libc.so.3`getpwuid returned 0x0
libc.so.3`perror("get user name")
libc.so.3`strerror(22)
```

This runtime trace confirms the disassembly and explains both visible suffixes.
With the original reported run, the last line is `strerror(2)`, which produces
`No such file or directory`; this fresh run retained 22 and produced
`Invalid argument`.  The stable failure is libc3 `getpwuid(0)` returning
`NULL` immediately before `perror("get user name")`.

## 4. Establish the compatibility boundary

Check the same UID through the jail's current account interface and confirm the
database file exists:

```sh
getent passwd 0
ls -l /etc/spwd.db /usr/lib32/libc.so.7
```

```text
root:*:0:0:Charlie &:/root:/bin/sh
-rw------- 1 root wheel   40960 ... /etc/spwd.db
-r--r--r-- 1 root wheel 1920200 ... /usr/lib32/libc.so.7
```

UID 0 and `/etc/spwd.db` both exist, so creating a user or inventing a missing
path would not address the observed call.  What is proven is narrower: the old
FreeBSD 3 libc lookup returns no passwd entry for an existing UID in this
modern jail.  The trace does not prove the precise internal database-format or
key-decoding defect.

The smallest compatibility change is therefore to intercept `getpwuid()` and
delegate it to the modern 32-bit `libc.so.7`.  An explicit `dlopen()`/`dlsym()`
handle avoids resolving the wrapper recursively back to itself.  Do not add
`getpwnam()`: the trace observes only the numeric `getpwuid(0)` lookup, so that
extra interposer is not evidence-driven.

Relative to stage 01, add:

```c
#include <sys/types.h>
#include <errno.h>
#include <pwd.h>

static struct passwd *(*rl_getpwuid)(uid_t);
```

Resolve the modern function after `dlopen("libc.so.7", ...)` succeeds:

```c
rl_getpwuid = dlsym(libc7_h, "getpwuid");
```

Export the interposed name:

```c
struct passwd *
getpwuid(uid_t uid)
{
    if (!libc7_h)
        libc7_init();
    if (rl_getpwuid)
        return rl_getpwuid(uid);
    errno = ENOENT;
    return NULL;
}
```

The complete incremental source is
[`libc3-shim-02.c`](libc3-shim-02.c).

## 5. Rebuild and test the conclusion

Build to a temporary path so the installed shim is not overwritten:

```sh
cc -m32 -shared -fPIC -Wall -Wextra \
  -o /tmp/libc3-shim-02-rebuilt.so \
  /root/libc3-shim-02.c

nm -D /tmp/libc3-shim-02-rebuilt.so |
  egrep ' (getpwuid|sigaction|sigprocmask)$'
```

```text
00001940 T getpwuid
00001610 T sigaction
00001830 T sigprocmask
```

Run the focused probe again with only the preload changed:

```sh
export LD_32_PRELOAD=/tmp/libc3-shim-02-rebuilt.so
dtrace -q -x evaltime=postinit \
  -s /root/02-getpwuid.d \
  -c '/applixware1999cdrom/applix -wp'
```

The relevant measured output is now:

```text
libc3-shim-02-rebuilt.so`getpwuid(0)
    applix`0x804c7e9
libc.so.7`getpwuid(0)
    libc3-shim-02-rebuilt.so`getpwuid+0x47
libc.so.7`getpwuid returned 0x283fe948
libc3-shim-02-rebuilt.so`getpwuid returned 0x283fe948
```

The wrapper receives UID 0, calls libc7, and returns the same non-null passwd
pointer to Applix.  The `perror("get user name")` branch is no longer taken.
A direct launcher run returned `return code=0, signal=0`; later messages about
the read-only CD home or an already-running `axnet` are separate startup stages.

That before/after return value is the validation: stage 01 returned `NULL` from
libc3 and printed the error; stage 02 returned a passwd entry through libc7 and
crossed the account-lookup boundary.
