]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/testsuite/ld-plugin/plugin.exp
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / testsuite / ld-plugin / plugin.exp
CommitLineData
5d3236ee 1# Expect script for ld-plugin tests
a2c58332 2# Copyright (C) 2010-2022 Free Software Foundation, Inc.
5d3236ee
DK
3#
4# This file is part of the GNU Binutils.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
20
21# These tests require the plugin API to be configured in.
22if ![check_plugin_api_available] {
23 return
24}
25
c3842ce5
DK
26# And a compiler to be available.
27set can_compile 1
44ed8092 28if { ![check_compiler_available] } {
c3842ce5
DK
29 # Don't fail immediately,
30 set can_compile 0
31}
32
5d3236ee
DK
33pass "plugin API enabled"
34
5d3236ee
DK
35# Look for the name we can dlopen in the test plugin's libtool control script.
36set plugin_name [file_contents "$base_dir/libldtestplug.la"]
37set plugin_name [regsub "'.*" [regsub ".*dlname='" "$plugin_name" ""] ""]
760f6ee8
NC
38# Even though the API supports plugins it does not mean that the
39# linker was configured with --enable-plugins. Check for that here.
40if { $plugin_name == "" } {
41 verbose "The linker is not configured to support plugins"
42 return
43}
5d3236ee
DK
44verbose "plugin name is '$plugin_name'"
45
439b7f41
L
46set plugin2_name [file_contents "$base_dir/libldtestplug2.la"]
47set plugin2_name [regsub "'.*" [regsub ".*dlname='" "$plugin2_name" ""] ""]
48verbose "plugin2 name is '$plugin2_name'"
49
50set plugin3_name [file_contents "$base_dir/libldtestplug3.la"]
51set plugin3_name [regsub "'.*" [regsub ".*dlname='" "$plugin3_name" ""] ""]
52verbose "plugin3 name is '$plugin3_name'"
53
3355cb3b
L
54set plugin4_name [file_contents "$base_dir/libldtestplug4.la"]
55set plugin4_name [regsub "'.*" [regsub ".*dlname='" "$plugin4_name" ""] ""]
56verbose "plugin4 name is '$plugin4_name'"
57
5d3236ee
DK
58# Use libtool to find full path to plugin rather than worrying
59# about run paths or anything like that.
60catch "exec $base_dir/libtool --config" lt_config
61verbose "Full lt config: $lt_config" 3
62# Look for "objdir=.libs"
63regexp -line "^objdir=.*$" "$lt_config" lt_objdir
64verbose "lt_objdir line is '$lt_objdir'" 3
65set lt_objdir [regsub "objdir=" "$lt_objdir" ""]
66set plugin_path "$base_dir/$lt_objdir/$plugin_name"
439b7f41
L
67set plugin2_path "$base_dir/$lt_objdir/$plugin2_name"
68set plugin3_path "$base_dir/$lt_objdir/$plugin3_name"
3355cb3b 69set plugin4_path "$base_dir/$lt_objdir/$plugin4_name"
5d3236ee 70verbose "Full plugin path $plugin_path" 2
439b7f41
L
71verbose "Full plugin2 path $plugin2_path" 2
72verbose "Full plugin3 path $plugin3_path" 2
3355cb3b 73verbose "Full plugin4 path $plugin4_path" 2
5d3236ee 74
5d3236ee
DK
75set regclm "-plugin-opt registerclaimfile"
76set regas "-plugin-opt registerallsymbolsread"
f58926a6 77set regassilent "-plugin-opt registerallsymbolsreadsilent"
5d3236ee
DK
78set regcln "-plugin-opt registercleanup"
79
80# In order to define symbols in plugin options in the list of tests below,
81# we need to know if the platform prepends an underscore to C symbols,
82# which we find out by compiling the test objects now. If there is any
83# error compiling, we defer reporting it until after the list of tests has
84# been initialised, so that we can use the names in the list to report;
85# otherwise, we scan one of the files with 'nm' and look for a known symbol
86# in the output to see if it is prefixed or not.
87set failed_compile 0
88set _ ""
89set plugin_nm_output ""
ad77db1c
AM
90set old_CFLAGS "$CFLAGS_FOR_TARGET"
91append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
92if { [istarget m681*-*-*] || [istarget m68hc1*-*-*] || [istarget m9s12x*-*-*] } {
93 # otherwise get FAILS due to _.frame
94 append CFLAGS_FOR_TARGET " -fomit-frame-pointer"
95}
16f9c644 96
c3842ce5 97if { $can_compile && \
669d5115
AM
98 (![ld_compile $CC_FOR_TARGET $srcdir/$subdir/main.c tmpdir/main.o] \
99 || ![ld_compile $CC_FOR_TARGET $srcdir/$subdir/func.c tmpdir/func.o] \
100 || ![ld_compile $CC_FOR_TARGET $srcdir/$subdir/text.c tmpdir/text.o] \
101 || ![ld_compile $CC_FOR_TARGET $srcdir/$subdir/pr20070a.c tmpdir/pr20070a.o] \
102 || ![ld_compile $CC_FOR_TARGET $srcdir/$subdir/dummy.s tmpdir/dummy.o] \
103 || ![ld_compile $CC_FOR_TARGET $srcdir/$subdir/pr17973.s tmpdir/pr17973.o]) } {
5d3236ee
DK
104 # Defer fail until we have list of tests set.
105 set failed_compile 1
c3842ce5
DK
106}
107
4e95fbcd 108set dotsym 0
c3842ce5 109if { $can_compile && !$failed_compile } {
5d3236ee
DK
110 # Find out if symbols have prefix on this platform before setting tests.
111 catch "exec $NM tmpdir/func.o" plugin_nm_output
112 if { [regexp "_func" "$plugin_nm_output"] } {
113 set _ "_"
114 }
4e95fbcd
AM
115 if { [regexp "\\.func" "$plugin_nm_output"] } {
116 set dotsym 1
117 }
5d3236ee
DK
118}
119
36fe835f
DK
120set testobjfiles "tmpdir/main.o tmpdir/func.o tmpdir/text.o"
121set testobjfiles_notext "tmpdir/main.o tmpdir/func.o"
439b7f41
L
122set testsrcfiles "tmpdir/main.o $srcdir/$subdir/func.c tmpdir/text.o"
123set testsrcfiles_notext "tmpdir/main.o $srcdir/$subdir/func.c"
36fe835f
DK
124# Rather than having libs we just define dummy values for anything
125# we may need to link a target exe; we aren't going to run it anyway.
d9816402 126set libs "[ld_link_defsyms] --defsym ${_}printf=${_}main --defsym ${_}puts=${_}main"
4e95fbcd
AM
127if { $dotsym } {
128 append libs " --defsym .printf=.main --defsym .puts=.main"
129}
16f9c644
AM
130if [is_pecoff_format] {
131 #otherwise relocs overflow to symbols defined on the command line
132 append libs " --image-base=0x10000000"
133}
36fe835f 134
5d3236ee
DK
135set plugin_tests [list \
136 [list "load plugin" "-plugin $plugin_path \
897aea50 137 $testobjfiles $libs" "" "" "" {{ld plugin-1.d}} "main.x" ] \
5d3236ee 138 [list "fail plugin onload" "-plugin $plugin_path -plugin-opt failonload \
897aea50 139 $testobjfiles $libs" "" "" "" {{ld plugin-2.d}} "main.x" ] \
5d3236ee
DK
140 [list "fail plugin allsymbolsread" "-plugin $plugin_path $regas \
141 -plugin-opt failallsymbolsread \
897aea50 142 $testobjfiles $libs" "" "" "" {{ld plugin-3.d}} "main.x" ] \
5d3236ee
DK
143 [list "fail plugin cleanup" "-plugin $plugin_path -plugin-opt failcleanup \
144 $regcln \
897aea50 145 $testobjfiles $libs" "" "" "" {{ld plugin-4.d}} "main.x" ] \
5d3236ee 146 [list "plugin all hooks" "-plugin $plugin_path $regclm $regas $regcln \
897aea50 147 $testobjfiles $libs" "" "" "" {{ld plugin-5.d}} "main.x" ] \
5d3236ee
DK
148 [list "plugin claimfile lost symbol" "-plugin $plugin_path $regclm \
149 $regas $regcln -plugin-opt claim:tmpdir/func.o \
897aea50 150 $testobjfiles $libs" "" "" "" {{ld plugin-6.d}} "main.x" ] \
5d3236ee
DK
151 [list "plugin claimfile replace symbol" "-plugin $plugin_path $regclm \
152 $regas $regcln -plugin-opt claim:tmpdir/func.o \
153 -plugin-opt sym:${_}func::0:0:0 \
897aea50 154 $testobjfiles $libs" "" "" "" {{ld plugin-7.d}} "main.x" ] \
5d3236ee
DK
155 [list "plugin claimfile resolve symbol" "-plugin $plugin_path $regclm \
156 $regas $regcln -plugin-opt claim:tmpdir/func.o \
157 -plugin-opt sym:${_}func::0:0:0 \
158 -plugin-opt sym:${_}func2::0:0:0 \
159 -plugin-opt dumpresolutions \
897aea50 160 $testobjfiles $libs" "" "" "" {{ld plugin-8.d}} "main.x" ] \
5d3236ee
DK
161 [list "plugin claimfile replace file" "-plugin $plugin_path $regclm \
162 $regas $regcln -plugin-opt claim:tmpdir/func.o \
163 -plugin-opt sym:${_}func::0:0:0 \
164 -plugin-opt sym:${_}func2::0:0:0 \
165 -plugin-opt dumpresolutions \
166 -plugin-opt add:tmpdir/func.o \
897aea50 167 $testobjfiles $libs" "" "" "" {{ld plugin-9.d}} "main.x" ] \
439b7f41
L
168 [list "load plugin with source" "-plugin $plugin_path $regclm \
169 -plugin-opt claim:$srcdir/$subdir/func.c \
170 $testsrcfiles $libs" "" "" "" {{ld plugin-13.d}} "main.x" ] \
171 [list "plugin claimfile lost symbol with source" \
172 "-plugin $plugin_path $regclm $regas $regcln \
173 -plugin-opt claim:$srcdir/$subdir/func.c \
174 $testsrcfiles $libs" "" "" "" {{ld plugin-14.d}} "main.x" ] \
175 [list "plugin claimfile replace symbol with source" \
176 "-plugin $plugin_path $regclm $regas $regcln \
177 -plugin-opt claim:$srcdir/$subdir/func.c \
178 -plugin-opt sym:${_}func::0:0:0 \
179 $testsrcfiles $libs" "" "" "" {{ld plugin-15.d}} "main.x" ] \
180 [list "plugin claimfile resolve symbol with source" \
181 "-plugin $plugin_path $regclm $regas $regcln \
182 -plugin-opt claim:$srcdir/$subdir/func.c \
183 -plugin-opt sym:${_}func::0:0:0 \
184 -plugin-opt sym:${_}func2::0:0:0 \
185 -plugin-opt dumpresolutions \
186 $testsrcfiles $libs" "" "" "" {{ld plugin-16.d}} "main.x" ] \
187 [list "plugin claimfile replace file with source" \
188 "-plugin $plugin_path $regclm $regas $regcln \
189 -plugin-opt claim:$srcdir/$subdir/func.c \
190 -plugin-opt sym:${_}func::0:0:0 \
191 -plugin-opt sym:${_}func2::0:0:0 \
192 -plugin-opt dumpresolutions \
193 -plugin-opt add:tmpdir/func.o \
194 $testsrcfiles $libs" "" "" "" {{ld plugin-17.d}} "main.x" ] \
195 [list "load plugin with source not claimed" "-plugin $plugin_path $regclm \
196 $testsrcfiles $libs" "" "" "" {{ld plugin-26.d}} "main.x" ] \
45e81354
L
197 [list "plugin fatal error" "-plugin $plugin2_path -plugin-opt fatal \
198 $testobjfiles $libs" "" "" "" {{ld plugin-27.d}} "main.x" ] \
199 [list "plugin error" "-plugin $plugin2_path -plugin-opt error \
200 $testobjfiles $libs" "" "" "" {{ld plugin-28.d}} "main.x" ] \
201 [list "plugin warning" "-plugin $plugin2_path -plugin-opt warning \
202 $testobjfiles $libs" "" "" "" {{ld plugin-29.d}} "main.x" ] \
5d3236ee
DK
203]
204
4a85cc09
SKS
205if [check_shared_lib_support] {
206 lappend plugin_tests [list "PR ld/17973" "-plugin $plugin2_path -shared $regassilent \
207 -plugin-opt add:tmpdir/pr17973.o \
208 tmpdir/dummy.o" "" "" "" {{readelf -sW pr17973.d}} "main.x" ]
209}
210
211
5d3236ee
DK
212set plugin_lib_tests [list \
213 [list "plugin ignore lib" "-plugin $plugin_path $regclm \
214 $regas $regcln -plugin-opt claim:tmpdir/func.o \
215 -plugin-opt sym:${_}func::0:0:0 \
216 -plugin-opt sym:${_}func2::0:0:0 \
217 -plugin-opt dumpresolutions \
218 -plugin-opt add:tmpdir/func.o \
897aea50 219 $testobjfiles_notext -Ltmpdir -ltext $libs" "" "" "" {{ld plugin-10.d}} "main.x" ] \
5d3236ee
DK
220 [list "plugin claimfile replace lib" "-plugin $plugin_path $regclm \
221 $regas $regcln -plugin-opt claim:tmpdir/func.o \
222 -plugin-opt sym:${_}func::0:0:0 \
223 -plugin-opt sym:${_}func2::0:0:0 \
224 -plugin-opt dumpresolutions \
225 -plugin-opt add:tmpdir/func.o \
226 -plugin-opt claim:tmpdir/libtext.a \
227 -plugin-opt sym:${_}text::0:0:0 \
228 -plugin-opt add:tmpdir/text.o \
897aea50 229 $testobjfiles_notext -Ltmpdir -ltext $libs" "" "" "" {{ld plugin-11.d}} "main.x" ] \
439b7f41
L
230 [list "plugin ignore lib with source" \
231 "-plugin $plugin_path $regclm $regas $regcln \
232 -plugin-opt claim:$srcdir/$subdir/func.c \
233 -plugin-opt sym:${_}func::0:0:0 \
234 -plugin-opt sym:${_}func2::0:0:0 \
235 -plugin-opt dumpresolutions \
236 -plugin-opt add:tmpdir/func.o \
237 $testsrcfiles_notext -Ltmpdir -ltext $libs" "" "" "" {{ld plugin-18.d}} "main.x" ] \
238 [list "plugin claimfile replace lib with source" \
239 "-plugin $plugin_path $regclm $regas $regcln \
240 -plugin-opt claim:$srcdir/$subdir/func.c \
241 -plugin-opt sym:${_}func::0:0:0 \
242 -plugin-opt sym:${_}func2::0:0:0 \
243 -plugin-opt dumpresolutions \
244 -plugin-opt add:tmpdir/func.o \
245 -plugin-opt claim:tmpdir/libtext.a \
246 -plugin-opt sym:${_}text::0:0:0 \
247 -plugin-opt add:tmpdir/text.o \
248 $testsrcfiles_notext -Ltmpdir -ltext $libs" "" "" "" {{ld plugin-19.d}} "main.x" ] \
ace667e5
AB
249 [list "plugin with empty archive" \
250 "-plugin $plugin_path $regclm \
251 -plugin-opt read:8 \
252 $testobjfiles tmpdir/libempty.a $libs" "" "" "" {{ld plugin-30.d}} "main.x" ] \
5d3236ee
DK
253]
254
255set plugin_extra_elf_tests [list \
256 [list "plugin set symbol visibility" "-plugin $plugin_path $regclm \
257 $regas $regcln -plugin-opt claim:tmpdir/func.o \
258 -plugin-opt sym:${_}func::0:0:0 \
259 -plugin-opt sym:${_}func1::0:1:0 \
260 -plugin-opt sym:${_}func2::0:2:0 \
261 -plugin-opt sym:${_}func3::0:3:0 \
262 -plugin-opt dumpresolutions \
9e2278f5 263 -plugin-opt add:tmpdir/func.o \
c4139418
L
264 -plugin-opt add:tmpdir/func1p.o \
265 -plugin-opt add:tmpdir/func2i.o \
266 -plugin-opt add:tmpdir/func3h.o \
897aea50 267 $testobjfiles $libs --verbose=2" "" "" "" {{ld plugin-12.d} \
c4139418 268 {readelf -s plugin-vis-1.d}} "main.x" ] \
439b7f41
L
269 [list "plugin set symbol visibility with source" \
270 "-plugin $plugin_path $regclm $regas $regcln \
271 -plugin-opt claim:$srcdir/$subdir/func.c \
272 -plugin-opt sym:${_}func::0:0:0 \
273 -plugin-opt sym:${_}func1::0:1:0 \
274 -plugin-opt sym:${_}func2::0:2:0 \
275 -plugin-opt sym:${_}func3::0:3:0 \
276 -plugin-opt dumpresolutions \
277 -plugin-opt add:tmpdir/func.o \
278 -plugin-opt add:tmpdir/func1p.o \
279 -plugin-opt add:tmpdir/func2i.o \
280 -plugin-opt add:tmpdir/func3h.o \
281 $testsrcfiles $libs --verbose=2" "" "" "" {{ld plugin-12.d} \
282 {readelf -s plugin-vis-1.d}} "main.x" ] \
5d3236ee
DK
283]
284
c3842ce5 285if { !$can_compile || $failed_compile } {
5d3236ee 286 foreach testitem $plugin_tests {
5b1f6c95 287 unsupported [lindex $testitem 0]
5d3236ee
DK
288 }
289 if { [is_elf_format] } {
290 foreach testitem $plugin_extra_elf_tests {
5b1f6c95 291 unsupported [lindex $testitem 0]
5d3236ee
DK
292 }
293 }
ad77db1c 294 set CFLAGS_FOR_TARGET "$old_CFLAGS"
5d3236ee
DK
295 return
296}
297
298run_ld_link_tests $plugin_tests
299
c4139418 300if { [is_elf_format] \
669d5115
AM
301 && [ld_compile $CC_FOR_TARGET $srcdir/$subdir/func1p.c tmpdir/func1p.o] \
302 && [ld_compile $CC_FOR_TARGET $srcdir/$subdir/func2i.c tmpdir/func2i.o] \
303 && [ld_compile $CC_FOR_TARGET $srcdir/$subdir/func3h.c tmpdir/func3h.o] } {
5d3236ee
DK
304 run_ld_link_tests $plugin_extra_elf_tests
305}
306
ace667e5
AB
307if {![ar_simple_create $ar "" "tmpdir/libtext.a" "tmpdir/text.o"] || \
308 ![ar_simple_create $ar "" "tmpdir/libempty.a" ""]} {
5d3236ee 309 foreach testitem $plugin_lib_tests {
5b1f6c95 310 unsupported [lindex $testitem 0]
5d3236ee
DK
311 }
312} else {
313 run_ld_link_tests $plugin_lib_tests
314}
439b7f41
L
315
316set plugin_src_tests [list \
317 [list "plugin 2 with source lib" \
318 "-plugin $plugin2_path $regclm $regas $regcln \
319 -plugin-opt dumpresolutions \
320 tmpdir/main.o -Ltmpdir -ltext -lfunc $libs" "" "" "" {{ld plugin-20.d}} "main.x" ] \
321 [list "load plugin 2 with source" \
322 "-plugin $plugin2_path $regclm $regas $regcln \
323 -plugin-opt dumpresolutions \
324 $testsrcfiles $libs" "" "" "" {{ld plugin-21.d}} "main.x" ] \
325 [list "load plugin 2 with source and -r" \
326 "-r -plugin $plugin2_path $regclm $regas $regcln \
327 -plugin-opt dumpresolutions \
328 $testsrcfiles $libs" "" "" "" {{ld plugin-24.d}} "main.x" ] \
329 [list "plugin 3 with source lib" \
330 "-plugin $plugin3_path $regclm $regas $regcln \
331 -plugin-opt dumpresolutions \
332 tmpdir/main.o -Ltmpdir -ltext -lfunc $libs" "" "" "" {{ld plugin-22.d}} "main.x" ] \
333 [list "load plugin 3 with source" \
334 "-plugin $plugin3_path $regclm $regas $regcln \
335 -plugin-opt dumpresolutions \
336 $testsrcfiles $libs" "" "" "" {{ld plugin-23.d}} "main.x" ] \
337 [list "load plugin 3 with source and -r" \
338 "-r -plugin $plugin3_path $regclm $regas $regcln \
339 -plugin-opt dumpresolutions \
340 $testsrcfiles $libs" "" "" "" {{ld plugin-25.d}} "main.x" ] \
341]
342
343# Check if nm --plugin works.
344set testname "nm --plugin"
345set nm_plugin "$NM --plugin $plugin2_path $srcdir/$subdir/func.c"
346catch "exec $nm_plugin" plugin_nm_output
347send_log "$nm_plugin\n"
348send_log "$plugin_nm_output\n"
349if { [regexp "0+ T func" "$plugin_nm_output"] &&
350 [regexp "0+ T _func" "$plugin_nm_output"] } {
351 pass $testname
352} else {
353 fail $testname
354}
355
356# Check if ar --plugin works.
357file delete tmpdir/libfunc.a
358if [ar_simple_create $ar "--plugin $plugin2_path" "tmpdir/libfunc.a" \
359 "tmpdir/main.o $srcdir/$subdir/func.c"] {
360 set testname "ar --plugin"
361 set nm_plugin "$NM -s --plugin $plugin2_path tmpdir/libfunc.a"
362 catch "exec $nm_plugin" plugin_nm_output
363 send_log "$nm_plugin\n"
364 send_log "$plugin_nm_output\n"
365 if { [regexp "func in func.c" "$plugin_nm_output"] &&
366 [regexp "_func in func.c" "$plugin_nm_output"] } {
367 pass $testname
368 run_ld_link_tests $plugin_src_tests
369 } else {
370 fail $testname
371 }
372} else {
373 foreach testitem $plugin_src_tests {
5b1f6c95 374 unsupported [lindex $testitem 0]
439b7f41
L
375 }
376}
3355cb3b
L
377
378file delete tmpdir/libpr20070.a
379if [ar_simple_create $ar "--plugin $plugin4_path" "tmpdir/libpr20070.a" \
380 "$srcdir/$subdir/pr20070b.c"] {
381 run_ld_link_tests [list \
382 [list \
383 "PR ld/20070" \
384 "-Bstatic -plugin $plugin4_path $regclm \
385 $regas $regcln \
386 -plugin-opt claim:$srcdir/$subdir/pr20070b.c \
387 -plugin-opt claim:tmpdir/libpr20070.a \
388 -plugin-opt dumpresolutions \
389 tmpdir/pr20070a.o tmpdir/text.o tmpdir/libpr20070.a $libs" \
390 "" "" "" {{ld pr20070.d}} "pr20070.x" \
391 ] \
392 ]
393} else {
5b1f6c95 394 unsupported "PR ld/20070"
3355cb3b 395}
16f9c644 396
ad77db1c 397set CFLAGS_FOR_TARGET "$old_CFLAGS"