summaryrefslogtreecommitdiff
path: root/tests/tst-objdump-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-objdump-runner.sh
parentd130fe27fa753be02ed76fd9b277aa3c3d928eb9 (diff)
downloadlibmcpu-0.4.0.tar.xz
Version 0.4.00.4.0
Diffstat (limited to 'tests/tst-objdump-runner.sh')
-rwxr-xr-xtests/tst-objdump-runner.sh157
1 files changed, 157 insertions, 0 deletions
diff --git a/tests/tst-objdump-runner.sh b/tests/tst-objdump-runner.sh
new file mode 100755
index 0000000..94142eb
--- /dev/null
+++ b/tests/tst-objdump-runner.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+set -eu
+
+: "${PACKAGE_VERSION:?}"
+
+: "${CPU_AS:?}" "${CPU_LD:?}" "${CPU_AR:?}" "${CPU_OBJDUMP:?}"
+
+TMP=${TMPDIR:-/tmp}/libmcpu-objdump-$$
+trap 'rm -rf "$TMP"' EXIT HUP INT TERM
+mkdir -p "$TMP"
+
+cat > "$TMP/unit.s" <<'EOS'
+.global entry
+.global data_global
+.extern external_function
+.text
+entry:
+ SETF CF
+ CLRF AF
+ CLRCPUF
+ CLRMPUF
+ LDI r64@0x00000, 41
+ CALL local_helper
+ CALL external_function
+ RET
+local_helper:
+ LDI r64@0x00008, 1
+ IADD r64@0x00000, r64@0x00000, r64@0x00008
+ RET
+.data
+data_global:
+ .integer 64, 7
+data_pointer:
+ .ptr data_global
+EOS
+
+cat > "$TMP/dyn.s" <<'EOS'
+.global dyn_api
+.global dyn_data
+.text
+dyn_api:
+ CALL local_dyn
+ RET
+local_dyn:
+ LDI r64@0x00008, 2
+ RET
+.data
+dyn_data:
+ .integer 64, 9
+EOS
+
+cat > "$TMP/a.s" <<'EOS'
+.global archive_a
+.text
+archive_a:
+ RET
+EOS
+
+cat > "$TMP/b.s" <<'EOS'
+.global archive_b
+.text
+archive_b:
+ RET
+EOS
+
+"$CPU_AS" -o "$TMP/unit.o" "$TMP/unit.s"
+"$CPU_AS" -o "$TMP/dyn.o" "$TMP/dyn.s"
+"$CPU_AS" -o "$TMP/a.o" "$TMP/a.s"
+"$CPU_AS" -o "$TMP/b.o" "$TMP/b.s"
+"$CPU_LD" --shared --soname=libobjdump.so.1 -o "$TMP/libobjdump.so" "$TMP/dyn.o"
+"$CPU_AR" rcs "$TMP/libobjdump.a" "$TMP/a.o" "$TMP/b.o"
+
+"$CPU_OBJDUMP" -h "$TMP/unit.o" > "$TMP/headers.txt"
+grep -q '^Sections:$' "$TMP/headers.txt"
+grep -q '\.text' "$TMP/headers.txt"
+grep -q '\.data' "$TMP/headers.txt"
+grep -q 'PROGBITS.*CODE' "$TMP/headers.txt"
+
+"$CPU_OBJDUMP" -t "$TMP/unit.o" > "$TMP/symbols.txt"
+grep -q '^SYMBOL TABLE:$' "$TMP/symbols.txt"
+grep -q ' entry$' "$TMP/symbols.txt"
+grep -q ' local_helper$' "$TMP/symbols.txt"
+grep -q ' data_global$' "$TMP/symbols.txt"
+grep -q ' external_function$' "$TMP/symbols.txt"
+
+"$CPU_OBJDUMP" -r "$TMP/unit.o" > "$TMP/relocs.txt"
+grep -q 'RELOCATION RECORDS FOR \[.text\]' "$TMP/relocs.txt"
+grep -q 'R_MCPU_CODE64.*local_helper' "$TMP/relocs.txt"
+grep -q 'R_MCPU_CODE64.*external_function' "$TMP/relocs.txt"
+
+"$CPU_OBJDUMP" -d -r "$TMP/unit.o" > "$TMP/disasm.txt"
+grep -q 'Disassembly of section .text:' "$TMP/disasm.txt"
+grep -q '<entry>:' "$TMP/disasm.txt"
+grep -q '<local_helper>:' "$TMP/disasm.txt"
+grep -q 'SETF.*CF' "$TMP/disasm.txt"
+grep -q 'CLRF.*AF' "$TMP/disasm.txt"
+grep -q 'CLRCPUF' "$TMP/disasm.txt"
+grep -q 'CLRMPUF' "$TMP/disasm.txt"
+grep -q 'LDI.*r64@0x00000, 41' "$TMP/disasm.txt"
+grep -q 'CALL.*<local_helper>' "$TMP/disasm.txt"
+grep -q 'CALL.*<external_function>' "$TMP/disasm.txt"
+grep -q 'R_MCPU_CODE64.*local_helper' "$TMP/disasm.txt"
+# -r remains complete with -d: non-executable-section relocations are
+# printed separately while .text relocations stay next to instructions.
+grep -q 'RELOCATION RECORDS FOR \[.data\]' "$TMP/disasm.txt"
+grep -q 'R_MCPU_PTR64_DATA.*data_global' "$TMP/disasm.txt"
+
+"$CPU_OBJDUMP" -s -j .data "$TMP/unit.o" > "$TMP/data.txt"
+grep -q 'Contents of section .data:' "$TMP/data.txt"
+if grep -q 'Contents of section .text:' "$TMP/data.txt"; then
+ echo "mcpu-objdump -j .data unexpectedly displayed .text contents" >&2
+ exit 1
+fi
+
+"$CPU_OBJDUMP" -D -j .data "$TMP/unit.o" > "$TMP/all-data.txt"
+grep -q 'Disassembly of section .data:' "$TMP/all-data.txt"
+
+"$CPU_OBJDUMP" -T "$TMP/libobjdump.so" > "$TMP/dynamic.txt"
+grep -q '^DYNAMIC SYMBOL TABLE:$' "$TMP/dynamic.txt"
+grep -q ' dyn_api$' "$TMP/dynamic.txt"
+grep -q ' dyn_data$' "$TMP/dynamic.txt"
+
+"$CPU_OBJDUMP" -d -r "$TMP/libobjdump.so" > "$TMP/dynamic-disasm.txt"
+grep -q '<dyn_api>:' "$TMP/dynamic-disasm.txt"
+grep -q 'CALL.*<.L\$0\$0>' "$TMP/dynamic-disasm.txt"
+grep -q 'R_MCPU_CODE64.*.L\$0\$0' "$TMP/dynamic-disasm.txt"
+
+"$CPU_OBJDUMP" -t "$TMP/libobjdump.a" > "$TMP/archive.txt"
+grep -F "$TMP/libobjdump.a(a.o):" "$TMP/archive.txt" >/dev/null
+grep -F "$TMP/libobjdump.a(b.o):" "$TMP/archive.txt" >/dev/null
+grep -q ' archive_a$' "$TMP/archive.txt"
+grep -q ' archive_b$' "$TMP/archive.txt"
+
+# Grouped short options and attached -j argument are accepted.
+"$CPU_OBJDUMP" -hdr -j.text "$TMP/unit.o" > "$TMP/grouped.txt"
+grep -q 'Sections:' "$TMP/grouped.txt"
+grep -q 'Disassembly of section .text:' "$TMP/grouped.txt"
+grep -q 'R_MCPU_CODE64' "$TMP/grouped.txt"
+
+# Missing/invalid input remains a hard error.
+dd if=/dev/zero of="$TMP/bad" bs=256 count=1 2>/dev/null
+if "$CPU_OBJDUMP" -h "$TMP/bad" > /dev/null 2> "$TMP/bad.err"; then
+ echo "mcpu-objdump unexpectedly accepted a non-MELF file" >&2
+ exit 1
+fi
+grep -q 'not a MELF file' "$TMP/bad.err"
+
+if "$CPU_OBJDUMP" "$TMP/unit.o" > /dev/null 2> "$TMP/no-option.err"; then
+ echo "mcpu-objdump unexpectedly accepted no display option" >&2
+ exit 1
+fi
+grep -q 'at least one display option is required' "$TMP/no-option.err"
+
+"$CPU_OBJDUMP" --help | grep -q -- '--disassemble-all'
+"$CPU_OBJDUMP" --help | grep -q -- '--dynamic-syms'
+"$CPU_OBJDUMP" --help | grep -q -- '--section=NAME'
+"$CPU_OBJDUMP" --version | grep -Fq "$PACKAGE_VERSION"