# Resolving the 32-bit Runtime Libraries

Stage 02 has produced the preload library:

```sh
cc -m32 -shared -fPIC -Wall -Wextra \
  -o /usr/local/lib/libc3_shim.so /root/libc3_shim_02.c
```

Running Applix with only the preload fails:

```sh
# LD_32_PRELOAD=/usr/local/lib/libc3_shim.so \
    /applixware1999cdrom/applix
ld-elf32.so.1: Shared object "libshelf.so" not found, required by "axmain"
/applixware1999cdrom/axdata/axmain: Operation not permitted
```

This is a loader-search failure, not a shim failure. `LD_32_PRELOAD` tells the
32-bit loader which extra object to load; it does not tell the loader where to
find ApplixWare's own shared libraries.

## 1. Add the ApplixWare library directory

The loader reports only the library's name, not its location. Search the
ApplixWare tree for that exact filename:

```sh
find /applixware1999cdrom -type f -name libshelf.so -print
```

```text
/applixware1999cdrom/axdata/axshlib/libshelf.so
```

This proves that `libshelf.so` exists and identifies its directory:

```text
/applixware1999cdrom/axdata/axshlib
```

Put that directory after the old FreeBSD compatibility directory in
`LD_32_LIBRARY_PATH`:

```sh
# LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/applixware1999cdrom/axdata/axshlib \
    LD_32_PRELOAD=/usr/local/lib/libc3_shim.so \
    /applixware1999cdrom/applix
ld-elf32.so.1: Shared object "libg++.so.4" not found, required by "axmain"
/applixware1999cdrom/axdata/axmain: Operation not permitted
```

The change worked: `libshelf.so` is no longer missing. The loader reached the
next unresolved dependency, `libg++.so.4`.

## 2. Use `readelf` to identify the dependencies

Do not infer library names from the order of the error messages. Read the
binary's dynamic section:

```sh
readelf -d /applixware1999cdrom/axdata/axmain | grep NEEDED
```

The relevant entries are:

```text
Shared library: [libshelf.so]
Shared library: [libX11.so.6]
Shared library: [libg++.so.4]
Shared library: [libstdc++.so.2]
Shared library: [libm.so.2]
Shared library: [libc.so.3]
```

Each `NEEDED` entry is a direct dependency of `axmain`. The loader searches
`LD_32_LIBRARY_PATH` and its 32-bit hints for each name. It stops startup when
one cannot be found, so fixing one library exposes the next missing one.

## 3. Identify the old C++ libraries

After installing `libg++.so.4`, run the same Applix command again:

```sh
# LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/applixware1999cdrom/axdata/axshlib \
    LD_32_PRELOAD=/usr/local/lib/libc3_shim.so \
    /applixware1999cdrom/applix
ld-elf32.so.1: Shared object "libstdc++.so.2" not found, required by "axmain"
/applixware1999cdrom/axdata/axmain: Operation not permitted
```

This is the next `NEEDED` entry shown by `readelf`. After installing
`libstdc++.so.2`, the loader proceeds into the libraries used by
`libshelf.so`.

## 4. Follow the transitive dependency

The next run reaches a library loaded by `axmain`:

```sh
# LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/applixware1999cdrom/axdata/axshlib \
    LD_32_PRELOAD=/usr/local/lib/libc3_shim.so \
    /applixware1999cdrom/applix
ld-elf32.so.1: Shared object "libgnuregex.so.2" not found, required by "libfontfs.so"
/applixware1999cdrom/axdata/axmain: Operation not permitted
```

This time the message names `libfontfs.so` as the consumer. Confirm it rather
than searching `axmain` again:

```sh
readelf -d /applixware1999cdrom/axdata/axshlib/libfontfs.so |
  grep NEEDED
```

The result includes:

```text
Shared library: [libgnuregex.so.2]
```

The three missing compatibility libraries are now known:

```text
libg++.so.4
libstdc++.so.2
libgnuregex.so.2
```

The dependency path is therefore:

```text
axmain -> libshelf.so
       -> libg++.so.4
       -> libstdc++.so.2
       -> ... -> libfontfs.so -> libgnuregex.so.2
```

## 5. Extract the libraries from FreeBSD 3.5.1

Use the FreeBSD 3.5.1 `bin.*` distribution files already downloaded under
`/var/tmp/applix-compat-libs/freebsd351-bin`. Extract the libraries directly
into the directory at the front of `LD_32_LIBRARY_PATH`:

```sh
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 \
  "*libg++.so*" "*libstdc++.so*" "*libgnuregex.so*"
```

`--strip-components 2` removes the leading `usr/lib/`, placing the files directly in:

```text
/usr/local/lib32/compat/applix
```

These must be the 32-bit FreeBSD 3.5.1 libraries. Do not substitute modern
libraries merely because their names are similar.

## 6. Verify and run

Before launching, ask the 32-bit loader to list the resolved dependency tree:

```sh
env \
  LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/applixware1999cdrom/axdata/axshlib \
  LD_32_PRELOAD=/usr/local/lib/libc3_shim.so \
  ldd /applixware1999cdrom/axdata/axmain
```

There should be no `not found` lines. Then run Applix with the same two loader
variables:

```sh
LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/applixware1999cdrom/axdata/axshlib \
LD_32_PRELOAD=/usr/local/lib/libc3_shim.so \
/applixware1999cdrom/applix
```

Applix now runs.
