#! /bin/sh
set -e

if [ "${AUTOPKGTEST_TMP}" = "" ]
then
	AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
	# shellcheck disable=SC2064
	trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi
cd "$AUTOPKGTEST_TMP"

ROCM_CLANG_PREFIX="/usr/lib/rocm/llvm/bin"

# Verify that binaries linked with the bundled clang+lld carry a
# usable dynamic-lookup hash table. Debian/Ubuntu defaults to
# --hash-style=gnu, so we expect GNU_HASH (SysV .hash is dead weight on
# modern glibc and is intentionally not emitted). The previously-disabled
# 0011-fixup-hash-issues.patch tried to force both styles unconditionally,
# which is stricter than the distro default and not required.
# This test fails loudly if a future change accidentally produces a
# binary with no hash table at all.

cat > hello.c << END
int main(void) { return 0; }
END

echo "$ ${ROCM_CLANG_PREFIX}/clang -fuse-ld=lld hello.c -o hello"
${ROCM_CLANG_PREFIX}/clang -fuse-ld=lld hello.c -o hello

echo "$ readelf -d hello"
readelf -d hello | tee dyn.log

if grep -qE "GNU_HASH|\(HASH\)" dyn.log; then
    echo "ok: binary has a dynamic-lookup hash table"
else
    echo "FAIL: binary has neither GNU_HASH nor SysV HASH"
    exit 1
fi
