1# Copyright (C) 2014-2020 Free Software Foundation, Inc.
2#
3# This file is part of the GNU Binutils.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18# MA 02110-1301, USA.
19
20# Check that scripts using the "=" sysroot-prefix work, for both
21# toolchains with and without --sysroot support.
22
23# We test this by emitting archives into a subdirectory and expect
24# constructs such as GROUP and AS_NEEDED (the only two constructs
25# actually tested) to find them (or not); both quoted and unquoted
26# paths, with different prefixes on the path and with --sysroot
27# present or not, with different arguments.
28
29# Find out if the linker supports sysroot; if it was configured
30# "--with-sysroot X" where X is a non-empty string.
31set with_sysroot [check_sysroot_available]
32verbose -log "Has (non-empty) sysroot support: $with_sysroot; \"$ld_sysroot\""
33
34# We also need to know if the sysroot is "/" (a common use) as some of
35# the tests prepend sysroot to the current directory and on most
36# common systems "//dir/path" is handled as "/dir/path".
37if {$ld_sysroot == "/"} {
38    # Use a modified test-subset for testing.
39    set with_sysroot 3
40}
41
42# The linker on Windows expects Windows style paths.
43# MSYS2 runtime converts paths in the command line automatically.
44# However, for our test linker scripts, we have to prepare
45# the correct paths manually.
46proc get_base_dir_for_scripts {} {
47    global base_dir
48    global get_base_dir_for_scripts_saved
49
50    if { ![info exists get_base_dir_for_scripts_saved] } {
51	set get_base_dir_for_scripts_saved $base_dir
52	if { ([ishost *-*-cygwin] || [ishost *-*-mingw*] )
53	     && ![catch "exec cygpath -m $base_dir" base_dir_converted] } {
54	    set get_base_dir_for_scripts_saved $base_dir_converted
55	}
56    }
57    return $get_base_dir_for_scripts_saved
58}
59
60# Entries in the array-tables:
61# 0: Testtype; an inclusive bitmask indicating that the test should run on a
62# build configured for: 1: non-sysroot, 2: sysroot != "/", 4: sysroot == "/".
63# 1: Description, forming part of the dejagnu test-name.
64# 2: Replacement for @p@.
65# 3: Option to pass to linker (usually something with --sysroot).
66# 4: Message substring; a substring to match against the error message
67# if an error is expected, or empty if no error is expected.
68#
69# If the replacement or option contains @cwd@, that'll be replaced by
70# "$base_dir/tmpdir", the full path to the location of the script
71# (with the actual files in the "sysroot" subdirectory).  If the
72# description contains @cwd@, that will be replaced by "<CWD>".
73
74set sysroot_prefix_tests {
75    {7 "plain -Lpath" "sysroot/" {} ""}
76    {7 "root-anchored but -Lpath" "/sysroot/" {} "cannot find"}
77    {7 "full-path" "@cwd@/sysroot/" {} ""}
78    {7 "root-anchored =-prefixed -Lpath" "=/sysroot/" {} "cannot find"}
79    {7 "root-anchored $SYSROOT-prefixed -Lpath" "$SYSROOT/sysroot/" {} "cannot find"}
80    {7 "plain =-prefixed with empty" "=sysroot/" "--sysroot=" ""}
81    {7 "plain $SYSROOT-prefixed with empty" "$SYSROOTsysroot/" "--sysroot=" ""}
82    {6 "root-anchored but script outside sysroot" "/" "--sysroot=@cwd@/sysroot" "cannot find"}
83    {6 "root-anchored and script inside sysroot" "/sysroot/" "--sysroot=@cwd@" ""}
84    {6 "root-anchored =-prefixed script outside" "=/" "--sysroot=@cwd@/sysroot" ""}
85    {6 "root-anchored $SYSROOT-prefixed script outside" "$SYSROOT/" "--sysroot=@cwd@/sysroot" ""}
86    {6 "root-anchored =-prefixed script inside" "=/sysroot/" "--sysroot=@cwd@" ""}
87    {6 "root-anchored $SYSROOT-prefixed script inside" "$SYSROOT/sysroot/" "--sysroot=@cwd@" ""}
88    {2 "plain =-prefixed without but -Lpath" "=sysroot/" {} "cannot find"}
89    {2 "plain $SYSROOT-prefixed without but -Lpath" "$SYSROOTsysroot/" {} "cannot find"}
90    {2 "full-path =-prefixed without" "=@cwd@/sysroot/" {} "cannot find"}
91    {2 "full-path $SYSROOT-prefixed without" "$SYSROOT@cwd@/sysroot/" {} "cannot find"}
92    {1 "plain =-prefixed -Lpath" "=sysroot/" {} ""}
93    {1 "plain $SYSROOT-prefixed -Lpath" "$SYSROOTsysroot/" {} ""}
94    {1 "full-path =-prefixed without" "=@cwd@/sysroot/" {} ""}
95    {1 "full-path $SYSROOT-prefixed without" "$SYSROOT@cwd@/sysroot/" {} ""}
96}
97
98# May have to provide a target-specific assembler option for some targets.
99set gasopt ""
100
101# Intentionally similar to the ubiquitous glibc libc.so script.
102set template "GROUP ( @q@@p@tmp/ldtest-xyzzy/libx.a@q@ AS_NEEDED ( @q@@p@tmp/ldtest-xyzzy/liby.a@q@ ) )"
103
104# Set up everything from the variables above.
105proc sysroot_prefix_test_setup { } {
106    global as gasopt srcdir subdir ar
107
108    if {![ld_assemble_flags $as $gasopt $srcdir/$subdir/pr14962a.s tmpdir/main.o]} {
109	perror "Error assembling a trivial file for sysroot-prefix tests framework"
110	return 0
111    }
112
113    # We need somewhere under tmpdir to point the sysroot, a subdirectory
114    # that is benevolent if it escapes into "/".
115    remote_exec host "mkdir -p tmpdir/sysroot/tmp/ldtest-xyzzy"
116
117    # 0: a "main" object that needs a symbol (x) (most portably by
118    # using a pre-existing file).  1: a library with an object that
119    # provides that symbol and needs another one (y).  2: another
120    # library with a third object providing that other symbol.
121    set sysroot_prefix_tests_framework_objects  {
122	{"pr14962a.s" "main" ""}
123	{"sysroot-prefix-x.s" "x" "x"}
124	{"sysroot-prefix-y.s" "y"  "y"}
125    }
126
127    foreach test_object $sysroot_prefix_tests_framework_objects {
128	set sname [lindex $test_object 0]
129	set onamebase [lindex $test_object 1]
130	set oname "tmpdir/$onamebase.o"
131	set libnamebase [lindex $test_object 2]
132
133	if ![ld_assemble_flags $as $gasopt $srcdir/$subdir/$sname $oname] {
134	    perror "Error assembling trivial file $sname for sysroot-prefix tests framework"
135	    return 0
136	}
137
138	if { [string length $libnamebase] != 0 &&
139	     ![ar_simple_create $ar "" tmpdir/sysroot/tmp/ldtest-xyzzy/lib$libnamebase.a $oname] } {
140	    perror "Error creating archive $libnamebase for sysroot-prefix tests framework"
141	    return 0
142	}
143    }
144
145    return 1
146}
147
148# Run a single linker test.
149proc single_sysroot_prefix_test { type xtestname finalscript ldopt errstr } {
150    global ld exec_output with_sysroot
151    set scriptname "tmpdir/libsysroottest.a"
152    set testname "sysroot-prefix $xtestname"
153
154    if { ($type & ($with_sysroot + 1)) == 0 } {
155	unsupported $testname
156	return
157    }
158
159    if [catch { set ofd [open $scriptname w] } x] {
160	perror "$x"
161	unresolved $testname
162	return
163    }
164
165    puts $ofd "$finalscript"
166    close $ofd
167    verbose -log "script $scriptname: $finalscript"
168
169    set res [ld_link $ld tmpdir/output "$ldopt tmpdir/main.o -Ltmpdir -lsysroottest"]
170    set ld_output "$exec_output"
171    set expect_success [expr [string length $errstr] == 0]
172
173    if { $res == $expect_success &&
174         ($expect_success || [regexp "$errstr" $ld_output]) } {
175	pass $testname
176	catch "exec rm -f $scriptname"
177    } {
178	fail $testname
179    }
180}
181
182# Run all interesting variants from an option-and-path combination.
183proc run_sysroot_prefix_test { type name templ p ldopt errstr } {
184    global base_dir
185    set qlist { { "quoted" "\"" } { "unquoted" {} } }
186
187    regsub -all "@p@" $templ $p templ
188    regsub -all "@cwd@" $templ "[get_base_dir_for_scripts]/tmpdir" templ
189    regsub -all "@cwd@" $ldopt "$base_dir/tmpdir" ldopt
190    regsub -all "@cwd@" $name "<CWD>" name
191
192    foreach qitems $qlist {
193	regsub -all "@q@" $templ [lindex $qitems 1] finalscript
194	single_sysroot_prefix_test $type "$name, [lindex $qitems 0]" \
195		$finalscript $ldopt $errstr
196    }
197}
198
199# Run a list of option-and-path test-combinations.
200proc run_sysroot_prefix_tests { descr templ items } {
201    foreach item $items {
202	set type [lindex $item 0]
203	set name [lindex $item 1]
204	set p [lindex $item 2]
205	set ldopt [lindex $item 3]
206	set errstr [lindex $item 4]
207	run_sysroot_prefix_test $type "$descr $name" $templ "$p" "$ldopt" "$errstr"
208    }
209}
210
211if ![sysroot_prefix_test_setup] {
212    return
213}
214
215run_sysroot_prefix_tests "common" $template $sysroot_prefix_tests
216