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