593 lines
26 KiB
Bash
Executable File
593 lines
26 KiB
Bash
Executable File
#!/bin/sh
|
|
# M7.2 debug-info gate: `llc -filetype=obj` DWARF through tms-link.
|
|
#
|
|
# scripts/tests/m7/debug.sh
|
|
#
|
|
# 1. Compile scripts/m7/debug.c with `tms-clang -g -O0` (clang -g through the
|
|
# bit-pointer barrier) and `llc -filetype=obj`; require the object's
|
|
# long-named `.debug_*` sections and assert, in the final IR, that the
|
|
# `@debug_ptr` body's barrier-synthesized instructions (`mul`, `shl i32`,
|
|
# `ptrtoint ptr`, `llvm.tms34010.bitaddr`, `inttoptr i32`) plus its
|
|
# loads/stores carry
|
|
# `!dbg`. The TU has three functions: debug_mul (int arithmetic),
|
|
# debug_add (int arithmetic), and debug_ptr (`p += debug_index; return *p`,
|
|
# a runtime GEP the barrier rewrites).
|
|
# 2. Also build the same TU with `tms-clang -g -O2` (the native M10 new-PM
|
|
# pipeline: `TMS34010BitPtrPass` -> optimizer -> `TMS34010ScaleGEPPass` ->
|
|
# `TMS34010LowerBitPtrPass` -> instcombine), require
|
|
# the six core `.debug_*` sections, link it with `--keep-debug`, wrap it and
|
|
# smoke-check `debug.c`, a subprogram name and the linked base address with
|
|
# llvm-dwarfdump. The optimized layout is not stable, so the exact
|
|
# low/high/line assertions stay on the -O0 fixture.
|
|
# 3. GSPA assembles scripts/m7/debug-entry.asm; tms-link links DEBUG.O first
|
|
# (so debug_mul is the text-origin function) and DENTRY.O second, twice:
|
|
# --keep-debug and the default strip.
|
|
# 4. The kept -O0 `.debug_*` sections are wrapped in a minimal ELF32 container
|
|
# by scripts/m7/elf-debug-wrap.py and read by llvm-dwarfdump. The gate
|
|
# asserts:
|
|
# - filename debug.c and .debug_line rows for all three functions;
|
|
# - low PC = text origin / 8 + function offset (debug_mul 0x314000,
|
|
# debug_add 0x31402c, debug_ptr 0x314058) and the CU low/high;
|
|
# - DW_AT_high_pc is the function byte size (DW_FORM_data4), not divided;
|
|
# - .debug_addr entries read by index after the DWARF v5 header;
|
|
# - .debug_str_offsets/.debug_str resolve the function/local/global names.
|
|
# 5. The default strip link has exactly .text/.data/.bss and its image loads
|
|
# and runs in MAME (debug_mul -> 43 at 018A8000, debug_add -> 14 at
|
|
# 018A8020, debug_ptr -> 30 at 018A8040).
|
|
#
|
|
# Overridable: LLVM_BIN TMS_CLANG TMS_LLC TMS_LINK M7COFF M7_DEBUG_WORK M7_SDK
|
|
# SDK_IMG DOSBOX DOSBOX_CONF MAME MAME_TIMEOUT LOADER_AT
|
|
# LOADER_END
|
|
set -eu
|
|
|
|
ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
|
|
DIR=$ROOT/scripts/m7
|
|
|
|
LLVM_BIN=${LLVM_BIN:-$ROOT/build/llvm-clean-build/bin}
|
|
TMS_CLANG=${TMS_CLANG:-$ROOT/scripts/tms-clang}
|
|
TMS_LLC=${TMS_LLC:-$LLVM_BIN/llc}
|
|
TMS_LINK=${TMS_LINK:-$ROOT/tools/tms-link/tms-link}
|
|
DWARFDUMP=${DWARFDUMP:-$LLVM_BIN/llvm-dwarfdump}
|
|
M7COFF=${M7COFF:-$ROOT/build/llvm-clean/llvm/test/CodeGen/TMS34010/m7coff.py}
|
|
WORK=${M7_DEBUG_WORK:-$ROOT/build/m7-debug}
|
|
SDK=${M7_SDK:-$WORK/sdk}
|
|
SDK_IMG=${SDK_IMG:-$ROOT/vendor/vt1200/tools/tms34010-sdk/1987-05 TMS34010 SDK/1987-11-06 TMS34010 Simulator-C Tools-ASM Tools.img}
|
|
DOSBOX=${DOSBOX:-dosbox}
|
|
DOSBOX_CONF=${DOSBOX_CONF:-$ROOT/vendor/vt1200/harness/sdk-scratch/dosbox-bench.conf}
|
|
MAME=${MAME:-$ROOT/build/mame-src/vt1200}
|
|
MAME_TIMEOUT=${MAME_TIMEOUT:-900}
|
|
LOADER_LUA=$ROOT/vendor/vt1200/dwt/loader/loader.lua
|
|
LOADER_BIN=$ROOT/vendor/vt1200/dwt/loader/build/loader.bin
|
|
LOADER_AT=${LOADER_AT:-2500}
|
|
LOADER_END=${LOADER_END:-2700}
|
|
|
|
fail() {
|
|
echo "M7 debug: FAIL: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
[ -x "$TMS_CLANG" ] || fail "missing tms-clang: $TMS_CLANG"
|
|
[ -x "$TMS_LLC" ] || fail "missing llc: $TMS_LLC"
|
|
[ -x "$TMS_LINK" ] || fail "missing tms-link: $TMS_LINK"
|
|
[ -x "$DWARFDUMP" ] || fail "missing llvm-dwarfdump: $DWARFDUMP"
|
|
[ -f "$M7COFF" ] \
|
|
|| fail "independent TI-COFF reader not found: $M7COFF (set M7COFF=...)"
|
|
[ -f "$DOSBOX_CONF" ] || fail "missing DOSBox config: $DOSBOX_CONF"
|
|
command -v "$DOSBOX" >/dev/null 2>&1 || fail "missing DOSBox: $DOSBOX"
|
|
[ -x "$MAME" ] || fail "missing MAME binary: $MAME"
|
|
[ -f "$LOADER_LUA" ] || fail "missing COFF loader: $LOADER_LUA"
|
|
[ -f "$LOADER_BIN" ] || fail "missing loader payload: $LOADER_BIN"
|
|
|
|
rm -rf "$WORK"
|
|
mkdir -p "$WORK/dos" "$WORK/run/nvram" "$WORK/run/cfg" "$WORK/evidence"
|
|
WORK=$(cd "$WORK" && pwd)
|
|
|
|
echo "== compile scripts/m7/debug.c with tms-clang -g and llc -filetype=obj"
|
|
# Compile from scripts/m7 with the relative name so DW_AT_name and the
|
|
# .debug_line file table name are exactly "debug.c", not an absolute path.
|
|
( cd "$DIR" && "$TMS_CLANG" -g -O0 -o "$WORK/debug.ll" debug.c ) \
|
|
|| fail "tms-clang -g failed"
|
|
grep -q '!dbg' "$WORK/debug.ll" || fail "debug.ll has no !dbg metadata"
|
|
|
|
# The barrier-synthesized instructions must carry !dbg, not just the ordinary
|
|
# loads/stores. Extract @debug_ptr and check the llvm.tms34010.bitaddr
|
|
# chain plus the load/store through the computed pointer. This is the
|
|
# attribution the line-table rows alone cannot prove.
|
|
awk '
|
|
/^define .*@debug_ptr\(/ { in_fn = 1 }
|
|
in_fn { print }
|
|
in_fn && /^}/ { exit }
|
|
' "$WORK/debug.ll" > "$WORK/debug_ptr.ll"
|
|
[ -s "$WORK/debug_ptr.ll" ] \
|
|
|| fail "no @debug_ptr definition in $WORK/debug.ll"
|
|
check_dbg_line() { # check_dbg_line LABEL ERE
|
|
label=$1
|
|
pattern=$2
|
|
grep -E "$pattern" "$WORK/debug_ptr.ll" \
|
|
> "$WORK/debug_ptr.check" || true
|
|
if [ ! -s "$WORK/debug_ptr.check" ]; then
|
|
cat "$WORK/debug_ptr.ll" >&2
|
|
fail "@debug_ptr has no $label (pattern: $pattern)"
|
|
fi
|
|
if grep -vq '!dbg' "$WORK/debug_ptr.check"; then
|
|
cat "$WORK/debug_ptr.ll" >&2
|
|
fail "@debug_ptr has a $label without !dbg: $(grep -v '!dbg' \
|
|
"$WORK/debug_ptr.check" | head -n 1)"
|
|
fi
|
|
}
|
|
check_dbg_line "GEP index multiply" 'mul i32 .*'
|
|
check_dbg_line "bit-offset shl" 'shl i32 .* 3'
|
|
check_dbg_line "ptrtoint base" 'ptrtoint ptr .* to i32'
|
|
check_dbg_line "bit-address intrinsic" 'call i32 @llvm\.tms34010\.bitaddr'
|
|
check_dbg_line "lowered inttoptr" 'inttoptr i32 .* to ptr'
|
|
for op in store load; do
|
|
grep -E "^[[:space:]]*${op} |^[[:space:]]*%[^ ]+ = ${op} " \
|
|
"$WORK/debug_ptr.ll" > "$WORK/debug_ptr.$op.ll" || true
|
|
[ -s "$WORK/debug_ptr.$op.ll" ] \
|
|
|| { cat "$WORK/debug_ptr.ll" >&2; \
|
|
fail "@debug_ptr has no $op through the computed pointer"; }
|
|
if grep -vq '!dbg' "$WORK/debug_ptr.$op.ll"; then
|
|
cat "$WORK/debug_ptr.ll" >&2
|
|
fail "@debug_ptr has a $op without !dbg"
|
|
fi
|
|
done
|
|
echo " @debug_ptr barrier IR: mul/shl/ptrtoint/bitaddr/inttoptr + loads/stores"
|
|
echo " all carry !dbg"
|
|
|
|
"$TMS_LLC" -march=tms34010 -O0 -filetype=obj \
|
|
"$WORK/debug.ll" -o "$WORK/debug.o" \
|
|
|| fail "llc -filetype=obj failed"
|
|
python3 - "$M7COFF" "$WORK/debug.o" <<'PY' || fail "debug object has no .debug_* sections"
|
|
import os
|
|
import sys
|
|
sys.dont_write_bytecode = True
|
|
sys.path.insert(0, os.path.dirname(sys.argv[1]))
|
|
import m7coff # noqa: E402
|
|
c = m7coff.parse(sys.argv[2])
|
|
debug = [s["name"] for s in c["sections"] if s["name"].startswith(".debug_")]
|
|
if not debug:
|
|
sys.exit("no .debug_* sections in the object")
|
|
for name in (".debug_info", ".debug_abbrev", ".debug_line", ".debug_str",
|
|
".debug_str_offsets", ".debug_addr"):
|
|
if name not in debug:
|
|
sys.exit("missing %s; got %r" % (name, debug))
|
|
print(" object debug sections: " + " ".join(debug))
|
|
PY
|
|
|
|
echo "== build the same TU at -O2 -g (full barrier/optimizer pipeline)"
|
|
# -O2 runs clang's native M10 new-PM pipeline: TMS34010BitPtrPass (barrier) ->
|
|
# optimizer -> TMS34010ScaleGEPPass -> TMS34010LowerBitPtrPass -> instcombine.
|
|
# Requiring the object's .debug_* sections from this build (and
|
|
# the dwarfdump check below) proves !dbg survives the barrier and the
|
|
# optimizer, not just the -O0 path.
|
|
( cd "$DIR" && "$TMS_CLANG" -g -O2 -o "$WORK/debug-o2.ll" debug.c ) \
|
|
|| fail "tms-clang -g -O2 failed"
|
|
grep -q '!dbg' "$WORK/debug-o2.ll" \
|
|
|| fail "the -O2 -g IR has no !dbg metadata"
|
|
"$TMS_LLC" -march=tms34010 -O0 -filetype=obj \
|
|
"$WORK/debug-o2.ll" -o "$WORK/debug-o2.o" \
|
|
|| fail "llc -filetype=obj failed for the -O2 -g IR"
|
|
python3 - "$M7COFF" "$WORK/debug-o2.o" <<'PY' \
|
|
|| fail "the -O2 -g object has no .debug_* sections"
|
|
import os
|
|
import sys
|
|
sys.dont_write_bytecode = True
|
|
sys.path.insert(0, os.path.dirname(sys.argv[1]))
|
|
import m7coff # noqa: E402
|
|
c = m7coff.parse(sys.argv[2])
|
|
debug = [s["name"] for s in c["sections"] if s["name"].startswith(".debug_")]
|
|
for name in (".debug_info", ".debug_abbrev", ".debug_line", ".debug_str",
|
|
".debug_str_offsets", ".debug_addr"):
|
|
if name not in debug:
|
|
sys.exit("-O2 -g object missing %s; got %r" % (name, debug))
|
|
print(" -O2 -g object debug sections: " + " ".join(debug))
|
|
PY
|
|
|
|
echo "== extract the GSPA/GSPLNK toolchain"
|
|
if [ ! -f "$SDK/GSPA.EXE" ]; then
|
|
command -v mcopy >/dev/null 2>&1 || fail "mtools (mcopy) is required"
|
|
[ -f "$SDK_IMG" ] || fail "SDK image not found: $SDK_IMG"
|
|
mkdir -p "$SDK"
|
|
mcopy -s -n -i "$SDK_IMG" "::/A2/*" "$SDK/"
|
|
mcopy -s -n -i "$SDK_IMG" "::/C2/*" "$SDK/"
|
|
fi
|
|
[ -f "$SDK/GSPA.EXE" ] || fail "GSPA.EXE is not extracted in $SDK"
|
|
|
|
DOS=$WORK/dos
|
|
cp "$SDK/GSPA.EXE" "$DOS/"
|
|
cp "$WORK/debug.o" "$DOS/DEBUG.O"
|
|
sed 's/\r$//' "$DIR/debug-entry.asm" | sed 's/$/\r/' > "$DOS/DENTRY.ASM"
|
|
|
|
echo "== assemble debug-entry.asm with GSPA 2.00"
|
|
rm -f "$DOS/DENTRY.O" "$DOS/DENTRY.LST"
|
|
SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy \
|
|
timeout "${DOSBOX_TIMEOUT:-300}" "$DOSBOX" -conf "$DOSBOX_CONF" \
|
|
-noprimaryconf -nolocalconf \
|
|
-c "mount c $DOS" -c "c:" \
|
|
-c "gspa DENTRY DENTRY.O DENTRY.LST -l" \
|
|
-c "exit" -exit > "$WORK/dosbox.log" 2>&1 \
|
|
|| { tail -20 "$WORK/dosbox.log" >&2; fail "DOSBox failed"; }
|
|
[ -f "$DOS/DENTRY.LST" ] || fail "GSPA produced no DENTRY.LST"
|
|
sed 's/\r$//' "$DOS/DENTRY.LST" > "$WORK/dentry.lst"
|
|
grep -q 'No Errors' "$WORK/dentry.lst" \
|
|
|| { grep -i -B2 -A2 'error' "$WORK/dentry.lst" >&2 || true; \
|
|
fail "GSPA reported errors"; }
|
|
grep -q 'No Warnings' "$WORK/dentry.lst" \
|
|
|| { grep -i -B2 -A2 'warning' "$WORK/dentry.lst" >&2 || true; \
|
|
fail "GSPA reported warnings"; }
|
|
|
|
echo "== link with tms-link: --keep-debug and the default strip"
|
|
"$TMS_LINK" -o "$WORK/debug-keep.coff" --keep-debug \
|
|
--map "$WORK/debug-keep.map" --entry _start \
|
|
--text 018A0000 --bss 01A00000 \
|
|
"$DOS/DEBUG.O" "$DOS/DENTRY.O" \
|
|
|| fail "tms-link --keep-debug failed"
|
|
"$TMS_LINK" -o "$WORK/debug-strip.coff" \
|
|
--map "$WORK/debug-strip.map" --entry _start \
|
|
--text 018A0000 --bss 01A00000 \
|
|
"$DOS/DEBUG.O" "$DOS/DENTRY.O" \
|
|
|| fail "tms-link (strip) failed"
|
|
|
|
echo "== link the -O2 -g object with --keep-debug and dump it"
|
|
"$TMS_LINK" -o "$WORK/debug-o2-keep.coff" --keep-debug \
|
|
--map "$WORK/debug-o2-keep.map" --entry _start \
|
|
--text 018A0000 --bss 01A00000 \
|
|
"$WORK/debug-o2.o" "$DOS/DENTRY.O" \
|
|
|| fail "tms-link --keep-debug failed for the -O2 -g object"
|
|
"$DIR/elf-debug-wrap.py" -o "$WORK/debug-o2.elf" \
|
|
"$WORK/debug-o2-keep.coff" \
|
|
|| fail "elf-debug-wrap failed for the -O2 -g object"
|
|
"$DWARFDUMP" --debug-info --verbose --debug-line "$WORK/debug-o2.elf" \
|
|
> "$WORK/dwarf-o2.txt" 2> "$WORK/dwarf-o2.err" \
|
|
|| { cat "$WORK/dwarf-o2.err" >&2; \
|
|
fail "llvm-dwarfdump failed on the -O2 -g image"; }
|
|
grep -q 'DW_AT_name.*"debug.c"' "$WORK/dwarf-o2.txt" \
|
|
|| { head -30 "$WORK/dwarf-o2.txt" >&2; \
|
|
fail "the -O2 -g image has no debug.c compile unit"; }
|
|
grep -Eq 'string = "debug_(mul|add|ptr)"' "$WORK/dwarf-o2.txt" \
|
|
|| { head -40 "$WORK/dwarf-o2.txt" >&2; \
|
|
fail "the -O2 -g image has no debug subprogram name"; }
|
|
grep -q '0x0000000000314000' "$WORK/dwarf-o2.txt" \
|
|
|| { head -40 "$WORK/dwarf-o2.txt" >&2; \
|
|
fail "the -O2 -g image has no linked low PC 0x314000"; }
|
|
echo " -O2 -g linked image: debug.c, subprogram names and 0x314000 present"
|
|
|
|
echo "== inspect the linked keep/strip images"
|
|
python3 - "$M7COFF" "$WORK/debug-keep.coff" "$WORK/debug-strip.coff" \
|
|
"$WORK/debug-env" <<'PY' || fail "linked debug image layout is wrong"
|
|
import os
|
|
import sys
|
|
sys.dont_write_bytecode = True
|
|
sys.path.insert(0, os.path.dirname(sys.argv[1]))
|
|
import m7coff # noqa: E402
|
|
|
|
keep_path, strip_path, env_path = sys.argv[2], sys.argv[3], sys.argv[4]
|
|
keep = m7coff.parse(keep_path)
|
|
strip = m7coff.parse(strip_path)
|
|
keep_sections = [s["name"] for s in keep["sections"]]
|
|
strip_sections = [s["name"] for s in strip["sections"]]
|
|
if strip_sections != [".text", ".data", ".bss"]:
|
|
sys.exit("default strip has sections %r, want .text/.data/.bss"
|
|
% strip_sections)
|
|
if keep_sections[:3] != [".text", ".data", ".bss"]:
|
|
sys.exit("kept image leading sections %r" % keep_sections[:3])
|
|
debug = [n for n in keep_sections[3:] if n.startswith(".debug_")]
|
|
if debug != keep_sections[3:] or not debug:
|
|
sys.exit("kept image sections after .bss are %r"
|
|
% keep_sections[3:])
|
|
for name in (".debug_info", ".debug_abbrev", ".debug_line", ".debug_str",
|
|
".debug_str_offsets", ".debug_addr"):
|
|
if name not in debug:
|
|
sys.exit("kept image missing %s; got %r" % (name, debug))
|
|
|
|
for sa, sb in zip(keep["sections"][:3], strip["sections"]):
|
|
if sa["paddr"] != sb["paddr"] or sa["size"] != sb["size"]:
|
|
sys.exit("%s keep/strip paddr/size %#x/%d != %#x/%d"
|
|
% (sa["name"], sa["paddr"], sa["size"], sb["paddr"],
|
|
sb["size"]))
|
|
if sa["flags"] != sb["flags"]:
|
|
sys.exit("%s keep/strip flags differ" % sa["name"])
|
|
if sa["name"] == ".bss":
|
|
continue
|
|
if sa["raw"] != sb["raw"]:
|
|
sys.exit("%s keep/strip bytes differ" % sa["name"])
|
|
for sec in keep["sections"]:
|
|
if sec["name"].startswith(".debug_"):
|
|
if sec["paddr"] != 0 or sec["vaddr"] != 0:
|
|
sys.exit("%s paddr/vaddr %#x/%#x, want 0/0"
|
|
% (sec["name"], sec["paddr"], sec["vaddr"]))
|
|
if sec["flags"] != 0:
|
|
sys.exit("%s flags %#x, want 0" % (sec["name"], sec["flags"]))
|
|
if sec["nreloc"]:
|
|
sys.exit("%s still has %d relocations"
|
|
% (sec["name"], sec["nreloc"]))
|
|
elif sec["nreloc"]:
|
|
sys.exit("%s still has %d relocations" % (sec["name"], sec["nreloc"]))
|
|
|
|
syms = {s["name"]: s for s in keep["symbols"]}
|
|
for name in ("_debug_mul", "_debug_add", "_debug_ptr", "_start",
|
|
"_debug_array", "_debug_index"):
|
|
if name not in syms:
|
|
sys.exit("kept image missing symbol %s" % name)
|
|
mul = syms["_debug_mul"]["value"]
|
|
add = syms["_debug_add"]["value"]
|
|
ptr = syms["_debug_ptr"]["value"]
|
|
start = syms["_start"]["value"]
|
|
array = syms["_debug_array"]["value"]
|
|
index = syms["_debug_index"]["value"]
|
|
text = m7coff.section(keep, ".text")
|
|
text_end = text["paddr"] + text["size"]
|
|
if not (text["paddr"] <= mul < add < ptr < start < text_end):
|
|
sys.exit("debug functions are not at increasing text offsets: "
|
|
"%#x %#x %#x %#x %#x %#x" % (text["paddr"], mul, add, ptr,
|
|
start, text_end))
|
|
|
|
# `.debug_addr` is DWARF v5: unit_length (4), version (2), address_size (1),
|
|
# segment_selector_size (1), then 4-byte entries. Read the entries by index.
|
|
addr = m7coff.section(keep, ".debug_addr")["raw"]
|
|
if len(addr) < 8:
|
|
sys.exit(".debug_addr is too short: %s" % addr.hex())
|
|
unit_length = int.from_bytes(addr[0:4], "little")
|
|
version = int.from_bytes(addr[4:6], "little")
|
|
addr_size = addr[6]
|
|
seg_size = addr[7]
|
|
if version != 5 or addr_size != 4 or seg_size != 0:
|
|
sys.exit(".debug_addr header version/size/seg %d/%d/%d, want 5/4/0"
|
|
% (version, addr_size, seg_size))
|
|
if unit_length != len(addr) - 4:
|
|
sys.exit(".debug_addr unit_length %d does not cover %d bytes"
|
|
% (unit_length, len(addr) - 4))
|
|
entries = [int.from_bytes(addr[8 + i * 4:12 + i * 4], "little")
|
|
for i in range((len(addr) - 8) // 4)]
|
|
# The first three entries are the CU/subprogram low PCs; the last two are the
|
|
# .data globals referenced by DW_OP_addrx in their DW_TAG_variable locations.
|
|
# This exact list/order is deterministic at compiler b9ef51a587b5; a DWARF
|
|
# emission change may legitimately add or reorder entries, in which case relax
|
|
# this assertion to a prefix/subset while keeping the indexed read.
|
|
want_addr = [mul // 8, add // 8, ptr // 8, array // 8, index // 8]
|
|
if entries != want_addr:
|
|
sys.exit(".debug_addr entries %r, want %r"
|
|
% ([hex(v) for v in entries], [hex(v) for v in want_addr]))
|
|
|
|
# The names live in .debug_str and are selected through .debug_str_offsets.
|
|
debug_str = m7coff.section(keep, ".debug_str")["raw"]
|
|
for name in (b"debug_mul", b"debug_add", b"debug_ptr", b"product", b"sum",
|
|
b"debug_array", b"debug_index", b"debug.c"):
|
|
if name + b"\0" not in debug_str:
|
|
sys.exit(".debug_str does not contain %r" % name)
|
|
|
|
with open(env_path, "w", encoding="ascii") as fh:
|
|
fh.write("DEBUG_TEXT_ORIGIN=%d\n" % text["paddr"])
|
|
fh.write("DEBUG_MUL_BITS=%d\n" % mul)
|
|
fh.write("DEBUG_ADD_BITS=%d\n" % add)
|
|
fh.write("DEBUG_PTR_BITS=%d\n" % ptr)
|
|
fh.write("DEBUG_START_BITS=%d\n" % start)
|
|
fh.write("DEBUG_TEXT_END=%d\n" % text_end)
|
|
print(" keep sections: %s" % " ".join(keep_sections))
|
|
print(" strip sections: %s" % " ".join(strip_sections))
|
|
print(" _debug_mul=%#x _debug_add=%#x _debug_ptr=%#x _start=%#x (bits), "
|
|
"text end=%#x" % (mul, add, ptr, start, text_end))
|
|
PY
|
|
|
|
. "$WORK/debug-env"
|
|
MUL_SIZE=$(((DEBUG_ADD_BITS - DEBUG_MUL_BITS) / 8))
|
|
ADD_SIZE=$(((DEBUG_PTR_BITS - DEBUG_ADD_BITS) / 8))
|
|
PTR_SIZE=$(((DEBUG_START_BITS - DEBUG_PTR_BITS) / 8))
|
|
[ "$((DEBUG_TEXT_ORIGIN / 8))" -eq $((0x314000)) ] \
|
|
|| fail "text origin byte address is $((DEBUG_TEXT_ORIGIN / 8)), want 0x314000"
|
|
|
|
echo "== wrap the kept .debug_* sections in a minimal ELF32 container"
|
|
"$DIR/elf-debug-wrap.py" -o "$WORK/debug.elf" "$WORK/debug-keep.coff" \
|
|
|| fail "elf-debug-wrap failed"
|
|
|
|
echo "== llvm-dwarfdump"
|
|
"$DWARFDUMP" --debug-info --verbose "$WORK/debug.elf" \
|
|
> "$WORK/dwarf-info.txt" 2> "$WORK/dwarf-info.err" \
|
|
|| { cat "$WORK/dwarf-info.err" >&2; fail "llvm-dwarfdump --debug-info failed"; }
|
|
"$DWARFDUMP" --debug-line "$WORK/debug.elf" \
|
|
> "$WORK/dwarf-line.txt" 2> "$WORK/dwarf-line.err" \
|
|
|| { cat "$WORK/dwarf-line.err" >&2; fail "llvm-dwarfdump --debug-line failed"; }
|
|
grep -q 'DW_AT_name.*"debug.c"' "$WORK/dwarf-info.txt" \
|
|
|| { head -40 "$WORK/dwarf-info.txt" >&2; \
|
|
fail "DWARF has no debug.c compilation-unit name"; }
|
|
grep -q 'name: "debug.c"' "$WORK/dwarf-line.txt" \
|
|
|| { head -40 "$WORK/dwarf-line.txt" >&2; \
|
|
fail ".debug_line has no debug.c filename"; }
|
|
|
|
python3 - "$WORK/dwarf-info.txt" "$WORK/dwarf-line.txt" \
|
|
"$DEBUG_TEXT_ORIGIN" "$DEBUG_MUL_BITS" "$DEBUG_ADD_BITS" \
|
|
"$DEBUG_PTR_BITS" "$DEBUG_START_BITS" "$MUL_SIZE" "$ADD_SIZE" \
|
|
"$PTR_SIZE" <<'PY' \
|
|
|| fail "DWARF subprograms/line rows are wrong"
|
|
import re
|
|
import sys
|
|
|
|
info_path, line_path = sys.argv[1], sys.argv[2]
|
|
text_origin_bits = int(sys.argv[3])
|
|
mul_bits = int(sys.argv[4])
|
|
add_bits = int(sys.argv[5])
|
|
ptr_bits = int(sys.argv[6])
|
|
start_bits = int(sys.argv[7])
|
|
mul_size = int(sys.argv[8])
|
|
add_size = int(sys.argv[9])
|
|
ptr_size = int(sys.argv[10])
|
|
|
|
# DWARF addresses are bytes; COFF symbol values are bits. The linked low PC is
|
|
# the text origin in bytes plus the function's bit offset / 8 (exactly once).
|
|
text_origin = text_origin_bits // 8
|
|
mul_low = text_origin + (mul_bits - text_origin_bits) // 8
|
|
add_low = text_origin + (add_bits - text_origin_bits) // 8
|
|
ptr_low = text_origin + (ptr_bits - text_origin_bits) // 8
|
|
start_low = text_origin + (start_bits - text_origin_bits) // 8
|
|
if text_origin != 0x314000:
|
|
sys.exit("text origin byte address %#x, want 0x314000" % text_origin)
|
|
|
|
subs = []
|
|
cu = {"name": None, "low": None, "high": None, "high_form": None,
|
|
"line": None}
|
|
cur = None
|
|
mode = None
|
|
for raw in open(info_path, encoding="latin1"):
|
|
line = raw.rstrip("\n")
|
|
if "DW_TAG_compile_unit" in line:
|
|
mode = "cu"
|
|
cur = cu
|
|
continue
|
|
if "DW_TAG_subprogram" in line:
|
|
cur = {"name": None, "low": None, "high": None, "high_form": None,
|
|
"line": None}
|
|
subs.append(cur)
|
|
mode = "sub"
|
|
continue
|
|
if "DW_TAG_" in line:
|
|
mode = None
|
|
continue
|
|
if mode is None or cur is None:
|
|
continue
|
|
m = re.search(r"DW_AT_low_pc.*address = 0x([0-9a-fA-F]+)", line)
|
|
if m:
|
|
cur["low"] = int(m.group(1), 16)
|
|
continue
|
|
m = re.search(r"DW_AT_high_pc \[([A-Za-z0-9_]+)\]\s+\(0x([0-9a-fA-F]+)\)", line)
|
|
if m:
|
|
cur["high_form"] = m.group(1)
|
|
cur["high"] = int(m.group(2), 16)
|
|
continue
|
|
m = re.search(r'DW_AT_name.*string = "([^"]+)"', line)
|
|
if m:
|
|
cur["name"] = m.group(1)
|
|
continue
|
|
m = re.search(r"DW_AT_decl_line \[[A-Za-z0-9_]+\]\s+\((\d+)\)", line)
|
|
if m:
|
|
cur["line"] = int(m.group(1))
|
|
continue
|
|
|
|
if cu["name"] != "debug.c":
|
|
sys.exit("compile unit name %r, want debug.c" % cu["name"])
|
|
if cu["low"] != text_origin:
|
|
sys.exit("compile unit low_pc %#x, want text origin %#x"
|
|
% (cu["low"], text_origin))
|
|
cu_size = (start_bits - text_origin_bits) // 8
|
|
if cu["high_form"] != "DW_FORM_data4" or cu["high"] != cu_size:
|
|
sys.exit("compile unit high_pc %r/%#x, want DW_FORM_data4/%#x"
|
|
% (cu["high_form"], cu["high"], cu_size))
|
|
|
|
by_name = {s["name"]: s for s in subs if s["name"]}
|
|
for name, low, size, decl in (
|
|
("debug_mul", mul_low, mul_size, 1),
|
|
("debug_add", add_low, add_size, 6),
|
|
("debug_ptr", ptr_low, ptr_size, 14)):
|
|
if name not in by_name:
|
|
sys.exit("no DW_TAG_subprogram named %s (saw %r)"
|
|
% (name, sorted(by_name)))
|
|
s = by_name[name]
|
|
if s["low"] != low:
|
|
sys.exit("%s low_pc %#x, want %#x (text %#x + offset %#x)"
|
|
% (name, s["low"], low, text_origin, low - text_origin))
|
|
if s["high_form"] != "DW_FORM_data4":
|
|
sys.exit("%s high_pc form %r, want DW_FORM_data4"
|
|
% (name, s["high_form"]))
|
|
if s["high"] != size:
|
|
sys.exit("%s high_pc field %#x, want byte size %#x (not divided)"
|
|
% (name, s["high"], size))
|
|
if s["line"] != decl:
|
|
sys.exit("%s DW_AT_decl_line %r, want %d" % (name, s["line"], decl))
|
|
|
|
# Line rows: a row at each function low PC with the declaration line, plus a
|
|
# body row for the first local-variable/pointer-update line.
|
|
rows = {}
|
|
files = {}
|
|
for raw in open(line_path, encoding="latin1"):
|
|
m = re.match(r"^(0x[0-9a-fA-F]+)\s+(\d+)\s+(\d+)\s+(\d+)\s",
|
|
raw.strip() + " ")
|
|
if m:
|
|
rows[int(m.group(1), 16)] = int(m.group(2))
|
|
files[int(m.group(1), 16)] = int(m.group(4))
|
|
if rows.get(mul_low) != 1:
|
|
sys.exit("no .debug_line row at debug_mul %#x with line 1 (rows %r)"
|
|
% (mul_low, rows))
|
|
if rows.get(add_low) != 6:
|
|
sys.exit("no .debug_line row at debug_add %#x with line 6 (rows %r)"
|
|
% (add_low, rows))
|
|
if rows.get(ptr_low) != 15:
|
|
sys.exit("no .debug_line row at debug_ptr %#x with line 15 (rows %r)"
|
|
% (ptr_low, rows))
|
|
for label, low in (("debug_mul", mul_low), ("debug_add", add_low),
|
|
("debug_ptr", ptr_low)):
|
|
if files.get(low) != 0:
|
|
sys.exit("%s .debug_line file index %r, want 0"
|
|
% (label, files.get(low)))
|
|
def has_line(lo, hi, want):
|
|
return any(lo <= a < hi and line == want for a, line in rows.items())
|
|
if not has_line(mul_low, add_low, 2):
|
|
sys.exit("no line-2 row inside debug_mul")
|
|
if not has_line(add_low, ptr_low, 7):
|
|
sys.exit("no line-7 row inside debug_add")
|
|
if not has_line(ptr_low, start_low, 16):
|
|
sys.exit("no line-16 pointer-update row inside debug_ptr")
|
|
|
|
print(" compile unit: %s low=%#x high=%#x (%d B)"
|
|
% (cu["name"], cu["low"], cu["high"], cu["high"]))
|
|
print(" subprograms: " + ", ".join(
|
|
"%s low=%#x high=%#x (%d B) line=%d"
|
|
% (s["name"], s["low"], s["high"], s["high"], s["line"])
|
|
for s in subs if s["name"] in ("debug_mul", "debug_add", "debug_ptr")))
|
|
print(" line rows: debug_mul@%#x=1 (body 2), debug_add@%#x=6 (body 7), "
|
|
"debug_ptr@%#x=15 (pointer update 16)"
|
|
% (mul_low, add_low, ptr_low))
|
|
PY
|
|
|
|
echo "== run the stripped image in MAME"
|
|
cat > "$WORK/debug-check.lua" <<'LUA'
|
|
dofile(os.getenv("M4_LOADER_LUA"))
|
|
local frames = 0
|
|
emu.register_frame_done(function()
|
|
frames = frames + 1
|
|
if frames == tonumber(os.getenv("LOADER_END") or "2700") - 1 then
|
|
local prog = manager.machine.devices[':maincpu'].spaces['program']
|
|
local mul = prog:read_u32(0x018A8000)
|
|
local add = prog:read_u32(0x018A8020)
|
|
local ptr = prog:read_u32(0x018A8040)
|
|
print(string.format("DEBUG: mul=%d add=%d ptr=%d", mul, add, ptr))
|
|
if mul == 43 and add == 14 and ptr == 30 then
|
|
print("DEBUG MAME: PASS")
|
|
else
|
|
print("DEBUG MAME: FAIL (want mul=43 add=14 ptr=30)")
|
|
end
|
|
end
|
|
end, "debug-check")
|
|
LUA
|
|
|
|
rm -rf "$WORK/run/nvram"
|
|
mkdir -p "$WORK/run/nvram"
|
|
VT1200_MONITOR=VR319 \
|
|
VT1200_SKIP_LEVEL1=1 \
|
|
LOADER_AT="$LOADER_AT" LOADER_END="$LOADER_END" \
|
|
LOADER_BIN="$LOADER_BIN" LOADER_COFF="$WORK/debug-strip.coff" \
|
|
LOADER_OUT="$WORK/evidence" \
|
|
M4_LOADER_LUA="$LOADER_LUA" \
|
|
timeout "$MAME_TIMEOUT" "$MAME" vt1200 \
|
|
-rompath "$ROOT/vendor/vt1200/harness/mame/roms" \
|
|
-video none -sound none -nothrottle -skip_gameinfo \
|
|
-nvram_directory "$WORK/run/nvram" \
|
|
-cfg_directory "$WORK/run/cfg" \
|
|
-debug -debugger none \
|
|
-autoboot_script "$WORK/debug-check.lua" -autoboot_delay 0 \
|
|
> "$WORK/mame.log" 2>&1 \
|
|
|| { tail -30 "$WORK/mame.log" >&2; fail "MAME run failed"; }
|
|
|
|
grep -q 'LOADER: result status=0 (OK)' "$WORK/mame.log" \
|
|
|| { grep 'LOADER: result' "$WORK/mame.log" >&2 || true; \
|
|
fail "COFF loader did not report status 0"; }
|
|
grep -q 'placed \.text .* 0 differing' "$WORK/mame.log" \
|
|
|| fail "COFF loader could not verify the placed .text bytes"
|
|
grep -q '^DEBUG: mul=43 add=14 ptr=30$' "$WORK/mame.log" \
|
|
|| { grep '^DEBUG:' "$WORK/mame.log" >&2 || true; \
|
|
fail "debug markers are wrong"; }
|
|
grep -q '^DEBUG MAME: PASS$' "$WORK/mame.log" \
|
|
|| fail "MAME marker check failed"
|
|
|
|
echo "M7 debug: PASS (barrier-rewritten pointer TU at -O0 and -O2, DWARF"
|
|
echo " filename/line/low/high/strings, indexed .debug_addr,"
|
|
echo " stripped load image runs in MAME)"
|