#!/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(" 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(" 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("