Files
arti 0075e57c0d m7.2: add object/debug end-to-end gates
- oracle.sh: link the M5 native objects with GSPLNK and tms-link, require
  identical allocated bytes/public symbols through the independent reader,
  then run the tms-link image in MAME and decode _native_value with dis34.
- cinit.sh: a C TI .cinit record table (packed u16 count/u32 destination/
  data words) linked by tms-link, the termtris copy loop in MAME, cinit at
  the .cinit origin, the destination resolved to a bit address, and
  bytes/placement equal to GSPLNK.
- longname.sh: a 44-character public function defined in one TU and called
  from another, linked by tms-link, full name in the map, runs in MAME.
- debug.sh: tms-clang -g -> TI-COFF -> tms-link --keep-debug, .debug_*
  wrapped in a minimal ELF32 container and validated with llvm-dwarfdump
  (debug.c, line rows, low PC 0x314000, high_pc byte size, string names),
  plus the stripped image loading in MAME.
- tms-clang: pass -g through to clang; the bit-pointer passes preserve !dbg.
- tms-link: accept GSPA's .text/.data/.bss/.cinit order as well as llc's
  .text/.data/.cinit/.bss order (the real startup cinit.asm shape).
2026-11-14 22:21:03 +00:00

225 lines
8.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Wrap linked TI-COFF `.debug_*` sections in a minimal ELF32 container.
The TMS34010 native path is TI-COFF end to end; `llvm-dwarfdump` reads ELF,
Mach-O and Wasm, not the 1987 TI COFF variant. This test-only helper extracts
the non-allocated `.debug_*` sections from a `tms-link --keep-debug` image and
writes an ELF32 little-endian relocatable container with section headers and a
`.shstrtab` but no program headers or symbol table.
No addresses or relocations are rewritten: `tms-link` has already resolved the
DWARF byte addresses (for example `.debug_addr` holds 0x314000 for text origin
0x018A0000), and DWARF section-internal offsets are section-relative.
Usage:
elf-debug-wrap.py -o OUT.elf LINKED.coff [SECTION ...]
With no SECTION arguments every `.debug_*` section is wrapped. The input is
parsed directly (the helper has no project dependencies) so the debug gate can
assert the linked TI-COFF and the ELF wrapper independently.
"""
from __future__ import annotations
import argparse
import struct
import sys
MAGIC = 0x0090
class WrapError(Exception):
pass
def _u16(data, off):
return struct.unpack_from("<H", data, off)[0]
def _u32(data, off):
return struct.unpack_from("<I", data, off)[0]
def _string_at(table, offset, what):
if offset < 4:
raise WrapError("%s string-table offset %d is inside the 4-byte size "
"field" % (what, offset))
end = table.find(b"\0", offset)
if end < 0:
raise WrapError("%s string-table entry at %d is not terminated"
% (what, offset))
return table[offset:end].decode("latin1")
def read_coff(path):
with open(path, "rb") as fh:
data = fh.read()
if len(data) < 20:
raise WrapError("%s: shorter than a COFF file header" % path)
machine, nscns, _timdat, symptr, nsyms, opthdr, _flags = \
struct.unpack_from("<HHIIIHH", data, 0)
if machine != MAGIC:
raise WrapError("%s: machine %#06x, expected %#06x"
% (path, machine, MAGIC))
if nscns == 0:
raise WrapError("%s: no sections" % path)
string_table = b""
if nsyms:
string_base = symptr + nsyms * 18
if string_base + 4 > len(data):
raise WrapError("%s: symbol/string table runs past EOF" % path)
size = _u32(data, string_base)
if size < 4 or string_base + size > len(data):
raise WrapError("%s: implausible string-table size %d"
% (path, size))
string_table = data[string_base:string_base + size]
sections = []
base = 20 + opthdr
for i in range(nscns):
off = base + i * 40
if off + 40 > len(data):
raise WrapError("%s: section header %d past EOF" % (path, i))
raw_name = data[off:off + 8]
if raw_name.startswith(b"/"):
digits = raw_name[1:].split(b"\0", 1)[0]
if not digits.isdigit():
raise WrapError("%s: bad long section name %r"
% (path, raw_name))
name = _string_at(string_table, int(digits),
"%s: section %d" % (path, i + 1))
else:
name = raw_name.split(b"\0", 1)[0].decode("latin1")
paddr, vaddr, size, scnptr, relptr, _lnnoptr, nreloc, _nlnno, flags = \
struct.unpack_from("<IIIIIIHHI", data, off + 8)
if size % 8:
raise WrapError("%s: section %r size %d bits is not whole bytes"
% (path, name, size))
blob = b""
if size and name != ".bss":
if scnptr + size // 8 > len(data):
raise WrapError("%s: section %r raw data runs past EOF"
% (path, name))
blob = data[scnptr:scnptr + size // 8]
sections.append(dict(name=name, paddr=paddr, vaddr=vaddr, size=size,
flags=flags, nreloc=nreloc, raw=blob))
by_name = {s["name"]: s for s in sections}
if len(by_name) != len(sections):
raise WrapError("%s: duplicate section names" % path)
return sections
def build_elf(sections):
# Only the section-header table and the section payloads exist; the DWARF
# data is already absolute, so no symbol table or relocations are needed.
shstr = bytearray(b"\0")
name_off = {}
for sec in sections:
name_off[sec["name"]] = len(shstr)
shstr.extend(sec["name"].encode("latin1"))
shstr.append(0)
shstr_name = len(shstr)
shstr.extend(b".shstrtab\0")
shnum = len(sections) + 2 # null section + payload sections + shstrtab
shoff = 52
cursor = (shoff + shnum * 40 + 3) & ~3
payload_off = []
for sec in sections:
payload_off.append(cursor)
cursor += len(sec["raw"])
cursor = (cursor + 3) & ~3
shstr_off = cursor
cursor += len(shstr)
total = (cursor + 3) & ~3
out = bytearray(total)
e_ident = b"\x7fELF" + bytes([1, 1, 1, 0]) + bytes(8) # 32-bit LE v1
struct.pack_into("<16sHHIIIIIHHHHHH", out, 0, e_ident,
1, # e_type ET_REL
# EM_386 keeps llvm-dwarfdump from warning about an
# unknown machine (there is no standard TMS34010 EM_*).
3, # e_machine EM_386
1, # e_version
0, # e_entry
0, # e_phoff (no program headers)
shoff, # e_shoff
0, # e_flags
52, # e_ehsize
0, 0, # e_phentsize, e_phnum
40, # e_shentsize
shnum, # e_shnum
shnum - 1) # e_shstrndx
for i, sec in enumerate(sections, start=1):
off = shoff + i * 40
struct.pack_into("<IIIIIIIIII", out, off,
name_off[sec["name"]],
1, # SHT_PROGBITS
0, # no sh_flags
0, # sh_addr (DWARF is absolute)
payload_off[i - 1],
len(sec["raw"]),
0, 0, # sh_link, sh_info
1, # sh_addralign
0) # sh_entsize
out[payload_off[i - 1]:payload_off[i - 1] + len(sec["raw"])] = \
sec["raw"]
off = shoff + (shnum - 1) * 40
struct.pack_into("<IIIIIIIIII", out, off, shstr_name, 3, 0, 0,
shstr_off, len(shstr), 0, 0, 1, 0)
out[shstr_off:shstr_off + len(shstr)] = shstr
return bytes(out)
def main(argv=None):
p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
p.add_argument("-o", "--output", required=True, metavar="OUT.elf",
help="ELF32 output container")
p.add_argument("coff", metavar="LINKED.coff",
help="linked TI-COFF with --keep-debug")
p.add_argument("sections", nargs="*", metavar="SECTION",
help="debug section names to include (default: all "
".debug_*)")
args = p.parse_args(argv)
try:
all_sections = read_coff(args.coff)
by_name = {s["name"]: s for s in all_sections}
if args.sections:
missing = [n for n in args.sections if n not in by_name]
if missing:
raise WrapError("%s: missing section(s): %s"
% (args.coff, ", ".join(missing)))
chosen = [by_name[n] for n in args.sections]
else:
chosen = [s for s in all_sections
if s["name"].startswith(".debug_")]
if not chosen:
raise WrapError("%s: no .debug_* sections to wrap" % args.coff)
for sec in chosen:
if not sec["name"].startswith(".debug_"):
raise WrapError("%s: %r is not a .debug_* section"
% (args.coff, sec["name"]))
if sec["paddr"] != 0 or sec["vaddr"] != 0:
raise WrapError("%s: debug section %r has paddr/vaddr "
"%#x/%#x, expected 0/0"
% (args.coff, sec["name"], sec["paddr"],
sec["vaddr"]))
elf = build_elf(chosen)
with open(args.output, "wb") as fh:
fh.write(elf)
print("elf-debug-wrap: %s <- %s (%s)"
% (args.output, args.coff,
" ".join("%s=%dB" % (s["name"], len(s["raw"]))
for s in chosen)))
return 0
except (WrapError, OSError) as exc:
print("elf-debug-wrap: error: %s" % exc, file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(main())