#!/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_integration/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 integration_debug_checkpoint freertos_risc_v_application_interrupt_handler g_trace g_barrier_mask g_dynamic_queue_heap_cost g_pressure_dropped g_integration_pass; do
  if ! "$nm" --defined-only "$elf" | grep -Fq "$symbol"; then echo "missing evidence: $symbol" >&2; exit 1; fi
done
for symbol in g_input_storage g_service_storage g_timer_storage g_producer_storage g_processor_storage g_reporter_storage g_trace; do
  if ! "$nm" -C --defined-only "$elf" | grep -E "[[:space:]][bB][[:space:]].*$symbol" >/dev/null; then
    echo "integration static storage is not in .bss: $symbol" >&2; exit 1
  fi
done
echo "PASS ABI: static services in .bss, one measured dynamic queue, no C++ runtime"
