Files
cc09/docs/reloc-srecords.md
T
vincente adeafa5742 Add POSIX a9/xld/xc9 recreation sources and goldens.
Keep WINTEK/ read-only. Add assembler, linker, compiler driver, hex, runtime asm, reloc S-record docs, tests/golden, Makefile, and a pointer to the Mimic 21-Implementation plan. Ignore host bin/ and object files.
2026-10-18 13:41:26 -05:00

8.5 KiB

Relocatable S-Records Format (Wintek 6809)

This document captures the reverse-engineered object and library format used by the original Wintek A9 assembler and XLD linker. It is the critical "glue" that allows hand-written assembly and C-compiler-generated assembly to be fed into a common linker.

The files (.rel, contents of .lib) are text files using CRLF line endings, consisting of standard Motorola S-record lines with custom extensions via record types S4, S8, SD and augmented payloads on S2/S3/S7.

Standard Motorola S-Record Types Used

  • S0: Header / module name record. Payload after addr (usually 0000) is ASCII module name (e.g., KRCstart).
  • S2: 24-bit address variant data record. In reloc files the 24-bit "addr" field in the record is often a local record/sequence number or unused; real section + placement is in the payload bytes.
  • S3: 32-bit address variant data record (used for some named contributions or fixed data).
  • S7: 32-bit termination record (per chunk or module). In this format, used as "end of symbol/section contribution".
  • S9: 16-bit termination + optional entry point (normal end of file). S9030000FC is common (no entry or default).

Wintek Extensions (Non-standard types and payloads)

  • S4 records: Control / allocation / section metadata.
    • Examples observed:
      • S4060000000100FFF9 : possibly BSCT/direct-page or memory region setup (01 00 ff).
      • S406000000020014E3
      • S406000000050001F3, S406000000060001F2
    • Appear to declare/allocate space or switch current section context for following data, and carry "type" + size info.
  • S8 records: Symbol definition/export records (the heart of reloc).
    • Format sketch (from samples):
      • S80C00000200002E435354525433 decodes (after type/len/addr16) to roughly: flags/type=02, local-offset=0000, name=".CSTRT"
      • S80D0002020000737072696E7466E8 -> symbol "printf" (and variant "printf" with different flags?).
      • S809FFFF000000454F461E -> special like "EOF" at FFFF (absolute?).
      • S80B0001000000737464696E... -> "stdin"
    • S8 records appear before or with the data that defines the symbol. The 16-bit "addr" field + payload bytes encode the symbol's module-local address within its section and attributes (global, section id, etc.).
    • Used by linker for:
      • Building per-module export tables.
      • Library searches (XLD scans .lib for unresolved externals by scanning S8 name records).
  • SD records: Trailer / summary (e.g. SD0500000208F0).
    • Likely "section directory" or size summary per section type at end of module (02 08 perhaps PSCT size or count).
  • Payload augmentation on data records (S2 especially):
    • Observed payloads start with:
      • 02 <16-bit> <6809-bytes...>
      • 05 ..., 06 ...
    • Interpretation (working hypothesis):
      • First byte after S-rec overhead: section tag (02 = PSCT/code, 05/06 = other (DSCT? ASCT?)).
      • Following 16-bit: offset within that section's contribution for this chunk.
      • Then the raw 6809 object bytes. Where an address field needs relocation (external ref or section-reloc), it is emitted as 0000 (or placeholder) and the linker uses the symbol table (from S8) + fixup kind implied by context or extra bytes to patch the final absolute image.
    • S7 records following a chunk often repeat the section tag + name of the just-emitted symbol/procedure (e.g. S70A0005022E636C72696FA7 ends the chunk for ".clrio").
  • Other notes:
    • Checksums are standard ones-complement (must validate on read, original linker errors on bad checksum).
    • "Out of order loader records" — strict sequencing expected (S0, S4/S8 metadata, S2/S3 data chunks, S7 terminators, SD, S9).
    • S5/S6/SA/SB/SC explicitly not implemented (per XLD strings).
    • Modules can contribute to multiple sections (PSCT for code, DSCT for data, BSCT for "direct" zero-page-ish, ASCT for absolute?).

