summaryrefslogtreecommitdiff
path: root/tests/tst-relocatable-runner.sh
blob: 20d1c3533a6c5c06462b9d7075543dc9858e8e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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