summaryrefslogtreecommitdiff
path: root/tests/tst-relocatable-runner.sh
diff options
context:
space:
mode:
authorkx <kx@radix-linux.su>2026-10-09 18:00:15 +0300
committerkx <kx@radix-linux.su>2026-10-09 18:00:15 +0300
commit065da3509019b15dd0e678072e6be629e5330bef (patch)
tree69c934c9e786ac048b4fbe8f4c4c5571661521f4 /tests/tst-relocatable-runner.sh
parentd130fe27fa753be02ed76fd9b277aa3c3d928eb9 (diff)
downloadlibmcpu-0.4.0.tar.xz
Version 0.4.00.4.0
Diffstat (limited to 'tests/tst-relocatable-runner.sh')
-rwxr-xr-xtests/tst-relocatable-runner.sh75
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/tst-relocatable-runner.sh b/tests/tst-relocatable-runner.sh
new file mode 100755
index 0000000..20d1c35
--- /dev/null
+++ b/tests/tst-relocatable-runner.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+set -eu
+: "${CPU_AS:?}"
+: "${CPU_LD:?}"
+: "${CPU_RUN:?}"
+: "${TEST_SRCDIR:?}"
+
+D="${TMPDIR:-/tmp}/libcpu-reloc-$$"
+trap 'rm -rf "$D"' EXIT HUP INT TERM
+mkdir -p "$D"
+
+cat > "$D/main.s" <<'EOS'
+.global _start
+.global _life
+.extern add_two
+.extern shared
+.text
+_start_life:
+ LDI r64@0x00000, 40
+ CALL add_two
+ STORE shared, r64@0x00000
+ LOAD r64@0x00018, shared
+ LDI r32@0x00030, 0
+ EXIT r32@0x00030
+EOS
+
+cat > "$D/lib.s" <<'EOS'
+.global add_two
+.global shared
+.data
+.align 32
+shared:
+ .integer 64, 0
+raw_bytes:
+ .byte 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x40, 0x00
+ .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+ .byte 0x00, 0x00, 0xd4, 0x37
+.text
+add_two:
+ LDI r64@0x00008, 2
+ IADD r64@0x00000, r64@0x00000, r64@0x00008
+ RET
+EOS
+
+"$CPU_AS" -o "$D/main.o" "$D/main.s" >/dev/null
+"$CPU_AS" "$D/lib.s" -o "$D/lib.o" >/dev/null
+"$CPU_LD" --stack-size=512 --heap-size=1M --heap-limit=8G -o "$D/a" "$D/main.o" "$D/lib.o" >/dev/null
+"$CPU_LD" --stack-size=1KiB --heap-size=1MiB --heap-limit=1GiB -o "$D/a-units" "$D/main.o" "$D/lib.o" >/dev/null
+if "$CPU_LD" --stack-size=1KiB --heap-size=-1M --heap-limit=1GiB -o "$D/a-bad" "$D/main.o" "$D/lib.o" >/dev/null 2>&1; then
+ echo "mcpu-ld accepted negative heap size" >&2
+ exit 1
+fi
+if "$CPU_LD" --stack-size=1KiB --heap-size=1B --heap-limit=1GiB -o "$D/a-bad" "$D/main.o" "$D/lib.o" >/dev/null 2>&1; then
+ echo "mcpu-ld accepted unsupported B suffix" >&2
+ exit 1
+fi
+v=`"$CPU_RUN" --int-reg=r64@0x00018 "$D/a"`
+test "x$v" = x42
+
+# DATA integer output is target-representation aware.
+v=`"$CPU_RUN" --int-data=64@32 "$D/a"`
+test "x$v" = x42
+
+# raw_bytes starts at DATA offset 40. Request beyond the end of .data:
+# cpu-run must clip the dump to the 20 bytes that remain in .data.
+out=`"$CPU_RUN" --raw-data=64@40 "$D/a"`
+printf '%s\n' "$out" | grep -F 'DATA memory dump: (20 bytes):' >/dev/null
+printf '%s\n' "$out" | grep -F ' 0000 : 02 00 01 00 01 00 40 00 00 00 00 00 00 00 00 00 | ......@.........' >/dev/null
+printf '%s\n' "$out" | grep -F ' 0010 : 00 00 d4 37 ' >/dev/null
+
+# The linker-owned DATA NULL guard is not part of .data and must not be dumpable.
+if "$CPU_RUN" --raw-data=1@16 "$D/a" >/dev/null 2>&1; then
+ echo 'raw DATA dump escaped the linked .data section' >&2
+ exit 1
+fi