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