Mark the plugin symbol undefined
[external/binutils.git] / ld / testsuite / ld-plugin / lto.exp
1 # Expect script for ld-plugin LTO tests
2 #   Copyright (C) 2011-2015 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 plugin and LTO.
22 if { ![check_plugin_api_available]
23      || ![check_lto_available] } {
24     return
25 }
26
27 global CFLAGS
28 global CXXFLAGS
29 set saved_CFLAGS "$CFLAGS"
30 set saved_CXXFLAGS "$CXXFLAGS"
31 regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CFLAGS "" CFLAGS
32 regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CXXFLAGS "" CXXFLAGS
33
34 proc restore_notify { } {
35   global saved_CFLAGS
36   global saved_CXXFLAGS
37   set CFLAGS "$saved_CFLAGS"
38   set CXXFLAGS "$saved_CXXFLAGS"
39 }
40
41 set plugin_names {
42     liblto_plugin.so
43     liblto_plugin-0.dll
44     cyglto_plugin-0.dll
45 }
46 set plug_opt ""
47 foreach plug $plugin_names {
48     set plug_so [run_host_cmd $CC "--print-prog-name $plug"]
49     if { $plug_so eq $plug } then {
50         set plug_so [run_host_cmd $CC "--print-file-name $plug"]
51     }
52     if { $plug_so ne $plug } then {
53         set plug_opt "--plugin $plug_so"
54         break
55     }
56 }
57 set lto_fat ""
58 if { [check_lto_fat_available] } {
59   set lto_fat "-ffat-lto-objects"
60 }
61
62 # Simple LTO tests and generate input files for complex LTO tests.
63 set lto_link_tests [list \
64   [list "LTO 1" \
65    "-O2 -flto -fuse-linker-plugin" "-flto -fuse-linker-plugin" \
66    {lto-1a.c lto-1b.c} {} "lto-1.exe"] \
67   [list "Compile 2" \
68    "" "-O2 -flto -fuse-linker-plugin" \
69    {lto-2.c} {} ""] \
70   [list "LTO 2" \
71    "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" "" \
72    {dummy.c} {} "lto-2.exe"] \
73   [list "Compile 3a" \
74    "" "-flto" \
75    {lto-3a.c} {} ""] \
76   [list "Compile 3c" \
77    "" "-O2" \
78    {lto-3c.c} {} ""] \
79   [list "Build liblto-3.a" \
80    "" "-flto $lto_fat" \
81    {lto-3b.c} {} "liblto-3.a"] \
82   [list "Compile 5a" \
83    "" "-flto $lto_fat" \
84    {lto-5a.c} {} ""] \
85   [list "Compile 5b" \
86    "" "-flto $lto_fat" \
87    {lto-5b.c} {} ""] \
88   [list "LTO 6" \
89    "-O2 -flto -fuse-linker-plugin" "" \
90    {lto-6.c} {} "lto-6.exe" "c"] \
91   [list "Compile PR ld/12365" \
92    "" "-flto -O2 $lto_fat" \
93    {pr12365a.c pr12365b.c pr12365c.c} {} ""] \
94   [list "Compile 9" \
95    "" "-O2 -finline -flto" \
96    {lto-9.cc} {} "" "c++"] \
97   [list "Compile 11a" \
98    "" "-O -flto" \
99    {lto-11a.c} {} ""] \
100   [list "Compile 11b" \
101    "" "-O -flto" \
102    {lto-11b.c} {} ""] \
103   [list "Compile 11c" \
104    "" "-O" \
105    {lto-11c.c} {} ""] \
106   [list "Build liblto-12.a" \
107    "$plug_opt" "-O2 -flto" \
108    {lto-12c.c} {} "liblto-12.a"] \
109   [list "Compile 12" \
110    "" "-O2 -flto" \
111    {lto-12a.c lto-12b.c} {} ""] \
112   [list "Compile 13" \
113    "" "-O2 -flto" \
114    {lto-13a.c lto-13b.c} {} ""] \
115   [list "Build liblto-13.a" \
116    "" "-O2" \
117    {lto-13c.c} {} "liblto-13.a"] \
118   [list "Compile 14a" \
119    "" "-flto" \
120    {lto-14a.c lto-14b.c} {} ""] \
121   [list "Build liblto-14.a" \
122    "$plug_opt" "-flto" \
123    {lto-14c.c} {} "liblto-14.a"] \
124   [list "Compile 15a" \
125    "" "-flto" \
126    {lto-15a.c} {} ""] \
127   [list "Build liblto-15.a" \
128    "$plug_opt" "-flto" \
129    {lto-15b.c} {} "liblto-15.a"] \
130   [list "PR ld/12696" \
131    "-O2 -flto -fuse-linker-plugin -r -nostdlib" "-O2 -flto" \
132    {pr12696-1.cc} {} "pr12696-1r.o" "c++"] \
133   [list "Compile PR ld/12758" \
134    "" "" \
135    {pr12758a.s} {} ""] \
136   [list "Build libpr12758.a" \
137    "" "-flto -O2 $lto_fat" \
138    {pr12758b.c} {} "libpr12758.a"] \
139   [list "PR ld/12758" \
140    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12758a.o -Wl,--start-group tmpdir/libpr12758.a -Wl,--end-group" "" \
141    {dummy.c} {} "pr12758.exe"] \
142   [list "Compile PR ld/12760" \
143    "" "-g -O0" \
144    {pr12760a.c} {} ""] \
145   [list "Build libpr12760.a" \
146    "" "-flto -O2 $lto_fat" \
147    {pr12760b.c} {} "libpr12760.a"] \
148   [list "PR ld/12760" \
149    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12760a.o -Wl,--start-group tmpdir/libpr12760.a -Wl,--end-group" "" \
150    {dummy.c} {} "pr12760.exe" "c" "pr12760a.c:6: warning: Bad bar"] \
151   [list "Build libpr13183.a" \
152    "-T" "-flto -O2 $lto_fat" \
153    {pr13183a.c} {} "libpr13183.a"] \
154   [list "Compile PR ld/13183" \
155    "" "-flto -O2" \
156    {pr13183b.c} {} ""] \
157   [list "Compile PR ld/13201" \
158    "" "-flto -O2" \
159    {pr13201.c} {} ""] \
160   [list "PR ld/13287" \
161    "-flto -fuse-linker-plugin -Wl,--as-needed" "-flto" \
162    {pr13287.cc} {} "pr13287.exe" "c++"] \
163   [list "PR ld/15323" \
164    "" "-O2" \
165    {pr15323a.c} {} "" "c"] \
166   [list "Compile(1) PR ld/pr16846" \
167    "" "-flto" \
168    {pr16846a.c pr16846b.c} {} ""] \
169   [list "Compile(2) PR ld/pr16846" \
170    "" "" \
171    {pr16846c.c} {} ""] \
172   [list "PR ld/pr16846(1)" \
173    "-flto -fuse-linker-plugin tmpdir/pr16846a.o tmpdir/pr16846b.o tmpdir/pr16846c.o" "" \
174    {dummy.c} {} "pr16846a.exe"] \
175   [list "PR ld/pr16846(2)" \
176    "-flto -fuse-linker-plugin tmpdir/pr16846a.o tmpdir/pr16846c.o tmpdir/pr16846b.o" "" \
177    {dummy.c} {} "pr16846b.exe"] \
178 ]
179
180 if { [at_least_gcc_version 4 7] } {
181     set lto_link_tests [concat $lto_link_tests [list \
182       [list "Compile PR ld/12942 (1)" \
183        "" "-flto -O2" \
184        {pr12942a.cc pr12942c.cc} {} "" "c++"] \
185       [list "Compile PR ld/12942 (2)" \
186        "" "-O0" \
187        {pr12942b.cc} {} "" "c++"] \
188     ]]
189 }
190
191 # Generate input files for complex LTO tests for ELF.
192 set lto_link_elf_tests [list \
193   [list "Compile 7" \
194    "" "-flto -O2" \
195    {lto-7a.c lto-7b.c lto-7c.c} {} ""] \
196   [list "Build liblto-7.so" \
197    "-shared" "-O2 -fpic" \
198    {lto-7d.c} {} "liblto-7.so" "c"] \
199   [list "Compile 8a" \
200    "" "-O2" \
201    {lto-8a.c} {} ""] \
202   [list "Compile 8b" \
203    "" "-flto -O2" \
204    {lto-8b.c} {} ""] \
205   [list "Build liblto-17a.so" \
206    "-shared -O2 -fpic -flto -fuse-linker-plugin" "-O2 -fpic -flto" \
207    {lto-17a.c} {{"nm" {} "lto-17a.d"}} "liblto-17a.so" "c"] \
208   [list "Build liblto-17b.so 1" \
209    "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto" \
210    {lto-17b.c} {{"nm" {} "lto-17b-1.d"}} "liblto-17b.so"] \
211   [list "Build liblto-17b.so 2" \
212    "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto" \
213    {lto-17b.c} {{"nm" {} "lto-17b-2.d"}} "liblto-17b.so"] \
214   [list "PR ld/12982" \
215    "-O2 -flto -fuse-linker-plugin" "-O2 -flto" \
216    {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"] \
217   [list "PR ld/12975" \
218    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib -Wl,-version-script,pr12975.t" "-O2 -flto" \
219    {pr12975.c} {{"readelf" {-s --wide} "pr12975.d"}} "pr12975.so" "c"] \
220   [list "PR ld/13229" \
221    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -finline -fno-early-inlining -flto" \
222    {pr13229.cc} {{"readelf" {-s --wide} "pr13229.d"}} "pr13229.so" "c++"] \
223   [list "PR ld/13244" \
224    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -fno-early-inlining -flto" \
225    {pr13244.c} {{"readelf" {-s --wide} "pr13244.d"}} "pr13244.so" "c"] \
226   [list "Build libpr15146a.a" \
227    "$plug_opt" "-flto -O2" \
228    {pr15146a.c} {} "lib15146a.a"] \
229   [list "Build pr15146b.so" \
230    "-shared" "-O2 -fpic" \
231    {pr15146b.c} {} "pr15146b.so" "c"] \
232   [list "Build pr15146c.so" \
233    "-shared -Wl,--no-as-needed tmpdir/pr15146b.so" "-O2 -fpic" \
234    {pr15146c.c} {} "pr15146c.so" "c"] \
235   [list "PR ld/15146 (1)" \
236    "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed tmpdir/pr15146a.o tmpdir/pr15146c.so" "" \
237    {dummy.c} {{"readelf" {-d} "pr15146.d"}} "pr15146a.exe"] \
238   [list "Build libpr15146d.a" \
239    "$plug_opt" "-flto -O2" \
240    {pr15146d.c} {} "lib15146d.a"] \
241   [list "Build libpr16746a.a" \
242    "" "" \
243    {pr16746a.c pr16746b.c} {} "lib15146a.a"] \
244   [list "Build libpr16746b.a" \
245    "$plug_opt" "-O2 -flto" \
246    {pr16746c.c pr16746d.c} {} "lib15146b.a"] \
247   [list "PR ld/16746 (1)" \
248    "-O2 -flto -fuse-linker-plugin tmpdir/pr16746a.o tmpdir/pr16746c.o" "-O2 -flto" \
249    {dummy.c} {} "pr16746a.exe"] \
250   [list "PR ld/16746 (2)" \
251    "-O2 -flto -fuse-linker-plugin tmpdir/pr16746c.o tmpdir/pr16746a.o" "-O2 -flto" \
252    {dummy.c} {} "pr16746b.exe"] \
253   [list "PR ld/14918" \
254    "-flto" "-flto" \
255    {pr14918.c} {{"readelf" {-d --wide} "pr14918.d"}} "pr14918.exe" "c"] \
256 ]
257
258 # Check final symbols in executables.
259 set lto_link_symbol_tests [list \
260   [list "LTO 3 symbol" \
261    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" "" \
262    {dummy.c} {{"nm" {} "lto-3.d"}} "lto-3.exe" "c"] \
263   [list "LTO 5 symbol" \
264    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" "" \
265    {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"] \
266   [list "LTO 9 symbol" \
267    "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" "" \
268    {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"] \
269   [list "LTO 16a symbol" \
270    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" "-flto" \
271    {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"] \
272   [list "LTO 16b symbol" \
273    "-O2 -Wl,-e,foo -u bar -nostdlib -flto -fuse-linker-plugin" "-flto" \
274    {lto-16a.c lto-16b.c} {{"nm" {} "lto-16b.d"}} "lto-16b.exe" "c"] \
275   [list "PR ld/13183" \
276    "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" "" \
277    {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"] \
278 ]
279
280 # LTO run-time tests.
281 set lto_run_tests [list \
282   [list "LTO 3a" \
283    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" "" \
284    {dummy.c} "lto-3b.exe" "lto-3.out" "" "c"] \
285   [list "LTO 3b" \
286    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/lto-3.o" "" \
287    {dummy.c} "lto-3c.exe" "lto-3.out" "" "c"] \
288   [list "LTO 3c" \
289    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o -Wl,--whole-archive tmpdir/liblto-3.a -Wl,--no-whole-archive tmpdir/liblto-3.a" "" \
290    {dummy.c} "lto-3d.exe" "lto-3.out" "" "c"] \
291   [list "LTO 5" \
292    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" "" \
293    {dummy.c} "lto-5.exe" "lto-5.out" "" "c"] \
294   [list "LTO 11" \
295    "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" "" \
296    {dummy.c} "lto-11.exe" "lto-11.out" "" "c"] \
297   [list "LTO 12a" \
298    "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/liblto-12.a tmpdir/lto-12b.o" "" \
299    {dummy.c} "lto-12a.exe" "lto-12.out" "" "c"] \
300   [list "LTO 12b" \
301    "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/lto-12b.o tmpdir/liblto-12.a" "" \
302    {dummy.c} "lto-12b.exe" "lto-12.out" "" "c"] \
303   [list "LTO 13" \
304    "-O -flto -fuse-linker-plugin tmpdir/lto-13a.o tmpdir/liblto-13.a tmpdir/lto-13b.o" "" \
305    {dummy.c} "lto-13.exe" "lto-13.out" "" "c"] \
306   [list "LTO 14" \
307    "-O2 -flto -fuse-linker-plugin tmpdir/lto-14a.o -Wl,--whole-archive tmpdir/liblto-14.a -Wl,--no-whole-archive tmpdir/lto-14b.o" "" \
308    {dummy.c} "lto-14.exe" "lto-14.out" "" "c"] \
309   [list "LTO 15" \
310    "-O2 -flto -fuse-linker-plugin -Wl,--start-group tmpdir/liblto-15.a tmpdir/lto-15a.o -Wl,--end-group" "" \
311    {dummy.c} "lto-15.exe" "lto-15.out" "" "c"] \
312   [list "PR ld/13066" \
313    "-O2 -flto -fuse-linker-plugin" "" \
314    {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"] \
315   [list "PR ld/13201" \
316    "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" "" \
317    {dummy.c} "pr13201.exe" "pr13201.out" "" "c"] \
318   [list "PR ld/15323" \
319    "-O2 -flto -fuse-linker-plugin tmpdir/pr15323a.o" "" \
320    {pr15323b.c} "pr15323.exe" "pr15323.out" "-flto -O2" "c"] \
321 ]
322
323 if { [at_least_gcc_version 4 7] } {
324     set lto_run_tests [concat $lto_run_tests [list \
325       [list "PR ld/12942 (1)" \
326        "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" "" \
327        {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"] \
328       [list "PR ld/12942 (2)" \
329        "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" "" \
330        {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"] \
331     ]]
332 }
333
334 # LTO run-time tests for ELF
335 set lto_run_elf_tests [list \
336   [list "LTO 7" \
337    "-O2 -flto -fuse-linker-plugin tmpdir/lto-7b.o tmpdir/lto-7c.o tmpdir/lto-7a.o -Wl,--no-as-needed tmpdir/liblto-7.so" "" \
338    {dummy.c} "lto-7.exe" "lto-7.out" "" "c"] \
339   [list "LTO 8" \
340    "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" "" \
341    {dummy.c} "lto-8.exe" "lto-8.out" "" "c"] \
342   [list "LTO TLS IE" \
343    "-O2 -flto -fuse-linker-plugin" "" \
344    {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"] \
345 ]
346
347 run_cc_link_tests $lto_link_tests
348
349 # Restrict these to ELF targets that support shared libs and PIC.
350 if { [is_elf_format] && [check_lto_shared_available] } {
351     run_cc_link_tests $lto_link_elf_tests
352     set testname "PR ld/15146 (2)"
353     set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed tmpdir/pr15146d.o tmpdir/pr15146c.so"]
354     if { [ regexp "undefined reference to symbol 'xxx'" $exec_output ] } {
355         pass $testname
356     } {
357         fail $testname
358     }
359     set testname "PR ld/16746 (3)"
360     set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746b.o tmpdir/pr16746d.o"]
361     if { [ regexp "warning: foobar" $exec_output ] && ![ regexp "symbol from plugin" $exec_output ] } {
362         pass $testname
363     } {
364         fail $testname
365     }
366     set testname "PR ld/16746 (4)"
367     set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746d.o tmpdir/pr16746b.o"]
368     if { [ regexp "warning: foobar" $exec_output ] && ![ regexp "symbol from plugin" $exec_output ] } {
369         pass $testname
370     } {
371         fail $testname
372     }
373 }
374
375 set testname "Build liblto-11.a"
376 remote_file host delete "tmpdir/liblto-11.a"
377 set catch_output [run_host_cmd "$ar" "rc $plug_opt tmpdir/liblto-11.a tmpdir/lto-11a.o tmpdir/lto-11b.o tmpdir/lto-11c.o"]
378 if {![string match "" $catch_output]} {
379     unresolved $testname
380     restore_notify
381     return
382 }
383
384 if { [at_least_gcc_version 4 7] } {
385     # Check expected LTO linker errors.
386     set testname "PR ld/12365"
387     set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr12365a.o tmpdir/pr12365b.o tmpdir/pr12365c.o"]
388     if { [ regexp "undefined reference to `my_bcopy'" $exec_output ] } {
389         pass $testname
390     } {
391         fail $testname
392     }
393     set testname "PR ld/12942 (3)"
394     set exec_output [run_host_cmd "$CXX" "-O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
395     if { [ regexp "undefined reference to `link_error\\(\\)'" $exec_output ] } {
396         pass $testname
397     } {
398         fail $testname
399     }
400 }
401
402 # Run "ld -r" to generate inputs for complex LTO tests.
403 run_dump_test "lto-3r"
404 remote_exec host "mv" "tmpdir/dump tmpdir/lto-3.o"
405 run_dump_test "lto-5r"
406 remote_exec host "mv" "tmpdir/dump tmpdir/lto-5.o"
407
408 run_cc_link_tests $lto_link_symbol_tests
409
410 # The following tests require running the executable generated by ld.
411 if ![isnative] {
412     return
413 }
414
415 run_ld_link_exec_tests [] $lto_run_tests
416
417 if { [is_elf_format] } {
418     run_ld_link_exec_tests [] $lto_run_elf_tests
419 }
420
421 restore_notify