Blob Blame History Raw
#!/bin/bash

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

rm -f hello.o hello2.o hello3.o libhello.so missing-notes-test.exe

GCC=${GCC:-gcc}
ANNOCHECK=${ANNOCHECK:-../annocheck/annocheck}
PLUGIN=${PLUGIN:-../gcc-plugin/.libs/annobin.so}

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

# FIXME: Horrible hack time.  This test passes on the i686 target, when it
# should fail.  The reason is that the linker places a __x86.get_pc_thunk
# stub function at the end of the compiled hello2.o file.  Annochecks picks
# up on this, and decides that it can ignore the gap.  In real i686 binaries
# this is a good thing.  In our test code it is bad.  So in order to trigger
# a failure on the i686 target, without affecting other targets, I have
# dropped -mstack-realign from EXTRA_OPTS.  Annocheck will detect this and
# report a failure...
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 -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 
	    if [ $? != 0 ];
	    then
		echo "Failed :-("
		exit 1
	    fi
	fi
    fi
fi

$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $EXTRA_OPTS $srcdir/hello.c 

# NB/ This file is compiled without the plugin enabled!
$GCC $OPTS $EXTRA_OPTS $srcdir/hello2.c 

$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $EXTRA_OPTS $srcdir/hello3.c

$GCC -fplugin=$PLUGIN $PLUGIN_OPTS $OPTS $EXTRA_OPTS -shared $srcdir/hello_lib.c -o libhello.so

$GCC -L . -pie -Wl,-z,now -Wl,-z,relro \
     hello.o hello2.o hello3.o -lhello -o missing-notes-test.exe

# Debugging...
# readelf --wide --notes --sections --syms missing-notes-test.exe

# FIXME - we should check that the notes were parsed correctly...
$ANNOCHECK -v missing-notes-test.exe