# Build and Preload the First Shim

The old runtime libraries now load, but FreeBSD 3.5.1 libc calls an obsolete
`sigaction` interface:

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

The first shim exports a replacement `sigaction()` that translates the old
FreeBSD 3.x structure and delegates the operation to modern 32-bit libc7.
`LD_32_PRELOAD` makes the runtime loader place that replacement before
`libc.so.3` during symbol lookup.

## 1. What `LD_PRELOAD` does

A dynamically linked executable does not contain all of the library code it
uses. Its ELF dynamic section names required shared objects with `DT_NEEDED`
entries. At startup, the runtime loader:

1. maps the executable into memory;
2. loads its required shared objects;
3. connects undefined dynamic symbols, such as `sigaction`, to definitions in
   the loaded objects; and
4. applies the relocations needed to call those definitions.

Normally, the loader searches the executable and its ordinary dependencies in
its standard global symbol order. `LD_PRELOAD` asks it to load an additional
shared object before those ordinary dependencies. A definition in the
preloaded object can therefore be selected before a definition with the same
name in libc. This behavior is called **symbol interposition**.

For example, without a preload the lookup is effectively:

```text
Applix reference to sigaction
              |
              v
FreeBSD 3.5.1 libc.so.3: sigaction
              |
              v
obsolete kernel interface -> ENOSYS -> SIGSYS
```

With the shim preloaded, the first matching dynamic definition changes:

```text
Applix reference to sigaction
              |
              v
preloaded libc3_shim.so: sigaction   <- selected first
              |
              v
translate the old structure
              |
              v
modern 32-bit libc.so.7: sigaction
```

The shim obtains libc7's implementation with `dlopen("libc.so.7")` and
`dlsym()`. Calling `sigaction` by name from inside the shim would risk resolving
back to the shim's own interposed function and recursing. Looking up the symbol
through the explicit libc7 handle selects the intended implementation.

Preloading affects dynamically resolved symbols only. It does not rewrite the
executable, replace direct system calls, or automatically make incompatible
data structures compatible. The shim must export the expected name and
perform the required ABI translation itself.

## 2. Why this shim uses `LD_32_PRELOAD`

The problem is not that ApplixWare lacks a function named `sigaction`.
FreeBSD 3.5.1 `libc.so.3` supplies it, but that implementation reaches an old
i386 system-call interface that FreeBSD 15 no longer implements. Replacing all
of libc3 would change a much larger ABI surface. Preloading lets us replace
only the demonstrated incompatibility while leaving ApplixWare's remaining
libc3 calls alone.

FreeBSD amd64 distinguishes native and 32-bit loader variables:

```text
LD_PRELOAD       native 64-bit processes
LD_32_PRELOAD    32-bit processes
```

ApplixWare is a 32-bit i386 executable, so plain `LD_PRELOAD` is the wrong
variable. `LD_32_PRELOAD` names one or more 32-bit shared objects that
`ld-elf32.so.1` must load before the executable's normal dependencies.

Preloading does not remove `libc.so.3`; it changes which definition satisfies
the selected dynamic symbol. This allows one incompatible function to be
translated while the rest of ApplixWare continues using its original libc3
ABI.

`LD_32_PRELOAD` also does not add library search directories. Continue using
`LD_32_LIBRARY_PATH` to locate libc3 and the ApplixWare libraries.

## 3. Put the source inside the jail

Copy `libc3-shim-01.c` into the jail as:

```text
/root/libc3-shim-01.c
```

Then enter the jail:

```sh
jexec applix151 sh
```

The remaining commands in this document run inside that shell.

## 4. Compile a 32-bit shared object

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

The compiler options mean:

- `-m32`: generate i386 code that a 32-bit process can load.
- `-shared`: produce a shared object rather than an executable.
- `-fPIC`: generate position-independent code suitable for runtime loading.
- `-Wall -Wextra`: enable useful compiler diagnostics.
- `-o`: select the installed preload filename.

Compilation must finish without errors. A 64-bit shim cannot be loaded into
the 32-bit ApplixWare process.

## 5. Verify the shim before loading it

Check its executable format:

```sh
file /usr/local/lib/libc3_shim.so
```

The important fields are:

```text
ELF 32-bit LSB shared object, Intel i386
```

Confirm that its dynamic symbol table exports the replacement:

```sh
nm -D /usr/local/lib/libc3_shim.so | grep ' T sigaction$'
```

Expected symbol type:

```text
T sigaction
```

`T` means the shared object globally defines executable code named
`sigaction`. The address printed by `nm` is build-dependent.

## 6. Preload the shim into ApplixWare

Use both 32-bit loader variables in the same invocation:

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

The first variable locates the old runtime and ApplixWare libraries. The
second loads the shim early enough for its `sigaction` definition to
interpose on libc3's definition.

Do not set `LD_32_PRELOAD` globally. Limit it to ApplixWare commands because
the shim implements an application-specific translation for the old libc3
ABI.

## 7. Interpret the next result

If `Bad system call` disappears, the process crossed the stage-1 signal ABI
boundary. The next failure is a new debugging stage and should not be treated
as evidence that preloading failed.

To remove the preload for a comparison run:

```sh
unset LD_32_PRELOAD
```
