REL_tools 1.7 -- Microsoft REL object tools in portable C
=========================================================

A library and eight programs for the Microsoft REL object format: the
format that MACRO-80, LINK-80 and LIB-80 used on CP/M, and that a
surprising amount of 8080 and Z80 software still exists only in.

    librel      the format: read it, write it, link it
    mz80        an assembler, MACRO-80 3.44 compatible, Z80 and Z180
    pz80        an assembler, PASM 2.00 compatible (the dialect TurboDOS
                was written in), writing REL directly, no RELCVT needed
    mlink       a linker, LINK-80 3.44 compatible
    mlib        a librarian, LIB-80 compatible

and four programs for looking at what you have:

    relmap      every link item in a file, with its bit offset
    relir       what those items mean: modules, segments, symbols
    relcmp      the first item at which two files differ
    relfix      repair MACRO-80 3.44's COMMON-block attribution

The point of the package is to make an emulator unnecessary.  Anywhere
you would have reached for a CP/M emulator and M80.COM or L80.COM, these
run natively instead -- and produce the same bytes.


Why you might want it
---------------------

REL is a bit stream.  Nothing in it is byte-aligned: a two-bit tag, a
sixteen-bit value and a three-bit length all sit wherever the previous
field left off.  That is why so few tools read it, and why the ones that
do tend to be wrong in the corners -- external reference chains, COMMON
block attribution, the relocation base of a partially-evaluated
expression.

This package was written against a real workload, the TurboDOS 1.43
reconstruction: 193 modules, about 130,000 lines of production Z80.
Every one of them is assembled by both mz80 and the original MACRO-80
and the objects compared item by item, the listings line by line, and
the whole system linked from mz80's output and compared with the
manufacturer's shipped image.


What it measures against the originals
--------------------------------------

    objects    identical=191  differs=2   failures=0
    listings   identical=193  differs=0
    link       images IDENTICAL (22272 bytes), without relfix
    mlink      8 of 8 link configurations byte-identical to LINK-80

The two differing objects are LCLUSR and SYSNIT, the two modules
MACRO-80 3.44 gets wrong: a chain head attributed to the wrong COMMON
block, and a select-common item carrying a corrupt block name.  mz80
emits the correct encoding for both.  The end-to-end link is the
evidence that this is a correction and not merely a difference -- it
repairs nothing, and the image still comes out identical.

Ten places were found where MACRO-80 3.44 contradicts its own manual.
They are listed, with the measurement behind each, in mz80/SPEC.md and
in the manual.


What changed in 1.7
-------------------

A documentation release.  One example in the mz80 chapter of the manual
no longer names a particular vendor's boards.  The programs, their
output and their tests are unchanged from 1.6.


What changed in 1.6
-------------------

A hardening release, from a testing campaign whose plan and log are in
TESTPLAN.md.  Nothing changes in what the tools produce for legal input:
the 193-module corpus still assembles to the same objects, the listings
still match, and the S6SNGL system still links byte for byte.  What
changes is what they refuse.

mz80 accepts only legal input.  An instruction is accepted only if the
Zilog tables have an encoding for it, and a statement only if nothing but
a comment follows its operands.  1.3 assembled JP HL as JP 0000H, ADD
HL,IX as ADD HL,HL, LD B,(5) as LD B,5, NOP 5 as NOP, SUB A,B as SUB B
and PUBLIC A B as PUBLIC A, all in silence: 55,004 of the 92,876 lines of
a grid of every mnemonic against every kind of operand.  Every line of
that grid is now flagged, and every one of the 1,346 legal forms in the
matching grid assembles to the bytes an independent model of the
instruction set gives.  The grids come from tests/grid.c, which ships;
MACRO-80 3.44's verdict on every line is recorded in tests/m80quirks.txt
and in mz80/SPEC.md, together with the rules mz80 follows where 3.44 is
lenient (a register name is not a value of zero; PUSH SP is not PUSH AF;
a displacement does not wrap).  147 curated cases the assembler must
refuse and 26 it must accept, to the byte, are in mz80/tests.  Also
fixed along the way: .RADIX read its argument in the current radix; a
macro with a LOCAL called twice freed its parameter list twice; a macro
that called itself expanded until memory ran out; REPT nested thirty
deep did the same; an unterminated string inside a false IF was an error;
a V error, a redefined EQU, a doubly defined label and an eight-letter
PUBLIC name were reported on the silent pass and so never seen.

Every program that reads a file reads it through librel's new
rel_slurp(), which refuses a directory (it used to allocate LONG_MAX
bytes and say "Cannot allocate memory") and anything over 64 MB.  An
empty file is no longer an object file: relmap, relir, relfix, mlink,
disasm and serial all took one in silence, and mlink linked it.  The
loader rejects a COMMON-relative entry point, start address or word in a
module that selected no COMMON block, which gen and mlink dereferenced as
a null block.  A module that fails to load is no longer leaked.  mlib
reads every input before it creates its output, so a refused run leaves
nothing behind.  serial refuses -s x/y and -D X=ZZ instead of stamping
zeros.  disasm no longer writes past its arrays for an entry point or an
operand beyond the segment.  These came from 1,831 bit-exact truncations,
20,000 mutations of real objects and 20,000 of real sources, 139
hand-built hostile files and 133 pathological sources, all under the
sanitizers (TESTPLAN.md, tier T8).

