# Expect script for binutils tests
# Copyright (C) 2006-2020 Free Software Foundation, Inc.
#
# This file is part of the GNU Binutils.
#
# This program 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 of the License, or
# (at your option) any later version.
#
# This program 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Written by H.J. Lu (hongjiu.lu@intel.com)
#

# Make sure that binutils can correctly handle ld output in ELF.

if { !([istarget *-*-linux*]
       || [istarget arm*-*-uclinuxfdpiceabi]
       || [istarget *-*-nacl*]
       || [istarget *-*-gnu*]) || [istarget *ecoff] } then {
    return
}

# The optional test_name argument provides a mechanism for the caller
# to hardwire the test name. This is important if ld_options contains
# absolute path names because the default test name is constructed
# from the prog_name and ld_options and we do not want absolute paths
# to appear in the test_name.  The optional readelf_options can be
# used to specify different options for readelf.
proc binutils_test { prog_name ld_options test {test_name ""} {readelf_options "-l"}} {
    global as
    global ld
    global READELF
    global objcopy
    global strip
    global srcdir
    global subdir
    global link_output

    eval set prog \$$prog_name

    if { "$test_name" == "" } {
      set test_name "$prog_name $ld_options ($test)"
    }

    if { ![ld_assemble $as $srcdir/$subdir/$test.s tmpdir/$test.o ] } {
	unresolved "$test_name"
	return
    }

    if { ![ld_link $ld tmpdir/$test "$ld_options -z stack-size=0 tmpdir/$test.o"] } {
	if { [string match "*not supported*" $link_output]
	     || [string match "*unrecognized option*" $link_output]
	     || [string match "*-z relro ignored*" $link_output] } {
	    unsupported "$ld_options is not supported by this target"
	} else {
	    unresolved "$test_name"
	}
	return
    }

    send_log "$READELF $readelf_options --wide tmpdir/$test > tmpdir/$test.exp\n"
    set got [remote_exec host "$READELF $readelf_options --wide tmpdir/$test" "" "/dev/null" "tmpdir/$test.exp"]
    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	send_log "$got\n"
	unresolved "$test_name"
	return
    }

    if [string match "*-z relro*" $ld_options] {
	# These targets set TEXT_DYNAMIC and therefore have `.dynamic'
	# in the (read-only) text segment rather than data segment.
	set relro_dynamic [expr !([istarget "hppa64*-*-*"] \
				  || ([istarget "mips*-*-*"] \
				      && ![istarget "*-*-vxworks*"]))]
	# These targets have `.got' in regular data even though they do
	# not have `.got' there.
	set relro_gotplt [expr [istarget "mips*-*-*"]]
	# These targets do not set NO_SMALL_DATA or DATA_GOT and therefore
	# have `.got' in (read-write) small data rather than regular data.
	set relro_got [expr !([istarget "alpha*-*-*"] \
			      || [istarget "hppa*-*-linux-*"] \
			      || [istarget "hppa*-*-netbsd*"] \
			      || [istarget "hppa*-*-openbsd*"] \
			      || [istarget "metag-*-*"] \
			      || [istarget "mips*-*-*"] \
			      || [istarget "nios2*-*-*"] \
			      || [istarget "or1k-*-*"] \
			      || [istarget "riscv*-*-*"] \
			      || [istarget "sh*-*-*"] \
			      || [istarget "x86_64-*-rdos*"])]
	# Check if GNU_RELRO segment is generated.
	set got [remote_exec host "grep GNU_RELRO tmpdir/$test.exp"]
	if { ![string match "*GNU_RELRO*" $got] } then {
	    set got [remote_exec host "cat tmpdir/$test.exp"]
	    if { [string match "*.data.rel.ro*" $got]
		 || ($relro_dynamic && [string match "*.dynamic*" $got])
		 || ($relro_gotplt && [string match "*.got.plt*" $got])
		 || ($relro_got && [string match "*.got*" $got])
		 || [string match "*.eh_frame*" $got]
		 || [string match "*.gcc_except_table*" $got]
		 || [string match "*.exception_ranges*" $got]
		 || [string match "*.ctors*" $got]
		 || [string match "*.dtors*" $got]
		 || [string match "*.tdata*" $got]
		 || [string match "*.preinit_array*" $got]
		 || [string match "*.init_array*" $got]
		 || [string match "*.fini_array*" $got] } then {
		# Fail if GNU_RELRO segment isn't generated, but should.
		fail "$test_name"
		return
	    }
	}
    }

    send_log "$prog tmpdir/$test\n"
    set got [remote_exec host "$prog tmpdir/$test"]
    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	send_log "$got\n"
	fail "$test_name"
	return
    }

    send_log "$READELF $readelf_options --wide tmpdir/$test > tmpdir/$test.out\n"
    set got [remote_exec host "$READELF $readelf_options --wide tmpdir/$test" "" "/dev/null" "tmpdir/$test.out"]
    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	send_log "$got\n"
	unresolved "$test_name"
	return
    }

    if { [catch {exec cmp tmpdir/$test.exp tmpdir/$test.out}] } then {
	send_log "tmpdir/$test.exp tmpdir/$test.out differ.\n"
	fail "$test_name"
	return
    }

    pass "$test_name"
}

binutils_test strip "-z max-page-size=0x200000" maxpage1
binutils_test strip "-z max-page-size=0x200000 -z common-page-size=0x100000" maxpage1
binutils_test strip "-z max-page-size=0x100000" maxpage1
binutils_test strip "-z max-page-size=0x100000 -z common-page-size=0x1000" maxpage1

binutils_test strip "" maxpage1
binutils_test strip "-shared" maxpage1
binutils_test objcopy "" maxpage1
binutils_test objcopy "-shared" maxpage1

binutils_test strip "-z relro" relro1
binutils_test strip "-z relro -shared" relro1
binutils_test objcopy "-z relro" relro1
binutils_test objcopy "-z relro -shared" relro1
if { ([istarget "i?86-*-elf*"]
      || [istarget "i?86-*-linux*"]
      || [istarget "i?86-*-gnu*"]
      || [istarget "x86_64-*-linux*"]
      || [istarget "amd64-*-linux*"]) } {
    binutils_test strip "-z relro -shared" relro2
    binutils_test objcopy "-z relro -shared" relro2
}

binutils_test strip "-T ${srcdir}/${subdir}/lma.lnk" lma "strip -T lma.lnk"

set tls_tests { "tdata1" "tdata2" }
# hppa64 has its own .tbss section, with different flags.
if { ![istarget "hppa64-*-*"] } {
    lappend tls_tests "tdata3" "tbss1" "tbss2" "tbss3"
}
set tls_opts {
    "-z noseparate-code"
    "-z separate-code"
    "-z relro"
    "-shared -z noseparate-code"
    "-shared -z separate-code"
    "-shared -z relro"
    "-z max-page-size=0x100000"
    "-z max-page-size=0x100000 -z common-page-size=0x1000"
}

foreach testitem $tls_tests {
    foreach testopt $tls_opts {
	binutils_test objcopy $testopt $testitem
    }
}

binutils_test strip "-s" gap "" "-lS"