#!/bin/bash

# Copyright (c) 2021-2026 Red Hat.
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your
# option) any later version.
#
# It is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# Tests that the special file ldconfig can pass annocheck

TEST_NAME=ldconfig
. $srcdir/common.sh

uname -m > host
grep -q -i -e "riscv" host
if [ $? == 0 ];
then
    echo "$TEST_NAME: SKIP: On Risc-V machines ldconfig is a script not a binary"
    cat host
    exit $EXIT_TEST_SKIPPED
fi

SKIPS="--skip-cf-protection --skip-property-note --skip-stack-realign --skip-entry --skip-dynamic-tags --skip-not-dynamic-tags --skip-branch-protection --skip-not-branch-protection --skip-lto --skip-pie --suppress-version-warnings"

LDCONFIG=/sbin/ldconfig
DEBUG=/usr/lib/debug/sbin/ldconfig.debug

start_test

if [ -f $LDCONFIG ];
then
    # The /sbin/ldconfig file on the s390x is a symbolic link,
    # hence the --follow-links option.
    A_OPTS="--follow-links --ignore-gaps"

    if [ -f $DEBUG ];
    then
	A_OPTS+="--debug-file $DEBUG"
    fi

    $ANNOCHECK $LDCONFIG $SKIPS $A_OPTS  > $A_OUT

    grep -q -e "PASS" $A_OUT
    if [ $? != 0 ];
    then
        echo " $TEST_NAME: FAIL: ldconfig did not pass annocheck"
        cat $A_OUT
	end_test
        exit $EXIT_TEST_FAILED
    fi
else
    echo " $TEST_NAME: SKIP: $LDCONFIG file not found"
    end_test
    exit $EXIT_TEST_SKIPPED
fi

end_test
