#!/usr/bin/env sh
set -e
nm=$RISCV_NM
readelf=$RISCV_READELF
if [ -z "$nm" ]; then nm=riscv64-unknown-elf-nm; fi
if [ -z "$readelf" ]; then readelf=riscv64-unknown-elf-readelf; fi
elf=build/task01_isr_drivers/prog.elf
if "$nm" -C -u "$elf" | grep -Eq '__cxa_|_Unwind_|std::|libstdc\+\+'; then echo "hosted C++ runtime dependency" >&2; exit 1; fi
if "$nm" -C --defined-only "$elf" | grep -Eq 'vtable for|typeinfo for|typeinfo name for'; then echo "virtual/RTTI metadata" >&2; exit 1; fi
if "$readelf" -S "$elf" | grep -Eq '\.init_array|\.eh_frame|\.gcc_except_table'; then echo "initializer/exception/unwind section" >&2; exit 1; fi
for symbol in freertos_risc_v_application_interrupt_handler isr_drivers_debug_checkpoint g_mcause g_rx_queued g_rx_dropped g_isr_yield_calls g_isr_drivers_pass; do
  if ! "$nm" --defined-only "$elf" | grep -Fq "$symbol"; then echo "missing evidence: $symbol" >&2; exit 1; fi
done
for symbol in g_uart_registers g_gpio_registers g_rx_queue_control g_rx_queue_storage g_receiver_tcb g_receiver_stack; do
  if ! "$nm" -C --defined-only "$elf" | grep -E "[[:space:]][bB][[:space:]].*$symbol" >/dev/null; then
    echo "static ISR storage is not in .bss: $symbol" >&2; exit 1
  fi
done
echo "PASS ABI: MMIO model, queue and task storage in .bss; no C++ runtime"
