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