#!/bin/bash

# Copyright (c) 2018-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.


# Test running the section size checker

TEST_NAME=section-size
. $srcdir/common.sh

PLUGIN_OPTS="-fplugin-arg-annobin-no-attach -fplugin-arg-annobin-note-format=note"

start_test

$GCC -fplugin=$PLUGIN $PLUGIN_OPTS -c $srcdir/hello.c $srcdir/hello2.c $srcdir/hello3.c $srcdir/hello_lib.c
$GCC hello.o hello2.o hello3.o hello_lib.o -o $EXE > $G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: Could not compile source files"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_SKIPPED
fi

SEC=.gnu.build.attributes

$ANNOCHECK -v --disable-hardened --sec=$SEC --human --ignore-unknown $EXE > $A_OUT
grep -q -e"$SEC" $A_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: annocheck did not report section size"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

echo " $TEST_NAME: PASS: annocheck reported the size of section $SEC"

end_test
