#!/bin/bash

# Copyright (c) 2017-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_NAME=function-sections
. $srcdir/common.sh

PLUGIN_OPTS="-fplugin-arg-annobin-no-attach"
OPTS="-Wformat -Werror=format-security -Wall -c -O2 -D_FORTIFY_SOURCE=2 -fPIE -fstack-protector-strong -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -fno-lto"
EXTRA_OPTS="-fcf-protection -mstackrealign"

start_test

$GCC -fplugin=$PLUGIN $PLUGIN_OPTS -g $OPTS $EXTRA_OPTS -o hello_hard.o $srcdir/hello_hard.c
if [ $? != 0 ];
then
    echo "Compiler might not support -fcf-protection, retrying without it"
    EXTRA_OPTS="-mstackrealign"
    $GCC -fplugin=$PLUGIN $PLUGIN_OPTS -g $OPTS  $EXTRA_OPTS $srcdir/hello_hard.c
    if [ $? != 0 ];
    then
	echo "Compiler might not support -mstackrealign, retrying without it"
	EXTRA_OPTS="-fcf-protection"
	$GCC -fplugin=$PLUGIN $PLUGIN_OPTS -g $OPTS $EXTRA_OPTS $srcdir/hello_hard.c
	if [ $? != 0 ];
	then
	    echo "Compiler might not support either -fcf-protection or -mstackrealign, retrying without both"
	    EXTRA_OPTS=""
	    $GCC -fplugin=$PLUGIN $PLUGIN_OPTS -g $OPTS $EXTRA_OPTS $srcdir/hello_hard.c > $G_OUT 2>&1
	    if [ $? != 0 ];
	    then
		echo "$TEST_NAME: SKIP: Could not compile test sources"
		cat $G_OUT
		end_test
		exit $EXIT_TEST_SKIPPED
	    fi
	fi
    fi
fi


COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $EXTRA_OPTS -ffunction-sections $srcdir/unused_code.c"
$COMMAND 2>$G_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: unable to compile test file unused_code.c"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_SKIPPED
fi

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $EXTRA_OPTS $srcdir/hello.c"
$COMMAND 2>$G_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: unable to compile test file hello.c"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_SKIPPED
fi

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $EXTRA_OPTS -ffunction-sections $srcdir/hello2.c"
$COMMAND 2>$G_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: unable to compile test file hello2.c"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_SKIPPED
fi

EXE=function-sections-test.exe

# For extra debugging add these to linker command line below:
#     -Wl,--print-gc-sections \
#     -Wl,--orphan-handling=warn \

COMMAND="$GCC -pie \
     -Wl,-z,now,-z,relro \
     -Wl,--gc-sections \
     -Wl,-Map,function-sections.map \
 hello.o hello2.o unused_code.o -o $EXE"
$COMMAND 2>$G_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: unable to link test objects"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_SKIPPED
fi

A_OUT=function-sections.out
A_COMMAND="$ANNOCHECK -v --ignore-gaps --skip-lto --skip-entry $EXE --suppress-version-warnings"
$A_COMMAND > $A_OUT

grep -q -e"FAIL: optimization test" $A_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: annocheck did not FAIL for executable compiled with one function lacking proper optimization level"
    echo " $TEST_NAME: annocheck command: $A_COMMAND"
    echo " $TEST_NAME: annocheck output:"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

end_test