Sections (from assembler + linker)

  • PSCT : (Program?) code / ROM section. Primary for executable code.
  • DSCT : Data / RAM section (variables, stack in CSTART goes here).
  • BSCT : "Base page" / direct-page section (6809 direct addressing, usually constrained to 00-FF). Special handling and warnings if not in range.
  • ASCT : Absolute section? (used with ORG in examples).
  • Named sections and COMMON blocks also supported.
  • In linker directives: SECTION name TYPE @ start-end or ranges like 0000-DFFF, SECTION * BSCT @ 0-FF.
  • Linker builds allocation map, places modules' contributions, detects overlaps, unsatisfied refs, etc.

How A9 Produces These

  • Assembler is two-pass.
  • Pass 1: collect labels (with section context), evaluate constant expr, size instructions (esp. branch sizing, indexed postbytes).
  • Pass 2: emit bytes per section, tracking current section and "chunk" starts for each GLOBAL symbol.
  • For every GLOBAL symbol emitted, produce corresponding S8 record describing its section + offset.
  • Data bytes for instructions/data (FCB, FDB, FCC, RMB holes?) wrapped in S2 (with the section+offset prefix bytes) or S3.
  • At symbol/procedure boundaries or end of section contrib use S7.
  • At end: SD + S9 (entry point from END directive if given).
  • Relocatable expressions: only certain ops allowed on reloc terms (add abs const, subtract same-section, etc.). Errors: "relocatable expression illegal here", "illegal operation for relocatable expression", "illegal operation involving externals".
  • External refs become 0 placeholders + recorded for linker fixup (probably using the symbol name emitted via other means or inferred).
  • Reads all input .rel (user) + those found via LIBRARY in .dir or -l.
  • Parses S0 for module name, S8 for its defined symbols (with their local offsets), S4 for its section requirements, S2/S3 for raw bytes + their section/offset tags.
  • Uses directives (or built-in defaults) to set up memory regions, reserve areas, place named sections.
  • Allocates "block descriptors", places each module's section contributions into free space in the correct region (first-fit?).
  • For each external ref (detected during data load or separate symbol records), resolves against global symbol table (user modules first, then libs in order).
  • Applies relocations: patches the 8/16-bit fields in the allocated images (6809 specifics: 5-bit/8-bit offsets, 16-bit, PC-relative for branches/LBRA/LBSR, DP vs extended, etc.).
  • Emits final absolute image as normal S1 (or S2) + S9 records (the .mik file or -o absfile).
  • Also produces optional map file (symbols per module/section, sizes ASCT/BSCT/DSCT/PSCT, unsatisfied list, memory summary, entry point).
  • -nh : no header (suppress banners?).
  • -v : verbose (shows directive file, user files, libs, allocation?).

Library Format (.lib)

Simply the concatenation of multiple complete relocatable object modules (each with own S0 ... S9 sequence). Linker searches sequentially for needed symbols by scanning S8 records.

Sample Records (from KRCSTART.REL + libs)

See hexdumps in source control or run python parsers over WINTEK/KRCSTART.REL and WINTEK/*LIB9.LIB.

Key observed symbols in runtime libs: printf (and variants), getchar, putchar, putc, getc, cleario, .clrio, exit, main, stdin, stdout, EOF, plus dozens of compiler-support symbols: .LMUL, .LDIV, .LADD, .LSUB, .CTOD, .DTOI, .DADD, .FMUL, switch helpers .SWTCH, struct copy helpers, etc.

Recommendations for Recreation

  • Implement a srec_reloc.c/h that can:
    • Read a stream of records, classify, extract module/symbol/section info into structs (module, symbol_table, section_contrib list with (section_id, offset, raw_bytes, fixup_notes)).
    • Write records in canonical order with correct checksums, using CRLF.
    • Provide both "writer for assembler" and "reader for linker".
  • Define clear section enum + contrib structs early.
  • For the assembler emitter: maintain current_section, current_chunk_offset, per-symbol start offsets. When emitting bytes for a labeled proc, start a new S2 chunk with the magic prefix, emit bytes (leave 00 for addresses that are reloc or extern), and on symbol end or section change emit the S7 closer.
  • Linker side will be responsible for most fixup math once full images per section are assembled in memory buffers.
  • Keep checksum validation + "out of order" detection to match original errors.

This format is what makes "Assembly and C ... fed into the common linker" possible without separate object formats.

Further details will be added from more reverse engineering (running original under dosbox on crafted cases that exercise sections, macros, complex relocs, multiple modules, etc.).