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