# Stage the FreeBSD 3.5.1 Runtime Libraries

After FreeBSD's modern 32-bit base support is installed, the ApplixWare
launcher reaches its first unresolved library:

```text
ld-elf32.so.1: Shared object "libX11.so.6" not found, required by "applix"
```

ApplixWare was built for FreeBSD 3.x. This stage obtains its old X11, math,
and C libraries from the final FreeBSD 3.x release rather than substituting
modern libraries with incompatible ABIs.

## 1. Create an Applix-specific library directory

Run from the FreeBSD host:

```sh
jexec applix151 mkdir -p \
  /usr/local/lib32/compat \
  /usr/local/lib32/compat/applix
```

`/usr/local/lib32/compat/applix` will be placed first in
`LD_32_LIBRARY_PATH`, giving ApplixWare a controlled view of the legacy
runtime.

## 2. Obtain FreeBSD 3.5.1 `libX11.so.6`

`libX11.so.6` is an X11 client library, not part of the FreeBSD base archive.
The matching FreeBSD 3.5.1 i386 package is
`XFree86-libraries-4.0.tgz`.

Download the package without installing it into the jail's package database:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  archive=$work/freebsd351-packages

  mkdir -p "$archive"
  fetch -o "$archive/XFree86-libraries-4.0.tgz" \
    https://archive.freebsd.org/old-releases/i386/3.5.1-RELEASE/packages/All/XFree86-libraries-4.0.tgz
'
```

The package prefix is `/usr/X11R6`, and its archive members begin with
`lib/`. List and extract only the `libX11` link and versioned file:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  package=$work/freebsd351-packages/XFree86-libraries-4.0.tgz
  applix=/usr/local/lib32/compat/applix

  mkdir -p "$applix"
  tar -tzvf "$package" "lib/libX11.so*"
  tar -xzvf "$package" -C "$applix" --strip-components 1 \
    "lib/libX11.so*"
'
```

`--strip-components 1` removes the archive's leading `lib/`, leaving the
libraries directly under `/usr/local/lib32/compat/applix`.

Inspect the extracted library:

```sh
jexec applix151 readelf -d \
  /usr/local/lib32/compat/applix/libX11.so.6
```

It has SONAME `libX11.so.6` and no `DT_NEEDED` entries. Unlike modern
`libX11`, it does not add dependencies on `libxcb`, `libXau`, or `libXdmcp`.
It can still communicate with TigerVNC through the stable X11 wire protocol.

## 3. Register and test `libX11`

Update the 32-bit loader cache. Plain `ldconfig` updates the native 64-bit
cache; `-32` selects the cache used by ApplixWare:

```sh
jexec applix151 ldconfig -32 \
  /usr/lib32 \
  /usr/local/lib32/compat \
  /usr/local/lib32/compat/applix \
  /applixware1999cdrom/axdata/axshlib
```

Verify that the cache contains the library:

```sh
jexec applix151 ldconfig -32 -r | grep 'libX11\.so\.6'
```

Run the launcher again:

```sh
jexec applix151 /applixware1999cdrom/applix
ld-elf32.so.1: Shared object "libm.so.2" not found, required by "applix"
```

The changed error proves that `libX11.so.6` now resolves. The loader has moved
to an old FreeBSD base-library dependency.

## 4. Identify the remaining direct dependencies

Use the Applix-specific search path when inspecting the launcher:

```sh
jexec applix151 sh -c '
  env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    ldd /applixware1999cdrom/applix
'
```

The unresolved direct libraries are:

```text
libm.so.2 => not found
libc.so.3 => not found
```

These SONAMEs belong to the old FreeBSD 3.x math and C library ABIs. Both are
available in the FreeBSD 3.5.1 i386 base distribution:

```text
https://archive.freebsd.org/old-releases/i386/3.5.1-RELEASE/bin/
```

The required archive members are `usr/lib/libm.so.2` and
`usr/lib/libc.so.3`.

## 5. Download the FreeBSD 3.5.1 base archive

The base distribution is split into `bin.??` chunks. Its complete data range
is `bin.aa` through `bin.dz`, followed by `bin.ea` through `bin.eg`.

```sh
jexec applix151 sh -c '
  set -eu
  pkg install -y curl

  work=/var/tmp/applix-compat-libs
  src=https://archive.freebsd.org/old-releases/i386/3.5.1-RELEASE/bin

  mkdir -p "$work/freebsd351-bin"
  cd "$work/freebsd351-bin"

  curl -fL --retry 3 -O "$src/CHECKSUM.MD5"
  curl -fL --retry 3 -O "$src/bin.[a-d][a-z]"
  curl -fL --retry 3 -O "$src/bin.e[a-g]"
'
```

## 6. Verify every downloaded chunk

Do not concatenate or extract the archive until all chunk checksums match:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  cd "$work/freebsd351-bin"

  awk "/^MD5 \\(bin\\./ { gsub(/[()]/, \"\", \\$2); print \\$2, \\$4 }" CHECKSUM.MD5 |
  while read -r file expected; do
    actual=$(md5 -q "$file")
    [ "$actual" = "$expected" ] || {
      echo "checksum mismatch for $file: $actual != $expected" >&2
      exit 1
    }
  done
'
```

No output means every file listed in `CHECKSUM.MD5` matched.

## 7. Extract `libm.so.2` and `libc.so.3`

First confirm that the reconstructed archive contains the expected members:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  cd "$work/freebsd351-bin"

  cat bin.* | tar -tzf - "usr/lib/lib[mc].so*"
'
```

Then extract only those libraries:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  applix=/usr/local/lib32/compat/applix

  mkdir -p "$applix"
  cd "$work/freebsd351-bin"
  cat bin.* | tar -xzvf - -C "$applix" --strip-components 2 \
    "usr/lib/lib[mc].so*"
'
```

`--strip-components 2` removes `usr/lib/`, placing the files directly in the
Applix-specific compatibility directory.

## 8. Verify the complete launcher dependency set

Refresh the cache:

```sh
jexec applix151 ldconfig -32 \
  /usr/lib32 \
  /usr/local/lib32/compat \
  /usr/local/lib32/compat/applix \
  /applixware1999cdrom/axdata/axshlib
```

Run `ldd` with the same search path that will be used for ApplixWare:

```sh
jexec applix151 sh -c '
  env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    ldd /applixware1999cdrom/applix
'
```

The key resolved lines are:

```text
libX11.so.6 => /usr/local/lib32/compat/applix/libX11.so.6
libm.so.2 => /usr/local/lib32/compat/applix/libm.so.2
libc.so.3 => /usr/local/lib32/compat/applix/libc.so.3
```

## 9. Cross the loader boundary

Run ApplixWare with the 32-bit library path:

```sh
jexec applix151 sh -c '
  env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    /applixware1999cdrom/applix
'
```

The loader is now satisfied. Startup reaches old libc code and fails at the
next compatibility boundary:

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

On FreeBSD, signal 12 is `SIGSYS`:

```sh
jexec applix151 awk '$3 == 12' /usr/include/sys/signal.h
```

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

The shell status is `128 + 12 = 140`. A no-preload `truss` identifies the old
interface attempted immediately before the signal:

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

`ERR#78` is `ENOSYS`. FreeBSD 3.5.1 libc attempted its old i386 `sigaction`
system-call interface, which FreeBSD 15.1 no longer implements in that form.
The dynamic-loader work is complete; the next stage must translate this libc
ABI boundary.
