summaryrefslogtreecommitdiff
path: root/tests/tst-library-search-runner.sh
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tst-library-search-runner.sh')
-rwxr-xr-xtests/tst-library-search-runner.sh118
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/tst-library-search-runner.sh b/tests/tst-library-search-runner.sh
new file mode 100755
index 0000000..6c5ee73
--- /dev/null
+++ b/tests/tst-library-search-runner.sh
@@ -0,0 +1,118 @@
+#!/bin/sh
+set -eu
+
+: "${CPU_AS:?}" "${CPU_LD:?}" "${CPU_RUN:?}" "${TEST_SRCDIR:?}"
+
+TMP=${TMPDIR:-/tmp}/libmcpu-library-search-$$
+HOME="$TMP/home"
+export HOME
+mkdir -p "$HOME/.mcpu/etc" "$TMP/root/lib" "$TMP/first" "$TMP/second" "$TMP/user/lib"
+trap 'rm -rf "$TMP"' EXIT HUP INT TERM
+
+EXAMPLES=`cd "$TEST_SRCDIR/fixtures/shared-libs" && pwd`
+
+"$CPU_AS" -o "$TMP/main.o" "$EXAMPLES/dynamic-main.s"
+"$CPU_AS" -o "$TMP/a.o" "$EXAMPLES/dynamic-liba.s"
+"$CPU_AS" -o "$TMP/b.o" "$EXAMPLES/dynamic-libb.s"
+"$CPU_LD" --dynamic --stack-size=4096 --no-heap \
+ -o "$TMP/main-program" "$TMP/main.o"
+"$CPU_LD" --shared --soname=liba.so.1 -o "$TMP/liba.so" "$TMP/a.o"
+"$CPU_LD" --shared --soname=libb.so.1 -o "$TMP/libb.so" "$TMP/b.o"
+
+cp "$TMP/liba.so" "$TMP/root/lib/liba.so"
+cp "$TMP/libb.so" "$TMP/root/lib/libb.so"
+cp "$TMP/liba.so" "$TMP/root/lib/libpick.so"
+cp "$TMP/libb.so" "$TMP/first/libpick.so"
+cp "$TMP/liba.so" "$TMP/first/libfirst.so"
+cp "$TMP/liba.so" "$TMP/first/liborder.so"
+cp "$TMP/libb.so" "$TMP/second/libpick.so"
+cp "$TMP/libb.so" "$TMP/second/libsecond.so"
+cp "$TMP/libb.so" "$TMP/second/liborder.so"
+
+# A bare preload name is resolved through <sys-root>/lib.
+[ "`"$CPU_RUN" --sys-root="$TMP/root" --int-reg=r64@0x00018 \
+ --ldpreload=liba.so "$TMP/main-program"`" = 42 ]
+
+# Explicit --library-path directories have priority over the system tree.
+[ "`"$CPU_RUN" --sys-root="$TMP/root" --library-path="$TMP/first" \
+ --int-reg=r64@0x00018 --ldpreload=libpick.so "$TMP/main-program"`" = 43 ]
+
+# Colon-list order is the actual search order.
+[ "`"$CPU_RUN" --sys-root="$TMP/root" \
+ --library-path="$TMP/first:$TMP/second" --int-reg=r64@0x00018 \
+ --ldpreload=liborder.so "$TMP/main-program"`" = 42 ]
+[ "`"$CPU_RUN" --sys-root="$TMP/root" \
+ --library-path="$TMP/second:$TMP/first" --int-reg=r64@0x00018 \
+ --ldpreload=liborder.so "$TMP/main-program"`" = 43 ]
+
+# Repeated --library-path occurrences append in command-line order.
+cat > "$TMP/run-search.expected" <<EOT
+libraries:
+ $TMP/first
+ $TMP/second
+ $TMP/root/lib
+EOT
+"$CPU_RUN" --sys-root="$TMP/root" --library-path="$TMP/first" \
+ --library-path "$TMP/second" --print-search-dirs > "$TMP/run-search.txt"
+cmp "$TMP/run-search.expected" "$TMP/run-search.txt"
+
+# mcpu-ld uses the same common search state through -L.
+cat > "$TMP/ld-search.expected" <<EOT
+libraries:
+ $TMP/first
+ $TMP/second
+ $TMP/root/lib
+EOT
+"$CPU_LD" --sys-root="$TMP/root" -L"$TMP/first" -L "$TMP/second" \
+ --print-search-dirs > "$TMP/ld-search.txt"
+cmp "$TMP/ld-search.expected" "$TMP/ld-search.txt"
+
+# Explicit pathnames keep the historical behavior and bypass name lookup.
+[ "`"$CPU_RUN" --sys-root="$TMP/root" --library-path="$TMP/first" \
+ --int-reg=r64@0x00018 --ldpreload="$TMP/root/lib/liba.so" \
+ "$TMP/main-program"`" = 42 ]
+
+# Resolving a list does not change the established rightmost preload priority.
+[ "`"$CPU_RUN" --sys-root="$TMP/root" --int-reg=r64@0x00018 \
+ --ldpreload=liba.so:libb.so "$TMP/main-program"`" = 43 ]
+[ "`"$CPU_RUN" --sys-root="$TMP/root" --int-reg=r64@0x00018 \
+ --ldpreload=libb.so:liba.so "$TMP/main-program"`" = 42 ]
+
+# The configured system path participates in real name lookup.
+cp "$TMP/liba.so" "$TMP/user/lib/libuser.so"
+cat > "$HOME/.mcpu/etc/mcpu-ld.conf" <<EOC
+MCPU_LD_SYSTEM_LIBRARY_PATH = $TMP/user/lib;
+EOC
+[ "`"$CPU_RUN" --int-reg=r64@0x00018 --ldpreload=libuser.so \
+ "$TMP/main-program"`" = 42 ]
+
+# An explicitly empty system path has no fallback, while an explicit path works.
+printf '%s\n' 'MCPU_LD_SYSTEM_LIBRARY_PATH = ;' \
+ > "$HOME/.mcpu/etc/mcpu-ld.conf"
+if "$CPU_RUN" --ldpreload=liba.so "$TMP/main-program" >/dev/null 2>&1; then
+ echo "bare preload unexpectedly resolved with empty system path" >&2
+ exit 1
+fi
+[ "`"$CPU_RUN" --library-path="$TMP/root/lib" --int-reg=r64@0x00018 \
+ --ldpreload=liba.so "$TMP/main-program"`" = 42 ]
+
+# Missing names and empty path-list elements are errors.
+if "$CPU_RUN" --sys-root="$TMP/root" --ldpreload=does-not-exist.so \
+ "$TMP/main-program" >/dev/null 2>&1; then
+ echo "missing preload name unexpectedly resolved" >&2
+ exit 1
+fi
+if "$CPU_RUN" --sys-root="$TMP/root" --library-path="$TMP/first::$TMP/second" \
+ --print-search-dirs >/dev/null 2>&1; then
+ echo "empty --library-path element unexpectedly accepted" >&2
+ exit 1
+fi
+
+# 0.2.15 exposes -l for real link-time dependency resolution. Search-dir
+# diagnostics remain independent of whether a later link uses library operands.
+"$CPU_LD" --help 2>&1 | grep -q -- '-l <name>'
+
+"$CPU_RUN" --help 2>&1 | grep -q -- '--library-path'
+"$CPU_LD" --help 2>&1 | grep -q -- '-L <dir>'
+
+exit 0