Two new suites run from "make test": tests/cli.sh, the command-line
contract of every program -- usage, unknown options, missing, empty and
directory inputs, unwritable outputs, determinism, and the man page's
option list against getopt's -- and tests/downstream.sh, every object the
assembler's tests produce through relmap, relir, mlib and mlink.
IDEAS.md lists what was wanted during the campaign and is not there.

Pairs with turbotools 1.3.


What changed in 1.5
-------------------

A bug-fix release.  All four came out of building a new TurboDOS system,
tdosbox, with these tools: a workload the 193-module corpus does not
cover, because no shipped module does any of these things.

librel, and so mlink and GEN: a word in one COMMON block that names a
label in another was deposited into the OTHER block, with no error.
LINK-80 keeps two things apart that the loader had kept in one
variable: the block a COMMON-relative word is relative to, which
select-common changes, and the block bytes are deposited into, which
moves only on set-location.  MACRO-80 relies on the distinction -- it
selects the other block for the word and goes straight on depositing
in this one.  mlink/tests/run.sh has the case; every TurboDOS image
relinks byte-identically.

mz80: an undefined name in an instruction operand is now the U error
that MACRO-80 reports.  1.2 checked only DB and DW; IN, OUT, LD A,n,
LD HL,nn, JR and (IX+d) assembled a missing name as zero and said
nothing, and JR assembled a jump to 0000H.  The listing's symbol table
had marked the name U all along, which is how it was found.

mz80: .PHASE inside a named COMMON block put every phased byte at
offset 0 of the block; the location items read the wrong counter.

mz80: after a word naming a label in another COMMON block, 1.2
reselected the current block and re-set the location, two items 3.44
does not emit.  It no longer does, and the object is item-for-item
MACRO-80's.  Both of these are in mz80/tests with 3.44's own output as
the golden.

The Makefiles: every object now depends on a stamp named for the
compiler's target, so a native make in a tree last built for another
CPU rebuilds everything instead of mixing two architectures in one
archive.  That is how the first bug was nearly missed.


What changed in 1.3
-------------------

mz80 grows -I, an INCLUDE search path:

    mz80 -I ../inc -o FILMGR.rel os/FILMGR.Z80

The name as written is still tried first, in all three spellings, so an
assembly with no -I behaves exactly as it did in 1.2.  CP/M had no
directories, so MACRO-80 had no search path and needed none; this exists
for a source tree that keeps its include files in one directory and its
modules in several.  Nothing else changed.


Building
--------

    make ; sudo make install

No configure script.  Builds with gcc 13 on Linux and clang 19 on
FreeBSD, -std=c2x, warning-free.  PREFIX, DESTDIR, CC and CFLAGS are
honoured.  It is also built and tested on 64-bit RISC-V -- see Platforms
below.  Other targets:

    make test           the full suite
    make sanitize-test  the suite under ASan and UBSan
    make manual         REL_tools_manual.pdf (needs pdflatex)
    make dist           the release tarball

The comparisons against MACRO-80 and LINK-80 need the originals and a
CP/M emulator, and are skipped with a note when they are not present.
Everything else runs anywhere.


Platforms
---------

Built and tested on three architectures:

    x86-64 Linux     gcc 13, glibc
    x86-64 FreeBSD   clang 19, LLD
    riscv64 Linux    gcc 13, cross-compiled and run under qemu-user, and
                     again with a native riscv64 gcc in a container

The suite passes on all three.  The point of the third is that RISC-V
makes plain char UNSIGNED, which x86 does not on either operating
system: a byte held in a bare char and tested against a negative
sentinel behaves differently there, and a bit-stream codec is exactly
the kind of code that gets that wrong.  It does not, and the evidence is
stronger than a passing suite -- the 199 objects of the TurboDOS
reconstruction assemble to the same bytes on riscv64 as on x86-64, and
the system linked from them is identical, all 22,272 of them.

AddressSanitizer and valgrind are not run on the RISC-V side; neither
works usefully under emulation.  UndefinedBehaviorSanitizer does, and
the suite and the whole 193-module corpus are clean under it there.

For TurboDOS specifically -- linking a system with GEN, disassembling its
modules, stamping distribution serials -- see the companion package
turbotools, which builds against this one.


Documentation
-------------

    REL_tools_manual.pdf    the manual, in six parts: the object format,
                            librel, mz80, mlink, mlib, the utilities
    man pages               librel(3), mz80(1), mlink(1), mlib(1),
                            relmap(1), relir(1), relcmp(1), relfix(1)
    mz80/SPEC.md            the assembler's specification, including
                            every measured divergence from the manual

The manual's first part documents the REL format itself -- the bit
stream, all sixteen link items, the segment model, external chains and
library format.  It is written to be read on its own by anyone who has
to deal with the format, whether or not they use this code.


Licence
-------

Two-clause BSD.  Copyright 2026 Robert Cousins.

Robert Cousins <rec@rcousins.com>
https://rcousins.com/pub/
