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