#!/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=lto
. $srcdir/common.sh

PLUGIN_OPTS="-fplugin-arg-annobin-no-attach"
OPTS="-g -c -O2 -fPIE -flto -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -fstack-clash-protection"

start_test

$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $srcdir/hello.c
mv hello.o lto-hello.o
$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $srcdir/hello2.c
$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $srcdir/hello3.c
$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS -shared $srcdir/hello_lib.c -o libhello.so

EXE=lto-test.exe

# Link without system files as these may not have been hardened.
$GCC -fplugin=$PLUGIN $PLUGIN_OPTS \
    -flto \
    -pie \
  lto-hello.o hello2.o hello3.o -L. -lhello -o $EXE
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: unable to compile test files"
    end_test
    exit $EXIT_TEST_SKIPPED
fi

A_OUT=lto-test.out
# Run annocheck, but only enable the LTO test.

$ANNOCHECK -v --ignore-gaps --skip-all --test-lto --fixed-format-messages --suppress-version-warnings $EXE > $A_OUT

grep -q -e"PASS: test: lto" $A_OUT
if [ $? != 0 ];
then
    echo "$TEST_NAME: FAIL: did not detect LTO compilation"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

end_test
