#!/bin/bash
# Copyright © 2026 Collabora Ltd.
# SPDX-License-Identifier: MIT

# usage: tests/assert-correct-ldso.sh "$DEB_HOST_ARCH" BINARY [BINARY...]
# Assert that the ELF interpreter of each BINARY is appropriate
# for the architecture.

set -eux -o pipefail

case "$1" in
    (amd64)
        ldso=/lib64/ld-linux-x86-64.so.2
        ;;
    (i386)
        ldso=/lib/ld-linux.so.2
        ;;
    (arm64)
        ldso=/lib/ld-linux-aarch64.so.1
        ;;
    (*)
        echo "Unknown architecture $1" >&2
        exit 1
        ;;
esac

shift

export LC_ALL=C
failed=0

for binary in "$@"; do
    if readelf -l -- "$binary" | grep -F "[Requesting program interpreter: $ldso]"; then
        echo "ELF interpreter of $binary is $ldso as expected" >&2
    else
        echo "ELF interpreter of $binary is not as expected" >&2
        readelf -l -- "$binary" >&2
        failed=1
    fi
done

exit "$failed"

# vim:set sw=4 sts=4 et ft=sh:
