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