]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - 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
1 # Expect script for ld-plugin tests
2 # Copyright (C) 2010-2023 Free Software Foundation, Inc.
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.
22 if ![check_plugin_api_available] {
23 return
24 }
25
26 # And a compiler to be available.
27 set can_compile 1
28 if { ![check_compiler_available] } {
29 # Don't fail immediately,
30 set can_compile 0
31 }
32
33 pass "plugin API enabled"
34
35 # Look for the name we can dlopen in the test plugin's libtool control script.
36 set plugin_name [file_contents "$base_dir/libldtestplug.la"]
37 set plugin_name [regsub "'.*" [regsub ".*dlname='" "$plugin_name" ""] ""]
38 # Even though the API supports plugins it does not mean that the
39 # linker was configured with --enable-plugins. Check for that here.
40 if { $plugin_name == "" } {
41 verbose "The linker is not configured to support plugins"
42 return
43 }
44 verbose "plugin name is '$plugin_name'"
45
46 set plugin2_name [file_contents "$base_dir/libldtestplug2.la"]
47 set plugin2_name [regsub "'.*" [regsub ".*dlname='" "$plugin2_name" ""] ""]
48 verbose "plugin2 name is '$plugin2_name'"
49
50 set plugin3_name [file_contents "$base_dir/libldtestplug3.la"]
51 set plugin3_name [regsub "'.*" [regsub ".*dlname='" "$plugin3_name" ""] ""]
52 verbose "plugin3 name is '$plugin3_name'"
53
54 set plugin4_name [file_contents "$base_dir/libldtestplug4.la"]
55 set plugin4_name [regsub "'.*" [regsub ".*dlname='" "$plugin4_name" ""] ""]
56 verbose "plugin4 name is '$plugin4_name'"
57
58 # Use libtool to find full path to plugin rather than worrying
59 # about run paths or anything like that.
60 catch "exec $base_dir/libtool --config" lt_config
61 verbose "Full lt config: $lt_config" 3
62 # Look for "objdir=.libs"
63 regexp -line "^objdir=.*$" "$lt_config" lt_objdir
64 verbose "lt_objdir line is '$lt_objdir'" 3
65 set lt_objdir [regsub "objdir=" "$lt_objdir" ""]
66 set plugin_path "$base_dir/$lt_objdir/$plugin_name"
67 set plugin2_path "$base_dir/$lt_objdir/$plugin2_name"
68 set plugin3_path "$base_dir/$lt_objdir/$plugin3_name"
69 set plugin4_path "$base_dir/$lt_objdir/$plugin4_name"
70 verbose "Full plugin path $plugin_path" 2
71 verbose "Full plugin2 path $plugin2_path" 2
72 verbose "Full plugin3 path $plugin3_path" 2
73 verbose "Full plugin4 path $plugin4_path" 2
74
75 set regclm "-plugin-opt registerclaimfile"
76 set regas "-plugin-opt registerallsymbolsread"
77 set regassilent "-plugin-opt registerallsymbolsreadsilent"
78 set 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.
87 set failed_compile 0
88 set _ ""
89 set plugin_nm_output ""
90 set old_CFLAGS "$CFLAGS_FOR_TARGET"
91 append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
92 if { [istarget m681*-*-*] || [istarget m68hc1*-*-*] || [istarget m9s12x*-*-*] } {
93 # otherwise get FAILS due to _.frame
94 append CFLAGS_FOR_TARGET " -fomit-frame-pointer"
95 }
96
97 if { $can_compile && \
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]) } {
104 # Defer fail until we have list of tests set.
105 set failed_compile 1
106 }
107
108 set dotsym 0
109 if { $can_compile && !$failed_compile } {
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 }
115 if { [regexp "\\.func" "$plugin_nm_output"] } {
116 set dotsym 1
117 }
118 }
119
120 set testobjfiles "tmpdir/main.o tmpdir/func.o tmpdir/text.o"
121 set testobjfiles_notext "tmpdir/main.o tmpdir/func.o"
122 set testsrcfiles "tmpdir/main.o $srcdir/$subdir/func.c tmpdir/text.o"
123 set testsrcfiles_notext "tmpdir/main.o $srcdir/$subdir/func.c"
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.
126 set libs "[ld_link_defsyms] --defsym ${_}printf=${_}main --defsym ${_}puts=${_}main"
127 if { $dotsym } {
128 append libs " --defsym .printf=.main --defsym .puts=.main"
129 }
130 if [is_pecoff_format] {
131 #otherwise relocs overflow to symbols defined on the command line
132 append libs " --image-base=0x10000000"
133 }
134
135 set plugin_tests [list \
136 [list "load plugin" "-plugin $plugin_path \
137 $testobjfiles $libs" "" "" "" {{ld plugin-1.d}} "main.x" ] \
138 [list "fail plugin onload" "-plugin $plugin_path -plugin-opt failonload \
139 $testobjfiles $libs" "" "" "" {{ld plugin-2.d}} "main.x" ] \
140 [list "fail plugin allsymbolsread" "-plugin $plugin_path $regas \
141 -plugin-opt failallsymbolsread \
142 $testobjfiles $libs" "" "" "" {{ld plugin-3.d}} "main.x" ] \
143 [list "fail plugin cleanup" "-plugin $plugin_path -plugin-opt failcleanup \
144 $regcln \
145 $testobjfiles $libs" "" "" "" {{ld plugin-4.d}} "main.x" ] \
146 [list "plugin all hooks" "-plugin $plugin_path $regclm $regas $regcln \
147 $testobjfiles $libs" "" "" "" {{ld plugin-5.d}} "main.x" ] \
148 [list "plugin claimfile lost symbol" "-plugin $plugin_path $regclm \
149 $regas $regcln -plugin-opt claim:tmpdir/func.o \
150 $testobjfiles $libs" "" "" "" {{ld plugin-6.d}} "main.x" ] \
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 \
154 $testobjfiles $libs" "" "" "" {{ld plugin-7.d}} "main.x" ] \
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 \
160 $testobjfiles $libs" "" "" "" {{ld plugin-8.d}} "main.x" ] \
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 \
167 $testobjfiles $libs" "" "" "" {{ld plugin-9.d}} "main.x" ] \
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" ] \
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" ] \
203 ]
204
205 if [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
212 set 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 \
219 $testobjfiles_notext -Ltmpdir -ltext $libs" "" "" "" {{ld plugin-10.d}} "main.x" ] \
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 \
229 $testobjfiles_notext -Ltmpdir -ltext $libs" "" "" "" {{ld plugin-11.d}} "main.x" ] \
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" ] \
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" ] \
253 ]
254
255 set 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 \
263 -plugin-opt add:tmpdir/func.o \
264 -plugin-opt add:tmpdir/func1p.o \
265 -plugin-opt add:tmpdir/func2i.o \
266 -plugin-opt add:tmpdir/func3h.o \
267 $testobjfiles $libs --verbose=2" "" "" "" {{ld plugin-12.d} \
268 {readelf -s plugin-vis-1.d}} "main.x" ] \
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" ] \
283 ]
284
285 if { !$can_compile || $failed_compile } {
286 foreach testitem $plugin_tests {
287 unsupported [lindex $testitem 0]
288 }
289 if { [is_elf_format] } {
290 foreach testitem $plugin_extra_elf_tests {
291 unsupported [lindex $testitem 0]
292 }
293 }
294 set CFLAGS_FOR_TARGET "$old_CFLAGS"
295 return
296 }
297
298 run_ld_link_tests $plugin_tests
299
300 if { [is_elf_format] \
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] } {
304 run_ld_link_tests $plugin_extra_elf_tests
305 }
306
307 if {![ar_simple_create $ar "" "tmpdir/libtext.a" "tmpdir/text.o"] || \
308 ![ar_simple_create $ar "" "tmpdir/libempty.a" ""]} {
309 foreach testitem $plugin_lib_tests {
310 unsupported [lindex $testitem 0]
311 }
312 } else {
313 run_ld_link_tests $plugin_lib_tests
314 }
315
316 set 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.
344 set testname "nm --plugin"
345 set nm_plugin "$NM --plugin $plugin2_path $srcdir/$subdir/func.c"
346 catch "exec $nm_plugin" plugin_nm_output
347 send_log "$nm_plugin\n"
348 send_log "$plugin_nm_output\n"
349 if { [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.
357 file delete tmpdir/libfunc.a
358 if [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 {
374 unsupported [lindex $testitem 0]
375 }
376 }
377
378 file delete tmpdir/libpr20070.a
379 if [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 {
394 unsupported "PR ld/20070"
395 }
396
397 set CFLAGS_FOR_TARGET "$old_CFLAGS"