Skip the PR 14918 linker test for ARM targets.
[external/binutils.git] / ld / testsuite / ld-plugin / lto.exp
1 # Expect script for ld-plugin LTO tests
2 #   Copyright (C) 2011-2017 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 # Check to see if the C and C++ compilers work
22 if { [which $CC] == 0 || [which $CXX] == 0 } {
23     return
24 }
25
26 # These tests require plugin and LTO.
27 if { ![check_plugin_api_available]
28      || ![check_lto_available] } {
29     return
30 }
31
32 global CFLAGS
33 global CXXFLAGS
34 set saved_CFLAGS "$CFLAGS"
35 set saved_CXXFLAGS "$CXXFLAGS"
36 regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CFLAGS "" CFLAGS
37 regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CXXFLAGS "" CXXFLAGS
38
39 proc restore_notify { } {
40   global saved_CFLAGS
41   global saved_CXXFLAGS
42   set CFLAGS "$saved_CFLAGS"
43   set CXXFLAGS "$saved_CXXFLAGS"
44 }
45
46 set plugin_names {
47     liblto_plugin.so
48     liblto_plugin-0.dll
49     cyglto_plugin-0.dll
50 }
51 set plug_opt ""
52 foreach plug $plugin_names {
53     set plug_so [run_host_cmd $CC "--print-prog-name $plug"]
54     if { $plug_so eq $plug } then {
55         set plug_so [run_host_cmd $CC "--print-file-name $plug"]
56     }
57     if { $plug_so ne $plug } then {
58         set plug_opt "--plugin $plug_so"
59         break
60     }
61 }
62 set lto_fat ""
63 set lto_no_fat ""
64 if { [check_lto_fat_available] } {
65   set lto_fat "-ffat-lto-objects"
66   set lto_no_fat "-fno-fat-lto-objects"
67 }
68
69 # Simple LTO tests and generate input files for complex LTO tests.
70 set lto_link_tests [list \
71   [list "LTO 1" \
72    "-O2 -flto -fuse-linker-plugin" "-flto -fuse-linker-plugin" \
73    {lto-1a.c lto-1b.c} {} "lto-1.exe"] \
74   [list "Compile 2" \
75    "" "-O2 -flto -fuse-linker-plugin" \
76    {lto-2.c} {} ""] \
77   [list "LTO 2" \
78    "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" "" \
79    {dummy.c} {} "lto-2.exe"] \
80   [list "Compile 3a" \
81    "" "-flto" \
82    {lto-3a.c} {} ""] \
83   [list "Compile 3c" \
84    "" "-O2" \
85    {lto-3c.c} {} ""] \
86   [list "Build liblto-3.a" \
87    "" "-flto $lto_fat" \
88    {lto-3b.c} {} "liblto-3.a"] \
89   [list "Compile 5a" \
90    "" "-flto $lto_fat" \
91    {lto-5a.c} {} ""] \
92   [list "Compile 5b" \
93    "" "-flto $lto_fat" \
94    {lto-5b.c} {} ""] \
95   [list "LTO 6" \
96    "-O2 -flto -fuse-linker-plugin" "" \
97    {lto-6.c} {} "lto-6.exe" "c"] \
98   [list "Compile PR ld/12365" \
99    "" "-flto -O2 $lto_fat" \
100    {pr12365a.c pr12365b.c pr12365c.c} {} ""] \
101   [list "Compile 9" \
102    "" "-O2 -finline -flto" \
103    {lto-9.cc} {} "" "c++"] \
104   [list "Compile 11a" \
105    "" "-O -flto" \
106    {lto-11a.c} {} ""] \
107   [list "Compile 11b" \
108    "" "-O -flto" \
109    {lto-11b.c} {} ""] \
110   [list "Compile 11c" \
111    "" "-O" \
112    {lto-11c.c} {} ""] \
113   [list "Build liblto-12.a" \
114    "$plug_opt" "-O2 -flto" \
115    {lto-12c.c} {} "liblto-12.a"] \
116   [list "Compile 12" \
117    "" "-O2 -flto" \
118    {lto-12a.c lto-12b.c} {} ""] \
119   [list "Compile 13" \
120    "" "-O2 -flto" \
121    {lto-13a.c lto-13b.c} {} ""] \
122   [list "Build liblto-13.a" \
123    "" "-O2" \
124    {lto-13c.c} {} "liblto-13.a"] \
125   [list "Compile 14a" \
126    "" "-flto" \
127    {lto-14a.c lto-14b.c} {} ""] \
128   [list "Build liblto-14.a" \
129    "$plug_opt" "-flto" \
130    {lto-14c.c} {} "liblto-14.a"] \
131   [list "Compile 15a" \
132    "" "-flto" \
133    {lto-15a.c} {} ""] \
134   [list "Build liblto-15.a" \
135    "$plug_opt" "-flto" \
136    {lto-15b.c} {} "liblto-15.a"] \
137   [list "PR ld/12696" \
138    "-O2 -flto -fuse-linker-plugin -r -nostdlib" "-O2 -flto" \
139    {pr12696-1.cc} {} "pr12696-1r.o" "c++"] \
140   [list "Compile PR ld/12758" \
141    "" "" \
142    {pr12758a.s} {} ""] \
143   [list "Build libpr12758.a" \
144    "" "-flto -O2 $lto_fat" \
145    {pr12758b.c} {} "libpr12758.a"] \
146   [list "PR ld/12758" \
147    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12758a.o -Wl,--start-group tmpdir/libpr12758.a -Wl,--end-group" "" \
148    {dummy.c} {} "pr12758.exe"] \
149   [list "Compile PR ld/12760" \
150    "" "-g -O0" \
151    {pr12760a.c} {} ""] \
152   [list "Build libpr12760.a" \
153    "" "-flto -O2 $lto_fat" \
154    {pr12760b.c} {} "libpr12760.a"] \
155   [list "PR ld/12760" \
156    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12760a.o -Wl,--start-group tmpdir/libpr12760.a -Wl,--end-group" "" \
157    {dummy.c} {{warning "pr12760a.c:6: warning: Bad \\.?bar"}} \
158    "pr12760.exe" "c"] \
159   [list "Build libpr13183.a" \
160    "-T" "-flto -O2 $lto_fat" \
161    {pr13183a.c} {} "libpr13183.a"] \
162   [list "Compile PR ld/13183" \
163    "" "-flto -O2" \
164    {pr13183b.c} {} ""] \
165   [list "Compile PR ld/13201" \
166    "" "-flto -O2" \
167    {pr13201.c} {} ""] \
168   [list "PR ld/13287" \
169    "-flto -fuse-linker-plugin -Wl,--as-needed" "-flto" \
170    {pr13287.cc} {} "pr13287.exe" "c++"] \
171   [list "PR ld/15323 (1)" \
172    "" "-O2" \
173    {pr15323a.c} {} "" "c"] \
174   [list "PR ld/15323 (2)" \
175    "-O2 -flto -r -nostdlib" "-O2 -flto" \
176    {pr15323a.c} {} "pr15323a-r.o" "c"] \
177   [list "Compile(1) PR ld/pr16846" \
178    "" "-flto" \
179    {pr16846a.c pr16846b.c} {} ""] \
180   [list "Compile(2) PR ld/pr16846" \
181    "" "" \
182    {pr16846c.c} {} ""] \
183   [list "PR ld/pr16846(1)" \
184    "-flto -fuse-linker-plugin tmpdir/pr16846a.o tmpdir/pr16846b.o tmpdir/pr16846c.o" "" \
185    {dummy.c} {} "pr16846a.exe"] \
186   [list "PR ld/pr16846(2)" \
187    "-flto -fuse-linker-plugin tmpdir/pr16846a.o tmpdir/pr16846c.o tmpdir/pr16846b.o" "" \
188    {dummy.c} {} "pr16846b.exe"] \
189   [list "PR ld/19317 (1)" \
190    "$plug_opt" "-flto $lto_no_fat" \
191    {pr19317.c} {} "libpr19317.a"] \
192   [list "Build pr20276a.o" \
193    "" "-fno-lto" \
194    {pr20276a.c}] \
195   [list "Build pr20276b.o" \
196    "$plug_opt" "-flto $lto_no_fat" \
197    {pr20276b.c}] \
198   [list "Build pr20267a.o" \
199    "" "" \
200    {pr20267a.c}] \
201   [list "Build libpr20267a.a" \
202    "$plug_opt" "-flto $lto_fat" \
203    {pr20267b.c} {} "libpr20267a.a"] \
204   [list "Build libpr20267b.a" \
205    "$plug_opt" "-flto $lto_no_fat" \
206    {pr20267b.c} {} "libpr20267b.a"] \
207   [list "Build pr20321" \
208    "-flto -Wl,-plugin,$plug_so" "-flto" \
209    {pr20321.c} {{warning ".*: duplicated plugin"}} \
210    "pr20321" "c"] \
211 ]
212
213 if { [at_least_gcc_version 4 7] } {
214     set lto_link_tests [concat $lto_link_tests [list \
215       [list "Compile PR ld/12942 (1)" \
216        "" "-flto -O2" \
217        {pr12942a.cc pr12942c.cc} {} "" "c++"] \
218       [list "Compile PR ld/12942 (2)" \
219        "" "-O0" \
220        {pr12942b.cc} {} "" "c++"] \
221     ]]
222 }
223
224 set lto_compile_elf_tests [list \
225   [list "Compile 7" \
226    "" "-flto -O2" \
227    {lto-7a.c lto-7b.c lto-7c.c} {} ""] \
228   [list "Compile 8a" \
229    "" "-O2" \
230    {lto-8a.c} {} ""] \
231   [list "Compile 8b" \
232    "" "-flto -O2" \
233    {lto-8b.c} {} ""] \
234 ]
235
236 # Generate input files for complex LTO tests for ELF.
237 set lto_link_elf_tests [list \
238   [list "Build liblto-7.so" \
239    "-shared" "-O2 -fpic" \
240    {lto-7d.c} {} "liblto-7.so" "c"] \
241   [list "Build liblto-17a.so" \
242    "-shared -O2 -fpic -flto -fuse-linker-plugin" "-O2 -fpic -flto" \
243    {lto-17a.c} {{"nm" {} "lto-17a.d"}} "liblto-17a.so" "c"] \
244   [list "Build liblto-17b.so 1" \
245    "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto" \
246    {lto-17b.c} {{"nm" {} "lto-17b-1.d"}} "liblto-17b.so"] \
247   [list "Build liblto-17b.so 2" \
248    "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto" \
249    {lto-17b.c} {{"nm" {} "lto-17b-2.d"}} "liblto-17b.so"] \
250   [list "PR ld/12982" \
251    "-O2 -flto -fuse-linker-plugin" "-O2 -flto" \
252    {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"] \
253   [list "PR ld/12975" \
254    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib -Wl,-version-script,pr12975.t" "-O2 -flto" \
255    {pr12975.c} {{"readelf" {-s --wide} "pr12975.d"}} "pr12975.so" "c"] \
256   [list "PR ld/13229" \
257    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -finline -fno-early-inlining -flto" \
258    {pr13229.cc} {{"readelf" {-s --wide} "pr13229.d"}} "pr13229.so" "c++"] \
259   [list "PR ld/13244" \
260    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -fno-early-inlining -flto" \
261    {pr13244.c} {{"readelf" {-s --wide} "pr13244.d"}} "pr13244.so" "c"] \
262   [list "Build libpr15146a.a" \
263    "$plug_opt" "-flto -O2" \
264    {pr15146a.c} {} "lib15146a.a"] \
265   [list "Build pr15146b.so" \
266    "-shared" "-O2 -fpic" \
267    {pr15146b.c} {} "pr15146b.so" "c"] \
268   [list "Build pr15146c.so" \
269    "-shared -Wl,--no-as-needed tmpdir/pr15146b.so" "-O2 -fpic" \
270    {pr15146c.c} {} "pr15146c.so" "c"] \
271   [list "PR ld/15146 (1)" \
272    "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed tmpdir/pr15146a.o tmpdir/pr15146c.so" "" \
273    {dummy.c} {{"readelf" {-d} "pr15146.d"}} "pr15146a.exe"] \
274   [list "Build libpr15146d.a" \
275    "$plug_opt" "-flto -O2" \
276    {pr15146d.c} {} "lib15146d.a"] \
277   [list "Build libpr16746a.a" \
278    "" "" \
279    {pr16746a.c pr16746b.c} {} "lib15146a.a"] \
280   [list "Build libpr16746b.a" \
281    "$plug_opt" "-O2 -flto" \
282    {pr16746c.c pr16746d.c} {} "lib15146b.a"] \
283   [list "PR ld/16746 (1)" \
284    "-O2 -flto -fuse-linker-plugin tmpdir/pr16746a.o tmpdir/pr16746c.o" "-O2 -flto" \
285    {dummy.c} {} "pr16746a.exe"] \
286   [list "PR ld/16746 (2)" \
287    "-O2 -flto -fuse-linker-plugin tmpdir/pr16746c.o tmpdir/pr16746a.o" "-O2 -flto" \
288    {dummy.c} {} "pr16746b.exe"] \
289   [list "Build pr21382a.o" \
290    "" "-O2 -flto" \
291    {pr21382a.c} {} "" "c"] \
292   [list "Build pr21382.so" \
293    "-shared" "-O2 -fpic" \
294    {pr21382b.c} {} "pr21382.so" "c"] \
295   [list {Build pr22220lib.so} \
296    {-shared -Wl,--version-script=pr22220lib.ver} {-fPIC} \
297    {pr22220lib.cc} {} {pr22220lib.so} {c++}] \
298   [list {Build pr22220main.o} \
299    {} {-flto} \
300    {pr22220main.cc} {} {} {c++}] \
301 ]
302
303 # PR 14918 checks that libgcc is not spuriously included in a shared link of
304 # an empty program.  The ARM crt1.o startup code however calls __libc_csu_init
305 # in /usr/lib/libc_nonshared.a(elf-init.oS).  This in turn needs
306 # __aeabi_unwind_cpp_pr0@@GCC_3.5 which is provided by libgcc_s.so.1, so the
307 # test fails.  Hence this code to skip the test.
308 if { ! [istarget "arm*-*-*"] } {
309     set lto_link_elf_tests [concat $lto_link_elf_tests [list \
310   [list "PR ld/14918" \
311    "-flto" "-flto" \
312    {pr14918.c} {{"readelf" {-d --wide} "pr14918.d"}} "pr14918.exe" "c"] \
313   ]]
314 }
315
316 # Check final symbols in executables.
317 set lto_link_symbol_tests [list \
318   [list "LTO 3 symbol" \
319    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" "" \
320    {dummy.c} {{"nm" {} "lto-3.d"}} "lto-3.exe" "c"] \
321   [list "LTO 5 symbol" \
322    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" "" \
323    {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"] \
324   [list "LTO 9 symbol" \
325    "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" "" \
326    {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"] \
327   [list "LTO 16a symbol" \
328    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" "-flto" \
329    {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"] \
330   [list "LTO 16b symbol" \
331    "-O2 -Wl,-e,foo -u bar -nostdlib -flto -fuse-linker-plugin" "-flto" \
332    {lto-16a.c lto-16b.c} {{"nm" {} "lto-16b.d"}} "lto-16b.exe" "c"] \
333   [list "PR ld/13183" \
334    "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" "" \
335    {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"] \
336 ]
337
338 # LTO run-time tests.
339 set lto_run_tests [list \
340   [list "LTO 3a" \
341    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" "" \
342    {dummy.c} "lto-3b.exe" "lto-3.out" "" "c"] \
343   [list "LTO 3b" \
344    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/lto-3.o" "" \
345    {dummy.c} "lto-3c.exe" "lto-3.out" "" "c"] \
346   [list "LTO 3c" \
347    "-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" "" \
348    {dummy.c} "lto-3d.exe" "lto-3.out" "" "c"] \
349   [list "LTO 5" \
350    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" "" \
351    {dummy.c} "lto-5.exe" "lto-5.out" "" "c"] \
352   [list "LTO 11" \
353    "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" "" \
354    {dummy.c} "lto-11.exe" "lto-11.out" "" "c"] \
355   [list "LTO 12a" \
356    "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/liblto-12.a tmpdir/lto-12b.o" "" \
357    {dummy.c} "lto-12a.exe" "lto-12.out" "" "c"] \
358   [list "LTO 12b" \
359    "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/lto-12b.o tmpdir/liblto-12.a" "" \
360    {dummy.c} "lto-12b.exe" "lto-12.out" "" "c"] \
361   [list "LTO 13" \
362    "-O -flto -fuse-linker-plugin tmpdir/lto-13a.o tmpdir/liblto-13.a tmpdir/lto-13b.o" "" \
363    {dummy.c} "lto-13.exe" "lto-13.out" "" "c"] \
364   [list "LTO 14" \
365    "-O2 -flto -fuse-linker-plugin tmpdir/lto-14a.o -Wl,--whole-archive tmpdir/liblto-14.a -Wl,--no-whole-archive tmpdir/lto-14b.o" "" \
366    {dummy.c} "lto-14.exe" "lto-14.out" "" "c"] \
367   [list "LTO 15" \
368    "-O2 -flto -fuse-linker-plugin -Wl,--start-group tmpdir/liblto-15.a tmpdir/lto-15a.o -Wl,--end-group" "" \
369    {dummy.c} "lto-15.exe" "lto-15.out" "" "c"] \
370   [list "PR ld/13066" \
371    "-O2 -flto -fuse-linker-plugin" "" \
372    {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"] \
373   [list "PR ld/13201" \
374    "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" "" \
375    {dummy.c} "pr13201.exe" "pr13201.out" "" "c"] \
376   [list "PR ld/15323 (3)" \
377    "-O2 -flto -fuse-linker-plugin tmpdir/pr15323a.o" "" \
378    {pr15323b.c} "pr15323.exe" "pr15323.out" "-flto -O2" "c"] \
379   [list "PR ld/15323 (4)" \
380    "-O2 -flto tmpdir/pr15323a-r.o" "" \
381    {dummy.c} "pr15323a.exe" "pr15323.out" "-flto -O2" "c"] \
382   [list "PR ld/19317 (3)" \
383    "-O2 -flto tmpdir/pr19317-r.o" "" \
384    {dummy.c} "pr19317.exe" "pr19317.out" "-flto -O2" "c"] \
385   [list "Run pr20276" \
386    "-O2 -flto tmpdir/pr20276a.o tmpdir/pr20276b.o" "" \
387    {dummy.c} "pr20276" "pass.out" "-flto -O2" "c"] \
388   [list "Run pr20267a" \
389    "-O2 -flto tmpdir/pr20267a.o tmpdir/libpr20267a.a" "" \
390    {dummy.c} "pr20267a" "pass.out" "-flto -O2" "c"] \
391   [list "Run pr20267b" \
392    "-O2 -flto tmpdir/pr20267a.o tmpdir/libpr20267b.a" "" \
393    {dummy.c} "pr20267b" "pass.out" "-flto -O2" "c"] \
394 ]
395
396 if { [at_least_gcc_version 4 7] } {
397     set lto_run_tests [concat $lto_run_tests [list \
398       [list "PR ld/12942 (1)" \
399        "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" "" \
400        {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"] \
401       [list "PR ld/12942 (2)" \
402        "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" "" \
403        {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"] \
404     ]]
405 }
406
407 # LTO run-time tests for ELF which require shared library support.
408 set lto_run_elf_shared_tests [list \
409   [list "LTO 7" \
410    "-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" "" \
411    {dummy.c} "lto-7.exe" "lto-7.out" "" "c"] \
412   [list "Run pr21382" \
413    "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr21382a.o tmpdir/pr21382.so" "" \
414    {dummy.c} "pr21382.exe" "pass.out" "" "c"] \
415   [list {pr22220a} \
416    {-flto -fuse-linker-plugin tmpdir/pr22220main.o tmpdir/pr22220lib.so} {} \
417    {dummy.c} {pr22220a.exe} {pass.out} {} {c++}] \
418   [list {pr22220b} \
419    {-flto -fuse-linker-plugin -Wl,--no-as-needed tmpdir/pr22220lib.so tmpdir/pr22220main.o} {} \
420    {dummy.c} {pr22220b.exe} {pass.out} {} {c++}] \
421 ]
422
423 # LTO run-time tests for ELF
424 set lto_run_elf_tests [list \
425   [list "LTO 8" \
426    "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" "" \
427    {dummy.c} "lto-8.exe" "lto-8.out" "" "c"] \
428   [list "LTO TLS IE" \
429    "-O2 -flto -fuse-linker-plugin" "" \
430    {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"] \
431 ]
432
433 run_cc_link_tests $lto_link_tests
434
435 # These compilation tests generate intermediate object files which will be used
436 # by some elf tests besides shared libs tests.  So, always compile them.
437 run_cc_link_tests $lto_compile_elf_tests
438
439 # Restrict these to ELF targets that support shared libs and PIC.
440 if { [is_elf_format] && [check_lto_shared_available] } {
441     run_cc_link_tests $lto_link_elf_tests
442     set testname "PR ld/15146 (2)"
443     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"]
444     if { [ regexp "undefined reference to symbol '\\.?xxx'" $exec_output ] } {
445         pass $testname
446     } {
447         fail $testname
448     }
449     set testname "PR ld/16746 (3)"
450     set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746b.o tmpdir/pr16746d.o"]
451     if { [ regexp "warning: \\.?foobar" $exec_output ] && ![ regexp "symbol from plugin" $exec_output ] } {
452         pass $testname
453     } {
454         fail $testname
455     }
456     set testname "PR ld/16746 (4)"
457     set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746d.o tmpdir/pr16746b.o"]
458     if { [ regexp "warning: \\.?foobar" $exec_output ] && ![ regexp "symbol from plugin" $exec_output ] } {
459         pass $testname
460     } {
461         fail $testname
462     }
463 }
464
465 set testname "Build liblto-11.a"
466 remote_file host delete "tmpdir/liblto-11.a"
467 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"]
468 if {![string match "" $catch_output]} {
469     unresolved $testname
470     restore_notify
471     return
472 }
473
474 if { [at_least_gcc_version 4 7] } {
475     # Check expected LTO linker errors.
476     # Since the asm symbol name hack in pr12365b.c doesn't work on all
477     # targets, run PR ld/12365 tests only for known targets.
478     if { ([istarget "i?86-*-elf*"]
479            || (([istarget "i?86-*-linux*"] || [istarget "i?86-*-gnu*"])
480                 && ![istarget "*-*-*aout*"]
481                 && ![istarget "*-*-*oldld*"])
482            || [istarget "i?86-*-nacl*"]
483            || [istarget "x86_64-*-nacl*"]
484            || [istarget "x86_64-*-linux*"]
485            || [istarget "amd64-*-linux*"]) } {
486         set testname "PR ld/12365"
487         set exec_output [run_host_cmd "$CC" "-O2 -flto -flto-partition=none -fuse-linker-plugin -o tmpdir/pr12365 tmpdir/pr12365a.o tmpdir/pr12365b.o tmpdir/pr12365c.o"]
488         if { [ regexp "undefined reference to `my_bcopy'" $exec_output ] } {
489             # Linker should catch the reference to undefined `my_bcopy'
490             # error caused by a GCC bug.
491             pass $testname
492         } elseif { [ string match "" $exec_output ] } {
493             global READELF
494             set exec_output [run_host_cmd "$READELF" "-s -W tmpdir/pr12365"]
495             if { [ regexp "my_bcopy" $exec_output ] } {
496                 # Verify that there is no `my_bcopy' symbol in executable.
497                 fail $testname
498             } {
499                 pass $testname
500             }
501         } {
502             fail $testname
503         }
504     }
505     set testname "PR ld/12942 (3)"
506     set exec_output [run_host_cmd "$CXX" "-O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
507     if { [ regexp "undefined reference to `\\.?link_error\\(\\)'" $exec_output ] } {
508         pass $testname
509     } {
510         fail $testname
511     }
512 }
513
514 # Run "ld -r" to generate inputs for complex LTO tests.
515 run_dump_test "lto-3r"
516 remote_exec host "mv" "tmpdir/dump tmpdir/lto-3.o"
517 run_dump_test "lto-5r"
518 remote_exec host "mv" "tmpdir/dump tmpdir/lto-5.o"
519
520 run_cc_link_tests $lto_link_symbol_tests
521
522 run_ld_link_tests [list \
523   [list "PR ld/19317 (2)" \
524    "-r tmpdir/pr19317.o" "" "" \
525    {dummy.s} {} "pr19317-r.o"] \
526 ]
527
528 run_ld_link_exec_tests $lto_run_tests
529
530 if { [is_elf_format] } {
531     run_ld_link_exec_tests $lto_run_elf_tests
532 }
533
534 if { [is_elf_format] && [check_lto_shared_available] } {
535     run_ld_link_exec_tests $lto_run_elf_shared_tests
536 }
537
538 proc pr20103 {cflags libs} {
539     global CC
540
541     set testname "PR ld/20103 ($cflags $libs)"
542     set exec_output [run_host_cmd "$CC" "$cflags $libs"]
543     if { [ regexp "undefined reference to `\\.?dead'" $exec_output ] } {
544         pass "$testname (1)"
545     } {
546         fail "$testname (1)"
547     }
548     if { [ regexp "plugin needed to handle lto object" $exec_output ] } {
549         fail "$testname (2)"
550     } {
551         pass "$testname (2)"
552     }
553 }
554
555 if { [check_lto_fat_available] } {
556     run_cc_link_tests [list \
557         [list \
558             "Build fatpr20103a.a" \
559             "$plug_opt" "-flto -ffat-lto-objects" \
560             {pr20103a.c} {} "fatpr20103a.a"
561         ] \
562         [list \
563             "Build fatpr20103b.a" \
564             "$plug_opt" "-flto -ffat-lto-objects" \
565             {pr20103b.c} {} "fatpr20103b.a"
566         ] \
567         [list \
568             "Build fatpr20103c.a" \
569             "$plug_opt" "-flto -ffat-lto-objects" \
570             {pr20103c.c} {} "fatpr20103c.a" \
571         ] \
572         [list \
573             "Build thinpr20103a.a" \
574             "$plug_opt" "-flto -fno-fat-lto-objects" \
575             {pr20103a.c} {} "thinpr20103a.a"
576         ] \
577         [list \
578             "Build thinpr20103b.a" \
579             "$plug_opt" "-flto -fno-fat-lto-objects" \
580             {pr20103b.c} {} "thinpr20103b.a"
581         ] \
582         [list \
583             "Build thinpr20103c.a" \
584             "$plug_opt" "-flto -fno-fat-lto-objects" \
585             {pr20103c.c} {} "thinpr20103c.a" \
586         ] \
587         [list \
588             "Build pr20103a" \
589             "-O2 -flto -Wl,--start-group tmpdir/thinpr20103a.a tmpdir/thinpr20103b.a tmpdir/thinpr20103c.a -Wl,--end-group" \
590             "-O2 -flto" \
591             {dummy.c} {} "pr20103a" \
592         ] \
593         [list \
594             "Build pr20103b" \
595             "-O2 -flto -Wl,--start-group tmpdir/fatpr20103a.a tmpdir/fatpr20103b.a tmpdir/fatpr20103c.a -Wl,--end-group" \
596             "-O2 -flto" \
597             {dummy.c} {} "pr20103b" \
598         ] \
599         [list \
600             "Build pr20103c" \
601             "-O2 -Wl,--start-group tmpdir/fatpr20103a.a tmpdir/fatpr20103b.a tmpdir/fatpr20103c.a -Wl,--end-group" \
602             "-O2" \
603             {dummy.c} {} "pr20103c" \
604         ] \
605     ]
606     pr20103 "-O2 -flto" "tmpdir/thinpr20103a.a tmpdir/thinpr20103b.a tmpdir/thinpr20103c.a"
607     pr20103 "-O2 -flto" "tmpdir/fatpr20103a.a tmpdir/fatpr20103b.a tmpdir/fatpr20103c.a"
608     pr20103 "-O2" "tmpdir/fatpr20103a.a tmpdir/fatpr20103b.a tmpdir/fatpr20103c.a"
609
610     if { [at_least_gcc_version 4 9] } {
611         run_cc_link_tests [list \
612             [list \
613                 "Build pr20103d" \
614                 "-O2 -Wl,--start-group tmpdir/thinpr20103a.a tmpdir/thinpr20103b.a tmpdir/thinpr20103c.a -Wl,--end-group" \
615                 "-O2" \
616                 {dummy.c} {} "pr20103d" \
617             ] \
618         ]
619         pr20103 "-O2" "tmpdir/thinpr20103a.a tmpdir/thinpr20103b.a tmpdir/thinpr20103c.a"
620     }
621 }
622
623 restore_notify